koishi-plugin-chatluna 1.4.2 → 1.4.3

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.
@@ -50,7 +50,7 @@ export declare class ChainMiddleware {
50
50
  constructor(name: string, execute: ChainMiddlewareFunction, graph: ChatChainDependencyGraph);
51
51
  before<T extends keyof ChainMiddlewareName>(name: T): this;
52
52
  after<T extends keyof ChainMiddlewareName>(name: T): this;
53
- run(session: Session, options: ChainMiddlewareContext): Promise<string | ChainMiddlewareRunStatus | h[] | h[][]>;
53
+ run(session: Session, options: ChainMiddlewareContext): Promise<string | h[] | h[][] | ChainMiddlewareRunStatus>;
54
54
  }
55
55
  export interface ChainMiddlewareContext {
56
56
  config: Config;
package/lib/index.cjs CHANGED
@@ -3258,10 +3258,11 @@ function apply15(ctx, config, chain) {
3258
3258
  __name(apply15, "apply");
3259
3259
 
3260
3260
  // src/middlewares/chat/cooldown_time.ts
3261
- var lastChatTime = 0;
3261
+ var lastChatTimes = /* @__PURE__ */ new Map();
3262
3262
  function apply16(ctx, config, chain) {
3263
3263
  chain.middleware("cooldown_time", async (session, context) => {
3264
3264
  const currentChatTime = Date.now();
3265
+ const lastChatTime = lastChatTimes.get(session.channelId) ?? 0;
3265
3266
  if (currentChatTime - lastChatTime < config.msgCooldown * 1e3) {
3266
3267
  const waitTime = (config.msgCooldown * 1e3 - (currentChatTime - lastChatTime)) / 1e3;
3267
3268
  context.message = session.text(
@@ -3270,9 +3271,9 @@ function apply16(ctx, config, chain) {
3270
3271
  );
3271
3272
  return 1 /* STOP */;
3272
3273
  }
3273
- lastChatTime = currentChatTime;
3274
+ lastChatTimes.set(session.channelId, currentChatTime);
3274
3275
  return 2 /* CONTINUE */;
3275
- }).after("allow_reply");
3276
+ }).after("allow_reply").after("message_delay");
3276
3277
  }
3277
3278
  __name(apply16, "apply");
3278
3279
 
@@ -3315,7 +3316,7 @@ function apply17(ctx, config, chain) {
3315
3316
  queues.set(conversationId, queue);
3316
3317
  const tailTurn = queue.turns[queue.turns.length - 1];
3317
3318
  let turn;
3318
- if (tailTurn && tailTurn.state !== "processing" && tailTurn.userName === userName) {
3319
+ if (tailTurn && tailTurn.state !== "processing") {
3319
3320
  turn = tailTurn;
3320
3321
  logger9.debug(
3321
3322
  `Joining turn for ${conversationId}, messageId: ${messageId}, user: ${userName}, total: ${turn.messages.length + 1}`
@@ -3324,7 +3325,6 @@ function apply17(ctx, config, chain) {
3324
3325
  const state = queue.turns.length === 0 && config.messageQueueDelay > 0 ? "collecting" : "waiting";
3325
3326
  turn = {
3326
3327
  messages: [],
3327
- userName,
3328
3328
  state
3329
3329
  };
3330
3330
  queue.turns.push(turn);
@@ -3501,11 +3501,18 @@ function mergeMessages(messages) {
3501
3501
  if (messages.length === 1) return messages[0];
3502
3502
  const base = messages[0];
3503
3503
  const mergedContent = [];
3504
+ const multiUser = messages.some((msg) => msg.name !== base.name);
3504
3505
  for (const msg of messages) {
3505
3506
  const content = msg.content;
3506
3507
  if (mergedContent.length > 0) {
3507
3508
  mergedContent.push({ type: "text", text: "\n" });
3508
3509
  }
3510
+ if (multiUser) {
3511
+ mergedContent.push({
3512
+ type: "text",
3513
+ text: `${msg.name ?? "unknown"}: `
3514
+ });
3515
+ }
3509
3516
  if (typeof content === "string") {
3510
3517
  mergedContent.push({ type: "text", text: content });
3511
3518
  } else if (Array.isArray(content)) {
@@ -7750,7 +7757,6 @@ var ReplyStream = class {
7750
7757
  sentAdditional = false;
7751
7758
  closed = false;
7752
7759
  moderated;
7753
- pending = [];
7754
7760
  renderMessage;
7755
7761
  renderAdditional;
7756
7762
  async write(frame) {
@@ -7767,10 +7773,6 @@ var ReplyStream = class {
7767
7773
  return;
7768
7774
  }
7769
7775
  if (frame.type === "mark" && frame.instant) {
7770
- if (this.moderated) {
7771
- this.pending.push({ content: frame.content ?? frame.name });
7772
- return;
7773
- }
7774
7776
  await this.sendMessage(
7775
7777
  { content: frame.content ?? frame.name },
7776
7778
  "split"
@@ -7791,26 +7793,10 @@ var ReplyStream = class {
7791
7793
  if (frame != null) {
7792
7794
  await this.write(frame);
7793
7795
  }
7794
- if (this.moderated && this.finalMessage == null) return;
7795
- if (frame?.type === "done" && this.mode === "split" && !this.closed) {
7796
- return;
7797
- }
7798
- if (this.closed) {
7799
- if (frame?.type === "done") {
7800
- if (this.mode === "edit") {
7801
- await this.sendMessage(frame.message, "edit");
7802
- } else if (this.mode === "buffer" || this.firstChunk) {
7803
- await this.sendMessage(frame.message, "split");
7804
- }
7805
- }
7806
- return await this.finish();
7807
- }
7796
+ if (frame?.type !== "done" && frame?.type !== "error") return;
7797
+ if (this.closed) return;
7808
7798
  this.closed = true;
7809
7799
  await this.context.recallThinkingMessage?.();
7810
- for (const message of this.pending) {
7811
- await this.sendMessage(message, "split");
7812
- }
7813
- this.pending = [];
7814
7800
  if (this.mode === "buffer") {
7815
7801
  if (this.finalMessage != null) {
7816
7802
  await this.sendMessage(this.finalMessage, "split");
@@ -7865,7 +7851,7 @@ var ReplyStream = class {
7865
7851
  await this.context.send([elements]);
7866
7852
  }
7867
7853
  async censor(message) {
7868
- if (!this.moderated) return message;
7854
+ if (!this.moderated || this.ctx.censor == null) return message;
7869
7855
  return await censorMessage(
7870
7856
  this.ctx,
7871
7857
  message,
package/lib/index.mjs CHANGED
@@ -3265,10 +3265,11 @@ function apply15(ctx, config, chain) {
3265
3265
  __name(apply15, "apply");
3266
3266
 
3267
3267
  // src/middlewares/chat/cooldown_time.ts
3268
- var lastChatTime = 0;
3268
+ var lastChatTimes = /* @__PURE__ */ new Map();
3269
3269
  function apply16(ctx, config, chain) {
3270
3270
  chain.middleware("cooldown_time", async (session, context) => {
3271
3271
  const currentChatTime = Date.now();
3272
+ const lastChatTime = lastChatTimes.get(session.channelId) ?? 0;
3272
3273
  if (currentChatTime - lastChatTime < config.msgCooldown * 1e3) {
3273
3274
  const waitTime = (config.msgCooldown * 1e3 - (currentChatTime - lastChatTime)) / 1e3;
3274
3275
  context.message = session.text(
@@ -3277,9 +3278,9 @@ function apply16(ctx, config, chain) {
3277
3278
  );
3278
3279
  return 1 /* STOP */;
3279
3280
  }
3280
- lastChatTime = currentChatTime;
3281
+ lastChatTimes.set(session.channelId, currentChatTime);
3281
3282
  return 2 /* CONTINUE */;
3282
- }).after("allow_reply");
3283
+ }).after("allow_reply").after("message_delay");
3283
3284
  }
3284
3285
  __name(apply16, "apply");
3285
3286
 
@@ -3322,7 +3323,7 @@ function apply17(ctx, config, chain) {
3322
3323
  queues.set(conversationId, queue);
3323
3324
  const tailTurn = queue.turns[queue.turns.length - 1];
3324
3325
  let turn;
3325
- if (tailTurn && tailTurn.state !== "processing" && tailTurn.userName === userName) {
3326
+ if (tailTurn && tailTurn.state !== "processing") {
3326
3327
  turn = tailTurn;
3327
3328
  logger9.debug(
3328
3329
  `Joining turn for ${conversationId}, messageId: ${messageId}, user: ${userName}, total: ${turn.messages.length + 1}`
@@ -3331,7 +3332,6 @@ function apply17(ctx, config, chain) {
3331
3332
  const state = queue.turns.length === 0 && config.messageQueueDelay > 0 ? "collecting" : "waiting";
3332
3333
  turn = {
3333
3334
  messages: [],
3334
- userName,
3335
3335
  state
3336
3336
  };
3337
3337
  queue.turns.push(turn);
@@ -3508,11 +3508,18 @@ function mergeMessages(messages) {
3508
3508
  if (messages.length === 1) return messages[0];
3509
3509
  const base = messages[0];
3510
3510
  const mergedContent = [];
3511
+ const multiUser = messages.some((msg) => msg.name !== base.name);
3511
3512
  for (const msg of messages) {
3512
3513
  const content = msg.content;
3513
3514
  if (mergedContent.length > 0) {
3514
3515
  mergedContent.push({ type: "text", text: "\n" });
3515
3516
  }
3517
+ if (multiUser) {
3518
+ mergedContent.push({
3519
+ type: "text",
3520
+ text: `${msg.name ?? "unknown"}: `
3521
+ });
3522
+ }
3516
3523
  if (typeof content === "string") {
3517
3524
  mergedContent.push({ type: "text", text: content });
3518
3525
  } else if (Array.isArray(content)) {
@@ -7791,7 +7798,6 @@ var ReplyStream = class {
7791
7798
  sentAdditional = false;
7792
7799
  closed = false;
7793
7800
  moderated;
7794
- pending = [];
7795
7801
  renderMessage;
7796
7802
  renderAdditional;
7797
7803
  async write(frame) {
@@ -7808,10 +7814,6 @@ var ReplyStream = class {
7808
7814
  return;
7809
7815
  }
7810
7816
  if (frame.type === "mark" && frame.instant) {
7811
- if (this.moderated) {
7812
- this.pending.push({ content: frame.content ?? frame.name });
7813
- return;
7814
- }
7815
7817
  await this.sendMessage(
7816
7818
  { content: frame.content ?? frame.name },
7817
7819
  "split"
@@ -7832,26 +7834,10 @@ var ReplyStream = class {
7832
7834
  if (frame != null) {
7833
7835
  await this.write(frame);
7834
7836
  }
7835
- if (this.moderated && this.finalMessage == null) return;
7836
- if (frame?.type === "done" && this.mode === "split" && !this.closed) {
7837
- return;
7838
- }
7839
- if (this.closed) {
7840
- if (frame?.type === "done") {
7841
- if (this.mode === "edit") {
7842
- await this.sendMessage(frame.message, "edit");
7843
- } else if (this.mode === "buffer" || this.firstChunk) {
7844
- await this.sendMessage(frame.message, "split");
7845
- }
7846
- }
7847
- return await this.finish();
7848
- }
7837
+ if (frame?.type !== "done" && frame?.type !== "error") return;
7838
+ if (this.closed) return;
7849
7839
  this.closed = true;
7850
7840
  await this.context.recallThinkingMessage?.();
7851
- for (const message of this.pending) {
7852
- await this.sendMessage(message, "split");
7853
- }
7854
- this.pending = [];
7855
7841
  if (this.mode === "buffer") {
7856
7842
  if (this.finalMessage != null) {
7857
7843
  await this.sendMessage(this.finalMessage, "split");
@@ -7906,7 +7892,7 @@ var ReplyStream = class {
7906
7892
  await this.context.send([elements]);
7907
7893
  }
7908
7894
  async censor(message) {
7909
- if (!this.moderated) return message;
7895
+ if (!this.moderated || this.ctx.censor == null) return message;
7910
7896
  return await censorMessage(
7911
7897
  this.ctx,
7912
7898
  message,
@@ -21,8 +21,8 @@ declare class AgentTaskTool extends StructuredTool {
21
21
  }, "strip", z.ZodTypeAny, {
22
22
  message?: string;
23
23
  id?: string;
24
- action?: "run" | "message" | "status" | "list" | "stop" | "list_all";
25
24
  prompt?: string;
25
+ action?: "message" | "status" | "list" | "run" | "stop" | "list_all";
26
26
  agent?: string;
27
27
  background?: boolean;
28
28
  goal?: string;
@@ -30,8 +30,8 @@ declare class AgentTaskTool extends StructuredTool {
30
30
  }, {
31
31
  message?: string;
32
32
  id?: string;
33
- action?: "run" | "message" | "status" | "list" | "stop" | "list_all";
34
33
  prompt?: string;
34
+ action?: "message" | "status" | "list" | "run" | "stop" | "list_all";
35
35
  agent?: string;
36
36
  background?: boolean;
37
37
  goal?: string;
@@ -39,8 +39,8 @@ declare class AgentTaskTool extends StructuredTool {
39
39
  }>, {
40
40
  message?: string;
41
41
  id?: string;
42
- action?: "run" | "message" | "status" | "list" | "stop" | "list_all";
43
42
  prompt?: string;
43
+ action?: "message" | "status" | "list" | "run" | "stop" | "list_all";
44
44
  agent?: string;
45
45
  background?: boolean;
46
46
  goal?: string;
@@ -48,8 +48,8 @@ declare class AgentTaskTool extends StructuredTool {
48
48
  }, {
49
49
  message?: string;
50
50
  id?: string;
51
- action?: "run" | "message" | "status" | "list" | "stop" | "list_all";
52
51
  prompt?: string;
52
+ action?: "message" | "status" | "list" | "run" | "stop" | "list_all";
53
53
  agent?: string;
54
54
  background?: boolean;
55
55
  goal?: string;
@@ -1,4 +1,3 @@
1
- import { Embeddings } from '@langchain/core/embeddings';
2
1
  import { AIMessage } from '@langchain/core/messages';
3
2
  import { ComputedRef } from '@vue/reactivity';
4
3
  import { ChatLunaChatModel } from 'koishi-plugin-chatluna/llm-core/platform/model';
@@ -6,7 +5,7 @@ import { PlatformService } from 'koishi-plugin-chatluna/llm-core/platform/servic
6
5
  import { ModelInfo } from 'koishi-plugin-chatluna/llm-core/platform/types';
7
6
  import type { ChatLunaService } from '../../services/chat';
8
7
  export declare function createDisplayResponse(responseMessage: AIMessage): AIMessage;
9
- export declare function initEmbeddings(service: PlatformService, model: string | undefined): Promise<ComputedRef<Embeddings<number[]>>>;
8
+ export declare function initEmbeddings(service: PlatformService, model: string | undefined): Promise<ComputedRef<import("koishi-plugin-chatluna/llm-core/model/in_memory").EmptyEmbeddings>>;
10
9
  export declare function initModel(chatluna: ChatLunaService, llmPlatform: string, llmModelName: string): Promise<[
11
10
  ComputedRef<ChatLunaChatModel>,
12
11
  ComputedRef<ModelInfo | undefined>
@@ -45,4 +45,4 @@ export declare function isComplexRoom(room: LegacyRoomRecord, members: LegacyRoo
45
45
  export declare function filterValidRooms(rooms: LegacyRoomRecord[]): LegacyRoomRecord[];
46
46
  export declare function aclKey(conversationId: string, principalType: string, principalId: string, permission: string): string;
47
47
  export declare function resolveRoomBindingKey(room: LegacyRoomRecord, members: LegacyRoomMemberRecord[], groups: LegacyRoomGroupRecord[], routeModes: Map<string, 'shared' | 'personal'>): string;
48
- export declare function inferLegacyGroupRouteModes(users: LegacyUserRecord[], rooms: LegacyRoomRecord[], groups: LegacyRoomGroupRecord[]): Map<string, "personal" | "shared">;
48
+ export declare function inferLegacyGroupRouteModes(users: LegacyUserRecord[], rooms: LegacyRoomRecord[], groups: LegacyRoomGroupRecord[]): Map<string, "shared" | "personal">;
@@ -25,7 +25,6 @@ export declare class ReplyStream {
25
25
  private sentAdditional;
26
26
  private closed;
27
27
  private moderated;
28
- private pending;
29
28
  private renderMessage;
30
29
  private renderAdditional?;
31
30
  constructor(ctx: Context, config: Config, context: ChainMiddlewareContext, renderer: Renderer, opts: ReplyStreamOptions);
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.4.2",
4
+ "version": "1.4.3",
5
5
  "main": "lib/index.cjs",
6
6
  "module": "lib/index.mjs",
7
7
  "typings": "lib/index.d.ts",