t3-code-qqbot 1.2.2 → 1.2.3
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 +2 -2
- package/dist/bridge.mjs +396 -96
- package/dist/cli.mjs +197 -85
- package/dist/publish.mjs +1 -1
- package/docs/SETUP.md +4 -4
- package/package.json +1 -1
- package/scripts/install-windows-task.ps1 +44 -10
- package/scripts/start-windows.ps1 +4 -17
package/dist/bridge.mjs
CHANGED
|
@@ -125,7 +125,8 @@ async function copyStagedAttachment(root, file, directory, index) {
|
|
|
125
125
|
}
|
|
126
126
|
|
|
127
127
|
// src/config.ts
|
|
128
|
-
var APP_VERSION = "1.2.
|
|
128
|
+
var APP_VERSION = "1.2.3";
|
|
129
|
+
var SELF_UPDATE_EXIT_CODE = 75;
|
|
129
130
|
var DEFAULT_ATTACHMENT_HOSTS = [
|
|
130
131
|
"multimedia.nt.qq.com.cn",
|
|
131
132
|
"multimedia.nt.qq.com",
|
|
@@ -487,8 +488,9 @@ var Observer = class {
|
|
|
487
488
|
// src/slash-commands.ts
|
|
488
489
|
import { execFile } from "node:child_process";
|
|
489
490
|
import { existsSync } from "node:fs";
|
|
490
|
-
import { mkdir as mkdir2, readFile as readFile3, unlink, writeFile as writeFile2 } from "node:fs/promises";
|
|
491
|
-
import {
|
|
491
|
+
import { cp, mkdir as mkdir2, mkdtemp, readFile as readFile3, rm, unlink, writeFile as writeFile2 } from "node:fs/promises";
|
|
492
|
+
import { tmpdir } from "node:os";
|
|
493
|
+
import { dirname as dirname2, join as join3, resolve as resolve3 } from "node:path";
|
|
492
494
|
import { promisify } from "node:util";
|
|
493
495
|
var execFileAsync = promisify(execFile);
|
|
494
496
|
var SLASH_COMMAND_MAP = {
|
|
@@ -519,6 +521,7 @@ var SLASH_COMMAND_MAP = {
|
|
|
519
521
|
"/t": "think",
|
|
520
522
|
"/provider": "provider",
|
|
521
523
|
"/providers": "provider",
|
|
524
|
+
"/provide": "provider",
|
|
522
525
|
"/p": "provider",
|
|
523
526
|
"/tasks": "tasks",
|
|
524
527
|
"/cancel": "cancel",
|
|
@@ -777,8 +780,43 @@ function createSlashCommandHandler(deps) {
|
|
|
777
780
|
}
|
|
778
781
|
return project;
|
|
779
782
|
};
|
|
780
|
-
const getCapabilities = () => deps.observer.call("orchestrator_capabilities", {});
|
|
781
|
-
const getThreadConfig = (threadId) => deps.observer.call("t3_thread_configure", { threadId });
|
|
783
|
+
const getCapabilities = (search) => deps.observer.call("orchestrator_capabilities", search ? { search } : {});
|
|
784
|
+
const getThreadConfig = (threadId) => deps.getThreadConfig ? deps.getThreadConfig(threadId) : deps.observer.call("t3_thread_configure", { threadId });
|
|
785
|
+
const setThreadModel = async (threadId, modelSelection) => {
|
|
786
|
+
if (deps.setThreadModel) {
|
|
787
|
+
await deps.setThreadModel(threadId, modelSelection);
|
|
788
|
+
return;
|
|
789
|
+
}
|
|
790
|
+
await deps.observer.call("t3_thread_configure", { threadId, modelSelection });
|
|
791
|
+
};
|
|
792
|
+
const createNewThread = async (input) => {
|
|
793
|
+
if (deps.createThread) {
|
|
794
|
+
let sel = input.modelSelection;
|
|
795
|
+
if (!sel?.model) {
|
|
796
|
+
const caps = await getCapabilities();
|
|
797
|
+
const firstProv = caps.providers.find((p) => p.models.length > 0 && p.canRunChildTask !== false) ?? caps.providers[0];
|
|
798
|
+
const firstModel = firstProv?.models[0]?.id;
|
|
799
|
+
if (!firstProv || !firstModel) throw new Error("T3 Code \u5F53\u524D\u6CA1\u6709\u53EF\u7528\u7684 Provider \u6A21\u578B\u3002");
|
|
800
|
+
sel = { instanceId: firstProv.providerInstanceId, model: firstModel };
|
|
801
|
+
}
|
|
802
|
+
return deps.createThread({ projectId: input.projectId, title: input.title, modelSelection: sel });
|
|
803
|
+
}
|
|
804
|
+
return deps.observer.call("t3_thread_launch", {
|
|
805
|
+
projectId: input.projectId,
|
|
806
|
+
title: input.title,
|
|
807
|
+
...input.modelSelection?.model ? { modelSelection: input.modelSelection } : {}
|
|
808
|
+
});
|
|
809
|
+
};
|
|
810
|
+
const stopThread = async (threadId, options = {}) => {
|
|
811
|
+
if (deps.stopThread) {
|
|
812
|
+
await deps.stopThread(threadId, options);
|
|
813
|
+
return;
|
|
814
|
+
}
|
|
815
|
+
await deps.observer.call("t3_thread_interrupt", {
|
|
816
|
+
threadId,
|
|
817
|
+
...options.reason ? { reason: options.reason } : {}
|
|
818
|
+
});
|
|
819
|
+
};
|
|
782
820
|
return async function handleSlashCommand(cmd, binding, scope) {
|
|
783
821
|
if (cmd.name === "help") {
|
|
784
822
|
await deps.sendReply(formatSlashHelpText());
|
|
@@ -855,10 +893,7 @@ function createSlashCommandHandler(deps) {
|
|
|
855
893
|
await deps.sendReply(`Provider "${picked.providerInstanceId}" \u5F53\u524D\u6CA1\u6709\u53EF\u7528\u6A21\u578B\u3002`);
|
|
856
894
|
return "handled";
|
|
857
895
|
}
|
|
858
|
-
await
|
|
859
|
-
threadId,
|
|
860
|
-
modelSelection: { instanceId: picked.providerInstanceId, model: nextModel }
|
|
861
|
-
});
|
|
896
|
+
await setThreadModel(threadId, { instanceId: picked.providerInstanceId, model: nextModel });
|
|
862
897
|
await deps.sendReply(
|
|
863
898
|
`\u5DF2\u5207\u6362 Provider \u4E3A\uFF1A${picked.displayName ?? picked.providerInstanceId} (${picked.providerInstanceId})
|
|
864
899
|
\u9ED8\u8BA4\u6A21\u578B\uFF1A${nextModel}`
|
|
@@ -883,12 +918,37 @@ function createSlashCommandHandler(deps) {
|
|
|
883
918
|
return `${mark}[${idx + 1}] ${m.label ?? m.id} (${m.id})`;
|
|
884
919
|
}),
|
|
885
920
|
"",
|
|
886
|
-
"\u5FEB\u6377\u5207\u6362\uFF1A/model 1 \xB7 /model 4n \xB7 /model 4h xhigh \xB7 /model 3.8f \xB7 /model sol \xB7 /model provider/model"
|
|
921
|
+
"\u5FEB\u6377\u5207\u6362\uFF1A/model 1 \xB7 /model 4n \xB7 /model 4h xhigh \xB7 /model 3.8f \xB7 /model sol \xB7 /model provider/model \xB7 /model all"
|
|
887
922
|
];
|
|
888
923
|
await deps.sendReply(lines.join("\n"));
|
|
889
924
|
return "handled";
|
|
890
925
|
}
|
|
891
|
-
|
|
926
|
+
if (cmd.args.trim().toLowerCase() === "all" || cmd.args.trim() === "\u5168\u90E8") {
|
|
927
|
+
const lines = [
|
|
928
|
+
"\u5168\u90E8\u53EF\u7528\u4F9B\u5E94\u5546\u4E0E\u6A21\u578B\u4E00\u89C8",
|
|
929
|
+
`\u5F53\u524D\uFF1A${activeProvider?.displayName ?? currentInstance ?? "(\u9ED8\u8BA4)"} / ${currentModel ?? "(\u9ED8\u8BA4)"}${currentEffort ? ` \xB7 ${currentEffort}` : ""}`,
|
|
930
|
+
"",
|
|
931
|
+
...enabled.flatMap((p) => [
|
|
932
|
+
`\u25B8 ${p.displayName ?? p.providerInstanceId} (${p.providerInstanceId})`,
|
|
933
|
+
...p.models.map((m) => {
|
|
934
|
+
const isCur = p.providerInstanceId === currentInstance && m.id === currentModel;
|
|
935
|
+
return `${isCur ? " \u{1F449} " : " "}${m.id}`;
|
|
936
|
+
})
|
|
937
|
+
]),
|
|
938
|
+
"",
|
|
939
|
+
"\u8DE8\u4F9B\u5E94\u5546\u5207\u6362\u793A\u4F8B\uFF1A/model codex/sol \xB7 /model antigravity_cloudstay/4n"
|
|
940
|
+
];
|
|
941
|
+
await deps.sendReply(lines.join("\n"));
|
|
942
|
+
return "handled";
|
|
943
|
+
}
|
|
944
|
+
let resolved = resolveModelFromInput(cmd.args, caps.providers, currentInstance);
|
|
945
|
+
if (!resolved.match && !resolved.candidates?.length) {
|
|
946
|
+
const searchTerm = cmd.args.trim().split(/\s+/)[0].split("/").pop() ?? cmd.args.trim();
|
|
947
|
+
const searched = await getCapabilities(searchTerm).catch(() => null);
|
|
948
|
+
if (searched?.providers?.length) {
|
|
949
|
+
resolved = resolveModelFromInput(cmd.args, searched.providers, currentInstance);
|
|
950
|
+
}
|
|
951
|
+
}
|
|
892
952
|
if (resolved.candidates?.length) {
|
|
893
953
|
const lines = [
|
|
894
954
|
`\u627E\u5230\u591A\u4E2A\u5339\u914D\u6A21\u578B\uFF0C\u8BF7\u8F93\u5165\u66F4\u7CBE\u786E\u7684\u7F16\u53F7\u6216\u540D\u79F0\uFF1A`,
|
|
@@ -898,7 +958,7 @@ function createSlashCommandHandler(deps) {
|
|
|
898
958
|
return "handled";
|
|
899
959
|
}
|
|
900
960
|
if (!resolved.match) {
|
|
901
|
-
await deps.sendReply(`\u672A\u5339\u914D\u5230\u6A21\u578B "${cmd.args}"\u3002\u53D1\u9001 /model \u67E5\u770B\u5F53\u524D\u53EF\u7528\u6A21\u578B\u5217\u8868\u3002`);
|
|
961
|
+
await deps.sendReply(`\u672A\u5339\u914D\u5230\u6A21\u578B "${cmd.args}"\u3002\u53D1\u9001 /model \u67E5\u770B\u5F53\u524D\u53EF\u7528\u6A21\u578B\u5217\u8868\uFF0C\u6216\u53D1 /model all \u67E5\u770B\u5168\u90E8\u3002`);
|
|
902
962
|
return "handled";
|
|
903
963
|
}
|
|
904
964
|
const { provider, model, inferredEffort } = resolved.match;
|
|
@@ -913,13 +973,10 @@ function createSlashCommandHandler(deps) {
|
|
|
913
973
|
appliedEffort = norm;
|
|
914
974
|
}
|
|
915
975
|
}
|
|
916
|
-
await
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
model: model.id,
|
|
921
|
-
...nextOptions ? { options: nextOptions } : {}
|
|
922
|
-
}
|
|
976
|
+
await setThreadModel(threadId, {
|
|
977
|
+
instanceId: provider.providerInstanceId,
|
|
978
|
+
model: model.id,
|
|
979
|
+
...nextOptions ? { options: nextOptions } : {}
|
|
923
980
|
});
|
|
924
981
|
await deps.sendReply(
|
|
925
982
|
`\u5DF2\u5207\u6362\u6A21\u578B\uFF1A${model.label ?? model.id} (${model.id})
|
|
@@ -956,13 +1013,10 @@ Provider\uFF1A${provider.displayName ?? provider.providerInstanceId}${appliedEff
|
|
|
956
1013
|
}
|
|
957
1014
|
const optionId = effortOpt?.id ?? "thought_level";
|
|
958
1015
|
const applied = effortOpt ? normalizeEffortValueForOption(parsed, effortOpt) ?? parsed : parsed;
|
|
959
|
-
await
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
model: currentModelId,
|
|
964
|
-
options: [{ id: optionId, value: applied }]
|
|
965
|
-
}
|
|
1016
|
+
await setThreadModel(threadId, {
|
|
1017
|
+
...currentInstance ? { instanceId: currentInstance } : {},
|
|
1018
|
+
model: currentModelId,
|
|
1019
|
+
options: [{ id: optionId, value: applied }]
|
|
966
1020
|
});
|
|
967
1021
|
await deps.sendReply(`\u5DF2\u5207\u6362\u601D\u8003\u5F3A\u5EA6\uFF1A${applied}\uFF08\u6A21\u578B\uFF1A${currentModelId}\uFF09`);
|
|
968
1022
|
return "handled";
|
|
@@ -973,10 +1027,16 @@ Provider\uFF1A${provider.displayName ?? provider.providerInstanceId}${appliedEff
|
|
|
973
1027
|
getThreadConfig(threadId).catch(() => null)
|
|
974
1028
|
]);
|
|
975
1029
|
const title = cmd.args || `QQ ${scope.startsWith("group:") ? "\u7FA4\u804A" : "\u79C1\u804A"} ${(/* @__PURE__ */ new Date()).toISOString().slice(5, 16).replace("T", " ")}`;
|
|
976
|
-
const
|
|
1030
|
+
const inheritedSel = cfgRes?.modelSelection?.model ? {
|
|
1031
|
+
...cfgRes.modelSelection.instanceId ? { instanceId: cfgRes.modelSelection.instanceId } : {},
|
|
1032
|
+
...cfgRes.modelSelection.provider && !cfgRes.modelSelection.instanceId ? { instanceId: cfgRes.modelSelection.provider } : {},
|
|
1033
|
+
model: cfgRes.modelSelection.model,
|
|
1034
|
+
...cfgRes.modelSelection.options ? { options: cfgRes.modelSelection.options } : {}
|
|
1035
|
+
} : void 0;
|
|
1036
|
+
const created = await createNewThread({
|
|
977
1037
|
projectId: project.id,
|
|
978
1038
|
title,
|
|
979
|
-
|
|
1039
|
+
modelSelection: inheritedSel
|
|
980
1040
|
});
|
|
981
1041
|
if (!created?.threadId) throw new Error("T3 Code \u672A\u8FD4\u56DE\u65B0\u4F1A\u8BDD threadId");
|
|
982
1042
|
await deps.setActiveThreadOverride(binding, created.threadId);
|
|
@@ -1090,25 +1150,19 @@ Provider\uFF1A${provider.displayName ?? provider.providerInstanceId}${appliedEff
|
|
|
1090
1150
|
if (cmd.name === "cancel") {
|
|
1091
1151
|
const arg = cmd.args.trim();
|
|
1092
1152
|
if (!arg) {
|
|
1093
|
-
await
|
|
1094
|
-
|
|
1095
|
-
|
|
1153
|
+
await stopThread(threadId, {
|
|
1154
|
+
reason: "Stopped by user via QQ /stop command",
|
|
1155
|
+
stopDelegatedTasks: false
|
|
1096
1156
|
});
|
|
1097
1157
|
await deps.sendReply("\u5DF2\u53D1\u9001\u4E2D\u65AD\u6307\u4EE4\uFF0C\u505C\u6B62\u5F53\u524D T3 \u4F1A\u8BDD\u6B63\u5728\u6267\u884C\u7684\u8F6E\u6B21\u3002");
|
|
1098
1158
|
return "handled";
|
|
1099
1159
|
}
|
|
1100
1160
|
if (arg.toLowerCase() === "all") {
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1161
|
+
await stopThread(threadId, {
|
|
1162
|
+
reason: "Stopped via QQ /cancel all",
|
|
1163
|
+
stopDelegatedTasks: true
|
|
1104
1164
|
});
|
|
1105
|
-
|
|
1106
|
-
for (const t of pending) {
|
|
1107
|
-
await deps.observer.call("task_cancel", { taskId: t.taskId, reason: "Cancelled via QQ /cancel all" }).catch(() => {
|
|
1108
|
-
});
|
|
1109
|
-
cancelled++;
|
|
1110
|
-
}
|
|
1111
|
-
await deps.sendReply(`\u5DF2\u4E2D\u65AD\u4E3B\u4F1A\u8BDD\u5E76\u53D6\u6D88 ${cancelled} \u4E2A\u540E\u53F0\u59D4\u6D3E\u4EFB\u52A1\u3002`);
|
|
1165
|
+
await deps.sendReply("\u5DF2\u4E2D\u65AD\u4E3B\u4F1A\u8BDD\u5E76\u505C\u6B62\u5168\u90E8\u5173\u8054\u540E\u53F0\u59D4\u6D3E\u4EFB\u52A1\u3002");
|
|
1112
1166
|
return "handled";
|
|
1113
1167
|
}
|
|
1114
1168
|
await deps.observer.call("task_cancel", {
|
|
@@ -1121,52 +1175,148 @@ Provider\uFF1A${provider.displayName ?? provider.providerInstanceId}${appliedEff
|
|
|
1121
1175
|
return "passthrough";
|
|
1122
1176
|
};
|
|
1123
1177
|
}
|
|
1124
|
-
|
|
1125
|
-
const
|
|
1126
|
-
const
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
const
|
|
1130
|
-
|
|
1131
|
-
|
|
1178
|
+
function compareSemver(a, b) {
|
|
1179
|
+
const parse = (v) => v.trim().replace(/^v/i, "").split("-")[0].split(".").map((part) => Number.parseInt(part, 10) || 0);
|
|
1180
|
+
const pa = parse(a);
|
|
1181
|
+
const pb = parse(b);
|
|
1182
|
+
for (let i = 0; i < Math.max(pa.length, pb.length, 3); i++) {
|
|
1183
|
+
const da = pa[i] ?? 0;
|
|
1184
|
+
const db = pb[i] ?? 0;
|
|
1185
|
+
if (da !== db) return da > db ? 1 : -1;
|
|
1186
|
+
}
|
|
1187
|
+
return 0;
|
|
1188
|
+
}
|
|
1189
|
+
function detectInstallMode(pkgRoot) {
|
|
1190
|
+
if (existsSync(join3(pkgRoot, ".git"))) return "git";
|
|
1191
|
+
const normalized = pkgRoot.replace(/\\/g, "/").toLowerCase();
|
|
1192
|
+
if (normalized.includes("/node_modules/t3-code-qqbot")) return "npm-global";
|
|
1193
|
+
return "standalone";
|
|
1194
|
+
}
|
|
1195
|
+
function buildNodeToolEnv() {
|
|
1196
|
+
const env = { ...process.env };
|
|
1197
|
+
const nodeDir = dirname2(process.execPath);
|
|
1198
|
+
const pathKey = Object.keys(env).find((k) => k.toLowerCase() === "path") ?? "PATH";
|
|
1199
|
+
const existing = env[pathKey] ?? "";
|
|
1200
|
+
const sep2 = process.platform === "win32" ? ";" : ":";
|
|
1201
|
+
env[pathKey] = existing ? `${nodeDir}${sep2}${existing}` : nodeDir;
|
|
1202
|
+
return env;
|
|
1203
|
+
}
|
|
1204
|
+
async function runNpm(args, cwd, timeout = 12e4) {
|
|
1205
|
+
const env = buildNodeToolEnv();
|
|
1206
|
+
if (process.platform === "win32") {
|
|
1207
|
+
const npmCliJs = join3(dirname2(process.execPath), "node_modules", "npm", "bin", "npm-cli.js");
|
|
1208
|
+
if (existsSync(npmCliJs)) {
|
|
1209
|
+
return execFileAsync(process.execPath, [npmCliJs, ...args], { cwd, env, windowsHide: true, timeout });
|
|
1210
|
+
}
|
|
1211
|
+
return execFileAsync("npm.cmd", args, { cwd, env, shell: true, windowsHide: true, timeout });
|
|
1212
|
+
}
|
|
1213
|
+
return execFileAsync("npm", args, { cwd, env, windowsHide: true, timeout });
|
|
1214
|
+
}
|
|
1215
|
+
async function handleSelfUpdate(deps, options = {}) {
|
|
1216
|
+
const pkgRoot = options.pkgRoot ?? resolve3(import.meta.dirname, "..");
|
|
1217
|
+
const currentVersion = options.currentVersion ?? APP_VERSION;
|
|
1218
|
+
const mode = detectInstallMode(pkgRoot);
|
|
1219
|
+
if (mode === "git") {
|
|
1220
|
+
await deps.sendReply("\u6B63\u5728\u4ECE GitHub \u62C9\u53D6\u6700\u65B0\u6E90\u7801\u5E76\u68C0\u67E5\u66F4\u65B0\u2026");
|
|
1221
|
+
const env = buildNodeToolEnv();
|
|
1222
|
+
const before = (await execFileAsync("git", ["rev-parse", "--short", "HEAD"], { cwd: pkgRoot, env, windowsHide: true })).stdout.trim();
|
|
1223
|
+
await execFileAsync("git", ["pull", "--ff-only"], { cwd: pkgRoot, env, windowsHide: true, timeout: 6e4 });
|
|
1224
|
+
const after = (await execFileAsync("git", ["rev-parse", "--short", "HEAD"], { cwd: pkgRoot, env, windowsHide: true })).stdout.trim();
|
|
1132
1225
|
if (before === after) {
|
|
1133
|
-
await deps.sendReply(`\u5F53\u524D\u5DF2\u662F\u6700\u65B0\u7248\u672C\uFF08v${
|
|
1226
|
+
await deps.sendReply(`\u5F53\u524D\u5DF2\u662F\u6700\u65B0\u6E90\u7801\u7248\u672C\uFF08v${currentVersion} \xB7 ${after}\uFF09\uFF0C\u65E0\u9700\u66F4\u65B0\u3002`);
|
|
1134
1227
|
return;
|
|
1135
1228
|
}
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
await execFileAsync("npm", ["install", "--no-audit", "--no-fund"], {
|
|
1140
|
-
cwd: pkgRoot,
|
|
1141
|
-
windowsHide: true,
|
|
1142
|
-
timeout: 12e4
|
|
1143
|
-
});
|
|
1144
|
-
}
|
|
1145
|
-
await execFileAsync(npmBin, npmBuildArgs, { cwd: pkgRoot, windowsHide: true, timeout: 12e4 });
|
|
1146
|
-
await deps.sendReply(`\u66F4\u65B0\u5B8C\u6210\uFF08${before} -> ${after}\uFF09\uFF0C\u6B63\u5728\u70ED\u91CD\u542F\u6865\u63A5\u670D\u52A1\u4EE5\u52A0\u8F7D\u65B0\u7248\u2026`);
|
|
1229
|
+
await runNpm(["install", "--no-audit", "--no-fund"], pkgRoot, 12e4);
|
|
1230
|
+
await runNpm(["run", "build"], pkgRoot, 12e4);
|
|
1231
|
+
await deps.sendReply(`\u6E90\u7801\u66F4\u65B0\u5B8C\u6210\uFF08${before} -> ${after}\uFF09\uFF0C\u6B63\u5728\u70ED\u91CD\u542F\u670D\u52A1\u4EE5\u52A0\u8F7D\u65B0\u7248\u2026`);
|
|
1147
1232
|
if (deps.triggerGracefulRestart) {
|
|
1148
1233
|
setTimeout(() => deps.triggerGracefulRestart(), 800);
|
|
1149
1234
|
}
|
|
1150
1235
|
return;
|
|
1151
1236
|
}
|
|
1152
|
-
const
|
|
1153
|
-
|
|
1154
|
-
|
|
1237
|
+
const registryUrl = options.registryUrl ?? "https://registry.npmjs.org/t3-code-qqbot/latest";
|
|
1238
|
+
const res = await fetch(registryUrl, {
|
|
1239
|
+
headers: { Accept: "application/json", "User-Agent": `t3-code-qqbot/${currentVersion}` },
|
|
1240
|
+
signal: AbortSignal.timeout(15e3)
|
|
1155
1241
|
});
|
|
1156
1242
|
if (!res.ok) {
|
|
1157
|
-
await deps.sendReply(`\u5F53\u524D\u7248\u672C\uFF1Av${
|
|
1243
|
+
await deps.sendReply(`\u5F53\u524D\u7248\u672C\uFF1Av${currentVersion}\u3002\u68C0\u67E5 npm \u6700\u65B0\u7248\u672C\u5931\u8D25 (HTTP ${res.status})\u3002`);
|
|
1158
1244
|
return;
|
|
1159
1245
|
}
|
|
1160
|
-
const
|
|
1161
|
-
const
|
|
1162
|
-
if (
|
|
1163
|
-
await deps.sendReply(`\u5F53\u524D\
|
|
1164
|
-
|
|
1246
|
+
const meta = await res.json();
|
|
1247
|
+
const latestVersion = meta.version?.trim();
|
|
1248
|
+
if (!latestVersion) {
|
|
1249
|
+
await deps.sendReply(`\u5F53\u524D\u7248\u672C\uFF1Av${currentVersion}\u3002npm \u8FD4\u56DE\u7684\u7248\u672C\u4FE1\u606F\u65E0\u6548\u3002`);
|
|
1250
|
+
return;
|
|
1251
|
+
}
|
|
1252
|
+
if (compareSemver(latestVersion, currentVersion) <= 0) {
|
|
1253
|
+
await deps.sendReply(`\u5F53\u524D\u5DF2\u662F npm \u6700\u65B0\u7248\u672C\uFF08v${currentVersion}\uFF09\uFF0C\u65E0\u9700\u66F4\u65B0\u3002`);
|
|
1254
|
+
return;
|
|
1255
|
+
}
|
|
1256
|
+
if (mode === "npm-global") {
|
|
1257
|
+
await deps.sendReply(`\u53D1\u73B0 npm \u65B0\u7248\u672C v${latestVersion}\uFF08\u5F53\u524D v${currentVersion}\uFF09\uFF0C\u6B63\u5728\u901A\u8FC7 npm \u81EA\u52A8\u66F4\u65B0\u2026`);
|
|
1258
|
+
const installArgs = ["install", "-g", `t3-code-qqbot@${latestVersion}`, "--no-audit", "--no-fund"];
|
|
1259
|
+
if (options.npmGlobalPrefix) {
|
|
1260
|
+
installArgs.push("--prefix", options.npmGlobalPrefix);
|
|
1261
|
+
}
|
|
1262
|
+
await runNpm(installArgs, tmpdir(), 18e4);
|
|
1263
|
+
await deps.sendReply(`\u5DF2\u901A\u8FC7 npm \u81EA\u52A8\u66F4\u65B0\u81F3 v${latestVersion}\uFF08\u539F v${currentVersion}\uFF09\uFF0C\u6B63\u5728\u70ED\u91CD\u542F\u670D\u52A1\u4EE5\u52A0\u8F7D\u65B0\u7248\u2026`);
|
|
1264
|
+
if (deps.triggerGracefulRestart) {
|
|
1265
|
+
setTimeout(() => deps.triggerGracefulRestart(), 800);
|
|
1266
|
+
}
|
|
1267
|
+
return;
|
|
1268
|
+
}
|
|
1269
|
+
const tarballUrl = meta.dist?.tarball;
|
|
1270
|
+
if (!tarballUrl || !tarballUrl.startsWith("https://")) {
|
|
1165
1271
|
await deps.sendReply(
|
|
1166
|
-
`\u53D1\u73B0\u65B0\u7248\u672C ${
|
|
1167
|
-
\u2022 npm \u7528\u6237\u5347\u7EA7\uFF1Anpm i -g t3-code-qqbot@latest && t3-qqbot service restart
|
|
1168
|
-
\u2022 \u53D1\u5E03\u9875\uFF1A${latest.html_url ?? "https://github.com/Shiratsuk1/t3-code-qqbot/releases"}`
|
|
1272
|
+
`\u53D1\u73B0 npm \u65B0\u7248\u672C v${latestVersion}\uFF08\u5F53\u524D v${currentVersion}\uFF09\uFF0C\u4F46\u672A\u83B7\u53D6\u5230\u6709\u6548\u7684\u4E0B\u8F7D\u5305\u5730\u5740\uFF0C\u8BF7\u624B\u52A8\u6267\u884C npm i -g t3-code-qqbot@latest\u3002`
|
|
1169
1273
|
);
|
|
1274
|
+
return;
|
|
1275
|
+
}
|
|
1276
|
+
await deps.sendReply(`\u53D1\u73B0 npm \u65B0\u7248\u672C v${latestVersion}\uFF08\u5F53\u524D v${currentVersion}\uFF09\uFF0C\u6B63\u5728\u4ECE npm \u62C9\u53D6\u66F4\u65B0\u5305\u2026`);
|
|
1277
|
+
const stageDir = await mkdtemp(join3(tmpdir(), "t3-qqbot-update-"));
|
|
1278
|
+
try {
|
|
1279
|
+
const tgzPath = join3(stageDir, `t3-code-qqbot-${latestVersion}.tgz`);
|
|
1280
|
+
const extractDir = join3(stageDir, "extracted");
|
|
1281
|
+
await mkdir2(extractDir, { recursive: true });
|
|
1282
|
+
const dl = await fetch(tarballUrl, { signal: AbortSignal.timeout(6e4) });
|
|
1283
|
+
if (!dl.ok) throw new Error(`\u4E0B\u8F7D npm \u66F4\u65B0\u5305\u5931\u8D25 (HTTP ${dl.status})`);
|
|
1284
|
+
const bytes = Buffer.from(await dl.arrayBuffer());
|
|
1285
|
+
await writeFile2(tgzPath, bytes);
|
|
1286
|
+
await execFileAsync("tar", ["-xzf", tgzPath, "-C", extractDir], { windowsHide: true, timeout: 6e4 });
|
|
1287
|
+
const unpackedPkgDir = join3(extractDir, "package");
|
|
1288
|
+
if (!existsSync(join3(unpackedPkgDir, "dist", "bridge.mjs")) || !existsSync(join3(unpackedPkgDir, "package.json"))) {
|
|
1289
|
+
throw new Error("\u4E0B\u8F7D\u7684 npm \u5305\u7ED3\u6784\u4E0D\u5B8C\u6574\uFF08\u7F3A\u5C11 dist/bridge.mjs \u6216 package.json\uFF09");
|
|
1290
|
+
}
|
|
1291
|
+
const oldPkgRaw = await readFile3(join3(pkgRoot, "package.json"), "utf8").catch(() => "{}");
|
|
1292
|
+
const newPkgRaw = await readFile3(join3(unpackedPkgDir, "package.json"), "utf8");
|
|
1293
|
+
const oldDeps = JSON.stringify(JSON.parse(oldPkgRaw).dependencies ?? {});
|
|
1294
|
+
const newDeps = JSON.stringify(JSON.parse(newPkgRaw).dependencies ?? {});
|
|
1295
|
+
for (const entry of [
|
|
1296
|
+
"dist",
|
|
1297
|
+
"docs",
|
|
1298
|
+
"scripts",
|
|
1299
|
+
"package.json",
|
|
1300
|
+
"README.md",
|
|
1301
|
+
"LICENSE",
|
|
1302
|
+
"config.example.json",
|
|
1303
|
+
"credentials.example.json"
|
|
1304
|
+
]) {
|
|
1305
|
+
const src = join3(unpackedPkgDir, entry);
|
|
1306
|
+
if (!existsSync(src)) continue;
|
|
1307
|
+
const dst = join3(pkgRoot, entry);
|
|
1308
|
+
await cp(src, dst, { recursive: true, force: true });
|
|
1309
|
+
}
|
|
1310
|
+
if (oldDeps !== newDeps) {
|
|
1311
|
+
await runNpm(["install", "--omit=dev", "--no-audit", "--no-fund"], pkgRoot, 18e4);
|
|
1312
|
+
}
|
|
1313
|
+
await deps.sendReply(`\u5DF2\u901A\u8FC7 npm \u81EA\u52A8\u66F4\u65B0\u81F3 v${latestVersion}\uFF08\u539F v${currentVersion}\uFF09\uFF0C\u6B63\u5728\u70ED\u91CD\u542F\u670D\u52A1\u4EE5\u52A0\u8F7D\u65B0\u7248\u2026`);
|
|
1314
|
+
if (deps.triggerGracefulRestart) {
|
|
1315
|
+
setTimeout(() => deps.triggerGracefulRestart(), 800);
|
|
1316
|
+
}
|
|
1317
|
+
} finally {
|
|
1318
|
+
await rm(stageDir, { recursive: true, force: true }).catch(() => {
|
|
1319
|
+
});
|
|
1170
1320
|
}
|
|
1171
1321
|
}
|
|
1172
1322
|
async function readActiveThreadOverride(stateDir, scopeHash) {
|
|
@@ -1194,7 +1344,7 @@ async function writeActiveThreadOverride(stateDir, scopeHash, threadId) {
|
|
|
1194
1344
|
import { execFile as execFile2 } from "node:child_process";
|
|
1195
1345
|
import { access as access2, readFile as readFile4 } from "node:fs/promises";
|
|
1196
1346
|
import { homedir } from "node:os";
|
|
1197
|
-
import { dirname as
|
|
1347
|
+
import { dirname as dirname3, join as join4 } from "node:path";
|
|
1198
1348
|
var STRIPPED_ENV_KEYS = /* @__PURE__ */ new Set([
|
|
1199
1349
|
"QQ_BRIDGE_SECRET",
|
|
1200
1350
|
"QQ_APP_ID",
|
|
@@ -1228,7 +1378,7 @@ function sanitizeChildEnv(baseEnv) {
|
|
|
1228
1378
|
}
|
|
1229
1379
|
function parseWindowsT3Cmd(scriptText, options = {}) {
|
|
1230
1380
|
const callerEnv = options.env ?? {};
|
|
1231
|
-
const scriptDir = options.scriptPath ?
|
|
1381
|
+
const scriptDir = options.scriptPath ? dirname3(options.scriptPath) : "";
|
|
1232
1382
|
const env = {};
|
|
1233
1383
|
const lines = scriptText.split(/\r?\n/);
|
|
1234
1384
|
for (const line of lines) {
|
|
@@ -1567,25 +1717,25 @@ function planCheckpointRollback(projection, turns, restoreFiles = false) {
|
|
|
1567
1717
|
const isEligibleStatus = (status) => restoreFiles ? status === "ready" : status === "ready" || status === "missing";
|
|
1568
1718
|
const activeScope = scopes.findLast(
|
|
1569
1719
|
(s) => activeProviderThreadId !== null && s.providerThreadId === activeProviderThreadId && s.kind === "root_run"
|
|
1570
|
-
) ?? scopes.findLast((s) => activeProviderThreadId !== null && s.providerThreadId === activeProviderThreadId) ?? scopes.findLast((s) => allCheckpoints.some((
|
|
1720
|
+
) ?? scopes.findLast((s) => activeProviderThreadId !== null && s.providerThreadId === activeProviderThreadId) ?? scopes.findLast((s) => allCheckpoints.some((cp2) => cp2.scopeId === s.id && isEligibleStatus(cp2.status))) ?? scopes.at(-1);
|
|
1571
1721
|
if (!activeScope) {
|
|
1572
1722
|
return { error: "\u5F53\u524D\u4F1A\u8BDD\u5C1A\u672A\u751F\u6210 T3 \u56DE\u6EAF\u68C0\u67E5\u70B9\uFF08checkpoint\uFF09\uFF0C\u6682\u4E0D\u652F\u6301\u539F\u5730\u64A4\u56DE\uFF1B\u8BF7\u53D1\u9001 /new \u5F00\u542F\u65B0\u4F1A\u8BDD\u3002" };
|
|
1573
1723
|
}
|
|
1574
|
-
const scopeCheckpoints = allCheckpoints.filter((
|
|
1575
|
-
const eligible = scopeCheckpoints.filter((
|
|
1724
|
+
const scopeCheckpoints = allCheckpoints.filter((cp2) => cp2.scopeId === activeScope.id);
|
|
1725
|
+
const eligible = scopeCheckpoints.filter((cp2) => isEligibleStatus(cp2.status));
|
|
1576
1726
|
if (eligible.length === 0) {
|
|
1577
|
-
if (restoreFiles && scopeCheckpoints.some((
|
|
1727
|
+
if (restoreFiles && scopeCheckpoints.some((cp2) => cp2.status === "missing")) {
|
|
1578
1728
|
return {
|
|
1579
1729
|
error: `\u5F53\u524D\u5DE5\u4F5C\u533A\u672A\u751F\u6210\u53EF\u6062\u590D\u7684\u6587\u4EF6\u5FEB\u7167\uFF08\u975E\u72EC\u7ACB worktree \u6216\u975E git \u4ED3\u5E93\uFF09\uFF1B\u8BF7\u6539\u53D1 /rewind ${turns} \u4EC5\u64A4\u56DE\u5BF9\u8BDD\u4E0A\u4E0B\u6587\u3002`
|
|
1580
1730
|
};
|
|
1581
1731
|
}
|
|
1582
1732
|
return { error: "\u5F53\u524D\u4F1A\u8BDD\u6CA1\u6709\u53EF\u7528\u7684\u56DE\u6EAF\u68C0\u67E5\u70B9\uFF0C\u8BF7\u53D1\u9001 /new \u5F00\u542F\u65B0\u4F1A\u8BDD\u3002" };
|
|
1583
1733
|
}
|
|
1584
|
-
const baseline = eligible.findLast((
|
|
1734
|
+
const baseline = eligible.findLast((cp2) => cp2.ordinalWithinScope === 0 && cp2.appRunOrdinal === null);
|
|
1585
1735
|
const runCheckpointsByOrdinal = /* @__PURE__ */ new Map();
|
|
1586
|
-
for (const
|
|
1587
|
-
if (typeof
|
|
1588
|
-
runCheckpointsByOrdinal.set(
|
|
1736
|
+
for (const cp2 of eligible) {
|
|
1737
|
+
if (typeof cp2.appRunOrdinal === "number" && cp2.appRunOrdinal > 0) {
|
|
1738
|
+
runCheckpointsByOrdinal.set(cp2.appRunOrdinal, cp2);
|
|
1589
1739
|
}
|
|
1590
1740
|
}
|
|
1591
1741
|
const runCheckpoints = [...runCheckpointsByOrdinal.values()].sort(
|
|
@@ -1608,7 +1758,7 @@ function planCheckpointRollback(projection, turns, restoreFiles = false) {
|
|
|
1608
1758
|
targetCheckpoint = baseline;
|
|
1609
1759
|
targetOrdinal = 0;
|
|
1610
1760
|
if (!targetCheckpoint) {
|
|
1611
|
-
if (restoreFiles && scopeCheckpoints.some((
|
|
1761
|
+
if (restoreFiles && scopeCheckpoints.some((cp2) => cp2.ordinalWithinScope === 0 && cp2.status === "missing")) {
|
|
1612
1762
|
return {
|
|
1613
1763
|
error: `\u5F53\u524D\u5DE5\u4F5C\u533A\u7F3A\u5C11\u521D\u59CB\u6587\u4EF6\u5FEB\u7167\uFF0C\u65E0\u6CD5\u5E26 --files \u64A4\u56DE\u5168\u90E8 ${turns} \u8F6E\uFF1B\u8BF7\u6539\u53D1 /rewind ${turns} \u4EC5\u64A4\u56DE\u5BF9\u8BDD\u3002`
|
|
1614
1764
|
};
|
|
@@ -1619,7 +1769,7 @@ function planCheckpointRollback(projection, turns, restoreFiles = false) {
|
|
|
1619
1769
|
}
|
|
1620
1770
|
} else {
|
|
1621
1771
|
const keepLastRun = scopeRuns[availableTurns - 1 - turns];
|
|
1622
|
-
targetCheckpoint = runCheckpointsByOrdinal.get(keepLastRun.ordinal) ?? runCheckpoints.findLast((
|
|
1772
|
+
targetCheckpoint = runCheckpointsByOrdinal.get(keepLastRun.ordinal) ?? runCheckpoints.findLast((cp2) => (cp2.appRunOrdinal ?? 0) <= keepLastRun.ordinal);
|
|
1623
1773
|
targetOrdinal = targetCheckpoint?.appRunOrdinal ?? keepLastRun.ordinal;
|
|
1624
1774
|
if (!targetCheckpoint) {
|
|
1625
1775
|
return {
|
|
@@ -1725,6 +1875,96 @@ async function rollbackThreadTurns(origin, token, threadId, options) {
|
|
|
1725
1875
|
socket.close();
|
|
1726
1876
|
}
|
|
1727
1877
|
}
|
|
1878
|
+
async function withT3Socket(origin, token, fn) {
|
|
1879
|
+
const socket = openT3Socket(origin, token);
|
|
1880
|
+
const ignoreLateError = () => {
|
|
1881
|
+
};
|
|
1882
|
+
socket.on("error", ignoreLateError);
|
|
1883
|
+
try {
|
|
1884
|
+
await new Promise((resolve5, reject) => {
|
|
1885
|
+
const onOpen = () => {
|
|
1886
|
+
socket.off("error", onError);
|
|
1887
|
+
resolve5();
|
|
1888
|
+
};
|
|
1889
|
+
const onError = (err) => {
|
|
1890
|
+
socket.off("open", onOpen);
|
|
1891
|
+
reject(err);
|
|
1892
|
+
};
|
|
1893
|
+
socket.once("open", onOpen);
|
|
1894
|
+
socket.once("error", onError);
|
|
1895
|
+
});
|
|
1896
|
+
let seq = 0;
|
|
1897
|
+
const rpc = (tag, payload) => sendRpc(socket, tag, payload, String(++seq));
|
|
1898
|
+
return await fn(rpc);
|
|
1899
|
+
} finally {
|
|
1900
|
+
socket.close();
|
|
1901
|
+
}
|
|
1902
|
+
}
|
|
1903
|
+
async function getThreadState(origin, token, threadId) {
|
|
1904
|
+
return withT3Socket(origin, token, async (rpc) => {
|
|
1905
|
+
const proj = await rpc("orchestration.getThreadProjection", { threadId });
|
|
1906
|
+
const t = proj?.thread ?? {};
|
|
1907
|
+
return {
|
|
1908
|
+
threadId: String(t.id ?? threadId),
|
|
1909
|
+
title: typeof t.title === "string" ? t.title : void 0,
|
|
1910
|
+
projectId: typeof t.projectId === "string" ? t.projectId : void 0,
|
|
1911
|
+
providerInstanceId: typeof t.providerInstanceId === "string" ? t.providerInstanceId : void 0,
|
|
1912
|
+
modelSelection: t.modelSelection && typeof t.modelSelection === "object" ? t.modelSelection : void 0,
|
|
1913
|
+
runtimeMode: typeof t.runtimeMode === "string" ? t.runtimeMode : void 0,
|
|
1914
|
+
interactionMode: typeof t.interactionMode === "string" ? t.interactionMode : void 0
|
|
1915
|
+
};
|
|
1916
|
+
});
|
|
1917
|
+
}
|
|
1918
|
+
async function setThreadModelSelection(origin, token, threadId, modelSelection) {
|
|
1919
|
+
await withT3Socket(origin, token, async (rpc) => {
|
|
1920
|
+
await rpc("orchestration.dispatchCommand", {
|
|
1921
|
+
type: "thread.model-selection.set",
|
|
1922
|
+
commandId: `qq-model:${randomUUID()}`,
|
|
1923
|
+
threadId,
|
|
1924
|
+
modelSelection
|
|
1925
|
+
});
|
|
1926
|
+
});
|
|
1927
|
+
}
|
|
1928
|
+
async function createNativeThread(origin, token, input) {
|
|
1929
|
+
const threadId = input.threadId ?? randomUUID();
|
|
1930
|
+
await withT3Socket(origin, token, async (rpc) => {
|
|
1931
|
+
await rpc("orchestration.dispatchCommand", {
|
|
1932
|
+
type: "thread.create",
|
|
1933
|
+
commandId: `qq-create:${randomUUID()}`,
|
|
1934
|
+
threadId,
|
|
1935
|
+
projectId: input.projectId,
|
|
1936
|
+
title: input.title,
|
|
1937
|
+
modelSelection: input.modelSelection,
|
|
1938
|
+
runtimeMode: input.runtimeMode ?? "full-access",
|
|
1939
|
+
interactionMode: input.interactionMode ?? "default",
|
|
1940
|
+
branch: null,
|
|
1941
|
+
worktreePath: null,
|
|
1942
|
+
createdBy: "user",
|
|
1943
|
+
creationSource: "server"
|
|
1944
|
+
});
|
|
1945
|
+
});
|
|
1946
|
+
return { threadId };
|
|
1947
|
+
}
|
|
1948
|
+
async function stopNativeThread(origin, token, threadId, options = {}) {
|
|
1949
|
+
await withT3Socket(origin, token, async (rpc) => {
|
|
1950
|
+
await rpc("orchestration.dispatchCommand", {
|
|
1951
|
+
type: "thread.stop",
|
|
1952
|
+
commandId: `qq-stop:${randomUUID()}`,
|
|
1953
|
+
threadId,
|
|
1954
|
+
...options.reason ? { reason: options.reason } : {},
|
|
1955
|
+
...options.stopDelegatedTasks !== void 0 ? { stopDelegatedTasks: options.stopDelegatedTasks } : {}
|
|
1956
|
+
});
|
|
1957
|
+
});
|
|
1958
|
+
}
|
|
1959
|
+
async function settleNativeThread(origin, token, threadId) {
|
|
1960
|
+
await withT3Socket(origin, token, async (rpc) => {
|
|
1961
|
+
await rpc("orchestration.dispatchCommand", {
|
|
1962
|
+
type: "thread.settle",
|
|
1963
|
+
commandId: `qq-settle:${randomUUID()}`,
|
|
1964
|
+
threadId
|
|
1965
|
+
});
|
|
1966
|
+
});
|
|
1967
|
+
}
|
|
1728
1968
|
async function readReply(origin, token, threadId, runId, options = {}) {
|
|
1729
1969
|
const deadline = Date.now() + (options.lifetimeMs ?? DEFAULT_OBSERVE_LIFETIME_MS);
|
|
1730
1970
|
const textByItem = /* @__PURE__ */ new Map();
|
|
@@ -1989,11 +2229,29 @@ async function createBridgeRuntime(config, credentials, options = {}) {
|
|
|
1989
2229
|
`Workspace "${binding.workspace}" is not registered in T3 Code. Open or add it as a T3 project first.`
|
|
1990
2230
|
);
|
|
1991
2231
|
}
|
|
1992
|
-
|
|
1993
|
-
|
|
1994
|
-
|
|
1995
|
-
|
|
1996
|
-
|
|
2232
|
+
let modelSelection = binding.modelSelection?.model ? {
|
|
2233
|
+
...binding.modelSelection.provider ? { instanceId: binding.modelSelection.provider } : {},
|
|
2234
|
+
model: binding.modelSelection.model
|
|
2235
|
+
} : void 0;
|
|
2236
|
+
if (!modelSelection) {
|
|
2237
|
+
const caps = await observer.call("orchestrator_capabilities", {});
|
|
2238
|
+
const firstProv = caps.providers.find((p) => p.models.length > 0 && p.canRunChildTask !== false) ?? caps.providers[0];
|
|
2239
|
+
const firstModel = firstProv?.models[0]?.id;
|
|
2240
|
+
if (!firstProv || !firstModel) {
|
|
2241
|
+
throw new Error("T3 Code has no enabled provider model available to auto-create a thread");
|
|
2242
|
+
}
|
|
2243
|
+
modelSelection = { instanceId: firstProv.providerInstanceId, model: firstModel };
|
|
2244
|
+
}
|
|
2245
|
+
const created = await t3Cli.withSession(
|
|
2246
|
+
{ scopes: ["orchestration:read", "orchestration:operate"], ttl: "2m", label: "T3 QQBot auto thread" },
|
|
2247
|
+
(token) => createNativeThread(config.t3.origin, token, {
|
|
2248
|
+
projectId: project.id,
|
|
2249
|
+
title: `QQ ${scope.startsWith("group:") ? "Group" : "Chat"} ${binding.scopeHash.slice(0, 8)}`,
|
|
2250
|
+
modelSelection,
|
|
2251
|
+
runtimeMode: "full-access",
|
|
2252
|
+
interactionMode: "default"
|
|
2253
|
+
})
|
|
2254
|
+
);
|
|
1997
2255
|
if (!created?.threadId) throw new Error("T3 did not return threadId when auto-creating a thread");
|
|
1998
2256
|
await writeActiveThreadOverride(root, binding.scopeHash, created.threadId);
|
|
1999
2257
|
console.info("Auto-created T3 thread for scope", `sha256:${binding.scopeHash.slice(0, 12)}`, created.threadId);
|
|
@@ -2020,13 +2278,37 @@ async function createBridgeRuntime(config, credentials, options = {}) {
|
|
|
2020
2278
|
threadLocks.delete(binding.scopeHash);
|
|
2021
2279
|
await writeActiveThreadOverride(root, binding.scopeHash, nextThreadId);
|
|
2022
2280
|
},
|
|
2281
|
+
getThreadConfig: (threadId) => t3Cli.withSession(
|
|
2282
|
+
{ scopes: ["orchestration:read"], ttl: "2m", label: "T3 QQBot read thread" },
|
|
2283
|
+
(token) => getThreadState(config.t3.origin, token, threadId)
|
|
2284
|
+
),
|
|
2285
|
+
setThreadModel: (threadId, modelSelection) => t3Cli.withSession(
|
|
2286
|
+
{ scopes: ["orchestration:read", "orchestration:operate"], ttl: "2m", label: "T3 QQBot set model" },
|
|
2287
|
+
(token) => setThreadModelSelection(config.t3.origin, token, threadId, modelSelection)
|
|
2288
|
+
),
|
|
2289
|
+
createThread: (input) => t3Cli.withSession(
|
|
2290
|
+
{ scopes: ["orchestration:read", "orchestration:operate"], ttl: "2m", label: "T3 QQBot create thread" },
|
|
2291
|
+
(token) => createNativeThread(config.t3.origin, token, {
|
|
2292
|
+
...input,
|
|
2293
|
+
runtimeMode: "full-access",
|
|
2294
|
+
interactionMode: "default"
|
|
2295
|
+
})
|
|
2296
|
+
),
|
|
2297
|
+
stopThread: (threadId, options2) => t3Cli.withSession(
|
|
2298
|
+
{ scopes: ["orchestration:read", "orchestration:operate"], ttl: "2m", label: "T3 QQBot stop thread" },
|
|
2299
|
+
(token) => stopNativeThread(config.t3.origin, token, threadId, options2)
|
|
2300
|
+
),
|
|
2023
2301
|
rollbackTurns: (threadId, turns, restoreFiles) => t3Cli.withSession(
|
|
2024
2302
|
{ scopes: ["orchestration:read", "orchestration:operate"], ttl: "2m", label: "T3 QQBot rollback" },
|
|
2025
2303
|
(token) => rollbackThreadTurns(config.t3.origin, token, threadId, { turns, restoreFiles })
|
|
2026
2304
|
),
|
|
2027
2305
|
triggerGracefulRestart: () => {
|
|
2028
2306
|
console.info("Triggering graceful restart after /update...");
|
|
2029
|
-
|
|
2307
|
+
if (options.onRestartRequested) {
|
|
2308
|
+
options.onRestartRequested();
|
|
2309
|
+
} else {
|
|
2310
|
+
process.exit(SELF_UPDATE_EXIT_CODE);
|
|
2311
|
+
}
|
|
2030
2312
|
}
|
|
2031
2313
|
});
|
|
2032
2314
|
async function relayBackground(threadId, runId, taskIds, reply) {
|
|
@@ -2076,7 +2358,7 @@ ${item.text?.trim() ?? ""}`.trim();
|
|
|
2076
2358
|
}
|
|
2077
2359
|
}
|
|
2078
2360
|
}
|
|
2079
|
-
async function observeRun(key, reply, threadId, runId, sequence, live = true) {
|
|
2361
|
+
async function observeRun(key, reply, threadId, runId, sequence, live = true, emptyCompletedNotice) {
|
|
2080
2362
|
const maxChars = config.bridge.maxMessageChars;
|
|
2081
2363
|
const streams = /* @__PURE__ */ new Map();
|
|
2082
2364
|
const observedItems = /* @__PURE__ */ new Set();
|
|
@@ -2199,10 +2481,14 @@ ${item.text?.trim() ?? ""}`.trim();
|
|
|
2199
2481
|
if (!result.body) {
|
|
2200
2482
|
console.info("T3 finished without a text reply", key, runId, result.status);
|
|
2201
2483
|
if (result.status !== "completed") {
|
|
2202
|
-
|
|
2484
|
+
const statusText = result.status === "interrupted" ? "\u5DF2\u4E2D\u65AD" : `\u72B6\u6001\uFF1A${result.status}`;
|
|
2485
|
+
await qq.sendText(reply, `T3 Code \u672C\u8F6E\u4EFB\u52A1\u672A\u5B8C\u6210\uFF08${statusText}\uFF09\uFF0C\u8BF7\u5728 T3 Code \u4E2D\u67E5\u770B\u8BE6\u60C5\u3002`).catch(
|
|
2203
2486
|
() => {
|
|
2204
2487
|
}
|
|
2205
2488
|
);
|
|
2489
|
+
} else if (emptyCompletedNotice) {
|
|
2490
|
+
await qq.sendText(reply, emptyCompletedNotice).catch(() => {
|
|
2491
|
+
});
|
|
2206
2492
|
}
|
|
2207
2493
|
}
|
|
2208
2494
|
if (result.status === "completed" && result.backgroundTaskIds.length) {
|
|
@@ -2211,7 +2497,10 @@ ${item.text?.trim() ?? ""}`.trim();
|
|
|
2211
2497
|
);
|
|
2212
2498
|
}
|
|
2213
2499
|
if (config.bridge.autoSettleThread && result.status === "completed" && !result.backgroundTaskIds.length) {
|
|
2214
|
-
void
|
|
2500
|
+
void t3Cli.withSession(
|
|
2501
|
+
{ scopes: ["orchestration:read", "orchestration:operate"], ttl: "2m", label: "T3 QQBot settle" },
|
|
2502
|
+
(token) => settleNativeThread(config.t3.origin, token, threadId)
|
|
2503
|
+
).catch(() => {
|
|
2215
2504
|
});
|
|
2216
2505
|
}
|
|
2217
2506
|
}
|
|
@@ -2233,6 +2522,7 @@ ${item.text?.trim() ?? ""}`.trim();
|
|
|
2233
2522
|
await qq.sendText(reply, "\u8BF7\u8F93\u5165\u6D88\u606F\u6B63\u6587\uFF0C\u6216\u9644\u4E0A\u8981\u53D1\u9001\u7684\u6587\u4EF6\u3002");
|
|
2234
2523
|
return;
|
|
2235
2524
|
}
|
|
2525
|
+
let emptyCompletedNotice;
|
|
2236
2526
|
if (config.bridge.enableSlashCommands) {
|
|
2237
2527
|
const slash = parseSlashCommand(event.content ?? "", Boolean(event.attachments?.length));
|
|
2238
2528
|
if (slash) {
|
|
@@ -2242,6 +2532,9 @@ ${item.text?.trim() ?? ""}`.trim();
|
|
|
2242
2532
|
console.info("QQ slash command handled", slash.name, key);
|
|
2243
2533
|
return;
|
|
2244
2534
|
}
|
|
2535
|
+
if (slash.name === "compact") {
|
|
2536
|
+
emptyCompletedNotice = "\u5F53\u524D T3 \u4F1A\u8BDD\u4E0A\u4E0B\u6587\u538B\u7F29\u5DF2\u5B8C\u6210\u3002";
|
|
2537
|
+
}
|
|
2245
2538
|
} catch (error) {
|
|
2246
2539
|
const msg = error instanceof Error ? error.message : String(error);
|
|
2247
2540
|
console.error("QQ slash command failed", slash.name, msg);
|
|
@@ -2315,7 +2608,7 @@ ${item.text?.trim() ?? ""}`.trim();
|
|
|
2315
2608
|
await writeFile3(join5(root, digest(accepted.runId) + ".run.json"), JSON.stringify(record), { mode: 384 });
|
|
2316
2609
|
timing("dispatch_ack", key, accepted.runId);
|
|
2317
2610
|
console.info("T3 accepted", key, threadId, accepted.runId);
|
|
2318
|
-
await observeRun(key, reply, threadId, accepted.runId, accepted.sequence);
|
|
2611
|
+
await observeRun(key, reply, threadId, accepted.runId, accepted.sequence, true, emptyCompletedNotice);
|
|
2319
2612
|
} finally {
|
|
2320
2613
|
stopTyping();
|
|
2321
2614
|
}
|
|
@@ -2446,7 +2739,14 @@ async function runBridgeCli(argv = process.argv.slice(2)) {
|
|
|
2446
2739
|
}
|
|
2447
2740
|
return;
|
|
2448
2741
|
}
|
|
2449
|
-
|
|
2742
|
+
let runtime;
|
|
2743
|
+
runtime = await createBridgeRuntime(config, credentials, {
|
|
2744
|
+
listen: true,
|
|
2745
|
+
onRestartRequested: () => {
|
|
2746
|
+
console.info(`Restarting T3 QQBot bridge after self-update (exit code ${SELF_UPDATE_EXIT_CODE})...`);
|
|
2747
|
+
void runtime.close().finally(() => process.exit(SELF_UPDATE_EXIT_CODE));
|
|
2748
|
+
}
|
|
2749
|
+
});
|
|
2450
2750
|
const address = runtime.server?.address();
|
|
2451
2751
|
const port = typeof address === "object" && address ? address.port : config.bridge.port;
|
|
2452
2752
|
console.info(
|