Update files
This commit is contained in:
@@ -10,6 +10,8 @@ import type {
|
||||
AgentPersistenceHandle,
|
||||
AgentRuntimeInfo,
|
||||
AgentTimelineItem,
|
||||
ToolCallDetail,
|
||||
ToolCallTimelineItem,
|
||||
AgentUsage,
|
||||
} from "../server/agent/agent-sdk-types.js";
|
||||
|
||||
@@ -147,8 +149,150 @@ export type StructuredToolResult =
|
||||
| { type: "file_read"; filePath: string; content: string }
|
||||
| { type: "generic"; data: unknown };
|
||||
|
||||
export const AgentTimelineItemPayloadSchema: z.ZodType<AgentTimelineItem> =
|
||||
z.discriminatedUnion("type", [
|
||||
const ToolCallDetailPayloadSchema: z.ZodType<ToolCallDetail> = z.discriminatedUnion("type", [
|
||||
z.object({
|
||||
type: z.literal("shell"),
|
||||
command: z.string(),
|
||||
cwd: z.string().optional(),
|
||||
output: z.string().optional(),
|
||||
exitCode: z.number().nullable().optional(),
|
||||
}),
|
||||
z.object({
|
||||
type: z.literal("read"),
|
||||
filePath: z.string(),
|
||||
content: z.string().optional(),
|
||||
offset: z.number().optional(),
|
||||
limit: z.number().optional(),
|
||||
}),
|
||||
z.object({
|
||||
type: z.literal("edit"),
|
||||
filePath: z.string(),
|
||||
oldString: z.string().optional(),
|
||||
newString: z.string().optional(),
|
||||
unifiedDiff: z.string().optional(),
|
||||
}),
|
||||
z.object({
|
||||
type: z.literal("write"),
|
||||
filePath: z.string(),
|
||||
content: z.string().optional(),
|
||||
}),
|
||||
z.object({
|
||||
type: z.literal("search"),
|
||||
query: z.string(),
|
||||
}),
|
||||
]);
|
||||
|
||||
const NonUndefinedUnknownSchema = z.union([
|
||||
z.null(),
|
||||
z.boolean(),
|
||||
z.number(),
|
||||
z.string(),
|
||||
z.array(z.unknown()),
|
||||
z.object({}).passthrough(),
|
||||
]);
|
||||
|
||||
const NonNullUnknownSchema = z.union([
|
||||
z.boolean(),
|
||||
z.number(),
|
||||
z.string(),
|
||||
z.array(z.unknown()),
|
||||
z.object({}).passthrough(),
|
||||
]);
|
||||
|
||||
const ToolCallBasePayloadSchema = z.object({
|
||||
type: z.literal("tool_call"),
|
||||
callId: z.string(),
|
||||
name: z.string(),
|
||||
input: NonUndefinedUnknownSchema,
|
||||
output: NonUndefinedUnknownSchema,
|
||||
detail: ToolCallDetailPayloadSchema.optional(),
|
||||
metadata: z.record(z.unknown()).optional(),
|
||||
});
|
||||
|
||||
const ToolCallRunningPayloadSchema = ToolCallBasePayloadSchema.extend({
|
||||
status: z.literal("running"),
|
||||
error: z.null(),
|
||||
});
|
||||
|
||||
const ToolCallCompletedPayloadSchema = ToolCallBasePayloadSchema.extend({
|
||||
status: z.literal("completed"),
|
||||
error: z.null(),
|
||||
});
|
||||
|
||||
const ToolCallFailedPayloadSchema = ToolCallBasePayloadSchema.extend({
|
||||
status: z.literal("failed"),
|
||||
error: NonNullUnknownSchema,
|
||||
});
|
||||
|
||||
const ToolCallCanceledPayloadSchema = ToolCallBasePayloadSchema.extend({
|
||||
status: z.literal("canceled"),
|
||||
error: z.null(),
|
||||
});
|
||||
|
||||
const LEGACY_TOOL_CALL_STATUS_MAP: Record<string, ToolCallTimelineItem["status"]> = {
|
||||
inprogress: "running",
|
||||
in_progress: "running",
|
||||
started: "running",
|
||||
complete: "completed",
|
||||
done: "completed",
|
||||
success: "completed",
|
||||
errored: "failed",
|
||||
error: "failed",
|
||||
cancelled: "canceled",
|
||||
};
|
||||
|
||||
function normalizeLegacyToolCallPayload(value: unknown): unknown {
|
||||
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
||||
return value;
|
||||
}
|
||||
|
||||
const record = value as Record<string, unknown>;
|
||||
if (record.type !== "tool_call") {
|
||||
return value;
|
||||
}
|
||||
|
||||
const normalized: Record<string, unknown> = { ...record };
|
||||
const rawStatus = typeof record.status === "string" ? record.status.trim() : "";
|
||||
|
||||
if (rawStatus.length > 0) {
|
||||
const statusKey = rawStatus.toLowerCase().replace(/[\s-]+/g, "_");
|
||||
const mappedStatus = LEGACY_TOOL_CALL_STATUS_MAP[statusKey];
|
||||
if (mappedStatus) {
|
||||
normalized.status = mappedStatus;
|
||||
}
|
||||
}
|
||||
|
||||
if (!("input" in normalized)) {
|
||||
normalized.input = null;
|
||||
}
|
||||
|
||||
if (!("output" in normalized)) {
|
||||
normalized.output = null;
|
||||
}
|
||||
|
||||
if (normalized.status === "failed") {
|
||||
if (normalized.error === undefined || normalized.error === null) {
|
||||
normalized.error = { message: "Tool call failed" };
|
||||
}
|
||||
} else if (normalized.error === undefined || normalized.error !== null) {
|
||||
normalized.error = null;
|
||||
}
|
||||
|
||||
return normalized;
|
||||
}
|
||||
|
||||
const ToolCallTimelineItemPayloadSchema: z.ZodType<ToolCallTimelineItem, z.ZodTypeDef, unknown> = z.preprocess(
|
||||
normalizeLegacyToolCallPayload,
|
||||
z.union([
|
||||
ToolCallRunningPayloadSchema,
|
||||
ToolCallCompletedPayloadSchema,
|
||||
ToolCallFailedPayloadSchema,
|
||||
ToolCallCanceledPayloadSchema,
|
||||
])
|
||||
);
|
||||
|
||||
export const AgentTimelineItemPayloadSchema: z.ZodType<AgentTimelineItem, z.ZodTypeDef, unknown> =
|
||||
z.union([
|
||||
z.object({
|
||||
type: z.literal("user_message"),
|
||||
text: z.string(),
|
||||
@@ -162,16 +306,7 @@ export const AgentTimelineItemPayloadSchema: z.ZodType<AgentTimelineItem> =
|
||||
type: z.literal("reasoning"),
|
||||
text: z.string(),
|
||||
}),
|
||||
z.object({
|
||||
type: z.literal("tool_call"),
|
||||
name: z.string(),
|
||||
callId: z.string().optional(),
|
||||
status: z.string().optional(),
|
||||
input: z.unknown().optional(),
|
||||
output: z.unknown().optional(),
|
||||
error: z.unknown().optional(),
|
||||
metadata: z.record(z.unknown()).optional(),
|
||||
}),
|
||||
ToolCallTimelineItemPayloadSchema,
|
||||
z.object({
|
||||
type: z.literal("todo"),
|
||||
items: z.array(
|
||||
|
||||
Reference in New Issue
Block a user