koishi-plugin-chatluna-think-viewer 2.2.5 → 2.2.6

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.
Files changed (2) hide show
  1. package/index.js +39 -7
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -8,8 +8,8 @@ const inject = {
8
8
  chatluna: { required: false },
9
9
  };
10
10
 
11
- const defaultForbidden = [
12
- '<think>[\\s\\S]*?<\\/think>',
11
+ const defaultForbidden = [
12
+ '<think>[\\s\\S]*?<\\/think>',
13
13
  '<status>[\\s\\S]*?<\\/status>',
14
14
  '<output>[\\s\\S]*?<\\/output>',
15
15
  '<analysis>[\\s\\S]*?<\\/analysis>',
@@ -28,7 +28,7 @@ const defaultForbidden = [
28
28
  const strictOutputPattern =
29
29
  '^\\s*<output>\\s*(<message>(?:<at>\\d+<\\/at>\\s*)?(?:<sticker>[^<]*<\\/sticker>|[^<]*)<\\/message>\\s*){1,5}<\\/output>\\s*$';
30
30
 
31
- const Config = Schema.intersect([
31
+ const Config = Schema.intersect([
32
32
  Schema.object({
33
33
  command: Schema.string().default('think').description('\u67e5\u770b\u601d\u8003\u5185\u5bb9\u7684\u6307\u4ee4\u540d'),
34
34
  keywords: Schema.array(Schema.string()).default(['\u67e5\u770b\u601d\u8003', '\u4e0a\u6b21\u601d\u8003']).description('\u53ef\u65e0\u524d\u7f00\u89e6\u53d1\u7684\u5173\u952e\u8bcd'),
@@ -323,10 +323,42 @@ function apply(ctx, config) {
323
323
  });
324
324
 
325
325
  // \u5f02\u5e38\u8f93\u51fa\u81ea\u52a8\u5904\u7406
326
- applyGuard(ctx, config);
327
- }
328
-
329
- module.exports = {
326
+ applyGuard(ctx, config);
327
+
328
+ // Hot-fix chatluna-character completionMessages trimming bug:
329
+ // it used to drop newest messages; we wrap getTemp to trim from the head.
330
+ const service = ctx.chatluna_character;
331
+ if (service && typeof service.getTemp === 'function') {
332
+ const originalGetTemp = service.getTemp.bind(service);
333
+ service.getTemp = async function patchedGetTemp(session) {
334
+ const temp = await originalGetTemp(session);
335
+ const limit = () => {
336
+ const c = service._config?.modelCompletionCount ?? 3;
337
+ return Math.max(2, c * 2);
338
+ };
339
+ if (Array.isArray(temp.completionMessages) && !temp.completionMessages._thinkViewerPatched) {
340
+ const arr = temp.completionMessages;
341
+ const originalPush = arr.push;
342
+ arr.push = function patchedPush(...args) {
343
+ const res = originalPush.apply(this, args);
344
+ const max = limit();
345
+ if (this.length > max) {
346
+ this.splice(0, this.length - max);
347
+ }
348
+ return res;
349
+ };
350
+ Object.defineProperty(arr, '_thinkViewerPatched', { value: true, enumerable: false });
351
+ }
352
+ return temp;
353
+ };
354
+
355
+ ctx.on('dispose', () => {
356
+ service.getTemp = originalGetTemp;
357
+ });
358
+ }
359
+ }
360
+
361
+ module.exports = {
330
362
  name,
331
363
  apply,
332
364
  Config,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "koishi-plugin-chatluna-think-viewer",
3
- "version": "2.2.5",
3
+ "version": "2.2.6",
4
4
  "main": "index.js",
5
5
  "description": "通过命令/关键词查看 chatluna-character 最近一次回复的 <think> 思考内容,并在消息格式异常时自动拦截/撤回。",
6
6
  "license": "MIT",