@mastra/client-js 0.0.0-switch-to-core-20250424015131 → 0.0.0-taofeeq-fix-tool-call-showing-after-message-20250806184630

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.
Files changed (84) hide show
  1. package/.turbo/turbo-build.log +18 -0
  2. package/CHANGELOG.md +1356 -2
  3. package/LICENSE.md +11 -42
  4. package/README.md +2 -1
  5. package/dist/adapters/agui.d.ts +23 -0
  6. package/dist/adapters/agui.d.ts.map +1 -0
  7. package/dist/client.d.ts +265 -0
  8. package/dist/client.d.ts.map +1 -0
  9. package/dist/example.d.ts +2 -0
  10. package/dist/example.d.ts.map +1 -0
  11. package/dist/index.cjs +1785 -82
  12. package/dist/index.cjs.map +1 -0
  13. package/dist/index.d.ts +4 -585
  14. package/dist/index.d.ts.map +1 -0
  15. package/dist/index.js +1785 -86
  16. package/dist/index.js.map +1 -0
  17. package/dist/resources/a2a.d.ts +44 -0
  18. package/dist/resources/a2a.d.ts.map +1 -0
  19. package/dist/resources/agent.d.ts +112 -0
  20. package/dist/resources/agent.d.ts.map +1 -0
  21. package/dist/resources/base.d.ts +13 -0
  22. package/dist/resources/base.d.ts.map +1 -0
  23. package/dist/resources/index.d.ts +11 -0
  24. package/dist/resources/index.d.ts.map +1 -0
  25. package/dist/resources/legacy-workflow.d.ts +87 -0
  26. package/dist/resources/legacy-workflow.d.ts.map +1 -0
  27. package/dist/resources/mcp-tool.d.ts +27 -0
  28. package/dist/resources/mcp-tool.d.ts.map +1 -0
  29. package/dist/resources/memory-thread.d.ts +53 -0
  30. package/dist/resources/memory-thread.d.ts.map +1 -0
  31. package/dist/resources/network-memory-thread.d.ts +47 -0
  32. package/dist/resources/network-memory-thread.d.ts.map +1 -0
  33. package/dist/resources/network.d.ts +30 -0
  34. package/dist/resources/network.d.ts.map +1 -0
  35. package/dist/resources/tool.d.ts +23 -0
  36. package/dist/resources/tool.d.ts.map +1 -0
  37. package/dist/resources/vNextNetwork.d.ts +42 -0
  38. package/dist/resources/vNextNetwork.d.ts.map +1 -0
  39. package/dist/resources/vector.d.ts +48 -0
  40. package/dist/resources/vector.d.ts.map +1 -0
  41. package/dist/resources/workflow.d.ts +154 -0
  42. package/dist/resources/workflow.d.ts.map +1 -0
  43. package/dist/types.d.ts +422 -0
  44. package/dist/types.d.ts.map +1 -0
  45. package/dist/utils/index.d.ts +3 -0
  46. package/dist/utils/index.d.ts.map +1 -0
  47. package/dist/utils/process-client-tools.d.ts +3 -0
  48. package/dist/utils/process-client-tools.d.ts.map +1 -0
  49. package/dist/utils/zod-to-json-schema.d.ts +105 -0
  50. package/dist/utils/zod-to-json-schema.d.ts.map +1 -0
  51. package/integration-tests/agui-adapter.test.ts +122 -0
  52. package/integration-tests/package.json +18 -0
  53. package/integration-tests/src/mastra/index.ts +35 -0
  54. package/integration-tests/vitest.config.ts +9 -0
  55. package/package.json +32 -19
  56. package/src/adapters/agui.test.ts +322 -0
  57. package/src/adapters/agui.ts +257 -0
  58. package/src/client.ts +414 -5
  59. package/src/example.ts +59 -29
  60. package/src/index.test.ts +526 -10
  61. package/src/index.ts +1 -0
  62. package/src/resources/a2a.ts +88 -0
  63. package/src/resources/agent.ts +629 -49
  64. package/src/resources/base.ts +8 -2
  65. package/src/resources/index.ts +4 -1
  66. package/src/resources/legacy-workflow.ts +242 -0
  67. package/src/resources/mcp-tool.ts +48 -0
  68. package/src/resources/memory-thread.test.ts +285 -0
  69. package/src/resources/memory-thread.ts +49 -3
  70. package/src/resources/network-memory-thread.test.ts +269 -0
  71. package/src/resources/network-memory-thread.ts +81 -0
  72. package/src/resources/network.ts +11 -17
  73. package/src/resources/tool.ts +16 -3
  74. package/src/resources/vNextNetwork.ts +194 -0
  75. package/src/resources/workflow.ts +289 -94
  76. package/src/types.ts +298 -20
  77. package/src/utils/index.ts +11 -0
  78. package/src/utils/process-client-tools.ts +32 -0
  79. package/src/utils/zod-to-json-schema.ts +10 -0
  80. package/src/v2-messages.test.ts +180 -0
  81. package/tsconfig.build.json +9 -0
  82. package/tsconfig.json +1 -1
  83. package/tsup.config.ts +17 -0
  84. package/dist/index.d.cts +0 -585
