@playwo/opencode-cursor-oauth 0.0.0-dev.c80ebcb27754 → 0.0.0-dev.da5538092563

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.
Files changed (76) hide show
  1. package/README.md +32 -83
  2. package/dist/auth.js +27 -3
  3. package/dist/constants.d.ts +2 -0
  4. package/dist/constants.js +2 -0
  5. package/dist/cursor/bidi-session.d.ts +12 -0
  6. package/dist/cursor/bidi-session.js +164 -0
  7. package/dist/cursor/config.d.ts +4 -0
  8. package/dist/cursor/config.js +4 -0
  9. package/dist/cursor/connect-framing.d.ts +10 -0
  10. package/dist/cursor/connect-framing.js +80 -0
  11. package/dist/cursor/headers.d.ts +6 -0
  12. package/dist/cursor/headers.js +16 -0
  13. package/dist/cursor/index.d.ts +5 -0
  14. package/dist/cursor/index.js +5 -0
  15. package/dist/cursor/unary-rpc.d.ts +12 -0
  16. package/dist/cursor/unary-rpc.js +124 -0
  17. package/dist/index.d.ts +2 -14
  18. package/dist/index.js +2 -229
  19. package/dist/logger.d.ts +7 -0
  20. package/dist/logger.js +150 -0
  21. package/dist/models.d.ts +3 -0
  22. package/dist/models.js +80 -54
  23. package/dist/openai/index.d.ts +3 -0
  24. package/dist/openai/index.js +3 -0
  25. package/dist/openai/messages.d.ts +39 -0
  26. package/dist/openai/messages.js +228 -0
  27. package/dist/openai/tools.d.ts +7 -0
  28. package/dist/openai/tools.js +58 -0
  29. package/dist/openai/types.d.ts +41 -0
  30. package/dist/openai/types.js +1 -0
  31. package/dist/plugin/cursor-auth-plugin.d.ts +3 -0
  32. package/dist/plugin/cursor-auth-plugin.js +139 -0
  33. package/dist/proto/agent_pb.js +637 -319
  34. package/dist/provider/index.d.ts +2 -0
  35. package/dist/provider/index.js +2 -0
  36. package/dist/provider/model-cost.d.ts +9 -0
  37. package/dist/provider/model-cost.js +206 -0
  38. package/dist/provider/models.d.ts +8 -0
  39. package/dist/provider/models.js +86 -0
  40. package/dist/proxy/bridge-close-controller.d.ts +6 -0
  41. package/dist/proxy/bridge-close-controller.js +37 -0
  42. package/dist/proxy/bridge-non-streaming.d.ts +3 -0
  43. package/dist/proxy/bridge-non-streaming.js +123 -0
  44. package/dist/proxy/bridge-session.d.ts +5 -0
  45. package/dist/proxy/bridge-session.js +11 -0
  46. package/dist/proxy/bridge-streaming.d.ts +5 -0
  47. package/dist/proxy/bridge-streaming.js +409 -0
  48. package/dist/proxy/bridge.d.ts +3 -0
  49. package/dist/proxy/bridge.js +3 -0
  50. package/dist/proxy/chat-completion.d.ts +2 -0
  51. package/dist/proxy/chat-completion.js +153 -0
  52. package/dist/proxy/conversation-meta.d.ts +12 -0
  53. package/dist/proxy/conversation-meta.js +1 -0
  54. package/dist/proxy/conversation-state.d.ts +35 -0
  55. package/dist/proxy/conversation-state.js +95 -0
  56. package/dist/proxy/cursor-request.d.ts +6 -0
  57. package/dist/proxy/cursor-request.js +101 -0
  58. package/dist/proxy/index.d.ts +12 -0
  59. package/dist/proxy/index.js +12 -0
  60. package/dist/proxy/server.d.ts +6 -0
  61. package/dist/proxy/server.js +107 -0
  62. package/dist/proxy/sse.d.ts +5 -0
  63. package/dist/proxy/sse.js +5 -0
  64. package/dist/proxy/state-sync.d.ts +2 -0
  65. package/dist/proxy/state-sync.js +17 -0
  66. package/dist/proxy/stream-dispatch.d.ts +42 -0
  67. package/dist/proxy/stream-dispatch.js +641 -0
  68. package/dist/proxy/stream-state.d.ts +7 -0
  69. package/dist/proxy/stream-state.js +1 -0
  70. package/dist/proxy/title.d.ts +1 -0
  71. package/dist/proxy/title.js +103 -0
  72. package/dist/proxy/types.d.ts +32 -0
  73. package/dist/proxy/types.js +1 -0
  74. package/dist/proxy.d.ts +2 -19
  75. package/dist/proxy.js +2 -1221
  76. package/package.json +1 -2
