@kddd/artinet-sdk
Version:
TypeScript SDK for the Agent2Agent (A2A) Protocol
85 lines • 2.98 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getCurrentTimestamp = getCurrentTimestamp;
exports.isObject = isObject;
exports.isTaskStatusUpdate = isTaskStatusUpdate;
exports.isArtifactUpdate = isArtifactUpdate;
exports.validateTaskResubscribeParams = validateTaskResubscribeParams;
exports.validateTaskSendParams = validateTaskSendParams;
exports.extractTaskId = extractTaskId;
const errors_js_1 = require("./errors.js");
/**
* Generates a timestamp in ISO 8601 format.
* @returns The current timestamp as a string.
*/
function getCurrentTimestamp() {
return new Date().toISOString();
}
/**
* Checks if a value is a plain object (excluding arrays and null).
* @param value The value to check.
* @returns True if the value is a plain object, false otherwise.
*/
function isObject(value) {
return typeof value === "object" && value !== null && !Array.isArray(value);
}
/**
* Type guard to check if an object is a TaskStatus update (lacks 'parts').
* Used to differentiate yielded updates from the handler.
*/
function isTaskStatusUpdate(update) {
return isObject(update) && "state" in update && !("parts" in update);
}
/**
* Type guard to check if an object is an Artifact update (has 'parts').
* Used to differentiate yielded updates from the handler.
*/
function isArtifactUpdate(update) {
return isObject(update) && "parts" in update && !("state" in update);
}
function validateTaskResubscribeParams(params) {
// Structure validation
if (!params || typeof params !== "object") {
throw (0, errors_js_1.INVALID_PARAMS)("Invalid parameters");
}
if (typeof params.id !== "string" || params.id === "") {
throw (0, errors_js_1.INVALID_PARAMS)("Invalid task ID");
}
}
/**
* Validates a task send parameters object.
* @param params The parameters to validate
* @throws INVALID_PARAMS if the parameters are invalid
*/
function validateTaskSendParams(params) {
// Structure validation
if (!params || typeof params !== "object") {
throw (0, errors_js_1.INVALID_PARAMS)("Invalid parameters");
}
if (typeof params.id !== "string" || params.id === "") {
throw (0, errors_js_1.INVALID_PARAMS)("Invalid task ID");
}
// Message validation
if (!params.message || typeof params.message !== "object") {
throw (0, errors_js_1.INVALID_PARAMS)("Invalid message");
}
// Role validation
if (params.message.role !== "user") {
throw (0, errors_js_1.INVALID_PARAMS)("Invalid message role");
}
// Parts validation
if (!Array.isArray(params.message.parts) ||
params.message.parts.length === 0) {
throw (0, errors_js_1.INVALID_PARAMS)("Invalid message parts");
}
}
function extractTaskId(id) {
if (!id) {
throw (0, errors_js_1.INVALID_PARAMS)("Missing task ID");
}
if (typeof id === "number") {
return id.toString();
}
return id;
}
//# sourceMappingURL=utils.js.map