@a2a-js/sdk 0.2.1
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/LICENSE +202 -0
- package/README.md +422 -0
- package/build/src/a2a_response.d.ts +5 -0
- package/build/src/a2a_response.js +2 -0
- package/build/src/client/client.d.ts +118 -0
- package/build/src/client/client.js +409 -0
- package/build/src/index.d.ts +21 -0
- package/build/src/index.js +18 -0
- package/build/src/samples/agents/movie-agent/genkit.d.ts +2 -0
- package/build/src/samples/agents/movie-agent/genkit.js +11 -0
- package/build/src/samples/agents/movie-agent/index.d.ts +1 -0
- package/build/src/samples/agents/movie-agent/index.js +252 -0
- package/build/src/samples/agents/movie-agent/tmdb.d.ts +7 -0
- package/build/src/samples/agents/movie-agent/tmdb.js +32 -0
- package/build/src/samples/agents/movie-agent/tools.d.ts +15 -0
- package/build/src/samples/agents/movie-agent/tools.js +74 -0
- package/build/src/samples/cli.d.ts +2 -0
- package/build/src/samples/cli.js +289 -0
- package/build/src/server/a2a_express_app.d.ts +14 -0
- package/build/src/server/a2a_express_app.js +98 -0
- package/build/src/server/agent_execution/agent_executor.d.ts +18 -0
- package/build/src/server/agent_execution/agent_executor.js +2 -0
- package/build/src/server/agent_execution/request_context.d.ts +9 -0
- package/build/src/server/agent_execution/request_context.js +15 -0
- package/build/src/server/error.d.ts +23 -0
- package/build/src/server/error.js +57 -0
- package/build/src/server/events/execution_event_bus.d.ts +16 -0
- package/build/src/server/events/execution_event_bus.js +13 -0
- package/build/src/server/events/execution_event_bus_manager.d.ts +27 -0
- package/build/src/server/events/execution_event_bus_manager.js +36 -0
- package/build/src/server/events/execution_event_queue.d.ts +24 -0
- package/build/src/server/events/execution_event_queue.js +63 -0
- package/build/src/server/request_handler/a2a_request_handler.d.ts +11 -0
- package/build/src/server/request_handler/a2a_request_handler.js +2 -0
- package/build/src/server/request_handler/default_request_handler.d.ts +22 -0
- package/build/src/server/request_handler/default_request_handler.js +296 -0
- package/build/src/server/result_manager.d.ts +29 -0
- package/build/src/server/result_manager.js +149 -0
- package/build/src/server/store.d.ts +25 -0
- package/build/src/server/store.js +17 -0
- package/build/src/server/transports/jsonrpc_transport_handler.d.ts +15 -0
- package/build/src/server/transports/jsonrpc_transport_handler.js +114 -0
- package/build/src/server/utils.d.ts +22 -0
- package/build/src/server/utils.js +34 -0
- package/build/src/types-old.d.ts +832 -0
- package/build/src/types-old.js +20 -0
- package/build/src/types.d.ts +2046 -0
- package/build/src/types.js +8 -0
- package/package.json +52 -0
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Express } from 'express';
|
|
2
|
+
import { A2ARequestHandler } from "./request_handler/a2a_request_handler.js";
|
|
3
|
+
export declare class A2AExpressApp {
|
|
4
|
+
private requestHandler;
|
|
5
|
+
private jsonRpcTransportHandler;
|
|
6
|
+
constructor(requestHandler: A2ARequestHandler);
|
|
7
|
+
/**
|
|
8
|
+
* Adds A2A routes to an existing Express app.
|
|
9
|
+
* @param app Optional existing Express app.
|
|
10
|
+
* @param baseUrl The base URL for A2A endpoints (e.g., "/a2a/api").
|
|
11
|
+
* @returns The Express app with A2A routes.
|
|
12
|
+
*/
|
|
13
|
+
setupRoutes(app: Express, baseUrl?: string): Express;
|
|
14
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import express from 'express';
|
|
2
|
+
import { A2AError } from "./error.js";
|
|
3
|
+
import { JsonRpcTransportHandler } from "./transports/jsonrpc_transport_handler.js";
|
|
4
|
+
export class A2AExpressApp {
|
|
5
|
+
requestHandler; // Kept for getAgentCard
|
|
6
|
+
jsonRpcTransportHandler;
|
|
7
|
+
constructor(requestHandler) {
|
|
8
|
+
this.requestHandler = requestHandler; // DefaultRequestHandler instance
|
|
9
|
+
this.jsonRpcTransportHandler = new JsonRpcTransportHandler(requestHandler);
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Adds A2A routes to an existing Express app.
|
|
13
|
+
* @param app Optional existing Express app.
|
|
14
|
+
* @param baseUrl The base URL for A2A endpoints (e.g., "/a2a/api").
|
|
15
|
+
* @returns The Express app with A2A routes.
|
|
16
|
+
*/
|
|
17
|
+
setupRoutes(app, baseUrl = '') {
|
|
18
|
+
app.use(express.json());
|
|
19
|
+
app.get(`${baseUrl}/.well-known/agent.json`, async (req, res) => {
|
|
20
|
+
try {
|
|
21
|
+
// getAgentCard is on A2ARequestHandler, which DefaultRequestHandler implements
|
|
22
|
+
const agentCard = await this.requestHandler.getAgentCard();
|
|
23
|
+
res.json(agentCard);
|
|
24
|
+
}
|
|
25
|
+
catch (error) {
|
|
26
|
+
console.error("Error fetching agent card:", error);
|
|
27
|
+
res.status(500).json({ error: "Failed to retrieve agent card" });
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
app.post(baseUrl, async (req, res) => {
|
|
31
|
+
try {
|
|
32
|
+
const rpcResponseOrStream = await this.jsonRpcTransportHandler.handle(req.body);
|
|
33
|
+
// Check if it's an AsyncGenerator (stream)
|
|
34
|
+
if (typeof rpcResponseOrStream?.[Symbol.asyncIterator] === 'function') {
|
|
35
|
+
const stream = rpcResponseOrStream;
|
|
36
|
+
res.setHeader('Content-Type', 'text/event-stream');
|
|
37
|
+
res.setHeader('Cache-Control', 'no-cache');
|
|
38
|
+
res.setHeader('Connection', 'keep-alive');
|
|
39
|
+
res.flushHeaders();
|
|
40
|
+
try {
|
|
41
|
+
for await (const event of stream) {
|
|
42
|
+
// Each event from the stream is already a JSONRPCResult
|
|
43
|
+
res.write(`id: ${new Date().getTime()}\n`);
|
|
44
|
+
res.write(`data: ${JSON.stringify(event)}\n\n`);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
catch (streamError) {
|
|
48
|
+
console.error(`Error during SSE streaming (request ${req.body?.id}):`, streamError);
|
|
49
|
+
// If the stream itself throws an error, send a final JSONRPCErrorResponse
|
|
50
|
+
const a2aError = streamError instanceof A2AError ? streamError : A2AError.internalError(streamError.message || 'Streaming error.');
|
|
51
|
+
const errorResponse = {
|
|
52
|
+
jsonrpc: '2.0',
|
|
53
|
+
id: req.body?.id || null, // Use original request ID if available
|
|
54
|
+
error: a2aError.toJSONRPCError(),
|
|
55
|
+
};
|
|
56
|
+
if (!res.headersSent) { // Should not happen if flushHeaders worked
|
|
57
|
+
res.status(500).json(errorResponse); // Should be JSON, not SSE here
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
// Try to send as last SSE event if possible, though client might have disconnected
|
|
61
|
+
res.write(`id: ${new Date().getTime()}\n`);
|
|
62
|
+
res.write(`event: error\n`); // Custom event type for client-side handling
|
|
63
|
+
res.write(`data: ${JSON.stringify(errorResponse)}\n\n`);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
finally {
|
|
67
|
+
if (!res.writableEnded) {
|
|
68
|
+
res.end();
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
else { // Single JSON-RPC response
|
|
73
|
+
const rpcResponse = rpcResponseOrStream;
|
|
74
|
+
res.status(200).json(rpcResponse);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
catch (error) { // Catch errors from jsonRpcTransportHandler.handle itself (e.g., initial parse error)
|
|
78
|
+
console.error("Unhandled error in A2AExpressApp POST handler:", error);
|
|
79
|
+
const a2aError = error instanceof A2AError ? error : A2AError.internalError('General processing error.');
|
|
80
|
+
const errorResponse = {
|
|
81
|
+
jsonrpc: '2.0',
|
|
82
|
+
id: req.body?.id || null,
|
|
83
|
+
error: a2aError.toJSONRPCError(),
|
|
84
|
+
};
|
|
85
|
+
if (!res.headersSent) {
|
|
86
|
+
res.status(500).json(errorResponse);
|
|
87
|
+
}
|
|
88
|
+
else if (!res.writableEnded) {
|
|
89
|
+
// If headers sent (likely during a stream attempt that failed early), try to end gracefully
|
|
90
|
+
res.end();
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
// The separate /stream endpoint is no longer needed.
|
|
95
|
+
return app;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
//# sourceMappingURL=a2a_express_app.js.map
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { ExecutionEventBus } from "../events/execution_event_bus.js";
|
|
2
|
+
import { RequestContext } from "./request_context.js";
|
|
3
|
+
export interface AgentExecutor {
|
|
4
|
+
/**
|
|
5
|
+
* Executes the agent logic based on the request context and publishes events.
|
|
6
|
+
* @param requestContext The context of the current request.
|
|
7
|
+
* @param eventBus The bus to publish execution events to.
|
|
8
|
+
*/
|
|
9
|
+
execute: (requestContext: RequestContext, eventBus: ExecutionEventBus) => Promise<void>;
|
|
10
|
+
/**
|
|
11
|
+
* Method to explicitly cancel a running task.
|
|
12
|
+
* The implementation should handle the logic of stopping the execution
|
|
13
|
+
* and publishing the final 'canceled' status event on the provided event bus.
|
|
14
|
+
* @param taskId The ID of the task to cancel.
|
|
15
|
+
* @param eventBus The event bus associated with the task's execution.
|
|
16
|
+
*/
|
|
17
|
+
cancelTask: (taskId: string, eventBus: ExecutionEventBus) => Promise<void>;
|
|
18
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Message, Task } from "../../types.js";
|
|
2
|
+
export declare class RequestContext {
|
|
3
|
+
readonly userMessage: Message;
|
|
4
|
+
readonly task?: Task;
|
|
5
|
+
readonly referenceTasks?: Task[];
|
|
6
|
+
readonly taskId: string;
|
|
7
|
+
readonly contextId: string;
|
|
8
|
+
constructor(userMessage: Message, taskId: string, contextId: string, task?: Task, referenceTasks?: Task[]);
|
|
9
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export class RequestContext {
|
|
2
|
+
userMessage;
|
|
3
|
+
task;
|
|
4
|
+
referenceTasks;
|
|
5
|
+
taskId;
|
|
6
|
+
contextId;
|
|
7
|
+
constructor(userMessage, taskId, contextId, task, referenceTasks) {
|
|
8
|
+
this.userMessage = userMessage;
|
|
9
|
+
this.taskId = taskId;
|
|
10
|
+
this.contextId = contextId;
|
|
11
|
+
this.task = task;
|
|
12
|
+
this.referenceTasks = referenceTasks;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=request_context.js.map
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import * as schema from "../types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Custom error class for A2A server operations, incorporating JSON-RPC error codes.
|
|
4
|
+
*/
|
|
5
|
+
export declare class A2AError extends Error {
|
|
6
|
+
code: number;
|
|
7
|
+
data?: Record<string, unknown>;
|
|
8
|
+
taskId?: string;
|
|
9
|
+
constructor(code: number, message: string, data?: Record<string, unknown>, taskId?: string);
|
|
10
|
+
/**
|
|
11
|
+
* Formats the error into a standard JSON-RPC error object structure.
|
|
12
|
+
*/
|
|
13
|
+
toJSONRPCError(): schema.JSONRPCError;
|
|
14
|
+
static parseError(message: string, data?: Record<string, unknown>): A2AError;
|
|
15
|
+
static invalidRequest(message: string, data?: Record<string, unknown>): A2AError;
|
|
16
|
+
static methodNotFound(method: string): A2AError;
|
|
17
|
+
static invalidParams(message: string, data?: Record<string, unknown>): A2AError;
|
|
18
|
+
static internalError(message: string, data?: Record<string, unknown>): A2AError;
|
|
19
|
+
static taskNotFound(taskId: string): A2AError;
|
|
20
|
+
static taskNotCancelable(taskId: string): A2AError;
|
|
21
|
+
static pushNotificationNotSupported(): A2AError;
|
|
22
|
+
static unsupportedOperation(operation: string): A2AError;
|
|
23
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Custom error class for A2A server operations, incorporating JSON-RPC error codes.
|
|
3
|
+
*/
|
|
4
|
+
export class A2AError extends Error {
|
|
5
|
+
code;
|
|
6
|
+
data;
|
|
7
|
+
taskId; // Optional task ID context
|
|
8
|
+
constructor(code, message, data, taskId) {
|
|
9
|
+
super(message);
|
|
10
|
+
this.name = "A2AError";
|
|
11
|
+
this.code = code;
|
|
12
|
+
this.data = data;
|
|
13
|
+
this.taskId = taskId; // Store associated task ID if provided
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Formats the error into a standard JSON-RPC error object structure.
|
|
17
|
+
*/
|
|
18
|
+
toJSONRPCError() {
|
|
19
|
+
const errorObject = {
|
|
20
|
+
code: this.code,
|
|
21
|
+
message: this.message,
|
|
22
|
+
};
|
|
23
|
+
if (this.data !== undefined) {
|
|
24
|
+
errorObject.data = this.data;
|
|
25
|
+
}
|
|
26
|
+
return errorObject;
|
|
27
|
+
}
|
|
28
|
+
// Static factory methods for common errors
|
|
29
|
+
static parseError(message, data) {
|
|
30
|
+
return new A2AError(-32700, message, data);
|
|
31
|
+
}
|
|
32
|
+
static invalidRequest(message, data) {
|
|
33
|
+
return new A2AError(-32600, message, data);
|
|
34
|
+
}
|
|
35
|
+
static methodNotFound(method) {
|
|
36
|
+
return new A2AError(-32601, `Method not found: ${method}`);
|
|
37
|
+
}
|
|
38
|
+
static invalidParams(message, data) {
|
|
39
|
+
return new A2AError(-32602, message, data);
|
|
40
|
+
}
|
|
41
|
+
static internalError(message, data) {
|
|
42
|
+
return new A2AError(-32603, message, data);
|
|
43
|
+
}
|
|
44
|
+
static taskNotFound(taskId) {
|
|
45
|
+
return new A2AError(-32001, `Task not found: ${taskId}`, undefined, taskId);
|
|
46
|
+
}
|
|
47
|
+
static taskNotCancelable(taskId) {
|
|
48
|
+
return new A2AError(-32002, `Task not cancelable: ${taskId}`, undefined, taskId);
|
|
49
|
+
}
|
|
50
|
+
static pushNotificationNotSupported() {
|
|
51
|
+
return new A2AError(-32003, "Push Notification is not supported");
|
|
52
|
+
}
|
|
53
|
+
static unsupportedOperation(operation) {
|
|
54
|
+
return new A2AError(-32004, `Unsupported operation: ${operation}`);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
//# sourceMappingURL=error.js.map
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { EventEmitter } from 'events';
|
|
2
|
+
import { Message, Task, TaskStatusUpdateEvent, TaskArtifactUpdateEvent } from "../../types.js";
|
|
3
|
+
export type AgentExecutionEvent = Message | Task | TaskStatusUpdateEvent | TaskArtifactUpdateEvent;
|
|
4
|
+
export interface ExecutionEventBus {
|
|
5
|
+
publish(event: AgentExecutionEvent): void;
|
|
6
|
+
on(eventName: 'event' | 'finished', listener: (event: AgentExecutionEvent) => void): this;
|
|
7
|
+
off(eventName: 'event' | 'finished', listener: (event: AgentExecutionEvent) => void): this;
|
|
8
|
+
once(eventName: 'event' | 'finished', listener: (event: AgentExecutionEvent) => void): this;
|
|
9
|
+
removeAllListeners(eventName?: 'event' | 'finished'): this;
|
|
10
|
+
finished(): void;
|
|
11
|
+
}
|
|
12
|
+
export declare class DefaultExecutionEventBus extends EventEmitter implements ExecutionEventBus {
|
|
13
|
+
constructor();
|
|
14
|
+
publish(event: AgentExecutionEvent): void;
|
|
15
|
+
finished(): void;
|
|
16
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { EventEmitter } from 'events';
|
|
2
|
+
export class DefaultExecutionEventBus extends EventEmitter {
|
|
3
|
+
constructor() {
|
|
4
|
+
super();
|
|
5
|
+
}
|
|
6
|
+
publish(event) {
|
|
7
|
+
this.emit('event', event);
|
|
8
|
+
}
|
|
9
|
+
finished() {
|
|
10
|
+
this.emit('finished');
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=execution_event_bus.js.map
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { ExecutionEventBus } from "./execution_event_bus.js";
|
|
2
|
+
export interface ExecutionEventBusManager {
|
|
3
|
+
createOrGetByTaskId(taskId: string): ExecutionEventBus;
|
|
4
|
+
getByTaskId(taskId: string): ExecutionEventBus | undefined;
|
|
5
|
+
cleanupByTaskId(taskId: string): void;
|
|
6
|
+
}
|
|
7
|
+
export declare class DefaultExecutionEventBusManager implements ExecutionEventBusManager {
|
|
8
|
+
private taskIdToBus;
|
|
9
|
+
/**
|
|
10
|
+
* Creates or retrieves an existing ExecutionEventBus based on the taskId.
|
|
11
|
+
* @param taskId The ID of the task.
|
|
12
|
+
* @returns An instance of IExecutionEventBus.
|
|
13
|
+
*/
|
|
14
|
+
createOrGetByTaskId(taskId: string): ExecutionEventBus;
|
|
15
|
+
/**
|
|
16
|
+
* Retrieves an existing ExecutionEventBus based on the taskId.
|
|
17
|
+
* @param taskId The ID of the task.
|
|
18
|
+
* @returns An instance of IExecutionEventBus or undefined if not found.
|
|
19
|
+
*/
|
|
20
|
+
getByTaskId(taskId: string): ExecutionEventBus | undefined;
|
|
21
|
+
/**
|
|
22
|
+
* Removes the event bus for a given taskId.
|
|
23
|
+
* This should be called when an execution flow is complete to free resources.
|
|
24
|
+
* @param taskId The ID of the task.
|
|
25
|
+
*/
|
|
26
|
+
cleanupByTaskId(taskId: string): void;
|
|
27
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { DefaultExecutionEventBus } from "./execution_event_bus.js";
|
|
2
|
+
export class DefaultExecutionEventBusManager {
|
|
3
|
+
taskIdToBus = new Map();
|
|
4
|
+
/**
|
|
5
|
+
* Creates or retrieves an existing ExecutionEventBus based on the taskId.
|
|
6
|
+
* @param taskId The ID of the task.
|
|
7
|
+
* @returns An instance of IExecutionEventBus.
|
|
8
|
+
*/
|
|
9
|
+
createOrGetByTaskId(taskId) {
|
|
10
|
+
if (!this.taskIdToBus.has(taskId)) {
|
|
11
|
+
this.taskIdToBus.set(taskId, new DefaultExecutionEventBus());
|
|
12
|
+
}
|
|
13
|
+
return this.taskIdToBus.get(taskId);
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Retrieves an existing ExecutionEventBus based on the taskId.
|
|
17
|
+
* @param taskId The ID of the task.
|
|
18
|
+
* @returns An instance of IExecutionEventBus or undefined if not found.
|
|
19
|
+
*/
|
|
20
|
+
getByTaskId(taskId) {
|
|
21
|
+
return this.taskIdToBus.get(taskId);
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Removes the event bus for a given taskId.
|
|
25
|
+
* This should be called when an execution flow is complete to free resources.
|
|
26
|
+
* @param taskId The ID of the task.
|
|
27
|
+
*/
|
|
28
|
+
cleanupByTaskId(taskId) {
|
|
29
|
+
const bus = this.taskIdToBus.get(taskId);
|
|
30
|
+
if (bus) {
|
|
31
|
+
bus.removeAllListeners();
|
|
32
|
+
}
|
|
33
|
+
this.taskIdToBus.delete(taskId);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
//# sourceMappingURL=execution_event_bus_manager.js.map
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { ExecutionEventBus, AgentExecutionEvent } from "./execution_event_bus.js";
|
|
2
|
+
/**
|
|
3
|
+
* An async queue that subscribes to an ExecutionEventBus for events
|
|
4
|
+
* and provides an async generator to consume them.
|
|
5
|
+
*/
|
|
6
|
+
export declare class ExecutionEventQueue {
|
|
7
|
+
private eventBus;
|
|
8
|
+
private eventQueue;
|
|
9
|
+
private resolvePromise?;
|
|
10
|
+
private stopped;
|
|
11
|
+
private boundHandleEvent;
|
|
12
|
+
constructor(eventBus: ExecutionEventBus);
|
|
13
|
+
private handleEvent;
|
|
14
|
+
private handleFinished;
|
|
15
|
+
/**
|
|
16
|
+
* Provides an async generator that yields events from the event bus.
|
|
17
|
+
* Stops when a Message event is received or a TaskStatusUpdateEvent with final=true is received.
|
|
18
|
+
*/
|
|
19
|
+
events(): AsyncGenerator<AgentExecutionEvent, void, undefined>;
|
|
20
|
+
/**
|
|
21
|
+
* Stops the event queue from processing further events.
|
|
22
|
+
*/
|
|
23
|
+
stop(): void;
|
|
24
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An async queue that subscribes to an ExecutionEventBus for events
|
|
3
|
+
* and provides an async generator to consume them.
|
|
4
|
+
*/
|
|
5
|
+
export class ExecutionEventQueue {
|
|
6
|
+
eventBus;
|
|
7
|
+
eventQueue = [];
|
|
8
|
+
resolvePromise;
|
|
9
|
+
stopped = false;
|
|
10
|
+
boundHandleEvent;
|
|
11
|
+
constructor(eventBus) {
|
|
12
|
+
this.eventBus = eventBus;
|
|
13
|
+
this.eventBus.on('event', this.handleEvent);
|
|
14
|
+
this.eventBus.on('finished', this.handleFinished);
|
|
15
|
+
}
|
|
16
|
+
handleEvent = (event) => {
|
|
17
|
+
if (this.stopped)
|
|
18
|
+
return;
|
|
19
|
+
this.eventQueue.push(event);
|
|
20
|
+
if (this.resolvePromise) {
|
|
21
|
+
this.resolvePromise();
|
|
22
|
+
this.resolvePromise = undefined;
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
handleFinished = () => {
|
|
26
|
+
this.stop();
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* Provides an async generator that yields events from the event bus.
|
|
30
|
+
* Stops when a Message event is received or a TaskStatusUpdateEvent with final=true is received.
|
|
31
|
+
*/
|
|
32
|
+
async *events() {
|
|
33
|
+
while (!this.stopped || this.eventQueue.length > 0) {
|
|
34
|
+
if (this.eventQueue.length > 0) {
|
|
35
|
+
const event = this.eventQueue.shift();
|
|
36
|
+
yield event;
|
|
37
|
+
if (event.kind === 'message' || (event.kind === 'status-update' &&
|
|
38
|
+
event.final)) {
|
|
39
|
+
this.handleFinished();
|
|
40
|
+
break;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
else if (!this.stopped) {
|
|
44
|
+
await new Promise((resolve) => {
|
|
45
|
+
this.resolvePromise = resolve;
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Stops the event queue from processing further events.
|
|
52
|
+
*/
|
|
53
|
+
stop() {
|
|
54
|
+
this.stopped = true;
|
|
55
|
+
if (this.resolvePromise) {
|
|
56
|
+
this.resolvePromise(); // Unblock any pending await
|
|
57
|
+
this.resolvePromise = undefined;
|
|
58
|
+
}
|
|
59
|
+
this.eventBus.off('event', this.handleEvent);
|
|
60
|
+
this.eventBus.off('finished', this.handleFinished);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
//# sourceMappingURL=execution_event_queue.js.map
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Message, AgentCard, MessageSendParams, Task, TaskStatusUpdateEvent, TaskArtifactUpdateEvent, TaskQueryParams, TaskIdParams, TaskPushNotificationConfig } from "../../types.js";
|
|
2
|
+
export interface A2ARequestHandler {
|
|
3
|
+
getAgentCard(): Promise<AgentCard>;
|
|
4
|
+
sendMessage(params: MessageSendParams): Promise<Message | Task>;
|
|
5
|
+
sendMessageStream(params: MessageSendParams): AsyncGenerator<Message | Task | TaskStatusUpdateEvent | TaskArtifactUpdateEvent, void, undefined>;
|
|
6
|
+
getTask(params: TaskQueryParams): Promise<Task>;
|
|
7
|
+
cancelTask(params: TaskIdParams): Promise<Task>;
|
|
8
|
+
setTaskPushNotificationConfig(params: TaskPushNotificationConfig): Promise<TaskPushNotificationConfig>;
|
|
9
|
+
getTaskPushNotificationConfig(params: TaskIdParams): Promise<TaskPushNotificationConfig>;
|
|
10
|
+
resubscribe(params: TaskIdParams): AsyncGenerator<Task | TaskStatusUpdateEvent | TaskArtifactUpdateEvent, void, undefined>;
|
|
11
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Message, AgentCard, Task, MessageSendParams, TaskStatusUpdateEvent, TaskArtifactUpdateEvent, TaskQueryParams, TaskIdParams, TaskPushNotificationConfig } from "../../types.js";
|
|
2
|
+
import { AgentExecutor } from "../agent_execution/agent_executor.js";
|
|
3
|
+
import { ExecutionEventBusManager } from "../events/execution_event_bus_manager.js";
|
|
4
|
+
import { TaskStore } from "../store.js";
|
|
5
|
+
import { A2ARequestHandler } from "./a2a_request_handler.js";
|
|
6
|
+
export declare class DefaultRequestHandler implements A2ARequestHandler {
|
|
7
|
+
private readonly agentCard;
|
|
8
|
+
private readonly taskStore;
|
|
9
|
+
private readonly agentExecutor;
|
|
10
|
+
private readonly eventBusManager;
|
|
11
|
+
private readonly pushNotificationConfigs;
|
|
12
|
+
constructor(agentCard: AgentCard, taskStore: TaskStore, agentExecutor: AgentExecutor, eventBusManager?: ExecutionEventBusManager);
|
|
13
|
+
getAgentCard(): Promise<AgentCard>;
|
|
14
|
+
private _createRequestContext;
|
|
15
|
+
sendMessage(params: MessageSendParams): Promise<Message | Task>;
|
|
16
|
+
sendMessageStream(params: MessageSendParams): AsyncGenerator<Message | Task | TaskStatusUpdateEvent | TaskArtifactUpdateEvent, void, undefined>;
|
|
17
|
+
getTask(params: TaskQueryParams): Promise<Task>;
|
|
18
|
+
cancelTask(params: TaskIdParams): Promise<Task>;
|
|
19
|
+
setTaskPushNotificationConfig(params: TaskPushNotificationConfig): Promise<TaskPushNotificationConfig>;
|
|
20
|
+
getTaskPushNotificationConfig(params: TaskIdParams): Promise<TaskPushNotificationConfig>;
|
|
21
|
+
resubscribe(params: TaskIdParams): AsyncGenerator<Task | TaskStatusUpdateEvent | TaskArtifactUpdateEvent, void, undefined>;
|
|
22
|
+
}
|