mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-03 01:28:55 +00:00
80 lines
4.9 KiB
TypeScript
80 lines
4.9 KiB
TypeScript
const BOOKING = /\b(?:buchung(?:en)?|reservierung(?:en)?|bookings?|reservations?)\b/iu;
|
|
const RECORD = /\b(?:anlegen|eintragen|erfassen|erstellen|speichern|hinzufügen|add|create|log|record|save)\b/iu;
|
|
const NEGATED_BOOKING =
|
|
/\b(?:keine|keinerlei|no)\s+(?:buchung(?:en)?|reservierung(?:en)?|bookings?|reservations?)\b|\b(?:do not|don't)\s+(?:add|create|log|record|save)\b|\bnicht\s+(?:anlegen|eintragen|erfassen|erstellen|speichern|hinzufügen)\b/iu;
|
|
const PLACE_WORK = [
|
|
/\b(?:add|create|schedule|plan)\s+(?:(?:me|a|an|the|this|another|new)\s+){0,4}(?:places?|activities|activity|stops?|restaurants?|itinerary|(?:first|next) day)\b/iu,
|
|
/\b(?:ort|orte|aktivität|aktivitäten|station|tagesplan|reiseplan)(?:\s+(?:bitte|auch|noch|dazu|zusätzlich))*\s+(?:anlegen|einplanen|erstellen|hinzufügen)\b/iu,
|
|
/\b(?:und|and|zusätzlich|also|außerdem)\b[^.!?;\n]{0,65}\b(?:ort|orte|places?|activities|activity|stops?|tagesplan|reiseplan|itinerary)\b/iu,
|
|
/\b(?:den|das|die)\s+[^.!?;\n]{0,40}\b(?:als\s+(?:ort|stopp)|in\s+den\s+(?:tagesplan|reiseplan))\b/iu,
|
|
];
|
|
|
|
/** Recognize explicit booking-entry requests. Other turns keep the usual tool selection. */
|
|
export function bookingEntryIntent(text: string): 'booking' | 'combined' | null {
|
|
if (!BOOKING.test(text) || !RECORD.test(text) || NEGATED_BOOKING.test(text)) return null;
|
|
return PLACE_WORK.some((pattern) => pattern.test(text)) ? 'combined' : 'booking';
|
|
}
|
|
|
|
export const BOOKING_PLACE_REFUSAL =
|
|
'This request is to record a booking. Use create_reservation or create_transport from the bookings tools. Reuse an existing place or day assignment where one matches, or record the booking on its own.';
|
|
|
|
const CONFIRMED = [
|
|
/\b(?:ist|wurde|war)\s+(?:(?:bereits|schon)\s+)?(?:(?:vom|von|durch)\s+(?:[\p{L}\p{N}-]+\s+){1,6})?bestätigt\b/iu,
|
|
/\b(?:bereits|schon)\s+bestätigt\b/iu,
|
|
/\b(?:is|was|has\s+(?:already\s+)?been)\s+(?:already\s+)?confirmed\b|\balready confirmed\b/iu,
|
|
/\b(?:bestätigte[nr]?\s+(?:buchung|reservierung)|confirmed\s+(?:booking|reservation))\b/iu,
|
|
];
|
|
const UNCERTAIN_CONFIRMATION =
|
|
/\b(?:wenn|falls|sofern|if|whether|sobald|once|maybe|vielleicht|nicht|not|unconfirmed|unbestätigt|wird|will|should|soll)\b/iu;
|
|
const BOOKING_CODES =
|
|
/\b(?:buchungsnummer|buchungscode|bestätigungsnummer|(?:booking|reservation|confirmation)\s+(?:code|number|reference)|pnr)\s*[:#]?\s*(?=[\p{L}\d-]*\d)([\p{L}\d][\p{L}\d-]*)/giu;
|
|
const CONFIRMATION_WORD = /\b(?:bestätigt\p{L}*|unbestätigt|confirmed|unconfirmed)\b/iu;
|
|
|
|
export interface BookingConfirmation {
|
|
reference: string | null;
|
|
}
|
|
|
|
/** Only explicit confirmation from the traveler's own booking request supplies a default. */
|
|
export function bookingConfirmationFrom(text: string): BookingConfirmation | null {
|
|
if (!bookingEntryIntent(text)) return null;
|
|
const clauses = text.split(/(?<=[.!?;])\s+|\n/u).filter((clause) => CONFIRMATION_WORD.test(clause));
|
|
if (clauses.some((clause) => clause.endsWith('?') || UNCERTAIN_CONFIRMATION.test(clause))) return null;
|
|
if (!clauses.some((clause) => CONFIRMED.some((pattern) => pattern.test(clause)))) return null;
|
|
const references = new Set([...text.matchAll(BOOKING_CODES)].map((match) => match[1]));
|
|
if (references.size > 1 || /\b(?:buchungen|reservierungen|bookings|reservations)\b/iu.test(text)) return null;
|
|
return { reference: references.values().next().value ?? null };
|
|
}
|
|
|
|
export function createsBooking(tool: string): boolean {
|
|
return tool === 'create_reservation' || tool === 'create_transport';
|
|
}
|
|
|
|
export function savesBooking(tool: string): boolean {
|
|
return createsBooking(tool) || tool === 'update_reservation' || tool === 'update_transport';
|
|
}
|
|
|
|
const EDIT_BOOKING =
|
|
/\b(?:verknüpfe(?:n)?|ordne(?:n)?|zuordnen|aktualisiere(?:n)?|bearbeite(?:n)?|bestätige(?:n)?|korrigiere(?:n)?|link|update|edit|confirm|correct)\b/iu;
|
|
const EXTRA_REQUEST =
|
|
/\b(?:und|and|sowie|danach|anschließend|außerdem|zusätzlich|then|also|afterwards|separately)\b|[;\n]/iu;
|
|
|
|
/** End on a booking receipt only for a clear, single booking task. */
|
|
export function isStandaloneBookingRequest(text: string): boolean {
|
|
if (EXTRA_REQUEST.test(text) || NEGATED_BOOKING.test(text)) return false;
|
|
const entry = bookingEntryIntent(text);
|
|
if (entry !== null) return entry === 'booking' && !EDIT_BOOKING.test(text);
|
|
return BOOKING.test(text) && EDIT_BOOKING.test(text) && !PLACE_WORK.some((pattern) => pattern.test(text));
|
|
}
|
|
|
|
export function withBookingConfirmation(
|
|
tool: string,
|
|
args: Record<string, unknown>,
|
|
confirmation: BookingConfirmation | null | undefined,
|
|
onlyBooking: boolean,
|
|
): Record<string, unknown> {
|
|
if (!confirmation || !createsBooking(tool) || (args.status !== undefined && args.status !== 'pending')) return args;
|
|
const reference = typeof args.confirmation_number === 'string' ? args.confirmation_number.trim() : '';
|
|
const matches = confirmation.reference && reference ? confirmation.reference === reference : onlyBooking;
|
|
return matches ? { ...args, status: 'confirmed' } : args;
|
|
}
|