@ai-workbench/remote-agent 0.9.0-toolbridge.2 → 0.9.0-toolbridge.7
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 +12 -3
- package/dist/acp.d.ts +10 -0
- package/dist/acp.js +71 -6
- package/dist/agents.d.ts +2 -0
- package/dist/agents.js +4 -3
- package/dist/claude-local.d.ts +1 -0
- package/dist/claude-local.js +2 -2
- package/dist/client.js +1 -1
- package/dist/cloud-failure.d.ts +11 -0
- package/dist/cloud-failure.js +54 -0
- package/dist/employee-mcp.d.ts +8 -24
- package/dist/employee-mcp.js +24 -188
- package/dist/employee-tools.d.ts +5 -22
- package/dist/employee-tools.js +4 -292
- package/dist/index.d.ts +4 -0
- package/dist/index.js +31 -15
- package/dist/model-catalog.d.ts +1 -1
- package/dist/model-catalog.js +4 -0
- package/dist/presentation.js +1 -0
- package/dist/provider.js +4 -2
- package/dist/toolbridge-loop.js +3 -0
- package/dist/toolbridge-table.js +4 -1
- package/dist/toolbridge.d.ts +1 -0
- package/dist/toolbridge.js +1 -0
- package/package.json +10 -6
- package/helpers/ripgrep/LICENSE-vscode +0 -13
- package/helpers/ripgrep/PROVENANCE.md +0 -9
- package/helpers/ripgrep/darwin-arm64/rg +0 -0
- package/helpers/ripgrep/darwin-x64/rg +0 -0
- package/helpers/ripgrep/manifest.json +0 -56
- package/helpers/ripgrep/notices/COPYING-ripgrep +0 -3
- package/helpers/ripgrep/notices/DEPENDENCIES.json +0 -946
- package/helpers/ripgrep/notices/LICENCE-PCRE2.md +0 -103
- package/helpers/ripgrep/notices/LICENSE-MIT-ripgrep +0 -21
- package/helpers/ripgrep/notices/UNLICENSE-ripgrep +0 -24
- package/helpers/ripgrep/notices/aho-corasick-1.1.3/COPYING +0 -3
- package/helpers/ripgrep/notices/aho-corasick-1.1.3/LICENSE-MIT +0 -21
- package/helpers/ripgrep/notices/aho-corasick-1.1.3/UNLICENSE +0 -24
- package/helpers/ripgrep/notices/anyhow-1.0.100/LICENSE-APACHE +0 -176
- package/helpers/ripgrep/notices/anyhow-1.0.100/LICENSE-MIT +0 -23
- package/helpers/ripgrep/notices/arbitrary-1.4.2/LICENSE-APACHE +0 -201
- package/helpers/ripgrep/notices/arbitrary-1.4.2/LICENSE-MIT +0 -27
- package/helpers/ripgrep/notices/bstr-1.12.0/COPYING +0 -8
- package/helpers/ripgrep/notices/bstr-1.12.0/LICENSE-APACHE +0 -201
- package/helpers/ripgrep/notices/bstr-1.12.0/LICENSE-MIT +0 -21
- package/helpers/ripgrep/notices/cc-1.2.41/LICENSE-APACHE +0 -201
- package/helpers/ripgrep/notices/cc-1.2.41/LICENSE-MIT +0 -25
- package/helpers/ripgrep/notices/cfg-if-1.0.4/LICENSE-APACHE +0 -201
- package/helpers/ripgrep/notices/cfg-if-1.0.4/LICENSE-MIT +0 -25
- package/helpers/ripgrep/notices/crossbeam-channel-0.5.15/LICENSE-APACHE +0 -201
- package/helpers/ripgrep/notices/crossbeam-channel-0.5.15/LICENSE-MIT +0 -27
- package/helpers/ripgrep/notices/crossbeam-channel-0.5.15/LICENSE-THIRD-PARTY +0 -593
- package/helpers/ripgrep/notices/crossbeam-deque-0.8.6/LICENSE-APACHE +0 -201
- package/helpers/ripgrep/notices/crossbeam-deque-0.8.6/LICENSE-MIT +0 -27
- package/helpers/ripgrep/notices/crossbeam-epoch-0.9.18/LICENSE-APACHE +0 -201
- package/helpers/ripgrep/notices/crossbeam-epoch-0.9.18/LICENSE-MIT +0 -27
- package/helpers/ripgrep/notices/crossbeam-utils-0.8.21/LICENSE-APACHE +0 -201
- package/helpers/ripgrep/notices/crossbeam-utils-0.8.21/LICENSE-MIT +0 -27
- package/helpers/ripgrep/notices/derive_arbitrary-1.4.2/LICENSE-APACHE +0 -201
- package/helpers/ripgrep/notices/derive_arbitrary-1.4.2/LICENSE-MIT +0 -27
- package/helpers/ripgrep/notices/encoding_rs-0.8.35/COPYRIGHT +0 -17
- package/helpers/ripgrep/notices/encoding_rs-0.8.35/LICENSE-APACHE +0 -202
- package/helpers/ripgrep/notices/encoding_rs-0.8.35/LICENSE-MIT +0 -25
- package/helpers/ripgrep/notices/encoding_rs-0.8.35/LICENSE-WHATWG +0 -26
- package/helpers/ripgrep/notices/encoding_rs_io-0.1.7/COPYING +0 -8
- package/helpers/ripgrep/notices/encoding_rs_io-0.1.7/LICENSE-APACHE +0 -201
- package/helpers/ripgrep/notices/encoding_rs_io-0.1.7/LICENSE-MIT +0 -21
- package/helpers/ripgrep/notices/find-msvc-tools-0.1.4/LICENSE-APACHE +0 -201
- package/helpers/ripgrep/notices/find-msvc-tools-0.1.4/LICENSE-MIT +0 -25
- package/helpers/ripgrep/notices/getrandom-0.3.4/LICENSE-APACHE +0 -201
- package/helpers/ripgrep/notices/getrandom-0.3.4/LICENSE-MIT +0 -26
- package/helpers/ripgrep/notices/glob-0.3.3/LICENSE-APACHE +0 -201
- package/helpers/ripgrep/notices/glob-0.3.3/LICENSE-MIT +0 -25
- package/helpers/ripgrep/notices/itoa-1.0.15/LICENSE-APACHE +0 -176
- package/helpers/ripgrep/notices/itoa-1.0.15/LICENSE-MIT +0 -23
- package/helpers/ripgrep/notices/jobserver-0.1.34/LICENSE-APACHE +0 -201
- package/helpers/ripgrep/notices/jobserver-0.1.34/LICENSE-MIT +0 -25
- package/helpers/ripgrep/notices/lexopt-0.3.1/LICENSE +0 -19
- package/helpers/ripgrep/notices/libc-0.2.177/LICENSE-APACHE +0 -176
- package/helpers/ripgrep/notices/libc-0.2.177/LICENSE-MIT +0 -25
- package/helpers/ripgrep/notices/log-0.4.28/LICENSE-APACHE +0 -201
- package/helpers/ripgrep/notices/log-0.4.28/LICENSE-MIT +0 -25
- package/helpers/ripgrep/notices/memchr-2.7.6/COPYING +0 -3
- package/helpers/ripgrep/notices/memchr-2.7.6/LICENSE-MIT +0 -21
- package/helpers/ripgrep/notices/memchr-2.7.6/UNLICENSE +0 -24
- package/helpers/ripgrep/notices/memmap2-0.9.8/LICENSE-APACHE +0 -201
- package/helpers/ripgrep/notices/memmap2-0.9.8/LICENSE-MIT +0 -26
- package/helpers/ripgrep/notices/msvc_spectre_libs-0.1.3/LICENSE +0 -21
- package/helpers/ripgrep/notices/pcre2-0.2.11/COPYING +0 -3
- package/helpers/ripgrep/notices/pcre2-0.2.11/LICENSE-MIT +0 -21
- package/helpers/ripgrep/notices/pcre2-0.2.11/UNLICENSE +0 -24
- package/helpers/ripgrep/notices/pcre2-sys-0.2.10/COPYING +0 -3
- package/helpers/ripgrep/notices/pcre2-sys-0.2.10/LICENSE-MIT +0 -21
- package/helpers/ripgrep/notices/pcre2-sys-0.2.10/UNLICENSE +0 -24
- package/helpers/ripgrep/notices/pkg-config-0.3.32/LICENSE-APACHE +0 -201
- package/helpers/ripgrep/notices/pkg-config-0.3.32/LICENSE-MIT +0 -25
- package/helpers/ripgrep/notices/proc-macro2-1.0.101/LICENSE-APACHE +0 -176
- package/helpers/ripgrep/notices/proc-macro2-1.0.101/LICENSE-MIT +0 -23
- package/helpers/ripgrep/notices/quote-1.0.41/LICENSE-APACHE +0 -176
- package/helpers/ripgrep/notices/quote-1.0.41/LICENSE-MIT +0 -23
- package/helpers/ripgrep/notices/r-efi-5.3.0/AUTHORS +0 -74
- package/helpers/ripgrep/notices/regex-1.12.2/LICENSE-APACHE +0 -201
- package/helpers/ripgrep/notices/regex-1.12.2/LICENSE-MIT +0 -25
- package/helpers/ripgrep/notices/regex-automata-0.4.13/LICENSE-APACHE +0 -201
- package/helpers/ripgrep/notices/regex-automata-0.4.13/LICENSE-MIT +0 -25
- package/helpers/ripgrep/notices/regex-syntax-0.8.8/LICENSE-APACHE +0 -201
- package/helpers/ripgrep/notices/regex-syntax-0.8.8/LICENSE-MIT +0 -25
- package/helpers/ripgrep/notices/regex-syntax-0.8.8/src/unicode_tables/LICENSE-UNICODE +0 -57
- package/helpers/ripgrep/notices/ryu-1.0.20/LICENSE-APACHE +0 -176
- package/helpers/ripgrep/notices/ryu-1.0.20/LICENSE-BOOST +0 -23
- package/helpers/ripgrep/notices/same-file-1.0.6/COPYING +0 -3
- package/helpers/ripgrep/notices/same-file-1.0.6/LICENSE-MIT +0 -21
- package/helpers/ripgrep/notices/same-file-1.0.6/UNLICENSE +0 -24
- package/helpers/ripgrep/notices/serde-1.0.228/LICENSE-APACHE +0 -176
- package/helpers/ripgrep/notices/serde-1.0.228/LICENSE-MIT +0 -23
- package/helpers/ripgrep/notices/serde_core-1.0.228/LICENSE-APACHE +0 -176
- package/helpers/ripgrep/notices/serde_core-1.0.228/LICENSE-MIT +0 -23
- package/helpers/ripgrep/notices/serde_derive-1.0.228/LICENSE-APACHE +0 -176
- package/helpers/ripgrep/notices/serde_derive-1.0.228/LICENSE-MIT +0 -23
- package/helpers/ripgrep/notices/serde_json-1.0.145/LICENSE-APACHE +0 -176
- package/helpers/ripgrep/notices/serde_json-1.0.145/LICENSE-MIT +0 -23
- package/helpers/ripgrep/notices/shlex-1.3.0/LICENSE-APACHE +0 -13
- package/helpers/ripgrep/notices/shlex-1.3.0/LICENSE-MIT +0 -21
- package/helpers/ripgrep/notices/syn-2.0.106/LICENSE-APACHE +0 -176
- package/helpers/ripgrep/notices/syn-2.0.106/LICENSE-MIT +0 -23
- package/helpers/ripgrep/notices/termcolor-1.4.1/COPYING +0 -3
- package/helpers/ripgrep/notices/termcolor-1.4.1/LICENSE-MIT +0 -21
- package/helpers/ripgrep/notices/termcolor-1.4.1/UNLICENSE +0 -24
- package/helpers/ripgrep/notices/textwrap-0.16.2/LICENSE +0 -21
- package/helpers/ripgrep/notices/tikv-jemalloc-sys-0.6.0+5.3.0-1-ge13ca993e8ccb9ba9847cc330696e02839f328f7/LICENSE-APACHE +0 -201
- package/helpers/ripgrep/notices/tikv-jemalloc-sys-0.6.0+5.3.0-1-ge13ca993e8ccb9ba9847cc330696e02839f328f7/LICENSE-MIT +0 -25
- package/helpers/ripgrep/notices/tikv-jemalloc-sys-0.6.0+5.3.0-1-ge13ca993e8ccb9ba9847cc330696e02839f328f7/jemalloc/COPYING +0 -27
- package/helpers/ripgrep/notices/tikv-jemallocator-0.6.0/LICENSE-APACHE +0 -201
- package/helpers/ripgrep/notices/tikv-jemallocator-0.6.0/LICENSE-MIT +0 -25
- package/helpers/ripgrep/notices/unicode-ident-1.0.19/LICENSE-APACHE +0 -176
- package/helpers/ripgrep/notices/unicode-ident-1.0.19/LICENSE-MIT +0 -23
- package/helpers/ripgrep/notices/unicode-ident-1.0.19/LICENSE-UNICODE +0 -39
- package/helpers/ripgrep/notices/walkdir-2.5.0/COPYING +0 -3
- package/helpers/ripgrep/notices/walkdir-2.5.0/LICENSE-MIT +0 -21
- package/helpers/ripgrep/notices/walkdir-2.5.0/UNLICENSE +0 -24
- package/helpers/ripgrep/notices/wasip2-1.0.1+wasi-0.2.4/LICENSE-APACHE +0 -201
- package/helpers/ripgrep/notices/wasip2-1.0.1+wasi-0.2.4/LICENSE-Apache-2.0_WITH_LLVM-exception +0 -220
- package/helpers/ripgrep/notices/wasip2-1.0.1+wasi-0.2.4/LICENSE-MIT +0 -23
- package/helpers/ripgrep/notices/winapi-util-0.1.11/COPYING +0 -3
- package/helpers/ripgrep/notices/winapi-util-0.1.11/LICENSE-MIT +0 -21
- package/helpers/ripgrep/notices/winapi-util-0.1.11/UNLICENSE +0 -24
- package/helpers/ripgrep/notices/windows-link-0.2.1/license-apache-2.0 +0 -201
- package/helpers/ripgrep/notices/windows-link-0.2.1/license-mit +0 -21
- package/helpers/ripgrep/notices/windows-sys-0.61.2/license-apache-2.0 +0 -201
- package/helpers/ripgrep/notices/windows-sys-0.61.2/license-mit +0 -21
- package/helpers/ripgrep/notices/wit-bindgen-0.46.0/LICENSE-APACHE +0 -201
- package/helpers/ripgrep/notices/wit-bindgen-0.46.0/LICENSE-Apache-2.0_WITH_LLVM-exception +0 -220
- package/helpers/ripgrep/notices/wit-bindgen-0.46.0/LICENSE-MIT +0 -23
package/dist/employee-mcp.js
CHANGED
|
@@ -1,194 +1,30 @@
|
|
|
1
|
+
import { openEmployeeMcp as open, EmployeeSocketTransport as SocketTransport, EMPLOYEE_MCP_TOOLS } from '@ai-workbench/employee-tools';
|
|
2
|
+
import { NeedError } from "./remote.js";
|
|
1
3
|
import { closeCode } from "./close-diagnostics.js";
|
|
2
|
-
import { createServer } from 'node:net';
|
|
3
|
-
import { mkdtemp, lstat, rm, access } from 'node:fs/promises';
|
|
4
|
-
import { constants } from 'node:fs';
|
|
5
|
-
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
|
|
6
|
-
import { CallToolRequestSchema, ListToolsRequestSchema, JSONRPCMessageSchema } from '@modelcontextprotocol/sdk/types.js';
|
|
7
|
-
import { createEmployeeTools } from "./employee-tools.js";
|
|
8
4
|
import { employeeRouting } from "./employee-relay.js";
|
|
9
|
-
|
|
10
|
-
const
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
schema('Read', { file_path: string, offset: integer, limit: integer, pages: string }, ['file_path']),
|
|
14
|
-
schema('Write', { file_path: string, content: string }, ['file_path', 'content']),
|
|
15
|
-
schema('Edit', { file_path: string, old_string: string, new_string: string, replace_all: boolean }, ['file_path', 'old_string', 'new_string']),
|
|
16
|
-
schema('Glob', { pattern: string, path: string }, ['pattern']),
|
|
17
|
-
schema('Grep', { pattern: string, path: string, glob: string, output_mode: string, '-B': integer, '-A': integer, '-C': integer, '-n': boolean, '-i': boolean, type: string, head_limit: integer, offset: integer, multiline: boolean }, ['pattern']),
|
|
18
|
-
schema('Bash', { command: string, timeout: integer, description: string, run_in_background: boolean, dangerouslyDisableSandbox: boolean }, ['command']),
|
|
19
|
-
];
|
|
20
|
-
/** Same newline-delimited MCP messages as stdio, over an employee-only Unix stream. */
|
|
21
|
-
export class EmployeeSocketTransport {
|
|
22
|
-
socket;
|
|
23
|
-
maxBytes;
|
|
24
|
-
observeClose;
|
|
25
|
-
onclose;
|
|
26
|
-
onerror;
|
|
27
|
-
onmessage;
|
|
28
|
-
pending = Buffer.alloc(0);
|
|
29
|
-
ended = false;
|
|
30
|
-
failure;
|
|
31
|
-
get error() { return this.failure; }
|
|
32
|
-
lifetime = new AbortController();
|
|
33
|
-
signal = this.lifetime.signal;
|
|
34
|
-
constructor(socket, maxBytes, observeClose) {
|
|
35
|
-
this.socket = socket;
|
|
36
|
-
this.maxBytes = maxBytes;
|
|
37
|
-
this.observeClose = observeClose;
|
|
38
|
-
}
|
|
39
|
-
failed(error) { this.failure ??= error; try {
|
|
40
|
-
this.onerror?.(error);
|
|
41
|
-
}
|
|
42
|
-
catch {
|
|
43
|
-
console.warn('EMPLOYEE_MCP_DIAGNOSTIC_DROPPED');
|
|
44
|
-
}
|
|
45
|
-
finally {
|
|
46
|
-
void this.close({ layer: 'relay', initiator: 'mcp.socket-failure', reason: closeCode(error, 'EMPLOYEE_MCP_PROTOCOL_OR_SOCKET_ERROR'), action: 'failure' });
|
|
47
|
-
} }
|
|
48
|
-
async start() {
|
|
49
|
-
this.socket.on('error', error => this.failed(error));
|
|
50
|
-
this.socket.on('close', () => void this.close({ layer: 'relay', initiator: 'mcp.socket-peer-close', reason: 'MCP_SOCKET_CLOSED', action: 'close' }));
|
|
51
|
-
this.socket.on('data', (bytes) => {
|
|
52
|
-
try {
|
|
53
|
-
let cursor = 0;
|
|
54
|
-
while (cursor < bytes.length) {
|
|
55
|
-
const newline = bytes.indexOf(10, cursor), end = newline < 0 ? bytes.length : newline;
|
|
56
|
-
if (this.pending.length + end - cursor > this.maxBytes)
|
|
57
|
-
throw new NeedError('EMPLOYEE_MCP_MESSAGE_TOO_LARGE', '本机工具消息超过acpMaxMessageBytes,未截断。', '调整具名会话消息容量');
|
|
58
|
-
this.pending = Buffer.concat([this.pending, bytes.subarray(cursor, end)]);
|
|
59
|
-
cursor = newline < 0 ? bytes.length : newline + 1;
|
|
60
|
-
if (newline >= 0) {
|
|
61
|
-
const line = new TextDecoder('utf8', { fatal: true }).decode(this.pending);
|
|
62
|
-
this.pending = Buffer.alloc(0);
|
|
63
|
-
this.onmessage?.(JSONRPCMessageSchema.parse(JSON.parse(line)));
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
catch (error) {
|
|
68
|
-
this.failed(error);
|
|
69
|
-
}
|
|
70
|
-
});
|
|
71
|
-
}
|
|
72
|
-
async send(message) {
|
|
73
|
-
if (this.ended)
|
|
74
|
-
throw new Error('EMPLOYEE_MCP_CLOSED');
|
|
75
|
-
const wire = Buffer.from(JSON.stringify(message) + '\n');
|
|
76
|
-
if (wire.length - 1 > this.maxBytes) {
|
|
77
|
-
await this.close({ layer: 'relay', initiator: 'mcp.send-capacity', reason: 'EMPLOYEE_MCP_MESSAGE_TOO_LARGE', action: 'close' });
|
|
78
|
-
throw new Error('EMPLOYEE_MCP_MESSAGE_TOO_LARGE');
|
|
79
|
-
}
|
|
80
|
-
await new Promise((yes, no) => this.socket.write(wire, error => error ? no(error) : yes()));
|
|
81
|
-
}
|
|
82
|
-
async close(cause = { layer: 'relay', initiator: 'mcp.transport-close', reason: 'MCP_TRANSPORT_CLOSED', action: 'close' }) { try {
|
|
83
|
-
this.observeClose?.({ ...cause, changes: cause.changes ?? [{ field: 'mcp.ended', before: this.ended, after: true }] });
|
|
84
|
-
}
|
|
85
|
-
catch {
|
|
86
|
-
console.warn('EMPLOYEE_MCP_DIAGNOSTIC_DROPPED');
|
|
87
|
-
} if (this.ended)
|
|
88
|
-
return; this.ended = true; this.lifetime.abort(); this.socket.destroy(); try {
|
|
89
|
-
this.onclose?.();
|
|
90
|
-
}
|
|
91
|
-
catch {
|
|
92
|
-
console.warn('EMPLOYEE_MCP_CLOSE_OBSERVER_FAILED');
|
|
93
|
-
} }
|
|
5
|
+
export { EMPLOYEE_MCP_TOOLS };
|
|
6
|
+
const runtime = { NeedError, closeCode };
|
|
7
|
+
export class EmployeeSocketTransport extends SocketTransport {
|
|
8
|
+
constructor(socket, maxBytes, observeClose) { super(socket, maxBytes, runtime, observeClose); }
|
|
94
9
|
}
|
|
95
10
|
export async function openEmployeeMcp(options) {
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
throw new NeedError('EMPLOYEE_MCP_STORAGE_INVALID', '本机工具私有通道不可用。', '当前员工的700私有Unix目录');
|
|
107
|
-
}
|
|
108
|
-
catch (error) {
|
|
109
|
-
options.closeDiagnostics?.record({ layer: 'relay', initiator: 'mcp.storage-failure', reason: closeCode(error), action: 'failure' });
|
|
110
|
-
for (const clean of [() => tools.close({ layer: 'executor', initiator: 'mcp.storage-cleanup', reason: 'MCP_STORAGE_FAILED', action: 'close' }), () => rm(root, { recursive: true })]) {
|
|
111
|
-
try {
|
|
112
|
-
await clean();
|
|
113
|
-
}
|
|
114
|
-
catch {
|
|
115
|
-
console.warn('EMPLOYEE_MCP_CLEANUP_FAILED');
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
throw error;
|
|
119
|
-
}
|
|
120
|
-
const clients = new Set(), transports = new Set();
|
|
121
|
-
let closing;
|
|
122
|
-
function observeUsing(event, sink) { try {
|
|
123
|
-
sink(event);
|
|
124
|
-
}
|
|
125
|
-
catch {
|
|
126
|
-
console.warn('EMPLOYEE_MCP_DIAGNOSTIC_DROPPED');
|
|
127
|
-
} }
|
|
128
|
-
function observe(event) { observeUsing(event, options.detail); }
|
|
129
|
-
const server = createServer(socket => {
|
|
130
|
-
if (closing || options.lease.signal.aborted) {
|
|
131
|
-
options.closeDiagnostics?.record({ layer: 'relay', initiator: 'mcp.reject-late-socket', reason: 'MCP_CLOSING', action: 'close' });
|
|
132
|
-
socket.destroy();
|
|
133
|
-
return;
|
|
134
|
-
}
|
|
135
|
-
const observeConnection = options.closeDiagnostics?.child().capture();
|
|
136
|
-
const mcp = new Server({ name: 'ai-workbench-employee', version: '0.8.14' }, { capabilities: { tools: {} } }), transport = new EmployeeSocketTransport(socket, options.maxBytes, observeConnection);
|
|
137
|
-
clients.add(mcp);
|
|
138
|
-
transports.add(transport);
|
|
139
|
-
mcp.setRequestHandler(ListToolsRequestSchema, async () => ({ tools: [...EMPLOYEE_MCP_TOOLS, ...options.bridge?.tools ?? []] }));
|
|
140
|
-
mcp.setRequestHandler(CallToolRequestSchema, async (request, extra) => {
|
|
141
|
-
const active = options.activeSignal();
|
|
142
|
-
if (!active || active.aborted)
|
|
143
|
-
return { isError: true, content: [{ type: 'text', text: 'EMPLOYEE_NO_ACTIVE_TURN:当前没有已接纳的用户轮次,本机工具未执行。' }] };
|
|
144
|
-
const signal = AbortSignal.any([active, extra.signal, transport.signal, options.lease.signal]);
|
|
145
|
-
if (options.bridge?.isBridge(request.params.name))
|
|
146
|
-
return options.bridge.call(request.params.name, request.params.arguments ?? {}, signal);
|
|
147
|
-
let sink;
|
|
148
|
-
try {
|
|
149
|
-
sink = options.captureDetail?.() ?? options.detail;
|
|
150
|
-
}
|
|
151
|
-
catch {
|
|
152
|
-
console.warn('EMPLOYEE_MCP_DIAGNOSTIC_DROPPED');
|
|
153
|
-
}
|
|
154
|
-
return tools.call(request.params.name, request.params.arguments ?? {}, signal, event => { if (sink)
|
|
155
|
-
observeUsing(event, sink); });
|
|
156
|
-
});
|
|
157
|
-
mcp.onerror = () => { observeConnection?.({ layer: 'relay', initiator: 'mcp.server-error', reason: 'EMPLOYEE_MCP_PROTOCOL_OR_TRANSPORT_ERROR', action: 'failure' }); observe({ kind: 'session', severity: 'warning', code: 'CLAUDE_EMPLOYEE_MCP_UNKNOWN', summary: '本机工具连接中断,在途结果未知,没有重放;恢复后可在原会话继续。' }); };
|
|
158
|
-
mcp.onclose = () => { observeConnection?.({ layer: 'relay', initiator: 'mcp.client-closed', reason: 'MCP_CLIENT_CLOSED', action: 'close' }); clients.delete(mcp); transports.delete(transport); };
|
|
159
|
-
void mcp.connect(transport).catch(error => { observeConnection?.({ layer: 'relay', initiator: 'mcp.connect-failure', reason: closeCode(error, 'MCP_CONNECT_FAILED'), action: 'close' }); socket.destroy(); });
|
|
160
|
-
});
|
|
161
|
-
const close = (cause = { layer: 'relay', initiator: 'mcp.close', reason: 'MCP_CLOSE_REQUESTED', action: 'close' }) => {
|
|
162
|
-
options.closeDiagnostics?.record({ ...cause, changes: cause.changes ?? [{ field: 'mcp.closing', before: !!closing, after: true }] });
|
|
163
|
-
return closing ??= Promise.resolve().then(async () => {
|
|
164
|
-
options.lease.signal.removeEventListener('abort', aborted);
|
|
165
|
-
const clean = async (operation) => { try {
|
|
166
|
-
await operation();
|
|
11
|
+
// Compose with the existing request-dispatch port without changing the frozen
|
|
12
|
+
// shared executor or its six schemas. No approval/executor is entered here.
|
|
13
|
+
const writes = new Set(['Write', 'Edit', 'Bash']);
|
|
14
|
+
const guarded = options.readOnly ? {
|
|
15
|
+
get tools() { return options.bridge?.tools ?? []; },
|
|
16
|
+
isBridge: (name) => writes.has(name) && options.readOnly() || options.bridge?.isBridge(name) === true,
|
|
17
|
+
call: async (name, input, signal) => {
|
|
18
|
+
if (writes.has(name)) {
|
|
19
|
+
options.detail({ kind: 'tool', severity: 'info', code: 'EMPLOYEE_READ_ONLY', summary: '当前为只读档,员工写操作已拒绝,未请求审批。', payload: { tool: name } });
|
|
20
|
+
return { isError: true, content: [{ type: 'text', text: 'EMPLOYEE_READ_ONLY:当前为只读档,此员工写操作未执行。' }] };
|
|
167
21
|
}
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
options.closeDiagnostics?.record({ layer: 'relay', initiator: 'mcp.server-close', reason: 'MCP_CLEANUP', action: 'close' });
|
|
177
|
-
if (server.listening)
|
|
178
|
-
await clean(() => new Promise((yes, no) => server.close(error => error ? no(error) : yes())));
|
|
179
|
-
await clean(() => rm(root, { recursive: true }));
|
|
180
|
-
});
|
|
181
|
-
};
|
|
182
|
-
const aborted = () => { void close({ layer: 'endpoint', initiator: 'mcp.lease-abort', reason: closeCode(options.lease.signal.reason, 'EMPLOYEE_CONTEXT_CHANGED'), action: 'abort' }).catch(() => console.warn('EMPLOYEE_MCP_CLEANUP_FAILED')); };
|
|
183
|
-
try {
|
|
184
|
-
await new Promise((yes, no) => { server.once('error', no); server.listen(socketPath, () => { server.removeListener('error', no); yes(); }); });
|
|
185
|
-
options.lease.signal.addEventListener('abort', aborted, { once: true });
|
|
186
|
-
if (options.lease.signal.aborted)
|
|
187
|
-
throw options.lease.signal.reason;
|
|
188
|
-
return { routing: employeeRouting(options.lease.target, socketPath, options.maxBytes), signal: options.lease.signal, close };
|
|
189
|
-
}
|
|
190
|
-
catch (error) {
|
|
191
|
-
await close({ layer: 'relay', initiator: 'mcp.open-failure', reason: closeCode(error, 'MCP_OPEN_FAILED'), action: 'failure' });
|
|
192
|
-
throw error;
|
|
193
|
-
}
|
|
22
|
+
return options.bridge.call(name, input, signal);
|
|
23
|
+
},
|
|
24
|
+
} : undefined;
|
|
25
|
+
return open({ get bridge() { return guarded ?? options.bridge; }, get closeDiagnostics() { return options.closeDiagnostics; },
|
|
26
|
+
context: { get cwd() { return options.lease.target.local.directory.path; }, get shell() { return options.lease.target.endpoint.endpointShell; }, get uid() { return options.lease.target.endpoint.remoteUid; }, get signal() { return options.lease.signal; }, close: cause => options.lease.close(cause) },
|
|
27
|
+
routing: socketPath => employeeRouting(options.lease.target, socketPath, options.maxBytes),
|
|
28
|
+
get maxBytes() { return options.maxBytes; }, get killGraceMs() { return options.killGraceMs; }, activeSignal: () => options.activeSignal(),
|
|
29
|
+
get detail() { return options.detail; }, get captureDetail() { return options.captureDetail ? () => options.captureDetail() : undefined; }, get runShell() { return options.runShell; }, }, runtime);
|
|
194
30
|
}
|
package/dist/employee-tools.d.ts
CHANGED
|
@@ -1,23 +1,7 @@
|
|
|
1
|
-
import { type
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
export type EmployeeToolName = typeof EMPLOYEE_TOOL_NAMES[number];
|
|
6
|
-
interface Options {
|
|
7
|
-
closeDiagnostics?: CloseDiagnostics;
|
|
8
|
-
cwd: string;
|
|
9
|
-
shell: string;
|
|
10
|
-
uid: number;
|
|
11
|
-
maxBytes: number;
|
|
12
|
-
killGraceMs: number;
|
|
13
|
-
validate: (signal?: AbortSignal) => Promise<void>;
|
|
14
|
-
detail: (event: DetailObservation) => void;
|
|
15
|
-
rgPath?: string;
|
|
16
|
-
runShell?: EmployeeShell;
|
|
17
|
-
}
|
|
18
|
-
/** Executed only in the employee DSH process; never creates a host fallback. */
|
|
19
|
-
export declare function createEmployeeTools(options: Options): {
|
|
20
|
-
call(name: string, input: unknown, requestSignal?: AbortSignal, observer?: Options["detail"]): Promise<{
|
|
1
|
+
import { type EmployeeToolsOptions } from '@ai-workbench/employee-tools';
|
|
2
|
+
export { EMPLOYEE_TOOL_NAMES, type EmployeeToolName } from '@ai-workbench/employee-tools';
|
|
3
|
+
export declare function createEmployeeTools(options: EmployeeToolsOptions): {
|
|
4
|
+
call(name: string, input: unknown, requestSignal?: AbortSignal, observer?: EmployeeToolsOptions["detail"]): Promise<{
|
|
21
5
|
[x: string]: unknown;
|
|
22
6
|
content: ({
|
|
23
7
|
type: "text";
|
|
@@ -114,6 +98,5 @@ export declare function createEmployeeTools(options: Options): {
|
|
|
114
98
|
} | undefined;
|
|
115
99
|
isError?: boolean | undefined;
|
|
116
100
|
}>;
|
|
117
|
-
close(cause?: CloseCause): Promise<void>;
|
|
101
|
+
close(cause?: import("@ai-workbench/employee-tools").CloseCause): Promise<void>;
|
|
118
102
|
};
|
|
119
|
-
export {};
|
package/dist/employee-tools.js
CHANGED
|
@@ -1,293 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { randomUUID, createHash } from 'node:crypto';
|
|
3
|
-
import { readFile, writeFile, mkdir, glob, lstat, access, realpath, stat } from 'node:fs/promises';
|
|
4
|
-
import { constants } from 'node:fs';
|
|
5
|
-
import { resolve, dirname, isAbsolute, basename, join } from 'node:path';
|
|
6
|
-
import { fileURLToPath } from 'node:url';
|
|
1
|
+
import { createEmployeeTools as create } from '@ai-workbench/employee-tools';
|
|
7
2
|
import { NeedError } from "./remote.js";
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
function
|
|
11
|
-
throw failure('EMPLOYEE_ARGUMENT_INVALID'); return value; }
|
|
12
|
-
function string(args, key, required = true) {
|
|
13
|
-
const v = args[key];
|
|
14
|
-
if (v === undefined && !required)
|
|
15
|
-
return;
|
|
16
|
-
if (typeof v !== 'string' || v.includes('\0') || (required && !v))
|
|
17
|
-
throw failure('EMPLOYEE_ARGUMENT_INVALID');
|
|
18
|
-
return v;
|
|
19
|
-
}
|
|
20
|
-
function integer(args, key, min) { const v = args[key]; if (v === undefined)
|
|
21
|
-
return; if (!Number.isSafeInteger(v) || v < min)
|
|
22
|
-
throw failure('EMPLOYEE_ARGUMENT_INVALID'); return v; }
|
|
23
|
-
function boolean(args, key) { const v = args[key]; if (v === undefined)
|
|
24
|
-
return; if (typeof v !== 'boolean')
|
|
25
|
-
throw failure('EMPLOYEE_ARGUMENT_INVALID'); return v; }
|
|
26
|
-
function fields(args, allowed) { if (Object.keys(args).some(k => !allowed.includes(k)))
|
|
27
|
-
throw failure('EMPLOYEE_ARGUMENT_UNSUPPORTED'); }
|
|
28
|
-
function text(value) { return { content: [{ type: 'text', text: value }] }; }
|
|
29
|
-
// Shared across this plugin's sessions: protect the entire read/modify/write, not just writeFile.
|
|
30
|
-
const fileQueues = new Map();
|
|
31
|
-
async function serialized(key, work) {
|
|
32
|
-
const previous = fileQueues.get(key) ?? Promise.resolve();
|
|
33
|
-
let release = () => { };
|
|
34
|
-
const next = new Promise(yes => { release = yes; });
|
|
35
|
-
fileQueues.set(key, next);
|
|
36
|
-
await previous;
|
|
37
|
-
try {
|
|
38
|
-
return await work();
|
|
39
|
-
}
|
|
40
|
-
finally {
|
|
41
|
-
release();
|
|
42
|
-
if (fileQueues.get(key) === next)
|
|
43
|
-
fileQueues.delete(key);
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
async function canonicalFile(path) {
|
|
47
|
-
try {
|
|
48
|
-
return await realpath(path);
|
|
49
|
-
}
|
|
50
|
-
catch (error) {
|
|
51
|
-
if (error.code !== 'ENOENT')
|
|
52
|
-
throw error;
|
|
53
|
-
const parent = dirname(path);
|
|
54
|
-
if (parent === path)
|
|
55
|
-
throw error;
|
|
56
|
-
return join(await canonicalFile(parent), basename(path));
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
async function mutate(path, signal, work) {
|
|
60
|
-
return serialized('path:' + await canonicalFile(path), async () => {
|
|
61
|
-
signal.throwIfAborted();
|
|
62
|
-
let inode;
|
|
63
|
-
try {
|
|
64
|
-
const info = await stat(path);
|
|
65
|
-
inode = `inode:${info.dev}:${info.ino}`;
|
|
66
|
-
}
|
|
67
|
-
catch (error) {
|
|
68
|
-
if (error.code !== 'ENOENT')
|
|
69
|
-
throw error;
|
|
70
|
-
}
|
|
71
|
-
const perform = () => { signal.throwIfAborted(); return work(); };
|
|
72
|
-
return inode ? serialized(inode, perform) : perform();
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
async function bundledRipgrep() {
|
|
76
|
-
if (process.platform !== 'darwin' || !['arm64', 'x64'].includes(process.arch))
|
|
77
|
-
throw failure('EMPLOYEE_PLATFORM_UNSUPPORTED');
|
|
78
|
-
const root = new URL('./helpers/ripgrep/', import.meta.resolve('@ai-workbench/remote-agent/package.json'));
|
|
79
|
-
const manifest = JSON.parse(await readFile(new URL('manifest.json', root), 'utf8'));
|
|
80
|
-
const arch = 'darwin-' + process.arch, path = fileURLToPath(new URL(arch + '/rg', root)), entry = manifest.files[arch];
|
|
81
|
-
const info = await lstat(path);
|
|
82
|
-
if (!entry || !info.isFile() || info.isSymbolicLink() || info.size !== entry.bytes || createHash('sha256').update(await readFile(path)).digest('hex') !== entry.sha256)
|
|
83
|
-
throw failure('EMPLOYEE_SEARCH_COMPONENT_INVALID');
|
|
84
|
-
await access(path, constants.X_OK);
|
|
85
|
-
return path;
|
|
86
|
-
}
|
|
87
|
-
/** Executed only in the employee DSH process; never creates a host fallback. */
|
|
88
|
-
export function createEmployeeTools(options) {
|
|
89
|
-
if (!isAbsolute(options.cwd) || !isAbsolute(options.shell) || !Number.isSafeInteger(options.uid) || options.uid !== process.getuid?.() || !Number.isSafeInteger(options.maxBytes) || options.maxBytes < 1 || !Number.isSafeInteger(options.killGraceMs) || options.killGraceMs < 0)
|
|
90
|
-
throw failure('EMPLOYEE_CONTEXT_INVALID');
|
|
91
|
-
const lifetime = new AbortController(), children = new Set(), calls = new Set();
|
|
92
|
-
const path = (value) => resolve(options.cwd, value);
|
|
93
|
-
const capacity = (value) => { if (Buffer.byteLength(value) > options.maxBytes)
|
|
94
|
-
throw failure('EMPLOYEE_OUTPUT_TOO_LARGE'); };
|
|
95
|
-
async function run(command, args, signal, detail, timeout) {
|
|
96
|
-
signal.throwIfAborted();
|
|
97
|
-
const diagnostics = options.closeDiagnostics?.child(detail);
|
|
98
|
-
const operation = new Promise((yes, no) => {
|
|
99
|
-
const child = spawn(command, args, { cwd: options.cwd, env: process.env, stdio: ['pipe', 'pipe', 'pipe'] });
|
|
100
|
-
child.stdin.end();
|
|
101
|
-
let bytes = 0, stdout = [], stderr = [], failed, killTimer, timeoutTimer;
|
|
102
|
-
const kill = (which) => { diagnostics?.record({ layer: 'executor', initiator: 'tools.process-kill', reason: failed?.code ?? 'PROCESS_CLEANUP', action: 'terminate', signal: which }); if (child.exitCode === null && child.signalCode === null)
|
|
103
|
-
child.kill(which); };
|
|
104
|
-
const stop = (code) => { diagnostics?.record({ layer: 'executor', initiator: 'tools.process-stop', reason: code, action: 'terminate', changes: [{ field: 'tools.stop-code', before: failed?.code ?? null, after: failed?.code ?? code }] }); if (failed)
|
|
105
|
-
return; failed = failure(code); kill('SIGTERM'); killTimer = setTimeout(() => kill('SIGKILL'), options.killGraceMs); killTimer.unref(); };
|
|
106
|
-
const aborted = () => stop('EMPLOYEE_CANCELLED_UNKNOWN');
|
|
107
|
-
const collect = (dest, chunk) => { bytes += chunk.length; if (bytes > options.maxBytes) {
|
|
108
|
-
stop('EMPLOYEE_OUTPUT_TOO_LARGE_UNKNOWN');
|
|
109
|
-
return;
|
|
110
|
-
} dest.push(chunk); };
|
|
111
|
-
child.stdout.on('data', (b) => collect(stdout, b));
|
|
112
|
-
child.stderr.on('data', (b) => collect(stderr, b));
|
|
113
|
-
child.on('spawn', () => { try {
|
|
114
|
-
detail({ kind: 'session', severity: 'info', code: 'CLAUDE_EMPLOYEE_PROCESS', summary: '本机工具子进程', payload: { childPid: child.pid ?? null, uid: options.uid, cwd: options.cwd } });
|
|
115
|
-
}
|
|
116
|
-
catch {
|
|
117
|
-
stop('CONVERSATION_DETAILS_FAILED');
|
|
118
|
-
} if (signal.aborted)
|
|
119
|
-
aborted(); });
|
|
120
|
-
child.on('error', e => { diagnostics?.record({ layer: 'executor', initiator: 'tools.process-error', reason: e.code === 'ENOENT' ? 'EMPLOYEE_COMMAND_UNAVAILABLE' : 'EMPLOYEE_PROCESS_FAILED', action: 'failure' }); failed ??= failure(e.code === 'ENOENT' ? 'EMPLOYEE_COMMAND_UNAVAILABLE' : 'EMPLOYEE_PROCESS_FAILED'); });
|
|
121
|
-
child.on('close', (exitCode, exitSignal) => { diagnostics?.record({ layer: 'executor', initiator: 'tools.process-close', reason: failed?.code ?? 'PROCESS_EXIT', action: 'exit', exitCode, signal: exitSignal }); signal.removeEventListener('abort', aborted); if (killTimer)
|
|
122
|
-
clearTimeout(killTimer); if (timeoutTimer)
|
|
123
|
-
clearTimeout(timeoutTimer); failed ? no(failed) : yes({ stdout: Buffer.concat(stdout).toString('utf8'), stderr: Buffer.concat(stderr).toString('utf8'), exitCode, signal: exitSignal }); });
|
|
124
|
-
signal.addEventListener('abort', aborted, { once: true });
|
|
125
|
-
if (signal.aborted)
|
|
126
|
-
aborted();
|
|
127
|
-
if (timeout !== undefined) {
|
|
128
|
-
timeoutTimer = setTimeout(() => stop('EMPLOYEE_TIMEOUT_UNKNOWN'), timeout);
|
|
129
|
-
timeoutTimer.unref();
|
|
130
|
-
}
|
|
131
|
-
});
|
|
132
|
-
children.add(operation);
|
|
133
|
-
try {
|
|
134
|
-
return await operation;
|
|
135
|
-
}
|
|
136
|
-
finally {
|
|
137
|
-
children.delete(operation);
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
async function execute(name, args, signal, detail) {
|
|
141
|
-
const check = () => signal.throwIfAborted();
|
|
142
|
-
if (name === 'Read') {
|
|
143
|
-
fields(args, ['file_path', 'offset', 'limit', 'pages']);
|
|
144
|
-
if (args.pages !== undefined)
|
|
145
|
-
throw failure('EMPLOYEE_READ_PDF_UNSUPPORTED');
|
|
146
|
-
const target = path(string(args, 'file_path')), offset = integer(args, 'offset', 1) ?? 1, limit = integer(args, 'limit', 1);
|
|
147
|
-
const info = await lstat(target);
|
|
148
|
-
if (info.size > options.maxBytes)
|
|
149
|
-
throw failure('EMPLOYEE_OUTPUT_TOO_LARGE');
|
|
150
|
-
const bytes = await readFile(target, { signal });
|
|
151
|
-
check();
|
|
152
|
-
capacity(bytes);
|
|
153
|
-
const magic = bytes.subarray(0, 16), mime = magic[0] === 137 && magic.subarray(1, 4).toString() === 'PNG' ? 'image/png' : magic[0] === 255 && magic[1] === 216 ? 'image/jpeg' : magic.subarray(0, 4).toString() === 'RIFF' && magic.subarray(8, 12).toString() === 'WEBP' ? 'image/webp' : magic.subarray(0, 3).toString() === 'GIF' ? 'image/gif' : undefined;
|
|
154
|
-
if (mime) {
|
|
155
|
-
if (args.offset !== undefined || args.limit !== undefined)
|
|
156
|
-
throw failure('EMPLOYEE_IMAGE_RANGE_UNSUPPORTED');
|
|
157
|
-
return { content: [{ type: 'image', mimeType: mime, data: bytes.toString('base64') }] };
|
|
158
|
-
}
|
|
159
|
-
let contents;
|
|
160
|
-
try {
|
|
161
|
-
contents = new TextDecoder('utf8', { fatal: true }).decode(bytes);
|
|
162
|
-
}
|
|
163
|
-
catch {
|
|
164
|
-
throw failure('EMPLOYEE_READ_BINARY_UNSUPPORTED');
|
|
165
|
-
}
|
|
166
|
-
if (contents.includes('\0') || contents.startsWith('%PDF-'))
|
|
167
|
-
throw failure('EMPLOYEE_READ_BINARY_UNSUPPORTED');
|
|
168
|
-
const lines = contents.split('\n'), selected = lines.slice(offset - 1, limit === undefined ? undefined : offset - 1 + limit);
|
|
169
|
-
return text(selected.map((line, i) => String(offset + i) + '\t' + line).join('\n'));
|
|
170
|
-
}
|
|
171
|
-
if (name === 'Write') {
|
|
172
|
-
fields(args, ['file_path', 'content']);
|
|
173
|
-
const target = path(string(args, 'file_path')), content = string(args, 'content', false);
|
|
174
|
-
if (content === undefined)
|
|
175
|
-
throw failure('EMPLOYEE_ARGUMENT_INVALID');
|
|
176
|
-
capacity(content);
|
|
177
|
-
await mutate(target, signal, async () => { check(); await mkdir(dirname(target), { recursive: true }); check(); await writeFile(target, content, { signal }); });
|
|
178
|
-
return text('文件已写入:' + target);
|
|
179
|
-
}
|
|
180
|
-
if (name === 'Edit') {
|
|
181
|
-
fields(args, ['file_path', 'old_string', 'new_string', 'replace_all']);
|
|
182
|
-
const target = path(string(args, 'file_path')), old = string(args, 'old_string', false), replacement = string(args, 'new_string', false), all = boolean(args, 'replace_all') ?? false;
|
|
183
|
-
if (old === undefined || replacement === undefined || !old)
|
|
184
|
-
throw failure('EMPLOYEE_EDIT_EMPTY_MATCH_UNSUPPORTED');
|
|
185
|
-
return mutate(target, signal, async () => {
|
|
186
|
-
const bytes = await readFile(target, { signal });
|
|
187
|
-
capacity(bytes);
|
|
188
|
-
let content;
|
|
189
|
-
try {
|
|
190
|
-
content = new TextDecoder('utf8', { fatal: true, ignoreBOM: true }).decode(bytes);
|
|
191
|
-
}
|
|
192
|
-
catch {
|
|
193
|
-
throw failure('EMPLOYEE_READ_BINARY_UNSUPPORTED');
|
|
194
|
-
}
|
|
195
|
-
const matches = content.split(old).length - 1;
|
|
196
|
-
if (!matches)
|
|
197
|
-
throw failure('EMPLOYEE_EDIT_NOT_FOUND');
|
|
198
|
-
if (matches > 1 && !all)
|
|
199
|
-
throw failure('EMPLOYEE_EDIT_AMBIGUOUS');
|
|
200
|
-
const next = all ? content.replaceAll(old, () => replacement) : content.replace(old, () => replacement);
|
|
201
|
-
capacity(next);
|
|
202
|
-
check();
|
|
203
|
-
await writeFile(target, next, { signal });
|
|
204
|
-
return text('文件已修改:' + target);
|
|
205
|
-
});
|
|
206
|
-
}
|
|
207
|
-
if (name === 'Glob') {
|
|
208
|
-
fields(args, ['pattern', 'path']);
|
|
209
|
-
const pattern = string(args, 'pattern'), cwd = path(string(args, 'path', false) ?? '.'), found = [];
|
|
210
|
-
for await (const item of glob(pattern, { cwd })) {
|
|
211
|
-
check();
|
|
212
|
-
found.push(resolve(cwd, item));
|
|
213
|
-
capacity(JSON.stringify(found));
|
|
214
|
-
}
|
|
215
|
-
return text(JSON.stringify(found));
|
|
216
|
-
}
|
|
217
|
-
if (name === 'Grep') {
|
|
218
|
-
fields(args, ['pattern', 'path', 'glob', 'output_mode', '-B', '-A', '-C', '-n', '-i', 'type', 'head_limit', 'offset', 'multiline']);
|
|
219
|
-
const pattern = string(args, 'pattern'), mode = string(args, 'output_mode', false) ?? 'files_with_matches', argv = ['--hidden', '--pcre2', '--color', 'never'];
|
|
220
|
-
if (mode === 'files_with_matches')
|
|
221
|
-
argv.push('--files-with-matches');
|
|
222
|
-
else if (mode === 'count')
|
|
223
|
-
argv.push('--count');
|
|
224
|
-
else if (mode !== 'content')
|
|
225
|
-
throw failure('EMPLOYEE_ARGUMENT_UNSUPPORTED');
|
|
226
|
-
for (const flag of ['-B', '-A', '-C']) {
|
|
227
|
-
const value = integer(args, flag, 0);
|
|
228
|
-
if (value !== undefined)
|
|
229
|
-
argv.push(flag, String(value));
|
|
230
|
-
}
|
|
231
|
-
if (boolean(args, '-n'))
|
|
232
|
-
argv.push('--line-number');
|
|
233
|
-
if (boolean(args, '-i'))
|
|
234
|
-
argv.push('--ignore-case');
|
|
235
|
-
if (boolean(args, 'multiline'))
|
|
236
|
-
argv.push('--multiline', '--multiline-dotall');
|
|
237
|
-
for (const [key, flag] of [['glob', '--glob'], ['type', '--type']]) {
|
|
238
|
-
const v = string(args, key, false);
|
|
239
|
-
if (v !== undefined)
|
|
240
|
-
argv.push(flag, v);
|
|
241
|
-
}
|
|
242
|
-
argv.push('--', pattern, path(string(args, 'path', false) ?? '.'));
|
|
243
|
-
const result = await run(options.rgPath ?? await bundledRipgrep(), argv, signal, detail);
|
|
244
|
-
if (result.exitCode !== 0 && result.exitCode !== 1)
|
|
245
|
-
return { isError: true, content: [{ type: 'text', text: JSON.stringify(result) }] };
|
|
246
|
-
const offset = integer(args, 'offset', 0) ?? 0, limit = integer(args, 'head_limit', 0), lines = result.stdout.split('\n');
|
|
247
|
-
return text(lines.slice(offset, limit === undefined || limit === 0 ? undefined : offset + limit).join('\n'));
|
|
248
|
-
}
|
|
249
|
-
fields(args, ['command', 'timeout', 'description', 'run_in_background', 'dangerouslyDisableSandbox']);
|
|
250
|
-
if (boolean(args, 'run_in_background'))
|
|
251
|
-
throw failure('EMPLOYEE_BACKGROUND_UNSUPPORTED');
|
|
252
|
-
boolean(args, 'dangerouslyDisableSandbox');
|
|
253
|
-
string(args, 'description', false);
|
|
254
|
-
const timeout = integer(args, 'timeout', 1);
|
|
255
|
-
if (timeout !== undefined && timeout > 2147483647)
|
|
256
|
-
throw failure('EMPLOYEE_ARGUMENT_INVALID');
|
|
257
|
-
const command = string(args, 'command');
|
|
258
|
-
if (!options.runShell)
|
|
259
|
-
throw failure('EMPLOYEE_SHELL_ENDPOINT_UNAVAILABLE');
|
|
260
|
-
const result = await options.runShell({ command, signal, ...timeout === undefined ? {} : { timeout } });
|
|
261
|
-
return { ...text(JSON.stringify(result)), ...(result.exitCode !== 0 || result.signal ? { isError: true } : {}) };
|
|
262
|
-
}
|
|
263
|
-
async function call(name, input, requestSignal, observer = options.detail) {
|
|
264
|
-
const signal = AbortSignal.any([lifetime.signal, ...requestSignal ? [requestSignal] : []]);
|
|
265
|
-
signal.throwIfAborted();
|
|
266
|
-
const executionId = randomUUID();
|
|
267
|
-
let submitted = false;
|
|
268
|
-
const detail = (status, code) => observer({ kind: 'session', severity: status === 'completed' ? 'info' : 'warning', code: 'CLAUDE_EMPLOYEE_OPERATION', summary: 'Claude本机工具执行位置与结果', payload: { executionId, operation: name, status, ...code ? { reason: code } : {}, cwd: options.cwd, uid: options.uid, pid: process.pid } });
|
|
269
|
-
try {
|
|
270
|
-
if (!EMPLOYEE_TOOL_NAMES.includes(name))
|
|
271
|
-
throw failure('EMPLOYEE_TOOL_UNSUPPORTED');
|
|
272
|
-
await options.validate(signal);
|
|
273
|
-
signal.throwIfAborted();
|
|
274
|
-
submitted = true;
|
|
275
|
-
detail('started');
|
|
276
|
-
const result = await execute(name, object(input), signal, observer);
|
|
277
|
-
detail(result.isError ? 'failed' : 'completed');
|
|
278
|
-
await options.validate(signal);
|
|
279
|
-
signal.throwIfAborted();
|
|
280
|
-
capacity(JSON.stringify(result));
|
|
281
|
-
return result;
|
|
282
|
-
}
|
|
283
|
-
catch (error) {
|
|
284
|
-
const code = error instanceof NeedError ? error.code : signal.aborted ? 'EMPLOYEE_CANCELLED_UNKNOWN' : typeof error?.code === 'string' ? 'EMPLOYEE_IO_' + error.code : 'EMPLOYEE_OPERATION_FAILED';
|
|
285
|
-
detail(submitted && signal.aborted ? 'unknown' : 'failed', code);
|
|
286
|
-
return { isError: true, content: [{ type: 'text', text: code + ':本机操作未完成;没有在主机代跑。' }] };
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
return {
|
|
290
|
-
call(name, input, requestSignal, observer) { const operation = call(name, input, requestSignal, observer); calls.add(operation); void operation.then(() => calls.delete(operation), () => calls.delete(operation)); return operation; },
|
|
291
|
-
async close(cause = { layer: 'executor', initiator: 'tools.close', reason: 'EMPLOYEE_TOOLS_CLOSED', action: 'close' }) { options.closeDiagnostics?.record({ ...cause, changes: cause.changes ?? [{ field: 'tools.aborted', before: lifetime.signal.aborted, after: true }] }); lifetime.abort(); await Promise.allSettled([...calls, ...children]); },
|
|
292
|
-
};
|
|
293
|
-
}
|
|
3
|
+
import { closeCode } from "./close-diagnostics.js";
|
|
4
|
+
export { EMPLOYEE_TOOL_NAMES } from '@ai-workbench/employee-tools';
|
|
5
|
+
export function createEmployeeTools(options) { return create(options, { NeedError, closeCode }); }
|
package/dist/index.d.ts
CHANGED
|
@@ -19,13 +19,16 @@ export declare const DEFAULT_CONFIG: Readonly<{
|
|
|
19
19
|
readonly brokerMaxFrameBytes: 2097152;
|
|
20
20
|
readonly brokerControlMaxBytes: 1048576;
|
|
21
21
|
readonly employeeShellKillGraceMs: 1000;
|
|
22
|
+
readonly cloudCancelReceiptWaitMs: 10000;
|
|
22
23
|
}>;
|
|
23
24
|
export declare const Config: Schema<Schemastery.ObjectS<NoInfer<{
|
|
25
|
+
cloudCancelReceiptWaitMs: Schema<number, number, "volatile-defined">;
|
|
24
26
|
acpMaxMessageBytes: Schema<number, number, "volatile-defined">;
|
|
25
27
|
brokerMaxFrameBytes: Schema<number, number, "volatile-defined">;
|
|
26
28
|
brokerControlMaxBytes: Schema<number, number, "volatile-defined">;
|
|
27
29
|
employeeShellKillGraceMs: Schema<number, number, "volatile-defined">;
|
|
28
30
|
}>>, Schemastery.ObjectT<NoInfer<{
|
|
31
|
+
cloudCancelReceiptWaitMs: Schema<number, number, "volatile-defined">;
|
|
29
32
|
acpMaxMessageBytes: Schema<number, number, "volatile-defined">;
|
|
30
33
|
brokerMaxFrameBytes: Schema<number, number, "volatile-defined">;
|
|
31
34
|
brokerControlMaxBytes: Schema<number, number, "volatile-defined">;
|
|
@@ -36,6 +39,7 @@ export interface PluginConfig {
|
|
|
36
39
|
brokerMaxFrameBytes?: number;
|
|
37
40
|
brokerControlMaxBytes?: number;
|
|
38
41
|
employeeShellKillGraceMs?: number;
|
|
42
|
+
cloudCancelReceiptWaitMs?: number;
|
|
39
43
|
}
|
|
40
44
|
type LiveConfig = {
|
|
41
45
|
[K in keyof PluginConfig]: PluginConfig[K] | {
|