@myrialabs/clopen 0.4.47 → 0.4.49
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 +11 -11
- package/backend/artifacts/project/sources.ts +1 -1
- package/backend/auth/auth-service.ts +52 -358
- package/backend/auth/onboarding.test.ts +2 -3
- package/backend/auth/permissions.test.ts +10 -20
- package/backend/auth/permissions.ts +4 -290
- package/backend/auth/rate-limiter.test.ts +8 -9
- package/backend/auth/rate-limiter.ts +0 -2
- package/backend/auth/tokens.ts +1 -8
- package/backend/bootstrap.ts +7 -0
- package/backend/chat/attachments.test.ts +88 -0
- package/backend/chat/attachments.ts +159 -0
- package/backend/chat/engine-handoff.test.ts +0 -2
- package/backend/chat/message-pages.test.ts +163 -0
- package/backend/chat/message-pages.ts +233 -0
- package/backend/chat/stream-manager.ts +91 -67
- package/backend/chat/turn-answer.test.ts +64 -0
- package/backend/chat/turn-answer.ts +125 -0
- package/backend/chat-apps/awaiting.test.ts +39 -0
- package/backend/chat-apps/awaiting.ts +49 -0
- package/backend/chat-apps/conversation.ts +746 -0
- package/backend/chat-apps/inbound.ts +324 -0
- package/backend/chat-apps/index.ts +309 -0
- package/backend/chat-apps/link-events.ts +20 -0
- package/backend/chat-apps/media.test.ts +62 -0
- package/backend/chat-apps/media.ts +208 -0
- package/backend/chat-apps/messages.test.ts +229 -0
- package/backend/chat-apps/messages.ts +365 -0
- package/backend/chat-apps/outbound.ts +243 -0
- package/backend/chat-apps/pairing.test.ts +45 -0
- package/backend/chat-apps/pairing.ts +74 -0
- package/backend/chat-apps/platforms/index.ts +20 -0
- package/backend/chat-apps/platforms/telegram.test.ts +35 -0
- package/backend/chat-apps/platforms/telegram.ts +191 -0
- package/backend/chat-apps/platforms/types.ts +114 -0
- package/backend/chat-apps/questions.ts +133 -0
- package/backend/chat-apps/runtime.ts +572 -0
- package/backend/chat-apps/storage.test.ts +201 -0
- package/backend/chat-apps/turns.test.ts +45 -0
- package/backend/chat-apps/turns.ts +208 -0
- package/backend/containers/pty.ts +3 -11
- package/backend/control/actions/accounts.ts +89 -0
- package/backend/control/actions/files.ts +116 -0
- package/backend/control/actions/models.ts +188 -0
- package/backend/control/actions/projects.ts +272 -0
- package/backend/control/actions/sessions.ts +280 -0
- package/backend/control/actions/skills.ts +111 -0
- package/backend/control/control.test.ts +169 -0
- package/backend/control/define.ts +20 -0
- package/backend/control/folders.test.ts +67 -0
- package/backend/control/index.ts +213 -0
- package/backend/control/invoke.ts +44 -0
- package/backend/control/links.ts +35 -0
- package/backend/control/paging.ts +26 -0
- package/backend/control/types.ts +117 -0
- package/backend/control/workspace.ts +127 -0
- package/backend/database/migrations/084_create_channel_links.ts +71 -0
- package/backend/database/migrations/085_add_home_projects.ts +36 -0
- package/backend/database/migrations/086_single_owner.ts +418 -0
- package/backend/database/migrations/087_create_blob_line_stats.ts +37 -0
- package/backend/database/migrations/index.ts +28 -0
- package/backend/database/queries/auth-queries.ts +7 -85
- package/backend/database/queries/blob-line-stats-queries.ts +48 -0
- package/backend/database/queries/channel-queries.ts +270 -0
- package/backend/database/queries/db-client-connection-queries.ts +18 -56
- package/backend/database/queries/file-share-queries.ts +5 -23
- package/backend/database/queries/git-identity-queries.ts +31 -42
- package/backend/database/queries/graph-queries.ts +1 -1
- package/backend/database/queries/index.ts +15 -1
- package/backend/database/queries/message-queries.test.ts +146 -0
- package/backend/database/queries/message-queries.ts +42 -1
- package/backend/database/queries/permission-set-queries.ts +1 -1
- package/backend/database/queries/profile-queries.ts +1 -1
- package/backend/database/queries/project-mcp-trust-queries.ts +1 -1
- package/backend/database/queries/project-queries.ts +50 -63
- package/backend/database/queries/session-queries.ts +28 -56
- package/backend/database/queries/ssh-connection-queries.ts +18 -41
- package/backend/db-client/env/diff.ts +9 -33
- package/backend/db-client/env/env.test.ts +8 -8
- package/backend/db-client/env/service.ts +1 -1
- package/backend/db-client/integrations/projector.ts +0 -5
- package/backend/engine/adapters/claude/message-converter.test.ts +47 -0
- package/backend/engine/adapters/claude/message-converter.ts +25 -2
- package/backend/engine/adapters/cline/message-converter.ts +0 -1
- package/backend/engine/adapters/cline/stream.ts +11 -15
- package/backend/engine/adapters/codex/message-converter.ts +0 -2
- package/backend/engine/adapters/copilot/message-converter.ts +1 -2
- package/backend/engine/adapters/cursor/message-converter.ts +0 -1
- package/backend/engine/adapters/opencode/message-converter.ts +2 -4
- package/backend/engine/adapters/opencode/stream.ts +11 -0
- package/backend/engine/adapters/parent-scope.test.ts +38 -0
- package/backend/engine/adapters/parent-scope.ts +44 -0
- package/backend/engine/adapters/pi/message-converter.ts +0 -1
- package/backend/engine/adapters/pi/stream.ts +4 -5
- package/backend/engine/adapters/qwen/message-converter.ts +0 -2
- package/backend/engine/bootstrap-default-engine.ts +2 -2
- package/backend/engine/docs/adapter-contract.md +8 -1
- package/backend/engine/install-runner.ts +8 -17
- package/backend/files/file-shares.ts +20 -44
- package/backend/files/file-size-limit.ts +3 -3
- package/backend/files/file-watcher.ts +19 -0
- package/backend/files/image-processing.ts +1 -1
- package/backend/files/os-clipboard.ts +2 -2
- package/backend/git/contributors.test.ts +35 -0
- package/backend/git/contributors.ts +46 -0
- package/backend/git/git-executor.ts +25 -5
- package/backend/git/git-service.ts +420 -19
- package/backend/git/identity/credential-helper.test.ts +2 -7
- package/backend/git/identity/import-local.test.ts +11 -16
- package/backend/git/identity/import-local.ts +12 -12
- package/backend/git/identity/index.ts +10 -24
- package/backend/git/identity/resolve.ts +13 -14
- package/backend/git/identity/scope-config.ts +33 -28
- package/backend/git/identity/service.ts +44 -47
- package/backend/git/op-lock.test.ts +111 -0
- package/backend/git/op-lock.ts +180 -0
- package/backend/git/remote-sync.test.ts +249 -0
- package/backend/host/metrics.ts +1 -1
- package/backend/http/bearer-auth.ts +1 -2
- package/backend/http/files-download.test.ts +8 -9
- package/backend/http/files-download.ts +2 -2
- package/backend/http/files-shared.test.ts +62 -89
- package/backend/http/files-shared.ts +2 -2
- package/backend/http/files-upload.test.ts +3 -7
- package/backend/http/files-upload.ts +3 -3
- package/backend/http/notes-images.test.ts +15 -38
- package/backend/http/notes-images.ts +6 -8
- package/backend/http/ssh-sftp.ts +5 -10
- package/backend/http/static-assets.test.ts +78 -0
- package/backend/http/static-assets.ts +104 -0
- package/backend/index.ts +32 -16
- package/backend/integrations/accounts.ts +61 -3
- package/backend/integrations/index.ts +2 -2
- package/backend/integrations/registry/define.ts +15 -0
- package/backend/integrations/registry/index.ts +2 -0
- package/backend/integrations/registry/providers/telegram.ts +54 -0
- package/backend/integrations/registry/types.ts +21 -1
- package/backend/integrations/webhooks/gateway.ts +7 -6
- package/backend/integrations/webhooks/verify.test.ts +30 -0
- package/backend/integrations/webhooks/verify.ts +4 -0
- package/backend/mcp/external/project.ts +1 -1
- package/backend/mcp/internal/remote-server.test.ts +1 -3
- package/backend/mcp/internal/remote-server.ts +4 -4
- package/backend/mcp/internal/servers/browser-automation/actions/dialogs.ts +67 -0
- package/backend/mcp/internal/servers/browser-automation/actions/index.ts +3 -1
- package/backend/mcp/internal/servers/browser-automation/actions/input.ts +5 -5
- package/backend/mcp/internal/servers/browser-automation/actions/tabs.ts +157 -28
- package/backend/mcp/internal/servers/browser-automation/context.ts +1 -0
- package/backend/mcp/internal/servers/browser-automation/description.ts +5 -2
- package/backend/mcp/internal/servers/browser-automation/format.ts +24 -1
- package/backend/mcp/internal/servers/browser-automation/runner.ts +68 -3
- package/backend/memory/redact.ts +1 -1
- package/backend/notes/access.test.ts +28 -29
- package/backend/notes/access.ts +6 -11
- package/backend/permissions/service.ts +1 -1
- package/backend/preview/browser/browser-clipboard.test.ts +46 -0
- package/backend/preview/browser/browser-clipboard.ts +67 -0
- package/backend/preview/browser/browser-dialog-handler.ts +95 -6
- package/backend/preview/browser/browser-host-bridge.ts +9 -0
- package/backend/preview/browser/browser-interaction-handler.ts +316 -86
- package/backend/preview/browser/browser-mcp-control.ts +34 -0
- package/backend/preview/browser/browser-pool.ts +67 -6
- package/backend/preview/browser/browser-preview-service.ts +383 -29
- package/backend/preview/browser/browser-tab-manager.ts +66 -23
- package/backend/preview/browser/browser-video-capture.ts +92 -5
- package/backend/preview/browser/element-locator.ts +190 -17
- package/backend/preview/browser/keyboard-chords.test.ts +68 -0
- package/backend/preview/browser/keyboard-chords.ts +132 -0
- package/backend/preview/browser/paste.ts +81 -0
- package/backend/preview/browser/process-usage.ts +142 -0
- package/backend/preview/browser/profile-lock.ts +209 -0
- package/backend/preview/browser/scripts/host-bridge.ts +59 -0
- package/backend/preview/browser/scripts/video-stream.ts +58 -0
- package/backend/preview/browser/types.ts +40 -2
- package/backend/preview/browser/viewport.test.ts +45 -0
- package/backend/preview/browser/viewport.ts +96 -0
- package/backend/profiles/service.ts +2 -2
- package/backend/project/status-manager.ts +43 -172
- package/backend/projects/home.ts +61 -0
- package/backend/projects/project-cleanup.integration.test.ts +2 -2
- package/backend/projects/project-cleanup.ts +0 -8
- package/backend/skills/service.ts +2 -3
- package/backend/snapshot/blob-store.ts +9 -1
- package/backend/snapshot/helpers.ts +3 -11
- package/backend/snapshot/line-stats.test.ts +80 -0
- package/backend/snapshot/line-stats.ts +64 -0
- package/backend/snapshot/restore.ts +252 -0
- package/backend/snapshot/snapshot-service.ts +14 -27
- package/backend/snapshot/turn-changes.test.ts +10 -5
- package/backend/snapshot/turn-changes.ts +78 -40
- package/backend/ssh/ptykit.ts +1 -2
- package/backend/terminal/ptykit.ts +10 -8
- package/backend/tunnel/public-origin.ts +31 -20
- package/backend/utils/user-helpers.ts +0 -55
- package/backend/utils/ws-wire.test.ts +87 -0
- package/backend/utils/ws-wire.ts +189 -0
- package/backend/utils/ws.ts +101 -192
- package/backend/worktrees/branching/engine.ts +3 -16
- package/backend/worktrees/branching/index.ts +1 -1
- package/backend/worktrees/branching/projector.ts +1 -1
- package/backend/worktrees/branching/service.ts +13 -25
- package/backend/ws/access.ts +12 -21
- package/backend/ws/artifacts/generate.ts +2 -2
- package/backend/ws/auth/device.ts +16 -71
- package/backend/ws/auth/index.ts +4 -22
- package/backend/ws/auth/login.test.ts +14 -60
- package/backend/ws/auth/login.ts +24 -124
- package/backend/ws/auth/status.ts +0 -1
- package/backend/ws/chat/composer.ts +118 -118
- package/backend/ws/chat/stream.ts +105 -57
- package/backend/ws/chat-apps/index.ts +117 -0
- package/backend/ws/containers/actions.ts +11 -25
- package/backend/ws/containers/index.ts +3 -4
- package/backend/ws/containers/list.ts +10 -36
- package/backend/ws/containers/logs.ts +2 -2
- package/backend/ws/containers/tunnel.ts +1 -2
- package/backend/ws/db-client/access.ts +2 -16
- package/backend/ws/db-client/accounts.ts +5 -15
- package/backend/ws/db-client/connections.ts +17 -25
- package/backend/ws/db-client/context.ts +1 -1
- package/backend/ws/db-client/env.ts +7 -10
- package/backend/ws/db-client/index.ts +2 -2
- package/backend/ws/db-client/io.ts +5 -5
- package/backend/ws/db-client/query-history-retention.test.ts +8 -8
- package/backend/ws/db-client/query.ts +18 -17
- package/backend/ws/db-client/schema.ts +13 -13
- package/backend/ws/db-client/structure.ts +35 -35
- package/backend/ws/db-client/supabase.ts +4 -21
- package/backend/ws/deployments/context.ts +1 -1
- package/backend/ws/deployments/crud.ts +0 -5
- package/backend/ws/engine/access.ts +7 -20
- package/backend/ws/engine/claude/accounts.ts +4 -4
- package/backend/ws/engine/cline/accounts.ts +5 -5
- package/backend/ws/engine/codex/accounts.ts +3 -3
- package/backend/ws/engine/pi/accounts.ts +5 -5
- package/backend/ws/files/clipboard.ts +8 -27
- package/backend/ws/files/image-edit.ts +6 -6
- package/backend/ws/files/index.ts +1 -1
- package/backend/ws/files/path-access.test.ts +32 -131
- package/backend/ws/files/path-access.ts +8 -87
- package/backend/ws/files/read.ts +45 -19
- package/backend/ws/files/reveal.ts +3 -3
- package/backend/ws/files/search.ts +7 -7
- package/backend/ws/files/share.ts +9 -13
- package/backend/ws/files/state.ts +9 -12
- package/backend/ws/files/watch.ts +3 -3
- package/backend/ws/files/write.ts +30 -31
- package/backend/ws/git/branch.ts +80 -59
- package/backend/ws/git/commit.ts +63 -44
- package/backend/ws/git/conflict.ts +16 -18
- package/backend/ws/git/identity-context.ts +8 -17
- package/backend/ws/git/identity.ts +17 -27
- package/backend/ws/git/index.ts +3 -1
- package/backend/ws/git/log.ts +52 -0
- package/backend/ws/git/op-state.ts +139 -0
- package/backend/ws/git/remote.ts +241 -141
- package/backend/ws/git/repo-context.ts +67 -0
- package/backend/ws/git/staging.ts +18 -28
- package/backend/ws/git/status.ts +5 -2
- package/backend/ws/index.ts +5 -1
- package/backend/ws/instructions/crud.ts +1 -1
- package/backend/ws/integrations/crud.ts +1 -3
- package/backend/ws/mcp/crud.ts +3 -5
- package/backend/ws/mcp/tools.ts +1 -1
- package/backend/ws/memory/crud.ts +14 -46
- package/backend/ws/memory/index.ts +2 -2
- package/backend/ws/messages/crud.ts +78 -8
- package/backend/ws/notes/index.ts +33 -39
- package/backend/ws/permissions/crud.ts +3 -4
- package/backend/ws/ports/index.ts +1 -2
- package/backend/ws/ports/kill.ts +5 -10
- package/backend/ws/ports/list.ts +4 -10
- package/backend/ws/preview/access.ts +1 -1
- package/backend/ws/preview/browser/interact.ts +52 -79
- package/backend/ws/preview/browser/native-ui.ts +24 -0
- package/backend/ws/preview/browser/stats.ts +98 -1
- package/backend/ws/preview/browser/tab-info.ts +15 -0
- package/backend/ws/preview/browser/tab.ts +19 -4
- package/backend/ws/preview/index.ts +29 -0
- package/backend/ws/preview/watchers.ts +84 -0
- package/backend/ws/profiles/crud.ts +6 -8
- package/backend/ws/project-artifacts/index.ts +30 -31
- package/backend/ws/projects/crud.ts +33 -35
- package/backend/ws/projects/index.ts +3 -8
- package/backend/ws/projects/info.ts +3 -3
- package/backend/ws/projects/overview.ts +19 -30
- package/backend/ws/projects/status.ts +17 -57
- package/backend/ws/sessions/crud.ts +42 -57
- package/backend/ws/sessions/index.ts +1 -1
- package/backend/ws/settings/crud.ts +1 -1
- package/backend/ws/skills/crud.ts +3 -4
- package/backend/ws/skills/index.ts +1 -1
- package/backend/ws/snapshot/changes.ts +12 -13
- package/backend/ws/snapshot/restore.ts +11 -295
- package/backend/ws/snapshot/timeline.ts +2 -4
- package/backend/ws/ssh/access.ts +6 -21
- package/backend/ws/ssh/connections.ts +21 -31
- package/backend/ws/ssh/forwards.ts +12 -12
- package/backend/ws/ssh/sftp.ts +34 -34
- package/backend/ws/ssh/tunnel.ts +1 -2
- package/backend/ws/stack/install.ts +7 -17
- package/backend/ws/subagents/crud.ts +1 -1
- package/backend/ws/terminal/tunnel.ts +2 -2
- package/backend/ws/tunnel/share.ts +0 -2
- package/backend/ws/user/crud.ts +1 -59
- package/backend/ws/user/index.ts +1 -1
- package/backend/ws/work/context.ts +1 -1
- package/backend/ws/work/crud.ts +0 -5
- package/backend/ws/workspace/state.ts +10 -13
- package/backend/ws/worktrees/branching.ts +7 -36
- package/backend/ws/worktrees/crud.ts +2 -2
- package/bin/clopen.ts +46 -41
- package/dist/assets/addon-clipboard-wHJhZAA4.js.br +0 -0
- package/dist/assets/addon-clipboard-wHJhZAA4.js.gz +0 -0
- package/dist/assets/addon-fit-DX4qG4td.js.br +1 -0
- package/dist/assets/addon-fit-DX4qG4td.js.gz +0 -0
- package/dist/assets/addon-ligatures-DpDY8N_X.js.br +0 -0
- package/dist/assets/addon-ligatures-DpDY8N_X.js.gz +0 -0
- package/dist/assets/addon-unicode11-Bt8F3D7-.js.br +0 -0
- package/dist/assets/addon-unicode11-Bt8F3D7-.js.gz +0 -0
- package/dist/assets/addon-web-links-DIbG5aQx.js.br +0 -0
- package/dist/assets/addon-web-links-DIbG5aQx.js.gz +0 -0
- package/dist/assets/diff.worker-BoHLxmr2.js +7 -0
- package/dist/assets/diff.worker-BoHLxmr2.js.br +0 -0
- package/dist/assets/diff.worker-BoHLxmr2.js.gz +0 -0
- package/dist/assets/graph-layout.worker-CRFqN7ZQ.js.br +0 -0
- package/dist/assets/graph-layout.worker-CRFqN7ZQ.js.gz +0 -0
- package/dist/assets/{index-CwuKQelV.js → index-CrVWZH_D.js} +1 -1
- package/dist/assets/index-CrVWZH_D.js.br +0 -0
- package/dist/assets/index-CrVWZH_D.js.gz +0 -0
- package/dist/assets/{index-BTcR1byF.js → index-DD0-3EWg.js} +1 -1
- package/dist/assets/index-DD0-3EWg.js.br +0 -0
- package/dist/assets/index-DD0-3EWg.js.gz +0 -0
- package/dist/assets/main-D2_TmcwQ.js +1240 -0
- package/dist/assets/main-D2_TmcwQ.js.br +0 -0
- package/dist/assets/main-D2_TmcwQ.js.gz +0 -0
- package/dist/assets/main-Yucigcmf.css +1 -0
- package/dist/assets/main-Yucigcmf.css.br +0 -0
- package/dist/assets/main-Yucigcmf.css.gz +0 -0
- package/dist/assets/preview-video.worker-DcpDhCWL.js.br +0 -0
- package/dist/assets/preview-video.worker-DcpDhCWL.js.gz +0 -0
- package/dist/assets/xterm-B-qIQCd3.js.br +0 -0
- package/dist/assets/xterm-B-qIQCd3.js.gz +0 -0
- package/dist/assets/zipkit-engine-CaR0t_Uy.wasm.br +0 -0
- package/dist/assets/zipkit-engine-CaR0t_Uy.wasm.gz +0 -0
- package/dist/fonts/dm-sans.css.br +0 -0
- package/dist/fonts/dm-sans.css.gz +0 -0
- package/dist/index.html +2 -2
- package/dist/index.html.br +0 -0
- package/dist/index.html.gz +0 -0
- package/dist/sw.js.br +0 -0
- package/dist/sw.js.gz +0 -0
- package/package.json +3 -3
- package/shared/constants/attachments.ts +14 -0
- package/shared/constants/engine-tools.ts +1 -1
- package/shared/constants/preview.test.ts +259 -0
- package/shared/constants/preview.ts +1018 -57
- package/shared/constants/tool-icons.ts +5 -0
- package/shared/types/containers/container.ts +1 -1
- package/shared/types/database/schema.ts +9 -6
- package/shared/types/git-identity.ts +1 -1
- package/shared/types/git.ts +116 -0
- package/shared/types/integrations.ts +7 -0
- package/shared/types/stores/settings.ts +1 -1
- package/shared/types/unified/index.ts +1 -1
- package/shared/types/unified/message.ts +0 -7
- package/shared/types/unified/stream.ts +13 -5
- package/shared/types/unified/tool.ts +15 -1
- package/shared/utils/ask-user-question.test.ts +319 -0
- package/shared/utils/ask-user-question.ts +154 -0
- package/shared/utils/diff-calculator.test.ts +178 -41
- package/shared/utils/diff-calculator.ts +383 -100
- package/shared/utils/home-project.ts +25 -0
- package/shared/utils/logger.ts +2 -0
- package/shared/utils/process-supervisor.test.ts +172 -0
- package/shared/utils/process-supervisor.ts +251 -0
- package/shared/utils/ws-chunk.test.ts +33 -0
- package/shared/utils/ws-chunk.ts +73 -0
- package/shared/utils/ws-client-chunks.test.ts +45 -0
- package/shared/utils/ws-client.ts +43 -0
- package/shared/utils/ws-server.ts +49 -21
- package/backend/auth/session-invalidation.test.ts +0 -112
- package/backend/auth/session-invalidation.ts +0 -77
- package/backend/project/status-manager.test.ts +0 -32
- package/backend/ws/auth/invites.ts +0 -94
- package/backend/ws/auth/users.ts +0 -112
- package/backend/ws/projects/presence.ts +0 -46
- package/backend/ws/stack/access.ts +0 -31
- package/dist/assets/main-BiDNMWb3.css +0 -1
- package/dist/assets/main-BlhKWz2B.js +0 -1241
package/backend/utils/ws.ts
CHANGED
|
@@ -34,18 +34,13 @@ import type { WSConnection } from '$shared/utils/ws-server';
|
|
|
34
34
|
import { encodeBinaryMessage, isBinaryAction } from '$shared/utils/ws-server';
|
|
35
35
|
import { debug } from '$shared/utils/logger';
|
|
36
36
|
import { clientIpFromConnection } from './client-ip';
|
|
37
|
+
import { sendBinary, sendText } from './ws-wire';
|
|
37
38
|
|
|
38
|
-
/**
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
MAX_BUFFER_SIZE: 1024 * 1024, // 1MB
|
|
44
|
-
/** Enable backpressure handling */
|
|
45
|
-
ENABLE_BACKPRESSURE: true,
|
|
46
|
-
/** Log dropped frames */
|
|
47
|
-
LOG_DROPPED_FRAMES: true
|
|
48
|
-
};
|
|
39
|
+
/** The action of a serialized `{"action":"…",…}` event, for the traffic meter. */
|
|
40
|
+
function actionOf(message: string): string {
|
|
41
|
+
const match = /^\{"action":"([^"]+)"/.exec(message);
|
|
42
|
+
return match ? match[1] : 'unknown';
|
|
43
|
+
}
|
|
49
44
|
|
|
50
45
|
/**
|
|
51
46
|
* Metrics for monitoring
|
|
@@ -54,7 +49,6 @@ interface WSMetrics {
|
|
|
54
49
|
totalConnections: number;
|
|
55
50
|
activeProjects: number;
|
|
56
51
|
activeUsers: number;
|
|
57
|
-
droppedFrames: number;
|
|
58
52
|
totalEmits: number;
|
|
59
53
|
}
|
|
60
54
|
|
|
@@ -72,8 +66,6 @@ interface ConnectionState {
|
|
|
72
66
|
cleanups: Set<() => void>;
|
|
73
67
|
/** Whether this connection has been authenticated */
|
|
74
68
|
authenticated: boolean;
|
|
75
|
-
/** User role (admin or member) — set by auth handler */
|
|
76
|
-
role: 'admin' | 'member' | null;
|
|
77
69
|
/** Hash of the session token used for this connection */
|
|
78
70
|
sessionTokenHash: string | null;
|
|
79
71
|
/** Epoch ms of the last DB session re-validation (throttles the auth-gate check). */
|
|
@@ -105,15 +97,20 @@ class WSServer {
|
|
|
105
97
|
/** Room-based: Chat Session ID → Map<wsId, WSConnection> */
|
|
106
98
|
private chatSessionRooms = new Map<string, Map<string, WSConnection>>();
|
|
107
99
|
|
|
108
|
-
/**
|
|
109
|
-
|
|
100
|
+
/**
|
|
101
|
+
* Virtual connections — an identity with no socket behind it, so a caller
|
|
102
|
+
* that is not a browser (a chat app, later an MCP client) can run a route
|
|
103
|
+
* through the SAME router, auth gate and validation a tab goes through.
|
|
104
|
+
* Kept out of `connections` and every room on purpose: a virtual caller is
|
|
105
|
+
* not "online", must not show in presence, and must not receive broadcasts.
|
|
106
|
+
*/
|
|
107
|
+
private virtualIds = new Set<string>();
|
|
110
108
|
|
|
111
109
|
/** Metrics tracking */
|
|
112
110
|
private metrics: WSMetrics = {
|
|
113
111
|
totalConnections: 0,
|
|
114
112
|
activeProjects: 0,
|
|
115
113
|
activeUsers: 0,
|
|
116
|
-
droppedFrames: 0,
|
|
117
114
|
totalEmits: 0
|
|
118
115
|
};
|
|
119
116
|
|
|
@@ -151,7 +148,7 @@ class WSServer {
|
|
|
151
148
|
const id = crypto.randomUUID();
|
|
152
149
|
this.rawToId.set(raw, id);
|
|
153
150
|
this.connections.set(id, conn);
|
|
154
|
-
this.connectionState.set(id, { userId: null, projectId: null, worktreeId: null, chatSessionIds: new Set(), cleanups: new Set(), authenticated: false,
|
|
151
|
+
this.connectionState.set(id, { userId: null, projectId: null, worktreeId: null, chatSessionIds: new Set(), cleanups: new Set(), authenticated: false, sessionTokenHash: null, lastSessionCheck: 0 });
|
|
155
152
|
|
|
156
153
|
this.metrics.totalConnections = this.connections.size;
|
|
157
154
|
debug.log('websocket', `Connection registered: ${id} (total: ${this.connections.size})`);
|
|
@@ -165,6 +162,7 @@ class WSServer {
|
|
|
165
162
|
*/
|
|
166
163
|
private ensureRegistered(conn: WSConnection): string {
|
|
167
164
|
const existingId = this.resolveId(conn);
|
|
165
|
+
if (existingId && this.virtualIds.has(existingId)) return existingId;
|
|
168
166
|
if (existingId && this.connections.has(existingId)) {
|
|
169
167
|
this.connections.set(existingId, conn);
|
|
170
168
|
return existingId;
|
|
@@ -279,7 +277,7 @@ class WSServer {
|
|
|
279
277
|
|
|
280
278
|
/**
|
|
281
279
|
* Set user ID for a connection.
|
|
282
|
-
* Tracks which
|
|
280
|
+
* Tracks which account this connection signed in as (for emit.user / emit.signedIn).
|
|
283
281
|
*/
|
|
284
282
|
setUser(conn: WSConnection, userId: string | null): void {
|
|
285
283
|
const wsId = this.ensureRegistered(conn);
|
|
@@ -310,16 +308,6 @@ class WSServer {
|
|
|
310
308
|
this.userConnections.set(userId, new Map());
|
|
311
309
|
}
|
|
312
310
|
this.userConnections.get(userId)!.set(wsId, conn);
|
|
313
|
-
|
|
314
|
-
// If connection already has a project, track membership
|
|
315
|
-
// (handles case where setUser is called after setProject)
|
|
316
|
-
const currentProjectId = state?.projectId;
|
|
317
|
-
if (currentProjectId) {
|
|
318
|
-
if (!this.projectMembers.has(currentProjectId)) {
|
|
319
|
-
this.projectMembers.set(currentProjectId, new Set());
|
|
320
|
-
}
|
|
321
|
-
this.projectMembers.get(currentProjectId)!.add(userId);
|
|
322
|
-
}
|
|
323
311
|
}
|
|
324
312
|
|
|
325
313
|
this.metrics.activeUsers = this.userConnections.size;
|
|
@@ -379,16 +367,6 @@ class WSServer {
|
|
|
379
367
|
this.projectRooms.set(projectId, new Map());
|
|
380
368
|
}
|
|
381
369
|
this.projectRooms.get(projectId)!.set(wsId, conn);
|
|
382
|
-
|
|
383
|
-
// Track project membership (persists across project switches)
|
|
384
|
-
// Used by emit.projectMembers() for cross-project notifications
|
|
385
|
-
const userId = state?.userId;
|
|
386
|
-
if (userId) {
|
|
387
|
-
if (!this.projectMembers.has(projectId)) {
|
|
388
|
-
this.projectMembers.set(projectId, new Set());
|
|
389
|
-
}
|
|
390
|
-
this.projectMembers.get(projectId)!.add(userId);
|
|
391
|
-
}
|
|
392
370
|
}
|
|
393
371
|
|
|
394
372
|
this.metrics.activeProjects = this.projectRooms.size;
|
|
@@ -495,56 +473,6 @@ class WSServer {
|
|
|
495
473
|
state.chatSessionIds.clear();
|
|
496
474
|
}
|
|
497
475
|
|
|
498
|
-
/**
|
|
499
|
-
* Get user info for all connections in a chat session room.
|
|
500
|
-
* Returns deduplicated list of { userId, userName } for users currently viewing the session.
|
|
501
|
-
*/
|
|
502
|
-
getChatSessionUsers(chatSessionId: string): { userId: string; userName: string }[] {
|
|
503
|
-
const room = this.chatSessionRooms.get(chatSessionId);
|
|
504
|
-
if (!room || room.size === 0) return [];
|
|
505
|
-
|
|
506
|
-
const seen = new Map<string, string>(); // userId → userName
|
|
507
|
-
for (const [wsId] of room) {
|
|
508
|
-
const state = this.connectionState.get(wsId);
|
|
509
|
-
if (state?.userId) {
|
|
510
|
-
// Resolve userName from user connections or fallback
|
|
511
|
-
if (!seen.has(state.userId)) {
|
|
512
|
-
seen.set(state.userId, state.userId); // default to userId
|
|
513
|
-
}
|
|
514
|
-
}
|
|
515
|
-
}
|
|
516
|
-
return Array.from(seen.entries()).map(([userId, userName]) => ({ userId, userName }));
|
|
517
|
-
}
|
|
518
|
-
|
|
519
|
-
/**
|
|
520
|
-
* Get all chat session IDs that have active connections for a given project.
|
|
521
|
-
* Used for per-session presence data.
|
|
522
|
-
*/
|
|
523
|
-
getProjectChatSessions(projectId: string): Map<string, { userId: string }[]> {
|
|
524
|
-
const result = new Map<string, { userId: string }[]>();
|
|
525
|
-
|
|
526
|
-
// Iterate through all chat session rooms
|
|
527
|
-
for (const [chatSessionId, room] of this.chatSessionRooms) {
|
|
528
|
-
const users: { userId: string }[] = [];
|
|
529
|
-
const seenUsers = new Set<string>();
|
|
530
|
-
|
|
531
|
-
for (const [wsId] of room) {
|
|
532
|
-
const state = this.connectionState.get(wsId);
|
|
533
|
-
// Only include connections that belong to this project
|
|
534
|
-
if (state?.projectId === projectId && state?.userId && !seenUsers.has(state.userId)) {
|
|
535
|
-
seenUsers.add(state.userId);
|
|
536
|
-
users.push({ userId: state.userId });
|
|
537
|
-
}
|
|
538
|
-
}
|
|
539
|
-
|
|
540
|
-
if (users.length > 0) {
|
|
541
|
-
result.set(chatSessionId, users);
|
|
542
|
-
}
|
|
543
|
-
}
|
|
544
|
-
|
|
545
|
-
return result;
|
|
546
|
-
}
|
|
547
|
-
|
|
548
476
|
/**
|
|
549
477
|
* Get raw connection state for a connection.
|
|
550
478
|
* Used internally by ws:set-context to read back current values.
|
|
@@ -566,30 +494,65 @@ class WSServer {
|
|
|
566
494
|
|
|
567
495
|
/**
|
|
568
496
|
* Set authentication state for a connection.
|
|
569
|
-
* Called by auth handlers after successful login/setup/
|
|
497
|
+
* Called by auth handlers after successful login/setup/device claim.
|
|
570
498
|
*/
|
|
571
|
-
setAuth(conn: WSConnection, userId: string,
|
|
499
|
+
setAuth(conn: WSConnection, userId: string, sessionTokenHash: string): void {
|
|
572
500
|
const wsId = this.ensureRegistered(conn);
|
|
573
501
|
const state = this.connectionState.get(wsId);
|
|
574
502
|
if (state) {
|
|
575
503
|
state.authenticated = true;
|
|
576
|
-
state.role = role;
|
|
577
504
|
state.sessionTokenHash = sessionTokenHash;
|
|
578
505
|
}
|
|
579
506
|
// Also set userId via existing method (handles room management)
|
|
580
507
|
this.setUser(conn, userId);
|
|
581
|
-
debug.log('websocket', `Connection ${wsId} authenticated: userId=${userId}
|
|
508
|
+
debug.log('websocket', `Connection ${wsId} authenticated: userId=${userId}`);
|
|
582
509
|
// A device just came online — refresh Remote Access counts/lists everywhere.
|
|
583
510
|
this.notifyPresenceChanged();
|
|
584
511
|
}
|
|
585
512
|
|
|
586
513
|
/**
|
|
587
|
-
*
|
|
514
|
+
* Open a virtual connection acting as `userId`, viewing `projectId`. Authenticated by construction — the caller vouches for the
|
|
515
|
+
* identity (a chat app proved it by pairing) — and never registered in any
|
|
516
|
+
* room. Always pair with `closeVirtualConnection()`.
|
|
588
517
|
*/
|
|
589
|
-
|
|
518
|
+
openVirtualConnection(identity: { userId: string; projectId?: string | null }): WSConnection {
|
|
519
|
+
const raw = {};
|
|
520
|
+
const id = `virtual-${crypto.randomUUID()}`;
|
|
521
|
+
const conn = { raw, readyState: 1, send: () => {}, close: () => {} } as unknown as WSConnection;
|
|
522
|
+
this.rawToId.set(raw, id);
|
|
523
|
+
this.virtualIds.add(id);
|
|
524
|
+
this.connectionState.set(id, {
|
|
525
|
+
userId: identity.userId,
|
|
526
|
+
projectId: identity.projectId ?? null,
|
|
527
|
+
worktreeId: null,
|
|
528
|
+
chatSessionIds: new Set(),
|
|
529
|
+
cleanups: new Set(),
|
|
530
|
+
authenticated: true,
|
|
531
|
+
sessionTokenHash: null,
|
|
532
|
+
lastSessionCheck: Date.now()
|
|
533
|
+
});
|
|
534
|
+
return conn;
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
/** Run a virtual connection's cleanups and forget it. */
|
|
538
|
+
closeVirtualConnection(conn: WSConnection): void {
|
|
590
539
|
const id = this.resolveId(conn);
|
|
591
|
-
if (!id) return
|
|
592
|
-
|
|
540
|
+
if (!id || !this.virtualIds.has(id)) return;
|
|
541
|
+
const state = this.connectionState.get(id);
|
|
542
|
+
for (const cleanup of state?.cleanups ?? []) {
|
|
543
|
+
try {
|
|
544
|
+
cleanup();
|
|
545
|
+
} catch (err) {
|
|
546
|
+
debug.warn('websocket', `Cleanup error for virtual connection ${id}:`, err);
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
this.connectionState.delete(id);
|
|
550
|
+
this.virtualIds.delete(id);
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
isVirtual(conn: WSConnection): boolean {
|
|
554
|
+
const id = this.resolveId(conn);
|
|
555
|
+
return Boolean(id && this.virtualIds.has(id));
|
|
593
556
|
}
|
|
594
557
|
|
|
595
558
|
/**
|
|
@@ -620,7 +583,6 @@ class WSServer {
|
|
|
620
583
|
const state = this.connectionState.get(id);
|
|
621
584
|
if (state) {
|
|
622
585
|
state.authenticated = false;
|
|
623
|
-
state.role = null;
|
|
624
586
|
state.sessionTokenHash = null;
|
|
625
587
|
}
|
|
626
588
|
debug.log('websocket', `Connection ${id} auth cleared`);
|
|
@@ -637,7 +599,6 @@ class WSServer {
|
|
|
637
599
|
for (const [id, state] of this.connectionState) {
|
|
638
600
|
if (state.authenticated) {
|
|
639
601
|
state.authenticated = false;
|
|
640
|
-
state.role = null;
|
|
641
602
|
state.sessionTokenHash = null;
|
|
642
603
|
cleared++;
|
|
643
604
|
}
|
|
@@ -648,52 +609,6 @@ class WSServer {
|
|
|
648
609
|
return cleared;
|
|
649
610
|
}
|
|
650
611
|
|
|
651
|
-
/**
|
|
652
|
-
* Get all active WebSocket connections for a specific user.
|
|
653
|
-
* Used for session invalidation when project access changes.
|
|
654
|
-
*
|
|
655
|
-
* @param userId - The user ID to look up
|
|
656
|
-
* @returns Array of active WSConnection objects for the user
|
|
657
|
-
*/
|
|
658
|
-
getConnectionsForUser(userId: string): WSConnection[] {
|
|
659
|
-
const userConns = this.userConnections.get(userId);
|
|
660
|
-
if (!userConns) return [];
|
|
661
|
-
|
|
662
|
-
const result: WSConnection[] = [];
|
|
663
|
-
for (const wsConn of userConns.values()) {
|
|
664
|
-
if (wsConn.readyState === 1) { // Only active connections
|
|
665
|
-
result.push(wsConn);
|
|
666
|
-
}
|
|
667
|
-
}
|
|
668
|
-
return result;
|
|
669
|
-
}
|
|
670
|
-
|
|
671
|
-
/**
|
|
672
|
-
* Clear authentication state for a specific set of connections.
|
|
673
|
-
* Used for targeted session invalidation (e.g., when project access is revoked).
|
|
674
|
-
*
|
|
675
|
-
* @param connections - Array of connections to clear
|
|
676
|
-
* @returns Number of connections that were cleared
|
|
677
|
-
*/
|
|
678
|
-
clearAuthForConnections(connections: WSConnection[]): number {
|
|
679
|
-
let cleared = 0;
|
|
680
|
-
for (const conn of connections) {
|
|
681
|
-
const id = this.resolveId(conn);
|
|
682
|
-
if (!id) continue;
|
|
683
|
-
const state = this.connectionState.get(id);
|
|
684
|
-
if (state && state.authenticated) {
|
|
685
|
-
state.authenticated = false;
|
|
686
|
-
state.role = null;
|
|
687
|
-
state.sessionTokenHash = null;
|
|
688
|
-
cleared++;
|
|
689
|
-
}
|
|
690
|
-
}
|
|
691
|
-
if (cleared > 0) {
|
|
692
|
-
debug.log('websocket', `Cleared auth on ${cleared} targeted connection(s)`);
|
|
693
|
-
}
|
|
694
|
-
return cleared;
|
|
695
|
-
}
|
|
696
|
-
|
|
697
612
|
/**
|
|
698
613
|
* Get the session token hash bound to a connection, or null.
|
|
699
614
|
*/
|
|
@@ -772,7 +687,6 @@ class WSServer {
|
|
|
772
687
|
const state = this.connectionState.get(id);
|
|
773
688
|
if (state) {
|
|
774
689
|
state.authenticated = false;
|
|
775
|
-
state.role = null;
|
|
776
690
|
state.sessionTokenHash = null;
|
|
777
691
|
}
|
|
778
692
|
this.sendEventToConnection(conn, 'auth:force-logout-user', { reason });
|
|
@@ -810,33 +724,22 @@ class WSServer {
|
|
|
810
724
|
}
|
|
811
725
|
|
|
812
726
|
/**
|
|
813
|
-
*
|
|
727
|
+
* Whether a connection can be written to at all.
|
|
728
|
+
*
|
|
729
|
+
* Deliberately not a backpressure gate. An earlier one dropped events while
|
|
730
|
+
* the socket's buffer held more than 1 MB — but it read `getBufferedAmount`
|
|
731
|
+
* off Elysia's wrapper, which does not have it, so it never fired. Making it
|
|
732
|
+
* fire would be worse than leaving it dead: events are a stream the client
|
|
733
|
+
* reassembles (stream deltas, file changes, terminal output), and a silently
|
|
734
|
+
* missing one is a wrong screen. What used to fill the buffer — one huge
|
|
735
|
+
* reply — is now chunked and drained instead (backend/utils/ws-wire.ts).
|
|
814
736
|
*/
|
|
815
737
|
private canSend(conn: WSConnection): boolean {
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
// Check WebSocket ready state
|
|
819
|
-
if (conn.readyState !== 1) return false;
|
|
820
|
-
|
|
821
|
-
// Check buffer size if available (Bun/uWebSockets)
|
|
822
|
-
const rawWs = conn as any;
|
|
823
|
-
if (typeof rawWs.getBufferedAmount === 'function') {
|
|
824
|
-
const buffered = rawWs.getBufferedAmount();
|
|
825
|
-
if (buffered > CONFIG.MAX_BUFFER_SIZE) {
|
|
826
|
-
if (CONFIG.LOG_DROPPED_FRAMES) {
|
|
827
|
-
this.metrics.droppedFrames++;
|
|
828
|
-
const id = this.resolveId(conn) || 'unknown';
|
|
829
|
-
debug.warn('websocket', `Backpressure: dropping frame for ${id} (buffer: ${buffered})`);
|
|
830
|
-
}
|
|
831
|
-
return false;
|
|
832
|
-
}
|
|
833
|
-
}
|
|
834
|
-
|
|
835
|
-
return true;
|
|
738
|
+
return conn.readyState === 1;
|
|
836
739
|
}
|
|
837
740
|
|
|
838
741
|
/**
|
|
839
|
-
* Send
|
|
742
|
+
* Send a serialized event to a single connection.
|
|
840
743
|
*/
|
|
841
744
|
private sendToConnection(conn: WSConnection, message: string | ArrayBuffer): boolean {
|
|
842
745
|
if (!this.canSend(conn)) {
|
|
@@ -844,11 +747,10 @@ class WSServer {
|
|
|
844
747
|
}
|
|
845
748
|
|
|
846
749
|
try {
|
|
847
|
-
// For binary data (ArrayBuffer), wrap in Buffer for Bun/Elysia compatibility
|
|
848
750
|
if (message instanceof ArrayBuffer) {
|
|
849
|
-
conn
|
|
751
|
+
sendBinary(conn, 'binary-event', message);
|
|
850
752
|
} else {
|
|
851
|
-
conn
|
|
753
|
+
sendText(conn, actionOf(message), message);
|
|
852
754
|
}
|
|
853
755
|
return true;
|
|
854
756
|
} catch (err) {
|
|
@@ -930,6 +832,27 @@ class WSServer {
|
|
|
930
832
|
debug.log('websocket', `Emitted ${eventStr}${hasBinary ? ' (binary)' : ''} to ${sentCount}/${room.size} connections in project ${projectId}`);
|
|
931
833
|
},
|
|
932
834
|
|
|
835
|
+
/**
|
|
836
|
+
* Emit to the connections of a project room that pass `include` — for a
|
|
837
|
+
* stream only some of the room is looking at (a panel's live detail).
|
|
838
|
+
*/
|
|
839
|
+
projectWhere: <K extends keyof WSAPI['server']>(
|
|
840
|
+
projectId: string,
|
|
841
|
+
include: (connectionId: string) => boolean,
|
|
842
|
+
event: K,
|
|
843
|
+
payload: WSAPI['server'][K]
|
|
844
|
+
): void => {
|
|
845
|
+
this.metrics.totalEmits++;
|
|
846
|
+
const room = this.projectRooms.get(projectId);
|
|
847
|
+
if (!room || room.size === 0) return;
|
|
848
|
+
let message: string | null = null;
|
|
849
|
+
for (const [connectionId, wsConn] of room) {
|
|
850
|
+
if (!include(connectionId)) continue;
|
|
851
|
+
message ??= JSON.stringify({ action: event, payload });
|
|
852
|
+
this.sendToConnection(wsConn, message);
|
|
853
|
+
}
|
|
854
|
+
},
|
|
855
|
+
|
|
933
856
|
/**
|
|
934
857
|
* Emit event to all connections of a specific user
|
|
935
858
|
*/
|
|
@@ -976,26 +899,17 @@ class WSServer {
|
|
|
976
899
|
},
|
|
977
900
|
|
|
978
901
|
/**
|
|
979
|
-
* Emit event to
|
|
980
|
-
* Unlike emit.project()
|
|
981
|
-
*
|
|
982
|
-
*
|
|
983
|
-
* Used for cross-project notifications (e.g., stream finished).
|
|
902
|
+
* Emit event to every signed-in connection, whichever project it is
|
|
903
|
+
* viewing. Unlike emit.project() this reaches devices that switched to
|
|
904
|
+
* another project, so cross-project notifications (e.g. stream finished)
|
|
905
|
+
* still arrive.
|
|
984
906
|
*/
|
|
985
|
-
|
|
986
|
-
projectId: string,
|
|
907
|
+
signedIn: <K extends keyof WSAPI['server']>(
|
|
987
908
|
event: K,
|
|
988
909
|
payload: WSAPI['server'][K]
|
|
989
910
|
): void => {
|
|
990
911
|
this.metrics.totalEmits++;
|
|
991
912
|
|
|
992
|
-
const memberIds = this.projectMembers.get(projectId);
|
|
993
|
-
if (!memberIds || memberIds.size === 0) {
|
|
994
|
-
debug.warn('websocket', `No members for project: ${projectId} for event: ${String(event)}`);
|
|
995
|
-
return;
|
|
996
|
-
}
|
|
997
|
-
|
|
998
|
-
// Check if payload contains binary data - use binary encoding if so
|
|
999
913
|
const eventStr = String(event);
|
|
1000
914
|
const hasBinary = isBinaryAction(eventStr, payload);
|
|
1001
915
|
const message = hasBinary
|
|
@@ -1005,12 +919,7 @@ class WSServer {
|
|
|
1005
919
|
let sentCount = 0;
|
|
1006
920
|
let totalConns = 0;
|
|
1007
921
|
|
|
1008
|
-
|
|
1009
|
-
for (const userId of memberIds) {
|
|
1010
|
-
const userConns = this.userConnections.get(userId);
|
|
1011
|
-
if (!userConns || userConns.size === 0) continue;
|
|
1012
|
-
|
|
1013
|
-
// Clean stale connections
|
|
922
|
+
for (const [userId, userConns] of this.userConnections) {
|
|
1014
923
|
this.cleanStaleFromRoom(userConns);
|
|
1015
924
|
if (userConns.size === 0) {
|
|
1016
925
|
this.userConnections.delete(userId);
|
|
@@ -1025,7 +934,7 @@ class WSServer {
|
|
|
1025
934
|
}
|
|
1026
935
|
}
|
|
1027
936
|
|
|
1028
|
-
debug.log('websocket', `Emitted ${eventStr}${hasBinary ? ' (binary)' : ''} to ${sentCount}/${totalConns}
|
|
937
|
+
debug.log('websocket', `Emitted ${eventStr}${hasBinary ? ' (binary)' : ''} to ${sentCount}/${totalConns} signed-in connections`);
|
|
1029
938
|
},
|
|
1030
939
|
|
|
1031
940
|
/**
|
|
@@ -397,30 +397,17 @@ export function resolveEngineForBranch(row: WorktreeBranchRow): BranchEngine | n
|
|
|
397
397
|
|
|
398
398
|
/* ------------------------------------------------------------------ sources */
|
|
399
399
|
|
|
400
|
-
/** Who is asking, so the list only offers what they may actually use. */
|
|
401
|
-
export interface BranchPrincipal {
|
|
402
|
-
userId: string;
|
|
403
|
-
isAdmin: boolean;
|
|
404
|
-
}
|
|
405
|
-
|
|
406
400
|
/**
|
|
407
401
|
* Everywhere a worktree database could come from.
|
|
408
402
|
*
|
|
409
403
|
* Accounts and connections in ONE list, ordered native-first, because the user
|
|
410
404
|
* is choosing which database and not which mechanism. The mechanism is carried
|
|
411
405
|
* on each row as `isNative` and `notice` so the dialog can say what it costs.
|
|
412
|
-
*
|
|
413
|
-
* SCOPED TO THE CALLER, and the two halves scope differently. An integration
|
|
414
|
-
* account belongs to the INSTALL and spends its quota, so it is admin-only —
|
|
415
|
-
* the same rule `db-client:link` follows. A DB Client connection belongs to
|
|
416
|
-
* whoever saved it, so a member sees their own and an admin sees every one;
|
|
417
|
-
* anything else would let a member copy a database from a credential they
|
|
418
|
-
* cannot otherwise reach.
|
|
419
406
|
*/
|
|
420
|
-
export function listBranchSources(
|
|
407
|
+
export function listBranchSources(): BranchSourceOption[] {
|
|
421
408
|
const sources: BranchSourceOption[] = [];
|
|
422
409
|
|
|
423
|
-
for (const adapter of
|
|
410
|
+
for (const adapter of listBranchProviders()) {
|
|
424
411
|
for (const account of integrationAccountQueries.getByProvider(adapter.provider)) {
|
|
425
412
|
if (account.is_enabled !== 1) continue;
|
|
426
413
|
if (!integrationAccountQueries.capabilitiesOf(account).includes('worktree-branching')) continue;
|
|
@@ -438,7 +425,7 @@ export function listBranchSources(principal: BranchPrincipal): BranchSourceOptio
|
|
|
438
425
|
}
|
|
439
426
|
}
|
|
440
427
|
|
|
441
|
-
for (const connection of dbClientConnectionQueries.
|
|
428
|
+
for (const connection of dbClientConnectionQueries.list()) {
|
|
442
429
|
if (!canBranchFrom(connection)) continue;
|
|
443
430
|
// A connection that a branch already projected is not a parent: copying a
|
|
444
431
|
// worktree's own copy would nest one disposable database inside another.
|
|
@@ -51,6 +51,6 @@ export { registerBranchProvider, getBranchProvider, listBranchProviders } from '
|
|
|
51
51
|
export { worktreeBranchProjector } from './projector';
|
|
52
52
|
export { branchDatabaseNameFor, branchNameFor, isClopenBranchName, isClopenDatabaseName, slugifyForBranch } from './naming';
|
|
53
53
|
export { listBranchSources, resolveEngine, resolveEngineForBranch } from './engine';
|
|
54
|
-
export type { BranchEngine
|
|
54
|
+
export type { BranchEngine } from './engine';
|
|
55
55
|
export { suggestBranchSource } from './suggest';
|
|
56
56
|
export type { BranchProviderAdapter, BranchProviderContext } from './types';
|
|
@@ -53,7 +53,7 @@ function connectionNameFor(row: WorktreeBranchRow, noun: string): string {
|
|
|
53
53
|
* ACTIVE and still attached to a worktree. An orphan is deliberately excluded:
|
|
54
54
|
* its remote database may well still exist — that is what makes it an orphan —
|
|
55
55
|
* but the worktree it belonged to is gone, so leaving a connection behind would
|
|
56
|
-
* clutter
|
|
56
|
+
* clutter the connection list with rows pointing at databases nobody is using.
|
|
57
57
|
* The orphan list is where those live, and it offers a delete rather than a
|
|
58
58
|
* connection.
|
|
59
59
|
*/
|
|
@@ -56,8 +56,7 @@ import {
|
|
|
56
56
|
releaseConnectionBranch,
|
|
57
57
|
resolveEngine,
|
|
58
58
|
resolveEngineForBranch,
|
|
59
|
-
type BranchEngine
|
|
60
|
-
type BranchPrincipal
|
|
59
|
+
type BranchEngine
|
|
61
60
|
} from './engine';
|
|
62
61
|
import { suggestBranchSource, type BranchSuggestion } from './suggest';
|
|
63
62
|
import { debug } from '$shared/utils/logger';
|
|
@@ -67,23 +66,16 @@ function messageOf(error: unknown): string {
|
|
|
67
66
|
}
|
|
68
67
|
|
|
69
68
|
/**
|
|
70
|
-
* The source, if
|
|
71
|
-
*
|
|
72
|
-
*
|
|
73
|
-
* already answers "what may they use", so asking it rather than re-deriving the
|
|
74
|
-
* rule is what stops the two from drifting — and an id that is not in the list
|
|
75
|
-
* is refused with the same sentence whether it is someone else's connection or
|
|
76
|
-
* an account a member cannot reach.
|
|
69
|
+
* The source, if it can be branched from. `listBranchSources` already answers
|
|
70
|
+
* which sources qualify, so asking it rather than re-deriving the rule is what
|
|
71
|
+
* stops the two from drifting.
|
|
77
72
|
*/
|
|
78
|
-
function requireSource(
|
|
79
|
-
|
|
80
|
-
principal: BranchPrincipal
|
|
81
|
-
): BranchSourceOption {
|
|
82
|
-
const source = listBranchSources(principal).find(
|
|
73
|
+
function requireSource(input: { sourceKind: BranchSourceKind; sourceId: string }): BranchSourceOption {
|
|
74
|
+
const source = listBranchSources().find(
|
|
83
75
|
(entry) => entry.kind === input.sourceKind && entry.id === input.sourceId
|
|
84
76
|
);
|
|
85
77
|
if (!source) {
|
|
86
|
-
throw new Error('That database source is not available
|
|
78
|
+
throw new Error('That database source is not available, so nothing was saved.');
|
|
87
79
|
}
|
|
88
80
|
return source;
|
|
89
81
|
}
|
|
@@ -306,7 +298,7 @@ export const worktreeBranching = {
|
|
|
306
298
|
* project with both has one right answer and picking the other produces a
|
|
307
299
|
* database nothing reads.
|
|
308
300
|
*/
|
|
309
|
-
async state(projectId: string
|
|
301
|
+
async state(projectId: string): Promise<WorktreeBranchingState> {
|
|
310
302
|
const project = projectQueries.getById(projectId);
|
|
311
303
|
const binding = worktreeBranchBindingQueries.get(projectId);
|
|
312
304
|
const resolved = resolveEngine(binding);
|
|
@@ -314,7 +306,7 @@ export const worktreeBranching = {
|
|
|
314
306
|
|
|
315
307
|
return {
|
|
316
308
|
providers: listBranchProviders().map((adapter) => adapter.info()),
|
|
317
|
-
sources: listBranchSources(
|
|
309
|
+
sources: listBranchSources(),
|
|
318
310
|
binding:
|
|
319
311
|
binding && engine
|
|
320
312
|
? {
|
|
@@ -355,11 +347,8 @@ export const worktreeBranching = {
|
|
|
355
347
|
* Two shapes behind one route: a provider's own list of projects, or the
|
|
356
348
|
* databases on a server Clopen already has a connection to.
|
|
357
349
|
*/
|
|
358
|
-
async parents(
|
|
359
|
-
input
|
|
360
|
-
principal: BranchPrincipal
|
|
361
|
-
): Promise<BranchParent[]> {
|
|
362
|
-
requireSource(input, principal);
|
|
350
|
+
async parents(input: { sourceKind: BranchSourceKind; sourceId: string }): Promise<BranchParent[]> {
|
|
351
|
+
requireSource(input);
|
|
363
352
|
if (input.sourceKind === 'connection') {
|
|
364
353
|
const parents = await connectionParents(input.sourceId);
|
|
365
354
|
return parents.map((parent) => ({
|
|
@@ -396,10 +385,9 @@ export const worktreeBranching = {
|
|
|
396
385
|
parentRef: string;
|
|
397
386
|
parentName: string;
|
|
398
387
|
config: Partial<WorktreeBranchConfig>;
|
|
399
|
-
}
|
|
400
|
-
principal: BranchPrincipal
|
|
388
|
+
}
|
|
401
389
|
): WorktreeBranchBindingInfo {
|
|
402
|
-
const source = requireSource(input
|
|
390
|
+
const source = requireSource(input);
|
|
403
391
|
|
|
404
392
|
const config: WorktreeBranchConfig = {
|
|
405
393
|
...DEFAULT_WORKTREE_BRANCH_CONFIG,
|
package/backend/ws/access.ts
CHANGED
|
@@ -4,18 +4,15 @@ import { ws } from '$backend/utils/ws';
|
|
|
4
4
|
import { projectQueries, sessionQueries, messageQueries } from '$backend/database/queries';
|
|
5
5
|
import { resolveWorktreeRoot } from '$backend/worktrees';
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
}
|
|
13
|
-
|
|
7
|
+
/**
|
|
8
|
+
* A project in the workspace list. A removed project keeps its record (so its
|
|
9
|
+
* chats can be restored) but is not reachable until it is added again.
|
|
10
|
+
*/
|
|
11
|
+
export function requireProjectAccess(projectId: string): Project {
|
|
14
12
|
const project = projectQueries.getById(projectId);
|
|
15
|
-
if (!project) {
|
|
13
|
+
if (!project || !projectQueries.isListed(projectId)) {
|
|
16
14
|
throw new Error('Access denied');
|
|
17
15
|
}
|
|
18
|
-
|
|
19
16
|
return project;
|
|
20
17
|
}
|
|
21
18
|
|
|
@@ -31,7 +28,7 @@ export function requireProjectWorkspace(conn: WSConnection, projectId: string):
|
|
|
31
28
|
root: string;
|
|
32
29
|
worktreeId: string | null;
|
|
33
30
|
} {
|
|
34
|
-
const project = requireProjectAccess(
|
|
31
|
+
const project = requireProjectAccess(projectId);
|
|
35
32
|
const resolved = resolveWorktreeRoot(projectId, ws.getWorktreeId(conn));
|
|
36
33
|
return { project, root: resolved.path || project.path, worktreeId: resolved.worktreeId };
|
|
37
34
|
}
|
|
@@ -43,32 +40,26 @@ export function requireCurrentProjectAccess(conn: WSConnection): {
|
|
|
43
40
|
} {
|
|
44
41
|
const userId = ws.getUserId(conn);
|
|
45
42
|
const projectId = ws.getProjectId(conn);
|
|
46
|
-
const project = requireProjectAccess(
|
|
43
|
+
const project = requireProjectAccess(projectId);
|
|
47
44
|
return { userId, projectId, project };
|
|
48
45
|
}
|
|
49
46
|
|
|
50
|
-
export function requireSessionAccess(
|
|
47
|
+
export function requireSessionAccess(sessionId: string): ChatSession {
|
|
51
48
|
const session = sessionQueries.getById(sessionId);
|
|
52
49
|
if (!session) {
|
|
53
50
|
throw new Error('Access denied');
|
|
54
51
|
}
|
|
55
|
-
|
|
56
|
-
const userId = ws.getUserId(conn);
|
|
57
|
-
const hasAccess = projectQueries.userHasProject(userId, session.project_id);
|
|
58
|
-
if (!hasAccess) {
|
|
59
|
-
throw new Error('Access denied');
|
|
60
|
-
}
|
|
61
|
-
|
|
52
|
+
requireProjectAccess(session.project_id);
|
|
62
53
|
return session;
|
|
63
54
|
}
|
|
64
55
|
|
|
65
|
-
export function requireMessageAccess(
|
|
56
|
+
export function requireMessageAccess(messageId: string): DatabaseMessage {
|
|
66
57
|
const message = messageQueries.getById(messageId);
|
|
67
58
|
if (!message) {
|
|
68
59
|
throw new Error('Access denied');
|
|
69
60
|
}
|
|
70
61
|
|
|
71
62
|
// Message access inherits session access controls.
|
|
72
|
-
requireSessionAccess(
|
|
63
|
+
requireSessionAccess(message.session_id);
|
|
73
64
|
return message;
|
|
74
65
|
}
|