koishi-plugin-chatluna-dsh-adapter 0.2.1 → 0.3.0
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 +72 -24
- package/lib/bridge.cjs +38 -8
- package/lib/index.cjs +75 -28
- package/lib/scope.cjs +144 -0
- package/lib/tool.cjs +70 -36
- package/package.json +57 -57
package/README.md
CHANGED
|
@@ -4,6 +4,19 @@
|
|
|
4
4
|
|
|
5
5
|
也保留「模型适配器模式」:把 ChatLuna 的模型直接选成 `dsh/deepseek-harness`,让 Harness 当对话主脑。两种模式可以共存,但通常只需要工具模式。
|
|
6
6
|
|
|
7
|
+
从 **0.3.0** 起,每个会话有独立工作区:部署者(`trustedUsers`)使用基础 `workspace` 和全部工具;其他私聊使用 `private/<QQ号>`;群聊使用 `groups/<群号>`(也可按成员再分)。访客可以正常用 Harness,但越界操作不会自动批准。
|
|
8
|
+
|
|
9
|
+
> ### ⚠️ 如果你同时用 `chatluna-character`
|
|
10
|
+
>
|
|
11
|
+
> 请在 `chatluna-character` 里关闭 **「是否启用 `<think>` 标签」**(配置项 `toolCallReplyThinkTag`,默认是开启的)。
|
|
12
|
+
>
|
|
13
|
+
> 开启时,它的 `character_reply` 工具会强制要求 `think` 字段。`deepseek-flash` 这类模型有时会漏掉,随后报:
|
|
14
|
+
>
|
|
15
|
+
> - `missing character_reply tool call`
|
|
16
|
+
> - 或 `invalid character_reply tool call ... required property "think"`
|
|
17
|
+
>
|
|
18
|
+
> 这时 Harness 其实已经执行完了(日志里有 `harness task done`),失败的是角色插件生成最终回复。关闭该选项后不再要求 `think`;如果一定要保留 think 标签,就换更强的 tool-calling 模型,并在预设/作者注里明确要求每次 `character_reply` 都带 `"think": "..."`。
|
|
19
|
+
|
|
7
20
|
## 两种模式
|
|
8
21
|
|
|
9
22
|
### 工具模式(默认,推荐)
|
|
@@ -36,6 +49,8 @@ Koishi ──► ChatLuna(原模型 / 原预设 / chatluna-character)
|
|
|
36
49
|
- **默认不经过 Harness**。只有最后一条用户消息包含 `triggerKeywords`(默认 `#harness`、`harness`、`用harness`、`调用harness`)时,`harness` 工具才会被授权给这一轮。
|
|
37
50
|
- **关键词门禁不依赖工具 selector**。`chatluna-character` 传给 `selector(history)` 的历史不含当前消息,所以本插件用一个前置 Koishi middleware 按会话“武装”工具,再由 `authorization(session)` 检查。这样角色插件也能正确触发。
|
|
38
51
|
- **工具由 DSH 执行**。模型只负责决定“要不要调用”和“任务描述是什么”;文件、命令、网页、子代理都在 DSH 内完成。
|
|
52
|
+
- **工作区按会话隔离**。DSH 进程的 `cwd` 和 `sandbox-policy.workspaceRoot` 都指向该会话的文件夹;`trustedUsers` 永远用基础 `workspace`,所以即使人在群里,也按“部署者”处理。
|
|
53
|
+
- **访客能干活,但不能越界**。访客仍可使用 shell/fs/web 等工具,但 DSH 的“离开工作区”审批会被拒绝;只有 owner 的审批会放行。
|
|
39
54
|
|
|
40
55
|
### 模型适配器模式(可选)
|
|
41
56
|
|
|
@@ -73,13 +88,13 @@ corepack yarn add koishi-plugin-chatluna-dsh-adapter
|
|
|
73
88
|
开发阶段或离线安装可以用 tgz。Yarn 4 必须写成 `包名@file:路径`:
|
|
74
89
|
|
|
75
90
|
```bash
|
|
76
|
-
corepack yarn add "koishi-plugin-chatluna-dsh-adapter@file:./koishi-plugin-chatluna-dsh-adapter-0.
|
|
91
|
+
corepack yarn add "koishi-plugin-chatluna-dsh-adapter@file:./koishi-plugin-chatluna-dsh-adapter-0.3.0.tgz"
|
|
77
92
|
```
|
|
78
93
|
|
|
79
94
|
如果 Yarn 报 `YN0027` / `YN0035`,或者把路径当成 npm 包名去 registry 找,就是这个格式问题。也可以直接用 npm:
|
|
80
95
|
|
|
81
96
|
```bash
|
|
82
|
-
npm install "./koishi-plugin-chatluna-dsh-adapter-0.
|
|
97
|
+
npm install "./koishi-plugin-chatluna-dsh-adapter-0.3.0.tgz"
|
|
83
98
|
```
|
|
84
99
|
|
|
85
100
|
然后在 Koishi 控制台启用 `chatluna-dsh-adapter`。**不需要**去 ChatLuna 或 `chatluna-character` 里改模型。
|
|
@@ -92,17 +107,20 @@ npm install "./koishi-plugin-chatluna-dsh-adapter-0.2.1.tgz"
|
|
|
92
107
|
| `cliPath` | 空 | 桌面版的 `cli.js` 路径;使用 PATH 里的 `dsh` 时留空。 |
|
|
93
108
|
| `profile` | `acp` | DSH profile。 |
|
|
94
109
|
| `dshHome` | 空 | `DSH_HOME`。留空继承 Koishi 进程环境;Windows 默认是 `C:\Users\<你>\.dsh`。 |
|
|
95
|
-
| `workspace` | 空 |
|
|
110
|
+
| `workspace` | 空 | 基础工作区;留空用 `<koishi baseDir>/data/chatluna-dsh`。owner 直接用这里,访客用它的子目录。 |
|
|
111
|
+
| `groupWorkspaceMode` | `shared` | `shared` = 每个群一个共享文件夹;`per-user` = 每个群成员在群文件夹下再分自己的子目录。 |
|
|
96
112
|
| `model` | 空 | DSH 模型,可填 `deepseek-flash`、`deepseek-account/deepseek-flash` 或 `["deepseek-account","deepseek-flash"]`。适配器会按 DSH 会话暴露的模型选项自动解析;留空用默认路由(缺凭据时会失败)。 |
|
|
97
|
-
| `toolPermission` | `trusted` | `allow` =
|
|
98
|
-
| `
|
|
99
|
-
| `
|
|
113
|
+
| `toolPermission` | `trusted` | `allow` = 所有人可用工具;`trusted` = owner 用基础工作区+全部工具,访客按 `guestToolAccess`;`reject` = 所有人禁用工具。 |
|
|
114
|
+
| `guestToolAccess` | `true` | 非 owner 是否也能使用工具;他们只能在自己的私聊/群文件夹里操作。 |
|
|
115
|
+
| `trustedUsers` | `[]` | 完全信任的 Koishi `userId`:基础工作区 + 全部工具;在群里也按 owner 处理。 |
|
|
116
|
+
| `trustedGuilds` | `[]` | 允许使用 Harness 的群 `guildId`;不授予基础工作区,成员仍用群文件夹。 |
|
|
100
117
|
| `requestTimeout` | `180000` | 单次 DSH 调用超时(毫秒)。 |
|
|
101
118
|
| `contextSize` | `128000` | 仅模型适配器模式使用:向 ChatLuna 声明的上下文长度。 |
|
|
102
119
|
| `toolEnabled` | `true` | 是否注册 `harness` ChatLuna 工具。 |
|
|
103
120
|
| `toolName` | `harness` | 注册给 ChatLuna 的工具名。 |
|
|
104
121
|
| `triggerKeywords` | `["#harness","harness","用harness","调用harness"]` | 只有最后一条用户消息包含其中之一,才授权工具。 |
|
|
105
122
|
| `toolSystemPrompt` | 空 | Harness 任务执行器的系统提示词;留空用内置的简洁执行器提示。 |
|
|
123
|
+
| `characterThinkTagReminder` | `true` | 仅用于在配置页显示 `chatluna-character` 的 `<think>` 兼容提醒,不影响运行。 |
|
|
106
124
|
| `debug` | `false` | 输出关键词匹配、授权、任务耗时等日志。 |
|
|
107
125
|
|
|
108
126
|
### 桌面版示例
|
|
@@ -115,6 +133,8 @@ dshHome: C:\Users\MECHREV\.dsh
|
|
|
115
133
|
workspace: D:\koishi-dsh-workspace
|
|
116
134
|
model: deepseek-flash
|
|
117
135
|
toolPermission: trusted
|
|
136
|
+
guestToolAccess: true
|
|
137
|
+
groupWorkspaceMode: shared
|
|
118
138
|
trustedUsers:
|
|
119
139
|
- '你的QQ号'
|
|
120
140
|
trustedGuilds: []
|
|
@@ -155,12 +175,17 @@ harness 帮我查一下今天的日期,用 pwsh
|
|
|
155
175
|
|
|
156
176
|
如果仍报错,把 `debug` 打开,确认日志里出现了 `harness authorization allowed=true`,并且 `chatluna-character` 自己没有把模型切到 dsh。
|
|
157
177
|
|
|
158
|
-
##
|
|
159
|
-
|
|
160
|
-
-
|
|
161
|
-
-
|
|
162
|
-
-
|
|
163
|
-
- DSH
|
|
178
|
+
## 安全与隔离
|
|
179
|
+
|
|
180
|
+
- **owner(`trustedUsers`)**:使用基础 `workspace`,拥有全部工具;在群里触发也按 owner 处理。只把自己和完全信任的人放进去。
|
|
181
|
+
- **私聊访客**:工作区是 `<workspace>/private/<QQ号>`。第一次调用时自动创建。
|
|
182
|
+
- **群聊访客**:工作区是 `<workspace>/groups/<群号>`;同一群成员共用这个文件夹(`groupWorkspaceMode: shared`)。如果不想让群友互相看到文件,改成 `per-user`。
|
|
183
|
+
- **访客工具**:默认 `guestToolAccess: true`,所以访客也能用 shell/fs/web 等工具。DSH 会把**写入/删除**限制在各自文件夹,进程 `cwd` 也在那里;但**读取不受 DSH sandbox 限制**(见下面的“读隔离”)。
|
|
184
|
+
- `toolPermission: reject` 会禁用所有工具,只保留聊天;`allow` 对访客仍只给文件夹,不会给基础工作区。
|
|
185
|
+
- **读隔离(重要)**:DSH 当前只围栏写/删除与 cwd,不围栏读。访客进程与 Koishi 使用同一个 Windows 账户,因此能读取该账户可读的任何文件,包括基础 `workspace`、用户目录和 DSH 凭据。不要把密钥、`.env`、个人资料放在同一账户可读的位置;公开机器人应先设 `guestToolAccess: false`,或把 Koishi 放进独立 Windows 账户/虚拟机。真正的读隔离需要受限 MCP 文件工具或 AppContainer,当前版本尚未实现。
|
|
186
|
+
- **不要把基础 `workspace` 指向含密钥、`.env`、个人资料的目录**。owner 能访问整个基础工作区;访客的写入被限制,但读取仍受同一账户权限支配。
|
|
187
|
+
- 群共享文件夹的副作用:群友可以读写别人留下的文件;需要隐私时用 `per-user`,或只让 owner 使用。
|
|
188
|
+
- 访客可以发起耗时任务,目前没有每用户限流;公开群建议配合 Koishi 的频率限制或只开 `trustedGuilds`。
|
|
164
189
|
- 插件不接触 API Key;DSH 使用 `dshHome` 下已保存的凭据。
|
|
165
190
|
|
|
166
191
|
## 扩展接口
|
|
@@ -168,29 +193,45 @@ harness 帮我查一下今天的日期,用 pwsh
|
|
|
168
193
|
插件在 Koishi 上暴露 `ctx.dshHarness`,给以后单独的 Harness 配置插件使用:
|
|
169
194
|
|
|
170
195
|
```js
|
|
171
|
-
ctx.dshHarness.version
|
|
172
|
-
ctx.dshHarness.
|
|
173
|
-
ctx.dshHarness.
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
}
|
|
196
|
+
ctx.dshHarness.version // 2
|
|
197
|
+
ctx.dshHarness.toolPluginIds // DSH profile 里可被策略开关的工具插件 id
|
|
198
|
+
ctx.dshHarness.config // 当前配置
|
|
199
|
+
ctx.dshHarness.isOwner(session)
|
|
200
|
+
ctx.dshHarness.isTrusted(session) // 兼容别名,语义同 isOwner
|
|
201
|
+
|
|
202
|
+
ctx.dshHarness.resolveScope(session)
|
|
203
|
+
// { kind: 'owner'|'private'|'group', workspace, allowTools,
|
|
204
|
+
// approvePermissions, fullAccess, label, ... }
|
|
205
|
+
|
|
206
|
+
await ctx.dshHarness.runTask(task, { session, signal })
|
|
180
207
|
|
|
181
208
|
const off = ctx.dshHarness.registerPermissionProvider(async (session, task) => {
|
|
182
209
|
// 返回 false 拒绝;true/undefined 放行
|
|
183
210
|
})
|
|
184
211
|
|
|
185
212
|
const off2 = ctx.dshHarness.registerPostProcessor(async (text, context) => {
|
|
186
|
-
//
|
|
213
|
+
// context.scope 含本次工作区;可以改写 Harness 返回的文本
|
|
187
214
|
return text
|
|
188
215
|
})
|
|
189
216
|
|
|
217
|
+
const off3 = ctx.dshHarness.registerScopeResolver((session, base) => {
|
|
218
|
+
// 返回 { workspace, allowTools };访客的 workspace 不允许逃出 base
|
|
219
|
+
return null
|
|
220
|
+
})
|
|
221
|
+
|
|
222
|
+
const off4 = ctx.dshHarness.registerToolPolicy((session, scope) => {
|
|
223
|
+
// 未来 DSH 插件可在这里按私聊/群限制可用工具
|
|
224
|
+
return { enabledToolIds: ['tool-fs', 'tool-web'] }
|
|
225
|
+
// 或 { disabledToolIds: ['tool-pwsh'] }
|
|
226
|
+
})
|
|
227
|
+
|
|
190
228
|
ctx.dshHarness.registerTool('my_tool', myChatLunaTool)
|
|
191
229
|
```
|
|
192
230
|
|
|
193
|
-
`
|
|
231
|
+
- `registerScopeResolver` 在默认工作区计算之后执行,可覆盖 `workspace`/`allowTools`;访客返回 `base` 之外的绝对路径会抛错,owner 不受限。
|
|
232
|
+
- `registerToolPolicy` 可返回 `enabledToolIds`(白名单)或 `disabledToolIds`(黑名单);它影响的是 DSH profile 里的工具插件,不是 ChatLuna 工具。
|
|
233
|
+
- `registerPermissionProvider` 在每次任务前执行,任一返回 `false` 就抛错;`registerPostProcessor` 在结果返回前执行;`registerTool` 直接转给 `ChatLunaPlugin.registerTool`。
|
|
234
|
+
- 这些接口是 v2,后续尽量只增不改。
|
|
194
235
|
|
|
195
236
|
## 已知限制
|
|
196
237
|
|
|
@@ -199,6 +240,9 @@ ctx.dshHarness.registerTool('my_tool', myChatLunaTool)
|
|
|
199
240
|
- **关键词只看用户消息**:由前置 middleware 判断 `session.content`;引用、转发里出现关键词也可能触发。
|
|
200
241
|
- **工具模式需要原模型支持 tool calling**。如果原模型不支持,`harness` 工具不会被调用。
|
|
201
242
|
- **ChatLuna 的工具不会自动变成 DSH 工具**,反之亦然;两边工具是分开的。
|
|
243
|
+
- **群文件夹默认共享**:同群成员会看到同一个 `groups/<群号>` 下的文件;需要隔离时设 `groupWorkspaceMode: per-user`。
|
|
244
|
+
- **隔离依赖 DSH sandbox**:它只限制写入/删除与 cwd,不限制读取,也不是完整容器;对恶意用户仍建议独立账户/虚拟机。
|
|
245
|
+
- **目前没有每用户并发/频率限制**:ChatLuna 的全局并发限制仍生效,但单个用户连续触发会连续启动 DSH 进程;公开机器人建议在 Koishi 侧再加限流。
|
|
202
246
|
- **`{{char}}` 不会自动映射**(仅模型适配器模式相关):ChatLuna 变量表里是 `{{name}}` 和 `{{user}}`,未识别的 `{{char}}` 会渲染成空串。
|
|
203
247
|
- 目前只在 Windows + DSH 0.2.1-rc.5 + ChatLuna 1.4.4 + chatluna-character 0.0.235 上验证。
|
|
204
248
|
|
|
@@ -206,7 +250,8 @@ ctx.dshHarness.registerTool('my_tool', myChatLunaTool)
|
|
|
206
250
|
|
|
207
251
|
| 现象 | 处理 |
|
|
208
252
|
| --- | --- |
|
|
209
|
-
| `missing character_reply tool call` |
|
|
253
|
+
| `missing character_reply tool call` | 先确认 `chatluna-character` 的模型不是 `dsh/deepseek-harness`;工具模式下保持原模型,`toolCalling` 开启。若日志里已经有 `harness task done`,多半是下面的 `think` 问题。 |
|
|
254
|
+
| `invalid character_reply tool call ... required property "think"` | 在 `chatluna-character` 关闭「是否启用 `<think>` 标签」(`toolCallReplyThinkTag: false`),或换更强模型并在预设中强制带 `"think": ""`。 |
|
|
210
255
|
| `dsh exited with code 1`,stderr 是 `'dsh' 不是内部或外部命令` | 填桌面版 `executable` + `cliPath`,或 `dsh.cmd` 的完整路径。 |
|
|
211
256
|
| `no API key for provider route "deepseek-official"` | 设 `model` 为有凭据的模型,如 `deepseek-flash` 或 `deepseek-account/deepseek-flash`。 |
|
|
212
257
|
| `unknown model option: deepseek-flash` | 0.2.1 已支持裸模型名;若仍出现,说明 DSH 的模型列表里没有该模型,填 `deepseek-account/deepseek-flash` 或先跑 `dsh --profile acp --dump-config` 确认。 |
|
|
@@ -220,6 +265,9 @@ ctx.dshHarness.registerTool('my_tool', myChatLunaTool)
|
|
|
220
265
|
- 工具模式真实 DSH:消息 `#harness 现在几点?`,Harness 执行 `Get-Date -Format o`,返回 `2026-11-03T18:42:57.0117758+08:00`,耗时约 5.9 秒;无关键词时授权为 `false`。
|
|
221
266
|
- 模型适配器模式:注入 persona 后模型回答 `I am TestPersona, and my color is teal.`,`stopReason: end_turn`。
|
|
222
267
|
- 非可信用户写文件被禁用工具插件阻止;可信用户可正常创建文件。
|
|
268
|
+
- 0.3.0 隔离测试:访客私聊 `Get-Location` 返回 `D:\koishi-dsh-workspace\private\guest-1`;尝试写 `D:\koishi-dsh-workspace\escape-guest.txt` 被 sandbox/approval 拒绝,文件未创建。
|
|
269
|
+
- 0.3.0 owner 测试:`trustedUsers` 用户在群里仍使用基础 `D:\koishi-dsh-workspace`,`Get-Date` 正常返回。
|
|
270
|
+
- 0.3.0 读侧测试:访客能读取基础 workspace 里的测试文件;这不是 bug,而是 DSH sandbox 只围栏写/删除的已知限制(见“读隔离”)。
|
|
223
271
|
|
|
224
272
|
## License
|
|
225
273
|
|
package/lib/bridge.cjs
CHANGED
|
@@ -101,8 +101,22 @@ const TOOL_PLUGIN_IDS = [
|
|
|
101
101
|
'plan-mode'
|
|
102
102
|
]
|
|
103
103
|
|
|
104
|
-
function buildPatch(systemText, allowTools = true) {
|
|
104
|
+
function buildPatch(systemText, allowTools = true, toolPolicy = {}) {
|
|
105
105
|
const lines = []
|
|
106
|
+
const disabled = new Set((toolPolicy.disabledToolIds || []).map(String))
|
|
107
|
+
|
|
108
|
+
if (!allowTools) {
|
|
109
|
+
for (const id of TOOL_PLUGIN_IDS) disabled.add(id)
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// A future DSH-side plugin can supply an allow-list ("this private chat may
|
|
113
|
+
// only use fs and web") instead of a deny-list.
|
|
114
|
+
if (allowTools && Array.isArray(toolPolicy.enabledToolIds)) {
|
|
115
|
+
const enabled = new Set(toolPolicy.enabledToolIds.map(String))
|
|
116
|
+
for (const id of TOOL_PLUGIN_IDS) {
|
|
117
|
+
if (!enabled.has(id)) disabled.add(id)
|
|
118
|
+
}
|
|
119
|
+
}
|
|
106
120
|
|
|
107
121
|
if (systemText && systemText.trim()) {
|
|
108
122
|
lines.push(
|
|
@@ -114,10 +128,17 @@ function buildPatch(systemText, allowTools = true) {
|
|
|
114
128
|
)
|
|
115
129
|
}
|
|
116
130
|
|
|
117
|
-
if (
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
131
|
+
if (toolPolicy.workspaceRoot) {
|
|
132
|
+
lines.push(
|
|
133
|
+
'- id: sandbox-policy',
|
|
134
|
+
" name: '@deepseek-ai/dsh-sandbox-policy'",
|
|
135
|
+
' config:',
|
|
136
|
+
` workspaceRoot: ${JSON.stringify(String(toolPolicy.workspaceRoot))}`
|
|
137
|
+
)
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
for (const id of disabled) {
|
|
141
|
+
lines.push(`- id: ${id}`, ' disabled: true')
|
|
121
142
|
}
|
|
122
143
|
|
|
123
144
|
if (lines.length === 0) return ''
|
|
@@ -223,7 +244,15 @@ function resolveModelValue(session, model) {
|
|
|
223
244
|
return match.value
|
|
224
245
|
}
|
|
225
246
|
|
|
226
|
-
function runAcp({
|
|
247
|
+
function runAcp({
|
|
248
|
+
systemText,
|
|
249
|
+
promptText,
|
|
250
|
+
config,
|
|
251
|
+
allowTools = false,
|
|
252
|
+
approvePermissions = allowTools,
|
|
253
|
+
signal,
|
|
254
|
+
toolPolicy = {}
|
|
255
|
+
}) {
|
|
227
256
|
return new Promise((resolve, reject) => {
|
|
228
257
|
if (!promptText || !promptText.trim()) {
|
|
229
258
|
reject(new Error('DSH adapter: empty prompt'))
|
|
@@ -247,7 +276,7 @@ function runAcp({ systemText, promptText, config, allowTools = false, signal })
|
|
|
247
276
|
// With no ChatLuna system message, a chat-only request would otherwise
|
|
248
277
|
// inherit DSH's coding-agent persona and may print raw tool-call text.
|
|
249
278
|
const persona = (systemText && systemText.trim()) || (!allowTools ? 'You are a helpful assistant.' : '')
|
|
250
|
-
const patchContent = buildPatch(persona, allowTools)
|
|
279
|
+
const patchContent = buildPatch(persona, allowTools, { ...toolPolicy, workspaceRoot: cwd })
|
|
251
280
|
if (patchContent) {
|
|
252
281
|
patchDir = fs.mkdtempSync(path.join(os.tmpdir(), 'dsh-chatluna-'))
|
|
253
282
|
patchPath = path.join(patchDir, 'persona.patch.yml')
|
|
@@ -335,7 +364,7 @@ function runAcp({ systemText, promptText, config, allowTools = false, signal })
|
|
|
335
364
|
// Server -> client request (permission prompts, fs, terminal, ...).
|
|
336
365
|
if (message.id != null && message.method) {
|
|
337
366
|
if (message.method === 'session/request_permission') {
|
|
338
|
-
const optionId =
|
|
367
|
+
const optionId = approvePermissions ? 'allow-once' : 'reject-once'
|
|
339
368
|
send({
|
|
340
369
|
jsonrpc: '2.0',
|
|
341
370
|
id: message.id,
|
|
@@ -460,6 +489,7 @@ function runAcp({ systemText, promptText, config, allowTools = false, signal })
|
|
|
460
489
|
}
|
|
461
490
|
|
|
462
491
|
module.exports = {
|
|
492
|
+
TOOL_PLUGIN_IDS,
|
|
463
493
|
buildPatch,
|
|
464
494
|
buildPrompt,
|
|
465
495
|
contentToText,
|
package/lib/index.cjs
CHANGED
|
@@ -10,12 +10,12 @@ const { ModelType, ModelCapabilities } = require('koishi-plugin-chatluna/llm-cor
|
|
|
10
10
|
const { ChatLunaError, ChatLunaErrorCode } = require('koishi-plugin-chatluna/utils/error')
|
|
11
11
|
const { ChatGenerationChunk } = require('@langchain/core/outputs')
|
|
12
12
|
const { AIMessageChunk } = require('@langchain/core/messages')
|
|
13
|
-
const { runAcp, buildPrompt } = require('./bridge.cjs')
|
|
13
|
+
const { runAcp, buildPrompt, TOOL_PLUGIN_IDS } = require('./bridge.cjs')
|
|
14
|
+
const { isOwner, resolveScope } = require('./scope.cjs')
|
|
14
15
|
const {
|
|
15
16
|
DEFAULT_KEYWORDS,
|
|
16
17
|
createHarnessRuntime,
|
|
17
18
|
createHarnessTool,
|
|
18
|
-
isTrustedSession,
|
|
19
19
|
markArmed,
|
|
20
20
|
matchesKeywords,
|
|
21
21
|
normalizeKeywords,
|
|
@@ -25,19 +25,22 @@ const {
|
|
|
25
25
|
const PLATFORM = 'dsh'
|
|
26
26
|
const MODEL = 'deepseek-harness'
|
|
27
27
|
|
|
28
|
-
function
|
|
28
|
+
function sessionFromVariables(variables) {
|
|
29
29
|
const built = (variables && variables.built) || {}
|
|
30
|
-
const userId = built.userId == null ? '' : String(built.userId)
|
|
31
30
|
const guildId = built.guildId == null ? '' : String(built.guildId)
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
31
|
+
return {
|
|
32
|
+
userId: built.userId == null ? '' : String(built.userId),
|
|
33
|
+
guildId,
|
|
34
|
+
platform: built.chatPlatform || 'qq',
|
|
35
|
+
isDirect: !guildId
|
|
36
|
+
}
|
|
35
37
|
}
|
|
36
38
|
|
|
37
39
|
class DshRequester extends ModelRequester {
|
|
38
|
-
constructor(ctx, configPool, pluginConfig, plugin) {
|
|
40
|
+
constructor(ctx, configPool, pluginConfig, plugin, scopeResolvers = []) {
|
|
39
41
|
super(ctx, configPool, pluginConfig, plugin)
|
|
40
42
|
this.logger = ctx.logger('chatluna-dsh-adapter')
|
|
43
|
+
this._scopeResolvers = scopeResolvers
|
|
41
44
|
}
|
|
42
45
|
|
|
43
46
|
async *completionStreamInternal(params) {
|
|
@@ -48,23 +51,23 @@ class DshRequester extends ModelRequester {
|
|
|
48
51
|
|
|
49
52
|
const config = this._pluginConfig
|
|
50
53
|
const built = (params.variables && params.variables.built) || {}
|
|
51
|
-
const
|
|
52
|
-
|
|
53
|
-
(config.toolPermission === 'trusted' && isTrusted(config, params.variables))
|
|
54
|
+
const session = sessionFromVariables(params.variables)
|
|
55
|
+
const scope = resolveScope(config, session, this._scopeResolvers)
|
|
54
56
|
|
|
55
57
|
let result
|
|
56
58
|
try {
|
|
57
59
|
result = await runAcp({
|
|
58
60
|
systemText,
|
|
59
61
|
promptText,
|
|
60
|
-
allowTools,
|
|
62
|
+
allowTools: scope.allowTools,
|
|
63
|
+
approvePermissions: scope.approvePermissions,
|
|
61
64
|
signal: params.signal,
|
|
62
65
|
config: {
|
|
63
66
|
executable: config.executable,
|
|
64
67
|
cliPath: config.cliPath,
|
|
65
68
|
profile: config.profile,
|
|
66
69
|
dshHome: config.dshHome,
|
|
67
|
-
workspace:
|
|
70
|
+
workspace: scope.workspace,
|
|
68
71
|
model: config.model,
|
|
69
72
|
requestTimeout: config.requestTimeout
|
|
70
73
|
}
|
|
@@ -91,11 +94,11 @@ class DshRequester extends ModelRequester {
|
|
|
91
94
|
}
|
|
92
95
|
|
|
93
96
|
class DshClient extends PlatformModelClient {
|
|
94
|
-
constructor(ctx, config, plugin) {
|
|
97
|
+
constructor(ctx, config, plugin, scopeResolvers = []) {
|
|
95
98
|
super(ctx, plugin.platformConfigPool)
|
|
96
99
|
this._config = config
|
|
97
100
|
this.plugin = plugin
|
|
98
|
-
this._requester = new DshRequester(ctx, plugin.platformConfigPool, config, plugin)
|
|
101
|
+
this._requester = new DshRequester(ctx, plugin.platformConfigPool, config, plugin, scopeResolvers)
|
|
99
102
|
}
|
|
100
103
|
|
|
101
104
|
platform = PLATFORM
|
|
@@ -155,7 +158,16 @@ function apply(ctx, config) {
|
|
|
155
158
|
const logger = ctx.logger('chatluna-dsh-adapter')
|
|
156
159
|
const permissionProviders = []
|
|
157
160
|
const postProcessors = []
|
|
158
|
-
const
|
|
161
|
+
const scopeResolvers = []
|
|
162
|
+
const toolPolicies = []
|
|
163
|
+
const runtime = createHarnessRuntime({
|
|
164
|
+
config,
|
|
165
|
+
logger,
|
|
166
|
+
permissionProviders,
|
|
167
|
+
postProcessors,
|
|
168
|
+
scopeResolvers,
|
|
169
|
+
toolPolicies
|
|
170
|
+
})
|
|
159
171
|
|
|
160
172
|
const plugin = new ChatLunaPlugin(ctx, config, PLATFORM)
|
|
161
173
|
plugin.parseConfig(() => [
|
|
@@ -168,7 +180,7 @@ function apply(ctx, config) {
|
|
|
168
180
|
concurrentMaxSize: config.chatConcurrentMaxSize
|
|
169
181
|
}
|
|
170
182
|
])
|
|
171
|
-
plugin.registerClient(() => new DshClient(ctx, config, plugin))
|
|
183
|
+
plugin.registerClient(() => new DshClient(ctx, config, plugin, scopeResolvers))
|
|
172
184
|
|
|
173
185
|
if (config.toolEnabled !== false) {
|
|
174
186
|
const harnessTool = createHarnessTool({ config, logger, runtime, name: config.toolName })
|
|
@@ -183,9 +195,9 @@ function apply(ctx, config) {
|
|
|
183
195
|
config.toolPermission
|
|
184
196
|
)
|
|
185
197
|
}
|
|
186
|
-
if (config.toolPermission === 'trusted' &&
|
|
187
|
-
logger.
|
|
188
|
-
'chatluna-dsh-adapter:
|
|
198
|
+
if (config.toolPermission === 'trusted' && config.guestToolAccess !== false) {
|
|
199
|
+
logger.info(
|
|
200
|
+
'chatluna-dsh-adapter: guests may use harness inside their own folder; trustedUsers get the base workspace and full tools.'
|
|
189
201
|
)
|
|
190
202
|
}
|
|
191
203
|
}
|
|
@@ -193,10 +205,13 @@ function apply(ctx, config) {
|
|
|
193
205
|
// Extension surface for a future "harness" plugin. Stable-ish: keep the
|
|
194
206
|
// shape, add fields instead of renaming them.
|
|
195
207
|
const harnessApi = {
|
|
196
|
-
version:
|
|
208
|
+
version: 2,
|
|
209
|
+
toolPluginIds: [...TOOL_PLUGIN_IDS],
|
|
197
210
|
config,
|
|
198
211
|
logger,
|
|
199
|
-
|
|
212
|
+
isOwner: (session) => isOwner(config, session),
|
|
213
|
+
isTrusted: (session) => isOwner(config, session),
|
|
214
|
+
resolveScope: (session) => resolveScope(config, session, scopeResolvers),
|
|
200
215
|
runTask: (task, options = {}) => runtime.runTask(task, options),
|
|
201
216
|
registerPermissionProvider(provider) {
|
|
202
217
|
permissionProviders.push(provider)
|
|
@@ -212,6 +227,20 @@ function apply(ctx, config) {
|
|
|
212
227
|
if (index >= 0) postProcessors.splice(index, 1)
|
|
213
228
|
}
|
|
214
229
|
},
|
|
230
|
+
registerScopeResolver(resolver) {
|
|
231
|
+
scopeResolvers.push(resolver)
|
|
232
|
+
return () => {
|
|
233
|
+
const index = scopeResolvers.indexOf(resolver)
|
|
234
|
+
if (index >= 0) scopeResolvers.splice(index, 1)
|
|
235
|
+
}
|
|
236
|
+
},
|
|
237
|
+
registerToolPolicy(policy) {
|
|
238
|
+
toolPolicies.push(policy)
|
|
239
|
+
return () => {
|
|
240
|
+
const index = toolPolicies.indexOf(policy)
|
|
241
|
+
if (index >= 0) toolPolicies.splice(index, 1)
|
|
242
|
+
}
|
|
243
|
+
},
|
|
215
244
|
registerTool(name, tool) {
|
|
216
245
|
plugin.registerTool(name, tool)
|
|
217
246
|
}
|
|
@@ -237,25 +266,38 @@ const Config = Schema.intersect([
|
|
|
237
266
|
.description('DSH_HOME;留空继承 Koishi 进程环境(通常是 `~/.dsh`)。'),
|
|
238
267
|
workspace: Schema.string()
|
|
239
268
|
.default('')
|
|
240
|
-
.description(
|
|
269
|
+
.description(
|
|
270
|
+
'基础工作目录。trustedUsers 直接使用它;其他私聊用 `<workspace>/private/<QQ号>`,群聊用 `<workspace>/groups/<群号>`。留空使用 `<koishi baseDir>/data/chatluna-dsh`。'
|
|
271
|
+
),
|
|
272
|
+
groupWorkspaceMode: Schema.union([
|
|
273
|
+
Schema.const('shared').description('每个群一个共享文件夹,所有群成员共用(默认)。'),
|
|
274
|
+
Schema.const('per-user').description('每个群成员在群文件夹下再分自己的子文件夹。')
|
|
275
|
+
])
|
|
276
|
+
.default('shared')
|
|
277
|
+
.description('群聊工作区隔离粒度。'),
|
|
241
278
|
model: Schema.string()
|
|
242
279
|
.default('')
|
|
243
280
|
.description(
|
|
244
281
|
'DSH 模型,可填 `deepseek-flash`、`deepseek-account/deepseek-flash` 或 `["deepseek-account","deepseek-flash"]`;适配器会按 session 的模型选项自动解析。留空使用 DSH 默认路由(缺凭据时会失败)。'
|
|
245
282
|
),
|
|
246
283
|
toolPermission: Schema.union([
|
|
247
|
-
Schema.const('allow').description('
|
|
248
|
-
Schema.const('trusted').description('
|
|
284
|
+
Schema.const('allow').description('所有人可用工具;访客仍限制在自己的文件夹。'),
|
|
285
|
+
Schema.const('trusted').description('trustedUsers 用基础工作区+全部工具;访客按 guestToolAccess 决定是否可用工具。'),
|
|
249
286
|
Schema.const('reject').description('所有人禁用全部工具,只保留聊天。')
|
|
250
287
|
])
|
|
251
288
|
.default('trusted')
|
|
252
|
-
.description('
|
|
289
|
+
.description('工具总开关。访客的隔离由 workspace/sandbox 决定,不靠隐藏工具。'),
|
|
290
|
+
guestToolAccess: Schema.boolean()
|
|
291
|
+
.default(true)
|
|
292
|
+
.description(
|
|
293
|
+
'非 trustedUsers 是否也能使用 DSH 工具。注意:DSH 只限制写入/删除与 cwd,不限制读取;开启前请阅读 README 的“读隔离”一节。'
|
|
294
|
+
),
|
|
253
295
|
trustedUsers: Schema.array(Schema.string())
|
|
254
296
|
.default([])
|
|
255
|
-
.description('
|
|
297
|
+
.description('完全信任的 Koishi userId:使用基础工作区,拥有全部工具;在群里也按此处理。'),
|
|
256
298
|
trustedGuilds: Schema.array(Schema.string())
|
|
257
299
|
.default([])
|
|
258
|
-
.description('
|
|
300
|
+
.description('允许使用 harness 的群 guildId(不授予基础工作区,仍用群文件夹)。'),
|
|
259
301
|
requestTimeout: Schema.number()
|
|
260
302
|
.default(180000)
|
|
261
303
|
.description('单次 DSH 请求超时(毫秒)。'),
|
|
@@ -274,6 +316,11 @@ const Config = Schema.intersect([
|
|
|
274
316
|
toolSystemPrompt: Schema.string()
|
|
275
317
|
.default('')
|
|
276
318
|
.description('Harness 任务执行器的系统提示词;留空使用内置的简洁执行器提示。'),
|
|
319
|
+
characterThinkTagReminder: Schema.boolean()
|
|
320
|
+
.default(true)
|
|
321
|
+
.description(
|
|
322
|
+
'⚠️ 仅提醒,不影响运行:若 chatluna-character 开启了「是否启用 <think> 标签」(toolCallReplyThinkTag),它的 character_reply 会强制要求 think 字段;deepseek-flash 可能漏掉,表现为 missing/invalid character_reply。请在 character 中关闭该选项,或换更强模型并在预设中要求每次带 think。'
|
|
323
|
+
),
|
|
277
324
|
debug: Schema.boolean()
|
|
278
325
|
.default(false)
|
|
279
326
|
.description('打开后输出关键词匹配、授权、任务耗时等调试日志。')
|
package/lib/scope.cjs
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const path = require('node:path')
|
|
4
|
+
|
|
5
|
+
function normalizeId(value) {
|
|
6
|
+
return value == null ? '' : String(value)
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Turn a QQ id into a single, safe path segment. We keep dots for readability
|
|
11
|
+
* but never allow a segment that is exactly "." or ".."; anything else unusual
|
|
12
|
+
* becomes "_". This is deliberately stricter than the platform ids we expect.
|
|
13
|
+
*/
|
|
14
|
+
function safeSegment(value, fallback = 'unknown') {
|
|
15
|
+
let segment = normalizeId(value).trim()
|
|
16
|
+
segment = segment.replace(/[^A-Za-z0-9._-]+/g, '_')
|
|
17
|
+
if (!segment || segment === '.' || segment === '..') segment = fallback
|
|
18
|
+
if (segment.length > 80) segment = segment.slice(0, 80)
|
|
19
|
+
return segment
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function conversationKey(session) {
|
|
23
|
+
if (session == null) return ''
|
|
24
|
+
const platform = session.platform || 'unknown'
|
|
25
|
+
const selfId = session.selfId || ''
|
|
26
|
+
if (isDirectSession(session)) {
|
|
27
|
+
return platform + ':' + selfId + ':private:' + (session.userId || '')
|
|
28
|
+
}
|
|
29
|
+
return platform + ':' + selfId + ':guild:' + (session.guildId || session.channelId || '')
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function isDirectSession(session) {
|
|
33
|
+
if (session == null) return true
|
|
34
|
+
if (session.isDirect === true) return true
|
|
35
|
+
return !session.guildId
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function hasUser(config, session) {
|
|
39
|
+
const userId = normalizeId(session && session.userId)
|
|
40
|
+
return Boolean(userId) && (config.trustedUsers || []).map(normalizeId).includes(userId)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function isOwner(config, session) {
|
|
44
|
+
return hasUser(config, session)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function isTrustedGroup(config, session) {
|
|
48
|
+
const guildId = normalizeId(session && session.guildId)
|
|
49
|
+
return Boolean(guildId) && (config.trustedGuilds || []).map(normalizeId).includes(guildId)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function isInside(parent, child) {
|
|
53
|
+
const relative = path.relative(parent, child)
|
|
54
|
+
return relative === '' || (!relative.startsWith('..') && !path.isAbsolute(relative))
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function defaultWorkspaceFor(config, session, base) {
|
|
58
|
+
const userId = safeSegment(session && session.userId)
|
|
59
|
+
if (isDirectSession(session)) {
|
|
60
|
+
return path.join(base, 'private', userId)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const guildId = safeSegment(session && session.guildId, 'unknown-group')
|
|
64
|
+
const groupRoot = path.join(base, 'groups', guildId)
|
|
65
|
+
if (config.groupWorkspaceMode === 'per-user') {
|
|
66
|
+
return path.join(groupRoot, userId)
|
|
67
|
+
}
|
|
68
|
+
return groupRoot
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Decide the filesystem scope for one conversation.
|
|
73
|
+
*
|
|
74
|
+
* Owners (trustedUsers) always get the base workspace and every tool. Everyone
|
|
75
|
+
* else gets:
|
|
76
|
+
* - private/<QQ号> for a private chat
|
|
77
|
+
* - groups/<群号> for a group (shared by default)
|
|
78
|
+
* - groups/<群号>/<QQ号> when groupWorkspaceMode === 'per-user'
|
|
79
|
+
*
|
|
80
|
+
* The DSH process is started with this folder as its cwd. The acp profile's
|
|
81
|
+
* sandbox-policy uses process.cwd() as workspaceRoot, so this also becomes the
|
|
82
|
+
* root for the DSH filesystem/shell sandbox.
|
|
83
|
+
*/
|
|
84
|
+
function resolveScope(config, session, resolvers = []) {
|
|
85
|
+
const base = path.resolve(config.workspace || process.cwd())
|
|
86
|
+
const userId = normalizeId(session && session.userId)
|
|
87
|
+
const guildId = normalizeId(session && session.guildId)
|
|
88
|
+
const owner = isOwner(config, session)
|
|
89
|
+
const direct = isDirectSession(session)
|
|
90
|
+
const kind = owner ? 'owner' : direct ? 'private' : 'group'
|
|
91
|
+
const label = kind + ':' + (owner || direct ? userId : guildId)
|
|
92
|
+
|
|
93
|
+
let workspace = owner ? base : defaultWorkspaceFor(config, session, base)
|
|
94
|
+
let allowTools =
|
|
95
|
+
config.toolPermission === 'reject'
|
|
96
|
+
? false
|
|
97
|
+
: config.toolPermission === 'allow'
|
|
98
|
+
? true
|
|
99
|
+
: owner || config.guestToolAccess !== false || isTrustedGroup(config, session)
|
|
100
|
+
|
|
101
|
+
for (const resolver of resolvers) {
|
|
102
|
+
if (typeof resolver !== 'function') continue
|
|
103
|
+
const custom = resolver(session, { base, owner, direct, userId, guildId, kind })
|
|
104
|
+
if (!custom) continue
|
|
105
|
+
if (custom.workspace) {
|
|
106
|
+
const candidate = path.isAbsolute(String(custom.workspace))
|
|
107
|
+
? path.resolve(String(custom.workspace))
|
|
108
|
+
: path.resolve(base, String(custom.workspace))
|
|
109
|
+
if (!owner && !isInside(base, candidate)) {
|
|
110
|
+
throw new Error('harness scope: workspace resolver escaped the base workspace')
|
|
111
|
+
}
|
|
112
|
+
workspace = candidate
|
|
113
|
+
}
|
|
114
|
+
if (custom.allowTools != null) allowTools = Boolean(custom.allowTools)
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
return {
|
|
118
|
+
key: conversationKey(session),
|
|
119
|
+
kind,
|
|
120
|
+
label,
|
|
121
|
+
owner,
|
|
122
|
+
fullAccess: owner,
|
|
123
|
+
// Only the owner may approve DSH's "this operation leaves the workspace"
|
|
124
|
+
// escalation prompts. Guests still get tools, but the sandbox stays shut.
|
|
125
|
+
approvePermissions: owner,
|
|
126
|
+
direct,
|
|
127
|
+
userId,
|
|
128
|
+
guildId,
|
|
129
|
+
workspace,
|
|
130
|
+
allowTools
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
module.exports = {
|
|
135
|
+
conversationKey,
|
|
136
|
+
defaultWorkspaceFor,
|
|
137
|
+
isDirectSession,
|
|
138
|
+
isInside,
|
|
139
|
+
isOwner,
|
|
140
|
+
isTrustedGroup,
|
|
141
|
+
normalizeId,
|
|
142
|
+
resolveScope,
|
|
143
|
+
safeSegment
|
|
144
|
+
}
|
package/lib/tool.cjs
CHANGED
|
@@ -2,6 +2,13 @@
|
|
|
2
2
|
|
|
3
3
|
const { tool } = require('@langchain/core/tools')
|
|
4
4
|
const { runAcp, contentToText } = require('./bridge.cjs')
|
|
5
|
+
const {
|
|
6
|
+
conversationKey,
|
|
7
|
+
isOwner,
|
|
8
|
+
isTrustedGroup,
|
|
9
|
+
normalizeId,
|
|
10
|
+
resolveScope
|
|
11
|
+
} = require('./scope.cjs')
|
|
5
12
|
|
|
6
13
|
const DEFAULT_KEYWORDS = ['#harness', 'harness', '用harness', '调用harness']
|
|
7
14
|
const DEFAULT_SYSTEM_PROMPT = [
|
|
@@ -42,19 +49,14 @@ function matchesKeywords(text, keywords) {
|
|
|
42
49
|
return keywords.some((keyword) => lower.includes(keyword.toLowerCase()))
|
|
43
50
|
}
|
|
44
51
|
|
|
45
|
-
function normalizeId(value) {
|
|
46
|
-
return value == null ? '' : String(value)
|
|
47
|
-
}
|
|
48
|
-
|
|
49
52
|
function isTrustedSession(config, session) {
|
|
50
53
|
if (session == null) return false
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
return false
|
|
54
|
+
return (
|
|
55
|
+
isOwner(config, session) ||
|
|
56
|
+
isTrustedGroup(config, session) ||
|
|
57
|
+
((config.trustedUsers || []).map(normalizeId).includes(normalizeId(session.userId)) &&
|
|
58
|
+
Boolean(normalizeId(session.userId)))
|
|
59
|
+
)
|
|
58
60
|
}
|
|
59
61
|
|
|
60
62
|
function hasTrustedList(config) {
|
|
@@ -70,14 +72,6 @@ function hasTrustedList(config) {
|
|
|
70
72
|
*/
|
|
71
73
|
const armedConversations = new Map()
|
|
72
74
|
|
|
73
|
-
function conversationKey(session) {
|
|
74
|
-
if (session == null) return ''
|
|
75
|
-
const platform = session.platform || 'unknown'
|
|
76
|
-
const selfId = session.selfId || ''
|
|
77
|
-
if (session.isDirect) return platform + ':' + selfId + ':private:' + (session.userId || '')
|
|
78
|
-
return platform + ':' + selfId + ':guild:' + (session.guildId || session.channelId || '')
|
|
79
|
-
}
|
|
80
|
-
|
|
81
75
|
function markArmed(session) {
|
|
82
76
|
const key = conversationKey(session)
|
|
83
77
|
if (!key) return
|
|
@@ -106,18 +100,30 @@ function isArmed(session) {
|
|
|
106
100
|
|
|
107
101
|
|
|
108
102
|
/**
|
|
109
|
-
*
|
|
110
|
-
*
|
|
111
|
-
*
|
|
103
|
+
* Whether this conversation may expose the harness tool at all. Tool access is
|
|
104
|
+
* no longer the same thing as filesystem access:
|
|
105
|
+
* - trustedUsers are owners: base workspace + all tools
|
|
106
|
+
* - a private guest gets private/<QQ号> + tools (guestToolAccess)
|
|
107
|
+
* - a group guest gets groups/<群号> + tools (guestToolAccess)
|
|
108
|
+
* Filesystem isolation is enforced by the DSH sandbox workspaceRoot, not by
|
|
109
|
+
* hiding the tool.
|
|
112
110
|
*/
|
|
113
111
|
function isAuthorized(config, session) {
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
112
|
+
try {
|
|
113
|
+
return resolveScope(config, session).allowTools
|
|
114
|
+
} catch (error) {
|
|
115
|
+
return false
|
|
116
|
+
}
|
|
118
117
|
}
|
|
119
118
|
|
|
120
|
-
function createHarnessRuntime({
|
|
119
|
+
function createHarnessRuntime({
|
|
120
|
+
config,
|
|
121
|
+
logger,
|
|
122
|
+
permissionProviders = [],
|
|
123
|
+
postProcessors = [],
|
|
124
|
+
scopeResolvers = [],
|
|
125
|
+
toolPolicies = []
|
|
126
|
+
}) {
|
|
121
127
|
const debug = (...args) => {
|
|
122
128
|
if (config.debug && logger) logger.info(...args)
|
|
123
129
|
}
|
|
@@ -125,15 +131,34 @@ function createHarnessRuntime({ config, logger, permissionProviders = [], postPr
|
|
|
125
131
|
async function runTask(task, options = {}) {
|
|
126
132
|
const session = options.session
|
|
127
133
|
const started = Date.now()
|
|
128
|
-
const
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
134
|
+
const scope = resolveScope(config, session, scopeResolvers)
|
|
135
|
+
// The extension surface may narrow access, but it must never widen a
|
|
136
|
+
// guest's scope. Widening belongs in a scope resolver.
|
|
137
|
+
const allowTools = scope.allowTools && options.allowTools !== false
|
|
138
|
+
|
|
139
|
+
const toolPolicy = { disabledToolIds: [], enabledToolIds: null }
|
|
140
|
+
for (const policy of toolPolicies) {
|
|
141
|
+
if (typeof policy !== 'function') continue
|
|
142
|
+
let value
|
|
143
|
+
try {
|
|
144
|
+
value = await policy(session, scope, { config })
|
|
145
|
+
} catch (error) {
|
|
146
|
+
if (logger) logger.warn('harness tool policy failed: %s', error && error.message)
|
|
147
|
+
continue
|
|
148
|
+
}
|
|
149
|
+
if (!value) continue
|
|
150
|
+
if (Array.isArray(value.disabledToolIds)) {
|
|
151
|
+
toolPolicy.disabledToolIds.push(...value.disabledToolIds.map(String))
|
|
152
|
+
}
|
|
153
|
+
if (Array.isArray(value.enabledToolIds)) {
|
|
154
|
+
toolPolicy.enabledToolIds = value.enabledToolIds.map(String)
|
|
155
|
+
}
|
|
156
|
+
}
|
|
132
157
|
|
|
133
158
|
for (const provider of permissionProviders) {
|
|
134
159
|
let allowed
|
|
135
160
|
try {
|
|
136
|
-
allowed = await provider(session, task, { config })
|
|
161
|
+
allowed = await provider(session, task, { config, scope })
|
|
137
162
|
} catch (error) {
|
|
138
163
|
if (logger) logger.warn('harness permission provider failed: %s', error && error.message)
|
|
139
164
|
allowed = false
|
|
@@ -148,6 +173,10 @@ function createHarnessRuntime({ config, logger, permissionProviders = [], postPr
|
|
|
148
173
|
(session && session.userId) +
|
|
149
174
|
' guild=' +
|
|
150
175
|
(session && session.guildId) +
|
|
176
|
+
' scope=' +
|
|
177
|
+
scope.label +
|
|
178
|
+
' workspace=' +
|
|
179
|
+
scope.workspace +
|
|
151
180
|
' tools=' +
|
|
152
181
|
allowTools +
|
|
153
182
|
' task=' +
|
|
@@ -160,13 +189,15 @@ function createHarnessRuntime({ config, logger, permissionProviders = [], postPr
|
|
|
160
189
|
systemText: config.toolSystemPrompt || DEFAULT_SYSTEM_PROMPT,
|
|
161
190
|
promptText: task,
|
|
162
191
|
allowTools,
|
|
192
|
+
approvePermissions: scope.approvePermissions,
|
|
163
193
|
signal: options.signal,
|
|
194
|
+
toolPolicy,
|
|
164
195
|
config: {
|
|
165
196
|
executable: config.executable,
|
|
166
197
|
cliPath: config.cliPath,
|
|
167
198
|
profile: config.profile,
|
|
168
199
|
dshHome: config.dshHome,
|
|
169
|
-
workspace:
|
|
200
|
+
workspace: scope.workspace,
|
|
170
201
|
model: config.model,
|
|
171
202
|
requestTimeout: config.requestTimeout
|
|
172
203
|
}
|
|
@@ -188,7 +219,7 @@ function createHarnessRuntime({ config, logger, permissionProviders = [], postPr
|
|
|
188
219
|
let text = result.text || ''
|
|
189
220
|
for (const processor of postProcessors) {
|
|
190
221
|
try {
|
|
191
|
-
const next = await processor(text, { result, session, config, task })
|
|
222
|
+
const next = await processor(text, { result, session, config, task, scope })
|
|
192
223
|
if (typeof next === 'string') text = next
|
|
193
224
|
} catch (error) {
|
|
194
225
|
if (logger) logger.warn('harness post processor failed: %s', error && error.message)
|
|
@@ -203,10 +234,12 @@ function createHarnessRuntime({ config, logger, permissionProviders = [], postPr
|
|
|
203
234
|
' chars=' +
|
|
204
235
|
text.length +
|
|
205
236
|
' tools=' +
|
|
206
|
-
allowTools
|
|
237
|
+
allowTools +
|
|
238
|
+
' scope=' +
|
|
239
|
+
scope.label
|
|
207
240
|
)
|
|
208
241
|
|
|
209
|
-
return { ...result, text, allowTools, elapsedMs: Date.now() - started }
|
|
242
|
+
return { ...result, text, allowTools, scope, elapsedMs: Date.now() - started }
|
|
210
243
|
}
|
|
211
244
|
|
|
212
245
|
return { runTask }
|
|
@@ -299,6 +332,7 @@ module.exports = {
|
|
|
299
332
|
createHarnessTool,
|
|
300
333
|
isAuthorized,
|
|
301
334
|
isArmed,
|
|
335
|
+
isOwner,
|
|
302
336
|
isTrustedSession,
|
|
303
337
|
lastHumanText,
|
|
304
338
|
markArmed,
|
package/package.json
CHANGED
|
@@ -1,57 +1,57 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "koishi-plugin-chatluna-dsh-adapter",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "DeepSeek Harness tools for ChatLuna: keyword-gated Harness tool for the normal model, plus an optional DSH model adapter. ChatLuna keeps its presets, character pipeline, and QQ rendering.",
|
|
5
|
-
"main": "lib/index.cjs",
|
|
6
|
-
"files": [
|
|
7
|
-
"lib",
|
|
8
|
-
"README.md"
|
|
9
|
-
],
|
|
10
|
-
"keywords": [
|
|
11
|
-
"chatbot",
|
|
12
|
-
"koishi",
|
|
13
|
-
"chatluna",
|
|
14
|
-
"deepseek",
|
|
15
|
-
"harness",
|
|
16
|
-
"adapter",
|
|
17
|
-
"qq"
|
|
18
|
-
],
|
|
19
|
-
"license": "MIT",
|
|
20
|
-
"author": "Kun-Fox",
|
|
21
|
-
"contributors": [
|
|
22
|
-
"Kun-Fox"
|
|
23
|
-
],
|
|
24
|
-
"repository": {
|
|
25
|
-
"type": "git",
|
|
26
|
-
"url": "git+https://github.com/Kun-Fox/koishi-plugin-chatluna-dsh-adapter.git"
|
|
27
|
-
},
|
|
28
|
-
"homepage": "https://github.com/Kun-Fox/koishi-plugin-chatluna-dsh-adapter#readme",
|
|
29
|
-
"bugs": {
|
|
30
|
-
"url": "https://github.com/Kun-Fox/koishi-plugin-chatluna-dsh-adapter/issues"
|
|
31
|
-
},
|
|
32
|
-
"publishConfig": {
|
|
33
|
-
"registry": "https://registry.npmjs.org/"
|
|
34
|
-
},
|
|
35
|
-
"engines": {
|
|
36
|
-
"node": ">=18.0.0"
|
|
37
|
-
},
|
|
38
|
-
"dependencies": {
|
|
39
|
-
"@langchain/core": "^0.3.80"
|
|
40
|
-
},
|
|
41
|
-
"peerDependencies": {
|
|
42
|
-
"koishi": "^4.18.9",
|
|
43
|
-
"koishi-plugin-chatluna": "^1.4.4"
|
|
44
|
-
},
|
|
45
|
-
"koishi": {
|
|
46
|
-
"category": "ai",
|
|
47
|
-
"description": {
|
|
48
|
-
"zh": "给 ChatLuna 增加一个关键词触发的 DeepSeek Harness 工具:普通聊天仍走原模型和原预设,只有用户说 #harness 时才把任务交给 Harness
|
|
49
|
-
"en": "Adds a keyword-gated DeepSeek Harness tool to ChatLuna while keeping the normal model, presets, and character pipeline.
|
|
50
|
-
},
|
|
51
|
-
"service": {
|
|
52
|
-
"required": [
|
|
53
|
-
"chatluna"
|
|
54
|
-
]
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "koishi-plugin-chatluna-dsh-adapter",
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"description": "DeepSeek Harness tools for ChatLuna with per-user/per-group workspaces: keyword-gated Harness tool for the normal model, plus an optional DSH model adapter. ChatLuna keeps its presets, character pipeline, and QQ rendering.",
|
|
5
|
+
"main": "lib/index.cjs",
|
|
6
|
+
"files": [
|
|
7
|
+
"lib",
|
|
8
|
+
"README.md"
|
|
9
|
+
],
|
|
10
|
+
"keywords": [
|
|
11
|
+
"chatbot",
|
|
12
|
+
"koishi",
|
|
13
|
+
"chatluna",
|
|
14
|
+
"deepseek",
|
|
15
|
+
"harness",
|
|
16
|
+
"adapter",
|
|
17
|
+
"qq"
|
|
18
|
+
],
|
|
19
|
+
"license": "MIT",
|
|
20
|
+
"author": "Kun-Fox",
|
|
21
|
+
"contributors": [
|
|
22
|
+
"Kun-Fox"
|
|
23
|
+
],
|
|
24
|
+
"repository": {
|
|
25
|
+
"type": "git",
|
|
26
|
+
"url": "git+https://github.com/Kun-Fox/koishi-plugin-chatluna-dsh-adapter.git"
|
|
27
|
+
},
|
|
28
|
+
"homepage": "https://github.com/Kun-Fox/koishi-plugin-chatluna-dsh-adapter#readme",
|
|
29
|
+
"bugs": {
|
|
30
|
+
"url": "https://github.com/Kun-Fox/koishi-plugin-chatluna-dsh-adapter/issues"
|
|
31
|
+
},
|
|
32
|
+
"publishConfig": {
|
|
33
|
+
"registry": "https://registry.npmjs.org/"
|
|
34
|
+
},
|
|
35
|
+
"engines": {
|
|
36
|
+
"node": ">=18.0.0"
|
|
37
|
+
},
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"@langchain/core": "^0.3.80"
|
|
40
|
+
},
|
|
41
|
+
"peerDependencies": {
|
|
42
|
+
"koishi": "^4.18.9",
|
|
43
|
+
"koishi-plugin-chatluna": "^1.4.4"
|
|
44
|
+
},
|
|
45
|
+
"koishi": {
|
|
46
|
+
"category": "ai",
|
|
47
|
+
"description": {
|
|
48
|
+
"zh": "给 ChatLuna 增加一个关键词触发的 DeepSeek Harness 工具:普通聊天仍走原模型和原预设,只有用户说 #harness 时才把任务交给 Harness;每个私聊/群有独立工作区,部署者保留完整权限。",
|
|
49
|
+
"en": "Adds a keyword-gated DeepSeek Harness tool to ChatLuna while keeping the normal model, presets, and character pipeline. Each private chat/group gets an isolated workspace; the owner keeps full access."
|
|
50
|
+
},
|
|
51
|
+
"service": {
|
|
52
|
+
"required": [
|
|
53
|
+
"chatluna"
|
|
54
|
+
]
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|