@a2a-js/sdk 0.3.7 → 0.3.8

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.
@@ -1,19 +1,30 @@
1
1
  import {
2
2
  A2A_ERROR_CODE,
3
+ AgentCard,
4
+ FromProto,
5
+ ListTaskPushNotificationConfigResponse,
3
6
  SSE_HEADERS,
7
+ SendMessageRequest,
8
+ SendMessageResponse,
9
+ StreamResponse,
10
+ Task,
11
+ TaskPushNotificationConfig,
12
+ ToProto,
4
13
  formatSSEErrorEvent,
5
14
  formatSSEEvent
6
- } from "../../chunk-SJNAG4AL.js";
15
+ } from "../../chunk-DHC2REQH.js";
7
16
  import {
8
17
  AGENT_CARD_PATH,
9
18
  HTTP_EXTENSION_HEADER
10
19
  } from "../../chunk-3QDLXHKS.js";
11
20
  import {
12
- A2AError,
13
21
  JsonRpcTransportHandler,
14
22
  ServerCallContext,
15
23
  UnauthenticatedUser
16
- } from "../../chunk-LTPINR5K.js";
24
+ } from "../../chunk-NUQQPJNY.js";
25
+ import {
26
+ A2AError
27
+ } from "../../chunk-UHZEIZLS.js";
17
28
  import {
18
29
  Extensions
19
30
  } from "../../chunk-ZX6KNMCP.js";
@@ -469,13 +480,16 @@ function restHandler(options) {
469
480
  res.setHeader(HTTP_EXTENSION_HEADER, Array.from(context.activatedExtensions));
470
481
  }
471
482
  };
472
- const sendResponse = (res, statusCode, context, body) => {
483
+ const sendResponse = (res, statusCode, context, body, responseType) => {
473
484
  setExtensionsHeader(res, context);
474
485
  res.status(statusCode);
475
486
  if (statusCode === HTTP_STATUS.NO_CONTENT) {
476
487
  res.end();
477
488
  } else {
478
- res.json(body);
489
+ if (!responseType) {
490
+ throw new Error("Bug: toJson serializer must be provided for non-204 responses.");
491
+ }
492
+ res.json(responseType.toJSON(body));
479
493
  }
480
494
  };
481
495
  const sendStreamResponse = async (res, stream, context) => {
@@ -496,10 +510,14 @@ function restHandler(options) {
496
510
  res.flushHeaders();
497
511
  try {
498
512
  if (!firstResult.done) {
499
- res.write(formatSSEEvent(firstResult.value));
513
+ const proto = ToProto.messageStreamResult(firstResult.value);
514
+ const result = StreamResponse.toJSON(proto);
515
+ res.write(formatSSEEvent(result));
500
516
  }
501
517
  for await (const event of { [Symbol.asyncIterator]: () => iterator }) {
502
- res.write(formatSSEEvent(event));
518
+ const proto = ToProto.messageStreamResult(event);
519
+ const result = StreamResponse.toJSON(proto);
520
+ res.write(formatSSEEvent(result));
503
521
  }
504
522
  } catch (streamError) {
505
523
  console.error("SSE streaming error:", streamError);
@@ -540,22 +558,34 @@ function restHandler(options) {
540
558
  asyncHandler(async (req, res) => {
541
559
  const context = await buildContext(req);
542
560
  const result = await restTransportHandler.getAuthenticatedExtendedAgentCard(context);
543
- sendResponse(res, HTTP_STATUS.OK, context, result);
561
+ const protoResult = ToProto.agentCard(result);
562
+ sendResponse(res, HTTP_STATUS.OK, context, protoResult, AgentCard);
544
563
  })
545
564
  );
546
565
  router.post(
547
566
  "/v1/message\\:send",
548
567
  asyncHandler(async (req, res) => {
549
568
  const context = await buildContext(req);
550
- const result = await restTransportHandler.sendMessage(req.body, context);
551
- sendResponse(res, HTTP_STATUS.CREATED, context, result);
569
+ const protoReq = SendMessageRequest.fromJSON(req.body);
570
+ const params = FromProto.messageSendParams(protoReq);
571
+ const result = await restTransportHandler.sendMessage(params, context);
572
+ const protoResult = ToProto.messageSendResult(result);
573
+ sendResponse(
574
+ res,
575
+ HTTP_STATUS.CREATED,
576
+ context,
577
+ protoResult,
578
+ SendMessageResponse
579
+ );
552
580
  })
553
581
  );
554
582
  router.post(
555
583
  "/v1/message\\:stream",
556
584
  asyncHandler(async (req, res) => {
557
585
  const context = await buildContext(req);
558
- const stream = await restTransportHandler.sendMessageStream(req.body, context);
586
+ const protoReq = SendMessageRequest.fromJSON(req.body);
587
+ const params = FromProto.messageSendParams(protoReq);
588
+ const stream = await restTransportHandler.sendMessageStream(params, context);
559
589
  await sendStreamResponse(res, stream, context);
560
590
  })
561
591
  );
@@ -566,9 +596,11 @@ function restHandler(options) {
566
596
  const result = await restTransportHandler.getTask(
567
597
  req.params.taskId,
568
598
  context,
569
- req.query.historyLength
599
+ //TODO: clarify for version 1.0.0 the format of the historyLength query parameter, and if history should always be added to the returned object
600
+ req.query.historyLength ?? req.query.history_length
570
601
  );
571
- sendResponse(res, HTTP_STATUS.OK, context, result);
602
+ const protoResult = ToProto.task(result);
603
+ sendResponse(res, HTTP_STATUS.OK, context, protoResult, Task);
572
604
  })
573
605
  );
574
606
  router.post(
@@ -576,7 +608,8 @@ function restHandler(options) {
576
608
  asyncHandler(async (req, res) => {
577
609
  const context = await buildContext(req);
578
610
  const result = await restTransportHandler.cancelTask(req.params.taskId, context);
579
- sendResponse(res, HTTP_STATUS.ACCEPTED, context, result);
611
+ const protoResult = ToProto.task(result);
612
+ sendResponse(res, HTTP_STATUS.ACCEPTED, context, protoResult, Task);
580
613
  })
581
614
  );
582
615
  router.post(
@@ -597,7 +630,14 @@ function restHandler(options) {
597
630
  task_id: req.params.taskId
598
631
  };
599
632
  const result = await restTransportHandler.setTaskPushNotificationConfig(config, context);
600
- sendResponse(res, HTTP_STATUS.CREATED, context, result);
633
+ const protoResult = ToProto.taskPushNotificationConfig(result);
634
+ sendResponse(
635
+ res,
636
+ HTTP_STATUS.CREATED,
637
+ context,
638
+ protoResult,
639
+ TaskPushNotificationConfig
640
+ );
601
641
  })
602
642
  );
603
643
  router.get(
@@ -608,7 +648,14 @@ function restHandler(options) {
608
648
  req.params.taskId,
609
649
  context
610
650
  );
611
- sendResponse(res, HTTP_STATUS.OK, context, result);
651
+ const protoResult = ToProto.listTaskPushNotificationConfig(result);
652
+ sendResponse(
653
+ res,
654
+ HTTP_STATUS.OK,
655
+ context,
656
+ protoResult,
657
+ ListTaskPushNotificationConfigResponse
658
+ );
612
659
  })
