@zaofan/dsh-qqbot 1.11.29 → 1.11.31
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.
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"channel-tools.d.ts","sourceRoot":"","sources":["../src/channel-tools.ts"],"names":[],"mappings":"AAYA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAC;
|
|
1
|
+
{"version":3,"file":"channel-tools.d.ts","sourceRoot":"","sources":["../src/channel-tools.ts"],"names":[],"mappings":"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"}
|
package/dist/channel-tools.js
CHANGED
|
@@ -12,6 +12,7 @@ import { uploadMarkdownImage, listMarkdownImages, deleteMarkdownImage } from './
|
|
|
12
12
|
import { setMaterialTargetFactory } from './features/md-media-rewrite.js';
|
|
13
13
|
import { appendFileSync, existsSync, mkdirSync, readFileSync, readdirSync, rmSync, statSync, writeFileSync } from 'node:fs';
|
|
14
14
|
import { join, dirname } from 'node:path';
|
|
15
|
+
import { dataRootOf } from './gateway/data-root.js';
|
|
15
16
|
import { homedir } from 'node:os';
|
|
16
17
|
import { getStickerStore } from './features/sticker-store.js';
|
|
17
18
|
import { autoTagImage } from './features/sticker-tagger.js';
|
|
@@ -117,6 +118,16 @@ export const inject = ['tools'];
|
|
|
117
118
|
let channelBridges = [];
|
|
118
119
|
/** 全局懒扫节流(2026-10-10):ensureExtToolsGlobal 挂在"每 step 都触发"的钩子上,别每次都扫盘 */
|
|
119
120
|
let lastExtGlobAt = 0;
|
|
121
|
+
/**
|
|
122
|
+
* **插件 apply 时的 ctx** —— 普通扩展工具(apply 里的 `ctx.tools.register`)就注册在它上面,
|
|
123
|
+
* 所以只有 QQ 会话(插件的子 agent)继承得到,非 QQ 会话看不见。
|
|
124
|
+
*
|
|
125
|
+
* ★★ 2026-10-31(主人第三次纠正,要求"详查到底怎么注册的"):
|
|
126
|
+
* `expose:'all'` 以前注册到的是 **session-manager 传进来的 this.ctx**(中途还试过 `ctx.root`
|
|
127
|
+
* 和 `ctx.get('tools')`),跟"普通工具那个 ctx"根本不是同一个作用域对象 ⇒ 怎么改都还是全局可见。
|
|
128
|
+
* 现在统一用这个记下来的 apply ctx,与普通工具**同一个对象**。
|
|
129
|
+
*/
|
|
130
|
+
let pluginApplyCtx = undefined;
|
|
120
131
|
/**
|
|
121
132
|
* expose:'all' 全局注册的**专用诊断**(2026-10-10 排查用)。
|
|
122
133
|
* ⚠️ 无条件写:不走 diagWrite/diagLog 那套(那套默认关,害我以为"没跑",其实是"没记")。
|
|
@@ -536,6 +547,9 @@ function stickerStoreOf(exec) {
|
|
|
536
547
|
}
|
|
537
548
|
/** 装载本插件时把 qqChannel 一并注入(由 dsh-qqbot setup 提供) */
|
|
538
549
|
export async function apply(ctx) {
|
|
550
|
+
// ★★★ 2026-10-31:记住本插件的 apply ctx —— expose:'all' 的工具必须注册到**这个** ctx 上,
|
|
551
|
+
// 才能和普通扩展工具(下面的 `ctx.tools.register`)处在同一个作用域。
|
|
552
|
+
pluginApplyCtx = ctx;
|
|
539
553
|
const toolsAny = ctx.tools;
|
|
540
554
|
diag(`apply 调用: tools=${typeof toolsAny} register=${typeof toolsAny?.register} ctxKeys=${Object.keys(ctx).slice(0, 12).join(',')}`);
|
|
541
555
|
const sendMediaTool = defineTool({
|
|
@@ -3792,56 +3806,93 @@ const extGrantSet = new WeakMap();
|
|
|
3792
3806
|
const extRestrictState2 = new WeakMap();
|
|
3793
3807
|
/** 见过的会话作用域 → 它的 selfTag(扫描完成后要按新索引**重放一次**剪枝) */
|
|
3794
3808
|
const extSeenCtx = new Map();
|
|
3809
|
+
/**
|
|
3810
|
+
* 「用户主动关掉」的工具(per agent):`gain_qq_tools.disable` 加进来,`enable` 移出去。
|
|
3811
|
+
* applyExtToolVisibility 计算该注册哪些时**排除**它们 —— 否则"自己实例那份"每轮都会被
|
|
3812
|
+
* 无条件算回 want,disable 就形同虚设(主人实测:关了还能调)。
|
|
3813
|
+
*/
|
|
3814
|
+
const extUserOff = new WeakMap();
|
|
3795
3815
|
/** 按当前会话的「已领取集合」重设一次剪枝(幂等:deny 名单没变就不动) */
|
|
3796
3816
|
export function applyExtToolVisibility(agentCtx, selfTag) {
|
|
3797
3817
|
try {
|
|
3798
3818
|
const ctx = agentCtx;
|
|
3799
3819
|
if (!ctx || typeof ctx !== 'object')
|
|
3800
3820
|
return;
|
|
3801
|
-
// ⚠️ 2026-10-28(主人实测:enable 一个 standard 后,自己实例那 4 个被移除):
|
|
3802
|
-
// gain_qq_tools 内部调本函数时**不传 selfTag**,原来会把 ownSuffix 算成空串 ⇒
|
|
3803
|
-
// 连带把本实例自己的那份也 deny 掉。现在:不传就**沿用上次记住的 tag**。
|
|
3804
3821
|
const prevTag = extSeenCtx.get(ctx) ?? '';
|
|
3805
3822
|
const tag = selfTag !== undefined ? String(selfTag) : prevTag;
|
|
3806
3823
|
extSeenCtx.set(ctx, tag);
|
|
3807
|
-
|
|
3824
|
+
// ★★★ 2026-10-31(主人实测后的最终结论):**改成 per-agent 注册**。
|
|
3825
|
+
// 实测对照:同一次运行里
|
|
3826
|
+
// · ensureExtToolsGlobal 注册到 apply(ctx) → QQ 会话调不到(unknown tool)
|
|
3827
|
+
// · gain_qq_tools.enable 注册到 agent.ctx → 立刻可用 ✅
|
|
3828
|
+
// 说明只有**注册进 agent 自己的作用域**才算数。所以这里改成:
|
|
3829
|
+
// 该 agent 应有的 = 本实例自己的(__selfTag)+ 已领取的,逐个注册到它的 ctx;
|
|
3830
|
+
// 既不用 deny,也不用 root —— 非 QQ agent 不走这段,天然零泄漏。
|
|
3831
|
+
// ⚠️ 拿不到 tag 就什么都不做(否则会把"自己实例那份"也漏掉)。
|
|
3832
|
+
if (!tag) {
|
|
3833
|
+
traceToolReg('[ext-vis] 跳过(拿不到 selfTag)');
|
|
3834
|
+
return;
|
|
3835
|
+
}
|
|
3836
|
+
const all = [...extToolIndex.values()];
|
|
3808
3837
|
if (!all.length)
|
|
3809
|
-
return; //
|
|
3810
|
-
// ⚠️ 2026-10-28(主人纠正):开关**不是**"关=不剪枝、全可见"。
|
|
3811
|
-
// 关掉元工具 = "按需取用"这条通道整个关掉 ⇒ 别人实例 expose 的工具**永远不可见**
|
|
3812
|
-
// (没有 gain 入口,也就不该偷偷放出来)。所以无论开关开/关,都照常剪:
|
|
3813
|
-
// · 自己实例那份(xxx__本tag):始终放开
|
|
3814
|
-
// · 别人那份:始终 deny;开关开着时可通过 gain_qq_tools 临时放开
|
|
3815
|
-
// ★ 2026-10-28(主人实测:关了元工具,之前领取的工具还在):开关关掉时
|
|
3816
|
-
// **忽略并清空领取记录** —— 通道一关,别人的工具就该彻底消失,不能靠残留的 granted 续命。
|
|
3838
|
+
return; // 还没扫到东西:先不动
|
|
3817
3839
|
const metaOnNow = globalThis.__qqbotExtMetaOn !== false;
|
|
3818
3840
|
if (!metaOnNow)
|
|
3819
3841
|
extGrantSet.delete(ctx);
|
|
3820
3842
|
const granted = metaOnNow ? (extGrantSet.get(ctx) || new Set()) : new Set();
|
|
3821
|
-
|
|
3822
|
-
//
|
|
3823
|
-
|
|
3824
|
-
|
|
3825
|
-
|
|
3843
|
+
const ownSuffix = '__' + tag;
|
|
3844
|
+
// ★★★ 2026-10-31(主人实测:disable 之后还能调用)——
|
|
3845
|
+
// "自己实例那份"是靠 `endsWith(ownSuffix)` 无条件进 want 的,所以 disable 只删 grant 没用:
|
|
3846
|
+
// 下一轮 apply 又把它算进 want ⇒ 重新注册 ⇒ 照样能调。
|
|
3847
|
+
// 现在加一个"用户主动关掉"的集合 `extUserOff`:want 计算时排除它;
|
|
3848
|
+
// enable 时移除、disable 时加入并**立即**调 disposer 注销。
|
|
3849
|
+
const offSet = extUserOff.get(ctx) ?? new Set();
|
|
3850
|
+
const want = new Map();
|
|
3851
|
+
for (const ent of all) {
|
|
3852
|
+
if (offSet.has(ent.full))
|
|
3853
|
+
continue; // 用户关掉的,一律不注册
|
|
3854
|
+
if (ent.full.endsWith(ownSuffix) || granted.has(ent.full))
|
|
3855
|
+
want.set(ent.full, ent);
|
|
3856
|
+
// 注:granted 里可能有别的实例的工具(gain 领取的),也一并注册进本 agent
|
|
3857
|
+
}
|
|
3858
|
+
const sig = (metaOnNow ? '1' : '0') + '|' + [...want.keys()].sort().join(',');
|
|
3826
3859
|
const st = extRestrictState2.get(ctx);
|
|
3827
3860
|
if (st && st.sig === sig)
|
|
3828
|
-
return;
|
|
3829
|
-
if (st?.dispose)
|
|
3830
|
-
liftRestriction(st.dispose);
|
|
3861
|
+
return; // 状态一致 → 不动(防抖)
|
|
3831
3862
|
const toolsObj = toolsOfScope(ctx);
|
|
3832
|
-
const
|
|
3833
|
-
if (typeof
|
|
3834
|
-
traceToolReg('[ext-vis]
|
|
3863
|
+
const reg = toolsObj?.register;
|
|
3864
|
+
if (!toolsObj || typeof reg !== 'function') {
|
|
3865
|
+
traceToolReg('[ext-vis] per-agent 注册失败: 拿不到 tools.register');
|
|
3835
3866
|
return;
|
|
3836
3867
|
}
|
|
3837
|
-
|
|
3838
|
-
|
|
3839
|
-
|
|
3868
|
+
// 先撤掉不再需要的
|
|
3869
|
+
const disposers = st?.disposers ?? new Map();
|
|
3870
|
+
for (const [name, d] of [...disposers]) {
|
|
3871
|
+
if (want.has(name))
|
|
3872
|
+
continue;
|
|
3873
|
+
try {
|
|
3874
|
+
d();
|
|
3875
|
+
}
|
|
3876
|
+
catch { /* ignore */ }
|
|
3877
|
+
disposers.delete(name);
|
|
3840
3878
|
}
|
|
3841
|
-
|
|
3842
|
-
const
|
|
3843
|
-
|
|
3844
|
-
|
|
3879
|
+
// 再补缺的
|
|
3880
|
+
for (const [full, ent] of want) {
|
|
3881
|
+
if (disposers.has(full))
|
|
3882
|
+
continue;
|
|
3883
|
+
try {
|
|
3884
|
+
const tool = buildExtTool(ent, pluginApplyCtx ?? ctx, (ctx.logger) ?? console);
|
|
3885
|
+
const d = reg.call(toolsObj, tool);
|
|
3886
|
+
if (typeof d === 'function')
|
|
3887
|
+
disposers.set(full, d);
|
|
3888
|
+
traceToolReg('[ext-vis] per-agent 注册 ' + full);
|
|
3889
|
+
}
|
|
3890
|
+
catch (e) {
|
|
3891
|
+
traceToolReg('[ext-vis] 注册失败 ' + full + ': ' + String(e?.message ?? e).slice(0, 100));
|
|
3892
|
+
}
|
|
3893
|
+
}
|
|
3894
|
+
extRestrictState2.set(ctx, { sig, dispose: undefined, disposers });
|
|
3895
|
+
traceToolReg('[ext-vis] per-agent 应注册=' + want.size + '(own=' + ownSuffix + ' granted=' + granted.size + ')');
|
|
3845
3896
|
}
|
|
3846
3897
|
catch (e) {
|
|
3847
3898
|
traceToolReg('[ext-vis] 异常: ' + String(e?.message ?? e).slice(0, 140));
|
|
@@ -3942,7 +3993,20 @@ function makeGainTool(globalCtx, logger) {
|
|
|
3942
3993
|
const wantTag = String(args?.tag ?? '').trim();
|
|
3943
3994
|
const limRaw = Number(args?.limit);
|
|
3944
3995
|
const lim = Math.max(1, Math.min(50, Number.isFinite(limRaw) && limRaw > 0 ? Math.trunc(limRaw) : 20));
|
|
3945
|
-
|
|
3996
|
+
// ★★ 2026-10-31(主人定):**不该看到自己的工具** —— 本实例那批 expose:'all' 的工具
|
|
3997
|
+
// 已经在 agent ctx 自动注册、随时可用,列进"可领取"里纯属噪音(还容易重复 enable)。
|
|
3998
|
+
// 非 QQ 会话拿不到 selfTag ⇒ 不过滤(能领所有已勾选实例的)。
|
|
3999
|
+
let selfTagOfCaller = '';
|
|
4000
|
+
try {
|
|
4001
|
+
const sess0 = findSessionRec(channelOf(exec), exec);
|
|
4002
|
+
const mgr0 = sess0?.ch?.manager;
|
|
4003
|
+
if (mgr0)
|
|
4004
|
+
selfTagOfCaller = instanceTagOfManager(mgr0);
|
|
4005
|
+
}
|
|
4006
|
+
catch { /* ignore */ }
|
|
4007
|
+
const rows = [...extToolIndex.values()]
|
|
4008
|
+
.filter((e) => !selfTagOfCaller || e.tag !== selfTagOfCaller)
|
|
4009
|
+
.filter((e) => (!wantTag || e.tag === wantTag));
|
|
3946
4010
|
try {
|
|
3947
4011
|
if (action === 'list' || action === 'search') {
|
|
3948
4012
|
const hit = action === 'search'
|
|
@@ -3984,6 +4048,11 @@ function makeGainTool(globalCtx, logger) {
|
|
|
3984
4048
|
continue;
|
|
3985
4049
|
}
|
|
3986
4050
|
gset.add(ent.full);
|
|
4051
|
+
// enable 时把它从"用户主动关掉"的集合里移出来(否则 apply 仍会排除它)
|
|
4052
|
+
try {
|
|
4053
|
+
extUserOff.get(actx)?.delete(ent.full);
|
|
4054
|
+
}
|
|
4055
|
+
catch { /* ignore */ }
|
|
3987
4056
|
// 兜底:按需取用模式下工具未全局注册,得真注册一次
|
|
3988
4057
|
if (!mm.has(ent.full) && toolsObj && typeof regFn === 'function') {
|
|
3989
4058
|
try {
|
|
@@ -4003,56 +4072,59 @@ function makeGainTool(globalCtx, logger) {
|
|
|
4003
4072
|
if (action === 'disable') {
|
|
4004
4073
|
const actx = agentCtxOf(exec);
|
|
4005
4074
|
if (actx) {
|
|
4006
|
-
|
|
4007
|
-
|
|
4008
|
-
|
|
4009
|
-
|
|
4010
|
-
|
|
4011
|
-
|
|
4012
|
-
|
|
4013
|
-
|
|
4014
|
-
|
|
4015
|
-
|
|
4016
|
-
|
|
4017
|
-
|
|
4075
|
+
// ★★★ 2026-10-31(主人实测:disable 后还能调用)——
|
|
4076
|
+
// 两个都要做:① 加进"用户主动关掉"集合(apply 计算 want 时排除)
|
|
4077
|
+
// ② **立即**调 disposer 注销(否则本会话的 schema 还在,照样能调)
|
|
4078
|
+
const offNow = (() => {
|
|
4079
|
+
let s = extUserOff.get(actx);
|
|
4080
|
+
if (!s) {
|
|
4081
|
+
s = new Set();
|
|
4082
|
+
extUserOff.set(actx, s);
|
|
4083
|
+
}
|
|
4084
|
+
return s;
|
|
4085
|
+
})();
|
|
4086
|
+
const killNow = (full) => {
|
|
4087
|
+
try {
|
|
4088
|
+
const stAny = extRestrictState2.get(actx);
|
|
4089
|
+
const dm = stAny?.disposers;
|
|
4090
|
+
const d = dm?.get(full);
|
|
4091
|
+
if (d) {
|
|
4018
4092
|
d();
|
|
4093
|
+
dm.delete(full);
|
|
4094
|
+
}
|
|
4095
|
+
const g2 = extGranted.get(actx);
|
|
4096
|
+
const d2 = g2?.get(full);
|
|
4097
|
+
if (d2) {
|
|
4098
|
+
d2();
|
|
4099
|
+
g2.delete(full);
|
|
4019
4100
|
}
|
|
4020
|
-
catch { /* ignore */ }
|
|
4021
|
-
dm.delete(full);
|
|
4022
4101
|
}
|
|
4102
|
+
catch { /* ignore */ }
|
|
4023
4103
|
};
|
|
4024
4104
|
if (!wantTools.length) {
|
|
4025
|
-
const
|
|
4026
|
-
|
|
4027
|
-
|
|
4028
|
-
|
|
4029
|
-
d();
|
|
4030
|
-
}
|
|
4031
|
-
catch { /* ignore */ }
|
|
4032
|
-
}
|
|
4033
|
-
dm.clear();
|
|
4105
|
+
const all2 = [...extToolIndex.keys()];
|
|
4106
|
+
for (const full of all2) {
|
|
4107
|
+
offNow.add(full);
|
|
4108
|
+
killNow(full);
|
|
4034
4109
|
}
|
|
4035
|
-
|
|
4036
|
-
|
|
4037
|
-
return { ok: true, msg: '
|
|
4110
|
+
extGrantSet.get(actx)?.clear();
|
|
4111
|
+
extRestrictState2.set(actx, { sig: '', dispose: undefined });
|
|
4112
|
+
return { ok: true, msg: '已在本会话关闭全部扩展工具(已注销)' };
|
|
4038
4113
|
}
|
|
4039
4114
|
const done = [];
|
|
4040
|
-
const miss = [];
|
|
4041
4115
|
for (const name of wantTools) {
|
|
4042
4116
|
const ent = extToolIndex.get(name) || rows.find((e) => e.baseName === name) || undefined;
|
|
4043
4117
|
const full = ent ? ent.full : name;
|
|
4044
|
-
|
|
4045
|
-
|
|
4046
|
-
|
|
4047
|
-
|
|
4048
|
-
else
|
|
4049
|
-
miss.push(name);
|
|
4118
|
+
offNow.add(full);
|
|
4119
|
+
killNow(full);
|
|
4120
|
+
extGrantSet.get(actx)?.delete(full);
|
|
4121
|
+
done.push(full);
|
|
4050
4122
|
}
|
|
4051
|
-
|
|
4123
|
+
// 让下一轮 apply 按新状态重算(sig 置空强制重跑)
|
|
4124
|
+
extRestrictState2.set(actx, { sig: '', dispose: undefined });
|
|
4052
4125
|
return {
|
|
4053
|
-
ok:
|
|
4054
|
-
msg:
|
|
4055
|
-
+ (miss.length ? (';未领取:' + miss.join(', ')) : ''),
|
|
4126
|
+
ok: true,
|
|
4127
|
+
msg: '已在本会话关闭 ' + done.length + ' 个:' + done.join(', ') + '(已注销,再调用会报 unknown tool)',
|
|
4056
4128
|
};
|
|
4057
4129
|
}
|
|
4058
4130
|
const m = extGranted.get(actx);
|
|
@@ -4143,21 +4215,27 @@ export function ensureExtToolsGlobal(globalCtx) {
|
|
|
4143
4215
|
try {
|
|
4144
4216
|
if (!globalCtx || typeof globalCtx !== 'object')
|
|
4145
4217
|
return;
|
|
4146
|
-
// ★★ 2026-10-
|
|
4147
|
-
//
|
|
4148
|
-
//
|
|
4149
|
-
//
|
|
4150
|
-
//
|
|
4151
|
-
|
|
4152
|
-
|
|
4153
|
-
const rootCtx = globalCtx;
|
|
4154
|
-
extGlobDiag('scope=plugin-ctx(不再挂 root)');
|
|
4218
|
+
// ★★ 2026-10-31(主人第三次纠正,要求"详查到底怎么注册的"):
|
|
4219
|
+
// 普通扩展工具注册在 **`apply(ctx)` 的参数 ctx** 上(见本文件 apply 里的 `ctx.tools.register`),
|
|
4220
|
+
// 而 expose:'all' 这条一直用的是 **session-manager 传来的 this.ctx** —— 两者**不是同一个作用域对象**!
|
|
4221
|
+
// (中途还试过 `ctx.root`(根层)、`ctx.get('tools')`(向上查找到的根层服务),都会落到全局层。)
|
|
4222
|
+
// ⇒ 统一用 `pluginApplyCtx`(apply 时记下的那个),与普通工具**完全同一个对象**。
|
|
4223
|
+
const rootCtx = (pluginApplyCtx ?? globalCtx);
|
|
4224
|
+
extGlobDiag('scope=apply-ctx(同普通扩展工具)' + (pluginApplyCtx ? '' : '(未记录,退回传入 ctx)'));
|
|
4155
4225
|
const gctx = rootCtx;
|
|
4156
4226
|
let toolsObj;
|
|
4227
|
+
// ⚠️ 必须**优先 `ctx.tools`(属性访问 → 绑定本作用域)**;
|
|
4228
|
+
// `ctx.get('tools')` 是服务查找,会向上拿到**根层**的实例 ⇒ 注册等于挂全局。
|
|
4157
4229
|
try {
|
|
4158
|
-
toolsObj = gctx.
|
|
4230
|
+
toolsObj = gctx.tools;
|
|
4231
|
+
}
|
|
4232
|
+
catch { /* 未 inject 会抛 */ }
|
|
4233
|
+
if (!toolsObj || typeof toolsObj.register !== 'function') {
|
|
4234
|
+
try {
|
|
4235
|
+
toolsObj = gctx.get?.('tools');
|
|
4236
|
+
}
|
|
4237
|
+
catch { /* ignore */ }
|
|
4159
4238
|
}
|
|
4160
|
-
catch { /* ignore */ }
|
|
4161
4239
|
if (!toolsObj || typeof toolsObj.register !== 'function') {
|
|
4162
4240
|
extGlobDiag('FAIL 拿不到 tools 服务');
|
|
4163
4241
|
extDiag('[extglob] 拿不到 tools 服务, 跳过');
|
|
@@ -4165,39 +4243,46 @@ export function ensureExtToolsGlobal(globalCtx) {
|
|
|
4165
4243
|
}
|
|
4166
4244
|
// 2026-10-28:元工具**先注册**,且不依赖"有没有实例"——
|
|
4167
4245
|
// 上一版放在扫描末尾,若中途 early-return 就永远注册不上(实测:开了按需取用却连元工具都没有)。
|
|
4246
|
+
// ★★★ 2026-10-31(主人实测:关掉三号的勾选,非 QQ 会话的 gain 也被移除了)——
|
|
4247
|
+
// **只要有一个启动中的实例还勾着,gain_qq_tools 就必须在**。
|
|
4248
|
+
// 以前第 ① 步读的是 `managersOf()[0]`(通常是三号 standard),它一关就 return false,
|
|
4249
|
+
// 把 gain 整个摘掉 ⇒ 二号明明还开着,非 QQ 会话却没入口了。
|
|
4250
|
+
// 现在:遍历**所有实例**,任一勾选即 true(文件优先,config 兜底)。
|
|
4168
4251
|
const metaOn = (() => {
|
|
4169
|
-
|
|
4170
|
-
// settings-host 保存时写 {dataRoot}/.qqbot/ext-meta-tool.json,这里每次扫描现读;
|
|
4171
|
-
// 不依赖 config 快照,也不依赖 globalThis 有没有被写过。
|
|
4252
|
+
let anyOn = false;
|
|
4172
4253
|
try {
|
|
4173
|
-
const
|
|
4174
|
-
|
|
4175
|
-
|
|
4176
|
-
if (
|
|
4177
|
-
|
|
4254
|
+
for (const m of managersOf()) {
|
|
4255
|
+
const mm = m;
|
|
4256
|
+
let dr = String(mm.dataRoot ?? '').trim();
|
|
4257
|
+
if (!dr) {
|
|
4258
|
+
try {
|
|
4259
|
+
dr = dataRootOf(mm.config);
|
|
4260
|
+
}
|
|
4261
|
+
catch {
|
|
4262
|
+
dr = '';
|
|
4263
|
+
}
|
|
4264
|
+
}
|
|
4265
|
+
if (dr) {
|
|
4266
|
+
try {
|
|
4267
|
+
const o = JSON.parse(readFileSync(join(dr, '.qqbot', 'ext-meta-tool.json'), 'utf8'));
|
|
4268
|
+
if (o?.on === true)
|
|
4269
|
+
anyOn = true;
|
|
4270
|
+
continue; // 该实例有明确文件设置,按文件算
|
|
4271
|
+
}
|
|
4272
|
+
catch { /* 没文件,看 config */ }
|
|
4273
|
+
}
|
|
4274
|
+
if (mm.config?.extMetaTool === true)
|
|
4275
|
+
anyOn = true;
|
|
4178
4276
|
}
|
|
4179
4277
|
}
|
|
4180
|
-
catch { /* 文件不存在 = 还没设过,往下走 */ }
|
|
4181
|
-
// ② 进程级热标记:设置页保存时 settings-host 写入(config 是启动快照,不会自己变)。
|
|
4182
|
-
try {
|
|
4183
|
-
const live = globalThis.__qqbotExtMetaTool;
|
|
4184
|
-
if (typeof live === 'boolean')
|
|
4185
|
-
return live;
|
|
4186
|
-
}
|
|
4187
4278
|
catch { /* ignore */ }
|
|
4188
|
-
//
|
|
4189
|
-
// index.ts 的 'qqbot/settings-changed' 监听会 把
|
|
4190
|
-
// 保存值原地合并进当前实例的 config(这就是面板其它开关的热更路子);但只有**被保存的那个
|
|
4191
|
-
// 实例**里有这个字段,第一个 manager 未必是它 ⇒ 只看第 0 个会永远读到 undefined/false。
|
|
4279
|
+
// 进程级热标记:只在"刚保存为开"时补一票(绝不能因为它是 false 就否定别的实例)
|
|
4192
4280
|
try {
|
|
4193
|
-
|
|
4194
|
-
|
|
4195
|
-
if (cfgM && cfgM.extMetaTool === true)
|
|
4196
|
-
return true;
|
|
4197
|
-
}
|
|
4281
|
+
if (globalThis.__qqbotExtMetaTool === true)
|
|
4282
|
+
anyOn = true;
|
|
4198
4283
|
}
|
|
4199
4284
|
catch { /* ignore */ }
|
|
4200
|
-
return
|
|
4285
|
+
return anyOn;
|
|
4201
4286
|
})();
|
|
4202
4287
|
// ★ 2026-10-28(主人急):**无条件注册元工具** —— 它是个极轻的入口,本来就不该被任何开关挡住。
|
|
4203
4288
|
// "开了按需取用却连 gain_qq_tools 都看不到" 这种状态毫无意义:要么两个都有,要么两个都没有。
|
|
@@ -4223,6 +4308,7 @@ export function ensureExtToolsGlobal(globalCtx) {
|
|
|
4223
4308
|
if (!g.__qqbotExtToolsGlobal)
|
|
4224
4309
|
g.__qqbotExtToolsGlobal = new Map();
|
|
4225
4310
|
const reg = g.__qqbotExtToolsGlobal;
|
|
4311
|
+
void reg; // 2026-10-31:全局注册表已弃用(改 per-agent 注册),保留键名只为兼容。
|
|
4226
4312
|
// 开关(默认开 = 按需取用):关掉后回到"扩展工具全部全局注册"的旧行为
|
|
4227
4313
|
// 2026-10-28(主人实测纠正):默认**保持旧行为**(全部 expose:'all' 全局注册)。
|
|
4228
4314
|
// 原因:第一版把默认值设成"按需取用",结果元工具没注册上 => 所有会话一个扩展工具都没有
|
|
@@ -4233,7 +4319,31 @@ export function ensureExtToolsGlobal(globalCtx) {
|
|
|
4233
4319
|
globalThis.__qqbotExtMetaOn = metaMode;
|
|
4234
4320
|
extGlobDiag('metaMode=' + String(metaMode));
|
|
4235
4321
|
// ── 阶段 1:扫描,**只填索引**(不注册)────────────────────────────
|
|
4322
|
+
// ★★ 2026-10-31(主人实测:关掉三号的开关,gain 还能列/领它的工具):
|
|
4323
|
+
// **只扫"勾了按需取用"的实例**。没勾的实例,它扩展目录里的 expose:'all'
|
|
4324
|
+
// 既不进索引、也就不会出现在 gain 的可领取列表里、更领不到。
|
|
4236
4325
|
for (const inst of insts) {
|
|
4326
|
+
const optIn = (() => {
|
|
4327
|
+
try {
|
|
4328
|
+
const o = JSON.parse(readFileSync(join(inst.dataRoot, '.qqbot', 'ext-meta-tool.json'), 'utf8'));
|
|
4329
|
+
if (typeof o?.on === 'boolean')
|
|
4330
|
+
return o.on;
|
|
4331
|
+
}
|
|
4332
|
+
catch { /* 没文件 → 看 config */ }
|
|
4333
|
+
try {
|
|
4334
|
+
for (const m of managersOf()) {
|
|
4335
|
+
const mm = m;
|
|
4336
|
+
if (String(mm.dataRoot ?? '') === inst.dataRoot && mm.config?.extMetaTool === true)
|
|
4337
|
+
return true;
|
|
4338
|
+
}
|
|
4339
|
+
}
|
|
4340
|
+
catch { /* ignore */ }
|
|
4341
|
+
return false;
|
|
4342
|
+
})();
|
|
4343
|
+
if (!optIn) {
|
|
4344
|
+
extGlobDiag('SKIP not-opted-in: ' + inst.tag);
|
|
4345
|
+
continue;
|
|
4346
|
+
}
|
|
4237
4347
|
let defs = [];
|
|
4238
4348
|
try {
|
|
4239
4349
|
defs = await loadExtensionTools(inst.dataRoot, (gctx.logger ?? console));
|
|
@@ -4257,18 +4367,23 @@ export function ensureExtToolsGlobal(globalCtx) {
|
|
|
4257
4367
|
});
|
|
4258
4368
|
}
|
|
4259
4369
|
}
|
|
4260
|
-
//
|
|
4261
|
-
//
|
|
4262
|
-
//
|
|
4263
|
-
//
|
|
4264
|
-
|
|
4265
|
-
|
|
4266
|
-
if (
|
|
4267
|
-
|
|
4370
|
+
// ★★ 2026-10-31(主人要求):**每个实例各自落一份清单**到自己的 dataRoot:
|
|
4371
|
+
// `{dataRoot}/.qqbot/ext-tools-index.json`,只列**自己 tag** 的 expose:'all' 工具。
|
|
4372
|
+
// 用途:① 非 QQ 会话的 gain_qq_tools 读它来"看见能领什么"并现场注册;
|
|
4373
|
+
// ② 重启后即使扫描还没跑,也能从文件读出名单。
|
|
4374
|
+
// 注意:注册行为与它无关(注册早已改成 per-agent,见 applyExtToolVisibility)。
|
|
4375
|
+
for (const inst of insts) {
|
|
4376
|
+
if (!inst || !inst.dataRoot)
|
|
4377
|
+
continue;
|
|
4378
|
+
try {
|
|
4379
|
+
const f = join(inst.dataRoot, '.qqbot', 'ext-tools-index.json');
|
|
4380
|
+
const mine = [...extToolIndex.values()].filter((e) => e.tag === inst.tag);
|
|
4268
4381
|
const payload = {
|
|
4269
4382
|
updatedAt: new Date().toISOString(),
|
|
4270
|
-
|
|
4271
|
-
|
|
4383
|
+
instance: inst.tag,
|
|
4384
|
+
dataRoot: inst.dataRoot,
|
|
4385
|
+
note: "expose:'all' 扩展工具清单(本实例;供 gain_qq_tools 查询与现场注册)",
|
|
4386
|
+
tools: mine.map((e) => ({
|
|
4272
4387
|
full: e.full,
|
|
4273
4388
|
tag: e.tag,
|
|
4274
4389
|
baseName: e.baseName,
|
|
@@ -4279,11 +4394,11 @@ export function ensureExtToolsGlobal(globalCtx) {
|
|
|
4279
4394
|
};
|
|
4280
4395
|
mkdirSync(dirname(f), { recursive: true });
|
|
4281
4396
|
writeFileSync(f, JSON.stringify(payload, null, 2), 'utf8');
|
|
4282
|
-
extGlobDiag('清单已落盘: ' + f + ' (' + payload.tools.length + '
|
|
4397
|
+
extGlobDiag('清单已落盘: ' + f + ' (' + payload.tools.length + ' 件, ' + inst.tag + ')');
|
|
4398
|
+
}
|
|
4399
|
+
catch (e) {
|
|
4400
|
+
extGlobDiag('清单落盘失败(' + inst.tag + '): ' + ((e instanceof Error ? e.message : String(e)) || '').slice(0, 120));
|
|
4283
4401
|
}
|
|
4284
|
-
}
|
|
4285
|
-
catch (e) {
|
|
4286
|
-
extGlobDiag('清单落盘失败: ' + ((e instanceof Error ? e.message : String(e)) || '').slice(0, 120));
|
|
4287
4402
|
}
|
|
4288
4403
|
// ── 阶段 2:**先剪枝**(此时索引已完整)──────────────────────────
|
|
4289
4404
|
// ★ 2026-10-28(主人抓到抖动:"新增 7 个 → 下个 step 又移除 7 个"):
|
|
@@ -4291,76 +4406,12 @@ export function ensureExtToolsGlobal(globalCtx) {
|
|
|
4291
4406
|
// 现在顺序固定为 **扫描 → 剪枝 → 注册**:注册落地时 deny 已经就位,模型永远看不到那一步。
|
|
4292
4407
|
for (const [c, tg] of extSeenCtx)
|
|
4293
4408
|
applyExtToolVisibility(c, tg);
|
|
4294
|
-
// ── 阶段 3
|
|
4295
|
-
//
|
|
4296
|
-
//
|
|
4297
|
-
|
|
4298
|
-
|
|
4299
|
-
|
|
4300
|
-
continue;
|
|
4301
|
-
const def = ent.def;
|
|
4302
|
-
const inst = ent.inst;
|
|
4303
|
-
const full = ent.full;
|
|
4304
|
-
try {
|
|
4305
|
-
const tool = defineTool({
|
|
4306
|
-
name: full,
|
|
4307
|
-
description: String(def.description ?? '') + ' (用户扩展工具 · ' + inst.tag + ')',
|
|
4308
|
-
parameters: def.inputSchema,
|
|
4309
|
-
output: {
|
|
4310
|
-
schema: {
|
|
4311
|
-
type: 'object', additionalProperties: false,
|
|
4312
|
-
properties: { ok: { type: 'boolean', required: true }, msg: { type: 'string', required: true } },
|
|
4313
|
-
},
|
|
4314
|
-
render: (_a, v) => [{ type: 'text', text: v.ok ? v.msg : ('失败: ' + v.msg) }],
|
|
4315
|
-
},
|
|
4316
|
-
async execute(args, exec) {
|
|
4317
|
-
let pack;
|
|
4318
|
-
try {
|
|
4319
|
-
pack = buildWorkspaceQqPack(inst.tag, inst.dataRoot);
|
|
4320
|
-
}
|
|
4321
|
-
catch {
|
|
4322
|
-
pack = undefined;
|
|
4323
|
-
}
|
|
4324
|
-
const env = {
|
|
4325
|
-
cwd: inst.cwd || inst.dataRoot,
|
|
4326
|
-
exec,
|
|
4327
|
-
ctx: globalCtx,
|
|
4328
|
-
logger: gctx.logger,
|
|
4329
|
-
qq: pack,
|
|
4330
|
-
};
|
|
4331
|
-
try {
|
|
4332
|
-
const sess = findSessionRec(channelOf(exec), exec);
|
|
4333
|
-
if (sess) {
|
|
4334
|
-
env.manager = sess.ch.manager;
|
|
4335
|
-
env.sender = sess.ch.sender;
|
|
4336
|
-
env.replyTarget = sess.rec.replyTarget;
|
|
4337
|
-
}
|
|
4338
|
-
else if (pack) {
|
|
4339
|
-
env.sender = pack.sender;
|
|
4340
|
-
}
|
|
4341
|
-
}
|
|
4342
|
-
catch { /* ignore */ }
|
|
4343
|
-
try {
|
|
4344
|
-
const r = await def.run(args, env);
|
|
4345
|
-
if (r && typeof r === 'object')
|
|
4346
|
-
return { ok: r.ok === true, msg: String(r.msg ?? 'done') };
|
|
4347
|
-
return { ok: true, msg: r === undefined || r === null ? 'done' : String(r) };
|
|
4348
|
-
}
|
|
4349
|
-
catch (err) {
|
|
4350
|
-
return { ok: false, msg: err instanceof Error ? err.message : String(err) };
|
|
4351
|
-
}
|
|
4352
|
-
},
|
|
4353
|
-
});
|
|
4354
|
-
const d = toolsObj.register.call(toolsObj, tool);
|
|
4355
|
-
if (typeof d === 'function')
|
|
4356
|
-
reg.set(full, d);
|
|
4357
|
-
extGlobDiag('OK registered ' + full);
|
|
4358
|
-
}
|
|
4359
|
-
catch (e) {
|
|
4360
|
-
extGlobDiag('FAIL register ' + full + ' :: ' + ((e instanceof Error ? e.message : String(e)) || '').slice(0, 200));
|
|
4361
|
-
}
|
|
4362
|
-
}
|
|
4363
|
-
}
|
|
4409
|
+
// ── 阶段 3:**不再全局注册**(2026-10-31 实测定论)──────────────
|
|
4410
|
+
// 实测对照:注册到 apply(ctx) 后 QQ 会话调不到(unknown tool),
|
|
4411
|
+
// 而 gain_qq_tools.enable 用 agent.ctx 注册**立刻可用**。
|
|
4412
|
+
// ⇒ 注册一律走 per-agent(见 applyExtToolVisibility),这里只保留索引。
|
|
4413
|
+
void rootCtx;
|
|
4414
|
+
void toolsObj;
|
|
4364
4415
|
extGlobDiag('index=' + extToolIndex.size + ' 可领取');
|
|
4365
4416
|
}
|
|
4366
4417
|
catch (e) {
|