@paean-ai/adk 0.2.17 → 0.2.19

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 (253) hide show
  1. package/dist/cjs/index.js +3 -3
  2. package/dist/cjs/index.js.map +3 -3
  3. package/dist/esm/index.js +3 -3
  4. package/dist/esm/index.js.map +3 -3
  5. package/dist/web/index.js +1 -1
  6. package/dist/web/index.js.map +3 -3
  7. package/package.json +2 -2
  8. package/dist/cjs/agents/active_streaming_tool.js +0 -44
  9. package/dist/cjs/agents/base_agent.js +0 -245
  10. package/dist/cjs/agents/base_llm_processor.js +0 -44
  11. package/dist/cjs/agents/callback_context.js +0 -98
  12. package/dist/cjs/agents/content_processor_utils.js +0 -346
  13. package/dist/cjs/agents/functions.js +0 -424
  14. package/dist/cjs/agents/instructions.js +0 -110
  15. package/dist/cjs/agents/invocation_context.js +0 -107
  16. package/dist/cjs/agents/live_request_queue.js +0 -136
  17. package/dist/cjs/agents/llm_agent.js +0 -1247
  18. package/dist/cjs/agents/loop_agent.js +0 -68
  19. package/dist/cjs/agents/parallel_agent.js +0 -78
  20. package/dist/cjs/agents/readonly_context.js +0 -68
  21. package/dist/cjs/agents/run_config.js +0 -70
  22. package/dist/cjs/agents/sequential_agent.js +0 -84
  23. package/dist/cjs/agents/transcription_entry.js +0 -27
  24. package/dist/cjs/artifacts/base_artifact_service.js +0 -27
  25. package/dist/cjs/artifacts/gcs_artifact_service.js +0 -140
  26. package/dist/cjs/artifacts/in_memory_artifact_service.js +0 -119
  27. package/dist/cjs/auth/auth_credential.js +0 -46
  28. package/dist/cjs/auth/auth_handler.js +0 -92
  29. package/dist/cjs/auth/auth_schemes.js +0 -62
  30. package/dist/cjs/auth/auth_tool.js +0 -27
  31. package/dist/cjs/auth/credential_service/base_credential_service.js +0 -27
  32. package/dist/cjs/auth/credential_service/in_memory_credential_service.js +0 -63
  33. package/dist/cjs/auth/exchanger/base_credential_exchanger.js +0 -40
  34. package/dist/cjs/auth/exchanger/credential_exchanger_registry.js +0 -59
  35. package/dist/cjs/code_executors/base_code_executor.js +0 -76
  36. package/dist/cjs/code_executors/built_in_code_executor.js +0 -58
  37. package/dist/cjs/code_executors/code_execution_utils.js +0 -142
  38. package/dist/cjs/code_executors/code_executor_context.js +0 -198
  39. package/dist/cjs/common.js +0 -181
  40. package/dist/cjs/events/event.js +0 -119
  41. package/dist/cjs/events/event_actions.js +0 -83
  42. package/dist/cjs/examples/base_example_provider.js +0 -40
  43. package/dist/cjs/examples/example.js +0 -27
  44. package/dist/cjs/examples/example_util.js +0 -107
  45. package/dist/cjs/index_web.js +0 -33
  46. package/dist/cjs/memory/base_memory_service.js +0 -27
  47. package/dist/cjs/memory/in_memory_memory_service.js +0 -97
  48. package/dist/cjs/memory/memory_entry.js +0 -27
  49. package/dist/cjs/models/base_llm.js +0 -95
  50. package/dist/cjs/models/base_llm_connection.js +0 -27
  51. package/dist/cjs/models/gemini_llm_connection.js +0 -132
  52. package/dist/cjs/models/google_llm.js +0 -559
  53. package/dist/cjs/models/llm_request.js +0 -82
  54. package/dist/cjs/models/llm_response.js +0 -71
  55. package/dist/cjs/models/registry.js +0 -121
  56. package/dist/cjs/plugins/base_plugin.js +0 -236
  57. package/dist/cjs/plugins/logging_plugin.js +0 -222
  58. package/dist/cjs/plugins/plugin_manager.js +0 -239
  59. package/dist/cjs/plugins/security_plugin.js +0 -153
  60. package/dist/cjs/runner/in_memory_runner.js +0 -58
  61. package/dist/cjs/runner/runner.js +0 -277
  62. package/dist/cjs/sessions/base_session_service.js +0 -71
  63. package/dist/cjs/sessions/in_memory_session_service.js +0 -184
  64. package/dist/cjs/sessions/session.js +0 -48
  65. package/dist/cjs/sessions/state.js +0 -101
  66. package/dist/cjs/telemetry/google_cloud.js +0 -85
  67. package/dist/cjs/telemetry/setup.js +0 -97
  68. package/dist/cjs/telemetry/tracing.js +0 -231
  69. package/dist/cjs/tools/agent_tool.js +0 -134
  70. package/dist/cjs/tools/base_tool.js +0 -107
  71. package/dist/cjs/tools/base_toolset.js +0 -76
  72. package/dist/cjs/tools/forwarding_artifact_service.js +0 -71
  73. package/dist/cjs/tools/function_tool.js +0 -101
  74. package/dist/cjs/tools/google_search_tool.js +0 -77
  75. package/dist/cjs/tools/long_running_tool.js +0 -63
  76. package/dist/cjs/tools/mcp/mcp_session_manager.js +0 -65
  77. package/dist/cjs/tools/mcp/mcp_tool.js +0 -65
  78. package/dist/cjs/tools/mcp/mcp_toolset.js +0 -61
  79. package/dist/cjs/tools/tool_confirmation.js +0 -49
  80. package/dist/cjs/tools/tool_context.js +0 -129
  81. package/dist/cjs/utils/client_labels.js +0 -56
  82. package/dist/cjs/utils/deep_clone.js +0 -44
  83. package/dist/cjs/utils/env_aware_utils.js +0 -83
  84. package/dist/cjs/utils/gemini_schema_util.js +0 -88
  85. package/dist/cjs/utils/logger.js +0 -121
  86. package/dist/cjs/utils/model_name.js +0 -76
  87. package/dist/cjs/utils/simple_zod_to_json.js +0 -191
  88. package/dist/cjs/utils/variant_utils.js +0 -55
  89. package/dist/cjs/version.js +0 -39
  90. package/dist/esm/agents/active_streaming_tool.js +0 -14
  91. package/dist/esm/agents/base_agent.js +0 -214
  92. package/dist/esm/agents/base_llm_processor.js +0 -13
  93. package/dist/esm/agents/callback_context.js +0 -68
  94. package/dist/esm/agents/content_processor_utils.js +0 -315
  95. package/dist/esm/agents/functions.js +0 -383
  96. package/dist/esm/agents/instructions.js +0 -80
  97. package/dist/esm/agents/invocation_context.js +0 -76
  98. package/dist/esm/agents/live_request_queue.js +0 -106
  99. package/dist/esm/agents/llm_agent.js +0 -1215
  100. package/dist/esm/agents/loop_agent.js +0 -38
  101. package/dist/esm/agents/parallel_agent.js +0 -48
  102. package/dist/esm/agents/readonly_context.js +0 -38
  103. package/dist/esm/agents/run_config.js +0 -39
  104. package/dist/esm/agents/sequential_agent.js +0 -54
  105. package/dist/esm/agents/transcription_entry.js +0 -5
  106. package/dist/esm/artifacts/base_artifact_service.js +0 -5
  107. package/dist/esm/artifacts/gcs_artifact_service.js +0 -110
  108. package/dist/esm/artifacts/in_memory_artifact_service.js +0 -89
  109. package/dist/esm/auth/auth_credential.js +0 -16
  110. package/dist/esm/auth/auth_handler.js +0 -62
  111. package/dist/esm/auth/auth_schemes.js +0 -31
  112. package/dist/esm/auth/auth_tool.js +0 -5
  113. package/dist/esm/auth/credential_service/base_credential_service.js +0 -5
  114. package/dist/esm/auth/credential_service/in_memory_credential_service.js +0 -33
  115. package/dist/esm/auth/exchanger/base_credential_exchanger.js +0 -10
  116. package/dist/esm/auth/exchanger/credential_exchanger_registry.js +0 -29
  117. package/dist/esm/code_executors/base_code_executor.js +0 -46
  118. package/dist/esm/code_executors/built_in_code_executor.js +0 -28
  119. package/dist/esm/code_executors/code_execution_utils.js +0 -108
  120. package/dist/esm/code_executors/code_executor_context.js +0 -168
  121. package/dist/esm/common.js +0 -98
  122. package/dist/esm/events/event.js +0 -83
  123. package/dist/esm/events/event_actions.js +0 -52
  124. package/dist/esm/examples/base_example_provider.js +0 -10
  125. package/dist/esm/examples/example.js +0 -5
  126. package/dist/esm/examples/example_util.js +0 -76
  127. package/dist/esm/index_web.js +0 -6
  128. package/dist/esm/memory/base_memory_service.js +0 -5
  129. package/dist/esm/memory/in_memory_memory_service.js +0 -67
  130. package/dist/esm/memory/memory_entry.js +0 -5
  131. package/dist/esm/models/base_llm.js +0 -64
  132. package/dist/esm/models/base_llm_connection.js +0 -5
  133. package/dist/esm/models/gemini_llm_connection.js +0 -102
  134. package/dist/esm/models/google_llm.js +0 -533
  135. package/dist/esm/models/llm_request.js +0 -50
  136. package/dist/esm/models/llm_response.js +0 -41
  137. package/dist/esm/models/registry.js +0 -91
  138. package/dist/esm/plugins/base_plugin.js +0 -206
  139. package/dist/esm/plugins/logging_plugin.js +0 -192
  140. package/dist/esm/plugins/plugin_manager.js +0 -209
  141. package/dist/esm/plugins/security_plugin.js +0 -119
  142. package/dist/esm/runner/in_memory_runner.js +0 -28
  143. package/dist/esm/runner/runner.js +0 -247
  144. package/dist/esm/sessions/base_session_service.js +0 -41
  145. package/dist/esm/sessions/in_memory_session_service.js +0 -154
  146. package/dist/esm/sessions/session.js +0 -18
  147. package/dist/esm/sessions/state.js +0 -71
  148. package/dist/esm/telemetry/google_cloud.js +0 -54
  149. package/dist/esm/telemetry/setup.js +0 -67
  150. package/dist/esm/telemetry/tracing.js +0 -195
  151. package/dist/esm/tools/agent_tool.js +0 -104
  152. package/dist/esm/tools/base_tool.js +0 -77
  153. package/dist/esm/tools/base_toolset.js +0 -46
  154. package/dist/esm/tools/forwarding_artifact_service.js +0 -41
  155. package/dist/esm/tools/function_tool.js +0 -71
  156. package/dist/esm/tools/google_search_tool.js +0 -47
  157. package/dist/esm/tools/long_running_tool.js +0 -33
  158. package/dist/esm/tools/mcp/mcp_session_manager.js +0 -35
  159. package/dist/esm/tools/mcp/mcp_tool.js +0 -35
  160. package/dist/esm/tools/mcp/mcp_toolset.js +0 -31
  161. package/dist/esm/tools/tool_confirmation.js +0 -19
  162. package/dist/esm/tools/tool_context.js +0 -99
  163. package/dist/esm/utils/client_labels.js +0 -26
  164. package/dist/esm/utils/deep_clone.js +0 -14
  165. package/dist/esm/utils/env_aware_utils.js +0 -49
  166. package/dist/esm/utils/gemini_schema_util.js +0 -58
  167. package/dist/esm/utils/logger.js +0 -89
  168. package/dist/esm/utils/model_name.js +0 -41
  169. package/dist/esm/utils/simple_zod_to_json.js +0 -160
  170. package/dist/esm/utils/variant_utils.js +0 -24
  171. package/dist/esm/version.js +0 -9
  172. package/dist/web/agents/active_streaming_tool.js +0 -14
  173. package/dist/web/agents/base_agent.js +0 -265
  174. package/dist/web/agents/base_llm_processor.js +0 -13
  175. package/dist/web/agents/callback_context.js +0 -68
  176. package/dist/web/agents/content_processor_utils.js +0 -315
  177. package/dist/web/agents/functions.js +0 -383
  178. package/dist/web/agents/instructions.js +0 -80
  179. package/dist/web/agents/invocation_context.js +0 -76
  180. package/dist/web/agents/live_request_queue.js +0 -124
  181. package/dist/web/agents/llm_agent.js +0 -1345
  182. package/dist/web/agents/loop_agent.js +0 -71
  183. package/dist/web/agents/parallel_agent.js +0 -83
  184. package/dist/web/agents/readonly_context.js +0 -38
  185. package/dist/web/agents/run_config.js +0 -54
  186. package/dist/web/agents/sequential_agent.js +0 -99
  187. package/dist/web/agents/transcription_entry.js +0 -5
  188. package/dist/web/artifacts/base_artifact_service.js +0 -5
  189. package/dist/web/artifacts/gcs_artifact_service.js +0 -126
  190. package/dist/web/artifacts/in_memory_artifact_service.js +0 -89
  191. package/dist/web/auth/auth_credential.js +0 -16
  192. package/dist/web/auth/auth_handler.js +0 -62
  193. package/dist/web/auth/auth_schemes.js +0 -31
  194. package/dist/web/auth/auth_tool.js +0 -5
  195. package/dist/web/auth/credential_service/base_credential_service.js +0 -5
  196. package/dist/web/auth/credential_service/in_memory_credential_service.js +0 -33
  197. package/dist/web/auth/exchanger/base_credential_exchanger.js +0 -10
  198. package/dist/web/auth/exchanger/credential_exchanger_registry.js +0 -29
  199. package/dist/web/code_executors/base_code_executor.js +0 -46
  200. package/dist/web/code_executors/built_in_code_executor.js +0 -28
  201. package/dist/web/code_executors/code_execution_utils.js +0 -105
  202. package/dist/web/code_executors/code_executor_context.js +0 -168
  203. package/dist/web/common.js +0 -98
  204. package/dist/web/events/event.js +0 -101
  205. package/dist/web/events/event_actions.js +0 -67
  206. package/dist/web/examples/base_example_provider.js +0 -10
  207. package/dist/web/examples/example.js +0 -5
  208. package/dist/web/examples/example_util.js +0 -75
  209. package/dist/web/index_web.js +0 -6
  210. package/dist/web/memory/base_memory_service.js +0 -5
  211. package/dist/web/memory/in_memory_memory_service.js +0 -67
  212. package/dist/web/memory/memory_entry.js +0 -5
  213. package/dist/web/models/base_llm.js +0 -64
  214. package/dist/web/models/base_llm_connection.js +0 -5
  215. package/dist/web/models/gemini_llm_connection.js +0 -120
  216. package/dist/web/models/google_llm.js +0 -574
  217. package/dist/web/models/llm_request.js +0 -50
  218. package/dist/web/models/llm_response.js +0 -41
  219. package/dist/web/models/registry.js +0 -91
  220. package/dist/web/plugins/base_plugin.js +0 -206
  221. package/dist/web/plugins/logging_plugin.js +0 -192
  222. package/dist/web/plugins/plugin_manager.js +0 -209
  223. package/dist/web/plugins/security_plugin.js +0 -119
  224. package/dist/web/runner/in_memory_runner.js +0 -28
  225. package/dist/web/runner/runner.js +0 -278
  226. package/dist/web/sessions/base_session_service.js +0 -41
  227. package/dist/web/sessions/in_memory_session_service.js +0 -154
  228. package/dist/web/sessions/session.js +0 -18
  229. package/dist/web/sessions/state.js +0 -87
  230. package/dist/web/telemetry/google_cloud.js +0 -54
  231. package/dist/web/telemetry/setup.js +0 -67
  232. package/dist/web/telemetry/tracing.js +0 -210
  233. package/dist/web/tools/agent_tool.js +0 -118
  234. package/dist/web/tools/base_tool.js +0 -77
  235. package/dist/web/tools/base_toolset.js +0 -46
  236. package/dist/web/tools/forwarding_artifact_service.js +0 -41
  237. package/dist/web/tools/function_tool.js +0 -71
  238. package/dist/web/tools/google_search_tool.js +0 -47
  239. package/dist/web/tools/long_running_tool.js +0 -50
  240. package/dist/web/tools/mcp/mcp_session_manager.js +0 -35
  241. package/dist/web/tools/mcp/mcp_tool.js +0 -35
  242. package/dist/web/tools/mcp/mcp_toolset.js +0 -31
  243. package/dist/web/tools/tool_confirmation.js +0 -19
  244. package/dist/web/tools/tool_context.js +0 -99
  245. package/dist/web/utils/client_labels.js +0 -26
  246. package/dist/web/utils/deep_clone.js +0 -14
  247. package/dist/web/utils/env_aware_utils.js +0 -49
  248. package/dist/web/utils/gemini_schema_util.js +0 -58
  249. package/dist/web/utils/logger.js +0 -89
  250. package/dist/web/utils/model_name.js +0 -41
  251. package/dist/web/utils/simple_zod_to_json.js +0 -174
  252. package/dist/web/utils/variant_utils.js +0 -24
  253. package/dist/web/version.js +0 -9
