diff --git a/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts b/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts index f930599950..8f8c681eef 100644 --- a/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts +++ b/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts @@ -107,10 +107,9 @@ function toCanonicalJsonValue(value: unknown): CanonicalJsonValue | undefined { if (value === undefined) { return undefined; } - const normalized = JSON.parse( + return JSON.parse( JSON.stringify(value, (_key, nestedValue) => (nestedValue === undefined ? null : nestedValue)), - ) as CanonicalJsonValue | null; - return normalized ?? undefined; + ) as CanonicalJsonValue; } function buildContextWindowActivityPayload( diff --git a/apps/server/src/provider/Layers/ClaudeAdapter.ts b/apps/server/src/provider/Layers/ClaudeAdapter.ts index 18fc226842..65d3cba77b 100644 --- a/apps/server/src/provider/Layers/ClaudeAdapter.ts +++ b/apps/server/src/provider/Layers/ClaudeAdapter.ts @@ -75,6 +75,7 @@ import { type ProviderAdapterError, } from "../Errors.ts"; import { ClaudeAdapter, type ClaudeAdapterShape } from "../Services/ClaudeAdapter.ts"; +import { normalizeCommandValue, pushChangedFile } from "../toolDataUtils.ts"; import { type EventNdjsonLogger, makeEventNdjsonLogger } from "./EventNdjsonLogger.ts"; const PROVIDER = "claudeAgent" as const; @@ -747,32 +748,6 @@ function tryParseJsonRecord(value: string): Record | undefined } } -function normalizeCommandValue(value: unknown): string | undefined { - if (typeof value === "string") { - const trimmed = value.trim(); - return trimmed.length > 0 ? trimmed : undefined; - } - if (!Array.isArray(value)) { - return undefined; - } - const parts = value - .map((entry) => (typeof entry === "string" ? entry.trim() : "")) - .filter((entry) => entry.length > 0); - return parts.length > 0 ? parts.join(" ") : undefined; -} - -function pushChangedFile(target: string[], seen: Set, value: unknown) { - if (typeof value !== "string") { - return; - } - const normalized = value.trim(); - if (normalized.length === 0 || seen.has(normalized)) { - return; - } - seen.add(normalized); - target.push(normalized); -} - function collectChangedFiles(value: unknown, target: string[], seen: Set, depth: number) { if (depth > 4 || target.length >= 12) { return; @@ -1540,19 +1515,14 @@ function makeClaudeAdapter(options?: ClaudeAdapterLiveOptions) { status: status === "completed" ? "completed" : "failed", title: tool.title, ...(tool.detail ? { detail: tool.detail } : {}), - ...(canonicalToolLifecycleData({ - itemType: tool.itemType, - toolName: tool.toolName, - input: tool.input, - }) - ? { - data: canonicalToolLifecycleData({ - itemType: tool.itemType, - toolName: tool.toolName, - input: tool.input, - }), - } - : {}), + ...(() => { + const toolData = canonicalToolLifecycleData({ + itemType: tool.itemType, + toolName: tool.toolName, + input: tool.input, + }); + return toolData ? { data: toolData } : {}; + })(), }, providerRefs: nativeProviderRefs(context, { providerItemId: tool.itemId }), raw: { @@ -1744,19 +1714,14 @@ function makeClaudeAdapter(options?: ClaudeAdapterLiveOptions) { status: "inProgress", title: nextTool.title, ...(nextTool.detail ? { detail: nextTool.detail } : {}), - ...(canonicalToolLifecycleData({ - itemType: nextTool.itemType, - toolName: nextTool.toolName, - input: nextTool.input, - }) - ? { - data: canonicalToolLifecycleData({ - itemType: nextTool.itemType, - toolName: nextTool.toolName, - input: nextTool.input, - }), - } - : {}), + ...(() => { + const toolData = canonicalToolLifecycleData({ + itemType: nextTool.itemType, + toolName: nextTool.toolName, + input: nextTool.input, + }); + return toolData ? { data: toolData } : {}; + })(), }, providerRefs: nativeProviderRefs(context, { providerItemId: nextTool.itemId }), raw: { @@ -1822,19 +1787,14 @@ function makeClaudeAdapter(options?: ClaudeAdapterLiveOptions) { status: "inProgress", title: tool.title, ...(tool.detail ? { detail: tool.detail } : {}), - ...(canonicalToolLifecycleData({ - itemType: tool.itemType, - toolName: tool.toolName, - input: toolInput, - }) - ? { - data: canonicalToolLifecycleData({ - itemType: tool.itemType, - toolName: tool.toolName, - input: toolInput, - }), - } - : {}), + ...(() => { + const toolData = canonicalToolLifecycleData({ + itemType: tool.itemType, + toolName: tool.toolName, + input: toolInput, + }); + return toolData ? { data: toolData } : {}; + })(), }, providerRefs: nativeProviderRefs(context, { providerItemId: tool.itemId }), raw: { diff --git a/apps/server/src/provider/Layers/CodexAdapter.ts b/apps/server/src/provider/Layers/CodexAdapter.ts index 037b8320a0..dea9bed84f 100644 --- a/apps/server/src/provider/Layers/CodexAdapter.ts +++ b/apps/server/src/provider/Layers/CodexAdapter.ts @@ -33,6 +33,7 @@ import { type ProviderAdapterError, } from "../Errors.ts"; import { CodexAdapter, type CodexAdapterShape } from "../Services/CodexAdapter.ts"; +import { normalizeCommandValue, pushChangedFile } from "../toolDataUtils.ts"; import { CodexAppServerManager, type CodexAppServerStartSessionInput, @@ -111,32 +112,6 @@ function asNumber(value: unknown): number | undefined { return typeof value === "number" && Number.isFinite(value) ? value : undefined; } -function normalizeCommandValue(value: unknown): string | undefined { - if (typeof value === "string") { - const trimmed = value.trim(); - return trimmed.length > 0 ? trimmed : undefined; - } - if (!Array.isArray(value)) { - return undefined; - } - const parts = value - .map((entry) => (typeof entry === "string" ? entry.trim() : "")) - .filter((entry) => entry.length > 0); - return parts.length > 0 ? parts.join(" ") : undefined; -} - -function pushChangedFile(target: string[], seen: Set, value: unknown) { - if (typeof value !== "string") { - return; - } - const normalized = value.trim(); - if (normalized.length === 0 || seen.has(normalized)) { - return; - } - seen.add(normalized); - target.push(normalized); -} - function collectChangedFiles(value: unknown, target: string[], seen: Set, depth: number) { if (depth > 4 || target.length >= 12) { return; @@ -670,9 +645,10 @@ function mapItemLifecycle( ...(status ? { status } : {}), ...(itemTitle(itemType) ? { title: itemTitle(itemType) } : {}), ...(detail ? { detail } : {}), - ...(buildCanonicalToolLifecycleData(itemType, source, payload ?? {}) - ? { data: buildCanonicalToolLifecycleData(itemType, source, payload ?? {}) } - : {}), + ...(() => { + const toolData = buildCanonicalToolLifecycleData(itemType, source, payload ?? {}); + return toolData ? { data: toolData } : {}; + })(), }, }; } diff --git a/apps/server/src/provider/toolDataUtils.ts b/apps/server/src/provider/toolDataUtils.ts new file mode 100644 index 0000000000..9f49093613 --- /dev/null +++ b/apps/server/src/provider/toolDataUtils.ts @@ -0,0 +1,25 @@ +export function normalizeCommandValue(value: unknown): string | undefined { + if (typeof value === "string") { + const trimmed = value.trim(); + return trimmed.length > 0 ? trimmed : undefined; + } + if (!Array.isArray(value)) { + return undefined; + } + const parts = value + .map((entry) => (typeof entry === "string" ? entry.trim() : "")) + .filter((entry) => entry.length > 0); + return parts.length > 0 ? parts.join(" ") : undefined; +} + +export function pushChangedFile(target: string[], seen: Set, value: unknown) { + if (typeof value !== "string") { + return; + } + const normalized = value.trim(); + if (normalized.length === 0 || seen.has(normalized)) { + return; + } + seen.add(normalized); + target.push(normalized); +}