@google/adk 0.4.0 → 0.5.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 (132) hide show
  1. package/dist/cjs/a2a/a2a_event.js +290 -0
  2. package/dist/cjs/a2a/event_converter_utils.js +201 -0
  3. package/dist/cjs/a2a/executor_context.js +53 -0
  4. package/dist/cjs/a2a/metadata_converter_utils.js +125 -0
  5. package/dist/cjs/a2a/part_converter_utils.js +23 -21
  6. package/dist/cjs/agents/base_agent.js +3 -3
  7. package/dist/cjs/{tools/tool_context.js → agents/context.js} +70 -15
  8. package/dist/cjs/agents/functions.js +2 -2
  9. package/dist/cjs/agents/llm_agent.js +21 -674
  10. package/dist/cjs/agents/processors/agent_transfer_llm_request_processor.js +132 -0
  11. package/dist/cjs/agents/processors/basic_llm_request_processor.js +68 -0
  12. package/dist/cjs/agents/processors/code_execution_request_processor.js +389 -0
  13. package/dist/cjs/agents/processors/content_request_processor.js +66 -0
  14. package/dist/cjs/agents/processors/identity_llm_request_processor.js +54 -0
  15. package/dist/cjs/agents/processors/instructions_llm_request_processor.js +85 -0
  16. package/dist/cjs/agents/processors/request_confirmation_llm_request_processor.js +165 -0
  17. package/dist/cjs/common.js +11 -7
  18. package/dist/cjs/index.js +89 -53
  19. package/dist/cjs/index.js.map +7 -0
  20. package/dist/cjs/models/llm_response.js +2 -0
  21. package/dist/cjs/plugins/base_plugin.js +1 -1
  22. package/dist/cjs/runner/runner.js +1 -1
  23. package/dist/cjs/sessions/database_session_service.js +4 -1
  24. package/dist/cjs/sessions/db/operations.js +24 -12
  25. package/dist/cjs/tools/base_tool.js +3 -0
  26. package/dist/cjs/tools/base_toolset.js +13 -3
  27. package/dist/cjs/tools/exit_loop_tool.js +63 -0
  28. package/dist/cjs/tools/mcp/mcp_toolset.js +9 -5
  29. package/dist/cjs/utils/logger.js +61 -54
  30. package/dist/cjs/version.js +1 -1
  31. package/dist/esm/a2a/a2a_event.js +243 -0
  32. package/dist/esm/a2a/event_converter_utils.js +187 -0
  33. package/dist/esm/a2a/executor_context.js +23 -0
  34. package/dist/esm/a2a/metadata_converter_utils.js +90 -0
  35. package/dist/esm/a2a/part_converter_utils.js +25 -21
  36. package/dist/esm/agents/base_agent.js +3 -3
  37. package/dist/esm/{tools/tool_context.js → agents/context.js} +66 -11
  38. package/dist/esm/agents/functions.js +2 -2
  39. package/dist/esm/agents/llm_agent.js +14 -683
  40. package/dist/esm/agents/processors/agent_transfer_llm_request_processor.js +101 -0
  41. package/dist/esm/agents/processors/basic_llm_request_processor.js +37 -0
  42. package/dist/esm/agents/processors/code_execution_request_processor.js +363 -0
  43. package/dist/esm/agents/processors/content_request_processor.js +38 -0
  44. package/dist/esm/agents/processors/identity_llm_request_processor.js +23 -0
  45. package/dist/esm/agents/processors/instructions_llm_request_processor.js +54 -0
  46. package/dist/esm/agents/processors/request_confirmation_llm_request_processor.js +140 -0
  47. package/dist/esm/common.js +11 -9
  48. package/dist/esm/index.js +95 -18
  49. package/dist/esm/index.js.map +7 -0
  50. package/dist/esm/models/llm_response.js +2 -0
  51. package/dist/esm/plugins/base_plugin.js +1 -1
  52. package/dist/esm/runner/runner.js +1 -1
  53. package/dist/esm/sessions/database_session_service.js +4 -1
  54. package/dist/esm/sessions/db/operations.js +31 -7
  55. package/dist/esm/tools/base_tool.js +3 -0
  56. package/dist/esm/tools/base_toolset.js +11 -2
  57. package/dist/esm/tools/exit_loop_tool.js +32 -0
  58. package/dist/esm/tools/mcp/mcp_toolset.js +9 -5
  59. package/dist/esm/utils/logger.js +51 -54
  60. package/dist/esm/version.js +1 -1
  61. package/dist/types/a2a/a2a_event.d.ts +122 -0
  62. package/dist/types/a2a/event_converter_utils.d.ts +20 -0
  63. package/dist/types/a2a/executor_context.d.ts +33 -0
  64. package/dist/types/a2a/metadata_converter_utils.d.ts +62 -0
  65. package/dist/types/a2a/part_converter_utils.d.ts +4 -3
  66. package/dist/types/agents/base_agent.d.ts +2 -2
  67. package/dist/types/{tools/tool_context.d.ts → agents/context.d.ts} +43 -8
  68. package/dist/types/agents/llm_agent.d.ts +8 -31
  69. package/dist/types/agents/processors/agent_transfer_llm_request_processor.d.ts +18 -0
  70. package/dist/types/agents/{base_llm_processor.d.ts → processors/base_llm_processor.d.ts} +4 -4
  71. package/dist/types/agents/processors/basic_llm_request_processor.d.ts +13 -0
  72. package/dist/types/agents/processors/code_execution_request_processor.d.ts +34 -0
  73. package/dist/types/agents/processors/content_request_processor.d.ts +13 -0
  74. package/dist/types/agents/processors/identity_llm_request_processor.d.ts +13 -0
  75. package/dist/types/agents/processors/instructions_llm_request_processor.d.ts +16 -0
  76. package/dist/types/agents/processors/request_confirmation_llm_request_processor.d.ts +13 -0
  77. package/dist/types/auth/credential_service/base_credential_service.d.ts +3 -3
  78. package/dist/types/auth/credential_service/in_memory_credential_service.d.ts +3 -3
  79. package/dist/types/common.d.ts +4 -4
  80. package/dist/types/models/llm_response.d.ts +5 -1
  81. package/dist/types/plugins/base_plugin.d.ts +12 -13
  82. package/dist/types/plugins/logging_plugin.d.ts +9 -10
  83. package/dist/types/plugins/plugin_manager.d.ts +9 -10
  84. package/dist/types/plugins/security_plugin.d.ts +2 -2
  85. package/dist/types/sessions/database_session_service.d.ts +2 -1
  86. package/dist/types/sessions/db/operations.d.ts +1 -1
  87. package/dist/types/tools/base_tool.d.ts +3 -3
  88. package/dist/types/tools/base_toolset.d.ts +12 -3
  89. package/dist/types/tools/exit_loop_tool.d.ts +24 -0
  90. package/dist/types/tools/forwarding_artifact_service.d.ts +2 -2
  91. package/dist/types/tools/function_tool.d.ts +2 -2
  92. package/dist/types/tools/mcp/mcp_toolset.d.ts +1 -1
  93. package/dist/types/utils/logger.d.ts +5 -9
  94. package/dist/types/version.d.ts +1 -1
  95. package/dist/web/a2a/a2a_event.js +243 -0
  96. package/dist/web/a2a/event_converter_utils.js +201 -0
  97. package/dist/web/a2a/executor_context.js +23 -0
  98. package/dist/web/a2a/metadata_converter_utils.js +107 -0
  99. package/dist/web/a2a/part_converter_utils.js +25 -21
  100. package/dist/web/agents/base_agent.js +3 -3
  101. package/dist/web/{tools/tool_context.js → agents/context.js} +66 -11
  102. package/dist/web/agents/functions.js +2 -2
  103. package/dist/web/agents/llm_agent.js +14 -661
  104. package/dist/web/agents/processors/agent_transfer_llm_request_processor.js +100 -0
  105. package/dist/web/agents/processors/basic_llm_request_processor.js +71 -0
  106. package/dist/web/agents/processors/code_execution_request_processor.js +365 -0
  107. package/dist/web/agents/processors/content_request_processor.js +56 -0
  108. package/dist/web/agents/processors/identity_llm_request_processor.js +41 -0
  109. package/dist/web/agents/processors/instructions_llm_request_processor.js +72 -0
  110. package/dist/web/agents/processors/request_confirmation_llm_request_processor.js +158 -0
  111. package/dist/web/common.js +11 -9
  112. package/dist/web/index.js +13 -18
  113. package/dist/web/index.js.map +7 -0
  114. package/dist/web/models/llm_response.js +2 -0
  115. package/dist/web/plugins/base_plugin.js +1 -1
  116. package/dist/web/runner/runner.js +1 -1
  117. package/dist/web/sessions/database_session_service.js +4 -1
  118. package/dist/web/sessions/db/operations.js +31 -7
  119. package/dist/web/tools/base_tool.js +3 -0
  120. package/dist/web/tools/base_toolset.js +11 -2
  121. package/dist/web/tools/exit_loop_tool.js +32 -0
  122. package/dist/web/tools/mcp/mcp_toolset.js +27 -5
  123. package/dist/web/utils/logger.js +51 -54
  124. package/dist/web/version.js +1 -1
  125. package/package.json +3 -2
  126. package/dist/cjs/agents/callback_context.js +0 -101
  127. package/dist/esm/agents/callback_context.js +0 -71
  128. package/dist/types/agents/callback_context.d.ts +0 -42
  129. package/dist/web/agents/callback_context.js +0 -71
  130. /package/dist/cjs/agents/{base_llm_processor.js → processors/base_llm_processor.js} +0 -0
  131. /package/dist/esm/agents/{base_llm_processor.js → processors/base_llm_processor.js} +0 -0
  132. /package/dist/web/agents/{base_llm_processor.js → processors/base_llm_processor.js} +0 -0
