koishi-plugin-chatluna 1.0.0-beta.76 → 1.0.0-beta.78

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.cjs CHANGED
@@ -1078,8 +1078,15 @@ var ChatHubChatChain = class _ChatHubChatChain extends import_base.ChatHubLLMCha
1078
1078
  message,
1079
1079
  stream,
1080
1080
  events,
1081
- conversationId
1081
+ conversationId,
1082
+ systemPrompts
1082
1083
  }) {
1084
+ if (systemPrompts != null) {
1085
+ const prompt = this.chain.prompt;
1086
+ if (prompt instanceof ChatHubChatPrompt) {
1087
+ prompt.systemPrompts = systemPrompts;
1088
+ }
1089
+ }
1083
1090
  const requests = {
1084
1091
  input: message
1085
1092
  };
@@ -4406,7 +4413,22 @@ function apply39(ctx, config, chain) {
4406
4413
  logger9.debug(`current balance: ${balance}`);
4407
4414
  }, "llm-used-token-count")
4408
4415
  },
4409
- config.streamResponse
4416
+ config.streamResponse,
4417
+ (0, import_prompt2.formatPresetTemplate)(presetTemplate, {
4418
+ name: config.botName,
4419
+ date: (/* @__PURE__ */ new Date()).toLocaleString(),
4420
+ bot_id: session.bot.selfId,
4421
+ is_group: (!session.isDirect || session.guildId != null).toString(),
4422
+ is_private: session.isDirect?.toString(),
4423
+ user_id: session.author?.user?.id ?? session.event?.user?.id ?? "0",
4424
+ user: (0, import_string.getNotEmptyString)(
4425
+ session.author?.nick,
4426
+ session.author?.name,
4427
+ session.event.user?.name,
4428
+ session.username
4429
+ ),
4430
+ weekday: (0, import_string.getCurrentWeekday)()
4431
+ })
4410
4432
  );
4411
4433
  } catch (e) {
4412
4434
  if (e?.message?.includes("output values have 1 keys")) {
package/lib/index.mjs CHANGED
@@ -1067,8 +1067,15 @@ var ChatHubChatChain = class _ChatHubChatChain extends ChatHubLLMChainWrapper {
1067
1067
  message,
1068
1068
  stream,
1069
1069
  events,
1070
- conversationId
1070
+ conversationId,
1071
+ systemPrompts
1071
1072
  }) {
1073
+ if (systemPrompts != null) {
1074
+ const prompt = this.chain.prompt;
1075
+ if (prompt instanceof ChatHubChatPrompt) {
1076
+ prompt.systemPrompts = systemPrompts;
1077
+ }
1078
+ }
1072
1079
  const requests = {
1073
1080
  input: message
1074
1081
  };
@@ -4249,7 +4256,10 @@ __name(renderMessage, "renderMessage");
4249
4256
 
4250
4257
  // src/middlewares/request_model.ts
4251
4258
  import { sleep as sleep2 } from "koishi";
4252
- import { formatPresetTemplateString } from "koishi-plugin-chatluna/llm-core/prompt";
4259
+ import {
4260
+ formatPresetTemplate,
4261
+ formatPresetTemplateString
4262
+ } from "koishi-plugin-chatluna/llm-core/prompt";
4253
4263
  import { parseRawModelName as parseRawModelName3 } from "koishi-plugin-chatluna/llm-core/utils/count_tokens";
4254
4264
  import {
4255
4265
  ChatLunaError as ChatLunaError6,
@@ -4424,7 +4434,22 @@ function apply39(ctx, config, chain) {
4424
4434
  logger9.debug(`current balance: ${balance}`);
4425
4435
  }, "llm-used-token-count")
4426
4436
  },
4427
- config.streamResponse
4437
+ config.streamResponse,
4438
+ formatPresetTemplate(presetTemplate, {
4439
+ name: config.botName,
4440
+ date: (/* @__PURE__ */ new Date()).toLocaleString(),
4441
+ bot_id: session.bot.selfId,
4442
+ is_group: (!session.isDirect || session.guildId != null).toString(),
4443
+ is_private: session.isDirect?.toString(),
4444
+ user_id: session.author?.user?.id ?? session.event?.user?.id ?? "0",
4445
+ user: getNotEmptyString(
4446
+ session.author?.nick,
4447
+ session.author?.name,
4448
+ session.event.user?.name,
4449
+ session.username
4450
+ ),
4451
+ weekday: getCurrentWeekday()
4452
+ })
4428
4453
  );
