UNPKG

@nyrra/foundry-ai

Version:

Thin Palantir Foundry provider adapters and model catalog for the Vercel AI SDK.

99 lines (98 loc) 3.57 kB
import { wrapFoundryLanguageModel } from "../chunk-NF2EPQB5.mjs"; import { resolveFoundryConfig, resolveModelTarget } from "../chunk-7OARDNWY.mjs"; // src/providers/anthropic.ts import { createAnthropic } from "@ai-sdk/anthropic"; import { NoSuchModelError } from "ai"; function createFoundryAnthropic(config) { const providerId = "foundry-anthropic"; const resolvedConfig = resolveFoundryConfig(config, "createFoundryAnthropic"); const headers = resolvedConfig.attributionRid || resolvedConfig.traceParent || resolvedConfig.traceState ? { ...resolvedConfig.attributionRid ? { attribution: resolvedConfig.attributionRid } : {}, ...resolvedConfig.traceParent ? { traceParent: resolvedConfig.traceParent } : {}, ...resolvedConfig.traceState ? { traceState: resolvedConfig.traceState } : {} } : void 0; const baseProvider = createAnthropic({ authToken: resolvedConfig.token, baseURL: `${resolvedConfig.foundryUrl}/api/v2/llm/proxy/anthropic/v1`, headers, name: providerId }); const createLanguageModel = (modelId) => { const resolvedModel = resolveModelTarget(modelId); return wrapFoundryLanguageModel(baseProvider(resolvedModel.rid), { modelId, providerId, transformParams: applyAnthropicCompat }); }; function provider(modelId) { return createLanguageModel(modelId); } const callableProvider = provider; callableProvider.specificationVersion = baseProvider.specificationVersion; callableProvider.languageModel = createLanguageModel; callableProvider.chat = createLanguageModel; callableProvider.messages = createLanguageModel; callableProvider.embeddingModel = (modelId) => { throw new NoSuchModelError({ modelId, modelType: "embeddingModel" }); }; callableProvider.imageModel = (modelId) => { throw new NoSuchModelError({ modelId, modelType: "imageModel" }); }; return callableProvider; } function applyAnthropicCompat(params) { const anthropicOptions = asRecord(params.providerOptions?.anthropic); const foundryAnthropicOptions = asRecord(params.providerOptions?.["foundry-anthropic"]); if (anthropicOptions.toolStreaming === true || foundryAnthropicOptions.toolStreaming === true) { throw new Error( "Foundry Anthropic does not support toolStreaming=true in Anthropic provider options. Remove the option or set it to false." ); } return { ...params, providerOptions: { ...params.providerOptions ?? {}, anthropic: { ...anthropicOptions, structuredOutputMode: "jsonTool", toolStreaming: false }, ...params.providerOptions?.["foundry-anthropic"] != null ? { "foundry-anthropic": { ...foundryAnthropicOptions, structuredOutputMode: "jsonTool", toolStreaming: false } } : {} }, ...params.tools != null ? { tools: params.tools.map(rejectAnthropicEagerInputStreaming) } : {} }; } function rejectAnthropicEagerInputStreaming(tool) { if (tool.type !== "function") { return tool; } const anthropicOptions = asRecord(tool.providerOptions?.anthropic); if (anthropicOptions.eagerInputStreaming === true) { throw new Error( "Foundry Anthropic does not support tool providerOptions.anthropic.eagerInputStreaming=true. Remove the option or set it to false." ); } return tool; } function asRecord(value) { if (value == null || typeof value !== "object" || Array.isArray(value)) { return {}; } return value; } export { createFoundryAnthropic }; //# sourceMappingURL=anthropic.mjs.map