@ryuu-64/dsh-session-tools 0.8.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 -67
- package/README.zh-CN.md +39 -0
- package/cordis.patch.yml +4 -4
- package/lib/client.js +623 -528
- package/lib/communication-permissions.js +144 -101
- package/lib/communication-remote.js +75 -64
- package/lib/index.js +906 -894
- package/lib/message-receipts.js +156 -121
- package/lib/session-activity.js +135 -135
- package/lib/session-discovery.js +213 -213
- package/locale/en.json +7 -0
- package/locale/zh.json +7 -0
- package/package.json +27 -15
package/lib/session-discovery.js
CHANGED
|
@@ -1,213 +1,213 @@
|
|
|
1
|
-
import { createHmac, randomBytes, randomUUID, timingSafeEqual } from "node:crypto";
|
|
2
|
-
import { isAbsolute } from "node:path";
|
|
3
|
-
import { symbols } from "@deepseek-ai/cordis";
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
// Resource guards, not measured throughput promises. No event/log/Agent is retained.
|
|
7
|
-
const TTL = 5 * 60_000;
|
|
8
|
-
const MAX_SETS = 16;
|
|
9
|
-
const MAX_SET_IDS = 10_000;
|
|
10
|
-
const MAX_IDS = 50_000;
|
|
11
|
-
const MAX_BYTES = 8 * 1024 * 1024;
|
|
12
|
-
const TITLE_BATCH_IDS = 64;
|
|
13
|
-
const identity = service => service?.[symbols.original] ?? service;
|
|
14
|
-
const ordinary = ({ header }) => (header.delegationDepth ?? 0) <= 0 && header.origin !== "subagent";
|
|
15
|
-
const normalizeTitle = value => value.normalize("NFC").trim().replace(/\s+/gu, " ").toLowerCase();
|
|
16
|
-
const fail = message => new Error(`list_sessions: ${message}`);
|
|
17
|
-
|
|
18
|
-
function optionalText(value, name, bytes) {
|
|
19
|
-
if (value === undefined) return undefined;
|
|
20
|
-
if (typeof value !== "string" || !value.trim() || Buffer.byteLength(value) > bytes) {
|
|
21
|
-
throw fail(`${name} must be a nonempty string of at most ${bytes} UTF-8 bytes`);
|
|
22
|
-
}
|
|
23
|
-
return value;
|
|
24
|
-
}
|
|
25
|
-
function filters(args) {
|
|
26
|
-
const title = optionalText(args.titleQuery, "titleQuery", 1024);
|
|
27
|
-
const workspaceId = optionalText(args.workspaceId, "workspaceId", 256);
|
|
28
|
-
const workspacePath = optionalText(args.workspacePath, "workspacePath", 4096);
|
|
29
|
-
if (workspaceId !== undefined && workspacePath !== undefined) throw fail("workspaceId and workspacePath are mutually exclusive");
|
|
30
|
-
if (workspacePath !== undefined && !isAbsolute(workspacePath)) throw fail("workspacePath must be absolute");
|
|
31
|
-
return { titleQuery: title === undefined ? undefined : normalizeTitle(title), workspaceId, workspacePath };
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/** One plugin instance owns these short-lived result sets. Tokens never authorize a read. */
|
|
35
|
-
export function createSessionDiscovery(ctx, sortByActivity, inSlot, sessionState) {
|
|
36
|
-
const sets = new Map();
|
|
37
|
-
const key = randomBytes(32);
|
|
38
|
-
let sources;
|
|
39
|
-
let disposed = false;
|
|
40
|
-
let count = 0, bytes = 0, expiryTimer;
|
|
41
|
-
const remove = id => {
|
|
42
|
-
const set = sets.get(id);
|
|
43
|
-
if (!set) return;
|
|
44
|
-
count -= set.rows.length; bytes -= set.bytes; sets.delete(id);
|
|
45
|
-
};
|
|
46
|
-
const clear = () => { clearTimeout(expiryTimer); expiryTimer = undefined; sets.clear(); count = 0; bytes = 0; };
|
|
47
|
-
ctx.on?.("dispose", () => { disposed = true; clear(); });
|
|
48
|
-
const services = () => ["sessionQuery", "sessionPersistence", "workspaceRegistry", "sessions"].map(name => identity(ctx.get(name))).concat(identity(ctx.agents));
|
|
49
|
-
const equalSources = (a, b) => a.length === b.length && a.every((value, i) => value === b[i]);
|
|
50
|
-
const sweep = () => { for (const [id, set] of sets) if (Date.now() >= set.expiresAt) remove(id); };
|
|
51
|
-
const scheduleExpiry = () => {
|
|
52
|
-
clearTimeout(expiryTimer);
|
|
53
|
-
expiryTimer = undefined;
|
|
54
|
-
if (!sets.size) return;
|
|
55
|
-
const expiresAt = Math.min(...Array.from(sets.values(), set => set.expiresAt));
|
|
56
|
-
expiryTimer = setTimeout(() => { sweep(); scheduleExpiry(); }, Math.max(1, expiresAt - Date.now()));
|
|
57
|
-
expiryTimer.unref();
|
|
58
|
-
};
|
|
59
|
-
function token(id, offset) {
|
|
60
|
-
const body = Buffer.from(JSON.stringify([id, offset])).toString("base64url");
|
|
61
|
-
return `${body}.${createHmac("sha256", key).update(body).digest("base64url")}`;
|
|
62
|
-
}
|
|
63
|
-
function decode(value) {
|
|
64
|
-
optionalText(value, "cursor", 512);
|
|
65
|
-
try {
|
|
66
|
-
const [body, signature, extra] = value.split(".");
|
|
67
|
-
if (extra !== undefined) throw Error();
|
|
68
|
-
const expected = createHmac("sha256", key).update(body).digest();
|
|
69
|
-
const actual = Buffer.from(signature, "base64url");
|
|
70
|
-
if (actual.length !== expected.length || !timingSafeEqual(actual, expected)) throw Error();
|
|
71
|
-
const [id, offset] = JSON.parse(Buffer.from(body, "base64url").toString());
|
|
72
|
-
if (typeof id !== "string" || !Number.isSafeInteger(offset) || offset < 1) throw Error();
|
|
73
|
-
return { id, offset };
|
|
74
|
-
} catch { throw fail("invalid cursor; start a new query without cursor"); }
|
|
75
|
-
}
|
|
76
|
-
async function workspace(filter, signal) {
|
|
77
|
-
if (filter.workspaceId === undefined && filter.workspacePath === undefined) return undefined;
|
|
78
|
-
const registry = ctx.get("workspaceRegistry");
|
|
79
|
-
let result;
|
|
80
|
-
if (filter.workspaceId !== undefined) {
|
|
81
|
-
if (typeof registry?.get !== "function") throw fail("this host cannot look up workspace IDs");
|
|
82
|
-
result = registry.get(filter.workspaceId);
|
|
83
|
-
} else {
|
|
84
|
-
if (typeof registry?.resolveByPath !== "function") throw fail("this host cannot resolve workspace paths");
|
|
85
|
-
try { result = await registry.resolveByPath(filter.workspacePath); }
|
|
86
|
-
catch (cause) { throw fail(`workspacePath could not be resolved (${cause.message}); use an existing registered workspace`); }
|
|
87
|
-
}
|
|
88
|
-
signal?.throwIfAborted();
|
|
89
|
-
if (!result) throw fail("workspace is not registered; use an existing workspace ID or path");
|
|
90
|
-
return { id: String(result.id), path: String(result.path), ids: new Set(result.sessionIds.map(String)) };
|
|
91
|
-
}
|
|
92
|
-
async function matchingTitles(query, records, text, signal) {
|
|
93
|
-
if (typeof query.readTitleSnapshots !== "function") throw fail("titleQuery needs the host's readTitleSnapshots API");
|
|
94
|
-
const titles = new Map();
|
|
95
|
-
for (let start = 0; start < records.length; start += TITLE_BATCH_IDS) {
|
|
96
|
-
const ids = records.slice(start, start + TITLE_BATCH_IDS).map(record => String(record.header.id));
|
|
97
|
-
// One corpus listing per batch. Reserve all permits atomically so no other
|
|
98
|
-
// plugin activity/title read overlaps this batch. The provider owns its
|
|
99
|
-
// internal concurrency (default 4); at most 64 requested IDs can be active.
|
|
100
|
-
const result = await inSlot(() => query.readTitleSnapshots(ids, signal), signal, 4);
|
|
101
|
-
signal?.throwIfAborted();
|
|
102
|
-
if (!Array.isArray(result) || result.length !== ids.length) throw fail("incomplete title batch; no complete matching result is available");
|
|
103
|
-
for (let index = 0; index < ids.length; index++) {
|
|
104
|
-
const id = ids[index], observation = result[index];
|
|
105
|
-
if (observation?.sessionId !== id || observation.status !== "fulfilled" || String(observation.value?.session?.id) !== id) {
|
|
106
|
-
throw fail(`could not read the title of ${id}; no complete matching result is available`);
|
|
107
|
-
}
|
|
108
|
-
// The same read may observe a newer header than the directory scan.
|
|
109
|
-
if (!ordinary({ header: observation.value.session })) throw fail("session visibility changed during title scan; start a new query");
|
|
110
|
-
const title = observation.value.title?.title;
|
|
111
|
-
if (title !== undefined && typeof title !== "string") throw fail(`invalid title snapshot for ${id}`);
|
|
112
|
-
if (title !== undefined && normalizeTitle(title).includes(text)) titles.set(id, title);
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
return titles;
|
|
116
|
-
}
|
|
117
|
-
return async (args, exec) => {
|
|
118
|
-
exec.signal?.throwIfAborted();
|
|
119
|
-
if (disposed) throw fail("plugin was unloaded; start a new query");
|
|
120
|
-
const currentSources = services();
|
|
121
|
-
if (sources && !equalSources(sources, currentSources)) clear();
|
|
122
|
-
sources = currentSources;
|
|
123
|
-
sweep();
|
|
124
|
-
const query = ctx.get("sessionQuery");
|
|
125
|
-
if (query === undefined) throw fail("needs session lookup, which this DSH does not offer");
|
|
126
|
-
if (args.limit !== undefined && (typeof args.limit !== "number" || !Number.isFinite(args.limit))) throw fail("limit must be a finite number");
|
|
127
|
-
const limit = Math.max(1, Math.min(100, Math.trunc(args.limit ?? 20)));
|
|
128
|
-
// Agent.id is the existing public tool-execution identity; arguments cannot supply it.
|
|
129
|
-
const owner = typeof exec.agent?.id === "string" && exec.agent.id ? exec.agent.id : undefined;
|
|
130
|
-
let filter = filters(args), set, offset = 0, setId;
|
|
131
|
-
if (args.cursor !== undefined) {
|
|
132
|
-
({ id: setId, offset } = decode(args.cursor));
|
|
133
|
-
set = sets.get(setId);
|
|
134
|
-
if (!set) throw fail("cursor expired, was evicted, or belongs to another plugin/host; start a new query without cursor");
|
|
135
|
-
if (!owner || owner !== set.owner) throw fail("cursor belongs to a different calling session");
|
|
136
|
-
if (offset >= set.rows.length) throw fail("invalid cursor offset");
|
|
137
|
-
if (filter.titleQuery !== undefined && filter.titleQuery !== set.filter.titleQuery) throw fail("cursor titleQuery does not match its original query");
|
|
138
|
-
filter = { ...set.filter, ...(filter.workspaceId !== undefined || filter.workspacePath !== undefined ? { workspaceId: filter.workspaceId, workspacePath: filter.workspacePath } : {}) };
|
|
139
|
-
}
|
|
140
|
-
const selected = await workspace(filter, exec.signal);
|
|
141
|
-
if (set && (selected?.id !== set.workspaceId || selected?.path !== set.workspacePath)) throw fail("cursor workspace does not match its original query");
|
|
142
|
-
const records = await query.listSessions(exec.signal);
|
|
143
|
-
exec.signal?.throwIfAborted();
|
|
144
|
-
sortByActivity.prune(records);
|
|
145
|
-
let candidates = records.filter(record => ordinary(record) && (!selected || selected.ids.has(String(record.header.id))));
|
|
146
|
-
const byId = new Map(candidates.map(record => [String(record.header.id), record]));
|
|
147
|
-
if (set) {
|
|
148
|
-
// Recheck the entire frozen range, not just the requested slice. Never silently skip.
|
|
149
|
-
if (set.rows.some(row => !byId.has(row.id))) {
|
|
150
|
-
remove(setId);
|
|
151
|
-
throw fail("result set contains a session no longer visible or in this workspace; start a new query");
|
|
152
|
-
}
|
|
153
|
-
} else {
|
|
154
|
-
if ((owner || filter.titleQuery !== undefined) && candidates.length > MAX_SET_IDS) throw fail(`candidate set exceeds ${MAX_SET_IDS} sessions; narrow the workspace filter`);
|
|
155
|
-
const titles = filter.titleQuery === undefined ? undefined : await matchingTitles(query, candidates, filter.titleQuery, exec.signal);
|
|
156
|
-
if (titles) candidates = candidates.filter(record => titles.has(String(record.header.id)));
|
|
157
|
-
const ordered = await sortByActivity(candidates, exec.signal, records);
|
|
158
|
-
const rows = ordered.map(record => ({ id: String(record.header.id), ...(titles ? { title: titles.get(String(record.header.id)) } : {}) }));
|
|
159
|
-
set = { owner, filter: { titleQuery: filter.titleQuery, workspaceId: selected?.id }, rows, workspaceId: selected?.id, workspacePath: selected?.path, createdAt: Date.now() };
|
|
160
|
-
set.expiresAt = set.createdAt + TTL;
|
|
161
|
-
set.bytes = Buffer.byteLength(JSON.stringify(set));
|
|
162
|
-
if (owner && rows.length > limit && set.bytes > MAX_BYTES) throw fail("result set exceeds the 8 MiB metadata budget; narrow the query");
|
|
163
|
-
}
|
|
164
|
-
const page = set.rows.slice(offset, offset + limit);
|
|
165
|
-
let communication;
|
|
166
|
-
try {
|
|
167
|
-
communication = await
|
|
168
|
-
} catch {
|
|
169
|
-
communication = page.map(() => unknownCommunication());
|
|
170
|
-
}
|
|
171
|
-
exec.signal?.throwIfAborted();
|
|
172
|
-
const listed = [];
|
|
173
|
-
for (const [pageIndex, row] of page.entries()) {
|
|
174
|
-
exec.signal?.throwIfAborted();
|
|
175
|
-
const header = byId.get(row.id).header;
|
|
176
|
-
let title = row.title;
|
|
177
|
-
if (set.filter.titleQuery === undefined) try {
|
|
178
|
-
// Legacy no-query listing keeps best-effort titles and reads only this page.
|
|
179
|
-
title = (await inSlot(() => query.readTitle(row.id, exec.signal), exec.signal))?.title;
|
|
180
|
-
} catch {}
|
|
181
|
-
exec.signal?.throwIfAborted();
|
|
182
|
-
listed.push({ sessionId: row.id, communication: communication[pageIndex],
|
|
183
|
-
...title === undefined || title === "" ? {} : { title: String(title) },
|
|
184
|
-
...header.cwd === undefined ? {} : { cwd: String(header.cwd) },
|
|
185
|
-
state: sessionState(ctx, row.id), ...row.id === owner ? { current: true } : {},
|
|
186
|
-
});
|
|
187
|
-
}
|
|
188
|
-
exec.signal?.throwIfAborted();
|
|
189
|
-
if (disposed) throw fail("plugin was unloaded; start a new query");
|
|
190
|
-
const hasMore = offset + listed.length < set.rows.length;
|
|
191
|
-
if (setId || (hasMore && owner)) {
|
|
192
|
-
if (!equalSources(currentSources, services())) { clear(); throw fail("host services changed; start a new query"); }
|
|
193
|
-
if (Date.now() >= set.expiresAt) { if (setId) remove(setId); throw fail("cursor expired while reading; start a new query"); }
|
|
194
|
-
if (!setId) {
|
|
195
|
-
sweep();
|
|
196
|
-
while (sets.size >= MAX_SETS || count + set.rows.length > MAX_IDS || bytes + set.bytes > MAX_BYTES) remove(sets.keys().next().value);
|
|
197
|
-
setId = randomUUID(); count += set.rows.length; bytes += set.bytes;
|
|
198
|
-
} else {
|
|
199
|
-
// An overlapping call may have evicted this set while this page was read.
|
|
200
|
-
if (sets.get(setId) !== set) throw fail("cursor was evicted while reading; start a new query");
|
|
201
|
-
sets.delete(setId);
|
|
202
|
-
}
|
|
203
|
-
sets.set(setId, set);
|
|
204
|
-
scheduleExpiry();
|
|
205
|
-
}
|
|
206
|
-
return { sessions: listed, total: records.length, matchedTotal: set.rows.length,
|
|
207
|
-
...(setId ? { resultSetCreatedAt: set.createdAt, resultSetExpiresAt: set.expiresAt,
|
|
208
|
-
...(hasMore ? { nextCursor: token(setId, offset + listed.length) } : {}) } : {}),
|
|
209
|
-
...(hasMore && !owner ? { paginationUnavailable: "A calling Agent identity is required to continue; run list_sessions through the host tool runtime." } : {}),
|
|
210
|
-
...(selected ? { workspaceId: selected.id, workspacePath: selected.path } : {}),
|
|
211
|
-
};
|
|
212
|
-
};
|
|
213
|
-
}
|
|
1
|
+
import { createHmac, randomBytes, randomUUID, timingSafeEqual } from "node:crypto";
|
|
2
|
+
import { isAbsolute } from "node:path";
|
|
3
|
+
import { symbols } from "@deepseek-ai/cordis";
|
|
4
|
+
import { trustedSource, unknownCommunication } from "./communication-permissions.js";
|
|
5
|
+
|
|
6
|
+
// Resource guards, not measured throughput promises. No event/log/Agent is retained.
|
|
7
|
+
const TTL = 5 * 60_000;
|
|
8
|
+
const MAX_SETS = 16;
|
|
9
|
+
const MAX_SET_IDS = 10_000;
|
|
10
|
+
const MAX_IDS = 50_000;
|
|
11
|
+
const MAX_BYTES = 8 * 1024 * 1024;
|
|
12
|
+
const TITLE_BATCH_IDS = 64;
|
|
13
|
+
const identity = service => service?.[symbols.original] ?? service;
|
|
14
|
+
const ordinary = ({ header }) => (header.delegationDepth ?? 0) <= 0 && header.origin !== "subagent";
|
|
15
|
+
const normalizeTitle = value => value.normalize("NFC").trim().replace(/\s+/gu, " ").toLowerCase();
|
|
16
|
+
const fail = message => new Error(`list_sessions: ${message}`);
|
|
17
|
+
|
|
18
|
+
function optionalText(value, name, bytes) {
|
|
19
|
+
if (value === undefined) return undefined;
|
|
20
|
+
if (typeof value !== "string" || !value.trim() || Buffer.byteLength(value) > bytes) {
|
|
21
|
+
throw fail(`${name} must be a nonempty string of at most ${bytes} UTF-8 bytes`);
|
|
22
|
+
}
|
|
23
|
+
return value;
|
|
24
|
+
}
|
|
25
|
+
function filters(args) {
|
|
26
|
+
const title = optionalText(args.titleQuery, "titleQuery", 1024);
|
|
27
|
+
const workspaceId = optionalText(args.workspaceId, "workspaceId", 256);
|
|
28
|
+
const workspacePath = optionalText(args.workspacePath, "workspacePath", 4096);
|
|
29
|
+
if (workspaceId !== undefined && workspacePath !== undefined) throw fail("workspaceId and workspacePath are mutually exclusive");
|
|
30
|
+
if (workspacePath !== undefined && !isAbsolute(workspacePath)) throw fail("workspacePath must be absolute");
|
|
31
|
+
return { titleQuery: title === undefined ? undefined : normalizeTitle(title), workspaceId, workspacePath };
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** One plugin instance owns these short-lived result sets. Tokens never authorize a read. */
|
|
35
|
+
export function createSessionDiscovery(ctx, sortByActivity, inSlot, sessionState, permissions) {
|
|
36
|
+
const sets = new Map();
|
|
37
|
+
const key = randomBytes(32);
|
|
38
|
+
let sources;
|
|
39
|
+
let disposed = false;
|
|
40
|
+
let count = 0, bytes = 0, expiryTimer;
|
|
41
|
+
const remove = id => {
|
|
42
|
+
const set = sets.get(id);
|
|
43
|
+
if (!set) return;
|
|
44
|
+
count -= set.rows.length; bytes -= set.bytes; sets.delete(id);
|
|
45
|
+
};
|
|
46
|
+
const clear = () => { clearTimeout(expiryTimer); expiryTimer = undefined; sets.clear(); count = 0; bytes = 0; };
|
|
47
|
+
ctx.on?.("dispose", () => { disposed = true; clear(); });
|
|
48
|
+
const services = () => ["sessionQuery", "sessionPersistence", "workspaceRegistry", "sessions"].map(name => identity(ctx.get(name))).concat(identity(ctx.agents));
|
|
49
|
+
const equalSources = (a, b) => a.length === b.length && a.every((value, i) => value === b[i]);
|
|
50
|
+
const sweep = () => { for (const [id, set] of sets) if (Date.now() >= set.expiresAt) remove(id); };
|
|
51
|
+
const scheduleExpiry = () => {
|
|
52
|
+
clearTimeout(expiryTimer);
|
|
53
|
+
expiryTimer = undefined;
|
|
54
|
+
if (!sets.size) return;
|
|
55
|
+
const expiresAt = Math.min(...Array.from(sets.values(), set => set.expiresAt));
|
|
56
|
+
expiryTimer = setTimeout(() => { sweep(); scheduleExpiry(); }, Math.max(1, expiresAt - Date.now()));
|
|
57
|
+
expiryTimer.unref();
|
|
58
|
+
};
|
|
59
|
+
function token(id, offset) {
|
|
60
|
+
const body = Buffer.from(JSON.stringify([id, offset])).toString("base64url");
|
|
61
|
+
return `${body}.${createHmac("sha256", key).update(body).digest("base64url")}`;
|
|
62
|
+
}
|
|
63
|
+
function decode(value) {
|
|
64
|
+
optionalText(value, "cursor", 512);
|
|
65
|
+
try {
|
|
66
|
+
const [body, signature, extra] = value.split(".");
|
|
67
|
+
if (extra !== undefined) throw Error();
|
|
68
|
+
const expected = createHmac("sha256", key).update(body).digest();
|
|
69
|
+
const actual = Buffer.from(signature, "base64url");
|
|
70
|
+
if (actual.length !== expected.length || !timingSafeEqual(actual, expected)) throw Error();
|
|
71
|
+
const [id, offset] = JSON.parse(Buffer.from(body, "base64url").toString());
|
|
72
|
+
if (typeof id !== "string" || !Number.isSafeInteger(offset) || offset < 1) throw Error();
|
|
73
|
+
return { id, offset };
|
|
74
|
+
} catch { throw fail("invalid cursor; start a new query without cursor"); }
|
|
75
|
+
}
|
|
76
|
+
async function workspace(filter, signal) {
|
|
77
|
+
if (filter.workspaceId === undefined && filter.workspacePath === undefined) return undefined;
|
|
78
|
+
const registry = ctx.get("workspaceRegistry");
|
|
79
|
+
let result;
|
|
80
|
+
if (filter.workspaceId !== undefined) {
|
|
81
|
+
if (typeof registry?.get !== "function") throw fail("this host cannot look up workspace IDs");
|
|
82
|
+
result = registry.get(filter.workspaceId);
|
|
83
|
+
} else {
|
|
84
|
+
if (typeof registry?.resolveByPath !== "function") throw fail("this host cannot resolve workspace paths");
|
|
85
|
+
try { result = await registry.resolveByPath(filter.workspacePath); }
|
|
86
|
+
catch (cause) { throw fail(`workspacePath could not be resolved (${cause.message}); use an existing registered workspace`); }
|
|
87
|
+
}
|
|
88
|
+
signal?.throwIfAborted();
|
|
89
|
+
if (!result) throw fail("workspace is not registered; use an existing workspace ID or path");
|
|
90
|
+
return { id: String(result.id), path: String(result.path), ids: new Set(result.sessionIds.map(String)) };
|
|
91
|
+
}
|
|
92
|
+
async function matchingTitles(query, records, text, signal) {
|
|
93
|
+
if (typeof query.readTitleSnapshots !== "function") throw fail("titleQuery needs the host's readTitleSnapshots API");
|
|
94
|
+
const titles = new Map();
|
|
95
|
+
for (let start = 0; start < records.length; start += TITLE_BATCH_IDS) {
|
|
96
|
+
const ids = records.slice(start, start + TITLE_BATCH_IDS).map(record => String(record.header.id));
|
|
97
|
+
// One corpus listing per batch. Reserve all permits atomically so no other
|
|
98
|
+
// plugin activity/title read overlaps this batch. The provider owns its
|
|
99
|
+
// internal concurrency (default 4); at most 64 requested IDs can be active.
|
|
100
|
+
const result = await inSlot(() => query.readTitleSnapshots(ids, signal), signal, 4);
|
|
101
|
+
signal?.throwIfAborted();
|
|
102
|
+
if (!Array.isArray(result) || result.length !== ids.length) throw fail("incomplete title batch; no complete matching result is available");
|
|
103
|
+
for (let index = 0; index < ids.length; index++) {
|
|
104
|
+
const id = ids[index], observation = result[index];
|
|
105
|
+
if (observation?.sessionId !== id || observation.status !== "fulfilled" || String(observation.value?.session?.id) !== id) {
|
|
106
|
+
throw fail(`could not read the title of ${id}; no complete matching result is available`);
|
|
107
|
+
}
|
|
108
|
+
// The same read may observe a newer header than the directory scan.
|
|
109
|
+
if (!ordinary({ header: observation.value.session })) throw fail("session visibility changed during title scan; start a new query");
|
|
110
|
+
const title = observation.value.title?.title;
|
|
111
|
+
if (title !== undefined && typeof title !== "string") throw fail(`invalid title snapshot for ${id}`);
|
|
112
|
+
if (title !== undefined && normalizeTitle(title).includes(text)) titles.set(id, title);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
return titles;
|
|
116
|
+
}
|
|
117
|
+
return async (args, exec) => {
|
|
118
|
+
exec.signal?.throwIfAborted();
|
|
119
|
+
if (disposed) throw fail("plugin was unloaded; start a new query");
|
|
120
|
+
const currentSources = services();
|
|
121
|
+
if (sources && !equalSources(sources, currentSources)) clear();
|
|
122
|
+
sources = currentSources;
|
|
123
|
+
sweep();
|
|
124
|
+
const query = ctx.get("sessionQuery");
|
|
125
|
+
if (query === undefined) throw fail("needs session lookup, which this DSH does not offer");
|
|
126
|
+
if (args.limit !== undefined && (typeof args.limit !== "number" || !Number.isFinite(args.limit))) throw fail("limit must be a finite number");
|
|
127
|
+
const limit = Math.max(1, Math.min(100, Math.trunc(args.limit ?? 20)));
|
|
128
|
+
// Agent.id is the existing public tool-execution identity; arguments cannot supply it.
|
|
129
|
+
const owner = typeof exec.agent?.id === "string" && exec.agent.id ? exec.agent.id : undefined;
|
|
130
|
+
let filter = filters(args), set, offset = 0, setId;
|
|
131
|
+
if (args.cursor !== undefined) {
|
|
132
|
+
({ id: setId, offset } = decode(args.cursor));
|
|
133
|
+
set = sets.get(setId);
|
|
134
|
+
if (!set) throw fail("cursor expired, was evicted, or belongs to another plugin/host; start a new query without cursor");
|
|
135
|
+
if (!owner || owner !== set.owner) throw fail("cursor belongs to a different calling session");
|
|
136
|
+
if (offset >= set.rows.length) throw fail("invalid cursor offset");
|
|
137
|
+
if (filter.titleQuery !== undefined && filter.titleQuery !== set.filter.titleQuery) throw fail("cursor titleQuery does not match its original query");
|
|
138
|
+
filter = { ...set.filter, ...(filter.workspaceId !== undefined || filter.workspacePath !== undefined ? { workspaceId: filter.workspaceId, workspacePath: filter.workspacePath } : {}) };
|
|
139
|
+
}
|
|
140
|
+
const selected = await workspace(filter, exec.signal);
|
|
141
|
+
if (set && (selected?.id !== set.workspaceId || selected?.path !== set.workspacePath)) throw fail("cursor workspace does not match its original query");
|
|
142
|
+
const records = await query.listSessions(exec.signal);
|
|
143
|
+
exec.signal?.throwIfAborted();
|
|
144
|
+
sortByActivity.prune(records);
|
|
145
|
+
let candidates = records.filter(record => ordinary(record) && (!selected || selected.ids.has(String(record.header.id))));
|
|
146
|
+
const byId = new Map(candidates.map(record => [String(record.header.id), record]));
|
|
147
|
+
if (set) {
|
|
148
|
+
// Recheck the entire frozen range, not just the requested slice. Never silently skip.
|
|
149
|
+
if (set.rows.some(row => !byId.has(row.id))) {
|
|
150
|
+
remove(setId);
|
|
151
|
+
throw fail("result set contains a session no longer visible or in this workspace; start a new query");
|
|
152
|
+
}
|
|
153
|
+
} else {
|
|
154
|
+
if ((owner || filter.titleQuery !== undefined) && candidates.length > MAX_SET_IDS) throw fail(`candidate set exceeds ${MAX_SET_IDS} sessions; narrow the workspace filter`);
|
|
155
|
+
const titles = filter.titleQuery === undefined ? undefined : await matchingTitles(query, candidates, filter.titleQuery, exec.signal);
|
|
156
|
+
if (titles) candidates = candidates.filter(record => titles.has(String(record.header.id)));
|
|
157
|
+
const ordered = await sortByActivity(candidates, exec.signal, records);
|
|
158
|
+
const rows = ordered.map(record => ({ id: String(record.header.id), ...(titles ? { title: titles.get(String(record.header.id)) } : {}) }));
|
|
159
|
+
set = { owner, filter: { titleQuery: filter.titleQuery, workspaceId: selected?.id }, rows, workspaceId: selected?.id, workspacePath: selected?.path, createdAt: Date.now() };
|
|
160
|
+
set.expiresAt = set.createdAt + TTL;
|
|
161
|
+
set.bytes = Buffer.byteLength(JSON.stringify(set));
|
|
162
|
+
if (owner && rows.length > limit && set.bytes > MAX_BYTES) throw fail("result set exceeds the 8 MiB metadata budget; narrow the query");
|
|
163
|
+
}
|
|
164
|
+
const page = set.rows.slice(offset, offset + limit);
|
|
165
|
+
let communication;
|
|
166
|
+
try {
|
|
167
|
+
communication = await permissions.statuses(trustedSource(exec), page.map(row => row.id));
|
|
168
|
+
} catch {
|
|
169
|
+
communication = page.map(() => unknownCommunication());
|
|
170
|
+
}
|
|
171
|
+
exec.signal?.throwIfAborted();
|
|
172
|
+
const listed = [];
|
|
173
|
+
for (const [pageIndex, row] of page.entries()) {
|
|
174
|
+
exec.signal?.throwIfAborted();
|
|
175
|
+
const header = byId.get(row.id).header;
|
|
176
|
+
let title = row.title;
|
|
177
|
+
if (set.filter.titleQuery === undefined) try {
|
|
178
|
+
// Legacy no-query listing keeps best-effort titles and reads only this page.
|
|
179
|
+
title = (await inSlot(() => query.readTitle(row.id, exec.signal), exec.signal))?.title;
|
|
180
|
+
} catch {}
|
|
181
|
+
exec.signal?.throwIfAborted();
|
|
182
|
+
listed.push({ sessionId: row.id, communication: communication[pageIndex],
|
|
183
|
+
...title === undefined || title === "" ? {} : { title: String(title) },
|
|
184
|
+
...header.cwd === undefined ? {} : { cwd: String(header.cwd) },
|
|
185
|
+
state: sessionState(ctx, row.id), ...row.id === owner ? { current: true } : {},
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
exec.signal?.throwIfAborted();
|
|
189
|
+
if (disposed) throw fail("plugin was unloaded; start a new query");
|
|
190
|
+
const hasMore = offset + listed.length < set.rows.length;
|
|
191
|
+
if (setId || (hasMore && owner)) {
|
|
192
|
+
if (!equalSources(currentSources, services())) { clear(); throw fail("host services changed; start a new query"); }
|
|
193
|
+
if (Date.now() >= set.expiresAt) { if (setId) remove(setId); throw fail("cursor expired while reading; start a new query"); }
|
|
194
|
+
if (!setId) {
|
|
195
|
+
sweep();
|
|
196
|
+
while (sets.size >= MAX_SETS || count + set.rows.length > MAX_IDS || bytes + set.bytes > MAX_BYTES) remove(sets.keys().next().value);
|
|
197
|
+
setId = randomUUID(); count += set.rows.length; bytes += set.bytes;
|
|
198
|
+
} else {
|
|
199
|
+
// An overlapping call may have evicted this set while this page was read.
|
|
200
|
+
if (sets.get(setId) !== set) throw fail("cursor was evicted while reading; start a new query");
|
|
201
|
+
sets.delete(setId);
|
|
202
|
+
}
|
|
203
|
+
sets.set(setId, set);
|
|
204
|
+
scheduleExpiry();
|
|
205
|
+
}
|
|
206
|
+
return { sessions: listed, total: records.length, matchedTotal: set.rows.length,
|
|
207
|
+
...(setId ? { resultSetCreatedAt: set.createdAt, resultSetExpiresAt: set.expiresAt,
|
|
208
|
+
...(hasMore ? { nextCursor: token(setId, offset + listed.length) } : {}) } : {}),
|
|
209
|
+
...(hasMore && !owner ? { paginationUnavailable: "A calling Agent identity is required to continue; run list_sessions through the host tool runtime." } : {}),
|
|
210
|
+
...(selected ? { workspaceId: selected.id, workspacePath: selected.path } : {}),
|
|
211
|
+
};
|
|
212
|
+
};
|
|
213
|
+
}
|
package/locale/en.json
ADDED
package/locale/zh.json
ADDED
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ryuu-64/dsh-session-tools",
|
|
3
|
-
"version": "0.8.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.8.1",
|
|
4
|
+
"description": "Delegate tasks across DeepSeek Harness sessions. Create conversations, assign tasks, and bring results back to the current chat.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"scripts": {
|
|
@@ -12,6 +12,8 @@
|
|
|
12
12
|
"deepseek-harness",
|
|
13
13
|
"dsh",
|
|
14
14
|
"dsh-plugin",
|
|
15
|
+
"cross-session",
|
|
16
|
+
"task-delegation",
|
|
15
17
|
"sessions",
|
|
16
18
|
"tools"
|
|
17
19
|
],
|
|
@@ -19,11 +21,16 @@
|
|
|
19
21
|
"type": "git",
|
|
20
22
|
"url": "git+https://github.com/Ryuu-64/dsh-session-tools.git"
|
|
21
23
|
},
|
|
24
|
+
"homepage": "https://github.com/Ryuu-64/dsh-session-tools#readme",
|
|
25
|
+
"bugs": {
|
|
26
|
+
"url": "https://github.com/Ryuu-64/dsh-session-tools/issues"
|
|
27
|
+
},
|
|
22
28
|
"main": "lib/index.js",
|
|
23
29
|
"exports": {
|
|
24
30
|
".": "./lib/index.js",
|
|
25
31
|
"./client": "./lib/client.js",
|
|
26
|
-
"./package.json": "./package.json"
|
|
32
|
+
"./package.json": "./package.json",
|
|
33
|
+
"./locale/*.json": "./locale/*.json"
|
|
27
34
|
},
|
|
28
35
|
"dsh": {
|
|
29
36
|
"bundle": {
|
|
@@ -46,27 +53,29 @@
|
|
|
46
53
|
},
|
|
47
54
|
"peerDependencies": {
|
|
48
55
|
"@deepseek-ai/cordis": "^4.0.2",
|
|
49
|
-
"@deepseek-ai/dsh-agent": "0.2.0-rc.2",
|
|
50
|
-
"@deepseek-ai/dsh-agent-default-model": "0.2.0-rc.2",
|
|
51
|
-
"@deepseek-ai/dsh-brand": "0.2.0-rc.2",
|
|
52
|
-
"@deepseek-ai/dsh-llm": "0.2.0-rc.2",
|
|
53
|
-
"@deepseek-ai/dsh-session-title": "0.2.0-rc.2",
|
|
54
|
-
"@deepseek-ai/dsh-tools": "0.2.0-rc.2",
|
|
55
|
-
"@deepseek-ai/dsh-workspace": "0.2.0-rc.2",
|
|
56
|
+
"@deepseek-ai/dsh-agent": "0.2.0-rc.2 || 0.2.2-rc.3",
|
|
57
|
+
"@deepseek-ai/dsh-agent-default-model": "0.2.0-rc.2 || 0.2.2-rc.3",
|
|
58
|
+
"@deepseek-ai/dsh-brand": "0.2.0-rc.2 || 0.2.2-rc.3",
|
|
59
|
+
"@deepseek-ai/dsh-llm": "0.2.0-rc.2 || 0.2.2-rc.3",
|
|
60
|
+
"@deepseek-ai/dsh-session-title": "0.2.0-rc.2 || 0.2.2-rc.3",
|
|
61
|
+
"@deepseek-ai/dsh-tools": "0.2.0-rc.2 || 0.2.2-rc.3",
|
|
62
|
+
"@deepseek-ai/dsh-workspace": "0.2.0-rc.2 || 0.2.2-rc.3",
|
|
56
63
|
"@deepseek-ai/schemastery": "^3.18.2",
|
|
57
64
|
"react": ">=18 <20",
|
|
58
|
-
"@deepseek-ai/dsh-storage-domain": "0.2.0-rc.2",
|
|
59
|
-
"@deepseek-ai/dsh-typert-protocol": "0.2.0-rc.2",
|
|
60
|
-
"@deepseek-ai/dsh-client-ui-primitives": "0.2.0-rc.2",
|
|
61
|
-
"@deepseek-ai/dsh-client-ui-sidebar-right": "0.1.7-alpha.1 || 0.1.7-alpha.2 || 0.1.7-rc.1 || 0.1.7-rc.2 || 0.2.0-rc.1 || 0.2.0-rc.2"
|
|
65
|
+
"@deepseek-ai/dsh-storage-domain": "0.2.0-rc.2 || 0.2.2-rc.3",
|
|
66
|
+
"@deepseek-ai/dsh-typert-protocol": "0.2.0-rc.2 || 0.2.2-rc.3",
|
|
67
|
+
"@deepseek-ai/dsh-client-ui-primitives": "0.2.0-rc.2 || 0.2.2-rc.3",
|
|
68
|
+
"@deepseek-ai/dsh-client-ui-sidebar-right": "0.1.7-alpha.1 || 0.1.7-alpha.2 || 0.1.7-rc.1 || 0.1.7-rc.2 || 0.2.0-rc.1 || 0.2.0-rc.2 || 0.2.2-rc.3"
|
|
62
69
|
},
|
|
63
70
|
"files": [
|
|
64
71
|
"lib",
|
|
72
|
+
"locale/*.json",
|
|
65
73
|
"cordis.patch.yml",
|
|
66
74
|
"README.md"
|
|
67
75
|
],
|
|
68
76
|
"engines": {
|
|
69
|
-
"node": "^22.19.0 || ^24.0.0"
|
|
77
|
+
"node": "^22.19.0 || ^24.0.0",
|
|
78
|
+
"dsh": "0.2.0-rc.2 || 0.2.2-rc.3"
|
|
70
79
|
},
|
|
71
80
|
"devDependencies": {
|
|
72
81
|
"@deepseek-ai/cordis": "4.0.4",
|
|
@@ -80,7 +89,9 @@
|
|
|
80
89
|
"@deepseek-ai/dsh-brand": "0.2.0-rc.2",
|
|
81
90
|
"@deepseek-ai/dsh-client-store": "0.2.0-rc.2",
|
|
82
91
|
"@deepseek-ai/dsh-client-ui-layout": "0.2.0-rc.2",
|
|
92
|
+
"@deepseek-ai/dsh-client-ui-primitives": "0.2.0-rc.2",
|
|
83
93
|
"@deepseek-ai/dsh-client-ui-renderer": "0.2.0-rc.2",
|
|
94
|
+
"@deepseek-ai/dsh-client-ui-sidebar-right": "0.2.0-rc.2",
|
|
84
95
|
"@deepseek-ai/dsh-client-ui-slots": "0.2.0-rc.2",
|
|
85
96
|
"@deepseek-ai/dsh-client-ui-workspace": "0.2.0-rc.2",
|
|
86
97
|
"@deepseek-ai/dsh-llm": "0.2.0-rc.2",
|
|
@@ -98,6 +109,7 @@
|
|
|
98
109
|
"@deepseek-ai/dsh-storage-json": "0.2.0-rc.2",
|
|
99
110
|
"@deepseek-ai/dsh-system-prompt": "0.2.0-rc.2",
|
|
100
111
|
"@deepseek-ai/dsh-tools": "0.2.0-rc.2",
|
|
112
|
+
"@deepseek-ai/dsh-typert-protocol": "0.2.0-rc.2",
|
|
101
113
|
"@deepseek-ai/dsh-user-approval": "0.2.0-rc.2",
|
|
102
114
|
"@deepseek-ai/dsh-workspace": "0.2.0-rc.2",
|
|
103
115
|
"@deepseek-ai/schemastery": "3.18.4",
|