Browse Source

zen: error handling for stream requests

Frank 2 months ago
parent
commit
647331de28
1 changed files with 6 additions and 19 deletions
  1. 6 19
      packages/console/app/src/routes/zen/util/handler.ts

+ 6 - 19
packages/console/app/src/routes/zen/util/handler.ts

@@ -143,6 +143,9 @@ export async function handler(
     // Store sticky provider
     await stickyTracker?.set(providerInfo.id)
 
+    // Temporarily change 404 to 400 status code b/c solid start automatically override 404 response
+    const resStatus = res.status === 404 ? 400 : res.status
+
     // Scrub response headers
     const resHeaders = new Headers()
     const keepHeaders = ["content-type", "cache-control"]
@@ -153,7 +156,7 @@ export async function handler(
     }
     logger.debug("STATUS: " + res.status + " " + res.statusText)
 
-    // Handle non-streaming response for non-stream request
+    // Handle non-streaming response
     if (!isStream) {
       const responseConverter = createResponseConverter(providerInfo.format, opts.format)
       const json = await res.json()
@@ -168,23 +171,7 @@ export async function handler(
       await trackUsage(authInfo, modelInfo, providerInfo, tokensInfo)
       await reload(authInfo)
       return new Response(body, {
-        status: res.status,
-        statusText: res.statusText,
-        headers: resHeaders,
-      })
-    }
-
-    // Handle non-streaming response for stream request
-    const contentType = res.headers.get("content-type") ?? ""
-    if (!contentType.includes("text/event-stream")) {
-      const body = await res.text()
-      logger.metric({ response_length: body.length })
-      logger.debug("RESPONSE: " + body)
-      dataDumper?.provideResponse(body)
-      dataDumper?.flush()
-      await rateLimiter?.track()
-      return new Response(body, {
-        status: res.status,
+        status: resStatus,
         statusText: res.statusText,
         headers: resHeaders,
       })
@@ -262,7 +249,7 @@ export async function handler(
     })
 
     return new Response(stream, {
-      status: res.status,
+      status: resStatus,
       statusText: res.statusText,
       headers: resHeaders,
     })