From a87aed4108ccf5902e9a7055cc7d59d29bdda08e Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Tue, 23 Jun 2026 00:22:16 +0800 Subject: [PATCH] refactor(agents): reuse shared error normalization --- src/agents/sessions/tools/bash.ts | 17 ++--------------- src/agents/sessions/tools/ls.ts | 17 ++--------------- src/agents/sessions/tools/read.ts | 17 ++--------------- 3 files changed, 6 insertions(+), 45 deletions(-) diff --git a/src/agents/sessions/tools/bash.ts b/src/agents/sessions/tools/bash.ts index 9f15b0c7c0ce..cb53c87bca3b 100644 --- a/src/agents/sessions/tools/bash.ts +++ b/src/agents/sessions/tools/bash.ts @@ -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 { 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; -} diff --git a/src/agents/sessions/tools/ls.ts b/src/agents/sessions/tools/ls.ts index da0c18e609c3..672eea354694 100644 --- a/src/agents/sessions/tools/ls.ts +++ b/src/agents/sessions/tools/ls.ts @@ -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 { 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; -} diff --git a/src/agents/sessions/tools/read.ts b/src/agents/sessions/tools/read.ts index 21f0ec126a0e..01175e4e171d 100644 --- a/src/agents/sessions/tools/read.ts +++ b/src/agents/sessions/tools/read.ts @@ -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 { 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; -}