koishi-plugin-chatluna-character 0.0.63 → 0.0.65

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,7 +89,7 @@ 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] ?? (() => {
92
+ currentPreset = presetPool[guildId] ?? await (async () => {
89
93
  const template = preset.getPresetForCache(
90
94
  currentGuildConfig.preset
91
95
  );
@@ -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,15 +647,20 @@ 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) {
649
654
  this.ctx = ctx;
655
+ ctx.on("dispose", () => {
656
+ this._aborter?.abort();
657
+ });
650
658
  }
651
659
  static {
652
660
  __name(this, "Preset");
653
661
  }
654
662
  _presets = [];
663
+ _aborter = null;
655
664
  async loadAllPreset() {
656
665
  await this._checkPresetDir();
657
666
  const presetDir = this.resolvePresetDir();
@@ -676,6 +685,7 @@ var Preset = class {
676
685
  this._presets.map((preset) => preset.name).concat("无").map((name2) => import_koishi3.Schema.const(name2))
677
686
  )
678
687
  );
688
+ this.ctx.emit("chatluna_character/preset_updated");
679
689
  }
680
690
  async getPreset(triggerKeyword, loadForDisk = true, throwError = true) {
681
691
  if (loadForDisk) {
@@ -683,6 +693,40 @@ var Preset = class {
683
693
  }
684
694
  return this.getPresetForCache(triggerKeyword, throwError);
685
695
  }
696
+ watchPreset() {
697
+ let fsWait = false;
698
+ if (this._aborter != null) {
699
+ this._aborter.abort();
700
+ }
701
+ this._aborter = new AbortController();
702
+ (0, import_fs.watch)(
703
+ this.resolvePresetDir(),
704
+ {
705
+ signal: this._aborter.signal
706
+ },
707
+ async (event, filename) => {
708
+ if (filename) {
709
+ if (fsWait) return;
710
+ fsWait = setTimeout(() => {
711
+ fsWait = false;
712
+ }, 100);
713
+ await this.loadAllPreset();
714
+ this.ctx.chatluna_character.logger.debug(
715
+ `trigger full reload preset ${filename}`
716
+ );
717
+ return;
718
+ }
719
+ await this.loadAllPreset();
720
+ this.ctx.chatluna_character.logger.debug(
721
+ `trigger full reload preset`
722
+ );
723
+ }
724
+ );
725
+ }
726
+ async init() {
727
+ await this.loadAllPreset();
728
+ this.watchPreset();
729
+ }
686
730
  getPresetForCache(triggerKeyword, throwError = true) {
687
731
  const preset = this._presets.find(
688
732
  (preset2) => preset2.name === triggerKeyword
@@ -1079,7 +1123,7 @@ function apply6(ctx, config) {
1079
1123
  {
1080
1124
  apply: /* @__PURE__ */ __name(async (ctx2, config2) => {
1081
1125
  await ctx2.chatluna_character.stickerService.init();
1082
- await ctx2.chatluna_character.preset.loadAllPreset();
1126
+ await ctx2.chatluna_character.preset.init();
1083
1127
  await plugins(ctx2, config2);
1084
1128
  }, "apply"),
1085
1129
  inject: Object.assign({}, inject2, {
@@ -1148,7 +1192,7 @@ var Config = import_koishi6.Schema.intersect([
1148
1192
  model: import_koishi6.Schema.dynamic("model").description("模型")
1149
1193
  })
1150
1194
  ).description("针对某个群的模型设置,会覆盖上面的配置"),
1151
- maxTokens: import_koishi6.Schema.number().default(4e3).min(1024).max(16e3).description("使用聊天的最大 token 数")
1195
+ maxTokens: import_koishi6.Schema.number().default(4e3).min(1024).max(24e3).description("使用聊天的最大 token 数")
1152
1196
  }).description("模型配置"),
1153
1197
  import_koishi6.Schema.object({
1154
1198
  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,7 +57,7 @@ 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] ?? (() => {
60
+ currentPreset = presetPool[guildId] ?? await (async () => {
57
61
  const template = preset.getPresetForCache(
58
62
  currentGuildConfig.preset
59
63
  );
@@ -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,14 +618,19 @@ 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;
625
+ ctx.on("dispose", () => {
626
+ this._aborter?.abort();
627
+ });
620
628
  }
621
629
  static {
622
630
  __name(this, "Preset");
623
631
  }
624
632
  _presets = [];
633
+ _aborter = null;
625
634
  async loadAllPreset() {
626
635
  await this._checkPresetDir();
627
636
  const presetDir = this.resolvePresetDir();
@@ -646,6 +655,7 @@ var Preset = class {
646
655
  this._presets.map((preset) => preset.name).concat("无").map((name2) => Schema2.const(name2))
647
656
  )
648
657
  );
658
+ this.ctx.emit("chatluna_character/preset_updated");
649
659
  }
650
660
  async getPreset(triggerKeyword, loadForDisk = true, throwError = true) {
651
661
  if (loadForDisk) {
@@ -653,6 +663,40 @@ var Preset = class {
653
663
  }
654
664
  return this.getPresetForCache(triggerKeyword, throwError);
655
665
  }
666
+ watchPreset() {
667
+ let fsWait = false;
668
+ if (this._aborter != null) {
669
+ this._aborter.abort();
670
+ }
671
+ this._aborter = new AbortController();
672
+ watch(
673
+ this.resolvePresetDir(),
674
+ {
675
+ signal: this._aborter.signal
676
+ },
677
+ async (event, filename) => {
678
+ if (filename) {
679
+ if (fsWait) return;
680
+ fsWait = setTimeout(() => {
681
+ fsWait = false;
682
+ }, 100);
683
+ await this.loadAllPreset();
684
+ this.ctx.chatluna_character.logger.debug(
685
+ `trigger full reload preset ${filename}`
686
+ );
687
+ return;
688
+ }
689
+ await this.loadAllPreset();
690
+ this.ctx.chatluna_character.logger.debug(
691
+ `trigger full reload preset`
692
+ );
693
+ }
694
+ );
695
+ }
696
+ async init() {
697
+ await this.loadAllPreset();
698
+ this.watchPreset();
699
+ }
656
700
  getPresetForCache(triggerKeyword, throwError = true) {
657
701
  const preset = this._presets.find(
658
702
  (preset2) => preset2.name === triggerKeyword
@@ -1048,7 +1092,7 @@ function apply6(ctx, config) {
1048
1092
  {
1049
1093
  apply: /* @__PURE__ */ __name(async (ctx2, config2) => {
1050
1094
  await ctx2.chatluna_character.stickerService.init();
1051
- await ctx2.chatluna_character.preset.loadAllPreset();
1095
+ await ctx2.chatluna_character.preset.init();
1052
1096
  await plugins(ctx2, config2);
1053
1097
  }, "apply"),
1054
1098
  inject: Object.assign({}, inject2, {
@@ -1117,7 +1161,7 @@ var Config = Schema3.intersect([
1117
1161
  model: Schema3.dynamic("model").description("模型")
1118
1162
  })
1119
1163
  ).description("针对某个群的模型设置,会覆盖上面的配置"),
1120
- maxTokens: Schema3.number().default(4e3).min(1024).max(16e3).description("使用聊天的最大 token 数")
1164
+ maxTokens: Schema3.number().default(4e3).min(1024).max(24e3).description("使用聊天的最大 token 数")
1121
1165
  }).description("模型配置"),
1122
1166
  Schema3.object({
1123
1167
  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.65",
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
  你需要在聊天中选择合适的时机去使用这些词汇。