koishi-plugin-openai-compatible 1.0.0 → 1.0.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.
package/lib/service.js CHANGED
@@ -1,11 +1,7 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
3
  exports.OpenAICompatibleService = void 0;
7
4
  const koishi_1 = require("koishi");
8
- const axios_1 = __importDefault(require("axios"));
9
5
  class OpenAICompatibleService extends koishi_1.Service {
10
6
  constructor(ctx, config) {
11
7
  super(ctx, 'openai-compatible');
@@ -87,7 +83,7 @@ class OpenAICompatibleService extends koishi_1.Service {
87
83
  });
88
84
  try {
89
85
  // 调用API
90
- const response = await axios_1.default.post(`${this.config.apiEndpoint}/chat/completions`, {
86
+ const response = await this.ctx.http.post(`${this.config.apiEndpoint}/chat/completions`, {
91
87
  model: this.config.model,
92
88
  messages: history.messages,
93
89
  temperature: this.config.temperature,
@@ -99,7 +95,7 @@ class OpenAICompatibleService extends koishi_1.Service {
99
95
  }
100
96
  });
101
97
  // 处理响应
102
- const assistantMessage = response.data.choices[0]?.message;
98
+ const assistantMessage = response.choices[0]?.message;
103
99
  if (!assistantMessage || !assistantMessage.content) {
104
100
  throw new Error('API返回的响应格式不正确');
105
101
  }
@@ -114,14 +110,15 @@ class OpenAICompatibleService extends koishi_1.Service {
114
110
  this.ctx.logger('openai-compatible').error('API调用失败:', error);
115
111
  // 从历史记录中移除最后一条用户消息(因为没有得到回复)
116
112
  history.messages.pop();
117
- if (error.response) {
118
- throw new Error(`API调用失败: ${error.response.status} ${error.response.statusText}`);
113
+ // 处理 ctx.http 的错误
114
+ if (error.status) {
115
+ throw new Error(`API调用失败: ${error.status} ${error.statusText || 'Bad Request'}`);
119
116
  }
120
- else if (error.request) {
117
+ else if (error.code === 'ETIMEDOUT' || error.code === 'ECONNABORTED') {
121
118
  throw new Error('API请求超时或无响应');
122
119
  }
123
120
  else {
124
- throw new Error(`请求错误: ${error.message}`);
121
+ throw new Error(`请求错误: ${error.message || '未知错误'}`);
125
122
  }
126
123
  }
127
124
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-openai-compatible",
3
- "version": "1.0.0",
4
- "description": "Koishi plugin compatible with OpenAI API for various AI chat services",
3
+ "version": "1.0.1",
4
+ "description": "这是一个适用于KoishiOpenAI兼容聊天插件,支持与所有兼容OpenAI API的大模型进行聊天交互。",
5
5
  "main": "lib/index.js",
6
6
  "scripts": {
7
7
  "build": "tsc",
@@ -19,7 +19,6 @@
19
19
  "type": "commonjs",
20
20
  "dependencies": {
21
21
  "@koishijs/plugin-help": "^2.4.5",
22
- "axios": "^1.11.0",
23
22
  "koishi": "^4.0.0"
24
23
  },
25
24
  "peerDependencies": {
package/src/service.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  import { Context, Service } from 'koishi'
2
- import axios from 'axios'
3
2
  import { Config, Message, ChatHistory } from './config'
4
3
 
5
4
  export class OpenAICompatibleService extends Service {
@@ -97,7 +96,7 @@ export class OpenAICompatibleService extends Service {
97
96
 
98
97
  try {
99
98
  // 调用API
100
- const response = await axios.post(
99
+ const response = await this.ctx.http.post(
101
100
  `${this.config.apiEndpoint}/chat/completions`,
102
101
  {
103
102
  model: this.config.model,
@@ -114,7 +113,7 @@ export class OpenAICompatibleService extends Service {
114
113
  )
115
114
 
116
115
  // 处理响应
117
- const assistantMessage = response.data.choices[0]?.message
116
+ const assistantMessage = response.choices[0]?.message
118
117
 
119
118
  if (!assistantMessage || !assistantMessage.content) {
120
119
  throw new Error('API返回的响应格式不正确')
@@ -133,12 +132,13 @@ export class OpenAICompatibleService extends Service {
133
132
  // 从历史记录中移除最后一条用户消息(因为没有得到回复)
134
133
  history.messages.pop()
135
134
 
136
- if (error.response) {
137
- throw new Error(`API调用失败: ${error.response.status} ${error.response.statusText}`)
138
- } else if (error.request) {
135
+ // 处理 ctx.http 的错误
136
+ if (error.status) {
137
+ throw new Error(`API调用失败: ${error.status} ${error.statusText || 'Bad Request'}`)
138
+ } else if (error.code === 'ETIMEDOUT' || error.code === 'ECONNABORTED') {
139
139
  throw new Error('API请求超时或无响应')
140
140
  } else {
141
- throw new Error(`请求错误: ${error.message}`)
141
+ throw new Error(`请求错误: ${error.message || '未知错误'}`)
142
142
  }
143
143
  }
144
144
  }