@google/adk 0.1.2 → 0.2.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 (57) hide show
  1. package/dist/cjs/agents/content_processor_utils.js +20 -8
  2. package/dist/cjs/agents/functions.js +23 -32
  3. package/dist/cjs/agents/llm_agent.js +1 -0
  4. package/dist/cjs/artifacts/gcs_artifact_service.js +140 -0
  5. package/dist/cjs/common.js +12 -0
  6. package/dist/cjs/index.js +5 -5
  7. package/dist/cjs/index.js.map +4 -4
  8. package/dist/cjs/models/base_llm.js +12 -3
  9. package/dist/cjs/models/google_llm.js +6 -20
  10. package/dist/cjs/models/registry.js +1 -1
  11. package/dist/cjs/telemetry/google_cloud.js +85 -0
  12. package/dist/cjs/telemetry/setup.js +97 -0
  13. package/dist/cjs/telemetry/tracing.js +231 -0
  14. package/dist/cjs/utils/client_labels.js +56 -0
  15. package/dist/cjs/version.js +1 -1
  16. package/dist/esm/agents/content_processor_utils.js +20 -8
  17. package/dist/esm/agents/functions.js +23 -32
  18. package/dist/esm/agents/llm_agent.js +1 -0
  19. package/dist/esm/artifacts/gcs_artifact_service.js +110 -0
  20. package/dist/esm/common.js +8 -0
  21. package/dist/esm/index.js +5 -5
  22. package/dist/esm/index.js.map +4 -4
  23. package/dist/esm/models/base_llm.js +12 -3
  24. package/dist/esm/models/google_llm.js +6 -20
  25. package/dist/esm/models/registry.js +1 -1
  26. package/dist/esm/telemetry/google_cloud.js +54 -0
  27. package/dist/esm/telemetry/setup.js +67 -0
  28. package/dist/esm/telemetry/tracing.js +195 -0
  29. package/dist/esm/utils/client_labels.js +26 -0
  30. package/dist/esm/version.js +1 -1
  31. package/dist/types/artifacts/gcs_artifact_service.d.ts +16 -0
  32. package/dist/types/common.d.ts +4 -0
  33. package/dist/types/index.d.ts +3 -0
  34. package/dist/types/models/base_llm.d.ts +6 -3
  35. package/dist/types/models/google_llm.d.ts +1 -2
  36. package/dist/types/models/registry.d.ts +6 -2
  37. package/dist/types/telemetry/google_cloud.d.ts +9 -0
  38. package/dist/types/telemetry/setup.d.ts +48 -0
  39. package/dist/types/telemetry/tracing.d.ts +111 -0
  40. package/dist/types/utils/client_labels.d.ts +9 -0
  41. package/dist/types/version.d.ts +1 -1
  42. package/dist/web/agents/content_processor_utils.js +20 -8
  43. package/dist/web/agents/functions.js +23 -32
  44. package/dist/web/agents/llm_agent.js +1 -0
  45. package/dist/web/artifacts/gcs_artifact_service.js +126 -0
  46. package/dist/web/common.js +8 -0
  47. package/dist/web/index.js +1 -1
  48. package/dist/web/index.js.map +4 -4
  49. package/dist/web/models/base_llm.js +12 -3
  50. package/dist/web/models/google_llm.js +6 -20
  51. package/dist/web/models/registry.js +1 -1
  52. package/dist/web/telemetry/google_cloud.js +54 -0
  53. package/dist/web/telemetry/setup.js +67 -0
  54. package/dist/web/telemetry/tracing.js +210 -0
  55. package/dist/web/utils/client_labels.js +26 -0
  56. package/dist/web/version.js +1 -1
  57. package/package.json +20 -4
@@ -103,8 +103,8 @@ function isEventFromAnotherAgent(agentName, event) {
103
103
  return !!agentName && event.author !== agentName && event.author !== "user";
104
104
  }
