UNPKG

@juspay/neurolink

Version:

The pipe layer of an AI nervous system: one interface connecting provider neurons to your application, across three inference types — generate, stream and decide. `decide` returns typed, calibrated judgements from a non-generative model (~400ms, ~$0.00002

179 lines (178 loc) • 7.75 kB
import { type AIProviderName } from "../../constants/enums.js"; import { BaseProvider } from "../../core/baseProvider.js"; import type { AnthropicProviderConfig, StreamOptions, StreamResult, ValidationSchema, EnhancedGenerateResult, TextGenerationOptions, AnthropicAuthMethod, AnthropicRateLimitInfo, AnthropicResponseMetadata, ClaudeSubscriptionTier, ClaudeUsageInfo } from "../../types/index.js"; import type { LanguageModel } from "../../types/index.js"; /** * Anthropic Provider v2 - BaseProvider Implementation * Enhanced with OAuth support, subscription tiers, and beta headers for Claude Code integration. */ export declare class AnthropicProvider extends BaseProvider { private client; private readonly authMethod; private readonly subscriptionTier; private readonly enableBetaFeatures; /** * Where requests go when not to `api.anthropic.com`: the credentials' * `baseURL`, else the config's, else `ANTHROPIC_BASE_URL` — normalized once * (no trailing slash, no `/vN` suffix: the SDK appends `/v1` itself). * Undefined means the vendor's own endpoint; every "proxy in use" decision * reads this, never the environment directly, so a per-instance gateway is * honoured the same way the environment one always was. */ private readonly baseURL; private oauthToken; private lastResponseMetadata; private usageInfo; private refreshPromise?; /** * Create a new Anthropic provider instance. * * @param modelName - Optional model name to use (defaults to CLAUDE_3_5_SONNET) * @param sdk - Optional NeuroLink SDK instance * @param config - Optional configuration options for auth, subscription tier, and beta features */ constructor(modelName?: string, sdk?: unknown, config?: AnthropicProviderConfig, credentials?: { apiKey?: string; oauthToken?: string; baseURL?: string; }); /** * Get authentication headers based on current auth method and configuration. * * @returns Headers object containing auth and beta feature headers */ getAuthHeaders(): Record<string, string>; /** * Validate if a model is accessible with the current subscription tier. * * @param model - The model ID to validate * @returns true if the model is accessible, false otherwise * * @example * ```typescript * const provider = new AnthropicProvider(); * if (provider.validateModelAccess("claude-opus-4-5-20251101")) { * // Use the model * } else { * // Fall back to a different model or show upgrade prompt * } * ``` */ validateModelAccess(model: string): boolean; /** * Get current usage information. * * Returns usage tracking data including messages sent, tokens consumed, * and remaining quotas. This information is updated after each API request. * * @returns Current usage info or null if no requests have been made * * @example * ```typescript * const usage = provider.getUsageInfo(); * if (usage && usage.tokenQuotaPercent > 80) { * console.warn("Approaching token quota limit"); * } * ``` */ getUsageInfo(): ClaudeUsageInfo | null; /** * Check if beta features are enabled for this provider instance. * * @returns true if beta features are enabled */ areBetaFeaturesEnabled(): boolean; /** * Get model capabilities for the current model. * * @returns The model capabilities or undefined if not found */ getModelCapabilities(): import("../../index.js").AnthropicModelMetadata | undefined; /** * Get the current subscription tier. * @returns The detected or configured subscription tier */ getSubscriptionTier(): ClaudeSubscriptionTier; /** * Get the authentication method being used. * @returns The current authentication method */ getAuthMethod(): AnthropicAuthMethod; /** * Refresh OAuth token if needed and possible. * This method checks if the token is expired or about to expire, * and attempts to refresh it using the refresh token if available. * * @returns Promise that resolves when refresh is complete (or not needed) * @throws Error if refresh is needed but fails */ refreshAuthIfNeeded(): Promise<void>; /** * Get the last response metadata including rate limit information. * @returns The last response metadata or null if no request has been made */ getLastResponseMetadata(): AnthropicResponseMetadata | null; /** * Update response metadata from a captured limit snapshot. * * Takes already-parsed rate-limit info rather than raw headers: parsing now * lives in `rateLimitCapture`, which is the only layer that sees the raw * response and understands both header families (unified subscription * windows and legacy per-tier counters). * * @param rateLimit - Parsed rate-limit figures * @param requestId - Optional Anthropic request ID * @param usageUpdate - Optional token counts to fold into usage tracking */ protected updateResponseMetadata(rateLimit: AnthropicRateLimitInfo, requestId?: string, usageUpdate?: { inputTokens?: number; outputTokens?: number; }): void; getProviderName(): AIProviderName; getDefaultModel(): string; /** * Returns a V3-shaped delegating model whose `doGenerate` drives the * official Anthropic Messages API directly. BaseProvider's `generate()` * path (and its middleware wrapping) keeps working unchanged; the * streaming path bypasses this entirely via `executeStream`. */ getAISDKModel(): LanguageModel; protected formatProviderError(error: unknown): Error; /** * Override generate to refresh the OAuth token before delegating to * BaseProvider so that expired tokens are renewed automatically. */ generate(optionsOrPrompt: TextGenerationOptions | string, analysisSchema?: ValidationSchema): Promise<EnhancedGenerateResult | null>; /** * Text turns run natively; every other request kind still goes to * BaseProvider.generate(). * * The loop runs over this provider's own delegating-model `doGenerate`, * which issues a NON-streaming `messages.create`. That matters: the streaming * loop adapter hardcodes `stream: true`, and an earlier attempt that routed * generate through it silently changed the wire. */ private dispatchGenerate; private executeNativeGenerate; /** * Fold a captured snapshot into the provider's usage bookkeeping and log it. * * `updateResponseMetadata` had no callers before this — the metadata it * maintains, and the public `getLastResponseMetadata()` / `getUsageInfo()` * that read it, were never populated by anything. */ private recordLimitSnapshot; /** * The native stream loop below is the one implementation of the * `executionControl` contract: it arms the lifetime policy, hands the * per-request deadline and the terminal-event requirement to the loop * adapter, and runs the step-boundary callback through the engine. Every * other provider rejects the option rather than ignoring it. */ supportsExecutionControl(): boolean; protected executeStream(options: StreamOptions, analysisSchema?: ValidationSchema): Promise<StreamResult>; private executeStreamInCaptureScope; isAvailable(): Promise<boolean>; getModel(): LanguageModel; } export { getModelCapabilities, getRecommendedModelForTier, isModelAvailableForTier, ModelAccessError, } from "../../models/anthropicModels.js";