koishi-plugin-satori-ai 1.1.2 → 1.1.4

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/api.d.ts CHANGED
@@ -4,7 +4,6 @@ export declare class APIClient {
4
4
  private ctx;
5
5
  private config;
6
6
  private currentKeyIndex;
7
- private retryCount;
8
7
  constructor(ctx: Context, config: APIConfig);
9
8
  initialize(): Promise<void>;
10
9
  chat(messages: Sat.Msg[]): Promise<{
@@ -18,20 +17,3 @@ export declare class APIClient {
18
17
  private rotateKey;
19
18
  testConnection(): Promise<boolean>;
20
19
  }
21
- interface APIAdapter {
22
- formatRequest(messages: Sat.Msg[]): any;
23
- parseResponse(response: any): string;
24
- }
25
- export declare class OpenAIAdapter implements APIAdapter {
26
- formatRequest(messages: any): {
27
- messages: any;
28
- model: string;
29
- };
30
- parseResponse(response: any): any;
31
- }
32
- export declare class ExtendedAPIClient extends APIClient {
33
- private adapter;
34
- constructor(ctx: Context, config: APIConfig, adapter: APIAdapter);
35
- createPayload(messages: Sat.Msg[]): any;
36
- }
37
- export {};
package/lib/index.js CHANGED
@@ -97,7 +97,6 @@ var APIClient = class {
97
97
  __name(this, "APIClient");
98
98
  }
99
99
  currentKeyIndex = 0;
100
- retryCount = 0;
101
100
  // 启动时测试连接
102
101
  async initialize() {
103
102
  if (!await this.testConnection()) {
@@ -106,16 +105,18 @@ var APIClient = class {
106
105
  }
107
106
  // 发送聊天请求
108
107
  async chat(messages) {
108
+ this.initialize();
109
109
  const payload = this.createPayload(messages);
110
110
  for (let i = 0; i < this.config.keys.length; i++) {
111
111
  try {
112
112
  return await this.tryRequest(payload);
113
113
  } catch (error) {
114
+ if (i == this.config.keys.length - 1) {
115
+ return this.handleAPIError(error);
116
+ }
114
117
  this.rotateKey();
115
- return this.handleAPIError(error);
116
118
  }
117
119
  }
118
- throw new Error("All API keys exhausted");
119
120
  }
120
121
  // 生成请求体
121
122
  createPayload(messages) {
@@ -133,11 +134,22 @@ var APIClient = class {
133
134
  async tryRequest(payload) {
134
135
  const url = `${trimSlash(this.config.baseURL)}/v1/chat/completions`;
135
136
  const headers = this.createHeaders();
136
- const response = await this.ctx.http.post(url, payload, { headers, responseType: "text", timeout: 0 });
137
- logger.info("API response:", response);
138
- const filteredResponse = response.split("\n").filter((line) => line.trim() !== "").join("\n");
139
- const jsonResponse = JSON.parse(filteredResponse);
140
- return { content: jsonResponse.choices[0].message.content, error: false };
137
+ let content;
138
+ for (let i = 0; i < this.config.maxRetryTimes; i++) {
139
+ try {
140
+ const response = await this.ctx.http.post(url, payload, { headers, timeout: 36e5 });
141
+ content = response.choices[0].message.content;
142
+ return { content, error: false };
143
+ } catch (error) {
144
+ if (i == this.config.maxRetryTimes - 1) {
145
+ return this.handleAPIError(error);
146
+ }
147
+ this.handleAPIError(error);
148
+ logger.warn(`API请求失败,重试(第${i}次)中...`);
149
+ continue;
150
+ }
151
+ }
152
+ throw new Error("unreachable");
141
153
  }
142
154
  // 生成请求头
143
155
  createHeaders() {
@@ -150,9 +162,16 @@ var APIClient = class {
150
162
  handleAPIError(error) {
151
163
  if (!isErrorWithMessage(error))
152
164
  throw error;
153
- const status = error.response?.status || 500;
165
+ const status = error.response?.status || 0;
154
166
  const errorCode = error.response?.data?.error?.code || "unknown";
155
167
  const message = error.response?.data?.error?.message || error.message;
168
+ let errorData;
169
+ try {
170
+ errorData = typeof error.response?.data === "string" ? JSON.parse(error.response.data) : error.response?.data;
171
+ } catch (e) {
172
+ errorData = error.response?.data;
173
+ }
174
+ logger.error(`API原始响应: ${JSON.stringify(errorData)}`);
156
175
  logger.error(`API Error [${status}]: ${errorCode} - ${message}`);
157
176
  switch (status) {
158
177
  case 400:
@@ -177,8 +196,8 @@ var APIClient = class {
177
196
  logger.error("api服务器负载过高");
178
197
  return { content: "api服务器负载过高", error: true };
179
198
  default:
180
- logger.error("未知错误");
181
- throw error;
199
+ logger.error(message);
200
+ return { content: message, error: true };
182
201
  }
183
202
  }
184
203
  // 切换API密钥
@@ -189,9 +208,8 @@ var APIClient = class {
189
208
  // 测试连接
190
209
  async testConnection() {
191
210
  try {
192
- await this.ctx.http.get(`${trimSlash(this.config.baseURL)}/v1/models`, {
193
- headers: this.createHeaders()
194
- });
211
+ await this.ctx.http.get(`${trimSlash(this.config.baseURL)}/v1/models`, { headers: this.createHeaders() });
212
+ logger.info("API connection test succeeded");
195
213
  return true;
196
214
  } catch (error) {
197
215
  logger.error("API connection test failed:", error);
@@ -721,6 +739,7 @@ var SAT = class extends Sat {
721
739
  keys: this.config.key,
722
740
  appointModel: this.config.appointModel,
723
741
  content_max_tokens: this.config.content_max_tokens,
742
+ maxRetryTimes: this.config.maxRetryTimes,
724
743
  temperature: this.config.temperature
725
744
  };
726
745
  }
package/lib/types.d.ts CHANGED
@@ -52,6 +52,7 @@ export interface APIConfig {
52
52
  appointModel: string;
53
53
  content_max_tokens: number;
54
54
  temperature: number;
55
+ maxRetryTimes?: number;
55
56
  timeout?: number;
56
57
  }
57
58
  export interface APIError extends Error {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-satori-ai",
3
3
  "description": "基于 Satori 适配器的智能对话插件,具备长期记忆、常识管理和好感度系统",
4
- "version": "1.1.2",
4
+ "version": "1.1.4",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "types": "lib/index.d.ts",