UNPKG

@kddd/artinet-sdk

Version:

TypeScript SDK for the Agent2Agent (A2A) Protocol

111 lines 5.38 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.defaultGetTaskPushNotificationMethod = exports.defaultSetTaskPushNotificationMethod = exports.defaultCancelTaskMethod = exports.defaultGetTaskMethod = exports.defaultSendTaskMethod = void 0; const index_js_1 = require("../../../utils/index.js"); const index_js_2 = require("../../../utils/index.js"); const state_js_1 = require("../state.js"); const index_js_3 = require("../../../utils/index.js"); const index_js_4 = require("../../../utils/index.js"); const defaultSendTaskMethod = async (deps, requestParams, callback) => { const { taskStore, taskHandler, createTaskContext } = deps; const taskId = (0, index_js_1.extractTaskId)(requestParams.id); const { message, sessionId, metadata } = requestParams; let currentData = await (0, state_js_1.loadState)(taskStore, taskId, message, sessionId, metadata); const context = createTaskContext(currentData.task, message, currentData.history); const generator = taskHandler(context); try { for await (const update of generator) { currentData = await (0, state_js_1.updateState)(taskStore, currentData, update); } return callback(null, currentData.task); // Success } catch (innerError) { const failedUpdate = (0, index_js_3.FAILED_UPDATE)(`Task processing failed: ${innerError instanceof Error ? innerError.message : String(innerError)}`); // Ensure state is saved before calling callback with error await (0, state_js_1.updateState)(taskStore, currentData, failedUpdate); // Throwing here would be caught by createMethod's catch block if (innerError instanceof index_js_3.SystemError) { throw innerError; } throw (0, index_js_3.INTERNAL_ERROR)(innerError); } }; exports.defaultSendTaskMethod = defaultSendTaskMethod; const defaultGetTaskMethod = async (deps, requestParams, callback) => { const { taskStore } = deps; const taskId = (0, index_js_1.extractTaskId)(requestParams.id); if (!taskId) throw (0, index_js_3.INVALID_PARAMS)("Missing task ID"); const data = await taskStore.load(taskId); if (!data) { (0, index_js_2.logError)("taskGetLogic", "Task not found", { taskId }); throw (0, index_js_3.TASK_NOT_FOUND)("Task Id: " + taskId); } callback(null, data.task); // Success }; exports.defaultGetTaskMethod = defaultGetTaskMethod; const defaultCancelTaskMethod = async (deps, requestParams, callback) => { const { taskStore, activeCancellations, closeStreamsForTask } = deps; const taskId = (0, index_js_1.extractTaskId)(requestParams.id); if (!taskId) throw (0, index_js_3.INVALID_PARAMS)("Missing task ID"); const data = await taskStore.load(taskId); if (!data) throw (0, index_js_3.TASK_NOT_FOUND)("Task Id: " + taskId); if (index_js_4.FINAL_STATES.includes(data.task.status.state)) { throw (0, index_js_3.TASK_NOT_CANCELABLE)("Task is in a final state"); } activeCancellations.add(taskId); const canceledTask = { ...data.task, status: { state: "canceled", timestamp: (0, index_js_1.getCurrentTimestamp)(), message: { role: "agent", parts: [{ type: "text", text: "Task was canceled." }], }, }, }; const updatedData = { task: canceledTask, history: data.history }; await taskStore.save(updatedData); closeStreamsForTask(taskId); return callback(null, canceledTask); }; exports.defaultCancelTaskMethod = defaultCancelTaskMethod; const defaultSetTaskPushNotificationMethod = async (deps, requestParams, callback) => { const { taskStore, card } = deps; (0, index_js_2.logWarn)("tasks/pushNotification/set", "Push notifications not fully implemented."); if (!card.capabilities?.pushNotifications) { throw (0, index_js_3.PUSH_NOTIFICATION_NOT_SUPPORTED)("Push notifications not supported"); } const config = requestParams; const taskId = (0, index_js_1.extractTaskId)(config.id); if (!taskId || !config.pushNotificationConfig?.url) { throw (0, index_js_3.INVALID_PARAMS)("Missing task ID or push notification URL"); } const data = await taskStore.load(taskId); if (!data) { throw (0, index_js_3.TASK_NOT_FOUND)("Task Id: " + taskId); } return callback(null, config); }; exports.defaultSetTaskPushNotificationMethod = defaultSetTaskPushNotificationMethod; const defaultGetTaskPushNotificationMethod = async (deps, requestParams, callback) => { const { taskStore, card } = deps; (0, index_js_2.logWarn)("tasks/pushNotification/get", "Push notifications not fully implemented."); if (!card.capabilities?.pushNotifications) { throw (0, index_js_3.PUSH_NOTIFICATION_NOT_SUPPORTED)("Push notifications not supported"); } const taskId = (0, index_js_1.extractTaskId)(requestParams.id); if (!taskId) { throw (0, index_js_3.INVALID_PARAMS)("Missing task ID"); } const data = await taskStore.load(taskId); if (!data) { throw (0, index_js_3.TASK_NOT_FOUND)("Task Id: " + taskId); } return callback(null, null); }; exports.defaultGetTaskPushNotificationMethod = defaultGetTaskPushNotificationMethod; //# sourceMappingURL=a2a-methods.js.map