Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

feat(assistant): an attached file without a booking says why

Maurice41fb5759e227
diff --git a/server/src/nest/assistant/assistant-booking-import.service.ts b/server/src/nest/assistant/assistant-booking-import.service.ts
index 3981beeb7..f1f448d79 100644
--- a/server/src/nest/assistant/assistant-booking-import.service.ts
+++ b/server/src/nest/assistant/assistant-booking-import.service.ts
@@ -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> })),
};
}
diff --git a/server/src/nest/assistant/assistant-history.ts b/server/src/nest/assistant/assistant-history.ts
index ccd6e29a6..30c22cf8d 100644
--- a/server/src/nest/assistant/assistant-history.ts
+++ b/server/src/nest/assistant/assistant-history.ts
@@ -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) {
diff --git a/server/tests/unit/nest/assistant/assistant-booking-import.service.test.ts b/server/tests/unit/nest/assistant/assistant-booking-import.service.test.ts
index 5657b2c07..1fda6c83b 100644
--- a/server/tests/unit/nest/assistant/assistant-booking-import.service.test.ts
+++ b/server/tests/unit/nest/assistant/assistant-booking-import.service.test.ts
@@ -4,7 +4,10 @@
* step's worth of bookings), and the checks a booking passes when the user
* applies it, the same an MCP write runs.
*/
-import { AssistantBookingImportService } from '../../../../src/nest/assistant/assistant-booking-import.service';
+import {
+ AssistantBookingImportService,
+ emptyReason,
+} from '../../../../src/nest/assistant/assistant-booking-import.service';
import { MAX_WRITES_PER_STEP } from '../../../../src/nest/assistant/assistant-tools.constants';
import { BookingImportService } from '../../../../src/nest/booking-import/booking-import.service';
import { DemoService } from '../../../../src/nest/common/demo.service';
@@ -85,15 +88,39 @@ describe('AssistantBookingImportService', () => {
it("ASTBKI-002: reads with the AI only as a fallback, and keeps a step's worth of bookings", async () => {
const plain = await make();
- const items = await plain.service.read(7, file('a.pdf'));
+ const { items, reason } = await plain.service.read(7, file('a.pdf'));
const given = expect.objectContaining({ originalname: 'a.pdf' });
expect(plain.bookingImport.preview).toHaveBeenCalledWith([given], 'no-ai', 7);
expect(items).toHaveLength(MAX_WRITES_PER_STEP);
+ expect(reason).toBeNull();
const ai = await make({ ai: true });
await ai.service.read(7, file('a.pdf'));
expect(ai.bookingImport.preview.mock.calls[0][1]).toBe('fallback-on-empty');
});
+ it('ASTBKI-005: a file without a booking says why, in the read step the chat keeps', async () => {
+ const t = await make();
+ t.bookingImport.preview.mockResolvedValueOnce({ items: [], warnings: ['a.pdf: no reservations found'] });
+ expect(await t.service.propose(7, 5, file('a.pdf'))).toEqual({
+ found: { bookings: [], reason: 'ai_not_configured' },
+ proposals: [],
+ });
+ const found = await make();
+ const read = await found.service.propose(7, 5, file('a.pdf'));
+ expect(read.found).toEqual({ bookings: expect.any(Array) });
+ });
+
+ it('ASTBKI-006: reads the reason from the warnings of the preview, for each way it comes back empty', () => {
+ const preview = (...warnings: string[]) => ({ items: [], warnings });
+ expect(emptyReason(preview('a.pdf: no reservations found'), false)).toBe('ai_not_configured');
+ const scan = preview('a.pdf: no readable text found (a scanned PDF needs a model that reads images)');
+ expect(emptyReason(scan, true)).toBe('no_text');
+ expect(emptyReason(preview('a.pdf: AI parsing failed — 401'), true)).toBe('ai_failed');
+ expect(emptyReason(preview('a.pdf: could not read file — bad xref'), true)).toBe('ai_failed');
+ expect(emptyReason(preview('a.jpg: the configured model does not read images'), true)).toBe('ai_failed');
+ expect(emptyReason(preview('a.pdf: no reservations found'), true)).toBe('nothing_found');
+ });
+
it('ASTBKI-003: an applied booking is created through the import, on the trip, without a socket', async () => {
const t = await make();
const run = await t.service.apply(7, 5, { ...ITEM });
diff --git a/server/tests/unit/nest/assistant/assistant-history.test.ts b/server/tests/unit/nest/assistant/assistant-history.test.ts
index d9911e1fc..8681739d0 100644
--- a/server/tests/unit/nest/assistant/assistant-history.test.ts
+++ b/server/tests/unit/nest/assistant/assistant-history.test.ts
@@ -206,6 +206,29 @@ describe('toChatHistory', () => {
]);
});
+ it('ASTHIS-005b: a file that held no booking says why, and a reason this build does not know adds nothing', () => {
+ const read = (reason: unknown) =>
+ serverStepsNote([
+ {
+ type: 'tool-call',
+ toolCallId: 'r',
+ toolName: 'read_booking_file',
+ args: { name: 'AirBnB Buchung.pdf' },
+ result: { bookings: [], reason },
+ },
+ ]);
+ expect(read('ai_not_configured')).toBe(
+ 'TREK read the attached file "AirBnB Buchung.pdf" and found no booking in it: AI reading for imports is not ' +
+ "set up on this server, so only TREK's own reader looked at it.",
+ );
+ expect(read('ai_failed')).toContain('found no booking in it: the AI reader for imports could not read it.');
+ expect(read('no_text')).toContain('found no booking in it: it holds no readable text, most likely a scan.');
+ expect(read('nothing_found')).toContain(
+ "found no booking in it: neither TREK's reader nor the AI reader found one.",
+ );
+ expect(read('later_reason')).toBe('TREK read the attached file "AirBnB Buchung.pdf" and found no booking in it.');
+ });
+
it('ASTHIS-006: a place the user picked from the search results is told as a note, whatever became of it', () => {
const found = (approval: AssistantToolApproval, isError = false) =>
serverStepsNote([