@sanqianx/claude-ai-workbench 0.13.2 → 0.13.3
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 +201 -201
- package/README.md +156 -156
- package/apps/agent-terminal/app.css +636 -613
- package/apps/agent-terminal/app.js +125 -65
- package/apps/agent-terminal/conversation-composer.js +44 -0
- package/apps/agent-terminal/index.html +171 -170
- package/apps/agent-terminal/panel-host-proof.html +152 -152
- package/apps/agent-terminal/panel-proof-content.html +41 -41
- package/apps/agent-terminal/right-workspace-dock.css +187 -159
- package/apps/agent-terminal/right-workspace-dock.js +623 -519
- package/apps/ai-profile-settings-host/index.html +25 -25
- package/apps/ai-profile-settings-host/page.css +1 -1
- package/apps/ai-profile-settings-host/page.js +113 -113
- package/apps/minimal-test-host/README.md +15 -15
- package/apps/minimal-test-host/host.css +2 -2
- package/apps/minimal-test-host/host.js +35 -35
- package/apps/minimal-test-host/index.html +58 -58
- package/apps/minimal-test-host/optional-mock/fault-injection.js +15 -15
- package/docs/sdk/FILE_LAYOUT.md +31 -31
- package/docs/sdk/INTEGRATION.md +126 -126
- package/docs/sdk/README.md +21 -21
- package/docs/sdk/SESSION_EVENTS.md +13 -13
- package/docs/sdk/openapi.yaml +8 -1
- package/package.json +1 -1
- package/packages/client/README.md +29 -29
- package/packages/client/index.d.ts +122 -122
- package/packages/client/index.js +260 -260
- package/packages/client/package.json +2 -2
- package/packages/contracts/README.md +9 -9
- package/packages/contracts/index.d.ts +519 -517
- package/packages/contracts/index.js +312 -312
- package/packages/contracts/package.json +1 -1
- package/packages/core/README.md +8 -8
- package/packages/core/index.d.ts +76 -76
- package/packages/core/index.js +222 -222
- package/packages/core/package.json +2 -2
- package/packages/server/README.md +17 -17
- package/packages/server/bin/agent-terminal-server.js +42 -42
- package/packages/server/bin/claude-workbench.js +225 -225
- package/packages/server/index.d.ts +171 -171
- package/packages/server/index.js +230 -230
- package/packages/server/lib/agent-discovery.js +133 -133
- package/packages/server/lib/agent-registry.js +143 -143
- package/packages/server/lib/agent-spawn-env.js +17 -17
- package/packages/server/lib/attachment-store.js +78 -78
- package/packages/server/lib/bridges/app-server-bridge.js +373 -373
- package/packages/server/lib/bridges/opencode-server-bridge.js +506 -506
- package/packages/server/lib/capture-settings-store.js +35 -35
- package/packages/server/lib/claude-history-recovery.js +87 -87
- package/packages/server/lib/cli-session-recovery.js +28 -28
- package/packages/server/lib/context-resolver.js +55 -55
- package/packages/server/lib/conversation-capture.js +51 -51
- package/packages/server/lib/delegation-phase-store.js +357 -357
- package/packages/server/lib/delegation-store.js +474 -468
- package/packages/server/lib/diagnostics.js +32 -32
- package/packages/server/lib/drivers/cli-json-runner.js +974 -974
- package/packages/server/lib/drivers/codex-driver.js +1442 -1442
- package/packages/server/lib/drivers/opencode-driver.js +1554 -1554
- package/packages/server/lib/drivers/zcode-driver.js +1250 -1250
- package/packages/server/lib/drivers/zcode-profile-runtime.js +71 -71
- package/packages/server/lib/event-stream.js +138 -138
- package/packages/server/lib/folder-picker.js +623 -623
- package/packages/server/lib/http-handler.js +339 -339
- package/packages/server/lib/json-file.js +26 -26
- package/packages/server/lib/knowledge-context.js +33 -33
- package/packages/server/lib/model-detection.js +295 -295
- package/packages/server/lib/parent-context.js +44 -0
- package/packages/server/lib/profile-store.js +283 -283
- package/packages/server/lib/project-store.js +102 -102
- package/packages/server/lib/runtime-loader.js +120 -120
- package/packages/server/lib/session-archive-store.js +53 -53
- package/packages/server/lib/session-history.js +612 -612
- package/packages/server/lib/system-doctor.js +125 -125
- package/packages/server/lib/ui-preferences-store.js +48 -48
- package/packages/server/lib/workbench-service.js +1696 -1675
- package/packages/server/package.json +2 -2
- package/packages/ui/README.md +30 -30
- package/packages/ui/ai-profile-settings.d.ts +45 -45
- package/packages/ui/ai-profile-settings.js +283 -283
- package/packages/ui/claude-terminal.d.ts +31 -31
- package/packages/ui/claude-terminal.js +343 -343
- package/packages/ui/index.d.ts +18 -18
- package/packages/ui/index.js +51 -51
- package/packages/ui/package.json +2 -2
- package/packages/ui/theme.css +13 -13
- package/src/assets/claude-prompts.json +21 -21
- package/src/backend/http/ai-profile-routes.js +61 -61
- package/src/backend/http/claude-routes.js +170 -170
- package/src/backend/http/node-helpers.js +45 -45
- package/src/backend/index.js +21 -21
- package/src/backend/lib/ai-adapter.js +144 -144
- package/src/backend/lib/ai-profile-store.js +259 -259
- package/src/backend/lib/ai-vendor-presets.js +83 -83
- package/src/backend/lib/ai-workspace.js +140 -140
- package/src/backend/lib/automation-config.js +253 -253
- package/src/backend/lib/claude-cli-runner.js +2392 -2392
- package/src/backend/lib/data-dir.js +169 -169
- package/src/backend/lib/llm-client.js +176 -176
- package/src/backend/lib/model-context-windows.js +82 -82
- package/src/backend/lib/native-task-control.js +217 -217
- package/src/backend/lib/native-task-history-index.js +475 -475
- package/src/backend/lib/native-task-history-reader.js +526 -526
- package/src/backend/lib/native-task-messages.js +457 -457
- package/src/backend/lib/native-task-registry.js +325 -325
- package/src/backend/lib/native-task-runtime.js +242 -242
- package/src/backend/lib/native-task-trace.js +440 -440
- package/src/backend/lib/native-task-transcript.js +393 -393
- package/src/backend/lib/prompt-registry.js +166 -166
- package/src/backend/lib/runtime-config.js +121 -121
- package/src/backend/lib/sdk-session-stream.js +143 -143
- package/src/backend/lib/sdk-turn-stream.js +16 -16
- package/src/backend/lib/store-scope.js +131 -131
- package/src/backend/lib/task-identity.js +104 -104
- package/src/backend/lib/task-state-projection.js +58 -58
- package/src/index.js +7 -7
- package/src/sdk-wave-a.ts +1 -1
- package/src/sdk-wave-b.ts +1 -1
- package/src/sdk-wave-c.ts +1 -1
- package/src/sdk-wave-d.ts +1 -1
- package/src/sdk-wave-e.ts +1 -1
package/packages/client/index.js
CHANGED
|
@@ -1,260 +1,260 @@
|
|
|
1
|
-
(function (global, factory) {
|
|
2
|
-
const exported = factory();
|
|
3
|
-
if (typeof module !== 'undefined' && module.exports) module.exports = exported;
|
|
4
|
-
global.createClaudeWorkbenchClient = exported.createClaudeWorkbenchClient;
|
|
5
|
-
})(typeof window !== 'undefined' ? window : globalThis, function () {
|
|
6
|
-
'use strict';
|
|
7
|
-
|
|
8
|
-
const EVENT_TYPES = [
|
|
9
|
-
'workbench/stream-open', 'workbench/stream-reconnecting', 'workbench/resync', 'claude/init', 'claude/state',
|
|
10
|
-
'claude/session-ready', 'claude/user-prompt', 'claude/system-prompt', 'claude/text-start',
|
|
11
|
-
'claude/text-delta', 'claude/result', 'claude/thinking-start', 'claude/thinking-delta',
|
|
12
|
-
'claude/tool-use', 'claude/tool-result', 'claude/permission-request', 'claude/permission-resolved',
|
|
13
|
-
'claude/usage', 'claude/commands', 'claude/image', 'claude/retry', 'claude/error', 'claude/stderr', 'claude/aborted',
|
|
14
|
-
'claude/selection-changed',
|
|
15
|
-
'claude/turn-end', 'claude/task-started', 'claude/task-progress', 'claude/task-updated', 'claude/task-notification',
|
|
16
|
-
'claude/native-trace', 'claude/native-task-state', 'claude/native-task-transcript', 'claude/task-interrupted',
|
|
17
|
-
'workbench/sessions-snapshot', 'workbench/session-changed',
|
|
18
|
-
];
|
|
19
|
-
|
|
20
|
-
function createClaudeWorkbenchClient(options = {}) {
|
|
21
|
-
const apiBase = String(options.apiBase || '/api/claude-workbench/v1').replace(/\/$/, '');
|
|
22
|
-
const fetchImpl = options.fetch || globalThis.fetch;
|
|
23
|
-
const EventSourceImpl = options.EventSource || globalThis.EventSource;
|
|
24
|
-
if (typeof fetchImpl !== 'function') throw new Error('fetch is required');
|
|
25
|
-
const streams = new Set();
|
|
26
|
-
const controllers = new Set();
|
|
27
|
-
const readTimeoutMs = options.readTimeoutMs ?? 3000;
|
|
28
|
-
let destroyed = false;
|
|
29
|
-
let reconnects = 0;
|
|
30
|
-
|
|
31
|
-
async function request(path, init = {}) {
|
|
32
|
-
if (destroyed) throw new Error('ClaudeWorkbenchClient is destroyed');
|
|
33
|
-
const controller = new AbortController();
|
|
34
|
-
const external = init.signal;
|
|
35
|
-
const abort = () => controller.abort();
|
|
36
|
-
if (external) {
|
|
37
|
-
if (external.aborted) controller.abort();
|
|
38
|
-
else external.addEventListener('abort', abort, { once: true });
|
|
39
|
-
}
|
|
40
|
-
controllers.add(controller);
|
|
41
|
-
const timer = (init.method || 'GET') === 'GET' && readTimeoutMs > 0 ? setTimeout(abort, readTimeoutMs) : null;
|
|
42
|
-
try {
|
|
43
|
-
const response = await fetchImpl(`${apiBase}${path}`, {
|
|
44
|
-
...init,
|
|
45
|
-
headers: { 'Content-Type': 'application/json', ...(init.headers || {}) },
|
|
46
|
-
signal: controller.signal,
|
|
47
|
-
});
|
|
48
|
-
const text = await response.text();
|
|
49
|
-
const data = text ? JSON.parse(text) : null;
|
|
50
|
-
if (!response.ok) {
|
|
51
|
-
const error = new Error(data && data.error || `HTTP ${response.status}`);
|
|
52
|
-
error.status = response.status; error.body = data;
|
|
53
|
-
// The server's structured verdicts (409 code+resync, 503 latched
|
|
54
|
-
// failure codes) ride on the error itself so callers can react
|
|
55
|
-
// without inspecting error.body. Additive only: status and body
|
|
56
|
-
// keep their original meaning.
|
|
57
|
-
if (data && typeof data === 'object') {
|
|
58
|
-
if (data.code !== undefined) error.code = data.code;
|
|
59
|
-
if (data.resync === true) error.resync = true;
|
|
60
|
-
}
|
|
61
|
-
throw error;
|
|
62
|
-
}
|
|
63
|
-
return data;
|
|
64
|
-
} finally {
|
|
65
|
-
clearTimeout(timer);
|
|
66
|
-
controllers.delete(controller);
|
|
67
|
-
if (external) external.removeEventListener('abort', abort);
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
function subscribe(sessionId, handlers, onError, subscribeOptions = {}, route) {
|
|
72
|
-
if (destroyed) throw new Error('ClaudeWorkbenchClient is destroyed');
|
|
73
|
-
if (typeof EventSourceImpl !== 'function') throw new Error('EventSource is required');
|
|
74
|
-
const callback = typeof handlers === 'function' ? handlers : handlers && handlers.onEvent;
|
|
75
|
-
const errorCallback = typeof onError === 'function' ? onError : handlers && handlers.onError;
|
|
76
|
-
const openCallback = subscribeOptions.onOpen || handlers && typeof handlers === 'object' && handlers.onOpen;
|
|
77
|
-
const after = Number(subscribeOptions.afterSequence || handlers && handlers.afterSequence || 0);
|
|
78
|
-
const suffix = after > 0 ? `?after=${encodeURIComponent(after)}` : '';
|
|
79
|
-
const stream = new EventSourceImpl(route || `${apiBase}/sessions/${encodeURIComponent(sessionId)}/events${suffix}`);
|
|
80
|
-
streams.add(stream);
|
|
81
|
-
let closed = false;
|
|
82
|
-
const deliver = message => {
|
|
83
|
-
try { callback && callback(JSON.parse(message.data)); }
|
|
84
|
-
catch (error) { errorCallback && errorCallback(error); }
|
|
85
|
-
};
|
|
86
|
-
EVENT_TYPES.forEach(type => stream.addEventListener && stream.addEventListener(type, deliver));
|
|
87
|
-
stream.onmessage = deliver;
|
|
88
|
-
stream.onopen = () => { openCallback && openCallback(); };
|
|
89
|
-
stream.onerror = () => {
|
|
90
|
-
reconnects += 1;
|
|
91
|
-
errorCallback && errorCallback(new Error('SSE connection interrupted; EventSource will reconnect'));
|
|
92
|
-
};
|
|
93
|
-
const close = () => {
|
|
94
|
-
if (closed) return; closed = true; streams.delete(stream); stream.close();
|
|
95
|
-
};
|
|
96
|
-
return close;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
const client = {
|
|
100
|
-
health: () => request('/health'),
|
|
101
|
-
runtime: () => request('/runtime'),
|
|
102
|
-
async listAgents() { const result = await request('/agents'); return result.agents || result || []; },
|
|
103
|
-
agentStatus: () => request('/agents'),
|
|
104
|
-
refreshAgents: () => request('/agents/refresh', { method: 'POST', body: '{}' }),
|
|
105
|
-
async listProjects() { const result = await request('/projects'); return result.projects || result || []; },
|
|
106
|
-
createProject(project) {
|
|
107
|
-
return request('/projects', { method: 'POST', body: JSON.stringify(project) });
|
|
108
|
-
},
|
|
109
|
-
getProject(id) { return request(`/projects/${encodeURIComponent(id)}`); },
|
|
110
|
-
renameProject(id, name) {
|
|
111
|
-
return request(`/projects/${encodeURIComponent(id)}`, { method: 'PATCH', body: JSON.stringify({ name }) });
|
|
112
|
-
},
|
|
113
|
-
updateProject(id, patch) {
|
|
114
|
-
return request(`/projects/${encodeURIComponent(id)}`, { method: 'PATCH', body: JSON.stringify(patch) });
|
|
115
|
-
},
|
|
116
|
-
removeProject(id, opts = {}) {
|
|
117
|
-
const qs = opts.keepSessions ? '?keepSessions=1' : '';
|
|
118
|
-
return request(`/projects/${encodeURIComponent(id)}${qs}`, { method: 'DELETE' });
|
|
119
|
-
},
|
|
120
|
-
async listProfiles() { const result = await request('/profiles'); return result.profiles || result || []; },
|
|
121
|
-
saveProfile(profile) {
|
|
122
|
-
const id = encodeURIComponent(profile.id);
|
|
123
|
-
return request(`/profiles/${id}`, { method: 'PUT', body: JSON.stringify(profile) });
|
|
124
|
-
},
|
|
125
|
-
deleteProfile(id) { return request(`/profiles/${encodeURIComponent(id)}`, { method: 'DELETE' }); },
|
|
126
|
-
setCredential(id, credential) {
|
|
127
|
-
const payload = typeof credential === 'string' ? { secret: credential } : credential;
|
|
128
|
-
return request(`/profiles/${encodeURIComponent(id)}/credential`, { method: 'PUT', body: JSON.stringify(payload) });
|
|
129
|
-
},
|
|
130
|
-
testProfile(id) { return request(`/profiles/${encodeURIComponent(id)}/test`, { method: 'POST', body: '{}' }); },
|
|
131
|
-
testProfileDraft(profile, credential = '') {
|
|
132
|
-
return request('/profiles/test', { method: 'POST', body: JSON.stringify({ profile, credential }) });
|
|
133
|
-
},
|
|
134
|
-
detectProfileModels(profile, credential = '') {
|
|
135
|
-
return request('/profiles/detect-models', { method: 'POST', body: JSON.stringify({ profile, credential }) });
|
|
136
|
-
},
|
|
137
|
-
async listSessions(context, opts = {}) {
|
|
138
|
-
const contextId = context && (context.contextId || context.projectId) || context;
|
|
139
|
-
const archived = opts.includeArchived === true || opts.archived === true ? '&archived=1' : '';
|
|
140
|
-
const result = await request(`/sessions?contextId=${encodeURIComponent(contextId)}${archived}`, opts);
|
|
141
|
-
return result.sessions || result || [];
|
|
142
|
-
},
|
|
143
|
-
startSession(context, opts = {}) {
|
|
144
|
-
const payloadContext = { ...context, contextId: context.contextId || context.projectId };
|
|
145
|
-
const result = request('/sessions', {
|
|
146
|
-
method: 'POST',
|
|
147
|
-
body: JSON.stringify({
|
|
148
|
-
context: payloadContext,
|
|
149
|
-
aiProfileId: opts.aiProfileId !== undefined ? opts.aiProfileId : context.aiProfileId,
|
|
150
|
-
model: opts.model !== undefined ? opts.model : context.model,
|
|
151
|
-
permissionMode: opts.permissionMode || 'default',
|
|
152
|
-
agentId: opts.agentId || undefined,
|
|
153
|
-
effort: opts.effort || undefined,
|
|
154
|
-
inputOrigin: opts.inputOrigin || 'human',
|
|
155
|
-
}),
|
|
156
|
-
signal: opts.signal,
|
|
157
|
-
});
|
|
158
|
-
return result.then(r => r.session || r);
|
|
159
|
-
},
|
|
160
|
-
loadSession(id, opts = {}) { return request(`/sessions/${encodeURIComponent(id)}`, opts); },
|
|
161
|
-
loadHistory(id, opts = {}) {
|
|
162
|
-
const query = new URLSearchParams();
|
|
163
|
-
if (opts.before != null) query.set('before', opts.before);
|
|
164
|
-
if (opts.limit != null) query.set('limit', opts.limit);
|
|
165
|
-
return request(`/sessions/${encodeURIComponent(id)}/history?${query}`, { signal: opts.signal });
|
|
166
|
-
},
|
|
167
|
-
listNativeTasks(id, scope = {}, opts = {}) {
|
|
168
|
-
const query = new URLSearchParams({ storeScope: scope.storeScope, contextId: scope.contextId });
|
|
169
|
-
return request(`/sessions/${encodeURIComponent(id)}/native-tasks?${query}`, { signal: opts.signal });
|
|
170
|
-
},
|
|
171
|
-
loadNativeTaskHistory(id, opts = {}) {
|
|
172
|
-
const body = { taskRef: opts.taskRef };
|
|
173
|
-
if (opts.cursor != null) body.cursor = opts.cursor;
|
|
174
|
-
if (opts.limit !== undefined) body.limit = opts.limit;
|
|
175
|
-
return request(`/sessions/${encodeURIComponent(id)}/native-tasks/history`, {
|
|
176
|
-
method: 'POST', body: JSON.stringify(body), signal: opts.signal,
|
|
177
|
-
});
|
|
178
|
-
},
|
|
179
|
-
// Delegated sessions. createDelegation issues one Workbench delegation
|
|
180
|
-
// command (idempotent by requestId; accepted ≠ model receipt — a
|
|
181
|
-
// dispatch-unknown verdict must never be blindly re-sent). listDelegations
|
|
182
|
-
// is a pure read of the trusted relations and real child status.
|
|
183
|
-
createDelegation(id, payload = {}, opts = {}) {
|
|
184
|
-
return request(`/sessions/${encodeURIComponent(id)}/delegations`, {
|
|
185
|
-
method: 'POST', body: JSON.stringify(payload), signal: opts.signal,
|
|
186
|
-
});
|
|
187
|
-
},
|
|
188
|
-
listDelegations(id, scope = {}, opts = {}) {
|
|
189
|
-
const query = new URLSearchParams({ storeScope: scope.storeScope, contextId: scope.contextId });
|
|
190
|
-
return request(`/sessions/${encodeURIComponent(id)}/delegations?${query}`, { signal: opts.signal });
|
|
191
|
-
},
|
|
192
|
-
async listCommands(id, opts = {}) {
|
|
193
|
-
const result = await request(`/sessions/${encodeURIComponent(id)}/commands`, opts);
|
|
194
|
-
return result.commands || result || [];
|
|
195
|
-
},
|
|
196
|
-
subscribe,
|
|
197
|
-
subscribeSessionChanges(contextId, handlers, onError, opts = {}) {
|
|
198
|
-
const query = new URLSearchParams({ contextId });
|
|
199
|
-
if (opts.afterRevision) query.set('after', opts.afterRevision);
|
|
200
|
-
let revision = Number(opts.afterRevision) || 0;
|
|
201
|
-
return subscribe(null, event => {
|
|
202
|
-
if (event.type === 'workbench/resync' || event.type === 'workbench/sessions-snapshot') revision = Number(event.revision) || 0;
|
|
203
|
-
else if (event.type === 'workbench/session-changed') {
|
|
204
|
-
if (Number(event.revision) <= revision) return;
|
|
205
|
-
revision = Number(event.revision);
|
|
206
|
-
} else return;
|
|
207
|
-
handlers(event);
|
|
208
|
-
}, onError, opts, `${apiBase}/sessions/changes?${query}`);
|
|
209
|
-
},
|
|
210
|
-
send(sessionOrArgs, text, opts = {}) {
|
|
211
|
-
const args = typeof sessionOrArgs === 'object' ? sessionOrArgs : { sessionId: sessionOrArgs, text, options: opts };
|
|
212
|
-
return request(`/sessions/${encodeURIComponent(args.sessionId)}/input`, {
|
|
213
|
-
method: 'POST', body: JSON.stringify({ text: args.text || '', attachments: args.attachments || [], options: args.options || {} }),
|
|
214
|
-
});
|
|
215
|
-
},
|
|
216
|
-
resolvePermission(sessionOrArgs, requestId, allow, message) {
|
|
217
|
-
const args = typeof sessionOrArgs === 'object' ? sessionOrArgs : { sessionId: sessionOrArgs, requestId, allow, message };
|
|
218
|
-
return request(`/sessions/${encodeURIComponent(args.sessionId)}/permissions/${encodeURIComponent(args.requestId)}/resolve`, {
|
|
219
|
-
method: 'POST', body: JSON.stringify({ allow: args.allow === true, message: args.message || '' }),
|
|
220
|
-
});
|
|
221
|
-
},
|
|
222
|
-
abort(sessionOrArgs) {
|
|
223
|
-
const id = typeof sessionOrArgs === 'object' ? sessionOrArgs.sessionId : sessionOrArgs;
|
|
224
|
-
return request(`/sessions/${encodeURIComponent(id)}/abort`, { method: 'POST', body: '{}' });
|
|
225
|
-
},
|
|
226
|
-
setPermissionMode(sessionOrArgs, mode) {
|
|
227
|
-
const args = typeof sessionOrArgs === 'object' ? sessionOrArgs : { sessionId: sessionOrArgs, mode };
|
|
228
|
-
return request(`/sessions/${encodeURIComponent(args.sessionId)}/permission-mode`, {
|
|
229
|
-
method: 'PUT', body: JSON.stringify({ mode: args.mode }),
|
|
230
|
-
});
|
|
231
|
-
},
|
|
232
|
-
updateSessionSelection(sessionOrArgs, selection = {}) {
|
|
233
|
-
const args = typeof sessionOrArgs === 'object' ? sessionOrArgs : { sessionId: sessionOrArgs, ...selection };
|
|
234
|
-
return request(`/sessions/${encodeURIComponent(args.sessionId)}/selection`, {
|
|
235
|
-
method: 'PUT',
|
|
236
|
-
body: JSON.stringify({ aiProfileId: args.aiProfileId !== undefined ? args.aiProfileId : selection.aiProfileId, model: args.model !== undefined ? args.model : selection.model }),
|
|
237
|
-
});
|
|
238
|
-
},
|
|
239
|
-
archiveSession(sessionOrArgs) {
|
|
240
|
-
const id = typeof sessionOrArgs === 'object' ? sessionOrArgs.sessionId : sessionOrArgs;
|
|
241
|
-
return request(`/sessions/${encodeURIComponent(id)}/archive`, { method: 'PUT', body: '{}' });
|
|
242
|
-
},
|
|
243
|
-
restoreSession(sessionOrArgs) {
|
|
244
|
-
const id = typeof sessionOrArgs === 'object' ? sessionOrArgs.sessionId : sessionOrArgs;
|
|
245
|
-
return request(`/sessions/${encodeURIComponent(id)}/archive`, { method: 'DELETE' });
|
|
246
|
-
},
|
|
247
|
-
resourceCounts() { return { streams: streams.size, requests: controllers.size, reconnects }; },
|
|
248
|
-
destroy() {
|
|
249
|
-
destroyed = true;
|
|
250
|
-
for (const stream of streams) try { stream.close(); } catch {}
|
|
251
|
-
streams.clear();
|
|
252
|
-
for (const controller of controllers) controller.abort();
|
|
253
|
-
controllers.clear();
|
|
254
|
-
},
|
|
255
|
-
};
|
|
256
|
-
return client;
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
return { createClaudeWorkbenchClient, EVENT_TYPES };
|
|
260
|
-
});
|
|
1
|
+
(function (global, factory) {
|
|
2
|
+
const exported = factory();
|
|
3
|
+
if (typeof module !== 'undefined' && module.exports) module.exports = exported;
|
|
4
|
+
global.createClaudeWorkbenchClient = exported.createClaudeWorkbenchClient;
|
|
5
|
+
})(typeof window !== 'undefined' ? window : globalThis, function () {
|
|
6
|
+
'use strict';
|
|
7
|
+
|
|
8
|
+
const EVENT_TYPES = [
|
|
9
|
+
'workbench/stream-open', 'workbench/stream-reconnecting', 'workbench/resync', 'claude/init', 'claude/state',
|
|
10
|
+
'claude/session-ready', 'claude/user-prompt', 'claude/system-prompt', 'claude/text-start',
|
|
11
|
+
'claude/text-delta', 'claude/result', 'claude/thinking-start', 'claude/thinking-delta',
|
|
12
|
+
'claude/tool-use', 'claude/tool-result', 'claude/permission-request', 'claude/permission-resolved',
|
|
13
|
+
'claude/usage', 'claude/commands', 'claude/image', 'claude/retry', 'claude/error', 'claude/stderr', 'claude/aborted',
|
|
14
|
+
'claude/selection-changed',
|
|
15
|
+
'claude/turn-end', 'claude/task-started', 'claude/task-progress', 'claude/task-updated', 'claude/task-notification',
|
|
16
|
+
'claude/native-trace', 'claude/native-task-state', 'claude/native-task-transcript', 'claude/task-interrupted',
|
|
17
|
+
'workbench/sessions-snapshot', 'workbench/session-changed',
|
|
18
|
+
];
|
|
19
|
+
|
|
20
|
+
function createClaudeWorkbenchClient(options = {}) {
|
|
21
|
+
const apiBase = String(options.apiBase || '/api/claude-workbench/v1').replace(/\/$/, '');
|
|
22
|
+
const fetchImpl = options.fetch || globalThis.fetch;
|
|
23
|
+
const EventSourceImpl = options.EventSource || globalThis.EventSource;
|
|
24
|
+
if (typeof fetchImpl !== 'function') throw new Error('fetch is required');
|
|
25
|
+
const streams = new Set();
|
|
26
|
+
const controllers = new Set();
|
|
27
|
+
const readTimeoutMs = options.readTimeoutMs ?? 3000;
|
|
28
|
+
let destroyed = false;
|
|
29
|
+
let reconnects = 0;
|
|
30
|
+
|
|
31
|
+
async function request(path, init = {}) {
|
|
32
|
+
if (destroyed) throw new Error('ClaudeWorkbenchClient is destroyed');
|
|
33
|
+
const controller = new AbortController();
|
|
34
|
+
const external = init.signal;
|
|
35
|
+
const abort = () => controller.abort();
|
|
36
|
+
if (external) {
|
|
37
|
+
if (external.aborted) controller.abort();
|
|
38
|
+
else external.addEventListener('abort', abort, { once: true });
|
|
39
|
+
}
|
|
40
|
+
controllers.add(controller);
|
|
41
|
+
const timer = (init.method || 'GET') === 'GET' && readTimeoutMs > 0 ? setTimeout(abort, readTimeoutMs) : null;
|
|
42
|
+
try {
|
|
43
|
+
const response = await fetchImpl(`${apiBase}${path}`, {
|
|
44
|
+
...init,
|
|
45
|
+
headers: { 'Content-Type': 'application/json', ...(init.headers || {}) },
|
|
46
|
+
signal: controller.signal,
|
|
47
|
+
});
|
|
48
|
+
const text = await response.text();
|
|
49
|
+
const data = text ? JSON.parse(text) : null;
|
|
50
|
+
if (!response.ok) {
|
|
51
|
+
const error = new Error(data && data.error || `HTTP ${response.status}`);
|
|
52
|
+
error.status = response.status; error.body = data;
|
|
53
|
+
// The server's structured verdicts (409 code+resync, 503 latched
|
|
54
|
+
// failure codes) ride on the error itself so callers can react
|
|
55
|
+
// without inspecting error.body. Additive only: status and body
|
|
56
|
+
// keep their original meaning.
|
|
57
|
+
if (data && typeof data === 'object') {
|
|
58
|
+
if (data.code !== undefined) error.code = data.code;
|
|
59
|
+
if (data.resync === true) error.resync = true;
|
|
60
|
+
}
|
|
61
|
+
throw error;
|
|
62
|
+
}
|
|
63
|
+
return data;
|
|
64
|
+
} finally {
|
|
65
|
+
clearTimeout(timer);
|
|
66
|
+
controllers.delete(controller);
|
|
67
|
+
if (external) external.removeEventListener('abort', abort);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function subscribe(sessionId, handlers, onError, subscribeOptions = {}, route) {
|
|
72
|
+
if (destroyed) throw new Error('ClaudeWorkbenchClient is destroyed');
|
|
73
|
+
if (typeof EventSourceImpl !== 'function') throw new Error('EventSource is required');
|
|
74
|
+
const callback = typeof handlers === 'function' ? handlers : handlers && handlers.onEvent;
|
|
75
|
+
const errorCallback = typeof onError === 'function' ? onError : handlers && handlers.onError;
|
|
76
|
+
const openCallback = subscribeOptions.onOpen || handlers && typeof handlers === 'object' && handlers.onOpen;
|
|
77
|
+
const after = Number(subscribeOptions.afterSequence || handlers && handlers.afterSequence || 0);
|
|
78
|
+
const suffix = after > 0 ? `?after=${encodeURIComponent(after)}` : '';
|
|
79
|
+
const stream = new EventSourceImpl(route || `${apiBase}/sessions/${encodeURIComponent(sessionId)}/events${suffix}`);
|
|
80
|
+
streams.add(stream);
|
|
81
|
+
let closed = false;
|
|
82
|
+
const deliver = message => {
|
|
83
|
+
try { callback && callback(JSON.parse(message.data)); }
|
|
84
|
+
catch (error) { errorCallback && errorCallback(error); }
|
|
85
|
+
};
|
|
86
|
+
EVENT_TYPES.forEach(type => stream.addEventListener && stream.addEventListener(type, deliver));
|
|
87
|
+
stream.onmessage = deliver;
|
|
88
|
+
stream.onopen = () => { openCallback && openCallback(); };
|
|
89
|
+
stream.onerror = () => {
|
|
90
|
+
reconnects += 1;
|
|
91
|
+
errorCallback && errorCallback(new Error('SSE connection interrupted; EventSource will reconnect'));
|
|
92
|
+
};
|
|
93
|
+
const close = () => {
|
|
94
|
+
if (closed) return; closed = true; streams.delete(stream); stream.close();
|
|
95
|
+
};
|
|
96
|
+
return close;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const client = {
|
|
100
|
+
health: () => request('/health'),
|
|
101
|
+
runtime: () => request('/runtime'),
|
|
102
|
+
async listAgents() { const result = await request('/agents'); return result.agents || result || []; },
|
|
103
|
+
agentStatus: () => request('/agents'),
|
|
104
|
+
refreshAgents: () => request('/agents/refresh', { method: 'POST', body: '{}' }),
|
|
105
|
+
async listProjects() { const result = await request('/projects'); return result.projects || result || []; },
|
|
106
|
+
createProject(project) {
|
|
107
|
+
return request('/projects', { method: 'POST', body: JSON.stringify(project) });
|
|
108
|
+
},
|
|
109
|
+
getProject(id) { return request(`/projects/${encodeURIComponent(id)}`); },
|
|
110
|
+
renameProject(id, name) {
|
|
111
|
+
return request(`/projects/${encodeURIComponent(id)}`, { method: 'PATCH', body: JSON.stringify({ name }) });
|
|
112
|
+
},
|
|
113
|
+
updateProject(id, patch) {
|
|
114
|
+
return request(`/projects/${encodeURIComponent(id)}`, { method: 'PATCH', body: JSON.stringify(patch) });
|
|
115
|
+
},
|
|
116
|
+
removeProject(id, opts = {}) {
|
|
117
|
+
const qs = opts.keepSessions ? '?keepSessions=1' : '';
|
|
118
|
+
return request(`/projects/${encodeURIComponent(id)}${qs}`, { method: 'DELETE' });
|
|
119
|
+
},
|
|
120
|
+
async listProfiles() { const result = await request('/profiles'); return result.profiles || result || []; },
|
|
121
|
+
saveProfile(profile) {
|
|
122
|
+
const id = encodeURIComponent(profile.id);
|
|
123
|
+
return request(`/profiles/${id}`, { method: 'PUT', body: JSON.stringify(profile) });
|
|
124
|
+
},
|
|
125
|
+
deleteProfile(id) { return request(`/profiles/${encodeURIComponent(id)}`, { method: 'DELETE' }); },
|
|
126
|
+
setCredential(id, credential) {
|
|
127
|
+
const payload = typeof credential === 'string' ? { secret: credential } : credential;
|
|
128
|
+
return request(`/profiles/${encodeURIComponent(id)}/credential`, { method: 'PUT', body: JSON.stringify(payload) });
|
|
129
|
+
},
|
|
130
|
+
testProfile(id) { return request(`/profiles/${encodeURIComponent(id)}/test`, { method: 'POST', body: '{}' }); },
|
|
131
|
+
testProfileDraft(profile, credential = '') {
|
|
132
|
+
return request('/profiles/test', { method: 'POST', body: JSON.stringify({ profile, credential }) });
|
|
133
|
+
},
|
|
134
|
+
detectProfileModels(profile, credential = '') {
|
|
135
|
+
return request('/profiles/detect-models', { method: 'POST', body: JSON.stringify({ profile, credential }) });
|
|
136
|
+
},
|
|
137
|
+
async listSessions(context, opts = {}) {
|
|
138
|
+
const contextId = context && (context.contextId || context.projectId) || context;
|
|
139
|
+
const archived = opts.includeArchived === true || opts.archived === true ? '&archived=1' : '';
|
|
140
|
+
const result = await request(`/sessions?contextId=${encodeURIComponent(contextId)}${archived}`, opts);
|
|
141
|
+
return result.sessions || result || [];
|
|
142
|
+
},
|
|
143
|
+
startSession(context, opts = {}) {
|
|
144
|
+
const payloadContext = { ...context, contextId: context.contextId || context.projectId };
|
|
145
|
+
const result = request('/sessions', {
|
|
146
|
+
method: 'POST',
|
|
147
|
+
body: JSON.stringify({
|
|
148
|
+
context: payloadContext,
|
|
149
|
+
aiProfileId: opts.aiProfileId !== undefined ? opts.aiProfileId : context.aiProfileId,
|
|
150
|
+
model: opts.model !== undefined ? opts.model : context.model,
|
|
151
|
+
permissionMode: opts.permissionMode || 'default',
|
|
152
|
+
agentId: opts.agentId || undefined,
|
|
153
|
+
effort: opts.effort || undefined,
|
|
154
|
+
inputOrigin: opts.inputOrigin || 'human',
|
|
155
|
+
}),
|
|
156
|
+
signal: opts.signal,
|
|
157
|
+
});
|
|
158
|
+
return result.then(r => r.session || r);
|
|
159
|
+
},
|
|
160
|
+
loadSession(id, opts = {}) { return request(`/sessions/${encodeURIComponent(id)}`, opts); },
|
|
161
|
+
loadHistory(id, opts = {}) {
|
|
162
|
+
const query = new URLSearchParams();
|
|
163
|
+
if (opts.before != null) query.set('before', opts.before);
|
|
164
|
+
if (opts.limit != null) query.set('limit', opts.limit);
|
|
165
|
+
return request(`/sessions/${encodeURIComponent(id)}/history?${query}`, { signal: opts.signal });
|
|
166
|
+
},
|
|
167
|
+
listNativeTasks(id, scope = {}, opts = {}) {
|
|
168
|
+
const query = new URLSearchParams({ storeScope: scope.storeScope, contextId: scope.contextId });
|
|
169
|
+
return request(`/sessions/${encodeURIComponent(id)}/native-tasks?${query}`, { signal: opts.signal });
|
|
170
|
+
},
|
|
171
|
+
loadNativeTaskHistory(id, opts = {}) {
|
|
172
|
+
const body = { taskRef: opts.taskRef };
|
|
173
|
+
if (opts.cursor != null) body.cursor = opts.cursor;
|
|
174
|
+
if (opts.limit !== undefined) body.limit = opts.limit;
|
|
175
|
+
return request(`/sessions/${encodeURIComponent(id)}/native-tasks/history`, {
|
|
176
|
+
method: 'POST', body: JSON.stringify(body), signal: opts.signal,
|
|
177
|
+
});
|
|
178
|
+
},
|
|
179
|
+
// Delegated sessions. createDelegation issues one Workbench delegation
|
|
180
|
+
// command (idempotent by requestId; accepted ≠ model receipt — a
|
|
181
|
+
// dispatch-unknown verdict must never be blindly re-sent). listDelegations
|
|
182
|
+
// is a pure read of the trusted relations and real child status.
|
|
183
|
+
createDelegation(id, payload = {}, opts = {}) {
|
|
184
|
+
return request(`/sessions/${encodeURIComponent(id)}/delegations`, {
|
|
185
|
+
method: 'POST', body: JSON.stringify(payload), signal: opts.signal,
|
|
186
|
+
});
|
|
187
|
+
},
|
|
188
|
+
listDelegations(id, scope = {}, opts = {}) {
|
|
189
|
+
const query = new URLSearchParams({ storeScope: scope.storeScope, contextId: scope.contextId });
|
|
190
|
+
return request(`/sessions/${encodeURIComponent(id)}/delegations?${query}`, { signal: opts.signal });
|
|
191
|
+
},
|
|
192
|
+
async listCommands(id, opts = {}) {
|
|
193
|
+
const result = await request(`/sessions/${encodeURIComponent(id)}/commands`, opts);
|
|
194
|
+
return result.commands || result || [];
|
|
195
|
+
},
|
|
196
|
+
subscribe,
|
|
197
|
+
subscribeSessionChanges(contextId, handlers, onError, opts = {}) {
|
|
198
|
+
const query = new URLSearchParams({ contextId });
|
|
199
|
+
if (opts.afterRevision) query.set('after', opts.afterRevision);
|
|
200
|
+
let revision = Number(opts.afterRevision) || 0;
|
|
201
|
+
return subscribe(null, event => {
|
|
202
|
+
if (event.type === 'workbench/resync' || event.type === 'workbench/sessions-snapshot') revision = Number(event.revision) || 0;
|
|
203
|
+
else if (event.type === 'workbench/session-changed') {
|
|
204
|
+
if (Number(event.revision) <= revision) return;
|
|
205
|
+
revision = Number(event.revision);
|
|
206
|
+
} else return;
|
|
207
|
+
handlers(event);
|
|
208
|
+
}, onError, opts, `${apiBase}/sessions/changes?${query}`);
|
|
209
|
+
},
|
|
210
|
+
send(sessionOrArgs, text, opts = {}) {
|
|
211
|
+
const args = typeof sessionOrArgs === 'object' ? sessionOrArgs : { sessionId: sessionOrArgs, text, options: opts };
|
|
212
|
+
return request(`/sessions/${encodeURIComponent(args.sessionId)}/input`, {
|
|
213
|
+
method: 'POST', body: JSON.stringify({ text: args.text || '', attachments: args.attachments || [], options: args.options || {} }),
|
|
214
|
+
});
|
|
215
|
+
},
|
|
216
|
+
resolvePermission(sessionOrArgs, requestId, allow, message) {
|
|
217
|
+
const args = typeof sessionOrArgs === 'object' ? sessionOrArgs : { sessionId: sessionOrArgs, requestId, allow, message };
|
|
218
|
+
return request(`/sessions/${encodeURIComponent(args.sessionId)}/permissions/${encodeURIComponent(args.requestId)}/resolve`, {
|
|
219
|
+
method: 'POST', body: JSON.stringify({ allow: args.allow === true, message: args.message || '' }),
|
|
220
|
+
});
|
|
221
|
+
},
|
|
222
|
+
abort(sessionOrArgs) {
|
|
223
|
+
const id = typeof sessionOrArgs === 'object' ? sessionOrArgs.sessionId : sessionOrArgs;
|
|
224
|
+
return request(`/sessions/${encodeURIComponent(id)}/abort`, { method: 'POST', body: '{}' });
|
|
225
|
+
},
|
|
226
|
+
setPermissionMode(sessionOrArgs, mode) {
|
|
227
|
+
const args = typeof sessionOrArgs === 'object' ? sessionOrArgs : { sessionId: sessionOrArgs, mode };
|
|
228
|
+
return request(`/sessions/${encodeURIComponent(args.sessionId)}/permission-mode`, {
|
|
229
|
+
method: 'PUT', body: JSON.stringify({ mode: args.mode }),
|
|
230
|
+
});
|
|
231
|
+
},
|
|
232
|
+
updateSessionSelection(sessionOrArgs, selection = {}) {
|
|
233
|
+
const args = typeof sessionOrArgs === 'object' ? sessionOrArgs : { sessionId: sessionOrArgs, ...selection };
|
|
234
|
+
return request(`/sessions/${encodeURIComponent(args.sessionId)}/selection`, {
|
|
235
|
+
method: 'PUT',
|
|
236
|
+
body: JSON.stringify({ aiProfileId: args.aiProfileId !== undefined ? args.aiProfileId : selection.aiProfileId, model: args.model !== undefined ? args.model : selection.model }),
|
|
237
|
+
});
|
|
238
|
+
},
|
|
239
|
+
archiveSession(sessionOrArgs) {
|
|
240
|
+
const id = typeof sessionOrArgs === 'object' ? sessionOrArgs.sessionId : sessionOrArgs;
|
|
241
|
+
return request(`/sessions/${encodeURIComponent(id)}/archive`, { method: 'PUT', body: '{}' });
|
|
242
|
+
},
|
|
243
|
+
restoreSession(sessionOrArgs) {
|
|
244
|
+
const id = typeof sessionOrArgs === 'object' ? sessionOrArgs.sessionId : sessionOrArgs;
|
|
245
|
+
return request(`/sessions/${encodeURIComponent(id)}/archive`, { method: 'DELETE' });
|
|
246
|
+
},
|
|
247
|
+
resourceCounts() { return { streams: streams.size, requests: controllers.size, reconnects }; },
|
|
248
|
+
destroy() {
|
|
249
|
+
destroyed = true;
|
|
250
|
+
for (const stream of streams) try { stream.close(); } catch {}
|
|
251
|
+
streams.clear();
|
|
252
|
+
for (const controller of controllers) controller.abort();
|
|
253
|
+
controllers.clear();
|
|
254
|
+
},
|
|
255
|
+
};
|
|
256
|
+
return client;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
return { createClaudeWorkbenchClient, EVENT_TYPES };
|
|
260
|
+
});
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@claude-ai-workbench/client",
|
|
3
|
-
"version": "0.13.
|
|
3
|
+
"version": "0.13.3",
|
|
4
4
|
"type": "commonjs",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"browser": "index.js",
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
"README.md"
|
|
21
21
|
],
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@claude-ai-workbench/contracts": "0.13.
|
|
23
|
+
"@claude-ai-workbench/contracts": "0.13.3"
|
|
24
24
|
},
|
|
25
25
|
"engines": {
|
|
26
26
|
"node": ">=18"
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
# @claude-ai-workbench/contracts
|
|
2
|
-
|
|
3
|
-
Claude AI Workbench 的公共数据契约和 TypeScript 类型,等价于传统 SDK 的公共头文件。
|
|
4
|
-
|
|
5
|
-
```ts
|
|
6
|
-
import type { AiProfile, AiProfileInput, ProfileTestResult } from "@claude-ai-workbench/contracts";
|
|
7
|
-
```
|
|
8
|
-
|
|
9
|
-
本包不包含运行时服务,也不接触 API Key。
|
|
1
|
+
# @claude-ai-workbench/contracts
|
|
2
|
+
|
|
3
|
+
Claude AI Workbench 的公共数据契约和 TypeScript 类型,等价于传统 SDK 的公共头文件。
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
import type { AiProfile, AiProfileInput, ProfileTestResult } from "@claude-ai-workbench/contracts";
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
本包不包含运行时服务,也不接触 API Key。
|