|
| 1 | +/** |
| 2 | + * qBraid Provider for OpenCode |
| 3 | + * |
| 4 | + * This provider extends @ai-sdk/openai-compatible with support for |
| 5 | + * Gemini 3 thought signatures in multi-turn function calling. |
| 6 | + */ |
| 7 | +import { createOpenAICompatible, OpenAICompatibleChatLanguageModel } from "@ai-sdk/openai-compatible" |
| 8 | +import type { LanguageModelV2 } from "@ai-sdk/provider" |
| 9 | +import { type FetchFunction, withoutTrailingSlash } from "@ai-sdk/provider-utils" |
| 10 | + |
| 11 | +export interface QBraidProviderSettings { |
| 12 | + /** |
| 13 | + * API key for authenticating requests. |
| 14 | + */ |
| 15 | + apiKey?: string |
| 16 | + |
| 17 | + /** |
| 18 | + * Base URL for the qBraid API calls. |
| 19 | + * Defaults to https://api.qbraid.com/ai/v1 |
| 20 | + */ |
| 21 | + baseURL?: string |
| 22 | + |
| 23 | + /** |
| 24 | + * Custom headers to include in the requests. |
| 25 | + */ |
| 26 | + headers?: Record<string, string> |
| 27 | + |
| 28 | + /** |
| 29 | + * Custom fetch implementation. |
| 30 | + */ |
| 31 | + fetch?: FetchFunction |
| 32 | +} |
| 33 | + |
| 34 | +// Store for thought signatures keyed by tool call ID |
| 35 | +// This allows us to retrieve them when building the next request |
| 36 | +const thoughtSignatureStore = new Map<string, string>() |
| 37 | + |
| 38 | +/** |
| 39 | + * Get thought signature for a tool call ID |
| 40 | + */ |
| 41 | +export function getThoughtSignature(toolCallId: string): string | undefined { |
| 42 | + return thoughtSignatureStore.get(toolCallId) |
| 43 | +} |
| 44 | + |
| 45 | +/** |
| 46 | + * Clear thought signatures (call after they've been used) |
| 47 | + */ |
| 48 | +export function clearThoughtSignatures(): void { |
| 49 | + thoughtSignatureStore.clear() |
| 50 | +} |
| 51 | + |
| 52 | +/** |
| 53 | + * Create a metadata extractor that captures _thought_signature from tool calls |
| 54 | + */ |
| 55 | +function createThoughtSignatureExtractor() { |
| 56 | + return { |
| 57 | + extractMetadata: async ({ parsedBody }: { parsedBody: unknown }) => { |
| 58 | + const body = parsedBody as { |
| 59 | + choices?: Array<{ |
| 60 | + message?: { |
| 61 | + tool_calls?: Array<{ |
| 62 | + id?: string |
| 63 | + _thought_signature?: string |
| 64 | + }> |
| 65 | + } |
| 66 | + }> |
| 67 | + } |
| 68 | + |
| 69 | + // Extract thought signatures from tool calls in non-streaming response |
| 70 | + const toolCalls = body?.choices?.[0]?.message?.tool_calls |
| 71 | + if (toolCalls) { |
| 72 | + for (const tc of toolCalls) { |
| 73 | + if (tc.id && tc._thought_signature) { |
| 74 | + thoughtSignatureStore.set(tc.id, tc._thought_signature) |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + // Return metadata with thought signatures for this response |
| 80 | + const signatures: Record<string, string> = {} |
| 81 | + if (toolCalls) { |
| 82 | + for (const tc of toolCalls) { |
| 83 | + if (tc.id && tc._thought_signature) { |
| 84 | + signatures[tc.id] = tc._thought_signature |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + if (Object.keys(signatures).length > 0) { |
| 90 | + return { |
| 91 | + qbraid: { |
| 92 | + thoughtSignatures: signatures, |
| 93 | + }, |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + return undefined |
| 98 | + }, |
| 99 | + |
| 100 | + createStreamExtractor: () => { |
| 101 | + const signatures: Record<string, string> = {} |
| 102 | + |
| 103 | + return { |
| 104 | + processChunk(parsedChunk: unknown): void { |
| 105 | + const chunk = parsedChunk as { |
| 106 | + choices?: Array<{ |
| 107 | + delta?: { |
| 108 | + tool_calls?: Array<{ |
| 109 | + index?: number |
| 110 | + id?: string |
| 111 | + _thought_signature?: string |
| 112 | + }> |
| 113 | + } |
| 114 | + }> |
| 115 | + } |
| 116 | + |
| 117 | + // Extract thought signatures from streaming tool call deltas |
| 118 | + const toolCalls = chunk?.choices?.[0]?.delta?.tool_calls |
| 119 | + if (toolCalls) { |
| 120 | + for (const tc of toolCalls) { |
| 121 | + if (tc.id && tc._thought_signature) { |
| 122 | + signatures[tc.id] = tc._thought_signature |
| 123 | + thoughtSignatureStore.set(tc.id, tc._thought_signature) |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + }, |
| 128 | + |
| 129 | + buildMetadata() { |
| 130 | + if (Object.keys(signatures).length > 0) { |
| 131 | + return { |
| 132 | + qbraid: { |
| 133 | + thoughtSignatures: signatures, |
| 134 | + }, |
| 135 | + } |
| 136 | + } |
| 137 | + return undefined |
| 138 | + }, |
| 139 | + } |
| 140 | + }, |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +/** |
| 145 | + * Create a qBraid provider instance. |
| 146 | + * |
| 147 | + * This provider uses @ai-sdk/openai-compatible but adds a custom metadata extractor |
| 148 | + * to capture Gemini 3 thought signatures from tool calls. |
| 149 | + */ |
| 150 | +export function createQBraid(options: QBraidProviderSettings = {}): (modelId: string) => LanguageModelV2 { |
| 151 | + const baseURL = withoutTrailingSlash(options.baseURL ?? "https://api.qbraid.com/ai/v1") |
| 152 | + |
| 153 | + const headers = { |
| 154 | + ...(options.apiKey && { Authorization: `Bearer ${options.apiKey}` }), |
| 155 | + ...options.headers, |
| 156 | + } |
| 157 | + |
| 158 | + const metadataExtractor = createThoughtSignatureExtractor() |
| 159 | + |
| 160 | + // Return a function that creates language models with our custom metadata extractor |
| 161 | + const provider = (modelId: string): LanguageModelV2 => { |
| 162 | + return new OpenAICompatibleChatLanguageModel(modelId, { |
| 163 | + provider: "qbraid.chat", |
| 164 | + headers: () => headers, |
| 165 | + url: ({ path }) => `${baseURL}${path}`, |
| 166 | + fetch: options.fetch, |
| 167 | + metadataExtractor, |
| 168 | + }) |
| 169 | + } |
| 170 | + |
| 171 | + // Add commonly expected methods for compatibility |
| 172 | + ;(provider as any).languageModel = provider |
| 173 | + ;(provider as any).chat = provider |
| 174 | + ;(provider as any).chatModel = provider |
| 175 | + |
| 176 | + return provider |
| 177 | +} |
| 178 | + |
| 179 | +export default createQBraid |
0 commit comments