@google/adk 0.2.5 → 0.4.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 (264) hide show
  1. package/dist/cjs/a2a/part_converter_utils.js +210 -0
  2. package/dist/cjs/agents/active_streaming_tool.js +1 -1
  3. package/dist/cjs/agents/base_agent.js +46 -24
  4. package/dist/cjs/agents/base_llm_processor.js +1 -1
  5. package/dist/cjs/agents/callback_context.js +5 -2
  6. package/dist/cjs/agents/content_processor_utils.js +16 -8
  7. package/dist/cjs/agents/functions.js +81 -30
  8. package/dist/cjs/agents/instructions.js +1 -1
  9. package/dist/cjs/agents/invocation_context.js +1 -1
  10. package/dist/cjs/agents/live_request_queue.js +1 -1
  11. package/dist/cjs/agents/llm_agent.js +106 -62
  12. package/dist/cjs/agents/loop_agent.js +3 -2
  13. package/dist/cjs/agents/parallel_agent.js +4 -5
  14. package/dist/cjs/agents/readonly_context.js +13 -1
  15. package/dist/cjs/agents/run_config.js +2 -1
  16. package/dist/cjs/agents/sequential_agent.js +1 -1
  17. package/dist/cjs/agents/transcription_entry.js +1 -1
  18. package/dist/cjs/artifacts/base_artifact_service.js +1 -1
  19. package/dist/cjs/artifacts/file_artifact_service.js +491 -0
  20. package/dist/cjs/artifacts/gcs_artifact_service.js +145 -58
  21. package/dist/cjs/artifacts/in_memory_artifact_service.js +72 -10
  22. package/dist/cjs/artifacts/registry.js +55 -0
  23. package/dist/cjs/auth/auth_credential.js +1 -1
  24. package/dist/cjs/auth/auth_handler.js +4 -2
  25. package/dist/cjs/auth/auth_schemes.js +1 -1
  26. package/dist/cjs/auth/auth_tool.js +1 -1
  27. package/dist/cjs/auth/credential_service/base_credential_service.js +1 -1
  28. package/dist/cjs/auth/credential_service/in_memory_credential_service.js +1 -1
  29. package/dist/cjs/auth/exchanger/base_credential_exchanger.js +1 -1
  30. package/dist/cjs/auth/exchanger/credential_exchanger_registry.js +1 -1
  31. package/dist/cjs/code_executors/base_code_executor.js +4 -2
  32. package/dist/cjs/code_executors/built_in_code_executor.js +8 -4
  33. package/dist/cjs/code_executors/code_execution_utils.js +1 -1
  34. package/dist/cjs/code_executors/code_executor_context.js +6 -6
  35. package/dist/cjs/common.js +18 -1
  36. package/dist/cjs/events/event.js +34 -7
  37. package/dist/cjs/events/event_actions.js +2 -2
  38. package/dist/cjs/events/structured_events.js +105 -0
  39. package/dist/cjs/examples/base_example_provider.js +1 -1
  40. package/dist/cjs/examples/example.js +1 -1
  41. package/dist/cjs/examples/example_util.js +1 -1
  42. package/dist/cjs/index.js +54 -83
  43. package/dist/cjs/index_web.js +1 -1
  44. package/dist/cjs/memory/base_memory_service.js +1 -1
  45. package/dist/cjs/memory/in_memory_memory_service.js +4 -2
  46. package/dist/cjs/memory/memory_entry.js +1 -1
  47. package/dist/cjs/models/apigee_llm.js +182 -0
  48. package/dist/cjs/models/base_llm.js +9 -5
  49. package/dist/cjs/models/base_llm_connection.js +1 -1
  50. package/dist/cjs/models/gemini_llm_connection.js +2 -1
  51. package/dist/cjs/models/google_llm.js +73 -54
  52. package/dist/cjs/models/llm_request.js +1 -1
  53. package/dist/cjs/models/llm_response.js +1 -1
  54. package/dist/cjs/models/registry.js +3 -1
  55. package/dist/cjs/plugins/base_plugin.js +13 -1
  56. package/dist/cjs/plugins/logging_plugin.js +51 -14
  57. package/dist/cjs/plugins/plugin_manager.js +57 -25
  58. package/dist/cjs/plugins/security_plugin.js +2 -2
  59. package/dist/cjs/runner/in_memory_runner.js +1 -1
  60. package/dist/cjs/runner/runner.js +142 -96
  61. package/dist/cjs/sessions/base_session_service.js +53 -3
  62. package/dist/cjs/sessions/database_session_service.js +364 -0
  63. package/dist/cjs/sessions/db/operations.js +114 -0
  64. package/dist/cjs/sessions/db/schema.js +204 -0
  65. package/dist/cjs/sessions/in_memory_session_service.js +60 -34
  66. package/dist/cjs/sessions/registry.js +49 -0
  67. package/dist/cjs/sessions/session.js +1 -1
  68. package/dist/cjs/sessions/state.js +1 -1
  69. package/dist/cjs/telemetry/google_cloud.js +8 -10
  70. package/dist/cjs/telemetry/setup.js +16 -8
  71. package/dist/cjs/telemetry/tracing.js +38 -16
  72. package/dist/cjs/tools/agent_tool.js +9 -5
  73. package/dist/cjs/tools/base_tool.js +5 -3
  74. package/dist/cjs/tools/base_toolset.js +1 -1
  75. package/dist/cjs/tools/forwarding_artifact_service.js +18 -2
  76. package/dist/cjs/tools/function_tool.js +2 -3
  77. package/dist/cjs/tools/google_search_tool.js +2 -3
  78. package/dist/cjs/tools/long_running_tool.js +1 -1
  79. package/dist/cjs/tools/mcp/mcp_session_manager.js +17 -11
  80. package/dist/cjs/tools/mcp/mcp_tool.js +2 -4
  81. package/dist/cjs/tools/mcp/mcp_toolset.js +2 -2
  82. package/dist/cjs/tools/tool_confirmation.js +1 -1
  83. package/dist/cjs/tools/tool_context.js +1 -1
  84. package/dist/cjs/utils/client_labels.js +1 -1
  85. package/dist/cjs/utils/env_aware_utils.js +11 -2
  86. package/dist/cjs/utils/gemini_schema_util.js +10 -5
  87. package/dist/cjs/utils/logger.js +48 -4
  88. package/dist/cjs/utils/model_name.js +1 -1
  89. package/dist/cjs/utils/object_notation_utils.js +78 -0
  90. package/dist/cjs/utils/simple_zod_to_json.js +101 -142
  91. package/dist/cjs/utils/variant_utils.js +3 -9
  92. package/dist/cjs/version.js +2 -2
  93. package/dist/esm/a2a/part_converter_utils.js +171 -0
  94. package/dist/esm/agents/base_agent.js +50 -24
  95. package/dist/esm/agents/callback_context.js +4 -1
  96. package/dist/esm/agents/content_processor_utils.js +25 -9
  97. package/dist/esm/agents/functions.js +84 -29
  98. package/dist/esm/agents/llm_agent.js +110 -62
  99. package/dist/esm/agents/loop_agent.js +2 -1
  100. package/dist/esm/agents/parallel_agent.js +3 -4
  101. package/dist/esm/agents/readonly_context.js +12 -0
  102. package/dist/esm/agents/run_config.js +1 -0
  103. package/dist/esm/artifacts/file_artifact_service.js +451 -0
  104. package/dist/esm/artifacts/gcs_artifact_service.js +144 -57
  105. package/dist/esm/artifacts/in_memory_artifact_service.js +69 -8
  106. package/dist/esm/artifacts/registry.js +28 -0
  107. package/dist/esm/auth/auth_handler.js +3 -1
  108. package/dist/esm/code_executors/base_code_executor.js +3 -1
  109. package/dist/esm/code_executors/built_in_code_executor.js +7 -3
  110. package/dist/esm/code_executors/code_executor_context.js +5 -5
  111. package/dist/esm/common.js +12 -2
  112. package/dist/esm/events/event.js +30 -5
  113. package/dist/esm/events/event_actions.js +1 -1
  114. package/dist/esm/events/structured_events.js +74 -0
  115. package/dist/esm/index.js +18 -88
  116. package/dist/esm/memory/in_memory_memory_service.js +3 -1
  117. package/dist/esm/models/apigee_llm.js +152 -0
  118. package/dist/esm/models/base_llm.js +8 -4
  119. package/dist/esm/models/gemini_llm_connection.js +1 -0
  120. package/dist/esm/models/google_llm.js +75 -53
  121. package/dist/esm/models/registry.js +2 -0
  122. package/dist/esm/plugins/base_plugin.js +12 -0
  123. package/dist/esm/plugins/logging_plugin.js +55 -14
  124. package/dist/esm/plugins/plugin_manager.js +56 -24
  125. package/dist/esm/plugins/security_plugin.js +1 -1
  126. package/dist/esm/runner/runner.js +145 -96
  127. package/dist/esm/sessions/base_session_service.js +49 -1
  128. package/dist/esm/sessions/database_session_service.js +350 -0
  129. package/dist/esm/sessions/db/operations.js +87 -0
  130. package/dist/esm/sessions/db/schema.js +172 -0
  131. package/dist/esm/sessions/in_memory_session_service.js +61 -33
  132. package/dist/esm/sessions/registry.js +25 -0
  133. package/dist/esm/telemetry/google_cloud.js +7 -9
  134. package/dist/esm/telemetry/setup.js +23 -9
  135. package/dist/esm/telemetry/tracing.js +37 -15
  136. package/dist/esm/tools/agent_tool.js +8 -4
  137. package/dist/esm/tools/base_tool.js +4 -2
  138. package/dist/esm/tools/forwarding_artifact_service.js +17 -1
  139. package/dist/esm/tools/function_tool.js +1 -2
  140. package/dist/esm/tools/google_search_tool.js +2 -5
  141. package/dist/esm/tools/long_running_tool.js +3 -1
  142. package/dist/esm/tools/mcp/mcp_session_manager.js +22 -12
  143. package/dist/esm/tools/mcp/mcp_tool.js +1 -3
  144. package/dist/esm/tools/mcp/mcp_toolset.js +1 -1
  145. package/dist/esm/utils/env_aware_utils.js +9 -1
  146. package/dist/esm/utils/gemini_schema_util.js +9 -4
  147. package/dist/esm/utils/logger.js +43 -2
  148. package/dist/esm/utils/object_notation_utils.js +47 -0
  149. package/dist/esm/utils/simple_zod_to_json.js +102 -141
  150. package/dist/esm/utils/variant_utils.js +1 -7
  151. package/dist/esm/version.js +1 -1
  152. package/dist/types/a2a/part_converter_utils.d.ts +47 -0
  153. package/dist/types/agents/base_agent.d.ts +2 -1
  154. package/dist/types/agents/callback_context.d.ts +1 -1
  155. package/dist/types/agents/llm_agent.d.ts +12 -12
  156. package/dist/types/agents/loop_agent.d.ts +1 -1
  157. package/dist/types/agents/parallel_agent.d.ts +1 -1
  158. package/dist/types/agents/readonly_context.d.ts +8 -0
  159. package/dist/types/agents/run_config.d.ts +6 -0
  160. package/dist/types/artifacts/base_artifact_service.d.ts +31 -0
  161. package/dist/types/artifacts/file_artifact_service.d.ts +43 -0
  162. package/dist/types/artifacts/gcs_artifact_service.d.ts +3 -1
  163. package/dist/types/artifacts/in_memory_artifact_service.d.ts +8 -5
  164. package/dist/types/artifacts/registry.d.ts +7 -0
  165. package/dist/types/code_executors/built_in_code_executor.d.ts +1 -1
  166. package/dist/types/code_executors/code_executor_context.d.ts +2 -4
  167. package/dist/types/common.d.ts +13 -3
  168. package/dist/types/events/event.d.ts +15 -1
  169. package/dist/types/events/event_actions.d.ts +1 -1
  170. package/dist/types/events/structured_events.d.ts +106 -0
  171. package/dist/types/index.d.ts +7 -3
  172. package/dist/types/models/apigee_llm.d.ts +59 -0
  173. package/dist/types/models/base_llm_connection.d.ts +1 -1
  174. package/dist/types/models/google_llm.d.ts +5 -2
  175. package/dist/types/models/llm_response.d.ts +1 -1
  176. package/dist/types/plugins/logging_plugin.d.ts +12 -12
  177. package/dist/types/plugins/plugin_manager.d.ts +12 -12
  178. package/dist/types/plugins/security_plugin.d.ts +1 -1
  179. package/dist/types/runner/runner.d.ts +16 -1
  180. package/dist/types/sessions/base_session_service.d.ts +20 -0
  181. package/dist/types/sessions/database_session_service.d.ts +31 -0
  182. package/dist/types/sessions/db/operations.d.ts +29 -0
  183. package/dist/types/sessions/db/schema.d.ts +45 -0
  184. package/dist/types/sessions/in_memory_session_service.d.ts +9 -6
  185. package/dist/types/sessions/registry.d.ts +7 -0
  186. package/dist/types/telemetry/setup.d.ts +1 -1
  187. package/dist/types/telemetry/tracing.d.ts +7 -6
  188. package/dist/types/tools/agent_tool.d.ts +1 -1
  189. package/dist/types/tools/base_tool.d.ts +1 -1
  190. package/dist/types/tools/base_toolset.d.ts +2 -1
  191. package/dist/types/tools/forwarding_artifact_service.d.ts +4 -2
  192. package/dist/types/tools/function_tool.d.ts +4 -3
  193. package/dist/types/tools/google_search_tool.d.ts +3 -3
  194. package/dist/types/tools/mcp/mcp_session_manager.d.ts +10 -3
  195. package/dist/types/tools/mcp/mcp_toolset.d.ts +1 -2
  196. package/dist/types/utils/env_aware_utils.d.ts +7 -0
  197. package/dist/types/utils/gemini_schema_util.d.ts +4 -12
  198. package/dist/types/utils/logger.d.ts +11 -10
  199. package/dist/types/utils/object_notation_utils.d.ts +21 -0
  200. package/dist/types/utils/simple_zod_to_json.d.ts +5 -4
  201. package/dist/types/version.d.ts +1 -1
  202. package/dist/web/a2a/part_converter_utils.js +171 -0
  203. package/dist/web/agents/base_agent.js +96 -35
  204. package/dist/web/agents/callback_context.js +4 -1
  205. package/dist/web/agents/content_processor_utils.js +25 -9
  206. package/dist/web/agents/functions.js +84 -29
  207. package/dist/web/agents/llm_agent.js +166 -83
  208. package/dist/web/agents/loop_agent.js +2 -1
  209. package/dist/web/agents/parallel_agent.js +3 -4
  210. package/dist/web/agents/readonly_context.js +12 -0
  211. package/dist/web/agents/run_config.js +2 -1
  212. package/dist/web/artifacts/file_artifact_service.js +506 -0
  213. package/dist/web/artifacts/gcs_artifact_service.js +139 -54
  214. package/dist/web/artifacts/in_memory_artifact_service.js +69 -8
  215. package/dist/web/artifacts/registry.js +28 -0
  216. package/dist/web/auth/auth_handler.js +3 -1
  217. package/dist/web/code_executors/base_code_executor.js +3 -1
  218. package/dist/web/code_executors/built_in_code_executor.js +7 -3
  219. package/dist/web/code_executors/code_executor_context.js +5 -5
  220. package/dist/web/common.js +12 -2
  221. package/dist/web/events/event.js +30 -5
  222. package/dist/web/events/event_actions.js +1 -1
  223. package/dist/web/events/structured_events.js +74 -0
  224. package/dist/web/index.js +18 -8
  225. package/dist/web/memory/in_memory_memory_service.js +3 -1
  226. package/dist/web/models/apigee_llm.js +219 -0
  227. package/dist/web/models/base_llm.js +8 -4
  228. package/dist/web/models/gemini_llm_connection.js +1 -0
  229. package/dist/web/models/google_llm.js +75 -50
  230. package/dist/web/models/registry.js +2 -0
  231. package/dist/web/plugins/base_plugin.js +12 -0
  232. package/dist/web/plugins/logging_plugin.js +55 -14
  233. package/dist/web/plugins/plugin_manager.js +56 -24
  234. package/dist/web/plugins/security_plugin.js +1 -1
  235. package/dist/web/runner/runner.js +192 -108
  236. package/dist/web/sessions/base_session_service.js +49 -1
  237. package/dist/web/sessions/database_session_service.js +368 -0
  238. package/dist/web/sessions/db/operations.js +87 -0
  239. package/dist/web/sessions/db/schema.js +172 -0
  240. package/dist/web/sessions/in_memory_session_service.js +61 -33
  241. package/dist/web/sessions/registry.js +25 -0
  242. package/dist/web/telemetry/google_cloud.js +7 -9
  243. package/dist/web/telemetry/setup.js +23 -9
  244. package/dist/web/telemetry/tracing.js +37 -15
  245. package/dist/web/tools/agent_tool.js +8 -4
  246. package/dist/web/tools/base_tool.js +4 -2
  247. package/dist/web/tools/forwarding_artifact_service.js +17 -1
  248. package/dist/web/tools/function_tool.js +1 -2
  249. package/dist/web/tools/google_search_tool.js +2 -5
  250. package/dist/web/tools/long_running_tool.js +3 -1
  251. package/dist/web/tools/mcp/mcp_session_manager.js +22 -12
  252. package/dist/web/tools/mcp/mcp_tool.js +1 -3
  253. package/dist/web/tools/mcp/mcp_toolset.js +1 -1
  254. package/dist/web/utils/env_aware_utils.js +9 -1
  255. package/dist/web/utils/gemini_schema_util.js +9 -4
  256. package/dist/web/utils/logger.js +43 -2
  257. package/dist/web/utils/object_notation_utils.js +47 -0
  258. package/dist/web/utils/simple_zod_to_json.js +102 -155
  259. package/dist/web/utils/variant_utils.js +1 -7
  260. package/dist/web/version.js +1 -1
  261. package/package.json +16 -5
  262. package/dist/cjs/index.js.map +0 -7
  263. package/dist/esm/index.js.map +0 -7
  264. package/dist/web/index.js.map +0 -7
