chatluna-plugin-onebot-sandbox 1.2.2 → 1.2.4

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/lib/index.js CHANGED
@@ -722,6 +722,57 @@ function toOneBotRawMessage(segments) {
722
722
  return `[CQ:${type}${attributes ? `,${attributes}` : ""}]`;
723
723
  }).join("");
724
724
  }
725
+ /** adapter-onebot 把带 `forward` 属性的 `<message>` 与 `<figure>` 发成合并转发(`send_*_forward_msg`)。 */
726
+ function isOneBotForwardElement(element) {
727
+ return element.type === "message" && "forward" in element.attrs || element.type === "figure";
728
+ }
729
+ /**
730
+ * 按 adapter-onebot 的 MessageEncoder 把转发元素编成 node 列表,连同它的行为一起复刻,插件在沙盒里看到的就是真实效果:
731
+ * 作者与引用 ID 记在整个转发上而不是单个节点上,所以前一个节点的 `<author>` 会沿用到后面没写作者的节点,
732
+ * 出现过 `<message id>` 之后的节点全部按引用节点发出;子 `<message>` 的属性只有 `time` 生效,
733
+ * 子 `<message>` 之外的内容合成一个节点,作者缺省为机器人;只有空白的节点不发。
734
+ */
735
+ function toOneBotForwardNodes(element, bot) {
736
+ const nodes = [];
737
+ const author = {};
738
+ let segments = [];
739
+ const flush = () => {
740
+ const first = segments[0];
741
+ if (first?.type === "text") first.data.text = first.data.text.trimStart();
742
+ const last = segments.at(-1);
743
+ if (last?.type === "text") last.data.text = last.data.text.trimEnd();
744
+ segments = segments.filter((segment) => segment.type !== "text" || segment.data.text);
745
+ if (!segments.length && author.messageId === void 0) return;
746
+ nodes.push(author.messageId !== void 0 ? {
747
+ type: "node",
748
+ data: { id: author.messageId }
749
+ } : {
750
+ type: "node",
751
+ data: {
752
+ name: String(author.name || bot.name),
753
+ uin: String(author.id || bot.id),
754
+ content: segments,
755
+ time: String(Math.floor((Number(author.time) || Date.now()) / 1e3))
756
+ }
757
+ });
758
+ segments = [];
759
+ };
760
+ const render = (children) => {
761
+ for (const child of children) if (child.type === "author") Object.assign(author, child.attrs);
762
+ else if (child.type === "message") {
763
+ flush();
764
+ if ("id" in child.attrs) author.messageId = String(child.attrs.id);
765
+ else {
766
+ if ("time" in child.attrs) author.time = child.attrs.time;
767
+ render(child.children);
768
+ flush();
769
+ }
770
+ } else segments.push(...toOneBotElementSegments(child));
771
+ };
772
+ render(element.children);
773
+ flush();
774
+ return nodes;
775
+ }
725
776
  function appendKoishiElement(result, element) {
726
777
  if (element.type === "text") {
727
778
  result.content += String(element.attrs.content ?? "");
@@ -2873,16 +2924,59 @@ var SandboxBot = class extends Bot {
2873
2924
  type: resolveConversation(this.control.getSnapshot(), conversationId)?.type === "group" ? 0 : 1
2874
2925
  }
2875
2926
  })).transform(h.normalize(fragment));
