Skip to content

Commit a87aed4

Browse files
committed
refactor(agents): reuse shared error normalization
1 parent 69c4d1a commit a87aed4

3 files changed

Lines changed: 6 additions & 45 deletions

File tree

src/agents/sessions/tools/bash.ts

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { existsSync } from "node:fs";
88
import { Container, Text, truncateToWidth } from "@earendil-works/pi-tui";
99
import { resolveTimerTimeoutMs } from "@openclaw/normalization-core/number-coercion";
1010
import { Type } from "typebox";
11+
import { toErrorObject } from "../../../infra/errors.js";
1112
import { keyHint } from "../../modes/interactive/components/keybinding-hints.js";
1213
import { truncateToVisualLines } from "../../modes/interactive/components/visual-truncate.js";
1314
import { theme } from "../../modes/interactive/theme/theme.js";
@@ -121,7 +122,7 @@ export function createLocalBashOperations(options?: { shellPath?: string }): Bas
121122
if (signal) {
122123
signal.removeEventListener("abort", onAbort);
123124
}
124-
reject(toLintErrorObject(err, "Non-Error rejection"));
125+
reject(toErrorObject(err, "Non-Error rejection"));
125126
});
126127
});
127128
},
@@ -473,17 +474,3 @@ export function createBashTool(
473474
): AgentTool<typeof bashSchema> {
474475
return wrapToolDefinition(createBashToolDefinition(cwd, options));
475476
}
476-
477-
function toLintErrorObject(value: unknown, fallbackMessage: string): Error {
478-
if (value instanceof Error) {
479-
return value;
480-
}
481-
if (typeof value === "string") {
482-
return new Error(value);
483-
}
484-
const error = new Error(fallbackMessage, { cause: value });
485-
if ((typeof value === "object" && value !== null) || typeof value === "function") {
486-
Object.assign(error, value);
487-
}
488-
return error;
489-
}

src/agents/sessions/tools/ls.ts

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { existsSync, readdirSync, statSync } from "node:fs";
77
import nodePath from "node:path";
88
import { Text } from "@earendil-works/pi-tui";
99
import { Type } from "typebox";
10+
import { toErrorObject } from "../../../infra/errors.js";
1011
import type { AgentTool } from "../../runtime/index.js";
1112
import type { ToolDefinition, ToolRenderResultOptions } from "../extensions/types.js";
1213
import { normalizePositiveLimit } from "./limits.js";
@@ -216,7 +217,7 @@ export function createLsToolDefinition(
216217
});
217218
} catch (e: unknown) {
218219
signal?.removeEventListener("abort", onAbort);
219-
reject(toLintErrorObject(e, "Non-Error rejection"));
220+
reject(toErrorObject(e, "Non-Error rejection"));
220221
}
221222
})();
222223
});
@@ -237,17 +238,3 @@ export function createLsToolDefinition(
237238
export function createLsTool(cwd: string, options?: LsToolOptions): AgentTool<typeof lsSchema> {
238239
return wrapToolDefinition(createLsToolDefinition(cwd, options));
239240
}
240-
241-
function toLintErrorObject(value: unknown, fallbackMessage: string): Error {
242-
if (value instanceof Error) {
243-
return value;
244-
}
245-
if (typeof value === "string") {
246-
return new Error(value);
247-
}
248-
const error = new Error(fallbackMessage, { cause: value });
249-
if ((typeof value === "object" && value !== null) || typeof value === "function") {
250-
Object.assign(error, value);
251-
}
252-
return error;
253-
}

src/agents/sessions/tools/read.ts

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { access as fsAccess, readFile as fsReadFile } from "node:fs/promises";
88
import { basename, dirname, isAbsolute, relative, resolve as resolvePath, sep } from "node:path";
99
import { Text } from "@earendil-works/pi-tui";
1010
import { Type } from "typebox";
11+
import { toErrorObject } from "../../../infra/errors.js";
1112
import { decodeWindowsTextFileBuffer } from "../../../infra/windows-encoding.js";
1213
import type { ImageContent, Model, TextContent } from "../../../llm/types.js";
1314
import {
@@ -409,7 +410,7 @@ export function createReadToolDefinition(
409410
} catch (error: unknown) {
410411
signal?.removeEventListener("abort", onAbort);
411412
if (!aborted) {
412-
reject(toLintErrorObject(error, "Non-Error rejection"));
413+
reject(toErrorObject(error, "Non-Error rejection"));
413414
}
414415
}
415416
})();
@@ -451,17 +452,3 @@ export function createReadTool(
451452
): AgentTool<typeof readSchema> {
452453
return wrapToolDefinition(createReadToolDefinition(cwd, options));
453454
}
454-
455-
function toLintErrorObject(value: unknown, fallbackMessage: string): Error {
456-
if (value instanceof Error) {
457-
return value;
458-
}
459-
if (typeof value === "string") {
460-
return new Error(value);
461-
}
462-
const error = new Error(fallbackMessage, { cause: value });
463-
if ((typeof value === "object" && value !== null) || typeof value === "function") {
464-
Object.assign(error, value);
465-
}
466-
return error;
467-
}

0 commit comments

Comments
 (0)