@google/adk 0.5.0 → 0.6.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/README.md +1 -1
- package/dist/cjs/a2a/a2a_event.js +53 -26
- package/dist/cjs/a2a/a2a_remote_agent.js +179 -0
- package/dist/cjs/a2a/a2a_remote_agent_run_processor.js +205 -0
- package/dist/cjs/a2a/a2a_remote_agent_utils.js +165 -0
- package/dist/cjs/a2a/agent_card.js +380 -0
- package/dist/cjs/a2a/agent_executor.js +221 -0
- package/dist/cjs/a2a/agent_to_a2a.js +115 -0
- package/dist/cjs/a2a/event_processor_utils.js +180 -0
- package/dist/cjs/a2a/executor_context.js +1 -1
- package/dist/cjs/a2a/metadata_converter_utils.js +1 -0
- package/dist/cjs/a2a/part_converter_utils.js +24 -13
- package/dist/cjs/agents/llm_agent.js +17 -0
- package/dist/cjs/agents/{content_processor_utils.js → processors/content_processor_utils.js} +21 -2
- package/dist/cjs/agents/processors/content_request_processor.js +24 -3
- package/dist/cjs/agents/processors/context_compactor_request_processor.js +61 -0
- package/dist/cjs/agents/processors/instructions_llm_request_processor.js +1 -1
- package/dist/cjs/artifacts/file_artifact_service.js +35 -4
- package/dist/cjs/common.js +37 -0
- package/dist/cjs/context/base_context_compactor.js +27 -0
- package/dist/cjs/context/summarizers/base_summarizer.js +27 -0
- package/dist/cjs/context/summarizers/llm_summarizer.js +93 -0
- package/dist/cjs/context/token_based_context_compactor.js +135 -0
- package/dist/cjs/context/truncating_context_compactor.js +58 -0
- package/dist/cjs/events/compacted_event.js +53 -0
- package/dist/cjs/index.js +38 -14
- package/dist/cjs/index.js.map +4 -4
- package/dist/cjs/memory/in_memory_memory_service.js +1 -1
- package/dist/cjs/runner/runner.js +19 -10
- package/dist/cjs/sessions/db/operations.js +4 -14
- package/dist/cjs/sessions/state.js +2 -2
- package/dist/cjs/tools/agent_tool.js +6 -5
- package/dist/cjs/tools/load_artifacts_tool.js +188 -0
- package/dist/cjs/tools/load_memory_tool.js +107 -0
- package/dist/cjs/tools/preload_memory_tool.js +109 -0
- package/dist/cjs/utils/gemini_schema_util.js +54 -12
- package/dist/cjs/utils/logger.js +1 -0
- package/dist/cjs/version.js +1 -1
- package/dist/esm/a2a/a2a_event.js +52 -26
- package/dist/esm/a2a/a2a_remote_agent.js +153 -0
- package/dist/esm/a2a/a2a_remote_agent_run_processor.js +182 -0
- package/dist/esm/a2a/a2a_remote_agent_utils.js +131 -0
- package/dist/esm/a2a/agent_card.js +340 -0
- package/dist/esm/a2a/agent_executor.js +202 -0
- package/dist/esm/a2a/agent_to_a2a.js +80 -0
- package/dist/esm/a2a/event_processor_utils.js +159 -0
- package/dist/esm/a2a/executor_context.js +1 -1
- package/dist/esm/a2a/metadata_converter_utils.js +1 -0
- package/dist/esm/a2a/part_converter_utils.js +24 -13
- package/dist/esm/agents/llm_agent.js +17 -0
- package/dist/esm/agents/{content_processor_utils.js → processors/content_processor_utils.js} +23 -2
- package/dist/esm/agents/processors/content_request_processor.js +25 -4
- package/dist/esm/agents/processors/context_compactor_request_processor.js +31 -0
- package/dist/esm/agents/processors/instructions_llm_request_processor.js +1 -1
- package/dist/esm/artifacts/file_artifact_service.js +30 -3
- package/dist/esm/common.js +33 -1
- package/dist/esm/context/base_context_compactor.js +5 -0
- package/dist/esm/context/summarizers/base_summarizer.js +5 -0
- package/dist/esm/context/summarizers/llm_summarizer.js +65 -0
- package/dist/esm/context/token_based_context_compactor.js +105 -0
- package/dist/esm/context/truncating_context_compactor.js +28 -0
- package/dist/esm/events/compacted_event.js +22 -0
- package/dist/esm/index.js +38 -14
- package/dist/esm/index.js.map +4 -4
- package/dist/esm/memory/in_memory_memory_service.js +1 -1
- package/dist/esm/runner/runner.js +17 -9
- package/dist/esm/sessions/db/operations.js +4 -14
- package/dist/esm/sessions/state.js +2 -2
- package/dist/esm/tools/agent_tool.js +6 -5
- package/dist/esm/tools/load_artifacts_tool.js +159 -0
- package/dist/esm/tools/load_memory_tool.js +78 -0
- package/dist/esm/tools/preload_memory_tool.js +80 -0
- package/dist/esm/utils/gemini_schema_util.js +54 -12
- package/dist/esm/utils/logger.js +1 -0
- package/dist/esm/version.js +1 -1
- package/dist/types/a2a/a2a_event.d.ts +23 -6
- package/dist/types/a2a/a2a_remote_agent.d.ts +67 -0
- package/dist/types/a2a/a2a_remote_agent_run_processor.d.ts +31 -0
- package/dist/types/a2a/a2a_remote_agent_utils.d.ts +38 -0
- package/dist/types/a2a/agent_card.d.ts +23 -0
- package/dist/types/a2a/agent_executor.d.ts +52 -0
- package/dist/types/a2a/agent_to_a2a.d.ts +45 -0
- package/dist/types/a2a/event_processor_utils.d.ts +24 -0
- package/dist/types/a2a/executor_context.d.ts +1 -1
- package/dist/types/a2a/metadata_converter_utils.d.ts +2 -1
- package/dist/types/agents/llm_agent.d.ts +6 -0
- package/dist/types/agents/loop_agent.d.ts +1 -1
- package/dist/types/agents/{content_processor_utils.d.ts → processors/content_processor_utils.d.ts} +1 -1
- package/dist/types/agents/processors/context_compactor_request_processor.d.ts +22 -0
- package/dist/types/artifacts/file_artifact_service.d.ts +4 -0
- package/dist/types/common.d.ts +16 -1
- package/dist/types/context/base_context_compactor.d.ts +24 -0
- package/dist/types/context/summarizers/base_summarizer.d.ts +19 -0
- package/dist/types/context/summarizers/llm_summarizer.d.ts +23 -0
- package/dist/types/context/token_based_context_compactor.d.ts +33 -0
- package/dist/types/context/truncating_context_compactor.d.ts +24 -0
- package/dist/types/events/compacted_event.d.ts +33 -0
- package/dist/types/index.d.ts +8 -0
- package/dist/types/runner/runner.d.ts +13 -0
- package/dist/types/sessions/db/operations.d.ts +2 -3
- package/dist/types/tools/load_artifacts_tool.d.ts +21 -0
- package/dist/types/tools/load_memory_tool.d.ts +22 -0
- package/dist/types/tools/preload_memory_tool.d.ts +23 -0
- package/dist/types/version.d.ts +1 -1
- package/dist/web/a2a/a2a_event.js +52 -26
- package/dist/web/a2a/a2a_remote_agent.js +184 -0
- package/dist/web/a2a/a2a_remote_agent_run_processor.js +182 -0
- package/dist/web/a2a/a2a_remote_agent_utils.js +131 -0
- package/dist/web/a2a/agent_card.js +340 -0
- package/dist/web/a2a/agent_executor.js +216 -0
- package/dist/web/a2a/agent_to_a2a.js +80 -0
- package/dist/web/a2a/event_processor_utils.js +168 -0
- package/dist/web/a2a/executor_context.js +1 -1
- package/dist/web/a2a/metadata_converter_utils.js +1 -0
- package/dist/web/a2a/part_converter_utils.js +24 -13
- package/dist/web/agents/llm_agent.js +17 -0
- package/dist/web/agents/{content_processor_utils.js → processors/content_processor_utils.js} +22 -2
- package/dist/web/agents/processors/content_request_processor.js +25 -4
- package/dist/web/agents/processors/context_compactor_request_processor.js +49 -0
- package/dist/web/agents/processors/instructions_llm_request_processor.js +1 -1
- package/dist/web/artifacts/file_artifact_service.js +30 -3
- package/dist/web/common.js +33 -1
- package/dist/web/context/base_context_compactor.js +5 -0
- package/dist/web/context/summarizers/base_summarizer.js +5 -0
- package/dist/web/context/summarizers/llm_summarizer.js +74 -0
- package/dist/web/context/token_based_context_compactor.js +105 -0
- package/dist/web/context/truncating_context_compactor.js +28 -0
- package/dist/web/events/compacted_event.js +40 -0
- package/dist/web/index.js +1 -1
- package/dist/web/index.js.map +4 -4
- package/dist/web/memory/in_memory_memory_service.js +1 -1
- package/dist/web/runner/runner.js +17 -9
- package/dist/web/sessions/db/operations.js +4 -14
- package/dist/web/sessions/state.js +2 -2
- package/dist/web/tools/agent_tool.js +6 -5
- package/dist/web/tools/load_artifacts_tool.js +150 -0
- package/dist/web/tools/load_memory_tool.js +77 -0
- package/dist/web/tools/preload_memory_tool.js +75 -0
- package/dist/web/utils/gemini_schema_util.js +85 -12
- package/dist/web/utils/logger.js +1 -0
- package/dist/web/version.js +1 -1
- package/package.json +7 -5
|
@@ -58,7 +58,7 @@ function getUserKey(appName, userId) {
|
|
|
58
58
|
}
|
|
59
59
|
function extractWordsLower(text) {
|
|
60
60
|
return new Set(
|
|
61
|
-
[...text.matchAll(/[A-Za-z]+/)].map((match) => match[0].toLowerCase())
|
|
61
|
+
[...text.matchAll(/[A-Za-z]+/g)].map((match) => match[0].toLowerCase())
|
|
62
62
|
);
|
|
63
63
|
}
|
|
64
64
|
function formatTimestamp(timestamp) {
|
|
@@ -51,6 +51,7 @@ var __forAwait = (obj, it, method) => (it = obj[__knownSymbol("asyncIterator")])
|
|
|
51
51
|
* Copyright 2025 Google LLC
|
|
52
52
|
* SPDX-License-Identifier: Apache-2.0
|
|
53
53
|
*/
|
|
54
|
+
var _a;
|
|
54
55
|
import { createPartFromText } from "@google/genai";
|
|
55
56
|
import { context, trace } from "@opentelemetry/api";
|
|
56
57
|
import {
|
|
@@ -72,12 +73,18 @@ import {
|
|
|
72
73
|
} from "../telemetry/tracing.js";
|
|
73
74
|
import { logger } from "../utils/logger.js";
|
|
74
75
|
import { isGemini2OrAbove } from "../utils/model_name.js";
|
|
76
|
+
const RUNNER_SIGNATURE_SYMBOL = Symbol.for("google.adk.runner");
|
|
77
|
+
function isRunner(obj) {
|
|
78
|
+
return typeof obj === "object" && obj !== null && RUNNER_SIGNATURE_SYMBOL in obj && obj[RUNNER_SIGNATURE_SYMBOL] === true;
|
|
79
|
+
}
|
|
80
|
+
_a = RUNNER_SIGNATURE_SYMBOL;
|
|
75
81
|
class Runner {
|
|
76
82
|
constructor(input) {
|
|
77
|
-
|
|
83
|
+
this[_a] = true;
|
|
84
|
+
var _a2;
|
|
78
85
|
this.appName = input.appName;
|
|
79
86
|
this.agent = input.agent;
|
|
80
|
-
this.pluginManager = new PluginManager((
|
|
87
|
+
this.pluginManager = new PluginManager((_a2 = input.plugins) != null ? _a2 : []);
|
|
81
88
|
this.artifactService = input.artifactService;
|
|
82
89
|
this.sessionService = input.sessionService;
|
|
83
90
|
this.memoryService = input.memoryService;
|
|
@@ -141,7 +148,7 @@ class Runner {
|
|
|
141
148
|
this,
|
|
142
149
|
function() {
|
|
143
150
|
return __asyncGenerator(this, null, function* () {
|
|
144
|
-
var
|
|
151
|
+
var _a2;
|
|
145
152
|
const session = yield new __await(this.sessionService.getSession({
|
|
146
153
|
appName: this.appName,
|
|
147
154
|
userId,
|
|
@@ -186,7 +193,7 @@ class Runner {
|
|
|
186
193
|
newMessage = pluginUserMessage;
|
|
187
194
|
}
|
|
188
195
|
if (newMessage) {
|
|
189
|
-
if (!((
|
|
196
|
+
if (!((_a2 = newMessage.parts) == null ? void 0 : _a2.length)) {
|
|
190
197
|
throw new Error("No parts in the newMessage.");
|
|
191
198
|
}
|
|
192
199
|
if (runConfig.saveInputBlobsAsArtifacts) {
|
|
@@ -276,8 +283,8 @@ class Runner {
|
|
|
276
283
|
* @param message The message containing parts to process.
|
|
277
284
|
*/
|
|
278
285
|
async saveArtifacts(invocationId, userId, sessionId, message) {
|
|
279
|
-
var
|
|
280
|
-
if (!this.artifactService || !((
|
|
286
|
+
var _a2;
|
|
287
|
+
if (!this.artifactService || !((_a2 = message.parts) == null ? void 0 : _a2.length)) {
|
|
281
288
|
return;
|
|
282
289
|
}
|
|
283
290
|
for (let i = 0; i < message.parts.length; i++) {
|
|
@@ -358,12 +365,12 @@ class Runner {
|
|
|
358
365
|
// TODO - b/425992518: Implement runLive and related methods.
|
|
359
366
|
}
|
|
360
367
|
function findEventByLastFunctionResponseId(events) {
|
|
361
|
-
var
|
|
368
|
+
var _a2, _b, _c, _d;
|
|
362
369
|
if (!events.length) {
|
|
363
370
|
return null;
|
|
364
371
|
}
|
|
365
372
|
const lastEvent = events[events.length - 1];
|
|
366
|
-
const functionCallId = (_d = (_c = (_b = (
|
|
373
|
+
const functionCallId = (_d = (_c = (_b = (_a2 = lastEvent.content) == null ? void 0 : _a2.parts) == null ? void 0 : _b.find(
|
|
367
374
|
(part) => part.functionResponse
|
|
368
375
|
)) == null ? void 0 : _c.functionResponse) == null ? void 0 : _d.id;
|
|
369
376
|
if (!functionCallId) {
|
|
@@ -384,5 +391,6 @@ function findEventByLastFunctionResponseId(events) {
|
|
|
384
391
|
return null;
|
|
385
392
|
}
|
|
386
393
|
export {
|
|
387
|
-
Runner
|
|
394
|
+
Runner,
|
|
395
|
+
isRunner
|
|
388
396
|
};
|
|
@@ -25,7 +25,6 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
25
25
|
* Copyright 2026 Google LLC
|
|
26
26
|
* SPDX-License-Identifier: Apache-2.0
|
|
27
27
|
*/
|
|
28
|
-
import { MikroORM } from "@mikro-orm/core";
|
|
29
28
|
import {
|
|
30
29
|
ENTITIES,
|
|
31
30
|
SCHEMA_VERSION_1_JSON,
|
|
@@ -61,9 +60,10 @@ async function getConnectionOptionsFromUri(uri) {
|
|
|
61
60
|
}
|
|
62
61
|
const { host, port, username, password, pathname } = new URL(uri);
|
|
63
62
|
const hostName = host.split(":")[0];
|
|
63
|
+
const dbName = uri.startsWith("sqlite://") ? uri.substring("sqlite://".length) : pathname.slice(1);
|
|
64
64
|
return {
|
|
65
65
|
entities: ENTITIES,
|
|
66
|
-
dbName
|
|
66
|
+
dbName,
|
|
67
67
|
host: hostName,
|
|
68
68
|
port: port ? parseInt(port) : void 0,
|
|
69
69
|
user: username,
|
|
@@ -71,19 +71,9 @@ async function getConnectionOptionsFromUri(uri) {
|
|
|
71
71
|
driver
|
|
72
72
|
};
|
|
73
73
|
}
|
|
74
|
-
async function ensureDatabaseCreated(
|
|
75
|
-
let orm;
|
|
76
|
-
if (ormOrUrlOrOptions instanceof MikroORM) {
|
|
77
|
-
orm = ormOrUrlOrOptions;
|
|
78
|
-
} else if (typeof ormOrUrlOrOptions === "string") {
|
|
79
|
-
orm = await MikroORM.init(
|
|
80
|
-
await getConnectionOptionsFromUri(ormOrUrlOrOptions)
|
|
81
|
-
);
|
|
82
|
-
} else {
|
|
83
|
-
orm = await MikroORM.init(ormOrUrlOrOptions);
|
|
84
|
-
}
|
|
74
|
+
async function ensureDatabaseCreated(orm) {
|
|
85
75
|
await orm.schema.ensureDatabase();
|
|
86
|
-
await orm.schema.updateSchema();
|
|
76
|
+
await orm.schema.updateSchema({ safe: true });
|
|
87
77
|
}
|
|
88
78
|
async function validateDatabaseSchemaVersion(orm) {
|
|
89
79
|
const em = orm.em.fork();
|
|
@@ -69,8 +69,8 @@ class State {
|
|
|
69
69
|
* @param delta The delta to update the state with.
|
|
70
70
|
*/
|
|
71
71
|
update(delta) {
|
|
72
|
-
|
|
73
|
-
|
|
72
|
+
Object.assign(this.delta, delta);
|
|
73
|
+
Object.assign(this.value, delta);
|
|
74
74
|
}
|
|
75
75
|
/**
|
|
76
76
|
* Returns the state as a plain JSON object.
|
|
@@ -65,7 +65,7 @@ class AgentTool extends (_b = BaseTool, _a = AGENT_TOOL_SIGNATURE_SYMBOL, _b) {
|
|
|
65
65
|
args,
|
|
66
66
|
toolContext
|
|
67
67
|
}) {
|
|
68
|
-
var _a2, _b2;
|
|
68
|
+
var _a2, _b2, _c, _d;
|
|
69
69
|
if (this.skipSummarization) {
|
|
70
70
|
toolContext.actions.skipSummarization = true;
|
|
71
71
|
}
|
|
@@ -84,13 +84,14 @@ class AgentTool extends (_b = BaseTool, _a = AGENT_TOOL_SIGNATURE_SYMBOL, _b) {
|
|
|
84
84
|
appName: this.agent.name,
|
|
85
85
|
agent: this.agent,
|
|
86
86
|
artifactService: new ForwardingArtifactService(toolContext),
|
|
87
|
-
sessionService: new InMemorySessionService(),
|
|
88
|
-
memoryService: new InMemoryMemoryService(),
|
|
87
|
+
sessionService: (_a2 = toolContext.invocationContext.sessionService) != null ? _a2 : new InMemorySessionService(),
|
|
88
|
+
memoryService: (_b2 = toolContext.invocationContext.memoryService) != null ? _b2 : new InMemoryMemoryService(),
|
|
89
89
|
credentialService: toolContext.invocationContext.credentialService
|
|
90
90
|
});
|
|
91
91
|
const session = await runner.sessionService.createSession({
|
|
92
92
|
appName: this.agent.name,
|
|
93
|
-
userId:
|
|
93
|
+
userId: toolContext.invocationContext.userId,
|
|
94
|
+
sessionId: toolContext.invocationContext.session.id,
|
|
94
95
|
state: toolContext.state.toRecord()
|
|
95
96
|
});
|
|
96
97
|
let lastEvent;
|
|
@@ -116,7 +117,7 @@ class AgentTool extends (_b = BaseTool, _a = AGENT_TOOL_SIGNATURE_SYMBOL, _b) {
|
|
|
116
117
|
throw error[0];
|
|
117
118
|
}
|
|
118
119
|
}
|
|
119
|
-
if (!((
|
|
120
|
+
if (!((_d = (_c = lastEvent == null ? void 0 : lastEvent.content) == null ? void 0 : _c.parts) == null ? void 0 : _d.length)) {
|
|
120
121
|
return "";
|
|
121
122
|
}
|
|
122
123
|
const hasOutputSchema = isLlmAgent(this.agent) && this.agent.outputSchema;
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2026 Google LLC
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import { Type } from "@google/genai";
|
|
7
|
+
import { appendInstructions } from "../models/llm_request.js";
|
|
8
|
+
import { getLogger } from "../utils/logger.js";
|
|
9
|
+
import {
|
|
10
|
+
BaseTool
|
|
11
|
+
} from "./base_tool.js";
|
|
12
|
+
const logger = getLogger();
|
|
13
|
+
const GEMINI_SUPPORTED_INLINE_MIME_PREFIXES = ["image/", "audio/", "video/"];
|
|
14
|
+
const GEMINI_SUPPORTED_INLINE_MIME_TYPES = /* @__PURE__ */ new Set(["application/pdf"]);
|
|
15
|
+
const TEXT_LIKE_MIME_TYPES = /* @__PURE__ */ new Set([
|
|
16
|
+
"application/csv",
|
|
17
|
+
"application/json",
|
|
18
|
+
"application/xml"
|
|
19
|
+
]);
|
|
20
|
+
function normalizeMimeType(mimeType) {
|
|
21
|
+
if (!mimeType) {
|
|
22
|
+
return void 0;
|
|
23
|
+
}
|
|
24
|
+
return mimeType.split(";")[0].trim();
|
|
25
|
+
}
|
|
26
|
+
function isInlineMimeTypeSupported(mimeType) {
|
|
27
|
+
const normalized = normalizeMimeType(mimeType);
|
|
28
|
+
if (!normalized) {
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
return GEMINI_SUPPORTED_INLINE_MIME_PREFIXES.some(
|
|
32
|
+
(prefix) => normalized.startsWith(prefix)
|
|
33
|
+
) || GEMINI_SUPPORTED_INLINE_MIME_TYPES.has(normalized);
|
|
34
|
+
}
|
|
35
|
+
function asSafePartForLlm(artifact, artifactName) {
|
|
36
|
+
const inlineData = artifact.inlineData;
|
|
37
|
+
if (!inlineData) {
|
|
38
|
+
return artifact;
|
|
39
|
+
}
|
|
40
|
+
if (isInlineMimeTypeSupported(inlineData.mimeType)) {
|
|
41
|
+
return artifact;
|
|
42
|
+
}
|
|
43
|
+
const mimeType = normalizeMimeType(inlineData.mimeType) || "application/octet-stream";
|
|
44
|
+
const data = inlineData.data;
|
|
45
|
+
if (!data) {
|
|
46
|
+
return {
|
|
47
|
+
text: "[Artifact: ".concat(artifactName, ", type: ").concat(mimeType, ". No inline data was provided.]")
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
const isTextLike = mimeType.startsWith("text/") || TEXT_LIKE_MIME_TYPES.has(mimeType);
|
|
51
|
+
const decodedBuffer = Buffer.from(data, "base64");
|
|
52
|
+
if (isTextLike) {
|
|
53
|
+
try {
|
|
54
|
+
const decoded = decodedBuffer.toString("utf8");
|
|
55
|
+
return { text: decoded };
|
|
56
|
+
} catch (e) {
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
const sizeKb = decodedBuffer.length / 1024;
|
|
60
|
+
return {
|
|
61
|
+
text: "[Binary artifact: ".concat(artifactName, ", type: ").concat(mimeType, ", size: ").concat(sizeKb.toFixed(1), " KB. Content cannot be displayed inline.]")
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
class LoadArtifactsTool extends BaseTool {
|
|
65
|
+
constructor() {
|
|
66
|
+
super({
|
|
67
|
+
name: "load_artifacts",
|
|
68
|
+
description: "Loads artifacts into the session for this request.\n\nNOTE: Call when you need access to artifacts (for example, uploads saved by the web UI)."
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
_getDeclaration() {
|
|
72
|
+
return {
|
|
73
|
+
name: this.name,
|
|
74
|
+
description: this.description,
|
|
75
|
+
parameters: {
|
|
76
|
+
type: Type.OBJECT,
|
|
77
|
+
properties: {
|
|
78
|
+
artifact_names: {
|
|
79
|
+
type: Type.ARRAY,
|
|
80
|
+
items: {
|
|
81
|
+
type: Type.STRING
|
|
82
|
+
},
|
|
83
|
+
description: "The names of the artifacts to load."
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
async runAsync({ args }) {
|
|
90
|
+
const artifactNames = args["artifact_names"] || [];
|
|
91
|
+
return {
|
|
92
|
+
artifact_names: artifactNames,
|
|
93
|
+
status: "artifact contents temporarily inserted and removed. to access these artifacts, call load_artifacts tool again."
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
async processLlmRequest(request) {
|
|
97
|
+
await super.processLlmRequest(request);
|
|
98
|
+
await this.appendArtifactsToLlmRequest(
|
|
99
|
+
request.toolContext,
|
|
100
|
+
request.llmRequest
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
async appendArtifactsToLlmRequest(toolContext, llmRequest) {
|
|
104
|
+
if (!toolContext.invocationContext.artifactService) {
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
const artifactNames = await toolContext.listArtifacts();
|
|
108
|
+
if (!artifactNames || artifactNames.length === 0) {
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
appendInstructions(llmRequest, [
|
|
112
|
+
"You have a list of artifacts:\n ".concat(JSON.stringify(
|
|
113
|
+
artifactNames
|
|
114
|
+
), "\n\n When the user asks questions about any of the artifacts, you should call the\n `load_artifacts` function to load the artifact. Always call load_artifacts\n before answering questions related to the artifacts, regardless of whether the\n artifacts have been loaded before. Do not depend on prior answers about the\n artifacts.")
|
|
115
|
+
]);
|
|
116
|
+
const contents = llmRequest.contents;
|
|
117
|
+
if (contents && contents.length > 0) {
|
|
118
|
+
const lastContent = contents[contents.length - 1];
|
|
119
|
+
if (lastContent.role === "user" && lastContent.parts && lastContent.parts.length > 0) {
|
|
120
|
+
const functionResponsePart = lastContent.parts[0];
|
|
121
|
+
const functionResponse = functionResponsePart.functionResponse;
|
|
122
|
+
if (functionResponse && functionResponse.name === "load_artifacts") {
|
|
123
|
+
const response = functionResponse.response || {};
|
|
124
|
+
const namesToLoad = response["artifact_names"] || [];
|
|
125
|
+
for (const artifactName of namesToLoad) {
|
|
126
|
+
let artifact = await toolContext.loadArtifact(artifactName);
|
|
127
|
+
if (!artifact && !artifactName.startsWith("user:")) {
|
|
128
|
+
const prefixedName = "user:".concat(artifactName);
|
|
129
|
+
artifact = await toolContext.loadArtifact(prefixedName);
|
|
130
|
+
}
|
|
131
|
+
if (!artifact) {
|
|
132
|
+
logger.warn('Artifact "'.concat(artifactName, '" not found, skipping'));
|
|
133
|
+
continue;
|
|
134
|
+
}
|
|
135
|
+
const artifactPart = asSafePartForLlm(artifact, artifactName);
|
|
136
|
+
llmRequest.contents.push({
|
|
137
|
+
role: "user",
|
|
138
|
+
parts: [{ text: "Artifact ".concat(artifactName, " is:") }, artifactPart]
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
const LOAD_ARTIFACTS = new LoadArtifactsTool();
|
|
147
|
+
export {
|
|
148
|
+
LOAD_ARTIFACTS,
|
|
149
|
+
LoadArtifactsTool
|
|
150
|
+
};
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2026 Google LLC
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import { Type } from "@google/genai";
|
|
7
|
+
import { appendInstructions } from "../models/llm_request.js";
|
|
8
|
+
import {
|
|
9
|
+
BaseTool
|
|
10
|
+
} from "./base_tool.js";
|
|
11
|
+
class LoadMemoryTool extends BaseTool {
|
|
12
|
+
constructor() {
|
|
13
|
+
super({
|
|
14
|
+
name: "load_memory",
|
|
15
|
+
description: "Loads the memory for the current user.\n\nNOTE: Currently this tool only uses text part from the memory."
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
_getDeclaration() {
|
|
19
|
+
return {
|
|
20
|
+
name: this.name,
|
|
21
|
+
description: this.description,
|
|
22
|
+
parameters: {
|
|
23
|
+
type: Type.OBJECT,
|
|
24
|
+
properties: {
|
|
25
|
+
query: {
|
|
26
|
+
type: Type.STRING,
|
|
27
|
+
description: "The query to load the memory for."
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
required: ["query"]
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
async runAsync({
|
|
35
|
+
args,
|
|
36
|
+
toolContext
|
|
37
|
+
}) {
|
|
38
|
+
try {
|
|
39
|
+
const query = args["query"];
|
|
40
|
+
if (!toolContext.invocationContext.memoryService) {
|
|
41
|
+
throw new Error("Memory service is not initialized.");
|
|
42
|
+
}
|
|
43
|
+
const searchMemoryResponse = await toolContext.searchMemory(query);
|
|
44
|
+
return {
|
|
45
|
+
memories: searchMemoryResponse.memories.map((m) => {
|
|
46
|
+
var _a, _b;
|
|
47
|
+
return {
|
|
48
|
+
// Join all text parts by a space, or empty string if no text parts
|
|
49
|
+
content: (_b = (_a = m.content.parts) == null ? void 0 : _a.map((p) => {
|
|
50
|
+
var _a2;
|
|
51
|
+
return (_a2 = p.text) != null ? _a2 : "";
|
|
52
|
+
}).join(" ")) != null ? _b : "",
|
|
53
|
+
author: m.author,
|
|
54
|
+
timestamp: m.timestamp
|
|
55
|
+
};
|
|
56
|
+
})
|
|
57
|
+
};
|
|
58
|
+
} catch (e) {
|
|
59
|
+
console.error("ERROR in LoadMemoryTool runAsync:", e);
|
|
60
|
+
throw e;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
async processLlmRequest(request) {
|
|
64
|
+
await super.processLlmRequest(request);
|
|
65
|
+
if (!request.toolContext.invocationContext.memoryService) {
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
appendInstructions(request.llmRequest, [
|
|
69
|
+
"You have memory. You can use it to answer questions. If any questions need\nyou to look up the memory, you should call load_memory function with a query."
|
|
70
|
+
]);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
const LOAD_MEMORY = new LoadMemoryTool();
|
|
74
|
+
export {
|
|
75
|
+
LOAD_MEMORY,
|
|
76
|
+
LoadMemoryTool
|
|
77
|
+
};
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2026 Google LLC
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import { appendInstructions } from "../models/llm_request.js";
|
|
7
|
+
import { logger } from "../utils/logger.js";
|
|
8
|
+
import {
|
|
9
|
+
BaseTool
|
|
10
|
+
} from "./base_tool.js";
|
|
11
|
+
class PreloadMemoryTool extends BaseTool {
|
|
12
|
+
constructor() {
|
|
13
|
+
super({
|
|
14
|
+
// Name and description are not used because this tool only
|
|
15
|
+
// changes llm_request.
|
|
16
|
+
name: "preload_memory",
|
|
17
|
+
description: "preload_memory"
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
async runAsync({
|
|
21
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
22
|
+
args,
|
|
23
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
24
|
+
toolContext
|
|
25
|
+
}) {
|
|
26
|
+
throw new Error("PreloadMemoryTool should not be called by model");
|
|
27
|
+
}
|
|
28
|
+
async processLlmRequest(request) {
|
|
29
|
+
var _a, _b, _c;
|
|
30
|
+
await super.processLlmRequest(request);
|
|
31
|
+
const userContent = request.toolContext.userContent;
|
|
32
|
+
if (!userContent || !userContent.parts || !((_a = userContent.parts[0]) == null ? void 0 : _a.text)) {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
const userQuery = userContent.parts[0].text;
|
|
36
|
+
let response;
|
|
37
|
+
try {
|
|
38
|
+
if (!request.toolContext.invocationContext.memoryService) {
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
response = await request.toolContext.searchMemory(userQuery);
|
|
42
|
+
} catch (_) {
|
|
43
|
+
logger.warn("Failed to preload memory for query: ".concat(userQuery));
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
if (!response.memories || response.memories.length === 0) {
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
const memoryTextLines = [];
|
|
50
|
+
for (const memory of response.memories) {
|
|
51
|
+
const timeStr = memory.timestamp ? "Time: ".concat(memory.timestamp) : "";
|
|
52
|
+
if (timeStr) memoryTextLines.push(timeStr);
|
|
53
|
+
const memoryText = (_c = (_b = memory.content.parts) == null ? void 0 : _b.map((p) => {
|
|
54
|
+
var _a2;
|
|
55
|
+
return (_a2 = p.text) != null ? _a2 : "";
|
|
56
|
+
}).join(" ")) != null ? _c : "";
|
|
57
|
+
if (memoryText) {
|
|
58
|
+
memoryTextLines.push(
|
|
59
|
+
memory.author ? "".concat(memory.author, ": ").concat(memoryText) : memoryText
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
if (memoryTextLines.length === 0) {
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
const fullMemoryText = memoryTextLines.join("\n");
|
|
67
|
+
const si = "The following content is from your previous conversations with the user.\nThey may be useful for answering the user's current query.\n<PAST_CONVERSATIONS>\n".concat(fullMemoryText, "\n</PAST_CONVERSATIONS>\n");
|
|
68
|
+
appendInstructions(request.llmRequest, [si]);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
const PRELOAD_MEMORY = new PreloadMemoryTool();
|
|
72
|
+
export {
|
|
73
|
+
PRELOAD_MEMORY,
|
|
74
|
+
PreloadMemoryTool
|
|
75
|
+
};
|
|
@@ -1,3 +1,34 @@
|
|
|
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
|
+
var __objRest = (source, exclude) => {
|
|
21
|
+
var target = {};
|
|
22
|
+
for (var prop in source)
|
|
23
|
+
if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
|
|
24
|
+
target[prop] = source[prop];
|
|
25
|
+
if (source != null && __getOwnPropSymbols)
|
|
26
|
+
for (var prop of __getOwnPropSymbols(source)) {
|
|
27
|
+
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
|
|
28
|
+
target[prop] = source[prop];
|
|
29
|
+
}
|
|
30
|
+
return target;
|
|
31
|
+
};
|
|
1
32
|
/**
|
|
2
33
|
* @license
|
|
3
34
|
* Copyright 2025 Google LLC
|
|
@@ -26,22 +57,50 @@ function toGeminiType(mcpType) {
|
|
|
26
57
|
return Type.ARRAY;
|
|
27
58
|
case "object":
|
|
28
59
|
return Type.OBJECT;
|
|
60
|
+
case "null":
|
|
61
|
+
return Type.NULL;
|
|
29
62
|
default:
|
|
30
63
|
return Type.TYPE_UNSPECIFIED;
|
|
31
64
|
}
|
|
32
65
|
}
|
|
66
|
+
const getTypeFromArrayItem = (mcpType) => {
|
|
67
|
+
var _a, _b;
|
|
68
|
+
if (typeof mcpType === "string") {
|
|
69
|
+
return mcpType.toLowerCase();
|
|
70
|
+
}
|
|
71
|
+
return (_b = (_a = mcpType == null ? void 0 : mcpType.type) == null ? void 0 : _a.toLowerCase) == null ? void 0 : _b.call(_a);
|
|
72
|
+
};
|
|
33
73
|
function toGeminiSchema(mcpSchema) {
|
|
34
74
|
if (!mcpSchema) {
|
|
35
75
|
return void 0;
|
|
36
76
|
}
|
|
37
77
|
function recursiveConvert(mcp) {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
78
|
+
var _a;
|
|
79
|
+
const sourceType = (_a = mcp.anyOf) != null ? _a : mcp.type;
|
|
80
|
+
let isNullable = false;
|
|
81
|
+
let nonNullTypes;
|
|
82
|
+
if (Array.isArray(sourceType)) {
|
|
83
|
+
nonNullTypes = sourceType.filter(
|
|
84
|
+
(t) => getTypeFromArrayItem(t) !== "null"
|
|
85
|
+
);
|
|
86
|
+
isNullable = sourceType.some(
|
|
87
|
+
(t) => getTypeFromArrayItem(t) === "null"
|
|
88
|
+
);
|
|
89
|
+
if (nonNullTypes.length === 1) {
|
|
90
|
+
const nonNullType = nonNullTypes[0];
|
|
91
|
+
if (typeof nonNullType === "object") {
|
|
92
|
+
mcp = nonNullType;
|
|
93
|
+
} else {
|
|
94
|
+
const _b = mcp, { type: _removed, anyOf: _removedAnyOf } = _b, rest = __objRest(_b, ["type", "anyOf"]);
|
|
95
|
+
mcp = __spreadProps(__spreadValues({}, rest), { type: nonNullType });
|
|
96
|
+
}
|
|
97
|
+
} else if (nonNullTypes.length === 0 && isNullable) {
|
|
98
|
+
const _c = mcp, { type: _removed, anyOf: _removedAnyOf } = _c, rest = __objRest(_c, ["type", "anyOf"]);
|
|
99
|
+
mcp = __spreadProps(__spreadValues({}, rest), { type: "null" });
|
|
100
|
+
} else if (typeof mcp.anyOf === "undefined") {
|
|
101
|
+
const anyOfItems = mcp.type.map((t) => ({ type: t }));
|
|
102
|
+
const _d = mcp, { type: _removed } = _d, rest = __objRest(_d, ["type"]);
|
|
103
|
+
mcp = __spreadProps(__spreadValues({}, rest), { anyOf: anyOfItems });
|
|
45
104
|
}
|
|
46
105
|
}
|
|
47
106
|
if (!mcp.type) {
|
|
@@ -49,13 +108,25 @@ function toGeminiSchema(mcpSchema) {
|
|
|
49
108
|
mcp.type = "object";
|
|
50
109
|
} else if (mcp.items) {
|
|
51
110
|
mcp.type = "array";
|
|
111
|
+
} else if (isNullable) {
|
|
112
|
+
mcp.type = "null";
|
|
52
113
|
}
|
|
53
114
|
}
|
|
54
115
|
const geminiType = toGeminiType(mcp.type);
|
|
55
|
-
const geminiSchema = {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
116
|
+
const geminiSchema = {};
|
|
117
|
+
if (mcp.anyOf) {
|
|
118
|
+
geminiSchema.anyOf = mcp.anyOf.map(
|
|
119
|
+
(item) => recursiveConvert(item)
|
|
120
|
+
);
|
|
121
|
+
} else {
|
|
122
|
+
geminiSchema.type = geminiType;
|
|
123
|
+
}
|
|
124
|
+
if (mcp.description) {
|
|
125
|
+
geminiSchema.description = mcp.description;
|
|
126
|
+
}
|
|
127
|
+
if (isNullable && mcp.type !== "null") {
|
|
128
|
+
geminiSchema.nullable = true;
|
|
129
|
+
}
|
|
59
130
|
if (geminiType === Type.OBJECT) {
|
|
60
131
|
geminiSchema.properties = {};
|
|
61
132
|
if (mcp.properties) {
|
|
@@ -65,7 +136,9 @@ function toGeminiSchema(mcpSchema) {
|
|
|
65
136
|
);
|
|
66
137
|
}
|
|
67
138
|
}
|
|
68
|
-
|
|
139
|
+
if (mcp.required) {
|
|
140
|
+
geminiSchema.required = mcp.required;
|
|
141
|
+
}
|
|
69
142
|
} else if (geminiType === Type.ARRAY) {
|
|
70
143
|
if (mcp.items) {
|
|
71
144
|
geminiSchema.items = recursiveConvert(mcp.items);
|
package/dist/web/utils/logger.js
CHANGED
package/dist/web/version.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@google/adk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.1",
|
|
4
4
|
"description": "Google ADK JS",
|
|
5
5
|
"author": "Google",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -41,20 +41,22 @@
|
|
|
41
41
|
},
|
|
42
42
|
"dependencies": {
|
|
43
43
|
"@a2a-js/sdk": "^0.3.10",
|
|
44
|
-
"winston": "^3.19.0",
|
|
45
44
|
"@google/genai": "^1.37.0",
|
|
46
|
-
"@mikro-orm/core": "^6.6.
|
|
45
|
+
"@mikro-orm/core": "^6.6.10",
|
|
47
46
|
"@mikro-orm/reflection": "^6.6.6",
|
|
48
47
|
"@modelcontextprotocol/sdk": "^1.26.0",
|
|
48
|
+
"express": "^4.22.1",
|
|
49
49
|
"google-auth-library": "^10.3.0",
|
|
50
50
|
"lodash-es": "^4.17.23",
|
|
51
|
+
"winston": "^3.19.0",
|
|
51
52
|
"zod": "^4.2.1",
|
|
52
53
|
"zod-to-json-schema": "^3.25.1"
|
|
53
54
|
},
|
|
54
55
|
"devDependencies": {
|
|
56
|
+
"@mikro-orm/sqlite": "^6.6.6",
|
|
57
|
+
"@types/express": "^4.17.25",
|
|
55
58
|
"@types/lodash-es": "^4.17.12",
|
|
56
|
-
"openapi-types": "^12.1.3"
|
|
57
|
-
"@mikro-orm/sqlite": "^6.6.6"
|
|
59
|
+
"openapi-types": "^12.1.3"
|
|
58
60
|
},
|
|
59
61
|
"peerDependencies": {
|
|
60
62
|
"@google-cloud/opentelemetry-cloud-monitoring-exporter": "^0.21.0",
|