Skip to content

Commit a5932cf

Browse files
committed
chore: Improve debugging and fix openai json response format
1 parent 9bd5455 commit a5932cf

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

src/providers/GeminiProvider.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ class GeminiProvider implements Provider {
7474
body: JSON.stringify(this.constructBodyWithMessages(messages)),
7575
});
7676

77+
if (this.debug) {
78+
console.log('[GeminiProvider] Response status:', res.status);
79+
}
80+
7781
if (!res.ok) {
7882
throw new Error(`Gemini API error ${res.status}: ${await res.text()}`);
7983
}
@@ -88,6 +92,9 @@ class GeminiProvider implements Provider {
8892
}
8993
} else {
9094
const payload = await res.json();
95+
if (this.debug) {
96+
console.log('[GeminiProvider] Response body:', payload);
97+
}
9198
const text = payload.candidates?.[0]?.content?.parts?.[0]?.text;
9299
if (typeof text === 'string') {
93100
yield text;

src/providers/OpenaiProvider.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ class OpenaiProvider implements Provider {
7272
body: JSON.stringify(this.constructBodyWithMessages(messages)),
7373
});
7474

75+
if (this.debug) {
76+
console.log('[OpenaiProvider] Response status:', res.status);
77+
}
78+
7579
if (!res.ok) {
7680
throw new Error(`Openai API error ${res.status}: ${await res.text()}`);
7781
}
@@ -86,7 +90,10 @@ class OpenaiProvider implements Provider {
8690
}
8791
} else {
8892
const payload = await res.json();
89-
const text = payload.candidates?.[0]?.content?.parts?.[0]?.text;
93+
if (this.debug) {
94+
console.log('[OpenaiProvider] Response body:', payload);
95+
}
96+
const text = payload.choices?.[0]?.message?.content;
9097
if (typeof text === 'string') {
9198
yield text;
9299
} else {

src/providers/WebLlmProvider.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ class WebLlmProvider implements Provider {
6666
}
6767

6868
const result = await this.engine?.chat.completions.create(this.constructBodyWithMessages(messages));
69+
70+
if (this.debug) {
71+
console.log('[WebLlmProvider] Response:', result);
72+
}
73+
6974
if (result && Symbol.asyncIterator in result) {
7075
for await (const chunk of result as AsyncIterable<ChatCompletionChunk>) {
7176
const delta = chunk.choices[0]?.delta?.content;

0 commit comments

Comments
 (0)