/** * OAuth-bearer LLM calls for admin-side classifiers. * * Every admin-side classifier (memory-classify, memory-rank, commitment, * inbound-gateway, query classifier, summarisation, email * screening) uses Claude Code OAuth credentials — never the Anthropic * API key. The API-key path is reserved for the public agent. * * Mechanism: read the access token from ~/.claude/.credentials.json, * refresh it via the standard refresh-token grant if expired, then call * api.anthropic.com with `Authorization: Bearer ` and the * `anthropic-beta: oauth-2025-04-20` header. Billed against the operator's * Claude Code subscription, not the API-key key. Refresh uses a * module-level mutex so concurrent classifier calls during expiry don't * race and invalidate each other's tokens (Anthropic rotates both tokens * on refresh). * * Implementation note: this lib uses raw fetch() so the maxy-code tree * carries no Anthropic SDK dependency at all. The Messages API surface * we use (model + system + one user message + max_tokens) is small and * stable. * * Failure modes are surfaced as a structured `Result` so callers can * pattern-match without parsing error strings. The skill / agent layer * is responsible for translating `fallback` results into operator-visible * blocker messages (loud failure doctrine). */ declare const OAUTH_BETA_HEADER = "oauth-2025-04-20"; /** Anthropic tool definition — structural-output enforcement via function calling. */ export interface OauthLlmTool { name: string; description: string; input_schema: Record; } export interface CallOauthLlmParams { /** Anthropic model id (e.g. claude-haiku-4-5). */ model: string; /** System prompt — the classifier instructions. */ system: string; /** User message — the input to classify / rank / etc. */ userMessage: string; /** Max output tokens. Default 4096. */ maxTokens?: number; /** * Hard timeout in ms for the model call. Default 60_000. * * 60s fits short admin classifiers (commitment, query, * inbound-gateway, screening, summary — all <2K input, <500 output). * Long-output callers MUST override per-call: `memory-classify` runs at * 8K maxTokens against documents up to 20K chars and has been observed * at 56s p99 on Haiku 4.5, so it passes `timeoutMs: 180_000`. * Do not raise this default — short classifiers rely on the 60s safety * net to surface transient stalls instead of disguising them as latency. */ timeoutMs?: number; /** * Optional tools for structured output. When provided with a forced * `toolChoiceName`, the model returns a `tool_use` block whose `input` is * a structured object matching the tool's `input_schema` — strictly typed * JSON without the parsing brittleness of free-form text output. */ tools?: OauthLlmTool[]; /** Force the model to call this tool. Required when `tools` is provided. */ toolChoiceName?: string; } /** Result when the call did not declare any tool — only text or fallback. * `stopReason` and `outputTokens` are exposed so callers can * distinguish a `max_tokens`-truncated response (which can produce a * one-sided fence) from a clean `end_turn`. */ export type CallOauthLlmTextResult = { kind: "ok"; text: string; stopReason?: string; outputTokens?: number; } | CallOauthLlmFallback; /** Result when the call forced a tool — only the tool input or fallback. */ export type CallOauthLlmToolResult = { kind: "ok-tool"; toolName: string; input: Record; stopReason?: string; outputTokens?: number; } | CallOauthLlmFallback; /** Discriminated union of every possible result. Returned by the no-overload form. */ export type CallOauthLlmResult = CallOauthLlmTextResult | CallOauthLlmToolResult; export interface CallOauthLlmFallback { kind: "fallback"; /** Human-readable single-line reason (for operator-visible blocker). */ reason: string; /** Stable classifier — callers can pattern-match for retry/abort policy. */ cause: "missing-creds" | "dead-token" | "refresh-failed" | "auth-error" | "rate-limit" | "server-error" | "network-error" | "timeout" | "empty-response" | "malformed-response" | "input-too-large" | "unexpected"; } /** * Call an Anthropic model via OAuth bearer auth. * * Returns: * { kind: "ok", text } — model's text response * { kind: "ok-tool", toolName, input } — when `tools` + `toolChoiceName` provided * { kind: "fallback", reason, cause } — caller decides whether to abort * * The caller is responsible for translating fallback results into * operator-visible blocker messages — this wrapper never silently * substitutes a degraded response. * * Overloads narrow the return type by call shape: text-only callers (no * `tools`) statically rule out `ok-tool`; tool callers statically rule out * `ok`. Mixed callers get the full union. */ export declare function callOauthLlm(params: Omit): Promise; export declare function callOauthLlm(params: CallOauthLlmParams & { tools: OauthLlmTool[]; toolChoiceName: string; }): Promise; export declare function callOauthLlm(params: CallOauthLlmParams): Promise; export { OAUTH_BETA_HEADER }; //# sourceMappingURL=index.d.ts.map