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.
TypeScript budget-trackercollaborativedockeritinerarymcp-serveroffline-firstopen-sourceopenstreetmappacking-listpwareal-timeself-hostedselfhostedtraveltravel-apptravel-journaltravel-plannertripitwanderlog
feat(assistant): a journey's chat request names the page it is asked from
Maurice2508fd8624b7
diff --git a/server/src/nest/assistant/journey-assistant.controller.ts b/server/src/nest/assistant/journey-assistant.controller.tsindex f9086d7eb..7f34632eb 100644--- a/server/src/nest/assistant/journey-assistant.controller.ts+++ b/server/src/nest/assistant/journey-assistant.controller.ts@@ -16,21 +16,28 @@ import {assistantThreadResponseSchema,assistantUndoResponseSchema,successResponseSchema,+ type AssistantJourneySurface,type AssistantScope,} from '@trek/shared';import type { Response } from 'express';-/** The scope of a request JourneyAssistantGuard let through: the journey is open to the user. */-function journeyScope(journeyId: string): AssistantScope {- return { kind: 'journey', journeyId: Number(journeyId) };+/**+ * The scope of a request JourneyAssistantGuard let through: the journey is+ * open to the user. A turn and a decision also name the page they come from+ * (the Studio, or the journal page); the threads are the same on both.+ */+function journeyScope(journeyId: string, surface?: AssistantJourneySurface): AssistantScope {+ const scope = { kind: 'journey' as const, journeyId: Number(journeyId) };+ return surface ? { ...scope, surface } : scope;}/*** `/api/journeys/:journeyId/assistant`: the user's conversations with the- * assistant in TREK Studio, about one journey and its book. The same+ * assistant about one journey, in TREK Studio (its book) and on the journal+ * page (its entries, photos and settings), which share them. The same* conversations, turns, change cards, decisions and undo as on a trip, with- * the book's tools; any member may talk about the journey, and what a+ * the page's tools; any member may talk about the journey, and what a* proposed change may do is decided by the tool that runs it, with the* user's own rights (a viewer's turn is offered no write at all). A journey* without trips works. No files, found places or sharing to a trip's chat@@ -78,7 +85,7 @@ export class JourneyAssistantController {@Body() body: AssistantChatDto,@Res() res: Response,): Promise<void> {- await this.assistant.chat(user.id, journeyScope(journeyId), body, channelOf(res));+ await this.assistant.chat(user.id, journeyScope(journeyId, body.surface), body, channelOf(res));}// response-contract-exempt: streams the turn through @Res() as server-sent events, there is no JSON body to check.@@ -90,7 +97,8 @@ export class JourneyAssistantController {@Body() body: AssistantDecisionsDto,@Res() res: Response,): Promise<void> {- await this.assistant.decide(user.id, journeyScope(journeyId), idParam(threadId), body, channelOf(res));+ const scope = journeyScope(journeyId, body.surface);+ await this.assistant.decide(user.id, scope, idParam(threadId), body, channelOf(res));}@Post('tool-calls/:toolCallId/undo')diff --git a/server/tests/unit/nest/assistant/journey-assistant.controller.test.ts b/server/tests/unit/nest/assistant/journey-assistant.controller.test.tsindex c9d142506..dc7c4a7e2 100644--- a/server/tests/unit/nest/assistant/journey-assistant.controller.test.ts+++ b/server/tests/unit/nest/assistant/journey-assistant.controller.test.ts@@ -131,6 +131,19 @@ describe('JourneyAssistantController', () => {gone.emit('close');expect(service.decide.mock.calls[0][4].signal.aborted).toBe(true);});++ it('ASTJCTL-005: a turn and a decision from the journal page name it in the scope, the threads stay one list', async () => {+ const { service, controller } = make();+ const journal = { ...JOURNEY, surface: 'journal' };+ const chat = { locale: 'en', timeZone: 'UTC', threadId: null, text: 'Hi', surface: 'journal' as const };+ await controller.chat(user, '12', chat, new FakeResponse() as unknown as Response);+ expect(service.chat.mock.calls[0].slice(0, 3)).toEqual([7, journal, chat]);+ const decisions = { locale: 'en', timeZone: 'UTC', decisions: [], surface: 'studio' as const };+ await controller.decide(user, '12', '9', decisions, new FakeResponse() as unknown as Response);+ expect(service.decide.mock.calls[0].slice(0, 3)).toEqual([7, { ...JOURNEY, surface: 'studio' }, 9]);+ await controller.threads(user, '12');+ expect(service.listThreads).toHaveBeenCalledWith(7, JOURNEY);+ });});describe('JourneyAssistantGuard', () => {diff --git a/shared/src/assistant/assistant-scope.schema.spec.ts b/shared/src/assistant/assistant-scope.schema.spec.tsindex f3eb91382..deebe2c1a 100644--- a/shared/src/assistant/assistant-scope.schema.spec.ts+++ b/shared/src/assistant/assistant-scope.schema.spec.ts@@ -1,4 +1,9 @@-import { assistantScopeKey, assistantScopeSchema, HOME_ASSISTANT_SCOPE } from './assistant-scope.schema';+import {+ ASSISTANT_JOURNEY_SURFACES,+ assistantScopeKey,+ assistantScopeSchema,+ HOME_ASSISTANT_SCOPE,+} from './assistant-scope.schema';import { assistantFocusSchema } from './assistant.schema';import { describe, expect, it } from 'vitest';@@ -22,6 +27,15 @@ describe('assistantScopeSchema', () => {expect(assistantScopeKey({ kind: 'journey', journeyId: 7 })).toBe('journey:7');});+ it('ASTSCOPE-007: a journey names the page it is asked from, which never changes its key', () => {+ const journal = { kind: 'journey', journeyId: 7, surface: 'journal' } as const;+ expect(assistantScopeSchema.parse(journal)).toEqual(journal);+ expect(assistantScopeSchema.safeParse({ ...journal, surface: 'planner' }).success).toBe(false);+ expect(assistantScopeSchema.safeParse({ kind: 'trip', tripId: 7, surface: 'journal' }).success).toBe(false);+ expect(assistantScopeKey(journal)).toBe(assistantScopeKey({ kind: 'journey', journeyId: 7, surface: 'studio' }));+ expect(ASSISTANT_JOURNEY_SURFACES).toEqual(['studio', 'journal']);+ });+it('ASTSCOPE-006: has a home scope that names no trip and no journey', () => {expect(assistantScopeSchema.parse({ kind: 'home' })).toEqual(HOME_ASSISTANT_SCOPE);expect(assistantScopeSchema.safeParse({ kind: 'home', tripId: 5 }).success).toBe(false);diff --git a/shared/src/assistant/assistant-scope.schema.ts b/shared/src/assistant/assistant-scope.schema.tsindex db4414a2b..adef15bfe 100644--- a/shared/src/assistant/assistant-scope.schema.ts+++ b/shared/src/assistant/assistant-scope.schema.ts@@ -12,9 +12,25 @@ import { z } from 'zod';* it: `/api/trips/:tripId/assistant`, `/api/journeys/:journeyId/assistant` and* `/api/assistant`.*/+/**+ * The two pages a journey's conversation is held on: the book in TREK Studio,+ * and the journal itself (its entries, photos and settings). They share the+ * journey's threads; what a turn offers and what it is told follow the page.+ */+export const ASSISTANT_JOURNEY_SURFACES = ['studio', 'journal'] as const;+export const assistantJourneySurfaceSchema = z.enum(ASSISTANT_JOURNEY_SURFACES);+export type AssistantJourneySurface = z.infer<typeof assistantJourneySurfaceSchema>;+export const assistantScopeSchema = z.discriminatedUnion('kind', [z.object({ kind: z.literal('trip'), tripId: idSchema }).strict(),- z.object({ kind: z.literal('journey'), journeyId: idSchema }).strict(),+ z+ .object({+ kind: z.literal('journey'),+ journeyId: idSchema,+ /** The page a request comes from; left out, the Studio's. Never part of the key: both pages keep one list. */+ surface: assistantJourneySurfaceSchema.optional(),+ })+ .strict(),z.object({ kind: z.literal('home') }).strict(),]);export type AssistantScope = z.infer<typeof assistantScopeSchema>;diff --git a/shared/src/assistant/assistant.schema.spec.ts b/shared/src/assistant/assistant.schema.spec.tsindex ebcb28a0d..cd4b4c0a3 100644--- a/shared/src/assistant/assistant.schema.spec.ts+++ b/shared/src/assistant/assistant.schema.spec.ts@@ -64,6 +64,21 @@ describe('assistantChatRequestSchema', () => {it('needs the locale and time zone', () => {expect(assistantChatRequestSchema.safeParse({ threadId: null, text: 'hi' }).success).toBe(false);});++ it('names the page of a journey it is asked from, the Studio or the journal, and nothing else', () => {+ const base = { ...ctx, threadId: null, text: 'hi' };+ expect(assistantChatRequestSchema.parse({ ...base, surface: 'journal' }).surface).toBe('journal');+ expect(assistantChatRequestSchema.parse({ ...base, surface: 'studio' }).surface).toBe('studio');+ expect(assistantChatRequestSchema.parse(base).surface).toBeUndefined();+ expect(assistantChatRequestSchema.safeParse({ ...base, surface: 'planner' }).success).toBe(false);+ const decision = { toolCallId: 'a', approved: true };+ expect(assistantDecisionsRequestSchema.parse({ ...ctx, decisions: [decision], surface: 'journal' }).surface).toBe(+ 'journal',+ );+ expect(assistantDecisionsRequestSchema.safeParse({ ...ctx, decisions: [decision], surface: 'x' }).success).toBe(+ false,+ );+ });});describe('assistantAttachmentRequestSchema', () => {diff --git a/shared/src/assistant/assistant.schema.ts b/shared/src/assistant/assistant.schema.tsindex 4b08b4082..f9875e862 100644--- a/shared/src/assistant/assistant.schema.ts+++ b/shared/src/assistant/assistant.schema.ts@@ -1,6 +1,7 @@import { bookElementIdSchema, bookSpreadIdSchema } from '../book/book-ops.schema';import { idParamSchema, idSchema } from '../common/primitives.schema';import { assistantAttachmentIdSchema } from './assistant-attachments.schema';+import { assistantJourneySurfaceSchema } from './assistant-scope.schema';import { assistantPageRenderSchema, assistantPageViewSchema } from './assistant-vision.schema';import {assistantCardSchema,@@ -248,6 +249,8 @@ export const assistantChatRequestSchema = assistantClientCtxSchemamentions: z.array(assistantMentionSchema).max(50).optional(),/** The client draws a Studio page when the turn asks for one (the Studio's chat, assistant-vision.schema.ts). */renders: z.boolean().optional(),+ /** A journey's chat only: the page the user asks from, the Studio's when left out. A trip's chat ignores it. */+ surface: assistantJourneySurfaceSchema.optional(),}).strict();export type AssistantChatRequest = z.infer<typeof assistantChatRequestSchema>;@@ -279,6 +282,8 @@ export const assistantDecisionsRequestSchema = assistantClientCtxSchemadecisions: z.array(assistantDecisionSchema).min(1).max(50),/** As on a chat turn: the client draws a Studio page when the turn asks for one. */renders: z.boolean().optional(),+ /** As on a chat turn: the page of a journey the decision is made on, which picks the tools the turn goes on with. */+ surface: assistantJourneySurfaceSchema.optional(),}).strict();export type AssistantDecisionsRequest = z.infer<typeof assistantDecisionsRequestSchema>;