@lukstei/dsh-session-handoff 0.2.2-rc.5.5 → 0.2.2-rc.5.7
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/dist/index.js +350 -0
- package/package.json +8 -5
package/dist/index.js
ADDED
|
@@ -0,0 +1,350 @@
|
|
|
1
|
+
import { mkdir, writeFile } from "node:fs/promises";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { createUserMessage } from "@deepseek-ai/dsh-llm";
|
|
4
|
+
import { scratchDirectory } from "@lukstei/dsh-scratch-dir";
|
|
5
|
+
// src/handoff.ts
|
|
6
|
+
/**
|
|
7
|
+
* Bounds on quoted text. The Goal and older turns quote a head and a tail of the user's
|
|
8
|
+
* ask; the last RECENT_TURNS use a larger budget so the receiving agent still reads the
|
|
9
|
+
* actual words.
|
|
10
|
+
*/
|
|
11
|
+
const ASK_HEAD = 500;
|
|
12
|
+
const ASK_TAIL = 300;
|
|
13
|
+
const OUTCOME_LIMIT = 240;
|
|
14
|
+
const FAILURE_LIMIT = 200;
|
|
15
|
+
const ARTIFACT_LIMIT = 12;
|
|
16
|
+
const RECENT_TURNS = 5;
|
|
17
|
+
const RECENT_HEAD = 1200;
|
|
18
|
+
const RECENT_TAIL = 400;
|
|
19
|
+
/** Tool argument keys that carry a durable file path. */
|
|
20
|
+
const PATH_KEYS = [
|
|
21
|
+
"path",
|
|
22
|
+
"file_path",
|
|
23
|
+
"filePath",
|
|
24
|
+
"notebook_path"
|
|
25
|
+
];
|
|
26
|
+
function renderHandoff(snapshot) {
|
|
27
|
+
const turns = [];
|
|
28
|
+
const byId = /* @__PURE__ */ new Map();
|
|
29
|
+
const failures = [];
|
|
30
|
+
const artifacts = [];
|
|
31
|
+
const callNames = /* @__PURE__ */ new Map();
|
|
32
|
+
const seenArtifacts = /* @__PURE__ */ new Set();
|
|
33
|
+
let open;
|
|
34
|
+
let goal = "";
|
|
35
|
+
for (const event of snapshot.events) switch (event.type) {
|
|
36
|
+
case "turn/start": {
|
|
37
|
+
const opened = {
|
|
38
|
+
turn: event.data.turn,
|
|
39
|
+
ask: "",
|
|
40
|
+
outcome: "",
|
|
41
|
+
reason: void 0
|
|
42
|
+
};
|
|
43
|
+
turns.push(opened);
|
|
44
|
+
byId.set(event.data.turn, opened);
|
|
45
|
+
open = opened;
|
|
46
|
+
break;
|
|
47
|
+
}
|
|
48
|
+
case "user/message": {
|
|
49
|
+
if (event.data.source.kind !== "user") break;
|
|
50
|
+
const text = blockText(event.data.content);
|
|
51
|
+
if (text === "") break;
|
|
52
|
+
if (goal === "") goal = elide(flatten(text), ASK_HEAD, ASK_TAIL);
|
|
53
|
+
if (open !== void 0 && open.ask === "") open.ask = text;
|
|
54
|
+
break;
|
|
55
|
+
}
|
|
56
|
+
case "assistant/message": {
|
|
57
|
+
const arc = byId.get(event.data.turn);
|
|
58
|
+
if (arc === void 0) break;
|
|
59
|
+
arc.outcome = blockText(event.data.message.content);
|
|
60
|
+
break;
|
|
61
|
+
}
|
|
62
|
+
case "turn/end": {
|
|
63
|
+
const arc = byId.get(event.data.turn);
|
|
64
|
+
if (arc === void 0) break;
|
|
65
|
+
arc.reason = event.data.reason;
|
|
66
|
+
break;
|
|
67
|
+
}
|
|
68
|
+
case "tool/call": {
|
|
69
|
+
const arc = byId.get(event.data.turn);
|
|
70
|
+
callNames.set(event.data.callId, event.data.name);
|
|
71
|
+
if (arc === void 0) break;
|
|
72
|
+
collectArtifacts(event.data.arguments, artifacts, seenArtifacts);
|
|
73
|
+
break;
|
|
74
|
+
}
|
|
75
|
+
case "tool/result": {
|
|
76
|
+
const arc = byId.get(event.data.turn);
|
|
77
|
+
if (event.data.message.isError !== true || arc === void 0) break;
|
|
78
|
+
const callId = event.data.message.toolCallId;
|
|
79
|
+
const name = callNames.get(callId);
|
|
80
|
+
failures.push(`- **T${arc.turn}** tool \`${name === void 0 ? callId : name}\`: ${clip(errorReason(event.data.error), FAILURE_LIMIT)}`);
|
|
81
|
+
break;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
const id = String(snapshot.session.id);
|
|
85
|
+
const cwd = snapshot.session.cwd;
|
|
86
|
+
return {
|
|
87
|
+
markdown: `${[
|
|
88
|
+
`# Handoff — ${id}`,
|
|
89
|
+
"",
|
|
90
|
+
`- session: \`${id}\``,
|
|
91
|
+
`- cwd: \`${cwd === void 0 ? "unknown" : cwd}\``,
|
|
92
|
+
`- created: ${new Date(snapshot.session.createdAt).toISOString()}`,
|
|
93
|
+
`- events: ${snapshot.events.length}`,
|
|
94
|
+
`- turns: ${turns.length}`,
|
|
95
|
+
"",
|
|
96
|
+
"## Goal",
|
|
97
|
+
"",
|
|
98
|
+
goal === "" ? "none" : goal,
|
|
99
|
+
"",
|
|
100
|
+
"## Turns",
|
|
101
|
+
"",
|
|
102
|
+
turns.length === 0 ? "none" : turns.map((turn, index) => turnBlock(turn, index >= turns.length - RECENT_TURNS)).join("\n\n"),
|
|
103
|
+
"",
|
|
104
|
+
"## Failures",
|
|
105
|
+
"",
|
|
106
|
+
failures.length === 0 ? "none" : failures.join("\n"),
|
|
107
|
+
"",
|
|
108
|
+
"## Artifacts",
|
|
109
|
+
"",
|
|
110
|
+
artifacts.length === 0 ? "none" : artifacts.map((file) => `- \`${file}\``).join("\n")
|
|
111
|
+
].join("\n")}\n`,
|
|
112
|
+
turns: turns.length
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
function handoffInstruction(sessionId, file) {
|
|
116
|
+
return [
|
|
117
|
+
`The handoff brief for session ${sessionId} is at ${file}.`,
|
|
118
|
+
"",
|
|
119
|
+
"Read that file now, then reply with three lines and stop:",
|
|
120
|
+
"Goal: <one line>",
|
|
121
|
+
"State: <one line>",
|
|
122
|
+
"Next: <one line>",
|
|
123
|
+
"",
|
|
124
|
+
"Do not start the work until the user confirms the next action."
|
|
125
|
+
].join("\n");
|
|
126
|
+
}
|
|
127
|
+
/** Recent turns quote the ask and reply at length; older turns are flattened and short. */
|
|
128
|
+
function turnBlock(turn, recent) {
|
|
129
|
+
const kind = turn.reason === void 0 ? "unfinished" : turn.reason.kind;
|
|
130
|
+
const body = (recent ? [elide(turn.ask, RECENT_HEAD, RECENT_TAIL), elide(turn.outcome, RECENT_HEAD, RECENT_TAIL)] : [elide(flatten(turn.ask), ASK_HEAD, ASK_TAIL), clip(turn.outcome, OUTCOME_LIMIT)]).filter((part) => part !== "").join("\n\n");
|
|
131
|
+
return `### T${turn.turn} [${kind}]\n\n${body === "" ? "(no content)" : body}`;
|
|
132
|
+
}
|
|
133
|
+
function blockText(blocks) {
|
|
134
|
+
const parts = [];
|
|
135
|
+
for (const block of blocks) if (block.type === "text") parts.push(block.text);
|
|
136
|
+
return parts.join("\n");
|
|
137
|
+
}
|
|
138
|
+
/** Collapses whitespace so a quote fits on one line, then cuts it. */
|
|
139
|
+
function clip(value, limit) {
|
|
140
|
+
const flat = flatten(value);
|
|
141
|
+
if (flat.length <= limit) return flat;
|
|
142
|
+
return `${flat.slice(0, limit - 1)}…`;
|
|
143
|
+
}
|
|
144
|
+
function flatten(value) {
|
|
145
|
+
return value.replace(/\s+/g, " ").trim();
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Keeps `head` chars from the start and `tail` chars from the end, naming how much was
|
|
149
|
+
* dropped. Raw text is preserved so the Tail keeps the shape of code blocks and tables.
|
|
150
|
+
*/
|
|
151
|
+
function elide(value, head, tail) {
|
|
152
|
+
const trimmed = value.trim();
|
|
153
|
+
if (trimmed.length <= head + tail) return trimmed;
|
|
154
|
+
const omitted = trimmed.length - head - tail;
|
|
155
|
+
return `${trimmed.slice(0, head)} … [${omitted} chars omitted] … ${trimmed.slice(-tail)}`;
|
|
156
|
+
}
|
|
157
|
+
function errorReason(error) {
|
|
158
|
+
if (error === void 0) return "failed";
|
|
159
|
+
if (error.reason === void 0) return error.code;
|
|
160
|
+
return error.reason;
|
|
161
|
+
}
|
|
162
|
+
function collectArtifacts(args, into, seen) {
|
|
163
|
+
let parsed;
|
|
164
|
+
try {
|
|
165
|
+
parsed = JSON.parse(args);
|
|
166
|
+
} catch {
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) return;
|
|
170
|
+
const record = parsed;
|
|
171
|
+
for (const key of PATH_KEYS) {
|
|
172
|
+
const value = record[key];
|
|
173
|
+
if (typeof value !== "string" || value === "" || seen.has(value)) continue;
|
|
174
|
+
seen.add(value);
|
|
175
|
+
into.push(value);
|
|
176
|
+
if (into.length >= ARTIFACT_LIMIT) return;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
// src/launch.ts
|
|
180
|
+
/**
|
|
181
|
+
* First line of the launch result. A slash command has no structured return
|
|
182
|
+
* channel, so the Host encodes the new Session id here and the client reads it
|
|
183
|
+
* back out of the durable `command/done` record.
|
|
184
|
+
*/
|
|
185
|
+
const LAUNCH_PREFIX = "Handoff launched: ";
|
|
186
|
+
function formatLaunchResult(sessionId) {
|
|
187
|
+
return `${LAUNCH_PREFIX}${String(sessionId)}`;
|
|
188
|
+
}
|
|
189
|
+
// src/index.ts
|
|
190
|
+
const name = "session-handoff";
|
|
191
|
+
const inject = [
|
|
192
|
+
"commands",
|
|
193
|
+
"sessionQuery",
|
|
194
|
+
"workingDirectory",
|
|
195
|
+
"sessionController",
|
|
196
|
+
"sessionTitle",
|
|
197
|
+
"workspaceRegistry"
|
|
198
|
+
];
|
|
199
|
+
const HANDOFF_FILE = "HANDOFF.md";
|
|
200
|
+
const RECENT_LIMIT = 5;
|
|
201
|
+
const SHORT_ID_LENGTH = 8;
|
|
202
|
+
const RESUMED_SUFFIX = " (Resumed)";
|
|
203
|
+
function apply(ctx) {
|
|
204
|
+
ctx.commands.register({
|
|
205
|
+
name: "handoff",
|
|
206
|
+
description: "Hand this session off to a new one, or write the resume brief for <session-id> into this one",
|
|
207
|
+
input: { hint: "[session-id]" },
|
|
208
|
+
handler: (invocation) => handoff(ctx, invocation)
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
function resolveHandoffTarget(sessions, query) {
|
|
212
|
+
const exact = sessions.filter((session) => String(session.header.id) === query);
|
|
213
|
+
if (exact.length > 0) return exact;
|
|
214
|
+
return sessions.filter((session) => String(session.header.id).startsWith(query));
|
|
215
|
+
}
|
|
216
|
+
async function handoff(ctx, invocation) {
|
|
217
|
+
const raw = invocation.rawInput.trim();
|
|
218
|
+
const launch = raw === "";
|
|
219
|
+
const agent = invocation.agent;
|
|
220
|
+
let targetId;
|
|
221
|
+
if (launch) targetId = agent.session.id;
|
|
222
|
+
else {
|
|
223
|
+
const sessions = await ctx.sessionQuery.listSessions(invocation.signal);
|
|
224
|
+
const matches = resolveHandoffTarget(sessions, raw);
|
|
225
|
+
if (matches.length === 0) return {
|
|
226
|
+
kind: "error",
|
|
227
|
+
text: `No session matches "${raw}". Most recent sessions:\n${sessions.slice(0, RECENT_LIMIT).map((session) => `- ${String(session.header.id)}`).join("\n")}`
|
|
228
|
+
};
|
|
229
|
+
if (matches.length > 1) return {
|
|
230
|
+
kind: "error",
|
|
231
|
+
text: `"${raw}" is ambiguous:\n${matches.map((session) => `- ${String(session.header.id)}`).join("\n")}`
|
|
232
|
+
};
|
|
233
|
+
targetId = matches[0].header.id;
|
|
234
|
+
if (targetId === agent.session.id) return {
|
|
235
|
+
kind: "error",
|
|
236
|
+
text: "That is this session; there is nothing to hand off."
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
try {
|
|
240
|
+
const snapshot = await ctx.sessionQuery.readSession(targetId);
|
|
241
|
+
const rendered = renderHandoff(snapshot);
|
|
242
|
+
if (rendered.turns === 0) return {
|
|
243
|
+
kind: "error",
|
|
244
|
+
text: launch ? "This session has no turns to hand off." : `Session ${shortId(targetId)} has no turns to hand off.`
|
|
245
|
+
};
|
|
246
|
+
if (launch) return {
|
|
247
|
+
kind: "success",
|
|
248
|
+
text: formatLaunchResult((await createBeside(ctx, agent, targetId, rendered.markdown, snapshot, invocation.signal)).sessionId)
|
|
249
|
+
};
|
|
250
|
+
const file = await deliver(ctx, agent, targetId, rendered.markdown);
|
|
251
|
+
return {
|
|
252
|
+
kind: "success",
|
|
253
|
+
text: `Handoff for ${shortId(targetId)} (${rendered.turns} turns) → ${file}`
|
|
254
|
+
};
|
|
255
|
+
} catch (error) {
|
|
256
|
+
return {
|
|
257
|
+
kind: "error",
|
|
258
|
+
text: `Handoff failed: ${error instanceof Error ? error.message : String(error)}`
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Write the brief into the agent's own scratch directory and queue the follow-up
|
|
264
|
+
* turn that tells it to read that file.
|
|
265
|
+
* @param ctx - Host context.
|
|
266
|
+
* @param agent - Agent that receives the brief and the follow-up.
|
|
267
|
+
* @param targetId - Durable id of the handed-off session.
|
|
268
|
+
* @param markdown - Rendered brief.
|
|
269
|
+
* @returns absolute path of the written brief.
|
|
270
|
+
*/
|
|
271
|
+
async function deliver(ctx, agent, targetId, markdown) {
|
|
272
|
+
const dir = scratchDirectory(ctx.workingDirectory.get(agent.session), String(agent.session.id));
|
|
273
|
+
if (dir === void 0) throw new Error(`Unsafe session id "${String(agent.session.id)}"; no handoff written.`);
|
|
274
|
+
await mkdir(dir, { recursive: true });
|
|
275
|
+
const file = path.join(dir, HANDOFF_FILE);
|
|
276
|
+
await writeFile(file, markdown, "utf8");
|
|
277
|
+
agent.followup(createUserMessage({
|
|
278
|
+
content: [{
|
|
279
|
+
type: "text",
|
|
280
|
+
text: handoffInstruction(targetId, file)
|
|
281
|
+
}],
|
|
282
|
+
source: { kind: "user" }
|
|
283
|
+
}));
|
|
284
|
+
return file;
|
|
285
|
+
}
|
|
286
|
+
function workspaceOf(ctx, sessionId) {
|
|
287
|
+
for (const workspace of ctx.workspaceRegistry.list()) if (workspace.sessionIds.includes(sessionId)) return workspace;
|
|
288
|
+
}
|
|
289
|
+
function kanbanLinkArgs(snapshot) {
|
|
290
|
+
const all = [];
|
|
291
|
+
for (const event of snapshot.events) {
|
|
292
|
+
if (event.type !== "command/run" || event.data.name !== "kanban") continue;
|
|
293
|
+
const args = event.data.args;
|
|
294
|
+
if (args === void 0) continue;
|
|
295
|
+
try {
|
|
296
|
+
const parsed = JSON.parse(args);
|
|
297
|
+
if (typeof parsed === "object" && parsed !== null && "action" in parsed && parsed.action === "add-link") all.push(args);
|
|
298
|
+
} catch {}
|
|
299
|
+
}
|
|
300
|
+
return all;
|
|
301
|
+
}
|
|
302
|
+
function shortId(id) {
|
|
303
|
+
return id.slice(0, SHORT_ID_LENGTH);
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* Create the resumed session beside this one: same workspace, same preset, same
|
|
307
|
+
* model, the brief in its own scratch directory, and one follow-up turn that
|
|
308
|
+
* tells it to read that brief.
|
|
309
|
+
* @param ctx - Host context.
|
|
310
|
+
* @param source - Session the command runs in.
|
|
311
|
+
* @param targetId - Durable id of the handed-off session.
|
|
312
|
+
* @param markdown - Rendered brief.
|
|
313
|
+
* @param snapshot - Source session log, the origin of the kanban links to copy.
|
|
314
|
+
* @param signal - Cancellation signal of the invoking command.
|
|
315
|
+
* @returns the created session identity.
|
|
316
|
+
*/
|
|
317
|
+
async function createBeside(ctx, source, targetId, markdown, snapshot, signal) {
|
|
318
|
+
const selection = source.model.current();
|
|
319
|
+
const agentPreset = source.session.header.agentPreset;
|
|
320
|
+
const workspace = workspaceOf(ctx, source.session.id);
|
|
321
|
+
const created = await ctx.sessionController.create(workspace === void 0 ? {
|
|
322
|
+
cwd: ctx.workingDirectory.get(source.session),
|
|
323
|
+
agentPreset
|
|
324
|
+
} : {
|
|
325
|
+
workspaceId: workspace.id,
|
|
326
|
+
agentPreset
|
|
327
|
+
});
|
|
328
|
+
const resolved = await ctx.sessionController.resolveAgent(created.sessionId);
|
|
329
|
+
if ("error" in resolved) throw new Error(resolved.error.message);
|
|
330
|
+
const agent = resolved.agent;
|
|
331
|
+
if (selection !== void 0) agent.model.select(selection);
|
|
332
|
+
const sourceTitle = ctx.sessionTitle.get(source.session);
|
|
333
|
+
ctx.sessionTitle.rename(agent.session, sourceTitle === void 0 ? `Handoff ${shortId(targetId)}` : sourceTitle.title);
|
|
334
|
+
if (sourceTitle !== void 0 && !sourceTitle.title.endsWith(RESUMED_SUFFIX)) ctx.sessionTitle.rename(source.session, `${sourceTitle.title}${RESUMED_SUFFIX}`);
|
|
335
|
+
await ctx.commands.execute(agent, `/kanban ${JSON.stringify({
|
|
336
|
+
action: "init",
|
|
337
|
+
createdAt: Date.now()
|
|
338
|
+
})}`, [], signal);
|
|
339
|
+
for (const args of kanbanLinkArgs(snapshot)) await ctx.commands.execute(agent, `/kanban ${args}`, [], signal);
|
|
340
|
+
const sourceLink = {
|
|
341
|
+
action: "add-link",
|
|
342
|
+
label: `Resumed from ${targetId}`,
|
|
343
|
+
url: `dsh://sessions/${targetId}`,
|
|
344
|
+
priority: 0
|
|
345
|
+
};
|
|
346
|
+
await ctx.commands.execute(agent, `/kanban ${JSON.stringify(sourceLink)}`, [], signal);
|
|
347
|
+
await deliver(ctx, agent, targetId, markdown);
|
|
348
|
+
return created;
|
|
349
|
+
}
|
|
350
|
+
export { apply, inject, name, resolveHandoffTarget };
|
package/package.json
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lukstei/dsh-session-handoff",
|
|
3
|
-
"version": "0.2.2-rc.5.
|
|
3
|
+
"version": "0.2.2-rc.5.7",
|
|
4
4
|
"description": "Session handoff brief command for DSH",
|
|
5
5
|
"type": "module",
|
|
6
|
-
"main": "./
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
7
|
"types": "./src/index.ts",
|
|
8
8
|
"exports": {
|
|
9
|
-
".":
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./src/index.ts",
|
|
11
|
+
"default": "./dist/index.js"
|
|
12
|
+
},
|
|
10
13
|
"./client": "./dist/client.js",
|
|
11
14
|
"./package.json": "./package.json"
|
|
12
15
|
},
|
|
@@ -39,7 +42,7 @@
|
|
|
39
42
|
}
|
|
40
43
|
},
|
|
41
44
|
"dependencies": {
|
|
42
|
-
"@lukstei/dsh-scratch-dir": "0.2.2-rc.5.
|
|
45
|
+
"@lukstei/dsh-scratch-dir": "0.2.2-rc.5.7"
|
|
43
46
|
},
|
|
44
47
|
"peerDependencies": {
|
|
45
48
|
"@deepseek-ai/cordis": "*",
|
|
@@ -78,7 +81,7 @@
|
|
|
78
81
|
"@deepseek-ai/dsh-token-meter": "0.2.2-rc.5",
|
|
79
82
|
"@deepseek-ai/dsh-working-directory": "0.2.2-rc.5",
|
|
80
83
|
"@deepseek-ai/dsh-workspace": "0.2.2-rc.5",
|
|
81
|
-
"@lukstei/dsh-client-split-view": "0.2.2-rc.5.
|
|
84
|
+
"@lukstei/dsh-client-split-view": "0.2.2-rc.5.7",
|
|
82
85
|
"@types/node": "^26.0.0",
|
|
83
86
|
"@types/react": "^19.3.0",
|
|
84
87
|
"@zaimokuza/dsh-acp-adapter": "0.2.2-rc.5.2",
|