feat(tours): prepare_tour_change prepares a change to a saved city walk for review

An assistant can now prepare a change to a saved city walk and show the
traveller what it would do before anything is saved. Preparing writes
nothing: no tour, station, visit or day plan changes, and turning the
proposal down keeps the plan as it was.

- The changes apply in order and name the saved stations by their keys:
  add a station after another (a spontaneous stop is an add after the
  station the traveller is at), remove, replace and move. Stops put after
  the same station keep the order they were given in.
- What the travellers did stays as it is: a station visited or skipped on
  any day of the walk is never removed, replaced or moved, nothing goes
  before the last station already done, a protected station keeps its
  place and a leg drawn by hand stays as drawn. A proposal breaking a rule
  is refused whole, naming every change and why. Other tour types, and a
  walk that is a recorded track, are refused too: so far only a city walk
  takes prepared changes, since the others plan their way with routing of
  their own the server cannot run.
- The answer sets the walk after the change against the saved one: each
  station with what changed it and where it stood, what was removed, the
  way on foot before and after planned by the same router (the stored
  distance and walking time given apart, never mixed in), the stays as far
  as known, every leg with the new ones marked, the gaps that could not be
  worked out, a warning for a must station removed or replaced, and each
  new station as create_place takes it, to keep it for later as a place of
  the trip instead.
- The proposal names the revision it was prepared on, so a save of it is
  refused once the tour has moved on, and says whether the traveller may
  change the trip's places at all.

The way on foot of a new walk, its creation and a prepared change now come
from one CityWalkFootWayService instead of a copy in the briefing service.
This commit is contained in:
Maurice
2026-10-20 12:29:11 +02:00
parent b826ecd425
commit 1969ca57d9
16 changed files with 1455 additions and 33 deletions
+1
View File
@@ -64,6 +64,7 @@ export * from './tours/tour-trail-grade.schema';
export * from './tours/tour-walk.schema';
export * from './tours/tour-walk';
export * from './tours/tour-walk-briefing.schema';
export * from './tours/tour-change.schema';
export * from './tours/walk-hours';
export * from './tours/tours.metrics';
export * from './tours/tour-gpx-import.schema';
@@ -0,0 +1,36 @@
import { TOUR_CHANGE_MAX, TOUR_CHANGE_OPS, tourChangeRequestSchema, tourChangeSchema } from './tour-change.schema';
import { describe, expect, it } from 'vitest';
const cafe = { lat: 48.1, lng: 11.5, name: 'Cafe', stay_minutes: 20 };
describe('a change to a saved tour prepared for review (MCP-T02)', () => {
it('takes an add, a remove, a replace and a move, each naming its stations by key', () => {
expect(tourChangeSchema.options.map((option) => option.shape.op.value)).toEqual([...TOUR_CHANGE_OPS]);
const changes = [
{ op: 'add', station: cafe, after_key: 'stop-gate' },
{ op: 'remove', key: 'stop-park' },
{ op: 'replace', key: 'stop-museum', station: { ...cafe, opening_hours: 'Mo-Su 08:00-18:00' } },
{ op: 'move', key: 'stop-market', after_key: 'stop-gate' },
];
expect(tourChangeRequestSchema.parse({ changes })).toEqual({ changes });
});
it('refuses a change it does not know, a key that is no key, a field a change does not carry and too many changes', () => {
const refused = [
{ changes: [] },
{ changes: [{ op: 'rename', key: 'stop-gate' }] },
{ changes: [{ op: 'remove', key: 'x' }] },
{ changes: [{ op: 'remove', key: 'stop-gate', after_key: 'stop-park' }] },
{ changes: [{ op: 'add', station: cafe }] },
{ changes: [{ op: 'add', station: { ...cafe, key: 'stop-new1' }, after_key: 'stop-gate' }] },
{ changes: [{ op: 'add', station: { ...cafe, lat: 91 }, after_key: 'stop-gate' }] },
{ changes: [{ op: 'move', key: 'stop-gate' }] },
{ changes: [{ op: 'remove', key: 'stop-gate' }], save: true },
{ changes: Array.from({ length: TOUR_CHANGE_MAX + 1 }, () => ({ op: 'remove', key: 'stop-gate' })) },
];
for (const body of refused) {
expect(tourChangeRequestSchema.safeParse(body).success, JSON.stringify(body)).toBe(false);
}
});
});
+48
View File
@@ -0,0 +1,48 @@
import { cityWalkStationSchema } from './tour-walk-briefing.schema';
import { stationKeySchema } from './tours.schema';
import { z } from 'zod';
/**
* MCP-T02: a change to a saved tour an assistant prepares for the traveller to
* review. Preparing it reads only: the answer shows the plan as it would be
* after the change, set against the saved plan, with the effects worked out
* and what could not be. Every change names the stations it touches by their
* keys, the same across saves, so it cannot drift onto another station. A
* spontaneous stop is a change like any other: an `add` after the station the
* traveller is at.
*/
/** The most changes one proposal carries: enough to rework a walk, few enough to review at once. */
export const TOUR_CHANGE_MAX = 10;
/** The operations a proposal can hold; which of them a tour type takes is the server's rule. */
export const TOUR_CHANGE_OPS = ['add', 'remove', 'replace', 'move'] as const;
export type TourChangeOp = (typeof TOUR_CHANGE_OPS)[number];
const key = stationKeySchema.describe('The key of a station of the saved tour, as get_tour_plan gives it');
const afterKey = stationKeySchema.describe(
'The key of the saved station it comes after; stops added after the same station keep the order they are given in',
);
const station = cityWalkStationSchema.describe(
'The new station: its position, name, stay in minutes (0 passed through, null not known), importance, protected, opening hours as OpenStreetMap writes them, and the place of this trip it stands for',
);
export const tourChangeSchema = z.discriminatedUnion('op', [
z.object({ op: z.literal('add'), station, after_key: afterKey }).strict(),
z.object({ op: z.literal('remove'), key }).strict(),
z.object({ op: z.literal('replace'), key, station }).strict(),
z.object({ op: z.literal('move'), key, after_key: afterKey }).strict(),
]);
export type TourChange = z.infer<typeof tourChangeSchema>;
export const tourChangeRequestSchema = z
.object({
changes: z
.array(tourChangeSchema)
.min(1)
.max(TOUR_CHANGE_MAX)
.describe('The changes in the order they apply, each to the plan the ones before it left'),
})
.strict();
export type TourChangeRequest = z.infer<typeof tourChangeRequestSchema>;