@google/adk 0.3.0 → 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 (183) 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 +3 -3
  4. package/dist/cjs/agents/base_llm_processor.js +1 -1
  5. package/dist/cjs/agents/callback_context.js +1 -1
  6. package/dist/cjs/agents/content_processor_utils.js +1 -1
  7. package/dist/cjs/agents/functions.js +2 -1
  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 +58 -40
  12. package/dist/cjs/agents/loop_agent.js +1 -1
  13. package/dist/cjs/agents/parallel_agent.js +1 -1
  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 +127 -48
  21. package/dist/cjs/artifacts/in_memory_artifact_service.js +54 -6
  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 +1 -1
  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 +1 -1
  32. package/dist/cjs/code_executors/built_in_code_executor.js +1 -1
  33. package/dist/cjs/code_executors/code_execution_utils.js +1 -1
  34. package/dist/cjs/code_executors/code_executor_context.js +1 -1
  35. package/dist/cjs/common.js +14 -1
  36. package/dist/cjs/events/event.js +33 -4
  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 +1 -1
  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 +1 -1
  49. package/dist/cjs/models/base_llm_connection.js +1 -1
  50. package/dist/cjs/models/gemini_llm_connection.js +1 -1
  51. package/dist/cjs/models/google_llm.js +70 -51
  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 +1 -1
  56. package/dist/cjs/plugins/logging_plugin.js +1 -1
  57. package/dist/cjs/plugins/plugin_manager.js +1 -1
  58. package/dist/cjs/plugins/security_plugin.js +1 -1
  59. package/dist/cjs/runner/in_memory_runner.js +1 -1
  60. package/dist/cjs/runner/runner.js +32 -1
  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 +24 -22
  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 +1 -1
  70. package/dist/cjs/telemetry/setup.js +1 -1
  71. package/dist/cjs/telemetry/tracing.js +1 -1
  72. package/dist/cjs/tools/agent_tool.js +1 -1
  73. package/dist/cjs/tools/base_tool.js +1 -1
  74. package/dist/cjs/tools/base_toolset.js +1 -1
  75. package/dist/cjs/tools/forwarding_artifact_service.js +17 -1
  76. package/dist/cjs/tools/function_tool.js +1 -1
  77. package/dist/cjs/tools/google_search_tool.js +1 -1
  78. package/dist/cjs/tools/long_running_tool.js +1 -1
  79. package/dist/cjs/tools/mcp/mcp_session_manager.js +1 -1
  80. package/dist/cjs/tools/mcp/mcp_tool.js +1 -1
  81. package/dist/cjs/tools/mcp/mcp_toolset.js +1 -1
  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 +10 -1
  86. package/dist/cjs/utils/gemini_schema_util.js +1 -1
  87. package/dist/cjs/utils/logger.js +1 -1
  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 +1 -1
  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 +2 -2
  95. package/dist/esm/agents/functions.js +1 -0
  96. package/dist/esm/agents/llm_agent.js +58 -40
  97. package/dist/esm/agents/readonly_context.js +12 -0
  98. package/dist/esm/agents/run_config.js +1 -0
  99. package/dist/esm/artifacts/file_artifact_service.js +451 -0
  100. package/dist/esm/artifacts/gcs_artifact_service.js +126 -47
  101. package/dist/esm/artifacts/in_memory_artifact_service.js +51 -4
  102. package/dist/esm/artifacts/registry.js +28 -0
  103. package/dist/esm/common.js +9 -1
  104. package/dist/esm/events/event.js +29 -2
  105. package/dist/esm/events/event_actions.js +1 -1
  106. package/dist/esm/events/structured_events.js +74 -0
  107. package/dist/esm/index.js +18 -88
  108. package/dist/esm/models/apigee_llm.js +152 -0
  109. package/dist/esm/models/google_llm.js +67 -49
  110. package/dist/esm/models/registry.js +2 -0
  111. package/dist/esm/runner/runner.js +31 -0
  112. package/dist/esm/sessions/base_session_service.js +49 -1
  113. package/dist/esm/sessions/database_session_service.js +350 -0
  114. package/dist/esm/sessions/db/operations.js +87 -0
  115. package/dist/esm/sessions/db/schema.js +172 -0
  116. package/dist/esm/sessions/in_memory_session_service.js +23 -21
  117. package/dist/esm/sessions/registry.js +25 -0
  118. package/dist/esm/tools/forwarding_artifact_service.js +16 -0
  119. package/dist/esm/utils/env_aware_utils.js +8 -0
  120. package/dist/esm/utils/object_notation_utils.js +47 -0
  121. package/dist/esm/utils/variant_utils.js +1 -7
  122. package/dist/esm/version.js +1 -1
  123. package/dist/types/a2a/part_converter_utils.d.ts +47 -0
  124. package/dist/types/agents/llm_agent.d.ts +11 -11
  125. package/dist/types/agents/readonly_context.d.ts +8 -0
  126. package/dist/types/agents/run_config.d.ts +6 -0
  127. package/dist/types/artifacts/base_artifact_service.d.ts +31 -0
  128. package/dist/types/artifacts/file_artifact_service.d.ts +43 -0
  129. package/dist/types/artifacts/gcs_artifact_service.d.ts +3 -1
  130. package/dist/types/artifacts/in_memory_artifact_service.d.ts +5 -2
  131. package/dist/types/artifacts/registry.d.ts +7 -0
  132. package/dist/types/common.d.ts +11 -2
  133. package/dist/types/events/event.d.ts +15 -1
  134. package/dist/types/events/event_actions.d.ts +1 -1
  135. package/dist/types/events/structured_events.d.ts +106 -0
  136. package/dist/types/index.d.ts +5 -1
  137. package/dist/types/models/apigee_llm.d.ts +59 -0
  138. package/dist/types/models/google_llm.d.ts +5 -2
  139. package/dist/types/runner/runner.d.ts +15 -0
  140. package/dist/types/sessions/base_session_service.d.ts +20 -0
  141. package/dist/types/sessions/database_session_service.d.ts +31 -0
  142. package/dist/types/sessions/db/operations.d.ts +29 -0
  143. package/dist/types/sessions/db/schema.d.ts +45 -0
  144. package/dist/types/sessions/in_memory_session_service.d.ts +4 -1
  145. package/dist/types/sessions/registry.d.ts +7 -0
  146. package/dist/types/tools/forwarding_artifact_service.d.ts +3 -1
  147. package/dist/types/utils/env_aware_utils.d.ts +7 -0
  148. package/dist/types/utils/object_notation_utils.d.ts +21 -0
  149. package/dist/types/version.d.ts +1 -1
  150. package/dist/web/a2a/part_converter_utils.js +171 -0
  151. package/dist/web/agents/base_agent.js +2 -2
  152. package/dist/web/agents/functions.js +1 -0
  153. package/dist/web/agents/llm_agent.js +79 -59
  154. package/dist/web/agents/readonly_context.js +12 -0
  155. package/dist/web/agents/run_config.js +2 -1
  156. package/dist/web/artifacts/file_artifact_service.js +506 -0
  157. package/dist/web/artifacts/gcs_artifact_service.js +123 -46
  158. package/dist/web/artifacts/in_memory_artifact_service.js +51 -4
  159. package/dist/web/artifacts/registry.js +28 -0
  160. package/dist/web/common.js +9 -1
  161. package/dist/web/events/event.js +29 -2
  162. package/dist/web/events/event_actions.js +1 -1
  163. package/dist/web/events/structured_events.js +74 -0
  164. package/dist/web/index.js +18 -8
  165. package/dist/web/models/apigee_llm.js +219 -0
  166. package/dist/web/models/google_llm.js +67 -46
  167. package/dist/web/models/registry.js +2 -0
  168. package/dist/web/runner/runner.js +33 -0
  169. package/dist/web/sessions/base_session_service.js +49 -1
  170. package/dist/web/sessions/database_session_service.js +368 -0
  171. package/dist/web/sessions/db/operations.js +87 -0
  172. package/dist/web/sessions/db/schema.js +172 -0
  173. package/dist/web/sessions/in_memory_session_service.js +23 -21
  174. package/dist/web/sessions/registry.js +25 -0
  175. package/dist/web/tools/forwarding_artifact_service.js +16 -0
  176. package/dist/web/utils/env_aware_utils.js +8 -0
  177. package/dist/web/utils/object_notation_utils.js +47 -0
  178. package/dist/web/utils/variant_utils.js +1 -7
  179. package/dist/web/version.js +1 -1
  180. package/package.json +13 -4
  181. package/dist/cjs/index.js.map +0 -7
  182. package/dist/esm/index.js.map +0 -7
  183. package/dist/web/index.js.map +0 -7
