@mistriwork/acp 0.0.1 → 1.4.1
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/LICENSE +21 -0
- package/README.md +10 -0
- package/dist/adapter.d.ts +32 -0
- package/dist/adapter.js +102 -0
- package/dist/cliAdapter.d.ts +54 -0
- package/dist/cliAdapter.js +197 -0
- package/dist/client.d.ts +6 -0
- package/dist/client.js +680 -0
- package/dist/env.d.ts +29 -0
- package/dist/env.js +114 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.js +12 -0
- package/dist/presets.d.ts +29 -0
- package/dist/presets.js +60 -0
- package/dist/stdioMcp.d.ts +37 -0
- package/dist/stdioMcp.js +61 -0
- package/dist/types.d.ts +237 -0
- package/dist/types.js +1 -0
- package/dist/worktree.d.ts +52 -0
- package/dist/worktree.js +145 -0
- package/package.json +38 -2
package/dist/env.d.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Safe subprocess environment allowlisting and process-group teardown shared by
|
|
3
|
+
* `@mistriwork/acp`'s ACP client and stdio MCP client.
|
|
4
|
+
*
|
|
5
|
+
* Security invariants:
|
|
6
|
+
* - Never inherits Mistri server secrets (`BETTER_AUTH_SECRET`, `MISTRI_API_TOKEN`,
|
|
7
|
+
* `DATABASE_URL`, provider API keys, or nested `CLAUDECODE` flags) into spawned
|
|
8
|
+
* agent or MCP subprocesses unless explicitly opted in via `extraEnv` or
|
|
9
|
+
* `passThroughEnv`.
|
|
10
|
+
* - Spawns children in their own POSIX process group (`detached: true`) so
|
|
11
|
+
* cancellation kills the complete subtree without leaving orphan workers.
|
|
12
|
+
*/
|
|
13
|
+
import { type ChildProcess } from 'node:child_process';
|
|
14
|
+
/** Build an allowlisted environment object for a spawned ACP or stdio MCP child process. */
|
|
15
|
+
export declare function buildSafeChildEnv(options?: {
|
|
16
|
+
readonly extraEnv?: Readonly<Record<string, string>>;
|
|
17
|
+
readonly passThroughEnv?: readonly string[];
|
|
18
|
+
readonly sourceEnv?: Readonly<Record<string, string | undefined>>;
|
|
19
|
+
}): Record<string, string>;
|
|
20
|
+
/** Spawn a subprocess in its own process group (on POSIX) with piped stdio and allowlisted env. */
|
|
21
|
+
export declare function spawnManagedSubprocess(options: {
|
|
22
|
+
readonly command: string;
|
|
23
|
+
readonly args?: readonly string[];
|
|
24
|
+
readonly cwd?: string;
|
|
25
|
+
readonly extraEnv?: Readonly<Record<string, string>>;
|
|
26
|
+
readonly passThroughEnv?: readonly string[];
|
|
27
|
+
}): ChildProcess;
|
|
28
|
+
/** Terminate a managed child process and its entire POSIX process group gracefully, then SIGKILL. */
|
|
29
|
+
export declare function killProcessTree(child: ChildProcess, graceMs?: number): Promise<void>;
|
package/dist/env.js
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Safe subprocess environment allowlisting and process-group teardown shared by
|
|
3
|
+
* `@mistriwork/acp`'s ACP client and stdio MCP client.
|
|
4
|
+
*
|
|
5
|
+
* Security invariants:
|
|
6
|
+
* - Never inherits Mistri server secrets (`BETTER_AUTH_SECRET`, `MISTRI_API_TOKEN`,
|
|
7
|
+
* `DATABASE_URL`, provider API keys, or nested `CLAUDECODE` flags) into spawned
|
|
8
|
+
* agent or MCP subprocesses unless explicitly opted in via `extraEnv` or
|
|
9
|
+
* `passThroughEnv`.
|
|
10
|
+
* - Spawns children in their own POSIX process group (`detached: true`) so
|
|
11
|
+
* cancellation kills the complete subtree without leaving orphan workers.
|
|
12
|
+
*/
|
|
13
|
+
import { spawn } from 'node:child_process';
|
|
14
|
+
const DEFAULT_ALLOWED_ENV = new Set([
|
|
15
|
+
'PATH',
|
|
16
|
+
'HOME',
|
|
17
|
+
'USER',
|
|
18
|
+
'LOGNAME',
|
|
19
|
+
'SHELL',
|
|
20
|
+
'TERM',
|
|
21
|
+
'COLORTERM',
|
|
22
|
+
'LANG',
|
|
23
|
+
'TMPDIR',
|
|
24
|
+
'TMP',
|
|
25
|
+
'TEMP',
|
|
26
|
+
'XDG_CONFIG_HOME',
|
|
27
|
+
'XDG_DATA_HOME',
|
|
28
|
+
'XDG_CACHE_HOME',
|
|
29
|
+
'XDG_STATE_HOME',
|
|
30
|
+
'HTTP_PROXY',
|
|
31
|
+
'HTTPS_PROXY',
|
|
32
|
+
'NO_PROXY',
|
|
33
|
+
'http_proxy',
|
|
34
|
+
'https_proxy',
|
|
35
|
+
'no_proxy',
|
|
36
|
+
'SSL_CERT_FILE',
|
|
37
|
+
'SSL_CERT_DIR',
|
|
38
|
+
'NODE_EXTRA_CA_CERTS',
|
|
39
|
+
'SYSTEMROOT',
|
|
40
|
+
'COMSPEC',
|
|
41
|
+
'PATHEXT',
|
|
42
|
+
'APPDATA',
|
|
43
|
+
'LOCALAPPDATA',
|
|
44
|
+
]);
|
|
45
|
+
/** Build an allowlisted environment object for a spawned ACP or stdio MCP child process. */
|
|
46
|
+
export function buildSafeChildEnv(options = {}) {
|
|
47
|
+
const source = options.sourceEnv ?? process.env;
|
|
48
|
+
const passThrough = new Set(options.passThroughEnv ?? []);
|
|
49
|
+
const out = {};
|
|
50
|
+
for (const [key, value] of Object.entries(source)) {
|
|
51
|
+
if (value === undefined)
|
|
52
|
+
continue;
|
|
53
|
+
if (DEFAULT_ALLOWED_ENV.has(key) || key.startsWith('LC_') || passThrough.has(key)) {
|
|
54
|
+
out[key] = value;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
out.NODE_NO_WARNINGS = '1';
|
|
58
|
+
for (const [key, value] of Object.entries(options.extraEnv ?? {})) {
|
|
59
|
+
out[key] = value;
|
|
60
|
+
}
|
|
61
|
+
return out;
|
|
62
|
+
}
|
|
63
|
+
/** Spawn a subprocess in its own process group (on POSIX) with piped stdio and allowlisted env. */
|
|
64
|
+
export function spawnManagedSubprocess(options) {
|
|
65
|
+
return spawn(options.command, [...(options.args ?? [])], {
|
|
66
|
+
cwd: options.cwd ?? process.cwd(),
|
|
67
|
+
env: buildSafeChildEnv({
|
|
68
|
+
extraEnv: options.extraEnv,
|
|
69
|
+
passThroughEnv: options.passThroughEnv,
|
|
70
|
+
}),
|
|
71
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
72
|
+
detached: process.platform !== 'win32',
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
/** Terminate a managed child process and its entire POSIX process group gracefully, then SIGKILL. */
|
|
76
|
+
export async function killProcessTree(child, graceMs = 2_000) {
|
|
77
|
+
if (child.exitCode !== null || child.signalCode !== null)
|
|
78
|
+
return;
|
|
79
|
+
const pid = child.pid;
|
|
80
|
+
const sendSignal = (sig) => {
|
|
81
|
+
try {
|
|
82
|
+
if (pid && process.platform !== 'win32')
|
|
83
|
+
process.kill(-pid, sig);
|
|
84
|
+
else
|
|
85
|
+
child.kill(sig);
|
|
86
|
+
}
|
|
87
|
+
catch {
|
|
88
|
+
try {
|
|
89
|
+
child.kill(sig);
|
|
90
|
+
}
|
|
91
|
+
catch {
|
|
92
|
+
// Already exited.
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
await new Promise((resolve) => {
|
|
97
|
+
let done = false;
|
|
98
|
+
const finish = () => {
|
|
99
|
+
if (done)
|
|
100
|
+
return;
|
|
101
|
+
done = true;
|
|
102
|
+
clearTimeout(timer);
|
|
103
|
+
resolve();
|
|
104
|
+
};
|
|
105
|
+
child.once('exit', finish);
|
|
106
|
+
child.once('close', finish);
|
|
107
|
+
sendSignal('SIGTERM');
|
|
108
|
+
const timer = setTimeout(() => {
|
|
109
|
+
sendSignal('SIGKILL');
|
|
110
|
+
finish();
|
|
111
|
+
}, graceMs);
|
|
112
|
+
timer.unref?.();
|
|
113
|
+
});
|
|
114
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@mistriwork/acp`: Agent Client Protocol (ACP) v1 host runtime, stdio MCP client,
|
|
3
|
+
* and coding-agent CLI presets for Mistri Apps.
|
|
4
|
+
*/
|
|
5
|
+
export { ACP_PROTOCOL_VERSION } from './types.js';
|
|
6
|
+
export type { AcpAgentCapabilities, AcpAuthMethod, AcpAvailableCommand, AcpClient, AcpClientCapabilities, AcpClientOptions, AcpConfigOption, AcpConfigOptionValue, AcpDiffEntry, AcpFsCallbacks, AcpImplementationInfo, AcpInitializeResult, AcpMcpServerSpec, AcpPermissionOption, AcpPlanEntry, AcpPromptBlock, AcpPromptResult, AcpRequestPermissionParams, AcpSessionInfo, AcpStdioMcpServerConfig, AcpStopReason, AcpToolCallRecord, } from './types.js';
|
|
7
|
+
export { buildSafeChildEnv, killProcessTree, spawnManagedSubprocess } from './env.js';
|
|
8
|
+
export { createAcpClient } from './client.js';
|
|
9
|
+
export { createAcpAgentRuntime } from './adapter.js';
|
|
10
|
+
export type { AcpAgentRuntime, AcpAgentRuntimeOptions } from './adapter.js';
|
|
11
|
+
export { claudeCodeAcp, codexAcp, customAcp, geminiAcp } from './presets.js';
|
|
12
|
+
export type { PresetBaseOptions } from './presets.js';
|
|
13
|
+
export { createWorkspaceSandbox, createWorktreeSandbox } from './worktree.js';
|
|
14
|
+
export type { WorkspaceSandbox, WorkspaceSandboxOptions, WorktreeSandbox, WorktreeSandboxOptions } from './worktree.js';
|
|
15
|
+
export { createCliAgentAdapter } from './cliAdapter.js';
|
|
16
|
+
export type { CliAgentAdapter, CliAgentAdapterOptions, CliAgentKind, CliAgentRunOptions, CliAgentRunResult } from './cliAdapter.js';
|
|
17
|
+
export { createStdioMcpClient, createStdioMcpTransport } from './stdioMcp.js';
|
|
18
|
+
export type { CreateStdioMcpClientOptions, StdioMcpTransportOptions } from './stdioMcp.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@mistriwork/acp`: Agent Client Protocol (ACP) v1 host runtime, stdio MCP client,
|
|
3
|
+
* and coding-agent CLI presets for Mistri Apps.
|
|
4
|
+
*/
|
|
5
|
+
export { ACP_PROTOCOL_VERSION } from './types.js';
|
|
6
|
+
export { buildSafeChildEnv, killProcessTree, spawnManagedSubprocess } from './env.js';
|
|
7
|
+
export { createAcpClient } from './client.js';
|
|
8
|
+
export { createAcpAgentRuntime } from './adapter.js';
|
|
9
|
+
export { claudeCodeAcp, codexAcp, customAcp, geminiAcp } from './presets.js';
|
|
10
|
+
export { createWorkspaceSandbox, createWorktreeSandbox } from './worktree.js';
|
|
11
|
+
export { createCliAgentAdapter } from './cliAdapter.js';
|
|
12
|
+
export { createStdioMcpClient, createStdioMcpTransport } from './stdioMcp.js';
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ready-to-use ACP adapter presets for major coding CLIs (`claude-agent-acp`,
|
|
3
|
+
* `codex-acp`, `gemini --acp`, and custom binaries). Isolates CLI-specific flags
|
|
4
|
+
* and environment conventions cleanly inside `@mistriwork/acp` rather than Mistri core.
|
|
5
|
+
*/
|
|
6
|
+
import { type AcpAgentRuntime, type AcpAgentRuntimeOptions } from './adapter.js';
|
|
7
|
+
import type { AcpMcpServerSpec } from './types.js';
|
|
8
|
+
export interface PresetBaseOptions {
|
|
9
|
+
readonly id?: string;
|
|
10
|
+
readonly command?: string;
|
|
11
|
+
readonly args?: readonly string[];
|
|
12
|
+
readonly cwd?: string;
|
|
13
|
+
readonly mcpServers?: readonly AcpMcpServerSpec[];
|
|
14
|
+
readonly extraEnv?: Readonly<Record<string, string>>;
|
|
15
|
+
readonly passThroughEnv?: readonly string[];
|
|
16
|
+
readonly onStderr?: (chunk: string) => void;
|
|
17
|
+
}
|
|
18
|
+
/** Claude Code ACP preset (`claude-agent-acp`). Uses CLI login by default and loads project settings (`CLAUDE.md`). */
|
|
19
|
+
export declare function claudeCodeAcp(options?: PresetBaseOptions & {
|
|
20
|
+
readonly settingSources?: readonly ('user' | 'project' | 'local')[];
|
|
21
|
+
}): AcpAgentRuntime;
|
|
22
|
+
/** OpenAI Codex ACP preset (`codex-acp`). Reads the operator's Codex CLI login and config by default. */
|
|
23
|
+
export declare function codexAcp(options?: PresetBaseOptions & {
|
|
24
|
+
readonly configOverrides?: Readonly<Record<string, string>>;
|
|
25
|
+
}): AcpAgentRuntime;
|
|
26
|
+
/** Google Gemini CLI ACP preset (`gemini --acp`). Passes through `GEMINI_API_KEY` / `GOOGLE_API_KEY` if set. */
|
|
27
|
+
export declare function geminiAcp(options?: PresetBaseOptions): AcpAgentRuntime;
|
|
28
|
+
/** Create a custom ACP agent runtime for any ACP-compliant binary or stream pair. */
|
|
29
|
+
export declare function customAcp(options: AcpAgentRuntimeOptions): AcpAgentRuntime;
|
package/dist/presets.js
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ready-to-use ACP adapter presets for major coding CLIs (`claude-agent-acp`,
|
|
3
|
+
* `codex-acp`, `gemini --acp`, and custom binaries). Isolates CLI-specific flags
|
|
4
|
+
* and environment conventions cleanly inside `@mistriwork/acp` rather than Mistri core.
|
|
5
|
+
*/
|
|
6
|
+
import { createAcpAgentRuntime } from './adapter.js';
|
|
7
|
+
/** Claude Code ACP preset (`claude-agent-acp`). Uses CLI login by default and loads project settings (`CLAUDE.md`). */
|
|
8
|
+
export function claudeCodeAcp(options = {}) {
|
|
9
|
+
return createAcpAgentRuntime({
|
|
10
|
+
id: options.id ?? 'claude-code',
|
|
11
|
+
command: options.command ?? 'claude-agent-acp',
|
|
12
|
+
args: options.args ?? [],
|
|
13
|
+
defaultCwd: options.cwd,
|
|
14
|
+
mcpServers: options.mcpServers,
|
|
15
|
+
extraEnv: options.extraEnv,
|
|
16
|
+
passThroughEnv: options.passThroughEnv,
|
|
17
|
+
onStderr: options.onStderr,
|
|
18
|
+
sessionMeta: {
|
|
19
|
+
claudeCode: {
|
|
20
|
+
options: {
|
|
21
|
+
settingSources: options.settingSources ?? ['user', 'project', 'local'],
|
|
22
|
+
},
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
/** OpenAI Codex ACP preset (`codex-acp`). Reads the operator's Codex CLI login and config by default. */
|
|
28
|
+
export function codexAcp(options = {}) {
|
|
29
|
+
const extraArgs = [];
|
|
30
|
+
for (const [k, v] of Object.entries(options.configOverrides ?? {})) {
|
|
31
|
+
extraArgs.push('-c', `${k}=${v}`);
|
|
32
|
+
}
|
|
33
|
+
return createAcpAgentRuntime({
|
|
34
|
+
id: options.id ?? 'codex',
|
|
35
|
+
command: options.command ?? 'codex-acp',
|
|
36
|
+
args: [...extraArgs, ...(options.args ?? [])],
|
|
37
|
+
defaultCwd: options.cwd,
|
|
38
|
+
mcpServers: options.mcpServers,
|
|
39
|
+
extraEnv: options.extraEnv,
|
|
40
|
+
passThroughEnv: options.passThroughEnv,
|
|
41
|
+
onStderr: options.onStderr,
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
/** Google Gemini CLI ACP preset (`gemini --acp`). Passes through `GEMINI_API_KEY` / `GOOGLE_API_KEY` if set. */
|
|
45
|
+
export function geminiAcp(options = {}) {
|
|
46
|
+
return createAcpAgentRuntime({
|
|
47
|
+
id: options.id ?? 'gemini',
|
|
48
|
+
command: options.command ?? 'gemini',
|
|
49
|
+
args: ['--acp', ...(options.args ?? [])],
|
|
50
|
+
defaultCwd: options.cwd,
|
|
51
|
+
mcpServers: options.mcpServers,
|
|
52
|
+
extraEnv: options.extraEnv,
|
|
53
|
+
passThroughEnv: ['GEMINI_API_KEY', 'GOOGLE_API_KEY', ...(options.passThroughEnv ?? [])],
|
|
54
|
+
onStderr: options.onStderr,
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
/** Create a custom ACP agent runtime for any ACP-compliant binary or stream pair. */
|
|
58
|
+
export function customAcp(options) {
|
|
59
|
+
return createAcpAgentRuntime(options);
|
|
60
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Local stdio MCP transport and client for workspace-scoped MCP servers
|
|
3
|
+
* (git, LSP, test runners, local tooling). Runs on Mistri's shared NDJSON
|
|
4
|
+
* JSON-RPC peer with strict allowlisted child environments and process-group teardown.
|
|
5
|
+
*/
|
|
6
|
+
import type { Readable } from 'node:stream';
|
|
7
|
+
import { type McpClient, type McpClientTransport } from '@mistriwork/mistri/mcp';
|
|
8
|
+
export interface StdioMcpTransportOptions {
|
|
9
|
+
/** Executable path or command name to spawn (mutually exclusive with `streams`). */
|
|
10
|
+
readonly command?: string;
|
|
11
|
+
readonly args?: readonly string[];
|
|
12
|
+
readonly cwd?: string;
|
|
13
|
+
readonly extraEnv?: Readonly<Record<string, string>>;
|
|
14
|
+
readonly passThroughEnv?: readonly string[];
|
|
15
|
+
/** Optional raw stream pair instead of spawning a subprocess (for tests). */
|
|
16
|
+
readonly streams?: {
|
|
17
|
+
readonly input: Readable;
|
|
18
|
+
readonly output: {
|
|
19
|
+
write(chunk: string): unknown;
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
readonly onNotification?: (method: string, params: Record<string, unknown>) => void;
|
|
23
|
+
readonly onStderr?: (chunk: string) => void;
|
|
24
|
+
readonly timeoutMs?: number;
|
|
25
|
+
}
|
|
26
|
+
/** Create an `McpClientTransport` speaking NDJSON JSON-RPC 2.0 over a managed child process's stdio. */
|
|
27
|
+
export declare function createStdioMcpTransport(options: StdioMcpTransportOptions): McpClientTransport;
|
|
28
|
+
export interface CreateStdioMcpClientOptions extends StdioMcpTransportOptions {
|
|
29
|
+
readonly clientInfo?: {
|
|
30
|
+
readonly name: string;
|
|
31
|
+
readonly version: string;
|
|
32
|
+
};
|
|
33
|
+
readonly capabilities?: Readonly<Record<string, unknown>>;
|
|
34
|
+
readonly preferLegacyInitialize?: boolean;
|
|
35
|
+
}
|
|
36
|
+
/** Spawn a local stdio MCP server and return a full Mistri `McpClient` bound to it. */
|
|
37
|
+
export declare function createStdioMcpClient(options: CreateStdioMcpClientOptions): McpClient;
|
package/dist/stdioMcp.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { createJsonRpcPeer } from '@mistriwork/engine/server/jsonrpc.js';
|
|
2
|
+
import { createMcpClient } from '@mistriwork/mistri/mcp';
|
|
3
|
+
import { killProcessTree, spawnManagedSubprocess } from './env.js';
|
|
4
|
+
/** Create an `McpClientTransport` speaking NDJSON JSON-RPC 2.0 over a managed child process's stdio. */
|
|
5
|
+
export function createStdioMcpTransport(options) {
|
|
6
|
+
if (!options.command && !options.streams) {
|
|
7
|
+
throw new Error('createStdioMcpTransport requires either `command` or `streams`.');
|
|
8
|
+
}
|
|
9
|
+
const child = options.command
|
|
10
|
+
? spawnManagedSubprocess({
|
|
11
|
+
command: options.command,
|
|
12
|
+
args: options.args,
|
|
13
|
+
cwd: options.cwd,
|
|
14
|
+
extraEnv: options.extraEnv,
|
|
15
|
+
passThroughEnv: options.passThroughEnv,
|
|
16
|
+
})
|
|
17
|
+
: undefined;
|
|
18
|
+
if (child?.stderr && options.onStderr) {
|
|
19
|
+
child.stderr.setEncoding('utf8');
|
|
20
|
+
child.stderr.on('data', (chunk) => options.onStderr(chunk));
|
|
21
|
+
}
|
|
22
|
+
const input = options.streams ? options.streams.input : child.stdout;
|
|
23
|
+
const output = options.streams ? options.streams.output : child.stdin;
|
|
24
|
+
const peer = createJsonRpcPeer({
|
|
25
|
+
input,
|
|
26
|
+
output,
|
|
27
|
+
defaultTimeoutMs: options.timeoutMs ?? 30_000,
|
|
28
|
+
onNotification: options.onNotification,
|
|
29
|
+
});
|
|
30
|
+
return {
|
|
31
|
+
request(method, params, reqOpts) {
|
|
32
|
+
return peer.request(method, params, { ...(reqOpts?.signal ? { signal: reqOpts.signal } : {}) });
|
|
33
|
+
},
|
|
34
|
+
notify(method, params) {
|
|
35
|
+
peer.notify(method, params);
|
|
36
|
+
},
|
|
37
|
+
async close() {
|
|
38
|
+
peer.close('Stdio MCP client closed.');
|
|
39
|
+
if (child) {
|
|
40
|
+
try {
|
|
41
|
+
child.stdin?.end();
|
|
42
|
+
}
|
|
43
|
+
catch {
|
|
44
|
+
// ignore
|
|
45
|
+
}
|
|
46
|
+
await killProcessTree(child);
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
/** Spawn a local stdio MCP server and return a full Mistri `McpClient` bound to it. */
|
|
52
|
+
export function createStdioMcpClient(options) {
|
|
53
|
+
const transport = createStdioMcpTransport(options);
|
|
54
|
+
return createMcpClient({
|
|
55
|
+
transport,
|
|
56
|
+
clientInfo: options.clientInfo,
|
|
57
|
+
capabilities: options.capabilities,
|
|
58
|
+
preferLegacyInitialize: options.preferLegacyInitialize,
|
|
59
|
+
onNotification: options.onNotification,
|
|
60
|
+
});
|
|
61
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Agent Client Protocol (ACP) v1 wire types and Mistri host abstractions.
|
|
3
|
+
*/
|
|
4
|
+
import type { Readable } from 'node:stream';
|
|
5
|
+
import type { AgentEvent, AgentPrincipal, PermissionBroker, PermissionMode, SandboxAdapter } from '@mistriwork/mistri/agents';
|
|
6
|
+
import type { AcpHttpMcpServerConfig } from '@mistriwork/mistri/mcp';
|
|
7
|
+
export declare const ACP_PROTOCOL_VERSION: 1;
|
|
8
|
+
export interface AcpImplementationInfo {
|
|
9
|
+
readonly name: string;
|
|
10
|
+
readonly version: string;
|
|
11
|
+
readonly title?: string;
|
|
12
|
+
}
|
|
13
|
+
export interface AcpClientCapabilities {
|
|
14
|
+
readonly fs?: {
|
|
15
|
+
readonly readTextFile?: boolean;
|
|
16
|
+
readonly writeTextFile?: boolean;
|
|
17
|
+
};
|
|
18
|
+
readonly terminal?: boolean;
|
|
19
|
+
readonly auth?: {
|
|
20
|
+
readonly terminal?: boolean;
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
export interface AcpAgentCapabilities {
|
|
24
|
+
readonly loadSession?: boolean;
|
|
25
|
+
readonly promptCapabilities?: {
|
|
26
|
+
readonly image?: boolean;
|
|
27
|
+
readonly audio?: boolean;
|
|
28
|
+
readonly embeddedContext?: boolean;
|
|
29
|
+
};
|
|
30
|
+
readonly mcpCapabilities?: {
|
|
31
|
+
readonly http?: boolean;
|
|
32
|
+
readonly sse?: boolean;
|
|
33
|
+
};
|
|
34
|
+
readonly sessionCapabilities?: Readonly<Record<string, unknown>>;
|
|
35
|
+
}
|
|
36
|
+
export interface AcpAuthMethod {
|
|
37
|
+
readonly id: string;
|
|
38
|
+
readonly name: string;
|
|
39
|
+
readonly description?: string;
|
|
40
|
+
readonly type?: string;
|
|
41
|
+
}
|
|
42
|
+
export interface AcpInitializeResult {
|
|
43
|
+
readonly protocolVersion: number;
|
|
44
|
+
readonly agentInfo?: AcpImplementationInfo;
|
|
45
|
+
readonly agentCapabilities: AcpAgentCapabilities;
|
|
46
|
+
readonly authMethods: readonly AcpAuthMethod[];
|
|
47
|
+
}
|
|
48
|
+
export interface AcpStdioMcpServerConfig {
|
|
49
|
+
readonly name: string;
|
|
50
|
+
readonly command: string;
|
|
51
|
+
readonly args: readonly string[];
|
|
52
|
+
readonly env: ReadonlyArray<{
|
|
53
|
+
readonly name: string;
|
|
54
|
+
readonly value: string;
|
|
55
|
+
}>;
|
|
56
|
+
}
|
|
57
|
+
export type AcpMcpServerSpec = AcpHttpMcpServerConfig | AcpStdioMcpServerConfig;
|
|
58
|
+
export interface AcpConfigOptionValue {
|
|
59
|
+
readonly value: string;
|
|
60
|
+
readonly name: string;
|
|
61
|
+
readonly description?: string;
|
|
62
|
+
}
|
|
63
|
+
export interface AcpConfigOption {
|
|
64
|
+
readonly id: string;
|
|
65
|
+
readonly name: string;
|
|
66
|
+
readonly description?: string;
|
|
67
|
+
readonly category?: string;
|
|
68
|
+
readonly currentValue: string;
|
|
69
|
+
readonly options: readonly AcpConfigOptionValue[];
|
|
70
|
+
}
|
|
71
|
+
export interface AcpAvailableCommand {
|
|
72
|
+
readonly name: string;
|
|
73
|
+
readonly description?: string;
|
|
74
|
+
readonly input?: {
|
|
75
|
+
readonly hint?: string;
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
export interface AcpSessionInfo {
|
|
79
|
+
readonly sessionId: string;
|
|
80
|
+
readonly modes?: {
|
|
81
|
+
readonly currentModeId: string;
|
|
82
|
+
readonly availableModes: ReadonlyArray<{
|
|
83
|
+
readonly id: string;
|
|
84
|
+
readonly name: string;
|
|
85
|
+
readonly description?: string;
|
|
86
|
+
}>;
|
|
87
|
+
};
|
|
88
|
+
readonly configOptions?: readonly AcpConfigOption[];
|
|
89
|
+
}
|
|
90
|
+
export type AcpPromptBlock = {
|
|
91
|
+
readonly type: 'text';
|
|
92
|
+
readonly text: string;
|
|
93
|
+
} | {
|
|
94
|
+
readonly type: 'image';
|
|
95
|
+
readonly data: string;
|
|
96
|
+
readonly mimeType: string;
|
|
97
|
+
} | {
|
|
98
|
+
readonly type: 'resource';
|
|
99
|
+
readonly resource: {
|
|
100
|
+
readonly uri: string;
|
|
101
|
+
readonly mimeType?: string;
|
|
102
|
+
readonly text?: string;
|
|
103
|
+
readonly blob?: string;
|
|
104
|
+
};
|
|
105
|
+
} | {
|
|
106
|
+
readonly type: 'resource_link';
|
|
107
|
+
readonly uri: string;
|
|
108
|
+
readonly name?: string;
|
|
109
|
+
readonly title?: string;
|
|
110
|
+
readonly mimeType?: string;
|
|
111
|
+
};
|
|
112
|
+
export type AcpStopReason = 'end_turn' | 'max_tokens' | 'max_turn_requests' | 'refusal' | 'cancelled';
|
|
113
|
+
export interface AcpPlanEntry {
|
|
114
|
+
readonly content: string;
|
|
115
|
+
readonly status: 'pending' | 'in_progress' | 'completed';
|
|
116
|
+
readonly priority?: 'low' | 'medium' | 'high';
|
|
117
|
+
}
|
|
118
|
+
export interface AcpDiffEntry {
|
|
119
|
+
readonly path: string;
|
|
120
|
+
readonly oldText?: string;
|
|
121
|
+
readonly newText: string;
|
|
122
|
+
}
|
|
123
|
+
export interface AcpToolCallRecord {
|
|
124
|
+
readonly toolCallId: string;
|
|
125
|
+
readonly title: string;
|
|
126
|
+
readonly kind?: string;
|
|
127
|
+
readonly status: 'pending' | 'in_progress' | 'completed' | 'failed';
|
|
128
|
+
readonly rawInput?: Record<string, unknown>;
|
|
129
|
+
readonly rawOutput?: unknown;
|
|
130
|
+
readonly diffs?: readonly AcpDiffEntry[];
|
|
131
|
+
}
|
|
132
|
+
export interface AcpPermissionOption {
|
|
133
|
+
readonly optionId: string;
|
|
134
|
+
readonly name: string;
|
|
135
|
+
readonly kind: 'allow_once' | 'allow_always' | 'reject_once' | 'reject_always' | string;
|
|
136
|
+
}
|
|
137
|
+
export interface AcpRequestPermissionParams {
|
|
138
|
+
readonly sessionId: string;
|
|
139
|
+
readonly toolCall: {
|
|
140
|
+
readonly toolCallId: string;
|
|
141
|
+
readonly title?: string;
|
|
142
|
+
readonly kind?: string;
|
|
143
|
+
readonly rawInput?: Record<string, unknown>;
|
|
144
|
+
readonly content?: readonly Record<string, unknown>[];
|
|
145
|
+
};
|
|
146
|
+
readonly options: readonly AcpPermissionOption[];
|
|
147
|
+
}
|
|
148
|
+
export interface AcpPromptResult {
|
|
149
|
+
readonly sessionId: string;
|
|
150
|
+
readonly stopReason: AcpStopReason;
|
|
151
|
+
readonly text: string;
|
|
152
|
+
readonly reasoning: string;
|
|
153
|
+
readonly toolCalls: readonly AcpToolCallRecord[];
|
|
154
|
+
readonly plan: readonly AcpPlanEntry[];
|
|
155
|
+
readonly diffs: readonly AcpDiffEntry[];
|
|
156
|
+
readonly _meta?: Readonly<Record<string, unknown>>;
|
|
157
|
+
}
|
|
158
|
+
export interface AcpFsCallbacks {
|
|
159
|
+
readTextFile?(params: {
|
|
160
|
+
readonly sessionId: string;
|
|
161
|
+
readonly path: string;
|
|
162
|
+
readonly line?: number;
|
|
163
|
+
readonly limit?: number;
|
|
164
|
+
}): Promise<{
|
|
165
|
+
readonly content: string;
|
|
166
|
+
}>;
|
|
167
|
+
writeTextFile?(params: {
|
|
168
|
+
readonly sessionId: string;
|
|
169
|
+
readonly path: string;
|
|
170
|
+
readonly content: string;
|
|
171
|
+
}): Promise<void>;
|
|
172
|
+
}
|
|
173
|
+
export interface AcpClientOptions {
|
|
174
|
+
/** Executable binary to spawn (mutually exclusive with `streams`). */
|
|
175
|
+
readonly command?: string;
|
|
176
|
+
/** Arguments passed to the agent executable. */
|
|
177
|
+
readonly args?: readonly string[];
|
|
178
|
+
/** Working directory for the spawned agent process (defaults to `process.cwd()`). */
|
|
179
|
+
readonly cwd?: string;
|
|
180
|
+
/** Explicit environment variables added to the allowlisted child environment. */
|
|
181
|
+
readonly extraEnv?: Readonly<Record<string, string>>;
|
|
182
|
+
/** Additional host environment variable names allowed through to the child process. */
|
|
183
|
+
readonly passThroughEnv?: readonly string[];
|
|
184
|
+
/** Optional raw stream pair instead of spawning a child process (ideal for tests or remote bridges). */
|
|
185
|
+
readonly streams?: {
|
|
186
|
+
readonly input: Readable;
|
|
187
|
+
readonly output: {
|
|
188
|
+
write(chunk: string): unknown;
|
|
189
|
+
};
|
|
190
|
+
};
|
|
191
|
+
/** Client identity advertised during `initialize`. */
|
|
192
|
+
readonly clientInfo?: AcpImplementationInfo;
|
|
193
|
+
/** Principal on whose behalf this client acts (passed to `PermissionBroker`). */
|
|
194
|
+
readonly principal?: AgentPrincipal;
|
|
195
|
+
/** Shared Mistri permission broker for `session/request_permission` and sandbox writes. */
|
|
196
|
+
readonly permissions?: PermissionBroker;
|
|
197
|
+
/** Optional Mistri `SandboxAdapter` used to serve `fs/*` and `terminal/*` callbacks safely. */
|
|
198
|
+
readonly sandbox?: SandboxAdapter;
|
|
199
|
+
/** Optional custom `fs/*` callbacks (takes precedence over `sandbox` for file operations). */
|
|
200
|
+
readonly fs?: AcpFsCallbacks;
|
|
201
|
+
/** Enable client-side `terminal/*` callbacks backed by `sandbox.exec` (default `true` when `sandbox` is provided). */
|
|
202
|
+
readonly enableTerminal?: boolean;
|
|
203
|
+
/** Observer for raw `session/update` notifications. */
|
|
204
|
+
readonly onSessionUpdate?: (sessionId: string, update: Record<string, unknown>) => void;
|
|
205
|
+
/** Observer for normalized Mistri `AgentEvent`s emitted across all sessions on this client. */
|
|
206
|
+
readonly onEvent?: (event: AgentEvent) => void;
|
|
207
|
+
/** Observer for subprocess stderr log chunks. */
|
|
208
|
+
readonly onStderr?: (chunk: string) => void;
|
|
209
|
+
/** Default timeout in ms for control RPCs (`initialize`, `session/new`, etc., default `30_000`). */
|
|
210
|
+
readonly controlTimeoutMs?: number;
|
|
211
|
+
}
|
|
212
|
+
export interface AcpClient {
|
|
213
|
+
initialize(signal?: AbortSignal): Promise<AcpInitializeResult>;
|
|
214
|
+
authenticate(methodId: string, signal?: AbortSignal): Promise<void>;
|
|
215
|
+
newSession(params: {
|
|
216
|
+
readonly cwd: string;
|
|
217
|
+
readonly mcpServers?: readonly AcpMcpServerSpec[];
|
|
218
|
+
readonly mode?: PermissionMode | string;
|
|
219
|
+
readonly _meta?: Readonly<Record<string, unknown>>;
|
|
220
|
+
readonly signal?: AbortSignal;
|
|
221
|
+
}): Promise<AcpSessionInfo>;
|
|
222
|
+
loadSession(params: {
|
|
223
|
+
readonly sessionId: string;
|
|
224
|
+
readonly cwd: string;
|
|
225
|
+
readonly mcpServers?: readonly AcpMcpServerSpec[];
|
|
226
|
+
readonly signal?: AbortSignal;
|
|
227
|
+
}): Promise<AcpSessionInfo>;
|
|
228
|
+
prompt(sessionId: string, prompt: string | readonly AcpPromptBlock[], options?: {
|
|
229
|
+
readonly signal?: AbortSignal;
|
|
230
|
+
readonly emit?: (event: AgentEvent) => void;
|
|
231
|
+
readonly _meta?: Readonly<Record<string, unknown>>;
|
|
232
|
+
}): Promise<AcpPromptResult>;
|
|
233
|
+
setMode(sessionId: string, modeId: PermissionMode | string, signal?: AbortSignal): Promise<void>;
|
|
234
|
+
setConfigOption(sessionId: string, configId: string, value: string, signal?: AbortSignal): Promise<readonly AcpConfigOption[]>;
|
|
235
|
+
cancel(sessionId: string): void;
|
|
236
|
+
close(): Promise<void>;
|
|
237
|
+
}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const ACP_PROTOCOL_VERSION = 1;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type { SandboxAdapter } from '@mistriwork/mistri/agents';
|
|
2
|
+
export interface WorkspaceSandboxOptions {
|
|
3
|
+
/** Root directory that confines every file read, write, and command execution. */
|
|
4
|
+
readonly rootDir: string;
|
|
5
|
+
/** Default command timeout in milliseconds (default: 30_000). */
|
|
6
|
+
readonly defaultTimeoutMs?: number;
|
|
7
|
+
/** Maximum stdout/stderr buffer in bytes (default: 2 MiB). */
|
|
8
|
+
readonly maxBufferBytes?: number;
|
|
9
|
+
}
|
|
10
|
+
export interface WorkspaceSandbox extends SandboxAdapter {
|
|
11
|
+
/** Resolved canonical root directory confining this sandbox. */
|
|
12
|
+
readonly rootDir: string;
|
|
13
|
+
}
|
|
14
|
+
export interface WorktreeSandboxOptions {
|
|
15
|
+
/** Path to the source Git repository. */
|
|
16
|
+
readonly repoDir: string;
|
|
17
|
+
/** Branch name to attach or create inside the worktree. */
|
|
18
|
+
readonly branch: string;
|
|
19
|
+
/** Target directory for the worktree (defaults to `<repoDir>/.mistri-worktrees/<safe-branch>`). */
|
|
20
|
+
readonly worktreeDir?: string;
|
|
21
|
+
/** Base commit/ref when creating a new branch (defaults to `'HEAD'`). */
|
|
22
|
+
readonly baseRef?: string;
|
|
23
|
+
/** Create `branch` via `git worktree add -b` if it does not exist yet (default: `true`). */
|
|
24
|
+
readonly createBranch?: boolean;
|
|
25
|
+
/** Default command timeout in milliseconds (default: 30_000). */
|
|
26
|
+
readonly defaultTimeoutMs?: number;
|
|
27
|
+
}
|
|
28
|
+
export interface WorktreeSandbox extends WorkspaceSandbox {
|
|
29
|
+
/** Path to the source Git repository. */
|
|
30
|
+
readonly repoDir: string;
|
|
31
|
+
/** Branch checked out inside this isolated worktree. */
|
|
32
|
+
readonly branch: string;
|
|
33
|
+
/** Return unified `git diff` (against `baseRef` or working tree when omitted). */
|
|
34
|
+
diff(baseRef?: string): Promise<string>;
|
|
35
|
+
/** Return `git status --porcelain` lines for this worktree. */
|
|
36
|
+
status(): Promise<readonly string[]>;
|
|
37
|
+
/** Remove the git worktree cleanly (and optionally delete the temporary branch). */
|
|
38
|
+
cleanup(options?: {
|
|
39
|
+
readonly force?: boolean;
|
|
40
|
+
readonly deleteBranch?: boolean;
|
|
41
|
+
}): Promise<void>;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Create a root-contained `SandboxAdapter` backed by local filesystem I/O and
|
|
45
|
+
* `/bin/sh -c` command execution, blocking path traversal and symlink escapes.
|
|
46
|
+
*/
|
|
47
|
+
export declare function createWorkspaceSandbox(options: WorkspaceSandboxOptions): WorkspaceSandbox;
|
|
48
|
+
/**
|
|
49
|
+
* Create an isolated Git worktree for a coding-agent session and return a
|
|
50
|
+
* root-contained `WorktreeSandbox` (`SandboxAdapter` + `diff`, `status`, `cleanup`).
|
|
51
|
+
*/
|
|
52
|
+
export declare function createWorktreeSandbox(options: WorktreeSandboxOptions): Promise<WorktreeSandbox>;
|