koishi-plugin-openai-compatible 1.1.0 → 1.1.2

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.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Context, Logger } from 'koishi';
1
+ import { Context, Schema, Logger } from 'koishi';
2
2
  import { Config, EmotionType, EmojiConfig } from './types';
3
3
  declare module 'koishi' {
4
4
  interface Context {
@@ -36,7 +36,7 @@ declare class OpenAICompatible {
36
36
  private cooldownManager;
37
37
  private logger;
38
38
  constructor(ctx: Context, config: Config);
39
- private ensureConfigDefaults;
39
+ private normalizeConfig;
40
40
  private registerService;
41
41
  private registerCommand;
42
42
  chat(message: string, options?: {
@@ -68,5 +68,8 @@ declare class OpenAICompatible {
68
68
  }
69
69
  export { OpenAICompatible, Config, EmotionAnalyzer };
70
70
  export * from './types';
71
- declare const _default: (ctx: Context, config: Config) => OpenAICompatible;
72
- export default _default;
71
+ declare const plugin: {
72
+ (ctx: Context, config: Config): OpenAICompatible;
73
+ Config: Schema<Config>;
74
+ };
75
+ export default plugin;
package/lib/index.js CHANGED
@@ -47,10 +47,11 @@ class CooldownManager {
47
47
  // OpenAI兼容客户端
48
48
  class OpenAICompatibleClient {
49
49
  constructor(config, logger, isEmotion = false) {
50
+ var _a;
50
51
  this.config = config;
51
52
  this.logger = logger.extend(isEmotion ? 'emotion-client' : 'client');
52
53
  // 如果是情绪分析客户端,使用情绪分析配置(如果提供)
53
- if (isEmotion && config.emotionAnalysis.enabled) {
54
+ if (isEmotion && ((_a = config.emotionAnalysis) === null || _a === void 0 ? void 0 : _a.enabled)) {
54
55
  // 使用情绪分析配置,如果为空则使用主配置
55
56
  const emotionEndpoint = config.emotionAnalysis.endpoint || config.endpoint;
56
57
  this.endpoint = (emotionEndpoint || 'https://api.openai.com/v1').replace(/\/$/, '');
@@ -83,8 +84,12 @@ class OpenAICompatibleClient {
83
84
  // 根据是否为情绪分析设置不同的参数
84
85
  let requestConfig;
85
86
  if (isEmotion) {
87
+ const emotionConfig = this.config.emotionAnalysis || {
88
+ model: 'gpt-3.5-turbo',
89
+ prompt: ''
90
+ };
86
91
  requestConfig = {
87
- model: ((_a = config === null || config === void 0 ? void 0 : config.emotionAnalysis) === null || _a === void 0 ? void 0 : _a.model) || this.config.emotionAnalysis.model,
92
+ model: ((_a = config === null || config === void 0 ? void 0 : config.emotionAnalysis) === null || _a === void 0 ? void 0 : _a.model) || emotionConfig.model,
88
93
  messages,
89
94
  max_tokens: (config === null || config === void 0 ? void 0 : config.emotionMaxTokens) || this.config.emotionMaxTokens,
90
95
  temperature: (config === null || config === void 0 ? void 0 : config.emotionTemperature) || this.config.emotionTemperature,
@@ -180,7 +185,8 @@ class EmotionAnalyzer {
180
185
  this.imageAsMarkdown = config.imageAsMarkdown;
181
186
  }
182
187
  async analyze(text) {
183
- if (!this.config.emotionAnalysis.enabled || !text.trim()) {
188
+ var _a;
189
+ if (!((_a = this.config.emotionAnalysis) === null || _a === void 0 ? void 0 : _a.enabled) || !text.trim()) {
184
190
  const neutralConfig = this.emotionEmojis['neutral'] || { text: '😐', image: '' };
185
191
  return {
186
192
  emotion: 'neutral',
@@ -329,40 +335,40 @@ class OpenAICompatible {
329
335
  this.config = config;
330
336
  this.logger = ctx.logger('openai-compatible');
331
337
  // 确保配置有默认值
332
- this.ensureConfigDefaults();
338
+ this.config = this.normalizeConfig(config);
333
339
  this.client = new OpenAICompatibleClient(this.config, this.logger);
334
340
  this.emotionAnalyzer = new EmotionAnalyzer(this.config, this.logger);
335
341
  this.cooldownManager = new CooldownManager(this.config.cooldown);
336
342
  this.registerCommand();
337
343
  this.registerService();
338
344
  }
339
- ensureConfigDefaults() {
345
+ normalizeConfig(config) {
346
+ // 创建一个深拷贝,确保所有配置都有默认值
347
+ const normalized = { ...config };
340
348
  // 确保所有必要的配置都有默认值
341
- if (!this.config.endpoint) {
342
- this.config.endpoint = 'https://api.openai.com/v1';
349
+ if (!normalized.endpoint) {
350
+ normalized.endpoint = 'https://api.openai.com/v1';
343
351
  }
344
- if (!this.config.model) {
345
- this.config.model = 'gpt-3.5-turbo';
352
+ if (!normalized.model) {
353
+ normalized.model = 'gpt-3.5-turbo';
346
354
  }
347
- if (!this.config.apiKey) {
348
- this.config.apiKey = '';
355
+ if (!normalized.apiKey) {
356
+ normalized.apiKey = '';
349
357
  this.logger.warn('API密钥未配置,插件将无法正常工作');
350
358
  }
351
359
  // 确保情绪分析配置有默认值
352
- if (this.config.emotionAnalysis) {
353
- if (!this.config.emotionAnalysis.endpoint) {
354
- this.config.emotionAnalysis.endpoint = '';
355
- }
356
- if (!this.config.emotionAnalysis.apiKey) {
357
- this.config.emotionAnalysis.apiKey = '';
358
- }
359
- if (!this.config.emotionAnalysis.model) {
360
- this.config.emotionAnalysis.model = 'gpt-3.5-turbo';
361
- }
360
+ if (!normalized.emotionAnalysis) {
361
+ normalized.emotionAnalysis = {
362
+ enabled: true,
363
+ endpoint: '',
364
+ apiKey: '',
365
+ model: 'gpt-3.5-turbo',
366
+ prompt: '请分析以下文本的情绪类型,只能返回以下情绪类型之一:happy(开心)、sad(悲伤)、angry(愤怒)、neutral(中性)、surprised(惊讶)、fearful(害怕)、disgusted(厌恶)、excited(兴奋)、calm(平静)、confused(困惑)。不要返回其他内容,只返回情绪类型单词。',
367
+ };
362
368
  }
363
369
  // 确保情绪表情配置有默认值
364
- if (!this.config.emotionEmojis) {
365
- this.config.emotionEmojis = {
370
+ if (!normalized.emotionEmojis) {
371
+ normalized.emotionEmojis = {
366
372
  'happy': { text: '😊', image: '' },
367
373
  'sad': { text: '😢', image: '' },
368
374
  'angry': { text: '😠', image: '' },
@@ -375,6 +381,23 @@ class OpenAICompatible {
375
381
  'confused': { text: '😕', image: '' },
376
382
  };
377
383
  }
384
+ // 确保其他配置有默认值
385
+ if (normalized.blacklist === undefined) {
386
+ normalized.blacklist = [];
387
+ }
388
+ if (normalized.cooldown === undefined) {
389
+ normalized.cooldown = 0;
390
+ }
391
+ if (normalized.showError === undefined) {
392
+ normalized.showError = true;
393
+ }
394
+ if (normalized.showEmotionImage === undefined) {
395
+ normalized.showEmotionImage = true;
396
+ }
397
+ if (normalized.imageAsMarkdown === undefined) {
398
+ normalized.imageAsMarkdown = false;
399
+ }
400
+ return normalized;
378
401
  }
379
402
  registerService() {
380
403
  this.ctx.openai = this;
@@ -733,6 +756,10 @@ class OpenAICompatible {
733
756
  }
734
757
  exports.OpenAICompatible = OpenAICompatible;
735
758
  __exportStar(require("./types"), exports);
736
- exports.default = (ctx, config) => {
759
+ // Koishi 插件导出格式
760
+ const plugin = (ctx, config) => {
737
761
  return new OpenAICompatible(ctx, config);
738
762
  };
763
+ // 为插件添加 Config 属性
764
+ plugin.Config = types_1.Config;
765
+ exports.default = plugin;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "koishi-plugin-openai-compatible",
3
- "version": "1.1.0",
3
+ "version": "1.1.2",
4
4
  "description": "这是一个适用于Koishi的OpenAI兼容聊天插件,支持与所有兼容OpenAI API的大模型进行聊天交互。",
5
5
  "main": "lib/index.js",
6
6
  "scripts": {
package/src/index.ts CHANGED
@@ -56,7 +56,7 @@ class OpenAICompatibleClient {
56
56
  this.logger = logger.extend(isEmotion ? 'emotion-client' : 'client')
57
57
 
58
58
  // 如果是情绪分析客户端,使用情绪分析配置(如果提供)
59
- if (isEmotion && config.emotionAnalysis.enabled) {
59
+ if (isEmotion && config.emotionAnalysis?.enabled) {
60
60
  // 使用情绪分析配置,如果为空则使用主配置
61
61
  const emotionEndpoint = config.emotionAnalysis.endpoint || config.endpoint
62
62
  this.endpoint = (emotionEndpoint || 'https://api.openai.com/v1').replace(/\/$/, '')
@@ -97,8 +97,12 @@ class OpenAICompatibleClient {
97
97
  let requestConfig: any
98
98
 
99
99
  if (isEmotion) {
100
+ const emotionConfig = this.config.emotionAnalysis || {
101
+ model: 'gpt-3.5-turbo',
102
+ prompt: ''
103
+ }
100
104
  requestConfig = {
101
- model: config?.emotionAnalysis?.model || this.config.emotionAnalysis.model,
105
+ model: config?.emotionAnalysis?.model || emotionConfig.model,
102
106
  messages,
103
107
  max_tokens: config?.emotionMaxTokens || this.config.emotionMaxTokens,
104
108
  temperature: config?.emotionTemperature || this.config.emotionTemperature,
@@ -220,7 +224,7 @@ class EmotionAnalyzer {
220
224
  success: boolean
221
225
  error?: string
222
226
  }> {
223
- if (!this.config.emotionAnalysis.enabled || !text.trim()) {
227
+ if (!this.config.emotionAnalysis?.enabled || !text.trim()) {
224
228
  const neutralConfig = this.emotionEmojis['neutral'] || { text: '😐', image: '' }
225
229
  return {
226
230
  emotion: 'neutral',
@@ -403,7 +407,7 @@ class OpenAICompatible {
403
407
  this.logger = ctx.logger('openai-compatible')
404
408
 
405
409
  // 确保配置有默认值
406
- this.ensureConfigDefaults()
410
+ this.config = this.normalizeConfig(config)
407
411
 
408
412
  this.client = new OpenAICompatibleClient(this.config, this.logger)
409
413
  this.emotionAnalyzer = new EmotionAnalyzer(this.config, this.logger)
@@ -413,37 +417,38 @@ class OpenAICompatible {
413
417
  this.registerService()
414
418
  }
415
419
 
416
- private ensureConfigDefaults() {
420
+ private normalizeConfig(config: Config): Config {
421
+ // 创建一个深拷贝,确保所有配置都有默认值
422
+ const normalized = { ...config }
423
+
417
424
  // 确保所有必要的配置都有默认值
418
- if (!this.config.endpoint) {
419
- this.config.endpoint = 'https://api.openai.com/v1'
425
+ if (!normalized.endpoint) {
426
+ normalized.endpoint = 'https://api.openai.com/v1'
420
427
  }
421
428
 
422
- if (!this.config.model) {
423
- this.config.model = 'gpt-3.5-turbo'
429
+ if (!normalized.model) {
430
+ normalized.model = 'gpt-3.5-turbo'
424
431
  }
425
432
 
426
- if (!this.config.apiKey) {
427
- this.config.apiKey = ''
433
+ if (!normalized.apiKey) {
434
+ normalized.apiKey = ''
428
435
  this.logger.warn('API密钥未配置,插件将无法正常工作')
429
436
  }
430
437
 
431
438
  // 确保情绪分析配置有默认值
432
- if (this.config.emotionAnalysis) {
433
- if (!this.config.emotionAnalysis.endpoint) {
434
- this.config.emotionAnalysis.endpoint = ''
435
- }
436
- if (!this.config.emotionAnalysis.apiKey) {
437
- this.config.emotionAnalysis.apiKey = ''
438
- }
439
- if (!this.config.emotionAnalysis.model) {
440
- this.config.emotionAnalysis.model = 'gpt-3.5-turbo'
439
+ if (!normalized.emotionAnalysis) {
440
+ normalized.emotionAnalysis = {
441
+ enabled: true,
442
+ endpoint: '',
443
+ apiKey: '',
444
+ model: 'gpt-3.5-turbo',
445
+ prompt: '请分析以下文本的情绪类型,只能返回以下情绪类型之一:happy(开心)、sad(悲伤)、angry(愤怒)、neutral(中性)、surprised(惊讶)、fearful(害怕)、disgusted(厌恶)、excited(兴奋)、calm(平静)、confused(困惑)。不要返回其他内容,只返回情绪类型单词。',
441
446
  }
442
447
  }
443
448
 
444
449
  // 确保情绪表情配置有默认值
445
- if (!this.config.emotionEmojis) {
446
- this.config.emotionEmojis = {
450
+ if (!normalized.emotionEmojis) {
451
+ normalized.emotionEmojis = {
447
452
  'happy': { text: '😊', image: '' },
448
453
  'sad': { text: '😢', image: '' },
449
454
  'angry': { text: '😠', image: '' },
@@ -456,6 +461,29 @@ class OpenAICompatible {
456
461
  'confused': { text: '😕', image: '' },
457
462
  }
458
463
  }
464
+
465
+ // 确保其他配置有默认值
466
+ if (normalized.blacklist === undefined) {
467
+ normalized.blacklist = []
468
+ }
469
+
470
+ if (normalized.cooldown === undefined) {
471
+ normalized.cooldown = 0
472
+ }
473
+
474
+ if (normalized.showError === undefined) {
475
+ normalized.showError = true
476
+ }
477
+
478
+ if (normalized.showEmotionImage === undefined) {
479
+ normalized.showEmotionImage = true
480
+ }
481
+
482
+ if (normalized.imageAsMarkdown === undefined) {
483
+ normalized.imageAsMarkdown = false
484
+ }
485
+
486
+ return normalized
459
487
  }
460
488
 
461
489
  private registerService() {
@@ -894,9 +922,16 @@ class OpenAICompatible {
894
922
  }
895
923
  }
896
924
 
925
+ // 导出插件
897
926
  export { OpenAICompatible, Config, EmotionAnalyzer }
898
927
  export * from './types'
899
928
 
900
- export default (ctx: Context, config: Config) => {
929
+ // Koishi 插件导出格式
930
+ const plugin = (ctx: Context, config: Config) => {
901
931
  return new OpenAICompatible(ctx, config)
902
- }
932
+ }
933
+
934
+ // 为插件添加 Config 属性
935
+ plugin.Config = Config
936
+
937
+ export default plugin