Skip to content

armgit / pibot

Public Imported from GitHub
0

Open-source, local-first AI agent team for your desktop: multi-agent group chats, computer use, browser, MCP, routines and approvals. macOS, Windows, Linux + Android. Built on pi.

12.4 KB
Raw Blame
diff --git a/dist/index.cjs b/dist/index.cjs
--- a/dist/index.cjs
+++ b/dist/index.cjs
@@ -8434,6 +8434,14 @@
                         const eventString = buffer.substring(0, delimiterIndex);
                         buffer = buffer.substring(delimiterIndex + delimiterLength);
                         const trimmedEvent = eventString.trim();
+                        // Gateways may send an explicit SSE error event, with a nested or flat JSON payload.
+                        if (/^event: ?error[ \t]*\r?$/m.test(eventString)) {
+                            const data = eventString.split(/\r\n|[\r\n]/).filter(line => line.startsWith(dataPrefix))
+                                .map(line => line.slice(dataPrefix.length).replace(/^ /, '')).join('\n');
+                            let payload;
+                            try { payload = JSON.parse(data); } catch { /* Keep malformed error payloads out of diagnostics. */ }
+                            throw googleStreamError(response.status, payload?.error ?? payload);
+                        }
                         if (trimmedEvent.startsWith(dataPrefix)) {
                             const processedChunkString = trimmedEvent
                                 .substring(dataPrefix.length)
@@ -8657,6 +8665,11 @@
         }
         return uploadUrl;
     }
+}
+// Preserve the HTTP status of the stream rather than treating a provider's application code as HTTP.
+function googleStreamError(status, error) {
+    const message = typeof error?.message === 'string' ? error.message : 'Provider stream error';
+    return new ApiError({ status, message: `HTTP ${status}: ${message}` });
 }
 async function throwErrorIfNotOK(response) {
     var _a;
@@ -22102,7 +22115,10 @@
                             _c = apiResponse_2_1.value;
                             _d = false;
                             const chunk = _c;
-                            const resp = generateContentResponseFromMldev((yield __await(chunk.json())), params);
+                            const data = yield __await(chunk.json());
+                            // The generated model-response converter drops error envelopes as unknown fields.
+                            if (data?.error) throw googleStreamError(chunk.responseInternal.status, data.error);
+                            const resp = generateContentResponseFromMldev(data, params);
                             resp['sdkHttpResponse'] = {
                                 headers: chunk.headers,
                             };
diff --git a/dist/index.mjs b/dist/index.mjs
--- a/dist/index.mjs
+++ b/dist/index.mjs
@@ -8432,6 +8432,14 @@
                         const eventString = buffer.substring(0, delimiterIndex);
                         buffer = buffer.substring(delimiterIndex + delimiterLength);
                         const trimmedEvent = eventString.trim();
+                        // Gateways may send an explicit SSE error event, with a nested or flat JSON payload.
+                        if (/^event: ?error[ \t]*\r?$/m.test(eventString)) {
+                            const data = eventString.split(/\r\n|[\r\n]/).filter(line => line.startsWith(dataPrefix))
+                                .map(line => line.slice(dataPrefix.length).replace(/^ /, '')).join('\n');
+                            let payload;
+                            try { payload = JSON.parse(data); } catch { /* Keep malformed error payloads out of diagnostics. */ }
+                            throw googleStreamError(response.status, payload?.error ?? payload);
+                        }
                         if (trimmedEvent.startsWith(dataPrefix)) {
                             const processedChunkString = trimmedEvent
                                 .substring(dataPrefix.length)
@@ -8655,6 +8663,11 @@
         }
         return uploadUrl;
     }
+}
+// Preserve the HTTP status of the stream rather than treating a provider's application code as HTTP.
+function googleStreamError(status, error) {
+    const message = typeof error?.message === 'string' ? error.message : 'Provider stream error';
+    return new ApiError({ status, message: `HTTP ${status}: ${message}` });
 }
 async function throwErrorIfNotOK(response) {
     var _a;
@@ -22100,7 +22113,10 @@
                             _c = apiResponse_2_1.value;
                             _d = false;
                             const chunk = _c;
-                            const resp = generateContentResponseFromMldev((yield __await(chunk.json())), params);
+                            const data = yield __await(chunk.json());
+                            // The generated model-response converter drops error envelopes as unknown fields.
+                            if (data?.error) throw googleStreamError(chunk.responseInternal.status, data.error);
+                            const resp = generateContentResponseFromMldev(data, params);
                             resp['sdkHttpResponse'] = {
                                 headers: chunk.headers,
                             };
diff --git a/dist/node/index.cjs b/dist/node/index.cjs
--- a/dist/node/index.cjs
+++ b/dist/node/index.cjs
@@ -13863,6 +13863,14 @@
                         const eventString = buffer.substring(0, delimiterIndex);
                         buffer = buffer.substring(delimiterIndex + delimiterLength);
                         const trimmedEvent = eventString.trim();
+                        // Gateways may send an explicit SSE error event, with a nested or flat JSON payload.
+                        if (/^event: ?error[ \t]*\r?$/m.test(eventString)) {
+                            const data = eventString.split(/\r\n|[\r\n]/).filter(line => line.startsWith(dataPrefix))
+                                .map(line => line.slice(dataPrefix.length).replace(/^ /, '')).join('\n');
+                            let payload;
+                            try { payload = JSON.parse(data); } catch { /* Keep malformed error payloads out of diagnostics. */ }
+                            throw googleStreamError(response.status, payload?.error ?? payload);
+                        }
                         if (trimmedEvent.startsWith(dataPrefix)) {
                             const processedChunkString = trimmedEvent
                                 .substring(dataPrefix.length)
@@ -14086,6 +14094,11 @@
         }
         return uploadUrl;
     }
