UNPKG

@artinet/sdk

Version:

A TypeScript SDK for building collaborative AI agents.

74 lines (73 loc) 2.25 kB
/** * Copyright 2025 The Artinet Project * SPDX-License-Identifier: Apache-2.0 */ import { A2A } from "../../types/index.js"; import { logger } from "../../config/index.js"; import { handleUpdate } from "../../services/a2a/handlers/update.js"; import { v4 } from "uuid"; import { getCurrentTimestamp } from "../../utils/utils.js"; class LoadManager { _loadable; constructor(_loadable) { this._loadable = _loadable; } async get(id, _context) { return await this._loadable.load(id, _context); } async set(id, data, context) { return await this._loadable.save(data, context, id); } async delete(id) { logger.warn("Delete not supported for loadable", { id }); return; } async has(id) { return (await this._loadable.load(id)) !== undefined; } } class TaskManager extends LoadManager { constructor(_loadable) { super(_loadable); } async update(context, update) { logger.info(`TaskManager[update]: updating task`, { taskId: context.taskId, }); logger.debug(`TaskManager[update]: update`, { update }); logger.debug(`TaskManager[update]: context`, { context }); const task = await handleUpdate({ context, task: await context.getTask(), update, }); logger.debug(`TaskManager[update]: task`, task); await this.set(task.id, task); return task; } async create(params) { if (params.id) { const task = await this.get(params.id); if (task) { return task; } } logger.info(`TaskManager[create]: creating task`, { id: params.id }); const task = { ...params, id: params.id ?? v4(), contextId: params.contextId ?? v4(), kind: "task", status: { state: A2A.TaskState.submitted, timestamp: getCurrentTimestamp(), }, }; logger.debug(`TaskManager[create]:`, { taskId: task.id }); await this.set(task.id, task); return task; } } export const tasks = (taskStore) => { return new TaskManager(taskStore); };