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