@@ -0,0 +1,201 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __defProps = Object.defineProperties;
3
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
4
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
7
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
8
+ var __spreadValues = (a, b) => {
9
+ for (var prop in b || (b = {}))
10
+ if (__hasOwnProp.call(b, prop))
11
+ __defNormalProp(a, prop, b[prop]);
12
+ if (__getOwnPropSymbols)
13
+ for (var prop of __getOwnPropSymbols(b)) {
14
+ if (__propIsEnum.call(b, prop))
15
+ __defNormalProp(a, prop, b[prop]);
16
+ }
17
+ return a;
18
+ };
19
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
20
+ /**
21
+ * @license
22
+ * Copyright 2026 Google LLC
23
+ * SPDX-License-Identifier: Apache-2.0
24
+ */
25
+ import {
26
+ createModelContent,
27
+ createUserContent
28
+ } from "@google/genai";
29
+ import { createEvent } from "../events/event.js";
30
+ import { createEventActions } from "../events/event_actions.js";
31
+ import { randomUUID } from "../utils/env_aware_utils.js";
32
+ import {
33
+ getEventMetadata,
34
+ getFailedTaskStatusUpdateEventError,
35
+ isFailedTaskStatusUpdateEvent,
36
+ isInputRequiredTaskStatusUpdateEvent,
37
+ isMessage,
38
+ isTask,
39
+ isTaskArtifactUpdateEvent,
40
+ isTaskStatusUpdateEvent,
41
+ isTerminalTaskStatusUpdateEvent,
42
+ MessageRole
43
+ } from "./a2a_event.js";
44
+ import {
45
+ A2AMetadataKeys,
46
+ getA2AEventMetadata
47
+ } from "./metadata_converter_utils.js";
48
+ import { toA2AParts, toGenAIPart, toGenAIParts } from "./part_converter_utils.js";
49
+ function toA2AMessage(event, {
50
+ appName,
51
+ userId,
52
+ sessionId
53
+ }) {
54
+ var _a;
55
+ return {
56
+ kind: "message",
57
+ messageId: randomUUID(),
58
+ role: event.author === MessageRole.USER ? MessageRole.USER : MessageRole.AGENT,
59
+ parts: toA2AParts(((_a = event.content) == null ? void 0 : _a.parts) || [], event.longRunningToolIds),
60
+ metadata: getA2AEventMetadata(event, { appName, userId, sessionId })
61
+ };
62
+ }
63
+ function toAdkEvent(event, invocationId, agentName) {
64
+ if (isMessage(event)) {
65
+ return messageToAdkEvent(event, invocationId, agentName);
66
+ }
67
+ if (isTask(event)) {
68
+ return taskToAdkEvent(event, invocationId, agentName);
69
+ }
70
+ if (isTaskArtifactUpdateEvent(event)) {
71
+ return artifactUpdateToAdkEvent(event, invocationId, agentName);
72
+ }
73
+ if (isTaskStatusUpdateEvent(event)) {
74
+ return event.final ? finalTaskStatusUpdateToAdkEvent(event, invocationId, agentName) : taskStatusUpdateToAdkEvent(event, invocationId, agentName);
75
+ }
76
+ return void 0;
77
+ }
78
+ function messageToAdkEvent(msg, invocationId, agentName, parentEvent) {
79
+ const parts = toGenAIParts(msg.parts);
80
+ return __spreadProps(__spreadValues({}, createAdkEventFromMetadata(parentEvent || msg)), {
81
+ invocationId,
82
+ author: msg.role === MessageRole.USER ? MessageRole.USER : agentName,
83
+ content: msg.role === MessageRole.USER ? createUserContent(parts) : createModelContent(parts),
84
+ turnComplete: true,
85
+ partial: false
86
+ });
87
+ }
88
+ function artifactUpdateToAdkEvent(a2aEvent, invocationId, agentName) {
89
+ var _a;
90
+ const partsToConvert = ((_a = a2aEvent.artifact) == null ? void 0 : _a.parts) || [];
91
+ if (partsToConvert.length === 0) {
92
+ return void 0;
93
+ }
94
+ const partial = !!getEventMetadata(a2aEvent)[A2AMetadataKeys.PARTIAL] || a2aEvent.append || !a2aEvent.lastChunk;
95
+ return __spreadProps(__spreadValues({}, createAdkEventFromMetadata(a2aEvent)), {
96
+ invocationId,
97
+ author: agentName,
98
+ content: createModelContent(toGenAIParts(partsToConvert)),
99
+ longRunningToolIds: getLongRunningToolIDs(partsToConvert),
100
+ partial
101
+ });
102
+ }
103
+ function finalTaskStatusUpdateToAdkEvent(a2aEvent, invocationId, agentName) {
104
+ var _a;
105
+ const partsToConvert = ((_a = a2aEvent.status.message) == null ? void 0 : _a.parts) || [];
106
+ if (partsToConvert.length === 0) {
107
+ return void 0;
108
+ }
109
+ const parts = toGenAIParts(partsToConvert);
110
+ const isFailedTask = isFailedTaskStatusUpdateEvent(a2aEvent);
111
+ const hasContent = !isFailedTask && parts.length > 0;
112
+ return __spreadProps(__spreadValues({}, createAdkEventFromMetadata(a2aEvent)), {
113
+ invocationId,
114
+ author: agentName,
115
+ errorMessage: isFailedTask ? getFailedTaskStatusUpdateEventError(a2aEvent) : void 0,
116
+ content: hasContent ? createModelContent(parts) : void 0,
117
+ longRunningToolIds: getLongRunningToolIDs(partsToConvert),
118
+ turnComplete: true
119
+ });
120
+ }
121
+ function taskStatusUpdateToAdkEvent(a2aEvent, invocationId, agentName) {
122
+ const msg = a2aEvent.status.message;
123
+ if (!msg) {
124
+ return void 0;
125
+ }
126
+ const parts = toGenAIParts(msg.parts);
127
+ return __spreadProps(__spreadValues({}, createAdkEventFromMetadata(a2aEvent)), {
128
+ invocationId,
129
+ author: agentName,
130
+ content: createModelContent(parts),
131
+ turnComplete: false,
132
+ partial: true
133
+ });
134
+ }
135
+ function taskToAdkEvent(a2aTask, invocationId, agentName) {
136
+ var _a, _b;
137
+ const parts = [];
138
+ const longRunningToolIds = [];
139
+ if (a2aTask.artifacts) {
140
+ for (const artifact of a2aTask.artifacts) {
141
+ if (((_a = artifact.parts) == null ? void 0 : _a.length) > 0) {
142
+ const artifactParts = toGenAIParts(artifact.parts);
143
+ parts.push(...artifactParts);
144
+ longRunningToolIds.push(...getLongRunningToolIDs(artifact.parts));
145
+ }
146
+ }
147
+ }
148
+ if ((_b = a2aTask.status) == null ? void 0 : _b.message) {
149
+ const a2aParts = a2aTask.status.message.parts;
150
+ const genAIParts = toGenAIParts(a2aParts);
151
+ parts.push(...genAIParts);
152
+ longRunningToolIds.push(...getLongRunningToolIDs(a2aParts));
153
+ }
154
+ const isTerminal = isTerminalTaskStatusUpdateEvent(a2aTask) || isInputRequiredTaskStatusUpdateEvent(a2aTask);
155
+ const isFailed = isFailedTaskStatusUpdateEvent(a2aTask);
156
+ if (parts.length === 0 && !isTerminal) {
157
+ return void 0;
158
+ }
159
+ return __spreadProps(__spreadValues({}, createAdkEventFromMetadata(a2aTask)), {
160
+ invocationId,
161
+ author: agentName,
162
+ content: isFailed ? void 0 : createModelContent(parts),
163
+ errorMessage: isFailed ? getFailedTaskStatusUpdateEventError(a2aTask) : void 0,
164
+ longRunningToolIds,
165
+ turnComplete: isTerminal
166
+ });
167
+ }
168
+ function createAdkEventFromMetadata(a2aEvent) {
169
+ const metadata = a2aEvent.metadata || {};
170
+ return createEvent({
171
+ branch: metadata[A2AMetadataKeys.BRANCH],
172
+ author: metadata[A2AMetadataKeys.AUTHOR],
173
+ partial: metadata[A2AMetadataKeys.PARTIAL],
174
+ errorCode: metadata[A2AMetadataKeys.ERROR_CODE],
175
+ errorMessage: metadata[A2AMetadataKeys.ERROR_MESSAGE],
176
+ citationMetadata: metadata[A2AMetadataKeys.CITATION_METADATA],
177
+ groundingMetadata: metadata[A2AMetadataKeys.GROUNDING_METADATA],
178
+ usageMetadata: metadata[A2AMetadataKeys.USAGE_METADATA],
179
+ customMetadata: metadata[A2AMetadataKeys.CUSTOM_METADATA],
180
+ actions: createEventActions({
181
+ escalate: !!metadata[A2AMetadataKeys.ESCALATE],
182
+ transferToAgent: metadata[A2AMetadataKeys.TRANSFER_TO_AGENT]
183
+ })
184
+ });
185
+ }
186
+ function getLongRunningToolIDs(parts) {
187
+ const ids = [];
188
+ for (const a2aPart of parts) {
189
+ if (a2aPart.metadata && a2aPart.metadata[A2AMetadataKeys.IS_LONG_RUNNING]) {
190
+ const genAIPart = toGenAIPart(a2aPart);
191
+ if (genAIPart.functionCall && genAIPart.functionCall.id) {
192
+ ids.push(genAIPart.functionCall.id);
193
+ }
194
+ }
195
+ }
196
+ return ids;
197
+ }
198
+ export {
199
+ toA2AMessage,
200
+ toAdkEvent
201
+ };
@@ -0,0 +1,23 @@
1
+ /**
2
+ * @license
3
+ * Copyright 2026 Google LLC
4
+ * SPDX-License-Identifier: Apache-2.0
5
+ */
6
+ function createExecutorContext({
7
+ session,
8
+ userContent,
9
+ requestContext
10
+ }) {
11
+ return {
12
+ userId: session.userId,
13
+ sessionId: session.id,
14
+ agentName: session.appName,
15
+ readonlyState: session.state,
16
+ events: session.events,
17
+ userContent,
18
+ requestContext
19
+ };
20
+ }
21
+ export {
22
+ createExecutorContext
23
+ };
@@ -0,0 +1,107 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __defProps = Object.defineProperties;
3
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
4
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
7
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
8
+ var __spreadValues = (a, b) => {
9
+ for (var prop in b || (b = {}))
10
+ if (__hasOwnProp.call(b, prop))
11
+ __defNormalProp(a, prop, b[prop]);
12
+ if (__getOwnPropSymbols)
13
+ for (var prop of __getOwnPropSymbols(b)) {
14
+ if (__propIsEnum.call(b, prop))
15
+ __defNormalProp(a, prop, b[prop]);
16
+ }
17
+ return a;
18
+ };
19
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
20
+ /**
21
+ * @license
22
+ * Copyright 2026 Google LLC
23
+ * SPDX-License-Identifier: Apache-2.0
24
+ */
25
+ import { isTask } from "./a2a_event.js";
26
+ const ADK_METADATA_KEY_PREFIX = "adk_";
27
+ const A2A_METADATA_KEY_PREFIX = "a2a:";
28
+ var A2AMetadataKeys = ((A2AMetadataKeys2) => {
29
+ A2AMetadataKeys2[A2AMetadataKeys2["APP_NAME"] = "".concat(ADK_METADATA_KEY_PREFIX, "app_name")] = "APP_NAME";
30
+ A2AMetadataKeys2[A2AMetadataKeys2["USER_ID"] = "".concat(ADK_METADATA_KEY_PREFIX, "user_id")] = "USER_ID";
31
+ A2AMetadataKeys2[A2AMetadataKeys2["SESSION_ID"] = "".concat(ADK_METADATA_KEY_PREFIX, "session_id")] = "SESSION_ID";
32
+ A2AMetadataKeys2[A2AMetadataKeys2["INVOCATION_ID"] = "".concat(ADK_METADATA_KEY_PREFIX, "invocation_id")] = "INVOCATION_ID";
33
+ A2AMetadataKeys2[A2AMetadataKeys2["AUTHOR"] = "".concat(ADK_METADATA_KEY_PREFIX, "author")] = "AUTHOR";
34
+ A2AMetadataKeys2[A2AMetadataKeys2["BRANCH"] = "".concat(ADK_METADATA_KEY_PREFIX, "branch")] = "BRANCH";
35
+ A2AMetadataKeys2[A2AMetadataKeys2["DATA_PART_TYPE"] = "".concat(ADK_METADATA_KEY_PREFIX, "type")] = "DATA_PART_TYPE";
36
+ A2AMetadataKeys2[A2AMetadataKeys2["PARTIAL"] = "".concat(ADK_METADATA_KEY_PREFIX, "partial")] = "PARTIAL";
37
+ A2AMetadataKeys2[A2AMetadataKeys2["ESCALATE"] = "".concat(ADK_METADATA_KEY_PREFIX, "escalate")] = "ESCALATE";
38
+ A2AMetadataKeys2[A2AMetadataKeys2["TRANSFER_TO_AGENT"] = "".concat(ADK_METADATA_KEY_PREFIX, "transfer_to_agent")] = "TRANSFER_TO_AGENT";
39
+ A2AMetadataKeys2[A2AMetadataKeys2["IS_LONG_RUNNING"] = "".concat(ADK_METADATA_KEY_PREFIX, "is_long_running")] = "IS_LONG_RUNNING";
40
+ A2AMetadataKeys2[A2AMetadataKeys2["THOUGHT"] = "".concat(ADK_METADATA_KEY_PREFIX, "thought")] = "THOUGHT";
41
+ A2AMetadataKeys2[A2AMetadataKeys2["ERROR_CODE"] = "".concat(ADK_METADATA_KEY_PREFIX, "error_code")] = "ERROR_CODE";
42
+ A2AMetadataKeys2[A2AMetadataKeys2["ERROR_MESSAGE"] = "".concat(ADK_METADATA_KEY_PREFIX, "error_message")] = "ERROR_MESSAGE";
43
+ A2AMetadataKeys2[A2AMetadataKeys2["CITATION_METADATA"] = "".concat(ADK_METADATA_KEY_PREFIX, "citation_metadata")] = "CITATION_METADATA";
44
+ A2AMetadataKeys2[A2AMetadataKeys2["GROUNDING_METADATA"] = "".concat(ADK_METADATA_KEY_PREFIX, "grounding_metadata")] = "GROUNDING_METADATA";
45
+ A2AMetadataKeys2[A2AMetadataKeys2["USAGE_METADATA"] = "".concat(ADK_METADATA_KEY_PREFIX, "usage_metadata")] = "USAGE_METADATA";
46
+ A2AMetadataKeys2[A2AMetadataKeys2["CUSTOM_METADATA"] = "".concat(ADK_METADATA_KEY_PREFIX, "custom_metadata")] = "CUSTOM_METADATA";
47
+ return A2AMetadataKeys2;
48
+ })(A2AMetadataKeys || {});
49
+ var AdkMetadataKeys = ((AdkMetadataKeys2) => {
50
+ AdkMetadataKeys2[AdkMetadataKeys2["TASK_ID"] = "".concat(A2A_METADATA_KEY_PREFIX, "task_id")] = "TASK_ID";
51
+ AdkMetadataKeys2[AdkMetadataKeys2["CONTEXT_ID"] = "".concat(A2A_METADATA_KEY_PREFIX, "context_id")] = "CONTEXT_ID";
52
+ return AdkMetadataKeys2;
53
+ })(AdkMetadataKeys || {});
54
+ function getAdkEventMetadata(a2aEvent) {
55
+ return {
56
+ [AdkMetadataKeys.TASK_ID]: isTask(a2aEvent) ? a2aEvent.id : a2aEvent.taskId,
57
+ [AdkMetadataKeys.CONTEXT_ID]: a2aEvent.contextId
58
+ };
59
+ }
60
+ function getA2AEventMetadata(adkEvent, {
61
+ appName,
62
+ userId,
63
+ sessionId
64
+ }) {
65
+ return __spreadProps(__spreadValues(__spreadValues({}, getA2AEventMetadataFromActions(adkEvent.actions)), getA2ASessionMetadata({
66
+ appName,
67
+ userId,
68
+ sessionId
69
+ })), {
70
+ [A2AMetadataKeys.INVOCATION_ID]: adkEvent.invocationId,
71
+ [A2AMetadataKeys.AUTHOR]: adkEvent.author,
72
+ [A2AMetadataKeys.BRANCH]: adkEvent.branch,
73
+ [A2AMetadataKeys.ERROR_CODE]: adkEvent.errorMessage,
74
+ [A2AMetadataKeys.ERROR_MESSAGE]: adkEvent.errorMessage,
75
+ [A2AMetadataKeys.CITATION_METADATA]: adkEvent.citationMetadata,
76
+ [A2AMetadataKeys.GROUNDING_METADATA]: adkEvent.groundingMetadata,
77
+ [A2AMetadataKeys.USAGE_METADATA]: adkEvent.usageMetadata,
78
+ [A2AMetadataKeys.CUSTOM_METADATA]: adkEvent.customMetadata,
79
+ [A2AMetadataKeys.PARTIAL]: adkEvent.partial,
80
+ [A2AMetadataKeys.IS_LONG_RUNNING]: (adkEvent.longRunningToolIds || []).length > 0
81
+ });
82
+ }
83
+ function getA2ASessionMetadata({
84
+ appName,
85
+ userId,
86
+ sessionId
87
+ }) {
88
+ return {
89
+ [A2AMetadataKeys.APP_NAME]: appName,
90
+ [A2AMetadataKeys.USER_ID]: userId,
91
+ [A2AMetadataKeys.SESSION_ID]: sessionId
92
+ };
93
+ }
94
+ function getA2AEventMetadataFromActions(actions) {
95
+ return {
96
+ [A2AMetadataKeys.ESCALATE]: actions.escalate,
97
+ [A2AMetadataKeys.TRANSFER_TO_AGENT]: actions.transferToAgent
98
+ };
99
+ }
100
+ export {
101
+ A2AMetadataKeys,
102
+ AdkMetadataKeys,
103
+ getA2AEventMetadata,
104
+ getA2AEventMetadataFromActions,
105
+ getA2ASessionMetadata,
106
+ getAdkEventMetadata
107
+ };
@@ -3,12 +3,11 @@
3
3
  * Copyright 2026 Google LLC
