@myrialabs/clopen 0.3.8 → 0.3.10
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 +22 -19
- package/backend/auth/auth-service.ts +56 -1
- package/backend/auth/permissions.ts +66 -2
- package/backend/auth/session-cleanup.ts +58 -0
- package/backend/chat/stream-manager.ts +143 -13
- package/backend/database/migrations/032_seed_copilot_provider.ts +19 -0
- package/backend/database/migrations/033_seed_codex_provider.ts +19 -0
- package/backend/database/migrations/034_seed_qwen_provider.ts +119 -0
- package/backend/database/migrations/035_add_owner_to_db_client_connections.ts +34 -0
- package/backend/database/migrations/index.ts +29 -1
- package/backend/database/queries/db-client-connection-queries.ts +160 -54
- package/backend/database/queries/engine-queries.ts +12 -0
- package/backend/database/queries/project-queries.ts +10 -5
- package/backend/db-client/connection-manager.ts +9 -3
- package/backend/engine/README.md +1702 -0
- package/backend/engine/adapters/claude/models.ts +141 -0
- package/backend/engine/adapters/claude/stream.ts +1 -1
- package/backend/engine/adapters/codex/credential.ts +179 -0
- package/backend/engine/adapters/codex/error-handler.ts +65 -0
- package/backend/engine/adapters/codex/index.ts +1 -0
- package/backend/engine/adapters/codex/message-converter.ts +646 -0
- package/backend/engine/adapters/codex/models.ts +105 -0
- package/backend/engine/adapters/codex/patch-rollout.ts +169 -0
- package/backend/engine/adapters/codex/session-fork.ts +149 -0
- package/backend/engine/adapters/codex/stream.ts +310 -0
- package/backend/engine/adapters/codex/usage-rollout.ts +145 -0
- package/backend/engine/adapters/copilot/error-handler.ts +94 -0
- package/backend/engine/adapters/copilot/index.ts +1 -0
- package/backend/engine/adapters/copilot/message-converter.ts +973 -0
- package/backend/engine/adapters/copilot/models.ts +92 -0
- package/backend/engine/adapters/copilot/session-fork.ts +111 -0
- package/backend/engine/adapters/copilot/stream.ts +570 -0
- package/backend/engine/adapters/opencode/config.ts +56 -104
- package/backend/engine/adapters/opencode/error-handler.ts +71 -0
- package/backend/engine/adapters/opencode/models.ts +79 -0
- package/backend/engine/adapters/opencode/presets.ts +91 -0
- package/backend/engine/adapters/opencode/stream.ts +8 -114
- package/backend/engine/adapters/qwen/credential.ts +78 -0
- package/backend/engine/adapters/qwen/environment.ts +84 -0
- package/backend/engine/adapters/qwen/error-handler.ts +71 -0
- package/backend/engine/adapters/qwen/index.ts +1 -0
- package/backend/engine/adapters/qwen/message-converter.ts +1055 -0
- package/backend/engine/adapters/qwen/models.ts +81 -0
- package/backend/engine/adapters/qwen/presets.ts +51 -0
- package/backend/engine/adapters/qwen/session-fork.ts +97 -0
- package/backend/engine/adapters/qwen/stream.ts +379 -0
- package/backend/engine/index.ts +35 -0
- package/backend/engine/install-recipes.ts +80 -1
- package/backend/engine/install-runner.ts +4 -0
- package/backend/files/path-browsing.ts +20 -0
- package/backend/git/git-service.ts +81 -11
- package/backend/git/git-spawn-validation.test.ts +75 -0
- package/backend/git/git-spawn-validation.ts +77 -0
- package/backend/index.ts +5 -0
- package/backend/mcp/README.md +84 -14
- package/backend/mcp/config.ts +218 -17
- package/backend/mcp/index.ts +2 -0
- package/backend/mcp/project-context.ts +55 -0
- package/backend/mcp/servers/browser-automation/actions.ts +6 -1
- package/backend/mcp/servers/helper.ts +13 -0
- package/backend/middleware/error-handler.ts +16 -2
- package/backend/preview/browser/browser-preview-service.ts +4 -4
- package/backend/preview/browser/browser-tab-manager.ts +23 -3
- package/backend/snapshot/gitignore.ts +4 -9
- package/backend/tunnel/cloudflared/binary.ts +2 -2
- package/backend/tunnel/global-tunnel-manager.ts +9 -3
- package/backend/utils/ws.ts +5 -1
- package/backend/ws/README.md +128 -3
- package/backend/ws/access.ts +56 -0
- package/backend/ws/auth/index.ts +14 -0
- package/backend/ws/auth/login.ts +3 -0
- package/backend/ws/auth/users.ts +61 -1
- package/backend/ws/chat/stream.ts +139 -22
- package/backend/ws/db-client/access.ts +19 -0
- package/backend/ws/db-client/connections.ts +31 -17
- package/backend/ws/db-client/io.ts +7 -9
- package/backend/ws/db-client/query.ts +86 -32
- package/backend/ws/db-client/schema.ts +9 -4
- package/backend/ws/db-client/structure.ts +22 -10
- package/backend/ws/engine/access.ts +39 -0
- package/backend/ws/engine/claude/accounts.ts +15 -10
- package/backend/ws/engine/codex/accounts.ts +516 -0
- package/backend/ws/engine/codex/index.ts +14 -0
- package/backend/ws/engine/codex/status.ts +87 -0
- package/backend/ws/engine/copilot/accounts.ts +122 -0
- package/backend/ws/engine/copilot/index.ts +13 -0
- package/backend/ws/engine/copilot/status.ts +54 -0
- package/backend/ws/engine/index.ts +7 -1
- package/backend/ws/engine/opencode/providers.ts +1 -1
- package/backend/ws/engine/qwen/accounts.ts +137 -0
- package/backend/ws/engine/qwen/index.ts +15 -0
- package/backend/ws/engine/qwen/presets.ts +40 -0
- package/backend/ws/engine/qwen/status.ts +54 -0
- package/backend/ws/files/path-access.ts +101 -0
- package/backend/ws/files/read.ts +26 -11
- package/backend/ws/files/search.ts +10 -6
- package/backend/ws/files/state.ts +3 -0
- package/backend/ws/files/watch.ts +7 -1
- package/backend/ws/files/write.ts +109 -15
- package/backend/ws/git/branch.ts +13 -19
- package/backend/ws/git/commit-message.ts +3 -4
- package/backend/ws/git/commit.ts +5 -7
- package/backend/ws/git/conflict.ts +7 -10
- package/backend/ws/git/diff.ts +7 -10
- package/backend/ws/git/log.ts +3 -4
- package/backend/ws/git/remote.ts +30 -43
- package/backend/ws/git/staging.ts +13 -19
- package/backend/ws/git/status.ts +7 -11
- package/backend/ws/messages/crud.ts +7 -13
- package/backend/ws/preview/access.ts +45 -0
- package/backend/ws/preview/browser/cleanup.ts +3 -11
- package/backend/ws/preview/browser/console.ts +7 -42
- package/backend/ws/preview/browser/interact.ts +2 -6
- package/backend/ws/preview/browser/native-ui.ts +5 -18
- package/backend/ws/preview/browser/stats.ts +2 -12
- package/backend/ws/preview/browser/tab-info.ts +4 -20
- package/backend/ws/preview/browser/tab.ts +4 -27
- package/backend/ws/preview/browser/webcodecs.ts +6 -47
- package/backend/ws/projects/crud.ts +15 -16
- package/backend/ws/projects/status.ts +5 -1
- package/backend/ws/sessions/crud.ts +31 -36
- package/backend/ws/settings/crud.ts +13 -13
- package/backend/ws/snapshot/restore.ts +5 -2
- package/backend/ws/snapshot/timeline.ts +3 -1
- package/backend/ws/system-tools/access.ts +28 -0
- package/backend/ws/system-tools/install.ts +13 -4
- package/backend/ws/system-tools/status.ts +5 -2
- package/backend/ws/terminal/access.ts +56 -0
- package/backend/ws/terminal/persistence.ts +13 -7
- package/backend/ws/terminal/session.ts +35 -14
- package/backend/ws/terminal/stream.ts +4 -1
- package/dist/assets/{index-DqAGCTFT.js → index-C1afGwHY.js} +2 -2
- package/dist/assets/main-CZb3Y_Z_.js +315 -0
- package/dist/assets/main-Cn8cX2DY.css +1 -0
- package/dist/index.html +2 -2
- package/package.json +5 -1
- package/shared/constants/engines.ts +45 -140
- package/shared/types/database/schema.ts +1 -0
- package/shared/types/unified/common.ts +1 -1
- package/shared/types/unified/engine.ts +29 -1
- package/shared/types/unified/index.ts +2 -0
- package/shared/types/unified/message.ts +1 -1
- package/shared/types/unified/tool.ts +17 -2
- package/shared/utils/ws-server.ts +14 -0
- package/dist/assets/main-DsED0rLr.js +0 -304
- package/dist/assets/main-eWY9R8g9.css +0 -1
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* GitHub Copilot Account Management Handlers
|
|
3
|
+
*
|
|
4
|
+
* Auth model: paste-token only. The user provides a GitHub Personal Access
|
|
5
|
+
* Token with Copilot access (no PTY / device-flow). Tokens are stored as the
|
|
6
|
+
* generic `credential` field on engine_accounts under provider slug 'github'.
|
|
7
|
+
*
|
|
8
|
+
* Switching/deleting active accounts disposes the project-scoped CopilotEngine
|
|
9
|
+
* instances so the next stream picks up the new credential.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { t } from 'elysia';
|
|
13
|
+
import { createRouter } from '$shared/utils/ws-server';
|
|
14
|
+
import { engineQueries } from '../../../database/queries';
|
|
15
|
+
import { disposeAllProjectEnginesByType } from '../../../engine';
|
|
16
|
+
|
|
17
|
+
async function disposeCopilotEngines(): Promise<void> {
|
|
18
|
+
try {
|
|
19
|
+
await disposeAllProjectEnginesByType('copilot');
|
|
20
|
+
} catch {
|
|
21
|
+
/* engine may not be initialised — ignore */
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export const copilotAccountsHandler = createRouter()
|
|
26
|
+
|
|
27
|
+
.http('engine:copilot-accounts-list', {
|
|
28
|
+
data: t.Object({}),
|
|
29
|
+
response: t.Object({
|
|
30
|
+
accounts: t.Array(t.Object({
|
|
31
|
+
id: t.Number(),
|
|
32
|
+
name: t.String(),
|
|
33
|
+
isActive: t.Boolean(),
|
|
34
|
+
createdAt: t.String()
|
|
35
|
+
}))
|
|
36
|
+
})
|
|
37
|
+
}, async () => {
|
|
38
|
+
const provider = engineQueries.getProviderBySlug('copilot', 'github');
|
|
39
|
+
if (!provider) return { accounts: [] };
|
|
40
|
+
const accounts = engineQueries.getAccountsByProvider(provider.id);
|
|
41
|
+
return {
|
|
42
|
+
accounts: accounts.map(a => ({
|
|
43
|
+
id: a.id,
|
|
44
|
+
name: a.name,
|
|
45
|
+
isActive: a.is_active === 1,
|
|
46
|
+
createdAt: a.created_at
|
|
47
|
+
}))
|
|
48
|
+
};
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
.http('engine:copilot-accounts-add', {
|
|
52
|
+
data: t.Object({
|
|
53
|
+
name: t.String({ minLength: 1 }),
|
|
54
|
+
token: t.String({ minLength: 1 })
|
|
55
|
+
}),
|
|
56
|
+
response: t.Object({
|
|
57
|
+
account: t.Object({
|
|
58
|
+
id: t.Number(),
|
|
59
|
+
name: t.String(),
|
|
60
|
+
isActive: t.Boolean(),
|
|
61
|
+
createdAt: t.String()
|
|
62
|
+
})
|
|
63
|
+
})
|
|
64
|
+
}, async ({ data }) => {
|
|
65
|
+
const provider = engineQueries.getProviderBySlug('copilot', 'github');
|
|
66
|
+
if (!provider) {
|
|
67
|
+
throw new Error('GitHub Copilot provider not found in database');
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const account = engineQueries.createAccount(provider.id, data.name.trim(), data.token.trim());
|
|
71
|
+
|
|
72
|
+
// If this is the first account it became active — drop any stale engine instances.
|
|
73
|
+
if (account.is_active === 1) {
|
|
74
|
+
await disposeCopilotEngines();
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return {
|
|
78
|
+
account: {
|
|
79
|
+
id: account.id,
|
|
80
|
+
name: account.name,
|
|
81
|
+
isActive: account.is_active === 1,
|
|
82
|
+
createdAt: account.created_at
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
.http('engine:copilot-accounts-switch', {
|
|
88
|
+
data: t.Object({ id: t.Number() }),
|
|
89
|
+
response: t.Object({ success: t.Boolean() })
|
|
90
|
+
}, async ({ data }) => {
|
|
91
|
+
engineQueries.switchAccount(data.id);
|
|
92
|
+
await disposeCopilotEngines();
|
|
93
|
+
return { success: true };
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
.http('engine:copilot-accounts-delete', {
|
|
97
|
+
data: t.Object({ id: t.Number() }),
|
|
98
|
+
response: t.Object({ success: t.Boolean() })
|
|
99
|
+
}, async ({ data }) => {
|
|
100
|
+
const active = engineQueries.getActiveAccountForEngine('copilot');
|
|
101
|
+
engineQueries.deleteAccount(data.id);
|
|
102
|
+
if (active?.id === data.id) await disposeCopilotEngines();
|
|
103
|
+
return { success: true };
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
.http('engine:copilot-accounts-rename', {
|
|
107
|
+
data: t.Object({ id: t.Number(), name: t.String({ minLength: 1 }) }),
|
|
108
|
+
response: t.Object({ success: t.Boolean() })
|
|
109
|
+
}, async ({ data }) => {
|
|
110
|
+
engineQueries.renameAccount(data.id, data.name.trim());
|
|
111
|
+
return { success: true };
|
|
112
|
+
})
|
|
113
|
+
|
|
114
|
+
// Restart all Copilot engine instances. Use after changing the active token
|
|
115
|
+
// so subsequent models:list / chat calls re-initialise with fresh credentials.
|
|
116
|
+
.http('engine:copilot-restart', {
|
|
117
|
+
data: t.Object({}),
|
|
118
|
+
response: t.Object({ success: t.Boolean() })
|
|
119
|
+
}, async () => {
|
|
120
|
+
await disposeCopilotEngines();
|
|
121
|
+
return { success: true };
|
|
122
|
+
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copilot Engine Router
|
|
3
|
+
*
|
|
4
|
+
* Combines status detection and account management handlers.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { createRouter } from '$shared/utils/ws-server';
|
|
8
|
+
import { copilotStatusHandler } from './status';
|
|
9
|
+
import { copilotAccountsHandler } from './accounts';
|
|
10
|
+
|
|
11
|
+
export const copilotEngineRouter = createRouter()
|
|
12
|
+
.merge(copilotStatusHandler)
|
|
13
|
+
.merge(copilotAccountsHandler);
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* GitHub Copilot Engine Status Handler
|
|
3
|
+
*
|
|
4
|
+
* Reports SDK availability and active account state. The SDK bundles its own
|
|
5
|
+
* CLI internally so there is nothing to install — `installed` is always true.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { t } from 'elysia';
|
|
9
|
+
import { createRouter } from '$shared/utils/ws-server';
|
|
10
|
+
import { engineQueries } from '../../../database/queries';
|
|
11
|
+
import { debug } from '$shared/utils/logger';
|
|
12
|
+
import { getBackendOS } from '../../../utils/os';
|
|
13
|
+
|
|
14
|
+
function readSdkVersion(): string | null {
|
|
15
|
+
try {
|
|
16
|
+
const path = require.resolve('@github/copilot-sdk/package.json');
|
|
17
|
+
const pkg = require(path) as { version?: string };
|
|
18
|
+
return pkg.version ?? null;
|
|
19
|
+
} catch {
|
|
20
|
+
return null;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export const copilotStatusHandler = createRouter()
|
|
25
|
+
.http('engine:copilot-status', {
|
|
26
|
+
data: t.Object({}),
|
|
27
|
+
response: t.Object({
|
|
28
|
+
installed: t.Boolean(),
|
|
29
|
+
version: t.Union([t.String(), t.Null()]),
|
|
30
|
+
activeAccount: t.Union([
|
|
31
|
+
t.Object({
|
|
32
|
+
id: t.Number(),
|
|
33
|
+
name: t.String()
|
|
34
|
+
}),
|
|
35
|
+
t.Null()
|
|
36
|
+
]),
|
|
37
|
+
accountsCount: t.Number(),
|
|
38
|
+
backendOS: t.Union([t.Literal('windows'), t.Literal('macos'), t.Literal('linux')])
|
|
39
|
+
})
|
|
40
|
+
}, async () => {
|
|
41
|
+
debug.log('engine', 'Checking Copilot status...');
|
|
42
|
+
|
|
43
|
+
const provider = engineQueries.getProviderBySlug('copilot', 'github');
|
|
44
|
+
const accounts = provider ? engineQueries.getAccountsByProvider(provider.id) : [];
|
|
45
|
+
const activeAccount = engineQueries.getActiveAccountForEngine('copilot');
|
|
46
|
+
|
|
47
|
+
return {
|
|
48
|
+
installed: true,
|
|
49
|
+
version: readSdkVersion(),
|
|
50
|
+
activeAccount: activeAccount ? { id: activeAccount.id, name: activeAccount.name } : null,
|
|
51
|
+
accountsCount: accounts.length,
|
|
52
|
+
backendOS: getBackendOS()
|
|
53
|
+
};
|
|
54
|
+
});
|
|
@@ -8,7 +8,13 @@
|
|
|
8
8
|
import { createRouter } from '$shared/utils/ws-server';
|
|
9
9
|
import { claudeCodeEngineRouter } from './claude';
|
|
10
10
|
import { openCodeEngineRouter } from './opencode';
|
|
11
|
+
import { copilotEngineRouter } from './copilot';
|
|
12
|
+
import { codexEngineRouter } from './codex';
|
|
13
|
+
import { qwenEngineRouter } from './qwen';
|
|
11
14
|
|
|
12
15
|
export const engineRouter = createRouter()
|
|
13
16
|
.merge(claudeCodeEngineRouter)
|
|
14
|
-
.merge(openCodeEngineRouter)
|
|
17
|
+
.merge(openCodeEngineRouter)
|
|
18
|
+
.merge(copilotEngineRouter)
|
|
19
|
+
.merge(codexEngineRouter)
|
|
20
|
+
.merge(qwenEngineRouter);
|
|
@@ -10,7 +10,7 @@ import { engineQueries } from '../../../database/queries';
|
|
|
10
10
|
import {
|
|
11
11
|
fetchAndCacheModelsDevCatalog,
|
|
12
12
|
getCachedModelsDevCatalog,
|
|
13
|
-
} from '../../../engine/adapters/opencode/
|
|
13
|
+
} from '../../../engine/adapters/opencode/presets';
|
|
14
14
|
import { disposeOpenCodeClient } from '../../../engine/adapters/opencode/server';
|
|
15
15
|
import { disposeEngine } from '../../../engine';
|
|
16
16
|
import { streamManager } from '../../../chat';
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Qwen Code Account Management Handlers
|
|
3
|
+
*
|
|
4
|
+
* Auth model: paste-token, per-account. Each `engine_accounts.credential`
|
|
5
|
+
* stores a JSON blob of `{ apiKey, preset }`. The chosen preset (DashScope
|
|
6
|
+
* CN/INTL, OpenRouter, Fireworks) decides the base URL — we no longer offer
|
|
7
|
+
* a custom-endpoint escape hatch since every account stands alone.
|
|
8
|
+
*
|
|
9
|
+
* Switching/deleting active accounts disposes the project-scoped Qwen engine
|
|
10
|
+
* instances so the next stream picks up the new credential.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { t } from 'elysia';
|
|
14
|
+
import { createRouter } from '$shared/utils/ws-server';
|
|
15
|
+
import { engineQueries } from '../../../database/queries';
|
|
16
|
+
import { disposeAllProjectEnginesByType } from '../../../engine';
|
|
17
|
+
import {
|
|
18
|
+
parseQwenCredential,
|
|
19
|
+
serializeQwenCredential,
|
|
20
|
+
} from '../../../engine/adapters/qwen/credential';
|
|
21
|
+
import type { QwenProviderPresetId } from '../../../engine/adapters/qwen/presets';
|
|
22
|
+
|
|
23
|
+
const PRESET_LITERALS = t.Union([
|
|
24
|
+
t.Literal('dashscope-cn'),
|
|
25
|
+
t.Literal('dashscope-intl'),
|
|
26
|
+
t.Literal('openrouter'),
|
|
27
|
+
t.Literal('fireworks'),
|
|
28
|
+
]);
|
|
29
|
+
|
|
30
|
+
async function disposeQwenEngines(): Promise<void> {
|
|
31
|
+
try {
|
|
32
|
+
await disposeAllProjectEnginesByType('qwen');
|
|
33
|
+
} catch {
|
|
34
|
+
/* engine may not be initialised — ignore */
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export const qwenAccountsHandler = createRouter()
|
|
39
|
+
|
|
40
|
+
.http('engine:qwen-accounts-list', {
|
|
41
|
+
data: t.Object({}),
|
|
42
|
+
response: t.Object({
|
|
43
|
+
accounts: t.Array(t.Object({
|
|
44
|
+
id: t.Number(),
|
|
45
|
+
name: t.String(),
|
|
46
|
+
isActive: t.Boolean(),
|
|
47
|
+
createdAt: t.String(),
|
|
48
|
+
preset: PRESET_LITERALS,
|
|
49
|
+
})),
|
|
50
|
+
})
|
|
51
|
+
}, async () => {
|
|
52
|
+
const provider = engineQueries.getProviderBySlug('qwen', 'qwen');
|
|
53
|
+
if (!provider) return { accounts: [] };
|
|
54
|
+
const accounts = engineQueries.getAccountsByProvider(provider.id);
|
|
55
|
+
return {
|
|
56
|
+
accounts: accounts.map(a => {
|
|
57
|
+
const credential = parseQwenCredential(a.credential);
|
|
58
|
+
return {
|
|
59
|
+
id: a.id,
|
|
60
|
+
name: a.name,
|
|
61
|
+
isActive: a.is_active === 1,
|
|
62
|
+
createdAt: a.created_at,
|
|
63
|
+
preset: credential.preset as QwenProviderPresetId,
|
|
64
|
+
};
|
|
65
|
+
}),
|
|
66
|
+
};
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
.http('engine:qwen-accounts-add', {
|
|
70
|
+
data: t.Object({
|
|
71
|
+
name: t.String({ minLength: 1 }),
|
|
72
|
+
apiKey: t.String({ minLength: 1 }),
|
|
73
|
+
preset: PRESET_LITERALS,
|
|
74
|
+
}),
|
|
75
|
+
response: t.Object({
|
|
76
|
+
account: t.Object({
|
|
77
|
+
id: t.Number(),
|
|
78
|
+
name: t.String(),
|
|
79
|
+
isActive: t.Boolean(),
|
|
80
|
+
createdAt: t.String(),
|
|
81
|
+
preset: PRESET_LITERALS,
|
|
82
|
+
})
|
|
83
|
+
})
|
|
84
|
+
}, async ({ data }) => {
|
|
85
|
+
const provider = engineQueries.getProviderBySlug('qwen', 'qwen');
|
|
86
|
+
if (!provider) {
|
|
87
|
+
throw new Error('Qwen Code provider not found in database');
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const credential = serializeQwenCredential({
|
|
91
|
+
apiKey: data.apiKey.trim(),
|
|
92
|
+
preset: data.preset,
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
const account = engineQueries.createAccount(provider.id, data.name.trim(), credential);
|
|
96
|
+
|
|
97
|
+
if (account.is_active === 1) {
|
|
98
|
+
await disposeQwenEngines();
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return {
|
|
102
|
+
account: {
|
|
103
|
+
id: account.id,
|
|
104
|
+
name: account.name,
|
|
105
|
+
isActive: account.is_active === 1,
|
|
106
|
+
createdAt: account.created_at,
|
|
107
|
+
preset: data.preset,
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
})
|
|
111
|
+
|
|
112
|
+
.http('engine:qwen-accounts-switch', {
|
|
113
|
+
data: t.Object({ id: t.Number() }),
|
|
114
|
+
response: t.Object({ success: t.Boolean() })
|
|
115
|
+
}, async ({ data }) => {
|
|
116
|
+
engineQueries.switchAccount(data.id);
|
|
117
|
+
await disposeQwenEngines();
|
|
118
|
+
return { success: true };
|
|
119
|
+
})
|
|
120
|
+
|
|
121
|
+
.http('engine:qwen-accounts-delete', {
|
|
122
|
+
data: t.Object({ id: t.Number() }),
|
|
123
|
+
response: t.Object({ success: t.Boolean() })
|
|
124
|
+
}, async ({ data }) => {
|
|
125
|
+
const active = engineQueries.getActiveAccountForEngine('qwen');
|
|
126
|
+
engineQueries.deleteAccount(data.id);
|
|
127
|
+
if (active?.id === data.id) await disposeQwenEngines();
|
|
128
|
+
return { success: true };
|
|
129
|
+
})
|
|
130
|
+
|
|
131
|
+
.http('engine:qwen-accounts-rename', {
|
|
132
|
+
data: t.Object({ id: t.Number(), name: t.String({ minLength: 1 }) }),
|
|
133
|
+
response: t.Object({ success: t.Boolean() })
|
|
134
|
+
}, async ({ data }) => {
|
|
135
|
+
engineQueries.renameAccount(data.id, data.name.trim());
|
|
136
|
+
return { success: true };
|
|
137
|
+
});
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Qwen Code Engine Router
|
|
3
|
+
*
|
|
4
|
+
* Combines status detection, account management and provider preset handlers.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { createRouter } from '$shared/utils/ws-server';
|
|
8
|
+
import { qwenStatusHandler } from './status';
|
|
9
|
+
import { qwenAccountsHandler } from './accounts';
|
|
10
|
+
import { qwenPresetsHandler } from './presets';
|
|
11
|
+
|
|
12
|
+
export const qwenEngineRouter = createRouter()
|
|
13
|
+
.merge(qwenStatusHandler)
|
|
14
|
+
.merge(qwenAccountsHandler)
|
|
15
|
+
.merge(qwenPresetsHandler);
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Qwen Code Provider Presets Handler
|
|
3
|
+
*
|
|
4
|
+
* Exposes the static preset catalog (DashScope CN/INTL, OpenRouter, Fireworks)
|
|
5
|
+
* to the frontend so it can render the picker without importing backend
|
|
6
|
+
* modules directly.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { t } from 'elysia';
|
|
10
|
+
import { createRouter } from '$shared/utils/ws-server';
|
|
11
|
+
import {
|
|
12
|
+
QWEN_PROVIDER_PRESETS,
|
|
13
|
+
DEFAULT_QWEN_PRESET,
|
|
14
|
+
} from '../../../engine/adapters/qwen/presets';
|
|
15
|
+
|
|
16
|
+
const PRESET_LITERALS = t.Union([
|
|
17
|
+
t.Literal('dashscope-cn'),
|
|
18
|
+
t.Literal('dashscope-intl'),
|
|
19
|
+
t.Literal('openrouter'),
|
|
20
|
+
t.Literal('fireworks'),
|
|
21
|
+
]);
|
|
22
|
+
|
|
23
|
+
export const qwenPresetsHandler = createRouter()
|
|
24
|
+
.http('engine:qwen-presets-list', {
|
|
25
|
+
data: t.Object({}),
|
|
26
|
+
response: t.Object({
|
|
27
|
+
presets: t.Array(t.Object({
|
|
28
|
+
id: PRESET_LITERALS,
|
|
29
|
+
name: t.String(),
|
|
30
|
+
defaultBaseUrl: t.String(),
|
|
31
|
+
docsUrl: t.Optional(t.String()),
|
|
32
|
+
})),
|
|
33
|
+
defaultPreset: PRESET_LITERALS,
|
|
34
|
+
})
|
|
35
|
+
}, async () => {
|
|
36
|
+
return {
|
|
37
|
+
presets: QWEN_PROVIDER_PRESETS,
|
|
38
|
+
defaultPreset: DEFAULT_QWEN_PRESET,
|
|
39
|
+
};
|
|
40
|
+
});
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Qwen Code Engine Status Handler
|
|
3
|
+
*
|
|
4
|
+
* Reports SDK availability and active account state. The SDK bundles its own
|
|
5
|
+
* CLI internally (`@qwen-code/sdk` v0.1.1+) so `installed` is always true.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { t } from 'elysia';
|
|
9
|
+
import { createRouter } from '$shared/utils/ws-server';
|
|
10
|
+
import { engineQueries } from '../../../database/queries';
|
|
11
|
+
import { debug } from '$shared/utils/logger';
|
|
12
|
+
import { getBackendOS } from '../../../utils/os';
|
|
13
|
+
|
|
14
|
+
function readSdkVersion(): string | null {
|
|
15
|
+
try {
|
|
16
|
+
const path = require.resolve('@qwen-code/sdk/package.json');
|
|
17
|
+
const pkg = require(path) as { version?: string };
|
|
18
|
+
return pkg.version ?? null;
|
|
19
|
+
} catch {
|
|
20
|
+
return null;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export const qwenStatusHandler = createRouter()
|
|
25
|
+
.http('engine:qwen-status', {
|
|
26
|
+
data: t.Object({}),
|
|
27
|
+
response: t.Object({
|
|
28
|
+
installed: t.Boolean(),
|
|
29
|
+
version: t.Union([t.String(), t.Null()]),
|
|
30
|
+
activeAccount: t.Union([
|
|
31
|
+
t.Object({
|
|
32
|
+
id: t.Number(),
|
|
33
|
+
name: t.String()
|
|
34
|
+
}),
|
|
35
|
+
t.Null()
|
|
36
|
+
]),
|
|
37
|
+
accountsCount: t.Number(),
|
|
38
|
+
backendOS: t.Union([t.Literal('windows'), t.Literal('macos'), t.Literal('linux')])
|
|
39
|
+
})
|
|
40
|
+
}, async () => {
|
|
41
|
+
debug.log('engine', 'Checking Qwen Code status...');
|
|
42
|
+
|
|
43
|
+
const provider = engineQueries.getProviderBySlug('qwen', 'qwen');
|
|
44
|
+
const accounts = provider ? engineQueries.getAccountsByProvider(provider.id) : [];
|
|
45
|
+
const activeAccount = engineQueries.getActiveAccountForEngine('qwen');
|
|
46
|
+
|
|
47
|
+
return {
|
|
48
|
+
installed: true,
|
|
49
|
+
version: readSdkVersion(),
|
|
50
|
+
activeAccount: activeAccount ? { id: activeAccount.id, name: activeAccount.name } : null,
|
|
51
|
+
accountsCount: accounts.length,
|
|
52
|
+
backendOS: getBackendOS()
|
|
53
|
+
};
|
|
54
|
+
});
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { isAbsolute, relative, resolve } from 'node:path';
|
|
2
|
+
|
|
3
|
+
import { projectQueries } from '../../database/queries/project-queries';
|
|
4
|
+
import { ws } from '$backend/utils/ws';
|
|
5
|
+
import type { WSConnection } from '$shared/utils/ws-server';
|
|
6
|
+
import type { Project } from '$shared/types/database/schema';
|
|
7
|
+
import { requireProjectAccess } from '../access';
|
|
8
|
+
|
|
9
|
+
function isPathInside(rootPath: string, candidatePath: string): boolean {
|
|
10
|
+
const root = resolve(rootPath);
|
|
11
|
+
const candidate = resolve(candidatePath);
|
|
12
|
+
const rel = relative(root, candidate);
|
|
13
|
+
return rel === '' || (!!rel && !rel.startsWith('..') && !isAbsolute(rel));
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function requireProjectPathAccess(conn: WSConnection, projectPath: string): Project {
|
|
17
|
+
const project = projectQueries.getByPath(projectPath);
|
|
18
|
+
if (!project) {
|
|
19
|
+
throw new Error('Access denied');
|
|
20
|
+
}
|
|
21
|
+
if (ws.getRole(conn) === 'admin') {
|
|
22
|
+
return project;
|
|
23
|
+
}
|
|
24
|
+
return requireProjectAccess(conn, project.id);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function requireFilePathAccess(conn: WSConnection, filePath: string): string {
|
|
28
|
+
const normalizedPath = resolve(filePath);
|
|
29
|
+
if (ws.getRole(conn) === 'admin') {
|
|
30
|
+
return normalizedPath;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const userId = ws.getUserId(conn);
|
|
34
|
+
const projects = projectQueries.getAllForUser(userId);
|
|
35
|
+
|
|
36
|
+
const hasAccess = projects.some((project) => isPathInside(project.path, normalizedPath));
|
|
37
|
+
if (!hasAccess) {
|
|
38
|
+
throw new Error('Access denied');
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return normalizedPath;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Picker-style guard: allow paths inside the user's accessible projects OR
|
|
45
|
+
// outside every registered project. Rejects only when the path lives inside
|
|
46
|
+
// another project the user cannot access. Used by FolderBrowser flows that
|
|
47
|
+
// operate around / before a project exists.
|
|
48
|
+
export function requireSharedFilePathAccess(conn: WSConnection, filePath: string): string {
|
|
49
|
+
const normalizedPath = resolve(filePath);
|
|
50
|
+
if (ws.getRole(conn) === 'admin') {
|
|
51
|
+
return normalizedPath;
|
|
52
|
+
}
|
|
53
|
+
const userId = ws.getUserId(conn);
|
|
54
|
+
const allProjects = projectQueries.getAll();
|
|
55
|
+
|
|
56
|
+
// Ancestor guard: reject if the path is a parent of another user's project.
|
|
57
|
+
// Without this, renaming/deleting `/foo` would break a project rooted at
|
|
58
|
+
// `/foo/bar` even though `/foo` itself is not "inside" any project.
|
|
59
|
+
for (const project of allProjects) {
|
|
60
|
+
const projectRoot = resolve(project.path);
|
|
61
|
+
if (projectRoot === normalizedPath) continue; // equality is handled below
|
|
62
|
+
if (!isPathInside(normalizedPath, project.path)) continue;
|
|
63
|
+
if (!projectQueries.userHasProject(userId, project.id)) {
|
|
64
|
+
throw new Error('Access denied');
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// Pick the most specific (longest matching root) project so nested project
|
|
69
|
+
// roots resolve to the inner one, not whichever the DB returns first.
|
|
70
|
+
let containing: { id: string; path: string } | null = null;
|
|
71
|
+
for (const project of allProjects) {
|
|
72
|
+
if (!isPathInside(project.path, normalizedPath)) continue;
|
|
73
|
+
const projectRoot = resolve(project.path);
|
|
74
|
+
if (!containing || projectRoot.length > resolve(containing.path).length) {
|
|
75
|
+
containing = project;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (!containing) {
|
|
80
|
+
return normalizedPath;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const hasAccess = projectQueries.userHasProject(userId, containing.id);
|
|
84
|
+
if (!hasAccess) {
|
|
85
|
+
throw new Error('Access denied');
|
|
86
|
+
}
|
|
87
|
+
return normalizedPath;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export function filterAccessibleExpandedPaths(rootPath: string, expandedPaths?: Set<string>): Set<string> | undefined {
|
|
91
|
+
if (!expandedPaths) return undefined;
|
|
92
|
+
const filtered = new Set<string>();
|
|
93
|
+
|
|
94
|
+
for (const expandedPath of expandedPaths) {
|
|
95
|
+
if (isPathInside(rootPath, expandedPath)) {
|
|
96
|
+
filtered.add(resolve(expandedPath));
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return filtered;
|
|
101
|
+
}
|
package/backend/ws/files/read.ts
CHANGED
|
@@ -21,6 +21,12 @@ import { handlePathBrowsing } from '../../files/path-browsing';
|
|
|
21
21
|
import { gitService } from '../../git/git-service';
|
|
22
22
|
import { projectQueries } from '../../database/queries/project-queries';
|
|
23
23
|
import { isAbsolute, relative } from 'node:path';
|
|
24
|
+
import { requireProjectAccess } from '../access';
|
|
25
|
+
import {
|
|
26
|
+
filterAccessibleExpandedPaths,
|
|
27
|
+
requireFilePathAccess,
|
|
28
|
+
requireProjectPathAccess
|
|
29
|
+
} from './path-access';
|
|
24
30
|
|
|
25
31
|
// Bun-compatible existsSync implementation
|
|
26
32
|
async function existsSync(path: string): Promise<boolean> {
|
|
@@ -63,8 +69,11 @@ export const readHandler = createRouter()
|
|
|
63
69
|
})
|
|
64
70
|
])
|
|
65
71
|
)
|
|
66
|
-
}, async ({ data }) => {
|
|
67
|
-
|
|
72
|
+
}, async ({ data, conn }) => {
|
|
73
|
+
const project = requireProjectPathAccess(conn, data.project_path);
|
|
74
|
+
const projectPath = project.path;
|
|
75
|
+
|
|
76
|
+
if (!(await existsSync(projectPath))) {
|
|
68
77
|
throw new Error('Project path does not exist');
|
|
69
78
|
}
|
|
70
79
|
|
|
@@ -79,8 +88,9 @@ export const readHandler = createRouter()
|
|
|
79
88
|
.filter(Boolean);
|
|
80
89
|
expandedPaths = new Set(paths);
|
|
81
90
|
}
|
|
91
|
+
expandedPaths = filterAccessibleExpandedPaths(projectPath, expandedPaths);
|
|
82
92
|
|
|
83
|
-
const fileTree = await buildFileTree(
|
|
93
|
+
const fileTree = await buildFileTree(projectPath, 3, 0, expandedPaths);
|
|
84
94
|
return fileTree as any;
|
|
85
95
|
})
|
|
86
96
|
|
|
@@ -111,6 +121,8 @@ export const readHandler = createRouter()
|
|
|
111
121
|
error: t.Optional(t.String())
|
|
112
122
|
})
|
|
113
123
|
}, async ({ data }) => {
|
|
124
|
+
// FolderBrowser uses this before a project exists, so it must be able to
|
|
125
|
+
// browse roots like "home", "drives", ".", and arbitrary candidate paths.
|
|
114
126
|
const result = await handlePathBrowsing(data.path);
|
|
115
127
|
return result;
|
|
116
128
|
})
|
|
@@ -133,8 +145,9 @@ export const readHandler = createRouter()
|
|
|
133
145
|
})
|
|
134
146
|
)
|
|
135
147
|
)
|
|
136
|
-
}, async ({ data }) => {
|
|
137
|
-
const
|
|
148
|
+
}, async ({ data, conn }) => {
|
|
149
|
+
const dirPath = requireFilePathAccess(conn, data.dir_path);
|
|
150
|
+
const result = await listDirectoryContents(dirPath);
|
|
138
151
|
return result;
|
|
139
152
|
})
|
|
140
153
|
|
|
@@ -152,8 +165,9 @@ export const readHandler = createRouter()
|
|
|
152
165
|
isBinary: t.Optional(t.Boolean()),
|
|
153
166
|
error: t.Optional(t.String())
|
|
154
167
|
})
|
|
155
|
-
}, async ({ data }) => {
|
|
156
|
-
const
|
|
168
|
+
}, async ({ data, conn }) => {
|
|
169
|
+
const filePath = requireFilePathAccess(conn, data.file_path);
|
|
170
|
+
const result = await readFileContents(filePath);
|
|
157
171
|
return result;
|
|
158
172
|
})
|
|
159
173
|
|
|
@@ -168,9 +182,10 @@ export const readHandler = createRouter()
|
|
|
168
182
|
content: t.Union([t.String(), t.Null()]),
|
|
169
183
|
ref: t.String()
|
|
170
184
|
})
|
|
171
|
-
}, async ({ data }) => {
|
|
185
|
+
}, async ({ data, conn }) => {
|
|
186
|
+
requireProjectAccess(conn, data.projectId);
|
|
172
187
|
const project = projectQueries.getById(data.projectId);
|
|
173
|
-
if (!project) throw new Error('
|
|
188
|
+
if (!project) throw new Error('Access denied');
|
|
174
189
|
|
|
175
190
|
const ref = data.ref || 'HEAD';
|
|
176
191
|
|
|
@@ -195,8 +210,8 @@ export const readHandler = createRouter()
|
|
|
195
210
|
content: t.String(), // Base64 encoded binary content
|
|
196
211
|
contentType: t.String()
|
|
197
212
|
})
|
|
198
|
-
}, async ({ data }) => {
|
|
199
|
-
const
|
|
213
|
+
}, async ({ data, conn }) => {
|
|
214
|
+
const path = requireFilePathAccess(conn, data.path);
|
|
200
215
|
|
|
201
216
|
// Read file as binary
|
|
202
217
|
const file = Bun.file(path);
|