refactor(agents): reuse shared error normalization

This commit is contained in:
Vincent Koc
2026-06-23 00:22:16 +08:00
parent 69c4d1aa85
commit a87aed4108
3 changed files with 6 additions and 45 deletions
+2 -15
View File
@@ -8,6 +8,7 @@ import { existsSync } from "node:fs";
import { Container, Text, truncateToWidth } from "@earendil-works/pi-tui";
import { resolveTimerTimeoutMs } from "@openclaw/normalization-core/number-coercion";
import { Type } from "typebox";
import { toErrorObject } from "../../../infra/errors.js";
import { keyHint } from "../../modes/interactive/components/keybinding-hints.js";
import { truncateToVisualLines } from "../../modes/interactive/components/visual-truncate.js";
import { theme } from "../../modes/interactive/theme/theme.js";
@@ -121,7 +122,7 @@ export function createLocalBashOperations(options?: { shellPath?: string }): Bas
if (signal) {
signal.removeEventListener("abort", onAbort);
}
reject(toLintErrorObject(err, "Non-Error rejection"));
reject(toErrorObject(err, "Non-Error rejection"));
});
});
},
@@ -473,17 +474,3 @@ export function createBashTool(
): AgentTool<typeof bashSchema> {
return wrapToolDefinition(createBashToolDefinition(cwd, options));
}
function toLintErrorObject(value: unknown, fallbackMessage: string): Error {
if (value instanceof Error) {
return value;
}
if (typeof value === "string") {
return new Error(value);
}
const error = new Error(fallbackMessage, { cause: value });
if ((typeof value === "object" && value !== null) || typeof value === "function") {
Object.assign(error, value);
}
return error;
}
+2 -15
View File
@@ -7,6 +7,7 @@ import { existsSync, readdirSync, statSync } from "node:fs";
import nodePath from "node:path";
import { Text } from "@earendil-works/pi-tui";
import { Type } from "typebox";
import { toErrorObject } from "../../../infra/errors.js";
import type { AgentTool } from "../../runtime/index.js";
import type { ToolDefinition, ToolRenderResultOptions } from "../extensions/types.js";
import { normalizePositiveLimit } from "./limits.js";
@@ -216,7 +217,7 @@ export function createLsToolDefinition(
});
} catch (e: unknown) {
signal?.removeEventListener("abort", onAbort);
reject(toLintErrorObject(e, "Non-Error rejection"));
reject(toErrorObject(e, "Non-Error rejection"));
}
})();
});
@@ -237,17 +238,3 @@ export function createLsToolDefinition(
export function createLsTool(cwd: string, options?: LsToolOptions): AgentTool<typeof lsSchema> {
return wrapToolDefinition(createLsToolDefinition(cwd, options));
}
function toLintErrorObject(value: unknown, fallbackMessage: string): Error {
if (value instanceof Error) {
return value;
}
if (typeof value === "string") {
return new Error(value);
}
const error = new Error(fallbackMessage, { cause: value });
if ((typeof value === "object" && value !== null) || typeof value === "function") {
Object.assign(error, value);
}
return error;
}
+2 -15
View File
@@ -8,6 +8,7 @@ import { access as fsAccess, readFile as fsReadFile } from "node:fs/promises";
import { basename, dirname, isAbsolute, relative, resolve as resolvePath, sep } from "node:path";
import { Text } from "@earendil-works/pi-tui";
import { Type } from "typebox";
import { toErrorObject } from "../../../infra/errors.js";
import { decodeWindowsTextFileBuffer } from "../../../infra/windows-encoding.js";
import type { ImageContent, Model, TextContent } from "../../../llm/types.js";
import {
@@ -409,7 +410,7 @@ export function createReadToolDefinition(
} catch (error: unknown) {
signal?.removeEventListener("abort", onAbort);
if (!aborted) {
reject(toLintErrorObject(error, "Non-Error rejection"));
reject(toErrorObject(error, "Non-Error rejection"));
}
}
})();
@@ -451,17 +452,3 @@ export function createReadTool(
): AgentTool<typeof readSchema> {
return wrapToolDefinition(createReadToolDefinition(cwd, options));
}
function toLintErrorObject(value: unknown, fallbackMessage: string): Error {
if (value instanceof Error) {
return value;
}
if (typeof value === "string") {
return new Error(value);
}
const error = new Error(fallbackMessage, { cause: value });
if ((typeof value === "object" && value !== null) || typeof value === "function") {
Object.assign(error, value);
}
return error;
}