Skip to content

Commit 44fc5b5

Browse files
committed
fix: onfinish responseRes
1 parent 2d3f7c9 commit 44fc5b5

15 files changed

Lines changed: 50 additions & 42 deletions

File tree

app/client/api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export interface ChatOptions {
7070
config: LLMConfig;
7171

7272
onUpdate?: (message: string, chunk: string) => void;
73-
onFinish: (message: string) => void;
73+
onFinish: (message: string, responseRes: Response) => void;
7474
onError?: (err: Error) => void;
7575
onController?: (controller: AbortController) => void;
7676
onBeforeTool?: (tool: ChatMessageTool) => void;

app/client/platforms/alibaba.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ export class QwenApi implements LLMApi {
143143
let responseText = "";
144144
let remainText = "";
145145
let finished = false;
146+
let responseRes: Response;
146147

147148
// animate response to make it looks smooth
148149
function animateResponseText() {
@@ -172,7 +173,7 @@ export class QwenApi implements LLMApi {
172173
const finish = () => {
173174
if (!finished) {
174175
finished = true;
175-
options.onFinish(responseText + remainText);
176+
options.onFinish(responseText + remainText, responseRes);
176177
}
177178
};
178179

@@ -188,6 +189,7 @@ export class QwenApi implements LLMApi {
188189
"[Alibaba] request response content type: ",
189190
contentType,
190191
);
192+
responseRes = res;
191193

192194
if (contentType?.startsWith("text/plain")) {
193195
responseText = await res.clone().text();
@@ -254,7 +256,7 @@ export class QwenApi implements LLMApi {
254256

255257
const resJson = await res.json();
256258
const message = this.extractMessage(resJson);
257-
options.onFinish(message);
259+
options.onFinish(message, res);
258260
}
259261
} catch (e) {
260262
console.log("[Request] failed to make a chat request", e);

app/client/platforms/anthropic.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,13 +317,14 @@ export class ClaudeApi implements LLMApi {
317317
};
318318

319319
try {
320-
controller.signal.onabort = () => options.onFinish("");
320+
controller.signal.onabort = () =>
321+
options.onFinish("", new Response(null, { status: 400 }));
321322

322323
const res = await fetch(path, payload);
323324
const resJson = await res.json();
324325

325326
const message = this.extractMessage(resJson);
326-
options.onFinish(message);
327+
options.onFinish(message, res);
327328
} catch (e) {
328329
console.error("failed to chat", e);
329330
options.onError?.(e as Error);

app/client/platforms/baidu.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ export class ErnieApi implements LLMApi {
162162
let responseText = "";
163163
let remainText = "";
164164
let finished = false;
165+
let responseRes: Response;
165166

166167
// animate response to make it looks smooth
167168
function animateResponseText() {
@@ -191,7 +192,7 @@ export class ErnieApi implements LLMApi {
191192
const finish = () => {
192193
if (!finished) {
193194
finished = true;
194-
options.onFinish(responseText + remainText);
195+
options.onFinish(responseText + remainText, responseRes);
195196
}
196197
};
197198

@@ -204,7 +205,7 @@ export class ErnieApi implements LLMApi {
204205
clearTimeout(requestTimeoutId);
205206
const contentType = res.headers.get("content-type");
206207
console.log("[Baidu] request response content type: ", contentType);
207-
208+
responseRes = res;
208209
if (contentType?.startsWith("text/plain")) {
209210
responseText = await res.clone().text();
210211
return finish();
@@ -267,7 +268,7 @@ export class ErnieApi implements LLMApi {
267268

268269
const resJson = await res.json();
269270
const message = resJson?.result;
270-
options.onFinish(message);
271+
options.onFinish(message, res);
271272
}
272273
} catch (e) {
273274
console.log("[Request] failed to make a chat request", e);

app/client/platforms/bytedance.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ export class DoubaoApi implements LLMApi {
130130
let responseText = "";
131131
let remainText = "";
132132
let finished = false;
133+
let responseRes: Response;
133134

134135
// animate response to make it looks smooth
135136
function animateResponseText() {
@@ -159,7 +160,7 @@ export class DoubaoApi implements LLMApi {
159160
const finish = () => {
160161
if (!finished) {
161162
finished = true;
162-
options.onFinish(responseText + remainText);
163+
options.onFinish(responseText + remainText, responseRes);
163164
}
164165
};
165166

@@ -175,7 +176,7 @@ export class DoubaoApi implements LLMApi {
175176
"[ByteDance] request response content type: ",
176177
contentType,
177178
);
178-
179+
responseRes = res;
179180
if (contentType?.startsWith("text/plain")) {
180181
responseText = await res.clone().text();
181182
return finish();
@@ -241,7 +242,7 @@ export class DoubaoApi implements LLMApi {
241242

242243
const resJson = await res.json();
243244
const message = this.extractMessage(resJson);
244-
options.onFinish(message);
245+
options.onFinish(message, res);
245246
}
246247
} catch (e) {
247248
console.log("[Request] failed to make a chat request", e);

app/client/platforms/glm.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ export class ChatGLMApi implements LLMApi {
177177

178178
const resJson = await res.json();
179179
const message = this.extractMessage(resJson);
180-
options.onFinish(message);
180+
options.onFinish(message, res);
181181
}
182182
} catch (e) {
183183
console.log("[Request] failed to make a chat request", e);

app/client/platforms/google.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ export class GeminiProApi implements LLMApi {
274274
);
275275
}
276276
const message = apiClient.extractMessage(resJson);
277-
options.onFinish(message);
277+
options.onFinish(message, res);
278278
}
279279
} catch (e) {
280280
console.log("[Request] failed to make a chat request", e);

app/client/platforms/iflytek.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ export class SparkApi implements LLMApi {
117117
let responseText = "";
118118
let remainText = "";
119119
let finished = false;
120+
let responseRes: Response;
120121

121122
// Animate response text to make it look smooth
122123
function animateResponseText() {
@@ -143,7 +144,7 @@ export class SparkApi implements LLMApi {
143144
const finish = () => {
144145
if (!finished) {
145146
finished = true;
146-
options.onFinish(responseText + remainText);
147+
options.onFinish(responseText + remainText, responseRes);
147148
}
148149
};
149150

@@ -156,7 +157,7 @@ export class SparkApi implements LLMApi {
156157
clearTimeout(requestTimeoutId);
157158
const contentType = res.headers.get("content-type");
158159
console.log("[Spark] request response content type: ", contentType);
159-
160+
responseRes = res;
160161
if (contentType?.startsWith("text/plain")) {
161162
responseText = await res.clone().text();
162163
return finish();
@@ -231,7 +232,7 @@ export class SparkApi implements LLMApi {
231232

232233
const resJson = await res.json();
233234
const message = this.extractMessage(resJson);
234-
options.onFinish(message);
235+
options.onFinish(message, res);
235236
}
236237
} catch (e) {
237238
console.log("[Request] failed to make a chat request", e);

app/client/platforms/moonshot.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ export class MoonshotApi implements LLMApi {
180180

181181
const resJson = await res.json();
182182
const message = this.extractMessage(resJson);
183-
options.onFinish(message);
183+
options.onFinish(message, res);
184184
}
185185
} catch (e) {
186186
console.log("[Request] failed to make a chat request", e);

app/client/platforms/openai.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ export class ChatGPTApi implements LLMApi {
361361

362362
const resJson = await res.json();
363363
const message = await this.extractMessage(resJson);
364-
options.onFinish(message);
364+
options.onFinish(message, res);
365365
}
366366
} catch (e) {
367367
console.log("[Request] failed to make a chat request", e);

0 commit comments

Comments
 (0)