@@ -0,0 +1,152 @@
1
+ /**
2
+ * @license
3
+ * Copyright 2026 Google LLC
4
+ * SPDX-License-Identifier: Apache-2.0
5
+ */
6
+ import { isBrowser } from "../utils/env_aware_utils.js";
7
+ import { logger } from "../utils/logger.js";
8
+ import { Gemini, geminiInitParams } from "./google_llm.js";
9
+ const APIGEE_PROXY_URL_ENV_VARIABLE_NAME = "APIGEE_PROXY_URL";
10
+ class ApigeeLlm extends Gemini {
11
+ constructor({
12
+ model,
13
+ proxyUrl,
14
+ apiKey,
15
+ vertexai,
16
+ location,
17
+ project,
18
+ headers
19
+ }) {
20
+ var _a;
21
+ if (!validateModel(model)) {
22
+ throw new Error(
23
+ `Model ${model} is not a valid Apigee model, expected apigee/[<provider>/][<version>/]<model_id>`
24
+ );
25
+ }
26
+ super({
27
+ ...apigeeToGeminiInitParams({ model, vertexai, project, location, apiKey }),
28
+ headers
29
+ });
30
+ this.proxyUrl = proxyUrl != null ? proxyUrl : "";
31
+ if (!isBrowser() && !this.proxyUrl) {
32
+ this.proxyUrl = (_a = process.env[APIGEE_PROXY_URL_ENV_VARIABLE_NAME]) != null ? _a : "";
33
+ }
34
+ if (!this.proxyUrl) {
35
+ throw new Error(
36
+ `Proxy URL must be provided via the constructor or ${APIGEE_PROXY_URL_ENV_VARIABLE_NAME} environment variable.`
37
+ );
38
+ }
39
+ }
40
+ getHttpOptions() {
41
+ const opts = super.getHttpOptions();
42
+ opts.baseUrl = this.proxyUrl;
43
+ return opts;
44
+ }
45
+ getLiveHttpOptions() {
46
+ const opts = super.getLiveHttpOptions();
47
+ opts.baseUrl = this.proxyUrl;
48
+ return opts;
49
+ }
50
+ identifyApiVersion() {
51
+ const modelTrimmed = this.model.startsWith("apigee/") ? this.model.substring("apigee/".length) : this.model;
52
+ const components = modelTrimmed.split("/");
53
+ if (components.length === 3) {
54
+ return components[1];
55
+ }
56
+ if (components.length === 2) {
57
+ if (components[0] != "vertex_ai" && components[0] != "gemini" && components[0].startsWith("v")) {
58
+ return components[0];
59
+ }
60
+ }
61
+ return this.vertexai ? "v1beta1" : "v1alpha";
62
+ }
63
+ get liveApiVersion() {
64
+ if (!this._apigeeLiveApiVersion) {
65
+ this._apigeeLiveApiVersion = this.identifyApiVersion();
66
+ }
67
+ return this._apigeeLiveApiVersion;
68
+ }
69
+ async *generateContentAsync(llmRequest, stream = false) {
70
+ var _a;
71
+ const modelToUse = (_a = llmRequest.model) != null ? _a : this.model;
72
+ llmRequest.model = getModelId(modelToUse);
73
+ yield* super.generateContentAsync(llmRequest, stream);
74
+ }
75
+ async connect(llmRequest) {
76
+ var _a;
77
+ const modelToUse = (_a = llmRequest.model) != null ? _a : this.model;
78
+ llmRequest.model = getModelId(modelToUse);
79
+ return super.connect(llmRequest);
80
+ }
81
+ }
82
+ /**
83
+ * A list of model name patterns that are supported by this LLM.
84
+ *
85
+ * @returns A list of supported models.
86
+ */
87
+ ApigeeLlm.supportedModels = [
88
+ /apigee\/.*/
89
+ ];
90
+ function apigeeToGeminiInitParams({
91
+ model,
92
+ vertexai,
93
+ project,
94
+ location,
95
+ apiKey
96
+ }) {
97
+ var _a;
98
+ const params = geminiInitParams({ model, vertexai, project, location, apiKey });
99
+ params.vertexai = params.vertexai || ((_a = params.model) == null ? void 0 : _a.startsWith("apigee/vertex_ai/"));
100
+ if (params.vertexai) {
101
+ return params;
102
+ }
103
+ if (!params.apiKey) {
104
+ logger.warn(
105
+ `No API key provided when using a Gemini model, using a fake key "-".`
106
+ );
107
+ params.apiKey = "-";
108
+ }
109
+ return params;
110
+ }
111
+ function getModelId(model) {
112
+ if (!validateModel(model)) {
113
+ throw new Error(
114
+ `Model ${model} is not a valid Apigee model, expected apigee/[<provider>/][<version>/]<model_id>`
115
+ );
116
+ }
117
+ const components = model.split("/");
118
+ return components[components.length - 1];
119
+ }
120
+ function validateModel(model) {
121
+ const validProviders = ["vertex_ai", "gemini"];
122
+ if (!model.startsWith("apigee/")) {
123
+ return false;
124
+ }
125
+ const modelPart = model.substring("apigee/".length);
126
+ if (modelPart.length === 0) {
127
+ return false;
128
+ }
129
+ const components = modelPart.split("/", -1);
130
+ if (components[components.length - 1].length === 0) {
131
+ return false;
132
+ }
133
+ if (components.length == 1) {
134
+ return true;
135
+ }
136
+ if (components.length == 2) {
137
+ if (validProviders.includes(components[0])) {
138
+ return true;
139
+ }
140
+ return components[0].startsWith("v");
141
+ }
142
+ if (components.length == 3) {
143
+ if (!validProviders.includes(components[0])) {
144
+ return false;
145
+ }
146
+ return components[1].startsWith("v");
147
+ }
148
+ return false;
149
+ }
150
+ export {
151
+ ApigeeLlm
152
+ };
@@ -8,6 +8,7 @@ import {
8
8
  FinishReason,
9
9
  GoogleGenAI
10
10
  } from "@google/genai";
