dsh-ws-recency 0.24.0 → 0.24.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.
Files changed (4) hide show
  1. package/README.md +6 -6
  2. package/client.js +189 -57
  3. package/index.js +1 -1
  4. package/package.json +1 -1
package/README.md CHANGED
@@ -122,22 +122,22 @@ DSH 原生的置顶只把会话顶到**它所属工作区分组的第一行**,
122
122
 
123
123
  长期会话的上下文迟早会满,被动压缩往往比"换个新会话、带上交接"更差。交接全程在对话里完成,不弹窗:
124
124
 
125
- 1. 每轮回复底部的动作行(复制 / 点赞 / 分支那一排)里点 **交接到新会话**:插件把交接指令放进输入框并尝试直接发送(发不出去会在卡片上提示你按回车);
125
+ 1. 每轮回复底部的动作行(复制 / 点赞 / 分支那一排)或会话「…」菜单 → **交接到新会话**:插件把交接指令直接交给宿主的 Session 控制器发送(`sessions.using` + `session.prompt`),不写输入框、不模拟按键;
126
126
  2. 当前会话的 AI 输出一个 ```handoff 代码块——**只写文档路径和要点,不粘贴文档内容**;
127
127
  3. 抓到代码块后,**在该轮回复下方**出现一张问询卡片,和工具向用户提问的形态一致:
128
- - 卡片样式沿用宿主问询卡:标题 + 编号选项 + 底部动作;**确认,交接到新会话**(默认选中):当前工作区开新会话,内容预填进输入框;
128
+ - 卡片样式沿用宿主问询卡:标题 + 编号选项 + 底部动作;**确认,交接到新会话**(默认选中):用宿主的 `connectWorkspace` 在当前工作区建一个空白会话并打开,再把「前言 + 正文」作为它的第一条消息发出去;
129
129
  - **不满意,重新生成一版**:让当前会话按同样的要求再写一份;
130
130
  - 底部输入框:填写调整意见(例如"多写接口约束、少写历史"),提交后按这些要求重写;
131
131
  - 「查看内容」展开只读预览,「放弃交接」收起卡片。
132
132
  4. 交接内容另存一份到 `~/.hypers-work/.wsr/handoff/<sessionId>-<时间>.md`。
133
133
 
134
- 细节想改不用在这里改——新会话的输入框里直接调更顺手。轮询最多 5 分钟自动收起。
134
+ 细节想改不用在这里改——新会话里直接说更顺手。轮询最多 5 分钟自动收起。
135
135
 
136
136
  交接块按 markdown 围栏解析:**只有独占一行的 ``` 才算结束**,正文里引用「```handoff 块」不会把内容截半(0.23.2 修)。
137
137
 
138
- 交接块用 `~~~handoff` 围栏(三个波浪线,CommonMark 与反引号等价,展示仍是代码块):正文里提到标记不会切断内容,写了一半的回复也不会被提前抓取;旧的 ```handoff 回复仍兼容。
138
+ 交接块用 `~~~handoff` 围栏(三个波浪线,CommonMark 与反引号等价,宿主自己的 markdown 解析器就用 `~{3,}` 识别围栏,展示仍是代码块):正文里提到标记不会切断内容,写了一半的回复也不会被提前抓取;旧的 ```handoff 回复仍兼容。
139
139
 
140
- 两处发送都是模拟 Enter:当前会话里先写入、等 250ms 再按回车,并按需再补两次(共 3 次);草稿会话在 14 秒内重试。始终送不出去时文本仍留在输入框,手动回车即可。
140
+ 发送走宿主 API(0.24.1 起):`ctx.sessions.using(id, …)` 保持会话可寻址,再调 `session.prompt([{type:"text",…}], "queue")`;菜单项也能对不在前台的会话发起交接。只有在 Client 没有 `sessions` 服务时才退回旧路:写入当前会话的输入框、250ms 后按回车、必要时再补两次;目标会话不在屏幕上时不写错输入框,卡片会提示按回车。
141
141
 
142
142
  ## 设置
143
143
 
@@ -175,7 +175,7 @@ dsh plugin --profile <profile> add github:chyxhtc/dsh-ws-recency
175
175
  或固定版本的预构建包:
176
176
 
177
177
  ```powershell
