UNPKG

@kddd/artinet-sdk

Version:

TypeScript SDK for the Agent2Agent (A2A) Protocol

117 lines 4.27 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.updateTaskStatus = updateTaskStatus; exports.checkBounds = checkBounds; exports.updateByIndex = updateByIndex; exports.updateByName = updateByName; exports.processArtifactUpdate = processArtifactUpdate; exports.updateState = updateState; exports.loadState = loadState; const utils_js_1 = require("../../utils/common/utils.js"); const errors_js_1 = require("../../utils/common/errors.js"); function updateTaskStatus(task, update) { return { ...task.status, ...update, timestamp: (0, utils_js_1.getCurrentTimestamp)() }; } function checkBounds(index, length) { return index >= 0 && index < length; } function updateByIndex(artifacts, index, update) { const existingArtifact = artifacts[index]; if (update.append) { //deep copy to avoid mutating original const appendedArtifact = JSON.parse(JSON.stringify(existingArtifact)); appendedArtifact.parts.push(...update.parts); if (update.metadata) { appendedArtifact.metadata = { ...(appendedArtifact.metadata || {}), ...update.metadata, }; } if (update.lastChunk !== undefined) { appendedArtifact.lastChunk = update.lastChunk; } if (update.description) { appendedArtifact.description = update.description; } artifacts[index] = appendedArtifact; } else { artifacts[index] = { ...update }; } return { artifacts, replaced: true }; } function updateByName(artifacts, update) { const namedIndex = artifacts.findIndex((a) => a.name === update.name); if (namedIndex >= 0) { artifacts[namedIndex] = { ...update }; return { artifacts, replaced: true }; } return { artifacts: artifacts, replaced: false }; } function processArtifactUpdate(artifacts, update) { const existingIndex = update.index ?? -1; let replaced = false; let newArtifacts = artifacts; if (checkBounds(existingIndex, artifacts.length)) { ({ artifacts: newArtifacts, replaced } = updateByIndex(artifacts, existingIndex, update)); } else if (update.name) { ({ artifacts: newArtifacts, replaced } = updateByName(artifacts, update)); } if (!replaced) { newArtifacts.push({ ...update }); if (newArtifacts.some((a) => a.index !== undefined)) { newArtifacts.sort((a, b) => (a.index ?? 0) - (b.index ?? 0)); } replaced = true; } return { artifacts: newArtifacts, replaced }; } async function updateState(taskStore, current, update) { const newTask = { ...current.task }; const newHistory = [...current.history]; if ((0, utils_js_1.isTaskStatusUpdate)(update)) { newTask.status = updateTaskStatus(newTask, update); if (update.message?.role === "agent") { newHistory.push(update.message); } } else if ((0, utils_js_1.isArtifactUpdate)(update)) { const artifacts = !newTask.artifacts ? [] : [...newTask.artifacts]; const { artifacts: newArtifacts, replaced } = processArtifactUpdate(artifacts, update); if (!replaced) { throw (0, errors_js_1.INVALID_REQUEST)("Invalid artifact update"); } newTask.artifacts = newArtifacts; } const currentData = { task: newTask, history: newHistory }; await taskStore.save(currentData); return currentData; } /** * Loads or creates a task and its history. * @param taskId The task ID * @param message The message to process * @param sessionId Optional session ID * @param metadata Optional metadata * @returns The task and history */ async function loadState(taskStore, taskId, message, sessionId, metadata) { const existingData = await taskStore.load(taskId); if (existingData) { return existingData; } const timestamp = (0, utils_js_1.getCurrentTimestamp)(); const newTask = { id: taskId, sessionId: sessionId, status: { state: "submitted", timestamp, }, metadata: metadata, }; const newHistory = [message]; return { task: newTask, history: newHistory }; } //# sourceMappingURL=state.js.map