@h-rig/pi-ai 0.0.6-alpha.23

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 (218) hide show
  1. package/README.md +1391 -0
  2. package/dist/api-registry.d.ts +20 -0
  3. package/dist/api-registry.d.ts.map +1 -0
  4. package/dist/api-registry.js +44 -0
  5. package/dist/api-registry.js.map +1 -0
  6. package/dist/bedrock-provider.d.ts +5 -0
  7. package/dist/bedrock-provider.d.ts.map +1 -0
  8. package/dist/bedrock-provider.js +6 -0
  9. package/dist/bedrock-provider.js.map +1 -0
  10. package/dist/cli.d.ts +3 -0
  11. package/dist/cli.d.ts.map +1 -0
  12. package/dist/cli.js +130 -0
  13. package/dist/cli.js.map +1 -0
  14. package/dist/env-api-keys.d.ts +18 -0
  15. package/dist/env-api-keys.d.ts.map +1 -0
  16. package/dist/env-api-keys.js +181 -0
  17. package/dist/env-api-keys.js.map +1 -0
  18. package/dist/image-models.d.ts +10 -0
  19. package/dist/image-models.d.ts.map +1 -0
  20. package/dist/image-models.generated.d.ts +485 -0
  21. package/dist/image-models.generated.d.ts.map +1 -0
  22. package/dist/image-models.generated.js +487 -0
  23. package/dist/image-models.generated.js.map +1 -0
  24. package/dist/image-models.js +23 -0
  25. package/dist/image-models.js.map +1 -0
  26. package/dist/images-api-registry.d.ts +14 -0
  27. package/dist/images-api-registry.d.ts.map +1 -0
  28. package/dist/images-api-registry.js +22 -0
  29. package/dist/images-api-registry.js.map +1 -0
  30. package/dist/images.d.ts +4 -0
  31. package/dist/images.d.ts.map +1 -0
  32. package/dist/images.js +14 -0
  33. package/dist/images.js.map +1 -0
  34. package/dist/index.d.ts +32 -0
  35. package/dist/index.d.ts.map +1 -0
  36. package/dist/index.js +20 -0
  37. package/dist/index.js.map +1 -0
  38. package/dist/models.d.ts +18 -0
  39. package/dist/models.d.ts.map +1 -0
  40. package/dist/models.generated.d.ts +18411 -0
  41. package/dist/models.generated.d.ts.map +1 -0
  42. package/dist/models.generated.js +16944 -0
  43. package/dist/models.generated.js.map +1 -0
  44. package/dist/models.js +71 -0
  45. package/dist/models.js.map +1 -0
  46. package/dist/oauth.d.ts +2 -0
  47. package/dist/oauth.d.ts.map +1 -0
  48. package/dist/oauth.js +2 -0
  49. package/dist/oauth.js.map +1 -0
  50. package/dist/providers/amazon-bedrock.d.ts +38 -0
  51. package/dist/providers/amazon-bedrock.d.ts.map +1 -0
  52. package/dist/providers/amazon-bedrock.js +826 -0
  53. package/dist/providers/amazon-bedrock.js.map +1 -0
  54. package/dist/providers/anthropic.d.ts +71 -0
  55. package/dist/providers/anthropic.d.ts.map +1 -0
  56. package/dist/providers/anthropic.js +959 -0
  57. package/dist/providers/anthropic.js.map +1 -0
  58. package/dist/providers/azure-openai-responses.d.ts +15 -0
  59. package/dist/providers/azure-openai-responses.d.ts.map +1 -0
  60. package/dist/providers/azure-openai-responses.js +221 -0
  61. package/dist/providers/azure-openai-responses.js.map +1 -0
  62. package/dist/providers/cloudflare.d.ts +13 -0
  63. package/dist/providers/cloudflare.d.ts.map +1 -0
  64. package/dist/providers/cloudflare.js +26 -0
  65. package/dist/providers/cloudflare.js.map +1 -0
  66. package/dist/providers/faux.d.ts +56 -0
  67. package/dist/providers/faux.d.ts.map +1 -0
  68. package/dist/providers/faux.js +368 -0
  69. package/dist/providers/faux.js.map +1 -0
  70. package/dist/providers/github-copilot-headers.d.ts +8 -0
  71. package/dist/providers/github-copilot-headers.d.ts.map +1 -0
  72. package/dist/providers/github-copilot-headers.js +29 -0
  73. package/dist/providers/github-copilot-headers.js.map +1 -0
  74. package/dist/providers/google-shared.d.ts +70 -0
  75. package/dist/providers/google-shared.d.ts.map +1 -0
  76. package/dist/providers/google-shared.js +329 -0
  77. package/dist/providers/google-shared.js.map +1 -0
  78. package/dist/providers/google-vertex.d.ts +15 -0
  79. package/dist/providers/google-vertex.d.ts.map +1 -0
  80. package/dist/providers/google-vertex.js +442 -0
  81. package/dist/providers/google-vertex.js.map +1 -0
  82. package/dist/providers/google.d.ts +13 -0
  83. package/dist/providers/google.d.ts.map +1 -0
  84. package/dist/providers/google.js +402 -0
  85. package/dist/providers/google.js.map +1 -0
  86. package/dist/providers/images/openrouter.d.ts +3 -0
  87. package/dist/providers/images/openrouter.d.ts.map +1 -0
  88. package/dist/providers/images/openrouter.js +128 -0
  89. package/dist/providers/images/openrouter.js.map +1 -0
  90. package/dist/providers/images/register-builtins.d.ts +4 -0
  91. package/dist/providers/images/register-builtins.d.ts.map +1 -0
  92. package/dist/providers/images/register-builtins.js +34 -0
  93. package/dist/providers/images/register-builtins.js.map +1 -0
  94. package/dist/providers/mistral.d.ts +25 -0
  95. package/dist/providers/mistral.d.ts.map +1 -0
  96. package/dist/providers/mistral.js +534 -0
  97. package/dist/providers/mistral.js.map +1 -0
  98. package/dist/providers/openai-codex-responses.d.ts +30 -0
  99. package/dist/providers/openai-codex-responses.d.ts.map +1 -0
  100. package/dist/providers/openai-codex-responses.js +1171 -0
  101. package/dist/providers/openai-codex-responses.js.map +1 -0
  102. package/dist/providers/openai-completions.d.ts +19 -0
  103. package/dist/providers/openai-completions.d.ts.map +1 -0
  104. package/dist/providers/openai-completions.js +976 -0
  105. package/dist/providers/openai-completions.js.map +1 -0
  106. package/dist/providers/openai-prompt-cache.d.ts +3 -0
  107. package/dist/providers/openai-prompt-cache.d.ts.map +1 -0
  108. package/dist/providers/openai-prompt-cache.js +10 -0
  109. package/dist/providers/openai-prompt-cache.js.map +1 -0
  110. package/dist/providers/openai-responses-shared.d.ts +18 -0
  111. package/dist/providers/openai-responses-shared.d.ts.map +1 -0
  112. package/dist/providers/openai-responses-shared.js +496 -0
  113. package/dist/providers/openai-responses-shared.js.map +1 -0
  114. package/dist/providers/openai-responses.d.ts +13 -0
  115. package/dist/providers/openai-responses.d.ts.map +1 -0
  116. package/dist/providers/openai-responses.js +234 -0
  117. package/dist/providers/openai-responses.js.map +1 -0
  118. package/dist/providers/register-builtins.d.ts +35 -0
  119. package/dist/providers/register-builtins.d.ts.map +1 -0
  120. package/dist/providers/register-builtins.js +254 -0
  121. package/dist/providers/register-builtins.js.map +1 -0
  122. package/dist/providers/simple-options.d.ts +8 -0
  123. package/dist/providers/simple-options.d.ts.map +1 -0
  124. package/dist/providers/simple-options.js +42 -0
  125. package/dist/providers/simple-options.js.map +1 -0
  126. package/dist/providers/transform-messages.d.ts +8 -0
  127. package/dist/providers/transform-messages.d.ts.map +1 -0
  128. package/dist/providers/transform-messages.js +184 -0
  129. package/dist/providers/transform-messages.js.map +1 -0
  130. package/dist/session-resources.d.ts +4 -0
  131. package/dist/session-resources.d.ts.map +1 -0
  132. package/dist/session-resources.js +22 -0
  133. package/dist/session-resources.js.map +1 -0
  134. package/dist/stream.d.ts +8 -0
  135. package/dist/stream.d.ts.map +1 -0
  136. package/dist/stream.js +39 -0
  137. package/dist/stream.js.map +1 -0
  138. package/dist/types.d.ts +516 -0
  139. package/dist/types.d.ts.map +1 -0
  140. package/dist/types.js +2 -0
  141. package/dist/types.js.map +1 -0
  142. package/dist/utils/abort-signals.d.ts +6 -0
  143. package/dist/utils/abort-signals.d.ts.map +1 -0
  144. package/dist/utils/abort-signals.js +34 -0
  145. package/dist/utils/abort-signals.js.map +1 -0
  146. package/dist/utils/diagnostics.d.ts +19 -0
  147. package/dist/utils/diagnostics.d.ts.map +1 -0
  148. package/dist/utils/diagnostics.js +25 -0
  149. package/dist/utils/diagnostics.js.map +1 -0
  150. package/dist/utils/event-stream.d.ts +21 -0
  151. package/dist/utils/event-stream.d.ts.map +1 -0
  152. package/dist/utils/event-stream.js +81 -0
  153. package/dist/utils/event-stream.js.map +1 -0
  154. package/dist/utils/hash.d.ts +3 -0
  155. package/dist/utils/hash.d.ts.map +1 -0
  156. package/dist/utils/hash.js +14 -0
  157. package/dist/utils/hash.js.map +1 -0
  158. package/dist/utils/headers.d.ts +2 -0
  159. package/dist/utils/headers.d.ts.map +1 -0
  160. package/dist/utils/headers.js +8 -0
  161. package/dist/utils/headers.js.map +1 -0
  162. package/dist/utils/json-parse.d.ts +16 -0
  163. package/dist/utils/json-parse.d.ts.map +1 -0
  164. package/dist/utils/json-parse.js +113 -0
  165. package/dist/utils/json-parse.js.map +1 -0
  166. package/dist/utils/node-http-proxy.d.ts +10 -0
  167. package/dist/utils/node-http-proxy.d.ts.map +1 -0
  168. package/dist/utils/node-http-proxy.js +97 -0
  169. package/dist/utils/node-http-proxy.js.map +1 -0
  170. package/dist/utils/oauth/anthropic.d.ts +25 -0
  171. package/dist/utils/oauth/anthropic.d.ts.map +1 -0
  172. package/dist/utils/oauth/anthropic.js +335 -0
  173. package/dist/utils/oauth/anthropic.js.map +1 -0
  174. package/dist/utils/oauth/device-code.d.ts +21 -0
  175. package/dist/utils/oauth/device-code.d.ts.map +1 -0
  176. package/dist/utils/oauth/device-code.js +56 -0
  177. package/dist/utils/oauth/device-code.js.map +1 -0
  178. package/dist/utils/oauth/github-copilot.d.ts +30 -0
  179. package/dist/utils/oauth/github-copilot.d.ts.map +1 -0
  180. package/dist/utils/oauth/github-copilot.js +280 -0
  181. package/dist/utils/oauth/github-copilot.js.map +1 -0
  182. package/dist/utils/oauth/index.d.ts +58 -0
  183. package/dist/utils/oauth/index.d.ts.map +1 -0
  184. package/dist/utils/oauth/index.js +122 -0
  185. package/dist/utils/oauth/index.js.map +1 -0
  186. package/dist/utils/oauth/oauth-page.d.ts +3 -0
  187. package/dist/utils/oauth/oauth-page.d.ts.map +1 -0
  188. package/dist/utils/oauth/oauth-page.js +105 -0
  189. package/dist/utils/oauth/oauth-page.js.map +1 -0
  190. package/dist/utils/oauth/openai-codex.d.ts +43 -0
  191. package/dist/utils/oauth/openai-codex.d.ts.map +1 -0
  192. package/dist/utils/oauth/openai-codex.js +487 -0
  193. package/dist/utils/oauth/openai-codex.js.map +1 -0
  194. package/dist/utils/oauth/pkce.d.ts +13 -0
  195. package/dist/utils/oauth/pkce.d.ts.map +1 -0
  196. package/dist/utils/oauth/pkce.js +31 -0
  197. package/dist/utils/oauth/pkce.js.map +1 -0
  198. package/dist/utils/oauth/types.d.ts +64 -0
  199. package/dist/utils/oauth/types.d.ts.map +1 -0
  200. package/dist/utils/oauth/types.js +2 -0
  201. package/dist/utils/oauth/types.js.map +1 -0
  202. package/dist/utils/overflow.d.ts +57 -0
  203. package/dist/utils/overflow.d.ts.map +1 -0
  204. package/dist/utils/overflow.js +154 -0
  205. package/dist/utils/overflow.js.map +1 -0
  206. package/dist/utils/sanitize-unicode.d.ts +22 -0
  207. package/dist/utils/sanitize-unicode.d.ts.map +1 -0
  208. package/dist/utils/sanitize-unicode.js +26 -0
  209. package/dist/utils/sanitize-unicode.js.map +1 -0
  210. package/dist/utils/typebox-helpers.d.ts +17 -0
  211. package/dist/utils/typebox-helpers.d.ts.map +1 -0
  212. package/dist/utils/typebox-helpers.js +21 -0
  213. package/dist/utils/typebox-helpers.js.map +1 -0
  214. package/dist/utils/validation.d.ts +18 -0
  215. package/dist/utils/validation.d.ts.map +1 -0
  216. package/dist/utils/validation.js +281 -0
  217. package/dist/utils/validation.js.map +1 -0
  218. package/package.json +93 -0