178
- dsh plugin --profile <profile> add https://github.com/chyxhtc/dsh-ws-recency/releases/download/v0.24.0/dsh-ws-recency-0.24.0.tgz
178
+ dsh plugin --profile <profile> add https://github.com/chyxhtc/dsh-ws-recency/releases/download/v0.24.1/dsh-ws-recency-0.24.1.tgz
179
179
  ```
180
180
 
181
181
  装完把 `dsh-ws-recency` 加进 `~/.hypers-work/profiles/<profile>/package.json` 的 `dsh.profile.bundles`,重启应用。
package/client.js CHANGED
@@ -1956,11 +1956,62 @@
1956
1956
  var handoffTries = 0;
1957
1957
  var handoffSession = null;
1958
1958
 
1959
+ /** Retention-source tag the Host books this plugin's borrows under. */
1960
+ var HANDOFF_SOURCE = "dsh-ws-recency";
1961
+
1959
1962
  /** The instruction the user sends to the current Session. */
1960
1963
  function handoffInstruction() {
1961
1964
  return t("handoff.instruction");
1962
1965
  }
1963
1966
 
1967
+ /** The Host's session service, when this Client provides it. */
1968
+ function sessionsService() {
1969
+ var ctx = data.ctx;
1970
+ if (ctx === null || ctx === undefined || typeof ctx.get !== "function") return null;
1971
+ var sessions;
1972
+ try { sessions = ctx.get("sessions"); } catch (error) { sessions = undefined; }
1973
+ return sessions === undefined ? null : sessions;
1974
+ }
1975
+
1976
+ /**
1977
+ * Send one prompt through the Host's own Session controller.
1978
+ *
1979
+ * The composer path further down is a synthetic key event: it depends on
1980
+ * the editor being mounted, focused, and on the insert and the key landing
1981
+ * in different frames. The Session controller owns a real prompt call, so
1982
+ * a plugin can retain the session, wait until it is open, and hand the text
1983
+ * over - no DOM, no key events, and it works for a session that is not the
1984
+ * one on screen (the "..." menu starts a handoff that way).
1985
+ *
1986
+ * @param sessionId - target Session.
1987
+ * @param text - prompt text, sent as one text block.
1988
+ * @param onUnsent - called when the prompt could not be dispatched.
1989
+ * @returns true when a request was dispatched (its promise may still fail).
1990
+ */
1991
+ function promptSession(sessionId, text, onUnsent) {
1992
+ var sessions = sessionsService();
1993
+ if (sessions !== null && typeof sessions.using === "function") {
1994
+ try {
1995
+ sessions.using(sessionId, { source: HANDOFF_SOURCE }, function (reference) {
1996
+ var binding = reference === null || reference === undefined ? undefined : reference.binding;
1997
+ var session = binding === null || binding === undefined ? undefined : binding.session;
1998
+ if (session === undefined || session === null || typeof session.prompt !== "function") throw new Error("session unavailable");
1999
+ return Promise.resolve(session.prompt([{ type: "text", text: text }], "queue")).then(function (result) {
2000
+ if (result !== null && result !== undefined && result.ok === false) throw new Error("the session refused the prompt");
2001
+ return result;
2002
+ });
2003
+ }).catch(function () {
2004
+ if (typeof onUnsent === "function") onUnsent();
2005
+ });
2006
+ return true;
2007
+ } catch (error) {
2008
+ /* fall through to the caller's own path */
2009
+ }
2010
+ }
2011
+ if (typeof onUnsent === "function") onUnsent();
2012
+ return false;
2013
+ }
2014
+
1964
2015
  function handoffPost(payload) {
1965
2016
  return fetch("/ws-recency/handoff", {
1966
2017
  method: "POST",
@@ -2078,9 +2129,12 @@
2078
2129
  }, attempt === 0 ? 250 : 800);
2079
2130
  }
2080
2131
 
2081
- /** Write text into the composer and send it, if the editor is reachable. */
2082
- function sendToSession(text) {
2083
- if (typeof document === "undefined" || typeof text !== "string" || text === "") return false;
2132
+ /** Fallback: write into the open composer and press Enter until it leaves. */
2133
+ function sendThroughComposer(sessionId, text) {
2134
+ if (typeof document === "undefined") return false;
2135
+ /* Never write into another conversation's composer: the open editor
2136
+ belongs to the session on screen, not to the target id. */
2137
+ if (currentConversationId() !== sessionId) return false;
2084
2138
  var editor = composerEditor();
2085
2139
  if (editor === null || editor === undefined) return false;
2086
2140
  if (!writeIntoEditor(editor, text)) return false;
@@ -2088,6 +2142,24 @@
2088
2142
  return true;
2089
2143
  }
2090
2144
 
2145
+ /**
2146
+ * Send one prompt to one Session.
2147
+ *
2148
+ * Preferred: the Host's Session controller (see promptSession). Only when
2149
+ * that seam is unavailable does it fall back to writing into the composer
2150
+ * of the session on screen and pressing Enter; a target that is not on
2151
+ * screen then just reports "press Enter" on the card instead of sending
2152
+ * the instruction into the wrong conversation.
2153
+ */
2154
+ function sendToSession(sessionId, text) {
2155
+ if (typeof text !== "string" || text === "") return false;
2156
+ var fallback = function () {
2157
+ if (sendThroughComposer(sessionId, text)) return;
2158
+ setHandoffState({ sessionId: sessionId, phase: "running", block: "", needsEnter: true });
2159
+ };
2160
+ return promptSession(sessionId, text, fallback);
2161
+ }
2162
+
2091
2163
  function stopHandoffPolling() {
2092
2164
  if (handoffTimer !== null && typeof window.clearTimeout === "function") window.clearTimeout(handoffTimer);
2093
2165
  handoffTimer = null;
@@ -2133,38 +2205,38 @@
2133
2205
  return blocks;
2134
2206
  }
2135
2207
 
2136
- /**
2137
- * Pull the handoff block out of an assistant reply.
2138
- *
2139
- * Both fence kinds are read: the prompt asks for `~~~handoff` (three tildes
2140
- * rarely appear in prose), `' + '```handoff` stays supported for older replies.
2141
- * A fence only counts when it owns its line and is closed by the same kind,
2142
- * so a note that mentions the marker in a sentence cannot cut itself short,
2143
- * and a reply that is still streaming is not captured half-written.
2144
- */
2145
- function handoffBlockOf(text) {
2146
- if (typeof text !== "string") return "";
2147
- var bodyOf = function (openedAt, openedLength, mark) {
2148
- var closer = new RegExp("^[ \\t]*" + mark + "[ \\t]*$", "m");
2149
- var rest = text.slice(openedAt + openedLength);
2150
- var closed = closer.exec(rest);
2151
- if (closed === null) return "";
2152
- return rest.slice(0, closed.index).replace(/^\r?\n/, "").trim();
2153
- };
2154
- var tagged = /^[ \t]*(```|~~~)[ \t]*handoff[ \t]*$/m.exec(text);
2155
- if (tagged !== null) return bodyOf(tagged.index, tagged[0].length, tagged[1]);
2156
- var fence = /^[ \t]*(```|~~~)[ \t]*[a-zA-Z0-9_-]*[ \t]*$/gm;
2157
- var opened;
2158
- while ((opened = fence.exec(text)) !== null) {
2159
- var candidate = bodyOf(opened.index, opened[0].length, opened[1]);
2160
- if (candidate !== "" && candidate.indexOf("#") !== -1) return candidate;
2161
- var next = text.indexOf("\n", opened.index + opened[0].length);
2162
- if (next === -1) return "";
2163
- fence.lastIndex = next + 1;
2164
- }
2165
- return "";
2166
- }
2167
-
2208
+ /**
2209
+ * Pull the handoff block out of an assistant reply.
2210
+ *
2211
+ * Both fence kinds are read: the prompt asks for `~~~handoff` (three tildes
2212
+ * rarely appear in prose), `' + '```handoff` stays supported for older replies.
2213
+ * A fence only counts when it owns its line and is closed by the same kind,
2214
+ * so a note that mentions the marker in a sentence cannot cut itself short,
2215
+ * and a reply that is still streaming is not captured half-written.
2216
+ */
2217
+ function handoffBlockOf(text) {
2218
+ if (typeof text !== "string") return "";
2219
+ var bodyOf = function (openedAt, openedLength, mark) {
2220
+ var closer = new RegExp("^[ \\t]*" + mark + "[ \\t]*$", "m");
2221
+ var rest = text.slice(openedAt + openedLength);
2222
+ var closed = closer.exec(rest);
2223
+ if (closed === null) return "";
2224
+ return rest.slice(0, closed.index).replace(/^\r?\n/, "").trim();
2225
+ };
2226
+ var tagged = /^[ \t]*(```|~~~)[ \t]*handoff[ \t]*$/m.exec(text);
2227
+ if (tagged !== null) return bodyOf(tagged.index, tagged[0].length, tagged[1]);
2228
+ var fence = /^[ \t]*(```|~~~)[ \t]*[a-zA-Z0-9_-]*[ \t]*$/gm;
2229
+ var opened;
2230
+ while ((opened = fence.exec(text)) !== null) {
2231
+ var candidate = bodyOf(opened.index, opened[0].length, opened[1]);
2232
+ if (candidate !== "" && candidate.indexOf("#") !== -1) return candidate;
2233
+ var next = text.indexOf("\n", opened.index + opened[0].length);
2234
+ if (next === -1) return "";
2235
+ fence.lastIndex = next + 1;
2236
+ }
2237
+ return "";
2238
+ }
2239
+
2168
2240
  /** Start the watch: the next reply that carries a handoff block becomes the card. */
