Files
accounted/lib/ai/index.ts
T
3edbf0a2e3 fix(agent): chat console keeps its thread across turns and reloads (#1859)
Three user-reported failures in the assistant panel, one root cause each:

1. "The chat asks what I'm referring to" when continuing a thread. The
   single-call console (general.help, AskConsole -> /api/agent/ask) was
   stateless since the 08-20 model-agnostic cutover: conversationId was only
   the tool actor id, so every turn was answered blind, reload or not.
   The provider-agnostic GenerateTextRequest gains an optional `history`
   (real message turns before the prompt, in both the Anthropic-family and
   the OpenAI-compatible adapter; absent/empty leaves the request
   byte-identical to the single-turn call). The route loads the thread's
   earlier turns server-side (loadChatHistory: text only, hidden and tool
   rows dropped, alternation repaired, newest 16 rows / 10k chars) before
   writing the new question, and hands them to the model.

2. A full page reload (the deploy prompt's "Ladda om") closed the docked
   panel and dropped the thread from view. The panel now remembers its open
   thread per tab in sessionStorage (lib/agent-panel/session-restore) and
   the provider reopens it on mount; the sheet loads it exactly like a pick
   from "Tidigare konversationer". Close and "Ny konversation" forget it; a
   thread that no longer opens is dropped instead of retried on every reload.

3. "Can't type any more" once the update banner shows. DeployReloadPrompt's
   full-width wrapper sits at z-[60] after the panel in DOM order and
   swallowed clicks on the panel's composer; only the card takes input now.


Claude-Session: https://claude.ai/code/session_01VjoXN3xdNZrHZeYA6qMi3g

Co-authored-by: Jakob Wennberg <311770904+jakobwennberg-oss@users.noreply.github.com>
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-08-24 16:37:30 +02:00

69 lines
2.1 KiB
TypeScript

import { getAiStatus, readAiConfig, type ResolvedAiConfig } from './config'
import { createAnthropicFamilyService } from './services/anthropic-family'
import { createOpenAICompatibleService } from './services/openai-compatible'
import type { AiService } from './types'
export type {
AiCapabilities,
AiChatTurn,
AiDocumentInput,
AiImageMediaType,
AiPdfMode,
AiProviderKind,
AiService,
AiToolDef,
AiStatus,
AiTier,
AiUsage,
ExtractFromDocumentRequest,
ExtractFromDocumentResult,
ExtractionSkipReason,
GenerateStructuredRequest,
GenerateStructuredResult,
GenerateTextRequest,
GenerateTextResult,
} from './types'
export { getAiStatus, readAiConfig, resolveTierModel } from './config'
export { extractJsonObject } from './json'
// One service per resolved configuration. Keyed on the non-secret parts of
// the config plus credential presence, so a changed environment (tests, the
// smoke script) gets a fresh service while a long-lived process reuses one.
let cached: { key: string; service: AiService } | null = null
function cacheKey(cfg: ResolvedAiConfig): string {
return JSON.stringify({
provider: cfg.provider,
configured: cfg.configured,
baseUrl: cfg.baseUrl,
hasKey: !!cfg.apiKey,
models: cfg.models,
vision: cfg.vision,
strictJson: cfg.strictJson,
pdfMode: cfg.pdfMode,
pdfMaxPages: cfg.pdfMaxPages,
})
}
/**
* The AI service for this deployment. Never throws on construction: an
* unconfigured deployment still gets a service whose extractFromDocument
* answers `skipped: ai_unconfigured`, so upload paths degrade quietly.
*/
export function getAiService(): AiService {
const cfg = readAiConfig()
const key = cacheKey(cfg)
if (cached && cached.key === key) return cached.service
const service =
cfg.provider === 'openai-compatible'
? createOpenAICompatibleService(cfg)
: createAnthropicFamilyService(cfg)
cached = { key, service }
return service
}
/** Tests only: drop the cached service so the next call re-reads the environment. */
export function resetAiServiceForTests(): void {
cached = null
}