@4nti3ntropy/dsh-chat-mode 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +168 -2
- package/client/client.js +265 -0
- package/cordis.patch.yml +12 -0
- package/docs/preview-closeup.png +0 -0
- package/docs/preview.png +0 -0
- package/icon.svg +7 -0
- package/index.js +409 -0
- package/package.json +71 -4
- package/screenshots.json +4 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 dsh-chat-mode contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,169 @@
|
|
|
1
|
-
#
|
|
1
|
+
# dsh-chat-mode
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://github.com/Everglow28/dsh-chat-mode/releases/latest)
|
|
4
|
+
[](https://www.npmjs.com/package/@4nti3ntropy/dsh-chat-mode)
|
|
5
|
+
[](https://github.com/Everglow28/dsh-chat-mode/commits/main)
|
|
6
|
+
[](LICENSE)
|
|
7
|
+

|
|
8
|
+
[](https://github.com/Everglow28/dsh-chat-mode/pulls)
|
|
9
|
+
|
|
10
|
+
给 DeepSeek Harness(DSH)的输入框模式位加一个 **聊天模式** 开关。开启后,
|
|
11
|
+
agent 不再**主动**读取或修改文件:只有当你明确附上文件、点名文件(路径或文件名),
|
|
12
|
+
或明确要求做文件/命令操作时,它才会碰文件。其余能力与标准模式一致(回答、推理、
|
|
13
|
+
记忆、联网检索、追问等)。
|
|
14
|
+
|
|
15
|
+
A DSH plugin that adds a Chat Mode toggle to the composer: the agent stops touching
|
|
16
|
+
files unless you explicitly ask for file work.
|
|
17
|
+
|
|
18
|
+
## 截图
|
|
19
|
+
|
|
20
|
+
| 完整界面 | 模式位特写 |
|
|
21
|
+
| :---: | :---: |
|
|
22
|
+
|  |  |
|
|
23
|
+
|
|
24
|
+
## 要求
|
|
25
|
+
|
|
26
|
+
- DeepSeek Harness **0.2.1-rc.5 或更新**(`<0.3.0`;Desktop 或 `dsh web`)。
|
|
27
|
+
- Node.js 18+;插件零构建、零运行时依赖,安装时不需要下载额外依赖或执行安装脚本。
|
|
28
|
+
- 已在 DSH 0.2.2-rc.5 上实测。
|
|
29
|
+
|
|
30
|
+
插件在 `package.json` 中声明了宿主要求(`engines.dsh` 与 `peerDependencies["@deepseek-ai/dsh"]`),
|
|
31
|
+
不兼容的宿主会在安装/激活时被 DSH 插件管理器拦截。
|
|
32
|
+
|
|
33
|
+
## 安装
|
|
34
|
+
|
|
35
|
+
**方式 A:GitHub(仓库已公开)**
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
dsh plugin --profile <你的profile> add github:Everglow28/dsh-chat-mode
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
**方式 B:npm**
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
dsh plugin --profile <你的profile> add @4nti3ntropy/dsh-chat-mode
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
**方式 C:本地目录**
|
|
48
|
+
|
|
49
|
+
```bash
|
|
50
|
+
git clone https://github.com/Everglow28/dsh-chat-mode.git
|
|
51
|
+
dsh plugin --profile <你的profile> add <克隆目录的绝对路径>
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
也可以让 DSH 会话里的 Agent 用插件管理面板 / `plugin_manager` 的 `install_bundle`
|
|
55
|
+
安装仓库地址。安装后**重启 DSH**(Host 半侧变更必须重启;仅浏览器半侧刷新页面即可)。
|
|
56
|
+
|
|
57
|
+
## 它能做什么
|
|
58
|
+
|
|
59
|
+
- **软引导**:聊天模式开启时,系统提示词注入 `chat:policy` 段落,明确告诉模型
|
|
60
|
+
“这是一个对话模式,不要碰文件”,并给出被拒绝时该怎么说。
|
|
61
|
+
- **硬门禁**:`tools.guard` 在工具真正执行前做最终拒绝。未授权的 `read`、`glob`、
|
|
62
|
+
`grep`、`write`、`edit`、`pwsh`/`bash`/`shell`、`terminal_open`、`terminal_send`、
|
|
63
|
+
`create_worktree` 调用会直接失败,并返回一条让模型知道“别重试、怎么让你授权”的
|
|
64
|
+
错误文本。门禁在宿主进程里执行,模型无法绕开。
|
|
65
|
+
- **状态与会话日志同源**:开关只是替你发送 `/chat on|off` 命令;`chatMode` 会话
|
|
66
|
+
投影折叠 `command/run` + `command/done` 事件得到 `{ active }`。因此刷新、重启、
|
|
67
|
+
恢复、fork、压缩之后状态都能从日志复原,不需要额外的配置文件或 HTTP 通道。
|
|
68
|
+
- **模式位 UI**:输入框工具行(权限选择器右侧)出现紧凑的“聊天”开关。开启时
|
|
69
|
+
使用品牌色高亮并显示一个小圆点;窄输入框只保留图标。
|
|
70
|
+
|
|
71
|
+
## 授权规则
|
|
72
|
+
|
|
73
|
+
宿主按“用户最近一条消息”计算授权(纯“好的/继续/ok”会继承上一条实质消息):
|
|
74
|
+
|
|
75
|
+
| 用户消息里有 | 读取/搜索 | 写入/修改 | shell/终端 |
|
|
76
|
+
|---|---|---|---|
|
|
77
|
+
| 附件(文件或图片) | ✅ | ❌ | ❌ |
|
|
78
|
+
| 点名文件(`README.md`、`C:\a\b.ts`、`.env`、`叫 README 的文件`…) | ✅ | ❌ | ❌ |
|
|
79
|
+
| 点名文件 + 修改意图(“把…改成…”“修复…”) | ✅ | ✅ | ✅ |
|
|
80
|
+
| 明确运行意图(“跑一下测试”“build”) | ❌ | ❌ | ✅ |
|
|
81
|
+
| 只有闲聊 | ❌ | ❌ | ❌ |
|
|
82
|
+
|
|
83
|
+
一条没有文件信号的实质消息会终止“继续”继承——闲聊不会借用上一个文件任务的授权。
|
|
84
|
+
判定是启发式的:如果模型被误拦,它会把宿主返回的原始报错误转述给你,给出完整路径
|
|
85
|
+
或直接附上文件即可;也可以切回标准模式。
|
|
86
|
+
|
|
87
|
+
## 使用
|
|
88
|
+
|
|
89
|
+
1. 打开任意会话,输入框工具行(`+` 按钮右侧、权限选择器“标准模式/工作区内修改”
|
|
90
|
+
旁边)出现 `聊天` 开关。
|
|
91
|
+
2. 点击开启/关闭;开启后按钮高亮并带一个小圆点。
|
|
92
|
+
3. 也可以直接输入斜杠命令:`/chat on`、`/chat off`、`/chat`(查询当前状态)。
|
|
93
|
+
|
|
94
|
+
## 与计划模式的关系
|
|
95
|
+
|
|
96
|
+
聊天模式和计划模式对“能不能读文件”的指引相反。开启聊天模式时会尽量调用
|
|
97
|
+
`ctx.planMode.set(agent, false)` 退出计划模式;若两者同时存在,`chat:policy`
|
|
98
|
+
明确声明聊天模式在文件问题上优先。关闭聊天模式不会碰计划模式。
|
|
99
|
+
|
|
100
|
+
## 配置(可选)
|
|
101
|
+
|
|
102
|
+
写在 profile 的 `cordis.patch.yml`(或本包的 `cordis.patch.yml` 行)的 `config` 下:
|
|
103
|
+
|
|
104
|
+
```yaml
|
|
105
|
+
- id: chat-mode
|
|
106
|
+
name: '@4nti3ntropy/dsh-chat-mode'
|
|
107
|
+
config:
|
|
108
|
+
# 覆盖内置策略文本(完整替换 chat:policy 段落)
|
|
109
|
+
section: |
|
|
110
|
+
...
|
|
111
|
+
# false = 只发提示词、不做工具门禁
|
|
112
|
+
enforce: false
|
|
113
|
+
# 覆盖默认工具名单(整体替换,不是追加)
|
|
114
|
+
readTools: [read, glob, grep]
|
|
115
|
+
writeTools: [write, edit]
|
|
116
|
+
shellTools: [pwsh, bash, shell, terminal_open, terminal_send]
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
## 实现要点
|
|
120
|
+
|
|
121
|
+
| 文件 | 作用 |
|
|
122
|
+
|---|---|
|
|
123
|
+
| `index.js` | 宿主半侧:`chatMode` 投影、`chat:policy` 段落、`tools.guard`、`/chat` 命令 |
|
|
124
|
+
| `client/client.js` | 浏览器半侧:手写 lazy-CJS bundle,注册字典 + `conversation.input.left` 槽位 |
|
|
125
|
+
| `cordis.patch.yml` | 组合包层:插入一行宿主插件(浏览器 bundle 由 `dsh.client` 声明发布) |
|
|
126
|
+
| `dev/` | 无依赖的宿主单测 + 真实浏览器 CDP 端到端脚本(不随插件发布) |
|
|
127
|
+
|
|
128
|
+
宿主半侧只用运行时公开服务:`systemPrompt.section()`、`tools.guard()`、
|
|
129
|
+
`sessionProjections.register()`、`commands.register()`、`planMode.set()`(尽力而为)、
|
|
130
|
+
`session.eventAt()` / `session.seq`(读取最近用户消息)。浏览器半侧只用平台模块
|
|
131
|
+
`react` 和 `ctx.remote.commands.execute()`,不引入任何 npm 依赖。
|
|
132
|
+
|
|
133
|
+
## 验证
|
|
134
|
+
|
|
135
|
+
在**临时 `dsh web` 实例 + headless Chrome** 上做了端到端验证(脚本在 `dev/`):
|
|
136
|
+
|
|
137
|
+
| 检查项 | 结果 |
|
|
138
|
+
|---|---|
|
|
139
|
+
| 输入框模式位渲染出“聊天”开关,位置在权限选择器右侧 | ✅ |
|
|
140
|
+
| 点击后 `aria-pressed` 变 true;再点变 false(投影帧往返) | ✅ |
|
|
141
|
+
| 面板/刷新后状态保持(重启宿主后从会话日志恢复为开启) | ✅ |
|
|
142
|
+
| 系统提示词出现 `chat:policy`(解码会话日志确认,len≈7.4K) | ✅ |
|
|
143
|
+
| 未授权时模型调用 `read` 被宿主拒绝并转述报错 | ✅ |
|
|
144
|
+
| 用户点名“README”后 `read` 放行,读到 `README.md` 第一行 | ✅ |
|
|
145
|
+
| 未开聊天模式时文件工具行为不变 | ✅ |
|
|
146
|
+
|
|
147
|
+
宿主逻辑另有 `node dev/host-test.mjs` 覆盖投影折叠、授权矩阵、命令参数、
|
|
148
|
+
计划模式退出与配置覆盖。
|
|
149
|
+
|
|
150
|
+
## 已知限制
|
|
151
|
+
|
|
152
|
+
- **子代理不继承**:spawn 出来的子会话有自己的日志,默认不处于聊天模式;模型被
|
|
153
|
+
明确要求不得借子代理绕行。fork 出来的会话会复制命令日志,因此会继承状态。
|
|
154
|
+
- **启发式授权可能误判**:没有扩展名、也不在通用文件名列表里的名字(例如
|
|
155
|
+
“README”已覆盖,“那个配置文件”不覆盖)可能被拒绝。完整路径或附件最稳。
|
|
156
|
+
- **命令会留痕**:每次点击开关会在会话里留下 `command/run` + `command/done`
|
|
157
|
+
日志(与 `/plan` 一致),对话里能看到一行命令记录。
|
|
158
|
+
- **只约束工具调用**:宿主自动读取的 AGENTS.md、技能目录等上下文不受影响;
|
|
159
|
+
网络检索、会话搜索等非文件工具也照常可用(“其他能力和标准模式相同”)。
|
|
160
|
+
|
|
161
|
+
## 卸载
|
|
162
|
+
|
|
163
|
+
```bash
|
|
164
|
+
dsh plugin --profile <你的profile> remove @4nti3ntropy/dsh-chat-mode
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
## License
|
|
168
|
+
|
|
169
|
+
[MIT](LICENSE)
|
package/client/client.js
ADDED
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
// dsh-chat-mode —— 浏览器半侧。
|
|
2
|
+
//
|
|
3
|
+
// 手写的 lazy-CJS 工厂,格式与 DSH 客户端模块系统(dsh-client-modules)一致:
|
|
4
|
+
// 执行本脚本只注册 factory,模块体(样式注入、字典、插件 apply)在首次物化时
|
|
5
|
+
// 才运行。factory 的 require 只能解析冻结的平台模块表(react 等),因此这里
|
|
6
|
+
// 不依赖任何 npm 包。
|
|
7
|
+
//
|
|
8
|
+
// 行为:在输入框工具行的模式位(conversation.input.left,紧挨权限选择器与
|
|
9
|
+
// 计划模式 chip)放一个“聊天”开关。状态不来自本地缓存,而是宿主 `chatMode`
|
|
10
|
+
// 会话投影的 `{ active }` wire 值(useProjection),切换则向宿主发送
|
|
11
|
+
// `/chat on|off` 命令 —— 与会话日志同源,刷新/重启/恢复后自然一致。
|
|
12
|
+
window.__ModuleLoader__.load({
|
|
13
|
+
id: 'dsh-chat-mode',
|
|
14
|
+
factory: (require) => {
|
|
15
|
+
var module = { exports: {} };
|
|
16
|
+
var exports = module.exports;
|
|
17
|
+
'use strict';
|
|
18
|
+
|
|
19
|
+
var React = require('react');
|
|
20
|
+
var h = React.createElement;
|
|
21
|
+
|
|
22
|
+
var PLUGIN_ID = 'dsh-chat-mode';
|
|
23
|
+
var LOCALE_NS = 'chatMode';
|
|
24
|
+
var SLOT = 'conversation.input.left';
|
|
25
|
+
var PROJECTION_KEY = 'chatMode';
|
|
26
|
+
var PENDING_TIMEOUT_MS = 4000;
|
|
27
|
+
var ERROR_TIMEOUT_MS = 6000;
|
|
28
|
+
|
|
29
|
+
/** 界面文案字典;运行时按激活语言取用,注册失败时兜底到中文。 */
|
|
30
|
+
var dictionaries = {
|
|
31
|
+
zh: {
|
|
32
|
+
label: '聊天',
|
|
33
|
+
error: '切换失败',
|
|
34
|
+
'aria.on': '聊天模式已开启,点击关闭。',
|
|
35
|
+
'aria.off': '开启聊天模式:不主动读取或修改文件。',
|
|
36
|
+
'title.on': '聊天模式开启中:不会主动读取或修改文件(除非你附上文件、点名文件或明确要求文件/命令操作)。点击关闭。',
|
|
37
|
+
'title.off': '聊天模式:不主动读取或修改文件;只有你附上文件、点名文件或明确要求文件/命令操作时才会操作文件。点击开启。'
|
|
38
|
+
},
|
|
39
|
+
en: {
|
|
40
|
+
label: 'Chat',
|
|
41
|
+
error: 'Switch failed',
|
|
42
|
+
'aria.on': 'Chat mode on. Click to turn off.',
|
|
43
|
+
'aria.off': 'Turn on chat mode: no proactive file access.',
|
|
44
|
+
'title.on': 'Chat mode is on: no proactive file reads or writes unless you attach a file, name a path, or ask for file/command work. Click to turn off.',
|
|
45
|
+
'title.off': 'Chat mode: no proactive file reads or writes. Files are touched only when you attach one, name a path, or explicitly ask for file/command work. Click to turn on.'
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
var CSS = [
|
|
50
|
+
'.dscm-root{display:inline-flex;align-items:center;gap:6px;flex:none;min-width:0}',
|
|
51
|
+
'.dscm-pill{display:inline-flex;align-items:center;gap:4px;height:28px;min-width:34px;padding:0 8px;',
|
|
52
|
+
'border:0;border-radius:var(--dsw-radius-sm,8px);background:transparent;',
|
|
53
|
+
'color:var(--dsw-alias-label-secondary);font:inherit;font-size:13px;font-weight:500;line-height:20px;',
|
|
54
|
+
'cursor:pointer;flex:none;transition:background .12s ease,color .12s ease}',
|
|
55
|
+
'.dscm-pill:hover:not(:disabled){background:var(--dsw-alias-interactive-bg-hover)}',
|
|
56
|
+
'.dscm-pill:focus-visible{outline:var(--dsw-focus-ring-width,2px) solid ',
|
|
57
|
+
'var(--dsw-focus-ring-color,var(--dsw-alias-state-business-primary));outline-offset:2px}',
|
|
58
|
+
'.dscm-pill:disabled{opacity:.6;cursor:default}',
|
|
59
|
+
'.dscm-on{background:var(--dsw-alias-state-business-tertiary);color:var(--dsw-alias-state-business-primary)}',
|
|
60
|
+
'.dscm-on:hover:not(:disabled){background:color-mix(in srgb,',
|
|
61
|
+
'var(--dsw-alias-state-business-tertiary),var(--dsw-alias-state-business-primary) 6%)}',
|
|
62
|
+
'.dscm-icon{display:inline-flex;flex:0 0 auto;width:14px;height:14px;color:currentColor}',
|
|
63
|
+
'.dscm-icon svg{display:block;width:14px;height:14px}',
|
|
64
|
+
'.dscm-label{min-width:0;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}',
|
|
65
|
+
'.dscm-dot{flex:none;width:6px;height:6px;border-radius:999px;background:currentColor;margin-left:1px}',
|
|
66
|
+
'.dscm-error{color:var(--dsw-alias-state-error-primary);font-size:12px;line-height:18px;',
|
|
67
|
+
'max-width:180px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}',
|
|
68
|
+
/* 窄输入框时只留图标(与权限选择器的 460px 断点一致;没有容器时这条规则不生效)。 */
|
|
69
|
+
'@container (max-width: 460px){.dscm-label{display:none}.dscm-error{display:none}}'
|
|
70
|
+
].join('');
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* 注册本包自有的样式表。factory 物化时注入,标签带 data-plugin /
|
|
74
|
+
* data-plugin-css,客户端模块系统据此在插件卸载时回收它。
|
|
75
|
+
*/
|
|
76
|
+
function installStyles() {
|
|
77
|
+
if (typeof document === 'undefined') return;
|
|
78
|
+
var selector = 'style[data-plugin-css="' + PLUGIN_ID + '/client"]';
|
|
79
|
+
if (document.querySelector(selector) !== null) return;
|
|
80
|
+
var tag = document.createElement('style');
|
|
81
|
+
tag.setAttribute('data-plugin', PLUGIN_ID);
|
|
82
|
+
tag.setAttribute('data-plugin-css', PLUGIN_ID + '/client');
|
|
83
|
+
tag.textContent = CSS;
|
|
84
|
+
(document.head || document.body).appendChild(tag);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
installStyles();
|
|
88
|
+
|
|
89
|
+
/** 从字典取一条文案;缺 key 时回退到中文,再回退到 key 本身。 */
|
|
90
|
+
function translate(key) {
|
|
91
|
+
var value = dictionaries.zh[key];
|
|
92
|
+
return value === undefined ? key : value;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** 输入框里那个小小的对话气泡图标。 */
|
|
96
|
+
function bubbleGlyph() {
|
|
97
|
+
return h('svg', { viewBox: '0 0 16 16', 'aria-hidden': 'true', focusable: 'false' },
|
|
98
|
+
h('path', {
|
|
99
|
+
d: 'M13.25 8.25c0 2.55-2.45 4.6-5.45 4.6-.72 0-1.4-.12-2.02-.34L3.05 13.7l.72-2.16C2.9 10.66 2.35 9.52 2.35 8.25c0-2.55 2.45-4.6 5.45-4.6s5.45 2.05 5.45 4.6Z',
|
|
100
|
+
fill: 'none',
|
|
101
|
+
stroke: 'currentColor',
|
|
102
|
+
strokeWidth: 1.2,
|
|
103
|
+
strokeLinejoin: 'round',
|
|
104
|
+
strokeLinecap: 'round'
|
|
105
|
+
}),
|
|
106
|
+
h('circle', { cx: 5.7, cy: 8.25, r: 0.72, fill: 'currentColor', stroke: 'none' }),
|
|
107
|
+
h('circle', { cx: 8, cy: 8.25, r: 0.72, fill: 'currentColor', stroke: 'none' }),
|
|
108
|
+
h('circle', { cx: 10.3, cy: 8.25, r: 0.72, fill: 'currentColor', stroke: 'none' })
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* 输入框模式位上的聊天模式开关。
|
|
114
|
+
* @param props 槽位组合后的 props:sessionId、useProjection、t 与注入面。
|
|
115
|
+
*/
|
|
116
|
+
function ChatModePill(props) {
|
|
117
|
+
var t = typeof props.t === 'function' ? props.t : translate;
|
|
118
|
+
var sessionId = props.sessionId;
|
|
119
|
+
var projection = typeof props.useProjection === 'function' ? props.useProjection(PROJECTION_KEY) : undefined;
|
|
120
|
+
var projectedActive = projection !== null && typeof projection === 'object' && projection.active === true;
|
|
121
|
+
|
|
122
|
+
var pendingState = React.useState(null);
|
|
123
|
+
var pending = pendingState[0];
|
|
124
|
+
var setPending = pendingState[1];
|
|
125
|
+
var errorState = React.useState(null);
|
|
126
|
+
var error = errorState[0];
|
|
127
|
+
var setError = errorState[1];
|
|
128
|
+
|
|
129
|
+
var active = pending !== null ? pending : projectedActive;
|
|
130
|
+
|
|
131
|
+
/** 会话切换时清掉上一条会话的临时状态。 */
|
|
132
|
+
React.useEffect(function () {
|
|
133
|
+
setPending(null);
|
|
134
|
+
setError(null);
|
|
135
|
+
}, [sessionId]);
|
|
136
|
+
|
|
137
|
+
/** 投影追上目标值后结束 pending。 */
|
|
138
|
+
React.useEffect(function () {
|
|
139
|
+
if (pending !== null && projectedActive === pending) setPending(null);
|
|
140
|
+
}, [projectedActive, pending]);
|
|
141
|
+
|
|
142
|
+
/** pending 兜底:命令没有回报或投影没到时,4 秒后恢复可点击。 */
|
|
143
|
+
React.useEffect(function () {
|
|
144
|
+
if (pending === null) return undefined;
|
|
145
|
+
var timer = setTimeout(function () { setPending(null); }, PENDING_TIMEOUT_MS);
|
|
146
|
+
return function () { clearTimeout(timer); };
|
|
147
|
+
}, [pending]);
|
|
148
|
+
|
|
149
|
+
/** 错误提示自动消失。 */
|
|
150
|
+
React.useEffect(function () {
|
|
151
|
+
if (error === null) return undefined;
|
|
152
|
+
var timer = setTimeout(function () { setError(null); }, ERROR_TIMEOUT_MS);
|
|
153
|
+
return function () { clearTimeout(timer); };
|
|
154
|
+
}, [error]);
|
|
155
|
+
|
|
156
|
+
function toggle() {
|
|
157
|
+
if (pending !== null || typeof props.runChatCommand !== 'function') return;
|
|
158
|
+
var target = !active;
|
|
159
|
+
setPending(target);
|
|
160
|
+
setError(null);
|
|
161
|
+
Promise.resolve()
|
|
162
|
+
.then(function () { return props.runChatCommand(sessionId, target ? '/chat on' : '/chat off'); })
|
|
163
|
+
.then(function (failure) {
|
|
164
|
+
if (failure === null || failure === undefined) return;
|
|
165
|
+
setPending(null);
|
|
166
|
+
setError(String(failure));
|
|
167
|
+
}, function (reason) {
|
|
168
|
+
setPending(null);
|
|
169
|
+
setError(reason instanceof Error ? reason.message : String(reason));
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
var label = t('label');
|
|
174
|
+
var title = active ? t('title.on') : t('title.off');
|
|
175
|
+
var aria = active ? t('aria.on') : t('aria.off');
|
|
176
|
+
var className = 'dscm-pill' + (active ? ' dscm-on' : '') + (pending !== null ? ' dscm-pending' : '');
|
|
177
|
+
|
|
178
|
+
return h('div', { className: 'dscm-root' },
|
|
179
|
+
h('button', {
|
|
180
|
+
type: 'button',
|
|
181
|
+
className: className,
|
|
182
|
+
'aria-pressed': active ? 'true' : 'false',
|
|
183
|
+
'aria-label': aria,
|
|
184
|
+
title: title,
|
|
185
|
+
disabled: pending !== null,
|
|
186
|
+
onMouseDown: function (event) { event.preventDefault(); },
|
|
187
|
+
onClick: toggle
|
|
188
|
+
},
|
|
189
|
+
h('span', { className: 'dscm-icon', 'aria-hidden': 'true' }, bubbleGlyph()),
|
|
190
|
+
h('span', { className: 'dscm-label' }, label),
|
|
191
|
+
active ? h('span', { className: 'dscm-dot', 'aria-hidden': 'true' }) : null
|
|
192
|
+
),
|
|
193
|
+
error === null ? null : h('span', { className: 'dscm-error', role: 'status', title: error }, t('error'))
|
|
194
|
+
);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/** 客户端插件依赖的服务:槽位、字典、命令远程通道。 */
|
|
198
|
+
var inject = ['slots', 'locale', 'remote', 'remote.commands'];
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* 浏览器半侧入口:注册字典,并把开关注入输入框模式位。
|
|
202
|
+
* @param ctx 客户端 Cordis 上下文。
|
|
203
|
+
*/
|
|
204
|
+
function apply(ctx) {
|
|
205
|
+
ctx.effect(function () {
|
|
206
|
+
return ctx.locale.register(LOCALE_NS, {
|
|
207
|
+
zh: dictionaries.zh,
|
|
208
|
+
en: dictionaries.en
|
|
209
|
+
});
|
|
210
|
+
}, 'dsh-chat-mode: dictionaries');
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* 把一个 `/chat on|off` 命令送到宿主执行。
|
|
214
|
+
* @returns null 成功;失败时返回可直接展示的错误文本。
|
|
215
|
+
*/
|
|
216
|
+
function runChatCommand(sessionId, line) {
|
|
217
|
+
return Promise.resolve(ctx.remote.commands.execute(sessionId, line, [])).then(function (result) {
|
|
218
|
+
if (result === null || typeof result !== 'object') return 'command transport returned no result';
|
|
219
|
+
if (result.ok !== true) {
|
|
220
|
+
var remoteError = result.error;
|
|
221
|
+
var message = remoteError !== null && typeof remoteError === 'object' && typeof remoteError.message === 'string'
|
|
222
|
+
? remoteError.message
|
|
223
|
+
: 'command failed';
|
|
224
|
+
var code = remoteError !== null && typeof remoteError === 'object' && typeof remoteError.code === 'string'
|
|
225
|
+
? remoteError.code
|
|
226
|
+
: '';
|
|
227
|
+
return code === '' ? message : message + ' (' + code + ')';
|
|
228
|
+
}
|
|
229
|
+
if (result.value === undefined || result.value === null) return 'unknown command: ' + line;
|
|
230
|
+
var commandResult = result.value.result;
|
|
231
|
+
if (commandResult !== null && typeof commandResult === 'object' && commandResult.kind === 'error') {
|
|
232
|
+
return typeof commandResult.text === 'string' ? commandResult.text : 'command failed';
|
|
233
|
+
}
|
|
234
|
+
return null;
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
ctx.slots.inject(SLOT, function () {
|
|
239
|
+
return ctx.slots.register({
|
|
240
|
+
name: SLOT,
|
|
241
|
+
id: 'chat-mode',
|
|
242
|
+
order: 5,
|
|
243
|
+
locale: LOCALE_NS,
|
|
244
|
+
registrant: PLUGIN_ID,
|
|
245
|
+
inject: function () { return { runChatCommand: runChatCommand }; }
|
|
246
|
+
}, ChatModePill);
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
exports.name = PLUGIN_ID;
|
|
251
|
+
exports.inject = inject;
|
|
252
|
+
exports.apply = apply;
|
|
253
|
+
// 供调试/测试直接引用(不是插件协议的一部分)。
|
|
254
|
+
exports.ChatModePill = ChatModePill;
|
|
255
|
+
exports.__internals = {
|
|
256
|
+
LOCALE_NS: LOCALE_NS,
|
|
257
|
+
SLOT: SLOT,
|
|
258
|
+
PROJECTION_KEY: PROJECTION_KEY,
|
|
259
|
+
dictionaries: dictionaries,
|
|
260
|
+
translate: translate
|
|
261
|
+
};
|
|
262
|
+
|
|
263
|
+
return module.exports;
|
|
264
|
+
}
|
|
265
|
+
});
|
package/cordis.patch.yml
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
# dsh-chat-mode 组合包层:插入一行宿主插件。
|
|
2
|
+
# 浏览器半侧由 dsh-client-modules 依据 package.json 的 dsh.client 声明自动发布,
|
|
3
|
+
# 这一行的存在即是对该 bundle 的启用;宿主半侧 index.js 注册投影、提示词段落、
|
|
4
|
+
# 工具门禁与 /chat 命令。
|
|
5
|
+
#
|
|
6
|
+
# 可选配置(写在 config 下):
|
|
7
|
+
# section: 覆盖内置策略文本
|
|
8
|
+
# enforce: false 只发提示词、不做工具门禁
|
|
9
|
+
# readTools / writeTools / shellTools: 覆盖默认工具名单
|
|
10
|
+
- insert:
|
|
11
|
+
- id: chat-mode
|
|
12
|
+
name: '@4nti3ntropy/dsh-chat-mode'
|
|
Binary file
|
package/docs/preview.png
ADDED
|
Binary file
|
package/icon.svg
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64">
|
|
2
|
+
<rect x="10" y="10" width="44" height="34" rx="10" fill="none" stroke="#0B7FFF" stroke-width="5"/>
|
|
3
|
+
<path d="M22 44 L18 56 L32 45" fill="#0B7FFF"/>
|
|
4
|
+
<circle cx="24" cy="27" r="3.2" fill="#0B7FFF"/>
|
|
5
|
+
<circle cx="32" cy="27" r="3.2" fill="#0B7FFF"/>
|
|
6
|
+
<circle cx="40" cy="27" r="3.2" fill="#0B7FFF"/>
|
|
7
|
+
</svg>
|
package/index.js
ADDED
|
@@ -0,0 +1,409 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dsh-chat-mode —— 宿主半侧。
|
|
3
|
+
*
|
|
4
|
+
* 聊天模式是输入框模式位上的一个会话级模式:开启后,agent 不再“主动”读取或
|
|
5
|
+
* 修改任何文件,除非用户在这一条消息里明确附上了文件、指名了文件(路径/文件名),
|
|
6
|
+
* 或明确要求做文件/命令操作。其余能力与标准模式一致(回答问题、记忆、联网检索、
|
|
7
|
+
* 用户提问等)。
|
|
8
|
+
*
|
|
9
|
+
* 设计要点(与 plan-mode 同构):
|
|
10
|
+
*
|
|
11
|
+
* 1. **状态在会话日志里,不在内存/配置文件里**。用户通过 `/chat on|off` 命令
|
|
12
|
+
* 切换(输入框旁的开关就是替用户发这条命令);命令生命周期事件
|
|
13
|
+
* `command/run` + `command/done` 由 `chatMode` 会话投影折叠出 `{ active }`。
|
|
14
|
+
* 因此刷新、重启、恢复、fork、压缩之后状态都能从日志复原,也不需要自定义
|
|
15
|
+
* HTTP 通道:投影的 client-visible wire(`{ active }`)会随会话投影帧推给浏览器,
|
|
16
|
+
* 开关组件直接读 `useProjection('chatMode')`。
|
|
17
|
+
*
|
|
18
|
+
* 2. **软引导 + 硬门禁**。`chat:policy` 系统提示词段落告诉模型当前模式及规则;
|
|
19
|
+
* `tools.guard` 在工具真正执行前做最终拒绝 —— 未授权的文件/命令工具调用
|
|
20
|
+
* 直接失败,并给模型一条可执行的解释。guard 是单调的最终拒绝(没有 allow
|
|
21
|
+
* 返回值),所以其他策略无法把它重新放行。
|
|
22
|
+
*
|
|
23
|
+
* 3. **授权的判定是“本轮用户消息”**。向后扫最近的用户消息(找不到就继续看
|
|
24
|
+
* 前一条“好的/继续”式确认),按附件、路径引用、修改意图、运行意图四类信号
|
|
25
|
+
* 计算 read/write/shell 授权。没有信号的消息(闲聊)会阻断继承,避免旧的
|
|
26
|
+
* 文件任务把授权泄漏到新的聊天话题里。
|
|
27
|
+
*
|
|
28
|
+
* 配置(全部可选,写在 cordis.patch.yml 的 config 下):
|
|
29
|
+
* section: string 覆盖内置的策略文本
|
|
30
|
+
* enforce: boolean 默认 true;false = 只发提示词、不做工具门禁
|
|
31
|
+
* readTools/writeTools/shellTools: string[] 覆盖默认的工具名单
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
export const name = 'dsh-chat-mode'
|
|
35
|
+
|
|
36
|
+
/** 硬依赖:提示词、工具门禁、会话投影、命令注册。 */
|
|
37
|
+
export const inject = ['systemPrompt', 'tools', 'sessionProjections', 'commands']
|
|
38
|
+
|
|
39
|
+
/** 投影 key:同时是客户端 `useProjection` 读取的名字。 */
|
|
40
|
+
const PROJECTION_KEY = 'chatMode'
|
|
41
|
+
|
|
42
|
+
/** 会话投影状态版本;状态结构变化时递增。 */
|
|
43
|
+
const STATE_VERSION = 1
|
|
44
|
+
|
|
45
|
+
/** 人类命令名(不含斜杠)。 */
|
|
46
|
+
const COMMAND_NAME = 'chat'
|
|
47
|
+
|
|
48
|
+
/** 投影折叠时最多回看的用户消息条数。 */
|
|
49
|
+
const MESSAGE_SCAN_LIMIT = 8
|
|
50
|
+
|
|
51
|
+
/** 授权扫描时最多回看的事件条数(工具轮次会产生较多事件)。 */
|
|
52
|
+
const EVENT_SCAN_LIMIT = 600
|
|
53
|
+
|
|
54
|
+
/** 策略文本:只在聊天模式开启时进入系统提示词。 */
|
|
55
|
+
const DEFAULT_SECTION = `Chat mode is ON for this session. It is a conversation mode, not a file-working session, and it is not plan mode.
|
|
56
|
+
|
|
57
|
+
Treat the user's files as off-limits by default. Do NOT read, list, search, open, create, edit, delete, move, or rename any file, and do NOT run shell or terminal commands, unless the user's latest message either attaches a file, names a file (a path or a filename), or explicitly asks you to modify files or run commands. If plan-mode guidance is also present, this chat-mode rule wins for anything that touches files.
|
|
58
|
+
|
|
59
|
+
The host enforces this: file, shell, and terminal tools are denied when the latest user message does not authorize file work. If a tool is denied, do not retry it in that turn and do not route around it with another tool or a subagent. Instead, say briefly that chat mode keeps files untouched and ask the user to attach the file, name the file/path, or switch back to standard mode (the mode chip above the composer, or /chat off). When the user's next message attaches or names the file, that turn is authorized and you may try again.
|
|
60
|
+
|
|
61
|
+
Everything else works as in standard mode: answer from your knowledge and the conversation, reason normally, and use non-file tools such as web search or user questions when they help.`
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* 未授权的工具拒绝文案。必须让模型知道“为什么、怎么办、别再试”。
|
|
65
|
+
* @param {string} toolName 被拒绝的工具名。
|
|
66
|
+
* @param {'read' | 'write' | 'shell'} kind 工具类别。
|
|
67
|
+
* @returns {string} 模型可见的拒绝原因。
|
|
68
|
+
*/
|
|
69
|
+
function denialText(toolName, kind) {
|
|
70
|
+
const label = kind === 'shell' ? 'shell/terminal' : 'file'
|
|
71
|
+
return `Chat mode is on: "${toolName}" is a ${label} tool, but this turn is not authorized for file work `
|
|
72
|
+
+ '(the latest user message has no attached file, no named file/path, and no explicit request to modify files or run commands). '
|
|
73
|
+
+ 'Do not retry it in this turn and do not route around it with another tool or a subagent. Answer in conversation only; '
|
|
74
|
+
+ 'if file access is needed, ask the user to attach the file, name the file/path, or switch back to standard mode (/chat off). '
|
|
75
|
+
+ 'A later user message that attaches or names the file authorizes a new attempt.'
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** 默认工具分流;配置可整体覆盖。 */
|
|
79
|
+
const DEFAULT_TOOLS = {
|
|
80
|
+
read: ['read', 'glob', 'grep'],
|
|
81
|
+
write: ['write', 'edit', 'create_worktree'],
|
|
82
|
+
shell: ['pwsh', 'powershell', 'bash', 'shell', 'terminal_open', 'terminal_send'],
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** 绝对路径、相对路径、盘符、UNC。 */
|
|
86
|
+
const PATH_LIKE = /(?:[A-Za-z]:[\\/]|\\\\[^\s"'`)】\]]+|(?:^|[\s"'`(([【])(?:\/|\.{1,2}[\\/])[^\s"'`)】\]]*)/u
|
|
87
|
+
|
|
88
|
+
/** 常见源码/文档/数据文件扩展名;写成“文件名.扩展名”即视为指名了文件。 */
|
|
89
|
+
const FILE_EXTENSIONS = 'md|markdown|mdx|txt|json|jsonc|json5|ya?ml|toml|ini|cfg|conf|env|ts|tsx|mts|cts|js|jsx|mjs|cjs|py|pyi|go|rs|java|kt|kts|cs|c|h|cc|cpp|hpp|rb|php|swift|scala|sh|bash|zsh|ps1|bat|cmd|html?|css|scss|sass|less|sql|graphql|gql|proto|lua|r|pl|dart|vue|svelte|astro|xml|svg|png|jpe?g|gif|webp|pdf|docx?|xlsx?|pptx?|csv|tsv|log|lock|gradle|properties|sln|csproj|msi|exe|zip|tar|gz'
|
|
90
|
+
const FILE_LIKE = new RegExp(`[\\w@.+-]*\\.(?:${FILE_EXTENSIONS})(?=$|[\\s"'\\x60)】\\],:;!?。、])`, 'iu')
|
|
91
|
+
|
|
92
|
+
/** 没有扩展名但业界通用的“文件名”(README、Makefile、.env……)。 */
|
|
93
|
+
const KNOWN_FILES = /(?:^|[\s"'`(([【@/])(?:readme|license|licence|makefile|dockerfile|changelog|contributing|codeowners|authors|notice|agents|claude|copying|install|todo|manifest|rakefile|gemfile|procfile|vagrantfile|justfile|cmakelists|brewfile|go\.mod|go\.sum|requirements|composer\.json|gradlew|\.gitignore|\.env|\.npmrc|\.editorconfig|\.prettierrc|\.eslintrc|\.gitattributes)(?=$|[\s"'`)】\],:;!?。、])/iu
|
|
94
|
+
|
|
95
|
+
/** “叫 XXX 的文件”“file named XXX”这类句式,本身就把 XXX 指为文件。 */
|
|
96
|
+
const NAMED_FILE = /(?:叫|名叫|名为|名字(?:叫|是))\s*["'`]?[\w.-]{2,}["'`]?\s*(?:这个|那个)?\s*(?:文件|目录|路径)|(?:named|called)\s+["'`]?[\w.-]{2,}["'`]?\s+file|file\s+(?:named|called)\s+["'`]?[\w.-]{2,}/iu
|
|
97
|
+
|
|
98
|
+
/** 明确的“改文件”意图。 */
|
|
99
|
+
const WRITE_INTENT = /(?:修改|改动|改一?下|改成|改为|编辑|写入|写一?下|写进|创建|新建|删除|移除|重命名|改名|移动|复制|替换|修复|修正|实现|重构|补上|加上|添加|增加|保存到|update|modify|change|edit|write|create|delete|remove|rename|move|replace|fix|refactor|implement|add\b)/iu
|
|
100
|
+
|
|
101
|
+
/** 明确的“运行/检查”意图(测试、构建、执行命令)。 */
|
|
102
|
+
const RUN_INTENT = /(?:运行|执行|跑一?下|跑个|测试|构建|编译|安装依赖|格式化|静态检查|lint|test|build|run\b|execute|compile|install)/iu
|
|
103
|
+
|
|
104
|
+
/** 纯确认/继续类消息:继承上一条实质消息的授权。 */
|
|
105
|
+
const ACK_ONLY = /^(?:好的?|行|可以|同意|没问题|对的?|是的?|嗯+|哦+|继续(?:吧|改|做)?|接着|来吧|就这样|收到|明白|了解|ok(?:ay)?|yes|yeah|yep|sure|go ahead|continue|keep going|proceed|sounds good|please do)[\s!!。.~~,,、]*$/iu
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* 校验并归一化配置。
|
|
109
|
+
* @param {unknown} input 组合包层传入的原始配置。
|
|
110
|
+
* @returns {{ section: string, enforce: boolean, readTools: Set<string>, writeTools: Set<string>, shellTools: Set<string> }}
|
|
111
|
+
*/
|
|
112
|
+
function resolveConfig(input) {
|
|
113
|
+
const raw = input !== null && typeof input === 'object' ? input : {}
|
|
114
|
+
const section = typeof raw.section === 'string' && raw.section.trim() !== '' ? raw.section : DEFAULT_SECTION
|
|
115
|
+
const list = (value, fallback) => {
|
|
116
|
+
if (!Array.isArray(value)) return new Set(fallback)
|
|
117
|
+
const items = value
|
|
118
|
+
.filter(item => typeof item === 'string' && item.trim() !== '')
|
|
119
|
+
.map(item => item.trim().toLowerCase())
|
|
120
|
+
return new Set(items.length === 0 ? fallback : items)
|
|
121
|
+
}
|
|
122
|
+
return {
|
|
123
|
+
section,
|
|
124
|
+
enforce: raw.enforce !== false,
|
|
125
|
+
readTools: list(raw.readTools, DEFAULT_TOOLS.read),
|
|
126
|
+
writeTools: list(raw.writeTools, DEFAULT_TOOLS.write),
|
|
127
|
+
shellTools: list(raw.shellTools, DEFAULT_TOOLS.shell),
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* 把工具名归入 read / write / shell 三类;不属于受管工具时返回 null。
|
|
133
|
+
* @param {string} toolName 工具名。
|
|
134
|
+
* @param {ReturnType<typeof resolveConfig>} config 归一化配置。
|
|
135
|
+
* @returns {'read' | 'write' | 'shell' | null}
|
|
136
|
+
*/
|
|
137
|
+
function classify(toolName, config) {
|
|
138
|
+
const name = String(toolName).toLowerCase()
|
|
139
|
+
if (config.writeTools.has(name)) return 'write'
|
|
140
|
+
if (config.readTools.has(name)) return 'read'
|
|
141
|
+
if (config.shellTools.has(name)) return 'shell'
|
|
142
|
+
return null
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* 从后往前找最近的、由真人提交的用户消息(跳过工具注入的上下文)。
|
|
147
|
+
* @param {object} session 会话。
|
|
148
|
+
* @param {number} limit 最多返回几条。
|
|
149
|
+
* @returns {Array<{ content?: unknown, source?: unknown }>}
|
|
150
|
+
*/
|
|
151
|
+
function lastHumanMessages(session, limit) {
|
|
152
|
+
const messages = []
|
|
153
|
+
let seq = Number(session.seq) - 1
|
|
154
|
+
const floor = Math.max(0, seq - EVENT_SCAN_LIMIT)
|
|
155
|
+
while (seq >= floor && messages.length < limit) {
|
|
156
|
+
const event = session.eventAt(seq)
|
|
157
|
+
if (event !== undefined && event !== null && event.type === 'user/message') {
|
|
158
|
+
const data = event.data
|
|
159
|
+
const source = data !== null && typeof data === 'object' ? data.source : undefined
|
|
160
|
+
if (source !== null && typeof source === 'object' && source.kind === 'user') messages.push(data)
|
|
161
|
+
}
|
|
162
|
+
seq -= 1
|
|
163
|
+
}
|
|
164
|
+
return messages
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* 解析一条用户消息里的授权信号。
|
|
169
|
+
* @param {{ content?: unknown }} message 用户消息。
|
|
170
|
+
* @returns {{ attachment: boolean, pathRef: boolean, writeIntent: boolean, runIntent: boolean, ackOnly: boolean }}
|
|
171
|
+
*/
|
|
172
|
+
function signalsOf(message) {
|
|
173
|
+
const blocks = Array.isArray(message.content) ? message.content : []
|
|
174
|
+
let text = ''
|
|
175
|
+
let attachment = false
|
|
176
|
+
for (const block of blocks) {
|
|
177
|
+
if (block === null || typeof block !== 'object') continue
|
|
178
|
+
if (block.type === 'text' && typeof block.text === 'string') {
|
|
179
|
+
text += (text === '' ? '' : '\n') + block.text
|
|
180
|
+
} else if (block.type === 'file' || block.type === 'image') {
|
|
181
|
+
attachment = true
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
const trimmed = text.trim()
|
|
185
|
+
return {
|
|
186
|
+
attachment,
|
|
187
|
+
pathRef: PATH_LIKE.test(text) || FILE_LIKE.test(text) || KNOWN_FILES.test(text) || NAMED_FILE.test(text),
|
|
188
|
+
writeIntent: WRITE_INTENT.test(text),
|
|
189
|
+
runIntent: RUN_INTENT.test(text),
|
|
190
|
+
ackOnly: trimmed !== '' && ACK_ONLY.test(trimmed),
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* 计算本轮对文件工具的授权。
|
|
196
|
+
*
|
|
197
|
+
* - 读/搜:用户附了文件,或指名了文件/路径;
|
|
198
|
+
* - 写/改:在“指名了文件”的前提下,还明确要求修改;
|
|
199
|
+
* - shell/终端:明确要求运行/测试,或已经授权修改(改完通常要跑检查)。
|
|
200
|
+
*
|
|
201
|
+
* 纯确认消息(好的/继续)继承上一条实质消息;一条没有文件信号的实质消息会
|
|
202
|
+
* 终止继承 —— 闲聊不会借用上一个文件任务的授权。
|
|
203
|
+
*
|
|
204
|
+
* @param {object} session 会话。
|
|
205
|
+
* @returns {{ read: boolean, write: boolean, shell: boolean }}
|
|
206
|
+
*/
|
|
207
|
+
function authorizationFor(session) {
|
|
208
|
+
const grant = { read: false, write: false, shell: false }
|
|
209
|
+
for (const message of lastHumanMessages(session, MESSAGE_SCAN_LIMIT)) {
|
|
210
|
+
const signals = signalsOf(message)
|
|
211
|
+
if (!signals.attachment && !signals.pathRef && !signals.writeIntent && !signals.runIntent) {
|
|
212
|
+
if (!signals.ackOnly) break
|
|
213
|
+
continue
|
|
214
|
+
}
|
|
215
|
+
const fileSignal = signals.attachment || signals.pathRef
|
|
216
|
+
if (fileSignal) grant.read = true
|
|
217
|
+
if (fileSignal && signals.writeIntent) grant.write = true
|
|
218
|
+
if (signals.runIntent || grant.write) grant.shell = true
|
|
219
|
+
break
|
|
220
|
+
}
|
|
221
|
+
return grant
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* 解析 `/chat` 的参数为投影目标状态。
|
|
226
|
+
* @param {unknown} args 命令原始参数(可能带前导空白)。
|
|
227
|
+
* @returns {boolean | null} true=on,false=off,null=非状态切换。
|
|
228
|
+
*/
|
|
229
|
+
function targetOfArgs(args) {
|
|
230
|
+
const value = String(args ?? '').trim().toLowerCase()
|
|
231
|
+
if (value === 'on') return true
|
|
232
|
+
if (value === 'off') return false
|
|
233
|
+
return null
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* 构造 `chatMode` 投影定义。
|
|
238
|
+
*
|
|
239
|
+
* 折叠规则与 plan-mode 同构:`command/run` 记录目标,`command/done` 成功才提交。
|
|
240
|
+
* 因此命令失败(或进程在 run/done 之间退出)不会留下半生效的状态。
|
|
241
|
+
*
|
|
242
|
+
* @param {ReturnType<typeof resolveConfig>} config 归一化配置。
|
|
243
|
+
* @returns {object} 0.2.2 运行时接受的投影定义(只要求 schema 有 parse)。
|
|
244
|
+
*/
|
|
245
|
+
function chatModeDefinition(config) {
|
|
246
|
+
const stateSchema = {
|
|
247
|
+
parse(value) {
|
|
248
|
+
if (value === null || typeof value !== 'object') throw new Error('chat-mode: state must be an object')
|
|
249
|
+
const running = value.running
|
|
250
|
+
if (running !== null && running !== undefined
|
|
251
|
+
&& (typeof running !== 'object' || typeof running.commandId !== 'string' || typeof running.target !== 'boolean')) {
|
|
252
|
+
throw new Error('chat-mode: state.running is malformed')
|
|
253
|
+
}
|
|
254
|
+
if (typeof value.active !== 'boolean') throw new Error('chat-mode: state.active must be a boolean')
|
|
255
|
+
return { active: value.active, running: running ?? null }
|
|
256
|
+
},
|
|
257
|
+
}
|
|
258
|
+
const viewSchema = {
|
|
259
|
+
parse(value) {
|
|
260
|
+
if (value === null || typeof value !== 'object' || typeof value.active !== 'boolean') {
|
|
261
|
+
throw new Error('chat-mode: view.active must be a boolean')
|
|
262
|
+
}
|
|
263
|
+
return { active: value.active }
|
|
264
|
+
},
|
|
265
|
+
}
|
|
266
|
+
return {
|
|
267
|
+
key: PROJECTION_KEY,
|
|
268
|
+
stateVersion: STATE_VERSION,
|
|
269
|
+
stateSchema,
|
|
270
|
+
init: () => ({ active: false, running: null }),
|
|
271
|
+
apply: (state, event) => {
|
|
272
|
+
if (event.type === 'command/run' && event.data !== null && event.data.name === COMMAND_NAME) {
|
|
273
|
+
const target = targetOfArgs(event.data.args)
|
|
274
|
+
if (target === null) return state
|
|
275
|
+
return { ...state, running: { commandId: String(event.data.commandId), target } }
|
|
276
|
+
}
|
|
277
|
+
if (event.type === 'command/done' && state.running !== null
|
|
278
|
+
&& String(event.data.commandId) === state.running.commandId) {
|
|
279
|
+
return {
|
|
280
|
+
active: event.data.kind === 'success' ? state.running.target : state.active,
|
|
281
|
+
running: null,
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
return state
|
|
285
|
+
},
|
|
286
|
+
wire: {
|
|
287
|
+
viewSchema,
|
|
288
|
+
view: (state) => ({ active: state.active }),
|
|
289
|
+
},
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* 放置 `chat:policy` 段落:紧跟 `plan:policy`(first-party 顺序 500 之后)。
|
|
295
|
+
* @param {object} ctx 宿主上下文。
|
|
296
|
+
* @returns {number} 段落顺序。
|
|
297
|
+
*/
|
|
298
|
+
function chatSectionOrder(ctx) {
|
|
299
|
+
try {
|
|
300
|
+
const planOrder = ctx.systemPrompt.getSectionOrder('PLAN_POLICY')
|
|
301
|
+
if (Number.isFinite(planOrder)) return planOrder + 1
|
|
302
|
+
} catch {
|
|
303
|
+
/* 没有 plan-mode 时用默认顺序 */
|
|
304
|
+
}
|
|
305
|
+
return 500
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* 开启聊天模式时尽量退出计划模式(两者对“能不能读文件”的指引相反)。
|
|
310
|
+
* @param {object} ctx 宿主上下文。
|
|
311
|
+
* @param {object} agent 目标 agent。
|
|
312
|
+
* @returns {Promise<void>}
|
|
313
|
+
*/
|
|
314
|
+
async function exitPlanMode(ctx, agent) {
|
|
315
|
+
try {
|
|
316
|
+
const planMode = ctx.get('planMode')
|
|
317
|
+
if (planMode === undefined || typeof planMode.get !== 'function' || typeof planMode.set !== 'function') return
|
|
318
|
+
const state = planMode.get(agent)
|
|
319
|
+
if (state !== null && typeof state === 'object' && state.active === true) planMode.set(agent, false)
|
|
320
|
+
} catch (error) {
|
|
321
|
+
ctx.logger?.warn?.('dsh-chat-mode: could not leave plan mode: %o', error)
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* 注册聊天模式:投影 + 策略段落 + 工具门禁 + `/chat` 命令。
|
|
327
|
+
* @param {object} ctx 宿主 Cordis 上下文。
|
|
328
|
+
* @param {unknown} config 组合包层配置。
|
|
329
|
+
* @returns {Promise<void>}
|
|
330
|
+
*/
|
|
331
|
+
export async function apply(ctx, config) {
|
|
332
|
+
const resolved = resolveConfig(config)
|
|
333
|
+
|
|
334
|
+
// 先注册投影并等待 ready:策略段落/门禁/命令都依赖它的同步 stateOf。
|
|
335
|
+
const handle = await ctx.sessionProjections.register(chatModeDefinition(resolved))
|
|
336
|
+
const stateOf = (session) => {
|
|
337
|
+
if (session === undefined || session === null) return { active: false }
|
|
338
|
+
try {
|
|
339
|
+
const state = handle.stateOf(session)
|
|
340
|
+
return state !== null && typeof state === 'object' ? state : { active: false }
|
|
341
|
+
} catch (error) {
|
|
342
|
+
ctx.logger?.warn?.('dsh-chat-mode: could not read projection state: %o', error)
|
|
343
|
+
return { active: false }
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
// 1) 软引导:只在聊天模式开启时贡献文本。
|
|
348
|
+
ctx.systemPrompt.section({
|
|
349
|
+
name: 'chat:policy',
|
|
350
|
+
order: chatSectionOrder(ctx),
|
|
351
|
+
text: (context) => {
|
|
352
|
+
if (context === undefined || context === null || context.agent === undefined) return ''
|
|
353
|
+
return stateOf(context.agent.session).active === true ? resolved.section : ''
|
|
354
|
+
},
|
|
355
|
+
})
|
|
356
|
+
|
|
357
|
+
// 2) 硬门禁:未授权的文件/命令工具调用最终拒绝。
|
|
358
|
+
if (resolved.enforce) {
|
|
359
|
+
ctx.tools.guard((execution) => {
|
|
360
|
+
try {
|
|
361
|
+
const agent = execution.agent
|
|
362
|
+
if (agent === undefined || agent === null) return undefined
|
|
363
|
+
if (stateOf(agent.session).active !== true) return undefined
|
|
364
|
+
const kind = classify(execution.name, resolved)
|
|
365
|
+
if (kind === null) return undefined
|
|
366
|
+
const grant = authorizationFor(agent.session)
|
|
367
|
+
if (grant[kind]) return undefined
|
|
368
|
+
return denialText(execution.name, kind)
|
|
369
|
+
} catch (error) {
|
|
370
|
+
// 门禁自身的异常不应该把工具全锁死。
|
|
371
|
+
ctx.logger?.warn?.('dsh-chat-mode: tool guard failed open: %o', error)
|
|
372
|
+
return undefined
|
|
373
|
+
}
|
|
374
|
+
})
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
// 3) 人类命令:输入框开关和键盘用户共用这一条写入路径。
|
|
378
|
+
ctx.commands.register({
|
|
379
|
+
definitionId: 'dsh-chat-mode',
|
|
380
|
+
name: COMMAND_NAME,
|
|
381
|
+
description: 'Enter or leave chat mode (no proactive file access)',
|
|
382
|
+
input: { hint: '[on|off]' },
|
|
383
|
+
handler: async ({ agent, rawInput }) => {
|
|
384
|
+
const arg = String(rawInput ?? '').trim().toLowerCase()
|
|
385
|
+
const current = stateOf(agent.session).active === true
|
|
386
|
+
if (arg === '') {
|
|
387
|
+
return { kind: 'success', text: current ? 'Chat mode is on.' : 'Chat mode is off.' }
|
|
388
|
+
}
|
|
389
|
+
if (arg !== 'on' && arg !== 'off') {
|
|
390
|
+
return { kind: 'error', text: 'Usage: /chat on|off' }
|
|
391
|
+
}
|
|
392
|
+
const target = arg === 'on'
|
|
393
|
+
if (target === current) {
|
|
394
|
+
return { kind: 'success', text: target ? 'Chat mode is already on.' : 'Chat mode is already off.' }
|
|
395
|
+
}
|
|
396
|
+
if (target) await exitPlanMode(ctx, agent)
|
|
397
|
+
return {
|
|
398
|
+
kind: 'success',
|
|
399
|
+
text: target
|
|
400
|
+
? 'Chat mode on. Files stay untouched unless you attach one, name a path, or ask for file work.'
|
|
401
|
+
: 'Chat mode off. Standard file access restored.',
|
|
402
|
+
}
|
|
403
|
+
},
|
|
404
|
+
})
|
|
405
|
+
|
|
406
|
+
ctx.logger?.info?.('dsh-chat-mode: chat mode ready (%s)', resolved.enforce ? 'enforcing' : 'guidance only')
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
export default { name, inject, apply }
|
package/package.json
CHANGED
|
@@ -1,6 +1,73 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@4nti3ntropy/dsh-chat-mode",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Adds a Chat Mode toggle to the DeepSeek Harness composer: the agent stops touching files unless you explicitly ask for file work.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "index.js",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./index.js",
|
|
9
|
+
"./client": "./client/client.js",
|
|
10
|
+
"./package.json": "./package.json"
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"index.js",
|
|
14
|
+
"client/client.js",
|
|
15
|
+
"cordis.patch.yml",
|
|
16
|
+
"README.md",
|
|
17
|
+
"LICENSE",
|
|
18
|
+
"screenshots.json",
|
|
19
|
+
"icon.svg",
|
|
20
|
+
"docs/*.png"
|
|
21
|
+
],
|
|
22
|
+
"keywords": [
|
|
23
|
+
"dsh",
|
|
24
|
+
"deepseek-harness",
|
|
25
|
+
"dsh-plugin",
|
|
26
|
+
"cordis",
|
|
27
|
+
"chat-mode",
|
|
28
|
+
"agent-mode",
|
|
29
|
+
"file-guard",
|
|
30
|
+
"productivity"
|
|
31
|
+
],
|
|
32
|
+
"license": "MIT",
|
|
33
|
+
"engines": {
|
|
34
|
+
"node": ">=18",
|
|
35
|
+
"dsh": ">=0.2.1-rc.5 <0.3.0-0"
|
|
36
|
+
},
|
|
37
|
+
"repository": {
|
|
38
|
+
"type": "git",
|
|
39
|
+
"url": "git+https://github.com/Everglow28/dsh-chat-mode.git"
|
|
40
|
+
},
|
|
41
|
+
"bugs": {
|
|
42
|
+
"url": "https://github.com/Everglow28/dsh-chat-mode/issues"
|
|
43
|
+
},
|
|
44
|
+
"homepage": "https://github.com/Everglow28/dsh-chat-mode#readme",
|
|
45
|
+
"icon": "./icon.svg",
|
|
46
|
+
"dsh": {
|
|
47
|
+
"bundle": {
|
|
48
|
+
"patch": "./cordis.patch.yml"
|
|
49
|
+
},
|
|
50
|
+
"client": {
|
|
51
|
+
"platform": "web",
|
|
52
|
+
"inject": [
|
|
53
|
+
"@deepseek-ai/dsh-api-remotes",
|
|
54
|
+
"@deepseek-ai/dsh-api-session-controller",
|
|
55
|
+
"@deepseek-ai/dsh-client-locale",
|
|
56
|
+
"@deepseek-ai/dsh-client-ui-conversation"
|
|
57
|
+
]
|
|
58
|
+
},
|
|
59
|
+
"marketplace": {
|
|
60
|
+
"profiles": [
|
|
61
|
+
"desktop",
|
|
62
|
+
"web"
|
|
63
|
+
],
|
|
64
|
+
"requiresBuildApproval": false,
|
|
65
|
+
"requiresRestart": false,
|
|
66
|
+
"manualSteps": false
|
|
67
|
+
}
|
|
68
|
+
},
|
|
69
|
+
"peerDependencies": {
|
|
70
|
+
"@deepseek-ai/dsh": ">=0.2.1-rc.5 <0.3.0-0",
|
|
71
|
+
"react": "*"
|
|
72
|
+
}
|
|
73
|
+
}
|
package/screenshots.json
ADDED