refactor(agents): dedupe code mode json serialization · openclaw/openclaw@2e44610
vincentkoc
·
2026-06-18
·
via Recent Commits to openclaw:main
| Original file line number | Diff line number | Diff line change |
|---|
@@ -6,6 +6,7 @@ import { readFile } from "node:fs/promises";
|
6 | 6 | import { createRequire } from "node:module"; |
7 | 7 | import { parentPort, workerData } from "node:worker_threads"; |
8 | 8 | import { EvalFlags, Intrinsics, JSException, QuickJS, type JSValueHandle } from "quickjs-wasi"; |
| 9 | +import { toCodeModeJsonSafe as toJsonSafe } from "./code-mode-json.js"; |
9 | 10 | |
10 | 11 | const require = createRequire(import.meta.url); |
11 | 12 | const QUICKJS_WASM_PATH = require.resolve("quickjs-wasi/quickjs.wasm"); |
@@ -151,35 +152,6 @@ function isRecord(value: unknown): value is Record<string, unknown> {
|
151 | 152 | return Boolean(value && typeof value === "object" && !Array.isArray(value)); |
152 | 153 | } |
153 | 154 | |
154 | | -function toJsonSafe(value: unknown): unknown { |
155 | | -if (value === undefined) { |
156 | | -return null; |
157 | | -} |
158 | | -try { |
159 | | -const serialized = JSON.stringify(value); |
160 | | -return serialized === undefined ? null : (JSON.parse(serialized) as unknown); |
161 | | -} catch { |
162 | | -if (value instanceof Error) { |
163 | | -return { name: value.name, message: value.message }; |
164 | | -} |
165 | | -if (value === null) { |
166 | | -return null; |
167 | | -} |
168 | | -switch (typeof value) { |
169 | | -case "string": |
170 | | -case "number": |
171 | | -case "boolean": |
172 | | -return value; |
173 | | -case "bigint": |
174 | | -case "symbol": |
175 | | -case "function": |
176 | | -return String(value); |
177 | | -default: |
178 | | -return Object.prototype.toString.call(value); |
179 | | -} |
180 | | -} |
181 | | -} |
182 | | - |
183 | 155 | function errorMessage(error: unknown): string { |
184 | 156 | if (error instanceof JSException) { |
185 | 157 | return error.stack || error.message || String(error); |
|
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。