UNPKG

simple-ai-provider

Version:

A simple and extensible AI provider package for easy integration of multiple AI services

75 lines (74 loc) 3 kB
/** * Claude Code Provider * * Wraps `@anthropic-ai/claude-agent-sdk` so consumers can use a local * Claude Code installation — including Claude Pro/Max subscription * accounts authenticated via `claude login` — through the same * BaseAIProvider interface as the other providers. * * Tradeoffs vs the direct Anthropic API provider: * - Authenticates via the local CLI, so subscription users (no API key) * can use it. * - Requires `claude` to be installed and logged in on the host. * - Higher latency (shells out to a CLI process per request). * - Designed for agent workflows, but used here in single-turn mode * (maxTurns: 1, no tools) for plain text completion. * * @see https://docs.anthropic.com/claude/docs/claude-code-sdk */ import type { AIProviderConfig, CompletionChunk, CompletionParams, CompletionResponse, ProviderInfo } from '../types/index.js'; import { BaseAIProvider } from './base.js'; import { AIProviderError } from '../types/index.js'; /** * Configuration for the Claude Code provider. `apiKey` is optional — * when omitted the SDK falls back to ANTHROPIC_API_KEY in the * environment or the local subscription credentials managed by the * `claude` CLI. */ export interface ClaudeCodeConfig extends Omit<AIProviderConfig, 'apiKey'> { apiKey?: string; /** * Default model. Accepts SDK aliases (`'sonnet'`, `'opus'`, `'haiku'`, * `'inherit'`) or any full Claude model ID. * @default 'sonnet' */ defaultModel?: string; /** * Working directory for the Claude Code session. * @default process.cwd() */ cwd?: string; /** * Tool names the model is allowed to call. Defaults to none, which * makes the provider behave as a pure text completion endpoint. */ allowedTools?: string[]; /** * Maximum agent turns. Defaults to 1 for plain completion; raise this * if you also pass `allowedTools` and want tool-use loops. * @default 1 */ maxTurns?: number; } export declare class ClaudeCodeProvider extends BaseAIProvider { private readonly defaultModel; private readonly cwd; private readonly allowedTools; private readonly maxTurns; constructor(config: ClaudeCodeConfig); /** * apiKey is optional for this provider — the SDK reads * ANTHROPIC_API_KEY from the environment or falls back to the local * Claude Code credentials. We override the base validator to skip the * non-empty apiKey requirement. */ protected validateAndNormalizeConfig(config: AIProviderConfig): AIProviderConfig; protected doInitialize(): Promise<void>; protected doComplete(params: CompletionParams): Promise<CompletionResponse<string>>; protected doStream<T = any>(params: CompletionParams<T>): AsyncIterable<CompletionChunk>; getInfo(): ProviderInfo; protected mapProviderError(error: any): AIProviderError | null; private buildPrompt; private buildOptions; private errorForSdkAssistantError; }