SPB Git

Repository Public

ai-providers.ts 9.6 KB 168 lines typescript

1// SP Mail Next — fournisseurs de la couche d'intelligence : « local » (passerelle compatible OpenAI du cluster) et « anthropic » (SDK officiel).2// Une seule interface : complete({ system, messages, json?, maxTokens, signal }). Aucun outil n'est jamais déclaré au modèle ;3// la sortie est du texte, ou du JSON validé par schéma (une seconde tentative si la première est invalide). Rien n'est journalisé ici.4import Anthropic from "@anthropic-ai/sdk"5import { zodOutputFormat } from "@anthropic-ai/sdk/helpers/zod"6import type { z } from "zod"7import { config } from "../config.js"8import { aiConfigured } from "../templates-ai.js"9import { bad, HttpError } from "../util.js"1011export type ProviderName = "local" | "anthropic"12export type ChatMessage = { role: "user" | "assistant"; content: string }13export type CompleteInput<T = unknown> = { system: string; messages: ChatMessage[]; json?: z.ZodType<T>; maxTokens: number; signal?: AbortSignal }14export type AiUsage = { input_tokens: number; output_tokens: number }15export type CompleteResult<T = unknown> = { text: string; json?: T; provider: ProviderName; model: string; usage: AiUsage; attempts: number }16export interface AiProvider {17  name: ProviderName18  model: string19  complete<T = unknown>(input: CompleteInput<T>): Promise<CompleteResult<T>>20}2122/** Fournisseurs réellement configurés sur ce serveur. */23export const available = (): Record<ProviderName, boolean> => ({ local: !!config.aiLocalUrl, anthropic: aiConfigured() })2425// ------------------------------------------------------------------ sortie : nettoyage, JSON, plafonds26const MAX_BODY = 512 * 102427/** Retire le raisonnement apparent de certains modèles locaux (<think>…</think>) et les espaces de bord. */28export const cleanText = (s: string) => s.replace(/<think>[\s\S]*?<\/think>/gi, "").replace(/^[\s\S]*?<\/think>/i, "").trim()2930/** Extrait l'objet JSON d'une réponse de modèle : tolère les clôtures ``` et le texte autour, jamais autre chose qu'un objet. */31export function parseJsonOutput(text: string): unknown {32  const t = cleanText(text).replace(/^```(?:json)?\s*/i, "").replace(/\s*```\s*$/i, "")33  const a = t.indexOf("{"), b = t.lastIndexOf("}")34  if (a < 0 || b <= a) throw new SyntaxError("no JSON object in the answer")35  return JSON.parse(t.slice(a, b + 1))36}3738type Raw = { text: string; model: string; usage: AiUsage; truncated: boolean }39const RETRY_NOTE = "\n\n(Note: your previous answer was not a valid JSON object of the requested shape. Answer again with the JSON object only.)"4041/** Texte plafonné, ou JSON validé par schéma avec une seule relance si la sortie est invalide. */42async function finish<T>(name: ProviderName, i: CompleteInput<T>, call: (messages: ChatMessage[]) => Promise<Raw>): Promise<CompleteResult<T>> {43  const usage: AiUsage = { input_tokens: 0, output_tokens: 0 }44  const cap = i.maxTokens * 845  let messages = i.messages46  for (let attempt = 1; attempt <= 2; attempt++) {47    const r = await call(messages)48    usage.input_tokens += r.usage.input_tokens49    usage.output_tokens += r.usage.output_tokens50    const text = cleanText(r.text).slice(0, cap)51    if (!i.json) {52      if (r.truncated) throw bad("ai_output_truncated", 502, "The model's answer was cut off")53      if (!text) throw bad("ai_invalid_output", 502, "The model returned an empty answer")54      return { text, provider: name, model: r.model, usage, attempts: attempt }55    }56    let parsed: unknown57    try { parsed = parseJsonOutput(text) } catch { parsed = undefined }58    const v = parsed === undefined ? null : i.json.safeParse(parsed)59    if (v?.success) return { text, json: v.data, provider: name, model: r.model, usage, attempts: attempt }60    if (attempt === 2) throw new AiInvalidOutput(r.model, usage)61    const last = i.messages[i.messages.length - 1]62    messages = [...i.messages.slice(0, -1), { role: last.role, content: last.content + RETRY_NOTE }]63  }64  throw bad("ai_invalid_output", 502)65}66/** Sortie invalide après relance : porte le modèle et l'usage pour le journal. */67export class AiInvalidOutput extends HttpError {68  constructor(public model: string, public usage: AiUsage) { super(502, "ai_invalid_output", "The model's answer did not match the expected structure") }69}7071const signalOf = (s?: AbortSignal) => (s ? AbortSignal.any([s, AbortSignal.timeout(config.aiTimeoutMs)]) : AbortSignal.timeout(config.aiTimeoutMs))72const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms))73async function readCapped(r: Response): Promise<string> {74  const buf = Buffer.from(await r.arrayBuffer())75  if (buf.length > MAX_BODY) throw bad("ai_invalid_output", 502, "The model's answer is too large")76  return buf.toString("utf8")77}7879// ------------------------------------------------------------------ local : /v1/chat/completions compatible OpenAI80/** Requête envoyée au fournisseur local : jamais de champ `tools`, `functions` ni `tool_choice`. */81export function localRequestBody(model: string, i: Pick<CompleteInput, "system" | "maxTokens">, messages: ChatMessage[]) {82  return { model, messages: [{ role: "system", content: i.system }, ...messages], max_tokens: i.maxTokens, temperature: 0.2, stream: false }83}84async function localCall(i: CompleteInput, messages: ChatMessage[]): Promise<Raw> {85  const body = JSON.stringify(localRequestBody(config.aiLocalModel, i, messages))86  let lastStatus = 087  for (let attempt = 1; attempt <= 2; attempt++) {88    let r: Response89    try {90      r = await fetch(`${config.aiLocalUrl}/chat/completions`, {91        method: "POST", headers: { "content-type": "application/json", ...(config.aiLocalKey ? { authorization: `Bearer ${config.aiLocalKey}` } : {}) }, body, signal: signalOf(i.signal),92      })93    } catch (e) {94      if (i.signal?.aborted) throw bad("ai_cancelled", 499, "Request cancelled")95      if ((e as Error).name === "TimeoutError") throw bad("ai_timeout", 504, "The local AI service did not answer in time")96      if (attempt === 2) throw bad("ai_unreachable", 502, "The local AI service is unreachable")97      await sleep(400)98      continue99    }100    if (r.ok) {101      const j = JSON.parse(await readCapped(r)) as { model?: string; choices?: { message?: { content?: unknown }; finish_reason?: string }[]; usage?: { prompt_tokens?: number; completion_tokens?: number } }102      const c = j.choices?.[0]103      return {104        text: typeof c?.message?.content === "string" ? c.message.content : "", model: String(j.model ?? config.aiLocalModel).slice(0, 120), truncated: c?.finish_reason === "length",105        usage: { input_tokens: Number(j.usage?.prompt_tokens ?? 0) || 0, output_tokens: Number(j.usage?.completion_tokens ?? 0) || 0 },106      }107    }108    lastStatus = r.status109    await r.arrayBuffer().catch(() => undefined)110    if (attempt === 1 && (r.status === 429 || r.status >= 500)) {111      await sleep(Math.min(2000, (Number(r.headers.get("retry-after")) || 0.4) * 1000))112      continue113    }114    break115  }116  if (lastStatus === 401 || lastStatus === 403) throw bad("ai_not_configured", 503, "The local AI service rejected the credential")117  if (lastStatus === 429) throw bad("ai_rate_limited", 429, "The local AI service is rate limited; retry shortly")118  throw bad("ai_error", 502, `Local AI service error ${lastStatus}`)119}120const local: AiProvider = {121  name: "local",122  get model() { return config.aiLocalModel },123  complete: (i) => finish("local", i, (m) => localCall(i, m)),124}125126// ------------------------------------------------------------------ anthropic : SDK officiel, sortie structurée quand un schéma est demandé127let client: Anthropic | undefined128const sdk = () => (client ??= new Anthropic())129async function anthropicCall(i: CompleteInput, messages: ChatMessage[]): Promise<Raw> {130  try {131    const msg = await sdk().messages.create(132      {133        model: config.aiAnthropicModel,134        // La réflexion adaptative compte dans max_tokens : on lui laisse de la marge au-dessus du plafond de sortie voulu.135        max_tokens: i.maxTokens + 2048,136        output_config: { effort: "low", ...(i.json ? { format: zodOutputFormat(i.json as never) } : {}) },137        system: i.system,138        messages,139      },140      { signal: i.signal, timeout: config.aiTimeoutMs, maxRetries: 1 },141    )142    if (msg.stop_reason === "refusal") throw bad("ai_refused", 422, "The model declined this request")143    return {144      text: msg.content.flatMap((c) => (c.type === "text" ? [c.text] : [])).join(""), model: msg.model, truncated: msg.stop_reason === "max_tokens",145      usage: { input_tokens: msg.usage.input_tokens, output_tokens: msg.usage.output_tokens },146    }147  } catch (e) {148    if (e instanceof HttpError) throw e149    if (e instanceof Anthropic.APIUserAbortError) throw bad("ai_cancelled", 499, "Request cancelled")150    if (e instanceof Anthropic.AuthenticationError) throw bad("ai_not_configured", 503, "The Anthropic credential was rejected")151    if (e instanceof Anthropic.RateLimitError) throw bad("ai_rate_limited", 429, "The AI service is rate limited; retry shortly")152    if (e instanceof Anthropic.BadRequestError) throw bad("ai_bad_request", 502, "The AI service refused the request")153    if (e instanceof Anthropic.APIConnectionTimeoutError) throw bad("ai_timeout", 504, "The AI service did not answer in time")154    if (e instanceof Anthropic.APIConnectionError) throw bad("ai_unreachable", 502, "The AI service is unreachable")155    if (e instanceof Anthropic.APIError) throw bad("ai_error", 502, `AI service error ${e.status ?? ""}`.trim())156    throw e157  }158}159const anthropic: AiProvider = {160  name: "anthropic",161  get model() { return config.aiAnthropicModel },162  complete: (i) => finish("anthropic", i, (m) => anthropicCall(i, m)),163}164165export function providerOf(name: ProviderName): AiProvider {166  return name === "local" ? local : anthropic167}168
Files main
Menu

Theme