@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
@@ -3,6 +3,7 @@
3
3
  * Copyright 2025 Google LLC
4
4
  * SPDX-License-Identifier: Apache-2.0
5
5
  */
6
+ import { createUserContent } from "@google/genai";
6
7
  import { createEvent, getFunctionCalls } from "../events/event.js";
7
8
  import { mergeEventActions } from "../events/event_actions.js";
8
9
  import { ToolContext } from "../tools/tool_context.js";
@@ -126,30 +127,6 @@ async function callToolAsync(tool, args, toolContext) {
126
127
  logger.debug(`callToolAsync ${tool.name}`);
127
128
  return await tool.runAsync({ args, toolContext });
128
129
  }
129
- function buildResponseEvent(tool, functionResult, toolContext, invocationContext) {
130
- let responseResult = functionResult;
131
- if (typeof functionResult !== "object" || functionResult == null) {
132
- responseResult = { result: functionResult };
133
- }
134
- const partFunctionResponse = {
135
- functionResponse: {
136
- name: tool.name,
137
- response: responseResult,
138
- id: toolContext.functionCallId
139
- }
140
- };
141
- const content = {
142
- role: "user",
143
- parts: [partFunctionResponse]
144
- };
145
- return createEvent({
146
- invocationId: invocationContext.invocationId,
147
- author: invocationContext.agent.name,
148
- content,
149
- actions: toolContext.actions,
150
- branch: invocationContext.branch
151
- });
152
- }
153
130
  async function handleFunctionCallsAsync({
154
131
  invocationContext,
155
132
  functionCallEvent,
@@ -200,6 +177,7 @@ async function handleFunctionCallList({
200
177
  logger.debug(`execute_tool ${tool.name}`);
201
178
  const functionArgs = (_a = functionCall.args) != null ? _a : {};
202
179
  let functionResponse = null;
180
+ let functionResponseError;
203
181
  functionResponse = await invocationContext.pluginManager.runBeforeToolCallback({
204
182
  tool,
205
183
  toolArgs: functionArgs,
@@ -236,10 +214,11 @@ async function handleFunctionCallList({
236
214
  );
237
215
  if (onToolErrorResponse) {
238
216
  functionResponse = onToolErrorResponse;
217
+ } else {
218
+ functionResponseError = e.message;
239
219
  }
240
220
  } else {
241
- logger.error("Unknown error on tool execution type", e);
242
- throw e;
221
+ functionResponseError = e;
243
222
  }
244
223
  }
245
224
  }
@@ -268,12 +247,24 @@ async function handleFunctionCallList({
268
247
  if (tool.isLongRunning && !functionResponse) {
269
248
  continue;
270
249
  }
271
- const functionResponseEvent = buildResponseEvent(
272
- tool,
273
- functionResponse,
274
- toolContext,
275
- invocationContext
276
- );
250
+ if (functionResponseError) {
251
+ functionResponse = { error: functionResponseError };
252
+ } else if (typeof functionResponse !== "object" || functionResponse == null) {
253
+ functionResponse = { result: functionResponse };
254
+ }
255
+ const functionResponseEvent = createEvent({
256
+ invocationId: invocationContext.invocationId,
257
+ author: invocationContext.agent.name,
258
+ content: createUserContent({
259
+ functionResponse: {
260
+ id: toolContext.functionCallId,
261
+ name: tool.name,
262
+ response: functionResponse
263
+ }
264
+ }),
265
+ actions: toolContext.actions,
266
+ branch: invocationContext.branch
267
+ });
277
268
  logger.debug("traceToolCall", {
278
269
  tool: tool.name,
279
270
  args: functionArgs,
@@ -140,6 +140,7 @@ class AgentTransferLlmRequestProcessor extends BaseLlmRequestProcessor {
140
140
  throw new Error("toolContext is required.");
141
141
  }
142
142
  toolContext.actions.transferToAgent = args.agentName;
143
+ return "Transfer queued";
143
144
  }
144
145
  });
145
146
  }
@@ -0,0 +1,110 @@
1
+ /**
2
+ * @license
3
+ * Copyright 2025 Google LLC
4
+ * SPDX-License-Identifier: Apache-2.0
5
+ */
6
+ import { Storage } from "@google-cloud/storage";
7
+ import { createPartFromBase64, createPartFromText } from "@google/genai";
8
+ class GcsArtifactService {
9
+ constructor(bucket) {
10
+ this.bucket = new Storage().bucket(bucket);
11
+ }
12
+ async saveArtifact(request) {
13
+ const versions = await this.listVersions(request);
14
+ const version = versions.length > 0 ? Math.max(...versions) + 1 : 0;
15
+ const file = this.bucket.file(getFileName({
16
+ ...request,
17
+ version
18
+ }));
19
+ if (request.artifact.inlineData) {
20
+ await file.save(JSON.stringify(request.artifact.inlineData.data), {
21
+ contentType: request.artifact.inlineData.mimeType
22
+ });
23
+ return version;
24
+ }
25
+ if (request.artifact.text) {
26
+ await file.save(request.artifact.text, {
27
+ contentType: "text/plain"
28
+ });
29
+ return version;
30
+ }
31
+ throw new Error("Artifact must have either inlineData or text.");
32
+ }
33
+ async loadArtifact(request) {
34
+ let version = request.version;
35
+ if (version === void 0) {
36
+ const versions = await this.listVersions(request);
37
+ if (versions.length === 0) {
38
+ return void 0;
39
+ }
40
+ version = Math.max(...versions);
41
+ }
42
+ const file = this.bucket.file(getFileName({
43
+ ...request,
44
+ version
45
+ }));
46
+ const [[metadata], [rawDataBuffer]] = await Promise.all([file.getMetadata(), file.download()]);
47
+ if (metadata.contentType === "text/plain") {
48
+ return createPartFromText(rawDataBuffer.toString("utf-8"));
49
+ }
50
+ return createPartFromBase64(
51
+ rawDataBuffer.toString("base64"),
52
+ metadata.contentType
53
+ );
54
+ }
55
+ async listArtifactKeys(request) {
56
+ const fileNames = [];
57
+ const sessionPrefix = `${request.appName}/${request.userId}/${request.sessionId}/`;
58
+ const usernamePrefix = `${request.appName}/${request.userId}/user/`;
59
+ const [
60
+ [sessionFiles],
61
+ [userSessionFiles]
62
+ ] = await Promise.all([
63
+ this.bucket.getFiles({ prefix: sessionPrefix }),
64
+ this.bucket.getFiles({ prefix: usernamePrefix })
65
+ ]);
66
+ for (const file of sessionFiles) {
67
+ fileNames.push(file.name.split("/").pop());
68
+ }
69
+ for (const file of userSessionFiles) {
70
+ fileNames.push(file.name.split("/").pop());
71
+ }
72
+ return fileNames.sort((a, b) => a.localeCompare(b));
73
+ }
74
+ async deleteArtifact(request) {
75
+ const versions = await this.listVersions(request);
76
+ await Promise.all(versions.map((version) => {
77
+ const file = this.bucket.file(getFileName({
78
+ ...request,
79
+ version
80
+ }));
81
+ return file.delete();
82
+ }));
83
+ return;
84
+ }
85
+ async listVersions(request) {
86
+ const prefix = getFileName(request);
87
+ const [files] = await this.bucket.getFiles({ prefix });
88
+ const versions = [];
89
+ for (const file of files) {
90
+ const version = file.name.split("/").pop();
91
+ versions.push(parseInt(version, 10));
92
+ }
93
+ return versions;
94
+ }
95
+ }
96
+ function getFileName({
97
+ appName,
98
+ userId,
99
+ sessionId,
100
+ filename,
101
+ version
102
+ }) {
103
+ if (filename.startsWith("user:")) {
104
+ return `${appName}/${userId}/user/${filename}/${version}`;
105
+ }
106
+ return `${appName}/${userId}/${sessionId}/${filename}/${version}`;
107
+ }
108
+ export {
109
+ GcsArtifactService
110
+ };
@@ -15,9 +15,11 @@ import { StreamingMode } from "./agents/run_config.js";
15
15
  import { SequentialAgent } from "./agents/sequential_agent.js";
16
16
  import { InMemoryArtifactService } from "./artifacts/in_memory_artifact_service.js";
17
17
  import { createEvent, getFunctionCalls, getFunctionResponses, hasTrailingCodeExecutionResult, isFinalResponse, stringifyContent } from "./events/event.js";
18
+ import { createEventActions } from "./events/event_actions.js";
18
19
  import { InMemoryMemoryService } from "./memory/in_memory_memory_service.js";
19
20
  import { BaseLlm } from "./models/base_llm.js";
20
21
  import { Gemini } from "./models/google_llm.js";
22
+ import { LLMRegistry } from "./models/registry.js";
21
23
  import { BasePlugin } from "./plugins/base_plugin.js";
22
24
  import { LoggingPlugin } from "./plugins/logging_plugin.js";
23
25
  import { PluginManager } from "./plugins/plugin_manager.js";
@@ -26,6 +28,7 @@ import { InMemoryRunner } from "./runner/in_memory_runner.js";
26
28
  import { Runner } from "./runner/runner.js";
27
29
  import { InMemorySessionService } from "./sessions/in_memory_session_service.js";
28
30
  import { createSession } from "./sessions/session.js";
31
+ import { State } from "./sessions/state.js";
29
32
  import { AgentTool } from "./tools/agent_tool.js";
30
33
  import { BaseTool } from "./tools/base_tool.js";
31
34
  import { BaseToolset } from "./tools/base_toolset.js";
@@ -36,6 +39,7 @@ import { ToolConfirmation } from "./tools/tool_confirmation.js";
36
39
  import { ToolContext } from "./tools/tool_context.js";
37
40
  import { LogLevel, setLogLevel } from "./utils/logger.js";
38
41
  import { zodObjectToSchema } from "./utils/simple_zod_to_json.js";
42
+ import { version } from "./version.js";
39
43
  export * from "./artifacts/base_artifact_service.js";
40
44
  export * from "./memory/base_memory_service.js";
41
45
  export * from "./sessions/base_session_service.js";
@@ -57,6 +61,7 @@ export {
57
61
  InMemoryRunner,
58
62
  InMemorySessionService,
59
63
  InvocationContext,
64
+ LLMRegistry,
60
65
  LiveRequestQueue,
61
66
  LlmAgent,
62
67
  LogLevel,
@@ -70,10 +75,12 @@ export {
70
75
  Runner,
71
76
  SecurityPlugin,
72
77
  SequentialAgent,
78
+ State,
73
79
  StreamingMode,
74
80
  ToolConfirmation,
75
81
  ToolContext,
76
82
  createEvent,
83
+ createEventActions,
77
84
  createSession,
78
85
  functionsExportedForTestingOnly,
79
86
  getAskUserConfirmationFunctionCalls,
@@ -83,5 +90,6 @@ export {
83
90
  isFinalResponse,
84
91
  setLogLevel,
85
92
  stringifyContent,
93
+ version,
86
94
  zodObjectToSchema
87
95
  };