chatluna-plugin-onebot-sandbox 1.2.4 → 1.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.js CHANGED
@@ -655,7 +655,7 @@ function validateSceneConversations(scene, scope) {
655
655
  async function withAudioPreview(content) {
656
656
  if (content.type !== "audio") return content;
657
657
  const buffer = Buffer.from(content.dataBase64, "base64");
658
- if (!isVoiceIn("silk", buffer, content.mimeType)) {
658
+ if (!await isVoiceIn("silk", buffer, content.mimeType)) {
659
659
  if (content.mimeType === "audio/silk" || content.mimeType === "audio/x-silk") throw new SandboxDomainError("SILK 语音内容无效");
660
660
  return content;
661
661
  }
@@ -3020,7 +3020,7 @@ var SandboxBot = class extends Bot {
3020
3020
  conversationId,
3021
3021
  content: message.content,
3022
3022
  replyToMessageId,
3023
- media: this.control.storeMediaBatch(mediaInputs)
3023
+ media: await this.control.storeMediaBatch(mediaInputs)
3024
3024
  }) : await this.control.sendMessage({
3025
3025
  operatorId: this.selfId,
3026
3026
  conversationId,
@@ -3536,7 +3536,7 @@ var SandboxBot = class extends Bot {
3536
3536
  continue;
3537
3537
  }
3538
3538
  const input = await this.resolveOutboundMedia(source);
3539
- media.push(...this.control.storeMediaBatch([input]));
3539
+ media.push(...await this.control.storeMediaBatch([input]));
3540
3540
  }
3541
3541
  return media;
3542
3542
  }
@@ -4055,7 +4055,7 @@ var SandboxChatLunaStateStore = class {
4055
4055
  };
4056
4056
  // external/onebot-sandbox/src/chatluna/user-permissions.ts
4057
4057
  /**
4058
- * 工具门禁按 actor/speakers 重建身份,不读取入站 Session 的 user 行。用户等级因此注册成权限服务的
4058
+ * 工具门禁按 principal/actor 重建身份,不读取入站 Session 的 user 行。用户等级因此注册成权限服务的
4059
4059
  * 临时规则,只命中当前场景正在运行的沙盒机器人;不写平台用户表或全局 setLevel,避免影响真实账号。
4060
4060
  * 子作用域随 permissions 重载,场景与用户设置不跟着重建。等级未设置时完全沿用宿主权限。
4061
4061
  */
@@ -4590,7 +4590,18 @@ var SandboxMediaStorage = class {
4590
4590
  isPinned(id) {
4591
4591
  return this.pinned.has(id);
4592
4592
  }
4593
- save(input) {
4593
+ async save(input) {
4594
+ const { media, content } = this.prepare(input);
4595
+ if (media.type === "audio" && await isVoiceIn("silk", content, media.mimeType)) media.mimeType = "audio/silk";
4596
+ return this.persist(media, content);
4597
+ }
4598
+ /** 头像只收图片,不经异步的 SILK 识别,场景替换与恢复因此能同步补齐头像。 */
4599
+ saveImage(input) {
4600
+ const { media, content } = this.prepare(input);
4601
+ if (media.type !== "image") throw new SandboxDomainError(`媒体必须是图片:${media.mimeType}`);
4602
+ return this.persist(media, content);
4603
+ }
4604
+ prepare(input) {
4594
4605
  const mimeType = input.mimeType.trim().toLowerCase();
4595
4606
  const type = MEDIA_TYPES.get(mimeType);
4596
4607
  if (!type) throw new SandboxDomainError(`不支持的媒体类型:${input.mimeType || "未知"}`);
@@ -4604,14 +4615,20 @@ var SandboxMediaStorage = class {
4604
4615
  if (!content.length) throw new SandboxDomainError("媒体内容不能为空");
4605
4616
  assertMediaSize(content.length, this.maxSize);
4606
4617
  const id = createHash("sha256").update(content).digest("hex").slice(0, 32);
4607
- const media = {
4608
- id,
4609
- type,
4610
- name,
4611
- mimeType: type === "audio" && isVoiceIn("silk", content, mimeType) ? "audio/silk" : mimeType,
4612
- size: content.length,
4613
- reference: `sandbox-media://${id}`
4618
+ return {
4619
+ media: {
4620
+ id,
4621
+ type,
4622
+ name,
4623
+ mimeType,
4624
+ size: content.length,
4625
+ reference: `sandbox-media://${id}`
4626
+ },
4627
+ content
4614
4628
  };
4629
+ }
4630
+ persist(media, content) {
4631
+ const { id } = media;
4615
4632
  if (this.exists(id)) {
4616
4633
  const { dataBase64: _ignored, ...existing } = this.readById(id);
4617
4634
  return existing;
@@ -5333,8 +5350,8 @@ var SandboxControlService = class {
5333
5350
  get mediaMaxSize() {
5334
5351
  return this.mediaStorage.maxSize;
5335
5352
  }
5336
- storeMedia(input) {
5337
- const media = this.mediaStorage.save(input);
5353
+ async storeMedia(input) {
5354
+ const media = await this.mediaStorage.save(input);
5338
5355
  this.mediaStorage.pin(media.id);
5339
5356
  return media;
5340
5357
  }
@@ -5377,7 +5394,7 @@ var SandboxControlService = class {
5377
5394
  }
5378
5395
  saveAvatar(fileName, mimeType, dataBase64) {
5379
5396
  if (!mimeType.startsWith("image/")) throw new SandboxDomainError(`头像必须是图片:${mimeType || "未知类型"}`);
5380
- return this.mediaStorage.save({
5397
+ return this.mediaStorage.saveImage({
5381
5398
  fileName,
5382
5399
  mimeType,
5383
5400
  dataBase64
@@ -5400,7 +5417,7 @@ var SandboxControlService = class {
5400
5417
  if (!avatar) return;
5401
5418
  const current = avatar.reference;
5402
5419
  if (this.hasManagedAvatar(current)) return current;
5403
- const restored = this.mediaStorage.save({
5420
+ const restored = this.mediaStorage.saveImage({
5404
5421
  fileName: `builtin-${avatar.kind}-${avatar.id}.png`,
5405
5422
  mimeType: "image/png",
5406
5423
  dataBase64: avatar.png
@@ -5435,10 +5452,10 @@ var SandboxControlService = class {
5435
5452
  if (value.startsWith("base64://")) return this.saveAvatar(`${kind}-${entityId}-avatar.png`, "image/png", value.slice(9));
5436
5453
  throw new SandboxDomainError("外部头像 URL 需要先通过 importAvatar 导入受管媒体");
5437
5454
  }
5438
- storeMediaBatch(inputs) {
5455
+ async storeMediaBatch(inputs) {
5439
5456
  const media = [];
5440
5457
  try {
5441
- for (const input of inputs) media.push(this.storeMedia(input));
5458
+ for (const input of inputs) media.push(await this.storeMedia(input));
5442
5459
  return media;
5443
5460
  } catch (error) {
5444
5461
  for (const item of media) this.mediaStorage.remove(item);
@@ -6597,19 +6614,21 @@ var SandboxControlService = class {
6597
6614
  });
6598
6615
  }
6599
6616
  async sendMediaMessage(input) {
6600
- const { result, delivery } = this.startMediaMessageSend(input);
6617
+ const { result, delivery } = await this.startMediaMessageSend(input);
6601
6618
  await delivery;
6602
6619
  return {
6603
6620
  ...result,
6604
6621
  revision: this.scene.revision
6605
6622
  };
6606
6623
  }
6607
- startMediaMessageSend(input) {
6624
+ async startMediaMessageSend(input) {
6608
6625
  if (!input.media.length) throw new SandboxDomainError("至少需要一个媒体文件");
6609
- const context = this.getMessageContext(input);
6626
+ this.getMessageContext(input);
6610
6627
  const media = [];
6628
+ let context;
6611
6629
  try {
6612
- for (const file of input.media) media.push(this.storeMedia(file));
6630
+ for (const file of input.media) media.push(await this.storeMedia(file));
6631
+ context = this.getMessageContext(input);
6613
6632
  } catch (error) {
6614
6633
  for (const saved of media) this.mediaStorage.remove(saved);
6615
6634
  throw error;
@@ -8075,7 +8094,7 @@ function registerConsole(console, control, appearance, endpoint, testSpaces, pre
8075
8094
  });
8076
8095
  });
8077
8096
  registerListener("onebot-sandbox.send-media-message", async (input) => {
8078
- const { delivery } = (await resolveReadyControl(input, true)).startMediaMessageSend(assertInteractionInput(withoutSpaceId(input)));
8097
+ const { delivery } = await (await resolveReadyControl(input, true)).startMediaMessageSend(assertInteractionInput(withoutSpaceId(input)));
8079
8098
  delivery.catch(() => {});
8080
8099
  return getWorkspace({
8081
8100
  spaceId: input.spaceId,
@@ -11161,11 +11180,11 @@ function setBotPreset(runtime, args) {
11161
11180
  const presetId = args.presetId === void 0 ? void 0 : requireString(args.presetId, "presetId");
11162
11181
  return withChatLuna(() => runtime.presets.assign(botId, presetId), "preset_not_found");
11163
11182
  }
11164
- function uploadMedia(runtime, args) {
11183
+ async function uploadMedia(runtime, args) {
11165
11184
  const dataBase64 = requireString(args.dataBase64, "dataBase64");
11166
11185
  const digest = createHash("sha256").update(Buffer.from(dataBase64, "base64")).digest("hex");
11167
11186
  if (args.sha256 !== void 0 && args.sha256 !== digest) throw new SandboxMcpError("digest_mismatch", "媒体摘要不匹配");
11168
- const media = runtime.control.storeMedia({
11187
+ const media = await runtime.control.storeMedia({
11169
11188
  fileName: requireString(args.fileName, "fileName"),
11170
11189
  mimeType: requireString(args.mimeType, "mimeType"),
11171
11190
  dataBase64