mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-28 07:30:04 +00:00
feat(assistant): trip assistant chat with your own AI connection
Adds an in-app assistant that reads and plans the open trip. Users connect their own provider (Sign in with ChatGPT, or an Anthropic, OpenAI, Gemini or OpenRouter key) or use the one the admin set up, including a custom OpenAI-compatible server. It is an addon and only shows while a connection is usable. The chat runs on the existing MCP tools, pinned to the trip. Every change is held as a change card the user applies or discards, and applied changes can be undone. On the desktop it is a floating window that can be moved and resized; on the phone it is fullscreen. Search results show as place cards that can go onto a day, weather lookups as a strip, proposed places as ghost markers on the map with a before/after switch, and an answer can be shared into the trip chat. Booking files can be dropped in and are read into change cards.
This commit is contained in:
@@ -21,7 +21,7 @@ import {
|
||||
} from './PlaceFormModal.helpers'
|
||||
import { useNewCategory, usePlaceForm } from './usePlaceForm'
|
||||
import { usePlaceSearch } from './usePlaceSearch'
|
||||
import { guessCategoryId } from './placeCategoryGuess'
|
||||
import { guessCategoryId } from '@trek/shared'
|
||||
import { getNavigationTargets, openNavigationTarget } from './placeNavigation'
|
||||
import { NavigationMenu } from '../shared/NavigationMenu'
|
||||
import { offersGoogleRetry } from '../../utils/placeSource'
|
||||
|
||||
@@ -29,6 +29,7 @@ import { useToast } from '../shared/Toast'
|
||||
import { useTranslation } from '../../i18n'
|
||||
import { usePluginStore } from '../../store/pluginStore'
|
||||
import PluginFrame from '../Plugins/PluginFrame'
|
||||
import AssistantAskButton from '../Assistant/AssistantAskButton'
|
||||
import type { Place, Category, Day, Reservation, TripFile, AssignmentsMap, DistanceUnit } from '../../types'
|
||||
import type { CollectionStatus } from '@trek/shared'
|
||||
import { splitReservationDateTime, formatTime, formatMoney } from '../../utils/formatters'
|
||||
@@ -218,10 +219,9 @@ export default function PlaceInspector({
|
||||
leftWidth = 0, rightWidth = 0, clearMapPill = false,
|
||||
collectionStatus, onCopyToTrip, onSetStatus, onRemoveFromList, roadtripEndDay, roadtripStay, roadtripActive,
|
||||
}: PlaceInspectorProps) {
|
||||
// Editing the place is a place right. The planner hands the handlers over
|
||||
// regardless, and a member without the right saw Edit, Delete and the inline
|
||||
// rename and got the server's 403 for each (#2446). Collection mode gates
|
||||
// its own actions.
|
||||
// Editing the place is a place right. The planner hands the handlers over regardless, and a member
|
||||
// without the right saw Edit, Delete and the inline rename and got the server's 403 for each (#2446).
|
||||
// Collection mode gates its own actions.
|
||||
const can = useCanDo()
|
||||
const trip = useTripStore(s => s.trip)
|
||||
const mayEditPlace = mode !== 'trip' || can('place_edit', trip)
|
||||
@@ -686,10 +686,9 @@ function InspectorHead({ place, category, openNow, phone, email, rating, ratingC
|
||||
{price}
|
||||
</span>
|
||||
)}
|
||||
{/* Where the place came from, when it did not come from somebody
|
||||
typing it: a stay accepted out of their own recordings. The mark
|
||||
alone — the name beside it is already the place's name, and a
|
||||
word here would only repeat the tooltip. */}
|
||||
{/* Where the place came from, when it did not come from somebody typing it: a stay
|
||||
accepted out of their own recordings. The mark alone — the name beside it is
|
||||
already the place's name, and a word here would only repeat the tooltip. */}
|
||||
{place.source === 'dawarich' && (
|
||||
<Tooltip label={t('dawarich.place.fromDawarich')} placement="top">
|
||||
<span role="img" aria-label={t('dawarich.place.fromDawarich')} className={`${HEAD_PILL} px-1 py-[3px]`}>
|
||||
@@ -718,6 +717,7 @@ function InspectorHead({ place, category, openNow, phone, email, rating, ratingC
|
||||
)}
|
||||
</div>
|
||||
|
||||
<AssistantAskButton place={place} />
|
||||
<Tooltip label={t('common.close')}>
|
||||
<button type="button" onClick={onClose} aria-label={t('common.close')}
|
||||
className="grid h-8 w-8 flex-none place-items-center rounded-full bg-surface-card text-content-muted shadow-sm transition-colors hover:text-content">
|
||||
|
||||
@@ -1,53 +0,0 @@
|
||||
// FE-PLANNER-CATGUESS-001 to FE-PLANNER-CATGUESS-006
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { guessCategoryId, kindOfResult } from './placeCategoryGuess'
|
||||
|
||||
const seeded = [
|
||||
{ id: 1, name: 'Hotel', icon: '🏨' },
|
||||
{ id: 2, name: 'Restaurant', icon: '🍽️' },
|
||||
{ id: 3, name: 'Attraction', icon: '🏛️' },
|
||||
{ id: 4, name: 'Shopping', icon: '🛍️' },
|
||||
{ id: 5, name: 'Transport', icon: '🚌' },
|
||||
{ id: 6, name: 'Activity', icon: '🎯' },
|
||||
{ id: 7, name: 'Bar/Cafe', icon: '☕' },
|
||||
{ id: 8, name: 'Beach', icon: '🏖️' },
|
||||
{ id: 9, name: 'Nature', icon: '🌿' },
|
||||
{ id: 10, name: 'Other', icon: '📍' },
|
||||
]
|
||||
|
||||
describe('guessCategoryId', () => {
|
||||
it('FE-PLANNER-CATGUESS-001: an OSM hotel lands in the Hotel category', () => {
|
||||
expect(guessCategoryId({ category: 'hotel' }, seeded)).toBe(1)
|
||||
expect(guessCategoryId({ category: 'guest_house' }, seeded)).toBe(1)
|
||||
})
|
||||
|
||||
it('FE-PLANNER-CATGUESS-002: Google types are read in order, so a restaurant bar is still a bar', () => {
|
||||
expect(guessCategoryId({ types: ['restaurant', 'food', 'point_of_interest'] }, seeded)).toBe(2)
|
||||
expect(guessCategoryId({ types: ['bar', 'restaurant'] }, seeded)).toBe(7)
|
||||
expect(guessCategoryId({ types: ['museum', 'tourist_attraction'] }, seeded)).toBe(3)
|
||||
})
|
||||
|
||||
it('FE-PLANNER-CATGUESS-003: a shop and a station find their categories', () => {
|
||||
expect(guessCategoryId({ category: 'shop_bakery' }, seeded)).toBe(7)
|
||||
expect(guessCategoryId({ category: 'shop_clothes' }, seeded)).toBe(4)
|
||||
expect(guessCategoryId({ types: ['train_station', 'transit_station'] }, seeded)).toBe(5)
|
||||
})
|
||||
|
||||
it('FE-PLANNER-CATGUESS-004: renamed categories are still found, by a local name or their icon', () => {
|
||||
expect(guessCategoryId({ category: 'hotel' }, [{ id: 42, name: 'Unterkunft', icon: 'X' }])).toBe(42)
|
||||
expect(guessCategoryId({ category: 'hotel' }, [{ id: 43, name: 'Schlafen', icon: 'BedDouble' }])).toBe(43)
|
||||
})
|
||||
|
||||
it('FE-PLANNER-CATGUESS-005: nothing to go on, or nothing that fits, leaves the field empty', () => {
|
||||
expect(guessCategoryId({}, seeded)).toBeNull()
|
||||
expect(guessCategoryId({ types: ['point_of_interest'] }, seeded)).toBeNull()
|
||||
expect(guessCategoryId({ category: 'hotel' }, [{ id: 1, name: 'Food', icon: '🍴' }])).toBeNull()
|
||||
})
|
||||
|
||||
it('FE-PLANNER-CATGUESS-006: the kind is read from any source vocabulary', () => {
|
||||
expect(kindOfResult({ category: 'italian_restaurant' })).toBe('food')
|
||||
expect(kindOfResult({ category: 'beach' })).toBe('beach')
|
||||
expect(kindOfResult({ types: ['park'] })).toBe('nature')
|
||||
expect(kindOfResult({ category: 'swimming_pool' })).toBe('activity')
|
||||
})
|
||||
})
|
||||
@@ -1,72 +0,0 @@
|
||||
import type { Category } from '../../types'
|
||||
|
||||
type Kind = 'lodging' | 'food' | 'drinks' | 'sight' | 'shopping' | 'transport' | 'beach' | 'nature' | 'activity'
|
||||
|
||||
/**
|
||||
* What a search result says it is, read from whichever vocabulary its source speaks:
|
||||
* Google `types`, the OpenStreetMap tag value, or the TREK Places (Overture) category.
|
||||
* Checked in order, first hit wins, so the specific kinds sit before the broad ones
|
||||
* ("bar" before "restaurant" would misfile a "restaurant_bar" otherwise).
|
||||
*/
|
||||
const KIND_TERMS: [Kind, string[]][] = [
|
||||
['lodging', ['hotel', 'hostel', 'motel', 'guest_house', 'lodging', 'resort', 'camp_site', 'campground', 'campsite', 'chalet', 'bed_and_breakfast', 'alpine_hut', 'apartment', 'caravan_site']],
|
||||
['beach', ['beach']],
|
||||
['drinks', ['cafe', 'coffee', 'bar', 'pub', 'biergarten', 'night_club', 'nightclub', 'bakery', 'ice_cream', 'wine_bar', 'brewery', 'tea_house']],
|
||||
['food', ['restaurant', 'fast_food', 'food', 'meal_', 'bistro', 'diner', 'pizzeria', 'steakhouse']],
|
||||
['transport', ['station', 'airport', 'aerodrome', 'ferry_terminal', 'bus_stop', 'terminal', 'car_rental', 'parking']],
|
||||
['shopping', ['shop', 'store', 'mall', 'supermarket', 'market', 'department_store', 'boutique']],
|
||||
['sight', ['museum', 'attraction', 'viewpoint', 'monument', 'memorial', 'castle', 'artwork', 'gallery', 'church', 'cathedral', 'place_of_worship', 'historic', 'ruins', 'landmark', 'zoo', 'aquarium', 'palace', 'tower']],
|
||||
['nature', ['park', 'nature_reserve', 'national_park', 'peak', 'waterfall', 'forest', 'garden', 'natural_feature', 'lake', 'hiking']],
|
||||
['activity', ['amusement_park', 'theme_park', 'stadium', 'sports', 'spa', 'gym', 'cinema', 'theatre', 'theater', 'bowling', 'swimming', 'golf', 'climbing', 'ski']],
|
||||
]
|
||||
|
||||
/** How a trip's category of that kind is recognised: words in its name, then its icon. */
|
||||
const KIND_CATEGORY: Record<Kind, { names: string[]; icons: string[] }> = {
|
||||
lodging: { names: ['hotel', 'accommodation', 'lodging', 'unterkunft', 'hébergement', 'alojamiento', 'alloggio', 'stay', 'hostel', 'camping'], icons: ['🏨', '🛏️', '🏠', 'BedDouble', 'Home', 'Tent'] },
|
||||
food: { names: ['restaurant', 'food', 'essen', 'ristorante', 'restaurante', 'eat'], icons: ['🍽️', '🍴', 'UtensilsCrossed', 'Utensils'] },
|
||||
drinks: { names: ['cafe', 'café', 'bar', 'coffee', 'kaffee', 'pub', 'drinks'], icons: ['☕', '🍺', '🍸', 'Coffee', 'Beer', 'Wine'] },
|
||||
sight: { names: ['attraction', 'sight', 'sehenswürdigkeit', 'museum', 'attraktion', 'culture', 'kultur'], icons: ['🏛️', '📸', 'Landmark', 'Library', 'Camera', 'Church'] },
|
||||
shopping: { names: ['shopping', 'shop', 'einkauf', 'store', 'markt'], icons: ['🛍️', 'ShoppingBag', 'Store'] },
|
||||
transport: { names: ['transport', 'verkehr', 'transit', 'station'], icons: ['🚌', '🚆', '✈️', 'Bus', 'Train', 'Plane'] },
|
||||
beach: { names: ['beach', 'strand', 'plage', 'playa', 'spiaggia'], icons: ['🏖️', 'Waves'] },
|
||||
nature: { names: ['nature', 'natur', 'park', 'outdoor', 'hiking', 'wandern'], icons: ['🌿', '🌲', '⛰️', 'TreePine', 'Leaf', 'Mountain'] },
|
||||
activity: { names: ['activity', 'aktivität', 'activité', 'actividad', 'attività', 'sport'], icons: ['🎯', 'Activity', 'Dumbbell', 'Ticket'] },
|
||||
}
|
||||
|
||||
/** The words a result carries about itself, lower-cased, whatever the source. */
|
||||
function resultTerms(result: Record<string, unknown>): string[] {
|
||||
const terms: string[] = []
|
||||
if (Array.isArray(result.types)) for (const t of result.types) if (typeof t === 'string') terms.push(t.toLowerCase())
|
||||
if (typeof result.category === 'string') terms.push(result.category.toLowerCase())
|
||||
return terms
|
||||
}
|
||||
|
||||
export function kindOfResult(result: Record<string, unknown>): Kind | null {
|
||||
const terms = resultTerms(result)
|
||||
if (terms.length === 0) return null
|
||||
for (const [kind, words] of KIND_TERMS) {
|
||||
if (terms.some(term => words.some(word => term.includes(word)))) return kind
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
* The trip category a picked search result most likely belongs in (#2282), or null.
|
||||
*
|
||||
* Categories are the admin's own, freely named and freely iconed, so there is no key to
|
||||
* look up; they are recognised by their name, then by their icon. When nothing matches
|
||||
* the answer is null and the field stays empty, because a wrong category that looks
|
||||
* deliberate is worse than none.
|
||||
*/
|
||||
export function guessCategoryId(result: Record<string, unknown>, categories: Pick<Category, 'id' | 'name' | 'icon'>[]): number | null {
|
||||
const kind = kindOfResult(result)
|
||||
if (!kind) return null
|
||||
const { names, icons } = KIND_CATEGORY[kind]
|
||||
const byName = categories.find(c => {
|
||||
const name = (c.name || '').toLowerCase()
|
||||
return names.some(word => name.includes(word))
|
||||
})
|
||||
if (byName) return byName.id
|
||||
const byIcon = categories.find(c => c.icon != null && icons.includes(c.icon))
|
||||
return byIcon ? byIcon.id : null
|
||||
}
|
||||
Reference in New Issue
Block a user