@napuketto/adapter 0.15.0 → 0.15.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
1
- import { y as BaseProtocolAdapter } from "../core-Be1hDKmc.mjs";
1
+ import { b as BaseProtocolAdapter } from "../core-CxDffqmA.mjs";
2
2
  import { t as forEachRawMessage } from "../raw-message-D2vA27kj.mjs";
3
3
  import { createRequire } from "node:module";
4
4
  import { ChatType, toCanonicalElements } from "@napuketto/kernel";
@@ -590,6 +590,31 @@ const SILENT_LOGGER = {
590
590
  warn: () => void 0,
591
591
  error: () => void 0
592
592
  };
593
+ // src/kurobot/event-pipeline.ts
594
+ /** kurobot 事件管道(订阅接线板)。 */
595
+ var KurobotEventPipeline = class {
596
+ message;
597
+ msgChannel;
598
+ unsubscribe = null;
599
+ constructor(deps) {
600
+ this.message = deps.message;
601
+ this.msgChannel = deps.msgChannel;
602
+ }
603
+ /** 订阅 kernel 消息事件(幂等)。 */
604
+ subscribe() {
605
+ if (this.unsubscribe !== null) return;
606
+ this.unsubscribe = this.msgChannel.on("Msg/onRecvMsg", (msgs) => {
607
+ forEachRawMessage(msgs, (msg) => {
608
+ this.message.handle(msg);
609
+ });
610
+ });
611
+ }
612
+ /** 退订(幂等)。 */
613
+ unsubscribeAll() {
614
+ this.unsubscribe?.();
615
+ this.unsubscribe = null;
616
+ }
617
+ };
593
618
  // src/kurobot/translate.ts
594
619
  /**
595
620
  * 消息映射纯函数(任务书 KUROBOT-PROMPT §1.2 QQ↔MC 映射;翻译层无副作用,ADR-008)
@@ -664,141 +689,93 @@ function renderCommandResult(body) {
664
689
  if (output.length === 0) return "(命令执行成功,无输出)";
665
690
  return output.join("\n");
666
691
  }
667
- // src/kurobot/adapter.ts
692
+ // src/kurobot/inbound/message.ts
668
693
  /**
669
- * NapukettoKurobotAdapter:kurobridge-ws 协议适配器(KuroBot MVP-3,2026-09-13)
694
+ * kurobot inbound 消息域(2026-11-14 自 event-pipeline.ts 拆出)
670
695
  *
671
- * - 出链路:订阅 kernel 消息事件通道 → 仅群聊 → 富文本降级 → 出站绑定过滤 →
672
- * kurobridge-ws chat 帧;命令前缀消息 → command 帧(source 由本侧提取)。
673
- * - 入链路:KurobotConnection 收服务端帧 → chat/join/leave/death 渲染模板 →
674
- * kernel 群消息 API 纯文本发送;status 仅缓存;command_result 经 pending 表
675
- * 关联回发群(帧无 channel 字段,靠请求 UUID 关联)。
676
- *
677
- * 生命周期走 BaseProtocolAdapter 骨架(start 校验配置 → onStart 建连接 + 订阅 →
678
- * onStop 清理);连接层状态机见 connection.ts。不发协议事件给 network 广播器
679
- * (kurobot 是纯客户端出向协议,无第三方接入门面)。
696
+ * kernel QQ 群消息 → kurobridge-ws 出站帧:仅群聊 + 自消息过滤 + 富文本降级 +
697
+ * 命令前缀分流(command 帧)+ 出站绑定过滤(chat 帧)。连接出口经 conn 面
698
+ * (未 established 时连接层直接丢弃)。富文本降级与模板渲染在 translate.ts。
680
699
  */
