@botbotgo/agent-harness 0.0.475 → 0.0.476
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -1234
- package/README.zh.md +3 -1191
- package/dist/acp.js +1 -1
- package/dist/api.js +1 -404
- package/dist/benchmark/checkpoint-resume-cost-benchmark.js +1 -55
- package/dist/benchmark/deepagent-local-model-benchmark.js +2 -35
- package/dist/benchmark/upstream-runtime-ab-benchmark.js +1 -179
- package/dist/cli/chat-interactive.js +25 -244
- package/dist/cli/chat-rendering.js +6 -100
- package/dist/cli/chat-stream.js +23 -512
- package/dist/cli/chat-ui.js +21 -199
- package/dist/cli/chat-workspace.js +2 -210
- package/dist/cli/main.js +21 -428
- package/dist/cli/managed-service-commands.js +9 -63
- package/dist/cli/managed-service.js +2 -137
- package/dist/cli/options-init-chat.js +1 -108
- package/dist/cli/options-runtime.js +1 -158
- package/dist/cli/options-serve.js +1 -282
- package/dist/cli/options.js +2 -19
- package/dist/cli/process-guards.js +1 -139
- package/dist/cli/request-tree.js +7 -296
- package/dist/cli/runtime-commands.js +12 -258
- package/dist/cli/runtime-output.js +16 -155
- package/dist/cli/server-commands.js +16 -270
- package/dist/cli/workspace.js +1 -67
- package/dist/cli.js +1 -7
- package/dist/client/acp.js +1 -1
- package/dist/client/in-process.js +1 -67
- package/dist/client/index.js +1 -2
- package/dist/client/types.js +0 -1
- package/dist/client.js +1 -1
- package/dist/contracts/core.js +1 -1
- package/dist/contracts/runtime-evaluation.js +0 -1
- package/dist/contracts/runtime-memory.js +0 -1
- package/dist/contracts/runtime-observability.js +0 -1
- package/dist/contracts/runtime-requests.js +0 -1
- package/dist/contracts/runtime-scheduling.js +0 -1
- package/dist/contracts/runtime.js +1 -27
- package/dist/contracts/types.js +1 -3
- package/dist/contracts/workspace.js +0 -1
- package/dist/flow/build-flow-graph.js +1 -50
- package/dist/flow/export-mermaid.js +2 -464
- package/dist/flow/export-sequence-mermaid.js +2 -325
- package/dist/flow/flow-graph-normalization.js +1 -214
- package/dist/flow/flow-graph-runtime.js +1 -107
- package/dist/flow/flow-graph-upstream.js +1 -494
- package/dist/flow/index.js +1 -3
- package/dist/flow/types.js +0 -1
- package/dist/index.js +1 -5
- package/dist/init-project.js +1 -1
- package/dist/knowledge/config.js +1 -32
- package/dist/knowledge/contracts.js +0 -1
- package/dist/knowledge/index.js +1 -2
- package/dist/knowledge/module.js +12 -909
- package/dist/knowledge/procedural/config.js +1 -125
- package/dist/knowledge/procedural/index.js +1 -2
- package/dist/knowledge/procedural/manager.js +9 -345
- package/dist/mcp.js +1 -2
- package/dist/package-version.d.ts +1 -1
- package/dist/package-version.js +1 -2
- package/dist/persistence/file-store.js +3 -758
- package/dist/persistence/sqlite-request-context-store.js +5 -54
- package/dist/persistence/sqlite-request-queue-store.js +10 -108
- package/dist/persistence/sqlite-runtime.js +1 -86
- package/dist/persistence/sqlite-store.js +62 -810
- package/dist/persistence/types.js +0 -1
- package/dist/projections/presentation.js +37 -206
- package/dist/projections/request-events.js +2 -502
- package/dist/projections/upstream-events.js +1 -201
- package/dist/protocol/a2a/http-discovery.js +1 -178
- package/dist/protocol/a2a/http-rpc.js +6 -622
- package/dist/protocol/a2a/http.js +1 -138
- package/dist/protocol/a2a/task-state.js +3 -317
- package/dist/protocol/acp/client.js +8 -294
- package/dist/protocol/acp/harness-client.js +1 -218
- package/dist/protocol/acp/http.js +5 -130
- package/dist/protocol/acp/server.js +1 -310
- package/dist/protocol/acp/stdio.js +2 -69
- package/dist/protocol/ag-ui/http.js +3 -378
- package/dist/protocol/mcp/server.js +1 -428
- package/dist/resource/backend/workspace-scoped-backend.js +1 -319
- package/dist/resource/isolation.js +1 -237
- package/dist/resource/mcp/tool-support.js +3 -296
- package/dist/resource/mcp-tool-support.js +1 -2
- package/dist/resource/providers/resource-provider.js +1 -215
- package/dist/resource/resource-impl.js +1 -3
- package/dist/resource/resource-types.js +0 -1
- package/dist/resource/resource.js +1 -1
- package/dist/resource/sources.js +1 -247
- package/dist/resource/tools/function-tool-resolver.js +2 -272
- package/dist/runtime/adapter/compat/deepagent-compat.js +1 -29
- package/dist/runtime/adapter/compat/openai-compatible.js +1 -55
- package/dist/runtime/adapter/direct-builtin-utility.js +2 -90
- package/dist/runtime/adapter/flow/execution-context.js +1 -71
- package/dist/runtime/adapter/flow/invocation-flow.js +8 -425
- package/dist/runtime/adapter/flow/invoke-runtime.js +1 -20
- package/dist/runtime/adapter/flow/stream-runtime.js +11 -1395
- package/dist/runtime/adapter/invocation-result.js +2 -473
- package/dist/runtime/adapter/local-tool-invocation.js +6 -638
- package/dist/runtime/adapter/middleware/context-hygiene.js +1 -83
- package/dist/runtime/adapter/middleware-assembly.js +5 -477
- package/dist/runtime/adapter/model/invocation-request.js +3 -183
- package/dist/runtime/adapter/model/message-assembly.js +1 -28
- package/dist/runtime/adapter/model/model-providers.js +23 -1115
- package/dist/runtime/adapter/model/prompted-json-tool-call-capture.js +1 -40
- package/dist/runtime/adapter/model/prompted-json-tool-policy.js +1 -22
- package/dist/runtime/adapter/resilience.js +1 -104
- package/dist/runtime/adapter/runtime-adapter-support.js +3 -141
- package/dist/runtime/adapter/runtime-shell.js +5 -166
- package/dist/runtime/adapter/stream-event-projection.js +2 -622
- package/dist/runtime/adapter/stream-text-consumption.js +1 -18
- package/dist/runtime/adapter/terminal-status.js +2 -67
- package/dist/runtime/adapter/tool/builtin-middleware-tools.js +6 -627
- package/dist/runtime/adapter/tool/declared-middleware.js +1 -154
- package/dist/runtime/adapter/tool/interrupt-policy.js +1 -34
- package/dist/runtime/adapter/tool/provider-tool.js +1 -25
- package/dist/runtime/adapter/tool/resolved-tool.js +1 -225
- package/dist/runtime/adapter/tool/tool-arguments.js +3 -486
- package/dist/runtime/adapter/tool/tool-hitl.js +1 -346
- package/dist/runtime/adapter/tool/tool-name-mapping.js +1 -128
- package/dist/runtime/adapter/tool/tool-output-artifacts.js +2 -88
- package/dist/runtime/adapter/tool/tool-replay.js +1 -37
- package/dist/runtime/adapter/tool-resolution.js +1 -86
- package/dist/runtime/adapter/upstream-configurable-keys.js +1 -2
- package/dist/runtime/agent-runtime-adapter.js +60 -2338
- package/dist/runtime/agent-runtime-assembly.js +7 -249
- package/dist/runtime/env/runtime-env.js +1 -62
- package/dist/runtime/harness/background-runtime.js +1 -8
- package/dist/runtime/harness/bindings.js +1 -58
- package/dist/runtime/harness/events/event-bus.js +1 -16
- package/dist/runtime/harness/events/event-sink.js +1 -61
- package/dist/runtime/harness/events/events.js +1 -80
- package/dist/runtime/harness/events/listener-runtime.js +1 -13
- package/dist/runtime/harness/events/runtime-event-operations.js +1 -9
- package/dist/runtime/harness/events/streaming.js +1 -100
- package/dist/runtime/harness/events/timeline.js +1 -52
- package/dist/runtime/harness/public-shapes.js +1 -186
- package/dist/runtime/harness/run/artifact-paths.js +1 -15
- package/dist/runtime/harness/run/governance.js +1 -295
- package/dist/runtime/harness/run/helpers.js +1 -71
- package/dist/runtime/harness/run/inspection.js +1 -409
- package/dist/runtime/harness/run/operator-overview.js +1 -80
- package/dist/runtime/harness/run/queue-diagnostics.js +1 -15
- package/dist/runtime/harness/run/recovery.js +1 -162
- package/dist/runtime/harness/run/resources.js +1 -60
- package/dist/runtime/harness/run/resume.js +1 -56
- package/dist/runtime/harness/run/routing.js +1 -48
- package/dist/runtime/harness/run/run-lifecycle.js +1 -66
- package/dist/runtime/harness/run/run-operations.js +1 -217
- package/dist/runtime/harness/run/run-queue.js +1 -43
- package/dist/runtime/harness/run/run-slot-acquisition.js +1 -157
- package/dist/runtime/harness/run/session-records.js +1 -97
- package/dist/runtime/harness/run/start-run.js +1 -120
- package/dist/runtime/harness/run/startup-runtime.js +1 -69
- package/dist/runtime/harness/run/stream-run.js +8 -1418
- package/dist/runtime/harness/run/surface-semantics.js +1 -79
- package/dist/runtime/harness/runtime-defaults.js +1 -39
- package/dist/runtime/harness/system/boundary-analysis.js +1 -234
- package/dist/runtime/harness/system/health-monitor.js +1 -258
- package/dist/runtime/harness/system/inventory.js +1 -129
- package/dist/runtime/harness/system/mem0-ingestion-sync.js +5 -345
- package/dist/runtime/harness/system/policy-engine.js +1 -175
- package/dist/runtime/harness/system/runtime-memory-candidates.js +4 -110
- package/dist/runtime/harness/system/runtime-memory-consolidation.js +1 -51
- package/dist/runtime/harness/system/runtime-memory-manager.js +10 -693
- package/dist/runtime/harness/system/runtime-memory-policy.js +1 -155
- package/dist/runtime/harness/system/runtime-memory-records.js +11 -577
- package/dist/runtime/harness/system/runtime-memory-sync.js +5 -206
- package/dist/runtime/harness/system/session-memory-sync.js +3 -113
- package/dist/runtime/harness/system/skill-requirements.js +1 -112
- package/dist/runtime/harness/system/store.js +9 -365
- package/dist/runtime/harness/tool-gateway/index.js +1 -2
- package/dist/runtime/harness/tool-gateway/policy.js +1 -45
- package/dist/runtime/harness/tool-gateway/validation.js +1 -176
- package/dist/runtime/harness/tool-schema.js +1 -3
- package/dist/runtime/harness.js +3 -1490
- package/dist/runtime/index.js +1 -3
- package/dist/runtime/layout/runtime-layout.js +1 -31
- package/dist/runtime/maintenance/checkpoint-maintenance.js +2 -178
- package/dist/runtime/maintenance/file-checkpoint-saver.js +1 -106
- package/dist/runtime/maintenance/runtime-record-maintenance.js +2 -169
- package/dist/runtime/maintenance/sqlite-checkpoint-saver.js +4 -289
- package/dist/runtime/parsing/output-content.js +10 -550
- package/dist/runtime/parsing/output-parsing.js +1 -4
- package/dist/runtime/parsing/output-recovery.js +3 -213
- package/dist/runtime/parsing/output-tool-args.js +7 -663
- package/dist/runtime/parsing/stream-event-parsing.js +3 -362
- package/dist/runtime/prompts/runtime-prompts.js +4 -73
- package/dist/runtime/scheduling/system-schedule-manager.js +11 -532
- package/dist/runtime/skills/skill-metadata.js +1 -197
- package/dist/runtime/startup-tracing.js +2 -37
- package/dist/runtime/support/compiled-binding.js +1 -290
- package/dist/runtime/support/embedding-models.js +1 -118
- package/dist/runtime/support/harness-support.js +5 -137
- package/dist/runtime/support/llamaindex.js +1 -108
- package/dist/runtime/support/runtime-adapter-options.js +1 -29
- package/dist/runtime/support/runtime-factories.js +1 -51
- package/dist/runtime/support/vector-stores.js +9 -270
- package/dist/scaffold/init-project.js +54 -233
- package/dist/tooling/extensions.js +1 -311
- package/dist/tooling/module-loader.js +1 -55
- package/dist/tools.js +1 -176
- package/dist/utils/agent-display.js +1 -18
- package/dist/utils/bundled-text.js +4 -39
- package/dist/utils/compiled-binding.js +1 -33
- package/dist/utils/fs.js +2 -45
- package/dist/utils/id.js +1 -9
- package/dist/utils/message-content.js +1 -30
- package/dist/utils/object.js +1 -6
- package/dist/workspace/agent-binding-compiler.js +3 -613
- package/dist/workspace/compile.js +1 -472
- package/dist/workspace/framework-contract-validation.js +2 -322
- package/dist/workspace/index.js +1 -1
- package/dist/workspace/object-loader-paths.js +1 -71
- package/dist/workspace/object-loader-readers.js +1 -187
- package/dist/workspace/object-loader.js +1 -754
- package/dist/workspace/resource-compilers.js +1 -374
- package/dist/workspace/support/agent-capabilities.js +1 -37
- package/dist/workspace/support/agent-execution-config.js +1 -44
- package/dist/workspace/support/discovery.js +1 -147
- package/dist/workspace/support/source-collectors.js +1 -30
- package/dist/workspace/support/source-protocols.js +2 -192
- package/dist/workspace/support/workspace-ref-utils.js +1 -362
- package/dist/workspace/tool-hydration.js +1 -280
- package/dist/workspace/validate.js +1 -99
- package/dist/workspace/yaml-object-reader.js +1 -285
- package/package.json +7 -3
|
@@ -1,473 +1,2 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
import { buildStateSnapshot } from "./model/message-assembly.js";
|
|
4
|
-
import { asRecord } from "./tool/resolved-tool.js";
|
|
5
|
-
import { renderToolFailure } from "../support/harness-support.js";
|
|
6
|
-
import { mapTerminalStatusToRequestState, readTerminalExecutionStatus } from "./terminal-status.js";
|
|
7
|
-
function looksLikeLeakedToolCallText(value) {
|
|
8
|
-
const normalized = sanitizeVisibleText(value).trim();
|
|
9
|
-
if (!normalized) {
|
|
10
|
-
return false;
|
|
11
|
-
}
|
|
12
|
-
if (salvageFunctionLikeToolCall(normalized)) {
|
|
13
|
-
return true;
|
|
14
|
-
}
|
|
15
|
-
if (salvageJsonToolCalls(normalized).length > 0) {
|
|
16
|
-
return true;
|
|
17
|
-
}
|
|
18
|
-
const prefixedToolCallMatch = /^(?:\s*(?:Ready|Understood|Okay|Ok|Got it|Sure|All set|What is your request|Please provide a task for me to orchestrate)[.:?!]?\s*)+([A-Za-z_][A-Za-z0-9_]*\([\s\S]*\))\s*$/u.exec(normalized);
|
|
19
|
-
return !!(prefixedToolCallMatch && salvageFunctionLikeToolCall(prefixedToolCallMatch[1]));
|
|
20
|
-
}
|
|
21
|
-
function hasIncompleteStateSnapshotPlan(stateSnapshot) {
|
|
22
|
-
if (typeof stateSnapshot !== "object" || stateSnapshot === null) {
|
|
23
|
-
return false;
|
|
24
|
-
}
|
|
25
|
-
const todos = stateSnapshot.todos;
|
|
26
|
-
if (!Array.isArray(todos)) {
|
|
27
|
-
return false;
|
|
28
|
-
}
|
|
29
|
-
return todos.some((todo) => {
|
|
30
|
-
if (typeof todo !== "object" || todo === null) {
|
|
31
|
-
return false;
|
|
32
|
-
}
|
|
33
|
-
const status = typeof todo.status === "string"
|
|
34
|
-
? todo.status.trim().toLowerCase()
|
|
35
|
-
: "";
|
|
36
|
-
return status === "pending" || status === "in_progress";
|
|
37
|
-
});
|
|
38
|
-
}
|
|
39
|
-
function hasStateSnapshotPlan(stateSnapshot) {
|
|
40
|
-
return typeof stateSnapshot === "object"
|
|
41
|
-
&& stateSnapshot !== null
|
|
42
|
-
&& Array.isArray(stateSnapshot.todos)
|
|
43
|
-
&& (stateSnapshot.todos).length > 0;
|
|
44
|
-
}
|
|
45
|
-
function normalizePlanToolName(toolName) {
|
|
46
|
-
return typeof toolName === "string" ? toolName.trim().toLowerCase().replace(/[\s-]+/gu, "_") : "";
|
|
47
|
-
}
|
|
48
|
-
function isPlanToolName(toolName) {
|
|
49
|
-
const normalized = normalizePlanToolName(toolName);
|
|
50
|
-
return normalized === "write_todos"
|
|
51
|
-
|| normalized === "read_todos"
|
|
52
|
-
|| normalized === "tool_call_write_todos"
|
|
53
|
-
|| normalized === "tool_call_read_todos"
|
|
54
|
-
|| normalized === "call_write_todos"
|
|
55
|
-
|| normalized === "call_read_todos";
|
|
56
|
-
}
|
|
57
|
-
function hasPlanToolEvidence(executedToolResults) {
|
|
58
|
-
return executedToolResults.some((item) => {
|
|
59
|
-
if (!isPlanToolName(item.toolName)) {
|
|
60
|
-
return false;
|
|
61
|
-
}
|
|
62
|
-
const output = typeof item.output === "object" && item.output !== null ? item.output : null;
|
|
63
|
-
const summaryContainer = typeof output?.summary === "object" && output.summary !== null
|
|
64
|
-
? output.summary
|
|
65
|
-
: null;
|
|
66
|
-
const counts = typeof summaryContainer?.summary === "object" && summaryContainer.summary !== null
|
|
67
|
-
? summaryContainer.summary
|
|
68
|
-
: null;
|
|
69
|
-
return !(typeof counts?.total === "number" && counts.total <= 0);
|
|
70
|
-
});
|
|
71
|
-
}
|
|
72
|
-
function hasExecutionToolEvidence(executedToolResults) {
|
|
73
|
-
return executedToolResults.some((item) => item.isError !== true && !isPlanToolName(item.toolName));
|
|
74
|
-
}
|
|
75
|
-
function isPlaceholderTaskCompletion(value) {
|
|
76
|
-
const normalized = sanitizeVisibleText(value).trim();
|
|
77
|
-
return normalized === "Task completed";
|
|
78
|
-
}
|
|
79
|
-
function isLowSignalStructuredCompletion(value) {
|
|
80
|
-
const normalized = sanitizeVisibleText(value).trim();
|
|
81
|
-
if (!normalized) {
|
|
82
|
-
return false;
|
|
83
|
-
}
|
|
84
|
-
return /Status:\s*completed[\s\S]*Summary:\s*-\s*none[\s\S]*Likely Causes:\s*-\s*none[\s\S]*Blockers:\s*-\s*none[\s\S]*Next Commands:\s*-\s*none/i.test(normalized);
|
|
85
|
-
}
|
|
86
|
-
function looksLikeToolBlocker(value) {
|
|
87
|
-
const normalized = sanitizeVisibleText(value).trim();
|
|
88
|
-
if (!normalized) {
|
|
89
|
-
return false;
|
|
90
|
-
}
|
|
91
|
-
return /^Blocked:\s*/iu.test(normalized)
|
|
92
|
-
|| /error parsing tool call/iu.test(normalized)
|
|
93
|
-
|| /invalid tool call/iu.test(normalized)
|
|
94
|
-
|| /tool call.*schema/iu.test(normalized);
|
|
95
|
-
}
|
|
96
|
-
function isSubstantiveFinalOutput(value) {
|
|
97
|
-
const normalized = sanitizeVisibleText(value).trim();
|
|
98
|
-
if (normalized.length < 80) {
|
|
99
|
-
return false;
|
|
100
|
-
}
|
|
101
|
-
return !looksLikeLeakedToolCallText(normalized)
|
|
102
|
-
&& !looksLikeToolBlocker(normalized)
|
|
103
|
-
&& !looksLikeClarificationQuestion(normalized)
|
|
104
|
-
&& !looksLikeNonEvidenceApology(normalized)
|
|
105
|
-
&& !isLowSignalStructuredCompletion(normalized);
|
|
106
|
-
}
|
|
107
|
-
function normalizeToolOutputText(output) {
|
|
108
|
-
const directText = typeof output === "string"
|
|
109
|
-
? sanitizeVisibleText(output).trim()
|
|
110
|
-
: "";
|
|
111
|
-
if (directText
|
|
112
|
-
&& !looksLikeLeakedToolCallText(directText)
|
|
113
|
-
&& !isPlaceholderTaskCompletion(directText)
|
|
114
|
-
&& !isLowSignalStructuredCompletion(directText)) {
|
|
115
|
-
return directText;
|
|
116
|
-
}
|
|
117
|
-
const visibleOutput = sanitizeVisibleText(extractVisibleOutput(output)).trim();
|
|
118
|
-
if (visibleOutput
|
|
119
|
-
&& !looksLikeLeakedToolCallText(visibleOutput)
|
|
120
|
-
&& !isPlaceholderTaskCompletion(visibleOutput)
|
|
121
|
-
&& !isLowSignalStructuredCompletion(visibleOutput)) {
|
|
122
|
-
return visibleOutput;
|
|
123
|
-
}
|
|
124
|
-
const fallbackContext = sanitizeVisibleText(extractToolFallbackContext(output)).trim();
|
|
125
|
-
if (fallbackContext
|
|
126
|
-
&& !looksLikeLeakedToolCallText(fallbackContext)
|
|
127
|
-
&& !isPlaceholderTaskCompletion(fallbackContext)
|
|
128
|
-
&& !isLowSignalStructuredCompletion(fallbackContext)) {
|
|
129
|
-
return fallbackContext;
|
|
130
|
-
}
|
|
131
|
-
return "";
|
|
132
|
-
}
|
|
133
|
-
function extractLatestSuccessfulTaskResultText(executedToolResults) {
|
|
134
|
-
for (const toolResult of [...executedToolResults].reverse()) {
|
|
135
|
-
if (toolResult.isError === true || toolResult.toolName !== "task") {
|
|
136
|
-
continue;
|
|
137
|
-
}
|
|
138
|
-
const normalized = normalizeToolOutputText(toolResult.output);
|
|
139
|
-
if (normalized && !looksLikeToolBlocker(normalized)) {
|
|
140
|
-
return normalized;
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
return "";
|
|
144
|
-
}
|
|
145
|
-
function looksLikeReportOutput(value) {
|
|
146
|
-
const normalized = sanitizeVisibleText(value).trim();
|
|
147
|
-
if (!normalized) {
|
|
148
|
-
return false;
|
|
149
|
-
}
|
|
150
|
-
return /(?:^|\n)\s*Status\s*:/iu.test(normalized)
|
|
151
|
-
|| /(?:^|\n)\s*Summary\s*:/iu.test(normalized)
|
|
152
|
-
|| /(?:^|\n)\s*Likely Causes\s*:/iu.test(normalized)
|
|
153
|
-
|| /(?:^|\n)\s*Blockers\s*:/iu.test(normalized)
|
|
154
|
-
|| /(?:^|\n)\s*Next Commands\s*:/iu.test(normalized);
|
|
155
|
-
}
|
|
156
|
-
function looksLikeRawCommandTranscript(value) {
|
|
157
|
-
const normalized = sanitizeVisibleText(value).trim();
|
|
158
|
-
if (!normalized) {
|
|
159
|
-
return false;
|
|
160
|
-
}
|
|
161
|
-
return /^(?:stdout|stderr)\s*:/iu.test(normalized)
|
|
162
|
-
|| /(?:^|\n)\s*(?:stdout|stderr)\s*:/iu.test(normalized)
|
|
163
|
-
|| /(?:^|\n)\s*exitCode\s*:\s*-?\d+\s*$/iu.test(normalized);
|
|
164
|
-
}
|
|
165
|
-
function extractLatestSuccessfulNonTodoToolResultText(executedToolResults) {
|
|
166
|
-
const candidates = [];
|
|
167
|
-
for (const toolResult of executedToolResults) {
|
|
168
|
-
if (toolResult.isError === true) {
|
|
169
|
-
continue;
|
|
170
|
-
}
|
|
171
|
-
if (toolResult.toolName === "task" || isPlanToolName(toolResult.toolName)) {
|
|
172
|
-
continue;
|
|
173
|
-
}
|
|
174
|
-
const normalized = normalizeToolOutputText(toolResult.output);
|
|
175
|
-
if (normalized) {
|
|
176
|
-
candidates.push(normalized);
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
return [...candidates].reverse().find(looksLikeReportOutput)
|
|
180
|
-
?? [...candidates].reverse().find((candidate) => !looksLikeRawCommandTranscript(candidate))
|
|
181
|
-
?? candidates.at(-1)
|
|
182
|
-
?? "";
|
|
183
|
-
}
|
|
184
|
-
function isNonFunctionObject(value) {
|
|
185
|
-
return Object(value) === value && typeof value !== "function";
|
|
186
|
-
}
|
|
187
|
-
function readSerializedMessageType(value) {
|
|
188
|
-
if (!isNonFunctionObject(value)) {
|
|
189
|
-
return "";
|
|
190
|
-
}
|
|
191
|
-
const id = value.id;
|
|
192
|
-
if (!Array.isArray(id)) {
|
|
193
|
-
return "";
|
|
194
|
-
}
|
|
195
|
-
return id.map((item) => typeof item === "string" ? item : "").filter(Boolean).join(".");
|
|
196
|
-
}
|
|
197
|
-
function readToolMessageRecord(value) {
|
|
198
|
-
if (!isNonFunctionObject(value)) {
|
|
199
|
-
return null;
|
|
200
|
-
}
|
|
201
|
-
const typed = value;
|
|
202
|
-
const messageType = readSerializedMessageType(value);
|
|
203
|
-
const kwargs = isNonFunctionObject(typed.kwargs) ? typed.kwargs : undefined;
|
|
204
|
-
const isToolMessage = typed.role === "tool"
|
|
205
|
-
|| typed.type === "tool"
|
|
206
|
-
|| messageType.endsWith("ToolMessage")
|
|
207
|
-
|| kwargs?.name !== undefined && kwargs?.content !== undefined;
|
|
208
|
-
if (!isToolMessage) {
|
|
209
|
-
return null;
|
|
210
|
-
}
|
|
211
|
-
const toolNameCandidate = kwargs?.name ?? typed.name;
|
|
212
|
-
const toolName = typeof toolNameCandidate === "string" ? toolNameCandidate.trim() : "";
|
|
213
|
-
if (!toolName) {
|
|
214
|
-
return null;
|
|
215
|
-
}
|
|
216
|
-
const output = kwargs?.content ?? typed.content ?? "";
|
|
217
|
-
const status = kwargs?.status ?? typed.status;
|
|
218
|
-
const outputText = typeof output === "string" ? output : extractVisibleOutput(output);
|
|
219
|
-
return {
|
|
220
|
-
toolName,
|
|
221
|
-
output,
|
|
222
|
-
...(status === "error" || looksLikeToolBlocker(outputText) ? { isError: true } : {}),
|
|
223
|
-
};
|
|
224
|
-
}
|
|
225
|
-
function extractUpstreamToolResults(value, seen = new Set()) {
|
|
226
|
-
if (typeof value !== "object" || value === null || seen.has(value)) {
|
|
227
|
-
return [];
|
|
228
|
-
}
|
|
229
|
-
seen.add(value);
|
|
230
|
-
if (Array.isArray(value)) {
|
|
231
|
-
return value.flatMap((item) => extractUpstreamToolResults(item, seen));
|
|
232
|
-
}
|
|
233
|
-
const direct = readToolMessageRecord(value);
|
|
234
|
-
if (direct) {
|
|
235
|
-
return [direct];
|
|
236
|
-
}
|
|
237
|
-
const typed = value;
|
|
238
|
-
return [
|
|
239
|
-
...extractUpstreamToolResults(typed.messages, seen),
|
|
240
|
-
...extractUpstreamToolResults(typed.output, seen),
|
|
241
|
-
];
|
|
242
|
-
}
|
|
243
|
-
function hasDelegationBlocker(executedToolResults) {
|
|
244
|
-
return executedToolResults.some((toolResult) => {
|
|
245
|
-
if (toolResult.toolName !== "task") {
|
|
246
|
-
return false;
|
|
247
|
-
}
|
|
248
|
-
if (toolResult.isError === true) {
|
|
249
|
-
return true;
|
|
250
|
-
}
|
|
251
|
-
const normalized = normalizeToolOutputText(toolResult.output);
|
|
252
|
-
return looksLikeToolBlocker(normalized);
|
|
253
|
-
});
|
|
254
|
-
}
|
|
255
|
-
function looksLikeClarificationQuestion(value) {
|
|
256
|
-
const normalized = sanitizeVisibleText(value).trim();
|
|
257
|
-
if (!normalized) {
|
|
258
|
-
return false;
|
|
259
|
-
}
|
|
260
|
-
return /[??]\s*$/u.test(normalized)
|
|
261
|
-
|| /^(?:could you|can you|please provide|please share|i need|what is|which )/iu.test(normalized)
|
|
262
|
-
|| /^(?:请问|请提供|能否提供|能不能提供)/u.test(normalized);
|
|
263
|
-
}
|
|
264
|
-
function looksLikeNonEvidenceApology(value) {
|
|
265
|
-
const normalized = sanitizeVisibleText(value).trim();
|
|
266
|
-
if (!normalized) {
|
|
267
|
-
return false;
|
|
268
|
-
}
|
|
269
|
-
return /(?:i(?:'|’)m sorry|sorry)[\s\S]*(?:couldn(?:'|’)t|cannot|can(?:'|’)t|unable to)\s+(?:complete|proceed|finish|continue)/iu.test(normalized)
|
|
270
|
-
|| /(?:system limitation|technical limitation|internal limitation|recursion limit)/iu.test(normalized)
|
|
271
|
-
|| /(?:抱歉|对不起)[\s\S]*(?:无法|不能|未能)(?:完成|继续|处理)/u.test(normalized);
|
|
272
|
-
}
|
|
273
|
-
function looksLikeContradictedToolExecutionFailure(value) {
|
|
274
|
-
const normalized = sanitizeVisibleText(value).trim();
|
|
275
|
-
if (!normalized) {
|
|
276
|
-
return false;
|
|
277
|
-
}
|
|
278
|
-
const mentionsToolExecution = /(?:\btool\b|\bfunction\b|\bexecute\b|\binvoke\b|\bcall\b|工具|函数|调用|执行)/iu.test(normalized);
|
|
279
|
-
if (!mentionsToolExecution) {
|
|
280
|
-
return false;
|
|
281
|
-
}
|
|
282
|
-
return /(?:cancelled|canceled|timeout|timed out|race condition|cannot execute|can't execute|unable to execute|could not execute|failed to execute|被取消|超时|无法执行|不能执行|未能执行)/iu.test(normalized);
|
|
283
|
-
}
|
|
284
|
-
function extractDeterministicToolFailureReport(executedToolResults) {
|
|
285
|
-
const hasSuccessfulSubstantiveTool = executedToolResults.some((toolResult) => (toolResult.isError !== true
|
|
286
|
-
&& !isPlanToolName(toolResult.toolName)));
|
|
287
|
-
if (hasSuccessfulSubstantiveTool) {
|
|
288
|
-
return "";
|
|
289
|
-
}
|
|
290
|
-
const failures = executedToolResults.filter((toolResult) => toolResult.isError === true);
|
|
291
|
-
if (failures.length === 0) {
|
|
292
|
-
return "";
|
|
293
|
-
}
|
|
294
|
-
const blockerLines = failures
|
|
295
|
-
.slice(-3)
|
|
296
|
-
.map((toolResult) => `- ${renderToolFailure(toolResult.toolName, toolResult.output)}`);
|
|
297
|
-
return [
|
|
298
|
-
"Status: failed",
|
|
299
|
-
"Summary:",
|
|
300
|
-
"- The request could not proceed because required tools failed before any concrete evidence was gathered.",
|
|
301
|
-
"Likely Causes:",
|
|
302
|
-
"- none",
|
|
303
|
-
"Blockers:",
|
|
304
|
-
...blockerLines,
|
|
305
|
-
"Next Commands:",
|
|
306
|
-
"- none",
|
|
307
|
-
].join("\n");
|
|
308
|
-
}
|
|
309
|
-
function hasEmptyFinalMessage(result) {
|
|
310
|
-
const messages = Array.isArray(result.messages) ? result.messages : [];
|
|
311
|
-
const lastMessage = messages.at(-1);
|
|
312
|
-
if (!lastMessage || typeof lastMessage !== "object") {
|
|
313
|
-
return false;
|
|
314
|
-
}
|
|
315
|
-
const direct = lastMessage;
|
|
316
|
-
return direct.content === "" || direct.kwargs?.content === "" || direct.lc_kwargs?.content === "";
|
|
317
|
-
}
|
|
318
|
-
function hasFinalMessageToolCalls(result) {
|
|
319
|
-
const messages = Array.isArray(result.messages) ? result.messages : [];
|
|
320
|
-
const lastMessage = messages.at(-1);
|
|
321
|
-
if (!lastMessage || typeof lastMessage !== "object") {
|
|
322
|
-
return false;
|
|
323
|
-
}
|
|
324
|
-
const direct = lastMessage;
|
|
325
|
-
return Array.isArray(direct.tool_calls) && direct.tool_calls.length > 0
|
|
326
|
-
|| Array.isArray(direct.invalid_tool_calls) && direct.invalid_tool_calls.length > 0
|
|
327
|
-
|| Array.isArray(direct.kwargs?.tool_calls) && direct.kwargs.tool_calls.length > 0
|
|
328
|
-
|| Array.isArray(direct.kwargs?.invalid_tool_calls) && direct.kwargs.invalid_tool_calls.length > 0
|
|
329
|
-
|| Array.isArray(direct.lc_kwargs?.tool_calls) && direct.lc_kwargs.tool_calls.length > 0
|
|
330
|
-
|| Array.isArray(direct.lc_kwargs?.invalid_tool_calls) && direct.lc_kwargs.invalid_tool_calls.length > 0;
|
|
331
|
-
}
|
|
332
|
-
export function resolveDeterministicFinalOutput(params) {
|
|
333
|
-
const visibleOutput = params.visibleOutput ?? "";
|
|
334
|
-
const toolFallback = params.toolFallback ?? "";
|
|
335
|
-
const executedToolResults = params.executedToolResults ?? [];
|
|
336
|
-
const sanitizedVisibleOutput = visibleOutput && !looksLikeLeakedToolCallText(visibleOutput)
|
|
337
|
-
? sanitizeVisibleText(visibleOutput).trim()
|
|
338
|
-
: "";
|
|
339
|
-
const deterministicFailureReport = extractDeterministicToolFailureReport(executedToolResults);
|
|
340
|
-
const delegatedTaskOutput = extractLatestSuccessfulTaskResultText(executedToolResults);
|
|
341
|
-
const successfulToolOutput = extractLatestSuccessfulNonTodoToolResultText(executedToolResults);
|
|
342
|
-
if (sanitizedVisibleOutput && deterministicFailureReport && hasDelegationBlocker(executedToolResults) && !successfulToolOutput) {
|
|
343
|
-
return deterministicFailureReport;
|
|
344
|
-
}
|
|
345
|
-
if (sanitizedVisibleOutput && successfulToolOutput && hasDelegationBlocker(executedToolResults)) {
|
|
346
|
-
return deterministicFailureReport || delegatedTaskOutput || successfulToolOutput;
|
|
347
|
-
}
|
|
348
|
-
if (sanitizedVisibleOutput
|
|
349
|
-
&& (looksLikeClarificationQuestion(sanitizedVisibleOutput) || looksLikeNonEvidenceApology(sanitizedVisibleOutput))) {
|
|
350
|
-
return deterministicFailureReport || delegatedTaskOutput || successfulToolOutput || sanitizedVisibleOutput;
|
|
351
|
-
}
|
|
352
|
-
if (sanitizedVisibleOutput && successfulToolOutput && looksLikeContradictedToolExecutionFailure(sanitizedVisibleOutput)) {
|
|
353
|
-
return delegatedTaskOutput || successfulToolOutput;
|
|
354
|
-
}
|
|
355
|
-
if (sanitizedVisibleOutput && !isLowSignalStructuredCompletion(sanitizedVisibleOutput)) {
|
|
356
|
-
return sanitizedVisibleOutput;
|
|
357
|
-
}
|
|
358
|
-
if (delegatedTaskOutput) {
|
|
359
|
-
return delegatedTaskOutput;
|
|
360
|
-
}
|
|
361
|
-
if (successfulToolOutput) {
|
|
362
|
-
return successfulToolOutput;
|
|
363
|
-
}
|
|
364
|
-
const sanitizedToolFallback = toolFallback && !looksLikeLeakedToolCallText(toolFallback)
|
|
365
|
-
? sanitizeVisibleText(toolFallback).trim()
|
|
366
|
-
: "";
|
|
367
|
-
if (deterministicFailureReport) {
|
|
368
|
-
return deterministicFailureReport;
|
|
369
|
-
}
|
|
370
|
-
return isLowSignalStructuredCompletion(sanitizedToolFallback) ? "" : sanitizedToolFallback;
|
|
371
|
-
}
|
|
372
|
-
export function extractDelegatedFindingsText(executedToolResults) {
|
|
373
|
-
return extractLatestSuccessfulTaskResultText(executedToolResults);
|
|
374
|
-
}
|
|
375
|
-
export function extractToolResultFindingsText(executedToolResults) {
|
|
376
|
-
return extractLatestSuccessfulNonTodoToolResultText(executedToolResults);
|
|
377
|
-
}
|
|
378
|
-
export function finalizeRequestResult(params) {
|
|
379
|
-
const { bindingAgentId, sessionId, requestId, binding, result, executedToolResults } = params;
|
|
380
|
-
const allExecutedToolResults = [
|
|
381
|
-
...executedToolResults,
|
|
382
|
-
...extractUpstreamToolResults(result),
|
|
383
|
-
];
|
|
384
|
-
const interruptContent = Array.isArray(result.__interrupt__) && result.__interrupt__.length > 0 ? JSON.stringify(result.__interrupt__) : undefined;
|
|
385
|
-
const extractedOutput = extractVisibleOutput(result);
|
|
386
|
-
const visibleOutput = extractedOutput && !isLikelyToolArgsObject(tryParseJson(extractedOutput)) ? extractedOutput : "";
|
|
387
|
-
const emptyAssistantMessageFailure = extractEmptyAssistantMessageFailure(result);
|
|
388
|
-
const toolFallback = extractToolFallbackContext(result);
|
|
389
|
-
const outputContent = extractOutputContent(result);
|
|
390
|
-
const contentBlocks = extractContentBlocks(result);
|
|
391
|
-
const structuredResponse = result.structuredResponse;
|
|
392
|
-
const structuredTerminalStatus = readTerminalExecutionStatus(structuredResponse) ?? readTerminalExecutionStatus(result);
|
|
393
|
-
const files = asRecord(result.files);
|
|
394
|
-
if (!visibleOutput && !toolFallback && emptyAssistantMessageFailure) {
|
|
395
|
-
throw new Error(emptyAssistantMessageFailure);
|
|
396
|
-
}
|
|
397
|
-
if (!visibleOutput
|
|
398
|
-
&& !toolFallback
|
|
399
|
-
&& interruptContent === undefined
|
|
400
|
-
&& outputContent === undefined
|
|
401
|
-
&& contentBlocks.length === 0
|
|
402
|
-
&& structuredResponse === undefined
|
|
403
|
-
&& !files
|
|
404
|
-
&& allExecutedToolResults.length === 0
|
|
405
|
-
&& hasEmptyFinalMessage(result)
|
|
406
|
-
&& !hasFinalMessageToolCalls(result)) {
|
|
407
|
-
throw new Error("empty_final_output");
|
|
408
|
-
}
|
|
409
|
-
const stateSnapshot = buildStateSnapshot(result);
|
|
410
|
-
const hasIncompleteRequiredPlan = binding?.harnessRuntime?.executionContract?.requiresPlan === true
|
|
411
|
-
&& hasIncompleteStateSnapshotPlan(stateSnapshot);
|
|
412
|
-
const hasMissingRequiredPlanEvidence = binding?.harnessRuntime?.executionContract?.requiresPlan === true
|
|
413
|
-
&& !hasStateSnapshotPlan(stateSnapshot)
|
|
414
|
-
&& !hasPlanToolEvidence(allExecutedToolResults)
|
|
415
|
-
&& !hasExecutionToolEvidence(allExecutedToolResults);
|
|
416
|
-
const serializedResult = JSON.stringify(result, null, 2);
|
|
417
|
-
let output = resolveDeterministicFinalOutput({
|
|
418
|
-
visibleOutput,
|
|
419
|
-
toolFallback,
|
|
420
|
-
executedToolResults: allExecutedToolResults,
|
|
421
|
-
})
|
|
422
|
-
|| (containsLikelySkillDocument(result) ? "" : serializedResult);
|
|
423
|
-
const preliminaryTerminalStatus = readTerminalExecutionStatus(output);
|
|
424
|
-
const hasMissingRequiredFinalAnswer = binding?.harnessRuntime?.executionContract?.requiresPlan === true
|
|
425
|
-
&& !visibleOutput
|
|
426
|
-
&& !preliminaryTerminalStatus
|
|
427
|
-
&& !output.trim()
|
|
428
|
-
&& allExecutedToolResults.some((toolResult) => toolResult.isError !== true && !isPlanToolName(toolResult.toolName));
|
|
429
|
-
if (hasMissingRequiredPlanEvidence) {
|
|
430
|
-
output = "runtime_error=Agent ended before producing required plan evidence.";
|
|
431
|
-
}
|
|
432
|
-
else if (hasIncompleteRequiredPlan && !visibleOutput) {
|
|
433
|
-
output = "runtime_error=Agent ended while required plan still had unfinished work.";
|
|
434
|
-
}
|
|
435
|
-
else if (hasMissingRequiredFinalAnswer) {
|
|
436
|
-
output = "runtime_error=Agent ended after tool execution without producing a final answer.";
|
|
437
|
-
}
|
|
438
|
-
const finalMessageText = sanitizeVisibleText(output);
|
|
439
|
-
const terminalStatus = structuredTerminalStatus ?? readTerminalExecutionStatus(finalMessageText);
|
|
440
|
-
const hasSubstantiveFinalOutput = Boolean(visibleOutput) && isSubstantiveFinalOutput(finalMessageText);
|
|
441
|
-
const hasTerminalToolBlocker = looksLikeToolBlocker(finalMessageText);
|
|
442
|
-
const memoryCandidates = allExecutedToolResults.flatMap((toolResult) => toolResult.memoryCandidates ?? []);
|
|
443
|
-
return {
|
|
444
|
-
sessionId,
|
|
445
|
-
requestId,
|
|
446
|
-
agentId: bindingAgentId,
|
|
447
|
-
state: Array.isArray(result.__interrupt__) && result.__interrupt__.length > 0
|
|
448
|
-
? "waiting_for_approval"
|
|
449
|
-
: hasMissingRequiredPlanEvidence || (hasIncompleteRequiredPlan && !hasSubstantiveFinalOutput) || hasMissingRequiredFinalAnswer
|
|
450
|
-
? "failed"
|
|
451
|
-
: hasTerminalToolBlocker
|
|
452
|
-
? "failed"
|
|
453
|
-
: mapTerminalStatusToRequestState(terminalStatus),
|
|
454
|
-
interruptContent,
|
|
455
|
-
output: finalMessageText,
|
|
456
|
-
finalMessageText,
|
|
457
|
-
...(outputContent !== undefined ? { outputContent } : {}),
|
|
458
|
-
...(contentBlocks.length > 0 ? { contentBlocks } : {}),
|
|
459
|
-
...(structuredResponse !== undefined ? { structuredResponse } : {}),
|
|
460
|
-
metadata: {
|
|
461
|
-
...(allExecutedToolResults.length > 0 ? { executedToolResults: allExecutedToolResults } : {}),
|
|
462
|
-
...(memoryCandidates.length > 0 ? { memoryCandidates } : {}),
|
|
463
|
-
...(structuredResponse !== undefined ? { structuredResponse } : {}),
|
|
464
|
-
...(terminalStatus ? { terminalStatus } : {}),
|
|
465
|
-
...(outputContent !== undefined ? { outputContent } : {}),
|
|
466
|
-
...(contentBlocks.length > 0 ? { contentBlocks } : {}),
|
|
467
|
-
...(files ? { files } : {}),
|
|
468
|
-
...(stateSnapshot ? { stateSnapshot } : {}),
|
|
469
|
-
upstreamResult: result,
|
|
470
|
-
},
|
|
471
|
-
};
|
|
472
|
-
}
|
|
473
|
-
export const finalizeInvocationResult = finalizeRequestResult;
|
|
1
|
+
import{containsLikelySkillDocument as Q,extractContentBlocks as H,extractEmptyAssistantMessageFailure as K,extractOutputContent as X,extractToolFallbackContext as q,extractVisibleOutput as z,isLikelyToolArgsObject as Y,sanitizeVisibleText as a,tryParseJson as tt}from"../parsing/output-parsing.js";import{salvageFunctionLikeToolCall as P,salvageJsonToolCalls as et}from"../parsing/output-tool-args.js";import{buildStateSnapshot as nt}from"./model/message-assembly.js";import{asRecord as ot}from"./tool/resolved-tool.js";import{renderToolFailure as rt}from"../support/harness-support.js";import{mapTerminalStatusToRequestState as st,readTerminalExecutionStatus as A}from"./terminal-status.js";function m(e){const t=a(e).trim();if(!t)return!1;if(P(t)||et(t).length>0)return!0;const n=/^(?:\s*(?:Ready|Understood|Okay|Ok|Got it|Sure|All set|What is your request|Please provide a task for me to orchestrate)[.:?!]?\s*)+([A-Za-z_][A-Za-z0-9_]*\([\s\S]*\))\s*$/u.exec(t);return!!(n&&P(n[1]))}function it(e){if(typeof e!="object"||e===null)return!1;const t=e.todos;return Array.isArray(t)?t.some(n=>{if(typeof n!="object"||n===null)return!1;const o=typeof n.status=="string"?n.status.trim().toLowerCase():"";return o==="pending"||o==="in_progress"}):!1}function at(e){return typeof e=="object"&&e!==null&&Array.isArray(e.todos)&&e.todos.length>0}function lt(e){return typeof e=="string"?e.trim().toLowerCase().replace(/[\s-]+/gu,"_"):""}function T(e){const t=lt(e);return t==="write_todos"||t==="read_todos"||t==="tool_call_write_todos"||t==="tool_call_read_todos"||t==="call_write_todos"||t==="call_read_todos"}function ut(e){return e.some(t=>{if(!T(t.toolName))return!1;const n=typeof t.output=="object"&&t.output!==null?t.output:null,o=typeof n?.summary=="object"&&n.summary!==null?n.summary:null,s=typeof o?.summary=="object"&&o.summary!==null?o.summary:null;return!(typeof s?.total=="number"&&s.total<=0)})}function ct(e){return e.some(t=>t.isError!==!0&&!T(t.toolName))}function E(e){return a(e).trim()==="Task completed"}function p(e){const t=a(e).trim();return t?/Status:\s*completed[\s\S]*Summary:\s*-\s*none[\s\S]*Likely Causes:\s*-\s*none[\s\S]*Blockers:\s*-\s*none[\s\S]*Next Commands:\s*-\s*none/i.test(t):!1}function h(e){const t=a(e).trim();return t?/^Blocked:\s*/iu.test(t)||/error parsing tool call/iu.test(t)||/invalid tool call/iu.test(t)||/tool call.*schema/iu.test(t):!1}function ft(e){const t=a(e).trim();return t.length<80?!1:!m(t)&&!h(t)&&!J(t)&&!$(t)&&!p(t)}function O(e){const t=typeof e=="string"?a(e).trim():"";if(t&&!m(t)&&!E(t)&&!p(t))return t;const n=a(z(e)).trim();if(n&&!m(n)&&!E(n)&&!p(n))return n;const o=a(q(e)).trim();return o&&!m(o)&&!E(o)&&!p(o)?o:""}function B(e){for(const t of[...e].reverse()){if(t.isError===!0||t.toolName!=="task")continue;const n=O(t.output);if(n&&!h(n))return n}return""}function dt(e){const t=a(e).trim();return t?/(?:^|\n)\s*Status\s*:/iu.test(t)||/(?:^|\n)\s*Summary\s*:/iu.test(t)||/(?:^|\n)\s*Likely Causes\s*:/iu.test(t)||/(?:^|\n)\s*Blockers\s*:/iu.test(t)||/(?:^|\n)\s*Next Commands\s*:/iu.test(t):!1}function mt(e){const t=a(e).trim();return t?/^(?:stdout|stderr)\s*:/iu.test(t)||/(?:^|\n)\s*(?:stdout|stderr)\s*:/iu.test(t)||/(?:^|\n)\s*exitCode\s*:\s*-?\d+\s*$/iu.test(t):!1}function I(e){const t=[];for(const n of e){if(n.isError===!0||n.toolName==="task"||T(n.toolName))continue;const o=O(n.output);o&&t.push(o)}return[...t].reverse().find(dt)??[...t].reverse().find(n=>!mt(n))??t.at(-1)??""}function F(e){return Object(e)===e&&typeof e!="function"}function pt(e){if(!F(e))return"";const t=e.id;return Array.isArray(t)?t.map(n=>typeof n=="string"?n:"").filter(Boolean).join("."):""}function gt(e){if(!F(e))return null;const t=e,n=pt(e),o=F(t.kwargs)?t.kwargs:void 0;if(!(t.role==="tool"||t.type==="tool"||n.endsWith("ToolMessage")||o?.name!==void 0&&o?.content!==void 0))return null;const r=o?.name??t.name,l=typeof r=="string"?r.trim():"";if(!l)return null;const i=o?.content??t.content??"",u=o?.status??t.status,g=typeof i=="string"?i:z(i);return{toolName:l,output:i,...u==="error"||h(g)?{isError:!0}:{}}}function S(e,t=new Set){if(typeof e!="object"||e===null||t.has(e))return[];if(t.add(e),Array.isArray(e))return e.flatMap(s=>S(s,t));const n=gt(e);if(n)return[n];const o=e;return[...S(o.messages,t),...S(o.output,t)]}function D(e){return e.some(t=>{if(t.toolName!=="task")return!1;if(t.isError===!0)return!0;const n=O(t.output);return h(n)})}function J(e){const t=a(e).trim();return t?/[??]\s*$/u.test(t)||/^(?:could you|can you|please provide|please share|i need|what is|which )/iu.test(t)||/^(?:请问|请提供|能否提供|能不能提供)/u.test(t):!1}function $(e){const t=a(e).trim();return t?/(?:i(?:'|’)m sorry|sorry)[\s\S]*(?:couldn(?:'|’)t|cannot|can(?:'|’)t|unable to)\s+(?:complete|proceed|finish|continue)/iu.test(t)||/(?:system limitation|technical limitation|internal limitation|recursion limit)/iu.test(t)||/(?:抱歉|对不起)[\s\S]*(?:无法|不能|未能)(?:完成|继续|处理)/u.test(t):!1}function yt(e){const t=a(e).trim();return!t||!/(?:\btool\b|\bfunction\b|\bexecute\b|\binvoke\b|\bcall\b|工具|函数|调用|执行)/iu.test(t)?!1:/(?:cancelled|canceled|timeout|timed out|race condition|cannot execute|can't execute|unable to execute|could not execute|failed to execute|被取消|超时|无法执行|不能执行|未能执行)/iu.test(t)}function _t(e){if(e.some(s=>s.isError!==!0&&!T(s.toolName)))return"";const n=e.filter(s=>s.isError===!0);return n.length===0?"":["Status: failed","Summary:","- The request could not proceed because required tools failed before any concrete evidence was gathered.","Likely Causes:","- none","Blockers:",...n.slice(-3).map(s=>`- ${rt(s.toolName,s.output)}`),"Next Commands:","- none"].join(`
|
|
2
|
+
`)}function kt(e){const n=(Array.isArray(e.messages)?e.messages:[]).at(-1);if(!n||typeof n!="object")return!1;const o=n;return o.content===""||o.kwargs?.content===""||o.lc_kwargs?.content===""}function Tt(e){const n=(Array.isArray(e.messages)?e.messages:[]).at(-1);if(!n||typeof n!="object")return!1;const o=n;return Array.isArray(o.tool_calls)&&o.tool_calls.length>0||Array.isArray(o.invalid_tool_calls)&&o.invalid_tool_calls.length>0||Array.isArray(o.kwargs?.tool_calls)&&o.kwargs.tool_calls.length>0||Array.isArray(o.kwargs?.invalid_tool_calls)&&o.kwargs.invalid_tool_calls.length>0||Array.isArray(o.lc_kwargs?.tool_calls)&&o.lc_kwargs.tool_calls.length>0||Array.isArray(o.lc_kwargs?.invalid_tool_calls)&&o.lc_kwargs.invalid_tool_calls.length>0}function ht(e){const t=e.visibleOutput??"",n=e.toolFallback??"",o=e.executedToolResults??[],s=t&&!m(t)?a(t).trim():"",r=_t(o),l=B(o),i=I(o);if(s&&r&&D(o)&&!i)return r;if(s&&i&&D(o))return r||l||i;if(s&&(J(s)||$(s)))return r||l||i||s;if(s&&i&&yt(s))return l||i;if(s&&!p(s))return s;if(l)return l;if(i)return i;const u=n&&!m(n)?a(n).trim():"";return r||(p(u)?"":u)}function zt(e){return B(e)}function Et(e){return I(e)}function xt(e){const{bindingAgentId:t,sessionId:n,requestId:o,binding:s,result:r,executedToolResults:l}=e,i=[...l,...S(r)],u=Array.isArray(r.__interrupt__)&&r.__interrupt__.length>0?JSON.stringify(r.__interrupt__):void 0,g=z(r),c=g&&!Y(tt(g))?g:"",L=K(r),w=q(r),y=X(r),_=H(r),f=r.structuredResponse,V=A(f)??A(r),C=ot(r.files);if(!c&&!w&&L)throw new Error(L);if(!c&&!w&&u===void 0&&y===void 0&&_.length===0&&f===void 0&&!C&&i.length===0&&kt(r)&&!Tt(r))throw new Error("empty_final_output");const x=nt(r),v=s?.harnessRuntime?.executionContract?.requiresPlan===!0&&it(x),N=s?.harnessRuntime?.executionContract?.requiresPlan===!0&&!at(x)&&!ut(i)&&!ct(i),U=JSON.stringify(r,null,2);let d=ht({visibleOutput:c,toolFallback:w,executedToolResults:i})||(Q(r)?"":U);const W=A(d),M=s?.harnessRuntime?.executionContract?.requiresPlan===!0&&!c&&!W&&!d.trim()&&i.some(b=>b.isError!==!0&&!T(b.toolName));N?d="runtime_error=Agent ended before producing required plan evidence.":v&&!c?d="runtime_error=Agent ended while required plan still had unfinished work.":M&&(d="runtime_error=Agent ended after tool execution without producing a final answer.");const k=a(d),R=V??A(k),Z=!!c&&ft(k),G=h(k),j=i.flatMap(b=>b.memoryCandidates??[]);return{sessionId:n,requestId:o,agentId:t,state:Array.isArray(r.__interrupt__)&&r.__interrupt__.length>0?"waiting_for_approval":N||v&&!Z||M||G?"failed":st(R),interruptContent:u,output:k,finalMessageText:k,...y!==void 0?{outputContent:y}:{},..._.length>0?{contentBlocks:_}:{},...f!==void 0?{structuredResponse:f}:{},metadata:{...i.length>0?{executedToolResults:i}:{},...j.length>0?{memoryCandidates:j}:{},...f!==void 0?{structuredResponse:f}:{},...R?{terminalStatus:R}:{},...y!==void 0?{outputContent:y}:{},..._.length>0?{contentBlocks:_}:{},...C?{files:C}:{},...x?{stateSnapshot:x}:{},upstreamResult:r}}}const Ot=xt;export{zt as extractDelegatedFindingsText,Et as extractToolResultFindingsText,Ot as finalizeInvocationResult,xt as finalizeRequestResult,ht as resolveDeterministicFinalOutput};
|