@happyvertical/ai 0.96.1 → 0.98.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (87) hide show
  1. package/README.md +123 -3
  2. package/dist/chunks/{anthropic-CD9Ud7a5.js → anthropic-6ZDF32jo.js} +181 -27
  3. package/dist/chunks/anthropic-6ZDF32jo.js.map +1 -0
  4. package/dist/chunks/{bedrock-Bv1qCquH.js → bedrock-BQi3Z9jR.js} +71 -21
  5. package/dist/chunks/bedrock-BQi3Z9jR.js.map +1 -0
  6. package/dist/chunks/{bifrost-DzT2J0Lq.js → bifrost-DsI2I4ka.js} +9 -6
  7. package/dist/chunks/bifrost-DsI2I4ka.js.map +1 -0
  8. package/dist/chunks/{byteplus-modelark-BIEXaTWw.js → byteplus-modelark-Dq8AgCQD.js} +7 -5
  9. package/dist/chunks/byteplus-modelark-Dq8AgCQD.js.map +1 -0
  10. package/dist/chunks/{claude-cli-Chfnckyj.js → claude-cli-DIFxvG3c.js} +7 -5
  11. package/dist/chunks/claude-cli-DIFxvG3c.js.map +1 -0
  12. package/dist/chunks/continuation-D0NisZA1.js +209 -0
  13. package/dist/chunks/continuation-D0NisZA1.js.map +1 -0
  14. package/dist/chunks/{gateway-admin-04mvbDHC.js → gateway-audio-models-Dn7Wrc6P.js} +53 -3
  15. package/dist/chunks/gateway-audio-models-Dn7Wrc6P.js.map +1 -0
  16. package/dist/chunks/{gemini-BGwrHeGc.js → gemini-BLFwWN90.js} +131 -13
  17. package/dist/chunks/gemini-BLFwWN90.js.map +1 -0
  18. package/dist/chunks/{huggingface-mthYVbLR.js → huggingface-BuRwlABZ.js} +8 -6
  19. package/dist/chunks/huggingface-BuRwlABZ.js.map +1 -0
  20. package/dist/chunks/{litellm-xCO8QlGI.js → litellm-CpfCT6jN.js} +9 -6
  21. package/dist/chunks/litellm-CpfCT6jN.js.map +1 -0
  22. package/dist/chunks/{ollama-Bedt8Agy.js → ollama-CnrskTUg.js} +38 -7
  23. package/dist/chunks/ollama-CnrskTUg.js.map +1 -0
  24. package/dist/chunks/{openai-compat-video-B-xVe9bW.js → openai-compat-video-CJIDdkUO.js} +7 -5
  25. package/dist/chunks/openai-compat-video-CJIDdkUO.js.map +1 -0
  26. package/dist/chunks/{qwen-tts-D-UmiMMy.js → qwen-tts-PzEkWcEb.js} +2 -2
  27. package/dist/chunks/{qwen-tts-D-UmiMMy.js.map → qwen-tts-PzEkWcEb.js.map} +1 -1
  28. package/dist/chunks/rate-limit-BzWfHA0e.js +713 -0
  29. package/dist/chunks/rate-limit-BzWfHA0e.js.map +1 -0
  30. package/dist/chunks/{seevio-BJRiguLc.js → seevio-D2y5Io7C.js} +4 -5
  31. package/dist/chunks/seevio-D2y5Io7C.js.map +1 -0
  32. package/dist/chunks/{src-BUQUZK2M.js → src-CLT6r1W0.js} +36 -23
  33. package/dist/chunks/src-CLT6r1W0.js.map +1 -0
  34. package/dist/chunks/tool-messages-0ND35mYX.js +57 -0
  35. package/dist/chunks/tool-messages-0ND35mYX.js.map +1 -0
  36. package/dist/chunks/{types-BpnNcTU1.js → types-DB5W6_F-.js} +37 -5
  37. package/dist/chunks/types-DB5W6_F-.js.map +1 -0
  38. package/dist/chunks/{typesafe-B0gPZQJg.js → typesafe-CVUqQ8N1.js} +7 -5
  39. package/dist/chunks/typesafe-CVUqQ8N1.js.map +1 -0
  40. package/dist/index.js +4 -4
  41. package/dist/node.js +5 -5
  42. package/dist/shared/continuation.d.ts +51 -0
  43. package/dist/shared/continuation.d.ts.map +1 -0
  44. package/dist/shared/factory.d.ts.map +1 -1
  45. package/dist/shared/providers/anthropic.d.ts +53 -0
  46. package/dist/shared/providers/anthropic.d.ts.map +1 -1
  47. package/dist/shared/providers/bedrock.d.ts +13 -0
  48. package/dist/shared/providers/bedrock.d.ts.map +1 -1
  49. package/dist/shared/providers/bifrost.d.ts.map +1 -1
  50. package/dist/shared/providers/claude-cli.d.ts.map +1 -1
  51. package/dist/shared/providers/gateway-audio-models.d.ts +24 -0
  52. package/dist/shared/providers/gateway-audio-models.d.ts.map +1 -0
  53. package/dist/shared/providers/gemini.d.ts +28 -0
  54. package/dist/shared/providers/gemini.d.ts.map +1 -1
  55. package/dist/shared/providers/huggingface.d.ts.map +1 -1
  56. package/dist/shared/providers/litellm.d.ts.map +1 -1
  57. package/dist/shared/providers/ollama.d.ts.map +1 -1
  58. package/dist/shared/providers/openai.d.ts.map +1 -1
  59. package/dist/shared/providers/seevio.d.ts.map +1 -1
  60. package/dist/shared/providers/typesafe.d.ts.map +1 -1
  61. package/dist/shared/rate-limit.d.ts +19 -1
  62. package/dist/shared/rate-limit.d.ts.map +1 -1
  63. package/dist/shared/safety.d.ts.map +1 -1
  64. package/dist/shared/tool-messages.d.ts +27 -0
  65. package/dist/shared/tool-messages.d.ts.map +1 -0
  66. package/dist/shared/types.d.ts +191 -22
  67. package/dist/shared/types.d.ts.map +1 -1
  68. package/package.json +3 -3
  69. package/dist/chunks/anthropic-CD9Ud7a5.js.map +0 -1
  70. package/dist/chunks/bedrock-Bv1qCquH.js.map +0 -1
  71. package/dist/chunks/bifrost-DzT2J0Lq.js.map +0 -1
  72. package/dist/chunks/byteplus-modelark-BIEXaTWw.js.map +0 -1
  73. package/dist/chunks/claude-cli-Chfnckyj.js.map +0 -1
  74. package/dist/chunks/gateway-admin-04mvbDHC.js.map +0 -1
  75. package/dist/chunks/gemini-BGwrHeGc.js.map +0 -1
  76. package/dist/chunks/huggingface-mthYVbLR.js.map +0 -1
  77. package/dist/chunks/litellm-xCO8QlGI.js.map +0 -1
  78. package/dist/chunks/ollama-Bedt8Agy.js.map +0 -1
  79. package/dist/chunks/openai-compat-video-B-xVe9bW.js.map +0 -1
  80. package/dist/chunks/rate-limit-BVW_1Tey.js +0 -202
  81. package/dist/chunks/rate-limit-BVW_1Tey.js.map +0 -1
  82. package/dist/chunks/safety-BKswBoqy.js +0 -279
  83. package/dist/chunks/safety-BKswBoqy.js.map +0 -1
  84. package/dist/chunks/seevio-BJRiguLc.js.map +0 -1
  85. package/dist/chunks/src-BUQUZK2M.js.map +0 -1
  86. package/dist/chunks/types-BpnNcTU1.js.map +0 -1
  87. package/dist/chunks/typesafe-B0gPZQJg.js.map +0 -1
