diff --git a/server/tests/unit/mcp/tool-input-schemas.test.ts b/server/tests/unit/mcp/tool-input-schemas.test.ts index b1ad9ecfa..9f70086b3 100644 --- a/server/tests/unit/mcp/tool-input-schemas.test.ts +++ b/server/tests/unit/mcp/tool-input-schemas.test.ts @@ -116,11 +116,13 @@ describe('MCP tool input schemas', () => { it('MCP-SCHEMA-004: the page edit and the design revision, which take every page operation, stay under their ceiling', () => { // Every element kind's add and style operation widen the op union; the design's // revision takes the same ops. A layout's undo takes its layout as JSON text, so it stays small. + // Measured at about 45 400 since a map's scope (country, day, stop), its own view and its coast + // (book schema revision 12), up from 44 600, with the scope and the view each checked whole to stay small. const sizeOf = (tool: string) => JSON.stringify([...schemas].find(([key]) => key.endsWith(`#${tool}`))?.[1] ?? null).length; for (const tool of ['edit_book_page', 'revise_book_design']) { expect(sizeOf(tool)).toBeGreaterThan(10_000); - expect(sizeOf(tool)).toBeLessThan(45_000); + expect(sizeOf(tool)).toBeLessThan(46_000); } expect(sizeOf('restore_book_layout')).toBeLessThan(2_000); }); diff --git a/shared/src/book/book-element-ops.schema.ts b/shared/src/book/book-element-ops.schema.ts index cf8d00371..dfc5d1cb0 100644 --- a/shared/src/book/book-element-ops.schema.ts +++ b/shared/src/book/book-element-ops.schema.ts @@ -14,7 +14,6 @@ import { bookMapCoastDetailSchema, bookMapSourceSchema, bookMapStyleSchema, - bookMapViewSchemaIn, bookMetricSchema, bookPhotoFilterSchema, bookPhotoFitSchema, @@ -45,6 +44,7 @@ import { bookIconElementSchema, bookListElementSchema, bookMapElementSchema, + bookMapViewSchema, bookShapeElementSchema, bookStatsElementSchema, bookStickerElementSchema, @@ -163,6 +163,18 @@ const STICKER_PROPS = { .optional(), flipX: sticker.flipX.unwrap().describe('Mirror the drawing left to right.').optional(), }; +/** + * A map's own view as `style` takes it. The bounds the document holds + * (`bookMapViewSchema`) are checked whole rather than spelled out per number: + * the page edit's schema, which every MCP client loads, is held to a size. + */ +const mapViewIn = z + .strictObject({ west: z.number(), south: z.number(), east: z.number(), north: z.number() }) + .refine((v) => bookMapViewSchema.safeParse(v).success, { + message: 'west to east (east up to 540), south to north, in degrees', + }) + .describe('Degrees; east may pass 180 across the antimeridian. null fits the route.'); + const MAP_PROPS = { ...COMMON, ...TYPESET, @@ -178,7 +190,7 @@ const MAP_PROPS = { clip: bookMapClipSchema.optional(), fitPadding: map.fitPadding.unwrap().optional(), fitToCountries: map.fitToCountries.unwrap().optional(), - view: bookMapViewSchemaIn.nullable().optional(), + view: mapViewIn.nullable().optional(), coastDetail: bookMapCoastDetailSchema.optional(), }; const STATS_PROPS = { @@ -275,18 +287,32 @@ const listItem = z.strictObject({ * it: the whole journey, one trip, the stops in one country or on one day * (either within a trip), or the stop of one entry. */ +const ISO_DAY = /^\d{4}-\d{2}-\d{2}$/; +/** One of country, date and entryId at most, an entryId alone, and a scope that names something. */ +const oneMapScope = (v: { tripId?: number; country?: string; date?: string; entryId?: number }) => { + const narrowed = [v.country, v.date, v.entryId].filter((x) => x !== undefined).length; + if (narrowed > 1 || (v.entryId !== undefined && v.tripId !== undefined)) return false; + if (v.country !== undefined && !iso2.safeParse(v.country).success) return false; + if (v.date !== undefined && !ISO_DAY.test(v.date)) return false; + return narrowed > 0 || v.tripId !== undefined; +}; +/* + * One object rather than a union of five, checked whole: the page edit's + * schema is held to a size (MCP-SCHEMA-004 on the server). + */ const mapScope = z .union([ z.literal('journey'), - z.strictObject({ tripId: idSchema }), - z.strictObject({ country: iso2, tripId: idSchema.optional() }), - z.strictObject({ - date: z.string().regex(/^\d{4}-\d{2}-\d{2}$/), - tripId: idSchema.optional(), - }), - z.strictObject({ entryId: idSchema }), + z + .strictObject({ + tripId: idSchema.optional(), + country: z.string().length(2).optional(), + date: z.string().length(10).optional(), + entryId: idSchema.optional(), + }) + .refine(oneMapScope, { message: 'a tripId, a country or a date (either within the trip), or an entryId alone' }), ]) - .describe('journey, a tripId, a country code, a date (YYYY-MM-DD) or the entryId of one stop.'); + .describe('journey; a tripId; a country or a date YYYY-MM-DD, each optionally within the tripId; or one entryId.'); /* * What a new element is, kind by kind: where its content comes from. A text diff --git a/shared/src/book/book-op-enums.ts b/shared/src/book/book-op-enums.ts index 4f598b916..3a056d3f0 100644 --- a/shared/src/book/book-op-enums.ts +++ b/shared/src/book/book-op-enums.ts @@ -11,7 +11,6 @@ import { bookCountriesElementSchema, bookListElementSchema, bookMapElementSchema, - bookMapViewSchema, bookPageSetupSchema, bookPhotoElementSchema, bookShapeElementSchema, @@ -118,10 +117,7 @@ export const bookMapClipSchema = map.clip .describe('rect is a map in a box; country cuts it to the outline of its countries.'); export const bookMapCoastDetailSchema = z .enum(BOOK_MAP_COAST_DETAILS) - .describe('simple leaves out small islands; detailed draws them, Hawaii too; auto picks by how far the map reaches.'); -export const bookMapViewSchemaIn = bookMapViewSchema.describe( - 'The ground in degrees, framed by hand; east may pass 180 (to 540) across the antimeridian. null fits the route again.', -); + .describe('simple drops small islands, detailed keeps them, auto decides by reach.'); /** Where a new map's ground comes from. */ export const bookMapSourceSchema = z .enum(['vector', 'relief', 'satellite']) diff --git a/shared/src/book/book-ops-elements.ts b/shared/src/book/book-ops-elements.ts index ec59c787f..94a6b5bbc 100644 --- a/shared/src/book/book-ops-elements.ts +++ b/shared/src/book/book-ops-elements.ts @@ -393,14 +393,15 @@ function newTravel(spec: TravelSpec, at: At, env: ElementOpEnv): BookElement { /** The scope a new map asked for, as the engine places it: an entry's stop found where the journey has it. */ function mapScopeOf(scope: Extract['scope'], env: ElementOpEnv): BookMapScope { if (scope === 'journey') return scope; - if ('entryId' in scope) { + if (scope.entryId !== undefined) { const stop = bookMapStopFor(statsOf(env), travelOf(env).entries, scope.entryId); if (!stop) throw failOp('entry_not_found', `entry ${scope.entryId} is not a stop of this journey`, env.opIndex); return { stop }; } - if ('country' in scope) return { country: scope.country.toUpperCase(), tripId: scope.tripId ?? null }; - if ('date' in scope) return { dayDate: scope.date, tripId: scope.tripId ?? null }; - return scope; + if (scope.country !== undefined) return { country: scope.country.toUpperCase(), tripId: scope.tripId ?? null }; + if (scope.date !== undefined) return { dayDate: scope.date, tripId: scope.tripId ?? null }; + // The schema lets no scope through that names nothing; a trip is what is left. + return scope.tripId === undefined ? 'journey' : { tripId: scope.tripId }; } /** The journey's figures when an entry is named (an entry needs them to be found), else whatever is known. */