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