All checks were successful
Verify and publish V2 container / build-and-push (push) Successful in 1m2s
Providers and pricing - Add a DeepSeek provider: thinking mode with low/high/max effort, reasoning_content replayed between tool steps, cache-hit usage, /models discovery and catalog prices. - Fix "does not support tool calling": a rejected forced tool_choice (DeepSeek thinking mode) now asks for the call in the prompt; only a genuine no-tools 400 is "unsupported". - Seven reasoning-effort levels (Off to Max) mapped per adapter. - Cached-input price per model, cache-write and DeepSeek off-peak aware cost estimates. - Built-in bge-small embedding model (transformers.js) as the semantic search default. Quizzes - Numeric (type-the-answer) questions: units ignored, optional ± tolerance; editor, runner, importer, templates and generation prompts. - Quiz start screen instead of showing answers; navigator ring no longer clipped and key legend removed; clock only for timed exams; 7-point letter grades. Assistant and generation - Assistant turns run on the server independently of the browser, with re-attach and an explicit stop; drafts tray shows generations in progress. - Per-chat model switcher, @-tagging of class materials, full-width assistant page and a smaller assistant button. - Editable generation instructions and built-in presets, separate from the chatbot import instructions. Other - Dialog footer padding, keyboard hints removed, copy buttons work over plain HTTP, class and folder shares include documents. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
587 lines
27 KiB
TypeScript
587 lines
27 KiB
TypeScript
/**
|
||
* Anthropic — Messages API (`POST /v1/messages`, SSE).
|
||
*
|
||
* Verified against (2026-11):
|
||
* https://platform.claude.com/docs/en/api/messages
|
||
* https://platform.claude.com/docs/en/build-with-claude/streaming
|
||
* https://platform.claude.com/docs/en/build-with-claude/adaptive-thinking
|
||
* https://platform.claude.com/docs/en/build-with-claude/refusals-and-fallback
|
||
* https://platform.claude.com/docs/en/api/migration-2026-10-21
|
||
*
|
||
* Quirks handled here:
|
||
* - API version 2026-10-21 rejects temperature/top_p/top_k on EVERY model, so
|
||
* this adapter never sends a temperature (the catalog marks Claude models
|
||
* `temperature: false`).
|
||
* - `max_tokens` is required; thinking counts against it.
|
||
* - Thinking configuration differs per model generation (see `claudeProfile`).
|
||
* - Thinking blocks (with their signatures) must be replayed unchanged, so the
|
||
* raw content blocks of each turn are kept in `providerData`.
|
||
* - Newer models bind thinking blocks to the conversation prefix (system
|
||
* prompt, tool set, earlier messages). If the app changed any of those, the
|
||
* API answers 400 "Invalid `signature` in `thinking` block"; the documented
|
||
* recovery is to strip the thinking blocks and retry once.
|
||
* - Safety classifiers can end a turn with `stop_reason: "refusal"` (HTTP 200).
|
||
* For the models that support it we opt into server-side fallback, which
|
||
* re-runs a declined request on another Claude model inside the same call.
|
||
*/
|
||
import {
|
||
AIError,
|
||
type ChatMessage,
|
||
type ChatRequest,
|
||
type ContentPart,
|
||
type DiscoveredModel,
|
||
type ModelCapabilities,
|
||
type ProviderAdapter,
|
||
type ProviderConfig,
|
||
type StopReason,
|
||
type StreamEvent,
|
||
type TestResult,
|
||
} from '../types.ts';
|
||
import { lookupModelDefaults } from './catalog.ts';
|
||
import { isChatModelId, makeUsage, nearestEffort, num, parseArgsStrict, parseToolArguments, runTest, str, TEST_TIMEOUT_MS, withCatalogDefaults } from './common.ts';
|
||
import { ensureOk, errorFromProvider, httpRequest, isRecord, requestJson, sseJson, trimBaseUrl, type ErrorContext } from './http.ts';
|
||
|
||
type Json = Record<string, unknown>;
|
||
|
||
const LABEL = 'Anthropic';
|
||
const DEFAULT_BASE_URL = 'https://api.anthropic.com';
|
||
const API_VERSION = '2026-10-21';
|
||
/** Beta that enables the `fallbacks` request parameter. */
|
||
const FALLBACK_BETA = 'server-side-fallback-2026-07-01';
|
||
/** Streaming has no HTTP-timeout concern, so give the model room (thinking counts against this). */
|
||
const DEFAULT_MAX_TOKENS = 64_000;
|
||
|
||
/* ------------------------------------------------------------------ */
|
||
/* Per-model behaviour */
|
||
/* ------------------------------------------------------------------ */
|
||
|
||
export interface ClaudeProfile {
|
||
/**
|
||
* always – thinking cannot be turned off (Fable/Mythos, Opus 5.5+)
|
||
* default_on – adaptive thinking runs unless disabled (Opus 5, Sonnet 5+, Haiku 5.5)
|
||
* opt_in – adaptive thinking must be requested (Opus 4.6–4.8, Sonnet 4.6)
|
||
* budget – legacy `budget_tokens` thinking (Haiku 4.5, Opus/Sonnet 4.5 and older)
|
||
* none – no thinking support (Claude 3 and earlier)
|
||
*/
|
||
thinking: 'always' | 'default_on' | 'opt_in' | 'budget' | 'none';
|
||
/** How thinking is switched off, when the model allows it. */
|
||
off: 'disabled' | 'between_tools' | null;
|
||
/** Accepts `output_config.effort`. */
|
||
effort: boolean;
|
||
/** Accepts `thinking.display` (and defaults to hiding the reasoning text). */
|
||
display: boolean;
|
||
/** Accepts `tool_choice` of type `any` / `tool`. */
|
||
forcedToolChoice: boolean;
|
||
/** Supports the server-side refusal `fallbacks` parameter. */
|
||
fallbacks: boolean;
|
||
}
|
||
|
||
/** Thinking / tool-choice rules by model, from the model migration guides. */
|
||
export function claudeProfile(model: string): ClaudeProfile {
|
||
const m = /claude-(fable|mythos|opus|sonnet|haiku)-(\d+)(?:-(\d{1,2}))?(?!\d)/.exec(model);
|
||
if (!m) {
|
||
// "claude-3-5-sonnet-…" and older put the version first and never had adaptive thinking.
|
||
if (/claude-(?:[123]\b|instant)/.test(model)) {
|
||
return { thinking: 'none', off: null, effort: false, display: false, forcedToolChoice: true, fallbacks: false };
|
||
}
|
||
// Unknown naming scheme: assume it behaves like the newest generation.
|
||
return { thinking: 'default_on', off: null, effort: true, display: true, forcedToolChoice: false, fallbacks: false };
|
||
}
|
||
const family = m[1];
|
||
const v = Number(m[2]) * 10 + Number(m[3] ?? 0); // 4.6 -> 46, 5 -> 50, 5.5 -> 55
|
||
|
||
if (family === 'fable' || family === 'mythos') {
|
||
return {
|
||
thinking: 'always',
|
||
off: null,
|
||
effort: true,
|
||
display: true,
|
||
forcedToolChoice: v <= 50,
|
||
fallbacks: family === 'fable' && (v === 51 || v === 52),
|
||
};
|
||
}
|
||
if (family === 'opus') {
|
||
if (v >= 55) return { thinking: 'always', off: null, effort: true, display: true, forcedToolChoice: false, fallbacks: v === 55 };
|
||
if (v >= 50) return { thinking: 'default_on', off: 'disabled', effort: true, display: true, forcedToolChoice: true, fallbacks: v === 50 };
|
||
if (v >= 47) return { thinking: 'opt_in', off: 'disabled', effort: true, display: true, forcedToolChoice: true, fallbacks: false };
|
||
if (v === 46) return { thinking: 'opt_in', off: 'disabled', effort: true, display: false, forcedToolChoice: true, fallbacks: false };
|
||
return { thinking: 'budget', off: 'disabled', effort: v === 45, display: false, forcedToolChoice: true, fallbacks: false };
|
||
}
|
||
if (family === 'sonnet') {
|
||
// Sonnet 5.5 rejects {type:"disabled"}; its lowest setting is {type:"between_tools"}.
|
||
if (v >= 55) return { thinking: 'default_on', off: 'between_tools', effort: true, display: true, forcedToolChoice: false, fallbacks: v === 55 };
|
||
if (v >= 50) return { thinking: 'default_on', off: 'disabled', effort: true, display: true, forcedToolChoice: true, fallbacks: false };
|
||
if (v === 46) return { thinking: 'opt_in', off: 'disabled', effort: true, display: false, forcedToolChoice: true, fallbacks: false };
|
||
return { thinking: 'budget', off: 'disabled', effort: false, display: false, forcedToolChoice: true, fallbacks: false };
|
||
}
|
||
// haiku
|
||
if (v >= 55) return { thinking: 'default_on', off: 'disabled', effort: true, display: true, forcedToolChoice: true, fallbacks: false };
|
||
return { thinking: 'budget', off: 'disabled', effort: false, display: false, forcedToolChoice: true, fallbacks: false };
|
||
}
|
||
|
||
/** Thinking budgets for the legacy `budget_tokens` models. */
|
||
const LEGACY_BUDGET = { minimal: 1_024, low: 2_048, medium: 8_192, high: 16_384, xhigh: 24_576, max: 32_000 } as const;
|
||
|
||
/** `output_config.effort` levels. "xhigh" exists on the newest models only; a rejection steps it down. */
|
||
const CLAUDE_EFFORTS = ['low', 'medium', 'high', 'xhigh', 'max'] as const;
|
||
|
||
/* ------------------------------------------------------------------ */
|
||
/* Request building */
|
||
/* ------------------------------------------------------------------ */
|
||
|
||
interface BuildOptions {
|
||
/** Official endpoint: enables features a proxy may not understand. */
|
||
official: boolean;
|
||
stripThinking: boolean;
|
||
useFallbacks: boolean;
|
||
eagerToolInput: boolean;
|
||
promptCache: boolean;
|
||
/** Highest effort level the model accepted, after a rejection. */
|
||
effortCap?: (typeof CLAUDE_EFFORTS)[number];
|
||
}
|
||
|
||
/** Tool-call ids must match ^[a-zA-Z0-9_-]+$; ids minted by other providers may not. */
|
||
function safeId(id: string): string {
|
||
const cleaned = id.replace(/[^a-zA-Z0-9_-]/g, '_');
|
||
return cleaned || 'tool_call';
|
||
}
|
||
|
||
function userBlocks(parts: ContentPart[], caps: Partial<ModelCapabilities>): Json[] {
|
||
const results: Json[] = [];
|
||
const rest: Json[] = [];
|
||
for (const p of parts) {
|
||
switch (p.type) {
|
||
case 'tool_result': {
|
||
// All results of one assistant turn go into ONE user message, before any other content.
|
||
const block: Json = { type: 'tool_result', tool_use_id: safeId(p.toolCallId), content: p.output || '(no output)' };
|
||
if (p.isError) block.is_error = true;
|
||
results.push(block);
|
||
break;
|
||
}
|
||
case 'text':
|
||
if (p.text) rest.push({ type: 'text', text: p.text });
|
||
break;
|
||
case 'image':
|
||
if (caps.vision === false) rest.push({ type: 'text', text: '[An image was attached, but this model cannot read images.]' });
|
||
else rest.push({ type: 'image', source: { type: 'base64', media_type: p.mime, data: p.data } });
|
||
break;
|
||
case 'file':
|
||
if (p.mime === 'application/pdf' && caps.pdf !== false) {
|
||
const doc: Json = { type: 'document', source: { type: 'base64', media_type: 'application/pdf', data: p.data } };
|
||
if (p.filename) doc.title = p.filename;
|
||
rest.push(doc);
|
||
} else if (p.mime.startsWith('text/')) {
|
||
const doc: Json = { type: 'document', source: { type: 'text', media_type: 'text/plain', data: Buffer.from(p.data, 'base64').toString('utf8') } };
|
||
if (p.filename) doc.title = p.filename;
|
||
rest.push(doc);
|
||
} else {
|
||
rest.push({ type: 'text', text: `[The file "${p.filename ?? 'attachment'}" was attached, but this model cannot read it directly.]` });
|
||
}
|
||
break;
|
||
case 'tool_call':
|
||
break; // not valid in a user message
|
||
}
|
||
}
|
||
return [...results, ...rest];
|
||
}
|
||
|
||
function assistantBlocks(msg: ChatMessage, stripThinking: boolean): Json[] {
|
||
const pd = msg.providerData;
|
||
if (pd?.kind === 'anthropic' && isRecord(pd.data) && Array.isArray(pd.data.content)) {
|
||
// Same provider: replay the turn exactly as it was received, thinking blocks
|
||
// and signatures included.
|
||
const blocks = pd.data.content.filter(isRecord);
|
||
return stripThinking ? blocks.filter((b) => b.type !== 'thinking' && b.type !== 'redacted_thinking') : blocks;
|
||
}
|
||
// Another provider produced this turn: rebuild it from the neutral parts.
|
||
const blocks: Json[] = [];
|
||
for (const p of msg.content) {
|
||
if (p.type === 'text' && p.text) blocks.push({ type: 'text', text: p.text });
|
||
else if (p.type === 'tool_call') blocks.push({ type: 'tool_use', id: safeId(p.id), name: p.name, input: p.input });
|
||
}
|
||
return blocks;
|
||
}
|
||
|
||
function toWireMessages(messages: ChatMessage[], caps: Partial<ModelCapabilities>, stripThinking: boolean): Json[] {
|
||
const out: Json[] = [];
|
||
for (const msg of messages) {
|
||
const content = msg.role === 'user' ? userBlocks(msg.content, caps) : assistantBlocks(msg, stripThinking);
|
||
if (content.length === 0) continue; // the API rejects empty turns
|
||
out.push({ role: msg.role, content });
|
||
}
|
||
if (out[0]?.role === 'assistant') out.unshift({ role: 'user', content: [{ type: 'text', text: '(continuing the conversation)' }] });
|
||
return out;
|
||
}
|
||
|
||
function buildBody(req: ChatRequest, profile: ClaudeProfile, opts: BuildOptions): Json {
|
||
const caps = req.capabilities ?? {};
|
||
const defaults = lookupModelDefaults('anthropic', req.model);
|
||
const ceiling = defaults.maxOutput ?? Number.POSITIVE_INFINITY;
|
||
const maxTokens = Math.max(1, Math.min(req.maxOutputTokens ?? DEFAULT_MAX_TOKENS, ceiling));
|
||
|
||
const systemNotes: string[] = [];
|
||
const body: Json = { model: req.model, max_tokens: maxTokens, stream: true };
|
||
|
||
// --- tools -------------------------------------------------------
|
||
const tools = caps.tools === false ? [] : (req.tools ?? []);
|
||
let forcing = false;
|
||
if (tools.length > 0) {
|
||
body.tools = tools.map((t) => {
|
||
const tool: Json = { name: t.name, description: t.description, input_schema: { type: 'object', ...t.parameters } };
|
||
// Without this the API buffers each tool-input parameter and the stream goes
|
||
// silent for the whole time a large input (a generated quiz, say) is written.
|
||
// The price is that the input is no longer validated server-side, so it is
|
||
// parsed strictly below and truncated calls are dropped.
|
||
if (opts.eagerToolInput) tool.eager_input_streaming = true;
|
||
return tool;
|
||
});
|
||
const choice = req.toolChoice;
|
||
if (choice === 'none') {
|
||
body.tool_choice = { type: 'none' };
|
||
} else if (choice === 'required' || isRecord(choice)) {
|
||
const name = isRecord(choice) ? str(choice.name) : '';
|
||
if (profile.forcedToolChoice) {
|
||
body.tool_choice = name ? { type: 'tool', name } : { type: 'any' };
|
||
forcing = true;
|
||
} else {
|
||
// Fable 5.1+, Opus 5.5 and Sonnet 5.5 answer 400 to forced tool use; the
|
||
// documented replacement is `auto` plus an explicit instruction.
|
||
systemNotes.push(
|
||
name
|
||
? `For this turn you must call the \`${name}\` tool. Begin your response with that tool call.`
|
||
: 'For this turn you must call one of the available tools. Begin your response with the tool call.',
|
||
);
|
||
}
|
||
}
|
||
}
|
||
|
||
// --- thinking ----------------------------------------------------
|
||
if (caps.reasoning !== false && profile.thinking !== 'none') {
|
||
const effort = req.reasoningEffort;
|
||
const adaptive: Json = profile.display ? { type: 'adaptive', display: 'summarized' } : { type: 'adaptive' };
|
||
const outputConfig: Json = {};
|
||
if (effort === 'none') {
|
||
if (profile.thinking === 'always') {
|
||
// Cannot be disabled: the lowest effort is the closest thing to "off".
|
||
body.thinking = adaptive;
|
||
outputConfig.effort = 'low';
|
||
} else if (profile.thinking === 'default_on') {
|
||
body.thinking = { type: profile.off ?? 'disabled' }; // `between_tools` takes no other field
|
||
}
|
||
// opt_in / budget models do not think unless asked.
|
||
} else if (profile.thinking === 'always' || profile.thinking === 'default_on') {
|
||
// `display: "summarized"` because these models hide the reasoning text by
|
||
// default, which would look like a long pause in the UI.
|
||
body.thinking = adaptive;
|
||
if (effort) outputConfig.effort = opts.effortCap ? nearestEffort(effort, CLAUDE_EFFORTS.filter((e) => CLAUDE_EFFORTS.indexOf(e) <= CLAUDE_EFFORTS.indexOf(opts.effortCap!))) : nearestEffort(effort, CLAUDE_EFFORTS);
|
||
} else if (effort) {
|
||
// Forced tool use cannot be combined with thinking on the older generations.
|
||
if (profile.thinking === 'opt_in' && !forcing) body.thinking = adaptive;
|
||
if (profile.thinking === 'budget' && !forcing) {
|
||
const budget = Math.min(LEGACY_BUDGET[effort], maxTokens - 1_024); // must be < max_tokens, min 1024
|
||
if (budget >= 1_024) body.thinking = { type: 'enabled', budget_tokens: budget };
|
||
}
|
||
if (profile.effort) outputConfig.effort = opts.effortCap ? nearestEffort(effort, CLAUDE_EFFORTS.filter((e) => CLAUDE_EFFORTS.indexOf(e) <= CLAUDE_EFFORTS.indexOf(opts.effortCap!))) : nearestEffort(effort, CLAUDE_EFFORTS);
|
||
}
|
||
if (Object.keys(outputConfig).length > 0) body.output_config = outputConfig;
|
||
}
|
||
|
||
// --- system prompt -----------------------------------------------
|
||
// There is no schema-less JSON mode (structured outputs need a schema), and
|
||
// assistant prefill is rejected by current models, so JSON mode is an instruction.
|
||
if (req.jsonMode) systemNotes.push('Respond with a single valid JSON object and nothing else — no prose, no Markdown code fences.');
|
||
const system = [req.system?.trim(), ...systemNotes].filter(Boolean).join('\n\n');
|
||
if (system) body.system = system;
|
||
|
||
body.messages = toWireMessages(req.messages, caps, opts.stripThinking);
|
||
|
||
if (opts.promptCache) body.cache_control = { type: 'ephemeral' }; // automatic caching of the reusable prefix
|
||
if (opts.useFallbacks) body.fallbacks = 'default';
|
||
return body;
|
||
}
|
||
|
||
/* ------------------------------------------------------------------ */
|
||
/* Adapter */
|
||
/* ------------------------------------------------------------------ */
|
||
|
||
function rootUrl(cfg: ProviderConfig): string {
|
||
return trimBaseUrl(cfg.baseUrl || DEFAULT_BASE_URL).replace(/\/v1$/, '');
|
||
}
|
||
|
||
function isOfficial(cfg: ProviderConfig): boolean {
|
||
try {
|
||
return new URL(rootUrl(cfg)).hostname === 'api.anthropic.com';
|
||
} catch {
|
||
return false;
|
||
}
|
||
}
|
||
|
||
function errorContext(cfg: ProviderConfig): ErrorContext {
|
||
return { provider: LABEL, secrets: [cfg.apiKey, ...Object.values(cfg.headers ?? {})] };
|
||
}
|
||
|
||
function headers(cfg: ProviderConfig, betas: string[] = []): Record<string, string> {
|
||
if (!cfg.apiKey) throw new AIError('not_configured', 'Add an Anthropic API key in Settings to use Claude models.');
|
||
const h: Record<string, string> = { ...cfg.headers, 'x-api-key': cfg.apiKey, 'anthropic-version': API_VERSION };
|
||
const userBeta = Object.entries(cfg.headers ?? {}).find(([k]) => k.toLowerCase() === 'anthropic-beta')?.[1];
|
||
const all = [...(userBeta ? userBeta.split(',').map((s) => s.trim()) : []), ...betas].filter(Boolean);
|
||
for (const k of Object.keys(h)) if (k.toLowerCase() === 'anthropic-beta') delete h[k];
|
||
if (all.length > 0) h['anthropic-beta'] = [...new Set(all)].join(',');
|
||
return h;
|
||
}
|
||
|
||
function mapStopReason(reason: string): StopReason {
|
||
switch (reason) {
|
||
case 'end_turn':
|
||
case 'stop_sequence':
|
||
return 'end';
|
||
case 'tool_use':
|
||
return 'tool_use';
|
||
case 'max_tokens':
|
||
case 'model_context_window_exceeded':
|
||
return 'max_tokens';
|
||
case 'refusal':
|
||
return 'refusal';
|
||
default:
|
||
return 'other'; // pause_turn (server tools only) and anything new
|
||
}
|
||
}
|
||
|
||
async function* stream(cfg: ProviderConfig, req: ChatRequest): AsyncGenerator<StreamEvent> {
|
||
const ctx = errorContext(cfg);
|
||
const profile = claudeProfile(req.model);
|
||
const official = isOfficial(cfg);
|
||
const opts: BuildOptions = {
|
||
official,
|
||
stripThinking: false,
|
||
// The beta-gated extras are only sent to api.anthropic.com; gateways and
|
||
// proxies frequently reject fields they do not know.
|
||
useFallbacks: official && profile.fallbacks,
|
||
eagerToolInput: official,
|
||
promptCache: official,
|
||
};
|
||
|
||
// Open the stream, degrading once per recognised 400 (see file header).
|
||
let http;
|
||
for (let attempt = 0; ; attempt++) {
|
||
http = await httpRequest(`${rootUrl(cfg)}/v1/messages`, {
|
||
...ctx,
|
||
headers: headers(cfg, opts.useFallbacks ? [FALLBACK_BETA] : []),
|
||
body: buildBody(req, profile, opts),
|
||
signal: req.signal,
|
||
});
|
||
try {
|
||
await ensureOk(http);
|
||
break;
|
||
} catch (err) {
|
||
if (!(err instanceof AIError) || err.status !== 400 || attempt >= 3) throw err;
|
||
const detail = err.providerMessage ?? '';
|
||
if (!opts.stripThinking && /signature/i.test(detail) && /thinking/i.test(detail)) opts.stripThinking = true;
|
||
else if (opts.useFallbacks && /fallback|anthropic-beta/i.test(detail)) opts.useFallbacks = false;
|
||
else if (opts.eagerToolInput && /eager_input_streaming/i.test(detail)) opts.eagerToolInput = false;
|
||
else if (opts.promptCache && /cache_control/i.test(detail)) opts.promptCache = false;
|
||
else if (/effort/i.test(detail) && opts.effortCap !== 'medium' && req.reasoningEffort && req.reasoningEffort !== 'none') opts.effortCap = opts.effortCap === 'high' ? 'medium' : opts.effortCap === 'xhigh' ? 'high' : 'xhigh';
|
||
else throw err;
|
||
}
|
||
}
|
||
|
||
// Content blocks by index, accumulated exactly as the API defines them.
|
||
const blocks: Json[] = [];
|
||
const toolJson = new Map<number, string>();
|
||
let stopReason = '';
|
||
let sawStop = false;
|
||
let reasoningEmitted = false;
|
||
const usage = { input: 0, cacheRead: 0, cacheWrite: 0, output: 0 };
|
||
const readUsage = (u: unknown) => {
|
||
if (!isRecord(u)) return;
|
||
if (typeof u.input_tokens === 'number') usage.input = u.input_tokens;
|
||
if (typeof u.cache_read_input_tokens === 'number') usage.cacheRead = u.cache_read_input_tokens;
|
||
if (typeof u.cache_creation_input_tokens === 'number') usage.cacheWrite = u.cache_creation_input_tokens;
|
||
if (typeof u.output_tokens === 'number') usage.output = u.output_tokens;
|
||
};
|
||
|
||
for await (const { data } of sseJson(http)) {
|
||
if (!isRecord(data)) continue;
|
||
switch (data.type) {
|
||
case 'message_start':
|
||
if (isRecord(data.message)) readUsage(data.message.usage);
|
||
break;
|
||
case 'content_block_start': {
|
||
const index = num(data.index);
|
||
const block: Json = isRecord(data.content_block) ? { ...data.content_block } : { type: 'unknown' };
|
||
blocks[index] = block;
|
||
if (block.type === 'tool_use') toolJson.set(index, '');
|
||
if (block.type === 'text' && typeof block.text === 'string' && block.text) yield { type: 'text', delta: block.text };
|
||
if (block.type === 'thinking' && reasoningEmitted) yield { type: 'reasoning', delta: '\n\n' };
|
||
break;
|
||
}
|
||
case 'content_block_delta': {
|
||
const index = num(data.index);
|
||
const block = blocks[index];
|
||
const delta = data.delta;
|
||
if (!block || !isRecord(delta)) break;
|
||
if (delta.type === 'text_delta') {
|
||
const text = str(delta.text);
|
||
block.text = str(block.text) + text;
|
||
if (text) yield { type: 'text', delta: text };
|
||
} else if (delta.type === 'thinking_delta') {
|
||
const text = str(delta.thinking);
|
||
block.thinking = str(block.thinking) + text;
|
||
if (text) {
|
||
reasoningEmitted = true;
|
||
yield { type: 'reasoning', delta: text };
|
||
}
|
||
} else if (delta.type === 'signature_delta') {
|
||
block.signature = str(block.signature) + str(delta.signature);
|
||
} else if (delta.type === 'input_json_delta') {
|
||
toolJson.set(index, (toolJson.get(index) ?? '') + str(delta.partial_json));
|
||
} else if (delta.type === 'citations_delta') {
|
||
const list = Array.isArray(block.citations) ? block.citations : [];
|
||
list.push(delta.citation);
|
||
block.citations = list;
|
||
}
|
||
break;
|
||
}
|
||
case 'message_delta':
|
||
if (isRecord(data.delta) && typeof data.delta.stop_reason === 'string') stopReason = data.delta.stop_reason;
|
||
readUsage(data.usage);
|
||
break;
|
||
case 'message_stop':
|
||
sawStop = true;
|
||
break;
|
||
case 'error':
|
||
// e.g. overloaded_error delivered after the 200 response started
|
||
throw errorFromProvider(ctx, undefined, data);
|
||
default:
|
||
break; // ping, and event types added later
|
||
}
|
||
}
|
||
if (!sawStop && !stopReason) {
|
||
throw new AIError('network', 'The connection to Anthropic closed before the response finished.', { retryable: true });
|
||
}
|
||
|
||
// Tool calls are surfaced only now, at the end of the message, because the
|
||
// stop reason decides whether they are usable:
|
||
// - `refusal` can cut a tool_use off mid-input — never run that turn's tools;
|
||
// - `max_tokens` can truncate the last call — drop it rather than "repair" it;
|
||
// - after a mid-output fallback, tool_use blocks from before the last
|
||
// `fallback` block belong to the declined attempt and must not be echoed.
|
||
let boundary = -1;
|
||
blocks.forEach((b, i) => {
|
||
if (b?.type === 'fallback') boundary = i;
|
||
});
|
||
|
||
const content: ContentPart[] = [];
|
||
const replay: Json[] = [];
|
||
const calls: Array<{ id: string; name: string; input: Record<string, unknown> }> = [];
|
||
for (let i = 0; i < blocks.length; i++) {
|
||
const block = blocks[i];
|
||
if (!block) continue;
|
||
const beforeBoundary = i < boundary;
|
||
if (block.type === 'text') {
|
||
const text = str(block.text);
|
||
if (!text) continue; // empty text blocks are rejected when echoed back
|
||
content.push({ type: 'text', text });
|
||
replay.push(block);
|
||
} else if (block.type === 'tool_use') {
|
||
if (beforeBoundary || stopReason === 'refusal') continue;
|
||
const raw = toolJson.get(i) ?? '';
|
||
const name = str(block.name);
|
||
let input = parseArgsStrict(raw);
|
||
if (!input) {
|
||
if (stopReason === 'max_tokens') continue; // truncated mid-input
|
||
input = parseToolArguments(raw, LABEL, name);
|
||
}
|
||
const call = { id: str(block.id), name, input };
|
||
calls.push(call);
|
||
content.push({ type: 'tool_call', ...call });
|
||
replay.push({ ...block, input });
|
||
} else if (block.type === 'fallback') {
|
||
replay.push(block); // must stay exactly where it appeared
|
||
} else if (!beforeBoundary) {
|
||
replay.push(block); // thinking, redacted_thinking, …: echoed back unchanged
|
||
}
|
||
}
|
||
|
||
for (const call of calls) yield { type: 'tool_call', call };
|
||
|
||
let mapped = mapStopReason(stopReason);
|
||
if (mapped === 'tool_use' && calls.length === 0) mapped = 'other';
|
||
if (mapped === 'end' && calls.length > 0) mapped = 'tool_use';
|
||
|
||
const message: ChatMessage = { role: 'assistant', content };
|
||
if (replay.length > 0) message.providerData = { kind: 'anthropic', data: { content: replay } };
|
||
yield {
|
||
type: 'done',
|
||
message,
|
||
stopReason: mapped,
|
||
// Anthropic reports uncached, cache-read and cache-write input separately.
|
||
usage: makeUsage({
|
||
input: usage.input + usage.cacheRead + usage.cacheWrite,
|
||
output: usage.output,
|
||
cacheRead: usage.cacheRead,
|
||
cacheWrite: usage.cacheWrite,
|
||
}),
|
||
};
|
||
}
|
||
|
||
function supported(caps: unknown, key: string): boolean | undefined {
|
||
if (!isRecord(caps) || !isRecord(caps[key])) return undefined;
|
||
const v = (caps[key] as Json).supported;
|
||
return typeof v === 'boolean' ? v : undefined;
|
||
}
|
||
|
||
async function listModels(cfg: ProviderConfig, signal?: AbortSignal, timeoutMs?: number): Promise<DiscoveredModel[]> {
|
||
const ctx = errorContext(cfg);
|
||
const found: DiscoveredModel[] = [];
|
||
let afterId = '';
|
||
// https://platform.claude.com/docs/en/api/models/list — cursor pagination via has_more / last_id.
|
||
for (let page = 0; page < 20; page++) {
|
||
const url = `${rootUrl(cfg)}/v1/models?limit=1000${afterId ? `&after_id=${encodeURIComponent(afterId)}` : ''}`;
|
||
const res = await requestJson<Json>(url, { ...ctx, headers: headers(cfg), signal, firstByteTimeoutMs: timeoutMs, totalTimeoutMs: timeoutMs });
|
||
const data = Array.isArray(res.data) ? res.data.filter(isRecord) : [];
|
||
for (const m of data) {
|
||
const id = str(m.id);
|
||
if (!id || !isChatModelId(id)) continue;
|
||
const model: DiscoveredModel = {
|
||
id,
|
||
// The API version in use rejects sampling parameters on every model.
|
||
capabilities: { tools: true, temperature: false },
|
||
};
|
||
if (typeof m.display_name === 'string') model.label = m.display_name;
|
||
if (typeof m.max_input_tokens === 'number') model.contextWindow = m.max_input_tokens;
|
||
if (typeof m.max_tokens === 'number') model.maxOutput = m.max_tokens;
|
||
const vision = supported(m.capabilities, 'image_input');
|
||
const pdf = supported(m.capabilities, 'pdf_input');
|
||
const thinking = supported(m.capabilities, 'thinking');
|
||
if (vision !== undefined) model.capabilities = { ...model.capabilities, vision };
|
||
if (pdf !== undefined) model.capabilities = { ...model.capabilities, pdf };
|
||
if (thinking !== undefined) model.capabilities = { ...model.capabilities, reasoning: thinking };
|
||
found.push(model);
|
||
}
|
||
if (res.has_more !== true || typeof res.last_id !== 'string' || !res.last_id) break;
|
||
afterId = res.last_id;
|
||
}
|
||
return withCatalogDefaults('anthropic', found);
|
||
}
|
||
|
||
async function test(cfg: ProviderConfig): Promise<TestResult> {
|
||
// Listing models is free and proves both connectivity and the key.
|
||
return runTest(LABEL, async () => ({ modelCount: (await listModels(cfg, undefined, TEST_TIMEOUT_MS)).length }));
|
||
}
|
||
|
||
export const anthropicAdapter: ProviderAdapter = {
|
||
kind: 'anthropic',
|
||
meta: {
|
||
label: LABEL,
|
||
defaultBaseUrl: DEFAULT_BASE_URL,
|
||
requiresApiKey: true,
|
||
requiresBaseUrl: false,
|
||
supportsDiscovery: true,
|
||
docsUrl: 'https://platform.claude.com/settings/keys',
|
||
keyHint: 'sk-ant-…',
|
||
},
|
||
stream,
|
||
listModels: (cfg, signal) => listModels(cfg, signal),
|
||
test,
|
||
};
|