mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-04 01:44:13 +00:00
feat(assistant): an attached file without a booking says why
The chat's booking reader kept only the bookings of the import preview and dropped its warnings, so "AI reading is not set up", "the AI failed", "a scan without text" and "nothing in it" all came out as the same no_bookings_found. The reader now reads the reason from the preview (the mode it ran in and the AI fallback's warnings) and keeps it in the stored read step, and the note the model is sent names it.
This commit is contained in:
@@ -7,7 +7,12 @@ import type { AttachedFileReader, FileRead } from './assistant-attached-file';
|
||||
import { IMPORT_BOOKING_TOOL, MAX_WRITES_PER_STEP, READ_BOOKING_FILE_TOOL } 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';
|
||||
import {
|
||||
bookingImportPreviewItemSchema,
|
||||
LLM_PHOTO_EXTENSIONS,
|
||||
type BookingImportPreviewItem,
|
||||
type BookingImportPreviewResponse,
|
||||
} from '@trek/shared';
|
||||
|
||||
import { extname } from 'node:path';
|
||||
|
||||
@@ -17,6 +22,37 @@ const NOT_ALLOWED = 'You are not allowed to add bookings to this trip.';
|
||||
const NOT_ADDED = 'The booking could not be added.';
|
||||
const BROKEN = 'This booking can no longer be read.';
|
||||
|
||||
/**
|
||||
* Why a file held no booking: AI reading is not set up for imports (only the
|
||||
* extractor looked), the AI could not read it, it holds no text (a scan the
|
||||
* model cannot see), or nothing in it reads as a booking.
|
||||
*/
|
||||
export const BOOKING_READ_REASONS = ['ai_not_configured', 'ai_failed', 'no_text', 'nothing_found'] as const;
|
||||
export type BookingReadReason = (typeof BOOKING_READ_REASONS)[number];
|
||||
|
||||
/** What reading a file found: at most a step's worth of bookings, and why there were none. */
|
||||
export interface BookingRead {
|
||||
items: BookingImportPreviewItem[];
|
||||
/** Null when the file held bookings. */
|
||||
reason: BookingReadReason | null;
|
||||
}
|
||||
|
||||
/** The warnings of the AI fallback that say it could not read the file (llm-parse.service.ts words them). */
|
||||
const AI_FAILED = [/AI parsing failed/, /could not read file/, /does not read images/];
|
||||
const NO_TEXT = /no readable text found/;
|
||||
|
||||
/**
|
||||
* Why a preview of one file came back empty, from its warnings and its file
|
||||
* report. The import dialog shows those warnings as text; the chat names the
|
||||
* reason in its answer instead, so it reads them here once.
|
||||
*/
|
||||
export function emptyReason(preview: BookingImportPreviewResponse, aiAvailable: boolean): BookingReadReason {
|
||||
if (!aiAvailable) return 'ai_not_configured';
|
||||
if (preview.warnings.some((warning) => NO_TEXT.test(warning))) return 'no_text';
|
||||
if (preview.warnings.some((warning) => AI_FAILED.some((pattern) => pattern.test(warning)))) return 'ai_failed';
|
||||
return 'nothing_found';
|
||||
}
|
||||
|
||||
/**
|
||||
* The booking import as the assistant uses it: a file attached in the chat is
|
||||
* read into bookings with the same extractor (and AI fallback) the import
|
||||
@@ -52,21 +88,26 @@ export class AssistantBookingImportService implements AttachedFileReader {
|
||||
}
|
||||
|
||||
/**
|
||||
* The bookings in the file, at most a step's worth; the AI is asked only
|
||||
* where the extractor found none. Persists nothing.
|
||||
* The bookings in the file, at most a step's worth, and why there were
|
||||
* none; the AI is asked only where the extractor found none. Persists
|
||||
* nothing.
|
||||
*/
|
||||
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);
|
||||
async read(userId: number, file: Express.Multer.File): Promise<BookingRead> {
|
||||
const ai = await this.bookingImport.aiAvailable(userId);
|
||||
const preview = await this.bookingImport.preview([file], ai ? 'fallback-on-empty' : 'no-ai', userId);
|
||||
// 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);
|
||||
const items = preview.items.slice(0, MAX_WRITES_PER_STEP);
|
||||
return { items, reason: items.length > 0 ? null : emptyReason(preview, ai) };
|
||||
}
|
||||
|
||||
/** The bookings of the file as the changes the chat holds, one import_booking each. */
|
||||
/**
|
||||
* The bookings of the file as the changes the chat holds, one
|
||||
* import_booking each; a file without any says why in its read step.
|
||||
*/
|
||||
async propose(userId: number, _tripId: number, file: Express.Multer.File): Promise<FileRead> {
|
||||
const items = await this.read(userId, file);
|
||||
const { items, reason } = await this.read(userId, file);
|
||||
return {
|
||||
found: { bookings: items.map((item) => item.title) },
|
||||
found: { bookings: items.map((item) => item.title), ...(reason ? { reason } : {}) },
|
||||
proposals: items.map((item) => ({ toolName: IMPORT_BOOKING_TOOL, args: item as Record<string, unknown> })),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -118,6 +118,26 @@ function takenProposalOutcome(part: ToolCallPart): string {
|
||||
return `The user took the proposed ${what} ${name} to add it; it waits for them to add or discard it.`;
|
||||
}
|
||||
|
||||
/** Why a booking file held no booking, as the model is told it (BookingReadReason); a reason not listed adds nothing. */
|
||||
const EMPTY_REASONS: Readonly<Record<string, string>> = {
|
||||
ai_not_configured: "AI reading for imports is not set up on this server, so only TREK's own reader looked at it",
|
||||
ai_failed: 'the AI reader for imports could not read it',
|
||||
no_text: 'it holds no readable text, most likely a scan',
|
||||
nothing_found: "neither TREK's reader nor the AI reader found one",
|
||||
};
|
||||
|
||||
/** What reading an attached booking file found, as its stored step says. */
|
||||
function bookingReadNote(part: ToolCallPart): string {
|
||||
const file = quoted(part.args.name);
|
||||
if (part.isError) return `TREK could not read the attached file ${file}.`;
|
||||
const result = (part.result ?? {}) as { bookings?: unknown; reason?: unknown };
|
||||
const titles = Array.isArray(result.bookings) ? result.bookings.map(quoted) : [];
|
||||
if (titles.length > 0)
|
||||
return `TREK read the attached file ${file} and found ${titles.length} booking(s): ${titles.join(', ')}.`;
|
||||
const why = typeof result.reason === 'string' ? EMPTY_REASONS[result.reason] : undefined;
|
||||
return `TREK read the attached file ${file} and found no booking in it${why ? `: ${why}` : ''}.`;
|
||||
}
|
||||
|
||||
/** The note of a server step about tours (P6, P7), null for any other. */
|
||||
function tourStepNote(part: ToolCallPart): string | null {
|
||||
if (part.toolName === ADD_FOUND_PLACE_TOOL && part.args.via === 'insert_tour_stop') return tourStopOutcome(part);
|
||||
@@ -139,12 +159,7 @@ export function serverStepsNote(calls: readonly ToolCallPart[]): string {
|
||||
if (tourNote !== null) {
|
||||
lines.push(tourNote);
|
||||
} else if (part.toolName === READ_BOOKING_FILE_TOOL) {
|
||||
const result = (part.result ?? {}) as { bookings?: unknown };
|
||||
const titles = Array.isArray(result.bookings) ? result.bookings.map(quoted) : [];
|
||||
const file = quoted(part.args.name);
|
||||
const found = titles.length === 0 ? 'no booking in it' : `${titles.length} booking(s): ${titles.join(', ')}`;
|
||||
const read = `TREK read the attached file ${file} and found ${found}.`;
|
||||
lines.push(part.isError ? `TREK could not read the attached file ${file}.` : read);
|
||||
lines.push(bookingReadNote(part));
|
||||
} else if (part.toolName === IMPORT_BOOKING_TOOL) {
|
||||
lines.push(bookingOutcome(part));
|
||||
} else if (part.toolName === ADD_FOUND_PLACE_TOOL) {
|
||||
|
||||
Reference in New Issue
Block a user