@mastra/client-js 0.0.0-fix-fetching-workflow-snapshots-20250625000954 → 0.0.0-fix-traces-pagination-plus-share-for-cloud-20250717083008
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 +283 -2
- package/LICENSE.md +11 -42
- package/dist/index.cjs +468 -324
- package/dist/index.d.cts +119 -39
- package/dist/index.d.ts +119 -39
- package/dist/index.js +469 -325
- package/package.json +6 -5
- package/src/client.ts +48 -2
- package/src/example.ts +45 -17
- package/src/resources/agent.ts +382 -317
- package/src/resources/base.ts +1 -0
- package/src/resources/vNextNetwork.ts +51 -4
- package/src/resources/workflow.ts +34 -6
- package/src/types.ts +31 -3
package/dist/index.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { AbstractAgent, EventType } from '@ag-ui/client';
|
|
2
2
|
import { Observable } from 'rxjs';
|
|
3
|
-
import { processDataStream, parsePartialJson } from '@ai-sdk/ui-utils';
|
|
3
|
+
import { processTextStream, processDataStream, parsePartialJson } from '@ai-sdk/ui-utils';
|
|
4
4
|
import { ZodSchema } from 'zod';
|
|
5
5
|
import originalZodToJsonSchema from 'zod-to-json-schema';
|
|
6
6
|
import { isVercelTool } from '@mastra/core/tools';
|
|
7
|
+
import { v4 } from '@lukeed/uuid';
|
|
7
8
|
import { RuntimeContext } from '@mastra/core/runtime-context';
|
|
8
9
|
|
|
9
10
|
// src/adapters/agui.ts
|
|
@@ -252,6 +253,7 @@ var BaseResource = class {
|
|
|
252
253
|
// TODO: Bring this back once we figure out what we/users need to do to make this work with cross-origin requests
|
|
253
254
|
// 'x-mastra-client-type': 'js',
|
|
254
255
|
},
|
|
256
|
+
signal: this.options.abortSignal,
|
|
255
257
|
body: options.body instanceof FormData ? options.body : options.body ? JSON.stringify(options.body) : void 0
|
|
256
258
|
});
|
|
257
259
|
if (!response.ok) {
|
|
@@ -375,7 +377,11 @@ var Agent = class extends BaseResource {
|
|
|
375
377
|
body: processedParams
|
|
376
378
|
});
|
|
377
379
|
if (response.finishReason === "tool-calls") {
|
|
378
|
-
|
|
380
|
+
const toolCalls = response.toolCalls;
|
|
381
|
+
if (!toolCalls || !Array.isArray(toolCalls)) {
|
|
382
|
+
return response;
|
|
383
|
+
}
|
|
384
|
+
for (const toolCall of toolCalls) {
|
|
379
385
|
const clientTool = params.clientTools?.[toolCall.toolName];
|
|
380
386
|
if (clientTool && clientTool.execute) {
|
|
381
387
|
const result = await clientTool.execute(
|
|
@@ -418,7 +424,8 @@ var Agent = class extends BaseResource {
|
|
|
418
424
|
onToolCall,
|
|
419
425
|
onFinish,
|
|
420
426
|
getCurrentDate = () => /* @__PURE__ */ new Date(),
|
|
421
|
-
lastMessage
|
|
427
|
+
lastMessage,
|
|
428
|
+
streamProtocol
|
|
422
429
|
}) {
|
|
423
430
|
const replaceLastMessage = lastMessage?.role === "assistant";
|
|
424
431
|
let step = replaceLastMessage ? 1 + // find max step in existing tool invocations:
|
|
@@ -426,7 +433,7 @@ var Agent = class extends BaseResource {
|
|
|
426
433
|
return Math.max(max, toolInvocation.step ?? 0);
|
|
427
434
|
}, 0) ?? 0) : 0;
|
|
428
435
|
const message = replaceLastMessage ? structuredClone(lastMessage) : {
|
|
429
|
-
id:
|
|
436
|
+
id: v4(),
|
|
430
437
|
createdAt: getCurrentDate(),
|
|
431
438
|
role: "assistant",
|
|
432
439
|
content: "",
|
|
@@ -471,7 +478,7 @@ var Agent = class extends BaseResource {
|
|
|
471
478
|
// changes. This is why we need to add a revision id to ensure that the message
|
|
472
479
|
// is updated with SWR (without it, the changes get stuck in SWR and are not
|
|
473
480
|
// forwarded to rendering):
|
|
474
|
-
revisionId:
|
|
481
|
+
revisionId: v4()
|
|
475
482
|
};
|
|
476
483
|
update({
|
|
477
484
|
message: copiedMessage,
|
|
@@ -479,228 +486,213 @@ var Agent = class extends BaseResource {
|
|
|
479
486
|
replaceLastMessage
|
|
480
487
|
});
|
|
481
488
|
}
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
text: value
|
|
489
|
-
};
|
|
490
|
-
message.parts.push(currentTextPart);
|
|
491
|
-
} else {
|
|
492
|
-
currentTextPart.text += value;
|
|
489
|
+
if (streamProtocol === "text") {
|
|
490
|
+
await processTextStream({
|
|
491
|
+
stream,
|
|
492
|
+
onTextPart(value) {
|
|
493
|
+
message.content += value;
|
|
494
|
+
execUpdate();
|
|
493
495
|
}
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
if (
|
|
501
|
-
|
|
496
|
+
});
|
|
497
|
+
onFinish?.({ message, finishReason, usage });
|
|
498
|
+
} else {
|
|
499
|
+
await processDataStream({
|
|
500
|
+
stream,
|
|
501
|
+
onTextPart(value) {
|
|
502
|
+
if (currentTextPart == null) {
|
|
503
|
+
currentTextPart = {
|
|
504
|
+
type: "text",
|
|
505
|
+
text: value
|
|
506
|
+
};
|
|
507
|
+
message.parts.push(currentTextPart);
|
|
508
|
+
} else {
|
|
509
|
+
currentTextPart.text += value;
|
|
502
510
|
}
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
}
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
type: "
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
}
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
const invocation = {
|
|
566
|
-
state: "partial-call",
|
|
567
|
-
step,
|
|
568
|
-
toolCallId: value.toolCallId,
|
|
569
|
-
toolName: value.toolName,
|
|
570
|
-
args: void 0
|
|
571
|
-
};
|
|
572
|
-
message.toolInvocations.push(invocation);
|
|
573
|
-
updateToolInvocationPart(value.toolCallId, invocation);
|
|
574
|
-
execUpdate();
|
|
575
|
-
},
|
|
576
|
-
onToolCallDeltaPart(value) {
|
|
577
|
-
const partialToolCall = partialToolCalls[value.toolCallId];
|
|
578
|
-
partialToolCall.text += value.argsTextDelta;
|
|
579
|
-
const { value: partialArgs } = parsePartialJson(partialToolCall.text);
|
|
580
|
-
const invocation = {
|
|
581
|
-
state: "partial-call",
|
|
582
|
-
step: partialToolCall.step,
|
|
583
|
-
toolCallId: value.toolCallId,
|
|
584
|
-
toolName: partialToolCall.toolName,
|
|
585
|
-
args: partialArgs
|
|
586
|
-
};
|
|
587
|
-
message.toolInvocations[partialToolCall.index] = invocation;
|
|
588
|
-
updateToolInvocationPart(value.toolCallId, invocation);
|
|
589
|
-
execUpdate();
|
|
590
|
-
},
|
|
591
|
-
async onToolCallPart(value) {
|
|
592
|
-
const invocation = {
|
|
593
|
-
state: "call",
|
|
594
|
-
step,
|
|
595
|
-
...value
|
|
596
|
-
};
|
|
597
|
-
if (partialToolCalls[value.toolCallId] != null) {
|
|
598
|
-
message.toolInvocations[partialToolCalls[value.toolCallId].index] = invocation;
|
|
599
|
-
} else {
|
|
511
|
+
message.content += value;
|
|
512
|
+
execUpdate();
|
|
513
|
+
},
|
|
514
|
+
onReasoningPart(value) {
|
|
515
|
+
if (currentReasoningTextDetail == null) {
|
|
516
|
+
currentReasoningTextDetail = { type: "text", text: value };
|
|
517
|
+
if (currentReasoningPart != null) {
|
|
518
|
+
currentReasoningPart.details.push(currentReasoningTextDetail);
|
|
519
|
+
}
|
|
520
|
+
} else {
|
|
521
|
+
currentReasoningTextDetail.text += value;
|
|
522
|
+
}
|
|
523
|
+
if (currentReasoningPart == null) {
|
|
524
|
+
currentReasoningPart = {
|
|
525
|
+
type: "reasoning",
|
|
526
|
+
reasoning: value,
|
|
527
|
+
details: [currentReasoningTextDetail]
|
|
528
|
+
};
|
|
529
|
+
message.parts.push(currentReasoningPart);
|
|
530
|
+
} else {
|
|
531
|
+
currentReasoningPart.reasoning += value;
|
|
532
|
+
}
|
|
533
|
+
message.reasoning = (message.reasoning ?? "") + value;
|
|
534
|
+
execUpdate();
|
|
535
|
+
},
|
|
536
|
+
onReasoningSignaturePart(value) {
|
|
537
|
+
if (currentReasoningTextDetail != null) {
|
|
538
|
+
currentReasoningTextDetail.signature = value.signature;
|
|
539
|
+
}
|
|
540
|
+
},
|
|
541
|
+
onRedactedReasoningPart(value) {
|
|
542
|
+
if (currentReasoningPart == null) {
|
|
543
|
+
currentReasoningPart = {
|
|
544
|
+
type: "reasoning",
|
|
545
|
+
reasoning: "",
|
|
546
|
+
details: []
|
|
547
|
+
};
|
|
548
|
+
message.parts.push(currentReasoningPart);
|
|
549
|
+
}
|
|
550
|
+
currentReasoningPart.details.push({
|
|
551
|
+
type: "redacted",
|
|
552
|
+
data: value.data
|
|
553
|
+
});
|
|
554
|
+
currentReasoningTextDetail = void 0;
|
|
555
|
+
execUpdate();
|
|
556
|
+
},
|
|
557
|
+
onFilePart(value) {
|
|
558
|
+
message.parts.push({
|
|
559
|
+
type: "file",
|
|
560
|
+
mimeType: value.mimeType,
|
|
561
|
+
data: value.data
|
|
562
|
+
});
|
|
563
|
+
execUpdate();
|
|
564
|
+
},
|
|
565
|
+
onSourcePart(value) {
|
|
566
|
+
message.parts.push({
|
|
567
|
+
type: "source",
|
|
568
|
+
source: value
|
|
569
|
+
});
|
|
570
|
+
execUpdate();
|
|
571
|
+
},
|
|
572
|
+
onToolCallStreamingStartPart(value) {
|
|
600
573
|
if (message.toolInvocations == null) {
|
|
601
574
|
message.toolInvocations = [];
|
|
602
575
|
}
|
|
576
|
+
partialToolCalls[value.toolCallId] = {
|
|
577
|
+
text: "",
|
|
578
|
+
step,
|
|
579
|
+
toolName: value.toolName,
|
|
580
|
+
index: message.toolInvocations.length
|
|
581
|
+
};
|
|
582
|
+
const invocation = {
|
|
583
|
+
state: "partial-call",
|
|
584
|
+
step,
|
|
585
|
+
toolCallId: value.toolCallId,
|
|
586
|
+
toolName: value.toolName,
|
|
587
|
+
args: void 0
|
|
588
|
+
};
|
|
603
589
|
message.toolInvocations.push(invocation);
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
const
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
590
|
+
updateToolInvocationPart(value.toolCallId, invocation);
|
|
591
|
+
execUpdate();
|
|
592
|
+
},
|
|
593
|
+
onToolCallDeltaPart(value) {
|
|
594
|
+
const partialToolCall = partialToolCalls[value.toolCallId];
|
|
595
|
+
partialToolCall.text += value.argsTextDelta;
|
|
596
|
+
const { value: partialArgs } = parsePartialJson(partialToolCall.text);
|
|
597
|
+
const invocation = {
|
|
598
|
+
state: "partial-call",
|
|
599
|
+
step: partialToolCall.step,
|
|
600
|
+
toolCallId: value.toolCallId,
|
|
601
|
+
toolName: partialToolCall.toolName,
|
|
602
|
+
args: partialArgs
|
|
603
|
+
};
|
|
604
|
+
message.toolInvocations[partialToolCall.index] = invocation;
|
|
605
|
+
updateToolInvocationPart(value.toolCallId, invocation);
|
|
606
|
+
execUpdate();
|
|
607
|
+
},
|
|
608
|
+
async onToolCallPart(value) {
|
|
609
|
+
const invocation = {
|
|
610
|
+
state: "call",
|
|
611
|
+
step,
|
|
612
|
+
...value
|
|
613
|
+
};
|
|
614
|
+
if (partialToolCalls[value.toolCallId] != null) {
|
|
615
|
+
message.toolInvocations[partialToolCalls[value.toolCallId].index] = invocation;
|
|
616
|
+
} else {
|
|
617
|
+
if (message.toolInvocations == null) {
|
|
618
|
+
message.toolInvocations = [];
|
|
619
|
+
}
|
|
620
|
+
message.toolInvocations.push(invocation);
|
|
619
621
|
}
|
|
622
|
+
updateToolInvocationPart(value.toolCallId, invocation);
|
|
623
|
+
execUpdate();
|
|
624
|
+
if (onToolCall) {
|
|
625
|
+
const result = await onToolCall({ toolCall: value });
|
|
626
|
+
if (result != null) {
|
|
627
|
+
const invocation2 = {
|
|
628
|
+
state: "result",
|
|
629
|
+
step,
|
|
630
|
+
...value,
|
|
631
|
+
result
|
|
632
|
+
};
|
|
633
|
+
message.toolInvocations[message.toolInvocations.length - 1] = invocation2;
|
|
634
|
+
updateToolInvocationPart(value.toolCallId, invocation2);
|
|
635
|
+
execUpdate();
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
},
|
|
639
|
+
onToolResultPart(value) {
|
|
640
|
+
const toolInvocations = message.toolInvocations;
|
|
641
|
+
if (toolInvocations == null) {
|
|
642
|
+
throw new Error("tool_result must be preceded by a tool_call");
|
|
643
|
+
}
|
|
644
|
+
const toolInvocationIndex = toolInvocations.findIndex(
|
|
645
|
+
(invocation2) => invocation2.toolCallId === value.toolCallId
|
|
646
|
+
);
|
|
647
|
+
if (toolInvocationIndex === -1) {
|
|
648
|
+
throw new Error("tool_result must be preceded by a tool_call with the same toolCallId");
|
|
649
|
+
}
|
|
650
|
+
const invocation = {
|
|
651
|
+
...toolInvocations[toolInvocationIndex],
|
|
652
|
+
state: "result",
|
|
653
|
+
...value
|
|
654
|
+
};
|
|
655
|
+
toolInvocations[toolInvocationIndex] = invocation;
|
|
656
|
+
updateToolInvocationPart(value.toolCallId, invocation);
|
|
657
|
+
execUpdate();
|
|
658
|
+
},
|
|
659
|
+
onDataPart(value) {
|
|
660
|
+
data.push(...value);
|
|
661
|
+
execUpdate();
|
|
662
|
+
},
|
|
663
|
+
onMessageAnnotationsPart(value) {
|
|
664
|
+
if (messageAnnotations == null) {
|
|
665
|
+
messageAnnotations = [...value];
|
|
666
|
+
} else {
|
|
667
|
+
messageAnnotations.push(...value);
|
|
668
|
+
}
|
|
669
|
+
execUpdate();
|
|
670
|
+
},
|
|
671
|
+
onFinishStepPart(value) {
|
|
672
|
+
step += 1;
|
|
673
|
+
currentTextPart = value.isContinued ? currentTextPart : void 0;
|
|
674
|
+
currentReasoningPart = void 0;
|
|
675
|
+
currentReasoningTextDetail = void 0;
|
|
676
|
+
},
|
|
677
|
+
onStartStepPart(value) {
|
|
678
|
+
if (!replaceLastMessage) {
|
|
679
|
+
message.id = value.messageId;
|
|
680
|
+
}
|
|
681
|
+
message.parts.push({ type: "step-start" });
|
|
682
|
+
execUpdate();
|
|
683
|
+
},
|
|
684
|
+
onFinishMessagePart(value) {
|
|
685
|
+
finishReason = value.finishReason;
|
|
686
|
+
if (value.usage != null) {
|
|
687
|
+
usage = value.usage;
|
|
688
|
+
}
|
|
689
|
+
},
|
|
690
|
+
onErrorPart(error) {
|
|
691
|
+
throw new Error(error);
|
|
620
692
|
}
|
|
621
|
-
},
|
|
622
|
-
onToolResultPart(value) {
|
|
623
|
-
const toolInvocations = message.toolInvocations;
|
|
624
|
-
if (toolInvocations == null) {
|
|
625
|
-
throw new Error("tool_result must be preceded by a tool_call");
|
|
626
|
-
}
|
|
627
|
-
const toolInvocationIndex = toolInvocations.findIndex((invocation2) => invocation2.toolCallId === value.toolCallId);
|
|
628
|
-
if (toolInvocationIndex === -1) {
|
|
629
|
-
throw new Error("tool_result must be preceded by a tool_call with the same toolCallId");
|
|
630
|
-
}
|
|
631
|
-
const invocation = {
|
|
632
|
-
...toolInvocations[toolInvocationIndex],
|
|
633
|
-
state: "result",
|
|
634
|
-
...value
|
|
635
|
-
};
|
|
636
|
-
toolInvocations[toolInvocationIndex] = invocation;
|
|
637
|
-
updateToolInvocationPart(value.toolCallId, invocation);
|
|
638
|
-
execUpdate();
|
|
639
|
-
},
|
|
640
|
-
onDataPart(value) {
|
|
641
|
-
data.push(...value);
|
|
642
|
-
execUpdate();
|
|
643
|
-
},
|
|
644
|
-
onMessageAnnotationsPart(value) {
|
|
645
|
-
if (messageAnnotations == null) {
|
|
646
|
-
messageAnnotations = [...value];
|
|
647
|
-
} else {
|
|
648
|
-
messageAnnotations.push(...value);
|
|
649
|
-
}
|
|
650
|
-
execUpdate();
|
|
651
|
-
},
|
|
652
|
-
onFinishStepPart(value) {
|
|
653
|
-
step += 1;
|
|
654
|
-
currentTextPart = value.isContinued ? currentTextPart : void 0;
|
|
655
|
-
currentReasoningPart = void 0;
|
|
656
|
-
currentReasoningTextDetail = void 0;
|
|
657
|
-
},
|
|
658
|
-
onStartStepPart(value) {
|
|
659
|
-
if (!replaceLastMessage) {
|
|
660
|
-
message.id = value.messageId;
|
|
661
|
-
}
|
|
662
|
-
message.parts.push({ type: "step-start" });
|
|
663
|
-
execUpdate();
|
|
664
|
-
},
|
|
665
|
-
onFinishMessagePart(value) {
|
|
666
|
-
finishReason = value.finishReason;
|
|
667
|
-
if (value.usage != null) {
|
|
668
|
-
usage = value.usage;
|
|
669
|
-
}
|
|
670
|
-
},
|
|
671
|
-
onErrorPart(error) {
|
|
672
|
-
throw new Error(error);
|
|
673
|
-
}
|
|
674
|
-
});
|
|
675
|
-
onFinish?.({ message, finishReason, usage });
|
|
676
|
-
}
|
|
677
|
-
/**
|
|
678
|
-
* Streams a response from the agent
|
|
679
|
-
* @param params - Stream parameters including prompt
|
|
680
|
-
* @returns Promise containing the enhanced Response object with processDataStream method
|
|
681
|
-
*/
|
|
682
|
-
async stream(params) {
|
|
683
|
-
const processedParams = {
|
|
684
|
-
...params,
|
|
685
|
-
output: params.output ? zodToJsonSchema(params.output) : void 0,
|
|
686
|
-
experimental_output: params.experimental_output ? zodToJsonSchema(params.experimental_output) : void 0,
|
|
687
|
-
runtimeContext: parseClientRuntimeContext(params.runtimeContext),
|
|
688
|
-
clientTools: processClientTools(params.clientTools)
|
|
689
|
-
};
|
|
690
|
-
const { readable, writable } = new TransformStream();
|
|
691
|
-
const response = await this.processStreamResponse(processedParams, writable);
|
|
692
|
-
const streamResponse = new Response(readable, {
|
|
693
|
-
status: response.status,
|
|
694
|
-
statusText: response.statusText,
|
|
695
|
-
headers: response.headers
|
|
696
|
-
});
|
|
697
|
-
streamResponse.processDataStream = async (options = {}) => {
|
|
698
|
-
await processDataStream({
|
|
699
|
-
stream: streamResponse.body,
|
|
700
|
-
...options
|
|
701
693
|
});
|
|
702
|
-
|
|
703
|
-
|
|
694
|
+
onFinish?.({ message, finishReason, usage });
|
|
695
|
+
}
|
|
704
696
|
}
|
|
705
697
|
/**
|
|
706
698
|
* Processes the stream response and handles tool calls
|
|
@@ -715,80 +707,135 @@ var Agent = class extends BaseResource {
|
|
|
715
707
|
throw new Error("No response body");
|
|
716
708
|
}
|
|
717
709
|
try {
|
|
710
|
+
const streamProtocol = processedParams.output ? "text" : "data";
|
|
718
711
|
let toolCalls = [];
|
|
719
|
-
let finishReasonToolCalls = false;
|
|
720
712
|
let messages = [];
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
713
|
+
const [streamForWritable, streamForProcessing] = response.body.tee();
|
|
714
|
+
streamForWritable.pipeTo(writable, {
|
|
715
|
+
preventClose: true
|
|
716
|
+
}).catch((error) => {
|
|
717
|
+
console.error("Error piping to writable stream:", error);
|
|
718
|
+
});
|
|
719
|
+
this.processChatResponse({
|
|
720
|
+
stream: streamForProcessing,
|
|
725
721
|
update: ({ message }) => {
|
|
726
722
|
messages.push(message);
|
|
727
723
|
},
|
|
728
|
-
onFinish: ({ finishReason, message }) => {
|
|
724
|
+
onFinish: async ({ finishReason, message }) => {
|
|
729
725
|
if (finishReason === "tool-calls") {
|
|
730
|
-
finishReasonToolCalls = true;
|
|
731
726
|
const toolCall = [...message?.parts ?? []].reverse().find((part) => part.type === "tool-invocation")?.toolInvocation;
|
|
732
727
|
if (toolCall) {
|
|
733
728
|
toolCalls.push(toolCall);
|
|
734
729
|
}
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
730
|
+
for (const toolCall2 of toolCalls) {
|
|
731
|
+
const clientTool = processedParams.clientTools?.[toolCall2.toolName];
|
|
732
|
+
if (clientTool && clientTool.execute) {
|
|
733
|
+
const result = await clientTool.execute(
|
|
734
|
+
{
|
|
735
|
+
context: toolCall2?.args,
|
|
736
|
+
runId: processedParams.runId,
|
|
737
|
+
resourceId: processedParams.resourceId,
|
|
738
|
+
threadId: processedParams.threadId,
|
|
739
|
+
runtimeContext: processedParams.runtimeContext
|
|
740
|
+
},
|
|
741
|
+
{
|
|
742
|
+
messages: response.messages,
|
|
743
|
+
toolCallId: toolCall2?.toolCallId
|
|
744
|
+
}
|
|
745
|
+
);
|
|
746
|
+
const lastMessage = JSON.parse(JSON.stringify(messages[messages.length - 1]));
|
|
747
|
+
const toolInvocationPart = lastMessage?.parts?.find(
|
|
748
|
+
(part) => part.type === "tool-invocation" && part.toolInvocation?.toolCallId === toolCall2.toolCallId
|
|
749
|
+
);
|
|
750
|
+
if (toolInvocationPart) {
|
|
751
|
+
toolInvocationPart.toolInvocation = {
|
|
752
|
+
...toolInvocationPart.toolInvocation,
|
|
753
|
+
state: "result",
|
|
754
|
+
result
|
|
755
|
+
};
|
|
756
|
+
}
|
|
757
|
+
const toolInvocation = lastMessage?.toolInvocations?.find(
|
|
758
|
+
(toolInvocation2) => toolInvocation2.toolCallId === toolCall2.toolCallId
|
|
759
|
+
);
|
|
760
|
+
if (toolInvocation) {
|
|
761
|
+
toolInvocation.state = "result";
|
|
762
|
+
toolInvocation.result = result;
|
|
763
|
+
}
|
|
764
|
+
const writer = writable.getWriter();
|
|
765
|
+
try {
|
|
766
|
+
await writer.write(
|
|
767
|
+
new TextEncoder().encode(
|
|
768
|
+
"a:" + JSON.stringify({
|
|
769
|
+
toolCallId: toolCall2.toolCallId,
|
|
770
|
+
result
|
|
771
|
+
}) + "\n"
|
|
772
|
+
)
|
|
773
|
+
);
|
|
774
|
+
} finally {
|
|
775
|
+
writer.releaseLock();
|
|
776
|
+
}
|
|
777
|
+
const originalMessages = processedParams.messages;
|
|
778
|
+
const messageArray = Array.isArray(originalMessages) ? originalMessages : [originalMessages];
|
|
779
|
+
this.processStreamResponse(
|
|
780
|
+
{
|
|
781
|
+
...processedParams,
|
|
782
|
+
messages: [...messageArray, ...messages, lastMessage]
|
|
783
|
+
},
|
|
784
|
+
writable
|
|
785
|
+
);
|
|
755
786
|
}
|
|
756
|
-
);
|
|
757
|
-
const lastMessage = JSON.parse(JSON.stringify(messages[messages.length - 1]));
|
|
758
|
-
const toolInvocationPart = lastMessage?.parts?.find(
|
|
759
|
-
(part) => part.type === "tool-invocation" && part.toolInvocation?.toolCallId === toolCall.toolCallId
|
|
760
|
-
);
|
|
761
|
-
if (toolInvocationPart) {
|
|
762
|
-
toolInvocationPart.toolInvocation = {
|
|
763
|
-
...toolInvocationPart.toolInvocation,
|
|
764
|
-
state: "result",
|
|
765
|
-
result
|
|
766
|
-
};
|
|
767
787
|
}
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
}
|
|
775
|
-
const originalMessages = processedParams.messages;
|
|
776
|
-
const messageArray = Array.isArray(originalMessages) ? originalMessages : [originalMessages];
|
|
777
|
-
this.processStreamResponse(
|
|
778
|
-
{
|
|
779
|
-
...processedParams,
|
|
780
|
-
messages: [...messageArray, ...messages, lastMessage]
|
|
781
|
-
},
|
|
782
|
-
writable
|
|
783
|
-
);
|
|
788
|
+
} else {
|
|
789
|
+
setTimeout(() => {
|
|
790
|
+
if (!writable.locked) {
|
|
791
|
+
writable.close();
|
|
792
|
+
}
|
|
793
|
+
}, 0);
|
|
784
794
|
}
|
|
785
|
-
}
|
|
786
|
-
|
|
795
|
+
},
|
|
796
|
+
lastMessage: void 0,
|
|
797
|
+
streamProtocol
|
|
798
|
+
});
|
|
787
799
|
} catch (error) {
|
|
788
800
|
console.error("Error processing stream response:", error);
|
|
789
801
|
}
|
|
790
802
|
return response;
|
|
791
803
|
}
|
|
804
|
+
/**
|
|
805
|
+
* Streams a response from the agent
|
|
806
|
+
* @param params - Stream parameters including prompt
|
|
807
|
+
* @returns Promise containing the enhanced Response object with processDataStream and processTextStream methods
|
|
808
|
+
*/
|
|
809
|
+
async stream(params) {
|
|
810
|
+
const processedParams = {
|
|
811
|
+
...params,
|
|
812
|
+
output: params.output ? zodToJsonSchema(params.output) : void 0,
|
|
813
|
+
experimental_output: params.experimental_output ? zodToJsonSchema(params.experimental_output) : void 0,
|
|
814
|
+
runtimeContext: parseClientRuntimeContext(params.runtimeContext),
|
|
815
|
+
clientTools: processClientTools(params.clientTools)
|
|
816
|
+
};
|
|
817
|
+
const { readable, writable } = new TransformStream();
|
|
818
|
+
const response = await this.processStreamResponse(processedParams, writable);
|
|
819
|
+
const streamResponse = new Response(readable, {
|
|
820
|
+
status: response.status,
|
|
821
|
+
statusText: response.statusText,
|
|
822
|
+
headers: response.headers
|
|
823
|
+
});
|
|
824
|
+
streamResponse.processDataStream = async (options = {}) => {
|
|
825
|
+
await processDataStream({
|
|
826
|
+
stream: streamResponse.body,
|
|
827
|
+
...options
|
|
828
|
+
});
|
|
829
|
+
};
|
|
830
|
+
streamResponse.processTextStream = async (options) => {
|
|
831
|
+
await processTextStream({
|
|
832
|
+
stream: streamResponse.body,
|
|
833
|
+
onTextPart: options?.onTextPart ?? (() => {
|
|
834
|
+
})
|
|
835
|
+
});
|
|
836
|
+
};
|
|
837
|
+
return streamResponse;
|
|
838
|
+
}
|
|
792
839
|
/**
|
|
793
840
|
* Gets details about a specific tool available to the agent
|
|
794
841
|
* @param toolId - ID of the tool to retrieve
|
|
@@ -1286,10 +1333,10 @@ var Workflow = class extends BaseResource {
|
|
|
1286
1333
|
if (params?.toDate) {
|
|
1287
1334
|
searchParams.set("toDate", params.toDate.toISOString());
|
|
1288
1335
|
}
|
|
1289
|
-
if (params?.limit !== void 0) {
|
|
1336
|
+
if (params?.limit !== null && params?.limit !== void 0 && !isNaN(Number(params?.limit))) {
|
|
1290
1337
|
searchParams.set("limit", String(params.limit));
|
|
1291
1338
|
}
|
|
1292
|
-
if (params?.offset !== void 0) {
|
|
1339
|
+
if (params?.offset !== null && params?.offset !== void 0 && !isNaN(Number(params?.offset))) {
|
|
1293
1340
|
searchParams.set("offset", String(params.offset));
|
|
1294
1341
|
}
|
|
1295
1342
|
if (params?.resourceId) {
|
|
@@ -1317,6 +1364,27 @@ var Workflow = class extends BaseResource {
|
|
|
1317
1364
|
runExecutionResult(runId) {
|
|
1318
1365
|
return this.request(`/api/workflows/${this.workflowId}/runs/${runId}/execution-result`);
|
|
1319
1366
|
}
|
|
1367
|
+
/**
|
|
1368
|
+
* Cancels a specific workflow run by its ID
|
|
1369
|
+
* @param runId - The ID of the workflow run to cancel
|
|
1370
|
+
* @returns Promise containing a success message
|
|
1371
|
+
*/
|
|
1372
|
+
cancelRun(runId) {
|
|
1373
|
+
return this.request(`/api/workflows/${this.workflowId}/runs/${runId}/cancel`, {
|
|
1374
|
+
method: "POST"
|
|
1375
|
+
});
|
|
1376
|
+
}
|
|
1377
|
+
/**
|
|
1378
|
+
* Sends an event to a specific workflow run by its ID
|
|
1379
|
+
* @param params - Object containing the runId, event and data
|
|
1380
|
+
* @returns Promise containing a success message
|
|
1381
|
+
*/
|
|
1382
|
+
sendRunEvent(params) {
|
|
1383
|
+
return this.request(`/api/workflows/${this.workflowId}/runs/${params.runId}/send-event`, {
|
|
1384
|
+
method: "POST",
|
|
1385
|
+
body: { event: params.event, data: params.data }
|
|
1386
|
+
});
|
|
1387
|
+
}
|
|
1320
1388
|
/**
|
|
1321
1389
|
* Creates a new workflow run
|
|
1322
1390
|
* @param params - Optional object containing the optional runId
|
|
@@ -1406,6 +1474,7 @@ var Workflow = class extends BaseResource {
|
|
|
1406
1474
|
if (!response.body) {
|
|
1407
1475
|
throw new Error("Response body is null");
|
|
1408
1476
|
}
|
|
1477
|
+
let failedChunk = void 0;
|
|
1409
1478
|
const transformStream = new TransformStream({
|
|
1410
1479
|
start() {
|
|
1411
1480
|
},
|
|
@@ -1415,10 +1484,13 @@ var Workflow = class extends BaseResource {
|
|
|
1415
1484
|
const chunks = decoded.split(RECORD_SEPARATOR2);
|
|
1416
1485
|
for (const chunk2 of chunks) {
|
|
1417
1486
|
if (chunk2) {
|
|
1487
|
+
const newChunk = failedChunk ? failedChunk + chunk2 : chunk2;
|
|
1418
1488
|
try {
|
|
1419
|
-
const parsedChunk = JSON.parse(
|
|
1489
|
+
const parsedChunk = JSON.parse(newChunk);
|
|
1420
1490
|
controller.enqueue(parsedChunk);
|
|
1421
|
-
|
|
1491
|
+
failedChunk = void 0;
|
|
1492
|
+
} catch (error) {
|
|
1493
|
+
failedChunk = newChunk;
|
|
1422
1494
|
}
|
|
1423
1495
|
}
|
|
1424
1496
|
}
|
|
@@ -1600,6 +1672,54 @@ var MCPTool = class extends BaseResource {
|
|
|
1600
1672
|
}
|
|
1601
1673
|
};
|
|
1602
1674
|
|
|
1675
|
+
// src/resources/network-memory-thread.ts
|
|
1676
|
+
var NetworkMemoryThread = class extends BaseResource {
|
|
1677
|
+
constructor(options, threadId, networkId) {
|
|
1678
|
+
super(options);
|
|
1679
|
+
this.threadId = threadId;
|
|
1680
|
+
this.networkId = networkId;
|
|
1681
|
+
}
|
|
1682
|
+
/**
|
|
1683
|
+
* Retrieves the memory thread details
|
|
1684
|
+
* @returns Promise containing thread details including title and metadata
|
|
1685
|
+
*/
|
|
1686
|
+
get() {
|
|
1687
|
+
return this.request(`/api/memory/network/threads/${this.threadId}?networkId=${this.networkId}`);
|
|
1688
|
+
}
|
|
1689
|
+
/**
|
|
1690
|
+
* Updates the memory thread properties
|
|
1691
|
+
* @param params - Update parameters including title and metadata
|
|
1692
|
+
* @returns Promise containing updated thread details
|
|
1693
|
+
*/
|
|
1694
|
+
update(params) {
|
|
1695
|
+
return this.request(`/api/memory/network/threads/${this.threadId}?networkId=${this.networkId}`, {
|
|
1696
|
+
method: "PATCH",
|
|
1697
|
+
body: params
|
|
1698
|
+
});
|
|
1699
|
+
}
|
|
1700
|
+
/**
|
|
1701
|
+
* Deletes the memory thread
|
|
1702
|
+
* @returns Promise containing deletion result
|
|
1703
|
+
*/
|
|
1704
|
+
delete() {
|
|
1705
|
+
return this.request(`/api/memory/network/threads/${this.threadId}?networkId=${this.networkId}`, {
|
|
1706
|
+
method: "DELETE"
|
|
1707
|
+
});
|
|
1708
|
+
}
|
|
1709
|
+
/**
|
|
1710
|
+
* Retrieves messages associated with the thread
|
|
1711
|
+
* @param params - Optional parameters including limit for number of messages to retrieve
|
|
1712
|
+
* @returns Promise containing thread messages and UI messages
|
|
1713
|
+
*/
|
|
1714
|
+
getMessages(params) {
|
|
1715
|
+
const query = new URLSearchParams({
|
|
1716
|
+
networkId: this.networkId,
|
|
1717
|
+
...params?.limit ? { limit: params.limit.toString() } : {}
|
|
1718
|
+
});
|
|
1719
|
+
return this.request(`/api/memory/network/threads/${this.threadId}/messages?${query.toString()}`);
|
|
1720
|
+
}
|
|
1721
|
+
};
|
|
1722
|
+
|
|
1603
1723
|
// src/resources/vNextNetwork.ts
|
|
1604
1724
|
var RECORD_SEPARATOR3 = "";
|
|
1605
1725
|
var VNextNetwork = class extends BaseResource {
|
|
@@ -1622,7 +1742,10 @@ var VNextNetwork = class extends BaseResource {
|
|
|
1622
1742
|
generate(params) {
|
|
1623
1743
|
return this.request(`/api/networks/v-next/${this.networkId}/generate`, {
|
|
1624
1744
|
method: "POST",
|
|
1625
|
-
body:
|
|
1745
|
+
body: {
|
|
1746
|
+
...params,
|
|
1747
|
+
runtimeContext: parseClientRuntimeContext(params.runtimeContext)
|
|
1748
|
+
}
|
|
1626
1749
|
});
|
|
1627
1750
|
}
|
|
1628
1751
|
/**
|
|
@@ -1633,7 +1756,10 @@ var VNextNetwork = class extends BaseResource {
|
|
|
1633
1756
|
loop(params) {
|
|
1634
1757
|
return this.request(`/api/networks/v-next/${this.networkId}/loop`, {
|
|
1635
1758
|
method: "POST",
|
|
1636
|
-
body:
|
|
1759
|
+
body: {
|
|
1760
|
+
...params,
|
|
1761
|
+
runtimeContext: parseClientRuntimeContext(params.runtimeContext)
|
|
1762
|
+
}
|
|
1637
1763
|
});
|
|
1638
1764
|
}
|
|
1639
1765
|
async *streamProcessor(stream) {
|
|
@@ -1682,7 +1808,10 @@ var VNextNetwork = class extends BaseResource {
|
|
|
1682
1808
|
async stream(params, onRecord) {
|
|
1683
1809
|
const response = await this.request(`/api/networks/v-next/${this.networkId}/stream`, {
|
|
1684
1810
|
method: "POST",
|
|
1685
|
-
body:
|
|
1811
|
+
body: {
|
|
1812
|
+
...params,
|
|
1813
|
+
runtimeContext: parseClientRuntimeContext(params.runtimeContext)
|
|
1814
|
+
},
|
|
1686
1815
|
stream: true
|
|
1687
1816
|
});
|
|
1688
1817
|
if (!response.ok) {
|
|
@@ -1699,53 +1828,33 @@ var VNextNetwork = class extends BaseResource {
|
|
|
1699
1828
|
}
|
|
1700
1829
|
}
|
|
1701
1830
|
}
|
|
1702
|
-
};
|
|
1703
|
-
|
|
1704
|
-
// src/resources/network-memory-thread.ts
|
|
1705
|
-
var NetworkMemoryThread = class extends BaseResource {
|
|
1706
|
-
constructor(options, threadId, networkId) {
|
|
1707
|
-
super(options);
|
|
1708
|
-
this.threadId = threadId;
|
|
1709
|
-
this.networkId = networkId;
|
|
1710
|
-
}
|
|
1711
|
-
/**
|
|
1712
|
-
* Retrieves the memory thread details
|
|
1713
|
-
* @returns Promise containing thread details including title and metadata
|
|
1714
|
-
*/
|
|
1715
|
-
get() {
|
|
1716
|
-
return this.request(`/api/memory/network/threads/${this.threadId}?networkId=${this.networkId}`);
|
|
1717
|
-
}
|
|
1718
|
-
/**
|
|
1719
|
-
* Updates the memory thread properties
|
|
1720
|
-
* @param params - Update parameters including title and metadata
|
|
1721
|
-
* @returns Promise containing updated thread details
|
|
1722
|
-
*/
|
|
1723
|
-
update(params) {
|
|
1724
|
-
return this.request(`/api/memory/network/threads/${this.threadId}?networkId=${this.networkId}`, {
|
|
1725
|
-
method: "PATCH",
|
|
1726
|
-
body: params
|
|
1727
|
-
});
|
|
1728
|
-
}
|
|
1729
1831
|
/**
|
|
1730
|
-
*
|
|
1731
|
-
* @
|
|
1732
|
-
|
|
1733
|
-
delete() {
|
|
1734
|
-
return this.request(`/api/memory/network/threads/${this.threadId}?networkId=${this.networkId}`, {
|
|
1735
|
-
method: "DELETE"
|
|
1736
|
-
});
|
|
1737
|
-
}
|
|
1738
|
-
/**
|
|
1739
|
-
* Retrieves messages associated with the thread
|
|
1740
|
-
* @param params - Optional parameters including limit for number of messages to retrieve
|
|
1741
|
-
* @returns Promise containing thread messages and UI messages
|
|
1832
|
+
* Streams a response from the v-next network loop
|
|
1833
|
+
* @param params - Stream parameters including message
|
|
1834
|
+
* @returns Promise containing the results
|
|
1742
1835
|
*/
|
|
1743
|
-
|
|
1744
|
-
const
|
|
1745
|
-
|
|
1746
|
-
|
|
1836
|
+
async loopStream(params, onRecord) {
|
|
1837
|
+
const response = await this.request(`/api/networks/v-next/${this.networkId}/loop-stream`, {
|
|
1838
|
+
method: "POST",
|
|
1839
|
+
body: {
|
|
1840
|
+
...params,
|
|
1841
|
+
runtimeContext: parseClientRuntimeContext(params.runtimeContext)
|
|
1842
|
+
},
|
|
1843
|
+
stream: true
|
|
1747
1844
|
});
|
|
1748
|
-
|
|
1845
|
+
if (!response.ok) {
|
|
1846
|
+
throw new Error(`Failed to stream vNext network loop: ${response.statusText}`);
|
|
1847
|
+
}
|
|
1848
|
+
if (!response.body) {
|
|
1849
|
+
throw new Error("Response body is null");
|
|
1850
|
+
}
|
|
1851
|
+
for await (const record of this.streamProcessor(response.body)) {
|
|
1852
|
+
if (typeof record === "string") {
|
|
1853
|
+
onRecord(JSON.parse(record));
|
|
1854
|
+
} else {
|
|
1855
|
+
onRecord(record);
|
|
1856
|
+
}
|
|
1857
|
+
}
|
|
1749
1858
|
}
|
|
1750
1859
|
};
|
|
1751
1860
|
|
|
@@ -2143,6 +2252,41 @@ var MastraClient = class extends BaseResource {
|
|
|
2143
2252
|
getA2A(agentId) {
|
|
2144
2253
|
return new A2A(this.options, agentId);
|
|
2145
2254
|
}
|
|
2255
|
+
/**
|
|
2256
|
+
* Retrieves the working memory for a specific thread (optionally resource-scoped).
|
|
2257
|
+
* @param agentId - ID of the agent.
|
|
2258
|
+
* @param threadId - ID of the thread.
|
|
2259
|
+
* @param resourceId - Optional ID of the resource.
|
|
2260
|
+
* @returns Working memory for the specified thread or resource.
|
|
2261
|
+
*/
|
|
2262
|
+
getWorkingMemory({
|
|
2263
|
+
agentId,
|
|
2264
|
+
threadId,
|
|
2265
|
+
resourceId
|
|
2266
|
+
}) {
|
|
2267
|
+
return this.request(`/api/memory/threads/${threadId}/working-memory?agentId=${agentId}&resourceId=${resourceId}`);
|
|
2268
|
+
}
|
|
2269
|
+
/**
|
|
2270
|
+
* Updates the working memory for a specific thread (optionally resource-scoped).
|
|
2271
|
+
* @param agentId - ID of the agent.
|
|
2272
|
+
* @param threadId - ID of the thread.
|
|
2273
|
+
* @param workingMemory - The new working memory content.
|
|
2274
|
+
* @param resourceId - Optional ID of the resource.
|
|
2275
|
+
*/
|
|
2276
|
+
updateWorkingMemory({
|
|
2277
|
+
agentId,
|
|
2278
|
+
threadId,
|
|
2279
|
+
workingMemory,
|
|
2280
|
+
resourceId
|
|
2281
|
+
}) {
|
|
2282
|
+
return this.request(`/api/memory/threads/${threadId}/working-memory?agentId=${agentId}`, {
|
|
2283
|
+
method: "POST",
|
|
2284
|
+
body: {
|
|
2285
|
+
workingMemory,
|
|
2286
|
+
resourceId
|
|
2287
|
+
}
|
|
2288
|
+
});
|
|
2289
|
+
}
|
|
2146
2290
|
};
|
|
2147
2291
|
|
|
2148
2292
|
export { MastraClient };
|