@@ -1,533 +0,0 @@
1
- /**
2
- * @license
3
- * Copyright 2025 Google LLC
4
- * SPDX-License-Identifier: Apache-2.0
5
- */
6
- import {
7
- createPartFromText,
8
- FinishReason,
9
- GoogleGenAI
10
- } from "@google/genai";
11
- import { logger } from "../utils/logger.js";
12
- import { isGemini3PreviewModel } from "../utils/model_name.js";
13
- import { GoogleLLMVariant } from "../utils/variant_utils.js";
14
- import { BaseLlm } from "./base_llm.js";
15
- import { GeminiLlmConnection } from "./gemini_llm_connection.js";
16
- import { createLlmResponse } from "./llm_response.js";
17
- const AGENT_ENGINE_TELEMETRY_TAG = "remote_reasoning_engine";
18
- const AGENT_ENGINE_TELEMETRY_ENV_VARIABLE_NAME = "GOOGLE_CLOUD_AGENT_ENGINE_ID";
19
- const GEMINI3_PREVIEW_API_ENDPOINT = "https://aiplatform.googleapis.com/v1/publishers/google";
20
- class Gemini extends BaseLlm {
21
- /**
22
- * @param params The parameters for creating a Gemini instance.
23
- */
24
- constructor({
25
- model,
26
- apiKey,
27
- vertexai,
28
- project,
29
- location,
30
- headers,
31
- apiEndpoint
32
- }) {
33
- if (!model) {
34
- model = "gemini-2.5-flash";
35
- }
36
- super({ model });
37
- this.project = project;
38
- this.location = location;
39
- this.apiKey = apiKey;
40
- this.headers = headers;
41
- this.isGemini3Preview = isGemini3PreviewModel(model);
42
- const canReadEnv = typeof process === "object";
43
- this.apiEndpoint = apiEndpoint;
44
- if (!this.apiEndpoint && canReadEnv) {
45
- this.apiEndpoint = process.env["GEMINI_API_ENDPOINT"];
46
- }
47
- if (!this.apiEndpoint && this.isGemini3Preview) {
48
- this.apiEndpoint = GEMINI3_PREVIEW_API_ENDPOINT;
49
- logger.info(`Using Gemini 3 preview endpoint: ${this.apiEndpoint}`);
50
- }
51
- let useVertexAI = !!vertexai;
52
- if (!useVertexAI && canReadEnv) {
53
- const vertexAIfromEnv = process.env["GOOGLE_GENAI_USE_VERTEXAI"];
54
- if (vertexAIfromEnv) {
55
- useVertexAI = vertexAIfromEnv.toLowerCase() === "true" || vertexAIfromEnv === "1";
56
- }
57
- }
58
- if (this.isGemini3Preview && useVertexAI) {
59
- const availableApiKey = apiKey || (canReadEnv ? process.env["GOOGLE_GENAI_API_KEY"] || process.env["GEMINI_API_KEY"] : void 0);
60
- if (availableApiKey) {
61
- logger.info(
62
- "Gemini 3 preview detected with Vertex AI mode. Switching to API key mode for correct endpoint handling."
63
- );
64
- useVertexAI = false;
65
- this.apiKey = availableApiKey;
66
- } else {
67
- logger.warn(
68
- "Gemini 3 preview requires API key authentication for correct endpoint handling. Set GEMINI_API_KEY or GOOGLE_GENAI_API_KEY environment variable for best compatibility."
69
- );
70
- }
71
- }
72
- this.vertexai = useVertexAI;
73
- if (this.vertexai) {
74
- if (canReadEnv && !this.project) {
75
- this.project = process.env["GOOGLE_CLOUD_PROJECT"];
76
- }
77
- if (canReadEnv && !this.location) {
78
- this.location = process.env["GOOGLE_CLOUD_LOCATION"];
79
- }
80
- if (!this.project) {
81
- throw new Error(
82
- "VertexAI project must be provided via constructor or GOOGLE_CLOUD_PROJECT environment variable."
83
- );
84
- }
85
- if (!this.location) {
86
- throw new Error(
87
- "VertexAI location must be provided via constructor or GOOGLE_CLOUD_LOCATION environment variable."
88
- );
89
- }
90
- } else {
91
- if (!this.apiKey && canReadEnv) {
92
- this.apiKey = process.env["GOOGLE_GENAI_API_KEY"] || process.env["GEMINI_API_KEY"];
93
- }
94
- if (!this.apiKey) {
95
- throw new Error(
96
- "API key must be provided via constructor or GOOGLE_GENAI_API_KEY or GEMINI_API_KEY environment variable."
97
- );
98
- }
99
- }
100
- }
101
- /**
102
- * Sends a request to the Gemini model.
103
- *
104
- * @param llmRequest LlmRequest, the request to send to the Gemini model.
105
- * @param stream bool = false, whether to do streaming call.
106
- * @yields LlmResponse: The model response.
107
- */
108
- async *generateContentAsync(llmRequest, stream = false) {
109
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p;
110
- this.preprocessRequest(llmRequest);
111
- this.maybeAppendUserContent(llmRequest);
112
- logger.info(
113
- `Sending out request, model: ${llmRequest.model}, backend: ${this.apiBackend}, stream: ${stream}`
114
- );
115
- if ((_a = llmRequest.config) == null ? void 0 : _a.httpOptions) {
116
- llmRequest.config.httpOptions.headers = {
117
- ...llmRequest.config.httpOptions.headers,
118
- ...this.trackingHeaders
119
- };
120
- }
121
- if (stream) {
122
- const streamResult = await this.apiClient.models.generateContentStream({
123
- model: (_b = llmRequest.model) != null ? _b : this.model,
124
- contents: llmRequest.contents,
125
- config: llmRequest.config
126
- });
127
- let thoughtText = "";
128
- let thoughtSignature;
129
- let text = "";
130
- let usageMetadata;
131
- let lastResponse;
132
- if (this.isGemini3Preview && this.cachedThoughtSignature) {
133
- logger.info(
134
- `[Gemini3] Starting new request with CACHED thoughtSignature (length: ${this.cachedThoughtSignature.length})`
135
- );
136
- }
137
- for await (const response of streamResult) {
138
- lastResponse = response;
139
- const llmResponse = createLlmResponse(response);
140
- usageMetadata = llmResponse.usageMetadata;
141
- const firstPart = (_d = (_c = llmResponse.content) == null ? void 0 : _c.parts) == null ? void 0 : _d[0];
142
- const hasFunctionCalls = (_f = (_e = llmResponse.content) == null ? void 0 : _e.parts) == null ? void 0 : _f.some(
143
- (part) => part.functionCall
144
- );
145
- if (this.isGemini3Preview && ((_g = llmResponse.content) == null ? void 0 : _g.parts)) {
146
- for (const part of llmResponse.content.parts) {
147
- if (part.thoughtSignature && !thoughtSignature) {
148
- thoughtSignature = part.thoughtSignature;
149
- this.cachedThoughtSignature = thoughtSignature;
150
- logger.info(
151
- `[Gemini3] Captured and CACHED thoughtSignature from response part (length: ${thoughtSignature.length})`
152
- );
153
- break;
154
- }
155
- }
156
- }
157
- if (firstPart == null ? void 0 : firstPart.text) {
158
- if ("thought" in firstPart && firstPart.thought) {
159
- thoughtText += firstPart.text;
160
- if ("thoughtSignature" in firstPart && firstPart.thoughtSignature) {
161
- thoughtSignature = firstPart.thoughtSignature;
162
- this.cachedThoughtSignature = thoughtSignature;
163
- logger.info(
164
- `[Gemini3] Captured and CACHED thoughtSignature from thought part (length: ${thoughtSignature.length})`
165
- );
166
- }
167
- } else {
168
- text += firstPart.text;
169
- }
170
- llmResponse.partial = true;
171
- if (this.isGemini3Preview && hasFunctionCalls) {
172
- const responseHasSignature = (_i = (_h = llmResponse.content) == null ? void 0 : _h.parts) == null ? void 0 : _i.some(
173
- (p) => p.thoughtSignature
174
- );
175
- logger.info(
176
- `[Gemini3] Chunk has thought AND function calls. Response has signature: ${responseHasSignature}`
177
- );
178
- thoughtText = "";
179
- thoughtSignature = void 0;
180
- text = "";
181
- }
182
- } else if ((thoughtText || text) && (!firstPart || !firstPart.inlineData)) {
183
- if (this.isGemini3Preview && hasFunctionCalls && llmResponse.content) {
184
- logger.info(
185
- `[Gemini3] Merging thought with function calls. Has accumulated signature: ${!!thoughtSignature}`
186
- );
187
- const prependParts = [];
188
- if (thoughtText) {
189
- const thoughtPart = { text: thoughtText, thought: true };
190
- if (thoughtSignature) {
191
- thoughtPart.thoughtSignature = thoughtSignature;
192
- }
193
- prependParts.push(thoughtPart);
194
- logger.info(
195
- `[Gemini3] Created thought part with signature: ${!!thoughtSignature}`
196
- );
197
- }
198
- if (text) {
199
- prependParts.push(createPartFromText(text));
200
- }
201
- if (!thoughtText && thoughtSignature) {
202
- let signatureApplied = false;
203
- for (const part of llmResponse.content.parts || []) {
204
- if (part.functionCall && !signatureApplied) {
205
- if (!part.thoughtSignature) {
206
- part.thoughtSignature = thoughtSignature;
207
- logger.info(
208
- `[Gemini3] Applied accumulated signature to first function call: ${part.functionCall.name}`
209
- );
210
- }
211
- signatureApplied = true;
212
- }
213
- }
214
- }
215
- llmResponse.content.parts = [
216
- ...prependParts,
217
- ...llmResponse.content.parts || []
218
- ];
219
- logger.info(
220
- `[Gemini3] Final merged content has ${llmResponse.content.parts.length} parts`
221
- );
222
- for (let i = 0; i < llmResponse.content.parts.length; i++) {
223
- const p = llmResponse.content.parts[i];
224
- logger.info(
225
- `[Gemini3] Part ${i}: thought=${!!p.thought}, functionCall=${((_j = p.functionCall) == null ? void 0 : _j.name) || "none"}, hasSignature=${!!p.thoughtSignature}`
226
- );
227
- }
228
- thoughtText = "";
229
- thoughtSignature = void 0;
230
- text = "";
231
- } else {
232
- const parts = [];
233
- if (thoughtText) {
234
- const thoughtPart = { text: thoughtText, thought: true };
235
- if (thoughtSignature) {
236
- thoughtPart.thoughtSignature = thoughtSignature;
237
- }
238
- parts.push(thoughtPart);
239
- }
240
- if (text) {
241
- parts.push(createPartFromText(text));
242
- }
243
- yield {
244
- content: {
245
- role: "model",
246
- parts
247
- },
248
- usageMetadata: llmResponse.usageMetadata
249
- };
250
- thoughtText = "";
251
- thoughtSignature = void 0;
252
- text = "";
253
- }
254
- }
255
- if (this.isGemini3Preview && hasFunctionCalls && ((_k = llmResponse.content) == null ? void 0 : _k.parts)) {
256
- const hasExistingSignature = llmResponse.content.parts.some(
257
- (p) => p.thoughtSignature
258
- );
259
- if (!hasExistingSignature && thoughtSignature) {
260
- for (const part of llmResponse.content.parts) {
261
- if (part.functionCall) {
262
- part.thoughtSignature = thoughtSignature;
263
- logger.info(
264
- `[Gemini3] Applied cached signature to first function call: ${part.functionCall.name}`
265
- );
266
- break;
267
- }
268
- }
269
- }
270
- const functionCallNames = llmResponse.content.parts.filter((p) => p.functionCall).map((p) => p.functionCall.name);
271
- let partsWithSig = llmResponse.content.parts.filter(
272
- (p) => p.thoughtSignature
273
- ).length;
274
- if (partsWithSig === 0) {
275
- const signatureToUse = thoughtSignature || this.cachedThoughtSignature;
276
- if (signatureToUse) {
277
- for (const part of llmResponse.content.parts) {
278
- if (part.functionCall && !part.thoughtSignature) {
279
- part.thoughtSignature = signatureToUse;
280
- logger.info(
281
- `[Gemini3] Applied CACHED signature to function call: ${part.functionCall.name} (API didn't return new signature, using ${thoughtSignature ? "local" : "class-level"} cache)`
282
- );
283
- partsWithSig = 1;
284
- break;
285
- }
286
- }
287
- }
288
- }
289
- logger.info(
290
- `[Gemini3] Yielding function call response: calls=[${functionCallNames.join(", ")}], partsWithSignature=${partsWithSig}`
291
- );
292
- if (partsWithSig === 0) {
293
- logger.warn(
294
- `[Gemini3] WARNING: No thoughtSignature found and no cached signature available! This may cause 400 errors on next request.`
295
- );
296
- }
297
- }
298
- yield llmResponse;
299
- }
300
- if ((text || thoughtText) && ((_m = (_l = lastResponse == null ? void 0 : lastResponse.candidates) == null ? void 0 : _l[0]) == null ? void 0 : _m.finishReason) === FinishReason.STOP) {
301
- const parts = [];
302
- if (thoughtText) {
303
- const thoughtPart = { text: thoughtText, thought: true };
304
- if (thoughtSignature) {
305
- thoughtPart.thoughtSignature = thoughtSignature;
306
- }
307
- parts.push(thoughtPart);
308
- }
309
- if (text) {
310
- parts.push({ text });
311
- }
312
- yield {
313
- content: {
314
- role: "model",
315
- parts
316
- },
317
- usageMetadata
318
- };
319
- }
320
- } else {
321
- const response = await this.apiClient.models.generateContent({
322
- model: (_n = llmRequest.model) != null ? _n : this.model,
323
- contents: llmRequest.contents,
324
- config: llmRequest.config
325
- });
326
- const llmResponse = createLlmResponse(response);
327
- if (this.isGemini3Preview && ((_o = llmResponse.content) == null ? void 0 : _o.parts)) {
328
- let thoughtSig;
329
- let hasThoughtPartWithSignature = false;
330
- for (const part of llmResponse.content.parts) {
331
- if (part.thoughtSignature) {
332
- thoughtSig = part.thoughtSignature;
333
- this.cachedThoughtSignature = thoughtSig;
334
- if (part.thought) {
335
- hasThoughtPartWithSignature = true;
336
- }
337
- break;
338
- }
339
- }
340
- logger.info(
341
- `[Gemini3] Non-streaming response: hasSignature=${!!thoughtSig}, hasThoughtPart=${hasThoughtPartWithSignature}, hasCachedSig=${!!this.cachedThoughtSignature}`
342
- );
343
- if (thoughtSig && !hasThoughtPartWithSignature) {
344
- let signatureApplied = false;
345
- for (const part of llmResponse.content.parts) {
346
- if (part.functionCall) {
347
- if (!signatureApplied && !part.thoughtSignature) {
348
- part.thoughtSignature = thoughtSig;
349
- logger.info(
350
- `[Gemini3] Applied signature to first function call: ${part.functionCall.name}`
351
- );
352
- }
353
- signatureApplied = true;
354
- }
355
- }
356
- }
357
- const hasFunctionCalls = llmResponse.content.parts.some(
358
- (p) => p.functionCall
359
- );
360
- if (hasFunctionCalls) {
361
- let partsWithSig = llmResponse.content.parts.filter(
362
- (p) => p.thoughtSignature
363
- ).length;
364
- if (partsWithSig === 0 && this.cachedThoughtSignature) {
365
- for (const part of llmResponse.content.parts) {
366
- if (part.functionCall && !part.thoughtSignature) {
367
- part.thoughtSignature = this.cachedThoughtSignature;
368
- logger.info(
369
- `[Gemini3] Applied CACHED signature to function call: ${part.functionCall.name} (API didn't return new signature)`
370
- );
371
- partsWithSig = 1;
372
- break;
373
- }
374
- }
375
- }
376
- for (let i = 0; i < llmResponse.content.parts.length; i++) {
377
- const p = llmResponse.content.parts[i];
378
- logger.info(
379
- `[Gemini3] Response Part ${i}: thought=${!!p.thought}, functionCall=${((_p = p.functionCall) == null ? void 0 : _p.name) || "none"}, hasSignature=${!!p.thoughtSignature}`
380
- );
381
- }
382
- if (partsWithSig === 0) {
383
- logger.warn(
384
- `[Gemini3] WARNING: No thoughtSignature found and no cached signature available! This may cause 400 errors on next request.`
385
- );
386
- }
387
- }
388
- }
389
- yield llmResponse;
390
- }
391
- }
392
- get apiClient() {
393
- if (this._apiClient) {
394
- return this._apiClient;
395
- }
396
- const combinedHeaders = {
397
- ...this.trackingHeaders,
398
- ...this.headers
399
- };
400
- if (this.vertexai) {
401
- this._apiClient = new GoogleGenAI({
402
- vertexai: this.vertexai,
403
- project: this.project,
404
- location: this.location,
405
- httpOptions: { headers: combinedHeaders }
406
- });
407
- } else {
408
- const httpOptions = { headers: combinedHeaders };
409
- if (this.apiEndpoint) {
410
- httpOptions.baseUrl = this.apiEndpoint;
411
- logger.debug(`Using custom API endpoint: ${this.apiEndpoint}`);
412
- if (this.isGemini3Preview) {
413
- httpOptions.apiVersion = "";
414
- logger.info(
415
- `Gemini 3 preview mode: using direct API path without version prefix`
416
- );
417
- }
418
- }
419
- this._apiClient = new GoogleGenAI({
420
- apiKey: this.apiKey,
421
- vertexai: false,
422
- httpOptions
423
- });
424
- }
425
- return this._apiClient;
426
- }
427
- get apiBackend() {
428
- if (!this._apiBackend) {
429
- this._apiBackend = this.apiClient.vertexai ? GoogleLLMVariant.VERTEX_AI : GoogleLLMVariant.GEMINI_API;
430
- }
431
- return this._apiBackend;
432
- }
433
- get liveApiVersion() {
434
- if (!this._liveApiVersion) {
435
- this._liveApiVersion = this.apiBackend === GoogleLLMVariant.VERTEX_AI ? "v1beta1" : "v1alpha";
436
- }
437
- return this._liveApiVersion;
438
- }
439
- get liveApiClient() {
440
- if (!this._liveApiClient) {
441
- const httpOptions = {
442
- headers: this.trackingHeaders,
443
- apiVersion: this.liveApiVersion
444
- };
445
- if (this.apiEndpoint) {
446
- httpOptions.baseUrl = this.apiEndpoint;
447
- if (this.isGemini3Preview) {
448
- httpOptions.apiVersion = "";
449
- }
450
- }
451
- this._liveApiClient = new GoogleGenAI({
452
- apiKey: this.apiKey,
453
- httpOptions
454
- });
455
- }
456
- return this._liveApiClient;
457
- }
458
- /**
459
- * Connects to the Gemini model and returns an llm connection.
460
- *
461
- * @param llmRequest LlmRequest, the request to send to the Gemini model.
462
- * @returns BaseLlmConnection, the connection to the Gemini model.
463
- */
464
- async connect(llmRequest) {
465
- var _a, _b, _c, _d;
466
- if ((_a = llmRequest.liveConnectConfig) == null ? void 0 : _a.httpOptions) {
467
- if (!llmRequest.liveConnectConfig.httpOptions.headers) {
468
- llmRequest.liveConnectConfig.httpOptions.headers = {};
469
- }
470
- Object.assign(
471
- llmRequest.liveConnectConfig.httpOptions.headers,
472
- this.trackingHeaders
473
- );
474
- llmRequest.liveConnectConfig.httpOptions.apiVersion = this.isGemini3Preview ? "" : this.liveApiVersion;
475
- }
476
- if ((_b = llmRequest.config) == null ? void 0 : _b.systemInstruction) {
477
- llmRequest.liveConnectConfig.systemInstruction = {
478
- role: "system",
479
- // TODO - b/425992518: validate type casting works well.
480
- parts: [
481
- createPartFromText(llmRequest.config.systemInstruction)
482
- ]
483
- };
484
- }
485
- llmRequest.liveConnectConfig.tools = (_c = llmRequest.config) == null ? void 0 : _c.tools;
486
- const liveSession = await this.liveApiClient.live.connect({
487
- model: (_d = llmRequest.model) != null ? _d : this.model,
488
- config: llmRequest.liveConnectConfig,
489
- callbacks: {
490
- // TODO - b/425992518: GenAI SDK inconsistent API, missing methods.
491
- onmessage: () => {
492
- }
493
- }
494
- });
495
- return new GeminiLlmConnection(liveSession);
496
- }
497
- preprocessRequest(llmRequest) {
498
- if (this.apiBackend === GoogleLLMVariant.GEMINI_API) {
499
- if (llmRequest.config) {
500
- llmRequest.config.labels = void 0;
501
- }
502
- if (llmRequest.contents) {
503
- for (const content of llmRequest.contents) {
504
- if (!content.parts) continue;
505
- for (const part of content.parts) {
506
- removeDisplayNameIfPresent(part.inlineData);
507
- removeDisplayNameIfPresent(part.fileData);
508
- }
509
- }
510
- }
511
- }
512
- }
513
- }
514
- /**
515
- * A list of model name patterns that are supported by this LLM.
516
- *
517
- * @returns A list of supported models.
518
- */
519
- Gemini.supportedModels = [
520
- /gemini-.*/,
521
- // fine-tuned vertex endpoint pattern
522
- /projects\/.+\/locations\/.+\/endpoints\/.+/,
523
- // vertex gemini long name
524
- /projects\/.+\/locations\/.+\/publishers\/google\/models\/gemini.+/
525
- ];
526
- function removeDisplayNameIfPresent(dataObj) {
527
- if (dataObj && dataObj.displayName) {
528
- dataObj.displayName = void 0;
529
- }
530
- }
531
- export {
532
- Gemini
533
- };
@@ -1,50 +0,0 @@
1
- /**
2
- * @license
3
- * Copyright 2025 Google LLC
4
- * SPDX-License-Identifier: Apache-2.0
5
- */
6
- function appendInstructions(llmRequest, instructions) {
7
- if (!llmRequest.config) {
8
- llmRequest.config = {};
9
- }
10
- const newInstructions = instructions.join("\n\n");
11
- if (llmRequest.config.systemInstruction) {
12
- llmRequest.config.systemInstruction += "\n\n" + newInstructions;
13
- } else {
14
- llmRequest.config.systemInstruction = newInstructions;
15
- }
16
- }
17
- function appendTools(llmRequest, tools) {
18
- if (!(tools == null ? void 0 : tools.length)) {
19
- return;
20
- }
21
- const functionDeclarations = [];
22
- for (const tool of tools) {
23
- const declaration = tool._getDeclaration();
24
- if (declaration) {
25
- functionDeclarations.push(declaration);
26
- llmRequest.toolsDict[tool.name] = tool;
27
- }
28
- }
29
- if (functionDeclarations.length) {
30
- if (!llmRequest.config) {
31
- llmRequest.config = {};
32
- }
33
- if (!llmRequest.config.tools) {
34
- llmRequest.config.tools = [];
35
- }
36
- llmRequest.config.tools.push({ functionDeclarations });
37
- }
38
- }
39
- function setOutputSchema(llmRequest, schema) {
40
- if (!llmRequest.config) {
41
- llmRequest.config = {};
42
- }
43
- llmRequest.config.responseSchema = schema;
44
- llmRequest.config.responseMimeType = "application/json";
45
- }
46
- export {
47
- appendInstructions,
48
- appendTools,
49
- setOutputSchema
50
- };
@@ -1,41 +0,0 @@
1
- /**
2
- * @license
3
- * Copyright 2025 Google LLC
4
- * SPDX-License-Identifier: Apache-2.0
5
- */
6
- function createLlmResponse(response) {
7
- var _a;
8
- const usageMetadata = response.usageMetadata;
9
- if (response.candidates && response.candidates.length > 0) {
10
- const candidate = response.candidates[0];
11
- if (((_a = candidate.content) == null ? void 0 : _a.parts) && candidate.content.parts.length > 0) {
12
- return {
13
- content: candidate.content,
14
- groundingMetadata: candidate.groundingMetadata,
15
- usageMetadata,
16
- finishReason: candidate.finishReason
17
- };
18
- }
19
- return {
20
- errorCode: candidate.finishReason,
21
- errorMessage: candidate.finishMessage,
22
- usageMetadata,
23
- finishReason: candidate.finishReason
24
- };
25
- }
26
- if (response.promptFeedback) {
27
- return {
28
- errorCode: response.promptFeedback.blockReason,
29
- errorMessage: response.promptFeedback.blockReasonMessage,
30
- usageMetadata
31
- };
32
- }
33
- return {
34
- errorCode: "UNKNOWN_ERROR",
35
- errorMessage: "Unknown error.",
36
- usageMetadata
37
- };
38
- }
39
- export {
40
- createLlmResponse
41
- };