681
- /** kurobot 协议适配器。 */
682
- var NapukettoKurobotAdapter = class extends BaseProtocolAdapter {
683
- protocol = "kurobot";
684
- msgChannel;
685
- msgApi;
700
+ /** kurobot 消息域处理器(kernel 群消息 → 出站帧)。 */
701
+ var KurobotMessageHandler = class {
686
702
  selfUin;
703
+ currentConfig;
704
+ conn;
705
+ session;
687
706
  log;
688
- receiveFilter;
689
- connection = null;
690
- unsubscribe = null;
691
- /** 当前生效配置(收链路取 commandPrefix 等;onStart/onReload 刷新)。 */
692
- currentConfig = null;
693
- /** 出站绑定集(hello_ack 快照 + bindings_updated 全量替换;空集 = 全部不发)。 */
694
- bindings = /* @__PURE__ */ new Set();
695
- /** command 请求 UUID → 回发群号(command_result 帧无 channel,靠 id 关联)。 */
696
- pendingCommands = /* @__PURE__ */ new Map();
697
- /** query 请求 UUID → 查询类别。 */
698
- pendingQueries = /* @__PURE__ */ new Map();
699
- /** 最近一帧 status 快照(仅缓存不发群,M-04 决策同构)。 */
700
- lastStatus = null;
701
- constructor(opts) {
702
- super({
703
- config: opts.config,
704
- hooks: {
705
- onStart: (config) => this.startKurobot(config),
706
- onStop: () => this.stopAll(),
707
- onReload: (config) => this.reloadKurobot(config)
707
+ constructor(deps) {
708
+ this.selfUin = deps.selfUin;
709
+ this.currentConfig = deps.currentConfig;
710
+ this.conn = deps.conn;
711
+ this.session = deps.session;
712
+ this.log = deps.logger;
713
+ }
714
+ /** kernel 群消息 → kurobot 出站(仅群聊 + 自消息过滤 + 命令分流 + 绑定过滤)。 */
715
+ handle(msg) {
716
+ const config = this.currentConfig();
717
+ if (config === null) return;
718
+ if (msg.chatType !== ChatType.GROUP) return;
719
+ if (String(msg.senderUin) === this.selfUin) return;
720
+ const channel = msg.peerUid;
721
+ if (channel === "") return;
722
+ const text = degradeMessage(msg);
723
+ if (text === null) return;
724
+ if (text.startsWith(config.commandPrefix)) {
725
+ const command = text.slice(config.commandPrefix.length).trim();
726
+ if (command === "") return;
727
+ this.sendCommand(channel, command, String(msg.senderUin));
728
+ return;
729
+ }
730
+ if (!this.session.isBound(channel)) {
731
+ this.log?.debug({ channel }, "kurobot: 非绑定群,跳过 chat 帧");
732
+ return;
733
+ }
734
+ this.conn.send({
735
+ type: "chat",
736
+ body: {
737
+ channel,
738
+ sender: resolveSenderName(msg),
739
+ content: text
708
740
  }
709
741
  });
710
- this.msgChannel = opts.msgChannel;
711
- this.msgApi = opts.msgApi;
712
- this.selfUin = String(opts.self.uin);
713
- this.log = opts.logger;
714
- this.receiveFilter = opts.receiveFilter;
715
- }
716
- /** 最近一帧 status 快照(仅缓存;无帧时 null)。 */
717
- getLastStatus() {
718
- return this.lastStatus;
719
- }
720
- /** 连接是否已建立(hello_ack ok 后为 true)。 */
721
- isConnected() {
722
- return this.connection?.isEstablished() ?? false;
723
742
  }
724
- /** 发起 query 请求(status/bindings)。MVP 未接 QQ 触发面——预留能力(见 design.md §8)。 */
725
- query(kind) {
726
- const conn = this.connection;
727
- if (conn === null || !conn.isEstablished()) return false;
743
+ /** 发 command 请求帧(登记 pending 表供 command_result 关联)。 */
744
+ sendCommand(channel, command, userId) {
728
745
  const id = randomUUID();
729
- this.pendingQueries.set(id, kind);
730
- return conn.send({
731
- type: "query",
746
+ this.session.pendingCommands.set(id, channel);
747
+ if (this.conn.send({
748
+ type: "command",
732
749
  id,
733
- body: { kind }
734
- });
735
- }
736
- /** 启动:建连接(异步握手,日志可见)+ 订阅 kernel 消息。 */
737
- async startKurobot(config) {
738
- this.currentConfig = config;
739
- this.bindings = /* @__PURE__ */ new Set();
740
- this.pendingCommands.clear();
741
- this.pendingQueries.clear();
742
- this.lastStatus = null;
743
- const hello = {
744
- peerId: this.selfUin,
745
- platform: "qq",
746
- version: resolveAdapterVersion(),
747
- protocolVersion: PROTOCOL_VERSION,
748
- client: config.client ?? resolveDefaultClient()
749
- };
750
- if (config.token !== "") hello.token = config.token;
751
- const connOptions = {
752
- url: config.url,
753
- hello,
754
- pingIntervalMs: config.pingIntervalMs,
755
- helloTimeoutMs: 1e4,
756
- retryBaseDelayMs: 1e3,
757
- retryMaxDelayMs: 6e4,
758
- onEstablished: (ack) => this.onEstablished(ack),
759
- onFrame: (msg) => this.onServerFrame(msg),
760
- onEnded: (reason) => this.onEnded(reason)
761
- };
762
- if (this.log !== void 0) connOptions.logger = this.log;
763
- this.connection = new KurobotConnection(connOptions);
764
- this.connection.start();
765
- this.subscribe();
766
- }
767
- /** 配置热更新:停旧连接/退订 → 按新配置重建(对齐 satori reloadTransports)。 */
768
- async reloadKurobot(config) {
769
- await this.stopAll();
770
- await this.startKurobot(config);
771
- }
772
- /** 同步接收行过滤判定到 holder(绑定集变更后调用;私聊恒放行)。 */
773
- syncReceiveFilter() {
774
- if (this.receiveFilter === void 0) return;
775
- this.receiveFilter.current = (peerUin, isGroup) => !isGroup || this.bindings.has(peerUin);
776
- }
777
- /** 停止:退订 + 关连接 + 清 pending 表。 */
778
- async stopAll() {
779
- this.unsubscribeAll();
780
- this.connection?.stop();
781
- this.connection = null;
782
- this.pendingCommands.clear();
783
- this.pendingQueries.clear();
784
- if (this.receiveFilter !== void 0) this.receiveFilter.current = null;
785
- }
786
- /** 握手成功:绑定集整体替换为服务端快照 + 清上一连接的 pending 表。 */
787
- onEstablished(ack) {
788
- this.pendingCommands.clear();
789
- this.pendingQueries.clear();
790
- this.bindings = new Set(ack.channelBindings);
791
- this.syncReceiveFilter();
750
+ body: {
751
+ command,
752
+ source: {
753
+ channel,
754
+ userId
755
+ }
756
+ }
757
+ }) !== true) this.session.pendingCommands.delete(id);
792
758
  }
793
- /** 连接永久停止(1002/1008/hello_ack 拒绝):清 pending 与接收过滤(绑定集留给下次握手覆盖)。 */
794
- onEnded(reason) {
795
- this.pendingCommands.clear();
796
- this.pendingQueries.clear();
797
- if (this.receiveFilter !== void 0) this.receiveFilter.current = null;
798
- this.log?.error({ reason }, "kurobot: 连接终止");
759
+ };
760
+ // src/kurobot/inbound/server-frames.ts
761
+ /**
762
+ * kurobot inbound 服务端帧域:服务端帧 → 模板渲染回群文本、status 快照缓存、
763
+ * 绑定集维护(bindings_updated / query_result)、command_result 关联回发群。
764
+ * 共享状态在 session.ts。
765
+ */
766
+ var KurobotServerFrameHandler = class {
767
+ msgApi;
768
+ requireConfig;
769
+ session;
770
+ log;
771
+ constructor(deps) {
772
+ this.msgApi = deps.msgApi;
773
+ this.requireConfig = deps.requireConfig;
774
+ this.session = deps.session;
775
+ this.log = deps.logger;
799
776
  }
800
- /** 服务端帧分发(渲染回群 / 缓存 / 绑定集维护 / 命令结果关联回发)。 */
801
- onServerFrame(msg) {
777
+ /** 服务端帧分发(未知事件帧忽略,连接层已滤)。 */
778
+ handle(msg) {
802
779
  switch (msg.type) {
803
780
  case "pong": return;
804
781
  case "chat":
@@ -808,40 +785,103 @@ var NapukettoKurobotAdapter = class extends BaseProtocolAdapter {
808
785
  }));
809
786
  return;
810
787
  case "join":
811
- this.sendToGroup(msg.body.channel, renderTemplate(this.requireConfig().joinTemplate, { player: msg.body.playerName }));
812
- return;
813
- case "leave":
814
- this.sendToGroup(msg.body.channel, renderTemplate(this.requireConfig().leaveTemplate, { player: msg.body.playerName }));
788
+ case "leave": {
789
+ const config = this.requireConfig();
790
+ const template = msg.type === "join" ? config.joinTemplate : config.leaveTemplate;
791
+ this.sendToGroup(msg.body.channel, renderTemplate(template, { player: msg.body.playerName }));
815
792
  return;
793
+ }
816
794
  case "death":
817
795
  this.sendToGroup(msg.body.channel, truncateForGroup(renderDeathText(this.requireConfig().deathTemplate, msg.body.player, msg.body.message)));
818
796
  return;
819
797
  case "status":
820
- this.lastStatus = msg.body;
821
- this.log?.debug({ status: msg.body }, "kurobot: status 已缓存");
798
+ this.session.applyStatus(msg.body);
822
799
  return;
823
800
  case "bindings_updated":
824
- this.bindings = new Set(msg.body.channelBindings);
825
- this.syncReceiveFilter();
801
+ this.session.applyBindings(msg.body.channelBindings);
826
802
  this.log?.info({ bindings: msg.body.channelBindings }, "kurobot: 绑定表已更新");
827
803
  return;
828
804
  case "command_result": {
829
- const channel = this.pendingCommands.get(msg.id);
805
+ const channel = this.session.takeCommand(msg.id);
830
806
  if (channel === void 0) {
831
807
  this.log?.warn({ id: msg.id }, "kurobot: command_result 无对应请求,丢弃");
832
808
  return;
833
809
  }
834
- this.pendingCommands.delete(msg.id);
835
810
  this.sendToGroup(channel, truncateForGroup(renderCommandResult(msg.body)));
836
811
  return;
837
812
  }
838
813
  case "query_result":
839
- this.onQueryResult(msg.id, msg.body);
814
+ this.session.applyQueryResult(msg.id, msg.body);
840
815
  return;
841
816
  }
842
817
  }
818
+ /** 群文本发送(失败 error 日志不抛出——不影响连接与收链路)。 */
819
+ sendToGroup(channel, text) {
820
+ this.msgApi.sendMessage({
821
+ chatType: ChatType.GROUP,
822
+ peerUid: channel
823
+ }, [{
824
+ type: "text",
825
+ text
826
+ }]).then(() => {
827
+ this.log?.debug({ channel }, "kurobot: 群消息已发送");
828
+ }).catch((err) => {
829
+ this.log?.error({
830
+ channel,
831
+ err: err instanceof Error ? err.message : String(err)
832
+ }, "kurobot: 群消息发送失败");
833
+ });
834
+ }
835
+ };
836
+ // src/kurobot/session.ts
837
+ /** kurobot 会话共享状态(绑定集 / pending 表 / status 快照 / 接收过滤)。 */
838
+ var KurobotSession = class {
839
+ receiveFilter;
840
+ log;
841
+ /** 出站绑定集(空集 = 全部不发,对齐服务端空绑定丢弃语义)。 */
842
+ bindings = /* @__PURE__ */ new Set();
843
+ /** command 请求 UUID → 回发群号(command_result 帧无 channel,靠 id 关联)。 */
844
+ pendingCommands = /* @__PURE__ */ new Map();
845
+ /** query 请求 UUID → 查询类别。 */
846
+ pendingQueries = /* @__PURE__ */ new Map();
847
+ /** 最近一帧 status 快照(仅缓存不发群,M-04 决策同构)。 */
848
+ lastStatus = null;
849
+ constructor(deps) {
850
+ this.receiveFilter = deps.receiveFilter;
851
+ this.log = deps.logger;
852
+ }
853
+ /** 启动期重置:清绑定集与 pending 表、status 快照(对齐原 startKurobot 序)。 */
854
+ resetForStart() {
855
+ this.bindings = /* @__PURE__ */ new Set();
856
+ this.clearPending();
857
+ this.lastStatus = null;
858
+ }
859
+ /** 清 pending 表(重连握手 / 停止 / 终止时;跨连接的 UUID 不复用)。 */
860
+ clearPending() {
861
+ this.pendingCommands.clear();
862
+ this.pendingQueries.clear();
863
+ }
864
+ /** 清接收过滤(null = 不过滤;停止 / 连接终止时)。 */
865
+ clearReceiveFilter() {
866
+ if (this.receiveFilter !== void 0) this.receiveFilter.current = null;
867
+ }
868
+ /** 绑定集整体替换(hello_ack / bindings_updated / query_result)+ 同步接收过滤。 */
869
+ applyBindings(bindings) {
870
+ this.bindings = new Set(bindings);
871
+ this.syncReceiveFilter();
872
+ }
873
+ /** 出站绑定判定:空集 = 全部不发;非绑定群不发。 */
874
+ isBound(channel) {
875
+ return this.bindings.size > 0 && this.bindings.has(channel);
876
+ }
877
+ /** command_result 关联:取回发群号并出表(无对应请求 → undefined)。 */
878
+ takeCommand(id) {
879
+ const channel = this.pendingCommands.get(id);
880
+ if (channel !== void 0) this.pendingCommands.delete(id);
881
+ return channel;
882
+ }
843
883
  /** query_result 处理:bindings 刷新绑定集(防御性收窄);status 缓存。 */
844
- onQueryResult(id, body) {
884
+ applyQueryResult(id, body) {
845
885
  const kind = this.pendingQueries.get(id);
846
886
  if (kind === void 0) {
847
887
  this.log?.warn({ id }, "kurobot: query_result 无对应请求,丢弃");
@@ -856,8 +896,7 @@ var NapukettoKurobotAdapter = class extends BaseProtocolAdapter {
856
896
  return;
857
897
  }
858
898
  if (kind === "bindings" && isStringArray(body.data)) {
859
- this.bindings = new Set(body.data);
860
- this.syncReceiveFilter();
899
+ this.applyBindings(body.data);
861
900
  return;
862
901
  }
863
902
  this.log?.debug({
@@ -865,71 +904,141 @@ var NapukettoKurobotAdapter = class extends BaseProtocolAdapter {
865
904
  data: body.data
866
905
  }, "kurobot: 查询结果已记录");
867
906
  }
868
- /** kernel 群消息 → kurobot 出站(仅群聊 + 自消息过滤 + 命令分流 + 绑定过滤)。 */
869
- handleIncomingMessage(msg) {
870
- const config = this.currentConfig;
871
- if (config === null) return;
872
- if (msg.chatType !== ChatType.GROUP) return;
873
- if (String(msg.senderUin) === this.selfUin) return;
874
- const channel = msg.peerUid;
875
- if (channel === "") return;
876
- const text = degradeMessage(msg);
877
- if (text === null) return;
878
- if (text.startsWith(config.commandPrefix)) {
879
- const command = text.slice(config.commandPrefix.length).trim();
880
- if (command === "") return;
881
- this.sendCommand(channel, command, String(msg.senderUin));
882
- return;
883
- }
884
- if (this.bindings.size === 0 || !this.bindings.has(channel)) {
885
- this.log?.debug({ channel }, "kurobot: 非绑定群,跳过 chat 帧");
886
- return;
887
- }
888
- this.sendChat(channel, resolveSenderName(msg), text);
907
+ /** status 帧入账(仅缓存不发群,防刷屏)。 */
908
+ applyStatus(body) {
909
+ this.lastStatus = body;
910
+ this.log?.debug({ status: body }, "kurobot: status 已缓存");
889
911
  }
890
- /** 发 chat 事件帧(无 id)。 */
891
- sendChat(channel, sender, content) {
892
- this.connection?.send({
893
- type: "chat",
894
- body: {
895
- channel,
896
- sender,
897
- content
912
+ /** 同步接收行过滤判定到 holder(绑定集变更后调用;私聊恒放行)。 */
913
+ syncReceiveFilter() {
914
+ if (this.receiveFilter === void 0) return;
915
+ this.receiveFilter.current = (peerUin, isGroup) => !isGroup || this.bindings.has(peerUin);
916
+ }
917
+ };
918
+ /** string[] 防御性收窄(query_result bindings data)。 */
919
+ function isStringArray(value) {
920
+ return Array.isArray(value) && value.every((item) => typeof item === "string");
921
+ }
922
+ // src/kurobot/adapter.ts
923
+ /**
924
+ * NapukettoKurobotAdapter:kurobridge-ws 协议适配器(KuroBot MVP-3,2026-09-13)
925
+ *
926
+ * 出链路:kernel 群消息 → 降级 → 绑定过滤 → chat/command 帧;入链路:服务端帧
927
+ * → 模板渲染 → kernel 群文本发送。本类只做协调(构造装配 / 连接建立与启停 /
928
+ * 公共查询面):订阅接线在 event-pipeline.ts,域处理在 inbound/,共享状态在
929
+ * session.ts。纯客户端出向协议,不发协议事件给 network 广播器。
930
+ */
931
+ /** kurobot 协议适配器。 */
932
+ var NapukettoKurobotAdapter = class extends BaseProtocolAdapter {
933
+ protocol = "kurobot";
934
+ selfUin;
935
+ log;
936
+ /** 会话共享状态(绑定集 / pending 表 / status 快照 / 接收过滤)。 */
937
+ session;
938
+ /** QQ 消息 → 出站帧域处理(经 pipeline 订阅路由)。 */
939
+ message;
940
+ /** 服务端帧 → QQ 文本域处理(连接回调直连)。 */
941
+ frames;
942
+ pipeline;
943
+ connection = null;
944
+ /** 当前生效配置(收链路取 commandPrefix 等;onStart/onReload 刷新)。 */
945
+ currentConfig = null;
946
+ constructor(opts) {
947
+ super({
948
+ config: opts.config,
949
+ hooks: {
950
+ onStart: (config) => this.startKurobot(config),
951
+ onStop: () => this.stopAll(),
952
+ onReload: (config) => this.reloadKurobot(config)
898
953
  }
899
954
  });
955
+ this.selfUin = String(opts.self.uin);
956
+ this.log = opts.logger;
957
+ const conn = {
958
+ send: (frame) => this.connection?.send(frame) === true,
959
+ isEstablished: () => this.connection?.isEstablished() ?? false
960
+ };
961
+ this.session = new KurobotSession({
962
+ receiveFilter: opts.receiveFilter,
963
+ logger: opts.logger
964
+ });
965
+ this.message = new KurobotMessageHandler({
966
+ selfUin: this.selfUin,
967
+ currentConfig: () => this.currentConfig,
968
+ conn,
969
+ session: this.session,
970
+ logger: opts.logger
971
+ });
972
+ this.frames = new KurobotServerFrameHandler({
973
+ msgApi: opts.msgApi,
974
+ requireConfig: () => this.requireConfig(),
975
+ session: this.session,
976
+ logger: opts.logger
977
+ });
978
+ this.pipeline = new KurobotEventPipeline({
979
+ msgChannel: opts.msgChannel,
980
+ message: this.message
981
+ });
900
982
  }
901
- /** 发 command 请求帧(登记 pending 表供 command_result 关联)。 */
902
- sendCommand(channel, command, userId) {
983
+ /** 最近一帧 status 快照(仅缓存;无帧时 null)。 */
984
+ getLastStatus() {
985
+ return this.session.lastStatus;
986
+ }
987
+ /** 连接是否已建立(hello_ack ok 后为 true)。 */
988
+ isConnected() {
989
+ return this.connection?.isEstablished() ?? false;
990
+ }
991
+ /** 发起 query 请求(status/bindings)。MVP 未接 QQ 触发面——预留能力(见 design.md §8)。 */
992
+ query(kind) {
993
+ const conn = this.connection;
994
+ if (conn === null || !conn.isEstablished()) return false;
903
995
  const id = randomUUID();
904
- this.pendingCommands.set(id, channel);
905
- if (this.connection?.send({
906
- type: "command",
996
+ this.session.pendingQueries.set(id, kind);
997
+ return conn.send({
998
+ type: "query",
907
999
  id,
908
- body: {
909
- command,
910
- source: {
911
- channel,
912
- userId
913
- }
1000
+ body: { kind }
1001
+ });
1002
+ }
1003
+ /** 启动:建连接(异步握手,日志可见)+ 订阅 kernel 消息。 */
1004
+ async startKurobot(config) {
1005
+ this.currentConfig = config;
1006
+ this.session.resetForStart();
1007
+ const hello = {
1008
+ peerId: this.selfUin,
1009
+ platform: "qq",
1010
+ version: resolvePackageVersion() ?? "unknown",
1011
+ protocolVersion: PROTOCOL_VERSION,
1012
+ client: config.client ?? resolveDefaultClient()
1013
+ };
1014
+ if (config.token !== "") hello.token = config.token;
1015
+ const connOptions = {
1016
+ url: config.url,
1017
+ hello,
1018
+ pingIntervalMs: config.pingIntervalMs,
1019
+ helloTimeoutMs: 1e4,
1020
+ retryBaseDelayMs: 1e3,
1021
+ retryMaxDelayMs: 6e4,
1022
+ onEstablished: (ack) => {
1023
+ this.session.clearPending();
1024
+ this.session.applyBindings(ack.channelBindings);
1025
+ },
1026
+ onFrame: (msg) => this.frames.handle(msg),
1027
+ onEnded: (reason) => {
1028
+ this.session.clearPending();
1029
+ this.session.clearReceiveFilter();
1030
+ this.log?.error({ reason }, "kurobot: 连接终止");
914
1031
  }
915
- }) !== true) this.pendingCommands.delete(id);
1032
+ };
1033
+ if (this.log !== void 0) connOptions.logger = this.log;
1034
+ this.connection = new KurobotConnection(connOptions);
1035
+ this.connection.start();
1036
+ this.pipeline.subscribe();
916
1037
  }
917
- /** 群文本发送(失败 error 日志不抛出——不影响连接与收链路)。 */
918
- sendToGroup(channel, text) {
919
- this.msgApi.sendMessage({
920
- chatType: ChatType.GROUP,
921
- peerUid: channel
922
- }, [{
923
- type: "text",
924
- text
925
- }]).then(() => {
926
- this.log?.debug({ channel }, "kurobot: 群消息已发送");
927
- }).catch((err) => {
928
- this.log?.error({
929
- channel,
930
- err: err instanceof Error ? err.message : String(err)
931
- }, "kurobot: 群消息发送失败");
932
- });
1038
+ /** 配置热更新:停旧连接/退订 → 按新配置重建(对齐 satori reloadTransports)。 */
1039
+ async reloadKurobot(config) {
1040
+ await this.stopAll();
1041
+ await this.startKurobot(config);
933
1042
  }
934
1043
  /** 当前生效配置(收链路保证已 start)。 */
935
1044
  requireConfig() {
@@ -937,25 +1046,15 @@ var NapukettoKurobotAdapter = class extends BaseProtocolAdapter {
937
1046
  if (config === null) throw new Error("kurobot: 配置未初始化(适配器未启动)");
938
1047
  return config;
939
1048
  }
940
- /** 订阅 kernel 消息事件(幂等)。 */
941
- subscribe() {
942
- if (this.unsubscribe !== null) return;
943
- this.unsubscribe = this.msgChannel.on("Msg/onRecvMsg", (msgs) => {
944
- forEachRawMessage(msgs, (msg) => {
945
- this.handleIncomingMessage(msg);
946
- });
947
- });
948
- }
949
- /** 退订(幂等)。 */
950
- unsubscribeAll() {
951
- this.unsubscribe?.();
952
- this.unsubscribe = null;
1049
+ /** 停止:退订 + 清 pending 表与接收过滤 + 关连接。 */
1050
+ async stopAll() {
1051
+ this.pipeline.unsubscribeAll();
1052
+ this.session.clearPending();
1053
+ this.session.clearReceiveFilter();
1054
+ this.connection?.stop();
1055
+ this.connection = null;
953
1056
  }
954
1057
  };
955
- /** string[] 防御性收窄(query_result bindings data)。 */
956
- function isStringArray(value) {
957
- return Array.isArray(value) && value.every((item) => typeof item === "string");
958
- }
959
1058
  /** best-effort 读取 adapter 包版本(dist 与 src 双布局相对包根一致)。 */
960
1059
  function resolvePackageVersion() {
961
1060
  try {
@@ -966,10 +1065,6 @@ function resolvePackageVersion() {
966
1065
  return null;
967
1066
  }
968
1067
  }
969
- /** hello.version:adapter 包版本(取不到用 unknown)。 */
970
- function resolveAdapterVersion() {
971
- return resolvePackageVersion() ?? "unknown";
972
- }
973
1068
  /** hello.client 缺省身份:napukettoqq/<版本>(取不到版本则 napukettoqq)。 */
974
1069
  function resolveDefaultClient() {
975
1070
  const version = resolvePackageVersion();