// Live mode: the real Claude, called straight from this browser with the // visitor's own API key. The key stays in memory for this page only. It is // never saved and never sent anywhere except api.anthropic.com. let sdk; // loaded on first use, so Watch mode never downloads it export async function makeClaude({ apiKey, model, signal, onParams }) { sdk ??= await import("@anthropic-ai/sdk"); const Anthropic = sdk.default; const client = new Anthropic({ apiKey, dangerouslyAllowBrowser: true }); return async function callModel({ system, tools, messages }) { const params = { model, max_tokens: 16000, system, tools, messages, // Adaptive thinking, with a readable summary so the page can show it. thinking: { type: "adaptive", display: "summarized" }, // A small task: low effort keeps each turn quick and cheap. output_config: { effort: "low" }, // Cache the growing prefix, so each turn re-reads history at the cached rate. cache_control: { type: "ephemeral" }, }; // If a safety classifier declines, retry on Anthropic's recommended // fallback model. Haiku has no server-side fallback, so it skips this. if (model !== "claude-haiku-5-5") { params.betas = ["server-side-fallback-2026-07-01"]; params.fallbacks = "default"; } onParams?.(params); // lets the page show the exact request body return client.beta.messages.create(params, { signal }); }; } // Turn SDK errors into one plain sentence for the page. export function describeError(err) { if (!sdk || !(err instanceof sdk.default.APIError)) return err?.message || String(err); const A = sdk.default; if (err instanceof A.AuthenticationError) return "That API key was not accepted. Check it and try again."; if (err instanceof A.PermissionDeniedError) return "This key doesn't have access to that model."; if (err instanceof A.NotFoundError) return "That model isn't available to this key."; if (err instanceof A.RateLimitError) return "Rate limited, even after retries. Wait a minute and run it again."; if (err instanceof A.APIConnectionError) return "Couldn't reach the API. Check your connection."; if (err instanceof A.BadRequestError) return `The API rejected the request: ${err.message}`; return `API error ${err.status ?? ""}: ${err.message}`; }