package/src/index.test.ts CHANGED
@@ -1,7 +1,8 @@
1
- import type { MessageType } from '@mastra/core';
2
1
  import { describe, expect, beforeEach, it, vi } from 'vitest';
3
-
4
2
  import { MastraClient } from './client';
3
+ import type { McpServerListResponse } from './types';
4
+ import type { ServerDetailInfo } from '@mastra/core/mcp';
5
+ import { ScoringEntityType, ScoringSource } from '@mastra/core/scores';
5
6
 
6
7
  // Mock fetch globally
7
8
  global.fetch = vi.fn();
@@ -28,7 +29,13 @@ describe('MastraClient Resources', () => {
28
29
  } else {
29
30
  responseBody = new ReadableStream({
30
31
  start(controller) {
31
- controller.enqueue(new TextEncoder().encode(JSON.stringify(data)));
32
+ if (typeof data === 'string') {
33
+ controller.enqueue(new TextEncoder().encode(data));
34
+ } else if (typeof data === 'object' && data !== null) {
35
+ controller.enqueue(new TextEncoder().encode(JSON.stringify(data)));
36
+ } else {
37
+ controller.enqueue(new TextEncoder().encode(String(data)));
38
+ }
32
39
  controller.close();
33
40
  },
34
41
  });
@@ -237,6 +244,7 @@ describe('MastraClient Resources', () => {
237
244
  model: 'gpt-4',
238
245
  instructions: 'Test instructions',
239
246
  tools: {},
247
+ workflows: {},
240
248
  };
241
249
  mockFetchResponse(mockResponse);
242
250
 
@@ -279,7 +287,7 @@ describe('MastraClient Resources', () => {
279
287
  });
280
288
 
281
289
  it('should stream responses', async () => {
282
- const mockChunk = { content: 'test response' };
290
+ const mockChunk = `0:"test response"\n`;
283
291
  mockFetchResponse(mockChunk, { isStream: true });
284
292
 
285
293
  const response = await agent.stream({
@@ -298,10 +306,118 @@ describe('MastraClient Resources', () => {
298
306
  if (reader) {
299
307
  const { value, done } = await reader.read();
300
308
  expect(done).toBe(false);
301
- expect(new TextDecoder().decode(value)).toBe(JSON.stringify(mockChunk));
309
+ expect(new TextDecoder().decode(value)).toBe(mockChunk);
302
310
  }
303
311
  });
304
312
 
313
+ it('should stream responses with tool calls', async () => {
314
+ const firstMockChunk = `0:"test "
315
+ 0:"response"
316
+ 9:{"toolCallId":"tool1","toolName":"testTool","args":{"arg1":"value1"}}
317
+ e:{"finishReason":"tool-calls","usage":{"promptTokens":1,"completionTokens":1},"isContinued":false}
318
+ d:{"finishReason":"tool-calls","usage":{"promptTokens":2,"completionTokens":2}}
319
+ `;
320
+
321
+ const secondMockChunk = `0:"final response"
322
+ e:{"finishReason":"stop","usage":{"promptTokens":2,"completionTokens":2},"isContinued":false}
323
+ d:{"finishReason":"stop","usage":{"promptTokens":2,"completionTokens":2}}
324
+ `;
325
+
326
+ const firstResponseBody = new ReadableStream({
327
+ start(controller) {
328
+ controller.enqueue(new TextEncoder().encode(firstMockChunk));
329
+ controller.close();
330
+ },
331
+ });
332
+
333
+ const secondResponseBody = new ReadableStream({
334
+ start(controller) {
335
+ controller.enqueue(new TextEncoder().encode(secondMockChunk));
336
+ controller.close();
337
+ },
338
+ });
339
+
340
+ (global.fetch as any)
341
+ .mockResolvedValueOnce(
342
+ new Response(firstResponseBody, {
343
+ status: 200,
344
+ headers: new Headers({ 'Content-Type': 'text/event-stream' }),
345
+ }),
346
+ )
347
+ .mockResolvedValueOnce(
348
+ new Response(secondResponseBody, {
349
+ status: 200,
350
+ headers: new Headers({ 'Content-Type': 'text/event-stream' }),
351
+ }),
352
+ );
353
+
354
+ const response = await agent.stream({
355
+ messages: [
356
+ {
357
+ role: 'user',
358
+ content: 'test',
359
+ },
360
+ ],
361
+ clientTools: {
362
+ testTool: {
363
+ id: 'testTool',
364
+ description: 'Test Tool',
365
+ inputSchema: {
366
+ type: 'object',
367
+ properties: {
368
+ arg1: { type: 'string' },
369
+ },
370
+ },
371
+ execute: async () => {
372
+ return 'test result';
373
+ },
374
+ },
375
+ },
376
+ });
377
+
378
+ expect(response.body).toBeInstanceOf(ReadableStream);
379
+ const reader = response?.body?.getReader();
380
+ expect(reader).toBeDefined();
381
+
382
+ let output = '';
383
+ if (reader) {
384
+ while (true) {
385
+ const { value, done } = await reader.read();
386
+ if (done) break;
387
+ output += new TextDecoder().decode(value);
388
+ }
389
+ }
390
+
391
+ expect(global.fetch).toHaveBeenCalledTimes(2);
392
+
393
+ const [secondUrl, secondConfig] = (global.fetch as any).mock.calls[1];
394
+ expect(secondUrl).toBe(`${clientOptions.baseUrl}/api/agents/test-agent/stream`);
395
+
396
+ const secondRequestBody = JSON.parse(secondConfig.body);
397
+ expect(secondRequestBody.messages).toHaveLength(2);
398
+ expect(secondRequestBody.messages[0].content).toBe('test');
399
+ expect(secondRequestBody.messages[1].content).toBe('test response');
400
+ expect(secondRequestBody.messages[1].parts).toEqual([
401
+ {
402
+ type: 'text',
403
+ text: 'test response',
404
+ },
405
+ {
406
+ type: 'tool-invocation',
407
+ toolInvocation: {
408
+ state: 'result',
409
+ step: 0,
410
+ toolCallId: 'tool1',
411
+ toolName: 'testTool',
412
+ args: {
413
+ arg1: 'value1',
414
+ },
415
+ result: 'test result',
416
+ },
417
+ },
418
+ ]);
419
+ });
420
+
305
421
  it('should get agent tool', async () => {
306
422
  const mockResponse = {
307
423
  id: 'tool1',
@@ -489,7 +605,7 @@ describe('MastraClient Resources', () => {
489
605
  const result = await memoryThread.update({
490
606
  title: 'Updated Thread',
491
607
  metadata: { updated: true },
492
- resourceid: 'test-resource',
608
+ resourceId: 'test-resource',
493
609
  });
494
610
  expect(result).toEqual(mockResponse);
495
611
  expect(global.fetch).toHaveBeenCalledWith(
@@ -536,6 +652,7 @@ describe('MastraClient Resources', () => {
536
652
  content: 'test',
537
653
  role: 'user' as const,
538
654
  threadId: 'test-thread',
655
+ resourceId: 'test-resource',
539
656
  createdAt: new Date('2025-03-26T10:40:55.116Z'),
540
657
  },
541
658
  ];
@@ -552,6 +669,77 @@ describe('MastraClient Resources', () => {
552
669
  }),
553
670
  );
554
671
  });
672
+
673
+ it('should get thread messages with limit', async () => {
674
+ const mockResponse = {
675
+ messages: [
676
+ {
677
+ id: '1',
678
+ content: 'test',
679
+ threadId,
680
+ role: 'user',
681
+ type: 'text',
682
+ resourceId: 'test-resource',
683
+ createdAt: new Date(),
684
+ },
685
+ ],
686
+ uiMessages: [],
687
+ };
688
+ mockFetchResponse(mockResponse);
689
+
690
+ const limit = 5;
691
+ const result = await memoryThread.getMessages({ limit });
692
+
693
+ expect(result).toEqual(mockResponse);
694
+ expect(global.fetch).toHaveBeenCalledWith(
695
+ `${clientOptions.baseUrl}/api/memory/threads/${threadId}/messages?agentId=${agentId}&limit=${limit}`,
696
+ expect.objectContaining({
697
+ headers: expect.objectContaining(clientOptions.headers),
698
+ }),
699
+ );
700
+ });
701
+
702
+ it('should get paginated thread messages', async () => {
703
+ const mockResponse = {
704
+ messages: [
705
+ {
706
+ id: '1',
707
+ content: 'test message',
708
+ threadId,
709
+ role: 'user',
710
+ type: 'text',
711
+ resourceId: 'test-resource',
712
+ createdAt: new Date(),
713
+ },
714
+ ],
715
+ total: 5,
716
+ page: 1,
717
+ perPage: 2,
718
+ hasMore: true,
719
+ };
720
+ mockFetchResponse(mockResponse);
721
+
722
+ const selectBy = {
723
+ pagination: {
724
+ page: 1,
725
+ perPage: 2,
726
+ },
727
+ };
728
+
729
+ const result = await memoryThread.getMessagesPaginated({
730
+ resourceId: 'test-resource',
731
+ format: 'v2',
732
+ selectBy,
733
+ });
734
+
735
+ expect(result).toEqual(mockResponse);
736
+ expect(global.fetch).toHaveBeenCalledWith(
737
+ `${clientOptions.baseUrl}/api/memory/threads/${threadId}/messages/paginated?resourceId=test-resource&format=v2&selectBy=${encodeURIComponent(JSON.stringify(selectBy))}`,
738
+ expect.objectContaining({
739
+ headers: expect.objectContaining(clientOptions.headers),
740
+ }),
741
+ );
742
+ });
555
743
  });
556
744
 
557
745
  describe('Tool Resource', () => {
@@ -584,10 +772,10 @@ describe('MastraClient Resources', () => {
584
772
  it('should execute tool', async () => {
585
773
  const mockResponse = { data: 'test' };
586
774
  mockFetchResponse(mockResponse);
587
- const result = await tool.execute({ data: '' });
775
+ const result = await tool.execute({ data: '', runId: 'test-run-id' });
588
776
  expect(result).toEqual(mockResponse);
589
777
  expect(global.fetch).toHaveBeenCalledWith(
590
- `${clientOptions.baseUrl}/api/tools/test-tool/execute`,
778
+ `${clientOptions.baseUrl}/api/tools/test-tool/execute?runId=test-run-id`,
591
779
  expect.objectContaining({
592
780
  method: 'POST',
593
781
  headers: expect.objectContaining({
@@ -632,14 +820,14 @@ describe('MastraClient Resources', () => {
632
820
  };
633
821
  mockFetchResponse(mockResponse);
634
822
 
635
- const result = await workflow.startAsync({ triggerData: { test: 'test' } });
823
+ const result = await workflow.startAsync({ inputData: { test: 'test' } });
636
824
  expect(result).toEqual(mockResponse);
637
825
  expect(global.fetch).toHaveBeenCalledWith(
638
826
  `${clientOptions.baseUrl}/api/workflows/test-workflow/start-async?`,
639
827
  expect.objectContaining({
640
828
  method: 'POST',
641
829
  headers: expect.objectContaining(clientOptions.headers),
642
- body: JSON.stringify({ test: 'test' }),
830
+ body: JSON.stringify({ inputData: { test: 'test' } }),
643
831
  }),
644
832
  );
645
833
  });
@@ -707,4 +895,332 @@ describe('MastraClient Resources', () => {
707
895
  );
708
896
  });
709
897
  });
898
+
899
+ describe('MCP Server Registry Client Methods', () => {
900
+ const mockServerInfo1 = {
901
+ id: 'mcp-server-1',
902
+ name: 'Test MCP Server 1',
903
+ version_detail: { version: '1.0.0', release_date: '2023-01-01T00:00:00Z', is_latest: true },
904
+ };
905
+ const mockServerInfo2 = {
906
+ id: 'mcp-server-2',
907
+ name: 'Test MCP Server 2',
908
+ version_detail: { version: '1.1.0', release_date: '2023-02-01T00:00:00Z', is_latest: true },
909
+ };
910
+
911
+ const mockServerDetail1: ServerDetailInfo = {
912
+ ...mockServerInfo1,
913
+ description: 'Detailed description for server 1',
914
+ package_canonical: 'npm',
915
+ packages: [{ registry_name: 'npm', name: '@example/server1', version: '1.0.0' }],
916
+ remotes: [{ transport_type: 'sse', url: 'http://localhost/sse1' }],
917
+ };
918
+
919
+ describe('getMcpServers()', () => {
920
+ it('should fetch a list of MCP servers', async () => {
921
+ const mockResponse: McpServerListResponse = {
922
+ servers: [mockServerInfo1, mockServerInfo2],
923
+ total_count: 2,
924
+ next: null,
925
+ };
926
+ mockFetchResponse(mockResponse);
927
+
928
+ const result = await client.getMcpServers();
929
+ expect(result).toEqual(mockResponse);
930
+ expect(global.fetch).toHaveBeenCalledWith(
931
+ `${clientOptions.baseUrl}/api/mcp/v0/servers`,
932
+ expect.objectContaining({
933
+ headers: expect.objectContaining(clientOptions.headers),
934
+ }),
935
+ );
936
+ });
937
+
938
+ it('should fetch MCP servers with limit and offset parameters', async () => {
939
+ const mockResponse: McpServerListResponse = {
940
+ servers: [mockServerInfo1],
941
+ total_count: 2,
942
+ next: '/api/mcp/v0/servers?limit=1&offset=1',
943
+ };
944
+ mockFetchResponse(mockResponse);
945
+
946
+ const result = await client.getMcpServers({ limit: 1, offset: 0 });
947
+ expect(result).toEqual(mockResponse);
948
+ expect(global.fetch).toHaveBeenCalledWith(
949
+ `${clientOptions.baseUrl}/api/mcp/v0/servers?limit=1&offset=0`,
950
+ expect.objectContaining({
951
+ headers: expect.objectContaining(clientOptions.headers),
952
+ }),
953
+ );
954
+ });
955
+ });
956
+
957
+ describe('getMcpServerDetails()', () => {
958
+ const serverId = 'mcp-server-1';
959
+
960
+ it('should fetch details for a specific MCP server', async () => {
961
+ mockFetchResponse(mockServerDetail1);
962
+
963
+ const result = await client.getMcpServerDetails(serverId);
964
+ expect(result).toEqual(mockServerDetail1);
965
+ expect(global.fetch).toHaveBeenCalledWith(
966
+ `${clientOptions.baseUrl}/api/mcp/v0/servers/${serverId}`,
967
+ expect.objectContaining({
968
+ headers: expect.objectContaining(clientOptions.headers),
969
+ }),
970
+ );
971
+ });
972
+
973
+ it('should fetch MCP server details with a version parameter', async () => {
974
+ mockFetchResponse(mockServerDetail1);
975
+ const version = '1.0.0';
976
+
977
+ const result = await client.getMcpServerDetails(serverId, { version });
978
+ expect(result).toEqual(mockServerDetail1);
979
+ expect(global.fetch).toHaveBeenCalledWith(
980
+ `${clientOptions.baseUrl}/api/mcp/v0/servers/${serverId}?version=${version}`,
981
+ expect.objectContaining({
982
+ headers: expect.objectContaining(clientOptions.headers),
983
+ }),
984
+ );
985
+ });
986
+ });
987
+ });
988
+
989
+ describe('Scores Methods', () => {
990
+ describe('getScorers()', () => {
991
+ it('should fetch all available scorers', async () => {
992
+ const mockResponse = {
993
+ scorers: [
994
+ { id: 'scorer-1', name: 'Test Scorer 1', description: 'A test scorer' },
995
+ { id: 'scorer-2', name: 'Test Scorer 2', description: 'Another test scorer' },
996
+ ],
997
+ };
998
+ mockFetchResponse(mockResponse);
999
+
1000
+ const result = await client.getScorers();
1001
+ expect(result).toEqual(mockResponse);
1002
+ expect(global.fetch).toHaveBeenCalledWith(
1003
+ `${clientOptions.baseUrl}/api/scores/scorers`,
1004
+ expect.objectContaining({
1005
+ headers: expect.objectContaining(clientOptions.headers),
1006
+ }),
1007
+ );
1008
+ });
1009
+ });
1010
+
1011
+ describe('getScoresByRunId()', () => {
1012
+ it('should fetch scores by run ID without pagination', async () => {
1013
+ const mockResponse = {
1014
+ pagination: {
1015
+ total: 10,
1016
+ page: 0,
1017
+ perPage: 10,
1018
+ hasMore: false,
1019
+ },
1020
+ scores: [
1021
+ {
1022
+ id: 'score-1',
1023
+ runId: 'run-123',
1024
+ scorer: { name: 'test-scorer' },
1025
+ result: { score: 0.8 },
1026
+ input: { messages: [] },
1027
+ output: { response: 'test' },
1028
+ source: 'LIVE',
1029
+ createdAt: new Date(),
1030
+ updatedAt: new Date(),
1031
+ },
1032
+ ],
1033
+ };
1034
+
1035
+ mockFetchResponse({
1036
+ ...mockResponse,
1037
+ scores: mockResponse.scores.map(score => ({
1038
+ ...score,
1039
+ createdAt: score.createdAt.toISOString(),
1040
+ updatedAt: score.updatedAt.toISOString(),
1041
+ })),
1042
+ });
1043
+
1044
+ const result = await client.getScoresByRunId({ runId: 'run-123' });
1045
+
1046
+ expect(result).toEqual({
1047
+ ...mockResponse,
1048
+ scores: mockResponse.scores.map(score => ({
1049
+ ...score,
1050
+ createdAt: score.createdAt.toISOString(),
1051
+ updatedAt: score.updatedAt.toISOString(),
1052
+ })),
1053
+ });
1054
+
1055
+ expect(global.fetch).toHaveBeenCalledWith(
1056
+ `${clientOptions.baseUrl}/api/scores/run/run-123`,
1057
+ expect.objectContaining({
1058
+ headers: expect.objectContaining(clientOptions.headers),
1059
+ }),
1060
+ );
1061
+ });
1062
+
1063
+ it('should fetch scores by run ID with pagination', async () => {
1064
+ const mockResponse = {
1065
+ pagination: {
1066
+ total: 20,
1067
+ page: 1,
1068
+ perPage: 5,
1069
+ hasMore: true,
1070
+ },
1071
+ scores: [],
1072
+ };
1073
+ mockFetchResponse(mockResponse);
1074
+
1075
+ const result = await client.getScoresByRunId({
1076
+ runId: 'run-123',
1077
+ page: 1,
1078
+ perPage: 5,
1079
+ });
1080
+ expect(result).toEqual(mockResponse);
1081
+ expect(global.fetch).toHaveBeenCalledWith(
1082
+ `${clientOptions.baseUrl}/api/scores/run/run-123?page=1&perPage=5`,
1083
+ expect.objectContaining({
1084
+ headers: expect.objectContaining(clientOptions.headers),
1085
+ }),
1086
+ );
1087
+ });
1088
+ });
1089
+
1090
+ describe('getScoresByEntityId()', () => {
1091
+ it('should fetch scores by entity ID and type without pagination', async () => {
1092
+ const mockResponse = {
1093
+ pagination: {
1094
+ total: 5,
1095
+ page: 0,
1096
+ perPage: 10,
1097
+ hasMore: false,
1098
+ },
1099
+ scores: [
1100
+ {
1101
+ id: 'score-1',
1102
+ runId: 'run-123',
1103
+ entityId: 'agent-456',
1104
+ entityType: 'AGENT',
1105
+ scorer: { name: 'test-scorer' },
1106
+ result: { score: 0.9 },
1107
+ input: { messages: [] },
1108
+ output: { response: 'test' },
1109
+ source: 'LIVE',
1110
+ createdAt: new Date(),
1111
+ updatedAt: new Date(),
1112
+ },
1113
+ ],
1114
+ };
1115
+
1116
+ const mockResponseWithDates = mockResponse.scores.map(score => ({
1117
+ ...score,
1118
+ createdAt: score.createdAt.toISOString(),
1119
+ updatedAt: score.updatedAt.toISOString(),
1120
+ }));
1121
+
1122
+ mockFetchResponse({
1123
+ ...mockResponse,
1124
+ scores: mockResponseWithDates,
1125
+ });
1126
+
1127
+ const result = await client.getScoresByEntityId({
1128
+ entityId: 'agent-456',
1129
+ entityType: 'AGENT',
1130
+ });
1131
+
1132
+ expect(result).toEqual({
1133
+ ...mockResponse,
1134
+ scores: mockResponseWithDates,
1135
+ });
1136
+
1137
+ expect(global.fetch).toHaveBeenCalledWith(
1138
+ `${clientOptions.baseUrl}/api/scores/entity/AGENT/agent-456`,
1139
+ expect.objectContaining({
1140
+ headers: expect.objectContaining(clientOptions.headers),
1141
+ }),
1142
+ );
1143
+ });
1144
+
1145
+ it('should fetch scores by entity ID and type with pagination', async () => {
1146
+ const mockResponse = {
1147
+ pagination: {
1148
+ total: 15,
1149
+ page: 2,
1150
+ perPage: 5,
1151
+ hasMore: true,
1152
+ },
1153
+ scores: [],
1154
+ };
1155
+ mockFetchResponse(mockResponse);
1156
+
1157
+ const result = await client.getScoresByEntityId({
1158
+ entityId: 'workflow-789',
1159
+ entityType: 'WORKFLOW',
1160
+ page: 2,
1161
+ perPage: 5,
1162
+ });
1163
+ expect(result).toEqual(mockResponse);
1164
+ expect(global.fetch).toHaveBeenCalledWith(
1165
+ `${clientOptions.baseUrl}/api/scores/entity/WORKFLOW/workflow-789?page=2&perPage=5`,
1166
+ expect.objectContaining({
1167
+ body: undefined,
1168
+ headers: expect.objectContaining(clientOptions.headers),
1169
+ signal: undefined,
1170
+ }),
1171
+ );
1172
+ });
1173
+ });
1174
+
1175
+ describe('saveScore()', () => {
1176
+ it('should save a score', async () => {
1177
+ const scoreData = {
1178
+ id: 'score-1',
1179
+ scorerId: 'test-scorer',
1180
+ runId: 'run-123',
1181
+ scorer: { name: 'test-scorer' },
1182
+ score: 0.85,
1183
+ input: [],
1184
+ output: { response: 'test response' },
1185
+ source: 'LIVE' as ScoringSource,
1186
+ entityId: 'agent-456',
1187
+ entityType: 'AGENT' as ScoringEntityType,
1188
+ entity: { id: 'agent-456', name: 'test-agent' },
1189
+ createdAt: new Date(),
1190
+ updatedAt: new Date(),
1191
+ runtimeContext: {
1192
+ model: {
1193
+ name: 'test-model',
1194
+ version: '1.0.0',
1195
+ },
1196
+ },
1197
+ };
1198
+ const mockResponse = {
1199
+ score: {
1200
+ ...scoreData,
1201
+ createdAt: scoreData.createdAt.toISOString(),
1202
+ updatedAt: scoreData.updatedAt.toISOString(),
1203
+ },
1204
+ };
1205
+ mockFetchResponse(mockResponse);
1206
+
1207
+ const result = await client.saveScore({ score: scoreData });
1208
+ expect(result).toEqual({
1209
+ score: {
1210
+ ...scoreData,
1211
+ createdAt: scoreData.createdAt.toISOString(),
1212
+ updatedAt: scoreData.updatedAt.toISOString(),
1213
+ },
1214
+ });
1215
+ expect(global.fetch).toHaveBeenCalledWith(
1216
+ `${clientOptions.baseUrl}/api/scores`,
1217
+ expect.objectContaining({
1218
+ method: 'POST',
1219
+ headers: expect.objectContaining(clientOptions.headers),
1220
+ body: JSON.stringify({ score: scoreData }),
1221
+ }),
1222
+ );
1223
+ });
1224
+ });
1225
+ });
710
1226
  });
package/src/index.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  export * from './client';
2
2
  export * from './types';
3
+ export type { UIMessageWithMetadata } from '@mastra/core/agent';
@@ -0,0 +1,88 @@
1
+ import type { TaskSendParams, TaskQueryParams, TaskIdParams, Task, AgentCard, JSONRPCResponse } from '@mastra/core/a2a';
2
+ import type { ClientOptions } from '../types';
3
+ import { BaseResource } from './base';
4
+
5
+ /**
6
+ * Class for interacting with an agent via the A2A protocol
7
+ */
8
+ export class A2A extends BaseResource {
9
+ constructor(
10
+ options: ClientOptions,
11
+ private agentId: string,
12
+ ) {
13
+ super(options);
14
+ }
15
+
16
+ /**
17
+ * Get the agent card with metadata about the agent
18
+ * @returns Promise containing the agent card information
19
+ */
20
+ async getCard(): Promise<AgentCard> {
21
+ return this.request(`/.well-known/${this.agentId}/agent.json`);
22
+ }
23
+
24
+ /**
25
+ * Send a message to the agent and get a response
26
+ * @param params - Parameters for the task
27
+ * @returns Promise containing the task response
28
+ */
29
+ async sendMessage(params: TaskSendParams): Promise<{ task: Task }> {
30
+ const response = await this.request<JSONRPCResponse<Task>>(`/a2a/${this.agentId}`, {
31
+ method: 'POST',
32
+ body: {
33
+ method: 'tasks/send',
34
+ params,
35
+ },
36
+ });
37
+
38
+ return { task: response.result! };
39
+ }
40
+
41
+ /**
42
+ * Get the status and result of a task
43
+ * @param params - Parameters for querying the task
44
+ * @returns Promise containing the task response
45
+ */
46
+ async getTask(params: TaskQueryParams): Promise<Task> {
47
+ const response = await this.request<JSONRPCResponse<Task>>(`/a2a/${this.agentId}`, {
48
+ method: 'POST',
49
+ body: {
50
+ method: 'tasks/get',
51
+ params,
52
+ },
53
+ });
54
+
55
+ return response.result!;
56
+ }
57
+
58
+ /**
59
+ * Cancel a running task
60
+ * @param params - Parameters identifying the task to cancel
61
+ * @returns Promise containing the task response
62
+ */
63
+ async cancelTask(params: TaskIdParams): Promise<{ task: Task }> {
64
+ return this.request(`/a2a/${this.agentId}`, {
65
+ method: 'POST',
66
+ body: {
67
+ method: 'tasks/cancel',
68
+ params,
69
+ },
70
+ });
71
+ }
72
+
73
+ /**
74
+ * Send a message and subscribe to streaming updates (not fully implemented)
75
+ * @param params - Parameters for the task
76
+ * @returns Promise containing the task response
77
+ */
78
+ async sendAndSubscribe(params: TaskSendParams): Promise<Response> {
79
+ return this.request(`/a2a/${this.agentId}`, {
80
+ method: 'POST',
81
+ body: {
82
+ method: 'tasks/sendSubscribe',
83
+ params,
84
+ },
85
+ stream: true,
86
+ });
87
+ }
88
+ }