Files
TREK/client/src/components/Planner/stayFormModel.ts
T
Maurice 554d6f1b9f feat(tours): a city walk prepared from a briefing over MCP, and the station fields an assistant reads spelled out (CW-10, CW-11)
An assistant connected over MCP can now prepare a city walk from what the
traveller tells it. propose_city_walk takes a start (and an end), a time
frame on a day of the trip, the themes of CW-05, the stations the
traveller wishes for, the places of the trip it may use and fixed
appointments with their times, and saves nothing. Places for the themes
come from the search along a tour, at most eight, each with the theme,
category and source that brought it; nothing is made up. The stations are
ordered on estimates over the straight distance, and a place of a theme or
of the trip goes in only where it is open on arrival, keeps the frame and
the appointments, and leaves every station already on the walk open when
the walk gets there. The walk in that order is then planned on foot by the
router the road trip uses (the traveller's own routing server where they
set one), and its legs give the walking distance and time from one station
to the next. Where the router cannot be reached, or a station lies more
than 300 m off every way (the planner's limit, now shared with the
server), the times are estimates over the straight distance with an
allowance for streets at 4.5 km/h, and the answer says so and why. The
answer gives the arrival and leave times, the stay where the traveller
gave it and unknown otherwise, which makes every later arrival the
earliest one, and the opening hours checked against the arrival on that
day with the helpers the planner uses. Walking and stays are counted
apart, the total is complete only when every stay is known, and the
minutes the frame leaves for the unknown stays are named. Free time before
an appointment or at the end, wishes and appointments that cannot be kept
as asked, and places left out with their reason are listed; a tour of the
trip offered as a station is named as a tour. A wish or an appointment is
never dropped; an appointment is a must and protected. A place's default
duration is not read as a stay: every place carries one, so it says
nothing.

Once the traveller confirms, create_city_walk saves the walk through the
same creation the planner's save uses: one write, a key for every station,
the links to the trip's places checked, the trip told, with place editing
permission on the trip. Its way on foot is planned by the same router and
saved with the walk, together with its distance and walking time. Where
the router gives no way, the walk is saved with its stations only and the
answer says why: no line, no distance and no walking time. The tours list
says so (route_pending), the tour detail says in one line that the way is
planned in the planner, and opening the walk in the planner plans its way
at once and offers to save it, with a note once it is planned that it was
just planned. Such a walk can be saved as a new tour and taken over into
another trip, starting at its first stop, and is never offered as a place
to stay. A tour without a line needs two points of its own.

get_tour_plan now spells out every field of a city walk's stations: key,
name, stay (null when not known), importance (null for normal), protection
(false when not set) and opening hours (null when none are known);
list_tours names the station count and whether a walk waits for its way,
get_city_walk_progress gives each station's key and opening hours, and
take_over_tour says a walk keeps its stations with their protection. No
tool changes a walk's stations; deleting a walk's place takes its stations
along, protected ones included, and both delete tools now ask the
assistant to name those and ask the traveller first. REST answers the
stations as before.

A tour create refused for a type that is switched off, a description that
is too long or planned times out of range is a domain refusal now, so an
assistant gets the same text REST sends; REST still answers it as an error
at 400.
2026-10-20 07:09:57 +00:00

108 lines
4.0 KiB
TypeScript

import type { Accommodation, Day } from '../../types';
import { stayPlaces } from '../../utils/stayPlaces';
type Translate = (key: string, params?: Record<string, string | number>) => string;
/**
* The stay editor both shells show over the same rules: the desktop day panel's
* hotel picker and the phone's accommodation sheet. Each keeps its own state; the
* shape of a range, the day options, the places on offer and the request body are
* worked out here.
*/
/** The days a stay spans; `N` is how a shell writes "no day yet". */
export interface StayRange<N extends number | undefined> {
start: N;
end: N;
}
/** The stay's own fields as the editor holds them; `P` is how a shell writes "no place picked". */
export interface StayFormFields<P> {
check_in: string;
check_in_end: string;
check_out: string;
confirmation: string;
place_id: P;
}
/** The day options of the range pickers: the day's title, its date as the badge, or its number when titled. */
export function stayDayOptions(days: Day[], t: Translate, locale: string) {
return days.map((d, i) => ({
value: d.id,
label: d.title || t('planner.dayN', { n: i + 1 }),
badge: d.date
? new Date(d.date + 'T00:00:00Z').toLocaleDateString(locale, { day: 'numeric', month: 'short', timeZone: 'UTC' })
: d.title
? t('planner.dayN', { n: i + 1 })
: undefined,
}));
}
const position = (days: { id: number }[], id: number | undefined) => days.findIndex((d) => d.id === id);
/** A new first day; a range that would end before it is pulled along to it. */
export function stayRangeFromStart<N extends number | undefined>(
days: { id: number }[],
prev: StayRange<N>,
id: number
): StayRange<N | number> {
return { start: id, end: position(days, id) > position(days, prev.end) ? id : prev.end };
}
/** A new last day; a range that would start after it is pulled back to it. */
export function stayRangeFromEnd<N extends number | undefined>(
days: { id: number }[],
prev: StayRange<N>,
id: number
): StayRange<N | number> {
return { start: position(days, id) < position(days, prev.start) ? id : prev.start, end: id };
}
/**
* The day after the given one, which is where a new stay checks out by default: a
* stay virtually never checks out the day it checks in. Undefined on the trip's
* last day or for a day the trip does not have.
*/
export function stayCheckoutDay(days: { id: number }[], dayId: number | undefined): number | undefined {
const idx = position(days, dayId);
return (idx >= 0 && days[idx + 1]?.id) || undefined;
}
/** The editor's fields for an existing stay; `emptyPlace` stands in for a stay without a place. */
export function stayFormFrom<P>(
acc: Pick<Accommodation, 'check_in' | 'check_in_end' | 'check_out' | 'confirmation' | 'place_id'>,
emptyPlace: P
): StayFormFields<number | P> {
return {
check_in: acc.check_in || '',
check_in_end: acc.check_in_end || '',
check_out: acc.check_out || '',
confirmation: acc.confirmation || '',
place_id: acc.place_id ?? emptyPlace,
};
}
/**
* The places a stay can be at (no GPX tracks or tours, but always the one already picked),
* narrowed to a category when one is chosen.
*/
export function stayPlaceChoices<
P extends { id: number; route_geometry?: string | null; tour_place_id?: number | null; category_id?: number | null },
>(places: P[], placeId: number | string | null | undefined, categoryId: number | null): P[] {
const offered = stayPlaces<P>(places, placeId);
return categoryId != null ? offered.filter((p) => p.category_id === categoryId) : offered;
}
/** The request body for creating or updating a stay; empty times and codes are sent as null. */
export function stayRequestBody<P, N extends number | undefined>(form: StayFormFields<P>, range: StayRange<N>) {
return {
place_id: form.place_id,
start_day_id: range.start,
end_day_id: range.end,
check_in: form.check_in || null,
check_in_end: form.check_in_end || null,
check_out: form.check_out || null,
confirmation: form.confirmation || null,
};
}