@a2a-js/sdk 0.3.8 → 0.3.9

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.
@@ -0,0 +1,42 @@
1
+ import * as grpc from '@grpc/grpc-js';
2
+ import { c as Transport, R as RequestOptions, S as SendMessageResult, A as A2AStreamEventData, T as TransportFactory, a as TransportProtocolName } from '../../../core-Ci-lR0jz.cjs';
3
+ import { ae as AgentCard, x as MessageSendParams, $ as TaskPushNotificationConfig, a3 as GetTaskPushNotificationConfigParams, a7 as ListTaskPushNotificationConfigParams, a9 as DeleteTaskPushNotificationConfigParams, X as TaskQueryParams, ay as Task, Z as TaskIdParams } from '../../../extensions-DvruCIzw.cjs';
4
+
5
+ interface GrpcTransportOptions {
6
+ endpoint: string;
7
+ grpcChannelCredentials?: grpc.ChannelCredentials;
8
+ grpcCallOptions?: Partial<grpc.CallOptions>;
9
+ }
10
+ declare class GrpcTransport implements Transport {
11
+ private readonly grpcCallOptions?;
12
+ private readonly grpcClient;
13
+ constructor(options: GrpcTransportOptions);
14
+ getExtendedAgentCard(options?: RequestOptions): Promise<AgentCard>;
15
+ sendMessage(params: MessageSendParams, options?: RequestOptions): Promise<SendMessageResult>;
16
+ sendMessageStream(params: MessageSendParams, options?: RequestOptions): AsyncGenerator<A2AStreamEventData, void, undefined>;
17
+ setTaskPushNotificationConfig(params: TaskPushNotificationConfig, options?: RequestOptions): Promise<TaskPushNotificationConfig>;
18
+ getTaskPushNotificationConfig(params: GetTaskPushNotificationConfigParams, options?: RequestOptions): Promise<TaskPushNotificationConfig>;
19
+ listTaskPushNotificationConfig(params: ListTaskPushNotificationConfigParams, options?: RequestOptions): Promise<TaskPushNotificationConfig[]>;
20
+ deleteTaskPushNotificationConfig(params: DeleteTaskPushNotificationConfigParams, options?: RequestOptions): Promise<void>;
21
+ getTask(params: TaskQueryParams, options?: RequestOptions): Promise<Task>;
22
+ cancelTask(params: TaskIdParams, options?: RequestOptions): Promise<Task>;
23
+ resubscribeTask(params: TaskIdParams, options?: RequestOptions): AsyncGenerator<A2AStreamEventData, void, undefined>;
24
+ private _sendGrpcRequest;
25
+ private _sendGrpcStreamingRequest;
26
+ private isServiceError;
27
+ private _buildMetadata;
28
+ private static mapToError;
29
+ }
30
+ declare class GrpcTransportFactoryOptions {
31
+ grpcChannelCredentials?: grpc.ChannelCredentials;
32
+ grpcCallOptions?: Partial<grpc.CallOptions>;
33
+ }
34
+ declare class GrpcTransportFactory implements TransportFactory {
35
+ private readonly options?;
36
+ static readonly name: TransportProtocolName;
37
+ constructor(options?: GrpcTransportFactoryOptions);
38
+ get protocolName(): string;
39
+ create(url: string, _agentCard: AgentCard): Promise<Transport>;
40
+ }
41
+
42
+ export { GrpcTransport, GrpcTransportFactory, type GrpcTransportOptions };
@@ -0,0 +1,42 @@
1
+ import * as grpc from '@grpc/grpc-js';
2
+ import { c as Transport, R as RequestOptions, S as SendMessageResult, A as A2AStreamEventData, T as TransportFactory, a as TransportProtocolName } from '../../../core-BAzQJfA2.js';
3
+ import { ae as AgentCard, x as MessageSendParams, $ as TaskPushNotificationConfig, a3 as GetTaskPushNotificationConfigParams, a7 as ListTaskPushNotificationConfigParams, a9 as DeleteTaskPushNotificationConfigParams, X as TaskQueryParams, ay as Task, Z as TaskIdParams } from '../../../extensions-DvruCIzw.js';
4
+
5
+ interface GrpcTransportOptions {
6
+ endpoint: string;
7
+ grpcChannelCredentials?: grpc.ChannelCredentials;
8
+ grpcCallOptions?: Partial<grpc.CallOptions>;
9
+ }
10
+ declare class GrpcTransport implements Transport {
11
+ private readonly grpcCallOptions?;
12
+ private readonly grpcClient;
13
+ constructor(options: GrpcTransportOptions);
14
+ getExtendedAgentCard(options?: RequestOptions): Promise<AgentCard>;
15
+ sendMessage(params: MessageSendParams, options?: RequestOptions): Promise<SendMessageResult>;
16
+ sendMessageStream(params: MessageSendParams, options?: RequestOptions): AsyncGenerator<A2AStreamEventData, void, undefined>;
17
+ setTaskPushNotificationConfig(params: TaskPushNotificationConfig, options?: RequestOptions): Promise<TaskPushNotificationConfig>;
18
+ getTaskPushNotificationConfig(params: GetTaskPushNotificationConfigParams, options?: RequestOptions): Promise<TaskPushNotificationConfig>;
19
+ listTaskPushNotificationConfig(params: ListTaskPushNotificationConfigParams, options?: RequestOptions): Promise<TaskPushNotificationConfig[]>;
20
+ deleteTaskPushNotificationConfig(params: DeleteTaskPushNotificationConfigParams, options?: RequestOptions): Promise<void>;
21
+ getTask(params: TaskQueryParams, options?: RequestOptions): Promise<Task>;
22
+ cancelTask(params: TaskIdParams, options?: RequestOptions): Promise<Task>;
23
+ resubscribeTask(params: TaskIdParams, options?: RequestOptions): AsyncGenerator<A2AStreamEventData, void, undefined>;
24
+ private _sendGrpcRequest;
25
+ private _sendGrpcStreamingRequest;
26
+ private isServiceError;
27
+ private _buildMetadata;
28
+ private static mapToError;
29
+ }
30
+ declare class GrpcTransportFactoryOptions {
31
+ grpcChannelCredentials?: grpc.ChannelCredentials;
32
+ grpcCallOptions?: Partial<grpc.CallOptions>;
33
+ }
34
+ declare class GrpcTransportFactory implements TransportFactory {
35
+ private readonly options?;
36
+ static readonly name: TransportProtocolName;
37
+ constructor(options?: GrpcTransportFactoryOptions);
38
+ get protocolName(): string;
39
+ create(url: string, _agentCard: AgentCard): Promise<Transport>;
40
+ }
41
+
42
+ export { GrpcTransport, GrpcTransportFactory, type GrpcTransportOptions };
@@ -0,0 +1,262 @@
1
+ import {
2
+ AuthenticatedExtendedCardNotConfiguredError,
3
+ PushNotificationNotSupportedError,
4
+ TaskNotCancelableError,
5
+ TaskNotFoundError,
6
+ UnsupportedOperationError
7
+ } from "../../../chunk-EGOOH5HP.js";
8
+ import {
9
+ A2AServiceClient
10
+ } from "../../../chunk-QQCCX2KH.js";
11
+ import {
12
+ FromProto,
13
+ ToProto
14
+ } from "../../../chunk-U3QAVT4H.js";
15
+ import "../../../chunk-UHZEIZLS.js";
16
+
17
+ // src/client/transports/grpc/grpc_transport.ts
18
+ import * as grpc from "@grpc/grpc-js";
19
+ var GrpcTransport = class _GrpcTransport {
20
+ grpcCallOptions;
21
+ grpcClient;
22
+ constructor(options) {
23
+ this.grpcCallOptions = options.grpcCallOptions;
24
+ this.grpcClient = new A2AServiceClient(
25
+ options.endpoint,
26
+ options.grpcChannelCredentials ?? grpc.credentials.createInsecure()
27
+ );
28
+ }
29
+ async getExtendedAgentCard(options) {
30
+ const rpcResponse = await this._sendGrpcRequest(
31
+ "getAgentCard",
32
+ void 0,
33
+ options,
34
+ this.grpcClient.getAgentCard.bind(this.grpcClient),
35
+ ToProto.getAgentCardRequest,
36
+ FromProto.agentCard
37
+ );
38
+ return rpcResponse;
39
+ }
40
+ async sendMessage(params, options) {
41
+ const rpcResponse = await this._sendGrpcRequest(
42
+ "sendMessage",
43
+ params,
44
+ options,
45
+ this.grpcClient.sendMessage.bind(this.grpcClient),
46
+ ToProto.messageSendParams,
47
+ FromProto.sendMessageResult
48
+ );
49
+ return rpcResponse;
50
+ }
51
+ async *sendMessageStream(params, options) {
52
+ yield* this._sendGrpcStreamingRequest(
53
+ "sendStreamingMessage",
54
+ params,
55
+ options,
56
+ this.grpcClient.sendStreamingMessage.bind(this.grpcClient),
57
+ ToProto.messageSendParams
58
+ );
59
+ }
60
+ async setTaskPushNotificationConfig(params, options) {
61
+ const rpcResponse = await this._sendGrpcRequest(
62
+ "createTaskPushNotificationConfig",
63
+ params,
64
+ options,
65
+ this.grpcClient.createTaskPushNotificationConfig.bind(this.grpcClient),
66
+ ToProto.taskPushNotificationConfigCreate,
67
+ FromProto.taskPushNotificationConfig
68
+ );
69
+ return rpcResponse;
70
+ }
71
+ async getTaskPushNotificationConfig(params, options) {
72
+ const rpcResponse = await this._sendGrpcRequest(
73
+ "getTaskPushNotificationConfig",
74
+ params,
75
+ options,
76
+ this.grpcClient.getTaskPushNotificationConfig.bind(this.grpcClient),
77
+ ToProto.getTaskPushNotificationConfigParams,
78
+ FromProto.taskPushNotificationConfig
79
+ );
80
+ return rpcResponse;
81
+ }
82
+ async listTaskPushNotificationConfig(params, options) {
83
+ const rpcResponse = await this._sendGrpcRequest(
84
+ "listTaskPushNotificationConfig",
85
+ params,
86
+ options,
87
+ this.grpcClient.listTaskPushNotificationConfig.bind(this.grpcClient),
88
+ ToProto.listTaskPushNotificationConfigParams,
89
+ FromProto.listTaskPushNotificationConfig
90
+ );
91
+ return rpcResponse;
92
+ }
93
+ async deleteTaskPushNotificationConfig(params, options) {
94
+ await this._sendGrpcRequest(
95
+ "deleteTaskPushNotificationConfig",
96
+ params,
97
+ options,
98
+ this.grpcClient.deleteTaskPushNotificationConfig.bind(this.grpcClient),
99
+ ToProto.deleteTaskPushNotificationConfigParams,
100
+ () => {
101
+ }
102
+ );
103
+ }
104
+ async getTask(params, options) {
105
+ const rpcResponse = await this._sendGrpcRequest(
106
+ "getTask",
107
+ params,
108
+ options,
109
+ this.grpcClient.getTask.bind(this.grpcClient),
110
+ ToProto.taskQueryParams,
111
+ FromProto.task
112
+ );
113
+ return rpcResponse;
114
+ }
115
+ async cancelTask(params, options) {
116
+ const rpcResponse = await this._sendGrpcRequest(
117
+ "cancelTask",
118
+ params,
119
+ options,
120
+ this.grpcClient.cancelTask.bind(this.grpcClient),
121
+ ToProto.cancelTaskRequest,
122
+ FromProto.task
123
+ );
124
+ return rpcResponse;
125
+ }
126
+ async *resubscribeTask(params, options) {
127
+ yield* this._sendGrpcStreamingRequest(
128
+ "taskSubscription",
129
+ params,
130
+ options,
131
+ this.grpcClient.taskSubscription.bind(this.grpcClient),
132
+ ToProto.taskIdParams
133
+ );
134
+ }
135
+ async _sendGrpcRequest(method, params, options, call, parser, converter) {
136
+ return new Promise((resolve, reject) => {
137
+ let onAbort;
138
+ const clientCall = call(
139
+ parser(params),
140
+ this._buildMetadata(options),
141
+ this.grpcCallOptions ?? {},
142
+ (error, response) => {
143
+ if (options?.signal && onAbort) {
144
+ options.signal.removeEventListener("abort", onAbort);
145
+ }
146
+ if (error) {
147
+ return reject(_GrpcTransport.mapToError(error, method));
148
+ }
149
+ resolve(converter(response));
150
+ }
151
+ );
152
+ if (options?.signal) {
153
+ if (options.signal.aborted) {
154
+ clientCall.cancel();
155
+ } else {
156
+ onAbort = () => clientCall.cancel();
157
+ options.signal.addEventListener("abort", onAbort);
158
+ }
159
+ }
160
+ });
161
+ }
162
+ async *_sendGrpcStreamingRequest(method, params, options, call, parser) {
163
+ const streamResponse = call(
164
+ parser(params),
165
+ this._buildMetadata(options),
166
+ this.grpcCallOptions ?? {}
167
+ );
168
+ let onAbort;
169
+ if (options?.signal) {
170
+ if (options.signal.aborted) {
171
+ streamResponse.cancel();
172
+ } else {
173
+ onAbort = () => streamResponse.cancel();
174
+ options.signal.addEventListener("abort", onAbort);
175
+ }
176
+ }
177
+ try {
178
+ for await (const response of streamResponse) {
179
+ yield FromProto.messageStreamResult(response);
180
+ }
181
+ } catch (error) {
182
+ if (this.isServiceError(error)) {
183
+ throw _GrpcTransport.mapToError(error, method);
184
+ } else {
185
+ throw new Error(`GRPC error for ${String(method)}!`, {
186
+ cause: error
187
+ });
188
+ }
189
+ } finally {
190
+ if (options?.signal && onAbort) {
191
+ options.signal.removeEventListener("abort", onAbort);
192
+ }
193
+ streamResponse.cancel();
194
+ }
195
+ }
196
+ isServiceError(error) {
197
+ return typeof error === "object" && error !== null && "code" in error;
198
+ }
199
+ _buildMetadata(options) {
200
+ const metadata = new grpc.Metadata();
201
+ if (options?.serviceParameters) {
202
+ for (const [key, value] of Object.entries(options.serviceParameters)) {
203
+ metadata.set(key, value);
204
+ }
205
+ }
206
+ return metadata;
207
+ }
208
+ // TODO: the logic of mapToError will be removed in v1.0.0 with the enriched error model (https://a2a-protocol.org/latest/specification/#106-error-handling)
209
+ static mapToError(error, method) {
210
+ switch (error.code) {
211
+ case grpc.status.NOT_FOUND:
212
+ return new TaskNotFoundError(error.details);
213
+ case grpc.status.FAILED_PRECONDITION:
214
+ if (method === "cancelTask") {
215
+ return new TaskNotCancelableError(error.details);
216
+ }
217
+ if (method === "getAgentCard") {
218
+ return new AuthenticatedExtendedCardNotConfiguredError(error.details);
219
+ }
220
+ break;
221
+ case grpc.status.UNIMPLEMENTED:
222
+ if ([
223
+ "getTaskPushNotificationConfig",
224
+ "createTaskPushNotificationConfig",
225
+ "deleteTaskPushNotificationConfig",
226
+ "listTaskPushNotificationConfig"
227
+ ].includes(method)) {
228
+ return new PushNotificationNotSupportedError(error.details);
229
+ }
230
+ if (["getAgentCard", "taskSubscription"].includes(method)) {
231
+ return new UnsupportedOperationError(error.details);
232
+ }
233
+ break;
234
+ //TODO: add case for grpc.status.INVALID_ARGUMENT and grpc.status.INTERNAL (the respective a2a errors are not implemented yet)
235
+ default:
236
+ break;
237
+ }
238
+ return new Error(`GRPC error for ${String(method)}! ${error.code} ${error.details}`, {
239
+ cause: error
240
+ });
241
+ }
242
+ };
243
+ var GrpcTransportFactory = class _GrpcTransportFactory {
244
+ constructor(options) {
245
+ this.options = options;
246
+ }
247
+ static name = "GRPC";
248
+ get protocolName() {
249
+ return _GrpcTransportFactory.name;
250
+ }
251
+ async create(url, _agentCard) {
252
+ return new GrpcTransport({
253
+ endpoint: url,
254
+ grpcChannelCredentials: this.options?.grpcChannelCredentials,
255
+ grpcCallOptions: this.options?.grpcCallOptions
256
+ });
257
+ }
258
+ };
259
+ export {
260
+ GrpcTransport,
261
+ GrpcTransportFactory
262
+ };