Fix duplicate tool lifecycle computations, extract shared utils, preserve null in toCanonicalJsonValue
- Cache canonicalToolLifecycleData/buildCanonicalToolLifecycleData results in local variables instead of calling twice with identical arguments (ClaudeAdapter x3, CodexAdapter x1) - Extract identical normalizeCommandValue and pushChangedFile helpers into apps/server/src/provider/toolDataUtils.ts, imported by both adapters - Fix toCanonicalJsonValue to preserve null as a valid CanonicalJsonValue instead of silently converting it to undefined via the nullish coalescing operator
This commit is contained in:
4 files changed
+57
-97
No files matched your search
@@ -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(
|
||||
|
||||
@@ -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<string, unknown> | 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<string>, 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<string>, 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: {
|
||||
|
||||
@@ -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<string>, 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<string>, 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 } : {};
|
||||
})(),
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
@@ -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<string>, 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);
|
||||
}
|
||||
Reference in new issue
Block a user