@artinet/sdk
Version:
TypeScript SDK for Agentic Communication
237 lines • 7.45 kB
JavaScript
import { logDebug, register } from "../utils/index.js";
import { ExpressServer } from "./lib/express-server.js";
import { InMemoryTaskStore } from "./lib/storage/memory.js";
import { Protocol } from "../types/services/index.js";
/**
* @deprecated Use ExpressServer instead.
* Implements an A2A protocol compliant server using Express.
* Handles task creation, streaming, cancellation and more.
* Uses Jayson for JSON-RPC handling.
*/
export class A2AServer {
expressServer;
/**
* Returns the base path for the server
*/
getBasePath() {
return this.expressServer.basePath;
}
/**
* Returns the CORS options for the server
*/
getCorsOptions() {
return this.expressServer.corsOptions;
}
/**
* Returns the agent card for the server
*/
getCard() {
return this.expressServer.card;
}
/**
* Returns the task store
*/
getTaskStore() {
return this.expressServer.getService(Protocol.A2A)?.state.getTaskStore();
}
/**
* Returns the task handler
*/
getTaskHandler() {
return this.expressServer.engine;
}
/**
* Returns the set of active cancellations
*/
getActiveCancellations() {
return this.expressServer.getService(Protocol.A2A)?.state
.activeCancellations;
}
/**
* Returns the map of active streams
*/
getActiveStreams() {
return this.expressServer.getService(Protocol.A2A)?.state
.activeStreams;
}
/**
* Returns the port number
*/
getPort() {
return this.expressServer.port;
}
/**
* Returns the JSON-RPC server
*/
getRpcServer() {
throw new Error("Not implemented");
}
/**
* Returns the server instance
*/
getServerInstance() {
return this.expressServer.serverInstance;
}
/**
* Returns the Express app
*/
getExpressApp() {
return this.expressServer.getApp();
}
/**
* Returns a task context for the specified task and messages
*/
getTaskContext(task, userMessage, history) {
return this.createTaskContext(task, userMessage, history);
}
/**
* Returns the default agent card
*/
defaultAgentCard() {
return A2AServer.defaultAgentCard();
}
/**
* Creates a new A2AServer.
* @param handler The task handler function that will process tasks
* @param options Options for configuring the server
*/
constructor(params) {
this.expressServer = new ExpressServer({
card: params.card ?? A2AServer.defaultAgentCard(),
storage: params.taskStore ?? new InMemoryTaskStore(),
corsOptions: params.corsOptions,
basePath: params.basePath,
port: params.port,
fallbackPath: params.fallbackPath,
register: params.register,
engine: params.handler,
});
logDebug("A2AServer", "Server initialized", {
basePath: this.expressServer.basePath,
port: this.expressServer.port,
corsEnabled: !!this.expressServer.corsOptions,
});
}
/**
* Starts the Express server listening on the specified port.
* @returns The running Express application instance.
*/
start() {
if (this.expressServer.serverInstance) {
throw new Error("Server already started");
}
this.expressServer.start();
//lazily register your server with the A2A registry on startup
//this is so that you can start the server without having to wait for registration
//you can call also call this.registerServer() later to register your server
if (this.expressServer.register) {
this.registerServer();
}
return this.expressServer.getApp();
}
/**
* Stops the server and closes all connections.
* @returns A promise that resolves when the server is stopped.
*/
async stop() {
if (!this.expressServer.serverInstance) {
return;
}
await this.expressServer.stop();
}
/**
* Registers the server with the A2A registry.
* @returns A promise that resolves to the registration ID or an empty string if registration fails.
*/
async registerServer() {
if (this.expressServer.card) {
return await register(this.expressServer.card);
}
return "";
}
/**
* Handles task cancellation
* @param data Task and history data
* @param res Response object
*/
async onCancel(context, data, res) {
await this.expressServer.getService(Protocol.A2A)?.state.onCancel(context, data, res);
}
/**
* Handles cleanup when a task stream ends
* @param taskId The task ID
* @param res Response object
*/
async onEnd(taskId, res) {
await this.expressServer.getService(Protocol.A2A)?.state.onEnd(taskId, res);
}
/**
* Handles the message/stream method.
* @param req The SendTaskRequest object
* @param res The Express Response object
*/
async handleTaskSendSubscribe(req, res) {
await this.expressServer.getService(Protocol.A2A)?.handleSendStreamingMessage(req, res);
}
/**
* Handles the tasks/resubscribe method.
* @param req The TaskResubscriptionRequest object
* @param res The Express Response object
*/
async handleTaskResubscribe(req, res) {
await this.expressServer.getService(Protocol.A2A)?.handleTaskResubscribe(req, res);
}
/**
* Adds a response stream to the tracking map for a task.
* @param taskId The task ID
* @param res The response stream
*/
addStreamForTask(taskId, res) {
this.expressServer.getService(Protocol.A2A)?.state.addStreamForTask(taskId, res);
}
/**
* Removes a response stream from the tracking map for a task.
* @param taskId The task ID
* @param res The response stream
*/
removeStreamForTask(taskId, res) {
this.expressServer.getService(Protocol.A2A)?.state.removeStreamForTask(taskId, res);
}
/**
* Initializes the default agent card
*/
static defaultAgentCard() {
return {
name: "A2A Server",
description: "A general-purpose A2A protocol server",
version: "0.1.0",
url: "http://localhost",
capabilities: {
streaming: true,
pushNotifications: false,
stateTransitionHistory: true,
},
skills: [],
defaultInputModes: ["text"],
defaultOutputModes: ["text"],
};
}
/**
* Creates a TaskContext object for a task handler.
* @param task The task
* @param userMessage The user message
* @param history The message history
* @returns A TaskContext object
*/
createTaskContext(task, userMessage, history, configuration) {
return this.expressServer.getService(Protocol.A2A)?.state.createTaskContext(task, userMessage, history, configuration);
}
/**
* Closes any active streams for a task.
* @param taskId The task ID
*/
closeStreamsForTask(taskId) {
this.expressServer.getService(Protocol.A2A)?.state.closeStreamsForTask(taskId);
}
}
//# sourceMappingURL=a2a-server.js.map