@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
package/src/infra/settings.ts
CHANGED
|
@@ -2,12 +2,14 @@ import { readFileSync } from "node:fs";
|
|
|
2
2
|
import { homedir } from "node:os";
|
|
3
3
|
import { join } from "node:path";
|
|
4
4
|
import { normalizeLogLevel, type LogLevel } from "./file-logger";
|
|
5
|
+
import { DEFAULT_LANGUAGE, getLanguage, isLanguage, translate, type Language } from "./i18n";
|
|
5
6
|
|
|
6
7
|
export interface PiQQSettings {
|
|
7
8
|
thinkingRelay?: boolean;
|
|
8
9
|
deliverAs?: "steer" | "followUp";
|
|
9
10
|
fileMaxSize?: number;
|
|
10
11
|
logLevel?: LogLevel;
|
|
12
|
+
language?: Language;
|
|
11
13
|
debug?: boolean;
|
|
12
14
|
}
|
|
13
15
|
|
|
@@ -43,36 +45,47 @@ export function parseConfig(raw: unknown): ReadConfigResult {
|
|
|
43
45
|
if (!isRecord(raw)) {
|
|
44
46
|
return {
|
|
45
47
|
config: {},
|
|
46
|
-
error:
|
|
48
|
+
error: translate(getLanguage(), "settings.invalidRoot", { path: CONFIG_PATH }),
|
|
47
49
|
};
|
|
48
50
|
}
|
|
49
51
|
|
|
52
|
+
const language = isLanguage(raw.language) ? raw.language : getLanguage();
|
|
50
53
|
const config: PiQQSettings = {};
|
|
51
54
|
const errors: string[] = [];
|
|
52
55
|
|
|
56
|
+
if (raw.language !== undefined) {
|
|
57
|
+
if (isLanguage(raw.language)) config.language = raw.language;
|
|
58
|
+
else errors.push(translate(language, "settings.language"));
|
|
59
|
+
}
|
|
53
60
|
if (raw.thinkingRelay !== undefined) {
|
|
54
61
|
if (typeof raw.thinkingRelay === "boolean") config.thinkingRelay = raw.thinkingRelay;
|
|
55
|
-
else errors.push("thinkingRelay
|
|
62
|
+
else errors.push(translate(language, "settings.thinkingRelay"));
|
|
56
63
|
}
|
|
57
64
|
if (raw.deliverAs !== undefined) {
|
|
58
65
|
if (raw.deliverAs === "steer" || raw.deliverAs === "followUp") config.deliverAs = raw.deliverAs;
|
|
59
|
-
else errors.push(
|
|
66
|
+
else errors.push(translate(language, "settings.deliverAs"));
|
|
60
67
|
}
|
|
61
68
|
if (raw.fileMaxSize !== undefined) {
|
|
62
69
|
if (isPositiveSize(raw.fileMaxSize)) config.fileMaxSize = raw.fileMaxSize;
|
|
63
|
-
else errors.push("fileMaxSize
|
|
70
|
+
else errors.push(translate(language, "settings.fileMaxSize"));
|
|
64
71
|
}
|
|
65
72
|
if (raw.logLevel !== undefined) {
|
|
66
73
|
if (isLogLevel(raw.logLevel)) config.logLevel = raw.logLevel;
|
|
67
|
-
else errors.push(
|
|
74
|
+
else errors.push(translate(language, "settings.logLevel"));
|
|
68
75
|
}
|
|
69
76
|
if (raw.debug !== undefined) {
|
|
70
77
|
if (typeof raw.debug === "boolean") config.debug = raw.debug;
|
|
71
|
-
else errors.push("debug
|
|
78
|
+
else errors.push(translate(language, "settings.debug"));
|
|
72
79
|
}
|
|
73
80
|
|
|
74
81
|
return errors.length > 0
|
|
75
|
-
? {
|
|
82
|
+
? {
|
|
83
|
+
config,
|
|
84
|
+
error: translate(language, "settings.invalidFields", {
|
|
85
|
+
path: CONFIG_PATH,
|
|
86
|
+
errors: errors.join("; "),
|
|
87
|
+
}),
|
|
88
|
+
}
|
|
76
89
|
: { config };
|
|
77
90
|
}
|
|
78
91
|
|
|
@@ -86,7 +99,13 @@ export function readConfig(): ReadConfigResult {
|
|
|
86
99
|
try {
|
|
87
100
|
return parseConfig(JSON.parse(raw));
|
|
88
101
|
} catch (e) {
|
|
89
|
-
return {
|
|
102
|
+
return {
|
|
103
|
+
config: {},
|
|
104
|
+
error: translate(getLanguage(), "settings.parseFailed", {
|
|
105
|
+
path: CONFIG_PATH,
|
|
106
|
+
message: (e as Error).message,
|
|
107
|
+
}),
|
|
108
|
+
};
|
|
90
109
|
}
|
|
91
110
|
}
|
|
92
111
|
|
|
@@ -99,5 +118,6 @@ export function getSettings(config: PiQQSettings): ResolvedSettings {
|
|
|
99
118
|
deliverAs: config.deliverAs ?? "steer",
|
|
100
119
|
fileMaxSize: config.fileMaxSize ?? 50 * 1024 * 1024,
|
|
101
120
|
logLevel,
|
|
121
|
+
language: config.language ?? DEFAULT_LANGUAGE,
|
|
102
122
|
};
|
|
103
123
|
}
|
|
@@ -2,6 +2,7 @@ import type { QQBotInboundMessage } from "@tencent-connect/qqbot-nodejs";
|
|
|
2
2
|
import type { AttachmentFailure } from "../attachments/attachment-failure";
|
|
3
3
|
import { saveInboundFiles, type SavedFile } from "../attachments/file-attachments";
|
|
4
4
|
import { log } from "../infra/file-logger";
|
|
5
|
+
import { t } from "../infra/i18n";
|
|
5
6
|
import { isVoiceAttachment, resolveVoiceTranscript } from "../attachments/voice-transcript";
|
|
6
7
|
|
|
7
8
|
export interface ResolveInboundOptions {
|
|
@@ -45,7 +46,7 @@ export async function resolveInboundContent(
|
|
|
45
46
|
log.warn("inbound", "voice ASR returned no text", { messageId: msg.messageId });
|
|
46
47
|
return {
|
|
47
48
|
status: "rejected",
|
|
48
|
-
reply: "
|
|
49
|
+
reply: t("inbound.voiceFailed"),
|
|
49
50
|
};
|
|
50
51
|
}
|
|
51
52
|
log.warn("inbound", "voice ASR returned no text; using text content", { messageId: msg.messageId });
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { t } from "../infra/i18n";
|
|
2
3
|
|
|
3
4
|
/** The slice of ExtensionContext used by QQ inbound handling. */
|
|
4
5
|
export interface InboundContext {
|
|
@@ -36,33 +37,33 @@ export async function handleInboundCommand(
|
|
|
36
37
|
case "/help":
|
|
37
38
|
await reply(
|
|
38
39
|
[
|
|
39
|
-
"
|
|
40
|
-
"
|
|
41
|
-
"
|
|
42
|
-
"
|
|
43
|
-
"
|
|
44
|
-
"
|
|
45
|
-
"
|
|
46
|
-
"
|
|
40
|
+
t("commands.help.title"),
|
|
41
|
+
t("commands.help.stop"),
|
|
42
|
+
t("commands.help.compact"),
|
|
43
|
+
t("commands.help.status"),
|
|
44
|
+
t("commands.help.last"),
|
|
45
|
+
t("commands.help.new"),
|
|
46
|
+
t("commands.help.help"),
|
|
47
|
+
t("commands.help.other"),
|
|
47
48
|
].join("\n"),
|
|
48
49
|
);
|
|
49
50
|
break;
|
|
50
51
|
case "/stop":
|
|
51
52
|
deps.onStop?.();
|
|
52
53
|
if (deps.ctx.isIdle()) {
|
|
53
|
-
await reply("
|
|
54
|
+
await reply(t("commands.stop.none"));
|
|
54
55
|
} else {
|
|
55
56
|
deps.ctx.abort();
|
|
56
57
|
await deps.waitForEnd?.();
|
|
57
|
-
await reply("
|
|
58
|
+
await reply(t("commands.stop.abort"));
|
|
58
59
|
}
|
|
59
60
|
break;
|
|
60
61
|
case "/compact":
|
|
61
62
|
if (deps.ctx.isIdle()) {
|
|
62
63
|
deps.ctx.compact();
|
|
63
|
-
await reply("
|
|
64
|
+
await reply(t("commands.compact.done"));
|
|
64
65
|
} else {
|
|
65
|
-
await reply("
|
|
66
|
+
await reply(t("commands.compact.busy"));
|
|
66
67
|
}
|
|
67
68
|
break;
|
|
68
69
|
case "/status":
|
|
@@ -80,20 +81,20 @@ export async function handleInboundCommand(
|
|
|
80
81
|
await reply(text);
|
|
81
82
|
}
|
|
82
83
|
} else {
|
|
83
|
-
await reply("
|
|
84
|
+
await reply(t("commands.last.none"));
|
|
84
85
|
}
|
|
85
86
|
}
|
|
86
87
|
break;
|
|
87
88
|
}
|
|
88
89
|
case "/new":
|
|
89
90
|
if (!deps.ctx.isIdle()) {
|
|
90
|
-
await reply("
|
|
91
|
+
await reply(t("commands.new.busy"));
|
|
91
92
|
} else {
|
|
92
|
-
await reply("
|
|
93
|
+
await reply(t("commands.new.resetting"));
|
|
93
94
|
deps.pi.sendUserMessage("/qq-new", { expandPromptTemplates: true });
|
|
94
95
|
}
|
|
95
96
|
break;
|
|
96
97
|
default:
|
|
97
|
-
await reply(
|
|
98
|
+
await reply(t("commands.unknown", { cmd }));
|
|
98
99
|
}
|
|
99
100
|
}
|
package/src/qq/qq-bot.ts
CHANGED
|
@@ -9,6 +9,7 @@ import {
|
|
|
9
9
|
import type { QQBotInboundMessage } from "@tencent-connect/qqbot-nodejs";
|
|
10
10
|
import type { Credentials } from "../infra/settings";
|
|
11
11
|
import { log, sdkLogger } from "../infra/file-logger";
|
|
12
|
+
import { t } from "../infra/i18n";
|
|
12
13
|
|
|
13
14
|
export type BotState = "stopped" | "starting" | "connected" | "reconnecting" | "blocked";
|
|
14
15
|
|
|
@@ -91,15 +92,11 @@ export function startBot(opts: StartBotOptions): RunningBot {
|
|
|
91
92
|
const code = (err as { code?: string }).code;
|
|
92
93
|
if (code === GatewayErrorCode.FATAL_CLOSE) {
|
|
93
94
|
const closeCode = (err as { closeCode?: number }).closeCode;
|
|
94
|
-
report(
|
|
95
|
-
"blocked",
|
|
96
|
-
`Connection closed by the server (${closeCode ?? "unknown"}, 4914=offline/sandbox, 4915=banned). ` +
|
|
97
|
-
`Check the bot status on the QQ Open Platform, or run /qq-login again.`,
|
|
98
|
-
);
|
|
95
|
+
report("blocked", t("bot.fatalClose", { code: closeCode ?? "unknown" }));
|
|
99
96
|
} else if (code === GatewayErrorCode.RETRY_EXHAUSTED) {
|
|
100
|
-
report("stopped",
|
|
97
|
+
report("stopped", t("bot.retryExhausted", { message: (err as Error).message }));
|
|
101
98
|
} else {
|
|
102
|
-
report("stopped",
|
|
99
|
+
report("stopped", t("bot.startupFailed", { message: (err as Error).message }));
|
|
103
100
|
}
|
|
104
101
|
});
|
|
105
102
|
|
package/src/qq/qq-diagnostics.ts
CHANGED
|
@@ -3,6 +3,8 @@ import type { ReplyBudget } from "../quota/reply-quota";
|
|
|
3
3
|
import { formatBudgetStatus } from "../quota/reply-quota-report";
|
|
4
4
|
import { AppendStream } from "../relay/stream-session";
|
|
5
5
|
import type { RunningBot } from "./qq-bot";
|
|
6
|
+
import { t } from "../infra/i18n";
|
|
7
|
+
import { thinkingHeader } from "../relay/stream-block-format";
|
|
6
8
|
|
|
7
9
|
export interface QQDiagnosticsDeps {
|
|
8
10
|
getRunning: () => RunningBot | undefined;
|
|
@@ -23,46 +25,46 @@ export function registerQQDiagnostics(pi: ExtensionAPI, deps: QQDiagnosticsDeps)
|
|
|
23
25
|
};
|
|
24
26
|
|
|
25
27
|
pi.registerCommand("qq-test-budget", {
|
|
26
|
-
description: "
|
|
28
|
+
description: t("diagnostics.descBudget"),
|
|
27
29
|
handler: async (_args, ctx) => {
|
|
28
30
|
ctx.ui.notify(formatBudgetStatus(deps.budget.snapshot(), deps.budget.lastTarget).join("\n"), "info");
|
|
29
31
|
},
|
|
30
32
|
});
|
|
31
33
|
|
|
32
34
|
pi.registerCommand("qq-test-md", {
|
|
33
|
-
description: "
|
|
35
|
+
description: t("diagnostics.descMd"),
|
|
34
36
|
handler: async (args, ctx) => {
|
|
35
37
|
const running = deps.getRunning();
|
|
36
38
|
if (!running) {
|
|
37
|
-
ctx.ui.notify("
|
|
39
|
+
ctx.ui.notify(t("diagnostics.notConnected"), "warning");
|
|
38
40
|
return;
|
|
39
41
|
}
|
|
40
42
|
const target = diagTarget();
|
|
41
43
|
if (!target) {
|
|
42
|
-
ctx.ui.notify("
|
|
44
|
+
ctx.ui.notify(t("diagnostics.noTarget"), "warning");
|
|
43
45
|
return;
|
|
44
46
|
}
|
|
45
47
|
const content = args?.trim() || "**Markdown test**\n# Heading\n- Item one\n- Item two\n[Link](https://qq.com)";
|
|
46
48
|
try {
|
|
47
49
|
await running.bot.sendMarkdown(target, content);
|
|
48
|
-
ctx.ui.notify("
|
|
50
|
+
ctx.ui.notify(t("diagnostics.mdSent"), "info");
|
|
49
51
|
} catch (err) {
|
|
50
|
-
ctx.ui.notify(
|
|
52
|
+
ctx.ui.notify(t("diagnostics.mdFailed", { message: (err as Error).message }), "error");
|
|
51
53
|
}
|
|
52
54
|
},
|
|
53
55
|
});
|
|
54
56
|
|
|
55
57
|
pi.registerCommand("qq-test-stream", {
|
|
56
|
-
description: "
|
|
58
|
+
description: t("diagnostics.descStream"),
|
|
57
59
|
handler: async (args, ctx) => {
|
|
58
60
|
const running = deps.getRunning();
|
|
59
61
|
if (!running) {
|
|
60
|
-
ctx.ui.notify("
|
|
62
|
+
ctx.ui.notify(t("diagnostics.notConnected"), "warning");
|
|
61
63
|
return;
|
|
62
64
|
}
|
|
63
65
|
const last = deps.budget.lastTarget;
|
|
64
66
|
if (!last) {
|
|
65
|
-
ctx.ui.notify("
|
|
67
|
+
ctx.ui.notify(t("diagnostics.streamNoTarget"), "warning");
|
|
66
68
|
return;
|
|
67
69
|
}
|
|
68
70
|
|
|
@@ -72,13 +74,13 @@ export function registerQQDiagnostics(pi: ExtensionAPI, deps: QQDiagnosticsDeps)
|
|
|
72
74
|
if (args?.trim() === "resume") {
|
|
73
75
|
const first = new AppendStream(running.bot.messageApi, running.bot.getCredentials(), last.openid, last.msgId);
|
|
74
76
|
stream = first;
|
|
75
|
-
await first.append("
|
|
77
|
+
await first.append(t("diagnostics.probeResumeFrame1"));
|
|
76
78
|
await first.flush();
|
|
77
79
|
const checkpoint = first.checkpoint();
|
|
78
80
|
first.dispose();
|
|
79
81
|
stream = undefined;
|
|
80
82
|
if (!checkpoint?.streamMsgId) {
|
|
81
|
-
ctx.ui.notify("
|
|
83
|
+
ctx.ui.notify(t("diagnostics.resumeNoStream"), "error");
|
|
82
84
|
return;
|
|
83
85
|
}
|
|
84
86
|
await sleep(800);
|
|
@@ -90,12 +92,15 @@ export function registerQQDiagnostics(pi: ExtensionAPI, deps: QQDiagnosticsDeps)
|
|
|
90
92
|
{ resume: checkpoint },
|
|
91
93
|
);
|
|
92
94
|
stream = second;
|
|
93
|
-
await second.append("
|
|
95
|
+
await second.append(t("diagnostics.probeResumeFrame2"));
|
|
94
96
|
await second.done();
|
|
95
97
|
second.dispose();
|
|
96
98
|
stream = undefined;
|
|
97
99
|
ctx.ui.notify(
|
|
98
|
-
|
|
100
|
+
t("diagnostics.resumeResult", {
|
|
101
|
+
index: checkpoint.index,
|
|
102
|
+
streamMsgId: checkpoint.streamMsgId,
|
|
103
|
+
}),
|
|
99
104
|
"info",
|
|
100
105
|
);
|
|
101
106
|
return;
|
|
@@ -103,26 +108,28 @@ export function registerQQDiagnostics(pi: ExtensionAPI, deps: QQDiagnosticsDeps)
|
|
|
103
108
|
|
|
104
109
|
stream = new AppendStream(running.bot.messageApi, running.bot.getCredentials(), last.openid, last.msgId);
|
|
105
110
|
await stream.append(
|
|
106
|
-
"
|
|
111
|
+
`${thinkingHeader()}${t("diagnostics.probeThinking")}\n\n**💻 bash**\n\n\`echo probe\`\n\n\`\`\`text\n${t("diagnostics.probeCode")}\n\`\`\`\n`,
|
|
107
112
|
);
|
|
108
113
|
await sleep(1200);
|
|
109
|
-
await stream.append("\n
|
|
114
|
+
await stream.append(`\n${t("diagnostics.probeIntermediate")}\n`);
|
|
110
115
|
await sleep(1200);
|
|
111
|
-
await stream.append(
|
|
116
|
+
await stream.append(
|
|
117
|
+
`\n**📖 read**\n\n\`src/index.ts\`\n\n\`\`\`text\n${t("diagnostics.probeReadFile")}\n\`\`\`\n`,
|
|
118
|
+
);
|
|
112
119
|
await sleep(800);
|
|
113
|
-
await stream.append(
|
|
114
|
-
let doneNote = "
|
|
120
|
+
await stream.append(`\n${t("diagnostics.probeFinal")}`);
|
|
121
|
+
let doneNote = t("diagnostics.doneSucceeded");
|
|
115
122
|
try {
|
|
116
123
|
await stream.done();
|
|
117
124
|
} catch (doneErr) {
|
|
118
|
-
doneNote =
|
|
125
|
+
doneNote = t("diagnostics.doneRejected", { message: (doneErr as Error).message });
|
|
119
126
|
}
|
|
120
127
|
stream.dispose();
|
|
121
128
|
stream = undefined;
|
|
122
|
-
ctx.ui.notify(
|
|
129
|
+
ctx.ui.notify(t("diagnostics.streamResult", { done: doneNote }), "info");
|
|
123
130
|
} catch (err) {
|
|
124
131
|
stream?.dispose();
|
|
125
|
-
ctx.ui.notify(
|
|
132
|
+
ctx.ui.notify(t("diagnostics.streamFailed", { message: (err as Error).message }), "error");
|
|
126
133
|
}
|
|
127
134
|
},
|
|
128
135
|
});
|
|
@@ -9,6 +9,7 @@ import {
|
|
|
9
9
|
type SavedFile,
|
|
10
10
|
} from "../attachments/file-attachments";
|
|
11
11
|
import { log } from "../infra/file-logger";
|
|
12
|
+
import { t } from "../infra/i18n";
|
|
12
13
|
import { handleInboundCommand, type InboundCommandDeps } from "./inbound-command";
|
|
13
14
|
import { PendingAttachments, resolveInboundContent } from "./inbound-attachments";
|
|
14
15
|
|
|
@@ -107,7 +108,7 @@ export function createInboundHandler(deps: InboundDeps) {
|
|
|
107
108
|
const attachments = msg.attachments ?? [];
|
|
108
109
|
if (!contentText && attachments.length === 0) {
|
|
109
110
|
log.warn("inbound", "unsupported message: no text and no attachments", { messageId: msg.messageId });
|
|
110
|
-
await reply("
|
|
111
|
+
await reply(t("inbound.unsupported"));
|
|
111
112
|
return;
|
|
112
113
|
}
|
|
113
114
|
|
package/src/qq/qr-login.ts
CHANGED
|
@@ -3,6 +3,7 @@ import { generate } from "qrcode-terminal";
|
|
|
3
3
|
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
4
4
|
import type { Credentials } from "../infra/settings";
|
|
5
5
|
import { log } from "../infra/file-logger";
|
|
6
|
+
import { t } from "../infra/i18n";
|
|
6
7
|
|
|
7
8
|
export interface QrLoginResult extends Credentials {
|
|
8
9
|
userOpenid?: string;
|
|
@@ -12,7 +13,7 @@ export async function runQrLogin(ctx: ExtensionContext): Promise<QrLoginResult |
|
|
|
12
13
|
const ac = new AbortController();
|
|
13
14
|
log.info("login", "QR login started");
|
|
14
15
|
|
|
15
|
-
const qr = { lines: [] as string[], hint: "
|
|
16
|
+
const qr = { lines: [] as string[], hint: t("login.fetchingQr") };
|
|
16
17
|
let requestRender: (() => void) | undefined;
|
|
17
18
|
let closeOverlay: (() => void) | undefined;
|
|
18
19
|
|
|
@@ -46,22 +47,22 @@ export async function runQrLogin(ctx: ExtensionContext): Promise<QrLoginResult |
|
|
|
46
47
|
causeCode: cause && typeof cause === "object" ? (cause as NodeJS.ErrnoException).code : undefined,
|
|
47
48
|
causeMessage: cause instanceof Error ? cause.message : undefined,
|
|
48
49
|
});
|
|
49
|
-
ctx.ui.notify("
|
|
50
|
+
ctx.ui.notify(t("login.failed"), "error");
|
|
50
51
|
finish();
|
|
51
52
|
closeOverlay?.();
|
|
52
53
|
},
|
|
53
54
|
onQrDisplayed: (url) => {
|
|
54
55
|
log.debug("login", "QR code displayed");
|
|
55
|
-
qr.hint = "
|
|
56
|
+
qr.hint = t("login.scan");
|
|
56
57
|
generate(url, { small: true }, (ascii) => {
|
|
57
58
|
qr.lines = ascii.split("\n").filter((line) => line.trim().length > 0);
|
|
58
59
|
});
|
|
59
60
|
requestRender?.();
|
|
60
|
-
if (ctx.mode !== "tui") ctx.ui.notify(
|
|
61
|
+
if (ctx.mode !== "tui") ctx.ui.notify(t("login.qrUrl", { url }), "info");
|
|
61
62
|
},
|
|
62
63
|
onQrExpired: () => {
|
|
63
64
|
log.warn("login", "QR code expired; refreshing");
|
|
64
|
-
qr.hint = "
|
|
65
|
+
qr.hint = t("login.expired");
|
|
65
66
|
requestRender?.();
|
|
66
67
|
},
|
|
67
68
|
},
|
|
@@ -69,7 +70,7 @@ export async function runQrLogin(ctx: ExtensionContext): Promise<QrLoginResult |
|
|
|
69
70
|
);
|
|
70
71
|
|
|
71
72
|
if (ctx.mode !== "tui") {
|
|
72
|
-
ctx.ui.notify("
|
|
73
|
+
ctx.ui.notify(t("login.waiting"), "info");
|
|
73
74
|
return;
|
|
74
75
|
}
|
|
75
76
|
|
|
@@ -79,8 +80,8 @@ export async function runQrLogin(ctx: ExtensionContext): Promise<QrLoginResult |
|
|
|
79
80
|
requestRender = () => tui.requestRender();
|
|
80
81
|
return {
|
|
81
82
|
render(width: number): string[] {
|
|
82
|
-
const lines = qr.lines.length > 0 ? qr.lines : ["
|
|
83
|
-
return [...lines, "", qr.hint, theme.fg("dim", "
|
|
83
|
+
const lines = qr.lines.length > 0 ? qr.lines : [t("login.fetchingQrShort")];
|
|
84
|
+
return [...lines, "", qr.hint, theme.fg("dim", t("login.cancel"))].map((line) =>
|
|
84
85
|
line.length > width ? line.slice(0, width) : line,
|
|
85
86
|
);
|
|
86
87
|
},
|
package/src/qq/reply-sender.ts
CHANGED
|
@@ -2,6 +2,7 @@ import type { QQBot } from "@tencent-connect/qqbot-nodejs";
|
|
|
2
2
|
import { ReplyBudget } from "../quota/reply-quota";
|
|
3
3
|
import { AppendStream } from "../relay/stream-session";
|
|
4
4
|
import { log } from "../infra/file-logger";
|
|
5
|
+
import { t } from "../infra/i18n";
|
|
5
6
|
|
|
6
7
|
export const TEXT_CHUNK_LIMIT = 4500;
|
|
7
8
|
|
|
@@ -131,15 +132,15 @@ export async function sendReply(bot: QQBot, text: string, ch: ReplyChannel): Pro
|
|
|
131
132
|
try {
|
|
132
133
|
log.warn("outbound", "passive send failed; retrying proactively", { targetId: ch.boundOpenid });
|
|
133
134
|
await bot.sendText({ scope: "c2c", targetId: ch.boundOpenid }, chunk);
|
|
134
|
-
ch.onWarn?.("
|
|
135
|
+
ch.onWarn?.(t("outbound.passiveFailed"));
|
|
135
136
|
continue;
|
|
136
137
|
} catch (retryErr) {
|
|
137
138
|
log.error("outbound", "proactive retry failed", { targetId: ch.boundOpenid, err: retryErr });
|
|
138
|
-
ch.onWarn?.(
|
|
139
|
+
ch.onWarn?.(t("outbound.proactiveFailed", { message: (retryErr as Error).message }));
|
|
139
140
|
continue;
|
|
140
141
|
}
|
|
141
142
|
}
|
|
142
|
-
ch.onWarn?.(
|
|
143
|
+
ch.onWarn?.(t("outbound.sendFailed", { message: (err as Error).message }));
|
|
143
144
|
}
|
|
144
145
|
}
|
|
145
146
|
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import type { PowerStatus } from "../infra/power-status";
|
|
2
|
+
import { formatTokenCount } from "../infra/display-format";
|
|
3
|
+
import { t, type TranslationKey } from "../infra/i18n";
|
|
4
|
+
import type { BotState } from "./qq-bot";
|
|
5
|
+
|
|
6
|
+
export interface StatusReportInput {
|
|
7
|
+
login?: { appId: string; openid: string };
|
|
8
|
+
connection: { state: BotState; detail?: string };
|
|
9
|
+
agent: { running: boolean; qq: boolean };
|
|
10
|
+
model?: { name: string };
|
|
11
|
+
usage?: { tokens: number | null; contextWindow: number | null; percent?: number | null };
|
|
12
|
+
power?: PowerStatus;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const STATE_ICONS: Record<BotState, string> = {
|
|
16
|
+
connected: "🌐",
|
|
17
|
+
reconnecting: "🔄",
|
|
18
|
+
stopped: "⛔",
|
|
19
|
+
starting: "⏳",
|
|
20
|
+
blocked: "🚫",
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const STATE_LABELS: Record<BotState, TranslationKey> = {
|
|
24
|
+
connected: "status.state.connected",
|
|
25
|
+
reconnecting: "status.state.reconnecting",
|
|
26
|
+
stopped: "status.state.stopped",
|
|
27
|
+
starting: "status.state.starting",
|
|
28
|
+
blocked: "status.state.blocked",
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
function maskAppId(appId: string): string {
|
|
32
|
+
return `${appId.slice(0, 4)}****`;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function maskOpenid(openid: string): string {
|
|
36
|
+
return `${openid.slice(0, 8)}…`;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function label(
|
|
40
|
+
key:
|
|
41
|
+
| "status.agent"
|
|
42
|
+
| "status.context"
|
|
43
|
+
| "status.model"
|
|
44
|
+
| "status.login"
|
|
45
|
+
| "status.connection"
|
|
46
|
+
| "status.qqUser"
|
|
47
|
+
| "status.battery"
|
|
48
|
+
| "status.settings",
|
|
49
|
+
value: string,
|
|
50
|
+
): string {
|
|
51
|
+
return `${t(key)}${t("status.separator")}${value}`;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function agentLine(agent: StatusReportInput["agent"]): string {
|
|
55
|
+
if (!agent.running) return `✅ ${label("status.agent", t("status.agent.completed"))}`;
|
|
56
|
+
const value = agent.qq ? t("status.agent.runningQQ") : t("status.agent.runningLocal");
|
|
57
|
+
return `🏃 ${label("status.agent", value)}`;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function contextLine(usage: StatusReportInput["usage"]): string {
|
|
61
|
+
if (!usage) return `📊 ${label("status.context", t("status.unknown"))}`;
|
|
62
|
+
const percent = usage.percent?.toFixed(1) ?? "?";
|
|
63
|
+
const tokens = usage.tokens === null ? t("status.unknown") : formatTokenCount(usage.tokens);
|
|
64
|
+
const window = usage.contextWindow === null ? t("status.unknown") : formatTokenCount(usage.contextWindow);
|
|
65
|
+
return `📊 ${label("status.context", t("status.contextValue", { tokens, window, percent }))}`;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function batteryLine(power: PowerStatus | undefined): string | undefined {
|
|
69
|
+
if (!power) return undefined;
|
|
70
|
+
const icon = power.state === "charging" ? "🔌" : power.state === "discharging" ? "🔋" : "⚡";
|
|
71
|
+
const value =
|
|
72
|
+
power.percent === undefined
|
|
73
|
+
? t(
|
|
74
|
+
power.state === "charging"
|
|
75
|
+
? "status.batteryChargingNoPercent"
|
|
76
|
+
: power.state === "discharging"
|
|
77
|
+
? "status.batteryDischargingNoPercent"
|
|
78
|
+
: "status.batteryAcNoPercent",
|
|
79
|
+
)
|
|
80
|
+
: t(
|
|
81
|
+
power.state === "charging"
|
|
82
|
+
? "status.batteryCharging"
|
|
83
|
+
: power.state === "discharging"
|
|
84
|
+
? "status.batteryDischarging"
|
|
85
|
+
: "status.batteryAc",
|
|
86
|
+
{ percent: power.percent },
|
|
87
|
+
);
|
|
88
|
+
return `${icon} ${label("status.battery", value)}`;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export function formatStatusReport(input: StatusReportInput): string[] {
|
|
92
|
+
const stateLabel = t(STATE_LABELS[input.connection.state]);
|
|
93
|
+
const detail = input.connection.detail ? t("status.detail", { detail: input.connection.detail }) : "";
|
|
94
|
+
const loginLine = input.login
|
|
95
|
+
? `🔐 ${label("status.login", t("status.loginValue", { appId: maskAppId(input.login.appId) }))}`
|
|
96
|
+
: `⚪ ${label("status.login", t("status.notLoggedIn"))}`;
|
|
97
|
+
|
|
98
|
+
const lines = [
|
|
99
|
+
`🖥️ ${t("status.title")}`,
|
|
100
|
+
loginLine,
|
|
101
|
+
`${STATE_ICONS[input.connection.state]} ${label("status.connection", `${stateLabel}${detail}`)}`,
|
|
102
|
+
agentLine(input.agent),
|
|
103
|
+
`🧠 ${label("status.model", input.model?.name ?? t("status.none"))}`,
|
|
104
|
+
contextLine(input.usage),
|
|
105
|
+
`👤 ${label("status.qqUser", input.login ? maskOpenid(input.login.openid) : t("status.none"))}`,
|
|
106
|
+
];
|
|
107
|
+
|
|
108
|
+
const battery = batteryLine(input.power);
|
|
109
|
+
if (battery) lines.push(battery);
|
|
110
|
+
lines.push(`⚙️ ${label("status.settings", "~/.pi/agent/pi-qq.json")}`);
|
|
111
|
+
return lines;
|
|
112
|
+
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { t } from "../infra/i18n";
|
|
1
2
|
import type { BudgetSnapshot } from "./reply-quota";
|
|
2
3
|
|
|
3
4
|
interface BudgetTarget {
|
|
@@ -18,14 +19,41 @@ export function formatBudgetStatus(
|
|
|
18
19
|
const ageSec = last ? Math.max(0, Math.floor((now - last.arrivedAt) / 1000)) : null;
|
|
19
20
|
const estimated = snapshot.hasTarget ? Math.max(0, snapshot.remaining - snapshot.streamBubbles) : null;
|
|
20
21
|
|
|
22
|
+
const lastLine = last
|
|
23
|
+
? t("diagnostics.budget.lastInbound", {
|
|
24
|
+
openid: mask(last.openid),
|
|
25
|
+
msgId: mask(last.msgId),
|
|
26
|
+
age: ageSec ?? 0,
|
|
27
|
+
})
|
|
28
|
+
: t("diagnostics.budget.lastInboundNone");
|
|
29
|
+
|
|
30
|
+
const remainingLine = snapshot.hasTarget
|
|
31
|
+
? t("diagnostics.budget.passiveRemaining", { remaining: snapshot.remaining })
|
|
32
|
+
: t("diagnostics.budget.passiveRemainingNone");
|
|
33
|
+
|
|
34
|
+
const estimatedLine =
|
|
35
|
+
estimated === null
|
|
36
|
+
? t("diagnostics.budget.estimatedNone")
|
|
37
|
+
: t("diagnostics.budget.estimated", { remaining: estimated });
|
|
38
|
+
|
|
39
|
+
const windowLine = snapshot.hasTarget
|
|
40
|
+
? t("diagnostics.budget.window", {
|
|
41
|
+
window: snapshot.fresh ? t("diagnostics.budget.valid") : t("diagnostics.budget.expired"),
|
|
42
|
+
})
|
|
43
|
+
: t("diagnostics.budget.windowNone");
|
|
44
|
+
|
|
21
45
|
return [
|
|
22
|
-
"
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
46
|
+
t("diagnostics.budget.title"),
|
|
47
|
+
t("diagnostics.budget.limit", { limit: snapshot.limit, hours: snapshot.ttlHours }),
|
|
48
|
+
lastLine,
|
|
49
|
+
remainingLine,
|
|
50
|
+
t("diagnostics.budget.streamBubbles", { count: snapshot.streamBubbles }),
|
|
51
|
+
estimatedLine,
|
|
52
|
+
windowLine,
|
|
53
|
+
t("diagnostics.budget.recorded", {
|
|
54
|
+
passive: snapshot.totalReplies,
|
|
55
|
+
stream: snapshot.streamBubblesTotal,
|
|
56
|
+
tracked: snapshot.trackedMessages,
|
|
57
|
+
}),
|
|
30
58
|
];
|
|
31
59
|
}
|