+}
+// Preserve the HTTP status of the stream rather than treating a provider's application code as HTTP.
+function googleStreamError(status, error) {
+    const message = typeof error?.message === 'string' ? error.message : 'Provider stream error';
+    return new ApiError({ status, message: `HTTP ${status}: ${message}` });
 }
 async function throwErrorIfNotOK(response) {
     var _a;
@@ -15857,7 +15870,10 @@
                             _c = apiResponse_2_1.value;
                             _d = false;
                             const chunk = _c;
-                            const resp = generateContentResponseFromMldev((yield __await(chunk.json())), params);
+                            const data = yield __await(chunk.json());
+                            // The generated model-response converter drops error envelopes as unknown fields.
+                            if (data?.error) throw googleStreamError(chunk.responseInternal.status, data.error);
+                            const resp = generateContentResponseFromMldev(data, params);
                             resp['sdkHttpResponse'] = {
                                 headers: chunk.headers,
                             };
diff --git a/dist/node/index.mjs b/dist/node/index.mjs
--- a/dist/node/index.mjs
+++ b/dist/node/index.mjs
@@ -13841,6 +13841,14 @@
                         const eventString = buffer.substring(0, delimiterIndex);
                         buffer = buffer.substring(delimiterIndex + delimiterLength);
                         const trimmedEvent = eventString.trim();
+                        // Gateways may send an explicit SSE error event, with a nested or flat JSON payload.
+                        if (/^event: ?error[ \t]*\r?$/m.test(eventString)) {
+                            const data = eventString.split(/\r\n|[\r\n]/).filter(line => line.startsWith(dataPrefix))
+                                .map(line => line.slice(dataPrefix.length).replace(/^ /, '')).join('\n');
+                            let payload;
+                            try { payload = JSON.parse(data); } catch { /* Keep malformed error payloads out of diagnostics. */ }
+                            throw googleStreamError(response.status, payload?.error ?? payload);
+                        }
                         if (trimmedEvent.startsWith(dataPrefix)) {
                             const processedChunkString = trimmedEvent
                                 .substring(dataPrefix.length)
@@ -14064,6 +14072,11 @@
         }
         return uploadUrl;
     }
+}
+// Preserve the HTTP status of the stream rather than treating a provider's application code as HTTP.
+function googleStreamError(status, error) {
+    const message = typeof error?.message === 'string' ? error.message : 'Provider stream error';
+    return new ApiError({ status, message: `HTTP ${status}: ${message}` });
 }
 async function throwErrorIfNotOK(response) {
     var _a;
@@ -15835,7 +15848,10 @@
                             _c = apiResponse_2_1.value;
                             _d = false;
                             const chunk = _c;
-                            const resp = generateContentResponseFromMldev((yield __await(chunk.json())), params);
+                            const data = yield __await(chunk.json());
+                            // The generated model-response converter drops error envelopes as unknown fields.
+                            if (data?.error) throw googleStreamError(chunk.responseInternal.status, data.error);
+                            const resp = generateContentResponseFromMldev(data, params);
                             resp['sdkHttpResponse'] = {
                                 headers: chunk.headers,
                             };
diff --git a/dist/web/index.mjs b/dist/web/index.mjs
--- a/dist/web/index.mjs
+++ b/dist/web/index.mjs
@@ -13833,6 +13833,14 @@
                         const eventString = buffer.substring(0, delimiterIndex);
                         buffer = buffer.substring(delimiterIndex + delimiterLength);
                         const trimmedEvent = eventString.trim();
+                        // Gateways may send an explicit SSE error event, with a nested or flat JSON payload.
+                        if (/^event: ?error[ \t]*\r?$/m.test(eventString)) {
+                            const data = eventString.split(/\r\n|[\r\n]/).filter(line => line.startsWith(dataPrefix))
+                                .map(line => line.slice(dataPrefix.length).replace(/^ /, '')).join('\n');
+                            let payload;
+                            try { payload = JSON.parse(data); } catch { /* Keep malformed error payloads out of diagnostics. */ }
+                            throw googleStreamError(response.status, payload?.error ?? payload);
+                        }
                         if (trimmedEvent.startsWith(dataPrefix)) {
                             const processedChunkString = trimmedEvent
                                 .substring(dataPrefix.length)
@@ -14056,6 +14064,11 @@
         }
         return uploadUrl;
     }
+}
+// Preserve the HTTP status of the stream rather than treating a provider's application code as HTTP.
+function googleStreamError(status, error) {
+    const message = typeof error?.message === 'string' ? error.message : 'Provider stream error';
+    return new ApiError({ status, message: `HTTP ${status}: ${message}` });
 }
 async function throwErrorIfNotOK(response) {
     var _a;
@@ -15827,7 +15840,10 @@
                             _c = apiResponse_2_1.value;
                             _d = false;
                             const chunk = _c;
-                            const resp = generateContentResponseFromMldev((yield __await(chunk.json())), params);
+                            const data = yield __await(chunk.json());
+                            // The generated model-response converter drops error envelopes as unknown fields.
+                            if (data?.error) throw googleStreamError(chunk.responseInternal.status, data.error);
+                            const resp = generateContentResponseFromMldev(data, params);
                             resp['sdkHttpResponse'] = {
                                 headers: chunk.headers,
                             };