@mastra/client-js 0.0.0-fix-shareable-traces-20250717112558 → 0.0.0-fix-tool-call-history-20250730195323
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/.turbo/turbo-build.log +8 -8
- package/CHANGELOG.md +207 -2
- package/README.md +1 -0
- package/dist/index.cjs +369 -251
- package/dist/index.d.cts +159 -25
- package/dist/index.d.ts +159 -25
- package/dist/index.js +369 -251
- package/package.json +4 -4
- package/src/client.ts +97 -0
- package/src/index.test.ts +402 -6
- package/src/index.ts +1 -0
- package/src/resources/agent.ts +285 -300
- package/src/resources/base.ts +5 -1
- package/src/resources/memory-thread.test.ts +285 -0
- package/src/resources/memory-thread.ts +36 -0
- package/src/resources/network-memory-thread.test.ts +269 -0
- package/src/resources/network-memory-thread.ts +18 -0
- package/src/resources/network.ts +4 -3
- package/src/resources/workflow.ts +9 -0
- package/src/types.ts +81 -7
- package/src/utils/process-client-tools.ts +1 -1
- package/src/v2-messages.test.ts +180 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mastra/client-js",
|
|
3
|
-
"version": "0.0.0-fix-
|
|
3
|
+
"version": "0.0.0-fix-tool-call-history-20250730195323",
|
|
4
4
|
"description": "The official TypeScript library for the Mastra Client API",
|
|
5
5
|
"author": "",
|
|
6
6
|
"type": "module",
|
|
@@ -34,13 +34,13 @@
|
|
|
34
34
|
"rxjs": "7.8.1",
|
|
35
35
|
"zod": "^3.25.67",
|
|
36
36
|
"zod-to-json-schema": "^3.24.5",
|
|
37
|
-
"@mastra/core": "0.0.0-fix-
|
|
37
|
+
"@mastra/core": "0.0.0-fix-tool-call-history-20250730195323"
|
|
38
38
|
},
|
|
39
39
|
"peerDependencies": {
|
|
40
40
|
"zod": "^3.0.0"
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
|
-
"@babel/preset-env": "^7.
|
|
43
|
+
"@babel/preset-env": "^7.28.0",
|
|
44
44
|
"@babel/preset-typescript": "^7.27.1",
|
|
45
45
|
"@tsconfig/recommended": "^1.0.9",
|
|
46
46
|
"@types/json-schema": "^7.0.15",
|
|
@@ -48,7 +48,7 @@
|
|
|
48
48
|
"tsup": "^8.5.0",
|
|
49
49
|
"typescript": "^5.8.3",
|
|
50
50
|
"vitest": "^3.2.4",
|
|
51
|
-
"@internal/lint": "0.0.0-fix-
|
|
51
|
+
"@internal/lint": "0.0.0-fix-tool-call-history-20250730195323"
|
|
52
52
|
},
|
|
53
53
|
"scripts": {
|
|
54
54
|
"build": "tsup src/index.ts --format esm,cjs --dts --clean --treeshake=smallest --splitting",
|
package/src/client.ts
CHANGED
|
@@ -39,6 +39,13 @@ import type {
|
|
|
39
39
|
GetNetworkMemoryThreadParams,
|
|
40
40
|
CreateNetworkMemoryThreadParams,
|
|
41
41
|
SaveNetworkMessageToMemoryParams,
|
|
42
|
+
GetScorerResponse,
|
|
43
|
+
GetScoresByScorerIdParams,
|
|
44
|
+
GetScoresResponse,
|
|
45
|
+
GetScoresByRunIdParams,
|
|
46
|
+
GetScoresByEntityIdParams,
|
|
47
|
+
SaveScoreParams,
|
|
48
|
+
SaveScoreResponse,
|
|
42
49
|
} from './types';
|
|
43
50
|
|
|
44
51
|
export class MastraClient extends BaseResource {
|
|
@@ -523,4 +530,94 @@ export class MastraClient extends BaseResource {
|
|
|
523
530
|
},
|
|
524
531
|
});
|
|
525
532
|
}
|
|
533
|
+
|
|
534
|
+
/**
|
|
535
|
+
* Retrieves all available scorers
|
|
536
|
+
* @returns Promise containing list of available scorers
|
|
537
|
+
*/
|
|
538
|
+
public getScorers(): Promise<Record<string, GetScorerResponse>> {
|
|
539
|
+
return this.request('/api/scores/scorers');
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
/**
|
|
543
|
+
* Retrieves a scorer by ID
|
|
544
|
+
* @param scorerId - ID of the scorer to retrieve
|
|
545
|
+
* @returns Promise containing the scorer
|
|
546
|
+
*/
|
|
547
|
+
public getScorer(scorerId: string): Promise<GetScorerResponse> {
|
|
548
|
+
return this.request(`/api/scores/scorers/${scorerId}`);
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
public getScoresByScorerId(params: GetScoresByScorerIdParams): Promise<GetScoresResponse> {
|
|
552
|
+
const { page, perPage, scorerId, entityId, entityType } = params;
|
|
553
|
+
const searchParams = new URLSearchParams();
|
|
554
|
+
|
|
555
|
+
if (entityId) {
|
|
556
|
+
searchParams.set('entityId', entityId);
|
|
557
|
+
}
|
|
558
|
+
if (entityType) {
|
|
559
|
+
searchParams.set('entityType', entityType);
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
if (page !== undefined) {
|
|
563
|
+
searchParams.set('page', String(page));
|
|
564
|
+
}
|
|
565
|
+
if (perPage !== undefined) {
|
|
566
|
+
searchParams.set('perPage', String(perPage));
|
|
567
|
+
}
|
|
568
|
+
const queryString = searchParams.toString();
|
|
569
|
+
return this.request(`/api/scores/scorer/${scorerId}${queryString ? `?${queryString}` : ''}`);
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
/**
|
|
573
|
+
* Retrieves scores by run ID
|
|
574
|
+
* @param params - Parameters containing run ID and pagination options
|
|
575
|
+
* @returns Promise containing scores and pagination info
|
|
576
|
+
*/
|
|
577
|
+
public getScoresByRunId(params: GetScoresByRunIdParams): Promise<GetScoresResponse> {
|
|
578
|
+
const { runId, page, perPage } = params;
|
|
579
|
+
const searchParams = new URLSearchParams();
|
|
580
|
+
|
|
581
|
+
if (page !== undefined) {
|
|
582
|
+
searchParams.set('page', String(page));
|
|
583
|
+
}
|
|
584
|
+
if (perPage !== undefined) {
|
|
585
|
+
searchParams.set('perPage', String(perPage));
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
const queryString = searchParams.toString();
|
|
589
|
+
return this.request(`/api/scores/run/${runId}${queryString ? `?${queryString}` : ''}`);
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
/**
|
|
593
|
+
* Retrieves scores by entity ID and type
|
|
594
|
+
* @param params - Parameters containing entity ID, type, and pagination options
|
|
595
|
+
* @returns Promise containing scores and pagination info
|
|
596
|
+
*/
|
|
597
|
+
public getScoresByEntityId(params: GetScoresByEntityIdParams): Promise<GetScoresResponse> {
|
|
598
|
+
const { entityId, entityType, page, perPage } = params;
|
|
599
|
+
const searchParams = new URLSearchParams();
|
|
600
|
+
|
|
601
|
+
if (page !== undefined) {
|
|
602
|
+
searchParams.set('page', String(page));
|
|
603
|
+
}
|
|
604
|
+
if (perPage !== undefined) {
|
|
605
|
+
searchParams.set('perPage', String(perPage));
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
const queryString = searchParams.toString();
|
|
609
|
+
return this.request(`/api/scores/entity/${entityType}/${entityId}${queryString ? `?${queryString}` : ''}`);
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
/**
|
|
613
|
+
* Saves a score
|
|
614
|
+
* @param params - Parameters containing the score data to save
|
|
615
|
+
* @returns Promise containing the saved score
|
|
616
|
+
*/
|
|
617
|
+
public saveScore(params: SaveScoreParams): Promise<SaveScoreResponse> {
|
|
618
|
+
return this.request('/api/scores', {
|
|
619
|
+
method: 'POST',
|
|
620
|
+
body: params,
|
|
621
|
+
});
|
|
622
|
+
}
|
|
526
623
|
}
|
package/src/index.test.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { describe, expect, beforeEach, it, vi } from 'vitest';
|
|
2
2
|
import { MastraClient } from './client';
|
|
3
|
-
import type { McpServerListResponse
|
|
3
|
+
import type { McpServerListResponse } from './types';
|
|
4
|
+
import type { ServerDetailInfo } from '@mastra/core/mcp';
|
|
5
|
+
import { ScoringEntityType, ScoringSource } from '@mastra/core/scores';
|
|
4
6
|
|
|
5
7
|
// Mock fetch globally
|
|
6
8
|
global.fetch = vi.fn();
|
|
@@ -27,7 +29,13 @@ describe('MastraClient Resources', () => {
|
|
|
27
29
|
} else {
|
|
28
30
|
responseBody = new ReadableStream({
|
|
29
31
|
start(controller) {
|
|
30
|
-
|
|
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
|
+
}
|
|
31
39
|
controller.close();
|
|
32
40
|
},
|
|
33
41
|
});
|
|
@@ -279,7 +287,7 @@ describe('MastraClient Resources', () => {
|
|
|
279
287
|
});
|
|
280
288
|
|
|
281
289
|
it('should stream responses', async () => {
|
|
282
|
-
const mockChunk =
|
|
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(
|
|
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',
|
|
@@ -582,6 +698,48 @@ describe('MastraClient Resources', () => {
|
|
|
582
698
|
}),
|
|
583
699
|
);
|
|
584
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
|
+
});
|
|
585
743
|
});
|
|
586
744
|
|
|
587
745
|
describe('Tool Resource', () => {
|
|
@@ -662,14 +820,14 @@ describe('MastraClient Resources', () => {
|
|
|
662
820
|
};
|
|
663
821
|
mockFetchResponse(mockResponse);
|
|
664
822
|
|
|
665
|
-
const result = await workflow.startAsync({
|
|
823
|
+
const result = await workflow.startAsync({ inputData: { test: 'test' } });
|
|
666
824
|
expect(result).toEqual(mockResponse);
|
|
667
825
|
expect(global.fetch).toHaveBeenCalledWith(
|
|
668
826
|
`${clientOptions.baseUrl}/api/workflows/test-workflow/start-async?`,
|
|
669
827
|
expect.objectContaining({
|
|
670
828
|
method: 'POST',
|
|
671
829
|
headers: expect.objectContaining(clientOptions.headers),
|
|
672
|
-
body: JSON.stringify({ test: 'test' }),
|
|
830
|
+
body: JSON.stringify({ inputData: { test: 'test' } }),
|
|
673
831
|
}),
|
|
674
832
|
);
|
|
675
833
|
});
|
|
@@ -827,4 +985,242 @@ describe('MastraClient Resources', () => {
|
|
|
827
985
|
});
|
|
828
986
|
});
|
|
829
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
|
+
});
|
|
830
1226
|
});
|
package/src/index.ts
CHANGED