@@ -1 +0,0 @@
1
- {"version":3,"file":"bedrock-Bv1qCquH.js","names":[],"sources":["../../src/shared/providers/bedrock.ts"],"sourcesContent":["/**\n * AWS Bedrock provider implementation\n */\n\nimport {\n normalizeBaseAIOptions,\n normalizeChatOptions,\n normalizeImageGenerationOptions,\n type PreparedRequestControls,\n prepareRequestControls,\n} from '../safety';\nimport type {\n AICapabilities,\n AIInterface,\n AIMessage,\n AIModel,\n AIResponse,\n BedrockOptions,\n ChatOptions,\n CompletionOptions,\n EmbeddingOptions,\n EmbeddingResponse,\n ImageDescriptionOptions,\n ImageEmbeddingOptions,\n ImageGenerationOptions,\n ImageGenerationResponse,\n MessageOptions,\n TokenUsage,\n TTSOptions,\n TTSResponse,\n VideoGenerationJob,\n VideoGenerationOptions,\n VideoGenerationResult,\n VideoGenerationStatusResult,\n Voice,\n VoiceCloneOptions,\n VoiceDesignOptions,\n VoiceListOptions,\n} from '../types';\nimport {\n AIError,\n AuthenticationError,\n ContextLengthError,\n extractTextContent,\n ModelNotFoundError,\n RateLimitError,\n} from '../types';\nimport { emitUsage } from './usage';\n\nconst BEDROCK_DEFAULT_CHAT_MODEL = 'anthropic.claude-3-5-sonnet-20241022-v2:0';\nconst BEDROCK_TEXT_EMBEDDING_MODEL = 'amazon.titan-embed-text-v2:0';\nconst BEDROCK_IMAGE_EMBEDDING_MODEL = 'amazon.titan-embed-image-v1';\nconst BEDROCK_IMAGE_GENERATION_MODEL = 'amazon.titan-image-generator-v2:0';\n\nexport class BedrockProvider implements AIInterface {\n private options: BedrockOptions;\n private client: any; // Will be BedrockRuntimeClient instance from @aws-sdk/client-bedrock-runtime\n\n constructor(options: BedrockOptions) {\n this.options = normalizeBaseAIOptions({\n defaultModel: BEDROCK_DEFAULT_CHAT_MODEL,\n ...options,\n });\n\n // Initialize AWS Bedrock client\n this.initializeClientSync();\n }\n\n private initializeClientSync() {\n try {\n // Dynamic import in constructor - this will work if the package is installed\n import('@aws-sdk/client-bedrock-runtime')\n .then(({ BedrockRuntimeClient }) => {\n this.client = new BedrockRuntimeClient({\n region: this.options.region,\n credentials: this.options.credentials,\n endpoint: this.options.endpoint,\n maxAttempts: (this.options.maxRetries || 0) + 1,\n });\n })\n .catch(() => {\n // Client will be null and we'll handle it in methods\n });\n } catch (_error) {\n // Client will be null and we'll handle it in methods\n }\n }\n\n private async ensureClient() {\n if (!this.client) {\n try {\n const { BedrockRuntimeClient } = await import(\n '@aws-sdk/client-bedrock-runtime'\n );\n this.client = new BedrockRuntimeClient({\n region: this.options.region,\n credentials: this.options.credentials,\n endpoint: this.options.endpoint,\n maxAttempts: (this.options.maxRetries || 0) + 1,\n });\n } catch (_error) {\n throw new AIError(\n 'Failed to initialize Bedrock client. Make sure @aws-sdk/client-bedrock-runtime is installed.',\n 'INITIALIZATION_ERROR',\n 'bedrock',\n );\n }\n }\n }\n\n async chat(\n messages: AIMessage[],\n options: ChatOptions = {},\n ): Promise<AIResponse> {\n const startTime = Date.now();\n let controls: PreparedRequestControls | undefined;\n try {\n await this.ensureClient();\n\n const modelId = options.model || this.options.defaultModel;\n options = normalizeChatOptions(this.options, options, 'bedrock', modelId);\n controls = prepareRequestControls(this.options, options);\n const response = this.mapConverseResponse(\n await this.client.converse(\n await this.buildConverseRequest(\n messages,\n options,\n modelId!,\n controls.signal,\n ),\n { abortSignal: controls.signal },\n ),\n modelId!,\n );\n\n emitUsage(\n this.options,\n 'bedrock',\n 'chat',\n response.model || modelId || 'unknown',\n response.usage,\n startTime,\n options.usageTags,\n );\n return response;\n } catch (error) {\n if (controls?.didTimeout()) {\n throw new AIError(\n `AI request timed out after ${controls.timeout}ms`,\n 'AI_TIMEOUT',\n 'bedrock',\n options.model,\n );\n }\n if (options.signal?.aborted) {\n throw new AIError(\n 'AI request aborted by caller',\n 'AI_ABORTED',\n 'bedrock',\n options.model,\n );\n }\n throw this.mapError(error);\n } finally {\n controls?.cleanup();\n }\n }\n\n async complete(\n prompt: string,\n options: CompletionOptions = {},\n ): Promise<AIResponse> {\n return this.chat([{ role: 'user', content: prompt }], {\n model: options.model,\n maxTokens: options.maxTokens,\n temperature: options.temperature,\n topP: options.topP,\n n: options.n,\n stop: options.stop,\n stream: options.stream,\n onProgress: options.onProgress,\n signal: options.signal,\n timeout: options.timeout,\n reasoning: options.reasoning,\n usageTags: options.usageTags,\n });\n }\n\n /**\n * Simple message interface for single-turn interactions with optional history\n *\n * @param text - The message text to send\n * @param options - Configuration options including history, model, etc.\n * @returns Promise resolving to the response content string\n */\n async message(text: string, options: MessageOptions = {}): Promise<string> {\n // Build messages array from history + current message\n const messages: AIMessage[] = [\n ...(options.history || []),\n { role: options.role || 'user', content: text },\n ];\n\n const response = await this.chat(messages, {\n model: options.model,\n maxTokens: options.maxTokens,\n temperature: options.temperature,\n topP: options.topP,\n stop: options.stop,\n stream: options.stream,\n frequencyPenalty: options.frequencyPenalty,\n presencePenalty: options.presencePenalty,\n responseFormat: options.responseFormat,\n seed: options.seed,\n tools: options.tools,\n toolChoice: options.toolChoice,\n onProgress: options.onProgress,\n signal: options.signal,\n timeout: options.timeout,\n reasoning: options.reasoning,\n usageTags: options.usageTags,\n });\n\n return response.content;\n }\n\n async embed(\n text: string | string[],\n options: EmbeddingOptions = {},\n ): Promise<EmbeddingResponse> {\n const startTime = Date.now();\n try {\n await this.ensureClient();\n\n const model = options.model || BEDROCK_TEXT_EMBEDDING_MODEL;\n const inputs = Array.isArray(text) ? text : [text];\n const embeddings: number[][] = [];\n let totalTokens = 0;\n\n for (const inputText of inputs) {\n const response = await this.client.invokeModel({\n modelId: model,\n contentType: 'application/json',\n accept: 'application/json',\n body: JSON.stringify({\n inputText,\n ...(options.dimensions && { dimensions: options.dimensions }),\n normalize: true,\n }),\n });\n\n const payload = await this.parseInvokeModelBody(response.body);\n const embedding = this.extractEmbeddingVector(payload);\n if (embedding) {\n embeddings.push(embedding);\n }\n\n totalTokens +=\n payload.inputTextTokenCount ||\n payload.inputTokenCount ||\n payload.tokenCount ||\n 0;\n }\n\n const usage: TokenUsage | undefined =\n totalTokens > 0\n ? {\n promptTokens: totalTokens,\n completionTokens: 0,\n totalTokens,\n }\n : undefined;\n\n emitUsage(\n this.options,\n 'bedrock',\n 'embed',\n model,\n usage,\n startTime,\n options.usageTags,\n );\n\n return {\n embeddings,\n model,\n usage,\n };\n } catch (error) {\n throw this.mapError(error);\n }\n }\n\n async embedImage(\n image: string | Buffer,\n options: ImageEmbeddingOptions = {},\n ): Promise<EmbeddingResponse> {\n const startTime = Date.now();\n try {\n await this.ensureClient();\n\n const model = options.model || BEDROCK_IMAGE_EMBEDDING_MODEL;\n const response = await this.client.invokeModel({\n modelId: model,\n contentType: 'application/json',\n accept: 'application/json',\n body: JSON.stringify({\n inputImage: await this.imageToBase64(image),\n ...(options.dimensions && {\n embeddingConfig: {\n outputEmbeddingLength: options.dimensions,\n },\n }),\n }),\n });\n\n const payload = await this.parseInvokeModelBody(response.body);\n const embedding = this.extractEmbeddingVector(payload);\n\n emitUsage(\n this.options,\n 'bedrock',\n 'embedImage',\n model,\n undefined,\n startTime,\n );\n\n return {\n embeddings: embedding ? [embedding] : [],\n model,\n };\n } catch (error) {\n throw this.mapError(error);\n }\n }\n\n async describeImage(\n image: string | Buffer,\n prompt?: string,\n options: ImageDescriptionOptions = {},\n ): Promise<string> {\n const imageUrl = await this.imageToDataUrl(image, options.signal);\n const response = await this.chat(\n [\n {\n role: 'user',\n content: [\n {\n type: 'text',\n text:\n prompt ||\n 'Describe this image for a search index. Include objects, mood, lighting, and any visible text.',\n },\n {\n type: 'image_url',\n image_url: { url: imageUrl },\n },\n ],\n },\n ],\n {\n model: options.model || this.options.defaultModel,\n maxTokens: options.maxTokens ?? 500,\n signal: options.signal,\n timeout: options.timeout,\n reasoning: options.reasoning,\n usageTags: options.usageTags,\n },\n );\n\n return response.content;\n }\n\n async generateImage(\n prompt: string,\n options: ImageGenerationOptions = {},\n ): Promise<ImageGenerationResponse> {\n let controls: PreparedRequestControls | undefined;\n try {\n await this.ensureClient();\n\n const model = options.model || BEDROCK_IMAGE_GENERATION_MODEL;\n options = normalizeImageGenerationOptions(\n this.options,\n options,\n 'bedrock',\n model,\n );\n controls = prepareRequestControls(this.options, options);\n const size = this.resolveImageSize(options);\n const response = await this.client.invokeModel(\n {\n modelId: model,\n contentType: 'application/json',\n accept: 'application/json',\n body: JSON.stringify({\n taskType: 'TEXT_IMAGE',\n textToImageParams: {\n text: prompt,\n ...(options.imageInput && {\n conditionImage: await this.imageToBase64(\n options.imageInput,\n controls.signal,\n ),\n }),\n },\n imageGenerationConfig: {\n numberOfImages: options.n,\n quality: this.mapImageQuality(options.quality),\n ...size,\n },\n }),\n },\n { abortSignal: controls.signal },\n );\n\n const payload = await this.parseInvokeModelBody(response.body);\n if (payload.error) {\n throw new AIError(payload.error, 'API_ERROR', 'bedrock');\n }\n\n const images = (payload.images || []).map((encoded: string) => {\n const mimeType = 'image/png';\n if (options.outputFormat === 'base64') {\n return { data: encoded, mimeType };\n }\n if (options.outputFormat === 'url') {\n return { data: `data:${mimeType};base64,${encoded}`, mimeType };\n }\n return {\n data: Buffer.from(encoded, 'base64'),\n mimeType,\n };\n });\n\n return {\n images,\n model,\n };\n } catch (error) {\n if (controls?.didTimeout()) {\n throw new AIError(\n `AI request timed out after ${controls.timeout}ms`,\n 'AI_TIMEOUT',\n 'bedrock',\n options.model,\n );\n }\n if (options.signal?.aborted) {\n throw new AIError(\n 'AI request aborted by caller',\n 'AI_ABORTED',\n 'bedrock',\n options.model,\n );\n }\n throw this.mapError(error);\n } finally {\n controls?.cleanup();\n }\n }\n\n async *stream(\n messages: AIMessage[],\n options: ChatOptions = {},\n ): AsyncIterable<string> {\n const startTime = Date.now();\n let controls: PreparedRequestControls | undefined;\n try {\n await this.ensureClient();\n\n const modelId = options.model || this.options.defaultModel;\n options = normalizeChatOptions(this.options, options, 'bedrock', modelId);\n controls = prepareRequestControls(this.options, options);\n const response = await this.client.converseStream(\n await this.buildConverseRequest(\n messages,\n options,\n modelId!,\n controls.signal,\n ),\n { abortSignal: controls.signal },\n );\n\n let usage: TokenUsage | undefined;\n\n for await (const event of response.stream || []) {\n const text = event.contentBlockDelta?.delta?.text;\n if (text) {\n if (options.onProgress) {\n options.onProgress(text);\n }\n yield text;\n }\n\n if (event.metadata?.usage) {\n usage = {\n promptTokens: event.metadata.usage.inputTokens || 0,\n completionTokens: event.metadata.usage.outputTokens || 0,\n totalTokens: event.metadata.usage.totalTokens || 0,\n };\n }\n }\n\n emitUsage(\n this.options,\n 'bedrock',\n 'stream',\n modelId || 'unknown',\n usage,\n startTime,\n options.usageTags,\n );\n } catch (error) {\n if (controls?.didTimeout()) {\n throw new AIError(\n `AI request timed out after ${controls.timeout}ms`,\n 'AI_TIMEOUT',\n 'bedrock',\n options.model,\n );\n }\n if (options.signal?.aborted) {\n throw new AIError(\n 'AI request aborted by caller',\n 'AI_ABORTED',\n 'bedrock',\n options.model,\n );\n }\n throw this.mapError(error);\n } finally {\n controls?.cleanup();\n }\n }\n\n async countTokens(text: string): Promise<number> {\n try {\n await this.ensureClient();\n\n const modelId = this.options.defaultModel || BEDROCK_DEFAULT_CHAT_MODEL;\n const response = await this.client.countTokens({\n modelId,\n input: {\n converse: {\n messages: [\n {\n role: 'user',\n content: [{ text }],\n },\n ],\n },\n },\n });\n\n return response.inputTokens || Math.ceil(text.length / 4);\n } catch (error) {\n throw this.mapError(error);\n }\n }\n\n async getModels(): Promise<AIModel[]> {\n // Return static list of popular Bedrock models\n return [\n // Anthropic Claude models\n {\n id: 'anthropic.claude-3-5-sonnet-20241022-v2:0',\n name: 'Claude 3.5 Sonnet v2',\n description: 'Latest Claude 3.5 Sonnet model on Bedrock',\n contextLength: 200000,\n capabilities: ['text', 'chat', 'vision', 'functions'],\n supportsFunctions: true,\n supportsVision: true,\n },\n {\n id: 'anthropic.claude-3-opus-20240229-v1:0',\n name: 'Claude 3 Opus',\n description: 'Most powerful Claude model on Bedrock',\n contextLength: 200000,\n capabilities: ['text', 'chat', 'vision'],\n supportsFunctions: false,\n supportsVision: true,\n },\n // Amazon Titan models\n {\n id: 'amazon.titan-text-premier-v1:0',\n name: 'Titan Text Premier',\n description: 'Premier Amazon Titan text model',\n contextLength: 32000,\n capabilities: ['text', 'chat'],\n supportsFunctions: false,\n supportsVision: false,\n },\n {\n id: 'amazon.titan-embed-text-v1',\n name: 'Titan Embeddings Text',\n description: 'Amazon Titan text embeddings model',\n contextLength: 8192,\n capabilities: ['embeddings'],\n supportsFunctions: false,\n supportsVision: false,\n },\n {\n id: BEDROCK_TEXT_EMBEDDING_MODEL,\n name: 'Titan Embeddings Text V2',\n description: 'Amazon Titan text embeddings v2 model',\n contextLength: 8192,\n capabilities: ['embeddings'],\n supportsFunctions: false,\n supportsVision: false,\n },\n {\n id: BEDROCK_IMAGE_EMBEDDING_MODEL,\n name: 'Titan Multimodal Embeddings G1',\n description:\n 'Amazon Titan multimodal embeddings model for image similarity',\n contextLength: 256,\n capabilities: ['embeddings', 'image_embedding'],\n supportsFunctions: false,\n supportsVision: false,\n },\n {\n id: BEDROCK_IMAGE_GENERATION_MODEL,\n name: 'Titan Image Generator V2',\n description: 'Amazon Titan image generation model',\n contextLength: 0,\n capabilities: ['image_generation'],\n supportsFunctions: false,\n supportsVision: false,\n },\n // Cohere models\n {\n id: 'cohere.command-r-plus-v1:0',\n name: 'Command R+',\n description: 'Cohere Command R+ model with advanced capabilities',\n contextLength: 128000,\n capabilities: ['text', 'chat', 'functions'],\n supportsFunctions: true,\n supportsVision: false,\n },\n // Meta Llama models\n {\n id: 'meta.llama3-1-405b-instruct-v1:0',\n name: 'Llama 3.1 405B Instruct',\n description: 'Meta Llama 3.1 405B instruction-tuned model',\n contextLength: 128000,\n capabilities: ['text', 'chat'],\n supportsFunctions: false,\n supportsVision: false,\n },\n ];\n }\n\n async getCapabilities(): Promise<AICapabilities> {\n return {\n chat: true,\n completion: true,\n embeddings: true,\n streaming: true,\n functions: true, // Some models support function calling\n vision: true, // Some models support vision\n fineTuning: true, // Via Bedrock fine-tuning\n imageEmbeddings: true,\n imageGeneration: true,\n videoGeneration: false,\n tts: false,\n voiceCloning: false,\n voiceDesign: false,\n maxContextLength: 200000,\n supportedOperations: [\n 'chat',\n 'completion',\n 'embedding',\n 'streaming',\n 'functions',\n 'vision',\n 'image_embedding',\n 'image_generation',\n ],\n };\n }\n\n // ============================================================================\n // Video Generation Methods (Not supported - use gemini, byteplus-modelark,\n // or openai-compat-video provider)\n // ============================================================================\n\n async submitVideoGenerationJob(\n _options: VideoGenerationOptions,\n ): Promise<VideoGenerationJob> {\n throw new AIError(\n 'Video generation is not supported by Bedrock provider. Use gemini, byteplus-modelark, or openai-compat-video provider.',\n 'NOT_IMPLEMENTED',\n 'bedrock',\n );\n }\n\n async getVideoGenerationJob(\n _handle: VideoGenerationJob,\n ): Promise<VideoGenerationStatusResult> {\n throw new AIError(\n 'Video generation is not supported by Bedrock provider. Use gemini, byteplus-modelark, or openai-compat-video provider.',\n 'NOT_IMPLEMENTED',\n 'bedrock',\n );\n }\n\n async fetchVideoGenerationResult(\n _handle: VideoGenerationJob,\n ): Promise<VideoGenerationResult> {\n throw new AIError(\n 'Video generation is not supported by Bedrock provider. Use gemini, byteplus-modelark, or openai-compat-video provider.',\n 'NOT_IMPLEMENTED',\n 'bedrock',\n );\n }\n\n async cancelVideoGenerationJob(_handle: VideoGenerationJob): Promise<void> {\n throw new AIError(\n 'Video generation is not supported by Bedrock provider. Use gemini, byteplus-modelark, or openai-compat-video provider.',\n 'NOT_IMPLEMENTED',\n 'bedrock',\n );\n }\n\n async validateVideoGenerationAccess(): Promise<boolean> {\n throw new AIError(\n 'Video generation is not supported by Bedrock provider. Use gemini, byteplus-modelark, or openai-compat-video provider.',\n 'NOT_IMPLEMENTED',\n 'bedrock',\n );\n }\n\n // ============================================================================\n // TTS Methods (Not supported - use Qwen3-TTS provider)\n // ============================================================================\n\n async synthesizeSpeech(\n _text: string,\n _options?: TTSOptions,\n ): Promise<TTSResponse> {\n throw new AIError(\n 'TTS is not supported by Bedrock provider. Use Qwen3-TTS provider.',\n 'NOT_IMPLEMENTED',\n 'bedrock',\n );\n }\n\n streamSpeech(_text: string, _options?: TTSOptions): AsyncIterable<Buffer> {\n const error = new AIError(\n 'TTS streaming is not supported by Bedrock provider. Use Qwen3-TTS provider.',\n 'NOT_IMPLEMENTED',\n 'bedrock',\n );\n return {\n [Symbol.asyncIterator]: () => ({\n next: () => Promise.reject(error),\n }),\n };\n }\n\n async cloneVoice(_options: VoiceCloneOptions): Promise<Voice> {\n throw new AIError(\n 'Voice cloning is not supported by Bedrock provider. Use Qwen3-TTS provider.',\n 'NOT_IMPLEMENTED',\n 'bedrock',\n );\n }\n\n async designVoice(_options: VoiceDesignOptions): Promise<Voice> {\n throw new AIError(\n 'Voice design is not supported by Bedrock provider. Use Qwen3-TTS provider.',\n 'NOT_IMPLEMENTED',\n 'bedrock',\n );\n }\n\n async getVoices(_options?: VoiceListOptions): Promise<Voice[]> {\n throw new AIError(\n 'Voice listing is not supported by Bedrock provider. Use Qwen3-TTS provider.',\n 'NOT_IMPLEMENTED',\n 'bedrock',\n );\n }\n\n private async buildConverseRequest(\n messages: AIMessage[],\n options: ChatOptions,\n modelId: string,\n signal?: AbortSignal,\n ): Promise<Record<string, any>> {\n const { system, bedrockMessages } = await this.mapMessagesToBedrock(\n messages,\n signal,\n );\n const systemPrompt =\n options.responseFormat?.type === 'json_object'\n ? [\n system,\n 'Respond with valid JSON only. Do not include explanatory text outside the JSON object.',\n ]\n .filter(Boolean)\n .join('\\n\\n')\n : system;\n\n const inferenceConfig = Object.fromEntries(\n Object.entries({\n maxTokens: options.maxTokens,\n temperature: options.temperature,\n topP: options.topP,\n stopSequences: Array.isArray(options.stop)\n ? options.stop\n : options.stop\n ? [options.stop]\n : undefined,\n }).filter(([, value]) => value !== undefined),\n );\n\n const request: Record<string, any> = {\n modelId,\n messages: bedrockMessages,\n ...(Object.keys(inferenceConfig).length > 0 && { inferenceConfig }),\n ...(systemPrompt && { system: [{ text: systemPrompt }] }),\n };\n if ((options.reasoning?.maxTokens || 0) > 0) {\n request.additionalModelRequestFields = {\n thinking: {\n type: 'enabled',\n budget_tokens: options.reasoning?.maxTokens,\n },\n };\n }\n\n const toolConfig = this.mapToolConfig(options);\n if (toolConfig) {\n request.toolConfig = toolConfig;\n }\n\n return request;\n }\n\n private async mapMessagesToBedrock(\n messages: AIMessage[],\n signal?: AbortSignal,\n ): Promise<{\n system?: string;\n bedrockMessages: Array<{ role: 'user' | 'assistant'; content: any[] }>;\n }> {\n let system: string | undefined;\n const bedrockMessages: Array<{\n role: 'user' | 'assistant';\n content: any[];\n }> = [];\n\n for (const message of messages) {\n const textContent = extractTextContent(message.content);\n if (message.role === 'system') {\n system = system ? `${system}\\n\\n${textContent}` : textContent;\n continue;\n }\n\n const content: any[] = [];\n if (typeof message.content === 'string') {\n content.push({ text: message.content });\n } else {\n for (const part of message.content) {\n if (part.type === 'text') {\n content.push({ text: part.text });\n continue;\n }\n\n const image = await this.imageUrlToBedrockImage(\n part.image_url.url,\n signal,\n );\n content.push({ image });\n }\n }\n\n if (message.role === 'assistant' && message.tool_calls) {\n for (const toolCall of message.tool_calls) {\n content.push({\n toolUse: {\n toolUseId: toolCall.id,\n name: toolCall.function.name,\n input: this.safeJsonParse(toolCall.function.arguments),\n },\n });\n }\n }\n\n if (content.length === 0 && textContent) {\n content.push({ text: textContent });\n }\n\n bedrockMessages.push({\n role: message.role === 'assistant' ? 'assistant' : 'user',\n content,\n });\n }\n\n return { system, bedrockMessages };\n }\n\n private mapConverseResponse(response: any, modelId: string): AIResponse {\n const contentBlocks = response.output?.message?.content || [];\n const textContent = contentBlocks\n .filter((block: any) => typeof block.text === 'string')\n .map((block: any) => block.text)\n .join('');\n\n const toolCalls = contentBlocks\n .filter((block: any) => block.toolUse)\n .map((block: any) => ({\n id: block.toolUse.toolUseId,\n type: 'function' as const,\n function: {\n name: block.toolUse.name,\n arguments: JSON.stringify(block.toolUse.input || {}),\n },\n }));\n\n const usage =\n response.usage &&\n ({\n promptTokens: response.usage.inputTokens || 0,\n completionTokens: response.usage.outputTokens || 0,\n totalTokens: response.usage.totalTokens || 0,\n } satisfies TokenUsage);\n\n return {\n content: textContent,\n model: modelId,\n finishReason: this.mapBedrockFinishReason(response.stopReason),\n usage,\n toolCalls: toolCalls.length > 0 ? toolCalls : undefined,\n };\n }\n\n private mapBedrockFinishReason(\n reason: string | null | undefined,\n ): AIResponse['finishReason'] {\n switch (reason) {\n case 'end_turn':\n return 'stop';\n case 'max_tokens':\n return 'length';\n case 'stop_sequence':\n return 'stop';\n case 'tool_use':\n return 'tool_calls';\n default:\n return 'stop';\n }\n }\n\n private mapToolConfig(options: ChatOptions): Record<string, any> | undefined {\n if (!options.tools || options.tools.length === 0) {\n return undefined;\n }\n\n if (options.toolChoice === 'none') {\n return undefined;\n }\n\n return {\n tools: options.tools.map((tool) => ({\n toolSpec: {\n name: tool.function.name,\n description: tool.function.description || '',\n inputSchema: {\n json: tool.function.parameters || { type: 'object' },\n },\n },\n })),\n ...(options.toolChoice && {\n toolChoice: this.mapToolChoice(options.toolChoice),\n }),\n };\n }\n\n private mapToolChoice(\n toolChoice?:\n | 'auto'\n | 'none'\n | { type: 'function'; function: { name: string } },\n ): Record<string, any> | undefined {\n if (!toolChoice || toolChoice === 'auto') {\n return { auto: {} };\n }\n\n if (toolChoice === 'none') {\n return undefined;\n }\n\n return {\n tool: {\n name: toolChoice.function.name,\n },\n };\n }\n\n private async parseInvokeModelBody(body: unknown): Promise<any> {\n const bytes =\n typeof (body as any)?.transformToByteArray === 'function'\n ? await (body as any).transformToByteArray()\n : body instanceof Uint8Array\n ? body\n : Buffer.isBuffer(body)\n ? body\n : new Uint8Array(body as ArrayBuffer);\n return JSON.parse(new TextDecoder().decode(bytes));\n }\n\n private extractEmbeddingVector(payload: any): number[] | undefined {\n if (Array.isArray(payload.embedding)) {\n return payload.embedding;\n }\n\n if (Array.isArray(payload.embeddings?.[0])) {\n return payload.embeddings[0];\n }\n\n if (Array.isArray(payload.embeddingsByType?.float)) {\n return payload.embeddingsByType.float;\n }\n\n if (Array.isArray(payload.vector)) {\n return payload.vector;\n }\n\n return undefined;\n }\n\n private async imageUrlToBedrockImage(\n imageUrl: string,\n signal?: AbortSignal,\n ): Promise<any> {\n const { bytes, mimeType } = await this.imageToBytes(imageUrl, signal);\n return {\n format: this.mimeTypeToBedrockImageFormat(mimeType),\n source: { bytes },\n };\n }\n\n private async imageToDataUrl(\n image: string | Buffer,\n signal?: AbortSignal,\n ): Promise<string> {\n if (Buffer.isBuffer(image)) {\n return `data:image/png;base64,${image.toString('base64')}`;\n }\n if (image.startsWith('data:')) {\n return image;\n }\n\n const { bytes, mimeType } = await this.imageToBytes(image, signal);\n return `data:${mimeType};base64,${Buffer.from(bytes).toString('base64')}`;\n }\n\n private async imageToBase64(\n image: string | Buffer,\n signal?: AbortSignal,\n ): Promise<string> {\n if (Buffer.isBuffer(image)) {\n return image.toString('base64');\n }\n if (image.startsWith('data:')) {\n const match = image.match(/^data:([^;]+);base64,(.+)$/);\n if (!match) {\n throw new AIError(\n 'Invalid base64 data URL format',\n 'INVALID_INPUT',\n 'bedrock',\n );\n }\n return match[2];\n }\n\n const { bytes } = await this.imageToBytes(image, signal);\n return Buffer.from(bytes).toString('base64');\n }\n\n private async imageToBytes(\n image: string,\n signal?: AbortSignal,\n ): Promise<{ bytes: Uint8Array; mimeType: string }> {\n if (image.startsWith('data:')) {\n const match = image.match(/^data:([^;]+);base64,(.+)$/);\n if (!match) {\n throw new AIError(\n 'Invalid base64 data URL format',\n 'INVALID_INPUT',\n 'bedrock',\n );\n }\n\n return {\n bytes: Uint8Array.from(Buffer.from(match[2], 'base64')),\n mimeType: match[1],\n };\n }\n\n const response = await fetch(image, { signal });\n if (!response.ok) {\n throw new AIError(\n `Failed to fetch image: ${response.status} ${response.statusText}`,\n 'IMAGE_FETCH_ERROR',\n 'bedrock',\n );\n }\n\n const arrayBuffer = await response.arrayBuffer();\n return {\n bytes: new Uint8Array(arrayBuffer),\n mimeType: response.headers.get('content-type') || 'image/png',\n };\n }\n\n private mimeTypeToBedrockImageFormat(mimeType: string): string {\n if (mimeType.includes('png')) return 'png';\n if (mimeType.includes('jpeg') || mimeType.includes('jpg')) return 'jpeg';\n if (mimeType.includes('gif')) return 'gif';\n if (mimeType.includes('webp')) return 'webp';\n\n throw new AIError(\n `Unsupported image format for Bedrock: ${mimeType}`,\n 'INVALID_INPUT',\n 'bedrock',\n );\n }\n\n private resolveImageSize(options: ImageGenerationOptions): {\n width: number;\n height: number;\n } {\n if (options.size) {\n const [width, height] = options.size.split('x').map(Number);\n if (Number.isFinite(width) && Number.isFinite(height)) {\n return { width, height };\n }\n }\n\n const aspectRatioSizes: Record<string, { width: number; height: number }> =\n {\n '1:1': { width: 1024, height: 1024 },\n '2:3': { width: 768, height: 1152 },\n '3:2': { width: 1152, height: 768 },\n '3:5': { width: 768, height: 1280 },\n '5:3': { width: 1280, height: 768 },\n '7:9': { width: 896, height: 1152 },\n '9:7': { width: 1152, height: 896 },\n '6:11': { width: 768, height: 1408 },\n '11:6': { width: 1408, height: 768 },\n '5:11': { width: 640, height: 1408 },\n '11:5': { width: 1408, height: 640 },\n '9:5': { width: 1152, height: 640 },\n '16:9': { width: 1173, height: 640 },\n };\n\n return (\n aspectRatioSizes[options.aspectRatio || ''] || {\n width: 1024,\n height: 1024,\n }\n );\n }\n\n private mapImageQuality(quality?: string): string {\n if (!quality || quality === 'standard') {\n return 'standard';\n }\n\n if (quality === 'hd') {\n return 'premium';\n }\n\n return quality;\n }\n\n private safeJsonParse(input: string): any {\n try {\n return JSON.parse(input);\n } catch {\n return { rawArguments: input };\n }\n }\n\n private mapError(error: unknown): AIError {\n if (error instanceof AIError) {\n return error;\n }\n\n // Map common AWS error codes\n if (typeof error === 'object' && error !== null) {\n const awsError = error as { name?: string; message?: string };\n\n if (awsError.name === 'AccessDeniedException') {\n return new AuthenticationError('bedrock');\n }\n\n if (awsError.name === 'ThrottlingException') {\n return new RateLimitError('bedrock');\n }\n\n if (awsError.name === 'ResourceNotFoundException') {\n return new ModelNotFoundError(\n awsError.message || 'Model not found',\n 'bedrock',\n );\n }\n\n if (\n awsError.name === 'ValidationException' &&\n awsError.message?.includes('input is too long')\n ) {\n return new ContextLengthError('bedrock');\n }\n }\n\n const errorMessage =\n error instanceof Error ? error.message : 'Unknown Bedrock error occurred';\n return new AIError(errorMessage, 'UNKNOWN_ERROR', 'bedrock');\n }\n}\n"],"mappings":";;;;;;;AAiDA,IAAM,6BAA6B;AACnC,IAAM,+BAA+B;AACrC,IAAM,gCAAgC;AACtC,IAAM,iCAAiC;AAEvC,IAAa,kBAAb,MAAoD;CAClD;CACA;CAEA,YAAY,SAAyB;EACnC,KAAK,UAAU,uBAAuB;GACpC,cAAc;GACd,GAAG;EACL,CAAC;EAGD,KAAK,qBAAqB;CAC5B;CAEA,uBAA+B;EAC7B,IAAI;GAEF,OAAO,kCAAkC,CACtC,MAAM,EAAE,2BAA2B;IAClC,KAAK,SAAS,IAAI,qBAAqB;KACrC,QAAQ,KAAK,QAAQ;KACrB,aAAa,KAAK,QAAQ;KAC1B,UAAU,KAAK,QAAQ;KACvB,cAAc,KAAK,QAAQ,cAAc,KAAK;IAChD,CAAC;GACH,CAAC,CAAC,CACD,YAAY,CAEb,CAAC;EACL,SAAS,QAAQ,CAEjB;CACF;CAEA,MAAc,eAAe;EAC3B,IAAI,CAAC,KAAK,QACR,IAAI;GACF,MAAM,EAAE,yBAAyB,MAAM,OACrC;GAEF,KAAK,SAAS,IAAI,qBAAqB;IACrC,QAAQ,KAAK,QAAQ;IACrB,aAAa,KAAK,QAAQ;IAC1B,UAAU,KAAK,QAAQ;IACvB,cAAc,KAAK,QAAQ,cAAc,KAAK;GAChD,CAAC;EACH,SAAS,QAAQ;GACf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AC7C,OAAO;GACP,QAAQ;EACV;CAEJ;CAEA,gBAAwB,SAA0B;EAChD,IAAI,CAAC,WAAW,YAAY,YAC1B,OAAO;EAGT,IAAI,YAAY,MACd,OAAO;EAGT,OAAO;CACT;CAEA,cAAsB,OAAoB;EACxC,IAAI;GACF,OAAO,KAAK,MAAM,KAAK;EACzB,QAAQ;GACN,OAAO,EAAE,cAAc,MAAM;EAC/B;CACF;CAEA,SAAiB,OAAyB;EACxC,IAAI,iBAAiB,SACnB,OAAO;EAIT,IAAI,OAAO,UAAU,YAAY,UAAU,MAAM;GAC/C,MAAM,WAAW;GAEjB,IAAI,SAAS,SAAS,yBACpB,OAAO,IAAI,oBAAoB,SAAS;GAG1C,IAAI,SAAS,SAAS,uBACpB,OAAO,IAAI,eAAe,SAAS;GAGrC,IAAI,SAAS,SAAS,6BACpB,OAAO,IAAI,mBACT,SAAS,WAAW,mBACpB,SACF;GAGF,IACE,SAAS,SAAS,yBAClB,SAAS,SAAS,SAAS,mBAAmB,GAE9C,OAAO,IAAI,mBAAmB,SAAS;EAE3C;EAIA,OAAO,IAAI,QADT,iBAAiB,QAAQ,MAAM,UAAU,kCACV,iBAAiB,SAAS;CAC7D;AACF"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"bifrost-DzT2J0Lq.js","names":[],"sources":["../../src/shared/providers/bifrost.ts"],"sourcesContent":["import { ValidationError } from '@happyvertical/utils';\n\nimport {\n normalizeChatOptions,\n normalizeImageGenerationOptions,\n} from '../safety';\nimport type {\n AICapabilities,\n AIMessage,\n AIModel,\n AIResponse,\n BifrostOptions,\n ChatOptions,\n EmbeddingOptions,\n EmbeddingResponse,\n ImageDescriptionOptions,\n ImageEmbeddingOptions,\n ImageGenerationOptions,\n ImageGenerationResponse,\n} from '../types';\nimport {\n BifrostAdmin,\n normalizeGatewayBaseUrl,\n resolveGatewayAdminBaseUrl,\n} from './gateway-admin';\nimport { type OpenAICompatibleProfile, OpenAIProvider } from './openai';\n\nconst BIFROST_CAPABILITIES: AICapabilities = {\n chat: true,\n completion: true,\n embeddings: true,\n streaming: true,\n functions: true,\n vision: true,\n fineTuning: false,\n imageEmbeddings: true,\n imageGeneration: true,\n videoGeneration: false,\n tts: false,\n voiceCloning: false,\n voiceDesign: false,\n maxContextLength: 128000,\n supportedOperations: [\n 'chat',\n 'completion',\n 'embedding',\n 'streaming',\n 'functions',\n 'vision',\n 'image_embedding',\n 'image_generation',\n ],\n};\n\nfunction isEmbeddingModel(modelId: string): boolean {\n return /embed/i.test(modelId);\n}\n\nfunction isImageEmbeddingModel(modelId: string): boolean {\n return /titan-embed-image|multimodalembedding|image[-_]?embed|clip/i.test(\n modelId,\n );\n}\n\nfunction isImageGenerationModel(modelId: string): boolean {\n return /gpt-image|dall-e|imagen|stable[-_ ]diffusion|sdxl|flux|titan-image|image-generator/i.test(\n modelId,\n );\n}\n\nfunction isFilteredBifrostModel(modelId: string): boolean {\n return /moderation|transcrib|whisper|speech|tts|rerank/i.test(modelId);\n}\n\nfunction inferBifrostContextLength(modelId: string): number {\n if (isEmbeddingModel(modelId)) return 8192;\n if (/gemini-1\\.5|gemini-2\\.0|gemini-2\\.5|gemini-3/i.test(modelId)) {\n return 1000000;\n }\n if (/claude/i.test(modelId)) return 200000;\n if (/gpt-4o|gpt-4\\.1|o1|o3|o4|llama|mistral|qwen|deepseek/i.test(modelId)) {\n return 128000;\n }\n return 32768;\n}\n\nfunction inferBifrostFunctions(modelId: string): boolean {\n if (isEmbeddingModel(modelId)) return false;\n return /gpt|claude|gemini|command|llama|mistral|qwen|deepseek|o1|o3|o4/i.test(\n modelId,\n );\n}\n\nfunction inferBifrostVision(modelId: string): boolean {\n return /gpt-4o|gpt-4\\.1|vision|claude-3|gemini|pixtral|llava|qwen.*vl|vl-/i.test(\n modelId,\n );\n}\n\nfunction inferBifrostCapabilities(modelId: string): string[] {\n if (isImageGenerationModel(modelId)) {\n return ['image_generation'];\n }\n\n if (isImageEmbeddingModel(modelId)) {\n return ['embeddings', 'image_embedding'];\n }\n\n if (isEmbeddingModel(modelId)) {\n return ['embeddings'];\n }\n\n const capabilities = ['text', 'chat'];\n\n if (inferBifrostFunctions(modelId)) {\n capabilities.push('functions');\n }\n\n if (inferBifrostVision(modelId)) {\n capabilities.push('vision');\n }\n\n return capabilities;\n}\n\nfunction ensureBifrostOptions(options: BifrostOptions): BifrostOptions {\n if (!options.baseUrl?.trim()) {\n throw new ValidationError('Bifrost baseUrl is required', {\n provider: 'bifrost',\n hint: 'Pass baseUrl like http://localhost:8080, http://localhost:8080/openai, or http://localhost:8080/v1 and optionally adminBaseUrl for governance endpoints',\n });\n }\n\n const baseUrl = normalizeGatewayBaseUrl(options.baseUrl);\n const inferenceBaseUrl = /\\/(?:openai|v1|pydanticai\\/v1)$/.test(baseUrl)\n ? baseUrl\n : `${baseUrl}/openai`;\n const headers = options.apiKey\n ? {\n ...options.headers,\n 'x-bf-vk': options.apiKey,\n 'x-api-key': options.apiKey,\n }\n : options.headers;\n\n return {\n ...options,\n baseUrl: inferenceBaseUrl,\n headers,\n };\n}\n\nconst BIFROST_PROFILE: OpenAICompatibleProfile = {\n providerLabel: 'Bifrost',\n providerName: 'bifrost',\n defaultModel: 'openai/gpt-4o-mini',\n capabilities: BIFROST_CAPABILITIES,\n describeModel: (modelId) => `Bifrost model: ${modelId}`,\n getContextLength: inferBifrostContextLength,\n getModelCapabilities: inferBifrostCapabilities,\n shouldIncludeModel: (modelId) => !isFilteredBifrostModel(modelId),\n supportsFunctions: inferBifrostFunctions,\n supportsVision: inferBifrostVision,\n};\n\ntype BifrostResolvedCapability =\n | 'chat'\n | 'vision'\n | 'embeddings'\n | 'image_generation';\n\nfunction supportsCapability(\n model: AIModel,\n capability: BifrostResolvedCapability,\n): boolean {\n if (capability === 'vision') {\n return (\n model.capabilities.includes('vision') || model.supportsVision === true\n );\n }\n\n if (capability === 'embeddings') {\n return model.capabilities.includes('embeddings');\n }\n\n if (capability === 'image_generation') {\n return model.capabilities.includes('image_generation');\n }\n\n return model.capabilities.includes('chat');\n}\n\n/**\n * Bifrost provider implementation.\n *\n * Bifrost exposes OpenAI-compatible inference and governance admin APIs. This\n * provider reuses the OpenAI-compatible transport for inference and exposes\n * `admin` for tenant project and virtual-key provisioning.\n */\nexport class BifrostProvider extends OpenAIProvider {\n readonly admin: BifrostAdmin;\n private readonly configuredDefaultModel?: string;\n private readonly resolvedModelCache = new Map<\n BifrostResolvedCapability,\n Promise<string>\n >();\n\n constructor(options: BifrostOptions) {\n const normalized = ensureBifrostOptions(options);\n super(normalized, BIFROST_PROFILE);\n const adminKey = normalized.adminApiKey || normalized.apiKey;\n const adminUsername = normalized.adminUser || normalized.adminUsername;\n this.admin = new BifrostAdmin({\n provider: 'bifrost',\n baseUrl: resolveGatewayAdminBaseUrl(\n normalized.baseUrl,\n normalized.adminUrl || normalized.adminBaseUrl,\n 'bifrost',\n ),\n apiKey: adminKey,\n username: adminUsername,\n password: normalized.adminPassword,\n headers:\n adminKey && !(adminUsername && normalized.adminPassword)\n ? {\n ...normalized.adminHeaders,\n 'x-bf-vk': adminKey,\n 'x-api-key': adminKey,\n }\n : normalized.adminHeaders,\n timeout: normalized.timeout,\n });\n this.configuredDefaultModel = normalized.defaultModel;\n }\n\n private async resolveModel(\n capability: BifrostResolvedCapability,\n explicitModel?: string,\n ): Promise<string> {\n if (explicitModel) {\n return explicitModel;\n }\n\n if (\n this.configuredDefaultModel &&\n (capability === 'chat' || capability === 'vision')\n ) {\n return this.configuredDefaultModel;\n }\n\n const cached = this.resolvedModelCache.get(capability);\n if (cached) {\n return cached;\n }\n\n const pending = this.selectModel(capability).catch((error) => {\n this.resolvedModelCache.delete(capability);\n throw error;\n });\n this.resolvedModelCache.set(capability, pending);\n return pending;\n }\n\n private async selectModel(\n capability: BifrostResolvedCapability,\n ): Promise<string> {\n const models = await super.getModels();\n const model = models.find((candidate) =>\n supportsCapability(candidate, capability),\n );\n\n if (!model) {\n throw new ValidationError(\n `No ${capability} model is available from the Bifrost gateway`,\n {\n provider: 'bifrost',\n capability,\n hint: 'Pass defaultModel explicitly or ensure the gateway returns a compatible model for this key',\n },\n );\n }\n\n return model.id;\n }\n\n async chat(\n messages: AIMessage[],\n options: ChatOptions = {},\n ): Promise<AIResponse> {\n const safe = normalizeChatOptions(\n this.options,\n options,\n 'bifrost',\n options.model || this.options.defaultModel,\n );\n return super.chat(messages, {\n ...safe,\n model: await this.resolveModel('chat', safe.model),\n });\n }\n\n async *stream(\n messages: AIMessage[],\n options: ChatOptions = {},\n ): AsyncIterable<string> {\n const safe = normalizeChatOptions(\n this.options,\n options,\n 'bifrost',\n options.model || this.options.defaultModel,\n );\n yield* super.stream(messages, {\n ...safe,\n model: await this.resolveModel('chat', safe.model),\n });\n }\n\n async describeImage(\n image: string | Buffer,\n prompt?: string,\n options: ImageDescriptionOptions = {},\n ): Promise<string> {\n const safe = normalizeChatOptions(\n this.options,\n options,\n 'bifrost',\n options.model || this.options.defaultModel,\n );\n return super.describeImage(image, prompt, {\n ...safe,\n model: await this.resolveModel('vision', safe.model),\n });\n }\n\n async embed(\n text: string | string[],\n options: EmbeddingOptions = {},\n ): Promise<EmbeddingResponse> {\n return super.embed(text, {\n ...options,\n model: await this.resolveModel('embeddings', options.model),\n });\n }\n\n async embedImage(\n image: string | Buffer,\n options: ImageEmbeddingOptions = {},\n ): Promise<EmbeddingResponse> {\n return super.embedImage(image, {\n ...options,\n model: await this.resolveModel('embeddings', options.model),\n });\n }\n\n async generateImage(\n prompt: string,\n options: ImageGenerationOptions = {},\n ): Promise<ImageGenerationResponse> {\n const safe = normalizeImageGenerationOptions(\n this.options,\n options,\n 'bifrost',\n options.model,\n );\n return super.generateImage(prompt, {\n ...safe,\n model: await this.resolveModel('image_generation', safe.model),\n });\n }\n}\n"],"mappings":";;;;;AA2BA,IAAM,uBAAuC;CAC3C,MAAM;CACN,YAAY;CACZ,YAAY;CACZ,WAAW;CACX,WAAW;CACX,QAAQ;CACR,YAAY;CACZ,iBAAiB;CACjB,iBAAiB;CACjB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ACxC,GAAG;GACH,OAAO,MAAM,KAAK,aAAa,UAAU,KAAK,KAAK;EACrD,CAAC;CACH;CAEA,MAAM,MACJ,MACA,UAA4B,CAAC,GACD;EAC5B,OAAO,MAAM,MAAM,MAAM;GACvB,GAAG;GACH,OAAO,MAAM,KAAK,aAAa,cAAc,QAAQ,KAAK;EAC5D,CAAC;CACH;CAEA,MAAM,WACJ,OACA,UAAiC,CAAC,GACN;EAC5B,OAAO,MAAM,WAAW,OAAO;GAC7B,GAAG;GACH,OAAO,MAAM,KAAK,aAAa,cAAc,QAAQ,KAAK;EAC5D,CAAC;CACH;CAEA,MAAM,cACJ,QACA,UAAkC,CAAC,GACD;EAClC,MAAM,OAAO,gCACX,KAAK,SACL,SACA,WACA,QAAQ,KACV;EACA,OAAO,MAAM,cAAc,QAAQ;GACjC,GAAG;GACH,OAAO,MAAM,KAAK,aAAa,oBAAoB,KAAK,KAAK;EAC/D,CAAC;CACH;AACF"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"byteplus-modelark-BIEXaTWw.js","names":[],"sources":["../../src/shared/providers/byteplus-modelark.ts"],"sourcesContent":["/**\n * BytePlus ModelArk (Seedance) video-generation provider implementation\n *\n * Raw-HTTP provider following the `qwen3-tts` precedent: no vendor SDK, just\n * the ModelArk video-generation task API (create task / poll / fetch /\n * cancel). This provider is video-generation only; it throws\n * `NOT_IMPLEMENTED` for chat, embeddings, and other text/TTS operations.\n *\n * The base URL, endpoint paths (`/contents/generations/tasks`), request/\n * response field names, and the `queued|running|cancelled|succeeded|failed|\n * expired` status enum were confirmed directly against BytePlus's own\n * published docs and blog examples at the time this was written (see the\n * link below). The `expired` status, the `page_num`/`page_size`/`filter.*`\n * list query params, and the DELETE-as-cancel-or-delete semantics\n * (`queued` → cancelled, `running` → rejected, terminal → deleted) are\n * corroborated from that same documentation but have not been exercised\n * against a live ModelArk account by this package's test suite — see\n * `packages/ai/src/byteplus-modelark.integration.test.ts` for an opt-in\n * check (skipped unless `MODELARK_API_KEY` is set) that exercises those\n * specifics against the real API.\n *\n * @see https://docs.byteplus.com/en/docs/ModelArk/Video_Generation_API\n */\n\nimport { extractRetryAfterSeconds } from '../rate-limit';\nimport { normalizeBaseAIOptions } from '../safety';\nimport type {\n AICapabilities,\n AIInterface,\n AIMessage,\n AIModel,\n AIResponse,\n ByteplusModelArkOptions,\n ChatOptions,\n CompletionOptions,\n EmbeddingOptions,\n EmbeddingResponse,\n ImageDescriptionOptions,\n ImageEmbeddingOptions,\n ImageGenerationOptions,\n ImageGenerationResponse,\n MessageOptions,\n TTSOptions,\n TTSResponse,\n VideoGenerationJob,\n VideoGenerationOptions,\n VideoGenerationReferenceImage,\n VideoGenerationResult,\n VideoGenerationStatusResult,\n Voice,\n VoiceCloneOptions,\n VoiceDesignOptions,\n VoiceListOptions,\n} from '../types';\nimport { AIError, AuthenticationError, RateLimitError } from '../types';\nimport { emitUsage } from './usage';\n\nconst DEFAULT_MODELARK_BASE_URL =\n 'https://ark.ap-southeast.bytepluses.com/api/v3';\nconst DEFAULT_MODELARK_MODEL = 'seedance-1-0-lite-t2v-250428';\nconst DEFAULT_MODELARK_I2V_MODEL = 'seedance-1-0-lite-i2v-250428';\n/** Burst capacity for the submit token bucket — see {@link SubmitRateLimiter}. */\nconst SUBMIT_BURST_CAPACITY = 2;\n\ntype ModelArkTaskStatus =\n | 'queued'\n | 'running'\n | 'cancelled'\n | 'succeeded'\n | 'failed'\n | 'expired';\n\ninterface ModelArkContentItem {\n type: 'text' | 'image_url';\n text?: string;\n image_url?: { url: string };\n role?: string;\n}\n\ninterface ModelArkCreateTaskRequest {\n model: string;\n content: ModelArkContentItem[];\n resolution?: string;\n ratio?: string;\n duration?: number;\n seed?: number;\n}\n\ninterface ModelArkCreateTaskResponse {\n id: string;\n}\n\ninterface ModelArkTaskResponse {\n id: string;\n model: string;\n status: ModelArkTaskStatus;\n content?: { video_url?: string; last_frame_url?: string };\n error?: { code: string; message: string } | null;\n created_at: number;\n updated_at: number;\n seed?: number;\n resolution?: string;\n ratio?: string;\n duration?: number;\n}\n\n/**\n * Resolve after `ms` milliseconds, or as soon as `signal` aborts —\n * whichever comes first. Never rejects; callers re-check `signal.aborted`\n * themselves immediately after, so a single check covers both \"timed out\n * normally\" and \"woke up early because of an abort.\"\n */\nfunction delayOrAbort(ms: number, signal?: AbortSignal): Promise<void> {\n return new Promise((resolve) => {\n if (signal?.aborted) {\n resolve();\n return;\n }\n const timer = setTimeout(resolve, ms);\n signal?.addEventListener(\n 'abort',\n () => {\n clearTimeout(timer);\n resolve();\n },\n { once: true },\n );\n });\n}\n\n/**\n * Local token-bucket + concurrency limiter for submit-time throttling.\n *\n * ModelArk documents Seedance account limits of QPS 2 / 3 *concurrent\n * submissions*; this only gates `submitVideoGenerationJob` (task creation\n * HTTP calls), matching the \"submit-time throttling\" requirement — polling\n * and cancellation are not subject to the same account-level task-creation\n * limits. `maxConcurrent` bounds concurrent in-flight *submit HTTP\n * requests*, not the lifetime of the provider-side render tasks those\n * submissions create — this limiter has no way to observe when a task\n * actually finishes rendering on ModelArk's infrastructure, so\n * task-lifetime concurrency is not, and cannot be, tracked here.\n *\n * The bucket's burst capacity is capped at {@link SUBMIT_BURST_CAPACITY}\n * (independent of `requestsPerMinute`) so the bucket doesn't start full —\n * without this cap, a bucket sized to `requestsPerMinute` (e.g. 120) would\n * start with 120 tokens and let the first 120 submits through instantly,\n * defeating the QPS limit entirely.\n */\nclass SubmitRateLimiter {\n private tokens: number;\n private lastRefill: number;\n private readonly maxTokens: number;\n private readonly refillRate: number; // tokens per second\n private activeRequests = 0;\n private readonly maxConcurrent: number;\n\n constructor(options: { requestsPerMinute?: number; maxConcurrent?: number }) {\n const requestsPerMinute = options.requestsPerMinute ?? 120; // ~QPS 2\n this.refillRate = requestsPerMinute / 60;\n this.maxTokens = SUBMIT_BURST_CAPACITY;\n this.tokens = this.maxTokens;\n this.lastRefill = Date.now();\n this.maxConcurrent = options.maxConcurrent ?? 3;\n }\n\n /**\n * Acquire a submit slot + token, honoring `signal` throughout the wait.\n *\n * The concurrency slot and the token are checked and committed together\n * in one synchronous section (no `await` between the check and the\n * `tokens -=`/`activeRequests +=` mutations below), so a waiter can never\n * observe a stale \"slot available\" snapshot from before another waiter's\n * own commit — closing the TOCTOU window where two callers could both\n * pass the checks and push `activeRequests` past `maxConcurrent`.\n *\n * An aborted `signal` — whether already aborted at entry or aborted\n * while waiting for a slot/token — throws before any token is consumed\n * or `activeRequests` is incremented, so an aborted submission never\n * silently proceeds to create a billable job.\n */\n async acquire(signal?: AbortSignal): Promise<void> {\n const throwIfAborted = () => {\n if (signal?.aborted) {\n throw new AIError(\n 'ModelArk submission aborted by caller while waiting for the rate limiter',\n 'AI_ABORTED',\n 'byteplus-modelark',\n );\n }\n };\n\n throwIfAborted();\n\n const now = Date.now();\n const elapsed = (now - this.lastRefill) / 1000;\n this.tokens = Math.min(\n this.maxTokens,\n this.tokens + elapsed * this.refillRate,\n );\n this.lastRefill = now;\n\n for (;;) {\n throwIfAborted();\n\n if (this.activeRequests < this.maxConcurrent && this.tokens >= 1) {\n this.tokens -= 1;\n this.activeRequests += 1;\n return;\n }\n\n if (this.activeRequests >= this.maxConcurrent) {\n await delayOrAbort(100, signal);\n } else {\n const waitTime = Math.max(\n 1,\n ((1 - this.tokens) / this.refillRate) * 1000,\n );\n await delayOrAbort(waitTime, signal);\n const elapsedNow = (Date.now() - this.lastRefill) / 1000;\n this.tokens = Math.min(\n this.maxTokens,\n this.tokens + elapsedNow * this.refillRate,\n );\n this.lastRefill = Date.now();\n }\n }\n }\n\n release(): void {\n this.activeRequests = Math.max(0, this.activeRequests - 1);\n }\n}\n\n/**\n * FNV-1a hash, used only to avoid retaining raw API keys as in-memory Map\n * keys in {@link sharedSubmitLimiters}. Not cryptographic — collisions just\n * mean two distinct keys share a rate-limit bucket, which is a pacing\n * inefficiency, not a security issue.\n */\nfunction hashKey(value: string): string {\n let hash = 2166136261;\n for (let index = 0; index < value.length; index += 1) {\n hash ^= value.charCodeAt(index);\n hash = Math.imul(hash, 16777619);\n }\n return (hash >>> 0).toString(36);\n}\n\n/**\n * Submit-time limiters keyed by credential/config, shared module-wide.\n *\n * Provider instances are frequently constructed per pipeline step (a fresh\n * `getAI({ type: 'byteplus-modelark', ... })` per job), so a limiter owned\n * by a single provider instance would reset on every construction and never\n * actually enforce the account-wide QPS/concurrency limits. Keying by the\n * resolved credentials (or an explicit `rateLimit.key`) lets independently\n * constructed instances that target the same ModelArk account share one\n * bucket, mirroring how {@link createRateLimitedAI}'s budget coordinators\n * are shared by `rateLimit.key`.\n */\nconst sharedSubmitLimiters = new Map<string, SubmitRateLimiter>();\n\nfunction getSharedSubmitLimiter(\n options: ByteplusModelArkOptions,\n): SubmitRateLimiter {\n const key =\n options.rateLimit?.key?.trim() ||\n `byteplus-modelark:${hashKey(options.apiKey || 'default')}`;\n\n let limiter = sharedSubmitLimiters.get(key);\n if (!limiter) {\n limiter = new SubmitRateLimiter({\n requestsPerMinute: options.rateLimit?.requestsPerMinute,\n maxConcurrent: options.rateLimit?.maxConcurrent,\n });\n sharedSubmitLimiters.set(key, limiter);\n }\n return limiter;\n}\n\n/** @internal Test-only: clear shared submit limiters between test cases. */\nexport function __resetByteplusModelArkRateLimitersForTests(): void {\n sharedSubmitLimiters.clear();\n}\n\n/**\n * BytePlus ModelArk provider implementation for asynchronous video\n * generation (Seedance). Focuses on video-generation capabilities and\n * throws `NOT_IMPLEMENTED` for text generation methods.\n *\n * @example\n * ```typescript\n * import { getAI } from '@happyvertical/ai';\n *\n * const modelark = await getAI({\n * type: 'byteplus-modelark',\n * apiKey: process.env.MODELARK_API_KEY!,\n * });\n *\n * const job = await modelark.submitVideoGenerationJob({\n * prompt: 'A drone shot flying over a coastal cliff at sunrise',\n * durationSeconds: 5,\n * resolution: '720p',\n * aspectRatio: '16:9',\n * });\n *\n * // Persist `job`, poll later (even after a restart):\n * const status = await modelark.getVideoGenerationJob(job);\n * ```\n */\nexport class ByteplusModelArkProvider implements AIInterface {\n private options: ByteplusModelArkOptions;\n private readonly submitLimiter: SubmitRateLimiter;\n\n constructor(options: ByteplusModelArkOptions) {\n // Note: deliberately no `defaultModel` fallback baked in here — the\n // model-selection logic in submitVideoGenerationJob picks between the\n // t2v and i2v defaults based on whether referenceImages are present,\n // and an unconditional default here would always shadow that.\n this.options = normalizeBaseAIOptions({ ...options });\n\n this.submitLimiter = getSharedSubmitLimiter(this.options);\n }\n\n private get baseUrl(): string {\n return (this.options.baseUrl || DEFAULT_MODELARK_BASE_URL).replace(\n /\\/+$/,\n '',\n );\n }\n\n private async request<T>(\n path: string,\n options: {\n method?: 'GET' | 'POST' | 'DELETE';\n body?: unknown;\n query?: Record<string, string | number>;\n signal?: AbortSignal;\n timeout?: number;\n } = {},\n ): Promise<T> {\n if (options.signal?.aborted) {\n throw new AIError(\n 'ModelArk request aborted by caller',\n 'AI_ABORTED',\n 'byteplus-modelark',\n );\n }\n\n const url = new URL(`${this.baseUrl}${path}`);\n for (const [key, value] of Object.entries(options.query ?? {})) {\n url.searchParams.set(key, String(value));\n }\n\n const controller = new AbortController();\n const timeoutMs = options.timeout ?? this.options.timeout;\n let timedOut = false;\n const timer = timeoutMs\n ? setTimeout(() => {\n timedOut = true;\n controller.abort();\n }, timeoutMs)\n : undefined;\n // Mirrors `prepareRequestControls`'s pattern (packages/ai/src/shared/safety.ts):\n // addEventListener on an already-aborted signal never fires, so an\n // already-aborted `signal` must invoke the abort immediately instead of\n // only listening for a future 'abort' event.\n const onExternalAbort = () => controller.abort(options.signal?.reason);\n if (options.signal?.aborted) {\n onExternalAbort();\n } else {\n options.signal?.addEventListener('abort', onExternalAbort, {\n once: true,\n });\n }\n\n try {\n let response: Response;\n try {\n response = await fetch(url, {\n method: options.method || 'GET',\n headers: {\n 'Content-Type': 'application/json',\n Authorization: `Bearer ${this.options.apiKey ?? ''}`,\n ...(this.options.headers || {}),\n },\n body: options.body ? JSON.stringify(options.body) : undefined,\n signal: controller.signal,\n });\n } catch (error) {\n // The fetch() call itself failed (network error, DNS failure,\n // timeout/abort) — wrap it into the AIError taxonomy so callers get\n // a consistent, retryable-vs-terminal-distinguishable error rather\n // than a raw TypeError/DOMException.\n if (timedOut) {\n throw new AIError(\n `ModelArk request to ${path} timed out after ${timeoutMs}ms`,\n 'AI_TIMEOUT',\n 'byteplus-modelark',\n undefined,\n true,\n );\n }\n if (options.signal?.aborted) {\n throw new AIError(\n 'ModelArk request aborted by caller',\n 'AI_ABORTED',\n 'byteplus-modelark',\n );\n }\n throw new AIError(\n `Network error calling ModelArk ${path}: ${\n error instanceof Error ? error.message : String(error)\n }`,\n 'NETWORK_ERROR',\n 'byteplus-modelark',\n undefined,\n true,\n );\n }\n\n if (!response.ok) {\n const errorText = await response.text();\n\n if (response.status === 401 || response.status === 403) {\n throw new AuthenticationError('byteplus-modelark');\n }\n if (response.status === 429) {\n throw new RateLimitError(\n 'byteplus-modelark',\n extractRetryAfterSeconds({ headers: response.headers }),\n );\n }\n\n throw new AIError(\n `ModelArk request to ${path} failed: ${response.status} ${errorText}`,\n 'MODELARK_REQUEST_FAILED',\n 'byteplus-modelark',\n );\n }\n\n if (response.status === 204) {\n return {} as T;\n }\n\n const text = await response.text();\n return text ? (JSON.parse(text) as T) : ({} as T);\n } finally {\n if (timer) clearTimeout(timer);\n options.signal?.removeEventListener('abort', onExternalAbort);\n }\n }\n\n private toImageUrl(reference: VideoGenerationReferenceImage): string {\n if (typeof reference.image === 'string') {\n return reference.image;\n }\n const mimeType = reference.mimeType || 'image/png';\n return `data:${mimeType};base64,${reference.image.toString('base64')}`;\n }\n\n // ============================================================================\n // Video Generation Methods (Implemented)\n // ============================================================================\n\n /**\n * Submit an asynchronous Seedance video-generation task.\n *\n * Submission is locally throttled to approximate ModelArk's documented\n * Seedance account limits (QPS 2, capped at {@link SUBMIT_BURST_CAPACITY}\n * burst / 3 concurrent *submissions* — task-lifetime concurrency on\n * ModelArk's infrastructure is not tracked; see {@link SubmitRateLimiter}).\n *\n * When `referenceImages` are supplied and no explicit `model` is chosen,\n * this selects the image-to-video default model rather than silently\n * generating text-to-video with the image ignored.\n */\n async submitVideoGenerationJob(\n options: VideoGenerationOptions,\n ): Promise<VideoGenerationJob> {\n const startTime = Date.now();\n await this.submitLimiter.acquire(options.signal);\n\n try {\n const hasReferenceImages = (options.referenceImages?.length ?? 0) > 0;\n const model =\n options.model ||\n this.options.defaultModel ||\n (hasReferenceImages\n ? DEFAULT_MODELARK_I2V_MODEL\n : DEFAULT_MODELARK_MODEL);\n\n const content: ModelArkContentItem[] = [];\n if (options.prompt) {\n content.push({ type: 'text', text: options.prompt });\n }\n for (const reference of options.referenceImages ?? []) {\n content.push({\n type: 'image_url',\n image_url: { url: this.toImageUrl(reference) },\n role: reference.role || 'reference_image',\n });\n }\n\n if (content.length === 0) {\n throw new AIError(\n 'submitVideoGenerationJob requires a prompt or at least one reference image',\n 'INVALID_INPUT',\n 'byteplus-modelark',\n );\n }\n\n const body: ModelArkCreateTaskRequest = {\n model,\n content,\n resolution: options.resolution,\n ratio: options.aspectRatio,\n duration: options.durationSeconds,\n seed: options.seed,\n };\n // Note: ModelArk's documented create-task schema has no negative_prompt\n // or fps fields, so those VideoGenerationOptions are intentionally not\n // forwarded for this provider.\n\n const response = await this.request<ModelArkCreateTaskResponse>(\n '/contents/generations/tasks',\n {\n method: 'POST',\n body,\n signal: options.signal,\n timeout: options.timeout,\n },\n );\n\n emitUsage(\n this.options,\n 'byteplus-modelark',\n 'submitVideoGenerationJob',\n model,\n undefined,\n startTime,\n {\n ...(options.durationSeconds !== undefined\n ? { durationSeconds: String(options.durationSeconds) }\n : {}),\n ...(options.resolution ? { resolution: options.resolution } : {}),\n ...(options.aspectRatio ? { aspectRatio: options.aspectRatio } : {}),\n ...options.usageTags,\n },\n );\n\n return {\n jobId: response.id,\n provider: 'byteplus-modelark',\n model,\n createdAt: new Date().toISOString(),\n };\n } finally {\n this.submitLimiter.release();\n }\n }\n\n /**\n * Poll the status of a ModelArk video-generation task.\n */\n async getVideoGenerationJob(\n handle: VideoGenerationJob,\n ): Promise<VideoGenerationStatusResult> {\n const task = await this.request<ModelArkTaskResponse>(\n `/contents/generations/tasks/${handle.jobId}`,\n );\n return this.mapTask(task);\n }\n\n /**\n * Fetch the result of a completed ModelArk video-generation task.\n *\n * @throws {AIError} When the job has not succeeded yet\n */\n async fetchVideoGenerationResult(\n handle: VideoGenerationJob,\n ): Promise<VideoGenerationResult> {\n const status = await this.getVideoGenerationJob(handle);\n if (status.status !== 'succeeded' || !status.result) {\n throw new AIError(\n `ModelArk video-generation job ${handle.jobId} has not succeeded (status: ${status.status})`,\n 'VIDEO_JOB_NOT_READY',\n 'byteplus-modelark',\n handle.model,\n );\n }\n return status.result;\n }\n\n /**\n * Cancel a ModelArk video-generation task.\n *\n * ModelArk's DELETE endpoint only transitions a `queued` task to\n * `cancelled`; a task that is already `running` cannot be cancelled and\n * the request fails with an {@link AIError} describing why. Terminal\n * tasks (`succeeded`/`failed`/`expired`) are deleted instead.\n */\n async cancelVideoGenerationJob(handle: VideoGenerationJob): Promise<void> {\n await this.request(`/contents/generations/tasks/${handle.jobId}`, {\n method: 'DELETE',\n });\n }\n\n /**\n * Cheap auth-shaped check for ModelArk access: lists tasks with a page\n * size of 1. Callers on a hot path must cache the result themselves.\n */\n async validateVideoGenerationAccess(): Promise<boolean> {\n await this.request('/contents/generations/tasks', {\n query: { page_size: 1 },\n });\n return true;\n }\n\n private mapTask(task: ModelArkTaskResponse): VideoGenerationStatusResult {\n switch (task.status) {\n case 'queued':\n return { status: 'queued' };\n case 'running':\n return { status: 'running' };\n case 'cancelled':\n return { status: 'cancelled' };\n case 'expired':\n return {\n status: 'failed',\n error: 'ModelArk task expired before the result was retrieved',\n };\n case 'failed':\n return {\n status: 'failed',\n error: task.error?.message || 'ModelArk video generation failed',\n };\n case 'succeeded': {\n if (!task.content?.video_url) {\n return {\n status: 'failed',\n error: 'ModelArk reported success with no video_url',\n };\n }\n return {\n status: 'succeeded',\n result: {\n url: task.content.video_url,\n mimeType: 'video/mp4',\n durationSeconds: task.duration,\n },\n };\n }\n default:\n // Never invent a terminal state for a status this package doesn't\n // recognize (e.g. a future ModelArk status addition) — treat it as\n // still in flight so a caller doesn't misread a mid-render job as\n // failed, resubmit, and double-bill. The raw value is preserved for\n // diagnostics.\n return { status: 'running', rawStatus: task.status };\n }\n }\n\n async getModels(): Promise<AIModel[]> {\n return [\n {\n id: 'seedance-1-0-lite-t2v-250428',\n name: 'Seedance 1.0 Lite (text-to-video)',\n description: 'ModelArk Seedance 1.0 Lite text-to-video model',\n contextLength: 0,\n capabilities: ['video_generation'],\n supportsFunctions: false,\n supportsVision: false,\n },\n {\n id: 'seedance-1-0-lite-i2v-250428',\n name: 'Seedance 1.0 Lite (image-to-video)',\n description: 'ModelArk Seedance 1.0 Lite image-to-video model',\n contextLength: 0,\n capabilities: ['video_generation'],\n supportsFunctions: false,\n supportsVision: true,\n },\n ];\n }\n\n async getCapabilities(): Promise<AICapabilities> {\n return {\n chat: false,\n completion: false,\n embeddings: false,\n streaming: false,\n functions: false,\n vision: false,\n fineTuning: false,\n imageEmbeddings: false,\n imageGeneration: false,\n videoGeneration: true,\n tts: false,\n voiceCloning: false,\n voiceDesign: false,\n maxContextLength: 0,\n supportedOperations: [\n 'submitVideoGenerationJob',\n 'getVideoGenerationJob',\n 'fetchVideoGenerationResult',\n 'cancelVideoGenerationJob',\n ],\n };\n }\n\n // ============================================================================\n // Text Generation / TTS Methods (Not Implemented - Use another provider)\n // ============================================================================\n\n async chat(\n _messages: AIMessage[],\n _options?: ChatOptions,\n ): Promise<AIResponse> {\n throw new AIError(\n 'Chat is not supported by the BytePlus ModelArk provider. Use OpenAI, Anthropic, or another provider for text generation.',\n 'NOT_IMPLEMENTED',\n 'byteplus-modelark',\n );\n }\n\n async complete(\n _prompt: string,\n _options?: CompletionOptions,\n ): Promise<AIResponse> {\n throw new AIError(\n 'Complete is not supported by the BytePlus ModelArk provider.',\n 'NOT_IMPLEMENTED',\n 'byteplus-modelark',\n );\n }\n\n async message(_text: string, _options?: MessageOptions): Promise<string> {\n throw new AIError(\n 'Message is not supported by the BytePlus ModelArk provider.',\n 'NOT_IMPLEMENTED',\n 'byteplus-modelark',\n );\n }\n\n async embed(\n _text: string | string[],\n _options?: EmbeddingOptions,\n ): Promise<EmbeddingResponse> {\n throw new AIError(\n 'Embeddings are not supported by the BytePlus ModelArk provider.',\n 'NOT_IMPLEMENTED',\n 'byteplus-modelark',\n );\n }\n\n async embedImage(\n _image: string | Buffer,\n _options?: ImageEmbeddingOptions,\n ): Promise<EmbeddingResponse> {\n throw new AIError(\n 'Image embeddings are not supported by the BytePlus ModelArk provider.',\n 'NOT_IMPLEMENTED',\n 'byteplus-modelark',\n );\n }\n\n async describeImage(\n _image: string | Buffer,\n _prompt?: string,\n _options?: ImageDescriptionOptions,\n ): Promise<string> {\n throw new AIError(\n 'Image description is not supported by the BytePlus ModelArk provider.',\n 'NOT_IMPLEMENTED',\n 'byteplus-modelark',\n );\n }\n\n async generateImage(\n _prompt: string,\n _options?: ImageGenerationOptions,\n ): Promise<ImageGenerationResponse> {\n throw new AIError(\n 'Synchronous image generation is not yet implemented by the BytePlus ModelArk provider.',\n 'NOT_IMPLEMENTED',\n 'byteplus-modelark',\n );\n }\n\n stream(\n _messages: AIMessage[],\n _options?: ChatOptions,\n ): AsyncIterable<string> {\n const error = new AIError(\n 'Chat streaming is not supported by the BytePlus ModelArk provider.',\n 'NOT_IMPLEMENTED',\n 'byteplus-modelark',\n );\n return {\n [Symbol.asyncIterator]: () => ({\n next: () => Promise.reject(error),\n }),\n };\n }\n\n async countTokens(_text: string): Promise<number> {\n throw new AIError(\n 'Token counting is not supported by the BytePlus ModelArk provider.',\n 'NOT_IMPLEMENTED',\n 'byteplus-modelark',\n );\n }\n\n async synthesizeSpeech(\n _text: string,\n _options?: TTSOptions,\n ): Promise<TTSResponse> {\n throw new AIError(\n 'TTS is not supported by the BytePlus ModelArk provider. Use Qwen3-TTS provider.',\n 'NOT_IMPLEMENTED',\n 'byteplus-modelark',\n );\n }\n\n streamSpeech(_text: string, _options?: TTSOptions): AsyncIterable<Buffer> {\n const error = new AIError(\n 'TTS streaming is not supported by the BytePlus ModelArk provider. Use Qwen3-TTS provider.',\n 'NOT_IMPLEMENTED',\n 'byteplus-modelark',\n );\n return {\n [Symbol.asyncIterator]: () => ({\n next: () => Promise.reject(error),\n }),\n };\n }\n\n async cloneVoice(_options: VoiceCloneOptions): Promise<Voice> {\n throw new AIError(\n 'Voice cloning is not supported by the BytePlus ModelArk provider. Use Qwen3-TTS provider.',\n 'NOT_IMPLEMENTED',\n 'byteplus-modelark',\n );\n }\n\n async designVoice(_options: VoiceDesignOptions): Promise<Voice> {\n throw new AIError(\n 'Voice design is not supported by the BytePlus ModelArk provider. Use Qwen3-TTS provider.',\n 'NOT_IMPLEMENTED',\n 'byteplus-modelark',\n );\n }\n\n async getVoices(_options?: VoiceListOptions): Promise<Voice[]> {\n throw new AIError(\n 'Voice listing is not supported by the BytePlus ModelArk provider. Use Qwen3-TTS provider.',\n 'NOT_IMPLEMENTED',\n 'byteplus-modelark',\n );\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAyDA,IAAM,4BACJ;AACF,IAAM,yBAAyB;AAC/B,IAAM,6BAA6B;;AAEnC,IAAM,wBAAwB;;;;;;;AAkD9B,SAAS,aAAa,IAAY,QAAqC;CACrE,OAAO,IAAI,SAAS,YAAY;EAC9B,IAAI,QAAQ,SAAS;GACnB,QAAQ;GACR;EACF;EACA,MAAM,QAAQ,WAAW,SAAS,EAAE;EACpC,QAAQ,iBACN,eACM;GACJ,aAAa,KAAK;GAClB,QAAQ;EACV,GACA,EAAE,MAAM,KAAK,CACf;CACF,CAAC;AACH;;;;;;;;;;;;;;;;;;;;AAqBA,IAAM,oBAAN,MAAwB;CACtB;CACA;CACA;CACA;CACA,iBAAyB;CACzB;CAEA,YAAY,SAAiE;EAC3E,MAAM,oBAAoB,QAAQ,qBAAqB;EACvD,KA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,mBACA,mBACF;CACF;CAEA,MAAM,iBACJ,OACA,UACsB;EACtB,MAAM,IAAI,QACR,mFACA,mBACA,mBACF;CACF;CAEA,aAAa,OAAe,UAA8C;EACxE,MAAM,QAAQ,IAAI,QAChB,6FACA,mBACA,mBACF;EACA,OAAO,GACJ,OAAO,uBAAuB,EAC7B,YAAY,QAAQ,OAAO,KAAK,EAClC,GACF;CACF;CAEA,MAAM,WAAW,UAA6C;EAC5D,MAAM,IAAI,QACR,6FACA,mBACA,mBACF;CACF;CAEA,MAAM,YAAY,UAA8C;EAC9D,MAAM,IAAI,QACR,4FACA,mBACA,mBACF;CACF;CAEA,MAAM,UAAU,UAA+C;EAC7D,MAAM,IAAI,QACR,6FACA,mBACA,mBACF;CACF;AACF"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"claude-cli-Chfnckyj.js","names":[],"sources":["../../src/shared/providers/claude-cli.ts"],"sourcesContent":["/**\n * Claude CLI provider implementation\n *\n * Provides a standardized interface for interacting with Claude via the Claude Code CLI,\n * enabling users with Claude Max subscriptions to use their subscription instead of\n * paying for API usage. Supports chat completions and streaming responses.\n * Authentication is handled via existing Claude session or setup-token.\n *\n * **Authentication Priority:**\n * 1. ANTHROPIC_API_KEY environment variable (uses Anthropic SDK, no keychain prompts)\n * 2. Claude CLI with setup-token (for CI/CD, no keychain prompts)\n * 3. Claude CLI with keychain (may prompt for password on macOS)\n */\n\nimport { exec, spawn } from 'node:child_process';\nimport { promisify } from 'node:util';\n\nimport type {\n AICapabilities,\n AIInterface,\n AIMessage,\n AIModel,\n AIResponse,\n ChatOptions,\n ClaudeCliOptions,\n CompletionOptions,\n EmbeddingOptions,\n EmbeddingResponse,\n ImageDescriptionOptions,\n ImageEmbeddingOptions,\n ImageGenerationOptions,\n ImageGenerationResponse,\n MessageOptions,\n TokenUsage,\n TTSOptions,\n TTSResponse,\n VideoGenerationJob,\n VideoGenerationOptions,\n VideoGenerationResult,\n VideoGenerationStatusResult,\n Voice,\n VoiceCloneOptions,\n VoiceDesignOptions,\n VoiceListOptions,\n} from '../types';\nimport {\n AIError,\n AuthenticationError,\n ContextLengthError,\n extractTextContent,\n RateLimitError,\n} from '../types';\nimport { emitUsage } from './usage';\n\nconst execAsync = promisify(exec);\n\n/**\n * Claude CLI provider implementation that shells out to the Claude Code CLI.\n * Supports chat completions, streaming, and leverages Claude Max subscription.\n * Does not support embeddings (use OpenAI or another provider for embeddings).\n *\n * If ANTHROPIC_API_KEY environment variable is set, automatically falls back to\n * using the Anthropic provider to avoid macOS keychain password prompts.\n */\nexport class ClaudeCliProvider implements AIInterface {\n private options: ClaudeCliOptions;\n private cliPath: string | null = null;\n private anthropicFallback: AIInterface | null = null;\n\n /**\n * Creates a new Claude CLI provider instance\n * @param options - Configuration options for the Claude CLI provider\n */\n constructor(options: ClaudeCliOptions) {\n this.options = {\n defaultModel: 'sonnet',\n ...options,\n };\n }\n\n /**\n * Checks if ANTHROPIC_API_KEY is available and initializes fallback provider if needed\n * @private\n */\n private async initializeFallback(): Promise<void> {\n if (this.anthropicFallback !== null) {\n return; // Already initialized\n }\n\n const apiKey = process.env.ANTHROPIC_API_KEY;\n if (!apiKey) {\n return; // No API key, will use CLI\n }\n\n // Lazy load Anthropic provider to avoid circular dependency\n const { AnthropicProvider } = await import('./anthropic.js');\n\n // Map claude-cli model names to Anthropic model IDs\n const modelMap: Record<string, string> = {\n sonnet: 'claude-3-5-sonnet-20241022',\n opus: 'claude-3-opus-20240229',\n haiku: 'claude-3-haiku-20240307',\n };\n\n const defaultModel =\n modelMap[this.options.defaultModel || 'sonnet'] ||\n 'claude-3-5-sonnet-20241022';\n\n this.anthropicFallback = new AnthropicProvider({\n type: 'anthropic',\n apiKey,\n defaultModel,\n onUsage: this.options.onUsage,\n usageTags: this.options.usageTags,\n });\n }\n\n /**\n * Finds the Claude CLI binary in PATH or uses custom cliPath\n * Does NOT execute the CLI during detection to avoid keychain prompts\n * @throws {AIError} When CLI cannot be found\n * @private\n */\n private async findCli(): Promise<string> {\n if (this.cliPath) {\n return this.cliPath;\n }\n\n // If custom path provided, check if it exists (don't execute it)\n if (this.options.cliPath) {\n const fs = await import('node:fs/promises');\n try {\n await fs.access(this.options.cliPath);\n this.cliPath = this.options.cliPath;\n return this.cliPath;\n } catch (_error) {\n throw new AIError(\n `Claude CLI not found at specified path: ${this.options.cliPath}. Set ANTHROPIC_API_KEY environment variable to avoid CLI dependency.`,\n 'CLI_NOT_FOUND',\n 'claude-cli',\n );\n }\n }\n\n // Try common installation locations first\n const commonPaths = [\n `${process.env.HOME}/.claude/local/claude`, // Default installation path\n '/usr/local/bin/claude',\n '/opt/homebrew/bin/claude',\n ];\n\n const fs = await import('node:fs/promises');\n for (const path of commonPaths) {\n try {\n await fs.access(path);\n this.cliPath = path;\n return this.cliPath;\n } catch (_error) {\n // Try next path\n }\n }\n\n // Try to find in PATH with which (doesn't execute the binary)\n try {\n const { stdout } = await execAsync('which claude');\n const path = stdout.trim();\n if (path) {\n this.cliPath = path;\n return this.cliPath;\n }\n } catch (_error) {\n // Not found\n }\n\n throw new AIError(\n 'Claude CLI not found. Set ANTHROPIC_API_KEY environment variable to use Anthropic SDK instead, or install Claude Code CLI. Visit https://docs.claude.com/en/docs/claude-code/ for installation instructions.',\n 'CLI_NOT_FOUND',\n 'claude-cli',\n );\n }\n\n /**\n * Normalizes model name to full model ID or short name\n * @private\n */\n private normalizeModel(model?: string): string {\n const m = model || this.options.defaultModel || 'sonnet';\n\n // Map short names to CLI-compatible values\n const modelMap: Record<string, string> = {\n sonnet: 'sonnet',\n opus: 'opus',\n haiku: 'haiku',\n };\n\n return modelMap[m] || m;\n }\n\n /**\n * Execute Claude CLI command and return parsed JSON output\n * @private\n */\n private async executeCommand(\n prompt: string,\n options: {\n model?: string;\n systemPrompt?: string;\n temperature?: number;\n maxTokens?: number;\n } = {},\n ): Promise<any> {\n const cliPath = await this.findCli();\n const model = this.normalizeModel(options.model);\n\n const args = ['--print', '--output-format', 'json', '--model', model];\n\n if (options.systemPrompt) {\n args.push('--system-prompt', options.systemPrompt);\n }\n\n args.push(prompt);\n\n return new Promise((resolve, reject) => {\n let stdout = '';\n let stderr = '';\n\n const child = spawn(cliPath, args, {\n stdio: ['ignore', 'pipe', 'pipe'],\n });\n\n child.stdout.on('data', (data) => {\n stdout += data.toString();\n });\n\n child.stderr.on('data', (data) => {\n stderr += data.toString();\n });\n\n child.on('error', (error) => {\n reject(\n new AIError(\n `Failed to execute Claude CLI: ${error.message}`,\n 'EXECUTION_ERROR',\n 'claude-cli',\n ),\n );\n });\n\n child.on('close', (code) => {\n if (code !== 0) {\n reject(this.mapCliError(stderr, code || undefined));\n return;\n }\n\n try {\n // Parse JSON output\n const result = JSON.parse(stdout);\n resolve(result);\n } catch (_error) {\n reject(\n new AIError(\n 'Failed to parse CLI output as JSON',\n 'PARSE_ERROR',\n 'claude-cli',\n ),\n );\n }\n });\n });\n }\n\n /**\n * Execute Claude CLI command with streaming output\n * @private\n */\n private async *executeStreamingCommand(\n prompt: string,\n options: {\n model?: string;\n systemPrompt?: string;\n onProgress?: (chunk: string) => void;\n } = {},\n ): AsyncIterable<string> {\n const cliPath = await this.findCli();\n const model = this.normalizeModel(options.model);\n\n const args = [\n '--print',\n '--output-format',\n 'stream-json',\n '--verbose', // Required for stream-json format\n '--model',\n model,\n ];\n\n if (options.systemPrompt) {\n args.push('--system-prompt', options.systemPrompt);\n }\n\n args.push(prompt);\n\n const child = spawn(cliPath, args, {\n stdio: ['ignore', 'pipe', 'pipe'],\n });\n\n let stderr = '';\n\n child.stderr.on('data', (data) => {\n stderr += data.toString();\n });\n\n // Process streaming JSON chunks\n let buffer = '';\n\n for await (const chunk of child.stdout) {\n buffer += chunk.toString();\n\n // Process complete JSON objects (one per line)\n const lines = buffer.split('\\n');\n buffer = lines.pop() || ''; // Keep incomplete line in buffer\n\n for (const line of lines) {\n if (line.trim()) {\n try {\n const parsed = JSON.parse(line);\n // Claude CLI stream-json format outputs complete messages\n // Look for assistant message with content\n if (parsed.type === 'assistant' && parsed.message?.content) {\n for (const contentBlock of parsed.message.content) {\n if (contentBlock.type === 'text' && contentBlock.text) {\n const text = contentBlock.text;\n if (options.onProgress) {\n options.onProgress(text);\n }\n yield text;\n }\n }\n }\n // Also handle result type for final output\n else if (parsed.type === 'result' && parsed.result) {\n const text = parsed.result;\n if (options.onProgress) {\n options.onProgress(text);\n }\n yield text;\n }\n } catch (_error) {\n // Skip malformed JSON lines\n }\n }\n }\n }\n\n // Wait for process to complete\n const exitCode = await new Promise<number>((resolve) => {\n child.on('close', (code) => resolve(code || 0));\n });\n\n if (exitCode !== 0) {\n throw this.mapCliError(stderr, exitCode);\n }\n }\n\n /**\n * Maps messages to a single prompt for CLI\n * @private\n */\n private mapMessagesToPrompt(messages: AIMessage[]): {\n prompt: string;\n systemPrompt?: string;\n } {\n let systemPrompt: string | undefined;\n const conversationParts: string[] = [];\n\n for (const message of messages) {\n const textContent = extractTextContent(message.content);\n if (message.role === 'system') {\n systemPrompt = systemPrompt\n ? `${systemPrompt}\\n\\n${textContent}`\n : textContent;\n } else if (message.role === 'user') {\n conversationParts.push(`User: ${textContent}`);\n } else if (message.role === 'assistant') {\n conversationParts.push(`Assistant: ${textContent}`);\n }\n }\n\n // If only one user message and no conversation, return it directly\n if (\n conversationParts.length === 1 &&\n conversationParts[0].startsWith('User: ')\n ) {\n return {\n prompt: conversationParts[0].substring(6), // Remove \"User: \" prefix\n systemPrompt,\n };\n }\n\n return {\n prompt: conversationParts.join('\\n\\n'),\n systemPrompt,\n };\n }\n\n /**\n * Maps CLI errors to standardized error types\n * @private\n */\n\n private mapCliError(stderr: string, exitCode?: number): AIError {\n const errorText = stderr.toLowerCase();\n\n // Authentication errors\n if (\n errorText.includes('not authenticated') ||\n errorText.includes('login required')\n ) {\n return new AuthenticationError('claude-cli');\n }\n\n // Rate limiting\n if (\n errorText.includes('rate limit') ||\n errorText.includes('too many requests')\n ) {\n return new RateLimitError('claude-cli');\n }\n\n // Context length\n if (\n errorText.includes('context length') ||\n errorText.includes('too long')\n ) {\n return new ContextLengthError('claude-cli');\n }\n\n return new AIError(\n `Claude CLI error (exit code ${exitCode}): ${stderr}`,\n 'CLI_ERROR',\n 'claude-cli',\n );\n }\n\n /**\n * Generate a chat completion using Claude CLI\n * @param messages - Array of conversation messages\n * @param options - Optional configuration for the chat completion\n * @returns Promise resolving to the AI response with content and metadata\n * @throws {AIError} When the CLI execution fails\n *\n * @example\n * ```typescript\n * const response = await provider.chat([\n * { role: 'system', content: 'You are a helpful assistant.' },\n * { role: 'user', content: 'Explain quantum computing' }\n * ], {\n * model: 'sonnet'\n * });\n * ```\n */\n async chat(\n messages: AIMessage[],\n options: ChatOptions = {},\n ): Promise<AIResponse> {\n // Check if ANTHROPIC_API_KEY is available and use fallback if so\n await this.initializeFallback();\n if (this.anthropicFallback) {\n return this.anthropicFallback.chat(messages, options);\n }\n\n const startTime = Date.now();\n let { prompt, systemPrompt } = this.mapMessagesToPrompt(messages);\n\n // Add JSON format instruction if requested\n if (options.responseFormat?.type === 'json_object') {\n const jsonInstruction =\n '\\n\\nIMPORTANT: You must respond with valid JSON only. Do not include any explanatory text outside the JSON object.';\n systemPrompt = systemPrompt\n ? systemPrompt + jsonInstruction\n : jsonInstruction.trim();\n }\n\n const result = await this.executeCommand(prompt, {\n model: options.model,\n systemPrompt,\n temperature: options.temperature,\n maxTokens: options.maxTokens,\n });\n\n const model = options.model || this.options.defaultModel;\n const usage: TokenUsage | undefined = result.usage\n ? {\n promptTokens: result.usage.input_tokens || 0,\n completionTokens: result.usage.output_tokens || 0,\n totalTokens:\n (result.usage.input_tokens || 0) +\n (result.usage.output_tokens || 0),\n }\n : undefined;\n\n emitUsage(\n this.options,\n 'claude-cli',\n 'chat',\n model!,\n usage,\n startTime,\n options.usageTags,\n );\n\n // Parse Claude CLI JSON output\n return {\n content:\n result.result ||\n result.content ||\n result.text ||\n JSON.stringify(result),\n model,\n finishReason: result.is_error ? 'stop' : 'stop',\n usage,\n };\n }\n\n /**\n * Generate a text completion (delegates to chat)\n */\n async complete(\n prompt: string,\n options: CompletionOptions = {},\n ): Promise<AIResponse> {\n // Check if ANTHROPIC_API_KEY is available and use fallback if so\n await this.initializeFallback();\n if (this.anthropicFallback) {\n return this.anthropicFallback.complete(prompt, options);\n }\n\n return this.chat([{ role: 'user', content: prompt }], {\n model: options.model,\n maxTokens: options.maxTokens,\n temperature: options.temperature,\n topP: options.topP,\n n: options.n,\n stop: options.stop,\n stream: options.stream,\n onProgress: options.onProgress,\n usageTags: options.usageTags,\n });\n }\n\n /**\n * Simple message interface for single-turn interactions with optional history\n *\n * @param text - The message text to send\n * @param options - Configuration options including history, model, etc.\n * @returns Promise resolving to the response content string\n *\n * @example\n * ```typescript\n * // Simple usage\n * const response = await provider.message('Hello!');\n *\n * // With history\n * const response = await provider.message('What was my question?', {\n * history: [\n * { role: 'user', content: 'What is 2+2?' },\n * { role: 'assistant', content: '4' }\n * ]\n * });\n * ```\n */\n async message(text: string, options: MessageOptions = {}): Promise<string> {\n // Check if ANTHROPIC_API_KEY is available and use fallback if so\n await this.initializeFallback();\n if (this.anthropicFallback) {\n return this.anthropicFallback.message(text, options);\n }\n\n // Build messages array from history + current message\n const messages: AIMessage[] = [\n ...(options.history || []),\n { role: options.role || 'user', content: text },\n ];\n\n const response = await this.chat(messages, {\n model: options.model,\n maxTokens: options.maxTokens,\n temperature: options.temperature,\n topP: options.topP,\n stop: options.stop,\n stream: options.stream,\n frequencyPenalty: options.frequencyPenalty,\n presencePenalty: options.presencePenalty,\n responseFormat: options.responseFormat,\n seed: options.seed,\n tools: options.tools,\n toolChoice: options.toolChoice,\n onProgress: options.onProgress,\n usageTags: options.usageTags,\n });\n\n return response.content;\n }\n\n /**\n * Embeddings are not supported by Claude CLI or Anthropic\n */\n async embed(\n _text: string | string[],\n _options: EmbeddingOptions = {},\n ): Promise<EmbeddingResponse> {\n // Note: Even with fallback, Anthropic doesn't support embeddings\n throw new AIError(\n 'Claude CLI does not support embeddings. Use OpenAI or another provider for embeddings.',\n 'NOT_SUPPORTED',\n 'claude-cli',\n );\n }\n\n /**\n * Image embeddings are not supported by Claude CLI\n */\n async embedImage(\n _image: string | Buffer,\n _options: ImageEmbeddingOptions = {},\n ): Promise<EmbeddingResponse> {\n throw new AIError(\n 'Claude CLI does not support image embeddings. Use OpenAI or Gemini.',\n 'NOT_SUPPORTED',\n 'claude-cli',\n );\n }\n\n /**\n * Image description is not yet implemented for Claude CLI\n * Note: Claude supports vision, but this would require significant implementation\n */\n async describeImage(\n _image: string | Buffer,\n _prompt?: string,\n _options: ImageDescriptionOptions = {},\n ): Promise<string> {\n throw new AIError(\n 'Image description is not yet implemented for Claude CLI. Use OpenAI or Gemini.',\n 'NOT_IMPLEMENTED',\n 'claude-cli',\n );\n }\n\n /**\n * Image generation is not supported by Claude\n */\n async generateImage(\n _prompt: string,\n _options: ImageGenerationOptions = {},\n ): Promise<ImageGenerationResponse> {\n throw new AIError(\n 'Claude does not support image generation. Use OpenAI or Gemini.',\n 'NOT_SUPPORTED',\n 'claude-cli',\n );\n }\n\n /**\n * Stream chat completion using Claude CLI\n */\n async *stream(\n messages: AIMessage[],\n options: ChatOptions = {},\n ): AsyncIterable<string> {\n // Check if ANTHROPIC_API_KEY is available and use fallback if so\n await this.initializeFallback();\n if (this.anthropicFallback) {\n yield* this.anthropicFallback.stream(messages, options);\n return;\n }\n\n const startTime = Date.now();\n let { prompt, systemPrompt } = this.mapMessagesToPrompt(messages);\n\n // Add JSON format instruction if requested\n if (options.responseFormat?.type === 'json_object') {\n const jsonInstruction =\n '\\n\\nIMPORTANT: You must respond with valid JSON only. Do not include any explanatory text outside the JSON object.';\n systemPrompt = systemPrompt\n ? systemPrompt + jsonInstruction\n : jsonInstruction.trim();\n }\n\n yield* this.executeStreamingCommand(prompt, {\n model: options.model,\n systemPrompt,\n onProgress: options.onProgress,\n });\n\n const model = options.model || this.options.defaultModel;\n emitUsage(\n this.options,\n 'claude-cli',\n 'stream',\n model!,\n undefined,\n startTime,\n options.usageTags,\n );\n }\n\n /**\n * Count tokens in text (approximation)\n */\n async countTokens(text: string): Promise<number> {\n // Check if ANTHROPIC_API_KEY is available and use fallback if so\n await this.initializeFallback();\n if (this.anthropicFallback) {\n return this.anthropicFallback.countTokens(text);\n }\n\n // Claude uses a different tokenizer, approximate similar to Anthropic\n return Math.ceil(text.length / 3.5);\n }\n\n /**\n * Get available models (static list of Claude models)\n */\n async getModels(): Promise<AIModel[]> {\n // Check if ANTHROPIC_API_KEY is available and use fallback if so\n await this.initializeFallback();\n if (this.anthropicFallback) {\n return this.anthropicFallback.getModels();\n }\n\n return [\n {\n id: 'sonnet',\n name: 'Claude Sonnet',\n description: 'Balanced Claude model via CLI',\n contextLength: 200000,\n capabilities: ['text', 'chat', 'vision'],\n supportsFunctions: false,\n supportsVision: false,\n },\n {\n id: 'opus',\n name: 'Claude Opus',\n description: 'Most powerful Claude model via CLI',\n contextLength: 200000,\n capabilities: ['text', 'chat'],\n supportsFunctions: false,\n supportsVision: false,\n },\n {\n id: 'haiku',\n name: 'Claude Haiku',\n description: 'Fast Claude model via CLI',\n contextLength: 200000,\n capabilities: ['text', 'chat'],\n supportsFunctions: false,\n supportsVision: false,\n },\n ];\n }\n\n /**\n * Get provider capabilities\n */\n async getCapabilities(): Promise<AICapabilities> {\n // Check if ANTHROPIC_API_KEY is available and use fallback if so\n await this.initializeFallback();\n if (this.anthropicFallback) {\n return this.anthropicFallback.getCapabilities();\n }\n\n return {\n chat: true,\n completion: true,\n embeddings: false, // Claude CLI doesn't support embeddings\n streaming: true,\n functions: false, // Not supported via CLI\n vision: false, // Not supported in current CLI version\n fineTuning: false,\n imageEmbeddings: false,\n imageGeneration: false,\n videoGeneration: false,\n tts: false,\n voiceCloning: false,\n voiceDesign: false,\n maxContextLength: 200000,\n supportedOperations: ['chat', 'completion', 'streaming'],\n };\n }\n\n // ============================================================================\n // Video Generation Methods (Not supported - use gemini, byteplus-modelark,\n // or openai-compat-video provider)\n // ============================================================================\n\n async submitVideoGenerationJob(\n _options: VideoGenerationOptions,\n ): Promise<VideoGenerationJob> {\n throw new AIError(\n 'Video generation is not supported by Claude CLI provider. Use gemini, byteplus-modelark, or openai-compat-video provider.',\n 'NOT_IMPLEMENTED',\n 'claude-cli',\n );\n }\n\n async getVideoGenerationJob(\n _handle: VideoGenerationJob,\n ): Promise<VideoGenerationStatusResult> {\n throw new AIError(\n 'Video generation is not supported by Claude CLI provider. Use gemini, byteplus-modelark, or openai-compat-video provider.',\n 'NOT_IMPLEMENTED',\n 'claude-cli',\n );\n }\n\n async fetchVideoGenerationResult(\n _handle: VideoGenerationJob,\n ): Promise<VideoGenerationResult> {\n throw new AIError(\n 'Video generation is not supported by Claude CLI provider. Use gemini, byteplus-modelark, or openai-compat-video provider.',\n 'NOT_IMPLEMENTED',\n 'claude-cli',\n );\n }\n\n async cancelVideoGenerationJob(_handle: VideoGenerationJob): Promise<void> {\n throw new AIError(\n 'Video generation is not supported by Claude CLI provider. Use gemini, byteplus-modelark, or openai-compat-video provider.',\n 'NOT_IMPLEMENTED',\n 'claude-cli',\n );\n }\n\n async validateVideoGenerationAccess(): Promise<boolean> {\n throw new AIError(\n 'Video generation is not supported by Claude CLI provider. Use gemini, byteplus-modelark, or openai-compat-video provider.',\n 'NOT_IMPLEMENTED',\n 'claude-cli',\n );\n }\n\n // ============================================================================\n // TTS Methods (Not supported - use Qwen3-TTS provider)\n // ============================================================================\n\n async synthesizeSpeech(\n _text: string,\n _options?: TTSOptions,\n ): Promise<TTSResponse> {\n throw new AIError(\n 'TTS is not supported by Claude CLI provider. Use Qwen3-TTS provider.',\n 'NOT_IMPLEMENTED',\n 'claude-cli',\n );\n }\n\n streamSpeech(_text: string, _options?: TTSOptions): AsyncIterable<Buffer> {\n const error = new AIError(\n 'TTS streaming is not supported by Claude CLI provider. Use Qwen3-TTS provider.',\n 'NOT_IMPLEMENTED',\n 'claude-cli',\n );\n return {\n [Symbol.asyncIterator]: () => ({\n next: () => Promise.reject(error),\n }),\n };\n }\n\n async cloneVoice(_options: VoiceCloneOptions): Promise<Voice> {\n throw new AIError(\n 'Voice cloning is not supported by Claude CLI provider. Use Qwen3-TTS provider.',\n 'NOT_IMPLEMENTED',\n 'claude-cli',\n );\n }\n\n async designVoice(_options: VoiceDesignOptions): Promise<Voice> {\n throw new AIError(\n 'Voice design is not supported by Claude CLI provider. Use Qwen3-TTS provider.',\n 'NOT_IMPLEMENTED',\n 'claude-cli',\n );\n }\n\n async getVoices(_options?: VoiceListOptions): Promise<Voice[]> {\n throw new AIError(\n 'Voice listing is not supported by Claude CLI provider. Use Qwen3-TTS provider.',\n 'NOT_IMPLEMENTED',\n 'claude-cli',\n );\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAsDA,IAAM,YAAY,UAAU,IAAI;;;;;;;;;AAUhC,IAAa,oBAAb,MAAsD;CACpD;CACA,UAAiC;CACjC,oBAAgD;;;;;CAMhD,YAAY,SAA2B;EACrC,KAAK,UAAU;GACb,cAAc;GACd,GAAG;EACL;CACF;;;;;CAMA,MAAc,qBAAoC;EAChD,IAAI,KAAK,sBAAsB,MAC7B;EAGF,MAAM,SAAS,QAAQ,IAAI;EAC3B,IAAI,CAAC,QACH;EAIF,MAAM,EAAE,sBAAsB,MAAM,OAAO;EAS3C,MAAM,eACJ;GANA,QAAQ;GACR,MAAM;GACN,OAAO;EAIP,EAAS,KAAK,QAAQ,gBAAgB,aACtC;EAEF,KAAK,oBAAoB,IAAI,kBAAkB;GAC7C,MAAM;GACN;GACA;GACA,SAAS,KAAK,QAAQ;GACtB,WAAW,KAAK,QAAQ;EAC1B,CAAC;CACH;;;;;;;CAQA,MAAc,UAA2B;EACvC,IAAI,KAAK,SACP,OAAO,KAAK;EAId,IAAI,KAAK,QAAQ,SAAS;GACxB,MAAM,KAAK,MAAM,OAAO;GACxB,IAAI;IACF,MAAM,GAAG,OAAO,KAAK,QAAQ,OAAO;IACpC,KAAK,UAAU,KAAK,QAAQ;IAC5B,OAAO,KAAK;GACd,SAAS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ACjB,KAAK;GACL,cAAc;GACd,aAAa;GACb,kBAAkB;GAClB,qBAAqB;IAAC;IAAQ;IAAc;GAAW;EACzD;CACF;CAOA,MAAM,yBACJ,UAC6B;EAC7B,MAAM,IAAI,QACR,6HACA,mBACA,YACF;CACF;CAEA,MAAM,sBACJ,SACsC;EACtC,MAAM,IAAI,QACR,6HACA,mBACA,YACF;CACF;CAEA,MAAM,2BACJ,SACgC;EAChC,MAAM,IAAI,QACR,6HACA,mBACA,YACF;CACF;CAEA,MAAM,yBAAyB,SAA4C;EACzE,MAAM,IAAI,QACR,6HACA,mBACA,YACF;CACF;CAEA,MAAM,gCAAkD;EACtD,MAAM,IAAI,QACR,6HACA,mBACA,YACF;CACF;CAMA,MAAM,iBACJ,OACA,UACsB;EACtB,MAAM,IAAI,QACR,wEACA,mBACA,YACF;CACF;CAEA,aAAa,OAAe,UAA8C;EACxE,MAAM,QAAQ,IAAI,QAChB,kFACA,mBACA,YACF;EACA,OAAO,GACJ,OAAO,uBAAuB,EAC7B,YAAY,QAAQ,OAAO,KAAK,EAClC,GACF;CACF;CAEA,MAAM,WAAW,UAA6C;EAC5D,MAAM,IAAI,QACR,kFACA,mBACA,YACF;CACF;CAEA,MAAM,YAAY,UAA8C;EAC9D,MAAM,IAAI,QACR,iFACA,mBACA,YACF;CACF;CAEA,MAAM,UAAU,UAA+C;EAC7D,MAAM,IAAI,QACR,kFACA,mBACA,YACF;CACF;AACF"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"gateway-admin-04mvbDHC.js","names":[],"sources":["../../src/shared/providers/gateway-admin.ts"],"sourcesContent":["import type {\n AIAdminBudget,\n AIAdminInterface,\n AIAdminProject,\n AIAdminProviderConfig,\n AIAdminRateLimit,\n AIVirtualKey,\n CreateAIProjectOptions,\n CreateAIVirtualKeyOptions,\n} from '../types';\nimport { AIError } from '../types';\n\ninterface GatewayAdminTransportOptions {\n provider: string;\n baseUrl: string;\n apiKey?: string;\n username?: string;\n password?: string;\n headers?: Record<string, string>;\n timeout?: number;\n}\n\ntype JsonRecord = Record<string, unknown>;\n\nfunction stripTrailingSlash(value: string): string {\n return value.endsWith('/') ? value.slice(0, -1) : value;\n}\n\nexport function normalizeGatewayBaseUrl(baseUrl: string): string {\n return stripTrailingSlash(baseUrl.trim());\n}\n\n/**\n * Derive the gateway root URL for admin APIs from an OpenAI-compatible base URL.\n *\n * Strips known inference path suffixes iteratively so that a baseUrl such as\n * `http://host/openai/v1` collapses to the gateway root rather than to the\n * partial `http://host/openai`.\n */\nexport function deriveGatewayAdminBaseUrl(baseUrl: string): string {\n let normalized = normalizeGatewayBaseUrl(baseUrl);\n const suffixes = ['/pydanticai/v1', '/openai', '/v1'];\n let stripped = true;\n\n while (stripped) {\n stripped = false;\n for (const suffix of suffixes) {\n if (normalized.endsWith(suffix)) {\n normalized = normalized.slice(0, -suffix.length);\n stripped = true;\n break;\n }\n }\n }\n\n return normalized || normalizeGatewayBaseUrl(baseUrl);\n}\n\nexport function resolveGatewayAdminBaseUrl(\n baseUrl: string | undefined,\n adminBaseUrl: string | undefined,\n provider: string,\n): string {\n const configuredBaseUrl = adminBaseUrl || baseUrl;\n if (!configuredBaseUrl?.trim()) {\n throw new AIError(\n `${provider} baseUrl is required for admin operations`,\n 'ADMIN_BASE_URL_REQUIRED',\n provider,\n );\n }\n\n return adminBaseUrl\n ? normalizeGatewayBaseUrl(adminBaseUrl)\n : deriveGatewayAdminBaseUrl(configuredBaseUrl);\n}\n\nexport function slugifyProjectId(name: string, tenantId?: string): string {\n const value = [tenantId, name].filter(Boolean).join('-');\n const slug = value\n .toLowerCase()\n .replace(/[^a-z0-9]+/g, '-')\n .replace(/^-+|-+$/g, '');\n\n return slug || 'ai-project';\n}\n\nfunction toJsonRecord(value: unknown): JsonRecord {\n return value && typeof value === 'object' && !Array.isArray(value)\n ? (value as JsonRecord)\n : {};\n}\n\nfunction stringValue(value: unknown): string | undefined {\n return typeof value === 'string' && value.length > 0 ? value : undefined;\n}\n\nfunction encodeBasicCredentials(username: string, password: string): string {\n const value = `${username}:${password}`;\n\n if (typeof btoa === 'function') {\n return btoa(value);\n }\n\n return Buffer.from(value, 'utf8').toString('base64');\n}\n\nfunction removeUndefinedValues<T extends JsonRecord>(value: T): T {\n for (const key of Object.keys(value)) {\n if (value[key] === undefined) {\n delete value[key];\n }\n }\n\n return value;\n}\n\nfunction createMetadata(\n options: {\n tenantId?: string;\n description?: string;\n metadata?: Record<string, unknown>;\n },\n tenantField = 'tenant_id',\n): Record<string, unknown> | undefined {\n const metadata = {\n ...options.metadata,\n };\n\n if (options.tenantId) {\n metadata[tenantField] = options.tenantId;\n }\n\n if (options.description) {\n metadata.description = options.description;\n }\n\n return Object.keys(metadata).length > 0 ? metadata : undefined;\n}\n\nfunction responseMessage(body: unknown, fallback: string): string {\n const record = toJsonRecord(body);\n const error = toJsonRecord(record.error);\n\n return (\n stringValue(error.message) ||\n stringValue(record.message) ||\n stringValue(record.detail) ||\n fallback\n );\n}\n\nclass GatewayAdminTransport {\n private readonly baseUrl: string;\n private readonly provider: string;\n private readonly apiKey?: string;\n private readonly username?: string;\n private readonly password?: string;\n private readonly headers?: Record<string, string>;\n private readonly timeout?: number;\n\n constructor(options: GatewayAdminTransportOptions) {\n this.provider = options.provider;\n this.baseUrl = normalizeGatewayBaseUrl(options.baseUrl);\n this.apiKey = options.apiKey;\n this.username = options.username;\n this.password = options.password;\n this.headers = options.headers;\n this.timeout = options.timeout;\n }\n\n async request<T>(\n method: string,\n path: string,\n body?: unknown,\n query?: Record<string, string | undefined>,\n ): Promise<T> {\n const url = new URL(\n `${this.baseUrl}${path.startsWith('/') ? path : `/${path}`}`,\n );\n\n if (query) {\n for (const [key, value] of Object.entries(query)) {\n if (value !== undefined) {\n url.searchParams.set(key, value);\n }\n }\n }\n\n const controller =\n typeof AbortController === 'undefined'\n ? undefined\n : new AbortController();\n const timeoutHandle =\n controller && this.timeout\n ? setTimeout(() => controller.abort(), this.timeout)\n : undefined;\n\n try {\n const response = await fetch(url, {\n method,\n headers: {\n Accept: 'application/json',\n ...(body !== undefined ? { 'Content-Type': 'application/json' } : {}),\n ...this.headers,\n ...this.createAuthHeaders(),\n },\n body: body === undefined ? undefined : JSON.stringify(body),\n signal: controller?.signal,\n });\n\n const text = await response.text();\n const parsed = text ? this.parseJson(text) : undefined;\n\n if (!response.ok) {\n throw this.mapResponseError(response, parsed);\n }\n\n return parsed as T;\n } catch (error) {\n if (error instanceof AIError) {\n throw error;\n }\n\n throw new AIError(\n `${this.provider} admin request failed: ${\n error instanceof Error ? error.message : String(error)\n }`,\n 'ADMIN_REQUEST_FAILED',\n this.provider,\n );\n } finally {\n if (timeoutHandle) {\n clearTimeout(timeoutHandle);\n }\n }\n }\n\n private createAuthHeaders(): Record<string, string> {\n if (this.username && this.password) {\n return {\n Authorization: `Basic ${encodeBasicCredentials(\n this.username,\n this.password,\n )}`,\n };\n }\n\n return this.apiKey ? { Authorization: `Bearer ${this.apiKey}` } : {};\n }\n\n private parseJson(text: string): unknown {\n try {\n return JSON.parse(text);\n } catch (_error) {\n return { message: text };\n }\n }\n\n private mapResponseError(response: Response, body: unknown): AIError {\n const message = responseMessage(\n body,\n `${this.provider} admin request failed with ${response.status}`,\n );\n\n if (response.status === 401 || response.status === 403) {\n return new AIError(message, 'AUTH_ERROR', this.provider);\n }\n\n if (response.status === 429) {\n return new AIError(message, 'RATE_LIMIT', this.provider, undefined, true);\n }\n\n return new AIError(\n message,\n `ADMIN_HTTP_${response.status}`,\n this.provider,\n undefined,\n response.status >= 500,\n );\n }\n}\n\nfunction mapBifrostBudget(budget?: AIAdminBudget): JsonRecord | undefined {\n if (!budget) return undefined;\n\n return removeUndefinedValues({\n max_limit: budget.maxLimit,\n reset_duration: budget.resetDuration,\n calendar_aligned: budget.calendarAligned,\n });\n}\n\nfunction mapBifrostRateLimit(\n rateLimit?: AIAdminRateLimit,\n): JsonRecord | undefined {\n if (!rateLimit) return undefined;\n\n return removeUndefinedValues({\n token_max_limit: rateLimit.tokenMaxLimit ?? rateLimit.tpmLimit,\n token_reset_duration: rateLimit.tokenResetDuration,\n request_max_limit: rateLimit.requestMaxLimit ?? rateLimit.rpmLimit,\n request_reset_duration: rateLimit.requestResetDuration,\n });\n}\n\nfunction mapBifrostProviderConfigs(\n configs?: AIAdminProviderConfig[],\n): JsonRecord[] | undefined {\n if (!configs) return undefined;\n\n return configs.map((config) =>\n removeUndefinedValues({\n provider: config.provider,\n weight: config.weight,\n allowed_models: config.allowedModels,\n key_ids: config.keyIds,\n }),\n );\n}\n\nfunction mapLiteLLMBudget(\n budget?: AIAdminBudget,\n): Pick<JsonRecord, 'max_budget' | 'budget_duration'> {\n return removeUndefinedValues({\n max_budget: budget?.maxLimit,\n budget_duration: budget?.resetDuration,\n });\n}\n\nfunction mapLiteLLMRateLimit(\n rateLimit?: AIAdminRateLimit,\n): Pick<JsonRecord, 'tpm_limit' | 'rpm_limit'> {\n return removeUndefinedValues({\n tpm_limit: rateLimit?.tpmLimit ?? rateLimit?.tokenMaxLimit,\n rpm_limit: rateLimit?.rpmLimit ?? rateLimit?.requestMaxLimit,\n });\n}\n\nexport class BifrostAdmin implements AIAdminInterface {\n private readonly transport: GatewayAdminTransport;\n\n constructor(options: GatewayAdminTransportOptions) {\n this.transport = new GatewayAdminTransport(options);\n }\n\n async createProject(\n options: CreateAIProjectOptions,\n ): Promise<AIAdminProject> {\n const body = removeUndefinedValues({\n name: options.name,\n customer_id: options.tenantId,\n budget: mapBifrostBudget(options.budget),\n ...options.raw,\n });\n\n const response = await this.transport.request<JsonRecord>(\n 'POST',\n '/api/governance/teams',\n body,\n );\n const team = toJsonRecord(response.team ?? response);\n const id = stringValue(team.id) || stringValue(team.team_id);\n\n if (!id) {\n throw new AIError(\n 'Bifrost did not return a project id',\n 'ADMIN_INVALID_RESPONSE',\n 'bifrost',\n );\n }\n\n return {\n id,\n name:\n stringValue(team.name) || stringValue(team.team_alias) || options.name,\n tenantId: stringValue(team.customer_id) || options.tenantId,\n budgetId: stringValue(team.budget_id),\n provider: 'bifrost',\n raw: response,\n };\n }\n\n async createVirtualKey(\n options: CreateAIVirtualKeyOptions,\n ): Promise<AIVirtualKey> {\n const body = removeUndefinedValues({\n name: options.name,\n description: options.description,\n provider_configs: mapBifrostProviderConfigs(options.providerConfigs),\n team_id: options.projectId,\n customer_id: options.projectId ? undefined : options.tenantId,\n budget: mapBifrostBudget(options.budget),\n rate_limit: mapBifrostRateLimit(options.rateLimit),\n key_ids: options.keyIds,\n is_active: options.isActive ?? true,\n ...options.raw,\n });\n\n const response = await this.transport.request<JsonRecord>(\n 'POST',\n '/api/governance/virtual-keys',\n body,\n );\n const virtualKey = toJsonRecord(\n response.virtual_key ?? response.virtualKey ?? response,\n );\n\n return {\n id: stringValue(virtualKey.id) || stringValue(virtualKey.key_id),\n name: stringValue(virtualKey.name) || options.name,\n key: stringValue(virtualKey.value) || stringValue(virtualKey.key),\n maskedKey:\n stringValue(virtualKey.key_name) || stringValue(virtualKey.masked_key),\n projectId: stringValue(virtualKey.team_id) || options.projectId,\n tenantId: stringValue(virtualKey.customer_id) || options.tenantId,\n expiresAt:\n stringValue(virtualKey.expires) || stringValue(virtualKey.expires_at),\n provider: 'bifrost',\n raw: response,\n };\n }\n}\n\nexport class LiteLLMAdmin implements AIAdminInterface {\n private readonly transport: GatewayAdminTransport;\n\n constructor(options: GatewayAdminTransportOptions) {\n this.transport = new GatewayAdminTransport(options);\n }\n\n async createProject(\n options: CreateAIProjectOptions,\n ): Promise<AIAdminProject> {\n const projectId =\n options.id || slugifyProjectId(options.name, options.tenantId);\n const body = removeUndefinedValues({\n team_id: projectId,\n team_alias: options.name,\n models: options.models,\n ...mapLiteLLMBudget(options.budget),\n ...mapLiteLLMRateLimit(options.rateLimit),\n metadata: createMetadata(options),\n blocked: options.isBlocked,\n ...options.raw,\n });\n\n const response = await this.transport.request<JsonRecord>(\n 'POST',\n '/team/new',\n body,\n );\n const team = toJsonRecord(response.team ?? response);\n\n return {\n id: stringValue(team.team_id) || projectId,\n name:\n stringValue(team.team_alias) || stringValue(team.name) || options.name,\n tenantId: options.tenantId,\n provider: 'litellm',\n raw: response,\n };\n }\n\n async createVirtualKey(\n options: CreateAIVirtualKeyOptions,\n ): Promise<AIVirtualKey> {\n const body = removeUndefinedValues({\n key_alias: options.name,\n team_id: options.projectId,\n user_id: options.userId,\n models: options.models,\n duration: options.duration,\n ...mapLiteLLMBudget(options.budget),\n ...mapLiteLLMRateLimit(options.rateLimit),\n metadata: createMetadata(options),\n aliases: options.aliases,\n config: options.config,\n permissions: options.permissions,\n blocked: options.isActive === undefined ? undefined : !options.isActive,\n ...options.raw,\n });\n\n const response = await this.transport.request<JsonRecord>(\n 'POST',\n '/key/generate',\n body,\n );\n\n return {\n id: stringValue(response.token_id) || stringValue(response.key_id),\n name:\n stringValue(response.key_alias) ||\n stringValue(response.key_name) ||\n options.name,\n key: stringValue(response.key),\n maskedKey: stringValue(response.key_name),\n projectId: stringValue(response.team_id) || options.projectId,\n tenantId: options.tenantId,\n expiresAt: stringValue(response.expires),\n provider: 'litellm',\n raw: response,\n };\n }\n}\n"],"mappings":";;AAwBA,SAAS,mBAAmB,OAAuB;CACjD,OAAO,MAAM,SAAS,GAAG,IAAI,MAAM,MAAM,GAAG,EAAE,IAAI;AACpD;AAEA,SAAgB,wBAAwB,SAAyB;CAC/D,OAAO,mBAAmB,QAAQ,KAAK,CAAC;AAC1C;;;;;;;;AASA,SAAgB,0BAA0B,SAAyB;CACjE,IAAI,aAAa,wBAAwB,OAAO;CAChD,MAAM,WAAW;EAAC;EAAkB;EAAW;CAAK;CACpD,IAAI,WAAW;CAEf,OAAO,UAAU;EACf,WAAW;EACX,KAAK,MAAM,UAAU,UACnB,IAAI,WAAW,SAAS,MAAM,GAAG;GAC/B,aAAa,WAAW,MAAM,GAAG,CAAC,OAAO,MAAM;GAC/C,WAAW;GACX;EACF;CAEJ;CAEA,OAAO,cAAc,wBAAwB,OAAO;AACtD;AAEA,SAAgB,2BACd,SACA,cACA,UACQ;C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OAAO;GACL,IAAI,YAAY,KAAK,OAAO,KAAK;GACjC,MACE,YAAY,KAAK,UAAU,KAAK,YAAY,KAAK,IAAI,KAAK,QAAQ;GACpE,UAAU,QAAQ;GAClB,UAAU;GACV,KAAK;EACP;CACF;CAEA,MAAM,iBACJ,SACuB;EACvB,MAAM,OAAO,sBAAsB;GACjC,WAAW,QAAQ;GACnB,SAAS,QAAQ;GACjB,SAAS,QAAQ;GACjB,QAAQ,QAAQ;GAChB,UAAU,QAAQ;GAClB,GAAG,iBAAiB,QAAQ,MAAM;GAClC,GAAG,oBAAoB,QAAQ,SAAS;GACxC,UAAU,eAAe,OAAO;GAChC,SAAS,QAAQ;GACjB,QAAQ,QAAQ;GAChB,aAAa,QAAQ;GACrB,SAAS,QAAQ,aAAa,KAAA,IAAY,KAAA,IAAY,CAAC,QAAQ;GAC/D,GAAG,QAAQ;EACb,CAAC;EAED,MAAM,WAAW,MAAM,KAAK,UAAU,QACpC,QACA,iBACA,IACF;EAEA,OAAO;GACL,IAAI,YAAY,SAAS,QAAQ,KAAK,YAAY,SAAS,MAAM;GACjE,MACE,YAAY,SAAS,SAAS,KAC9B,YAAY,SAAS,QAAQ,KAC7B,QAAQ;GACV,KAAK,YAAY,SAAS,GAAG;GAC7B,WAAW,YAAY,SAAS,QAAQ;GACxC,WAAW,YAAY,SAAS,OAAO,KAAK,QAAQ;GACpD,UAAU,QAAQ;GAClB,WAAW,YAAY,SAAS,OAAO;GACvC,UAAU;GACV,KAAK;EACP;CACF;AACF"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"gemini-BGwrHeGc.js","names":[],"sources":["../../src/shared/providers/gemini.ts"],"sourcesContent":["/**\n * Google Gemini provider implementation\n */\n\nimport crypto from 'node:crypto';\nimport { mkdtemp, readFile, rm } from 'node:fs/promises';\nimport { tmpdir } from 'node:os';\nimport { join } from 'node:path';\nimport { extractRetryAfterSeconds } from '../rate-limit';\nimport {\n normalizeBaseAIOptions,\n normalizeChatOptions,\n normalizeImageGenerationOptions,\n type PreparedRequestControls,\n prepareRequestControls,\n} from '../safety';\nimport type {\n AICapabilities,\n AIInterface,\n AIMessage,\n AIModel,\n AIResponse,\n ChatOptions,\n CompletionOptions,\n EmbeddingOptions,\n EmbeddingResponse,\n GeminiOptions,\n ImageDescriptionOptions,\n ImageEmbeddingOptions,\n ImageGenerationOptions,\n ImageGenerationResponse,\n MessageOptions,\n TokenUsage,\n TTSOptions,\n TTSResponse,\n VideoGenerationJob,\n VideoGenerationOptions,\n VideoGenerationResult,\n VideoGenerationStatusResult,\n Voice,\n VoiceCloneOptions,\n VoiceDesignOptions,\n VoiceListOptions,\n} from '../types';\nimport {\n AIError,\n AuthenticationError,\n extractTextContent,\n ModelNotFoundError,\n RateLimitError,\n} from '../types';\nimport { emitUsage } from './usage';\n\n// Note: This implementation uses the new @google/genai package\n// @google/generative-ai is deprecated - migrated to @google/genai\n\n/**\n * Default Veo model for video generation. Veo 2 / Veo 3.0 are deprecated in\n * favor of Veo 3.1 (still \"preview\" in its model id, but the current\n * non-deprecated generation per Google's docs as of this writing). Veo 3.1\n * supports the `resolution` config field this provider forwards; Veo 2 does\n * not, so callers pinning back to Veo 2 should omit `resolution`.\n */\nconst DEFAULT_VEO_MODEL = 'veo-3.1-generate-preview';\n\n/**\n * Type-only reference to the real `GenerateVideosOperation` class. The\n * `@google/genai` SDK's `operations.getVideosOperation` calls\n * `operation._fromAPIResponse(...)`, an instance method — passing a plain\n * `{ name }` object literal throws at runtime. Typing the constructed\n * operation against this type (rather than `any`) makes tsc reject a\n * regression back to a plain literal, since `_fromAPIResponse` is a\n * required member of the real class's public shape.\n */\ntype VideosOperation = import('@google/genai').GenerateVideosOperation;\n\nexport class GeminiProvider implements AIInterface {\n private options: GeminiOptions;\n private client: any; // GoogleGenAI instance from @google/genai\n private operationCtor?: new () => VideosOperation;\n\n constructor(options: GeminiOptions) {\n this.options = normalizeBaseAIOptions({\n defaultModel: 'gemini-2.5-flash',\n ...options,\n });\n\n // Initialize Google Generative AI client\n this.initializeClientSync();\n }\n\n private initializeClientSync() {\n try {\n // Dynamic import in constructor - this will work if the package is installed\n import('@google/genai')\n .then(({ GoogleGenAI, GenerateVideosOperation }) => {\n this.client = new GoogleGenAI(this.buildClientConfig());\n this.operationCtor = GenerateVideosOperation;\n })\n .catch(() => {\n // Client will be null and we'll handle it in methods\n });\n } catch (_error) {\n // Client will be null and we'll handle it in methods\n }\n }\n\n private async ensureClient() {\n if (!this.client) {\n try {\n const { GoogleGenAI, GenerateVideosOperation } = await import(\n '@google/genai'\n );\n this.client = new GoogleGenAI(this.buildClientConfig());\n this.operationCtor = GenerateVideosOperation;\n } catch (_error) {\n throw new AIError(\n 'Failed to initialize Gemini client. Make sure @google/genai is installed.',\n 'INITIALIZATION_ERROR',\n 'gemini',\n );\n }\n }\n }\n\n /**\n * Build the GoogleGenAI client configuration based on provided options.\n * Supports both Google AI Studio (apiKey only) and Vertex AI (projectId + location).\n */\n private buildClientConfig(): Record<string, any> {\n // If projectId and location are provided, use Vertex AI mode\n if (this.options.projectId && this.options.location) {\n return {\n vertexai: true,\n project: this.options.projectId,\n location: this.options.location,\n apiKey: this.options.apiKey, // Optional for Vertex AI with ADC\n httpOptions: {\n timeout: this.options.timeout,\n retryOptions: { attempts: (this.options.maxRetries || 0) + 1 },\n },\n };\n }\n\n // Default to Google AI Studio mode with API key\n return {\n apiKey: this.options.apiKey,\n httpOptions: {\n timeout: this.options.timeout,\n retryOptions: { attempts: (this.options.maxRetries || 0) + 1 },\n },\n };\n }\n\n async chat(\n messages: AIMessage[],\n options: ChatOptions = {},\n ): Promise<AIResponse> {\n const startTime = Date.now();\n let controls: PreparedRequestControls | undefined;\n try {\n await this.ensureClient();\n\n const model = options.model || this.options.defaultModel;\n options = normalizeChatOptions(this.options, options, 'gemini', model);\n controls = prepareRequestControls(this.options, options);\n const requestConfig: Record<string, any> = {\n model,\n contents: this.messagesToGeminiFormat(messages),\n config: this.buildGenerateContentConfig(options, controls),\n };\n\n // Call new SDK API: ai.models.generateContent()\n const result = await this.client.models.generateContent(requestConfig);\n\n // Extract tool calls from response\n let toolCalls: AIResponse['toolCalls'];\n const firstCandidate = result.candidates?.[0];\n if (firstCandidate?.content?.parts) {\n const functionCalls = firstCandidate.content.parts.filter(\n (part: any) => part.functionCall,\n );\n if (functionCalls.length > 0) {\n toolCalls = functionCalls.map((part: any) => ({\n id: `call_${crypto.randomUUID()}`,\n type: 'function' as const,\n function: {\n name: part.functionCall.name,\n arguments: JSON.stringify(part.functionCall.args || {}),\n },\n }));\n }\n }\n\n // Clean content - remove markdown code blocks if JSON mode was requested\n let content = result.text || '';\n if (options.responseFormat?.type === 'json_object') {\n content = this.stripMarkdownCodeBlock(content);\n }\n\n const usage: TokenUsage = {\n promptTokens: result.usageMetadata?.promptTokenCount || 0,\n completionTokens: result.usageMetadata?.candidatesTokenCount || 0,\n totalTokens: result.usageMetadata?.totalTokenCount || 0,\n };\n emitUsage(\n this.options,\n 'gemini',\n 'chat',\n model!,\n usage,\n startTime,\n options.usageTags,\n );\n\n return {\n content,\n model,\n finishReason: this.mapFinishReason(result),\n usage,\n toolCalls: toolCalls && toolCalls.length > 0 ? toolCalls : undefined,\n };\n } catch (error) {\n if (controls?.didTimeout()) {\n throw new AIError(\n `AI request timed out after ${controls.timeout}ms`,\n 'AI_TIMEOUT',\n 'gemini',\n options.model,\n );\n }\n if (options.signal?.aborted) {\n throw new AIError(\n 'AI request aborted by caller',\n 'AI_ABORTED',\n 'gemini',\n options.model,\n );\n }\n throw this.mapError(error);\n } finally {\n controls?.cleanup();\n }\n }\n\n async complete(\n prompt: string,\n options: CompletionOptions = {},\n ): Promise<AIResponse> {\n return this.chat([{ role: 'user', content: prompt }], {\n model: options.model,\n maxTokens: options.maxTokens,\n temperature: options.temperature,\n topP: options.topP,\n n: options.n,\n stop: options.stop,\n stream: options.stream,\n onProgress: options.onProgress,\n signal: options.signal,\n timeout: options.timeout,\n reasoning: options.reasoning,\n usageTags: options.usageTags,\n });\n }\n\n /**\n * Simple message interface for single-turn interactions with optional history\n *\n * @param text - The message text to send\n * @param options - Configuration options including history, model, etc.\n * @returns Promise resolving to the response content string\n *\n * @example\n * ```typescript\n * // Simple usage\n * const response = await provider.message('Hello!');\n *\n * // With history\n * const response = await provider.message('What was my question?', {\n * history: [\n * { role: 'user', content: 'What is 2+2?' },\n * { role: 'assistant', content: '4' }\n * ]\n * });\n * ```\n */\n async message(text: string, options: MessageOptions = {}): Promise<string> {\n // Build messages array from history + current message\n const messages: AIMessage[] = [\n ...(options.history || []),\n { role: options.role || 'user', content: text },\n ];\n\n const response = await this.chat(messages, {\n model: options.model,\n maxTokens: options.maxTokens,\n temperature: options.temperature,\n topP: options.topP,\n stop: options.stop,\n stream: options.stream,\n frequencyPenalty: options.frequencyPenalty,\n presencePenalty: options.presencePenalty,\n responseFormat: options.responseFormat,\n seed: options.seed,\n tools: options.tools,\n toolChoice: options.toolChoice,\n onProgress: options.onProgress,\n signal: options.signal,\n timeout: options.timeout,\n reasoning: options.reasoning,\n usageTags: options.usageTags,\n });\n\n return response.content;\n }\n\n /**\n * Generate embeddings for text using Gemini embedding models\n * @param text - Single text string or array of texts to embed\n * @param options - Optional configuration for embeddings\n * @returns Promise resolving to embeddings response\n *\n * @example\n * ```typescript\n * const embedding = await provider.embed('Hello world');\n * const embeddings = await provider.embed(['Text 1', 'Text 2']);\n * ```\n */\n async embed(\n text: string | string[],\n options: EmbeddingOptions = {},\n ): Promise<EmbeddingResponse> {\n const startTime = Date.now();\n try {\n await this.ensureClient();\n\n const model = options.model || 'text-embedding-004';\n const input = Array.isArray(text) ? text : [text];\n\n const embeddings: number[][] = [];\n let totalTokens = 0;\n\n for (const content of input) {\n const config: Record<string, any> = {};\n if (options.dimensions) {\n config.outputDimensionality = options.dimensions;\n }\n\n const result = await this.client.models.embedContent({\n model,\n contents: content,\n config: Object.keys(config).length > 0 ? config : undefined,\n });\n\n if (result.embeddings?.[0]?.values) {\n embeddings.push(result.embeddings[0].values);\n }\n if (result.metadata?.tokenCount) {\n totalTokens += result.metadata.tokenCount;\n }\n }\n\n const usage: TokenUsage | undefined =\n totalTokens > 0\n ? {\n promptTokens: totalTokens,\n completionTokens: 0,\n totalTokens,\n }\n : undefined;\n emitUsage(\n this.options,\n 'gemini',\n 'embed',\n model,\n usage,\n startTime,\n options.usageTags,\n );\n\n return {\n embeddings,\n model,\n usage,\n };\n } catch (error) {\n throw this.mapError(error);\n }\n }\n\n /**\n * Decode an image input (URL, base64 data URL, or Buffer) to raw bytes.\n * Shared by the chat/vision inline-data format and the video-generation\n * `imageBytes` format, which use different wrapper shapes around the same\n * base64 payload.\n * @private\n */\n private async decodeImageInput(\n image: string | Buffer,\n signal?: AbortSignal,\n ): Promise<{ mimeType: string; base64Data: string }> {\n let mimeType = 'image/png';\n let base64Data: string;\n\n if (Buffer.isBuffer(image)) {\n base64Data = image.toString('base64');\n } else if (image.startsWith('data:')) {\n // Parse data URL\n const match = image.match(/^data:([^;]+);base64,(.+)$/);\n if (match) {\n mimeType = match[1];\n base64Data = match[2];\n } else {\n throw new AIError(\n 'Invalid base64 data URL format',\n 'INVALID_INPUT',\n 'gemini',\n );\n }\n } else {\n // Fetch from URL\n const response = await fetch(image, { signal });\n if (!response.ok) {\n throw new AIError(\n `Failed to fetch image: ${response.status} ${response.statusText}`,\n 'IMAGE_FETCH_ERROR',\n 'gemini',\n );\n }\n const arrayBuffer = await response.arrayBuffer();\n base64Data = Buffer.from(arrayBuffer).toString('base64');\n mimeType = response.headers.get('content-type') || 'image/png';\n }\n\n return { mimeType, base64Data };\n }\n\n /**\n * Convert an image to Gemini's inline-data format, used by chat/vision\n * `contents` parts (`generateContent`, `embedContent`).\n * @param image - Image as URL, base64 data URL, or Buffer\n * @returns Gemini inline data format\n * @private\n */\n private async imageToGeminiFormat(\n image: string | Buffer,\n signal?: AbortSignal,\n ): Promise<{ inlineData: { mimeType: string; data: string } }> {\n const { mimeType, base64Data } = await this.decodeImageInput(image, signal);\n return {\n inlineData: { mimeType, data: base64Data },\n };\n }\n\n /**\n * Convert a reference image to the shape `generateVideos`'s `image`\n * parameter expects: `{ imageBytes, mimeType }` (or `{ gcsUri }` for a\n * `gs://` URI in Vertex AI mode). This is a different shape than\n * {@link imageToGeminiFormat}'s `{ inlineData: {...} }` — the SDK's video\n * converters read `imageBytes`/`mimeType`/`gcsUri` directly and silently\n * drop anything else, which previously caused image-to-video requests to\n * convert to `{}` and fall back to text-to-video.\n * @private\n */\n private async imageToGeminiVideoFormat(\n image: string | Buffer,\n signal?: AbortSignal,\n ): Promise<{ imageBytes: string; mimeType: string } | { gcsUri: string }> {\n if (typeof image === 'string' && image.startsWith('gs://')) {\n return { gcsUri: image };\n }\n const { mimeType, base64Data } = await this.decodeImageInput(image, signal);\n return { imageBytes: base64Data, mimeType };\n }\n\n /**\n * Generate a text description of an image\n * @param image - Image as URL, base64 data URL, or Buffer\n * @param prompt - Custom prompt for description (optional)\n * @param options - Optional configuration\n * @returns Promise resolving to the description string\n *\n * @example\n * ```typescript\n * const description = await provider.describeImage('https://example.com/image.jpg');\n * ```\n */\n async describeImage(\n image: string | Buffer,\n prompt?: string,\n options: ImageDescriptionOptions = {},\n ): Promise<string> {\n let controls: PreparedRequestControls | undefined;\n try {\n await this.ensureClient();\n\n const defaultPrompt =\n 'Describe this image for a search index. Include objects, mood, lighting, and any visible text.';\n\n const model =\n options.model || this.options.defaultModel || 'gemini-2.5-flash';\n const normalized = normalizeChatOptions(\n this.options,\n { ...options, maxTokens: options.maxTokens ?? 500 },\n 'gemini',\n model,\n );\n controls = prepareRequestControls(this.options, normalized);\n const imageData = await this.imageToGeminiFormat(image, controls.signal);\n\n const response = await this.client.models.generateContent({\n model,\n contents: [{ text: prompt || defaultPrompt }, imageData],\n config: this.buildGenerateContentConfig(normalized, controls),\n });\n\n return response.text || '';\n } catch (error) {\n if (controls?.didTimeout()) {\n throw new AIError(\n `AI request timed out after ${controls.timeout}ms`,\n 'AI_TIMEOUT',\n 'gemini',\n options.model,\n );\n }\n if (options.signal?.aborted) {\n throw new AIError(\n 'AI request aborted by caller',\n 'AI_ABORTED',\n 'gemini',\n options.model,\n );\n }\n throw this.mapError(error);\n } finally {\n controls?.cleanup();\n }\n }\n\n /**\n * Generate embeddings for an image using native multimodal embeddings\n * @param image - Image as URL, base64 data URL, or Buffer\n * @param options - Optional configuration for image embeddings\n * @returns Promise resolving to embeddings response\n *\n * @example\n * ```typescript\n * const embedding = await provider.embedImage('https://example.com/image.jpg');\n * ```\n */\n async embedImage(\n image: string | Buffer,\n options: ImageEmbeddingOptions = {},\n ): Promise<EmbeddingResponse> {\n try {\n await this.ensureClient();\n\n // Gemini uses multimodal embedding model\n const model = options.model || 'multimodalembedding@001';\n const imageData = await this.imageToGeminiFormat(image);\n\n const config: Record<string, any> = {};\n if (options.dimensions) {\n config.outputDimensionality = options.dimensions;\n }\n\n const result = await this.client.models.embedContent({\n model,\n contents: [imageData],\n config: Object.keys(config).length > 0 ? config : undefined,\n });\n\n return {\n embeddings: result.embeddings?.[0]?.values\n ? [result.embeddings[0].values]\n : [],\n model,\n };\n } catch (error) {\n throw this.mapError(error);\n }\n }\n\n /**\n * Generate an image from a text prompt using Imagen 3\n * @param prompt - Text description of the image to generate\n * @param options - Optional configuration for image generation\n * @returns Promise resolving to generated image(s)\n *\n * @example\n * ```typescript\n * const result = await provider.generateImage('A sunset over mountains');\n * fs.writeFileSync('image.png', result.images[0].data);\n * ```\n */\n async generateImage(\n prompt: string,\n options: ImageGenerationOptions = {},\n ): Promise<ImageGenerationResponse> {\n let controls: PreparedRequestControls | undefined;\n try {\n await this.ensureClient();\n\n const model = options.model || 'imagen-3.0-generate-002';\n options = normalizeImageGenerationOptions(\n this.options,\n options,\n 'gemini',\n model,\n );\n controls = prepareRequestControls(this.options, options);\n\n const config: Record<string, any> = {\n numberOfImages: options.n,\n abortSignal: controls.signal,\n httpOptions: {\n timeout: controls.timeout,\n retryOptions: { attempts: (this.options.maxRetries || 0) + 1 },\n },\n };\n\n if (options.aspectRatio) {\n config.aspectRatio = options.aspectRatio;\n }\n\n const response = await this.client.models.generateImages({\n model,\n prompt,\n config,\n });\n\n const images = (response.generatedImages || []).map((img: any) => {\n let data: Buffer | string;\n const mimeType = 'image/png';\n const imageBytes = img.image?.imageBytes;\n\n if (options.outputFormat === 'base64') {\n data =\n typeof imageBytes === 'string'\n ? imageBytes\n : Buffer.from(imageBytes).toString('base64');\n } else if (options.outputFormat === 'url') {\n // Gemini Imagen doesn't provide URLs, return as base64 data URL\n const b64 =\n typeof imageBytes === 'string'\n ? imageBytes\n : Buffer.from(imageBytes).toString('base64');\n data = `data:${mimeType};base64,${b64}`;\n } else {\n // Default: buffer\n data =\n typeof imageBytes === 'string'\n ? Buffer.from(imageBytes, 'base64')\n : Buffer.from(imageBytes);\n }\n\n return {\n data,\n mimeType,\n };\n });\n\n return {\n images,\n model,\n };\n } catch (error) {\n if (controls?.didTimeout()) {\n throw new AIError(\n `AI request timed out after ${controls.timeout}ms`,\n 'AI_TIMEOUT',\n 'gemini',\n options.model,\n );\n }\n if (options.signal?.aborted) {\n throw new AIError(\n 'AI request aborted by caller',\n 'AI_ABORTED',\n 'gemini',\n options.model,\n );\n }\n throw this.mapError(error);\n } finally {\n controls?.cleanup();\n }\n }\n\n // ============================================================================\n // Video Generation Methods (Veo, via long-running operations)\n // ============================================================================\n\n /**\n * Submit an asynchronous video-generation job using Veo.\n *\n * Uses the same `@google/genai` client and credentials as {@link generateImage}.\n * Returns a serializable handle immediately; the render itself runs as a\n * long-running operation that callers poll via {@link getVideoGenerationJob}.\n *\n * @param options - Prompt, reference image(s), and generation parameters\n * @returns Promise resolving to a serializable job handle\n *\n * @example\n * ```typescript\n * const job = await provider.submitVideoGenerationJob({\n * prompt: 'A neon hologram of a cat driving at top speed',\n * durationSeconds: 8,\n * });\n * ```\n */\n async submitVideoGenerationJob(\n options: VideoGenerationOptions,\n ): Promise<VideoGenerationJob> {\n const startTime = Date.now();\n let controls: PreparedRequestControls | undefined;\n try {\n await this.ensureClient();\n\n const model = options.model || DEFAULT_VEO_MODEL;\n controls = prepareRequestControls(this.options, options);\n\n let image:\n | { imageBytes: string; mimeType: string }\n | { gcsUri: string }\n | undefined;\n const firstReference = options.referenceImages?.[0];\n if (firstReference) {\n image = await this.imageToGeminiVideoFormat(\n firstReference.image,\n controls.signal,\n );\n }\n\n const config = Object.fromEntries(\n Object.entries({\n abortSignal: controls.signal,\n httpOptions: {\n timeout: controls.timeout,\n retryOptions: { attempts: (this.options.maxRetries || 0) + 1 },\n },\n numberOfVideos: 1,\n durationSeconds: options.durationSeconds,\n fps: options.fps,\n seed: options.seed,\n aspectRatio: options.aspectRatio,\n resolution: options.resolution,\n negativePrompt: options.negativePrompt,\n }).filter(([, value]) => value !== undefined),\n );\n\n const operation = await this.client.models.generateVideos({\n model,\n prompt: options.prompt,\n image,\n config,\n });\n\n if (!operation.name) {\n throw new AIError(\n 'Gemini did not return an operation name for the video-generation job',\n 'VIDEO_JOB_SUBMIT_FAILED',\n 'gemini',\n model,\n );\n }\n\n emitUsage(\n this.options,\n 'gemini',\n 'submitVideoGenerationJob',\n model,\n undefined,\n startTime,\n {\n ...(options.durationSeconds !== undefined\n ? { durationSeconds: String(options.durationSeconds) }\n : {}),\n ...(options.resolution ? { resolution: options.resolution } : {}),\n ...(options.aspectRatio ? { aspectRatio: options.aspectRatio } : {}),\n ...options.usageTags,\n },\n );\n\n return {\n jobId: operation.name,\n provider: 'gemini',\n model,\n createdAt: new Date().toISOString(),\n raw: {\n vertexai: Boolean(this.options.projectId && this.options.location),\n },\n };\n } catch (error) {\n if (controls?.didTimeout()) {\n throw new AIError(\n `AI request timed out after ${controls.timeout}ms`,\n 'AI_TIMEOUT',\n 'gemini',\n options.model,\n );\n }\n if (options.signal?.aborted) {\n throw new AIError(\n 'AI request aborted by caller',\n 'AI_ABORTED',\n 'gemini',\n options.model,\n );\n }\n throw this.mapError(error);\n } finally {\n controls?.cleanup();\n }\n }\n\n /**\n * Build a real `GenerateVideosOperation` instance for polling, and assert\n * the handle is being resumed against a provider configured in the same\n * mode (Google AI Studio vs. Vertex AI) it was submitted in.\n * @private\n */\n private buildVideoOperation(handle: VideoGenerationJob): VideosOperation {\n if (!this.operationCtor) {\n throw new AIError(\n 'Gemini client failed to initialize the video-operation constructor. Make sure @google/genai is installed.',\n 'INITIALIZATION_ERROR',\n 'gemini',\n handle.model,\n );\n }\n this.assertResumeModeMatches(handle);\n\n const operation = new this.operationCtor();\n operation.name = handle.jobId;\n return operation;\n }\n\n /**\n * A handle submitted in Vertex AI mode cannot be resumed against a\n * provider configured for Google AI Studio (or vice versa): the SDK\n * routes `operations.getVideosOperation` based on how *this* client was\n * constructed, not how the job was submitted, so a mode mismatch would\n * otherwise surface as a confusing 404 deep in the SDK.\n * @private\n */\n private assertResumeModeMatches(handle: VideoGenerationJob): void {\n const submittedVertexAI = handle.raw?.vertexai;\n if (typeof submittedVertexAI !== 'boolean') {\n // Unknown/legacy handle shape: best effort, let the request proceed.\n return;\n }\n\n const currentVertexAI = Boolean(\n this.options.projectId && this.options.location,\n );\n if (submittedVertexAI !== currentVertexAI) {\n throw new AIError(\n `Video-generation job ${handle.jobId} was submitted in ${\n submittedVertexAI ? 'Vertex AI' : 'Google AI Studio'\n } mode, but this provider instance is configured for ${\n currentVertexAI ? 'Vertex AI' : 'Google AI Studio'\n } mode. Resume with a provider configured the same way it was submitted.`,\n 'VIDEO_JOB_MODE_MISMATCH',\n 'gemini',\n handle.model,\n );\n }\n }\n\n /**\n * Poll the status of a Veo video-generation job.\n *\n * On success, `result` carries the provider's `url` (a `files/*:download`\n * resource the caller cannot fetch directly without this provider's API\n * key) plus `mimeType`, but not `data` — call\n * {@link fetchVideoGenerationResult} to download the bytes.\n *\n * @param handle - The job handle returned by {@link submitVideoGenerationJob}\n * @returns Promise resolving to the current status, and result once succeeded\n */\n async getVideoGenerationJob(\n handle: VideoGenerationJob,\n ): Promise<VideoGenerationStatusResult> {\n try {\n await this.ensureClient();\n const operation = this.buildVideoOperation(handle);\n const updated = await this.client.operations.getVideosOperation({\n operation,\n });\n return this.mapVideoOperation(updated);\n } catch (error) {\n throw this.mapError(error);\n }\n }\n\n /**\n * Fetch the result of a completed Veo video-generation job, downloading\n * the rendered bytes with this provider's API key (the `uri` Gemini\n * returns is a `files/*:download` resource that requires the same\n * credentials this provider already holds; a bare consumer cannot fetch\n * it directly).\n *\n * @param handle - The job handle returned by {@link submitVideoGenerationJob}\n * @returns Promise resolving to the generated video's bytes and metadata\n * @throws {AIError} When the job has not succeeded yet\n */\n async fetchVideoGenerationResult(\n handle: VideoGenerationJob,\n ): Promise<VideoGenerationResult> {\n try {\n await this.ensureClient();\n const operation = this.buildVideoOperation(handle);\n const updated = await this.client.operations.getVideosOperation({\n operation,\n });\n const status = this.mapVideoOperation(updated);\n if (status.status !== 'succeeded' || !status.result) {\n throw new AIError(\n `Gemini video-generation job ${handle.jobId} has not succeeded (status: ${status.status})`,\n 'VIDEO_JOB_NOT_READY',\n 'gemini',\n handle.model,\n );\n }\n\n const video = updated.response?.generatedVideos?.[0]?.video;\n const mimeType = status.result.mimeType;\n\n if (video?.videoBytes) {\n // Already inline (e.g. Vertex AI bytesBase64Encoded) — no download needed.\n return {\n url: status.result.url,\n data: Buffer.from(video.videoBytes, 'base64'),\n mimeType,\n };\n }\n\n return {\n url: status.result.url,\n data: await this.downloadGeneratedVideo(video),\n mimeType,\n };\n } catch (error) {\n throw this.mapError(error);\n }\n }\n\n /**\n * Download a generated video's bytes via `ai.files.download`, which\n * resolves the `files/*:download` resource with this provider's API key\n * (matching Google's own documented usage pattern). The SDK's `download`\n * method only writes to a filesystem path, so this round-trips through a\n * temporary file and reads it back into memory.\n * @private\n */\n private async downloadGeneratedVideo(\n video: { uri?: string; videoBytes?: string; mimeType?: string } | undefined,\n ): Promise<Buffer> {\n if (!video?.uri) {\n throw new AIError(\n 'Gemini video-generation job succeeded with no downloadable output',\n 'VIDEO_JOB_NOT_READY',\n 'gemini',\n );\n }\n\n const dir = await mkdtemp(join(tmpdir(), 'have-ai-veo-'));\n const filePath = join(dir, 'video.mp4');\n try {\n await this.client.files.download({ file: video, downloadPath: filePath });\n return await readFile(filePath);\n } finally {\n await rm(dir, { recursive: true, force: true }).catch(() => {});\n }\n }\n\n /**\n * Cancel an in-flight Veo video-generation job.\n *\n * **The Gemini API has no cancel endpoint for video-generation\n * operations.** The generativelanguage v1beta discovery document only\n * defines `batches.cancel` (unrelated to video jobs); there is no\n * `models.operations.cancel`. This always throws — cancellation of a\n * Veo render is unsupported by the provider itself, not merely by this\n * client. Callers must treat cancellation as best-effort across all\n * video-generation providers and tolerate this failure (e.g. by simply\n * discarding the handle and not billing for further polling).\n *\n * @param handle - The job handle returned by {@link submitVideoGenerationJob}\n * @throws {AIError} Always — cancellation is unsupported by the Gemini API\n */\n async cancelVideoGenerationJob(handle: VideoGenerationJob): Promise<void> {\n if (this.options.projectId && this.options.location) {\n throw new AIError(\n 'Cancelling Gemini video-generation jobs is not implemented for Vertex AI mode by this provider (it requires OAuth credentials this provider does not manage).',\n 'NOT_IMPLEMENTED',\n 'gemini',\n handle.model,\n );\n }\n\n throw new AIError(\n 'The Gemini API has no cancel endpoint for video-generation operations (only batches.cancel exists, which does not apply to Veo jobs). ' +\n 'cancelVideoGenerationJob cannot stop an in-flight Veo render on this provider; treat cancellation as best-effort and stop polling / discard the handle instead.',\n 'VIDEO_CANCEL_UNSUPPORTED',\n 'gemini',\n handle.model,\n );\n }\n\n /**\n * Cheap auth-shaped check for Veo access: lists models with a page size\n * of 1. Callers on a hot path must cache the result themselves.\n *\n * @returns Promise resolving to true when access looks valid\n */\n async validateVideoGenerationAccess(): Promise<boolean> {\n try {\n await this.ensureClient();\n await this.client.models.list({ config: { pageSize: 1 } });\n return true;\n } catch (error) {\n throw this.mapError(error);\n }\n }\n\n private mapVideoOperation(operation: {\n done?: boolean;\n error?: Record<string, unknown>;\n response?: {\n generatedVideos?: Array<{\n video?: { uri?: string; videoBytes?: string; mimeType?: string };\n }>;\n };\n }): VideoGenerationStatusResult {\n if (!operation.done) {\n return { status: 'running' };\n }\n\n if (operation.error) {\n const errorInfo = operation.error as { code?: number; message?: unknown };\n const message =\n typeof errorInfo.message === 'string'\n ? errorInfo.message\n : JSON.stringify(operation.error);\n // google.rpc.Code.CANCELLED === 1; fall back to a message regex for\n // error shapes that don't carry a structured code.\n const cancelled = errorInfo.code === 1 || /cancel/i.test(message);\n return cancelled\n ? { status: 'cancelled' }\n : { status: 'failed', error: message };\n }\n\n const video = operation.response?.generatedVideos?.[0]?.video;\n if (!video) {\n return {\n status: 'failed',\n error:\n 'Gemini reported the video-generation job as done with no output',\n };\n }\n\n // Metadata only here — `data` requires a separate authenticated\n // download; see fetchVideoGenerationResult.\n const result: VideoGenerationResult = {\n url: video.uri,\n mimeType: video.mimeType || 'video/mp4',\n };\n\n return { status: 'succeeded', result };\n }\n\n async *stream(\n messages: AIMessage[],\n options: ChatOptions = {},\n ): AsyncIterable<string> {\n const startTime = Date.now();\n let controls: PreparedRequestControls | undefined;\n try {\n await this.ensureClient();\n\n const model = options.model || this.options.defaultModel;\n options = normalizeChatOptions(this.options, options, 'gemini', model);\n controls = prepareRequestControls(this.options, options);\n const stream = await this.client.models.generateContentStream({\n model,\n contents: this.messagesToGeminiFormat(messages),\n config: this.buildGenerateContentConfig(options, controls),\n });\n\n let usage: TokenUsage | undefined;\n\n for await (const chunk of stream) {\n if (chunk.usageMetadata) {\n usage = {\n promptTokens: chunk.usageMetadata.promptTokenCount || 0,\n completionTokens: chunk.usageMetadata.candidatesTokenCount || 0,\n totalTokens: chunk.usageMetadata.totalTokenCount || 0,\n };\n }\n\n const text = chunk.text || '';\n if (!text) {\n continue;\n }\n\n if (options.onProgress) {\n options.onProgress(text);\n }\n yield text;\n }\n\n emitUsage(\n this.options,\n 'gemini',\n 'stream',\n model!,\n usage,\n startTime,\n options.usageTags,\n );\n } catch (error) {\n if (controls?.didTimeout()) {\n throw new AIError(\n `AI request timed out after ${controls.timeout}ms`,\n 'AI_TIMEOUT',\n 'gemini',\n options.model,\n );\n }\n if (options.signal?.aborted) {\n throw new AIError(\n 'AI request aborted by caller',\n 'AI_ABORTED',\n 'gemini',\n options.model,\n );\n }\n throw this.mapError(error);\n } finally {\n controls?.cleanup();\n }\n }\n\n async countTokens(text: string): Promise<number> {\n try {\n await this.ensureClient();\n\n const model = this.options.defaultModel || 'gemini-2.5-flash';\n const response = await this.client.models.countTokens({\n model,\n contents: text,\n });\n\n return response.totalTokens || Math.ceil(text.length / 4);\n } catch (error) {\n throw this.mapError(error);\n }\n }\n\n async getModels(): Promise<AIModel[]> {\n // Return static list of known Gemini models\n return [\n {\n id: 'gemini-3-flash-preview',\n name: 'Gemini 3 Flash Preview',\n description:\n 'Preview of Gemini 3 Flash model. Available on Vertex AI in us-central1 only.',\n contextLength: 1000000,\n capabilities: ['text', 'chat', 'vision', 'functions'],\n supportsFunctions: true,\n supportsVision: true,\n },\n {\n id: 'gemini-2.0-flash-001',\n name: 'Gemini 2.0 Flash',\n description:\n 'Latest fast and efficient Gemini model with function calling',\n contextLength: 1000000,\n capabilities: ['text', 'chat', 'vision', 'functions'],\n supportsFunctions: true,\n supportsVision: true,\n },\n {\n id: 'gemini-2.5-flash',\n name: 'Gemini 2.5 Flash',\n description: 'Experimental next-generation Gemini model',\n contextLength: 1000000,\n capabilities: ['text', 'chat', 'vision', 'functions'],\n supportsFunctions: true,\n supportsVision: true,\n },\n {\n id: 'gemini-1.5-pro',\n name: 'Gemini 1.5 Pro (Legacy)',\n description: 'Previous generation model (may not be available)',\n contextLength: 2000000,\n capabilities: ['text', 'chat', 'vision', 'functions'],\n supportsFunctions: true,\n supportsVision: true,\n },\n ];\n }\n\n async getCapabilities(): Promise<AICapabilities> {\n return {\n chat: true,\n completion: true,\n embeddings: true,\n streaming: true,\n functions: true,\n vision: true,\n fineTuning: false,\n imageEmbeddings: true,\n imageGeneration: true,\n videoGeneration: true,\n tts: false,\n voiceCloning: false,\n voiceDesign: false,\n maxContextLength: 2000000,\n supportedOperations: [\n 'chat',\n 'completion',\n 'embedding',\n 'streaming',\n 'functions',\n 'vision',\n 'image_embedding',\n 'image_generation',\n 'video_generation',\n ],\n };\n }\n\n // ============================================================================\n // TTS Methods (Not supported - use Qwen3-TTS provider)\n // ============================================================================\n\n async synthesizeSpeech(\n _text: string,\n _options?: TTSOptions,\n ): Promise<TTSResponse> {\n throw new AIError(\n 'TTS is not supported by Gemini provider. Use Qwen3-TTS provider.',\n 'NOT_IMPLEMENTED',\n 'gemini',\n );\n }\n\n streamSpeech(_text: string, _options?: TTSOptions): AsyncIterable<Buffer> {\n const error = new AIError(\n 'TTS streaming is not supported by Gemini provider. Use Qwen3-TTS provider.',\n 'NOT_IMPLEMENTED',\n 'gemini',\n );\n return {\n [Symbol.asyncIterator]: () => ({\n next: () => Promise.reject(error),\n }),\n };\n }\n\n async cloneVoice(_options: VoiceCloneOptions): Promise<Voice> {\n throw new AIError(\n 'Voice cloning is not supported by Gemini provider. Use Qwen3-TTS provider.',\n 'NOT_IMPLEMENTED',\n 'gemini',\n );\n }\n\n async designVoice(_options: VoiceDesignOptions): Promise<Voice> {\n throw new AIError(\n 'Voice design is not supported by Gemini provider. Use Qwen3-TTS provider.',\n 'NOT_IMPLEMENTED',\n 'gemini',\n );\n }\n\n async getVoices(_options?: VoiceListOptions): Promise<Voice[]> {\n throw new AIError(\n 'Voice listing is not supported by Gemini provider. Use Qwen3-TTS provider.',\n 'NOT_IMPLEMENTED',\n 'gemini',\n );\n }\n\n private mapToolChoice(\n toolChoice?:\n | 'auto'\n | 'none'\n | { type: 'function'; function: { name: string } },\n ): any {\n if (!toolChoice || toolChoice === 'auto') {\n return { functionCallingConfig: { mode: 'AUTO' } };\n }\n\n if (toolChoice === 'none') {\n return { functionCallingConfig: { mode: 'NONE' } };\n }\n\n if (typeof toolChoice === 'object' && toolChoice.type === 'function') {\n return {\n functionCallingConfig: {\n mode: 'ANY',\n allowedFunctionNames: [toolChoice.function.name],\n },\n };\n }\n\n return { functionCallingConfig: { mode: 'AUTO' } };\n }\n\n private buildGenerateContentConfig(\n options: ChatOptions,\n controls: PreparedRequestControls,\n ): Record<string, any> {\n const config: Record<string, any> = {\n abortSignal: controls.signal,\n httpOptions: {\n timeout: controls.timeout,\n retryOptions: { attempts: (this.options.maxRetries || 0) + 1 },\n },\n maxOutputTokens: options.maxTokens,\n temperature: options.temperature,\n topP: options.topP,\n stopSequences: Array.isArray(options.stop)\n ? options.stop\n : options.stop\n ? [options.stop]\n : undefined,\n responseMimeType:\n options.responseFormat?.type === 'json_object'\n ? 'application/json'\n : undefined,\n frequencyPenalty: options.frequencyPenalty,\n presencePenalty: options.presencePenalty,\n seed: options.seed,\n };\n\n if (options.tools && options.tools.length > 0) {\n config.tools = [\n {\n functionDeclarations: options.tools.map((tool) => ({\n name: tool.function.name,\n description: tool.function.description || '',\n parameters: tool.function.parameters || { type: 'object' },\n })),\n },\n ];\n config.toolConfig = this.mapToolChoice(options.toolChoice);\n }\n\n const reasoning = options.reasoning;\n if (reasoning?.maxTokens !== undefined && reasoning.maxTokens > 0) {\n config.thinkingConfig = {\n thinkingBudget: reasoning.maxTokens,\n includeThoughts: reasoning.includeThoughts,\n };\n }\n\n return Object.fromEntries(\n Object.entries(config).filter(([, value]) => value !== undefined),\n );\n }\n\n private mapFinishReason(response: any): AIResponse['finishReason'] {\n // Check if response has function calls in any candidate\n const firstCandidate = response.candidates?.[0];\n if (firstCandidate?.content?.parts) {\n const hasFunctionCall = firstCandidate.content.parts.some(\n (part: any) => part.functionCall,\n );\n if (hasFunctionCall) {\n return 'tool_calls';\n }\n }\n\n // Gemini doesn't provide detailed finish reasons, default to 'stop'\n return 'stop';\n }\n\n private messagesToGeminiFormat(messages: AIMessage[]): string {\n // Convert messages to a simple text prompt\n // The new SDK expects a string for the contents field\n return messages\n .map((message) => {\n const textContent = extractTextContent(message.content);\n switch (message.role) {\n case 'system':\n return `Instructions: ${textContent}`;\n case 'user':\n return `Human: ${textContent}`;\n case 'assistant':\n return `Assistant: ${textContent}`;\n default:\n return textContent;\n }\n })\n .join('\\n\\n');\n }\n\n private stripMarkdownCodeBlock(text: string): string {\n // Remove markdown code blocks like ```json\\n...\\n```\n const codeBlockRegex =\n /^```(?:json|javascript|typescript)?\\s*\\n?([\\s\\S]*?)\\n?```\\s*$/;\n const match = text.match(codeBlockRegex);\n return match ? match[1].trim() : text.trim();\n }\n\n private mapError(error: unknown): AIError {\n if (error instanceof AIError) {\n return error;\n }\n\n // Map common Gemini error patterns\n const message =\n error instanceof Error ? error.message : 'Unknown Gemini error occurred';\n\n if (message.includes('API_KEY_INVALID') || message.includes('401')) {\n return new AuthenticationError('gemini');\n }\n\n if (message.includes('QUOTA_EXCEEDED') || message.includes('429')) {\n return new RateLimitError('gemini', extractRetryAfterSeconds(error));\n }\n\n if (message.includes('MODEL_NOT_FOUND') || message.includes('404')) {\n return new ModelNotFoundError(message, 'gemini');\n }\n\n return new AIError(message, 'UNKNOWN_ERROR', 'gemini');\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;AA+DA,IAAM,oBAAoB;AAa1B,IAAa,iBAAb,MAAmD;CACjD;CACA;CACA;CAEA,YAAY,SAAwB;EAClC,KAAK,UAAU,uBAAuB;GACpC,cAAc;GACd,GAAG;EACL,CAAC;EAGD,KAAK,qBAAqB;CAC5B;CAEA,uBAA+B;EAC7B,IAAI;GAEF,OAAO,gBAAgB,CACpB,MAAM,EAAE,aAAa,8BAA8B;IAClD,KAAK,SAAS,IAAI,YAAY,KAAK,kBAAkB,CAAC;IACtD,KAAK,gBAAgB;GACvB,CAAC,CAAC,CACD,YAAY,CAEb,CAAC;EACL,SAAS,QAAQ,CAEjB;CACF;CAEA,MAAc,eAAe;EAC3B,IAAI,CAAC,KAAK,QACR,IAAI;GACF,MAAM,EAAE,aAAa,4BAA4B,MAAM,OACrD;GAEF,KAAK,SAAS,IAAI,YAAY,KAAK,kBAAkB,CAAC;GACtD,KAAK,gBAAgB;EACvB,SAAS,QAAQ;G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