fix(assistant): green CI round after the shared ChatGPT plan

- Tests that still knew the ChatGPT sign-in as personal only follow the
  admin's shared plan: the service takes the caller with their role, the
  admin tiles show ChatGPT, the controller tests cover the admin routes.
- The table refused an instance ChatGPT connection with a CHECK, so
  sharing the plan failed. A new migration rebuilds ai_connections
  without that check; the shipped migration stays as it was.
- useChatgptConnect grew past the function size limit: reading the
  clipboard and noticing the return to the tab are helpers of their own.
- The file link factory hands its overrides over apart from the row,
  like every other factory, which ends the deep type instantiation.
- Knip: exports only their own file used are private again, and the
  booking cap reads MAX_WRITES_PER_STEP instead of an alias.
- lint:i18n-keys reaches the tools' own action names through a key
  builder and the connection hints through literal keys.
- MMehrSheet stays in the assistant hints pair only.
This commit is contained in:
Maurice
2026-10-21 21:40:44 +00:00
parent 70c7d1ba3f
commit f0a3fe9dbb
23 changed files with 238 additions and 98 deletions
@@ -0,0 +1,55 @@
import { Migration } from '@mikro-orm/migrations';
/**
* An admin may share their own ChatGPT sign-in with everyone, so a ChatGPT
* connection can be the instance connection too. The table refused that with
* `CHECK(kind <> 'chatgpt' OR scope = 'user')`, and saving the shared plan
* failed on it.
*
* SQLite cannot drop a CHECK in place, so the table is rebuilt without that
* one; every other check stays as it was. Nothing references
* `ai_connections`, so the old table can go before the new one takes its
* name. Ids are copied, so a connection keeps its id.
*/
export class Migration20200101046230_an_admin_can_share_a_chatgpt_plan extends Migration {
override name = 'Migration20200101046230_an_admin_can_share_a_chatgpt_plan';
override up(): void {
this.addSql(`
CREATE TABLE ai_connections_new (
id INTEGER PRIMARY KEY,
scope TEXT NOT NULL CHECK(scope IN ('instance', 'user')),
user_id INTEGER REFERENCES users(id) ON DELETE CASCADE,
kind TEXT NOT NULL CHECK(kind IN ('anthropic', 'openai', 'gemini', 'openrouter', 'chatgpt', 'custom')),
model TEXT NOT NULL,
base_url TEXT,
secrets TEXT,
secrets_version INTEGER NOT NULL DEFAULT 0,
hint TEXT,
account TEXT,
status TEXT NOT NULL DEFAULT 'ok' CHECK(status IN ('ok', 'needs_reauth', 'signed_out', 'error')),
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
CHECK((scope = 'user') = (user_id IS NOT NULL)),
CHECK(kind <> 'custom' OR (scope = 'instance' AND base_url IS NOT NULL)),
CHECK(base_url IS NULL OR kind = 'custom')
)
`);
this.addSql(`
INSERT INTO ai_connections_new (
id, scope, user_id, kind, model, base_url, secrets, secrets_version, hint, account, status, created_at, updated_at
)
SELECT
id, scope, user_id, kind, model, base_url, secrets, secrets_version, hint, account, status, created_at, updated_at
FROM ai_connections
`);
this.addSql('DROP TABLE ai_connections');
this.addSql('ALTER TABLE ai_connections_new RENAME TO ai_connections');
this.addSql(
'CREATE UNIQUE INDEX IF NOT EXISTS idx_ai_connections_user ON ai_connections(user_id) WHERE user_id IS NOT NULL',
);
this.addSql(
'CREATE UNIQUE INDEX IF NOT EXISTS idx_ai_connections_instance ON ai_connections(scope) WHERE user_id IS NULL',
);
}
}
@@ -3,7 +3,7 @@ import { BookingImportService } from '../booking-import/booking-import.service';
import { DemoService } from '../common/demo.service';
import { McpToolGuardsService } from '../mcp-shared/mcp-tool-guards.service';
import { TripAccessService } from '../trip-membership/trip-access.service';
import { MAX_BOOKINGS_PER_FILE } from './assistant-tools.constants';
import { MAX_WRITES_PER_STEP } from './assistant-tools.constants';
import type { ToolRun } from './assistant-tools.service';
import { Injectable } from '@nestjs/common';
import { bookingImportPreviewItemSchema, LLM_PHOTO_EXTENSIONS, type BookingImportPreviewItem } from '@trek/shared';
@@ -54,7 +54,8 @@ export class AssistantBookingImportService {
async read(userId: number, file: Express.Multer.File): Promise<BookingImportPreviewItem[]> {
const mode = (await this.bookingImport.aiAvailable(userId)) ? 'fallback-on-empty' : 'no-ai';
const preview = await this.bookingImport.preview([file], mode, userId);
return preview.items.slice(0, MAX_BOOKINGS_PER_FILE);
// One file proposes no more bookings than one step may hold changes; the rest are left out.
return preview.items.slice(0, MAX_WRITES_PER_STEP);
}
/**
@@ -3,7 +3,7 @@ import type { AssistantMessagePart } from '@trek/shared';
/** What the model is told about a change the user discarded. */
export const DISCARDED = { applied: false, reason: 'The user discarded this change.' };
/** ... one the user left undecided by asking something else. */
export const CANCELLED = { applied: false, reason: 'The user moved on without deciding.' };
const CANCELLED = { applied: false, reason: 'The user moved on without deciding.' };
/** ... one that was approved but never ran. */
export const NOT_RUN = { applied: false, reason: 'The change could not be run.' };
@@ -163,9 +163,6 @@ export const ASSISTANT_SERVER_TOOLS: ReadonlySet<string> = new Set([
...Object.keys(ASSISTANT_SERVER_WRITE_TOOLS),
]);
/** Bookings one attached file may propose; the rest are left out, as a step holds no more changes. */
export const MAX_BOOKINGS_PER_FILE = MAX_WRITES_PER_STEP;
/** The largest file the assistant reads. */
export const MAX_ATTACHMENT_BYTES = 10 * 1024 * 1024;
@@ -79,7 +79,7 @@ function clip(value: string): string {
}
/** "update_vacay_plan" reads as "update vacay plan"; only shown where nothing better is known. */
export function humanToolName(name: string): string {
function humanToolName(name: string): string {
return name.replace(/_/g, ' ');
}