Compare commits

...

3 Commits

Author SHA1 Message Date
Cline Evaluation b18f028e0f type fix 2025-06-23 23:28:31 -06:00
Cline Evaluation 569eae5b95 Raise error on openrouter stream termination midway 2025-06-23 10:48:44 -06:00
Cline Evaluation 3f9d21e873 Raise error on openrouter stream termination midway 2025-06-23 10:46:00 -06:00
2 changed files with 39 additions and 1 deletions
+15 -1
View File
@@ -56,7 +56,21 @@ export class ClineHandler implements ApiHandler {
this.lastGenerationId = chunk.id
}
const delta = chunk.choices[0]?.delta
// Check for mid-stream error via finish_reason
const choice = chunk.choices?.[0]
// OpenRouter may return finish_reason = "error" with error details
if ((choice?.finish_reason as string) === "error") {
const choiceWithError = choice as any
if (choiceWithError.error) {
const error = choiceWithError.error
console.error(`Cline Mid-Stream Error: ${error.code || error.type || "Unknown"} - ${error.message}`)
throw new Error(`Cline Mid-Stream Error: ${error.code || error.type || "Unknown"} - ${error.message}`)
} else {
throw new Error("Cline Mid-Stream Error: Stream terminated with error status but no error details provided")
}
}
const delta = choice?.delta
if (delta?.content) {
yield {
type: "text",
+24
View File
@@ -44,6 +44,7 @@ export class OpenRouterHandler implements ApiHandler {
for await (const chunk of stream) {
// openrouter returns an error object instead of the openai sdk throwing an error
// Check for error field directly on chunk
if ("error" in chunk) {
const error = chunk.error as OpenRouterErrorResponse["error"]
console.error(`OpenRouter API Error: ${error?.code} - ${error?.message}`)
@@ -52,6 +53,29 @@ export class OpenRouterHandler implements ApiHandler {
throw new Error(`OpenRouter API Error ${error.code}: ${error.message}${metadataStr}`)
}
// Check for error in choices[0].finish_reason
// OpenRouter may return errors in a non-standard way within choices
const choice = chunk.choices?.[0]
// Use type assertion since OpenRouter uses non-standard "error" finish_reason
if ((choice?.finish_reason as string) === "error") {
// Use type assertion since OpenRouter adds non-standard error property
const choiceWithError = choice as any
if (choiceWithError.error) {
const error = choiceWithError.error
console.error(
`OpenRouter Mid-Stream Error: ${error?.code || "Unknown"} - ${error?.message || "Unknown error"}`,
)
// Format error details
const errorDetails = typeof error === "object" ? JSON.stringify(error, null, 2) : String(error)
throw new Error(`OpenRouter Mid-Stream Error: ${errorDetails}`)
} else {
// Fallback if error details are not available
throw new Error(
`OpenRouter Mid-Stream Error: Stream terminated with error status but no error details provided`,
)
}
}
if (!this.lastGenerationId && chunk.id) {
this.lastGenerationId = chunk.id
}