koishi-plugin-chatluna 1.2.0-alpha.2 → 1.2.0-alpha.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.
package/lib/index.cjs CHANGED
@@ -72,9 +72,9 @@ __export(index_exports, {
72
72
  usage: () => usage
73
73
  });
74
74
  module.exports = __toCommonJS(index_exports);
75
- var import_koishi20 = require("koishi");
75
+ var import_koishi21 = require("koishi");
76
76
  var import_chat2 = require("koishi-plugin-chatluna/services/chat");
77
- var import_koishi21 = require("koishi-plugin-chatluna/utils/koishi");
77
+ var import_koishi22 = require("koishi-plugin-chatluna/utils/koishi");
78
78
  var import_logger9 = require("koishi-plugin-chatluna/utils/logger");
79
79
  var request = __toESM(require("koishi-plugin-chatluna/utils/request"), 1);
80
80
 
@@ -6011,7 +6011,7 @@ var Config2 = import_koishi13.Schema.intersect([
6011
6011
  });
6012
6012
 
6013
6013
  // src/render.ts
6014
- var import_koishi19 = require("koishi");
6014
+ var import_koishi20 = require("koishi");
6015
6015
  var import_error7 = require("koishi-plugin-chatluna/utils/error");
6016
6016
 
6017
6017
  // src/renders/default.ts
@@ -6285,6 +6285,120 @@ function renderTokens2(tokens) {
6285
6285
  }
6286
6286
  __name(renderTokens2, "renderTokens");
6287
6287
 
