@mastra/openai 1.1.0 → 1.1.1-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/LICENSE.md +6 -4
- package/dist/docs/SKILL.md +4 -3
- package/dist/docs/assets/SOURCE_MAP.json +1 -1
- package/dist/docs/references/docs-connections-overview.md +96 -0
- package/dist/docs/references/{docs-agents-sdk-agents.md → docs-connections-sdk-agents.md} +7 -1
- package/dist/docs/references/docs-memory-memory-processors.md +83 -10
- package/dist/index.cjs +881 -1085
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +879 -1082
- package/dist/index.js.map +1 -1
- package/package.json +16 -16
- package/CHANGELOG.md +0 -166
package/dist/index.cjs
CHANGED
|
@@ -1,1177 +1,973 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
// src/index.ts
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
let crypto = require("crypto");
|
|
3
|
+
let stream_web = require("stream/web");
|
|
4
|
+
let _mastra_core_agent = require("@mastra/core/agent");
|
|
5
|
+
let _mastra_core_request_context = require("@mastra/core/request-context");
|
|
6
|
+
let _mastra_core_stream = require("@mastra/core/stream");
|
|
7
|
+
let _openai_agents = require("@openai/agents");
|
|
8
|
+
let _mastra_core_agent_message_list = require("@mastra/core/agent/message-list");
|
|
9
|
+
let _mastra_core_observability = require("@mastra/core/observability");
|
|
10
|
+
let _mastra_core_schema = require("@mastra/core/schema");
|
|
11
|
+
//#region src/utils.ts
|
|
14
12
|
function createNoopModel({ modelId, provider }) {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
13
|
+
return {
|
|
14
|
+
modelId,
|
|
15
|
+
provider,
|
|
16
|
+
specificationVersion: "v3",
|
|
17
|
+
supportedUrls: {},
|
|
18
|
+
doGenerate: async () => createNoopStreamResult(),
|
|
19
|
+
doStream: async () => createNoopStreamResult()
|
|
20
|
+
};
|
|
23
21
|
}
|
|
24
22
|
function createNoopStreamResult() {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
}
|
|
98
|
-
function
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
providerMetadata
|
|
289
|
-
});
|
|
290
|
-
return;
|
|
291
|
-
}
|
|
292
|
-
modelSpan?.end({
|
|
293
|
-
output: {
|
|
294
|
-
text
|
|
295
|
-
},
|
|
296
|
-
attributes: {
|
|
297
|
-
finishReason,
|
|
298
|
-
responseId,
|
|
299
|
-
responseModel,
|
|
300
|
-
usage: usage ? toUsageStats(usage) : void 0,
|
|
301
|
-
costContext
|
|
302
|
-
}
|
|
303
|
-
});
|
|
304
|
-
};
|
|
305
|
-
const end = (result) => {
|
|
306
|
-
if (ended) {
|
|
307
|
-
return;
|
|
308
|
-
}
|
|
309
|
-
ended = true;
|
|
310
|
-
closeOpenToolSpans(true);
|
|
311
|
-
endModel(result);
|
|
312
|
-
agentSpan?.end({
|
|
313
|
-
output: {
|
|
314
|
-
text: result.text
|
|
315
|
-
}
|
|
316
|
-
});
|
|
317
|
-
};
|
|
318
|
-
const fail = (error) => {
|
|
319
|
-
if (ended) {
|
|
320
|
-
return;
|
|
321
|
-
}
|
|
322
|
-
ended = true;
|
|
323
|
-
const normalized = error instanceof Error ? error : new Error(String(error));
|
|
324
|
-
closeOpenToolSpans(false, normalized);
|
|
325
|
-
if (modelSpanTracker) {
|
|
326
|
-
modelSpanTracker.reportGenerationError({ error: normalized });
|
|
327
|
-
} else {
|
|
328
|
-
modelSpan?.error({ error: normalized });
|
|
329
|
-
}
|
|
330
|
-
agentSpan?.error({ error: normalized });
|
|
331
|
-
};
|
|
332
|
-
return {
|
|
333
|
-
execute: (fn) => observability.executeWithContext({ span: modelSpan ?? agentSpan, fn }),
|
|
334
|
-
endGenerate(result) {
|
|
335
|
-
end({
|
|
336
|
-
text: result.content.map((part) => part.text).join(""),
|
|
337
|
-
usage: toLanguageModelUsage(result.usage),
|
|
338
|
-
providerMetadata: result.providerMetadata,
|
|
339
|
-
finishReason: result.finishReason.unified,
|
|
340
|
-
responseId: result.response.id,
|
|
341
|
-
responseModel: result.response.modelId,
|
|
342
|
-
costContext: result.costContext
|
|
343
|
-
});
|
|
344
|
-
},
|
|
345
|
-
fail,
|
|
346
|
-
startToolCall,
|
|
347
|
-
endToolCall,
|
|
348
|
-
wrapStream(stream) {
|
|
349
|
-
const trackedStream = modelSpanTracker?.wrapStream(stream) ?? stream;
|
|
350
|
-
return wrapStreamForAgentSpan(trackedStream, {
|
|
351
|
-
end,
|
|
352
|
-
fail
|
|
353
|
-
});
|
|
354
|
-
},
|
|
355
|
-
outputOptions() {
|
|
356
|
-
return {
|
|
357
|
-
onFinish,
|
|
358
|
-
onStepFinish,
|
|
359
|
-
requestContext,
|
|
360
|
-
tracingContext: agentSpan ? { currentSpan: agentSpan } : tracingContext
|
|
361
|
-
};
|
|
362
|
-
}
|
|
363
|
-
};
|
|
23
|
+
return { stream: new stream_web.ReadableStream({ start: (controller) => controller.close() }) };
|
|
24
|
+
}
|
|
25
|
+
function createCompletedMastraStream({ runId, prompt, text, responseId, modelId, usage, providerMetadata, costContext, object }) {
|
|
26
|
+
return new stream_web.ReadableStream({ start(controller) {
|
|
27
|
+
const textId = (0, crypto.randomUUID)();
|
|
28
|
+
enqueueStartChunks(controller, {
|
|
29
|
+
runId,
|
|
30
|
+
prompt,
|
|
31
|
+
textId,
|
|
32
|
+
responseId,
|
|
33
|
+
modelId,
|
|
34
|
+
providerMetadata
|
|
35
|
+
});
|
|
36
|
+
if (text) enqueueTextDelta(controller, runId, textId, text);
|
|
37
|
+
enqueueFinishChunks(controller, {
|
|
38
|
+
runId,
|
|
39
|
+
prompt,
|
|
40
|
+
textId,
|
|
41
|
+
text,
|
|
42
|
+
responseId,
|
|
43
|
+
modelId,
|
|
44
|
+
usage,
|
|
45
|
+
providerMetadata,
|
|
46
|
+
costContext,
|
|
47
|
+
object
|
|
48
|
+
});
|
|
49
|
+
controller.close();
|
|
50
|
+
} });
|
|
51
|
+
}
|
|
52
|
+
function createMastraOutput({ messages, runId, modelId, provider, stream, options }) {
|
|
53
|
+
const messageList = new _mastra_core_agent_message_list.MessageList();
|
|
54
|
+
messageList.add(messages, "input");
|
|
55
|
+
messageList.add([{
|
|
56
|
+
role: "assistant",
|
|
57
|
+
content: ""
|
|
58
|
+
}], "response");
|
|
59
|
+
return new _mastra_core_stream.MastraModelOutput({
|
|
60
|
+
model: {
|
|
61
|
+
modelId,
|
|
62
|
+
provider,
|
|
63
|
+
version: "v3"
|
|
64
|
+
},
|
|
65
|
+
stream,
|
|
66
|
+
messageList,
|
|
67
|
+
messageId: (0, crypto.randomUUID)(),
|
|
68
|
+
options: {
|
|
69
|
+
...options,
|
|
70
|
+
runId
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
function toFullOutput({ messages, runId, provider, result, options }) {
|
|
75
|
+
const text = result.content.map((part) => part.text).join("");
|
|
76
|
+
const stream = createCompletedMastraStream({
|
|
77
|
+
runId,
|
|
78
|
+
prompt: promptToText(messages),
|
|
79
|
+
text,
|
|
80
|
+
responseId: result.response.id,
|
|
81
|
+
modelId: result.response.modelId,
|
|
82
|
+
usage: toLanguageModelUsage(result.usage),
|
|
83
|
+
providerMetadata: result.providerMetadata,
|
|
84
|
+
costContext: result.costContext,
|
|
85
|
+
object: result.object
|
|
86
|
+
});
|
|
87
|
+
return createMastraOutput({
|
|
88
|
+
messages,
|
|
89
|
+
runId,
|
|
90
|
+
modelId: result.response.modelId,
|
|
91
|
+
provider,
|
|
92
|
+
stream,
|
|
93
|
+
options
|
|
94
|
+
}).getFullOutput();
|
|
95
|
+
}
|
|
96
|
+
function createSDKAgentTelemetry({ agentId, agentName, provider, modelId, messages, prompt, runId, streaming, method, requestContext, instructions, maxSteps, tracingOptions, tracingContext, onFinish, onStepFinish, mastra }) {
|
|
97
|
+
const agentSpan = (0, _mastra_core_observability.getOrCreateSpan)({
|
|
98
|
+
type: _mastra_core_observability.SpanType.AGENT_RUN,
|
|
99
|
+
name: `agent run: '${agentId}'`,
|
|
100
|
+
entityType: _mastra_core_observability.EntityType.AGENT,
|
|
101
|
+
entityId: agentId,
|
|
102
|
+
entityName: agentName,
|
|
103
|
+
input: messages,
|
|
104
|
+
attributes: {
|
|
105
|
+
prompt,
|
|
106
|
+
instructions,
|
|
107
|
+
maxSteps
|
|
108
|
+
},
|
|
109
|
+
metadata: {
|
|
110
|
+
runId,
|
|
111
|
+
sdkAgent: true,
|
|
112
|
+
sdkProvider: provider,
|
|
113
|
+
sdkMethod: method
|
|
114
|
+
},
|
|
115
|
+
tracingOptions,
|
|
116
|
+
tracingContext,
|
|
117
|
+
requestContext,
|
|
118
|
+
mastra
|
|
119
|
+
});
|
|
120
|
+
const modelSpan = agentSpan?.createChildSpan({
|
|
121
|
+
type: _mastra_core_observability.SpanType.MODEL_GENERATION,
|
|
122
|
+
name: `llm: '${modelId}'`,
|
|
123
|
+
input: { messages },
|
|
124
|
+
attributes: {
|
|
125
|
+
model: modelId,
|
|
126
|
+
provider,
|
|
127
|
+
streaming
|
|
128
|
+
},
|
|
129
|
+
metadata: {
|
|
130
|
+
runId,
|
|
131
|
+
sdkAgent: true,
|
|
132
|
+
sdkProvider: provider,
|
|
133
|
+
sdkMethod: method
|
|
134
|
+
},
|
|
135
|
+
requestContext
|
|
136
|
+
});
|
|
137
|
+
const modelSpanTracker = getModelSpanTracker(modelSpan);
|
|
138
|
+
const toolSpans = /* @__PURE__ */ new Map();
|
|
139
|
+
let ended = false;
|
|
140
|
+
const startToolCall = ({ toolCallId, toolName, input }) => {
|
|
141
|
+
if (toolSpans.has(toolCallId)) return;
|
|
142
|
+
const parentSpan = agentSpan ?? modelSpan;
|
|
143
|
+
if (!parentSpan) return;
|
|
144
|
+
const mcp = parseMcpToolName(toolName);
|
|
145
|
+
const span = mcp ? parentSpan.createChildSpan({
|
|
146
|
+
type: _mastra_core_observability.SpanType.MCP_TOOL_CALL,
|
|
147
|
+
name: `mcp_tool: '${toolName}' on '${mcp.serverName}'`,
|
|
148
|
+
input,
|
|
149
|
+
entityType: _mastra_core_observability.EntityType.TOOL,
|
|
150
|
+
entityId: toolName,
|
|
151
|
+
entityName: toolName,
|
|
152
|
+
attributes: { mcpServer: mcp.serverName },
|
|
153
|
+
metadata: {
|
|
154
|
+
runId,
|
|
155
|
+
sdkAgent: true,
|
|
156
|
+
sdkProvider: provider,
|
|
157
|
+
sdkMethod: method,
|
|
158
|
+
toolCallId
|
|
159
|
+
},
|
|
160
|
+
requestContext
|
|
161
|
+
}) : parentSpan.createChildSpan({
|
|
162
|
+
type: _mastra_core_observability.SpanType.TOOL_CALL,
|
|
163
|
+
name: `tool: '${toolName}'`,
|
|
164
|
+
input,
|
|
165
|
+
entityType: _mastra_core_observability.EntityType.TOOL,
|
|
166
|
+
entityId: toolName,
|
|
167
|
+
entityName: toolName,
|
|
168
|
+
attributes: { toolType: "tool" },
|
|
169
|
+
metadata: {
|
|
170
|
+
runId,
|
|
171
|
+
sdkAgent: true,
|
|
172
|
+
sdkProvider: provider,
|
|
173
|
+
sdkMethod: method,
|
|
174
|
+
toolCallId
|
|
175
|
+
},
|
|
176
|
+
requestContext
|
|
177
|
+
});
|
|
178
|
+
toolSpans.set(toolCallId, span);
|
|
179
|
+
};
|
|
180
|
+
const endToolCall = ({ toolCallId, output, isError }) => {
|
|
181
|
+
const span = toolSpans.get(toolCallId);
|
|
182
|
+
if (!span) return;
|
|
183
|
+
toolSpans.delete(toolCallId);
|
|
184
|
+
if (isError) {
|
|
185
|
+
span.error({
|
|
186
|
+
error: output instanceof Error ? output : new Error(typeof output === "string" ? output : "SDK tool call failed"),
|
|
187
|
+
attributes: { success: false }
|
|
188
|
+
});
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
span.end({
|
|
192
|
+
output,
|
|
193
|
+
attributes: { success: true }
|
|
194
|
+
});
|
|
195
|
+
};
|
|
196
|
+
const closeOpenToolSpans = (success, error) => {
|
|
197
|
+
for (const [toolCallId, span] of toolSpans) {
|
|
198
|
+
toolSpans.delete(toolCallId);
|
|
199
|
+
if (success) {
|
|
200
|
+
span.end({ attributes: { success: true } });
|
|
201
|
+
continue;
|
|
202
|
+
}
|
|
203
|
+
const normalized = error instanceof Error ? error : new Error(String(error ?? "SDK agent run failed"));
|
|
204
|
+
span.error({
|
|
205
|
+
error: normalized,
|
|
206
|
+
attributes: { success: false }
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
};
|
|
210
|
+
const endModel = ({ text, usage, providerMetadata, finishReason = "stop", responseId, responseModel, costContext }) => {
|
|
211
|
+
if (modelSpanTracker) {
|
|
212
|
+
modelSpanTracker.endGeneration({
|
|
213
|
+
output: { text },
|
|
214
|
+
attributes: {
|
|
215
|
+
finishReason,
|
|
216
|
+
responseId,
|
|
217
|
+
responseModel,
|
|
218
|
+
costContext
|
|
219
|
+
},
|
|
220
|
+
usage,
|
|
221
|
+
providerMetadata
|
|
222
|
+
});
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
modelSpan?.end({
|
|
226
|
+
output: { text },
|
|
227
|
+
attributes: {
|
|
228
|
+
finishReason,
|
|
229
|
+
responseId,
|
|
230
|
+
responseModel,
|
|
231
|
+
usage: usage ? toUsageStats(usage) : void 0,
|
|
232
|
+
costContext
|
|
233
|
+
}
|
|
234
|
+
});
|
|
235
|
+
};
|
|
236
|
+
const end = (result) => {
|
|
237
|
+
if (ended) return;
|
|
238
|
+
ended = true;
|
|
239
|
+
closeOpenToolSpans(true);
|
|
240
|
+
endModel(result);
|
|
241
|
+
agentSpan?.end({ output: { text: result.text } });
|
|
242
|
+
};
|
|
243
|
+
const fail = (error) => {
|
|
244
|
+
if (ended) return;
|
|
245
|
+
ended = true;
|
|
246
|
+
const normalized = error instanceof Error ? error : new Error(String(error));
|
|
247
|
+
closeOpenToolSpans(false, normalized);
|
|
248
|
+
if (modelSpanTracker) modelSpanTracker.reportGenerationError({ error: normalized });
|
|
249
|
+
else modelSpan?.error({ error: normalized });
|
|
250
|
+
agentSpan?.error({ error: normalized });
|
|
251
|
+
};
|
|
252
|
+
return {
|
|
253
|
+
execute: (fn) => (0, _mastra_core_observability.executeWithContext)({
|
|
254
|
+
span: modelSpan ?? agentSpan,
|
|
255
|
+
fn
|
|
256
|
+
}),
|
|
257
|
+
endGenerate(result) {
|
|
258
|
+
end({
|
|
259
|
+
text: result.content.map((part) => part.text).join(""),
|
|
260
|
+
usage: toLanguageModelUsage(result.usage),
|
|
261
|
+
providerMetadata: result.providerMetadata,
|
|
262
|
+
finishReason: result.finishReason.unified,
|
|
263
|
+
responseId: result.response.id,
|
|
264
|
+
responseModel: result.response.modelId,
|
|
265
|
+
costContext: result.costContext
|
|
266
|
+
});
|
|
267
|
+
},
|
|
268
|
+
fail,
|
|
269
|
+
startToolCall,
|
|
270
|
+
endToolCall,
|
|
271
|
+
wrapStream(stream) {
|
|
272
|
+
return wrapStreamForAgentSpan(modelSpanTracker?.wrapStream(stream) ?? stream, {
|
|
273
|
+
end,
|
|
274
|
+
fail
|
|
275
|
+
});
|
|
276
|
+
},
|
|
277
|
+
outputOptions() {
|
|
278
|
+
return {
|
|
279
|
+
onFinish,
|
|
280
|
+
onStepFinish,
|
|
281
|
+
requestContext,
|
|
282
|
+
tracingContext: agentSpan ? { currentSpan: agentSpan } : tracingContext
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
};
|
|
364
286
|
}
|
|
365
287
|
function parseMcpToolName(toolName) {
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
toolName: match[2]
|
|
373
|
-
};
|
|
288
|
+
const match = /^mcp__([^_].*?)__(.+)$/.exec(toolName);
|
|
289
|
+
if (!match?.[1] || !match[2]) return;
|
|
290
|
+
return {
|
|
291
|
+
serverName: match[1],
|
|
292
|
+
toolName: match[2]
|
|
293
|
+
};
|
|
374
294
|
}
|
|
375
295
|
function getModelSpanTracker(modelSpan) {
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
}
|
|
379
|
-
return modelSpan.createTracker();
|
|
296
|
+
if (!modelSpan || !("createTracker" in modelSpan)) return;
|
|
297
|
+
return modelSpan.createTracker();
|
|
380
298
|
}
|
|
381
299
|
function wrapStreamForAgentSpan(stream, telemetry) {
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
}
|
|
403
|
-
controller.enqueue(chunk);
|
|
404
|
-
},
|
|
405
|
-
flush() {
|
|
406
|
-
telemetry.end({ text });
|
|
407
|
-
}
|
|
408
|
-
})
|
|
409
|
-
);
|
|
300
|
+
let text = "";
|
|
301
|
+
return stream.pipeThrough(new stream_web.TransformStream({
|
|
302
|
+
transform(chunk, controller) {
|
|
303
|
+
if (chunk.type === "text-delta") text += chunk.payload.text;
|
|
304
|
+
if (chunk.type === "finish") telemetry.end({
|
|
305
|
+
text,
|
|
306
|
+
usage: chunk.payload.output.usage,
|
|
307
|
+
providerMetadata: chunk.payload.providerMetadata,
|
|
308
|
+
finishReason: chunk.payload.stepResult.reason,
|
|
309
|
+
responseId: chunk.payload.response?.id,
|
|
310
|
+
responseModel: chunk.payload.response?.modelId,
|
|
311
|
+
costContext: getCostContext(chunk.payload.metadata?.costContext)
|
|
312
|
+
});
|
|
313
|
+
if (chunk.type === "error") telemetry.fail(chunk.payload.error);
|
|
314
|
+
controller.enqueue(chunk);
|
|
315
|
+
},
|
|
316
|
+
flush() {
|
|
317
|
+
telemetry.end({ text });
|
|
318
|
+
}
|
|
319
|
+
}));
|
|
410
320
|
}
|
|
411
321
|
function toUsageStats(usage) {
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
322
|
+
return {
|
|
323
|
+
inputTokens: usage.inputTokens,
|
|
324
|
+
outputTokens: usage.outputTokens,
|
|
325
|
+
inputDetails: {
|
|
326
|
+
cacheRead: usage.cachedInputTokens,
|
|
327
|
+
cacheWrite: usage.cacheCreationInputTokens
|
|
328
|
+
},
|
|
329
|
+
outputDetails: {
|
|
330
|
+
text: usage.outputTokens,
|
|
331
|
+
reasoning: usage.reasoningTokens
|
|
332
|
+
}
|
|
333
|
+
};
|
|
424
334
|
}
|
|
425
335
|
function getCostContext(value) {
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
}
|
|
462
|
-
});
|
|
463
|
-
controller.enqueue({
|
|
464
|
-
type: "text-start",
|
|
465
|
-
runId,
|
|
466
|
-
from: stream.ChunkFrom.AGENT,
|
|
467
|
-
payload: {
|
|
468
|
-
id: textId,
|
|
469
|
-
providerMetadata
|
|
470
|
-
}
|
|
471
|
-
});
|
|
336
|
+
if (!value || typeof value !== "object") return;
|
|
337
|
+
return value;
|
|
338
|
+
}
|
|
339
|
+
function enqueueStartChunks(controller, { runId, prompt, textId, responseId, modelId, providerMetadata }) {
|
|
340
|
+
controller.enqueue({
|
|
341
|
+
type: "start",
|
|
342
|
+
runId,
|
|
343
|
+
from: _mastra_core_stream.ChunkFrom.AGENT,
|
|
344
|
+
payload: {}
|
|
345
|
+
});
|
|
346
|
+
controller.enqueue({
|
|
347
|
+
type: "step-start",
|
|
348
|
+
runId,
|
|
349
|
+
from: _mastra_core_stream.ChunkFrom.AGENT,
|
|
350
|
+
payload: { request: { body: prompt } }
|
|
351
|
+
});
|
|
352
|
+
controller.enqueue({
|
|
353
|
+
type: "response-metadata",
|
|
354
|
+
runId,
|
|
355
|
+
from: _mastra_core_stream.ChunkFrom.AGENT,
|
|
356
|
+
payload: {
|
|
357
|
+
...responseId ? { id: responseId } : {},
|
|
358
|
+
modelId,
|
|
359
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
360
|
+
}
|
|
361
|
+
});
|
|
362
|
+
controller.enqueue({
|
|
363
|
+
type: "text-start",
|
|
364
|
+
runId,
|
|
365
|
+
from: _mastra_core_stream.ChunkFrom.AGENT,
|
|
366
|
+
payload: {
|
|
367
|
+
id: textId,
|
|
368
|
+
providerMetadata
|
|
369
|
+
}
|
|
370
|
+
});
|
|
472
371
|
}
|
|
473
372
|
function enqueueTextDelta(controller, runId, textId, text) {
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
}
|
|
484
|
-
function enqueueFinishChunks(controller, {
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
usage,
|
|
558
|
-
steps: []
|
|
559
|
-
},
|
|
560
|
-
metadata,
|
|
561
|
-
providerMetadata,
|
|
562
|
-
messages: {
|
|
563
|
-
all: [],
|
|
564
|
-
user: [],
|
|
565
|
-
nonUser: []
|
|
566
|
-
},
|
|
567
|
-
response
|
|
568
|
-
}
|
|
569
|
-
});
|
|
373
|
+
controller.enqueue({
|
|
374
|
+
type: "text-delta",
|
|
375
|
+
runId,
|
|
376
|
+
from: _mastra_core_stream.ChunkFrom.AGENT,
|
|
377
|
+
payload: {
|
|
378
|
+
id: textId,
|
|
379
|
+
text
|
|
380
|
+
}
|
|
381
|
+
});
|
|
382
|
+
}
|
|
383
|
+
function enqueueFinishChunks(controller, { runId, prompt, textId, text, responseId, modelId, usage, providerMetadata, costContext, object }) {
|
|
384
|
+
const timestamp = /* @__PURE__ */ new Date();
|
|
385
|
+
const response = {
|
|
386
|
+
...responseId ? { id: responseId } : {},
|
|
387
|
+
modelId,
|
|
388
|
+
timestamp
|
|
389
|
+
};
|
|
390
|
+
const metadata = {
|
|
391
|
+
providerMetadata,
|
|
392
|
+
costContext,
|
|
393
|
+
request: { body: prompt },
|
|
394
|
+
modelId,
|
|
395
|
+
timestamp
|
|
396
|
+
};
|
|
397
|
+
controller.enqueue({
|
|
398
|
+
type: "text-end",
|
|
399
|
+
runId,
|
|
400
|
+
from: _mastra_core_stream.ChunkFrom.AGENT,
|
|
401
|
+
payload: {
|
|
402
|
+
id: textId,
|
|
403
|
+
providerMetadata
|
|
404
|
+
}
|
|
405
|
+
});
|
|
406
|
+
if (object !== void 0) controller.enqueue({
|
|
407
|
+
type: "object-result",
|
|
408
|
+
runId,
|
|
409
|
+
from: _mastra_core_stream.ChunkFrom.AGENT,
|
|
410
|
+
object
|
|
411
|
+
});
|
|
412
|
+
controller.enqueue({
|
|
413
|
+
type: "step-finish",
|
|
414
|
+
runId,
|
|
415
|
+
from: _mastra_core_stream.ChunkFrom.AGENT,
|
|
416
|
+
payload: {
|
|
417
|
+
...responseId ? { id: responseId } : {},
|
|
418
|
+
providerMetadata,
|
|
419
|
+
totalUsage: usage,
|
|
420
|
+
response,
|
|
421
|
+
stepResult: {
|
|
422
|
+
reason: "stop",
|
|
423
|
+
warnings: []
|
|
424
|
+
},
|
|
425
|
+
output: {
|
|
426
|
+
text,
|
|
427
|
+
usage,
|
|
428
|
+
steps: []
|
|
429
|
+
},
|
|
430
|
+
metadata
|
|
431
|
+
}
|
|
432
|
+
});
|
|
433
|
+
controller.enqueue({
|
|
434
|
+
type: "finish",
|
|
435
|
+
runId,
|
|
436
|
+
from: _mastra_core_stream.ChunkFrom.AGENT,
|
|
437
|
+
payload: {
|
|
438
|
+
stepResult: {
|
|
439
|
+
reason: "stop",
|
|
440
|
+
warnings: []
|
|
441
|
+
},
|
|
442
|
+
output: {
|
|
443
|
+
usage,
|
|
444
|
+
steps: []
|
|
445
|
+
},
|
|
446
|
+
metadata,
|
|
447
|
+
providerMetadata,
|
|
448
|
+
messages: {
|
|
449
|
+
all: [],
|
|
450
|
+
user: [],
|
|
451
|
+
nonUser: []
|
|
452
|
+
},
|
|
453
|
+
response
|
|
454
|
+
}
|
|
455
|
+
});
|
|
570
456
|
}
|
|
571
457
|
function toLanguageModelUsage(usage) {
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
458
|
+
const inputTokens = usage.inputTokens.total ?? 0;
|
|
459
|
+
const outputTokens = usage.outputTokens.total ?? 0;
|
|
460
|
+
return {
|
|
461
|
+
inputTokens,
|
|
462
|
+
outputTokens,
|
|
463
|
+
totalTokens: inputTokens + outputTokens,
|
|
464
|
+
cachedInputTokens: usage.inputTokens.cacheRead,
|
|
465
|
+
cacheCreationInputTokens: usage.inputTokens.cacheWrite,
|
|
466
|
+
reasoningTokens: usage.outputTokens.reasoning,
|
|
467
|
+
raw: usage
|
|
468
|
+
};
|
|
583
469
|
}
|
|
584
470
|
function createProviderMetadata(provider, metadata) {
|
|
585
|
-
|
|
586
|
-
[provider]: toJsonRecord(metadata)
|
|
587
|
-
};
|
|
471
|
+
return { [provider]: toJsonRecord(metadata) };
|
|
588
472
|
}
|
|
589
473
|
function toJsonRecord(record) {
|
|
590
|
-
|
|
591
|
-
Object.entries(record).filter((entry) => entry[1] !== void 0).map(([key, value]) => [key, toJsonValue(value)])
|
|
592
|
-
);
|
|
474
|
+
return Object.fromEntries(Object.entries(record).filter((entry) => entry[1] !== void 0).map(([key, value]) => [key, toJsonValue(value)]));
|
|
593
475
|
}
|
|
594
476
|
function toJsonValue(value) {
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
}
|
|
601
|
-
if (value instanceof Date) {
|
|
602
|
-
return value.toISOString();
|
|
603
|
-
}
|
|
604
|
-
if (typeof value === "object") {
|
|
605
|
-
return toJsonRecord(value);
|
|
606
|
-
}
|
|
607
|
-
return String(value);
|
|
477
|
+
if (value === null || typeof value === "string" || typeof value === "number" || typeof value === "boolean") return value;
|
|
478
|
+
if (Array.isArray(value)) return value.filter((item) => item !== void 0).map(toJsonValue);
|
|
479
|
+
if (value instanceof Date) return value.toISOString();
|
|
480
|
+
if (typeof value === "object") return toJsonRecord(value);
|
|
481
|
+
return String(value);
|
|
608
482
|
}
|
|
609
483
|
function promptToText(prompt) {
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
}
|
|
619
|
-
const record = prompt;
|
|
620
|
-
if (typeof record.text === "string") {
|
|
621
|
-
return record.text;
|
|
622
|
-
}
|
|
623
|
-
if (typeof record.content === "string") {
|
|
624
|
-
return record.content;
|
|
625
|
-
}
|
|
626
|
-
if (record.content) {
|
|
627
|
-
return promptToText(record.content);
|
|
628
|
-
}
|
|
629
|
-
return "";
|
|
484
|
+
if (typeof prompt === "string") return prompt;
|
|
485
|
+
if (Array.isArray(prompt)) return prompt.map(promptToText).filter(Boolean).join("\n");
|
|
486
|
+
if (!prompt || typeof prompt !== "object") return "";
|
|
487
|
+
const record = prompt;
|
|
488
|
+
if (typeof record.text === "string") return record.text;
|
|
489
|
+
if (typeof record.content === "string") return record.content;
|
|
490
|
+
if (record.content) return promptToText(record.content);
|
|
491
|
+
return "";
|
|
630
492
|
}
|
|
631
493
|
function getStructuredOutputSchema(structuredOutput) {
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
schema: schema.standardSchemaToJSONSchema(schema.toStandardSchema(structuredOutput.schema))
|
|
640
|
-
};
|
|
494
|
+
if (!structuredOutput?.schema) return;
|
|
495
|
+
return {
|
|
496
|
+
type: "json_schema",
|
|
497
|
+
name: "mastra_output",
|
|
498
|
+
strict: false,
|
|
499
|
+
schema: (0, _mastra_core_schema.standardSchemaToJSONSchema)((0, _mastra_core_schema.toStandardSchema)(structuredOutput.schema))
|
|
500
|
+
};
|
|
641
501
|
}
|
|
642
502
|
async function getStructuredOutputFromValue(value, structuredOutput) {
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
}
|
|
656
|
-
} else {
|
|
657
|
-
parsed = value;
|
|
658
|
-
}
|
|
659
|
-
const schema$1 = schema.toStandardSchema(structuredOutput.schema);
|
|
660
|
-
const result = await schema$1["~standard"].validate(parsed);
|
|
661
|
-
if (!result.issues) {
|
|
662
|
-
return result.value;
|
|
663
|
-
}
|
|
664
|
-
const message = result.issues.map((issue) => `- ${issue.path?.join(".") || "root"}: ${issue.message}`).join("\n");
|
|
665
|
-
return handleStructuredOutputError(new Error(`Structured output validation failed:
|
|
666
|
-
${message}`), structuredOutput);
|
|
503
|
+
if (!structuredOutput?.schema) return;
|
|
504
|
+
let parsed;
|
|
505
|
+
if (typeof value === "string") try {
|
|
506
|
+
parsed = JSON.parse(value);
|
|
507
|
+
} catch (error) {
|
|
508
|
+
return handleStructuredOutputError(new Error("Structured output must be valid JSON.", { cause: error }), structuredOutput);
|
|
509
|
+
}
|
|
510
|
+
else parsed = value;
|
|
511
|
+
const result = await (0, _mastra_core_schema.toStandardSchema)(structuredOutput.schema)["~standard"].validate(parsed);
|
|
512
|
+
if (!result.issues) return result.value;
|
|
513
|
+
const message = result.issues.map((issue) => `- ${issue.path?.join(".") || "root"}: ${issue.message}`).join("\n");
|
|
514
|
+
return handleStructuredOutputError(/* @__PURE__ */ new Error(`Structured output validation failed:\n${message}`), structuredOutput);
|
|
667
515
|
}
|
|
668
516
|
function handleStructuredOutputError(error, structuredOutput) {
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
}
|
|
676
|
-
throw error;
|
|
517
|
+
if (structuredOutput.errorStrategy === "fallback") return structuredOutput.fallbackValue;
|
|
518
|
+
if (structuredOutput.errorStrategy === "warn") {
|
|
519
|
+
structuredOutput.logger?.warn(error.message);
|
|
520
|
+
return;
|
|
521
|
+
}
|
|
522
|
+
throw error;
|
|
677
523
|
}
|
|
678
524
|
function sumDefined(...values) {
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
var
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
525
|
+
const defined = values.filter((value) => typeof value === "number");
|
|
526
|
+
if (defined.length === 0) return;
|
|
527
|
+
return defined.reduce((sum, value) => sum + value, 0);
|
|
528
|
+
}
|
|
529
|
+
//#endregion
|
|
530
|
+
//#region src/index.ts
|
|
531
|
+
const PROVIDER = "@openai/agents";
|
|
532
|
+
const MODEL_ID = "openai-agents-sdk";
|
|
533
|
+
var OpenAISDKAgent = class extends _mastra_core_agent.Agent {
|
|
534
|
+
options;
|
|
535
|
+
#mastra;
|
|
536
|
+
#createdAgent;
|
|
537
|
+
constructor(options) {
|
|
538
|
+
super({
|
|
539
|
+
id: options.id,
|
|
540
|
+
name: options.name ?? options.id,
|
|
541
|
+
description: options.description,
|
|
542
|
+
instructions: "",
|
|
543
|
+
model: createNoopModel({
|
|
544
|
+
modelId: getModelId(options),
|
|
545
|
+
provider: PROVIDER
|
|
546
|
+
})
|
|
547
|
+
});
|
|
548
|
+
this.options = options;
|
|
549
|
+
}
|
|
550
|
+
__registerMastra(mastra) {
|
|
551
|
+
super.__registerMastra(mastra);
|
|
552
|
+
this.#mastra = mastra;
|
|
553
|
+
}
|
|
554
|
+
supportsMemory() {
|
|
555
|
+
return false;
|
|
556
|
+
}
|
|
557
|
+
async generate(messages, options) {
|
|
558
|
+
const prompt = promptToText(messages);
|
|
559
|
+
const runId = options?.runId ?? (0, crypto.randomUUID)();
|
|
560
|
+
const sdkAgent = getRunOpenAIAgent(this.resolveOpenAIAgent(), options);
|
|
561
|
+
const modelId = getModelId(this.options, sdkAgent);
|
|
562
|
+
const requestContext = options?.requestContext ?? new _mastra_core_request_context.RequestContext();
|
|
563
|
+
const instructions = options?.instructions ? promptToText(options.instructions) : void 0;
|
|
564
|
+
const telemetry = createSDKAgentTelemetry({
|
|
565
|
+
agentId: this.id,
|
|
566
|
+
agentName: this.name,
|
|
567
|
+
provider: PROVIDER,
|
|
568
|
+
modelId,
|
|
569
|
+
messages,
|
|
570
|
+
prompt,
|
|
571
|
+
runId,
|
|
572
|
+
streaming: false,
|
|
573
|
+
method: "generate",
|
|
574
|
+
requestContext,
|
|
575
|
+
instructions,
|
|
576
|
+
maxSteps: options?.maxSteps,
|
|
577
|
+
tracingOptions: options?.tracingOptions,
|
|
578
|
+
tracingContext: options?.tracingContext,
|
|
579
|
+
onFinish: options?.onFinish,
|
|
580
|
+
onStepFinish: options?.onStepFinish,
|
|
581
|
+
mastra: this.#mastra
|
|
582
|
+
});
|
|
583
|
+
let result;
|
|
584
|
+
try {
|
|
585
|
+
result = await telemetry.execute(() => runOpenAIGenerate(prompt, sdkAgent, runId, telemetry, options));
|
|
586
|
+
telemetry.endGenerate(result);
|
|
587
|
+
} catch (error) {
|
|
588
|
+
telemetry.fail(error);
|
|
589
|
+
throw error;
|
|
590
|
+
}
|
|
591
|
+
return toFullOutput({
|
|
592
|
+
messages,
|
|
593
|
+
runId,
|
|
594
|
+
provider: PROVIDER,
|
|
595
|
+
result,
|
|
596
|
+
options: {
|
|
597
|
+
...telemetry.outputOptions(),
|
|
598
|
+
structuredOutput: getStructuredOutputOption(options)
|
|
599
|
+
}
|
|
600
|
+
});
|
|
601
|
+
}
|
|
602
|
+
async stream(messages, options) {
|
|
603
|
+
const prompt = promptToText(messages);
|
|
604
|
+
const runId = options?.runId ?? (0, crypto.randomUUID)();
|
|
605
|
+
const sdkAgent = getRunOpenAIAgent(this.resolveOpenAIAgent(), options);
|
|
606
|
+
const modelId = getModelId(this.options, sdkAgent);
|
|
607
|
+
const requestContext = options?.requestContext ?? new _mastra_core_request_context.RequestContext();
|
|
608
|
+
const instructions = options?.instructions ? promptToText(options.instructions) : void 0;
|
|
609
|
+
const telemetry = createSDKAgentTelemetry({
|
|
610
|
+
agentId: this.id,
|
|
611
|
+
agentName: this.name,
|
|
612
|
+
provider: PROVIDER,
|
|
613
|
+
modelId,
|
|
614
|
+
messages,
|
|
615
|
+
prompt,
|
|
616
|
+
runId,
|
|
617
|
+
streaming: true,
|
|
618
|
+
method: "stream",
|
|
619
|
+
requestContext,
|
|
620
|
+
instructions,
|
|
621
|
+
maxSteps: options?.maxSteps,
|
|
622
|
+
tracingOptions: options?.tracingOptions,
|
|
623
|
+
tracingContext: options?.tracingContext,
|
|
624
|
+
onFinish: options?.onFinish,
|
|
625
|
+
onStepFinish: options?.onStepFinish,
|
|
626
|
+
mastra: this.#mastra
|
|
627
|
+
});
|
|
628
|
+
return createMastraOutput({
|
|
629
|
+
messages,
|
|
630
|
+
runId,
|
|
631
|
+
modelId,
|
|
632
|
+
provider: PROVIDER,
|
|
633
|
+
stream: telemetry.wrapStream(runOpenAIAsMastraStream(prompt, sdkAgent, runId, modelId, telemetry, options)),
|
|
634
|
+
options: {
|
|
635
|
+
...telemetry.outputOptions(),
|
|
636
|
+
structuredOutput: getStructuredOutputOption(options)
|
|
637
|
+
}
|
|
638
|
+
});
|
|
639
|
+
}
|
|
640
|
+
async resumeGenerate(resumeData, options) {
|
|
641
|
+
const data = validateOpenAIResumeData(resumeData);
|
|
642
|
+
return this.generate(data.message, createOpenAIResumeRunOptions(data, options));
|
|
643
|
+
}
|
|
644
|
+
async resumeStream(resumeData, options) {
|
|
645
|
+
const data = validateOpenAIResumeData(resumeData);
|
|
646
|
+
return this.stream(data.message, createOpenAIResumeRunOptions(data, options));
|
|
647
|
+
}
|
|
648
|
+
resolveOpenAIAgent() {
|
|
649
|
+
this.#createdAgent ??= this.options.agent ?? new _openai_agents.Agent(toOpenAIAgentOptions(this.options));
|
|
650
|
+
return this.#createdAgent;
|
|
651
|
+
}
|
|
802
652
|
};
|
|
803
653
|
function getStructuredOutputOption(options) {
|
|
804
|
-
|
|
654
|
+
return options?.structuredOutput;
|
|
805
655
|
}
|
|
806
656
|
function validateOpenAIResumeData(resumeData) {
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
if (typeof resumeData.previousResponseId === "string" || typeof resumeData.conversationId === "string" || resumeData.session !== void 0) {
|
|
812
|
-
return resumeData;
|
|
813
|
-
}
|
|
814
|
-
throw new Error("OpenAISDKAgent resumeData must include previousResponseId, conversationId, or session.");
|
|
657
|
+
const record = toRecord(resumeData);
|
|
658
|
+
if (!record || !("message" in record)) throw new Error("OpenAISDKAgent resumeData must include a message.");
|
|
659
|
+
if (typeof resumeData.previousResponseId === "string" || typeof resumeData.conversationId === "string" || resumeData.session !== void 0) return resumeData;
|
|
660
|
+
throw new Error("OpenAISDKAgent resumeData must include previousResponseId, conversationId, or session.");
|
|
815
661
|
}
|
|
816
662
|
function createOpenAIResumeRunOptions(resumeData, options) {
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
663
|
+
return {
|
|
664
|
+
...options,
|
|
665
|
+
previousResponseId: resumeData.previousResponseId ?? options?.previousResponseId,
|
|
666
|
+
conversationId: resumeData.conversationId ?? options?.conversationId,
|
|
667
|
+
session: resumeData.session ?? options?.session
|
|
668
|
+
};
|
|
823
669
|
}
|
|
824
670
|
async function runOpenAIGenerate(prompt, agent, runId, telemetry, options) {
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
671
|
+
const result = await (0, _openai_agents.run)(agent, prompt, createOpenAIRunOptions(options, false));
|
|
672
|
+
recordOpenAIToolTelemetry(result.newItems, telemetry);
|
|
673
|
+
const text = getTextFromFinalOutput(result.finalOutput);
|
|
674
|
+
const responseId = result.lastResponseId;
|
|
675
|
+
const modelId = getModelId(void 0, result.lastAgent ?? agent, result.rawResponses.at(-1));
|
|
676
|
+
const usage = createOpenAIUsageTotals(result.state.usage);
|
|
677
|
+
const providerMetadata = getOpenAIProviderMetadata({
|
|
678
|
+
modelId,
|
|
679
|
+
responseId,
|
|
680
|
+
lastResponseId: result.lastResponseId,
|
|
681
|
+
rawResponseCount: result.rawResponses.length,
|
|
682
|
+
itemCount: result.newItems.length,
|
|
683
|
+
usage
|
|
684
|
+
});
|
|
685
|
+
return {
|
|
686
|
+
content: [{
|
|
687
|
+
type: "text",
|
|
688
|
+
text
|
|
689
|
+
}],
|
|
690
|
+
finishReason: {
|
|
691
|
+
unified: "stop",
|
|
692
|
+
raw: "stop"
|
|
693
|
+
},
|
|
694
|
+
usage: toV3Usage(usage),
|
|
695
|
+
response: {
|
|
696
|
+
id: responseId,
|
|
697
|
+
modelId,
|
|
698
|
+
timestamp: /* @__PURE__ */ new Date()
|
|
699
|
+
},
|
|
700
|
+
providerMetadata,
|
|
701
|
+
object: await getStructuredOutputFromValue(result.finalOutput, options?.structuredOutput)
|
|
702
|
+
};
|
|
851
703
|
}
|
|
852
704
|
function runOpenAIAsMastraStream(prompt, agent, runId, requestedModelId, telemetry, options) {
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
controller.close();
|
|
914
|
-
}
|
|
915
|
-
}
|
|
916
|
-
});
|
|
705
|
+
return new stream_web.ReadableStream({ start: async (controller) => {
|
|
706
|
+
const textId = (0, crypto.randomUUID)();
|
|
707
|
+
let text = "";
|
|
708
|
+
let responseId;
|
|
709
|
+
let modelId = requestedModelId;
|
|
710
|
+
try {
|
|
711
|
+
const result = await (0, _openai_agents.run)(agent, prompt, createOpenAIRunOptions(options, true));
|
|
712
|
+
enqueueStartChunks(controller, {
|
|
713
|
+
runId,
|
|
714
|
+
prompt,
|
|
715
|
+
textId,
|
|
716
|
+
responseId,
|
|
717
|
+
modelId
|
|
718
|
+
});
|
|
719
|
+
for await (const event of result) {
|
|
720
|
+
const delta = getTextDelta(event);
|
|
721
|
+
if (delta) {
|
|
722
|
+
text += delta;
|
|
723
|
+
enqueueTextDelta(controller, runId, textId, delta);
|
|
724
|
+
}
|
|
725
|
+
recordOpenAIStreamToolTelemetry(event, telemetry);
|
|
726
|
+
}
|
|
727
|
+
await result.completed;
|
|
728
|
+
responseId = result.lastResponseId ?? responseId;
|
|
729
|
+
modelId = getModelId(void 0, result.lastAgent ?? agent, result.rawResponses.at(-1));
|
|
730
|
+
if (!text) {
|
|
731
|
+
text = getTextFromFinalOutput(result.finalOutput);
|
|
732
|
+
if (text) enqueueTextDelta(controller, runId, textId, text);
|
|
733
|
+
}
|
|
734
|
+
const usage = createOpenAIUsageTotals(result.state.usage);
|
|
735
|
+
const providerMetadata = getOpenAIProviderMetadata({
|
|
736
|
+
modelId,
|
|
737
|
+
responseId,
|
|
738
|
+
lastResponseId: result.lastResponseId,
|
|
739
|
+
rawResponseCount: result.rawResponses.length,
|
|
740
|
+
itemCount: result.newItems.length,
|
|
741
|
+
usage
|
|
742
|
+
});
|
|
743
|
+
enqueueFinishChunks(controller, {
|
|
744
|
+
runId,
|
|
745
|
+
prompt,
|
|
746
|
+
textId,
|
|
747
|
+
text,
|
|
748
|
+
responseId,
|
|
749
|
+
modelId,
|
|
750
|
+
usage: toLanguageModelUsage(toV3Usage(usage)),
|
|
751
|
+
providerMetadata,
|
|
752
|
+
object: await getStructuredOutputFromValue(result.finalOutput, options?.structuredOutput)
|
|
753
|
+
});
|
|
754
|
+
controller.close();
|
|
755
|
+
} catch (error) {
|
|
756
|
+
controller.enqueue({
|
|
757
|
+
type: "error",
|
|
758
|
+
runId,
|
|
759
|
+
from: _mastra_core_stream.ChunkFrom.AGENT,
|
|
760
|
+
payload: { error }
|
|
761
|
+
});
|
|
762
|
+
controller.close();
|
|
763
|
+
}
|
|
764
|
+
} });
|
|
917
765
|
}
|
|
918
766
|
function toOpenAIAgentOptions(options) {
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
767
|
+
return {
|
|
768
|
+
name: options.name ?? options.id,
|
|
769
|
+
...options.sdkOptions
|
|
770
|
+
};
|
|
923
771
|
}
|
|
924
772
|
function getRunOpenAIAgent(agent, options) {
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
}
|
|
929
|
-
return agent.clone({ outputType });
|
|
773
|
+
const outputType = getStructuredOutputSchema(options?.structuredOutput);
|
|
774
|
+
if (!outputType) return agent;
|
|
775
|
+
return agent.clone({ outputType });
|
|
930
776
|
}
|
|
931
777
|
function createOpenAIRunOptions(options, stream) {
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
778
|
+
const runOptions = { stream };
|
|
779
|
+
addDefined(runOptions, "maxTurns", options?.maxSteps);
|
|
780
|
+
addDefined(runOptions, "signal", options?.abortSignal ?? options?.signal);
|
|
781
|
+
addDefined(runOptions, "conversationId", options?.conversationId);
|
|
782
|
+
addDefined(runOptions, "previousResponseId", options?.previousResponseId);
|
|
783
|
+
addDefined(runOptions, "session", options?.session);
|
|
784
|
+
return runOptions;
|
|
939
785
|
}
|
|
940
786
|
function addDefined(target, key, value) {
|
|
941
|
-
|
|
942
|
-
target[key] = value;
|
|
943
|
-
}
|
|
787
|
+
if (value !== void 0) target[key] = value;
|
|
944
788
|
}
|
|
945
789
|
function getModelId(options, agent, rawResponse) {
|
|
946
|
-
|
|
790
|
+
return getModelNameFromUnknown(rawResponse) ?? getModelNameFromUnknown(agent?.model) ?? getModelNameFromUnknown(options?.sdkOptions?.model) ?? MODEL_ID;
|
|
947
791
|
}
|
|
948
792
|
function getModelNameFromUnknown(value) {
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
const record = toRecord(value);
|
|
953
|
-
return getString(record, "model") ?? getString(record, "modelId") ?? getString(record, "modelName") ?? getString(toRecord(record?.providerData), "model");
|
|
793
|
+
if (typeof value === "string") return value;
|
|
794
|
+
const record = toRecord(value);
|
|
795
|
+
return getString(record, "model") ?? getString(record, "modelId") ?? getString(record, "modelName") ?? getString(toRecord(record?.providerData), "model");
|
|
954
796
|
}
|
|
955
797
|
function getTextFromFinalOutput(output) {
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
if (output === void 0 || output === null) {
|
|
960
|
-
return "";
|
|
961
|
-
}
|
|
962
|
-
return JSON.stringify(output);
|
|
798
|
+
if (typeof output === "string") return output;
|
|
799
|
+
if (output === void 0 || output === null) return "";
|
|
800
|
+
return JSON.stringify(output);
|
|
963
801
|
}
|
|
964
802
|
function createOpenAIUsageTotals(usage) {
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
requestUsageEntries
|
|
980
|
-
};
|
|
803
|
+
const record = toRecord(usage);
|
|
804
|
+
if (!record) return {};
|
|
805
|
+
const inputDetails = getDetailRecords(record.inputTokensDetails);
|
|
806
|
+
const outputDetails = getDetailRecords(record.outputTokensDetails);
|
|
807
|
+
const requestUsageEntries = Array.isArray(record.requestUsageEntries) ? record.requestUsageEntries : void 0;
|
|
808
|
+
return {
|
|
809
|
+
inputTokens: getNumber(record.inputTokens),
|
|
810
|
+
outputTokens: getNumber(record.outputTokens),
|
|
811
|
+
cacheReadInputTokens: sumDetails(inputDetails, "cachedTokens", "cached_tokens", "cacheReadInputTokens"),
|
|
812
|
+
cacheCreationInputTokens: sumDetails(inputDetails, "cacheCreationInputTokens", "cache_creation_input_tokens"),
|
|
813
|
+
reasoningTokens: sumDetails(outputDetails, "reasoningTokens", "reasoning_tokens"),
|
|
814
|
+
requests: getNumber(record.requests),
|
|
815
|
+
requestUsageEntries
|
|
816
|
+
};
|
|
981
817
|
}
|
|
982
818
|
function getDetailRecords(value) {
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
const record = toRecord(value);
|
|
987
|
-
return record ? [record] : [];
|
|
819
|
+
if (Array.isArray(value)) return value.filter(isRecord);
|
|
820
|
+
const record = toRecord(value);
|
|
821
|
+
return record ? [record] : [];
|
|
988
822
|
}
|
|
989
823
|
function sumDetails(records, ...keys) {
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
}
|
|
1001
|
-
return found ? total : void 0;
|
|
824
|
+
let total = 0;
|
|
825
|
+
let found = false;
|
|
826
|
+
for (const record of records) for (const key of keys) {
|
|
827
|
+
const value = getNumber(record[key]);
|
|
828
|
+
if (value !== void 0) {
|
|
829
|
+
total += value;
|
|
830
|
+
found = true;
|
|
831
|
+
}
|
|
832
|
+
}
|
|
833
|
+
return found ? total : void 0;
|
|
1002
834
|
}
|
|
1003
835
|
function toV3Usage(usage) {
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
}
|
|
1024
|
-
function getOpenAIProviderMetadata({
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
model: modelId,
|
|
1034
|
-
responseId,
|
|
1035
|
-
lastResponseId,
|
|
1036
|
-
rawResponseCount,
|
|
1037
|
-
itemCount,
|
|
1038
|
-
usage
|
|
1039
|
-
});
|
|
836
|
+
const totalInputTokens = usage.inputTokens;
|
|
837
|
+
const cacheRead = usage.cacheReadInputTokens;
|
|
838
|
+
const cacheWrite = usage.cacheCreationInputTokens;
|
|
839
|
+
const noCache = totalInputTokens === void 0 ? void 0 : Math.max(totalInputTokens - (sumDefined(cacheRead, cacheWrite) ?? 0), 0);
|
|
840
|
+
const outputTokens = usage.outputTokens;
|
|
841
|
+
const reasoningTokens = usage.reasoningTokens;
|
|
842
|
+
return {
|
|
843
|
+
inputTokens: {
|
|
844
|
+
total: totalInputTokens,
|
|
845
|
+
noCache,
|
|
846
|
+
cacheRead,
|
|
847
|
+
cacheWrite
|
|
848
|
+
},
|
|
849
|
+
outputTokens: {
|
|
850
|
+
total: outputTokens,
|
|
851
|
+
text: outputTokens,
|
|
852
|
+
reasoning: reasoningTokens
|
|
853
|
+
}
|
|
854
|
+
};
|
|
855
|
+
}
|
|
856
|
+
function getOpenAIProviderMetadata({ modelId, responseId, lastResponseId, rawResponseCount, itemCount, usage }) {
|
|
857
|
+
return createProviderMetadata("openai", {
|
|
858
|
+
model: modelId,
|
|
859
|
+
responseId,
|
|
860
|
+
lastResponseId,
|
|
861
|
+
rawResponseCount,
|
|
862
|
+
itemCount,
|
|
863
|
+
usage
|
|
864
|
+
});
|
|
1040
865
|
}
|
|
1041
866
|
function recordOpenAIToolTelemetry(items, telemetry) {
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
telemetry.endToolCall(toolOutput);
|
|
1054
|
-
}
|
|
1055
|
-
}
|
|
1056
|
-
}
|
|
867
|
+
for (const item of items) {
|
|
868
|
+
if (item.type === "tool_call_item") {
|
|
869
|
+
const toolCall = getOpenAIToolCall(item.rawItem);
|
|
870
|
+
if (toolCall) telemetry.startToolCall(toolCall);
|
|
871
|
+
continue;
|
|
872
|
+
}
|
|
873
|
+
if (item.type === "tool_call_output_item") {
|
|
874
|
+
const toolOutput = getOpenAIToolOutput(item.rawItem, item.output);
|
|
875
|
+
if (toolOutput) telemetry.endToolCall(toolOutput);
|
|
876
|
+
}
|
|
877
|
+
}
|
|
1057
878
|
}
|
|
1058
879
|
function recordOpenAIStreamToolTelemetry(event, telemetry) {
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
if (event.name === "tool_output") {
|
|
1070
|
-
const toolOutput = getOpenAIToolOutput(event.item.rawItem, getObjectValue(event.item, "output"));
|
|
1071
|
-
if (toolOutput) {
|
|
1072
|
-
telemetry.endToolCall(toolOutput);
|
|
1073
|
-
}
|
|
1074
|
-
}
|
|
880
|
+
if (event.type !== "run_item_stream_event") return;
|
|
881
|
+
if (event.name === "tool_called") {
|
|
882
|
+
const toolCall = getOpenAIToolCall(event.item.rawItem);
|
|
883
|
+
if (toolCall) telemetry.startToolCall(toolCall);
|
|
884
|
+
return;
|
|
885
|
+
}
|
|
886
|
+
if (event.name === "tool_output") {
|
|
887
|
+
const toolOutput = getOpenAIToolOutput(event.item.rawItem, getObjectValue(event.item, "output"));
|
|
888
|
+
if (toolOutput) telemetry.endToolCall(toolOutput);
|
|
889
|
+
}
|
|
1075
890
|
}
|
|
1076
891
|
function getOpenAIToolCall(rawItem) {
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
return void 0;
|
|
1109
|
-
}
|
|
1110
|
-
return {
|
|
1111
|
-
toolCallId,
|
|
1112
|
-
toolName: String(record.type),
|
|
1113
|
-
input: record.action ?? record.operation
|
|
1114
|
-
};
|
|
1115
|
-
}
|
|
1116
|
-
return void 0;
|
|
892
|
+
const record = toRecord(rawItem);
|
|
893
|
+
if (!record) return;
|
|
894
|
+
if (record.type === "function_call") {
|
|
895
|
+
const toolCallId = getString(record, "callId") ?? getString(record, "id");
|
|
896
|
+
const toolName = getNamespacedToolName(record);
|
|
897
|
+
if (!toolCallId || !toolName) return;
|
|
898
|
+
return {
|
|
899
|
+
toolCallId,
|
|
900
|
+
toolName,
|
|
901
|
+
input: parseJsonString(record.arguments)
|
|
902
|
+
};
|
|
903
|
+
}
|
|
904
|
+
if (record.type === "hosted_tool_call") {
|
|
905
|
+
const toolCallId = getString(record, "id") ?? getString(record, "name");
|
|
906
|
+
const toolName = getString(record, "name");
|
|
907
|
+
if (!toolCallId || !toolName) return;
|
|
908
|
+
return {
|
|
909
|
+
toolCallId,
|
|
910
|
+
toolName,
|
|
911
|
+
input: parseJsonString(record.arguments)
|
|
912
|
+
};
|
|
913
|
+
}
|
|
914
|
+
if (record.type === "shell_call" || record.type === "apply_patch_call") {
|
|
915
|
+
const toolCallId = getString(record, "callId");
|
|
916
|
+
if (!toolCallId) return;
|
|
917
|
+
return {
|
|
918
|
+
toolCallId,
|
|
919
|
+
toolName: String(record.type),
|
|
920
|
+
input: record.action ?? record.operation
|
|
921
|
+
};
|
|
922
|
+
}
|
|
1117
923
|
}
|
|
1118
924
|
function getOpenAIToolOutput(rawItem, fallbackOutput) {
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
toolCallId,
|
|
1129
|
-
output: record.output ?? fallbackOutput,
|
|
1130
|
-
isError: record.status === "failed" || record.status === "incomplete"
|
|
1131
|
-
};
|
|
925
|
+
const record = toRecord(rawItem);
|
|
926
|
+
if (!record) return;
|
|
927
|
+
const toolCallId = getString(record, "callId") ?? getString(record, "id");
|
|
928
|
+
if (!toolCallId) return;
|
|
929
|
+
return {
|
|
930
|
+
toolCallId,
|
|
931
|
+
output: record.output ?? fallbackOutput,
|
|
932
|
+
isError: record.status === "failed" || record.status === "incomplete"
|
|
933
|
+
};
|
|
1132
934
|
}
|
|
1133
935
|
function getNamespacedToolName(record) {
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
}
|
|
1139
|
-
return namespace ? `${namespace}.${name}` : name;
|
|
936
|
+
const name = getString(record, "name");
|
|
937
|
+
const namespace = getString(record, "namespace");
|
|
938
|
+
if (!name) return;
|
|
939
|
+
return namespace ? `${namespace}.${name}` : name;
|
|
1140
940
|
}
|
|
1141
941
|
function getTextDelta(event) {
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
const data = toRecord(event.data);
|
|
1146
|
-
return data?.type === "output_text_delta" && typeof data.delta === "string" ? data.delta : "";
|
|
942
|
+
if (event.type !== "raw_model_stream_event") return "";
|
|
943
|
+
const data = toRecord(event.data);
|
|
944
|
+
return data?.type === "output_text_delta" && typeof data.delta === "string" ? data.delta : "";
|
|
1147
945
|
}
|
|
1148
946
|
function parseJsonString(value) {
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
return value;
|
|
1156
|
-
}
|
|
947
|
+
if (typeof value !== "string") return value;
|
|
948
|
+
try {
|
|
949
|
+
return JSON.parse(value);
|
|
950
|
+
} catch {
|
|
951
|
+
return value;
|
|
952
|
+
}
|
|
1157
953
|
}
|
|
1158
954
|
function getNumber(value) {
|
|
1159
|
-
|
|
955
|
+
return typeof value === "number" ? value : void 0;
|
|
1160
956
|
}
|
|
1161
957
|
function getString(record, key) {
|
|
1162
|
-
|
|
1163
|
-
|
|
958
|
+
const value = record?.[key];
|
|
959
|
+
return typeof value === "string" ? value : void 0;
|
|
1164
960
|
}
|
|
1165
961
|
function getObjectValue(value, key) {
|
|
1166
|
-
|
|
962
|
+
return toRecord(value)?.[key];
|
|
1167
963
|
}
|
|
1168
964
|
function toRecord(value) {
|
|
1169
|
-
|
|
965
|
+
return isRecord(value) ? value : void 0;
|
|
1170
966
|
}
|
|
1171
967
|
function isRecord(value) {
|
|
1172
|
-
|
|
968
|
+
return value !== null && typeof value === "object";
|
|
1173
969
|
}
|
|
1174
|
-
|
|
970
|
+
//#endregion
|
|
1175
971
|
exports.OpenAISDKAgent = OpenAISDKAgent;
|
|
1176
|
-
|
|
972
|
+
|
|
1177
973
|
//# sourceMappingURL=index.cjs.map
|