@ryuu-64/dsh-session-tools 0.7.0 → 0.8.1
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 +21 -21
- package/README.md +21 -50
- package/README.zh-CN.md +39 -0
- package/cordis.patch.yml +4 -4
- package/lib/client.js +623 -145
- package/lib/communication-permissions.js +144 -0
- package/lib/communication-remote.js +75 -0
- package/lib/index.js +906 -835
- package/lib/message-receipts.js +156 -121
- package/lib/session-activity.js +135 -135
- package/lib/session-discovery.js +213 -204
- package/locale/en.json +7 -0
- package/locale/zh.json +7 -0
- package/package.json +36 -13
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import { HarnessError } from '@deepseek-ai/dsh-llm';
|
|
2
|
+
import { symbols } from '@deepseek-ai/cordis';
|
|
3
|
+
import { defineDomain, domainTable } from '@deepseek-ai/dsh-storage-domain';
|
|
4
|
+
import { z } from 'zod';
|
|
5
|
+
|
|
6
|
+
const recordSchema = z.object({
|
|
7
|
+
send: z.boolean(), receive: z.boolean(), revision: z.number().int().nonnegative().safe(),
|
|
8
|
+
}).strict();
|
|
9
|
+
const spec = defineDomain({
|
|
10
|
+
name: 'session_tools_communication', version: 1,
|
|
11
|
+
// Authoritative data: malformed records reject the whole domain, never skip.
|
|
12
|
+
tables: { sessions: domainTable(recordSchema) },
|
|
13
|
+
});
|
|
14
|
+
const queues = new WeakMap();
|
|
15
|
+
const legacyDefaults = Object.freeze({ send: true, receive: true });
|
|
16
|
+
export class CommunicationDenied extends HarnessError {
|
|
17
|
+
constructor(direction) {
|
|
18
|
+
super(`未送达、未排队:${direction === 'send' ? '来源会话已关闭发送。' : '目标会话已关闭接收。'}没有消息回执;通信权限改变前不要重复发送。`, 'session-tools/permission-denied');
|
|
19
|
+
this.name = 'CommunicationDenied';
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
export const sessionIdentitySchema = z.string().min(1).max(1024);
|
|
23
|
+
function sessionKey(id) {
|
|
24
|
+
sessionIdentitySchema.parse(id);
|
|
25
|
+
return id;
|
|
26
|
+
}
|
|
27
|
+
export function trustedSource(exec) { return sessionKey(exec?.agent?.session?.header?.id); }
|
|
28
|
+
|
|
29
|
+
/** Share the durable table and serialization across plugin instances. */
|
|
30
|
+
function communicationQueue(ctx) {
|
|
31
|
+
const service = ctx.get('storageDomain');
|
|
32
|
+
if (typeof service?.open !== 'function') throw new Error('Session communication controls require the DSH storageDomain service.');
|
|
33
|
+
const identity = service[symbols.original] ?? service;
|
|
34
|
+
let queue = queues.get(identity);
|
|
35
|
+
if (!queue || queue.closing) {
|
|
36
|
+
const ready = queue?.closing ? queue.closing.then(() => service.open(spec)) : service.open(spec);
|
|
37
|
+
// A malformed or unavailable domain rejects access without an unhandled rejection.
|
|
38
|
+
ready.catch(() => {});
|
|
39
|
+
queue = { ready, tail: Promise.resolve(), owners: 0 };
|
|
40
|
+
queues.set(identity, queue);
|
|
41
|
+
}
|
|
42
|
+
queue.owners++;
|
|
43
|
+
let released = false;
|
|
44
|
+
const assertActive = () => {
|
|
45
|
+
if (released) throw new Error('Session communication controls were unloaded.');
|
|
46
|
+
};
|
|
47
|
+
const serial = async (action) => {
|
|
48
|
+
assertActive();
|
|
49
|
+
const result = queue.tail.then(async () => {
|
|
50
|
+
const table = (await queue.ready).table('sessions');
|
|
51
|
+
assertActive();
|
|
52
|
+
return action(table);
|
|
53
|
+
});
|
|
54
|
+
queue.tail = result.then(() => {}, () => {});
|
|
55
|
+
return result;
|
|
56
|
+
};
|
|
57
|
+
ctx.effect?.(() => () => {
|
|
58
|
+
released = true;
|
|
59
|
+
if (--queue.owners > 0) return;
|
|
60
|
+
queue.closing = queue.tail.then(async () => {
|
|
61
|
+
await (await queue.ready.catch(() => undefined))?.close();
|
|
62
|
+
}).finally(() => {
|
|
63
|
+
if (queues.get(identity) === queue) queues.delete(identity);
|
|
64
|
+
});
|
|
65
|
+
return queue.closing;
|
|
66
|
+
});
|
|
67
|
+
return { serial, assertActive };
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function communicationPermissions(ctx, getDefaults) {
|
|
71
|
+
const { serial: enqueue, assertActive } = communicationQueue(ctx);
|
|
72
|
+
const snapshotDefaults = () => ({ ...getDefaults(), revision: 0 });
|
|
73
|
+
const serial = action => enqueue(table => action(table, snapshotDefaults()));
|
|
74
|
+
const read = (table, id, defaults) => ({ ...recordSchema.parse(table.get(sessionKey(id)) ?? defaults) });
|
|
75
|
+
const check = (table, sourceId, targetId, defaults) => {
|
|
76
|
+
if (!read(table, sourceId, defaults).send) throw new CommunicationDenied('send');
|
|
77
|
+
if (targetId !== undefined && !read(table, targetId, defaults).receive) throw new CommunicationDenied('receive');
|
|
78
|
+
};
|
|
79
|
+
return {
|
|
80
|
+
get: (id) => serial((table, defaults) => read(table, id, defaults)),
|
|
81
|
+
check: (sourceId, targetId) => serial((table, defaults) => check(table, sourceId, targetId, defaults)),
|
|
82
|
+
prepareCreation: (sourceId, targetId, exec) => serial((table, defaults) => {
|
|
83
|
+
exec.signal?.throwIfAborted();
|
|
84
|
+
check(table, sourceId, targetId, defaults);
|
|
85
|
+
return {
|
|
86
|
+
// DSH invokes this synchronously after setup and just before publishing the child.
|
|
87
|
+
commit() {
|
|
88
|
+
assertActive();
|
|
89
|
+
exec.signal?.throwIfAborted();
|
|
90
|
+
check(table, sourceId, targetId, snapshotDefaults());
|
|
91
|
+
},
|
|
92
|
+
};
|
|
93
|
+
}),
|
|
94
|
+
// One authoritative snapshot for the current visible page; never retain it
|
|
95
|
+
// in discovery cursors. This is switch capability, not delivery or approval.
|
|
96
|
+
statuses: (sourceId, targetIds) => serial((table, defaults) => {
|
|
97
|
+
const source = read(table, sourceId, defaults);
|
|
98
|
+
const checkedAt = Date.now();
|
|
99
|
+
return targetIds.map(targetId => {
|
|
100
|
+
const target = read(table, targetId, defaults);
|
|
101
|
+
const reason = !source.send && !target.receive ? 'both_disabled'
|
|
102
|
+
: !source.send ? 'sender_send_disabled' : !target.receive ? 'target_receive_disabled' : 'switches_allow';
|
|
103
|
+
return { state: source.send && target.receive ? 'allowed' : 'blocked', reason,
|
|
104
|
+
senderSend: source.send, targetReceive: target.receive, checkedAt };
|
|
105
|
+
});
|
|
106
|
+
}),
|
|
107
|
+
set: (id, patch, expectedRevision, expectedDefaults = legacyDefaults) => serial(async (table, defaults) => {
|
|
108
|
+
const parsed = recordSchema.parse({ ...patch, revision: expectedRevision });
|
|
109
|
+
const current = read(table, id, defaults);
|
|
110
|
+
if (current.revision !== expectedRevision) return { status: 'conflict', current };
|
|
111
|
+
// Inherited values can change while revision zero still denotes no stored record.
|
|
112
|
+
if (current.revision === 0 && (current.send !== expectedDefaults.send || current.receive !== expectedDefaults.receive)) {
|
|
113
|
+
return { status: 'conflict', current };
|
|
114
|
+
}
|
|
115
|
+
if (current.revision === Number.MAX_SAFE_INTEGER) throw new Error('Permission revision exhausted');
|
|
116
|
+
const next = { send: parsed.send, receive: parsed.receive, revision: current.revision + 1 };
|
|
117
|
+
await table.put(sessionKey(id), next);
|
|
118
|
+
return { status: 'saved', current: { ...next } };
|
|
119
|
+
}),
|
|
120
|
+
// No await between the final synchronous read/check and followup callback.
|
|
121
|
+
admit: (sourceId, targetId, exec, followup) => serial((table, defaults) => {
|
|
122
|
+
exec.signal?.throwIfAborted();
|
|
123
|
+
check(table, sourceId, targetId, defaults);
|
|
124
|
+
return followup();
|
|
125
|
+
}),
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// Explicit unknown never invents open defaults when identity/storage is unavailable.
|
|
130
|
+
export const unknownCommunication = () => ({ state: 'unknown', reason: 'unavailable', checkedAt: Date.now() });
|
|
131
|
+
export const communicationStatusSchema = {
|
|
132
|
+
type: 'object', additionalProperties: false,
|
|
133
|
+
properties: {
|
|
134
|
+
state: { type: 'string', required: true, enum: ['allowed', 'blocked', 'unknown'] },
|
|
135
|
+
reason: { type: 'string', required: true, enum: ['switches_allow', 'sender_send_disabled', 'target_receive_disabled', 'both_disabled', 'unavailable'] },
|
|
136
|
+
senderSend: { type: 'boolean' }, targetReceive: { type: 'boolean' },
|
|
137
|
+
checkedAt: { type: 'number', required: true },
|
|
138
|
+
},
|
|
139
|
+
};
|
|
140
|
+
export function communicationStatusText(status) {
|
|
141
|
+
return ({ switches_allow: '通信开关允许', sender_send_disabled: '当前会话已关闭发送',
|
|
142
|
+
target_receive_disabled: '目标已关闭接收', both_disabled: '当前会话已关闭发送,目标已关闭接收',
|
|
143
|
+
unavailable: '通信状态无法确认' })[status.reason] ?? '通信状态无法确认';
|
|
144
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { symbols } from '@deepseek-ai/cordis';
|
|
2
|
+
import { Remote, RemoteError, TypertRemoteService } from '@deepseek-ai/dsh-typert-protocol';
|
|
3
|
+
import { z } from 'zod';
|
|
4
|
+
import { sessionIdentitySchema } from './communication-permissions.js';
|
|
5
|
+
|
|
6
|
+
const sessionId = sessionIdentitySchema;
|
|
7
|
+
const getSchema = z.object({ sessionId }).strict();
|
|
8
|
+
const switches = z.object({ send: z.boolean(), receive: z.boolean() }).strict();
|
|
9
|
+
const setSchema = switches.extend({
|
|
10
|
+
sessionId, expectedRevision: z.number().int().nonnegative().safe(), expectedDefaults: switches.optional(),
|
|
11
|
+
});
|
|
12
|
+
const recordSchema = z.object({ send: z.boolean(), receive: z.boolean(), revision: z.number().int().nonnegative().safe() }).strict();
|
|
13
|
+
const setResultSchema = z.object({ status: z.enum(['saved', 'conflict']), current: recordSchema }).strict();
|
|
14
|
+
export const communicationRemote = {
|
|
15
|
+
package: '@ryuu-64/dsh-session-tools',
|
|
16
|
+
descriptors: ['get', 'set'].map(method => ({
|
|
17
|
+
id: `@ryuu-64/dsh-session-tools:sessionCommunication.${method}`,
|
|
18
|
+
service: 'sessionCommunication', namespace: 'sessionCommunication', method,
|
|
19
|
+
invocation: { kind: 'direct' },
|
|
20
|
+
parameters: [{ name: 'request', wire: 'request', source: 'json', codec: {
|
|
21
|
+
mode: 'strict', typeSymbol: `@ryuu-64/dsh-session-tools:sessionCommunication.${method}:request`,
|
|
22
|
+
create: () => method === 'get' ? getSchema : setSchema,
|
|
23
|
+
} }],
|
|
24
|
+
result: {
|
|
25
|
+
mode: 'strict', typeSymbol: `@ryuu-64/dsh-session-tools:sessionCommunication.${method}:result`,
|
|
26
|
+
create: () => method === 'get' ? recordSchema : setResultSchema,
|
|
27
|
+
},
|
|
28
|
+
})),
|
|
29
|
+
};
|
|
30
|
+
const initializers = [];
|
|
31
|
+
const original = value => value?.[symbols.original] ?? value;
|
|
32
|
+
/** A Remote UI surface only. No corresponding model tool is registered. */
|
|
33
|
+
export class CommunicationRemote extends TypertRemoteService {
|
|
34
|
+
static inject = ['typert', 'connection'];
|
|
35
|
+
constructor(ctx, permissions) {
|
|
36
|
+
super(ctx, 'sessionCommunication');
|
|
37
|
+
this.permissions = permissions;
|
|
38
|
+
for (const initialize of initializers) initialize.call(this);
|
|
39
|
+
ctx.typert.register({
|
|
40
|
+
package: communicationRemote.package, face: 'host', schemas: [],
|
|
41
|
+
model: { services: [], events: [], objects: [] },
|
|
42
|
+
invocations: communicationRemote.descriptors,
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
authorize() {
|
|
46
|
+
const invocation = this.ctx.invocation;
|
|
47
|
+
const peer = invocation?.peer;
|
|
48
|
+
const principal = peer?.principal;
|
|
49
|
+
let admitted = principal?.kind === 'owner';
|
|
50
|
+
if (peer && principal === undefined) {
|
|
51
|
+
// The older gateway identifies its operator by the live Peer object.
|
|
52
|
+
const operator = this.ctx.connection.operator;
|
|
53
|
+
admitted = Boolean(operator && original(peer) === original(operator));
|
|
54
|
+
}
|
|
55
|
+
if (!admitted) {
|
|
56
|
+
throw new RemoteError('gateway/bad-request', 'Only the authenticated host owner can read or change communication permissions.', {});
|
|
57
|
+
}
|
|
58
|
+
invocation.signal.throwIfAborted();
|
|
59
|
+
}
|
|
60
|
+
async get(request) {
|
|
61
|
+
this.authorize();
|
|
62
|
+
const parsed = getSchema.parse(request);
|
|
63
|
+
return await this.permissions.get(parsed.sessionId);
|
|
64
|
+
}
|
|
65
|
+
async set(request) {
|
|
66
|
+
this.authorize();
|
|
67
|
+
const parsed = setSchema.parse(request);
|
|
68
|
+
return await this.permissions.set(parsed.sessionId, { send: parsed.send, receive: parsed.receive }, parsed.expectedRevision, parsed.expectedDefaults);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
// Apply the official standard-decorator initialization contract in plain JS.
|
|
72
|
+
for (const method of ['get', 'set']) Remote(CommunicationRemote.prototype[method], {
|
|
73
|
+
kind: 'method', name: method, static: false, private: false,
|
|
74
|
+
addInitializer: initialize => initializers.push(initialize),
|
|
75
|
+
});
|