feat(mcp): the page edit takes a map's scope and view checked whole

The scope of a new map is one object (tripId, country, date or entryId)
rather than a union of five, and style's view is four numbers checked
against the document's bounds, so edit_book_page and revise_book_design
grow by about 800 characters instead of 1400. Their ceiling moves from
45 000 to 46 000 with the measured size noted beside it.
This commit is contained in:
Maurice
2026-11-30 14:39:35 +01:00
parent d6fd916098
commit 83f3fee848
4 changed files with 45 additions and 20 deletions
@@ -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);
});
+36 -10
View File
@@ -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
+1 -5
View File
@@ -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'])
+5 -4
View File
@@ -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<TravelSpec, { kind: 'map' }>['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. */