@lukstei/dsh-session-handoff 0.0.0-stage → 0.2.2-rc.5.10
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 -0
- package/README.md +208 -2
- package/cordis.patch.yml +4 -0
- package/dist/client.js +248 -0
- package/dist/index.js +454 -0
- package/package.json +117 -3
- package/src/client/acp-usage.ts +31 -0
- package/src/client/index.ts +170 -0
- package/src/client/level.ts +31 -0
- package/src/client/ring.ts +123 -0
- package/src/handoff.ts +345 -0
- package/src/index.ts +340 -0
- package/src/launch.ts +28 -0
package/src/index.ts
ADDED
|
@@ -0,0 +1,340 @@
|
|
|
1
|
+
import { mkdir, writeFile } from "node:fs/promises";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import type { Context } from "@deepseek-ai/cordis";
|
|
4
|
+
import type { Agent, ModelSelection } from "@deepseek-ai/dsh-agent";
|
|
5
|
+
import type { SessionCreateValue } from "@deepseek-ai/dsh-api-session-controller";
|
|
6
|
+
import type {
|
|
7
|
+
CommandInvocation,
|
|
8
|
+
CommandResult,
|
|
9
|
+
} from "@deepseek-ai/dsh-commands";
|
|
10
|
+
import { createUserMessage } from "@deepseek-ai/dsh-llm";
|
|
11
|
+
import type {} from "@deepseek-ai/dsh-session";
|
|
12
|
+
import type { SessionId } from "@deepseek-ai/dsh-session/types";
|
|
13
|
+
import type { SessionLogSnapshot } from "@deepseek-ai/dsh-session-query";
|
|
14
|
+
import type {} from "@deepseek-ai/dsh-session-title";
|
|
15
|
+
import type {} from "@deepseek-ai/dsh-working-directory";
|
|
16
|
+
import type { Workspace } from "@deepseek-ai/dsh-workspace";
|
|
17
|
+
import { scratchDirectory } from "@lukstei/dsh-scratch-dir";
|
|
18
|
+
import {
|
|
19
|
+
handoffInstruction,
|
|
20
|
+
parentSessionId,
|
|
21
|
+
renderHandoff,
|
|
22
|
+
} from "./handoff.ts";
|
|
23
|
+
import { formatLaunchResult } from "./launch.ts";
|
|
24
|
+
|
|
25
|
+
export const name = "session-handoff";
|
|
26
|
+
export const inject = [
|
|
27
|
+
"commands",
|
|
28
|
+
"sessionQuery",
|
|
29
|
+
"workingDirectory",
|
|
30
|
+
"sessionController",
|
|
31
|
+
"sessionTitle",
|
|
32
|
+
"workspaceRegistry",
|
|
33
|
+
"llm",
|
|
34
|
+
];
|
|
35
|
+
|
|
36
|
+
const HANDOFF_FILE = "HANDOFF.md";
|
|
37
|
+
const SHORT_ID_LENGTH = 8;
|
|
38
|
+
const RESUMED_SUFFIX = " (Resumed)";
|
|
39
|
+
|
|
40
|
+
export function apply(ctx: Context): void {
|
|
41
|
+
ctx.commands.register({
|
|
42
|
+
name: "handoff",
|
|
43
|
+
description:
|
|
44
|
+
"Hand this session off to a new one (bare: pick the model from a list)",
|
|
45
|
+
// edge case: without a declared input the client sends the raw line to the
|
|
46
|
+
// model as a prompt and this handler never runs.
|
|
47
|
+
input: { hint: "[provider/model]" },
|
|
48
|
+
handler: (invocation: CommandInvocation): Promise<CommandResult> =>
|
|
49
|
+
handoff(ctx, invocation),
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Split `provider/model` at the first slash: provider ids have none, model ids may.
|
|
55
|
+
* @internal exported for the unit test only.
|
|
56
|
+
*/
|
|
57
|
+
export function parseModelArg(raw: string): ModelSelection | undefined {
|
|
58
|
+
const text = raw.trim();
|
|
59
|
+
const slash = text.indexOf("/");
|
|
60
|
+
if (slash <= 0 || slash === text.length - 1) return undefined;
|
|
61
|
+
return { provider: text.slice(0, slash), model: text.slice(slash + 1) };
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Check the route against the registered providers and the provider's own model
|
|
66
|
+
* resolution, the same check subagent routes get, so a typo fails here and not
|
|
67
|
+
* on the new session's first turn.
|
|
68
|
+
* @returns an error text, or `undefined` when the route resolves.
|
|
69
|
+
*/
|
|
70
|
+
async function modelError(
|
|
71
|
+
ctx: Context,
|
|
72
|
+
model: ModelSelection,
|
|
73
|
+
signal: AbortSignal,
|
|
74
|
+
): Promise<string | undefined> {
|
|
75
|
+
try {
|
|
76
|
+
const providers = ctx.llm.listProviders();
|
|
77
|
+
if (!providers.some((provider) => provider.id === model.provider)) {
|
|
78
|
+
const available = providers.map((provider) => provider.id).join(", ");
|
|
79
|
+
return `LLM provider '${model.provider}' is not registered; available providers: ${available || "(none)"}`;
|
|
80
|
+
}
|
|
81
|
+
await ctx.llm.resolveCallConfig(
|
|
82
|
+
{ provider: model.provider, model: model.model },
|
|
83
|
+
signal,
|
|
84
|
+
);
|
|
85
|
+
return undefined;
|
|
86
|
+
} catch (error: unknown) {
|
|
87
|
+
return error instanceof Error ? error.message : String(error);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
async function handoff(
|
|
92
|
+
ctx: Context,
|
|
93
|
+
invocation: CommandInvocation,
|
|
94
|
+
): Promise<CommandResult> {
|
|
95
|
+
const raw = invocation.rawInput.trim();
|
|
96
|
+
// edge case: no argument keeps the source's own model, the ring's path.
|
|
97
|
+
let model: ModelSelection | undefined;
|
|
98
|
+
if (raw !== "") {
|
|
99
|
+
model = parseModelArg(raw);
|
|
100
|
+
if (model === undefined) {
|
|
101
|
+
return {
|
|
102
|
+
kind: "error",
|
|
103
|
+
text: `Expected "<provider>/<model>", got "${raw}"; run bare /handoff to pick one.`,
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
const invalid = await modelError(ctx, model, invocation.signal);
|
|
107
|
+
if (invalid !== undefined) return { kind: "error", text: invalid };
|
|
108
|
+
}
|
|
109
|
+
const agent = invocation.agent;
|
|
110
|
+
const targetId = agent.session.id;
|
|
111
|
+
|
|
112
|
+
try {
|
|
113
|
+
const snapshot = await ctx.sessionQuery.readSession(targetId);
|
|
114
|
+
const rendered = renderHandoff(
|
|
115
|
+
await readChain(ctx, snapshot, invocation.signal),
|
|
116
|
+
);
|
|
117
|
+
if (rendered.turns === 0) {
|
|
118
|
+
return {
|
|
119
|
+
kind: "error",
|
|
120
|
+
text: "This session has no turns to hand off.",
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
const created = await createBeside(
|
|
124
|
+
ctx,
|
|
125
|
+
agent,
|
|
126
|
+
targetId,
|
|
127
|
+
rendered.markdown,
|
|
128
|
+
snapshot,
|
|
129
|
+
invocation.signal,
|
|
130
|
+
model,
|
|
131
|
+
);
|
|
132
|
+
return {
|
|
133
|
+
kind: "success",
|
|
134
|
+
text: formatLaunchResult(created.sessionId),
|
|
135
|
+
};
|
|
136
|
+
} catch (error: unknown) {
|
|
137
|
+
return {
|
|
138
|
+
kind: "error",
|
|
139
|
+
text: `Handoff failed: ${error instanceof Error ? error.message : String(error)}`,
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* The handed-off session preceded by every ancestor it was itself handed off from,
|
|
146
|
+
* oldest first, so the brief keeps what earlier briefs summarized.
|
|
147
|
+
*/
|
|
148
|
+
async function readChain(
|
|
149
|
+
ctx: Context,
|
|
150
|
+
snapshot: SessionLogSnapshot,
|
|
151
|
+
signal: AbortSignal,
|
|
152
|
+
): Promise<SessionLogSnapshot[]> {
|
|
153
|
+
const chain = [snapshot];
|
|
154
|
+
const seen = new Set([String(snapshot.session.id)]);
|
|
155
|
+
let parent = parentSessionId(snapshot);
|
|
156
|
+
if (parent === undefined) return chain;
|
|
157
|
+
// `readSession` has no typed not-found error, so deleted parents are found by id.
|
|
158
|
+
const known = new Set(
|
|
159
|
+
(await ctx.sessionQuery.listSessions(signal)).map((session) =>
|
|
160
|
+
String(session.header.id),
|
|
161
|
+
),
|
|
162
|
+
);
|
|
163
|
+
while (parent !== undefined && !seen.has(parent) && known.has(parent)) {
|
|
164
|
+
seen.add(parent);
|
|
165
|
+
// Safe: the id was just matched against the listed session ids.
|
|
166
|
+
const ancestor = await ctx.sessionQuery.readSession(parent as SessionId);
|
|
167
|
+
chain.unshift(ancestor);
|
|
168
|
+
parent = parentSessionId(ancestor);
|
|
169
|
+
}
|
|
170
|
+
return chain;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Write the brief into the agent's own scratch directory and queue the follow-up
|
|
175
|
+
* turn that tells it to read that file.
|
|
176
|
+
* @param ctx - Host context.
|
|
177
|
+
* @param agent - Agent that receives the brief and the follow-up.
|
|
178
|
+
* @param targetId - Durable id of the handed-off session.
|
|
179
|
+
* @param markdown - Rendered brief.
|
|
180
|
+
* @returns absolute path of the written brief.
|
|
181
|
+
*/
|
|
182
|
+
async function deliver(
|
|
183
|
+
ctx: Context,
|
|
184
|
+
agent: Agent,
|
|
185
|
+
targetId: string,
|
|
186
|
+
markdown: string,
|
|
187
|
+
): Promise<string> {
|
|
188
|
+
const dir = scratchDirectory(
|
|
189
|
+
ctx.workingDirectory.get(agent.session),
|
|
190
|
+
String(agent.session.id),
|
|
191
|
+
);
|
|
192
|
+
if (dir === undefined) {
|
|
193
|
+
throw new Error(
|
|
194
|
+
`Unsafe session id "${String(agent.session.id)}"; no handoff written.`,
|
|
195
|
+
);
|
|
196
|
+
}
|
|
197
|
+
await mkdir(dir, { recursive: true });
|
|
198
|
+
const file = path.join(dir, HANDOFF_FILE);
|
|
199
|
+
await writeFile(file, markdown, "utf8");
|
|
200
|
+
agent.followup(
|
|
201
|
+
createUserMessage({
|
|
202
|
+
content: [
|
|
203
|
+
{
|
|
204
|
+
type: "text",
|
|
205
|
+
text: handoffInstruction(targetId, file),
|
|
206
|
+
},
|
|
207
|
+
],
|
|
208
|
+
source: { kind: "user" },
|
|
209
|
+
}),
|
|
210
|
+
);
|
|
211
|
+
return file;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
function workspaceOf(
|
|
215
|
+
ctx: Context,
|
|
216
|
+
sessionId: SessionId,
|
|
217
|
+
): Workspace | undefined {
|
|
218
|
+
for (const workspace of ctx.workspaceRegistry.list()) {
|
|
219
|
+
if (workspace.sessionIds.includes(sessionId)) return workspace;
|
|
220
|
+
}
|
|
221
|
+
return undefined;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
function kanbanLinkArgs(snapshot: SessionLogSnapshot): string[] {
|
|
225
|
+
const all: string[] = [];
|
|
226
|
+
for (const event of snapshot.events) {
|
|
227
|
+
if (event.type !== "command/run" || event.data.name !== "kanban") continue;
|
|
228
|
+
const args = event.data.args;
|
|
229
|
+
if (args === undefined) continue;
|
|
230
|
+
try {
|
|
231
|
+
const parsed: unknown = JSON.parse(args);
|
|
232
|
+
if (
|
|
233
|
+
typeof parsed === "object" &&
|
|
234
|
+
parsed !== null &&
|
|
235
|
+
"action" in parsed &&
|
|
236
|
+
parsed.action === "add-link"
|
|
237
|
+
)
|
|
238
|
+
all.push(args);
|
|
239
|
+
} catch {
|
|
240
|
+
// edge case: non-JSON kanban args carry no link
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
return all;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
function shortId(id: string): string {
|
|
247
|
+
return id.slice(0, SHORT_ID_LENGTH);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Create the resumed session beside this one: same workspace, same preset, the
|
|
252
|
+
* source's model or the given one, the brief in its own scratch directory, and one follow-up turn that
|
|
253
|
+
* tells it to read that brief.
|
|
254
|
+
* @param ctx - Host context.
|
|
255
|
+
* @param source - Session the command runs in.
|
|
256
|
+
* @param targetId - Durable id of the handed-off session.
|
|
257
|
+
* @param markdown - Rendered brief.
|
|
258
|
+
* @param snapshot - Source session log, the origin of the kanban links to copy.
|
|
259
|
+
* @param signal - Cancellation signal of the invoking command.
|
|
260
|
+
* @param model - Model the new session starts on; the source's own when absent.
|
|
261
|
+
* @returns the created session identity.
|
|
262
|
+
*/
|
|
263
|
+
async function createBeside(
|
|
264
|
+
ctx: Context,
|
|
265
|
+
source: Agent,
|
|
266
|
+
targetId: string,
|
|
267
|
+
markdown: string,
|
|
268
|
+
snapshot: SessionLogSnapshot,
|
|
269
|
+
signal: AbortSignal,
|
|
270
|
+
model: ModelSelection | undefined,
|
|
271
|
+
): Promise<SessionCreateValue> {
|
|
272
|
+
const selection = model === undefined ? source.model.current() : model;
|
|
273
|
+
const agentPreset = source.session.header.agentPreset;
|
|
274
|
+
// edge case: a session created by cwd alone is attached to no workspace, so it
|
|
275
|
+
// would stay out of the sidebar tree until the next Host start.
|
|
276
|
+
const workspace = workspaceOf(ctx, source.session.id);
|
|
277
|
+
const created = await ctx.sessionController.create(
|
|
278
|
+
workspace === undefined
|
|
279
|
+
? { cwd: ctx.workingDirectory.get(source.session), agentPreset }
|
|
280
|
+
: { workspaceId: workspace.id, agentPreset },
|
|
281
|
+
);
|
|
282
|
+
const resolved = await ctx.sessionController.resolveAgent(created.sessionId);
|
|
283
|
+
if ("error" in resolved) throw new Error(resolved.error.message);
|
|
284
|
+
|
|
285
|
+
const agent = resolved.agent;
|
|
286
|
+
// edge case: the fresh agent would otherwise start on the deployment default,
|
|
287
|
+
// so the caller's selection is recorded before its first turn.
|
|
288
|
+
if (selection !== undefined) agent.model.select(selection);
|
|
289
|
+
|
|
290
|
+
// edge case: left to itself the session is titled from the first message it
|
|
291
|
+
// receives, which is the handoff instruction, so every handoff reads "Handoff
|
|
292
|
+
// brief"; a user title pins the derived one against automatic generation.
|
|
293
|
+
const sourceTitle = ctx.sessionTitle.get(source.session);
|
|
294
|
+
ctx.sessionTitle.rename(
|
|
295
|
+
agent.session,
|
|
296
|
+
sourceTitle === undefined
|
|
297
|
+
? `Handoff ${shortId(targetId)}`
|
|
298
|
+
: sourceTitle.title,
|
|
299
|
+
);
|
|
300
|
+
if (
|
|
301
|
+
sourceTitle !== undefined &&
|
|
302
|
+
!sourceTitle.title.endsWith(RESUMED_SUFFIX)
|
|
303
|
+
) {
|
|
304
|
+
ctx.sessionTitle.rename(
|
|
305
|
+
source.session,
|
|
306
|
+
`${sourceTitle.title}${RESUMED_SUFFIX}`,
|
|
307
|
+
);
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
// edge case: a session the controller creates carries no kanban marker, so the
|
|
311
|
+
// board would never list it; this is the init kanban's own client sends.
|
|
312
|
+
await ctx.commands.execute(
|
|
313
|
+
agent,
|
|
314
|
+
`/kanban ${JSON.stringify({ action: "init", createdAt: Date.now() })}`,
|
|
315
|
+
[],
|
|
316
|
+
signal,
|
|
317
|
+
);
|
|
318
|
+
|
|
319
|
+
// Replaying the source's add-link commands in order reproduces its link set,
|
|
320
|
+
// since the kanban fold dedupes by url.
|
|
321
|
+
for (const args of kanbanLinkArgs(snapshot)) {
|
|
322
|
+
await ctx.commands.execute(agent, `/kanban ${args}`, [], signal);
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
const sourceLink = {
|
|
326
|
+
action: "add-link",
|
|
327
|
+
label: `Resumed from ${targetId}`,
|
|
328
|
+
url: `dsh://sessions/${targetId}`,
|
|
329
|
+
priority: 0,
|
|
330
|
+
};
|
|
331
|
+
await ctx.commands.execute(
|
|
332
|
+
agent,
|
|
333
|
+
`/kanban ${JSON.stringify(sourceLink)}`,
|
|
334
|
+
[],
|
|
335
|
+
signal,
|
|
336
|
+
);
|
|
337
|
+
|
|
338
|
+
await deliver(ctx, agent, targetId, markdown);
|
|
339
|
+
return created;
|
|
340
|
+
}
|
package/src/launch.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { SessionEvent, SessionId } from "@deepseek-ai/dsh-session/types";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* First line of the launch result. A slash command has no structured return
|
|
5
|
+
* channel, so the Host encodes the new Session id here and the client reads it
|
|
6
|
+
* back out of the durable `command/done` record.
|
|
7
|
+
*/
|
|
8
|
+
export const LAUNCH_PREFIX = "Handoff launched: ";
|
|
9
|
+
|
|
10
|
+
export function formatLaunchResult(sessionId: SessionId): string {
|
|
11
|
+
return `${LAUNCH_PREFIX}${String(sessionId)}`;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* The launched Session id carried by one durable event, or `undefined` when the
|
|
16
|
+
* event is not a successful handoff launch.
|
|
17
|
+
*/
|
|
18
|
+
export function launchedSessionId(event: SessionEvent): SessionId | undefined {
|
|
19
|
+
if (event.type !== "command/done") return undefined;
|
|
20
|
+
if (event.data.kind !== "success") return undefined;
|
|
21
|
+
const { text } = event.data;
|
|
22
|
+
if (typeof text !== "string" || !text.startsWith(LAUNCH_PREFIX)) {
|
|
23
|
+
return undefined;
|
|
24
|
+
}
|
|
25
|
+
// edge case: the id is a host-generated uuid that round-tripped through the
|
|
26
|
+
// result text, so the brand is restored rather than validated.
|
|
27
|
+
return text.slice(LAUNCH_PREFIX.length) as SessionId;
|
|
28
|
+
}
|