@xmanrui/dsh-im 0.7.0 → 0.7.2
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/lib/index.js +119 -121
- package/package.json +1 -1
- package/src/channels/dingtalk/dingtalk-bridge.mjs +423 -8
- package/src/channels/dingtalk/harness-client.mjs +16 -366
- package/src/channels/discord/discord-api.mjs +1 -1
- package/src/channels/discord/discord-runtime.mjs +11 -1
- package/src/channels/discord/harness-client.mjs +10 -2
- package/src/channels/feishu/bridge.mjs +571 -50
- package/src/channels/feishu/feishu-runtime.mjs +41 -1
- package/src/channels/feishu/harness-client.mjs +16 -335
- package/src/channels/qq/harness-client.mjs +10 -2
- package/src/channels/qq/qq-bridge.mjs +428 -28
- package/src/channels/qq/qq-runtime.mjs +14 -3
- package/src/channels/shared/harness-approval.mjs +472 -0
- package/src/channels/shared/harness-client.mjs +858 -0
- package/src/channels/shared/harness-question.mjs +85 -0
- package/src/channels/shared/text-harness-bridge.mjs +486 -24
- package/src/channels/slack/harness-client.mjs +10 -2
- package/src/channels/slack/slack-runtime.mjs +11 -1
- package/src/channels/telegram/harness-client.mjs +10 -2
- package/src/channels/telegram/telegram-runtime.mjs +15 -4
- package/src/channels/wecom/harness-client.mjs +10 -2
- package/src/channels/wecom/wecom-bridge.mjs +434 -14
- package/src/channels/wecom/wecom-runtime.mjs +6 -0
- package/src/channels/weixin/harness-client.mjs +16 -326
- package/src/channels/weixin/weixin-api.mjs +1 -1
- package/src/channels/weixin/weixin-bridge.mjs +451 -22
- package/src/channels/weixin/weixin-runtime.mjs +56 -7
- package/src/channels/whatsapp/harness-client.mjs +10 -2
- package/src/channels/whatsapp/whatsapp-runtime.mjs +1 -0
|
@@ -1,329 +1,19 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
if (!Array.isArray(workspaceList?.items)
|
|
18
|
-
|| !Array.isArray(workspaceList?.archivedSessionIds)) {
|
|
19
|
-
throw new Error('Harness returned an invalid response for workspace.list');
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
const workspace = workspaceList.items.find((item) => item?.path === workspacePath);
|
|
23
|
-
if (!workspace) return null;
|
|
24
|
-
if (!Array.isArray(workspace.sessionIds)
|
|
25
|
-
|| workspace.sessionIds.some((sessionId) => typeof sessionId !== 'string')) {
|
|
26
|
-
throw new Error('Harness returned invalid session IDs for workspace.list');
|
|
27
|
-
}
|
|
28
|
-
return workspace;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
function workspaceSessions(workspace, archivedSessionIds, sessionList) {
|
|
32
|
-
if (!Array.isArray(sessionList?.items)) {
|
|
33
|
-
throw new Error('Harness returned an invalid response for session.list');
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
const archived = new Set(archivedSessionIds);
|
|
37
|
-
const summaries = new Map(sessionList.items.flatMap((item) => (
|
|
38
|
-
typeof item?.sessionId === 'string' ? [[item.sessionId, item]] : []
|
|
39
|
-
)));
|
|
40
|
-
return {
|
|
41
|
-
workspace: workspace.path,
|
|
42
|
-
sessions: workspace.sessionIds.map((sessionId) => {
|
|
43
|
-
const summary = summaries.get(sessionId);
|
|
44
|
-
const title = summary?.projections?.values?.title;
|
|
45
|
-
return {
|
|
46
|
-
sessionId,
|
|
47
|
-
title: typeof title === 'string' ? title : null,
|
|
48
|
-
archived: archived.has(sessionId),
|
|
49
|
-
blank: summary?.blank === true,
|
|
50
|
-
origin: summary?.origin === 'subagent' ? 'subagent' : null,
|
|
51
|
-
summaryAvailable: summary !== undefined,
|
|
52
|
-
};
|
|
53
|
-
}),
|
|
54
|
-
};
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
function assistantMessageText(event) {
|
|
58
|
-
return (event?.data?.message?.content ?? [])
|
|
59
|
-
.filter((part) => part.type === 'text' && typeof part.text === 'string')
|
|
60
|
-
.map((part) => part.text)
|
|
61
|
-
.join('\n')
|
|
62
|
-
.trim();
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
export class HarnessReplyTracker {
|
|
66
|
-
#promptRpcId;
|
|
67
|
-
#lastSeq;
|
|
68
|
-
#openTurn = null;
|
|
69
|
-
#targetTurn = null;
|
|
70
|
-
#stepText = new Map();
|
|
71
|
-
#latestText = '';
|
|
72
|
-
#finished = false;
|
|
73
|
-
#reason = null;
|
|
74
|
-
|
|
75
|
-
constructor({ promptRpcId, afterSeq = -1 }) {
|
|
76
|
-
this.#promptRpcId = promptRpcId;
|
|
77
|
-
this.#lastSeq = afterSeq;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
get finished() {
|
|
81
|
-
return this.#finished;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
get answer() {
|
|
85
|
-
return this.#latestText.trim();
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
get reason() {
|
|
89
|
-
return this.#reason;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
consume(entries) {
|
|
93
|
-
let update = null;
|
|
94
|
-
const ordered = [...entries]
|
|
95
|
-
.map((entry) => entry?.event ?? entry)
|
|
96
|
-
.filter(Boolean)
|
|
97
|
-
.sort((left, right) => (left.seq ?? -1) - (right.seq ?? -1));
|
|
98
|
-
|
|
99
|
-
for (const event of ordered) {
|
|
100
|
-
const seq = event.seq ?? -1;
|
|
101
|
-
if (seq <= this.#lastSeq) continue;
|
|
102
|
-
this.#lastSeq = seq;
|
|
103
|
-
|
|
104
|
-
if (event.type === 'turn/start') this.#openTurn = event.data?.turn ?? null;
|
|
105
|
-
|
|
106
|
-
if (event.type === 'user/message' && event.data?.source?.rpcId === this.#promptRpcId) {
|
|
107
|
-
this.#targetTurn = this.#openTurn;
|
|
108
|
-
continue;
|
|
109
|
-
}
|
|
110
|
-
if (this.#targetTurn === null) continue;
|
|
111
|
-
|
|
112
|
-
if (event.type === 'turn/end') {
|
|
113
|
-
if (event.data?.turn !== this.#targetTurn) continue;
|
|
114
|
-
this.#finished = true;
|
|
115
|
-
this.#reason = event.data?.reason ?? null;
|
|
116
|
-
this.#openTurn = null;
|
|
117
|
-
continue;
|
|
118
|
-
}
|
|
119
|
-
if (event.data?.turn !== this.#targetTurn) continue;
|
|
120
|
-
|
|
121
|
-
if (event.type === 'assistant/chunk' && event.data?.chunk?.type === 'text-delta') {
|
|
122
|
-
const step = event.data?.step ?? 0;
|
|
123
|
-
const index = event.data.chunk.index ?? 0;
|
|
124
|
-
const key = `${step}:${index}`;
|
|
125
|
-
this.#stepText.set(key, (this.#stepText.get(key) ?? '') + event.data.chunk.text);
|
|
126
|
-
const prefix = `${step}:`;
|
|
127
|
-
const text = [...this.#stepText.entries()]
|
|
128
|
-
.filter(([partKey]) => partKey.startsWith(prefix))
|
|
129
|
-
.sort(([left], [right]) => Number(left.split(':')[1]) - Number(right.split(':')[1]))
|
|
130
|
-
.map(([, part]) => part)
|
|
131
|
-
.join('\n')
|
|
132
|
-
.trim();
|
|
133
|
-
if (text && text !== this.#latestText) {
|
|
134
|
-
this.#latestText = text;
|
|
135
|
-
update = { type: 'text', text };
|
|
136
|
-
}
|
|
137
|
-
continue;
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
if (event.type === 'assistant/message') {
|
|
141
|
-
const text = assistantMessageText(event);
|
|
142
|
-
if (text && text !== this.#latestText) {
|
|
143
|
-
this.#latestText = text;
|
|
144
|
-
update = { type: 'text', text };
|
|
145
|
-
}
|
|
146
|
-
continue;
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
if (event.type === 'tool/call') {
|
|
150
|
-
update = { type: 'tool', name: event.data?.name ?? '工具' };
|
|
151
|
-
} else if (event.type === 'tool/result') {
|
|
152
|
-
update = { type: 'status', text: '正在整理结果…' };
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
return update;
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
export class HarnessRpcError extends Error {
|
|
160
|
-
constructor(method, error) {
|
|
161
|
-
super(`${method}: ${error?.message ?? 'unknown Harness RPC error'}`);
|
|
162
|
-
this.name = 'HarnessRpcError';
|
|
163
|
-
this.method = method;
|
|
164
|
-
this.code = error?.code ?? 'internal';
|
|
165
|
-
this.details = error?.details ?? {};
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
export class HarnessClient {
|
|
170
|
-
#baseUrl;
|
|
171
|
-
#workspace;
|
|
172
|
-
#agentPreset;
|
|
173
|
-
#autostart;
|
|
174
|
-
#dshBin;
|
|
175
|
-
#managedProcess = null;
|
|
176
|
-
|
|
177
|
-
constructor({ baseUrl, workspace, agentPreset = 'standard', autostart = false, dshBin = 'dsh' }) {
|
|
178
|
-
this.#baseUrl = new URL(baseUrl);
|
|
179
|
-
this.#workspace = workspace;
|
|
180
|
-
this.#agentPreset = agentPreset;
|
|
181
|
-
this.#autostart = autostart;
|
|
182
|
-
this.#dshBin = dshBin;
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
async rpc(method, payload = {}, timeoutMs = 30_000, options = {}) {
|
|
186
|
-
const rpcId = options.rpcId ?? `weixin-${randomUUID()}`;
|
|
187
|
-
const response = await fetch(new URL(`/api/${method}`, this.#baseUrl), {
|
|
188
|
-
method: 'POST',
|
|
189
|
-
headers: { 'content-type': 'application/json' },
|
|
190
|
-
body: JSON.stringify({ type: 'client-request', rpcId, method, payload }),
|
|
191
|
-
signal: AbortSignal.timeout(timeoutMs),
|
|
1
|
+
import {
|
|
2
|
+
HarnessClient as SharedHarnessClient,
|
|
3
|
+
} from '../shared/harness-client.mjs';
|
|
4
|
+
|
|
5
|
+
export {
|
|
6
|
+
HarnessInteractionError,
|
|
7
|
+
HarnessReplyTracker,
|
|
8
|
+
HarnessRpcError,
|
|
9
|
+
} from '../shared/harness-client.mjs';
|
|
10
|
+
|
|
11
|
+
export class HarnessClient extends SharedHarnessClient {
|
|
12
|
+
constructor(options) {
|
|
13
|
+
super({
|
|
14
|
+
...options,
|
|
15
|
+
rpcIdPrefix: 'weixin',
|
|
16
|
+
logPrefix: 'dsh-weixin',
|
|
192
17
|
});
|
|
193
|
-
if (!response.ok) throw new Error(`Harness transport ${method} failed: HTTP ${response.status}`);
|
|
194
|
-
const body = await response.json();
|
|
195
|
-
if (body?.type !== 'server-response' || body?.rpcId !== rpcId) {
|
|
196
|
-
throw new Error(`Harness returned an invalid response for ${method}`);
|
|
197
|
-
}
|
|
198
|
-
if (!body.result?.ok) throw new HarnessRpcError(method, body.result?.error);
|
|
199
|
-
return body.result.value;
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
async health() {
|
|
203
|
-
await this.rpc('host.describe', {}, 5_000);
|
|
204
|
-
return true;
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
async ensureRunning() {
|
|
208
|
-
try {
|
|
209
|
-
return await this.health();
|
|
210
|
-
} catch (firstError) {
|
|
211
|
-
if (!this.#autostart) throw firstError;
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
if (!this.#managedProcess || this.#managedProcess.exitCode !== null) {
|
|
215
|
-
const port = this.#baseUrl.port || (this.#baseUrl.protocol === 'https:' ? '443' : '80');
|
|
216
|
-
this.#managedProcess = spawn(this.#dshBin, [
|
|
217
|
-
'web', '--host', this.#baseUrl.hostname, '--port', port,
|
|
218
|
-
], {
|
|
219
|
-
cwd: this.#workspace,
|
|
220
|
-
env: process.env,
|
|
221
|
-
stdio: ['ignore', 'inherit', 'inherit'],
|
|
222
|
-
});
|
|
223
|
-
this.#managedProcess.on('error', (error) => {
|
|
224
|
-
console.error('[dsh-weixin] failed to start Harness:', error.message);
|
|
225
|
-
});
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
const deadline = Date.now() + 60_000;
|
|
229
|
-
let lastError;
|
|
230
|
-
while (Date.now() < deadline) {
|
|
231
|
-
await sleep(1_000);
|
|
232
|
-
try {
|
|
233
|
-
return await this.health();
|
|
234
|
-
} catch (error) {
|
|
235
|
-
lastError = error;
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
throw new Error(`Harness did not become ready: ${lastError?.message ?? 'timeout'}`);
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
async listWorkspaces(options = {}) {
|
|
242
|
-
await this.ensureRunning();
|
|
243
|
-
return workspacePaths(await this.rpc('workspace.list', {}, 30_000, options));
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
async listWorkspaceSessions(workspacePath, options = {}) {
|
|
247
|
-
await this.ensureRunning();
|
|
248
|
-
const workspaceList = await this.rpc('workspace.list', {}, 30_000, options);
|
|
249
|
-
const workspace = workspaceFromList(workspacePath, workspaceList);
|
|
250
|
-
if (!workspace) return { workspace: workspacePath, sessions: [] };
|
|
251
|
-
const sessionList = await this.rpc('session.list', {}, 30_000, options);
|
|
252
|
-
return workspaceSessions(workspace, workspaceList.archivedSessionIds, sessionList);
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
async adoptWorkspaceSession(value, options = {}) {
|
|
256
|
-
return adoptRegisteredWorkspaceSession(this, value, options);
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
async workspaceId(options = {}) {
|
|
260
|
-
const workspace = options.workspace ?? this.#workspace;
|
|
261
|
-
const { items } = await this.rpc('workspace.list', {});
|
|
262
|
-
const existing = items.find((item) => item.path === workspace);
|
|
263
|
-
if (existing) return existing.workspaceId;
|
|
264
|
-
const created = await this.rpc('workspace.create', { path: workspace });
|
|
265
|
-
return created.workspace.workspaceId;
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
async createSession(options = {}) {
|
|
269
|
-
await this.ensureRunning();
|
|
270
|
-
const workspaceId = await this.workspaceId(options);
|
|
271
|
-
const created = await this.rpc('session.create', {
|
|
272
|
-
workspaceId,
|
|
273
|
-
agentPreset: this.#agentPreset,
|
|
274
|
-
});
|
|
275
|
-
return created.sessionId;
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
async sessionExists(sessionId) {
|
|
279
|
-
try {
|
|
280
|
-
await this.rpc('session.history', { sessionId, maxMessages: 1 });
|
|
281
|
-
return true;
|
|
282
|
-
} catch (error) {
|
|
283
|
-
if (error instanceof HarnessRpcError && error.code === 'session-not-found') return false;
|
|
284
|
-
throw error;
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
async ask(sessionId, text, options = {}) {
|
|
289
|
-
if (typeof options === 'number') options = { timeoutMs: options };
|
|
290
|
-
const timeoutMs = options.timeoutMs ?? 600_000;
|
|
291
|
-
const onUpdate = typeof options.onUpdate === 'function' ? options.onUpdate : null;
|
|
292
|
-
await this.ensureRunning();
|
|
293
|
-
const before = await this.rpc('session.history', { sessionId, maxMessages: 1 });
|
|
294
|
-
const baselineSeq = Math.max(-1, ...(before.events ?? []).map(({ event }) => event.seq ?? -1));
|
|
295
|
-
const promptRpcId = `weixin-${randomUUID()}`;
|
|
296
|
-
const tracker = new HarnessReplyTracker({ promptRpcId, afterSeq: baselineSeq });
|
|
297
|
-
|
|
298
|
-
await this.rpc('session.prompt', {
|
|
299
|
-
sessionId,
|
|
300
|
-
mode: 'queue',
|
|
301
|
-
content: [{ type: 'text', text }],
|
|
302
|
-
clientTimeZone: Intl.DateTimeFormat().resolvedOptions().timeZone,
|
|
303
|
-
}, 30_000, { rpcId: promptRpcId });
|
|
304
|
-
|
|
305
|
-
const deadline = Date.now() + timeoutMs;
|
|
306
|
-
while (Date.now() < deadline) {
|
|
307
|
-
await sleep(300);
|
|
308
|
-
const history = await this.rpc('session.history', { sessionId, maxMessages: 50 });
|
|
309
|
-
const update = tracker.consume(history.events ?? []);
|
|
310
|
-
if (update && onUpdate) {
|
|
311
|
-
try {
|
|
312
|
-
await onUpdate(update);
|
|
313
|
-
} catch (error) {
|
|
314
|
-
console.warn('[dsh-weixin] ignored a progress update failure:', error.message);
|
|
315
|
-
}
|
|
316
|
-
}
|
|
317
|
-
if (!tracker.finished) continue;
|
|
318
|
-
if (tracker.answer) return tracker.answer;
|
|
319
|
-
throw new Error(
|
|
320
|
-
`Harness turn ended without a text reply${tracker.reason ? ` (${JSON.stringify(tracker.reason)})` : ''}`,
|
|
321
|
-
);
|
|
322
|
-
}
|
|
323
|
-
throw new Error(`Harness reply timed out after ${Math.round(timeoutMs / 1_000)} seconds`);
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
stopManagedProcess() {
|
|
327
|
-
if (this.#managedProcess?.exitCode === null) this.#managedProcess.kill('SIGTERM');
|
|
328
18
|
}
|
|
329
19
|
}
|