The floating chat window's header badge flipped meaning — a live per-turn token counter while streaming, the persisted context size at rest — so it "reset to 1" on each prompt and conflated two different numbers. Replace it with a stable "current / max" context badge (e.g. `572 / 200k`). The live "Thinking · N tokens" inside the chat body stays; only the duplicate live counter is removed from the header. Max comes from a new admin setting "Context window (tokens)". The server resolves it and attaches `maxContextTokens` to the completed assistant turn's metadata (next to contextTokens), so the badge needs no client-side model resolution and this survives public shares / per-role models. Server: - ai.types: chatContextWindow on AiProviderSettings + PROVIDER_SETTINGS_KEYS + ResolvedAiConfig + MaskedAiSettings. - workspace.repo: chatContextWindow in AI_PROVIDER_SETTINGS_ALLOWED (parity). - update-ai-settings.dto: @IsInt @Min(0) chatContextWindow. - ai-settings.service: coerce the ::text-stored value to a positive int in resolve()/getMasked(). - ai-chat.service: flushAssistant writes metadata.maxContextTokens (>0); the completed turn passes resolved.chatContextWindow. Client: - ai-chat.types: maxContextTokens on the message-row metadata. - ai-chat-window: read maxContextTokens; render "current [/ max]"; drop the liveTurnTokens state/branch and the onLiveTurnTokens prop; new tooltip. - chat-thread: remove the live-turn-token throttle effect and plumbing. - count-stream-tokens: drop the now-dead liveTurnTokens()/types; keep estimateTokens. - settings: chatContextWindow on IAiSettings(+Update) + a NumberInput in the AI provider settings form. i18n: add the badge/settings keys (en, ru); remove the two now-unused keys. Tests: flushAssistant maxContextTokens, DTO validation, trim token tests. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
118 lines
4.2 KiB
TypeScript
118 lines
4.2 KiB
TypeScript
import api from "@/lib/api-client";
|
|
|
|
// Supported LLM providers/drivers.
|
|
export type AiDriver = "openai" | "gemini" | "ollama";
|
|
|
|
// How STT (speech-to-text) requests are encoded for the transcription endpoint.
|
|
// - 'multipart' -> OpenAI-compatible multipart/form-data (OpenAI, speaches,
|
|
// faster-whisper-server)
|
|
// - 'json' -> JSON body with base64-encoded audio (OpenRouter)
|
|
export type SttApiStyle = "multipart" | "json";
|
|
|
|
// Chat provider implementation for the `openai` driver (chosen explicitly):
|
|
// - 'openai-compatible' -> maps streamed reasoning_content to reasoning parts
|
|
// (z.ai/GLM, DeepSeek, OpenRouter, ...). Default.
|
|
// - 'openai' -> official provider; real-OpenAI reasoning-model shaping.
|
|
export type ChatApiStyle = "openai-compatible" | "openai";
|
|
|
|
// Masked AI provider settings returned by the server.
|
|
// No API key is ever returned; only `hasApiKey` / `hasEmbeddingApiKey` indicate
|
|
// whether one is stored. `embeddingBaseUrl` is the RAW stored value (empty means
|
|
// "uses the chat base URL").
|
|
export interface IAiSettings {
|
|
driver?: AiDriver;
|
|
chatModel?: string;
|
|
// Max context window in tokens shown in the chat header badge; 0/unset = no limit.
|
|
chatContextWindow?: number;
|
|
chatApiStyle?: ChatApiStyle;
|
|
// Cheap model id for the anonymous public-share assistant; empty = chatModel.
|
|
publicShareChatModel?: string;
|
|
// Agent-role id whose persona the public-share assistant adopts; empty =
|
|
// built-in locked persona.
|
|
publicShareAssistantRoleId?: string;
|
|
embeddingModel?: string;
|
|
baseUrl?: string;
|
|
embeddingBaseUrl?: string;
|
|
systemPrompt?: string;
|
|
hasApiKey: boolean;
|
|
hasEmbeddingApiKey: boolean;
|
|
// STT-specific settings. `sttBaseUrl` is the RAW stored value (empty means
|
|
// "uses the chat base URL"). `hasSttApiKey` indicates whether an STT-specific
|
|
// key is stored (empty means "uses the chat API key").
|
|
sttModel?: string;
|
|
sttBaseUrl?: string;
|
|
sttApiStyle?: SttApiStyle;
|
|
// ISO-639-1 dictation language; empty = auto-detect.
|
|
sttLanguage?: string;
|
|
hasSttApiKey: boolean;
|
|
// RAG indexing coverage (pages indexed for semantic search).
|
|
indexedPages: number;
|
|
totalPages: number;
|
|
}
|
|
|
|
// Update payload. Key semantics (same for `apiKey` and `embeddingApiKey`):
|
|
// - omit the key -> key unchanged
|
|
// - `key: ''` -> clear the stored key
|
|
// - `key: 'non-empty'` -> set the key
|
|
// Non-secret fields are saved as given.
|
|
export interface IAiSettingsUpdate {
|
|
driver?: AiDriver;
|
|
chatModel?: string;
|
|
// Max context window in tokens for the chat header badge; 0 = clear the limit.
|
|
chatContextWindow?: number;
|
|
chatApiStyle?: ChatApiStyle;
|
|
publicShareChatModel?: string;
|
|
// Agent-role id whose persona the public-share assistant adopts; empty =
|
|
// built-in locked persona.
|
|
publicShareAssistantRoleId?: string;
|
|
embeddingModel?: string;
|
|
baseUrl?: string;
|
|
embeddingBaseUrl?: string;
|
|
systemPrompt?: string;
|
|
apiKey?: string;
|
|
embeddingApiKey?: string;
|
|
sttModel?: string;
|
|
sttBaseUrl?: string;
|
|
sttApiStyle?: SttApiStyle;
|
|
// ISO-639-1 dictation language; empty = auto-detect.
|
|
sttLanguage?: string;
|
|
// Write-only STT key (same semantics as `apiKey` / `embeddingApiKey`).
|
|
sttApiKey?: string;
|
|
}
|
|
|
|
// Result of a connection test against the configured provider.
|
|
// The error string is already sanitized server-side.
|
|
export interface IAiTestResult {
|
|
ok: boolean;
|
|
error?: string;
|
|
}
|
|
|
|
// Which endpoint a connection test probes.
|
|
export type AiTestCapability = "chat" | "embeddings" | "stt";
|
|
|
|
export async function getAiSettings(): Promise<IAiSettings> {
|
|
const req = await api.post<IAiSettings>("/workspace/ai-settings");
|
|
return req.data;
|
|
}
|
|
|
|
export async function updateAiSettings(
|
|
data: IAiSettingsUpdate,
|
|
): Promise<IAiSettings> {
|
|
const req = await api.post<IAiSettings>("/workspace/ai-settings/update", data);
|
|
return req.data;
|
|
}
|
|
|
|
export async function testAiConnection(
|
|
capability: AiTestCapability,
|
|
): Promise<IAiTestResult> {
|
|
const req = await api.post<IAiTestResult>("/workspace/ai-settings/test", {
|
|
capability,
|
|
});
|
|
return req.data;
|
|
}
|
|
|
|
export async function reindexAiEmbeddings(): Promise<IAiSettings> {
|
|
const req = await api.post<IAiSettings>("/workspace/ai-settings/reindex");
|
|
return req.data;
|
|
}
|