koishi-plugin-chatluna 1.2.2 → 1.3.0-alpha.1

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.
@@ -43,7 +43,7 @@ export declare class ChainMiddleware {
43
43
  constructor(name: string, execute: ChainMiddlewareFunction, graph: ChatChainDependencyGraph);
44
44
  before<T extends keyof ChainMiddlewareName>(name: T): this;
45
45
  after<T extends keyof ChainMiddlewareName>(name: T): this;
46
- run(session: Session, options: ChainMiddlewareContext): Promise<string | h[] | ChainMiddlewareRunStatus | h[][]>;
46
+ run(session: Session, options: ChainMiddlewareContext): Promise<string | h[] | h[][] | ChainMiddlewareRunStatus>;
47
47
  }
48
48
  export interface ChainMiddlewareContext {
49
49
  config: Config;
package/lib/index.cjs CHANGED
@@ -1291,8 +1291,8 @@ async function defaultFactory(ctx, service) {
1291
1291
  service.registerChatChain(
1292
1292
  "plugin",
1293
1293
  {
1294
- "zh-CN": "插件模式(基于 LangChain 的 Agent)",
1295
- "en-US": "Plugin mode (based on LangChain Agent)"
1294
+ "zh-CN": "Agent 模式",
1295
+ "en-US": "Agent mode"
1296
1296
  },
1297
1297
  async (params) => {
1298
1298
  return ChatLunaPluginChain.fromLLMAndTools(
@@ -6517,7 +6517,7 @@ var inject2 = {
6517
6517
  };
6518
6518
  var logger;
6519
6519
  var usage = `
6520
- ## chatluna v1.1
6520
+ ## chatluna v1.3 beta
6521
6521
 
6522
6522
  ChatLuna 插件交流 QQ 群:282381753 (有问题或出现 Bug 先加群问)
6523
6523
 
package/lib/index.d.ts CHANGED
@@ -20,5 +20,5 @@ export declare const inject2: {
20
20
  };
21
21
  };
22
22
  export declare let logger: Logger;
23
- export declare const usage = "\n## chatluna v1.1\n\nChatLuna \u63D2\u4EF6\u4EA4\u6D41 QQ \u7FA4\uFF1A282381753 \uFF08\u6709\u95EE\u9898\u6216\u51FA\u73B0 Bug \u5148\u52A0\u7FA4\u95EE\uFF09\n\n\u7FA4\u91CC\u76EE\u524D\u6CA1\u6709\u642D\u8F7D\u8BE5\u63D2\u4EF6\u7684 bot\uFF0C\u52A0\u7FA4\u7684\u8BDD\u6700\u597D\u662F\u6765\u8BE2\u95EE\u95EE\u9898\u6216\u8005\u63D0\u51FA\u610F\u89C1\u7684\u3002\n\n\u8BBF\u95EE [https://chatluna.chat](https://chatluna.chat) \u6765\u4E86\u89E3\u5982\u4F55\u4F7F\u7528 Chatluna\u3002\n\u4E5F\u53EF\u4EE5\u8BBF\u95EE [https://preset.chatluna.chat](https://preset.chatluna.chat) \u8FDB\u5165\u5728\u7EBF\u9884\u8BBE\u7F16\u8F91\u5668\u3002\u66F4\u6709\u9884\u8BBE\u5E7F\u573A\u6765\u6D4F\u89C8\u548C\u4E0B\u8F7D\u4F60\u5FC3\u4EEA\u7684\u9884\u8BBE\u3002\n";
23
+ export declare const usage = "\n## chatluna v1.3 beta\n\nChatLuna \u63D2\u4EF6\u4EA4\u6D41 QQ \u7FA4\uFF1A282381753 \uFF08\u6709\u95EE\u9898\u6216\u51FA\u73B0 Bug \u5148\u52A0\u7FA4\u95EE\uFF09\n\n\u7FA4\u91CC\u76EE\u524D\u6CA1\u6709\u642D\u8F7D\u8BE5\u63D2\u4EF6\u7684 bot\uFF0C\u52A0\u7FA4\u7684\u8BDD\u6700\u597D\u662F\u6765\u8BE2\u95EE\u95EE\u9898\u6216\u8005\u63D0\u51FA\u610F\u89C1\u7684\u3002\n\n\u8BBF\u95EE [https://chatluna.chat](https://chatluna.chat) \u6765\u4E86\u89E3\u5982\u4F55\u4F7F\u7528 Chatluna\u3002\n\u4E5F\u53EF\u4EE5\u8BBF\u95EE [https://preset.chatluna.chat](https://preset.chatluna.chat) \u8FDB\u5165\u5728\u7EBF\u9884\u8BBE\u7F16\u8F91\u5668\u3002\u66F4\u6709\u9884\u8BBE\u5E7F\u573A\u6765\u6D4F\u89C8\u548C\u4E0B\u8F7D\u4F60\u5FC3\u4EEA\u7684\u9884\u8BBE\u3002\n";
24
24
  export declare function apply(ctx: Context, config: Config): void;
package/lib/index.mjs CHANGED
@@ -1275,8 +1275,8 @@ async function defaultFactory(ctx, service) {
1275
1275
  service.registerChatChain(
1276
1276
  "plugin",
1277
1277
  {
1278
- "zh-CN": "插件模式(基于 LangChain 的 Agent)",
1279
- "en-US": "Plugin mode (based on LangChain Agent)"
1278
+ "zh-CN": "Agent 模式",
1279
+ "en-US": "Agent mode"
1280
1280
  },
1281
1281
  async (params) => {
1282
1282
  return ChatLunaPluginChain.fromLLMAndTools(
@@ -6524,7 +6524,7 @@ var inject2 = {
6524
6524
  };
6525
6525
  var logger;
6526
6526
  var usage = `
6527
- ## chatluna v1.1
6527
+ ## chatluna v1.3 beta
6528
6528
 
6529
6529
  ChatLuna 插件交流 QQ 群:282381753 (有问题或出现 Bug 先加群问)
6530
6530
 
@@ -22,4 +22,4 @@ export type CreateOpenAIAgentParams = {
22
22
  };
23
23
  export declare function createOpenAIAgent({ llm, tools, prompt }: CreateOpenAIAgentParams): RunnableSequence<{
24
24
  steps: AgentStep[];
25
- }, AgentAction | AgentFinish | AgentAction[]>;
25
+ }, AgentAction | AgentAction[] | AgentFinish>;
@@ -72,7 +72,7 @@ export type CreateReactAgentParams = {
72
72
  */
73
73
  export declare function createReactAgent({ llm, tools, prompt, streamRunnable, instructions }: CreateReactAgentParams): Promise<AgentRunnableSequence<{
74
74
  steps: AgentStep[];
75
- }, import("@langchain/core/agents").AgentFinish | import("@langchain/core/agents").AgentAction[]>>;
75
+ }, import("@langchain/core/agents").AgentAction[] | import("@langchain/core/agents").AgentFinish>>;
76
76
  /**
77
77
  * Construct the scratchpad that lets the agent continue its thought process.
78
78
  * @param intermediateSteps
@@ -41,7 +41,7 @@ var BasePlatformClient = class {
41
41
  await this.init();
42
42
  return true;
43
43
  } catch (e) {
44
- this.ctx.chatluna["logger"].error(e);
44
+ this.ctx.logger.error(e);
45
45
  if (i === this.config.maxRetries - 1) {
46
46
  return false;
47
47
  }
@@ -17,7 +17,7 @@ var BasePlatformClient = class {
17
17
  await this.init();
18
18
  return true;
19
19
  } catch (e) {
20
- this.ctx.chatluna["logger"].error(e);
20
+ this.ctx.logger.error(e);
21
21
  if (i === this.config.maxRetries - 1) {
22
22
  return false;
23
23
  }
@@ -343,8 +343,18 @@ var ChatLunaChatModel = class extends import_chat_models.BaseChatModel {
343
343
  }
344
344
  async _withTimeout(func, timeout) {
345
345
  const { promise, resolve, reject } = (0, import_promise.withResolver)();
346
+ let timeoutError = null;
347
+ try {
348
+ throw new import_error.ChatLunaError(
349
+ import_error.ChatLunaErrorCode.API_REQUEST_TIMEOUT,
350
+ null,
351
+ true
352
+ );
353
+ } catch (e) {
354
+ timeoutError = e;
355
+ }
346
356
  const timeoutId = setTimeout(() => {
347
- reject(new import_error.ChatLunaError(import_error.ChatLunaErrorCode.API_REQUEST_TIMEOUT));
357
+ reject(timeoutError);
348
358
  }, timeout);
349
359
  (0, import_promise.runAsync)(async () => {
350
360
  let result;
@@ -577,16 +587,24 @@ var ChatLunaEmbeddings = class extends ChatHubBaseEmbeddings {
577
587
  async _embeddingWithRetry(request) {
578
588
  request.timeout = request.timeout ?? this.timeout;
579
589
  try {
590
+ let timeoutError = null;
591
+ try {
592
+ throw new import_error.ChatLunaError(
593
+ import_error.ChatLunaErrorCode.API_REQUEST_TIMEOUT,
594
+ new Error(
595
+ `timeout when calling ${this.modelName} embeddings`
596
+ ),
597
+ true
598
+ );
599
+ } catch (e) {
600
+ timeoutError = e;
601
+ }
580
602
  return await this.caller.call(
581
603
  (request2) => {
582
604
  return Promise.race([
583
605
  new Promise((resolve, reject) => {
584
606
  setTimeout(() => {
585
- reject(
586
- Error(
587
- `timeout when calling ${this.modelName} embeddings`
588
- )
589
- );
607
+ reject(timeoutError);
590
608
  }, request2.timeout);
591
609
  }),
592
610
  new Promise(
@@ -325,8 +325,18 @@ var ChatLunaChatModel = class extends BaseChatModel {
325
325
  }
326
326
  async _withTimeout(func, timeout) {
327
327
  const { promise, resolve, reject } = withResolver();
328
+ let timeoutError = null;
329
+ try {
330
+ throw new ChatLunaError(
331
+ ChatLunaErrorCode.API_REQUEST_TIMEOUT,
332
+ null,
333
+ true
334
+ );
335
+ } catch (e) {
336
+ timeoutError = e;
337
+ }
328
338
  const timeoutId = setTimeout(() => {
329
- reject(new ChatLunaError(ChatLunaErrorCode.API_REQUEST_TIMEOUT));
339
+ reject(timeoutError);
330
340
  }, timeout);
331
341
  runAsync(async () => {
332
342
  let result;
@@ -559,16 +569,24 @@ var ChatLunaEmbeddings = class extends ChatHubBaseEmbeddings {
559
569
  async _embeddingWithRetry(request) {
560
570
  request.timeout = request.timeout ?? this.timeout;
561
571
  try {
572
+ let timeoutError = null;
573
+ try {
574
+ throw new ChatLunaError(
575
+ ChatLunaErrorCode.API_REQUEST_TIMEOUT,
576
+ new Error(
577
+ `timeout when calling ${this.modelName} embeddings`
578
+ ),
579
+ true
580
+ );
581
+ } catch (e) {
582
+ timeoutError = e;
583
+ }
562
584
  return await this.caller.call(
563
585
  (request2) => {
564
586
  return Promise.race([
565
587
  new Promise((resolve, reject) => {
566
588
  setTimeout(() => {
567
- reject(
568
- Error(
569
- `timeout when calling ${this.modelName} embeddings`
570
- )
571
- );
589
+ reject(timeoutError);
572
590
  }, request2.timeout);
573
591
  }),
574
592
  new Promise(
package/lib/preset.cjs CHANGED
@@ -191,6 +191,12 @@ var PresetService = class {
191
191
  );
192
192
  }
193
193
  async addPreset(preset) {
194
+ if (this._presets.some(
195
+ (p) => p.triggerKeyword.join(",") === preset.triggerKeyword.join(",")
196
+ )) {
197
+ logger.warn(`preset ${preset.path} already exists`);
198
+ return;
199
+ }
194
200
  this._presets.push(preset);
195
201
  this._updateSchema();
196
202
  }
package/lib/preset.mjs CHANGED
@@ -164,6 +164,12 @@ var PresetService = class {
164
164
  );
165
165
  }
166
166
  async addPreset(preset) {
167
+ if (this._presets.some(
168
+ (p) => p.triggerKeyword.join(",") === preset.triggerKeyword.join(",")
169
+ )) {
170
+ logger.warn(`preset ${preset.path} already exists`);
171
+ return;
172
+ }
167
173
  this._presets.push(preset);
168
174
  this._updateSchema();
169
175
  }
@@ -1564,11 +1564,17 @@ var ChatLunaService = class extends import_koishi11.Service {
1564
1564
  resolve();
1565
1565
  return promise;
1566
1566
  }
1567
+ let timeoutError = null;
1568
+ try {
1569
+ throw new Error(
1570
+ `Timeout waiting for platform ${pluginName} to load`
1571
+ );
1572
+ } catch (e) {
1573
+ timeoutError = e;
1574
+ }
1567
1575
  const timeoutId = setTimeout(() => {
1568
1576
  dispose();
1569
- reject(
1570
- new Error(`Timeout waiting for platform ${pluginName} to load`)
1571
- );
1577
+ reject(timeoutError);
1572
1578
  }, timeout);
1573
1579
  const dispose = this.ctx.on(
1574
1580
  "chatluna/model-added",
@@ -1565,11 +1565,17 @@ var ChatLunaService = class extends Service {
1565
1565
  resolve();
1566
1566
  return promise;
1567
1567
  }
1568
+ let timeoutError = null;
1569
+ try {
1570
+ throw new Error(
1571
+ `Timeout waiting for platform ${pluginName} to load`
1572
+ );
1573
+ } catch (e) {
1574
+ timeoutError = e;
1575
+ }
1568
1576
  const timeoutId = setTimeout(() => {
1569
1577
  dispose();
1570
- reject(
1571
- new Error(`Timeout waiting for platform ${pluginName} to load`)
1572
- );
1578
+ reject(timeoutError);
1573
1579
  }, timeout);
1574
1580
  const dispose = this.ctx.on(
1575
1581
  "chatluna/model-added",
@@ -34,21 +34,24 @@ var setErrorFormatTemplate = /* @__PURE__ */ __name((template) => {
34
34
  }
35
35
  }, "setErrorFormatTemplate");
36
36
  var ChatLunaError = class extends Error {
37
- constructor(errorCode = 999 /* UNKNOWN_ERROR */, originError) {
37
+ constructor(errorCode = 999 /* UNKNOWN_ERROR */, originError, isTimeout = false) {
38
38
  super(ERROR_FORMAT_TEMPLATE.replace("%s", errorCode.toString()));
39
39
  this.errorCode = errorCode;
40
40
  this.originError = originError;
41
+ this.isTimeout = isTimeout;
41
42
  this.name = "ChatLunaError";
42
43
  const logger = import_koishi_plugin_chatluna.logger ?? console;
43
- logger.error(
44
- "=".repeat(20) + "ChatLunaError:" + errorCode + "=".repeat(20)
45
- );
46
- if (originError) {
44
+ if (!isTimeout) {
45
+ logger.error(
46
+ "=".repeat(20) + "ChatLunaError:" + errorCode + "=".repeat(20)
47
+ );
48
+ }
49
+ if (originError && !isTimeout) {
47
50
  logger.error(originError);
48
51
  if (originError.cause) {
49
52
  logger.error(originError.cause);
50
53
  }
51
- } else {
54
+ } else if (!isTimeout) {
52
55
  logger.error(this);
53
56
  }
54
57
  }
@@ -3,7 +3,8 @@ export declare const setErrorFormatTemplate: (template: string | null) => void;
3
3
  export declare class ChatLunaError extends Error {
4
4
  errorCode: ChatLunaErrorCode;
5
5
  originError?: Error;
6
- constructor(errorCode?: ChatLunaErrorCode, originError?: Error);
6
+ isTimeout: boolean;
7
+ constructor(errorCode?: ChatLunaErrorCode, originError?: Error, isTimeout?: boolean);
7
8
  toString(): string;
8
9
  }
9
10
  export declare enum ChatLunaErrorCode {
@@ -10,21 +10,24 @@ var setErrorFormatTemplate = /* @__PURE__ */ __name((template) => {
10
10
  }
11
11
  }, "setErrorFormatTemplate");
12
12
  var ChatLunaError = class extends Error {
13
- constructor(errorCode = 999 /* UNKNOWN_ERROR */, originError) {
13
+ constructor(errorCode = 999 /* UNKNOWN_ERROR */, originError, isTimeout = false) {
14
14
  super(ERROR_FORMAT_TEMPLATE.replace("%s", errorCode.toString()));
15
15
  this.errorCode = errorCode;
16
16
  this.originError = originError;
17
+ this.isTimeout = isTimeout;
17
18
  this.name = "ChatLunaError";
18
19
  const logger = koishiLogger ?? console;
19
- logger.error(
20
- "=".repeat(20) + "ChatLunaError:" + errorCode + "=".repeat(20)
21
- );
22
- if (originError) {
20
+ if (!isTimeout) {
21
+ logger.error(
22
+ "=".repeat(20) + "ChatLunaError:" + errorCode + "=".repeat(20)
23
+ );
24
+ }
25
+ if (originError && !isTimeout) {
23
26
  logger.error(originError);
24
27
  if (originError.cause) {
25
28
  logger.error(originError.cause);
26
29
  }
27
- } else {
30
+ } else if (!isTimeout) {
28
31
  logger.error(this);
29
32
  }
30
33
  }
@@ -142,14 +142,18 @@ var RequestIdQueue = class {
142
142
  }
143
143
  if (item) {
144
144
  let timeoutId;
145
+ let timeoutError = null;
146
+ try {
147
+ throw new Error(
148
+ `Queue wait timeout after ${this._queueTimeout}ms`
149
+ );
150
+ } catch (e) {
151
+ timeoutError = e;
152
+ }
145
153
  try {
146
154
  const timeoutPromise = new Promise((_, reject) => {
147
155
  timeoutId = setTimeout(() => {
148
- reject(
149
- new Error(
150
- `Queue wait timeout after ${this._queueTimeout}ms`
151
- )
152
- );
156
+ reject(timeoutError);
153
157
  }, this._queueTimeout);
154
158
  });
155
159
  await Promise.race([item.notifyPromise.promise, timeoutPromise]);
@@ -124,14 +124,18 @@ var RequestIdQueue = class {
124
124
  }
125
125
  if (item) {
126
126
  let timeoutId;
127
+ let timeoutError = null;
128
+ try {
129
+ throw new Error(
130
+ `Queue wait timeout after ${this._queueTimeout}ms`
131
+ );
132
+ } catch (e) {
133
+ timeoutError = e;
134
+ }
127
135
  try {
128
136
  const timeoutPromise = new Promise((_, reject) => {
129
137
  timeoutId = setTimeout(() => {
130
- reject(
131
- new Error(
132
- `Queue wait timeout after ${this._queueTimeout}ms`
133
- )
134
- );
138
+ reject(timeoutError);
135
139
  }, this._queueTimeout);
136
140
  });
137
141
  await Promise.race([item.notifyPromise.promise, timeoutPromise]);
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.2",
4
+ "version": "1.3.0-alpha.1",
5
5
  "main": "lib/index.cjs",
6
6
  "module": "lib/index.mjs",
7
7
  "typings": "lib/index.d.ts",
@@ -5,7 +5,7 @@ keywords:
5
5
  prompts:
6
6
  - role: system
7
7
  content: |-
8
- You are ChatGPT, a large language model trained by OpenAI, based on the GPT-4 architecture.
8
+ You are ChatGPT, a large language model trained by OpenAI.
9
9
  Knowledge cutoff: 2024-06 Current date: {date}
10
10
 
11
11
  Structure your responses with iterative thinking: