mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-23 13:23:04 +00:00
feat(tours): read the walking time left of a city walk on a day over MCP, apart from its stays and the whole walk
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
import { Places } from '../../db/entities/Places.entity';
|
||||
import { TourStationVisits } from '../../db/entities/TourStationVisits.entity';
|
||||
import { TourWaypoints } from '../../db/entities/TourWaypoints.entity';
|
||||
import { Tours } from '../../db/entities/Tours.entity';
|
||||
import type { PlacesRepository } from '../../db/repositories/Places.repository';
|
||||
import type {
|
||||
TourStationVisitRow,
|
||||
TourStationVisitsRepository,
|
||||
@@ -8,15 +10,26 @@ import type {
|
||||
import type { TourWaypointRow, TourWaypointsRepository } from '../../db/repositories/TourWaypoints.repository';
|
||||
import type { ToursRepository } from '../../db/repositories/Tours.repository';
|
||||
import { PlacesService } from '../places/places.service';
|
||||
import { parseRouteGeometry } from './tours.helpers';
|
||||
import { InjectRepository } from '@mikro-orm/nestjs';
|
||||
import { Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { walkStationsOf, type TourVisit, type TourVisitListResponse, type TourVisitUpdateRequest } from '@trek/shared';
|
||||
import {
|
||||
isClosedLoop,
|
||||
walkRemainderOf,
|
||||
walkStationsOf,
|
||||
type TourVisit,
|
||||
type TourVisitListResponse,
|
||||
type TourVisitUpdateRequest,
|
||||
type WalkRemainder,
|
||||
} from '@trek/shared';
|
||||
|
||||
/** A city walk on one of its days: the assignment, the walk and its stations in order. */
|
||||
export interface WalkOnDay {
|
||||
assignmentId: number;
|
||||
dayId: number;
|
||||
placeId: number;
|
||||
/** The walk ends where it starts, so its line leads back to the start after the last station. */
|
||||
loop: boolean;
|
||||
stations: TourWaypointRow[];
|
||||
visits: TourStationVisitRow[];
|
||||
}
|
||||
@@ -33,6 +46,7 @@ export class TourVisitsService {
|
||||
@InjectRepository(TourStationVisits) private readonly visitsRepo: TourStationVisitsRepository,
|
||||
@InjectRepository(Tours) private readonly toursRepo: ToursRepository,
|
||||
@InjectRepository(TourWaypoints) private readonly waypointsRepo: TourWaypointsRepository,
|
||||
@InjectRepository(Places) private readonly placesRepo: PlacesRepository,
|
||||
private readonly places: PlacesService,
|
||||
) {}
|
||||
|
||||
@@ -52,6 +66,7 @@ export class TourVisitsService {
|
||||
assignmentId,
|
||||
dayId: assignment.day_id,
|
||||
placeId: assignment.place_id,
|
||||
loop: isClosedLoop(points),
|
||||
stations: walkStationsOf(points),
|
||||
visits: await this.visitsRepo.listForAssignment(assignmentId),
|
||||
};
|
||||
@@ -59,6 +74,40 @@ export class TourVisitsService {
|
||||
|
||||
/** CW-09: a walk on one day, read for an assistant; nothing changes. */
|
||||
async progress(tripId: number, placeId: number, dayId: number): Promise<WalkProgress> {
|
||||
return (await this.dayRun(tripId, placeId, dayId)).progress;
|
||||
}
|
||||
|
||||
/**
|
||||
* CW-13: what is left of a walk on one day, read for an assistant: the
|
||||
* reference it counts from, the walking time left along the saved line and
|
||||
* the stays still open, apart. Read afresh each time, so a station checked
|
||||
* off since counts; nothing changes.
|
||||
*/
|
||||
async remaining(tripId: number, placeId: number, dayId: number): Promise<WalkRemaining> {
|
||||
const [{ walk, progress }, place] = await Promise.all([
|
||||
this.dayRun(tripId, placeId, dayId),
|
||||
this.placesRepo.findInTrip(placeId, tripId),
|
||||
]);
|
||||
const remainder = walkRemainderOf({
|
||||
stations: progress.stations.map((station) => ({ ...station, stayMinutes: station.stay_minutes })),
|
||||
line: parseRouteGeometry(place?.route_geometry),
|
||||
walkingMinutes: progress.walking_minutes,
|
||||
});
|
||||
return {
|
||||
place_id: placeId,
|
||||
day_id: progress.day_id,
|
||||
assignment_id: progress.assignment_id,
|
||||
loop: walk.loop,
|
||||
...remainder,
|
||||
derived: REMAINDER_DERIVED,
|
||||
};
|
||||
}
|
||||
|
||||
private async dayRun(
|
||||
tripId: number,
|
||||
placeId: number,
|
||||
dayId: number,
|
||||
): Promise<{ walk: WalkOnDay; progress: WalkProgress }> {
|
||||
const assignmentId = await this.visitsRepo.assignmentOnDay(tripId, placeId, dayId);
|
||||
if (assignmentId === undefined) throw new NotFoundException('City walk not on this day');
|
||||
const walk = await this.walkOnDay(tripId, assignmentId);
|
||||
@@ -78,7 +127,7 @@ export class TourVisitsService {
|
||||
linked_place_id: station.linked_place_id ?? null,
|
||||
}));
|
||||
const open = stations.filter((station) => station.status === 'planned');
|
||||
return {
|
||||
const progress: WalkProgress = {
|
||||
place_id: placeId,
|
||||
day_id: walk.dayId,
|
||||
assignment_id: assignmentId,
|
||||
@@ -90,6 +139,7 @@ export class TourVisitsService {
|
||||
},
|
||||
walking_minutes: tour?.duration ?? null,
|
||||
};
|
||||
return { walk, progress };
|
||||
}
|
||||
|
||||
/** Marks a station of a walk on one of its days, or takes it back to planned. */
|
||||
@@ -144,6 +194,24 @@ export interface WalkProgress {
|
||||
walking_minutes: number | null;
|
||||
}
|
||||
|
||||
/** The fields of a remaining time worked out on reading, never stored with the walk. */
|
||||
const REMAINDER_DERIVED = [
|
||||
'reference',
|
||||
'finished',
|
||||
'walking.remaining_minutes',
|
||||
'walking.after_last_station_minutes',
|
||||
'open_stations.walk_minutes_from_reference',
|
||||
] as const;
|
||||
|
||||
/** CW-13: what is left of a walk on one day, for an assistant. */
|
||||
export type WalkRemaining = {
|
||||
place_id: number;
|
||||
day_id: number;
|
||||
assignment_id: number;
|
||||
loop: boolean;
|
||||
derived: readonly string[];
|
||||
} & WalkRemainder;
|
||||
|
||||
/** A visit as the routes and the trip's socket send it. */
|
||||
export function toVisit(row: TourStationVisitRow): TourVisit {
|
||||
return { assignment_id: row.assignment_id, station_key: row.station_key, status: row.status };
|
||||
|
||||
@@ -178,7 +178,7 @@ export class ToursMcp {
|
||||
@Tool({
|
||||
name: 'get_city_walk_progress',
|
||||
description:
|
||||
'Read how far a city walk (tour_type walk) got on one day it is on: every station in order with its key (its own id, as get_tour_plan gives it), its status that day (planned, visited or skipped, as the travellers checked them off in the day plan), its importance (normal, optional, must; get_tour_plan says null for normal), whether it is protected (a station a proposed change must leave where it is, neither removed, replaced nor moved), its stay in minutes (null when not known, 0 for a point passed through), its opening hours as OpenStreetMap writes them (null when none is known) and its linked place. "open" sums what is still planned: the stations, their known stays and how many stays are unknown; walking_minutes is the walking time of the whole walk. Visits belong to that day only: the same walk on another day, or a copy, has none. Anything the traveller tells you besides (for instance "continue from 2 pm") is theirs to say, not stored here. Reading changes nothing.',
|
||||
'Read how far a city walk (tour_type walk) got on one day it is on: every station in order with its key (its own id, as get_tour_plan gives it), its status that day (planned, visited or skipped, as the travellers checked them off in the day plan), its importance (normal, optional, must; get_tour_plan says null for normal), whether it is protected (a station a proposed change must leave where it is, neither removed, replaced nor moved), its stay in minutes (null when not known, 0 for a point passed through), its opening hours as OpenStreetMap writes them (null when none is known) and its linked place. "open" sums what is still planned: the stations, their known stays and how many stays are unknown; walking_minutes is the walking time of the whole walk. Visits belong to that day only: the same walk on another day, or a copy, has none. Anything the traveller tells you besides (for instance "continue from 2 pm") is theirs to say, not stored here. For the walking time still ahead, read get_city_walk_remaining_time: walking_minutes here is the whole walk. Reading changes nothing.',
|
||||
inputSchema: {
|
||||
tripId: idSchema,
|
||||
placeId: idSchema.describe('The place id of the city walk, as list_tours gives it'),
|
||||
@@ -201,6 +201,32 @@ export class ToursMcp {
|
||||
}
|
||||
}
|
||||
|
||||
@Tool({
|
||||
name: 'get_city_walk_remaining_time',
|
||||
description:
|
||||
'Read what is left of a city walk (tour_type walk) on one day it is on, from the stations the travellers checked off that day: the walking time still ahead, the stays still open and the stations still open, each apart. reference says what the estimate counts from: start (nothing visited yet), last_visited (the last station checked off as visited; its stay counts as done) or ambiguous (a station is still planned before the last one visited, so where the walk stands cannot be told: ask the travellers where they are rather than guessing). A status says what was done, never where anyone is now: live_position is always false, TREK knows no live position and no clock. walking.total_minutes is the walking time of the whole walk as saved; walking.remaining_minutes is the part of it still ahead of the reference, the saved walking time shared out by how far along the saved line each station is passed (never by how many stations are open), so stretches already walked are not in it; it holds no stay. walking.upper_bound is true when a station ahead was skipped (listed in skipped_ahead): the saved line still leads through it and the shorter way was never planned, so the time is at most that. walking.after_last_station_minutes is the part from the last station to the end of the line, for a loop (loop true) the way back to its start, still ahead even once every station is done. walking.remaining_minutes is null, with unknown_because, when it cannot be told: no_stations, route_pending (saved with its points only, no line yet), no_walking_time or progress_ambiguous; it is never given as 0 or as the whole walk instead. stays.remaining_known_minutes sums the stays of the open stations as far as entered, stays.unknown lists the open stations whose stay is not known (never counted as 0), stays.optional_known_minutes is the part of the known minutes at optional stations. open_stations lists the stations still planned in order, with importance, stay_minutes and walk_minutes_from_reference (walking time from the reference to it, null where the remaining time is not known); a skipped station is done and not in it. finished is true when no station is open. Visits belong to that day only: the same walk on another day, or a copy, starts from the beginning. A time the traveller tells you (for instance "continue from 2 pm") is theirs to add, not stored here. derived names the fields worked out on reading. Reading changes nothing: no status, tour or day plan.',
|
||||
inputSchema: {
|
||||
tripId: idSchema,
|
||||
placeId: idSchema.describe('The place id of the city walk, as list_tours gives it'),
|
||||
dayId: idSchema.describe('The day the walk is on, as find_tours_for_day gives it'),
|
||||
},
|
||||
annotations: TOOL_ANNOTATIONS_READONLY,
|
||||
when: addonGate(ADDON_IDS.TOURS),
|
||||
access: { group: 'trips', mode: 'read' },
|
||||
})
|
||||
async getCityWalkRemainingTime(
|
||||
{ tripId, placeId, dayId }: { tripId: number; placeId: number; dayId: number },
|
||||
ctx: McpContext,
|
||||
) {
|
||||
if (!(await this.access.findAccessible(tripId, ctx.userId))) return noAccess();
|
||||
try {
|
||||
return ok(await this.visits.remaining(tripId, placeId, dayId));
|
||||
} catch (err: unknown) {
|
||||
if (err instanceof NotFoundException) return errorResult('No city walk of this trip is on that day.');
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
@Tool({
|
||||
name: 'find_tours_for_day',
|
||||
description: 'List the saved tours put on one day of a trip, with the same facts as list_tours.',
|
||||
|
||||
Reference in New Issue
Block a user