UNPKG

@a2alite/sdk

Version:

A Modular SDK (Server & Client) for Agent to Agent (A2A) protocol, with easy task lifecycle management

104 lines (103 loc) 2.61 kB
import { v4 as uuidv4 } from "uuid"; class ArtifactHandler { constructor(base) { this.artifact = { artifactId: base?.artifactId || uuidv4(), name: base?.name, description: base?.description, metadata: base?.metadata, parts: base?.parts || [], }; } withId(artifactId) { this.artifact.artifactId = artifactId; return this; } withName(name) { this.artifact.name = name; return this; } withDescription(description) { this.artifact.description = description; return this; } withParts(parts) { this.artifact.parts = [...parts]; return this; } addParts(parts) { this.artifact.parts.push(...parts); return this; } clearParts() { this.artifact.parts = []; return this; } withMetadata(metadata) { this.artifact.metadata = metadata; return this; } getArtifact() { return { ...this.artifact }; } getParts() { return [...this.artifact.parts]; } getTextParts() { return this.artifact.parts.filter((part) => part.kind === "text"); } getText() { return this.getTextParts() .map((part) => part.text) .join("\n"); } getFileParts() { return this.artifact.parts.filter((part) => part.kind === "file"); } getFiles() { return this.getFileParts().map((part) => part.file); } getDataParts() { return this.artifact.parts.filter((part) => part.kind === "data"); } getData() { return this.getDataParts().map((part) => part.data); } // Static factory methods static fromText(text, metadata) { return new ArtifactHandler({ parts: [ { kind: "text", text, metadata, }, ], }); } static fromFile(file, metadata) { const name = file.name || "file-artifact"; return new ArtifactHandler({ name, parts: [ { kind: "file", file, metadata, }, ], }); } static fromData(data, metadata) { return new ArtifactHandler({ parts: [ { kind: "data", data, metadata, }, ], }); } } export { ArtifactHandler };