mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-22 03:50:30 +00:00
feat(assistant): the assistant opens outside a trip, as a home chat about all trips
A third scope, home, next to trip and journey: threads that name neither a trip nor a journey (the CHECK now only forbids both), reached under /api/assistant by their owner only. A home turn reads the user's trips through list_trips into a short card (under way, next, later, behind, undated), offers every read (a trip's on the id the model names, checked by the tool with the user's rights) and only the writes that take no trip or journey (atlas, vacation planner, collections, tour library, settings), all held as change cards. open_trip_chat answers with a card that goes on in that trip's own chat with the question waiting in its composer. On the desktop the window opens on the home chat outside a trip; its head lists the trips to switch into and a switched trip chat has the way back. On the phone the home chat opens full screen from the dashboard head and the dock's More list, and the card opens the trip with its chat. Both run on useAssistantHome.
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { assistantScopeKey, assistantScopeSchema } from './assistant-scope.schema';
|
||||
import { assistantScopeKey, assistantScopeSchema, HOME_ASSISTANT_SCOPE } from './assistant-scope.schema';
|
||||
import { assistantFocusSchema } from './assistant.schema';
|
||||
|
||||
import { describe, expect, it } from 'vitest';
|
||||
@@ -21,6 +21,13 @@ describe('assistantScopeSchema', () => {
|
||||
expect(assistantScopeKey({ kind: 'trip', tripId: 7 })).toBe('trip:7');
|
||||
expect(assistantScopeKey({ kind: 'journey', journeyId: 7 })).toBe('journey:7');
|
||||
});
|
||||
|
||||
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);
|
||||
expect(assistantScopeSchema.safeParse({ kind: 'home', journeyId: 12 }).success).toBe(false);
|
||||
expect(assistantScopeKey(HOME_ASSISTANT_SCOPE)).toBe('home');
|
||||
});
|
||||
});
|
||||
|
||||
describe('assistantFocusSchema in the Studio', () => {
|
||||
|
||||
@@ -3,19 +3,27 @@ import { idSchema } from '../common/primitives.schema';
|
||||
import { z } from 'zod';
|
||||
|
||||
/**
|
||||
* What an assistant conversation is about: a trip (the planner's chat) or a
|
||||
* journey (the chat in TREK Studio, which works on the journey's book). A
|
||||
* thread belongs to exactly one of them, and the tools a turn offers, the
|
||||
* context it reads and the routes it is reached under follow from it:
|
||||
* `/api/trips/:tripId/assistant` and `/api/journeys/:journeyId/assistant`.
|
||||
* What an assistant conversation is about: a trip (the planner's chat), a
|
||||
* journey (the chat in TREK Studio, which works on the journey's book) or no
|
||||
* single one (`home`, the chat on the dashboard, Vacay, Atlas, collections and
|
||||
* settings, which knows all of the user's trips and works on what belongs to
|
||||
* none of them). A thread belongs to exactly one of them, and the tools a turn
|
||||
* offers, the context it reads and the routes it is reached under follow from
|
||||
* it: `/api/trips/:tripId/assistant`, `/api/journeys/:journeyId/assistant` and
|
||||
* `/api/assistant`.
|
||||
*/
|
||||
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('home') }).strict(),
|
||||
]);
|
||||
export type AssistantScope = z.infer<typeof assistantScopeSchema>;
|
||||
|
||||
/** The key a scope's conversations are kept under on a client: `trip:5`, `journey:12`. */
|
||||
/** The scope of the chat outside any one trip or journey. */
|
||||
export const HOME_ASSISTANT_SCOPE: AssistantScope = { kind: 'home' };
|
||||
|
||||
/** The key a scope's conversations are kept under on a client: `trip:5`, `journey:12`, `home`. */
|
||||
export function assistantScopeKey(scope: AssistantScope): string {
|
||||
return scope.kind === 'trip' ? `trip:${scope.tripId}` : `journey:${scope.journeyId}`;
|
||||
if (scope.kind === 'trip') return `trip:${scope.tripId}`;
|
||||
return scope.kind === 'journey' ? `journey:${scope.journeyId}` : 'home';
|
||||
}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
import { tripChatCardSchema } from './trip-chat.card';
|
||||
|
||||
export * from './trip-chat.card';
|
||||
|
||||
/**
|
||||
* What the chat outside any one trip (the `home` scope) brings to the
|
||||
* assistant chat: the card that offers to go on in a trip's own chat. It has
|
||||
* no change cards of its own; the writes it proposes are the atlas', the
|
||||
* vacation planner's and the collections'. cards/index.ts spreads the kinds
|
||||
* into the card union.
|
||||
*/
|
||||
export const HOME_CARD_SCHEMAS = [tripChatCardSchema] as const;
|
||||
@@ -5,6 +5,7 @@ import { BOOKING_CARD_SCHEMAS } from './booking.contract';
|
||||
import { BUDGET_CARD_SCHEMAS } from './budget.contract';
|
||||
import { COLLAB_CARD_SCHEMAS } from './collab.contract';
|
||||
import { COLLECTION_CARD_SCHEMAS } from './collection.contract';
|
||||
import { HOME_CARD_SCHEMAS } from './home.contract';
|
||||
import { JOURNEY_CARD_SCHEMAS } from './journey.contract';
|
||||
import { PACKING_CARD_SCHEMAS } from './packing.contract';
|
||||
import { ROADTRIP_CARD_SCHEMAS } from './roadtrip.contract';
|
||||
@@ -21,6 +22,7 @@ export * from './booking.contract';
|
||||
export * from './budget.contract';
|
||||
export * from './collab.contract';
|
||||
export * from './collection.contract';
|
||||
export * from './home.contract';
|
||||
export * from './journey.contract';
|
||||
export * from './packing.contract';
|
||||
export * from './roadtrip.contract';
|
||||
@@ -47,6 +49,7 @@ export const trekCardSchema = z.discriminatedUnion('kind', [
|
||||
...PACKING_CARD_SCHEMAS,
|
||||
...BOOK_CARD_SCHEMAS,
|
||||
...BOOK_DESIGN_CARD_SCHEMAS,
|
||||
...HOME_CARD_SCHEMAS,
|
||||
]);
|
||||
export type TrekCard = z.infer<typeof trekCardSchema>;
|
||||
export type TrekCardKind = TrekCard['kind'];
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
import { storedTrekCardSchema, trekCardSchema } from './index';
|
||||
import { tripChatCardSchema } from './trip-chat.card';
|
||||
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
const card = {
|
||||
kind: 'tripChat',
|
||||
trip: { id: 5, title: 'Tokyo 2026', startDate: '2026-11-02', endDate: '2026-11-12', phase: 'next', placeCount: 34 },
|
||||
question: 'What is still missing on day 3?',
|
||||
} as const;
|
||||
|
||||
describe('tripChatCardSchema', () => {
|
||||
it('TRIPCHAT-001: is part of the card union and reads as itself', () => {
|
||||
expect(trekCardSchema.parse(card)).toEqual(card);
|
||||
expect(storedTrekCardSchema.parse(card)).toEqual(card);
|
||||
});
|
||||
|
||||
it('TRIPCHAT-002: takes a trip without dates, places or a question', () => {
|
||||
const bare = {
|
||||
kind: 'tripChat',
|
||||
trip: { id: 9, title: 'Someday', startDate: null, endDate: null, phase: 'undated', placeCount: null },
|
||||
question: null,
|
||||
};
|
||||
expect(tripChatCardSchema.parse(bare)).toEqual(bare);
|
||||
});
|
||||
|
||||
it('TRIPCHAT-003: refuses an unknown phase, a bad date, an empty title or an overlong question', () => {
|
||||
expect(tripChatCardSchema.safeParse({ ...card, trip: { ...card.trip, phase: 'soon' } }).success).toBe(false);
|
||||
expect(tripChatCardSchema.safeParse({ ...card, trip: { ...card.trip, startDate: '2.11.2026' } }).success).toBe(
|
||||
false,
|
||||
);
|
||||
expect(tripChatCardSchema.safeParse({ ...card, trip: { ...card.trip, title: '' } }).success).toBe(false);
|
||||
expect(tripChatCardSchema.safeParse({ ...card, question: 'x'.repeat(501) }).success).toBe(false);
|
||||
});
|
||||
|
||||
it('TRIPCHAT-004: a stored card with a broken trip is left out, not the message around it', () => {
|
||||
expect(storedTrekCardSchema.parse({ ...card, trip: { id: 0 } })).toBeNull();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,30 @@
|
||||
import { idSchema } from '../../common/primitives.schema';
|
||||
import { cardCountSchema, cardDateSchema, cardText } from './card-parts';
|
||||
|
||||
import { z } from 'zod';
|
||||
|
||||
/**
|
||||
* A trip the chat outside any one trip offers to go on with in that trip's own
|
||||
* chat: the conversation there knows the trip's days and places and can change
|
||||
* them, which the chat on the dashboard does not. The question is what the
|
||||
* user asked about the trip, handed over so it waits there ready to send.
|
||||
*/
|
||||
|
||||
/** Where the trip stands today: under way, the next one to start, later, behind, or without dates. */
|
||||
export const TRIP_CHAT_PHASES = ['now', 'next', 'upcoming', 'past', 'undated'] as const;
|
||||
export type TripChatPhase = (typeof TRIP_CHAT_PHASES)[number];
|
||||
|
||||
export const tripChatCardSchema = z.object({
|
||||
kind: z.literal('tripChat'),
|
||||
trip: z.object({
|
||||
id: idSchema,
|
||||
title: cardText(200),
|
||||
startDate: cardDateSchema.nullable(),
|
||||
endDate: cardDateSchema.nullable(),
|
||||
phase: z.enum(TRIP_CHAT_PHASES),
|
||||
placeCount: cardCountSchema.nullable(),
|
||||
}),
|
||||
/** The question to ask in the trip's chat, in the user's words; null when there is none to hand over. */
|
||||
question: cardText(500).nullable(),
|
||||
});
|
||||
export type TripChatCard = z.infer<typeof tripChatCardSchema>;
|
||||
Reference in New Issue
Block a user