@google/adk 2.2.1 → 2.3.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 (226) hide show
  1. package/dist/cjs/a2a/a2a_remote_agent.js +4 -1
  2. package/dist/cjs/a2a/agent_card.js +102 -26
  3. package/dist/cjs/a2a/agent_to_a2a.js +1 -1
  4. package/dist/cjs/agents/functions.js +199 -25
  5. package/dist/cjs/agents/instructions.js +4 -1
  6. package/dist/cjs/agents/invocation_context.js +26 -4
  7. package/dist/cjs/agents/langgraph_agent.js +152 -0
  8. package/dist/cjs/agents/llm_agent.js +175 -101
  9. package/dist/cjs/agents/processors/code_execution_request_processor.js +10 -6
  10. package/dist/cjs/agents/remote_agent.js +187 -0
  11. package/dist/cjs/artifacts/in_memory_artifact_service.js +14 -0
  12. package/dist/cjs/auth/auth_handler.js +37 -5
  13. package/dist/cjs/auth/auth_schemes.js +9 -0
  14. package/dist/cjs/cli/cli_eval.js +532 -0
  15. package/dist/cjs/cli/utils/envs.js +116 -0
  16. package/dist/cjs/cli/utils/evals.js +137 -0
  17. package/dist/cjs/cli/utils/logs.js +150 -0
  18. package/dist/cjs/code_executors/agent_engine_sandbox_code_executor.js +7 -2
  19. package/dist/cjs/code_executors/code_execution_utils.js +48 -16
  20. package/dist/cjs/code_executors/code_executor_context.js +6 -2
  21. package/dist/cjs/common.js +72 -0
  22. package/dist/cjs/context/summarizers/llm_summarizer.js +12 -10
  23. package/dist/cjs/context/token_based_context_compactor.js +2 -1
  24. package/dist/cjs/events/event.js +4 -2
  25. package/dist/cjs/examples/example_util.js +23 -2
  26. package/dist/cjs/flows/llm_flows/audio_transcriber.js +170 -0
  27. package/dist/cjs/index.js +97 -1
  28. package/dist/cjs/integrations/agent_registry/helpers.js +1 -1
  29. package/dist/cjs/models/gemini_llm_connection.js +16 -8
  30. package/dist/cjs/models/google_llm.js +13 -16
  31. package/dist/cjs/models/llm_response.js +9 -1
  32. package/dist/cjs/models/registry.js +1 -1
  33. package/dist/cjs/models/routed_llm.js +9 -8
  34. package/dist/cjs/planners/plan_re_act_planner.js +1 -1
  35. package/dist/cjs/runner/in_memory_runner.js +6 -11
  36. package/dist/cjs/runner/runner.js +105 -33
  37. package/dist/cjs/sessions/db/operations.js +1 -1
  38. package/dist/cjs/sessions/in_memory_session_service.js +3 -1
  39. package/dist/cjs/sessions/state.js +8 -4
  40. package/dist/cjs/sessions/vertex_ai_session_service.js +34 -15
  41. package/dist/cjs/telemetry/tracing.js +97 -66
  42. package/dist/cjs/tools/agent_tool.js +44 -0
  43. package/dist/cjs/tools/apihub_tool/apihub_toolset.js +142 -0
  44. package/dist/cjs/tools/apihub_tool/clients/apihub_client.js +272 -0
  45. package/dist/cjs/tools/apihub_tool/clients/secret_client.js +148 -0
  46. package/dist/cjs/tools/apihub_tool/clients/service_account.js +48 -0
  47. package/dist/cjs/tools/application_integration_tool/application_integration_toolset.js +185 -0
  48. package/dist/cjs/tools/application_integration_tool/clients/cloud_request.js +133 -0
  49. package/dist/cjs/tools/application_integration_tool/clients/connections_client.js +603 -0
  50. package/dist/cjs/tools/application_integration_tool/clients/integration_client.js +219 -0
  51. package/dist/cjs/tools/crewai_tool.js +132 -0
  52. package/dist/cjs/tools/example_tool.js +10 -0
  53. package/dist/cjs/tools/function_tool.js +319 -0
  54. package/dist/cjs/tools/google_api_tool/google_api_tool.js +97 -0
  55. package/dist/cjs/tools/google_api_tool/google_api_tool_set.js +226 -0
  56. package/dist/cjs/tools/google_api_tool/google_api_tool_sets.js +58 -0
  57. package/dist/cjs/tools/google_api_tool/googleapi_to_openapi_converter.js +440 -0
  58. package/dist/cjs/tools/google_api_tool/index.js +64 -0
  59. package/dist/cjs/tools/langchain_tool.js +172 -0
  60. package/dist/cjs/tools/mcp/index.js +12 -0
  61. package/dist/cjs/tools/mcp/mcp_session_manager.js +25 -0
  62. package/dist/cjs/tools/openapi_tool/auth/auth_helpers.js +1 -2
  63. package/dist/cjs/tools/openapi_tool/auth/credential_exchangers/auto_auth_credential_exchanger.js +46 -2
  64. package/dist/cjs/tools/openapi_tool/auth/credential_exchangers/oauth2_exchanger.js +2 -1
  65. package/dist/cjs/tools/openapi_tool/auth/credential_exchangers/service_account_exchanger.js +94 -10
  66. package/dist/cjs/tools/openapi_tool/openapi_spec_parser/tool_auth_handler.js +14 -2
  67. package/dist/cjs/tools/retrieval/base_retrieval_tool.js +18 -3
  68. package/dist/cjs/tools/toolbox_tool.js +108 -0
  69. package/dist/cjs/utils/env_aware_utils.js +6 -8
  70. package/dist/cjs/utils/error_utils.js +11 -11
  71. package/dist/cjs/utils/file_extension_utils.js +11 -0
  72. package/dist/cjs/utils/file_utils.js +2 -24
  73. package/dist/cjs/utils/gemini_schema_util.js +4 -1
  74. package/dist/cjs/utils/json_utils.js +49 -0
  75. package/dist/cjs/utils/live_connection_utils.js +4 -5
  76. package/dist/cjs/utils/logger_node.js +7 -1
  77. package/dist/cjs/utils/object_notation_utils.js +6 -3
  78. package/dist/cjs/utils/streaming_utils.js +12 -12
  79. package/dist/cjs/version.js +1 -1
  80. package/dist/esm/a2a/a2a_remote_agent.js +4 -1
  81. package/dist/esm/a2a/agent_card.js +103 -27
  82. package/dist/esm/a2a/agent_to_a2a.js +5 -2
  83. package/dist/esm/agents/functions.js +198 -25
  84. package/dist/esm/agents/instructions.js +4 -1
  85. package/dist/esm/agents/invocation_context.js +24 -4
  86. package/dist/esm/agents/langgraph_agent.js +123 -0
  87. package/dist/esm/agents/llm_agent.js +185 -104
  88. package/dist/esm/agents/processors/code_execution_request_processor.js +13 -8
  89. package/dist/esm/agents/remote_agent.js +164 -0
  90. package/dist/esm/artifacts/in_memory_artifact_service.js +14 -0
  91. package/dist/esm/auth/auth_handler.js +37 -5
  92. package/dist/esm/auth/auth_schemes.js +8 -0
  93. package/dist/esm/cli/cli_eval.js +510 -0
  94. package/dist/esm/cli/utils/envs.js +78 -0
  95. package/dist/esm/cli/utils/evals.js +109 -0
  96. package/dist/esm/cli/utils/logs.js +111 -0
  97. package/dist/esm/code_executors/agent_engine_sandbox_code_executor.js +7 -2
  98. package/dist/esm/code_executors/code_execution_utils.js +46 -16
  99. package/dist/esm/code_executors/code_executor_context.js +5 -2
  100. package/dist/esm/common.js +67 -3
  101. package/dist/esm/context/summarizers/llm_summarizer.js +12 -10
  102. package/dist/esm/context/token_based_context_compactor.js +2 -1
  103. package/dist/esm/events/event.js +4 -2
  104. package/dist/esm/examples/example_util.js +21 -1
  105. package/dist/esm/flows/llm_flows/audio_transcriber.js +142 -0
  106. package/dist/esm/index.js +99 -1
  107. package/dist/esm/integrations/agent_registry/helpers.js +1 -1
  108. package/dist/esm/models/gemini_llm_connection.js +16 -8
  109. package/dist/esm/models/google_llm.js +14 -17
  110. package/dist/esm/models/llm_response.js +9 -1
  111. package/dist/esm/models/registry.js +1 -1
  112. package/dist/esm/models/routed_llm.js +9 -8
  113. package/dist/esm/planners/plan_re_act_planner.js +1 -1
  114. package/dist/esm/runner/in_memory_runner.js +6 -11
  115. package/dist/esm/runner/runner.js +105 -33
  116. package/dist/esm/sessions/db/operations.js +1 -1
  117. package/dist/esm/sessions/in_memory_session_service.js +3 -1
  118. package/dist/esm/sessions/state.js +8 -4
  119. package/dist/esm/sessions/vertex_ai_session_service.js +34 -15
  120. package/dist/esm/telemetry/tracing.js +97 -66
  121. package/dist/esm/tools/agent_tool.js +44 -0
  122. package/dist/esm/tools/apihub_tool/apihub_toolset.js +106 -0
  123. package/dist/esm/tools/apihub_tool/clients/apihub_client.js +244 -0
  124. package/dist/esm/tools/apihub_tool/clients/secret_client.js +132 -0
  125. package/dist/esm/tools/apihub_tool/clients/service_account.js +21 -0
  126. package/dist/esm/tools/application_integration_tool/application_integration_toolset.js +163 -0
  127. package/dist/esm/tools/application_integration_tool/clients/cloud_request.js +103 -0
  128. package/dist/esm/tools/application_integration_tool/clients/connections_client.js +561 -0
  129. package/dist/esm/tools/application_integration_tool/clients/integration_client.js +215 -0
  130. package/dist/esm/tools/crewai_tool.js +107 -0
  131. package/dist/esm/tools/example_tool.js +13 -1
  132. package/dist/esm/tools/function_tool.js +316 -0
  133. package/dist/esm/tools/google_api_tool/google_api_tool.js +70 -0
  134. package/dist/esm/tools/google_api_tool/google_api_tool_set.js +188 -0
  135. package/dist/esm/tools/google_api_tool/google_api_tool_sets.js +24 -0
  136. package/dist/esm/tools/google_api_tool/googleapi_to_openapi_converter.js +393 -0
  137. package/dist/esm/tools/google_api_tool/index.js +33 -0
  138. package/dist/esm/tools/langchain_tool.js +148 -0
  139. package/dist/esm/tools/mcp/index.js +6 -0
  140. package/dist/esm/tools/mcp/mcp_session_manager.js +25 -0
  141. package/dist/esm/tools/openapi_tool/auth/auth_helpers.js +1 -2
  142. package/dist/esm/tools/openapi_tool/auth/credential_exchangers/auto_auth_credential_exchanger.js +50 -3
  143. package/dist/esm/tools/openapi_tool/auth/credential_exchangers/oauth2_exchanger.js +2 -1
  144. package/dist/esm/tools/openapi_tool/auth/credential_exchangers/service_account_exchanger.js +94 -10
  145. package/dist/esm/tools/openapi_tool/openapi_spec_parser/tool_auth_handler.js +15 -2
  146. package/dist/esm/tools/retrieval/base_retrieval_tool.js +16 -2
  147. package/dist/esm/tools/toolbox_tool.js +80 -0
  148. package/dist/esm/utils/env_aware_utils.js +6 -8
  149. package/dist/esm/utils/error_utils.js +9 -10
  150. package/dist/esm/utils/file_extension_utils.js +11 -0
  151. package/dist/esm/utils/file_utils.js +2 -23
  152. package/dist/esm/utils/gemini_schema_util.js +4 -1
  153. package/dist/esm/utils/json_utils.js +21 -0
  154. package/dist/esm/utils/live_connection_utils.js +4 -5
  155. package/dist/esm/utils/logger_node.js +7 -1
  156. package/dist/esm/utils/object_notation_utils.js +4 -2
  157. package/dist/esm/utils/streaming_utils.js +11 -11
  158. package/dist/esm/version.js +1 -1
  159. package/dist/types/a2a/a2a_remote_agent.d.ts +9 -0
  160. package/dist/types/a2a/agent_card.d.ts +44 -1
  161. package/dist/types/a2a/agent_to_a2a.d.ts +9 -0
  162. package/dist/types/a2a/index.d.ts +1 -0
  163. package/dist/types/agents/functions.d.ts +33 -1
  164. package/dist/types/agents/invocation_context.d.ts +20 -1
  165. package/dist/types/agents/langgraph_agent.d.ts +153 -0
  166. package/dist/types/agents/remote_agent.d.ts +68 -0
  167. package/dist/types/auth/auth_handler.d.ts +28 -1
  168. package/dist/types/auth/auth_schemes.d.ts +13 -0
  169. package/dist/types/cli/cli_eval.d.ts +173 -0
  170. package/dist/types/cli/utils/envs.d.ts +23 -0
  171. package/dist/types/cli/utils/evals.d.ts +47 -0
  172. package/dist/types/cli/utils/logs.d.ts +38 -0
  173. package/dist/types/code_executors/code_execution_utils.d.ts +39 -0
  174. package/dist/types/code_executors/code_executor_context.d.ts +1 -0
  175. package/dist/types/common.d.ts +27 -4
  176. package/dist/types/events/event.d.ts +4 -0
  177. package/dist/types/examples/example_util.d.ts +17 -0
  178. package/dist/types/flows/llm_flows/audio_transcriber.d.ts +85 -0
  179. package/dist/types/index.d.ts +16 -1
  180. package/dist/types/models/gemini_llm_connection.d.ts +6 -7
  181. package/dist/types/models/routed_llm.d.ts +1 -1
  182. package/dist/types/runner/in_memory_runner.d.ts +2 -1
  183. package/dist/types/runner/runner.d.ts +34 -0
  184. package/dist/types/sessions/state.d.ts +5 -1
  185. package/dist/types/sessions/vertex_ai_session_service.d.ts +8 -0
  186. package/dist/types/skills/skill.d.ts +1 -1
  187. package/dist/types/telemetry/tracing.d.ts +4 -2
  188. package/dist/types/tools/apihub_tool/apihub_toolset.d.ts +122 -0
  189. package/dist/types/tools/apihub_tool/clients/apihub_client.d.ts +142 -0
  190. package/dist/types/tools/apihub_tool/clients/secret_client.d.ts +55 -0
  191. package/dist/types/tools/apihub_tool/clients/service_account.d.ts +11 -0
  192. package/dist/types/tools/application_integration_tool/application_integration_toolset.d.ts +86 -0
  193. package/dist/types/tools/application_integration_tool/clients/cloud_request.d.ts +53 -0
  194. package/dist/types/tools/application_integration_tool/clients/connections_client.d.ts +215 -0
  195. package/dist/types/tools/application_integration_tool/clients/integration_client.d.ts +72 -0
  196. package/dist/types/tools/crewai_tool.d.ts +85 -0
  197. package/dist/types/tools/example_tool.d.ts +10 -0
  198. package/dist/types/tools/function_tool.d.ts +32 -0
  199. package/dist/types/tools/google_api_tool/google_api_tool.d.ts +30 -0
  200. package/dist/types/tools/google_api_tool/google_api_tool_set.d.ts +78 -0
  201. package/dist/types/tools/google_api_tool/google_api_tool_sets.d.ts +33 -0
  202. package/dist/types/tools/google_api_tool/googleapi_to_openapi_converter.d.ts +164 -0
  203. package/dist/types/tools/google_api_tool/index.d.ts +11 -0
  204. package/dist/types/tools/langchain_tool.d.ts +103 -0
  205. package/dist/types/tools/mcp/index.d.ts +3 -0
  206. package/dist/types/tools/mcp/mcp_session_manager.d.ts +34 -1
  207. package/dist/types/tools/mcp/mcp_toolset.d.ts +15 -0
  208. package/dist/types/tools/openapi_tool/auth/auth_helpers.d.ts +19 -0
  209. package/dist/types/tools/openapi_tool/auth/credential_exchangers/auto_auth_credential_exchanger.d.ts +18 -1
  210. package/dist/types/tools/openapi_tool/openapi_spec_parser/tool_auth_handler.d.ts +2 -1
  211. package/dist/types/tools/retrieval/base_retrieval_tool.d.ts +15 -0
  212. package/dist/types/tools/toolbox_tool.d.ts +73 -0
  213. package/dist/types/utils/env_aware_utils.d.ts +18 -2
  214. package/dist/types/utils/error_utils.d.ts +7 -0
  215. package/dist/types/utils/file_utils.d.ts +6 -1
  216. package/dist/types/utils/gemini_schema_util.d.ts +1 -2
  217. package/dist/types/utils/json_utils.d.ts +11 -0
  218. package/dist/types/utils/object_notation_utils.d.ts +7 -0
  219. package/dist/types/utils/streaming_utils.d.ts +11 -4
  220. package/dist/types/version.d.ts +1 -1
  221. package/dist/web/index_web.js +102 -103
  222. package/dist/web/index_web.js.map +4 -4
  223. package/package.json +8 -3
  224. package/dist/cjs/models/gemini_continuation.js +0 -232
  225. package/dist/esm/models/gemini_continuation.js +0 -205
  226. package/dist/types/models/gemini_continuation.d.ts +0 -59
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@google/adk",
3
- "version": "2.2.1",
3
+ "version": "2.3.0",
4
4
  "description": "Google ADK JS",