2169
2241
  function watchHandoff(sessionId) {
2170
2242
  handoffSession = sessionId;
@@ -2211,44 +2283,88 @@
2211
2283
  /** The turn action: send the instruction and wait for the block. */
2212
2284
  function startHandoff(sessionId) {
2213
2285
  if (typeof sessionId !== "string" || sessionId === "") return;
2214
- var sent = sendToSession(handoffInstruction());
2215
- setHandoffState({ sessionId: sessionId, phase: "running", block: "", needsEnter: sent !== true });
2286
+ /* The card starts in its "working" state; if the Host refuses the send
2287
+ the fallback flips needsEnter so the card tells the user to press
2288
+ Enter instead of pretending the handoff is running. */
2289
+ setHandoffState({ sessionId: sessionId, phase: "running", block: "", needsEnter: false });
2290
+ sendToSession(sessionId, handoffInstruction());
2216
2291
  watchHandoff(sessionId);
2217
2292
  }
2218
2293
 
2219
- /** The card carries it over: open a fresh Session in the same workspace. */
2294
+ /**
2295
+ * Fallback carry: the Host opened a blank session and keeps the note as an
2296
+ * unsent draft (the pre-0.35 behaviour). Only used when the Session
2297
+ * controller seam is missing or the workspace is unknown.
2298
+ */
2299
+ function startHandoffSession(workspaceId, prompt) {
2300
+ var service = data.service;
2301
+ if (service !== null && service !== undefined && typeof service.startSession === "function") {
2302
+ try {
2303
+ service.startSession(workspaceId, { prompt: prompt, whenDraftExists: "replace" });
2304
+ resetHandoff();
2305
+ autoSendDraft(t("handoff.preamble").slice(0, 12));
2306
+ return true;
2307
+ } catch (error) {
2308
+ /* fall through to the clipboard */
2309
+ }
2310
+ }
2311
+ try {
2312
+ if (navigator !== undefined && navigator.clipboard !== undefined) navigator.clipboard.writeText(prompt);
2313
+ } catch (error) {
2314
+ /* clipboard unavailable */
2315
+ }
2316
+ resetHandoff();
2317
+ return false;
2318
+ }
2319
+
2320
+ /**
2321
+ * The card carries it over: open a fresh Session in the same workspace and
2322
+ * put the note in as its first prompt through the Host's own API.
2323
+ *
2324
+ * `connectWorkspace` creates (or reuses) the blank and returns its id
2325
+ * without navigating; `sessions.using` retains that session until it is
2326
+ * open, so the prompt is the session's real first message - nothing has to
2327
+ * be typed into a composer and no Enter is synthesised.
2328
+ */
2220
2329
  function carryHandoff(sessionId, block) {
2221
2330
  var text = typeof block === "string" ? block.trim() : "";
2222
2331
  if (text === "") return;
2223
2332
  handoffPost({ action: "save", sessionId: sessionId, text: text }).catch(function () { return null; }).then(function () {
2224
2333
  var owner = workspaceOfSession(sessionId);
2225
- var workspaceId = owner === null ? undefined : owner.workspaceId;
2334
+ var workspaceId = owner === null || owner === undefined ? undefined : owner.workspaceId;
2226
2335
  var service = data.service;
2336
+ var sessions = sessionsService();
2227
2337
  var prompt = t("handoff.preamble") + "\n\n" + text;
2228
- if (service !== null && service !== undefined && typeof service.startSession === "function") {
2229
- try {
2230
- service.startSession(workspaceId, { prompt: prompt, whenDraftExists: "replace" });
2338
+ if (workspaceId !== undefined
2339
+ && service !== null && service !== undefined
2340
+ && typeof service.connectWorkspace === "function"
2341
+ && typeof service.openSession === "function"
2342
+ && sessions !== null && typeof sessions.using === "function") {
2343
+ service.connectWorkspace(workspaceId).then(function (newSessionId) {
2344
+ try { service.openSession(newSessionId); } catch (error) { /* the prompt still lands */ }
2345
+ return sessions.using(newSessionId, { source: HANDOFF_SOURCE }, function (reference) {
2346
+ var binding = reference === null || reference === undefined ? undefined : reference.binding;
2347
+ var session = binding === null || binding === undefined ? undefined : binding.session;
2348
+ if (session === undefined || session === null || typeof session.prompt !== "function") throw new Error("session unavailable");
2349
+ return session.prompt([{ type: "text", text: prompt }], "queue");
2350
+ });
2351
+ }).then(function (result) {
2352
+ if (result !== null && result !== undefined && result.ok === false) throw new Error("the session refused the prompt");
2231
2353
  resetHandoff();
2232
- autoSendDraft(t("handoff.preamble").slice(0, 12));
2233
- return;
2234
- } catch (error) {
2235
- /* fall through */
2236
- }
2237
- }
2238
- try {
2239
- if (navigator !== undefined && navigator.clipboard !== undefined) navigator.clipboard.writeText(prompt);
2240
- } catch (error) {
2241
- /* clipboard unavailable */
2354
+ }).catch(function () {
2355
+ startHandoffSession(workspaceId, prompt);
2356
+ });
2357
+ return;
2242
2358
  }
2243
- resetHandoff();
2359
+ startHandoffSession(workspaceId, prompt);
2244
2360
  });
2245
2361
  }
2246
2362
 
2247
2363
  /** Ask the current Session to rewrite the handoff with the user's adjustments. */
2248
2364
  function rewriteHandoff(sessionId, extra) {
2249
2365
  var text = extra.trim() === "" ? t("handoff.regeneratePrompt") : t("handoff.adjustPrompt") + "\n\n" + extra.trim();
2250
- var sent = sendToSession(text);
2251
- setHandoffState({ sessionId: sessionId, phase: "running", block: "", needsEnter: sent !== true });
2366
+ setHandoffState({ sessionId: sessionId, phase: "running", block: "", needsEnter: false });
2367
+ sendToSession(sessionId, text);
2252
2368
  handoffBaseline = 0;
2253
2369
  handoffTries = 0;
2254
2370
  handoffSession = sessionId;
@@ -2380,7 +2496,15 @@
2380
2496
  var Icon = ui.IconDataOutlineRegular;
2381
2497
  return h(Item, {
2382
2498
  icon: typeof Icon === "function" ? h(Icon, {}) : undefined,
2383
- onSelect: function () { openHandoffPanel(props.sessionId); },
2499
+ onSelect: function () {
2500
+ /* Open the conversation first so the running handoff (and later the
2501
+ card) is visible; the prompt itself does not need the composer. */
2502
+ var service = data.service;
2503
+ if (service !== null && service !== undefined && typeof service.openSession === "function") {
2504
+ try { service.openSession(props.sessionId); } catch (error) { /* the send still lands */ }
2505
+ }
2506
+ startHandoff(props.sessionId);
2507
+ },
2384
2508
  }, t("menu.handoff"));
2385
2509
  }
2386
2510
 
@@ -3257,7 +3381,15 @@
3257
3381
  }
3258
3382
 
3259
3383
  /* Test hook: the card state is a module observable, invisible from outside. */
3260
- exports.__handoff = { current: currentHandoffState, set: setHandoffState, blockOf: handoffBlockOf };
3384
+ exports.__handoff = {
3385
+ current: currentHandoffState,
3386
+ set: setHandoffState,
3387
+ blockOf: handoffBlockOf,
3388
+ start: startHandoff,
3389
+ rewrite: rewriteHandoff,
3390
+ carry: carryHandoff,
3391
+ send: sendToSession,
3392
+ };
3261
3393
 
3262
3394
  exports.name = "dsh-ws-recency";
3263
3395
  exports.inject = inject;
package/index.js CHANGED
@@ -32,7 +32,7 @@ import { dirname, join } from "node:path";
32
32
  export const name = "ws-recency";
33
33
 
34
34
  /** Version marker reported by the health route. */
35
- export const HOST_VERSION = "0.24.0";
35
+ export const HOST_VERSION = "0.24.1";
36
36
 
37
37
  /**
38
38
  * Exact routes the client half calls. Registered on the bare web server, so they
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-ws-recency",
3
- "version": "0.24.0",
3
+ "version": "0.24.1",
4
4
  "description": "Sidebar session management for DeepSeek Harness: workspace groups sorted by latest activity, stale history folded, a global pinned section, follow-up marks, session delete into a recoverable trash, archived sessions managed from that same trash panel, per-session token statistics with an argument/result split and an option to count tool arguments as the agent's writing, and moving a session to another workspace.",
5
5
  "license": "MIT",
6
6
  "author": "chyxhtc",