Skip to content

Commit fb17a32

Browse files
emanuelststeipete
authored andcommitted
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.
1 parent de454fc commit fb17a32

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

src/agents/pi-embedded-runner.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ export type EmbeddedPiRunResult = {
9999
mediaUrl?: string;
100100
mediaUrls?: string[];
101101
replyToId?: string;
102+
isError?: boolean;
102103
}>;
103104
meta: EmbeddedPiRunMeta;
104105
};
@@ -1009,12 +1010,17 @@ export async function runEmbeddedPiAgent(params: {
10091010
usage,
10101011
};
10111012

1012-
const replyItems: Array<{ text: string; media?: string[] }> = [];
1013+
const replyItems: Array<{
1014+
text: string;
1015+
media?: string[];
1016+
isError?: boolean;
1017+
}> = [];
10131018

10141019
const errorText = lastAssistant
10151020
? formatAssistantErrorText(lastAssistant)
10161021
: undefined;
1017-
if (errorText) replyItems.push({ text: errorText });
1022+
1023+
if (errorText) replyItems.push({ text: errorText, isError: true });
10181024

10191025
const inlineToolResults =
10201026
params.verboseLevel === "on" &&
@@ -1047,6 +1053,7 @@ export async function runEmbeddedPiAgent(params: {
10471053
text: item.text?.trim() ? item.text.trim() : undefined,
10481054
mediaUrls: item.media?.length ? item.media : undefined,
10491055
mediaUrl: item.media?.[0],
1056+
isError: item.isError,
10501057
}))
10511058
.filter(
10521059
(p) =>

src/auto-reply/reply/agent-runner.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -401,8 +401,25 @@ export async function runReplyAgent(params: {
401401
const sanitizedPayloads = isHeartbeat
402402
? payloadArray
403403
: payloadArray.flatMap((payload) => {
404-
const text = payload.text;
405-
if (!text || !text.includes("HEARTBEAT_OK")) return [payload];
404+
let text = payload.text;
405+
406+
if (payload.isError) {
407+
// Handle Bun fetch socket connection error that may indicate a context length issue
408+
// Error source: https://github.com/oven-sh/bun/blob/main/src/bun.js/webcore/fetch/FetchTasklet.zig
409+
const isBunFetchSocketError =
410+
text ===
411+
"The socket connection was closed unexpectedly. For more information, pass `verbose: true` in the second argument to fetch()";
412+
413+
if (isBunFetchSocketError) {
414+
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:
415+
\`\`\`
416+
${text || "Unknown error"}
417+
\`\`\``;
418+
}
419+
}
420+
421+
if (!text || !text.includes("HEARTBEAT_OK"))
422+
return [{ ...payload, text }];
406423
const stripped = stripHeartbeatToken(text, { mode: "message" });
407424
if (stripped.didStrip && !didLogHeartbeatStrip) {
408425
didLogHeartbeatStrip = true;

0 commit comments

Comments
 (0)