2876
- const message = parseKoishiOutboundMessage(transformed);
2927
+ const messageIds = [];
2928
+ let pending = [];
2929
+ for (const element of transformed) {
2930
+ if (!isOneBotForwardElement(element)) {
2931
+ pending.push(element);
2932
+ continue;
2933
+ }
2934
+ messageIds.push(...await this.sendOutboundElements(channelId, conversationId, pending));
2935
+ pending = [];
2936
+ messageIds.push(await this.sendOutboundForward(channelId, conversationId, element));
2937
+ }
2938
+ messageIds.push(...await this.sendOutboundElements(channelId, conversationId, pending));
2939
+ return messageIds;
2940
+ }
2941
+ async sendOutboundElements(channelId, conversationId, elements) {
2942
+ const message = parseKoishiOutboundMessage(elements);
2877
2943
  if (!message.content && !message.mediaSources.length) return [];
2878
- const action = resolveConversation(this.control.getSnapshot(), conversationId)?.type === "group" ? "send_group_msg" : "send_private_msg";
2944
+ const group = resolveConversation(this.control.getSnapshot(), conversationId)?.type === "group";
2879
2945
  const payload = {
2880
2946
  channel_id: channelId,
2881
- message: transformed.join("")
2947
+ message: elements.join("")
2882
2948
  };
2949
+ return [await this.recordOutbound(conversationId, group ? "send_group_msg" : "send_private_msg", payload, () => this.deliverOutboundMessage(conversationId, message))];
2950
+ }
2951
+ /** 节点按 adapter-onebot 编码后走 send_forward_msg 同一套解析与能力表,实现禁用合并转发时照样报错。 */
2952
+ async sendOutboundForward(channelId, conversationId, element) {
2953
+ const action = resolveConversation(this.control.getSnapshot(), conversationId)?.type === "group" ? "send_group_forward_msg" : "send_private_forward_msg";
2954
+ const messages = toOneBotForwardNodes(element, {
2955
+ id: this.selfId,
2956
+ name: this.user?.name || this.selfId
2957
+ });
2958
+ return this.recordOutbound(conversationId, action, {
2959
+ channel_id: channelId,
2960
+ messages
2961
+ }, async () => {
2962
+ resolveOneBotAction(this.implementation, this.disabledCapabilities, action);
2963
+ this.getVisibleConversation(conversationId);
2964
+ const nodes = await this.parseOneBotForwardNodes(messages);
2965
+ return (await this.control.applyForwardMessage({
2966
+ operatorId: this.selfId,
2967
+ conversationId,
2968
+ nodes
2969
+ })).messageId;
2970
+ });
2971
+ }
2972
+ /**
2973
+ * ChatLuna 与 Koishi 插件经 Satori sendMessage 发消息,不经过 action 层;真实适配器在这里会发出
2974
+ * send_group_msg / send_private_msg(转发是 send_*_forward_msg),调试页按这一形态补记,否则机器人的回复在调试记录里不可见。
2975
+ */
2976
+ async recordOutbound(conversationId, action, payload, deliver) {
2883
2977
  const startedAt = Date.now();
2884
2978
  try {
2885
- const messageId = await this.deliverOutboundMessage(conversationId, message);
2979
+ const messageId = await deliver();
2886
2980
  const conversationObservation = this.observeConversation({ conversationId });
2887
2981
  this.control.recordOneBotDebug({
2888
2982
  botId: this.selfId,
@@ -2900,7 +2994,7 @@ var SandboxBot = class extends Bot {
2900
2994
  },
2901
2995
  ...conversationObservation ? { conversationObservation } : {}
2902
2996
  });
2903
- return [getSatoriMessageId(this.implementation, messageId)];
2997
+ return getSatoriMessageId(this.implementation, messageId);
2904
2998
  } catch (error) {
2905
2999
  this.control.recordOneBotDebug({
2906
3000
  botId: this.selfId,
@@ -7567,6 +7661,12 @@ var SandboxTestSpaceService = class {
7567
7661
  if (persistence) this.queuePersistenceTask(() => persistence.delete(spaceId));
7568
7662
  this.syncOccupation();
7569
7663
  }
7664
+ /** WebUI 总览的「清空全部」:逐个走 deleteSpace,主环境不在其中。返回删除的空间数。 */
7665
+ deleteAllSpaces() {
7666
+ const ids = [...this.spaces.keys()];
7667
+ for (const id of ids) this.deleteSpace(id);
7668
+ return ids.length;
7669
+ }
7570
7670
  setStatus(spaceId, status) {
7571
7671
  const space = this.requireSpace(spaceId);
7572
7672
  if (space.status === "completed" || space.status === "failed") throw new SandboxDomainError("请先重新激活已结束空间");
@@ -8172,6 +8272,7 @@ function registerConsole(console, control, appearance, endpoint, testSpaces, pre
8172
8272
  registerListener("onebot-sandbox.terminate-test-space", ({ spaceId }) => testSpaces.terminateSpace(spaceId));
8173
8273
  registerListener("onebot-sandbox.reactivate-test-space", ({ spaceId }) => testSpaces.reactivateSpace(spaceId));
8174
8274
  registerListener("onebot-sandbox.delete-test-space", ({ spaceId }) => testSpaces.deleteSpace(spaceId));
8275
+ registerListener("onebot-sandbox.delete-all-test-spaces", () => ({ deleted: testSpaces.deleteAllSpaces() }));
8175
8276
  }
8176
8277
  }
8177
8278
  // external/onebot-sandbox/src/settings.ts