@mastra/client-js 0.0.0-zod-v4-compat-part-2-20250820135355 → 0.0.0-zod-v4-stuff-20250825154219
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 +18 -0
- package/CHANGELOG.md +266 -2
- package/dist/adapters/agui.d.ts +1 -1
- package/dist/adapters/agui.d.ts.map +1 -1
- package/dist/client.d.ts +9 -4
- package/dist/client.d.ts.map +1 -1
- package/dist/index.cjs +566 -31
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.js +566 -31
- package/dist/index.js.map +1 -1
- package/dist/resources/a2a.d.ts +15 -18
- package/dist/resources/a2a.d.ts.map +1 -1
- package/dist/resources/agent.d.ts +21 -2
- package/dist/resources/agent.d.ts.map +1 -1
- package/dist/resources/base.d.ts +1 -1
- package/dist/resources/index.d.ts +10 -10
- package/dist/resources/legacy-workflow.d.ts +2 -2
- package/dist/resources/mcp-tool.d.ts +2 -2
- package/dist/resources/memory-thread.d.ts +2 -2
- package/dist/resources/network-memory-thread.d.ts +2 -2
- package/dist/resources/network.d.ts +2 -2
- package/dist/resources/tool.d.ts +2 -2
- package/dist/resources/vNextNetwork.d.ts +2 -2
- package/dist/resources/vector.d.ts +2 -2
- package/dist/resources/workflow.d.ts +2 -2
- package/dist/types.d.ts +14 -2
- package/dist/types.d.ts.map +1 -1
- package/dist/utils/process-mastra-stream.d.ts +7 -0
- package/dist/utils/process-mastra-stream.d.ts.map +1 -0
- package/dist/utils/zod-to-json-schema.d.ts.map +1 -1
- package/package.json +8 -6
- package/src/adapters/agui.ts +29 -11
- package/src/client.ts +8 -0
- package/src/resources/a2a.ts +35 -25
- package/src/resources/agent.ts +660 -2
- package/src/types.ts +22 -1
- package/src/utils/process-mastra-stream.test.ts +353 -0
- package/src/utils/process-mastra-stream.ts +49 -0
- package/src/utils/zod-to-json-schema.ts +1 -0
- package/tsup.config.ts +2 -7
package/dist/index.cjs
CHANGED
|
@@ -151,6 +151,12 @@ function generateUUID() {
|
|
|
151
151
|
}
|
|
152
152
|
function convertMessagesToMastraMessages(messages) {
|
|
153
153
|
const result = [];
|
|
154
|
+
const toolCallsWithResults = /* @__PURE__ */ new Set();
|
|
155
|
+
for (const message of messages) {
|
|
156
|
+
if (message.role === "tool" && message.toolCallId) {
|
|
157
|
+
toolCallsWithResults.add(message.toolCallId);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
154
160
|
for (const message of messages) {
|
|
155
161
|
if (message.role === "assistant") {
|
|
156
162
|
const parts = message.content ? [{ type: "text", text: message.content }] : [];
|
|
@@ -167,15 +173,22 @@ function convertMessagesToMastraMessages(messages) {
|
|
|
167
173
|
content: parts
|
|
168
174
|
});
|
|
169
175
|
if (message.toolCalls?.length) {
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
176
|
+
for (const toolCall of message.toolCalls) {
|
|
177
|
+
if (!toolCallsWithResults.has(toolCall.id)) {
|
|
178
|
+
result.push({
|
|
179
|
+
role: "tool",
|
|
180
|
+
content: [
|
|
181
|
+
{
|
|
182
|
+
type: "tool-result",
|
|
183
|
+
toolCallId: toolCall.id,
|
|
184
|
+
toolName: toolCall.function.name,
|
|
185
|
+
result: JSON.parse(toolCall.function.arguments)
|
|
186
|
+
// This is still wrong but matches test expectations
|
|
187
|
+
}
|
|
188
|
+
]
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
}
|
|
179
192
|
}
|
|
180
193
|
} else if (message.role === "user") {
|
|
181
194
|
result.push({
|
|
@@ -188,8 +201,9 @@ function convertMessagesToMastraMessages(messages) {
|
|
|
188
201
|
content: [
|
|
189
202
|
{
|
|
190
203
|
type: "tool-result",
|
|
191
|
-
toolCallId: message.toolCallId,
|
|
204
|
+
toolCallId: message.toolCallId || "unknown",
|
|
192
205
|
toolName: "unknown",
|
|
206
|
+
// toolName is not available in tool messages from CopilotKit
|
|
193
207
|
result: message.content
|
|
194
208
|
}
|
|
195
209
|
]
|
|
@@ -307,6 +321,94 @@ function parseClientRuntimeContext(runtimeContext$1) {
|
|
|
307
321
|
}
|
|
308
322
|
return void 0;
|
|
309
323
|
}
|
|
324
|
+
|
|
325
|
+
// src/utils/process-mastra-stream.ts
|
|
326
|
+
async function processMastraStream({
|
|
327
|
+
stream,
|
|
328
|
+
onChunk
|
|
329
|
+
}) {
|
|
330
|
+
const reader = stream.getReader();
|
|
331
|
+
const decoder = new TextDecoder();
|
|
332
|
+
let buffer = "";
|
|
333
|
+
try {
|
|
334
|
+
while (true) {
|
|
335
|
+
const { done, value } = await reader.read();
|
|
336
|
+
if (done) break;
|
|
337
|
+
buffer += decoder.decode(value, { stream: true });
|
|
338
|
+
const lines = buffer.split("\n\n");
|
|
339
|
+
buffer = lines.pop() || "";
|
|
340
|
+
for (const line of lines) {
|
|
341
|
+
if (line.startsWith("data: ")) {
|
|
342
|
+
const data = line.slice(6);
|
|
343
|
+
if (data === "[DONE]") {
|
|
344
|
+
console.log("\u{1F3C1} Stream finished");
|
|
345
|
+
return;
|
|
346
|
+
}
|
|
347
|
+
try {
|
|
348
|
+
const json = JSON.parse(data);
|
|
349
|
+
await onChunk(json);
|
|
350
|
+
} catch (error) {
|
|
351
|
+
console.error("\u274C JSON parse error:", error, "Data:", data);
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
} finally {
|
|
357
|
+
reader.releaseLock();
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
// src/resources/agent.ts
|
|
362
|
+
async function executeToolCallAndRespond({
|
|
363
|
+
response,
|
|
364
|
+
params,
|
|
365
|
+
runId,
|
|
366
|
+
resourceId,
|
|
367
|
+
threadId,
|
|
368
|
+
runtimeContext,
|
|
369
|
+
respondFn
|
|
370
|
+
}) {
|
|
371
|
+
if (response.finishReason === "tool-calls") {
|
|
372
|
+
const toolCalls = response.toolCalls;
|
|
373
|
+
if (!toolCalls || !Array.isArray(toolCalls)) {
|
|
374
|
+
return response;
|
|
375
|
+
}
|
|
376
|
+
for (const toolCall of toolCalls) {
|
|
377
|
+
const clientTool = params.clientTools?.[toolCall.toolName];
|
|
378
|
+
if (clientTool && clientTool.execute) {
|
|
379
|
+
const result = await clientTool.execute(
|
|
380
|
+
{ context: toolCall?.args, runId, resourceId, threadId, runtimeContext },
|
|
381
|
+
{
|
|
382
|
+
messages: response.messages,
|
|
383
|
+
toolCallId: toolCall?.toolCallId
|
|
384
|
+
}
|
|
385
|
+
);
|
|
386
|
+
const updatedMessages = [
|
|
387
|
+
{
|
|
388
|
+
role: "user",
|
|
389
|
+
content: params.messages
|
|
390
|
+
},
|
|
391
|
+
...response.response.messages,
|
|
392
|
+
{
|
|
393
|
+
role: "tool",
|
|
394
|
+
content: [
|
|
395
|
+
{
|
|
396
|
+
type: "tool-result",
|
|
397
|
+
toolCallId: toolCall.toolCallId,
|
|
398
|
+
toolName: toolCall.toolName,
|
|
399
|
+
result
|
|
400
|
+
}
|
|
401
|
+
]
|
|
402
|
+
}
|
|
403
|
+
];
|
|
404
|
+
return respondFn({
|
|
405
|
+
...params,
|
|
406
|
+
messages: updatedMessages
|
|
407
|
+
});
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
}
|
|
310
412
|
var AgentVoice = class extends BaseResource {
|
|
311
413
|
constructor(options, agentId) {
|
|
312
414
|
super(options);
|
|
@@ -433,6 +535,34 @@ var Agent = class extends BaseResource {
|
|
|
433
535
|
}
|
|
434
536
|
return response;
|
|
435
537
|
}
|
|
538
|
+
async generateVNext(params) {
|
|
539
|
+
const processedParams = {
|
|
540
|
+
...params,
|
|
541
|
+
output: params.output ? zodToJsonSchema(params.output) : void 0,
|
|
542
|
+
runtimeContext: parseClientRuntimeContext(params.runtimeContext),
|
|
543
|
+
clientTools: processClientTools(params.clientTools)
|
|
544
|
+
};
|
|
545
|
+
const { runId, resourceId, threadId, runtimeContext } = processedParams;
|
|
546
|
+
const response = await this.request(
|
|
547
|
+
`/api/agents/${this.agentId}/generate/vnext`,
|
|
548
|
+
{
|
|
549
|
+
method: "POST",
|
|
550
|
+
body: processedParams
|
|
551
|
+
}
|
|
552
|
+
);
|
|
553
|
+
if (response.finishReason === "tool-calls") {
|
|
554
|
+
return executeToolCallAndRespond({
|
|
555
|
+
response,
|
|
556
|
+
params,
|
|
557
|
+
runId,
|
|
558
|
+
resourceId,
|
|
559
|
+
threadId,
|
|
560
|
+
runtimeContext,
|
|
561
|
+
respondFn: this.generateVNext.bind(this)
|
|
562
|
+
});
|
|
563
|
+
}
|
|
564
|
+
return response;
|
|
565
|
+
}
|
|
436
566
|
async processChatResponse({
|
|
437
567
|
stream,
|
|
438
568
|
update,
|
|
@@ -723,6 +853,392 @@ var Agent = class extends BaseResource {
|
|
|
723
853
|
};
|
|
724
854
|
return streamResponse;
|
|
725
855
|
}
|
|
856
|
+
async processChatResponse_vNext({
|
|
857
|
+
stream,
|
|
858
|
+
update,
|
|
859
|
+
onToolCall,
|
|
860
|
+
onFinish,
|
|
861
|
+
getCurrentDate = () => /* @__PURE__ */ new Date(),
|
|
862
|
+
lastMessage
|
|
863
|
+
}) {
|
|
864
|
+
const replaceLastMessage = lastMessage?.role === "assistant";
|
|
865
|
+
let step = replaceLastMessage ? 1 + // find max step in existing tool invocations:
|
|
866
|
+
(lastMessage.toolInvocations?.reduce((max, toolInvocation) => {
|
|
867
|
+
return Math.max(max, toolInvocation.step ?? 0);
|
|
868
|
+
}, 0) ?? 0) : 0;
|
|
869
|
+
const message = replaceLastMessage ? structuredClone(lastMessage) : {
|
|
870
|
+
id: uuid.v4(),
|
|
871
|
+
createdAt: getCurrentDate(),
|
|
872
|
+
role: "assistant",
|
|
873
|
+
content: "",
|
|
874
|
+
parts: []
|
|
875
|
+
};
|
|
876
|
+
let currentTextPart = void 0;
|
|
877
|
+
let currentReasoningPart = void 0;
|
|
878
|
+
let currentReasoningTextDetail = void 0;
|
|
879
|
+
function updateToolInvocationPart(toolCallId, invocation) {
|
|
880
|
+
const part = message.parts.find(
|
|
881
|
+
(part2) => part2.type === "tool-invocation" && part2.toolInvocation.toolCallId === toolCallId
|
|
882
|
+
);
|
|
883
|
+
if (part != null) {
|
|
884
|
+
part.toolInvocation = invocation;
|
|
885
|
+
} else {
|
|
886
|
+
message.parts.push({
|
|
887
|
+
type: "tool-invocation",
|
|
888
|
+
toolInvocation: invocation
|
|
889
|
+
});
|
|
890
|
+
}
|
|
891
|
+
}
|
|
892
|
+
const data = [];
|
|
893
|
+
let messageAnnotations = replaceLastMessage ? lastMessage?.annotations : void 0;
|
|
894
|
+
const partialToolCalls = {};
|
|
895
|
+
let usage = {
|
|
896
|
+
completionTokens: NaN,
|
|
897
|
+
promptTokens: NaN,
|
|
898
|
+
totalTokens: NaN
|
|
899
|
+
};
|
|
900
|
+
let finishReason = "unknown";
|
|
901
|
+
function execUpdate() {
|
|
902
|
+
const copiedData = [...data];
|
|
903
|
+
if (messageAnnotations?.length) {
|
|
904
|
+
message.annotations = messageAnnotations;
|
|
905
|
+
}
|
|
906
|
+
const copiedMessage = {
|
|
907
|
+
// deep copy the message to ensure that deep changes (msg attachments) are updated
|
|
908
|
+
// with SolidJS. SolidJS uses referential integration of sub-objects to detect changes.
|
|
909
|
+
...structuredClone(message),
|
|
910
|
+
// add a revision id to ensure that the message is updated with SWR. SWR uses a
|
|
911
|
+
// hashing approach by default to detect changes, but it only works for shallow
|
|
912
|
+
// changes. This is why we need to add a revision id to ensure that the message
|
|
913
|
+
// is updated with SWR (without it, the changes get stuck in SWR and are not
|
|
914
|
+
// forwarded to rendering):
|
|
915
|
+
revisionId: uuid.v4()
|
|
916
|
+
};
|
|
917
|
+
update({
|
|
918
|
+
message: copiedMessage,
|
|
919
|
+
data: copiedData,
|
|
920
|
+
replaceLastMessage
|
|
921
|
+
});
|
|
922
|
+
}
|
|
923
|
+
await processMastraStream({
|
|
924
|
+
stream,
|
|
925
|
+
// TODO: casting as any here because the stream types were all typed as any before in core.
|
|
926
|
+
// but this is completely wrong and this fn is probably broken. Remove ":any" and you'll see a bunch of type errors
|
|
927
|
+
onChunk: async (chunk) => {
|
|
928
|
+
switch (chunk.type) {
|
|
929
|
+
case "step-start": {
|
|
930
|
+
if (!replaceLastMessage) {
|
|
931
|
+
message.id = chunk.payload.messageId;
|
|
932
|
+
}
|
|
933
|
+
message.parts.push({ type: "step-start" });
|
|
934
|
+
execUpdate();
|
|
935
|
+
break;
|
|
936
|
+
}
|
|
937
|
+
case "text-delta": {
|
|
938
|
+
if (currentTextPart == null) {
|
|
939
|
+
currentTextPart = {
|
|
940
|
+
type: "text",
|
|
941
|
+
text: chunk.payload.text
|
|
942
|
+
};
|
|
943
|
+
message.parts.push(currentTextPart);
|
|
944
|
+
} else {
|
|
945
|
+
currentTextPart.text += chunk.payload.text;
|
|
946
|
+
}
|
|
947
|
+
message.content += chunk.payload.text;
|
|
948
|
+
execUpdate();
|
|
949
|
+
break;
|
|
950
|
+
}
|
|
951
|
+
case "reasoning-delta": {
|
|
952
|
+
if (currentReasoningTextDetail == null) {
|
|
953
|
+
currentReasoningTextDetail = { type: "text", text: chunk.payload.text };
|
|
954
|
+
if (currentReasoningPart != null) {
|
|
955
|
+
currentReasoningPart.details.push(currentReasoningTextDetail);
|
|
956
|
+
}
|
|
957
|
+
} else {
|
|
958
|
+
currentReasoningTextDetail.text += chunk.payload.text;
|
|
959
|
+
}
|
|
960
|
+
if (currentReasoningPart == null) {
|
|
961
|
+
currentReasoningPart = {
|
|
962
|
+
type: "reasoning",
|
|
963
|
+
reasoning: chunk.payload.text,
|
|
964
|
+
details: [currentReasoningTextDetail]
|
|
965
|
+
};
|
|
966
|
+
message.parts.push(currentReasoningPart);
|
|
967
|
+
} else {
|
|
968
|
+
currentReasoningPart.reasoning += chunk.payload.text;
|
|
969
|
+
}
|
|
970
|
+
message.reasoning = (message.reasoning ?? "") + chunk.payload.text;
|
|
971
|
+
execUpdate();
|
|
972
|
+
break;
|
|
973
|
+
}
|
|
974
|
+
case "file": {
|
|
975
|
+
message.parts.push({
|
|
976
|
+
type: "file",
|
|
977
|
+
mimeType: chunk.payload.mimeType,
|
|
978
|
+
data: chunk.payload.data
|
|
979
|
+
});
|
|
980
|
+
execUpdate();
|
|
981
|
+
break;
|
|
982
|
+
}
|
|
983
|
+
case "source": {
|
|
984
|
+
message.parts.push({
|
|
985
|
+
type: "source",
|
|
986
|
+
source: chunk.payload.source
|
|
987
|
+
});
|
|
988
|
+
execUpdate();
|
|
989
|
+
break;
|
|
990
|
+
}
|
|
991
|
+
case "tool-call": {
|
|
992
|
+
const invocation = {
|
|
993
|
+
state: "call",
|
|
994
|
+
step,
|
|
995
|
+
...chunk.payload
|
|
996
|
+
};
|
|
997
|
+
if (partialToolCalls[chunk.payload.toolCallId] != null) {
|
|
998
|
+
message.toolInvocations[partialToolCalls[chunk.payload.toolCallId].index] = invocation;
|
|
999
|
+
} else {
|
|
1000
|
+
if (message.toolInvocations == null) {
|
|
1001
|
+
message.toolInvocations = [];
|
|
1002
|
+
}
|
|
1003
|
+
message.toolInvocations.push(invocation);
|
|
1004
|
+
}
|
|
1005
|
+
updateToolInvocationPart(chunk.payload.toolCallId, invocation);
|
|
1006
|
+
execUpdate();
|
|
1007
|
+
if (onToolCall) {
|
|
1008
|
+
const result = await onToolCall({ toolCall: chunk.payload });
|
|
1009
|
+
if (result != null) {
|
|
1010
|
+
const invocation2 = {
|
|
1011
|
+
state: "result",
|
|
1012
|
+
step,
|
|
1013
|
+
...chunk.payload,
|
|
1014
|
+
result
|
|
1015
|
+
};
|
|
1016
|
+
message.toolInvocations[message.toolInvocations.length - 1] = invocation2;
|
|
1017
|
+
updateToolInvocationPart(chunk.payload.toolCallId, invocation2);
|
|
1018
|
+
execUpdate();
|
|
1019
|
+
}
|
|
1020
|
+
}
|
|
1021
|
+
}
|
|
1022
|
+
case "tool-call-input-streaming-start": {
|
|
1023
|
+
if (message.toolInvocations == null) {
|
|
1024
|
+
message.toolInvocations = [];
|
|
1025
|
+
}
|
|
1026
|
+
partialToolCalls[chunk.payload.toolCallId] = {
|
|
1027
|
+
text: "",
|
|
1028
|
+
step,
|
|
1029
|
+
toolName: chunk.payload.toolName,
|
|
1030
|
+
index: message.toolInvocations.length
|
|
1031
|
+
};
|
|
1032
|
+
const invocation = {
|
|
1033
|
+
state: "partial-call",
|
|
1034
|
+
step,
|
|
1035
|
+
toolCallId: chunk.payload.toolCallId,
|
|
1036
|
+
toolName: chunk.payload.toolName,
|
|
1037
|
+
args: void 0
|
|
1038
|
+
};
|
|
1039
|
+
message.toolInvocations.push(invocation);
|
|
1040
|
+
updateToolInvocationPart(chunk.payload.toolCallId, invocation);
|
|
1041
|
+
execUpdate();
|
|
1042
|
+
break;
|
|
1043
|
+
}
|
|
1044
|
+
case "tool-call-delta": {
|
|
1045
|
+
const partialToolCall = partialToolCalls[chunk.payload.toolCallId];
|
|
1046
|
+
partialToolCall.text += chunk.payload.argsTextDelta;
|
|
1047
|
+
const { value: partialArgs } = uiUtils.parsePartialJson(partialToolCall.text);
|
|
1048
|
+
const invocation = {
|
|
1049
|
+
state: "partial-call",
|
|
1050
|
+
step: partialToolCall.step,
|
|
1051
|
+
toolCallId: chunk.payload.toolCallId,
|
|
1052
|
+
toolName: partialToolCall.toolName,
|
|
1053
|
+
args: partialArgs
|
|
1054
|
+
};
|
|
1055
|
+
message.toolInvocations[partialToolCall.index] = invocation;
|
|
1056
|
+
updateToolInvocationPart(chunk.payload.toolCallId, invocation);
|
|
1057
|
+
execUpdate();
|
|
1058
|
+
break;
|
|
1059
|
+
}
|
|
1060
|
+
case "tool-result": {
|
|
1061
|
+
const toolInvocations = message.toolInvocations;
|
|
1062
|
+
if (toolInvocations == null) {
|
|
1063
|
+
throw new Error("tool_result must be preceded by a tool_call");
|
|
1064
|
+
}
|
|
1065
|
+
const toolInvocationIndex = toolInvocations.findIndex(
|
|
1066
|
+
(invocation2) => invocation2.toolCallId === chunk.payload.toolCallId
|
|
1067
|
+
);
|
|
1068
|
+
if (toolInvocationIndex === -1) {
|
|
1069
|
+
throw new Error("tool_result must be preceded by a tool_call with the same toolCallId");
|
|
1070
|
+
}
|
|
1071
|
+
const invocation = {
|
|
1072
|
+
...toolInvocations[toolInvocationIndex],
|
|
1073
|
+
state: "result",
|
|
1074
|
+
...chunk.payload
|
|
1075
|
+
};
|
|
1076
|
+
toolInvocations[toolInvocationIndex] = invocation;
|
|
1077
|
+
updateToolInvocationPart(chunk.payload.toolCallId, invocation);
|
|
1078
|
+
execUpdate();
|
|
1079
|
+
break;
|
|
1080
|
+
}
|
|
1081
|
+
case "error": {
|
|
1082
|
+
throw new Error(chunk.payload.error);
|
|
1083
|
+
}
|
|
1084
|
+
case "data": {
|
|
1085
|
+
data.push(...chunk.payload.data);
|
|
1086
|
+
execUpdate();
|
|
1087
|
+
break;
|
|
1088
|
+
}
|
|
1089
|
+
case "step-finish": {
|
|
1090
|
+
step += 1;
|
|
1091
|
+
currentTextPart = chunk.payload.isContinued ? currentTextPart : void 0;
|
|
1092
|
+
currentReasoningPart = void 0;
|
|
1093
|
+
currentReasoningTextDetail = void 0;
|
|
1094
|
+
execUpdate();
|
|
1095
|
+
break;
|
|
1096
|
+
}
|
|
1097
|
+
case "finish": {
|
|
1098
|
+
finishReason = chunk.payload.finishReason;
|
|
1099
|
+
if (chunk.payload.usage != null) {
|
|
1100
|
+
usage = chunk.payload.usage;
|
|
1101
|
+
}
|
|
1102
|
+
break;
|
|
1103
|
+
}
|
|
1104
|
+
}
|
|
1105
|
+
}
|
|
1106
|
+
});
|
|
1107
|
+
onFinish?.({ message, finishReason, usage });
|
|
1108
|
+
}
|
|
1109
|
+
async processStreamResponse_vNext(processedParams, writable) {
|
|
1110
|
+
const response = await this.request(`/api/agents/${this.agentId}/stream/vnext`, {
|
|
1111
|
+
method: "POST",
|
|
1112
|
+
body: processedParams,
|
|
1113
|
+
stream: true
|
|
1114
|
+
});
|
|
1115
|
+
if (!response.body) {
|
|
1116
|
+
throw new Error("No response body");
|
|
1117
|
+
}
|
|
1118
|
+
try {
|
|
1119
|
+
let toolCalls = [];
|
|
1120
|
+
let messages = [];
|
|
1121
|
+
const [streamForWritable, streamForProcessing] = response.body.tee();
|
|
1122
|
+
streamForWritable.pipeTo(writable, {
|
|
1123
|
+
preventClose: true
|
|
1124
|
+
}).catch((error) => {
|
|
1125
|
+
console.error("Error piping to writable stream:", error);
|
|
1126
|
+
});
|
|
1127
|
+
this.processChatResponse_vNext({
|
|
1128
|
+
stream: streamForProcessing,
|
|
1129
|
+
update: ({ message }) => {
|
|
1130
|
+
const existingIndex = messages.findIndex((m) => m.id === message.id);
|
|
1131
|
+
if (existingIndex !== -1) {
|
|
1132
|
+
messages[existingIndex] = message;
|
|
1133
|
+
} else {
|
|
1134
|
+
messages.push(message);
|
|
1135
|
+
}
|
|
1136
|
+
},
|
|
1137
|
+
onFinish: async ({ finishReason, message }) => {
|
|
1138
|
+
if (finishReason === "tool-calls") {
|
|
1139
|
+
const toolCall = [...message?.parts ?? []].reverse().find((part) => part.type === "tool-invocation")?.toolInvocation;
|
|
1140
|
+
if (toolCall) {
|
|
1141
|
+
toolCalls.push(toolCall);
|
|
1142
|
+
}
|
|
1143
|
+
for (const toolCall2 of toolCalls) {
|
|
1144
|
+
const clientTool = processedParams.clientTools?.[toolCall2.toolName];
|
|
1145
|
+
if (clientTool && clientTool.execute) {
|
|
1146
|
+
const result = await clientTool.execute(
|
|
1147
|
+
{
|
|
1148
|
+
context: toolCall2?.args,
|
|
1149
|
+
runId: processedParams.runId,
|
|
1150
|
+
resourceId: processedParams.resourceId,
|
|
1151
|
+
threadId: processedParams.threadId,
|
|
1152
|
+
runtimeContext: processedParams.runtimeContext
|
|
1153
|
+
},
|
|
1154
|
+
{
|
|
1155
|
+
messages: response.messages,
|
|
1156
|
+
toolCallId: toolCall2?.toolCallId
|
|
1157
|
+
}
|
|
1158
|
+
);
|
|
1159
|
+
const lastMessage = JSON.parse(JSON.stringify(messages[messages.length - 1]));
|
|
1160
|
+
const toolInvocationPart = lastMessage?.parts?.find(
|
|
1161
|
+
(part) => part.type === "tool-invocation" && part.toolInvocation?.toolCallId === toolCall2.toolCallId
|
|
1162
|
+
);
|
|
1163
|
+
if (toolInvocationPart) {
|
|
1164
|
+
toolInvocationPart.toolInvocation = {
|
|
1165
|
+
...toolInvocationPart.toolInvocation,
|
|
1166
|
+
state: "result",
|
|
1167
|
+
result
|
|
1168
|
+
};
|
|
1169
|
+
}
|
|
1170
|
+
const toolInvocation = lastMessage?.toolInvocations?.find(
|
|
1171
|
+
(toolInvocation2) => toolInvocation2.toolCallId === toolCall2.toolCallId
|
|
1172
|
+
);
|
|
1173
|
+
if (toolInvocation) {
|
|
1174
|
+
toolInvocation.state = "result";
|
|
1175
|
+
toolInvocation.result = result;
|
|
1176
|
+
}
|
|
1177
|
+
const writer = writable.getWriter();
|
|
1178
|
+
try {
|
|
1179
|
+
await writer.write(
|
|
1180
|
+
new TextEncoder().encode(
|
|
1181
|
+
"a:" + JSON.stringify({
|
|
1182
|
+
toolCallId: toolCall2.toolCallId,
|
|
1183
|
+
result
|
|
1184
|
+
}) + "\n"
|
|
1185
|
+
)
|
|
1186
|
+
);
|
|
1187
|
+
} finally {
|
|
1188
|
+
writer.releaseLock();
|
|
1189
|
+
}
|
|
1190
|
+
const originalMessages = processedParams.messages;
|
|
1191
|
+
const messageArray = Array.isArray(originalMessages) ? originalMessages : [originalMessages];
|
|
1192
|
+
this.processStreamResponse_vNext(
|
|
1193
|
+
{
|
|
1194
|
+
...processedParams,
|
|
1195
|
+
messages: [...messageArray, ...messages.filter((m) => m.id !== lastMessage.id), lastMessage]
|
|
1196
|
+
},
|
|
1197
|
+
writable
|
|
1198
|
+
).catch((error) => {
|
|
1199
|
+
console.error("Error processing stream response:", error);
|
|
1200
|
+
});
|
|
1201
|
+
}
|
|
1202
|
+
}
|
|
1203
|
+
} else {
|
|
1204
|
+
setTimeout(() => {
|
|
1205
|
+
writable.close();
|
|
1206
|
+
}, 0);
|
|
1207
|
+
}
|
|
1208
|
+
},
|
|
1209
|
+
lastMessage: void 0
|
|
1210
|
+
}).catch((error) => {
|
|
1211
|
+
console.error("Error processing stream response:", error);
|
|
1212
|
+
});
|
|
1213
|
+
} catch (error) {
|
|
1214
|
+
console.error("Error processing stream response:", error);
|
|
1215
|
+
}
|
|
1216
|
+
return response;
|
|
1217
|
+
}
|
|
1218
|
+
async streamVNext(params) {
|
|
1219
|
+
const processedParams = {
|
|
1220
|
+
...params,
|
|
1221
|
+
output: params.output ? zodToJsonSchema(params.output) : void 0,
|
|
1222
|
+
runtimeContext: parseClientRuntimeContext(params.runtimeContext),
|
|
1223
|
+
clientTools: processClientTools(params.clientTools)
|
|
1224
|
+
};
|
|
1225
|
+
const { readable, writable } = new TransformStream();
|
|
1226
|
+
const response = await this.processStreamResponse_vNext(processedParams, writable);
|
|
1227
|
+
const streamResponse = new Response(readable, {
|
|
1228
|
+
status: response.status,
|
|
1229
|
+
statusText: response.statusText,
|
|
1230
|
+
headers: response.headers
|
|
1231
|
+
});
|
|
1232
|
+
streamResponse.processDataStream = async ({
|
|
1233
|
+
onChunk
|
|
1234
|
+
}) => {
|
|
1235
|
+
await processMastraStream({
|
|
1236
|
+
stream: streamResponse.body,
|
|
1237
|
+
onChunk
|
|
1238
|
+
});
|
|
1239
|
+
};
|
|
1240
|
+
return streamResponse;
|
|
1241
|
+
}
|
|
726
1242
|
/**
|
|
727
1243
|
* Processes the stream response and handles tool calls
|
|
728
1244
|
*/
|
|
@@ -873,6 +1389,17 @@ var Agent = class extends BaseResource {
|
|
|
873
1389
|
liveEvals() {
|
|
874
1390
|
return this.request(`/api/agents/${this.agentId}/evals/live`);
|
|
875
1391
|
}
|
|
1392
|
+
/**
|
|
1393
|
+
* Updates the model for the agent
|
|
1394
|
+
* @param params - Parameters for updating the model
|
|
1395
|
+
* @returns Promise containing the updated model
|
|
1396
|
+
*/
|
|
1397
|
+
updateModel(params) {
|
|
1398
|
+
return this.request(`/api/agents/${this.agentId}/model`, {
|
|
1399
|
+
method: "POST",
|
|
1400
|
+
body: params
|
|
1401
|
+
});
|
|
1402
|
+
}
|
|
876
1403
|
};
|
|
877
1404
|
var Network = class extends BaseResource {
|
|
878
1405
|
constructor(options, networkId) {
|
|
@@ -1612,22 +2139,38 @@ var A2A = class extends BaseResource {
|
|
|
1612
2139
|
* @returns Promise containing the agent card information
|
|
1613
2140
|
*/
|
|
1614
2141
|
async getCard() {
|
|
1615
|
-
return this.request(`/.well-known/${this.agentId}/agent.json`);
|
|
2142
|
+
return this.request(`/.well-known/${this.agentId}/agent-card.json`);
|
|
1616
2143
|
}
|
|
1617
2144
|
/**
|
|
1618
|
-
* Send a message to the agent and
|
|
2145
|
+
* Send a message to the agent and gets a message or task response
|
|
1619
2146
|
* @param params - Parameters for the task
|
|
1620
|
-
* @returns Promise containing the
|
|
2147
|
+
* @returns Promise containing the response
|
|
1621
2148
|
*/
|
|
1622
2149
|
async sendMessage(params) {
|
|
1623
2150
|
const response = await this.request(`/a2a/${this.agentId}`, {
|
|
1624
2151
|
method: "POST",
|
|
1625
2152
|
body: {
|
|
1626
|
-
method: "
|
|
2153
|
+
method: "message/send",
|
|
1627
2154
|
params
|
|
1628
2155
|
}
|
|
1629
2156
|
});
|
|
1630
|
-
return
|
|
2157
|
+
return response;
|
|
2158
|
+
}
|
|
2159
|
+
/**
|
|
2160
|
+
* Sends a message to an agent to initiate/continue a task and subscribes
|
|
2161
|
+
* the client to real-time updates for that task via Server-Sent Events (SSE).
|
|
2162
|
+
* @param params - Parameters for the task
|
|
2163
|
+
* @returns A stream of Server-Sent Events. Each SSE `data` field contains a `SendStreamingMessageResponse`
|
|
2164
|
+
*/
|
|
2165
|
+
async sendStreamingMessage(params) {
|
|
2166
|
+
const response = await this.request(`/a2a/${this.agentId}`, {
|
|
2167
|
+
method: "POST",
|
|
2168
|
+
body: {
|
|
2169
|
+
method: "message/stream",
|
|
2170
|
+
params
|
|
2171
|
+
}
|
|
2172
|
+
});
|
|
2173
|
+
return response;
|
|
1631
2174
|
}
|
|
1632
2175
|
/**
|
|
1633
2176
|
* Get the status and result of a task
|
|
@@ -1642,7 +2185,7 @@ var A2A = class extends BaseResource {
|
|
|
1642
2185
|
params
|
|
1643
2186
|
}
|
|
1644
2187
|
});
|
|
1645
|
-
return response
|
|
2188
|
+
return response;
|
|
1646
2189
|
}
|
|
1647
2190
|
/**
|
|
1648
2191
|
* Cancel a running task
|
|
@@ -1658,21 +2201,6 @@ var A2A = class extends BaseResource {
|
|
|
1658
2201
|
}
|
|
1659
2202
|
});
|
|
1660
2203
|
}
|
|
1661
|
-
/**
|
|
1662
|
-
* Send a message and subscribe to streaming updates (not fully implemented)
|
|
1663
|
-
* @param params - Parameters for the task
|
|
1664
|
-
* @returns Promise containing the task response
|
|
1665
|
-
*/
|
|
1666
|
-
async sendAndSubscribe(params) {
|
|
1667
|
-
return this.request(`/a2a/${this.agentId}`, {
|
|
1668
|
-
method: "POST",
|
|
1669
|
-
body: {
|
|
1670
|
-
method: "tasks/sendSubscribe",
|
|
1671
|
-
params
|
|
1672
|
-
},
|
|
1673
|
-
stream: true
|
|
1674
|
-
});
|
|
1675
|
-
}
|
|
1676
2204
|
};
|
|
1677
2205
|
|
|
1678
2206
|
// src/resources/mcp-tool.ts
|
|
@@ -2417,6 +2945,13 @@ var MastraClient = class extends BaseResource {
|
|
|
2417
2945
|
body: params
|
|
2418
2946
|
});
|
|
2419
2947
|
}
|
|
2948
|
+
/**
|
|
2949
|
+
* Retrieves model providers with available keys
|
|
2950
|
+
* @returns Promise containing model providers with available keys
|
|
2951
|
+
*/
|
|
2952
|
+
getModelProviders() {
|
|
2953
|
+
return this.request(`/api/model-providers`);
|
|
2954
|
+
}
|
|
2420
2955
|
};
|
|
2421
2956
|
|
|
2422
2957
|
exports.MastraClient = MastraClient;
|