6288
+ // src/renders/pure-text.ts
6289
+ var import_koishi_plugin_markdown2 = require("koishi-plugin-markdown");
6290
+ var import_koishi19 = require("koishi");
6291
+
6292
+ // src/utils/remove-markdown.ts
6293
+ function removeMarkdown(md, options = {}) {
6294
+ options.listUnicodeChar = Object.prototype.hasOwnProperty.call(
6295
+ options,
6296
+ "listUnicodeChar"
6297
+ ) ? options.listUnicodeChar : false;
6298
+ options.stripListLeaders = Object.prototype.hasOwnProperty.call(
6299
+ options,
6300
+ "stripListLeaders"
6301
+ ) ? options.stripListLeaders : true;
6302
+ options.gfm = Object.prototype.hasOwnProperty.call(options, "gfm") ? options.gfm : true;
6303
+ options.useImgAltText = Object.prototype.hasOwnProperty.call(
6304
+ options,
6305
+ "useImgAltText"
6306
+ ) ? options.useImgAltText : true;
6307
+ options.abbr = Object.prototype.hasOwnProperty.call(options, "abbr") ? options.abbr : false;
6308
+ options.replaceLinksWithURL = Object.prototype.hasOwnProperty.call(
6309
+ options,
6310
+ "replaceLinksWithURL"
6311
+ ) ? options.replaceLinksWithURL : false;
6312
+ options.htmlTagsToSkip = Object.prototype.hasOwnProperty.call(
6313
+ options,
6314
+ "htmlTagsToSkip"
6315
+ ) ? options.htmlTagsToSkip : [];
6316
+ options.throwError = Object.prototype.hasOwnProperty.call(
6317
+ options,
6318
+ "throwError"
6319
+ ) ? options.throwError : false;
6320
+ let output = md || "";
6321
+ output = output.replace(
6322
+ /^ {0,3}((?:-[\t ]*){3,}|(?:_[ \t]*){3,}|(?:\*[ \t]*){3,})(?:\n+|$)/gm,
6323
+ ""
6324
+ );
6325
+ try {
6326
+ if (options.stripListLeaders) {
6327
+ if (options.listUnicodeChar)
6328
+ output = output.replace(
6329
+ /^([\s\t]*)([\*\-\+]|\d+\.)\s+/gm,
6330
+ options.listUnicodeChar + " $1"
6331
+ );
6332
+ else
6333
+ output = output.replace(/^([\s\t]*)([\*\-\+]|\d+\.)\s+/gm, "$1");
6334
+ }
6335
+ if (options.gfm) {
6336
+ output = output.replace(/\n={2,}/g, "\n").replace(/~{3}.*\n/g, "").replace(/~~/g, "").replace(/```(?:.*)\n([\s\S]*?)```/g, (_, code) => code.trim());
6337
+ }
6338
+ if (options.abbr) {
6339
+ output = output.replace(/\*\[.*\]:.*\n/, "");
6340
+ }
6341
+ let htmlReplaceRegex = /<[^>]*>/g;
6342
+ if (options.htmlTagsToSkip && options.htmlTagsToSkip.length > 0) {
6343
+ const joinedHtmlTagsToSkip = options.htmlTagsToSkip.join("|");
6344
+ htmlReplaceRegex = new RegExp(
6345
+ `<(?!/?(${joinedHtmlTagsToSkip})(?=>|s[^>]*>))[^>]*>`,
6346
+ "g"
6347
+ );
6348
+ }
6349
+ output = output.replace(htmlReplaceRegex, "").replace(/^[=\-]{2,}\s*$/g, "").replace(/\[\^.+?\](\: .*?$)?/g, "").replace(/\s{0,2}\[.*?\]: .*?$/g, "").replace(
6350
+ /\!\[(.*?)\][\[\(].*?[\]\)]/g,
6351
+ options.useImgAltText ? "$1" : ""
6352
+ ).replace(
6353
+ /\[([\s\S]*?)\]\s*[\(\[].*?[\)\]]/g,
6354
+ options.replaceLinksWithURL ? "$2" : "$1"
6355
+ ).replace(/^(\n)?\s{0,3}>\s?/gm, "$1").replace(/^\s{1,2}\[(.*?)\]: (\S+)( ".*?")?\s*$/g, "").replace(
6356
+ /^(\n)?\s{0,}#{1,6}\s*( (.+))? +#+$|^(\n)?\s{0,}#{1,6}\s*( (.+))?$/gm,
6357
+ "$1$3$4$6"
6358
+ ).replace(/([\*]+)(\S)(.*?\S)??\1/g, "$2$3").replace(/(^|\W)([_]+)(\S)(.*?\S)??\2($|\W)/g, "$1$3$4$5").replace(/(`{3,})(.*?)\1/gm, "$2").replace(/`(.+?)`/g, "$1").replace(/~(.*?)~/g, "$1");
6359
+ } catch (e) {
6360
+ if (options.throwError) throw e;
6361
+ console.error("remove-markdown encountered error: %s", e);
6362
+ return md;
6363
+ }
6364
+ return output;
6365
+ }
6366
+ __name(removeMarkdown, "removeMarkdown");
6367
+
6368
+ // src/renders/pure-text.ts
6369
+ var import_he3 = __toESM(require("he"), 1);
6370
+ var PureTextRenderer = class extends Renderer {
6371
+ static {
6372
+ __name(this, "PureTextRenderer");
6373
+ }
6374
+ async render(message, options) {
6375
+ let transformed = [import_koishi19.h.text(message.content)];
6376
+ if (options.split) {
6377
+ transformed = transformed.flatMap((element) => {
6378
+ const content = element.attrs["content"];
6379
+ return content.split("\n\n\n").map((paragraph) => {
6380
+ return import_koishi19.h.text(paragraph);
6381
+ });
6382
+ });
6383
+ }
6384
+ transformed = transformed.map((element) => {
6385
+ const content = element.attrs["content"];
6386
+ return import_koishi19.h.text(stripMarkdown(content));
6387
+ });
6388
+ return {
6389
+ element: transformed
6390
+ };
6391
+ }
6392
+ schema = import_koishi19.Schema.const("pure-text").i18n({
6393
+ "zh-CN": "将回复渲染为纯文本(去除 markdown 格式)",
6394
+ "en-US": "Render as pure text (remove markdown format)"
6395
+ });
6396
+ };
6397
+ function stripMarkdown(source) {
6398
+ return removeMarkdown(source);
6399
+ }
6400
+ __name(stripMarkdown, "stripMarkdown");
6401
+
6288
6402
  // src/render.ts
6289
6403
  var DefaultRenderer = class {
6290
6404
  constructor(ctx, config) {
@@ -6306,6 +6420,7 @@ var DefaultRenderer = class {
6306
6420
  "koishi-element",
6307
6421
  () => new KoishiElementRenderer(ctx2)
6308
6422
  );
6423
+ this.addRenderer("pure-text", () => new PureTextRenderer(ctx2));
6309
6424
  });
6310
6425
  }
6311
6426
  static {
@@ -6322,7 +6437,7 @@ var DefaultRenderer = class {
6322
6437
  for (const additionalMessage of message.additionalReplyMessages) {
6323
6438
  const elements = await rawRenderer.render(additionalMessage, options).then((r) => r.element);
6324
6439
  result2.push({
6325
- element: (0, import_koishi19.h)(
6440
+ element: (0, import_koishi20.h)(
6326
6441
  "message",
6327
6442
  { forward: true },
6328
6443
  Array.isArray(elements) ? elements : [elements]
@@ -6354,7 +6469,7 @@ var DefaultRenderer = class {
6354
6469
  }
6355
6470
  this.ctx.schema.set(
6356
6471
  "output-mode",
6357
- import_koishi19.Schema.union(this._getAllRendererScheme())
6472
+ import_koishi20.Schema.union(this._getAllRendererScheme())
6358
6473
  );
6359
6474
  }
6360
6475
  _getAllRendererScheme() {
@@ -6411,7 +6526,7 @@ async function setupEntryPoint(ctx, config, disposables) {
6411
6526
  });
6412
6527
  setupMiddleware(ctx2);
6413
6528
  }, "entryPointPlugin");
6414
- const entryPointDisposable = (0, import_koishi21.forkScopeToDisposable)(
6529
+ const entryPointDisposable = (0, import_koishi22.forkScopeToDisposable)(
6415
6530
  ctx.plugin(
6416
6531
  {
6417
6532
  apply: entryPointPlugin,
@@ -6458,7 +6573,7 @@ function setupMiddleware(ctx) {
6458
6573
  __name(setupMiddleware, "setupMiddleware");
6459
6574
  function setupLogger(config) {
6460
6575
  if (config.isLog) {
6461
- (0, import_logger9.setLoggerLevel)(import_koishi20.Logger.DEBUG);
6576
+ (0, import_logger9.setLoggerLevel)(import_koishi21.Logger.DEBUG);
6462
6577
  }
6463
6578
  }
6464
6579
  __name(setupLogger, "setupLogger");
@@ -6483,8 +6598,8 @@ function setupProxy(ctx, config) {
6483
6598
  __name(setupProxy, "setupProxy");
6484
6599
  async function setupServices(ctx, config, disposables) {
6485
6600
  disposables.push(
6486
- (0, import_koishi21.forkScopeToDisposable)(ctx.plugin(import_chat2.ChatLunaService, config)),
6487
- (0, import_koishi21.forkScopeToDisposable)(ctx.plugin(ChatLunaAuthService, config))
6601
+ (0, import_koishi22.forkScopeToDisposable)(ctx.plugin(import_chat2.ChatLunaService, config)),
6602
+ (0, import_koishi22.forkScopeToDisposable)(ctx.plugin(ChatLunaAuthService, config))
6488
6603
  );
6489
6604
  }
6490
6605
  __name(setupServices, "setupServices");
@@ -6544,7 +6659,7 @@ async function setupAutoDelete(ctx, config) {
6544
6659
  await execute();
6545
6660
  ctx.setInterval(async () => {
6546
6661
  await execute();
6547
- }, import_koishi20.Time.minute * 5);
6662
+ }, import_koishi21.Time.minute * 5);
6548
6663
  }
6549
6664
  __name(setupAutoDelete, "setupAutoDelete");
6550
6665
  // Annotate the CommonJS export names for ESM import in node:
package/lib/index.mjs CHANGED
@@ -6017,7 +6017,7 @@ var Config2 = Schema2.intersect([
6017
6017
  });
6018
6018
 
6019
6019
  // src/render.ts
6020
- import { h as h12, Schema as Schema8 } from "koishi";
6020
+ import { h as h13, Schema as Schema9 } from "koishi";
6021
6021
  import {
6022
6022
  ChatLunaError as ChatLunaError7,
6023
6023
  ChatLunaErrorCode as ChatLunaErrorCode7
@@ -6294,6 +6294,120 @@ function renderTokens2(tokens) {
6294
6294
  }
6295
6295
  __name(renderTokens2, "renderTokens");
6296
6296
 
6297
+ // src/renders/pure-text.ts
6298
+ import { transform as transform2 } from "koishi-plugin-markdown";
6299
+ import { h as h12, Schema as Schema8 } from "koishi";
6300
+
6301
+ // src/utils/remove-markdown.ts
6302
+ function removeMarkdown(md, options = {}) {
6303
+ options.listUnicodeChar = Object.prototype.hasOwnProperty.call(
6304
+ options,
6305
+ "listUnicodeChar"
6306
+ ) ? options.listUnicodeChar : false;
6307
+ options.stripListLeaders = Object.prototype.hasOwnProperty.call(
6308
+ options,
6309
+ "stripListLeaders"
6310
+ ) ? options.stripListLeaders : true;
6311
+ options.gfm = Object.prototype.hasOwnProperty.call(options, "gfm") ? options.gfm : true;
6312
+ options.useImgAltText = Object.prototype.hasOwnProperty.call(
6313
+ options,
6314
+ "useImgAltText"
6315
+ ) ? options.useImgAltText : true;
6316
+ options.abbr = Object.prototype.hasOwnProperty.call(options, "abbr") ? options.abbr : false;
6317
+ options.replaceLinksWithURL = Object.prototype.hasOwnProperty.call(
6318
+ options,
6319
+ "replaceLinksWithURL"
6320
+ ) ? options.replaceLinksWithURL : false;
6321
+ options.htmlTagsToSkip = Object.prototype.hasOwnProperty.call(
6322
+ options,
6323
+ "htmlTagsToSkip"
6324
+ ) ? options.htmlTagsToSkip : [];
6325
+ options.throwError = Object.prototype.hasOwnProperty.call(
6326
+ options,
6327
+ "throwError"
6328
+ ) ? options.throwError : false;
6329
+ let output = md || "";
6330
+ output = output.replace(
6331
+ /^ {0,3}((?:-[\t ]*){3,}|(?:_[ \t]*){3,}|(?:\*[ \t]*){3,})(?:\n+|$)/gm,
6332
+ ""
6333
+ );
6334
+ try {
6335
+ if (options.stripListLeaders) {
6336
+ if (options.listUnicodeChar)
6337
+ output = output.replace(
6338
+ /^([\s\t]*)([\*\-\+]|\d+\.)\s+/gm,
6339
+ options.listUnicodeChar + " $1"
6340
+ );
6341
+ else
6342
+ output = output.replace(/^([\s\t]*)([\*\-\+]|\d+\.)\s+/gm, "$1");
6343
+ }
6344
+ if (options.gfm) {
6345
+ output = output.replace(/\n={2,}/g, "\n").replace(/~{3}.*\n/g, "").replace(/~~/g, "").replace(/```(?:.*)\n([\s\S]*?)```/g, (_, code) => code.trim());
6346
+ }
6347
+ if (options.abbr) {
6348
+ output = output.replace(/\*\[.*\]:.*\n/, "");
6349
+ }
6350
+ let htmlReplaceRegex = /<[^>]*>/g;
6351
+ if (options.htmlTagsToSkip && options.htmlTagsToSkip.length > 0) {
6352
+ const joinedHtmlTagsToSkip = options.htmlTagsToSkip.join("|");
6353
+ htmlReplaceRegex = new RegExp(
6354
+ `<(?!/?(${joinedHtmlTagsToSkip})(?=>|s[^>]*>))[^>]*>`,
6355
+ "g"
6356
+ );
6357
+ }
6358
+ output = output.replace(htmlReplaceRegex, "").replace(/^[=\-]{2,}\s*$/g, "").replace(/\[\^.+?\](\: .*?$)?/g, "").replace(/\s{0,2}\[.*?\]: .*?$/g, "").replace(
6359
+ /\!\[(.*?)\][\[\(].*?[\]\)]/g,
6360
+ options.useImgAltText ? "$1" : ""
6361
+ ).replace(
6362
+ /\[([\s\S]*?)\]\s*[\(\[].*?[\)\]]/g,
6363
+ options.replaceLinksWithURL ? "$2" : "$1"
6364
+ ).replace(/^(\n)?\s{0,3}>\s?/gm, "$1").replace(/^\s{1,2}\[(.*?)\]: (\S+)( ".*?")?\s*$/g, "").replace(
6365
+ /^(\n)?\s{0,}#{1,6}\s*( (.+))? +#+$|^(\n)?\s{0,}#{1,6}\s*( (.+))?$/gm,
6366
+ "$1$3$4$6"
6367
+ ).replace(/([\*]+)(\S)(.*?\S)??\1/g, "$2$3").replace(/(^|\W)([_]+)(\S)(.*?\S)??\2($|\W)/g, "$1$3$4$5").replace(/(`{3,})(.*?)\1/gm, "$2").replace(/`(.+?)`/g, "$1").replace(/~(.*?)~/g, "$1");
6368
+ } catch (e) {
6369
+ if (options.throwError) throw e;
6370
+ console.error("remove-markdown encountered error: %s", e);
6371
+ return md;
6372
+ }
6373
+ return output;
6374
+ }
6375
+ __name(removeMarkdown, "removeMarkdown");
6376
+
6377
+ // src/renders/pure-text.ts
6378
+ import he3 from "he";
6379
+ var PureTextRenderer = class extends Renderer {
6380
+ static {
6381
+ __name(this, "PureTextRenderer");
6382
+ }
6383
+ async render(message, options) {
6384
+ let transformed = [h12.text(message.content)];
6385
+ if (options.split) {
6386
+ transformed = transformed.flatMap((element) => {
6387
+ const content = element.attrs["content"];
6388
+ return content.split("\n\n\n").map((paragraph) => {
6389
+ return h12.text(paragraph);
6390
+ });
6391
+ });
6392
+ }
6393
+ transformed = transformed.map((element) => {
6394
+ const content = element.attrs["content"];
6395
+ return h12.text(stripMarkdown(content));
6396
+ });
6397
+ return {
6398
+ element: transformed
6399
+ };
6400
+ }
6401
+ schema = Schema8.const("pure-text").i18n({
6402
+ "zh-CN": "将回复渲染为纯文本(去除 markdown 格式)",
6403
+ "en-US": "Render as pure text (remove markdown format)"
6404
+ });
6405
+ };
6406
+ function stripMarkdown(source) {
6407
+ return removeMarkdown(source);
6408
+ }
6409
+ __name(stripMarkdown, "stripMarkdown");
6410
+
6297
6411
  // src/render.ts
