@ryuu-64/dsh-session-tools 0.4.2 → 0.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 +60 -8
- package/lib/client.js +149 -0
- package/lib/index.js +379 -8
- package/package.json +17 -3
package/README.md
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
# dsh-session-tools
|
|
2
2
|
|
|
3
|
-
让对话里的 AI
|
|
3
|
+
让对话里的 AI 能**新建会话**、**给别的会话发消息**、**看看有哪些会话**,以及**等别的会话干完活**。
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
四个工具,`session_create`、`session_send`、`list_sessions`、`session_wait`。新会话会出现在左侧会话列表里,和手动新建的完全一样;想让它归到某个工作区下面也可以。
|
|
6
6
|
|
|
7
|
-
典型用法:你说"帮我开一个会话去查一下这件事"——AI
|
|
7
|
+
典型用法:你说"帮我开一个会话去查一下这件事"——AI 直接建好,不用你自己新建再复制粘贴。或者你已经有一个会话在跑,说"等它弄完把结果告诉我",AI 把活派过去,等它做完了再回来接着讲。
|
|
8
8
|
|
|
9
9
|
## session_create:新建会话
|
|
10
10
|
|
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
| `title` | 可选 | 会话标题。不填就和手动新建一样,由第一句话自动生成。 |
|
|
16
16
|
| `wait` | 可选 | 默认不等。只有一种情况需要等:你要在新建的那个会话跑完第一轮之后才继续。 |
|
|
17
17
|
|
|
18
|
-
|
|
18
|
+
做完返回新会话的名字和 id,你在左侧列表里就能看到它。如果中途失败,会把建了一半的东西清理掉,不留垃圾。
|
|
19
19
|
|
|
20
20
|
## session_send:给别的会话发消息
|
|
21
21
|
|
|
@@ -23,13 +23,64 @@
|
|
|
23
23
|
|---|---|---|
|
|
24
24
|
| `sessionId` | 必填 | 目标会话的 id,形如 `session-9311a0fb-65c7-4b04-b142-1444642a627e`。 |
|
|
25
25
|
| `message` | 必填 | 要发的内容。对方看不到当前这个会话,所以要写清楚。 |
|
|
26
|
+
| `wait` | 可选 | 默认不等:发出去就返回。填 `true` 就等对方把这条消息处理完,并把它的回答带回来。 |
|
|
27
|
+
| `timeoutMs` | 可选 | 等多久(毫秒)。默认 60000(一分钟),最多 300000(五分钟)。等超了不报错,只告诉你"它还在忙"。 |
|
|
26
28
|
|
|
27
|
-
|
|
29
|
+
默认发出即返回,返回只代表对方收到了、开始排队处理。消息来源标记为插件,不会被当成是你本人打的字。目标会话如果没开着,会被自动打开再把消息排进去。
|
|
30
|
+
|
|
31
|
+
填了 `wait` 就变成"发完等它做完",AI 拿到对方的回答之后可以接着往下办——比如让一个会话去跑测试,跑完再根据结果决定下一步。**等的是它处理完你刚发的这条消息**,不是"它现在不忙了"就算完。
|
|
32
|
+
|
|
33
|
+
## session_wait:等别的会话干完
|
|
34
|
+
|
|
35
|
+
如果消息已经发出去了,或者那个会话本来就在忙,用这个等它收尾。
|
|
36
|
+
|
|
37
|
+
| 参数 | 是否必填 | 说明 |
|
|
38
|
+
|---|---|---|
|
|
39
|
+
| `sessionId` | 必填 | 目标会话的 id。 |
|
|
40
|
+
| `timeoutMs` | 可选 | 等多久(毫秒),默认 60000,最多 300000。 |
|
|
41
|
+
|
|
42
|
+
它只负责等和看,不去动那个会话:不会打开它、不会改它、也不会打断它。对方本来就没在忙的话,立刻返回,把最后一条回答给你。等超了就说一句"还在忙",不算失败——你可以过会儿再问一次。
|
|
43
|
+
|
|
44
|
+
## list_sessions:先看看有哪些会话
|
|
45
|
+
|
|
46
|
+
不用记 id。当你要说的是"发给那个讨论发布的会话",AI 可以先列出会话(名字、目录、哪个是当前会话、现在在干什么),你说清是哪一个,它再发。
|
|
47
|
+
|
|
48
|
+
| 参数 | 是否必填 | 说明 |
|
|
49
|
+
|---|---|---|
|
|
50
|
+
| `limit` | 可选 | 最多列几个,默认 20,按最近活动排序。 |
|
|
51
|
+
|
|
52
|
+
每条会带一个状态,就是侧边栏上显示的那个东西:
|
|
53
|
+
|
|
54
|
+
| 状态 | 意思 |
|
|
55
|
+
|---|---|
|
|
56
|
+
| `running` | 进行中,正忙着。 |
|
|
57
|
+
| `idle` | 空闲,没在干活(开着或者躺着都算)。 |
|
|
58
|
+
| `archived` | 已归档,你把它从侧边栏收起来了。 |
|
|
59
|
+
|
|
60
|
+
子代理会话不会列出来(那是 AI 自己派出去的活)。已归档的会话**会**列出来并标 `archived`——因为"已归档"只是你把它从侧边栏收起来了,它本身还在;AI 看到这个标记就知道那是你收起来的会话,可以据此决定要不要往里发消息。
|
|
61
|
+
|
|
62
|
+
`limit` 填得不合理会按合理值处理:填 0 或负数按 1 算,填超过 100 按 100 算,小数取整。
|
|
63
|
+
|
|
64
|
+
## 动手前会先问你
|
|
65
|
+
|
|
66
|
+
新建会话、或者往别的会话发消息,**都会先弹一个确认卡**,写清它要干什么,比如:
|
|
67
|
+
|
|
68
|
+
> 允许一次:在工作区 `E:\Users\Ryuu\Desktop` 新建会话,内容是「查一下某件事」
|
|
69
|
+
|
|
70
|
+
你点了"允许一次"它才动手;拒绝或没应答,它就什么都不做。你自己完全关掉审批(切到完全权限模式)时,它不再打扰你——那是你的选择,不是它擅自跳过。
|
|
71
|
+
|
|
72
|
+
只是等一个会话做完、或者列一下有哪些会话,不会弹卡:这两件事不往任何地方写东西。
|
|
73
|
+
|
|
74
|
+
## 建好的会话,卡片上点一下就能过去
|
|
75
|
+
|
|
76
|
+
做完之后,对话里那张卡片会变成「已创建会话」加一个按钮——**点一下直接跳到新建的那个会话**,不用去左侧列表里找。
|
|
77
|
+
|
|
78
|
+
(这一部分由插件的浏览器端负责渲染,所以旧的对话记录也会跟着变成可点。)
|
|
28
79
|
|
|
29
80
|
## 安装
|
|
30
81
|
|
|
31
82
|
```
|
|
32
|
-
dsh plugin --profile desktop add @ryuu-64/dsh-session-tools@0.
|
|
83
|
+
dsh plugin --profile desktop add @ryuu-64/dsh-session-tools@0.5.3
|
|
33
84
|
```
|
|
34
85
|
|
|
35
86
|
从源码装:
|
|
@@ -45,6 +96,7 @@ dsh plugin --profile desktop add link:C:\path\to\dsh-session-tools
|
|
|
45
96
|
|
|
46
97
|
- **不会顺手新建工作区**。`workspacePath` 必须是已经存在的那个工作区的路径;写错了它会报错,并把现有的工作区列出来给你挑。
|
|
47
98
|
- **不能给子代理发消息**。目标必须是普通会话;AI 派出去的子代理要用 `send_message`。也不能给当前这个会话自己发。
|
|
48
|
-
-
|
|
99
|
+
- **关掉的会话要有工作目录才能唤醒**。发消息时如果目标会话没开着,插件会把它重新打开;但如果那条会话没记录工作目录,就打不开、发不进去,会明确告诉你原因。
|
|
100
|
+
- **重启期间收不到**。新建会话是立刻生效的;发出去的消息要等对方会话被打开处理,所以 DSH 得开着。等待也一样,DSH 关了就没法等。
|
|
49
101
|
- **归属定了就不能改**。会话归到哪个工作区是记在账上的,建完之后没法在工作区之间搬。
|
|
50
|
-
-
|
|
102
|
+
- **需要桌面版**。工作区归属靠桌面版才挂载的那个工作区服务,插件把它列为必需依赖,所以命令行无界面模式(headless)下这个插件不会加载。
|
package/lib/client.js
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Browser half: render the `session_create` tool card as something the person
|
|
3
|
+
* can act on. The host half prints a receipt; this half turns the session that
|
|
4
|
+
* receipt names into a button that opens it.
|
|
5
|
+
*
|
|
6
|
+
* Loaded by the browser module table as a CJS closure factory; `require` resolves
|
|
7
|
+
* only platform seeds and other registered bundles.
|
|
8
|
+
*/
|
|
9
|
+
window.__ModuleLoader__.load({
|
|
10
|
+
id: "@ryuu-64/dsh-session-tools",
|
|
11
|
+
factory: (require) => {
|
|
12
|
+
var module = { exports: {} };
|
|
13
|
+
var exports = module.exports;
|
|
14
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
15
|
+
|
|
16
|
+
const react = require("react");
|
|
17
|
+
|
|
18
|
+
/** Slots this client half needs before it can register anything. */
|
|
19
|
+
const inject = ["slots", "sessions"];
|
|
20
|
+
|
|
21
|
+
/** Diagnostics stay behind one switch so a quiet console stays quiet. */
|
|
22
|
+
const DEBUG = false;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Flatten a settled result's content blocks, mirroring how `ui-tool` reads
|
|
26
|
+
* them: text blocks verbatim, anything else as JSON.
|
|
27
|
+
* @param node - a candidate settled result node.
|
|
28
|
+
* @returns the flattened text, or "" when this is not a result node.
|
|
29
|
+
*/
|
|
30
|
+
function resultText(node) {
|
|
31
|
+
if (node === null || typeof node !== "object" || !Array.isArray(node.content)) return "";
|
|
32
|
+
return node.content
|
|
33
|
+
.map((part) => (part !== null && typeof part === "object" && part.type === "text" && typeof part.text === "string"
|
|
34
|
+
? part.text
|
|
35
|
+
: JSON.stringify(part)))
|
|
36
|
+
.join("\n");
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Read the created session out of a settled tool result.
|
|
41
|
+
*
|
|
42
|
+
* The result node's `content` blocks are the form the host half guarantees,
|
|
43
|
+
* so they are the first source; a structured value is used when present.
|
|
44
|
+
* Both are read defensively: this half must never break the transcript when
|
|
45
|
+
* a shape changes underneath it.
|
|
46
|
+
* @param block - the settled tool call block handed to the slot.
|
|
47
|
+
* @param output - the owner's rendered result text, when it passes one.
|
|
48
|
+
* @returns the session id and optional title, or undefined.
|
|
49
|
+
*/
|
|
50
|
+
function readCreatedSession(block, output) {
|
|
51
|
+
if (block === null || typeof block !== "object") return undefined;
|
|
52
|
+
// Settled results expose `content` directly; a raw event nests them under `result`.
|
|
53
|
+
const settled = Array.isArray(block.content) ? block : block.result ?? block;
|
|
54
|
+
if (DEBUG) {
|
|
55
|
+
try {
|
|
56
|
+
console.log("[session-tools] block:", Object.keys(block), "settled:", Object.keys(settled), "output:", String(output ?? "").slice(0, 160));
|
|
57
|
+
} catch {}
|
|
58
|
+
}
|
|
59
|
+
for (const candidate of [settled.value, settled.output, block.value, block.output]) {
|
|
60
|
+
if (candidate !== null && typeof candidate === "object" && typeof candidate.sessionId === "string") {
|
|
61
|
+
return { sessionId: candidate.sessionId, title: typeof candidate.title === "string" ? candidate.title : undefined };
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
const text = [resultText(settled), typeof output === "string" ? output : ""].filter((part) => part !== "").join("\n");
|
|
65
|
+
const id = /session-[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/u.exec(text)?.[0];
|
|
66
|
+
if (id === undefined) return undefined;
|
|
67
|
+
const title = /Created session:\s*(.+?)\s+·\s+session-/u.exec(text)?.[1];
|
|
68
|
+
return { sessionId: id, title };
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* One row for a finished `session_create` call: what was created, and a
|
|
73
|
+
* button that opens it in the conversation area.
|
|
74
|
+
* @param props - slot owner props plus the injected `openSession`.
|
|
75
|
+
* @returns the row element.
|
|
76
|
+
*/
|
|
77
|
+
function SessionCreateRow(props) {
|
|
78
|
+
const openSession = props.openSession ?? props.slot?.injected?.openSession;
|
|
79
|
+
// The owner hands over the raw result node; `output` is its rendered text.
|
|
80
|
+
// Reading both makes the row survive a shape change on either side.
|
|
81
|
+
const created = readCreatedSession(props.block, props.output);
|
|
82
|
+
const title = created?.title === undefined || created.title === "" ? "会话" : created.title;
|
|
83
|
+
const canOpen = created !== undefined && typeof openSession === "function";
|
|
84
|
+
const onOpen = () => {
|
|
85
|
+
if (!canOpen) return;
|
|
86
|
+
try {
|
|
87
|
+
openSession(created.sessionId);
|
|
88
|
+
} catch (error) {
|
|
89
|
+
console.error("[session-tools] opening the session failed:", error);
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
return react.createElement(
|
|
93
|
+
"div",
|
|
94
|
+
{
|
|
95
|
+
style: {
|
|
96
|
+
display: "flex",
|
|
97
|
+
alignItems: "center",
|
|
98
|
+
gap: "8px",
|
|
99
|
+
flexWrap: "wrap",
|
|
100
|
+
fontSize: "13px",
|
|
101
|
+
},
|
|
102
|
+
},
|
|
103
|
+
react.createElement("span", null, "已创建会话"),
|
|
104
|
+
react.createElement(
|
|
105
|
+
"button",
|
|
106
|
+
{
|
|
107
|
+
type: "button",
|
|
108
|
+
onClick: onOpen,
|
|
109
|
+
disabled: !canOpen,
|
|
110
|
+
title: created?.sessionId ?? "没有读到会话 id",
|
|
111
|
+
style: {
|
|
112
|
+
border: "1px solid var(--dsh-border, #3a4150)",
|
|
113
|
+
background: "transparent",
|
|
114
|
+
color: "inherit",
|
|
115
|
+
borderRadius: "999px",
|
|
116
|
+
padding: "1px 10px",
|
|
117
|
+
cursor: canOpen ? "pointer" : "default",
|
|
118
|
+
font: "inherit",
|
|
119
|
+
opacity: canOpen ? "1" : "0.55",
|
|
120
|
+
},
|
|
121
|
+
},
|
|
122
|
+
title,
|
|
123
|
+
),
|
|
124
|
+
created === undefined ? react.createElement("span", { style: { opacity: 0.6 } }, "(结果里没有会话信息)") : null,
|
|
125
|
+
created === undefined ? null : react.createElement("span", { style: { opacity: 0.55, fontFamily: "ui-monospace, monospace", fontSize: "11px" } }, created.sessionId),
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Wait for the tool view slot list to exist, then register a keyed row that
|
|
131
|
+
* replaces the default card for `session_create`.
|
|
132
|
+
* @param ctx - the browser plugin context.
|
|
133
|
+
*/
|
|
134
|
+
function apply(ctx) {
|
|
135
|
+
ctx.slots.inject("tool.call.toolview", () => ctx.slots.register(
|
|
136
|
+
{
|
|
137
|
+
name: "tool.call.toolview",
|
|
138
|
+
key: "session_create",
|
|
139
|
+
inject: () => ({ openSession: (sessionId) => ctx.sessions.open(sessionId) }),
|
|
140
|
+
},
|
|
141
|
+
SessionCreateRow,
|
|
142
|
+
));
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
exports.apply = apply;
|
|
146
|
+
exports.inject = inject;
|
|
147
|
+
return module.exports;
|
|
148
|
+
},
|
|
149
|
+
});
|
package/lib/index.js
CHANGED
|
@@ -23,6 +23,58 @@ function workspaceCatalogue(registry) {
|
|
|
23
23
|
return items.map((workspace) => `${workspace.title} (${workspace.path})`).join("; ");
|
|
24
24
|
}
|
|
25
25
|
/**
|
|
26
|
+
* Name a session in tool output, the way a person would say it: the title they
|
|
27
|
+
* will see in the sidebar, plus the id when the title is not known yet.
|
|
28
|
+
*
|
|
29
|
+
* Deliberately NOT the `@[label](dsh-session:…)` mention form. That form renders
|
|
30
|
+
* as a chip in message text but carries no navigation — it is a label, not a
|
|
31
|
+
* link — and whether it renders at all depends on machinery a tool result does
|
|
32
|
+
* not have. A receipt should read the same everywhere.
|
|
33
|
+
* @param title - the session's title, when it is known yet.
|
|
34
|
+
* @param sessionId - the session's id.
|
|
35
|
+
* @returns one line naming the session.
|
|
36
|
+
*/
|
|
37
|
+
function sessionReference(title, sessionId) {
|
|
38
|
+
// Title reads hand back a snapshot, not a string; take the text either way so
|
|
39
|
+
// a receipt can never print an object.
|
|
40
|
+
const raw = typeof title === "object" && title !== null ? title.title : title;
|
|
41
|
+
const label = typeof raw === "string" ? raw.trim() : "";
|
|
42
|
+
return label === "" ? sessionId : `${label} · ${sessionId}`;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Ask the person before touching another session. The approval service applies
|
|
46
|
+
* the session's own policy, so this asks only when that policy is `ask`; a
|
|
47
|
+
* `never` policy means the person already turned prompts off, which is not the
|
|
48
|
+
* same as refusing, so the action proceeds.
|
|
49
|
+
* @param ctx - services of the calling agent.
|
|
50
|
+
* @param exec - the running tool execution, for the agent and call id.
|
|
51
|
+
* @param reason - what is about to happen, in the person's own terms.
|
|
52
|
+
* @returns nothing; throws when the action was refused.
|
|
53
|
+
* @throws when no approval channel is available (fail closed) or the person refused.
|
|
54
|
+
*/
|
|
55
|
+
async function requireApproval(ctx, exec, reason) {
|
|
56
|
+
const approval = ctx.get("approval");
|
|
57
|
+
if (approval === void 0) {
|
|
58
|
+
// No approval capability in this composition: do not act on another session
|
|
59
|
+
// without a way to ask, because the person would never see it happen.
|
|
60
|
+
throw new Error(`this DSH cannot ask for approval, so ${reason} was not done`);
|
|
61
|
+
}
|
|
62
|
+
const policy = approval.effectivePolicy(exec.agent.session);
|
|
63
|
+
if (policy !== "ask") return;
|
|
64
|
+
const outcome = await approval.request({
|
|
65
|
+
agent: exec.agent,
|
|
66
|
+
toolName: exec.name,
|
|
67
|
+
callId: exec.callId,
|
|
68
|
+
reason
|
|
69
|
+
});
|
|
70
|
+
if (outcome === "allowed-once") return;
|
|
71
|
+
throw new Error(
|
|
72
|
+
outcome === "rejected"
|
|
73
|
+
? `the person declined: ${reason} was not done`
|
|
74
|
+
: `no answer to the approval request, so ${reason} was not done (${outcome})`
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
26
78
|
* Give the new session the same model as this one: read the model the calling
|
|
27
79
|
* session is using, and fall back to the default when it has not chosen one.
|
|
28
80
|
*/
|
|
@@ -67,6 +119,96 @@ async function resolvePreset(ctx, agent, presets) {
|
|
|
67
119
|
}
|
|
68
120
|
}
|
|
69
121
|
/**
|
|
122
|
+
* Bound a wait request to a usable budget: a missing or nonsensical value falls
|
|
123
|
+
* back to the default, and the ceiling keeps one tool call from hanging a turn
|
|
124
|
+
* forever. A timeout is reported, never treated as an error.
|
|
125
|
+
* @param requested - the caller's requested budget in milliseconds, if any.
|
|
126
|
+
* @returns the budget actually used.
|
|
127
|
+
*/
|
|
128
|
+
function waitBudgetMs(requested) {
|
|
129
|
+
const DEFAULT_MS = 60_000;
|
|
130
|
+
const MAX_MS = 300_000;
|
|
131
|
+
if (typeof requested !== "number" || !Number.isFinite(requested) || requested <= 0) return DEFAULT_MS;
|
|
132
|
+
return Math.min(Math.trunc(requested), MAX_MS);
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Read a session's final answer, applying the same rule the host uses for a
|
|
136
|
+
* subagent's result: the LAST non-empty assistant message wins, and the
|
|
137
|
+
* accumulated assistant text is the fallback when no message qualifies
|
|
138
|
+
* (`@deepseek-ai/dsh-subagent`'s assistant-output rule). Text blocks are joined
|
|
139
|
+
* verbatim between them, matching that package's `finalText`.
|
|
140
|
+
* @param ctx - services of the calling agent.
|
|
141
|
+
* @param sessionId - the session to read.
|
|
142
|
+
* @param signal - optional cancellation for the persisted-log read.
|
|
143
|
+
* @returns the final text, or undefined when the session has no assistant reply.
|
|
144
|
+
*/
|
|
145
|
+
async function readFinalAnswer(ctx, sessionId) {
|
|
146
|
+
const live = ctx.agents.get(sessionId);
|
|
147
|
+
const own = typeof live?.session?.snapshotEvents === "function" ? live.session.snapshotEvents() : undefined;
|
|
148
|
+
let events = own;
|
|
149
|
+
if (events === undefined) {
|
|
150
|
+
const query = ctx.get("sessionQuery");
|
|
151
|
+
if (query === void 0) throw new Error(`this DSH cannot read session ${JSON.stringify(sessionId)}`);
|
|
152
|
+
// Reads the log without making the session live, so waiting stays read-only.
|
|
153
|
+
events = (await query.readSession(sessionId)).events;
|
|
154
|
+
}
|
|
155
|
+
const texts = (content) => (content ?? []).filter((block) => block.type === "text").map((block) => block.text).join("");
|
|
156
|
+
let answer;
|
|
157
|
+
const accumulated = [];
|
|
158
|
+
for (const event of events) {
|
|
159
|
+
if (event.type !== "assistant/message") continue;
|
|
160
|
+
const text = texts(event.data.message?.content);
|
|
161
|
+
if (text.trim() !== "") answer = text;
|
|
162
|
+
else accumulated.push(text);
|
|
163
|
+
}
|
|
164
|
+
const found = answer ?? (accumulated.length === 0 ? undefined : accumulated.join(""));
|
|
165
|
+
return found === undefined || found.trim() === "" ? undefined : found;
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Report what a session is doing right now: working, waiting, or put away. The
|
|
169
|
+
* archive set lives on the workspace registry and answers whether the person has
|
|
170
|
+
* hidden the session from the sidebar, so it needs no log read of its own.
|
|
171
|
+
* @param ctx - services of the calling agent.
|
|
172
|
+
* @param sessionId - the session to describe.
|
|
173
|
+
* @returns `"running"`, `"archived"`, or `"idle"`.
|
|
174
|
+
*/
|
|
175
|
+
function sessionState(ctx, sessionId) {
|
|
176
|
+
if (ctx.get("workspaceRegistry")?.archivedSessionIds?.some((id) => String(id) === sessionId) === true) return "archived";
|
|
177
|
+
return ctx.agents.get(sessionId)?.status === "running" ? "running" : "idle";
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Wait until a message this plugin queued has been handed to the driver. A
|
|
181
|
+
* session can still report `idle` the instant the message is queued, and a wait
|
|
182
|
+
* that accepts that would return the answer from the PREVIOUS turn — so the
|
|
183
|
+
* condition is "this message left the inbox AND the session is idle again".
|
|
184
|
+
*
|
|
185
|
+
* The inbox is public Agent surface: `inbox.nextTurn` is a readonly list of the
|
|
186
|
+
* messages still waiting, and each carries its own id.
|
|
187
|
+
* @param agent - the live target agent.
|
|
188
|
+
* @param messageId - the id of the message we just queued.
|
|
189
|
+
* @param budgetMs - how long to keep polling before giving up.
|
|
190
|
+
* @returns true when the message was taken, false on timeout.
|
|
191
|
+
*/
|
|
192
|
+
async function waitUntilConsumed(agent, messageId, budgetMs) {
|
|
193
|
+
const deadline = Date.now() + budgetMs;
|
|
194
|
+
const stillQueued = () => {
|
|
195
|
+
const pending = agent.inbox?.nextTurn;
|
|
196
|
+
// An unrecognisable inbox shape cannot prove anything; report a timeout
|
|
197
|
+
// rather than a completion we did not observe.
|
|
198
|
+
if (!Array.isArray(pending)) return undefined;
|
|
199
|
+
return pending.some((queued) => queued?.id === messageId);
|
|
200
|
+
};
|
|
201
|
+
for (;;) {
|
|
202
|
+
const queued = stillQueued();
|
|
203
|
+
if (queued === false && agent.status !== "running") return true;
|
|
204
|
+
if (queued === undefined) return false;
|
|
205
|
+
if (Date.now() >= deadline) return false;
|
|
206
|
+
await new Promise((resolve) => {
|
|
207
|
+
setTimeout(resolve, 25);
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
70
212
|
* Get the live agent for a target session, opening the session again when it is
|
|
71
213
|
* not currently running. The session is reopened on the preset it was created
|
|
72
214
|
* with.
|
|
@@ -87,7 +229,7 @@ async function resolveTargetAgent(ctx, sessionId, callerAgent) {
|
|
|
87
229
|
throw new Error(`session_send cannot read ${JSON.stringify(sessionId)}: ${error instanceof Error ? error.message : String(error)}`);
|
|
88
230
|
}
|
|
89
231
|
if (record === void 0) throw new Error(`session_send cannot find session ${JSON.stringify(sessionId)}: no open or saved session has that id`);
|
|
90
|
-
if (record.header.cwd === void 0) throw new Error(`session_send cannot open ${JSON.stringify(sessionId)}:
|
|
232
|
+
if (record.header.cwd === void 0) throw new Error(`session_send cannot open ${JSON.stringify(sessionId)}: this saved session does not record a working directory, and reopening it needs one. Send to a session that has a directory, or ask the person to open that session once.`);
|
|
91
233
|
const agentPresets = ctx.get("agentPresets");
|
|
92
234
|
const presetId = record.header.agentPreset;
|
|
93
235
|
const setup = agentPresets === void 0 || presetId === void 0 ? void 0 : async (agentCtx) => {
|
|
@@ -145,12 +287,17 @@ function apply(ctx) {
|
|
|
145
287
|
required: true
|
|
146
288
|
},
|
|
147
289
|
workspaceId: { type: "string" },
|
|
148
|
-
workspacePath: { type: "string" }
|
|
290
|
+
workspacePath: { type: "string" },
|
|
291
|
+
title: { type: "string" }
|
|
149
292
|
}
|
|
150
293
|
},
|
|
294
|
+
// The card is the receipt: it names the session, so the person can find it
|
|
295
|
+
// in the sidebar without digging a session id out of a log.
|
|
151
296
|
render: (_args, value) => [{
|
|
152
297
|
type: "text",
|
|
153
|
-
text: value.grouped
|
|
298
|
+
text: value.grouped === true
|
|
299
|
+
? `Created session: ${sessionReference(value.title, value.sessionId)} in workspace ${value.workspacePath}`
|
|
300
|
+
: `Created session: ${sessionReference(value.title, value.sessionId)} (ungrouped)`
|
|
154
301
|
}]
|
|
155
302
|
},
|
|
156
303
|
async execute(args, exec) {
|
|
@@ -171,6 +318,10 @@ function apply(ctx) {
|
|
|
171
318
|
cwd = exec.agent.session.header.cwd;
|
|
172
319
|
if (cwd === void 0) throw new Error("session_create needs a working directory, and this session has none");
|
|
173
320
|
}
|
|
321
|
+
// Ask before creating anything: a session that appears in the person's
|
|
322
|
+
// list without them knowing about it is the whole risk of this tool.
|
|
323
|
+
const where = workspace === void 0 ? "ungrouped" : `in workspace ${JSON.stringify(workspace.path)}`;
|
|
324
|
+
await requireApproval(ctx, exec, `create a new session ${where}, starting with: ${prompt.slice(0, 120)}`);
|
|
174
325
|
const agentOptions = currentSelection(ctx, exec.agent);
|
|
175
326
|
const agentPresets = ctx.get("agentPresets");
|
|
176
327
|
const presetId = await resolvePreset(ctx, exec.agent, agentPresets);
|
|
@@ -183,6 +334,7 @@ function apply(ctx) {
|
|
|
183
334
|
agentOptions,
|
|
184
335
|
meta: {
|
|
185
336
|
cwd,
|
|
337
|
+
parentSession: String(exec.agent.session.header.id),
|
|
186
338
|
...presetId === void 0 ? {} : { agentPreset: presetId }
|
|
187
339
|
},
|
|
188
340
|
...agentPresets === void 0 || presetId === void 0 ? {} : { setup: async (agentCtx) => {
|
|
@@ -215,10 +367,18 @@ function apply(ctx) {
|
|
|
215
367
|
} catch {}
|
|
216
368
|
throw error;
|
|
217
369
|
}
|
|
370
|
+
// Name the session in the receipt when its title is known yet; a session
|
|
371
|
+
// titled from its first message may not have one until that turn runs.
|
|
372
|
+
let currentTitle;
|
|
373
|
+
try {
|
|
374
|
+
// The service returns a title snapshot; the text lives on `.title`.
|
|
375
|
+
currentTitle = ctx.sessionTitle.get(handle.agent.session)?.title;
|
|
376
|
+
} catch {}
|
|
218
377
|
return {
|
|
219
378
|
sessionId: String(sessionId),
|
|
220
379
|
grouped: workspace !== void 0,
|
|
221
380
|
cwd,
|
|
381
|
+
...currentTitle === void 0 || currentTitle === "" ? {} : { title: String(currentTitle) },
|
|
222
382
|
...workspace === void 0 ? {} : {
|
|
223
383
|
workspaceId: String(workspace.id),
|
|
224
384
|
workspacePath: workspace.path
|
|
@@ -239,6 +399,14 @@ function apply(ctx) {
|
|
|
239
399
|
type: "string",
|
|
240
400
|
required: true,
|
|
241
401
|
description: "Message text. Write it self-contained: the target cannot see this conversation unless the person linked the two."
|
|
402
|
+
},
|
|
403
|
+
wait: {
|
|
404
|
+
type: "boolean",
|
|
405
|
+
description: "Wait for the target to finish working on this message before returning, and report its answer. Defaults to false: sending returns as soon as the target accepts the message. Waiting is bounded by timeoutMs."
|
|
406
|
+
},
|
|
407
|
+
timeoutMs: {
|
|
408
|
+
type: "number",
|
|
409
|
+
description: "How long to wait when wait is true, in milliseconds. Defaults to 60000, at most 300000. A timeout reports that the target is still working rather than failing."
|
|
242
410
|
}
|
|
243
411
|
},
|
|
244
412
|
output: {
|
|
@@ -253,12 +421,20 @@ function apply(ctx) {
|
|
|
253
421
|
cwd: {
|
|
254
422
|
type: "string",
|
|
255
423
|
required: true
|
|
256
|
-
}
|
|
424
|
+
},
|
|
425
|
+
waited: { type: "boolean" },
|
|
426
|
+
completed: { type: "boolean" },
|
|
427
|
+
elapsedMs: { type: "number" },
|
|
428
|
+
output: { type: "string" }
|
|
257
429
|
}
|
|
258
430
|
},
|
|
259
431
|
render: (_args, value) => [{
|
|
260
432
|
type: "text",
|
|
261
|
-
text:
|
|
433
|
+
text: value.waited !== true
|
|
434
|
+
? `Delivered to session ${value.sessionId}`
|
|
435
|
+
: value.completed === true
|
|
436
|
+
? `Session ${value.sessionId} finished (${Math.round((value.elapsedMs ?? 0) / 1000)}s):\n${value.output ?? "(no text answer)"}`
|
|
437
|
+
: `Delivered to session ${value.sessionId}; still working after ${Math.round((value.elapsedMs ?? 0) / 1000)}s`
|
|
262
438
|
}]
|
|
263
439
|
},
|
|
264
440
|
async execute(args, exec) {
|
|
@@ -272,7 +448,10 @@ function apply(ctx) {
|
|
|
272
448
|
if (targetId === String(exec.agent.id)) throw new Error("session_send cannot send to its own session: use the current conversation instead");
|
|
273
449
|
const depth = target.session.header.delegationDepth ?? 0;
|
|
274
450
|
if (depth > 0) throw new Error(`session_send cannot send to ${JSON.stringify(targetId)}: that is a subagent session, not a regular one. Use send_message for it.`);
|
|
275
|
-
|
|
451
|
+
// Ask before writing into another session: the person should see a message
|
|
452
|
+
// arrive in a conversation they are not looking at.
|
|
453
|
+
await requireApproval(ctx, exec, `send a message into session ${JSON.stringify(targetId)}: ${message.slice(0, 120)}`);
|
|
454
|
+
const sent = createUserMessage({
|
|
276
455
|
content: [{
|
|
277
456
|
type: "text",
|
|
278
457
|
text: message
|
|
@@ -281,11 +460,203 @@ function apply(ctx) {
|
|
|
281
460
|
kind: "plugin",
|
|
282
461
|
plugin: name
|
|
283
462
|
}
|
|
284
|
-
})
|
|
285
|
-
|
|
463
|
+
});
|
|
464
|
+
target.followup(sent);
|
|
465
|
+
if (args.wait !== true) return {
|
|
286
466
|
sessionId: targetId,
|
|
287
467
|
cwd: String(target.session.header.cwd ?? "")
|
|
288
468
|
};
|
|
469
|
+
// Waiting for our own message, not merely for quiet. The agent can still
|
|
470
|
+
// look idle the instant the message is queued, so an idle wait alone
|
|
471
|
+
// would return the PREVIOUS answer; wait until this message has left the
|
|
472
|
+
// inbox first. Both signals are public Agent surface.
|
|
473
|
+
const startedAt = Date.now();
|
|
474
|
+
const timeoutMs = waitBudgetMs(args.timeoutMs);
|
|
475
|
+
const consumed = await waitUntilConsumed(target, sent.id, timeoutMs);
|
|
476
|
+
const answer = await readFinalAnswer(ctx, targetId);
|
|
477
|
+
return {
|
|
478
|
+
sessionId: targetId,
|
|
479
|
+
cwd: String(target.session.header.cwd ?? ""),
|
|
480
|
+
waited: true,
|
|
481
|
+
completed: consumed,
|
|
482
|
+
elapsedMs: Date.now() - startedAt,
|
|
483
|
+
...answer === void 0 ? {} : { output: answer }
|
|
484
|
+
};
|
|
485
|
+
}
|
|
486
|
+
}));
|
|
487
|
+
ctx.tools.register(defineTool({
|
|
488
|
+
name: "list_sessions",
|
|
489
|
+
description: "List the sessions the person can see, newest first, with the id each one carries and what it is doing right now (working, waiting, or put away). Use it before session_send when the target is described by name rather than by id, so the message goes to the right session.",
|
|
490
|
+
parameters: {
|
|
491
|
+
limit: {
|
|
492
|
+
type: "number",
|
|
493
|
+
description: "How many sessions to list, most recently active first. Defaults to 20."
|
|
494
|
+
}
|
|
495
|
+
},
|
|
496
|
+
output: {
|
|
497
|
+
schema: {
|
|
498
|
+
type: "object",
|
|
499
|
+
additionalProperties: false,
|
|
500
|
+
properties: {
|
|
501
|
+
sessions: {
|
|
502
|
+
type: "array",
|
|
503
|
+
required: true,
|
|
504
|
+
items: {
|
|
505
|
+
type: "object",
|
|
506
|
+
additionalProperties: false,
|
|
507
|
+
properties: {
|
|
508
|
+
sessionId: {
|
|
509
|
+
type: "string",
|
|
510
|
+
required: true
|
|
511
|
+
},
|
|
512
|
+
title: { type: "string" },
|
|
513
|
+
cwd: { type: "string" },
|
|
514
|
+
state: {
|
|
515
|
+
type: "string",
|
|
516
|
+
required: true,
|
|
517
|
+
enum: ["running", "idle", "archived"],
|
|
518
|
+
description: "What the session is doing right now: running (working on a turn), idle (open or saved, not working), archived (put away from the sidebar)."
|
|
519
|
+
},
|
|
520
|
+
current: { type: "boolean" },
|
|
521
|
+
subagent: { type: "boolean" }
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
},
|
|
525
|
+
total: {
|
|
526
|
+
type: "number",
|
|
527
|
+
required: true
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
},
|
|
531
|
+
render: (_args, value) => [{
|
|
532
|
+
type: "text",
|
|
533
|
+
text: value.total === 0
|
|
534
|
+
? "No other sessions."
|
|
535
|
+
: [
|
|
536
|
+
value.sessions.map((entry) => [
|
|
537
|
+
sessionReference(entry.title, entry.sessionId),
|
|
538
|
+
entry.current === true ? "(this session)" : undefined,
|
|
539
|
+
entry.subagent === true ? "(subagent)" : undefined,
|
|
540
|
+
entry.state === "running" ? "(working now)" : undefined,
|
|
541
|
+
entry.state === "archived" ? "(put away)" : undefined,
|
|
542
|
+
entry.cwd
|
|
543
|
+
].filter((part) => part !== undefined).join(" · ")).join("\n"),
|
|
544
|
+
value.sessions.some((entry) => entry.state === "archived") ? "Sessions marked (put away) are archived: they are hidden from the sidebar but still listed here." : undefined
|
|
545
|
+
].filter((part) => part !== undefined).join("\n")
|
|
546
|
+
}]
|
|
547
|
+
},
|
|
548
|
+
async execute(args, exec) {
|
|
549
|
+
const query = ctx.get("sessionQuery");
|
|
550
|
+
if (query === void 0) throw new Error("list_sessions needs session lookup, which this DSH does not offer");
|
|
551
|
+
const requested = args.limit === void 0 ? 20 : Math.trunc(args.limit);
|
|
552
|
+
const limit = requested < 1 ? 1 : requested > 100 ? 100 : requested;
|
|
553
|
+
const self = exec.agent === void 0 ? undefined : String(exec.agent.id);
|
|
554
|
+
const records = await query.listSessions();
|
|
555
|
+
const listed = [];
|
|
556
|
+
let total = 0;
|
|
557
|
+
for (const record of records) {
|
|
558
|
+
const header = record.header;
|
|
559
|
+
const sessionId = String(header.id);
|
|
560
|
+
total += 1;
|
|
561
|
+
// Subagent sessions are someone else's working state, not a target the
|
|
562
|
+
// person can be asked about; the count still reports them.
|
|
563
|
+
if ((header.delegationDepth ?? 0) > 0 || header.origin === "subagent") continue;
|
|
564
|
+
if (listed.length >= limit) continue;
|
|
565
|
+
let title;
|
|
566
|
+
try {
|
|
567
|
+
// Title reads return a snapshot, not a string: the text lives on
|
|
568
|
+
// `.title`, exactly like the title service the create path uses.
|
|
569
|
+
title = (await query.readTitle(sessionId))?.title;
|
|
570
|
+
} catch {}
|
|
571
|
+
listed.push({
|
|
572
|
+
sessionId,
|
|
573
|
+
...title === void 0 || title === "" ? {} : { title: String(title) },
|
|
574
|
+
...header.cwd === void 0 ? {} : { cwd: String(header.cwd) },
|
|
575
|
+
state: sessionState(ctx, sessionId),
|
|
576
|
+
...sessionId === self ? { current: true } : {}
|
|
577
|
+
});
|
|
578
|
+
}
|
|
579
|
+
return { sessions: listed, total };
|
|
580
|
+
}
|
|
581
|
+
}));
|
|
582
|
+
ctx.tools.register(defineTool({
|
|
583
|
+
name: "session_wait",
|
|
584
|
+
description: "Wait for another chat session to finish what it is doing and report its answer. Use it after session_send to hear back, or on its own when the person asks how another session is getting on. It only reads: it never opens, changes, or interrupts a session, and a session that is not running returns immediately.",
|
|
585
|
+
parameters: {
|
|
586
|
+
sessionId: {
|
|
587
|
+
type: "string",
|
|
588
|
+
required: true,
|
|
589
|
+
description: "Target Session id, for example \"session-2330bd36-fe62-4621-8d42-c7e04566f447\"."
|
|
590
|
+
},
|
|
591
|
+
timeoutMs: {
|
|
592
|
+
type: "number",
|
|
593
|
+
description: "How long to wait, in milliseconds. Defaults to 60000, at most 300000. A timeout reports that the session is still working rather than failing."
|
|
594
|
+
}
|
|
595
|
+
},
|
|
596
|
+
output: {
|
|
597
|
+
schema: {
|
|
598
|
+
type: "object",
|
|
599
|
+
additionalProperties: false,
|
|
600
|
+
properties: {
|
|
601
|
+
sessionId: {
|
|
602
|
+
type: "string",
|
|
603
|
+
required: true
|
|
604
|
+
},
|
|
605
|
+
running: {
|
|
606
|
+
type: "boolean",
|
|
607
|
+
required: true
|
|
608
|
+
},
|
|
609
|
+
completed: {
|
|
610
|
+
type: "boolean",
|
|
611
|
+
required: true
|
|
612
|
+
},
|
|
613
|
+
elapsedMs: { type: "number" },
|
|
614
|
+
output: { type: "string" }
|
|
615
|
+
}
|
|
616
|
+
},
|
|
617
|
+
render: (_args, value) => [{
|
|
618
|
+
type: "text",
|
|
619
|
+
text: value.running !== true || value.completed === true
|
|
620
|
+
? `Session ${value.sessionId} is not working on anything${value.output === undefined ? "" : `; its answer:\n${value.output}`}`
|
|
621
|
+
: `Session ${value.sessionId} is still working after ${Math.round((value.elapsedMs ?? 0) / 1000)}s`
|
|
622
|
+
}]
|
|
623
|
+
},
|
|
624
|
+
async execute(args, exec) {
|
|
625
|
+
const requested = args.sessionId.trim();
|
|
626
|
+
if (requested === "") throw new Error("session_wait needs a session id");
|
|
627
|
+
if (exec.agent !== void 0 && requested === String(exec.agent.id)) throw new Error("session_wait cannot wait on its own session: that would wait for the answer it is writing");
|
|
628
|
+
const target = ctx.agents.get(requested);
|
|
629
|
+
if (target === void 0) {
|
|
630
|
+
// Not running: nothing to wait for. Confirm the id exists at all so a
|
|
631
|
+
// typo is reported instead of looking like an idle session.
|
|
632
|
+
const query = ctx.get("sessionQuery");
|
|
633
|
+
if (query === void 0) throw new Error(`session_wait cannot check ${JSON.stringify(requested)}: this DSH does not offer session lookup`);
|
|
634
|
+
const known = (await query.listSessions()).some((entry) => String(entry.header.id) === requested);
|
|
635
|
+
if (!known) throw new Error(`session_wait cannot find session ${JSON.stringify(requested)}: no open or saved session has that id`);
|
|
636
|
+
const answer = await readFinalAnswer(ctx, requested);
|
|
637
|
+
return {
|
|
638
|
+
sessionId: requested,
|
|
639
|
+
running: false,
|
|
640
|
+
completed: true,
|
|
641
|
+
elapsedMs: 0,
|
|
642
|
+
...answer === void 0 ? {} : { output: answer }
|
|
643
|
+
};
|
|
644
|
+
}
|
|
645
|
+
const startedAt = Date.now();
|
|
646
|
+
const timedOut = await Promise.race([
|
|
647
|
+
target.whenIdle().then(() => false),
|
|
648
|
+
new Promise((resolve) => {
|
|
649
|
+
setTimeout(() => resolve(true), waitBudgetMs(args.timeoutMs));
|
|
650
|
+
})
|
|
651
|
+
]);
|
|
652
|
+
const answer = await readFinalAnswer(ctx, requested);
|
|
653
|
+
return {
|
|
654
|
+
sessionId: requested,
|
|
655
|
+
running: true,
|
|
656
|
+
completed: !timedOut,
|
|
657
|
+
elapsedMs: Date.now() - startedAt,
|
|
658
|
+
...answer === void 0 ? {} : { output: answer }
|
|
659
|
+
};
|
|
289
660
|
}
|
|
290
661
|
}));
|
|
291
662
|
}
|
package/package.json
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ryuu-64/dsh-session-tools",
|
|
3
|
-
"version": "0.4
|
|
4
|
-
"description": "Let the agent start a new chat session,
|
|
3
|
+
"version": "0.5.4",
|
|
4
|
+
"description": "Let the agent start a new chat session, send a message to another one, or wait for it to finish.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"test": "node --test \"test/**/*.test.mjs\"",
|
|
9
|
+
"check": "node --check lib/index.js && node --check lib/client.js && npm test"
|
|
10
|
+
},
|
|
7
11
|
"keywords": [
|
|
8
12
|
"deepseek-harness",
|
|
9
13
|
"dsh",
|
|
@@ -18,11 +22,20 @@
|
|
|
18
22
|
"main": "lib/index.js",
|
|
19
23
|
"exports": {
|
|
20
24
|
".": "./lib/index.js",
|
|
25
|
+
"./client": "./lib/client.js",
|
|
21
26
|
"./package.json": "./package.json"
|
|
22
27
|
},
|
|
23
28
|
"dsh": {
|
|
24
29
|
"bundle": {
|
|
25
30
|
"patch": "./cordis.patch.yml"
|
|
31
|
+
},
|
|
32
|
+
"client": {
|
|
33
|
+
"platform": "web",
|
|
34
|
+
"inject": [
|
|
35
|
+
"@deepseek-ai/dsh-client-runtime",
|
|
36
|
+
"@deepseek-ai/dsh-client-ui-slots",
|
|
37
|
+
"@deepseek-ai/dsh-client-ui-tool"
|
|
38
|
+
]
|
|
26
39
|
}
|
|
27
40
|
},
|
|
28
41
|
"publishConfig": {
|
|
@@ -38,7 +51,8 @@
|
|
|
38
51
|
"@deepseek-ai/dsh-session-title": ">=0.1.5-rc.2 <0.1.6-0 || >=0.1.6-0 <0.2.0-0",
|
|
39
52
|
"@deepseek-ai/dsh-tools": ">=0.1.5-rc.2 <0.1.6-0 || >=0.1.6-0 <0.2.0-0",
|
|
40
53
|
"@deepseek-ai/dsh-workspace": ">=0.1.5-rc.2 <0.1.6-0 || >=0.1.6-0 <0.2.0-0",
|
|
41
|
-
"@deepseek-ai/schemastery": "^3.18.2"
|
|
54
|
+
"@deepseek-ai/schemastery": "^3.18.2",
|
|
55
|
+
"react": ">=18 <20"
|
|
42
56
|
},
|
|
43
57
|
"files": [
|
|
44
58
|
"lib",
|