Skip to content
This repository was archived by the owner on Mar 19, 2026. It is now read-only.

Commit c5f51b8

Browse files
committed
fix(core): improve error handling in API response and allow nullish values for token counts
1 parent a504d99 commit c5f51b8

2 files changed

Lines changed: 8 additions & 16 deletions

File tree

packages/openai-compatible/src/convert-to-openai-compatible-chat-messages.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { BaseMessage } from 'cortexluna'
1+
import { BaseMessage, dataContentToBase64 } from 'cortexluna'
22

33
export function convertToOpenAICompatibleChatMessages(
44
prompt: BaseMessage[]
@@ -62,7 +62,7 @@ export function convertToOpenAICompatibleChatMessages(
6262
: `data:${
6363
part.mineType ??
6464
'image/jpeg'
65-
};base64,${convertUint8ArrayToBase64(imageUrl)}`
65+
};base64,${dataContentToBase64(imageUrl)}`
6666
}
6767
}
6868
}
@@ -196,13 +196,3 @@ export interface OpenAICompatibleToolMessage {
196196
content: string
197197
tool_call_id: string
198198
}
199-
function convertUint8ArrayToBase64(
200-
image:
201-
| string
202-
| ArrayBuffer
203-
| Uint8Array<ArrayBuffer>
204-
| Buffer<ArrayBuffer>
205-
| URL
206-
) {
207-
throw new Error('Function not implemented.')
208-
}

packages/openai-compatible/src/language-model.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,9 @@ export class OpenAICompatibleLanguageModel implements LanguageModel {
134134
OpenAICompatibleChatResponseSchema.safeParse(responseJSON)
135135

136136
if (!parsedResponse.success) {
137-
throw new Error(`Invalid response from API: ${responseBody}`)
137+
throw new Error(
138+
`Invalid response from API: ${responseBody} ${parsedResponse.error}`
139+
)
138140
}
139141

140142
return parsedResponse.data
@@ -496,11 +498,11 @@ const OpenAICompatibleChatResponseSchema = z.object({
496498
),
497499
usage: z
498500
.object({
499-
prompt_tokens: z.number(),
500-
completion_tokens: z.number(),
501+
prompt_tokens: z.number().nullish(),
502+
completion_tokens: z.number().nullish(),
501503
prompt_tokens_details: z
502504
.object({
503-
cached_tokens: z.number()
505+
cached_tokens: z.number().nullish()
504506
})
505507
.nullish()
506508
})

0 commit comments

Comments
 (0)