@xmanrui/dsh-im 5.0.1 → 5.0.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/client.js +2 -1
- package/lib/index.js +282 -282
- package/package.json +1 -1
- package/plugin-src/host/harness-command-executor.mjs +4 -0
- package/plugin-src/host/harness-peer.mjs +18 -0
- package/plugin-src/host/modern-harness-api.mjs +37 -4
- package/src/channels/shared/diagnostic-details.mjs +1 -1
- package/src/channels/shared/harness-client.mjs +4 -1
- package/src/channels/shared/harness-session-binding.mjs +3 -3
- package/src/channels/wecom/wecom-bridge.mjs +6 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xmanrui/dsh-im",
|
|
3
|
-
"version": "5.0.
|
|
3
|
+
"version": "5.0.2",
|
|
4
4
|
"icon": "./assets/logo-plugin-message-link-gradient.webp",
|
|
5
5
|
"description": "把微信、飞书、企微、钉钉、QQ、Telegram、WhatsApp、Slack、Discord、Matrix接入DSH。 Connect Telegram, WhatsApp, Slack, Discord, Matrix, WeChat, Feishu, WeCom, DingTalk, and QQ to DSH.",
|
|
6
6
|
"keywords": [
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { harnessPeer } from './harness-peer.mjs';
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* Adapt the Host's Typert command gateway to the channel Harness client.
|
|
3
5
|
* Programmatic production fixtures may omit the gateway and exercise other assembly paths.
|
|
@@ -12,12 +14,14 @@ export function createHarnessCommandExecutor(ctx, provided) {
|
|
|
12
14
|
if (typeof gateway.invoke !== 'function') {
|
|
13
15
|
throw new TypeError('dsh-im requires a callable ctx.typertGateway');
|
|
14
16
|
}
|
|
17
|
+
const peer = harnessPeer(ctx);
|
|
15
18
|
return async (sessionId, line, options = {}) => {
|
|
16
19
|
const request = {
|
|
17
20
|
namespace: 'commands',
|
|
18
21
|
method: 'execute',
|
|
19
22
|
args: { agentId: sessionId, line, images: [] },
|
|
20
23
|
signal: options.signal,
|
|
24
|
+
...(peer === undefined ? {} : { peer }),
|
|
21
25
|
};
|
|
22
26
|
try {
|
|
23
27
|
return await gateway.invoke(request);
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
const peers = new WeakMap();
|
|
2
|
+
|
|
3
|
+
/** One caller identity shared by the Host's session and command gateways. */
|
|
4
|
+
export function harnessPeer(ctx) {
|
|
5
|
+
const scope = ctx?.root ?? ctx;
|
|
6
|
+
const cached = peers.get(scope);
|
|
7
|
+
if (cached) return cached;
|
|
8
|
+
const connection = ctx?.connection;
|
|
9
|
+
if (typeof connection?.createPeer !== 'function') return undefined;
|
|
10
|
+
// IM approvals and questions have their own listeners and response routing.
|
|
11
|
+
const peer = connection.createPeer({ principal: { kind: 'owner' }, interactions: 'none' });
|
|
12
|
+
peers.set(scope, peer);
|
|
13
|
+
ctx.effect?.(() => () => {
|
|
14
|
+
if (peers.get(scope) === peer) peers.delete(scope);
|
|
15
|
+
return peer.dispose();
|
|
16
|
+
}, 'dsh-im: shared Harness caller');
|
|
17
|
+
return peer;
|
|
18
|
+
}
|
|
@@ -2,6 +2,8 @@ import { randomUUID } from 'node:crypto';
|
|
|
2
2
|
|
|
3
3
|
import { activeHarnessInteractionOwner, clearModelCatalogCache, hasActiveHarnessInteractionOwner, harnessToolCallFromEvent, HarnessInteractionError } from '../../src/channels/shared/harness-client.mjs';
|
|
4
4
|
import { extractConnectionEvidence } from '../../src/channels/shared/connection-error.mjs';
|
|
5
|
+
import { harnessPeer } from './harness-peer.mjs';
|
|
6
|
+
import { sameWorkspacePath } from '../../src/channels/shared/default-workspace.mjs';
|
|
5
7
|
|
|
6
8
|
const modernApis = new WeakMap();
|
|
7
9
|
|
|
@@ -54,6 +56,12 @@ function questionError(message, code) {
|
|
|
54
56
|
return error;
|
|
55
57
|
}
|
|
56
58
|
|
|
59
|
+
function adoptionError(code, message) {
|
|
60
|
+
const error = new Error(message);
|
|
61
|
+
error.failure = { code, message, details: {} };
|
|
62
|
+
return error;
|
|
63
|
+
}
|
|
64
|
+
|
|
57
65
|
function sessionEvents(session) {
|
|
58
66
|
if (typeof session?.snapshotEvents === 'function') {
|
|
59
67
|
const events = session.snapshotEvents();
|
|
@@ -211,9 +219,7 @@ class ModernHarnessApi {
|
|
|
211
219
|
constructor(ctx, gateway, scope) {
|
|
212
220
|
this.#gateway = gateway;
|
|
213
221
|
this.#scope = scope;
|
|
214
|
-
|
|
215
|
-
// existing event listeners; this peer only owns Gateway calls and streams.
|
|
216
|
-
this.#peer = ctx.connection?.createPeer?.({ principal: { kind: 'owner' }, interactions: 'none' });
|
|
222
|
+
this.#peer = harnessPeer(ctx);
|
|
217
223
|
|
|
218
224
|
this.host = Object.freeze({
|
|
219
225
|
describe: (request) => rpcResult(request, () => ({ ready: true, transport: 'typert' })),
|
|
@@ -231,6 +237,7 @@ class ModernHarnessApi {
|
|
|
231
237
|
create: (request, signal) => rpcResult(request, () => this.#invoke(
|
|
232
238
|
'session', 'create', { request: request.payload }, signal,
|
|
233
239
|
)),
|
|
240
|
+
adopt: (request, signal) => rpcResult(request, () => this.#adoptSession(request.payload, signal)),
|
|
234
241
|
history: (request, signal) => rpcResult(request, () => this.#history(request.payload, signal)),
|
|
235
242
|
prompt: (request, signal) => rpcResult(request, () => this.#invoke(
|
|
236
243
|
'session',
|
|
@@ -342,6 +349,33 @@ class ModernHarnessApi {
|
|
|
342
349
|
return frame;
|
|
343
350
|
}
|
|
344
351
|
|
|
352
|
+
async #adoptSession({ sessionId, workspaceId, cwd }, signal) {
|
|
353
|
+
// Since DSH 0.2.1, create only reuses blank Sessions and can replace their
|
|
354
|
+
// preset. Binding must observe the exact existing identity instead; the
|
|
355
|
+
// next prompt resumes its Agent with the persisted configuration.
|
|
356
|
+
const { header } = await this.#sessionSnapshot(sessionId, 1, signal);
|
|
357
|
+
if (header?.id !== sessionId || typeof header.cwd !== 'string') {
|
|
358
|
+
throw new Error('Harness returned an invalid Session header for adoption');
|
|
359
|
+
}
|
|
360
|
+
if (header.origin !== undefined) {
|
|
361
|
+
throw adoptionError('session-subagent-unsupported', 'Only ordinary Sessions can be adopted');
|
|
362
|
+
}
|
|
363
|
+
let expectedCwd = cwd;
|
|
364
|
+
if (workspaceId !== undefined) {
|
|
365
|
+
const baseline = await this.#workspaceList(signal);
|
|
366
|
+
const workspace = baseline.items?.find(item => item.workspaceId === workspaceId);
|
|
367
|
+
if (cwd !== undefined || !workspace?.sessionIds?.includes(sessionId)) {
|
|
368
|
+
throw adoptionError('session-workspace-ambiguous', 'Session workspace membership changed during adoption');
|
|
369
|
+
}
|
|
370
|
+
expectedCwd = workspace.path;
|
|
371
|
+
}
|
|
372
|
+
if (!await sameWorkspacePath(header.cwd, expectedCwd)) {
|
|
373
|
+
throw adoptionError('session-workspace-ambiguous', 'Session directory does not match its workspace');
|
|
374
|
+
}
|
|
375
|
+
signal?.throwIfAborted();
|
|
376
|
+
return { sessionId };
|
|
377
|
+
}
|
|
378
|
+
|
|
345
379
|
async #history(payload, signal) {
|
|
346
380
|
const { sessionId, maxMessages = 50, beforeSeq } = payload;
|
|
347
381
|
let cursor = this.#sessionCursors.get(sessionId);
|
|
@@ -871,7 +905,6 @@ class ModernHarnessApi {
|
|
|
871
905
|
questionError('dsh-im interaction adapter was disposed', 'ASK_ABORTED'),
|
|
872
906
|
);
|
|
873
907
|
for (const dispose of this.#disposers.splice(0).reverse()) dispose?.();
|
|
874
|
-
return this.#peer?.dispose();
|
|
875
908
|
}
|
|
876
909
|
}
|
|
877
910
|
|
|
@@ -23,7 +23,7 @@ const REASONS = new Set([
|
|
|
23
23
|
]);
|
|
24
24
|
const RESOURCES = new Set(['credential-store', 'account-config', 'account-state', 'workspace-config', 'workspace-directory']);
|
|
25
25
|
const RPC_METHODS = new Set([
|
|
26
|
-
'host.describe', 'workspace.list', 'workspace.create', 'session.list', 'session.create',
|
|
26
|
+
'host.describe', 'workspace.list', 'workspace.create', 'session.list', 'session.create', 'session.adopt',
|
|
27
27
|
'session.history', 'session.prompt', 'session.rename', 'session.cancel', 'session.models',
|
|
28
28
|
'session.permissions', 'session.selectModel', 'llm.models',
|
|
29
29
|
]);
|
|
@@ -1320,7 +1320,10 @@ export class HarnessClient {
|
|
|
1320
1320
|
}
|
|
1321
1321
|
|
|
1322
1322
|
async adoptWorkspaceSession(value, options = {}) {
|
|
1323
|
-
|
|
1323
|
+
// Modern Hosts separate observing an existing Session from creating one.
|
|
1324
|
+
// Keep the legacy transport contract for Hosts without this adapter seam.
|
|
1325
|
+
const method = typeof this.#apiProxy?.sessions?.adopt === 'function' ? 'session.adopt' : 'session.create';
|
|
1326
|
+
return adoptRegisteredWorkspaceSession(this, value, options, 30_000, method);
|
|
1324
1327
|
}
|
|
1325
1328
|
|
|
1326
1329
|
async isUngroupedWorkspace(workspace) {
|
|
@@ -83,7 +83,7 @@ function sessionSummary(sessionId, value) {
|
|
|
83
83
|
return { cwd: summary.cwd, title: typeof title === 'string' ? title : null };
|
|
84
84
|
}
|
|
85
85
|
|
|
86
|
-
export async function adoptRegisteredWorkspaceSession(client, value, options = {}, timeoutMs = 30_000) {
|
|
86
|
+
export async function adoptRegisteredWorkspaceSession(client, value, options = {}, timeoutMs = 30_000, method = 'session.create') {
|
|
87
87
|
const sessionId = validatedSessionId(value);
|
|
88
88
|
await client.ensureRunning(options);
|
|
89
89
|
const workspaceList = await client.rpc('workspace.list', {}, timeoutMs, options);
|
|
@@ -103,12 +103,12 @@ export async function adoptRegisteredWorkspaceSession(client, value, options = {
|
|
|
103
103
|
if (await client.isUngroupedWorkspace?.(workspace.path)) {
|
|
104
104
|
await mkdir(workspace.path, { recursive: true });
|
|
105
105
|
}
|
|
106
|
-
const adopted = await client.rpc(
|
|
106
|
+
const adopted = await client.rpc(method, {
|
|
107
107
|
...(workspace.workspaceId ? { workspaceId: workspace.workspaceId } : { cwd: workspace.path }),
|
|
108
108
|
sessionId,
|
|
109
109
|
}, timeoutMs, options);
|
|
110
110
|
if (!adopted || adopted.sessionId !== sessionId) {
|
|
111
|
-
throw new Error(
|
|
111
|
+
throw new Error(`Harness returned an invalid response for ${method}`);
|
|
112
112
|
}
|
|
113
113
|
return {
|
|
114
114
|
sessionId,
|
|
@@ -24,6 +24,7 @@ import { runCompactCommand } from '../shared/compact-command.mjs';
|
|
|
24
24
|
import { isSharedLocalCommand } from '../shared/command-permission.mjs';
|
|
25
25
|
import { isClearCommand, runClearCommand } from '../shared/clear-command.mjs';
|
|
26
26
|
import { isHistoryCommand, runHistoryCommand } from '../shared/history-command.mjs';
|
|
27
|
+
import { isPermissionCommand, runPermissionCommand } from '../shared/permission-command.mjs';
|
|
27
28
|
import {
|
|
28
29
|
isControlCommand,
|
|
29
30
|
runControlCommand,
|
|
@@ -86,6 +87,8 @@ function helpText() {
|
|
|
86
87
|
t('/new 开启一个全新会话'),
|
|
87
88
|
t('/compact 压缩当前会话的较早上下文'),
|
|
88
89
|
t('/clear 清空当前会话上下文(保留 Session 绑定和历史记录)'),
|
|
90
|
+
t('/permissionlist 按序号列出权限档位'),
|
|
91
|
+
t('/permission [序号或完整ID] 查看或切换当前会话权限'),
|
|
89
92
|
t('/history [数量] 查看最近历史消息(默认 3 条,最多 5 条)'),
|
|
90
93
|
t('/workspace 工作区序号或绝对路径 切换工作区'),
|
|
91
94
|
t('/workspacelist 列出工作区绝对路径'),
|
|
@@ -921,7 +924,8 @@ export class WecomHarnessBridge {
|
|
|
921
924
|
text: commandText,
|
|
922
925
|
hasImages: hasInboundImages(commandMessage),
|
|
923
926
|
hasFiles: hasInboundFiles(commandMessage),
|
|
924
|
-
...(
|
|
927
|
+
...(isPermissionCommand(commandText)
|
|
928
|
+
|| (parseWecomMenu(commandText) && !hasInboundImages(commandMessage) && !hasInboundFiles(commandMessage))
|
|
925
929
|
? { isCommand: true } : {}),
|
|
926
930
|
});
|
|
927
931
|
if (!access.allowed) {
|
|
@@ -976,6 +980,7 @@ export class WecomHarnessBridge {
|
|
|
976
980
|
&& (parseWecomMenu(commandText) || ['/help', '/status', '/new'].includes(commandText.toLowerCase())
|
|
977
981
|
|| this.#cardFrames.has(frame));
|
|
978
982
|
const commandRunner = isClearCommand(commandText) ? runClearCommand
|
|
983
|
+
: isPermissionCommand(commandText) ? runPermissionCommand
|
|
979
984
|
: isHistoryCommand(commandText) ? runHistoryCommand
|
|
980
985
|
: hasInboundFiles(commandMessage) ? null : isControlCommand(commandText)
|
|
981
986
|
? runControlCommand
|