@lukstei/dsh-session-handoff 0.2.2-rc.5.3 → 0.2.2-rc.5.4
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/README.md +12 -5
- package/dist/client.js +124 -1
- package/package.json +27 -5
- package/src/client/acp-usage.ts +31 -0
- package/src/client/index.ts +51 -1
- package/src/client/level.ts +31 -0
- package/src/client/ring.ts +85 -0
- package/src/index.ts +35 -1
package/README.md
CHANGED
|
@@ -65,13 +65,19 @@ Server-side and client-side entries.
|
|
|
65
65
|
| `sessionTitle` | Required | @deepseek-ai/dsh-session-title | Plugin stays pending; never applies. |
|
|
66
66
|
| `workspaceRegistry` | Required | @deepseek-ai/dsh-workspace | Plugin stays pending; never applies. |
|
|
67
67
|
|
|
68
|
-
**Client-side** (`inject`: `sessions`, `splitView`;
|
|
68
|
+
**Client-side** (`inject`: `sessions`, `splitView`, `slots`; all required):
|
|
69
69
|
|
|
70
70
|
| Plugin / service | Required? | Provided by | Effect without it |
|
|
71
71
|
|---|---|---|---|
|
|
72
72
|
| `sessions` | Required | @deepseek-ai/dsh-api-session-controller | Plugin stays pending; never applies. |
|
|
73
73
|
| `splitView` | Required | [`@lukstei/dsh-client-split-view`](../dsh-client-split-view/README.md) (also in `dsh.client.inject`) | Plugin stays pending; never applies. The client entry (launch into a pane) does not activate; the host `/handoff` command still works. |
|
|
74
74
|
|
|
75
|
+
| `slots` | Required | @deepseek-ai/dsh-client-ui-renderer | Plugin stays pending; never applies. |
|
|
76
|
+
|
|
77
|
+
### Handoff ring
|
|
78
|
+
|
|
79
|
+
A small ring beside the send button marks a session worth handing off: yellow at 70k prompt tokens, red at 100k. After 5 idle minutes the prompt cache is cold and the bars drop to 40k and 70k. Both bars are also capped at 60% and 80% of the model's context window. For ACP agent sessions, which never report usage to DSH, it reads the agent's context usage from `@zaimokuza/dsh-acp-adapter` once per finished turn. The adapter is an optional peer dependency: without it the ring only covers regular DSH sessions. Display only; run `/handoff` yourself. Thresholds live in `src/client/level.ts`.
|
|
80
|
+
|
|
75
81
|
Code import (no mount): [`@lukstei/dsh-scratch-dir`](../dsh-scratch-dir/README.md) (`scratchDirectory`).
|
|
76
82
|
|
|
77
83
|
Used by: nothing.
|
|
@@ -80,7 +86,7 @@ Used by: nothing.
|
|
|
80
86
|
|
|
81
87
|
| Input | Brief describes | Brief lands in | Then |
|
|
82
88
|
|---|---|---|---|
|
|
83
|
-
| `/handoff` | this session | scratch directory of a newly created session | New session opens as a split pane. |
|
|
89
|
+
| `/handoff` | this session | scratch directory of a newly created session | New session opens as a split pane; the source is marked done in Kanban and its pane is closed. |
|
|
84
90
|
| `/handoff <id-or-prefix>` | that session | `<cwd>/.agents/scratch/<current-session-id>/HANDOFF.md` | Current agent is told to read it. |
|
|
85
91
|
|
|
86
92
|
Id resolution: exact id first, then unique prefix over `sessionQuery.listSessions()`. Unknown ids return an error listing the 5 most recent sessions; ambiguous prefixes list candidates. Passing the current session's id is an error. Any non-id text, including `launch`, is treated as an id. A session with no turns returns an error. All I/O failures return `{ kind: "error" }`.
|
|
@@ -92,8 +98,9 @@ Id resolution: exact id first, then unique prefix over `sessionQuery.listSession
|
|
|
92
98
|
3. Replays the current model selection onto the new agent.
|
|
93
99
|
4. Titles it `<source title> - Handoff` (or `Handoff <first 8 id chars>` when untitled). A user title pins it against auto-generated titles.
|
|
94
100
|
5. Runs `/kanban {"action":"init","createdAt":…}` against the new agent so it appears on the Kanban board. No `prompt` is sent, since that would seed the composer draft with the follow-up text.
|
|
95
|
-
6.
|
|
96
|
-
7.
|
|
101
|
+
6. Copies the source session's kanban links by replaying its `add-link` commands against the new agent.
|
|
102
|
+
7. Writes `HANDOFF.md` into the new session's scratch directory and queues the read-the-brief follow-up.
|
|
103
|
+
8. Returns `Handoff launched: <id>`.
|
|
97
104
|
|
|
98
105
|
The client opens the pane. Without the client bundle the session is created but not opened.
|
|
99
106
|
|
|
@@ -127,7 +134,7 @@ Files:
|
|
|
127
134
|
Details:
|
|
128
135
|
|
|
129
136
|
- The command declares `input: { hint: "[session-id]" }`. Without a declared input the client sends the raw line to the model as a prompt and the handler never runs.
|
|
130
|
-
- A slash command has no structured return channel to the client, so the Host encodes the new id in the command result text, which is persisted in the `command/done` event. The client subscribes to every open session's event source and calls `ctx.splitView.toggleSession()` when it sees a successful `command/done` starting with `Handoff launched: `.
|
|
137
|
+
- A slash command has no structured return channel to the client, so the Host encodes the new id in the command result text, which is persisted in the `command/done` event. The client subscribes to every open session's event source and calls `ctx.splitView.toggleSession()` when it sees a successful `command/done` starting with `Handoff launched: `. It then sends `/kanban {"action":"done"}` to the source session and calls `ctx.splitView.removeSession()` on it, as the Kanban Done button does. A source never initialised in Kanban ignores the done action but still loses its pane.
|
|
131
138
|
- The pane is added through the split store, not `uiWorkspace.openSession()`. The split view patches that method into a read-only overlay without its own composer, so prompts would keep landing in the pane underneath.
|
|
132
139
|
- The client reacts only to `append` changes. A `replace` replays the whole window on first open and after reconnect gaps and would reopen panes the user closed; the split view restores its panes from local storage.
|
|
133
140
|
- Kanban membership is a session-log fact: the board lists only sessions whose `kanban` projection is initialized, which happens when `/kanban` runs in that session. Hence the explicit `init`.
|
package/dist/client.js
CHANGED
|
@@ -4,6 +4,7 @@ window.__ModuleLoader__.load({
|
|
|
4
4
|
var module = { exports: {} };
|
|
5
5
|
var exports = module.exports;
|
|
6
6
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
7
|
+
let react = require("react");
|
|
7
8
|
/**
|
|
8
9
|
* The launched Session id carried by one durable event, or `undefined` when the
|
|
9
10
|
* event is not a successful handoff launch.
|
|
@@ -15,8 +16,100 @@ window.__ModuleLoader__.load({
|
|
|
15
16
|
if (typeof text !== "string" || !text.startsWith("Handoff launched: ")) return;
|
|
16
17
|
return text.slice(18);
|
|
17
18
|
}
|
|
19
|
+
// src/client/acp-usage.ts
|
|
20
|
+
let reader;
|
|
21
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
22
|
+
function setAcpUsageReader(next) {
|
|
23
|
+
reader = next;
|
|
24
|
+
for (const listener of listeners) listener();
|
|
25
|
+
}
|
|
26
|
+
/** `useSyncExternalStore` seat, so a ring mounted before the adapter re-reads once it arrives. */
|
|
27
|
+
function subscribeAcpUsageReader(listener) {
|
|
28
|
+
listeners.add(listener);
|
|
29
|
+
return () => {
|
|
30
|
+
listeners.delete(listener);
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
function currentAcpUsageReader() {
|
|
34
|
+
return reader;
|
|
35
|
+
}
|
|
36
|
+
// src/client/level.ts
|
|
37
|
+
/** Anthropic prompt-cache TTL: past this idle gap the next turn re-writes the whole context. */
|
|
38
|
+
const CACHE_TTL_MS = 3e5;
|
|
39
|
+
const WARM = {
|
|
40
|
+
soft: 7e4,
|
|
41
|
+
strong: 1e5
|
|
42
|
+
};
|
|
43
|
+
const COLD = {
|
|
44
|
+
soft: 4e4,
|
|
45
|
+
strong: 7e4
|
|
46
|
+
};
|
|
47
|
+
const SOFT_WINDOW_SHARE = .6;
|
|
48
|
+
const STRONG_WINDOW_SHARE = .8;
|
|
49
|
+
function handoffLevel(pressureTokens, contextWindow, cold) {
|
|
50
|
+
if (pressureTokens === void 0) return "none";
|
|
51
|
+
const bar = cold ? COLD : WARM;
|
|
52
|
+
const soft = contextWindow === void 0 ? bar.soft : Math.min(bar.soft, SOFT_WINDOW_SHARE * contextWindow);
|
|
53
|
+
if (pressureTokens >= (contextWindow === void 0 ? bar.strong : Math.min(bar.strong, STRONG_WINDOW_SHARE * contextWindow))) return "strong";
|
|
54
|
+
return pressureTokens >= soft ? "soft" : "none";
|
|
55
|
+
}
|
|
56
|
+
// src/client/ring.ts
|
|
57
|
+
const COLOR = {
|
|
58
|
+
soft: "#d9a400",
|
|
59
|
+
strong: "#d62839"
|
|
60
|
+
};
|
|
61
|
+
/** Small ring beside the send button once the session is big enough that `/handoff` pays off. */
|
|
62
|
+
function HandoffRing({ sessionId, useProjection, useSession }) {
|
|
63
|
+
const pressure = useProjection("contextPressure");
|
|
64
|
+
const running = useSession((session) => session.running);
|
|
65
|
+
const [acp, setAcp] = (0, react.useState)(void 0);
|
|
66
|
+
const [cold, setCold] = (0, react.useState)(false);
|
|
67
|
+
const readUsage = (0, react.useSyncExternalStore)(subscribeAcpUsageReader, currentAcpUsageReader);
|
|
68
|
+
(0, react.useEffect)(() => {
|
|
69
|
+
if (running || readUsage === void 0) return;
|
|
70
|
+
let live = true;
|
|
71
|
+
readUsage(sessionId).then((usage) => {
|
|
72
|
+
if (live) setAcp(usage);
|
|
73
|
+
});
|
|
74
|
+
return () => {
|
|
75
|
+
live = false;
|
|
76
|
+
};
|
|
77
|
+
}, [
|
|
78
|
+
sessionId,
|
|
79
|
+
running,
|
|
80
|
+
readUsage
|
|
81
|
+
]);
|
|
82
|
+
const tokens = pressure !== void 0 && pressure.pressureTokens !== void 0 ? pressure.pressureTokens : acp?.used;
|
|
83
|
+
const window = pressure !== void 0 && pressure.contextWindow !== void 0 ? pressure.contextWindow : acp?.size;
|
|
84
|
+
(0, react.useEffect)(() => {
|
|
85
|
+
setCold(false);
|
|
86
|
+
if (running) return;
|
|
87
|
+
const timer = setTimeout(() => setCold(true), CACHE_TTL_MS);
|
|
88
|
+
return () => clearTimeout(timer);
|
|
89
|
+
}, [tokens, running]);
|
|
90
|
+
const level = handoffLevel(tokens, window, cold);
|
|
91
|
+
if (level === "none" || tokens === void 0) return null;
|
|
92
|
+
const k = Math.round(tokens / 1e3);
|
|
93
|
+
return (0, react.createElement)("span", {
|
|
94
|
+
role: "img",
|
|
95
|
+
title: `Context ${k}k tokens: ${cold ? "cache is cold, a fresh session is cheaper now" : "consider a fresh session"} (/handoff)`,
|
|
96
|
+
"aria-label": `Handoff ${level === "strong" ? "recommended" : "suggested"}`,
|
|
97
|
+
style: {
|
|
98
|
+
width: 14,
|
|
99
|
+
height: 14,
|
|
100
|
+
boxSizing: "border-box",
|
|
101
|
+
borderRadius: "50%",
|
|
102
|
+
border: `2px solid ${COLOR[level]}`,
|
|
103
|
+
flex: "none"
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
}
|
|
18
107
|
// src/client/index.ts
|
|
19
|
-
const inject = [
|
|
108
|
+
const inject = [
|
|
109
|
+
"sessions",
|
|
110
|
+
"splitView",
|
|
111
|
+
"slots"
|
|
112
|
+
];
|
|
20
113
|
/**
|
|
21
114
|
* Open every session the Host reports as launched as a pane of its own. A slash
|
|
22
115
|
* command cannot reach the client, so the Host publishes the new id in the
|
|
@@ -31,6 +124,15 @@ window.__ModuleLoader__.load({
|
|
|
31
124
|
function apply(ctx) {
|
|
32
125
|
const sessions = ctx.sessions;
|
|
33
126
|
const off = /* @__PURE__ */ new Map();
|
|
127
|
+
const finish = async (id) => {
|
|
128
|
+
await sessions.using(id, { source: "splitView" }, async (reference) => {
|
|
129
|
+
await reference.binding.session.command(`/kanban ${JSON.stringify({
|
|
130
|
+
action: "done",
|
|
131
|
+
doneAt: Date.now()
|
|
132
|
+
})}`);
|
|
133
|
+
});
|
|
134
|
+
ctx.splitView.removeSession(id);
|
|
135
|
+
};
|
|
34
136
|
const watch = (id) => {
|
|
35
137
|
const binding = sessions.binding(id);
|
|
36
138
|
if (binding === void 0) return;
|
|
@@ -42,6 +144,7 @@ window.__ModuleLoader__.load({
|
|
|
42
144
|
const launched = launchedSessionId(entry.event);
|
|
43
145
|
if (launched === void 0) continue;
|
|
44
146
|
ctx.splitView.toggleSession(launched);
|
|
147
|
+
finish(id);
|
|
45
148
|
return;
|
|
46
149
|
}
|
|
47
150
|
});
|
|
@@ -58,6 +161,26 @@ window.__ModuleLoader__.load({
|
|
|
58
161
|
off.delete(id);
|
|
59
162
|
}
|
|
60
163
|
};
|
|
164
|
+
ctx.effect(() => ctx.slots.inject("conversation.input.right", () => ctx.slots.register({
|
|
165
|
+
name: "conversation.input.right",
|
|
166
|
+
id: "session-handoff.ring",
|
|
167
|
+
order: 90,
|
|
168
|
+
inject: () => ({})
|
|
169
|
+
}, HandoffRing)), "session-handoff: handoff ring");
|
|
170
|
+
ctx.inject(["remote", "remote.dshAcp"], (acpCtx) => {
|
|
171
|
+
const remote = acpCtx.remote.dshAcp;
|
|
172
|
+
const reader = async (id) => {
|
|
173
|
+
const result = await remote.agentSessionSnapshot(id);
|
|
174
|
+
if (!result.ok) {
|
|
175
|
+
console.warn("session-handoff: ACP usage unavailable", result);
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
const usage = result.value.contextUsage;
|
|
179
|
+
return usage === null ? void 0 : usage;
|
|
180
|
+
};
|
|
181
|
+
setAcpUsageReader(reader);
|
|
182
|
+
return () => setAcpUsageReader(void 0);
|
|
183
|
+
});
|
|
61
184
|
const dispose = sessions.list.subscribe(sync);
|
|
62
185
|
sync();
|
|
63
186
|
ctx.effect(() => () => {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lukstei/dsh-session-handoff",
|
|
3
|
-
"version": "0.2.2-rc.5.
|
|
3
|
+
"version": "0.2.2-rc.5.4",
|
|
4
4
|
"description": "Session handoff brief command for DSH",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/index.ts",
|
|
@@ -32,41 +32,63 @@
|
|
|
32
32
|
"immediately": true,
|
|
33
33
|
"inject": [
|
|
34
34
|
"@deepseek-ai/dsh-api-session-controller",
|
|
35
|
-
"@lukstei/dsh-client-split-view"
|
|
35
|
+
"@lukstei/dsh-client-split-view",
|
|
36
|
+
"@deepseek-ai/dsh-client-ui-conversation",
|
|
37
|
+
"@deepseek-ai/dsh-client-ui-renderer"
|
|
36
38
|
]
|
|
37
39
|
}
|
|
38
40
|
},
|
|
39
41
|
"dependencies": {
|
|
40
|
-
"@lukstei/dsh-scratch-dir": "0.2.2-rc.5.
|
|
42
|
+
"@lukstei/dsh-scratch-dir": "0.2.2-rc.5.4"
|
|
41
43
|
},
|
|
42
44
|
"peerDependencies": {
|
|
43
45
|
"@deepseek-ai/cordis": "*",
|
|
44
46
|
"@deepseek-ai/dsh-agent": "*",
|
|
45
47
|
"@deepseek-ai/dsh-api-session-controller": "*",
|
|
48
|
+
"@deepseek-ai/dsh-client-ui-conversation": "*",
|
|
49
|
+
"@deepseek-ai/dsh-client-ui-renderer": "*",
|
|
50
|
+
"@deepseek-ai/dsh-client-ui-session": "*",
|
|
51
|
+
"@deepseek-ai/dsh-client-ui-slots": "*",
|
|
46
52
|
"@deepseek-ai/dsh-commands": "*",
|
|
47
53
|
"@deepseek-ai/dsh-llm": "*",
|
|
48
54
|
"@deepseek-ai/dsh-session": "*",
|
|
49
55
|
"@deepseek-ai/dsh-session-query": "*",
|
|
50
56
|
"@deepseek-ai/dsh-session-title": "*",
|
|
57
|
+
"@deepseek-ai/dsh-token-meter": "*",
|
|
51
58
|
"@deepseek-ai/dsh-working-directory": "*",
|
|
52
|
-
"@deepseek-ai/dsh-workspace": "*"
|
|
59
|
+
"@deepseek-ai/dsh-workspace": "*",
|
|
60
|
+
"@zaimokuza/dsh-acp-adapter": "*",
|
|
61
|
+
"react": "*"
|
|
53
62
|
},
|
|
54
63
|
"devDependencies": {
|
|
55
64
|
"@deepseek-ai/cordis": "4.1.0",
|
|
56
65
|
"@deepseek-ai/dsh-agent": "0.2.2-rc.5",
|
|
57
66
|
"@deepseek-ai/dsh-api-session-controller": "0.2.2-rc.5",
|
|
67
|
+
"@deepseek-ai/dsh-client-ui-conversation": "0.2.2-rc.5",
|
|
68
|
+
"@deepseek-ai/dsh-client-ui-renderer": "0.2.2-rc.5",
|
|
69
|
+
"@deepseek-ai/dsh-client-ui-session": "0.2.2-rc.5",
|
|
70
|
+
"@deepseek-ai/dsh-client-ui-slots": "0.2.2-rc.5",
|
|
58
71
|
"@deepseek-ai/dsh-commands": "0.2.2-rc.5",
|
|
59
72
|
"@deepseek-ai/dsh-llm": "0.2.2-rc.5",
|
|
60
73
|
"@deepseek-ai/dsh-session": "0.2.2-rc.5",
|
|
61
74
|
"@deepseek-ai/dsh-session-query": "0.2.2-rc.5",
|
|
62
75
|
"@deepseek-ai/dsh-session-title": "0.2.2-rc.5",
|
|
76
|
+
"@deepseek-ai/dsh-token-meter": "0.2.2-rc.5",
|
|
63
77
|
"@deepseek-ai/dsh-working-directory": "0.2.2-rc.5",
|
|
64
78
|
"@deepseek-ai/dsh-workspace": "0.2.2-rc.5",
|
|
65
|
-
"@lukstei/dsh-client-split-view": "0.2.2-rc.5.
|
|
79
|
+
"@lukstei/dsh-client-split-view": "0.2.2-rc.5.4",
|
|
66
80
|
"@types/node": "^26.0.0",
|
|
81
|
+
"@types/react": "^19.3.0",
|
|
82
|
+
"@zaimokuza/dsh-acp-adapter": "0.2.2-rc.5.2",
|
|
83
|
+
"react": "^19.3.1",
|
|
67
84
|
"tsdown": "^0.23.0",
|
|
68
85
|
"typescript": "^7.0.2"
|
|
69
86
|
},
|
|
87
|
+
"peerDependenciesMeta": {
|
|
88
|
+
"@zaimokuza/dsh-acp-adapter": {
|
|
89
|
+
"optional": true
|
|
90
|
+
}
|
|
91
|
+
},
|
|
70
92
|
"license": "MIT",
|
|
71
93
|
"repository": {
|
|
72
94
|
"type": "git",
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/** Context occupancy an ACP agent reported, as the adapter exposes it. */
|
|
2
|
+
export interface AcpUsage {
|
|
3
|
+
readonly used: number;
|
|
4
|
+
readonly size: number;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export type AcpUsageReader = (
|
|
8
|
+
sessionId: string,
|
|
9
|
+
) => Promise<AcpUsage | undefined>;
|
|
10
|
+
|
|
11
|
+
// smell: module-level seat; the optional ACP scope in `index.ts` fills it, so the ring needs no
|
|
12
|
+
// hard dependency on the adapter.
|
|
13
|
+
let reader: AcpUsageReader | undefined;
|
|
14
|
+
const listeners = new Set<() => void>();
|
|
15
|
+
|
|
16
|
+
export function setAcpUsageReader(next: AcpUsageReader | undefined): void {
|
|
17
|
+
reader = next;
|
|
18
|
+
for (const listener of listeners) listener();
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** `useSyncExternalStore` seat, so a ring mounted before the adapter re-reads once it arrives. */
|
|
22
|
+
export function subscribeAcpUsageReader(listener: () => void): () => void {
|
|
23
|
+
listeners.add(listener);
|
|
24
|
+
return () => {
|
|
25
|
+
listeners.delete(listener);
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function currentAcpUsageReader(): AcpUsageReader | undefined {
|
|
30
|
+
return reader;
|
|
31
|
+
}
|
package/src/client/index.ts
CHANGED
|
@@ -1,10 +1,15 @@
|
|
|
1
1
|
import type { Context } from "@deepseek-ai/cordis";
|
|
2
2
|
import type { ISessions } from "@deepseek-ai/dsh-api-session-controller/client";
|
|
3
|
+
import type {} from "@deepseek-ai/dsh-client-ui-conversation/client";
|
|
4
|
+
import type {} from "@deepseek-ai/dsh-client-ui-renderer/client";
|
|
3
5
|
import type { SessionId } from "@deepseek-ai/dsh-session/types";
|
|
4
6
|
import type {} from "@lukstei/dsh-client-split-view/client-contract";
|
|
7
|
+
import type {} from "@zaimokuza/dsh-acp-adapter/remote";
|
|
5
8
|
import { launchedSessionId } from "../launch.ts";
|
|
9
|
+
import { type AcpUsageReader, setAcpUsageReader } from "./acp-usage.ts";
|
|
10
|
+
import { HandoffRing } from "./ring.ts";
|
|
6
11
|
|
|
7
|
-
export const inject = ["sessions", "splitView"];
|
|
12
|
+
export const inject = ["sessions", "splitView", "slots"];
|
|
8
13
|
|
|
9
14
|
/**
|
|
10
15
|
* Open every session the Host reports as launched as a pane of its own. A slash
|
|
@@ -23,6 +28,16 @@ export function apply(ctx: Context): void {
|
|
|
23
28
|
const sessions = ctx.sessions as unknown as ISessions;
|
|
24
29
|
const off = new Map<SessionId, () => void>();
|
|
25
30
|
|
|
31
|
+
// Same effect as the Kanban Done button: record done, then close the pane.
|
|
32
|
+
const finish = async (id: SessionId): Promise<void> => {
|
|
33
|
+
await sessions.using(id, { source: "splitView" }, async (reference) => {
|
|
34
|
+
await reference.binding.session.command(
|
|
35
|
+
`/kanban ${JSON.stringify({ action: "done", doneAt: Date.now() })}`,
|
|
36
|
+
);
|
|
37
|
+
});
|
|
38
|
+
ctx.splitView.removeSession(id);
|
|
39
|
+
};
|
|
40
|
+
|
|
26
41
|
const watch = (id: SessionId): void => {
|
|
27
42
|
const binding = sessions.binding(id);
|
|
28
43
|
if (binding === undefined) return;
|
|
@@ -37,6 +52,7 @@ export function apply(ctx: Context): void {
|
|
|
37
52
|
const launched = launchedSessionId(entry.event);
|
|
38
53
|
if (launched === undefined) continue;
|
|
39
54
|
ctx.splitView.toggleSession(launched);
|
|
55
|
+
void finish(id);
|
|
40
56
|
return;
|
|
41
57
|
}
|
|
42
58
|
});
|
|
@@ -55,6 +71,40 @@ export function apply(ctx: Context): void {
|
|
|
55
71
|
}
|
|
56
72
|
};
|
|
57
73
|
|
|
74
|
+
ctx.effect(
|
|
75
|
+
() =>
|
|
76
|
+
ctx.slots.inject("conversation.input.right", () =>
|
|
77
|
+
ctx.slots.register(
|
|
78
|
+
{
|
|
79
|
+
name: "conversation.input.right",
|
|
80
|
+
id: "session-handoff.ring",
|
|
81
|
+
order: 90,
|
|
82
|
+
inject: () => ({}),
|
|
83
|
+
},
|
|
84
|
+
HandoffRing,
|
|
85
|
+
),
|
|
86
|
+
),
|
|
87
|
+
"session-handoff: handoff ring",
|
|
88
|
+
);
|
|
89
|
+
|
|
90
|
+
// Optional: the ACP adapter's remote exists only while that plugin is mounted. Outside this
|
|
91
|
+
// scope the reader stays unset and the ring falls back to the DSH `contextPressure`.
|
|
92
|
+
// `remote` is requested alongside, as the adapter's own client does for its namespace.
|
|
93
|
+
ctx.inject(["remote", "remote.dshAcp"], (acpCtx) => {
|
|
94
|
+
const remote = acpCtx.remote.dshAcp;
|
|
95
|
+
const reader: AcpUsageReader = async (id) => {
|
|
96
|
+
const result = await remote.agentSessionSnapshot(id);
|
|
97
|
+
if (!result.ok) {
|
|
98
|
+
console.warn("session-handoff: ACP usage unavailable", result);
|
|
99
|
+
return undefined;
|
|
100
|
+
}
|
|
101
|
+
const usage = result.value.contextUsage;
|
|
102
|
+
return usage === null ? undefined : usage;
|
|
103
|
+
};
|
|
104
|
+
setAcpUsageReader(reader);
|
|
105
|
+
return () => setAcpUsageReader(undefined);
|
|
106
|
+
});
|
|
107
|
+
|
|
58
108
|
const dispose = sessions.list.subscribe(sync);
|
|
59
109
|
sync();
|
|
60
110
|
ctx.effect(
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export type HandoffLevel = "none" | "soft" | "strong";
|
|
2
|
+
|
|
3
|
+
/** Anthropic prompt-cache TTL: past this idle gap the next turn re-writes the whole context. */
|
|
4
|
+
export const CACHE_TTL_MS = 5 * 60_000;
|
|
5
|
+
|
|
6
|
+
// Absolute prompt sizes, tuned by cost: a fresh session starts near 18k tokens, so a handoff
|
|
7
|
+
// pays back sooner the more context a turn re-sends. A cold cache lowers both bars.
|
|
8
|
+
const WARM = { soft: 70_000, strong: 100_000 };
|
|
9
|
+
const COLD = { soft: 40_000, strong: 70_000 };
|
|
10
|
+
// The window still caps the bars, so small-window models warn before compaction does.
|
|
11
|
+
const SOFT_WINDOW_SHARE = 0.6;
|
|
12
|
+
const STRONG_WINDOW_SHARE = 0.8;
|
|
13
|
+
|
|
14
|
+
export function handoffLevel(
|
|
15
|
+
pressureTokens: number | undefined,
|
|
16
|
+
contextWindow: number | undefined,
|
|
17
|
+
cold: boolean,
|
|
18
|
+
): HandoffLevel {
|
|
19
|
+
if (pressureTokens === undefined) return "none";
|
|
20
|
+
const bar = cold ? COLD : WARM;
|
|
21
|
+
const soft =
|
|
22
|
+
contextWindow === undefined
|
|
23
|
+
? bar.soft
|
|
24
|
+
: Math.min(bar.soft, SOFT_WINDOW_SHARE * contextWindow);
|
|
25
|
+
const strong =
|
|
26
|
+
contextWindow === undefined
|
|
27
|
+
? bar.strong
|
|
28
|
+
: Math.min(bar.strong, STRONG_WINDOW_SHARE * contextWindow);
|
|
29
|
+
if (pressureTokens >= strong) return "strong";
|
|
30
|
+
return pressureTokens >= soft ? "soft" : "none";
|
|
31
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import type {} from "@deepseek-ai/dsh-client-ui-session/client";
|
|
2
|
+
import type { SessionStandardProps } from "@deepseek-ai/dsh-client-ui-slots";
|
|
3
|
+
import type {} from "@deepseek-ai/dsh-token-meter/client";
|
|
4
|
+
import {
|
|
5
|
+
createElement,
|
|
6
|
+
useEffect,
|
|
7
|
+
useState,
|
|
8
|
+
useSyncExternalStore,
|
|
9
|
+
} from "react";
|
|
10
|
+
import {
|
|
11
|
+
type AcpUsage,
|
|
12
|
+
currentAcpUsageReader,
|
|
13
|
+
subscribeAcpUsageReader,
|
|
14
|
+
} from "./acp-usage.ts";
|
|
15
|
+
import { CACHE_TTL_MS, handoffLevel } from "./level.ts";
|
|
16
|
+
|
|
17
|
+
const COLOR = { soft: "#d9a400", strong: "#d62839" } as const;
|
|
18
|
+
|
|
19
|
+
/** Small ring beside the send button once the session is big enough that `/handoff` pays off. */
|
|
20
|
+
export function HandoffRing({
|
|
21
|
+
sessionId,
|
|
22
|
+
useProjection,
|
|
23
|
+
useSession,
|
|
24
|
+
}: Pick<SessionStandardProps, "sessionId" | "useProjection" | "useSession">) {
|
|
25
|
+
const pressure = useProjection("contextPressure");
|
|
26
|
+
const running = useSession((session) => session.running);
|
|
27
|
+
const [acp, setAcp] = useState<AcpUsage | undefined>(undefined);
|
|
28
|
+
const [cold, setCold] = useState(false);
|
|
29
|
+
|
|
30
|
+
// ACP agents report occupancy to the adapter, never to DSH's `contextPressure`; read it
|
|
31
|
+
// when a turn settles. The reader is unset (so this is a no-op) without the adapter.
|
|
32
|
+
const readUsage = useSyncExternalStore(
|
|
33
|
+
subscribeAcpUsageReader,
|
|
34
|
+
currentAcpUsageReader,
|
|
35
|
+
);
|
|
36
|
+
useEffect(() => {
|
|
37
|
+
if (running || readUsage === undefined) return;
|
|
38
|
+
let live = true;
|
|
39
|
+
void readUsage(sessionId).then((usage) => {
|
|
40
|
+
if (live) setAcp(usage);
|
|
41
|
+
});
|
|
42
|
+
return () => {
|
|
43
|
+
live = false;
|
|
44
|
+
};
|
|
45
|
+
}, [sessionId, running, readUsage]);
|
|
46
|
+
|
|
47
|
+
const tokens =
|
|
48
|
+
pressure !== undefined && pressure.pressureTokens !== undefined
|
|
49
|
+
? pressure.pressureTokens
|
|
50
|
+
: acp?.used;
|
|
51
|
+
const window =
|
|
52
|
+
pressure !== undefined && pressure.contextWindow !== undefined
|
|
53
|
+
? pressure.contextWindow
|
|
54
|
+
: acp?.size;
|
|
55
|
+
|
|
56
|
+
// A new usage sample or a running turn means the cache was just touched; once idle for
|
|
57
|
+
// the TTL it is cold. Mount counts as warm because the last request time is unknown.
|
|
58
|
+
// biome-ignore lint/correctness/useExhaustiveDependencies: `tokens` is the restart trigger
|
|
59
|
+
useEffect(() => {
|
|
60
|
+
setCold(false);
|
|
61
|
+
if (running) return;
|
|
62
|
+
const timer = setTimeout(() => setCold(true), CACHE_TTL_MS);
|
|
63
|
+
return () => clearTimeout(timer);
|
|
64
|
+
}, [tokens, running]);
|
|
65
|
+
|
|
66
|
+
const level = handoffLevel(tokens, window, cold);
|
|
67
|
+
if (level === "none" || tokens === undefined) return null;
|
|
68
|
+
const k = Math.round(tokens / 1000);
|
|
69
|
+
const hint = cold
|
|
70
|
+
? "cache is cold, a fresh session is cheaper now"
|
|
71
|
+
: "consider a fresh session";
|
|
72
|
+
return createElement("span", {
|
|
73
|
+
role: "img",
|
|
74
|
+
title: `Context ${k}k tokens: ${hint} (/handoff)`,
|
|
75
|
+
"aria-label": `Handoff ${level === "strong" ? "recommended" : "suggested"}`,
|
|
76
|
+
style: {
|
|
77
|
+
width: 14,
|
|
78
|
+
height: 14,
|
|
79
|
+
boxSizing: "border-box",
|
|
80
|
+
borderRadius: "50%",
|
|
81
|
+
border: `2px solid ${COLOR[level]}`,
|
|
82
|
+
flex: "none",
|
|
83
|
+
},
|
|
84
|
+
});
|
|
85
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -10,7 +10,10 @@ import type {
|
|
|
10
10
|
import { createUserMessage } from "@deepseek-ai/dsh-llm";
|
|
11
11
|
import type {} from "@deepseek-ai/dsh-session";
|
|
12
12
|
import type { SessionId } from "@deepseek-ai/dsh-session/types";
|
|
13
|
-
import type {
|
|
13
|
+
import type {
|
|
14
|
+
SessionLogSnapshot,
|
|
15
|
+
SessionRecord,
|
|
16
|
+
} from "@deepseek-ai/dsh-session-query";
|
|
14
17
|
import type {} from "@deepseek-ai/dsh-session-title";
|
|
15
18
|
import type {} from "@deepseek-ai/dsh-working-directory";
|
|
16
19
|
import type { Workspace } from "@deepseek-ai/dsh-workspace";
|
|
@@ -116,6 +119,7 @@ async function handoff(
|
|
|
116
119
|
agent,
|
|
117
120
|
targetId,
|
|
118
121
|
rendered.markdown,
|
|
122
|
+
snapshot,
|
|
119
123
|
invocation.signal,
|
|
120
124
|
);
|
|
121
125
|
return {
|
|
@@ -188,6 +192,28 @@ function workspaceOf(
|
|
|
188
192
|
return undefined;
|
|
189
193
|
}
|
|
190
194
|
|
|
195
|
+
function kanbanLinkArgs(snapshot: SessionLogSnapshot): string[] {
|
|
196
|
+
const all: string[] = [];
|
|
197
|
+
for (const event of snapshot.events) {
|
|
198
|
+
if (event.type !== "command/run" || event.data.name !== "kanban") continue;
|
|
199
|
+
const args = event.data.args;
|
|
200
|
+
if (args === undefined) continue;
|
|
201
|
+
try {
|
|
202
|
+
const parsed: unknown = JSON.parse(args);
|
|
203
|
+
if (
|
|
204
|
+
typeof parsed === "object" &&
|
|
205
|
+
parsed !== null &&
|
|
206
|
+
"action" in parsed &&
|
|
207
|
+
parsed.action === "add-link"
|
|
208
|
+
)
|
|
209
|
+
all.push(args);
|
|
210
|
+
} catch {
|
|
211
|
+
// edge case: non-JSON kanban args carry no link
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
return all;
|
|
215
|
+
}
|
|
216
|
+
|
|
191
217
|
function shortId(id: string): string {
|
|
192
218
|
return id.slice(0, SHORT_ID_LENGTH);
|
|
193
219
|
}
|
|
@@ -200,6 +226,7 @@ function shortId(id: string): string {
|
|
|
200
226
|
* @param source - Session the command runs in.
|
|
201
227
|
* @param targetId - Durable id of the handed-off session.
|
|
202
228
|
* @param markdown - Rendered brief.
|
|
229
|
+
* @param snapshot - Source session log, the origin of the kanban links to copy.
|
|
203
230
|
* @param signal - Cancellation signal of the invoking command.
|
|
204
231
|
* @returns the created session identity.
|
|
205
232
|
*/
|
|
@@ -208,6 +235,7 @@ async function createBeside(
|
|
|
208
235
|
source: Agent,
|
|
209
236
|
targetId: string,
|
|
210
237
|
markdown: string,
|
|
238
|
+
snapshot: SessionLogSnapshot,
|
|
211
239
|
signal: AbortSignal,
|
|
212
240
|
): Promise<SessionCreateValue> {
|
|
213
241
|
const selection = source.model.current();
|
|
@@ -248,6 +276,12 @@ async function createBeside(
|
|
|
248
276
|
signal,
|
|
249
277
|
);
|
|
250
278
|
|
|
279
|
+
// Replaying the source's add-link commands in order reproduces its link set,
|
|
280
|
+
// since the kanban fold dedupes by url.
|
|
281
|
+
for (const args of kanbanLinkArgs(snapshot)) {
|
|
282
|
+
await ctx.commands.execute(agent, `/kanban ${args}`, [], signal);
|
|
283
|
+
}
|
|
284
|
+
|
|
251
285
|
await deliver(ctx, agent, targetId, markdown);
|
|
252
286
|
return created;
|
|
253
287
|
}
|