Files
TREK/server/src/nest/ai-connections/ai-chat-client.interface.ts
T
Maurice d07b8ba56e feat(assistant): web search through the provider, and cards with a way back for the smaller tour writes
- Web search: ChatGPT (Responses web_search), Anthropic (web_search server
  tool) and OpenRouter (web plugin) search the web themselves when the admin
  switch allows it (on as it ships). A search shows as a step ("Searching the
  web for ..."), the cited pages as source chips under the answer. Anthropic
  answers that searched are replayed with their blocks (new provider state
  adapter); the search step never goes back to the model as a call. The
  prompt says when to search and that chosen places still go through
  search_place.
- Tour writes: assign_tour_track (undo: take that assignment off),
  unassign_tour_track, set_tour_activity_excluded (undo: the other switch),
  link_tour_packing and unlink_tour_packing (undo from the link before, which
  both answers now name) get change cards and their ways back.
2026-10-23 19:13:51 +02:00

92 lines
3.6 KiB
TypeScript

import type { AiProviderState } from '../../db/json-columns';
import type { AiConnectionKind } from '@trek/shared';
export type { AiProviderState };
export { AiProviderError, isImageRefusal } from './providers/provider-errors';
/** A tool the model may call: its name, what it is for and its arguments as JSON Schema. */
export interface AiToolSpec {
name: string;
description: string;
parameters: Record<string, unknown>;
}
/** A call the model made, with the id the provider gave it and its parsed arguments. */
export interface AiToolCall {
id: string;
name: string;
args: Record<string, unknown>;
}
/**
* An image a message carries to a model that reads images (the Studio page
* the assistant looks at), base64 without line breaks. It lives for one turn
* in memory; nothing stores it.
*/
export interface AiImage {
mimeType: 'image/jpeg' | 'image/png' | 'image/webp';
data: string;
detail?: 'low' | 'high';
}
/**
* The provider-neutral history a turn sends. A tool result answers the call
* with the same id; an assistant message may carry the items its provider
* needs replayed verbatim (`providerState`), which an adapter for another
* provider ignores and rebuilds from the text and the calls instead. A user
* message or a tool result may carry images; each adapter places them where
* its provider reads them, and the assistant sends none to a model that reads
* no images.
*/
export type AiChatMessage =
| { role: 'user'; text: string; images?: AiImage[] }
| { role: 'assistant'; text: string; toolCalls: AiToolCall[]; providerState?: AiProviderState | null }
| { role: 'tool'; toolCallId: string; name: string; output: string; isError?: boolean; images?: AiImage[] };
export interface AiChatRequest {
/** The standing instructions, the same on every turn of a conversation (cached where the provider can). */
system: string;
/** What changes from turn to turn (the trip card), sent after the instructions. */
context?: string;
messages: AiChatMessage[];
tools: AiToolSpec[];
/** Keeps a conversation on the provider's prompt cache (the thread id). */
cacheKey?: string;
/** Lets the model search the web with its provider's own search; a client without one ignores it. */
webSearch?: boolean;
}
/** A page the model's answer draws on, from its web search. */
export interface AiSource {
url: string;
title: string;
}
export type AiFinishReason = 'stop' | 'tool-calls' | 'length';
/**
* What a streamed turn yields, in order: text as it arrives, each tool call
* once complete, then the token usage, the provider's items to replay (only
* where it needs them) and the finish. A refusal or a broken stream is thrown
* as an AiProviderError, never yielded.
*/
export type AiChatEvent =
| { type: 'text-delta'; delta: string }
| { type: 'tool-call'; call: AiToolCall }
/** `cachedInputTokens` is the part of `inputTokens` the provider read from its prompt cache. */
| { type: 'usage'; inputTokens: number; cachedInputTokens: number; outputTokens: number }
| { type: 'provider-state'; state: AiProviderState }
/** The provider started a web search of its own; `query` where it says what it looks for. */
| { type: 'web-search'; query: string | null }
/** A page the answer cites, each once. */
| { type: 'source'; source: AiSource }
| { type: 'finish'; reason: AiFinishReason };
export interface AiChatClient {
readonly kind: AiConnectionKind;
readonly model: string;
/** Whether the provider can search the web itself when a request asks it to. */
readonly webSearch: boolean;
stream(request: AiChatRequest, signal: AbortSignal): AsyncIterable<AiChatEvent>;
}