@playwo/opencode-cursor-oauth 0.0.0-dev.e795e5ffd849 → 0.0.0-dev.eb79b6283515
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/dist/auth.js +1 -2
- package/dist/constants.d.ts +2 -0
- package/dist/constants.js +2 -0
- package/dist/cursor/bidi-session.d.ts +12 -0
- package/dist/cursor/bidi-session.js +164 -0
- package/dist/cursor/config.d.ts +4 -0
- package/dist/cursor/config.js +4 -0
- package/dist/cursor/connect-framing.d.ts +10 -0
- package/dist/cursor/connect-framing.js +80 -0
- package/dist/cursor/headers.d.ts +6 -0
- package/dist/cursor/headers.js +16 -0
- package/dist/cursor/index.d.ts +5 -0
- package/dist/cursor/index.js +5 -0
- package/dist/cursor/unary-rpc.d.ts +12 -0
- package/dist/cursor/unary-rpc.js +124 -0
- package/dist/index.d.ts +2 -14
- package/dist/index.js +2 -297
- package/dist/logger.js +7 -2
- package/dist/models.js +1 -23
- package/dist/openai/index.d.ts +3 -0
- package/dist/openai/index.js +3 -0
- package/dist/openai/messages.d.ts +39 -0
- package/dist/openai/messages.js +228 -0
- package/dist/openai/tools.d.ts +7 -0
- package/dist/openai/tools.js +58 -0
- package/dist/openai/types.d.ts +41 -0
- package/dist/openai/types.js +1 -0
- package/dist/plugin/cursor-auth-plugin.d.ts +3 -0
- package/dist/plugin/cursor-auth-plugin.js +139 -0
- package/dist/proto/agent_pb.js +637 -319
- package/dist/provider/index.d.ts +2 -0
- package/dist/provider/index.js +2 -0
- package/dist/provider/model-cost.d.ts +9 -0
- package/dist/provider/model-cost.js +206 -0
- package/dist/provider/models.d.ts +8 -0
- package/dist/provider/models.js +86 -0
- package/dist/proxy/bridge-non-streaming.d.ts +3 -0
- package/dist/proxy/bridge-non-streaming.js +119 -0
- package/dist/proxy/bridge-session.d.ts +5 -0
- package/dist/proxy/bridge-session.js +11 -0
- package/dist/proxy/bridge-streaming.d.ts +5 -0
- package/dist/proxy/bridge-streaming.js +317 -0
- package/dist/proxy/bridge.d.ts +3 -0
- package/dist/proxy/bridge.js +3 -0
- package/dist/proxy/chat-completion.d.ts +2 -0
- package/dist/proxy/chat-completion.js +114 -0
- package/dist/proxy/conversation-meta.d.ts +12 -0
- package/dist/proxy/conversation-meta.js +1 -0
- package/dist/proxy/conversation-state.d.ts +35 -0
- package/dist/proxy/conversation-state.js +95 -0
- package/dist/proxy/cursor-request.d.ts +6 -0
- package/dist/proxy/cursor-request.js +104 -0
- package/dist/proxy/index.d.ts +12 -0
- package/dist/proxy/index.js +12 -0
- package/dist/proxy/server.d.ts +6 -0
- package/dist/proxy/server.js +107 -0
- package/dist/proxy/sse.d.ts +5 -0
- package/dist/proxy/sse.js +5 -0
- package/dist/proxy/state-sync.d.ts +2 -0
- package/dist/proxy/state-sync.js +17 -0
- package/dist/proxy/stream-dispatch.d.ts +42 -0
- package/dist/proxy/stream-dispatch.js +619 -0
- package/dist/proxy/stream-state.d.ts +9 -0
- package/dist/proxy/stream-state.js +1 -0
- package/dist/proxy/title.d.ts +1 -0
- package/dist/proxy/title.js +103 -0
- package/dist/proxy/types.d.ts +27 -0
- package/dist/proxy/types.js +1 -0
- package/dist/proxy.d.ts +2 -20
- package/dist/proxy.js +2 -1416
- package/package.json +1 -1
|
@@ -0,0 +1,619 @@
|
|
|
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, 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, 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
|
+
function getPendingExecKey(exec) {
|
|
132
|
+
return exec.toolCallId || `${exec.toolName}:${exec.decodedArgs}`;
|
|
133
|
+
}
|
|
134
|
+
function replacePendingExec(state, exec) {
|
|
135
|
+
const execKey = getPendingExecKey(exec);
|
|
136
|
+
const existingIndex = state.pendingExecs.findIndex((candidate) => getPendingExecKey(candidate) === execKey);
|
|
137
|
+
if (existingIndex >= 0) {
|
|
138
|
+
state.pendingExecs[existingIndex] = exec;
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
state.pendingExecs.push(exec);
|
|
142
|
+
}
|
|
143
|
+
function hasUsableDecodedArgs(decodedArgs) {
|
|
144
|
+
const trimmed = decodedArgs.trim();
|
|
145
|
+
return trimmed !== "" && trimmed !== "{}";
|
|
146
|
+
}
|
|
147
|
+
function mergePendingExec(existing, incoming) {
|
|
148
|
+
const incomingHasExecMetadata = incoming.execMsgId !== 0;
|
|
149
|
+
const existingHasExecMetadata = existing.execMsgId !== 0;
|
|
150
|
+
return {
|
|
151
|
+
execId: incomingHasExecMetadata || !existing.execId ? incoming.execId : existing.execId,
|
|
152
|
+
execMsgId: incomingHasExecMetadata || !existingHasExecMetadata
|
|
153
|
+
? incoming.execMsgId
|
|
154
|
+
: existing.execMsgId,
|
|
155
|
+
toolCallId: existing.toolCallId || incoming.toolCallId,
|
|
156
|
+
toolName: incoming.toolName && incoming.toolName !== "unknown_mcp_tool"
|
|
157
|
+
? incoming.toolName
|
|
158
|
+
: existing.toolName,
|
|
159
|
+
decodedArgs: hasUsableDecodedArgs(incoming.decodedArgs)
|
|
160
|
+
? incoming.decodedArgs
|
|
161
|
+
: existing.decodedArgs,
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
function emitPendingExec(exec, state, onMcpExec) {
|
|
165
|
+
const execKey = getPendingExecKey(exec);
|
|
166
|
+
const existing = state.pendingExecs.find((candidate) => getPendingExecKey(candidate) === execKey);
|
|
167
|
+
const nextExec = existing ? mergePendingExec(existing, exec) : exec;
|
|
168
|
+
if (state.emittedToolCallIds.has(execKey)) {
|
|
169
|
+
replacePendingExec(state, nextExec);
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
state.emittedToolCallIds.add(execKey);
|
|
173
|
+
replacePendingExec(state, nextExec);
|
|
174
|
+
onMcpExec(nextExec);
|
|
175
|
+
}
|
|
176
|
+
export function processServerMessage(msg, blobStore, mcpTools, sendFrame, state, onText, onMcpExec, onCheckpoint, onTurnEnded, onUnsupportedMessage, onUnhandledExec) {
|
|
177
|
+
const msgCase = msg.message.case;
|
|
178
|
+
if (msgCase === "interactionUpdate") {
|
|
179
|
+
handleInteractionUpdate(msg.message.value, state, onText, onMcpExec, onTurnEnded, onUnsupportedMessage);
|
|
180
|
+
}
|
|
181
|
+
else if (msgCase === "kvServerMessage") {
|
|
182
|
+
handleKvMessage(msg.message.value, blobStore, sendFrame);
|
|
183
|
+
}
|
|
184
|
+
else if (msgCase === "execServerMessage") {
|
|
185
|
+
handleExecMessage(msg.message.value, mcpTools, sendFrame, onMcpExec, onUnhandledExec);
|
|
186
|
+
}
|
|
187
|
+
else if (msgCase === "execServerControlMessage") {
|
|
188
|
+
onUnsupportedMessage?.({
|
|
189
|
+
category: "execServerControl",
|
|
190
|
+
caseName: msg.message.value.message.case ?? "undefined",
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
else if (msgCase === "interactionQuery") {
|
|
194
|
+
handleInteractionQuery(msg.message.value, sendFrame, onUnsupportedMessage);
|
|
195
|
+
}
|
|
196
|
+
else if (msgCase === "conversationCheckpointUpdate") {
|
|
197
|
+
const stateStructure = msg.message.value;
|
|
198
|
+
if (stateStructure.tokenDetails) {
|
|
199
|
+
state.totalTokens = stateStructure.tokenDetails.usedTokens;
|
|
200
|
+
}
|
|
201
|
+
if (onCheckpoint) {
|
|
202
|
+
onCheckpoint(toBinary(ConversationStateStructureSchema, stateStructure));
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
else {
|
|
206
|
+
onUnsupportedMessage?.({
|
|
207
|
+
category: "agentMessage",
|
|
208
|
+
caseName: msgCase ?? "undefined",
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
function handleInteractionUpdate(update, state, onText, onMcpExec, onTurnEnded, onUnsupportedMessage) {
|
|
213
|
+
const updateCase = update.message?.case;
|
|
214
|
+
if (updateCase === "textDelta") {
|
|
215
|
+
const delta = update.message.value.text || "";
|
|
216
|
+
if (delta)
|
|
217
|
+
onText(delta, false);
|
|
218
|
+
}
|
|
219
|
+
else if (updateCase === "thinkingDelta") {
|
|
220
|
+
const delta = update.message.value.text || "";
|
|
221
|
+
if (delta)
|
|
222
|
+
onText(delta, true);
|
|
223
|
+
}
|
|
224
|
+
else if (updateCase === "tokenDelta") {
|
|
225
|
+
state.outputTokens += update.message.value.tokens ?? 0;
|
|
226
|
+
}
|
|
227
|
+
else if (updateCase === "partialToolCall") {
|
|
228
|
+
const partial = update.message.value;
|
|
229
|
+
if (partial.callId && partial.argsTextDelta) {
|
|
230
|
+
const existing = state.interactionToolArgsText.get(partial.callId) ?? "";
|
|
231
|
+
state.interactionToolArgsText.set(partial.callId, `${existing}${partial.argsTextDelta}`);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
else if (updateCase === "toolCallCompleted") {
|
|
235
|
+
const exec = decodeInteractionToolCall(update.message.value, state);
|
|
236
|
+
if (exec)
|
|
237
|
+
emitPendingExec(exec, state, onMcpExec);
|
|
238
|
+
}
|
|
239
|
+
else if (updateCase === "turnEnded") {
|
|
240
|
+
onTurnEnded?.();
|
|
241
|
+
}
|
|
242
|
+
else if (updateCase === "toolCallStarted" ||
|
|
243
|
+
updateCase === "toolCallDelta" ||
|
|
244
|
+
updateCase === "thinkingCompleted" ||
|
|
245
|
+
updateCase === "userMessageAppended" ||
|
|
246
|
+
updateCase === "summary" ||
|
|
247
|
+
updateCase === "summaryStarted" ||
|
|
248
|
+
updateCase === "summaryCompleted" ||
|
|
249
|
+
updateCase === "heartbeat" ||
|
|
250
|
+
updateCase === "stepStarted" ||
|
|
251
|
+
updateCase === "stepCompleted") {
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
254
|
+
else {
|
|
255
|
+
onUnsupportedMessage?.({
|
|
256
|
+
category: "interactionUpdate",
|
|
257
|
+
caseName: updateCase ?? "undefined",
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
// toolCallStarted, partialToolCall, toolCallDelta, and non-MCP
|
|
261
|
+
// toolCallCompleted updates are informational only. Actionable MCP tool
|
|
262
|
+
// calls may still appear here on some models, so we surface those, but we
|
|
263
|
+
// do not abort the bridge for native Cursor tool-call progress events.
|
|
264
|
+
}
|
|
265
|
+
function decodeInteractionToolCall(update, state) {
|
|
266
|
+
const callId = update.callId ?? "";
|
|
267
|
+
const toolCase = update.toolCall?.tool?.case;
|
|
268
|
+
if (toolCase !== "mcpToolCall")
|
|
269
|
+
return null;
|
|
270
|
+
const mcpArgs = update.toolCall?.tool?.value?.args;
|
|
271
|
+
if (!mcpArgs)
|
|
272
|
+
return null;
|
|
273
|
+
const toolCallId = mcpArgs.toolCallId || callId || crypto.randomUUID();
|
|
274
|
+
const decodedMap = decodeMcpArgsMap(mcpArgs.args ?? {});
|
|
275
|
+
const partialArgsText = callId
|
|
276
|
+
? state.interactionToolArgsText.get(callId)?.trim()
|
|
277
|
+
: undefined;
|
|
278
|
+
let decodedArgs = "{}";
|
|
279
|
+
if (Object.keys(decodedMap).length > 0) {
|
|
280
|
+
decodedArgs = JSON.stringify(decodedMap);
|
|
281
|
+
}
|
|
282
|
+
else if (partialArgsText) {
|
|
283
|
+
decodedArgs = partialArgsText;
|
|
284
|
+
}
|
|
285
|
+
if (callId)
|
|
286
|
+
state.interactionToolArgsText.delete(callId);
|
|
287
|
+
return {
|
|
288
|
+
execId: callId || toolCallId,
|
|
289
|
+
execMsgId: 0,
|
|
290
|
+
toolCallId,
|
|
291
|
+
toolName: mcpArgs.toolName || mcpArgs.name || "unknown_mcp_tool",
|
|
292
|
+
decodedArgs,
|
|
293
|
+
};
|
|
294
|
+
}
|
|
295
|
+
function handleInteractionQuery(query, sendFrame, onUnsupportedMessage) {
|
|
296
|
+
const queryCase = query.query.case;
|
|
297
|
+
if (queryCase === "webSearchRequestQuery") {
|
|
298
|
+
const response = create(WebSearchRequestResponseSchema, {
|
|
299
|
+
result: {
|
|
300
|
+
case: "rejected",
|
|
301
|
+
value: create(WebSearchRequestResponse_RejectedSchema, {
|
|
302
|
+
reason: "Native Cursor web search is not available in this environment. Use the provided MCP tool `websearch` instead.",
|
|
303
|
+
}),
|
|
304
|
+
},
|
|
305
|
+
});
|
|
306
|
+
sendInteractionResponse(query.id, "webSearchRequestResponse", response, sendFrame);
|
|
307
|
+
return;
|
|
308
|
+
}
|
|
309
|
+
if (queryCase === "askQuestionInteractionQuery") {
|
|
310
|
+
const response = create(AskQuestionInteractionResponseSchema, {
|
|
311
|
+
result: create(AskQuestionResultSchema, {
|
|
312
|
+
result: {
|
|
313
|
+
case: "rejected",
|
|
314
|
+
value: create(AskQuestionRejectedSchema, {
|
|
315
|
+
reason: "Native Cursor question prompts are not available in this environment. Use the provided MCP tool `question` instead.",
|
|
316
|
+
}),
|
|
317
|
+
},
|
|
318
|
+
}),
|
|
319
|
+
});
|
|
320
|
+
sendInteractionResponse(query.id, "askQuestionInteractionResponse", response, sendFrame);
|
|
321
|
+
return;
|
|
322
|
+
}
|
|
323
|
+
if (queryCase === "switchModeRequestQuery") {
|
|
324
|
+
const response = create(SwitchModeRequestResponseSchema, {
|
|
325
|
+
result: {
|
|
326
|
+
case: "rejected",
|
|
327
|
+
value: create(SwitchModeRequestResponse_RejectedSchema, {
|
|
328
|
+
reason: "Cursor mode switching is not available in this environment. Continue using the current agent and the provided MCP tools.",
|
|
329
|
+
}),
|
|
330
|
+
},
|
|
331
|
+
});
|
|
332
|
+
sendInteractionResponse(query.id, "switchModeRequestResponse", response, sendFrame);
|
|
333
|
+
return;
|
|
334
|
+
}
|
|
335
|
+
if (queryCase === "exaSearchRequestQuery") {
|
|
336
|
+
const response = create(ExaSearchRequestResponseSchema, {
|
|
337
|
+
result: {
|
|
338
|
+
case: "rejected",
|
|
339
|
+
value: create(ExaSearchRequestResponse_RejectedSchema, {
|
|
340
|
+
reason: "Native Cursor Exa search is not available in this environment. Use the provided MCP tool `websearch` instead.",
|
|
341
|
+
}),
|
|
342
|
+
},
|
|
343
|
+
});
|
|
344
|
+
sendInteractionResponse(query.id, "exaSearchRequestResponse", response, sendFrame);
|
|
345
|
+
return;
|
|
346
|
+
}
|
|
347
|
+
if (queryCase === "exaFetchRequestQuery") {
|
|
348
|
+
const response = create(ExaFetchRequestResponseSchema, {
|
|
349
|
+
result: {
|
|
350
|
+
case: "rejected",
|
|
351
|
+
value: create(ExaFetchRequestResponse_RejectedSchema, {
|
|
352
|
+
reason: "Native Cursor Exa fetch is not available in this environment. Use the provided MCP tools `websearch` and `webfetch` instead.",
|
|
353
|
+
}),
|
|
354
|
+
},
|
|
355
|
+
});
|
|
356
|
+
sendInteractionResponse(query.id, "exaFetchRequestResponse", response, sendFrame);
|
|
357
|
+
return;
|
|
358
|
+
}
|
|
359
|
+
if (queryCase === "createPlanRequestQuery") {
|
|
360
|
+
const response = create(CreatePlanRequestResponseSchema, {
|
|
361
|
+
result: create(CreatePlanResultSchema, {
|
|
362
|
+
planUri: "",
|
|
363
|
+
result: {
|
|
364
|
+
case: "error",
|
|
365
|
+
value: create(CreatePlanErrorSchema, {
|
|
366
|
+
error: "Native Cursor plan creation is not available in this environment. Use the provided MCP planning tools instead.",
|
|
367
|
+
}),
|
|
368
|
+
},
|
|
369
|
+
}),
|
|
370
|
+
});
|
|
371
|
+
sendInteractionResponse(query.id, "createPlanRequestResponse", response, sendFrame);
|
|
372
|
+
return;
|
|
373
|
+
}
|
|
374
|
+
onUnsupportedMessage?.({
|
|
375
|
+
category: "interactionQuery",
|
|
376
|
+
caseName: queryCase ?? "undefined",
|
|
377
|
+
});
|
|
378
|
+
}
|
|
379
|
+
/** Send a KV client response back to Cursor. */
|
|
380
|
+
function sendKvResponse(kvMsg, messageCase, value, sendFrame) {
|
|
381
|
+
const response = create(KvClientMessageSchema, {
|
|
382
|
+
id: kvMsg.id,
|
|
383
|
+
message: { case: messageCase, value: value },
|
|
384
|
+
});
|
|
385
|
+
const clientMsg = create(AgentClientMessageSchema, {
|
|
386
|
+
message: { case: "kvClientMessage", value: response },
|
|
387
|
+
});
|
|
388
|
+
sendFrame(toBinary(AgentClientMessageSchema, clientMsg));
|
|
389
|
+
}
|
|
390
|
+
function sendInteractionResponse(queryId, messageCase, value, sendFrame) {
|
|
391
|
+
const response = create(InteractionResponseSchema, {
|
|
392
|
+
id: queryId,
|
|
393
|
+
result: { case: messageCase, value: value },
|
|
394
|
+
});
|
|
395
|
+
const clientMessage = create(AgentClientMessageSchema, {
|
|
396
|
+
message: { case: "interactionResponse", value: response },
|
|
397
|
+
});
|
|
398
|
+
sendFrame(toBinary(AgentClientMessageSchema, clientMessage));
|
|
399
|
+
}
|
|
400
|
+
function handleKvMessage(kvMsg, blobStore, sendFrame) {
|
|
401
|
+
const kvCase = kvMsg.message.case;
|
|
402
|
+
if (kvCase === "getBlobArgs") {
|
|
403
|
+
const blobId = kvMsg.message.value.blobId;
|
|
404
|
+
const blobIdKey = Buffer.from(blobId).toString("hex");
|
|
405
|
+
const blobData = blobStore.get(blobIdKey);
|
|
406
|
+
if (!blobData) {
|
|
407
|
+
logPluginWarn("Cursor requested missing blob", {
|
|
408
|
+
blobId: blobIdKey,
|
|
409
|
+
knownBlobCount: blobStore.size,
|
|
410
|
+
});
|
|
411
|
+
}
|
|
412
|
+
sendKvResponse(kvMsg, "getBlobResult", create(GetBlobResultSchema, blobData ? { blobData } : {}), sendFrame);
|
|
413
|
+
}
|
|
414
|
+
else if (kvCase === "setBlobArgs") {
|
|
415
|
+
const { blobId, blobData } = kvMsg.message.value;
|
|
416
|
+
blobStore.set(Buffer.from(blobId).toString("hex"), blobData);
|
|
417
|
+
sendKvResponse(kvMsg, "setBlobResult", create(SetBlobResultSchema, {}), sendFrame);
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
function handleExecMessage(execMsg, mcpTools, sendFrame, onMcpExec, onUnhandledExec) {
|
|
421
|
+
const execCase = execMsg.message.case;
|
|
422
|
+
if (execCase === "requestContextArgs") {
|
|
423
|
+
const requestContext = create(RequestContextSchema, {
|
|
424
|
+
rules: [],
|
|
425
|
+
repositoryInfo: [],
|
|
426
|
+
tools: mcpTools,
|
|
427
|
+
gitRepos: [],
|
|
428
|
+
projectLayouts: [],
|
|
429
|
+
mcpInstructions: [],
|
|
430
|
+
fileContents: {},
|
|
431
|
+
customSubagents: [],
|
|
432
|
+
});
|
|
433
|
+
const result = create(RequestContextResultSchema, {
|
|
434
|
+
result: {
|
|
435
|
+
case: "success",
|
|
436
|
+
value: create(RequestContextSuccessSchema, { requestContext }),
|
|
437
|
+
},
|
|
438
|
+
});
|
|
439
|
+
sendExecResult(execMsg, "requestContextResult", result, sendFrame);
|
|
440
|
+
return;
|
|
441
|
+
}
|
|
442
|
+
if (execCase === "mcpArgs") {
|
|
443
|
+
const mcpArgs = execMsg.message.value;
|
|
444
|
+
const decoded = decodeMcpArgsMap(mcpArgs.args ?? {});
|
|
445
|
+
onMcpExec({
|
|
446
|
+
execId: execMsg.execId,
|
|
447
|
+
execMsgId: execMsg.id,
|
|
448
|
+
toolCallId: mcpArgs.toolCallId || crypto.randomUUID(),
|
|
449
|
+
toolName: mcpArgs.toolName || mcpArgs.name,
|
|
450
|
+
decodedArgs: JSON.stringify(decoded),
|
|
451
|
+
});
|
|
452
|
+
return;
|
|
453
|
+
}
|
|
454
|
+
// --- Reject native Cursor tools ---
|
|
455
|
+
// The model tries these first. We must respond with rejection/error
|
|
456
|
+
// so it falls back to our MCP tools (registered via RequestContext).
|
|
457
|
+
const REJECT_REASON = "Tool not available in this environment. Use the MCP tools provided instead.";
|
|
458
|
+
if (execCase === "readArgs") {
|
|
459
|
+
const args = execMsg.message.value;
|
|
460
|
+
const result = create(ReadResultSchema, {
|
|
461
|
+
result: {
|
|
462
|
+
case: "rejected",
|
|
463
|
+
value: create(ReadRejectedSchema, {
|
|
464
|
+
path: args.path,
|
|
465
|
+
reason: REJECT_REASON,
|
|
466
|
+
}),
|
|
467
|
+
},
|
|
468
|
+
});
|
|
469
|
+
sendExecResult(execMsg, "readResult", result, sendFrame);
|
|
470
|
+
return;
|
|
471
|
+
}
|
|
472
|
+
if (execCase === "lsArgs") {
|
|
473
|
+
const args = execMsg.message.value;
|
|
474
|
+
const result = create(LsResultSchema, {
|
|
475
|
+
result: {
|
|
476
|
+
case: "rejected",
|
|
477
|
+
value: create(LsRejectedSchema, {
|
|
478
|
+
path: args.path,
|
|
479
|
+
reason: REJECT_REASON,
|
|
480
|
+
}),
|
|
481
|
+
},
|
|
482
|
+
});
|
|
483
|
+
sendExecResult(execMsg, "lsResult", result, sendFrame);
|
|
484
|
+
return;
|
|
485
|
+
}
|
|
486
|
+
if (execCase === "grepArgs") {
|
|
487
|
+
const result = create(GrepResultSchema, {
|
|
488
|
+
result: {
|
|
489
|
+
case: "error",
|
|
490
|
+
value: create(GrepErrorSchema, { error: REJECT_REASON }),
|
|
491
|
+
},
|
|
492
|
+
});
|
|
493
|
+
sendExecResult(execMsg, "grepResult", result, sendFrame);
|
|
494
|
+
return;
|
|
495
|
+
}
|
|
496
|
+
if (execCase === "writeArgs") {
|
|
497
|
+
const args = execMsg.message.value;
|
|
498
|
+
const result = create(WriteResultSchema, {
|
|
499
|
+
result: {
|
|
500
|
+
case: "rejected",
|
|
501
|
+
value: create(WriteRejectedSchema, {
|
|
502
|
+
path: args.path,
|
|
503
|
+
reason: REJECT_REASON,
|
|
504
|
+
}),
|
|
505
|
+
},
|
|
506
|
+
});
|
|
507
|
+
sendExecResult(execMsg, "writeResult", result, sendFrame);
|
|
508
|
+
return;
|
|
509
|
+
}
|
|
510
|
+
if (execCase === "deleteArgs") {
|
|
511
|
+
const args = execMsg.message.value;
|
|
512
|
+
const result = create(DeleteResultSchema, {
|
|
513
|
+
result: {
|
|
514
|
+
case: "rejected",
|
|
515
|
+
value: create(DeleteRejectedSchema, {
|
|
516
|
+
path: args.path,
|
|
517
|
+
reason: REJECT_REASON,
|
|
518
|
+
}),
|
|
519
|
+
},
|
|
520
|
+
});
|
|
521
|
+
sendExecResult(execMsg, "deleteResult", result, sendFrame);
|
|
522
|
+
return;
|
|
523
|
+
}
|
|
524
|
+
if (execCase === "shellArgs" || execCase === "shellStreamArgs") {
|
|
525
|
+
const args = execMsg.message.value;
|
|
526
|
+
const result = create(ShellResultSchema, {
|
|
527
|
+
result: {
|
|
528
|
+
case: "rejected",
|
|
529
|
+
value: create(ShellRejectedSchema, {
|
|
530
|
+
command: args.command ?? "",
|
|
531
|
+
workingDirectory: args.workingDirectory ?? "",
|
|
532
|
+
reason: REJECT_REASON,
|
|
533
|
+
isReadonly: false,
|
|
534
|
+
}),
|
|
535
|
+
},
|
|
536
|
+
});
|
|
537
|
+
sendExecResult(execMsg, "shellResult", result, sendFrame);
|
|
538
|
+
return;
|
|
539
|
+
}
|
|
540
|
+
if (execCase === "backgroundShellSpawnArgs") {
|
|
541
|
+
const args = execMsg.message.value;
|
|
542
|
+
const result = create(BackgroundShellSpawnResultSchema, {
|
|
543
|
+
result: {
|
|
544
|
+
case: "rejected",
|
|
545
|
+
value: create(ShellRejectedSchema, {
|
|
546
|
+
command: args.command ?? "",
|
|
547
|
+
workingDirectory: args.workingDirectory ?? "",
|
|
548
|
+
reason: REJECT_REASON,
|
|
549
|
+
isReadonly: false,
|
|
550
|
+
}),
|
|
551
|
+
},
|
|
552
|
+
});
|
|
553
|
+
sendExecResult(execMsg, "backgroundShellSpawnResult", result, sendFrame);
|
|
554
|
+
return;
|
|
555
|
+
}
|
|
556
|
+
if (execCase === "writeShellStdinArgs") {
|
|
557
|
+
const result = create(WriteShellStdinResultSchema, {
|
|
558
|
+
result: {
|
|
559
|
+
case: "error",
|
|
560
|
+
value: create(WriteShellStdinErrorSchema, { error: REJECT_REASON }),
|
|
561
|
+
},
|
|
562
|
+
});
|
|
563
|
+
sendExecResult(execMsg, "writeShellStdinResult", result, sendFrame);
|
|
564
|
+
return;
|
|
565
|
+
}
|
|
566
|
+
if (execCase === "fetchArgs") {
|
|
567
|
+
const args = execMsg.message.value;
|
|
568
|
+
const result = create(FetchResultSchema, {
|
|
569
|
+
result: {
|
|
570
|
+
case: "error",
|
|
571
|
+
value: create(FetchErrorSchema, {
|
|
572
|
+
url: args.url ?? "",
|
|
573
|
+
error: REJECT_REASON,
|
|
574
|
+
}),
|
|
575
|
+
},
|
|
576
|
+
});
|
|
577
|
+
sendExecResult(execMsg, "fetchResult", result, sendFrame);
|
|
578
|
+
return;
|
|
579
|
+
}
|
|
580
|
+
if (execCase === "diagnosticsArgs") {
|
|
581
|
+
const result = create(DiagnosticsResultSchema, {});
|
|
582
|
+
sendExecResult(execMsg, "diagnosticsResult", result, sendFrame);
|
|
583
|
+
return;
|
|
584
|
+
}
|
|
585
|
+
// MCP resource/screen/computer exec types
|
|
586
|
+
const miscCaseMap = {
|
|
587
|
+
listMcpResourcesExecArgs: "listMcpResourcesExecResult",
|
|
588
|
+
readMcpResourceExecArgs: "readMcpResourceExecResult",
|
|
589
|
+
recordScreenArgs: "recordScreenResult",
|
|
590
|
+
computerUseArgs: "computerUseResult",
|
|
591
|
+
};
|
|
592
|
+
const resultCase = miscCaseMap[execCase];
|
|
593
|
+
if (resultCase) {
|
|
594
|
+
sendExecResult(execMsg, resultCase, create(McpResultSchema, {}), sendFrame);
|
|
595
|
+
return;
|
|
596
|
+
}
|
|
597
|
+
logPluginError("Unhandled Cursor exec type", {
|
|
598
|
+
execCase: execCase ?? "undefined",
|
|
599
|
+
execId: execMsg.execId,
|
|
600
|
+
execMsgId: execMsg.id,
|
|
601
|
+
});
|
|
602
|
+
onUnhandledExec?.({
|
|
603
|
+
execCase: execCase ?? "undefined",
|
|
604
|
+
execId: execMsg.execId,
|
|
605
|
+
execMsgId: execMsg.id,
|
|
606
|
+
});
|
|
607
|
+
}
|
|
608
|
+
/** Send an exec client message back to Cursor. */
|
|
609
|
+
function sendExecResult(execMsg, messageCase, value, sendFrame) {
|
|
610
|
+
const execClientMessage = create(ExecClientMessageSchema, {
|
|
611
|
+
id: execMsg.id,
|
|
612
|
+
execId: execMsg.execId,
|
|
613
|
+
message: { case: messageCase, value: value },
|
|
614
|
+
});
|
|
615
|
+
const clientMessage = create(AgentClientMessageSchema, {
|
|
616
|
+
message: { case: "execClientMessage", value: execClientMessage },
|
|
617
|
+
});
|
|
618
|
+
sendFrame(toBinary(AgentClientMessageSchema, clientMessage));
|
|
619
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { PendingExec } from "./types";
|
|
2
|
+
export interface StreamState {
|
|
3
|
+
toolCallIndex: number;
|
|
4
|
+
pendingExecs: PendingExec[];
|
|
5
|
+
outputTokens: number;
|
|
6
|
+
totalTokens: number;
|
|
7
|
+
interactionToolArgsText: Map<string, string>;
|
|
8
|
+
emittedToolCallIds: Set<string>;
|
|
9
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function handleTitleGenerationRequest(sourceText: string, accessToken: string, modelId: string, stream: boolean): Promise<Response>;
|