/** * @plan PLAN-20251023-STATELESS-HARDENING.P08 * @requirement REQ-SP2-001 * @project-plans/debuglogging/requirements.md */ import { type IModel } from '../IModel.js'; import type { ToolFormat } from '../../tools/IToolFormatter.js'; import { type IProviderConfig } from '../types/IProviderConfig.js'; import { BaseProvider, type NormalizedGenerateChatOptions } from '../BaseProvider.js'; import { type OAuthManager } from '../../auth/precedence.js'; import { type IContent } from '../../services/history/IContent.js'; type AnthropicImageBlock = { type: 'image'; source: { type: 'base64'; media_type: string; data: string; } | { type: 'url'; url: string; }; }; type AnthropicDocumentBlock = { type: 'document'; source: { type: 'base64'; media_type: string; data: string; }; title?: string; }; type AnthropicToolResultContent = string | Array<{ type: 'text'; text: string; } | AnthropicImageBlock | AnthropicDocumentBlock>; /** * Rate limit information from Anthropic API response headers */ export interface AnthropicRateLimitInfo { requestsLimit?: number; requestsRemaining?: number; requestsReset?: Date; tokensLimit?: number; tokensRemaining?: number; tokensReset?: Date; inputTokensLimit?: number; inputTokensRemaining?: number; } /** * Discriminated union of Anthropic content block types that support cache_control. * Each variant lists only the Anthropic-permitted keys for its block type. * @issue #1414 */ export type CacheableAnthropicBlock = { type: 'text'; text: string; } | { type: 'tool_use'; id: string; name: string; input: unknown; } | { type: 'tool_result'; tool_use_id: string; content: AnthropicToolResultContent; is_error?: boolean; } | AnthropicImageBlock | { type: 'thinking'; thinking: string; signature?: string; } | { type: 'redacted_thinking'; data: string; }; /** * A cacheable block with cache_control attached. * @issue #1414 */ export type CachedAnthropicBlock = CacheableAnthropicBlock & { cache_control: { type: 'ephemeral'; ttl: '5m' | '1h'; }; }; /** * Sanitize a content block before attaching cache_control. * Only copies Anthropic-permitted keys for each block type so that extra * properties (from deserialization, SDK mutations, etc.) never reach the API. * Prevents Anthropic 400 "text: Extra inputs are not permitted". * Unknown block types are returned as minimal text blocks to avoid * permissive spread of unexpected keys. * @issue #1414 */ export declare function sanitizeBlockForCacheControl(block: CacheableAnthropicBlock, ttl: '5m' | '1h'): CachedAnthropicBlock; export declare class AnthropicProvider extends BaseProvider { private static modelTokenPatterns; private lastRateLimitInfo?; constructor(apiKey?: string, baseURL?: string, config?: IProviderConfig, oauthManager?: OAuthManager); /** * Implementation of BaseProvider abstract method * Determines if this provider supports OAuth authentication */ protected supportsOAuth(): boolean; private getLogger; private getStreamingLogger; private getToolsLogger; private getAuthLogger; private getErrorsLogger; private getCacheLogger; private getRateLimitLogger; private instantiateClient; /** * @plan PLAN-20251023-STATELESS-HARDENING.P08 * @requirement REQ-SP4-002 * @project-plans/20251023stateless4/analysis/pseudocode/provider-cache-elimination.md line 11 * Build provider client per call with fresh SDK instance */ private buildProviderClient; /** * @plan PLAN-20251023-STATELESS-HARDENING.P08 * @requirement REQ-SP4-002 * @project-plans/20251023stateless4/analysis/pseudocode/provider-cache-elimination.md line 15 * No operation - stateless provider has no cache to clear */ clearClientCache(_runtimeKey?: string): void; clearAuthCache(): void; getModels(): Promise; getCurrentModel(): string; getDefaultModel(): string; /** * Returns default model list when no authentication is available */ private getDefaultModels; /** * Helper method to get the latest Claude 4 model ID for a given tier. * This can be used when you want to ensure you're using the latest model. * @param tier - The model tier: 'opus', 'sonnet', or 'haiku' * @returns The latest model ID for that tier */ getLatestClaude4Model(tier?: 'opus' | 'sonnet' | 'haiku'): string; private getMaxTokensForModel; private getContextWindowForModel; /** * Anthropic always requires payment (API key or OAuth) */ isPaidMode(): boolean; /** * Get the list of server tools supported by this provider */ getServerTools(): string[]; /** * Invoke a server tool (native provider tool) */ invokeServerTool(_toolName: string, _params: unknown, _config?: unknown, _signal?: AbortSignal): Promise; getToolFormat(): ToolFormat; getRateLimitInfo(): AnthropicRateLimitInfo | undefined; /** * Get current model parameters from SettingsService per call * @returns Current parameters or undefined if not set * @plan PLAN-20251023-STATELESS-HARDENING.P08 * @plan PLAN-20260126-SETTINGS-SEPARATION.P09 * @requirement REQ-SP4-003 * Gets model parameters from SettingsService per call (stateless) * Now uses pre-separated modelParams from invocation context */ getModelParams(): Record | undefined; /** * Check if the provider is authenticated using any available method * Uses the base provider's isAuthenticated implementation */ isAuthenticated(): Promise; /** * Detect the appropriate tool format for the current model/configuration * @returns The detected tool format */ detectToolFormat(): ToolFormat; /** * Normalize tool IDs from Anthropic format to history format */ private normalizeToHistoryToolId; private unprefixToolName; /** * Find the JSON schema for a tool by name from the tools array. * Used for schema-aware parameter coercion (issue #1146). */ private findToolSchema; /** * Sort object keys alphabetically for stable JSON serialization * This prevents cache invalidation due to key order changes */ private sortObjectKeys; /** * Merge beta headers, ensuring no duplicates */ private mergeBetaHeaders; /** * @plan PLAN-20251023-STATELESS-HARDENING.P08 * @requirement REQ-SP4-002, REQ-SP4-003 * @project-plans/20251023stateless4/analysis/pseudocode/provider-cache-elimination.md line 11 */ protected generateChatCompletionWithOptions(options: NormalizedGenerateChatOptions): AsyncIterableIterator; private getRetryConfig; /** * Extract rate limit information from response headers */ private extractRateLimitHeaders; /** * Check rate limits and log warnings if approaching limits */ private checkRateLimits; /** * Normalize tool IDs from various formats to Anthropic format. * Sanitizes invalid characters (not matching ^[a-zA-Z0-9_-]+$) by replacing with hyphens. */ private normalizeToAnthropicToolId; /** * Wait for rate limit reset if needed based on current rate limit state * This proactively throttles requests before they're made to prevent hitting rate limits * @private */ private waitForRateLimitIfNeeded; /** * Sleep for the specified number of milliseconds * @private */ private sleep; } export {};