4
4
  * SPDX-License-Identifier: Apache-2.0
5
5
  */
6
- var MetadataKeys = /* @__PURE__ */ ((MetadataKeys2) => {
7
- MetadataKeys2["TYPE"] = "adk_type";
8
- MetadataKeys2["LONG_RUNNING"] = "adk_is_long_running";
9
- MetadataKeys2["THOUGHT"] = "adk_thought";
10
- return MetadataKeys2;
11
- })(MetadataKeys || {});
6
+ import {
7
+ createModelContent,
8
+ createUserContent
9
+ } from "@google/genai";
10
+ import { A2AMetadataKeys } from "./metadata_converter_utils.js";
12
11
  var DataPartType = /* @__PURE__ */ ((DataPartType2) => {
13
12
  DataPartType2["FUNCTION_CALL"] = "function_call";
14
13
  DataPartType2["FUNCTION_RESPONSE"] = "function_response";
@@ -16,7 +15,7 @@ var DataPartType = /* @__PURE__ */ ((DataPartType2) => {
16
15
  DataPartType2["CODE_EXECUTABLE_CODE"] = "executable_code";
17
16
  return DataPartType2;
18
17
  })(DataPartType || {});
19
- function toA2AParts(parts, longRunningToolIDs = []) {
18
+ function toA2AParts(parts = [], longRunningToolIDs = []) {
20
19
  return parts.map((part) => toA2APart(part, longRunningToolIDs));
21
20
  }
22
21
  function toA2APart(part, longRunningToolIDs) {
@@ -30,9 +29,9 @@ function toA2APart(part, longRunningToolIDs) {
30
29
  }
31
30
  function toA2ATextPart(part) {
32
31
  const a2aPart = { kind: "text", text: part.text || "" };
33
- if ("thought" in part && part["thought"]) {
32
+ if (part.thought) {
34
33
  a2aPart.metadata = {
35
- ["adk_thought" /* THOUGHT */]: true
34
+ [A2AMetadataKeys.THOUGHT]: true
36
35
  };
37
36
  }
38
37
  return a2aPart;
@@ -61,31 +60,31 @@ function toA2AFilePart(part) {
61
60
  throw new Error("Not a file part: ".concat(JSON.stringify(part)));
62
61
  }
63
62
  function toA2ADataPart(part, longRunningToolIDs = []) {
64
- let type;
63
+ let dataPartType;
65
64
  let data;
66
65
  if (part.functionCall) {
67
- type = "function_call" /* FUNCTION_CALL */;
66
+ dataPartType = "function_call" /* FUNCTION_CALL */;
68
67
  data = part.functionCall;
69
68
  } else if (part.functionResponse) {
70
- type = "function_response" /* FUNCTION_RESPONSE */;
69
+ dataPartType = "function_response" /* FUNCTION_RESPONSE */;
71
70
  data = part.functionResponse;
72
71
  } else if (part.executableCode) {
73
- type = "executable_code" /* CODE_EXECUTABLE_CODE */;
72
+ dataPartType = "executable_code" /* CODE_EXECUTABLE_CODE */;
74
73
  data = part.executableCode;
75
74
  } else if (part.codeExecutionResult) {
76
- type = "code_execution_result" /* CODE_EXEC_RESULT */;
75
+ dataPartType = "code_execution_result" /* CODE_EXEC_RESULT */;
77
76
  data = part.codeExecutionResult;
78
77
  } else {
79
78
  throw new Error("Unknown part type: ".concat(JSON.stringify(part)));
80
79
  }
81
80
  const metadata = {
82
- ["adk_type" /* TYPE */]: type
81
+ [A2AMetadataKeys.DATA_PART_TYPE]: dataPartType
83
82
  };
84
- if (part.functionCall && part.functionCall.name && longRunningToolIDs.includes(part.functionCall.name)) {
85
- metadata["adk_is_long_running" /* LONG_RUNNING */] = true;
83
+ if (part.functionCall && part.functionCall.id && longRunningToolIDs.includes(part.functionCall.id)) {
84
+ metadata[A2AMetadataKeys.IS_LONG_RUNNING] = true;
86
85
  }
87
- if (part.functionResponse && part.functionResponse.name && longRunningToolIDs.includes(part.functionResponse.name)) {
88
- metadata["adk_is_long_running" /* LONG_RUNNING */] = true;
86
+ if (part.functionResponse && part.functionResponse.id && longRunningToolIDs.includes(part.functionResponse.id)) {
87
+ metadata[A2AMetadataKeys.IS_LONG_RUNNING] = true;
89
88
  }
90
89
  return {
91
90
  kind: "data",
@@ -93,6 +92,10 @@ function toA2ADataPart(part, longRunningToolIDs = []) {
93
92
  metadata
94
93
  };
95
94
  }
95
+ function toGenAIContent(a2aMessage) {
96
+ const parts = toGenAIParts(a2aMessage.parts);
97
+ return a2aMessage.role === "user" ? createUserContent(parts) : createModelContent(parts);
98
+ }
96
99
  function toGenAIParts(a2aParts) {
97
100
  return a2aParts.map((a2aPart) => toGenAIPart(a2aPart));
98
101
  }
@@ -112,7 +115,7 @@ function toGenAIPartText(a2aPart) {
112
115
  var _a;
113
116
  return {
114
117
  text: a2aPart.text,
115
- thought: !!((_a = a2aPart.metadata) == null ? void 0 : _a["adk_thought" /* THOUGHT */])
118
+ thought: !!((_a = a2aPart.metadata) == null ? void 0 : _a[A2AMetadataKeys.THOUGHT])
116
119
  };
117
120
  }
118
121
  function toGenAIPartFile(a2aPart) {
@@ -140,7 +143,7 @@ function toGenAIPartData(a2aPart) {
140
143
  throw new Error("No data in part: ".concat(JSON.stringify(a2aPart)));
141
144
  }
142
145
  const data = a2aPart.data;
143
- const type = (_a = a2aPart.metadata) == null ? void 0 : _a["adk_type" /* TYPE */];
146
+ const type = (_a = a2aPart.metadata) == null ? void 0 : _a[A2AMetadataKeys.DATA_PART_TYPE];
144
147
  if (type === "function_call" /* FUNCTION_CALL */) {
145
148
  return { functionCall: data };
146
149
  }
@@ -163,6 +166,7 @@ export {
163
166
  toA2APart,
164
167
  toA2AParts,
165
168
  toA2ATextPart,
169
+ toGenAIContent,
166
170
  toGenAIPart,
167
171
  toGenAIPartData,
168
172
  toGenAIPartFile,
@@ -78,7 +78,7 @@ import {
78
78
  traceAgentInvocation,
79
79
  tracer
80
80
  } from "../telemetry/tracing.js";
81
- import { CallbackContext } from "./callback_context.js";
81
+ import { Context } from "./context.js";
82
82
  import { InvocationContext } from "./invocation_context.js";
83
83
  const BASE_AGENT_SIGNATURE_SYMBOL = Symbol.for("google.adk.baseAgent");
84
84
  function isBaseAgent(obj) {
@@ -239,7 +239,7 @@ class BaseAgent {
239
239
  if (this.beforeAgentCallback.length === 0) {
240
240
  return void 0;
241
241
  }
242
- const callbackContext = new CallbackContext({ invocationContext });
242
+ const callbackContext = new Context({ invocationContext });
243
243
  for (const callback of this.beforeAgentCallback) {
244
244
  const content = await callback(callbackContext);
245
245
  if (content) {
@@ -274,7 +274,7 @@ class BaseAgent {
274
274
  if (this.afterAgentCallback.length === 0) {
275
275
  return void 0;
276
276
  }
277
- const callbackContext = new CallbackContext({ invocationContext });
277
+ const callbackContext = new Context({ invocationContext });
278
278
  for (const callback of this.afterAgentCallback) {
279
279
  const content = await callback(callbackContext);
280
280
  if (content) {
@@ -3,29 +3,84 @@
3
3
  * Copyright 2025 Google LLC
4
4
  * SPDX-License-Identifier: Apache-2.0
5
5
  */
6
- import { CallbackContext } from "../agents/callback_context.js";
7
6
  import { AuthHandler } from "../auth/auth_handler.js";
7
+ import { createEventActions } from "../events/event_actions.js";
8
+ import { State } from "../sessions/state.js";
8
9
  import { ToolConfirmation } from "../tools/tool_confirmation.js";
9
- class ToolContext extends CallbackContext {
10
+ import { ReadonlyContext } from "./readonly_context.js";
11
+ class Context extends ReadonlyContext {
10
12
  /**
11
- * @param params.invocationContext The invocation context of the tool.
12
- * @param params.eventActions The event actions of the current tool call.
13
- * @param params.functionCallId The function call id of the current tool call.
13
+ * @param options The configuration options for the Context.
14
+ * @param options.invocationContext The invocation context.
15
+ * @param options.eventActions The event actions of the current call.
16
+ * @param options.functionCallId The function call id of the current tool call.
14
17
  * This id was returned in the function call event from LLM to identify a
15
18
  * function call. If LLM didn't return this id, ADK will assign one to it.
16
19
  * This id is used to map function call response to the original function
17
20
  * call.
18
- * @param params.toolConfirmation The tool confirmation of the current tool
21
+ * @param options.toolConfirmation The tool confirmation of the current tool
19
22
  * call.
20
23
  */
21
- constructor(params) {
22
- super(params);
23
- this.functionCallId = params.functionCallId;
24
- this.toolConfirmation = params.toolConfirmation;
24
+ constructor(options) {
25
+ super(options.invocationContext);
26
+ this.eventActions = options.eventActions || createEventActions();
27
+ this._state = new State(
28
+ options.invocationContext.session.state,
29
+ this.eventActions.stateDelta
30
+ );
31
+ this.functionCallId = options.functionCallId;
32
+ this.toolConfirmation = options.toolConfirmation;
33
+ }
34
+ /**
35
+ * The delta-aware state of the current session.
36
+ */
37
+ get state() {
38
+ return this._state;
25
39
  }
26
40
  get actions() {
27
41
  return this.eventActions;
28
42
  }
43
+ /**
44
+ * Loads an artifact attached to the current session.
45
+ *
46
+ * @param filename The filename of the artifact.
47
+ * @param version The version of the artifact. If not provided, the latest
48
+ * version will be used.
49
+ * @return A promise that resolves to the loaded artifact.
50
+ */
51
+ loadArtifact(filename, version) {
52
+ if (!this.invocationContext.artifactService) {
53
+ throw new Error("Artifact service is not initialized.");
54
+ }
55
+ return this.invocationContext.artifactService.loadArtifact({
56
+ appName: this.invocationContext.appName,
57
+ userId: this.invocationContext.userId,
58
+ sessionId: this.invocationContext.session.id,
59
+ filename,
60
+ version
61
+ });
62
+ }
63
+ /**
64
+ * Saves an artifact attached to the current session.
65
+ *
66
+ * @param filename The filename of the artifact.
67
+ * @param artifact The artifact to save.
68
+ * @return A promise that resolves to the version of the saved artifact.
69
+ */
70
+ async saveArtifact(filename, artifact) {
71
+ if (!this.invocationContext.artifactService) {
72
+ throw new Error("Artifact service is not initialized.");
73
+ }
74
+ const version = await this.invocationContext.artifactService.saveArtifact({
75
+ appName: this.invocationContext.appName,
76
+ userId: this.invocationContext.userId,
77
+ sessionId: this.invocationContext.session.id,
78
+ filename,
79
+ artifact
80
+ });
81
+ this.eventActions.artifactDelta[filename] = version;
82
+ return version;
83
+ }
29
84
  requestCredential(authConfig) {
30
85
  if (!this.functionCallId) {
31
86
  throw new Error("functionCallId is not set.");
@@ -90,5 +145,5 @@ class ToolContext extends CallbackContext {
90
145
  }
91
146
  }
92
147
  export {
93
- ToolContext
148
+ Context
94
149
  };
@@ -7,9 +7,9 @@ import { createUserContent } from "@google/genai";
7
7
  import { isEmpty } from "lodash-es";
8
8
  import { createEvent, getFunctionCalls } from "../events/event.js";
9
9
  import { mergeEventActions } from "../events/event_actions.js";
10
- import { ToolContext } from "../tools/tool_context.js";
11
10
  import { randomUUID } from "../utils/env_aware_utils.js";
12
11
  import { logger } from "../utils/logger.js";
12
+ import { Context } from "./context.js";
13
13
  import {
14
14
  traceMergedToolCalls,
15
15
  tracer,
@@ -352,7 +352,7 @@ function getToolAndContext({
352
352
  "Function ".concat(functionCall.name, " is not found in the toolsDict.")
353
353
  );
354
354
  }
355
- const toolContext = new ToolContext({
355
+ const toolContext = new Context({
356
356
  invocationContext,
357
357
  functionCallId: functionCall.id || void 0,
358
358
  toolConfirmation