@mastra/cursor 0.3.0 → 0.3.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 +3 -2
- 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/index.cjs +831 -1033
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +830 -1031
- package/dist/index.js.map +1 -1
- package/package.json +17 -17
- package/CHANGELOG.md +0 -171
package/dist/index.cjs
CHANGED
|
@@ -1,1099 +1,897 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
// 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 _cursor_sdk = require("@cursor/sdk");
|
|
5
|
+
let _mastra_core_agent = require("@mastra/core/agent");
|
|
6
|
+
let _mastra_core_request_context = require("@mastra/core/request-context");
|
|
7
|
+
let _mastra_core_stream = require("@mastra/core/stream");
|
|
8
|
+
let _mastra_core_agent_message_list = require("@mastra/core/agent/message-list");
|
|
9
|
+
let _mastra_core_observability = require("@mastra/core/observability");
|
|
10
|
+
//#region src/utils.ts
|
|
13
11
|
function createNoopModel({ modelId, provider }) {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
12
|
+
return {
|
|
13
|
+
modelId,
|
|
14
|
+
provider,
|
|
15
|
+
specificationVersion: "v3",
|
|
16
|
+
supportedUrls: {},
|
|
17
|
+
doGenerate: async () => createNoopStreamResult(),
|
|
18
|
+
doStream: async () => createNoopStreamResult()
|
|
19
|
+
};
|
|
22
20
|
}
|
|
23
21
|
function createNoopStreamResult() {
|
|
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
|
-
}
|
|
97
|
-
function
|
|
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
|
-
|
|
287
|
-
providerMetadata
|
|
288
|
-
});
|
|
289
|
-
return;
|
|
290
|
-
}
|
|
291
|
-
modelSpan?.end({
|
|
292
|
-
output: {
|
|
293
|
-
text
|
|
294
|
-
},
|
|
295
|
-
attributes: {
|
|
296
|
-
finishReason,
|
|
297
|
-
responseId,
|
|
298
|
-
responseModel,
|
|
299
|
-
usage: usage ? toUsageStats(usage) : void 0,
|
|
300
|
-
costContext
|
|
301
|
-
}
|
|
302
|
-
});
|
|
303
|
-
};
|
|
304
|
-
const end = (result) => {
|
|
305
|
-
if (ended) {
|
|
306
|
-
return;
|
|
307
|
-
}
|
|
308
|
-
ended = true;
|
|
309
|
-
closeOpenToolSpans(true);
|
|
310
|
-
endModel(result);
|
|
311
|
-
agentSpan?.end({
|
|
312
|
-
output: {
|
|
313
|
-
text: result.text
|
|
314
|
-
}
|
|
315
|
-
});
|
|
316
|
-
};
|
|
317
|
-
const fail = (error) => {
|
|
318
|
-
if (ended) {
|
|
319
|
-
return;
|
|
320
|
-
}
|
|
321
|
-
ended = true;
|
|
322
|
-
const normalized = error instanceof Error ? error : new Error(String(error));
|
|
323
|
-
closeOpenToolSpans(false, normalized);
|
|
324
|
-
if (modelSpanTracker) {
|
|
325
|
-
modelSpanTracker.reportGenerationError({ error: normalized });
|
|
326
|
-
} else {
|
|
327
|
-
modelSpan?.error({ error: normalized });
|
|
328
|
-
}
|
|
329
|
-
agentSpan?.error({ error: normalized });
|
|
330
|
-
};
|
|
331
|
-
return {
|
|
332
|
-
execute: (fn) => observability.executeWithContext({ span: modelSpan ?? agentSpan, fn }),
|
|
333
|
-
endGenerate(result) {
|
|
334
|
-
end({
|
|
335
|
-
text: result.content.map((part) => part.text).join(""),
|
|
336
|
-
usage: toLanguageModelUsage(result.usage),
|
|
337
|
-
providerMetadata: result.providerMetadata,
|
|
338
|
-
finishReason: result.finishReason.unified,
|
|
339
|
-
responseId: result.response.id,
|
|
340
|
-
responseModel: result.response.modelId,
|
|
341
|
-
costContext: result.costContext
|
|
342
|
-
});
|
|
343
|
-
},
|
|
344
|
-
fail,
|
|
345
|
-
startToolCall,
|
|
346
|
-
endToolCall,
|
|
347
|
-
wrapStream(stream) {
|
|
348
|
-
const trackedStream = modelSpanTracker?.wrapStream(stream) ?? stream;
|
|
349
|
-
return wrapStreamForAgentSpan(trackedStream, {
|
|
350
|
-
end,
|
|
351
|
-
fail
|
|
352
|
-
});
|
|
353
|
-
},
|
|
354
|
-
outputOptions() {
|
|
355
|
-
return {
|
|
356
|
-
onFinish,
|
|
357
|
-
onStepFinish,
|
|
358
|
-
requestContext,
|
|
359
|
-
tracingContext: agentSpan ? { currentSpan: agentSpan } : tracingContext
|
|
360
|
-
};
|
|
361
|
-
}
|
|
362
|
-
};
|
|
22
|
+
return { stream: new stream_web.ReadableStream({ start: (controller) => controller.close() }) };
|
|
23
|
+
}
|
|
24
|
+
function createCompletedMastraStream({ runId, prompt, text, responseId, modelId, usage, providerMetadata, costContext, object }) {
|
|
25
|
+
return new stream_web.ReadableStream({ start(controller) {
|
|
26
|
+
const textId = (0, crypto.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 _mastra_core_agent_message_list.MessageList();
|
|
53
|
+
messageList.add(messages, "input");
|
|
54
|
+
messageList.add([{
|
|
55
|
+
role: "assistant",
|
|
56
|
+
content: ""
|
|
57
|
+
}], "response");
|
|
58
|
+
return new _mastra_core_stream.MastraModelOutput({
|
|
59
|
+
model: {
|
|
60
|
+
modelId,
|
|
61
|
+
provider,
|
|
62
|
+
version: "v3"
|
|
63
|
+
},
|
|
64
|
+
stream,
|
|
65
|
+
messageList,
|
|
66
|
+
messageId: (0, crypto.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 = (0, _mastra_core_observability.getOrCreateSpan)({
|
|
97
|
+
type: _mastra_core_observability.SpanType.AGENT_RUN,
|
|
98
|
+
name: `agent run: '${agentId}'`,
|
|
99
|
+
entityType: _mastra_core_observability.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: _mastra_core_observability.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: _mastra_core_observability.SpanType.MCP_TOOL_CALL,
|
|
146
|
+
name: `mcp_tool: '${toolName}' on '${mcp.serverName}'`,
|
|
147
|
+
input,
|
|
148
|
+
entityType: _mastra_core_observability.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: _mastra_core_observability.SpanType.TOOL_CALL,
|
|
162
|
+
name: `tool: '${toolName}'`,
|
|
163
|
+
input,
|
|
164
|
+
entityType: _mastra_core_observability.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) => (0, _mastra_core_observability.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
|
+
};
|
|
363
285
|
}
|
|
364
286
|
function parseMcpToolName(toolName) {
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
toolName: match[2]
|
|
372
|
-
};
|
|
287
|
+
const match = /^mcp__([^_].*?)__(.+)$/.exec(toolName);
|
|
288
|
+
if (!match?.[1] || !match[2]) return;
|
|
289
|
+
return {
|
|
290
|
+
serverName: match[1],
|
|
291
|
+
toolName: match[2]
|
|
292
|
+
};
|
|
373
293
|
}
|
|
374
294
|
function getModelSpanTracker(modelSpan) {
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
}
|
|
378
|
-
return modelSpan.createTracker();
|
|
295
|
+
if (!modelSpan || !("createTracker" in modelSpan)) return;
|
|
296
|
+
return modelSpan.createTracker();
|
|
379
297
|
}
|
|
380
298
|
function wrapStreamForAgentSpan(stream, telemetry) {
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
}
|
|
402
|
-
controller.enqueue(chunk);
|
|
403
|
-
},
|
|
404
|
-
flush() {
|
|
405
|
-
telemetry.end({ text });
|
|
406
|
-
}
|
|
407
|
-
})
|
|
408
|
-
);
|
|
299
|
+
let text = "";
|
|
300
|
+
return stream.pipeThrough(new stream_web.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
|
+
}));
|
|
409
319
|
}
|
|
410
320
|
function toUsageStats(usage) {
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
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
|
+
};
|
|
423
333
|
}
|
|
424
334
|
function getCostContext(value) {
|
|
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
|
-
});
|
|
462
|
-
controller.enqueue({
|
|
463
|
-
type: "text-start",
|
|
464
|
-
runId,
|
|
465
|
-
from: stream.ChunkFrom.AGENT,
|
|
466
|
-
payload: {
|
|
467
|
-
id: textId,
|
|
468
|
-
providerMetadata
|
|
469
|
-
}
|
|
470
|
-
});
|
|
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: _mastra_core_stream.ChunkFrom.AGENT,
|
|
343
|
+
payload: {}
|
|
344
|
+
});
|
|
345
|
+
controller.enqueue({
|
|
346
|
+
type: "step-start",
|
|
347
|
+
runId,
|
|
348
|
+
from: _mastra_core_stream.ChunkFrom.AGENT,
|
|
349
|
+
payload: { request: { body: prompt } }
|
|
350
|
+
});
|
|
351
|
+
controller.enqueue({
|
|
352
|
+
type: "response-metadata",
|
|
353
|
+
runId,
|
|
354
|
+
from: _mastra_core_stream.ChunkFrom.AGENT,
|
|
355
|
+
payload: {
|
|
356
|
+
id: responseId,
|
|
357
|
+
modelId,
|
|
358
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
359
|
+
}
|
|
360
|
+
});
|
|
361
|
+
controller.enqueue({
|
|
362
|
+
type: "text-start",
|
|
363
|
+
runId,
|
|
364
|
+
from: _mastra_core_stream.ChunkFrom.AGENT,
|
|
365
|
+
payload: {
|
|
366
|
+
id: textId,
|
|
367
|
+
providerMetadata
|
|
368
|
+
}
|
|
369
|
+
});
|
|
471
370
|
}
|
|
472
371
|
function enqueueTextDelta(controller, runId, textId, text) {
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
}
|
|
483
|
-
function enqueueFinishChunks(controller, {
|
|
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
|
-
|
|
556
|
-
usage,
|
|
557
|
-
steps: []
|
|
558
|
-
},
|
|
559
|
-
metadata,
|
|
560
|
-
providerMetadata,
|
|
561
|
-
messages: {
|
|
562
|
-
all: [],
|
|
563
|
-
user: [],
|
|
564
|
-
nonUser: []
|
|
565
|
-
},
|
|
566
|
-
response
|
|
567
|
-
}
|
|
568
|
-
});
|
|
372
|
+
controller.enqueue({
|
|
373
|
+
type: "text-delta",
|
|
374
|
+
runId,
|
|
375
|
+
from: _mastra_core_stream.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
|
+
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: _mastra_core_stream.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: _mastra_core_stream.ChunkFrom.AGENT,
|
|
409
|
+
object
|
|
410
|
+
});
|
|
411
|
+
controller.enqueue({
|
|
412
|
+
type: "step-finish",
|
|
413
|
+
runId,
|
|
414
|
+
from: _mastra_core_stream.ChunkFrom.AGENT,
|
|
415
|
+
payload: {
|
|
416
|
+
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: _mastra_core_stream.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
|
+
});
|
|
569
455
|
}
|
|
570
456
|
function toLanguageModelUsage(usage) {
|
|
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
|
+
raw: usage
|
|
466
|
+
};
|
|
581
467
|
}
|
|
582
468
|
function createProviderMetadata(provider, metadata) {
|
|
583
|
-
|
|
584
|
-
[provider]: toJsonRecord(metadata)
|
|
585
|
-
};
|
|
469
|
+
return { [provider]: toJsonRecord(metadata) };
|
|
586
470
|
}
|
|
587
471
|
function toJsonRecord(record) {
|
|
588
|
-
|
|
589
|
-
Object.entries(record).filter((entry) => entry[1] !== void 0).map(([key, value]) => [key, toJsonValue(value)])
|
|
590
|
-
);
|
|
472
|
+
return Object.fromEntries(Object.entries(record).filter((entry) => entry[1] !== void 0).map(([key, value]) => [key, toJsonValue(value)]));
|
|
591
473
|
}
|
|
592
474
|
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);
|
|
475
|
+
if (value === null || typeof value === "string" || typeof value === "number" || typeof value === "boolean") return value;
|
|
476
|
+
if (Array.isArray(value)) return value.filter((item) => item !== void 0).map(toJsonValue);
|
|
477
|
+
if (value instanceof Date) return value.toISOString();
|
|
478
|
+
if (typeof value === "object") return toJsonRecord(value);
|
|
479
|
+
return String(value);
|
|
606
480
|
}
|
|
607
481
|
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 "";
|
|
482
|
+
if (typeof prompt === "string") return prompt;
|
|
483
|
+
if (Array.isArray(prompt)) return prompt.map(promptToText).filter(Boolean).join("\n");
|
|
484
|
+
if (!prompt || typeof prompt !== "object") return "";
|
|
485
|
+
const record = prompt;
|
|
486
|
+
if (typeof record.text === "string") return record.text;
|
|
487
|
+
if (typeof record.content === "string") return record.content;
|
|
488
|
+
if (record.content) return promptToText(record.content);
|
|
489
|
+
return "";
|
|
628
490
|
}
|
|
629
491
|
function sumDefined(...values) {
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
var
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
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
|
-
...toCursorCreateOptions(this.options),
|
|
774
|
-
...resumeData.sdkOptions
|
|
775
|
-
});
|
|
776
|
-
}
|
|
492
|
+
const defined = values.filter((value) => typeof value === "number");
|
|
493
|
+
if (defined.length === 0) return;
|
|
494
|
+
return defined.reduce((sum, value) => sum + value, 0);
|
|
495
|
+
}
|
|
496
|
+
//#endregion
|
|
497
|
+
//#region src/index.ts
|
|
498
|
+
const PROVIDER = "@cursor/sdk";
|
|
499
|
+
const MODEL_ID = "cursor-agent-sdk";
|
|
500
|
+
var CursorSDKAgent = class extends _mastra_core_agent.Agent {
|
|
501
|
+
options;
|
|
502
|
+
#mastra;
|
|
503
|
+
#createdAgent;
|
|
504
|
+
constructor(options) {
|
|
505
|
+
super({
|
|
506
|
+
id: options.id,
|
|
507
|
+
name: options.name ?? options.id,
|
|
508
|
+
description: options.description,
|
|
509
|
+
instructions: "",
|
|
510
|
+
model: createNoopModel({
|
|
511
|
+
modelId: getModelId(getRequestedModel(options)),
|
|
512
|
+
provider: PROVIDER
|
|
513
|
+
})
|
|
514
|
+
});
|
|
515
|
+
this.options = options;
|
|
516
|
+
}
|
|
517
|
+
__registerMastra(mastra) {
|
|
518
|
+
super.__registerMastra(mastra);
|
|
519
|
+
this.#mastra = mastra;
|
|
520
|
+
}
|
|
521
|
+
supportsMemory() {
|
|
522
|
+
return false;
|
|
523
|
+
}
|
|
524
|
+
async generate(messages, options) {
|
|
525
|
+
assertStructuredOutputUnsupported(options);
|
|
526
|
+
const sdkAgent = await this.resolveCursorAgent();
|
|
527
|
+
return this.generateWithAgent(messages, sdkAgent, options);
|
|
528
|
+
}
|
|
529
|
+
async generateWithAgent(messages, sdkAgent, options) {
|
|
530
|
+
const prompt = promptToText(messages);
|
|
531
|
+
const runId = options?.runId ?? (0, crypto.randomUUID)();
|
|
532
|
+
const modelId = getCursorModelId(this.options, sdkAgent);
|
|
533
|
+
const requestContext = options?.requestContext ?? new _mastra_core_request_context.RequestContext();
|
|
534
|
+
const instructions = options?.instructions ? promptToText(options.instructions) : void 0;
|
|
535
|
+
const telemetry = createSDKAgentTelemetry({
|
|
536
|
+
agentId: this.id,
|
|
537
|
+
agentName: this.name,
|
|
538
|
+
provider: PROVIDER,
|
|
539
|
+
modelId,
|
|
540
|
+
messages,
|
|
541
|
+
prompt,
|
|
542
|
+
runId,
|
|
543
|
+
streaming: false,
|
|
544
|
+
method: "generate",
|
|
545
|
+
requestContext,
|
|
546
|
+
instructions,
|
|
547
|
+
maxSteps: options?.maxSteps,
|
|
548
|
+
tracingOptions: options?.tracingOptions,
|
|
549
|
+
tracingContext: options?.tracingContext,
|
|
550
|
+
onFinish: options?.onFinish,
|
|
551
|
+
onStepFinish: options?.onStepFinish,
|
|
552
|
+
mastra: this.#mastra
|
|
553
|
+
});
|
|
554
|
+
let result;
|
|
555
|
+
try {
|
|
556
|
+
result = await telemetry.execute(() => runCursorGenerate(prompt, this.options, sdkAgent, telemetry));
|
|
557
|
+
telemetry.endGenerate(result);
|
|
558
|
+
} catch (error) {
|
|
559
|
+
telemetry.fail(error);
|
|
560
|
+
throw error;
|
|
561
|
+
}
|
|
562
|
+
return toFullOutput({
|
|
563
|
+
messages,
|
|
564
|
+
runId,
|
|
565
|
+
provider: PROVIDER,
|
|
566
|
+
result,
|
|
567
|
+
options: telemetry.outputOptions()
|
|
568
|
+
});
|
|
569
|
+
}
|
|
570
|
+
async stream(messages, options) {
|
|
571
|
+
assertStructuredOutputUnsupported(options);
|
|
572
|
+
const sdkAgent = await this.resolveCursorAgent();
|
|
573
|
+
return this.streamWithAgent(messages, sdkAgent, options);
|
|
574
|
+
}
|
|
575
|
+
async streamWithAgent(messages, sdkAgent, options) {
|
|
576
|
+
const runId = options?.runId ?? (0, crypto.randomUUID)();
|
|
577
|
+
const prompt = promptToText(messages);
|
|
578
|
+
const modelId = getCursorModelId(this.options, sdkAgent);
|
|
579
|
+
const requestContext = options?.requestContext ?? new _mastra_core_request_context.RequestContext();
|
|
580
|
+
const instructions = options?.instructions ? promptToText(options.instructions) : void 0;
|
|
581
|
+
const telemetry = createSDKAgentTelemetry({
|
|
582
|
+
agentId: this.id,
|
|
583
|
+
agentName: this.name,
|
|
584
|
+
provider: PROVIDER,
|
|
585
|
+
modelId,
|
|
586
|
+
messages,
|
|
587
|
+
prompt,
|
|
588
|
+
runId,
|
|
589
|
+
streaming: true,
|
|
590
|
+
method: "stream",
|
|
591
|
+
requestContext,
|
|
592
|
+
instructions,
|
|
593
|
+
maxSteps: options?.maxSteps,
|
|
594
|
+
tracingOptions: options?.tracingOptions,
|
|
595
|
+
tracingContext: options?.tracingContext,
|
|
596
|
+
onFinish: options?.onFinish,
|
|
597
|
+
onStepFinish: options?.onStepFinish,
|
|
598
|
+
mastra: this.#mastra
|
|
599
|
+
});
|
|
600
|
+
return createMastraOutput({
|
|
601
|
+
messages,
|
|
602
|
+
runId,
|
|
603
|
+
modelId,
|
|
604
|
+
provider: PROVIDER,
|
|
605
|
+
stream: telemetry.wrapStream(runCursorAsMastraStream(prompt, this.options, sdkAgent, runId, telemetry)),
|
|
606
|
+
options: telemetry.outputOptions()
|
|
607
|
+
});
|
|
608
|
+
}
|
|
609
|
+
async resumeGenerate(resumeData, options) {
|
|
610
|
+
assertStructuredOutputUnsupported(options);
|
|
611
|
+
const data = validateCursorResumeData(resumeData);
|
|
612
|
+
const sdkAgent = await this.resolveResumeCursorAgent(data);
|
|
613
|
+
return this.generateWithAgent(data.message, sdkAgent, options);
|
|
614
|
+
}
|
|
615
|
+
async resumeStream(resumeData, options) {
|
|
616
|
+
assertStructuredOutputUnsupported(options);
|
|
617
|
+
const data = validateCursorResumeData(resumeData);
|
|
618
|
+
const sdkAgent = await this.resolveResumeCursorAgent(data);
|
|
619
|
+
return this.streamWithAgent(data.message, sdkAgent, options);
|
|
620
|
+
}
|
|
621
|
+
resolveCursorAgent() {
|
|
622
|
+
this.#createdAgent ??= resolveCursorAgent(this.options.agent, this.options).catch((error) => {
|
|
623
|
+
this.#createdAgent = void 0;
|
|
624
|
+
throw error;
|
|
625
|
+
});
|
|
626
|
+
return this.#createdAgent;
|
|
627
|
+
}
|
|
628
|
+
async resolveResumeCursorAgent(resumeData) {
|
|
629
|
+
if (!resumeData.agentId) return this.resolveCursorAgent();
|
|
630
|
+
return _cursor_sdk.Agent.resume(resumeData.agentId, {
|
|
631
|
+
...toCursorCreateOptions(this.options),
|
|
632
|
+
...resumeData.sdkOptions
|
|
633
|
+
});
|
|
634
|
+
}
|
|
777
635
|
};
|
|
778
636
|
function validateCursorResumeData(resumeData) {
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
if (resumeData.agentId !== void 0 && typeof resumeData.agentId !== "string") {
|
|
783
|
-
throw new Error("CursorSDKAgent resumeData.agentId must be a string when provided.");
|
|
784
|
-
}
|
|
785
|
-
return resumeData;
|
|
637
|
+
if (!toRecord(resumeData) || !("message" in resumeData)) throw new Error("CursorSDKAgent resumeData must include a message.");
|
|
638
|
+
if (resumeData.agentId !== void 0 && typeof resumeData.agentId !== "string") throw new Error("CursorSDKAgent resumeData.agentId must be a string when provided.");
|
|
639
|
+
return resumeData;
|
|
786
640
|
}
|
|
787
641
|
function assertStructuredOutputUnsupported(options) {
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
throw new Error(
|
|
791
|
-
"CursorSDKAgent does not support structuredOutput because the Cursor TypeScript SDK does not expose a schema-constrained output API."
|
|
792
|
-
);
|
|
793
|
-
}
|
|
642
|
+
const structuredOutput = toRecord(toRecord(options)?.structuredOutput);
|
|
643
|
+
if (structuredOutput && "schema" in structuredOutput) throw new Error("CursorSDKAgent does not support structuredOutput because the Cursor TypeScript SDK does not expose a schema-constrained output API.");
|
|
794
644
|
}
|
|
795
645
|
async function runCursorGenerate(prompt, options, agent, telemetry) {
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
timestamp: /* @__PURE__ */ new Date()
|
|
820
|
-
},
|
|
821
|
-
providerMetadata
|
|
822
|
-
};
|
|
646
|
+
const usage = createCursorUsageCollector();
|
|
647
|
+
const run = await agent.send(prompt, createCursorSendOptions(options, usage, telemetry));
|
|
648
|
+
const result = await run.wait();
|
|
649
|
+
if (result.status === "error" || result.status === "cancelled") throw new Error(`Cursor run ${result.id} ended with status ${result.status}`);
|
|
650
|
+
const responseModel = getModelId(result.model ?? run.model ?? getRequestedModel(options) ?? agent.model);
|
|
651
|
+
const providerMetadata = getCursorProviderMetadata(options, agent.agentId, result.id, result.status, result.durationMs, usage.totals(), responseModel);
|
|
652
|
+
return {
|
|
653
|
+
content: [{
|
|
654
|
+
type: "text",
|
|
655
|
+
text: result.result ?? ""
|
|
656
|
+
}],
|
|
657
|
+
finishReason: {
|
|
658
|
+
unified: "stop",
|
|
659
|
+
raw: "stop"
|
|
660
|
+
},
|
|
661
|
+
usage: usage.toV3Usage(),
|
|
662
|
+
response: {
|
|
663
|
+
id: result.id,
|
|
664
|
+
modelId: responseModel,
|
|
665
|
+
timestamp: /* @__PURE__ */ new Date()
|
|
666
|
+
},
|
|
667
|
+
providerMetadata
|
|
668
|
+
};
|
|
823
669
|
}
|
|
824
670
|
function runCursorAsMastraStream(prompt, options, agent, runId, telemetry) {
|
|
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
|
-
|
|
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
|
-
enqueueFinishChunks(controller, {
|
|
887
|
-
runId,
|
|
888
|
-
prompt,
|
|
889
|
-
textId,
|
|
890
|
-
text,
|
|
891
|
-
responseId,
|
|
892
|
-
modelId: getModelId(result.model ?? run.model ?? getRequestedModel(options) ?? agent.model),
|
|
893
|
-
usage: usage.toLanguageModelUsage(),
|
|
894
|
-
providerMetadata
|
|
895
|
-
});
|
|
896
|
-
controller.close();
|
|
897
|
-
} catch (error) {
|
|
898
|
-
controller.enqueue({
|
|
899
|
-
type: "error",
|
|
900
|
-
runId,
|
|
901
|
-
from: stream.ChunkFrom.AGENT,
|
|
902
|
-
payload: { error }
|
|
903
|
-
});
|
|
904
|
-
controller.close();
|
|
905
|
-
}
|
|
906
|
-
}
|
|
907
|
-
});
|
|
671
|
+
return new stream_web.ReadableStream({ start: async (controller) => {
|
|
672
|
+
const textId = (0, crypto.randomUUID)();
|
|
673
|
+
const usage = createCursorUsageCollector();
|
|
674
|
+
let text = "";
|
|
675
|
+
try {
|
|
676
|
+
const run = await agent.send(prompt, createCursorSendOptions(options, usage, telemetry));
|
|
677
|
+
const responseId = run.id;
|
|
678
|
+
const responseModel = getModelId(run.model ?? getRequestedModel(options) ?? agent.model);
|
|
679
|
+
enqueueStartChunks(controller, {
|
|
680
|
+
runId,
|
|
681
|
+
prompt,
|
|
682
|
+
textId,
|
|
683
|
+
responseId,
|
|
684
|
+
modelId: responseModel,
|
|
685
|
+
providerMetadata: getCursorProviderMetadata(options, agent.agentId, run.id, run.status, run.durationMs, usage.totals(), responseModel)
|
|
686
|
+
});
|
|
687
|
+
let result;
|
|
688
|
+
if (run.supports("stream")) {
|
|
689
|
+
for await (const message of run.stream()) {
|
|
690
|
+
const delta = getTextFromCursorMessage(message);
|
|
691
|
+
if (delta) {
|
|
692
|
+
text += delta;
|
|
693
|
+
enqueueTextDelta(controller, runId, textId, delta);
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
result = await run.wait();
|
|
697
|
+
} else {
|
|
698
|
+
result = await run.wait();
|
|
699
|
+
if (result.status === "error" || result.status === "cancelled") throw new Error(`Cursor run ${result.id} ended with status ${result.status}`);
|
|
700
|
+
if (result.result) {
|
|
701
|
+
text += result.result;
|
|
702
|
+
enqueueTextDelta(controller, runId, textId, result.result);
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
if (result.status === "error" || result.status === "cancelled") throw new Error(`Cursor run ${result.id} ended with status ${result.status}`);
|
|
706
|
+
if (!text && result.result) {
|
|
707
|
+
text = result.result;
|
|
708
|
+
enqueueTextDelta(controller, runId, textId, result.result);
|
|
709
|
+
}
|
|
710
|
+
const providerMetadata = getCursorProviderMetadata(options, agent.agentId, run.id, result.status, result.durationMs, usage.totals(), getModelId(result.model ?? run.model ?? getRequestedModel(options) ?? agent.model));
|
|
711
|
+
enqueueFinishChunks(controller, {
|
|
712
|
+
runId,
|
|
713
|
+
prompt,
|
|
714
|
+
textId,
|
|
715
|
+
text,
|
|
716
|
+
responseId,
|
|
717
|
+
modelId: getModelId(result.model ?? run.model ?? getRequestedModel(options) ?? agent.model),
|
|
718
|
+
usage: usage.toLanguageModelUsage(),
|
|
719
|
+
providerMetadata
|
|
720
|
+
});
|
|
721
|
+
controller.close();
|
|
722
|
+
} catch (error) {
|
|
723
|
+
controller.enqueue({
|
|
724
|
+
type: "error",
|
|
725
|
+
runId,
|
|
726
|
+
from: _mastra_core_stream.ChunkFrom.AGENT,
|
|
727
|
+
payload: { error }
|
|
728
|
+
});
|
|
729
|
+
controller.close();
|
|
730
|
+
}
|
|
731
|
+
} });
|
|
908
732
|
}
|
|
909
733
|
async function resolveCursorAgent(agent, options) {
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
}
|
|
913
|
-
return typeof agent === "function" ? agent(toCursorCreateOptions(options)) : agent;
|
|
734
|
+
if (!agent) return _cursor_sdk.Agent.create(toCursorCreateOptions(options));
|
|
735
|
+
return typeof agent === "function" ? agent(toCursorCreateOptions(options)) : agent;
|
|
914
736
|
}
|
|
915
737
|
function toCursorCreateOptions(options) {
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
738
|
+
const createOptions = { ...options.sdkOptions };
|
|
739
|
+
const apiKey = createOptions.apiKey ?? process.env["CURSOR_API_KEY"];
|
|
740
|
+
if (apiKey) createOptions.apiKey = apiKey;
|
|
741
|
+
if (options.name && !createOptions.name) createOptions.name = options.name;
|
|
742
|
+
return createOptions;
|
|
921
743
|
}
|
|
922
744
|
function createCursorSendOptions(options, usage, telemetry) {
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
745
|
+
return {
|
|
746
|
+
mcpServers: options.sdkOptions?.mcpServers,
|
|
747
|
+
onDelta: async (args) => {
|
|
748
|
+
usage.record(args.update);
|
|
749
|
+
recordCursorToolTelemetry(args.update, telemetry);
|
|
750
|
+
}
|
|
751
|
+
};
|
|
930
752
|
}
|
|
931
753
|
function createCursorUsageCollector() {
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
}
|
|
962
|
-
};
|
|
754
|
+
const totals = {
|
|
755
|
+
inputTokens: 0,
|
|
756
|
+
outputTokens: 0,
|
|
757
|
+
cacheReadTokens: 0,
|
|
758
|
+
cacheWriteTokens: 0
|
|
759
|
+
};
|
|
760
|
+
return {
|
|
761
|
+
record(update) {
|
|
762
|
+
if (update.type !== "turn-ended" || !update.usage) return;
|
|
763
|
+
totals.inputTokens += update.usage.inputTokens;
|
|
764
|
+
totals.outputTokens += update.usage.outputTokens;
|
|
765
|
+
totals.cacheReadTokens += update.usage.cacheReadTokens;
|
|
766
|
+
totals.cacheWriteTokens += update.usage.cacheWriteTokens;
|
|
767
|
+
},
|
|
768
|
+
totals() {
|
|
769
|
+
return {
|
|
770
|
+
inputTokens: totals.inputTokens || void 0,
|
|
771
|
+
outputTokens: totals.outputTokens || void 0,
|
|
772
|
+
cacheReadTokens: totals.cacheReadTokens || void 0,
|
|
773
|
+
cacheWriteTokens: totals.cacheWriteTokens || void 0
|
|
774
|
+
};
|
|
775
|
+
},
|
|
776
|
+
toV3Usage() {
|
|
777
|
+
return toV3Usage(totals);
|
|
778
|
+
},
|
|
779
|
+
toLanguageModelUsage() {
|
|
780
|
+
return toLanguageModelUsage(toV3Usage(totals));
|
|
781
|
+
}
|
|
782
|
+
};
|
|
963
783
|
}
|
|
964
784
|
function toV3Usage(usage) {
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
785
|
+
const noCache = usage.inputTokens;
|
|
786
|
+
const cacheRead = usage.cacheReadTokens;
|
|
787
|
+
const cacheWrite = usage.cacheWriteTokens;
|
|
788
|
+
const totalInputTokens = sumDefined(noCache, cacheRead, cacheWrite);
|
|
789
|
+
const outputTokens = usage.outputTokens;
|
|
790
|
+
return {
|
|
791
|
+
inputTokens: {
|
|
792
|
+
total: totalInputTokens,
|
|
793
|
+
noCache,
|
|
794
|
+
cacheRead,
|
|
795
|
+
cacheWrite
|
|
796
|
+
},
|
|
797
|
+
outputTokens: {
|
|
798
|
+
total: outputTokens,
|
|
799
|
+
text: outputTokens
|
|
800
|
+
}
|
|
801
|
+
};
|
|
982
802
|
}
|
|
983
803
|
function getRequestedModel(options) {
|
|
984
|
-
|
|
804
|
+
return options.sdkOptions?.model;
|
|
985
805
|
}
|
|
986
806
|
function getModelId(model) {
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
}
|
|
990
|
-
return typeof model === "string" ? model : model.id;
|
|
807
|
+
if (!model) return MODEL_ID;
|
|
808
|
+
return typeof model === "string" ? model : model.id;
|
|
991
809
|
}
|
|
992
810
|
function getCursorModelId(options, agent) {
|
|
993
|
-
|
|
811
|
+
return getModelId(getRequestedModel(options) ?? agent.model);
|
|
994
812
|
}
|
|
995
813
|
function getCursorProviderMetadata(options, agentId, runId, status, durationMs, usage, requestedModel) {
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
814
|
+
return createProviderMetadata("cursor", {
|
|
815
|
+
agentId,
|
|
816
|
+
runId,
|
|
817
|
+
status,
|
|
818
|
+
requestedModel: requestedModel ?? getModelId(getRequestedModel(options)),
|
|
819
|
+
durationMs,
|
|
820
|
+
mcpServerNames: getMcpServerNames(options),
|
|
821
|
+
usage
|
|
822
|
+
});
|
|
1005
823
|
}
|
|
1006
824
|
function getMcpServerNames(options) {
|
|
1007
|
-
|
|
1008
|
-
|
|
825
|
+
const servers = options.sdkOptions?.mcpServers;
|
|
826
|
+
return servers ? Object.keys(servers) : void 0;
|
|
1009
827
|
}
|
|
1010
828
|
function getTextFromCursorMessage(message) {
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
}).filter(Boolean).join("");
|
|
1018
|
-
}
|
|
1019
|
-
if (message.type === "task") {
|
|
1020
|
-
return message.text ?? "";
|
|
1021
|
-
}
|
|
1022
|
-
return "";
|
|
829
|
+
if (message.type === "assistant") return message.message.content.map((block) => {
|
|
830
|
+
if (block.type === "text") return block.text;
|
|
831
|
+
return "";
|
|
832
|
+
}).filter(Boolean).join("");
|
|
833
|
+
if (message.type === "task") return message.text ?? "";
|
|
834
|
+
return "";
|
|
1023
835
|
}
|
|
1024
836
|
function recordCursorToolTelemetry(update, telemetry) {
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
});
|
|
1050
|
-
telemetry.endToolCall({
|
|
1051
|
-
toolCallId: toolCall.toolCallId,
|
|
1052
|
-
output: toolCall.output,
|
|
1053
|
-
isError: toolCall.isError
|
|
1054
|
-
});
|
|
837
|
+
const updateRecord = toRecord(update);
|
|
838
|
+
if (!updateRecord) return;
|
|
839
|
+
const updateType = typeof updateRecord?.type === "string" ? updateRecord.type : void 0;
|
|
840
|
+
if (updateType !== "tool-call-started" && updateType !== "partial-tool-call" && updateType !== "tool-call-completed") return;
|
|
841
|
+
const toolCall = getCursorToolCall(updateRecord);
|
|
842
|
+
if (!toolCall) return;
|
|
843
|
+
if (updateType === "tool-call-started" || updateType === "partial-tool-call") {
|
|
844
|
+
telemetry.startToolCall({
|
|
845
|
+
toolCallId: toolCall.toolCallId,
|
|
846
|
+
toolName: toolCall.toolName,
|
|
847
|
+
input: toolCall.input
|
|
848
|
+
});
|
|
849
|
+
return;
|
|
850
|
+
}
|
|
851
|
+
telemetry.startToolCall({
|
|
852
|
+
toolCallId: toolCall.toolCallId,
|
|
853
|
+
toolName: toolCall.toolName,
|
|
854
|
+
input: toolCall.input
|
|
855
|
+
});
|
|
856
|
+
telemetry.endToolCall({
|
|
857
|
+
toolCallId: toolCall.toolCallId,
|
|
858
|
+
output: toolCall.output,
|
|
859
|
+
isError: toolCall.isError
|
|
860
|
+
});
|
|
1055
861
|
}
|
|
1056
862
|
function getCursorToolCall(update) {
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
}
|
|
1085
|
-
return {
|
|
1086
|
-
toolCallId,
|
|
1087
|
-
toolName,
|
|
1088
|
-
input: "args" in toolCall ? toolCall.args : void 0,
|
|
1089
|
-
output: "result" in toolCall ? toolCall.result : void 0,
|
|
1090
|
-
isError: toolCall.status === "error"
|
|
1091
|
-
};
|
|
863
|
+
const toolCallId = typeof update.callId === "string" ? update.callId : void 0;
|
|
864
|
+
const toolCall = toRecord(update.toolCall);
|
|
865
|
+
if (!toolCallId || !toolCall) return;
|
|
866
|
+
if (toolCall.type === "mcp") {
|
|
867
|
+
const args = toRecord(toolCall.args);
|
|
868
|
+
if (!args) return;
|
|
869
|
+
const providerIdentifier = typeof args?.providerIdentifier === "string" ? args.providerIdentifier : void 0;
|
|
870
|
+
const toolName = typeof args?.toolName === "string" ? args.toolName : void 0;
|
|
871
|
+
if (!providerIdentifier || !toolName) return;
|
|
872
|
+
const result = toRecord(toolCall.result);
|
|
873
|
+
return {
|
|
874
|
+
toolCallId,
|
|
875
|
+
toolName: `mcp__${providerIdentifier}__${toolName}`,
|
|
876
|
+
input: args.args,
|
|
877
|
+
output: result?.value ?? toolCall.result,
|
|
878
|
+
isError: result?.status === "error" || result?.status === "failed"
|
|
879
|
+
};
|
|
880
|
+
}
|
|
881
|
+
const toolName = typeof toolCall.name === "string" ? toolCall.name : typeof toolCall.type === "string" ? toolCall.type : void 0;
|
|
882
|
+
if (!toolName) return;
|
|
883
|
+
return {
|
|
884
|
+
toolCallId,
|
|
885
|
+
toolName,
|
|
886
|
+
input: "args" in toolCall ? toolCall.args : void 0,
|
|
887
|
+
output: "result" in toolCall ? toolCall.result : void 0,
|
|
888
|
+
isError: toolCall.status === "error"
|
|
889
|
+
};
|
|
1092
890
|
}
|
|
1093
891
|
function toRecord(value) {
|
|
1094
|
-
|
|
892
|
+
return value !== null && typeof value === "object" ? value : void 0;
|
|
1095
893
|
}
|
|
1096
|
-
|
|
894
|
+
//#endregion
|
|
1097
895
|
exports.CursorSDKAgent = CursorSDKAgent;
|
|
1098
|
-
|
|
896
|
+
|
|
1099
897
|
//# sourceMappingURL=index.cjs.map
|