koishi-plugin-chatluna-character 0.0.63 → 0.0.64

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
@@ -75,8 +75,12 @@ async function apply(ctx, config) {
75
75
  })();
76
76
  }
77
77
  }
78
- const globalPreset = await preset.getPreset(config.defaultPreset);
79
- const presetPool = {};
78
+ let globalPreset = await preset.getPreset(config.defaultPreset);
79
+ let presetPool = {};
80
+ ctx.on("chatluna_character/preset_updated", async () => {
81
+ globalPreset = await preset.getPreset(config.defaultPreset);
82
+ presetPool = {};
83
+ });
80
84
  service.collect(async (session, messages) => {
81
85
  const guildId = session.event.guild?.id ?? session.guildId;
82
86
  const model = await (modelPool[guildId] ?? Promise.resolve(globalModel));
@@ -85,8 +89,8 @@ async function apply(ctx, config) {
85
89
  let currentPreset = globalPreset;
86
90
  if (currentGuildConfig != null) {
87
91
  copyOfConfig = Object.assign({}, copyOfConfig, currentGuildConfig);
88
- currentPreset = presetPool[guildId] ?? (() => {
89
- const template = preset.getPresetForCache(
92
+ currentPreset = presetPool[guildId] ?? await (async () => {
93
+ const template = await preset.getPreset(
90
94
  currentGuildConfig.preset
91
95
  );
92
96
  presetPool[guildId] = template;
@@ -184,7 +188,7 @@ async function apply(ctx, config) {
184
188
  logger.error(e);
185
189
  await session.send(elements);
186
190
  }
187
- break;
191
+ continue;
188
192
  }
189
193
  try {
190
194
  await (0, import_koishi.sleep)(random.int(maxTime / 2, maxTime));
@@ -643,6 +647,7 @@ var import_koishi3 = require("koishi");
643
647
  var import_error = require("koishi-plugin-chatluna/utils/error");
644
648
  var import_path = __toESM(require("path"), 1);
645
649
  var import_url = require("url");
650
+ var import_fs = require("fs");
646
651
  var import_meta = {};
647
652
  var Preset = class {
648
653
  constructor(ctx) {
@@ -652,6 +657,7 @@ var Preset = class {
652
657
  __name(this, "Preset");
653
658
  }
654
659
  _presets = [];
660
+ _aborter = null;
655
661
  async loadAllPreset() {
656
662
  await this._checkPresetDir();
657
663
  const presetDir = this.resolvePresetDir();
@@ -676,6 +682,7 @@ var Preset = class {
676
682
  this._presets.map((preset) => preset.name).concat("无").map((name2) => import_koishi3.Schema.const(name2))
677
683
  )
678
684
  );
685
+ this.ctx.emit("chatluna_character/preset_updated");
679
686
  }
680
687
  async getPreset(triggerKeyword, loadForDisk = true, throwError = true) {
681
688
  if (loadForDisk) {
@@ -683,6 +690,40 @@ var Preset = class {
683
690
  }
684
691
  return this.getPresetForCache(triggerKeyword, throwError);
685
692
  }
693
+ watchPreset() {
694
+ let fsWait = false;
695
+ if (this._aborter != null) {
696
+ this._aborter.abort();
697
+ }
698
+ this._aborter = new AbortController();
699
+ (0, import_fs.watch)(
700
+ this.resolvePresetDir(),
701
+ {
702
+ signal: this._aborter.signal
703
+ },
704
+ async (event, filename) => {
705
+ if (filename) {
706
+ if (fsWait) return;
707
+ fsWait = setTimeout(() => {
708
+ fsWait = false;
709
+ }, 100);
710
+ await this.loadAllPreset();
711
+ this.ctx.chatluna_character.logger.debug(
712
+ `trigger full reload preset ${filename}`
713
+ );
714
+ return;
715
+ }
716
+ await this.loadAllPreset();
717
+ this.ctx.chatluna_character.logger.debug(
718
+ `trigger full reload preset`
719
+ );
720
+ }
721
+ );
722
+ }
723
+ async init() {
724
+ await this.loadAllPreset();
725
+ this.watchPreset();
726
+ }
686
727
  getPresetForCache(triggerKeyword, throwError = true) {
687
728
  const preset = this._presets.find(
688
729
  (preset2) => preset2.name === triggerKeyword
@@ -1079,7 +1120,7 @@ function apply6(ctx, config) {
1079
1120
  {
1080
1121
  apply: /* @__PURE__ */ __name(async (ctx2, config2) => {
1081
1122
  await ctx2.chatluna_character.stickerService.init();
1082
- await ctx2.chatluna_character.preset.loadAllPreset();
1123
+ await ctx2.chatluna_character.preset.init();
1083
1124
  await plugins(ctx2, config2);
1084
1125
  }, "apply"),
1085
1126
  inject: Object.assign({}, inject2, {
@@ -1148,7 +1189,7 @@ var Config = import_koishi6.Schema.intersect([
1148
1189
  model: import_koishi6.Schema.dynamic("model").description("模型")
1149
1190
  })
1150
1191
  ).description("针对某个群的模型设置,会覆盖上面的配置"),
1151
- maxTokens: import_koishi6.Schema.number().default(4e3).min(1024).max(16e3).description("使用聊天的最大 token 数")
1192
+ maxTokens: import_koishi6.Schema.number().default(4e3).min(1024).max(24e3).description("使用聊天的最大 token 数")
1152
1193
  }).description("模型配置"),
1153
1194
  import_koishi6.Schema.object({
1154
1195
  isNickname: import_koishi6.Schema.boolean().description("允许 bot 配置中的昵称引发回复").default(true),
package/lib/index.d.ts CHANGED
@@ -50,9 +50,12 @@ export function plugins(ctx: Context, parent: Config): Promise<void>;
50
50
  export class Preset {
51
51
  private readonly ctx;
52
52
  private readonly _presets;
53
+ private _aborter;
53
54
  constructor(ctx: Context);
54
55
  loadAllPreset(): Promise<void>;
55
56
  getPreset(triggerKeyword: string, loadForDisk?: boolean, throwError?: boolean): Promise<PresetTemplate>;
57
+ watchPreset(): void;
58
+ init(): Promise<void>;
56
59
  getPresetForCache(triggerKeyword: string, throwError?: boolean): PresetTemplate;
57
60
  getDefaultPreset(): Promise<PresetTemplate>;
58
61
  getAllPreset(): Promise<string[]>;
@@ -62,6 +65,11 @@ export class Preset {
62
65
  private _copyDefaultPresets;
63
66
  }
64
67
  export function loadPreset(text: string): PresetTemplate;
68
+ declare module 'koishi' {
69
+ interface Events {
70
+ 'chatluna_character/preset_updated': () => void;
71
+ }
72
+ }
65
73
  export class StickerService {
66
74
  private _ctx;
67
75
  private _config;
package/lib/index.mjs CHANGED
@@ -43,8 +43,12 @@ async function apply(ctx, config) {
43
43
  })();
44
44
  }
45
45
  }
46
- const globalPreset = await preset.getPreset(config.defaultPreset);
47
- const presetPool = {};
46
+ let globalPreset = await preset.getPreset(config.defaultPreset);
47
+ let presetPool = {};
48
+ ctx.on("chatluna_character/preset_updated", async () => {
49
+ globalPreset = await preset.getPreset(config.defaultPreset);
50
+ presetPool = {};
51
+ });
48
52
  service.collect(async (session, messages) => {
49
53
  const guildId = session.event.guild?.id ?? session.guildId;
50
54
  const model = await (modelPool[guildId] ?? Promise.resolve(globalModel));
@@ -53,8 +57,8 @@ async function apply(ctx, config) {
53
57
  let currentPreset = globalPreset;
54
58
  if (currentGuildConfig != null) {
55
59
  copyOfConfig = Object.assign({}, copyOfConfig, currentGuildConfig);
56
- currentPreset = presetPool[guildId] ?? (() => {
57
- const template = preset.getPresetForCache(
60
+ currentPreset = presetPool[guildId] ?? await (async () => {
61
+ const template = await preset.getPreset(
58
62
  currentGuildConfig.preset
59
63
  );
60
64
  presetPool[guildId] = template;
@@ -152,7 +156,7 @@ async function apply(ctx, config) {
152
156
  logger.error(e);
153
157
  await session.send(elements);
154
158
  }
155
- break;
159
+ continue;
156
160
  }
157
161
  try {
158
162
  await sleep(random.int(maxTime / 2, maxTime));
@@ -614,6 +618,7 @@ import {
614
618
  } from "koishi-plugin-chatluna/utils/error";
615
619
  import path from "path";
616
620
  import { fileURLToPath } from "url";
621
+ import { watch } from "fs";
617
622
  var Preset = class {
618
623
  constructor(ctx) {
619
624
  this.ctx = ctx;
@@ -622,6 +627,7 @@ var Preset = class {
622
627
  __name(this, "Preset");
623
628
  }
624
629
  _presets = [];
630
+ _aborter = null;
625
631
  async loadAllPreset() {
626
632
  await this._checkPresetDir();
627
633
  const presetDir = this.resolvePresetDir();
@@ -646,6 +652,7 @@ var Preset = class {
646
652
  this._presets.map((preset) => preset.name).concat("无").map((name2) => Schema2.const(name2))
647
653
  )
648
654
  );
655
+ this.ctx.emit("chatluna_character/preset_updated");
649
656
  }
650
657
  async getPreset(triggerKeyword, loadForDisk = true, throwError = true) {
651
658
  if (loadForDisk) {
@@ -653,6 +660,40 @@ var Preset = class {
653
660
  }
654
661
  return this.getPresetForCache(triggerKeyword, throwError);
655
662
  }
663
+ watchPreset() {
664
+ let fsWait = false;
665
+ if (this._aborter != null) {
666
+ this._aborter.abort();
667
+ }
668
+ this._aborter = new AbortController();
669
+ watch(
670
+ this.resolvePresetDir(),
671
+ {
672
+ signal: this._aborter.signal
673
+ },
674
+ async (event, filename) => {
675
+ if (filename) {
676
+ if (fsWait) return;
677
+ fsWait = setTimeout(() => {
678
+ fsWait = false;
679
+ }, 100);
680
+ await this.loadAllPreset();
681
+ this.ctx.chatluna_character.logger.debug(
682
+ `trigger full reload preset ${filename}`
683
+ );
684
+ return;
685
+ }
686
+ await this.loadAllPreset();
687
+ this.ctx.chatluna_character.logger.debug(
688
+ `trigger full reload preset`
689
+ );
690
+ }
691
+ );
692
+ }
693
+ async init() {
694
+ await this.loadAllPreset();
695
+ this.watchPreset();
696
+ }
656
697
  getPresetForCache(triggerKeyword, throwError = true) {
657
698
  const preset = this._presets.find(
658
699
  (preset2) => preset2.name === triggerKeyword
@@ -1048,7 +1089,7 @@ function apply6(ctx, config) {
1048
1089
  {
1049
1090
  apply: /* @__PURE__ */ __name(async (ctx2, config2) => {
1050
1091
  await ctx2.chatluna_character.stickerService.init();
1051
- await ctx2.chatluna_character.preset.loadAllPreset();
1092
+ await ctx2.chatluna_character.preset.init();
1052
1093
  await plugins(ctx2, config2);
1053
1094
  }, "apply"),
1054
1095
  inject: Object.assign({}, inject2, {
@@ -1117,7 +1158,7 @@ var Config = Schema3.intersect([
1117
1158
  model: Schema3.dynamic("model").description("模型")
1118
1159
  })
1119
1160
  ).description("针对某个群的模型设置,会覆盖上面的配置"),
1120
- maxTokens: Schema3.number().default(4e3).min(1024).max(16e3).description("使用聊天的最大 token 数")
1161
+ maxTokens: Schema3.number().default(4e3).min(1024).max(24e3).description("使用聊天的最大 token 数")
1121
1162
  }).description("模型配置"),
1122
1163
  Schema3.object({
1123
1164
  isNickname: Schema3.boolean().description("允许 bot 配置中的昵称引发回复").default(true),
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-chatluna-character",
3
3
  "description": "Let the large language model play a role, disguise as a group friend",
4
- "version": "0.0.63",
4
+ "version": "0.0.64",
5
5
  "type": "module",
6
6
  "main": "lib/index.cjs",
7
7
  "module": "lib/index.mjs",
@@ -41,7 +41,7 @@
41
41
  },
42
42
  "homepage": "https://github.com/ChatLunaLab/chatluna-character#readme",
43
43
  "dependencies": {
44
- "@langchain/core": "^0.1.63",
44
+ "@langchain/core": "^0.2.27",
45
45
  "js-yaml": "^4.1.0"
46
46
  },
47
47
  "devDependencies": {
@@ -101,6 +101,7 @@ system: |
101
101
  233333:哈哈大笑的意思,来源猫扑的第233号表情包,是一张大笑的表情。
102
102
  xx使我快乐:比喻某件事让你感到开心。
103
103
  你礼貌吗:歌手耿斯汉和袁娅维第一次见面,耿刚打完招呼,就向袁娅维要微信,这种直男让周深直接就调侃说了一句“你礼貌吗”
104
+ 笑点解析:令人忍俊不禁
104
105
 
105
106
 
106
107
  你需要在聊天中选择合适的时机去使用这些词汇。