5
5
  "author": "Google",
6
6
  "license": "Apache-2.0",
@@ -84,9 +84,9 @@
84
84
  "dependencies": {
85
85
  "@a2a-js/sdk": "^0.3.10",
86
86
  "@google-cloud/vertexai": "^1.12.0",
87
- "@google/genai": "^2.9.0",
87
+ "@google/genai": "^2.29.1",
88
88
  "@mikro-orm/core": "^7.2.0",
89
- "@opentelemetry/api": "1.9.0",
89
+ "@opentelemetry/api": "^1.9.0",
90
90
  "@opentelemetry/api-logs": "^0.222.0",
91
91
  "@opentelemetry/exporter-logs-otlp-http": "^0.222.0",
92
92
  "@opentelemetry/exporter-metrics-otlp-http": "^0.222.0",
@@ -113,6 +113,7 @@
113
113
  "devDependencies": {
114
114
  "@google-cloud/opentelemetry-cloud-monitoring-exporter": "^0.21.0",
115
115
  "@google-cloud/opentelemetry-cloud-trace-exporter": "^3.0.0",
116
+ "@google-cloud/secret-manager": "^7.1.1",
116
117
  "@google-cloud/storage": "^7.17.1",
117
118
  "@mikro-orm/sqlite": "^7.2.0",
118
119
  "@modelcontextprotocol/sdk": "^1.26.0",
@@ -126,6 +127,7 @@
126
127
  "peerDependencies": {
127
128
  "@google-cloud/opentelemetry-cloud-monitoring-exporter": "^0.21.0",
128
129
  "@google-cloud/opentelemetry-cloud-trace-exporter": "^3.0.0",
130
+ "@google-cloud/secret-manager": "^7.1.1",
129
131
  "@google-cloud/storage": "^7.17.1",
130
132
  "@mikro-orm/mariadb": "^7.2.0",
131
133
  "@mikro-orm/mssql": "^7.2.0",
@@ -142,6 +144,9 @@
142
144
  "@google-cloud/opentelemetry-cloud-trace-exporter": {
143
145
  "optional": true
144
146
  },
147
+ "@google-cloud/secret-manager": {
148
+ "optional": true
149
+ },
145
150
  "@google-cloud/storage": {
146
151
  "optional": true
147
152
  },
@@ -1,232 +0,0 @@
1
- /**
2
- * @license
3
- * Copyright 2026 Google LLC
4
- * SPDX-License-Identifier: Apache-2.0
5
- */
6
-
7
- "use strict";
8
- var __defProp = Object.defineProperty;
9
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
10
- var __getOwnPropNames = Object.getOwnPropertyNames;
11
- var __hasOwnProp = Object.prototype.hasOwnProperty;
12
- var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
13
- var __export = (target, all) => {
14
- for (var name in all)
15
- __defProp(target, name, { get: all[name], enumerable: true });
16
- };
17
- var __copyProps = (to, from, except, desc) => {
18
- if (from && typeof from === "object" || typeof from === "function") {
19
- for (let key of __getOwnPropNames(from))
20
- if (!__hasOwnProp.call(to, key) && key !== except)
21
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
22
- }
23
- return to;
24
- };
25
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
26
- var gemini_continuation_exports = {};
27
- __export(gemini_continuation_exports, {
28
- GeminiContinuation: () => GeminiContinuation,
29
- MAX_RESUMES: () => MAX_RESUMES,
30
- resumeToken: () => resumeToken
31
- });
32
- module.exports = __toCommonJS(gemini_continuation_exports);
33
- var import_genai = require("@google/genai");
34
- var import_content_processor_utils = require("../agents/processors/content_processor_utils.js");
35
- var import_logger = require("../utils/logger.js");
36
- var import_llm_response = require("./llm_response.js");
37
- /**
38
- * @license
39
- * Copyright 2026 Google LLC
40
- * SPDX-License-Identifier: Apache-2.0
41
- */
42
- const MAX_RESUMES = 256;
43
- function resumeToken(response) {
44
- var _a;
45
- const candidate = (_a = response.candidates) == null ? void 0 : _a[0];
46
- if ((candidate == null ? void 0 : candidate.finishReason) !== import_genai.FinishReason.CONTINUATION) {
47
- return void 0;
48
- }
49
- if (!candidate.continuationToken) {
50
- import_logger.logger.warn(
51
- "The model paused the generation for continuation, but the response carries no continuation token, so the partial output is returned."
52
- );
53
- return void 0;
54
- }
55
- return candidate.continuationToken;
56
- }
57
- __name(resumeToken, "resumeToken");
58
- const _GeminiContinuation = class _GeminiContinuation {
59
- constructor(contents, config) {
60
- this.config = config;
61
- this.parts = [];
62
- this.resumes = 0;
63
- this.contents = [...contents];
64
- }
65
- /**
66
- * Adds one request's output to the generation: its parts to the content and
67
- * its usage to the total.
68
- */
69
- addOutput(newParts, newUsage) {
70
- this.usage = addUsage(this.usage, newUsage);
71
- this.appendParts(newParts);
72
- }
73
- /**
74
- * Returns the request that resumes the generation after a request that
75
- * ended with `nextToken`. Returns undefined if the request did not pause,
76
- * returned the previous token again, or the generation was already resumed
77
- * {@link MAX_RESUMES} times.
78
- */
79
- resumeRequest(nextToken) {
80
- if (nextToken === void 0) {
81
- return void 0;
82
- }
83
- if (!this.willResume(nextToken)) {
84
- if (nextToken === this.token) {
85
- import_logger.logger.warn(
86
- "The model returned the same continuation token twice; returning the output generated so far."
87
- );
88
- } else {
89
- import_logger.logger.warn(
90
- `The model paused the generation ${MAX_RESUMES} times; returning the output generated so far.`
91
- );
92
- }
93
- return void 0;
94
- }
95
- this.token = nextToken;
96
- this.resumes++;
97
- import_logger.logger.debug("The model paused the generation; resuming it.");
98
- return this.buildResumeRequest(nextToken);
99
- }
100
- /** Returns `response` with the content and usage of all requests, if resumed. */
101
- complete(response) {
102
- var _a;
103
- if (!this.resumed) {
104
- return (0, import_llm_response.createLlmResponse)(response);
105
- }
106
- const candidate = (_a = response.candidates) == null ? void 0 : _a[0];
107
- return (0, import_llm_response.createLlmResponse)({
108
- ...response,
109
- usageMetadata: this.usage,
110
- candidates: candidate && this.parts.length > 0 ? [{ ...candidate, content: this.content() }] : response.candidates
111
- });
112
- }
113
- /**
114
- * Returns the final streamed `response`, with usage summed across all
115
- * requests if resumed.
116
- */
117
- withSummedUsage(response) {
118
- return this.resumed ? { ...response, usageMetadata: this.usage } : response;
119
- }
120
- /** Returns whether a request that ended with `nextToken` is resumed. */
121
- willResume(nextToken) {
122
- return nextToken !== this.token && this.resumes < MAX_RESUMES;
123
- }
124
- /** Whether the generation took more than one request. */
125
- get resumed() {
126
- return this.token !== void 0;
127
- }
128
- content() {
129
- return { role: "model", parts: [...this.parts] };
130
- }
131
- buildResumeRequest(nextToken) {
132
- const contents = [...this.contents];
133
- if (this.parts.length > 0) {
134
- const content = structuredClone(this.content());
135
- (0, import_content_processor_utils.removeClientFunctionCallId)(content);
136
- contents.push(content);
137
- }
138
- return {
139
- contents,
140
- config: { ...this.config, continuationToken: nextToken }
141
- };
142
- }
143
- /** Appends `newParts`, joining text as the streaming aggregator does. */
144
- appendParts(newParts) {
145
- for (const part of newParts) {
146
- const last = this.parts.length - 1;
147
- const previous = last >= 0 ? this.parts[last] : void 0;
148
- if (previous && canJoin(previous, part)) {
149
- const joined = { text: previous.text + part.text };
150
- if (previous.thought !== void 0) {
151
- joined.thought = previous.thought;
152
- }
153
- const signature = previous.thoughtSignature || part.thoughtSignature;
154
- if (signature !== void 0) {
155
- joined.thoughtSignature = signature;
156
- }
157
- this.parts[last] = joined;
158
- } else {
159
- this.parts.push(part);
160
- }
161
- }
162
- }
163
- };
164
- __name(_GeminiContinuation, "GeminiContinuation");
165
- let GeminiContinuation = _GeminiContinuation;
166
- function canJoin(first, second) {
167
- var _a, _b;
168
- return isText(first) && isText(second) && ((_a = first.thought) != null ? _a : false) === ((_b = second.thought) != null ? _b : false);
169
- }
170
- __name(canJoin, "canJoin");
171
- function isText(part) {
172
- if (!part.text) {
173
- return false;
174
- }
175
- return Object.entries(part).every(
176
- ([key, value]) => value === void 0 || key === "text" || key === "thought" || key === "thoughtSignature"
177
- );
178
- }
179
- __name(isText, "isText");
180
- const COUNT_FIELDS = [
181
- "promptTokenCount",
182
- "candidatesTokenCount",
183
- "totalTokenCount",
184
- "cachedContentTokenCount",
185
- "thoughtsTokenCount",
186
- "toolUsePromptTokenCount"
187
- ];
188
- const MODALITY_FIELDS = [
189
- "promptTokensDetails",
190
- "candidatesTokensDetails",
191
- "cacheTokensDetails",
192
- "toolUsePromptTokensDetails"
193
- ];
194
- function addUsage(total, next) {
195
- var _a, _b;
196
- if (!total || !next) {
197
- return total != null ? total : next;
198
- }
199
- const combined = { ...next };
200
- for (const field of COUNT_FIELDS) {
201
- if (total[field] !== void 0 || next[field] !== void 0) {
202
- combined[field] = ((_a = total[field]) != null ? _a : 0) + ((_b = next[field]) != null ? _b : 0);
203
- }
204
- }
205
- for (const field of MODALITY_FIELDS) {
206
- if (total[field] !== void 0 || next[field] !== void 0) {
207
- combined[field] = addModalityCounts(total[field], next[field]);
208
- }
209
- }
210
- return combined;
211
- }
212
- __name(addUsage, "addUsage");
213
- function addModalityCounts(first, second) {
214
- var _a, _b;
215
- const totals = /* @__PURE__ */ new Map();
216
- for (const count of [...first != null ? first : [], ...second != null ? second : []]) {
217
- totals.set(
218
- count.modality,
219
- ((_a = totals.get(count.modality)) != null ? _a : 0) + ((_b = count.tokenCount) != null ? _b : 0)
220
- );
221
- }
222
- return [...totals].map(
223
- ([modality, tokenCount]) => modality === void 0 ? { tokenCount } : { modality, tokenCount }
224
- );
225
- }
226
- __name(addModalityCounts, "addModalityCounts");
227
- // Annotate the CommonJS export names for ESM import in node:
228
- 0 && (module.exports = {
229
- GeminiContinuation,
230
- MAX_RESUMES,
231
- resumeToken
232
- });
@@ -1,205 +0,0 @@
1
- import {createRequire as topLevelCreateRequire} from 'module';
2
- const require = topLevelCreateRequire(import.meta.url);
3
- var __defProp = Object.defineProperty;
4
- var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
5
- /**
6
- * @license
7
- * Copyright 2026 Google LLC
8
- * SPDX-License-Identifier: Apache-2.0
9
- */
10
- import {
11
- FinishReason
12
- } from "@google/genai";
13
- import { removeClientFunctionCallId } from "../agents/processors/content_processor_utils.js";
14
- import { logger } from "../utils/logger.js";
15
- import { createLlmResponse } from "./llm_response.js";
16
- const MAX_RESUMES = 256;
17
- function resumeToken(response) {
18
- var _a;
19
- const candidate = (_a = response.candidates) == null ? void 0 : _a[0];
20
- if ((candidate == null ? void 0 : candidate.finishReason) !== FinishReason.CONTINUATION) {
21
- return void 0;
22
- }
23
- if (!candidate.continuationToken) {
24
- logger.warn(
25
- "The model paused the generation for continuation, but the response carries no continuation token, so the partial output is returned."
26
- );
27
- return void 0;
28
- }
29
- return candidate.continuationToken;
30
- }
31
- __name(resumeToken, "resumeToken");
32
- const _GeminiContinuation = class _GeminiContinuation {
33
- constructor(contents, config) {
34
- this.config = config;
35
- this.parts = [];
36
- this.resumes = 0;
37
- this.contents = [...contents];
38
- }
39
- /**
40
- * Adds one request's output to the generation: its parts to the content and
41
- * its usage to the total.
42
- */
43
- addOutput(newParts, newUsage) {
44
- this.usage = addUsage(this.usage, newUsage);
45
- this.appendParts(newParts);
46
- }
47
- /**
48
- * Returns the request that resumes the generation after a request that
49
- * ended with `nextToken`. Returns undefined if the request did not pause,
50
- * returned the previous token again, or the generation was already resumed
51
- * {@link MAX_RESUMES} times.
52
- */
53
- resumeRequest(nextToken) {
54
- if (nextToken === void 0) {
55
- return void 0;
56
- }
57
- if (!this.willResume(nextToken)) {
58
- if (nextToken === this.token) {
59
- logger.warn(
60
- "The model returned the same continuation token twice; returning the output generated so far."
61
- );
62
- } else {
63
- logger.warn(
64
- `The model paused the generation ${MAX_RESUMES} times; returning the output generated so far.`
65
- );
66
- }
67
- return void 0;
68
- }
69
- this.token = nextToken;
70
- this.resumes++;
71
- logger.debug("The model paused the generation; resuming it.");
72
- return this.buildResumeRequest(nextToken);
73
- }
74
- /** Returns `response` with the content and usage of all requests, if resumed. */
75
- complete(response) {
76
- var _a;
77
- if (!this.resumed) {
78
- return createLlmResponse(response);
79
- }
80
- const candidate = (_a = response.candidates) == null ? void 0 : _a[0];
81
- return createLlmResponse({
82
- ...response,
83
- usageMetadata: this.usage,
84
- candidates: candidate && this.parts.length > 0 ? [{ ...candidate, content: this.content() }] : response.candidates
85
- });
86
- }
87
- /**
88
- * Returns the final streamed `response`, with usage summed across all
89
- * requests if resumed.
90
- */
91
- withSummedUsage(response) {
92
- return this.resumed ? { ...response, usageMetadata: this.usage } : response;
93
- }
94
- /** Returns whether a request that ended with `nextToken` is resumed. */
95
- willResume(nextToken) {
96
- return nextToken !== this.token && this.resumes < MAX_RESUMES;
97
- }
98
- /** Whether the generation took more than one request. */
99
- get resumed() {
100
- return this.token !== void 0;
101
- }
102
- content() {
103
- return { role: "model", parts: [...this.parts] };
104
- }
105
- buildResumeRequest(nextToken) {
106
- const contents = [...this.contents];
107
- if (this.parts.length > 0) {
108
- const content = structuredClone(this.content());
109
- removeClientFunctionCallId(content);
110
- contents.push(content);
111
- }
112
- return {
113
- contents,
114
- config: { ...this.config, continuationToken: nextToken }
115
- };
116
- }
117
- /** Appends `newParts`, joining text as the streaming aggregator does. */
118
- appendParts(newParts) {
119
- for (const part of newParts) {
120
- const last = this.parts.length - 1;
121
- const previous = last >= 0 ? this.parts[last] : void 0;
122
- if (previous && canJoin(previous, part)) {
123
- const joined = { text: previous.text + part.text };
124
- if (previous.thought !== void 0) {
125
- joined.thought = previous.thought;
126
- }
127
- const signature = previous.thoughtSignature || part.thoughtSignature;
128
- if (signature !== void 0) {
129
- joined.thoughtSignature = signature;
130
- }
131
- this.parts[last] = joined;
132
- } else {
133
- this.parts.push(part);
134
- }
135
- }
136
- }
137
- };
138
- __name(_GeminiContinuation, "GeminiContinuation");
139
- let GeminiContinuation = _GeminiContinuation;
140
- function canJoin(first, second) {
141
- var _a, _b;
142
- return isText(first) && isText(second) && ((_a = first.thought) != null ? _a : false) === ((_b = second.thought) != null ? _b : false);
143
- }
144
- __name(canJoin, "canJoin");
145
- function isText(part) {
146
- if (!part.text) {
147
- return false;
148
- }
149
- return Object.entries(part).every(
150
- ([key, value]) => value === void 0 || key === "text" || key === "thought" || key === "thoughtSignature"
151
- );
152
- }
153
- __name(isText, "isText");
154
- const COUNT_FIELDS = [
155
- "promptTokenCount",
156
- "candidatesTokenCount",
157
- "totalTokenCount",
158
- "cachedContentTokenCount",
159
- "thoughtsTokenCount",
160
- "toolUsePromptTokenCount"
161
- ];
162
- const MODALITY_FIELDS = [
163
- "promptTokensDetails",
164
- "candidatesTokensDetails",
165
- "cacheTokensDetails",
166
- "toolUsePromptTokensDetails"
167
- ];
168
- function addUsage(total, next) {
169
- var _a, _b;
170
- if (!total || !next) {
171
- return total != null ? total : next;
172
- }
173
- const combined = { ...next };
174
- for (const field of COUNT_FIELDS) {
175
- if (total[field] !== void 0 || next[field] !== void 0) {
176
- combined[field] = ((_a = total[field]) != null ? _a : 0) + ((_b = next[field]) != null ? _b : 0);
177
- }
178
- }
179
- for (const field of MODALITY_FIELDS) {
180
- if (total[field] !== void 0 || next[field] !== void 0) {
181
- combined[field] = addModalityCounts(total[field], next[field]);
182
- }
183
- }
184
- return combined;
185
- }
186
- __name(addUsage, "addUsage");
187
- function addModalityCounts(first, second) {
188
- var _a, _b;
189
- const totals = /* @__PURE__ */ new Map();
190
- for (const count of [...first != null ? first : [], ...second != null ? second : []]) {
191
- totals.set(
192
- count.modality,
193
- ((_a = totals.get(count.modality)) != null ? _a : 0) + ((_b = count.tokenCount) != null ? _b : 0)
194
- );
195
- }
196
- return [...totals].map(
197
- ([modality, tokenCount]) => modality === void 0 ? { tokenCount } : { modality, tokenCount }
198
- );
199
- }
200
- __name(addModalityCounts, "addModalityCounts");
201
- export {
202
- GeminiContinuation,
203
- MAX_RESUMES,
204
- resumeToken
205
- };
@@ -1,59 +0,0 @@
1
- /**
2
- * @license
3
- * Copyright 2026 Google LLC
4
- * SPDX-License-Identifier: Apache-2.0
5
- */
6
- import { Content, GenerateContentConfig, GenerateContentResponse, GenerateContentResponseUsageMetadata, Part } from '@google/genai';
7
- import { LlmResponse } from './llm_response.js';
8
- /** The most times one generation is resumed after the model pauses it. */
9
- export declare const MAX_RESUMES = 256;
10
- /** The contents and config of a request that resumes a paused generation. */
11
- export interface ResumeRequest {
12
- contents: Content[];
13
- config: GenerateContentConfig;
14
- }
15
- /**
16
- * Returns the token that resumes `response`, or undefined if it did not pause
17
- * or has no token.
18
- */
19
- export declare function resumeToken(response: GenerateContentResponse): string | undefined;
20
- /**
21
- * One generation, carried across the requests that resume it after the model
22
- * pauses it.
23
- */
24
- export declare class GeminiContinuation {
25
- private readonly config?;
26
- private readonly contents;
27
- private readonly parts;
28
- private usage?;
29
- private token?;
30
- private resumes;
31
- constructor(contents: Content[], config?: GenerateContentConfig | undefined);
32
- /**
33
- * Adds one request's output to the generation: its parts to the content and
34
- * its usage to the total.
35
- */
36
- addOutput(newParts: Part[], newUsage: GenerateContentResponseUsageMetadata | undefined): void;
37
- /**
38
- * Returns the request that resumes the generation after a request that
39
- * ended with `nextToken`. Returns undefined if the request did not pause,
40
- * returned the previous token again, or the generation was already resumed
41
- * {@link MAX_RESUMES} times.
42
- */
43
- resumeRequest(nextToken: string | undefined): ResumeRequest | undefined;
44
- /** Returns `response` with the content and usage of all requests, if resumed. */
45
- complete(response: GenerateContentResponse): LlmResponse;
46
- /**
47
- * Returns the final streamed `response`, with usage summed across all
48
- * requests if resumed.
49
- */
50
- withSummedUsage(response: LlmResponse): LlmResponse;
51
- /** Returns whether a request that ended with `nextToken` is resumed. */
52
- willResume(nextToken: string): boolean;
53
- /** Whether the generation took more than one request. */
54
- private get resumed();
55
- private content;
56
- private buildResumeRequest;
57
- /** Appends `newParts`, joining text as the streaming aggregator does. */
58
- private appendParts;
59
- }