deepccc 0.1.1 → 0.1.2
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 +217 -200
- package/dist/index.js +14 -8
- package/dist/privacy.d.ts +21 -0
- package/dist/privacy.js +124 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,200 +1,217 @@
|
|
|
1
|
-
# deepccc
|
|
2
|
-
|
|
3
|
-
`deepccc` 是一个轻量级本地编程 Agent,针对 DeepSeek 使用体验做了优化,同时支持其他 OpenAI-compatible 模型接口。
|
|
4
|
-
|
|
5
|
-
它提供交互式命令行、JSONL 流式输出、本地文件工具、命令执行、项目提示词自动注入和持久化上下文。
|
|
6
|
-
|
|
7
|
-
## 当前状态
|
|
8
|
-
|
|
9
|
-
代码已经开源在 GitHub:
|
|
10
|
-
|
|
11
|
-
https://github.com/wzj998/deepccc-agent
|
|
12
|
-
|
|
13
|
-
npm 包名规划为 `deepccc`。如果 npm 包已经发布,可以直接全局安装:
|
|
14
|
-
|
|
15
|
-
```bash
|
|
16
|
-
npm install -g deepccc
|
|
17
|
-
```
|
|
18
|
-
|
|
19
|
-
如果还没有发布,可以从源码运行:
|
|
20
|
-
|
|
21
|
-
```bash
|
|
22
|
-
git clone https://github.com/wzj998/deepccc-agent.git
|
|
23
|
-
cd deepccc-agent
|
|
24
|
-
npm install
|
|
25
|
-
npm run build
|
|
26
|
-
node bin/deepccc.mjs --help
|
|
27
|
-
```
|
|
28
|
-
|
|
29
|
-
运行要求:
|
|
30
|
-
|
|
31
|
-
- Node.js >= 20
|
|
32
|
-
- DeepSeek 或其他 OpenAI-compatible 模型服务的 API Key
|
|
33
|
-
|
|
34
|
-
## 配置
|
|
35
|
-
|
|
36
|
-
最快的方式是使用环境变量:
|
|
37
|
-
|
|
38
|
-
```bash
|
|
39
|
-
export DEEPCCC_API_KEY="sk-..."
|
|
40
|
-
export DEEPCCC_BASE_URL="https://api.deepseek.com/v1"
|
|
41
|
-
export DEEPCCC_MODEL="deepseek-v4-pro"
|
|
42
|
-
```
|
|
43
|
-
|
|
44
|
-
Windows PowerShell:
|
|
45
|
-
|
|
46
|
-
```powershell
|
|
47
|
-
$env:DEEPCCC_API_KEY="sk-..."
|
|
48
|
-
$env:DEEPCCC_BASE_URL="https://api.deepseek.com/v1"
|
|
49
|
-
$env:DEEPCCC_MODEL="deepseek-v4-pro"
|
|
50
|
-
```
|
|
51
|
-
|
|
52
|
-
也兼容这些 DeepSeek 别名:
|
|
53
|
-
|
|
54
|
-
- `DEEPSEEK_API_KEY`
|
|
55
|
-
- `DEEPSEEK_BASE_URL`
|
|
56
|
-
- `DEEPSEEK_MODEL`
|
|
57
|
-
- `DEEPSEEK_EFFORT`
|
|
58
|
-
|
|
59
|
-
也可以创建 `~/.deepccc/config.json`:
|
|
60
|
-
|
|
61
|
-
```json
|
|
62
|
-
{
|
|
63
|
-
"apiKey": "sk-...",
|
|
64
|
-
"baseURL": "https://api.deepseek.com/v1",
|
|
65
|
-
"model": "deepseek-v4-pro",
|
|
66
|
-
"effort": "",
|
|
67
|
-
"rawStreamLogs": {
|
|
68
|
-
"enabled": false,
|
|
69
|
-
"maxBytesPerTurn": 1048576,
|
|
70
|
-
"retentionDays": 7,
|
|
71
|
-
"keepCompleted": false
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
```
|
|
75
|
-
|
|
76
|
-
##
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
```
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
deepccc
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
`
|
|
188
|
-
|
|
189
|
-
-
|
|
190
|
-
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
1
|
+
# deepccc
|
|
2
|
+
|
|
3
|
+
`deepccc` 是一个轻量级本地编程 Agent,针对 DeepSeek 使用体验做了优化,同时支持其他 OpenAI-compatible 模型接口。
|
|
4
|
+
|
|
5
|
+
它提供交互式命令行、JSONL 流式输出、本地文件工具、命令执行、项目提示词自动注入和持久化上下文。
|
|
6
|
+
|
|
7
|
+
## 当前状态
|
|
8
|
+
|
|
9
|
+
代码已经开源在 GitHub:
|
|
10
|
+
|
|
11
|
+
https://github.com/wzj998/deepccc-agent
|
|
12
|
+
|
|
13
|
+
npm 包名规划为 `deepccc`。如果 npm 包已经发布,可以直接全局安装:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm install -g deepccc
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
如果还没有发布,可以从源码运行:
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
git clone https://github.com/wzj998/deepccc-agent.git
|
|
23
|
+
cd deepccc-agent
|
|
24
|
+
npm install
|
|
25
|
+
npm run build
|
|
26
|
+
node bin/deepccc.mjs --help
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
运行要求:
|
|
30
|
+
|
|
31
|
+
- Node.js >= 20
|
|
32
|
+
- DeepSeek 或其他 OpenAI-compatible 模型服务的 API Key
|
|
33
|
+
|
|
34
|
+
## 配置
|
|
35
|
+
|
|
36
|
+
最快的方式是使用环境变量:
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
export DEEPCCC_API_KEY="sk-..."
|
|
40
|
+
export DEEPCCC_BASE_URL="https://api.deepseek.com/v1"
|
|
41
|
+
export DEEPCCC_MODEL="deepseek-v4-pro"
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Windows PowerShell:
|
|
45
|
+
|
|
46
|
+
```powershell
|
|
47
|
+
$env:DEEPCCC_API_KEY="sk-..."
|
|
48
|
+
$env:DEEPCCC_BASE_URL="https://api.deepseek.com/v1"
|
|
49
|
+
$env:DEEPCCC_MODEL="deepseek-v4-pro"
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
也兼容这些 DeepSeek 别名:
|
|
53
|
+
|
|
54
|
+
- `DEEPSEEK_API_KEY`
|
|
55
|
+
- `DEEPSEEK_BASE_URL`
|
|
56
|
+
- `DEEPSEEK_MODEL`
|
|
57
|
+
- `DEEPSEEK_EFFORT`
|
|
58
|
+
|
|
59
|
+
也可以创建 `~/.deepccc/config.json`:
|
|
60
|
+
|
|
61
|
+
```json
|
|
62
|
+
{
|
|
63
|
+
"apiKey": "sk-...",
|
|
64
|
+
"baseURL": "https://api.deepseek.com/v1",
|
|
65
|
+
"model": "deepseek-v4-pro",
|
|
66
|
+
"effort": "",
|
|
67
|
+
"rawStreamLogs": {
|
|
68
|
+
"enabled": false,
|
|
69
|
+
"maxBytesPerTurn": 1048576,
|
|
70
|
+
"retentionDays": 7,
|
|
71
|
+
"keepCompleted": false
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
## 隐私替换
|
|
77
|
+
|
|
78
|
+
`deepccc` 支持在展示层(终端输出 / JSONL 流式输出)把敏感信息替换为掩码值,防止用户名、路径等隐私内容出现在终端或管道输出里。创建 `~/.deepccc/privacy.json` 即可启用:
|
|
79
|
+
|
|
80
|
+
```json
|
|
81
|
+
{
|
|
82
|
+
"enabled": true,
|
|
83
|
+
"rules": {
|
|
84
|
+
"weizhangjian": "wzj"
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
- `rules` 的键按字面量替换(`split`/`join`,非正则),可配置多条规则;不配置 `enabled`/`rules` 字段时,扁平写法 `{ "weizhangjian": "wzj" }` 也兼容(等效 `enabled: true`)。
|
|
90
|
+
- 替换作用于模型回复文本、工具调用参数与结果、错误消息中的字符串字段;持久化上下文仍保存原文,替换仅影响展示层。
|
|
91
|
+
- 文件变更后自动热加载,无需重启进程。
|
|
92
|
+
|
|
93
|
+
## 命令行交互
|
|
94
|
+
|
|
95
|
+
在当前目录启动一个交互式 Agent:
|
|
96
|
+
|
|
97
|
+
```bash
|
|
98
|
+
deepccc
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
指定其他模型或 OpenAI-compatible 接口:
|
|
102
|
+
|
|
103
|
+
```bash
|
|
104
|
+
deepccc --base-url https://api.openai.com/v1 --api-key "$OPENAI_API_KEY" --model gpt-4.1
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
指定工作目录:
|
|
108
|
+
|
|
109
|
+
```bash
|
|
110
|
+
deepccc --cwd /path/to/project
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
恢复当前工作目录最近一次会话:
|
|
114
|
+
|
|
115
|
+
```bash
|
|
116
|
+
deepccc --resume
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
设置工具调用步数上限:
|
|
120
|
+
|
|
121
|
+
```bash
|
|
122
|
+
deepccc --max-steps 20
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
默认情况下,`deepccc` 不设置固定步数上限,会让模型自然完成工具循环。
|
|
126
|
+
|
|
127
|
+
设置推理强度(reasoning effort):
|
|
128
|
+
|
|
129
|
+
```bash
|
|
130
|
+
deepccc --effort high
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
可选值:`none` / `minimal` / `low` / `medium` / `high` / `xhigh` / `max`(留空则不传 `reasoning_effort` 请求字段)。
|
|
134
|
+
|
|
135
|
+
## 终端过程区块
|
|
136
|
+
|
|
137
|
+
交互模式下,每轮回复渲染为固定"过程区块":状态行(生成中/完成/已停止/异常结束)+ 折叠工具行 + 原地更新正文,不滚屏刷 JSON。生成中有心跳点号动画;完成/停止/异常后区块定型留在屏幕上。
|
|
138
|
+
|
|
139
|
+
如果终端渲染出现异常,可以强制回退为纯文本流式输出:
|
|
140
|
+
|
|
141
|
+
```bash
|
|
142
|
+
deepccc --plain
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
## JSONL 流式输出
|
|
146
|
+
|
|
147
|
+
JSONL 模式适合脚本、服务端集成或其他上层系统调用:
|
|
148
|
+
|
|
149
|
+
```bash
|
|
150
|
+
deepccc --stream-json --prompt "检查这个仓库并总结测试命令"
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
也可以从 stdin 传入提示词:
|
|
154
|
+
|
|
155
|
+
```bash
|
|
156
|
+
echo "运行测试并解释失败原因" | deepccc --stream-json
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
输出是逐行 JSON:
|
|
160
|
+
|
|
161
|
+
```jsonl
|
|
162
|
+
{"type":"start","session_id":"session-...","mode":"new","cwd":"/repo","model":"deepseek-v4-pro"}
|
|
163
|
+
{"type":"text_delta","text":"...","accumulated":"..."}
|
|
164
|
+
{"type":"tool_call","id":"call_...","name":"read_file","input":{"path":"package.json"}}
|
|
165
|
+
{"type":"tool_result","tool_call_id":"call_...","name":"read_file","content":{},"is_error":false}
|
|
166
|
+
{"type":"done","text":"..."}
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
## 在 ChatCCC 中使用
|
|
170
|
+
|
|
171
|
+
ChatCCC 公有仓库:
|
|
172
|
+
|
|
173
|
+
https://github.com/wzj998/ChatCCC
|
|
174
|
+
|
|
175
|
+
在 ChatCCC 会话里可以使用隐藏指令创建 `deepccc` Agent 会话:
|
|
176
|
+
|
|
177
|
+
```text
|
|
178
|
+
/new ccc
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
这种方式适合已经在 ChatCCC 里协作的场景:ChatCCC 负责会话入口和消息通道,`deepccc` 负责本地编程 Agent 能力,包括读取项目提示词、运行命令、编辑文件和输出流式结果。
|
|
182
|
+
|
|
183
|
+
## 项目提示词自动注入
|
|
184
|
+
|
|
185
|
+
会话启动时,`deepccc` 会从当前工作目录读取这些文件,如果存在就注入为项目级提示词:
|
|
186
|
+
|
|
187
|
+
- `AGENTS.md`
|
|
188
|
+
- `AGENTS.local.md`
|
|
189
|
+
- `CLAUDE.md`
|
|
190
|
+
- `CLAUDE.local.md`
|
|
191
|
+
|
|
192
|
+
这些内容会放在固定系统提示词之后,作为项目指导使用。
|
|
193
|
+
|
|
194
|
+
## Codex-style Skills 支持
|
|
195
|
+
|
|
196
|
+
`deepccc` 会扫描以下目录,把本机的 Codex 目录式 skill(`<name>/SKILL.md`,含 `name` + `description` frontmatter)索引注入系统提示词;模型在任务匹配时先用 `read_file` 读取 `SKILL.md` 全文再执行:
|
|
197
|
+
|
|
198
|
+
- `~/.codex/skills`
|
|
199
|
+
- `~/.agents/skills`
|
|
200
|
+
- `<cwd>/.codex/skills`(项目级,优先级最高)
|
|
201
|
+
|
|
202
|
+
## 内置工具
|
|
203
|
+
|
|
204
|
+
`deepccc` 可以让模型调用这些本地工具:
|
|
205
|
+
|
|
206
|
+
- 按行读取文件
|
|
207
|
+
- 列目录
|
|
208
|
+
- 用 ripgrep 搜索代码
|
|
209
|
+
- 编辑、创建、删除、移动文件
|
|
210
|
+
- 应用 unified diff patch
|
|
211
|
+
- 运行非交互式 shell 命令,并返回 stdout、stderr、exitCode 和超时状态
|
|
212
|
+
|
|
213
|
+
命令返回非零退出码时不会直接被当成工具异常;模型可以读取结构化结果,继续判断下一步。
|
|
214
|
+
|
|
215
|
+
## License
|
|
216
|
+
|
|
217
|
+
Apache-2.0
|
package/dist/index.js
CHANGED
|
@@ -12,6 +12,7 @@ import { createRawStreamLog, } from "./raw-stream-log.js";
|
|
|
12
12
|
import { BuiltinContextManager, buildSummaryPrompt, defaultBuiltinSessionId, } from "./context.js";
|
|
13
13
|
import { createBuiltinFileTools } from "./file-tools.js";
|
|
14
14
|
import { buildDefaultSkillDirs, buildSkillsIndexPrompt, scanSkillsDirs } from "./skills.js";
|
|
15
|
+
import { applyPrivacy, applyPrivacyToJson } from "./privacy.js";
|
|
15
16
|
// ---------------------------------------------------------------------------
|
|
16
17
|
// 系统提示词 — 编译期冻结常量(DeepCCC 英文品牌)
|
|
17
18
|
// ---------------------------------------------------------------------------
|
|
@@ -130,6 +131,7 @@ export class ChatSession {
|
|
|
130
131
|
async *chat(userMessage, signal) {
|
|
131
132
|
this.context.appendMessage({ role: "user", content: userMessage });
|
|
132
133
|
let fullText = "";
|
|
134
|
+
let safeAccumulated = "";
|
|
133
135
|
let rawLog = null;
|
|
134
136
|
let completed = false;
|
|
135
137
|
try {
|
|
@@ -170,7 +172,11 @@ export class ChatSession {
|
|
|
170
172
|
rawLog?.writeLine(safeRawStreamJson(part));
|
|
171
173
|
if (part.type === "text-delta") {
|
|
172
174
|
fullText += part.text;
|
|
173
|
-
|
|
175
|
+
// 隐私替换只在展示层:safeAccumulated 供事件消费者(终端/JSONL)使用,
|
|
176
|
+
// fullText 原文用于持久化上下文,避免替换结果回流污染上下文。
|
|
177
|
+
const safeText = applyPrivacy(part.text);
|
|
178
|
+
safeAccumulated += safeText;
|
|
179
|
+
yield { type: "text", text: safeText, accumulated: safeAccumulated };
|
|
174
180
|
}
|
|
175
181
|
else if (part.type === "tool-call") {
|
|
176
182
|
toolContext.push(`tool_call ${part.toolName}: ${safeJson(part.input)}`);
|
|
@@ -178,7 +184,7 @@ export class ChatSession {
|
|
|
178
184
|
type: "tool_use",
|
|
179
185
|
id: part.toolCallId,
|
|
180
186
|
name: part.toolName,
|
|
181
|
-
input: part.input,
|
|
187
|
+
input: applyPrivacyToJson(part.input),
|
|
182
188
|
};
|
|
183
189
|
}
|
|
184
190
|
else if (part.type === "tool-result") {
|
|
@@ -187,7 +193,7 @@ export class ChatSession {
|
|
|
187
193
|
type: "tool_result",
|
|
188
194
|
tool_use_id: part.toolCallId,
|
|
189
195
|
name: part.toolName,
|
|
190
|
-
content: part.output,
|
|
196
|
+
content: applyPrivacyToJson(part.output),
|
|
191
197
|
is_error: false,
|
|
192
198
|
};
|
|
193
199
|
}
|
|
@@ -198,13 +204,13 @@ export class ChatSession {
|
|
|
198
204
|
type: "tool_result",
|
|
199
205
|
tool_use_id: part.toolCallId,
|
|
200
206
|
name: part.toolName,
|
|
201
|
-
content: message,
|
|
207
|
+
content: applyPrivacy(message),
|
|
202
208
|
is_error: true,
|
|
203
209
|
};
|
|
204
210
|
}
|
|
205
211
|
else if (part.type === "error") {
|
|
206
212
|
const message = errorMessage(part.error);
|
|
207
|
-
yield { type: "error", message };
|
|
213
|
+
yield { type: "error", message: applyPrivacy(message) };
|
|
208
214
|
throw new Error(message);
|
|
209
215
|
}
|
|
210
216
|
}
|
|
@@ -213,7 +219,7 @@ export class ChatSession {
|
|
|
213
219
|
? `${fullText}\n\n[Tool transcript]\n${toolContext.join("\n")}`
|
|
214
220
|
: fullText;
|
|
215
221
|
this.context.appendMessage({ role: "assistant", content: persistedText });
|
|
216
|
-
yield { type: "done", text:
|
|
222
|
+
yield { type: "done", text: safeAccumulated };
|
|
217
223
|
}
|
|
218
224
|
catch (err) {
|
|
219
225
|
const message = err instanceof Error ? err.message : String(err);
|
|
@@ -222,10 +228,10 @@ export class ChatSession {
|
|
|
222
228
|
if (fullText) {
|
|
223
229
|
this.context.appendMessage({ role: "assistant", content: `${fullText}\n[interrupted]` });
|
|
224
230
|
}
|
|
225
|
-
yield { type: "done", text:
|
|
231
|
+
yield { type: "done", text: safeAccumulated };
|
|
226
232
|
return;
|
|
227
233
|
}
|
|
228
|
-
yield { type: "error", message };
|
|
234
|
+
yield { type: "error", message: applyPrivacy(message) };
|
|
229
235
|
throw err;
|
|
230
236
|
}
|
|
231
237
|
finally {
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
interface PrivacyRules {
|
|
2
|
+
[key: string]: string;
|
|
3
|
+
}
|
|
4
|
+
interface PrivacyConfig {
|
|
5
|
+
enabled: boolean;
|
|
6
|
+
rules: PrivacyRules;
|
|
7
|
+
}
|
|
8
|
+
export declare function getPrivacyConfig(): PrivacyConfig;
|
|
9
|
+
export declare function getPrivacyRules(): PrivacyRules;
|
|
10
|
+
export declare function reloadPrivacyRules(): void;
|
|
11
|
+
/**
|
|
12
|
+
* 把文本中的隐私规则字面量替换为掩码值(split+join,规则键保持字面量)。
|
|
13
|
+
* 仅作用于展示层:调用方应保持内部数据(如持久化上下文)为原文。
|
|
14
|
+
*/
|
|
15
|
+
export declare function applyPrivacy(text: string): string;
|
|
16
|
+
/**
|
|
17
|
+
* 递归替换结构化数据(工具调用参数/结果)中的字符串字段。
|
|
18
|
+
* 数组逐项、对象逐值、字符串走 applyPrivacy,其余类型原样返回。
|
|
19
|
+
*/
|
|
20
|
+
export declare function applyPrivacyToJson(value: unknown): unknown;
|
|
21
|
+
export {};
|
package/dist/privacy.js
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { existsSync, readFileSync, statSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { DEEPCCC_HOME } from "./config.js";
|
|
4
|
+
let config = null;
|
|
5
|
+
let loadedStamp;
|
|
6
|
+
function privacyFilePath() {
|
|
7
|
+
return join(DEEPCCC_HOME, "privacy.json");
|
|
8
|
+
}
|
|
9
|
+
function privacyFileStamp() {
|
|
10
|
+
const filePath = privacyFilePath();
|
|
11
|
+
if (!existsSync(filePath))
|
|
12
|
+
return null;
|
|
13
|
+
try {
|
|
14
|
+
const s = statSync(filePath);
|
|
15
|
+
return `${s.mtimeMs}:${s.size}`;
|
|
16
|
+
}
|
|
17
|
+
catch {
|
|
18
|
+
return null;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
function sanitizeRules(raw) {
|
|
22
|
+
const result = {};
|
|
23
|
+
for (const [from, to] of Object.entries(raw)) {
|
|
24
|
+
if (!from) {
|
|
25
|
+
console.error("[PRIVACY] privacy.json rule key cannot be empty, skipped");
|
|
26
|
+
continue;
|
|
27
|
+
}
|
|
28
|
+
if (typeof to !== "string") {
|
|
29
|
+
console.error(`[PRIVACY] privacy.json value must be a string, skipped "${from}"`);
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
result[from] = to;
|
|
33
|
+
}
|
|
34
|
+
return result;
|
|
35
|
+
}
|
|
36
|
+
function normalizeConfig(parsed) {
|
|
37
|
+
const hasNewSchema = Object.prototype.hasOwnProperty.call(parsed, "enabled") ||
|
|
38
|
+
Object.prototype.hasOwnProperty.call(parsed, "rules");
|
|
39
|
+
if (!hasNewSchema) {
|
|
40
|
+
return { enabled: true, rules: sanitizeRules(parsed) };
|
|
41
|
+
}
|
|
42
|
+
const enabledRaw = parsed.enabled;
|
|
43
|
+
const enabled = enabledRaw === undefined ? true : enabledRaw !== false;
|
|
44
|
+
if (enabledRaw !== undefined && typeof enabledRaw !== "boolean") {
|
|
45
|
+
console.error("[PRIVACY] privacy.json enabled must be a boolean, using true");
|
|
46
|
+
}
|
|
47
|
+
const rulesRaw = parsed.rules;
|
|
48
|
+
if (rulesRaw === undefined)
|
|
49
|
+
return { enabled, rules: {} };
|
|
50
|
+
if (typeof rulesRaw !== "object" || rulesRaw === null || Array.isArray(rulesRaw)) {
|
|
51
|
+
console.error("[PRIVACY] privacy.json rules must be an object");
|
|
52
|
+
return { enabled, rules: {} };
|
|
53
|
+
}
|
|
54
|
+
return { enabled, rules: sanitizeRules(rulesRaw) };
|
|
55
|
+
}
|
|
56
|
+
function loadConfig() {
|
|
57
|
+
const filePath = privacyFilePath();
|
|
58
|
+
if (!existsSync(filePath)) {
|
|
59
|
+
return { enabled: true, rules: {} };
|
|
60
|
+
}
|
|
61
|
+
try {
|
|
62
|
+
const raw = readFileSync(filePath, "utf-8").replace(/^\uFEFF/, "");
|
|
63
|
+
const parsed = JSON.parse(raw);
|
|
64
|
+
if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) {
|
|
65
|
+
console.error("[PRIVACY] privacy.json must be an object");
|
|
66
|
+
return { enabled: true, rules: {} };
|
|
67
|
+
}
|
|
68
|
+
return normalizeConfig(parsed);
|
|
69
|
+
}
|
|
70
|
+
catch (err) {
|
|
71
|
+
console.error(`[PRIVACY] failed to read privacy.json: ${err.message}`);
|
|
72
|
+
return { enabled: true, rules: {} };
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
export function getPrivacyConfig() {
|
|
76
|
+
const stamp = privacyFileStamp();
|
|
77
|
+
if (!config || stamp !== loadedStamp) {
|
|
78
|
+
config = loadConfig();
|
|
79
|
+
loadedStamp = stamp;
|
|
80
|
+
}
|
|
81
|
+
return config;
|
|
82
|
+
}
|
|
83
|
+
export function getPrivacyRules() {
|
|
84
|
+
return getPrivacyConfig().rules;
|
|
85
|
+
}
|
|
86
|
+
export function reloadPrivacyRules() {
|
|
87
|
+
config = null;
|
|
88
|
+
loadedStamp = undefined;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* 把文本中的隐私规则字面量替换为掩码值(split+join,规则键保持字面量)。
|
|
92
|
+
* 仅作用于展示层:调用方应保持内部数据(如持久化上下文)为原文。
|
|
93
|
+
*/
|
|
94
|
+
export function applyPrivacy(text) {
|
|
95
|
+
const { enabled, rules } = getPrivacyConfig();
|
|
96
|
+
if (!enabled || Object.keys(rules).length === 0 || !text)
|
|
97
|
+
return text;
|
|
98
|
+
let result = text;
|
|
99
|
+
for (const [from, to] of Object.entries(rules)) {
|
|
100
|
+
result = result.split(from).join(to);
|
|
101
|
+
}
|
|
102
|
+
return result;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* 递归替换结构化数据(工具调用参数/结果)中的字符串字段。
|
|
106
|
+
* 数组逐项、对象逐值、字符串走 applyPrivacy,其余类型原样返回。
|
|
107
|
+
*/
|
|
108
|
+
export function applyPrivacyToJson(value) {
|
|
109
|
+
const { enabled, rules } = getPrivacyConfig();
|
|
110
|
+
if (!enabled || Object.keys(rules).length === 0)
|
|
111
|
+
return value;
|
|
112
|
+
if (typeof value === "string")
|
|
113
|
+
return applyPrivacy(value);
|
|
114
|
+
if (Array.isArray(value))
|
|
115
|
+
return value.map((item) => applyPrivacyToJson(item));
|
|
116
|
+
if (value !== null && typeof value === "object") {
|
|
117
|
+
const out = {};
|
|
118
|
+
for (const [key, item] of Object.entries(value)) {
|
|
119
|
+
out[key] = applyPrivacyToJson(item);
|
|
120
|
+
}
|
|
121
|
+
return out;
|
|
122
|
+
}
|
|
123
|
+
return value;
|
|
124
|
+
}
|