@llumiverse/drivers 1.0.0-dev.20260202.145450Z → 1.0.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 (240) hide show
  1. package/lib/cjs/adobe/firefly.js +120 -0
  2. package/lib/cjs/adobe/firefly.js.map +1 -0
  3. package/lib/cjs/azure/azure_foundry.js +432 -0
  4. package/lib/cjs/azure/azure_foundry.js.map +1 -0
  5. package/lib/cjs/bedrock/converse.js +359 -0
  6. package/lib/cjs/bedrock/converse.js.map +1 -0
  7. package/lib/cjs/bedrock/index.js +1441 -0
  8. package/lib/cjs/bedrock/index.js.map +1 -0
  9. package/lib/cjs/bedrock/nova-image-payload.js +207 -0
  10. package/lib/cjs/bedrock/nova-image-payload.js.map +1 -0
  11. package/lib/cjs/bedrock/payloads.js +3 -0
  12. package/lib/cjs/bedrock/payloads.js.map +1 -0
  13. package/lib/cjs/bedrock/s3.js +107 -0
  14. package/lib/cjs/bedrock/s3.js.map +1 -0
  15. package/lib/cjs/bedrock/twelvelabs.js +87 -0
  16. package/lib/cjs/bedrock/twelvelabs.js.map +1 -0
  17. package/lib/cjs/groq/index.js +326 -0
  18. package/lib/cjs/groq/index.js.map +1 -0
  19. package/lib/cjs/huggingface_ie.js +201 -0
  20. package/lib/cjs/huggingface_ie.js.map +1 -0
  21. package/lib/cjs/index.js +31 -0
  22. package/lib/cjs/index.js.map +1 -0
  23. package/lib/cjs/mistral/index.js +176 -0
  24. package/lib/cjs/mistral/index.js.map +1 -0
  25. package/lib/cjs/mistral/types.js +83 -0
  26. package/lib/cjs/mistral/types.js.map +1 -0
  27. package/lib/cjs/openai/azure_openai.js +72 -0
  28. package/lib/cjs/openai/azure_openai.js.map +1 -0
  29. package/lib/cjs/openai/index.js +1100 -0
  30. package/lib/cjs/openai/index.js.map +1 -0
  31. package/lib/cjs/openai/openai.js +21 -0
  32. package/lib/cjs/openai/openai.js.map +1 -0
  33. package/lib/cjs/openai/openai_compatible.js +63 -0
  34. package/lib/cjs/openai/openai_compatible.js.map +1 -0
  35. package/lib/cjs/openai/openai_format.js +131 -0
  36. package/lib/cjs/openai/openai_format.js.map +1 -0
  37. package/lib/cjs/package.json +3 -0
  38. package/lib/cjs/replicate.js +275 -0
  39. package/lib/cjs/replicate.js.map +1 -0
  40. package/lib/cjs/test-driver/TestErrorCompletionStream.js +20 -0
  41. package/lib/cjs/test-driver/TestErrorCompletionStream.js.map +1 -0
  42. package/lib/cjs/test-driver/TestValidationErrorCompletionStream.js +24 -0
  43. package/lib/cjs/test-driver/TestValidationErrorCompletionStream.js.map +1 -0
  44. package/lib/cjs/test-driver/index.js +109 -0
  45. package/lib/cjs/test-driver/index.js.map +1 -0
  46. package/lib/cjs/test-driver/utils.js +30 -0
  47. package/lib/cjs/test-driver/utils.js.map +1 -0
  48. package/lib/cjs/togetherai/index.js +126 -0
  49. package/lib/cjs/togetherai/index.js.map +1 -0
  50. package/lib/cjs/togetherai/interfaces.js +3 -0
  51. package/lib/cjs/togetherai/interfaces.js.map +1 -0
  52. package/lib/cjs/vertexai/debug.js +12 -0
  53. package/lib/cjs/vertexai/debug.js.map +1 -0
  54. package/lib/cjs/vertexai/embeddings/embeddings-image.js +27 -0
  55. package/lib/cjs/vertexai/embeddings/embeddings-image.js.map +1 -0
  56. package/lib/cjs/vertexai/embeddings/embeddings-text.js +23 -0
  57. package/lib/cjs/vertexai/embeddings/embeddings-text.js.map +1 -0
  58. package/lib/cjs/vertexai/index.js +635 -0
  59. package/lib/cjs/vertexai/index.js.map +1 -0
  60. package/lib/cjs/vertexai/models/claude.js +842 -0
  61. package/lib/cjs/vertexai/models/claude.js.map +1 -0
  62. package/lib/cjs/vertexai/models/gemini.js +1110 -0
  63. package/lib/cjs/vertexai/models/gemini.js.map +1 -0
  64. package/lib/cjs/vertexai/models/imagen.js +303 -0
  65. package/lib/cjs/vertexai/models/imagen.js.map +1 -0
  66. package/lib/cjs/vertexai/models/llama.js +183 -0
  67. package/lib/cjs/vertexai/models/llama.js.map +1 -0
  68. package/lib/cjs/vertexai/models.js +35 -0
  69. package/lib/cjs/vertexai/models.js.map +1 -0
  70. package/lib/cjs/watsonx/index.js +161 -0
  71. package/lib/cjs/watsonx/index.js.map +1 -0
  72. package/lib/cjs/watsonx/interfaces.js +3 -0
  73. package/lib/cjs/watsonx/interfaces.js.map +1 -0
  74. package/lib/cjs/xai/index.js +65 -0
  75. package/lib/cjs/xai/index.js.map +1 -0
  76. package/lib/esm/adobe/firefly.js +116 -0
  77. package/lib/esm/adobe/firefly.js.map +1 -0
  78. package/lib/esm/azure/azure_foundry.js +426 -0
  79. package/lib/esm/azure/azure_foundry.js.map +1 -0
  80. package/lib/esm/bedrock/converse.js +352 -0
  81. package/lib/esm/bedrock/converse.js.map +1 -0
  82. package/lib/esm/bedrock/index.js +1434 -0
  83. package/lib/esm/bedrock/index.js.map +1 -0
  84. package/lib/esm/bedrock/nova-image-payload.js +203 -0
  85. package/lib/esm/bedrock/nova-image-payload.js.map +1 -0
  86. package/lib/esm/bedrock/payloads.js +2 -0
  87. package/lib/esm/bedrock/payloads.js.map +1 -0
  88. package/lib/esm/bedrock/s3.js +99 -0
  89. package/lib/esm/bedrock/s3.js.map +1 -0
  90. package/lib/esm/bedrock/twelvelabs.js +84 -0
  91. package/lib/esm/bedrock/twelvelabs.js.map +1 -0
  92. package/lib/esm/groq/index.js +319 -0
  93. package/lib/esm/groq/index.js.map +1 -0
  94. package/lib/esm/huggingface_ie.js +197 -0
  95. package/lib/esm/huggingface_ie.js.map +1 -0
  96. package/lib/esm/index.js +15 -0
  97. package/lib/esm/index.js.map +1 -0
  98. package/lib/esm/mistral/index.js +172 -0
  99. package/lib/esm/mistral/index.js.map +1 -0
  100. package/lib/esm/mistral/types.js +80 -0
  101. package/lib/esm/mistral/types.js.map +1 -0
  102. package/lib/esm/openai/azure_openai.js +68 -0
  103. package/lib/esm/openai/azure_openai.js.map +1 -0
  104. package/lib/esm/openai/index.js +1093 -0
  105. package/lib/esm/openai/index.js.map +1 -0
  106. package/lib/esm/openai/openai.js +14 -0
  107. package/lib/esm/openai/openai.js.map +1 -0
  108. package/lib/esm/openai/openai_compatible.js +56 -0
  109. package/lib/esm/openai/openai_compatible.js.map +1 -0
  110. package/lib/esm/openai/openai_format.js +127 -0
  111. package/lib/esm/openai/openai_format.js.map +1 -0
  112. package/lib/esm/replicate.js +268 -0
  113. package/lib/esm/replicate.js.map +1 -0
  114. package/lib/esm/test-driver/TestErrorCompletionStream.js +16 -0
  115. package/lib/esm/test-driver/TestErrorCompletionStream.js.map +1 -0
  116. package/lib/esm/test-driver/TestValidationErrorCompletionStream.js +20 -0
  117. package/lib/esm/test-driver/TestValidationErrorCompletionStream.js.map +1 -0
  118. package/lib/esm/test-driver/index.js +91 -0
  119. package/lib/esm/test-driver/index.js.map +1 -0
  120. package/lib/esm/test-driver/utils.js +25 -0
  121. package/lib/esm/test-driver/utils.js.map +1 -0
  122. package/lib/esm/togetherai/index.js +122 -0
  123. package/lib/esm/togetherai/index.js.map +1 -0
  124. package/lib/esm/togetherai/interfaces.js +2 -0
  125. package/lib/esm/togetherai/interfaces.js.map +1 -0
  126. package/lib/esm/vertexai/debug.js +6 -0
  127. package/lib/esm/vertexai/debug.js.map +1 -0
  128. package/lib/esm/vertexai/embeddings/embeddings-image.js +24 -0
  129. package/lib/esm/vertexai/embeddings/embeddings-image.js.map +1 -0
  130. package/lib/esm/vertexai/embeddings/embeddings-text.js +20 -0
  131. package/lib/esm/vertexai/embeddings/embeddings-text.js.map +1 -0
  132. package/lib/esm/vertexai/index.js +630 -0
  133. package/lib/esm/vertexai/index.js.map +1 -0
  134. package/lib/esm/vertexai/models/claude.js +833 -0
  135. package/lib/esm/vertexai/models/claude.js.map +1 -0
  136. package/lib/esm/vertexai/models/gemini.js +1104 -0
  137. package/lib/esm/vertexai/models/gemini.js.map +1 -0
  138. package/lib/esm/vertexai/models/imagen.js +299 -0
  139. package/lib/esm/vertexai/models/imagen.js.map +1 -0
  140. package/lib/esm/vertexai/models/llama.js +179 -0
  141. package/lib/esm/vertexai/models/llama.js.map +1 -0
  142. package/lib/esm/vertexai/models.js +32 -0
  143. package/lib/esm/vertexai/models.js.map +1 -0
  144. package/lib/esm/watsonx/index.js +157 -0
  145. package/lib/esm/watsonx/index.js.map +1 -0
  146. package/lib/esm/watsonx/interfaces.js +2 -0
  147. package/lib/esm/watsonx/interfaces.js.map +1 -0
  148. package/lib/esm/xai/index.js +58 -0
  149. package/lib/esm/xai/index.js.map +1 -0
  150. package/lib/types/adobe/firefly.d.ts +30 -0
  151. package/lib/types/adobe/firefly.d.ts.map +1 -0
  152. package/lib/types/azure/azure_foundry.d.ts +52 -0
  153. package/lib/types/azure/azure_foundry.d.ts.map +1 -0
  154. package/lib/types/bedrock/converse.d.ts +8 -0
  155. package/lib/types/bedrock/converse.d.ts.map +1 -0
  156. package/lib/types/bedrock/index.d.ts +135 -0
  157. package/lib/types/bedrock/index.d.ts.map +1 -0
  158. package/lib/types/bedrock/nova-image-payload.d.ts +74 -0
  159. package/lib/types/bedrock/nova-image-payload.d.ts.map +1 -0
  160. package/lib/types/bedrock/payloads.d.ts +12 -0
  161. package/lib/types/bedrock/payloads.d.ts.map +1 -0
  162. package/lib/types/bedrock/s3.d.ts +23 -0
  163. package/lib/types/bedrock/s3.d.ts.map +1 -0
  164. package/lib/types/bedrock/twelvelabs.d.ts +50 -0
  165. package/lib/types/bedrock/twelvelabs.d.ts.map +1 -0
  166. package/lib/types/groq/index.d.ts +27 -0
  167. package/lib/types/groq/index.d.ts.map +1 -0
  168. package/lib/types/huggingface_ie.d.ts +35 -0
  169. package/lib/types/huggingface_ie.d.ts.map +1 -0
  170. package/lib/types/index.d.ts +15 -0
  171. package/lib/types/index.d.ts.map +1 -0
  172. package/lib/types/mistral/index.d.ts +25 -0
  173. package/lib/types/mistral/index.d.ts.map +1 -0
  174. package/lib/types/mistral/types.d.ts +127 -0
  175. package/lib/types/mistral/types.d.ts.map +1 -0
  176. package/lib/types/openai/azure_openai.d.ts +25 -0
  177. package/lib/types/openai/azure_openai.d.ts.map +1 -0
  178. package/lib/types/openai/index.d.ts +126 -0
  179. package/lib/types/openai/index.d.ts.map +1 -0
  180. package/lib/types/openai/openai.d.ts +15 -0
  181. package/lib/types/openai/openai.d.ts.map +1 -0
  182. package/lib/types/openai/openai_compatible.d.ts +31 -0
  183. package/lib/types/openai/openai_compatible.d.ts.map +1 -0
  184. package/lib/types/openai/openai_format.d.ts +21 -0
  185. package/lib/types/openai/openai_format.d.ts.map +1 -0
  186. package/lib/types/replicate.d.ts +48 -0
  187. package/lib/types/replicate.d.ts.map +1 -0
  188. package/lib/types/test-driver/TestErrorCompletionStream.d.ts +9 -0
  189. package/lib/types/test-driver/TestErrorCompletionStream.d.ts.map +1 -0
  190. package/lib/types/test-driver/TestValidationErrorCompletionStream.d.ts +9 -0
  191. package/lib/types/test-driver/TestValidationErrorCompletionStream.d.ts.map +1 -0
  192. package/lib/types/test-driver/index.d.ts +24 -0
  193. package/lib/types/test-driver/index.d.ts.map +1 -0
  194. package/lib/types/test-driver/utils.d.ts +5 -0
  195. package/lib/types/test-driver/utils.d.ts.map +1 -0
  196. package/lib/types/togetherai/index.d.ts +23 -0
  197. package/lib/types/togetherai/index.d.ts.map +1 -0
  198. package/lib/types/togetherai/interfaces.d.ts +96 -0
  199. package/lib/types/togetherai/interfaces.d.ts.map +1 -0
  200. package/lib/types/vertexai/debug.d.ts +2 -0
  201. package/lib/types/vertexai/debug.d.ts.map +1 -0
  202. package/lib/types/vertexai/embeddings/embeddings-image.d.ts +11 -0
  203. package/lib/types/vertexai/embeddings/embeddings-image.d.ts.map +1 -0
  204. package/lib/types/vertexai/embeddings/embeddings-text.d.ts +10 -0
  205. package/lib/types/vertexai/embeddings/embeddings-text.d.ts.map +1 -0
  206. package/lib/types/vertexai/index.d.ts +79 -0
  207. package/lib/types/vertexai/index.d.ts.map +1 -0
  208. package/lib/types/vertexai/models/claude.d.ts +103 -0
  209. package/lib/types/vertexai/models/claude.d.ts.map +1 -0
  210. package/lib/types/vertexai/models/gemini.d.ts +78 -0
  211. package/lib/types/vertexai/models/gemini.d.ts.map +1 -0
  212. package/lib/types/vertexai/models/imagen.d.ts +75 -0
  213. package/lib/types/vertexai/models/imagen.d.ts.map +1 -0
  214. package/lib/types/vertexai/models/llama.d.ts +20 -0
  215. package/lib/types/vertexai/models/llama.d.ts.map +1 -0
  216. package/lib/types/vertexai/models.d.ts +20 -0
  217. package/lib/types/vertexai/models.d.ts.map +1 -0
  218. package/lib/types/watsonx/index.d.ts +27 -0
  219. package/lib/types/watsonx/index.d.ts.map +1 -0
  220. package/lib/types/watsonx/interfaces.d.ts +65 -0
  221. package/lib/types/watsonx/interfaces.d.ts.map +1 -0
  222. package/lib/types/xai/index.d.ts +18 -0
  223. package/lib/types/xai/index.d.ts.map +1 -0
  224. package/package.json +18 -18
  225. package/src/bedrock/converse.ts +85 -10
  226. package/src/bedrock/error-handling.test.ts +352 -0
  227. package/src/bedrock/index.ts +293 -16
  228. package/src/groq/index.ts +9 -4
  229. package/src/mistral/index.ts +25 -22
  230. package/src/mistral/types.ts +0 -5
  231. package/src/openai/error-handling.test.ts +567 -0
  232. package/src/openai/index.ts +513 -33
  233. package/src/openai/openai_compatible.ts +7 -0
  234. package/src/openai/openai_format.ts +1 -1
  235. package/src/vertexai/index.ts +61 -13
  236. package/src/vertexai/models/claude-error-handling.test.ts +432 -0
  237. package/src/vertexai/models/claude.ts +287 -10
  238. package/src/vertexai/models/gemini-error-handling.test.ts +353 -0
  239. package/src/vertexai/models/gemini.ts +329 -52
  240. package/src/vertexai/models.ts +7 -2