4429
4454
  } catch (e) {
4430
4455
  if (e?.message?.includes("output values have 1 keys")) {
@@ -25,7 +25,7 @@ export declare class OpenAIFunctionsAgentOutputParser extends AgentActionOutputP
25
25
  lc_namespace: string[];
26
26
  static lc_name(): string;
27
27
  parse(text: string): Promise<AgentAction | AgentFinish>;
28
- parseResult(generations: ChatGeneration[]): Promise<FunctionsAgentAction | AgentFinish>;
28
+ parseResult(generations: ChatGeneration[]): Promise<AgentFinish | FunctionsAgentAction>;
29
29
  /**
30
30
  * Parses the output message into a FunctionsAgentAction or AgentFinish
31
31
  * object.
@@ -21,6 +21,7 @@ export interface ChatHubLLMCallArg {
21
21
  stream: boolean;
22
22
  conversationId: string;
23
23
  session: Session;
24
+ systemPrompts?: SystemPrompts;
24
25
  }
25
26
  export interface ChatHubLLMChainInput extends ChainInputs {
26
27
  /** Prompt object to use */
@@ -19,6 +19,6 @@ export declare class ChatHubChatChain extends ChatHubLLMChainWrapper implements
19
19
  chain: ChatHubLLMChain;
20
20
  });
21
21
  static fromLLM(llm: ChatLunaChatModel, { botName, longMemory, historyMemory, systemPrompts, humanMessagePrompt }: ChatHubChatChainInput): ChatHubLLMChainWrapper;
22
- call({ message, stream, events, conversationId }: ChatHubLLMCallArg): Promise<ChainValues>;
22
+ call({ message, stream, events, conversationId, systemPrompts }: ChatHubLLMCallArg): Promise<ChainValues>;
23
23
  get model(): ChatLunaChatModel;
24
24
  }
@@ -346,7 +346,7 @@ var ChatInterface = class {
346
346
  _vectorStoreRetrieverMemory;
347
347
  _chatHistory;
348
348
  _chains = {};
349
- _errorCount = {};
349
+ _errorCountsMap = {};
350
350
  _chatCount = 0;
351
351
  _random = new import_koishi.Random();
352
352
  async chat(arg) {
@@ -361,11 +361,16 @@ var ChatInterface = class {
361
361
  }
362
362
  return response;
363
363
  } catch (e) {
364
- this._errorCount[configMD5] = this._errorCount[config.md5()] ?? 0;
365
- this._errorCount[configMD5] += 1;
366
- if (this._errorCount[configMD5] > config.value.maxRetries) {
364
+ this._errorCountsMap[configMD5] = this._errorCountsMap[configMD5] ?? [];
365
+ const errorCountsArray = this._errorCountsMap[configMD5];
366
+ errorCountsArray.push(Date.now());
367
+ if (errorCountsArray.length > config.value.maxRetries && // 20 mins
368
+ checkRange(
369
+ errorCountsArray.splice(-config.value.maxRetries),
370
+ 1e3 * 60 * 20
371
+ )) {
367
372
  delete this._chains[configMD5];
368
- delete this._errorCount[configMD5];
373
+ delete this._errorCountsMap[configMD5];
369
374
  const service = this.ctx.chatluna.platform;
370
375
  await service.makeConfigStatus(config.value, false);
371
376
  }
@@ -691,6 +696,12 @@ var ChatInterface = class {
691
696
  });
692
697
  }
693
698
  };
699
+ function checkRange(times, delayTime) {
700
+ const first = times[0];
701
+ const last = times[times.length - 1];
702
+ return last - first < delayTime;
703
+ }
704
+ __name(checkRange, "checkRange");
694
705
  // Annotate the CommonJS export names for ESM import in node:
695
706
  0 && (module.exports = {
696
707
  ChatInterface
@@ -11,7 +11,7 @@ export declare class ChatInterface {
11
11
  private _vectorStoreRetrieverMemory;
12
12
  private _chatHistory;
13
13
  private _chains;
14
- private _errorCount;
14
+ private _errorCountsMap;
15
15
  private _chatCount;
16
16
  private _random;
17
17
  constructor(ctx: Context, input: ChatInterfaceInput);
@@ -349,7 +349,7 @@ var ChatInterface = class {
349
349
  _vectorStoreRetrieverMemory;
350
350
  _chatHistory;
351
351
  _chains = {};
352
- _errorCount = {};
352
+ _errorCountsMap = {};
353
353
  _chatCount = 0;
354
354
  _random = new Random();
355
355
  async chat(arg) {
@@ -364,11 +364,16 @@ var ChatInterface = class {
364
364
  }
365
365
  return response;
366
366
  } catch (e) {
367
- this._errorCount[configMD5] = this._errorCount[config.md5()] ?? 0;
368
- this._errorCount[configMD5] += 1;
369
- if (this._errorCount[configMD5] > config.value.maxRetries) {
367
+ this._errorCountsMap[configMD5] = this._errorCountsMap[configMD5] ?? [];
368
+ const errorCountsArray = this._errorCountsMap[configMD5];
369
+ errorCountsArray.push(Date.now());
370
+ if (errorCountsArray.length > config.value.maxRetries && // 20 mins
371
+ checkRange(
372
+ errorCountsArray.splice(-config.value.maxRetries),
373
+ 1e3 * 60 * 20
374
+ )) {
370
375
  delete this._chains[configMD5];
371
- delete this._errorCount[configMD5];
376
+ delete this._errorCountsMap[configMD5];
372
377
  const service = this.ctx.chatluna.platform;
373
378
  await service.makeConfigStatus(config.value, false);
374
379
  }
@@ -694,6 +699,12 @@ var ChatInterface = class {
694
699
  });
695
700
  }
696
701
  };
702
+ function checkRange(times, delayTime) {
703
+ const first = times[0];
704
+ const last = times[times.length - 1];
705
+ return last - first < delayTime;
706
+ }
707
+ __name(checkRange, "checkRange");
697
708
  export {
698
709
  ChatInterface
699
710
  };
@@ -657,11 +657,18 @@ var ChatLunaService = class extends import_koishi2.Service {
657
657
  findPlugin(fun) {
658
658
  return this._plugins.find(fun);
659
659
  }
660
- chat(session, room, message, event, stream = false) {
660
+ chat(session, room, message, event, stream = false, systemPrompts) {
661
661
  const { model: modelName } = room;
662
662
  const [platform] = (0, import_count_tokens.parseRawModelName)(modelName);
663
663
  const chatInterfaceWrapper = this._chatInterfaceWrapper[platform] ?? this._createChatInterfaceWrapper(platform);
664
- return chatInterfaceWrapper.chat(session, room, message, event, stream);
664
+ return chatInterfaceWrapper.chat(
665
+ session,
666
+ room,
667
+ message,
668
+ event,
669
+ stream,
670
+ systemPrompts
671
+ );
665
672
  }
666
673
  queryInterfaceWrapper(room) {
667
674
  const { model: modelName } = room;
@@ -1071,7 +1078,7 @@ var ChatInterfaceWrapper = class {
1071
1078
  _modelQueue = new import_queue.RequestIdQueue();
1072
1079
  _conversationQueue = new import_queue.RequestIdQueue();
1073
1080
  _platformService;
1074
- async chat(session, room, message, event, stream) {
1081
+ async chat(session, room, message, event, stream, systemPrompts) {
1075
1082
  const { conversationId, model: fullModelName } = room;
1076
1083
  const [platform] = (0, import_count_tokens.parseRawModelName)(fullModelName);
1077
1084
  const config = this._platformService.getConfigs(platform)[0];
@@ -1094,7 +1101,8 @@ var ChatInterfaceWrapper = class {
1094
1101
  events: event,
1095
1102
  stream,
1096
1103
  conversationId,
1097
- session
1104
+ session,
1105
+ systemPrompts
1098
1106
  });
1099
1107
  return {
1100
1108
  content: chainValues.message.content,
@@ -2,7 +2,7 @@ import { Awaitable, Computed, Context, Schema, Service, Session } from 'koishi';
2
2
  import { ChatInterface } from 'koishi-plugin-chatluna/llm-core/chat/app';
3
3
  import { Cache } from '../cache';
4
4
  import { ChatChain } from '../chains/chain';
5
- import { ChatHubLLMChainWrapper } from 'koishi-plugin-chatluna/llm-core/chain/base';
5
+ import { ChatHubLLMChainWrapper, SystemPrompts } from 'koishi-plugin-chatluna/llm-core/chain/base';
6
6
  import { BasePlatformClient } from 'koishi-plugin-chatluna/llm-core/platform/client';
7
7
  import { ClientConfig, ClientConfigPool } from 'koishi-plugin-chatluna/llm-core/platform/config';
8
8
  import { ChatHubBaseEmbeddings, ChatLunaChatModel } from 'koishi-plugin-chatluna/llm-core/platform/model';
@@ -34,7 +34,7 @@ export declare class ChatLunaService extends Service {
34
34
  awaitLoadPlatform(plugin: ChatLunaPlugin | string): Promise<unknown>;
35
35
  unregisterPlugin(plugin: ChatLunaPlugin | string): Promise<void>;
36
36
  findPlugin(fun: (plugin: ChatLunaPlugin) => boolean): ChatLunaPlugin;
37
- chat(session: Session, room: ConversationRoom, message: Message, event: ChatEvents, stream?: boolean): Promise<Message>;
37
+ chat(session: Session, room: ConversationRoom, message: Message, event: ChatEvents, stream?: boolean, systemPrompts?: SystemPrompts): Promise<Message>;
38
38
  queryInterfaceWrapper(room: ConversationRoom): ChatInterfaceWrapper;
39
39
  clearChatHistory(room: ConversationRoom): Promise<void>;
40
40
  getCachedInterfaceWrappers(): ChatInterfaceWrapper[];
@@ -87,7 +87,7 @@ declare class ChatInterfaceWrapper {
87
87
  private _conversationQueue;
88
88
  private _platformService;
89
89
  constructor(_service: ChatLunaService);
90
- chat(session: Session, room: ConversationRoom, message: Message, event: ChatEvents, stream: boolean): Promise<Message>;
90
+ chat(session: Session, room: ConversationRoom, message: Message, event: ChatEvents, stream: boolean, systemPrompts?: SystemPrompts): Promise<Message>;
91
91
  query(room: ConversationRoom): Promise<ChatInterface>;
92
92
  clearChatHistory(room: ConversationRoom): Promise<void>;
93
93
  clear(room: ConversationRoom | string): Promise<void>;
@@ -644,11 +644,18 @@ var ChatLunaService = class extends Service {
644
644
  findPlugin(fun) {
645
645
  return this._plugins.find(fun);
646
646
  }
647
- chat(session, room, message, event, stream = false) {
647
+ chat(session, room, message, event, stream = false, systemPrompts) {
648
648
  const { model: modelName } = room;
649
649
  const [platform] = parseRawModelName(modelName);
650
650
  const chatInterfaceWrapper = this._chatInterfaceWrapper[platform] ?? this._createChatInterfaceWrapper(platform);
651
- return chatInterfaceWrapper.chat(session, room, message, event, stream);
651
+ return chatInterfaceWrapper.chat(
652
+ session,
653
+ room,
654
+ message,
655
+ event,
656
+ stream,
657
+ systemPrompts
658
+ );
652
659
  }
653
660
  queryInterfaceWrapper(room) {
654
661
  const { model: modelName } = room;
@@ -1058,7 +1065,7 @@ var ChatInterfaceWrapper = class {
1058
1065
  _modelQueue = new RequestIdQueue();
1059
1066
  _conversationQueue = new RequestIdQueue();
1060
1067
  _platformService;
1061
- async chat(session, room, message, event, stream) {
1068
+ async chat(session, room, message, event, stream, systemPrompts) {
1062
1069
  const { conversationId, model: fullModelName } = room;
1063
1070
  const [platform] = parseRawModelName(fullModelName);
1064
1071
  const config = this._platformService.getConfigs(platform)[0];
@@ -1081,7 +1088,8 @@ var ChatInterfaceWrapper = class {
1081
1088
  events: event,
1082
1089
  stream,
1083
1090
  conversationId,
1084
- session
1091
+ session,
1092
+ systemPrompts
1085
1093
  });
1086
1094
  return {
1087
1095
  content: chainValues.message.content,
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.0.0-beta.76",
4
+ "version": "1.0.0-beta.78",
5
5
  "main": "lib/index.cjs",
6
6
  "module": "lib/index.mjs",
7
7
  "typings": "lib/index.d.ts",
@@ -223,7 +223,7 @@
223
223
  "@types/md5": "^2.3.5",
224
224
  "@types/qrcode": "^1.5.5",
225
225
  "@types/useragent": "^2",
226
- "@types/uuid": "^9.0.8",
226
+ "@types/uuid": "^10.0.0",
227
227
  "atsc": "^2.1.0",
228
228
  "koishi-plugin-puppeteer": "^3.9.0"
229
229
  },