march-cli 0.1.12 → 0.1.14
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/package.json +2 -1
- package/src/agent/command-exec-tool.mjs +42 -8
- package/src/agent/{read-file-tool.mjs → file-tools/read-file-tool.mjs} +2 -2
- package/src/agent/file-tools/read-image-tool.mjs +76 -0
- package/src/agent/runner/runner-utils.mjs +6 -0
- package/src/agent/runner.mjs +17 -16
- package/src/agent/runtime/ipc/ipc-peer.mjs +99 -0
- package/src/agent/runtime/ipc/process-ipc-transport.mjs +16 -0
- package/src/agent/runtime/remote-runner-client.mjs +73 -0
- package/src/agent/runtime/remote-ui-client.mjs +19 -0
- package/src/agent/runtime/runner-ipc-target.mjs +126 -0
- package/src/agent/runtime/runner-process-client.mjs +47 -0
- package/src/agent/runtime/runner-process-entry.mjs +93 -0
- package/src/agent/runtime/runner-runtime-host.mjs +1 -0
- package/src/agent/runtime/ui-event-bridge.mjs +85 -0
- package/src/agent/screen-tools/list-windows-tool.mjs +39 -0
- package/src/agent/screen-tools/screen-tool.mjs +49 -0
- package/src/agent/screen-tools/windows-screen.mjs +133 -0
- package/src/agent/session/session-options.mjs +2 -1
- package/src/agent/tool-summary.mjs +112 -0
- package/src/agent/tools.mjs +12 -5
- package/src/agent/turn/turn-events.mjs +46 -0
- package/src/agent/turn/turn-runner.mjs +2 -1
- package/src/agent/vision-capability.mjs +14 -0
- package/src/cli/args.mjs +8 -0
- package/src/cli/commands/copy-command.mjs +16 -2
- package/src/cli/commands/status-command.mjs +7 -4
- package/src/cli/commands/thinking-command.mjs +10 -3
- package/src/cli/repl-loop.mjs +3 -1
- package/src/cli/startup/create-runtime-runner.mjs +61 -0
- package/src/cli/startup/startup-banner.mjs +64 -10
- package/src/cli/tui/layout/main-pane-layout.mjs +16 -7
- package/src/cli/tui/selection-screen.mjs +83 -34
- package/src/cli/tui/status/status-bar.mjs +155 -18
- package/src/cli/tui/tool-rendering.mjs +3 -113
- package/src/cli/tui/tui-handlers.mjs +1 -1
- package/src/cli/tui/ui-theme.mjs +14 -5
- package/src/cli/ui.mjs +1 -1
- package/src/config/config-json.mjs +11 -0
- package/src/context/engine.mjs +10 -9
- package/src/context/profiles.mjs +39 -0
- package/src/context/system-core/base.md +2 -1
- package/src/main.mjs +42 -35
- package/src/provider/accept-command.mjs +89 -0
- package/src/provider/command.mjs +21 -0
- package/src/provider/custom-provider.mjs +5 -4
- package/src/provider/share-command.mjs +79 -0
- package/src/provider/share-payload.mjs +52 -0
- package/src/supergrok/tool.mjs +6 -6
- package/src/context/center-memory.mjs +0 -14
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "march-cli",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.14",
|
|
4
4
|
"description": "March CLI — terminal-native coding agent with context reconstruction",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/main.mjs",
|
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
"scripts": {
|
|
15
15
|
"dev": "cd .. && node march-cli/bin/march.mjs",
|
|
16
16
|
"test": "node test/smoke.test.mjs",
|
|
17
|
+
"test:fast": "node test/fast.test.mjs",
|
|
17
18
|
"test:real": "npm run test:shell-runtime-real && npm run test:shell-tui-real && npm run test:tui-key-real",
|
|
18
19
|
"test:shell-runtime-real": "node test/shell-real-runtime.acceptance.mjs",
|
|
19
20
|
"test:shell-tui-real": "node test/shell-tui-real.acceptance.mjs",
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { spawnSync } from "node:child_process";
|
|
1
|
+
import { spawn, spawnSync } from "node:child_process";
|
|
2
2
|
import { existsSync } from "node:fs";
|
|
3
3
|
import { defineTool } from "@earendil-works/pi-coding-agent";
|
|
4
4
|
import { Type } from "typebox";
|
|
@@ -21,23 +21,22 @@ export function createCommandExecTool({ cwd }) {
|
|
|
21
21
|
});
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
-
export function executeCommand({ cwd, command, shell = "auto", timeout = 60,
|
|
24
|
+
export async function executeCommand({ cwd, command, shell = "auto", timeout = 60, spawnImpl = spawn }) {
|
|
25
25
|
let resolved;
|
|
26
26
|
try {
|
|
27
27
|
resolved = resolveCommandShell(shell);
|
|
28
28
|
} catch (err) {
|
|
29
29
|
return toolText(`Error: ${err.message}`, { error: true });
|
|
30
30
|
}
|
|
31
|
-
|
|
31
|
+
|
|
32
|
+
const timeoutMs = Math.max(1, Number(timeout) || 60) * 1000;
|
|
33
|
+
const result = await spawnCommand(spawnImpl, resolved.bin, [...resolved.args, String(command ?? "")], {
|
|
32
34
|
cwd,
|
|
33
|
-
|
|
34
|
-
timeout: Math.max(1, Number(timeout) || 60) * 1000,
|
|
35
|
+
timeoutMs,
|
|
35
36
|
windowsHide: true,
|
|
36
|
-
maxBuffer: OUTPUT_LIMIT,
|
|
37
37
|
});
|
|
38
38
|
if (result.error) {
|
|
39
|
-
const
|
|
40
|
-
const detail = isTimeout ? ` (timed out after ${timeout}s)` : "";
|
|
39
|
+
const detail = result.timedOut ? ` (timed out after ${timeout}s)` : "";
|
|
41
40
|
return toolText(`Error: ${result.error.message}${detail}`, { error: true });
|
|
42
41
|
}
|
|
43
42
|
const stdout = stripAnsi(result.stdout ?? "");
|
|
@@ -52,6 +51,41 @@ export function executeCommand({ cwd, command, shell = "auto", timeout = 60, spa
|
|
|
52
51
|
});
|
|
53
52
|
}
|
|
54
53
|
|
|
54
|
+
function spawnCommand(spawnImpl, bin, args, options) {
|
|
55
|
+
return new Promise((resolve) => {
|
|
56
|
+
let stdout = "";
|
|
57
|
+
let stderr = "";
|
|
58
|
+
let settled = false;
|
|
59
|
+
let timedOut = false;
|
|
60
|
+
const child = spawnImpl(bin, args, { cwd: options.cwd, windowsHide: options.windowsHide });
|
|
61
|
+
const timer = setTimeout(() => {
|
|
62
|
+
timedOut = true;
|
|
63
|
+
child.kill?.("SIGTERM");
|
|
64
|
+
}, options.timeoutMs);
|
|
65
|
+
timer.unref?.();
|
|
66
|
+
|
|
67
|
+
child.stdout?.setEncoding?.("utf8");
|
|
68
|
+
child.stderr?.setEncoding?.("utf8");
|
|
69
|
+
child.stdout?.on?.("data", (chunk) => { stdout = appendLimited(stdout, chunk); });
|
|
70
|
+
child.stderr?.on?.("data", (chunk) => { stderr = appendLimited(stderr, chunk); });
|
|
71
|
+
child.once?.("error", (error) => finish({ error }));
|
|
72
|
+
child.once?.("close", (status, signal) => finish({ status, signal }));
|
|
73
|
+
|
|
74
|
+
function finish(result) {
|
|
75
|
+
if (settled) return;
|
|
76
|
+
settled = true;
|
|
77
|
+
clearTimeout(timer);
|
|
78
|
+
const error = result.error ?? (timedOut ? Object.assign(new Error("Command timed out"), { code: "ETIMEDOUT" }) : null);
|
|
79
|
+
resolve({ ...result, error, stdout, stderr, timedOut });
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function appendLimited(current, chunk) {
|
|
85
|
+
const next = current + String(chunk ?? "");
|
|
86
|
+
return next.length <= OUTPUT_LIMIT ? next : next.slice(-OUTPUT_LIMIT);
|
|
87
|
+
}
|
|
88
|
+
|
|
55
89
|
export function resolveCommandShell(shell = "auto", platform = process.platform) {
|
|
56
90
|
const normalized = String(shell ?? "auto").trim().toLowerCase();
|
|
57
91
|
if (normalized === "powershell" || (normalized === "auto" && platform === "win32")) {
|
|
@@ -2,7 +2,7 @@ import { readFileSync, readdirSync, statSync } from "node:fs";
|
|
|
2
2
|
import { resolve } from "node:path";
|
|
3
3
|
import { defineTool } from "@earendil-works/pi-coding-agent";
|
|
4
4
|
import { Type } from "typebox";
|
|
5
|
-
import { toolText } from "
|
|
5
|
+
import { toolText } from "../tool-result.mjs";
|
|
6
6
|
|
|
7
7
|
const DEFAULT_LIMIT = 30;
|
|
8
8
|
const DEFAULT_DIRECTORY_LIMIT = 200;
|
|
@@ -47,7 +47,7 @@ export function readFileSlice({ engine, path, offset = 1, limit = DEFAULT_LIMIT
|
|
|
47
47
|
const count = clampLimit(limit);
|
|
48
48
|
const selected = lines.slice(start - 1, start - 1 + count);
|
|
49
49
|
const end = start + selected.length - 1;
|
|
50
|
-
const body = selected.map((line, index) => `${start + index}
|
|
50
|
+
const body = selected.map((line, index) => `${start + index}| ${line}`).join("\n");
|
|
51
51
|
const header = `--- ${absPath} (lines ${start}-${end} of ${lines.length}) ---`;
|
|
52
52
|
const remaining = lines.length - end;
|
|
53
53
|
const footer = remaining > 0 ? `\n\n[${remaining} more lines in file. Use offset=${end + 1} to continue.]` : "";
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { readFileSync, statSync } from "node:fs";
|
|
2
|
+
import { extname } from "node:path";
|
|
3
|
+
import { defineTool } from "@earendil-works/pi-coding-agent";
|
|
4
|
+
import { Type } from "typebox";
|
|
5
|
+
import { currentModelImageInputError } from "../vision-capability.mjs";
|
|
6
|
+
const IMAGE_MIME_BY_EXT = new Map([
|
|
7
|
+
[".png", "image/png"],
|
|
8
|
+
[".jpg", "image/jpeg"],
|
|
9
|
+
[".jpeg", "image/jpeg"],
|
|
10
|
+
[".webp", "image/webp"],
|
|
11
|
+
[".gif", "image/gif"],
|
|
12
|
+
]);
|
|
13
|
+
|
|
14
|
+
export function createReadImageTool({ engine, getCurrentModel = null }) {
|
|
15
|
+
return defineTool({
|
|
16
|
+
name: "read_image",
|
|
17
|
+
label: "Read Image",
|
|
18
|
+
description: "Read a local image file and send it to the model as an image attachment. Supports png, jpg/jpeg, webp, and gif.",
|
|
19
|
+
parameters: Type.Object({
|
|
20
|
+
path: Type.String({ description: "Absolute or relative path to the image file" }),
|
|
21
|
+
}),
|
|
22
|
+
execute: async (_toolCallId, params) => {
|
|
23
|
+
const capabilityError = currentModelImageInputError(getCurrentModel);
|
|
24
|
+
if (capabilityError) return imageError(capabilityError, { unsupportedModel: true });
|
|
25
|
+
return readImageFile({ engine, ...params });
|
|
26
|
+
},
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function readImageFile({ engine, path }) {
|
|
31
|
+
const absPath = engine.resolvePath(path);
|
|
32
|
+
let stat;
|
|
33
|
+
try {
|
|
34
|
+
stat = statSync(absPath);
|
|
35
|
+
} catch (err) {
|
|
36
|
+
return imageError(`Error reading image ${absPath}: ${err.message}`, { path: absPath });
|
|
37
|
+
}
|
|
38
|
+
if (stat.isDirectory()) {
|
|
39
|
+
return imageError(`Error reading image ${absPath}: this is a directory. Use ls(path) or find(pattern, path) to inspect it.`, { path: absPath, isDirectory: true });
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const mimeType = IMAGE_MIME_BY_EXT.get(extname(absPath).toLowerCase());
|
|
43
|
+
if (!mimeType) {
|
|
44
|
+
return imageError(`Error reading image ${absPath}: unsupported image type. Supported types: png, jpg, jpeg, webp, gif.`, { path: absPath });
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
let data;
|
|
48
|
+
try {
|
|
49
|
+
data = readFileSync(absPath).toString("base64");
|
|
50
|
+
} catch (err) {
|
|
51
|
+
return imageError(`Error reading image ${absPath}: ${err.message}`, { path: absPath });
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const size = formatSize(stat.size);
|
|
55
|
+
return {
|
|
56
|
+
content: [
|
|
57
|
+
{ type: "text", text: `Read image file: ${absPath}\nMIME: ${mimeType}\nSize: ${size}` },
|
|
58
|
+
{ type: "image", data, mimeType },
|
|
59
|
+
],
|
|
60
|
+
details: {
|
|
61
|
+
path: absPath,
|
|
62
|
+
mimeType,
|
|
63
|
+
sizeBytes: stat.size,
|
|
64
|
+
},
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function imageError(text, details = {}) {
|
|
69
|
+
return { content: [{ type: "text", text }], details: { ...details, error: true } };
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function formatSize(bytes) {
|
|
73
|
+
if (bytes < 1024) return `${bytes} B`;
|
|
74
|
+
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;
|
|
75
|
+
return `${(bytes / 1024 / 1024).toFixed(1)} MB`;
|
|
76
|
+
}
|
|
@@ -16,3 +16,9 @@ export async function notifyTurnEndBestEffort(turnNotifier, event) {
|
|
|
16
16
|
return { ok: false, reason: err?.message ?? String(err), results: [] };
|
|
17
17
|
}
|
|
18
18
|
}
|
|
19
|
+
|
|
20
|
+
export function notifyTurnEndDetached(turnNotifier, event, onResult = () => {}) {
|
|
21
|
+
const pending = notifyTurnEndBestEffort(turnNotifier, event);
|
|
22
|
+
pending.then(onResult, () => {});
|
|
23
|
+
return pending;
|
|
24
|
+
}
|
package/src/agent/runner.mjs
CHANGED
|
@@ -1,8 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
createAgentSession,
|
|
3
|
-
ModelRegistry,
|
|
4
|
-
SettingsManager,
|
|
5
|
-
} from "@earendil-works/pi-coding-agent";
|
|
1
|
+
import { createAgentSession, ModelRegistry, SettingsManager } from "@earendil-works/pi-coding-agent";
|
|
6
2
|
import { createMarchAuthStorage } from "../auth/storage.mjs";
|
|
7
3
|
import { ContextEngine } from "../context/engine.mjs";
|
|
8
4
|
import { createMarchLifecycleAdapter } from "../extensions/lifecycle-adapter.mjs";
|
|
@@ -14,8 +10,9 @@ import { appendProviderUserMessage, estimateProviderPayloadTokens, installModelP
|
|
|
14
10
|
import { resolveInitialModel, resolveRunnerSessionManager } from "./runner/runner-init.mjs";
|
|
15
11
|
import { runRunnerCleanup } from "./runner/runner-cleanup.mjs";
|
|
16
12
|
import { createRunnerRuntimeHost } from "./runtime/runner-runtime-host.mjs";
|
|
13
|
+
import { createRuntimeUiBridge } from "./runtime/ui-event-bridge.mjs";
|
|
17
14
|
import { getRunnerSessionStats, syncEngineSessionState } from "./runner/runner-session-state.mjs";
|
|
18
|
-
import { notifyTurnEndBestEffort, providerContextToPayload } from "./runner/runner-utils.mjs";
|
|
15
|
+
import { notifyTurnEndBestEffort, notifyTurnEndDetached, providerContextToPayload } from "./runner/runner-utils.mjs";
|
|
19
16
|
import { resolveRunnerSessionOptions } from "./session/session-options.mjs";
|
|
20
17
|
import { createSessionBinding } from "./session/session-binding.mjs";
|
|
21
18
|
import { maybeAutoNameSession } from "./session/session-auto-name.mjs";
|
|
@@ -30,7 +27,7 @@ export { MARCH_BASE_TOOL_NAMES };
|
|
|
30
27
|
export { installModelPayloadDumper } from "./model-payload-dumper.mjs";
|
|
31
28
|
export { createDefaultSessionManager, resolveRunnerSessionManager } from "./runner/runner-init.mjs";
|
|
32
29
|
export { getRunnerSessionStats, syncEngineSessionState } from "./runner/runner-session-state.mjs";
|
|
33
|
-
export async function createRunner({ cwd, modelId = null, provider = null, providers = {}, stateRoot, ui, memoryRoot = null,
|
|
30
|
+
export async function createRunner({ cwd, modelId = null, provider = null, providers = {}, stateRoot, ui, memoryRoot = null, profilePaths = null, memoryStore = null, memoryTools = [], shellRuntime = null, mcpTools = [], mcpInjections = [], mcpClientManager = null, webTools = [], namespace = "", sessionManager = null, useRuntimeHost = false, projectMarchDir = null, syncPiSidecar = false, extensionPaths = [], lifecycleHooks = [], lifecycleDiagnostics = [], authStorage = null, permissionController = null, modelContextDumper = null, turnNotifier = null, logger = null, onModelPayload = null, createAgentSessionImpl = createAgentSession, createAgentSessionRuntimeImpl, createRuntimeServices, createRuntimeSessionFromServices, maxTurns, trimBatch, serviceTier = null, hostedTools = {} }) {
|
|
34
31
|
if (!useRuntimeHost && extensionPaths.length > 0) {
|
|
35
32
|
throw new Error("--extension requires the default pi runtime host path");
|
|
36
33
|
}
|
|
@@ -50,8 +47,9 @@ export async function createRunner({ cwd, modelId = null, provider = null, provi
|
|
|
50
47
|
compaction: { enabled: false },
|
|
51
48
|
retry: { enabled: true, maxRetries: 3, baseDelayMs: 2000 },
|
|
52
49
|
});
|
|
53
|
-
const
|
|
54
|
-
const
|
|
50
|
+
const { ui: runtimeUi, eventBus: runtimeUiEvents, detach: detachRuntimeUi } = createRuntimeUiBridge(ui);
|
|
51
|
+
const lspService = new LspService({ cwd, onEvent: (event) => runtimeUi.status?.(formatLspServiceEvent(event)) });
|
|
52
|
+
const engine = new ContextEngine({ cwd, modelId, provider, namespace, memoryRoot, profilePaths, shellRuntime, lspService, injections: mcpInjections, maxTurns, trimBatch });
|
|
55
53
|
const resolvedSessionManager = resolveRunnerSessionManager(cwd, sessionManager);
|
|
56
54
|
const sessionBinding = createSessionBinding(null);
|
|
57
55
|
let currentModelCallKind = "model", currentTurnId = null;
|
|
@@ -68,7 +66,7 @@ export async function createRunner({ cwd, modelId = null, provider = null, provi
|
|
|
68
66
|
cwd, stateRoot, provider, modelId,
|
|
69
67
|
authStorage: resolvedAuth, settingsManager, modelRegistry,
|
|
70
68
|
providers,
|
|
71
|
-
sessionManager: resolvedSessionManager, sessionBinding, engine, ui,
|
|
69
|
+
sessionManager: resolvedSessionManager, sessionBinding, engine, ui: runtimeUi,
|
|
72
70
|
projectMarchDir,
|
|
73
71
|
memoryTools, memoryStore, shellRuntime, lspService, mcpTools, webTools,
|
|
74
72
|
permissionController, extensionPaths, hostedTools,
|
|
@@ -82,9 +80,10 @@ export async function createRunner({ cwd, modelId = null, provider = null, provi
|
|
|
82
80
|
});
|
|
83
81
|
} else {
|
|
84
82
|
const sessionOptions = resolveRunnerSessionOptions({
|
|
85
|
-
cwd, provider, modelId, modelRegistry, engine, ui,
|
|
83
|
+
cwd, provider, modelId, modelRegistry, engine, ui: runtimeUi,
|
|
86
84
|
memoryTools, shellRuntime, lspService, mcpTools, webTools, permissionController,
|
|
87
85
|
authStorage: resolvedAuth, projectMarchDir,
|
|
86
|
+
getCurrentModel: () => sessionBinding.get()?.model ?? selectedModel,
|
|
88
87
|
});
|
|
89
88
|
const { session } = await createAgentSessionImpl({
|
|
90
89
|
cwd, agentDir: stateRoot, ...sessionOptions,
|
|
@@ -109,6 +108,7 @@ export async function createRunner({ cwd, modelId = null, provider = null, provi
|
|
|
109
108
|
engine,
|
|
110
109
|
get session() { return sessionBinding.get(); },
|
|
111
110
|
shellRuntime,
|
|
111
|
+
runtimeUiEvents,
|
|
112
112
|
async runTurn(prompt, userMessage, { userRecallHints = [], currentProject = "" } = {}) {
|
|
113
113
|
currentPromptForContext = prompt;
|
|
114
114
|
const contextMode = nextTurnContextMode;
|
|
@@ -120,7 +120,7 @@ export async function createRunner({ cwd, modelId = null, provider = null, provi
|
|
|
120
120
|
try {
|
|
121
121
|
const result = await runRunnerTurn({
|
|
122
122
|
prompt, userMessage, options: { userRecallHints, currentProject },
|
|
123
|
-
sessionBinding, engine, ui, projectMarchDir, memoryStore,
|
|
123
|
+
sessionBinding, engine, ui: runtimeUi, projectMarchDir, memoryStore,
|
|
124
124
|
setModelCallKind: (kind) => { currentModelCallKind = kind; },
|
|
125
125
|
logger: turnLog.logger,
|
|
126
126
|
setPhase: turnLog.setPhase,
|
|
@@ -129,21 +129,21 @@ export async function createRunner({ cwd, modelId = null, provider = null, provi
|
|
|
129
129
|
autoNameSession,
|
|
130
130
|
contextMode,
|
|
131
131
|
});
|
|
132
|
-
|
|
132
|
+
notifyTurnEndDetached(turnNotifier, {
|
|
133
133
|
status: "success",
|
|
134
134
|
sessionName: engine.sessionName,
|
|
135
135
|
draft: result?.draft ?? "",
|
|
136
136
|
durationMs: Date.now() - turnStartedAt,
|
|
137
|
-
});
|
|
137
|
+
}, (notificationResult) => { lastNotificationResult = notificationResult; });
|
|
138
138
|
turnLog.endSuccess(result);
|
|
139
139
|
return result;
|
|
140
140
|
} catch (err) {
|
|
141
|
-
|
|
141
|
+
notifyTurnEndDetached(turnNotifier, {
|
|
142
142
|
status: "error",
|
|
143
143
|
sessionName: engine.sessionName,
|
|
144
144
|
errorMessage: err?.message ?? String(err),
|
|
145
145
|
durationMs: Date.now() - turnStartedAt,
|
|
146
|
-
});
|
|
146
|
+
}, (notificationResult) => { lastNotificationResult = notificationResult; });
|
|
147
147
|
turnLog.endError(err);
|
|
148
148
|
throw err;
|
|
149
149
|
} finally {
|
|
@@ -251,6 +251,7 @@ export async function createRunner({ cwd, modelId = null, provider = null, provi
|
|
|
251
251
|
() => shellRuntime?.dispose?.() ?? shellRuntime?.killAll?.(),
|
|
252
252
|
() => lspService.dispose(),
|
|
253
253
|
() => mcpClientManager?.disconnectAll?.(),
|
|
254
|
+
() => detachRuntimeUi(),
|
|
254
255
|
]);
|
|
255
256
|
},
|
|
256
257
|
};
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
const CHANNEL = "march-runtime";
|
|
2
|
+
|
|
3
|
+
export function createRuntimeIpcPeer({ send, subscribe, target = {}, timeoutMs = 0 } = {}) {
|
|
4
|
+
if (typeof send !== "function") throw new Error("send is required");
|
|
5
|
+
if (typeof subscribe !== "function") throw new Error("subscribe is required");
|
|
6
|
+
|
|
7
|
+
let nextId = 1;
|
|
8
|
+
const pending = new Map();
|
|
9
|
+
const detach = subscribe((message) => handleMessage(message));
|
|
10
|
+
|
|
11
|
+
return {
|
|
12
|
+
call(method, ...args) {
|
|
13
|
+
const id = nextId++;
|
|
14
|
+
const result = waitForResult(id, { timeoutMs, pending });
|
|
15
|
+
send({ channel: CHANNEL, kind: "request", id, method, args });
|
|
16
|
+
return result;
|
|
17
|
+
},
|
|
18
|
+
notify(method, ...args) {
|
|
19
|
+
send({ channel: CHANNEL, kind: "notify", method, args });
|
|
20
|
+
},
|
|
21
|
+
dispose() {
|
|
22
|
+
detach?.();
|
|
23
|
+
for (const { reject, timer } of pending.values()) {
|
|
24
|
+
if (timer) clearTimeout(timer);
|
|
25
|
+
reject(new Error("runtime IPC peer disposed"));
|
|
26
|
+
}
|
|
27
|
+
pending.clear();
|
|
28
|
+
},
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
async function handleMessage(message) {
|
|
32
|
+
if (!isRuntimeMessage(message)) return;
|
|
33
|
+
if (message.kind === "result" || message.kind === "error") {
|
|
34
|
+
settlePending(message, pending);
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
if (message.kind === "notify") {
|
|
38
|
+
await invokeTarget(message, target);
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
if (message.kind === "request") {
|
|
42
|
+
try {
|
|
43
|
+
const result = await invokeTarget(message, target);
|
|
44
|
+
send({ channel: CHANNEL, kind: "result", id: message.id, result });
|
|
45
|
+
} catch (error) {
|
|
46
|
+
send({ channel: CHANNEL, kind: "error", id: message.id, error: serializeError(error) });
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function waitForResult(id, { timeoutMs, pending }) {
|
|
53
|
+
return new Promise((resolve, reject) => {
|
|
54
|
+
const timer = timeoutMs > 0
|
|
55
|
+
? setTimeout(() => {
|
|
56
|
+
pending.delete(id);
|
|
57
|
+
reject(new Error(`runtime IPC request timed out: ${id}`));
|
|
58
|
+
}, timeoutMs)
|
|
59
|
+
: null;
|
|
60
|
+
pending.set(id, { resolve, reject, timer });
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function settlePending(message, pending) {
|
|
65
|
+
const entry = pending.get(message.id);
|
|
66
|
+
if (!entry) return;
|
|
67
|
+
pending.delete(message.id);
|
|
68
|
+
if (entry.timer) clearTimeout(entry.timer);
|
|
69
|
+
if (message.kind === "result") {
|
|
70
|
+
entry.resolve(message.result);
|
|
71
|
+
} else {
|
|
72
|
+
entry.reject(deserializeError(message.error));
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
async function invokeTarget(message, target) {
|
|
77
|
+
const method = target[message.method];
|
|
78
|
+
if (typeof method !== "function") throw new Error(`unknown runtime IPC method: ${message.method}`);
|
|
79
|
+
return method(...(message.args ?? []));
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function isRuntimeMessage(message) {
|
|
83
|
+
return message?.channel === CHANNEL && typeof message.kind === "string";
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function serializeError(error) {
|
|
87
|
+
return {
|
|
88
|
+
name: error?.name ?? "Error",
|
|
89
|
+
message: error?.message ?? String(error),
|
|
90
|
+
stack: error?.stack,
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function deserializeError(error) {
|
|
95
|
+
const result = new Error(error?.message ?? "runtime IPC error");
|
|
96
|
+
result.name = error?.name ?? "Error";
|
|
97
|
+
if (error?.stack) result.stack = error.stack;
|
|
98
|
+
return result;
|
|
99
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { createRuntimeIpcPeer } from "./ipc-peer.mjs";
|
|
2
|
+
|
|
3
|
+
export function createProcessRuntimeIpcPeer({ processLike = process, target = {}, timeoutMs = 0 } = {}) {
|
|
4
|
+
return createRuntimeIpcPeer({
|
|
5
|
+
send: (message) => {
|
|
6
|
+
if (typeof processLike.send !== "function") throw new Error("process IPC send is unavailable");
|
|
7
|
+
processLike.send(message);
|
|
8
|
+
},
|
|
9
|
+
subscribe: (listener) => {
|
|
10
|
+
processLike.on("message", listener);
|
|
11
|
+
return () => processLike.off("message", listener);
|
|
12
|
+
},
|
|
13
|
+
target,
|
|
14
|
+
timeoutMs,
|
|
15
|
+
});
|
|
16
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
export function createRemoteRunnerClient(peer, { initialState = null } = {}) {
|
|
2
|
+
let state = initialState;
|
|
3
|
+
|
|
4
|
+
const client = {
|
|
5
|
+
get engine() { return createEngineFacade(); },
|
|
6
|
+
get runtimeState() { return state; },
|
|
7
|
+
async init(options = {}) {
|
|
8
|
+
state = await peer.call("init", options);
|
|
9
|
+
return state;
|
|
10
|
+
},
|
|
11
|
+
async runTurn(prompt, userMessage, options = {}) {
|
|
12
|
+
const result = await peer.call("runTurn", prompt, userMessage, options);
|
|
13
|
+
await refreshState();
|
|
14
|
+
return result;
|
|
15
|
+
},
|
|
16
|
+
abort: () => peer.call("abort"),
|
|
17
|
+
async cycleModel() { return applyResultWithState(await peer.call("cycleModel")); },
|
|
18
|
+
async setModel(model) { return applyResultWithState(await peer.call("setModel", model)); },
|
|
19
|
+
getCurrentModel: () => state?.currentModel ?? null,
|
|
20
|
+
getScopedModels: () => state?.scopedModels ?? [],
|
|
21
|
+
getConfiguredProviders: () => state?.configuredProviders ?? [],
|
|
22
|
+
getSessionStats: () => state?.sessionStats ?? null,
|
|
23
|
+
async refreshState() { return refreshState(); },
|
|
24
|
+
getLastNotificationResult: () => peer.call("getLastNotificationResult"),
|
|
25
|
+
notifyTest: (options) => peer.call("notifyTest", options),
|
|
26
|
+
estimateContextTokens: (userMessage = "") => peer.call("estimateContextTokens", userMessage),
|
|
27
|
+
async setSessionName(name) { return applyResultWithState(await peer.call("setSessionName", name)); },
|
|
28
|
+
canSwitchPiSession: () => Boolean(state?.canSwitchPiSession),
|
|
29
|
+
async startNewSession() { return applyResultWithState(await peer.call("startNewSession")); },
|
|
30
|
+
getExtensionDiagnostics: () => state?.extensionDiagnostics ?? [],
|
|
31
|
+
getExtensionLifecycleState: () => state?.extensionLifecycleState ?? null,
|
|
32
|
+
getLspStatus: () => state?.lspStatus ?? null,
|
|
33
|
+
async switchPiSession(sessionPath) { return applyResultWithState(await peer.call("switchPiSession", sessionPath)); },
|
|
34
|
+
async cycleThinkingLevel() { return applyResultWithState(await peer.call("cycleThinkingLevel")); },
|
|
35
|
+
getThinkingLevel: () => state?.engine?.thinkingLevel ?? null,
|
|
36
|
+
async setThinkingLevel(level) { return applyResultWithState(await peer.call("setThinkingLevel", level)); },
|
|
37
|
+
getAvailableThinkingLevels: () => state?.availableThinkingLevels ?? [],
|
|
38
|
+
async dispose() {
|
|
39
|
+
await peer.call("dispose");
|
|
40
|
+
peer.dispose();
|
|
41
|
+
},
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
return client;
|
|
45
|
+
|
|
46
|
+
async function refreshState() {
|
|
47
|
+
state = await peer.call("getState");
|
|
48
|
+
return state;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function applyResultWithState(response) {
|
|
52
|
+
if (response && Object.hasOwn(response, "state")) {
|
|
53
|
+
state = response.state;
|
|
54
|
+
return response.result;
|
|
55
|
+
}
|
|
56
|
+
return response;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function createEngineFacade() {
|
|
60
|
+
const engine = state?.engine ?? {};
|
|
61
|
+
return {
|
|
62
|
+
...engine,
|
|
63
|
+
hasRenderedPendingAssistantRecallHints: () => true,
|
|
64
|
+
takePendingAssistantRecallHints: () => [],
|
|
65
|
+
peekPendingAssistantRecallHints: () => [],
|
|
66
|
+
markPendingAssistantRecallHintsRendered: () => {},
|
|
67
|
+
getRecentRecallMemoryIds: () => [],
|
|
68
|
+
restoreSession: () => {
|
|
69
|
+
throw new Error("remote runner session restore is not available");
|
|
70
|
+
},
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export function createRemoteRuntimeUiClient(peer) {
|
|
2
|
+
return {
|
|
3
|
+
turnStart: () => peer.notify("uiEvent", { type: "turn_start" }),
|
|
4
|
+
turnEnd: () => peer.notify("uiEvent", { type: "turn_end" }),
|
|
5
|
+
assistantReplyEnd: () => peer.notify("uiEvent", { type: "assistant_reply_end" }),
|
|
6
|
+
textDelta: (delta) => peer.notify("uiEvent", { type: "text_delta", delta }),
|
|
7
|
+
thinkingStart: () => peer.notify("uiEvent", { type: "thinking_start" }),
|
|
8
|
+
thinkingDelta: (delta) => peer.notify("uiEvent", { type: "thinking_delta", delta }),
|
|
9
|
+
thinkingEnd: (tokens) => peer.notify("uiEvent", { type: "thinking_end", tokens }),
|
|
10
|
+
toolStart: (name, args) => peer.notify("uiEvent", { type: "tool_start", name, args }),
|
|
11
|
+
toolEnd: (name, isError, result) => peer.notify("uiEvent", { type: "tool_end", name, isError, result }),
|
|
12
|
+
retryStart: (event) => peer.notify("uiEvent", { type: "retry_start", ...event }),
|
|
13
|
+
retryEnd: (event) => peer.notify("uiEvent", { type: "retry_end", ...event }),
|
|
14
|
+
status: (text) => peer.notify("uiEvent", { type: "status", text }),
|
|
15
|
+
memoryHint: ({ source, hints }) => peer.notify("uiEvent", { type: "memory_hint", source, hints }),
|
|
16
|
+
editDiff: (path, diffLines) => peer.notify("uiEvent", { type: "edit_diff", path, diffLines }),
|
|
17
|
+
requestPermission: (request) => peer.call("uiRequest", { type: "permission_request", ...request }),
|
|
18
|
+
};
|
|
19
|
+
}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
export function createRunnerIpcTarget({ createRunnerImpl, runnerOptions = {} } = {}) {
|
|
2
|
+
if (typeof createRunnerImpl !== "function") throw new Error("createRunnerImpl is required");
|
|
3
|
+
|
|
4
|
+
let runner = null;
|
|
5
|
+
|
|
6
|
+
return {
|
|
7
|
+
async init(options = {}) {
|
|
8
|
+
if (runner) return getRunnerState(runner);
|
|
9
|
+
runner = await createRunnerImpl({ ...runnerOptions, ...options });
|
|
10
|
+
return getRunnerState(runner);
|
|
11
|
+
},
|
|
12
|
+
async runTurn(prompt, userMessage, options = {}) {
|
|
13
|
+
return getRunner().runTurn(prompt, userMessage, options);
|
|
14
|
+
},
|
|
15
|
+
abort() {
|
|
16
|
+
return getRunner().abort();
|
|
17
|
+
},
|
|
18
|
+
async cycleModel() {
|
|
19
|
+
const result = await getRunner().cycleModel();
|
|
20
|
+
return { result, state: getRunnerState(runner) };
|
|
21
|
+
},
|
|
22
|
+
async setModel(model) {
|
|
23
|
+
const result = await getRunner().setModel(model);
|
|
24
|
+
return { result, state: getRunnerState(runner) };
|
|
25
|
+
},
|
|
26
|
+
getCurrentModel() {
|
|
27
|
+
return getRunner().getCurrentModel();
|
|
28
|
+
},
|
|
29
|
+
getScopedModels() {
|
|
30
|
+
return getRunner().getScopedModels();
|
|
31
|
+
},
|
|
32
|
+
getConfiguredProviders() {
|
|
33
|
+
return getRunner().getConfiguredProviders();
|
|
34
|
+
},
|
|
35
|
+
getSessionStats() {
|
|
36
|
+
return getRunner().getSessionStats();
|
|
37
|
+
},
|
|
38
|
+
getState() {
|
|
39
|
+
return getRunnerState(getRunner());
|
|
40
|
+
},
|
|
41
|
+
getLastNotificationResult() {
|
|
42
|
+
return getRunner().getLastNotificationResult();
|
|
43
|
+
},
|
|
44
|
+
notifyTest(options) {
|
|
45
|
+
return getRunner().notifyTest(options);
|
|
46
|
+
},
|
|
47
|
+
estimateContextTokens(userMessage = "") {
|
|
48
|
+
return getRunner().estimateContextTokens(userMessage);
|
|
49
|
+
},
|
|
50
|
+
setSessionName(name) {
|
|
51
|
+
const result = getRunner().setSessionName(name);
|
|
52
|
+
return { result, state: getRunnerState(runner) };
|
|
53
|
+
},
|
|
54
|
+
canSwitchPiSession() {
|
|
55
|
+
return getRunner().canSwitchPiSession();
|
|
56
|
+
},
|
|
57
|
+
async startNewSession() {
|
|
58
|
+
const result = await getRunner().startNewSession();
|
|
59
|
+
return { result, state: getRunnerState(runner) };
|
|
60
|
+
},
|
|
61
|
+
getExtensionDiagnostics() {
|
|
62
|
+
return getRunner().getExtensionDiagnostics();
|
|
63
|
+
},
|
|
64
|
+
getExtensionLifecycleState() {
|
|
65
|
+
return getRunner().getExtensionLifecycleState();
|
|
66
|
+
},
|
|
67
|
+
getLspStatus() {
|
|
68
|
+
return getRunner().getLspStatus();
|
|
69
|
+
},
|
|
70
|
+
async switchPiSession(sessionPath) {
|
|
71
|
+
const result = await getRunner().switchPiSession(sessionPath);
|
|
72
|
+
return { result, state: getRunnerState(runner) };
|
|
73
|
+
},
|
|
74
|
+
cycleThinkingLevel() {
|
|
75
|
+
const result = getRunner().cycleThinkingLevel();
|
|
76
|
+
return { result, state: getRunnerState(runner) };
|
|
77
|
+
},
|
|
78
|
+
getThinkingLevel() {
|
|
79
|
+
return getRunner().getThinkingLevel();
|
|
80
|
+
},
|
|
81
|
+
setThinkingLevel(level) {
|
|
82
|
+
const result = getRunner().setThinkingLevel(level);
|
|
83
|
+
return { result, state: getRunnerState(runner) };
|
|
84
|
+
},
|
|
85
|
+
getAvailableThinkingLevels() {
|
|
86
|
+
return getRunner().getAvailableThinkingLevels();
|
|
87
|
+
},
|
|
88
|
+
async dispose() {
|
|
89
|
+
if (!runner) return;
|
|
90
|
+
const active = runner;
|
|
91
|
+
runner = null;
|
|
92
|
+
await active.dispose();
|
|
93
|
+
},
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
function getRunner() {
|
|
97
|
+
if (!runner) throw new Error("runtime runner is not initialized");
|
|
98
|
+
return runner;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export function getRunnerState(runner) {
|
|
103
|
+
const currentModel = runner.getCurrentModel?.() ?? null;
|
|
104
|
+
const scopedModels = runner.getScopedModels?.() ?? [];
|
|
105
|
+
const thinkingLevel = runner.getThinkingLevel?.() ?? runner.engine?.thinkingLevel ?? null;
|
|
106
|
+
return {
|
|
107
|
+
engine: {
|
|
108
|
+
cwd: runner.engine?.cwd ?? null,
|
|
109
|
+
modelId: runner.engine?.modelId ?? currentModel?.id ?? null,
|
|
110
|
+
provider: runner.engine?.provider ?? currentModel?.provider ?? null,
|
|
111
|
+
thinkingLevel,
|
|
112
|
+
sessionName: runner.engine?.sessionName ?? "",
|
|
113
|
+
turns: runner.engine?.turns ?? [],
|
|
114
|
+
},
|
|
115
|
+
currentModel,
|
|
116
|
+
scopedModels,
|
|
117
|
+
configuredProviders: runner.getConfiguredProviders?.() ?? [],
|
|
118
|
+
availableThinkingLevels: runner.getAvailableThinkingLevels?.() ?? [],
|
|
119
|
+
canSwitchPiSession: runner.canSwitchPiSession?.() ?? false,
|
|
120
|
+
sessionStats: runner.getSessionStats?.() ?? null,
|
|
121
|
+
lspStatus: runner.getLspStatus?.() ?? null,
|
|
122
|
+
extensionDiagnostics: runner.getExtensionDiagnostics?.() ?? [],
|
|
123
|
+
extensionLifecycleState: runner.getExtensionLifecycleState?.() ?? null,
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
|