mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-14 00:01:03 +00:00
b64da26651
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.
83 lines
3.1 KiB
TypeScript
83 lines
3.1 KiB
TypeScript
import { type TriggerAdapter, type TriggerItem, useAui } from '@assistant-ui/react';
|
|
import { ASSISTANT_MENTION_KINDS } from '@trek/shared';
|
|
import { useMemo } from 'react';
|
|
|
|
import { useTranslation } from '../../i18n';
|
|
import { type AssistantMentionItem, useAssistantStore } from '../../store/assistantStore';
|
|
import { useTripStore } from '../../store/tripStore';
|
|
import { MENTION_KINDS, mentionGroups, mentionOf, searchMentions, withMention } from './assistantMentionModel';
|
|
|
|
/**
|
|
* Writes the composer's text and keeps the draft in step: a text set from
|
|
* code fires no change event, so the store would otherwise keep the old one.
|
|
*/
|
|
function useComposerWriter(tripId: number) {
|
|
const aui = useAui();
|
|
const setDraft = useAssistantStore((s) => s.setDraft);
|
|
const read = () => aui.composer.getState().text;
|
|
return {
|
|
read,
|
|
/** Takes into the draft what the popover wrote into the text itself. */
|
|
keep: () => setDraft(tripId, read()),
|
|
write: (text: string) => {
|
|
aui.composer.setText(text);
|
|
setDraft(tripId, text);
|
|
},
|
|
};
|
|
}
|
|
|
|
/**
|
|
* `@` in the composer: the trip's days, places and bookings to pick from
|
|
* (nothing for a trip the planner is not showing, whose lists are not at
|
|
* hand). The text is the truth: a pick stays as `@label` in it, and a
|
|
* mention whose `@label` was deleted is not sent.
|
|
*/
|
|
export function useAssistantMentions(tripId: number) {
|
|
const { t, locale } = useTranslation();
|
|
const here = useTripStore((s) => s.trip?.id === tripId);
|
|
const days = useTripStore((s) => s.days);
|
|
const places = useTripStore((s) => s.places);
|
|
const reservations = useTripStore((s) => s.reservations);
|
|
const addMention = useAssistantStore((s) => s.addMention);
|
|
const composer = useComposerWriter(tripId);
|
|
|
|
const adapter = useMemo((): TriggerAdapter | undefined => {
|
|
if (!here) return undefined;
|
|
const groups = mentionGroups({ days, places, reservations }, t, locale);
|
|
const kinds = ASSISTANT_MENTION_KINDS.filter((k) => groups[k].length > 0);
|
|
const categories = kinds.map((k) => ({ id: k, label: t(MENTION_KINDS[k].labelKey) }));
|
|
return {
|
|
categories: () => categories,
|
|
categoryItems: (id) => {
|
|
const kind = ASSISTANT_MENTION_KINDS.find((k) => k === id);
|
|
return kind ? groups[kind] : [];
|
|
},
|
|
search: (query) => searchMentions(groups, query),
|
|
};
|
|
}, [here, days, places, reservations, t, locale]);
|
|
|
|
return {
|
|
adapter,
|
|
/** After the popover wrote `@label` into the text. */
|
|
onInserted: (item: TriggerItem) => {
|
|
const mention = mentionOf(item);
|
|
if (mention) addMention(tripId, mention);
|
|
composer.keep();
|
|
},
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Names something in the question from outside the text, such as a place
|
|
* dropped on the chat: `@label` goes at the end of what is typed, the way a
|
|
* pick from the popover leaves it, once.
|
|
*/
|
|
export function useInsertMention(tripId: number) {
|
|
const addMention = useAssistantStore((s) => s.addMention);
|
|
const composer = useComposerWriter(tripId);
|
|
return (mention: AssistantMentionItem) => {
|
|
addMention(tripId, mention);
|
|
composer.write(withMention(composer.read(), mention.label));
|
|
};
|
|
}
|