@wwwfz/dsh-agent-bridge 0.8.2 → 0.8.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/README.md +194 -184
- package/cordis.patch.yml +4 -4
- package/package.json +40 -40
- package/src/assets.js +348 -348
- package/src/catalog.js +605 -605
- package/src/client.js +310 -310
- package/src/config.js +88 -88
- package/src/export.js +12 -12
- package/src/index.js +136 -136
- package/src/project-archive.js +145 -145
- package/src/projects.js +980 -980
- package/src/session-import.js +93 -93
- package/src/watch.js +309 -309
- package/web/Geist-OFL.txt +93 -93
- package/web/agent-icons/NOTICE.md +19 -19
- package/web/agent-icons/antigravity.svg +76 -76
- package/web/agent-icons/claude.svg +3 -3
- package/web/agent-icons/dsh.svg +58 -58
- package/web/agent-icons/licenses/copilot.txt +21 -21
- package/web/agent-icons/licenses/dsh.txt +21 -21
- package/web/agent-icons/licenses/gemini.txt +201 -201
- package/web/agent-icons/licenses/opencode.txt +21 -21
- package/web/agent-icons/licenses/qoder-notice.txt +18 -18
- package/web/agent-icons/licenses/qoder.txt +22 -22
- package/web/agent-icons/licenses/qwen.txt +202 -202
- package/web/agent-icons/qoder.svg +4 -4
- package/web/agent-icons/qwen.svg +5 -5
- package/web/agent-icons/workbuddy.svg +29 -29
- package/web/panel.css +502 -502
- package/web/panel.html +246 -246
- package/web/panel.js +1559 -1559
- package/web/project-context.js +25 -25
package/src/client.js
CHANGED
|
@@ -1,310 +1,310 @@
|
|
|
1
|
-
window.__ModuleLoader__.load({
|
|
2
|
-
id: 'dsh-agent-bridge',
|
|
3
|
-
factory: (require) => {
|
|
4
|
-
const { createElement: h, useEffect, useRef } = require('react');
|
|
5
|
-
const panelId = 'dsh-agent-bridge';
|
|
6
|
-
const messages = {
|
|
7
|
-
zh: { title: 'Agent 工作接续' },
|
|
8
|
-
en: { title: 'Agent Workspace Handoff' },
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
function validPath(value) {
|
|
12
|
-
return typeof value === 'string' && value.length > 0 && value.length <= 4096
|
|
13
|
-
&& !/[\u0000-\u001f]/u.test(value);
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
function pathKey(path) {
|
|
17
|
-
if (/^[a-z]:[\\/]|^\\\\|^\/\//i.test(path)) {
|
|
18
|
-
return path.replace(/\//g, '\\').replace(/^\\\\\?\\UNC\\/i, '\\\\')
|
|
19
|
-
.replace(/^\\\\\?\\/, '').replace(/\\+$/, '').toLowerCase();
|
|
20
|
-
}
|
|
21
|
-
return path.replace(/\/+$/, '');
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
function sessionRow(summary) {
|
|
25
|
-
if (!summary || summary.blank || summary.origin === 'subagent' || !validPath(summary.cwd)) return;
|
|
26
|
-
return {
|
|
27
|
-
id: summary.id,
|
|
28
|
-
title: (summary.title || summary.displayTitle || '未命名会话').slice(0, 240),
|
|
29
|
-
path: summary.cwd,
|
|
30
|
-
updatedAt: summary.updatedAt,
|
|
31
|
-
};
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
function dialogueFrom(event) {
|
|
35
|
-
if (event?.surfaceOp !== 'append') return;
|
|
36
|
-
let content;
|
|
37
|
-
let role;
|
|
38
|
-
if (event.type === 'user/message' && event.data?.source?.kind === 'user') {
|
|
39
|
-
role = 'user';
|
|
40
|
-
content = event.data.content;
|
|
41
|
-
} else if (event.type === 'assistant/message') {
|
|
42
|
-
role = 'assistant';
|
|
43
|
-
content = event.data?.message?.content;
|
|
44
|
-
}
|
|
45
|
-
if (!Array.isArray(content)) return;
|
|
46
|
-
const text = content.filter(block => block?.type === 'text' && typeof block.text === 'string')
|
|
47
|
-
.map(block => block.text).join('\n\n');
|
|
48
|
-
return text.trim() ? { role, text } : undefined;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
async function sessionDialogue(remote, sessionId, signal) {
|
|
52
|
-
const collected = [];
|
|
53
|
-
const address = { kind: 'session', sessionId };
|
|
54
|
-
let remaining = 28_000;
|
|
55
|
-
let throughSeq;
|
|
56
|
-
let beforeSeq;
|
|
57
|
-
let hasMore = true;
|
|
58
|
-
let truncated = false;
|
|
59
|
-
for (let page = 0; page < 8 && hasMore && !truncated; page++) {
|
|
60
|
-
const result = await remote.events({ address, maxMessages: 40,
|
|
61
|
-
...(throughSeq === undefined ? {} : { throughSeq, beforeSeq }),
|
|
62
|
-
}, signal);
|
|
63
|
-
if (!result.ok) throw new Error('无法读取这条 DSH 会话的历史。');
|
|
64
|
-
const { records, cursor } = result.value;
|
|
65
|
-
hasMore = result.value.hasMore;
|
|
66
|
-
throughSeq ??= cursor;
|
|
67
|
-
for (let i = records.length - 1; i >= 0; i--) {
|
|
68
|
-
const message = dialogueFrom(records[i].event);
|
|
69
|
-
if (!message) continue;
|
|
70
|
-
if (collected.length >= 24 || message.text.length > remaining) {
|
|
71
|
-
if (!collected.length) collected.push({ ...message,
|
|
72
|
-
text: message.text.slice(0, remaining).replace(/[\uD800-\uDBFF]$/, ''),
|
|
73
|
-
});
|
|
74
|
-
truncated = true;
|
|
75
|
-
break;
|
|
76
|
-
}
|
|
77
|
-
collected.push(message);
|
|
78
|
-
remaining -= message.text.length;
|
|
79
|
-
}
|
|
80
|
-
const firstSeq = records[0]?.event?.seq;
|
|
81
|
-
if (hasMore && (!Number.isSafeInteger(firstSeq) || firstSeq <= 0 || firstSeq >= (beforeSeq ?? Infinity))) break;
|
|
82
|
-
beforeSeq = firstSeq;
|
|
83
|
-
}
|
|
84
|
-
return { messages: collected.reverse(), truncated: truncated || hasMore };
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
function PanelIcon({ size = 16 }) {
|
|
88
|
-
return h('svg', {
|
|
89
|
-
width: size, height: size, viewBox: '0 0 24 24', fill: 'none',
|
|
90
|
-
stroke: 'currentColor', strokeWidth: 1.7, strokeLinecap: 'round', strokeLinejoin: 'round',
|
|
91
|
-
'aria-hidden': true,
|
|
92
|
-
}, h('path', { d: 'M3 17.5V6.5A1.5 1.5 0 0 1 4.5 5H9l2 2h8.5A1.5 1.5 0 0 1 21 8.5v9a1.5 1.5 0 0 1-1.5 1.5h-15A1.5 1.5 0 0 1 3 17.5Z M8 13h8m-3-3 3 3-3 3' }));
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
function apply(ctx) {
|
|
96
|
-
ctx.effect(() => ctx.locale.register(panelId, messages), 'agent bridge locale');
|
|
97
|
-
const t = ctx.locale.bind(panelId);
|
|
98
|
-
const recentImports = new Map();
|
|
99
|
-
const incompleteDrafts = new Map();
|
|
100
|
-
let importing = false;
|
|
101
|
-
|
|
102
|
-
async function exportRequest(request, signal) {
|
|
103
|
-
if (request.path !== undefined && !validPath(request.path)) throw new Error('工作目录格式无效。');
|
|
104
|
-
const scope = request.path === undefined ? undefined : pathKey(request.path);
|
|
105
|
-
const snapshot = ctx.sessions.list.getSnapshot();
|
|
106
|
-
if (snapshot.phase !== 'ready') throw new Error('DSH 会话列表正在加载,请稍后重试。');
|
|
107
|
-
const rows = snapshot.ids.map(id => sessionRow(snapshot.byId[id]))
|
|
108
|
-
.filter(row => row && (scope === undefined || pathKey(row.path) === scope));
|
|
109
|
-
if (request.type === 'dsh-agent-bridge:sessions') return { sessions: rows };
|
|
110
|
-
|
|
111
|
-
const selected = rows.find(row => row.id === request.sessionId);
|
|
112
|
-
if (!selected) throw new Error('未在这个工作区找到可导出的 DSH 会话。');
|
|
113
|
-
const dialogue = await sessionDialogue(ctx.remote.session, selected.id, signal);
|
|
114
|
-
if (!dialogue.messages.length) throw new Error('这条 DSH 会话暂无可导出的文字。');
|
|
115
|
-
return { conversation: { title: selected.title, path: selected.path, ...dialogue } };
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
function rememberImport(requestId, imported) {
|
|
119
|
-
if (!recentImports.has(requestId) && recentImports.size >= 32) {
|
|
120
|
-
recentImports.delete(recentImports.keys().next().value);
|
|
121
|
-
}
|
|
122
|
-
recentImports.set(requestId, imported);
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
async function rollbackDraft(imported, binding) {
|
|
126
|
-
try {
|
|
127
|
-
const session = binding?.session;
|
|
128
|
-
const state = session?.getSnapshot();
|
|
129
|
-
const input = binding && ctx.conversation.input.for(binding.ctx).snapshot;
|
|
130
|
-
// A preserved draft, attachments, or a chat already in use belong to
|
|
131
|
-
// the user. Archive only an unused, empty chat created by this handoff.
|
|
132
|
-
if (state && (!state.blank || state.running || state.promptAttempted || state.removed)) return false;
|
|
133
|
-
if (binding && (!input || input.draft !== '' || input.attachmentIds?.length
|
|
134
|
-
|| ['adjudicating', 'submitting', 'sending'].includes(input.phase))) return false;
|
|
135
|
-
await ctx.workspaces.archiveSession(imported.sessionId);
|
|
136
|
-
incompleteDrafts.delete(imported.path);
|
|
137
|
-
for (const [key, item] of recentImports) if (item === imported) recentImports.delete(key);
|
|
138
|
-
return true;
|
|
139
|
-
} catch {
|
|
140
|
-
return false;
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
async function prepareSession(request, reveal) {
|
|
145
|
-
const path = pathKey(request.path);
|
|
146
|
-
let imported = recentImports.get(request.requestId) ?? incompleteDrafts.get(path);
|
|
147
|
-
if (imported && imported.path !== path) throw new Error('交接工作目录已变化,请重新预览。');
|
|
148
|
-
if (!imported) {
|
|
149
|
-
const workspace = await ctx.workspaces.create({ path: request.path });
|
|
150
|
-
const sessionId = await ctx.sessions.create({ workspaceId: workspace.workspaceId });
|
|
151
|
-
imported = { sessionId, path, ready: false };
|
|
152
|
-
incompleteDrafts.set(path, imported);
|
|
153
|
-
}
|
|
154
|
-
rememberImport(request.requestId, imported);
|
|
155
|
-
|
|
156
|
-
let reference;
|
|
157
|
-
try {
|
|
158
|
-
if (!imported.ready) {
|
|
159
|
-
let binding;
|
|
160
|
-
let outcome;
|
|
161
|
-
try {
|
|
162
|
-
reference = ctx.sessions.retain(imported.sessionId, { source: panelId });
|
|
163
|
-
binding = reference.binding;
|
|
164
|
-
await reference.ready;
|
|
165
|
-
outcome = ctx.conversation.input.requestDraftInitialization(binding, {
|
|
166
|
-
prompt: request.prompt,
|
|
167
|
-
whenDraftExists: 'keep',
|
|
168
|
-
});
|
|
169
|
-
} catch {
|
|
170
|
-
// A Host error can also leave an empty session to roll back.
|
|
171
|
-
}
|
|
172
|
-
if (outcome !== 'applied') {
|
|
173
|
-
if (outcome !== 'preserved' && await rollbackDraft(imported, binding)) {
|
|
174
|
-
throw new Error('DSH 未能填入交接草稿。空白会话已归档,可重试。');
|
|
175
|
-
}
|
|
176
|
-
throw new Error(outcome === 'preserved'
|
|
177
|
-
? '会话中已有草稿,已原样保留。可打开会话并手动补入交接资料。'
|
|
178
|
-
: 'DSH 未能填入交接草稿。可重试同一会话,或打开后手动补入交接资料。');
|
|
179
|
-
}
|
|
180
|
-
imported.ready = true;
|
|
181
|
-
incompleteDrafts.delete(path);
|
|
182
|
-
}
|
|
183
|
-
reveal(imported.sessionId);
|
|
184
|
-
return imported.sessionId;
|
|
185
|
-
} finally {
|
|
186
|
-
reference?.release();
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
function Panel() {
|
|
191
|
-
const iframe = useRef(null);
|
|
192
|
-
const url = new URL('agent-bridge/panel', document.baseURI).href;
|
|
193
|
-
|
|
194
|
-
useEffect(() => {
|
|
195
|
-
const lifetime = new AbortController();
|
|
196
|
-
async function onMessage(event) {
|
|
197
|
-
const frame = iframe.current;
|
|
198
|
-
if (!frame || event.source !== frame.contentWindow || event.origin !== window.origin) return;
|
|
199
|
-
try {
|
|
200
|
-
if (event.source.location.href !== url) return;
|
|
201
|
-
} catch {
|
|
202
|
-
return;
|
|
203
|
-
}
|
|
204
|
-
const request = event.data;
|
|
205
|
-
if (typeof request?.requestId !== 'string' || !/^[a-zA-Z0-9_-]{1,128}$/.test(request.requestId)) return;
|
|
206
|
-
const reply = (result) => {
|
|
207
|
-
if (iframe.current !== frame) return;
|
|
208
|
-
event.source.postMessage({ type: 'dsh-agent-bridge:result', requestId: request.requestId, ...result },
|
|
209
|
-
event.origin === 'null' ? '*' : event.origin);
|
|
210
|
-
};
|
|
211
|
-
|
|
212
|
-
if (request.type === 'dsh-agent-bridge:sessions' || request.type === 'dsh-agent-bridge:session-export') {
|
|
213
|
-
try {
|
|
214
|
-
const signal = AbortSignal.any([lifetime.signal, AbortSignal.timeout(20_000)]);
|
|
215
|
-
reply({ ok: true, ...await exportRequest(request, signal) });
|
|
216
|
-
} catch (error) {
|
|
217
|
-
reply({ ok: false, error: error instanceof Error ? error.message.slice(0, 500) : '无法导出 DSH 会话。' });
|
|
218
|
-
}
|
|
219
|
-
return;
|
|
220
|
-
}
|
|
221
|
-
if (request.type === 'dsh-agent-bridge:open-imported') {
|
|
222
|
-
try {
|
|
223
|
-
await ctx.sessions.refresh();
|
|
224
|
-
const snapshot = ctx.sessions.list.getSnapshot();
|
|
225
|
-
if (!Object.hasOwn(snapshot.byId, request.sessionId)) throw new Error('会话已保存,请从工作区列表打开。');
|
|
226
|
-
if (iframe.current === frame) ctx.uiWorkspace.openSession(request.sessionId);
|
|
227
|
-
reply({ ok: true });
|
|
228
|
-
} catch (error) {
|
|
229
|
-
reply({ ok: false, error: error instanceof Error ? error.message.slice(0, 500) : '会话已保存,请从工作区列表打开。' });
|
|
230
|
-
}
|
|
231
|
-
return;
|
|
232
|
-
}
|
|
233
|
-
if (request.type === 'dsh-agent-bridge:recover-draft') {
|
|
234
|
-
const partial = recentImports.get(request.requestId);
|
|
235
|
-
if (importing || !partial || partial.ready || partial.sessionId !== request.sessionId) {
|
|
236
|
-
reply({ ok: false, error: '无法定位这条交接会话,请回到工作区检查。' });
|
|
237
|
-
return;
|
|
238
|
-
}
|
|
239
|
-
try {
|
|
240
|
-
ctx.uiWorkspace.openSession(partial.sessionId);
|
|
241
|
-
reply({ ok: true });
|
|
242
|
-
} catch {
|
|
243
|
-
reply({ ok: false, error: '暂时无法打开这条会话,请在工作区列表中重试。' });
|
|
244
|
-
}
|
|
245
|
-
return;
|
|
246
|
-
}
|
|
247
|
-
if (request.type !== 'dsh-agent-bridge:open') return;
|
|
248
|
-
if (!validPath(request.path) || typeof request.prompt !== 'string'
|
|
249
|
-
|| !request.prompt.trim() || request.prompt.length > 65_536) {
|
|
250
|
-
reply({ ok: false, error: '项目目录或交接草稿格式无效。' });
|
|
251
|
-
return;
|
|
252
|
-
}
|
|
253
|
-
if (importing) {
|
|
254
|
-
reply({ ok: false, error: '另一个项目正在导入。' });
|
|
255
|
-
return;
|
|
256
|
-
}
|
|
257
|
-
importing = true;
|
|
258
|
-
try {
|
|
259
|
-
const sessionId = await prepareSession(request, (id) => {
|
|
260
|
-
if (iframe.current !== frame) return;
|
|
261
|
-
ctx.uiWorkspace.openSession(id);
|
|
262
|
-
if (typeof request.bridgeNonce === 'string' && /^[a-f0-9]{64}$/.test(request.bridgeNonce)) {
|
|
263
|
-
// The native workspace may replace the panel before it receives the reply.
|
|
264
|
-
void fetch(new URL('api', url), {
|
|
265
|
-
method: 'POST', keepalive: true,
|
|
266
|
-
headers: { 'Content-Type': 'application/json', 'X-DSH-Agent-Bridge': request.bridgeNonce },
|
|
267
|
-
body: JSON.stringify({ action: 'record-project-continuation', ticket: request.requestId }),
|
|
268
|
-
}).catch(() => {});
|
|
269
|
-
}
|
|
270
|
-
});
|
|
271
|
-
reply({ ok: true, sessionId });
|
|
272
|
-
} catch (error) {
|
|
273
|
-
const partial = recentImports.get(request.requestId);
|
|
274
|
-
reply({
|
|
275
|
-
ok: false,
|
|
276
|
-
recoverySessionId: partial?.ready === false ? partial.sessionId : undefined,
|
|
277
|
-
error: error instanceof Error ? error.message.slice(0, 500) : 'DSH 无法打开交接会话。',
|
|
278
|
-
});
|
|
279
|
-
} finally {
|
|
280
|
-
importing = false;
|
|
281
|
-
}
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
window.addEventListener('message', onMessage);
|
|
285
|
-
return () => {
|
|
286
|
-
lifetime.abort();
|
|
287
|
-
window.removeEventListener('message', onMessage);
|
|
288
|
-
};
|
|
289
|
-
}, [url]);
|
|
290
|
-
|
|
291
|
-
return h('iframe', {
|
|
292
|
-
ref: iframe,
|
|
293
|
-
src: url,
|
|
294
|
-
title: t('title'),
|
|
295
|
-
referrerPolicy: 'no-referrer',
|
|
296
|
-
style: { display: 'block', width: '100%', height: '100%', border: 0 },
|
|
297
|
-
});
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
ctx.slots.inject('main', () => ctx.slots.register({
|
|
301
|
-
name: 'main', key: panelId, locale: panelId,
|
|
302
|
-
}, Panel));
|
|
303
|
-
ctx.slots.inject('sidebar.panellist', () => ctx.slots.register({
|
|
304
|
-
name: 'sidebar.panellist', id: panelId, order: 20, label: () => t('title'), locale: panelId,
|
|
305
|
-
}, PanelIcon));
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
return { apply, inject: ['slots', 'locale', 'uiWorkspace', 'workspaces', 'sessions', 'conversation', 'remote', 'remote.session'] };
|
|
309
|
-
},
|
|
310
|
-
});
|
|
1
|
+
window.__ModuleLoader__.load({
|
|
2
|
+
id: '@wwwfz/dsh-agent-bridge',
|
|
3
|
+
factory: (require) => {
|
|
4
|
+
const { createElement: h, useEffect, useRef } = require('react');
|
|
5
|
+
const panelId = 'dsh-agent-bridge';
|
|
6
|
+
const messages = {
|
|
7
|
+
zh: { title: 'Agent 工作接续' },
|
|
8
|
+
en: { title: 'Agent Workspace Handoff' },
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
function validPath(value) {
|
|
12
|
+
return typeof value === 'string' && value.length > 0 && value.length <= 4096
|
|
13
|
+
&& !/[\u0000-\u001f]/u.test(value);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function pathKey(path) {
|
|
17
|
+
if (/^[a-z]:[\\/]|^\\\\|^\/\//i.test(path)) {
|
|
18
|
+
return path.replace(/\//g, '\\').replace(/^\\\\\?\\UNC\\/i, '\\\\')
|
|
19
|
+
.replace(/^\\\\\?\\/, '').replace(/\\+$/, '').toLowerCase();
|
|
20
|
+
}
|
|
21
|
+
return path.replace(/\/+$/, '');
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function sessionRow(summary) {
|
|
25
|
+
if (!summary || summary.blank || summary.origin === 'subagent' || !validPath(summary.cwd)) return;
|
|
26
|
+
return {
|
|
27
|
+
id: summary.id,
|
|
28
|
+
title: (summary.title || summary.displayTitle || '未命名会话').slice(0, 240),
|
|
29
|
+
path: summary.cwd,
|
|
30
|
+
updatedAt: summary.updatedAt,
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function dialogueFrom(event) {
|
|
35
|
+
if (event?.surfaceOp !== 'append') return;
|
|
36
|
+
let content;
|
|
37
|
+
let role;
|
|
38
|
+
if (event.type === 'user/message' && event.data?.source?.kind === 'user') {
|
|
39
|
+
role = 'user';
|
|
40
|
+
content = event.data.content;
|
|
41
|
+
} else if (event.type === 'assistant/message') {
|
|
42
|
+
role = 'assistant';
|
|
43
|
+
content = event.data?.message?.content;
|
|
44
|
+
}
|
|
45
|
+
if (!Array.isArray(content)) return;
|
|
46
|
+
const text = content.filter(block => block?.type === 'text' && typeof block.text === 'string')
|
|
47
|
+
.map(block => block.text).join('\n\n');
|
|
48
|
+
return text.trim() ? { role, text } : undefined;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async function sessionDialogue(remote, sessionId, signal) {
|
|
52
|
+
const collected = [];
|
|
53
|
+
const address = { kind: 'session', sessionId };
|
|
54
|
+
let remaining = 28_000;
|
|
55
|
+
let throughSeq;
|
|
56
|
+
let beforeSeq;
|
|
57
|
+
let hasMore = true;
|
|
58
|
+
let truncated = false;
|
|
59
|
+
for (let page = 0; page < 8 && hasMore && !truncated; page++) {
|
|
60
|
+
const result = await remote.events({ address, maxMessages: 40,
|
|
61
|
+
...(throughSeq === undefined ? {} : { throughSeq, beforeSeq }),
|
|
62
|
+
}, signal);
|
|
63
|
+
if (!result.ok) throw new Error('无法读取这条 DSH 会话的历史。');
|
|
64
|
+
const { records, cursor } = result.value;
|
|
65
|
+
hasMore = result.value.hasMore;
|
|
66
|
+
throughSeq ??= cursor;
|
|
67
|
+
for (let i = records.length - 1; i >= 0; i--) {
|
|
68
|
+
const message = dialogueFrom(records[i].event);
|
|
69
|
+
if (!message) continue;
|
|
70
|
+
if (collected.length >= 24 || message.text.length > remaining) {
|
|
71
|
+
if (!collected.length) collected.push({ ...message,
|
|
72
|
+
text: message.text.slice(0, remaining).replace(/[\uD800-\uDBFF]$/, ''),
|
|
73
|
+
});
|
|
74
|
+
truncated = true;
|
|
75
|
+
break;
|
|
76
|
+
}
|
|
77
|
+
collected.push(message);
|
|
78
|
+
remaining -= message.text.length;
|
|
79
|
+
}
|
|
80
|
+
const firstSeq = records[0]?.event?.seq;
|
|
81
|
+
if (hasMore && (!Number.isSafeInteger(firstSeq) || firstSeq <= 0 || firstSeq >= (beforeSeq ?? Infinity))) break;
|
|
82
|
+
beforeSeq = firstSeq;
|
|
83
|
+
}
|
|
84
|
+
return { messages: collected.reverse(), truncated: truncated || hasMore };
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function PanelIcon({ size = 16 }) {
|
|
88
|
+
return h('svg', {
|
|
89
|
+
width: size, height: size, viewBox: '0 0 24 24', fill: 'none',
|
|
90
|
+
stroke: 'currentColor', strokeWidth: 1.7, strokeLinecap: 'round', strokeLinejoin: 'round',
|
|
91
|
+
'aria-hidden': true,
|
|
92
|
+
}, h('path', { d: 'M3 17.5V6.5A1.5 1.5 0 0 1 4.5 5H9l2 2h8.5A1.5 1.5 0 0 1 21 8.5v9a1.5 1.5 0 0 1-1.5 1.5h-15A1.5 1.5 0 0 1 3 17.5Z M8 13h8m-3-3 3 3-3 3' }));
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function apply(ctx) {
|
|
96
|
+
ctx.effect(() => ctx.locale.register(panelId, messages), 'agent bridge locale');
|
|
97
|
+
const t = ctx.locale.bind(panelId);
|
|
98
|
+
const recentImports = new Map();
|
|
99
|
+
const incompleteDrafts = new Map();
|
|
100
|
+
let importing = false;
|
|
101
|
+
|
|
102
|
+
async function exportRequest(request, signal) {
|
|
103
|
+
if (request.path !== undefined && !validPath(request.path)) throw new Error('工作目录格式无效。');
|
|
104
|
+
const scope = request.path === undefined ? undefined : pathKey(request.path);
|
|
105
|
+
const snapshot = ctx.sessions.list.getSnapshot();
|
|
106
|
+
if (snapshot.phase !== 'ready') throw new Error('DSH 会话列表正在加载,请稍后重试。');
|
|
107
|
+
const rows = snapshot.ids.map(id => sessionRow(snapshot.byId[id]))
|
|
108
|
+
.filter(row => row && (scope === undefined || pathKey(row.path) === scope));
|
|
109
|
+
if (request.type === 'dsh-agent-bridge:sessions') return { sessions: rows };
|
|
110
|
+
|
|
111
|
+
const selected = rows.find(row => row.id === request.sessionId);
|
|
112
|
+
if (!selected) throw new Error('未在这个工作区找到可导出的 DSH 会话。');
|
|
113
|
+
const dialogue = await sessionDialogue(ctx.remote.session, selected.id, signal);
|
|
114
|
+
if (!dialogue.messages.length) throw new Error('这条 DSH 会话暂无可导出的文字。');
|
|
115
|
+
return { conversation: { title: selected.title, path: selected.path, ...dialogue } };
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function rememberImport(requestId, imported) {
|
|
119
|
+
if (!recentImports.has(requestId) && recentImports.size >= 32) {
|
|
120
|
+
recentImports.delete(recentImports.keys().next().value);
|
|
121
|
+
}
|
|
122
|
+
recentImports.set(requestId, imported);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
async function rollbackDraft(imported, binding) {
|
|
126
|
+
try {
|
|
127
|
+
const session = binding?.session;
|
|
128
|
+
const state = session?.getSnapshot();
|
|
129
|
+
const input = binding && ctx.conversation.input.for(binding.ctx).snapshot;
|
|
130
|
+
// A preserved draft, attachments, or a chat already in use belong to
|
|
131
|
+
// the user. Archive only an unused, empty chat created by this handoff.
|
|
132
|
+
if (state && (!state.blank || state.running || state.promptAttempted || state.removed)) return false;
|
|
133
|
+
if (binding && (!input || input.draft !== '' || input.attachmentIds?.length
|
|
134
|
+
|| ['adjudicating', 'submitting', 'sending'].includes(input.phase))) return false;
|
|
135
|
+
await ctx.workspaces.archiveSession(imported.sessionId);
|
|
136
|
+
incompleteDrafts.delete(imported.path);
|
|
137
|
+
for (const [key, item] of recentImports) if (item === imported) recentImports.delete(key);
|
|
138
|
+
return true;
|
|
139
|
+
} catch {
|
|
140
|
+
return false;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
async function prepareSession(request, reveal) {
|
|
145
|
+
const path = pathKey(request.path);
|
|
146
|
+
let imported = recentImports.get(request.requestId) ?? incompleteDrafts.get(path);
|
|
147
|
+
if (imported && imported.path !== path) throw new Error('交接工作目录已变化,请重新预览。');
|
|
148
|
+
if (!imported) {
|
|
149
|
+
const workspace = await ctx.workspaces.create({ path: request.path });
|
|
150
|
+
const sessionId = await ctx.sessions.create({ workspaceId: workspace.workspaceId });
|
|
151
|
+
imported = { sessionId, path, ready: false };
|
|
152
|
+
incompleteDrafts.set(path, imported);
|
|
153
|
+
}
|
|
154
|
+
rememberImport(request.requestId, imported);
|
|
155
|
+
|
|
156
|
+
let reference;
|
|
157
|
+
try {
|
|
158
|
+
if (!imported.ready) {
|
|
159
|
+
let binding;
|
|
160
|
+
let outcome;
|
|
161
|
+
try {
|
|
162
|
+
reference = ctx.sessions.retain(imported.sessionId, { source: panelId });
|
|
163
|
+
binding = reference.binding;
|
|
164
|
+
await reference.ready;
|
|
165
|
+
outcome = ctx.conversation.input.requestDraftInitialization(binding, {
|
|
166
|
+
prompt: request.prompt,
|
|
167
|
+
whenDraftExists: 'keep',
|
|
168
|
+
});
|
|
169
|
+
} catch {
|
|
170
|
+
// A Host error can also leave an empty session to roll back.
|
|
171
|
+
}
|
|
172
|
+
if (outcome !== 'applied') {
|
|
173
|
+
if (outcome !== 'preserved' && await rollbackDraft(imported, binding)) {
|
|
174
|
+
throw new Error('DSH 未能填入交接草稿。空白会话已归档,可重试。');
|
|
175
|
+
}
|
|
176
|
+
throw new Error(outcome === 'preserved'
|
|
177
|
+
? '会话中已有草稿,已原样保留。可打开会话并手动补入交接资料。'
|
|
178
|
+
: 'DSH 未能填入交接草稿。可重试同一会话,或打开后手动补入交接资料。');
|
|
179
|
+
}
|
|
180
|
+
imported.ready = true;
|
|
181
|
+
incompleteDrafts.delete(path);
|
|
182
|
+
}
|
|
183
|
+
reveal(imported.sessionId);
|
|
184
|
+
return imported.sessionId;
|
|
185
|
+
} finally {
|
|
186
|
+
reference?.release();
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function Panel() {
|
|
191
|
+
const iframe = useRef(null);
|
|
192
|
+
const url = new URL('agent-bridge/panel', document.baseURI).href;
|
|
193
|
+
|
|
194
|
+
useEffect(() => {
|
|
195
|
+
const lifetime = new AbortController();
|
|
196
|
+
async function onMessage(event) {
|
|
197
|
+
const frame = iframe.current;
|
|
198
|
+
if (!frame || event.source !== frame.contentWindow || event.origin !== window.origin) return;
|
|
199
|
+
try {
|
|
200
|
+
if (event.source.location.href !== url) return;
|
|
201
|
+
} catch {
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
const request = event.data;
|
|
205
|
+
if (typeof request?.requestId !== 'string' || !/^[a-zA-Z0-9_-]{1,128}$/.test(request.requestId)) return;
|
|
206
|
+
const reply = (result) => {
|
|
207
|
+
if (iframe.current !== frame) return;
|
|
208
|
+
event.source.postMessage({ type: 'dsh-agent-bridge:result', requestId: request.requestId, ...result },
|
|
209
|
+
event.origin === 'null' ? '*' : event.origin);
|
|
210
|
+
};
|
|
211
|
+
|
|
212
|
+
if (request.type === 'dsh-agent-bridge:sessions' || request.type === 'dsh-agent-bridge:session-export') {
|
|
213
|
+
try {
|
|
214
|
+
const signal = AbortSignal.any([lifetime.signal, AbortSignal.timeout(20_000)]);
|
|
215
|
+
reply({ ok: true, ...await exportRequest(request, signal) });
|
|
216
|
+
} catch (error) {
|
|
217
|
+
reply({ ok: false, error: error instanceof Error ? error.message.slice(0, 500) : '无法导出 DSH 会话。' });
|
|
218
|
+
}
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
if (request.type === 'dsh-agent-bridge:open-imported') {
|
|
222
|
+
try {
|
|
223
|
+
await ctx.sessions.refresh();
|
|
224
|
+
const snapshot = ctx.sessions.list.getSnapshot();
|
|
225
|
+
if (!Object.hasOwn(snapshot.byId, request.sessionId)) throw new Error('会话已保存,请从工作区列表打开。');
|
|
226
|
+
if (iframe.current === frame) ctx.uiWorkspace.openSession(request.sessionId);
|
|
227
|
+
reply({ ok: true });
|
|
228
|
+
} catch (error) {
|
|
229
|
+
reply({ ok: false, error: error instanceof Error ? error.message.slice(0, 500) : '会话已保存,请从工作区列表打开。' });
|
|
230
|
+
}
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
if (request.type === 'dsh-agent-bridge:recover-draft') {
|
|
234
|
+
const partial = recentImports.get(request.requestId);
|
|
235
|
+
if (importing || !partial || partial.ready || partial.sessionId !== request.sessionId) {
|
|
236
|
+
reply({ ok: false, error: '无法定位这条交接会话,请回到工作区检查。' });
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
239
|
+
try {
|
|
240
|
+
ctx.uiWorkspace.openSession(partial.sessionId);
|
|
241
|
+
reply({ ok: true });
|
|
242
|
+
} catch {
|
|
243
|
+
reply({ ok: false, error: '暂时无法打开这条会话,请在工作区列表中重试。' });
|
|
244
|
+
}
|
|
245
|
+
return;
|
|
246
|
+
}
|
|
247
|
+
if (request.type !== 'dsh-agent-bridge:open') return;
|
|
248
|
+
if (!validPath(request.path) || typeof request.prompt !== 'string'
|
|
249
|
+
|| !request.prompt.trim() || request.prompt.length > 65_536) {
|
|
250
|
+
reply({ ok: false, error: '项目目录或交接草稿格式无效。' });
|
|
251
|
+
return;
|
|
252
|
+
}
|
|
253
|
+
if (importing) {
|
|
254
|
+
reply({ ok: false, error: '另一个项目正在导入。' });
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
importing = true;
|
|
258
|
+
try {
|
|
259
|
+
const sessionId = await prepareSession(request, (id) => {
|
|
260
|
+
if (iframe.current !== frame) return;
|
|
261
|
+
ctx.uiWorkspace.openSession(id);
|
|
262
|
+
if (typeof request.bridgeNonce === 'string' && /^[a-f0-9]{64}$/.test(request.bridgeNonce)) {
|
|
263
|
+
// The native workspace may replace the panel before it receives the reply.
|
|
264
|
+
void fetch(new URL('api', url), {
|
|
265
|
+
method: 'POST', keepalive: true,
|
|
266
|
+
headers: { 'Content-Type': 'application/json', 'X-DSH-Agent-Bridge': request.bridgeNonce },
|
|
267
|
+
body: JSON.stringify({ action: 'record-project-continuation', ticket: request.requestId }),
|
|
268
|
+
}).catch(() => {});
|
|
269
|
+
}
|
|
270
|
+
});
|
|
271
|
+
reply({ ok: true, sessionId });
|
|
272
|
+
} catch (error) {
|
|
273
|
+
const partial = recentImports.get(request.requestId);
|
|
274
|
+
reply({
|
|
275
|
+
ok: false,
|
|
276
|
+
recoverySessionId: partial?.ready === false ? partial.sessionId : undefined,
|
|
277
|
+
error: error instanceof Error ? error.message.slice(0, 500) : 'DSH 无法打开交接会话。',
|
|
278
|
+
});
|
|
279
|
+
} finally {
|
|
280
|
+
importing = false;
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
window.addEventListener('message', onMessage);
|
|
285
|
+
return () => {
|
|
286
|
+
lifetime.abort();
|
|
287
|
+
window.removeEventListener('message', onMessage);
|
|
288
|
+
};
|
|
289
|
+
}, [url]);
|
|
290
|
+
|
|
291
|
+
return h('iframe', {
|
|
292
|
+
ref: iframe,
|
|
293
|
+
src: url,
|
|
294
|
+
title: t('title'),
|
|
295
|
+
referrerPolicy: 'no-referrer',
|
|
296
|
+
style: { display: 'block', width: '100%', height: '100%', border: 0 },
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
ctx.slots.inject('main', () => ctx.slots.register({
|
|
301
|
+
name: 'main', key: panelId, locale: panelId,
|
|
302
|
+
}, Panel));
|
|
303
|
+
ctx.slots.inject('sidebar.panellist', () => ctx.slots.register({
|
|
304
|
+
name: 'sidebar.panellist', id: panelId, order: 20, label: () => t('title'), locale: panelId,
|
|
305
|
+
}, PanelIcon));
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
return { apply, inject: ['slots', 'locale', 'uiWorkspace', 'workspaces', 'sessions', 'conversation', 'remote', 'remote.session'] };
|
|
309
|
+
},
|
|
310
|
+
});
|