feat: enhance error handling for socket connection errors

- Added `isError` property to `EmbeddedPiRunResult` and reply items to indicate error states.
- Updated error handling in `runReplyAgent` to provide more informative messages for specific socket connection errors.
This commit is contained in:
Emanuel Stadler
2026-01-06 21:17:55 +01:00
committed by Peter Steinberger
parent de454fc385
commit fb17a32283
2 changed files with 28 additions and 4 deletions

View File

@@ -401,8 +401,25 @@ export async function runReplyAgent(params: {
const sanitizedPayloads = isHeartbeat
? payloadArray
: payloadArray.flatMap((payload) => {
const text = payload.text;
if (!text || !text.includes("HEARTBEAT_OK")) return [payload];
let text = payload.text;
if (payload.isError) {
// Handle Bun fetch socket connection error that may indicate a context length issue
// Error source: https://github.com/oven-sh/bun/blob/main/src/bun.js/webcore/fetch/FetchTasklet.zig
const isBunFetchSocketError =
text ===
"The socket connection was closed unexpectedly. For more information, pass `verbose: true` in the second argument to fetch()";
if (isBunFetchSocketError) {
text = `⚠️ LLM connection failed. This could be due to server issues, network problems, or context length exceeded (e.g., with local LLMs like LM Studio). Original error:
\`\`\`
${text || "Unknown error"}
\`\`\``;
}
}
if (!text || !text.includes("HEARTBEAT_OK"))
return [{ ...payload, text }];
const stripped = stripHeartbeatToken(text, { mode: "message" });
if (stripped.didStrip && !didLogHeartbeatStrip) {
didLogHeartbeatStrip = true;