mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-23 13:23: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:
@@ -0,0 +1,44 @@
|
||||
import { type AppendMessage, useExternalStoreRuntime } from '@assistant-ui/react';
|
||||
import type { AssistantMessage } from '@trek/shared';
|
||||
import { useMemo } from 'react';
|
||||
|
||||
import { useNetworkMode } from '../../hooks/useNetworkMode';
|
||||
import { pendingToolCallIds } from '../../store/assistantMessages';
|
||||
import { chatOf, useAssistantStore } from '../../store/assistantStore';
|
||||
import { toThreadMessage } from './assistantModel';
|
||||
|
||||
function textOf(message: AppendMessage): string {
|
||||
const texts: string[] = [];
|
||||
for (const part of message.content) if (part.type === 'text') texts.push(part.text);
|
||||
return texts.join('\n');
|
||||
}
|
||||
|
||||
/**
|
||||
* The one place the chat runtime is built. The assistant store stays the
|
||||
* source of truth: the runtime reads its messages through `toThreadMessage`
|
||||
* and hands sending and stopping back to it. There is no editing, branching
|
||||
* or reloading, so the thread stays the line the server keeps.
|
||||
*
|
||||
* Offline the composer is off; while a change waits for a decision, typing
|
||||
* works but sending waits, because the model is still waiting for that answer.
|
||||
*/
|
||||
export function useAssistantRuntime(tripId: number) {
|
||||
const messages = useAssistantStore((s) => chatOf(s, tripId).messages);
|
||||
const isRunning = useAssistantStore((s) => chatOf(s, tripId).isRunning);
|
||||
const isLoading = useAssistantStore((s) => chatOf(s, tripId).loading);
|
||||
const send = useAssistantStore((s) => s.send);
|
||||
const cancel = useAssistantStore((s) => s.cancel);
|
||||
const { offline } = useNetworkMode();
|
||||
const waiting = useMemo(() => pendingToolCallIds(messages).length > 0, [messages]);
|
||||
|
||||
return useExternalStoreRuntime<AssistantMessage>({
|
||||
messages,
|
||||
convertMessage: toThreadMessage,
|
||||
isRunning,
|
||||
isLoading,
|
||||
isDisabled: offline,
|
||||
isSendDisabled: waiting,
|
||||
onNew: (message) => send(tripId, textOf(message)),
|
||||
onCancel: async () => cancel(tripId),
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user