@artinet/sdk
Version:
A TypeScript SDK for building collaborative AI agents.
46 lines (45 loc) • 1.56 kB
JavaScript
/**
* Copyright 2025 The Artinet Project
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @fileoverview A2A Context Factory Functions
*
* This module provides factory functions for creating A2A execution contexts
* and command channels. It handles the composition of various context components
* including command proxies, event managers, and cancellation mechanisms.
*
* @module A2AContextFactory
* @version 0.6.0-preview
* @since 0.5.6
* @author The Artinet Project
*/
import { createStateMachine } from "./state-machine.js";
import { v4 as uuidv4 } from "uuid";
export function createBaseContext({ contextId = uuidv4(), service, task, overrides, abortSignal = new AbortController().signal, }) {
const isCancelled = async () => (await service.cancellations.has(task.id)) || abortSignal.aborted;
const getState = async (args) => args ? await service.tasks.get(args) : task;
const context = {
contextId: contextId,
service: service,
publisher: createStateMachine({ contextId, service, task, overrides }),
isCancelled,
abortSignal: abortSignal,
getState,
};
return context;
}
export function createContext({ baseContext, taskId, messenger, extensions, references, userId, }) {
const getTask = async () => baseContext.publisher.currentTask;
const context = {
...baseContext,
taskId,
userMessage: messenger.message,
messages: messenger,
getTask,
extensions,
references,
userId,
};
return context;
}