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:
committed by
Peter Steinberger
parent
de454fc385
commit
fb17a32283
@@ -99,6 +99,7 @@ export type EmbeddedPiRunResult = {
|
|||||||
mediaUrl?: string;
|
mediaUrl?: string;
|
||||||
mediaUrls?: string[];
|
mediaUrls?: string[];
|
||||||
replyToId?: string;
|
replyToId?: string;
|
||||||
|
isError?: boolean;
|
||||||
}>;
|
}>;
|
||||||
meta: EmbeddedPiRunMeta;
|
meta: EmbeddedPiRunMeta;
|
||||||
};
|
};
|
||||||
@@ -1009,12 +1010,17 @@ export async function runEmbeddedPiAgent(params: {
|
|||||||
usage,
|
usage,
|
||||||
};
|
};
|
||||||
|
|
||||||
const replyItems: Array<{ text: string; media?: string[] }> = [];
|
const replyItems: Array<{
|
||||||
|
text: string;
|
||||||
|
media?: string[];
|
||||||
|
isError?: boolean;
|
||||||
|
}> = [];
|
||||||
|
|
||||||
const errorText = lastAssistant
|
const errorText = lastAssistant
|
||||||
? formatAssistantErrorText(lastAssistant)
|
? formatAssistantErrorText(lastAssistant)
|
||||||
: undefined;
|
: undefined;
|
||||||
if (errorText) replyItems.push({ text: errorText });
|
|
||||||
|
if (errorText) replyItems.push({ text: errorText, isError: true });
|
||||||
|
|
||||||
const inlineToolResults =
|
const inlineToolResults =
|
||||||
params.verboseLevel === "on" &&
|
params.verboseLevel === "on" &&
|
||||||
@@ -1047,6 +1053,7 @@ export async function runEmbeddedPiAgent(params: {
|
|||||||
text: item.text?.trim() ? item.text.trim() : undefined,
|
text: item.text?.trim() ? item.text.trim() : undefined,
|
||||||
mediaUrls: item.media?.length ? item.media : undefined,
|
mediaUrls: item.media?.length ? item.media : undefined,
|
||||||
mediaUrl: item.media?.[0],
|
mediaUrl: item.media?.[0],
|
||||||
|
isError: item.isError,
|
||||||
}))
|
}))
|
||||||
.filter(
|
.filter(
|
||||||
(p) =>
|
(p) =>
|
||||||
|
|||||||
@@ -401,8 +401,25 @@ export async function runReplyAgent(params: {
|
|||||||
const sanitizedPayloads = isHeartbeat
|
const sanitizedPayloads = isHeartbeat
|
||||||
? payloadArray
|
? payloadArray
|
||||||
: payloadArray.flatMap((payload) => {
|
: payloadArray.flatMap((payload) => {
|
||||||
const text = payload.text;
|
let text = payload.text;
|
||||||
if (!text || !text.includes("HEARTBEAT_OK")) return [payload];
|
|
||||||
|
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" });
|
const stripped = stripHeartbeatToken(text, { mode: "message" });
|
||||||
if (stripped.didStrip && !didLogHeartbeatStrip) {
|
if (stripped.didStrip && !didLogHeartbeatStrip) {
|
||||||
didLogHeartbeatStrip = true;
|
didLogHeartbeatStrip = true;
|
||||||
|
|||||||
Reference in New Issue
Block a user