@chen_hu_hu/pi-qq 1.3.3 → 1.3.5
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/CHANGELOG.md +21 -1
- package/README-zh.md +6 -3
- package/README.md +6 -3
- package/doc/enhance-status.md +427 -0
- package/doc/send-skill.md +257 -0
- package/index.ts +38 -31
- package/package.json +1 -1
- package/src/attachments/attachment-download.ts +13 -8
- package/src/attachments/attachment-failure.ts +4 -2
- package/src/attachments/file-attachments.ts +11 -4
- package/src/infra/file-logger.ts +6 -2
- package/src/infra/i18n.ts +525 -0
- package/src/infra/power-status.ts +116 -0
- package/src/infra/settings.ts +28 -8
- package/src/qq/inbound-attachments.ts +2 -1
- package/src/qq/inbound-command.ts +17 -16
- package/src/qq/qq-bot.ts +4 -7
- package/src/qq/qq-diagnostics.ts +28 -21
- package/src/qq/qq-message-handler.ts +2 -1
- package/src/qq/qr-login.ts +9 -8
- package/src/qq/reply-sender.ts +4 -3
- package/src/qq/status-report.ts +112 -0
- package/src/quota/reply-quota-report.ts +36 -8
- package/src/relay/bubble-writer.ts +12 -3
- package/src/relay/run-stream-relay.ts +21 -5
- package/src/relay/stream-block-format.ts +30 -2
- package/src/relay/stream-session.ts +3 -4
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
# QQ 侧 Skill 执行显示设计
|
|
2
|
+
|
|
3
|
+
> 状态:设计文档,已实现
|
|
4
|
+
> 日期:2026-11-04
|
|
5
|
+
> 涉及版本:pi-qq 1.3.4,pi 1.5.0
|
|
6
|
+
> 证据截图:
|
|
7
|
+
>
|
|
8
|
+
> - pi TUI:`/home/chen/project/pi-qq-test/pi.png`
|
|
9
|
+
> - QQ:`/home/chen/project/pi-qq-test/qq.jpg`
|
|
10
|
+
|
|
11
|
+
## 1. 目标
|
|
12
|
+
|
|
13
|
+
用户在 QQ 上应该看到 **Skill 的执行**,而不是普通的 `read` 工具执行。
|
|
14
|
+
|
|
15
|
+
目标格式:
|
|
16
|
+
|
|
17
|
+
```markdown
|
|
18
|
+
## 🧩 Skill
|
|
19
|
+
|
|
20
|
+
`playwright-cli`
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
明确不做:
|
|
24
|
+
|
|
25
|
+
- 不显示 `SKILL.md` 的完整内容;
|
|
26
|
+
- 不显示 skill 的来源路径(全局/项目);
|
|
27
|
+
- 不支持 QQ 侧 `/skill:` 命令;
|
|
28
|
+
- 不区分“自动选择”和“显式调用”,只展示 skill 名称。
|
|
29
|
+
|
|
30
|
+
## 2. 调研结论
|
|
31
|
+
|
|
32
|
+
### 2.1 pi 的 `[skill]` 是什么
|
|
33
|
+
|
|
34
|
+
截图中的:
|
|
35
|
+
|
|
36
|
+
```text
|
|
37
|
+
[skill] playwright-cli (ctrl+o to expand)
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
是 pi TUI 对 **读取 `SKILL.md` 的 read 工具** 的特殊渲染,不是独立的 skill 事件。
|
|
41
|
+
|
|
42
|
+
pi 的 read 渲染器会检测文件名:
|
|
43
|
+
|
|
44
|
+
```ts
|
|
45
|
+
// pi: dist/core/tools/renderers/read.js
|
|
46
|
+
if (fileName === "SKILL.md") {
|
|
47
|
+
return { kind: "skill", label: basename(dirname(absolutePath)) };
|
|
48
|
+
}
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
所以 TUI 把它折叠成 `[skill]`;会话记录中实际仍然是:
|
|
52
|
+
|
|
53
|
+
```text
|
|
54
|
+
toolCall: read /home/chen/.pi/agent/skills/playwright-cli/SKILL.md
|
|
55
|
+
toolResult: SKILL.md 内容
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
对应会话:
|
|
59
|
+
|
|
60
|
+
```text
|
|
61
|
+
/home/chen/.pi/agent/sessions/--home-chen-project-pi-qq-test--/
|
|
62
|
+
2026-11-03T15-37-34-742Z_01a1a20f-0115-7773-a4c5-a011cda00d57.jsonl
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### 2.2 QQ 当前显示什么
|
|
66
|
+
|
|
67
|
+
`qq.jpg` 中显示的是:
|
|
68
|
+
|
|
69
|
+
- `📖 read` 工具气泡;
|
|
70
|
+
- 参数路径 `.../playwright-cli/SKILL.md`;
|
|
71
|
+
- `SKILL.md` 的完整内容;
|
|
72
|
+
- 后续思考和 bash。
|
|
73
|
+
|
|
74
|
+
因此当前 QQ **有内容**,但语义上是“read 了一个文件”,不是“执行了 skill”。这正是需要改变的地方。
|
|
75
|
+
|
|
76
|
+
### 2.3 显式 `/skill:` 路径
|
|
77
|
+
|
|
78
|
+
pi 也支持从 TUI 执行 `/skill:name`,会展开成一条包含 `<skill ...>` 的 user message,并用 `SkillInvocationMessageComponent` 渲染。
|
|
79
|
+
|
|
80
|
+
但本设计**不支持 QQ `/skill:`**:
|
|
81
|
+
|
|
82
|
+
- QQ 入站命令路由会把 `/skill:` 当未知命令;
|
|
83
|
+
- pi-qq 不转发 user message;
|
|
84
|
+
- 这些行为保持不变。
|
|
85
|
+
|
|
86
|
+
本次只处理模型自动读取 `SKILL.md` 的情况。
|
|
87
|
+
|
|
88
|
+
## 3. 当前实现路径
|
|
89
|
+
|
|
90
|
+
模型读取 `SKILL.md` 时,pi-qq 收到普通工具事件:
|
|
91
|
+
|
|
92
|
+
```text
|
|
93
|
+
tool_execution_start: toolName=read, args.path=/.../SKILL.md
|
|
94
|
+
tool_execution_end: result=SKILL.md 内容
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
当前链路:
|
|
98
|
+
|
|
99
|
+
- `src/relay/run-stream-relay.ts` 把 `read` 加入工具队列;
|
|
100
|
+
- `src/relay/bubble-writer.ts` 调用 `formatToolLine("read", args)`;
|
|
101
|
+
- `src/relay/stream-block-format.ts` 输出 `**📖 read**` + 路径;
|
|
102
|
+
- `onToolEnd` 再输出完整 `SKILL.md` 结果。
|
|
103
|
+
|
|
104
|
+
没有任何 skill 识别逻辑,所以 QQ 看到的是普通 read。
|
|
105
|
+
|
|
106
|
+
## 4. 根因
|
|
107
|
+
|
|
108
|
+
pi 的 `[skill]` 是 **展示层分类**,不是协议事件:
|
|
109
|
+
|
|
110
|
+
- 没有 `skill_start` / `skill_end`;
|
|
111
|
+
- pi-qq 无法从事件类型知道“这是 skill”;
|
|
112
|
+
- 唯一可用的信号是 `read` 工具参数中的 `SKILL.md` 路径;
|
|
113
|
+
- 当前格式化逻辑没有利用这个信号。
|
|
114
|
+
|
|
115
|
+
## 5. 修改方案
|
|
116
|
+
|
|
117
|
+
### 5.1 识别 skill read
|
|
118
|
+
|
|
119
|
+
新增纯函数,例如:
|
|
120
|
+
|
|
121
|
+
```ts
|
|
122
|
+
export interface SkillRead {
|
|
123
|
+
name: string;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export function detectSkillRead(toolName: string, args: unknown): SkillRead | undefined;
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
规则:
|
|
130
|
+
|
|
131
|
+
- `toolName === "read"`;
|
|
132
|
+
- 参数路径的最后一段是 `SKILL.md`;
|
|
133
|
+
- 支持 `path` 和 `file_path` 两种参数名;
|
|
134
|
+
- 支持 `/` 和 `\` 路径分隔符;
|
|
135
|
+
- skill 名称取父目录名:
|
|
136
|
+
- `.../playwright-cli/SKILL.md` → `playwright-cli`;
|
|
137
|
+
- `.../my-skill/SKILL.md` → `my-skill`。
|
|
138
|
+
|
|
139
|
+
不读取、不解析 `SKILL.md` 内容,也不显示路径。
|
|
140
|
+
|
|
141
|
+
### 5.2 渲染 skill 气泡
|
|
142
|
+
|
|
143
|
+
在 `formatToolLine()` 或 `BubbleWriter` 中增加 skill 分支,输出:
|
|
144
|
+
|
|
145
|
+
```markdown
|
|
146
|
+
## 🧩 Skill
|
|
147
|
+
|
|
148
|
+
`playwright-cli`
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
- 标题使用 Markdown 二级标题;
|
|
152
|
+
- skill 名称用行内代码;
|
|
153
|
+
- 不附加路径、来源、描述或内容。
|
|
154
|
+
|
|
155
|
+
标题固定为英文 `Skill`,不跟随 `language` 配置,也不做翻译。
|
|
156
|
+
|
|
157
|
+
### 5.3 抑制原 read 工具输出
|
|
158
|
+
|
|
159
|
+
检测到 skill read 后:
|
|
160
|
+
|
|
161
|
+
- **不输出** `📖 read` 工具行;
|
|
162
|
+
- **不输出** `SKILL.md` 的 tool result;
|
|
163
|
+
- 只输出一个 skill 气泡;
|
|
164
|
+
- 后续 assistant 文本/工具继续按原顺序输出。
|
|
165
|
+
|
|
166
|
+
实现上需要在 `ToolCall` 上记录 skill 信息:
|
|
167
|
+
|
|
168
|
+
```ts
|
|
169
|
+
interface ToolCall {
|
|
170
|
+
...
|
|
171
|
+
skillName?: string;
|
|
172
|
+
}
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
- `onToolStart` 检测并写入 `skillName`;
|
|
176
|
+
- `startNextTool` 如果 `skillName` 存在,调用 `writer.pushSkill(name)`;
|
|
177
|
+
- `finishTool` 对 skill 跳过 `pushToolResult`,但仍清理 active tool、启动下一个工具、flush deferred 文本。
|
|
178
|
+
|
|
179
|
+
### 5.4 顺序与气泡边界
|
|
180
|
+
|
|
181
|
+
- skill 气泡独立成泡;
|
|
182
|
+
- skill 之后如果还有工具,继续按工具顺序输出;
|
|
183
|
+
- 如果 skill 后面是 assistant 文本,文本从新气泡开始;
|
|
184
|
+
- 如果同一个 run 多次读取 `SKILL.md`,每次读取都会产生一个 skill 气泡;第一版不做去重。
|
|
185
|
+
|
|
186
|
+
### 5.5 失败处理
|
|
187
|
+
|
|
188
|
+
当前实现:
|
|
189
|
+
|
|
190
|
+
- 仍然显示 skill 气泡,表示模型尝试加载该 skill;
|
|
191
|
+
- 不显示 `SKILL.md` 内容;
|
|
192
|
+
- 不额外显示失败原因。后续如需提示加载失败,可以再调整。
|
|
193
|
+
|
|
194
|
+
## 6. 执行计划
|
|
195
|
+
|
|
196
|
+
### Phase 1:识别与格式
|
|
197
|
+
|
|
198
|
+
- [x] 新增 `detectSkillRead()`,处理 `path` / `file_path`、Unix/Windows 路径;
|
|
199
|
+
- [x] 新增 `formatSkillBlock(name)`;
|
|
200
|
+
- [x] 单元测试:
|
|
201
|
+
- `read .../playwright-cli/SKILL.md` → skill;
|
|
202
|
+
- 普通 `read` 不受影响;
|
|
203
|
+
- Windows 路径;
|
|
204
|
+
- 大小写/非 `SKILL.md` 文件名;
|
|
205
|
+
- 缺少父目录时的回退名称。
|
|
206
|
+
|
|
207
|
+
### Phase 2:接入 relay
|
|
208
|
+
|
|
209
|
+
- [x] `ToolCall` 增加 `skillName`;
|
|
210
|
+
- [x] `onToolStart` 检测 skill;
|
|
211
|
+
- [x] `BubbleWriter` 增加 `pushSkill(name)`;
|
|
212
|
+
- [x] `startNextTool` 对 skill 使用 `pushSkill`;
|
|
213
|
+
- [x] `finishTool` 对 skill 跳过 read 结果;
|
|
214
|
+
- [x] 测试:
|
|
215
|
+
- 只出现 skill 气泡,不出现 read 工具行/结果;
|
|
216
|
+
- skill 与前后工具的顺序;
|
|
217
|
+
- 流式熔断时 skill 仍按现有 fallback 行为;
|
|
218
|
+
- 多个 skill / 多次读取。
|
|
219
|
+
|
|
220
|
+
### Phase 3:文案与文档
|
|
221
|
+
|
|
222
|
+
- [x] 更新 README 的 skill 说明;
|
|
223
|
+
- [x] 更新 `CHANGELOG.md`。
|
|
224
|
+
|
|
225
|
+
### Phase 4:验证
|
|
226
|
+
|
|
227
|
+
- [x] `npm run lint`
|
|
228
|
+
- [x] `npm run typecheck`
|
|
229
|
+
- [x] `npm test`
|
|
230
|
+
- [x] `npm run format:check`
|
|
231
|
+
- [ ] 在 `pi-qq-test` 中让模型读取 `playwright-cli/SKILL.md`,对比:
|
|
232
|
+
- pi TUI:`[skill] playwright-cli`
|
|
233
|
+
- QQ:`## 🧩 Skill` + `playwright-cli`
|
|
234
|
+
- QQ 不再出现 `📖 read` 和 `SKILL.md` 全文
|
|
235
|
+
|
|
236
|
+
## 7. 变更文件
|
|
237
|
+
|
|
238
|
+
| 文件 | 变更 |
|
|
239
|
+
| ---------------------------------------- | ----------------------------------------- |
|
|
240
|
+
| `src/relay/stream-block-format.ts` | `detectSkillRead()`、`formatSkillBlock()` |
|
|
241
|
+
| `src/relay/run-stream-relay.ts` | `ToolCall.skillName`、start/finish 分支 |
|
|
242
|
+
| `src/relay/bubble-writer.ts` | `pushSkill()` |
|
|
243
|
+
| `test/relay/stream-block-format.test.ts` | skill 识别/格式测试 |
|
|
244
|
+
| `test/relay/run-stream-relay.test.ts` | skill 气泡与 read 抑制测试 |
|
|
245
|
+
| `README.md` / `README-zh.md` | skill 显示说明 |
|
|
246
|
+
| `CHANGELOG.md` | `[Unreleased]` 变更 |
|
|
247
|
+
|
|
248
|
+
## 8. 已确认
|
|
249
|
+
|
|
250
|
+
1. QQ 显示 skill 执行,不显示普通 read 工具;
|
|
251
|
+
2. 格式为 `## 🧩 Skill` + skill 名称;
|
|
252
|
+
3. 标题固定英文,不跟随语言配置;
|
|
253
|
+
4. 不显示 `SKILL.md` 完整内容;
|
|
254
|
+
5. 不支持 QQ `/skill:` 命令;
|
|
255
|
+
6. 不显示 skill 来源;
|
|
256
|
+
7. 读取失败时仍显示 skill 气泡,不显示错误;
|
|
257
|
+
8. 同一个 run 多次读取 `SKILL.md` 不去重。
|
package/index.ts
CHANGED
|
@@ -8,8 +8,10 @@ import { registerQQDiagnostics } from "./src/qq/qq-diagnostics";
|
|
|
8
8
|
import { markdownToPlain, sendReply } from "./src/qq/reply-sender";
|
|
9
9
|
import { ReplyBudget } from "./src/quota/reply-quota";
|
|
10
10
|
import { RunFeedback } from "./src/relay/run-stream-relay";
|
|
11
|
-
import { formatTokenCount } from "./src/infra/display-format";
|
|
12
11
|
import { configureLogger, flushLogs, isLogEnabled, log, previewForLog } from "./src/infra/file-logger";
|
|
12
|
+
import { configureI18n, t } from "./src/infra/i18n";
|
|
13
|
+
import { getPowerStatus } from "./src/infra/power-status";
|
|
14
|
+
import { formatStatusReport } from "./src/qq/status-report";
|
|
13
15
|
import { pruneUploadIndex, sessionUploadDir } from "./src/attachments/file-attachments";
|
|
14
16
|
|
|
15
17
|
interface LoginState {
|
|
@@ -43,10 +45,9 @@ const runFeedback = new RunFeedback({
|
|
|
43
45
|
onWarn: (msg) => sessionCtx?.ui.notify(`pi-qq ${msg}`, "warning"),
|
|
44
46
|
});
|
|
45
47
|
|
|
46
|
-
const fmtTokens = (n: number | null): string => (n === null ? "unknown" : formatTokenCount(n));
|
|
47
|
-
|
|
48
48
|
export default function (pi: ExtensionAPI) {
|
|
49
|
-
|
|
49
|
+
activeSettings = getSettings(readConfig().config);
|
|
50
|
+
configureI18n(activeSettings.language);
|
|
50
51
|
|
|
51
52
|
const getAllowedOpenid = () => getLoginState()?.allowedOpenid;
|
|
52
53
|
const getDeliverAs = () => activeSettings.deliverAs;
|
|
@@ -56,6 +57,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
56
57
|
const applyConfig = (ctx: ExtensionContext) => {
|
|
57
58
|
const { config, error } = readConfig();
|
|
58
59
|
activeSettings = getSettings(config);
|
|
60
|
+
configureI18n(activeSettings.language);
|
|
59
61
|
if (error) ctx.ui.notify(`pi-qq ${error}`, "error");
|
|
60
62
|
return { settings: activeSettings, error };
|
|
61
63
|
};
|
|
@@ -63,18 +65,23 @@ export default function (pi: ExtensionAPI) {
|
|
|
63
65
|
const getStatusLines = (ctx: ExtensionContext | undefined = sessionCtx): string[] => {
|
|
64
66
|
const login = getLoginState();
|
|
65
67
|
const usage = ctx?.getContextUsage();
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
68
|
+
return formatStatusReport({
|
|
69
|
+
login: login ? { appId: login.credentials.appId, openid: login.allowedOpenid } : undefined,
|
|
70
|
+
connection: { state: lastState, detail: stateDetail },
|
|
71
|
+
agent: {
|
|
72
|
+
running: ctx ? !ctx.isIdle() : runFeedback.isQQRunActive(),
|
|
73
|
+
qq: runFeedback.isQQRunActive(),
|
|
74
|
+
},
|
|
75
|
+
model: ctx?.model ? { name: ctx.model.name } : undefined,
|
|
76
|
+
usage: usage
|
|
77
|
+
? {
|
|
78
|
+
tokens: usage.tokens,
|
|
79
|
+
contextWindow: usage.contextWindow,
|
|
80
|
+
percent: usage.percent,
|
|
81
|
+
}
|
|
82
|
+
: undefined,
|
|
83
|
+
power: getPowerStatus(),
|
|
84
|
+
});
|
|
78
85
|
};
|
|
79
86
|
|
|
80
87
|
const stopIfRunning = () => {
|
|
@@ -124,8 +131,8 @@ export default function (pi: ExtensionAPI) {
|
|
|
124
131
|
onStateChange: (state, detail) => {
|
|
125
132
|
lastState = state;
|
|
126
133
|
stateDetail = detail;
|
|
127
|
-
if (state === "connected") ctx.ui.notify("
|
|
128
|
-
else if (state === "blocked") ctx.ui.notify(`pi-qq ${detail ?? "
|
|
134
|
+
if (state === "connected") ctx.ui.notify(t("notify.connected"), "info");
|
|
135
|
+
else if (state === "blocked") ctx.ui.notify(`pi-qq ${detail ?? t("notify.connectionBlocked")}`, "error");
|
|
129
136
|
else if (state === "stopped" && detail) ctx.ui.notify(`pi-qq ${detail}`, "warning");
|
|
130
137
|
},
|
|
131
138
|
});
|
|
@@ -147,7 +154,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
147
154
|
if (login) {
|
|
148
155
|
await startWithCredentials(ctx, login.credentials, login.allowedOpenid);
|
|
149
156
|
} else if (ctx.hasUI) {
|
|
150
|
-
ctx.ui.notify("
|
|
157
|
+
ctx.ui.notify(t("notify.loginRequired"), "warning");
|
|
151
158
|
}
|
|
152
159
|
});
|
|
153
160
|
|
|
@@ -228,7 +235,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
228
235
|
|
|
229
236
|
let text: string;
|
|
230
237
|
if (isError) {
|
|
231
|
-
text =
|
|
238
|
+
text = t("errors.model", { message: msg.errorMessage ?? t("status.unknown") });
|
|
232
239
|
} else {
|
|
233
240
|
text = msg.content
|
|
234
241
|
.filter((b) => b.type === "text")
|
|
@@ -253,7 +260,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
253
260
|
const bound = getLoginState()?.allowedOpenid;
|
|
254
261
|
if (!bound) {
|
|
255
262
|
log.warn("pi", "no bound user; reply skipped");
|
|
256
|
-
ctx.ui.notify("
|
|
263
|
+
ctx.ui.notify(t("notify.noBoundUser"), "warning");
|
|
257
264
|
return;
|
|
258
265
|
}
|
|
259
266
|
|
|
@@ -266,7 +273,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
266
273
|
});
|
|
267
274
|
|
|
268
275
|
pi.registerCommand("qq-login", {
|
|
269
|
-
description: "
|
|
276
|
+
description: t("commands.descQqLogin"),
|
|
270
277
|
handler: async (_args, ctx) => {
|
|
271
278
|
applyConfig(ctx);
|
|
272
279
|
stopIfRunning();
|
|
@@ -276,13 +283,13 @@ export default function (pi: ExtensionAPI) {
|
|
|
276
283
|
result = await runQrLogin(ctx);
|
|
277
284
|
} catch (err) {
|
|
278
285
|
log.error("login", "QR login command failed", { err });
|
|
279
|
-
ctx.ui.notify("
|
|
286
|
+
ctx.ui.notify(t("login.failed"), "error");
|
|
280
287
|
return;
|
|
281
288
|
}
|
|
282
289
|
if (!result) return;
|
|
283
290
|
if (!result.userOpenid) {
|
|
284
291
|
log.error("login", "QR login did not return a user openid");
|
|
285
|
-
ctx.ui.notify("
|
|
292
|
+
ctx.ui.notify(t("login.failed"), "error");
|
|
286
293
|
return;
|
|
287
294
|
}
|
|
288
295
|
|
|
@@ -292,36 +299,36 @@ export default function (pi: ExtensionAPI) {
|
|
|
292
299
|
await startWithCredentials(ctx, credentials, result.userOpenid);
|
|
293
300
|
} catch (err) {
|
|
294
301
|
log.error("login", "starting bot after login failed", { err });
|
|
295
|
-
ctx.ui.notify("
|
|
302
|
+
ctx.ui.notify(t("login.startFailed"), "error");
|
|
296
303
|
}
|
|
297
304
|
},
|
|
298
305
|
});
|
|
299
306
|
|
|
300
307
|
pi.registerCommand("qq-status", {
|
|
301
|
-
description: "
|
|
308
|
+
description: t("commands.descQqStatus"),
|
|
302
309
|
handler: async (_args, ctx) => {
|
|
303
310
|
ctx.ui.notify(getStatusLines(ctx).join("\n"), "info");
|
|
304
311
|
},
|
|
305
312
|
});
|
|
306
313
|
|
|
307
314
|
pi.registerCommand("qq-logout", {
|
|
308
|
-
description: "
|
|
315
|
+
description: t("commands.descQqLogout"),
|
|
309
316
|
handler: async (_args, ctx) => {
|
|
310
317
|
stopIfRunning();
|
|
311
318
|
setLoginState(undefined);
|
|
312
|
-
ctx.ui.notify("
|
|
319
|
+
ctx.ui.notify(t("notify.loggedOut"), "info");
|
|
313
320
|
},
|
|
314
321
|
});
|
|
315
322
|
|
|
316
323
|
pi.registerCommand("qq-new", {
|
|
317
|
-
description: "
|
|
324
|
+
description: t("commands.descQqNew"),
|
|
318
325
|
handler: async (_args, ctx) => {
|
|
319
326
|
if (!ctx.isIdle()) {
|
|
320
|
-
ctx.ui.notify("
|
|
327
|
+
ctx.ui.notify(t("notify.busyReset"), "warning");
|
|
321
328
|
return;
|
|
322
329
|
}
|
|
323
330
|
const { cancelled } = await ctx.newSession();
|
|
324
|
-
if (cancelled) ctx.ui.notify("
|
|
331
|
+
if (cancelled) ctx.ui.notify(t("notify.resetCancelled"), "warning");
|
|
325
332
|
},
|
|
326
333
|
});
|
|
327
334
|
|
package/package.json
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { formatBytes } from "../infra/display-format";
|
|
2
|
+
import { t } from "../infra/i18n";
|
|
2
3
|
|
|
3
4
|
export interface InboundAttachmentLike {
|
|
4
5
|
content_type: string;
|
|
@@ -19,7 +20,9 @@ async function readBodyLimited(response: Response, limit: number, kind: string):
|
|
|
19
20
|
if (!response.body) {
|
|
20
21
|
const body = Buffer.from(await response.arrayBuffer());
|
|
21
22
|
if (body.byteLength > limit) {
|
|
22
|
-
throw new Error(
|
|
23
|
+
throw new Error(
|
|
24
|
+
t("attachments.tooLarge", { kind, size: formatBytes(body.byteLength), limit: formatBytes(limit) }),
|
|
25
|
+
);
|
|
23
26
|
}
|
|
24
27
|
return body;
|
|
25
28
|
}
|
|
@@ -35,14 +38,14 @@ async function readBodyLimited(response: Response, limit: number, kind: string):
|
|
|
35
38
|
total += value.byteLength;
|
|
36
39
|
if (total > limit) {
|
|
37
40
|
await reader.cancel();
|
|
38
|
-
throw new Error(
|
|
41
|
+
throw new Error(t("attachments.tooLargeShort", { kind, limit: formatBytes(limit) }));
|
|
39
42
|
}
|
|
40
43
|
chunks.push(value);
|
|
41
44
|
}
|
|
42
45
|
} finally {
|
|
43
46
|
reader.releaseLock();
|
|
44
47
|
}
|
|
45
|
-
if (total === 0) throw new Error(
|
|
48
|
+
if (total === 0) throw new Error(t("attachments.empty", { kind }));
|
|
46
49
|
return Buffer.concat(chunks, total);
|
|
47
50
|
}
|
|
48
51
|
|
|
@@ -52,10 +55,10 @@ export async function downloadLimited(opts: DownloadOptions): Promise<Buffer> {
|
|
|
52
55
|
try {
|
|
53
56
|
parsed = new URL(opts.url);
|
|
54
57
|
} catch {
|
|
55
|
-
throw new Error(
|
|
58
|
+
throw new Error(t("attachments.invalidUrl", { kind: opts.kind }));
|
|
56
59
|
}
|
|
57
60
|
if (parsed.protocol !== "http:" && parsed.protocol !== "https:") {
|
|
58
|
-
throw new Error(
|
|
61
|
+
throw new Error(t("attachments.unsupportedProtocol", { kind: opts.kind }));
|
|
59
62
|
}
|
|
60
63
|
|
|
61
64
|
const controller = new AbortController();
|
|
@@ -64,19 +67,21 @@ export async function downloadLimited(opts: DownloadOptions): Promise<Buffer> {
|
|
|
64
67
|
try {
|
|
65
68
|
const response = await fetchImpl(parsed.toString(), { signal: controller.signal, redirect: "follow" });
|
|
66
69
|
if (!response.ok) {
|
|
67
|
-
throw new Error(
|
|
70
|
+
throw new Error(t("attachments.httpFailed", { status: response.status }));
|
|
68
71
|
}
|
|
69
72
|
|
|
70
73
|
const lengthHeader = response.headers.get("content-length");
|
|
71
74
|
const contentLength = lengthHeader ? Number(lengthHeader) : NaN;
|
|
72
75
|
if (Number.isFinite(contentLength) && contentLength > limit) {
|
|
73
76
|
await response.body?.cancel();
|
|
74
|
-
throw new Error(
|
|
77
|
+
throw new Error(
|
|
78
|
+
t("attachments.tooLarge", { kind: opts.kind, size: formatBytes(contentLength), limit: formatBytes(limit) }),
|
|
79
|
+
);
|
|
75
80
|
}
|
|
76
81
|
return await readBodyLimited(response, limit, opts.kind);
|
|
77
82
|
} catch (err) {
|
|
78
83
|
if (controller.signal.aborted) {
|
|
79
|
-
throw new Error(
|
|
84
|
+
throw new Error(t("attachments.timeout", { seconds: opts.timeoutMs / 1000 }), { cause: err });
|
|
80
85
|
}
|
|
81
86
|
throw err;
|
|
82
87
|
} finally {
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { t } from "../infra/i18n";
|
|
2
|
+
|
|
1
3
|
export interface AttachmentFailure {
|
|
2
4
|
filename: string;
|
|
3
5
|
reason: string;
|
|
@@ -6,7 +8,7 @@ export interface AttachmentFailure {
|
|
|
6
8
|
export function formatAttachmentFailures(failures: readonly AttachmentFailure[], successCount: number): string {
|
|
7
9
|
const title =
|
|
8
10
|
successCount > 0
|
|
9
|
-
?
|
|
10
|
-
: "
|
|
11
|
+
? t("attachments.failedMany", { failed: failures.length, success: successCount })
|
|
12
|
+
: t("attachments.failedNone");
|
|
11
13
|
return [title, ...failures.map((failure) => `• ${failure.filename}: ${failure.reason}`)].join("\n");
|
|
12
14
|
}
|
|
@@ -5,6 +5,7 @@ import { downloadLimited, type InboundAttachmentLike } from "./attachment-downlo
|
|
|
5
5
|
import type { AttachmentFailure } from "./attachment-failure";
|
|
6
6
|
import { formatBytes } from "../infra/display-format";
|
|
7
7
|
import { log } from "../infra/file-logger";
|
|
8
|
+
import { t } from "../infra/i18n";
|
|
8
9
|
|
|
9
10
|
const DOWNLOAD_TIMEOUT_MS = 60_000;
|
|
10
11
|
const UPLOAD_INDEX_FILENAME = ".index.json";
|
|
@@ -179,7 +180,7 @@ async function saveOrReuse(
|
|
|
179
180
|
return { file: { path: candidatePath }, changed: true };
|
|
180
181
|
}
|
|
181
182
|
|
|
182
|
-
throw new Error("
|
|
183
|
+
throw new Error(t("attachments.noUniqueName"));
|
|
183
184
|
}
|
|
184
185
|
|
|
185
186
|
export async function saveInboundFiles(
|
|
@@ -207,7 +208,13 @@ export async function saveInboundFiles(
|
|
|
207
208
|
const fallback = `file-${position + 1}`;
|
|
208
209
|
const filename = sanitizeFilename(attachment.filename, fallback);
|
|
209
210
|
if (typeof attachment.size === "number" && attachment.size > limit) {
|
|
210
|
-
failures.push({
|
|
211
|
+
failures.push({
|
|
212
|
+
filename,
|
|
213
|
+
reason: t("attachments.fileTooLarge", {
|
|
214
|
+
size: formatBytes(attachment.size),
|
|
215
|
+
limit: formatBytes(limit),
|
|
216
|
+
}),
|
|
217
|
+
});
|
|
211
218
|
continue;
|
|
212
219
|
}
|
|
213
220
|
|
|
@@ -216,14 +223,14 @@ export async function saveInboundFiles(
|
|
|
216
223
|
url: attachment.url,
|
|
217
224
|
maxSize: limit,
|
|
218
225
|
timeoutMs: DOWNLOAD_TIMEOUT_MS,
|
|
219
|
-
kind: "
|
|
226
|
+
kind: t("attachments.kindFile"),
|
|
220
227
|
fetchImpl,
|
|
221
228
|
});
|
|
222
229
|
const result = await saveOrReuse(uploadDir, filename, body, index);
|
|
223
230
|
indexChanged ||= result.changed;
|
|
224
231
|
addFile(result.file);
|
|
225
232
|
} catch (err) {
|
|
226
|
-
failures.push({ filename, reason: err instanceof Error ? err.message : "
|
|
233
|
+
failures.push({ filename, reason: err instanceof Error ? err.message : t("attachments.downloadFailed") });
|
|
227
234
|
}
|
|
228
235
|
}
|
|
229
236
|
|
package/src/infra/file-logger.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { appendFile, mkdir } from "node:fs/promises";
|
|
2
2
|
import { join } from "node:path";
|
|
3
|
+
import { localizeLogMessage } from "./i18n";
|
|
3
4
|
|
|
4
5
|
export type LogLevel = "silent" | "error" | "warn" | "info" | "debug";
|
|
5
6
|
type LogFields = Record<string, unknown>;
|
|
@@ -78,11 +79,13 @@ function stringifyError(err: unknown): string {
|
|
|
78
79
|
function writeRecord(level: Exclude<LogLevel, "silent">, scope: string, message: string, data?: unknown): void {
|
|
79
80
|
if (!isLevelEnabled(activeLevel, level)) return;
|
|
80
81
|
|
|
82
|
+
const localized = localizeLogMessage(message);
|
|
81
83
|
const record: LogFields = {
|
|
82
84
|
time: new Date().toISOString(),
|
|
83
85
|
level,
|
|
84
86
|
scope,
|
|
85
|
-
|
|
87
|
+
...(localized.msgKey ? { msgKey: localized.msgKey } : {}),
|
|
88
|
+
msg: localized.msg,
|
|
86
89
|
};
|
|
87
90
|
const fields = normalizeData(data);
|
|
88
91
|
if (fields) {
|
|
@@ -105,7 +108,8 @@ function writeRecord(level: Exclude<LogLevel, "silent">, scope: string, message:
|
|
|
105
108
|
time: record.time,
|
|
106
109
|
level,
|
|
107
110
|
scope,
|
|
108
|
-
|
|
111
|
+
...(localized.msgKey ? { msgKey: localized.msgKey } : {}),
|
|
112
|
+
msg: localized.msg,
|
|
109
113
|
serializationError: stringifyError(err),
|
|
110
114
|
});
|
|
111
115
|
}
|