@@ -3,14 +3,17 @@
3
3
  * Copyright 2025 Google LLC
4
4
  * SPDX-License-Identifier: Apache-2.0
5
5
  */
6
- import { createPartFromText, FinishReason, GoogleGenAI } from "@google/genai";
6
+ import {
7
+ createPartFromText,
8
+ FinishReason,
9
+ GoogleGenAI
10
+ } from "@google/genai";
11
+ import { getBooleanEnvVar, isBrowser } from "../utils/env_aware_utils.js";
7
12
  import { logger } from "../utils/logger.js";
8
13
  import { GoogleLLMVariant } from "../utils/variant_utils.js";
9
14
  import { BaseLlm } from "./base_llm.js";
10
15
  import { GeminiLlmConnection } from "./gemini_llm_connection.js";
11
16
  import { createLlmResponse } from "./llm_response.js";
12
- const AGENT_ENGINE_TELEMETRY_TAG = "remote_reasoning_engine";
13
- const AGENT_ENGINE_TELEMETRY_ENV_VARIABLE_NAME = "GOOGLE_CLOUD_AGENT_ENGINE_ID";
14
17
  class Gemini extends BaseLlm {
15
18
  /**
16
19
  * @param params The parameters for creating a Gemini instance.
@@ -27,45 +30,23 @@ class Gemini extends BaseLlm {
27
30
  model = "gemini-2.5-flash";
28
31
  }
29
32
  super({ model });
30
- this.project = project;
31
- this.location = location;
32
- this.apiKey = apiKey;
33
- this.headers = headers;
34
- const canReadEnv = typeof process === "object";
35
- this.vertexai = !!vertexai;
36
- if (!this.vertexai && canReadEnv) {
37
- const vertexAIfromEnv = process.env["GOOGLE_GENAI_USE_VERTEXAI"];
38
- if (vertexAIfromEnv) {
39
- this.vertexai = vertexAIfromEnv.toLowerCase() === "true" || vertexAIfromEnv === "1";
40
- }
41
- }
42
- if (this.vertexai) {
43
- if (canReadEnv && !this.project) {
44
- this.project = process.env["GOOGLE_CLOUD_PROJECT"];
45
- }
46
- if (canReadEnv && !this.location) {
47
- this.location = process.env["GOOGLE_CLOUD_LOCATION"];
48
- }
49
- if (!this.project) {
50
- throw new Error(
51
- "VertexAI project must be provided via constructor or GOOGLE_CLOUD_PROJECT environment variable."
52
- );
53
- }
54
- if (!this.location) {
55
- throw new Error(
56
- "VertexAI location must be provided via constructor or GOOGLE_CLOUD_LOCATION environment variable."
57
- );
58
- }
59
- } else {
60
- if (!this.apiKey && canReadEnv) {
61
- this.apiKey = process.env["GOOGLE_GENAI_API_KEY"] || process.env["GEMINI_API_KEY"];
62
- }
63
- if (!this.apiKey) {
64
- throw new Error(
65
- "API key must be provided via constructor or GOOGLE_GENAI_API_KEY or GEMINI_API_KEY environment variable."
66
- );
67
- }
33
+ const params = geminiInitParams({
34
+ model,
35
+ vertexai,
36
+ project,
37
+ location,
38
+ apiKey
39
+ });
40
+ if (!params.vertexai && !params.apiKey) {
41
+ throw new Error(
42
+ "API key must be provided via constructor or GOOGLE_GENAI_API_KEY or GEMINI_API_KEY environment variable."
43
+ );
68
44
  }
45
+ this.project = params.project;
46
+ this.location = params.location;
47
+ this.apiKey = params.apiKey;
48
+ this.headers = headers;
49
+ this.vertexai = !!params.vertexai;
69
50
  }
70
51
  /**
71
52
  * Sends a request to the Gemini model.
@@ -154,25 +135,24 @@ class Gemini extends BaseLlm {
154
135
  yield createLlmResponse(response);
155
136
  }
156
137
  }
138
+ getHttpOptions() {
139
+ return { headers: { ...this.trackingHeaders, ...this.headers } };
140
+ }
157
141
  get apiClient() {
158
142
  if (this._apiClient) {
159
143
  return this._apiClient;
160
144
  }
161
- const combinedHeaders = {
162
- ...this.trackingHeaders,
163
- ...this.headers
164
- };
165
145
  if (this.vertexai) {
166
146
  this._apiClient = new GoogleGenAI({
167
147
  vertexai: this.vertexai,
168
148
  project: this.project,
169
149
  location: this.location,
170
- httpOptions: { headers: combinedHeaders }
150
+ httpOptions: this.getHttpOptions()
171
151
  });
172
152
  } else {
173
153
  this._apiClient = new GoogleGenAI({
174
154
  apiKey: this.apiKey,
175
- httpOptions: { headers: combinedHeaders }
155
+ httpOptions: this.getHttpOptions()
176
156
  });
177
157
  }
178
158
  return this._apiClient;
@@ -189,14 +169,17 @@ class Gemini extends BaseLlm {
189
169
  }
190
170
  return this._liveApiVersion;
191
171
  }
172
+ getLiveHttpOptions() {
173
+ return {
174
+ headers: this.trackingHeaders,
175
+ apiVersion: this.liveApiVersion
176
+ };
177
+ }
192
178
  get liveApiClient() {
193
179
  if (!this._liveApiClient) {
194
180
  this._liveApiClient = new GoogleGenAI({
195
181
  apiKey: this.apiKey,
196
- httpOptions: {
197
- headers: this.trackingHeaders,
198
- apiVersion: this.liveApiVersion
199
- }
182
+ httpOptions: this.getLiveHttpOptions()
200
183
  });
201
184
  }
202
185
  return this._liveApiClient;
@@ -223,7 +206,9 @@ class Gemini extends BaseLlm {
223
206
  llmRequest.liveConnectConfig.systemInstruction = {
224
207
  role: "system",
225
208
  // TODO - b/425992518: validate type casting works well.
226
- parts: [createPartFromText(llmRequest.config.systemInstruction)]
209
+ parts: [
210
+ createPartFromText(llmRequest.config.systemInstruction)
211
+ ]
227
212
  };
228
213
  }
229
214
  llmRequest.liveConnectConfig.tools = (_c = llmRequest.config) == null ? void 0 : _c.tools;
@@ -272,6 +257,43 @@ function removeDisplayNameIfPresent(dataObj) {
272
257
  dataObj.displayName = void 0;
273
258
  }
274
259
  }
260
+ function geminiInitParams({
261
+ model,
262
+ vertexai,
263
+ project,
264
+ location,
265
+ apiKey
266
+ }) {
267
+ const params = { model, vertexai, project, location, apiKey };
268
+ params.vertexai = !!vertexai;
269
+ if (!params.vertexai && !isBrowser()) {
270
+ params.vertexai = getBooleanEnvVar("GOOGLE_GENAI_USE_VERTEXAI");
271
+ }
272
+ if (params.vertexai) {
273
+ if (!isBrowser() && !params.project) {
274
+ params.project = process.env["GOOGLE_CLOUD_PROJECT"];
275
+ }
276
+ if (!isBrowser() && !params.location) {
277
+ params.location = process.env["GOOGLE_CLOUD_LOCATION"];
278
+ }
279
+ if (!params.project) {
280
+ throw new Error(
281
+ "VertexAI project must be provided via constructor or GOOGLE_CLOUD_PROJECT environment variable."
282
+ );
283
+ }
284
+ if (!params.location) {
285
+ throw new Error(
286
+ "VertexAI location must be provided via constructor or GOOGLE_CLOUD_LOCATION environment variable."
287
+ );
288
+ }
289
+ } else {
290
+ if (!params.apiKey && !isBrowser()) {
291
+ params.apiKey = process.env["GOOGLE_GENAI_API_KEY"] || process.env["GEMINI_API_KEY"];
292
+ }
293
+ }
294
+ return params;
295
+ }
275
296
  export {
276
- Gemini
297
+ Gemini,
298
+ geminiInitParams
277
299
  };
@@ -4,6 +4,7 @@
4
4
  * SPDX-License-Identifier: Apache-2.0
5
5
  */
6
6
  import { logger } from "../utils/logger.js";
7
+ import { ApigeeLlm } from "./apigee_llm.js";
7
8
  import { Gemini } from "./google_llm.js";
8
9
  class LRUCache {
9
10
  constructor(maxSize) {
@@ -86,6 +87,7 @@ _LLMRegistry.llmRegistryDict = /* @__PURE__ */ new Map();
86
87
  _LLMRegistry.resolveCache = new LRUCache(32);
87
88
  let LLMRegistry = _LLMRegistry;
88
89
  LLMRegistry.register(Gemini);
90
+ LLMRegistry.register(ApigeeLlm);
89
91
  export {
90
92
  LLMRegistry
91
93
  };
@@ -25,6 +25,7 @@ class BasePlugin {
25
25
  * value to replace the user message. Returning `undefined` to proceed
26
26
  * normally.
27
27
  */
28
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
28
29
  async onUserMessageCallback(params) {
29
30
  return;
30
31
  }
@@ -40,6 +41,7 @@ class BasePlugin {
40
41
  * to halt execution of the runner and ends the runner with that event.
41
42
  * Return `undefined` to proceed normally.
42
43
  */
44
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
43
45
  async beforeRunCallback(params) {
44
46
  return;
45
47
  }
@@ -55,6 +57,7 @@ class BasePlugin {
55
57
  * framework to modify or replace the response. Returning `undefined`
56
58
  * allows the original response to be used.
57
59
  */
60
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
58
61
  async onEventCallback(params) {
59
62
  return;
60
63
  }
@@ -67,6 +70,7 @@ class BasePlugin {
67
70
  * @param params.invocationContext The context for the entire invocation.
68
71
  * @returns undefined
69
72
  */
73
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
70
74
  async afterRunCallback(params) {
71
75
  return;
72
76
  }
@@ -82,6 +86,7 @@ class BasePlugin {
82
86
  * bypass the agent's callbacks and its execution, and return this value
83
87
  * directly. Returning `undefined` allows the agent to proceed normally.
84
88
  */
89
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
85
90
  async beforeAgentCallback(params) {
86
91
  return;
87
92
  }
@@ -97,6 +102,7 @@ class BasePlugin {
97
102
  * replace the agent's original result. Returning `undefined` uses the
98
103
  * original, unmodified result.
99
104
  */
105
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
100
106
  async afterAgentCallback(params) {
101
107
  return;
102
108
  }
@@ -113,6 +119,7 @@ class BasePlugin {
113
119
  * trigger an early exit and returns the response immediately. Returning
114
120
  * `undefined` allows the LLM request to proceed normally.
115
121
  */
122
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
116
123
  async beforeModelCallback(params) {
117
124
  return;
118
125
  }
@@ -128,6 +135,7 @@ class BasePlugin {
128
135
  * framework to modify or replace the response. Returning `undefined`
129
136
  * allows the original response to be used.
130
137
  */
138
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
131
139
  async afterModelCallback(params) {
132
140
  return;
133
141
  }
@@ -145,6 +153,7 @@ class BasePlugin {
145
153
  * used instead of propagating the error. Returning `undefined` allows
146
154
  * the original error to be raised.
147
155
  */
156
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
148
157
  async onModelErrorCallback(params) {
149
158
  return;
150
159
  }
@@ -162,6 +171,7 @@ class BasePlugin {
162
171
  * the tool execution and return this response immediately. Returning
163
172
  * `undefined` uses the original, unmodified arguments.
164
173
  */
174
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
165
175
  async beforeToolCallback(params) {
166
176
  return;
167
177
  }
@@ -180,6 +190,7 @@ class BasePlugin {
180
190
  * post-processing or altering tool outputs. Returning `undefined` uses
181
191
  * the original, unmodified result.
182
192
  */
193
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
183
194
  async afterToolCallback(params) {
184
195
  return;
185
196
  }
@@ -207,6 +218,7 @@ class BasePlugin {
207
218
  * used as the tool response instead of propagating the error. Returning
208
219
  * `undefined` allows the original error to be raised.
209
220
  */
221
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
210
222
  async onToolErrorCallback(params) {
211
223
  return;
212
224
  }
@@ -3,7 +3,11 @@
3
3
  * Copyright 2025 Google LLC
4
4
  * SPDX-License-Identifier: Apache-2.0
5
5
  */
6
- import { getFunctionCalls, getFunctionResponses, isFinalResponse } from "../events/event.js";
6
+ import {
7
+ getFunctionCalls,
8
+ getFunctionResponses,
9
+ isFinalResponse
10
+ } from "../events/event.js";
7
11
  import { logger } from "../utils/logger.js";
8
12
  import { BasePlugin } from "./base_plugin.js";
9
13
  class LoggingPlugin extends BasePlugin {
@@ -15,7 +19,10 @@ class LoggingPlugin extends BasePlugin {
15
19
  constructor(name = "logging_plugin") {
16
20
  super(name);
17
21
  }
18
- async onUserMessageCallback({ invocationContext, userMessage }) {
22
+ async onUserMessageCallback({
23
+ invocationContext,
24
+ userMessage
25
+ }) {
19
26
  var _a;
20
27
  this.log("\u{1F680} USER MESSAGE RECEIVED");
21
28
  this.log(` Invocation ID: ${invocationContext.invocationId}`);
@@ -29,14 +36,18 @@ class LoggingPlugin extends BasePlugin {
29
36
  }
30
37
  return void 0;
31
38
  }
32
- async beforeRunCallback({ invocationContext }) {
39
+ async beforeRunCallback({
40
+ invocationContext
41
+ }) {
33
42
  var _a;
34
43
  this.log("\u{1F3C3} INVOCATION STARTING");
35
44
  this.log(` Invocation ID: ${invocationContext.invocationId}`);
36
45
  this.log(` Starting Agent: ${(_a = invocationContext.agent.name) != null ? _a : "Unknown"}`);
37
46
  return void 0;
38
47
  }
39
- async onEventCallback({ invocationContext, event }) {
48
+ async onEventCallback({
49
+ event
50
+ }) {
40
51
  this.log("\u{1F4E2} EVENT YIELDED");
41
52
  this.log(` Event ID: ${event.id}`);
42
53
  this.log(` Author: ${event.author}`);
@@ -57,14 +68,18 @@ class LoggingPlugin extends BasePlugin {
57
68
  }
58
69
  return void 0;
59
70
  }
60
- async afterRunCallback({ invocationContext }) {
71
+ async afterRunCallback({
72
+ invocationContext
73
+ }) {
61
74
  var _a;
62
75
  this.log("\u2705 INVOCATION COMPLETED");
63
76
  this.log(` Invocation ID: ${invocationContext.invocationId}`);
64
77
  this.log(` Final Agent: ${(_a = invocationContext.agent.name) != null ? _a : "Unknown"}`);
65
78
  return void 0;
66
79
  }
67
- async beforeAgentCallback({ agent, callbackContext }) {
80
+ async beforeAgentCallback({
81
+ callbackContext
82
+ }) {
68
83
  this.log("\u{1F916} AGENT STARTING");
69
84
  this.log(` Agent Name: ${callbackContext.agentName}`);
70
85
  this.log(` Invocation ID: ${callbackContext.invocationId}`);
@@ -73,13 +88,18 @@ class LoggingPlugin extends BasePlugin {
73
88
  }
74
89
  return void 0;
75
90
  }
76
- async afterAgentCallback({ agent, callbackContext }) {
91
+ async afterAgentCallback({
92
+ callbackContext
93
+ }) {
77
94
  this.log("\u{1F916} AGENT COMPLETED");
78
95
  this.log(` Agent Name: ${callbackContext.agentName}`);
79
96
  this.log(` Invocation ID: ${callbackContext.invocationId}`);
80
97
  return void 0;
81
98
  }
82
- async beforeModelCallback({ callbackContext, llmRequest }) {
99
+ async beforeModelCallback({
100
+ callbackContext,
101
+ llmRequest
102
+ }) {
83
103
  var _a;
84
104
  this.log("\u{1F9E0} LLM REQUEST");
85
105
  this.log(` Model: ${(_a = llmRequest.model) != null ? _a : "default"}`);
@@ -97,7 +117,10 @@ class LoggingPlugin extends BasePlugin {
97
117
  }
98
118
  return void 0;
99
119
  }
100
- async afterModelCallback({ callbackContext, llmResponse }) {
120
+ async afterModelCallback({
121
+ callbackContext,
122
+ llmResponse
123
+ }) {
101
124
  this.log("\u{1F9E0} LLM RESPONSE");
102
125
  this.log(` Agent: ${callbackContext.agentName}`);
103
126
  if (llmResponse.errorCode) {
@@ -113,11 +136,17 @@ class LoggingPlugin extends BasePlugin {
113
136
  }
114
137
  }
115
138
  if (llmResponse.usageMetadata) {
116
- this.log(` Token Usage - Input: ${llmResponse.usageMetadata.promptTokenCount}, Output: ${llmResponse.usageMetadata.candidatesTokenCount}`);
139
+ this.log(
140
+ ` Token Usage - Input: ${llmResponse.usageMetadata.promptTokenCount}, Output: ${llmResponse.usageMetadata.candidatesTokenCount}`
141
+ );
117
142
  }
118
143
  return void 0;
119
144
  }
120
- async beforeToolCallback({ tool, toolArgs, toolContext }) {
145
+ async beforeToolCallback({
146
+ tool,
147
+ toolArgs,
148
+ toolContext
149
+ }) {
121
150
  this.log("\u{1F527} TOOL STARTING");
122
151
  this.log(` Tool Name: ${tool.name}`);
123
152
  this.log(` Agent: ${toolContext.agentName}`);
@@ -125,7 +154,11 @@ class LoggingPlugin extends BasePlugin {
125
154
  this.log(` Arguments: ${this.formatArgs(toolArgs)}`);
126
155
  return void 0;
127
156
  }
128
- async afterToolCallback({ tool, toolArgs, toolContext, result }) {
157
+ async afterToolCallback({
158
+ tool,
159
+ toolContext,
160
+ result
161
+ }) {
129
162
  this.log("\u{1F527} TOOL COMPLETED");
130
163
  this.log(` Tool Name: ${tool.name}`);
131
164
  this.log(` Agent: ${toolContext.agentName}`);
@@ -133,13 +166,21 @@ class LoggingPlugin extends BasePlugin {
133
166
  this.log(` Result: ${this.formatArgs(result)}`);
134
167
  return void 0;
135
168
  }
136
- async onModelErrorCallback({ callbackContext, llmRequest, error }) {
169
+ async onModelErrorCallback({
170
+ callbackContext,
171
+ error
172
+ }) {
137
173
  this.log("\u{1F9E0} LLM ERROR");
138
174
  this.log(` Agent: ${callbackContext.agentName}`);
139
175
  this.log(` Error: ${error}`);
140
176
  return void 0;
141
177
  }
142
- async onToolErrorCallback({ tool, toolArgs, toolContext, error }) {
178
+ async onToolErrorCallback({
179
+ tool,
180
+ toolArgs,
181
+ toolContext,
182
+ error
183
+ }) {
143
184
  this.log("\u{1F527} TOOL ERROR");
144
185
  this.log(` Tool Name: ${tool.name}`);
145
186
  this.log(` Agent: ${toolContext.agentName}`);
@@ -78,19 +78,22 @@ class PluginManager {
78
78
  /**
79
79
  * Runs the `onUserMessageCallback` for all plugins.
80
80
  */
81
- async runOnUserMessageCallback({ userMessage, invocationContext }) {
81
+ async runOnUserMessageCallback({
82
+ userMessage,
83
+ invocationContext
84
+ }) {
82
85
  return await this.runCallbacks(
83
86
  this.plugins,
84
- (plugin) => plugin.onUserMessageCallback(
85
- { userMessage, invocationContext }
86
- ),
87
+ (plugin) => plugin.onUserMessageCallback({ userMessage, invocationContext }),
87
88
  "onUserMessageCallback"
88
89
  );
89
90
  }
90
91
  /**
91
92
  * Runs the `beforeRunCallback` for all plugins.
92
93
  */
93
- async runBeforeRunCallback({ invocationContext }) {
94
+ async runBeforeRunCallback({
95
+ invocationContext
96
+ }) {
94
97
  return await this.runCallbacks(
95
98
  this.plugins,
96
99
  (plugin) => plugin.beforeRunCallback({ invocationContext }),
@@ -100,7 +103,9 @@ class PluginManager {
100
103
  /**
101
104
  * Runs the `afterRunCallback` for all plugins.
102
105
  */
103
- async runAfterRunCallback({ invocationContext }) {
106
+ async runAfterRunCallback({
107
+ invocationContext
108
+ }) {
104
109
  await this.runCallbacks(
105
110
  this.plugins,
106
111
  (plugin) => plugin.afterRunCallback({ invocationContext }),
@@ -110,7 +115,10 @@ class PluginManager {
110
115
  /**
111
116
  * Runs the `onEventCallback` for all plugins.
112
117
  */
113
- async runOnEventCallback({ invocationContext, event }) {
118
+ async runOnEventCallback({
119
+ invocationContext,
120
+ event
121
+ }) {
114
122
  return await this.runCallbacks(
115
123
  this.plugins,
116
124
  (plugin) => plugin.onEventCallback({ invocationContext, event }),
@@ -120,7 +128,10 @@ class PluginManager {
120
128
  /**
121
129
  * Runs the `beforeAgentCallback` for all plugins.
122
130
  */
123
- async runBeforeAgentCallback({ agent, callbackContext }) {
131
+ async runBeforeAgentCallback({
132
+ agent,
133
+ callbackContext
134
+ }) {
124
135
  return await this.runCallbacks(
125
136
  this.plugins,
126
137
  (plugin) => plugin.beforeAgentCallback({ agent, callbackContext }),
@@ -130,7 +141,10 @@ class PluginManager {
130
141
  /**
131
142
  * Runs the `afterAgentCallback` for all plugins.
132
143
  */
133
- async runAfterAgentCallback({ agent, callbackContext }) {
144
+ async runAfterAgentCallback({
145
+ agent,
146
+ callbackContext
147
+ }) {
134
148
  return await this.runCallbacks(
135
149
  this.plugins,
136
150
  (plugin) => plugin.afterAgentCallback({ agent, callbackContext }),
@@ -140,7 +154,11 @@ class PluginManager {
140
154
  /**
141
155
  * Runs the `beforeToolCallback` for all plugins.
142
156
  */
143
- async runBeforeToolCallback({ tool, toolArgs, toolContext }) {
157
+ async runBeforeToolCallback({
158
+ tool,
159
+ toolArgs,
160
+ toolContext
161
+ }) {
144
162
  return await this.runCallbacks(
145
163
  this.plugins,
146
164
  (plugin) => plugin.beforeToolCallback({ tool, toolArgs, toolContext }),
@@ -150,31 +168,39 @@ class PluginManager {
150
168
  /**
151
169
  * Runs the `afterToolCallback` for all plugins.
152
170
  */
153
- async runAfterToolCallback({ tool, toolArgs, toolContext, result }) {
171
+ async runAfterToolCallback({
172
+ tool,
173
+ toolArgs,
174
+ toolContext,
175
+ result
176
+ }) {
154
177
  return await this.runCallbacks(
155
178
  this.plugins,
156
- (plugin) => plugin.afterToolCallback(
157
- { tool, toolArgs, toolContext, result }
158
- ),
179
+ (plugin) => plugin.afterToolCallback({ tool, toolArgs, toolContext, result }),
159
180
  "afterToolCallback"
160
181
  );
161
182
  }
162
183
  /**
163
184
  * Runs the `onModelErrorCallback` for all plugins.
164
185
  */
165
- async runOnModelErrorCallback({ callbackContext, llmRequest, error }) {
186
+ async runOnModelErrorCallback({
187
+ callbackContext,
188
+ llmRequest,
189
+ error
190
+ }) {
166
191
  return await this.runCallbacks(
167
192
  this.plugins,
168
- (plugin) => plugin.onModelErrorCallback(
169
- { callbackContext, llmRequest, error }
170
- ),
193
+ (plugin) => plugin.onModelErrorCallback({ callbackContext, llmRequest, error }),
171
194
  "onModelErrorCallback"
172
195
  );
173
196
  }
174
197
  /**
175
198
  * Runs the `beforeModelCallback` for all plugins.
176
199
  */
177
- async runBeforeModelCallback({ callbackContext, llmRequest }) {
200
+ async runBeforeModelCallback({
201
+ callbackContext,
202
+ llmRequest
203
+ }) {
178
204
  return await this.runCallbacks(
179
205
  this.plugins,
180
206
  (plugin) => plugin.beforeModelCallback({ callbackContext, llmRequest }),
@@ -184,7 +210,10 @@ class PluginManager {
184
210
  /**
185
211
  * Runs the `afterModelCallback` for all plugins.
186
212
  */
187
- async runAfterModelCallback({ callbackContext, llmResponse }) {
213
+ async runAfterModelCallback({
214
+ callbackContext,
215
+ llmResponse
216
+ }) {
188
217
  return await this.runCallbacks(
189
218
  this.plugins,
190
219
  (plugin) => plugin.afterModelCallback({ callbackContext, llmResponse }),
@@ -194,12 +223,15 @@ class PluginManager {
194
223
  /**
195
224
  * Runs the `onToolErrorCallback` for all plugins.
196
225
  */
197
- async runOnToolErrorCallback({ tool, toolArgs, toolContext, error }) {
226
+ async runOnToolErrorCallback({
227
+ tool,
228
+ toolArgs,
229
+ toolContext,
230
+ error
231
+ }) {
198
232
  return await this.runCallbacks(
199
233
  this.plugins,
200
- (plugin) => plugin.onToolErrorCallback(
201
- { tool, toolArgs, toolContext, error }
202
- ),
234
+ (plugin) => plugin.onToolErrorCallback({ tool, toolArgs, toolContext, error }),
203
235
  "onToolErrorCallback"
204
236
  );
205
237
  }
@@ -14,7 +14,7 @@ var PolicyOutcome = /* @__PURE__ */ ((PolicyOutcome2) => {
14
14
  return PolicyOutcome2;
15
15
  })(PolicyOutcome || {});
16
16
  class InMemoryPolicyEngine {
17
- async evaluate(context) {
17
+ async evaluate() {
18
18
  return Promise.resolve({
19
19
  outcome: "ALLOW" /* ALLOW */,
20
20
  reason: "For prototyping purpose, all tool calls are allowed."