@myrialabs/clopen 0.3.17 → 0.3.18
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/database/migrations/038_add_workspace_state_to_user_projects.ts +23 -0
- package/backend/database/migrations/index.ts +7 -0
- package/backend/database/queries/project-queries.ts +18 -0
- package/backend/git/git-service.ts +135 -2
- package/backend/preview/browser/browser-preview-service.ts +9 -6
- package/backend/utils/ws.ts +3 -1
- package/backend/ws/git/commit.ts +63 -0
- package/backend/ws/git/remote.ts +36 -1
- package/backend/ws/index.ts +2 -0
- package/backend/ws/preview/access.ts +20 -1
- package/backend/ws/preview/browser/tab-info.ts +15 -6
- package/backend/ws/preview/index.ts +7 -0
- package/backend/ws/workspace/index.ts +14 -0
- package/backend/ws/workspace/state.ts +44 -0
- package/dist/assets/main-6cCq8H0w.css +1 -0
- package/dist/assets/main-CaqEjA6B.js +361 -0
- package/dist/index.html +2 -2
- package/package.json +1 -1
- package/shared/constants/notification-sounds.ts +1 -1
- package/shared/utils/logger.ts +4 -6
- package/dist/assets/main-CHTThSzD.js +0 -358
- package/dist/assets/main-Dzb_5CtR.css +0 -1
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { DatabaseConnection } from '$shared/types/database/connection';
|
|
2
|
+
import { debug } from '$shared/utils/logger';
|
|
3
|
+
|
|
4
|
+
export const description = 'Add workspace_state to user_projects for per-project dock/layout restore';
|
|
5
|
+
|
|
6
|
+
export const up = (db: DatabaseConnection): void => {
|
|
7
|
+
debug.log('migration', 'Adding workspace_state to user_projects...');
|
|
8
|
+
db.exec(`ALTER TABLE user_projects ADD COLUMN workspace_state TEXT`);
|
|
9
|
+
debug.log('migration', 'workspace_state column added');
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export const down = (db: DatabaseConnection): void => {
|
|
13
|
+
debug.log('migration', 'Removing workspace_state from user_projects...');
|
|
14
|
+
db.exec(`
|
|
15
|
+
CREATE TABLE user_projects_backup AS
|
|
16
|
+
SELECT user_id, project_id, joined_at, current_session_id, files_panel_state FROM user_projects
|
|
17
|
+
`);
|
|
18
|
+
db.exec(`DROP TABLE user_projects`);
|
|
19
|
+
db.exec(`ALTER TABLE user_projects_backup RENAME TO user_projects`);
|
|
20
|
+
db.exec(`CREATE INDEX IF NOT EXISTS idx_user_projects_user_id ON user_projects(user_id)`);
|
|
21
|
+
db.exec(`CREATE INDEX IF NOT EXISTS idx_user_projects_project_id ON user_projects(project_id)`);
|
|
22
|
+
debug.log('migration', 'workspace_state column removed');
|
|
23
|
+
};
|
|
@@ -36,6 +36,7 @@ import * as migration034 from './034_seed_qwen_provider';
|
|
|
36
36
|
import * as migration035 from './035_add_owner_to_db_client_connections';
|
|
37
37
|
import * as migration036 from './036_create_auth_audit_log';
|
|
38
38
|
import * as migration037 from './037_repair_auth_audit_log_schema';
|
|
39
|
+
import * as migration038 from './038_add_workspace_state_to_user_projects';
|
|
39
40
|
|
|
40
41
|
// Export all migrations in order
|
|
41
42
|
export const migrations = [
|
|
@@ -260,6 +261,12 @@ export const migrations = [
|
|
|
260
261
|
description: migration037.description,
|
|
261
262
|
up: migration037.up,
|
|
262
263
|
down: migration037.down
|
|
264
|
+
},
|
|
265
|
+
{
|
|
266
|
+
id: '038',
|
|
267
|
+
description: migration038.description,
|
|
268
|
+
up: migration038.up,
|
|
269
|
+
down: migration038.down
|
|
263
270
|
}
|
|
264
271
|
];
|
|
265
272
|
|
|
@@ -138,5 +138,23 @@ export const projectQueries = {
|
|
|
138
138
|
SET files_panel_state = ?
|
|
139
139
|
WHERE user_id = ? AND project_id = ?
|
|
140
140
|
`).run(state, userId, projectId);
|
|
141
|
+
},
|
|
142
|
+
|
|
143
|
+
getWorkspaceState(userId: string, projectId: string): string | null {
|
|
144
|
+
const db = getDatabase();
|
|
145
|
+
const result = db.prepare(`
|
|
146
|
+
SELECT workspace_state FROM user_projects
|
|
147
|
+
WHERE user_id = ? AND project_id = ?
|
|
148
|
+
`).get(userId, projectId) as { workspace_state: string | null } | null;
|
|
149
|
+
return result?.workspace_state || null;
|
|
150
|
+
},
|
|
151
|
+
|
|
152
|
+
setWorkspaceState(userId: string, projectId: string, state: string | null): void {
|
|
153
|
+
const db = getDatabase();
|
|
154
|
+
db.prepare(`
|
|
155
|
+
UPDATE user_projects
|
|
156
|
+
SET workspace_state = ?
|
|
157
|
+
WHERE user_id = ? AND project_id = ?
|
|
158
|
+
`).run(state, userId, projectId);
|
|
141
159
|
}
|
|
142
160
|
};
|
|
@@ -4,6 +4,8 @@
|
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
6
|
import { execGit, isGitRepo, getGitRoot } from './git-executor';
|
|
7
|
+
import { resolveBinary } from '../utils/cli';
|
|
8
|
+
import { getCleanSpawnEnv } from '../utils/env';
|
|
7
9
|
import {
|
|
8
10
|
parseStatus,
|
|
9
11
|
parseBranches,
|
|
@@ -378,9 +380,11 @@ export class GitService {
|
|
|
378
380
|
return result.stderr || result.stdout; // git fetch outputs to stderr
|
|
379
381
|
}
|
|
380
382
|
|
|
381
|
-
async pull(cwd: string, remote = 'origin', branch?: string): Promise<{ success: boolean; message: string }> {
|
|
383
|
+
async pull(cwd: string, remote = 'origin', branch?: string, rebase = false): Promise<{ success: boolean; message: string }> {
|
|
382
384
|
assertSafeGitRemoteName(remote);
|
|
383
|
-
const args = ['pull'
|
|
385
|
+
const args = ['pull'];
|
|
386
|
+
if (rebase) args.push('--rebase');
|
|
387
|
+
args.push(remote);
|
|
384
388
|
if (branch) {
|
|
385
389
|
assertSafeGitRevish(branch, 'pull branch');
|
|
386
390
|
args.push(branch);
|
|
@@ -409,6 +413,49 @@ export class GitService {
|
|
|
409
413
|
};
|
|
410
414
|
}
|
|
411
415
|
|
|
416
|
+
/**
|
|
417
|
+
* Advanced push variants surfaced in the "More" menu:
|
|
418
|
+
* - `with-tags` → push branch + reachable annotated tags (--follow-tags)
|
|
419
|
+
* - `all-tags` → push all local tags (--tags), no branch
|
|
420
|
+
* - `force-lease` → safe force push (--force-with-lease)
|
|
421
|
+
* - `force` → unconditional force push (--force)
|
|
422
|
+
*/
|
|
423
|
+
async pushAdvanced(
|
|
424
|
+
cwd: string,
|
|
425
|
+
mode: 'with-tags' | 'all-tags' | 'force-lease' | 'force',
|
|
426
|
+
remote = 'origin',
|
|
427
|
+
branch?: string
|
|
428
|
+
): Promise<{ success: boolean; message: string }> {
|
|
429
|
+
assertSafeGitRemoteName(remote);
|
|
430
|
+
const args = ['push', remote];
|
|
431
|
+
if (mode === 'all-tags') {
|
|
432
|
+
args.push('--tags');
|
|
433
|
+
} else {
|
|
434
|
+
if (branch) {
|
|
435
|
+
assertSafeGitRevish(branch, 'push branch');
|
|
436
|
+
args.push(branch);
|
|
437
|
+
}
|
|
438
|
+
if (mode === 'with-tags') args.push('--follow-tags');
|
|
439
|
+
else if (mode === 'force-lease') args.push('--force-with-lease');
|
|
440
|
+
else if (mode === 'force') args.push('--force');
|
|
441
|
+
args.push('-u');
|
|
442
|
+
}
|
|
443
|
+
const result = await execGit(args, cwd, 60000);
|
|
444
|
+
return {
|
|
445
|
+
success: result.exitCode === 0,
|
|
446
|
+
message: result.exitCode === 0 ? (result.stderr || result.stdout) : result.stderr
|
|
447
|
+
};
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
/** Fetch every configured remote and prune deleted remote-tracking refs. */
|
|
451
|
+
async fetchAll(cwd: string): Promise<string> {
|
|
452
|
+
const result = await execGit(['fetch', '--all', '--prune', '--tags'], cwd, 60000);
|
|
453
|
+
if (result.exitCode !== 0) {
|
|
454
|
+
throw new Error(`git fetch --all failed: ${result.stderr}`);
|
|
455
|
+
}
|
|
456
|
+
return result.stderr || result.stdout;
|
|
457
|
+
}
|
|
458
|
+
|
|
412
459
|
async addRemote(cwd: string, name: string, url: string): Promise<void> {
|
|
413
460
|
assertSafeGitRemoteName(name);
|
|
414
461
|
assertSafeGitRemoteUrl(url);
|
|
@@ -627,6 +674,92 @@ export class GitService {
|
|
|
627
674
|
throw new Error(`git reset --merge failed: ${reset.stderr}`);
|
|
628
675
|
}
|
|
629
676
|
}
|
|
677
|
+
|
|
678
|
+
// ============================================
|
|
679
|
+
// History rewrite / undo
|
|
680
|
+
// ============================================
|
|
681
|
+
|
|
682
|
+
/**
|
|
683
|
+
* Undo the last commit by moving HEAD back one commit.
|
|
684
|
+
* - `soft` keeps the changes staged
|
|
685
|
+
* - `mixed` keeps the changes in the working tree (unstaged)
|
|
686
|
+
* - `hard` discards the changes entirely (destructive)
|
|
687
|
+
*/
|
|
688
|
+
async undoLastCommit(cwd: string, mode: 'soft' | 'mixed' | 'hard'): Promise<void> {
|
|
689
|
+
const flag = mode === 'soft' ? '--soft' : mode === 'hard' ? '--hard' : '--mixed';
|
|
690
|
+
const result = await execGit(['reset', flag, 'HEAD~1'], cwd);
|
|
691
|
+
if (result.exitCode !== 0) {
|
|
692
|
+
throw new Error(`git reset ${flag} failed: ${result.stderr}`);
|
|
693
|
+
}
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
/** Create a new commit that reverses a previous one (default: HEAD). */
|
|
697
|
+
async revertCommit(cwd: string, ref = 'HEAD'): Promise<{ success: boolean; message: string }> {
|
|
698
|
+
assertSafeGitRevish(ref, 'revert ref');
|
|
699
|
+
const result = await execGit(['revert', '--no-edit', ref], cwd);
|
|
700
|
+
return {
|
|
701
|
+
success: result.exitCode === 0,
|
|
702
|
+
message: result.exitCode === 0 ? (result.stdout || result.stderr) : result.stderr
|
|
703
|
+
};
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
// ============================================
|
|
707
|
+
// Maintenance
|
|
708
|
+
// ============================================
|
|
709
|
+
|
|
710
|
+
/** Remove untracked files and directories (`git clean -fd`). */
|
|
711
|
+
async cleanUntracked(cwd: string): Promise<string> {
|
|
712
|
+
const result = await execGit(['clean', '-fd'], cwd);
|
|
713
|
+
if (result.exitCode !== 0) {
|
|
714
|
+
throw new Error(`git clean failed: ${result.stderr}`);
|
|
715
|
+
}
|
|
716
|
+
return result.stdout.trim();
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
/** Run garbage collection to optimize the repository. */
|
|
720
|
+
async optimize(cwd: string): Promise<string> {
|
|
721
|
+
const result = await execGit(['gc'], cwd, 180000);
|
|
722
|
+
if (result.exitCode !== 0) {
|
|
723
|
+
throw new Error(`git gc failed: ${result.stderr}`);
|
|
724
|
+
}
|
|
725
|
+
return (result.stderr || result.stdout).trim() || 'Repository optimized';
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
// ============================================
|
|
729
|
+
// npm version
|
|
730
|
+
// ============================================
|
|
731
|
+
|
|
732
|
+
/**
|
|
733
|
+
* Bump the package version with `npm version <patch|minor|major>`. In a git
|
|
734
|
+
* repo npm creates the version-bump commit and tag automatically. Requires a
|
|
735
|
+
* clean working tree (npm refuses otherwise) — that error is surfaced verbatim.
|
|
736
|
+
*/
|
|
737
|
+
async npmVersion(
|
|
738
|
+
cwd: string,
|
|
739
|
+
bump: 'patch' | 'minor' | 'major'
|
|
740
|
+
): Promise<{ success: boolean; version: string; message: string }> {
|
|
741
|
+
const npmPath = resolveBinary('npm');
|
|
742
|
+
if (!npmPath) {
|
|
743
|
+
throw new Error('npm binary not found on PATH');
|
|
744
|
+
}
|
|
745
|
+
const proc = Bun.spawn([npmPath, 'version', bump, '-m', 'chore(release): v%s'], {
|
|
746
|
+
cwd,
|
|
747
|
+
stdout: 'pipe',
|
|
748
|
+
stderr: 'pipe',
|
|
749
|
+
env: { ...getCleanSpawnEnv() }
|
|
750
|
+
});
|
|
751
|
+
const [stdout, stderr] = await Promise.all([
|
|
752
|
+
new Response(proc.stdout).text(),
|
|
753
|
+
new Response(proc.stderr).text()
|
|
754
|
+
]);
|
|
755
|
+
const exitCode = await proc.exited;
|
|
756
|
+
if (exitCode !== 0) {
|
|
757
|
+
return { success: false, version: '', message: (stderr || stdout).trim() };
|
|
758
|
+
}
|
|
759
|
+
// npm prints the new version (e.g. "v1.2.3") on stdout.
|
|
760
|
+
const version = stdout.trim().split('\n').pop()?.trim() || '';
|
|
761
|
+
return { success: true, version, message: version };
|
|
762
|
+
}
|
|
630
763
|
}
|
|
631
764
|
|
|
632
765
|
// Singleton
|
|
@@ -669,26 +669,29 @@ class BrowserPreviewServiceManager {
|
|
|
669
669
|
ws.emit.project(projectId, 'preview:browser-navigation-spa', data);
|
|
670
670
|
});
|
|
671
671
|
|
|
672
|
-
// Forward tab events
|
|
672
|
+
// Forward tab events. Each carries projectId so the frontend can drop
|
|
673
|
+
// events for a project it has since switched away from — without it, a
|
|
674
|
+
// background stream (or a late event) could create/mutate tabs in the
|
|
675
|
+
// project the user is now viewing.
|
|
673
676
|
service.on('preview:browser-tab-opened', (data) => {
|
|
674
677
|
debug.log('preview', `🚀 Forwarding preview:browser-tab-opened to project ${projectId}:`, data);
|
|
675
|
-
ws.emit.project(projectId, 'preview:browser-tab-opened', data);
|
|
678
|
+
ws.emit.project(projectId, 'preview:browser-tab-opened', { ...data, projectId });
|
|
676
679
|
});
|
|
677
680
|
|
|
678
681
|
service.on('preview:browser-tab-closed', (data) => {
|
|
679
|
-
ws.emit.project(projectId, 'preview:browser-tab-closed', data);
|
|
682
|
+
ws.emit.project(projectId, 'preview:browser-tab-closed', { ...data, projectId });
|
|
680
683
|
});
|
|
681
684
|
|
|
682
685
|
service.on('preview:browser-tab-switched', (data) => {
|
|
683
|
-
ws.emit.project(projectId, 'preview:browser-tab-switched', data);
|
|
686
|
+
ws.emit.project(projectId, 'preview:browser-tab-switched', { ...data, projectId });
|
|
684
687
|
});
|
|
685
688
|
|
|
686
689
|
service.on('preview:browser-tab-navigated', (data) => {
|
|
687
|
-
ws.emit.project(projectId, 'preview:browser-tab-navigated', data);
|
|
690
|
+
ws.emit.project(projectId, 'preview:browser-tab-navigated', { ...data, projectId });
|
|
688
691
|
});
|
|
689
692
|
|
|
690
693
|
service.on('preview:browser-viewport-changed', (data) => {
|
|
691
|
-
ws.emit.project(projectId, 'preview:browser-viewport-changed', data);
|
|
694
|
+
ws.emit.project(projectId, 'preview:browser-viewport-changed', { ...data, projectId });
|
|
692
695
|
});
|
|
693
696
|
|
|
694
697
|
// Forward console events
|
package/backend/utils/ws.ts
CHANGED
|
@@ -747,7 +747,9 @@ class WSServer {
|
|
|
747
747
|
|
|
748
748
|
const room = this.projectRooms.get(projectId);
|
|
749
749
|
if (!room || room.size === 0) {
|
|
750
|
-
|
|
750
|
+
// Expected/benign: e.g. a background terminal in a project the client
|
|
751
|
+
// has switched away from keeps emitting output to an empty room.
|
|
752
|
+
debug.log('websocket', `No connections in project room: ${projectId} for event: ${String(event)}`);
|
|
751
753
|
return;
|
|
752
754
|
}
|
|
753
755
|
|
package/backend/ws/git/commit.ts
CHANGED
|
@@ -34,4 +34,67 @@ export const commitHandler = createRouter()
|
|
|
34
34
|
const project = requireProjectAccess(conn, data.projectId);
|
|
35
35
|
const hash = await gitService.amendCommit(project.path, data.message);
|
|
36
36
|
return { hash };
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
.http('git:undo-commit', {
|
|
40
|
+
data: t.Object({
|
|
41
|
+
projectId: t.String(),
|
|
42
|
+
mode: t.Union([t.Literal('soft'), t.Literal('mixed'), t.Literal('hard')])
|
|
43
|
+
}),
|
|
44
|
+
response: t.Object({ ok: t.Boolean() })
|
|
45
|
+
}, async ({ data, conn }) => {
|
|
46
|
+
const project = requireProjectAccess(conn, data.projectId);
|
|
47
|
+
await gitService.undoLastCommit(project.path, data.mode);
|
|
48
|
+
return { ok: true };
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
.http('git:revert', {
|
|
52
|
+
data: t.Object({
|
|
53
|
+
projectId: t.String(),
|
|
54
|
+
ref: t.Optional(t.String())
|
|
55
|
+
}),
|
|
56
|
+
response: t.Object({
|
|
57
|
+
success: t.Boolean(),
|
|
58
|
+
message: t.String()
|
|
59
|
+
})
|
|
60
|
+
}, async ({ data, conn }) => {
|
|
61
|
+
const project = requireProjectAccess(conn, data.projectId);
|
|
62
|
+
return await gitService.revertCommit(project.path, data.ref);
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
.http('git:clean', {
|
|
66
|
+
data: t.Object({
|
|
67
|
+
projectId: t.String()
|
|
68
|
+
}),
|
|
69
|
+
response: t.Object({ message: t.String() })
|
|
70
|
+
}, async ({ data, conn }) => {
|
|
71
|
+
const project = requireProjectAccess(conn, data.projectId);
|
|
72
|
+
const message = await gitService.cleanUntracked(project.path);
|
|
73
|
+
return { message };
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
.http('git:gc', {
|
|
77
|
+
data: t.Object({
|
|
78
|
+
projectId: t.String()
|
|
79
|
+
}),
|
|
80
|
+
response: t.Object({ message: t.String() })
|
|
81
|
+
}, async ({ data, conn }) => {
|
|
82
|
+
const project = requireProjectAccess(conn, data.projectId);
|
|
83
|
+
const message = await gitService.optimize(project.path);
|
|
84
|
+
return { message };
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
.http('git:npm-version', {
|
|
88
|
+
data: t.Object({
|
|
89
|
+
projectId: t.String(),
|
|
90
|
+
bump: t.Union([t.Literal('patch'), t.Literal('minor'), t.Literal('major')])
|
|
91
|
+
}),
|
|
92
|
+
response: t.Object({
|
|
93
|
+
success: t.Boolean(),
|
|
94
|
+
version: t.String(),
|
|
95
|
+
message: t.String()
|
|
96
|
+
})
|
|
97
|
+
}, async ({ data, conn }) => {
|
|
98
|
+
const project = requireProjectAccess(conn, data.projectId);
|
|
99
|
+
return await gitService.npmVersion(project.path, data.bump);
|
|
37
100
|
});
|
package/backend/ws/git/remote.ts
CHANGED
|
@@ -40,6 +40,28 @@ export const remoteHandler = createRouter()
|
|
|
40
40
|
data: t.Object({
|
|
41
41
|
projectId: t.String(),
|
|
42
42
|
remote: t.Optional(t.String()),
|
|
43
|
+
branch: t.Optional(t.String()),
|
|
44
|
+
rebase: t.Optional(t.Boolean())
|
|
45
|
+
}),
|
|
46
|
+
response: t.Object({
|
|
47
|
+
success: t.Boolean(),
|
|
48
|
+
message: t.String()
|
|
49
|
+
})
|
|
50
|
+
}, async ({ data, conn }) => {
|
|
51
|
+
const project = requireProjectAccess(conn, data.projectId);
|
|
52
|
+
return await gitService.pull(project.path, data.remote, data.branch, data.rebase);
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
.http('git:push-advanced', {
|
|
56
|
+
data: t.Object({
|
|
57
|
+
projectId: t.String(),
|
|
58
|
+
mode: t.Union([
|
|
59
|
+
t.Literal('with-tags'),
|
|
60
|
+
t.Literal('all-tags'),
|
|
61
|
+
t.Literal('force-lease'),
|
|
62
|
+
t.Literal('force')
|
|
63
|
+
]),
|
|
64
|
+
remote: t.Optional(t.String()),
|
|
43
65
|
branch: t.Optional(t.String())
|
|
44
66
|
}),
|
|
45
67
|
response: t.Object({
|
|
@@ -48,7 +70,20 @@ export const remoteHandler = createRouter()
|
|
|
48
70
|
})
|
|
49
71
|
}, async ({ data, conn }) => {
|
|
50
72
|
const project = requireProjectAccess(conn, data.projectId);
|
|
51
|
-
return await gitService.
|
|
73
|
+
return await gitService.pushAdvanced(project.path, data.mode, data.remote, data.branch);
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
.http('git:fetch-all', {
|
|
77
|
+
data: t.Object({
|
|
78
|
+
projectId: t.String()
|
|
79
|
+
}),
|
|
80
|
+
response: t.Object({
|
|
81
|
+
message: t.String()
|
|
82
|
+
})
|
|
83
|
+
}, async ({ data, conn }) => {
|
|
84
|
+
const project = requireProjectAccess(conn, data.projectId);
|
|
85
|
+
const message = await gitService.fetchAll(project.path);
|
|
86
|
+
return { message };
|
|
52
87
|
})
|
|
53
88
|
|
|
54
89
|
.http('git:push', {
|
package/backend/ws/index.ts
CHANGED
|
@@ -24,6 +24,7 @@ import { messagesRouter } from './messages';
|
|
|
24
24
|
import { settingsRouter } from './settings';
|
|
25
25
|
import { userRouter } from './user';
|
|
26
26
|
import { filesRouter } from './files';
|
|
27
|
+
import { workspaceRouter } from './workspace';
|
|
27
28
|
import { systemRouter } from './system';
|
|
28
29
|
import { tunnelRouter } from './tunnel';
|
|
29
30
|
import { gitRouter } from './git';
|
|
@@ -58,6 +59,7 @@ export const wsRouter = createRouter()
|
|
|
58
59
|
.merge(settingsRouter)
|
|
59
60
|
.merge(userRouter)
|
|
60
61
|
.merge(filesRouter)
|
|
62
|
+
.merge(workspaceRouter)
|
|
61
63
|
.merge(systemRouter)
|
|
62
64
|
.merge(tunnelRouter)
|
|
63
65
|
|
|
@@ -9,7 +9,8 @@
|
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
11
|
import type { WSConnection } from '$shared/utils/ws-server';
|
|
12
|
-
import {
|
|
12
|
+
import { ws } from '$backend/utils/ws';
|
|
13
|
+
import { requireCurrentProjectAccess, requireProjectAccess } from '../access';
|
|
13
14
|
import { browserPreviewServiceManager } from '../../preview/index';
|
|
14
15
|
|
|
15
16
|
type PreviewService = ReturnType<typeof browserPreviewServiceManager.getService>;
|
|
@@ -27,6 +28,24 @@ export function requireBrowserPreviewAccess(conn: WSConnection): BrowserPreviewC
|
|
|
27
28
|
return { userId, projectId, previewService };
|
|
28
29
|
}
|
|
29
30
|
|
|
31
|
+
/**
|
|
32
|
+
* Resolve preview access for an explicitly-supplied project, after verifying
|
|
33
|
+
* the caller is a member. Used by session-recovery reads (tab list / switch)
|
|
34
|
+
* that fire during a project switch: the connection's room context may not have
|
|
35
|
+
* caught up yet (the context-sync round-trip can resolve via timeout), so
|
|
36
|
+
* trusting it would recover the *previous* project's tabs. Passing the target
|
|
37
|
+
* projectId explicitly makes recovery deterministic.
|
|
38
|
+
*/
|
|
39
|
+
export function requireBrowserPreviewAccessFor(
|
|
40
|
+
conn: WSConnection,
|
|
41
|
+
projectId: string
|
|
42
|
+
): BrowserPreviewContext {
|
|
43
|
+
requireProjectAccess(conn, projectId); // throws if the caller isn't a member
|
|
44
|
+
const userId = ws.getUserId(conn);
|
|
45
|
+
const previewService = browserPreviewServiceManager.getService(projectId);
|
|
46
|
+
return { userId, projectId, previewService };
|
|
47
|
+
}
|
|
48
|
+
|
|
30
49
|
/**
|
|
31
50
|
* Resolve a tab in the caller's project. If `tabId` is omitted the active tab
|
|
32
51
|
* is returned. Throws if the caller can't access the project, or if no tab
|
|
@@ -8,7 +8,7 @@ import { t } from 'elysia';
|
|
|
8
8
|
import { createRouter } from '$shared/utils/ws-server';
|
|
9
9
|
import { browserMcpControl } from '../../../preview/browser/browser-mcp-control';
|
|
10
10
|
import { debug } from '$shared/utils/logger';
|
|
11
|
-
import { requireBrowserPreviewAccess, requireBrowserTabAccess } from '../access';
|
|
11
|
+
import { requireBrowserPreviewAccess, requireBrowserPreviewAccessFor, requireBrowserTabAccess } from '../access';
|
|
12
12
|
|
|
13
13
|
export const tabInfoPreviewHandler = createRouter()
|
|
14
14
|
// Get single tab info
|
|
@@ -43,7 +43,10 @@ export const tabInfoPreviewHandler = createRouter()
|
|
|
43
43
|
|
|
44
44
|
// Get all active tabs (for session recovery after browser refresh)
|
|
45
45
|
.http('preview:browser-tabs-list', {
|
|
46
|
-
data: t.Object({
|
|
46
|
+
data: t.Object({
|
|
47
|
+
// Explicit project target for switch-time recovery (see access helper).
|
|
48
|
+
projectId: t.Optional(t.String())
|
|
49
|
+
}),
|
|
47
50
|
response: t.Object({
|
|
48
51
|
tabs: t.Array(t.Object({
|
|
49
52
|
tabId: t.String(),
|
|
@@ -59,8 +62,10 @@ export const tabInfoPreviewHandler = createRouter()
|
|
|
59
62
|
activeTabId: t.Union([t.String(), t.Null()]),
|
|
60
63
|
count: t.Number()
|
|
61
64
|
})
|
|
62
|
-
}, async ({ conn }) => {
|
|
63
|
-
const { projectId, previewService } =
|
|
65
|
+
}, async ({ data, conn }) => {
|
|
66
|
+
const { projectId, previewService } = data.projectId
|
|
67
|
+
? requireBrowserPreviewAccessFor(conn, data.projectId)
|
|
68
|
+
: requireBrowserPreviewAccess(conn);
|
|
64
69
|
|
|
65
70
|
const allTabsInfo = previewService.getAllTabsInfo();
|
|
66
71
|
const activeTab = previewService.getActiveTab();
|
|
@@ -87,7 +92,9 @@ export const tabInfoPreviewHandler = createRouter()
|
|
|
87
92
|
// Switch to a specific tab (for session recovery)
|
|
88
93
|
.http('preview:browser-tab-switch', {
|
|
89
94
|
data: t.Object({
|
|
90
|
-
tabId: t.String()
|
|
95
|
+
tabId: t.String(),
|
|
96
|
+
// Explicit project target for switch-time recovery (see access helper).
|
|
97
|
+
projectId: t.Optional(t.String())
|
|
91
98
|
}),
|
|
92
99
|
response: t.Object({
|
|
93
100
|
success: t.Boolean(),
|
|
@@ -96,7 +103,9 @@ export const tabInfoPreviewHandler = createRouter()
|
|
|
96
103
|
})
|
|
97
104
|
}, async ({ data, conn }) => {
|
|
98
105
|
const { tabId } = data;
|
|
99
|
-
const { projectId, previewService } =
|
|
106
|
+
const { projectId, previewService } = data.projectId
|
|
107
|
+
? requireBrowserPreviewAccessFor(conn, data.projectId)
|
|
108
|
+
: requireBrowserPreviewAccess(conn);
|
|
100
109
|
|
|
101
110
|
const success = previewService.switchTab(tabId);
|
|
102
111
|
if (!success) {
|
|
@@ -60,6 +60,9 @@ export const previewRouter = createRouter()
|
|
|
60
60
|
.merge(mcpPreviewHandler)
|
|
61
61
|
// Server-emitted events (for type safety)
|
|
62
62
|
.emit('preview:browser-tab-opened', t.Object({
|
|
63
|
+
// projectId lets the frontend reject events that belong to a project it has
|
|
64
|
+
// since switched away from (prevents cross-project tab leaks).
|
|
65
|
+
projectId: t.String(),
|
|
63
66
|
tabId: t.String(),
|
|
64
67
|
url: t.String(),
|
|
65
68
|
title: t.String(),
|
|
@@ -67,16 +70,19 @@ export const previewRouter = createRouter()
|
|
|
67
70
|
timestamp: t.Number()
|
|
68
71
|
}))
|
|
69
72
|
.emit('preview:browser-tab-closed', t.Object({
|
|
73
|
+
projectId: t.String(),
|
|
70
74
|
tabId: t.String(),
|
|
71
75
|
newActiveTabId: t.Union([t.String(), t.Null()]),
|
|
72
76
|
timestamp: t.Number()
|
|
73
77
|
}))
|
|
74
78
|
.emit('preview:browser-tab-switched', t.Object({
|
|
79
|
+
projectId: t.String(),
|
|
75
80
|
previousTabId: t.String(),
|
|
76
81
|
newTabId: t.String(),
|
|
77
82
|
timestamp: t.Number()
|
|
78
83
|
}))
|
|
79
84
|
.emit('preview:browser-tab-navigated', t.Object({
|
|
85
|
+
projectId: t.String(),
|
|
80
86
|
tabId: t.String(),
|
|
81
87
|
url: t.String(),
|
|
82
88
|
title: t.String(),
|
|
@@ -145,6 +151,7 @@ export const previewRouter = createRouter()
|
|
|
145
151
|
source: t.Literal('mcp')
|
|
146
152
|
}))
|
|
147
153
|
.emit('preview:browser-viewport-changed', t.Object({
|
|
154
|
+
projectId: t.String(),
|
|
148
155
|
tabId: t.String(),
|
|
149
156
|
deviceSize: t.String(),
|
|
150
157
|
rotation: t.String(),
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Workspace Router
|
|
3
|
+
*
|
|
4
|
+
* Per-project workspace state persistence (dock layout + dock view state).
|
|
5
|
+
*
|
|
6
|
+
* Structure:
|
|
7
|
+
* - state.ts: HTTP endpoints for get/save per-project workspace state
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { createRouter } from '$shared/utils/ws-server';
|
|
11
|
+
import { workspaceStateHandler } from './state';
|
|
12
|
+
|
|
13
|
+
export const workspaceRouter = createRouter()
|
|
14
|
+
.merge(workspaceStateHandler);
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Workspace State Handlers
|
|
3
|
+
*
|
|
4
|
+
* Persist per-user-per-project workspace state (dock layout/ratios/preset,
|
|
5
|
+
* git dock UI state, preview prefs, terminal active tab, and other dock view
|
|
6
|
+
* state). Single source of truth in DB so each project's workspace survives
|
|
7
|
+
* browser refresh and follows the user across devices.
|
|
8
|
+
*
|
|
9
|
+
* Mirrors the files:*-panel-state pattern.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { t } from 'elysia';
|
|
13
|
+
import { createRouter } from '$shared/utils/ws-server';
|
|
14
|
+
import { projectQueries } from '../../database/queries/project-queries';
|
|
15
|
+
import { ws as wsServer } from '../../utils/ws';
|
|
16
|
+
import { requireProjectAccess } from '../access';
|
|
17
|
+
|
|
18
|
+
export const workspaceStateHandler = createRouter()
|
|
19
|
+
.http('workspace:get-state', {
|
|
20
|
+
data: t.Object({
|
|
21
|
+
projectId: t.String()
|
|
22
|
+
}),
|
|
23
|
+
response: t.Object({
|
|
24
|
+
state: t.Union([t.String(), t.Null()])
|
|
25
|
+
})
|
|
26
|
+
}, async ({ data, conn }) => {
|
|
27
|
+
requireProjectAccess(conn, data.projectId);
|
|
28
|
+
const userId = wsServer.getUserId(conn);
|
|
29
|
+
const state = projectQueries.getWorkspaceState(userId, data.projectId);
|
|
30
|
+
return { state };
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
.http('workspace:save-state', {
|
|
34
|
+
data: t.Object({
|
|
35
|
+
projectId: t.String(),
|
|
36
|
+
state: t.Union([t.String(), t.Null()])
|
|
37
|
+
}),
|
|
38
|
+
response: t.Object({ ok: t.Boolean() })
|
|
39
|
+
}, async ({ data, conn }) => {
|
|
40
|
+
requireProjectAccess(conn, data.projectId);
|
|
41
|
+
const userId = wsServer.getUserId(conn);
|
|
42
|
+
projectQueries.setWorkspaceState(userId, data.projectId, data.state);
|
|
43
|
+
return { ok: true };
|
|
44
|
+
});
|