feat(journey): the assistant designs the whole book as one proposal

design_book, revise_book_design and apply_book_design work on a draft, the
apply keeps the old book as a revision for undo, skips pages changed since,
and broadcasts once after the commit. Book level tools set the page setup,
duplicate pages and save layouts.
This commit is contained in:
Maurice
2026-10-22 21:25:46 +00:00
parent 09ffc7d946
commit bda07d6a3e
55 changed files with 4327 additions and 37 deletions
+24
View File
@@ -1,9 +1,14 @@
import { bookElementIdSchema, bookSpreadIdSchema } from '../book/book-ops.schema';
import { idParamSchema, idSchema } from '../common/primitives.schema';
import { assistantPageRenderSchema, assistantPageViewSchema } from './assistant-vision.schema';
import {
assistantCardSchema,
assistantCardWriteSchema,
ATLAS_APPLIED_ENTITIES,
ATLAS_VERB_KEYS,
BOOK_APPLIED_ENTITIES,
BOOK_DESIGN_VERB_KEYS,
BOOK_VERB_KEYS,
BOOKING_APPLIED_ENTITIES,
BOOKING_VERB_KEYS,
BUDGET_APPLIED_ENTITIES,
@@ -116,6 +121,8 @@ export const ASSISTANT_CHANGE_VERB_KEYS = [
...VACAY_VERB_KEYS,
...JOURNEY_VERB_KEYS,
...PACKING_VERB_KEYS,
...BOOK_VERB_KEYS,
...BOOK_DESIGN_VERB_KEYS,
] as const;
export const assistantChangeVerbKeySchema = z.enum(ASSISTANT_CHANGE_VERB_KEYS);
export type AssistantChangeVerbKey = z.infer<typeof assistantChangeVerbKeySchema>;
@@ -202,6 +209,9 @@ export const assistantFocusSchema = z
dayId: idSchema.optional(),
placeIds: z.array(idSchema).max(20).optional(),
view: assistantMapViewSchema.optional(),
/** In the Studio: the page (spread) on screen, and the elements selected on it. */
spreadId: bookSpreadIdSchema.optional(),
elementIds: z.array(bookElementIdSchema).max(30).optional(),
})
.strict();
export type AssistantFocus = z.infer<typeof assistantFocusSchema>;
@@ -220,6 +230,8 @@ export const assistantChatRequestSchema = assistantClientCtxSchema
text: z.string().trim().min(1).max(32_000),
focus: assistantFocusSchema.optional(),
mentions: 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(),
})
.strict();
export type AssistantChatRequest = z.infer<typeof assistantChatRequestSchema>;
@@ -249,6 +261,8 @@ export type AssistantDecision = z.infer<typeof assistantDecisionSchema>;
export const assistantDecisionsRequestSchema = assistantClientCtxSchema
.extend({
decisions: 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(),
})
.strict();
export type AssistantDecisionsRequest = z.infer<typeof assistantDecisionsRequestSchema>;
@@ -382,6 +396,7 @@ const assistantAppliedEntitySchema = z.enum([
...VACAY_APPLIED_ENTITIES,
...JOURNEY_APPLIED_ENTITIES,
...PACKING_APPLIED_ENTITIES,
...BOOK_APPLIED_ENTITIES,
]);
/** An applied change as the server writes it: what it touched, and until when it can be undone. */
@@ -579,6 +594,8 @@ function messagePartSchema<
z.object({ type: z.literal('data-trek-card'), data: card }),
// Only on a user message.
z.object({ type: z.literal('data-trek-mentions'), data: assistantMentionsSchema }),
// A Studio page the turn looked at: what it was and its description, never the image.
z.object({ type: z.literal('data-trek-page-view'), data: assistantPageViewSchema }),
]);
}
@@ -679,6 +696,13 @@ export const uiStreamChunkSchema = z.discriminatedUnion('type', [
data: assistantActivitySchema,
transient: z.literal(true).optional(),
}),
// A Studio page the turn asks the Studio tab to draw and upload; the message never keeps it.
z.object({
type: z.literal('data-trek-render-request'),
data: assistantPageRenderSchema,
transient: z.literal(true).optional(),
}),
z.object({ type: z.literal('data-trek-page-view'), data: assistantPageViewSchema }),
z.object({
type: z.literal('data-trek-quota'),
data: z.object({ used: z.number().int().nonnegative(), limit: z.number().int().nonnegative() }),
@@ -0,0 +1,90 @@
import { ASSISTANT_CHANGE_VERB_KEYS, assistantChangePreviewSchema } from '../assistant.schema';
import { BOOK_DESIGN_CARD_LIMITS, bookDesignCardSchema } from './book-design.card';
import { BOOK_DESIGN_VERB_KEYS } from './book-design.contract';
import { storedTrekCardSchema, trekCardSchema } from './index';
import { describe, expect, it } from 'vitest';
// BKDCARD-001 to BKDCARD-004
function card(over: Record<string, unknown> = {}) {
return {
kind: 'bookDesign',
change: 'proposed',
journeyId: 4,
designId: 'd-1',
baseVersion: 7,
style: 'editorial',
paper: '#faf8f4',
ink: '#1a1a1a',
accent: '#b45309',
heading: 'playfair',
body: 'garamond',
spreads: [
{
spreadId: 'sp-1',
index: 0,
action: 'replace',
beat: 'cover',
chapter: null,
entryId: null,
photoCount: 1,
severe: 0,
warn: 0,
},
{
spreadId: 'sp-9',
index: 3,
action: 'insert',
beat: 'hero',
chapter: 'Alfama',
entryId: 12,
photoCount: 1,
severe: 0,
warn: 1,
},
],
more: 0,
leftOut: 3,
revisionId: null,
skipped: [],
...over,
};
}
describe('the book design card', () => {
it('BKDCARD-001: reads a design card through the union, as itself', () => {
const parsed = trekCardSchema.parse(card());
expect(parsed.kind).toBe('bookDesign');
expect(bookDesignCardSchema.parse(parsed).spreads).toHaveLength(2);
expect(storedTrekCardSchema.parse(card({ change: 'applied', revisionId: 5 }))).toMatchObject({ revisionId: 5 });
});
it('BKDCARD-002: a field past its bound, an unknown style or reason, reads as no card', () => {
const page = card().spreads[1];
const longChapter = [{ ...page, chapter: 'x'.repeat(BOOK_DESIGN_CARD_LIMITS.chapter + 1) }];
expect(storedTrekCardSchema.parse(card({ spreads: longChapter }))).toBeNull();
expect(storedTrekCardSchema.parse(card({ style: 'baroque' }))).toBeNull();
expect(storedTrekCardSchema.parse(card({ skipped: [{ spreadId: 'sp-1', reason: 'tired' }] }))).toBeNull();
const many = Array.from({ length: BOOK_DESIGN_CARD_LIMITS.spreads + 1 }, () => page);
expect(storedTrekCardSchema.parse(card({ spreads: many }))).toBeNull();
});
it('BKDCARD-003: rides on a change preview with its own verb', () => {
const preview = assistantChangePreviewSchema.parse({
toolCallId: 'call_1',
verbKey: 'designBook',
subject: '',
rows: [],
dayId: null,
placeIds: [],
undoable: true,
card: card(),
});
expect([preview.verbKey, preview.card?.kind]).toEqual(['designBook', 'bookDesign']);
});
it('BKDCARD-004: names the verbs of the book-level changes in the shared enum', () => {
expect(ASSISTANT_CHANGE_VERB_KEYS).toEqual(expect.arrayContaining([...BOOK_DESIGN_VERB_KEYS]));
});
});
@@ -0,0 +1,66 @@
import { BOOK_DESIGN_SKIP_REASONS } from '../../book/book-design-tools.schema';
import { bookDesignIdSchema, bookSpreadIdSchema } from '../../book/book-ops.schema';
import { BOOK_FONTS_IDS } from '../../book/book.schema';
import { BOOK_DESIGN_STYLES } from '../../book/design/design-directives.schema';
import { BOOK_DESIGN_ACTIONS } from '../../book/design/design-merge';
import { BOOK_DESIGN_BEATS } from '../../book/design/design-pacing';
import { idSchema } from '../../common/primitives.schema';
import { cardCountSchema, cardHexColorSchema, cardText } from './card-parts';
import { z } from 'zod';
/**
* A whole-book design as the Studio's chat shows it: the book's look (style,
* paper, ink, accent, the two typefaces) and the plan page by page, what
* each page becomes and how many findings the server's check left on it.
* `none` is the answer of `design_book` (a draft, nothing proposed yet),
* `proposed` the held `apply_book_design`, `applied` its receipt with the
* revision that takes it back and the pages it left as they were. The pages
* themselves are drawn from the draft (GET /api/journeys/:id/book-designs/:designId),
* never carried in the card.
*/
export const BOOK_DESIGN_CARD_LIMITS = { spreads: 120, chapter: 40 } as const;
export const BOOK_DESIGN_CARD_CHANGES = ['none', 'proposed', 'applied'] as const;
/** One page of the plan as a card names it. */
export const bookDesignCardSpreadSchema = z.object({
spreadId: bookSpreadIdSchema,
index: cardCountSchema,
action: z.enum(BOOK_DESIGN_ACTIONS),
beat: z.enum(BOOK_DESIGN_BEATS),
chapter: cardText(BOOK_DESIGN_CARD_LIMITS.chapter).nullable(),
entryId: idSchema.nullable(),
photoCount: cardCountSchema,
severe: cardCountSchema,
warn: cardCountSchema,
});
export type BookDesignCardSpread = z.infer<typeof bookDesignCardSpreadSchema>;
export const bookDesignCardSchema = z.object({
kind: z.literal('bookDesign'),
change: z.enum(BOOK_DESIGN_CARD_CHANGES),
journeyId: idSchema,
designId: bookDesignIdSchema,
/** The book's version the draft was made against; null for a journey without a book. */
baseVersion: cardCountSchema.nullable(),
style: z.enum(BOOK_DESIGN_STYLES),
paper: cardHexColorSchema,
ink: cardHexColorSchema,
accent: cardHexColorSchema,
heading: z.enum(BOOK_FONTS_IDS),
body: z.enum(BOOK_FONTS_IDS),
spreads: z.array(bookDesignCardSpreadSchema).max(BOOK_DESIGN_CARD_LIMITS.spreads),
/** Plan pages past the ones listed. */
more: cardCountSchema,
/** Photos of the journey the design left out. */
leftOut: cardCountSchema,
/** Once applied: the revision the undo restores. */
revisionId: idSchema.nullable(),
/** Once applied: the pages the apply left as they were, and why. */
skipped: z
.array(z.object({ spreadId: bookSpreadIdSchema, reason: z.enum(BOOK_DESIGN_SKIP_REASONS) }))
.max(BOOK_DESIGN_CARD_LIMITS.spreads),
});
export type BookDesignCard = z.infer<typeof bookDesignCardSchema>;
@@ -0,0 +1,19 @@
import { bookDesignCardSchema } from './book-design.card';
export * from './book-design.card';
/**
* What the assistant's whole-book design brings to the chat: the card of a
* design and its proposal, and the verbs of the book-level changes (the
* design applied, the page format and numbers, a page copied, a layout kept
* or taken away). They touch pages of the book, so they name the photo
* book's own entity (`book_page`, book.contract.ts) and add none.
*/
export const BOOK_DESIGN_CARD_SCHEMAS = [bookDesignCardSchema] as const;
export const BOOK_DESIGN_VERB_KEYS = [
'designBook',
'setBookSetup',
'duplicateBookPage',
'saveBookLayout',
'removeBookLayout',
] as const;
+6
View File
@@ -1,4 +1,6 @@
import { ATLAS_CARD_SCHEMAS } from './atlas.contract';
import { BOOK_DESIGN_CARD_SCHEMAS } from './book-design.contract';
import { BOOK_CARD_SCHEMAS } from './book.contract';
import { BOOKING_CARD_SCHEMAS } from './booking.contract';
import { BUDGET_CARD_SCHEMAS } from './budget.contract';
import { COLLAB_CARD_SCHEMAS } from './collab.contract';
@@ -13,6 +15,8 @@ import { z } from 'zod';
export * from './card-parts';
export * from './atlas.contract';
export * from './book.contract';
export * from './book-design.contract';
export * from './booking.contract';
export * from './budget.contract';
export * from './collab.contract';
@@ -41,6 +45,8 @@ export const trekCardSchema = z.discriminatedUnion('kind', [
...VACAY_CARD_SCHEMAS,
...JOURNEY_CARD_SCHEMAS,
...PACKING_CARD_SCHEMAS,
...BOOK_CARD_SCHEMAS,
...BOOK_DESIGN_CARD_SCHEMAS,
]);
export type TrekCard = z.infer<typeof trekCardSchema>;
export type TrekCardKind = TrekCard['kind'];