@artinet/sdk 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +45 -0
- package/LICENSE +21 -0
- package/README.md +618 -0
- package/dist/client/a2a-client.d.ts +99 -0
- package/dist/client/a2a-client.d.ts.map +1 -0
- package/dist/client/a2a-client.js +171 -0
- package/dist/client/a2a-client.js.map +1 -0
- package/dist/client/index.d.ts +3 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/client/index.js +3 -0
- package/dist/client/index.js.map +1 -0
- package/dist/client/interfaces/client.d.ts +19 -0
- package/dist/client/interfaces/client.d.ts.map +1 -0
- package/dist/client/interfaces/client.js +2 -0
- package/dist/client/interfaces/client.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/dist/server/a2a-server.d.ts +144 -0
- package/dist/server/a2a-server.d.ts.map +1 -0
- package/dist/server/a2a-server.js +384 -0
- package/dist/server/a2a-server.js.map +1 -0
- package/dist/server/index.d.ts +9 -0
- package/dist/server/index.d.ts.map +1 -0
- package/dist/server/index.js +9 -0
- package/dist/server/index.js.map +1 -0
- package/dist/server/interfaces/context.d.ts +41 -0
- package/dist/server/interfaces/context.d.ts.map +1 -0
- package/dist/server/interfaces/context.js +2 -0
- package/dist/server/interfaces/context.js.map +1 -0
- package/dist/server/interfaces/params.d.ts +72 -0
- package/dist/server/interfaces/params.d.ts.map +1 -0
- package/dist/server/interfaces/params.js +3 -0
- package/dist/server/interfaces/params.js.map +1 -0
- package/dist/server/interfaces/server.d.ts +34 -0
- package/dist/server/interfaces/server.d.ts.map +1 -0
- package/dist/server/interfaces/server.js +2 -0
- package/dist/server/interfaces/server.js.map +1 -0
- package/dist/server/interfaces/store.d.ts +30 -0
- package/dist/server/interfaces/store.d.ts.map +1 -0
- package/dist/server/interfaces/store.js +2 -0
- package/dist/server/interfaces/store.js.map +1 -0
- package/dist/server/lib/express-server.d.ts +13 -0
- package/dist/server/lib/express-server.d.ts.map +1 -0
- package/dist/server/lib/express-server.js +51 -0
- package/dist/server/lib/express-server.js.map +1 -0
- package/dist/server/lib/json-middleware.d.ts +19 -0
- package/dist/server/lib/json-middleware.d.ts.map +1 -0
- package/dist/server/lib/json-middleware.js +180 -0
- package/dist/server/lib/json-middleware.js.map +1 -0
- package/dist/server/lib/state.d.ts +28 -0
- package/dist/server/lib/state.d.ts.map +1 -0
- package/dist/server/lib/state.js +108 -0
- package/dist/server/lib/state.js.map +1 -0
- package/dist/server/lib/storage/file.d.ts +62 -0
- package/dist/server/lib/storage/file.d.ts.map +1 -0
- package/dist/server/lib/storage/file.js +148 -0
- package/dist/server/lib/storage/file.js.map +1 -0
- package/dist/server/lib/storage/memory.d.ts +21 -0
- package/dist/server/lib/storage/memory.d.ts.map +1 -0
- package/dist/server/lib/storage/memory.js +40 -0
- package/dist/server/lib/storage/memory.js.map +1 -0
- package/dist/transport/index.d.ts +5 -0
- package/dist/transport/index.d.ts.map +1 -0
- package/dist/transport/index.js +5 -0
- package/dist/transport/index.js.map +1 -0
- package/dist/transport/rpc/parser.d.ts +12 -0
- package/dist/transport/rpc/parser.d.ts.map +1 -0
- package/dist/transport/rpc/parser.js +37 -0
- package/dist/transport/rpc/parser.js.map +1 -0
- package/dist/transport/rpc/rpc-client.d.ts +77 -0
- package/dist/transport/rpc/rpc-client.d.ts.map +1 -0
- package/dist/transport/rpc/rpc-client.js +182 -0
- package/dist/transport/rpc/rpc-client.js.map +1 -0
- package/dist/transport/streaming/event-stream.d.ts +22 -0
- package/dist/transport/streaming/event-stream.d.ts.map +1 -0
- package/dist/transport/streaming/event-stream.js +79 -0
- package/dist/transport/streaming/event-stream.js.map +1 -0
- package/dist/transport/streaming/stream.d.ts +42 -0
- package/dist/transport/streaming/stream.d.ts.map +1 -0
- package/dist/transport/streaming/stream.js +128 -0
- package/dist/transport/streaming/stream.js.map +1 -0
- package/dist/types/extended-schema.d.ts +52 -0
- package/dist/types/extended-schema.d.ts.map +1 -0
- package/dist/types/extended-schema.js +7 -0
- package/dist/types/extended-schema.js.map +1 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +2 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/schema.d.ts +729 -0
- package/dist/types/schema.d.ts.map +1 -0
- package/dist/types/schema.js +23 -0
- package/dist/types/schema.js.map +1 -0
- package/dist/utils/common/constants.d.ts +6 -0
- package/dist/utils/common/constants.d.ts.map +1 -0
- package/dist/utils/common/constants.js +28 -0
- package/dist/utils/common/constants.js.map +1 -0
- package/dist/utils/common/errors.d.ts +26 -0
- package/dist/utils/common/errors.d.ts.map +1 -0
- package/dist/utils/common/errors.js +59 -0
- package/dist/utils/common/errors.js.map +1 -0
- package/dist/utils/common/utils.d.ts +30 -0
- package/dist/utils/common/utils.d.ts.map +1 -0
- package/dist/utils/common/utils.js +67 -0
- package/dist/utils/common/utils.js.map +1 -0
- package/dist/utils/index.d.ts +6 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +6 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/logging/log.d.ts +30 -0
- package/dist/utils/logging/log.d.ts.map +1 -0
- package/dist/utils/logging/log.js +58 -0
- package/dist/utils/logging/log.js.map +1 -0
- package/dist/utils/logging/logger.d.ts +16 -0
- package/dist/utils/logging/logger.d.ts.map +1 -0
- package/dist/utils/logging/logger.js +38 -0
- package/dist/utils/logging/logger.js.map +1 -0
- package/package.json +83 -0
|
@@ -0,0 +1,384 @@
|
|
|
1
|
+
import util from "util";
|
|
2
|
+
import { getCurrentTimestamp, validateTaskSendParams, INVALID_PARAMS, TASK_NOT_FOUND, INVALID_REQUEST, errorHandler, CANCEL_UPDATE, FINAL_STATES, WORKING_UPDATE, logDebug, } from "../utils/index.js";
|
|
3
|
+
import { sendSSEEvent, setupSseStream, processTaskStream, } from "../transport/index.js";
|
|
4
|
+
import { defaultCreateJSONRPCServer } from "./lib/json-middleware.js";
|
|
5
|
+
import { createExpressServer } from "./lib/express-server.js";
|
|
6
|
+
import { updateState, loadState } from "./lib/state.js";
|
|
7
|
+
import { InMemoryTaskStore } from "./lib/storage/memory.js";
|
|
8
|
+
/**
|
|
9
|
+
* Implements an A2A protocol compliant server using Express.
|
|
10
|
+
* Handles task creation, streaming, cancellation and more.
|
|
11
|
+
* Uses Jayson for JSON-RPC handling.
|
|
12
|
+
*/
|
|
13
|
+
export class A2AServer {
|
|
14
|
+
/**
|
|
15
|
+
* Returns the base path for the server
|
|
16
|
+
*/
|
|
17
|
+
getBasePath() {
|
|
18
|
+
return this.basePath;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Returns the CORS options for the server
|
|
22
|
+
*/
|
|
23
|
+
getCorsOptions() {
|
|
24
|
+
return this.corsOptions;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Returns the agent card for the server
|
|
28
|
+
*/
|
|
29
|
+
getCard() {
|
|
30
|
+
return this.card;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Returns the task store
|
|
34
|
+
*/
|
|
35
|
+
getTaskStore() {
|
|
36
|
+
return this.taskStore;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Returns the task handler
|
|
40
|
+
*/
|
|
41
|
+
getTaskHandler() {
|
|
42
|
+
return this.taskHandler;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Returns the set of active cancellations
|
|
46
|
+
*/
|
|
47
|
+
getActiveCancellations() {
|
|
48
|
+
return this.activeCancellations;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Returns the map of active streams
|
|
52
|
+
*/
|
|
53
|
+
getActiveStreams() {
|
|
54
|
+
return this.activeStreams;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Returns the port number
|
|
58
|
+
*/
|
|
59
|
+
getPort() {
|
|
60
|
+
return this.port;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Returns the JSON-RPC server
|
|
64
|
+
*/
|
|
65
|
+
getRpcServer() {
|
|
66
|
+
return this.rpcServer;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Returns the server instance
|
|
70
|
+
*/
|
|
71
|
+
getServerInstance() {
|
|
72
|
+
return this.serverInstance;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Returns a task context for the specified task and messages
|
|
76
|
+
*/
|
|
77
|
+
getTaskContext(task, userMessage, history) {
|
|
78
|
+
return this.createTaskContext(task, userMessage, history);
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Returns the default agent card
|
|
82
|
+
*/
|
|
83
|
+
defaultAgentCard() {
|
|
84
|
+
return A2AServer.defaultAgentCard();
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Creates a new A2AServer.
|
|
88
|
+
* @param handler The task handler function that will process tasks
|
|
89
|
+
* @param options Options for configuring the server
|
|
90
|
+
*/
|
|
91
|
+
constructor(params) {
|
|
92
|
+
// Track active cancellations
|
|
93
|
+
this.activeCancellations = new Set();
|
|
94
|
+
this.activeStreams = new Map();
|
|
95
|
+
// Store the handler
|
|
96
|
+
this.taskHandler = params.handler;
|
|
97
|
+
// Set up store
|
|
98
|
+
this.taskStore = params.taskStore ?? new InMemoryTaskStore();
|
|
99
|
+
// Configure CORS
|
|
100
|
+
this.corsOptions = params.corsOptions ?? {
|
|
101
|
+
origin: "*",
|
|
102
|
+
methods: ["GET", "POST"],
|
|
103
|
+
allowedHeaders: ["Content-Type"],
|
|
104
|
+
};
|
|
105
|
+
// Set port
|
|
106
|
+
this.port = params.port ?? 41241;
|
|
107
|
+
let basePath = params.basePath ?? "/";
|
|
108
|
+
if (basePath !== "/") {
|
|
109
|
+
basePath = `/${basePath.replace(/^\/|\/$/g, "")}/`;
|
|
110
|
+
}
|
|
111
|
+
this.basePath = basePath;
|
|
112
|
+
// Set up default agent card if not provided
|
|
113
|
+
this.card = params.card ?? A2AServer.defaultAgentCard();
|
|
114
|
+
// Initialize the Jayson server
|
|
115
|
+
this.rpcServer = params.createJSONRPCServer
|
|
116
|
+
? params.createJSONRPCServer({
|
|
117
|
+
taskStore: this.taskStore,
|
|
118
|
+
card: this.card,
|
|
119
|
+
taskHandler: this.taskHandler,
|
|
120
|
+
activeCancellations: this.activeCancellations,
|
|
121
|
+
createTaskContext: this.createTaskContext.bind(this),
|
|
122
|
+
closeStreamsForTask: this.closeStreamsForTask.bind(this),
|
|
123
|
+
})
|
|
124
|
+
: defaultCreateJSONRPCServer({
|
|
125
|
+
taskStore: this.taskStore,
|
|
126
|
+
card: this.card,
|
|
127
|
+
taskHandler: this.taskHandler,
|
|
128
|
+
activeCancellations: this.activeCancellations,
|
|
129
|
+
createTaskContext: this.createTaskContext.bind(this),
|
|
130
|
+
closeStreamsForTask: this.closeStreamsForTask.bind(this),
|
|
131
|
+
});
|
|
132
|
+
logDebug("A2AServer", "Server initialized", {
|
|
133
|
+
basePath: this.basePath,
|
|
134
|
+
port: this.port,
|
|
135
|
+
corsEnabled: !!this.corsOptions,
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Starts the Express server listening on the specified port.
|
|
140
|
+
* @returns The running Express application instance.
|
|
141
|
+
*/
|
|
142
|
+
start() {
|
|
143
|
+
if (this.serverInstance) {
|
|
144
|
+
throw new Error("Server already started");
|
|
145
|
+
}
|
|
146
|
+
const { app, server } = createExpressServer({
|
|
147
|
+
card: this.card,
|
|
148
|
+
corsOptions: this.corsOptions,
|
|
149
|
+
basePath: this.basePath,
|
|
150
|
+
port: this.port,
|
|
151
|
+
rpcServer: this.rpcServer,
|
|
152
|
+
errorHandler: errorHandler,
|
|
153
|
+
onTaskSendSubscribe: this.handleTaskSendSubscribe.bind(this),
|
|
154
|
+
onTaskResubscribe: this.handleTaskResubscribe.bind(this),
|
|
155
|
+
});
|
|
156
|
+
this.serverInstance = server;
|
|
157
|
+
return app;
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Stops the server and closes all connections.
|
|
161
|
+
* @returns A promise that resolves when the server is stopped.
|
|
162
|
+
*/
|
|
163
|
+
async stop() {
|
|
164
|
+
if (!this.serverInstance) {
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
// Close all active streams first
|
|
168
|
+
this.activeStreams.forEach((streams, taskId) => {
|
|
169
|
+
if (streams.length > 0) {
|
|
170
|
+
logDebug("A2AServer", "Closing streams for task during stop", {
|
|
171
|
+
taskId,
|
|
172
|
+
});
|
|
173
|
+
this.closeStreamsForTask(taskId);
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
this.activeStreams.clear();
|
|
177
|
+
const closeServer = util
|
|
178
|
+
.promisify(this.serverInstance.close)
|
|
179
|
+
.bind(this.serverInstance);
|
|
180
|
+
try {
|
|
181
|
+
await closeServer();
|
|
182
|
+
logDebug("A2AServer", "Server stopped successfully.");
|
|
183
|
+
this.serverInstance = undefined;
|
|
184
|
+
}
|
|
185
|
+
catch (err) {
|
|
186
|
+
logDebug("A2AServer", "Error stopping server:", err);
|
|
187
|
+
this.serverInstance = undefined;
|
|
188
|
+
throw err;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Handles task cancellation
|
|
193
|
+
* @param data Task and history data
|
|
194
|
+
* @param res Response object
|
|
195
|
+
*/
|
|
196
|
+
async onCancel(data, res) {
|
|
197
|
+
const currentData = await updateState(this.taskStore, data, CANCEL_UPDATE);
|
|
198
|
+
// Send the canceled status
|
|
199
|
+
sendSSEEvent(res, {
|
|
200
|
+
id: currentData.task.id,
|
|
201
|
+
status: currentData.task.status,
|
|
202
|
+
final: true,
|
|
203
|
+
});
|
|
204
|
+
this.closeStreamsForTask(currentData.task.id);
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Handles cleanup when a task stream ends
|
|
208
|
+
* @param taskId The task ID
|
|
209
|
+
* @param res Response object
|
|
210
|
+
*/
|
|
211
|
+
async onEnd(taskId, res) {
|
|
212
|
+
this.activeCancellations.delete(taskId);
|
|
213
|
+
this.removeStreamForTask(taskId, res);
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* Handles the tasks/sendSubscribe method.
|
|
217
|
+
* @param req The SendTaskRequest object
|
|
218
|
+
* @param res The Express Response object
|
|
219
|
+
*/
|
|
220
|
+
async handleTaskSendSubscribe(req, res) {
|
|
221
|
+
validateTaskSendParams(req.params);
|
|
222
|
+
const { id: taskId, message, sessionId, metadata } = req.params;
|
|
223
|
+
// Set up SSE stream with initial status
|
|
224
|
+
setupSseStream(res, taskId, {
|
|
225
|
+
id: taskId,
|
|
226
|
+
status: {
|
|
227
|
+
state: "submitted",
|
|
228
|
+
timestamp: getCurrentTimestamp(),
|
|
229
|
+
message: null,
|
|
230
|
+
},
|
|
231
|
+
}, this.addStreamForTask.bind(this));
|
|
232
|
+
// Load or create task
|
|
233
|
+
let currentData = await loadState(this.taskStore, taskId, message, sessionId, metadata);
|
|
234
|
+
// Create task context
|
|
235
|
+
const context = this.createTaskContext(currentData.task, message, currentData.history);
|
|
236
|
+
currentData = await updateState(this.taskStore, currentData, WORKING_UPDATE);
|
|
237
|
+
// Send the working status
|
|
238
|
+
sendSSEEvent(res, {
|
|
239
|
+
id: taskId,
|
|
240
|
+
status: currentData.task.status,
|
|
241
|
+
final: false,
|
|
242
|
+
});
|
|
243
|
+
// Process the task using the shared method
|
|
244
|
+
await processTaskStream(this.taskStore, this.taskHandler, res, taskId, context, currentData, this.onCancel.bind(this), this.onEnd.bind(this));
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* Handles the tasks/resubscribe method.
|
|
248
|
+
* @param req The TaskResubscriptionRequest object
|
|
249
|
+
* @param res The Express Response object
|
|
250
|
+
*/
|
|
251
|
+
async handleTaskResubscribe(req, res) {
|
|
252
|
+
const { id: taskId } = req.params;
|
|
253
|
+
if (!taskId) {
|
|
254
|
+
console.error("Task ID is required", req);
|
|
255
|
+
throw INVALID_PARAMS();
|
|
256
|
+
}
|
|
257
|
+
// Try to load the task
|
|
258
|
+
const data = await this.taskStore.load(taskId);
|
|
259
|
+
if (!data) {
|
|
260
|
+
throw TASK_NOT_FOUND();
|
|
261
|
+
}
|
|
262
|
+
// Set up SSE stream with current task status
|
|
263
|
+
setupSseStream(res, taskId, {
|
|
264
|
+
id: taskId,
|
|
265
|
+
status: data.task.status,
|
|
266
|
+
final: false,
|
|
267
|
+
}, this.addStreamForTask.bind(this));
|
|
268
|
+
// Check if task is in final state
|
|
269
|
+
if (FINAL_STATES.includes(data.task.status.state)) {
|
|
270
|
+
// If the task is already complete, send all artifacts and close
|
|
271
|
+
if (data.task.artifacts && data.task.artifacts.length > 0) {
|
|
272
|
+
for (const artifact of data.task.artifacts) {
|
|
273
|
+
sendSSEEvent(res, {
|
|
274
|
+
id: taskId,
|
|
275
|
+
artifact,
|
|
276
|
+
final: true,
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
// Remove from tracking and close
|
|
281
|
+
this.removeStreamForTask(taskId, res);
|
|
282
|
+
res.write("event: close\ndata: {}\n\n");
|
|
283
|
+
res.end();
|
|
284
|
+
return;
|
|
285
|
+
}
|
|
286
|
+
// For non-final states, create context and continue processing
|
|
287
|
+
// We need to use the last user message as the current message
|
|
288
|
+
const lastUserMessage = data.history
|
|
289
|
+
.filter((msg) => msg.role === "user")
|
|
290
|
+
.pop();
|
|
291
|
+
if (!lastUserMessage) {
|
|
292
|
+
throw INVALID_REQUEST();
|
|
293
|
+
}
|
|
294
|
+
const context = this.createTaskContext(data.task, lastUserMessage, data.history);
|
|
295
|
+
// Continue processing the task using the shared method
|
|
296
|
+
await processTaskStream(this.taskStore, this.taskHandler, res, taskId, context, data, this.onCancel.bind(this), this.onEnd.bind(this));
|
|
297
|
+
}
|
|
298
|
+
/**
|
|
299
|
+
* Adds a response stream to the tracking map for a task.
|
|
300
|
+
* @param taskId The task ID
|
|
301
|
+
* @param res The response stream
|
|
302
|
+
*/
|
|
303
|
+
addStreamForTask(taskId, res) {
|
|
304
|
+
if (!this.activeStreams.has(taskId)) {
|
|
305
|
+
this.activeStreams.set(taskId, []);
|
|
306
|
+
}
|
|
307
|
+
logDebug("A2AServer", "Adding stream for task", {
|
|
308
|
+
taskId,
|
|
309
|
+
activeStreams: this.activeStreams,
|
|
310
|
+
});
|
|
311
|
+
this.activeStreams.get(taskId)?.push(res);
|
|
312
|
+
}
|
|
313
|
+
/**
|
|
314
|
+
* Removes a response stream from the tracking map for a task.
|
|
315
|
+
* @param taskId The task ID
|
|
316
|
+
* @param res The response stream
|
|
317
|
+
*/
|
|
318
|
+
removeStreamForTask(taskId, res) {
|
|
319
|
+
const streams = this.activeStreams.get(taskId);
|
|
320
|
+
if (streams) {
|
|
321
|
+
const index = streams.indexOf(res);
|
|
322
|
+
if (index !== -1) {
|
|
323
|
+
streams.splice(index, 1);
|
|
324
|
+
if (streams.length === 0) {
|
|
325
|
+
logDebug("A2AServer", "Removing stream for task", {
|
|
326
|
+
taskId,
|
|
327
|
+
activeStreams: this.activeStreams,
|
|
328
|
+
});
|
|
329
|
+
this.activeStreams.delete(taskId);
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
/**
|
|
335
|
+
* Initializes the default agent card
|
|
336
|
+
*/
|
|
337
|
+
static defaultAgentCard() {
|
|
338
|
+
return {
|
|
339
|
+
name: "A2A Server",
|
|
340
|
+
description: "A general-purpose A2A protocol server",
|
|
341
|
+
version: "0.1.0",
|
|
342
|
+
url: "http://localhost",
|
|
343
|
+
capabilities: {
|
|
344
|
+
streaming: true,
|
|
345
|
+
pushNotifications: false,
|
|
346
|
+
stateTransitionHistory: true,
|
|
347
|
+
},
|
|
348
|
+
skills: [],
|
|
349
|
+
};
|
|
350
|
+
}
|
|
351
|
+
/**
|
|
352
|
+
* Creates a TaskContext object for a task handler.
|
|
353
|
+
* @param task The task
|
|
354
|
+
* @param userMessage The user message
|
|
355
|
+
* @param history The message history
|
|
356
|
+
* @returns A TaskContext object
|
|
357
|
+
*/
|
|
358
|
+
createTaskContext(task, userMessage, history) {
|
|
359
|
+
return {
|
|
360
|
+
task,
|
|
361
|
+
userMessage,
|
|
362
|
+
history,
|
|
363
|
+
isCancelled: () => this.activeCancellations.has(task.id),
|
|
364
|
+
};
|
|
365
|
+
}
|
|
366
|
+
/**
|
|
367
|
+
* Closes any active streams for a task.
|
|
368
|
+
* @param taskId The task ID
|
|
369
|
+
*/
|
|
370
|
+
closeStreamsForTask(taskId) {
|
|
371
|
+
const streams = this.activeStreams.get(taskId);
|
|
372
|
+
if (streams) {
|
|
373
|
+
// Send close event to all streams
|
|
374
|
+
for (const stream of streams) {
|
|
375
|
+
if (stream.writable) {
|
|
376
|
+
stream.write("event: close\ndata: {}\n\n");
|
|
377
|
+
stream.end();
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
this.activeStreams.delete(taskId);
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
//# sourceMappingURL=a2a-server.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"a2a-server.js","sourceRoot":"","sources":["../../src/server/a2a-server.ts"],"names":[],"mappings":"AAGA,OAAO,IAAI,MAAM,MAAM,CAAC;AASxB,OAAO,EACL,mBAAmB,EACnB,sBAAsB,EACtB,cAAc,EACd,cAAc,EACd,eAAe,EACf,YAAY,EACZ,aAAa,EACb,YAAY,EACZ,cAAc,EACd,QAAQ,GACT,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EACL,YAAY,EACZ,cAAc,EACd,iBAAiB,GAClB,MAAM,uBAAuB,CAAC;AAO/B,OAAO,EAAE,0BAA0B,EAAE,MAAM,0BAA0B,CAAC;AACtE,OAAO,EAAE,mBAAmB,EAAE,MAAM,yBAAyB,CAAC;AAC9D,O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AAE,GAAG,EAAE,CAAC,IAAI,CAAC,mBAAmB,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC;SACzD,CAAC;IACJ,CAAC;IAED;;;OAGG;IACI,mBAAmB,CAAC,MAAc;QACvC,MAAM,OAAO,GAAG,IAAI,CAAC,aAAa,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;QAC/C,IAAI,OAAO,EAAE,CAAC;YACZ,kCAAkC;YAClC,KAAK,MAAM,MAAM,IAAI,OAAO,EAAE,CAAC;gBAC7B,IAAI,MAAM,CAAC,QAAQ,EAAE,CAAC;oBACpB,MAAM,CAAC,KAAK,CAAC,4BAA4B,CAAC,CAAC;oBAC3C,MAAM,CAAC,GAAG,EAAE,CAAC;gBACf,CAAC;YACH,CAAC;YACD,IAAI,CAAC,aAAa,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;QACpC,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export * from "./a2a-server.js";
|
|
2
|
+
export * from "./interfaces/params.js";
|
|
3
|
+
export * from "./interfaces/context.js";
|
|
4
|
+
export * from "./interfaces/store.js";
|
|
5
|
+
export * from "./lib/json-middleware.js";
|
|
6
|
+
export * from "./lib/express-server.js";
|
|
7
|
+
export * from "./lib/storage/file.js";
|
|
8
|
+
export * from "./lib/storage/memory.js";
|
|
9
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/server/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAC;AAChC,cAAc,wBAAwB,CAAC;AACvC,cAAc,yBAAyB,CAAC;AACxC,cAAc,uBAAuB,CAAC;AACtC,cAAc,0BAA0B,CAAC;AACzC,cAAc,yBAAyB,CAAC;AACxC,cAAc,uBAAuB,CAAC;AACtC,cAAc,yBAAyB,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export * from "./a2a-server.js";
|
|
2
|
+
export * from "./interfaces/params.js";
|
|
3
|
+
export * from "./interfaces/context.js";
|
|
4
|
+
export * from "./interfaces/store.js";
|
|
5
|
+
export * from "./lib/json-middleware.js";
|
|
6
|
+
export * from "./lib/express-server.js";
|
|
7
|
+
export * from "./lib/storage/file.js";
|
|
8
|
+
export * from "./lib/storage/memory.js";
|
|
9
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/server/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAC;AAChC,cAAc,wBAAwB,CAAC;AACvC,cAAc,yBAAyB,CAAC;AACxC,cAAc,uBAAuB,CAAC;AACtC,cAAc,0BAA0B,CAAC;AACzC,cAAc,yBAAyB,CAAC;AACxC,cAAc,uBAAuB,CAAC;AACtC,cAAc,yBAAyB,CAAC"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { Task, Message, TaskYieldUpdate } from "../../types/extended-schema.js";
|
|
2
|
+
/**
|
|
3
|
+
* Context object provided to the TaskHandler.
|
|
4
|
+
* Contains the information needed for the handler to process the task.
|
|
5
|
+
*/
|
|
6
|
+
export interface TaskContext {
|
|
7
|
+
/**
|
|
8
|
+
* The current state of the task when the handler is invoked or resumed.
|
|
9
|
+
* This is a snapshot - the latest state may need to be reloaded during async operations.
|
|
10
|
+
*/
|
|
11
|
+
task: Task;
|
|
12
|
+
/**
|
|
13
|
+
* The specific user message that triggered this handler invocation or resumption.
|
|
14
|
+
*/
|
|
15
|
+
userMessage: Message;
|
|
16
|
+
/**
|
|
17
|
+
* Function to check if cancellation has been requested for this task.
|
|
18
|
+
* Handlers should check this periodically during long-running operations.
|
|
19
|
+
* @returns True if cancellation has been requested, false otherwise.
|
|
20
|
+
*/
|
|
21
|
+
isCancelled(): boolean;
|
|
22
|
+
/**
|
|
23
|
+
* The message history associated with the task up to the point the handler is invoked.
|
|
24
|
+
*/
|
|
25
|
+
history: Message[];
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Defines the signature for a task handler function.
|
|
29
|
+
*
|
|
30
|
+
* Handlers are implemented as async generators. They receive context about the
|
|
31
|
+
* task and the triggering message. They perform work and yield status
|
|
32
|
+
* or artifact updates (TaskYieldUpdate). The server consumes these yields,
|
|
33
|
+
* updates the task state in the store, and streams events if applicable.
|
|
34
|
+
*
|
|
35
|
+
* @param context The TaskContext object containing task details and state.
|
|
36
|
+
* @yields Updates to the task's status or artifacts.
|
|
37
|
+
* @returns Optionally returns the final complete Task object (needed for non-streaming 'tasks/send').
|
|
38
|
+
* If void is returned, the server uses the last known state after processing all yields.
|
|
39
|
+
*/
|
|
40
|
+
export type TaskHandler = (context: TaskContext) => AsyncGenerator<TaskYieldUpdate, Task | void, unknown>;
|
|
41
|
+
//# sourceMappingURL=context.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../../../src/server/interfaces/context.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,IAAI,EACJ,OAAO,EACP,eAAe,EAChB,MAAM,gCAAgC,CAAC;AAExC;;;GAGG;AACH,MAAM,WAAW,WAAW;IAC1B;;;OAGG;IACH,IAAI,EAAE,IAAI,CAAC;IAEX;;OAEG;IACH,WAAW,EAAE,OAAO,CAAC;IAErB;;;;OAIG;IACH,WAAW,IAAI,OAAO,CAAC;IAEvB;;OAEG;IACH,OAAO,EAAE,OAAO,EAAE,CAAC;CACpB;AAED;;;;;;;;;;;;GAYG;AACH,MAAM,MAAM,WAAW,GAAG,CACxB,OAAO,EAAE,WAAW,KACjB,cAAc,CAAC,eAAe,EAAE,IAAI,GAAG,IAAI,EAAE,OAAO,CAAC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.js","sourceRoot":"","sources":["../../../src/server/interfaces/context.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { CorsOptions } from "cors";
|
|
2
|
+
import { AgentCard, Message, SendTaskStreamingRequest, Task, TaskResubscriptionRequest } from "../../types/extended-schema.js";
|
|
3
|
+
import { TaskStore } from "./store.js";
|
|
4
|
+
import { TaskHandler, TaskContext } from "./context.js";
|
|
5
|
+
import jayson from "jayson";
|
|
6
|
+
import { ErrorHandler } from "../../utils/common/errors.js";
|
|
7
|
+
import { Response } from "express";
|
|
8
|
+
export type JSONRPCServerType = jayson.Server;
|
|
9
|
+
export declare const JSONRPCServer: jayson.ServerConstructor;
|
|
10
|
+
type JSONRPCError = jayson.JSONRPCError;
|
|
11
|
+
export type JSONRPCCallback = (error: JSONRPCError | null, result?: any) => void;
|
|
12
|
+
/**
|
|
13
|
+
* Type for the ActiveCancellations set
|
|
14
|
+
* This can be used by consumers to implement their own server solutions
|
|
15
|
+
*/
|
|
16
|
+
export type ActiveCancellations = Set<string>;
|
|
17
|
+
/**
|
|
18
|
+
* Type for the CloseStreamsForTask function
|
|
19
|
+
* This can be used by consumers to implement their own server solutions
|
|
20
|
+
*/
|
|
21
|
+
export type CloseStreamsForTask = (taskId: string) => void;
|
|
22
|
+
/**
|
|
23
|
+
* Type for the CreateTaskContext function
|
|
24
|
+
* This can be used by consumers to implement their own server solutions
|
|
25
|
+
*/
|
|
26
|
+
export type CreateTaskContext = (task: Task, message: Message, history: Message[]) => TaskContext;
|
|
27
|
+
/**
|
|
28
|
+
* Type for the createJSONRPCServer function
|
|
29
|
+
* This can be used by consumers to implement their own server solutions
|
|
30
|
+
*/
|
|
31
|
+
export type CreateJSONRPCServer = (params: CreateJSONRPCServerParams) => JSONRPCServerType;
|
|
32
|
+
/**
|
|
33
|
+
* Interface for createJSONRPCServer parameters
|
|
34
|
+
* This can be used by consumers to implement their own server solutions
|
|
35
|
+
*/
|
|
36
|
+
export interface CreateJSONRPCServerParams {
|
|
37
|
+
taskStore: TaskStore;
|
|
38
|
+
card: AgentCard;
|
|
39
|
+
taskHandler: TaskHandler;
|
|
40
|
+
activeCancellations: ActiveCancellations;
|
|
41
|
+
createTaskContext: CreateTaskContext;
|
|
42
|
+
closeStreamsForTask: CloseStreamsForTask;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Parameters for creating an A2AServer instance.
|
|
46
|
+
*/
|
|
47
|
+
export type A2AServerParams = {
|
|
48
|
+
handler: TaskHandler;
|
|
49
|
+
corsOptions?: CorsOptions;
|
|
50
|
+
port?: number;
|
|
51
|
+
basePath?: string;
|
|
52
|
+
taskStore?: TaskStore;
|
|
53
|
+
card?: AgentCard;
|
|
54
|
+
createJSONRPCServer?: JSONRPCServerFactory;
|
|
55
|
+
};
|
|
56
|
+
/**
|
|
57
|
+
* Parameters for creating an Express server.
|
|
58
|
+
*/
|
|
59
|
+
export type CreateExpressServerParams = Omit<A2AServerParams, "createJSONRPCServer" | "taskStore" | "handler"> & Required<Pick<A2AServerParams, "card" | "basePath" | "port" | "corsOptions">> & {
|
|
60
|
+
rpcServer: JSONRPCServerType;
|
|
61
|
+
errorHandler: ErrorHandler;
|
|
62
|
+
onTaskSendSubscribe: (req: SendTaskStreamingRequest, res: Response) => Promise<void>;
|
|
63
|
+
onTaskResubscribe: (req: TaskResubscriptionRequest, res: Response) => Promise<void>;
|
|
64
|
+
};
|
|
65
|
+
/**
|
|
66
|
+
* Function type for creating a JSON-RPC server
|
|
67
|
+
* @param params Parameters required to initialize the server
|
|
68
|
+
* @returns A configured JSON-RPC server instance
|
|
69
|
+
*/
|
|
70
|
+
export type JSONRPCServerFactory = (params: CreateJSONRPCServerParams) => JSONRPCServerType;
|
|
71
|
+
export {};
|
|
72
|
+
//# sourceMappingURL=params.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"params.d.ts","sourceRoot":"","sources":["../../../src/server/interfaces/params.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,MAAM,CAAC;AACnC,OAAO,EACL,SA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AC1C;AAED;;GAEG;AACH,MAAM,MAAM,eAAe,GAAG;IAC5B,OAAO,EAAE,WAAW,CAAC;IACrB,WAAW,CAAC,EAAE,WAAW,CAAC;IAC1B,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,SAAS,CAAC,EAAE,SAAS,CAAC;IACtB,IAAI,CAAC,EAAE,SAAS,CAAC;IACjB,mBAAmB,CAAC,EAAE,oBAAoB,CAAC;CAC5C,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,yBAAyB,GAAG,IAAI,CAC1C,eAAe,EACf,qBAAqB,GAAG,WAAW,GAAG,SAAS,CAChD,GACC,QAAQ,CACN,IAAI,CAAC,eAAe,EAAE,MAAM,GAAG,UAAU,GAAG,MAAM,GAAG,aAAa,CAAC,CACpE,GAAG;IACF,SAAS,EAAE,iBAAiB,CAAC;IAC7B,YAAY,EAAE,YAAY,CAAC;IAC3B,mBAAmB,EAAE,CACnB,GAAG,EAAE,wBAAwB,EAC7B,GAAG,EAAE,QAAQ,KACV,OAAO,CAAC,IAAI,CAAC,CAAC;IACnB,iBAAiB,EAAE,CACjB,GAAG,EAAE,yBAAyB,EAC9B,GAAG,EAAE,QAAQ,KACV,OAAO,CAAC,IAAI,CAAC,CAAC;CACpB,CAAC;AAEJ;;;;GAIG;AACH,MAAM,MAAM,oBAAoB,GAAG,CACjC,MAAM,EAAE,yBAAyB,KAC9B,iBAAiB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"params.js","sourceRoot":"","sources":["../../../src/server/interfaces/params.ts"],"names":[],"mappings":"AAUA,OAAO,MAAM,MAAM,QAAQ,CAAC;AAK5B,MAAM,CAAC,MAAM,aAAa,GAAG,MAAM,CAAC,MAAM,CAAC"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import express, { Response } from "express";
|
|
2
|
+
import http from "http";
|
|
3
|
+
import { CorsOptions } from "cors";
|
|
4
|
+
import { AgentCard, Message, Task, SendTaskStreamingRequest, TaskResubscriptionRequest } from "../../types/index.js";
|
|
5
|
+
import { TaskStore } from "./store.js";
|
|
6
|
+
import { TaskHandler } from "./context.js";
|
|
7
|
+
import { JSONRPCServerType } from "./params.js";
|
|
8
|
+
import { TaskAndHistory } from "./store.js";
|
|
9
|
+
import { TaskContext } from "./context.js";
|
|
10
|
+
export interface Server {
|
|
11
|
+
start(): express.Express;
|
|
12
|
+
stop(): Promise<void>;
|
|
13
|
+
getBasePath(): string;
|
|
14
|
+
getCorsOptions(): CorsOptions;
|
|
15
|
+
getCard(): AgentCard;
|
|
16
|
+
getTaskStore(): TaskStore;
|
|
17
|
+
getTaskHandler(): TaskHandler;
|
|
18
|
+
getActiveCancellations(): Set<string>;
|
|
19
|
+
getActiveStreams(): Map<string, Response[]>;
|
|
20
|
+
getPort(): number;
|
|
21
|
+
getRpcServer(): JSONRPCServerType;
|
|
22
|
+
getServerInstance(): http.Server | undefined;
|
|
23
|
+
getTaskContext(task: Task, userMessage: Message, history: Message[]): TaskContext;
|
|
24
|
+
onCancel(data: TaskAndHistory, res: Response): Promise<void>;
|
|
25
|
+
onEnd(taskId: string, res: Response): Promise<void>;
|
|
26
|
+
addStreamForTask(taskId: string, res: Response): void;
|
|
27
|
+
removeStreamForTask(taskId: string, res: Response): void;
|
|
28
|
+
closeStreamsForTask(taskId: string): void;
|
|
29
|
+
handleTaskSendSubscribe(req: SendTaskStreamingRequest, res: Response): Promise<void>;
|
|
30
|
+
handleTaskResubscribe(req: TaskResubscriptionRequest, res: Response): Promise<void>;
|
|
31
|
+
defaultAgentCard(): AgentCard;
|
|
32
|
+
createTaskContext(task: Task, userMessage: Message, history: Message[]): TaskContext;
|
|
33
|
+
}
|
|
34
|
+
//# sourceMappingURL=server.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../../../src/server/interfaces/server.ts"],"names":[],"mappings":"AAAA,OAAO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MAAM,EAAE,GAAG,EAAE,QAAQ,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IACpD,gBAAgB,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,EAAE,QAAQ,GAAG,IAAI,CAAC;IACtD,mBAAmB,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,EAAE,QAAQ,GAAG,IAAI,CAAC;IACzD,mBAAmB,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;IAC1C,uBAAuB,CACrB,GAAG,EAAE,wBAAwB,EAC7B,GAAG,EAAE,QAAQ,GACZ,OAAO,CAAC,IAAI,CAAC,CAAC;IACjB,qBAAqB,CACnB,GAAG,EAAE,yBAAyB,EAC9B,GAAG,EAAE,QAAQ,GACZ,OAAO,CAAC,IAAI,CAAC,CAAC;IACjB,gBAAgB,IAAI,SAAS,CAAC;IAC9B,iBAAiB,CACf,IAAI,EAAE,IAAI,EACV,WAAW,EAAE,OAAO,EACpB,OAAO,EAAE,OAAO,EAAE,GACjB,WAAW,CAAC;CAChB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server.js","sourceRoot":"","sources":["../../../src/server/interfaces/server.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { Task, Message } from "../../types/extended-schema.js";
|
|
2
|
+
/**
|
|
3
|
+
* Represents a task and its associated message history.
|
|
4
|
+
*/
|
|
5
|
+
export interface TaskAndHistory {
|
|
6
|
+
/** The task object */
|
|
7
|
+
task: Task;
|
|
8
|
+
/** The complete message history associated with the task */
|
|
9
|
+
history: Message[];
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Interface for task storage providers.
|
|
13
|
+
* Abstracts the storage mechanism for tasks and their message history.
|
|
14
|
+
*/
|
|
15
|
+
export interface TaskStore {
|
|
16
|
+
/**
|
|
17
|
+
* Saves a task and its associated message history.
|
|
18
|
+
* Overwrites existing data if the task ID exists.
|
|
19
|
+
* @param data An object containing the task and its history.
|
|
20
|
+
* @returns A promise resolving when the save operation is complete.
|
|
21
|
+
*/
|
|
22
|
+
save(data: TaskAndHistory): Promise<void>;
|
|
23
|
+
/**
|
|
24
|
+
* Loads a task and its history by task ID.
|
|
25
|
+
* @param taskId The ID of the task to load.
|
|
26
|
+
* @returns A promise resolving to an object containing the Task and its history, or null if not found.
|
|
27
|
+
*/
|
|
28
|
+
load(taskId: string): Promise<TaskAndHistory | null>;
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=store.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"store.d.ts","sourceRoot":"","sources":["../../../src/server/interfaces/store.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,gCAAgC,CAAC;AAEpE;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B,sBAAsB;IACtB,IAAI,EAAE,IAAI,CAAC;IAEX,4DAA4D;IAC5D,OAAO,EAAE,OAAO,EAAE,CAAC;CACpB;AAED;;;GAGG;AACH,MAAM,WAAW,SAAS;IACxB;;;;;OAKG;IACH,IAAI,CAAC,IAAI,EAAE,cAAc,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAE1C;;;;OAIG;IACH,IAAI,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,cAAc,GAAG,IAAI,CAAC,CAAC;CACtD"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"store.js","sourceRoot":"","sources":["../../../src/server/interfaces/store.ts"],"names":[],"mappings":""}
|