pi-cursor-sdk 0.1.16 → 0.1.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +29 -1
- package/README.md +1 -1
- package/docs/cursor-live-smoke-checklist.md +35 -39
- package/docs/cursor-model-ux-spec.md +3 -2
- package/package.json +11 -5
- package/scripts/steering-rpc-smoke.mjs +238 -0
- package/scripts/tmux-live-smoke.sh +418 -0
- package/scripts/validate-smoke-jsonl.mjs +152 -0
- package/src/cursor-edit-diff.ts +11 -0
- package/src/cursor-env-boolean.ts +22 -0
- package/src/cursor-live-run-coordinator.ts +483 -0
- package/src/cursor-native-tool-display-registration.ts +93 -0
- package/src/cursor-native-tool-display-replay.ts +465 -0
- package/src/cursor-native-tool-display-state.ts +78 -0
- package/src/cursor-native-tool-display-tools.ts +102 -0
- package/src/cursor-native-tool-display.ts +10 -648
- package/src/cursor-partial-content-emitter.ts +121 -0
- package/src/cursor-pi-tool-bridge-abort.ts +133 -0
- package/src/cursor-pi-tool-bridge-diagnostics.ts +179 -0
- package/src/cursor-pi-tool-bridge-mcp.ts +118 -0
- package/src/cursor-pi-tool-bridge-run.ts +384 -0
- package/src/cursor-pi-tool-bridge-server.ts +182 -0
- package/src/cursor-pi-tool-bridge-snapshot.ts +88 -0
- package/src/cursor-pi-tool-bridge-types.ts +80 -0
- package/src/cursor-pi-tool-bridge.ts +42 -1104
- package/src/cursor-provider-live-run-drain.ts +379 -0
- package/src/cursor-provider-turn-coordinator.ts +456 -0
- package/src/cursor-provider.ts +72 -1103
- package/src/cursor-record-utils.ts +26 -0
- package/src/cursor-sdk-output-filter.ts +100 -0
- package/src/cursor-sensitive-text.ts +37 -0
- package/src/cursor-tool-transcript.ts +28 -1229
- package/src/cursor-transcript-tool-formatters.ts +641 -0
- package/src/cursor-transcript-tool-specs.ts +441 -0
- package/src/cursor-transcript-utils.ts +276 -0
package/src/cursor-provider.ts
CHANGED
|
@@ -6,12 +6,11 @@ import {
|
|
|
6
6
|
type Model,
|
|
7
7
|
type SimpleStreamOptions,
|
|
8
8
|
type AssistantMessage,
|
|
9
|
-
type ToolResultMessage,
|
|
10
9
|
} from "@earendil-works/pi-ai";
|
|
11
|
-
import { AsyncLocalStorage } from "node:async_hooks";
|
|
12
10
|
import { Agent, createAgentPlatform } from "@cursor/sdk";
|
|
13
|
-
import type {
|
|
11
|
+
import type { SDKAgent, SettingSource } from "@cursor/sdk";
|
|
14
12
|
import { installCursorMcpToolTimeoutOverride } from "./cursor-mcp-timeout-override.js";
|
|
13
|
+
import { installCursorSdkOutputFilter, suppressCursorSdkOutput } from "./cursor-sdk-output-filter.js";
|
|
15
14
|
import { buildCursorSendPrompt } from "./context.js";
|
|
16
15
|
import {
|
|
17
16
|
acquireSessionCursorAgent,
|
|
@@ -23,29 +22,32 @@ import {
|
|
|
23
22
|
type CursorPiBridgeToolRequest,
|
|
24
23
|
type CursorPiToolBridgeRun,
|
|
25
24
|
} from "./cursor-pi-tool-bridge.js";
|
|
26
|
-
import {
|
|
27
|
-
consumeCursorLiveToolResults,
|
|
28
|
-
createCursorLiveRunAccountingState,
|
|
29
|
-
takeCursorLiveTurnInputTokens,
|
|
30
|
-
type CursorLiveRunAccountingState,
|
|
31
|
-
} from "./cursor-live-run-accounting.js";
|
|
32
25
|
import {
|
|
33
26
|
applyCursorApproximateUsage,
|
|
34
27
|
estimateCursorPromptInputTokens,
|
|
35
28
|
getCursorPromptOptions,
|
|
36
29
|
} from "./cursor-usage-accounting.js";
|
|
37
30
|
import { getCursorSessionCwd } from "./cursor-session-cwd.js";
|
|
31
|
+
import { CursorLiveRunAbortError, type CursorLiveRun } from "./cursor-live-run-coordinator.js";
|
|
32
|
+
import {
|
|
33
|
+
abandonSessionCursorAgent,
|
|
34
|
+
createCursorNativeReplayId,
|
|
35
|
+
cursorLiveRuns,
|
|
36
|
+
drainCursorLiveRunTurn,
|
|
37
|
+
drainExistingCursorLiveRunBeforeSend,
|
|
38
|
+
DEFAULT_CURSOR_NATIVE_REPLAY_IDLE_DISPOSE_MS,
|
|
39
|
+
getPendingCursorLiveRun,
|
|
40
|
+
hasTrailingUserMessagesAfterToolResults,
|
|
41
|
+
resetCursorNativeReplayIdleDisposeMs,
|
|
42
|
+
selectCursorFinalText,
|
|
43
|
+
setCursorNativeReplayIdleDisposeMs,
|
|
44
|
+
settleCursorLiveToolBatch,
|
|
45
|
+
} from "./cursor-provider-live-run-drain.js";
|
|
38
46
|
import { getEffectiveFastForModelId } from "./cursor-state.js";
|
|
39
47
|
import { buildCursorModelSelection } from "./model-discovery.js";
|
|
40
48
|
import { getCheckpointContextWindow, saveCachedContextWindow } from "./context-window-cache.js";
|
|
41
|
-
import {
|
|
42
|
-
import {
|
|
43
|
-
canRenderCursorToolNatively,
|
|
44
|
-
isCursorNativeToolDisplayRuntimeEnabled,
|
|
45
|
-
deleteCursorNativeToolDisplay,
|
|
46
|
-
recordCursorNativeToolDisplay,
|
|
47
|
-
type CursorNativeToolDisplayItem,
|
|
48
|
-
} from "./cursor-native-tool-display.js";
|
|
49
|
+
import { CursorSdkTurnCoordinator } from "./cursor-provider-turn-coordinator.js";
|
|
50
|
+
import { isCursorNativeToolDisplayRuntimeEnabled } from "./cursor-native-tool-display.js";
|
|
49
51
|
|
|
50
52
|
function makeInitialMessage(model: Model<Api>): AssistantMessage {
|
|
51
53
|
return {
|
|
@@ -67,13 +69,6 @@ function makeInitialMessage(model: Model<Api>): AssistantMessage {
|
|
|
67
69
|
};
|
|
68
70
|
}
|
|
69
71
|
|
|
70
|
-
class CursorAbortError extends Error {
|
|
71
|
-
constructor() {
|
|
72
|
-
super("aborted");
|
|
73
|
-
this.name = "CursorAbortError";
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
|
|
77
72
|
const CURSOR_API_KEY_ENV_VAR = "CURSOR_API_KEY";
|
|
78
73
|
const MISSING_API_KEY_MESSAGE =
|
|
79
74
|
"Cursor SDK runs require a Cursor API key. Run /login -> Use an API key -> Cursor, set CURSOR_API_KEY before starting pi, or restart pi with --api-key.";
|
|
@@ -81,74 +76,10 @@ const GENERIC_CURSOR_SDK_ERROR_MESSAGE =
|
|
|
81
76
|
"Cursor SDK request failed. The API key may be missing, invalid, or unauthorized. Run /login -> Use an API key -> Cursor, verify CURSOR_API_KEY, or pass --api-key, then retry.";
|
|
82
77
|
const AUTH_CURSOR_SDK_ERROR_MESSAGE =
|
|
83
78
|
"Cursor SDK request failed because the API key may be invalid or unauthorized. Run /login -> Use an API key -> Cursor, verify CURSOR_API_KEY, or pass --api-key, then retry.";
|
|
84
|
-
const CURSOR_ACTIVITY_TRACE_MAX_CHARS = 50000;
|
|
85
|
-
const DEFAULT_CURSOR_NATIVE_REPLAY_IDLE_DISPOSE_MS = 5 * 60 * 1000;
|
|
86
|
-
const CURSOR_NATIVE_REPLAY_TOOL_ID_PATTERN = /^(cursor-replay-\d+-\d+)-tool-\d+$/;
|
|
87
79
|
const CURSOR_SETTING_SOURCES_ENV = "PI_CURSOR_SETTING_SOURCES";
|
|
88
|
-
const cursorSdkOutputSuppression = new AsyncLocalStorage<boolean>();
|
|
89
|
-
|
|
90
|
-
type CursorLiveQueuedEvent =
|
|
91
|
-
| { type: "thinking-delta"; text: string }
|
|
92
|
-
| { type: "thinking-completed" }
|
|
93
|
-
| { type: "text-delta"; text: string }
|
|
94
|
-
| { type: "tool"; tool: CursorNativeToolDisplayItem }
|
|
95
|
-
| { type: "bridge-tool"; request: CursorPiBridgeToolRequest };
|
|
96
|
-
|
|
97
|
-
interface CursorLiveSdkRun {
|
|
98
|
-
cancel(): Promise<void>;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
interface CursorLiveRun {
|
|
102
|
-
id: string;
|
|
103
|
-
agent: SDKAgent;
|
|
104
|
-
bridgeRun?: CursorPiToolBridgeRun;
|
|
105
|
-
sessionBridgeRun?: CursorPiToolBridgeRun;
|
|
106
|
-
sessionAgentScopeKey?: string;
|
|
107
|
-
sdkRun?: CursorLiveSdkRun;
|
|
108
|
-
accounting: CursorLiveRunAccountingState;
|
|
109
|
-
pendingEvents: CursorLiveQueuedEvent[];
|
|
110
|
-
textDeltas: string[];
|
|
111
|
-
emittedText: string;
|
|
112
|
-
recordedToolDisplayIds: string[];
|
|
113
|
-
finalText?: string;
|
|
114
|
-
done: boolean;
|
|
115
|
-
cancelled: boolean;
|
|
116
|
-
disposed: boolean;
|
|
117
|
-
errorMessage?: string;
|
|
118
|
-
idleDisposeTimer?: ReturnType<typeof setTimeout>;
|
|
119
|
-
waiters: Set<() => void>;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
interface CursorLiveTurnState {
|
|
123
|
-
stream: AssistantMessageEventStream;
|
|
124
|
-
partial: AssistantMessage;
|
|
125
|
-
thinkingContentIndex: number;
|
|
126
|
-
textContentIndex: number;
|
|
127
|
-
emittedText: string;
|
|
128
|
-
}
|
|
129
80
|
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
const pendingCursorLiveRuns = new Map<string, CursorLiveRun>();
|
|
133
|
-
|
|
134
|
-
function escapeRegExp(value: string): string {
|
|
135
|
-
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
function scrubSensitiveText(text: string, apiKey?: string): string {
|
|
139
|
-
let scrubbed = text;
|
|
140
|
-
const trimmedKey = apiKey?.trim();
|
|
141
|
-
if (trimmedKey) {
|
|
142
|
-
scrubbed = scrubbed.replace(new RegExp(escapeRegExp(trimmedKey), "g"), "[redacted]");
|
|
143
|
-
}
|
|
144
|
-
return scrubbed
|
|
145
|
-
.replace(/Bearer\s+[A-Za-z0-9._~+/=-]+/gi, "Bearer [redacted]")
|
|
146
|
-
.replace(/((?:^|[\s,{])cookie["']?\s*[:=]\s*["']?)[^\n]+/gi, "$1[redacted]")
|
|
147
|
-
.replace(
|
|
148
|
-
/((?:authorization|api[_-]?key|apiKey|token|session(?:[_-]?id)?)["']?\s*[:=]\s*["']?)[^"'\s,;}]+/gi,
|
|
149
|
-
"$1[redacted]",
|
|
150
|
-
);
|
|
151
|
-
}
|
|
81
|
+
import { scrubSensitiveText } from "./cursor-sensitive-text.js";
|
|
82
|
+
import { hasUsableText } from "./cursor-record-utils.js";
|
|
152
83
|
|
|
153
84
|
function isGenericErrorMessage(message: string): boolean {
|
|
154
85
|
const normalized = message.trim().toLowerCase();
|
|
@@ -187,92 +118,6 @@ function sanitizeError(error: unknown, apiKey?: string): string {
|
|
|
187
118
|
return scrubbed || GENERIC_CURSOR_SDK_ERROR_MESSAGE;
|
|
188
119
|
}
|
|
189
120
|
|
|
190
|
-
function isCursorSdkOutputSuppressed(): boolean {
|
|
191
|
-
return cursorSdkOutputSuppression.getStore() === true;
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
function suppressCursorSdkOutput<T>(operation: () => Promise<T>): Promise<T> {
|
|
195
|
-
return cursorSdkOutputSuppression.run(true, operation);
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
const CURSOR_SDK_STARTUP_NOISE_PATTERNS = [
|
|
199
|
-
"managed_skills.",
|
|
200
|
-
"CursorPluginsAgentSkillsService load completed",
|
|
201
|
-
"LocalCursorRulesService load completed",
|
|
202
|
-
"AgentSkillsCursorRulesService load completed",
|
|
203
|
-
];
|
|
204
|
-
|
|
205
|
-
function isCursorSdkStartupNoise(text: string): boolean {
|
|
206
|
-
return CURSOR_SDK_STARTUP_NOISE_PATTERNS.some((pattern) => text.includes(pattern));
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
function createFilteredProcessWrite<TWrite extends typeof process.stdout.write>(write: TWrite, stream: NodeJS.WriteStream): TWrite {
|
|
210
|
-
return ((
|
|
211
|
-
chunk: string | Uint8Array,
|
|
212
|
-
encodingOrCallback?: BufferEncoding | ((error?: Error | null) => void),
|
|
213
|
-
callback?: (error?: Error | null) => void,
|
|
214
|
-
): boolean => {
|
|
215
|
-
const text = typeof chunk === "string" ? chunk : Buffer.from(chunk).toString("utf8");
|
|
216
|
-
if (isCursorSdkOutputSuppressed() || isCursorSdkStartupNoise(text)) {
|
|
217
|
-
const done = typeof encodingOrCallback === "function" ? encodingOrCallback : callback;
|
|
218
|
-
done?.();
|
|
219
|
-
return true;
|
|
220
|
-
}
|
|
221
|
-
return write.call(stream, chunk as string, encodingOrCallback as BufferEncoding, callback);
|
|
222
|
-
}) as TWrite;
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
function createFilteredConsoleMethod<TMethod extends typeof console.log>(method: TMethod): TMethod {
|
|
226
|
-
return ((...args: Parameters<TMethod>): void => {
|
|
227
|
-
const text = args.map((arg) => (typeof arg === "string" ? arg : String(arg))).join(" ");
|
|
228
|
-
if (isCursorSdkOutputSuppressed() || isCursorSdkStartupNoise(text)) return;
|
|
229
|
-
method(...args);
|
|
230
|
-
}) as TMethod;
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
function installCursorSdkOutputFilter(): () => void {
|
|
234
|
-
const stdoutWrite = process.stdout.write;
|
|
235
|
-
const stderrWrite = process.stderr.write;
|
|
236
|
-
const consoleLog = console.log;
|
|
237
|
-
const consoleInfo = console.info;
|
|
238
|
-
const consoleWarn = console.warn;
|
|
239
|
-
const consoleError = console.error;
|
|
240
|
-
const consoleDebug = console.debug;
|
|
241
|
-
process.stdout.write = createFilteredProcessWrite(stdoutWrite, process.stdout);
|
|
242
|
-
process.stderr.write = createFilteredProcessWrite(stderrWrite, process.stderr) as typeof process.stderr.write;
|
|
243
|
-
console.log = createFilteredConsoleMethod(consoleLog);
|
|
244
|
-
console.info = createFilteredConsoleMethod(consoleInfo);
|
|
245
|
-
console.warn = createFilteredConsoleMethod(consoleWarn);
|
|
246
|
-
console.error = createFilteredConsoleMethod(consoleError);
|
|
247
|
-
console.debug = createFilteredConsoleMethod(consoleDebug);
|
|
248
|
-
let restored = false;
|
|
249
|
-
return () => {
|
|
250
|
-
if (restored) return;
|
|
251
|
-
restored = true;
|
|
252
|
-
process.stdout.write = stdoutWrite;
|
|
253
|
-
process.stderr.write = stderrWrite;
|
|
254
|
-
console.log = consoleLog;
|
|
255
|
-
console.info = consoleInfo;
|
|
256
|
-
console.warn = consoleWarn;
|
|
257
|
-
console.error = consoleError;
|
|
258
|
-
console.debug = consoleDebug;
|
|
259
|
-
};
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
function getObjectField(value: unknown, field: string): unknown {
|
|
263
|
-
if (!value || typeof value !== "object") return undefined;
|
|
264
|
-
return (value as Record<string, unknown>)[field];
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
function getCursorToolName(toolCall: unknown): string {
|
|
268
|
-
if (!toolCall || typeof toolCall !== "object") return "unknown";
|
|
269
|
-
const data = toolCall as Record<string, unknown>;
|
|
270
|
-
if (typeof data.name === "string") return data.name;
|
|
271
|
-
if (typeof data.type === "string") return data.type;
|
|
272
|
-
if (typeof data.toolName === "string") return data.toolName;
|
|
273
|
-
return "unknown";
|
|
274
|
-
}
|
|
275
|
-
|
|
276
121
|
async function cacheSdkContextWindow(agentId: string, modelId: string): Promise<void> {
|
|
277
122
|
try {
|
|
278
123
|
const platform = await createAgentPlatform();
|
|
@@ -284,564 +129,6 @@ async function cacheSdkContextWindow(agentId: string, modelId: string): Promise<
|
|
|
284
129
|
}
|
|
285
130
|
}
|
|
286
131
|
|
|
287
|
-
function sanitizeSingleLine(value: string): string {
|
|
288
|
-
return value.replace(/\s+/g, " ").trim();
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
function truncateSingleLine(value: string, maxLength = 240): string {
|
|
292
|
-
const sanitized = sanitizeSingleLine(value);
|
|
293
|
-
return sanitized.length > maxLength ? `${sanitized.slice(0, maxLength - 1)}…` : sanitized;
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
function formatCursorToolName(toolCall: unknown): string {
|
|
297
|
-
return truncateSingleLine(getCursorToolName(toolCall), 80) || "unknown";
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
function hasUsableText(value: string | undefined): value is string {
|
|
301
|
-
return typeof value === "string" && value.trim().length > 0;
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
interface CursorShellOutputDelta {
|
|
305
|
-
stream: "stdout" | "stderr";
|
|
306
|
-
data: string;
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
interface CursorShellOutputDeltas {
|
|
310
|
-
stdout: string[];
|
|
311
|
-
stderr: string[];
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
function isCursorShellToolCall(toolCall: unknown): boolean {
|
|
315
|
-
const normalizedName = getCursorToolName(toolCall).replace(/\s+/g, " ").trim().toLowerCase();
|
|
316
|
-
return normalizedName === "shell" || normalizedName === "run_terminal_cmd" || normalizedName === "terminal" || normalizedName === "bash";
|
|
317
|
-
}
|
|
318
|
-
|
|
319
|
-
function getCursorShellOutputDelta(update: InteractionUpdate): CursorShellOutputDelta | undefined {
|
|
320
|
-
if (update.type !== "shell-output-delta") return undefined;
|
|
321
|
-
const event = getObjectField(update, "event");
|
|
322
|
-
const eventCase = getObjectField(event, "case");
|
|
323
|
-
if (eventCase !== "stdout" && eventCase !== "stderr") return undefined;
|
|
324
|
-
const value = getObjectField(event, "value");
|
|
325
|
-
const data = getObjectField(value, "data");
|
|
326
|
-
if (typeof data !== "string" || data.length === 0) return undefined;
|
|
327
|
-
return { stream: eventCase, data };
|
|
328
|
-
}
|
|
329
|
-
|
|
330
|
-
function mergeShellOutputDeltasIntoCursorToolCall(toolCall: unknown, deltas: CursorShellOutputDeltas | undefined): unknown {
|
|
331
|
-
if (!deltas) return toolCall;
|
|
332
|
-
const stdout = deltas.stdout.join("");
|
|
333
|
-
const stderr = deltas.stderr.join("");
|
|
334
|
-
if (!hasUsableText(stdout) && !hasUsableText(stderr)) return toolCall;
|
|
335
|
-
|
|
336
|
-
const toolRecord = toolCall && typeof toolCall === "object" && !Array.isArray(toolCall) ? (toolCall as Record<string, unknown>) : undefined;
|
|
337
|
-
const result = getObjectField(toolRecord, "result");
|
|
338
|
-
const resultRecord = result && typeof result === "object" && !Array.isArray(result) ? (result as Record<string, unknown>) : undefined;
|
|
339
|
-
if (!toolRecord || !resultRecord || resultRecord.status !== "success") return toolCall;
|
|
340
|
-
|
|
341
|
-
const value = getObjectField(resultRecord, "value");
|
|
342
|
-
const valueRecord = value && typeof value === "object" && !Array.isArray(value) ? (value as Record<string, unknown>) : undefined;
|
|
343
|
-
const completedStdout = getObjectField(valueRecord, "stdout");
|
|
344
|
-
const completedStderr = getObjectField(valueRecord, "stderr");
|
|
345
|
-
if (hasUsableText(typeof completedStdout === "string" ? completedStdout : undefined)) return toolCall;
|
|
346
|
-
if (hasUsableText(typeof completedStderr === "string" ? completedStderr : undefined)) return toolCall;
|
|
347
|
-
|
|
348
|
-
return {
|
|
349
|
-
...toolRecord,
|
|
350
|
-
result: {
|
|
351
|
-
...resultRecord,
|
|
352
|
-
value: {
|
|
353
|
-
...(valueRecord ?? {}),
|
|
354
|
-
stdout,
|
|
355
|
-
stderr,
|
|
356
|
-
},
|
|
357
|
-
},
|
|
358
|
-
};
|
|
359
|
-
}
|
|
360
|
-
|
|
361
|
-
function scrubDisplayValue(value: unknown, apiKey?: string): unknown {
|
|
362
|
-
if (typeof value === "string") return scrubSensitiveText(value, apiKey);
|
|
363
|
-
if (Array.isArray(value)) return value.map((entry) => scrubDisplayValue(entry, apiKey));
|
|
364
|
-
if (value && typeof value === "object") {
|
|
365
|
-
return Object.fromEntries(Object.entries(value).map(([key, entry]) => [key, scrubDisplayValue(entry, apiKey)]));
|
|
366
|
-
}
|
|
367
|
-
return value;
|
|
368
|
-
}
|
|
369
|
-
|
|
370
|
-
function createCursorNativeReplayId(): string {
|
|
371
|
-
cursorNativeReplayCounter += 1;
|
|
372
|
-
return `cursor-replay-${Date.now()}-${cursorNativeReplayCounter}`;
|
|
373
|
-
}
|
|
374
|
-
|
|
375
|
-
function getCursorNativeReplayIdFromToolCallId(toolCallId: string): string | undefined {
|
|
376
|
-
return CURSOR_NATIVE_REPLAY_TOOL_ID_PATTERN.exec(toolCallId)?.[1];
|
|
377
|
-
}
|
|
378
|
-
|
|
379
|
-
function getPendingCursorLiveRun(context: Context): CursorLiveRun | undefined {
|
|
380
|
-
for (let index = context.messages.length - 1; index >= 0; index -= 1) {
|
|
381
|
-
const message = context.messages[index];
|
|
382
|
-
if (message.role !== "toolResult") break;
|
|
383
|
-
const replayId = getCursorNativeReplayIdFromToolCallId(message.toolCallId);
|
|
384
|
-
if (replayId) {
|
|
385
|
-
const replayRun = pendingCursorLiveRuns.get(replayId);
|
|
386
|
-
if (replayRun) return replayRun;
|
|
387
|
-
}
|
|
388
|
-
for (const run of pendingCursorLiveRuns.values()) {
|
|
389
|
-
if (run.bridgeRun?.hasPendingPiToolCallId(message.toolCallId)) return run;
|
|
390
|
-
}
|
|
391
|
-
}
|
|
392
|
-
return undefined;
|
|
393
|
-
}
|
|
394
|
-
|
|
395
|
-
function isCursorLiveRunToolResult(run: CursorLiveRun, message: ToolResultMessage): boolean {
|
|
396
|
-
const replayId = getCursorNativeReplayIdFromToolCallId(message.toolCallId);
|
|
397
|
-
if (replayId) return replayId === run.id;
|
|
398
|
-
return run.bridgeRun?.hasPendingPiToolCallId(message.toolCallId) ?? false;
|
|
399
|
-
}
|
|
400
|
-
|
|
401
|
-
function consumeCursorLiveRunToolResults(run: CursorLiveRun, context: Context) {
|
|
402
|
-
const consumed = consumeCursorLiveToolResults(run.accounting, context, (toolResult) => isCursorLiveRunToolResult(run, toolResult));
|
|
403
|
-
run.accounting = consumed.state;
|
|
404
|
-
return consumed;
|
|
405
|
-
}
|
|
406
|
-
|
|
407
|
-
function splitTextIntoReplayDeltas(text: string): string[] {
|
|
408
|
-
const deltas: string[] = [];
|
|
409
|
-
let remaining = text;
|
|
410
|
-
while (remaining.length > 0) {
|
|
411
|
-
if (remaining.length <= 96) {
|
|
412
|
-
deltas.push(remaining);
|
|
413
|
-
break;
|
|
414
|
-
}
|
|
415
|
-
const boundary = Math.max(48, remaining.lastIndexOf(" ", 96));
|
|
416
|
-
deltas.push(remaining.slice(0, boundary));
|
|
417
|
-
remaining = remaining.slice(boundary);
|
|
418
|
-
}
|
|
419
|
-
return deltas;
|
|
420
|
-
}
|
|
421
|
-
|
|
422
|
-
async function emitTextDeltas(
|
|
423
|
-
stream: AssistantMessageEventStream,
|
|
424
|
-
partial: AssistantMessage,
|
|
425
|
-
deltas: string[],
|
|
426
|
-
): Promise<string> {
|
|
427
|
-
if (deltas.length === 0) return "";
|
|
428
|
-
const contentIndex = partial.content.length;
|
|
429
|
-
partial.content.push({ type: "text", text: "" });
|
|
430
|
-
stream.push({ type: "text_start", contentIndex, partial });
|
|
431
|
-
const block = partial.content[contentIndex];
|
|
432
|
-
if (block.type !== "text") return "";
|
|
433
|
-
for (const delta of deltas) {
|
|
434
|
-
block.text += delta;
|
|
435
|
-
stream.push({ type: "text_delta", contentIndex, delta, partial });
|
|
436
|
-
await Promise.resolve();
|
|
437
|
-
}
|
|
438
|
-
stream.push({ type: "text_end", contentIndex, content: block.text, partial });
|
|
439
|
-
return block.text;
|
|
440
|
-
}
|
|
441
|
-
|
|
442
|
-
function notifyCursorNativeRun(run: CursorLiveRun): void {
|
|
443
|
-
for (const waiter of run.waiters) waiter();
|
|
444
|
-
run.waiters.clear();
|
|
445
|
-
}
|
|
446
|
-
|
|
447
|
-
function queueCursorNativeEvent(run: CursorLiveRun, event: CursorLiveQueuedEvent): void {
|
|
448
|
-
run.pendingEvents.push(event);
|
|
449
|
-
notifyCursorNativeRun(run);
|
|
450
|
-
}
|
|
451
|
-
|
|
452
|
-
function clearCursorNativeRunIdleDispose(run: CursorLiveRun): void {
|
|
453
|
-
if (!run.idleDisposeTimer) return;
|
|
454
|
-
clearTimeout(run.idleDisposeTimer);
|
|
455
|
-
run.idleDisposeTimer = undefined;
|
|
456
|
-
}
|
|
457
|
-
|
|
458
|
-
function scheduleCursorNativeRunIdleDispose(run: CursorLiveRun): void {
|
|
459
|
-
if (run.disposed) return;
|
|
460
|
-
clearCursorNativeRunIdleDispose(run);
|
|
461
|
-
run.idleDisposeTimer = setTimeout(() => {
|
|
462
|
-
void releaseCursorLiveRun(run);
|
|
463
|
-
}, cursorNativeReplayIdleDisposeMs);
|
|
464
|
-
run.idleDisposeTimer.unref?.();
|
|
465
|
-
}
|
|
466
|
-
|
|
467
|
-
function isCursorNativeRunReady(run: CursorLiveRun): boolean {
|
|
468
|
-
return run.pendingEvents.length > 0 || run.done || run.cancelled || run.errorMessage !== undefined;
|
|
469
|
-
}
|
|
470
|
-
|
|
471
|
-
async function waitForCursorNativeRunProgress(run: CursorLiveRun, signal?: AbortSignal): Promise<void> {
|
|
472
|
-
if (signal?.aborted) throw new CursorAbortError();
|
|
473
|
-
if (isCursorNativeRunReady(run)) return;
|
|
474
|
-
await new Promise<void>((resolve, reject) => {
|
|
475
|
-
let waiter: (() => void) | undefined;
|
|
476
|
-
const cleanup = (): void => {
|
|
477
|
-
if (waiter) run.waiters.delete(waiter);
|
|
478
|
-
signal?.removeEventListener("abort", onAbort);
|
|
479
|
-
};
|
|
480
|
-
const onAbort = (): void => {
|
|
481
|
-
cleanup();
|
|
482
|
-
reject(new CursorAbortError());
|
|
483
|
-
};
|
|
484
|
-
waiter = (): void => {
|
|
485
|
-
cleanup();
|
|
486
|
-
resolve();
|
|
487
|
-
};
|
|
488
|
-
run.waiters.add(waiter);
|
|
489
|
-
if (signal?.aborted) {
|
|
490
|
-
onAbort();
|
|
491
|
-
return;
|
|
492
|
-
}
|
|
493
|
-
signal?.addEventListener("abort", onAbort, { once: true });
|
|
494
|
-
});
|
|
495
|
-
}
|
|
496
|
-
|
|
497
|
-
async function settleCursorLiveToolBatch(run: CursorLiveRun): Promise<void> {
|
|
498
|
-
const eventType = run.pendingEvents[0]?.type;
|
|
499
|
-
if (eventType !== "tool" && eventType !== "bridge-tool") return;
|
|
500
|
-
await new Promise((resolve) => setTimeout(resolve, 75));
|
|
501
|
-
}
|
|
502
|
-
|
|
503
|
-
function closeCursorNativeThinkingBlock(turn: CursorLiveTurnState): void {
|
|
504
|
-
if (turn.thinkingContentIndex < 0) return;
|
|
505
|
-
const block = turn.partial.content[turn.thinkingContentIndex];
|
|
506
|
-
if (block.type === "thinking") {
|
|
507
|
-
turn.stream.push({
|
|
508
|
-
type: "thinking_end",
|
|
509
|
-
contentIndex: turn.thinkingContentIndex,
|
|
510
|
-
content: block.thinking,
|
|
511
|
-
partial: turn.partial,
|
|
512
|
-
});
|
|
513
|
-
}
|
|
514
|
-
turn.thinkingContentIndex = -1;
|
|
515
|
-
}
|
|
516
|
-
|
|
517
|
-
function closeCursorNativeTextBlock(turn: CursorLiveTurnState): string {
|
|
518
|
-
if (turn.textContentIndex < 0) return "";
|
|
519
|
-
const contentIndex = turn.textContentIndex;
|
|
520
|
-
const block = turn.partial.content[contentIndex];
|
|
521
|
-
turn.textContentIndex = -1;
|
|
522
|
-
if (block.type !== "text") return "";
|
|
523
|
-
turn.stream.push({
|
|
524
|
-
type: "text_end",
|
|
525
|
-
contentIndex,
|
|
526
|
-
content: block.text,
|
|
527
|
-
partial: turn.partial,
|
|
528
|
-
});
|
|
529
|
-
return block.text;
|
|
530
|
-
}
|
|
531
|
-
|
|
532
|
-
function closeCursorNativeTurnBlocks(turn: CursorLiveTurnState): string {
|
|
533
|
-
closeCursorNativeThinkingBlock(turn);
|
|
534
|
-
return closeCursorNativeTextBlock(turn);
|
|
535
|
-
}
|
|
536
|
-
|
|
537
|
-
function emitCursorNativeThinkingDelta(turn: CursorLiveTurnState, delta: string): void {
|
|
538
|
-
closeCursorNativeTextBlock(turn);
|
|
539
|
-
if (turn.thinkingContentIndex < 0) {
|
|
540
|
-
turn.thinkingContentIndex = turn.partial.content.length;
|
|
541
|
-
turn.partial.content.push({ type: "thinking", thinking: "" });
|
|
542
|
-
turn.stream.push({ type: "thinking_start", contentIndex: turn.thinkingContentIndex, partial: turn.partial });
|
|
543
|
-
}
|
|
544
|
-
const block = turn.partial.content[turn.thinkingContentIndex];
|
|
545
|
-
if (block.type !== "thinking") return;
|
|
546
|
-
block.thinking += delta;
|
|
547
|
-
turn.stream.push({ type: "thinking_delta", contentIndex: turn.thinkingContentIndex, delta, partial: turn.partial });
|
|
548
|
-
}
|
|
549
|
-
|
|
550
|
-
function emitCursorNativeTextDelta(turn: CursorLiveTurnState, delta: string): void {
|
|
551
|
-
closeCursorNativeThinkingBlock(turn);
|
|
552
|
-
if (turn.textContentIndex < 0) {
|
|
553
|
-
turn.textContentIndex = turn.partial.content.length;
|
|
554
|
-
turn.partial.content.push({ type: "text", text: "" });
|
|
555
|
-
turn.stream.push({ type: "text_start", contentIndex: turn.textContentIndex, partial: turn.partial });
|
|
556
|
-
}
|
|
557
|
-
const block = turn.partial.content[turn.textContentIndex];
|
|
558
|
-
if (block.type !== "text") return;
|
|
559
|
-
block.text += delta;
|
|
560
|
-
turn.stream.push({ type: "text_delta", contentIndex: turn.textContentIndex, delta, partial: turn.partial });
|
|
561
|
-
}
|
|
562
|
-
|
|
563
|
-
function emitCursorLiveQueuedEvent(
|
|
564
|
-
turn: CursorLiveTurnState,
|
|
565
|
-
event: Exclude<CursorLiveQueuedEvent, { type: "tool" } | { type: "bridge-tool" }>,
|
|
566
|
-
run?: CursorLiveRun,
|
|
567
|
-
): void {
|
|
568
|
-
if (event.type === "thinking-delta") {
|
|
569
|
-
emitCursorNativeThinkingDelta(turn, event.text);
|
|
570
|
-
} else if (event.type === "thinking-completed") {
|
|
571
|
-
closeCursorNativeThinkingBlock(turn);
|
|
572
|
-
} else if (event.type === "text-delta") {
|
|
573
|
-
turn.emittedText += event.text;
|
|
574
|
-
if (run) run.emittedText += event.text;
|
|
575
|
-
emitCursorNativeTextDelta(turn, event.text);
|
|
576
|
-
}
|
|
577
|
-
}
|
|
578
|
-
|
|
579
|
-
function collectCursorNativeToolBatch(run: CursorLiveRun): CursorNativeToolDisplayItem[] {
|
|
580
|
-
const tools: CursorNativeToolDisplayItem[] = [];
|
|
581
|
-
while (run.pendingEvents[0]?.type === "tool") {
|
|
582
|
-
const event = run.pendingEvents.shift();
|
|
583
|
-
if (event?.type === "tool") tools.push(event.tool);
|
|
584
|
-
}
|
|
585
|
-
return tools;
|
|
586
|
-
}
|
|
587
|
-
|
|
588
|
-
function collectCursorBridgeToolBatch(run: CursorLiveRun): CursorPiBridgeToolRequest[] {
|
|
589
|
-
const requests: CursorPiBridgeToolRequest[] = [];
|
|
590
|
-
while (run.pendingEvents[0]?.type === "bridge-tool") {
|
|
591
|
-
const event = run.pendingEvents.shift();
|
|
592
|
-
if (event?.type === "bridge-tool") requests.push(event.request);
|
|
593
|
-
}
|
|
594
|
-
return requests;
|
|
595
|
-
}
|
|
596
|
-
|
|
597
|
-
function isCursorTextBoundary(text: string, index: number): boolean {
|
|
598
|
-
if (index <= 0 || index >= text.length) return true;
|
|
599
|
-
const before = text[index - 1];
|
|
600
|
-
const after = text[index];
|
|
601
|
-
return !/[\p{L}\p{N}_]/u.test(before) || !/[\p{L}\p{N}_]/u.test(after);
|
|
602
|
-
}
|
|
603
|
-
|
|
604
|
-
function trimAlreadyEmittedCursorText(text: string, emittedText: string, options?: { allowPartialPrefix?: boolean }): string {
|
|
605
|
-
if (!text || !emittedText) return text;
|
|
606
|
-
if (text === emittedText) return "";
|
|
607
|
-
if (text.startsWith(emittedText) && (options?.allowPartialPrefix || isCursorTextBoundary(text, emittedText.length))) {
|
|
608
|
-
return text.slice(emittedText.length);
|
|
609
|
-
}
|
|
610
|
-
if (emittedText.endsWith(text) && isCursorTextBoundary(emittedText, emittedText.length - text.length)) return "";
|
|
611
|
-
const trimmedText = text.trim();
|
|
612
|
-
const trimmedEmittedText = emittedText.trim();
|
|
613
|
-
if (trimmedText === trimmedEmittedText) return "";
|
|
614
|
-
if (trimmedText && trimmedEmittedText.endsWith(trimmedText)) {
|
|
615
|
-
const suffixStart = trimmedEmittedText.length - trimmedText.length;
|
|
616
|
-
if (isCursorTextBoundary(trimmedEmittedText, suffixStart)) return "";
|
|
617
|
-
}
|
|
618
|
-
return text;
|
|
619
|
-
}
|
|
620
|
-
|
|
621
|
-
function trimCurrentTurnAlreadyEmittedCursorText(text: string, currentTurnEmittedText: string, emittedText = currentTurnEmittedText): string {
|
|
622
|
-
if (!currentTurnEmittedText) return trimAlreadyEmittedCursorText(text, emittedText);
|
|
623
|
-
const currentTurnTrimmedText = trimAlreadyEmittedCursorText(text, currentTurnEmittedText, { allowPartialPrefix: true });
|
|
624
|
-
if (currentTurnTrimmedText !== text) return currentTurnTrimmedText;
|
|
625
|
-
if (emittedText.endsWith(currentTurnEmittedText)) {
|
|
626
|
-
const emittedTextTrimmedText = trimAlreadyEmittedCursorText(text, emittedText, { allowPartialPrefix: true });
|
|
627
|
-
if (emittedTextTrimmedText !== text) return emittedTextTrimmedText;
|
|
628
|
-
}
|
|
629
|
-
return trimAlreadyEmittedCursorText(text, emittedText);
|
|
630
|
-
}
|
|
631
|
-
|
|
632
|
-
function selectCursorFinalText(
|
|
633
|
-
resultText: unknown,
|
|
634
|
-
textDeltas: readonly string[],
|
|
635
|
-
emittedText: string,
|
|
636
|
-
fallbackText?: string,
|
|
637
|
-
options?: { allowPartialPrefix?: boolean },
|
|
638
|
-
): string {
|
|
639
|
-
const candidates = [typeof resultText === "string" ? resultText : undefined, fallbackText, textDeltas.join("")];
|
|
640
|
-
for (const candidate of candidates) {
|
|
641
|
-
if (!hasUsableText(candidate)) continue;
|
|
642
|
-
const trimmedCandidate = trimAlreadyEmittedCursorText(candidate, emittedText, options);
|
|
643
|
-
if (hasUsableText(trimmedCandidate)) return trimmedCandidate;
|
|
644
|
-
}
|
|
645
|
-
return "";
|
|
646
|
-
}
|
|
647
|
-
|
|
648
|
-
function takeCursorLiveSessionInputTokens(run: CursorLiveRun, toolResultInputTokens: number): number {
|
|
649
|
-
// Native replay can split one Cursor run into multiple pi turns; count prompt input once.
|
|
650
|
-
const taken = takeCursorLiveTurnInputTokens(run.accounting, toolResultInputTokens);
|
|
651
|
-
run.accounting = taken.state;
|
|
652
|
-
return taken.sessionInputTokens;
|
|
653
|
-
}
|
|
654
|
-
|
|
655
|
-
function emitCursorNativeToolUseTurn(
|
|
656
|
-
stream: AssistantMessageEventStream,
|
|
657
|
-
partial: AssistantMessage,
|
|
658
|
-
model: Model<Api>,
|
|
659
|
-
context: Context,
|
|
660
|
-
run: CursorLiveRun,
|
|
661
|
-
toolResultInputTokens: number,
|
|
662
|
-
tools: CursorNativeToolDisplayItem[],
|
|
663
|
-
): void {
|
|
664
|
-
const shouldTerminate = run.done && !run.finalText?.trim() && run.pendingEvents.length === 0;
|
|
665
|
-
for (const tool of tools) {
|
|
666
|
-
const contentIndex = partial.content.length;
|
|
667
|
-
partial.content.push({
|
|
668
|
-
type: "toolCall",
|
|
669
|
-
id: tool.id,
|
|
670
|
-
name: tool.toolName,
|
|
671
|
-
arguments: tool.args,
|
|
672
|
-
});
|
|
673
|
-
stream.push({ type: "toolcall_start", contentIndex, partial });
|
|
674
|
-
stream.push({ type: "toolcall_delta", contentIndex, delta: JSON.stringify(tool.args), partial });
|
|
675
|
-
const block = partial.content[contentIndex];
|
|
676
|
-
if (block.type === "toolCall") stream.push({ type: "toolcall_end", contentIndex, toolCall: block, partial });
|
|
677
|
-
if (recordCursorNativeToolDisplay({ ...tool, terminate: shouldTerminate })) {
|
|
678
|
-
run.recordedToolDisplayIds.push(tool.id);
|
|
679
|
-
}
|
|
680
|
-
}
|
|
681
|
-
applyCursorApproximateUsage(partial, model, context, takeCursorLiveSessionInputTokens(run, toolResultInputTokens));
|
|
682
|
-
partial.stopReason = "toolUse";
|
|
683
|
-
stream.push({ type: "done", reason: "toolUse", message: partial });
|
|
684
|
-
scheduleCursorNativeRunIdleDispose(run);
|
|
685
|
-
}
|
|
686
|
-
|
|
687
|
-
function emitCursorBridgeToolUseTurn(
|
|
688
|
-
stream: AssistantMessageEventStream,
|
|
689
|
-
partial: AssistantMessage,
|
|
690
|
-
model: Model<Api>,
|
|
691
|
-
context: Context,
|
|
692
|
-
run: CursorLiveRun,
|
|
693
|
-
toolResultInputTokens: number,
|
|
694
|
-
requests: CursorPiBridgeToolRequest[],
|
|
695
|
-
): void {
|
|
696
|
-
for (const request of requests) {
|
|
697
|
-
const contentIndex = partial.content.length;
|
|
698
|
-
partial.content.push({
|
|
699
|
-
type: "toolCall",
|
|
700
|
-
id: request.piToolCallId,
|
|
701
|
-
name: request.piToolName,
|
|
702
|
-
arguments: request.args,
|
|
703
|
-
});
|
|
704
|
-
stream.push({ type: "toolcall_start", contentIndex, partial });
|
|
705
|
-
stream.push({ type: "toolcall_delta", contentIndex, delta: JSON.stringify(request.args), partial });
|
|
706
|
-
const block = partial.content[contentIndex];
|
|
707
|
-
if (block.type === "toolCall") stream.push({ type: "toolcall_end", contentIndex, toolCall: block, partial });
|
|
708
|
-
}
|
|
709
|
-
applyCursorApproximateUsage(partial, model, context, takeCursorLiveSessionInputTokens(run, toolResultInputTokens));
|
|
710
|
-
partial.stopReason = "toolUse";
|
|
711
|
-
stream.push({ type: "done", reason: "toolUse", message: partial });
|
|
712
|
-
scheduleCursorNativeRunIdleDispose(run);
|
|
713
|
-
}
|
|
714
|
-
|
|
715
|
-
function isSuccessfulCursorLiveRun(run: CursorLiveRun): boolean {
|
|
716
|
-
return run.done && !run.cancelled && !run.errorMessage;
|
|
717
|
-
}
|
|
718
|
-
|
|
719
|
-
async function abandonSessionCursorAgent(scopeKey: string | undefined): Promise<void> {
|
|
720
|
-
if (!scopeKey) return;
|
|
721
|
-
await resetSessionCursorAgent(scopeKey);
|
|
722
|
-
}
|
|
723
|
-
|
|
724
|
-
async function releaseCursorLiveRun(run: CursorLiveRun): Promise<void> {
|
|
725
|
-
if (run.disposed) return;
|
|
726
|
-
const abandoned = !isSuccessfulCursorLiveRun(run);
|
|
727
|
-
run.disposed = true;
|
|
728
|
-
pendingCursorLiveRuns.delete(run.id);
|
|
729
|
-
clearCursorNativeRunIdleDispose(run);
|
|
730
|
-
run.bridgeRun?.cancel("Cursor live run released");
|
|
731
|
-
for (const toolDisplayId of run.recordedToolDisplayIds) deleteCursorNativeToolDisplay(toolDisplayId);
|
|
732
|
-
run.recordedToolDisplayIds = [];
|
|
733
|
-
run.waiters.clear();
|
|
734
|
-
if (run.sessionBridgeRun) {
|
|
735
|
-
run.sessionBridgeRun.setOnToolRequest(undefined);
|
|
736
|
-
}
|
|
737
|
-
if (run.bridgeRun && run.bridgeRun !== run.sessionBridgeRun) {
|
|
738
|
-
try {
|
|
739
|
-
await run.bridgeRun.dispose();
|
|
740
|
-
} catch {
|
|
741
|
-
// bridge disposal failure should not mask the provider result
|
|
742
|
-
}
|
|
743
|
-
}
|
|
744
|
-
if (abandoned) {
|
|
745
|
-
try {
|
|
746
|
-
await run.sdkRun?.cancel();
|
|
747
|
-
} catch {
|
|
748
|
-
// cancellation failure should not block session-agent abandonment
|
|
749
|
-
}
|
|
750
|
-
await abandonSessionCursorAgent(run.sessionAgentScopeKey);
|
|
751
|
-
}
|
|
752
|
-
}
|
|
753
|
-
|
|
754
|
-
async function emitCursorNativeRunNextTurn(
|
|
755
|
-
stream: AssistantMessageEventStream,
|
|
756
|
-
partial: AssistantMessage,
|
|
757
|
-
model: Model<Api>,
|
|
758
|
-
context: Context,
|
|
759
|
-
run: CursorLiveRun,
|
|
760
|
-
toolResultInputTokens: number,
|
|
761
|
-
signal?: AbortSignal,
|
|
762
|
-
): Promise<void> {
|
|
763
|
-
const turn: CursorLiveTurnState = {
|
|
764
|
-
stream,
|
|
765
|
-
partial,
|
|
766
|
-
thinkingContentIndex: -1,
|
|
767
|
-
textContentIndex: -1,
|
|
768
|
-
emittedText: "",
|
|
769
|
-
};
|
|
770
|
-
|
|
771
|
-
while (true) {
|
|
772
|
-
while (run.pendingEvents.length > 0) {
|
|
773
|
-
const event = run.pendingEvents[0];
|
|
774
|
-
if (event.type === "tool") {
|
|
775
|
-
await settleCursorLiveToolBatch(run);
|
|
776
|
-
if (signal?.aborted) throw new CursorAbortError();
|
|
777
|
-
closeCursorNativeTurnBlocks(turn);
|
|
778
|
-
const tools = collectCursorNativeToolBatch(run);
|
|
779
|
-
emitCursorNativeToolUseTurn(stream, partial, model, context, run, toolResultInputTokens, tools);
|
|
780
|
-
return;
|
|
781
|
-
}
|
|
782
|
-
if (event.type === "bridge-tool") {
|
|
783
|
-
await settleCursorLiveToolBatch(run);
|
|
784
|
-
if (signal?.aborted) throw new CursorAbortError();
|
|
785
|
-
closeCursorNativeTurnBlocks(turn);
|
|
786
|
-
const requests = collectCursorBridgeToolBatch(run);
|
|
787
|
-
emitCursorBridgeToolUseTurn(stream, partial, model, context, run, toolResultInputTokens, requests);
|
|
788
|
-
return;
|
|
789
|
-
}
|
|
790
|
-
run.pendingEvents.shift();
|
|
791
|
-
emitCursorLiveQueuedEvent(turn, event, run);
|
|
792
|
-
}
|
|
793
|
-
|
|
794
|
-
if (run.cancelled) {
|
|
795
|
-
partial.stopReason = "aborted";
|
|
796
|
-
stream.push({ type: "error", reason: "aborted", error: partial });
|
|
797
|
-
await releaseCursorLiveRun(run);
|
|
798
|
-
return;
|
|
799
|
-
}
|
|
800
|
-
if (run.errorMessage) {
|
|
801
|
-
partial.stopReason = "error";
|
|
802
|
-
partial.errorMessage = run.errorMessage;
|
|
803
|
-
stream.push({ type: "error", reason: "error", error: partial });
|
|
804
|
-
await releaseCursorLiveRun(run);
|
|
805
|
-
return;
|
|
806
|
-
}
|
|
807
|
-
if (run.done) {
|
|
808
|
-
closeCursorNativeTurnBlocks(turn);
|
|
809
|
-
const finalText = trimCurrentTurnAlreadyEmittedCursorText(run.finalText ?? run.textDeltas.join(""), turn.emittedText, run.emittedText);
|
|
810
|
-
if (finalText) {
|
|
811
|
-
await emitTextDeltas(stream, partial, splitTextIntoReplayDeltas(finalText));
|
|
812
|
-
}
|
|
813
|
-
applyCursorApproximateUsage(partial, model, context, takeCursorLiveSessionInputTokens(run, toolResultInputTokens));
|
|
814
|
-
partial.stopReason = "stop";
|
|
815
|
-
stream.push({ type: "done", reason: "stop", message: partial });
|
|
816
|
-
await releaseCursorLiveRun(run);
|
|
817
|
-
return;
|
|
818
|
-
}
|
|
819
|
-
|
|
820
|
-
await waitForCursorNativeRunProgress(run, signal);
|
|
821
|
-
}
|
|
822
|
-
}
|
|
823
|
-
|
|
824
|
-
async function replayPendingCursorLiveRun(
|
|
825
|
-
stream: AssistantMessageEventStream,
|
|
826
|
-
partial: AssistantMessage,
|
|
827
|
-
model: Model<Api>,
|
|
828
|
-
context: Context,
|
|
829
|
-
signal?: AbortSignal,
|
|
830
|
-
): Promise<boolean> {
|
|
831
|
-
const run = getPendingCursorLiveRun(context);
|
|
832
|
-
if (!run) return false;
|
|
833
|
-
clearCursorNativeRunIdleDispose(run);
|
|
834
|
-
const consumed = consumeCursorLiveRunToolResults(run, context);
|
|
835
|
-
run.bridgeRun?.resolveToolResults(consumed.toolResults);
|
|
836
|
-
try {
|
|
837
|
-
await emitCursorNativeRunNextTurn(stream, partial, model, context, run, consumed.toolResultInputTokens, signal);
|
|
838
|
-
} catch (error) {
|
|
839
|
-
if (error instanceof CursorAbortError) await releaseCursorLiveRun(run);
|
|
840
|
-
throw error;
|
|
841
|
-
}
|
|
842
|
-
return true;
|
|
843
|
-
}
|
|
844
|
-
|
|
845
132
|
export function streamCursor(
|
|
846
133
|
model: Model<Api>,
|
|
847
134
|
context: Context,
|
|
@@ -864,13 +151,13 @@ export function streamCursor(
|
|
|
864
151
|
|
|
865
152
|
try {
|
|
866
153
|
const throwIfAborted = (): void => {
|
|
867
|
-
if (options?.signal?.aborted) throw new
|
|
154
|
+
if (options?.signal?.aborted) throw new CursorLiveRunAbortError();
|
|
868
155
|
};
|
|
869
156
|
|
|
870
157
|
stream.push({ type: "start", partial });
|
|
871
158
|
throwIfAborted();
|
|
872
159
|
|
|
873
|
-
if (await
|
|
160
|
+
if ((await drainExistingCursorLiveRunBeforeSend(stream, partial, model, context, options?.signal)) === "stream_ended") {
|
|
874
161
|
stream.end();
|
|
875
162
|
return;
|
|
876
163
|
}
|
|
@@ -895,7 +182,7 @@ export function streamCursor(
|
|
|
895
182
|
settingSources,
|
|
896
183
|
onBridgeToolRequest: (request: CursorPiBridgeToolRequest) => {
|
|
897
184
|
if (liveRunForBridgeQueue && !liveRunForBridgeQueue.disposed) {
|
|
898
|
-
|
|
185
|
+
cursorLiveRuns.queueEvent(liveRunForBridgeQueue, { type: "bridge-tool", request });
|
|
899
186
|
} else {
|
|
900
187
|
queuedBridgeRequestsBeforeLiveRun.push(request);
|
|
901
188
|
}
|
|
@@ -921,354 +208,38 @@ export function streamCursor(
|
|
|
921
208
|
}
|
|
922
209
|
const sessionBridgeRun = sessionAgentLease.bridgeRun;
|
|
923
210
|
const promptInputTokens = estimateCursorPromptInputTokens(prompt, promptOptions);
|
|
924
|
-
let thinkingContentIndex = -1;
|
|
925
|
-
let activityTraceChars = 0;
|
|
926
|
-
let activityTraceTruncated = false;
|
|
927
|
-
let nativeToolDisplayCounter = 0;
|
|
928
|
-
let textContentIndex = -1;
|
|
929
211
|
const useNativeToolReplay = isCursorNativeToolDisplayRuntimeEnabled();
|
|
930
212
|
const nativeReplayId = createCursorNativeReplayId();
|
|
931
213
|
const textDeltas: string[] = [];
|
|
932
|
-
let nativeToolReplayStarted = false;
|
|
933
214
|
const useLiveRun = useNativeToolReplay || bridgeRun !== undefined;
|
|
934
215
|
const liveRun: CursorLiveRun | undefined = useLiveRun
|
|
935
|
-
? {
|
|
216
|
+
? cursorLiveRuns.start({
|
|
936
217
|
id: useNativeToolReplay ? nativeReplayId : bridgeRun!.id,
|
|
937
218
|
agent,
|
|
938
219
|
bridgeRun,
|
|
939
220
|
sessionBridgeRun,
|
|
940
221
|
sessionAgentScopeKey,
|
|
941
|
-
|
|
942
|
-
pendingEvents: [],
|
|
222
|
+
promptInputTokens,
|
|
943
223
|
textDeltas,
|
|
944
|
-
|
|
945
|
-
recordedToolDisplayIds: [],
|
|
946
|
-
done: false,
|
|
947
|
-
cancelled: false,
|
|
948
|
-
disposed: false,
|
|
949
|
-
waiters: new Set(),
|
|
950
|
-
}
|
|
224
|
+
})
|
|
951
225
|
: undefined;
|
|
952
226
|
if (liveRun) {
|
|
953
|
-
pendingCursorLiveRuns.set(liveRun.id, liveRun);
|
|
954
227
|
activeLiveRun = liveRun;
|
|
955
228
|
liveRunForBridgeQueue = liveRun;
|
|
956
229
|
for (const request of queuedBridgeRequestsBeforeLiveRun.splice(0)) {
|
|
957
|
-
|
|
230
|
+
cursorLiveRuns.queueEvent(liveRun, { type: "bridge-tool", request });
|
|
958
231
|
}
|
|
959
232
|
}
|
|
960
|
-
const
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
const appendLiveTextDelta = (text: string): void => {
|
|
971
|
-
if (textContentIndex < 0) {
|
|
972
|
-
textContentIndex = partial.content.length;
|
|
973
|
-
partial.content.push({ type: "text", text: "" });
|
|
974
|
-
stream.push({ type: "text_start", contentIndex: textContentIndex, partial });
|
|
975
|
-
}
|
|
976
|
-
const block = partial.content[textContentIndex];
|
|
977
|
-
if (block.type !== "text") return;
|
|
978
|
-
block.text += text;
|
|
979
|
-
stream.push({
|
|
980
|
-
type: "text_delta",
|
|
981
|
-
contentIndex: textContentIndex,
|
|
982
|
-
delta: text,
|
|
983
|
-
partial,
|
|
984
|
-
});
|
|
985
|
-
};
|
|
986
|
-
|
|
987
|
-
const appendTraceDelta = (text: string): void => {
|
|
988
|
-
if (activityTraceTruncated) return;
|
|
989
|
-
|
|
990
|
-
let delta = text;
|
|
991
|
-
if (activityTraceChars + delta.length > CURSOR_ACTIVITY_TRACE_MAX_CHARS) {
|
|
992
|
-
const remainingChars = Math.max(CURSOR_ACTIVITY_TRACE_MAX_CHARS - activityTraceChars, 0);
|
|
993
|
-
delta = `${delta.slice(0, remainingChars)}\n[Cursor activity trace truncated]\n`;
|
|
994
|
-
activityTraceTruncated = true;
|
|
995
|
-
}
|
|
996
|
-
if (!delta) return;
|
|
997
|
-
|
|
998
|
-
if (thinkingContentIndex < 0) {
|
|
999
|
-
thinkingContentIndex = partial.content.length;
|
|
1000
|
-
partial.content.push({ type: "thinking", thinking: "" });
|
|
1001
|
-
stream.push({ type: "thinking_start", contentIndex: thinkingContentIndex, partial });
|
|
1002
|
-
}
|
|
1003
|
-
const block = partial.content[thinkingContentIndex];
|
|
1004
|
-
if (block.type === "thinking") {
|
|
1005
|
-
block.thinking += delta;
|
|
1006
|
-
activityTraceChars += delta.length;
|
|
1007
|
-
stream.push({
|
|
1008
|
-
type: "thinking_delta",
|
|
1009
|
-
contentIndex: thinkingContentIndex,
|
|
1010
|
-
delta,
|
|
1011
|
-
partial,
|
|
1012
|
-
});
|
|
1013
|
-
}
|
|
1014
|
-
};
|
|
1015
|
-
|
|
1016
|
-
const appendTraceLine = (text: string): void => {
|
|
1017
|
-
appendTraceDelta(`${text}\n`);
|
|
1018
|
-
};
|
|
1019
|
-
|
|
1020
|
-
const appendTraceBlock = (text: string): void => {
|
|
1021
|
-
closeTraceBlock();
|
|
1022
|
-
appendTraceDelta(text.endsWith("\n") ? text : `${text}\n`);
|
|
1023
|
-
closeTraceBlock();
|
|
1024
|
-
};
|
|
1025
|
-
|
|
1026
|
-
const emitCursorToolTrace = (text: string): void => {
|
|
1027
|
-
const traceText = text.endsWith("\n") ? text : `${text}\n`;
|
|
1028
|
-
if (liveRun) {
|
|
1029
|
-
queueCursorNativeEvent(liveRun, { type: "thinking-delta", text: traceText });
|
|
1030
|
-
queueCursorNativeEvent(liveRun, { type: "thinking-completed" });
|
|
1031
|
-
return;
|
|
1032
|
-
}
|
|
1033
|
-
appendTraceBlock(traceText);
|
|
1034
|
-
};
|
|
1035
|
-
|
|
1036
|
-
const closeTraceBlock = (): void => {
|
|
1037
|
-
if (thinkingContentIndex < 0) return;
|
|
1038
|
-
const block = partial.content[thinkingContentIndex];
|
|
1039
|
-
if (block.type === "thinking") {
|
|
1040
|
-
stream.push({
|
|
1041
|
-
type: "thinking_end",
|
|
1042
|
-
contentIndex: thinkingContentIndex,
|
|
1043
|
-
content: block.thinking,
|
|
1044
|
-
partial,
|
|
1045
|
-
});
|
|
1046
|
-
}
|
|
1047
|
-
thinkingContentIndex = -1;
|
|
1048
|
-
};
|
|
1049
|
-
|
|
1050
|
-
const flushText = (deltas: string[]): string => {
|
|
1051
|
-
for (const delta of deltas) appendLiveTextDelta(delta);
|
|
1052
|
-
if (textContentIndex < 0) return "";
|
|
1053
|
-
const block = partial.content[textContentIndex];
|
|
1054
|
-
if (block.type !== "text") return "";
|
|
1055
|
-
stream.push({
|
|
1056
|
-
type: "text_end",
|
|
1057
|
-
contentIndex: textContentIndex,
|
|
1058
|
-
content: block.text,
|
|
1059
|
-
partial,
|
|
1060
|
-
});
|
|
1061
|
-
return block.text;
|
|
1062
|
-
};
|
|
1063
|
-
|
|
1064
|
-
const getToolFingerprint = (value: unknown): string => {
|
|
1065
|
-
try {
|
|
1066
|
-
return JSON.stringify(value);
|
|
1067
|
-
} catch {
|
|
1068
|
-
return String(value);
|
|
1069
|
-
}
|
|
1070
|
-
};
|
|
1071
|
-
|
|
1072
|
-
const getStartedToolCallFingerprint = (toolCall: unknown): string => {
|
|
1073
|
-
return getToolFingerprint({ toolName: getCursorToolName(toolCall), args: getObjectField(toolCall, "args") });
|
|
1074
|
-
};
|
|
1075
|
-
|
|
1076
|
-
const clearStartedToolCall = (callId: string): void => {
|
|
1077
|
-
startedToolCalls.delete(callId);
|
|
1078
|
-
bridgeStartedToolCallIds.delete(callId);
|
|
1079
|
-
activeShellCallIds.delete(callId);
|
|
1080
|
-
ambiguousShellOutputCallIds.delete(callId);
|
|
1081
|
-
};
|
|
1082
|
-
|
|
1083
|
-
const takeBridgeStartedToolCallId = (callId: unknown): string | undefined => {
|
|
1084
|
-
if (typeof callId !== "string" || !bridgeStartedToolCallIds.has(callId)) return undefined;
|
|
1085
|
-
bridgeStartedToolCallIds.delete(callId);
|
|
1086
|
-
return callId;
|
|
1087
|
-
};
|
|
1088
|
-
|
|
1089
|
-
const takeShellOutputDeltas = (callId: string): CursorShellOutputDeltas | undefined => {
|
|
1090
|
-
const deltas = shellOutputDeltasByCallId.get(callId);
|
|
1091
|
-
shellOutputDeltasByCallId.delete(callId);
|
|
1092
|
-
return deltas;
|
|
1093
|
-
};
|
|
1094
|
-
|
|
1095
|
-
const appendShellOutputDelta = (delta: CursorShellOutputDelta): void => {
|
|
1096
|
-
if (activeShellCallIds.size !== 1) {
|
|
1097
|
-
for (const activeCallId of activeShellCallIds) {
|
|
1098
|
-
ambiguousShellOutputCallIds.add(activeCallId);
|
|
1099
|
-
shellOutputDeltasByCallId.delete(activeCallId);
|
|
1100
|
-
}
|
|
1101
|
-
return;
|
|
1102
|
-
}
|
|
1103
|
-
const [callId] = activeShellCallIds;
|
|
1104
|
-
if (!callId || ambiguousShellOutputCallIds.has(callId)) return;
|
|
1105
|
-
let deltas = shellOutputDeltasByCallId.get(callId);
|
|
1106
|
-
if (!deltas) {
|
|
1107
|
-
deltas = { stdout: [], stderr: [] };
|
|
1108
|
-
shellOutputDeltasByCallId.set(callId, deltas);
|
|
1109
|
-
}
|
|
1110
|
-
deltas[delta.stream].push(delta.data);
|
|
1111
|
-
};
|
|
1112
|
-
|
|
1113
|
-
const removeStartedToolCallForStep = (toolCall: unknown, stepId: unknown): string | undefined => {
|
|
1114
|
-
if (typeof stepId === "string" && startedToolCalls.has(stepId)) {
|
|
1115
|
-
clearStartedToolCall(stepId);
|
|
1116
|
-
return stepId;
|
|
1117
|
-
}
|
|
1118
|
-
const fingerprint = getStartedToolCallFingerprint(toolCall);
|
|
1119
|
-
for (const [callId, startedToolCall] of startedToolCalls) {
|
|
1120
|
-
if (getStartedToolCallFingerprint(startedToolCall) !== fingerprint) continue;
|
|
1121
|
-
clearStartedToolCall(callId);
|
|
1122
|
-
return callId;
|
|
1123
|
-
}
|
|
1124
|
-
return undefined;
|
|
1125
|
-
};
|
|
1126
|
-
|
|
1127
|
-
const discardIncompleteStartedToolCalls = (): void => {
|
|
1128
|
-
startedToolCalls.clear();
|
|
1129
|
-
bridgeStartedToolCallIds.clear();
|
|
1130
|
-
activeShellCallIds.clear();
|
|
1131
|
-
ambiguousShellOutputCallIds.clear();
|
|
1132
|
-
shellOutputDeltasByCallId.clear();
|
|
1133
|
-
};
|
|
1134
|
-
|
|
1135
|
-
const handleCompletedToolCall = (
|
|
1136
|
-
toolCall: unknown,
|
|
1137
|
-
options: { identity?: string; source?: "started" | "fallback" } = {},
|
|
1138
|
-
): void => {
|
|
1139
|
-
const planText = getCursorCreatePlanText(toolCall);
|
|
1140
|
-
if (planText) cursorPlanTextCandidate = scrubSensitiveText(planText, resolvedApiKey);
|
|
1141
|
-
|
|
1142
|
-
if (liveRun?.bridgeRun?.isBridgeMcpToolCall(toolCall)) {
|
|
1143
|
-
if (options.identity) completedToolIdentities.add(options.identity);
|
|
1144
|
-
return;
|
|
1145
|
-
}
|
|
1146
|
-
const transcript = scrubSensitiveText(formatCursorToolTranscript(toolCall, { cwd }), resolvedApiKey);
|
|
1147
|
-
const display = buildCursorPiToolDisplay(toolCall, { cwd });
|
|
1148
|
-
const fingerprint = getToolFingerprint({ toolName: display.toolName, args: display.args, result: display.result });
|
|
1149
|
-
if (options.identity && completedToolIdentities.has(options.identity)) return;
|
|
1150
|
-
if (options.source === "started") {
|
|
1151
|
-
if (completedFallbackToolFingerprints.has(fingerprint)) return;
|
|
1152
|
-
} else if (completedStartedToolFingerprints.has(fingerprint) || completedFallbackToolFingerprints.has(fingerprint)) {
|
|
1153
|
-
return;
|
|
1154
|
-
}
|
|
1155
|
-
if (options.identity) completedToolIdentities.add(options.identity);
|
|
1156
|
-
if (options.source === "started") {
|
|
1157
|
-
completedStartedToolFingerprints.add(fingerprint);
|
|
1158
|
-
} else {
|
|
1159
|
-
completedFallbackToolFingerprints.add(fingerprint);
|
|
1160
|
-
}
|
|
1161
|
-
|
|
1162
|
-
const nativeRenderable = canRenderCursorToolNatively(display.toolName);
|
|
1163
|
-
const route = useNativeToolReplay && nativeRenderable && liveRun ? "native_replay" : "trace";
|
|
1164
|
-
|
|
1165
|
-
if (route === "native_replay" && liveRun) {
|
|
1166
|
-
nativeToolReplayStarted = true;
|
|
1167
|
-
const id = `${nativeReplayId}-tool-${++nativeToolDisplayCounter}`;
|
|
1168
|
-
queueCursorNativeEvent(liveRun, {
|
|
1169
|
-
type: "tool",
|
|
1170
|
-
tool: {
|
|
1171
|
-
...display,
|
|
1172
|
-
id,
|
|
1173
|
-
args: scrubDisplayValue(display.args, resolvedApiKey) as Record<string, unknown>,
|
|
1174
|
-
result: scrubDisplayValue(display.result, resolvedApiKey) as typeof display.result,
|
|
1175
|
-
},
|
|
1176
|
-
});
|
|
1177
|
-
return;
|
|
1178
|
-
}
|
|
1179
|
-
|
|
1180
|
-
emitCursorToolTrace(transcript || `Cursor tool: ${formatCursorToolName(toolCall)} completed`);
|
|
1181
|
-
};
|
|
1182
|
-
|
|
1183
|
-
const onDelta = (args: { update: InteractionUpdate }): void => {
|
|
1184
|
-
const update = args.update;
|
|
1185
|
-
|
|
1186
|
-
if (update.type === "text-delta") {
|
|
1187
|
-
textDeltas.push(update.text);
|
|
1188
|
-
if (liveRun) {
|
|
1189
|
-
queueCursorNativeEvent(liveRun, { type: "text-delta", text: update.text });
|
|
1190
|
-
} else {
|
|
1191
|
-
appendLiveTextDelta(update.text);
|
|
1192
|
-
}
|
|
1193
|
-
} else if (update.type === "thinking-delta") {
|
|
1194
|
-
if (liveRun) {
|
|
1195
|
-
queueCursorNativeEvent(liveRun, { type: "thinking-delta", text: update.text });
|
|
1196
|
-
} else {
|
|
1197
|
-
appendTraceDelta(update.text);
|
|
1198
|
-
}
|
|
1199
|
-
} else if (update.type === "thinking-completed") {
|
|
1200
|
-
if (liveRun) {
|
|
1201
|
-
queueCursorNativeEvent(liveRun, { type: "thinking-completed" });
|
|
1202
|
-
} else {
|
|
1203
|
-
closeTraceBlock();
|
|
1204
|
-
}
|
|
1205
|
-
} else if (update.type === "tool-call-started") {
|
|
1206
|
-
if (liveRun?.bridgeRun?.isBridgeMcpToolCall(update.toolCall)) {
|
|
1207
|
-
if (typeof update.callId === "string") bridgeStartedToolCallIds.add(update.callId);
|
|
1208
|
-
} else {
|
|
1209
|
-
startedToolCalls.set(update.callId, update.toolCall);
|
|
1210
|
-
if (isCursorShellToolCall(update.toolCall)) activeShellCallIds.add(update.callId);
|
|
1211
|
-
}
|
|
1212
|
-
} else if (update.type === "tool-call-completed") {
|
|
1213
|
-
const identity = typeof update.callId === "string" ? `cursor-tool:${update.callId}` : undefined;
|
|
1214
|
-
const bridgeStartedCallId = takeBridgeStartedToolCallId(update.callId);
|
|
1215
|
-
if (bridgeStartedCallId) {
|
|
1216
|
-
completedToolIdentities.add(`cursor-tool:${bridgeStartedCallId}`);
|
|
1217
|
-
return;
|
|
1218
|
-
}
|
|
1219
|
-
const mergedToolCall = mergeCursorToolCalls(startedToolCalls.get(update.callId), update.toolCall);
|
|
1220
|
-
clearStartedToolCall(update.callId);
|
|
1221
|
-
const toolCallWithShellOutput = mergeShellOutputDeltasIntoCursorToolCall(mergedToolCall, takeShellOutputDeltas(update.callId));
|
|
1222
|
-
handleCompletedToolCall(toolCallWithShellOutput, {
|
|
1223
|
-
identity,
|
|
1224
|
-
source: identity ? "started" : "fallback",
|
|
1225
|
-
});
|
|
1226
|
-
} else if (update.type === "shell-output-delta") {
|
|
1227
|
-
const delta = getCursorShellOutputDelta(update);
|
|
1228
|
-
if (delta) appendShellOutputDelta(delta);
|
|
1229
|
-
} else if (update.type === "summary") {
|
|
1230
|
-
const summary = `Cursor summary: ${truncateSingleLine(update.summary)}\n`;
|
|
1231
|
-
if (liveRun) {
|
|
1232
|
-
queueCursorNativeEvent(liveRun, { type: "thinking-delta", text: summary });
|
|
1233
|
-
} else {
|
|
1234
|
-
appendTraceDelta(summary);
|
|
1235
|
-
}
|
|
1236
|
-
}
|
|
1237
|
-
// Cursor turn-ended usage is intentionally not copied into pi usage: the SDK reports
|
|
1238
|
-
// cumulative internal agent/tool/cache tokens, not the replayable pi prompt context.
|
|
1239
|
-
// partial-tool-call, summary-started, summary-completed, turn-ended,
|
|
1240
|
-
// token-delta, step-* are intentionally not surfaced.
|
|
1241
|
-
};
|
|
1242
|
-
|
|
1243
|
-
const onStep = (args: { step: unknown }): void => {
|
|
1244
|
-
const stepType = getObjectField(args.step, "type");
|
|
1245
|
-
const step = getObjectField(args.step, "message") ? args.step : undefined;
|
|
1246
|
-
const rawStepToolCall = getObjectField(step, "message");
|
|
1247
|
-
if (stepType !== "toolCall") return;
|
|
1248
|
-
const toolCall = rawStepToolCall;
|
|
1249
|
-
const stepId = getObjectField(args.step, "id") ?? getObjectField(toolCall, "id") ?? getObjectField(toolCall, "callId");
|
|
1250
|
-
if (toolCall) {
|
|
1251
|
-
const bridgeStartedCallId = takeBridgeStartedToolCallId(stepId);
|
|
1252
|
-
if (bridgeStartedCallId) {
|
|
1253
|
-
completedToolIdentities.add(`cursor-tool:${bridgeStartedCallId}`);
|
|
1254
|
-
return;
|
|
1255
|
-
}
|
|
1256
|
-
const matchedStartedCallId = removeStartedToolCallForStep(toolCall, stepId);
|
|
1257
|
-
const toolCallWithShellOutput = mergeShellOutputDeltasIntoCursorToolCall(
|
|
1258
|
-
toolCall,
|
|
1259
|
-
matchedStartedCallId ? takeShellOutputDeltas(matchedStartedCallId) : undefined,
|
|
1260
|
-
);
|
|
1261
|
-
if (liveRun?.bridgeRun?.isBridgeMcpToolCall(toolCall)) {
|
|
1262
|
-
if (matchedStartedCallId) completedToolIdentities.add(`cursor-tool:${matchedStartedCallId}`);
|
|
1263
|
-
return;
|
|
1264
|
-
}
|
|
1265
|
-
const identityId = typeof stepId === "string" ? stepId : matchedStartedCallId;
|
|
1266
|
-
handleCompletedToolCall(toolCallWithShellOutput, {
|
|
1267
|
-
identity: identityId ? `cursor-tool:${identityId}` : undefined,
|
|
1268
|
-
});
|
|
1269
|
-
if (matchedStartedCallId && matchedStartedCallId !== stepId) completedToolIdentities.add(`cursor-tool:${matchedStartedCallId}`);
|
|
1270
|
-
}
|
|
1271
|
-
};
|
|
233
|
+
const turnCoordinator = new CursorSdkTurnCoordinator({
|
|
234
|
+
stream,
|
|
235
|
+
partial,
|
|
236
|
+
cwd,
|
|
237
|
+
resolvedApiKey,
|
|
238
|
+
liveRun,
|
|
239
|
+
useNativeToolReplay,
|
|
240
|
+
nativeReplayId,
|
|
241
|
+
textDeltas,
|
|
242
|
+
});
|
|
1272
243
|
|
|
1273
244
|
// Handle abort signal
|
|
1274
245
|
let run: Awaited<ReturnType<SDKAgent["send"]>> | null = null;
|
|
@@ -1284,12 +255,15 @@ export function streamCursor(
|
|
|
1284
255
|
throwIfAborted();
|
|
1285
256
|
run = await agent.send(
|
|
1286
257
|
{ text: prompt.text, images: prompt.images.length > 0 ? prompt.images : undefined },
|
|
1287
|
-
{
|
|
258
|
+
{
|
|
259
|
+
onDelta: (args) => turnCoordinator.handleDelta(args.update),
|
|
260
|
+
onStep: (args) => turnCoordinator.handleStep(args.step),
|
|
261
|
+
},
|
|
1288
262
|
);
|
|
1289
|
-
if (liveRun) liveRun
|
|
263
|
+
if (liveRun) cursorLiveRuns.attachSdkRun(liveRun, run);
|
|
1290
264
|
if (options?.signal?.aborted) {
|
|
1291
265
|
await run.cancel().catch(() => {});
|
|
1292
|
-
throw new
|
|
266
|
+
throw new CursorLiveRunAbortError();
|
|
1293
267
|
}
|
|
1294
268
|
|
|
1295
269
|
if (liveRun) {
|
|
@@ -1297,39 +271,35 @@ export function streamCursor(
|
|
|
1297
271
|
.wait()
|
|
1298
272
|
.then(async (result) => {
|
|
1299
273
|
if (liveRun.disposed) return;
|
|
1300
|
-
discardIncompleteStartedToolCalls();
|
|
274
|
+
turnCoordinator.discardIncompleteStartedToolCalls();
|
|
1301
275
|
await cacheSdkContextWindow(liveRun.agent.agentId, model.id);
|
|
1302
276
|
if (liveRun.disposed) return;
|
|
1303
277
|
if (result.status === "finished" && !options?.signal?.aborted) {
|
|
1304
278
|
commitSessionAgentSend(sessionAgentScopeKey, context, bootstrap);
|
|
279
|
+
cursorLiveRuns.markFinished(
|
|
280
|
+
liveRun,
|
|
281
|
+
selectCursorFinalText(result.result, liveRun.textDeltas, liveRun.emittedText, turnCoordinator.planTextCandidate),
|
|
282
|
+
);
|
|
283
|
+
} else if (result.status === "cancelled" || options?.signal?.aborted) {
|
|
284
|
+
cursorLiveRuns.markCancelled(liveRun);
|
|
1305
285
|
} else {
|
|
1306
|
-
|
|
1307
|
-
}
|
|
1308
|
-
liveRun.cancelled = result.status === "cancelled";
|
|
1309
|
-
if (result.status === "error") {
|
|
1310
|
-
liveRun.errorMessage = sanitizeError(result.result ?? "Cursor SDK run failed", resolvedApiKey ?? options?.apiKey);
|
|
1311
|
-
} else {
|
|
1312
|
-
liveRun.finalText = selectCursorFinalText(result.result, liveRun.textDeltas, liveRun.emittedText, cursorPlanTextCandidate);
|
|
286
|
+
cursorLiveRuns.markError(liveRun, sanitizeError(result.result ?? "Cursor SDK run failed", resolvedApiKey ?? options?.apiKey));
|
|
1313
287
|
}
|
|
1314
|
-
liveRun.done = true;
|
|
1315
|
-
notifyCursorNativeRun(liveRun);
|
|
1316
|
-
scheduleCursorNativeRunIdleDispose(liveRun);
|
|
1317
288
|
})
|
|
1318
289
|
.catch(async (error: unknown) => {
|
|
1319
290
|
if (liveRun.disposed) return;
|
|
1320
|
-
|
|
1321
|
-
liveRun.errorMessage = sanitizeError(error, resolvedApiKey ?? options?.apiKey);
|
|
1322
|
-
notifyCursorNativeRun(liveRun);
|
|
1323
|
-
scheduleCursorNativeRunIdleDispose(liveRun);
|
|
291
|
+
cursorLiveRuns.markError(liveRun, sanitizeError(error, resolvedApiKey ?? options?.apiKey));
|
|
1324
292
|
});
|
|
1325
293
|
|
|
1326
294
|
try {
|
|
1327
|
-
await
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
295
|
+
await cursorLiveRuns.withRunLease(liveRun, options?.signal, async () => {
|
|
296
|
+
await cursorLiveRuns.waitForProgress(liveRun, options?.signal);
|
|
297
|
+
await settleCursorLiveToolBatch(liveRun);
|
|
298
|
+
turnCoordinator.closeTraceBlock();
|
|
299
|
+
await drainCursorLiveRunTurn(stream, partial, model, context, liveRun, 0, { mode: "emit", signal: options?.signal });
|
|
300
|
+
});
|
|
1331
301
|
} catch (error) {
|
|
1332
|
-
if (error instanceof
|
|
302
|
+
if (error instanceof CursorLiveRunAbortError) await cursorLiveRuns.release(liveRun);
|
|
1333
303
|
throw error;
|
|
1334
304
|
}
|
|
1335
305
|
agent = null;
|
|
@@ -1337,12 +307,12 @@ export function streamCursor(
|
|
|
1337
307
|
}
|
|
1338
308
|
|
|
1339
309
|
const result = await run.wait();
|
|
1340
|
-
discardIncompleteStartedToolCalls();
|
|
310
|
+
turnCoordinator.discardIncompleteStartedToolCalls();
|
|
1341
311
|
await cacheSdkContextWindow(agent.agentId, model.id);
|
|
1342
312
|
|
|
1343
313
|
// Close any open thinking/activity trace, then use the final run result only when
|
|
1344
314
|
// Cursor did not stream text deltas.
|
|
1345
|
-
closeTraceBlock();
|
|
315
|
+
turnCoordinator.closeTraceBlock();
|
|
1346
316
|
|
|
1347
317
|
if (result.status === "cancelled") {
|
|
1348
318
|
await abandonSessionCursorAgent(sessionAgentScopeKey);
|
|
@@ -1355,17 +325,17 @@ export function streamCursor(
|
|
|
1355
325
|
stream.push({ type: "error", reason: "error", error: partial });
|
|
1356
326
|
} else {
|
|
1357
327
|
commitSessionAgentSend(sessionAgentScopeKey, context, bootstrap);
|
|
1358
|
-
const finalCursorText = selectCursorFinalText(result.result, textDeltas, textDeltas.join(""),
|
|
328
|
+
const finalCursorText = selectCursorFinalText(result.result, textDeltas, textDeltas.join(""), turnCoordinator.planTextCandidate, {
|
|
1359
329
|
allowPartialPrefix: true,
|
|
1360
330
|
});
|
|
1361
|
-
flushText(hasUsableText(finalCursorText) ? [finalCursorText] : []);
|
|
331
|
+
turnCoordinator.flushText(hasUsableText(finalCursorText) ? [finalCursorText] : []);
|
|
1362
332
|
applyCursorApproximateUsage(partial, model, context, promptInputTokens);
|
|
1363
333
|
stream.push({ type: "done", reason: "stop", message: partial });
|
|
1364
334
|
}
|
|
1365
335
|
} catch (error) {
|
|
1366
|
-
if (activeLiveRun && !activeLiveRun.disposed) await
|
|
336
|
+
if (activeLiveRun && !activeLiveRun.disposed) await cursorLiveRuns.release(activeLiveRun);
|
|
1367
337
|
else await abandonSessionCursorAgent(sessionAgentScopeKey);
|
|
1368
|
-
if (error instanceof
|
|
338
|
+
if (error instanceof CursorLiveRunAbortError) {
|
|
1369
339
|
partial.stopReason = "aborted";
|
|
1370
340
|
stream.push({ type: "error", reason: "aborted", error: partial });
|
|
1371
341
|
} else {
|
|
@@ -1389,12 +359,11 @@ export function streamCursor(
|
|
|
1389
359
|
|
|
1390
360
|
export const __testUtils = {
|
|
1391
361
|
DEFAULT_CURSOR_NATIVE_REPLAY_IDLE_DISPOSE_MS,
|
|
1392
|
-
pendingCursorNativeRunCount:
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
},
|
|
362
|
+
pendingCursorNativeRunCount: cursorLiveRuns.count,
|
|
363
|
+
getPendingCursorLiveRun,
|
|
364
|
+
getActiveCursorLiveRunForScope: cursorLiveRuns.getActiveForScope,
|
|
365
|
+
hasTrailingUserMessagesAfterToolResults,
|
|
366
|
+
setCursorNativeReplayIdleDisposeMs,
|
|
367
|
+
resetCursorNativeReplayIdleDisposeMs,
|
|
1399
368
|
resetSessionCursorAgents: () => disposeAllSessionCursorAgents(),
|
|
1400
369
|
};
|