@google/adk 0.3.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/a2a/a2a_event.js +290 -0
- package/dist/cjs/a2a/event_converter_utils.js +201 -0
- package/dist/cjs/a2a/executor_context.js +53 -0
- package/dist/cjs/a2a/metadata_converter_utils.js +125 -0
- package/dist/cjs/a2a/part_converter_utils.js +212 -0
- package/dist/cjs/agents/active_streaming_tool.js +1 -1
- package/dist/cjs/agents/base_agent.js +6 -6
- package/dist/cjs/agents/content_processor_utils.js +1 -1
- package/dist/cjs/{tools/tool_context.js → agents/context.js} +71 -16
- package/dist/cjs/agents/functions.js +4 -3
- package/dist/cjs/agents/instructions.js +1 -1
- package/dist/cjs/agents/invocation_context.js +1 -1
- package/dist/cjs/agents/live_request_queue.js +1 -1
- package/dist/cjs/agents/llm_agent.js +76 -711
- package/dist/cjs/agents/loop_agent.js +1 -1
- package/dist/cjs/agents/parallel_agent.js +1 -1
- package/dist/cjs/agents/processors/agent_transfer_llm_request_processor.js +132 -0
- package/dist/cjs/agents/{base_llm_processor.js → processors/base_llm_processor.js} +1 -1
- package/dist/cjs/agents/processors/basic_llm_request_processor.js +68 -0
- package/dist/cjs/agents/processors/code_execution_request_processor.js +389 -0
- package/dist/cjs/agents/processors/content_request_processor.js +66 -0
- package/dist/cjs/agents/processors/identity_llm_request_processor.js +54 -0
- package/dist/cjs/agents/processors/instructions_llm_request_processor.js +85 -0
- package/dist/cjs/agents/processors/request_confirmation_llm_request_processor.js +165 -0
- package/dist/cjs/agents/readonly_context.js +13 -1
- package/dist/cjs/agents/run_config.js +2 -1
- package/dist/cjs/agents/sequential_agent.js +1 -1
- package/dist/cjs/agents/transcription_entry.js +1 -1
- package/dist/cjs/artifacts/base_artifact_service.js +1 -1
- package/dist/cjs/artifacts/file_artifact_service.js +491 -0
- package/dist/cjs/artifacts/gcs_artifact_service.js +127 -48
- package/dist/cjs/artifacts/in_memory_artifact_service.js +54 -6
- package/dist/cjs/artifacts/registry.js +55 -0
- package/dist/cjs/auth/auth_credential.js +1 -1
- package/dist/cjs/auth/auth_handler.js +1 -1
- package/dist/cjs/auth/auth_schemes.js +1 -1
- package/dist/cjs/auth/auth_tool.js +1 -1
- package/dist/cjs/auth/credential_service/base_credential_service.js +1 -1
- package/dist/cjs/auth/credential_service/in_memory_credential_service.js +1 -1
- package/dist/cjs/auth/exchanger/base_credential_exchanger.js +1 -1
- package/dist/cjs/auth/exchanger/credential_exchanger_registry.js +1 -1
- package/dist/cjs/code_executors/base_code_executor.js +1 -1
- package/dist/cjs/code_executors/built_in_code_executor.js +1 -1
- package/dist/cjs/code_executors/code_execution_utils.js +1 -1
- package/dist/cjs/code_executors/code_executor_context.js +1 -1
- package/dist/cjs/common.js +25 -8
- package/dist/cjs/events/event.js +33 -4
- package/dist/cjs/events/event_actions.js +2 -2
- package/dist/cjs/events/structured_events.js +105 -0
- package/dist/cjs/examples/base_example_provider.js +1 -1
- package/dist/cjs/examples/example.js +1 -1
- package/dist/cjs/examples/example_util.js +1 -1
- package/dist/cjs/index.js +24 -17
- package/dist/cjs/index.js.map +4 -4
- package/dist/cjs/index_web.js +1 -1
- package/dist/cjs/memory/base_memory_service.js +1 -1
- package/dist/cjs/memory/in_memory_memory_service.js +1 -1
- package/dist/cjs/memory/memory_entry.js +1 -1
- package/dist/cjs/models/apigee_llm.js +182 -0
- package/dist/cjs/models/base_llm.js +1 -1
- package/dist/cjs/models/base_llm_connection.js +1 -1
- package/dist/cjs/models/gemini_llm_connection.js +1 -1
- package/dist/cjs/models/google_llm.js +70 -51
- package/dist/cjs/models/llm_request.js +1 -1
- package/dist/cjs/models/llm_response.js +3 -1
- package/dist/cjs/models/registry.js +3 -1
- package/dist/cjs/plugins/base_plugin.js +2 -2
- package/dist/cjs/plugins/logging_plugin.js +1 -1
- package/dist/cjs/plugins/plugin_manager.js +1 -1
- package/dist/cjs/plugins/security_plugin.js +1 -1
- package/dist/cjs/runner/in_memory_runner.js +1 -1
- package/dist/cjs/runner/runner.js +33 -2
- package/dist/cjs/sessions/base_session_service.js +53 -3
- package/dist/cjs/sessions/database_session_service.js +367 -0
- package/dist/cjs/sessions/db/operations.js +126 -0
- package/dist/cjs/sessions/db/schema.js +204 -0
- package/dist/cjs/sessions/in_memory_session_service.js +24 -22
- package/dist/cjs/sessions/registry.js +49 -0
- package/dist/cjs/sessions/session.js +1 -1
- package/dist/cjs/sessions/state.js +1 -1
- package/dist/cjs/telemetry/google_cloud.js +1 -1
- package/dist/cjs/telemetry/setup.js +1 -1
- package/dist/cjs/telemetry/tracing.js +1 -1
- package/dist/cjs/tools/agent_tool.js +1 -1
- package/dist/cjs/tools/base_tool.js +4 -1
- package/dist/cjs/tools/base_toolset.js +14 -4
- package/dist/cjs/tools/exit_loop_tool.js +63 -0
- package/dist/cjs/tools/forwarding_artifact_service.js +17 -1
- package/dist/cjs/tools/function_tool.js +1 -1
- package/dist/cjs/tools/google_search_tool.js +1 -1
- package/dist/cjs/tools/long_running_tool.js +1 -1
- package/dist/cjs/tools/mcp/mcp_session_manager.js +1 -1
- package/dist/cjs/tools/mcp/mcp_tool.js +1 -1
- package/dist/cjs/tools/mcp/mcp_toolset.js +10 -6
- package/dist/cjs/tools/tool_confirmation.js +1 -1
- package/dist/cjs/utils/client_labels.js +1 -1
- package/dist/cjs/utils/env_aware_utils.js +10 -1
- package/dist/cjs/utils/gemini_schema_util.js +1 -1
- package/dist/cjs/utils/logger.js +62 -55
- package/dist/cjs/utils/model_name.js +1 -1
- package/dist/cjs/utils/object_notation_utils.js +78 -0
- package/dist/cjs/utils/simple_zod_to_json.js +1 -1
- package/dist/cjs/utils/variant_utils.js +3 -9
- package/dist/cjs/version.js +2 -2
- package/dist/esm/a2a/a2a_event.js +243 -0
- package/dist/esm/a2a/event_converter_utils.js +187 -0
- package/dist/esm/a2a/executor_context.js +23 -0
- package/dist/esm/a2a/metadata_converter_utils.js +90 -0
- package/dist/esm/a2a/part_converter_utils.js +175 -0
- package/dist/esm/agents/base_agent.js +5 -5
- package/dist/esm/{tools/tool_context.js → agents/context.js} +66 -11
- package/dist/esm/agents/functions.js +3 -2
- package/dist/esm/agents/llm_agent.js +69 -720
- package/dist/esm/agents/processors/agent_transfer_llm_request_processor.js +101 -0
- package/dist/esm/agents/processors/basic_llm_request_processor.js +37 -0
- package/dist/esm/agents/processors/code_execution_request_processor.js +363 -0
- package/dist/esm/agents/processors/content_request_processor.js +38 -0
- package/dist/esm/agents/processors/identity_llm_request_processor.js +23 -0
- package/dist/esm/agents/processors/instructions_llm_request_processor.js +54 -0
- package/dist/esm/agents/processors/request_confirmation_llm_request_processor.js +140 -0
- package/dist/esm/agents/readonly_context.js +12 -0
- package/dist/esm/agents/run_config.js +1 -0
- package/dist/esm/artifacts/file_artifact_service.js +451 -0
- package/dist/esm/artifacts/gcs_artifact_service.js +126 -47
- package/dist/esm/artifacts/in_memory_artifact_service.js +51 -4
- package/dist/esm/artifacts/registry.js +28 -0
- package/dist/esm/common.js +20 -10
- package/dist/esm/events/event.js +29 -2
- package/dist/esm/events/event_actions.js +1 -1
- package/dist/esm/events/structured_events.js +74 -0
- package/dist/esm/index.js +24 -17
- package/dist/esm/index.js.map +4 -4
- package/dist/esm/models/apigee_llm.js +152 -0
- package/dist/esm/models/google_llm.js +67 -49
- package/dist/esm/models/llm_response.js +2 -0
- package/dist/esm/models/registry.js +2 -0
- package/dist/esm/plugins/base_plugin.js +1 -1
- package/dist/esm/runner/runner.js +32 -1
- package/dist/esm/sessions/base_session_service.js +49 -1
- package/dist/esm/sessions/database_session_service.js +353 -0
- package/dist/esm/sessions/db/operations.js +111 -0
- package/dist/esm/sessions/db/schema.js +172 -0
- package/dist/esm/sessions/in_memory_session_service.js +23 -21
- package/dist/esm/sessions/registry.js +25 -0
- package/dist/esm/tools/base_tool.js +3 -0
- package/dist/esm/tools/base_toolset.js +11 -2
- package/dist/esm/tools/exit_loop_tool.js +32 -0
- package/dist/esm/tools/forwarding_artifact_service.js +16 -0
- package/dist/esm/tools/mcp/mcp_toolset.js +9 -5
- package/dist/esm/utils/env_aware_utils.js +8 -0
- package/dist/esm/utils/logger.js +51 -54
- package/dist/esm/utils/object_notation_utils.js +47 -0
- package/dist/esm/utils/variant_utils.js +1 -7
- package/dist/esm/version.js +1 -1
- package/dist/types/a2a/a2a_event.d.ts +122 -0
- package/dist/types/a2a/event_converter_utils.d.ts +20 -0
- package/dist/types/a2a/executor_context.d.ts +33 -0
- package/dist/types/a2a/metadata_converter_utils.d.ts +62 -0
- package/dist/types/a2a/part_converter_utils.d.ts +48 -0
- package/dist/types/agents/base_agent.d.ts +2 -2
- package/dist/types/{tools/tool_context.d.ts → agents/context.d.ts} +43 -8
- package/dist/types/agents/llm_agent.d.ts +19 -42
- package/dist/types/agents/processors/agent_transfer_llm_request_processor.d.ts +18 -0
- package/dist/types/agents/{base_llm_processor.d.ts → processors/base_llm_processor.d.ts} +4 -4
- package/dist/types/agents/processors/basic_llm_request_processor.d.ts +13 -0
- package/dist/types/agents/processors/code_execution_request_processor.d.ts +34 -0
- package/dist/types/agents/processors/content_request_processor.d.ts +13 -0
- package/dist/types/agents/processors/identity_llm_request_processor.d.ts +13 -0
- package/dist/types/agents/processors/instructions_llm_request_processor.d.ts +16 -0
- package/dist/types/agents/processors/request_confirmation_llm_request_processor.d.ts +13 -0
- package/dist/types/agents/readonly_context.d.ts +8 -0
- package/dist/types/agents/run_config.d.ts +6 -0
- package/dist/types/artifacts/base_artifact_service.d.ts +31 -0
- package/dist/types/artifacts/file_artifact_service.d.ts +43 -0
- package/dist/types/artifacts/gcs_artifact_service.d.ts +3 -1
- package/dist/types/artifacts/in_memory_artifact_service.d.ts +5 -2
- package/dist/types/artifacts/registry.d.ts +7 -0
- package/dist/types/auth/credential_service/base_credential_service.d.ts +3 -3
- package/dist/types/auth/credential_service/in_memory_credential_service.d.ts +3 -3
- package/dist/types/common.d.ts +15 -6
- package/dist/types/events/event.d.ts +15 -1
- package/dist/types/events/event_actions.d.ts +1 -1
- package/dist/types/events/structured_events.d.ts +106 -0
- package/dist/types/index.d.ts +5 -1
- package/dist/types/models/apigee_llm.d.ts +59 -0
- package/dist/types/models/google_llm.d.ts +5 -2
- package/dist/types/models/llm_response.d.ts +5 -1
- package/dist/types/plugins/base_plugin.d.ts +12 -13
- package/dist/types/plugins/logging_plugin.d.ts +9 -10
- package/dist/types/plugins/plugin_manager.d.ts +9 -10
- package/dist/types/plugins/security_plugin.d.ts +2 -2
- package/dist/types/runner/runner.d.ts +15 -0
- package/dist/types/sessions/base_session_service.d.ts +20 -0
- package/dist/types/sessions/database_session_service.d.ts +32 -0
- package/dist/types/sessions/db/operations.d.ts +29 -0
- package/dist/types/sessions/db/schema.d.ts +45 -0
- package/dist/types/sessions/in_memory_session_service.d.ts +4 -1
- package/dist/types/sessions/registry.d.ts +7 -0
- package/dist/types/tools/base_tool.d.ts +3 -3
- package/dist/types/tools/base_toolset.d.ts +12 -3
- package/dist/types/tools/exit_loop_tool.d.ts +24 -0
- package/dist/types/tools/forwarding_artifact_service.d.ts +5 -3
- package/dist/types/tools/function_tool.d.ts +2 -2
- package/dist/types/tools/mcp/mcp_toolset.d.ts +1 -1
- package/dist/types/utils/env_aware_utils.d.ts +7 -0
- package/dist/types/utils/logger.d.ts +5 -9
- package/dist/types/utils/object_notation_utils.d.ts +21 -0
- package/dist/types/version.d.ts +1 -1
- package/dist/web/a2a/a2a_event.js +243 -0
- package/dist/web/a2a/event_converter_utils.js +201 -0
- package/dist/web/a2a/executor_context.js +23 -0
- package/dist/web/a2a/metadata_converter_utils.js +107 -0
- package/dist/web/a2a/part_converter_utils.js +175 -0
- package/dist/web/agents/base_agent.js +5 -5
- package/dist/web/{tools/tool_context.js → agents/context.js} +66 -11
- package/dist/web/agents/functions.js +3 -2
- package/dist/web/agents/llm_agent.js +90 -717
- package/dist/web/agents/processors/agent_transfer_llm_request_processor.js +100 -0
- package/dist/web/agents/processors/basic_llm_request_processor.js +71 -0
- package/dist/web/agents/processors/code_execution_request_processor.js +365 -0
- package/dist/web/agents/processors/content_request_processor.js +56 -0
- package/dist/web/agents/processors/identity_llm_request_processor.js +41 -0
- package/dist/web/agents/processors/instructions_llm_request_processor.js +72 -0
- package/dist/web/agents/processors/request_confirmation_llm_request_processor.js +158 -0
- package/dist/web/agents/readonly_context.js +12 -0
- package/dist/web/agents/run_config.js +2 -1
- package/dist/web/artifacts/file_artifact_service.js +506 -0
- package/dist/web/artifacts/gcs_artifact_service.js +123 -46
- package/dist/web/artifacts/in_memory_artifact_service.js +51 -4
- package/dist/web/artifacts/registry.js +28 -0
- package/dist/web/common.js +20 -10
- package/dist/web/events/event.js +29 -2
- package/dist/web/events/event_actions.js +1 -1
- package/dist/web/events/structured_events.js +74 -0
- package/dist/web/index.js +7 -2
- package/dist/web/index.js.map +4 -4
- package/dist/web/models/apigee_llm.js +219 -0
- package/dist/web/models/google_llm.js +67 -46
- package/dist/web/models/llm_response.js +2 -0
- package/dist/web/models/registry.js +2 -0
- package/dist/web/plugins/base_plugin.js +1 -1
- package/dist/web/runner/runner.js +34 -1
- package/dist/web/sessions/base_session_service.js +49 -1
- package/dist/web/sessions/database_session_service.js +371 -0
- package/dist/web/sessions/db/operations.js +111 -0
- package/dist/web/sessions/db/schema.js +172 -0
- package/dist/web/sessions/in_memory_session_service.js +23 -21
- package/dist/web/sessions/registry.js +25 -0
- package/dist/web/tools/base_tool.js +3 -0
- package/dist/web/tools/base_toolset.js +11 -2
- package/dist/web/tools/exit_loop_tool.js +32 -0
- package/dist/web/tools/forwarding_artifact_service.js +16 -0
- package/dist/web/tools/mcp/mcp_toolset.js +27 -5
- package/dist/web/utils/env_aware_utils.js +8 -0
- package/dist/web/utils/logger.js +51 -54
- package/dist/web/utils/object_notation_utils.js +47 -0
- package/dist/web/utils/variant_utils.js +1 -7
- package/dist/web/version.js +1 -1
- package/package.json +13 -3
- package/dist/cjs/agents/callback_context.js +0 -101
- package/dist/esm/agents/callback_context.js +0 -71
- package/dist/types/agents/callback_context.d.ts +0 -42
- package/dist/web/agents/callback_context.js +0 -71
- /package/dist/esm/agents/{base_llm_processor.js → processors/base_llm_processor.js} +0 -0
- /package/dist/web/agents/{base_llm_processor.js → processors/base_llm_processor.js} +0 -0
|
@@ -5,713 +5,47 @@
|
|
|
5
5
|
*/
|
|
6
6
|
var _a, _b;
|
|
7
7
|
import { context, trace } from "@opentelemetry/api";
|
|
8
|
-
import { cloneDeep } from "lodash-es";
|
|
9
|
-
import { z } from "zod";
|
|
10
|
-
import {
|
|
11
|
-
isBaseCodeExecutor
|
|
12
|
-
} from "../code_executors/base_code_executor.js";
|
|
13
|
-
import { isBuiltInCodeExecutor } from "../code_executors/built_in_code_executor.js";
|
|
14
|
-
import {
|
|
15
|
-
buildCodeExecutionResultPart,
|
|
16
|
-
buildExecutableCodePart,
|
|
17
|
-
convertCodeExecutionParts,
|
|
18
|
-
extractCodeAndTruncateContent
|
|
19
|
-
} from "../code_executors/code_execution_utils.js";
|
|
20
|
-
import { CodeExecutorContext } from "../code_executors/code_executor_context.js";
|
|
21
8
|
import {
|
|
22
9
|
createEvent,
|
|
23
10
|
createNewEventId,
|
|
24
11
|
getFunctionCalls,
|
|
25
|
-
getFunctionResponses,
|
|
26
12
|
isFinalResponse
|
|
27
13
|
} from "../events/event.js";
|
|
28
|
-
import { createEventActions } from "../events/event_actions.js";
|
|
29
14
|
import { isBaseLlm } from "../models/base_llm.js";
|
|
30
|
-
import {
|
|
31
|
-
appendInstructions,
|
|
32
|
-
setOutputSchema
|
|
33
|
-
} from "../models/llm_request.js";
|
|
34
15
|
import { LLMRegistry } from "../models/registry.js";
|
|
35
|
-
import {
|
|
36
|
-
import { BaseTool } from "../tools/base_tool.js";
|
|
37
|
-
import { FunctionTool } from "../tools/function_tool.js";
|
|
38
|
-
import { ToolConfirmation } from "../tools/tool_confirmation.js";
|
|
39
|
-
import { ToolContext } from "../tools/tool_context.js";
|
|
40
|
-
import { base64Decode } from "../utils/env_aware_utils.js";
|
|
16
|
+
import { isBaseTool } from "../tools/base_tool.js";
|
|
41
17
|
import { logger } from "../utils/logger.js";
|
|
18
|
+
import { Context } from "./context.js";
|
|
42
19
|
import {
|
|
43
20
|
runAsyncGeneratorWithOtelContext,
|
|
44
21
|
traceCallLlm,
|
|
45
22
|
tracer
|
|
46
23
|
} from "../telemetry/tracing.js";
|
|
24
|
+
import { isZodObject, zodObjectToSchema } from "../utils/simple_zod_to_json.js";
|
|
47
25
|
import { BaseAgent } from "./base_agent.js";
|
|
48
|
-
import {
|
|
49
|
-
BaseLlmRequestProcessor
|
|
50
|
-
} from "./base_llm_processor.js";
|
|
51
|
-
import { CallbackContext } from "./callback_context.js";
|
|
52
|
-
import {
|
|
53
|
-
getContents,
|
|
54
|
-
getCurrentTurnContents
|
|
55
|
-
} from "./content_processor_utils.js";
|
|
56
26
|
import {
|
|
57
27
|
generateAuthEvent,
|
|
58
28
|
generateRequestConfirmationEvent,
|
|
59
29
|
getLongRunningFunctionCalls,
|
|
60
|
-
handleFunctionCallList,
|
|
61
30
|
handleFunctionCallsAsync,
|
|
62
|
-
populateClientFunctionCallId
|
|
63
|
-
REQUEST_CONFIRMATION_FUNCTION_CALL_NAME
|
|
31
|
+
populateClientFunctionCallId
|
|
64
32
|
} from "./functions.js";
|
|
65
|
-
import {
|
|
33
|
+
import { AGENT_TRANSFER_LLM_REQUEST_PROCESSOR } from "./processors/agent_transfer_llm_request_processor.js";
|
|
34
|
+
import { BASIC_LLM_REQUEST_PROCESSOR } from "./processors/basic_llm_request_processor.js";
|
|
35
|
+
import { CODE_EXECUTION_REQUEST_PROCESSOR } from "./processors/code_execution_request_processor.js";
|
|
36
|
+
import { CONTENT_REQUEST_PROCESSOR } from "./processors/content_request_processor.js";
|
|
37
|
+
import { IDENTITY_LLM_REQUEST_PROCESSOR } from "./processors/identity_llm_request_processor.js";
|
|
38
|
+
import { INSTRUCTIONS_LLM_REQUEST_PROCESSOR } from "./processors/instructions_llm_request_processor.js";
|
|
39
|
+
import { REQUEST_CONFIRMATION_LLM_REQUEST_PROCESSOR } from "./processors/request_confirmation_llm_request_processor.js";
|
|
66
40
|
import { ReadonlyContext } from "./readonly_context.js";
|
|
67
41
|
import { StreamingMode } from "./run_config.js";
|
|
68
42
|
const ADK_AGENT_NAME_LABEL_KEY = "adk_agent_name";
|
|
69
43
|
async function convertToolUnionToTools(toolUnion, context2) {
|
|
70
|
-
if (toolUnion
|
|
44
|
+
if (isBaseTool(toolUnion)) {
|
|
71
45
|
return [toolUnion];
|
|
72
46
|
}
|
|
73
47
|
return await toolUnion.getTools(context2);
|
|
74
48
|
}
|
|
75
|
-
class BasicLlmRequestProcessor extends BaseLlmRequestProcessor {
|
|
76
|
-
// eslint-disable-next-line require-yield
|
|
77
|
-
async *runAsync(invocationContext, llmRequest) {
|
|
78
|
-
var _a2;
|
|
79
|
-
const agent = invocationContext.agent;
|
|
80
|
-
if (!isLlmAgent(agent)) {
|
|
81
|
-
return;
|
|
82
|
-
}
|
|
83
|
-
llmRequest.model = agent.canonicalModel.model;
|
|
84
|
-
llmRequest.config = { ...(_a2 = agent.generateContentConfig) != null ? _a2 : {} };
|
|
85
|
-
if (agent.outputSchema) {
|
|
86
|
-
setOutputSchema(llmRequest, agent.outputSchema);
|
|
87
|
-
}
|
|
88
|
-
if (invocationContext.runConfig) {
|
|
89
|
-
llmRequest.liveConnectConfig.responseModalities = invocationContext.runConfig.responseModalities;
|
|
90
|
-
llmRequest.liveConnectConfig.speechConfig = invocationContext.runConfig.speechConfig;
|
|
91
|
-
llmRequest.liveConnectConfig.outputAudioTranscription = invocationContext.runConfig.outputAudioTranscription;
|
|
92
|
-
llmRequest.liveConnectConfig.inputAudioTranscription = invocationContext.runConfig.inputAudioTranscription;
|
|
93
|
-
llmRequest.liveConnectConfig.realtimeInputConfig = invocationContext.runConfig.realtimeInputConfig;
|
|
94
|
-
llmRequest.liveConnectConfig.enableAffectiveDialog = invocationContext.runConfig.enableAffectiveDialog;
|
|
95
|
-
llmRequest.liveConnectConfig.proactivity = invocationContext.runConfig.proactivity;
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
const BASIC_LLM_REQUEST_PROCESSOR = new BasicLlmRequestProcessor();
|
|
100
|
-
class IdentityLlmRequestProcessor extends BaseLlmRequestProcessor {
|
|
101
|
-
// eslint-disable-next-line require-yield
|
|
102
|
-
async *runAsync(invocationContext, llmRequest) {
|
|
103
|
-
const agent = invocationContext.agent;
|
|
104
|
-
const si = [`You are an agent. Your internal name is "${agent.name}".`];
|
|
105
|
-
if (agent.description) {
|
|
106
|
-
si.push(`The description about you is "${agent.description}"`);
|
|
107
|
-
}
|
|
108
|
-
appendInstructions(llmRequest, si);
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
const IDENTITY_LLM_REQUEST_PROCESSOR = new IdentityLlmRequestProcessor();
|
|
112
|
-
class InstructionsLlmRequestProcessor extends BaseLlmRequestProcessor {
|
|
113
|
-
/**
|
|
114
|
-
* Handles instructions and global instructions for LLM flow.
|
|
115
|
-
*/
|
|
116
|
-
// eslint-disable-next-line require-yield
|
|
117
|
-
async *runAsync(invocationContext, llmRequest) {
|
|
118
|
-
const agent = invocationContext.agent;
|
|
119
|
-
if (!(agent instanceof LlmAgent) || !(agent.rootAgent instanceof LlmAgent)) {
|
|
120
|
-
return;
|
|
121
|
-
}
|
|
122
|
-
const rootAgent = agent.rootAgent;
|
|
123
|
-
if (isLlmAgent(rootAgent) && rootAgent.globalInstruction) {
|
|
124
|
-
const { instruction, requireStateInjection } = await rootAgent.canonicalGlobalInstruction(
|
|
125
|
-
new ReadonlyContext(invocationContext)
|
|
126
|
-
);
|
|
127
|
-
let instructionWithState = instruction;
|
|
128
|
-
if (requireStateInjection) {
|
|
129
|
-
instructionWithState = await injectSessionState(
|
|
130
|
-
instruction,
|
|
131
|
-
new ReadonlyContext(invocationContext)
|
|
132
|
-
);
|
|
133
|
-
}
|
|
134
|
-
appendInstructions(llmRequest, [instructionWithState]);
|
|
135
|
-
}
|
|
136
|
-
if (agent.instruction) {
|
|
137
|
-
const { instruction, requireStateInjection } = await agent.canonicalInstruction(
|
|
138
|
-
new ReadonlyContext(invocationContext)
|
|
139
|
-
);
|
|
140
|
-
let instructionWithState = instruction;
|
|
141
|
-
if (requireStateInjection) {
|
|
142
|
-
instructionWithState = await injectSessionState(
|
|
143
|
-
instruction,
|
|
144
|
-
new ReadonlyContext(invocationContext)
|
|
145
|
-
);
|
|
146
|
-
}
|
|
147
|
-
appendInstructions(llmRequest, [instructionWithState]);
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
const INSTRUCTIONS_LLM_REQUEST_PROCESSOR = new InstructionsLlmRequestProcessor();
|
|
152
|
-
class ContentRequestProcessor {
|
|
153
|
-
// eslint-disable-next-line require-yield
|
|
154
|
-
async *runAsync(invocationContext, llmRequest) {
|
|
155
|
-
const agent = invocationContext.agent;
|
|
156
|
-
if (!agent || !isLlmAgent(agent)) {
|
|
157
|
-
return;
|
|
158
|
-
}
|
|
159
|
-
if (agent.includeContents === "default") {
|
|
160
|
-
llmRequest.contents = getContents(
|
|
161
|
-
invocationContext.session.events,
|
|
162
|
-
agent.name,
|
|
163
|
-
invocationContext.branch
|
|
164
|
-
);
|
|
165
|
-
} else {
|
|
166
|
-
llmRequest.contents = getCurrentTurnContents(
|
|
167
|
-
invocationContext.session.events,
|
|
168
|
-
agent.name,
|
|
169
|
-
invocationContext.branch
|
|
170
|
-
);
|
|
171
|
-
}
|
|
172
|
-
return;
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
const CONTENT_REQUEST_PROCESSOR = new ContentRequestProcessor();
|
|
176
|
-
class AgentTransferLlmRequestProcessor extends BaseLlmRequestProcessor {
|
|
177
|
-
constructor() {
|
|
178
|
-
super(...arguments);
|
|
179
|
-
this.toolName = "transfer_to_agent";
|
|
180
|
-
this.tool = new FunctionTool({
|
|
181
|
-
name: this.toolName,
|
|
182
|
-
description: "Transfer the question to another agent. This tool hands off control to another agent when it is more suitable to answer the user question according to the agent description.",
|
|
183
|
-
parameters: z.object({
|
|
184
|
-
agentName: z.string().describe("the agent name to transfer to.")
|
|
185
|
-
}),
|
|
186
|
-
execute: function(args, toolContext) {
|
|
187
|
-
if (!toolContext) {
|
|
188
|
-
throw new Error("toolContext is required.");
|
|
189
|
-
}
|
|
190
|
-
toolContext.actions.transferToAgent = args.agentName;
|
|
191
|
-
return "Transfer queued";
|
|
192
|
-
}
|
|
193
|
-
});
|
|
194
|
-
}
|
|
195
|
-
// eslint-disable-next-line require-yield
|
|
196
|
-
async *runAsync(invocationContext, llmRequest) {
|
|
197
|
-
if (!(invocationContext.agent instanceof LlmAgent)) {
|
|
198
|
-
return;
|
|
199
|
-
}
|
|
200
|
-
const transferTargets = this.getTransferTargets(invocationContext.agent);
|
|
201
|
-
if (!transferTargets.length) {
|
|
202
|
-
return;
|
|
203
|
-
}
|
|
204
|
-
appendInstructions(llmRequest, [
|
|
205
|
-
this.buildTargetAgentsInstructions(
|
|
206
|
-
invocationContext.agent,
|
|
207
|
-
transferTargets
|
|
208
|
-
)
|
|
209
|
-
]);
|
|
210
|
-
const toolContext = new ToolContext({ invocationContext });
|
|
211
|
-
await this.tool.processLlmRequest({ toolContext, llmRequest });
|
|
212
|
-
}
|
|
213
|
-
buildTargetAgentsInfo(targetAgent) {
|
|
214
|
-
return `
|
|
215
|
-
Agent name: ${targetAgent.name}
|
|
216
|
-
Agent description: ${targetAgent.description}
|
|
217
|
-
`;
|
|
218
|
-
}
|
|
219
|
-
buildTargetAgentsInstructions(agent, targetAgents) {
|
|
220
|
-
let instructions = `
|
|
221
|
-
You have a list of other agents to transfer to:
|
|
222
|
-
|
|
223
|
-
${targetAgents.map(this.buildTargetAgentsInfo).join("\n")}
|
|
224
|
-
|
|
225
|
-
If you are the best to answer the question according to your description, you
|
|
226
|
-
can answer it.
|
|
227
|
-
|
|
228
|
-
If another agent is better for answering the question according to its
|
|
229
|
-
description, call \`${this.toolName}\` function to transfer the
|
|
230
|
-
question to that agent. When transferring, do not generate any text other than
|
|
231
|
-
the function call.
|
|
232
|
-
`;
|
|
233
|
-
if (agent.parentAgent && !agent.disallowTransferToParent) {
|
|
234
|
-
instructions += `
|
|
235
|
-
Your parent agent is ${agent.parentAgent.name}. If neither the other agents nor
|
|
236
|
-
you are best for answering the question according to the descriptions, transfer
|
|
237
|
-
to your parent agent.
|
|
238
|
-
`;
|
|
239
|
-
}
|
|
240
|
-
return instructions;
|
|
241
|
-
}
|
|
242
|
-
getTransferTargets(agent) {
|
|
243
|
-
const targets = [];
|
|
244
|
-
targets.push(...agent.subAgents);
|
|
245
|
-
if (!agent.parentAgent || !isLlmAgent(agent.parentAgent)) {
|
|
246
|
-
return targets;
|
|
247
|
-
}
|
|
248
|
-
if (!agent.disallowTransferToParent) {
|
|
249
|
-
targets.push(agent.parentAgent);
|
|
250
|
-
}
|
|
251
|
-
if (!agent.disallowTransferToPeers) {
|
|
252
|
-
targets.push(
|
|
253
|
-
...agent.parentAgent.subAgents.filter(
|
|
254
|
-
(peerAgent) => peerAgent.name !== agent.name
|
|
255
|
-
)
|
|
256
|
-
);
|
|
257
|
-
}
|
|
258
|
-
return targets;
|
|
259
|
-
}
|
|
260
|
-
}
|
|
261
|
-
const AGENT_TRANSFER_LLM_REQUEST_PROCESSOR = new AgentTransferLlmRequestProcessor();
|
|
262
|
-
class RequestConfirmationLlmRequestProcessor extends BaseLlmRequestProcessor {
|
|
263
|
-
/** Handles tool confirmation information to build the LLM request. */
|
|
264
|
-
async *runAsync(invocationContext) {
|
|
265
|
-
const agent = invocationContext.agent;
|
|
266
|
-
if (!isLlmAgent(agent)) {
|
|
267
|
-
return;
|
|
268
|
-
}
|
|
269
|
-
const events = invocationContext.session.events;
|
|
270
|
-
if (!events || events.length === 0) {
|
|
271
|
-
return;
|
|
272
|
-
}
|
|
273
|
-
const requestConfirmationFunctionResponses = {};
|
|
274
|
-
let confirmationEventIndex = -1;
|
|
275
|
-
for (let i = events.length - 1; i >= 0; i--) {
|
|
276
|
-
const event = events[i];
|
|
277
|
-
if (event.author !== "user") {
|
|
278
|
-
continue;
|
|
279
|
-
}
|
|
280
|
-
const responses = getFunctionResponses(event);
|
|
281
|
-
if (!responses) {
|
|
282
|
-
continue;
|
|
283
|
-
}
|
|
284
|
-
let foundConfirmation = false;
|
|
285
|
-
for (const functionResponse of responses) {
|
|
286
|
-
if (functionResponse.name !== REQUEST_CONFIRMATION_FUNCTION_CALL_NAME) {
|
|
287
|
-
continue;
|
|
288
|
-
}
|
|
289
|
-
foundConfirmation = true;
|
|
290
|
-
let toolConfirmation = null;
|
|
291
|
-
if (functionResponse.response && Object.keys(functionResponse.response).length === 1 && "response" in functionResponse.response) {
|
|
292
|
-
toolConfirmation = JSON.parse(
|
|
293
|
-
functionResponse.response["response"]
|
|
294
|
-
);
|
|
295
|
-
} else if (functionResponse.response) {
|
|
296
|
-
toolConfirmation = new ToolConfirmation({
|
|
297
|
-
hint: functionResponse.response["hint"],
|
|
298
|
-
payload: functionResponse.response["payload"],
|
|
299
|
-
confirmed: functionResponse.response["confirmed"]
|
|
300
|
-
});
|
|
301
|
-
}
|
|
302
|
-
if (functionResponse.id && toolConfirmation) {
|
|
303
|
-
requestConfirmationFunctionResponses[functionResponse.id] = toolConfirmation;
|
|
304
|
-
}
|
|
305
|
-
}
|
|
306
|
-
if (foundConfirmation) {
|
|
307
|
-
confirmationEventIndex = i;
|
|
308
|
-
break;
|
|
309
|
-
}
|
|
310
|
-
}
|
|
311
|
-
if (Object.keys(requestConfirmationFunctionResponses).length === 0) {
|
|
312
|
-
return;
|
|
313
|
-
}
|
|
314
|
-
for (let i = confirmationEventIndex - 1; i >= 0; i--) {
|
|
315
|
-
const event = events[i];
|
|
316
|
-
const functionCalls = getFunctionCalls(event);
|
|
317
|
-
if (!functionCalls) {
|
|
318
|
-
continue;
|
|
319
|
-
}
|
|
320
|
-
const toolsToResumeWithConfirmation = {};
|
|
321
|
-
const toolsToResumeWithArgs = {};
|
|
322
|
-
for (const functionCall of functionCalls) {
|
|
323
|
-
if (!functionCall.id || !(functionCall.id in requestConfirmationFunctionResponses)) {
|
|
324
|
-
continue;
|
|
325
|
-
}
|
|
326
|
-
const args = functionCall.args;
|
|
327
|
-
if (!args || !("originalFunctionCall" in args)) {
|
|
328
|
-
continue;
|
|
329
|
-
}
|
|
330
|
-
const originalFunctionCall = args["originalFunctionCall"];
|
|
331
|
-
if (originalFunctionCall.id) {
|
|
332
|
-
toolsToResumeWithConfirmation[originalFunctionCall.id] = requestConfirmationFunctionResponses[functionCall.id];
|
|
333
|
-
toolsToResumeWithArgs[originalFunctionCall.id] = originalFunctionCall;
|
|
334
|
-
}
|
|
335
|
-
}
|
|
336
|
-
if (Object.keys(toolsToResumeWithConfirmation).length === 0) {
|
|
337
|
-
continue;
|
|
338
|
-
}
|
|
339
|
-
for (let j = events.length - 1; j > confirmationEventIndex; j--) {
|
|
340
|
-
const eventToCheck = events[j];
|
|
341
|
-
const functionResponses = getFunctionResponses(eventToCheck);
|
|
342
|
-
if (!functionResponses) {
|
|
343
|
-
continue;
|
|
344
|
-
}
|
|
345
|
-
for (const fr of functionResponses) {
|
|
346
|
-
if (fr.id && fr.id in toolsToResumeWithConfirmation) {
|
|
347
|
-
delete toolsToResumeWithConfirmation[fr.id];
|
|
348
|
-
delete toolsToResumeWithArgs[fr.id];
|
|
349
|
-
}
|
|
350
|
-
}
|
|
351
|
-
if (Object.keys(toolsToResumeWithConfirmation).length === 0) {
|
|
352
|
-
break;
|
|
353
|
-
}
|
|
354
|
-
}
|
|
355
|
-
if (Object.keys(toolsToResumeWithConfirmation).length === 0) {
|
|
356
|
-
continue;
|
|
357
|
-
}
|
|
358
|
-
const toolsList = await agent.canonicalTools(
|
|
359
|
-
new ReadonlyContext(invocationContext)
|
|
360
|
-
);
|
|
361
|
-
const toolsDict = Object.fromEntries(
|
|
362
|
-
toolsList.map((tool) => [tool.name, tool])
|
|
363
|
-
);
|
|
364
|
-
const functionResponseEvent = await handleFunctionCallList({
|
|
365
|
-
invocationContext,
|
|
366
|
-
functionCalls: Object.values(toolsToResumeWithArgs),
|
|
367
|
-
toolsDict,
|
|
368
|
-
beforeToolCallbacks: agent.canonicalBeforeToolCallbacks,
|
|
369
|
-
afterToolCallbacks: agent.canonicalAfterToolCallbacks,
|
|
370
|
-
filters: new Set(Object.keys(toolsToResumeWithConfirmation)),
|
|
371
|
-
toolConfirmationDict: toolsToResumeWithConfirmation
|
|
372
|
-
});
|
|
373
|
-
if (functionResponseEvent) {
|
|
374
|
-
yield functionResponseEvent;
|
|
375
|
-
}
|
|
376
|
-
return;
|
|
377
|
-
}
|
|
378
|
-
}
|
|
379
|
-
}
|
|
380
|
-
const REQUEST_CONFIRMATION_LLM_REQUEST_PROCESSOR = new RequestConfirmationLlmRequestProcessor();
|
|
381
|
-
class CodeExecutionRequestProcessor extends BaseLlmRequestProcessor {
|
|
382
|
-
async *runAsync(invocationContext, llmRequest) {
|
|
383
|
-
if (!(invocationContext.agent instanceof LlmAgent)) {
|
|
384
|
-
return;
|
|
385
|
-
}
|
|
386
|
-
if (!invocationContext.agent.codeExecutor) {
|
|
387
|
-
return;
|
|
388
|
-
}
|
|
389
|
-
for await (const event of runPreProcessor(invocationContext, llmRequest)) {
|
|
390
|
-
yield event;
|
|
391
|
-
}
|
|
392
|
-
if (!isBaseCodeExecutor(invocationContext.agent.codeExecutor)) {
|
|
393
|
-
return;
|
|
394
|
-
}
|
|
395
|
-
for (const content of llmRequest.contents) {
|
|
396
|
-
const delimeters = invocationContext.agent.codeExecutor.codeBlockDelimiters.length ? invocationContext.agent.codeExecutor.codeBlockDelimiters[0] : ["", ""];
|
|
397
|
-
convertCodeExecutionParts(
|
|
398
|
-
content,
|
|
399
|
-
delimeters,
|
|
400
|
-
invocationContext.agent.codeExecutor.executionResultDelimiters
|
|
401
|
-
);
|
|
402
|
-
}
|
|
403
|
-
}
|
|
404
|
-
}
|
|
405
|
-
const DATA_FILE_UTIL_MAP = {
|
|
406
|
-
"text/csv": {
|
|
407
|
-
extension: ".csv",
|
|
408
|
-
loaderCodeTemplate: "pd.read_csv('{filename}')"
|
|
409
|
-
}
|
|
410
|
-
};
|
|
411
|
-
const DATA_FILE_HELPER_LIB = `
|
|
412
|
-
import pandas as pd
|
|
413
|
-
|
|
414
|
-
def explore_df(df: pd.DataFrame) -> None:
|
|
415
|
-
"""Prints some information about a pandas DataFrame."""
|
|
416
|
-
|
|
417
|
-
with pd.option_context(
|
|
418
|
-
'display.max_columns', None, 'display.expand_frame_repr', False
|
|
419
|
-
):
|
|
420
|
-
# Print the column names to never encounter KeyError when selecting one.
|
|
421
|
-
df_dtypes = df.dtypes
|
|
422
|
-
|
|
423
|
-
# Obtain information about data types and missing values.
|
|
424
|
-
df_nulls = (len(df) - df.isnull().sum()).apply(
|
|
425
|
-
lambda x: f'{x} / {df.shape[0]} non-null'
|
|
426
|
-
)
|
|
427
|
-
|
|
428
|
-
# Explore unique total values in columns using \`.unique()\`.
|
|
429
|
-
df_unique_count = df.apply(lambda x: len(x.unique()))
|
|
430
|
-
|
|
431
|
-
# Explore unique values in columns using \`.unique()\`.
|
|
432
|
-
df_unique = df.apply(lambda x: crop(str(list(x.unique()))))
|
|
433
|
-
|
|
434
|
-
df_info = pd.concat(
|
|
435
|
-
(
|
|
436
|
-
df_dtypes.rename('Dtype'),
|
|
437
|
-
df_nulls.rename('Non-Null Count'),
|
|
438
|
-
df_unique_count.rename('Unique Values Count'),
|
|
439
|
-
df_unique.rename('Unique Values'),
|
|
440
|
-
),
|
|
441
|
-
axis=1,
|
|
442
|
-
)
|
|
443
|
-
df_info.index.name = 'Columns'
|
|
444
|
-
print(f"""Total rows: {df.shape[0]}
|
|
445
|
-
Total columns: {df.shape[1]}
|
|
446
|
-
|
|
447
|
-
{df_info}""")
|
|
448
|
-
`;
|
|
449
|
-
class CodeExecutionResponseProcessor {
|
|
450
|
-
/**
|
|
451
|
-
* Processes the LLM response asynchronously.
|
|
452
|
-
*
|
|
453
|
-
* @param invocationContext The invocation context
|
|
454
|
-
* @param llmResponse The LLM response to process
|
|
455
|
-
* @returns An async generator yielding events
|
|
456
|
-
*/
|
|
457
|
-
async *runAsync(invocationContext, llmResponse) {
|
|
458
|
-
if (llmResponse.partial) {
|
|
459
|
-
return;
|
|
460
|
-
}
|
|
461
|
-
for await (const event of runPostProcessor(
|
|
462
|
-
invocationContext,
|
|
463
|
-
llmResponse
|
|
464
|
-
)) {
|
|
465
|
-
yield event;
|
|
466
|
-
}
|
|
467
|
-
}
|
|
468
|
-
}
|
|
469
|
-
const responseProcessor = new CodeExecutionResponseProcessor();
|
|
470
|
-
async function* runPreProcessor(invocationContext, llmRequest) {
|
|
471
|
-
const agent = invocationContext.agent;
|
|
472
|
-
if (!isLlmAgent(agent)) {
|
|
473
|
-
return;
|
|
474
|
-
}
|
|
475
|
-
const codeExecutor = agent.codeExecutor;
|
|
476
|
-
if (!codeExecutor || !isBaseCodeExecutor(codeExecutor)) {
|
|
477
|
-
return;
|
|
478
|
-
}
|
|
479
|
-
if (isBuiltInCodeExecutor(codeExecutor)) {
|
|
480
|
-
codeExecutor.processLlmRequest(llmRequest);
|
|
481
|
-
return;
|
|
482
|
-
}
|
|
483
|
-
if (!codeExecutor.optimizeDataFile) {
|
|
484
|
-
return;
|
|
485
|
-
}
|
|
486
|
-
const codeExecutorContext = new CodeExecutorContext(
|
|
487
|
-
new State(invocationContext.session.state)
|
|
488
|
-
);
|
|
489
|
-
if (codeExecutorContext.getErrorCount(invocationContext.invocationId) >= codeExecutor.errorRetryAttempts) {
|
|
490
|
-
return;
|
|
491
|
-
}
|
|
492
|
-
const allInputFiles = extractAndReplaceInlineFiles(
|
|
493
|
-
codeExecutorContext,
|
|
494
|
-
llmRequest
|
|
495
|
-
);
|
|
496
|
-
const processedFileNames = new Set(
|
|
497
|
-
codeExecutorContext.getProcessedFileNames()
|
|
498
|
-
);
|
|
499
|
-
const filesToProcess = allInputFiles.filter(
|
|
500
|
-
(f) => !processedFileNames.has(f.name)
|
|
501
|
-
);
|
|
502
|
-
for (const file of filesToProcess) {
|
|
503
|
-
const codeStr = getDataFilePreprocessingCode(file);
|
|
504
|
-
if (!codeStr) {
|
|
505
|
-
return;
|
|
506
|
-
}
|
|
507
|
-
const codeContent = {
|
|
508
|
-
role: "model",
|
|
509
|
-
parts: [
|
|
510
|
-
{ text: `Processing input file: \`${file.name}\`` },
|
|
511
|
-
buildExecutableCodePart(codeStr)
|
|
512
|
-
]
|
|
513
|
-
};
|
|
514
|
-
llmRequest.contents.push(cloneDeep(codeContent));
|
|
515
|
-
yield createEvent({
|
|
516
|
-
invocationId: invocationContext.invocationId,
|
|
517
|
-
author: agent.name,
|
|
518
|
-
branch: invocationContext.branch,
|
|
519
|
-
content: codeContent
|
|
520
|
-
});
|
|
521
|
-
const executionId = getOrSetExecutionId(
|
|
522
|
-
invocationContext,
|
|
523
|
-
codeExecutorContext
|
|
524
|
-
);
|
|
525
|
-
const codeExecutionResult = await codeExecutor.executeCode({
|
|
526
|
-
invocationContext,
|
|
527
|
-
codeExecutionInput: {
|
|
528
|
-
code: codeStr,
|
|
529
|
-
inputFiles: [file],
|
|
530
|
-
executionId
|
|
531
|
-
}
|
|
532
|
-
});
|
|
533
|
-
codeExecutorContext.updateCodeExecutionResult({
|
|
534
|
-
invocationId: invocationContext.invocationId,
|
|
535
|
-
code: codeStr,
|
|
536
|
-
resultStdout: codeExecutionResult.stdout,
|
|
537
|
-
resultStderr: codeExecutionResult.stderr
|
|
538
|
-
});
|
|
539
|
-
codeExecutorContext.addProcessedFileNames([file.name]);
|
|
540
|
-
const executionResultEvent = await postProcessCodeExecutionResult(
|
|
541
|
-
invocationContext,
|
|
542
|
-
codeExecutorContext,
|
|
543
|
-
codeExecutionResult
|
|
544
|
-
);
|
|
545
|
-
yield executionResultEvent;
|
|
546
|
-
llmRequest.contents.push(cloneDeep(executionResultEvent.content));
|
|
547
|
-
}
|
|
548
|
-
}
|
|
549
|
-
async function* runPostProcessor(invocationContext, llmResponse) {
|
|
550
|
-
const agent = invocationContext.agent;
|
|
551
|
-
if (!isLlmAgent(agent)) {
|
|
552
|
-
return;
|
|
553
|
-
}
|
|
554
|
-
const codeExecutor = agent.codeExecutor;
|
|
555
|
-
if (!codeExecutor || !isBaseCodeExecutor(codeExecutor)) {
|
|
556
|
-
return;
|
|
557
|
-
}
|
|
558
|
-
if (!llmResponse || !llmResponse.content) {
|
|
559
|
-
return;
|
|
560
|
-
}
|
|
561
|
-
if (isBuiltInCodeExecutor(codeExecutor)) {
|
|
562
|
-
return;
|
|
563
|
-
}
|
|
564
|
-
const codeExecutorContext = new CodeExecutorContext(
|
|
565
|
-
new State(invocationContext.session.state)
|
|
566
|
-
);
|
|
567
|
-
if (codeExecutorContext.getErrorCount(invocationContext.invocationId) >= codeExecutor.errorRetryAttempts) {
|
|
568
|
-
return;
|
|
569
|
-
}
|
|
570
|
-
const responseContent = llmResponse.content;
|
|
571
|
-
const codeStr = extractCodeAndTruncateContent(
|
|
572
|
-
responseContent,
|
|
573
|
-
codeExecutor.codeBlockDelimiters
|
|
574
|
-
);
|
|
575
|
-
if (!codeStr) {
|
|
576
|
-
return;
|
|
577
|
-
}
|
|
578
|
-
yield createEvent({
|
|
579
|
-
invocationId: invocationContext.invocationId,
|
|
580
|
-
author: agent.name,
|
|
581
|
-
branch: invocationContext.branch,
|
|
582
|
-
content: responseContent
|
|
583
|
-
});
|
|
584
|
-
const executionId = getOrSetExecutionId(
|
|
585
|
-
invocationContext,
|
|
586
|
-
codeExecutorContext
|
|
587
|
-
);
|
|
588
|
-
const codeExecutionResult = await codeExecutor.executeCode({
|
|
589
|
-
invocationContext,
|
|
590
|
-
codeExecutionInput: {
|
|
591
|
-
code: codeStr,
|
|
592
|
-
inputFiles: codeExecutorContext.getInputFiles(),
|
|
593
|
-
executionId
|
|
594
|
-
}
|
|
595
|
-
});
|
|
596
|
-
codeExecutorContext.updateCodeExecutionResult({
|
|
597
|
-
invocationId: invocationContext.invocationId,
|
|
598
|
-
code: codeStr,
|
|
599
|
-
resultStdout: codeExecutionResult.stdout,
|
|
600
|
-
resultStderr: codeExecutionResult.stderr
|
|
601
|
-
});
|
|
602
|
-
yield await postProcessCodeExecutionResult(
|
|
603
|
-
invocationContext,
|
|
604
|
-
codeExecutorContext,
|
|
605
|
-
codeExecutionResult
|
|
606
|
-
);
|
|
607
|
-
llmResponse.content = void 0;
|
|
608
|
-
}
|
|
609
|
-
function extractAndReplaceInlineFiles(codeExecutorContext, llmRequest) {
|
|
610
|
-
var _a2;
|
|
611
|
-
const allInputFiles = codeExecutorContext.getInputFiles();
|
|
612
|
-
const savedFileNames = new Set(allInputFiles.map((f) => f.name));
|
|
613
|
-
for (let i = 0; i < llmRequest.contents.length; i++) {
|
|
614
|
-
const content = llmRequest.contents[i];
|
|
615
|
-
if (content.role !== "user" || !content.parts) {
|
|
616
|
-
continue;
|
|
617
|
-
}
|
|
618
|
-
for (let j = 0; j < content.parts.length; j++) {
|
|
619
|
-
const part = content.parts[j];
|
|
620
|
-
const mimeType = (_a2 = part.inlineData) == null ? void 0 : _a2.mimeType;
|
|
621
|
-
if (!mimeType || !part.inlineData || !DATA_FILE_UTIL_MAP[mimeType]) {
|
|
622
|
-
continue;
|
|
623
|
-
}
|
|
624
|
-
const fileName = `data_${i + 1}_${j + 1}${DATA_FILE_UTIL_MAP[mimeType].extension}`;
|
|
625
|
-
part.text = `
|
|
626
|
-
Available file: \`${fileName}\`
|
|
627
|
-
`;
|
|
628
|
-
const file = {
|
|
629
|
-
name: fileName,
|
|
630
|
-
content: base64Decode(part.inlineData.data),
|
|
631
|
-
mimeType
|
|
632
|
-
};
|
|
633
|
-
if (!savedFileNames.has(fileName)) {
|
|
634
|
-
codeExecutorContext.addInputFiles([file]);
|
|
635
|
-
allInputFiles.push(file);
|
|
636
|
-
}
|
|
637
|
-
}
|
|
638
|
-
}
|
|
639
|
-
return allInputFiles;
|
|
640
|
-
}
|
|
641
|
-
function getOrSetExecutionId(invocationContext, codeExecutorContext) {
|
|
642
|
-
var _a2;
|
|
643
|
-
const agent = invocationContext.agent;
|
|
644
|
-
if (!isLlmAgent(agent) || !((_a2 = agent.codeExecutor) == null ? void 0 : _a2.stateful)) {
|
|
645
|
-
return void 0;
|
|
646
|
-
}
|
|
647
|
-
let executionId = codeExecutorContext.getExecutionId();
|
|
648
|
-
if (!executionId) {
|
|
649
|
-
executionId = invocationContext.session.id;
|
|
650
|
-
codeExecutorContext.setExecutionId(executionId);
|
|
651
|
-
}
|
|
652
|
-
return executionId;
|
|
653
|
-
}
|
|
654
|
-
async function postProcessCodeExecutionResult(invocationContext, codeExecutorContext, codeExecutionResult) {
|
|
655
|
-
if (!invocationContext.artifactService) {
|
|
656
|
-
throw new Error("Artifact service is not initialized.");
|
|
657
|
-
}
|
|
658
|
-
const resultContent = {
|
|
659
|
-
role: "model",
|
|
660
|
-
parts: [buildCodeExecutionResultPart(codeExecutionResult)]
|
|
661
|
-
};
|
|
662
|
-
const eventActions = createEventActions({
|
|
663
|
-
stateDelta: codeExecutorContext.getStateDelta()
|
|
664
|
-
});
|
|
665
|
-
if (codeExecutionResult.stderr) {
|
|
666
|
-
codeExecutorContext.incrementErrorCount(invocationContext.invocationId);
|
|
667
|
-
} else {
|
|
668
|
-
codeExecutorContext.resetErrorCount(invocationContext.invocationId);
|
|
669
|
-
}
|
|
670
|
-
for (const outputFile of codeExecutionResult.outputFiles) {
|
|
671
|
-
const version = await invocationContext.artifactService.saveArtifact({
|
|
672
|
-
appName: invocationContext.appName || "",
|
|
673
|
-
userId: invocationContext.userId || "",
|
|
674
|
-
sessionId: invocationContext.session.id,
|
|
675
|
-
filename: outputFile.name,
|
|
676
|
-
artifact: {
|
|
677
|
-
inlineData: { data: outputFile.content, mimeType: outputFile.mimeType }
|
|
678
|
-
}
|
|
679
|
-
});
|
|
680
|
-
eventActions.artifactDelta[outputFile.name] = version;
|
|
681
|
-
}
|
|
682
|
-
return createEvent({
|
|
683
|
-
invocationId: invocationContext.invocationId,
|
|
684
|
-
author: invocationContext.agent.name,
|
|
685
|
-
branch: invocationContext.branch,
|
|
686
|
-
content: resultContent,
|
|
687
|
-
actions: eventActions
|
|
688
|
-
});
|
|
689
|
-
}
|
|
690
|
-
function getDataFilePreprocessingCode(file) {
|
|
691
|
-
function getNormalizedFileName(fileName) {
|
|
692
|
-
const [varName2] = fileName.split(".");
|
|
693
|
-
let normalizedName = varName2.replace(/[^a-zA-Z0-9_]/g, "_");
|
|
694
|
-
if (/^\d/.test(normalizedName)) {
|
|
695
|
-
normalizedName = "_" + normalizedName;
|
|
696
|
-
}
|
|
697
|
-
return normalizedName;
|
|
698
|
-
}
|
|
699
|
-
if (!DATA_FILE_UTIL_MAP[file.mimeType]) {
|
|
700
|
-
return void 0;
|
|
701
|
-
}
|
|
702
|
-
const varName = getNormalizedFileName(file.name);
|
|
703
|
-
const loaderCode = DATA_FILE_UTIL_MAP[file.mimeType].loaderCodeTemplate.replace("{filename}", file.name);
|
|
704
|
-
return `
|
|
705
|
-
${DATA_FILE_HELPER_LIB}
|
|
706
|
-
|
|
707
|
-
# Load the dataframe.
|
|
708
|
-
${varName} = ${loaderCode}
|
|
709
|
-
|
|
710
|
-
# Use \`explore_df\` to guide my analysis.
|
|
711
|
-
explore_df(${varName})
|
|
712
|
-
`;
|
|
713
|
-
}
|
|
714
|
-
const CODE_EXECUTION_REQUEST_PROCESSOR = new CodeExecutionRequestProcessor();
|
|
715
49
|
const LLM_AGENT_SIGNATURE_SYMBOL = Symbol.for("google.adk.llmAgent");
|
|
716
50
|
function isLlmAgent(obj) {
|
|
717
51
|
return typeof obj === "object" && obj !== null && LLM_AGENT_SIGNATURE_SYMBOL in obj && obj[LLM_AGENT_SIGNATURE_SYMBOL] === true;
|
|
@@ -730,8 +64,8 @@ class LlmAgent extends (_b = BaseAgent, _a = LLM_AGENT_SIGNATURE_SYMBOL, _b) {
|
|
|
730
64
|
this.disallowTransferToParent = (_d = config.disallowTransferToParent) != null ? _d : false;
|
|
731
65
|
this.disallowTransferToPeers = (_e = config.disallowTransferToPeers) != null ? _e : false;
|
|
732
66
|
this.includeContents = (_f = config.includeContents) != null ? _f : "default";
|
|
733
|
-
this.inputSchema = config.inputSchema;
|
|
734
|
-
this.outputSchema = config.outputSchema;
|
|
67
|
+
this.inputSchema = isZodObject(config.inputSchema) ? zodObjectToSchema(config.inputSchema) : config.inputSchema;
|
|
68
|
+
this.outputSchema = isZodObject(config.outputSchema) ? zodObjectToSchema(config.outputSchema) : config.outputSchema;
|
|
735
69
|
this.outputKey = config.outputKey;
|
|
736
70
|
this.beforeModelCallback = config.beforeModelCallback;
|
|
737
71
|
this.afterModelCallback = config.afterModelCallback;
|
|
@@ -776,16 +110,6 @@ class LlmAgent extends (_b = BaseAgent, _a = LLM_AGENT_SIGNATURE_SYMBOL, _b) {
|
|
|
776
110
|
this.disallowTransferToParent = true;
|
|
777
111
|
this.disallowTransferToPeers = true;
|
|
778
112
|
}
|
|
779
|
-
if (this.subAgents && this.subAgents.length > 0) {
|
|
780
|
-
throw new Error(
|
|
781
|
-
`Invalid config for agent ${this.name}: if outputSchema is set, subAgents must be empty to disable agent transfer.`
|
|
782
|
-
);
|
|
783
|
-
}
|
|
784
|
-
if (this.tools && this.tools.length > 0) {
|
|
785
|
-
throw new Error(
|
|
786
|
-
`Invalid config for agent ${this.name}: if outputSchema is set, tools must be empty`
|
|
787
|
-
);
|
|
788
|
-
}
|
|
789
113
|
}
|
|
790
114
|
}
|
|
791
115
|
/**
|
|
@@ -1006,7 +330,7 @@ class LlmAgent extends (_b = BaseAgent, _a = LLM_AGENT_SIGNATURE_SYMBOL, _b) {
|
|
|
1006
330
|
}
|
|
1007
331
|
}
|
|
1008
332
|
for (const toolUnion of this.tools) {
|
|
1009
|
-
const toolContext = new
|
|
333
|
+
const toolContext = new Context({ invocationContext });
|
|
1010
334
|
const tools = await convertToolUnionToTools(
|
|
1011
335
|
toolUnion,
|
|
1012
336
|
new ReadonlyContext(invocationContext)
|
|
@@ -1029,28 +353,36 @@ class LlmAgent extends (_b = BaseAgent, _a = LLM_AGENT_SIGNATURE_SYMBOL, _b) {
|
|
|
1029
353
|
ctx,
|
|
1030
354
|
this,
|
|
1031
355
|
async function* () {
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
llmRequest,
|
|
1035
|
-
modelResponseEvent
|
|
1036
|
-
)) {
|
|
1037
|
-
for await (const event of this.postprocess(
|
|
356
|
+
const responsesGenerator = async function* () {
|
|
357
|
+
for await (const llmResponse of this.callLlmAsync(
|
|
1038
358
|
invocationContext,
|
|
1039
359
|
llmRequest,
|
|
1040
|
-
llmResponse,
|
|
1041
360
|
modelResponseEvent
|
|
1042
361
|
)) {
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
362
|
+
for await (const event of this.postprocess(
|
|
363
|
+
invocationContext,
|
|
364
|
+
llmRequest,
|
|
365
|
+
llmResponse,
|
|
366
|
+
modelResponseEvent
|
|
367
|
+
)) {
|
|
368
|
+
modelResponseEvent.id = createNewEventId();
|
|
369
|
+
modelResponseEvent.timestamp = (/* @__PURE__ */ new Date()).getTime();
|
|
370
|
+
yield event;
|
|
371
|
+
}
|
|
1046
372
|
}
|
|
1047
|
-
}
|
|
373
|
+
};
|
|
374
|
+
yield* this.runAndHandleError(
|
|
375
|
+
responsesGenerator.call(this),
|
|
376
|
+
invocationContext,
|
|
377
|
+
llmRequest,
|
|
378
|
+
modelResponseEvent
|
|
379
|
+
);
|
|
1048
380
|
}
|
|
1049
381
|
);
|
|
1050
382
|
span.end();
|
|
1051
383
|
}
|
|
1052
384
|
async *postprocess(invocationContext, llmRequest, llmResponse, modelResponseEvent) {
|
|
1053
|
-
var _a2;
|
|
385
|
+
var _a2, _b2;
|
|
1054
386
|
for (const processor of this.responseProcessors) {
|
|
1055
387
|
for await (const event of processor.runAsync(
|
|
1056
388
|
invocationContext,
|
|
@@ -1079,6 +411,10 @@ class LlmAgent extends (_b = BaseAgent, _a = LLM_AGENT_SIGNATURE_SYMBOL, _b) {
|
|
|
1079
411
|
if (!((_a2 = getFunctionCalls(mergedEvent)) == null ? void 0 : _a2.length)) {
|
|
1080
412
|
return;
|
|
1081
413
|
}
|
|
414
|
+
if ((_b2 = invocationContext.runConfig) == null ? void 0 : _b2.pauseOnToolCalls) {
|
|
415
|
+
invocationContext.endInvocation = true;
|
|
416
|
+
return;
|
|
417
|
+
}
|
|
1082
418
|
const functionResponseEvent = await handleFunctionCallsAsync({
|
|
1083
419
|
invocationContext,
|
|
1084
420
|
functionCallEvent: mergedEvent,
|
|
@@ -1103,6 +439,8 @@ class LlmAgent extends (_b = BaseAgent, _a = LLM_AGENT_SIGNATURE_SYMBOL, _b) {
|
|
|
1103
439
|
});
|
|
1104
440
|
if (toolConfirmationEvent) {
|
|
1105
441
|
yield toolConfirmationEvent;
|
|
442
|
+
invocationContext.endInvocation = true;
|
|
443
|
+
return;
|
|
1106
444
|
}
|
|
1107
445
|
yield functionResponseEvent;
|
|
1108
446
|
const nextAgentName = functionResponseEvent.actions.transferToAgent;
|
|
@@ -1159,12 +497,7 @@ class LlmAgent extends (_b = BaseAgent, _a = LLM_AGENT_SIGNATURE_SYMBOL, _b) {
|
|
|
1159
497
|
/* stream= */
|
|
1160
498
|
((_e = invocationContext.runConfig) == null ? void 0 : _e.streamingMode) === StreamingMode.SSE
|
|
1161
499
|
);
|
|
1162
|
-
for await (const llmResponse of
|
|
1163
|
-
responsesGenerator,
|
|
1164
|
-
invocationContext,
|
|
1165
|
-
llmRequest,
|
|
1166
|
-
modelResponseEvent
|
|
1167
|
-
)) {
|
|
500
|
+
for await (const llmResponse of responsesGenerator) {
|
|
1168
501
|
traceCallLlm({
|
|
1169
502
|
invocationContext,
|
|
1170
503
|
eventId: modelResponseEvent.id,
|
|
@@ -1181,7 +514,7 @@ class LlmAgent extends (_b = BaseAgent, _a = LLM_AGENT_SIGNATURE_SYMBOL, _b) {
|
|
|
1181
514
|
}
|
|
1182
515
|
}
|
|
1183
516
|
async handleBeforeModelCallback(invocationContext, llmRequest, modelResponseEvent) {
|
|
1184
|
-
const callbackContext = new
|
|
517
|
+
const callbackContext = new Context({
|
|
1185
518
|
invocationContext,
|
|
1186
519
|
eventActions: modelResponseEvent.actions
|
|
1187
520
|
});
|
|
@@ -1204,7 +537,7 @@ class LlmAgent extends (_b = BaseAgent, _a = LLM_AGENT_SIGNATURE_SYMBOL, _b) {
|
|
|
1204
537
|
return void 0;
|
|
1205
538
|
}
|
|
1206
539
|
async handleAfterModelCallback(invocationContext, llmResponse, modelResponseEvent) {
|
|
1207
|
-
const callbackContext = new
|
|
540
|
+
const callbackContext = new Context({
|
|
1208
541
|
invocationContext,
|
|
1209
542
|
eventActions: modelResponseEvent.actions
|
|
1210
543
|
});
|
|
@@ -1232,7 +565,7 @@ class LlmAgent extends (_b = BaseAgent, _a = LLM_AGENT_SIGNATURE_SYMBOL, _b) {
|
|
|
1232
565
|
yield response;
|
|
1233
566
|
}
|
|
1234
567
|
} catch (modelError) {
|
|
1235
|
-
const callbackContext = new
|
|
568
|
+
const callbackContext = new Context({
|
|
1236
569
|
invocationContext,
|
|
1237
570
|
eventActions: modelResponseEvent.actions
|
|
1238
571
|
});
|
|
@@ -1245,11 +578,29 @@ class LlmAgent extends (_b = BaseAgent, _a = LLM_AGENT_SIGNATURE_SYMBOL, _b) {
|
|
|
1245
578
|
if (onModelErrorCallbackResponse) {
|
|
1246
579
|
yield onModelErrorCallbackResponse;
|
|
1247
580
|
} else {
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
581
|
+
let errorCode = "UNKNOWN_ERROR";
|
|
582
|
+
let errorMessage = modelError.message;
|
|
583
|
+
try {
|
|
584
|
+
const errorResponse = JSON.parse(modelError.message);
|
|
585
|
+
if (errorResponse == null ? void 0 : errorResponse.error) {
|
|
586
|
+
errorCode = String(errorResponse.error.code || "UNKNOWN_ERROR");
|
|
587
|
+
errorMessage = errorResponse.error.message || errorMessage;
|
|
588
|
+
}
|
|
589
|
+
} catch {
|
|
590
|
+
}
|
|
591
|
+
if (modelResponseEvent.actions) {
|
|
592
|
+
yield createEvent({
|
|
593
|
+
invocationId: invocationContext.invocationId,
|
|
594
|
+
author: this.name,
|
|
595
|
+
errorCode,
|
|
596
|
+
errorMessage
|
|
597
|
+
});
|
|
598
|
+
} else {
|
|
599
|
+
yield {
|
|
600
|
+
errorCode,
|
|
601
|
+
errorMessage
|
|
602
|
+
};
|
|
603
|
+
}
|
|
1253
604
|
}
|
|
1254
605
|
} else {
|
|
1255
606
|
logger.error("Unknown error during response generation", modelError);
|
|
@@ -1266,7 +617,5 @@ class LlmAgent extends (_b = BaseAgent, _a = LLM_AGENT_SIGNATURE_SYMBOL, _b) {
|
|
|
1266
617
|
}
|
|
1267
618
|
export {
|
|
1268
619
|
LlmAgent,
|
|
1269
|
-
|
|
1270
|
-
isLlmAgent,
|
|
1271
|
-
responseProcessor
|
|
620
|
+
isLlmAgent
|
|
1272
621
|
};
|