diff --git a/server/src/nest/assistant/assistant-file-answer.service.ts b/server/src/nest/assistant/assistant-file-answer.service.ts new file mode 100644 index 000000000..b059922cd --- /dev/null +++ b/server/src/nest/assistant/assistant-file-answer.service.ts @@ -0,0 +1,130 @@ +import { logError } from '../audit/audit-log.logger'; +import { withRequestContext } from '../database/request-context'; +import type { HeldCall } from './assistant-agent'; +import type { AttachedFileReader, FileRead } from './assistant-attached-file'; +import { todayIn } from './assistant-context'; +import { AssistantToolsService } from './assistant-tools.service'; +import { readTripCard } from './assistant-trip-scope'; +import { AssistantCardContext } from './cards/card-context'; +import type { UiStreamWriter } from './ui-stream.writer'; +import { MikroORM } from '@mikro-orm/core'; +import { Injectable } from '@nestjs/common'; +import type { AssistantClientCtx, AssistantMessagePart } from '@trek/shared'; + +import { randomUUID } from 'node:crypto'; + +/** What the read step of an attached file answers when the file could not be read. */ +const FILE_UNREADABLE = 'The file could not be read.'; + +/** The turn a file was attached in: who, on which trip, the thread and the answer's first row. */ +export interface FileTurn extends AssistantClientCtx { + userId: number; + tripId: number; + threadId: number; + rowId: number; + name: string; +} + +/** How the answer's row is written: AssistantService's, which owns the rows and their transactions. */ +export interface FileAnswerRows { + save(parts: AssistantMessagePart[], status: 'complete' | 'aborted' | 'error'): Promise; + /** Saves the row waiting for the user, together with the changes it holds. */ + hold(parts: AssistantMessagePart[], held: HeldCall[]): Promise; +} + +/** Where the answer goes: the stream, the signal that the client went away, and the row. */ +export interface FileAnswerOut { + w: UiStreamWriter; + signal: AbortSignal; + rows: FileAnswerRows; +} + +/** + * The answer to a file the user attached in the trip chat, written by the + * server itself, in the answer's first row: the read step, then one held + * change per booking (`import_booking`) or track (`import_tour_file`), each + * with its card. No model is asked. + */ +@Injectable() +export class AssistantFileAnswers { + constructor( + private readonly tools: AssistantToolsService, + private readonly cardContext: AssistantCardContext, + private readonly orm: MikroORM, + ) {} + + /** + * Reads the attached file and proposes what it found. A file without any + * ends the answer with the reader's code; one that cannot be read, with + * `generic`. + * + * @txIndependent the row is written once the file was read, in a + * transaction of its own. + */ + async answer(turn: FileTurn, file: Express.Multer.File, reader: AttachedFileReader, out: FileAnswerOut) { + const { w, signal, rows } = out; + const readId = randomUUID(); + const readArgs = { name: turn.name }; + const readPart = (result: unknown, isError = false): AssistantMessagePart => ({ + type: 'tool-call', + toolCallId: readId, + toolName: reader.readTool, + args: readArgs, + result, + ...(isError ? { isError: true } : {}), + }); + w.startStep(); + w.toolInput(readId, reader.readTool, readArgs); + let read: FileRead; + try { + read = await withRequestContext(this.orm, () => reader.propose(turn.userId, turn.tripId, file)); + } catch (err) { + logError(`[assistant] reading ${turn.name} failed: ${err instanceof Error ? err.message : String(err)}`); + await rows.save([readPart(FILE_UNREADABLE, true)], 'error'); + w.toolError(readId, FILE_UNREADABLE); + w.error('generic'); + w.finish('error'); + return; + } + const { found, proposals } = read; + if (signal.aborted) { + await rows.save([readPart(found)], 'aborted'); + w.abort(); + return; + } + w.toolOutput(readId, found); + if (proposals.length === 0) { + await rows.save([readPart(found)], 'error'); + w.finishStep(); + w.error(reader.emptyCode); + w.finish('error'); + return; + } + const card = await readTripCard(this.tools, turn.userId, turn.tripId); + const ctx = this.cardContext.contextOf(card, new Map(), turn.locale, todayIn(turn.timeZone, new Date())); + const proposed = proposals.map((proposal) => ({ ...proposal, id: randomUUID() })); + // The thread shows a call as the reader shows it; the held call keeps everything it runs with. + const shown = new Map>(proposed.map((c) => [c.id, c.shownArgs ?? c.args])); + const described = await this.cardContext.describeProposed(turn.userId, turn.tripId, ctx, proposed); + const held: HeldCall[] = described.map((change) => ({ ...change, providerCallId: `import_${change.id}` })); + const parts: AssistantMessagePart[] = [ + readPart(found), + ...held.map((change): AssistantMessagePart => ({ + type: 'tool-call', + toolCallId: change.id, + toolName: change.toolName, + args: shown.get(change.id) ?? change.args, + approval: { id: change.id }, + })), + ...held.map((change): AssistantMessagePart => ({ type: 'data-trek-change', data: change.preview })), + ]; + await rows.hold(parts, held); + for (const change of held) { + w.toolInput(change.id, change.toolName, shown.get(change.id) ?? change.args); + w.approvalRequest(change.id); + w.change(change.preview); + } + w.finishStep(); + w.finish('tool-calls'); + } +} diff --git a/server/src/nest/assistant/assistant.module.ts b/server/src/nest/assistant/assistant.module.ts index bb96f0347..128c19874 100644 --- a/server/src/nest/assistant/assistant.module.ts +++ b/server/src/nest/assistant/assistant.module.ts @@ -21,6 +21,7 @@ import { AssistantAttachmentsController } from './assistant-attachments.controll import { AssistantBookContext } from './assistant-book-context'; import { AssistantBookGate } from './assistant-book-gate'; import { AssistantBookingImportService } from './assistant-booking-import.service'; +import { AssistantFileAnswers } from './assistant-file-answer.service'; import { AssistantFoundPlacesService } from './assistant-found-places.service'; import { AssistantProposalsService } from './assistant-proposals.service'; import { AssistantRenderRegistry, InMemoryAssistantRenderRegistry } from './assistant-render.registry'; @@ -99,6 +100,7 @@ import { Module } from '@nestjs/common'; AssistantService, AssistantToolsService, AssistantBookingImportService, + AssistantFileAnswers, AssistantTourFileService, AssistantFoundPlacesService, AssistantProposalsService, diff --git a/server/src/nest/assistant/assistant.service.ts b/server/src/nest/assistant/assistant.service.ts index 4f31c32d6..9be2f3b15 100644 --- a/server/src/nest/assistant/assistant.service.ts +++ b/server/src/nest/assistant/assistant.service.ts @@ -38,6 +38,7 @@ import { AssistantBookingImportService } from './assistant-booking-import.servic import { mentionItems } from './assistant-cards.helpers'; import { todayIn } from './assistant-context'; import { DISCARDED, NOT_RUN, type Outcome, withCancelled, withOutcomes } from './assistant-decisions.helpers'; +import { AssistantFileAnswers, type FileAnswerRows } from './assistant-file-answer.service'; import { messageIdOf, outputText, partsOf, toChatHistory, toThreadMessages } from './assistant-history'; import { rememberForTrip, rememberingPort } from './assistant-memory'; import { toolOutputForProvider } from './assistant-output'; @@ -69,7 +70,6 @@ import { Injectable } from '@nestjs/common'; import type { AssistantAttachmentRequest, AssistantChatRequest, - AssistantClientCtx, AssistantDecision, AssistantDecisionsRequest, AssistantFocus, @@ -91,9 +91,6 @@ const THREAD_ROWS = 50; /** Message rows a turn sends the provider. */ const HISTORY_ROWS = 120; -/** What the read step of an attached file answers when the file could not be read. */ -const FILE_UNREADABLE = 'The file could not be read.'; - /** The longest file name a message keeps. */ const MAX_FILE_NAME_CHARS = 255; @@ -172,6 +169,7 @@ export class AssistantService { private readonly usage: AiUsageService, private readonly tools: AssistantToolsService, private readonly bookings: AssistantBookingImportService, + private readonly fileAnswers: AssistantFileAnswers, private readonly tourFiles: AssistantTourFileService, private readonly settings: SettingsService, private readonly rateLimit: RateLimitService, @@ -255,8 +253,9 @@ export class AssistantService { * A file the user attached, in a thread of theirs on this trip or (no * threadId) a new one, answered as a stream: the file is read into bookings, * or a route file (GPX, KML, KMZ) into its tracks, and each is proposed as a - * change card, applied or discarded like any other. No model is asked here; the decision on the cards starts the next - * answer. Refused like a chat turn, and also when nothing here reads files. + * change card, applied or discarded like any other (AssistantFileAnswers). + * No model is asked here; the decision on the cards starts the next answer. + * Refused like a chat turn, and also when nothing here reads files. * * @txIndependent the opening rows are one transaction and the proposals * another, with the file being read between them. @@ -286,7 +285,7 @@ export class AssistantService { try { const { locale, timeZone } = req; const turn = { userId, tripId, threadId: begun.threadId, rowId: begun.rowId, locale, timeZone, name }; - await this.proposeFromFile(turn, file, reader, w, signal); + await this.fileAnswers.answer(turn, file, reader, { w, signal, rows: this.answerRows(turn) }); } catch (err) { const reason = err instanceof Error ? err.message : String(err); logError(`[assistant] attachment in thread ${begun.threadId} failed: ${reason}`); @@ -541,86 +540,15 @@ export class AssistantService { } /** - * Reads the attached file and proposes what it found, in the answer's first - * row: the read step, then one held change per booking (`import_booking`) - * or track (`import_tour_file`), each with its card. A file without any - * ends the answer with the reader's code; one that cannot be read, with - * `generic`. - * - * @txIndependent the row is written once the file was read, in a - * transaction of its own. + * How the answer to an attached file writes its first row. + * @txIndependent hands out two ways to write the row; an answer takes one, and each is its own transaction. */ - private async proposeFromFile( - turn: { userId: number; tripId: number; threadId: number; rowId: number; name: string } & AssistantClientCtx, - file: Express.Multer.File, - reader: AttachedFileReader, - w: UiStreamWriter, - signal: AbortSignal, - ): Promise { - const readId = randomUUID(); - const readArgs = { name: turn.name }; - const readPart = (result: unknown, isError = false): AssistantMessagePart => ({ - type: 'tool-call', - toolCallId: readId, - toolName: reader.readTool, - args: readArgs, - result, - ...(isError ? { isError: true } : {}), - }); + private answerRows(turn: { threadId: number; rowId: number }): FileAnswerRows { const fieldsOf = (parts: AssistantMessagePart[]) => ({ parts: encodeParts(parts), providerState: null }); - w.startStep(); - w.toolInput(readId, reader.readTool, readArgs); - let read: Awaited>; - try { - read = await this.inContext(() => reader.propose(turn.userId, turn.tripId, file)); - } catch (err) { - logError(`[assistant] reading ${turn.name} failed: ${err instanceof Error ? err.message : String(err)}`); - await this.saveRow(turn.threadId, turn.rowId, fieldsOf([readPart(FILE_UNREADABLE, true)]), 'error'); - w.toolError(readId, FILE_UNREADABLE); - w.error('generic'); - w.finish('error'); - return; - } - const { found, proposals } = read; - if (signal.aborted) { - await this.saveRow(turn.threadId, turn.rowId, fieldsOf([readPart(found)]), 'aborted'); - w.abort(); - return; - } - w.toolOutput(readId, found); - if (proposals.length === 0) { - await this.saveRow(turn.threadId, turn.rowId, fieldsOf([readPart(found)]), 'error'); - w.finishStep(); - w.error(reader.emptyCode); - w.finish('error'); - return; - } - const card = await readTripCard(this.tools, turn.userId, turn.tripId); - const ctx = this.cardContext.contextOf(card, new Map(), turn.locale, todayIn(turn.timeZone, new Date())); - const proposed = proposals.map((proposal) => ({ ...proposal, id: randomUUID() })); - // The thread shows a call as the reader shows it; the held call keeps everything it runs with. - const shown = new Map>(proposed.map((c) => [c.id, c.shownArgs ?? c.args])); - const described = await this.cardContext.describeProposed(turn.userId, turn.tripId, ctx, proposed); - const held: HeldCall[] = described.map((change) => ({ ...change, providerCallId: `import_${change.id}` })); - const parts: AssistantMessagePart[] = [ - readPart(found), - ...held.map((change): AssistantMessagePart => ({ - type: 'tool-call', - toolCallId: change.id, - toolName: change.toolName, - args: shown.get(change.id) ?? change.args, - approval: { id: change.id }, - })), - ...held.map((change): AssistantMessagePart => ({ type: 'data-trek-change', data: change.preview })), - ]; - await this.holdChanges(turn.threadId, turn.rowId, fieldsOf(parts), held); - for (const change of held) { - w.toolInput(change.id, change.toolName, shown.get(change.id) ?? change.args); - w.approvalRequest(change.id); - w.change(change.preview); - } - w.finishStep(); - w.finish('tool-calls'); + return { + save: (parts, status) => this.saveRow(turn.threadId, turn.rowId, fieldsOf(parts), status), + hold: (parts, held) => this.holdChanges(turn.threadId, turn.rowId, fieldsOf(parts), held), + }; } private async cancelOpen(threadId: number, now: string): Promise {