613
660
  );
614
661
  router.get(
@@ -620,7 +667,14 @@ function restHandler(options) {
620
667
  req.params.configId,
621
668
  context
622
669
  );
623
- sendResponse(res, HTTP_STATUS.OK, context, result);
670
+ const protoResult = ToProto.taskPushNotificationConfig(result);
671
+ sendResponse(
672
+ res,
673
+ HTTP_STATUS.OK,
674
+ context,
675
+ protoResult,
676
+ TaskPushNotificationConfig
677
+ );
624
678
  })
625
679
  );
626
680
  router.delete(
@@ -1,9 +1,11 @@
1
1
  import {
2
- A2AError,
3
2
  JsonRpcTransportHandler,
4
3
  ServerCallContext,
5
4
  UnauthenticatedUser
6
- } from "../chunk-LTPINR5K.js";
5
+ } from "../chunk-NUQQPJNY.js";
6
+ import {
7
+ A2AError
8
+ } from "../chunk-UHZEIZLS.js";
7
9
  import "../chunk-ZX6KNMCP.js";
8
10
 
9
11
  // src/server/agent_execution/request_context.ts
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@a2a-js/sdk",
3
- "version": "0.3.7",
3
+ "version": "0.3.8",
4
4
  "description": "Server & Client SDK for Agent2Agent protocol",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -13,6 +13,19 @@
13
13
  "main": "dist/index.js",
14
14
  "types": "dist/index.d.ts",
15
15
  "type": "module",
16
+ "typesVersions": {
17
+ "*": {
18
+ "server": [
19
+ "./dist/server/index.d.ts"
20
+ ],
21
+ "server/express": [
22
+ "./dist/server/express/index.d.ts"
23
+ ],
24
+ "client": [
25
+ "./dist/client/index.d.ts"
26
+ ]
27
+ }
28
+ },
16
29
  "exports": {
17
30
  ".": {
18
31
  "types": "./dist/index.d.ts",
@@ -1,122 +0,0 @@
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
- };