@google/adk 0.2.5 → 0.3.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.
- package/dist/cjs/agents/base_agent.js +43 -21
- package/dist/cjs/agents/callback_context.js +4 -1
- package/dist/cjs/agents/content_processor_utils.js +15 -7
- package/dist/cjs/agents/functions.js +79 -29
- package/dist/cjs/agents/llm_agent.js +59 -33
- package/dist/cjs/agents/loop_agent.js +2 -1
- package/dist/cjs/agents/parallel_agent.js +3 -4
- package/dist/cjs/artifacts/gcs_artifact_service.js +28 -20
- package/dist/cjs/artifacts/in_memory_artifact_service.js +18 -4
- package/dist/cjs/auth/auth_handler.js +3 -1
- package/dist/cjs/code_executors/base_code_executor.js +3 -1
- package/dist/cjs/code_executors/built_in_code_executor.js +7 -3
- package/dist/cjs/code_executors/code_executor_context.js +5 -5
- package/dist/cjs/common.js +4 -0
- package/dist/cjs/events/event.js +1 -3
- package/dist/cjs/index.js +19 -19
- package/dist/cjs/index.js.map +4 -4
- package/dist/cjs/memory/in_memory_memory_service.js +3 -1
- package/dist/cjs/models/base_llm.js +8 -4
- package/dist/cjs/models/gemini_llm_connection.js +1 -0
- package/dist/cjs/models/google_llm.js +3 -3
- package/dist/cjs/plugins/base_plugin.js +12 -0
- package/dist/cjs/plugins/logging_plugin.js +50 -13
- package/dist/cjs/plugins/plugin_manager.js +56 -24
- package/dist/cjs/plugins/security_plugin.js +1 -1
- package/dist/cjs/runner/runner.js +110 -95
- package/dist/cjs/sessions/in_memory_session_service.js +38 -14
- package/dist/cjs/telemetry/google_cloud.js +7 -9
- package/dist/cjs/telemetry/setup.js +15 -7
- package/dist/cjs/telemetry/tracing.js +37 -15
- package/dist/cjs/tools/agent_tool.js +8 -4
- package/dist/cjs/tools/base_tool.js +4 -2
- package/dist/cjs/tools/forwarding_artifact_service.js +1 -1
- package/dist/cjs/tools/function_tool.js +1 -2
- package/dist/cjs/tools/google_search_tool.js +1 -2
- package/dist/cjs/tools/mcp/mcp_session_manager.js +16 -10
- package/dist/cjs/tools/mcp/mcp_tool.js +1 -3
- package/dist/cjs/tools/mcp/mcp_toolset.js +1 -1
- package/dist/cjs/utils/env_aware_utils.js +1 -1
- package/dist/cjs/utils/gemini_schema_util.js +9 -4
- package/dist/cjs/utils/logger.js +47 -3
- package/dist/cjs/utils/simple_zod_to_json.js +100 -141
- package/dist/cjs/utils/variant_utils.js +1 -1
- package/dist/cjs/version.js +1 -1
- package/dist/esm/agents/base_agent.js +48 -22
- package/dist/esm/agents/callback_context.js +4 -1
- package/dist/esm/agents/content_processor_utils.js +25 -9
- package/dist/esm/agents/functions.js +83 -29
- package/dist/esm/agents/llm_agent.js +63 -33
- package/dist/esm/agents/loop_agent.js +2 -1
- package/dist/esm/agents/parallel_agent.js +3 -4
- package/dist/esm/artifacts/gcs_artifact_service.js +28 -20
- package/dist/esm/artifacts/in_memory_artifact_service.js +18 -4
- package/dist/esm/auth/auth_handler.js +3 -1
- package/dist/esm/code_executors/base_code_executor.js +3 -1
- package/dist/esm/code_executors/built_in_code_executor.js +7 -3
- package/dist/esm/code_executors/code_executor_context.js +5 -5
- package/dist/esm/common.js +3 -1
- package/dist/esm/events/event.js +1 -3
- package/dist/esm/index.js +19 -19
- package/dist/esm/index.js.map +4 -4
- package/dist/esm/memory/in_memory_memory_service.js +3 -1
- package/dist/esm/models/base_llm.js +8 -4
- package/dist/esm/models/gemini_llm_connection.js +1 -0
- package/dist/esm/models/google_llm.js +8 -4
- package/dist/esm/plugins/base_plugin.js +12 -0
- package/dist/esm/plugins/logging_plugin.js +55 -14
- package/dist/esm/plugins/plugin_manager.js +56 -24
- package/dist/esm/plugins/security_plugin.js +1 -1
- package/dist/esm/runner/runner.js +114 -96
- package/dist/esm/sessions/in_memory_session_service.js +41 -15
- package/dist/esm/telemetry/google_cloud.js +7 -9
- package/dist/esm/telemetry/setup.js +23 -9
- package/dist/esm/telemetry/tracing.js +37 -15
- package/dist/esm/tools/agent_tool.js +8 -4
- package/dist/esm/tools/base_tool.js +4 -2
- package/dist/esm/tools/forwarding_artifact_service.js +1 -1
- package/dist/esm/tools/function_tool.js +1 -2
- package/dist/esm/tools/google_search_tool.js +2 -5
- package/dist/esm/tools/long_running_tool.js +3 -1
- package/dist/esm/tools/mcp/mcp_session_manager.js +22 -12
- package/dist/esm/tools/mcp/mcp_tool.js +1 -3
- package/dist/esm/tools/mcp/mcp_toolset.js +1 -1
- package/dist/esm/utils/env_aware_utils.js +1 -1
- package/dist/esm/utils/gemini_schema_util.js +9 -4
- package/dist/esm/utils/logger.js +43 -2
- package/dist/esm/utils/simple_zod_to_json.js +102 -141
- package/dist/esm/utils/variant_utils.js +1 -1
- package/dist/esm/version.js +1 -1
- package/dist/types/agents/base_agent.d.ts +2 -1
- package/dist/types/agents/callback_context.d.ts +1 -1
- package/dist/types/agents/llm_agent.d.ts +1 -1
- package/dist/types/agents/loop_agent.d.ts +1 -1
- package/dist/types/agents/parallel_agent.d.ts +1 -1
- package/dist/types/artifacts/in_memory_artifact_service.d.ts +3 -3
- package/dist/types/code_executors/built_in_code_executor.d.ts +1 -1
- package/dist/types/code_executors/code_executor_context.d.ts +2 -4
- package/dist/types/common.d.ts +2 -1
- package/dist/types/index.d.ts +3 -3
- package/dist/types/models/base_llm_connection.d.ts +1 -1
- package/dist/types/models/llm_response.d.ts +1 -1
- package/dist/types/plugins/logging_plugin.d.ts +12 -12
- package/dist/types/plugins/plugin_manager.d.ts +12 -12
- package/dist/types/plugins/security_plugin.d.ts +1 -1
- package/dist/types/runner/runner.d.ts +1 -1
- package/dist/types/sessions/in_memory_session_service.d.ts +5 -5
- package/dist/types/telemetry/setup.d.ts +1 -1
- package/dist/types/telemetry/tracing.d.ts +7 -6
- package/dist/types/tools/agent_tool.d.ts +1 -1
- package/dist/types/tools/base_tool.d.ts +1 -1
- package/dist/types/tools/base_toolset.d.ts +2 -1
- package/dist/types/tools/forwarding_artifact_service.d.ts +2 -2
- package/dist/types/tools/function_tool.d.ts +4 -3
- package/dist/types/tools/google_search_tool.d.ts +3 -3
- package/dist/types/tools/mcp/mcp_session_manager.d.ts +10 -3
- package/dist/types/tools/mcp/mcp_toolset.d.ts +1 -2
- package/dist/types/utils/gemini_schema_util.d.ts +4 -12
- package/dist/types/utils/logger.d.ts +11 -10
- package/dist/types/utils/simple_zod_to_json.d.ts +5 -4
- package/dist/types/version.d.ts +1 -1
- package/dist/web/agents/base_agent.js +94 -33
- package/dist/web/agents/callback_context.js +4 -1
- package/dist/web/agents/content_processor_utils.js +25 -9
- package/dist/web/agents/functions.js +83 -29
- package/dist/web/agents/llm_agent.js +117 -54
- package/dist/web/agents/loop_agent.js +2 -1
- package/dist/web/agents/parallel_agent.js +3 -4
- package/dist/web/artifacts/gcs_artifact_service.js +25 -17
- package/dist/web/artifacts/in_memory_artifact_service.js +18 -4
- package/dist/web/auth/auth_handler.js +3 -1
- package/dist/web/code_executors/base_code_executor.js +3 -1
- package/dist/web/code_executors/built_in_code_executor.js +7 -3
- package/dist/web/code_executors/code_executor_context.js +5 -5
- package/dist/web/common.js +3 -1
- package/dist/web/events/event.js +1 -3
- package/dist/web/index.js +1 -1
- package/dist/web/index.js.map +4 -4
- package/dist/web/memory/in_memory_memory_service.js +3 -1
- package/dist/web/models/base_llm.js +8 -4
- package/dist/web/models/gemini_llm_connection.js +1 -0
- package/dist/web/models/google_llm.js +8 -4
- package/dist/web/plugins/base_plugin.js +12 -0
- package/dist/web/plugins/logging_plugin.js +55 -14
- package/dist/web/plugins/plugin_manager.js +56 -24
- package/dist/web/plugins/security_plugin.js +1 -1
- package/dist/web/runner/runner.js +159 -108
- package/dist/web/sessions/in_memory_session_service.js +41 -15
- package/dist/web/telemetry/google_cloud.js +7 -9
- package/dist/web/telemetry/setup.js +23 -9
- package/dist/web/telemetry/tracing.js +37 -15
- package/dist/web/tools/agent_tool.js +8 -4
- package/dist/web/tools/base_tool.js +4 -2
- package/dist/web/tools/forwarding_artifact_service.js +1 -1
- package/dist/web/tools/function_tool.js +1 -2
- package/dist/web/tools/google_search_tool.js +2 -5
- package/dist/web/tools/long_running_tool.js +3 -1
- package/dist/web/tools/mcp/mcp_session_manager.js +22 -12
- package/dist/web/tools/mcp/mcp_tool.js +1 -3
- package/dist/web/tools/mcp/mcp_toolset.js +1 -1
- package/dist/web/utils/env_aware_utils.js +1 -1
- package/dist/web/utils/gemini_schema_util.js +9 -4
- package/dist/web/utils/logger.js +43 -2
- package/dist/web/utils/simple_zod_to_json.js +102 -155
- package/dist/web/utils/variant_utils.js +1 -1
- package/dist/web/version.js +1 -1
- package/package.json +4 -2
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* SPDX-License-Identifier: Apache-2.0
|
|
5
5
|
*/
|
|
6
6
|
import { createPartFromText } from "@google/genai";
|
|
7
|
-
import { trace } from "@opentelemetry/api";
|
|
7
|
+
import { context, trace } from "@opentelemetry/api";
|
|
8
8
|
import {
|
|
9
9
|
InvocationContext,
|
|
10
10
|
newInvocationContextId
|
|
@@ -18,6 +18,10 @@ import {
|
|
|
18
18
|
import { createEvent, getFunctionCalls } from "../events/event.js";
|
|
19
19
|
import { createEventActions } from "../events/event_actions.js";
|
|
20
20
|
import { PluginManager } from "../plugins/plugin_manager.js";
|
|
21
|
+
import {
|
|
22
|
+
runAsyncGeneratorWithOtelContext,
|
|
23
|
+
tracer
|
|
24
|
+
} from "../telemetry/tracing.js";
|
|
21
25
|
import { logger } from "../utils/logger.js";
|
|
22
26
|
import { isGemini2OrAbove } from "../utils/model_name.js";
|
|
23
27
|
class Runner {
|
|
@@ -44,109 +48,123 @@ class Runner {
|
|
|
44
48
|
*/
|
|
45
49
|
// TODO - b/425992518: user, sessionId, and runConfig can be internalized.
|
|
46
50
|
async *runAsync(params) {
|
|
47
|
-
var _a;
|
|
48
51
|
const { userId, sessionId, stateDelta } = params;
|
|
49
52
|
const runConfig = createRunConfig(params.runConfig);
|
|
50
53
|
let newMessage = params.newMessage;
|
|
51
|
-
const span =
|
|
54
|
+
const span = tracer.startSpan("invocation");
|
|
55
|
+
const ctx = trace.setSpan(context.active(), span);
|
|
52
56
|
try {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
);
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
`CFC is not supported for model: ${modelName} in agent: ${this.agent.name}`
|
|
71
|
-
);
|
|
72
|
-
}
|
|
73
|
-
if (!isBuiltInCodeExecutor(this.agent.codeExecutor)) {
|
|
74
|
-
this.agent.codeExecutor = new BuiltInCodeExecutor();
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
const invocationContext = new InvocationContext({
|
|
78
|
-
artifactService: this.artifactService,
|
|
79
|
-
sessionService: this.sessionService,
|
|
80
|
-
memoryService: this.memoryService,
|
|
81
|
-
credentialService: this.credentialService,
|
|
82
|
-
invocationId: newInvocationContextId(),
|
|
83
|
-
agent: this.agent,
|
|
84
|
-
session,
|
|
85
|
-
userContent: newMessage,
|
|
86
|
-
runConfig,
|
|
87
|
-
pluginManager: this.pluginManager
|
|
88
|
-
});
|
|
89
|
-
const pluginUserMessage = await this.pluginManager.runOnUserMessageCallback({
|
|
90
|
-
userMessage: newMessage,
|
|
91
|
-
invocationContext
|
|
92
|
-
});
|
|
93
|
-
if (pluginUserMessage) {
|
|
94
|
-
newMessage = pluginUserMessage;
|
|
95
|
-
}
|
|
96
|
-
if (newMessage) {
|
|
97
|
-
if (!((_a = newMessage.parts) == null ? void 0 : _a.length)) {
|
|
98
|
-
throw new Error("No parts in the newMessage.");
|
|
99
|
-
}
|
|
100
|
-
if (runConfig.saveInputBlobsAsArtifacts) {
|
|
101
|
-
await this.saveArtifacts(
|
|
102
|
-
invocationContext.invocationId,
|
|
103
|
-
session.userId,
|
|
104
|
-
session.id,
|
|
105
|
-
newMessage
|
|
106
|
-
);
|
|
107
|
-
}
|
|
108
|
-
await this.sessionService.appendEvent({
|
|
109
|
-
session,
|
|
110
|
-
event: createEvent({
|
|
111
|
-
invocationId: invocationContext.invocationId,
|
|
112
|
-
author: "user",
|
|
113
|
-
actions: stateDelta ? createEventActions({ stateDelta }) : void 0,
|
|
114
|
-
content: newMessage
|
|
115
|
-
})
|
|
116
|
-
});
|
|
117
|
-
}
|
|
118
|
-
invocationContext.agent = this.determineAgentForResumption(
|
|
119
|
-
session,
|
|
120
|
-
this.agent
|
|
121
|
-
);
|
|
122
|
-
const beforeRunCallbackResponse = await this.pluginManager.runBeforeRunCallback({ invocationContext });
|
|
123
|
-
if (beforeRunCallbackResponse) {
|
|
124
|
-
const earlyExitEvent = createEvent({
|
|
125
|
-
invocationId: invocationContext.invocationId,
|
|
126
|
-
author: "model",
|
|
127
|
-
content: beforeRunCallbackResponse
|
|
128
|
-
});
|
|
129
|
-
await this.sessionService.appendEvent({ session, event: earlyExitEvent });
|
|
130
|
-
yield earlyExitEvent;
|
|
131
|
-
} else {
|
|
132
|
-
for await (const event of invocationContext.agent.runAsync(
|
|
133
|
-
invocationContext
|
|
134
|
-
)) {
|
|
135
|
-
if (!event.partial) {
|
|
136
|
-
await this.sessionService.appendEvent({ session, event });
|
|
57
|
+
yield* runAsyncGeneratorWithOtelContext(
|
|
58
|
+
ctx,
|
|
59
|
+
this,
|
|
60
|
+
async function* () {
|
|
61
|
+
var _a;
|
|
62
|
+
const session = await this.sessionService.getSession({
|
|
63
|
+
appName: this.appName,
|
|
64
|
+
userId,
|
|
65
|
+
sessionId
|
|
66
|
+
});
|
|
67
|
+
if (!session) {
|
|
68
|
+
if (!this.appName) {
|
|
69
|
+
throw new Error(
|
|
70
|
+
`Session lookup failed: appName must be provided in runner constructor`
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
throw new Error(`Session not found: ${sessionId}`);
|
|
137
74
|
}
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
75
|
+
if (runConfig.supportCfc && isLlmAgent(this.agent)) {
|
|
76
|
+
const modelName = this.agent.canonicalModel.model;
|
|
77
|
+
if (!isGemini2OrAbove(modelName)) {
|
|
78
|
+
throw new Error(
|
|
79
|
+
`CFC is not supported for model: ${modelName} in agent: ${this.agent.name}`
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
if (!isBuiltInCodeExecutor(this.agent.codeExecutor)) {
|
|
83
|
+
this.agent.codeExecutor = new BuiltInCodeExecutor();
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
const invocationContext = new InvocationContext({
|
|
87
|
+
artifactService: this.artifactService,
|
|
88
|
+
sessionService: this.sessionService,
|
|
89
|
+
memoryService: this.memoryService,
|
|
90
|
+
credentialService: this.credentialService,
|
|
91
|
+
invocationId: newInvocationContextId(),
|
|
92
|
+
agent: this.agent,
|
|
93
|
+
session,
|
|
94
|
+
userContent: newMessage,
|
|
95
|
+
runConfig,
|
|
96
|
+
pluginManager: this.pluginManager
|
|
97
|
+
});
|
|
98
|
+
const pluginUserMessage = await this.pluginManager.runOnUserMessageCallback({
|
|
99
|
+
userMessage: newMessage,
|
|
100
|
+
invocationContext
|
|
141
101
|
});
|
|
142
|
-
if (
|
|
143
|
-
|
|
144
|
-
}
|
|
145
|
-
|
|
102
|
+
if (pluginUserMessage) {
|
|
103
|
+
newMessage = pluginUserMessage;
|
|
104
|
+
}
|
|
105
|
+
if (newMessage) {
|
|
106
|
+
if (!((_a = newMessage.parts) == null ? void 0 : _a.length)) {
|
|
107
|
+
throw new Error("No parts in the newMessage.");
|
|
108
|
+
}
|
|
109
|
+
if (runConfig.saveInputBlobsAsArtifacts) {
|
|
110
|
+
await this.saveArtifacts(
|
|
111
|
+
invocationContext.invocationId,
|
|
112
|
+
session.userId,
|
|
113
|
+
session.id,
|
|
114
|
+
newMessage
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
await this.sessionService.appendEvent({
|
|
118
|
+
session,
|
|
119
|
+
event: createEvent({
|
|
120
|
+
invocationId: invocationContext.invocationId,
|
|
121
|
+
author: "user",
|
|
122
|
+
actions: stateDelta ? createEventActions({ stateDelta }) : void 0,
|
|
123
|
+
content: newMessage
|
|
124
|
+
})
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
invocationContext.agent = this.determineAgentForResumption(
|
|
128
|
+
session,
|
|
129
|
+
this.agent
|
|
130
|
+
);
|
|
131
|
+
if (newMessage) {
|
|
132
|
+
const beforeRunCallbackResponse = await this.pluginManager.runBeforeRunCallback({
|
|
133
|
+
invocationContext
|
|
134
|
+
});
|
|
135
|
+
if (beforeRunCallbackResponse) {
|
|
136
|
+
const earlyExitEvent = createEvent({
|
|
137
|
+
invocationId: invocationContext.invocationId,
|
|
138
|
+
author: "model",
|
|
139
|
+
content: beforeRunCallbackResponse
|
|
140
|
+
});
|
|
141
|
+
await this.sessionService.appendEvent({
|
|
142
|
+
session,
|
|
143
|
+
event: earlyExitEvent
|
|
144
|
+
});
|
|
145
|
+
yield earlyExitEvent;
|
|
146
|
+
} else {
|
|
147
|
+
for await (const event of invocationContext.agent.runAsync(
|
|
148
|
+
invocationContext
|
|
149
|
+
)) {
|
|
150
|
+
if (!event.partial) {
|
|
151
|
+
await this.sessionService.appendEvent({ session, event });
|
|
152
|
+
}
|
|
153
|
+
const modifiedEvent = await this.pluginManager.runOnEventCallback({
|
|
154
|
+
invocationContext,
|
|
155
|
+
event
|
|
156
|
+
});
|
|
157
|
+
if (modifiedEvent) {
|
|
158
|
+
yield modifiedEvent;
|
|
159
|
+
} else {
|
|
160
|
+
yield event;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
await this.pluginManager.runAfterRunCallback({ invocationContext });
|
|
164
|
+
}
|
|
146
165
|
}
|
|
147
166
|
}
|
|
148
|
-
|
|
149
|
-
await this.pluginManager.runAfterRunCallback({ invocationContext });
|
|
167
|
+
);
|
|
150
168
|
} finally {
|
|
151
169
|
span.end();
|
|
152
170
|
}
|
|
@@ -6,7 +6,9 @@
|
|
|
6
6
|
import { cloneDeep } from "lodash-es";
|
|
7
7
|
import { randomUUID } from "../utils/env_aware_utils.js";
|
|
8
8
|
import { logger } from "../utils/logger.js";
|
|
9
|
-
import {
|
|
9
|
+
import {
|
|
10
|
+
BaseSessionService
|
|
11
|
+
} from "./base_session_service.js";
|
|
10
12
|
import { createSession } from "./session.js";
|
|
11
13
|
import { State } from "./state.js";
|
|
12
14
|
class InMemorySessionService extends BaseSessionService {
|
|
@@ -26,7 +28,12 @@ class InMemorySessionService extends BaseSessionService {
|
|
|
26
28
|
*/
|
|
27
29
|
this.appState = {};
|
|
28
30
|
}
|
|
29
|
-
createSession({
|
|
31
|
+
createSession({
|
|
32
|
+
appName,
|
|
33
|
+
userId,
|
|
34
|
+
state,
|
|
35
|
+
sessionId
|
|
36
|
+
}) {
|
|
30
37
|
const session = createSession({
|
|
31
38
|
id: sessionId || randomUUID(),
|
|
32
39
|
appName,
|
|
@@ -46,7 +53,12 @@ class InMemorySessionService extends BaseSessionService {
|
|
|
46
53
|
this.mergeState(appName, userId, cloneDeep(session))
|
|
47
54
|
);
|
|
48
55
|
}
|
|
49
|
-
getSession({
|
|
56
|
+
getSession({
|
|
57
|
+
appName,
|
|
58
|
+
userId,
|
|
59
|
+
sessionId,
|
|
60
|
+
config
|
|
61
|
+
}) {
|
|
50
62
|
if (!this.sessions[appName] || !this.sessions[appName][userId] || !this.sessions[appName][userId][sessionId]) {
|
|
51
63
|
return Promise.resolve(void 0);
|
|
52
64
|
}
|
|
@@ -54,7 +66,9 @@ class InMemorySessionService extends BaseSessionService {
|
|
|
54
66
|
const copiedSession = cloneDeep(session);
|
|
55
67
|
if (config) {
|
|
56
68
|
if (config.numRecentEvents) {
|
|
57
|
-
copiedSession.events = copiedSession.events.slice(
|
|
69
|
+
copiedSession.events = copiedSession.events.slice(
|
|
70
|
+
-config.numRecentEvents
|
|
71
|
+
);
|
|
58
72
|
}
|
|
59
73
|
if (config.afterTimestamp) {
|
|
60
74
|
let i = copiedSession.events.length - 1;
|
|
@@ -71,31 +85,43 @@ class InMemorySessionService extends BaseSessionService {
|
|
|
71
85
|
}
|
|
72
86
|
return Promise.resolve(this.mergeState(appName, userId, copiedSession));
|
|
73
87
|
}
|
|
74
|
-
listSessions({
|
|
88
|
+
listSessions({
|
|
89
|
+
appName,
|
|
90
|
+
userId
|
|
91
|
+
}) {
|
|
75
92
|
if (!this.sessions[appName] || !this.sessions[appName][userId]) {
|
|
76
93
|
return Promise.resolve({ sessions: [] });
|
|
77
94
|
}
|
|
78
95
|
const sessionsWithoutEvents = [];
|
|
79
96
|
for (const session of Object.values(this.sessions[appName][userId])) {
|
|
80
|
-
sessionsWithoutEvents.push(
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
97
|
+
sessionsWithoutEvents.push(
|
|
98
|
+
createSession({
|
|
99
|
+
id: session.id,
|
|
100
|
+
appName: session.appName,
|
|
101
|
+
userId: session.userId,
|
|
102
|
+
state: {},
|
|
103
|
+
events: [],
|
|
104
|
+
lastUpdateTime: session.lastUpdateTime
|
|
105
|
+
})
|
|
106
|
+
);
|
|
88
107
|
}
|
|
89
108
|
return Promise.resolve({ sessions: sessionsWithoutEvents });
|
|
90
109
|
}
|
|
91
|
-
async deleteSession({
|
|
110
|
+
async deleteSession({
|
|
111
|
+
appName,
|
|
112
|
+
userId,
|
|
113
|
+
sessionId
|
|
114
|
+
}) {
|
|
92
115
|
const session = await this.getSession({ appName, userId, sessionId });
|
|
93
116
|
if (!session) {
|
|
94
117
|
return;
|
|
95
118
|
}
|
|
96
119
|
delete this.sessions[appName][userId][sessionId];
|
|
97
120
|
}
|
|
98
|
-
async appendEvent({
|
|
121
|
+
async appendEvent({
|
|
122
|
+
session,
|
|
123
|
+
event
|
|
124
|
+
}) {
|
|
99
125
|
await super.appendEvent({ session, event });
|
|
100
126
|
session.lastUpdateTime = event.timestamp;
|
|
101
127
|
const appName = session.appName;
|
|
@@ -3,13 +3,13 @@
|
|
|
3
3
|
* Copyright 2025 Google LLC
|
|
4
4
|
* SPDX-License-Identifier: Apache-2.0
|
|
5
5
|
*/
|
|
6
|
-
import {
|
|
7
|
-
import { PeriodicExportingMetricReader } from "@opentelemetry/sdk-metrics";
|
|
8
|
-
import { detectResources } from "@opentelemetry/resources";
|
|
9
|
-
import { gcpDetector } from "@opentelemetry/resource-detector-gcp";
|
|
6
|
+
import { MetricExporter } from "@google-cloud/opentelemetry-cloud-monitoring-exporter";
|
|
10
7
|
import { TraceExporter } from "@google-cloud/opentelemetry-cloud-trace-exporter";
|
|
8
|
+
import { gcpDetector } from "@opentelemetry/resource-detector-gcp";
|
|
9
|
+
import { detectResources } from "@opentelemetry/resources";
|
|
10
|
+
import { PeriodicExportingMetricReader } from "@opentelemetry/sdk-metrics";
|
|
11
11
|
import { BatchSpanProcessor } from "@opentelemetry/sdk-trace-base";
|
|
12
|
-
import {
|
|
12
|
+
import { GoogleAuth } from "google-auth-library";
|
|
13
13
|
import { logger } from "../utils/logger.js";
|
|
14
14
|
const GCP_PROJECT_ERROR_MESSAGE = "Cannot determine GCP Project. OTel GCP Exporters cannot be set up. Please make sure to log into correct GCP Project.";
|
|
15
15
|
async function getGcpProjectId() {
|
|
@@ -17,7 +17,7 @@ async function getGcpProjectId() {
|
|
|
17
17
|
const auth = new GoogleAuth();
|
|
18
18
|
const projectId = await auth.getProjectId();
|
|
19
19
|
return projectId || void 0;
|
|
20
|
-
} catch (
|
|
20
|
+
} catch (_e) {
|
|
21
21
|
return void 0;
|
|
22
22
|
}
|
|
23
23
|
}
|
|
@@ -33,9 +33,7 @@ async function getGcpExporters(config = {}) {
|
|
|
33
33
|
return {};
|
|
34
34
|
}
|
|
35
35
|
return {
|
|
36
|
-
spanProcessors: enableTracing ? [
|
|
37
|
-
new BatchSpanProcessor(new TraceExporter({ projectId }))
|
|
38
|
-
] : [],
|
|
36
|
+
spanProcessors: enableTracing ? [new BatchSpanProcessor(new TraceExporter({ projectId }))] : [],
|
|
39
37
|
metricReaders: enableMetrics ? [
|
|
40
38
|
new PeriodicExportingMetricReader({
|
|
41
39
|
exporter: new MetricExporter({ projectId }),
|
|
@@ -3,22 +3,32 @@
|
|
|
3
3
|
* Copyright 2025 Google LLC
|
|
4
4
|
* SPDX-License-Identifier: Apache-2.0
|
|
5
5
|
*/
|
|
6
|
-
import {
|
|
6
|
+
import { metrics, trace } from "@opentelemetry/api";
|
|
7
7
|
import { logs } from "@opentelemetry/api-logs";
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
8
|
+
import { OTLPLogExporter } from "@opentelemetry/exporter-logs-otlp-http";
|
|
9
|
+
import { OTLPMetricExporter } from "@opentelemetry/exporter-metrics-otlp-http";
|
|
10
|
+
import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-http";
|
|
10
11
|
import { detectResources } from "@opentelemetry/resources";
|
|
12
|
+
import {
|
|
13
|
+
BatchLogRecordProcessor,
|
|
14
|
+
LoggerProvider
|
|
15
|
+
} from "@opentelemetry/sdk-logs";
|
|
16
|
+
import {
|
|
17
|
+
MeterProvider,
|
|
18
|
+
PeriodicExportingMetricReader
|
|
19
|
+
} from "@opentelemetry/sdk-metrics";
|
|
11
20
|
import { BatchSpanProcessor } from "@opentelemetry/sdk-trace-base";
|
|
12
21
|
import { NodeTracerProvider } from "@opentelemetry/sdk-trace-node";
|
|
13
|
-
import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-http";
|
|
14
|
-
import { OTLPMetricExporter } from "@opentelemetry/exporter-metrics-otlp-http";
|
|
15
|
-
import { OTLPLogExporter } from "@opentelemetry/exporter-logs-otlp-http";
|
|
16
22
|
function maybeSetOtelProviders(otelHooksToSetup = [], otelResource) {
|
|
17
23
|
const resource = otelResource || getOtelResource();
|
|
18
24
|
const allHooks = [...otelHooksToSetup, getOtelExporters()];
|
|
19
|
-
const spanProcessors = allHooks.flatMap(
|
|
25
|
+
const spanProcessors = allHooks.flatMap(
|
|
26
|
+
(hooks) => hooks.spanProcessors || []
|
|
27
|
+
);
|
|
20
28
|
const metricReaders = allHooks.flatMap((hooks) => hooks.metricReaders || []);
|
|
21
|
-
const logRecordProcessors = allHooks.flatMap(
|
|
29
|
+
const logRecordProcessors = allHooks.flatMap(
|
|
30
|
+
(hooks) => hooks.logRecordProcessors || []
|
|
31
|
+
);
|
|
22
32
|
if (spanProcessors.length > 0) {
|
|
23
33
|
const tracerProvider = new NodeTracerProvider({
|
|
24
34
|
resource,
|
|
@@ -58,7 +68,11 @@ function getOtelExporters(config = getOtelExportersConfig()) {
|
|
|
58
68
|
const { enableTracing, enableMetrics, enableLogging } = config;
|
|
59
69
|
return {
|
|
60
70
|
spanProcessors: enableTracing ? [new BatchSpanProcessor(new OTLPTraceExporter())] : [],
|
|
61
|
-
metricReaders: enableMetrics ? [
|
|
71
|
+
metricReaders: enableMetrics ? [
|
|
72
|
+
new PeriodicExportingMetricReader({
|
|
73
|
+
exporter: new OTLPMetricExporter()
|
|
74
|
+
})
|
|
75
|
+
] : [],
|
|
62
76
|
logRecordProcessors: enableLogging ? [new BatchLogRecordProcessor(new OTLPLogExporter())] : []
|
|
63
77
|
};
|
|
64
78
|
}
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* Copyright 2025 Google LLC
|
|
4
4
|
* SPDX-License-Identifier: Apache-2.0
|
|
5
5
|
*/
|
|
6
|
-
import {
|
|
6
|
+
import { context, trace } from "@opentelemetry/api";
|
|
7
7
|
import { version } from "../version.js";
|
|
8
8
|
const GEN_AI_AGENT_DESCRIPTION = "gen_ai.agent.description";
|
|
9
9
|
const GEN_AI_AGENT_NAME = "gen_ai.agent.name";
|
|
@@ -13,14 +13,11 @@ const GEN_AI_TOOL_CALL_ID = "gen_ai.tool.call.id";
|
|
|
13
13
|
const GEN_AI_TOOL_DESCRIPTION = "gen_ai.tool.description";
|
|
14
14
|
const GEN_AI_TOOL_NAME = "gen_ai.tool.name";
|
|
15
15
|
const GEN_AI_TOOL_TYPE = "gen_ai.tool.type";
|
|
16
|
-
const tracer = trace.getTracer(
|
|
17
|
-
"gcp.vertex.agent",
|
|
18
|
-
version
|
|
19
|
-
);
|
|
16
|
+
const tracer = trace.getTracer("gcp.vertex.agent", version);
|
|
20
17
|
function safeJsonSerialize(obj) {
|
|
21
18
|
try {
|
|
22
19
|
return JSON.stringify(obj);
|
|
23
|
-
} catch (
|
|
20
|
+
} catch (_e) {
|
|
24
21
|
return "<not serializable>";
|
|
25
22
|
}
|
|
26
23
|
}
|
|
@@ -97,7 +94,10 @@ function traceMergedToolCalls({
|
|
|
97
94
|
"gcp.vertex.agent.llm_request": "{}",
|
|
98
95
|
"gcp.vertex.agent.llm_response": "{}"
|
|
99
96
|
});
|
|
100
|
-
span.setAttribute(
|
|
97
|
+
span.setAttribute(
|
|
98
|
+
"gcp.vertex.agent.tool_response",
|
|
99
|
+
shouldAddRequestResponseToSpans() ? safeJsonSerialize(functionResponseEvent) : "{}"
|
|
100
|
+
);
|
|
101
101
|
}
|
|
102
102
|
function traceCallLlm({
|
|
103
103
|
invocationContext,
|
|
@@ -121,14 +121,26 @@ function traceCallLlm({
|
|
|
121
121
|
span.setAttribute("gen_ai.request.top_p", llmRequest.config.topP);
|
|
122
122
|
}
|
|
123
123
|
if (((_b = llmRequest.config) == null ? void 0 : _b.maxOutputTokens) !== void 0) {
|
|
124
|
-
span.setAttribute(
|
|
124
|
+
span.setAttribute(
|
|
125
|
+
"gen_ai.request.max_tokens",
|
|
126
|
+
llmRequest.config.maxOutputTokens
|
|
127
|
+
);
|
|
125
128
|
}
|
|
126
|
-
span.setAttribute(
|
|
129
|
+
span.setAttribute(
|
|
130
|
+
"gcp.vertex.agent.llm_response",
|
|
131
|
+
shouldAddRequestResponseToSpans() ? safeJsonSerialize(llmResponse) : "{}"
|
|
132
|
+
);
|
|
127
133
|
if (llmResponse.usageMetadata) {
|
|
128
|
-
span.setAttribute(
|
|
134
|
+
span.setAttribute(
|
|
135
|
+
"gen_ai.usage.input_tokens",
|
|
136
|
+
llmResponse.usageMetadata.promptTokenCount || 0
|
|
137
|
+
);
|
|
129
138
|
}
|
|
130
139
|
if ((_c = llmResponse.usageMetadata) == null ? void 0 : _c.candidatesTokenCount) {
|
|
131
|
-
span.setAttribute(
|
|
140
|
+
span.setAttribute(
|
|
141
|
+
"gen_ai.usage.output_tokens",
|
|
142
|
+
llmResponse.usageMetadata.candidatesTokenCount
|
|
143
|
+
);
|
|
132
144
|
}
|
|
133
145
|
if (llmResponse.finishReason) {
|
|
134
146
|
const finishReasonValue = typeof llmResponse.finishReason === "string" ? llmResponse.finishReason.toLowerCase() : String(llmResponse.finishReason).toLowerCase();
|
|
@@ -146,7 +158,10 @@ function traceSendData({
|
|
|
146
158
|
"gcp.vertex.agent.invocation_id": invocationContext.invocationId,
|
|
147
159
|
"gcp.vertex.agent.event_id": eventId
|
|
148
160
|
});
|
|
149
|
-
span.setAttribute(
|
|
161
|
+
span.setAttribute(
|
|
162
|
+
"gcp.vertex.agent.data",
|
|
163
|
+
shouldAddRequestResponseToSpans() ? safeJsonSerialize(data) : "{}"
|
|
164
|
+
);
|
|
150
165
|
}
|
|
151
166
|
function buildLlmRequestForTrace(llmRequest) {
|
|
152
167
|
const result = {
|
|
@@ -166,7 +181,7 @@ function buildLlmRequestForTrace(llmRequest) {
|
|
|
166
181
|
});
|
|
167
182
|
return result;
|
|
168
183
|
}
|
|
169
|
-
function
|
|
184
|
+
function bindOtelContextToAsyncGenerator(ctx, generator) {
|
|
170
185
|
return {
|
|
171
186
|
// Bind the next() method to execute within the provided context
|
|
172
187
|
next: context.bind(ctx, generator.next.bind(generator)),
|
|
@@ -176,16 +191,23 @@ function bindAsyncGenerator(ctx, generator) {
|
|
|
176
191
|
throw: context.bind(ctx, generator.throw.bind(generator)),
|
|
177
192
|
// Ensure the async iterator symbol also returns a context-bound generator
|
|
178
193
|
[Symbol.asyncIterator]() {
|
|
179
|
-
return
|
|
194
|
+
return bindOtelContextToAsyncGenerator(
|
|
195
|
+
ctx,
|
|
196
|
+
generator[Symbol.asyncIterator]()
|
|
197
|
+
);
|
|
180
198
|
}
|
|
181
199
|
};
|
|
182
200
|
}
|
|
201
|
+
function runAsyncGeneratorWithOtelContext(otelContext, generatorFnContext, generatorFn) {
|
|
202
|
+
const generator = generatorFn.call(generatorFnContext);
|
|
203
|
+
return bindOtelContextToAsyncGenerator(otelContext, generator);
|
|
204
|
+
}
|
|
183
205
|
function shouldAddRequestResponseToSpans() {
|
|
184
206
|
const envValue = process.env.ADK_CAPTURE_MESSAGE_CONTENT_IN_SPANS || "true";
|
|
185
207
|
return envValue === "true" || envValue === "1";
|
|
186
208
|
}
|
|
187
209
|
export {
|
|
188
|
-
|
|
210
|
+
runAsyncGeneratorWithOtelContext,
|
|
189
211
|
traceAgentInvocation,
|
|
190
212
|
traceCallLlm,
|
|
191
213
|
traceMergedToolCalls,
|
|
@@ -18,9 +18,10 @@ function isAgentTool(obj) {
|
|
|
18
18
|
}
|
|
19
19
|
class AgentTool extends (_b = BaseTool, _a = AGENT_TOOL_SIGNATURE_SYMBOL, _b) {
|
|
20
20
|
constructor(config) {
|
|
21
|
-
super(
|
|
22
|
-
|
|
23
|
-
|
|
21
|
+
super({
|
|
22
|
+
name: config.agent.name,
|
|
23
|
+
description: config.agent.description || ""
|
|
24
|
+
});
|
|
24
25
|
/** A unique symbol to identify ADK agent tool class. */
|
|
25
26
|
this[_a] = true;
|
|
26
27
|
this.agent = config.agent;
|
|
@@ -58,7 +59,10 @@ class AgentTool extends (_b = BaseTool, _a = AGENT_TOOL_SIGNATURE_SYMBOL, _b) {
|
|
|
58
59
|
}
|
|
59
60
|
return declaration;
|
|
60
61
|
}
|
|
61
|
-
async runAsync({
|
|
62
|
+
async runAsync({
|
|
63
|
+
args,
|
|
64
|
+
toolContext
|
|
65
|
+
}) {
|
|
62
66
|
var _a2, _b2;
|
|
63
67
|
if (this.skipSummarization) {
|
|
64
68
|
toolContext.actions.skipSummarization = true;
|
|
@@ -49,7 +49,7 @@ class BaseTool {
|
|
|
49
49
|
*
|
|
50
50
|
* @param request The request to process the LLM request.
|
|
51
51
|
*/
|
|
52
|
-
async processLlmRequest({
|
|
52
|
+
async processLlmRequest({ llmRequest }) {
|
|
53
53
|
const functionDeclaration = this._getDeclaration();
|
|
54
54
|
if (!functionDeclaration) {
|
|
55
55
|
return;
|
|
@@ -78,7 +78,9 @@ class BaseTool {
|
|
|
78
78
|
}
|
|
79
79
|
function findToolWithFunctionDeclarations(llmRequest) {
|
|
80
80
|
var _a2;
|
|
81
|
-
return (((_a2 = llmRequest.config) == null ? void 0 : _a2.tools) || []).find(
|
|
81
|
+
return (((_a2 = llmRequest.config) == null ? void 0 : _a2.tools) || []).find(
|
|
82
|
+
(tool) => "functionDeclarations" in tool
|
|
83
|
+
);
|
|
82
84
|
}
|
|
83
85
|
export {
|
|
84
86
|
BaseTool,
|
|
@@ -16,7 +16,7 @@ class ForwardingArtifactService {
|
|
|
16
16
|
async loadArtifact(request) {
|
|
17
17
|
return this.toolContext.loadArtifact(request.filename, request.version);
|
|
18
18
|
}
|
|
19
|
-
async listArtifactKeys(
|
|
19
|
+
async listArtifactKeys() {
|
|
20
20
|
return this.toolContext.listArtifacts();
|
|
21
21
|
}
|
|
22
22
|
async deleteArtifact(request) {
|
|
@@ -5,7 +5,6 @@
|
|
|
5
5
|
*/
|
|
6
6
|
var _a, _b;
|
|
7
7
|
import { Type } from "@google/genai";
|
|
8
|
-
import { ZodObject } from "zod";
|
|
9
8
|
import { isZodObject, zodObjectToSchema } from "../utils/simple_zod_to_json.js";
|
|
10
9
|
import { BaseTool } from "./base_tool.js";
|
|
11
10
|
function toSchema(parameters) {
|
|
@@ -60,7 +59,7 @@ class FunctionTool extends (_b = BaseTool, _a = FUNCTION_TOOL_SIGNATURE_SYMBOL,
|
|
|
60
59
|
async runAsync(req) {
|
|
61
60
|
try {
|
|
62
61
|
let validatedArgs = req.args;
|
|
63
|
-
if (this.parameters
|
|
62
|
+
if (isZodObject(this.parameters)) {
|
|
64
63
|
validatedArgs = this.parameters.parse(req.args);
|
|
65
64
|
}
|
|
66
65
|
return await this.execute(
|
|
@@ -4,18 +4,15 @@
|
|
|
4
4
|
* SPDX-License-Identifier: Apache-2.0
|
|
5
5
|
*/
|
|
6
6
|
import { isGemini1Model, isGeminiModel } from "../utils/model_name.js";
|
|
7
|
-
import {
|
|
8
|
-
BaseTool
|
|
9
|
-
} from "./base_tool.js";
|
|
7
|
+
import { BaseTool } from "./base_tool.js";
|
|
10
8
|
class GoogleSearchTool extends BaseTool {
|
|
11
9
|
constructor() {
|
|
12
10
|
super({ name: "google_search", description: "Google Search Tool" });
|
|
13
11
|
}
|
|
14
|
-
runAsync(
|
|
12
|
+
runAsync() {
|
|
15
13
|
return Promise.resolve();
|
|
16
14
|
}
|
|
17
15
|
async processLlmRequest({
|
|
18
|
-
toolContext,
|
|
19
16
|
llmRequest
|
|
20
17
|
}) {
|
|
21
18
|
if (!llmRequest.model) {
|
|
@@ -3,7 +3,9 @@
|
|
|
3
3
|
* Copyright 2025 Google LLC
|
|
4
4
|
* SPDX-License-Identifier: Apache-2.0
|
|
5
5
|
*/
|
|
6
|
-
import {
|
|
6
|
+
import {
|
|
7
|
+
FunctionTool
|
|
8
|
+
} from "./function_tool.js";
|
|
7
9
|
const LONG_RUNNING_INSTRUCTION = `
|
|
8
10
|
|
|
9
11
|
NOTE: This is a long-running operation. Do not call this tool again if it has already returned some intermediate or pending status.`;
|