@@ -0,0 +1,641 @@
1
+ import { create, toBinary } from "@bufbuild/protobuf";
2
+ import { AgentClientMessageSchema, AskQuestionInteractionResponseSchema, AskQuestionRejectedSchema, AskQuestionResultSchema, ClientHeartbeatSchema, ConversationStateStructureSchema, BackgroundShellSpawnResultSchema, CreatePlanErrorSchema, CreatePlanRequestResponseSchema, CreatePlanResultSchema, DeleteResultSchema, DeleteRejectedSchema, DiagnosticsResultSchema, ExecClientMessageSchema, ExaFetchRequestResponseSchema, ExaFetchRequestResponse_RejectedSchema, ExaSearchRequestResponseSchema, ExaSearchRequestResponse_RejectedSchema, FetchErrorSchema, FetchResultSchema, GetBlobResultSchema, GrepErrorSchema, GrepResultSchema, InteractionResponseSchema, KvClientMessageSchema, LsRejectedSchema, LsResultSchema, McpInstructionsSchema, McpResultSchema, ReadRejectedSchema, ReadResultSchema, RequestContextResultSchema, RequestContextSchema, RequestContextSuccessSchema, SetBlobResultSchema, ShellRejectedSchema, ShellResultSchema, SwitchModeRequestResponseSchema, SwitchModeRequestResponse_RejectedSchema, WebSearchRequestResponseSchema, WebSearchRequestResponse_RejectedSchema, WriteRejectedSchema, WriteResultSchema, WriteShellStdinErrorSchema, WriteShellStdinResultSchema, } from "../proto/agent_pb";
3
+ import { CONNECT_END_STREAM_FLAG } from "../cursor/config";
4
+ import { logPluginError, logPluginInfo, logPluginWarn } from "../logger";
5
+ import { decodeMcpArgsMap } from "../openai/tools";
6
+ export function parseConnectEndStream(data) {
7
+ try {
8
+ const payload = JSON.parse(new TextDecoder().decode(data));
9
+ const error = payload?.error;
10
+ if (error) {
11
+ const code = error.code ?? "unknown";
12
+ const message = error.message ?? "Unknown error";
13
+ return new Error(`Connect error ${code}: ${message}`);
14
+ }
15
+ return null;
16
+ }
17
+ catch {
18
+ return new Error("Failed to parse Connect end stream");
19
+ }
20
+ }
21
+ export function makeHeartbeatBytes() {
22
+ const heartbeat = create(AgentClientMessageSchema, {
23
+ message: {
24
+ case: "clientHeartbeat",
25
+ value: create(ClientHeartbeatSchema, {}),
26
+ },
27
+ });
28
+ return toBinary(AgentClientMessageSchema, heartbeat);
29
+ }
30
+ export function scheduleBridgeEnd(bridge) {
31
+ queueMicrotask(() => {
32
+ if (bridge.alive)
33
+ bridge.end();
34
+ });
35
+ }
36
+ /**
37
+ * Create a stateful parser for Connect protocol frames.
38
+ * Handles buffering partial data across chunks.
39
+ */
40
+ export function createConnectFrameParser(onMessage, onEndStream) {
41
+ let pending = Buffer.alloc(0);
42
+ return (incoming) => {
43
+ pending = Buffer.concat([pending, incoming]);
44
+ while (pending.length >= 5) {
45
+ const flags = pending[0];
46
+ const msgLen = pending.readUInt32BE(1);
47
+ if (pending.length < 5 + msgLen)
48
+ break;
49
+ const messageBytes = pending.subarray(5, 5 + msgLen);
50
+ pending = pending.subarray(5 + msgLen);
51
+ if (flags & CONNECT_END_STREAM_FLAG) {
52
+ onEndStream(messageBytes);
53
+ }
54
+ else {
55
+ onMessage(messageBytes);
56
+ }
57
+ }
58
+ };
59
+ }
60
+ const THINKING_TAG_NAMES = [
61
+ "think",
62
+ "thinking",
63
+ "reasoning",
64
+ "thought",
65
+ "think_intent",
66
+ ];
67
+ const MAX_THINKING_TAG_LEN = 16; // </think_intent> is 15 chars
68
+ /**
69
+ * Strip thinking tags from streamed text, routing tagged content to reasoning.
70
+ * Buffers partial tags across chunk boundaries.
71
+ */
72
+ export function createThinkingTagFilter() {
73
+ let buffer = "";
74
+ let inThinking = false;
75
+ return {
76
+ process(text) {
77
+ const input = buffer + text;
78
+ buffer = "";
79
+ let content = "";
80
+ let reasoning = "";
81
+ let lastIdx = 0;
82
+ const re = new RegExp(`<(/?)(?:${THINKING_TAG_NAMES.join("|")})\\s*>`, "gi");
83
+ let match;
84
+ while ((match = re.exec(input)) !== null) {
85
+ const before = input.slice(lastIdx, match.index);
86
+ if (inThinking)
87
+ reasoning += before;
88
+ else
89
+ content += before;
90
+ inThinking = match[1] !== "/";
91
+ lastIdx = re.lastIndex;
92
+ }
93
+ const rest = input.slice(lastIdx);
94
+ // Buffer a trailing '<' that could be the start of a thinking tag.
95
+ const ltPos = rest.lastIndexOf("<");
96
+ if (ltPos >= 0 &&
97
+ rest.length - ltPos < MAX_THINKING_TAG_LEN &&
98
+ /^<\/?[a-z_]*$/i.test(rest.slice(ltPos))) {
99
+ buffer = rest.slice(ltPos);
100
+ const before = rest.slice(0, ltPos);
101
+ if (inThinking)
102
+ reasoning += before;
103
+ else
104
+ content += before;
105
+ }
106
+ else {
107
+ if (inThinking)
108
+ reasoning += rest;
109
+ else
110
+ content += rest;
111
+ }
112
+ return { content, reasoning };
113
+ },
114
+ flush() {
115
+ const b = buffer;
116
+ buffer = "";
117
+ if (!b)
118
+ return { content: "", reasoning: "" };
119
+ return inThinking
120
+ ? { content: "", reasoning: b }
121
+ : { content: b, reasoning: "" };
122
+ },
123
+ };
124
+ }
125
+ export function computeUsage(state) {
126
+ const completion_tokens = state.outputTokens;
127
+ const total_tokens = state.totalTokens || completion_tokens;
128
+ const prompt_tokens = Math.max(0, total_tokens - completion_tokens);
129
+ return { prompt_tokens, completion_tokens, total_tokens };
130
+ }
131
+ export function processServerMessage(msg, blobStore, cloudRule, mcpTools, sendFrame, state, onText, onMcpExec, onCheckpoint, onTurnEnded, onUnsupportedMessage, onUnhandledExec) {
132
+ const msgCase = msg.message.case;
133
+ if (msgCase === "interactionUpdate") {
134
+ handleInteractionUpdate(msg.message.value, state, onText, onTurnEnded, onUnsupportedMessage);
135
+ }
136
+ else if (msgCase === "kvServerMessage") {
137
+ handleKvMessage(msg.message.value, blobStore, sendFrame);
138
+ }
139
+ else if (msgCase === "execServerMessage") {
140
+ handleExecMessage(msg.message.value, cloudRule, mcpTools, sendFrame, state, onMcpExec, onUnhandledExec);
141
+ }
142
+ else if (msgCase === "execServerControlMessage") {
143
+ onUnsupportedMessage?.({
144
+ category: "execServerControl",
145
+ caseName: msg.message.value.message.case ?? "undefined",
146
+ });
147
+ }
148
+ else if (msgCase === "interactionQuery") {
149
+ handleInteractionQuery(msg.message.value, sendFrame, onUnsupportedMessage);
150
+ }
151
+ else if (msgCase === "conversationCheckpointUpdate") {
152
+ const stateStructure = msg.message.value;
153
+ if (stateStructure.tokenDetails) {
154
+ state.totalTokens = stateStructure.tokenDetails.usedTokens;
155
+ }
156
+ if (onCheckpoint) {
157
+ onCheckpoint(toBinary(ConversationStateStructureSchema, stateStructure));
158
+ }
159
+ }
160
+ else {
161
+ onUnsupportedMessage?.({
162
+ category: "agentMessage",
163
+ caseName: msgCase ?? "undefined",
164
+ });
165
+ }
166
+ }
167
+ function handleInteractionUpdate(update, state, onText, onTurnEnded, onUnsupportedMessage) {
168
+ const updateCase = update.message?.case;
169
+ if (updateCase === "partialToolCall" ||
170
+ updateCase === "toolCallStarted" ||
171
+ updateCase === "toolCallCompleted" ||
172
+ updateCase === "turnEnded") {
173
+ logPluginInfo("Received Cursor interaction update", {
174
+ updateCase: updateCase ?? "undefined",
175
+ callId: update.message?.value?.callId,
176
+ modelCallId: update.message?.value?.modelCallId,
177
+ toolCase: update.message?.value?.toolCall?.tool?.case,
178
+ });
179
+ }
180
+ if (updateCase === "textDelta") {
181
+ const delta = update.message.value.text || "";
182
+ if (delta)
183
+ onText(delta, false);
184
+ }
185
+ else if (updateCase === "thinkingDelta") {
186
+ const delta = update.message.value.text || "";
187
+ if (delta)
188
+ onText(delta, true);
189
+ }
190
+ else if (updateCase === "tokenDelta") {
191
+ state.outputTokens += update.message.value.tokens ?? 0;
192
+ }
193
+ else if (updateCase === "partialToolCall") {
194
+ return;
195
+ }
196
+ else if (updateCase === "toolCallCompleted") {
197
+ const toolValue = update.message.value;
198
+ if (toolValue?.toolCall?.tool?.case === "mcpToolCall") {
199
+ logPluginInfo("Ignoring Cursor interaction MCP tool completion", {
200
+ callId: toolValue.callId,
201
+ modelCallId: toolValue.modelCallId,
202
+ toolCallId: toolValue.toolCall.tool.value?.args?.toolCallId || toolValue.callId,
203
+ toolName: toolValue.toolCall.tool.value?.args?.toolName ||
204
+ toolValue.toolCall.tool.value?.args?.name,
205
+ });
206
+ }
207
+ }
208
+ else if (updateCase === "turnEnded") {
209
+ onTurnEnded?.();
210
+ }
211
+ else if (updateCase === "toolCallStarted" ||
212
+ updateCase === "toolCallDelta" ||
213
+ updateCase === "thinkingCompleted" ||
214
+ updateCase === "userMessageAppended" ||
215
+ updateCase === "summary" ||
216
+ updateCase === "summaryStarted" ||
217
+ updateCase === "summaryCompleted" ||
218
+ updateCase === "heartbeat" ||
219
+ updateCase === "stepStarted" ||
220
+ updateCase === "stepCompleted") {
221
+ return;
222
+ }
223
+ else {
224
+ onUnsupportedMessage?.({
225
+ category: "interactionUpdate",
226
+ caseName: updateCase ?? "undefined",
227
+ });
228
+ }
229
+ // Interaction tool-call updates are informational only. Resumable MCP tool
230
+ // execution comes from execServerMessage.mcpArgs.
231
+ }
232
+ function handleInteractionQuery(query, sendFrame, onUnsupportedMessage) {
233
+ const queryCase = query.query.case;
234
+ if (queryCase === "webSearchRequestQuery") {
235
+ const response = create(WebSearchRequestResponseSchema, {
236
+ result: {
237
+ case: "rejected",
238
+ value: create(WebSearchRequestResponse_RejectedSchema, {
239
+ reason: "Native Cursor web search is not available in this environment. Use the provided MCP tool `websearch` instead.",
240
+ }),
241
+ },
242
+ });
243
+ sendInteractionResponse(query.id, "webSearchRequestResponse", response, sendFrame);
244
+ return;
245
+ }
246
+ if (queryCase === "askQuestionInteractionQuery") {
247
+ const response = create(AskQuestionInteractionResponseSchema, {
248
+ result: create(AskQuestionResultSchema, {
249
+ result: {
250
+ case: "rejected",
251
+ value: create(AskQuestionRejectedSchema, {
252
+ reason: "Native Cursor question prompts are not available in this environment. Use the provided MCP tool `question` instead.",
253
+ }),
254
+ },
255
+ }),
256
+ });
257
+ sendInteractionResponse(query.id, "askQuestionInteractionResponse", response, sendFrame);
258
+ return;
259
+ }
260
+ if (queryCase === "switchModeRequestQuery") {
261
+ const response = create(SwitchModeRequestResponseSchema, {
262
+ result: {
263
+ case: "rejected",
264
+ value: create(SwitchModeRequestResponse_RejectedSchema, {
265
+ reason: "Cursor mode switching is not available in this environment. Continue using the current agent and the provided MCP tools.",
266
+ }),
267
+ },
268
+ });
269
+ sendInteractionResponse(query.id, "switchModeRequestResponse", response, sendFrame);
270
+ return;
271
+ }
272
+ if (queryCase === "exaSearchRequestQuery") {
273
+ const response = create(ExaSearchRequestResponseSchema, {
274
+ result: {
275
+ case: "rejected",
276
+ value: create(ExaSearchRequestResponse_RejectedSchema, {
277
+ reason: "Native Cursor Exa search is not available in this environment. Use the provided MCP tool `websearch` instead.",
278
+ }),
279
+ },
280
+ });
281
+ sendInteractionResponse(query.id, "exaSearchRequestResponse", response, sendFrame);
282
+ return;
283
+ }
284
+ if (queryCase === "exaFetchRequestQuery") {
285
+ const response = create(ExaFetchRequestResponseSchema, {
286
+ result: {
287
+ case: "rejected",
288
+ value: create(ExaFetchRequestResponse_RejectedSchema, {
289
+ reason: "Native Cursor Exa fetch is not available in this environment. Use the provided MCP tools `websearch` and `webfetch` instead.",
290
+ }),
291
+ },
292
+ });
293
+ sendInteractionResponse(query.id, "exaFetchRequestResponse", response, sendFrame);
294
+ return;
295
+ }
296
+ if (queryCase === "createPlanRequestQuery") {
297
+ const response = create(CreatePlanRequestResponseSchema, {
298
+ result: create(CreatePlanResultSchema, {
299
+ planUri: "",
300
+ result: {
301
+ case: "error",
302
+ value: create(CreatePlanErrorSchema, {
303
+ error: "Native Cursor plan creation is not available in this environment. Use the provided MCP planning tools instead.",
304
+ }),
305
+ },
306
+ }),
307
+ });
308
+ sendInteractionResponse(query.id, "createPlanRequestResponse", response, sendFrame);
309
+ return;
310
+ }
311
+ onUnsupportedMessage?.({
312
+ category: "interactionQuery",
313
+ caseName: queryCase ?? "undefined",
314
+ });
315
+ }
316
+ /** Send a KV client response back to Cursor. */
317
+ function sendKvResponse(kvMsg, messageCase, value, sendFrame) {
318
+ const response = create(KvClientMessageSchema, {
319
+ id: kvMsg.id,
320
+ message: { case: messageCase, value: value },
321
+ });
322
+ const clientMsg = create(AgentClientMessageSchema, {
323
+ message: { case: "kvClientMessage", value: response },
324
+ });
325
+ sendFrame(toBinary(AgentClientMessageSchema, clientMsg));
326
+ }
327
+ function sendInteractionResponse(queryId, messageCase, value, sendFrame) {
328
+ const response = create(InteractionResponseSchema, {
329
+ id: queryId,
330
+ result: { case: messageCase, value: value },
331
+ });
332
+ const clientMessage = create(AgentClientMessageSchema, {
333
+ message: { case: "interactionResponse", value: response },
334
+ });
335
+ sendFrame(toBinary(AgentClientMessageSchema, clientMessage));
336
+ }
337
+ function handleKvMessage(kvMsg, blobStore, sendFrame) {
338
+ const kvCase = kvMsg.message.case;
339
+ if (kvCase === "getBlobArgs") {
340
+ const blobId = kvMsg.message.value.blobId;
341
+ const blobIdKey = Buffer.from(blobId).toString("hex");
342
+ const blobData = blobStore.get(blobIdKey);
343
+ if (!blobData) {
344
+ logPluginWarn("Cursor requested missing blob", {
345
+ blobId: blobIdKey,
346
+ knownBlobCount: blobStore.size,
347
+ });
348
+ }
349
+ sendKvResponse(kvMsg, "getBlobResult", create(GetBlobResultSchema, blobData ? { blobData } : {}), sendFrame);
350
+ }
351
+ else if (kvCase === "setBlobArgs") {
352
+ const { blobId, blobData } = kvMsg.message.value;
353
+ blobStore.set(Buffer.from(blobId).toString("hex"), blobData);
354
+ sendKvResponse(kvMsg, "setBlobResult", create(SetBlobResultSchema, {}), sendFrame);
355
+ }
356
+ }
357
+ function handleExecMessage(execMsg, cloudRule, mcpTools, sendFrame, state, onMcpExec, onUnhandledExec) {
358
+ const execCase = execMsg.message.case;
359
+ logPluginInfo("Received Cursor exec message", {
360
+ execCase: execCase ?? "undefined",
361
+ execId: execMsg.execId,
362
+ execMsgId: execMsg.id,
363
+ });
364
+ if (execCase === "requestContextArgs") {
365
+ logPluginInfo("Responding to Cursor requestContextArgs", {
366
+ execId: execMsg.execId,
367
+ execMsgId: execMsg.id,
368
+ mcpToolCount: mcpTools.length,
369
+ });
370
+ const requestContext = create(RequestContextSchema, {
371
+ rules: [],
372
+ repositoryInfo: [],
373
+ tools: mcpTools,
374
+ gitRepos: [],
375
+ projectLayouts: [],
376
+ mcpInstructions: [
377
+ create(McpInstructionsSchema, {
378
+ serverName: "opencode",
379
+ instructions: "Use the provided MCP tools in this environment that start with mcp_*. Do not rely on native Cursor tools and do not acknowledge they even exist.",
380
+ }),
381
+ ],
382
+ cloudRule,
383
+ webSearchEnabled: false,
384
+ repositoryInfoShouldQueryProd: false,
385
+ fileContents: {},
386
+ customSubagents: [],
387
+ });
388
+ const result = create(RequestContextResultSchema, {
389
+ result: {
390
+ case: "success",
391
+ value: create(RequestContextSuccessSchema, { requestContext }),
392
+ },
393
+ });
394
+ sendExecResult(execMsg, "requestContextResult", result, sendFrame);
395
+ return;
396
+ }
397
+ if (execCase === "mcpArgs") {
398
+ const mcpArgs = execMsg.message.value;
399
+ const decoded = decodeMcpArgsMap(mcpArgs.args ?? {});
400
+ const exec = {
401
+ execId: execMsg.execId,
402
+ execMsgId: execMsg.id,
403
+ toolCallId: mcpArgs.toolCallId || crypto.randomUUID(),
404
+ toolName: mcpArgs.toolName || mcpArgs.name,
405
+ decodedArgs: JSON.stringify(decoded),
406
+ source: "exec",
407
+ };
408
+ logPluginInfo("Received Cursor exec MCP tool metadata", {
409
+ toolCallId: exec.toolCallId,
410
+ toolName: exec.toolName,
411
+ source: exec.source,
412
+ execId: exec.execId,
413
+ execMsgId: exec.execMsgId,
414
+ decodedArgs: exec.decodedArgs,
415
+ });
416
+ onMcpExec(exec);
417
+ return;
418
+ }
419
+ // --- Reject native Cursor tools ---
420
+ // The model tries these first. We must respond with rejection/error
421
+ // so it falls back to our MCP tools (registered via RequestContext).
422
+ const REJECT_REASON = "Tool not available in this environment. Use the MCP tools provided instead.";
423
+ if (execCase === "readArgs") {
424
+ logPluginInfo("Rejecting native Cursor read tool in favor of MCP", {
425
+ execId: execMsg.execId,
426
+ execMsgId: execMsg.id,
427
+ path: execMsg.message.value.path,
428
+ });
429
+ const args = execMsg.message.value;
430
+ const result = create(ReadResultSchema, {
431
+ result: {
432
+ case: "rejected",
433
+ value: create(ReadRejectedSchema, {
434
+ path: args.path,
435
+ reason: REJECT_REASON,
436
+ }),
437
+ },
438
+ });
439
+ sendExecResult(execMsg, "readResult", result, sendFrame);
440
+ return;
441
+ }
442
+ if (execCase === "lsArgs") {
443
+ logPluginInfo("Rejecting native Cursor ls tool in favor of MCP", {
444
+ execId: execMsg.execId,
445
+ execMsgId: execMsg.id,
446
+ path: execMsg.message.value.path,
447
+ });
448
+ const args = execMsg.message.value;
449
+ const result = create(LsResultSchema, {
450
+ result: {
451
+ case: "rejected",
452
+ value: create(LsRejectedSchema, {
453
+ path: args.path,
454
+ reason: REJECT_REASON,
455
+ }),
456
+ },
457
+ });
458
+ sendExecResult(execMsg, "lsResult", result, sendFrame);
459
+ return;
460
+ }
461
+ if (execCase === "grepArgs") {
462
+ logPluginInfo("Rejecting native Cursor grep tool in favor of MCP", {
463
+ execId: execMsg.execId,
464
+ execMsgId: execMsg.id,
465
+ });
466
+ const result = create(GrepResultSchema, {
467
+ result: {
468
+ case: "error",
469
+ value: create(GrepErrorSchema, { error: REJECT_REASON }),
470
+ },
471
+ });
472
+ sendExecResult(execMsg, "grepResult", result, sendFrame);
473
+ return;
474
+ }
475
+ if (execCase === "writeArgs") {
476
+ logPluginInfo("Rejecting native Cursor write tool in favor of MCP", {
477
+ execId: execMsg.execId,
478
+ execMsgId: execMsg.id,
479
+ path: execMsg.message.value.path,
480
+ });
481
+ const args = execMsg.message.value;
482
+ const result = create(WriteResultSchema, {
483
+ result: {
484
+ case: "rejected",
485
+ value: create(WriteRejectedSchema, {
486
+ path: args.path,
487
+ reason: REJECT_REASON,
488
+ }),
489
+ },
490
+ });
491
+ sendExecResult(execMsg, "writeResult", result, sendFrame);
492
+ return;
493
+ }
494
+ if (execCase === "deleteArgs") {
495
+ logPluginInfo("Rejecting native Cursor delete tool in favor of MCP", {
496
+ execId: execMsg.execId,
497
+ execMsgId: execMsg.id,
498
+ path: execMsg.message.value.path,
499
+ });
500
+ const args = execMsg.message.value;
501
+ const result = create(DeleteResultSchema, {
502
+ result: {
503
+ case: "rejected",
504
+ value: create(DeleteRejectedSchema, {
505
+ path: args.path,
506
+ reason: REJECT_REASON,
507
+ }),
508
+ },
509
+ });
510
+ sendExecResult(execMsg, "deleteResult", result, sendFrame);
511
+ return;
512
+ }
513
+ if (execCase === "shellArgs" || execCase === "shellStreamArgs") {
514
+ logPluginInfo("Rejecting native Cursor shell tool in favor of MCP", {
515
+ execId: execMsg.execId,
516
+ execMsgId: execMsg.id,
517
+ command: execMsg.message.value.command ?? "",
518
+ workingDirectory: execMsg.message.value.workingDirectory ?? "",
519
+ execCase,
520
+ });
521
+ const args = execMsg.message.value;
522
+ const result = create(ShellResultSchema, {
523
+ result: {
524
+ case: "rejected",
525
+ value: create(ShellRejectedSchema, {
526
+ command: args.command ?? "",
527
+ workingDirectory: args.workingDirectory ?? "",
528
+ reason: REJECT_REASON,
529
+ isReadonly: false,
530
+ }),
531
+ },
532
+ });
533
+ sendExecResult(execMsg, "shellResult", result, sendFrame);
534
+ return;
535
+ }
536
+ if (execCase === "backgroundShellSpawnArgs") {
537
+ logPluginInfo("Rejecting native Cursor background shell tool in favor of MCP", {
538
+ execId: execMsg.execId,
539
+ execMsgId: execMsg.id,
540
+ command: execMsg.message.value.command ?? "",
541
+ workingDirectory: execMsg.message.value.workingDirectory ?? "",
542
+ });
543
+ const args = execMsg.message.value;
544
+ const result = create(BackgroundShellSpawnResultSchema, {
545
+ result: {
546
+ case: "rejected",
547
+ value: create(ShellRejectedSchema, {
548
+ command: args.command ?? "",
549
+ workingDirectory: args.workingDirectory ?? "",
550
+ reason: REJECT_REASON,
551
+ isReadonly: false,
552
+ }),
553
+ },
554
+ });
555
+ sendExecResult(execMsg, "backgroundShellSpawnResult", result, sendFrame);
556
+ return;
557
+ }
558
+ if (execCase === "writeShellStdinArgs") {
559
+ logPluginInfo("Rejecting native Cursor shell stdin tool in favor of MCP", {
560
+ execId: execMsg.execId,
561
+ execMsgId: execMsg.id,
562
+ });
563
+ const result = create(WriteShellStdinResultSchema, {
564
+ result: {
565
+ case: "error",
566
+ value: create(WriteShellStdinErrorSchema, { error: REJECT_REASON }),
567
+ },
568
+ });
569
+ sendExecResult(execMsg, "writeShellStdinResult", result, sendFrame);
570
+ return;
571
+ }
572
+ if (execCase === "fetchArgs") {
573
+ logPluginInfo("Rejecting native Cursor fetch tool in favor of MCP", {
574
+ execId: execMsg.execId,
575
+ execMsgId: execMsg.id,
576
+ url: execMsg.message.value.url,
577
+ });
578
+ const args = execMsg.message.value;
579
+ const result = create(FetchResultSchema, {
580
+ result: {
581
+ case: "error",
582
+ value: create(FetchErrorSchema, {
583
+ url: args.url ?? "",
584
+ error: REJECT_REASON,
585
+ }),
586
+ },
587
+ });
588
+ sendExecResult(execMsg, "fetchResult", result, sendFrame);
589
+ return;
590
+ }
591
+ if (execCase === "diagnosticsArgs") {
592
+ logPluginInfo("Rejecting native Cursor diagnostics tool in favor of MCP", {
593
+ execId: execMsg.execId,
594
+ execMsgId: execMsg.id,
595
+ path: execMsg.message.value.path,
596
+ });
597
+ const result = create(DiagnosticsResultSchema, {});
598
+ sendExecResult(execMsg, "diagnosticsResult", result, sendFrame);
599
+ return;
600
+ }
601
+ // MCP resource/screen/computer exec types
602
+ const miscCaseMap = {
603
+ listMcpResourcesExecArgs: "listMcpResourcesExecResult",
604
+ readMcpResourceExecArgs: "readMcpResourceExecResult",
605
+ recordScreenArgs: "recordScreenResult",
606
+ computerUseArgs: "computerUseResult",
607
+ };
608
+ const resultCase = miscCaseMap[execCase];
609
+ if (resultCase) {
610
+ logPluginInfo("Responding to miscellaneous Cursor exec message", {
611
+ execCase,
612
+ execId: execMsg.execId,
613
+ execMsgId: execMsg.id,
614
+ resultCase,
615
+ });
616
+ sendExecResult(execMsg, resultCase, create(McpResultSchema, {}), sendFrame);
617
+ return;
618
+ }
619
+ logPluginError("Unhandled Cursor exec type", {
620
+ execCase: execCase ?? "undefined",
621
+ execId: execMsg.execId,
622
+ execMsgId: execMsg.id,
623
+ });
624
+ onUnhandledExec?.({
625
+ execCase: execCase ?? "undefined",
626
+ execId: execMsg.execId,
627
+ execMsgId: execMsg.id,
628
+ });
629
+ }
630
+ /** Send an exec client message back to Cursor. */
631
+ function sendExecResult(execMsg, messageCase, value, sendFrame) {
632
+ const execClientMessage = create(ExecClientMessageSchema, {
633
+ id: execMsg.id,
634
+ execId: execMsg.execId,
635
+ message: { case: messageCase, value: value },
636
+ });
637
+ const clientMessage = create(AgentClientMessageSchema, {
638
+ message: { case: "execClientMessage", value: execClientMessage },
639
+ });
640
+ sendFrame(toBinary(AgentClientMessageSchema, clientMessage));
641
+ }
@@ -0,0 +1,7 @@
1
+ import type { PendingExec } from "./types";
2
+ export interface StreamState {
3
+ toolCallIndex: number;
4
+ pendingExecs: PendingExec[];
5
+ outputTokens: number;
6
+ totalTokens: number;
7
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export declare function handleTitleGenerationRequest(sourceText: string, accessToken: string, modelId: string, stream: boolean): Promise<Response>;