105
105
  function convertForeignEvent(event) {
106
- var _a, _b, _c, _d, _e;
107
- if (!((_a = event.content) == null ? void 0 : _a.parts)) {
106
+ var _a, _b, _c, _d, _e, _f;
107
+ if (!((_b = (_a = event.content) == null ? void 0 : _a.parts) == null ? void 0 : _b.length)) {
108
108
  return event;
109
109
  }
110
110
  const content = {
@@ -115,19 +115,21 @@ function convertForeignEvent(event) {
115
115
  };
116
116
  for (const part of event.content.parts) {
117
117
  if (part.text && !part.thought) {
118
- (_b = content.parts) == null ? void 0 : _b.push({
118
+ (_c = content.parts) == null ? void 0 : _c.push({
119
119
  text: `[${event.author}] said: ${part.text}`
120
120
  });
121
121
  } else if (part.functionCall) {
122
- (_c = content.parts) == null ? void 0 : _c.push({
123
- text: `[${event.author}] called tool \`${part.functionCall.name}\` with parameters: ${part.functionCall.args}`
122
+ const argsText = safeStringify(part.functionCall.args);
123
+ (_d = content.parts) == null ? void 0 : _d.push({
124
+ text: `[${event.author}] called tool \`${part.functionCall.name}\` with parameters: ${argsText}`
124
125
  });
125
126
  } else if (part.functionResponse) {
126
- (_d = content.parts) == null ? void 0 : _d.push({
127
- text: `[${event.author}] tool \`${part.functionResponse.name}\` returned result: ${part.functionResponse.response}`
127
+ const responseText = safeStringify(part.functionResponse.response);
128
+ (_e = content.parts) == null ? void 0 : _e.push({
129
+ text: `[${event.author}] tool \`${part.functionResponse.name}\` returned result: ${responseText}`
128
130
  });
129
131
  } else {
130
- (_e = content.parts) == null ? void 0 : _e.push(part);
132
+ (_f = content.parts) == null ? void 0 : _f.push(part);
131
133
  }
132
134
  }
133
135
  return (0, import_event.createEvent)({
@@ -292,6 +294,16 @@ function rearrangeEventsForAsyncFunctionResponsesInHistory(events) {
292
294
  }
293
295
  return resultEvents;
294
296
  }
297
+ function safeStringify(obj) {
298
+ if (typeof obj === "string") {
299
+ return obj;
300
+ }
301
+ try {
302
+ return JSON.stringify(obj);
303
+ } catch (e) {
304
+ return String(obj);
305
+ }
306
+ }
295
307
  // Annotate the CommonJS export names for ESM import in node:
296
308
  0 && (module.exports = {
297
309
  getContents,
@@ -38,6 +38,7 @@ __export(functions_exports, {
38
38
  removeClientFunctionCallId: () => removeClientFunctionCallId
39
39
  });
40
40
  module.exports = __toCommonJS(functions_exports);
41
+ var import_genai = require("@google/genai");
41
42
  var import_event = require("../events/event.js");
42
43
  var import_event_actions = require("../events/event_actions.js");
43
44
  var import_tool_context = require("../tools/tool_context.js");
@@ -166,30 +167,6 @@ async function callToolAsync(tool, args, toolContext) {
166
167
  import_logger.logger.debug(`callToolAsync ${tool.name}`);
167
168
  return await tool.runAsync({ args, toolContext });
168
169
  }
169
- function buildResponseEvent(tool, functionResult, toolContext, invocationContext) {
170
- let responseResult = functionResult;
171
- if (typeof functionResult !== "object" || functionResult == null) {
172
- responseResult = { result: functionResult };
173
- }
174
- const partFunctionResponse = {
175
- functionResponse: {
176
- name: tool.name,
177
- response: responseResult,
178
- id: toolContext.functionCallId
179
- }
180
- };
181
- const content = {
182
- role: "user",
183
- parts: [partFunctionResponse]
184
- };
185
- return (0, import_event.createEvent)({
186
- invocationId: invocationContext.invocationId,
187
- author: invocationContext.agent.name,
188
- content,
189
- actions: toolContext.actions,
190
- branch: invocationContext.branch
191
- });
192
- }
193
170
  async function handleFunctionCallsAsync({
194
171
  invocationContext,
195
172
  functionCallEvent,
@@ -240,6 +217,7 @@ async function handleFunctionCallList({
240
217
  import_logger.logger.debug(`execute_tool ${tool.name}`);
241
218
  const functionArgs = (_a = functionCall.args) != null ? _a : {};
242
219
  let functionResponse = null;
220
+ let functionResponseError;
243
221
  functionResponse = await invocationContext.pluginManager.runBeforeToolCallback({
244
222
  tool,
245
223
  toolArgs: functionArgs,
@@ -276,10 +254,11 @@ async function handleFunctionCallList({
276
254
  );
277
255
  if (onToolErrorResponse) {
278
256
  functionResponse = onToolErrorResponse;
257
+ } else {
258
+ functionResponseError = e.message;
279
259
  }
280
260
  } else {
281
- import_logger.logger.error("Unknown error on tool execution type", e);
282
- throw e;
261
+ functionResponseError = e;
283
262
  }
284
263
  }
285
264
  }
@@ -308,12 +287,24 @@ async function handleFunctionCallList({
308
287
  if (tool.isLongRunning && !functionResponse) {
309
288
  continue;
310
289
  }
311
- const functionResponseEvent = buildResponseEvent(
312
- tool,
313
- functionResponse,
314
- toolContext,
315
- invocationContext
316
- );
290
+ if (functionResponseError) {
291
+ functionResponse = { error: functionResponseError };
292
+ } else if (typeof functionResponse !== "object" || functionResponse == null) {
293
+ functionResponse = { result: functionResponse };
294
+ }
295
+ const functionResponseEvent = (0, import_event.createEvent)({
296
+ invocationId: invocationContext.invocationId,
297
+ author: invocationContext.agent.name,
298
+ content: (0, import_genai.createUserContent)({
299
+ functionResponse: {
300
+ id: toolContext.functionCallId,
301
+ name: tool.name,
302
+ response: functionResponse
303
+ }
304
+ }),
305
+ actions: toolContext.actions,
306
+ branch: invocationContext.branch
307
+ });
317
308
  import_logger.logger.debug("traceToolCall", {
318
309
  tool: tool.name,
319
310
  args: functionArgs,
@@ -170,6 +170,7 @@ class AgentTransferLlmRequestProcessor extends import_base_llm_processor.BaseLlm
170
170
  throw new Error("toolContext is required.");
171
171
  }
172
172
  toolContext.actions.transferToAgent = args.agentName;
173
+ return "Transfer queued";
173
174
  }
174
175
  });
175
176
  }
@@ -0,0 +1,140 @@
1
+ /**
2
+ * @license
3
+ * Copyright 2025 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 __export = (target, all) => {
13
+ for (var name in all)
14
+ __defProp(target, name, { get: all[name], enumerable: true });
15
+ };
16
+ var __copyProps = (to, from, except, desc) => {
17
+ if (from && typeof from === "object" || typeof from === "function") {
18
+ for (let key of __getOwnPropNames(from))
19
+ if (!__hasOwnProp.call(to, key) && key !== except)
20
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
21
+ }
22
+ return to;
23
+ };
24
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
25
+ var gcs_artifact_service_exports = {};
26
+ __export(gcs_artifact_service_exports, {
27
+ GcsArtifactService: () => GcsArtifactService
28
+ });
29
+ module.exports = __toCommonJS(gcs_artifact_service_exports);
30
+ var import_storage = require("@google-cloud/storage");
31
+ var import_genai = require("@google/genai");
32
+ /**
33
+ * @license
34
+ * Copyright 2025 Google LLC
35
+ * SPDX-License-Identifier: Apache-2.0
36
+ */
37
+ class GcsArtifactService {
38
+ constructor(bucket) {
39
+ this.bucket = new import_storage.Storage().bucket(bucket);
40
+ }
41
+ async saveArtifact(request) {
42
+ const versions = await this.listVersions(request);
43
+ const version = versions.length > 0 ? Math.max(...versions) + 1 : 0;
44
+ const file = this.bucket.file(getFileName({
45
+ ...request,
46
+ version
47
+ }));
48
+ if (request.artifact.inlineData) {
49
+ await file.save(JSON.stringify(request.artifact.inlineData.data), {
50
+ contentType: request.artifact.inlineData.mimeType
51
+ });
52
+ return version;
53
+ }
54
+ if (request.artifact.text) {
55
+ await file.save(request.artifact.text, {
56
+ contentType: "text/plain"
57
+ });
58
+ return version;
59
+ }
60
+ throw new Error("Artifact must have either inlineData or text.");
61
+ }
62
+ async loadArtifact(request) {
63
+ let version = request.version;
64
+ if (version === void 0) {
65
+ const versions = await this.listVersions(request);
66
+ if (versions.length === 0) {
67
+ return void 0;
68
+ }
69
+ version = Math.max(...versions);
70
+ }
71
+ const file = this.bucket.file(getFileName({
72
+ ...request,
73
+ version
74
+ }));
75
+ const [[metadata], [rawDataBuffer]] = await Promise.all([file.getMetadata(), file.download()]);
76
+ if (metadata.contentType === "text/plain") {
77
+ return (0, import_genai.createPartFromText)(rawDataBuffer.toString("utf-8"));
78
+ }
79
+ return (0, import_genai.createPartFromBase64)(
80
+ rawDataBuffer.toString("base64"),
81
+ metadata.contentType
82
+ );
83
+ }
84
+ async listArtifactKeys(request) {
85
+ const fileNames = [];
86
+ const sessionPrefix = `${request.appName}/${request.userId}/${request.sessionId}/`;
87
+ const usernamePrefix = `${request.appName}/${request.userId}/user/`;
88
+ const [
89
+ [sessionFiles],
90
+ [userSessionFiles]
91
+ ] = await Promise.all([
92
+ this.bucket.getFiles({ prefix: sessionPrefix }),
93
+ this.bucket.getFiles({ prefix: usernamePrefix })
94
+ ]);
95
+ for (const file of sessionFiles) {
96
+ fileNames.push(file.name.split("/").pop());
97
+ }
98
+ for (const file of userSessionFiles) {
99
+ fileNames.push(file.name.split("/").pop());
100
+ }
101
+ return fileNames.sort((a, b) => a.localeCompare(b));
102
+ }
103
+ async deleteArtifact(request) {
104
+ const versions = await this.listVersions(request);
105
+ await Promise.all(versions.map((version) => {
106
+ const file = this.bucket.file(getFileName({
107
+ ...request,
108
+ version
109
+ }));
110
+ return file.delete();
111
+ }));
112
+ return;
113
+ }
114
+ async listVersions(request) {
115
+ const prefix = getFileName(request);
116
+ const [files] = await this.bucket.getFiles({ prefix });
117
+ const versions = [];
118
+ for (const file of files) {
119
+ const version = file.name.split("/").pop();
120
+ versions.push(parseInt(version, 10));
121
+ }
122
+ return versions;
123
+ }
124
+ }
125
+ function getFileName({
126
+ appName,
127
+ userId,
128
+ sessionId,
129
+ filename,
130
+ version
131
+ }) {
132
+ if (filename.startsWith("user:")) {
133
+ return `${appName}/${userId}/user/${filename}/${version}`;
134
+ }
135
+ return `${appName}/${userId}/${sessionId}/${filename}/${version}`;
136
+ }
137
+ // Annotate the CommonJS export names for ESM import in node:
138
+ 0 && (module.exports = {
139
+ GcsArtifactService
140
+ });
@@ -41,6 +41,7 @@ __export(common_exports, {
41
41
  InMemoryRunner: () => import_in_memory_runner.InMemoryRunner,
42
42
  InMemorySessionService: () => import_in_memory_session_service.InMemorySessionService,
43
43
  InvocationContext: () => import_invocation_context.InvocationContext,
44
+ LLMRegistry: () => import_registry.LLMRegistry,
44
45
  LiveRequestQueue: () => import_live_request_queue.LiveRequestQueue,
45
46
  LlmAgent: () => import_llm_agent.LlmAgent,
46
47
  LogLevel: () => import_logger.LogLevel,
@@ -54,10 +55,12 @@ __export(common_exports, {
54
55
  Runner: () => import_runner.Runner,
55
56
  SecurityPlugin: () => import_security_plugin.SecurityPlugin,
56
57
  SequentialAgent: () => import_sequential_agent.SequentialAgent,
58
+ State: () => import_state.State,
57
59
  StreamingMode: () => import_run_config.StreamingMode,
58
60
  ToolConfirmation: () => import_tool_confirmation.ToolConfirmation,
59
61
  ToolContext: () => import_tool_context.ToolContext,
60
62
  createEvent: () => import_event.createEvent,
63
+ createEventActions: () => import_event_actions.createEventActions,
61
64
  createSession: () => import_session.createSession,
62
65
  functionsExportedForTestingOnly: () => import_functions.functionsExportedForTestingOnly,
63
66
  getAskUserConfirmationFunctionCalls: () => import_security_plugin.getAskUserConfirmationFunctionCalls,
@@ -67,6 +70,7 @@ __export(common_exports, {
67
70
  isFinalResponse: () => import_event.isFinalResponse,
68
71
  setLogLevel: () => import_logger.setLogLevel,
69
72
  stringifyContent: () => import_event.stringifyContent,
73
+ version: () => import_version.version,
70
74
  zodObjectToSchema: () => import_simple_zod_to_json.zodObjectToSchema
71
75
  });
72
76
  module.exports = __toCommonJS(common_exports);
@@ -82,9 +86,11 @@ var import_run_config = require("./agents/run_config.js");
82
86
  var import_sequential_agent = require("./agents/sequential_agent.js");
83
87
  var import_in_memory_artifact_service = require("./artifacts/in_memory_artifact_service.js");
84
88
  var import_event = require("./events/event.js");
89
+ var import_event_actions = require("./events/event_actions.js");
85
90
  var import_in_memory_memory_service = require("./memory/in_memory_memory_service.js");
86
91
  var import_base_llm = require("./models/base_llm.js");
87
92
  var import_google_llm = require("./models/google_llm.js");
93
+ var import_registry = require("./models/registry.js");
88
94
  var import_base_plugin = require("./plugins/base_plugin.js");
89
95
  var import_logging_plugin = require("./plugins/logging_plugin.js");
90
96
  var import_plugin_manager = require("./plugins/plugin_manager.js");
@@ -93,6 +99,7 @@ var import_in_memory_runner = require("./runner/in_memory_runner.js");
93
99
  var import_runner = require("./runner/runner.js");
94
100
  var import_in_memory_session_service = require("./sessions/in_memory_session_service.js");
95
101
  var import_session = require("./sessions/session.js");
102
+ var import_state = require("./sessions/state.js");
96
103
  var import_agent_tool = require("./tools/agent_tool.js");
97
104
  var import_base_tool = require("./tools/base_tool.js");
98
105
  var import_base_toolset = require("./tools/base_toolset.js");
@@ -103,6 +110,7 @@ var import_tool_confirmation = require("./tools/tool_confirmation.js");
103
110
  var import_tool_context = require("./tools/tool_context.js");
104
111
  var import_logger = require("./utils/logger.js");
105
112
  var import_simple_zod_to_json = require("./utils/simple_zod_to_json.js");
113
+ var import_version = require("./version.js");
106
114
  __reExport(common_exports, require("./artifacts/base_artifact_service.js"), module.exports);
107
115
  __reExport(common_exports, require("./memory/base_memory_service.js"), module.exports);
108
116
  __reExport(common_exports, require("./sessions/base_session_service.js"), module.exports);
@@ -130,6 +138,7 @@ __reExport(common_exports, require("./tools/base_tool.js"), module.exports);
130
138
  InMemoryRunner,
131
139
  InMemorySessionService,
132
140
  InvocationContext,
141
+ LLMRegistry,
133
142
  LiveRequestQueue,
134
143
  LlmAgent,
135
144
  LogLevel,
@@ -143,10 +152,12 @@ __reExport(common_exports, require("./tools/base_tool.js"), module.exports);
143
152
  Runner,
144
153
  SecurityPlugin,
145
154
  SequentialAgent,
155
+ State,
146
156
  StreamingMode,
147
157
  ToolConfirmation,
148
158
  ToolContext,
149
159
  createEvent,
160
+ createEventActions,
150
161
  createSession,
151
162
  functionsExportedForTestingOnly,
152
163
  getAskUserConfirmationFunctionCalls,
@@ -156,6 +167,7 @@ __reExport(common_exports, require("./tools/base_tool.js"), module.exports);
156
167
  isFinalResponse,
157
168
  setLogLevel,
158
169
  stringifyContent,
170
+ version,
159
171
  zodObjectToSchema,
160
172
  ...require("./artifacts/base_artifact_service.js"),
161
173
  ...require("./memory/base_memory_service.js"),