@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,378 +1,3 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
function normalizePath(value, fallback) {
|
|
5
|
-
const source = typeof value === "string" && value.trim().length > 0 ? value.trim() : fallback;
|
|
6
|
-
return source.startsWith("/") ? source : `/${source}`;
|
|
7
|
-
}
|
|
8
|
-
function readRequestBody(request) {
|
|
9
|
-
return new Promise((resolve, reject) => {
|
|
10
|
-
const chunks = [];
|
|
11
|
-
request.on("data", (chunk) => {
|
|
12
|
-
chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
|
|
13
|
-
});
|
|
14
|
-
request.on("end", () => resolve(Buffer.concat(chunks).toString("utf8")));
|
|
15
|
-
request.on("error", reject);
|
|
16
|
-
});
|
|
17
|
-
}
|
|
18
|
-
function writeSseEvent(response, payload) {
|
|
19
|
-
return new Promise((resolve, reject) => {
|
|
20
|
-
response.write(`data: ${JSON.stringify(payload)}\n\n`, (error) => {
|
|
21
|
-
if (error) {
|
|
22
|
-
reject(error);
|
|
23
|
-
return;
|
|
24
|
-
}
|
|
25
|
-
resolve();
|
|
26
|
-
});
|
|
27
|
-
});
|
|
28
|
-
}
|
|
29
|
-
function parseRequestInput(payload) {
|
|
30
|
-
if (!payload || typeof payload !== "object" || Array.isArray(payload)) {
|
|
31
|
-
throw new Error("AG-UI request input must be an object.");
|
|
32
|
-
}
|
|
33
|
-
const typed = payload;
|
|
34
|
-
if (typeof typed.input !== "string"
|
|
35
|
-
&& !(Array.isArray(typed.input))) {
|
|
36
|
-
throw new Error("AG-UI request input requires `input`.");
|
|
37
|
-
}
|
|
38
|
-
return {
|
|
39
|
-
input: typed.input,
|
|
40
|
-
...(typeof typed.agentId === "string" && typed.agentId.trim().length > 0 ? { agentId: typed.agentId.trim() } : {}),
|
|
41
|
-
...(typeof typed.sessionId === "string" && typed.sessionId.trim().length > 0 ? { sessionId: typed.sessionId.trim() } : {}),
|
|
42
|
-
...(typed.invocation && typeof typed.invocation === "object" && !Array.isArray(typed.invocation)
|
|
43
|
-
? { invocation: typed.invocation }
|
|
44
|
-
: {}),
|
|
45
|
-
};
|
|
46
|
-
}
|
|
47
|
-
function createTimestamp() {
|
|
48
|
-
return new Date().toISOString();
|
|
49
|
-
}
|
|
50
|
-
function toRunStarted(event, input) {
|
|
51
|
-
return {
|
|
52
|
-
type: "RUN_STARTED",
|
|
53
|
-
timestamp: createTimestamp(),
|
|
54
|
-
sessionId: event.sessionId,
|
|
55
|
-
requestId: event.requestId,
|
|
56
|
-
input,
|
|
57
|
-
};
|
|
58
|
-
}
|
|
59
|
-
function asObject(value) {
|
|
60
|
-
return typeof value === "object" && value !== null ? value : null;
|
|
61
|
-
}
|
|
62
|
-
function readUpstreamEventName(event) {
|
|
63
|
-
return typeof asObject(event)?.event === "string" ? String(asObject(event)?.event) : "";
|
|
64
|
-
}
|
|
65
|
-
function readUpstreamToolName(event) {
|
|
66
|
-
return typeof asObject(event)?.name === "string" ? String(asObject(event)?.name) : "";
|
|
67
|
-
}
|
|
68
|
-
function readUpstreamRunType(event) {
|
|
69
|
-
return typeof asObject(event)?.run_type === "string" ? String(asObject(event)?.run_type) : "";
|
|
70
|
-
}
|
|
71
|
-
function isToolStartEvent(event) {
|
|
72
|
-
const eventName = readUpstreamEventName(event);
|
|
73
|
-
return eventName === "on_tool_start" || (eventName === "on_chain_start" && readUpstreamRunType(event) === "tool");
|
|
74
|
-
}
|
|
75
|
-
function isToolTerminalEvent(event) {
|
|
76
|
-
const eventName = readUpstreamEventName(event);
|
|
77
|
-
return eventName === "on_tool_end"
|
|
78
|
-
|| eventName === "on_tool_error"
|
|
79
|
-
|| ((eventName === "on_chain_end" || eventName === "on_chain_error") && readUpstreamRunType(event) === "tool");
|
|
80
|
-
}
|
|
81
|
-
function readToolArgs(event) {
|
|
82
|
-
const typed = asObject(event);
|
|
83
|
-
const data = asObject(typed?.data);
|
|
84
|
-
if (!data || !("input" in data)) {
|
|
85
|
-
return undefined;
|
|
86
|
-
}
|
|
87
|
-
return data.input;
|
|
88
|
-
}
|
|
89
|
-
function createToolCallState() {
|
|
90
|
-
const activeToolCallIds = new Map();
|
|
91
|
-
return {
|
|
92
|
-
start(toolName) {
|
|
93
|
-
const toolCallId = `tool-${createPersistentId()}`;
|
|
94
|
-
const activeIds = activeToolCallIds.get(toolName) ?? [];
|
|
95
|
-
activeIds.push(toolCallId);
|
|
96
|
-
activeToolCallIds.set(toolName, activeIds);
|
|
97
|
-
return toolCallId;
|
|
98
|
-
},
|
|
99
|
-
peek(toolName) {
|
|
100
|
-
const activeIds = activeToolCallIds.get(toolName);
|
|
101
|
-
return activeIds?.at(-1);
|
|
102
|
-
},
|
|
103
|
-
finish(toolName) {
|
|
104
|
-
const activeIds = activeToolCallIds.get(toolName);
|
|
105
|
-
if (!activeIds || activeIds.length === 0) {
|
|
106
|
-
return undefined;
|
|
107
|
-
}
|
|
108
|
-
const toolCallId = activeIds.pop();
|
|
109
|
-
if (!activeIds.length) {
|
|
110
|
-
activeToolCallIds.delete(toolName);
|
|
111
|
-
}
|
|
112
|
-
return toolCallId;
|
|
113
|
-
},
|
|
114
|
-
};
|
|
115
|
-
}
|
|
116
|
-
export async function serveAgUiOverHttp(runtime, options = {}) {
|
|
117
|
-
const hostname = options.hostname?.trim() || "127.0.0.1";
|
|
118
|
-
const port = typeof options.port === "number" && Number.isFinite(options.port) ? options.port : 0;
|
|
119
|
-
const runPath = normalizePath(options.runPath, "/run");
|
|
120
|
-
const httpServer = createServer(async (request, response) => {
|
|
121
|
-
const url = new URL(request.url ?? "/", `http://${hostname}`);
|
|
122
|
-
if (request.method !== "POST" || url.pathname !== runPath) {
|
|
123
|
-
response.statusCode = 404;
|
|
124
|
-
response.setHeader("content-type", "application/json; charset=utf-8");
|
|
125
|
-
response.end(JSON.stringify({ error: "Not Found", runPath }));
|
|
126
|
-
return;
|
|
127
|
-
}
|
|
128
|
-
response.statusCode = 200;
|
|
129
|
-
response.setHeader("content-type", "text/event-stream; charset=utf-8");
|
|
130
|
-
response.setHeader("cache-control", "no-cache, no-transform");
|
|
131
|
-
response.setHeader("connection", "keep-alive");
|
|
132
|
-
let requestId;
|
|
133
|
-
let sessionId;
|
|
134
|
-
let messageId;
|
|
135
|
-
let thinkingMessageId;
|
|
136
|
-
let textMessageStarted = false;
|
|
137
|
-
let thinkingMessageStarted = false;
|
|
138
|
-
const toolCalls = createToolCallState();
|
|
139
|
-
const upstreamReducer = createUpstreamTimelineReducer();
|
|
140
|
-
const ensureTextStart = async () => {
|
|
141
|
-
if (textMessageStarted) {
|
|
142
|
-
return;
|
|
143
|
-
}
|
|
144
|
-
messageId = messageId ?? `msg-${createPersistentId()}`;
|
|
145
|
-
textMessageStarted = true;
|
|
146
|
-
await writeSseEvent(response, {
|
|
147
|
-
type: "TEXT_MESSAGE_START",
|
|
148
|
-
timestamp: createTimestamp(),
|
|
149
|
-
messageId,
|
|
150
|
-
role: "assistant",
|
|
151
|
-
});
|
|
152
|
-
};
|
|
153
|
-
const ensureThinkingTextStart = async () => {
|
|
154
|
-
if (thinkingMessageStarted) {
|
|
155
|
-
return;
|
|
156
|
-
}
|
|
157
|
-
thinkingMessageId = thinkingMessageId ?? `thinking-${createPersistentId()}`;
|
|
158
|
-
thinkingMessageStarted = true;
|
|
159
|
-
await writeSseEvent(response, {
|
|
160
|
-
type: "THINKING_TEXT_MESSAGE_START",
|
|
161
|
-
timestamp: createTimestamp(),
|
|
162
|
-
messageId: thinkingMessageId,
|
|
163
|
-
role: "assistant",
|
|
164
|
-
});
|
|
165
|
-
};
|
|
166
|
-
const closeOpenMessages = async () => {
|
|
167
|
-
if (thinkingMessageStarted) {
|
|
168
|
-
await writeSseEvent(response, {
|
|
169
|
-
type: "THINKING_TEXT_MESSAGE_END",
|
|
170
|
-
timestamp: createTimestamp(),
|
|
171
|
-
messageId: thinkingMessageId,
|
|
172
|
-
});
|
|
173
|
-
thinkingMessageStarted = false;
|
|
174
|
-
}
|
|
175
|
-
if (textMessageStarted) {
|
|
176
|
-
await writeSseEvent(response, {
|
|
177
|
-
type: "TEXT_MESSAGE_END",
|
|
178
|
-
timestamp: createTimestamp(),
|
|
179
|
-
messageId: messageId,
|
|
180
|
-
});
|
|
181
|
-
textMessageStarted = false;
|
|
182
|
-
}
|
|
183
|
-
};
|
|
184
|
-
try {
|
|
185
|
-
const body = await readRequestBody(request);
|
|
186
|
-
const input = parseRequestInput(JSON.parse(body));
|
|
187
|
-
const result = await runtime.run({
|
|
188
|
-
agentId: input.agentId,
|
|
189
|
-
input: input.input,
|
|
190
|
-
...(input.sessionId ? { sessionId: input.sessionId } : {}),
|
|
191
|
-
...(input.invocation ? { invocation: input.invocation } : {}),
|
|
192
|
-
listeners: {
|
|
193
|
-
onEvent: async (event) => {
|
|
194
|
-
if (event.eventType === "request.created") {
|
|
195
|
-
requestId = event.requestId;
|
|
196
|
-
sessionId = event.sessionId;
|
|
197
|
-
await writeSseEvent(response, toRunStarted(event, input.input));
|
|
198
|
-
return;
|
|
199
|
-
}
|
|
200
|
-
if (event.eventType === "output.delta") {
|
|
201
|
-
requestId = requestId ?? event.requestId;
|
|
202
|
-
sessionId = sessionId ?? event.sessionId;
|
|
203
|
-
const delta = typeof event.payload.content === "string" ? event.payload.content : "";
|
|
204
|
-
if (!delta) {
|
|
205
|
-
return;
|
|
206
|
-
}
|
|
207
|
-
await ensureTextStart();
|
|
208
|
-
await writeSseEvent(response, {
|
|
209
|
-
type: "TEXT_MESSAGE_CONTENT",
|
|
210
|
-
timestamp: createTimestamp(),
|
|
211
|
-
messageId: messageId,
|
|
212
|
-
delta,
|
|
213
|
-
});
|
|
214
|
-
}
|
|
215
|
-
},
|
|
216
|
-
dataListener: async (event) => {
|
|
217
|
-
if (event.type !== "progress.commentary") {
|
|
218
|
-
return;
|
|
219
|
-
}
|
|
220
|
-
await writeSseEvent(response, {
|
|
221
|
-
type: "STATUS_UPDATE",
|
|
222
|
-
timestamp: createTimestamp(),
|
|
223
|
-
message: event.text,
|
|
224
|
-
});
|
|
225
|
-
},
|
|
226
|
-
onUpstreamEvent: async (event) => {
|
|
227
|
-
const eventName = readUpstreamEventName(event);
|
|
228
|
-
const toolName = readUpstreamToolName(event);
|
|
229
|
-
if (isToolStartEvent(event) && toolName) {
|
|
230
|
-
const toolCallId = toolCalls.start(toolName);
|
|
231
|
-
await writeSseEvent(response, {
|
|
232
|
-
type: "TOOL_CALL_START",
|
|
233
|
-
timestamp: createTimestamp(),
|
|
234
|
-
toolCallId,
|
|
235
|
-
toolName,
|
|
236
|
-
});
|
|
237
|
-
const args = readToolArgs(event);
|
|
238
|
-
if (args !== undefined) {
|
|
239
|
-
await writeSseEvent(response, {
|
|
240
|
-
type: "TOOL_CALL_ARGS",
|
|
241
|
-
timestamp: createTimestamp(),
|
|
242
|
-
toolCallId,
|
|
243
|
-
toolName,
|
|
244
|
-
args,
|
|
245
|
-
});
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
const projections = upstreamReducer.consume(event);
|
|
249
|
-
for (const projection of projections) {
|
|
250
|
-
if (projection.type === "thinking") {
|
|
251
|
-
if (!projection.text) {
|
|
252
|
-
continue;
|
|
253
|
-
}
|
|
254
|
-
await ensureThinkingTextStart();
|
|
255
|
-
await writeSseEvent(response, {
|
|
256
|
-
type: "THINKING_TEXT_MESSAGE_CONTENT",
|
|
257
|
-
timestamp: createTimestamp(),
|
|
258
|
-
messageId: thinkingMessageId,
|
|
259
|
-
delta: projection.text,
|
|
260
|
-
});
|
|
261
|
-
continue;
|
|
262
|
-
}
|
|
263
|
-
if (projection.type === "step") {
|
|
264
|
-
if (projection.status === "started") {
|
|
265
|
-
await writeSseEvent(response, {
|
|
266
|
-
type: "STEP_STARTED",
|
|
267
|
-
timestamp: createTimestamp(),
|
|
268
|
-
stepId: projection.key,
|
|
269
|
-
title: projection.step,
|
|
270
|
-
category: projection.category,
|
|
271
|
-
});
|
|
272
|
-
continue;
|
|
273
|
-
}
|
|
274
|
-
await writeSseEvent(response, {
|
|
275
|
-
type: "STEP_FINISHED",
|
|
276
|
-
timestamp: createTimestamp(),
|
|
277
|
-
stepId: projection.key,
|
|
278
|
-
title: projection.step,
|
|
279
|
-
category: projection.category,
|
|
280
|
-
status: projection.status,
|
|
281
|
-
});
|
|
282
|
-
continue;
|
|
283
|
-
}
|
|
284
|
-
const toolCallId = toolCalls.peek(projection.toolName) ?? toolCalls.start(projection.toolName);
|
|
285
|
-
await writeSseEvent(response, {
|
|
286
|
-
type: "TOOL_CALL_RESULT",
|
|
287
|
-
timestamp: createTimestamp(),
|
|
288
|
-
toolCallId,
|
|
289
|
-
toolName: projection.toolName,
|
|
290
|
-
result: projection.output,
|
|
291
|
-
...(projection.isError !== undefined ? { isError: projection.isError } : {}),
|
|
292
|
-
});
|
|
293
|
-
}
|
|
294
|
-
if (isToolTerminalEvent(event) && toolName) {
|
|
295
|
-
const toolCallId = toolCalls.finish(toolName) ?? `tool-${createPersistentId()}`;
|
|
296
|
-
await writeSseEvent(response, {
|
|
297
|
-
type: "TOOL_CALL_END",
|
|
298
|
-
timestamp: createTimestamp(),
|
|
299
|
-
toolCallId,
|
|
300
|
-
toolName,
|
|
301
|
-
});
|
|
302
|
-
}
|
|
303
|
-
if (eventName === "on_chat_model_end" && thinkingMessageStarted) {
|
|
304
|
-
await writeSseEvent(response, {
|
|
305
|
-
type: "THINKING_TEXT_MESSAGE_END",
|
|
306
|
-
timestamp: createTimestamp(),
|
|
307
|
-
messageId: thinkingMessageId,
|
|
308
|
-
});
|
|
309
|
-
thinkingMessageStarted = false;
|
|
310
|
-
}
|
|
311
|
-
},
|
|
312
|
-
},
|
|
313
|
-
});
|
|
314
|
-
requestId = requestId ?? result.requestId;
|
|
315
|
-
sessionId = sessionId ?? result.sessionId;
|
|
316
|
-
if (!textMessageStarted && result.output) {
|
|
317
|
-
await ensureTextStart();
|
|
318
|
-
await writeSseEvent(response, {
|
|
319
|
-
type: "TEXT_MESSAGE_CONTENT",
|
|
320
|
-
timestamp: createTimestamp(),
|
|
321
|
-
messageId: messageId,
|
|
322
|
-
delta: result.output,
|
|
323
|
-
});
|
|
324
|
-
}
|
|
325
|
-
await closeOpenMessages();
|
|
326
|
-
await writeSseEvent(response, {
|
|
327
|
-
type: "RUN_FINISHED",
|
|
328
|
-
timestamp: createTimestamp(),
|
|
329
|
-
sessionId: sessionId,
|
|
330
|
-
requestId: requestId,
|
|
331
|
-
state: result.state,
|
|
332
|
-
...(result.approvalId ? { approvalId: result.approvalId } : {}),
|
|
333
|
-
...(result.pendingActionId ? { pendingActionId: result.pendingActionId } : {}),
|
|
334
|
-
});
|
|
335
|
-
}
|
|
336
|
-
catch (error) {
|
|
337
|
-
await closeOpenMessages();
|
|
338
|
-
await writeSseEvent(response, {
|
|
339
|
-
type: "RUN_ERROR",
|
|
340
|
-
timestamp: createTimestamp(),
|
|
341
|
-
...(sessionId ? { sessionId } : {}),
|
|
342
|
-
...(requestId ? { requestId } : {}),
|
|
343
|
-
message: error instanceof Error ? error.message : "AG-UI request failed.",
|
|
344
|
-
});
|
|
345
|
-
}
|
|
346
|
-
finally {
|
|
347
|
-
response.end();
|
|
348
|
-
}
|
|
349
|
-
});
|
|
350
|
-
const completed = new Promise((resolve, reject) => {
|
|
351
|
-
httpServer.once("close", resolve);
|
|
352
|
-
httpServer.once("error", reject);
|
|
353
|
-
});
|
|
354
|
-
await new Promise((resolve, reject) => {
|
|
355
|
-
httpServer.listen(port, hostname, () => resolve());
|
|
356
|
-
httpServer.once("error", reject);
|
|
357
|
-
});
|
|
358
|
-
const address = httpServer.address();
|
|
359
|
-
const resolvedPort = typeof address === "object" && address ? address.port : port;
|
|
360
|
-
return {
|
|
361
|
-
hostname,
|
|
362
|
-
port: resolvedPort,
|
|
363
|
-
runPath,
|
|
364
|
-
runUrl: `http://${hostname}:${resolvedPort}${runPath}`,
|
|
365
|
-
completed,
|
|
366
|
-
close: async () => {
|
|
367
|
-
await new Promise((resolve, reject) => {
|
|
368
|
-
httpServer.close((error) => {
|
|
369
|
-
if (error) {
|
|
370
|
-
reject(error);
|
|
371
|
-
return;
|
|
372
|
-
}
|
|
373
|
-
resolve();
|
|
374
|
-
});
|
|
375
|
-
});
|
|
376
|
-
},
|
|
377
|
-
};
|
|
378
|
-
}
|
|
1
|
+
import{createServer as D}from"node:http";import{createPersistentId as R}from"../../utils/id.js";import{createUpstreamTimelineReducer as $}from"../../projections/upstream-events.js";function j(t,e){const a=typeof t=="string"&&t.trim().length>0?t.trim():e;return a.startsWith("/")?a:`/${a}`}function k(t){return new Promise((e,a)=>{const c=[];t.on("data",u=>{c.push(Buffer.isBuffer(u)?u:Buffer.from(u))}),t.on("end",()=>e(Buffer.concat(c).toString("utf8"))),t.on("error",a)})}function o(t,e){return new Promise((a,c)=>{t.write(`data: ${JSON.stringify(e)}
|
|
2
|
+
|
|
3
|
+
`,u=>{if(u){c(u);return}a()})})}function x(t){if(!t||typeof t!="object"||Array.isArray(t))throw new Error("AG-UI request input must be an object.");const e=t;if(typeof e.input!="string"&&!Array.isArray(e.input))throw new Error("AG-UI request input requires `input`.");return{input:e.input,...typeof e.agentId=="string"&&e.agentId.trim().length>0?{agentId:e.agentId.trim()}:{},...typeof e.sessionId=="string"&&e.sessionId.trim().length>0?{sessionId:e.sessionId.trim()}:{},...e.invocation&&typeof e.invocation=="object"&&!Array.isArray(e.invocation)?{invocation:e.invocation}:{}}}function s(){return new Date().toISOString()}function X(t,e){return{type:"RUN_STARTED",timestamp:s(),sessionId:t.sessionId,requestId:t.requestId,input:e}}function f(t){return typeof t=="object"&&t!==null?t:null}function U(t){return typeof f(t)?.event=="string"?String(f(t)?.event):""}function B(t){return typeof f(t)?.name=="string"?String(f(t)?.name):""}function M(t){return typeof f(t)?.run_type=="string"?String(f(t)?.run_type):""}function F(t){const e=U(t);return e==="on_tool_start"||e==="on_chain_start"&&M(t)==="tool"}function K(t){const e=U(t);return e==="on_tool_end"||e==="on_tool_error"||(e==="on_chain_end"||e==="on_chain_error")&&M(t)==="tool"}function J(t){const e=f(t),a=f(e?.data);if(!(!a||!("input"in a)))return a.input}function z(){const t=new Map;return{start(e){const a=`tool-${R()}`,c=t.get(e)??[];return c.push(a),t.set(e,c),a},peek(e){return t.get(e)?.at(-1)},finish(e){const a=t.get(e);if(!a||a.length===0)return;const c=a.pop();return a.length||t.delete(e),c}}}async function Y(t,e={}){const a=e.hostname?.trim()||"127.0.0.1",c=typeof e.port=="number"&&Number.isFinite(e.port)?e.port:0,u=j(e.runPath,"/run"),T=D(async(d,n)=>{const w=new URL(d.url??"/",`http://${a}`);if(d.method!=="POST"||w.pathname!==u){n.statusCode=404,n.setHeader("content-type","application/json; charset=utf-8"),n.end(JSON.stringify({error:"Not Found",runPath:u}));return}n.statusCode=200,n.setHeader("content-type","text/event-stream; charset=utf-8"),n.setHeader("cache-control","no-cache, no-transform"),n.setHeader("connection","keep-alive");let p,l,g,E,S=!1,_=!1;const N=z(),H=$(),L=async()=>{S||(g=g??`msg-${R()}`,S=!0,await o(n,{type:"TEXT_MESSAGE_START",timestamp:s(),messageId:g,role:"assistant"}))},b=async()=>{_||(E=E??`thinking-${R()}`,_=!0,await o(n,{type:"THINKING_TEXT_MESSAGE_START",timestamp:s(),messageId:E,role:"assistant"}))},P=async()=>{_&&(await o(n,{type:"THINKING_TEXT_MESSAGE_END",timestamp:s(),messageId:E}),_=!1),S&&(await o(n,{type:"TEXT_MESSAGE_END",timestamp:s(),messageId:g}),S=!1)};try{const h=await k(d),I=x(JSON.parse(h)),m=await t.run({agentId:I.agentId,input:I.input,...I.sessionId?{sessionId:I.sessionId}:{},...I.invocation?{invocation:I.invocation}:{},listeners:{onEvent:async r=>{if(r.eventType==="request.created"){p=r.requestId,l=r.sessionId,await o(n,X(r,I.input));return}if(r.eventType==="output.delta"){p=p??r.requestId,l=l??r.sessionId;const A=typeof r.payload.content=="string"?r.payload.content:"";if(!A)return;await L(),await o(n,{type:"TEXT_MESSAGE_CONTENT",timestamp:s(),messageId:g,delta:A})}},dataListener:async r=>{r.type==="progress.commentary"&&await o(n,{type:"STATUS_UPDATE",timestamp:s(),message:r.text})},onUpstreamEvent:async r=>{const A=U(r),y=B(r);if(F(r)&&y){const i=N.start(y);await o(n,{type:"TOOL_CALL_START",timestamp:s(),toolCallId:i,toolName:y});const O=J(r);O!==void 0&&await o(n,{type:"TOOL_CALL_ARGS",timestamp:s(),toolCallId:i,toolName:y,args:O})}const q=H.consume(r);for(const i of q){if(i.type==="thinking"){if(!i.text)continue;await b(),await o(n,{type:"THINKING_TEXT_MESSAGE_CONTENT",timestamp:s(),messageId:E,delta:i.text});continue}if(i.type==="step"){if(i.status==="started"){await o(n,{type:"STEP_STARTED",timestamp:s(),stepId:i.key,title:i.step,category:i.category});continue}await o(n,{type:"STEP_FINISHED",timestamp:s(),stepId:i.key,title:i.step,category:i.category,status:i.status});continue}const O=N.peek(i.toolName)??N.start(i.toolName);await o(n,{type:"TOOL_CALL_RESULT",timestamp:s(),toolCallId:O,toolName:i.toolName,result:i.output,...i.isError!==void 0?{isError:i.isError}:{}})}if(K(r)&&y){const i=N.finish(y)??`tool-${R()}`;await o(n,{type:"TOOL_CALL_END",timestamp:s(),toolCallId:i,toolName:y})}A==="on_chat_model_end"&&_&&(await o(n,{type:"THINKING_TEXT_MESSAGE_END",timestamp:s(),messageId:E}),_=!1)}}});p=p??m.requestId,l=l??m.sessionId,!S&&m.output&&(await L(),await o(n,{type:"TEXT_MESSAGE_CONTENT",timestamp:s(),messageId:g,delta:m.output})),await P(),await o(n,{type:"RUN_FINISHED",timestamp:s(),sessionId:l,requestId:p,state:m.state,...m.approvalId?{approvalId:m.approvalId}:{},...m.pendingActionId?{pendingActionId:m.pendingActionId}:{}})}catch(h){await P(),await o(n,{type:"RUN_ERROR",timestamp:s(),...l?{sessionId:l}:{},...p?{requestId:p}:{},message:h instanceof Error?h.message:"AG-UI request failed."})}finally{n.end()}}),v=new Promise((d,n)=>{T.once("close",d),T.once("error",n)});await new Promise((d,n)=>{T.listen(c,a,()=>d()),T.once("error",n)});const C=T.address(),G=typeof C=="object"&&C?C.port:c;return{hostname:a,port:G,runPath:u,runUrl:`http://${a}:${G}${u}`,completed:v,close:async()=>{await new Promise((d,n)=>{T.close(w=>{if(w){n(w);return}d()})})}}}export{Y as serveAgUiOverHttp};
|