import * as plugins from './plugins.js'; import { classifyAnthropicLimit } from './smartai.limits.anthropic.js'; import { createAnthropicCachingMiddleware } from './smartai.middleware.anthropic.js'; import { ANTHROPIC_API_BASE_URL, type IAnthropicModelConnection } from './smartai.oauth.anthropic.js'; import { isAnthropicCacheTransparentTransport, readAnthropicEffectiveCachePolicy } from './smartai.cache.anthropic.js'; import { anthropicMessagesTerminal } from './smartai.terminal.anthropic.js'; export { ANTHROPIC_API_BASE_URL, ANTHROPIC_API_VERSION, ANTHROPIC_EFFORTS, ANTHROPIC_OAUTH_BETA, AnthropicOAuthAuthError, AnthropicOAuthModelListError, createAnthropicOAuthHeaders, createAnthropicOAuthResolvingConnection, listAnthropicOAuthModels, mergeAnthropicOAuthBeta, type IAnthropicModelConnection, type IAnthropicOAuthAccessGrant, type IAnthropicOAuthAccessResolveRequest, type IAnthropicOAuthIdentity, type IAnthropicOAuthModel, type IAnthropicOAuthModelListOptions, type IAnthropicOAuthReadAccess, type IAnthropicOAuthResolvingConnectionOptions, type TAnthropicEffort, type TAnthropicOAuthModelListErrorCode, type TAnthropicOAuthRequestOutcome, } from './smartai.oauth.anthropic.js'; export { AnthropicApiKeyAuthError, createAnthropicApiKeyConnection, createAnthropicApiKeyHeaders, listAnthropicApiKeyModels, type IAnthropicApiKeyAccess, type IAnthropicApiKeyConnectionOptions, } from './smartai.apikey.anthropic.js'; export { ANTHROPIC_MODEL_PRICES, ANTHROPIC_SERVER_TOOL_PRICES, getAnthropicModelPrice, getAnthropicRequestPrice, getAnthropicServerToolPrice, readAnthropicMessagesUsage, type IAnthropicServerToolPrice, } from './smartai.pricing.anthropic.js'; export interface IAnthropicModelOptions extends plugins.ISmartAiOptions { provider: 'anthropic'; promptCaching?: boolean | plugins.ISmartAiCacheOptions; /** Authentication supplied by a component such as `createAnthropicOAuthResolvingConnection`; `apiKey` and `baseUrl` are then unused. */ connection?: IAnthropicModelConnection; } export const createAnthropicModelProvider = (): plugins.IModelProvider => ({ id: 'anthropic', getModel: (options) => { const connection = options.connection; // An explicit address, and for a connection an explicit key: the SDK would otherwise take // ANTHROPIC_BASE_URL and ANTHROPIC_API_KEY from the environment. A connection without a key // authenticates its own way, with its `authToken` for example. const settings: plugins.AnthropicProviderSettings = connection ? { ...connection.settings, apiKey: connection.settings.apiKey ?? '', baseURL: connection.settings.baseURL || ANTHROPIC_API_BASE_URL } : { apiKey: options.apiKey, baseURL: options.baseUrl || ANTHROPIC_API_BASE_URL }; const createModel = (observer?: plugins.TSmartAiCachePolicyObserver): plugins.LanguageModelV4 => { const fetchWithObservation: NonNullable = async (input, init) => { init?.signal?.throwIfAborted(); observer!({ dispatchedAt: Date.now(), policy: isAnthropicCacheTransparentTransport(settings.fetch) ? readAnthropicEffectiveCachePolicy(input instanceof Request ? input.url : String(input), init?.body) : undefined, }); return (settings.fetch ?? globalThis.fetch)(input, init); }; // An answer ends at its terminal event, not at the end of the connection (AGL-1130). const answers = plugins.endModelStreamsAtTerminalEvent( anthropicMessagesTerminal, observer ? fetchWithObservation : (input, init) => (settings.fetch ?? globalThis.fetch)(input, init), ); const model = plugins.createAnthropic({ ...settings, fetch: answers.fetch })(options.model); const limits = plugins.createModelLimitMiddleware({ provider: 'anthropic', classify: (failure) => connection?.classifyLimit?.(failure) ?? classifyAnthropicLimit(failure), }); const middleware = options.promptCaching === false ? [limits] : [limits, createAnthropicCachingMiddleware( typeof options.promptCaching === 'object' ? options.promptCaching : undefined, )]; // The answers' middleware innermost, next to the model whose fetch it pairs with. return plugins.wrapLanguageModel({ model, middleware: [...middleware, ...(connection?.middleware ? [connection.middleware] : []), answers.middleware], }); }; // Rebuild the owning pipeline with a request-local observer. An outer SDK wrapper // does not receive this capability, and simultaneous users never share a callback. return plugins.registerSmartAiCachePolicyObserver(createModel(), createModel); }, }); export { classifyAnthropicLimit, createAnthropicCachingMiddleware };