11
+ import { getBooleanEnvVar, isBrowser } from "../utils/env_aware_utils.js";
11
12
  import { logger } from "../utils/logger.js";
12
13
  import { GoogleLLMVariant } from "../utils/variant_utils.js";
13
14
  import { BaseLlm } from "./base_llm.js";
@@ -29,45 +30,23 @@ class Gemini extends BaseLlm {
29
30
  model = "gemini-2.5-flash";
30
31
  }
31
32
  super({ model });
32
- this.project = project;
33
- this.location = location;
34
- this.apiKey = apiKey;
35
- this.headers = headers;
36
- const canReadEnv = typeof process === "object";
37
- this.vertexai = !!vertexai;
38
- if (!this.vertexai && canReadEnv) {
39
- const vertexAIfromEnv = process.env["GOOGLE_GENAI_USE_VERTEXAI"];
40
- if (vertexAIfromEnv) {
41
- this.vertexai = vertexAIfromEnv.toLowerCase() === "true" || vertexAIfromEnv === "1";
42
- }
43
- }
44
- if (this.vertexai) {
45
- if (canReadEnv && !this.project) {
46
- this.project = process.env["GOOGLE_CLOUD_PROJECT"];
47
- }
48
- if (canReadEnv && !this.location) {
49
- this.location = process.env["GOOGLE_CLOUD_LOCATION"];
50
- }
51
- if (!this.project) {
52
- throw new Error(
53
- "VertexAI project must be provided via constructor or GOOGLE_CLOUD_PROJECT environment variable."
54
- );
55
- }
56
- if (!this.location) {
57
- throw new Error(
58
- "VertexAI location must be provided via constructor or GOOGLE_CLOUD_LOCATION environment variable."
59
- );
60
- }
61
- } else {
62
- if (!this.apiKey && canReadEnv) {
63
- this.apiKey = process.env["GOOGLE_GENAI_API_KEY"] || process.env["GEMINI_API_KEY"];
64
- }
65
- if (!this.apiKey) {
66
- throw new Error(
67
- "API key must be provided via constructor or GOOGLE_GENAI_API_KEY or GEMINI_API_KEY environment variable."
68
- );
69
- }
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
+ );
70
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;
71
50
  }
