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