UNPKG

@dexwox-labs/a2a-core

Version:

Core types, validation and telemetry for Google's Agent-to-Agent (A2A) protocol - shared foundation for client and server implementations

141 lines 4.96 kB
"use strict"; /** * @module ArtifactUtils * @description Utilities for working with artifacts in the A2A protocol * * Artifacts are used to represent various types of content (text, files, data) * that can be attached to tasks and messages in the A2A protocol. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.ArtifactErrorCode = void 0; exports.serializeArtifact = serializeArtifact; exports.deserializeArtifact = deserializeArtifact; exports.validateArtifact = validateArtifact; const errors_1 = require("../errors"); /** * Error codes specific to artifact operations * * These error codes are used to identify specific issues that can occur * when working with artifacts, such as serialization failures or invalid types. */ var ArtifactErrorCode; (function (ArtifactErrorCode) { /** Error code when artifact serialization fails (-32050) */ ArtifactErrorCode[ArtifactErrorCode["SerializationFailed"] = -32050] = "SerializationFailed"; /** Error code when artifact deserialization fails (-32051) */ ArtifactErrorCode[ArtifactErrorCode["DeserializationFailed"] = -32051] = "DeserializationFailed"; /** Error code when artifact content is missing (-32052) */ ArtifactErrorCode[ArtifactErrorCode["MissingContent"] = -32052] = "MissingContent"; /** Error code when artifact type is invalid (-32053) */ ArtifactErrorCode[ArtifactErrorCode["InvalidType"] = -32053] = "InvalidType"; })(ArtifactErrorCode || (exports.ArtifactErrorCode = ArtifactErrorCode = {})); /** * Serializes an artifact to a JSON string * * This function converts an Artifact object to a JSON string representation * that can be stored or transmitted. It handles error cases by throwing * appropriate A2AError instances. * * @param artifact - Artifact object to serialize * @returns JSON string representation of the artifact * @throws {A2AError} If serialization fails * * @example * ```typescript * const artifact: Artifact = { * id: 'artifact-123', * type: 'text', * content: 'This is a text artifact', * metadata: { created: new Date().toISOString() } * }; * * try { * const serialized = serializeArtifact(artifact); * console.log('Serialized artifact:', serialized); * } catch (error) { * console.error('Failed to serialize:', error); * } * ``` */ function serializeArtifact(artifact) { try { return JSON.stringify(artifact); } catch (err) { throw new errors_1.A2AError('Failed to serialize artifact', ArtifactErrorCode.SerializationFailed, { cause: err }); } } /** * Deserializes an artifact from a JSON string * * This function converts a JSON string representation back into an Artifact object. * It handles error cases by throwing appropriate A2AError instances. * * @param data - JSON encoded artifact data * @returns Deserialized Artifact object * @throws {A2AError} If deserialization fails * * @example * ```typescript * try { * const artifactJson = '{"id":"artifact-123","type":"text","content":"This is a text artifact"}'; * const artifact = deserializeArtifact(artifactJson); * console.log('Artifact type:', artifact.type); * console.log('Artifact content:', artifact.content); * } catch (error) { * console.error('Failed to deserialize:', error); * } * ``` */ function deserializeArtifact(data) { try { return JSON.parse(data); } catch (err) { throw new errors_1.A2AError('Failed to deserialize artifact', ArtifactErrorCode.DeserializationFailed, { cause: err }); } } /** * Validates an artifact's structure and content * * This function checks that an artifact has the required fields and that * the type is one of the supported values ('text', 'file', or 'data'). * It throws an appropriate error if validation fails. * * @param artifact - Artifact object to validate * @returns True if the artifact is valid * @throws {A2AError} If validation fails, with specific error codes * * @example * ```typescript * try { * // Valid artifact * const valid = validateArtifact({ * id: 'artifact-123', * type: 'file', * content: 'base64-encoded-content', * metadata: { filename: 'document.pdf', mimeType: 'application/pdf' } * }); * console.log('Artifact is valid:', valid); * * // This would throw an error * validateArtifact({ * id: 'invalid-artifact', * type: 'unsupported-type', // Invalid type * content: 'some-content' * }); * } catch (error) { * console.error('Validation failed:', error); * } * ``` */ function validateArtifact(artifact) { if (!artifact.content) { throw new errors_1.A2AError('Missing content for artifact', ArtifactErrorCode.MissingContent); } if (!['text', 'file', 'data'].includes(artifact.type)) { throw new errors_1.A2AError('Invalid artifact type', ArtifactErrorCode.InvalidType); } return true; } //# sourceMappingURL=artifact.js.map