@ai-workbench/remote-agent 0.9.0-toolbridge.36 → 0.9.0-toolbridge.37-rc.4
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 +10 -5
- package/dist/acp.d.ts +4 -3
- package/dist/acp.js +7 -9
- package/dist/claude-local.js +2 -2
- package/dist/cold-protocol.d.ts +1 -0
- package/dist/cold-protocol.js +14 -7
- package/dist/cold-recovery.d.ts +1 -0
- package/dist/cold-recovery.js +2 -2
- package/dist/cold-store.d.ts +0 -1
- package/dist/custom-instructions.d.ts +6 -0
- package/dist/custom-instructions.js +21 -0
- package/dist/employee-mcp.js +2 -2
- package/dist/employee-native.d.ts +2 -2
- package/dist/employee-native.js +1 -1
- package/dist/employee-provider.js +1 -1
- package/dist/employee-relay.js +1 -1
- package/dist/employee-tools.js +1 -1
- package/dist/index.d.ts +0 -2
- package/dist/index.js +32 -62
- package/dist/input.d.ts +2 -9
- package/dist/input.js +13 -62
- package/dist/native-content-transport.d.ts +42 -0
- package/dist/native-content-transport.js +83 -0
- package/dist/native-content.d.ts +67 -0
- package/dist/native-content.js +294 -0
- package/dist/provider.d.ts +4 -2
- package/dist/toolbridge-table.d.ts +5 -3
- package/dist/toolbridge-table.js +6 -15
- package/dist/toolbridge.js +21 -32
- package/package.json +7 -5
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
import { createHash, randomUUID } from 'node:crypto';
|
|
2
|
+
import { lstat, mkdir, mkdtemp, readFile, readdir, realpath, rename, rm, writeFile, symlink, readlink } from 'node:fs/promises';
|
|
3
|
+
import { basename, dirname, isAbsolute, join, relative, resolve, sep } from 'node:path';
|
|
4
|
+
const hash = (value) => createHash('sha256').update(value).digest('hex');
|
|
5
|
+
const fail = (reason) => { throw Object.assign(new Error('EMPLOYEE_CONTEXT_UNAVAILABLE'), { reason }); };
|
|
6
|
+
const name = (value) => /^[a-z0-9][a-z0-9-]*$/.test(value);
|
|
7
|
+
function subpath(value) {
|
|
8
|
+
if (!value || value.includes('\0') || value.includes('\\') || isAbsolute(value) || value.split('/').some(part => !part || part === '.' || part === '..'))
|
|
9
|
+
fail('PATH_INVALID');
|
|
10
|
+
return value;
|
|
11
|
+
}
|
|
12
|
+
function within(root, path) { const r = relative(root, path); return r === '' || !r.startsWith('..' + sep) && r !== '..' && !isAbsolute(r); }
|
|
13
|
+
function check(options, bytes) { options.signal?.throwIfAborted(); if (!Number.isSafeInteger(options.maxBytes) || options.maxBytes < 1 || bytes > options.maxBytes)
|
|
14
|
+
fail('TOO_LARGE'); }
|
|
15
|
+
/** Reads only an explicitly selected DSH package. Never scans the host native catalog. */
|
|
16
|
+
export async function captureNativeSkill(input, options) {
|
|
17
|
+
if (!name(input.name) || typeof input.description !== 'string' || !input.description)
|
|
18
|
+
fail('SKILL_INVALID');
|
|
19
|
+
const root = await realpath(input.resourceRoot), skill = await realpath(input.skillFile);
|
|
20
|
+
if (dirname(skill) !== root || basename(skill) !== 'SKILL.md')
|
|
21
|
+
fail('PACKAGE_ROOT_REQUIRED');
|
|
22
|
+
const files = [];
|
|
23
|
+
let total = 0;
|
|
24
|
+
const walk = async (directory) => {
|
|
25
|
+
for (const entry of (await readdir(directory, { withFileTypes: true })).sort((a, b) => a.name.localeCompare(b.name))) {
|
|
26
|
+
options.signal?.throwIfAborted();
|
|
27
|
+
const path = join(directory, entry.name), before = await lstat(path);
|
|
28
|
+
if (before.isSymbolicLink())
|
|
29
|
+
fail('RESOURCE_SYMLINK');
|
|
30
|
+
if (before.isDirectory()) {
|
|
31
|
+
await walk(path);
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
if (!before.isFile())
|
|
35
|
+
fail('RESOURCE_TYPE');
|
|
36
|
+
const bytes = await readFile(path), after = await lstat(path);
|
|
37
|
+
total += bytes.byteLength;
|
|
38
|
+
check(options, total);
|
|
39
|
+
if (before.dev !== after.dev || before.ino !== after.ino || before.size !== after.size || before.mtimeMs !== after.mtimeMs || before.ctimeMs !== after.ctimeMs)
|
|
40
|
+
fail('SOURCE_CHANGED');
|
|
41
|
+
files.push({ path: relative(root, path).split(sep).join('/'), bytes, executable: !!(before.mode & 0o111) });
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
await walk(root);
|
|
45
|
+
if (await realpath(input.resourceRoot) !== root || await realpath(input.skillFile) !== skill)
|
|
46
|
+
fail('SOURCE_CHANGED');
|
|
47
|
+
if (input.content !== undefined) {
|
|
48
|
+
const index = files.findIndex(file => file.path === 'SKILL.md'), original = files[index];
|
|
49
|
+
if (!original)
|
|
50
|
+
fail('SKILL_INVALID');
|
|
51
|
+
const raw = Buffer.from(original.bytes).toString('utf8'), frontmatter = /^---\r?\n[\s\S]*?\r?\n---(?:\r?\n|$)/u.exec(raw)?.[0];
|
|
52
|
+
const header = frontmatter ?? fail('SKILL_INVALID');
|
|
53
|
+
const bytes = Buffer.from(header + (header.endsWith('\n') ? '' : '\n') + input.content);
|
|
54
|
+
total += bytes.byteLength - original.bytes.byteLength;
|
|
55
|
+
check(options, total);
|
|
56
|
+
files[index] = { ...original, bytes };
|
|
57
|
+
}
|
|
58
|
+
return { name: input.name, description: input.description, files };
|
|
59
|
+
}
|
|
60
|
+
/** The caller supplies the exact instruction/import closure; no repository-wide copying. */
|
|
61
|
+
export async function captureNativeProject(employeeCwd, paths, options) {
|
|
62
|
+
const root = await realpath(employeeCwd), files = [];
|
|
63
|
+
let total = 0;
|
|
64
|
+
for (const item of paths) {
|
|
65
|
+
const path = subpath(item), full = join(root, path), canonical = await realpath(full);
|
|
66
|
+
if (!within(root, canonical))
|
|
67
|
+
fail('PROJECT_ESCAPE');
|
|
68
|
+
const before = await lstat(full);
|
|
69
|
+
if (!before.isFile() || before.isSymbolicLink())
|
|
70
|
+
fail('PROJECT_TYPE');
|
|
71
|
+
const bytes = await readFile(full), after = await lstat(full);
|
|
72
|
+
total += bytes.byteLength;
|
|
73
|
+
check(options, total);
|
|
74
|
+
if (before.dev !== after.dev || before.ino !== after.ino || before.size !== after.size || before.mtimeMs !== after.mtimeMs || before.ctimeMs !== after.ctimeMs || await realpath(full) !== canonical)
|
|
75
|
+
fail('SOURCE_CHANGED');
|
|
76
|
+
files.push({ path, bytes });
|
|
77
|
+
}
|
|
78
|
+
return files;
|
|
79
|
+
}
|
|
80
|
+
/** Only native manifest metadata is generated. Skill bodies come verbatim from their provider; project files remain original bytes. */
|
|
81
|
+
export function nativeContentPlan(snapshot, options) {
|
|
82
|
+
if (!snapshot.scopeId || !isAbsolute(snapshot.employeeCwd) || snapshot.employeeCwd.includes('\0'))
|
|
83
|
+
fail('SCOPE_INVALID');
|
|
84
|
+
const files = [], paths = new Set();
|
|
85
|
+
let total = 0;
|
|
86
|
+
const add = (path, file) => { subpath(path); if (paths.has(path))
|
|
87
|
+
fail('DUPLICATE_PATH'); paths.add(path); total += file.bytes.byteLength; check(options, total); files.push({ path, data: Buffer.from(file.bytes).toString('base64'), sha256: hash(file.bytes), executable: !!file.executable }); };
|
|
88
|
+
for (const file of snapshot.project)
|
|
89
|
+
add('project/' + subpath(file.path), file);
|
|
90
|
+
const names = new Set();
|
|
91
|
+
for (const skill of snapshot.skills) {
|
|
92
|
+
if (!name(skill.name) || !skill.description || names.has(skill.name) || !skill.files.some(file => file.path === 'SKILL.md'))
|
|
93
|
+
fail('SKILL_INVALID');
|
|
94
|
+
names.add(skill.name);
|
|
95
|
+
}
|
|
96
|
+
const scopeHash = hash(snapshot.scopeId), claudeNamespace = 'dsh-' + scopeHash.slice(0, 12);
|
|
97
|
+
add('plugin/.claude-plugin/plugin.json', { path: 'plugin.json', bytes: Buffer.from(JSON.stringify({ name: claudeNamespace, version: '1.0.0' })) });
|
|
98
|
+
for (const skill of snapshot.skills)
|
|
99
|
+
for (const file of skill.files) {
|
|
100
|
+
add('skills/' + skill.name + '/' + subpath(file.path), file);
|
|
101
|
+
add('plugin/skills/' + skill.name + '/' + subpath(file.path), file);
|
|
102
|
+
}
|
|
103
|
+
files.sort((a, b) => a.path.localeCompare(b.path));
|
|
104
|
+
const generation = hash(JSON.stringify({ scopeHash, employeeCwd: snapshot.employeeCwd, files: files.map(({ path, sha256, executable }) => ({ path, sha256, executable })) }));
|
|
105
|
+
return { version: 1, generation, scopeHash, employeeCwd: snapshot.employeeCwd, claudeNamespace, files };
|
|
106
|
+
}
|
|
107
|
+
function layout(root, plan) {
|
|
108
|
+
const base = join(root, plan.generation), hostCwd = join(root, 'current', 'project', plan.employeeCwd.replace(/^\/+/, ''));
|
|
109
|
+
return { generation: plan.generation, hostCwd, codexSkillsRoot: join(base, 'skills'), claudePluginRoot: join(base, 'plugin'), claudeNamespace: plan.claudeNamespace, cwdMap: { native: hostCwd, employee: plan.employeeCwd } };
|
|
110
|
+
}
|
|
111
|
+
/** Immutable generations coexist with user native skills; this never rewrites their files. */
|
|
112
|
+
export async function materializeNativeContent(plan, root, signal, employeeProject = false) {
|
|
113
|
+
validatePlan(plan);
|
|
114
|
+
signal?.throwIfAborted();
|
|
115
|
+
root = resolve(root);
|
|
116
|
+
await mkdir(root, { recursive: true, mode: 0o700 });
|
|
117
|
+
const info = await lstat(root);
|
|
118
|
+
if (!info.isDirectory() || info.isSymbolicLink() || info.uid !== process.getuid?.() || info.mode & 0o077 || await realpath(root) !== root)
|
|
119
|
+
fail('DESTINATION_UNSAFE');
|
|
120
|
+
const destination = join(root, plan.generation), expected = JSON.stringify(plan), manifest = 'native-content.json';
|
|
121
|
+
const verify = async () => { if (await readFile(join(destination, manifest), 'utf8') !== expected)
|
|
122
|
+
fail('GENERATION_CONFLICT'); for (const file of plan.files) {
|
|
123
|
+
const p = join(destination, file.path), s = await lstat(p);
|
|
124
|
+
if (!s.isFile() || s.isSymbolicLink() || hash(await readFile(p)) !== file.sha256)
|
|
125
|
+
fail('GENERATION_CONFLICT');
|
|
126
|
+
} };
|
|
127
|
+
try {
|
|
128
|
+
await lstat(destination);
|
|
129
|
+
await verify();
|
|
130
|
+
return layout(root, plan);
|
|
131
|
+
}
|
|
132
|
+
catch (error) {
|
|
133
|
+
if (error.code !== 'ENOENT')
|
|
134
|
+
throw error;
|
|
135
|
+
}
|
|
136
|
+
const stage = await mkdtemp(join(root, '.stage-'));
|
|
137
|
+
try {
|
|
138
|
+
if (employeeProject)
|
|
139
|
+
await symlink('/', join(stage, 'project'));
|
|
140
|
+
for (const file of plan.files) {
|
|
141
|
+
signal?.throwIfAborted();
|
|
142
|
+
if (employeeProject && file.path.startsWith('project/')) {
|
|
143
|
+
if (hash(await readFile('/' + file.path.slice('project/'.length))) !== file.sha256)
|
|
144
|
+
fail('SOURCE_CHANGED');
|
|
145
|
+
continue;
|
|
146
|
+
}
|
|
147
|
+
const path = join(stage, file.path);
|
|
148
|
+
await mkdir(dirname(path), { recursive: true, mode: 0o700 });
|
|
149
|
+
await writeFile(path, Buffer.from(file.data, 'base64'), { flag: 'wx', mode: file.executable ? 0o700 : 0o600 });
|
|
150
|
+
}
|
|
151
|
+
if (!employeeProject)
|
|
152
|
+
await mkdir(join(stage, 'project', plan.employeeCwd.replace(/^\/+/, '')), { recursive: true, mode: 0o700 });
|
|
153
|
+
await mkdir(join(stage, 'skills'), { recursive: true, mode: 0o700 });
|
|
154
|
+
await mkdir(join(stage, 'plugin', 'skills'), { recursive: true, mode: 0o700 });
|
|
155
|
+
await writeFile(join(stage, manifest), expected, { flag: 'wx', mode: 0o600 });
|
|
156
|
+
signal?.throwIfAborted();
|
|
157
|
+
try {
|
|
158
|
+
await rename(stage, destination);
|
|
159
|
+
}
|
|
160
|
+
catch (error) {
|
|
161
|
+
if (!['EEXIST', 'ENOTEMPTY'].includes(error.code ?? ''))
|
|
162
|
+
throw error;
|
|
163
|
+
await verify();
|
|
164
|
+
}
|
|
165
|
+
return layout(root, plan);
|
|
166
|
+
}
|
|
167
|
+
finally {
|
|
168
|
+
await rm(stage, { recursive: true, force: true });
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
function validatePlan(plan) {
|
|
172
|
+
if (plan.version !== 1 || !plan.files.length || !isAbsolute(plan.employeeCwd) || !name(plan.claudeNamespace) || !/^([a-f0-9]{64})$/.test(plan.generation) || !/^([a-f0-9]{64})$/.test(plan.scopeHash))
|
|
173
|
+
fail('PLAN_INVALID');
|
|
174
|
+
const paths = new Set();
|
|
175
|
+
for (const f of plan.files) {
|
|
176
|
+
subpath(f.path);
|
|
177
|
+
if (paths.has(f.path) || !f.path.match(/^(project|skills|plugin)\//) || hash(Buffer.from(f.data, 'base64')) !== f.sha256)
|
|
178
|
+
fail('PLAN_INVALID');
|
|
179
|
+
paths.add(f.path);
|
|
180
|
+
}
|
|
181
|
+
const generation = hash(JSON.stringify({ scopeHash: plan.scopeHash, employeeCwd: plan.employeeCwd, files: plan.files.map(({ path, sha256, executable }) => ({ path, sha256, executable })) }));
|
|
182
|
+
if (generation !== plan.generation)
|
|
183
|
+
fail('PLAN_INVALID');
|
|
184
|
+
}
|
|
185
|
+
/** Maps structured native paths only. Shell source is never searched or rewritten. */
|
|
186
|
+
export function employeePathForNative(path, map) {
|
|
187
|
+
if (!isAbsolute(path))
|
|
188
|
+
return path;
|
|
189
|
+
const p = resolve(path), root = resolve(map.native);
|
|
190
|
+
return within(root, p) ? resolve(map.employee, relative(root, p)) : path;
|
|
191
|
+
}
|
|
192
|
+
/** Register only this managed generation; user native packages are never overwritten. */
|
|
193
|
+
export async function registerNativeContent(plan, root, home) {
|
|
194
|
+
const result = await materializeNativeContent(plan, root);
|
|
195
|
+
const nativeRoots = [join(home, '.agents', 'skills'), join(home, '.claude', 'skills')];
|
|
196
|
+
for (const [index, parent] of nativeRoots.entries()) {
|
|
197
|
+
await mkdir(parent, { recursive: true, mode: 0o700 });
|
|
198
|
+
const link = join(parent, '.aw-dsh-' + plan.scopeHash.slice(0, 24)), target = join(root, plan.generation, index === 0 ? 'skills' : 'plugin');
|
|
199
|
+
try {
|
|
200
|
+
if (await readlink(link) === target)
|
|
201
|
+
continue;
|
|
202
|
+
const info = await lstat(link);
|
|
203
|
+
if (!info.isSymbolicLink() || info.uid !== process.getuid?.())
|
|
204
|
+
fail('DESTINATION_UNSAFE');
|
|
205
|
+
}
|
|
206
|
+
catch (error) {
|
|
207
|
+
if (error.code !== 'ENOENT')
|
|
208
|
+
throw error;
|
|
209
|
+
}
|
|
210
|
+
const staged = link + '.' + randomUUID();
|
|
211
|
+
try {
|
|
212
|
+
await symlink(target, staged);
|
|
213
|
+
await rename(staged, link);
|
|
214
|
+
}
|
|
215
|
+
finally {
|
|
216
|
+
await rm(staged, { force: true });
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
await activateNativeContent(plan, root);
|
|
220
|
+
return result;
|
|
221
|
+
}
|
|
222
|
+
export async function activateNativeContent(plan, root) {
|
|
223
|
+
const link = join(root, 'current'), staged = link + '.' + randomUUID();
|
|
224
|
+
try {
|
|
225
|
+
try {
|
|
226
|
+
const stat = await lstat(link);
|
|
227
|
+
if (!stat.isSymbolicLink() || stat.uid !== process.getuid?.())
|
|
228
|
+
fail('DESTINATION_UNSAFE');
|
|
229
|
+
}
|
|
230
|
+
catch (error) {
|
|
231
|
+
if (error.code !== 'ENOENT')
|
|
232
|
+
throw error;
|
|
233
|
+
}
|
|
234
|
+
await symlink(plan.generation, staged);
|
|
235
|
+
await rename(staged, link);
|
|
236
|
+
}
|
|
237
|
+
finally {
|
|
238
|
+
await rm(staged, { force: true });
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
/** Collect only project instruction files and their relative native imports. */
|
|
242
|
+
export async function captureProjectInstructions(cwd, options) {
|
|
243
|
+
const files = new Map();
|
|
244
|
+
let total = 0;
|
|
245
|
+
const add = async (path) => {
|
|
246
|
+
options.signal?.throwIfAborted();
|
|
247
|
+
let bytes;
|
|
248
|
+
try {
|
|
249
|
+
bytes = await readFile(path);
|
|
250
|
+
}
|
|
251
|
+
catch (error) {
|
|
252
|
+
if (['ENOENT', 'ENOTDIR', 'EISDIR'].includes(error.code ?? ''))
|
|
253
|
+
return;
|
|
254
|
+
throw error;
|
|
255
|
+
}
|
|
256
|
+
path = resolve(path);
|
|
257
|
+
const key = path.replace(/^\/+/, '');
|
|
258
|
+
if (files.has(key))
|
|
259
|
+
return;
|
|
260
|
+
total += bytes.byteLength;
|
|
261
|
+
check(options, total);
|
|
262
|
+
files.set(key, { path: key, bytes });
|
|
263
|
+
// Native CLAUDE imports are kept as raw files; no rewriting or prose projection.
|
|
264
|
+
for (const match of bytes.toString('utf8').matchAll(/(?:^|\s)@([^\s`]+\.md)(?=\s|$)/gm))
|
|
265
|
+
if (!isAbsolute(match[1]))
|
|
266
|
+
await add(resolve(dirname(path), match[1]));
|
|
267
|
+
};
|
|
268
|
+
const rules = async (path) => {
|
|
269
|
+
let entries;
|
|
270
|
+
try {
|
|
271
|
+
entries = await readdir(path, { withFileTypes: true });
|
|
272
|
+
}
|
|
273
|
+
catch (error) {
|
|
274
|
+
if (['ENOENT', 'ENOTDIR'].includes(error.code ?? ''))
|
|
275
|
+
return;
|
|
276
|
+
throw error;
|
|
277
|
+
}
|
|
278
|
+
for (const entry of entries) {
|
|
279
|
+
const full = join(path, entry.name);
|
|
280
|
+
if (entry.isDirectory())
|
|
281
|
+
await rules(full);
|
|
282
|
+
else if (entry.isFile() && entry.name.endsWith('.md'))
|
|
283
|
+
await add(full);
|
|
284
|
+
}
|
|
285
|
+
};
|
|
286
|
+
for (let path = resolve(cwd);; path = dirname(path)) {
|
|
287
|
+
for (const name of ['AGENTS.md', 'CLAUDE.md', 'CLAUDE.local.md', '.claude/CLAUDE.md'])
|
|
288
|
+
await add(join(path, name));
|
|
289
|
+
await rules(join(path, '.claude/rules'));
|
|
290
|
+
if (path === dirname(path))
|
|
291
|
+
break;
|
|
292
|
+
}
|
|
293
|
+
return [...files.values()].sort((a, b) => a.path.localeCompare(b.path));
|
|
294
|
+
}
|
package/dist/provider.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { NativeSessionContent } from './native-content-transport.ts';
|
|
1
2
|
import type { StartupTiming } from './startup-timing.ts';
|
|
2
3
|
import type { ColdRound, ColdStore } from './cold-store.ts';
|
|
3
4
|
import type { ToolBridge } from './toolbridge.ts';
|
|
@@ -29,6 +30,8 @@ export interface EmployeeExecution {
|
|
|
29
30
|
validate(signal?: AbortSignal): Promise<void>;
|
|
30
31
|
}
|
|
31
32
|
export interface Runtime {
|
|
33
|
+
nativeContent?: NativeSessionContent;
|
|
34
|
+
customInstructions?: string;
|
|
32
35
|
localDirectory?: {
|
|
33
36
|
path: string;
|
|
34
37
|
device: number;
|
|
@@ -81,6 +84,7 @@ export interface Runtime {
|
|
|
81
84
|
closeDiagnostics?: CloseDiagnostics;
|
|
82
85
|
}
|
|
83
86
|
export interface RuntimeRequest extends Invocation {
|
|
87
|
+
nativeEmployeeCwd?: string;
|
|
84
88
|
startupTiming?: StartupTiming;
|
|
85
89
|
toolBridge?: ToolBridge;
|
|
86
90
|
workspaceSource?: SessionSource;
|
|
@@ -88,7 +92,6 @@ export interface RuntimeRequest extends Invocation {
|
|
|
88
92
|
source: 'host';
|
|
89
93
|
}>;
|
|
90
94
|
closeDiagnostics?: CloseDiagnostics;
|
|
91
|
-
previousContextSha256?: string;
|
|
92
95
|
preflightFailureRecorded?: boolean;
|
|
93
96
|
recordEarlyFailure?: (error: unknown, stage: SourceAdmissionStage) => Promise<void>;
|
|
94
97
|
localContext?: Readonly<LocalWorkspaceContext>;
|
|
@@ -96,7 +99,6 @@ export interface RuntimeRequest extends Invocation {
|
|
|
96
99
|
detailChannel?: DetailChannel;
|
|
97
100
|
acpMaxMessageBytes?: number;
|
|
98
101
|
employeeScope?: string;
|
|
99
|
-
contextSha256?: string;
|
|
100
102
|
resumeSessionId?: string;
|
|
101
103
|
bindingRevision?: number;
|
|
102
104
|
currentInputIds?: readonly string[];
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ToolSchema
|
|
1
|
+
import type { ToolSchema } from '@deepseek-ai/dsh-llm';
|
|
2
2
|
import type { Tool } from '@modelcontextprotocol/sdk/types.js';
|
|
3
3
|
/** Fixed official tool names; no description heuristics or plugin-name branches. */
|
|
4
4
|
export declare const TOOLBRIDGE_EXCLUDED: readonly ["read", "write", "edit", "read_image", "glob", "grep", "bash", "pwsh", "str_replace_editor", "terminal_open", "terminal_send", "terminal_read", "terminal_signal", "terminal_close", "terminal_list", "run_code"];
|
|
@@ -7,5 +7,7 @@ export interface BridgeTool {
|
|
|
7
7
|
schema: ToolSchema;
|
|
8
8
|
wire: Tool;
|
|
9
9
|
}
|
|
10
|
-
export declare function
|
|
11
|
-
|
|
10
|
+
export declare function bridgeTable(tools?: readonly ToolSchema[]): ReadonlyMap<string, BridgeTool>;
|
|
11
|
+
/** Essential bridge entry points have a separate native namespace from deferred tools. */
|
|
12
|
+
export declare const TOOLBRIDGE_ESSENTIAL: readonly ["skill", "load_workspace_dependencies"];
|
|
13
|
+
export declare function essentialBridgeTool(name: string): boolean;
|
package/dist/toolbridge-table.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { createHash } from 'node:crypto';
|
|
2
1
|
import { NeedError } from "./remote.js";
|
|
3
2
|
/** Fixed official tool names; no description heuristics or plugin-name branches. */
|
|
4
3
|
export const TOOLBRIDGE_EXCLUDED = Object.freeze([
|
|
@@ -7,17 +6,7 @@ export const TOOLBRIDGE_EXCLUDED = Object.freeze([
|
|
|
7
6
|
'run_code',
|
|
8
7
|
]);
|
|
9
8
|
const excluded = new Set(TOOLBRIDGE_EXCLUDED);
|
|
10
|
-
export function
|
|
11
|
-
for (const message of messages.toReversed()) {
|
|
12
|
-
const source = message.source;
|
|
13
|
-
if (message.role !== 'user' || source?.kind !== 'skill-catalog' || source.form !== 'catalog' || !Array.isArray(source.entries))
|
|
14
|
-
continue;
|
|
15
|
-
if (!source.entries.every(entry => entry && typeof entry.name === 'string' && typeof entry.description === 'string'))
|
|
16
|
-
continue;
|
|
17
|
-
return source.entries.map(entry => entry.name);
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
export function bridgeTable(tools = [], catalog, catalogInCurrentPrompt = false) {
|
|
9
|
+
export function bridgeTable(tools = []) {
|
|
21
10
|
const table = new Map(), names = new Set();
|
|
22
11
|
for (const tool of tools) {
|
|
23
12
|
if (excluded.has(tool.name))
|
|
@@ -25,12 +14,14 @@ export function bridgeTable(tools = [], catalog, catalogInCurrentPrompt = false)
|
|
|
25
14
|
if (names.has(tool.name))
|
|
26
15
|
throw new NeedError('TOOLBRIDGE_TOOL_DUPLICATE', '当前工具表存在重名,工具桥未开始。', '官方唯一工具名称');
|
|
27
16
|
names.add(tool.name);
|
|
28
|
-
const alias = '
|
|
17
|
+
const alias = 'dsh__' + tool.name;
|
|
29
18
|
if (table.has(alias))
|
|
30
19
|
throw new NeedError('TOOLBRIDGE_NAME_COLLISION', '工具桥名称冲突,未执行。', '唯一工具桥名称');
|
|
31
20
|
const schema = structuredClone(tool);
|
|
32
|
-
|
|
33
|
-
table.set(alias, { alias, schema, wire: { name: alias, description: 'DSH工具 ' + schema.name + ':' + schema.description + skill, inputSchema: structuredClone(schema.parameters) } });
|
|
21
|
+
table.set(alias, { alias, schema, wire: { name: alias, description: schema.description, inputSchema: structuredClone(schema.parameters) } });
|
|
34
22
|
}
|
|
35
23
|
return table;
|
|
36
24
|
}
|
|
25
|
+
/** Essential bridge entry points have a separate native namespace from deferred tools. */
|
|
26
|
+
export const TOOLBRIDGE_ESSENTIAL = Object.freeze(['skill', 'load_workspace_dependencies']);
|
|
27
|
+
export function essentialBridgeTool(name) { return TOOLBRIDGE_ESSENTIAL.includes(name.replace(/^dsh__/, '')); }
|
package/dist/toolbridge.js
CHANGED
|
@@ -4,7 +4,7 @@ import { mkdtemp, lstat, rm } from 'node:fs/promises';
|
|
|
4
4
|
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
|
|
5
5
|
import { ListToolsRequestSchema, CallToolRequestSchema } from '@modelcontextprotocol/sdk/types.js';
|
|
6
6
|
import { EmployeeSocketTransport } from "./employee-mcp.js";
|
|
7
|
-
import { bridgeTable,
|
|
7
|
+
import { bridgeTable, essentialBridgeTool } from "./toolbridge-table.js";
|
|
8
8
|
import { NeedError } from "./remote.js";
|
|
9
9
|
export const TOOLBRIDGE_TIMEOUT_SECONDS = 1800;
|
|
10
10
|
export const TOOLBRIDGE_NOTICE = '含工具桥:本机文件夹的扩展工具(含DSH子代理、workflow和后台任务工具)由DSH执行并按当前权限审批;文件和命令保持原执行方式。工具表在新一轮主机连接时更新。';
|
|
@@ -19,22 +19,7 @@ export class ToolBridge {
|
|
|
19
19
|
get table() {
|
|
20
20
|
if (this.snapshot)
|
|
21
21
|
return this.snapshot;
|
|
22
|
-
|
|
23
|
-
// prepareInput只转发最后assistant之后的user内容;历史目录不能代替当前prompt。
|
|
24
|
-
// 正文不含目录全部名字时仍保留原schema快照,避免分支/切模型时丢发现入口。
|
|
25
|
-
const current = messages.slice(messages.findLastIndex(message => message.role === 'assistant') + 1);
|
|
26
|
-
const forwarded = catalog !== undefined && catalog.length > 0 && current.some(message => {
|
|
27
|
-
const names = dshSkillCatalog([message]);
|
|
28
|
-
if (names === undefined || JSON.stringify(names) !== JSON.stringify(catalog))
|
|
29
|
-
return false;
|
|
30
|
-
const text = message.content.filter(block => block.type === 'text').map(block => block.text).join('\n');
|
|
31
|
-
// 只认官方目录中明确声明的完整名字;子串命中不能证明office也已随office-xlsx送达。
|
|
32
|
-
// 未识别的格式继续携带原名字快照,不推测目录内容。
|
|
33
|
-
const declared = new Set([...text.matchAll(/<available_skills>([\s\S]*?)<\/available_skills>/g)]
|
|
34
|
-
.flatMap(section => [...section[1].matchAll(/^[ \t]*-[ \t]+`([^`\r\n]+)`[ \t]*:/gm)].map(item => item[1])));
|
|
35
|
-
return catalog.every(name => declared.has(name));
|
|
36
|
-
});
|
|
37
|
-
return this.snapshot = bridgeTable(this.options.tools, catalog, forwarded);
|
|
22
|
+
return this.snapshot = bridgeTable(this.options.tools);
|
|
38
23
|
}
|
|
39
24
|
calls = new Map();
|
|
40
25
|
enabled = false;
|
|
@@ -62,11 +47,12 @@ export class ToolBridge {
|
|
|
62
47
|
enable(record) { if (!this.table.size)
|
|
63
48
|
return; this.enabled = true; this.record = record; record({ kind: 'session', severity: 'info', code: 'TOOLBRIDGE_SELECTED', summary: TOOLBRIDGE_NOTICE, payload: { toolCount: this.table.size, toolTimeoutSec: TOOLBRIDGE_TIMEOUT_SECONDS, timeoutSource: 'owner-2026-10-26', snapshot: 'native-session-open', excluded: 'official-file-command-terminal-run_code' } }); }
|
|
64
49
|
get tools() { return [...this.table.values()].map(t => t.wire); }
|
|
65
|
-
isBridge(name) { return typeof name === 'string' && this.table.has(name.replace(/^
|
|
50
|
+
isBridge(name) { return typeof name === 'string' && this.table.has(name.replace(/^mcp__(?:employee|dsh)__/, '')); }
|
|
66
51
|
async call(alias, args, signal) {
|
|
52
|
+
alias = alias.replace(/^mcp__(?:employee|dsh)__/, '');
|
|
67
53
|
const tool = this.table.get(alias);
|
|
68
54
|
if (!this.enabled || this.ended || this.localTurnEnded || !this.active || this.active.aborted || !tool || signal.aborted)
|
|
69
|
-
return errorResult(
|
|
55
|
+
return errorResult(JSON.stringify({ code: -32603 }));
|
|
70
56
|
const id = ('awb-' + randomUUID());
|
|
71
57
|
const abort = () => this.abandon('MCP_REQUEST_CANCELLED');
|
|
72
58
|
const pending = { block: { type: 'tool-call', id, name: tool.schema.name, arguments: JSON.stringify(args) }, alias, reply: Promise.withResolvers(), exported: false, settled: false, off: () => signal.removeEventListener('abort', abort) };
|
|
@@ -101,7 +87,7 @@ export class ToolBridge {
|
|
|
101
87
|
if (this.localTurnEnded)
|
|
102
88
|
for (const p of this.calls.values())
|
|
103
89
|
if (!p.exported && !p.settled) {
|
|
104
|
-
batch.push({ pending: p, result: errorResult(
|
|
90
|
+
batch.push({ pending: p, result: errorResult(JSON.stringify({ code: -32603 })) });
|
|
105
91
|
this.record({ kind: 'tool', severity: 'info', code: 'TOOLBRIDGE_CONCLUDED_UNDISPATCHED', summary: 'DSH工具结束了本轮,尚未交给DSH的调用没有执行。', payload: { callId: p.block.id } });
|
|
106
92
|
}
|
|
107
93
|
const ready = batch.filter(row => !row.pending.settled);
|
|
@@ -122,7 +108,7 @@ export class ToolBridge {
|
|
|
122
108
|
}
|
|
123
109
|
if (block.type === 'image') {
|
|
124
110
|
if (block.offloaded)
|
|
125
|
-
return errorResult(
|
|
111
|
+
return errorResult(JSON.stringify({ code: -32603 }));
|
|
126
112
|
if (!this.readImage)
|
|
127
113
|
throw new NeedError('ATTACHMENT_REQUIRED', '我需要官方附件组件读取工具返回的图片。', 'attachments.readImage');
|
|
128
114
|
const image = await this.readImage(block.attachment, signal);
|
|
@@ -132,7 +118,7 @@ export class ToolBridge {
|
|
|
132
118
|
content.push({ type: 'image', mimeType: image.ref.mediaType, data: Buffer.from(image.data).toString('base64') });
|
|
133
119
|
continue;
|
|
134
120
|
}
|
|
135
|
-
return errorResult(
|
|
121
|
+
return errorResult(JSON.stringify({ code: -32603, data: { type: block.type } }));
|
|
136
122
|
}
|
|
137
123
|
return { content, isError: !!message.isError };
|
|
138
124
|
}
|
|
@@ -151,7 +137,7 @@ export class ToolBridge {
|
|
|
151
137
|
const u = update;
|
|
152
138
|
const raw = u.rawInput;
|
|
153
139
|
const name = u.name ?? u._meta?.claudeCode?.toolName;
|
|
154
|
-
if (this.isBridge(name) || raw?.server
|
|
140
|
+
if (this.isBridge(name) || ['employee', 'dsh'].includes(raw?.server) && this.isBridge(raw.tool) || this.isBridge(u.title))
|
|
155
141
|
this.nativeBridgeIds.add(u.toolCallId);
|
|
156
142
|
if ((u.status === 'completed' || u.status === 'failed') && this.nativeBridgeIds.has(u.toolCallId))
|
|
157
143
|
this.nativeTerminated.add(u.toolCallId);
|
|
@@ -184,7 +170,7 @@ export class ToolBridge {
|
|
|
184
170
|
if (!p.settled) {
|
|
185
171
|
p.settled = true;
|
|
186
172
|
p.off();
|
|
187
|
-
p.reply.resolve(errorResult(
|
|
173
|
+
p.reply.resolve(errorResult(JSON.stringify({ code: -32603 })));
|
|
188
174
|
}
|
|
189
175
|
this.changed();
|
|
190
176
|
}
|
|
@@ -194,28 +180,31 @@ export async function openToolBridgeMcp(bridge, maxBytes) {
|
|
|
194
180
|
const root = await mkdtemp('/private/tmp/aw-tb-'), socketPath = root + '/mcp';
|
|
195
181
|
const clients = new Set();
|
|
196
182
|
let closed = false;
|
|
197
|
-
const
|
|
183
|
+
const listeners = ['employee', 'dsh'].map(group => createServer(socket => {
|
|
198
184
|
if (closed) {
|
|
199
185
|
socket.destroy();
|
|
200
186
|
return;
|
|
201
187
|
}
|
|
202
188
|
const server = new Server({ name: 'ai-workbench-toolbridge', version: '0.9.0' }, { capabilities: { tools: {} } });
|
|
203
189
|
clients.add(server);
|
|
204
|
-
|
|
205
|
-
server.setRequestHandler(
|
|
190
|
+
const belongs = (name) => essentialBridgeTool(name) === (group === 'employee');
|
|
191
|
+
server.setRequestHandler(ListToolsRequestSchema, async () => ({ tools: bridge.tools.filter(tool => belongs(tool.name)) }));
|
|
192
|
+
server.setRequestHandler(CallToolRequestSchema, (r, extra) => belongs(r.params.name) ? bridge.call(r.params.name, r.params.arguments ?? {}, extra.signal) : Promise.resolve(errorResult(JSON.stringify({ code: -32601 }))));
|
|
206
193
|
server.onclose = () => clients.delete(server);
|
|
207
194
|
server.onerror = () => bridge.abandon('MCP_TRANSPORT_FAILED');
|
|
208
195
|
void server.connect(new EmployeeSocketTransport(socket, maxBytes)).catch(() => { socket.destroy(); bridge.abandon('MCP_CONNECT_FAILED'); });
|
|
209
|
-
});
|
|
196
|
+
}));
|
|
210
197
|
const close = async () => { if (closed)
|
|
211
|
-
return; closed = true; bridge.close(); await Promise.allSettled([...clients].map(c => c.close()));
|
|
212
|
-
|
|
198
|
+
return; closed = true; bridge.close(); await Promise.allSettled([...clients].map(c => c.close())); for (const listener of listeners)
|
|
199
|
+
if (listener.listening)
|
|
200
|
+
await new Promise(r => listener.close(() => r())); await rm(root, { recursive: true }); };
|
|
213
201
|
try {
|
|
214
202
|
const stat = await lstat(root);
|
|
215
203
|
if (stat.uid !== process.getuid?.() || (stat.mode & 0o077) !== 0)
|
|
216
204
|
throw Error('TOOLBRIDGE_PRIVATE_DIRECTORY_REQUIRED');
|
|
217
|
-
|
|
218
|
-
|
|
205
|
+
for (const [index, listener] of listeners.entries())
|
|
206
|
+
await new Promise((yes, no) => { listener.once('error', no); listener.listen(socketPath + index, () => { listener.removeListener('error', no); yes(); }); });
|
|
207
|
+
return { mcpServers: ['employee', 'dsh'].map((name, index) => ({ name, command: '/usr/bin/nc', args: ['-U', socketPath + index], env: [] })), close };
|
|
219
208
|
}
|
|
220
209
|
catch (error) {
|
|
221
210
|
await close();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ai-workbench/remote-agent",
|
|
3
|
-
"version": "0.9.0-toolbridge.
|
|
3
|
+
"version": "0.9.0-toolbridge.37-rc.4",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
"scripts": {
|
|
23
23
|
"test": "npm run build && node --test --test-concurrency=1 lib/test/*.test.js",
|
|
24
24
|
"typecheck": "node node_modules/typescript/bin/tsc --noEmit",
|
|
25
|
-
"build": "node node_modules/typescript/bin/tsc && node node_modules/typescript/bin/tsc -p tsconfig.build.json && node tools/build-client.mjs",
|
|
25
|
+
"build": "node tools/clean-build.mjs && node node_modules/typescript/bin/tsc && node node_modules/typescript/bin/tsc -p tsconfig.build.json && node tools/build-client.mjs",
|
|
26
26
|
"probe:host": "node tools/probe-host.ts",
|
|
27
27
|
"pack:artifact": "node tools/pack-artifact.mjs"
|
|
28
28
|
},
|
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
"dependencies": {
|
|
34
34
|
"@agentclientprotocol/sdk": "2.0.0",
|
|
35
35
|
"@modelcontextprotocol/sdk": "1.32.1",
|
|
36
|
-
"@openai/codex": "0.
|
|
36
|
+
"@openai/codex": "0.162.0",
|
|
37
37
|
"zod": "4.6.7"
|
|
38
38
|
},
|
|
39
39
|
"devDependencies": {
|
|
@@ -71,7 +71,8 @@
|
|
|
71
71
|
"react": "18.3.1",
|
|
72
72
|
"react-dom": "18.3.1",
|
|
73
73
|
"typescript": "5.9.3",
|
|
74
|
-
"zustand": "4.4.7"
|
|
74
|
+
"zustand": "4.4.7",
|
|
75
|
+
"@deepseek-ai/dsh-scope": "0.2.0-rc.2"
|
|
75
76
|
},
|
|
76
77
|
"peerDependencies": {
|
|
77
78
|
"@ai-workbench/connect": "~0.7.37",
|
|
@@ -85,7 +86,8 @@
|
|
|
85
86
|
"@deepseek-ai/dsh-storage-domain": ">=0.2.0-rc.2 <0.3.0-0",
|
|
86
87
|
"@deepseek-ai/dsh-typert-protocol": ">=0.2.0-rc.2 <0.3.0-0",
|
|
87
88
|
"@deepseek-ai/dsh-user-approval": ">=0.2.0-rc.2 <0.3.0-0",
|
|
88
|
-
"@deepseek-ai/schemastery": "^3.18.4"
|
|
89
|
+
"@deepseek-ai/schemastery": "^3.18.4",
|
|
90
|
+
"@deepseek-ai/dsh-scope": ">=0.2.0-rc.2 <0.3.0-0"
|
|
89
91
|
},
|
|
90
92
|
"dsh": {
|
|
91
93
|
"bundle": {
|