72
51
  /**
73
52
  * Sends a request to the Gemini model.
@@ -156,25 +135,24 @@ class Gemini extends BaseLlm {
156
135
  yield createLlmResponse(response);
157
136
  }
158
137
  }
138
+ getHttpOptions() {
139
+ return { headers: { ...this.trackingHeaders, ...this.headers } };
140
+ }
159
141
  get apiClient() {
160
142
  if (this._apiClient) {
161
143
  return this._apiClient;
162
144
  }
163
- const combinedHeaders = {
164
- ...this.trackingHeaders,
165
- ...this.headers
166
- };
167
145
  if (this.vertexai) {
168
146
  this._apiClient = new GoogleGenAI({
169
147
  vertexai: this.vertexai,
170
148
  project: this.project,
171
149
  location: this.location,
172
- httpOptions: { headers: combinedHeaders }
150
+ httpOptions: this.getHttpOptions()
173
151
  });
174
152
  } else {
175
153
  this._apiClient = new GoogleGenAI({
176
154
  apiKey: this.apiKey,
177
- httpOptions: { headers: combinedHeaders }
155
+ httpOptions: this.getHttpOptions()
178
156
  });
179
157
  }
180
158
  return this._apiClient;
@@ -191,14 +169,17 @@ class Gemini extends BaseLlm {
191
169
  }
192
170
  return this._liveApiVersion;
193
171
  }
172
+ getLiveHttpOptions() {
173
+ return {
174
+ headers: this.trackingHeaders,
175
+ apiVersion: this.liveApiVersion
176
+ };
177
+ }
194
178
  get liveApiClient() {
195
179
  if (!this._liveApiClient) {
196
180
  this._liveApiClient = new GoogleGenAI({
197
181
  apiKey: this.apiKey,
198
- httpOptions: {
199
- headers: this.trackingHeaders,
200
- apiVersion: this.liveApiVersion
201
- }
182
+ httpOptions: this.getLiveHttpOptions()
202
183
  });
203
184
  }
204
185
  return this._liveApiClient;
@@ -276,6 +257,43 @@ function removeDisplayNameIfPresent(dataObj) {
276
257
  dataObj.displayName = void 0;
277
258
  }
278
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
+ }
279
296
  export {
280
- Gemini
297
+ Gemini,
298
+ geminiInitParams
281
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
  };
@@ -35,6 +35,37 @@ class Runner {
35
35
  this.memoryService = input.memoryService;
36
36
  this.credentialService = input.credentialService;
37
37
  }
38
+ /**
39
+ * Runs the agent with a new, ephemeral session.
40
+ *
41
+ * @param params.userId The user ID of the session.
42
+ * @param params.newMessage A new message to append to the session.
43
+ * @param params.stateDelta An optional state delta to apply to the session.
44
+ * @param params.runConfig The run config for the agent.
45
+ * @yields The Events generated by the agent.
46
+ */
47
+ async *runEphemeral(params) {
48
+ const session = await this.sessionService.createSession({
49
+ appName: this.appName,
50
+ userId: params.userId
51
+ });
52
+ const sessionId = session.id;
53
+ try {
54
+ yield* this.runAsync({
55
+ userId: params.userId,
56
+ sessionId,
57
+ newMessage: params.newMessage,
58
+ stateDelta: params.stateDelta,
59
+ runConfig: params.runConfig
60
+ });
61
+ } finally {
62
+ await this.sessionService.deleteSession({
63
+ appName: this.appName,
64
+ userId: params.userId,
65
+ sessionId
66
+ });
67
+ }
68
+ }
38
69
  /**
39
70
  * Runs the agent with the given message, and returns an async generator of
40
71
  * events.
@@ -3,8 +3,29 @@
3
3
  * Copyright 2025 Google LLC
4
4
  * SPDX-License-Identifier: Apache-2.0
5
5
  */
