@mailmodo/a2a 0.3.3 → 0.3.7
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/README.md +385 -173
- package/dist/a2a_request_handler-1Isk3l-0.d.mts +49 -0
- package/dist/a2a_request_handler-BuP9LgXH.d.ts +49 -0
- package/dist/chunk-7JFJW6P6.mjs +38 -0
- package/dist/chunk-AZGEDUZX.mjs +261 -0
- package/dist/chunk-BNBEZNW7.mjs +122 -0
- package/dist/chunk-PHP7LM4Y.mjs +8 -0
- package/dist/client/index.d.mts +666 -0
- package/dist/client/index.d.ts +666 -0
- package/dist/client/index.js +1605 -0
- package/dist/client/index.mjs +1448 -0
- package/dist/extensions-DvruCIzw.d.mts +2589 -0
- package/dist/extensions-DvruCIzw.d.ts +2589 -0
- package/dist/index.d.mts +5 -2758
- package/dist/index.d.ts +5 -2758
- package/dist/index.js +28 -1637
- package/dist/index.mjs +9 -1628
- package/dist/server/express/index.d.mts +97 -0
- package/dist/server/express/index.d.ts +97 -0
- package/dist/server/express/index.js +994 -0
- package/dist/server/express/index.mjs +646 -0
- package/dist/server/index.d.mts +316 -0
- package/dist/server/index.d.ts +316 -0
- package/dist/server/index.js +1386 -0
- package/dist/server/index.mjs +1070 -0
- package/package.json +51 -22
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { E as Extensions, ae as AgentCard, x as MessageSendParams, F as Message, ay as Task, aQ as TaskStatusUpdateEvent, aS as TaskArtifactUpdateEvent, X as TaskQueryParams, Z as TaskIdParams, $ as TaskPushNotificationConfig, a3 as GetTaskPushNotificationConfigParams, a7 as ListTaskPushNotificationConfigParams, a9 as DeleteTaskPushNotificationConfigParams } from './extensions-DvruCIzw.mjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Represents a user accessing A2A server.
|
|
5
|
+
*/
|
|
6
|
+
interface User {
|
|
7
|
+
/**
|
|
8
|
+
* Indicates whether the user is authenticated.
|
|
9
|
+
*/
|
|
10
|
+
get isAuthenticated(): boolean;
|
|
11
|
+
/**
|
|
12
|
+
* A unique name (identifier) for the user.
|
|
13
|
+
*/
|
|
14
|
+
get userName(): string;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* An implementation of {@link User} representing an unauthenticated user.
|
|
18
|
+
*/
|
|
19
|
+
declare class UnauthenticatedUser implements User {
|
|
20
|
+
get isAuthenticated(): boolean;
|
|
21
|
+
get userName(): string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
declare class ServerCallContext {
|
|
25
|
+
private readonly _requestedExtensions?;
|
|
26
|
+
private readonly _user?;
|
|
27
|
+
private _activatedExtensions?;
|
|
28
|
+
constructor(requestedExtensions?: Extensions, user?: User);
|
|
29
|
+
get user(): User | undefined;
|
|
30
|
+
get activatedExtensions(): Extensions | undefined;
|
|
31
|
+
get requestedExtensions(): Extensions | undefined;
|
|
32
|
+
addActivatedExtension(uri: string): void;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
interface A2ARequestHandler {
|
|
36
|
+
getAgentCard(): Promise<AgentCard>;
|
|
37
|
+
getAuthenticatedExtendedAgentCard(context?: ServerCallContext): Promise<AgentCard>;
|
|
38
|
+
sendMessage(params: MessageSendParams, context?: ServerCallContext): Promise<Message | Task>;
|
|
39
|
+
sendMessageStream(params: MessageSendParams, context?: ServerCallContext): AsyncGenerator<Message | Task | TaskStatusUpdateEvent | TaskArtifactUpdateEvent, void, undefined>;
|
|
40
|
+
getTask(params: TaskQueryParams, context?: ServerCallContext): Promise<Task>;
|
|
41
|
+
cancelTask(params: TaskIdParams, context?: ServerCallContext): Promise<Task>;
|
|
42
|
+
setTaskPushNotificationConfig(params: TaskPushNotificationConfig, context?: ServerCallContext): Promise<TaskPushNotificationConfig>;
|
|
43
|
+
getTaskPushNotificationConfig(params: TaskIdParams | GetTaskPushNotificationConfigParams, context?: ServerCallContext): Promise<TaskPushNotificationConfig>;
|
|
44
|
+
listTaskPushNotificationConfigs(params: ListTaskPushNotificationConfigParams, context?: ServerCallContext): Promise<TaskPushNotificationConfig[]>;
|
|
45
|
+
deleteTaskPushNotificationConfig(params: DeleteTaskPushNotificationConfigParams, context?: ServerCallContext): Promise<void>;
|
|
46
|
+
resubscribe(params: TaskIdParams, context?: ServerCallContext): AsyncGenerator<Task | TaskStatusUpdateEvent | TaskArtifactUpdateEvent, void, undefined>;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export { type A2ARequestHandler as A, ServerCallContext as S, type User as U, UnauthenticatedUser as a };
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { E as Extensions, ae as AgentCard, x as MessageSendParams, F as Message, ay as Task, aQ as TaskStatusUpdateEvent, aS as TaskArtifactUpdateEvent, X as TaskQueryParams, Z as TaskIdParams, $ as TaskPushNotificationConfig, a3 as GetTaskPushNotificationConfigParams, a7 as ListTaskPushNotificationConfigParams, a9 as DeleteTaskPushNotificationConfigParams } from './extensions-DvruCIzw.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Represents a user accessing A2A server.
|
|
5
|
+
*/
|
|
6
|
+
interface User {
|
|
7
|
+
/**
|
|
8
|
+
* Indicates whether the user is authenticated.
|
|
9
|
+
*/
|
|
10
|
+
get isAuthenticated(): boolean;
|
|
11
|
+
/**
|
|
12
|
+
* A unique name (identifier) for the user.
|
|
13
|
+
*/
|
|
14
|
+
get userName(): string;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* An implementation of {@link User} representing an unauthenticated user.
|
|
18
|
+
*/
|
|
19
|
+
declare class UnauthenticatedUser implements User {
|
|
20
|
+
get isAuthenticated(): boolean;
|
|
21
|
+
get userName(): string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
declare class ServerCallContext {
|
|
25
|
+
private readonly _requestedExtensions?;
|
|
26
|
+
private readonly _user?;
|
|
27
|
+
private _activatedExtensions?;
|
|
28
|
+
constructor(requestedExtensions?: Extensions, user?: User);
|
|
29
|
+
get user(): User | undefined;
|
|
30
|
+
get activatedExtensions(): Extensions | undefined;
|
|
31
|
+
get requestedExtensions(): Extensions | undefined;
|
|
32
|
+
addActivatedExtension(uri: string): void;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
interface A2ARequestHandler {
|
|
36
|
+
getAgentCard(): Promise<AgentCard>;
|
|
37
|
+
getAuthenticatedExtendedAgentCard(context?: ServerCallContext): Promise<AgentCard>;
|
|
38
|
+
sendMessage(params: MessageSendParams, context?: ServerCallContext): Promise<Message | Task>;
|
|
39
|
+
sendMessageStream(params: MessageSendParams, context?: ServerCallContext): AsyncGenerator<Message | Task | TaskStatusUpdateEvent | TaskArtifactUpdateEvent, void, undefined>;
|
|
40
|
+
getTask(params: TaskQueryParams, context?: ServerCallContext): Promise<Task>;
|
|
41
|
+
cancelTask(params: TaskIdParams, context?: ServerCallContext): Promise<Task>;
|
|
42
|
+
setTaskPushNotificationConfig(params: TaskPushNotificationConfig, context?: ServerCallContext): Promise<TaskPushNotificationConfig>;
|
|
43
|
+
getTaskPushNotificationConfig(params: TaskIdParams | GetTaskPushNotificationConfigParams, context?: ServerCallContext): Promise<TaskPushNotificationConfig>;
|
|
44
|
+
listTaskPushNotificationConfigs(params: ListTaskPushNotificationConfigParams, context?: ServerCallContext): Promise<TaskPushNotificationConfig[]>;
|
|
45
|
+
deleteTaskPushNotificationConfig(params: DeleteTaskPushNotificationConfigParams, context?: ServerCallContext): Promise<void>;
|
|
46
|
+
resubscribe(params: TaskIdParams, context?: ServerCallContext): AsyncGenerator<Task | TaskStatusUpdateEvent | TaskArtifactUpdateEvent, void, undefined>;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export { type A2ARequestHandler as A, ServerCallContext as S, type User as U, UnauthenticatedUser as a };
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
// src/extensions.ts
|
|
2
|
+
var Extensions = {
|
|
3
|
+
/**
|
|
4
|
+
* Creates new {@link Extensions} from `current` and `additional`.
|
|
5
|
+
* If `current` already contains `additional` it is returned unmodified.
|
|
6
|
+
*/
|
|
7
|
+
createFrom: (current, additional) => {
|
|
8
|
+
if (current?.includes(additional)) {
|
|
9
|
+
return current;
|
|
10
|
+
}
|
|
11
|
+
return [...current ?? [], additional];
|
|
12
|
+
},
|
|
13
|
+
/**
|
|
14
|
+
* Creates {@link Extensions} from comma separated extensions identifiers as per
|
|
15
|
+
* https://a2a-protocol.org/latest/specification/#326-service-parameters.
|
|
16
|
+
* Parses the output of `toServiceParameter`.
|
|
17
|
+
*/
|
|
18
|
+
parseServiceParameter: (value) => {
|
|
19
|
+
if (!value) {
|
|
20
|
+
return [];
|
|
21
|
+
}
|
|
22
|
+
const unique = new Set(
|
|
23
|
+
value.split(",").map((ext) => ext.trim()).filter((ext) => ext.length > 0)
|
|
24
|
+
);
|
|
25
|
+
return Array.from(unique);
|
|
26
|
+
},
|
|
27
|
+
/**
|
|
28
|
+
* Converts {@link Extensions} to comma separated extensions identifiers as per
|
|
29
|
+
* https://a2a-protocol.org/latest/specification/#326-service-parameters.
|
|
30
|
+
*/
|
|
31
|
+
toServiceParameter: (value) => {
|
|
32
|
+
return value.join(",");
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
export {
|
|
37
|
+
Extensions
|
|
38
|
+
};
|
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Extensions
|
|
3
|
+
} from "./chunk-7JFJW6P6.mjs";
|
|
4
|
+
|
|
5
|
+
// src/server/error.ts
|
|
6
|
+
var A2AError = class _A2AError extends Error {
|
|
7
|
+
code;
|
|
8
|
+
data;
|
|
9
|
+
taskId;
|
|
10
|
+
// Optional task ID context
|
|
11
|
+
constructor(code, message, data, taskId) {
|
|
12
|
+
super(message);
|
|
13
|
+
this.name = "A2AError";
|
|
14
|
+
this.code = code;
|
|
15
|
+
this.data = data;
|
|
16
|
+
this.taskId = taskId;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Formats the error into a standard JSON-RPC error object structure.
|
|
20
|
+
*/
|
|
21
|
+
toJSONRPCError() {
|
|
22
|
+
const errorObject = {
|
|
23
|
+
code: this.code,
|
|
24
|
+
message: this.message
|
|
25
|
+
};
|
|
26
|
+
if (this.data !== void 0) {
|
|
27
|
+
errorObject.data = this.data;
|
|
28
|
+
}
|
|
29
|
+
return errorObject;
|
|
30
|
+
}
|
|
31
|
+
// Static factory methods for common errors
|
|
32
|
+
static parseError(message, data) {
|
|
33
|
+
return new _A2AError(-32700, message, data);
|
|
34
|
+
}
|
|
35
|
+
static invalidRequest(message, data) {
|
|
36
|
+
return new _A2AError(-32600, message, data);
|
|
37
|
+
}
|
|
38
|
+
static methodNotFound(method) {
|
|
39
|
+
return new _A2AError(-32601, `Method not found: ${method}`);
|
|
40
|
+
}
|
|
41
|
+
static invalidParams(message, data) {
|
|
42
|
+
return new _A2AError(-32602, message, data);
|
|
43
|
+
}
|
|
44
|
+
static internalError(message, data) {
|
|
45
|
+
return new _A2AError(-32603, message, data);
|
|
46
|
+
}
|
|
47
|
+
static taskNotFound(taskId) {
|
|
48
|
+
return new _A2AError(-32001, `Task not found: ${taskId}`, void 0, taskId);
|
|
49
|
+
}
|
|
50
|
+
static taskNotCancelable(taskId) {
|
|
51
|
+
return new _A2AError(-32002, `Task not cancelable: ${taskId}`, void 0, taskId);
|
|
52
|
+
}
|
|
53
|
+
static pushNotificationNotSupported() {
|
|
54
|
+
return new _A2AError(-32003, "Push Notification is not supported");
|
|
55
|
+
}
|
|
56
|
+
static unsupportedOperation(operation) {
|
|
57
|
+
return new _A2AError(-32004, `Unsupported operation: ${operation}`);
|
|
58
|
+
}
|
|
59
|
+
static authenticatedExtendedCardNotConfigured() {
|
|
60
|
+
return new _A2AError(-32007, `Extended card not configured.`);
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
// src/server/context.ts
|
|
65
|
+
var ServerCallContext = class {
|
|
66
|
+
_requestedExtensions;
|
|
67
|
+
_user;
|
|
68
|
+
_activatedExtensions;
|
|
69
|
+
constructor(requestedExtensions, user) {
|
|
70
|
+
this._requestedExtensions = requestedExtensions;
|
|
71
|
+
this._user = user;
|
|
72
|
+
}
|
|
73
|
+
get user() {
|
|
74
|
+
return this._user;
|
|
75
|
+
}
|
|
76
|
+
get activatedExtensions() {
|
|
77
|
+
return this._activatedExtensions;
|
|
78
|
+
}
|
|
79
|
+
get requestedExtensions() {
|
|
80
|
+
return this._requestedExtensions;
|
|
81
|
+
}
|
|
82
|
+
addActivatedExtension(uri) {
|
|
83
|
+
this._activatedExtensions = Extensions.createFrom(this._activatedExtensions, uri);
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
// src/server/transports/jsonrpc/jsonrpc_transport_handler.ts
|
|
88
|
+
var JsonRpcTransportHandler = class {
|
|
89
|
+
requestHandler;
|
|
90
|
+
constructor(requestHandler) {
|
|
91
|
+
this.requestHandler = requestHandler;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Handles an incoming JSON-RPC request.
|
|
95
|
+
* For streaming methods, it returns an AsyncGenerator of JSONRPCResult.
|
|
96
|
+
* For non-streaming methods, it returns a Promise of a single JSONRPCMessage (Result or ErrorResponse).
|
|
97
|
+
*/
|
|
98
|
+
async handle(requestBody, context) {
|
|
99
|
+
let rpcRequest;
|
|
100
|
+
try {
|
|
101
|
+
if (typeof requestBody === "string") {
|
|
102
|
+
rpcRequest = JSON.parse(requestBody);
|
|
103
|
+
} else if (typeof requestBody === "object" && requestBody !== null) {
|
|
104
|
+
rpcRequest = requestBody;
|
|
105
|
+
} else {
|
|
106
|
+
throw A2AError.parseError("Invalid request body type.");
|
|
107
|
+
}
|
|
108
|
+
if (!this.isRequestValid(rpcRequest)) {
|
|
109
|
+
throw A2AError.invalidRequest("Invalid JSON-RPC Request.");
|
|
110
|
+
}
|
|
111
|
+
} catch (error) {
|
|
112
|
+
const a2aError = error instanceof A2AError ? error : A2AError.parseError(
|
|
113
|
+
error instanceof SyntaxError && error.message || "Failed to parse JSON request."
|
|
114
|
+
);
|
|
115
|
+
return {
|
|
116
|
+
jsonrpc: "2.0",
|
|
117
|
+
id: rpcRequest?.id !== void 0 ? rpcRequest.id : null,
|
|
118
|
+
error: a2aError.toJSONRPCError()
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
const { method, id: requestId = null } = rpcRequest;
|
|
122
|
+
try {
|
|
123
|
+
if (method !== "agent/getAuthenticatedExtendedCard" && !this.paramsAreValid(rpcRequest.params)) {
|
|
124
|
+
throw A2AError.invalidParams(`Invalid method parameters.`);
|
|
125
|
+
}
|
|
126
|
+
if (method === "message/stream" || method === "tasks/resubscribe") {
|
|
127
|
+
const params = rpcRequest.params;
|
|
128
|
+
const agentCard = await this.requestHandler.getAgentCard();
|
|
129
|
+
if (!agentCard.capabilities.streaming) {
|
|
130
|
+
throw A2AError.unsupportedOperation(`Method ${method} requires streaming capability.`);
|
|
131
|
+
}
|
|
132
|
+
const agentEventStream = method === "message/stream" ? this.requestHandler.sendMessageStream(params, context) : this.requestHandler.resubscribe(params, context);
|
|
133
|
+
return (async function* jsonRpcEventStream() {
|
|
134
|
+
try {
|
|
135
|
+
for await (const event of agentEventStream) {
|
|
136
|
+
yield {
|
|
137
|
+
jsonrpc: "2.0",
|
|
138
|
+
id: requestId,
|
|
139
|
+
// Use the original request ID for all streamed responses
|
|
140
|
+
result: event
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
} catch (streamError) {
|
|
144
|
+
console.error(
|
|
145
|
+
`Error in agent event stream for ${method} (request ${requestId}):`,
|
|
146
|
+
streamError
|
|
147
|
+
);
|
|
148
|
+
throw streamError;
|
|
149
|
+
}
|
|
150
|
+
})();
|
|
151
|
+
} else {
|
|
152
|
+
let result;
|
|
153
|
+
switch (method) {
|
|
154
|
+
case "message/send":
|
|
155
|
+
result = await this.requestHandler.sendMessage(rpcRequest.params, context);
|
|
156
|
+
break;
|
|
157
|
+
case "tasks/get":
|
|
158
|
+
result = await this.requestHandler.getTask(rpcRequest.params, context);
|
|
159
|
+
break;
|
|
160
|
+
case "tasks/cancel":
|
|
161
|
+
result = await this.requestHandler.cancelTask(rpcRequest.params, context);
|
|
162
|
+
break;
|
|
163
|
+
case "tasks/pushNotificationConfig/set":
|
|
164
|
+
result = await this.requestHandler.setTaskPushNotificationConfig(
|
|
165
|
+
rpcRequest.params,
|
|
166
|
+
context
|
|
167
|
+
);
|
|
168
|
+
break;
|
|
169
|
+
case "tasks/pushNotificationConfig/get":
|
|
170
|
+
result = await this.requestHandler.getTaskPushNotificationConfig(
|
|
171
|
+
rpcRequest.params,
|
|
172
|
+
context
|
|
173
|
+
);
|
|
174
|
+
break;
|
|
175
|
+
case "tasks/pushNotificationConfig/delete":
|
|
176
|
+
await this.requestHandler.deleteTaskPushNotificationConfig(rpcRequest.params, context);
|
|
177
|
+
result = null;
|
|
178
|
+
break;
|
|
179
|
+
case "tasks/pushNotificationConfig/list":
|
|
180
|
+
result = await this.requestHandler.listTaskPushNotificationConfigs(
|
|
181
|
+
rpcRequest.params,
|
|
182
|
+
context
|
|
183
|
+
);
|
|
184
|
+
break;
|
|
185
|
+
case "agent/getAuthenticatedExtendedCard":
|
|
186
|
+
result = await this.requestHandler.getAuthenticatedExtendedAgentCard(context);
|
|
187
|
+
break;
|
|
188
|
+
default:
|
|
189
|
+
throw A2AError.methodNotFound(method);
|
|
190
|
+
}
|
|
191
|
+
return {
|
|
192
|
+
jsonrpc: "2.0",
|
|
193
|
+
id: requestId,
|
|
194
|
+
result
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
} catch (error) {
|
|
198
|
+
let a2aError;
|
|
199
|
+
if (error instanceof A2AError) {
|
|
200
|
+
a2aError = error;
|
|
201
|
+
} else {
|
|
202
|
+
a2aError = A2AError.internalError(
|
|
203
|
+
error instanceof Error && error.message || "An unexpected error occurred."
|
|
204
|
+
);
|
|
205
|
+
}
|
|
206
|
+
return {
|
|
207
|
+
jsonrpc: "2.0",
|
|
208
|
+
id: requestId,
|
|
209
|
+
error: a2aError.toJSONRPCError()
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
// Validates the basic structure of a JSON-RPC request
|
|
214
|
+
isRequestValid(rpcRequest) {
|
|
215
|
+
if (rpcRequest.jsonrpc !== "2.0") {
|
|
216
|
+
return false;
|
|
217
|
+
}
|
|
218
|
+
if ("id" in rpcRequest) {
|
|
219
|
+
const id = rpcRequest.id;
|
|
220
|
+
const isString = typeof id === "string";
|
|
221
|
+
const isInteger = typeof id === "number" && Number.isInteger(id);
|
|
222
|
+
const isNull = id === null;
|
|
223
|
+
if (!isString && !isInteger && !isNull) {
|
|
224
|
+
return false;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
if (!rpcRequest.method || typeof rpcRequest.method !== "string") {
|
|
228
|
+
return false;
|
|
229
|
+
}
|
|
230
|
+
return true;
|
|
231
|
+
}
|
|
232
|
+
// Validates that params is an object with non-empty string keys
|
|
233
|
+
paramsAreValid(params) {
|
|
234
|
+
if (typeof params !== "object" || params === null || Array.isArray(params)) {
|
|
235
|
+
return false;
|
|
236
|
+
}
|
|
237
|
+
for (const key of Object.keys(params)) {
|
|
238
|
+
if (key === "") {
|
|
239
|
+
return false;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
return true;
|
|
243
|
+
}
|
|
244
|
+
};
|
|
245
|
+
|
|
246
|
+
// src/server/authentication/user.ts
|
|
247
|
+
var UnauthenticatedUser = class {
|
|
248
|
+
get isAuthenticated() {
|
|
249
|
+
return false;
|
|
250
|
+
}
|
|
251
|
+
get userName() {
|
|
252
|
+
return "";
|
|
253
|
+
}
|
|
254
|
+
};
|
|
255
|
+
|
|
256
|
+
export {
|
|
257
|
+
A2AError,
|
|
258
|
+
ServerCallContext,
|
|
259
|
+
JsonRpcTransportHandler,
|
|
260
|
+
UnauthenticatedUser
|
|
261
|
+
};
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
// src/errors.ts
|
|
2
|
+
var A2A_ERROR_CODE = {
|
|
3
|
+
PARSE_ERROR: -32700,
|
|
4
|
+
INVALID_REQUEST: -32600,
|
|
5
|
+
METHOD_NOT_FOUND: -32601,
|
|
6
|
+
INVALID_PARAMS: -32602,
|
|
7
|
+
INTERNAL_ERROR: -32603,
|
|
8
|
+
TASK_NOT_FOUND: -32001,
|
|
9
|
+
TASK_NOT_CANCELABLE: -32002,
|
|
10
|
+
PUSH_NOTIFICATION_NOT_SUPPORTED: -32003,
|
|
11
|
+
UNSUPPORTED_OPERATION: -32004,
|
|
12
|
+
CONTENT_TYPE_NOT_SUPPORTED: -32005,
|
|
13
|
+
INVALID_AGENT_RESPONSE: -32006,
|
|
14
|
+
AUTHENTICATED_EXTENDED_CARD_NOT_CONFIGURED: -32007
|
|
15
|
+
};
|
|
16
|
+
var TaskNotFoundError = class extends Error {
|
|
17
|
+
constructor(message) {
|
|
18
|
+
super(message ?? "Task not found");
|
|
19
|
+
this.name = "TaskNotFoundError";
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
var TaskNotCancelableError = class extends Error {
|
|
23
|
+
constructor(message) {
|
|
24
|
+
super(message ?? "Task cannot be canceled");
|
|
25
|
+
this.name = "TaskNotCancelableError";
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
var PushNotificationNotSupportedError = class extends Error {
|
|
29
|
+
constructor(message) {
|
|
30
|
+
super(message ?? "Push Notification is not supported");
|
|
31
|
+
this.name = "PushNotificationNotSupportedError";
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
var UnsupportedOperationError = class extends Error {
|
|
35
|
+
constructor(message) {
|
|
36
|
+
super(message ?? "This operation is not supported");
|
|
37
|
+
this.name = "UnsupportedOperationError";
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
var ContentTypeNotSupportedError = class extends Error {
|
|
41
|
+
constructor(message) {
|
|
42
|
+
super(message ?? "Incompatible content types");
|
|
43
|
+
this.name = "ContentTypeNotSupportedError";
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
var InvalidAgentResponseError = class extends Error {
|
|
47
|
+
constructor(message) {
|
|
48
|
+
super(message ?? "Invalid agent response type");
|
|
49
|
+
this.name = "InvalidAgentResponseError";
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
var AuthenticatedExtendedCardNotConfiguredError = class extends Error {
|
|
53
|
+
constructor(message) {
|
|
54
|
+
super(message ?? "Authenticated Extended Card not configured");
|
|
55
|
+
this.name = "AuthenticatedExtendedCardNotConfiguredError";
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
// src/sse_utils.ts
|
|
60
|
+
var SSE_HEADERS = {
|
|
61
|
+
"Content-Type": "text/event-stream",
|
|
62
|
+
"Cache-Control": "no-cache",
|
|
63
|
+
Connection: "keep-alive",
|
|
64
|
+
"X-Accel-Buffering": "no"
|
|
65
|
+
// Disable buffering in nginx
|
|
66
|
+
};
|
|
67
|
+
function formatSSEEvent(event) {
|
|
68
|
+
return `data: ${JSON.stringify(event)}
|
|
69
|
+
|
|
70
|
+
`;
|
|
71
|
+
}
|
|
72
|
+
function formatSSEErrorEvent(error) {
|
|
73
|
+
return `event: error
|
|
74
|
+
data: ${JSON.stringify(error)}
|
|
75
|
+
|
|
76
|
+
`;
|
|
77
|
+
}
|
|
78
|
+
async function* parseSseStream(response) {
|
|
79
|
+
if (!response.body) {
|
|
80
|
+
throw new Error("SSE response body is undefined. Cannot read stream.");
|
|
81
|
+
}
|
|
82
|
+
let buffer = "";
|
|
83
|
+
let eventType = "message";
|
|
84
|
+
let eventData = "";
|
|
85
|
+
for await (const value of response.body.pipeThrough(new TextDecoderStream())) {
|
|
86
|
+
buffer += value;
|
|
87
|
+
let lineEndIndex;
|
|
88
|
+
while ((lineEndIndex = buffer.indexOf("\n")) >= 0) {
|
|
89
|
+
const line = buffer.substring(0, lineEndIndex).trim();
|
|
90
|
+
buffer = buffer.substring(lineEndIndex + 1);
|
|
91
|
+
if (line === "") {
|
|
92
|
+
if (eventData) {
|
|
93
|
+
yield { type: eventType, data: eventData };
|
|
94
|
+
eventData = "";
|
|
95
|
+
eventType = "message";
|
|
96
|
+
}
|
|
97
|
+
} else if (line.startsWith("event:")) {
|
|
98
|
+
eventType = line.substring("event:".length).trim();
|
|
99
|
+
} else if (line.startsWith("data:")) {
|
|
100
|
+
eventData = line.substring("data:".length).trim();
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
if (eventData) {
|
|
105
|
+
yield { type: eventType, data: eventData };
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export {
|
|
110
|
+
A2A_ERROR_CODE,
|
|
111
|
+
TaskNotFoundError,
|
|
112
|
+
TaskNotCancelableError,
|
|
113
|
+
PushNotificationNotSupportedError,
|
|
114
|
+
UnsupportedOperationError,
|
|
115
|
+
ContentTypeNotSupportedError,
|
|
116
|
+
InvalidAgentResponseError,
|
|
117
|
+
AuthenticatedExtendedCardNotConfiguredError,
|
|
118
|
+
SSE_HEADERS,
|
|
119
|
+
formatSSEEvent,
|
|
120
|
+
formatSSEErrorEvent,
|
|
121
|
+
parseSseStream
|
|
122
|
+
};
|