@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,83 +1 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { isRecord } from "../../../utils/object.js";
|
|
3
|
-
const REASONING_METADATA_KEYS = new Set([
|
|
4
|
-
"reasoning",
|
|
5
|
-
"reasoning_content",
|
|
6
|
-
"thinking",
|
|
7
|
-
"redacted_thinking",
|
|
8
|
-
]);
|
|
9
|
-
const REASONING_CONTENT_BLOCK_TYPES = new Set([
|
|
10
|
-
"reasoning",
|
|
11
|
-
"thinking",
|
|
12
|
-
"redacted_thinking",
|
|
13
|
-
]);
|
|
14
|
-
function pruneReasoningValue(value) {
|
|
15
|
-
if (Array.isArray(value)) {
|
|
16
|
-
let changed = false;
|
|
17
|
-
const next = [];
|
|
18
|
-
for (const item of value) {
|
|
19
|
-
if (isRecord(item) && typeof item.type === "string" && REASONING_CONTENT_BLOCK_TYPES.has(item.type)) {
|
|
20
|
-
changed = true;
|
|
21
|
-
continue;
|
|
22
|
-
}
|
|
23
|
-
const pruned = pruneReasoningValue(item);
|
|
24
|
-
changed = changed || pruned.changed;
|
|
25
|
-
next.push(pruned.value);
|
|
26
|
-
}
|
|
27
|
-
return changed ? { value: next, changed } : { value, changed: false };
|
|
28
|
-
}
|
|
29
|
-
if (!isRecord(value)) {
|
|
30
|
-
return { value, changed: false };
|
|
31
|
-
}
|
|
32
|
-
let changed = false;
|
|
33
|
-
const next = {};
|
|
34
|
-
for (const [key, entry] of Object.entries(value)) {
|
|
35
|
-
if (REASONING_METADATA_KEYS.has(key)) {
|
|
36
|
-
changed = true;
|
|
37
|
-
continue;
|
|
38
|
-
}
|
|
39
|
-
const pruned = pruneReasoningValue(entry);
|
|
40
|
-
changed = changed || pruned.changed;
|
|
41
|
-
next[key] = pruned.value;
|
|
42
|
-
}
|
|
43
|
-
return changed ? { value: next, changed } : { value, changed: false };
|
|
44
|
-
}
|
|
45
|
-
function pruneMessageReasoningMetadata(message) {
|
|
46
|
-
if (!isRecord(message)) {
|
|
47
|
-
return { message, changed: false };
|
|
48
|
-
}
|
|
49
|
-
let changed = false;
|
|
50
|
-
const next = Object.assign(Object.create(Object.getPrototypeOf(message)), message);
|
|
51
|
-
for (const key of ["additional_kwargs", "response_metadata", "content", "lc_kwargs", "kwargs"]) {
|
|
52
|
-
const pruned = pruneReasoningValue(message[key]);
|
|
53
|
-
if (pruned.changed) {
|
|
54
|
-
next[key] = pruned.value;
|
|
55
|
-
changed = true;
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
return changed ? { message: next, changed } : { message, changed: false };
|
|
59
|
-
}
|
|
60
|
-
export function pruneReasoningMetadataFromMessages(messages) {
|
|
61
|
-
if (!Array.isArray(messages)) {
|
|
62
|
-
return { messages, changed: false };
|
|
63
|
-
}
|
|
64
|
-
let changed = false;
|
|
65
|
-
const next = messages.map((message) => {
|
|
66
|
-
const pruned = pruneMessageReasoningMetadata(message);
|
|
67
|
-
changed = changed || pruned.changed;
|
|
68
|
-
return pruned.message;
|
|
69
|
-
});
|
|
70
|
-
return changed ? { messages: next, changed } : { messages, changed: false };
|
|
71
|
-
}
|
|
72
|
-
export function createContextHygieneMiddleware() {
|
|
73
|
-
return createMiddleware({
|
|
74
|
-
name: "HarnessContextHygieneMiddleware",
|
|
75
|
-
beforeModel: (state) => {
|
|
76
|
-
if (!isRecord(state)) {
|
|
77
|
-
return undefined;
|
|
78
|
-
}
|
|
79
|
-
const pruned = pruneReasoningMetadataFromMessages(state.messages);
|
|
80
|
-
return pruned.changed ? { messages: pruned.messages } : undefined;
|
|
81
|
-
},
|
|
82
|
-
});
|
|
83
|
-
}
|
|
1
|
+
import{createMiddleware as d}from"langchain";import{isRecord as c}from"../../../utils/object.js";const f=new Set(["reasoning","reasoning_content","thinking","redacted_thinking"]),g=new Set(["reasoning","thinking","redacted_thinking"]);function s(e){if(Array.isArray(e)){let t=!1;const r=[];for(const o of e){if(c(o)&&typeof o.type=="string"&&g.has(o.type)){t=!0;continue}const i=s(o);t=t||i.changed,r.push(i.value)}return t?{value:r,changed:t}:{value:e,changed:!1}}if(!c(e))return{value:e,changed:!1};let n=!1;const a={};for(const[t,r]of Object.entries(e)){if(f.has(t)){n=!0;continue}const o=s(r);n=n||o.changed,a[t]=o.value}return n?{value:a,changed:n}:{value:e,changed:!1}}function u(e){if(!c(e))return{message:e,changed:!1};let n=!1;const a=Object.assign(Object.create(Object.getPrototypeOf(e)),e);for(const t of["additional_kwargs","response_metadata","content","lc_kwargs","kwargs"]){const r=s(e[t]);r.changed&&(a[t]=r.value,n=!0)}return n?{message:a,changed:n}:{message:e,changed:!1}}function h(e){if(!Array.isArray(e))return{messages:e,changed:!1};let n=!1;const a=e.map(t=>{const r=u(t);return n=n||r.changed,r.message});return n?{messages:a,changed:n}:{messages:e,changed:!1}}function y(){return d({name:"HarnessContextHygieneMiddleware",beforeModel:e=>{if(!c(e))return;const n=h(e.messages);return n.changed?{messages:n.messages}:void 0}})}export{y as createContextHygieneMiddleware,h as pruneReasoningMetadataFromMessages};
|
|
@@ -1,477 +1,5 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
import { extractToolFallbackContext, extractVisibleOutput } from "../parsing/output-parsing.js";
|
|
7
|
-
import { isRecord } from "../../utils/object.js";
|
|
8
|
-
import { resolveDeclaredMiddleware } from "./tool/declared-middleware.js";
|
|
9
|
-
import { UPSTREAM_SESSION_CONFIG_KEY } from "./upstream-configurable-keys.js";
|
|
10
|
-
import { bindingHasLangChainSubagentSupport, bindingHasMiddlewareKind, getBindingBuiltinToolsConfig, getBindingExecutionKind, getBindingGeneralPurposeAgent, getBindingDeepAgentSubagents, getBindingInterruptCompatibilityRules, getBindingMiddlewareConfigs, getBindingMemorySources, getBindingPrimaryModel, getBindingPrimaryTools, getBindingSkills, getBindingSubagents, getBindingTaskDescription, isDeepAgentBinding, isLangChainBinding, } from "../support/compiled-binding.js";
|
|
11
|
-
import { materializeDeepAgentSkillSourcePaths } from "./compat/deepagent-compat.js";
|
|
12
|
-
import { DEFAULT_SUBAGENT_PROMPT } from "../prompts/runtime-prompts.js";
|
|
13
|
-
import { createContextHygieneMiddleware } from "./middleware/context-hygiene.js";
|
|
14
|
-
const INVALID_TOOL_MESSAGE_BLOCK_TYPES = new Set(["tool_use", "thinking", "redacted_thinking"]);
|
|
15
|
-
const DEFAULT_BUILTIN_TASK_TIMEOUT_MS = 180_000;
|
|
16
|
-
function resolveBuiltinTaskTimeoutMs(model) {
|
|
17
|
-
const timeout = model?.init?.timeout;
|
|
18
|
-
return typeof timeout === "number" && Number.isFinite(timeout) && timeout > 0
|
|
19
|
-
? Math.floor(timeout)
|
|
20
|
-
: DEFAULT_BUILTIN_TASK_TIMEOUT_MS;
|
|
21
|
-
}
|
|
22
|
-
async function withBuiltinTaskTimeout(producer, timeoutMs, subagentName) {
|
|
23
|
-
let timeoutHandle;
|
|
24
|
-
try {
|
|
25
|
-
return await Promise.race([
|
|
26
|
-
producer(),
|
|
27
|
-
new Promise((_, reject) => {
|
|
28
|
-
timeoutHandle = setTimeout(() => {
|
|
29
|
-
reject(new Error(`Delegated agent ${subagentName} timed out after ${timeoutMs}ms.`));
|
|
30
|
-
}, timeoutMs);
|
|
31
|
-
timeoutHandle.unref?.();
|
|
32
|
-
}),
|
|
33
|
-
]);
|
|
34
|
-
}
|
|
35
|
-
finally {
|
|
36
|
-
if (timeoutHandle) {
|
|
37
|
-
clearTimeout(timeoutHandle);
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
function extractDeepAgentTaskContent(result) {
|
|
42
|
-
if (typeof result !== "object" || result === null) {
|
|
43
|
-
return undefined;
|
|
44
|
-
}
|
|
45
|
-
const messages = result.messages;
|
|
46
|
-
if (!Array.isArray(messages) || messages.length === 0) {
|
|
47
|
-
return undefined;
|
|
48
|
-
}
|
|
49
|
-
const lastMessage = messages[messages.length - 1];
|
|
50
|
-
if (typeof lastMessage !== "object" || lastMessage === null) {
|
|
51
|
-
return undefined;
|
|
52
|
-
}
|
|
53
|
-
if ("content" in lastMessage) {
|
|
54
|
-
return lastMessage.content;
|
|
55
|
-
}
|
|
56
|
-
if ("kwargs" in lastMessage) {
|
|
57
|
-
return (lastMessage.kwargs)?.content;
|
|
58
|
-
}
|
|
59
|
-
return undefined;
|
|
60
|
-
}
|
|
61
|
-
export function extractSubagentRequestText(state) {
|
|
62
|
-
if (!isRecord(state)) {
|
|
63
|
-
return "";
|
|
64
|
-
}
|
|
65
|
-
const messages = Array.isArray(state.messages) ? state.messages : [];
|
|
66
|
-
const lastMessage = messages.length > 0 ? messages[messages.length - 1] : undefined;
|
|
67
|
-
if (!isRecord(lastMessage)) {
|
|
68
|
-
return "";
|
|
69
|
-
}
|
|
70
|
-
const content = lastMessage.content;
|
|
71
|
-
if (typeof content === "string") {
|
|
72
|
-
return content;
|
|
73
|
-
}
|
|
74
|
-
if (Array.isArray(content)) {
|
|
75
|
-
return content
|
|
76
|
-
.map((block) => {
|
|
77
|
-
if (typeof block === "string") {
|
|
78
|
-
return block;
|
|
79
|
-
}
|
|
80
|
-
if (isRecord(block) && typeof block.text === "string") {
|
|
81
|
-
return block.text;
|
|
82
|
-
}
|
|
83
|
-
return "";
|
|
84
|
-
})
|
|
85
|
-
.filter((part) => part.length > 0)
|
|
86
|
-
.join("\n");
|
|
87
|
-
}
|
|
88
|
-
return "";
|
|
89
|
-
}
|
|
90
|
-
export function wrapRequestResultAsSubagentResponse(result) {
|
|
91
|
-
return {
|
|
92
|
-
messages: [new AIMessage({ content: result.output || "Task completed" })],
|
|
93
|
-
...(result.structuredResponse !== undefined ? { structuredResponse: result.structuredResponse } : {}),
|
|
94
|
-
};
|
|
95
|
-
}
|
|
96
|
-
function shouldUseControlledBuiltinSubagent(subagent) {
|
|
97
|
-
return subagent.builtinTools?.filesystem === false || subagent.builtinTools?.todos === false;
|
|
98
|
-
}
|
|
99
|
-
function createFallbackDeepAgentBackend() {
|
|
100
|
-
return new StateBackend({});
|
|
101
|
-
}
|
|
102
|
-
async function buildControlledBuiltinSubagent(input) {
|
|
103
|
-
const resolvedModel = input.subagent.model ? await input.resolveModel(input.subagent.model) : undefined;
|
|
104
|
-
const resolvedTools = input.subagent.tools ? input.resolveTools(input.subagent.tools, input.binding) : undefined;
|
|
105
|
-
const resolvedSkillPaths = (await materializeDeepAgentSkillSourcePaths({
|
|
106
|
-
workspaceRoot: input.binding?.harnessRuntime.workspaceRoot,
|
|
107
|
-
runtimeRoot: input.binding?.harnessRuntime.runtimeRoot,
|
|
108
|
-
ownerId: `${input.binding?.agent.id ?? "agent"}-${input.subagent.name}`,
|
|
109
|
-
skillPaths: input.subagent.skills,
|
|
110
|
-
})) ?? [];
|
|
111
|
-
const resolvedCustomMiddleware = await resolveDeclaredMiddleware(input.subagent.middleware, input.createDeclaredMiddlewareResolverOptions(input.binding));
|
|
112
|
-
const interruptOn = compileInterruptOn(input.subagent.tools ?? [], input.subagent.interruptOn);
|
|
113
|
-
const backend = input.resolveBackend?.(input.binding) ?? createFallbackDeepAgentBackend();
|
|
114
|
-
const builtinTools = input.subagent.builtinTools ?? {};
|
|
115
|
-
const middleware = [
|
|
116
|
-
...(builtinTools.todos === false ? [] : [todoListMiddleware()]),
|
|
117
|
-
...(resolvedSkillPaths.length > 0 ? [createSkillsMiddleware({ backend: backend, sources: resolvedSkillPaths })] : []),
|
|
118
|
-
...(builtinTools.filesystem === false ? [] : [createFilesystemMiddleware({ backend: backend })]),
|
|
119
|
-
createSummarizationMiddleware({
|
|
120
|
-
model: resolvedModel,
|
|
121
|
-
backend: backend,
|
|
122
|
-
}),
|
|
123
|
-
createPatchToolCallsMiddleware(),
|
|
124
|
-
...resolvedCustomMiddleware,
|
|
125
|
-
...(interruptOn ? [humanInTheLoopMiddleware({ interruptOn })] : []),
|
|
126
|
-
];
|
|
127
|
-
const runnable = createAgent({
|
|
128
|
-
model: resolvedModel,
|
|
129
|
-
systemPrompt: input.subagent.systemPrompt,
|
|
130
|
-
tools: resolvedTools,
|
|
131
|
-
middleware: middleware,
|
|
132
|
-
name: input.subagent.name,
|
|
133
|
-
...(input.subagent.responseFormat !== undefined ? { responseFormat: input.subagent.responseFormat } : {}),
|
|
134
|
-
});
|
|
135
|
-
return {
|
|
136
|
-
name: input.subagent.name,
|
|
137
|
-
description: input.subagent.description,
|
|
138
|
-
systemPrompt: input.subagent.systemPrompt,
|
|
139
|
-
runnable,
|
|
140
|
-
};
|
|
141
|
-
}
|
|
142
|
-
export function buildBuiltinTaskSubagentMiddleware(input) {
|
|
143
|
-
const { selectedSubagent, builtinBackend, summarizationModel } = input;
|
|
144
|
-
const defaultSubagentMiddleware = [
|
|
145
|
-
createPatchToolCallsMiddleware(),
|
|
146
|
-
createSummarizationMiddleware({
|
|
147
|
-
model: summarizationModel,
|
|
148
|
-
backend: builtinBackend,
|
|
149
|
-
}),
|
|
150
|
-
];
|
|
151
|
-
const subagentSkillsMiddleware = selectedSubagent.skills?.length
|
|
152
|
-
? [createSkillsMiddleware({ backend: builtinBackend, sources: selectedSubagent.skills })]
|
|
153
|
-
: [];
|
|
154
|
-
const subagentMiddleware = selectedSubagent.middleware ?? [];
|
|
155
|
-
const interruptMiddleware = selectedSubagent.interruptOn
|
|
156
|
-
? [humanInTheLoopMiddleware({
|
|
157
|
-
interruptOn: compileInterruptOn((selectedSubagent.tools ?? []), selectedSubagent.interruptOn),
|
|
158
|
-
})]
|
|
159
|
-
: [];
|
|
160
|
-
return [
|
|
161
|
-
...defaultSubagentMiddleware,
|
|
162
|
-
...subagentSkillsMiddleware,
|
|
163
|
-
...subagentMiddleware,
|
|
164
|
-
...interruptMiddleware,
|
|
165
|
-
];
|
|
166
|
-
}
|
|
167
|
-
function buildLangChainContextMiddleware(params) {
|
|
168
|
-
const middleware = [];
|
|
169
|
-
const hasSkills = (params.skills?.length ?? 0) > 0;
|
|
170
|
-
const hasMemory = (params.memory?.length ?? 0) > 0;
|
|
171
|
-
if (!hasSkills && !hasMemory) {
|
|
172
|
-
return middleware;
|
|
173
|
-
}
|
|
174
|
-
const backend = params.resolveFilesystemBackend(params.binding);
|
|
175
|
-
if (hasSkills) {
|
|
176
|
-
middleware.push(createSkillsMiddleware({
|
|
177
|
-
backend,
|
|
178
|
-
sources: params.skills,
|
|
179
|
-
}));
|
|
180
|
-
}
|
|
181
|
-
if (hasMemory) {
|
|
182
|
-
middleware.push(createMemoryMiddleware({
|
|
183
|
-
backend,
|
|
184
|
-
sources: params.memory,
|
|
185
|
-
}));
|
|
186
|
-
}
|
|
187
|
-
return middleware;
|
|
188
|
-
}
|
|
189
|
-
export function planLangChainRuntimeExtensions(binding) {
|
|
190
|
-
const primaryModel = getBindingPrimaryModel(binding);
|
|
191
|
-
if (!isLangChainBinding(binding) || !primaryModel) {
|
|
192
|
-
return undefined;
|
|
193
|
-
}
|
|
194
|
-
const skills = getBindingSkills(binding);
|
|
195
|
-
const memory = getBindingMemorySources(binding);
|
|
196
|
-
return {
|
|
197
|
-
includePatchToolCalls: !bindingHasMiddlewareKind(binding, "patchToolCalls"),
|
|
198
|
-
includeAutomaticSummarization: !bindingHasMiddlewareKind(binding, "summarization"),
|
|
199
|
-
context: {
|
|
200
|
-
skills,
|
|
201
|
-
memory,
|
|
202
|
-
},
|
|
203
|
-
delegation: bindingHasLangChainSubagentSupport(binding)
|
|
204
|
-
? {
|
|
205
|
-
subagents: getBindingSubagents(binding),
|
|
206
|
-
generalPurposeAgent: getBindingGeneralPurposeAgent(binding),
|
|
207
|
-
taskDescription: getBindingTaskDescription(binding),
|
|
208
|
-
interruptOn: getBindingInterruptCompatibilityRules(binding),
|
|
209
|
-
}
|
|
210
|
-
: undefined,
|
|
211
|
-
};
|
|
212
|
-
}
|
|
213
|
-
export function resolveBuiltinMiddlewareBackend(input) {
|
|
214
|
-
const runtimeState = {
|
|
215
|
-
...(input.options?.state ?? {}),
|
|
216
|
-
...(isRecord(input.options?.files) ? { files: input.options?.files } : {}),
|
|
217
|
-
};
|
|
218
|
-
const runtimeLike = {
|
|
219
|
-
state: runtimeState,
|
|
220
|
-
store: input.runtimeAdapterOptions.storeResolver?.(input.binding),
|
|
221
|
-
};
|
|
222
|
-
const configuredBackend = isDeepAgentBinding(input.binding)
|
|
223
|
-
? input.runtimeAdapterOptions.backendResolver?.(input.binding)
|
|
224
|
-
: input.resolveFilesystemBackend(input.binding);
|
|
225
|
-
if (typeof configuredBackend === "function") {
|
|
226
|
-
return configuredBackend(runtimeLike);
|
|
227
|
-
}
|
|
228
|
-
if (configuredBackend) {
|
|
229
|
-
return configuredBackend;
|
|
230
|
-
}
|
|
231
|
-
return new StateBackend(runtimeLike);
|
|
232
|
-
}
|
|
233
|
-
export async function resolveSubagents(input) {
|
|
234
|
-
return Promise.all(input.subagents.map(async (subagent) => {
|
|
235
|
-
if (shouldUseControlledBuiltinSubagent(subagent)) {
|
|
236
|
-
return buildControlledBuiltinSubagent({
|
|
237
|
-
subagent,
|
|
238
|
-
binding: input.binding,
|
|
239
|
-
resolveModel: input.resolveModel,
|
|
240
|
-
resolveTools: input.resolveTools,
|
|
241
|
-
createDeclaredMiddlewareResolverOptions: input.createDeclaredMiddlewareResolverOptions,
|
|
242
|
-
resolveBackend: input.resolveBackend,
|
|
243
|
-
});
|
|
244
|
-
}
|
|
245
|
-
// Only pass DeepAgents-supported subagent fields through to upstream middleware.
|
|
246
|
-
// Harness-only extensions stay internal instead of becoming a second subagent dialect.
|
|
247
|
-
return {
|
|
248
|
-
name: subagent.name,
|
|
249
|
-
description: subagent.description,
|
|
250
|
-
systemPrompt: subagent.systemPrompt,
|
|
251
|
-
model: subagent.model ? (await input.resolveModel(subagent.model)) : undefined,
|
|
252
|
-
tools: subagent.tools ? input.resolveTools(subagent.tools, input.binding) : undefined,
|
|
253
|
-
skills: await materializeDeepAgentSkillSourcePaths({
|
|
254
|
-
workspaceRoot: input.binding?.harnessRuntime.workspaceRoot,
|
|
255
|
-
runtimeRoot: input.binding?.harnessRuntime.runtimeRoot,
|
|
256
|
-
ownerId: `${input.binding?.agent.id ?? "agent"}-${subagent.name}`,
|
|
257
|
-
skillPaths: subagent.skills,
|
|
258
|
-
}),
|
|
259
|
-
interruptOn: compileInterruptOn(subagent.tools ?? [], subagent.interruptOn),
|
|
260
|
-
responseFormat: subagent.responseFormat,
|
|
261
|
-
middleware: (await resolveDeclaredMiddleware(subagent.middleware, input.createDeclaredMiddlewareResolverOptions(input.binding))),
|
|
262
|
-
};
|
|
263
|
-
}));
|
|
264
|
-
}
|
|
265
|
-
export async function invokeBuiltinTaskTool(input) {
|
|
266
|
-
if (!isDeepAgentBinding(input.binding) || getBindingExecutionKind(input.binding) !== "deepagent") {
|
|
267
|
-
throw new Error("The built-in task tool is only available for deepagent bindings.");
|
|
268
|
-
}
|
|
269
|
-
const primaryModel = getBindingPrimaryModel(input.binding);
|
|
270
|
-
const primaryTools = getBindingPrimaryTools(input.binding);
|
|
271
|
-
const compiledSubagents = getBindingDeepAgentSubagents(input.binding)
|
|
272
|
-
.filter((subagent) => !("graphId" in subagent));
|
|
273
|
-
if (!primaryModel) {
|
|
274
|
-
throw new Error(`Agent ${input.binding.agent.id} has no deepagent params`);
|
|
275
|
-
}
|
|
276
|
-
const typedInput = isRecord(input.toolInput) ? input.toolInput : {};
|
|
277
|
-
const description = typeof typedInput.description === "string"
|
|
278
|
-
? typedInput.description
|
|
279
|
-
: typeof typedInput.instruction === "string"
|
|
280
|
-
? typedInput.instruction
|
|
281
|
-
: typeof typedInput.task === "string"
|
|
282
|
-
? typedInput.task
|
|
283
|
-
: typeof typedInput.prompt === "string"
|
|
284
|
-
? typedInput.prompt
|
|
285
|
-
: "";
|
|
286
|
-
const subagentType = typeof typedInput.subagent_type === "string"
|
|
287
|
-
? typedInput.subagent_type
|
|
288
|
-
: typeof typedInput.agentId === "string"
|
|
289
|
-
? typedInput.agentId
|
|
290
|
-
: typeof typedInput.agent_id === "string"
|
|
291
|
-
? typedInput.agent_id
|
|
292
|
-
: typeof typedInput.subagent === "string"
|
|
293
|
-
? typedInput.subagent
|
|
294
|
-
: "";
|
|
295
|
-
const builtinBackend = input.resolveBuiltinMiddlewareBackend(input.binding, input.options);
|
|
296
|
-
const resolvedSubagents = await input.resolveSubagents(compiledSubagents, input.binding);
|
|
297
|
-
const selectedSubagent = resolvedSubagents.find((subagent) => subagent.name === subagentType);
|
|
298
|
-
const selectedCompiledSubagent = compiledSubagents.find((subagent) => subagent.name === subagentType);
|
|
299
|
-
if (!selectedSubagent) {
|
|
300
|
-
const allowed = resolvedSubagents.map((subagent) => subagent.name);
|
|
301
|
-
const available = resolvedSubagents
|
|
302
|
-
.map((subagent) => `- ${subagent.name}: ${subagent.description}`)
|
|
303
|
-
.join("\n");
|
|
304
|
-
throw new Error(`Error: invoked agent of type ${subagentType}, the only allowed types are ${allowed.map((name) => `\`${name}\``).join(", ")}.${available ? `\nAvailable subagents:\n${available}` : ""}`);
|
|
305
|
-
}
|
|
306
|
-
const resolvedHostModel = selectedSubagent.runnable || selectedSubagent.model ? undefined : await input.resolveModel(primaryModel);
|
|
307
|
-
const resolvedSubagentTools = selectedSubagent.tools
|
|
308
|
-
?? (selectedCompiledSubagent?.tools ? input.resolveTools(selectedCompiledSubagent.tools, input.binding) : undefined)
|
|
309
|
-
?? input.resolveTools(primaryTools, input.binding);
|
|
310
|
-
const runnable = selectedSubagent.runnable ?? (() => {
|
|
311
|
-
const summarizationModel = selectedSubagent.model ?? resolvedHostModel;
|
|
312
|
-
const middleware = buildBuiltinTaskSubagentMiddleware({
|
|
313
|
-
selectedSubagent,
|
|
314
|
-
builtinBackend,
|
|
315
|
-
summarizationModel,
|
|
316
|
-
});
|
|
317
|
-
return createAgent({
|
|
318
|
-
model: (selectedSubagent.model ?? resolvedHostModel),
|
|
319
|
-
tools: resolvedSubagentTools,
|
|
320
|
-
systemPrompt: selectedSubagent.systemPrompt ?? DEFAULT_SUBAGENT_PROMPT,
|
|
321
|
-
middleware: middleware,
|
|
322
|
-
responseFormat: selectedSubagent.responseFormat,
|
|
323
|
-
name: selectedSubagent.name,
|
|
324
|
-
description: selectedSubagent.description,
|
|
325
|
-
});
|
|
326
|
-
})();
|
|
327
|
-
const invokeConfig = {
|
|
328
|
-
configurable: { [UPSTREAM_SESSION_CONFIG_KEY]: `${input.binding.agent.id}:builtin-task` },
|
|
329
|
-
...(input.options?.context ? { context: input.options.context } : {}),
|
|
330
|
-
};
|
|
331
|
-
const taskTimeoutMs = resolveBuiltinTaskTimeoutMs(selectedCompiledSubagent?.model ?? primaryModel);
|
|
332
|
-
const invokeSubagent = (content, priorMessages) => withBuiltinTaskTimeout(() => runnable.invoke({
|
|
333
|
-
messages: [
|
|
334
|
-
...(priorMessages ?? []),
|
|
335
|
-
new HumanMessage({ content }),
|
|
336
|
-
],
|
|
337
|
-
}, invokeConfig), taskTimeoutMs, selectedSubagent.name);
|
|
338
|
-
const result = await invokeSubagent(description);
|
|
339
|
-
const structuredResponse = typeof result === "object" && result !== null && "structuredResponse" in result
|
|
340
|
-
? result.structuredResponse
|
|
341
|
-
: undefined;
|
|
342
|
-
if (structuredResponse !== undefined) {
|
|
343
|
-
return JSON.stringify(structuredResponse);
|
|
344
|
-
}
|
|
345
|
-
const taskContent = extractDeepAgentTaskContent(result);
|
|
346
|
-
if (Array.isArray(taskContent)) {
|
|
347
|
-
const filtered = taskContent.filter((block) => {
|
|
348
|
-
const blockType = typeof block === "object" && block !== null && "type" in block
|
|
349
|
-
? block.type
|
|
350
|
-
: undefined;
|
|
351
|
-
return !(typeof blockType === "string" && INVALID_TOOL_MESSAGE_BLOCK_TYPES.has(blockType));
|
|
352
|
-
});
|
|
353
|
-
if (filtered.length > 0) {
|
|
354
|
-
return filtered;
|
|
355
|
-
}
|
|
356
|
-
}
|
|
357
|
-
else if (taskContent !== undefined && taskContent !== null && taskContent !== "") {
|
|
358
|
-
return taskContent;
|
|
359
|
-
}
|
|
360
|
-
const visibleOutput = extractVisibleOutput(result);
|
|
361
|
-
if (visibleOutput) {
|
|
362
|
-
return visibleOutput;
|
|
363
|
-
}
|
|
364
|
-
const fallbackOutput = extractToolFallbackContext(result);
|
|
365
|
-
return fallbackOutput || "Task completed";
|
|
366
|
-
}
|
|
367
|
-
export async function resolveBuiltinMiddlewareTools(input) {
|
|
368
|
-
const backend = input.resolveBuiltinMiddlewareBackend(input.binding, input.options);
|
|
369
|
-
const params = input.binding.execution?.params ?? input.binding.deepAgentParams ?? input.binding.langchainAgentParams;
|
|
370
|
-
const configuredSubagents = [
|
|
371
|
-
...(Array.isArray(params?.subagents)
|
|
372
|
-
? (params.subagents ?? [])
|
|
373
|
-
: []),
|
|
374
|
-
...(input.binding.agent.subagentRefs ?? []),
|
|
375
|
-
...(input.binding.agent.subagentPathRefs ?? []),
|
|
376
|
-
...(input.binding.agent.asyncSubagents ?? []),
|
|
377
|
-
];
|
|
378
|
-
const includeTaskTool = configuredSubagents.length > 0;
|
|
379
|
-
const taskSubagents = getBindingDeepAgentSubagents(input.binding)
|
|
380
|
-
.filter((subagent) => !("graphId" in subagent))
|
|
381
|
-
.map((subagent) => ({
|
|
382
|
-
name: subagent.name,
|
|
383
|
-
description: subagent.description,
|
|
384
|
-
}));
|
|
385
|
-
const tools = (await createBuiltinMiddlewareTools(backend, {
|
|
386
|
-
includeTaskTool,
|
|
387
|
-
taskSubagents,
|
|
388
|
-
workspaceRoot: input.binding.harnessRuntime.workspaceRoot,
|
|
389
|
-
toolRuntimeContext: input.options?.toolRuntimeContext,
|
|
390
|
-
invokeTaskTool: includeTaskTool
|
|
391
|
-
? (toolInput) => input.invokeBuiltinTaskTool(input.binding, toolInput, input.options)
|
|
392
|
-
: undefined,
|
|
393
|
-
}));
|
|
394
|
-
const builtinTools = getBindingBuiltinToolsConfig(input.binding) ?? {};
|
|
395
|
-
if (builtinTools.todos === false) {
|
|
396
|
-
tools.delete("write_todos");
|
|
397
|
-
tools.delete("read_todos");
|
|
398
|
-
}
|
|
399
|
-
if (builtinTools.filesystem === false) {
|
|
400
|
-
for (const name of [
|
|
401
|
-
"ls",
|
|
402
|
-
"read_file",
|
|
403
|
-
"write_file",
|
|
404
|
-
"edit_file",
|
|
405
|
-
"glob",
|
|
406
|
-
"grep",
|
|
407
|
-
"execute",
|
|
408
|
-
]) {
|
|
409
|
-
tools.delete(name);
|
|
410
|
-
}
|
|
411
|
-
}
|
|
412
|
-
return tools;
|
|
413
|
-
}
|
|
414
|
-
export async function materializeAutomaticSummarizationMiddleware(input) {
|
|
415
|
-
const primaryModel = getBindingPrimaryModel(input.binding);
|
|
416
|
-
if (!primaryModel) {
|
|
417
|
-
return [];
|
|
418
|
-
}
|
|
419
|
-
return resolveDeclaredMiddleware([{ kind: "summarization", model: primaryModel }], input.createDeclaredMiddlewareResolverOptions(input.binding));
|
|
420
|
-
}
|
|
421
|
-
export async function materializeLangChainRuntimeMiddleware(input) {
|
|
422
|
-
const primaryModel = getBindingPrimaryModel(input.binding);
|
|
423
|
-
const primaryTools = getBindingPrimaryTools(input.binding);
|
|
424
|
-
if (!primaryModel) {
|
|
425
|
-
return [];
|
|
426
|
-
}
|
|
427
|
-
const runtimeMiddleware = [];
|
|
428
|
-
if (input.plan.includePatchToolCalls) {
|
|
429
|
-
runtimeMiddleware.push(createPatchToolCallsMiddleware());
|
|
430
|
-
}
|
|
431
|
-
if (input.plan.includeAutomaticSummarization) {
|
|
432
|
-
runtimeMiddleware.push(...(await input.materializeAutomaticSummarizationMiddleware(input.binding)));
|
|
433
|
-
}
|
|
434
|
-
runtimeMiddleware.push(...buildLangChainContextMiddleware({
|
|
435
|
-
binding: input.binding,
|
|
436
|
-
skills: input.plan.context.skills,
|
|
437
|
-
memory: input.plan.context.memory,
|
|
438
|
-
resolveFilesystemBackend: input.resolveFilesystemBackend,
|
|
439
|
-
}));
|
|
440
|
-
if (input.plan.delegation && !isDeepAgentBinding(input.binding)) {
|
|
441
|
-
runtimeMiddleware.push(createSubAgentMiddleware({
|
|
442
|
-
defaultModel: (await input.resolveModel(primaryModel)),
|
|
443
|
-
defaultTools: input.resolveTools(primaryTools, input.binding),
|
|
444
|
-
defaultInterruptOn: input.plan.delegation.interruptOn,
|
|
445
|
-
subagents: (await input.resolveSubagents(input.plan.delegation.subagents, input.binding)),
|
|
446
|
-
generalPurposeAgent: input.plan.delegation.generalPurposeAgent,
|
|
447
|
-
taskDescription: input.plan.delegation.taskDescription ?? null,
|
|
448
|
-
}));
|
|
449
|
-
}
|
|
450
|
-
return runtimeMiddleware;
|
|
451
|
-
}
|
|
452
|
-
export async function resolveLangChainRuntimeExtensionMiddleware(input) {
|
|
453
|
-
const plan = planLangChainRuntimeExtensions(input.binding);
|
|
454
|
-
if (!plan) {
|
|
455
|
-
return [];
|
|
456
|
-
}
|
|
457
|
-
return materializeLangChainRuntimeMiddleware({
|
|
458
|
-
...input,
|
|
459
|
-
plan,
|
|
460
|
-
});
|
|
461
|
-
}
|
|
462
|
-
export async function resolveMiddleware(input) {
|
|
463
|
-
const declarativeUpstreamMiddleware = await resolveDeclaredMiddleware(getBindingMiddlewareConfigs(input.binding), input.createDeclaredMiddlewareResolverOptions(input.binding));
|
|
464
|
-
const runtimeExtensionMiddleware = isLangChainBinding(input.binding)
|
|
465
|
-
? await input.resolveLangChainRuntimeExtensionMiddleware(input.binding)
|
|
466
|
-
: [];
|
|
467
|
-
const middleware = [
|
|
468
|
-
createContextHygieneMiddleware(),
|
|
469
|
-
...declarativeUpstreamMiddleware,
|
|
470
|
-
...runtimeExtensionMiddleware,
|
|
471
|
-
...(input.runtimeAdapterOptions.middlewareResolver ? input.runtimeAdapterOptions.middlewareResolver(input.binding) : []),
|
|
472
|
-
];
|
|
473
|
-
if (input.interruptOn && Object.keys(input.interruptOn).length > 0) {
|
|
474
|
-
middleware.push(humanInTheLoopMiddleware({ interruptOn: input.interruptOn }));
|
|
475
|
-
}
|
|
476
|
-
return middleware;
|
|
477
|
-
}
|
|
1
|
+
import{AIMessage as z,HumanMessage as j}from"@langchain/core/messages";import{createMemoryMiddleware as U,createFilesystemMiddleware as N,createPatchToolCallsMiddleware as k,createSkillsMiddleware as M,createSubAgentMiddleware as H,createSummarizationMiddleware as A,StateBackend as O}from"deepagents";import{createAgent as x,humanInTheLoopMiddleware as v,todoListMiddleware as K}from"langchain";import{createBuiltinMiddlewareTools as G}from"./tool/builtin-middleware-tools.js";import{compileInterruptOn as T}from"./tool/interrupt-policy.js";import{extractToolFallbackContext as q,extractVisibleOutput as V}from"../parsing/output-parsing.js";import{isRecord as b}from"../../utils/object.js";import{resolveDeclaredMiddleware as w}from"./tool/declared-middleware.js";import{UPSTREAM_SESSION_CONFIG_KEY as Y}from"./upstream-configurable-keys.js";import{bindingHasLangChainSubagentSupport as J,bindingHasMiddlewareKind as P,getBindingBuiltinToolsConfig as Q,getBindingExecutionKind as W,getBindingGeneralPurposeAgent as X,getBindingDeepAgentSubagents as C,getBindingInterruptCompatibilityRules as Z,getBindingMiddlewareConfigs as ee,getBindingMemorySources as ne,getBindingPrimaryModel as y,getBindingPrimaryTools as _,getBindingSkills as te,getBindingSubagents as oe,getBindingTaskDescription as re,isDeepAgentBinding as h,isLangChainBinding as I}from"../support/compiled-binding.js";import{materializeDeepAgentSkillSourcePaths as D}from"./compat/deepagent-compat.js";import{DEFAULT_SUBAGENT_PROMPT as se}from"../prompts/runtime-prompts.js";import{createContextHygieneMiddleware as ie}from"./middleware/context-hygiene.js";const ae=new Set(["tool_use","thinking","redacted_thinking"]),le=18e4;function de(e){const n=e?.init?.timeout;return typeof n=="number"&&Number.isFinite(n)&&n>0?Math.floor(n):le}async function ce(e,n,r){let t;try{return await Promise.race([e(),new Promise((o,c)=>{t=setTimeout(()=>{c(new Error(`Delegated agent ${r} timed out after ${n}ms.`))},n),t.unref?.()})])}finally{t&&clearTimeout(t)}}function ge(e){if(typeof e!="object"||e===null)return;const n=e.messages;if(!Array.isArray(n)||n.length===0)return;const r=n[n.length-1];if(!(typeof r!="object"||r===null)){if("content"in r)return r.content;if("kwargs"in r)return r.kwargs?.content}}function De(e){if(!b(e))return"";const n=Array.isArray(e.messages)?e.messages:[],r=n.length>0?n[n.length-1]:void 0;if(!b(r))return"";const t=r.content;return typeof t=="string"?t:Array.isArray(t)?t.map(o=>typeof o=="string"?o:b(o)&&typeof o.text=="string"?o.text:"").filter(o=>o.length>0).join(`
|
|
2
|
+
`):""}function Ee(e){return{messages:[new z({content:e.output||"Task completed"})],...e.structuredResponse!==void 0?{structuredResponse:e.structuredResponse}:{}}}function me(e){return e.builtinTools?.filesystem===!1||e.builtinTools?.todos===!1}function ue(){return new O({})}async function fe(e){const n=e.subagent.model?await e.resolveModel(e.subagent.model):void 0,r=e.subagent.tools?e.resolveTools(e.subagent.tools,e.binding):void 0,t=await D({workspaceRoot:e.binding?.harnessRuntime.workspaceRoot,runtimeRoot:e.binding?.harnessRuntime.runtimeRoot,ownerId:`${e.binding?.agent.id??"agent"}-${e.subagent.name}`,skillPaths:e.subagent.skills})??[],o=await w(e.subagent.middleware,e.createDeclaredMiddlewareResolverOptions(e.binding)),c=T(e.subagent.tools??[],e.subagent.interruptOn),i=e.resolveBackend?.(e.binding)??ue(),g=e.subagent.builtinTools??{},a=[...g.todos===!1?[]:[K()],...t.length>0?[M({backend:i,sources:t})]:[],...g.filesystem===!1?[]:[N({backend:i})],A({model:n,backend:i}),k(),...o,...c?[v({interruptOn:c})]:[]],s=x({model:n,systemPrompt:e.subagent.systemPrompt,tools:r,middleware:a,name:e.subagent.name,...e.subagent.responseFormat!==void 0?{responseFormat:e.subagent.responseFormat}:{}});return{name:e.subagent.name,description:e.subagent.description,systemPrompt:e.subagent.systemPrompt,runnable:s}}function be(e){const{selectedSubagent:n,builtinBackend:r,summarizationModel:t}=e,o=[k(),A({model:t,backend:r})],c=n.skills?.length?[M({backend:r,sources:n.skills})]:[],i=n.middleware??[],g=n.interruptOn?[v({interruptOn:T(n.tools??[],n.interruptOn)})]:[];return[...o,...c,...i,...g]}function we(e){const n=[],r=(e.skills?.length??0)>0,t=(e.memory?.length??0)>0;if(!r&&!t)return n;const o=e.resolveFilesystemBackend(e.binding);return r&&n.push(M({backend:o,sources:e.skills})),t&&n.push(U({backend:o,sources:e.memory})),n}function ye(e){const n=y(e);if(!I(e)||!n)return;const r=te(e),t=ne(e);return{includePatchToolCalls:!P(e,"patchToolCalls"),includeAutomaticSummarization:!P(e,"summarization"),context:{skills:r,memory:t},delegation:J(e)?{subagents:oe(e),generalPurposeAgent:X(e),taskDescription:re(e),interruptOn:Z(e)}:void 0}}function Fe(e){const r={state:{...e.options?.state??{},...b(e.options?.files)?{files:e.options?.files}:{}},store:e.runtimeAdapterOptions.storeResolver?.(e.binding)},t=h(e.binding)?e.runtimeAdapterOptions.backendResolver?.(e.binding):e.resolveFilesystemBackend(e.binding);return typeof t=="function"?t(r):t||new O(r)}async function Le(e){return Promise.all(e.subagents.map(async n=>me(n)?fe({subagent:n,binding:e.binding,resolveModel:e.resolveModel,resolveTools:e.resolveTools,createDeclaredMiddlewareResolverOptions:e.createDeclaredMiddlewareResolverOptions,resolveBackend:e.resolveBackend}):{name:n.name,description:n.description,systemPrompt:n.systemPrompt,model:n.model?await e.resolveModel(n.model):void 0,tools:n.tools?e.resolveTools(n.tools,e.binding):void 0,skills:await D({workspaceRoot:e.binding?.harnessRuntime.workspaceRoot,runtimeRoot:e.binding?.harnessRuntime.runtimeRoot,ownerId:`${e.binding?.agent.id??"agent"}-${n.name}`,skillPaths:n.skills}),interruptOn:T(n.tools??[],n.interruptOn),responseFormat:n.responseFormat,middleware:await w(n.middleware,e.createDeclaredMiddlewareResolverOptions(e.binding))}))}async function $e(e){if(!h(e.binding)||W(e.binding)!=="deepagent")throw new Error("The built-in task tool is only available for deepagent bindings.");const n=y(e.binding),r=_(e.binding),t=C(e.binding).filter(l=>!("graphId"in l));if(!n)throw new Error(`Agent ${e.binding.agent.id} has no deepagent params`);const o=b(e.toolInput)?e.toolInput:{},c=typeof o.description=="string"?o.description:typeof o.instruction=="string"?o.instruction:typeof o.task=="string"?o.task:typeof o.prompt=="string"?o.prompt:"",i=typeof o.subagent_type=="string"?o.subagent_type:typeof o.agentId=="string"?o.agentId:typeof o.agent_id=="string"?o.agent_id:typeof o.subagent=="string"?o.subagent:"",g=e.resolveBuiltinMiddlewareBackend(e.binding,e.options),a=await e.resolveSubagents(t,e.binding),s=a.find(l=>l.name===i),p=t.find(l=>l.name===i);if(!s){const l=a.map(m=>m.name),d=a.map(m=>`- ${m.name}: ${m.description}`).join(`
|
|
3
|
+
`);throw new Error(`Error: invoked agent of type ${i}, the only allowed types are ${l.map(m=>`\`${m}\``).join(", ")}.${d?`
|
|
4
|
+
Available subagents:
|
|
5
|
+
${d}`:""}`)}const S=s.runnable||s.model?void 0:await e.resolveModel(n),E=s.tools??(p?.tools?e.resolveTools(p.tools,e.binding):void 0)??e.resolveTools(r,e.binding),F=s.runnable??(()=>{const l=s.model??S,d=be({selectedSubagent:s,builtinBackend:g,summarizationModel:l});return x({model:s.model??S,tools:E,systemPrompt:s.systemPrompt??se,middleware:d,responseFormat:s.responseFormat,name:s.name,description:s.description})})(),L={configurable:{[Y]:`${e.binding.agent.id}:builtin-task`},...e.options?.context?{context:e.options.context}:{}},$=de(p?.model??n),u=await((l,d)=>ce(()=>F.invoke({messages:[...d??[],new j({content:l})]},L),$,s.name))(c),R=typeof u=="object"&&u!==null&&"structuredResponse"in u?u.structuredResponse:void 0;if(R!==void 0)return JSON.stringify(R);const f=ge(u);if(Array.isArray(f)){const l=f.filter(d=>{const m=typeof d=="object"&&d!==null&&"type"in d?d.type:void 0;return!(typeof m=="string"&&ae.has(m))});if(l.length>0)return l}else if(f!=null&&f!=="")return f;const B=V(u);return B||q(u)||"Task completed"}async function ze(e){const n=e.resolveBuiltinMiddlewareBackend(e.binding,e.options),r=e.binding.execution?.params??e.binding.deepAgentParams??e.binding.langchainAgentParams,o=[...Array.isArray(r?.subagents)?r.subagents??[]:[],...e.binding.agent.subagentRefs??[],...e.binding.agent.subagentPathRefs??[],...e.binding.agent.asyncSubagents??[]].length>0,c=C(e.binding).filter(a=>!("graphId"in a)).map(a=>({name:a.name,description:a.description})),i=await G(n,{includeTaskTool:o,taskSubagents:c,workspaceRoot:e.binding.harnessRuntime.workspaceRoot,toolRuntimeContext:e.options?.toolRuntimeContext,invokeTaskTool:o?a=>e.invokeBuiltinTaskTool(e.binding,a,e.options):void 0}),g=Q(e.binding)??{};if(g.todos===!1&&(i.delete("write_todos"),i.delete("read_todos")),g.filesystem===!1)for(const a of["ls","read_file","write_file","edit_file","glob","grep","execute"])i.delete(a);return i}async function je(e){const n=y(e.binding);return n?w([{kind:"summarization",model:n}],e.createDeclaredMiddlewareResolverOptions(e.binding)):[]}async function pe(e){const n=y(e.binding),r=_(e.binding);if(!n)return[];const t=[];return e.plan.includePatchToolCalls&&t.push(k()),e.plan.includeAutomaticSummarization&&t.push(...await e.materializeAutomaticSummarizationMiddleware(e.binding)),t.push(...we({binding:e.binding,skills:e.plan.context.skills,memory:e.plan.context.memory,resolveFilesystemBackend:e.resolveFilesystemBackend})),e.plan.delegation&&!h(e.binding)&&t.push(H({defaultModel:await e.resolveModel(n),defaultTools:e.resolveTools(r,e.binding),defaultInterruptOn:e.plan.delegation.interruptOn,subagents:await e.resolveSubagents(e.plan.delegation.subagents,e.binding),generalPurposeAgent:e.plan.delegation.generalPurposeAgent,taskDescription:e.plan.delegation.taskDescription??null})),t}async function Ue(e){const n=ye(e.binding);return n?pe({...e,plan:n}):[]}async function Ne(e){const n=await w(ee(e.binding),e.createDeclaredMiddlewareResolverOptions(e.binding)),r=I(e.binding)?await e.resolveLangChainRuntimeExtensionMiddleware(e.binding):[],t=[ie(),...n,...r,...e.runtimeAdapterOptions.middlewareResolver?e.runtimeAdapterOptions.middlewareResolver(e.binding):[]];return e.interruptOn&&Object.keys(e.interruptOn).length>0&&t.push(v({interruptOn:e.interruptOn})),t}export{be as buildBuiltinTaskSubagentMiddleware,De as extractSubagentRequestText,$e as invokeBuiltinTaskTool,je as materializeAutomaticSummarizationMiddleware,pe as materializeLangChainRuntimeMiddleware,ye as planLangChainRuntimeExtensions,Fe as resolveBuiltinMiddlewareBackend,ze as resolveBuiltinMiddlewareTools,Ue as resolveLangChainRuntimeExtensionMiddleware,Ne as resolveMiddleware,Le as resolveSubagents,Ee as wrapRequestResultAsSubagentResponse};
|