@botbotgo/agent-harness 0.0.309 → 0.0.310
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 +14 -0
- package/README.zh.md +14 -0
- package/dist/acp.d.ts +1 -116
- package/dist/acp.js +1 -310
- package/dist/api.d.ts +1 -1
- package/dist/api.js +1 -1
- package/dist/cli/chat-interactive.d.ts +24 -0
- package/dist/cli/chat-interactive.js +244 -0
- package/dist/cli/chat-rendering.d.ts +9 -0
- package/dist/cli/chat-rendering.js +102 -0
- package/dist/cli/chat-stream.d.ts +23 -0
- package/dist/cli/chat-stream.js +330 -0
- package/dist/cli/chat-ui.d.ts +20 -0
- package/dist/cli/chat-ui.js +198 -0
- package/dist/cli/chat-workspace.d.ts +15 -0
- package/dist/cli/chat-workspace.js +205 -0
- package/dist/cli/main.d.ts +52 -0
- package/dist/cli/main.js +323 -0
- package/dist/cli/managed-service-commands.d.ts +23 -0
- package/dist/cli/managed-service-commands.js +63 -0
- package/dist/cli/managed-service.d.ts +27 -0
- package/dist/cli/managed-service.js +61 -0
- package/dist/cli/options-init-chat.d.ts +16 -0
- package/dist/cli/options-init-chat.js +108 -0
- package/dist/cli/options-runtime.d.ts +27 -0
- package/dist/cli/options-runtime.js +158 -0
- package/dist/cli/options-serve.d.ts +24 -0
- package/dist/cli/options-serve.js +166 -0
- package/dist/cli/options.d.ts +5 -0
- package/dist/cli/options.js +47 -0
- package/dist/cli/process-guards.d.ts +14 -0
- package/dist/cli/process-guards.js +139 -0
- package/dist/cli/request-tree.d.ts +12 -0
- package/dist/cli/request-tree.js +296 -0
- package/dist/cli/runtime-commands.d.ts +15 -0
- package/dist/cli/runtime-commands.js +247 -0
- package/dist/cli/runtime-output.d.ts +5 -0
- package/dist/cli/runtime-output.js +124 -0
- package/dist/cli/server-commands.d.ts +36 -0
- package/dist/cli/server-commands.js +250 -0
- package/dist/cli/workspace.d.ts +6 -0
- package/dist/cli/workspace.js +71 -0
- package/dist/cli.d.ts +1 -78
- package/dist/cli.js +2 -3024
- package/dist/client/acp.d.ts +1 -50
- package/dist/client/acp.js +1 -219
- package/dist/client/in-process.d.ts +5 -5
- package/dist/client/index.d.ts +2 -2
- package/dist/client/index.js +1 -1
- package/dist/contracts/runtime-evaluation.d.ts +103 -0
- package/dist/contracts/runtime-evaluation.js +1 -0
- package/dist/contracts/runtime-memory.d.ts +162 -0
- package/dist/contracts/runtime-memory.js +1 -0
- package/dist/contracts/runtime-observability.d.ts +248 -0
- package/dist/contracts/runtime-observability.js +1 -0
- package/dist/contracts/runtime-requests.d.ts +342 -0
- package/dist/contracts/runtime-requests.js +1 -0
- package/dist/contracts/runtime-scheduling.d.ts +146 -0
- package/dist/contracts/runtime-scheduling.js +1 -0
- package/dist/contracts/runtime.d.ts +5 -1042
- package/dist/contracts/runtime.js +27 -1
- package/dist/flow/build-flow-graph.js +4 -875
- package/dist/flow/flow-graph-normalization.d.ts +56 -0
- package/dist/flow/flow-graph-normalization.js +214 -0
- package/dist/flow/flow-graph-runtime.d.ts +8 -0
- package/dist/flow/flow-graph-runtime.js +107 -0
- package/dist/flow/flow-graph-upstream.d.ts +18 -0
- package/dist/flow/flow-graph-upstream.js +498 -0
- package/dist/flow/types.d.ts +1 -1
- package/dist/index.d.ts +3 -3
- package/dist/index.js +1 -1
- package/dist/init-project.d.ts +1 -12
- package/dist/init-project.js +1 -651
- package/dist/{procedural → knowledge/procedural}/manager.d.ts +3 -3
- package/dist/{procedural → knowledge/procedural}/manager.js +6 -6
- package/dist/mcp.d.ts +2 -76
- package/dist/mcp.js +2 -428
- package/dist/package-version.d.ts +1 -1
- package/dist/package-version.js +1 -1
- package/dist/persistence/file-store.js +1 -1
- package/dist/persistence/sqlite-runtime.d.ts +19 -0
- package/dist/persistence/sqlite-runtime.js +86 -0
- package/dist/persistence/sqlite-store.js +11 -99
- package/dist/{request-events.d.ts → projections/request-events.d.ts} +1 -1
- package/dist/{upstream-events.js → projections/upstream-events.js} +1 -1
- package/dist/protocol/a2a/http-discovery.d.ts +39 -0
- package/dist/protocol/a2a/http-discovery.js +178 -0
- package/dist/protocol/a2a/http-rpc.d.ts +28 -0
- package/dist/protocol/a2a/http-rpc.js +623 -0
- package/dist/protocol/a2a/http.d.ts +72 -1
- package/dist/protocol/a2a/http.js +14 -1124
- package/dist/protocol/a2a/task-state.d.ts +29 -0
- package/dist/protocol/a2a/task-state.js +317 -0
- package/dist/protocol/acp/client.js +1 -1
- package/dist/protocol/acp/harness-client.d.ts +50 -0
- package/dist/protocol/acp/harness-client.js +219 -0
- package/dist/protocol/acp/server.d.ts +116 -0
- package/dist/protocol/acp/server.js +310 -0
- package/dist/protocol/ag-ui/http.js +1 -1
- package/dist/protocol/mcp/server.d.ts +76 -0
- package/dist/protocol/mcp/server.js +428 -0
- package/dist/resource/backend/workspace-scoped-backend.d.ts +40 -0
- package/dist/resource/backend/workspace-scoped-backend.js +296 -0
- package/dist/resource/mcp/tool-support.d.ts +35 -0
- package/dist/resource/mcp/tool-support.js +296 -0
- package/dist/resource/mcp-tool-support.d.ts +2 -35
- package/dist/resource/mcp-tool-support.js +2 -296
- package/dist/resource/providers/resource-provider.d.ts +22 -0
- package/dist/resource/providers/resource-provider.js +215 -0
- package/dist/resource/resource-impl.d.ts +3 -33
- package/dist/resource/resource-impl.js +2 -808
- package/dist/resource/resource-types.d.ts +33 -0
- package/dist/resource/resource-types.js +1 -0
- package/dist/resource/tools/function-tool-resolver.d.ts +2 -0
- package/dist/resource/tools/function-tool-resolver.js +306 -0
- package/dist/runtime/adapter/middleware-assembly.js +1 -1
- package/dist/runtime/adapter/model/invocation-request.js +2 -2
- package/dist/runtime/adapter/model/message-assembly.js +1 -1
- package/dist/runtime/agent-runtime-adapter.d.ts +3 -63
- package/dist/runtime/agent-runtime-adapter.js +5 -233
- package/dist/runtime/agent-runtime-assembly.d.ts +67 -0
- package/dist/runtime/agent-runtime-assembly.js +211 -0
- package/dist/runtime/harness/background-runtime.d.ts +1 -1
- package/dist/runtime/harness/events/event-sink.js +1 -1
- package/dist/runtime/harness/events/runtime-event-operations.d.ts +1 -1
- package/dist/runtime/harness/events/streaming.js +1 -1
- package/dist/runtime/harness/public-shapes.d.ts +43 -0
- package/dist/runtime/harness/public-shapes.js +186 -0
- package/dist/runtime/harness/run/inspection.js +2 -2
- package/dist/runtime/harness/run/resources.js +1 -1
- package/dist/runtime/harness/run/surface-semantics.js +1 -1
- package/dist/runtime/harness/system/inventory.d.ts +1 -1
- package/dist/runtime/harness/system/inventory.js +2 -2
- package/dist/runtime/harness/system/policy-engine.js +1 -1
- package/dist/runtime/harness/system/runtime-memory-manager.js +1 -1
- package/dist/runtime/harness/system/skill-requirements.d.ts +1 -1
- package/dist/runtime/harness/system/skill-requirements.js +1 -1
- package/dist/runtime/harness.d.ts +2 -2
- package/dist/runtime/harness.js +7 -191
- package/dist/runtime/maintenance/checkpoint-maintenance.js +1 -1
- package/dist/runtime/maintenance/runtime-record-maintenance.js +1 -1
- package/dist/runtime/parsing/output-content.d.ts +11 -0
- package/dist/runtime/parsing/output-content.js +442 -0
- package/dist/runtime/parsing/output-parsing.d.ts +3 -29
- package/dist/runtime/parsing/output-parsing.js +3 -806
- package/dist/runtime/parsing/output-recovery.d.ts +14 -0
- package/dist/runtime/parsing/output-recovery.js +288 -0
- package/dist/runtime/parsing/output-tool-args.d.ts +4 -0
- package/dist/runtime/parsing/output-tool-args.js +120 -0
- package/dist/runtime/support/runtime-factories.js +1 -1
- package/dist/scaffold/init-project.d.ts +12 -0
- package/dist/scaffold/init-project.js +651 -0
- package/dist/{extensions.d.ts → tooling/extensions.d.ts} +1 -1
- package/dist/{extensions.js → tooling/extensions.js} +3 -3
- package/dist/{tool-modules.d.ts → tooling/module-loader.d.ts} +1 -1
- package/dist/{tool-modules.js → tooling/module-loader.js} +2 -2
- package/dist/workspace/agent-binding-compiler.js +2 -2
- package/dist/workspace/compile.js +2 -2
- package/dist/workspace/object-loader-paths.d.ts +11 -0
- package/dist/workspace/object-loader-paths.js +75 -0
- package/dist/workspace/object-loader-readers.d.ts +21 -0
- package/dist/workspace/object-loader-readers.js +187 -0
- package/dist/workspace/object-loader.d.ts +0 -1
- package/dist/workspace/object-loader.js +6 -260
- package/dist/workspace/resource-compilers.js +1 -1
- package/dist/workspace/support/discovery.js +1 -1
- package/package.json +1 -1
- package/dist/runtime/adapter/index.d.ts +0 -13
- package/dist/runtime/adapter/index.js +0 -13
- package/dist/runtime/harness/index.d.ts +0 -19
- package/dist/runtime/harness/index.js +0 -19
- package/dist/runtime/maintenance/index.d.ts +0 -4
- package/dist/runtime/maintenance/index.js +0 -4
- package/dist/runtime/parsing/index.d.ts +0 -2
- package/dist/runtime/parsing/index.js +0 -2
- package/dist/runtime/support/index.d.ts +0 -4
- package/dist/runtime/support/index.js +0 -4
- package/dist/workspace/support/index.d.ts +0 -2
- package/dist/workspace/support/index.js +0 -2
- /package/dist/{procedural → knowledge/procedural}/config.d.ts +0 -0
- /package/dist/{procedural → knowledge/procedural}/config.js +0 -0
- /package/dist/{procedural → knowledge/procedural}/index.d.ts +0 -0
- /package/dist/{procedural → knowledge/procedural}/index.js +0 -0
- /package/dist/{presentation.d.ts → projections/presentation.d.ts} +0 -0
- /package/dist/{presentation.js → projections/presentation.js} +0 -0
- /package/dist/{request-events.js → projections/request-events.js} +0 -0
- /package/dist/{upstream-events.d.ts → projections/upstream-events.d.ts} +0 -0
- /package/dist/runtime/{support → env}/runtime-env.d.ts +0 -0
- /package/dist/runtime/{support → env}/runtime-env.js +0 -0
- /package/dist/runtime/{support → layout}/runtime-layout.d.ts +0 -0
- /package/dist/runtime/{support → layout}/runtime-layout.js +0 -0
- /package/dist/runtime/{support → prompts}/runtime-prompts.d.ts +0 -0
- /package/dist/runtime/{support → prompts}/runtime-prompts.js +0 -0
- /package/dist/runtime/{support → skills}/skill-metadata.d.ts +0 -0
- /package/dist/runtime/{support → skills}/skill-metadata.js +0 -0
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import { readFileSync, readdirSync } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import YAML from "yaml";
|
|
4
|
+
import { interpolateEnvPlaceholders } from "../workspace/yaml-object-reader.js";
|
|
5
|
+
import { frameworkCliWorkspaceRoot, hasCliConfigYaml, resolveCliConfigRoot, } from "./workspace.js";
|
|
6
|
+
export function normalizeChatCommand(line) {
|
|
7
|
+
const trimmed = line.trim();
|
|
8
|
+
if (!trimmed.startsWith("/")) {
|
|
9
|
+
return null;
|
|
10
|
+
}
|
|
11
|
+
const [name, ...rest] = trimmed.slice(1).split(/\s+/);
|
|
12
|
+
return {
|
|
13
|
+
name,
|
|
14
|
+
arg: rest.length > 0 ? rest.join(" ") : undefined,
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
function asRecord(value) {
|
|
18
|
+
return typeof value === "object" && value !== null ? value : undefined;
|
|
19
|
+
}
|
|
20
|
+
function readCliConfigObjects(workspaceRoot) {
|
|
21
|
+
const roots = [
|
|
22
|
+
resolveCliConfigRoot(frameworkCliWorkspaceRoot()),
|
|
23
|
+
resolveCliConfigRoot(workspaceRoot),
|
|
24
|
+
];
|
|
25
|
+
const merged = new Map();
|
|
26
|
+
for (const configRoot of roots) {
|
|
27
|
+
if (!hasCliConfigYaml(configRoot)) {
|
|
28
|
+
continue;
|
|
29
|
+
}
|
|
30
|
+
const files = [];
|
|
31
|
+
const pending = [configRoot];
|
|
32
|
+
while (pending.length > 0) {
|
|
33
|
+
const current = pending.pop();
|
|
34
|
+
for (const entry of readdirSync(current, { withFileTypes: true })) {
|
|
35
|
+
const entryPath = path.join(current, entry.name);
|
|
36
|
+
if (entry.isDirectory()) {
|
|
37
|
+
if (entry.name === "node_modules") {
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
pending.push(entryPath);
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
if (entry.isFile() && /\.ya?ml$/i.test(entry.name)) {
|
|
44
|
+
files.push(entryPath);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
for (const filePath of files.sort()) {
|
|
49
|
+
const content = readFileSync(filePath, "utf8");
|
|
50
|
+
for (const document of YAML.parseAllDocuments(content)) {
|
|
51
|
+
const resolved = interpolateEnvPlaceholders(document.toJSON(), filePath);
|
|
52
|
+
const typed = asRecord(resolved);
|
|
53
|
+
if (!typed) {
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
if (typed.kind === "Models" && Array.isArray(typed.spec)) {
|
|
57
|
+
for (const item of typed.spec) {
|
|
58
|
+
const record = asRecord(item);
|
|
59
|
+
if (!record) {
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
const id = typeof record.name === "string" ? record.name : record.id;
|
|
63
|
+
if (typeof id !== "string" || !id) {
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
merged.set(`Model/${id}`, {
|
|
67
|
+
...record,
|
|
68
|
+
kind: "Model",
|
|
69
|
+
id,
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
const metadata = asRecord(typed.metadata);
|
|
75
|
+
const spec = asRecord(typed.spec);
|
|
76
|
+
if (typeof typed.kind === "string" && spec) {
|
|
77
|
+
const id = typeof metadata?.name === "string" ? metadata.name : typed.id;
|
|
78
|
+
if (typeof id !== "string" || !id) {
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
81
|
+
merged.set(`${typed.kind}/${id}`, {
|
|
82
|
+
...spec,
|
|
83
|
+
kind: typed.kind,
|
|
84
|
+
id,
|
|
85
|
+
});
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
const kind = typeof typed.kind === "string" ? typed.kind : undefined;
|
|
89
|
+
const id = typeof typed.id === "string" ? typed.id : undefined;
|
|
90
|
+
if (!kind || !id) {
|
|
91
|
+
continue;
|
|
92
|
+
}
|
|
93
|
+
merged.set(`${kind}/${id}`, typed);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
return Array.from(merged.values());
|
|
98
|
+
}
|
|
99
|
+
function getWorkspaceDefaultAgentId(workspaceRoot) {
|
|
100
|
+
const runtime = readCliConfigObjects(workspaceRoot).find((item) => item.kind === "Runtime" && item.id === "default");
|
|
101
|
+
const routing = asRecord(runtime?.routing);
|
|
102
|
+
return typeof routing?.defaultAgentId === "string" && routing.defaultAgentId.trim().length > 0 ? routing.defaultAgentId.trim() : undefined;
|
|
103
|
+
}
|
|
104
|
+
function getAgentModelRef(workspaceRoot, agentId) {
|
|
105
|
+
const agent = readCliConfigObjects(workspaceRoot).find((item) => item.kind === "Agent" && item.id === agentId);
|
|
106
|
+
return typeof agent?.modelRef === "string" && agent.modelRef.trim().length > 0 ? agent.modelRef.trim() : undefined;
|
|
107
|
+
}
|
|
108
|
+
function getModelInfo(workspaceRoot, modelRef) {
|
|
109
|
+
const modelName = modelRef.startsWith("model/") ? modelRef.slice("model/".length) : modelRef;
|
|
110
|
+
const model = readCliConfigObjects(workspaceRoot).find((item) => item.kind === "Model" && item.id === modelName);
|
|
111
|
+
if (!model) {
|
|
112
|
+
return undefined;
|
|
113
|
+
}
|
|
114
|
+
return {
|
|
115
|
+
provider: typeof model.provider === "string" ? model.provider : undefined,
|
|
116
|
+
model: typeof model.model === "string" ? model.model : undefined,
|
|
117
|
+
baseUrl: typeof model.baseUrl === "string" ? model.baseUrl : undefined,
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
export function readChatWorkspaceModelInfo(workspaceRoot, agentId) {
|
|
121
|
+
try {
|
|
122
|
+
const resolvedAgentId = agentId ?? getWorkspaceDefaultAgentId(workspaceRoot);
|
|
123
|
+
if (!resolvedAgentId) {
|
|
124
|
+
return getModelInfo(workspaceRoot, "model/default");
|
|
125
|
+
}
|
|
126
|
+
const modelRef = getAgentModelRef(workspaceRoot, resolvedAgentId) ?? "model/default";
|
|
127
|
+
return getModelInfo(workspaceRoot, modelRef);
|
|
128
|
+
}
|
|
129
|
+
catch {
|
|
130
|
+
return undefined;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
function renderProviderFailureHint(modelInfo) {
|
|
134
|
+
if (!modelInfo?.provider) {
|
|
135
|
+
return undefined;
|
|
136
|
+
}
|
|
137
|
+
if (modelInfo.provider === "ollama") {
|
|
138
|
+
const modelText = modelInfo.model ? ` and ensure \`${modelInfo.model}\` is available` : "";
|
|
139
|
+
return `Hint: start Ollama${modelInfo.baseUrl ? ` at ${modelInfo.baseUrl}` : ""}${modelText}. Example: \`ollama serve\`${modelInfo.model ? ` and \`ollama pull ${modelInfo.model}\`` : ""}.`;
|
|
140
|
+
}
|
|
141
|
+
if (modelInfo.provider === "openai") {
|
|
142
|
+
return "Hint: verify network access and that `OPENAI_API_KEY` is set for this shell.";
|
|
143
|
+
}
|
|
144
|
+
if (modelInfo.provider === "anthropic") {
|
|
145
|
+
return "Hint: verify network access and that `ANTHROPIC_API_KEY` is set for this shell.";
|
|
146
|
+
}
|
|
147
|
+
if (modelInfo.provider === "google" || modelInfo.provider === "google-genai" || modelInfo.provider === "gemini") {
|
|
148
|
+
return "Hint: verify network access and that the configured Google API key is available in this shell.";
|
|
149
|
+
}
|
|
150
|
+
if (modelInfo.provider === "openai-compatible") {
|
|
151
|
+
return `Hint: verify the configured endpoint${modelInfo.baseUrl ? ` (${modelInfo.baseUrl})` : ""} and the required API key for that provider.`;
|
|
152
|
+
}
|
|
153
|
+
return undefined;
|
|
154
|
+
}
|
|
155
|
+
export function renderChatRuntimeFailure(output, modelInfo) {
|
|
156
|
+
const trimmed = output.trim();
|
|
157
|
+
if (!trimmed.startsWith("runtime_error=")) {
|
|
158
|
+
return output;
|
|
159
|
+
}
|
|
160
|
+
const normalized = trimmed.toLowerCase();
|
|
161
|
+
if (!normalized.includes("fetch failed") &&
|
|
162
|
+
!normalized.includes("connection error") &&
|
|
163
|
+
!normalized.includes("timed out") &&
|
|
164
|
+
!normalized.includes("404 page not found")) {
|
|
165
|
+
return output;
|
|
166
|
+
}
|
|
167
|
+
const lines = [trimmed];
|
|
168
|
+
if (modelInfo?.provider || modelInfo?.model) {
|
|
169
|
+
lines.push(`provider=${modelInfo?.provider ?? "unknown"}${modelInfo?.model ? ` model=${modelInfo.model}` : ""}`);
|
|
170
|
+
}
|
|
171
|
+
if (modelInfo?.baseUrl) {
|
|
172
|
+
lines.push(`endpoint=${modelInfo.baseUrl}`);
|
|
173
|
+
}
|
|
174
|
+
if (normalized.includes("404 page not found") && modelInfo?.provider === "ollama") {
|
|
175
|
+
lines.push("Hint: the configured endpoint responded, but it does not look like an Ollama API route. Check that `baseUrl` points at the Ollama server root and that no other service is bound to this port.");
|
|
176
|
+
}
|
|
177
|
+
const hint = renderProviderFailureHint(modelInfo);
|
|
178
|
+
if (hint) {
|
|
179
|
+
lines.push(hint);
|
|
180
|
+
}
|
|
181
|
+
return lines.join("\n");
|
|
182
|
+
}
|
|
183
|
+
export async function probeChatWorkspace(input) {
|
|
184
|
+
const modelInfo = readChatWorkspaceModelInfo(input.workspaceRoot, input.agentId);
|
|
185
|
+
if (!modelInfo?.provider || modelInfo.provider !== "ollama" || !modelInfo.baseUrl) {
|
|
186
|
+
return undefined;
|
|
187
|
+
}
|
|
188
|
+
try {
|
|
189
|
+
const response = await fetch(new URL("/api/tags", modelInfo.baseUrl), {
|
|
190
|
+
method: "GET",
|
|
191
|
+
headers: { accept: "application/json" },
|
|
192
|
+
});
|
|
193
|
+
if (response.ok) {
|
|
194
|
+
return undefined;
|
|
195
|
+
}
|
|
196
|
+
if (response.status === 404) {
|
|
197
|
+
return renderChatRuntimeFailure("runtime_error=404 page not found", modelInfo);
|
|
198
|
+
}
|
|
199
|
+
return renderChatRuntimeFailure(`runtime_error=HTTP ${response.status} ${response.statusText}`.trim(), modelInfo);
|
|
200
|
+
}
|
|
201
|
+
catch (error) {
|
|
202
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
203
|
+
return renderChatRuntimeFailure(`runtime_error=${message}`, modelInfo);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { createInterface as createReadlineInterface } from "node:readline";
|
|
3
|
+
import type { Readable } from "node:stream";
|
|
4
|
+
import { createAgentHarness } from "../api.js";
|
|
5
|
+
import { type HarnessClient } from "../client.js";
|
|
6
|
+
import { serveA2aOverHttp } from "../protocol/a2a/http.js";
|
|
7
|
+
import { serveAgUiOverHttp } from "../protocol/ag-ui/http.js";
|
|
8
|
+
import { serveAcpOverHttp } from "../protocol/acp/http.js";
|
|
9
|
+
import { serveAcpOverStdio } from "../protocol/acp/stdio.js";
|
|
10
|
+
import { serveRuntimeMcpOverStdio, serveRuntimeMcpOverStreamableHttp } from "../mcp.js";
|
|
11
|
+
export { renderChatBanner, renderChatPromptLine } from "./chat-ui.js";
|
|
12
|
+
export { probeChatWorkspace, renderChatRuntimeFailure } from "./chat-workspace.js";
|
|
13
|
+
export { installChatSubprocessExitGuard, isChatServerNoiseLine } from "./process-guards.js";
|
|
14
|
+
type CliIo = {
|
|
15
|
+
cwd?: string;
|
|
16
|
+
stdin?: Readable;
|
|
17
|
+
stdout?: (message: string) => void;
|
|
18
|
+
stderr?: (message: string) => void;
|
|
19
|
+
};
|
|
20
|
+
type CliDeps = {
|
|
21
|
+
createChatClient?: (input: {
|
|
22
|
+
workspaceRoot: string;
|
|
23
|
+
transport: "stdio" | "http";
|
|
24
|
+
hostname?: string;
|
|
25
|
+
port?: number;
|
|
26
|
+
stderr?: (message: string) => void;
|
|
27
|
+
}) => Promise<HarnessClient>;
|
|
28
|
+
probeChatWorkspace?: (input: {
|
|
29
|
+
workspaceRoot: string;
|
|
30
|
+
agentId?: string;
|
|
31
|
+
}) => Promise<string | undefined>;
|
|
32
|
+
createReadlineInterface?: typeof createReadlineInterface;
|
|
33
|
+
createAgentHarness?: typeof createAgentHarness;
|
|
34
|
+
serveA2aOverHttp?: typeof serveA2aOverHttp;
|
|
35
|
+
serveAgUiOverHttp?: typeof serveAgUiOverHttp;
|
|
36
|
+
serveAcpOverHttp?: typeof serveAcpOverHttp;
|
|
37
|
+
serveAcpOverStdio?: typeof serveAcpOverStdio;
|
|
38
|
+
serveRuntimeMcpOverStdio?: typeof serveRuntimeMcpOverStdio;
|
|
39
|
+
serveRuntimeMcpOverStreamableHttp?: typeof serveRuntimeMcpOverStreamableHttp;
|
|
40
|
+
spawnManagedCliProcess?: (input: {
|
|
41
|
+
args: string[];
|
|
42
|
+
cwd: string;
|
|
43
|
+
stdoutPath: string;
|
|
44
|
+
stderrPath: string;
|
|
45
|
+
}) => Promise<{
|
|
46
|
+
pid?: number;
|
|
47
|
+
}>;
|
|
48
|
+
isManagedProcessRunning?: (pid: number) => boolean;
|
|
49
|
+
signalManagedProcess?: (pid: number, signal?: NodeJS.Signals) => void;
|
|
50
|
+
};
|
|
51
|
+
export declare function runCli(argv: string[], io?: CliIo, deps?: CliDeps): Promise<number>;
|
|
52
|
+
export declare function resolveInvokedCliHref(argvPath?: string): string;
|
package/dist/cli/main.js
ADDED
|
@@ -0,0 +1,323 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { existsSync, realpathSync } from "node:fs";
|
|
3
|
+
import { createInterface as createReadlineInterface } from "node:readline";
|
|
4
|
+
import { Writable } from "node:stream";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
import { pathToFileURL } from "node:url";
|
|
7
|
+
import { createAgentHarness } from "../api.js";
|
|
8
|
+
import { installCliEmitterListenerGuard, installCliStderrNoiseFilter, installCliWarningFilter, installCliWriteListenerGuard, } from "./process-guards.js";
|
|
9
|
+
import { createAgentHarnessClient, createAcpHttpHarnessClient, createInProcessHarnessClient, } from "../client.js";
|
|
10
|
+
import { initProject } from "../init-project.js";
|
|
11
|
+
import { serveA2aOverHttp } from "../protocol/a2a/http.js";
|
|
12
|
+
import { serveAgUiOverHttp } from "../protocol/ag-ui/http.js";
|
|
13
|
+
import { serveAcpOverHttp } from "../protocol/acp/http.js";
|
|
14
|
+
import { serveAcpOverStdio } from "../protocol/acp/stdio.js";
|
|
15
|
+
import { serveRuntimeMcpOverStdio, serveRuntimeMcpOverStreamableHttp } from "../mcp.js";
|
|
16
|
+
import { renderChatBanner } from "./chat-ui.js";
|
|
17
|
+
import { runInteractiveChatLoop } from "./chat-interactive.js";
|
|
18
|
+
import { streamChatMessage } from "./chat-stream.js";
|
|
19
|
+
import { probeChatWorkspace, readChatWorkspaceModelInfo, } from "./chat-workspace.js";
|
|
20
|
+
import { handleA2aCommand, handleAcpCommand, handleAgUiCommand, handleMcpCommand } from "./server-commands.js";
|
|
21
|
+
import { handleRuntimeCommand } from "./runtime-commands.js";
|
|
22
|
+
import { defaultIsManagedProcessRunning, defaultSignalManagedProcess, defaultSpawnManagedCliProcess, sleep, } from "./managed-service.js";
|
|
23
|
+
import { isTopLevelCliCommand, parseChatOptions, parseInitOptions, renderUsage, } from "./options.js";
|
|
24
|
+
import { resolveCliWorkspaceRoot, validateCliWorkspaceRoot, } from "./workspace.js";
|
|
25
|
+
export { renderChatBanner, renderChatPromptLine } from "./chat-ui.js";
|
|
26
|
+
export { probeChatWorkspace, renderChatRuntimeFailure } from "./chat-workspace.js";
|
|
27
|
+
export { installChatSubprocessExitGuard, isChatServerNoiseLine } from "./process-guards.js";
|
|
28
|
+
function isObject(value) {
|
|
29
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
30
|
+
}
|
|
31
|
+
function formatTimestamp(value) {
|
|
32
|
+
return typeof value === "string" && value.trim().length > 0 ? value.trim() : null;
|
|
33
|
+
}
|
|
34
|
+
function renderPlanResultSummary(result) {
|
|
35
|
+
if (typeof result === "string" && result.trim().length > 0) {
|
|
36
|
+
return result.trim();
|
|
37
|
+
}
|
|
38
|
+
if (!isObject(result)) {
|
|
39
|
+
return undefined;
|
|
40
|
+
}
|
|
41
|
+
const summary = typeof result.summary === "string" && result.summary.trim().length > 0
|
|
42
|
+
? result.summary.trim()
|
|
43
|
+
: typeof result.message === "string" && result.message.trim().length > 0
|
|
44
|
+
? result.message.trim()
|
|
45
|
+
: undefined;
|
|
46
|
+
if (summary) {
|
|
47
|
+
return summary;
|
|
48
|
+
}
|
|
49
|
+
const serialized = JSON.stringify(result);
|
|
50
|
+
return serialized && serialized !== "{}" ? serialized : undefined;
|
|
51
|
+
}
|
|
52
|
+
function renderChatPlanState(planState) {
|
|
53
|
+
const lines = [`[plan v${planState.version}] ${planState.summary.total} todo(s)`];
|
|
54
|
+
if (planState.summary.total === 0) {
|
|
55
|
+
lines.push("No todos tracked yet.");
|
|
56
|
+
return `${lines.join("\n")}\n`;
|
|
57
|
+
}
|
|
58
|
+
for (const [index, item] of planState.items.entries()) {
|
|
59
|
+
lines.push(`${index + 1}. [${item.status}] ${item.content}`);
|
|
60
|
+
const resultSummary = renderPlanResultSummary(item.result);
|
|
61
|
+
if (resultSummary) {
|
|
62
|
+
lines.push(` result: ${resultSummary}`);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return `${lines.join("\n")}\n`;
|
|
66
|
+
}
|
|
67
|
+
export async function runCli(argv, io = {}, deps = {}) {
|
|
68
|
+
const cwd = io.cwd ?? process.cwd();
|
|
69
|
+
const stdin = io.stdin ?? process.stdin;
|
|
70
|
+
const stdout = io.stdout ?? ((message) => process.stdout.write(message));
|
|
71
|
+
const stderr = io.stderr ?? ((message) => process.stderr.write(message));
|
|
72
|
+
const stdoutStream = io.stdout === undefined ? process.stdout : undefined;
|
|
73
|
+
const stderrStream = io.stderr === undefined ? process.stderr : undefined;
|
|
74
|
+
const [command, projectName, ...rest] = argv;
|
|
75
|
+
const probeWorkspace = deps.probeChatWorkspace ?? probeChatWorkspace;
|
|
76
|
+
const createChatLineReader = deps.createReadlineInterface ?? createReadlineInterface;
|
|
77
|
+
const createHarness = deps.createAgentHarness ?? createAgentHarness;
|
|
78
|
+
const serveA2a = deps.serveA2aOverHttp ?? serveA2aOverHttp;
|
|
79
|
+
const serveAgUi = deps.serveAgUiOverHttp ?? serveAgUiOverHttp;
|
|
80
|
+
const serveAcpHttp = deps.serveAcpOverHttp ?? serveAcpOverHttp;
|
|
81
|
+
const serveAcp = deps.serveAcpOverStdio ?? serveAcpOverStdio;
|
|
82
|
+
const serveRuntimeMcp = deps.serveRuntimeMcpOverStdio ?? serveRuntimeMcpOverStdio;
|
|
83
|
+
const serveRuntimeMcpStreamableHttp = deps.serveRuntimeMcpOverStreamableHttp ?? serveRuntimeMcpOverStreamableHttp;
|
|
84
|
+
const spawnManagedCliProcess = deps.spawnManagedCliProcess ?? defaultSpawnManagedCliProcess;
|
|
85
|
+
const isManagedProcessRunning = deps.isManagedProcessRunning ?? defaultIsManagedProcessRunning;
|
|
86
|
+
const signalManagedProcess = deps.signalManagedProcess ?? defaultSignalManagedProcess;
|
|
87
|
+
const createChatClient = deps.createChatClient ?? (async (input) => {
|
|
88
|
+
if (input.transport === "http") {
|
|
89
|
+
return createAcpHttpHarnessClient({
|
|
90
|
+
rpcUrl: `http://${input.hostname ?? "127.0.0.1"}:${input.port ?? 8787}/rpc`,
|
|
91
|
+
eventsUrl: `http://${input.hostname ?? "127.0.0.1"}:${input.port ?? 8787}/events`,
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
if (deps.createAgentHarness) {
|
|
95
|
+
const runtime = await createHarness(input.workspaceRoot);
|
|
96
|
+
return createInProcessHarnessClient(runtime);
|
|
97
|
+
}
|
|
98
|
+
return createAgentHarnessClient(input.workspaceRoot);
|
|
99
|
+
});
|
|
100
|
+
const executeChat = async (parsed) => {
|
|
101
|
+
const workspacePath = resolveCliWorkspaceRoot(cwd, parsed.workspaceRoot);
|
|
102
|
+
const workspaceError = validateCliWorkspaceRoot(workspacePath, parsed.workspaceRoot);
|
|
103
|
+
if (workspaceError) {
|
|
104
|
+
stderr(`${workspaceError}\n`);
|
|
105
|
+
return 1;
|
|
106
|
+
}
|
|
107
|
+
const workspaceModelInfo = readChatWorkspaceModelInfo(workspacePath, parsed.agentId);
|
|
108
|
+
const chatStdinStream = stdin;
|
|
109
|
+
let client;
|
|
110
|
+
let restoreWriteListenerGuard;
|
|
111
|
+
let restoreEmitterListenerGuard;
|
|
112
|
+
let restoreStderrNoiseFilter;
|
|
113
|
+
let restoreWarningFilter;
|
|
114
|
+
try {
|
|
115
|
+
restoreEmitterListenerGuard = installCliEmitterListenerGuard();
|
|
116
|
+
restoreStderrNoiseFilter = installCliStderrNoiseFilter(io.stderr === undefined);
|
|
117
|
+
restoreWarningFilter = installCliWarningFilter();
|
|
118
|
+
client = await createChatClient({
|
|
119
|
+
workspaceRoot: workspacePath,
|
|
120
|
+
transport: parsed.transport,
|
|
121
|
+
hostname: parsed.hostname,
|
|
122
|
+
port: parsed.port,
|
|
123
|
+
stderr,
|
|
124
|
+
});
|
|
125
|
+
restoreWriteListenerGuard = installCliWriteListenerGuard([stdoutStream, stderrStream]);
|
|
126
|
+
let activeAgentId = parsed.agentId;
|
|
127
|
+
let activeSessionId = parsed.sessionId;
|
|
128
|
+
let latestRequestId;
|
|
129
|
+
const useColor = false;
|
|
130
|
+
const preflightWarning = parsed.message
|
|
131
|
+
? undefined
|
|
132
|
+
: await probeWorkspace({
|
|
133
|
+
workspaceRoot: workspacePath,
|
|
134
|
+
agentId: activeAgentId,
|
|
135
|
+
});
|
|
136
|
+
if (parsed.message) {
|
|
137
|
+
await streamChatMessage({
|
|
138
|
+
client,
|
|
139
|
+
stdout,
|
|
140
|
+
stdoutStream,
|
|
141
|
+
stderr,
|
|
142
|
+
stderrStream,
|
|
143
|
+
agentId: activeAgentId,
|
|
144
|
+
sessionId: activeSessionId,
|
|
145
|
+
message: parsed.message,
|
|
146
|
+
modelInfo: workspaceModelInfo,
|
|
147
|
+
requestEvents: parsed.requestEvents,
|
|
148
|
+
liveRequestTree: parsed.requestEvents && chatStdinStream.isTTY === true,
|
|
149
|
+
colorRequestTree: useColor,
|
|
150
|
+
showToolResults: parsed.requestEvents,
|
|
151
|
+
showRunningState: false,
|
|
152
|
+
});
|
|
153
|
+
await client.stop();
|
|
154
|
+
restoreWriteListenerGuard?.();
|
|
155
|
+
restoreEmitterListenerGuard?.();
|
|
156
|
+
restoreStderrNoiseFilter?.();
|
|
157
|
+
restoreWarningFilter?.();
|
|
158
|
+
return 0;
|
|
159
|
+
}
|
|
160
|
+
stdout(renderChatBanner({
|
|
161
|
+
workspacePath,
|
|
162
|
+
transport: parsed.transport,
|
|
163
|
+
agentId: activeAgentId,
|
|
164
|
+
sessionId: activeSessionId,
|
|
165
|
+
color: useColor,
|
|
166
|
+
}));
|
|
167
|
+
if (preflightWarning) {
|
|
168
|
+
stdout(`${preflightWarning}\n\n`);
|
|
169
|
+
}
|
|
170
|
+
const stdoutSink = new Writable({
|
|
171
|
+
decodeStrings: false,
|
|
172
|
+
write(chunk, _encoding, callback) {
|
|
173
|
+
stdout(typeof chunk === "string" ? chunk : String(chunk));
|
|
174
|
+
callback();
|
|
175
|
+
},
|
|
176
|
+
});
|
|
177
|
+
const lineReader = createChatLineReader({
|
|
178
|
+
input: stdin,
|
|
179
|
+
output: stdoutSink,
|
|
180
|
+
crlfDelay: Infinity,
|
|
181
|
+
terminal: chatStdinStream.isTTY === true,
|
|
182
|
+
});
|
|
183
|
+
const interactiveState = await runInteractiveChatLoop({
|
|
184
|
+
client,
|
|
185
|
+
lineReader,
|
|
186
|
+
stdout,
|
|
187
|
+
stderr,
|
|
188
|
+
stdoutStream,
|
|
189
|
+
stderrStream,
|
|
190
|
+
workspacePath,
|
|
191
|
+
workspaceModelInfo,
|
|
192
|
+
requestEvents: parsed.requestEvents,
|
|
193
|
+
chatIsTty: chatStdinStream.isTTY === true,
|
|
194
|
+
useColor,
|
|
195
|
+
agentFilter: parsed.agentId,
|
|
196
|
+
activeAgentId,
|
|
197
|
+
activeSessionId,
|
|
198
|
+
});
|
|
199
|
+
activeAgentId = interactiveState.activeAgentId;
|
|
200
|
+
activeSessionId = interactiveState.activeSessionId;
|
|
201
|
+
latestRequestId = interactiveState.latestRequestId;
|
|
202
|
+
await client.stop();
|
|
203
|
+
restoreWriteListenerGuard?.();
|
|
204
|
+
restoreEmitterListenerGuard?.();
|
|
205
|
+
restoreStderrNoiseFilter?.();
|
|
206
|
+
restoreWarningFilter?.();
|
|
207
|
+
return 0;
|
|
208
|
+
}
|
|
209
|
+
catch (error) {
|
|
210
|
+
restoreWriteListenerGuard?.();
|
|
211
|
+
restoreEmitterListenerGuard?.();
|
|
212
|
+
restoreStderrNoiseFilter?.();
|
|
213
|
+
restoreWarningFilter?.();
|
|
214
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
215
|
+
stderr(`${message}\n`);
|
|
216
|
+
if (client) {
|
|
217
|
+
await client.stop().catch(() => undefined);
|
|
218
|
+
}
|
|
219
|
+
return 1;
|
|
220
|
+
}
|
|
221
|
+
};
|
|
222
|
+
if (command === "init") {
|
|
223
|
+
if (!projectName?.trim()) {
|
|
224
|
+
stderr(renderUsage());
|
|
225
|
+
return 1;
|
|
226
|
+
}
|
|
227
|
+
const parsed = parseInitOptions(rest);
|
|
228
|
+
if (parsed.error) {
|
|
229
|
+
stderr(`${parsed.error}\n`);
|
|
230
|
+
stderr(renderUsage());
|
|
231
|
+
return 1;
|
|
232
|
+
}
|
|
233
|
+
try {
|
|
234
|
+
const projectRoot = path.resolve(cwd, projectName);
|
|
235
|
+
const result = await initProject(projectRoot, projectName, parsed.options);
|
|
236
|
+
stdout(`Created ${result.projectSlug} at ${result.projectRoot}\n`);
|
|
237
|
+
stdout("Next steps:\n");
|
|
238
|
+
stdout(` cd ${projectName}\n`);
|
|
239
|
+
stdout(" npm install\n");
|
|
240
|
+
if ((parsed.options?.provider ?? "openai") === "openai") {
|
|
241
|
+
stdout(" export OPENAI_API_KEY=your_key_here\n");
|
|
242
|
+
}
|
|
243
|
+
stdout(' npm run start -- "Research a topic"\n');
|
|
244
|
+
return 0;
|
|
245
|
+
}
|
|
246
|
+
catch (error) {
|
|
247
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
248
|
+
stderr(`${message}\n`);
|
|
249
|
+
return 1;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
if (command === "chat") {
|
|
253
|
+
const parsed = parseChatOptions([projectName, ...rest].filter((item) => typeof item === "string"));
|
|
254
|
+
if (parsed.error) {
|
|
255
|
+
stderr(`${parsed.error}\n`);
|
|
256
|
+
stderr(renderUsage());
|
|
257
|
+
return 1;
|
|
258
|
+
}
|
|
259
|
+
return executeChat(parsed);
|
|
260
|
+
}
|
|
261
|
+
if (!isTopLevelCliCommand(command)) {
|
|
262
|
+
const parsed = parseChatOptions(argv);
|
|
263
|
+
if (parsed.error) {
|
|
264
|
+
stderr(`${parsed.error}\n`);
|
|
265
|
+
stderr(renderUsage());
|
|
266
|
+
return 1;
|
|
267
|
+
}
|
|
268
|
+
return executeChat(parsed);
|
|
269
|
+
}
|
|
270
|
+
if (command === "acp") {
|
|
271
|
+
return handleAcpCommand([projectName, ...rest], { cwd, stderr }, {
|
|
272
|
+
createHarness,
|
|
273
|
+
serveAcp,
|
|
274
|
+
serveAcpHttp,
|
|
275
|
+
spawnManagedCliProcess,
|
|
276
|
+
isManagedProcessRunning,
|
|
277
|
+
signalManagedProcess,
|
|
278
|
+
serveA2a,
|
|
279
|
+
serveAgUi,
|
|
280
|
+
serveRuntimeMcp,
|
|
281
|
+
serveRuntimeMcpStreamableHttp,
|
|
282
|
+
});
|
|
283
|
+
}
|
|
284
|
+
if (command === "ag-ui") {
|
|
285
|
+
return handleAgUiCommand([projectName, ...rest], { cwd, stderr }, { createHarness, serveAgUi });
|
|
286
|
+
}
|
|
287
|
+
if (command === "a2a") {
|
|
288
|
+
return handleA2aCommand([projectName, ...rest], { cwd, stderr }, {
|
|
289
|
+
createHarness,
|
|
290
|
+
serveA2a,
|
|
291
|
+
spawnManagedCliProcess,
|
|
292
|
+
isManagedProcessRunning,
|
|
293
|
+
signalManagedProcess,
|
|
294
|
+
});
|
|
295
|
+
}
|
|
296
|
+
if (command === "mcp" || command === "runtime-mcp") {
|
|
297
|
+
return handleMcpCommand([projectName, ...rest], { cwd, stderr }, {
|
|
298
|
+
createHarness,
|
|
299
|
+
serveRuntimeMcp,
|
|
300
|
+
serveRuntimeMcpStreamableHttp,
|
|
301
|
+
spawnManagedCliProcess,
|
|
302
|
+
isManagedProcessRunning,
|
|
303
|
+
signalManagedProcess,
|
|
304
|
+
});
|
|
305
|
+
}
|
|
306
|
+
if (command === "runtime") {
|
|
307
|
+
return handleRuntimeCommand([projectName, ...rest], { cwd, stdout, stderr }, {
|
|
308
|
+
createHarness,
|
|
309
|
+
...(deps.createAgentHarness ? {} : { createClient: createAgentHarnessClient }),
|
|
310
|
+
sleep,
|
|
311
|
+
});
|
|
312
|
+
}
|
|
313
|
+
stderr(renderUsage());
|
|
314
|
+
return 1;
|
|
315
|
+
}
|
|
316
|
+
export function resolveInvokedCliHref(argvPath) {
|
|
317
|
+
if (!argvPath) {
|
|
318
|
+
return "";
|
|
319
|
+
}
|
|
320
|
+
const resolved = path.resolve(argvPath);
|
|
321
|
+
const realPath = existsSync(resolved) ? realpathSync(resolved) : resolved;
|
|
322
|
+
return pathToFileURL(realPath).href;
|
|
323
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { type ManagedCliService } from "./managed-service.js";
|
|
2
|
+
export declare function startManagedHttpService(input: {
|
|
3
|
+
service: ManagedCliService;
|
|
4
|
+
workspacePath: string;
|
|
5
|
+
args: string[];
|
|
6
|
+
stderr: (message: string) => void;
|
|
7
|
+
spawnManagedCliProcess: (input: {
|
|
8
|
+
args: string[];
|
|
9
|
+
cwd: string;
|
|
10
|
+
stdoutPath: string;
|
|
11
|
+
stderrPath: string;
|
|
12
|
+
}) => Promise<{
|
|
13
|
+
pid?: number;
|
|
14
|
+
}>;
|
|
15
|
+
isManagedProcessRunning: (pid: number) => boolean;
|
|
16
|
+
}): Promise<number>;
|
|
17
|
+
export declare function stopManagedHttpService(input: {
|
|
18
|
+
service: ManagedCliService;
|
|
19
|
+
workspacePath: string;
|
|
20
|
+
stderr: (message: string) => void;
|
|
21
|
+
isManagedProcessRunning: (pid: number) => boolean;
|
|
22
|
+
signalManagedProcess: (pid: number, signal?: NodeJS.Signals) => void;
|
|
23
|
+
}): Promise<number>;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { clearManagedServiceState, managedServiceLogPath, readManagedServiceState, sleep, writeManagedServiceState, } from "./managed-service.js";
|
|
2
|
+
export async function startManagedHttpService(input) {
|
|
3
|
+
const existing = await readManagedServiceState(input.workspacePath, input.service);
|
|
4
|
+
if (existing && input.isManagedProcessRunning(existing.pid)) {
|
|
5
|
+
input.stderr(`${input.service.toUpperCase()} service is already running for ${input.workspacePath} (pid ${existing.pid}).\n`);
|
|
6
|
+
return 0;
|
|
7
|
+
}
|
|
8
|
+
if (existing) {
|
|
9
|
+
await clearManagedServiceState(input.workspacePath, input.service);
|
|
10
|
+
}
|
|
11
|
+
const stdoutPath = managedServiceLogPath(input.workspacePath, input.service, "stdout");
|
|
12
|
+
const stderrPath = managedServiceLogPath(input.workspacePath, input.service, "stderr");
|
|
13
|
+
const spawned = await input.spawnManagedCliProcess({
|
|
14
|
+
args: input.args,
|
|
15
|
+
cwd: input.workspacePath,
|
|
16
|
+
stdoutPath,
|
|
17
|
+
stderrPath,
|
|
18
|
+
});
|
|
19
|
+
if (!spawned.pid || spawned.pid <= 0) {
|
|
20
|
+
input.stderr(`Failed to start ${input.service.toUpperCase()} service for ${input.workspacePath}.\n`);
|
|
21
|
+
return 1;
|
|
22
|
+
}
|
|
23
|
+
await writeManagedServiceState({
|
|
24
|
+
pid: spawned.pid,
|
|
25
|
+
service: input.service,
|
|
26
|
+
workspaceRoot: input.workspacePath,
|
|
27
|
+
transport: input.service === "mcp" ? "streamable-http" : "http",
|
|
28
|
+
startedAt: new Date().toISOString(),
|
|
29
|
+
stdoutPath,
|
|
30
|
+
stderrPath,
|
|
31
|
+
});
|
|
32
|
+
await sleep(250);
|
|
33
|
+
if (!input.isManagedProcessRunning(spawned.pid)) {
|
|
34
|
+
await clearManagedServiceState(input.workspacePath, input.service);
|
|
35
|
+
input.stderr(`Failed to keep ${input.service.toUpperCase()} service running for ${input.workspacePath}. Check ${stderrPath}.\n`);
|
|
36
|
+
return 1;
|
|
37
|
+
}
|
|
38
|
+
input.stderr(`Started ${input.service.toUpperCase()} service for ${input.workspacePath} (pid ${spawned.pid}). Logs: ${stdoutPath}, ${stderrPath}\n`);
|
|
39
|
+
return 0;
|
|
40
|
+
}
|
|
41
|
+
export async function stopManagedHttpService(input) {
|
|
42
|
+
const existing = await readManagedServiceState(input.workspacePath, input.service);
|
|
43
|
+
if (!existing) {
|
|
44
|
+
input.stderr(`No managed ${input.service.toUpperCase()} service is recorded for ${input.workspacePath}.\n`);
|
|
45
|
+
return 0;
|
|
46
|
+
}
|
|
47
|
+
if (!input.isManagedProcessRunning(existing.pid)) {
|
|
48
|
+
await clearManagedServiceState(input.workspacePath, input.service);
|
|
49
|
+
input.stderr(`Managed ${input.service.toUpperCase()} service for ${input.workspacePath} is already stopped.\n`);
|
|
50
|
+
return 0;
|
|
51
|
+
}
|
|
52
|
+
input.signalManagedProcess(existing.pid, "SIGTERM");
|
|
53
|
+
for (let attempt = 0; attempt < 20; attempt += 1) {
|
|
54
|
+
await sleep(100);
|
|
55
|
+
if (!input.isManagedProcessRunning(existing.pid)) {
|
|
56
|
+
await clearManagedServiceState(input.workspacePath, input.service);
|
|
57
|
+
input.stderr(`Stopped ${input.service.toUpperCase()} service for ${input.workspacePath}.\n`);
|
|
58
|
+
return 0;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
input.stderr(`Timed out while stopping ${input.service.toUpperCase()} service for ${input.workspacePath} (pid ${existing.pid}).\n`);
|
|
62
|
+
return 1;
|
|
63
|
+
}
|