@paean-ai/adk 0.2.19 → 0.2.20
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/active_streaming_tool.js +44 -0
- package/dist/cjs/agents/base_agent.js +245 -0
- package/dist/cjs/agents/base_llm_processor.js +44 -0
- package/dist/cjs/agents/callback_context.js +98 -0
- package/dist/cjs/agents/content_processor_utils.js +311 -0
- package/dist/cjs/agents/functions.js +425 -0
- package/dist/cjs/agents/instructions.js +110 -0
- package/dist/cjs/agents/invocation_context.js +107 -0
- package/dist/cjs/agents/live_request_queue.js +136 -0
- package/dist/cjs/agents/llm_agent.js +1257 -0
- package/dist/cjs/agents/loop_agent.js +68 -0
- package/dist/cjs/agents/parallel_agent.js +78 -0
- package/dist/cjs/agents/readonly_context.js +68 -0
- package/dist/cjs/agents/run_config.js +70 -0
- package/dist/cjs/agents/sequential_agent.js +84 -0
- package/dist/cjs/agents/transcription_entry.js +27 -0
- package/dist/cjs/artifacts/base_artifact_service.js +27 -0
- package/dist/cjs/artifacts/gcs_artifact_service.js +140 -0
- package/dist/cjs/artifacts/in_memory_artifact_service.js +119 -0
- package/dist/cjs/auth/auth_credential.js +46 -0
- package/dist/cjs/auth/auth_handler.js +92 -0
- package/dist/cjs/auth/auth_schemes.js +62 -0
- package/dist/cjs/auth/auth_tool.js +27 -0
- package/dist/cjs/auth/credential_service/base_credential_service.js +27 -0
- package/dist/cjs/auth/credential_service/in_memory_credential_service.js +63 -0
- package/dist/cjs/auth/exchanger/base_credential_exchanger.js +40 -0
- package/dist/cjs/auth/exchanger/credential_exchanger_registry.js +59 -0
- package/dist/cjs/code_executors/base_code_executor.js +76 -0
- package/dist/cjs/code_executors/built_in_code_executor.js +58 -0
- package/dist/cjs/code_executors/code_execution_utils.js +142 -0
- package/dist/cjs/code_executors/code_executor_context.js +198 -0
- package/dist/cjs/common.js +181 -0
- package/dist/cjs/events/event.js +119 -0
- package/dist/cjs/events/event_actions.js +83 -0
- package/dist/cjs/examples/base_example_provider.js +40 -0
- package/dist/cjs/examples/example.js +27 -0
- package/dist/cjs/examples/example_util.js +107 -0
- package/dist/cjs/index.js +12 -12
- package/dist/cjs/index.js.map +3 -3
- package/dist/cjs/index_web.js +33 -0
- package/dist/cjs/memory/base_memory_service.js +27 -0
- package/dist/cjs/memory/in_memory_memory_service.js +97 -0
- package/dist/cjs/memory/memory_entry.js +27 -0
- package/dist/cjs/models/base_llm.js +95 -0
- package/dist/cjs/models/base_llm_connection.js +27 -0
- package/dist/cjs/models/gemini_llm_connection.js +132 -0
- package/dist/cjs/models/google_llm.js +472 -0
- package/dist/cjs/models/llm_request.js +82 -0
- package/dist/cjs/models/llm_response.js +71 -0
- package/dist/cjs/models/registry.js +121 -0
- package/dist/cjs/plugins/base_plugin.js +236 -0
- package/dist/cjs/plugins/logging_plugin.js +222 -0
- package/dist/cjs/plugins/plugin_manager.js +239 -0
- package/dist/cjs/plugins/security_plugin.js +153 -0
- package/dist/cjs/runner/in_memory_runner.js +58 -0
- package/dist/cjs/runner/runner.js +277 -0
- package/dist/cjs/sessions/base_session_service.js +71 -0
- package/dist/cjs/sessions/in_memory_session_service.js +184 -0
- package/dist/cjs/sessions/session.js +48 -0
- package/dist/cjs/sessions/state.js +101 -0
- package/dist/cjs/telemetry/google_cloud.js +85 -0
- package/dist/cjs/telemetry/setup.js +97 -0
- package/dist/cjs/telemetry/tracing.js +231 -0
- package/dist/cjs/tools/agent_tool.js +134 -0
- package/dist/cjs/tools/base_tool.js +107 -0
- package/dist/cjs/tools/base_toolset.js +76 -0
- package/dist/cjs/tools/forwarding_artifact_service.js +71 -0
- package/dist/cjs/tools/function_tool.js +101 -0
- package/dist/cjs/tools/google_search_tool.js +77 -0
- package/dist/cjs/tools/long_running_tool.js +63 -0
- package/dist/cjs/tools/mcp/mcp_session_manager.js +65 -0
- package/dist/cjs/tools/mcp/mcp_tool.js +65 -0
- package/dist/cjs/tools/mcp/mcp_toolset.js +61 -0
- package/dist/cjs/tools/tool_confirmation.js +49 -0
- package/dist/cjs/tools/tool_context.js +129 -0
- package/dist/cjs/utils/client_labels.js +56 -0
- package/dist/cjs/utils/deep_clone.js +44 -0
- package/dist/cjs/utils/env_aware_utils.js +83 -0
- package/dist/cjs/utils/gemini_schema_util.js +88 -0
- package/dist/cjs/utils/logger.js +121 -0
- package/dist/cjs/utils/model_name.js +76 -0
- package/dist/cjs/utils/simple_zod_to_json.js +191 -0
- package/dist/cjs/utils/variant_utils.js +55 -0
- package/dist/cjs/version.js +39 -0
- package/dist/esm/agents/active_streaming_tool.js +14 -0
- package/dist/esm/agents/base_agent.js +214 -0
- package/dist/esm/agents/base_llm_processor.js +13 -0
- package/dist/esm/agents/callback_context.js +68 -0
- package/dist/esm/agents/content_processor_utils.js +280 -0
- package/dist/esm/agents/functions.js +384 -0
- package/dist/esm/agents/instructions.js +80 -0
- package/dist/esm/agents/invocation_context.js +76 -0
- package/dist/esm/agents/live_request_queue.js +106 -0
- package/dist/esm/agents/llm_agent.js +1225 -0
- package/dist/esm/agents/loop_agent.js +38 -0
- package/dist/esm/agents/parallel_agent.js +48 -0
- package/dist/esm/agents/readonly_context.js +38 -0
- package/dist/esm/agents/run_config.js +39 -0
- package/dist/esm/agents/sequential_agent.js +54 -0
- package/dist/esm/agents/transcription_entry.js +5 -0
- package/dist/esm/artifacts/base_artifact_service.js +5 -0
- package/dist/esm/artifacts/gcs_artifact_service.js +110 -0
- package/dist/esm/artifacts/in_memory_artifact_service.js +89 -0
- package/dist/esm/auth/auth_credential.js +16 -0
- package/dist/esm/auth/auth_handler.js +62 -0
- package/dist/esm/auth/auth_schemes.js +31 -0
- package/dist/esm/auth/auth_tool.js +5 -0
- package/dist/esm/auth/credential_service/base_credential_service.js +5 -0
- package/dist/esm/auth/credential_service/in_memory_credential_service.js +33 -0
- package/dist/esm/auth/exchanger/base_credential_exchanger.js +10 -0
- package/dist/esm/auth/exchanger/credential_exchanger_registry.js +29 -0
- package/dist/esm/code_executors/base_code_executor.js +46 -0
- package/dist/esm/code_executors/built_in_code_executor.js +28 -0
- package/dist/esm/code_executors/code_execution_utils.js +108 -0
- package/dist/esm/code_executors/code_executor_context.js +168 -0
- package/dist/esm/common.js +98 -0
- package/dist/esm/events/event.js +83 -0
- package/dist/esm/events/event_actions.js +52 -0
- package/dist/esm/examples/base_example_provider.js +10 -0
- package/dist/esm/examples/example.js +5 -0
- package/dist/esm/examples/example_util.js +76 -0
- package/dist/esm/index.js +12 -12
- package/dist/esm/index.js.map +3 -3
- package/dist/esm/index_web.js +6 -0
- package/dist/esm/memory/base_memory_service.js +5 -0
- package/dist/esm/memory/in_memory_memory_service.js +67 -0
- package/dist/esm/memory/memory_entry.js +5 -0
- package/dist/esm/models/base_llm.js +64 -0
- package/dist/esm/models/base_llm_connection.js +5 -0
- package/dist/esm/models/gemini_llm_connection.js +102 -0
- package/dist/esm/models/google_llm.js +446 -0
- package/dist/esm/models/llm_request.js +50 -0
- package/dist/esm/models/llm_response.js +41 -0
- package/dist/esm/models/registry.js +91 -0
- package/dist/esm/plugins/base_plugin.js +206 -0
- package/dist/esm/plugins/logging_plugin.js +192 -0
- package/dist/esm/plugins/plugin_manager.js +209 -0
- package/dist/esm/plugins/security_plugin.js +119 -0
- package/dist/esm/runner/in_memory_runner.js +28 -0
- package/dist/esm/runner/runner.js +247 -0
- package/dist/esm/sessions/base_session_service.js +41 -0
- package/dist/esm/sessions/in_memory_session_service.js +154 -0
- package/dist/esm/sessions/session.js +18 -0
- package/dist/esm/sessions/state.js +71 -0
- package/dist/esm/telemetry/google_cloud.js +54 -0
- package/dist/esm/telemetry/setup.js +67 -0
- package/dist/esm/telemetry/tracing.js +195 -0
- package/dist/esm/tools/agent_tool.js +104 -0
- package/dist/esm/tools/base_tool.js +77 -0
- package/dist/esm/tools/base_toolset.js +46 -0
- package/dist/esm/tools/forwarding_artifact_service.js +41 -0
- package/dist/esm/tools/function_tool.js +71 -0
- package/dist/esm/tools/google_search_tool.js +47 -0
- package/dist/esm/tools/long_running_tool.js +33 -0
- package/dist/esm/tools/mcp/mcp_session_manager.js +35 -0
- package/dist/esm/tools/mcp/mcp_tool.js +35 -0
- package/dist/esm/tools/mcp/mcp_toolset.js +31 -0
- package/dist/esm/tools/tool_confirmation.js +19 -0
- package/dist/esm/tools/tool_context.js +99 -0
- package/dist/esm/utils/client_labels.js +26 -0
- package/dist/esm/utils/deep_clone.js +14 -0
- package/dist/esm/utils/env_aware_utils.js +49 -0
- package/dist/esm/utils/gemini_schema_util.js +58 -0
- package/dist/esm/utils/logger.js +89 -0
- package/dist/esm/utils/model_name.js +41 -0
- package/dist/esm/utils/simple_zod_to_json.js +160 -0
- package/dist/esm/utils/variant_utils.js +24 -0
- package/dist/esm/version.js +9 -0
- package/dist/types/models/google_llm.d.ts +0 -7
- package/dist/web/agents/active_streaming_tool.js +14 -0
- package/dist/web/agents/base_agent.js +265 -0
- package/dist/web/agents/base_llm_processor.js +13 -0
- package/dist/web/agents/callback_context.js +68 -0
- package/dist/web/agents/content_processor_utils.js +280 -0
- package/dist/web/agents/functions.js +384 -0
- package/dist/web/agents/instructions.js +80 -0
- package/dist/web/agents/invocation_context.js +76 -0
- package/dist/web/agents/live_request_queue.js +124 -0
- package/dist/web/agents/llm_agent.js +1355 -0
- package/dist/web/agents/loop_agent.js +71 -0
- package/dist/web/agents/parallel_agent.js +83 -0
- package/dist/web/agents/readonly_context.js +38 -0
- package/dist/web/agents/run_config.js +54 -0
- package/dist/web/agents/sequential_agent.js +99 -0
- package/dist/web/agents/transcription_entry.js +5 -0
- package/dist/web/artifacts/base_artifact_service.js +5 -0
- package/dist/web/artifacts/gcs_artifact_service.js +126 -0
- package/dist/web/artifacts/in_memory_artifact_service.js +89 -0
- package/dist/web/auth/auth_credential.js +16 -0
- package/dist/web/auth/auth_handler.js +62 -0
- package/dist/web/auth/auth_schemes.js +31 -0
- package/dist/web/auth/auth_tool.js +5 -0
- package/dist/web/auth/credential_service/base_credential_service.js +5 -0
- package/dist/web/auth/credential_service/in_memory_credential_service.js +33 -0
- package/dist/web/auth/exchanger/base_credential_exchanger.js +10 -0
- package/dist/web/auth/exchanger/credential_exchanger_registry.js +29 -0
- package/dist/web/code_executors/base_code_executor.js +46 -0
- package/dist/web/code_executors/built_in_code_executor.js +28 -0
- package/dist/web/code_executors/code_execution_utils.js +105 -0
- package/dist/web/code_executors/code_executor_context.js +168 -0
- package/dist/web/common.js +98 -0
- package/dist/web/events/event.js +101 -0
- package/dist/web/events/event_actions.js +67 -0
- package/dist/web/examples/base_example_provider.js +10 -0
- package/dist/web/examples/example.js +5 -0
- package/dist/web/examples/example_util.js +75 -0
- package/dist/web/index.js +1 -1
- package/dist/web/index.js.map +3 -3
- package/dist/web/index_web.js +6 -0
- package/dist/web/memory/base_memory_service.js +5 -0
- package/dist/web/memory/in_memory_memory_service.js +67 -0
- package/dist/web/memory/memory_entry.js +5 -0
- package/dist/web/models/base_llm.js +64 -0
- package/dist/web/models/base_llm_connection.js +5 -0
- package/dist/web/models/gemini_llm_connection.js +120 -0
- package/dist/web/models/google_llm.js +487 -0
- package/dist/web/models/llm_request.js +50 -0
- package/dist/web/models/llm_response.js +41 -0
- package/dist/web/models/registry.js +91 -0
- package/dist/web/plugins/base_plugin.js +206 -0
- package/dist/web/plugins/logging_plugin.js +192 -0
- package/dist/web/plugins/plugin_manager.js +209 -0
- package/dist/web/plugins/security_plugin.js +119 -0
- package/dist/web/runner/in_memory_runner.js +28 -0
- package/dist/web/runner/runner.js +278 -0
- package/dist/web/sessions/base_session_service.js +41 -0
- package/dist/web/sessions/in_memory_session_service.js +154 -0
- package/dist/web/sessions/session.js +18 -0
- package/dist/web/sessions/state.js +87 -0
- package/dist/web/telemetry/google_cloud.js +54 -0
- package/dist/web/telemetry/setup.js +67 -0
- package/dist/web/telemetry/tracing.js +210 -0
- package/dist/web/tools/agent_tool.js +118 -0
- package/dist/web/tools/base_tool.js +77 -0
- package/dist/web/tools/base_toolset.js +46 -0
- package/dist/web/tools/forwarding_artifact_service.js +41 -0
- package/dist/web/tools/function_tool.js +71 -0
- package/dist/web/tools/google_search_tool.js +47 -0
- package/dist/web/tools/long_running_tool.js +50 -0
- package/dist/web/tools/mcp/mcp_session_manager.js +35 -0
- package/dist/web/tools/mcp/mcp_tool.js +35 -0
- package/dist/web/tools/mcp/mcp_toolset.js +31 -0
- package/dist/web/tools/tool_confirmation.js +19 -0
- package/dist/web/tools/tool_context.js +99 -0
- package/dist/web/utils/client_labels.js +26 -0
- package/dist/web/utils/deep_clone.js +14 -0
- package/dist/web/utils/env_aware_utils.js +49 -0
- package/dist/web/utils/gemini_schema_util.js +58 -0
- package/dist/web/utils/logger.js +89 -0
- package/dist/web/utils/model_name.js +41 -0
- package/dist/web/utils/simple_zod_to_json.js +174 -0
- package/dist/web/utils/variant_utils.js +24 -0
- package/dist/web/version.js +9 -0
- package/package.json +1 -1
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2025 Google LLC
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import { createEventActions } from "../events/event_actions.js";
|
|
7
|
+
import { State } from "../sessions/state.js";
|
|
8
|
+
import { ReadonlyContext } from "./readonly_context.js";
|
|
9
|
+
class CallbackContext extends ReadonlyContext {
|
|
10
|
+
constructor({ invocationContext, eventActions }) {
|
|
11
|
+
super(invocationContext);
|
|
12
|
+
this.eventActions = eventActions || createEventActions();
|
|
13
|
+
this._state = new State(
|
|
14
|
+
invocationContext.session.state,
|
|
15
|
+
this.eventActions.stateDelta
|
|
16
|
+
);
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* The delta-aware state of the current session.
|
|
20
|
+
*/
|
|
21
|
+
get state() {
|
|
22
|
+
return this._state;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Loads an artifact attached to the current session.
|
|
26
|
+
*
|
|
27
|
+
* @param filename The filename of the artifact.
|
|
28
|
+
* @param version The version of the artifact. If not provided, the latest
|
|
29
|
+
* version will be used.
|
|
30
|
+
* @return A promise that resolves to the loaded artifact.
|
|
31
|
+
*/
|
|
32
|
+
loadArtifact(filename, version) {
|
|
33
|
+
if (!this.invocationContext.artifactService) {
|
|
34
|
+
throw new Error("Artifact service is not initialized.");
|
|
35
|
+
}
|
|
36
|
+
return this.invocationContext.artifactService.loadArtifact({
|
|
37
|
+
appName: this.invocationContext.appName,
|
|
38
|
+
userId: this.invocationContext.userId,
|
|
39
|
+
sessionId: this.invocationContext.session.id,
|
|
40
|
+
filename,
|
|
41
|
+
version
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Saves an artifact attached to the current session.
|
|
46
|
+
*
|
|
47
|
+
* @param filename The filename of the artifact.
|
|
48
|
+
* @param artifact The artifact to save.
|
|
49
|
+
* @return A promise that resolves to the version of the saved artifact.
|
|
50
|
+
*/
|
|
51
|
+
async saveArtifact(filename, artifact) {
|
|
52
|
+
if (!this.invocationContext.artifactService) {
|
|
53
|
+
throw new Error("Artifact service is not initialized.");
|
|
54
|
+
}
|
|
55
|
+
const version = await this.invocationContext.artifactService.saveArtifact({
|
|
56
|
+
appName: this.invocationContext.appName,
|
|
57
|
+
userId: this.invocationContext.userId,
|
|
58
|
+
sessionId: this.invocationContext.session.id,
|
|
59
|
+
filename,
|
|
60
|
+
artifact
|
|
61
|
+
});
|
|
62
|
+
this.eventActions.artifactDelta[filename] = version;
|
|
63
|
+
return version;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
export {
|
|
67
|
+
CallbackContext
|
|
68
|
+
};
|
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2025 Google LLC
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import { createEvent, getFunctionCalls, getFunctionResponses } from "../events/event.js";
|
|
7
|
+
import { deepClone } from "../utils/deep_clone.js";
|
|
8
|
+
import { removeClientFunctionCallId, REQUEST_CONFIRMATION_FUNCTION_CALL_NAME, REQUEST_EUC_FUNCTION_CALL_NAME } from "./functions.js";
|
|
9
|
+
function getContents(events, agentName, currentBranch) {
|
|
10
|
+
var _a, _b;
|
|
11
|
+
const filteredEvents = [];
|
|
12
|
+
for (const event of events) {
|
|
13
|
+
if (!((_a = event.content) == null ? void 0 : _a.role) || !((_b = event.content.parts) == null ? void 0 : _b.length)) {
|
|
14
|
+
continue;
|
|
15
|
+
}
|
|
16
|
+
if (currentBranch && event.branch && !currentBranch.startsWith(event.branch)) {
|
|
17
|
+
continue;
|
|
18
|
+
}
|
|
19
|
+
if (isAuthEvent(event)) {
|
|
20
|
+
continue;
|
|
21
|
+
}
|
|
22
|
+
if (isToolConfirmationEvent(event)) {
|
|
23
|
+
continue;
|
|
24
|
+
}
|
|
25
|
+
filteredEvents.push(
|
|
26
|
+
isEventFromAnotherAgent(agentName, event) ? convertForeignEvent(event) : event
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
let resultEvents = rearrangeEventsForLatestFunctionResponse(filteredEvents);
|
|
30
|
+
resultEvents = rearrangeEventsForAsyncFunctionResponsesInHistory(resultEvents);
|
|
31
|
+
const contents = [];
|
|
32
|
+
for (const event of resultEvents) {
|
|
33
|
+
const content = deepClone(event.content);
|
|
34
|
+
removeClientFunctionCallId(content);
|
|
35
|
+
contents.push(content);
|
|
36
|
+
}
|
|
37
|
+
return contents;
|
|
38
|
+
}
|
|
39
|
+
function getCurrentTurnContents(events, agentName, currentBranch) {
|
|
40
|
+
for (let i = events.length - 1; i >= 0; i--) {
|
|
41
|
+
const event = events[i];
|
|
42
|
+
if (event.author === "user" || isEventFromAnotherAgent(agentName, event)) {
|
|
43
|
+
return getContents(events.slice(i), agentName, currentBranch);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return [];
|
|
47
|
+
}
|
|
48
|
+
function isAuthEvent(event) {
|
|
49
|
+
var _a, _b, _c;
|
|
50
|
+
if (!((_a = event.content) == null ? void 0 : _a.parts)) {
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
for (const part of event.content.parts) {
|
|
54
|
+
if (((_b = part.functionCall) == null ? void 0 : _b.name) === REQUEST_EUC_FUNCTION_CALL_NAME || ((_c = part.functionResponse) == null ? void 0 : _c.name) === REQUEST_EUC_FUNCTION_CALL_NAME) {
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
function isToolConfirmationEvent(event) {
|
|
61
|
+
var _a, _b, _c;
|
|
62
|
+
if (!((_a = event.content) == null ? void 0 : _a.parts)) {
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
for (const part of event.content.parts) {
|
|
66
|
+
if (((_b = part.functionCall) == null ? void 0 : _b.name) === REQUEST_CONFIRMATION_FUNCTION_CALL_NAME || ((_c = part.functionResponse) == null ? void 0 : _c.name) === REQUEST_CONFIRMATION_FUNCTION_CALL_NAME) {
|
|
67
|
+
return true;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
function isEventFromAnotherAgent(agentName, event) {
|
|
73
|
+
return !!agentName && event.author !== agentName && event.author !== "user";
|
|
74
|
+
}
|
|
75
|
+
function convertForeignEvent(event) {
|
|
76
|
+
var _a, _b, _c, _d, _e, _f;
|
|
77
|
+
if (!((_b = (_a = event.content) == null ? void 0 : _a.parts) == null ? void 0 : _b.length)) {
|
|
78
|
+
return event;
|
|
79
|
+
}
|
|
80
|
+
const content = {
|
|
81
|
+
role: "user",
|
|
82
|
+
parts: [{
|
|
83
|
+
text: "For context:"
|
|
84
|
+
}]
|
|
85
|
+
};
|
|
86
|
+
for (const part of event.content.parts) {
|
|
87
|
+
if (part.text && !part.thought) {
|
|
88
|
+
(_c = content.parts) == null ? void 0 : _c.push({
|
|
89
|
+
text: "[".concat(event.author, "] said: ").concat(part.text)
|
|
90
|
+
});
|
|
91
|
+
} else if (part.functionCall) {
|
|
92
|
+
const argsText = safeStringify(part.functionCall.args);
|
|
93
|
+
(_d = content.parts) == null ? void 0 : _d.push({
|
|
94
|
+
text: "[".concat(event.author, "] called tool `").concat(part.functionCall.name, "` with parameters: ").concat(argsText)
|
|
95
|
+
});
|
|
96
|
+
} else if (part.functionResponse) {
|
|
97
|
+
const responseText = safeStringify(part.functionResponse.response);
|
|
98
|
+
(_e = content.parts) == null ? void 0 : _e.push({
|
|
99
|
+
text: "[".concat(event.author, "] tool `").concat(part.functionResponse.name, "` returned result: ").concat(responseText)
|
|
100
|
+
});
|
|
101
|
+
} else {
|
|
102
|
+
(_f = content.parts) == null ? void 0 : _f.push(part);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return createEvent({
|
|
106
|
+
invocationId: event.invocationId,
|
|
107
|
+
author: "user",
|
|
108
|
+
content,
|
|
109
|
+
branch: event.branch,
|
|
110
|
+
timestamp: event.timestamp
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
function mergeFunctionResponseEvents(events) {
|
|
114
|
+
var _a;
|
|
115
|
+
if (events.length === 0) {
|
|
116
|
+
throw new Error("Cannot merge an empty list of events.");
|
|
117
|
+
}
|
|
118
|
+
const mergedEvent = createEvent(events[0]);
|
|
119
|
+
const partsInMergedEvent = ((_a = mergedEvent.content) == null ? void 0 : _a.parts) || [];
|
|
120
|
+
if (partsInMergedEvent.length === 0) {
|
|
121
|
+
throw new Error("There should be at least one function_response part.");
|
|
122
|
+
}
|
|
123
|
+
const partIndicesInMergedEvent = {};
|
|
124
|
+
for (let i = 0; i < partsInMergedEvent.length; i++) {
|
|
125
|
+
const part = partsInMergedEvent[i];
|
|
126
|
+
if (part.functionResponse && part.functionResponse.id) {
|
|
127
|
+
partIndicesInMergedEvent[part.functionResponse.id] = i;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
for (const event of events.slice(1)) {
|
|
131
|
+
if (!event.content || !event.content.parts) {
|
|
132
|
+
throw new Error("There should be at least one function_response part.");
|
|
133
|
+
}
|
|
134
|
+
for (const part of event.content.parts) {
|
|
135
|
+
if (part.functionResponse && part.functionResponse.id) {
|
|
136
|
+
const functionCallId = part.functionResponse.id;
|
|
137
|
+
if (functionCallId in partIndicesInMergedEvent) {
|
|
138
|
+
partsInMergedEvent[partIndicesInMergedEvent[functionCallId]] = part;
|
|
139
|
+
} else {
|
|
140
|
+
partsInMergedEvent.push(part);
|
|
141
|
+
partIndicesInMergedEvent[functionCallId] = partsInMergedEvent.length - 1;
|
|
142
|
+
}
|
|
143
|
+
} else {
|
|
144
|
+
partsInMergedEvent.push(part);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
return mergedEvent;
|
|
149
|
+
}
|
|
150
|
+
function rearrangeEventsForLatestFunctionResponse(events) {
|
|
151
|
+
if (events.length === 0) {
|
|
152
|
+
return events;
|
|
153
|
+
}
|
|
154
|
+
const latestEvent = events[events.length - 1];
|
|
155
|
+
const functionResponses = getFunctionResponses(latestEvent);
|
|
156
|
+
if (!(functionResponses == null ? void 0 : functionResponses.length)) {
|
|
157
|
+
return events;
|
|
158
|
+
}
|
|
159
|
+
let functionResponsesIds = new Set(
|
|
160
|
+
functionResponses.filter((response) => !!response.id).map((response) => response.id)
|
|
161
|
+
);
|
|
162
|
+
const secondLatestEvent = events.at(-2);
|
|
163
|
+
if (secondLatestEvent) {
|
|
164
|
+
const functionCallsFromSecondLatest = getFunctionCalls(secondLatestEvent);
|
|
165
|
+
if (functionCallsFromSecondLatest) {
|
|
166
|
+
for (const functionCall of functionCallsFromSecondLatest) {
|
|
167
|
+
if (functionCall.id && functionResponsesIds.has(functionCall.id)) {
|
|
168
|
+
return events;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
let functionCallEventIdx = -1;
|
|
174
|
+
for (let idx = events.length - 2; idx >= 0; idx--) {
|
|
175
|
+
const event = events[idx];
|
|
176
|
+
const functionCalls = getFunctionCalls(event);
|
|
177
|
+
if (!(functionCalls == null ? void 0 : functionCalls.length)) {
|
|
178
|
+
continue;
|
|
179
|
+
}
|
|
180
|
+
for (const functionCall of functionCalls) {
|
|
181
|
+
if (functionCall.id && functionResponsesIds.has(functionCall.id)) {
|
|
182
|
+
functionCallEventIdx = idx;
|
|
183
|
+
const functionCallIds = new Set(
|
|
184
|
+
functionCalls.map((fc) => fc.id).filter((id) => !!id)
|
|
185
|
+
);
|
|
186
|
+
const isSubset = Array.from(functionResponsesIds).every((id) => functionCallIds.has(id));
|
|
187
|
+
if (!isSubset) {
|
|
188
|
+
throw new Error(
|
|
189
|
+
"Last response event should only contain the responses for the function calls in the same function call event. Function" + " call ids found : ".concat(Array.from(functionCallIds).join(", "), ", function response") + " ids provided: ".concat(Array.from(functionResponsesIds).join(", "))
|
|
190
|
+
);
|
|
191
|
+
}
|
|
192
|
+
functionResponsesIds = functionCallIds;
|
|
193
|
+
break;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
if (functionCallEventIdx === -1) {
|
|
198
|
+
throw new Error(
|
|
199
|
+
"No function call event found for function responses ids: ".concat(Array.from(
|
|
200
|
+
functionResponsesIds
|
|
201
|
+
).join(", "))
|
|
202
|
+
);
|
|
203
|
+
}
|
|
204
|
+
const functionResponseEvents = [];
|
|
205
|
+
for (let idx = functionCallEventIdx + 1; idx < events.length - 1; idx++) {
|
|
206
|
+
const event = events[idx];
|
|
207
|
+
const responses = getFunctionResponses(event);
|
|
208
|
+
if (responses && responses.some(
|
|
209
|
+
(response) => response.id && functionResponsesIds.has(response.id)
|
|
210
|
+
)) {
|
|
211
|
+
functionResponseEvents.push(event);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
functionResponseEvents.push(events[events.length - 1]);
|
|
215
|
+
const resultEvents = events.slice(0, functionCallEventIdx + 1);
|
|
216
|
+
resultEvents.push(mergeFunctionResponseEvents(functionResponseEvents));
|
|
217
|
+
return resultEvents;
|
|
218
|
+
}
|
|
219
|
+
function rearrangeEventsForAsyncFunctionResponsesInHistory(events) {
|
|
220
|
+
const functionCallIdToResponseEventIndex = /* @__PURE__ */ new Map();
|
|
221
|
+
for (let i = 0; i < events.length; i++) {
|
|
222
|
+
const event = events[i];
|
|
223
|
+
const functionResponses = getFunctionResponses(event);
|
|
224
|
+
if (functionResponses == null ? void 0 : functionResponses.length) {
|
|
225
|
+
for (const functionResponse of functionResponses) {
|
|
226
|
+
if (!functionResponse.id) {
|
|
227
|
+
continue;
|
|
228
|
+
}
|
|
229
|
+
functionCallIdToResponseEventIndex.set(functionResponse.id, i);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
const resultEvents = [];
|
|
234
|
+
for (const event of events) {
|
|
235
|
+
if (getFunctionResponses(event).length > 0) {
|
|
236
|
+
continue;
|
|
237
|
+
}
|
|
238
|
+
const functionCalls = getFunctionCalls(event);
|
|
239
|
+
if (functionCalls == null ? void 0 : functionCalls.length) {
|
|
240
|
+
const functionResponseEventsIndices = /* @__PURE__ */ new Set();
|
|
241
|
+
for (const functionCall of functionCalls) {
|
|
242
|
+
const functionCallId = functionCall.id;
|
|
243
|
+
if (functionCallId && functionCallIdToResponseEventIndex.has(functionCallId)) {
|
|
244
|
+
functionResponseEventsIndices.add(
|
|
245
|
+
functionCallIdToResponseEventIndex.get(functionCallId)
|
|
246
|
+
);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
resultEvents.push(event);
|
|
250
|
+
if (functionResponseEventsIndices.size === 0) {
|
|
251
|
+
continue;
|
|
252
|
+
}
|
|
253
|
+
if (functionResponseEventsIndices.size === 1) {
|
|
254
|
+
const [responseIndex] = [...functionResponseEventsIndices];
|
|
255
|
+
resultEvents.push(events[responseIndex]);
|
|
256
|
+
} else {
|
|
257
|
+
const indicesArray = Array.from(functionResponseEventsIndices).sort((a, b) => a - b);
|
|
258
|
+
const eventsToMerge = indicesArray.map((index) => events[index]);
|
|
259
|
+
resultEvents.push(mergeFunctionResponseEvents(eventsToMerge));
|
|
260
|
+
}
|
|
261
|
+
} else {
|
|
262
|
+
resultEvents.push(event);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
return resultEvents;
|
|
266
|
+
}
|
|
267
|
+
function safeStringify(obj) {
|
|
268
|
+
if (typeof obj === "string") {
|
|
269
|
+
return obj;
|
|
270
|
+
}
|
|
271
|
+
try {
|
|
272
|
+
return JSON.stringify(obj);
|
|
273
|
+
} catch (e) {
|
|
274
|
+
return String(obj);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
export {
|
|
278
|
+
getContents,
|
|
279
|
+
getCurrentTurnContents
|
|
280
|
+
};
|