@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,294 +1,8 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
}
|
|
10
|
-
function writeJsonLine(output, payload) {
|
|
11
|
-
return new Promise((resolve, reject) => {
|
|
12
|
-
output.write(`${JSON.stringify(payload)}\n`, (error) => {
|
|
13
|
-
if (error) {
|
|
14
|
-
reject(error);
|
|
15
|
-
return;
|
|
16
|
-
}
|
|
17
|
-
resolve();
|
|
18
|
-
});
|
|
19
|
-
});
|
|
20
|
-
}
|
|
21
|
-
function isResponse(value) {
|
|
22
|
-
return typeof value === "object"
|
|
23
|
-
&& value !== null
|
|
24
|
-
&& !Array.isArray(value)
|
|
25
|
-
&& value.jsonrpc === "2.0"
|
|
26
|
-
&& Object.hasOwn(value, "id");
|
|
27
|
-
}
|
|
28
|
-
function isNotification(value) {
|
|
29
|
-
return typeof value === "object"
|
|
30
|
-
&& value !== null
|
|
31
|
-
&& !Array.isArray(value)
|
|
32
|
-
&& value.jsonrpc === "2.0"
|
|
33
|
-
&& typeof value.method === "string"
|
|
34
|
-
&& !Object.hasOwn(value, "id");
|
|
35
|
-
}
|
|
36
|
-
async function parseJsonResponse(response) {
|
|
37
|
-
const text = await response.text();
|
|
38
|
-
if (!text.trim()) {
|
|
39
|
-
return undefined;
|
|
40
|
-
}
|
|
41
|
-
return JSON.parse(text);
|
|
42
|
-
}
|
|
43
|
-
export function createAcpStdioClient(options) {
|
|
44
|
-
const idPrefix = options.idPrefix?.trim() || "acp";
|
|
45
|
-
let sequence = 0;
|
|
46
|
-
let closed = false;
|
|
47
|
-
const pending = new Map();
|
|
48
|
-
const listeners = new Set();
|
|
49
|
-
const reader = createInterface({
|
|
50
|
-
input: options.output,
|
|
51
|
-
crlfDelay: Infinity,
|
|
52
|
-
});
|
|
53
|
-
const completed = (async () => {
|
|
54
|
-
try {
|
|
55
|
-
for await (const line of reader) {
|
|
56
|
-
const trimmed = line.trim();
|
|
57
|
-
if (!trimmed) {
|
|
58
|
-
continue;
|
|
59
|
-
}
|
|
60
|
-
let parsed;
|
|
61
|
-
try {
|
|
62
|
-
parsed = JSON.parse(trimmed);
|
|
63
|
-
}
|
|
64
|
-
catch {
|
|
65
|
-
continue;
|
|
66
|
-
}
|
|
67
|
-
if (isNotification(parsed)) {
|
|
68
|
-
for (const listener of Array.from(listeners)) {
|
|
69
|
-
try {
|
|
70
|
-
listener(parsed);
|
|
71
|
-
}
|
|
72
|
-
catch {
|
|
73
|
-
// Ignore listener failures so one subscriber cannot tear down the transport.
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
continue;
|
|
77
|
-
}
|
|
78
|
-
if (!isResponse(parsed)) {
|
|
79
|
-
continue;
|
|
80
|
-
}
|
|
81
|
-
const request = pending.get(parsed.id);
|
|
82
|
-
if (!request) {
|
|
83
|
-
continue;
|
|
84
|
-
}
|
|
85
|
-
pending.delete(parsed.id);
|
|
86
|
-
if ("error" in parsed) {
|
|
87
|
-
request.reject(new AcpClientError(parsed.error));
|
|
88
|
-
}
|
|
89
|
-
else {
|
|
90
|
-
request.resolve(parsed.result);
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
catch (error) {
|
|
95
|
-
closed = true;
|
|
96
|
-
for (const request of pending.values()) {
|
|
97
|
-
request.reject(error);
|
|
98
|
-
}
|
|
99
|
-
pending.clear();
|
|
100
|
-
}
|
|
101
|
-
})();
|
|
102
|
-
return {
|
|
103
|
-
async request(method, params) {
|
|
104
|
-
if (closed) {
|
|
105
|
-
throw new Error("ACP stdio client is closed.");
|
|
106
|
-
}
|
|
107
|
-
const id = `${idPrefix}-${++sequence}`;
|
|
108
|
-
const payload = {
|
|
109
|
-
jsonrpc: "2.0",
|
|
110
|
-
id,
|
|
111
|
-
method,
|
|
112
|
-
...(params === undefined ? {} : { params }),
|
|
113
|
-
};
|
|
114
|
-
const result = new Promise((resolve, reject) => {
|
|
115
|
-
pending.set(id, { resolve, reject });
|
|
116
|
-
});
|
|
117
|
-
try {
|
|
118
|
-
await writeJsonLine(options.input, payload);
|
|
119
|
-
}
|
|
120
|
-
catch (error) {
|
|
121
|
-
pending.delete(id);
|
|
122
|
-
throw error;
|
|
123
|
-
}
|
|
124
|
-
return result;
|
|
125
|
-
},
|
|
126
|
-
async notify(method, params) {
|
|
127
|
-
if (closed) {
|
|
128
|
-
throw new Error("ACP stdio client is closed.");
|
|
129
|
-
}
|
|
130
|
-
await writeJsonLine(options.input, {
|
|
131
|
-
jsonrpc: "2.0",
|
|
132
|
-
method,
|
|
133
|
-
...(params === undefined ? {} : { params }),
|
|
134
|
-
});
|
|
135
|
-
},
|
|
136
|
-
subscribe(listener) {
|
|
137
|
-
listeners.add(listener);
|
|
138
|
-
return () => {
|
|
139
|
-
listeners.delete(listener);
|
|
140
|
-
};
|
|
141
|
-
},
|
|
142
|
-
async close() {
|
|
143
|
-
closed = true;
|
|
144
|
-
reader.close();
|
|
145
|
-
if (typeof options.output.destroy === "function") {
|
|
146
|
-
options.output.destroy();
|
|
147
|
-
}
|
|
148
|
-
if (typeof options.input.end === "function") {
|
|
149
|
-
options.input.end();
|
|
150
|
-
}
|
|
151
|
-
await completed.catch(() => undefined);
|
|
152
|
-
for (const request of pending.values()) {
|
|
153
|
-
request.reject(new Error("ACP stdio client closed before response."));
|
|
154
|
-
}
|
|
155
|
-
pending.clear();
|
|
156
|
-
},
|
|
157
|
-
};
|
|
158
|
-
}
|
|
159
|
-
export function createAcpHttpClient(options) {
|
|
160
|
-
let closed = false;
|
|
161
|
-
let requestSequence = 0;
|
|
162
|
-
const listeners = new Set();
|
|
163
|
-
const eventsAbort = new AbortController();
|
|
164
|
-
let resolveReady;
|
|
165
|
-
let rejectReady;
|
|
166
|
-
const ready = new Promise((resolve, reject) => {
|
|
167
|
-
resolveReady = resolve;
|
|
168
|
-
rejectReady = reject;
|
|
169
|
-
});
|
|
170
|
-
const completed = (async () => {
|
|
171
|
-
const response = await fetch(options.eventsUrl, {
|
|
172
|
-
method: "GET",
|
|
173
|
-
headers: {
|
|
174
|
-
accept: "text/event-stream",
|
|
175
|
-
},
|
|
176
|
-
signal: eventsAbort.signal,
|
|
177
|
-
});
|
|
178
|
-
if (!response.ok || !response.body) {
|
|
179
|
-
throw new Error(`ACP HTTP events request failed with status ${response.status}.`);
|
|
180
|
-
}
|
|
181
|
-
resolveReady?.();
|
|
182
|
-
resolveReady = undefined;
|
|
183
|
-
rejectReady = undefined;
|
|
184
|
-
const reader = response.body
|
|
185
|
-
.pipeThrough(new TextDecoderStream())
|
|
186
|
-
.getReader();
|
|
187
|
-
let buffer = "";
|
|
188
|
-
try {
|
|
189
|
-
for (;;) {
|
|
190
|
-
const { done, value } = await reader.read();
|
|
191
|
-
if (done) {
|
|
192
|
-
break;
|
|
193
|
-
}
|
|
194
|
-
buffer += value;
|
|
195
|
-
let splitIndex = buffer.indexOf("\n\n");
|
|
196
|
-
while (splitIndex >= 0) {
|
|
197
|
-
const frame = buffer.slice(0, splitIndex);
|
|
198
|
-
buffer = buffer.slice(splitIndex + 2);
|
|
199
|
-
const dataLines = frame
|
|
200
|
-
.split("\n")
|
|
201
|
-
.map((line) => line.trimEnd())
|
|
202
|
-
.filter((line) => line.startsWith("data:"))
|
|
203
|
-
.map((line) => line.slice("data:".length).trim());
|
|
204
|
-
if (dataLines.length > 0) {
|
|
205
|
-
try {
|
|
206
|
-
const parsed = JSON.parse(dataLines.join("\n"));
|
|
207
|
-
if (isNotification(parsed)) {
|
|
208
|
-
for (const listener of Array.from(listeners)) {
|
|
209
|
-
try {
|
|
210
|
-
listener(parsed);
|
|
211
|
-
}
|
|
212
|
-
catch {
|
|
213
|
-
// Ignore malformed listener failures.
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
catch {
|
|
219
|
-
// Ignore malformed SSE payloads.
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
splitIndex = buffer.indexOf("\n\n");
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
finally {
|
|
227
|
-
reader.releaseLock();
|
|
228
|
-
}
|
|
229
|
-
})().catch((error) => {
|
|
230
|
-
rejectReady?.(error);
|
|
231
|
-
resolveReady = undefined;
|
|
232
|
-
rejectReady = undefined;
|
|
233
|
-
if (!closed && !(error instanceof DOMException && error.name === "AbortError")) {
|
|
234
|
-
throw error;
|
|
235
|
-
}
|
|
236
|
-
});
|
|
237
|
-
return {
|
|
238
|
-
async request(method, params) {
|
|
239
|
-
if (closed) {
|
|
240
|
-
throw new Error("ACP http client is closed.");
|
|
241
|
-
}
|
|
242
|
-
await ready;
|
|
243
|
-
const response = await fetch(options.rpcUrl, {
|
|
244
|
-
method: "POST",
|
|
245
|
-
headers: {
|
|
246
|
-
"content-type": "application/json",
|
|
247
|
-
},
|
|
248
|
-
body: JSON.stringify({
|
|
249
|
-
jsonrpc: "2.0",
|
|
250
|
-
id: `acp-http-${++requestSequence}`,
|
|
251
|
-
method,
|
|
252
|
-
...(params === undefined ? {} : { params }),
|
|
253
|
-
}),
|
|
254
|
-
});
|
|
255
|
-
const parsed = await parseJsonResponse(response);
|
|
256
|
-
if (!isResponse(parsed)) {
|
|
257
|
-
throw new Error("ACP http client received a non-JSON-RPC response.");
|
|
258
|
-
}
|
|
259
|
-
if ("error" in parsed) {
|
|
260
|
-
throw new AcpClientError(parsed.error);
|
|
261
|
-
}
|
|
262
|
-
return parsed.result;
|
|
263
|
-
},
|
|
264
|
-
async notify(method, params) {
|
|
265
|
-
if (closed) {
|
|
266
|
-
throw new Error("ACP http client is closed.");
|
|
267
|
-
}
|
|
268
|
-
await ready;
|
|
269
|
-
await fetch(options.rpcUrl, {
|
|
270
|
-
method: "POST",
|
|
271
|
-
headers: {
|
|
272
|
-
"content-type": "application/json",
|
|
273
|
-
},
|
|
274
|
-
body: JSON.stringify({
|
|
275
|
-
jsonrpc: "2.0",
|
|
276
|
-
method,
|
|
277
|
-
...(params === undefined ? {} : { params }),
|
|
278
|
-
}),
|
|
279
|
-
});
|
|
280
|
-
},
|
|
281
|
-
subscribe(listener) {
|
|
282
|
-
listeners.add(listener);
|
|
283
|
-
return () => {
|
|
284
|
-
listeners.delete(listener);
|
|
285
|
-
};
|
|
286
|
-
},
|
|
287
|
-
async close() {
|
|
288
|
-
closed = true;
|
|
289
|
-
eventsAbort.abort();
|
|
290
|
-
await completed.catch(() => undefined);
|
|
291
|
-
listeners.clear();
|
|
292
|
-
},
|
|
293
|
-
};
|
|
294
|
-
}
|
|
1
|
+
import{createInterface as E}from"node:readline";class m extends Error{error;constructor(n){super(n.message),this.error=n,this.name="AcpClientError"}}function A(e,n){return new Promise((p,c)=>{e.write(`${JSON.stringify(n)}
|
|
2
|
+
`,i=>{if(i){c(i);return}p()})})}function b(e){return typeof e=="object"&&e!==null&&!Array.isArray(e)&&e.jsonrpc==="2.0"&&Object.hasOwn(e,"id")}function j(e){return typeof e=="object"&&e!==null&&!Array.isArray(e)&&e.jsonrpc==="2.0"&&typeof e.method=="string"&&!Object.hasOwn(e,"id")}async function S(e){const n=await e.text();if(n.trim())return JSON.parse(n)}function x(e){const n=e.idPrefix?.trim()||"acp";let p=0,c=!1;const i=new Map,d=new Set,l=E({input:e.output,crlfDelay:1/0}),w=(async()=>{try{for await(const o of l){const t=o.trim();if(!t)continue;let r;try{r=JSON.parse(t)}catch{continue}if(j(r)){for(const a of Array.from(d))try{a(r)}catch{}continue}if(!b(r))continue;const s=i.get(r.id);s&&(i.delete(r.id),"error"in r?s.reject(new m(r.error)):s.resolve(r.result))}}catch(o){c=!0;for(const t of i.values())t.reject(o);i.clear()}})();return{async request(o,t){if(c)throw new Error("ACP stdio client is closed.");const r=`${n}-${++p}`,s={jsonrpc:"2.0",id:r,method:o,...t===void 0?{}:{params:t}},a=new Promise((y,u)=>{i.set(r,{resolve:y,reject:u})});try{await A(e.input,s)}catch(y){throw i.delete(r),y}return a},async notify(o,t){if(c)throw new Error("ACP stdio client is closed.");await A(e.input,{jsonrpc:"2.0",method:o,...t===void 0?{}:{params:t}})},subscribe(o){return d.add(o),()=>{d.delete(o)}},async close(){c=!0,l.close(),typeof e.output.destroy=="function"&&e.output.destroy(),typeof e.input.end=="function"&&e.input.end(),await w.catch(()=>{});for(const o of i.values())o.reject(new Error("ACP stdio client closed before response."));i.clear()}}}function C(e){let n=!1,p=0;const c=new Set,i=new AbortController;let d,l;const w=new Promise((t,r)=>{d=t,l=r}),o=(async()=>{const t=await fetch(e.eventsUrl,{method:"GET",headers:{accept:"text/event-stream"},signal:i.signal});if(!t.ok||!t.body)throw new Error(`ACP HTTP events request failed with status ${t.status}.`);d?.(),d=void 0,l=void 0;const r=t.body.pipeThrough(new TextDecoderStream).getReader();let s="";try{for(;;){const{done:a,value:y}=await r.read();if(a)break;s+=y;let u=s.indexOf(`
|
|
3
|
+
|
|
4
|
+
`);for(;u>=0;){const O=s.slice(0,u);s=s.slice(u+2);const h=O.split(`
|
|
5
|
+
`).map(f=>f.trimEnd()).filter(f=>f.startsWith("data:")).map(f=>f.slice(5).trim());if(h.length>0)try{const f=JSON.parse(h.join(`
|
|
6
|
+
`));if(j(f))for(const P of Array.from(c))try{P(f)}catch{}}catch{}u=s.indexOf(`
|
|
7
|
+
|
|
8
|
+
`)}}}finally{r.releaseLock()}})().catch(t=>{if(l?.(t),d=void 0,l=void 0,!n&&!(t instanceof DOMException&&t.name==="AbortError"))throw t});return{async request(t,r){if(n)throw new Error("ACP http client is closed.");await w;const s=await fetch(e.rpcUrl,{method:"POST",headers:{"content-type":"application/json"},body:JSON.stringify({jsonrpc:"2.0",id:`acp-http-${++p}`,method:t,...r===void 0?{}:{params:r}})}),a=await S(s);if(!b(a))throw new Error("ACP http client received a non-JSON-RPC response.");if("error"in a)throw new m(a.error);return a.result},async notify(t,r){if(n)throw new Error("ACP http client is closed.");await w,await fetch(e.rpcUrl,{method:"POST",headers:{"content-type":"application/json"},body:JSON.stringify({jsonrpc:"2.0",method:t,...r===void 0?{}:{params:r}})})},subscribe(t){return c.add(t),()=>{c.delete(t)}},async close(){n=!0,i.abort(),await o.catch(()=>{}),c.clear()}}}export{C as createAcpHttpClient,x as createAcpStdioClient};
|
|
@@ -1,218 +1 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { applyRequestStreamItemToSnapshot, createInitialRequestEventSnapshot, toRequestDataEvents, } from "../../projections/request-events.js";
|
|
3
|
-
function toEvent(notification) {
|
|
4
|
-
return notification.params.event;
|
|
5
|
-
}
|
|
6
|
-
function isRuntimeEventNotification(notification) {
|
|
7
|
-
return notification.method === "events.runtime";
|
|
8
|
-
}
|
|
9
|
-
function isStreamNotification(notification) {
|
|
10
|
-
return notification.method === "requests.stream";
|
|
11
|
-
}
|
|
12
|
-
export class AcpHarnessClient {
|
|
13
|
-
transport;
|
|
14
|
-
streamSequence = 0;
|
|
15
|
-
constructor(transport) {
|
|
16
|
-
this.transport = transport;
|
|
17
|
-
}
|
|
18
|
-
request(options) {
|
|
19
|
-
if (this.hasStreamingListeners(options)) {
|
|
20
|
-
return this.requestWithStreamingListeners(options);
|
|
21
|
-
}
|
|
22
|
-
return this.transport.request("requests.submit", options);
|
|
23
|
-
}
|
|
24
|
-
hasStreamingListeners(options) {
|
|
25
|
-
return Boolean(("eventListener" in options && options.eventListener)
|
|
26
|
-
|| ("dataListener" in options && options.dataListener)
|
|
27
|
-
|| options.listeners);
|
|
28
|
-
}
|
|
29
|
-
async *streamRequestInternal(options) {
|
|
30
|
-
const streamId = `harness-stream-${++this.streamSequence}`;
|
|
31
|
-
const queued = [];
|
|
32
|
-
let notify;
|
|
33
|
-
let settled = false;
|
|
34
|
-
let streamedResult = false;
|
|
35
|
-
let responseResolved = false;
|
|
36
|
-
let failure;
|
|
37
|
-
let fallbackTimer;
|
|
38
|
-
const waitForItem = () => new Promise((resolve) => {
|
|
39
|
-
notify = resolve;
|
|
40
|
-
});
|
|
41
|
-
const push = (item) => {
|
|
42
|
-
queued.push(item);
|
|
43
|
-
notify?.();
|
|
44
|
-
notify = undefined;
|
|
45
|
-
};
|
|
46
|
-
const settle = () => {
|
|
47
|
-
if (settled) {
|
|
48
|
-
return;
|
|
49
|
-
}
|
|
50
|
-
settled = true;
|
|
51
|
-
notify?.();
|
|
52
|
-
notify = undefined;
|
|
53
|
-
};
|
|
54
|
-
const unsubscribe = this.transport.subscribe((notification) => {
|
|
55
|
-
if (!isStreamNotification(notification) || notification.params.streamId !== streamId) {
|
|
56
|
-
return;
|
|
57
|
-
}
|
|
58
|
-
if (notification.params.item.type === "result") {
|
|
59
|
-
streamedResult = true;
|
|
60
|
-
if (fallbackTimer) {
|
|
61
|
-
clearTimeout(fallbackTimer);
|
|
62
|
-
fallbackTimer = undefined;
|
|
63
|
-
}
|
|
64
|
-
if (responseResolved) {
|
|
65
|
-
settle();
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
push(notification.params.item);
|
|
69
|
-
});
|
|
70
|
-
const resultPromise = this.transport.request("requests.submit", {
|
|
71
|
-
...options,
|
|
72
|
-
streamId,
|
|
73
|
-
listeners: undefined,
|
|
74
|
-
eventListener: undefined,
|
|
75
|
-
dataListener: undefined,
|
|
76
|
-
});
|
|
77
|
-
resultPromise
|
|
78
|
-
.then((result) => {
|
|
79
|
-
responseResolved = true;
|
|
80
|
-
if (streamedResult) {
|
|
81
|
-
settle();
|
|
82
|
-
return;
|
|
83
|
-
}
|
|
84
|
-
fallbackTimer = setTimeout(() => {
|
|
85
|
-
push({
|
|
86
|
-
type: "result",
|
|
87
|
-
result,
|
|
88
|
-
});
|
|
89
|
-
settle();
|
|
90
|
-
}, 50);
|
|
91
|
-
})
|
|
92
|
-
.catch((error) => {
|
|
93
|
-
failure = error;
|
|
94
|
-
settle();
|
|
95
|
-
})
|
|
96
|
-
.finally(() => undefined);
|
|
97
|
-
try {
|
|
98
|
-
while (!settled || queued.length > 0) {
|
|
99
|
-
if (queued.length === 0) {
|
|
100
|
-
await waitForItem();
|
|
101
|
-
continue;
|
|
102
|
-
}
|
|
103
|
-
const item = queued.shift();
|
|
104
|
-
if (item) {
|
|
105
|
-
yield item;
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
if (failure) {
|
|
109
|
-
throw failure;
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
finally {
|
|
113
|
-
if (fallbackTimer) {
|
|
114
|
-
clearTimeout(fallbackTimer);
|
|
115
|
-
}
|
|
116
|
-
unsubscribe();
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
async requestWithStreamingListeners(options) {
|
|
120
|
-
const legacyListeners = options.listeners;
|
|
121
|
-
const eventListener = "eventListener" in options ? options.eventListener : undefined;
|
|
122
|
-
const dataListener = "dataListener" in options ? options.dataListener : undefined;
|
|
123
|
-
let snapshot = createInitialRequestEventSnapshot();
|
|
124
|
-
let finalResult;
|
|
125
|
-
for await (const item of this.streamRequestInternal(options)) {
|
|
126
|
-
snapshot = applyRequestStreamItemToSnapshot(snapshot, item);
|
|
127
|
-
if (item.type === "event") {
|
|
128
|
-
await legacyListeners?.onEvent?.(item.event);
|
|
129
|
-
}
|
|
130
|
-
else if (item.type === "upstream-event") {
|
|
131
|
-
await legacyListeners?.onUpstreamEvent?.(item.event);
|
|
132
|
-
if (item.surfaceItem) {
|
|
133
|
-
await legacyListeners?.onTraceItem?.({
|
|
134
|
-
sessionId: item.sessionId,
|
|
135
|
-
requestId: item.requestId,
|
|
136
|
-
surfaceItem: item.surfaceItem,
|
|
137
|
-
event: item.event,
|
|
138
|
-
});
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
else if (item.type === "plan-state") {
|
|
142
|
-
await legacyListeners?.onPlanState?.(item.planState);
|
|
143
|
-
}
|
|
144
|
-
else if (item.type === "result") {
|
|
145
|
-
finalResult = item.result;
|
|
146
|
-
}
|
|
147
|
-
for (const dataEvent of toRequestDataEvents(item)) {
|
|
148
|
-
await dataListener?.(dataEvent);
|
|
149
|
-
}
|
|
150
|
-
await eventListener?.(snapshot);
|
|
151
|
-
}
|
|
152
|
-
if (!finalResult) {
|
|
153
|
-
throw new Error("ACP streaming request completed without a terminal result.");
|
|
154
|
-
}
|
|
155
|
-
return finalResult;
|
|
156
|
-
}
|
|
157
|
-
resolveApproval(options) {
|
|
158
|
-
return this.transport.request("approvals.resolve", options);
|
|
159
|
-
}
|
|
160
|
-
cancelRequest(options) {
|
|
161
|
-
return this.transport.request("requests.cancel", options);
|
|
162
|
-
}
|
|
163
|
-
subscribe(listener) {
|
|
164
|
-
return this.transport.subscribe((notification) => {
|
|
165
|
-
if (isRuntimeEventNotification(notification)) {
|
|
166
|
-
void listener(toEvent(notification));
|
|
167
|
-
}
|
|
168
|
-
});
|
|
169
|
-
}
|
|
170
|
-
listSessions(filter) {
|
|
171
|
-
return this.transport.request("sessions.list", filter);
|
|
172
|
-
}
|
|
173
|
-
listSessionSummaries(filter) {
|
|
174
|
-
return this.transport.request("sessions.summaries", filter);
|
|
175
|
-
}
|
|
176
|
-
listRequests(filter) {
|
|
177
|
-
return this.transport.request("requests.list", filter);
|
|
178
|
-
}
|
|
179
|
-
getSession(sessionId) {
|
|
180
|
-
return this.transport.request("sessions.get", { sessionId });
|
|
181
|
-
}
|
|
182
|
-
getRequest(requestId) {
|
|
183
|
-
return this.transport.request("requests.get", { requestId });
|
|
184
|
-
}
|
|
185
|
-
listApprovals(filter) {
|
|
186
|
-
return this.transport.request("approvals.list", filter);
|
|
187
|
-
}
|
|
188
|
-
getApproval(approvalId) {
|
|
189
|
-
return this.transport.request("approvals.get", { approvalId });
|
|
190
|
-
}
|
|
191
|
-
getRequestPlanState(input) {
|
|
192
|
-
return this.transport.request("requests.plan.get", input);
|
|
193
|
-
}
|
|
194
|
-
listRequestEvents(input) {
|
|
195
|
-
return this.transport.request("events.list", input);
|
|
196
|
-
}
|
|
197
|
-
listRequestTraceItems(input) {
|
|
198
|
-
return this.transport.request("requests.trace.list", input);
|
|
199
|
-
}
|
|
200
|
-
getHealth() {
|
|
201
|
-
return this.transport.request("runtime.health");
|
|
202
|
-
}
|
|
203
|
-
getOperatorOverview(options) {
|
|
204
|
-
return this.transport.request("runtime.overview", options);
|
|
205
|
-
}
|
|
206
|
-
stop() {
|
|
207
|
-
return this.transport.close();
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
export function createAcpHarnessClient(transport) {
|
|
211
|
-
return new AcpHarnessClient(transport);
|
|
212
|
-
}
|
|
213
|
-
export function createAcpStdioHarnessClient(options) {
|
|
214
|
-
return new AcpHarnessClient(createAcpStdioClient(options));
|
|
215
|
-
}
|
|
216
|
-
export function createAcpHttpHarnessClient(options) {
|
|
217
|
-
return new AcpHarnessClient(createAcpHttpClient(options));
|
|
218
|
-
}
|
|
1
|
+
import{createAcpHttpClient as d,createAcpStdioClient as v}from"./client.js";import{applyRequestStreamItemToSnapshot as w,createInitialRequestEventSnapshot as S,toRequestDataEvents as g}from"../../projections/request-events.js";function L(r){return r.params.event}function I(r){return r.method==="events.runtime"}function y(r){return r.method==="requests.stream"}class m{transport;streamSequence=0;constructor(e){this.transport=e}request(e){return this.hasStreamingListeners(e)?this.requestWithStreamingListeners(e):this.transport.request("requests.submit",e)}hasStreamingListeners(e){return!!("eventListener"in e&&e.eventListener||"dataListener"in e&&e.dataListener||e.listeners)}async*streamRequestInternal(e){const n=`harness-stream-${++this.streamSequence}`,u=[];let i,a=!1,l=!1,t=!1,p,o;const h=()=>new Promise(s=>{i=s}),f=s=>{u.push(s),i?.(),i=void 0},c=()=>{a||(a=!0,i?.(),i=void 0)},q=this.transport.subscribe(s=>{!y(s)||s.params.streamId!==n||(s.params.item.type==="result"&&(l=!0,o&&(clearTimeout(o),o=void 0),t&&c()),f(s.params.item))});this.transport.request("requests.submit",{...e,streamId:n,listeners:void 0,eventListener:void 0,dataListener:void 0}).then(s=>{if(t=!0,l){c();return}o=setTimeout(()=>{f({type:"result",result:s}),c()},50)}).catch(s=>{p=s,c()}).finally(()=>{});try{for(;!a||u.length>0;){if(u.length===0){await h();continue}const s=u.shift();s&&(yield s)}if(p)throw p}finally{o&&clearTimeout(o),q()}}async requestWithStreamingListeners(e){const n=e.listeners,u="eventListener"in e?e.eventListener:void 0,i="dataListener"in e?e.dataListener:void 0;let a=S(),l;for await(const t of this.streamRequestInternal(e)){a=w(a,t),t.type==="event"?await n?.onEvent?.(t.event):t.type==="upstream-event"?(await n?.onUpstreamEvent?.(t.event),t.surfaceItem&&await n?.onTraceItem?.({sessionId:t.sessionId,requestId:t.requestId,surfaceItem:t.surfaceItem,event:t.event})):t.type==="plan-state"?await n?.onPlanState?.(t.planState):t.type==="result"&&(l=t.result);for(const p of g(t))await i?.(p);await u?.(a)}if(!l)throw new Error("ACP streaming request completed without a terminal result.");return l}resolveApproval(e){return this.transport.request("approvals.resolve",e)}cancelRequest(e){return this.transport.request("requests.cancel",e)}subscribe(e){return this.transport.subscribe(n=>{I(n)&&e(L(n))})}listSessions(e){return this.transport.request("sessions.list",e)}listSessionSummaries(e){return this.transport.request("sessions.summaries",e)}listRequests(e){return this.transport.request("requests.list",e)}getSession(e){return this.transport.request("sessions.get",{sessionId:e})}getRequest(e){return this.transport.request("requests.get",{requestId:e})}listApprovals(e){return this.transport.request("approvals.list",e)}getApproval(e){return this.transport.request("approvals.get",{approvalId:e})}getRequestPlanState(e){return this.transport.request("requests.plan.get",e)}listRequestEvents(e){return this.transport.request("events.list",e)}listRequestTraceItems(e){return this.transport.request("requests.trace.list",e)}getHealth(){return this.transport.request("runtime.health")}getOperatorOverview(e){return this.transport.request("runtime.overview",e)}stop(){return this.transport.close()}}function E(r){return new m(r)}function C(r){return new m(v(r))}function H(r){return new m(d(r))}export{m as AcpHarnessClient,E as createAcpHarnessClient,H as createAcpHttpHarnessClient,C as createAcpStdioHarnessClient};
|
|
@@ -1,130 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
}
|
|
7
|
-
function writeJson(response, statusCode, payload) {
|
|
8
|
-
response.statusCode = statusCode;
|
|
9
|
-
response.setHeader("content-type", "application/json; charset=utf-8");
|
|
10
|
-
response.end(JSON.stringify(payload));
|
|
11
|
-
}
|
|
12
|
-
function readRequestBody(request) {
|
|
13
|
-
return new Promise((resolve, reject) => {
|
|
14
|
-
const chunks = [];
|
|
15
|
-
request.on("data", (chunk) => {
|
|
16
|
-
chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
|
|
17
|
-
});
|
|
18
|
-
request.on("end", () => {
|
|
19
|
-
resolve(Buffer.concat(chunks).toString("utf8"));
|
|
20
|
-
});
|
|
21
|
-
request.on("error", reject);
|
|
22
|
-
});
|
|
23
|
-
}
|
|
24
|
-
export async function serveAcpOverHttp(runtime, options = {}) {
|
|
25
|
-
const hostname = options.hostname?.trim() || "127.0.0.1";
|
|
26
|
-
const port = typeof options.port === "number" && Number.isFinite(options.port) ? options.port : 0;
|
|
27
|
-
const rpcPath = normalizePath(options.rpcPath, "/rpc");
|
|
28
|
-
const eventsPath = normalizePath(options.eventsPath, "/events");
|
|
29
|
-
const server = createAcpServer(runtime);
|
|
30
|
-
const eventSubscribers = new Set();
|
|
31
|
-
const unsubscribe = server.subscribe((notification) => {
|
|
32
|
-
const payload = `data: ${JSON.stringify(notification)}\n\n`;
|
|
33
|
-
for (const response of Array.from(eventSubscribers)) {
|
|
34
|
-
if (response.writableEnded || response.destroyed) {
|
|
35
|
-
eventSubscribers.delete(response);
|
|
36
|
-
continue;
|
|
37
|
-
}
|
|
38
|
-
response.write(payload);
|
|
39
|
-
}
|
|
40
|
-
});
|
|
41
|
-
const httpServer = createServer(async (request, response) => {
|
|
42
|
-
try {
|
|
43
|
-
const requestUrl = new URL(request.url ?? "/", `http://${hostname}`);
|
|
44
|
-
if (request.method === "GET" && requestUrl.pathname === eventsPath) {
|
|
45
|
-
response.statusCode = 200;
|
|
46
|
-
response.setHeader("content-type", "text/event-stream; charset=utf-8");
|
|
47
|
-
response.setHeader("cache-control", "no-cache, no-transform");
|
|
48
|
-
response.setHeader("connection", "keep-alive");
|
|
49
|
-
response.write(": connected\n\n");
|
|
50
|
-
eventSubscribers.add(response);
|
|
51
|
-
request.on("close", () => {
|
|
52
|
-
eventSubscribers.delete(response);
|
|
53
|
-
response.end();
|
|
54
|
-
});
|
|
55
|
-
return;
|
|
56
|
-
}
|
|
57
|
-
if (request.method === "POST" && requestUrl.pathname === rpcPath) {
|
|
58
|
-
const body = await readRequestBody(request);
|
|
59
|
-
let payload;
|
|
60
|
-
try {
|
|
61
|
-
payload = JSON.parse(body);
|
|
62
|
-
}
|
|
63
|
-
catch {
|
|
64
|
-
writeJson(response, 400, {
|
|
65
|
-
jsonrpc: "2.0",
|
|
66
|
-
id: null,
|
|
67
|
-
error: {
|
|
68
|
-
code: -32700,
|
|
69
|
-
message: "Invalid JSON payload.",
|
|
70
|
-
},
|
|
71
|
-
});
|
|
72
|
-
return;
|
|
73
|
-
}
|
|
74
|
-
const rpcResponse = await server.handle(payload);
|
|
75
|
-
if (!rpcResponse) {
|
|
76
|
-
response.statusCode = 204;
|
|
77
|
-
response.end();
|
|
78
|
-
return;
|
|
79
|
-
}
|
|
80
|
-
writeJson(response, 200, rpcResponse);
|
|
81
|
-
return;
|
|
82
|
-
}
|
|
83
|
-
writeJson(response, 404, {
|
|
84
|
-
error: "Not Found",
|
|
85
|
-
rpcPath,
|
|
86
|
-
eventsPath,
|
|
87
|
-
});
|
|
88
|
-
}
|
|
89
|
-
catch (error) {
|
|
90
|
-
writeJson(response, 500, {
|
|
91
|
-
error: error instanceof Error ? error.message : "ACP HTTP transport failed.",
|
|
92
|
-
});
|
|
93
|
-
}
|
|
94
|
-
});
|
|
95
|
-
const completed = new Promise((resolve, reject) => {
|
|
96
|
-
httpServer.once("close", resolve);
|
|
97
|
-
httpServer.once("error", reject);
|
|
98
|
-
});
|
|
99
|
-
await new Promise((resolve, reject) => {
|
|
100
|
-
httpServer.listen(port, hostname, () => resolve());
|
|
101
|
-
httpServer.once("error", reject);
|
|
102
|
-
});
|
|
103
|
-
const address = httpServer.address();
|
|
104
|
-
const resolvedPort = typeof address === "object" && address ? address.port : port;
|
|
105
|
-
return {
|
|
106
|
-
hostname,
|
|
107
|
-
port: resolvedPort,
|
|
108
|
-
rpcPath,
|
|
109
|
-
eventsPath,
|
|
110
|
-
rpcUrl: `http://${hostname}:${resolvedPort}${rpcPath}`,
|
|
111
|
-
eventsUrl: `http://${hostname}:${resolvedPort}${eventsPath}`,
|
|
112
|
-
completed,
|
|
113
|
-
close: async () => {
|
|
114
|
-
unsubscribe();
|
|
115
|
-
for (const response of Array.from(eventSubscribers)) {
|
|
116
|
-
response.end();
|
|
117
|
-
}
|
|
118
|
-
eventSubscribers.clear();
|
|
119
|
-
await new Promise((resolve, reject) => {
|
|
120
|
-
httpServer.close((error) => {
|
|
121
|
-
if (error) {
|
|
122
|
-
reject(error);
|
|
123
|
-
return;
|
|
124
|
-
}
|
|
125
|
-
resolve();
|
|
126
|
-
});
|
|
127
|
-
});
|
|
128
|
-
},
|
|
129
|
-
};
|
|
130
|
-
}
|
|
1
|
+
import{createServer as S}from"node:http";import{createAcpServer as $}from"../../acp.js";function w(r,c){const n=typeof r=="string"&&r.trim().length>0?r.trim():c;return n.startsWith("/")?n:`/${n}`}function m(r,c,n){r.statusCode=c,r.setHeader("content-type","application/json; charset=utf-8"),r.end(JSON.stringify(n))}function g(r){return new Promise((c,n)=>{const d=[];r.on("data",a=>{d.push(Buffer.isBuffer(a)?a:Buffer.from(a))}),r.on("end",()=>{c(Buffer.concat(d).toString("utf8"))}),r.on("error",n)})}async function O(r,c={}){const n=c.hostname?.trim()||"127.0.0.1",d=typeof c.port=="number"&&Number.isFinite(c.port)?c.port:0,a=w(c.rpcPath,"/rpc"),f=w(c.eventsPath,"/events"),h=$(r),s=new Set,v=h.subscribe(e=>{const t=`data: ${JSON.stringify(e)}
|
|
2
|
+
|
|
3
|
+
`;for(const o of Array.from(s)){if(o.writableEnded||o.destroyed){s.delete(o);continue}o.write(t)}}),i=S(async(e,t)=>{try{const o=new URL(e.url??"/",`http://${n}`);if(e.method==="GET"&&o.pathname===f){t.statusCode=200,t.setHeader("content-type","text/event-stream; charset=utf-8"),t.setHeader("cache-control","no-cache, no-transform"),t.setHeader("connection","keep-alive"),t.write(`: connected
|
|
4
|
+
|
|
5
|
+
`),s.add(t),e.on("close",()=>{s.delete(t),t.end()});return}if(e.method==="POST"&&o.pathname===a){const b=await g(e);let p;try{p=JSON.parse(b)}catch{m(t,400,{jsonrpc:"2.0",id:null,error:{code:-32700,message:"Invalid JSON payload."}});return}const y=await h.handle(p);if(!y){t.statusCode=204,t.end();return}m(t,200,y);return}m(t,404,{error:"Not Found",rpcPath:a,eventsPath:f})}catch(o){m(t,500,{error:o instanceof Error?o.message:"ACP HTTP transport failed."})}}),P=new Promise((e,t)=>{i.once("close",e),i.once("error",t)});await new Promise((e,t)=>{i.listen(d,n,()=>e()),i.once("error",t)});const l=i.address(),u=typeof l=="object"&&l?l.port:d;return{hostname:n,port:u,rpcPath:a,eventsPath:f,rpcUrl:`http://${n}:${u}${a}`,eventsUrl:`http://${n}:${u}${f}`,completed:P,close:async()=>{v();for(const e of Array.from(s))e.end();s.clear(),await new Promise((e,t)=>{i.close(o=>{if(o){t(o);return}e()})})}}}export{O as serveAcpOverHttp};
|