@@ -0,0 +1,402 @@
1
+ import { GoogleGenAI, } from "@google/genai";
2
+ import { calculateCost, clampThinkingLevel } from "../models.js";
3
+ import { AssistantMessageEventStream } from "../utils/event-stream.js";
4
+ import { sanitizeSurrogates } from "../utils/sanitize-unicode.js";
5
+ import { convertMessages, convertTools, isThinkingPart, mapStopReason, mapToolChoice, retainThoughtSignature, } from "./google-shared.js";
6
+ import { buildBaseOptions } from "./simple-options.js";
7
+ // Counter for generating unique tool call IDs
8
+ let toolCallCounter = 0;
9
+ export const streamGoogle = (model, context, options) => {
10
+ const stream = new AssistantMessageEventStream();
11
+ (async () => {
12
+ const output = {
13
+ role: "assistant",
14
+ content: [],
15
+ api: "google-generative-ai",
16
+ provider: model.provider,
17
+ model: model.id,
18
+ usage: {
19
+ input: 0,
20
+ output: 0,
21
+ cacheRead: 0,
22
+ cacheWrite: 0,
23
+ totalTokens: 0,
24
+ cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
25
+ },
26
+ stopReason: "stop",
27
+ timestamp: Date.now(),
28
+ };
29
+ try {
30
+ const apiKey = options?.apiKey;
31
+ if (!apiKey) {
32
+ throw new Error(`No API key for provider: ${model.provider}`);
33
+ }
34
+ const client = createClient(model, apiKey, options?.headers);
35
+ let params = buildParams(model, context, options);
36
+ const nextParams = await options?.onPayload?.(params, model);
37
+ if (nextParams !== undefined) {
38
+ params = nextParams;
39
+ }
40
+ const googleStream = await client.models.generateContentStream(params);
41
+ stream.push({ type: "start", partial: output });
42
+ let currentBlock = null;
43
+ const blocks = output.content;
44
+ const blockIndex = () => blocks.length - 1;
45
+ for await (const chunk of googleStream) {
46
+ // @google/genai documents GenerateContentResponse.responseId as an output-only field
47
+ // used to identify each response. Keep the first non-empty one from the stream.
48
+ output.responseId ||= chunk.responseId;
49
+ const candidate = chunk.candidates?.[0];
50
+ if (candidate?.content?.parts) {
51
+ for (const part of candidate.content.parts) {
52
+ if (part.text !== undefined) {
53
+ const isThinking = isThinkingPart(part);
54
+ if (!currentBlock ||
55
+ (isThinking && currentBlock.type !== "thinking") ||
56
+ (!isThinking && currentBlock.type !== "text")) {
57
+ if (currentBlock) {
58
+ if (currentBlock.type === "text") {
59
+ stream.push({
60
+ type: "text_end",
61
+ contentIndex: blocks.length - 1,
62
+ content: currentBlock.text,
63
+ partial: output,
64
+ });
65
+ }
66
+ else {
67
+ stream.push({
68
+ type: "thinking_end",
69
+ contentIndex: blockIndex(),
70
+ content: currentBlock.thinking,
71
+ partial: output,
72
+ });
73
+ }
74
+ }
75
+ if (isThinking) {
76
+ currentBlock = { type: "thinking", thinking: "", thinkingSignature: undefined };
77
+ output.content.push(currentBlock);
78
+ stream.push({ type: "thinking_start", contentIndex: blockIndex(), partial: output });
79
+ }
80
+ else {
81
+ currentBlock = { type: "text", text: "" };
82
+ output.content.push(currentBlock);
83
+ stream.push({ type: "text_start", contentIndex: blockIndex(), partial: output });
84
+ }
85
+ }
86
+ if (currentBlock.type === "thinking") {
87
+ currentBlock.thinking += part.text;
88
+ currentBlock.thinkingSignature = retainThoughtSignature(currentBlock.thinkingSignature, part.thoughtSignature);
89
+ stream.push({
90
+ type: "thinking_delta",
91
+ contentIndex: blockIndex(),
92
+ delta: part.text,
93
+ partial: output,
94
+ });
95
+ }
96
+ else {
97
+ currentBlock.text += part.text;
98
+ currentBlock.textSignature = retainThoughtSignature(currentBlock.textSignature, part.thoughtSignature);
99
+ stream.push({
100
+ type: "text_delta",
101
+ contentIndex: blockIndex(),
102
+ delta: part.text,
103
+ partial: output,
104
+ });
105
+ }
106
+ }
107
+ if (part.functionCall) {
108
+ if (currentBlock) {
109
+ if (currentBlock.type === "text") {
110
+ stream.push({
111
+ type: "text_end",
112
+ contentIndex: blockIndex(),
113
+ content: currentBlock.text,
114
+ partial: output,
115
+ });
116
+ }
117
+ else {
118
+ stream.push({
119
+ type: "thinking_end",
120
+ contentIndex: blockIndex(),
121
+ content: currentBlock.thinking,
122
+ partial: output,
123
+ });
124
+ }
125
+ currentBlock = null;
126
+ }
127
+ // Generate unique ID if not provided or if it's a duplicate
128
+ const providedId = part.functionCall.id;
129
+ const needsNewId = !providedId || output.content.some((b) => b.type === "toolCall" && b.id === providedId);
130
+ const toolCallId = needsNewId
131
+ ? `${part.functionCall.name}_${Date.now()}_${++toolCallCounter}`
132
+ : providedId;
133
+ const toolCall = {
134
+ type: "toolCall",
135
+ id: toolCallId,
136
+ name: part.functionCall.name || "",
137
+ arguments: part.functionCall.args ?? {},
138
+ ...(part.thoughtSignature && { thoughtSignature: part.thoughtSignature }),
139
+ };
140
+ output.content.push(toolCall);
141
+ stream.push({ type: "toolcall_start", contentIndex: blockIndex(), partial: output });
142
+ stream.push({
143
+ type: "toolcall_delta",
144
+ contentIndex: blockIndex(),
145
+ delta: JSON.stringify(toolCall.arguments),
146
+ partial: output,
147
+ });
148
+ stream.push({ type: "toolcall_end", contentIndex: blockIndex(), toolCall, partial: output });
149
+ }
150
+ }
151
+ }
152
+ if (candidate?.finishReason) {
153
+ output.stopReason = mapStopReason(candidate.finishReason);
154
+ if (output.content.some((b) => b.type === "toolCall")) {
155
+ output.stopReason = "toolUse";
156
+ }
157
+ }
158
+ if (chunk.usageMetadata) {
159
+ output.usage = {
160
+ input: (chunk.usageMetadata.promptTokenCount || 0) - (chunk.usageMetadata.cachedContentTokenCount || 0),
161
+ output: (chunk.usageMetadata.candidatesTokenCount || 0) + (chunk.usageMetadata.thoughtsTokenCount || 0),
162
+ cacheRead: chunk.usageMetadata.cachedContentTokenCount || 0,
163
+ cacheWrite: 0,
164
+ totalTokens: chunk.usageMetadata.totalTokenCount || 0,
165
+ cost: {
166
+ input: 0,
167
+ output: 0,
168
+ cacheRead: 0,
169
+ cacheWrite: 0,
170
+ total: 0,
171
+ },
172
+ };
173
+ calculateCost(model, output.usage);
174
+ }
175
+ }
176
+ if (currentBlock) {
177
+ if (currentBlock.type === "text") {
178
+ stream.push({
179
+ type: "text_end",
180
+ contentIndex: blockIndex(),
181
+ content: currentBlock.text,
182
+ partial: output,
183
+ });
184
+ }
185
+ else {
186
+ stream.push({
187
+ type: "thinking_end",
188
+ contentIndex: blockIndex(),
189
+ content: currentBlock.thinking,
190
+ partial: output,
191
+ });
192
+ }
193
+ }
194
+ if (options?.signal?.aborted) {
195
+ throw new Error("Request was aborted");
196
+ }
197
+ if (output.stopReason === "aborted" || output.stopReason === "error") {
198
+ throw new Error("An unknown error occurred");
199
+ }
200
+ stream.push({ type: "done", reason: output.stopReason, message: output });
201
+ stream.end();
202
+ }
203
+ catch (error) {
204
+ // Remove internal index property used during streaming
205
+ for (const block of output.content) {
206
+ if ("index" in block) {
207
+ delete block.index;
208
+ }
209
+ }
210
+ output.stopReason = options?.signal?.aborted ? "aborted" : "error";
211
+ output.errorMessage = error instanceof Error ? error.message : JSON.stringify(error);
212
+ stream.push({ type: "error", reason: output.stopReason, error: output });
213
+ stream.end();
214
+ }
215
+ })();
216
+ return stream;
217
+ };
218
+ export const streamSimpleGoogle = (model, context, options) => {
219
+ const apiKey = options?.apiKey;
220
+ if (!apiKey) {
221
+ throw new Error(`No API key for provider: ${model.provider}`);
222
+ }
223
+ const base = buildBaseOptions(model, options, apiKey);
224
+ if (!options?.reasoning) {
225
+ return streamGoogle(model, context, { ...base, thinking: { enabled: false } });
226
+ }
227
+ const clampedReasoning = clampThinkingLevel(model, options.reasoning);
228
+ const effort = (clampedReasoning === "off" ? "high" : clampedReasoning);
229
+ const googleModel = model;
230
+ if (isGemini3ProModel(googleModel) || isGemini3FlashModel(googleModel) || isGemma4Model(googleModel)) {
231
+ return streamGoogle(model, context, {
232
+ ...base,
233
+ thinking: {
234
+ enabled: true,
235
+ level: getThinkingLevel(effort, googleModel),
236
+ },
237
+ });
238
+ }
239
+ return streamGoogle(model, context, {
240
+ ...base,
241
+ thinking: {
242
+ enabled: true,
243
+ budgetTokens: getGoogleBudget(googleModel, effort, options.thinkingBudgets),
244
+ },
245
+ });
246
+ };
247
+ function createClient(model, apiKey, optionsHeaders) {
248
+ const httpOptions = {};
249
+ if (model.baseUrl) {
250
+ httpOptions.baseUrl = model.baseUrl;
251
+ httpOptions.apiVersion = ""; // baseUrl already includes version path, don't append
252
+ }
253
+ if (model.headers || optionsHeaders) {
254
+ httpOptions.headers = { ...model.headers, ...optionsHeaders };
255
+ }
256
+ return new GoogleGenAI({
257
+ apiKey,
258
+ httpOptions: Object.keys(httpOptions).length > 0 ? httpOptions : undefined,
259
+ });
260
+ }
261
+ function buildParams(model, context, options = {}) {
262
+ const contents = convertMessages(model, context);
263
+ const generationConfig = {};
264
+ if (options.temperature !== undefined) {
265
+ generationConfig.temperature = options.temperature;
266
+ }
267
+ if (options.maxTokens !== undefined) {
268
+ generationConfig.maxOutputTokens = options.maxTokens;
269
+ }
270
+ const config = {
271
+ ...(Object.keys(generationConfig).length > 0 && generationConfig),
272
+ ...(context.systemPrompt && { systemInstruction: sanitizeSurrogates(context.systemPrompt) }),
273
+ ...(context.tools && context.tools.length > 0 && { tools: convertTools(context.tools) }),
274
+ };
275
+ if (context.tools && context.tools.length > 0 && options.toolChoice) {
276
+ config.toolConfig = {
277
+ functionCallingConfig: {
278
+ mode: mapToolChoice(options.toolChoice),
279
+ },
280
+ };
281
+ }
282
+ else {
283
+ config.toolConfig = undefined;
284
+ }
285
+ if (options.thinking?.enabled && model.reasoning) {
286
+ const thinkingConfig = { includeThoughts: true };
287
+ if (options.thinking.level !== undefined) {
288
+ // Cast to any since our GoogleThinkingLevel mirrors Google's ThinkingLevel enum values
289
+ thinkingConfig.thinkingLevel = options.thinking.level;
290
+ }
291
+ else if (options.thinking.budgetTokens !== undefined) {
292
+ thinkingConfig.thinkingBudget = options.thinking.budgetTokens;
293
+ }
294
+ config.thinkingConfig = thinkingConfig;
295
+ }
296
+ else if (model.reasoning && options.thinking && !options.thinking.enabled) {
297
+ config.thinkingConfig = getDisabledThinkingConfig(model);
298
+ }
299
+ if (options.signal) {
300
+ if (options.signal.aborted) {
301
+ throw new Error("Request aborted");
302
+ }
303
+ config.abortSignal = options.signal;
304
+ }
305
+ const params = {
306
+ model: model.id,
307
+ contents,
308
+ config,
309
+ };
310
+ return params;
311
+ }
312
+ function isGemma4Model(model) {
313
+ return /gemma-?4/.test(model.id.toLowerCase());
314
+ }
315
+ function isGemini3ProModel(model) {
316
+ return /gemini-3(?:\.\d+)?-pro/.test(model.id.toLowerCase());
317
+ }
318
+ function isGemini3FlashModel(model) {
319
+ return /gemini-3(?:\.\d+)?-flash/.test(model.id.toLowerCase());
320
+ }
321
+ function getDisabledThinkingConfig(model) {
322
+ // Google docs: Gemini 3.1 Pro cannot disable thinking, and Gemini 3 Flash / Flash-Lite
323
+ // do not support full thinking-off either. For Gemini 3 models, use the lowest supported
324
+ // thinkingLevel without includeThoughts so hidden thinking remains invisible to pi.
325
+ if (isGemini3ProModel(model)) {
326
+ return { thinkingLevel: "LOW" };
327
+ }
328
+ if (isGemini3FlashModel(model)) {
329
+ return { thinkingLevel: "MINIMAL" };
330
+ }
331
+ if (isGemma4Model(model)) {
332
+ return { thinkingLevel: "MINIMAL" };
333
+ }
334
+ // Gemini 2.x supports disabling via thinkingBudget = 0.
335
+ return { thinkingBudget: 0 };
336
+ }
337
+ function getThinkingLevel(effort, model) {
338
+ if (isGemini3ProModel(model)) {
339
+ switch (effort) {
340
+ case "minimal":
341
+ case "low":
342
+ return "LOW";
343
+ case "medium":
344
+ case "high":
345
+ return "HIGH";
346
+ }
347
+ }
348
+ if (isGemma4Model(model)) {
349
+ switch (effort) {
350
+ case "minimal":
351
+ case "low":
352
+ return "MINIMAL";
353
+ case "medium":
354
+ case "high":
355
+ return "HIGH";
356
+ }
357
+ }
358
+ switch (effort) {
359
+ case "minimal":
360
+ return "MINIMAL";
361
+ case "low":
362
+ return "LOW";
363
+ case "medium":
364
+ return "MEDIUM";
365
+ case "high":
366
+ return "HIGH";
367
+ }
368
+ }
369
+ function getGoogleBudget(model, effort, customBudgets) {
370
+ if (customBudgets?.[effort] !== undefined) {
371
+ return customBudgets[effort];
372
+ }
373
+ if (model.id.includes("2.5-pro")) {
374
+ const budgets = {
375
+ minimal: 128,
376
+ low: 2048,
377
+ medium: 8192,
378
+ high: 32768,
379
+ };
380
+ return budgets[effort];
381
+ }
382
+ if (model.id.includes("2.5-flash-lite")) {
383
+ const budgets = {
384
+ minimal: 512,
385
+ low: 2048,
386
+ medium: 8192,
387
+ high: 24576,
388
+ };
389
+ return budgets[effort];
390
+ }
391
+ if (model.id.includes("2.5-flash")) {
392
+ const budgets = {
393
+ minimal: 128,
394
+ low: 2048,
395
+ medium: 8192,
396
+ high: 24576,
397
+ };
398
+ return budgets[effort];
399
+ }
400
+ return -1;
401
+ }
402
+ //# sourceMappingURL=google.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"google.js","sourceRoot":"","sources":["../../src/providers/google.ts"],"names":[],"mappings":"AAAA,OAAO,EAGN,WAAW,GAEX,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,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AO,QAAQ,CAAC;QACjB,KAAK,MAAM;YACV,OAAO,MAAM,CAAC;IAChB,CAAC;AAAA,CACD;AAED,SAAS,eAAe,CACvB,KAAoC,EACpC,MAA4B,EAC5B,aAA+B,EACtB;IACT,IAAI,aAAa,EAAE,CAAC,MAAM,CAAC,KAAK,SAAS,EAAE,CAAC;QAC3C,OAAO,aAAa,CAAC,MAAM,CAAE,CAAC;IAC/B,CAAC;IAED,IAAI,KAAK,CAAC,EAAE,CAAC,QAAQ,CAAC,SAAS,CAAC,EAAE,CAAC;QAClC,MAAM,OAAO,GAAyC;YACrD,OAAO,EAAE,GAAG;YACZ,GAAG,EAAE,IAAI;YACT,MAAM,EAAE,IAAI;YACZ,IAAI,EAAE,KAAK;SACX,CAAC;QACF,OAAO,OAAO,CAAC,MAAM,CAAC,CAAC;IACxB,CAAC;IAED,IAAI,KAAK,CAAC,EAAE,CAAC,QAAQ,CAAC,gBAAgB,CAAC,EAAE,CAAC;QACzC,MAAM,OAAO,GAAyC;YACrD,OAAO,EAAE,GAAG;YACZ,GAAG,EAAE,IAAI;YACT,MAAM,EAAE,IAAI;YACZ,IAAI,EAAE,KAAK;SACX,CAAC;QACF,OAAO,OAAO,CAAC,MAAM,CAAC,CAAC;IACxB,CAAC;IAED,IAAI,KAAK,CAAC,EAAE,CAAC,QAAQ,CAAC,WAAW,CAAC,EAAE,CAAC;QACpC,MAAM,OAAO,GAAyC;YACrD,OAAO,EAAE,GAAG;YACZ,GAAG,EAAE,IAAI;YACT,MAAM,EAAE,IAAI;YACZ,IAAI,EAAE,KAAK;SACX,CAAC;QACF,OAAO,OAAO,CAAC,MAAM,CAAC,CAAC;IACxB,CAAC;IAED,OAAO,CAAC,CAAC,CAAC;AAAA,CACV","sourcesContent":["import {\n\ttype GenerateContentConfig,\n\ttype GenerateContentParameters,\n\tGoogleGenAI,\n\ttype ThinkingConfig,\n} from \"@google/genai\";\nimport { calculateCost, clampThinkingLevel } from \"../models.ts\";\nimport type {\n\tApi,\n\tAssistantMessage,\n\tContext,\n\tModel,\n\tSimpleStreamOptions,\n\tStreamFunction,\n\tStreamOptions,\n\tTextContent,\n\tThinkingBudgets,\n\tThinkingContent,\n\tThinkingLevel,\n\tToolCall,\n} from \"../types.ts\";\nimport { AssistantMessageEventStream } from \"../utils/event-stream.ts\";\nimport { sanitizeSurrogates } from \"../utils/sanitize-unicode.ts\";\nimport type { GoogleThinkingLevel } from \"./google-shared.ts\";\nimport {\n\tconvertMessages,\n\tconvertTools,\n\tisThinkingPart,\n\tmapStopReason,\n\tmapToolChoice,\n\tretainThoughtSignature,\n} from \"./google-shared.ts\";\nimport { buildBaseOptions } from \"./simple-options.ts\";\n\nexport interface GoogleOptions extends StreamOptions {\n\ttoolChoice?: \"auto\" | \"none\" | \"any\";\n\tthinking?: {\n\t\tenabled: boolean;\n\t\tbudgetTokens?: number; // -1 for dynamic, 0 to disable\n\t\tlevel?: GoogleThinkingLevel;\n\t};\n}\n\n// Counter for generating unique tool call IDs\nlet toolCallCounter = 0;\n\nexport const streamGoogle: StreamFunction<\"google-generative-ai\", GoogleOptions> = (\n\tmodel: Model<\"google-generative-ai\">,\n\tcontext: Context,\n\toptions?: GoogleOptions,\n): AssistantMessageEventStream => {\n\tconst stream = new AssistantMessageEventStream();\n\n\t(async () => {\n\t\tconst output: AssistantMessage = {\n\t\t\trole: \"assistant\",\n\t\t\tcontent: [],\n\t\t\tapi: \"google-generative-ai\" as Api,\n\t\t\tprovider: model.provider,\n\t\t\tmodel: model.id,\n\t\t\tusage: {\n\t\t\t\tinput: 0,\n\t\t\t\toutput: 0,\n\t\t\t\tcacheRead: 0,\n\t\t\t\tcacheWrite: 0,\n\t\t\t\ttotalTokens: 0,\n\t\t\t\tcost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },\n\t\t\t},\n\t\t\tstopReason: \"stop\",\n\t\t\ttimestamp: Date.now(),\n\t\t};\n\n\t\ttry {\n\t\t\tconst apiKey = options?.apiKey;\n\t\t\tif (!apiKey) {\n\t\t\t\tthrow new Error(`No API key for provider: ${model.provider}`);\n\t\t\t}\n\t\t\tconst client = createClient(model, apiKey, options?.headers);\n\t\t\tlet params = buildParams(model, context, options);\n\t\t\tconst nextParams = await options?.onPayload?.(params, model);\n\t\t\tif (nextParams !== undefined) {\n\t\t\t\tparams = nextParams as GenerateContentParameters;\n\t\t\t}\n\t\t\tconst googleStream = await client.models.generateContentStream(params);\n\n\t\t\tstream.push({ type: \"start\", partial: output });\n\t\t\tlet currentBlock: TextContent | ThinkingContent | null = null;\n\t\t\tconst blocks = output.content;\n\t\t\tconst blockIndex = () => blocks.length - 1;\n\t\t\tfor await (const chunk of googleStream) {\n\t\t\t\t// @google/genai documents GenerateContentResponse.responseId as an output-only field\n\t\t\t\t// used to identify each response. Keep the first non-empty one from the stream.\n\t\t\t\toutput.responseId ||= chunk.responseId;\n\t\t\t\tconst candidate = chunk.candidates?.[0];\n\t\t\t\tif (candidate?.content?.parts) {\n\t\t\t\t\tfor (const part of candidate.content.parts) {\n\t\t\t\t\t\tif (part.text !== undefined) {\n\t\t\t\t\t\t\tconst isThinking = isThinkingPart(part);\n\t\t\t\t\t\t\tif (\n\t\t\t\t\t\t\t\t!currentBlock ||\n\t\t\t\t\t\t\t\t(isThinking && currentBlock.type !== \"thinking\") ||\n\t\t\t\t\t\t\t\t(!isThinking && currentBlock.type !== \"text\")\n\t\t\t\t\t\t\t) {\n\t\t\t\t\t\t\t\tif (currentBlock) {\n\t\t\t\t\t\t\t\t\tif (currentBlock.type === \"text\") {\n\t\t\t\t\t\t\t\t\t\tstream.push({\n\t\t\t\t\t\t\t\t\t\t\ttype: \"text_end\",\n\t\t\t\t\t\t\t\t\t\t\tcontentIndex: blocks.length - 1,\n\t\t\t\t\t\t\t\t\t\t\tcontent: currentBlock.text,\n\t\t\t\t\t\t\t\t\t\t\tpartial: output,\n\t\t\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\t\t\tstream.push({\n\t\t\t\t\t\t\t\t\t\t\ttype: \"thinking_end\",\n\t\t\t\t\t\t\t\t\t\t\tcontentIndex: blockIndex(),\n\t\t\t\t\t\t\t\t\t\t\tcontent: currentBlock.thinking,\n\t\t\t\t\t\t\t\t\t\t\tpartial: output,\n\t\t\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tif (isThinking) {\n\t\t\t\t\t\t\t\t\tcurrentBlock = { type: \"thinking\", thinking: \"\", thinkingSignature: undefined };\n\t\t\t\t\t\t\t\t\toutput.content.push(currentBlock);\n\t\t\t\t\t\t\t\t\tstream.push({ type: \"thinking_start\", contentIndex: blockIndex(), partial: output });\n\t\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\t\tcurrentBlock = { type: \"text\", text: \"\" };\n\t\t\t\t\t\t\t\t\toutput.content.push(currentBlock);\n\t\t\t\t\t\t\t\t\tstream.push({ type: \"text_start\", contentIndex: blockIndex(), partial: output });\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tif (currentBlock.type === \"thinking\") {\n\t\t\t\t\t\t\t\tcurrentBlock.thinking += part.text;\n\t\t\t\t\t\t\t\tcurrentBlock.thinkingSignature = retainThoughtSignature(\n\t\t\t\t\t\t\t\t\tcurrentBlock.thinkingSignature,\n\t\t\t\t\t\t\t\t\tpart.thoughtSignature,\n\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t\tstream.push({\n\t\t\t\t\t\t\t\t\ttype: \"thinking_delta\",\n\t\t\t\t\t\t\t\t\tcontentIndex: blockIndex(),\n\t\t\t\t\t\t\t\t\tdelta: part.text,\n\t\t\t\t\t\t\t\t\tpartial: output,\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\tcurrentBlock.text += part.text;\n\t\t\t\t\t\t\t\tcurrentBlock.textSignature = retainThoughtSignature(\n\t\t\t\t\t\t\t\t\tcurrentBlock.textSignature,\n\t\t\t\t\t\t\t\t\tpart.thoughtSignature,\n\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t\tstream.push({\n\t\t\t\t\t\t\t\t\ttype: \"text_delta\",\n\t\t\t\t\t\t\t\t\tcontentIndex: blockIndex(),\n\t\t\t\t\t\t\t\t\tdelta: part.text,\n\t\t\t\t\t\t\t\t\tpartial: output,\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tif (part.functionCall) {\n\t\t\t\t\t\t\tif (currentBlock) {\n\t\t\t\t\t\t\t\tif (currentBlock.type === \"text\") {\n\t\t\t\t\t\t\t\t\tstream.push({\n\t\t\t\t\t\t\t\t\t\ttype: \"text_end\",\n\t\t\t\t\t\t\t\t\t\tcontentIndex: blockIndex(),\n\t\t\t\t\t\t\t\t\t\tcontent: currentBlock.text,\n\t\t\t\t\t\t\t\t\t\tpartial: output,\n\t\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\t\tstream.push({\n\t\t\t\t\t\t\t\t\t\ttype: \"thinking_end\",\n\t\t\t\t\t\t\t\t\t\tcontentIndex: blockIndex(),\n\t\t\t\t\t\t\t\t\t\tcontent: currentBlock.thinking,\n\t\t\t\t\t\t\t\t\t\tpartial: output,\n\t\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tcurrentBlock = null;\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t// Generate unique ID if not provided or if it's a duplicate\n\t\t\t\t\t\t\tconst providedId = part.functionCall.id;\n\t\t\t\t\t\t\tconst needsNewId =\n\t\t\t\t\t\t\t\t!providedId || output.content.some((b) => b.type === \"toolCall\" && b.id === providedId);\n\t\t\t\t\t\t\tconst toolCallId = needsNewId\n\t\t\t\t\t\t\t\t? `${part.functionCall.name}_${Date.now()}_${++toolCallCounter}`\n\t\t\t\t\t\t\t\t: providedId;\n\n\t\t\t\t\t\t\tconst toolCall: ToolCall = {\n\t\t\t\t\t\t\t\ttype: \"toolCall\",\n\t\t\t\t\t\t\t\tid: toolCallId,\n\t\t\t\t\t\t\t\tname: part.functionCall.name || \"\",\n\t\t\t\t\t\t\t\targuments: (part.functionCall.args as Record<string, any>) ?? {},\n\t\t\t\t\t\t\t\t...(part.thoughtSignature && { thoughtSignature: part.thoughtSignature }),\n\t\t\t\t\t\t\t};\n\n\t\t\t\t\t\t\toutput.content.push(toolCall);\n\t\t\t\t\t\t\tstream.push({ type: \"toolcall_start\", contentIndex: blockIndex(), partial: output });\n\t\t\t\t\t\t\tstream.push({\n\t\t\t\t\t\t\t\ttype: \"toolcall_delta\",\n\t\t\t\t\t\t\t\tcontentIndex: blockIndex(),\n\t\t\t\t\t\t\t\tdelta: JSON.stringify(toolCall.arguments),\n\t\t\t\t\t\t\t\tpartial: output,\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\tstream.push({ type: \"toolcall_end\", contentIndex: blockIndex(), toolCall, partial: output });\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tif (candidate?.finishReason) {\n\t\t\t\t\toutput.stopReason = mapStopReason(candidate.finishReason);\n\t\t\t\t\tif (output.content.some((b) => b.type === \"toolCall\")) {\n\t\t\t\t\t\toutput.stopReason = \"toolUse\";\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tif (chunk.usageMetadata) {\n\t\t\t\t\toutput.usage = {\n\t\t\t\t\t\tinput:\n\t\t\t\t\t\t\t(chunk.usageMetadata.promptTokenCount || 0) - (chunk.usageMetadata.cachedContentTokenCount || 0),\n\t\t\t\t\t\toutput:\n\t\t\t\t\t\t\t(chunk.usageMetadata.candidatesTokenCount || 0) + (chunk.usageMetadata.thoughtsTokenCount || 0),\n\t\t\t\t\t\tcacheRead: chunk.usageMetadata.cachedContentTokenCount || 0,\n\t\t\t\t\t\tcacheWrite: 0,\n\t\t\t\t\t\ttotalTokens: chunk.usageMetadata.totalTokenCount || 0,\n\t\t\t\t\t\tcost: {\n\t\t\t\t\t\t\tinput: 0,\n\t\t\t\t\t\t\toutput: 0,\n\t\t\t\t\t\t\tcacheRead: 0,\n\t\t\t\t\t\t\tcacheWrite: 0,\n\t\t\t\t\t\t\ttotal: 0,\n\t\t\t\t\t\t},\n\t\t\t\t\t};\n\t\t\t\t\tcalculateCost(model, output.usage);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (currentBlock) {\n\t\t\t\tif (currentBlock.type === \"text\") {\n\t\t\t\t\tstream.push({\n\t\t\t\t\t\ttype: \"text_end\",\n\t\t\t\t\t\tcontentIndex: blockIndex(),\n\t\t\t\t\t\tcontent: currentBlock.text,\n\t\t\t\t\t\tpartial: output,\n\t\t\t\t\t});\n\t\t\t\t} else {\n\t\t\t\t\tstream.push({\n\t\t\t\t\t\ttype: \"thinking_end\",\n\t\t\t\t\t\tcontentIndex: blockIndex(),\n\t\t\t\t\t\tcontent: currentBlock.thinking,\n\t\t\t\t\t\tpartial: output,\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (options?.signal?.aborted) {\n\t\t\t\tthrow new Error(\"Request was aborted\");\n\t\t\t}\n\n\t\t\tif (output.stopReason === \"aborted\" || output.stopReason === \"error\") {\n\t\t\t\tthrow new Error(\"An unknown error occurred\");\n\t\t\t}\n\n\t\t\tstream.push({ type: \"done\", reason: output.stopReason, message: output });\n\t\t\tstream.end();\n\t\t} catch (error) {\n\t\t\t// Remove internal index property used during streaming\n\t\t\tfor (const block of output.content) {\n\t\t\t\tif (\"index\" in block) {\n\t\t\t\t\tdelete (block as { index?: number }).index;\n\t\t\t\t}\n\t\t\t}\n\t\t\toutput.stopReason = options?.signal?.aborted ? \"aborted\" : \"error\";\n\t\t\toutput.errorMessage = error instanceof Error ? error.message : JSON.stringify(error);\n\t\t\tstream.push({ type: \"error\", reason: output.stopReason, error: output });\n\t\t\tstream.end();\n\t\t}\n\t})();\n\n\treturn stream;\n};\n\nexport const streamSimpleGoogle: StreamFunction<\"google-generative-ai\", SimpleStreamOptions> = (\n\tmodel: Model<\"google-generative-ai\">,\n\tcontext: Context,\n\toptions?: SimpleStreamOptions,\n): AssistantMessageEventStream => {\n\tconst apiKey = options?.apiKey;\n\tif (!apiKey) {\n\t\tthrow new Error(`No API key for provider: ${model.provider}`);\n\t}\n\n\tconst base = buildBaseOptions(model, options, apiKey);\n\tif (!options?.reasoning) {\n\t\treturn streamGoogle(model, context, { ...base, thinking: { enabled: false } } satisfies GoogleOptions);\n\t}\n\n\tconst clampedReasoning = clampThinkingLevel(model, options.reasoning);\n\tconst effort = (clampedReasoning === \"off\" ? \"high\" : clampedReasoning) as ClampedThinkingLevel;\n\tconst googleModel = model as Model<\"google-generative-ai\">;\n\n\tif (isGemini3ProModel(googleModel) || isGemini3FlashModel(googleModel) || isGemma4Model(googleModel)) {\n\t\treturn streamGoogle(model, context, {\n\t\t\t...base,\n\t\t\tthinking: {\n\t\t\t\tenabled: true,\n\t\t\t\tlevel: getThinkingLevel(effort, googleModel),\n\t\t\t},\n\t\t} satisfies GoogleOptions);\n\t}\n\n\treturn streamGoogle(model, context, {\n\t\t...base,\n\t\tthinking: {\n\t\t\tenabled: true,\n\t\t\tbudgetTokens: getGoogleBudget(googleModel, effort, options.thinkingBudgets),\n\t\t},\n\t} satisfies GoogleOptions);\n};\n\nfunction createClient(\n\tmodel: Model<\"google-generative-ai\">,\n\tapiKey?: string,\n\toptionsHeaders?: Record<string, string>,\n): GoogleGenAI {\n\tconst httpOptions: { baseUrl?: string; apiVersion?: string; headers?: Record<string, string> } = {};\n\tif (model.baseUrl) {\n\t\thttpOptions.baseUrl = model.baseUrl;\n\t\thttpOptions.apiVersion = \"\"; // baseUrl already includes version path, don't append\n\t}\n\tif (model.headers || optionsHeaders) {\n\t\thttpOptions.headers = { ...model.headers, ...optionsHeaders };\n\t}\n\n\treturn new GoogleGenAI({\n\t\tapiKey,\n\t\thttpOptions: Object.keys(httpOptions).length > 0 ? httpOptions : undefined,\n\t});\n}\n\nfunction buildParams(\n\tmodel: Model<\"google-generative-ai\">,\n\tcontext: Context,\n\toptions: GoogleOptions = {},\n): GenerateContentParameters {\n\tconst contents = convertMessages(model, context);\n\n\tconst generationConfig: GenerateContentConfig = {};\n\tif (options.temperature !== undefined) {\n\t\tgenerationConfig.temperature = options.temperature;\n\t}\n\tif (options.maxTokens !== undefined) {\n\t\tgenerationConfig.maxOutputTokens = options.maxTokens;\n\t}\n\n\tconst config: GenerateContentConfig = {\n\t\t...(Object.keys(generationConfig).length > 0 && generationConfig),\n\t\t...(context.systemPrompt && { systemInstruction: sanitizeSurrogates(context.systemPrompt) }),\n\t\t...(context.tools && context.tools.length > 0 && { tools: convertTools(context.tools) }),\n\t};\n\n\tif (context.tools && context.tools.length > 0 && options.toolChoice) {\n\t\tconfig.toolConfig = {\n\t\t\tfunctionCallingConfig: {\n\t\t\t\tmode: mapToolChoice(options.toolChoice),\n\t\t\t},\n\t\t};\n\t} else {\n\t\tconfig.toolConfig = undefined;\n\t}\n\n\tif (options.thinking?.enabled && model.reasoning) {\n\t\tconst thinkingConfig: ThinkingConfig = { includeThoughts: true };\n\t\tif (options.thinking.level !== undefined) {\n\t\t\t// Cast to any since our GoogleThinkingLevel mirrors Google's ThinkingLevel enum values\n\t\t\tthinkingConfig.thinkingLevel = options.thinking.level as any;\n\t\t} else if (options.thinking.budgetTokens !== undefined) {\n\t\t\tthinkingConfig.thinkingBudget = options.thinking.budgetTokens;\n\t\t}\n\t\tconfig.thinkingConfig = thinkingConfig;\n\t} else if (model.reasoning && options.thinking && !options.thinking.enabled) {\n\t\tconfig.thinkingConfig = getDisabledThinkingConfig(model);\n\t}\n\n\tif (options.signal) {\n\t\tif (options.signal.aborted) {\n\t\t\tthrow new Error(\"Request aborted\");\n\t\t}\n\t\tconfig.abortSignal = options.signal;\n\t}\n\n\tconst params: GenerateContentParameters = {\n\t\tmodel: model.id,\n\t\tcontents,\n\t\tconfig,\n\t};\n\n\treturn params;\n}\n\ntype ClampedThinkingLevel = Exclude<ThinkingLevel, \"xhigh\">;\n\nfunction isGemma4Model(model: Model<\"google-generative-ai\">): boolean {\n\treturn /gemma-?4/.test(model.id.toLowerCase());\n}\n\nfunction isGemini3ProModel(model: Model<\"google-generative-ai\">): boolean {\n\treturn /gemini-3(?:\\.\\d+)?-pro/.test(model.id.toLowerCase());\n}\n\nfunction isGemini3FlashModel(model: Model<\"google-generative-ai\">): boolean {\n\treturn /gemini-3(?:\\.\\d+)?-flash/.test(model.id.toLowerCase());\n}\n\nfunction getDisabledThinkingConfig(model: Model<\"google-generative-ai\">): ThinkingConfig {\n\t// Google docs: Gemini 3.1 Pro cannot disable thinking, and Gemini 3 Flash / Flash-Lite\n\t// do not support full thinking-off either. For Gemini 3 models, use the lowest supported\n\t// thinkingLevel without includeThoughts so hidden thinking remains invisible to pi.\n\tif (isGemini3ProModel(model)) {\n\t\treturn { thinkingLevel: \"LOW\" as any };\n\t}\n\tif (isGemini3FlashModel(model)) {\n\t\treturn { thinkingLevel: \"MINIMAL\" as any };\n\t}\n\tif (isGemma4Model(model)) {\n\t\treturn { thinkingLevel: \"MINIMAL\" as any };\n\t}\n\n\t// Gemini 2.x supports disabling via thinkingBudget = 0.\n\treturn { thinkingBudget: 0 };\n}\n\nfunction getThinkingLevel(effort: ClampedThinkingLevel, model: Model<\"google-generative-ai\">): GoogleThinkingLevel {\n\tif (isGemini3ProModel(model)) {\n\t\tswitch (effort) {\n\t\t\tcase \"minimal\":\n\t\t\tcase \"low\":\n\t\t\t\treturn \"LOW\";\n\t\t\tcase \"medium\":\n\t\t\tcase \"high\":\n\t\t\t\treturn \"HIGH\";\n\t\t}\n\t}\n\tif (isGemma4Model(model)) {\n\t\tswitch (effort) {\n\t\t\tcase \"minimal\":\n\t\t\tcase \"low\":\n\t\t\t\treturn \"MINIMAL\";\n\t\t\tcase \"medium\":\n\t\t\tcase \"high\":\n\t\t\t\treturn \"HIGH\";\n\t\t}\n\t}\n\tswitch (effort) {\n\t\tcase \"minimal\":\n\t\t\treturn \"MINIMAL\";\n\t\tcase \"low\":\n\t\t\treturn \"LOW\";\n\t\tcase \"medium\":\n\t\t\treturn \"MEDIUM\";\n\t\tcase \"high\":\n\t\t\treturn \"HIGH\";\n\t}\n}\n\nfunction getGoogleBudget(\n\tmodel: Model<\"google-generative-ai\">,\n\teffort: ClampedThinkingLevel,\n\tcustomBudgets?: ThinkingBudgets,\n): number {\n\tif (customBudgets?.[effort] !== undefined) {\n\t\treturn customBudgets[effort]!;\n\t}\n\n\tif (model.id.includes(\"2.5-pro\")) {\n\t\tconst budgets: Record<ClampedThinkingLevel, number> = {\n\t\t\tminimal: 128,\n\t\t\tlow: 2048,\n\t\t\tmedium: 8192,\n\t\t\thigh: 32768,\n\t\t};\n\t\treturn budgets[effort];\n\t}\n\n\tif (model.id.includes(\"2.5-flash-lite\")) {\n\t\tconst budgets: Record<ClampedThinkingLevel, number> = {\n\t\t\tminimal: 512,\n\t\t\tlow: 2048,\n\t\t\tmedium: 8192,\n\t\t\thigh: 24576,\n\t\t};\n\t\treturn budgets[effort];\n\t}\n\n\tif (model.id.includes(\"2.5-flash\")) {\n\t\tconst budgets: Record<ClampedThinkingLevel, number> = {\n\t\t\tminimal: 128,\n\t\t\tlow: 2048,\n\t\t\tmedium: 8192,\n\t\t\thigh: 24576,\n\t\t};\n\t\treturn budgets[effort];\n\t}\n\n\treturn -1;\n}\n"]}
@@ -0,0 +1,3 @@
1
+ import type { ImagesFunction, ImagesOptions } from "../../types.ts";
2
+ export declare const generateImagesOpenRouter: ImagesFunction<"openrouter-images", ImagesOptions>;
3
+ //# sourceMappingURL=openrouter.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"openrouter.d.ts","sourceRoot":"","sources":["../../../src/providers/images/openrouter.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAIX,cAAc,EAEd,aAAa,EAEb,MAAM,gBAAgB,CAAC;AAoBxB,eAAO,MAAM,wBAAwB,EAAE,cAAc,CAAC,mBAAmB,EAAE,aAAa,CAmEvF,CAAC","sourcesContent":["import OpenAI from \"openai\";\nimport type {\n\tChatCompletion,\n\tChatCompletionContentPart,\n\tChatCompletionContentPartImage,\n\tChatCompletionContentPartText,\n\tChatCompletionCreateParamsNonStreaming,\n} from \"openai/resources/chat/completions.js\";\nimport type {\n\tAssistantImages,\n\tImageContent,\n\tImagesContext,\n\tImagesFunction,\n\tImagesModel,\n\tImagesOptions,\n\tTextContent,\n} from \"../../types.ts\";\nimport { headersToRecord } from \"../../utils/headers.ts\";\nimport { sanitizeSurrogates } from \"../../utils/sanitize-unicode.ts\";\n\ninterface OpenRouterGeneratedImage {\n\timage_url?: string | { url?: string };\n}\n\ntype OpenRouterImageGenerationMessage = ChatCompletion[\"choices\"][number][\"message\"] & {\n\timages?: OpenRouterGeneratedImage[];\n};\n\ntype OpenRouterImageGenerationChoice = ChatCompletion[\"choices\"][number] & {\n\tmessage: OpenRouterImageGenerationMessage;\n};\n\ntype OpenRouterImageGenerationResponse = ChatCompletion & {\n\tchoices: OpenRouterImageGenerationChoice[];\n};\n\nexport const generateImagesOpenRouter: ImagesFunction<\"openrouter-images\", ImagesOptions> = async (\n\tmodel: ImagesModel<\"openrouter-images\">,\n\tcontext: ImagesContext,\n\toptions?: ImagesOptions,\n) => {\n\tconst output: AssistantImages = {\n\t\tapi: model.api,\n\t\tprovider: model.provider,\n\t\tmodel: model.id,\n\t\toutput: [],\n\t\tstopReason: \"stop\",\n\t\ttimestamp: Date.now(),\n\t};\n\n\ttry {\n\t\tconst apiKey = options?.apiKey;\n\t\tif (!apiKey) {\n\t\t\tthrow new Error(`No API key for provider: ${model.provider}`);\n\t\t}\n\t\tconst client = createClient(model, apiKey, options?.headers);\n\t\tlet params = buildParams(model, context);\n\t\tconst nextParams = await options?.onPayload?.(params, model);\n\t\tif (nextParams !== undefined) {\n\t\t\tparams = nextParams as typeof params;\n\t\t}\n\t\tconst requestOptions = {\n\t\t\t...(options?.signal ? { signal: options.signal } : {}),\n\t\t\t...(options?.timeoutMs !== undefined ? { timeout: options.timeoutMs } : {}),\n\t\t\tmaxRetries: options?.maxRetries ?? 0,\n\t\t};\n\t\tconst { data: response, response: rawResponse } = await client.chat.completions\n\t\t\t.create(params as unknown as ChatCompletionCreateParamsNonStreaming, requestOptions)\n\t\t\t.withResponse();\n\t\tawait options?.onResponse?.({ status: rawResponse.status, headers: headersToRecord(rawResponse.headers) }, model);\n\n\t\tconst imageResponse = response as OpenRouterImageGenerationResponse;\n\t\toutput.responseId = imageResponse.id;\n\t\tif (imageResponse.usage) {\n\t\t\toutput.usage = parseUsage(imageResponse.usage, model);\n\t\t}\n\n\t\tconst choice = imageResponse.choices[0];\n\t\tif (choice) {\n\t\t\tconst content = choice.message.content;\n\t\t\tif (typeof content === \"string\" && content.length > 0) {\n\t\t\t\toutput.output.push({ type: \"text\", text: content } satisfies TextContent);\n\t\t\t}\n\n\t\t\tfor (const image of choice.message.images ?? []) {\n\t\t\t\tconst imageUrl = typeof image.image_url === \"string\" ? image.image_url : image.image_url?.url;\n\t\t\t\tif (!imageUrl?.startsWith(\"data:\")) continue;\n\t\t\t\tconst matches = imageUrl.match(/^data:([^;]+);base64,(.+)$/);\n\t\t\t\tif (!matches) continue;\n\t\t\t\toutput.output.push({\n\t\t\t\t\ttype: \"image\",\n\t\t\t\t\tmimeType: matches[1],\n\t\t\t\t\tdata: matches[2],\n\t\t\t\t} satisfies ImageContent);\n\t\t\t}\n\t\t}\n\n\t\treturn output;\n\t} catch (error) {\n\t\toutput.stopReason = options?.signal?.aborted ? \"aborted\" : \"error\";\n\t\toutput.errorMessage = error instanceof Error ? error.message : JSON.stringify(error);\n\t\treturn output;\n\t}\n};\n\nfunction createClient(\n\tmodel: ImagesModel<\"openrouter-images\">,\n\tapiKey: string,\n\toptionsHeaders?: Record<string, string>,\n): OpenAI {\n\treturn new OpenAI({\n\t\tapiKey,\n\t\tbaseURL: model.baseUrl,\n\t\tdangerouslyAllowBrowser: true,\n\t\tdefaultHeaders: {\n\t\t\t...model.headers,\n\t\t\t...optionsHeaders,\n\t\t},\n\t});\n}\n\ntype OpenRouterImagesCreateParams = Omit<ChatCompletionCreateParamsNonStreaming, \"modalities\"> & {\n\tmodalities: Array<\"image\" | \"text\">;\n};\n\nfunction buildParams(model: ImagesModel<\"openrouter-images\">, context: ImagesContext): OpenRouterImagesCreateParams {\n\tconst content: ChatCompletionContentPart[] = context.input.map((item): ChatCompletionContentPart => {\n\t\tif (item.type === \"text\") {\n\t\t\treturn {\n\t\t\t\ttype: \"text\",\n\t\t\t\ttext: sanitizeSurrogates(item.text),\n\t\t\t} satisfies ChatCompletionContentPartText;\n\t\t}\n\t\treturn {\n\t\t\ttype: \"image_url\",\n\t\t\timage_url: {\n\t\t\t\turl: `data:${item.mimeType};base64,${item.data}`,\n\t\t\t},\n\t\t} satisfies ChatCompletionContentPartImage;\n\t});\n\n\treturn {\n\t\tmodel: model.id,\n\t\tmessages: [\n\t\t\t{\n\t\t\t\trole: \"user\" as const,\n\t\t\t\tcontent,\n\t\t\t},\n\t\t],\n\t\tstream: false,\n\t\tmodalities: model.output.includes(\"text\") ? [\"image\", \"text\"] : [\"image\"],\n\t};\n}\n\nfunction parseUsage(\n\trawUsage: {\n\t\tprompt_tokens?: number;\n\t\tcompletion_tokens?: number;\n\t\tprompt_tokens_details?: { cached_tokens?: number; cache_write_tokens?: number };\n\t},\n\tmodel: ImagesModel<\"openrouter-images\">,\n) {\n\tconst promptTokens = rawUsage.prompt_tokens || 0;\n\tconst reportedCachedTokens = rawUsage.prompt_tokens_details?.cached_tokens || 0;\n\tconst cacheWriteTokens = rawUsage.prompt_tokens_details?.cache_write_tokens || 0;\n\tconst cacheReadTokens =\n\t\tcacheWriteTokens > 0 ? Math.max(0, reportedCachedTokens - cacheWriteTokens) : reportedCachedTokens;\n\tconst input = Math.max(0, promptTokens - cacheReadTokens - cacheWriteTokens);\n\tconst output = rawUsage.completion_tokens || 0;\n\tconst usage = {\n\t\tinput,\n\t\toutput,\n\t\tcacheRead: cacheReadTokens,\n\t\tcacheWrite: cacheWriteTokens,\n\t\ttotalTokens: input + output + cacheReadTokens + cacheWriteTokens,\n\t\tcost: {\n\t\t\tinput: (model.cost.input / 1000000) * input,\n\t\t\toutput: (model.cost.output / 1000000) * output,\n\t\t\tcacheRead: (model.cost.cacheRead / 1000000) * cacheReadTokens,\n\t\t\tcacheWrite: (model.cost.cacheWrite / 1000000) * cacheWriteTokens,\n\t\t\ttotal: 0,\n\t\t},\n\t};\n\tusage.cost.total = usage.cost.input + usage.cost.output + usage.cost.cacheRead + usage.cost.cacheWrite;\n\treturn usage;\n}\n"]}
@@ -0,0 +1,128 @@
1
+ import OpenAI from "openai";
2
+ import { headersToRecord } from "../../utils/headers.js";
3
+ import { sanitizeSurrogates } from "../../utils/sanitize-unicode.js";
4
+ export const generateImagesOpenRouter = async (model, context, options) => {
5
+ const output = {
6
+ api: model.api,
7
+ provider: model.provider,
8
+ model: model.id,
9
+ output: [],
10
+ stopReason: "stop",
11
+ timestamp: Date.now(),
12
+ };
13
+ try {
14
+ const apiKey = options?.apiKey;
15
+ if (!apiKey) {
16
+ throw new Error(`No API key for provider: ${model.provider}`);
17
+ }
18
+ const client = createClient(model, apiKey, options?.headers);
19
+ let params = buildParams(model, context);
20
+ const nextParams = await options?.onPayload?.(params, model);
21
+ if (nextParams !== undefined) {
22
+ params = nextParams;
23
+ }
24
+ const requestOptions = {
25
+ ...(options?.signal ? { signal: options.signal } : {}),
26
+ ...(options?.timeoutMs !== undefined ? { timeout: options.timeoutMs } : {}),
27
+ maxRetries: options?.maxRetries ?? 0,
28
+ };
29
+ const { data: response, response: rawResponse } = await client.chat.completions
30
+ .create(params, requestOptions)
31
+ .withResponse();
32
+ await options?.onResponse?.({ status: rawResponse.status, headers: headersToRecord(rawResponse.headers) }, model);
33
+ const imageResponse = response;
34
+ output.responseId = imageResponse.id;
35
+ if (imageResponse.usage) {
36
+ output.usage = parseUsage(imageResponse.usage, model);
37
+ }
38
+ const choice = imageResponse.choices[0];
39
+ if (choice) {
40
+ const content = choice.message.content;
41
+ if (typeof content === "string" && content.length > 0) {
42
+ output.output.push({ type: "text", text: content });
43
+ }
44
+ for (const image of choice.message.images ?? []) {
45
+ const imageUrl = typeof image.image_url === "string" ? image.image_url : image.image_url?.url;
46
+ if (!imageUrl?.startsWith("data:"))
47
+ continue;
48
+ const matches = imageUrl.match(/^data:([^;]+);base64,(.+)$/);
49
+ if (!matches)
50
+ continue;
51
+ output.output.push({
52
+ type: "image",
53
+ mimeType: matches[1],
54
+ data: matches[2],
55
+ });
56
+ }
57
+ }
58
+ return output;
59
+ }
60
+ catch (error) {
61
+ output.stopReason = options?.signal?.aborted ? "aborted" : "error";
62
+ output.errorMessage = error instanceof Error ? error.message : JSON.stringify(error);
63
+ return output;
64
+ }
65
+ };
66
+ function createClient(model, apiKey, optionsHeaders) {
67
+ return new OpenAI({
68
+ apiKey,
69
+ baseURL: model.baseUrl,
70
+ dangerouslyAllowBrowser: true,
71
+ defaultHeaders: {
72
+ ...model.headers,
73
+ ...optionsHeaders,
74
+ },
75
+ });
76
+ }
77
+ function buildParams(model, context) {
78
+ const content = context.input.map((item) => {
79
+ if (item.type === "text") {
80
+ return {
81
+ type: "text",
82
+ text: sanitizeSurrogates(item.text),
83
+ };
84
+ }
85
+ return {
86
+ type: "image_url",
87
+ image_url: {
88
+ url: `data:${item.mimeType};base64,${item.data}`,
89
+ },
90
+ };
91
+ });
92
+ return {
93
+ model: model.id,
94
+ messages: [
95
+ {
96
+ role: "user",
97
+ content,
98
+ },
99
+ ],
100
+ stream: false,
101
+ modalities: model.output.includes("text") ? ["image", "text"] : ["image"],
102
+ };
103
+ }
104
+ function parseUsage(rawUsage, model) {
105
+ const promptTokens = rawUsage.prompt_tokens || 0;
106
+ const reportedCachedTokens = rawUsage.prompt_tokens_details?.cached_tokens || 0;
107
+ const cacheWriteTokens = rawUsage.prompt_tokens_details?.cache_write_tokens || 0;
108
+ const cacheReadTokens = cacheWriteTokens > 0 ? Math.max(0, reportedCachedTokens - cacheWriteTokens) : reportedCachedTokens;
109
+ const input = Math.max(0, promptTokens - cacheReadTokens - cacheWriteTokens);
110
+ const output = rawUsage.completion_tokens || 0;
111
+ const usage = {
112
+ input,
113
+ output,
114
+ cacheRead: cacheReadTokens,
115
+ cacheWrite: cacheWriteTokens,
116
+ totalTokens: input + output + cacheReadTokens + cacheWriteTokens,
117
+ cost: {
118
+ input: (model.cost.input / 1000000) * input,
119
+ output: (model.cost.output / 1000000) * output,
120
+ cacheRead: (model.cost.cacheRead / 1000000) * cacheReadTokens,
121
+ cacheWrite: (model.cost.cacheWrite / 1000000) * cacheWriteTokens,
122
+ total: 0,
123
+ },
124
+ };
125
+ usage.cost.total = usage.cost.input + usage.cost.output + usage.cost.cacheRead + usage.cost.cacheWrite;
126
+ return usage;
127
+ }
128
+ //# sourceMappingURL=openrouter.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"openrouter.js","sourceRoot":"","sources":["../../../src/providers/images/openrouter.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,QAAQ,CAAC;AAiB5B,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AACzD,OAAO,EAAE,kBAAkB,EAAE,MAAM,iCAAiC,CAAC;AAkBrE,MAAM,CAAC,MAAM,wBAAwB,GAAuD,KAAK,EAChG,KAAuC,EACvC,OAAsB,EACtB,OAAuB,EACtB,EAAE,CAAC;IACJ,MAAM,MAAM,GAAoB;QAC/B,GAAG,EAAE,KAAK,CAAC,GAAG;QACd,QAAQ,EAAE,KAAK,CAAC,QAAQ;QACxB,KAAK,EAAE,KAAK,CAAC,EAAE;QACf,MAAM,EAAE,EAAE;QACV,UAAU,EAAE,MAAM;QAClB,SAAS,EAAE,IAAI,CAAC,GAAG,EAAE;KACrB,CAAC;IAEF,IAAI,CAAC;QACJ,MAAM,MA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,CAAC,GAAG,gBAAgB;YAChE,KAAK,EAAE,CAAC;SACR;KACD,CAAC;IACF,KAAK,CAAC,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC,IAAI,CAAC,SAAS,GAAG,KAAK,CAAC,IAAI,CAAC,UAAU,CAAC;IACvG,OAAO,KAAK,CAAC;AAAA,CACb","sourcesContent":["import OpenAI from \"openai\";\nimport type {\n\tChatCompletion,\n\tChatCompletionContentPart,\n\tChatCompletionContentPartImage,\n\tChatCompletionContentPartText,\n\tChatCompletionCreateParamsNonStreaming,\n} from \"openai/resources/chat/completions.js\";\nimport type {\n\tAssistantImages,\n\tImageContent,\n\tImagesContext,\n\tImagesFunction,\n\tImagesModel,\n\tImagesOptions,\n\tTextContent,\n} from \"../../types.ts\";\nimport { headersToRecord } from \"../../utils/headers.ts\";\nimport { sanitizeSurrogates } from \"../../utils/sanitize-unicode.ts\";\n\ninterface OpenRouterGeneratedImage {\n\timage_url?: string | { url?: string };\n}\n\ntype OpenRouterImageGenerationMessage = ChatCompletion[\"choices\"][number][\"message\"] & {\n\timages?: OpenRouterGeneratedImage[];\n};\n\ntype OpenRouterImageGenerationChoice = ChatCompletion[\"choices\"][number] & {\n\tmessage: OpenRouterImageGenerationMessage;\n};\n\ntype OpenRouterImageGenerationResponse = ChatCompletion & {\n\tchoices: OpenRouterImageGenerationChoice[];\n};\n\nexport const generateImagesOpenRouter: ImagesFunction<\"openrouter-images\", ImagesOptions> = async (\n\tmodel: ImagesModel<\"openrouter-images\">,\n\tcontext: ImagesContext,\n\toptions?: ImagesOptions,\n) => {\n\tconst output: AssistantImages = {\n\t\tapi: model.api,\n\t\tprovider: model.provider,\n\t\tmodel: model.id,\n\t\toutput: [],\n\t\tstopReason: \"stop\",\n\t\ttimestamp: Date.now(),\n\t};\n\n\ttry {\n\t\tconst apiKey = options?.apiKey;\n\t\tif (!apiKey) {\n\t\t\tthrow new Error(`No API key for provider: ${model.provider}`);\n\t\t}\n\t\tconst client = createClient(model, apiKey, options?.headers);\n\t\tlet params = buildParams(model, context);\n\t\tconst nextParams = await options?.onPayload?.(params, model);\n\t\tif (nextParams !== undefined) {\n\t\t\tparams = nextParams as typeof params;\n\t\t}\n\t\tconst requestOptions = {\n\t\t\t...(options?.signal ? { signal: options.signal } : {}),\n\t\t\t...(options?.timeoutMs !== undefined ? { timeout: options.timeoutMs } : {}),\n\t\t\tmaxRetries: options?.maxRetries ?? 0,\n\t\t};\n\t\tconst { data: response, response: rawResponse } = await client.chat.completions\n\t\t\t.create(params as unknown as ChatCompletionCreateParamsNonStreaming, requestOptions)\n\t\t\t.withResponse();\n\t\tawait options?.onResponse?.({ status: rawResponse.status, headers: headersToRecord(rawResponse.headers) }, model);\n\n\t\tconst imageResponse = response as OpenRouterImageGenerationResponse;\n\t\toutput.responseId = imageResponse.id;\n\t\tif (imageResponse.usage) {\n\t\t\toutput.usage = parseUsage(imageResponse.usage, model);\n\t\t}\n\n\t\tconst choice = imageResponse.choices[0];\n\t\tif (choice) {\n\t\t\tconst content = choice.message.content;\n\t\t\tif (typeof content === \"string\" && content.length > 0) {\n\t\t\t\toutput.output.push({ type: \"text\", text: content } satisfies TextContent);\n\t\t\t}\n\n\t\t\tfor (const image of choice.message.images ?? []) {\n\t\t\t\tconst imageUrl = typeof image.image_url === \"string\" ? image.image_url : image.image_url?.url;\n\t\t\t\tif (!imageUrl?.startsWith(\"data:\")) continue;\n\t\t\t\tconst matches = imageUrl.match(/^data:([^;]+);base64,(.+)$/);\n\t\t\t\tif (!matches) continue;\n\t\t\t\toutput.output.push({\n\t\t\t\t\ttype: \"image\",\n\t\t\t\t\tmimeType: matches[1],\n\t\t\t\t\tdata: matches[2],\n\t\t\t\t} satisfies ImageContent);\n\t\t\t}\n\t\t}\n\n\t\treturn output;\n\t} catch (error) {\n\t\toutput.stopReason = options?.signal?.aborted ? \"aborted\" : \"error\";\n\t\toutput.errorMessage = error instanceof Error ? error.message : JSON.stringify(error);\n\t\treturn output;\n\t}\n};\n\nfunction createClient(\n\tmodel: ImagesModel<\"openrouter-images\">,\n\tapiKey: string,\n\toptionsHeaders?: Record<string, string>,\n): OpenAI {\n\treturn new OpenAI({\n\t\tapiKey,\n\t\tbaseURL: model.baseUrl,\n\t\tdangerouslyAllowBrowser: true,\n\t\tdefaultHeaders: {\n\t\t\t...model.headers,\n\t\t\t...optionsHeaders,\n\t\t},\n\t});\n}\n\ntype OpenRouterImagesCreateParams = Omit<ChatCompletionCreateParamsNonStreaming, \"modalities\"> & {\n\tmodalities: Array<\"image\" | \"text\">;\n};\n\nfunction buildParams(model: ImagesModel<\"openrouter-images\">, context: ImagesContext): OpenRouterImagesCreateParams {\n\tconst content: ChatCompletionContentPart[] = context.input.map((item): ChatCompletionContentPart => {\n\t\tif (item.type === \"text\") {\n\t\t\treturn {\n\t\t\t\ttype: \"text\",\n\t\t\t\ttext: sanitizeSurrogates(item.text),\n\t\t\t} satisfies ChatCompletionContentPartText;\n\t\t}\n\t\treturn {\n\t\t\ttype: \"image_url\",\n\t\t\timage_url: {\n\t\t\t\turl: `data:${item.mimeType};base64,${item.data}`,\n\t\t\t},\n\t\t} satisfies ChatCompletionContentPartImage;\n\t});\n\n\treturn {\n\t\tmodel: model.id,\n\t\tmessages: [\n\t\t\t{\n\t\t\t\trole: \"user\" as const,\n\t\t\t\tcontent,\n\t\t\t},\n\t\t],\n\t\tstream: false,\n\t\tmodalities: model.output.includes(\"text\") ? [\"image\", \"text\"] : [\"image\"],\n\t};\n}\n\nfunction parseUsage(\n\trawUsage: {\n\t\tprompt_tokens?: number;\n\t\tcompletion_tokens?: number;\n\t\tprompt_tokens_details?: { cached_tokens?: number; cache_write_tokens?: number };\n\t},\n\tmodel: ImagesModel<\"openrouter-images\">,\n) {\n\tconst promptTokens = rawUsage.prompt_tokens || 0;\n\tconst reportedCachedTokens = rawUsage.prompt_tokens_details?.cached_tokens || 0;\n\tconst cacheWriteTokens = rawUsage.prompt_tokens_details?.cache_write_tokens || 0;\n\tconst cacheReadTokens =\n\t\tcacheWriteTokens > 0 ? Math.max(0, reportedCachedTokens - cacheWriteTokens) : reportedCachedTokens;\n\tconst input = Math.max(0, promptTokens - cacheReadTokens - cacheWriteTokens);\n\tconst output = rawUsage.completion_tokens || 0;\n\tconst usage = {\n\t\tinput,\n\t\toutput,\n\t\tcacheRead: cacheReadTokens,\n\t\tcacheWrite: cacheWriteTokens,\n\t\ttotalTokens: input + output + cacheReadTokens + cacheWriteTokens,\n\t\tcost: {\n\t\t\tinput: (model.cost.input / 1000000) * input,\n\t\t\toutput: (model.cost.output / 1000000) * output,\n\t\t\tcacheRead: (model.cost.cacheRead / 1000000) * cacheReadTokens,\n\t\t\tcacheWrite: (model.cost.cacheWrite / 1000000) * cacheWriteTokens,\n\t\t\ttotal: 0,\n\t\t},\n\t};\n\tusage.cost.total = usage.cost.input + usage.cost.output + usage.cost.cacheRead + usage.cost.cacheWrite;\n\treturn usage;\n}\n"]}
@@ -0,0 +1,4 @@
1
+ import type { ImagesFunction, ImagesOptions } from "../../types.ts";
2
+ export declare const generateImagesOpenRouter: ImagesFunction<"openrouter-images", ImagesOptions>;
3
+ export declare function registerBuiltInImagesApiProviders(): void;
4
+ //# sourceMappingURL=register-builtins.d.ts.map