koishi-plugin-chatluna 1.3.30 → 1.3.32

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.
@@ -46,7 +46,7 @@ export declare class ChainMiddleware {
46
46
  constructor(name: string, execute: ChainMiddlewareFunction, graph: ChatChainDependencyGraph);
47
47
  before<T extends keyof ChainMiddlewareName>(name: T): this;
48
48
  after<T extends keyof ChainMiddlewareName>(name: T): this;
49
- run(session: Session, options: ChainMiddlewareContext): Promise<string | h[] | h[][] | ChainMiddlewareRunStatus>;
49
+ run(session: Session, options: ChainMiddlewareContext): Promise<string | ChainMiddlewareRunStatus | h[] | h[][]>;
50
50
  }
51
51
  export interface ChainMiddlewareContext {
52
52
  config: Config;
@@ -582,7 +582,7 @@ var DefaultChatChainSender = class {
582
582
  }
583
583
  async send(session, messages) {
584
584
  if (!messages?.length) return;
585
- if (isElementArray(messages?.[0]) && messages[0][1].type === "markdown-qq") {
585
+ if (isElementArray(messages?.[0]) && messages[0][1]?.type === "markdown-qq") {
586
586
  await this.sendAsQQMarkdown(session, messages[0][0]);
587
587
  return;
588
588
  }
@@ -594,7 +594,7 @@ var DefaultChatChainSender = class {
594
594
  }
595
595
  async sendAsQQMarkdown(session, message) {
596
596
  const { user } = session.event;
597
- session.bot.internal.sendPrivateMessage(user.id, {
597
+ await session.bot.internal.sendPrivateMessage(user.id, {
598
598
  msg_type: 2,
599
599
  msg_seq: 1,
600
600
  msg_id: session.messageId,
@@ -537,7 +537,7 @@ var DefaultChatChainSender = class {
537
537
  }
538
538
  async send(session, messages) {
539
539
  if (!messages?.length) return;
540
- if (isElementArray(messages?.[0]) && messages[0][1].type === "markdown-qq") {
540
+ if (isElementArray(messages?.[0]) && messages[0][1]?.type === "markdown-qq") {
541
541
  await this.sendAsQQMarkdown(session, messages[0][0]);
542
542
  return;
543
543
  }
@@ -549,7 +549,7 @@ var DefaultChatChainSender = class {
549
549
  }
550
550
  async sendAsQQMarkdown(session, message) {
551
551
  const { user } = session.event;
552
- session.bot.internal.sendPrivateMessage(user.id, {
552
+ await session.bot.internal.sendPrivateMessage(user.id, {
553
553
  msg_type: 2,
554
554
  msg_seq: 1,
555
555
  msg_id: session.messageId,
package/lib/index.cjs CHANGED
@@ -3551,6 +3551,9 @@ function apply26(ctx, config, chain) {
3551
3551
  context.options.inputMessage = transformedMessage;
3552
3552
  return 2 /* CONTINUE */;
3553
3553
  }).after("resolve_room");
3554
+ ctx.chatluna.messageTransformer.before(async (session, elements) => {
3555
+ appendQQAttachments(session, elements);
3556
+ });
3554
3557
  ctx.chatluna.messageTransformer.intercept(
3555
3558
  "text",
3556
3559
  async (session, element, message) => {
@@ -4054,6 +4057,60 @@ function trackForwardId(element, message) {
4054
4057
  }
4055
4058
  }
4056
4059
  __name(trackForwardId, "trackForwardId");
4060
+ function appendQQAttachments(session, elements) {
4061
+ if (session.platform !== "qq") {
4062
+ return;
4063
+ }
4064
+ const qq = session.qq;
4065
+ const attachments = qq?.["d"]?.attachments;
4066
+ if (!attachments?.length) {
4067
+ return;
4068
+ }
4069
+ for (const attachment of attachments) {
4070
+ const type = attachment.content_type;
4071
+ const src = attachment.url;
4072
+ const exists = elements.some(
4073
+ (element) => element.attrs.src === src || element.attrs.url === src
4074
+ );
4075
+ if (exists) {
4076
+ continue;
4077
+ }
4078
+ if (type === "file") {
4079
+ elements.push(
4080
+ import_koishi7.h.file(src, {
4081
+ filename: attachment.filename
4082
+ })
4083
+ );
4084
+ } else if (type.startsWith("audio/")) {
4085
+ elements.push(
4086
+ import_koishi7.h.audio(src, {
4087
+ filename: attachment.filename,
4088
+ type,
4089
+ chatluna_file_url: src
4090
+ })
4091
+ );
4092
+ } else if (type === "voice") {
4093
+ elements.push(
4094
+ import_koishi7.h.audio(src, {
4095
+ filename: attachment.filename,
4096
+ type,
4097
+ chatluna_file_url: src
4098
+ })
4099
+ );
4100
+ } else if (type.startsWith("video/")) {
4101
+ elements.push(
4102
+ import_koishi7.h.video(src, {
4103
+ filename: attachment.filename,
4104
+ width: attachment.width,
4105
+ height: attachment.height,
4106
+ type,
4107
+ chatluna_file_url: src
4108
+ })
4109
+ );
4110
+ }
4111
+ }
4112
+ }
4113
+ __name(appendQQAttachments, "appendQQAttachments");
4057
4114
 
4058
4115
  // src/middlewares/chat/render_message.ts
4059
4116
  function apply27(ctx, config, chain) {
package/lib/index.mjs CHANGED
@@ -3557,6 +3557,9 @@ function apply26(ctx, config, chain) {
3557
3557
  context.options.inputMessage = transformedMessage;
3558
3558
  return 2 /* CONTINUE */;
3559
3559
  }).after("resolve_room");
3560
+ ctx.chatluna.messageTransformer.before(async (session, elements) => {
3561
+ appendQQAttachments(session, elements);
3562
+ });
3560
3563
  ctx.chatluna.messageTransformer.intercept(
3561
3564
  "text",
3562
3565
  async (session, element, message) => {
@@ -4060,6 +4063,60 @@ function trackForwardId(element, message) {
4060
4063
  }
4061
4064
  }
4062
4065
  __name(trackForwardId, "trackForwardId");
4066
+ function appendQQAttachments(session, elements) {
4067
+ if (session.platform !== "qq") {
4068
+ return;
4069
+ }
4070
+ const qq = session.qq;
4071
+ const attachments = qq?.["d"]?.attachments;
4072
+ if (!attachments?.length) {
4073
+ return;
4074
+ }
4075
+ for (const attachment of attachments) {
4076
+ const type = attachment.content_type;
4077
+ const src = attachment.url;
4078
+ const exists = elements.some(
4079
+ (element) => element.attrs.src === src || element.attrs.url === src
4080
+ );
4081
+ if (exists) {
4082
+ continue;
4083
+ }
4084
+ if (type === "file") {
4085
+ elements.push(
4086
+ h4.file(src, {
4087
+ filename: attachment.filename
4088
+ })
4089
+ );
4090
+ } else if (type.startsWith("audio/")) {
4091
+ elements.push(
4092
+ h4.audio(src, {
4093
+ filename: attachment.filename,
4094
+ type,
4095
+ chatluna_file_url: src
4096
+ })
4097
+ );
4098
+ } else if (type === "voice") {
4099
+ elements.push(
4100
+ h4.audio(src, {
4101
+ filename: attachment.filename,
4102
+ type,
4103
+ chatluna_file_url: src
4104
+ })
4105
+ );
4106
+ } else if (type.startsWith("video/")) {
4107
+ elements.push(
4108
+ h4.video(src, {
4109
+ filename: attachment.filename,
4110
+ width: attachment.width,
4111
+ height: attachment.height,
4112
+ type,
4113
+ chatluna_file_url: src
4114
+ })
4115
+ );
4116
+ }
4117
+ }
4118
+ }
4119
+ __name(appendQQAttachments, "appendQQAttachments");
4063
4120
 
4064
4121
  // src/middlewares/chat/render_message.ts
4065
4122
  function apply27(ctx, config, chain) {
@@ -718,7 +718,7 @@ var DefaultChatChainSender = class {
718
718
  }
719
719
  async send(session, messages) {
720
720
  if (!messages?.length) return;
721
- if (isElementArray(messages?.[0]) && messages[0][1].type === "markdown-qq") {
721
+ if (isElementArray(messages?.[0]) && messages[0][1]?.type === "markdown-qq") {
722
722
  await this.sendAsQQMarkdown(session, messages[0][0]);
723
723
  return;
724
724
  }
@@ -730,7 +730,7 @@ var DefaultChatChainSender = class {
730
730
  }
731
731
  async sendAsQQMarkdown(session, message) {
732
732
  const { user } = session.event;
733
- session.bot.internal.sendPrivateMessage(user.id, {
733
+ await session.bot.internal.sendPrivateMessage(user.id, {
734
734
  msg_type: 2,
735
735
  msg_seq: 1,
736
736
  msg_id: session.messageId,
@@ -836,6 +836,7 @@ var MessageTransformer = class {
836
836
  static {
837
837
  __name(this, "MessageTransformer");
838
838
  }
839
+ _beforeTransformFunctions = [];
839
840
  _transformFunctions = /* @__PURE__ */ new Map();
840
841
  async transform(session, elements, model, message = {
841
842
  content: "",
@@ -845,6 +846,13 @@ var MessageTransformer = class {
845
846
  quote: false,
846
847
  includeQuoteReply: true
847
848
  }) {
849
+ await this._runBeforeTransform(
850
+ session,
851
+ elements,
852
+ message,
853
+ model,
854
+ options
855
+ );
848
856
  const sourceElementString = elements.map((h2) => h2.toString(true)).join();
849
857
  const quoteElementString = ((session.quote && session.quote.elements) ?? []).map((h2) => h2.toString(true)).join();
850
858
  for (const element of elements) {
@@ -912,6 +920,27 @@ User's message: ${currentText}`;
912
920
  }
913
921
  return message;
914
922
  }
923
+ before(transformFunction, priority = 0) {
924
+ const wrapper = {
925
+ func: transformFunction,
926
+ priority
927
+ };
928
+ const insertIndex = this._beforeTransformFunctions.findIndex(
929
+ (item) => item.priority > priority
930
+ );
931
+ if (insertIndex === -1) {
932
+ this._beforeTransformFunctions.push(wrapper);
933
+ } else {
934
+ this._beforeTransformFunctions.splice(insertIndex, 0, wrapper);
935
+ }
936
+ return () => {
937
+ const index = this._beforeTransformFunctions.findIndex(
938
+ (item) => item.func === transformFunction
939
+ );
940
+ if (index === -1) return;
941
+ this._beforeTransformFunctions.splice(index, 1);
942
+ };
943
+ }
915
944
  intercept(type, transformFunction, priority = 0) {
916
945
  const functions = this._transformFunctions.get(type);
917
946
  if (type === "text" && functions?.length) {
@@ -1021,6 +1050,11 @@ User's message: ${currentText}`;
1021
1050
  });
1022
1051
  }
1023
1052
  }
1053
+ async _runBeforeTransform(session, elements, message, model, options) {
1054
+ for (const { func: transformFunction } of this._beforeTransformFunctions) {
1055
+ await transformFunction(session, elements, message, model, options);
1056
+ }
1057
+ }
1024
1058
  };
1025
1059
 
1026
1060
  // src/services/chat.ts
@@ -714,7 +714,7 @@ var DefaultChatChainSender = class {
714
714
  }
715
715
  async send(session, messages) {
716
716
  if (!messages?.length) return;
717
- if (isElementArray(messages?.[0]) && messages[0][1].type === "markdown-qq") {
717
+ if (isElementArray(messages?.[0]) && messages[0][1]?.type === "markdown-qq") {
718
718
  await this.sendAsQQMarkdown(session, messages[0][0]);
719
719
  return;
720
720
  }
@@ -726,7 +726,7 @@ var DefaultChatChainSender = class {
726
726
  }
727
727
  async sendAsQQMarkdown(session, message) {
728
728
  const { user } = session.event;
729
- session.bot.internal.sendPrivateMessage(user.id, {
729
+ await session.bot.internal.sendPrivateMessage(user.id, {
730
730
  msg_type: 2,
731
731
  msg_seq: 1,
732
732
  msg_id: session.messageId,
@@ -848,6 +848,7 @@ var MessageTransformer = class {
848
848
  static {
849
849
  __name(this, "MessageTransformer");
850
850
  }
851
+ _beforeTransformFunctions = [];
851
852
  _transformFunctions = /* @__PURE__ */ new Map();
852
853
  async transform(session, elements, model, message = {
853
854
  content: "",
@@ -857,6 +858,13 @@ var MessageTransformer = class {
857
858
  quote: false,
858
859
  includeQuoteReply: true
859
860
  }) {
861
+ await this._runBeforeTransform(
862
+ session,
863
+ elements,
864
+ message,
865
+ model,
866
+ options
867
+ );
860
868
  const sourceElementString = elements.map((h2) => h2.toString(true)).join();
861
869
  const quoteElementString = ((session.quote && session.quote.elements) ?? []).map((h2) => h2.toString(true)).join();
862
870
  for (const element of elements) {
@@ -924,6 +932,27 @@ User's message: ${currentText}`;
924
932
  }
925
933
  return message;
926
934
  }
935
+ before(transformFunction, priority = 0) {
936
+ const wrapper = {
937
+ func: transformFunction,
938
+ priority
939
+ };
940
+ const insertIndex = this._beforeTransformFunctions.findIndex(
941
+ (item) => item.priority > priority
942
+ );
943
+ if (insertIndex === -1) {
944
+ this._beforeTransformFunctions.push(wrapper);
945
+ } else {
946
+ this._beforeTransformFunctions.splice(insertIndex, 0, wrapper);
947
+ }
948
+ return () => {
949
+ const index = this._beforeTransformFunctions.findIndex(
950
+ (item) => item.func === transformFunction
951
+ );
952
+ if (index === -1) return;
953
+ this._beforeTransformFunctions.splice(index, 1);
954
+ };
955
+ }
927
956
  intercept(type, transformFunction, priority = 0) {
928
957
  const functions = this._transformFunctions.get(type);
929
958
  if (type === "text" && functions?.length) {
@@ -1033,6 +1062,11 @@ User's message: ${currentText}`;
1033
1062
  });
1034
1063
  }
1035
1064
  }
1065
+ async _runBeforeTransform(session, elements, message, model, options) {
1066
+ for (const { func: transformFunction } of this._beforeTransformFunctions) {
1067
+ await transformFunction(session, elements, message, model, options);
1068
+ }
1069
+ }
1036
1070
  };
1037
1071
 
1038
1072
  // src/services/chat.ts
@@ -1,17 +1,22 @@
1
1
  import { h, Session } from 'koishi';
2
2
  import { Config } from 'koishi-plugin-chatluna';
3
3
  import { Message } from '../types';
4
+ export interface MessageTransformOptions {
5
+ quote: boolean;
6
+ includeQuoteReply: boolean;
7
+ }
4
8
  export declare class MessageTransformer {
5
9
  private _config;
10
+ private _beforeTransformFunctions;
6
11
  private _transformFunctions;
7
12
  constructor(_config: Config);
8
- transform(session: Session, elements: h[], model: string, message?: Message, options?: {
9
- quote: boolean;
10
- includeQuoteReply: boolean;
11
- }): Promise<Message>;
13
+ transform(session: Session, elements: h[], model: string, message?: Message, options?: MessageTransformOptions): Promise<Message>;
14
+ before(transformFunction: BeforeMessageTransformFunction, priority?: number): () => void;
12
15
  intercept(type: string, transformFunction: MessageTransformFunction, priority?: number): () => void;
13
16
  replace(type: string, transformFunction: MessageTransformFunction): () => void;
14
17
  has(type: string): boolean;
15
18
  private _processElement;
19
+ private _runBeforeTransform;
16
20
  }
21
+ export type BeforeMessageTransformFunction = (session: Session, elements: h[], message: Message, model?: string, options?: MessageTransformOptions) => Promise<void>;
17
22
  export type MessageTransformFunction = (session: Session, element: h, message: Message, model?: string) => Promise<boolean | void>;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-chatluna",
3
3
  "description": "chatluna for koishi",
4
- "version": "1.3.30",
4
+ "version": "1.3.32",
5
5
  "main": "lib/index.cjs",
6
6
  "module": "lib/index.mjs",
7
7
  "typings": "lib/index.d.ts",
@@ -259,14 +259,14 @@
259
259
  "@initencounter/vits": "^0.0.3",
260
260
  "@koishijs/cache": "^2.1.0",
261
261
  "@koishijs/censor": "^1.1.0",
262
+ "@koishijs/plugin-adapter-qq": "^4.10.1",
262
263
  "@koishijs/plugin-notifier": "^1.2.1",
263
264
  "@types/he": "^1.2.3",
264
265
  "@types/js-yaml": "^4.0.9",
265
266
  "@types/qrcode": "^1.5.5",
266
267
  "@types/useragent": "^2",
267
268
  "atsc": "^2.1.0",
268
- "koishi-plugin-adapter-onebot": "^6.8.0",
269
- "@koishijs/plugin-adapter-qq": "^4.10.1"
269
+ "koishi-plugin-adapter-onebot": "^6.8.0"
270
270
  },
271
271
  "peerDependencies": {
272
272
  "koishi": "^4.18.9",