@playwo/opencode-cursor-oauth 0.0.0-dev.b8e6dd72a8b6 → 0.0.0-dev.c058193266d4
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/README.md +28 -86
- package/dist/auth.js +27 -3
- 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 -279
- package/dist/logger.d.ts +7 -0
- package/dist/logger.js +150 -0
- package/dist/models.js +22 -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-close-controller.d.ts +6 -0
- package/dist/proxy/bridge-close-controller.js +37 -0
- package/dist/proxy/bridge-non-streaming.d.ts +3 -0
- package/dist/proxy/bridge-non-streaming.js +123 -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 +409 -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 +153 -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 +101 -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 +634 -0
- package/dist/proxy/stream-state.d.ts +7 -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 +32 -0
- package/dist/proxy/types.js +1 -0
- package/dist/proxy.d.ts +2 -20
- package/dist/proxy.js +2 -1329
- package/package.json +1 -2
package/dist/index.d.ts
CHANGED
|
@@ -1,14 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
*
|
|
4
|
-
* Enables using Cursor models (Claude, GPT, etc.) inside OpenCode via:
|
|
5
|
-
* 1. Browser-based OAuth login to Cursor
|
|
6
|
-
* 2. Local proxy translating OpenAI format → Cursor gRPC protocol
|
|
7
|
-
*/
|
|
8
|
-
import type { Plugin } from "@opencode-ai/plugin";
|
|
9
|
-
/**
|
|
10
|
-
* OpenCode plugin that provides Cursor authentication and model access.
|
|
11
|
-
* Register in opencode.json: { "plugin": ["opencode-cursor-oauth"] }
|
|
12
|
-
*/
|
|
13
|
-
export declare const CursorAuthPlugin: Plugin;
|
|
14
|
-
export default CursorAuthPlugin;
|
|
1
|
+
export { CursorAuthPlugin } from "./plugin/cursor-auth-plugin";
|
|
2
|
+
export { CursorAuthPlugin as default } from "./plugin/cursor-auth-plugin";
|
package/dist/index.js
CHANGED
|
@@ -1,279 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import { startProxy, stopProxy } from "./proxy";
|
|
4
|
-
const CURSOR_PROVIDER_ID = "cursor";
|
|
5
|
-
let lastModelDiscoveryError = null;
|
|
6
|
-
/**
|
|
7
|
-
* OpenCode plugin that provides Cursor authentication and model access.
|
|
8
|
-
* Register in opencode.json: { "plugin": ["opencode-cursor-oauth"] }
|
|
9
|
-
*/
|
|
10
|
-
export const CursorAuthPlugin = async (input) => {
|
|
11
|
-
return {
|
|
12
|
-
auth: {
|
|
13
|
-
provider: CURSOR_PROVIDER_ID,
|
|
14
|
-
async loader(getAuth, provider) {
|
|
15
|
-
const auth = await getAuth();
|
|
16
|
-
if (!auth || auth.type !== "oauth")
|
|
17
|
-
return {};
|
|
18
|
-
// Ensure we have a valid access token, refreshing if expired
|
|
19
|
-
let accessToken = auth.access;
|
|
20
|
-
if (!accessToken || auth.expires < Date.now()) {
|
|
21
|
-
const refreshed = await refreshCursorToken(auth.refresh);
|
|
22
|
-
await input.client.auth.set({
|
|
23
|
-
path: { id: CURSOR_PROVIDER_ID },
|
|
24
|
-
body: {
|
|
25
|
-
type: "oauth",
|
|
26
|
-
refresh: refreshed.refresh,
|
|
27
|
-
access: refreshed.access,
|
|
28
|
-
expires: refreshed.expires,
|
|
29
|
-
},
|
|
30
|
-
});
|
|
31
|
-
accessToken = refreshed.access;
|
|
32
|
-
}
|
|
33
|
-
let models;
|
|
34
|
-
try {
|
|
35
|
-
models = await getCursorModels(accessToken);
|
|
36
|
-
lastModelDiscoveryError = null;
|
|
37
|
-
}
|
|
38
|
-
catch (error) {
|
|
39
|
-
const message = error instanceof Error
|
|
40
|
-
? error.message
|
|
41
|
-
: "Cursor model discovery failed.";
|
|
42
|
-
stopProxy();
|
|
43
|
-
if (provider) {
|
|
44
|
-
provider.models = {};
|
|
45
|
-
}
|
|
46
|
-
if (message !== lastModelDiscoveryError) {
|
|
47
|
-
lastModelDiscoveryError = message;
|
|
48
|
-
await showDiscoveryFailureToast(input, message);
|
|
49
|
-
}
|
|
50
|
-
return buildDisabledProviderConfig(message);
|
|
51
|
-
}
|
|
52
|
-
const port = await startProxy(async () => {
|
|
53
|
-
const currentAuth = await getAuth();
|
|
54
|
-
if (currentAuth.type !== "oauth") {
|
|
55
|
-
throw new Error("Cursor auth not configured");
|
|
56
|
-
}
|
|
57
|
-
if (!currentAuth.access || currentAuth.expires < Date.now()) {
|
|
58
|
-
const refreshed = await refreshCursorToken(currentAuth.refresh);
|
|
59
|
-
await input.client.auth.set({
|
|
60
|
-
path: { id: CURSOR_PROVIDER_ID },
|
|
61
|
-
body: {
|
|
62
|
-
type: "oauth",
|
|
63
|
-
refresh: refreshed.refresh,
|
|
64
|
-
access: refreshed.access,
|
|
65
|
-
expires: refreshed.expires,
|
|
66
|
-
},
|
|
67
|
-
});
|
|
68
|
-
return refreshed.access;
|
|
69
|
-
}
|
|
70
|
-
return currentAuth.access;
|
|
71
|
-
}, models);
|
|
72
|
-
if (provider) {
|
|
73
|
-
provider.models = buildCursorProviderModels(models, port);
|
|
74
|
-
}
|
|
75
|
-
return {
|
|
76
|
-
baseURL: `http://localhost:${port}/v1`,
|
|
77
|
-
apiKey: "cursor-proxy",
|
|
78
|
-
async fetch(requestInput, init) {
|
|
79
|
-
if (init?.headers) {
|
|
80
|
-
if (init.headers instanceof Headers) {
|
|
81
|
-
init.headers.delete("authorization");
|
|
82
|
-
}
|
|
83
|
-
else if (Array.isArray(init.headers)) {
|
|
84
|
-
init.headers = init.headers.filter(([key]) => key.toLowerCase() !== "authorization");
|
|
85
|
-
}
|
|
86
|
-
else {
|
|
87
|
-
delete init.headers["authorization"];
|
|
88
|
-
delete init.headers["Authorization"];
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
return fetch(requestInput, init);
|
|
92
|
-
},
|
|
93
|
-
};
|
|
94
|
-
},
|
|
95
|
-
methods: [
|
|
96
|
-
{
|
|
97
|
-
type: "oauth",
|
|
98
|
-
label: "Login with Cursor",
|
|
99
|
-
async authorize() {
|
|
100
|
-
const { verifier, uuid, loginUrl } = await generateCursorAuthParams();
|
|
101
|
-
return {
|
|
102
|
-
url: loginUrl,
|
|
103
|
-
instructions: "Complete login in your browser. This window will close automatically.",
|
|
104
|
-
method: "auto",
|
|
105
|
-
async callback() {
|
|
106
|
-
const { accessToken, refreshToken } = await pollCursorAuth(uuid, verifier);
|
|
107
|
-
return {
|
|
108
|
-
type: "success",
|
|
109
|
-
refresh: refreshToken,
|
|
110
|
-
access: accessToken,
|
|
111
|
-
expires: getTokenExpiry(accessToken),
|
|
112
|
-
};
|
|
113
|
-
},
|
|
114
|
-
};
|
|
115
|
-
},
|
|
116
|
-
},
|
|
117
|
-
],
|
|
118
|
-
},
|
|
119
|
-
};
|
|
120
|
-
};
|
|
121
|
-
function buildCursorProviderModels(models, port) {
|
|
122
|
-
return Object.fromEntries(models.map((model) => [
|
|
123
|
-
model.id,
|
|
124
|
-
{
|
|
125
|
-
id: model.id,
|
|
126
|
-
providerID: CURSOR_PROVIDER_ID,
|
|
127
|
-
api: {
|
|
128
|
-
id: model.id,
|
|
129
|
-
url: `http://localhost:${port}/v1`,
|
|
130
|
-
npm: "@ai-sdk/openai-compatible",
|
|
131
|
-
},
|
|
132
|
-
name: model.name,
|
|
133
|
-
capabilities: {
|
|
134
|
-
temperature: true,
|
|
135
|
-
reasoning: model.reasoning,
|
|
136
|
-
attachment: false,
|
|
137
|
-
toolcall: true,
|
|
138
|
-
input: {
|
|
139
|
-
text: true,
|
|
140
|
-
audio: false,
|
|
141
|
-
image: false,
|
|
142
|
-
video: false,
|
|
143
|
-
pdf: false,
|
|
144
|
-
},
|
|
145
|
-
output: {
|
|
146
|
-
text: true,
|
|
147
|
-
audio: false,
|
|
148
|
-
image: false,
|
|
149
|
-
video: false,
|
|
150
|
-
pdf: false,
|
|
151
|
-
},
|
|
152
|
-
interleaved: false,
|
|
153
|
-
},
|
|
154
|
-
cost: estimateModelCost(model.id),
|
|
155
|
-
limit: {
|
|
156
|
-
context: model.contextWindow,
|
|
157
|
-
output: model.maxTokens,
|
|
158
|
-
},
|
|
159
|
-
status: "active",
|
|
160
|
-
options: {},
|
|
161
|
-
headers: {},
|
|
162
|
-
release_date: "",
|
|
163
|
-
variants: {},
|
|
164
|
-
},
|
|
165
|
-
]));
|
|
166
|
-
}
|
|
167
|
-
async function showDiscoveryFailureToast(input, message) {
|
|
168
|
-
try {
|
|
169
|
-
await input.client.tui.showToast({
|
|
170
|
-
body: {
|
|
171
|
-
title: "Cursor plugin disabled",
|
|
172
|
-
message,
|
|
173
|
-
variant: "error",
|
|
174
|
-
duration: 8_000,
|
|
175
|
-
},
|
|
176
|
-
});
|
|
177
|
-
}
|
|
178
|
-
catch { }
|
|
179
|
-
}
|
|
180
|
-
function buildDisabledProviderConfig(message) {
|
|
181
|
-
return {
|
|
182
|
-
baseURL: "http://127.0.0.1/cursor-disabled/v1",
|
|
183
|
-
apiKey: "cursor-disabled",
|
|
184
|
-
async fetch() {
|
|
185
|
-
return new Response(JSON.stringify({
|
|
186
|
-
error: {
|
|
187
|
-
message,
|
|
188
|
-
type: "server_error",
|
|
189
|
-
code: "cursor_model_discovery_failed",
|
|
190
|
-
},
|
|
191
|
-
}), {
|
|
192
|
-
status: 503,
|
|
193
|
-
headers: { "Content-Type": "application/json" },
|
|
194
|
-
});
|
|
195
|
-
},
|
|
196
|
-
};
|
|
197
|
-
}
|
|
198
|
-
// $/M token rates from cursor.com/docs/models-and-pricing
|
|
199
|
-
const MODEL_COST_TABLE = {
|
|
200
|
-
// Anthropic
|
|
201
|
-
"claude-4-sonnet": { input: 3, output: 15, cache: { read: 0.3, write: 3.75 } },
|
|
202
|
-
"claude-4-sonnet-1m": { input: 6, output: 22.5, cache: { read: 0.6, write: 7.5 } },
|
|
203
|
-
"claude-4.5-haiku": { input: 1, output: 5, cache: { read: 0.1, write: 1.25 } },
|
|
204
|
-
"claude-4.5-opus": { input: 5, output: 25, cache: { read: 0.5, write: 6.25 } },
|
|
205
|
-
"claude-4.5-sonnet": { input: 3, output: 15, cache: { read: 0.3, write: 3.75 } },
|
|
206
|
-
"claude-4.6-opus": { input: 5, output: 25, cache: { read: 0.5, write: 6.25 } },
|
|
207
|
-
"claude-4.6-opus-fast": { input: 30, output: 150, cache: { read: 3, write: 37.5 } },
|
|
208
|
-
"claude-4.6-sonnet": { input: 3, output: 15, cache: { read: 0.3, write: 3.75 } },
|
|
209
|
-
// Cursor
|
|
210
|
-
"composer-1": { input: 1.25, output: 10, cache: { read: 0.125, write: 0 } },
|
|
211
|
-
"composer-1.5": { input: 3.5, output: 17.5, cache: { read: 0.35, write: 0 } },
|
|
212
|
-
"composer-2": { input: 0.5, output: 2.5, cache: { read: 0.2, write: 0 } },
|
|
213
|
-
"composer-2-fast": { input: 1.5, output: 7.5, cache: { read: 0.2, write: 0 } },
|
|
214
|
-
// Google
|
|
215
|
-
"gemini-2.5-flash": { input: 0.3, output: 2.5, cache: { read: 0.03, write: 0 } },
|
|
216
|
-
"gemini-3-flash": { input: 0.5, output: 3, cache: { read: 0.05, write: 0 } },
|
|
217
|
-
"gemini-3-pro": { input: 2, output: 12, cache: { read: 0.2, write: 0 } },
|
|
218
|
-
"gemini-3-pro-image": { input: 2, output: 12, cache: { read: 0.2, write: 0 } },
|
|
219
|
-
"gemini-3.1-pro": { input: 2, output: 12, cache: { read: 0.2, write: 0 } },
|
|
220
|
-
// OpenAI
|
|
221
|
-
"gpt-5": { input: 1.25, output: 10, cache: { read: 0.125, write: 0 } },
|
|
222
|
-
"gpt-5-fast": { input: 2.5, output: 20, cache: { read: 0.25, write: 0 } },
|
|
223
|
-
"gpt-5-mini": { input: 0.25, output: 2, cache: { read: 0.025, write: 0 } },
|
|
224
|
-
"gpt-5-codex": { input: 1.25, output: 10, cache: { read: 0.125, write: 0 } },
|
|
225
|
-
"gpt-5.1-codex": { input: 1.25, output: 10, cache: { read: 0.125, write: 0 } },
|
|
226
|
-
"gpt-5.1-codex-max": { input: 1.25, output: 10, cache: { read: 0.125, write: 0 } },
|
|
227
|
-
"gpt-5.1-codex-mini": { input: 0.25, output: 2, cache: { read: 0.025, write: 0 } },
|
|
228
|
-
"gpt-5.2": { input: 1.75, output: 14, cache: { read: 0.175, write: 0 } },
|
|
229
|
-
"gpt-5.2-codex": { input: 1.75, output: 14, cache: { read: 0.175, write: 0 } },
|
|
230
|
-
"gpt-5.3-codex": { input: 1.75, output: 14, cache: { read: 0.175, write: 0 } },
|
|
231
|
-
"gpt-5.4": { input: 2.5, output: 15, cache: { read: 0.25, write: 0 } },
|
|
232
|
-
"gpt-5.4-mini": { input: 0.75, output: 4.5, cache: { read: 0.075, write: 0 } },
|
|
233
|
-
"gpt-5.4-nano": { input: 0.2, output: 1.25, cache: { read: 0.02, write: 0 } },
|
|
234
|
-
// xAI
|
|
235
|
-
"grok-4.20": { input: 2, output: 6, cache: { read: 0.2, write: 0 } },
|
|
236
|
-
// Moonshot
|
|
237
|
-
"kimi-k2.5": { input: 0.6, output: 3, cache: { read: 0.1, write: 0 } },
|
|
238
|
-
};
|
|
239
|
-
// Most-specific first
|
|
240
|
-
const MODEL_COST_PATTERNS = [
|
|
241
|
-
{ match: (id) => /claude.*opus.*fast/i.test(id), cost: MODEL_COST_TABLE["claude-4.6-opus-fast"] },
|
|
242
|
-
{ match: (id) => /claude.*opus/i.test(id), cost: MODEL_COST_TABLE["claude-4.6-opus"] },
|
|
243
|
-
{ match: (id) => /claude.*haiku/i.test(id), cost: MODEL_COST_TABLE["claude-4.5-haiku"] },
|
|
244
|
-
{ match: (id) => /claude.*sonnet/i.test(id), cost: MODEL_COST_TABLE["claude-4.6-sonnet"] },
|
|
245
|
-
{ match: (id) => /claude/i.test(id), cost: MODEL_COST_TABLE["claude-4.6-sonnet"] },
|
|
246
|
-
{ match: (id) => /composer-?2/i.test(id), cost: MODEL_COST_TABLE["composer-2"] },
|
|
247
|
-
{ match: (id) => /composer-?1\.5/i.test(id), cost: MODEL_COST_TABLE["composer-1.5"] },
|
|
248
|
-
{ match: (id) => /composer/i.test(id), cost: MODEL_COST_TABLE["composer-1"] },
|
|
249
|
-
{ match: (id) => /gpt-5\.4.*nano/i.test(id), cost: MODEL_COST_TABLE["gpt-5.4-nano"] },
|
|
250
|
-
{ match: (id) => /gpt-5\.4.*mini/i.test(id), cost: MODEL_COST_TABLE["gpt-5.4-mini"] },
|
|
251
|
-
{ match: (id) => /gpt-5\.4/i.test(id), cost: MODEL_COST_TABLE["gpt-5.4"] },
|
|
252
|
-
{ match: (id) => /gpt-5\.3/i.test(id), cost: MODEL_COST_TABLE["gpt-5.3-codex"] },
|
|
253
|
-
{ match: (id) => /gpt-5\.2/i.test(id), cost: MODEL_COST_TABLE["gpt-5.2"] },
|
|
254
|
-
{ match: (id) => /gpt-5\.1.*mini/i.test(id), cost: MODEL_COST_TABLE["gpt-5.1-codex-mini"] },
|
|
255
|
-
{ match: (id) => /gpt-5\.1/i.test(id), cost: MODEL_COST_TABLE["gpt-5.1-codex"] },
|
|
256
|
-
{ match: (id) => /gpt-5.*mini/i.test(id), cost: MODEL_COST_TABLE["gpt-5-mini"] },
|
|
257
|
-
{ match: (id) => /gpt-5.*fast/i.test(id), cost: MODEL_COST_TABLE["gpt-5-fast"] },
|
|
258
|
-
{ match: (id) => /gpt-5/i.test(id), cost: MODEL_COST_TABLE["gpt-5"] },
|
|
259
|
-
{ match: (id) => /gemini.*3\.1/i.test(id), cost: MODEL_COST_TABLE["gemini-3.1-pro"] },
|
|
260
|
-
{ match: (id) => /gemini.*3.*flash/i.test(id), cost: MODEL_COST_TABLE["gemini-3-flash"] },
|
|
261
|
-
{ match: (id) => /gemini.*3/i.test(id), cost: MODEL_COST_TABLE["gemini-3-pro"] },
|
|
262
|
-
{ match: (id) => /gemini.*flash/i.test(id), cost: MODEL_COST_TABLE["gemini-2.5-flash"] },
|
|
263
|
-
{ match: (id) => /gemini/i.test(id), cost: MODEL_COST_TABLE["gemini-3.1-pro"] },
|
|
264
|
-
{ match: (id) => /grok/i.test(id), cost: MODEL_COST_TABLE["grok-4.20"] },
|
|
265
|
-
{ match: (id) => /kimi/i.test(id), cost: MODEL_COST_TABLE["kimi-k2.5"] },
|
|
266
|
-
];
|
|
267
|
-
const DEFAULT_COST = { input: 3, output: 15, cache: { read: 0.3, write: 0 } };
|
|
268
|
-
function estimateModelCost(modelId) {
|
|
269
|
-
const normalized = modelId.toLowerCase();
|
|
270
|
-
const exact = MODEL_COST_TABLE[normalized];
|
|
271
|
-
if (exact)
|
|
272
|
-
return exact;
|
|
273
|
-
const stripped = normalized.replace(/-(high|medium|low|preview|thinking|spark-preview)$/g, "");
|
|
274
|
-
const strippedMatch = MODEL_COST_TABLE[stripped];
|
|
275
|
-
if (strippedMatch)
|
|
276
|
-
return strippedMatch;
|
|
277
|
-
return MODEL_COST_PATTERNS.find((p) => p.match(normalized))?.cost ?? DEFAULT_COST;
|
|
278
|
-
}
|
|
279
|
-
export default CursorAuthPlugin;
|
|
1
|
+
export { CursorAuthPlugin } from "./plugin/cursor-auth-plugin";
|
|
2
|
+
export { CursorAuthPlugin as default } from "./plugin/cursor-auth-plugin";
|
package/dist/logger.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { PluginInput } from "@opencode-ai/plugin";
|
|
2
|
+
export declare function configurePluginLogger(input: PluginInput): void;
|
|
3
|
+
export declare function errorDetails(error: unknown): Record<string, unknown>;
|
|
4
|
+
export declare function logPluginWarn(message: string, extra?: Record<string, unknown>): void;
|
|
5
|
+
export declare function logPluginInfo(message: string, extra?: Record<string, unknown>): void;
|
|
6
|
+
export declare function logPluginError(message: string, extra?: Record<string, unknown>): void;
|
|
7
|
+
export declare function flushPluginLogs(): Promise<void>;
|
package/dist/logger.js
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
const PLUGIN_LOG_SERVICE = "opencode-cursor-oauth";
|
|
2
|
+
const MAX_STRING_LENGTH = 1_500;
|
|
3
|
+
const MAX_ARRAY_LENGTH = 20;
|
|
4
|
+
const MAX_OBJECT_KEYS = 25;
|
|
5
|
+
let currentLogger;
|
|
6
|
+
let pendingLogWrites = Promise.resolve();
|
|
7
|
+
export function configurePluginLogger(input) {
|
|
8
|
+
currentLogger = {
|
|
9
|
+
client: input.client,
|
|
10
|
+
directory: input.directory,
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
export function errorDetails(error) {
|
|
14
|
+
if (error instanceof Error) {
|
|
15
|
+
return {
|
|
16
|
+
errorName: error.name,
|
|
17
|
+
errorMessage: error.message,
|
|
18
|
+
errorStack: error.stack,
|
|
19
|
+
errorCause: serializeValue(error.cause, 1),
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
return {
|
|
23
|
+
errorType: typeof error,
|
|
24
|
+
errorValue: serializeValue(error, 1),
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
export function logPluginWarn(message, extra = {}) {
|
|
28
|
+
logPlugin("warn", message, extra);
|
|
29
|
+
}
|
|
30
|
+
export function logPluginInfo(message, extra = {}) {
|
|
31
|
+
logPlugin("info", message, extra);
|
|
32
|
+
}
|
|
33
|
+
export function logPluginError(message, extra = {}) {
|
|
34
|
+
logPlugin("error", message, extra);
|
|
35
|
+
}
|
|
36
|
+
export function flushPluginLogs() {
|
|
37
|
+
return pendingLogWrites;
|
|
38
|
+
}
|
|
39
|
+
function logPlugin(level, message, extra) {
|
|
40
|
+
const serializedExtra = serializeValue(extra, 0);
|
|
41
|
+
writeConsoleLog(level, message, serializedExtra);
|
|
42
|
+
if (!currentLogger?.client?.app?.log) {
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
pendingLogWrites = pendingLogWrites
|
|
46
|
+
.catch(() => { })
|
|
47
|
+
.then(async () => {
|
|
48
|
+
try {
|
|
49
|
+
await currentLogger?.client.app.log({
|
|
50
|
+
query: { directory: currentLogger.directory },
|
|
51
|
+
body: {
|
|
52
|
+
service: PLUGIN_LOG_SERVICE,
|
|
53
|
+
level,
|
|
54
|
+
message,
|
|
55
|
+
extra: serializedExtra,
|
|
56
|
+
},
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
catch (logError) {
|
|
60
|
+
writeConsoleLog("warn", "Failed to forward plugin log to OpenCode", {
|
|
61
|
+
originalLevel: level,
|
|
62
|
+
originalMessage: message,
|
|
63
|
+
...errorDetails(logError),
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
function writeConsoleLog(level, message, extra) {
|
|
69
|
+
const prefix = `[${PLUGIN_LOG_SERVICE}] ${message}`;
|
|
70
|
+
const suffix = Object.keys(extra).length > 0 ? ` ${JSON.stringify(extra)}` : "";
|
|
71
|
+
if (level === "error") {
|
|
72
|
+
console.error(`${prefix}${suffix}`);
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
console.warn(`${prefix}${suffix}`);
|
|
76
|
+
}
|
|
77
|
+
function serializeValue(value, depth, seen = new WeakSet()) {
|
|
78
|
+
if (value === null || value === undefined)
|
|
79
|
+
return value;
|
|
80
|
+
if (typeof value === "string")
|
|
81
|
+
return truncateString(value);
|
|
82
|
+
const valueType = typeof value;
|
|
83
|
+
if (valueType === "number" || valueType === "boolean")
|
|
84
|
+
return value;
|
|
85
|
+
if (valueType === "bigint")
|
|
86
|
+
return value.toString();
|
|
87
|
+
if (valueType === "symbol")
|
|
88
|
+
return String(value);
|
|
89
|
+
if (valueType === "function")
|
|
90
|
+
return `[function ${value.name || "anonymous"}]`;
|
|
91
|
+
if (value instanceof URL)
|
|
92
|
+
return value.toString();
|
|
93
|
+
if (value instanceof Headers)
|
|
94
|
+
return Object.fromEntries(value.entries());
|
|
95
|
+
if (value instanceof Error) {
|
|
96
|
+
return {
|
|
97
|
+
name: value.name,
|
|
98
|
+
message: value.message,
|
|
99
|
+
stack: truncateString(value.stack),
|
|
100
|
+
cause: serializeValue(value.cause, depth + 1, seen),
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
if (value instanceof Uint8Array) {
|
|
104
|
+
return serializeBinary(value);
|
|
105
|
+
}
|
|
106
|
+
if (Array.isArray(value)) {
|
|
107
|
+
if (depth >= 3)
|
|
108
|
+
return `[array(${value.length})]`;
|
|
109
|
+
return value
|
|
110
|
+
.slice(0, MAX_ARRAY_LENGTH)
|
|
111
|
+
.map((entry) => serializeValue(entry, depth + 1, seen));
|
|
112
|
+
}
|
|
113
|
+
if (typeof value === "object") {
|
|
114
|
+
if (seen.has(value))
|
|
115
|
+
return "[circular]";
|
|
116
|
+
seen.add(value);
|
|
117
|
+
if (depth >= 3) {
|
|
118
|
+
return `[object ${value.constructor?.name || "Object"}]`;
|
|
119
|
+
}
|
|
120
|
+
const entries = Object.entries(value).slice(0, MAX_OBJECT_KEYS);
|
|
121
|
+
return Object.fromEntries(entries.map(([key, entry]) => [
|
|
122
|
+
key,
|
|
123
|
+
serializeValue(entry, depth + 1, seen),
|
|
124
|
+
]));
|
|
125
|
+
}
|
|
126
|
+
return String(value);
|
|
127
|
+
}
|
|
128
|
+
function serializeBinary(value) {
|
|
129
|
+
const text = new TextDecoder().decode(value);
|
|
130
|
+
const printable = /^[\x09\x0a\x0d\x20-\x7e]*$/.test(text);
|
|
131
|
+
if (printable) {
|
|
132
|
+
return {
|
|
133
|
+
type: "uint8array",
|
|
134
|
+
length: value.length,
|
|
135
|
+
text: truncateString(text),
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
return {
|
|
139
|
+
type: "uint8array",
|
|
140
|
+
length: value.length,
|
|
141
|
+
base64: truncateString(Buffer.from(value).toString("base64")),
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
function truncateString(value) {
|
|
145
|
+
if (value === undefined)
|
|
146
|
+
return undefined;
|
|
147
|
+
if (value.length <= MAX_STRING_LENGTH)
|
|
148
|
+
return value;
|
|
149
|
+
return `${value.slice(0, MAX_STRING_LENGTH - 3)}...`;
|
|
150
|
+
}
|
package/dist/models.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { create, fromBinary, toBinary } from "@bufbuild/protobuf";
|
|
2
2
|
import { z } from "zod";
|
|
3
|
-
import { callCursorUnaryRpc } from "./
|
|
3
|
+
import { callCursorUnaryRpc, decodeConnectUnaryBody } from "./cursor";
|
|
4
|
+
import { errorDetails, logPluginError, logPluginWarn } from "./logger";
|
|
4
5
|
import { GetUsableModelsRequestSchema, GetUsableModelsResponseSchema, } from "./proto/agent_pb";
|
|
5
6
|
const GET_USABLE_MODELS_PATH = "/agent.v1.AgentService/GetUsableModels";
|
|
6
7
|
const MODEL_DISCOVERY_TIMEOUT_MS = 5_000;
|
|
@@ -35,16 +36,32 @@ async function fetchCursorUsableModels(apiKey) {
|
|
|
35
36
|
timeoutMs: MODEL_DISCOVERY_TIMEOUT_MS,
|
|
36
37
|
});
|
|
37
38
|
if (response.timedOut) {
|
|
39
|
+
logPluginError("Cursor model discovery timed out", {
|
|
40
|
+
rpcPath: GET_USABLE_MODELS_PATH,
|
|
41
|
+
timeoutMs: MODEL_DISCOVERY_TIMEOUT_MS,
|
|
42
|
+
});
|
|
38
43
|
throw new CursorModelDiscoveryError(`Cursor model discovery timed out after ${MODEL_DISCOVERY_TIMEOUT_MS}ms.`);
|
|
39
44
|
}
|
|
40
45
|
if (response.exitCode !== 0) {
|
|
46
|
+
logPluginError("Cursor model discovery HTTP failure", {
|
|
47
|
+
rpcPath: GET_USABLE_MODELS_PATH,
|
|
48
|
+
exitCode: response.exitCode,
|
|
49
|
+
responseBody: response.body,
|
|
50
|
+
});
|
|
41
51
|
throw new CursorModelDiscoveryError(buildDiscoveryHttpError(response.exitCode, response.body));
|
|
42
52
|
}
|
|
43
53
|
if (response.body.length === 0) {
|
|
54
|
+
logPluginWarn("Cursor model discovery returned an empty response", {
|
|
55
|
+
rpcPath: GET_USABLE_MODELS_PATH,
|
|
56
|
+
});
|
|
44
57
|
throw new CursorModelDiscoveryError("Cursor model discovery returned an empty response.");
|
|
45
58
|
}
|
|
46
59
|
const decoded = decodeGetUsableModelsResponse(response.body);
|
|
47
60
|
if (!decoded) {
|
|
61
|
+
logPluginError("Cursor model discovery returned an unreadable response", {
|
|
62
|
+
rpcPath: GET_USABLE_MODELS_PATH,
|
|
63
|
+
responseBody: response.body,
|
|
64
|
+
});
|
|
48
65
|
throw new CursorModelDiscoveryError("Cursor model discovery returned an unreadable response.");
|
|
49
66
|
}
|
|
50
67
|
const models = normalizeCursorModels(decoded.models);
|
|
@@ -56,6 +73,10 @@ async function fetchCursorUsableModels(apiKey) {
|
|
|
56
73
|
catch (error) {
|
|
57
74
|
if (error instanceof CursorModelDiscoveryError)
|
|
58
75
|
throw error;
|
|
76
|
+
logPluginError("Cursor model discovery crashed", {
|
|
77
|
+
rpcPath: GET_USABLE_MODELS_PATH,
|
|
78
|
+
...errorDetails(error),
|
|
79
|
+
});
|
|
59
80
|
throw new CursorModelDiscoveryError("Cursor model discovery failed.");
|
|
60
81
|
}
|
|
61
82
|
}
|
|
@@ -117,28 +138,6 @@ function decodeGetUsableModelsResponse(payload) {
|
|
|
117
138
|
}
|
|
118
139
|
}
|
|
119
140
|
}
|
|
120
|
-
function decodeConnectUnaryBody(payload) {
|
|
121
|
-
if (payload.length < 5)
|
|
122
|
-
return null;
|
|
123
|
-
let offset = 0;
|
|
124
|
-
while (offset + 5 <= payload.length) {
|
|
125
|
-
const flags = payload[offset];
|
|
126
|
-
const view = new DataView(payload.buffer, payload.byteOffset + offset, payload.byteLength - offset);
|
|
127
|
-
const messageLength = view.getUint32(1, false);
|
|
128
|
-
const frameEnd = offset + 5 + messageLength;
|
|
129
|
-
if (frameEnd > payload.length)
|
|
130
|
-
return null;
|
|
131
|
-
// Compression flag
|
|
132
|
-
if ((flags & 0b0000_0001) !== 0)
|
|
133
|
-
return null;
|
|
134
|
-
// End-of-stream flag — skip trailer frames
|
|
135
|
-
if ((flags & 0b0000_0010) === 0) {
|
|
136
|
-
return payload.subarray(offset + 5, frameEnd);
|
|
137
|
-
}
|
|
138
|
-
offset = frameEnd;
|
|
139
|
-
}
|
|
140
|
-
return null;
|
|
141
|
-
}
|
|
142
141
|
function normalizeCursorModels(models) {
|
|
143
142
|
if (models.length === 0)
|
|
144
143
|
return [];
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { ChatCompletionRequest, OpenAIMessage, OpenAIToolCall } from "./types";
|
|
2
|
+
export interface ToolResultInfo {
|
|
3
|
+
toolCallId: string;
|
|
4
|
+
content: string;
|
|
5
|
+
}
|
|
6
|
+
interface ParsedMessages {
|
|
7
|
+
systemPrompt: string;
|
|
8
|
+
userText: string;
|
|
9
|
+
turns: Array<{
|
|
10
|
+
userText: string;
|
|
11
|
+
assistantText: string;
|
|
12
|
+
}>;
|
|
13
|
+
toolResults: ToolResultInfo[];
|
|
14
|
+
pendingAssistantSummary: string;
|
|
15
|
+
completedTurnsFingerprint: string;
|
|
16
|
+
}
|
|
17
|
+
/** Normalize OpenAI message content to a plain string. */
|
|
18
|
+
export declare function textContent(content: OpenAIMessage["content"]): string;
|
|
19
|
+
export declare function parseMessages(messages: OpenAIMessage[]): ParsedMessages;
|
|
20
|
+
export declare function formatToolCallSummary(call: OpenAIToolCall): string;
|
|
21
|
+
export declare function formatToolResultSummary(result: ToolResultInfo): string;
|
|
22
|
+
export declare function buildCompletedTurnsFingerprint(systemPrompt: string, turns: Array<{
|
|
23
|
+
userText: string;
|
|
24
|
+
assistantText: string;
|
|
25
|
+
}>): string;
|
|
26
|
+
export declare function buildToolResumePrompt(userText: string, pendingAssistantSummary: string, toolResults: ToolResultInfo[]): string;
|
|
27
|
+
export declare function buildInitialHandoffPrompt(userText: string, turns: Array<{
|
|
28
|
+
userText: string;
|
|
29
|
+
assistantText: string;
|
|
30
|
+
}>, pendingAssistantSummary: string, toolResults: ToolResultInfo[]): string;
|
|
31
|
+
export declare function buildTitleSourceText(userText: string, turns: Array<{
|
|
32
|
+
userText: string;
|
|
33
|
+
assistantText: string;
|
|
34
|
+
}>, pendingAssistantSummary: string, toolResults: ToolResultInfo[]): string;
|
|
35
|
+
export declare function detectTitleRequest(body: ChatCompletionRequest): {
|
|
36
|
+
matched: boolean;
|
|
37
|
+
reason: string;
|
|
38
|
+
};
|
|
39
|
+
export {};
|