@mailmodo/a2a 0.3.3 → 0.3.5
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 +272 -138
- package/dist/a2a_request_handler-DPkhsCMt.d.mts +37 -0
- package/dist/a2a_request_handler-DQfg1Q-R.d.ts +37 -0
- package/dist/auth-handler-DVLcl8yj.d.mts +209 -0
- package/dist/auth-handler-Gzpf3xHC.d.ts +209 -0
- package/dist/chunk-HZFUOBJQ.mjs +198 -0
- package/dist/chunk-LIEYEFQG.mjs +879 -0
- package/dist/chunk-LVD4GF26.mjs +262 -0
- package/dist/chunk-PHP7LM4Y.mjs +8 -0
- package/dist/chunk-UBRSFN2J.mjs +776 -0
- package/dist/client/index.d.mts +312 -0
- package/dist/client/index.d.ts +312 -0
- package/dist/client/index.js +1158 -0
- package/dist/client/index.mjs +367 -0
- package/dist/error-DExKs0Q3.d.mts +233 -0
- package/dist/error-j1vYKII2.d.ts +233 -0
- package/dist/index.d.mts +14 -2739
- package/dist/index.d.ts +14 -2739
- package/dist/index.js +1605 -1158
- package/dist/index.mjs +29 -1612
- package/dist/server/express/index.d.mts +25 -0
- package/dist/server/express/index.d.ts +25 -0
- package/dist/server/express/index.js +468 -0
- package/dist/server/express/index.mjs +10 -0
- package/dist/server/index.d.mts +26 -0
- package/dist/server/index.d.ts +26 -0
- package/dist/server/index.js +1173 -0
- package/dist/server/index.mjs +32 -0
- package/dist/types-Due_Cv6t.d.mts +2550 -0
- package/dist/types-Due_Cv6t.d.ts +2550 -0
- package/package.json +18 -11
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
// src/server/error.ts
|
|
2
|
+
var A2AError = class _A2AError extends Error {
|
|
3
|
+
code;
|
|
4
|
+
data;
|
|
5
|
+
taskId;
|
|
6
|
+
// Optional task ID context
|
|
7
|
+
constructor(code, message, data, taskId) {
|
|
8
|
+
super(message);
|
|
9
|
+
this.name = "A2AError";
|
|
10
|
+
this.code = code;
|
|
11
|
+
this.data = data;
|
|
12
|
+
this.taskId = taskId;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Formats the error into a standard JSON-RPC error object structure.
|
|
16
|
+
*/
|
|
17
|
+
toJSONRPCError() {
|
|
18
|
+
const errorObject = {
|
|
19
|
+
code: this.code,
|
|
20
|
+
message: this.message
|
|
21
|
+
};
|
|
22
|
+
if (this.data !== void 0) {
|
|
23
|
+
errorObject.data = this.data;
|
|
24
|
+
}
|
|
25
|
+
return errorObject;
|
|
26
|
+
}
|
|
27
|
+
// Static factory methods for common errors
|
|
28
|
+
static parseError(message, data) {
|
|
29
|
+
return new _A2AError(-32700, message, data);
|
|
30
|
+
}
|
|
31
|
+
static invalidRequest(message, data) {
|
|
32
|
+
return new _A2AError(-32600, message, data);
|
|
33
|
+
}
|
|
34
|
+
static methodNotFound(method) {
|
|
35
|
+
return new _A2AError(-32601, `Method not found: ${method}`);
|
|
36
|
+
}
|
|
37
|
+
static invalidParams(message, data) {
|
|
38
|
+
return new _A2AError(-32602, message, data);
|
|
39
|
+
}
|
|
40
|
+
static internalError(message, data) {
|
|
41
|
+
return new _A2AError(-32603, message, data);
|
|
42
|
+
}
|
|
43
|
+
static taskNotFound(taskId) {
|
|
44
|
+
return new _A2AError(-32001, `Task not found: ${taskId}`, void 0, taskId);
|
|
45
|
+
}
|
|
46
|
+
static taskNotCancelable(taskId) {
|
|
47
|
+
return new _A2AError(-32002, `Task not cancelable: ${taskId}`, void 0, taskId);
|
|
48
|
+
}
|
|
49
|
+
static pushNotificationNotSupported() {
|
|
50
|
+
return new _A2AError(-32003, "Push Notification is not supported");
|
|
51
|
+
}
|
|
52
|
+
static unsupportedOperation(operation) {
|
|
53
|
+
return new _A2AError(-32004, `Unsupported operation: ${operation}`);
|
|
54
|
+
}
|
|
55
|
+
static authenticatedExtendedCardNotConfigured() {
|
|
56
|
+
return new _A2AError(-32007, `Extended card not configured.`);
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
// src/server/context.ts
|
|
61
|
+
var ServerCallContext = class {
|
|
62
|
+
_requestedExtensions;
|
|
63
|
+
_user;
|
|
64
|
+
_activatedExtensions;
|
|
65
|
+
constructor(requestedExtensions, user) {
|
|
66
|
+
this._requestedExtensions = requestedExtensions;
|
|
67
|
+
this._user = user;
|
|
68
|
+
}
|
|
69
|
+
get user() {
|
|
70
|
+
return this._user;
|
|
71
|
+
}
|
|
72
|
+
get activatedExtensions() {
|
|
73
|
+
return this._activatedExtensions;
|
|
74
|
+
}
|
|
75
|
+
get requestedExtensions() {
|
|
76
|
+
return this._requestedExtensions;
|
|
77
|
+
}
|
|
78
|
+
addActivatedExtension(uri) {
|
|
79
|
+
if (this._requestedExtensions?.has(uri)) {
|
|
80
|
+
if (!this._activatedExtensions) {
|
|
81
|
+
this._activatedExtensions = /* @__PURE__ */ new Set();
|
|
82
|
+
}
|
|
83
|
+
this._activatedExtensions.add(uri);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
// src/server/transports/jsonrpc_transport_handler.ts
|
|
89
|
+
var JsonRpcTransportHandler = class {
|
|
90
|
+
requestHandler;
|
|
91
|
+
constructor(requestHandler) {
|
|
92
|
+
this.requestHandler = requestHandler;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Handles an incoming JSON-RPC request.
|
|
96
|
+
* For streaming methods, it returns an AsyncGenerator of JSONRPCResult.
|
|
97
|
+
* For non-streaming methods, it returns a Promise of a single JSONRPCMessage (Result or ErrorResponse).
|
|
98
|
+
*/
|
|
99
|
+
async handle(requestBody, context) {
|
|
100
|
+
let rpcRequest;
|
|
101
|
+
try {
|
|
102
|
+
if (typeof requestBody === "string") {
|
|
103
|
+
rpcRequest = JSON.parse(requestBody);
|
|
104
|
+
} else if (typeof requestBody === "object" && requestBody !== null) {
|
|
105
|
+
rpcRequest = requestBody;
|
|
106
|
+
} else {
|
|
107
|
+
throw A2AError.parseError("Invalid request body type.");
|
|
108
|
+
}
|
|
109
|
+
if (!this.isRequestValid(rpcRequest)) {
|
|
110
|
+
throw A2AError.invalidRequest("Invalid JSON-RPC Request.");
|
|
111
|
+
}
|
|
112
|
+
} catch (error) {
|
|
113
|
+
const a2aError = error instanceof A2AError ? error : A2AError.parseError(
|
|
114
|
+
error instanceof SyntaxError && error.message || "Failed to parse JSON request."
|
|
115
|
+
);
|
|
116
|
+
return {
|
|
117
|
+
jsonrpc: "2.0",
|
|
118
|
+
id: rpcRequest?.id !== void 0 ? rpcRequest.id : null,
|
|
119
|
+
error: a2aError.toJSONRPCError()
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
const { method, id: requestId = null } = rpcRequest;
|
|
123
|
+
try {
|
|
124
|
+
if (method !== "agent/getAuthenticatedExtendedCard" && !this.paramsAreValid(rpcRequest.params)) {
|
|
125
|
+
throw A2AError.invalidParams(`Invalid method parameters.`);
|
|
126
|
+
}
|
|
127
|
+
if (method === "message/stream" || method === "tasks/resubscribe") {
|
|
128
|
+
const params = rpcRequest.params;
|
|
129
|
+
const agentCard = await this.requestHandler.getAgentCard();
|
|
130
|
+
if (!agentCard.capabilities.streaming) {
|
|
131
|
+
throw A2AError.unsupportedOperation(`Method ${method} requires streaming capability.`);
|
|
132
|
+
}
|
|
133
|
+
const agentEventStream = method === "message/stream" ? this.requestHandler.sendMessageStream(params, context) : this.requestHandler.resubscribe(params, context);
|
|
134
|
+
return (async function* jsonRpcEventStream() {
|
|
135
|
+
try {
|
|
136
|
+
for await (const event of agentEventStream) {
|
|
137
|
+
yield {
|
|
138
|
+
jsonrpc: "2.0",
|
|
139
|
+
id: requestId,
|
|
140
|
+
// Use the original request ID for all streamed responses
|
|
141
|
+
result: event
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
} catch (streamError) {
|
|
145
|
+
console.error(
|
|
146
|
+
`Error in agent event stream for ${method} (request ${requestId}):`,
|
|
147
|
+
streamError
|
|
148
|
+
);
|
|
149
|
+
throw streamError;
|
|
150
|
+
}
|
|
151
|
+
})();
|
|
152
|
+
} else {
|
|
153
|
+
let result;
|
|
154
|
+
switch (method) {
|
|
155
|
+
case "message/send":
|
|
156
|
+
result = await this.requestHandler.sendMessage(rpcRequest.params, context);
|
|
157
|
+
break;
|
|
158
|
+
case "tasks/get":
|
|
159
|
+
result = await this.requestHandler.getTask(rpcRequest.params, context);
|
|
160
|
+
break;
|
|
161
|
+
case "tasks/cancel":
|
|
162
|
+
result = await this.requestHandler.cancelTask(rpcRequest.params, context);
|
|
163
|
+
break;
|
|
164
|
+
case "tasks/pushNotificationConfig/set":
|
|
165
|
+
result = await this.requestHandler.setTaskPushNotificationConfig(
|
|
166
|
+
rpcRequest.params,
|
|
167
|
+
context
|
|
168
|
+
);
|
|
169
|
+
break;
|
|
170
|
+
case "tasks/pushNotificationConfig/get":
|
|
171
|
+
result = await this.requestHandler.getTaskPushNotificationConfig(
|
|
172
|
+
rpcRequest.params,
|
|
173
|
+
context
|
|
174
|
+
);
|
|
175
|
+
break;
|
|
176
|
+
case "tasks/pushNotificationConfig/delete":
|
|
177
|
+
await this.requestHandler.deleteTaskPushNotificationConfig(rpcRequest.params, context);
|
|
178
|
+
result = null;
|
|
179
|
+
break;
|
|
180
|
+
case "tasks/pushNotificationConfig/list":
|
|
181
|
+
result = await this.requestHandler.listTaskPushNotificationConfigs(
|
|
182
|
+
rpcRequest.params,
|
|
183
|
+
context
|
|
184
|
+
);
|
|
185
|
+
break;
|
|
186
|
+
case "agent/getAuthenticatedExtendedCard":
|
|
187
|
+
result = await this.requestHandler.getAuthenticatedExtendedAgentCard(context);
|
|
188
|
+
break;
|
|
189
|
+
default:
|
|
190
|
+
throw A2AError.methodNotFound(method);
|
|
191
|
+
}
|
|
192
|
+
return {
|
|
193
|
+
jsonrpc: "2.0",
|
|
194
|
+
id: requestId,
|
|
195
|
+
result
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
} catch (error) {
|
|
199
|
+
let a2aError;
|
|
200
|
+
if (error instanceof A2AError) {
|
|
201
|
+
a2aError = error;
|
|
202
|
+
} else {
|
|
203
|
+
a2aError = A2AError.internalError(
|
|
204
|
+
error instanceof Error && error.message || "An unexpected error occurred."
|
|
205
|
+
);
|
|
206
|
+
}
|
|
207
|
+
return {
|
|
208
|
+
jsonrpc: "2.0",
|
|
209
|
+
id: requestId,
|
|
210
|
+
error: a2aError.toJSONRPCError()
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
// Validates the basic structure of a JSON-RPC request
|
|
215
|
+
isRequestValid(rpcRequest) {
|
|
216
|
+
if (rpcRequest.jsonrpc !== "2.0") {
|
|
217
|
+
return false;
|
|
218
|
+
}
|
|
219
|
+
if ("id" in rpcRequest) {
|
|
220
|
+
const id = rpcRequest.id;
|
|
221
|
+
const isString = typeof id === "string";
|
|
222
|
+
const isInteger = typeof id === "number" && Number.isInteger(id);
|
|
223
|
+
const isNull = id === null;
|
|
224
|
+
if (!isString && !isInteger && !isNull) {
|
|
225
|
+
return false;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
if (!rpcRequest.method || typeof rpcRequest.method !== "string") {
|
|
229
|
+
return false;
|
|
230
|
+
}
|
|
231
|
+
return true;
|
|
232
|
+
}
|
|
233
|
+
// Validates that params is an object with non-empty string keys
|
|
234
|
+
paramsAreValid(params) {
|
|
235
|
+
if (typeof params !== "object" || params === null || Array.isArray(params)) {
|
|
236
|
+
return false;
|
|
237
|
+
}
|
|
238
|
+
for (const key of Object.keys(params)) {
|
|
239
|
+
if (key === "") {
|
|
240
|
+
return false;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
return true;
|
|
244
|
+
}
|
|
245
|
+
};
|
|
246
|
+
|
|
247
|
+
// src/server/authentication/user.ts
|
|
248
|
+
var UnauthenticatedUser = class {
|
|
249
|
+
get isAuthenticated() {
|
|
250
|
+
return false;
|
|
251
|
+
}
|
|
252
|
+
get userName() {
|
|
253
|
+
return "";
|
|
254
|
+
}
|
|
255
|
+
};
|
|
256
|
+
|
|
257
|
+
export {
|
|
258
|
+
A2AError,
|
|
259
|
+
ServerCallContext,
|
|
260
|
+
JsonRpcTransportHandler,
|
|
261
|
+
UnauthenticatedUser
|
|
262
|
+
};
|