feat(assistant): the home chat starts a journal and hands a journal's questions to its page

Asked on /journey to create "Japan 2026", the home chat had nothing to
offer: it keeps no write that names a trip or a journey. It now keeps
create_journey, whose trips the tool checks one by one, and delete_journey,
which the journey's own chat leaves out because the conversation goes
with it. For anything inside a journal it offers open_journey_chat, the
twin of open_trip_chat: the journal is read from the user's own journals
and shown as a card whose button goes on in the journal's chat with the
question waiting there. The trip and journal cards share their question
and button.
This commit is contained in:
Maurice
2026-10-24 21:14:43 +02:00
parent c0ec8e567b
commit 1d9648267b
44 changed files with 688 additions and 48 deletions
@@ -0,0 +1,98 @@
import type { AiToolSpec } from '../ai-connections/ai-chat-client.interface';
import type { ToolRun } from './assistant-tools.service';
/**
* The home chat's way onto a journal's own page, the twin of open_trip_chat:
* a journal's entries, photos and settings change in the chat on its page,
* which knows them, so the home chat hands the question over there. The
* journal is read from the user's own journals (`list_journeys`, with the
* user's rights), so the card never names one the user cannot open.
*/
/** The server's own step that offers to go on on a journal's page; no MCP tool has its name. */
export const OPEN_JOURNEY_CHAT_TOOL = 'open_journey_chat';
/** The read the step answers from, offered only where the journal addon lists journals. */
export const OPEN_JOURNEY_CHAT_READ = 'list_journeys';
/** The longest journal title and subtitle a card carries. */
const MAX_TITLE_CHARS = 200;
const MAX_SUBTITLE_CHARS = 300;
/** How the model is offered the way onto a journal's page. */
export const OPEN_JOURNEY_CHAT_SPEC: AiToolSpec = {
name: OPEN_JOURNEY_CHAT_TOOL,
description:
"Offer the user to go on in the chat on one journal's own page, shown as a card with a button. Use it when the " +
'user wants to write, change or delete entries of a journal, add or move its photos, or change its settings, ' +
"which only that journal's chat can do. Pass the journal's id from list_journeys (or from create_journey's " +
"answer) and the question to ask there, in the user's words and language. Call it once per journal, next to a " +
'short answer saying why.',
parameters: {
type: 'object',
properties: {
journeyId: { type: 'integer', minimum: 1, description: 'The id of the journal, from list_journeys.' },
question: {
type: 'string',
maxLength: 500,
description: "What to ask in the journal's chat, as the user would; leave it out when there is nothing to ask.",
},
},
required: ['journeyId'],
additionalProperties: false,
},
};
const DATE = /^\d{4}-\d{2}-\d{2}$/;
function dateOf(value: unknown): string | null {
return typeof value === 'string' && DATE.test(value.slice(0, 10)) ? value.slice(0, 10) : null;
}
function lineOf(value: unknown, max: number): string | null {
const text = typeof value === 'string' ? value.replace(/\s+/g, ' ').trim().slice(0, max) : '';
return text || null;
}
function countOf(value: unknown): number {
const count = Number(value);
return Number.isInteger(count) && count >= 0 ? Math.min(count, 1_000_000) : 0;
}
/** One journal of a `list_journeys` answer, as the card names it; null for a row without an id or a title. */
function journalOf(row: unknown) {
const record = (row ?? {}) as Record<string, unknown>;
const id = Number(record.id);
const title = lineOf(record.title, MAX_TITLE_CHARS);
if (!Number.isInteger(id) || id <= 0 || !title) return null;
return {
id,
title,
subtitle: lineOf(record.subtitle, MAX_SUBTITLE_CHARS),
startDate: dateOf(record.trip_date_min),
endDate: dateOf(record.trip_date_max),
entryCount: countOf(record.entry_count),
photoCount: countOf(record.photo_count),
};
}
/**
* Answers the step from the user's journals: the journal it names, as one
* the user may open, and the question to hand over. A journal the user
* cannot open is refused like one that does not exist.
*/
export function openJourneyChat(args: Record<string, unknown>, listed: ToolRun): ToolRun {
const journeyId = Number(args.journeyId);
const rows = listed.ok ? (listed.value as { journeys?: unknown } | null)?.journeys : null;
const row = Array.isArray(rows) ? rows.find((candidate) => Number(candidate?.id) === journeyId) : undefined;
const journey = row === undefined ? null : journalOf(row);
if (!journey) return { ok: false, value: 'No journal of the user has this id. Take the id from list_journeys.' };
return {
ok: true,
value: {
journey,
question: lineOf(args.question, 500),
shown: "Shown to the user as a card that opens the journal's page with its chat.",
},
};
}
@@ -227,6 +227,7 @@ How you work:
- The home card in the context lists the user's trips with their ids: the ones under way, the next ones, the latest ones behind. list_trips lists them all. Use those ids, or ids a tool returned; never invent one.
- You can read any of these trips: pass its id to the trip tools (get_trip_summary, list_places, the budget, packing and to-do lists, the bookings, the weather). Answer questions across trips that way, for example which trip is next, what is still unbooked, or how two trips compare.
- You cannot change a trip from here. When the user wants to change or plan one trip, answer in a sentence and call open_trip_chat with its id and their question: the trip's own chat knows its days and places and proposes the changes there. Also offer it when a conversation is about one trip only.
- A travel journal (a journey) is written in the chat on its own page. Here you can start a new one with create_journey, linking the trips the user names, and delete one for its owner with delete_journey; both are among the "journey" tools. To write, change or delete a journal's entries or photos or change its settings, answer in a sentence and call open_journey_chat with the journal's id from list_journeys (or from create_journey's answer) and the user's question.
- What belongs to no trip you can change here, through its tools: the atlas (visited countries and regions, the bucket list), the vacation planner, collections of saved places, tours in the tour library, and the user's settings. Their tools are not loaded at first: call use_addon_tools with the groups you need, then use them in the same reply.
- To change something, call the tool for the change. Do not describe a change in text and ask whether to make it: the app shows each call to the user as a change card, and nothing happens until they apply it. Never say a change was made unless its result says it was applied.
- Act, don't announce. Make the lookups and changes you need in this reply, in as few steps as you can; never end a reply with "I will now ..." and no calls.
@@ -234,22 +234,34 @@ const JOURNEY_WRITE_CONTROLLERS: ReadonlySet<string> = new Set([
'TripJournalLinkMcp',
]);
/**
* The journey writes the home chat keeps although they name trips or a
* journey: starting a journal (its trips are checked one by one by the tool,
* as for any MCP client), and deleting one, which its own chat leaves out
* because the conversation goes with it; the tool refuses everyone but the
* owner.
*/
const HOME_JOURNEY_WRITES: ReadonlySet<string> = new Set(['create_journey', 'delete_journey']);
/**
* Whether the home chat (no one trip, no one journey) keeps an attached tool
* once its schema and annotations are known: every read, a trip's or a
* journey's too, on the id the model names (the tool itself checks that the
* user may read it, as for any MCP client); and the writes that take neither
* a trip nor a journey (the atlas, the vacation planner, collections, the
* tour library, the settings). A trip's own changes belong in that trip's
* chat, which the home chat offers instead (open_trip_chat); a journey's stay
* with the journey page, as they do for the Studio's chat.
* tour library, the settings), with a journal's start and its deletion. A
* trip's own changes belong in that trip's chat, which the home chat offers
* instead (open_trip_chat); a journal's in the chat on its page
* (open_journey_chat).
*/
export function keptAtHome(
kind: 'read' | 'write',
schemaArgs: Iterable<string>,
className: string | undefined,
name = '',
): boolean {
if (kind === 'read') return true;
if (HOME_JOURNEY_WRITES.has(name)) return true;
if (className !== undefined && JOURNEY_WRITE_CONTROLLERS.has(className)) return false;
for (const arg of schemaArgs) if (TRIP_OR_JOURNEY_ARGS.has(arg)) return false;
return true;
@@ -5,6 +5,12 @@ import { traceEntry } from '../audit/entry-trace.logger';
import { withRequestContext } from '../database/request-context';
import { FOLLOW_UPS_SPEC } from './assistant-follow-ups';
import { OPEN_TRIP_CHAT_SPEC, OPEN_TRIP_CHAT_TOOL, openTripChat } from './assistant-home';
import {
OPEN_JOURNEY_CHAT_READ,
OPEN_JOURNEY_CHAT_SPEC,
OPEN_JOURNEY_CHAT_TOOL,
openJourneyChat,
} from './assistant-home-journeys';
import {
checkJournalTools,
heldInJourney,
@@ -150,7 +156,8 @@ export function checkToolCatalog(
if (!tools.has(name)) problems.push(`tool "${name}" with a card of its own is not registered`);
}
for (const name of Object.keys(cards)) {
if (!tools.has(name) && !ASSISTANT_SERVER_TOOLS.has(name) && name !== OPEN_TRIP_CHAT_TOOL) {
const serverStep = name === OPEN_TRIP_CHAT_TOOL || name === OPEN_JOURNEY_CHAT_TOOL;
if (!tools.has(name) && !ASSISTANT_SERVER_TOOLS.has(name) && !serverStep) {
problems.push(`tool "${name}" with cards of a package is neither registered nor the server's own`);
}
}
@@ -170,6 +177,7 @@ export function checkToolCatalog(
PREVIEW_BOOK_PAGE_TOOL,
ASSISTANT_FOLLOW_UPS_TOOL,
OPEN_TRIP_CHAT_TOOL,
OPEN_JOURNEY_CHAT_TOOL,
]) {
if (tools.has(name)) problems.push(`"${name}" is the server's own step`);
}
@@ -281,7 +289,8 @@ export class AssistantToolSession {
if (!this.grouped) return [...listed, FOLLOW_UPS_SPEC];
const more = Object.keys(ASSISTANT_TOOL_GROUPS).some((group) => !this.loaded.has(group));
if (this.mode === 'home') {
return [...listed, OPEN_TRIP_CHAT_SPEC, ...(more ? [HOME_USE_TOOLS_SPEC] : []), FOLLOW_UPS_SPEC];
const journals = this.offersJournalChat ? [OPEN_JOURNEY_CHAT_SPEC] : [];
return [...listed, OPEN_TRIP_CHAT_SPEC, ...journals, ...(more ? [HOME_USE_TOOLS_SPEC] : []), FOLLOW_UPS_SPEC];
}
return [...listed, REMEMBER_SPEC, ...(more ? [USE_TOOLS_SPEC] : []), FOLLOW_UPS_SPEC];
}
@@ -293,10 +302,16 @@ export class AssistantToolSession {
if (name === USE_TOOLS_TOOL) return this.grouped ? 'read' : null;
// Offering a trip's chat changes nothing; the user decides on the card.
if (name === OPEN_TRIP_CHAT_TOOL) return this.mode === 'home' ? 'read' : null;
if (name === OPEN_JOURNEY_CHAT_TOOL) return this.offersJournalChat ? 'read' : null;
const tool = this.tools.get(name);
return tool?.offered ? tool.kind : null;
}
/** The home chat offers a journal's page while the user's journals can be listed (the journal addon is on). */
private get offersJournalChat(): boolean {
return this.mode === 'home' && this.tools.has(OPEN_JOURNEY_CHAT_READ);
}
/** What a card for this tool goes by; null for a tool this session does not hold. */
hintOf(name: string): ToolHint | null {
return this.tools.get(name)?.hint ?? null;
@@ -321,6 +336,9 @@ export class AssistantToolSession {
async call(name: string, args: ToolArgs): Promise<ToolRun> {
if (name === USE_TOOLS_TOOL) return this.load(args);
if (name === OPEN_TRIP_CHAT_TOOL && this.mode === 'home') return this.offerTripChat(args);
if (name === OPEN_JOURNEY_CHAT_TOOL && this.offersJournalChat) {
return openJourneyChat(args, await this.invoke(OPEN_JOURNEY_CHAT_READ, {}));
}
const final = name === TRANSIT_CREATE_TOOL ? await this.withFoundItinerary(args) : args;
const run = await this.invoke(name, final);
this.audit(name);
@@ -493,6 +511,7 @@ export class AssistantToolsService implements OnApplicationBootstrap {
tool.kind,
Object.keys((tool.inputSchema.properties as object | undefined) ?? {}),
catalog.get(tool.name)?.className,
tool.name,
);
const mode: SessionMode = undoTargets ? 'undo' : 'home';
return this.session(userId, NO_PIN, mode, { catalog, include, keep });
+22 -2
View File
@@ -1,6 +1,7 @@
import { OPEN_TRIP_CHAT_TOOL, phaseOf } from '../../assistant-home';
import { OPEN_JOURNEY_CHAT_TOOL } from '../../assistant-home-journeys';
import type { AnswerInput, DomainCards } from '../card-types';
import { tripChatCardSchema, type TrekCard } from '@trek/shared';
import { journalChatCardSchema, tripChatCardSchema, type TrekCard } from '@trek/shared';
/**
* The home chat's own card: the way into a trip's chat its step offers,
@@ -31,7 +32,26 @@ export function tripChatAnswer(input: AnswerInput): TrekCard | null {
return parsed.success ? parsed.data : null;
}
/**
* The way onto a journal's page its step offers, built from what the step
* answered (a journal of the user's, read from their own journals) and
* checked against the card's schema.
*/
export function journalChatAnswer(input: AnswerInput): TrekCard | null {
const value = (input.value ?? {}) as { journey?: unknown; question?: unknown };
if (!value.journey) return null;
const parsed = journalChatCardSchema.safeParse({
kind: 'journalChat',
journey: value.journey,
question: typeof value.question === 'string' && value.question ? value.question : null,
});
return parsed.success ? parsed.data : null;
}
export const HOME_CARDS: DomainCards = {
id: 'home',
tools: { [OPEN_TRIP_CHAT_TOOL]: { answer: tripChatAnswer } },
tools: {
[OPEN_TRIP_CHAT_TOOL]: { answer: tripChatAnswer },
[OPEN_JOURNEY_CHAT_TOOL]: { answer: journalChatAnswer },
},
};