Files
TREK/server/tests/unit/nest/assistant/assistant-history.test.ts
T
Maurice 89c33ac3f4 feat(assistant): an attached file can be kept in Files, on its own or with a booking or an expense
The chips offered to keep an attached file in Files, but nothing could do it:
the assistant's upload_trip_file stays denied, and the file never reached
Files. A new server write, keep_attached_file, is offered to the model where
the thread holds an attachment and held as a change card naming the file and
the booking, expense or place it goes with.

Applied, it runs the checks of a Files upload with the user's rights (the
demo block, the trip, file_upload and file_edit for a link, the instance's
file types, every linked id on the same trip), copies the bytes to a trip
file of their own, and writes the trip file, its expense link and the
attachment's pointer to it in one transaction; the members' planners get
file:created after it. A second keep of the same attachment answers the file
kept the first time. FilesService gains storeBytes and discardBytes so the
storage write stays out of that transaction.

The receipt opens the Files tab; the card's verb, its "Attached to" row and
the receipt's open label are new keys in all 27 locales. Undo is not offered:
no tool takes a trip file back yet.
2026-10-24 22:32:48 +02:00

522 lines
22 KiB
TypeScript

/**
* A thread's stored steps read two ways: the conversation the provider gets
* (every call answered, held changes under the provider's own ids, earlier
* lookups cut short) and the messages the client shows (one per answer, with
* what happened to each held change since it was written).
*/
import type { AssistantMessageRow } from '../../../../src/db/repositories/AssistantMessages.repository';
import type { AssistantToolCallRow } from '../../../../src/db/repositories/AssistantToolCalls.repository';
import {
messageIdOf,
outputText,
serverStepsNote,
toChatHistory,
toThreadMessages,
} from '../../../../src/nest/assistant/assistant-history';
import type { AssistantMessagePart, AssistantToolApproval } from '@trek/shared';
import { describe, expect, it } from 'vitest';
let nextId = 1;
function row(
role: 'user' | 'assistant',
parts: AssistantMessagePart[],
extra: Partial<AssistantMessageRow> = {},
): AssistantMessageRow {
return {
id: nextId++,
thread_id: 1,
role,
parts: JSON.stringify(parts),
provider_state: null,
status: 'complete',
model: role === 'assistant' ? 'm' : null,
created_at: '2026-10-21 10:00:00',
...extra,
};
}
function call(id: string, state: string, extra: Partial<AssistantToolCallRow> = {}): AssistantToolCallRow {
return {
id,
thread_id: 1,
message_id: 0,
provider_call_id: `prov-${id}`,
tool_name: 'create_place',
args: '{}',
preview: 'null',
state,
result: null,
undo_until: null,
created_at: '2026-10-21 10:00:00',
decided_at: null,
...extra,
};
}
const NOW = new Date('2026-10-21T10:05:00Z');
describe('toChatHistory', () => {
it('ASTHIS-001: starts at the first user message and answers every call', () => {
const rows = [
row('assistant', [{ type: 'text', text: 'orphan' }]),
row('user', [{ type: 'text', text: 'Plan Friday' }]),
row(
'assistant',
[{ type: 'tool-call', toolCallId: 'toolu_1', toolName: 'list_places', args: {}, result: { places: [] } }],
{ provider_state: '{"adapter":"openai-responses","items":[{"type":"function_call"}]}' },
),
row('assistant', [
{ type: 'text', text: 'Shall I add it?' },
{
type: 'tool-call',
toolCallId: 'uuid-1',
toolName: 'create_place',
args: { name: 'A' },
approval: { id: 'uuid-1' },
},
{
type: 'tool-call',
toolCallId: 'uuid-2',
toolName: 'create_place',
args: { name: 'B' },
result: 'nope',
isError: true,
approval: { id: 'uuid-2', approved: true },
},
]),
row('assistant', [], { status: 'aborted' }),
];
const history = toChatHistory(
rows,
new Map([
['uuid-1', 'toolu_9'],
['uuid-2', 'toolu_10'],
]),
);
expect(history).toEqual([
{ role: 'user', text: 'Plan Friday' },
{
role: 'assistant',
text: '',
toolCalls: [{ id: 'toolu_1', name: 'list_places', args: {} }],
providerState: { adapter: 'openai-responses', items: [{ type: 'function_call' }] },
},
{ role: 'tool', toolCallId: 'toolu_1', name: 'list_places', output: '{"places":[]}' },
{
role: 'assistant',
text: 'Shall I add it?',
toolCalls: [
{ id: 'toolu_9', name: 'create_place', args: { name: 'A' } },
{ id: 'toolu_10', name: 'create_place', args: { name: 'B' } },
],
providerState: null,
},
{ role: 'tool', toolCallId: 'toolu_9', name: 'create_place', output: expect.stringContaining('did not apply') },
{ role: 'tool', toolCallId: 'toolu_10', name: 'create_place', output: 'nope', isError: true },
]);
});
it('ASTHIS-002: cuts the lookups of earlier turns, keeps the current turn whole', () => {
const big = { places: Array.from({ length: 500 }, (_, i) => ({ id: i, name: `Place number ${i}` })) };
const rows = [
row('user', [{ type: 'text', text: 'one' }]),
row('assistant', [{ type: 'tool-call', toolCallId: 'a', toolName: 'list_places', args: {}, result: big }]),
row('user', [{ type: 'text', text: 'two' }]),
row('assistant', [{ type: 'tool-call', toolCallId: 'b', toolName: 'list_places', args: {}, result: big }]),
];
const outputs = toChatHistory(rows, new Map()).filter((m) => m.role === 'tool') as Array<{ output: string }>;
expect(outputs[0].output.length).toBeLessThanOrEqual(8_000);
expect(JSON.parse(outputs[0].output)).toMatchObject({ truncated: true });
expect(outputs[1].output).toBe(JSON.stringify(big));
});
it('ASTHIS-003: a thread without a user message sends nothing; text answers stay text', () => {
expect(toChatHistory([row('assistant', [{ type: 'text', text: 'x' }])], new Map())).toEqual([]);
expect(outputText('plain')).toBe('plain');
expect(outputText({ a: 1 })).toBe('{"a":1}');
});
it('ASTHIS-004: an attached file and the bookings read from it are notes, never calls of unknown tools', () => {
type CallPart = Extract<AssistantMessagePart, { type: 'tool-call' }>;
type Outcome = Partial<Pick<CallPart, 'result' | 'isError' | 'approval'>>;
const booking = (id: string, title: string, extra: Outcome): AssistantMessagePart => ({
type: 'tool-call',
toolCallId: id,
toolName: 'import_booking',
args: { title },
...extra,
});
const rows = [
row('user', [{ type: 'attachment', name: 'trip.pdf', mime: 'application/pdf', size: 2048 }]),
row('assistant', [
{
type: 'tool-call',
toolCallId: 'r1',
toolName: 'read_booking_file',
args: { name: 'trip.pdf' },
result: { bookings: ['LH 716', 'Hotel Gion', 'Bus', 'Tour', 'Ferry'] },
},
booking('b1', 'LH 716', { result: { reservation: { id: 5 } }, approval: { id: 'b1', approved: true } }),
booking('b2', 'Hotel Gion', { result: 'no', isError: true, approval: { id: 'b2', approved: true } }),
booking('b3', 'Bus', { result: {}, approval: { id: 'b3', approved: false } }),
booking('b4', 'Tour', { approval: { id: 'b4', resolution: 'cancelled' } }),
booking('b5', 'Ferry', { approval: { id: 'b5' } }),
]),
row('assistant', [{ type: 'text', text: 'LH 716 is on your trip.' }]),
];
const history = toChatHistory(rows, new Map());
expect(history.map((message) => message.role)).toEqual(['user', 'user', 'assistant']);
expect(history[0]).toEqual({
role: 'user',
text: '[The user attached the file "trip.pdf" (application/pdf, 2048 bytes).]',
});
const note = (history[1] as { text: string }).text;
expect(note).toContain('TREK read the attached file "trip.pdf" and found 5 booking(s): "LH 716", "Hotel Gion"');
expect(note).toContain('"LH 716" was added to the trip as a booking.');
expect(note).toContain('"Hotel Gion" could not be added.');
expect(note).toContain('The user discarded "Bus".');
expect(note).toContain('"Tour" was not added; the user moved on without deciding.');
expect(note).toContain('"Ferry" is waiting for the user to add or discard it.');
expect(history[2]).toMatchObject({ role: 'assistant', text: 'LH 716 is on your trip.', toolCalls: [] });
});
it('ASTHIS-005: a file that could not be read or held nothing says so; text and a file read as one message', () => {
expect(
serverStepsNote([
{ type: 'tool-call', toolCallId: 'r', toolName: 'read_booking_file', args: {}, result: 'x', isError: true },
]),
).toBe('TREK could not read the attached file "?".');
expect(
serverStepsNote([
{ type: 'tool-call', toolCallId: 'r', toolName: 'read_booking_file', args: { name: 'a.ics' }, result: {} },
]),
).toBe('TREK read the attached file "a.ics" and found no booking in it.');
const history = toChatHistory(
[
row('user', [
{ type: 'text', text: 'Here it is' },
{ type: 'attachment', name: 'b.eml', mime: 'message/rfc822', size: 1 },
]),
],
new Map(),
);
expect(history).toEqual([
{ role: 'user', text: 'Here it is\n[The user attached the file "b.eml" (message/rfc822, 1 bytes).]' },
]);
});
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-005c: a kept attachment is named with the id the model reads it by', () => {
const id = '0b7c2f0e-6f6d-4a8e-9b1a-3d2c1e0f9a87';
const history = toChatHistory(
[
row('user', [
{ type: 'attachment', name: 'AirBnB Buchung.pdf', mime: 'application/pdf', size: 9, attachmentId: id },
]),
],
new Map(),
);
expect(history).toEqual([
{
role: 'user',
text:
`[The user attached the file "AirBnB Buchung.pdf" (application/pdf, 9 bytes), attachment id ${id}. ` +
'Read it with read_attached_file if the user wants you to work with its content.]',
},
]);
});
it('ASTHIS-005d: the text of a file the user let the model read comes after the step, once, cut in earlier turns', () => {
const id = '0b7c2f0e-6f6d-4a8e-9b1a-3d2c1e0f9a87';
type CallPart = Extract<AssistantMessagePart, { type: 'tool-call' }>;
const read = (toolCallId: string, extra: Partial<CallPart>): AssistantMessagePart => ({
type: 'tool-call',
toolCallId,
toolName: 'read_attached_file',
args: { attachmentId: id, name: 'AirBnB Buchung.pdf', sentTo: 'm (h)' },
...extra,
});
const long = `Casa Lisboa ${'x'.repeat(9_000)}`;
const rows = [
row('user', [{ type: 'text', text: 'Make a booking from it' }]),
row('assistant', [
{ type: 'text', text: 'I need to read it first.' },
read('r1', { approval: { id: 'r1', approved: true }, result: { name: 'AirBnB Buchung.pdf', read: true } }),
]),
row('assistant', [read('r2', { result: { name: 'AirBnB Buchung.pdf', read: true } })]),
row('user', [{ type: 'text', text: 'Thanks' }]),
];
const history = toChatHistory(rows, new Map(), new Map([[id, long]]));
expect(history.map((message) => message.role)).toEqual(['user', 'assistant', 'user', 'user', 'user']);
expect(history[1]).toMatchObject({ role: 'assistant', text: 'I need to read it first.', toolCalls: [] });
const told = (history[2] as { text: string }).text;
expect(told).toContain('The user let you read the attached file "AirBnB Buchung.pdf".');
expect(told).toContain('Casa Lisboa');
// An earlier turn's text is cut so it does not crowd out the conversation.
expect(told.length).toBeLessThan(long.length);
expect(told.endsWith('[...]')).toBe(true);
expect((history[3] as { text: string }).text).toBe(
'You read the attached file "AirBnB Buchung.pdf" again; its text is above.',
);
// The latest turn keeps the whole text.
const latest = toChatHistory(rows.slice(0, 2), new Map(), new Map([[id, long]]));
expect((latest[2] as { text: string }).text).toContain(long);
});
it('ASTHIS-005e: a read the user did not allow, one still waiting, a refused one and a swept file say so', () => {
const id = '0b7c2f0e-6f6d-4a8e-9b1a-3d2c1e0f9a87';
const step = (extra: Partial<Extract<AssistantMessagePart, { type: 'tool-call' }>>) =>
toChatHistory(
[
row('user', [{ type: 'text', text: 'Read it' }]),
row('assistant', [
{
type: 'tool-call',
toolCallId: 'r',
toolName: 'read_attached_file',
args: { attachmentId: id, name: 'a.pdf' },
...extra,
},
]),
],
new Map(),
).map((message) => (message as { text: string }).text);
expect(step({ approval: { id: 'r', approved: false } })[1]).toBe(
'The user did not let you read the attached file "a.pdf".',
);
expect(step({ approval: { id: 'r' } })[1]).toContain("waits for the user's agreement; you have not read it.");
expect(step({ approval: { id: 'r', resolution: 'expired' } })[1]).toContain('you have not read it');
expect(step({ result: 'No such attachment in this conversation.', isError: true })[1]).toBe(
'Reading the attached file "a.pdf" failed: No such attachment in this conversation.',
);
expect(step({ result: { read: true } })[1]).toBe(
'The attached file "a.pdf" is no longer kept; you cannot read it again.',
);
});
it('ASTHIS-005f: keeping an attached file is told after its step, whatever became of it', () => {
const keep = (extra: Partial<Extract<AssistantMessagePart, { type: 'tool-call' }>>) =>
toChatHistory(
[
row('user', [{ type: 'text', text: 'Keep it in Files' }]),
row('assistant', [
{ type: 'text', text: 'Here is the card.' },
{
type: 'tool-call',
toolCallId: 'k',
toolName: 'keep_attached_file',
args: { attachmentId: 'a', name: 'a.pdf' },
...extra,
},
]),
],
new Map(),
);
const kept = keep({ approval: { id: 'k', approved: true }, result: { file: { id: 31 } } });
expect(kept.map((message) => message.role)).toEqual(['user', 'assistant', 'user']);
expect((kept[2] as { text: string }).text).toBe(
'The attached file "a.pdf" is in the trip\'s Files now (file id 31).',
);
const lastOf = (extra: Parameters<typeof keep>[0]) => (keep(extra).at(-1) as { text: string }).text;
expect(
lastOf({ approval: { id: 'k', approved: true }, result: 'The file could not be kept.', isError: true }),
).toBe('The attached file "a.pdf" could not be kept in Files: The file could not be kept.');
expect(lastOf({ approval: { id: 'k', approved: false } })).toBe(
'The user did not keep the attached file "a.pdf" in Files.',
);
expect(lastOf({ result: 'No such attachment in this conversation.', isError: true })).toContain('was refused');
expect(lastOf({ approval: { id: 'k', resolution: 'cancelled' } })).toContain('the user moved on without deciding');
expect(lastOf({ approval: { id: 'k' } })).toContain('waits for the user to apply or discard 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([
{
type: 'tool-call',
toolCallId: 'f',
toolName: 'add_found_place',
args: { via: 'create_and_assign_place', dayId: 31, name: 'Ginkaku-ji' },
approval,
...(isError ? { isError: true } : {}),
},
]);
expect(found({ id: 'f' })).toBe(
'The user picked "Ginkaku-ji" from the search results for day 31; it waits for them to add or discard it.',
);
expect(found({ id: 'f', approved: true })).toBe('"Ginkaku-ji" was added to day 31.');
expect(found({ id: 'f', approved: true }, true)).toBe('"Ginkaku-ji" could not be added to day 31.');
expect(found({ id: 'f', approved: false })).toBe('The user discarded "Ginkaku-ji" for day 31.');
expect(found({ id: 'f', resolution: 'cancelled' })).toBe(
'"Ginkaku-ji" was not added to day 31; the user moved on without deciding.',
);
const history = toChatHistory(
[
row('user', [{ type: 'text', text: 'Temples?' }]),
row('assistant', [
{
type: 'tool-call',
toolCallId: 'f',
toolName: 'add_found_place',
args: { via: 'assign_place_to_day', dayId: 31, placeId: 4 },
approval: { id: 'f' },
},
]),
],
new Map(),
);
expect(history).toEqual([
{ role: 'user', text: 'Temples?' },
{
role: 'user',
text: 'The user picked "?" from the search results for day 31; it waits for them to add or discard it.',
},
]);
});
it('ASTHIS-013: a proposal the user took to add is told as a note, a hike or a city walk, whatever became of it', () => {
const taken = (approval: AssistantToolApproval, via = 'create_hike', isError = false) =>
serverStepsNote([
{
type: 'tool-call',
toolCallId: 'p',
toolName: 'take_proposal',
args: { via, name: 'Herzogstand' },
approval,
...(isError ? { isError: true } : {}),
},
]);
expect(taken({ id: 'p' })).toBe(
'The user took the proposed hike "Herzogstand" to add it; it waits for them to add or discard it.',
);
expect(taken({ id: 'p', approved: true })).toBe('The hike "Herzogstand" was added to the trip.');
expect(taken({ id: 'p', approved: true }, 'create_hike', true)).toBe('The hike "Herzogstand" could not be added.');
expect(taken({ id: 'p', approved: false }, 'create_city_walk')).toBe(
'The user discarded the city walk "Herzogstand".',
);
expect(taken({ id: 'p', resolution: 'expired' })).toBe(
'The hike "Herzogstand" was not added; the user moved on without deciding.',
);
});
it('ASTHIS-014: a page the Studio drew is replayed as its description; no image is stored or sent again', () => {
const view = { spreadId: 'sp-2', bookVersion: 3, width: 1280, height: 640, bytes: 182_000, description: 'Page 2' };
const looked = { description: 'Page 2, a double spread', findings: [] };
const args = { spreadId: 'sp-2' };
const rows = [
row('user', [{ type: 'text', text: 'Check page 2' }]),
row('assistant', [
{ type: 'tool-call', toolCallId: 'p1', toolName: 'preview_book_page', args, result: looked },
// Whatever reaches the store is what the shared part keeps: no bytes.
{ type: 'data-trek-page-view', data: { ...view, data: 'aGVsbG8=' } as typeof view },
]),
row('assistant', [{ type: 'text', text: 'It looks balanced.' }]),
];
const history = toChatHistory(rows, new Map());
expect(history).toEqual([
{ role: 'user', text: 'Check page 2' },
{ role: 'assistant', text: '', toolCalls: [{ id: 'p1', name: 'preview_book_page', args }], providerState: null },
{ role: 'tool', toolCallId: 'p1', name: 'preview_book_page', output: JSON.stringify(looked) },
{ role: 'assistant', text: 'It looks balanced.', toolCalls: [], providerState: null },
]);
expect(JSON.stringify(history)).not.toContain('aGVsbG8=');
const [, answer] = toThreadMessages(rows, new Map(), NOW);
expect(answer?.parts[1]).toEqual({ type: 'data-trek-page-view', data: view });
});
});
describe('toThreadMessages', () => {
it('ASTHIS-010: the steps of one answer are one message under the first row id', () => {
nextId = 10;
const rows = [
row('user', [{ type: 'text', text: 'hi' }]),
row('assistant', [{ type: 'tool-call', toolCallId: 't', toolName: 'list_places', args: {}, result: {} }]),
row('assistant', [{ type: 'text', text: 'done' }]),
row('user', [{ type: 'text', text: 'again' }]),
];
const messages = toThreadMessages(rows, new Map(), NOW);
expect(messages.map((m) => [m.id, m.role, m.parts.length, m.status])).toEqual([
['msg-10', 'user', 1, 'complete'],
['msg-11', 'assistant', 2, 'complete'],
['msg-13', 'user', 1, 'complete'],
]);
expect(messages[0].createdAt).toBe('2026-10-21T10:00:00Z');
expect(messageIdOf(11)).toBe('msg-11');
});
it('ASTHIS-011: a held change reads as it stands now', () => {
const parts = (id: string): AssistantMessagePart => ({
type: 'tool-call',
toolCallId: id,
toolName: 'create_place',
args: {},
approval: { id },
});
const rows = [
row('assistant', [parts('waiting'), parts('old'), parts('gone'), parts('yes'), parts('no'), parts('stuck')], {
status: 'awaiting_approval',
}),
];
const calls = new Map([
['waiting', call('waiting', 'approval_requested')],
['old', call('old', 'approval_requested', { created_at: '2026-10-19 10:00:00' })],
['gone', call('gone', 'expired')],
['yes', call('yes', 'executed')],
['no', call('no', 'rejected')],
['stuck', call('stuck', 'approved')],
]);
const [message] = toThreadMessages(rows, calls, NOW);
expect(message.parts.map((part) => (part.type === 'tool-call' ? part.approval : null))).toEqual([
{ id: 'waiting' },
{ id: 'old', resolution: 'expired' },
{ id: 'gone', resolution: 'expired' },
{ id: 'yes', approved: true },
{ id: 'no', approved: false },
{ id: 'stuck', resolution: 'cancelled' },
]);
expect(message.status).toBe('awaiting_approval');
calls.set('waiting', call('waiting', 'rejected'));
expect(toThreadMessages(rows, calls, NOW)[0].status).toBe('complete');
});
it('ASTHIS-012: an applied change can be undone until its window closes or it was undone', () => {
const applied: AssistantMessagePart = {
type: 'data-trek-applied',
data: { toolCallId: 'x', entity: 'place', ids: [1], dayId: null, undoUntil: '2026-10-21T10:15:00Z' },
};
const rows = [row('assistant', [applied])];
const undoUntil = (state: string, until: string | null) =>
(
toThreadMessages(rows, new Map([['x', call('x', state, { undo_until: until })]]), NOW)[0].parts[0] as {
data: { undoUntil: string | null };
}
).data.undoUntil;
expect(undoUntil('executed', '2026-10-21 10:15:00')).toBe('2026-10-21T10:15:00Z');
expect(undoUntil('executed', '2026-10-21 10:01:00')).toBeNull();
expect(undoUntil('undone', '2026-10-21 10:15:00')).toBeNull();
});
});