@@ -0,0 +1,1104 @@
1
+ import { FinishReason, FunctionCallingConfigMode, HarmBlockThreshold, HarmCategory, Modality, ProminentPeople, ThinkingLevel, Type } from "@google/genai";
2
+ import { getConversationMeta, getGeminiModelVersion, incrementConversationTurn, isGeminiModelVersionGte, LlumiverseError, ModelType, PromptRole, readStreamAsBase64, stripBase64ImagesFromConversation, stripHeartbeatsFromConversation, truncateLargeTextInConversation, unwrapConversationArray } from "@llumiverse/core";
3
+ import { asyncMap } from "@llumiverse/core/async";
4
+ function supportsStructuredOutput(options) {
5
+ // Gemini 1.0 Ultra does not support JSON output, 1.0 Pro does.
6
+ return !!options.result_schema && !options.model.includes("ultra");
7
+ }
8
+ const geminiSafetySettings = [
9
+ {
10
+ category: HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,
11
+ threshold: HarmBlockThreshold.BLOCK_ONLY_HIGH
12
+ },
13
+ {
14
+ category: HarmCategory.HARM_CATEGORY_HARASSMENT,
15
+ threshold: HarmBlockThreshold.BLOCK_ONLY_HIGH
16
+ },
17
+ {
18
+ category: HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT,
19
+ threshold: HarmBlockThreshold.BLOCK_ONLY_HIGH
20
+ },
21
+ {
22
+ category: HarmCategory.HARM_CATEGORY_HATE_SPEECH,
23
+ threshold: HarmBlockThreshold.BLOCK_ONLY_HIGH
24
+ },
25
+ {
26
+ category: HarmCategory.HARM_CATEGORY_UNSPECIFIED,
27
+ threshold: HarmBlockThreshold.BLOCK_ONLY_HIGH
28
+ },
29
+ {
30
+ category: HarmCategory.HARM_CATEGORY_CIVIC_INTEGRITY,
31
+ threshold: HarmBlockThreshold.BLOCK_ONLY_HIGH
32
+ }
33
+ ];
34
+ // We do the mapping here rather than in common to avoid bringing the SDK into the common package.
35
+ function getProminentPeopleOption(prominentPeople) {
36
+ switch (prominentPeople) {
37
+ case "ALLOW_PROMINENT_PEOPLE":
38
+ return ProminentPeople.ALLOW_PROMINENT_PEOPLE;
39
+ case "BLOCK_PROMINENT_PEOPLE":
40
+ return ProminentPeople.BLOCK_PROMINENT_PEOPLE;
41
+ case "PROMINENT_PEOPLE_UNSPECIFIED":
42
+ return ProminentPeople.PROMINENT_PEOPLE_UNSPECIFIED;
43
+ default:
44
+ return undefined;
45
+ }
46
+ }
47
+ function getGeminiPayload(options, prompt) {
48
+ const model_options = options.model_options;
49
+ const tools = getToolDefinitions(options.tools);
50
+ // When no tools are provided but conversation contains functionCall/functionResponse parts
51
+ // (e.g. checkpoint summary calls), convert them to text to avoid API errors
52
+ if (!tools && prompt.contents) {
53
+ const hasToolParts = prompt.contents.some(c => c.parts?.some(p => p.functionCall || p.functionResponse));
54
+ if (hasToolParts) {
55
+ prompt.contents = convertGeminiFunctionPartsToText(prompt.contents);
56
+ }
57
+ }
58
+ const useStructuredOutput = supportsStructuredOutput(options) && !tools;
59
+ const configNanoBanana = {
60
+ systemInstruction: prompt.system,
61
+ safetySettings: geminiSafetySettings,
62
+ responseModalities: [Modality.TEXT, Modality.IMAGE], // This is an error if only Text, and Only Image just gets blank responses.
63
+ candidateCount: 1,
64
+ //Model options
65
+ temperature: model_options?.temperature,
66
+ topP: model_options?.top_p,
67
+ maxOutputTokens: model_options?.max_tokens,
68
+ stopSequences: model_options?.stop_sequence,
69
+ thinkingConfig: geminiThinkingConfig(options),
70
+ imageConfig: {
71
+ imageSize: model_options?.image_size,
72
+ aspectRatio: model_options?.image_aspect_ratio,
73
+ personGeneration: model_options?.person_generation,
74
+ prominentPeople: getProminentPeopleOption(model_options?.prominent_people),
75
+ outputMimeType: model_options?.output_mime_type,
76
+ outputCompressionQuality: model_options?.output_compression_quality,
77
+ }
78
+ };
79
+ const config = {
80
+ systemInstruction: prompt.system,
81
+ safetySettings: geminiSafetySettings,
82
+ tools: tools ? [tools] : undefined,
83
+ toolConfig: tools ? {
84
+ functionCallingConfig: {
85
+ mode: FunctionCallingConfigMode.AUTO,
86
+ }
87
+ } : undefined,
88
+ candidateCount: 1,
89
+ //JSON/Structured output
90
+ responseMimeType: useStructuredOutput ? "application/json" : undefined,
91
+ responseSchema: useStructuredOutput ? parseJSONtoSchema(options.result_schema, true) : undefined,
92
+ //Model options
93
+ temperature: model_options?.temperature,
94
+ topP: model_options?.top_p,
95
+ topK: model_options?.top_k,
96
+ maxOutputTokens: model_options?.max_tokens,
97
+ stopSequences: model_options?.stop_sequence,
98
+ presencePenalty: model_options?.presence_penalty,
99
+ frequencyPenalty: model_options?.frequency_penalty,
100
+ seed: model_options?.seed,
101
+ thinkingConfig: geminiThinkingConfig(options),
102
+ };
103
+ return {
104
+ model: options.model,
105
+ contents: prompt.contents,
106
+ config: options.model.toLowerCase().includes("image") ? configNanoBanana : config,
107
+ };
108
+ }
109
+ /**
110
+ * Convert JSONSchema to Gemini Schema,
111
+ * Make all properties required by default
112
+ * Properties previously marked as optional will be marked as nullable.
113
+ */
114
+ function parseJSONtoSchema(schema, requiredAll = false) {
115
+ if (!schema) {
116
+ return {};
117
+ }
118
+ return convertSchema(schema, 0, requiredAll);
119
+ }
120
+ /**
121
+ * Convert JSONSchema type to Gemini Schema Type
122
+ */
123
+ function convertType(type) {
124
+ if (!type)
125
+ return undefined;
126
+ // Handle single type
127
+ if (typeof type === 'string') {
128
+ switch (type) {
129
+ case 'string': return Type.STRING;
130
+ case 'number': return Type.NUMBER;
131
+ case 'integer': return Type.INTEGER;
132
+ case 'boolean': return Type.BOOLEAN;
133
+ case 'object': return Type.OBJECT;
134
+ case 'array': return Type.ARRAY;
135
+ default: return type; // For unsupported types, return as is
136
+ }
137
+ }
138
+ // For array of types, take the first valid one as the primary type
139
+ // The full set of types will be handled with anyOf
140
+ for (const t of type) {
141
+ const converted = convertType(t);
142
+ if (converted)
143
+ return converted;
144
+ }
145
+ return undefined;
146
+ }
147
+ /**
148
+ * Deep clone and convert the schema from JSONSchema to Gemini Schema
149
+ * @throws {Error} If circular references are detected (max depth exceeded)
150
+ */
151
+ function convertSchema(jsSchema, depth = 0, requiredAll = false) {
152
+ // Prevent circular references
153
+ if (depth > 20) {
154
+ throw new Error("Maximum schema depth (20) exceeded. Possible circular reference detected.");
155
+ }
156
+ if (!jsSchema)
157
+ return {};
158
+ // Create new schema object rather than mutating
159
+ const result = {};
160
+ // Handle types
161
+ result.type = convertSchemaType(jsSchema);
162
+ // Handle description
163
+ if (jsSchema.description) {
164
+ result.description = jsSchema.description;
165
+ }
166
+ // Handle properties and required fields
167
+ if (jsSchema.properties) {
168
+ const propertyResult = convertSchemaProperties(jsSchema, depth + 1, requiredAll);
169
+ Object.assign(result, propertyResult);
170
+ }
171
+ // Handle items for arrays
172
+ if (jsSchema.items) {
173
+ result.items = convertSchema(jsSchema.items, depth + 1);
174
+ }
175
+ // Handle enum values
176
+ if (jsSchema.enum) {
177
+ result.enum = [...jsSchema.enum]; // Create a copy instead of reference
178
+ }
179
+ // Copy constraints
180
+ Object.assign(result, extractConstraints(jsSchema));
181
+ return result;
182
+ }
183
+ /**
184
+ * Convert schema type information, handling anyOf for multiple types
185
+ */
186
+ function convertSchemaType(jsSchema) {
187
+ // Handle multiple types using anyOf
188
+ if (jsSchema.type && Array.isArray(jsSchema.type) && jsSchema.type.length > 1) {
189
+ // Since anyOf is an advanced type, we'll return the first valid type
190
+ // and handle the multi-type case separately in the schema
191
+ return convertType(jsSchema.type[0]);
192
+ }
193
+ // Handle single type
194
+ else if (jsSchema.type) {
195
+ return convertType(jsSchema.type);
196
+ }
197
+ return undefined;
198
+ }
199
+ /**
200
+ * Handle properties conversion and required fields
201
+ */
202
+ function convertSchemaProperties(jsSchema, depth, requiredAll) {
203
+ const result = { properties: {} };
204
+ if (jsSchema.required) {
205
+ result.required = [...jsSchema.required]; // Create a copy
206
+ }
207
+ // Extract property ordering from the object keys
208
+ const propertyNames = Object.keys(jsSchema.properties || {});
209
+ // Set property ordering based on the existing order in the schema
210
+ if (propertyNames.length > 0) {
211
+ result.propertyOrdering = propertyNames;
212
+ if (requiredAll) {
213
+ // Mark all properties as required by default
214
+ // This ensures the model fills all fields
215
+ result.required = propertyNames;
216
+ // Get the original required properties
217
+ const originalRequired = jsSchema.required || [];
218
+ // Make previously optional properties nullable since we're marking them as required
219
+ for (const key of propertyNames) {
220
+ const propSchema = jsSchema.properties?.[key];
221
+ if (propSchema && !originalRequired.includes(key)) {
222
+ // Initialize the property if needed
223
+ if (!result.properties[key]) {
224
+ result.properties[key] = {};
225
+ }
226
+ // Mark as nullable
227
+ result.properties[key].nullable = true;
228
+ }
229
+ }
230
+ }
231
+ }
232
+ // Convert each property schema
233
+ for (const [key, value] of Object.entries(jsSchema.properties || {})) {
234
+ if (!result.properties[key]) {
235
+ result.properties[key] = {};
236
+ }
237
+ // Merge with converted schema
238
+ result.properties[key] = {
239
+ ...result.properties[key],
240
+ ...convertSchema(value, depth)
241
+ };
242
+ }
243
+ // Override with explicit propertyOrdering if present
244
+ if (jsSchema.propertyOrdering) {
245
+ result.propertyOrdering = [...jsSchema.propertyOrdering]; // Create a copy
246
+ }
247
+ return result;
248
+ }
249
+ /**
250
+ * Extract schema constraints (min/max values, formats, etc.)
251
+ */
252
+ function extractConstraints(jsSchema) {
253
+ const constraints = {};
254
+ if (jsSchema.minimum !== undefined)
255
+ constraints.minimum = jsSchema.minimum;
256
+ if (jsSchema.maximum !== undefined)
257
+ constraints.maximum = jsSchema.maximum;
258
+ if (jsSchema.minLength !== undefined)
259
+ constraints.minLength = jsSchema.minLength;
260
+ if (jsSchema.maxLength !== undefined)
261
+ constraints.maxLength = jsSchema.maxLength;
262
+ if (jsSchema.minItems !== undefined)
263
+ constraints.minItems = jsSchema.minItems;
264
+ if (jsSchema.maxItems !== undefined)
265
+ constraints.maxItems = jsSchema.maxItems;
266
+ if (jsSchema.nullable !== undefined)
267
+ constraints.nullable = jsSchema.nullable;
268
+ if (jsSchema.pattern)
269
+ constraints.pattern = jsSchema.pattern;
270
+ if (jsSchema.format)
271
+ constraints.format = jsSchema.format;
272
+ if (jsSchema.default !== undefined)
273
+ constraints.default = jsSchema.default;
274
+ if (jsSchema.example !== undefined)
275
+ constraints.example = jsSchema.example;
276
+ return constraints;
277
+ }
278
+ /**
279
+ * Check if a value is empty (null, undefined, empty string, empty array, empty object)
280
+ * @param value The value to check
281
+ * @returns True if the value is considered empty
282
+ */
283
+ function isEmpty(value) {
284
+ if (value === null || value === undefined) {
285
+ return true;
286
+ }
287
+ if (typeof value === 'string' && value.trim() === '') {
288
+ return true;
289
+ }
290
+ if (Array.isArray(value) && value.length === 0) {
291
+ return true;
292
+ }
293
+ // Check for empty object (no own enumerable properties)
294
+ if (typeof value === 'object' && Object.keys(value).length === 0) {
295
+ return true;
296
+ }
297
+ // Check for array of empty objects
298
+ if (Array.isArray(value) && value.every(item => isEmpty(item))) {
299
+ return true;
300
+ }
301
+ return false;
302
+ }
303
+ // No array cleaning function needed as we're only working with JSONObjects
304
+ /**
305
+ * Clean up the JSON result by removing empty values for optional fields
306
+ * Uses immutable patterns to create a new Content object rather than modifying the original
307
+ * @param content The original content from Gemini
308
+ * @param result_schema The JSON schema to use for cleaning
309
+ * @returns A new Content object with cleaned JSON text
310
+ */
311
+ function cleanEmptyFieldsContent(content, result_schema) {
312
+ // If no schema provided, return original content
313
+ if (!result_schema) {
314
+ return content;
315
+ }
316
+ // Create a new content object (shallow copy)
317
+ const cleanedContent = { ...content };
318
+ // Create a new parts array if it exists
319
+ if (cleanedContent.parts) {
320
+ cleanedContent.parts = cleanedContent.parts.map(part => {
321
+ // Only process parts with text
322
+ if (!part.text) {
323
+ return part; // Return unchanged if no text
324
+ }
325
+ // Create a new part object
326
+ const newPart = { ...part };
327
+ try {
328
+ // Parse JSON, clean it based on schema, then stringify
329
+ const jsonText = JSON.parse(part.text);
330
+ // Skip cleaning if not an object
331
+ if (typeof jsonText === 'object' && jsonText !== null && !Array.isArray(jsonText)) {
332
+ const cleanedJson = removeEmptyFields(jsonText, result_schema);
333
+ newPart.text = JSON.stringify(cleanedJson);
334
+ }
335
+ else {
336
+ // Keep original if not an object (string, number, array, etc.)
337
+ newPart.text = part.text;
338
+ }
339
+ }
340
+ catch (e) {
341
+ // On error, keep the original text
342
+ console.warn("Error parsing Gemini output to JSON in part:", e);
343
+ }
344
+ return newPart;
345
+ });
346
+ }
347
+ return cleanedContent;
348
+ }
349
+ /**
350
+ * Removes empty optional fields from the JSON result based on the provided schema
351
+ * @param object The object to clean
352
+ * @param schema The JSON schema to use for cleaning
353
+ * @returns A new object with empty optional fields removed
354
+ */
355
+ function removeEmptyFields(object, schema) {
356
+ if (!object) {
357
+ return object;
358
+ }
359
+ if (Array.isArray(object)) {
360
+ return removeEmptyJSONArray(object, schema);
361
+ }
362
+ if (typeof object == 'object' || object === null) {
363
+ return removeEmptyJSONObject(object, schema);
364
+ }
365
+ return object;
366
+ }
367
+ function removeEmptyJSONObject(object, schema) {
368
+ // Get the original required properties from schema
369
+ const requiredProps = schema.required || [];
370
+ const cleanedResult = { ...object };
371
+ // Process each property
372
+ for (const [key, value] of Object.entries(object)) {
373
+ const isRequired = requiredProps.includes(key);
374
+ const propSchema = schema.properties?.[key];
375
+ // Recursively clean nested objects based on their schema
376
+ cleanedResult[key] = removeEmptyFields(value, propSchema ?? {});
377
+ if (isEmpty(value)) {
378
+ if (isRequired) {
379
+ continue; // Keep required fields even if empty
380
+ }
381
+ else {
382
+ delete cleanedResult[key]; // Remove empty optional fields
383
+ }
384
+ }
385
+ }
386
+ return cleanedResult;
387
+ }
388
+ function removeEmptyJSONArray(array, schema) {
389
+ const cleanedArray = array.map(item => {
390
+ return removeEmptyFields(item, schema);
391
+ });
392
+ // Filter out empty objects from the array
393
+ return cleanedArray.filter(item => !isEmpty(item));
394
+ }
395
+ /**
396
+ * Collect all parts (text and images) from content in order.
397
+ * This preserves the original ordering of text and image parts.
398
+ */
399
+ function extractCompletionResults(content) {
400
+ const results = [];
401
+ const parts = content.parts;
402
+ if (parts) {
403
+ for (const part of parts) {
404
+ if (part.text) {
405
+ results.push({
406
+ type: "text",
407
+ value: part.text
408
+ });
409
+ }
410
+ else if (part.inlineData) {
411
+ const base64ImageBytes = part.inlineData.data ?? "";
412
+ const mimeType = part.inlineData.mimeType ?? "image/png";
413
+ const imageUrl = `data:${mimeType};base64,${base64ImageBytes}`;
414
+ results.push({
415
+ type: "image",
416
+ value: imageUrl
417
+ });
418
+ }
419
+ }
420
+ }
421
+ return results;
422
+ }
423
+ function collectToolUseParts(content) {
424
+ const out = [];
425
+ const parts = content.parts ?? [];
426
+ for (const part of parts) {
427
+ if (part.functionCall) {
428
+ const toolUse = {
429
+ id: part.functionCall.name ?? '',
430
+ tool_name: part.functionCall.name ?? '',
431
+ tool_input: part.functionCall.args,
432
+ };
433
+ // Capture thought_signature for Gemini thinking models (2.5+/3.0+)
434
+ // This must be passed back with the function response
435
+ if (part.thoughtSignature) {
436
+ toolUse.thought_signature = part.thoughtSignature;
437
+ }
438
+ out.push(toolUse);
439
+ }
440
+ }
441
+ return out.length > 0 ? out : undefined;
442
+ }
443
+ export function mergeConsecutiveRole(contents) {
444
+ if (!contents || contents.length === 0)
445
+ return [];
446
+ const needsMerging = contents.some((content, i) => i < contents.length - 1 && content.role === contents[i + 1].role);
447
+ // If no merging needed, return original array
448
+ if (!needsMerging) {
449
+ return contents;
450
+ }
451
+ const result = [];
452
+ let currentContent = { ...contents[0], parts: [...(contents[0].parts || [])] };
453
+ for (let i = 1; i < contents.length; i++) {
454
+ if (currentContent.role === contents[i].role) {
455
+ // Same role - concatenate parts (without merging individual parts)
456
+ currentContent.parts = (currentContent.parts || []).concat(...(contents[i].parts || []));
457
+ }
458
+ else {
459
+ // Different role - push current and start new
460
+ result.push(currentContent);
461
+ currentContent = { ...contents[i], parts: [...(contents[i].parts || [])] };
462
+ }
463
+ }
464
+ result.push(currentContent);
465
+ return result;
466
+ }
467
+ const supportedFinishReasons = [
468
+ FinishReason.MAX_TOKENS,
469
+ FinishReason.STOP,
470
+ FinishReason.FINISH_REASON_UNSPECIFIED,
471
+ ];
472
+ // Finish reasons that indicate tool call issues but should be recovered gracefully
473
+ // instead of throwing an error. The tool_use is still extracted and returned
474
+ // so the workflow can generate a proper toolError response.
475
+ const recoverableToolCallReasons = [
476
+ 'UNEXPECTED_TOOL_CALL', // Model called an undeclared tool
477
+ ];
478
+ function geminiThinkingBudget(option) {
479
+ const model_options = option.model_options;
480
+ // If thinking_budget_tokens is explicitly set in model options, use it directly
481
+ if (model_options?.thinking_budget_tokens) {
482
+ return model_options.thinking_budget_tokens;
483
+ }
484
+ // Set minimum thinking level by default.
485
+ // Docs: https://ai.google.dev/gemini-api/docs/thinking#set-budget
486
+ if (getGeminiModelVersion(option.model) == '2.5') {
487
+ if (option.model.includes("pro")) {
488
+ return 128;
489
+ }
490
+ return 0;
491
+ }
492
+ return undefined;
493
+ }
494
+ function geminiThinkingConfig(option) {
495
+ const model_options = option.model_options;
496
+ // If thinking options are explicitly set in model options, use them directly
497
+ const include_thoughts = model_options?.include_thoughts ?? false;
498
+ if (model_options?.thinking_budget_tokens || model_options?.thinking_level) {
499
+ return {
500
+ includeThoughts: include_thoughts,
501
+ thinkingBudget: model_options.thinking_budget_tokens,
502
+ thinkingLevel: model_options.thinking_level,
503
+ };
504
+ }
505
+ // Set a low thinking level by default.
506
+ // Docs: https://ai.google.dev/gemini-api/docs/thinking#set-budget
507
+ // https://docs.cloud.google.com/vertex-ai/generative-ai/docs/thinking
508
+ if (isGeminiModelVersionGte(option.model, '3.0')) {
509
+ return {
510
+ includeThoughts: include_thoughts,
511
+ thinkingLevel: ThinkingLevel.LOW
512
+ };
513
+ }
514
+ if (isGeminiModelVersionGte(option.model, '2.5')) {
515
+ const thinking_budget_tokens = geminiThinkingBudget(option) ?? 0;
516
+ return {
517
+ includeThoughts: include_thoughts,
518
+ thinkingBudget: thinking_budget_tokens
519
+ };
520
+ }
521
+ }
522
+ export class GeminiModelDefinition {
523
+ model;
524
+ constructor(modelId) {
525
+ this.model = {
526
+ id: modelId,
527
+ name: modelId,
528
+ provider: 'vertexai',
529
+ type: ModelType.Text,
530
+ can_stream: true
531
+ };
532
+ }
533
+ preValidationProcessing(result, options) {
534
+ // Guard clause, if no result_schema, error, or tool use, skip processing
535
+ if (!options.result_schema || !result.result || result.tool_use || result.error) {
536
+ return { result, options };
537
+ }
538
+ try {
539
+ // Extract text content for JSON processing - only process first text result
540
+ const textResult = result.result.find(r => r.type === 'text')?.value;
541
+ if (textResult) {
542
+ const jsonResult = JSON.parse(textResult);
543
+ const cleanedJson = JSON.stringify(removeEmptyFields(jsonResult, options.result_schema));
544
+ // Replace the text result with cleaned version
545
+ result.result = result.result.map(r => r.type === 'text' ? { ...r, value: cleanedJson } : r);
546
+ }
547
+ return { result, options };
548
+ }
549
+ catch (error) {
550
+ // Log error during processing but don't fail the completion
551
+ console.warn('Error during Gemini JSON pre-validation: ', error);
552
+ // Return original result if cleanup fails
553
+ return { result, options };
554
+ }
555
+ }
556
+ async createPrompt(_driver, segments, options) {
557
+ const splits = options.model.split("/");
558
+ const modelName = splits[splits.length - 1];
559
+ options = { ...options, model: modelName };
560
+ const schema = options.result_schema;
561
+ let contents = [];
562
+ let system = { role: "user", parts: [] }; // Single content block for system messages
563
+ const safety = [];
564
+ for (const msg of segments) {
565
+ // Role specific handling
566
+ if (msg.role === PromptRole.system) {
567
+ // Text only for system messages
568
+ if (msg.files && msg.files.length > 0) {
569
+ throw new Error("Gemini does not support files/images etc. in system messages. Only text content is allowed.");
570
+ }
571
+ if (msg.content) {
572
+ system.parts?.push({
573
+ text: msg.content
574
+ });
575
+ }
576
+ }
577
+ else if (msg.role === PromptRole.tool) {
578
+ if (!msg.tool_use_id) {
579
+ throw new Error("Tool response missing tool_use_id");
580
+ }
581
+ // Build functionResponse part with optional thought_signature for Gemini thinking models
582
+ const functionResponsePart = {
583
+ functionResponse: {
584
+ name: msg.tool_use_id,
585
+ response: formatFunctionResponse(msg.content || ''),
586
+ },
587
+ // Include thought_signature if provided (required for Gemini 2.5+/3.0+ thinking models)
588
+ thoughtSignature: msg.thought_signature,
589
+ };
590
+ contents.push({
591
+ role: 'user',
592
+ parts: [functionResponsePart]
593
+ });
594
+ }
595
+ else { // PromptRole.user, PromptRole.assistant, PromptRole.safety
596
+ const parts = [];
597
+ // Text content handling
598
+ if (msg.content) {
599
+ parts.push({
600
+ text: msg.content,
601
+ });
602
+ }
603
+ // File content handling
604
+ if (msg.files) {
605
+ for (const f of msg.files) {
606
+ const fileUrl = await f.getURL();
607
+ const isGsUrl = fileUrl.startsWith('gs://') || fileUrl.startsWith('https://storage.googleapis.com/');
608
+ if (isGsUrl) {
609
+ parts.push({
610
+ fileData: {
611
+ fileUri: fileUrl,
612
+ mimeType: f.mime_type
613
+ }
614
+ });
615
+ }
616
+ else {
617
+ // Inline data handling
618
+ const stream = await f.getStream();
619
+ const data = await readStreamAsBase64(stream);
620
+ parts.push({
621
+ inlineData: {
622
+ data,
623
+ mimeType: f.mime_type
624
+ }
625
+ });
626
+ }
627
+ }
628
+ }
629
+ if (parts.length > 0) {
630
+ if (msg.role === PromptRole.safety) {
631
+ safety.push({
632
+ role: 'user',
633
+ parts,
634
+ });
635
+ }
636
+ else {
637
+ contents.push({
638
+ role: msg.role === PromptRole.assistant ? 'model' : 'user',
639
+ parts,
640
+ });
641
+ }
642
+ }
643
+ }
644
+ }
645
+ // Adding JSON Schema to system message
646
+ if (schema) {
647
+ if (supportsStructuredOutput(options) && !options.tools) {
648
+ // Gemini structured output is unnecessarily sparse. Adding encouragement to fill the fields.
649
+ // Putting JSON in prompt is not recommended by Google, when using structured output.
650
+ system.parts?.push({ text: "Fill all appropriate fields in the JSON output." });
651
+ }
652
+ else {
653
+ // Fallback to putting the schema in the system instructions, if not using structured output.
654
+ if (options.tools) {
655
+ system.parts?.push({
656
+ text: "When not calling tools, the output must be a JSON object using the following JSON Schema:\n" + JSON.stringify(schema)
657
+ });
658
+ }
659
+ else {
660
+ system.parts?.push({ text: "The output must be a JSON object using the following JSON Schema:\n" + JSON.stringify(schema) });
661
+ }
662
+ }
663
+ }
664
+ // If no system messages, set system to undefined.
665
+ if (!system.parts || system.parts.length === 0) {
666
+ system = undefined;
667
+ }
668
+ // Add safety messages to the end of contents. They are in effect user messages that come at the end.
669
+ if (safety.length > 0) {
670
+ contents = contents.concat(safety);
671
+ }
672
+ // Merge consecutive messages with the same role. Note: this may not be necessary, works without it, keeping to match previous behavior.
673
+ contents = mergeConsecutiveRole(contents);
674
+ return { contents, system };
675
+ }
676
+ usageMetadataToTokenUsage(usageMetadata) {
677
+ if (!usageMetadata || !usageMetadata.totalTokenCount) {
678
+ return {};
679
+ }
680
+ const tokenUsage = { total: usageMetadata.totalTokenCount, prompt: usageMetadata.promptTokenCount };
681
+ //Output/Response side
682
+ tokenUsage.result = (usageMetadata.candidatesTokenCount ?? 0)
683
+ + (usageMetadata.thoughtsTokenCount ?? 0)
684
+ + (usageMetadata.toolUsePromptTokenCount ?? 0);
685
+ if ((tokenUsage.total ?? 0) != (tokenUsage.prompt ?? 0) + tokenUsage.result) {
686
+ console.warn("[VertexAI] Gemini token usage mismatch: total does not equal prompt + result", {
687
+ total: tokenUsage.total,
688
+ prompt: tokenUsage.prompt,
689
+ result: tokenUsage.result
690
+ });
691
+ }
692
+ if (!tokenUsage.result) {
693
+ tokenUsage.result = undefined; // If no result, mark as undefined
694
+ }
695
+ return tokenUsage;
696
+ }
697
+ async requestTextCompletion(driver, prompt, options) {
698
+ const splits = options.model.split("/");
699
+ let region = undefined;
700
+ if (splits[0] === "locations" && splits.length >= 2) {
701
+ region = splits[1];
702
+ }
703
+ const modelName = splits[splits.length - 1];
704
+ options = { ...options, model: modelName };
705
+ // Restore system instruction from stored conversation on resume.
706
+ // The stored _llumiverse_system contains the complete system (interaction prompt + schema)
707
+ // from the initial call. Always prefer it over the prompt's system, which on resume only
708
+ // contains the schema instruction (no interaction system segments are present on resume).
709
+ const existingSystem = extractSystemFromConversation(options.conversation);
710
+ if (existingSystem) {
711
+ prompt.system = existingSystem;
712
+ }
713
+ let conversation = updateConversation(options.conversation, prompt.contents);
714
+ prompt.contents = conversation;
715
+ // TODO: Remove hack, use global endpoint manually if needed.
716
+ if (options.model.includes("gemini-2.5-flash-image")) {
717
+ region = "global"; // Gemini Flash Image only available in global region, this is for nano-banana model
718
+ }
719
+ const client = driver.getGoogleGenAIClient(region);
720
+ const payload = getGeminiPayload(options, prompt);
721
+ const response = await client.models.generateContent(payload);
722
+ const token_usage = this.usageMetadataToTokenUsage(response.usageMetadata);
723
+ let tool_use;
724
+ let finish_reason, result;
725
+ const candidate = response.candidates && response.candidates[0];
726
+ if (candidate) {
727
+ switch (candidate.finishReason) {
728
+ case FinishReason.MAX_TOKENS:
729
+ finish_reason = "length";
730
+ break;
731
+ case FinishReason.STOP:
732
+ finish_reason = "stop";
733
+ break;
734
+ default: finish_reason = candidate.finishReason;
735
+ }
736
+ const content = candidate.content;
737
+ // Check for unsupported finish reasons, but allow recoverable tool call issues
738
+ const isRecoverableToolCall = recoverableToolCallReasons.includes(candidate.finishReason);
739
+ if (candidate.finishReason && !supportedFinishReasons.includes(candidate.finishReason) && !isRecoverableToolCall) {
740
+ throw new Error(`Unsupported finish reason: ${candidate.finishReason}, `
741
+ + `finish message: ${candidate.finishMessage}, `
742
+ + `content: ${JSON.stringify(content, null, 2)}, safety: ${JSON.stringify(candidate.safetyRatings, null, 2)}`);
743
+ }
744
+ if (content) {
745
+ tool_use = collectToolUseParts(content);
746
+ // For recoverable tool call issues, log warning but continue processing
747
+ // The workflow will handle the invalid tool call gracefully
748
+ if (isRecoverableToolCall && tool_use && tool_use.length > 0) {
749
+ console.warn(`[Gemini] Recoverable tool call issue (${candidate.finishReason}): ` +
750
+ `Model tried to call undeclared tool(s): ${tool_use.map(t => t.tool_name).join(', ')}`);
751
+ }
752
+ // We clean the content before validation, so we can update the conversation.
753
+ const cleanedContent = cleanEmptyFieldsContent(content, options.result_schema);
754
+ // Collect all parts in order (text and images)
755
+ result = extractCompletionResults(cleanedContent);
756
+ conversation = updateConversation(conversation, [cleanedContent]);
757
+ }
758
+ }
759
+ if (tool_use) {
760
+ finish_reason = "tool_use";
761
+ }
762
+ // Increment turn counter for deferred stripping
763
+ conversation = incrementConversationTurn(conversation);
764
+ // Strip large base64 image data based on options.stripImagesAfterTurns
765
+ const currentTurn = getConversationMeta(conversation).turnNumber;
766
+ const stripOptions = {
767
+ keepForTurns: options.stripImagesAfterTurns ?? Infinity,
768
+ currentTurn,
769
+ textMaxTokens: options.stripTextMaxTokens
770
+ };
771
+ let processedConversation = stripBase64ImagesFromConversation(conversation, stripOptions);
772
+ // Truncate large text content if configured
773
+ processedConversation = truncateLargeTextInConversation(processedConversation, stripOptions);
774
+ // Strip old heartbeat status messages
775
+ processedConversation = stripHeartbeatsFromConversation(processedConversation, {
776
+ keepForTurns: options.stripHeartbeatsAfterTurns ?? 1,
777
+ currentTurn,
778
+ });
779
+ // Preserve system instruction in conversation for multi-turn support
780
+ const finalConversation = storeSystemInConversation(processedConversation, prompt.system);
781
+ return {
782
+ result: result && result.length > 0 ? result : [{ type: "text", value: '' }],
783
+ token_usage: token_usage,
784
+ finish_reason: finish_reason,
785
+ original_response: options.include_original_response ? response : undefined,
786
+ conversation: finalConversation,
787
+ tool_use
788
+ };
789
+ }
790
+ async requestTextCompletionStream(driver, prompt, options) {
791
+ const splits = options.model.split("/");
792
+ let region = undefined;
793
+ if (splits[0] === "locations" && splits.length >= 2) {
794
+ region = splits[1];
795
+ }
796
+ const modelName = splits[splits.length - 1];
797
+ options = { ...options, model: modelName };
798
+ // Restore system instruction from stored conversation on resume.
799
+ // The stored _llumiverse_system contains the complete system (interaction prompt + schema)
800
+ // from the initial call. Always prefer it over the prompt's system, which on resume only
801
+ // contains the schema instruction (no interaction system segments are present on resume).
802
+ const existingSystem = extractSystemFromConversation(options.conversation);
803
+ if (existingSystem) {
804
+ prompt.system = existingSystem;
805
+ }
806
+ // Include conversation history in prompt contents (same as non-streaming)
807
+ const conversation = updateConversation(options.conversation, prompt.contents);
808
+ prompt.contents = conversation;
809
+ if (options.model.includes("gemini-2.5-flash-image")) {
810
+ region = "global"; // Gemini Flash Image only available in global region, this is for nano-banana model
811
+ }
812
+ const client = driver.getGoogleGenAIClient(region);
813
+ const payload = getGeminiPayload(options, prompt);
814
+ const response = await client.models.generateContentStream(payload);
815
+ const stream = asyncMap(response, async (item) => {
816
+ const token_usage = this.usageMetadataToTokenUsage(item.usageMetadata);
817
+ if (item.candidates && item.candidates.length > 0) {
818
+ for (const candidate of item.candidates) {
819
+ let tool_use;
820
+ let finish_reason;
821
+ switch (candidate.finishReason) {
822
+ case FinishReason.MAX_TOKENS:
823
+ finish_reason = "length";
824
+ break;
825
+ case FinishReason.STOP:
826
+ finish_reason = "stop";
827
+ break;
828
+ default: finish_reason = candidate.finishReason;
829
+ }
830
+ // Check for unsupported finish reasons, but allow recoverable tool call issues
831
+ const isRecoverableToolCall = recoverableToolCallReasons.includes(candidate.finishReason);
832
+ if (candidate.finishReason && !supportedFinishReasons.includes(candidate.finishReason) && !isRecoverableToolCall) {
833
+ throw new Error(`Unsupported finish reason: ${candidate.finishReason}, `
834
+ + `finish message: ${candidate.finishMessage}, `
835
+ + `content: ${JSON.stringify(candidate.content, null, 2)}, safety: ${JSON.stringify(candidate.safetyRatings, null, 2)}`);
836
+ }
837
+ if (candidate.content?.role === 'model') {
838
+ // Collect all parts in order (text and images)
839
+ const combinedResults = extractCompletionResults(candidate.content);
840
+ tool_use = collectToolUseParts(candidate.content);
841
+ if (tool_use) {
842
+ finish_reason = "tool_use";
843
+ // Log warning for recoverable tool call issues
844
+ if (isRecoverableToolCall) {
845
+ console.warn(`[Gemini] Recoverable tool call issue (${candidate.finishReason}): ` +
846
+ `Model tried to call undeclared tool(s): ${tool_use.map(t => t.tool_name).join(', ')}`);
847
+ }
848
+ }
849
+ return {
850
+ result: combinedResults.length > 0 ? combinedResults : [],
851
+ token_usage: token_usage,
852
+ finish_reason: finish_reason,
853
+ tool_use,
854
+ };
855
+ }
856
+ }
857
+ }
858
+ //No normal output, returning block reason if it exists.
859
+ return {
860
+ result: item.promptFeedback?.blockReasonMessage ? [{ type: "text", value: item.promptFeedback.blockReasonMessage }] : [],
861
+ finish_reason: item.promptFeedback?.blockReason ?? "",
862
+ token_usage: token_usage,
863
+ };
864
+ });
865
+ return stream;
866
+ }
867
+ /**
868
+ * Format Google API errors into LlumiverseError with proper status codes and retryability.
869
+ *
870
+ * Google API errors follow AIP-193 standard:
871
+ * - ApiError.status: HTTP status code
872
+ * - ApiError.message: Error message
873
+ *
874
+ * Common error codes:
875
+ * - 400 (INVALID_ARGUMENT): Invalid request parameters
876
+ * - 401 (UNAUTHENTICATED): Authentication required
877
+ * - 403 (PERMISSION_DENIED): Insufficient permissions
878
+ * - 404 (NOT_FOUND): Resource not found
879
+ * - 429 (RESOURCE_EXHAUSTED): Rate limit/quota exceeded
880
+ * - 500 (INTERNAL): Internal server error
881
+ * - 503 (UNAVAILABLE): Service temporarily unavailable
882
+ * - 504 (DEADLINE_EXCEEDED): Request timeout
883
+ *
884
+ * @see https://google.aip.dev/193
885
+ * @see https://docs.cloud.google.com/vertex-ai/generative-ai/docs/model-reference/api-errors
886
+ */
887
+ formatLlumiverseError(_driver, error, context) {
888
+ // Check if it's a Google API error with status code
889
+ const isApiError = this.isGoogleApiError(error);
890
+ if (!isApiError) {
891
+ // Not a Google API error, use default handling
892
+ // This will be called by the driver's default formatLlumiverseError
893
+ throw error;
894
+ }
895
+ const apiError = error;
896
+ const httpStatusCode = apiError.status;
897
+ // Extract error message
898
+ const message = apiError.message || String(error);
899
+ // Build user-facing message with status code
900
+ let userMessage = message;
901
+ // Include status code in message (for end-user visibility)
902
+ if (httpStatusCode) {
903
+ userMessage = `[${httpStatusCode}] ${userMessage}`;
904
+ }
905
+ // Determine retryability based on Google error codes
906
+ const retryable = this.isGeminiErrorRetryable(httpStatusCode);
907
+ // Extract error name/type from message if present
908
+ const errorName = this.extractErrorName(message);
909
+ return new LlumiverseError(`[${context.provider}] ${userMessage}`, retryable, context, error, httpStatusCode, errorName);
910
+ }
911
+ /**
912
+ * Type guard to check if error is a Google API error.
913
+ */
914
+ isGoogleApiError(error) {
915
+ return (error !== null &&
916
+ typeof error === 'object' &&
917
+ 'status' in error &&
918
+ typeof error.status === 'number' &&
919
+ 'message' in error);
920
+ }
921
+ /**
922
+ * Determine if a Google API error is retryable based on HTTP status code.
923
+ *
924
+ * Retryable errors (per Google AIP-194):
925
+ * - 408 (REQUEST_TIMEOUT): Request timeout
926
+ * - 429 (RESOURCE_EXHAUSTED): Rate limit exceeded, quota exhausted
927
+ * - 500 (INTERNAL): Internal server error
928
+ * - 502 (BAD_GATEWAY): Bad gateway
929
+ * - 503 (UNAVAILABLE): Service temporarily unavailable
930
+ * - 504 (DEADLINE_EXCEEDED): Gateway timeout
931
+ *
932
+ * Non-retryable errors:
933
+ * - 400 (INVALID_ARGUMENT): Invalid request parameters
934
+ * - 401 (UNAUTHENTICATED): Authentication required
935
+ * - 403 (PERMISSION_DENIED): Insufficient permissions
936
+ * - 404 (NOT_FOUND): Resource not found
937
+ * - 409 (CONFLICT): Resource conflict
938
+ * - Other 4xx client errors
939
+ *
940
+ * @param httpStatusCode - The HTTP status code from the API error
941
+ * @returns True if retryable, false if not retryable, undefined if unknown
942
+ */
943
+ isGeminiErrorRetryable(httpStatusCode) {
944
+ // Retryable status codes
945
+ if (httpStatusCode === 408)
946
+ return true; // Request timeout
947
+ if (httpStatusCode === 429)
948
+ return true; // Rate limit/quota
949
+ if (httpStatusCode === 502)
950
+ return true; // Bad gateway
951
+ if (httpStatusCode === 503)
952
+ return true; // Service unavailable
953
+ if (httpStatusCode === 504)
954
+ return true; // Gateway timeout
955
+ if (httpStatusCode >= 500 && httpStatusCode < 600)
956
+ return true; // Other 5xx server errors
957
+ // Non-retryable 4xx client errors
958
+ if (httpStatusCode >= 400 && httpStatusCode < 500)
959
+ return false;
960
+ // Unknown status codes - let consumer decide retry strategy
961
+ return undefined;
962
+ }
963
+ /**
964
+ * Extract error type name from error message.
965
+ * Google errors often include the error type in the message.
966
+ * Examples: "INVALID_ARGUMENT", "RESOURCE_EXHAUSTED", "PERMISSION_DENIED"
967
+ */
968
+ extractErrorName(message) {
969
+ // Common Google error patterns
970
+ const patterns = [
971
+ /^([A-Z_]+):/, // "ERROR_NAME: message"
972
+ /\[([A-Z_]+)\]/, // "[ERROR_NAME] message"
973
+ /^(\w+Error):/, // "ErrorTypeError: message"
974
+ ];
975
+ for (const pattern of patterns) {
976
+ const match = message.match(pattern);
977
+ if (match) {
978
+ return match[1];
979
+ }
980
+ }
981
+ return undefined;
982
+ }
983
+ }
984
+ /**
985
+ * Converts functionCall and functionResponse parts to text parts in Gemini Content[].
986
+ * Preserves tool call information while removing structured parts that require
987
+ * tools/toolConfig to be defined in the API request.
988
+ */
989
+ export function convertGeminiFunctionPartsToText(contents) {
990
+ return contents.map(content => {
991
+ if (!content.parts)
992
+ return content;
993
+ const hasFunctionParts = content.parts.some(p => p.functionCall || p.functionResponse);
994
+ if (!hasFunctionParts)
995
+ return content;
996
+ const newParts = content.parts.map(part => {
997
+ if (part.functionCall) {
998
+ const argsStr = part.functionCall.args ? JSON.stringify(part.functionCall.args) : '';
999
+ const truncated = argsStr.length > 500 ? argsStr.substring(0, 500) + '...' : argsStr;
1000
+ return { text: `[Tool call: ${part.functionCall.name}(${truncated})]` };
1001
+ }
1002
+ if (part.functionResponse) {
1003
+ const respStr = part.functionResponse.response
1004
+ ? JSON.stringify(part.functionResponse.response) : 'No response';
1005
+ const truncated = respStr.length > 500 ? respStr.substring(0, 500) + '...' : respStr;
1006
+ return { text: `[Tool result for ${part.functionResponse.name}: ${truncated}]` };
1007
+ }
1008
+ return part;
1009
+ });
1010
+ return { ...content, parts: newParts };
1011
+ });
1012
+ }
1013
+ function getToolDefinitions(tools) {
1014
+ if (!tools || tools.length === 0) {
1015
+ return undefined;
1016
+ }
1017
+ // VertexAI Gemini only supports one tool at a time.
1018
+ // For multiple tools, we have multiple functions in one tool.
1019
+ return {
1020
+ functionDeclarations: tools.map(getToolFunction),
1021
+ };
1022
+ }
1023
+ function getToolFunction(tool) {
1024
+ // If input_schema is a string, parse it; if it's already an object, use it directly
1025
+ let toolSchema;
1026
+ // Using a try-catch for safety, as the input_schema might not be a valid JSONSchema
1027
+ try {
1028
+ toolSchema = parseJSONtoSchema(tool.input_schema, false);
1029
+ }
1030
+ catch (e) {
1031
+ toolSchema = { ...tool.input_schema, type: Type.OBJECT };
1032
+ }
1033
+ return {
1034
+ name: tool.name,
1035
+ description: tool.description,
1036
+ parameters: toolSchema,
1037
+ };
1038
+ }
1039
+ /**
1040
+ * Update the conversation messages
1041
+ * @param prompt
1042
+ * @param response
1043
+ * @returns
1044
+ */
1045
+ function updateConversation(conversation, prompt) {
1046
+ // Unwrap array if wrapped, otherwise treat as array
1047
+ const unwrapped = unwrapConversationArray(conversation);
1048
+ const convArray = unwrapped ?? (conversation || []);
1049
+ return convArray.concat(prompt);
1050
+ }
1051
+ const SYSTEM_KEY = '_llumiverse_system';
1052
+ /**
1053
+ * Extract the stored system instruction from a Gemini conversation object.
1054
+ * Returns undefined if no system was stored.
1055
+ */
1056
+ function extractSystemFromConversation(conversation) {
1057
+ if (typeof conversation === 'object' && conversation !== null) {
1058
+ const c = conversation;
1059
+ if (c[SYSTEM_KEY] && typeof c[SYSTEM_KEY] === 'object') {
1060
+ return c[SYSTEM_KEY];
1061
+ }
1062
+ }
1063
+ return undefined;
1064
+ }
1065
+ /**
1066
+ * Store the system instruction in the Gemini conversation wrapper object.
1067
+ * The conversation is already wrapped by incrementConversationTurn into
1068
+ * { _arrayConversation: Content[], _llumiverse_meta: {...} }.
1069
+ * We add _llumiverse_system alongside these fields.
1070
+ */
1071
+ function storeSystemInConversation(conversation, system) {
1072
+ if (!system)
1073
+ return conversation;
1074
+ if (typeof conversation === 'object' && conversation !== null) {
1075
+ return { ...conversation, [SYSTEM_KEY]: system };
1076
+ }
1077
+ return conversation;
1078
+ }
1079
+ /**
1080
+ *
1081
+ * Gemini supports JSON output in the response. so we test if the response is a valid JSON object. otherwise we treat the response as a string.
1082
+ *
1083
+ * This is an excerpt from googleapis.github.io/python-genai:
1084
+ *
1085
+ * The function response in JSON object format.
1086
+ * Use “output” key to specify function output and “error” key to specify error details (if any).
1087
+ * If “output” and “error” keys are not specified, then whole “response” is treated as function output.
1088
+ * @see https://googleapis.github.io/python-genai/genai.html#genai.types.FunctionResponse
1089
+ */
1090
+ function formatFunctionResponse(response) {
1091
+ response = response.trim();
1092
+ if (response.startsWith("{") && response.endsWith("}")) {
1093
+ try {
1094
+ return JSON.parse(response);
1095
+ }
1096
+ catch (e) {
1097
+ return { output: response };
1098
+ }
1099
+ }
1100
+ else {
1101
+ return { output: response };
1102
+ }
1103
+ }
1104
+ //# sourceMappingURL=gemini.js.map