src / core / artifacts.ts
src / core / artifacts.ts
import { createId } from "./id";
import { sha256Text } from "./hash";
import type { InternalStorage } from "./internalStorage";
import { jsonStringify } from "./json";
export type ArtifactKind =
| "text"
| "json"
| "diff"
| "command_output"
| "agent_transcript"
| "snapshot"
| "web_content"
| "research_report"
| "task_board"
| "vcs_output"
| "browser_snapshot";
export interface ArtifactRef {
kind: ArtifactKind;
path: string;
sha256: string;
bytes: number;
description?: string;
}
export class ArtifactStore {
public readonly relativeRoot: string;
public readonly absoluteRoot: string;
public constructor(private readonly storage: InternalStorage) {
this.relativeRoot = storage.relative("artifacts");
this.absoluteRoot = storage.lexical(this.relativeRoot);
}
public async initialize(): Promise<void> {
await this.storage.ensureDirectory(this.relativeRoot);
}
public async writeText(
kind: ArtifactKind,
content: string,
extension = "txt",
description?: string,
): Promise<ArtifactRef> {
await this.initialize();
const relativePath = `${this.relativeRoot}/${createId(kind)}.${extension}`;
await this.storage.writeText(relativePath, content);
return this.ref(
kind,
relativePath,
Buffer.byteLength(content),
sha256Text(content),
description,
);
}
public async writeJson(
value: unknown,
description?: string,
): Promise<ArtifactRef> {
const content = `${jsonStringify(value)}\n`;
await this.initialize();
const relativePath = `${this.relativeRoot}/${createId("json")}.json`;
await this.storage.writeText(relativePath, content);
return this.ref(
"json",
relativePath,
Buffer.byteLength(content),
sha256Text(content),
description,
);
}
public relativePath(path: string): string {
return this.storage.boundary.relativePath(path);
}
private ref(
kind: ArtifactKind,
relativePath: string,
bytes: number,
sha256: string,
description?: string,
): ArtifactRef {
return {
kind,
path: relativePath.replaceAll("\\", "/"),
sha256,
bytes,
...(description ? { description } : {}),
};
}
}
import { createId } from "./id";
import { sha256Text } from "./hash";
import type { InternalStorage } from "./internalStorage";
import { jsonStringify } from "./json";
export type ArtifactKind =
| "text"
| "json"
| "diff"
| "command_output"
| "agent_transcript"
| "snapshot"
| "web_content"
| "research_report"
| "task_board"
| "vcs_output"
| "browser_snapshot";
export interface ArtifactRef {
kind: ArtifactKind;
path: string;
sha256: string;
bytes: number;
description?: string;
}
export class ArtifactStore {
public readonly relativeRoot: string;
public readonly absoluteRoot: string;
public constructor(private readonly storage: InternalStorage) {
this.relativeRoot = storage.relative("artifacts");
this.absoluteRoot = storage.lexical(this.relativeRoot);
}
public async initialize(): Promise<void> {
await this.storage.ensureDirectory(this.relativeRoot);
}
public async writeText(
kind: ArtifactKind,
content: string,
extension = "txt",
description?: string,
): Promise<ArtifactRef> {
await this.initialize();
const relativePath = `${this.relativeRoot}/${createId(kind)}.${extension}`;
await this.storage.writeText(relativePath, content);
return this.ref(
kind,
relativePath,
Buffer.byteLength(content),
sha256Text(content),
description,
);
}
public async writeJson(
value: unknown,
description?: string,
): Promise<ArtifactRef> {
const content = `${jsonStringify(value)}\n`;
await this.initialize();
const relativePath = `${this.relativeRoot}/${createId("json")}.json`;
await this.storage.writeText(relativePath, content);
return this.ref(
"json",
relativePath,
Buffer.byteLength(content),
sha256Text(content),
description,
);
}
public relativePath(path: string): string {
return this.storage.boundary.relativePath(path);
}
private ref(
kind: ArtifactKind,
relativePath: string,
bytes: number,
sha256: string,
description?: string,
): ArtifactRef {
return {
kind,
path: relativePath.replaceAll("\\", "/"),
sha256,
bytes,
...(description ? { description } : {}),
};
}
}