@myrialabs/clopen 0.4.14 → 0.4.16
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/backend/artifacts/matrix.ts +8 -0
- package/backend/artifacts/types.ts +2 -2
- package/backend/auth/permissions.ts +16 -0
- package/backend/chat/stream-manager.ts +5 -5
- package/backend/commands/detect.ts +1 -1
- package/backend/commands/sync.ts +1 -1
- package/backend/database/migrations/056_support_mssql_driver.ts +137 -0
- package/backend/database/migrations/057_seed_pi_provider.ts +25 -0
- package/backend/database/migrations/058_seed_cline_provider.ts +25 -0
- package/backend/database/migrations/059_seed_cursor_provider.ts +25 -0
- package/backend/database/migrations/index.ts +28 -0
- package/backend/database/queries/engine-queries.ts +13 -0
- package/backend/database/queries/message-queries.ts +43 -1
- package/backend/db-client/connection-manager.ts +8 -2
- package/backend/db-client/drivers/mssql.ts +829 -0
- package/backend/db-client/drivers/mysql.ts +33 -4
- package/backend/db-client/drivers/postgres.ts +55 -5
- package/backend/db-client/drivers/sql-builders.test.ts +70 -0
- package/backend/db-client/drivers/sql-builders.ts +9 -2
- package/backend/db-client/pool-config.ts +32 -0
- package/backend/db-client/query-executor.test.ts +42 -0
- package/backend/db-client/query-executor.ts +47 -3
- package/backend/engine/README.md +36 -13
- package/backend/engine/adapters/cline/agent-tool.ts +64 -0
- package/backend/engine/adapters/cline/ask-question-tool.ts +105 -0
- package/backend/engine/adapters/cline/credential.ts +166 -0
- package/backend/engine/adapters/cline/error-handler.ts +49 -0
- package/backend/engine/adapters/cline/index.ts +1 -0
- package/backend/engine/adapters/cline/mcp-tools.ts +119 -0
- package/backend/engine/adapters/cline/message-converter.ts +396 -0
- package/backend/engine/adapters/cline/models.ts +73 -0
- package/backend/engine/adapters/cline/presets.ts +128 -0
- package/backend/engine/adapters/cline/stream.ts +413 -0
- package/backend/engine/adapters/codex/credential.ts +1 -1
- package/backend/engine/adapters/codex/message-converter.ts +4 -4
- package/backend/engine/adapters/codex/session-fork.ts +1 -1
- package/backend/engine/adapters/codex/stream.ts +3 -3
- package/backend/engine/adapters/copilot/stream.ts +1 -1
- package/backend/engine/adapters/cursor/ask-question-tool.ts +98 -0
- package/backend/engine/adapters/cursor/credential.ts +68 -0
- package/backend/engine/adapters/cursor/environment.ts +32 -0
- package/backend/engine/adapters/cursor/error-handler.ts +60 -0
- package/backend/engine/adapters/cursor/index.ts +1 -0
- package/backend/engine/adapters/cursor/message-converter.ts +615 -0
- package/backend/engine/adapters/cursor/models.ts +68 -0
- package/backend/engine/adapters/cursor/session-fork.ts +68 -0
- package/backend/engine/adapters/cursor/stream.ts +341 -0
- package/backend/engine/adapters/opencode/config.ts +9 -0
- package/backend/engine/adapters/opencode/models.ts +10 -1
- package/backend/engine/adapters/opencode/server.ts +110 -2
- package/backend/engine/adapters/pi/agent-tool.ts +62 -0
- package/backend/engine/adapters/pi/ask-question-tool.ts +93 -0
- package/backend/engine/adapters/pi/credential.ts +137 -0
- package/backend/engine/adapters/pi/environment.ts +33 -0
- package/backend/engine/adapters/pi/error-handler.ts +46 -0
- package/backend/engine/adapters/pi/index.ts +1 -0
- package/backend/engine/adapters/pi/mcp-tools.ts +131 -0
- package/backend/engine/adapters/pi/message-converter.ts +446 -0
- package/backend/engine/adapters/pi/models.ts +53 -0
- package/backend/engine/adapters/pi/presets.ts +133 -0
- package/backend/engine/adapters/pi/session-fork.ts +46 -0
- package/backend/engine/adapters/pi/stream.ts +427 -0
- package/backend/engine/adapters/qwen/message-converter.ts +5 -5
- package/backend/engine/adapters/qwen/stream.ts +2 -2
- package/backend/engine/docs/adapter-contract.md +7 -2
- package/backend/engine/docs/adding-an-engine.md +70 -12
- package/backend/engine/docs/artifacts.md +124 -0
- package/backend/engine/docs/database.md +4 -4
- package/backend/engine/docs/frontend-and-chat.md +22 -4
- package/backend/engine/docs/lessons-learned.md +219 -32
- package/backend/engine/docs/quick-reference.md +18 -7
- package/backend/engine/docs/websocket-routes.md +2 -2
- package/backend/engine/index.ts +9 -0
- package/backend/index.ts +6 -0
- package/backend/mcp/README.md +3 -3
- package/backend/mcp/external/config.ts +11 -0
- package/backend/mcp/index.ts +8 -0
- package/backend/mcp/internal/config.ts +33 -1
- package/backend/permissions/service.ts +5 -2
- package/backend/preview/browser/browser-preview-service.ts +15 -6
- package/backend/preview/browser/browser-video-capture.ts +232 -100
- package/backend/preview/browser/scripts/video-stream.ts +216 -56
- package/backend/preview/browser/types.ts +24 -12
- package/backend/skills/sync.ts +1 -1
- package/backend/subagents/detect.ts +1 -1
- package/backend/subagents/sync.ts +1 -1
- package/backend/ws/chat/stream.ts +13 -5
- package/backend/ws/db-client/connections.ts +2 -1
- package/backend/ws/db-client/query.ts +41 -4
- package/backend/ws/db-client/schema.ts +45 -1
- package/backend/ws/engine/claude/accounts.ts +37 -13
- package/backend/ws/engine/cline/accounts.ts +317 -0
- package/backend/ws/engine/cline/index.ts +15 -0
- package/backend/ws/engine/cline/presets.ts +36 -0
- package/backend/ws/engine/cline/status.ts +49 -0
- package/backend/ws/engine/codex/accounts.ts +37 -5
- package/backend/ws/engine/copilot/accounts.ts +12 -0
- package/backend/ws/engine/cursor/accounts.ts +162 -0
- package/backend/ws/engine/cursor/index.ts +13 -0
- package/backend/ws/engine/cursor/status.ts +55 -0
- package/backend/ws/engine/index.ts +6 -0
- package/backend/ws/engine/opencode/providers.ts +72 -1
- package/backend/ws/engine/pi/accounts.ts +319 -0
- package/backend/ws/engine/pi/index.ts +15 -0
- package/backend/ws/engine/pi/presets.ts +34 -0
- package/backend/ws/engine/pi/status.ts +50 -0
- package/backend/ws/engine/qwen/accounts.ts +22 -0
- package/backend/ws/messages/crud.ts +13 -0
- package/backend/ws/preview/browser/interact.ts +10 -8
- package/backend/ws/preview/browser/webcodecs.ts +26 -2
- package/backend/ws/sessions/crud.ts +2 -2
- package/backend/ws/settings/crud.ts +2 -2
- package/dist/assets/{index-CDZf6lfv.js → index-BPvctwJ2.js} +1 -1
- package/dist/assets/{index-BWf-DDJN.js → index-vLKZpWQH.js} +1 -1
- package/dist/assets/{main-C15mQTCW.js → main-BtBZvCwv.js} +428 -386
- package/dist/assets/main-D7ATW8pp.css +1 -0
- package/dist/index.html +2 -2
- package/package.json +11 -1
- package/shared/constants/engine-tools.ts +16 -2
- package/shared/constants/engines.ts +27 -0
- package/shared/types/db-client/connection.ts +1 -1
- package/shared/types/db-client/query.ts +4 -1
- package/shared/types/unified/common.ts +1 -1
- package/shared/types/unified/engine.ts +82 -1
- package/shared/types/unified/index.ts +6 -0
- package/shared/types/unified/message.ts +1 -1
- package/shared/utils/db-client/split-sql.test.ts +13 -0
- package/shared/utils/db-client/split-sql.ts +38 -1
- package/shared/utils/subagent-wire-trim.ts +87 -0
- package/dist/assets/main-Ba5ZGlY4.css +0 -1
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* OpenAI Codex Account Management Handlers
|
|
3
3
|
*
|
|
4
|
-
* Codex supports TWO auth modes (per backend/engine/README.md §
|
|
4
|
+
* Codex supports TWO auth modes (per backend/engine/README.md §10.13):
|
|
5
5
|
*
|
|
6
6
|
* 1. **API key** — paste-token flow, identical to Copilot's. The `apiKey`
|
|
7
7
|
* is wrapped as `{kind:"api_key", apiKey}` and stored in
|
|
@@ -93,6 +93,8 @@ interface CodexLoginProcess {
|
|
|
93
93
|
userId: string;
|
|
94
94
|
accountName: string;
|
|
95
95
|
deviceAuth: boolean;
|
|
96
|
+
/** When set, re-authenticate this existing account in place instead of creating a new one. */
|
|
97
|
+
reauthAccountId: number | null;
|
|
96
98
|
timer: ReturnType<typeof setTimeout>;
|
|
97
99
|
}
|
|
98
100
|
|
|
@@ -157,7 +159,7 @@ function isLoginSuccess(buffer: string): boolean {
|
|
|
157
159
|
return /Successfully logged in/i.test(buffer);
|
|
158
160
|
}
|
|
159
161
|
|
|
160
|
-
function persistChatGptLoginResult(accountName: string): { ok: true; accountId: number } | { ok: false; error: string } {
|
|
162
|
+
function persistChatGptLoginResult(accountName: string, reauthAccountId?: number | null): { ok: true; accountId: number } | { ok: false; error: string } {
|
|
161
163
|
const provider = engineQueries.getProviderBySlug('codex', 'openai');
|
|
162
164
|
if (!provider) return { ok: false, error: 'OpenAI Codex provider not found in DB' };
|
|
163
165
|
|
|
@@ -167,6 +169,17 @@ function persistChatGptLoginResult(accountName: string): { ok: true; accountId:
|
|
|
167
169
|
}
|
|
168
170
|
|
|
169
171
|
const credential = serializeCodexCredential({ kind: 'chatgpt', authJson });
|
|
172
|
+
|
|
173
|
+
// Re-authentication: replace the existing account's blob in place so its
|
|
174
|
+
// name and active state survive.
|
|
175
|
+
if (reauthAccountId != null) {
|
|
176
|
+
const existing = engineQueries.getAccount(reauthAccountId);
|
|
177
|
+
if (!existing) return { ok: false, error: 'Account to re-authenticate not found' };
|
|
178
|
+
engineQueries.updateAccountCredential(reauthAccountId, credential);
|
|
179
|
+
if (accountName) engineQueries.renameAccount(reauthAccountId, accountName);
|
|
180
|
+
return { ok: true, accountId: reauthAccountId };
|
|
181
|
+
}
|
|
182
|
+
|
|
170
183
|
const account = engineQueries.createAccount(provider.id, accountName, credential);
|
|
171
184
|
return { ok: true, accountId: account.id };
|
|
172
185
|
}
|
|
@@ -285,6 +298,23 @@ export const codexAccountsHandler = createRouter()
|
|
|
285
298
|
return { success: true };
|
|
286
299
|
})
|
|
287
300
|
|
|
301
|
+
// Replace the API key for an api_key-mode account. ChatGPT accounts have no
|
|
302
|
+
// editable key — they re-authenticate via the login flow instead.
|
|
303
|
+
.http('engine:codex-accounts-update-api-key', {
|
|
304
|
+
data: t.Object({ id: t.Number(), apiKey: t.String({ minLength: 1 }) }),
|
|
305
|
+
response: t.Object({ success: t.Boolean() })
|
|
306
|
+
}, async ({ data }) => {
|
|
307
|
+
const account = engineQueries.getAccount(data.id);
|
|
308
|
+
if (!account) throw new Error('Account not found');
|
|
309
|
+
if (authModeOf(account) !== 'api_key') {
|
|
310
|
+
throw new Error('Only API-key accounts can have their key edited; ChatGPT accounts re-authenticate instead');
|
|
311
|
+
}
|
|
312
|
+
engineQueries.updateAccountCredential(data.id, serializeCodexCredential({ kind: 'api_key', apiKey: data.apiKey.trim() }));
|
|
313
|
+
const active = engineQueries.getActiveAccountForEngine('codex');
|
|
314
|
+
if (active?.id === data.id) await disposeCodexEngines();
|
|
315
|
+
return { success: true };
|
|
316
|
+
})
|
|
317
|
+
|
|
288
318
|
// ─── ChatGPT browser OAuth flow ───
|
|
289
319
|
//
|
|
290
320
|
// Spawn `codex login` in a PTY (the CLI silently no-ops when stdout
|
|
@@ -294,7 +324,8 @@ export const codexAccountsHandler = createRouter()
|
|
|
294
324
|
.on('engine:codex-account-setup-start', {
|
|
295
325
|
data: t.Object({
|
|
296
326
|
name: t.String({ minLength: 1 }),
|
|
297
|
-
deviceAuth: t.Optional(t.Boolean())
|
|
327
|
+
deviceAuth: t.Optional(t.Boolean()),
|
|
328
|
+
reauthAccountId: t.Optional(t.Number())
|
|
298
329
|
})
|
|
299
330
|
}, async ({ data, conn }) => {
|
|
300
331
|
const userId = ws.getUserId(conn);
|
|
@@ -363,6 +394,7 @@ export const codexAccountsHandler = createRouter()
|
|
|
363
394
|
userId,
|
|
364
395
|
accountName: data.name.trim(),
|
|
365
396
|
deviceAuth: !!data.deviceAuth,
|
|
397
|
+
reauthAccountId: data.reauthAccountId ?? null,
|
|
366
398
|
timer,
|
|
367
399
|
};
|
|
368
400
|
setupProcesses.set(setupId, entry);
|
|
@@ -409,7 +441,7 @@ export const codexAccountsHandler = createRouter()
|
|
|
409
441
|
if (isLoginSuccess(clean) && !entry.completed) {
|
|
410
442
|
entry.completed = true;
|
|
411
443
|
debug.log('engine', `[${setupId}] Codex login success — persisting auth.json`);
|
|
412
|
-
const result = persistChatGptLoginResult(entry.accountName);
|
|
444
|
+
const result = persistChatGptLoginResult(entry.accountName, entry.reauthAccountId);
|
|
413
445
|
if (result.ok) {
|
|
414
446
|
disposeCodexEngines().finally(() => {
|
|
415
447
|
ws.emit.user(userId, 'engine:codex-account-setup-complete', {
|
|
@@ -438,7 +470,7 @@ export const codexAccountsHandler = createRouter()
|
|
|
438
470
|
// in the final chunk right before exit.
|
|
439
471
|
if (isLoginSuccess(clean)) {
|
|
440
472
|
entry.completed = true;
|
|
441
|
-
const result = persistChatGptLoginResult(entry.accountName);
|
|
473
|
+
const result = persistChatGptLoginResult(entry.accountName, entry.reauthAccountId);
|
|
442
474
|
if (result.ok) {
|
|
443
475
|
disposeCodexEngines().finally(() => {
|
|
444
476
|
ws.emit.user(userId, 'engine:codex-account-setup-complete', {
|
|
@@ -111,6 +111,18 @@ export const copilotAccountsHandler = createRouter()
|
|
|
111
111
|
return { success: true };
|
|
112
112
|
})
|
|
113
113
|
|
|
114
|
+
// Replace the stored PAT for an account. When the edited account is the
|
|
115
|
+
// active one, drop engine instances so the next stream picks up the token.
|
|
116
|
+
.http('engine:copilot-accounts-update-token', {
|
|
117
|
+
data: t.Object({ id: t.Number(), token: t.String({ minLength: 1 }) }),
|
|
118
|
+
response: t.Object({ success: t.Boolean() })
|
|
119
|
+
}, async ({ data }) => {
|
|
120
|
+
engineQueries.updateAccountCredential(data.id, data.token.trim());
|
|
121
|
+
const active = engineQueries.getActiveAccountForEngine('copilot');
|
|
122
|
+
if (active?.id === data.id) await disposeCopilotEngines();
|
|
123
|
+
return { success: true };
|
|
124
|
+
})
|
|
125
|
+
|
|
114
126
|
// Restart all Copilot engine instances. Use after changing the active token
|
|
115
127
|
// so subsequent models:list / chat calls re-initialise with fresh credentials.
|
|
116
128
|
.http('engine:copilot-restart', {
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cursor Account Management Handlers
|
|
3
|
+
*
|
|
4
|
+
* Auth model: API-key only. The user pastes a Cursor API key (user key from the
|
|
5
|
+
* dashboard, or a team service-account key); no PTY / device flow. The key is
|
|
6
|
+
* validated with `Cursor.me({ apiKey })` before it is stored as a JSON credential
|
|
7
|
+
* (`{ "apiKey": "…" }`) on engine_accounts under provider slug 'cursor'.
|
|
8
|
+
*
|
|
9
|
+
* Switching/deleting the active account disposes the project-scoped CursorEngine
|
|
10
|
+
* instances so the next stream picks up the new credential.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { t } from 'elysia';
|
|
14
|
+
import { Cursor } from '@cursor/sdk';
|
|
15
|
+
import { createRouter } from '$shared/utils/ws-server';
|
|
16
|
+
import { engineQueries } from '../../../database/queries';
|
|
17
|
+
import { disposeAllProjectEnginesByType } from '../../../engine';
|
|
18
|
+
import { serializeCursorCredential } from '../../../engine/adapters/cursor/credential';
|
|
19
|
+
import { debug } from '$shared/utils/logger';
|
|
20
|
+
|
|
21
|
+
async function disposeCursorEngines(): Promise<void> {
|
|
22
|
+
try {
|
|
23
|
+
await disposeAllProjectEnginesByType('cursor');
|
|
24
|
+
} catch {
|
|
25
|
+
/* engine may not be initialised — ignore */
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Validate an API key by resolving the authenticated user. Only a genuine
|
|
31
|
+
* authentication failure (401 / `AuthenticationError`) means the key is bad —
|
|
32
|
+
* `Cursor.me()` also 403s with `plan_required` for free-tier keys (which are
|
|
33
|
+
* perfectly valid), and transient network errors shouldn't block saving a
|
|
34
|
+
* working key. So we reject ONLY on an auth error and accept everything else.
|
|
35
|
+
*/
|
|
36
|
+
async function validateApiKey(apiKey: string): Promise<void> {
|
|
37
|
+
try {
|
|
38
|
+
await Cursor.me({ apiKey });
|
|
39
|
+
} catch (error) {
|
|
40
|
+
const status = (error as { status?: number })?.status;
|
|
41
|
+
const name = (error as Error)?.name;
|
|
42
|
+
if (name === 'AuthenticationError' || status === 401) {
|
|
43
|
+
throw new Error('Invalid Cursor API key. Check the key from cursor.com/dashboard and try again.');
|
|
44
|
+
}
|
|
45
|
+
debug.warn('engine', `Cursor key validation non-fatal (${name ?? 'error'} ${status ?? ''}) — accepting key`);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export const cursorAccountsHandler = createRouter()
|
|
50
|
+
|
|
51
|
+
.http('engine:cursor-accounts-list', {
|
|
52
|
+
data: t.Object({}),
|
|
53
|
+
response: t.Object({
|
|
54
|
+
accounts: t.Array(t.Object({
|
|
55
|
+
id: t.Number(),
|
|
56
|
+
name: t.String(),
|
|
57
|
+
isActive: t.Boolean(),
|
|
58
|
+
createdAt: t.String()
|
|
59
|
+
}))
|
|
60
|
+
})
|
|
61
|
+
}, async () => {
|
|
62
|
+
const provider = engineQueries.getProviderBySlug('cursor', 'cursor');
|
|
63
|
+
if (!provider) return { accounts: [] };
|
|
64
|
+
const accounts = engineQueries.getAccountsByProvider(provider.id);
|
|
65
|
+
return {
|
|
66
|
+
accounts: accounts.map(a => ({
|
|
67
|
+
id: a.id,
|
|
68
|
+
name: a.name,
|
|
69
|
+
isActive: a.is_active === 1,
|
|
70
|
+
createdAt: a.created_at
|
|
71
|
+
}))
|
|
72
|
+
};
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
.http('engine:cursor-accounts-add', {
|
|
76
|
+
data: t.Object({
|
|
77
|
+
name: t.String({ minLength: 1 }),
|
|
78
|
+
apiKey: t.String({ minLength: 1 })
|
|
79
|
+
}),
|
|
80
|
+
response: t.Object({
|
|
81
|
+
account: t.Object({
|
|
82
|
+
id: t.Number(),
|
|
83
|
+
name: t.String(),
|
|
84
|
+
isActive: t.Boolean(),
|
|
85
|
+
createdAt: t.String()
|
|
86
|
+
})
|
|
87
|
+
})
|
|
88
|
+
}, async ({ data }) => {
|
|
89
|
+
const provider = engineQueries.getProviderBySlug('cursor', 'cursor');
|
|
90
|
+
if (!provider) {
|
|
91
|
+
throw new Error('Cursor provider not found in database');
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const apiKey = data.apiKey.trim();
|
|
95
|
+
await validateApiKey(apiKey);
|
|
96
|
+
|
|
97
|
+
const account = engineQueries.createAccount(provider.id, data.name.trim(), serializeCursorCredential({ apiKey }));
|
|
98
|
+
|
|
99
|
+
if (account.is_active === 1) {
|
|
100
|
+
await disposeCursorEngines();
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return {
|
|
104
|
+
account: {
|
|
105
|
+
id: account.id,
|
|
106
|
+
name: account.name,
|
|
107
|
+
isActive: account.is_active === 1,
|
|
108
|
+
createdAt: account.created_at
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
.http('engine:cursor-accounts-switch', {
|
|
114
|
+
data: t.Object({ id: t.Number() }),
|
|
115
|
+
response: t.Object({ success: t.Boolean() })
|
|
116
|
+
}, async ({ data }) => {
|
|
117
|
+
engineQueries.switchAccount(data.id);
|
|
118
|
+
await disposeCursorEngines();
|
|
119
|
+
return { success: true };
|
|
120
|
+
})
|
|
121
|
+
|
|
122
|
+
.http('engine:cursor-accounts-delete', {
|
|
123
|
+
data: t.Object({ id: t.Number() }),
|
|
124
|
+
response: t.Object({ success: t.Boolean() })
|
|
125
|
+
}, async ({ data }) => {
|
|
126
|
+
const active = engineQueries.getActiveAccountForEngine('cursor');
|
|
127
|
+
engineQueries.deleteAccount(data.id);
|
|
128
|
+
if (active?.id === data.id) await disposeCursorEngines();
|
|
129
|
+
return { success: true };
|
|
130
|
+
})
|
|
131
|
+
|
|
132
|
+
.http('engine:cursor-accounts-rename', {
|
|
133
|
+
data: t.Object({ id: t.Number(), name: t.String({ minLength: 1 }) }),
|
|
134
|
+
response: t.Object({ success: t.Boolean() })
|
|
135
|
+
}, async ({ data }) => {
|
|
136
|
+
engineQueries.renameAccount(data.id, data.name.trim());
|
|
137
|
+
return { success: true };
|
|
138
|
+
})
|
|
139
|
+
|
|
140
|
+
// Replace the stored API key for an account. When the edited account is the
|
|
141
|
+
// active one, drop engine instances so the next stream picks up the key.
|
|
142
|
+
.http('engine:cursor-accounts-update-key', {
|
|
143
|
+
data: t.Object({ id: t.Number(), apiKey: t.String({ minLength: 1 }) }),
|
|
144
|
+
response: t.Object({ success: t.Boolean() })
|
|
145
|
+
}, async ({ data }) => {
|
|
146
|
+
const apiKey = data.apiKey.trim();
|
|
147
|
+
await validateApiKey(apiKey);
|
|
148
|
+
engineQueries.updateAccountCredential(data.id, serializeCursorCredential({ apiKey }));
|
|
149
|
+
const active = engineQueries.getActiveAccountForEngine('cursor');
|
|
150
|
+
if (active?.id === data.id) await disposeCursorEngines();
|
|
151
|
+
return { success: true };
|
|
152
|
+
})
|
|
153
|
+
|
|
154
|
+
// Restart all Cursor engine instances so subsequent models:list / chat calls
|
|
155
|
+
// re-initialise with fresh credentials.
|
|
156
|
+
.http('engine:cursor-restart', {
|
|
157
|
+
data: t.Object({}),
|
|
158
|
+
response: t.Object({ success: t.Boolean() })
|
|
159
|
+
}, async () => {
|
|
160
|
+
await disposeCursorEngines();
|
|
161
|
+
return { success: true };
|
|
162
|
+
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cursor Engine Router
|
|
3
|
+
*
|
|
4
|
+
* Combines status detection and account management handlers.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { createRouter } from '$shared/utils/ws-server';
|
|
8
|
+
import { cursorStatusHandler } from './status';
|
|
9
|
+
import { cursorAccountsHandler } from './accounts';
|
|
10
|
+
|
|
11
|
+
export const cursorEngineRouter = createRouter()
|
|
12
|
+
.merge(cursorStatusHandler)
|
|
13
|
+
.merge(cursorAccountsHandler);
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cursor Engine Status Handler
|
|
3
|
+
*
|
|
4
|
+
* Reports SDK availability and active account state. `@cursor/sdk` is bundled
|
|
5
|
+
* with Clopen and runs its local agent in-process, so there is nothing to
|
|
6
|
+
* install — `installed` is always true.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { t } from 'elysia';
|
|
10
|
+
import { createRouter } from '$shared/utils/ws-server';
|
|
11
|
+
import { engineQueries } from '../../../database/queries';
|
|
12
|
+
import { debug } from '$shared/utils/logger';
|
|
13
|
+
import { getBackendOS } from '../../../utils/os';
|
|
14
|
+
|
|
15
|
+
function readSdkVersion(): string | null {
|
|
16
|
+
try {
|
|
17
|
+
const path = require.resolve('@cursor/sdk/package.json');
|
|
18
|
+
const pkg = require(path) as { version?: string };
|
|
19
|
+
return pkg.version ?? null;
|
|
20
|
+
} catch {
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export const cursorStatusHandler = createRouter()
|
|
26
|
+
.http('engine:cursor-status', {
|
|
27
|
+
data: t.Object({}),
|
|
28
|
+
response: t.Object({
|
|
29
|
+
installed: t.Boolean(),
|
|
30
|
+
version: t.Union([t.String(), t.Null()]),
|
|
31
|
+
activeAccount: t.Union([
|
|
32
|
+
t.Object({
|
|
33
|
+
id: t.Number(),
|
|
34
|
+
name: t.String()
|
|
35
|
+
}),
|
|
36
|
+
t.Null()
|
|
37
|
+
]),
|
|
38
|
+
accountsCount: t.Number(),
|
|
39
|
+
backendOS: t.Union([t.Literal('windows'), t.Literal('macos'), t.Literal('linux')])
|
|
40
|
+
})
|
|
41
|
+
}, async () => {
|
|
42
|
+
debug.log('engine', 'Checking Cursor status...');
|
|
43
|
+
|
|
44
|
+
const provider = engineQueries.getProviderBySlug('cursor', 'cursor');
|
|
45
|
+
const accounts = provider ? engineQueries.getAccountsByProvider(provider.id) : [];
|
|
46
|
+
const activeAccount = engineQueries.getActiveAccountForEngine('cursor');
|
|
47
|
+
|
|
48
|
+
return {
|
|
49
|
+
installed: true,
|
|
50
|
+
version: readSdkVersion(),
|
|
51
|
+
activeAccount: activeAccount ? { id: activeAccount.id, name: activeAccount.name } : null,
|
|
52
|
+
accountsCount: accounts.length,
|
|
53
|
+
backendOS: getBackendOS()
|
|
54
|
+
};
|
|
55
|
+
});
|
|
@@ -11,6 +11,9 @@ import { openCodeEngineRouter } from './opencode';
|
|
|
11
11
|
import { copilotEngineRouter } from './copilot';
|
|
12
12
|
import { codexEngineRouter } from './codex';
|
|
13
13
|
import { qwenEngineRouter } from './qwen';
|
|
14
|
+
import { piEngineRouter } from './pi';
|
|
15
|
+
import { clineEngineRouter } from './cline';
|
|
16
|
+
import { cursorEngineRouter } from './cursor';
|
|
14
17
|
import { engineRestartRouter } from './restart';
|
|
15
18
|
|
|
16
19
|
export const engineRouter = createRouter()
|
|
@@ -19,4 +22,7 @@ export const engineRouter = createRouter()
|
|
|
19
22
|
.merge(copilotEngineRouter)
|
|
20
23
|
.merge(codexEngineRouter)
|
|
21
24
|
.merge(qwenEngineRouter)
|
|
25
|
+
.merge(piEngineRouter)
|
|
26
|
+
.merge(clineEngineRouter)
|
|
27
|
+
.merge(cursorEngineRouter)
|
|
22
28
|
.merge(engineRestartRouter);
|
|
@@ -77,7 +77,10 @@ export const openCodeProviderHandler = createRouter()
|
|
|
77
77
|
apiUrl: t.Optional(t.String()),
|
|
78
78
|
options: t.Optional(t.String()),
|
|
79
79
|
accountName: t.String({ minLength: 1 }),
|
|
80
|
-
|
|
80
|
+
// Empty string allowed: custom OpenAI-compatible endpoints (e.g. a
|
|
81
|
+
// local LLM) often need no API key. The frontend still requires a
|
|
82
|
+
// credential for catalog providers via its own validation.
|
|
83
|
+
credential: t.String(),
|
|
81
84
|
}),
|
|
82
85
|
response: t.Object({
|
|
83
86
|
provider: ProviderSchema
|
|
@@ -133,6 +136,36 @@ export const openCodeProviderHandler = createRouter()
|
|
|
133
136
|
return { success: true };
|
|
134
137
|
})
|
|
135
138
|
|
|
139
|
+
.http('engine:opencode-provider-update', {
|
|
140
|
+
data: t.Object({
|
|
141
|
+
id: t.Number(),
|
|
142
|
+
slug: t.Optional(t.String({ minLength: 1 })),
|
|
143
|
+
name: t.Optional(t.String()),
|
|
144
|
+
apiUrl: t.Optional(t.String()),
|
|
145
|
+
options: t.Optional(t.String()),
|
|
146
|
+
}),
|
|
147
|
+
response: t.Object({ success: t.Boolean() })
|
|
148
|
+
}, async ({ data }) => {
|
|
149
|
+
if (data.options !== undefined) {
|
|
150
|
+
try { JSON.parse(data.options); } catch { throw new Error('Invalid JSON for options'); }
|
|
151
|
+
}
|
|
152
|
+
// Changing a provider's slug rewrites its identity in the generated
|
|
153
|
+
// opencode config — reject collisions so two providers can't share one.
|
|
154
|
+
if (data.slug !== undefined) {
|
|
155
|
+
const existing = engineQueries.getProviderBySlug('opencode', data.slug);
|
|
156
|
+
if (existing && existing.id !== data.id) {
|
|
157
|
+
throw new Error(`Provider "${data.slug}" already configured`);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
engineQueries.updateProvider(data.id, {
|
|
161
|
+
slug: data.slug,
|
|
162
|
+
name: data.name,
|
|
163
|
+
apiUrl: data.apiUrl,
|
|
164
|
+
options: data.options,
|
|
165
|
+
});
|
|
166
|
+
return { success: true };
|
|
167
|
+
})
|
|
168
|
+
|
|
136
169
|
.http('engine:opencode-provider-toggle', {
|
|
137
170
|
data: t.Object({ id: t.Number(), enabled: t.Boolean() }),
|
|
138
171
|
response: t.Object({ success: t.Boolean() })
|
|
@@ -201,6 +234,15 @@ export const openCodeProviderHandler = createRouter()
|
|
|
201
234
|
return { success: true };
|
|
202
235
|
})
|
|
203
236
|
|
|
237
|
+
.http('engine:opencode-account-update-credential', {
|
|
238
|
+
// Empty string allowed — a keyless custom endpoint may clear its key.
|
|
239
|
+
data: t.Object({ accountId: t.Number(), credential: t.String() }),
|
|
240
|
+
response: t.Object({ success: t.Boolean() })
|
|
241
|
+
}, async ({ data }) => {
|
|
242
|
+
engineQueries.updateAccountCredential(data.accountId, data.credential);
|
|
243
|
+
return { success: true };
|
|
244
|
+
})
|
|
245
|
+
|
|
204
246
|
// ═══════════════════════════════════════
|
|
205
247
|
// Models.dev Catalog
|
|
206
248
|
// ═══════════════════════════════════════
|
|
@@ -250,6 +292,35 @@ export const openCodeProviderHandler = createRouter()
|
|
|
250
292
|
return { catalog, cachedAt: new Date().toISOString() };
|
|
251
293
|
})
|
|
252
294
|
|
|
295
|
+
// ═══════════════════════════════════════
|
|
296
|
+
// Model Discovery
|
|
297
|
+
// ═══════════════════════════════════════
|
|
298
|
+
|
|
299
|
+
.http('engine:opencode-provider-fetch-models', {
|
|
300
|
+
data: t.Object({ id: t.Number() }),
|
|
301
|
+
response: t.Object({
|
|
302
|
+
models: t.Array(t.Object({ id: t.String(), name: t.Optional(t.String()) }))
|
|
303
|
+
})
|
|
304
|
+
}, async ({ data }) => {
|
|
305
|
+
const providers = engineQueries.getProvidersWithAccounts('opencode');
|
|
306
|
+
const provider = providers.find(p => p.id === data.id);
|
|
307
|
+
if (!provider) throw new Error('Provider not found');
|
|
308
|
+
if (!provider.api_url) throw new Error('Provider has no API URL');
|
|
309
|
+
|
|
310
|
+
const baseUrl = provider.api_url.replace(/\/+$/, '');
|
|
311
|
+
const activeAccount = provider.accounts.find(a => a.is_active === 1) || provider.accounts[0];
|
|
312
|
+
const headers: Record<string, string> = { 'Content-Type': 'application/json' };
|
|
313
|
+
if (activeAccount?.credential) {
|
|
314
|
+
headers['Authorization'] = `Bearer ${activeAccount.credential}`;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
const res = await fetch(`${baseUrl}/models`, { headers, signal: AbortSignal.timeout(5000) });
|
|
318
|
+
if (!res.ok) throw new Error(`HTTP ${res.status}`);
|
|
319
|
+
const body = await res.json() as { data?: { id: string; name?: string }[] };
|
|
320
|
+
const models = (body.data || []).map(m => ({ id: m.id, name: m.name }));
|
|
321
|
+
return { models };
|
|
322
|
+
})
|
|
323
|
+
|
|
253
324
|
// ═══════════════════════════════════════
|
|
254
325
|
// Server Restart
|
|
255
326
|
// ═══════════════════════════════════════
|