6
+ import { cloneDeep } from "lodash-es";
6
7
  import { State } from "./state.js";
7
8
  class BaseSessionService {
9
+ /**
10
+ * Gets a session or creates one if it doesn't exist.
11
+ *
12
+ * @param request The request to get or create a session.
13
+ * @return A promise that resolves to the session instance.
14
+ */
15
+ async getOrCreateSession(request) {
16
+ if (!request.sessionId) {
17
+ return this.createSession(request);
18
+ }
19
+ const session = await this.getSession({
20
+ appName: request.appName,
21
+ userId: request.userId,
22
+ sessionId: request.sessionId
23
+ });
24
+ if (session) {
25
+ return session;
26
+ }
27
+ return this.createSession(request);
28
+ }
8
29
  /**
9
30
  * Appends an event to a session.
10
31
  *
@@ -15,6 +36,7 @@ class BaseSessionService {
15
36
  if (event.partial) {
16
37
  return event;
17
38
  }
39
+ event = trimTempDeltaState(event);
18
40
  this.updateSessionState({ session, event });
19
41
  session.events.push(event);
20
42
  return event;
@@ -36,6 +58,32 @@ class BaseSessionService {
36
58
  }
37
59
  }
38
60
  }
61
+ function trimTempDeltaState(event) {
62
+ if (!event.actions || !event.actions.stateDelta) {
63
+ return event;
64
+ }
65
+ const stateDelta = event.actions.stateDelta;
66
+ const filteredStateDelta = {};
67
+ for (const [key, value] of Object.entries(stateDelta)) {
68
+ if (!key.startsWith(State.TEMP_PREFIX)) {
69
+ filteredStateDelta[key] = value;
70
+ }
71
+ }
72
+ event.actions.stateDelta = filteredStateDelta;
73
+ return event;
74
+ }
75
+ function mergeStates(appState = {}, userState = {}, sessionState = {}) {
76
+ const merged = cloneDeep(sessionState);
77
+ for (const [k, v] of Object.entries(appState)) {
78
+ merged[State.APP_PREFIX + k] = v;
79
+ }
80
+ for (const [k, v] of Object.entries(userState)) {
81
+ merged[State.USER_PREFIX + k] = v;
82
+ }
83
+ return merged;
84
+ }
39
85
  export {
40
- BaseSessionService
86
+ BaseSessionService,
87
+ mergeStates,
88
+ trimTempDeltaState
41
89
  };