6298
6412
  var DefaultRenderer = class {
6299
6413
  constructor(ctx, config) {
@@ -6315,6 +6429,7 @@ var DefaultRenderer = class {
6315
6429
  "koishi-element",
6316
6430
  () => new KoishiElementRenderer(ctx2)
6317
6431
  );
6432
+ this.addRenderer("pure-text", () => new PureTextRenderer(ctx2));
6318
6433
  });
6319
6434
  }
6320
6435
  static {
@@ -6331,7 +6446,7 @@ var DefaultRenderer = class {
6331
6446
  for (const additionalMessage of message.additionalReplyMessages) {
6332
6447
  const elements = await rawRenderer.render(additionalMessage, options).then((r) => r.element);
6333
6448
  result2.push({
6334
- element: h12(
6449
+ element: h13(
6335
6450
  "message",
6336
6451
  { forward: true },
6337
6452
  Array.isArray(elements) ? elements : [elements]
@@ -6363,7 +6478,7 @@ var DefaultRenderer = class {
6363
6478
  }
6364
6479
  this.ctx.schema.set(
6365
6480
  "output-mode",
6366
- Schema8.union(this._getAllRendererScheme())
6481
+ Schema9.union(this._getAllRendererScheme())
6367
6482
  );
6368
6483
  }
6369
6484
  _getAllRendererScheme() {
@@ -0,0 +1,9 @@
1
+ import { Message, RenderMessage, RenderOptions } from '../types';
2
+ import { Renderer } from './default';
3
+ import { h, Schema } from 'koishi';
4
+ export declare class PureTextRenderer extends Renderer {
5
+ render(message: Message, options: RenderOptions): Promise<RenderMessage>;
6
+ schema: Schema<"pure-text", "pure-text">;
7
+ }
8
+ export declare function transformAndEscape(source: string): h[];
9
+ export declare function stripMarkdown(source: string): string;
@@ -53,7 +53,7 @@ __export(chat_exports, {
53
53
  module.exports = __toCommonJS(chat_exports);
54
54
  var import_messages = require("@langchain/core/messages");
55
55
  var import_fs = __toESM(require("fs"), 1);
56
- var import_koishi9 = require("koishi");
56
+ var import_koishi10 = require("koishi");
57
57
  var import_app = require("koishi-plugin-chatluna/llm-core/chat/app");
58
58
  var import_path = __toESM(require("path"), 1);
59
59
  var import_lru_cache = require("lru-cache");
@@ -791,7 +791,7 @@ Please consider this quote when generating your response. User's message: ${mess
791
791
  var import_request = require("koishi-plugin-chatluna/utils/request");
792
792
 
793
793
  // src/render.ts
794
- var import_koishi8 = require("koishi");
794
+ var import_koishi9 = require("koishi");
795
795
  var import_error3 = require("koishi-plugin-chatluna/utils/error");
796
796
 
797
797
  // src/renders/default.ts
@@ -1065,6 +1065,120 @@ function renderTokens2(tokens) {
1065
1065
  }
1066
1066
  __name(renderTokens2, "renderTokens");
1067
1067
 
1068
+ // src/renders/pure-text.ts
1069
+ var import_koishi_plugin_markdown2 = require("koishi-plugin-markdown");
1070
+ var import_koishi8 = require("koishi");
1071
+
1072
+ // src/utils/remove-markdown.ts
1073
+ function removeMarkdown(md, options = {}) {
1074
+ options.listUnicodeChar = Object.prototype.hasOwnProperty.call(
1075
+ options,
1076
+ "listUnicodeChar"
1077
+ ) ? options.listUnicodeChar : false;
1078
+ options.stripListLeaders = Object.prototype.hasOwnProperty.call(
1079
+ options,
1080
+ "stripListLeaders"
1081
+ ) ? options.stripListLeaders : true;
1082
+ options.gfm = Object.prototype.hasOwnProperty.call(options, "gfm") ? options.gfm : true;
1083
+ options.useImgAltText = Object.prototype.hasOwnProperty.call(
1084
+ options,
1085
+ "useImgAltText"
1086
+ ) ? options.useImgAltText : true;
1087
+ options.abbr = Object.prototype.hasOwnProperty.call(options, "abbr") ? options.abbr : false;
1088
+ options.replaceLinksWithURL = Object.prototype.hasOwnProperty.call(
1089
+ options,
1090
+ "replaceLinksWithURL"
1091
+ ) ? options.replaceLinksWithURL : false;
1092
+ options.htmlTagsToSkip = Object.prototype.hasOwnProperty.call(
1093
+ options,
1094
+ "htmlTagsToSkip"
1095
+ ) ? options.htmlTagsToSkip : [];
1096
+ options.throwError = Object.prototype.hasOwnProperty.call(
1097
+ options,
1098
+ "throwError"
1099
+ ) ? options.throwError : false;
1100
+ let output = md || "";
1101
+ output = output.replace(
1102
+ /^ {0,3}((?:-[\t ]*){3,}|(?:_[ \t]*){3,}|(?:\*[ \t]*){3,})(?:\n+|$)/gm,
1103
+ ""
1104
+ );
1105
+ try {
1106
+ if (options.stripListLeaders) {
1107
+ if (options.listUnicodeChar)
1108
+ output = output.replace(
1109
+ /^([\s\t]*)([\*\-\+]|\d+\.)\s+/gm,
1110
+ options.listUnicodeChar + " $1"
1111
+ );
1112
+ else
1113
+ output = output.replace(/^([\s\t]*)([\*\-\+]|\d+\.)\s+/gm, "$1");
1114
+ }
1115
+ if (options.gfm) {
1116
+ output = output.replace(/\n={2,}/g, "\n").replace(/~{3}.*\n/g, "").replace(/~~/g, "").replace(/```(?:.*)\n([\s\S]*?)```/g, (_, code) => code.trim());
1117
+ }
1118
+ if (options.abbr) {
1119
+ output = output.replace(/\*\[.*\]:.*\n/, "");
1120
+ }
1121
+ let htmlReplaceRegex = /<[^>]*>/g;
1122
+ if (options.htmlTagsToSkip && options.htmlTagsToSkip.length > 0) {
1123
+ const joinedHtmlTagsToSkip = options.htmlTagsToSkip.join("|");
1124
+ htmlReplaceRegex = new RegExp(
1125
+ `<(?!/?(${joinedHtmlTagsToSkip})(?=>|s[^>]*>))[^>]*>`,
1126
+ "g"
1127
+ );
1128
+ }
1129
+ output = output.replace(htmlReplaceRegex, "").replace(/^[=\-]{2,}\s*$/g, "").replace(/\[\^.+?\](\: .*?$)?/g, "").replace(/\s{0,2}\[.*?\]: .*?$/g, "").replace(
1130
+ /\!\[(.*?)\][\[\(].*?[\]\)]/g,
1131
+ options.useImgAltText ? "$1" : ""
1132
+ ).replace(
1133
+ /\[([\s\S]*?)\]\s*[\(\[].*?[\)\]]/g,
1134
+ options.replaceLinksWithURL ? "$2" : "$1"
1135
+ ).replace(/^(\n)?\s{0,3}>\s?/gm, "$1").replace(/^\s{1,2}\[(.*?)\]: (\S+)( ".*?")?\s*$/g, "").replace(
1136
+ /^(\n)?\s{0,}#{1,6}\s*( (.+))? +#+$|^(\n)?\s{0,}#{1,6}\s*( (.+))?$/gm,
1137
+ "$1$3$4$6"
1138
+ ).replace(/([\*]+)(\S)(.*?\S)??\1/g, "$2$3").replace(/(^|\W)([_]+)(\S)(.*?\S)??\2($|\W)/g, "$1$3$4$5").replace(/(`{3,})(.*?)\1/gm, "$2").replace(/`(.+?)`/g, "$1").replace(/~(.*?)~/g, "$1");
1139
+ } catch (e) {
1140
+ if (options.throwError) throw e;
1141
+ console.error("remove-markdown encountered error: %s", e);
1142
+ return md;
1143
+ }
1144
+ return output;
1145
+ }
1146
+ __name(removeMarkdown, "removeMarkdown");
1147
+
1148
+ // src/renders/pure-text.ts
1149
+ var import_he3 = __toESM(require("he"), 1);
1150
+ var PureTextRenderer = class extends Renderer {
1151
+ static {
1152
+ __name(this, "PureTextRenderer");
1153
+ }
1154
+ async render(message, options) {
1155
+ let transformed = [import_koishi8.h.text(message.content)];
1156
+ if (options.split) {
1157
+ transformed = transformed.flatMap((element) => {
1158
+ const content = element.attrs["content"];
1159
+ return content.split("\n\n\n").map((paragraph) => {
1160
+ return import_koishi8.h.text(paragraph);
1161
+ });
1162
+ });
1163
+ }
1164
+ transformed = transformed.map((element) => {
1165
+ const content = element.attrs["content"];
1166
+ return import_koishi8.h.text(stripMarkdown(content));
1167
+ });
1168
+ return {
1169
+ element: transformed
1170
+ };
1171
+ }
1172
+ schema = import_koishi8.Schema.const("pure-text").i18n({
1173
+ "zh-CN": "将回复渲染为纯文本(去除 markdown 格式)",
1174
+ "en-US": "Render as pure text (remove markdown format)"
1175
+ });
1176
+ };
1177
+ function stripMarkdown(source) {
1178
+ return removeMarkdown(source);
1179
+ }
1180
+ __name(stripMarkdown, "stripMarkdown");
1181
+
1068
1182
  // src/render.ts
1069
1183
  var DefaultRenderer = class {
1070
1184
  constructor(ctx, config) {
@@ -1086,6 +1200,7 @@ var DefaultRenderer = class {
1086
1200
  "koishi-element",
1087
1201
  () => new KoishiElementRenderer(ctx2)
1088
1202
  );
1203
+ this.addRenderer("pure-text", () => new PureTextRenderer(ctx2));
1089
1204
  });
1090
1205
  }
1091
1206
  static {
@@ -1102,7 +1217,7 @@ var DefaultRenderer = class {
1102
1217
  for (const additionalMessage of message.additionalReplyMessages) {
1103
1218
  const elements = await rawRenderer.render(additionalMessage, options).then((r) => r.element);
1104
1219
  result.push({
1105
- element: (0, import_koishi8.h)(
1220
+ element: (0, import_koishi9.h)(
1106
1221
  "message",
1107
1222
  { forward: true },
1108
1223
  Array.isArray(elements) ? elements : [elements]
@@ -1134,7 +1249,7 @@ var DefaultRenderer = class {
1134
1249
  }
1135
1250
  this.ctx.schema.set(
1136
1251
  "output-mode",
1137
- import_koishi8.Schema.union(this._getAllRendererScheme())
1252
+ import_koishi9.Schema.union(this._getAllRendererScheme())
1138
1253
  );
1139
1254
  }
1140
1255
  _getAllRendererScheme() {
@@ -1148,7 +1263,7 @@ var DefaultRenderer = class {
1148
1263
  // src/services/chat.ts
1149
1264
  var import_promise = require("koishi-plugin-chatluna/utils/promise");
1150
1265
  var import_in_memory = require("koishi-plugin-chatluna/llm-core/model/in_memory");
1151
- var ChatLunaService = class extends import_koishi9.Service {
1266
+ var ChatLunaService = class extends import_koishi10.Service {
1152
1267
  constructor(ctx, config) {
1153
1268
  super(ctx, "chatluna");
1154
1269
  this.ctx = ctx;
@@ -1849,28 +1964,28 @@ ${reasoningContent}`
1849
1964
  }
1850
1965
  };
1851
1966
  ((ChatLunaPlugin2) => {
1852
- ChatLunaPlugin2.Config = import_koishi9.Schema.intersect([
1853
- import_koishi9.Schema.object({
1854
- chatConcurrentMaxSize: import_koishi9.Schema.number().min(1).max(8).default(3),
1855
- chatTimeLimit: import_koishi9.Schema.number().min(1).max(2e3).computed().default(200),
1856
- configMode: import_koishi9.Schema.union([
1857
- import_koishi9.Schema.const("default"),
1858
- import_koishi9.Schema.const("balance")
1967
+ ChatLunaPlugin2.Config = import_koishi10.Schema.intersect([
1968
+ import_koishi10.Schema.object({
1969
+ chatConcurrentMaxSize: import_koishi10.Schema.number().min(1).max(8).default(3),
1970
+ chatTimeLimit: import_koishi10.Schema.number().min(1).max(2e3).computed().default(200),
1971
+ configMode: import_koishi10.Schema.union([
1972
+ import_koishi10.Schema.const("default"),
1973
+ import_koishi10.Schema.const("balance")
1859
1974
  ]).default("default"),
1860
- maxRetries: import_koishi9.Schema.number().min(1).max(6).default(3),
1861
- timeout: import_koishi9.Schema.number().default(300 * 1e3),
1862
- proxyMode: import_koishi9.Schema.union([
1863
- import_koishi9.Schema.const("system"),
1864
- import_koishi9.Schema.const("off"),
1865
- import_koishi9.Schema.const("on")
1975
+ maxRetries: import_koishi10.Schema.number().min(1).max(6).default(3),
1976
+ timeout: import_koishi10.Schema.number().default(300 * 1e3),
1977
+ proxyMode: import_koishi10.Schema.union([
1978
+ import_koishi10.Schema.const("system"),
1979
+ import_koishi10.Schema.const("off"),
1980
+ import_koishi10.Schema.const("on")
1866
1981
  ]).default("system")
1867
1982
  }),
1868
- import_koishi9.Schema.union([
1869
- import_koishi9.Schema.object({
1870
- proxyMode: import_koishi9.Schema.const("on").required(),
1871
- proxyAddress: import_koishi9.Schema.string().default("")
1983
+ import_koishi10.Schema.union([
1984
+ import_koishi10.Schema.object({
1985
+ proxyMode: import_koishi10.Schema.const("on").required(),
1986
+ proxyAddress: import_koishi10.Schema.string().default("")
1872
1987
  }),
1873
- import_koishi9.Schema.object({})
1988
+ import_koishi10.Schema.object({})
1874
1989
  ])
1875
1990
  ]).i18n({
1876
1991
  "zh-CN": require_zh_CN_schema_plugin(),
@@ -23,7 +23,7 @@ var require_en_US_schema_plugin = __commonJS({
23
23
  import { HumanMessage } from "@langchain/core/messages";
24
24
  import fs from "fs";
25
25
  import {
26
- Schema as Schema7,
26
+ Schema as Schema8,
27
27
  Service
28
28
  } from "koishi";
29
29
  import { ChatInterface } from "koishi-plugin-chatluna/llm-core/chat/app";
@@ -780,7 +780,7 @@ Please consider this quote when generating your response. User's message: ${mess
780
780
  import { chatLunaFetch, ws } from "koishi-plugin-chatluna/utils/request";
781
781
 
782
782
  // src/render.ts
783
- import { h as h7, Schema as Schema6 } from "koishi";
783
+ import { h as h8, Schema as Schema7 } from "koishi";
784
784
  import {
785
785
  ChatLunaError as ChatLunaError3,
786
786
  ChatLunaErrorCode as ChatLunaErrorCode3
@@ -1057,6 +1057,120 @@ function renderTokens2(tokens) {
1057
1057
  }
1058
1058
  __name(renderTokens2, "renderTokens");
1059
1059
 
1060
+ // src/renders/pure-text.ts
1061
+ import { transform as transform2 } from "koishi-plugin-markdown";
1062
+ import { h as h7, Schema as Schema6 } from "koishi";
1063
+
1064
+ // src/utils/remove-markdown.ts
1065
+ function removeMarkdown(md, options = {}) {
1066
+ options.listUnicodeChar = Object.prototype.hasOwnProperty.call(
1067
+ options,
1068
+ "listUnicodeChar"
1069
+ ) ? options.listUnicodeChar : false;
1070
+ options.stripListLeaders = Object.prototype.hasOwnProperty.call(
1071
+ options,
1072
+ "stripListLeaders"
1073
+ ) ? options.stripListLeaders : true;
1074
+ options.gfm = Object.prototype.hasOwnProperty.call(options, "gfm") ? options.gfm : true;
1075
+ options.useImgAltText = Object.prototype.hasOwnProperty.call(
1076
+ options,
1077
+ "useImgAltText"
1078
+ ) ? options.useImgAltText : true;
1079
+ options.abbr = Object.prototype.hasOwnProperty.call(options, "abbr") ? options.abbr : false;
1080
+ options.replaceLinksWithURL = Object.prototype.hasOwnProperty.call(
1081
+ options,
1082
+ "replaceLinksWithURL"
1083
+ ) ? options.replaceLinksWithURL : false;
1084
+ options.htmlTagsToSkip = Object.prototype.hasOwnProperty.call(
1085
+ options,
1086
+ "htmlTagsToSkip"
1087
+ ) ? options.htmlTagsToSkip : [];
1088
+ options.throwError = Object.prototype.hasOwnProperty.call(
1089
+ options,
1090
+ "throwError"
1091
+ ) ? options.throwError : false;
1092
+ let output = md || "";
1093
+ output = output.replace(
1094
+ /^ {0,3}((?:-[\t ]*){3,}|(?:_[ \t]*){3,}|(?:\*[ \t]*){3,})(?:\n+|$)/gm,
1095
+ ""
1096
+ );
1097
+ try {
1098
+ if (options.stripListLeaders) {
1099
+ if (options.listUnicodeChar)
1100
+ output = output.replace(
1101
+ /^([\s\t]*)([\*\-\+]|\d+\.)\s+/gm,
1102
+ options.listUnicodeChar + " $1"
1103
+ );
1104
+ else
1105
+ output = output.replace(/^([\s\t]*)([\*\-\+]|\d+\.)\s+/gm, "$1");
1106
+ }
1107
+ if (options.gfm) {
1108
+ output = output.replace(/\n={2,}/g, "\n").replace(/~{3}.*\n/g, "").replace(/~~/g, "").replace(/```(?:.*)\n([\s\S]*?)```/g, (_, code) => code.trim());
1109
+ }
1110
+ if (options.abbr) {
1111
+ output = output.replace(/\*\[.*\]:.*\n/, "");
1112
+ }
1113
+ let htmlReplaceRegex = /<[^>]*>/g;
1114
+ if (options.htmlTagsToSkip && options.htmlTagsToSkip.length > 0) {
1115
+ const joinedHtmlTagsToSkip = options.htmlTagsToSkip.join("|");
1116
+ htmlReplaceRegex = new RegExp(
1117
+ `<(?!/?(${joinedHtmlTagsToSkip})(?=>|s[^>]*>))[^>]*>`,
1118
+ "g"
1119
+ );
1120
+ }
1121
+ output = output.replace(htmlReplaceRegex, "").replace(/^[=\-]{2,}\s*$/g, "").replace(/\[\^.+?\](\: .*?$)?/g, "").replace(/\s{0,2}\[.*?\]: .*?$/g, "").replace(
1122
+ /\!\[(.*?)\][\[\(].*?[\]\)]/g,
1123
+ options.useImgAltText ? "$1" : ""
1124
+ ).replace(
1125
+ /\[([\s\S]*?)\]\s*[\(\[].*?[\)\]]/g,
1126
+ options.replaceLinksWithURL ? "$2" : "$1"
1127
+ ).replace(/^(\n)?\s{0,3}>\s?/gm, "$1").replace(/^\s{1,2}\[(.*?)\]: (\S+)( ".*?")?\s*$/g, "").replace(
1128
+ /^(\n)?\s{0,}#{1,6}\s*( (.+))? +#+$|^(\n)?\s{0,}#{1,6}\s*( (.+))?$/gm,
1129
+ "$1$3$4$6"
1130
+ ).replace(/([\*]+)(\S)(.*?\S)??\1/g, "$2$3").replace(/(^|\W)([_]+)(\S)(.*?\S)??\2($|\W)/g, "$1$3$4$5").replace(/(`{3,})(.*?)\1/gm, "$2").replace(/`(.+?)`/g, "$1").replace(/~(.*?)~/g, "$1");
1131
+ } catch (e) {
1132
+ if (options.throwError) throw e;
1133
+ console.error("remove-markdown encountered error: %s", e);
1134
+ return md;
1135
+ }
1136
+ return output;
1137
+ }
1138
+ __name(removeMarkdown, "removeMarkdown");
1139
+
1140
+ // src/renders/pure-text.ts
1141
+ import he3 from "he";
1142
+ var PureTextRenderer = class extends Renderer {
1143
+ static {
1144
+ __name(this, "PureTextRenderer");
1145
+ }
1146
+ async render(message, options) {
1147
+ let transformed = [h7.text(message.content)];
1148
+ if (options.split) {
1149
+ transformed = transformed.flatMap((element) => {
1150
+ const content = element.attrs["content"];
1151
+ return content.split("\n\n\n").map((paragraph) => {
1152
+ return h7.text(paragraph);
1153
+ });
1154
+ });
1155
+ }
1156
+ transformed = transformed.map((element) => {
1157
+ const content = element.attrs["content"];
1158
+ return h7.text(stripMarkdown(content));
1159
+ });
1160
+ return {
1161
+ element: transformed
1162
+ };
1163
+ }
1164
+ schema = Schema6.const("pure-text").i18n({
1165
+ "zh-CN": "将回复渲染为纯文本(去除 markdown 格式)",
1166
+ "en-US": "Render as pure text (remove markdown format)"
1167
+ });
1168
+ };
1169
+ function stripMarkdown(source) {
1170
+ return removeMarkdown(source);
1171
+ }
1172
+ __name(stripMarkdown, "stripMarkdown");
1173
+
1060
1174
  // src/render.ts
1061
1175
  var DefaultRenderer = class {
1062
1176
  constructor(ctx, config) {
@@ -1078,6 +1192,7 @@ var DefaultRenderer = class {
1078
1192
  "koishi-element",
1079
1193
  () => new KoishiElementRenderer(ctx2)
1080
1194
  );
1195
+ this.addRenderer("pure-text", () => new PureTextRenderer(ctx2));
1081
1196
  });
1082
1197
  }
1083
1198
  static {
@@ -1094,7 +1209,7 @@ var DefaultRenderer = class {
1094
1209
  for (const additionalMessage of message.additionalReplyMessages) {
1095
1210
  const elements = await rawRenderer.render(additionalMessage, options).then((r) => r.element);
1096
1211
  result.push({
1097
- element: h7(
1212
+ element: h8(
1098
1213
  "message",
1099
1214
  { forward: true },
1100
1215
  Array.isArray(elements) ? elements : [elements]
@@ -1126,7 +1241,7 @@ var DefaultRenderer = class {
1126
1241
  }
1127
1242
  this.ctx.schema.set(
1128
1243
  "output-mode",
1129
- Schema6.union(this._getAllRendererScheme())
1244
+ Schema7.union(this._getAllRendererScheme())
1130
1245
  );
1131
1246
  }
1132
1247
  _getAllRendererScheme() {
@@ -1841,28 +1956,28 @@ ${reasoningContent}`
1841
1956
  }
1842
1957
  };
1843
1958
  ((ChatLunaPlugin2) => {
1844
- ChatLunaPlugin2.Config = Schema7.intersect([
1845
- Schema7.object({
1846
- chatConcurrentMaxSize: Schema7.number().min(1).max(8).default(3),
1847
- chatTimeLimit: Schema7.number().min(1).max(2e3).computed().default(200),
1848
- configMode: Schema7.union([
1849
- Schema7.const("default"),
1850
- Schema7.const("balance")
1959
+ ChatLunaPlugin2.Config = Schema8.intersect([
1960
+ Schema8.object({
1961
+ chatConcurrentMaxSize: Schema8.number().min(1).max(8).default(3),
1962
+ chatTimeLimit: Schema8.number().min(1).max(2e3).computed().default(200),
1963
+ configMode: Schema8.union([
1964
+ Schema8.const("default"),
1965
+ Schema8.const("balance")
1851
1966
  ]).default("default"),
1852
- maxRetries: Schema7.number().min(1).max(6).default(3),
1853
- timeout: Schema7.number().default(300 * 1e3),
1854
- proxyMode: Schema7.union([
1855
- Schema7.const("system"),
1856
- Schema7.const("off"),
1857
- Schema7.const("on")
1967
+ maxRetries: Schema8.number().min(1).max(6).default(3),
1968
+ timeout: Schema8.number().default(300 * 1e3),
1969
+ proxyMode: Schema8.union([
1970
+ Schema8.const("system"),
1971
+ Schema8.const("off"),
1972
+ Schema8.const("on")
1858
1973
  ]).default("system")
1859
1974
  }),
1860
- Schema7.union([
1861
- Schema7.object({
1862
- proxyMode: Schema7.const("on").required(),
1863
- proxyAddress: Schema7.string().default("")
1975
+ Schema8.union([
1976
+ Schema8.object({
1977
+ proxyMode: Schema8.const("on").required(),
1978
+ proxyAddress: Schema8.string().default("")
1864
1979
  }),
1865
- Schema7.object({})
1980
+ Schema8.object({})
1866
1981
  ])
1867
1982
  ]).i18n({
1868
1983
  "zh-CN": require_zh_CN_schema_plugin(),
@@ -0,0 +1,12 @@
1
+ export interface RemoveMarkdownOptions {
2
+ listUnicodeChar?: boolean;
3
+ stripListLeaders?: boolean;
4
+ gfm?: boolean;
5
+ useImgAltText?: boolean;
6
+ abbr?: boolean;
7
+ replaceLinksWithURL?: boolean;
8
+ htmlTagsToSkip?: string[];
9
+ throwError?: boolean;
10
+ }
11
+ export declare function removeMarkdown(md: string, options?: RemoveMarkdownOptions): string;
12
+ export default removeMarkdown;
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.2.0-alpha.2",
4
+ "version": "1.2.0-alpha.3",
5
5
  "main": "lib/index.cjs",
6
6
  "module": "lib/index.mjs",
7
7
  "typings": "lib/index.d.ts",