@mastra/server 1.20.0 → 1.21.0-alpha.1
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/CHANGELOG.md +96 -0
- package/dist/_types/@internal_core/dist/index.d.ts +12 -2
- package/dist/{chunk-NCCK5NVX.cjs → chunk-22YG2467.cjs} +3 -3
- package/dist/{chunk-NCCK5NVX.cjs.map → chunk-22YG2467.cjs.map} +1 -1
- package/dist/{chunk-3W54ZNYP.cjs → chunk-5IKQNFW5.cjs} +3 -3
- package/dist/{chunk-3W54ZNYP.cjs.map → chunk-5IKQNFW5.cjs.map} +1 -1
- package/dist/chunk-6MFKRF4K.cjs +176 -0
- package/dist/chunk-6MFKRF4K.cjs.map +1 -0
- package/dist/{chunk-G726SOOJ.cjs → chunk-7G44ZSFS.cjs} +3 -3
- package/dist/{chunk-G726SOOJ.cjs.map → chunk-7G44ZSFS.cjs.map} +1 -1
- package/dist/{chunk-TYYGY4H4.cjs → chunk-7HOYCW7S.cjs} +3 -3
- package/dist/{chunk-TYYGY4H4.cjs.map → chunk-7HOYCW7S.cjs.map} +1 -1
- package/dist/{chunk-MH2WUZWZ.js → chunk-7KJ4NUEH.js} +3 -3
- package/dist/{chunk-MH2WUZWZ.js.map → chunk-7KJ4NUEH.js.map} +1 -1
- package/dist/chunk-AJYJ4H7P.js +438 -0
- package/dist/chunk-AJYJ4H7P.js.map +1 -0
- package/dist/{chunk-ULLR3RN3.cjs → chunk-AYD7DM5U.cjs} +3 -3
- package/dist/{chunk-ULLR3RN3.cjs.map → chunk-AYD7DM5U.cjs.map} +1 -1
- package/dist/chunk-B6HNPO67.cjs +195 -0
- package/dist/chunk-B6HNPO67.cjs.map +1 -0
- package/dist/chunk-BQXQZPYZ.js +189 -0
- package/dist/chunk-BQXQZPYZ.js.map +1 -0
- package/dist/{chunk-DAEHQAZC.cjs → chunk-DNYJSPA5.cjs} +27 -6
- package/dist/chunk-DNYJSPA5.cjs.map +1 -0
- package/dist/{chunk-UE4FGLA3.cjs → chunk-DWXE75AZ.cjs} +38 -38
- package/dist/{chunk-UE4FGLA3.cjs.map → chunk-DWXE75AZ.cjs.map} +1 -1
- package/dist/chunk-FCCXLAOT.js +153 -0
- package/dist/chunk-FCCXLAOT.js.map +1 -0
- package/dist/{chunk-5CDCKTHB.js → chunk-FCLPA23B.js} +27 -6
- package/dist/chunk-FCLPA23B.js.map +1 -0
- package/dist/{chunk-XINNAPVH.cjs → chunk-FGXFUFWK.cjs} +29 -29
- package/dist/{chunk-XINNAPVH.cjs.map → chunk-FGXFUFWK.cjs.map} +1 -1
- package/dist/chunk-FURWD3DL.cjs +180 -0
- package/dist/chunk-FURWD3DL.cjs.map +1 -0
- package/dist/{chunk-23U24RPR.cjs → chunk-G5IQN5ZG.cjs} +3 -3
- package/dist/{chunk-23U24RPR.cjs.map → chunk-G5IQN5ZG.cjs.map} +1 -1
- package/dist/{chunk-UFK5THVP.cjs → chunk-GANMD6GP.cjs} +3 -3
- package/dist/{chunk-UFK5THVP.cjs.map → chunk-GANMD6GP.cjs.map} +1 -1
- package/dist/{chunk-OSMI5XLT.cjs → chunk-GLB2TK6A.cjs} +12 -7
- package/dist/chunk-GLB2TK6A.cjs.map +1 -0
- package/dist/chunk-GWPNQNXF.cjs +45 -0
- package/dist/chunk-GWPNQNXF.cjs.map +1 -0
- package/dist/chunk-IRRJW5EA.cjs +695 -0
- package/dist/chunk-IRRJW5EA.cjs.map +1 -0
- package/dist/{chunk-G2ZZKKQI.js → chunk-L7PDVJZD.js} +3 -3
- package/dist/{chunk-G2ZZKKQI.js.map → chunk-L7PDVJZD.js.map} +1 -1
- package/dist/{chunk-K6RNBFGH.js → chunk-M4BZ2B7D.js} +3 -3
- package/dist/{chunk-K6RNBFGH.js.map → chunk-M4BZ2B7D.js.map} +1 -1
- package/dist/{chunk-4D7KSU4X.js → chunk-MWRVCCZE.js} +3 -3
- package/dist/{chunk-4D7KSU4X.js.map → chunk-MWRVCCZE.js.map} +1 -1
- package/dist/chunk-NCW7IFFR.js +690 -0
- package/dist/chunk-NCW7IFFR.js.map +1 -0
- package/dist/{chunk-MKW43AJ3.js → chunk-NL7Y763D.js} +3 -3
- package/dist/{chunk-MKW43AJ3.js.map → chunk-NL7Y763D.js.map} +1 -1
- package/dist/{chunk-MYFV2L3T.js → chunk-OBR4SMSZ.js} +3 -3
- package/dist/{chunk-MYFV2L3T.js.map → chunk-OBR4SMSZ.js.map} +1 -1
- package/dist/chunk-OJ72T77G.cjs +452 -0
- package/dist/chunk-OJ72T77G.cjs.map +1 -0
- package/dist/{chunk-5YMAXL54.js → chunk-QSA443WV.js} +3 -3
- package/dist/{chunk-5YMAXL54.js.map → chunk-QSA443WV.js.map} +1 -1
- package/dist/{chunk-4NRXB7VI.js → chunk-SNGR4M5I.js} +3 -3
- package/dist/{chunk-4NRXB7VI.js.map → chunk-SNGR4M5I.js.map} +1 -1
- package/dist/{chunk-XDFOXO76.js → chunk-SW6IJZFQ.js} +9 -9
- package/dist/{chunk-XDFOXO76.js.map → chunk-SW6IJZFQ.js.map} +1 -1
- package/dist/{chunk-WPBRQTMO.js → chunk-VCA6BSYU.js} +3 -3
- package/dist/{chunk-WPBRQTMO.js.map → chunk-VCA6BSYU.js.map} +1 -1
- package/dist/{chunk-HHF4VF75.js → chunk-VHUB2TYN.js} +3 -3
- package/dist/{chunk-HHF4VF75.js.map → chunk-VHUB2TYN.js.map} +1 -1
- package/dist/chunk-VKIIGA2F.js +35 -0
- package/dist/chunk-VKIIGA2F.js.map +1 -0
- package/dist/{chunk-2QCTUEHS.cjs → chunk-VVWHCAF2.cjs} +3 -3
- package/dist/{chunk-2QCTUEHS.cjs.map → chunk-VVWHCAF2.cjs.map} +1 -1
- package/dist/{chunk-YFQ5JIUD.js → chunk-WO7BJJIB.js} +12 -7
- package/dist/chunk-WO7BJJIB.js.map +1 -0
- package/dist/{chunk-A5J2TY2W.cjs → chunk-XKKTZVZX.cjs} +3 -3
- package/dist/{chunk-A5J2TY2W.cjs.map → chunk-XKKTZVZX.cjs.map} +1 -1
- package/dist/chunk-ZA3NGM3L.js +174 -0
- package/dist/chunk-ZA3NGM3L.js.map +1 -0
- package/dist/docs/SKILL.md +1 -1
- package/dist/docs/assets/SOURCE_MAP.json +1 -1
- package/dist/docs/references/reference-server-routes.md +22 -1
- package/dist/{observational-memory-B25SASRW-T76NSRNU.cjs → observational-memory-WOEVNCG4-5THJIIQE.cjs} +26 -26
- package/dist/{observational-memory-B25SASRW-T76NSRNU.cjs.map → observational-memory-WOEVNCG4-5THJIIQE.cjs.map} +1 -1
- package/dist/{observational-memory-B25SASRW-L4GYD2ZN.js → observational-memory-WOEVNCG4-DT36JHO2.js} +3 -3
- package/dist/{observational-memory-B25SASRW-L4GYD2ZN.js.map → observational-memory-WOEVNCG4-DT36JHO2.js.map} +1 -1
- package/dist/server/handlers/agent-builder.cjs +16 -16
- package/dist/server/handlers/agent-builder.js +1 -1
- package/dist/server/handlers/conversations.cjs +24 -0
- package/dist/server/handlers/conversations.cjs.map +1 -0
- package/dist/server/handlers/conversations.d.ts +153 -0
- package/dist/server/handlers/conversations.d.ts.map +1 -0
- package/dist/server/handlers/conversations.js +3 -0
- package/dist/server/handlers/conversations.js.map +1 -0
- package/dist/server/handlers/datasets.cjs +26 -26
- package/dist/server/handlers/datasets.js +1 -1
- package/dist/server/handlers/logs.cjs +4 -4
- package/dist/server/handlers/logs.js +1 -1
- package/dist/server/handlers/observability-new-endpoints.cjs +27 -27
- package/dist/server/handlers/observability-new-endpoints.d.ts +8 -0
- package/dist/server/handlers/observability-new-endpoints.d.ts.map +1 -1
- package/dist/server/handlers/observability-new-endpoints.js +1 -1
- package/dist/server/handlers/observability.cjs +32 -32
- package/dist/server/handlers/observability.js +2 -2
- package/dist/server/handlers/responses.adapter.cjs +60 -0
- package/dist/server/handlers/responses.adapter.cjs.map +1 -0
- package/dist/server/handlers/responses.adapter.d.ts +98 -0
- package/dist/server/handlers/responses.adapter.d.ts.map +1 -0
- package/dist/server/handlers/responses.adapter.js +3 -0
- package/dist/server/handlers/responses.adapter.js.map +1 -0
- package/dist/server/handlers/responses.cjs +20 -0
- package/dist/server/handlers/responses.cjs.map +1 -0
- package/dist/server/handlers/responses.d.ts +387 -0
- package/dist/server/handlers/responses.d.ts.map +1 -0
- package/dist/server/handlers/responses.js +3 -0
- package/dist/server/handlers/responses.js.map +1 -0
- package/dist/server/handlers/responses.storage.cjs +28 -0
- package/dist/server/handlers/responses.storage.cjs.map +1 -0
- package/dist/server/handlers/responses.storage.d.ts +97 -0
- package/dist/server/handlers/responses.storage.d.ts.map +1 -0
- package/dist/server/handlers/responses.storage.js +3 -0
- package/dist/server/handlers/responses.storage.js.map +1 -0
- package/dist/server/handlers/scores.cjs +7 -7
- package/dist/server/handlers/scores.js +1 -1
- package/dist/server/handlers/stored-agents.cjs +7 -7
- package/dist/server/handlers/stored-agents.js +1 -1
- package/dist/server/handlers/stored-mcp-clients.cjs +6 -6
- package/dist/server/handlers/stored-mcp-clients.js +1 -1
- package/dist/server/handlers/stored-prompt-blocks.cjs +6 -6
- package/dist/server/handlers/stored-prompt-blocks.js +1 -1
- package/dist/server/handlers/stored-scorers.cjs +6 -6
- package/dist/server/handlers/stored-scorers.js +1 -1
- package/dist/server/handlers/tools.cjs +6 -6
- package/dist/server/handlers/tools.js +1 -1
- package/dist/server/handlers/workflows.cjs +24 -24
- package/dist/server/handlers/workflows.js +1 -1
- package/dist/server/handlers.cjs +28 -18
- package/dist/server/handlers.d.ts +2 -0
- package/dist/server/handlers.d.ts.map +1 -1
- package/dist/server/handlers.js +9 -7
- package/dist/server/schemas/conversations.d.ts +74 -0
- package/dist/server/schemas/conversations.d.ts.map +1 -0
- package/dist/server/schemas/index.cjs +185 -91
- package/dist/server/schemas/index.d.ts +2 -0
- package/dist/server/schemas/index.d.ts.map +1 -1
- package/dist/server/schemas/index.js +5 -3
- package/dist/server/schemas/responses.d.ts +341 -0
- package/dist/server/schemas/responses.d.ts.map +1 -0
- package/dist/server/server-adapter/index.cjs +153 -138
- package/dist/server/server-adapter/index.cjs.map +1 -1
- package/dist/server/server-adapter/index.js +30 -15
- package/dist/server/server-adapter/index.js.map +1 -1
- package/dist/server/server-adapter/routes/conversations.d.ts +150 -0
- package/dist/server/server-adapter/routes/conversations.d.ts.map +1 -0
- package/dist/server/server-adapter/routes/index.d.ts +4 -0
- package/dist/server/server-adapter/routes/index.d.ts.map +1 -1
- package/dist/server/server-adapter/routes/observability.d.ts +4 -0
- package/dist/server/server-adapter/routes/observability.d.ts.map +1 -1
- package/dist/server/server-adapter/routes/responses.d.ts +385 -0
- package/dist/server/server-adapter/routes/responses.d.ts.map +1 -0
- package/package.json +4 -4
- package/dist/chunk-5CDCKTHB.js.map +0 -1
- package/dist/chunk-DAEHQAZC.cjs.map +0 -1
- package/dist/chunk-OSMI5XLT.cjs.map +0 -1
- package/dist/chunk-YFQ5JIUD.js.map +0 -1
|
@@ -0,0 +1,695 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var chunk6MFKRF4K_cjs = require('./chunk-6MFKRF4K.cjs');
|
|
4
|
+
var chunkOJ72T77G_cjs = require('./chunk-OJ72T77G.cjs');
|
|
5
|
+
var chunkB6HNPO67_cjs = require('./chunk-B6HNPO67.cjs');
|
|
6
|
+
var chunkLSPBHVDD_cjs = require('./chunk-LSPBHVDD.cjs');
|
|
7
|
+
var chunkZKMVCV4G_cjs = require('./chunk-ZKMVCV4G.cjs');
|
|
8
|
+
var chunkB34S64RC_cjs = require('./chunk-B34S64RC.cjs');
|
|
9
|
+
var chunkEGSCXVMR_cjs = require('./chunk-EGSCXVMR.cjs');
|
|
10
|
+
var chunk64ITUOXI_cjs = require('./chunk-64ITUOXI.cjs');
|
|
11
|
+
var chunkO7I5CWRX_cjs = require('./chunk-O7I5CWRX.cjs');
|
|
12
|
+
var crypto = require('crypto');
|
|
13
|
+
|
|
14
|
+
// src/server/handlers/responses.ts
|
|
15
|
+
var responses_exports = {};
|
|
16
|
+
chunkO7I5CWRX_cjs.__export(responses_exports, {
|
|
17
|
+
CREATE_RESPONSE_ROUTE: () => CREATE_RESPONSE_ROUTE,
|
|
18
|
+
DELETE_RESPONSE_ROUTE: () => DELETE_RESPONSE_ROUTE,
|
|
19
|
+
GET_RESPONSE_ROUTE: () => GET_RESPONSE_ROUTE
|
|
20
|
+
});
|
|
21
|
+
var JSON_OBJECT_RESPONSE_SCHEMA = {
|
|
22
|
+
type: "object",
|
|
23
|
+
additionalProperties: true
|
|
24
|
+
};
|
|
25
|
+
function jsonResponse(data, status = 200) {
|
|
26
|
+
return new Response(JSON.stringify(data), {
|
|
27
|
+
status,
|
|
28
|
+
headers: {
|
|
29
|
+
"Content-Type": "application/json"
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
function createStructuredOutput(text) {
|
|
34
|
+
if (!text) {
|
|
35
|
+
return void 0;
|
|
36
|
+
}
|
|
37
|
+
switch (text.format.type) {
|
|
38
|
+
case "json_object":
|
|
39
|
+
return {
|
|
40
|
+
schema: JSON_OBJECT_RESPONSE_SCHEMA,
|
|
41
|
+
jsonPromptInjection: true
|
|
42
|
+
};
|
|
43
|
+
case "json_schema":
|
|
44
|
+
return {
|
|
45
|
+
schema: text.format.schema
|
|
46
|
+
};
|
|
47
|
+
default:
|
|
48
|
+
return void 0;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
function getStreamedMessageOutputItem(response, responseId) {
|
|
52
|
+
return response.output.find(
|
|
53
|
+
(item) => item.type === "message" && item.id === responseId
|
|
54
|
+
) ?? response.output.find(
|
|
55
|
+
(item) => item.type === "message"
|
|
56
|
+
) ?? null;
|
|
57
|
+
}
|
|
58
|
+
async function resolveThreadExecutionContext({
|
|
59
|
+
agent,
|
|
60
|
+
store,
|
|
61
|
+
conversationId,
|
|
62
|
+
previousResponseTurnRecord,
|
|
63
|
+
requestContext
|
|
64
|
+
}) {
|
|
65
|
+
if (conversationId && previousResponseTurnRecord && previousResponseTurnRecord.thread.id !== conversationId) {
|
|
66
|
+
throw new chunk64ITUOXI_cjs.HTTPException(400, {
|
|
67
|
+
message: "conversation_id and previous_response_id must reference the same conversation thread when both are provided"
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
if (previousResponseTurnRecord) {
|
|
71
|
+
return {
|
|
72
|
+
threadId: previousResponseTurnRecord.thread.id,
|
|
73
|
+
resourceId: previousResponseTurnRecord.thread.resourceId
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
const effectiveThreadId = chunkZKMVCV4G_cjs.getEffectiveThreadId(requestContext, void 0);
|
|
77
|
+
const effectiveResourceId = chunkZKMVCV4G_cjs.getEffectiveResourceId(requestContext, void 0);
|
|
78
|
+
if (!store && !conversationId && !effectiveThreadId) {
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
const memory = await agent.getMemory({ requestContext });
|
|
82
|
+
if (!memory) {
|
|
83
|
+
if (conversationId) {
|
|
84
|
+
throw new chunk64ITUOXI_cjs.HTTPException(400, {
|
|
85
|
+
message: "conversation_id requires the target agent to have memory configured"
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
return null;
|
|
89
|
+
}
|
|
90
|
+
if (conversationId) {
|
|
91
|
+
const existingThread2 = await memory.getThreadById({ threadId: conversationId });
|
|
92
|
+
if (!existingThread2) {
|
|
93
|
+
throw new chunk64ITUOXI_cjs.HTTPException(404, { message: `Conversation ${conversationId} was not found` });
|
|
94
|
+
}
|
|
95
|
+
await chunkZKMVCV4G_cjs.validateThreadOwnership(existingThread2, effectiveResourceId);
|
|
96
|
+
return {
|
|
97
|
+
threadId: existingThread2.id,
|
|
98
|
+
resourceId: effectiveResourceId ?? existingThread2.resourceId
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
if (!effectiveThreadId) {
|
|
102
|
+
if (!store) {
|
|
103
|
+
return null;
|
|
104
|
+
}
|
|
105
|
+
const threadId2 = crypto.randomUUID();
|
|
106
|
+
const createdThread2 = await memory.createThread({
|
|
107
|
+
threadId: threadId2,
|
|
108
|
+
resourceId: effectiveResourceId ?? threadId2
|
|
109
|
+
});
|
|
110
|
+
return {
|
|
111
|
+
threadId: createdThread2.id,
|
|
112
|
+
resourceId: createdThread2.resourceId
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
const threadId = effectiveThreadId;
|
|
116
|
+
const existingThread = await memory.getThreadById({ threadId });
|
|
117
|
+
if (existingThread) {
|
|
118
|
+
await chunkZKMVCV4G_cjs.validateThreadOwnership(existingThread, effectiveResourceId);
|
|
119
|
+
return {
|
|
120
|
+
threadId: existingThread.id,
|
|
121
|
+
resourceId: effectiveResourceId ?? existingThread.resourceId
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
if (!store) {
|
|
125
|
+
return null;
|
|
126
|
+
}
|
|
127
|
+
const resourceId = effectiveResourceId ?? threadId;
|
|
128
|
+
const createdThread = await memory.createThread({
|
|
129
|
+
threadId,
|
|
130
|
+
resourceId
|
|
131
|
+
});
|
|
132
|
+
return {
|
|
133
|
+
threadId: createdThread.id,
|
|
134
|
+
resourceId: createdThread.resourceId
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
function createExecutionMemory(threadContext) {
|
|
138
|
+
if (!threadContext) {
|
|
139
|
+
return void 0;
|
|
140
|
+
}
|
|
141
|
+
return {
|
|
142
|
+
memory: {
|
|
143
|
+
thread: threadContext.threadId,
|
|
144
|
+
resource: threadContext.resourceId
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
async function resolveResponseAgent({
|
|
149
|
+
mastra,
|
|
150
|
+
agentId,
|
|
151
|
+
previousResponseTurnRecord
|
|
152
|
+
}) {
|
|
153
|
+
const resolvedAgentId = agentId ?? previousResponseTurnRecord?.metadata.agentId;
|
|
154
|
+
if (!resolvedAgentId) {
|
|
155
|
+
throw new chunk64ITUOXI_cjs.HTTPException(400, {
|
|
156
|
+
message: "Responses requests require an agent_id, or a previous_response_id from a stored agent-backed response"
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
if (!mastra) {
|
|
160
|
+
throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Mastra instance is required for agent-backed responses" });
|
|
161
|
+
}
|
|
162
|
+
return chunkLSPBHVDD_cjs.getAgentFromSystem({ mastra, agentId: resolvedAgentId });
|
|
163
|
+
}
|
|
164
|
+
async function executeGenerate({
|
|
165
|
+
agent,
|
|
166
|
+
model,
|
|
167
|
+
instructions,
|
|
168
|
+
text,
|
|
169
|
+
providerOptions,
|
|
170
|
+
input,
|
|
171
|
+
requestContext,
|
|
172
|
+
abortSignal,
|
|
173
|
+
threadContext
|
|
174
|
+
}) {
|
|
175
|
+
const executionMemory = createExecutionMemory(threadContext);
|
|
176
|
+
const structuredOutput = createStructuredOutput(text);
|
|
177
|
+
const commonOptions = {
|
|
178
|
+
instructions,
|
|
179
|
+
requestContext,
|
|
180
|
+
abortSignal,
|
|
181
|
+
model,
|
|
182
|
+
structuredOutput,
|
|
183
|
+
providerOptions,
|
|
184
|
+
...executionMemory ?? {}
|
|
185
|
+
};
|
|
186
|
+
const resolvedModel = await agent.getModel({ requestContext, modelConfig: model });
|
|
187
|
+
if (resolvedModel.specificationVersion === "v1") {
|
|
188
|
+
if (threadContext) {
|
|
189
|
+
return await agent.generateLegacy(input, {
|
|
190
|
+
instructions,
|
|
191
|
+
requestContext,
|
|
192
|
+
abortSignal,
|
|
193
|
+
model,
|
|
194
|
+
output: structuredOutput?.schema,
|
|
195
|
+
providerOptions,
|
|
196
|
+
resourceId: threadContext.resourceId,
|
|
197
|
+
threadId: threadContext.threadId
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
return await agent.generateLegacy(input, {
|
|
201
|
+
instructions,
|
|
202
|
+
requestContext,
|
|
203
|
+
abortSignal,
|
|
204
|
+
model,
|
|
205
|
+
output: structuredOutput?.schema,
|
|
206
|
+
providerOptions
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
return await agent.generate(input, commonOptions);
|
|
210
|
+
}
|
|
211
|
+
async function executeStream({
|
|
212
|
+
agent,
|
|
213
|
+
model,
|
|
214
|
+
instructions,
|
|
215
|
+
text,
|
|
216
|
+
providerOptions,
|
|
217
|
+
input,
|
|
218
|
+
requestContext,
|
|
219
|
+
abortSignal,
|
|
220
|
+
threadContext
|
|
221
|
+
}) {
|
|
222
|
+
const executionMemory = createExecutionMemory(threadContext);
|
|
223
|
+
const structuredOutput = createStructuredOutput(text);
|
|
224
|
+
const commonOptions = {
|
|
225
|
+
instructions,
|
|
226
|
+
requestContext,
|
|
227
|
+
abortSignal,
|
|
228
|
+
model,
|
|
229
|
+
structuredOutput,
|
|
230
|
+
providerOptions,
|
|
231
|
+
...executionMemory ?? {}
|
|
232
|
+
};
|
|
233
|
+
const resolvedModel = await agent.getModel({ requestContext, modelConfig: model });
|
|
234
|
+
if (resolvedModel.specificationVersion === "v1") {
|
|
235
|
+
if (threadContext) {
|
|
236
|
+
return await agent.streamLegacy(input, {
|
|
237
|
+
instructions,
|
|
238
|
+
requestContext,
|
|
239
|
+
abortSignal,
|
|
240
|
+
model,
|
|
241
|
+
output: structuredOutput?.schema,
|
|
242
|
+
providerOptions,
|
|
243
|
+
resourceId: threadContext.resourceId,
|
|
244
|
+
threadId: threadContext.threadId
|
|
245
|
+
});
|
|
246
|
+
}
|
|
247
|
+
return await agent.streamLegacy(input, {
|
|
248
|
+
instructions,
|
|
249
|
+
requestContext,
|
|
250
|
+
abortSignal,
|
|
251
|
+
model,
|
|
252
|
+
output: structuredOutput?.schema,
|
|
253
|
+
providerOptions
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
return await agent.stream(input, commonOptions);
|
|
257
|
+
}
|
|
258
|
+
async function resolveUsage(result) {
|
|
259
|
+
return await (result.totalUsage ?? result.usage ?? null);
|
|
260
|
+
}
|
|
261
|
+
async function resolveProviderMetadata(result) {
|
|
262
|
+
return await (result.providerMetadata ?? void 0);
|
|
263
|
+
}
|
|
264
|
+
async function resolveFinishReason(result) {
|
|
265
|
+
return await result.finishReason ?? void 0;
|
|
266
|
+
}
|
|
267
|
+
async function resolveText(result) {
|
|
268
|
+
return await result.text ?? "";
|
|
269
|
+
}
|
|
270
|
+
async function resolveCompletedResponseState(result, fallbackText) {
|
|
271
|
+
const usage = await resolveUsage(result);
|
|
272
|
+
return {
|
|
273
|
+
completedAt: Math.floor(Date.now() / 1e3),
|
|
274
|
+
status: chunkOJ72T77G_cjs.toResponseStatus(await resolveFinishReason(result)),
|
|
275
|
+
text: await resolveText(result) || fallbackText,
|
|
276
|
+
usage,
|
|
277
|
+
usageDetails: chunkOJ72T77G_cjs.toResponseUsage(usage),
|
|
278
|
+
providerOptions: await resolveProviderMetadata(result)
|
|
279
|
+
};
|
|
280
|
+
}
|
|
281
|
+
async function storeCompletedResponse({
|
|
282
|
+
mastra,
|
|
283
|
+
didStore,
|
|
284
|
+
threadContext,
|
|
285
|
+
responseId,
|
|
286
|
+
metadata,
|
|
287
|
+
completedState,
|
|
288
|
+
messages
|
|
289
|
+
}) {
|
|
290
|
+
if (!didStore || !threadContext) {
|
|
291
|
+
return;
|
|
292
|
+
}
|
|
293
|
+
await chunkB6HNPO67_cjs.persistResponseTurnRecord({
|
|
294
|
+
mastra,
|
|
295
|
+
responseId,
|
|
296
|
+
metadata: {
|
|
297
|
+
...metadata,
|
|
298
|
+
completedAt: completedState.completedAt,
|
|
299
|
+
status: completedState.status,
|
|
300
|
+
usage: completedState.usageDetails,
|
|
301
|
+
providerOptions: completedState.providerOptions,
|
|
302
|
+
messageIds: []
|
|
303
|
+
},
|
|
304
|
+
threadContext,
|
|
305
|
+
messages
|
|
306
|
+
});
|
|
307
|
+
}
|
|
308
|
+
async function finalizeResponse({
|
|
309
|
+
mastra,
|
|
310
|
+
didStore,
|
|
311
|
+
threadContext,
|
|
312
|
+
result,
|
|
313
|
+
responseId,
|
|
314
|
+
createdAt,
|
|
315
|
+
model,
|
|
316
|
+
instructions,
|
|
317
|
+
previousResponseId,
|
|
318
|
+
conversationId,
|
|
319
|
+
configuredTools,
|
|
320
|
+
responseMetadata,
|
|
321
|
+
fallbackText
|
|
322
|
+
}) {
|
|
323
|
+
const completedState = await resolveCompletedResponseState(result, fallbackText);
|
|
324
|
+
const responseMessages = await chunkB6HNPO67_cjs.resolveResponseTurnMessagesForStorage({
|
|
325
|
+
result,
|
|
326
|
+
responseId,
|
|
327
|
+
text: completedState.text,
|
|
328
|
+
threadContext
|
|
329
|
+
});
|
|
330
|
+
const response = chunkOJ72T77G_cjs.buildCompletedResponse({
|
|
331
|
+
responseId,
|
|
332
|
+
outputMessageId: responseId,
|
|
333
|
+
model,
|
|
334
|
+
createdAt,
|
|
335
|
+
completedAt: completedState.completedAt,
|
|
336
|
+
status: completedState.status,
|
|
337
|
+
text: completedState.text,
|
|
338
|
+
usage: completedState.usage,
|
|
339
|
+
instructions,
|
|
340
|
+
textConfig: responseMetadata.text,
|
|
341
|
+
previousResponseId,
|
|
342
|
+
conversationId,
|
|
343
|
+
providerOptions: completedState.providerOptions,
|
|
344
|
+
tools: configuredTools,
|
|
345
|
+
messages: responseMessages,
|
|
346
|
+
store: didStore
|
|
347
|
+
});
|
|
348
|
+
await storeCompletedResponse({
|
|
349
|
+
mastra,
|
|
350
|
+
didStore,
|
|
351
|
+
threadContext,
|
|
352
|
+
responseId,
|
|
353
|
+
metadata: responseMetadata,
|
|
354
|
+
completedState,
|
|
355
|
+
messages: responseMessages
|
|
356
|
+
});
|
|
357
|
+
return { completedState, response, responseMessages };
|
|
358
|
+
}
|
|
359
|
+
async function prepareCreateResponseRequest({
|
|
360
|
+
body,
|
|
361
|
+
mastra,
|
|
362
|
+
requestContext
|
|
363
|
+
}) {
|
|
364
|
+
const previousResponseTurnRecord = body.previous_response_id ? await chunkB6HNPO67_cjs.findResponseTurnRecord({ mastra, responseId: body.previous_response_id, requestContext }) : null;
|
|
365
|
+
if (body.previous_response_id && !previousResponseTurnRecord) {
|
|
366
|
+
throw new chunk64ITUOXI_cjs.HTTPException(404, { message: `Stored response ${body.previous_response_id} was not found` });
|
|
367
|
+
}
|
|
368
|
+
const executionInput = chunkOJ72T77G_cjs.mapResponseInputToExecutionMessages(body.input);
|
|
369
|
+
const agent = await resolveResponseAgent({
|
|
370
|
+
mastra,
|
|
371
|
+
agentId: body.agent_id,
|
|
372
|
+
previousResponseTurnRecord
|
|
373
|
+
});
|
|
374
|
+
const configuredTools = chunkOJ72T77G_cjs.mapMastraToolsToResponseTools(
|
|
375
|
+
await Promise.resolve(agent.listTools({ requestContext }))
|
|
376
|
+
);
|
|
377
|
+
const responseId = chunkOJ72T77G_cjs.createMessageId();
|
|
378
|
+
const createdAt = Math.floor(Date.now() / 1e3);
|
|
379
|
+
const shouldStore = body.store ?? false;
|
|
380
|
+
const threadContext = await resolveThreadExecutionContext({
|
|
381
|
+
agent,
|
|
382
|
+
store: shouldStore,
|
|
383
|
+
conversationId: body.conversation_id,
|
|
384
|
+
previousResponseTurnRecord,
|
|
385
|
+
requestContext
|
|
386
|
+
});
|
|
387
|
+
if (shouldStore && !threadContext) {
|
|
388
|
+
throw new chunk64ITUOXI_cjs.HTTPException(400, {
|
|
389
|
+
message: "Stored responses require the target agent to have memory configured"
|
|
390
|
+
});
|
|
391
|
+
}
|
|
392
|
+
const didStore = shouldStore && Boolean(threadContext);
|
|
393
|
+
return {
|
|
394
|
+
agent,
|
|
395
|
+
configuredTools,
|
|
396
|
+
createdAt,
|
|
397
|
+
didStore,
|
|
398
|
+
executionInput,
|
|
399
|
+
previousResponseTurnRecord,
|
|
400
|
+
responseId,
|
|
401
|
+
responseMetadata: {
|
|
402
|
+
agentId: agent.id,
|
|
403
|
+
model: body.model,
|
|
404
|
+
createdAt,
|
|
405
|
+
instructions: body.instructions,
|
|
406
|
+
text: body.text,
|
|
407
|
+
previousResponseId: previousResponseTurnRecord?.message.id ?? body.previous_response_id,
|
|
408
|
+
tools: configuredTools,
|
|
409
|
+
store: didStore
|
|
410
|
+
},
|
|
411
|
+
threadContext
|
|
412
|
+
};
|
|
413
|
+
}
|
|
414
|
+
function createResponseEventStream({
|
|
415
|
+
body,
|
|
416
|
+
configuredTools,
|
|
417
|
+
createdAt,
|
|
418
|
+
didStore,
|
|
419
|
+
mastra,
|
|
420
|
+
previousResponseTurnRecord,
|
|
421
|
+
responseId,
|
|
422
|
+
responseMetadata,
|
|
423
|
+
streamResult,
|
|
424
|
+
threadContext
|
|
425
|
+
}) {
|
|
426
|
+
const createdResponse = chunkOJ72T77G_cjs.buildInProgressResponse({
|
|
427
|
+
responseId,
|
|
428
|
+
model: body.model,
|
|
429
|
+
createdAt,
|
|
430
|
+
instructions: body.instructions,
|
|
431
|
+
textConfig: body.text,
|
|
432
|
+
previousResponseId: body.previous_response_id,
|
|
433
|
+
conversationId: threadContext?.threadId ?? body.conversation_id,
|
|
434
|
+
tools: configuredTools,
|
|
435
|
+
store: didStore
|
|
436
|
+
});
|
|
437
|
+
return new ReadableStream({
|
|
438
|
+
async start(controller) {
|
|
439
|
+
let sequenceNumber = 1;
|
|
440
|
+
const enqueueEvent = (eventName, payload) => {
|
|
441
|
+
controller.enqueue(
|
|
442
|
+
chunkOJ72T77G_cjs.formatSseEvent(eventName, {
|
|
443
|
+
...payload,
|
|
444
|
+
sequence_number: sequenceNumber++
|
|
445
|
+
})
|
|
446
|
+
);
|
|
447
|
+
};
|
|
448
|
+
enqueueEvent("response.created", {
|
|
449
|
+
type: "response.created",
|
|
450
|
+
response: createdResponse
|
|
451
|
+
});
|
|
452
|
+
enqueueEvent("response.in_progress", {
|
|
453
|
+
type: "response.in_progress",
|
|
454
|
+
response: createdResponse
|
|
455
|
+
});
|
|
456
|
+
enqueueEvent("response.output_item.added", {
|
|
457
|
+
type: "response.output_item.added",
|
|
458
|
+
output_index: 0,
|
|
459
|
+
item: {
|
|
460
|
+
id: responseId,
|
|
461
|
+
type: "message",
|
|
462
|
+
role: "assistant",
|
|
463
|
+
status: "in_progress",
|
|
464
|
+
content: []
|
|
465
|
+
}
|
|
466
|
+
});
|
|
467
|
+
enqueueEvent("response.content_part.added", {
|
|
468
|
+
type: "response.content_part.added",
|
|
469
|
+
output_index: 0,
|
|
470
|
+
content_index: 0,
|
|
471
|
+
item_id: responseId,
|
|
472
|
+
part: chunkOJ72T77G_cjs.createOutputTextPart("")
|
|
473
|
+
});
|
|
474
|
+
let text = "";
|
|
475
|
+
const fullStream = await streamResult.fullStream;
|
|
476
|
+
const reader = fullStream.getReader();
|
|
477
|
+
try {
|
|
478
|
+
while (true) {
|
|
479
|
+
const { done, value } = await reader.read();
|
|
480
|
+
if (done) {
|
|
481
|
+
break;
|
|
482
|
+
}
|
|
483
|
+
const delta = chunkOJ72T77G_cjs.extractTextDelta(value);
|
|
484
|
+
if (delta) {
|
|
485
|
+
text += delta;
|
|
486
|
+
enqueueEvent("response.output_text.delta", {
|
|
487
|
+
type: "response.output_text.delta",
|
|
488
|
+
output_index: 0,
|
|
489
|
+
content_index: 0,
|
|
490
|
+
item_id: responseId,
|
|
491
|
+
delta
|
|
492
|
+
});
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
const { completedState, response } = await finalizeResponse({
|
|
496
|
+
mastra,
|
|
497
|
+
didStore,
|
|
498
|
+
threadContext,
|
|
499
|
+
result: streamResult,
|
|
500
|
+
responseId,
|
|
501
|
+
createdAt,
|
|
502
|
+
model: body.model,
|
|
503
|
+
instructions: body.instructions,
|
|
504
|
+
previousResponseId: previousResponseTurnRecord?.message.id ?? body.previous_response_id,
|
|
505
|
+
conversationId: threadContext?.threadId ?? body.conversation_id,
|
|
506
|
+
configuredTools,
|
|
507
|
+
responseMetadata,
|
|
508
|
+
fallbackText: text
|
|
509
|
+
});
|
|
510
|
+
enqueueEvent("response.output_text.done", {
|
|
511
|
+
type: "response.output_text.done",
|
|
512
|
+
output_index: 0,
|
|
513
|
+
content_index: 0,
|
|
514
|
+
item_id: responseId,
|
|
515
|
+
text: completedState.text
|
|
516
|
+
});
|
|
517
|
+
const completedItem = getStreamedMessageOutputItem(response, responseId) ?? {
|
|
518
|
+
id: responseId,
|
|
519
|
+
type: "message",
|
|
520
|
+
role: "assistant",
|
|
521
|
+
status: "completed",
|
|
522
|
+
content: [chunkOJ72T77G_cjs.createOutputTextPart(completedState.text)]
|
|
523
|
+
};
|
|
524
|
+
enqueueEvent("response.content_part.done", {
|
|
525
|
+
type: "response.content_part.done",
|
|
526
|
+
output_index: 0,
|
|
527
|
+
content_index: 0,
|
|
528
|
+
item_id: responseId,
|
|
529
|
+
part: chunkOJ72T77G_cjs.createOutputTextPart(completedState.text)
|
|
530
|
+
});
|
|
531
|
+
enqueueEvent("response.output_item.done", {
|
|
532
|
+
type: "response.output_item.done",
|
|
533
|
+
output_index: 0,
|
|
534
|
+
item: completedItem
|
|
535
|
+
});
|
|
536
|
+
enqueueEvent("response.completed", {
|
|
537
|
+
type: "response.completed",
|
|
538
|
+
response
|
|
539
|
+
});
|
|
540
|
+
controller.close();
|
|
541
|
+
} catch (error) {
|
|
542
|
+
controller.error(error);
|
|
543
|
+
} finally {
|
|
544
|
+
reader.releaseLock();
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
});
|
|
548
|
+
}
|
|
549
|
+
var CREATE_RESPONSE_ROUTE = chunkEGSCXVMR_cjs.createRoute({
|
|
550
|
+
method: "POST",
|
|
551
|
+
path: "/v1/responses",
|
|
552
|
+
responseType: "datastream-response",
|
|
553
|
+
bodySchema: chunk6MFKRF4K_cjs.createResponseBodySchema,
|
|
554
|
+
responseSchema: chunk6MFKRF4K_cjs.responseObjectSchema,
|
|
555
|
+
summary: "Create a response",
|
|
556
|
+
description: "Creates a response through a Mastra-hosted Responses API-compatible route",
|
|
557
|
+
tags: ["Responses"],
|
|
558
|
+
requiresAuth: true,
|
|
559
|
+
requiresPermission: "agents:execute",
|
|
560
|
+
handler: async ({ mastra, requestContext, abortSignal, ...body }) => {
|
|
561
|
+
try {
|
|
562
|
+
const {
|
|
563
|
+
agent,
|
|
564
|
+
configuredTools,
|
|
565
|
+
createdAt,
|
|
566
|
+
didStore,
|
|
567
|
+
executionInput,
|
|
568
|
+
previousResponseTurnRecord,
|
|
569
|
+
responseId,
|
|
570
|
+
responseMetadata,
|
|
571
|
+
threadContext
|
|
572
|
+
} = await prepareCreateResponseRequest({ body, mastra, requestContext });
|
|
573
|
+
if (!body.stream) {
|
|
574
|
+
const result = await executeGenerate({
|
|
575
|
+
agent,
|
|
576
|
+
model: body.model,
|
|
577
|
+
instructions: body.instructions,
|
|
578
|
+
text: body.text,
|
|
579
|
+
providerOptions: body.providerOptions,
|
|
580
|
+
input: executionInput,
|
|
581
|
+
requestContext,
|
|
582
|
+
abortSignal,
|
|
583
|
+
threadContext
|
|
584
|
+
});
|
|
585
|
+
const { response } = await finalizeResponse({
|
|
586
|
+
mastra,
|
|
587
|
+
didStore,
|
|
588
|
+
threadContext,
|
|
589
|
+
result,
|
|
590
|
+
responseId,
|
|
591
|
+
createdAt,
|
|
592
|
+
model: body.model,
|
|
593
|
+
instructions: body.instructions,
|
|
594
|
+
previousResponseId: previousResponseTurnRecord?.message.id ?? body.previous_response_id,
|
|
595
|
+
conversationId: threadContext?.threadId ?? body.conversation_id,
|
|
596
|
+
configuredTools,
|
|
597
|
+
responseMetadata,
|
|
598
|
+
fallbackText: ""
|
|
599
|
+
});
|
|
600
|
+
return jsonResponse(response);
|
|
601
|
+
}
|
|
602
|
+
const streamResult = await executeStream({
|
|
603
|
+
agent,
|
|
604
|
+
model: body.model,
|
|
605
|
+
instructions: body.instructions,
|
|
606
|
+
text: body.text,
|
|
607
|
+
providerOptions: body.providerOptions,
|
|
608
|
+
input: executionInput,
|
|
609
|
+
requestContext,
|
|
610
|
+
abortSignal,
|
|
611
|
+
threadContext
|
|
612
|
+
});
|
|
613
|
+
const stream = createResponseEventStream({
|
|
614
|
+
body,
|
|
615
|
+
configuredTools,
|
|
616
|
+
createdAt,
|
|
617
|
+
didStore,
|
|
618
|
+
mastra,
|
|
619
|
+
previousResponseTurnRecord,
|
|
620
|
+
responseId,
|
|
621
|
+
responseMetadata,
|
|
622
|
+
streamResult,
|
|
623
|
+
threadContext
|
|
624
|
+
});
|
|
625
|
+
return new Response(stream, {
|
|
626
|
+
headers: {
|
|
627
|
+
"Content-Type": "text/event-stream; charset=utf-8",
|
|
628
|
+
"Cache-Control": "no-cache",
|
|
629
|
+
Connection: "keep-alive",
|
|
630
|
+
"X-Accel-Buffering": "no"
|
|
631
|
+
}
|
|
632
|
+
});
|
|
633
|
+
} catch (error) {
|
|
634
|
+
return chunkB34S64RC_cjs.handleError(error, "Error creating response");
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
});
|
|
638
|
+
var GET_RESPONSE_ROUTE = chunkEGSCXVMR_cjs.createRoute({
|
|
639
|
+
method: "GET",
|
|
640
|
+
path: "/v1/responses/:responseId",
|
|
641
|
+
responseType: "json",
|
|
642
|
+
pathParamSchema: chunk6MFKRF4K_cjs.responseIdPathParams,
|
|
643
|
+
responseSchema: chunk6MFKRF4K_cjs.responseObjectSchema,
|
|
644
|
+
summary: "Retrieve a stored response",
|
|
645
|
+
description: "Returns a previously stored response object",
|
|
646
|
+
tags: ["Responses"],
|
|
647
|
+
requiresAuth: true,
|
|
648
|
+
requiresPermission: "agents:read",
|
|
649
|
+
handler: async ({ mastra, requestContext, responseId }) => {
|
|
650
|
+
try {
|
|
651
|
+
const responseTurnRecord = await chunkB6HNPO67_cjs.findResponseTurnRecord({ mastra, responseId, requestContext });
|
|
652
|
+
if (!responseTurnRecord) {
|
|
653
|
+
throw new chunk64ITUOXI_cjs.HTTPException(404, { message: `Stored response ${responseId} was not found` });
|
|
654
|
+
}
|
|
655
|
+
return chunkOJ72T77G_cjs.mapResponseTurnRecordToResponse(responseTurnRecord);
|
|
656
|
+
} catch (error) {
|
|
657
|
+
return chunkB34S64RC_cjs.handleError(error, "Error retrieving response");
|
|
658
|
+
}
|
|
659
|
+
}
|
|
660
|
+
});
|
|
661
|
+
var DELETE_RESPONSE_ROUTE = chunkEGSCXVMR_cjs.createRoute({
|
|
662
|
+
method: "DELETE",
|
|
663
|
+
path: "/v1/responses/:responseId",
|
|
664
|
+
responseType: "json",
|
|
665
|
+
pathParamSchema: chunk6MFKRF4K_cjs.responseIdPathParams,
|
|
666
|
+
responseSchema: chunk6MFKRF4K_cjs.deleteResponseSchema,
|
|
667
|
+
summary: "Delete a stored response",
|
|
668
|
+
description: "Deletes a stored response so it can no longer be retrieved or chained",
|
|
669
|
+
tags: ["Responses"],
|
|
670
|
+
requiresAuth: true,
|
|
671
|
+
requiresPermission: "agents:delete",
|
|
672
|
+
handler: async ({ mastra, requestContext, responseId }) => {
|
|
673
|
+
try {
|
|
674
|
+
const deleted = await chunkB6HNPO67_cjs.deleteResponseTurnRecord({ mastra, responseId, requestContext });
|
|
675
|
+
if (!deleted) {
|
|
676
|
+
throw new chunk64ITUOXI_cjs.HTTPException(404, { message: `Stored response ${responseId} was not found` });
|
|
677
|
+
}
|
|
678
|
+
const response = {
|
|
679
|
+
id: responseId,
|
|
680
|
+
object: "response",
|
|
681
|
+
deleted: true
|
|
682
|
+
};
|
|
683
|
+
return response;
|
|
684
|
+
} catch (error) {
|
|
685
|
+
return chunkB34S64RC_cjs.handleError(error, "Error deleting response");
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
});
|
|
689
|
+
|
|
690
|
+
exports.CREATE_RESPONSE_ROUTE = CREATE_RESPONSE_ROUTE;
|
|
691
|
+
exports.DELETE_RESPONSE_ROUTE = DELETE_RESPONSE_ROUTE;
|
|
692
|
+
exports.GET_RESPONSE_ROUTE = GET_RESPONSE_ROUTE;
|
|
693
|
+
exports.responses_exports = responses_exports;
|
|
694
|
+
//# sourceMappingURL=chunk-IRRJW5EA.cjs.map
|
|
695
|
+
//# sourceMappingURL=chunk-IRRJW5EA.cjs.map
|