Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions packages/core/src/canvas/runArtifactSchemas.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { describe, expect, it } from "vitest";
import { OUTPUT_ARTIFACT_TYPES, parseRunArtifacts } from "./runArtifactSchemas";

describe("parseRunArtifacts", () => {
it.each([
{ name: "undefined", raw: undefined },
{ name: "null", raw: null },
{ name: "an object", raw: { artifacts: [] } },
{ name: "a string", raw: "output" },
])("reads $name as no artifacts", ({ raw }) => {
expect(parseRunArtifacts(raw, OUTPUT_ARTIFACT_TYPES)).toEqual([]);
});

// A run's manifest mixes deliverables with plumbing nobody asked to see.
it("keeps only the requested types", () => {
const outputs = parseRunArtifacts(
[
{ id: "a", type: "output", name: "report.md" },
{ id: "b", type: "user_attachment", name: "clipboard.png" },
{ id: "c", type: "skill_bundle", name: "skills.zip" },
],
OUTPUT_ARTIFACT_TYPES,
);
expect(outputs).toEqual([{ id: "a", type: "output", name: "report.md" }]);
});

it("reads the size and upload time the row renders", () => {
const artifact = {
id: "a",
type: "output",
name: "report.md",
size: 16861,
content_type: "text/markdown",
uploaded_at: "2026-07-27T08:27:26.896719+00:00",
};
expect(parseRunArtifacts([artifact], OUTPUT_ARTIFACT_TYPES)).toEqual([
artifact,
]);
});

it("keeps an artifact that omits the optional fields", () => {
expect(
parseRunArtifacts([{ type: "output" }], OUTPUT_ARTIFACT_TYPES),
).toEqual([{ type: "output" }]);
});

// One bad entry shouldn't take the Artifacts tab down with it.
it("drops entries that don't match the shape", () => {
const outputs = parseRunArtifacts(
[
{ id: 42, type: "output" },
null,
"not an object",
{ type: "output", storage_path: "runs/1/report.md" },
],
OUTPUT_ARTIFACT_TYPES,
);
expect(outputs).toEqual([
{ type: "output", storage_path: "runs/1/report.md" },
]);
});

it("ignores an artifact with no type", () => {
expect(
parseRunArtifacts([{ id: "a", name: "mystery" }], OUTPUT_ARTIFACT_TYPES),
).toEqual([]);
});
});
33 changes: 33 additions & 0 deletions packages/core/src/canvas/runArtifactSchemas.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { z } from "zod";

export const runArtifactSchema = z.object({
id: z.string().optional(),
name: z.string().optional(),
type: z.string().optional(),
size: z.number().optional(),
content_type: z.string().optional(),
storage_path: z.string().optional(),
uploaded_at: z.string().optional(),
});
export type RunArtifact = z.infer<typeof runArtifactSchema>;

/** Artifacts the agent hands back as deliverables, via the `upload_artifact` tool. */
export const OUTPUT_ARTIFACT_TYPES = ["output"] as const;

/**
* The run's artifacts of the given types, in manifest order. A run's manifest
* also carries plumbing the user never asked for — skill bundles, their own
* attachments — so callers name the types they want.
*/
export function parseRunArtifacts(
raw: unknown,
types: readonly string[],
): RunArtifact[] {
if (!Array.isArray(raw)) return [];
return raw.flatMap((entry) => {
const parsed = runArtifactSchema.safeParse(entry);
if (!parsed.success) return [];
const { type } = parsed.data;
return type && types.includes(type) ? [parsed.data] : [];
});
}
Loading
Loading