@google/adk 2.2.0 → 2.2.1
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.
- package/dist/cjs/a2a/event_converter_utils.js +5 -1
- package/dist/cjs/a2a/metadata_converter_utils.js +2 -1
- package/dist/cjs/common.js +18 -0
- package/dist/cjs/events/abort_events.js +135 -0
- package/dist/cjs/events/internal_metadata.js +131 -0
- package/dist/cjs/models/gemini_continuation.js +232 -0
- package/dist/cjs/plugins/debug_logging_plugin.js +653 -0
- package/dist/cjs/plugins/plugin_manager.js +1 -1
- package/dist/cjs/plugins/tool_call_integrity_plugin.js +364 -0
- package/dist/cjs/runner/runner.js +17 -3
- package/dist/cjs/version.js +1 -1
- package/dist/esm/a2a/event_converter_utils.js +5 -1
- package/dist/esm/a2a/metadata_converter_utils.js +2 -1
- package/dist/esm/common.js +20 -0
- package/dist/esm/events/abort_events.js +110 -0
- package/dist/esm/events/internal_metadata.js +97 -0
- package/dist/esm/models/gemini_continuation.js +205 -0
- package/dist/esm/plugins/debug_logging_plugin.js +615 -0
- package/dist/esm/plugins/plugin_manager.js +1 -1
- package/dist/esm/plugins/tool_call_integrity_plugin.js +339 -0
- package/dist/esm/runner/runner.js +20 -3
- package/dist/esm/version.js +1 -1
- package/dist/types/common.d.ts +3 -0
- package/dist/types/events/abort_events.d.ts +43 -0
- package/dist/types/events/internal_metadata.d.ts +78 -0
- package/dist/types/models/gemini_continuation.d.ts +59 -0
- package/dist/types/plugins/debug_logging_plugin.d.ts +157 -0
- package/dist/types/plugins/tool_call_integrity_plugin.d.ts +116 -0
- package/dist/types/version.d.ts +1 -1
- package/dist/web/index_web.js +59 -59
- package/dist/web/index_web.js.map +4 -4
- package/package.json +1 -1
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import {createRequire as topLevelCreateRequire} from 'module';
|
|
2
|
+
const require = topLevelCreateRequire(import.meta.url);
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
5
|
+
/**
|
|
6
|
+
* @license
|
|
7
|
+
* Copyright 2026 Google LLC
|
|
8
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
9
|
+
*/
|
|
10
|
+
import { logger } from "../utils/logger.js";
|
|
11
|
+
const INTERNAL_METADATA_PREFIX = "__adk_internal_";
|
|
12
|
+
const RESTORED_EVENT_KEY = `${INTERNAL_METADATA_PREFIX}restored_event`;
|
|
13
|
+
function isMetadataObject(value) {
|
|
14
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
15
|
+
}
|
|
16
|
+
__name(isMetadataObject, "isMetadataObject");
|
|
17
|
+
function isInternalKey(key) {
|
|
18
|
+
return key.startsWith(INTERNAL_METADATA_PREFIX);
|
|
19
|
+
}
|
|
20
|
+
__name(isInternalKey, "isInternalKey");
|
|
21
|
+
function dropInternalKeys(metadata) {
|
|
22
|
+
return Object.fromEntries(
|
|
23
|
+
Object.entries(metadata).filter(([key]) => !isInternalKey(key))
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
__name(dropInternalKeys, "dropInternalKeys");
|
|
27
|
+
function hasInternalKeys(metadata) {
|
|
28
|
+
return isMetadataObject(metadata) && Object.keys(metadata).some(isInternalKey);
|
|
29
|
+
}
|
|
30
|
+
__name(hasInternalKeys, "hasInternalKeys");
|
|
31
|
+
function withoutInternalMetadata(metadata) {
|
|
32
|
+
if (!isMetadataObject(metadata)) {
|
|
33
|
+
return metadata;
|
|
34
|
+
}
|
|
35
|
+
const kept = dropInternalKeys(metadata);
|
|
36
|
+
const dropped = Object.keys(metadata).filter(isInternalKey);
|
|
37
|
+
if (dropped.length > 0) {
|
|
38
|
+
logger.debug(
|
|
39
|
+
`Dropping ADK-internal customMetadata keys: ${JSON.stringify(dropped.sort())}`
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
return kept;
|
|
43
|
+
}
|
|
44
|
+
__name(withoutInternalMetadata, "withoutInternalMetadata");
|
|
45
|
+
function publicMetadata(metadata) {
|
|
46
|
+
if (!isMetadataObject(metadata)) {
|
|
47
|
+
return metadata;
|
|
48
|
+
}
|
|
49
|
+
const kept = dropInternalKeys(metadata);
|
|
50
|
+
return Object.keys(kept).length > 0 ? kept : void 0;
|
|
51
|
+
}
|
|
52
|
+
__name(publicMetadata, "publicMetadata");
|
|
53
|
+
function internalMetadata(metadata) {
|
|
54
|
+
if (!isMetadataObject(metadata)) {
|
|
55
|
+
return {};
|
|
56
|
+
}
|
|
57
|
+
return Object.fromEntries(
|
|
58
|
+
Object.entries(metadata).filter(([key]) => isInternalKey(key))
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
__name(internalMetadata, "internalMetadata");
|
|
62
|
+
function markRestored(event) {
|
|
63
|
+
var _a;
|
|
64
|
+
event.customMetadata = {
|
|
65
|
+
...(_a = withoutInternalMetadata(event.customMetadata)) != null ? _a : {},
|
|
66
|
+
[RESTORED_EVENT_KEY]: true
|
|
67
|
+
};
|
|
68
|
+
return event;
|
|
69
|
+
}
|
|
70
|
+
__name(markRestored, "markRestored");
|
|
71
|
+
function publicEvent(event) {
|
|
72
|
+
if (!hasInternalKeys(event.customMetadata)) {
|
|
73
|
+
return event;
|
|
74
|
+
}
|
|
75
|
+
return { ...event, customMetadata: publicMetadata(event.customMetadata) };
|
|
76
|
+
}
|
|
77
|
+
__name(publicEvent, "publicEvent");
|
|
78
|
+
function publicSession(session) {
|
|
79
|
+
var _a;
|
|
80
|
+
const events = (_a = session.events) != null ? _a : [];
|
|
81
|
+
const publicEvents = events.map((event) => publicEvent(event));
|
|
82
|
+
if (publicEvents.every((event, i) => event === events[i])) {
|
|
83
|
+
return session;
|
|
84
|
+
}
|
|
85
|
+
return { ...session, events: publicEvents };
|
|
86
|
+
}
|
|
87
|
+
__name(publicSession, "publicSession");
|
|
88
|
+
export {
|
|
89
|
+
INTERNAL_METADATA_PREFIX,
|
|
90
|
+
RESTORED_EVENT_KEY,
|
|
91
|
+
internalMetadata,
|
|
92
|
+
markRestored,
|
|
93
|
+
publicEvent,
|
|
94
|
+
publicMetadata,
|
|
95
|
+
publicSession,
|
|
96
|
+
withoutInternalMetadata
|
|
97
|
+
};
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import {createRequire as topLevelCreateRequire} from 'module';
|
|
2
|
+
const require = topLevelCreateRequire(import.meta.url);
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
5
|
+
/**
|
|
6
|
+
* @license
|
|
7
|
+
* Copyright 2026 Google LLC
|
|
8
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
9
|
+
*/
|
|
10
|
+
import {
|
|
11
|
+
FinishReason
|
|
12
|
+
} from "@google/genai";
|
|
13
|
+
import { removeClientFunctionCallId } from "../agents/processors/content_processor_utils.js";
|
|
14
|
+
import { logger } from "../utils/logger.js";
|
|
15
|
+
import { createLlmResponse } from "./llm_response.js";
|
|
16
|
+
const MAX_RESUMES = 256;
|
|
17
|
+
function resumeToken(response) {
|
|
18
|
+
var _a;
|
|
19
|
+
const candidate = (_a = response.candidates) == null ? void 0 : _a[0];
|
|
20
|
+
if ((candidate == null ? void 0 : candidate.finishReason) !== FinishReason.CONTINUATION) {
|
|
21
|
+
return void 0;
|
|
22
|
+
}
|
|
23
|
+
if (!candidate.continuationToken) {
|
|
24
|
+
logger.warn(
|
|
25
|
+
"The model paused the generation for continuation, but the response carries no continuation token, so the partial output is returned."
|
|
26
|
+
);
|
|
27
|
+
return void 0;
|
|
28
|
+
}
|
|
29
|
+
return candidate.continuationToken;
|
|
30
|
+
}
|
|
31
|
+
__name(resumeToken, "resumeToken");
|
|
32
|
+
const _GeminiContinuation = class _GeminiContinuation {
|
|
33
|
+
constructor(contents, config) {
|
|
34
|
+
this.config = config;
|
|
35
|
+
this.parts = [];
|
|
36
|
+
this.resumes = 0;
|
|
37
|
+
this.contents = [...contents];
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Adds one request's output to the generation: its parts to the content and
|
|
41
|
+
* its usage to the total.
|
|
42
|
+
*/
|
|
43
|
+
addOutput(newParts, newUsage) {
|
|
44
|
+
this.usage = addUsage(this.usage, newUsage);
|
|
45
|
+
this.appendParts(newParts);
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Returns the request that resumes the generation after a request that
|
|
49
|
+
* ended with `nextToken`. Returns undefined if the request did not pause,
|
|
50
|
+
* returned the previous token again, or the generation was already resumed
|
|
51
|
+
* {@link MAX_RESUMES} times.
|
|
52
|
+
*/
|
|
53
|
+
resumeRequest(nextToken) {
|
|
54
|
+
if (nextToken === void 0) {
|
|
55
|
+
return void 0;
|
|
56
|
+
}
|
|
57
|
+
if (!this.willResume(nextToken)) {
|
|
58
|
+
if (nextToken === this.token) {
|
|
59
|
+
logger.warn(
|
|
60
|
+
"The model returned the same continuation token twice; returning the output generated so far."
|
|
61
|
+
);
|
|
62
|
+
} else {
|
|
63
|
+
logger.warn(
|
|
64
|
+
`The model paused the generation ${MAX_RESUMES} times; returning the output generated so far.`
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
return void 0;
|
|
68
|
+
}
|
|
69
|
+
this.token = nextToken;
|
|
70
|
+
this.resumes++;
|
|
71
|
+
logger.debug("The model paused the generation; resuming it.");
|
|
72
|
+
return this.buildResumeRequest(nextToken);
|
|
73
|
+
}
|
|
74
|
+
/** Returns `response` with the content and usage of all requests, if resumed. */
|
|
75
|
+
complete(response) {
|
|
76
|
+
var _a;
|
|
77
|
+
if (!this.resumed) {
|
|
78
|
+
return createLlmResponse(response);
|
|
79
|
+
}
|
|
80
|
+
const candidate = (_a = response.candidates) == null ? void 0 : _a[0];
|
|
81
|
+
return createLlmResponse({
|
|
82
|
+
...response,
|
|
83
|
+
usageMetadata: this.usage,
|
|
84
|
+
candidates: candidate && this.parts.length > 0 ? [{ ...candidate, content: this.content() }] : response.candidates
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Returns the final streamed `response`, with usage summed across all
|
|
89
|
+
* requests if resumed.
|
|
90
|
+
*/
|
|
91
|
+
withSummedUsage(response) {
|
|
92
|
+
return this.resumed ? { ...response, usageMetadata: this.usage } : response;
|
|
93
|
+
}
|
|
94
|
+
/** Returns whether a request that ended with `nextToken` is resumed. */
|
|
95
|
+
willResume(nextToken) {
|
|
96
|
+
return nextToken !== this.token && this.resumes < MAX_RESUMES;
|
|
97
|
+
}
|
|
98
|
+
/** Whether the generation took more than one request. */
|
|
99
|
+
get resumed() {
|
|
100
|
+
return this.token !== void 0;
|
|
101
|
+
}
|
|
102
|
+
content() {
|
|
103
|
+
return { role: "model", parts: [...this.parts] };
|
|
104
|
+
}
|
|
105
|
+
buildResumeRequest(nextToken) {
|
|
106
|
+
const contents = [...this.contents];
|
|
107
|
+
if (this.parts.length > 0) {
|
|
108
|
+
const content = structuredClone(this.content());
|
|
109
|
+
removeClientFunctionCallId(content);
|
|
110
|
+
contents.push(content);
|
|
111
|
+
}
|
|
112
|
+
return {
|
|
113
|
+
contents,
|
|
114
|
+
config: { ...this.config, continuationToken: nextToken }
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
/** Appends `newParts`, joining text as the streaming aggregator does. */
|
|
118
|
+
appendParts(newParts) {
|
|
119
|
+
for (const part of newParts) {
|
|
120
|
+
const last = this.parts.length - 1;
|
|
121
|
+
const previous = last >= 0 ? this.parts[last] : void 0;
|
|
122
|
+
if (previous && canJoin(previous, part)) {
|
|
123
|
+
const joined = { text: previous.text + part.text };
|
|
124
|
+
if (previous.thought !== void 0) {
|
|
125
|
+
joined.thought = previous.thought;
|
|
126
|
+
}
|
|
127
|
+
const signature = previous.thoughtSignature || part.thoughtSignature;
|
|
128
|
+
if (signature !== void 0) {
|
|
129
|
+
joined.thoughtSignature = signature;
|
|
130
|
+
}
|
|
131
|
+
this.parts[last] = joined;
|
|
132
|
+
} else {
|
|
133
|
+
this.parts.push(part);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
__name(_GeminiContinuation, "GeminiContinuation");
|
|
139
|
+
let GeminiContinuation = _GeminiContinuation;
|
|
140
|
+
function canJoin(first, second) {
|
|
141
|
+
var _a, _b;
|
|
142
|
+
return isText(first) && isText(second) && ((_a = first.thought) != null ? _a : false) === ((_b = second.thought) != null ? _b : false);
|
|
143
|
+
}
|
|
144
|
+
__name(canJoin, "canJoin");
|
|
145
|
+
function isText(part) {
|
|
146
|
+
if (!part.text) {
|
|
147
|
+
return false;
|
|
148
|
+
}
|
|
149
|
+
return Object.entries(part).every(
|
|
150
|
+
([key, value]) => value === void 0 || key === "text" || key === "thought" || key === "thoughtSignature"
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
__name(isText, "isText");
|
|
154
|
+
const COUNT_FIELDS = [
|
|
155
|
+
"promptTokenCount",
|
|
156
|
+
"candidatesTokenCount",
|
|
157
|
+
"totalTokenCount",
|
|
158
|
+
"cachedContentTokenCount",
|
|
159
|
+
"thoughtsTokenCount",
|
|
160
|
+
"toolUsePromptTokenCount"
|
|
161
|
+
];
|
|
162
|
+
const MODALITY_FIELDS = [
|
|
163
|
+
"promptTokensDetails",
|
|
164
|
+
"candidatesTokensDetails",
|
|
165
|
+
"cacheTokensDetails",
|
|
166
|
+
"toolUsePromptTokensDetails"
|
|
167
|
+
];
|
|
168
|
+
function addUsage(total, next) {
|
|
169
|
+
var _a, _b;
|
|
170
|
+
if (!total || !next) {
|
|
171
|
+
return total != null ? total : next;
|
|
172
|
+
}
|
|
173
|
+
const combined = { ...next };
|
|
174
|
+
for (const field of COUNT_FIELDS) {
|
|
175
|
+
if (total[field] !== void 0 || next[field] !== void 0) {
|
|
176
|
+
combined[field] = ((_a = total[field]) != null ? _a : 0) + ((_b = next[field]) != null ? _b : 0);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
for (const field of MODALITY_FIELDS) {
|
|
180
|
+
if (total[field] !== void 0 || next[field] !== void 0) {
|
|
181
|
+
combined[field] = addModalityCounts(total[field], next[field]);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
return combined;
|
|
185
|
+
}
|
|
186
|
+
__name(addUsage, "addUsage");
|
|
187
|
+
function addModalityCounts(first, second) {
|
|
188
|
+
var _a, _b;
|
|
189
|
+
const totals = /* @__PURE__ */ new Map();
|
|
190
|
+
for (const count of [...first != null ? first : [], ...second != null ? second : []]) {
|
|
191
|
+
totals.set(
|
|
192
|
+
count.modality,
|
|
193
|
+
((_a = totals.get(count.modality)) != null ? _a : 0) + ((_b = count.tokenCount) != null ? _b : 0)
|
|
194
|
+
);
|
|
195
|
+
}
|
|
196
|
+
return [...totals].map(
|
|
197
|
+
([modality, tokenCount]) => modality === void 0 ? { tokenCount } : { modality, tokenCount }
|
|
198
|
+
);
|
|
199
|
+
}
|
|
200
|
+
__name(addModalityCounts, "addModalityCounts");
|
|
201
|
+
export {
|
|
202
|
+
GeminiContinuation,
|
|
203
|
+
MAX_RESUMES,
|
|
204
|
+
resumeToken
|
|
205
|
+
};
|