Transcribe through OpenRouter when OPENROUTER_API_KEY is set

A Claude subscription can't be used by the app, so let it use any cheap
vision model on OpenRouter instead (default google/gemini-3.1-flash-lite,
OPENROUTER_MODEL to change). The Anthropic API stays available as the
second choice. Both providers share the prompt and live behind one
{ name, model, transcribe } interface in server/providers.mjs; OpenRouter
retries 408/429/5xx and network errors and turns 401/402/404 into
messages the editor can show.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HA82gVRDXpoukzgMFumoJt
This commit is contained in:
agent 2026-11-04 15:38:24 +00:00
parent 46aa1236ed
commit f353c14619
11 changed files with 258 additions and 91 deletions

View file

@ -1,50 +1,4 @@
// Turn page photos into play markup with a Claude vision model, one request
// per page, then stitch the pages together.
import Anthropic from "@anthropic-ai/sdk";
import { SYSTEM_PROMPT, USER_PROMPT } from "./prompt.mjs";
export const DEFAULT_MODEL = "claude-haiku-5-5";
export class PageError extends Error {}
// Transcribe one JPEG. `client` is an Anthropic client (injected so tests can stub it).
export async function transcribePage(client, model, jpeg) {
const response = await client.messages.create({
model,
max_tokens: 16000,
// Copying text off a page needs little reasoning; low effort keeps it cheap.
output_config: { effort: "low" },
system: [{ type: "text", text: SYSTEM_PROMPT, cache_control: { type: "ephemeral" } }],
messages: [{
role: "user",
content: [
{ type: "image", source: { type: "base64", media_type: "image/jpeg", data: jpeg.toString("base64") } },
{ type: "text", text: USER_PROMPT },
],
}],
});
if (response.stop_reason === "refusal") throw new PageError("model declined to transcribe this page");
if (response.stop_reason === "max_tokens") throw new PageError("transcription was cut off (page too long)");
const text = response.content.filter((b) => b.type === "text").map((b) => b.text).join("").trim();
if (!text) throw new PageError("model returned no text");
return { text, usage: response.usage };
}
export function makeClient(apiKey) {
return new Anthropic({ apiKey, maxRetries: 4 });
}
// A short, human-readable reason for a failed page (shown in the editor).
export function describeError(err) {
if (err instanceof PageError) return err.message;
if (err instanceof Anthropic.AuthenticationError) return "API key rejected (check ANTHROPIC_API_KEY)";
if (err instanceof Anthropic.PermissionDeniedError) return "API key not allowed to use this model";
if (err instanceof Anthropic.NotFoundError) return "model not found (check CLAUDE_MODEL)";
if (err instanceof Anthropic.RateLimitError) return "rate limited by the API, try again later";
if (err instanceof Anthropic.BadRequestError) return `API rejected the request: ${err.message}`;
if (err instanceof Anthropic.APIError) return `API error ${err.status ?? ""}: ${err.message}`.trim();
return String(err?.message || err);
}
// Stitch per-page transcriptions (see providers.mjs) into one play file.
// Split model output into { num, body }: the first "=N" line is the page number.
export function splitPage(text) {