@thevinci/web 1.0.0
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 +197 -0
- package/bin/cli-entry.js +55 -0
- package/bin/cli-output.js +145 -0
- package/bin/cli.js +4887 -0
- package/bin/cli.test.js +64 -0
- package/dist/apple-touch-icon-120x120.png +0 -0
- package/dist/apple-touch-icon-152x152.png +0 -0
- package/dist/apple-touch-icon-167x167.png +0 -0
- package/dist/apple-touch-icon-180x180.png +0 -0
- package/dist/apple-touch-icon.png +0 -0
- package/dist/apple-touch-icon.svg +528 -0
- package/dist/assets/JsonTreeView-CSm9OzXG.js +1 -0
- package/dist/assets/KaTeX_AMS-Regular-BQhdFMY1.woff2 +0 -0
- package/dist/assets/KaTeX_AMS-Regular-DMm9YOAa.woff +0 -0
- package/dist/assets/KaTeX_AMS-Regular-DRggAlZN.ttf +0 -0
- package/dist/assets/KaTeX_Caligraphic-Bold-ATXxdsX0.ttf +0 -0
- package/dist/assets/KaTeX_Caligraphic-Bold-BEiXGLvX.woff +0 -0
- package/dist/assets/KaTeX_Caligraphic-Bold-Dq_IR9rO.woff2 +0 -0
- package/dist/assets/KaTeX_Caligraphic-Regular-CTRA-rTL.woff +0 -0
- package/dist/assets/KaTeX_Caligraphic-Regular-Di6jR-x-.woff2 +0 -0
- package/dist/assets/KaTeX_Caligraphic-Regular-wX97UBjC.ttf +0 -0
- package/dist/assets/KaTeX_Fraktur-Bold-BdnERNNW.ttf +0 -0
- package/dist/assets/KaTeX_Fraktur-Bold-BsDP51OF.woff +0 -0
- package/dist/assets/KaTeX_Fraktur-Bold-CL6g_b3V.woff2 +0 -0
- package/dist/assets/KaTeX_Fraktur-Regular-CB_wures.ttf +0 -0
- package/dist/assets/KaTeX_Fraktur-Regular-CTYiF6lA.woff2 +0 -0
- package/dist/assets/KaTeX_Fraktur-Regular-Dxdc4cR9.woff +0 -0
- package/dist/assets/KaTeX_Main-Bold-Cx986IdX.woff2 +0 -0
- package/dist/assets/KaTeX_Main-Bold-Jm3AIy58.woff +0 -0
- package/dist/assets/KaTeX_Main-Bold-waoOVXN0.ttf +0 -0
- package/dist/assets/KaTeX_Main-BoldItalic-DxDJ3AOS.woff2 +0 -0
- package/dist/assets/KaTeX_Main-BoldItalic-DzxPMmG6.ttf +0 -0
- package/dist/assets/KaTeX_Main-BoldItalic-SpSLRI95.woff +0 -0
- package/dist/assets/KaTeX_Main-Italic-3WenGoN9.ttf +0 -0
- package/dist/assets/KaTeX_Main-Italic-BMLOBm91.woff +0 -0
- package/dist/assets/KaTeX_Main-Italic-NWA7e6Wa.woff2 +0 -0
- package/dist/assets/KaTeX_Main-Regular-B22Nviop.woff2 +0 -0
- package/dist/assets/KaTeX_Main-Regular-Dr94JaBh.woff +0 -0
- package/dist/assets/KaTeX_Main-Regular-ypZvNtVU.ttf +0 -0
- package/dist/assets/KaTeX_Math-BoldItalic-B3XSjfu4.ttf +0 -0
- package/dist/assets/KaTeX_Math-BoldItalic-CZnvNsCZ.woff2 +0 -0
- package/dist/assets/KaTeX_Math-BoldItalic-iY-2wyZ7.woff +0 -0
- package/dist/assets/KaTeX_Math-Italic-DA0__PXp.woff +0 -0
- package/dist/assets/KaTeX_Math-Italic-flOr_0UB.ttf +0 -0
- package/dist/assets/KaTeX_Math-Italic-t53AETM-.woff2 +0 -0
- package/dist/assets/KaTeX_SansSerif-Bold-CFMepnvq.ttf +0 -0
- package/dist/assets/KaTeX_SansSerif-Bold-D1sUS0GD.woff2 +0 -0
- package/dist/assets/KaTeX_SansSerif-Bold-DbIhKOiC.woff +0 -0
- package/dist/assets/KaTeX_SansSerif-Italic-C3H0VqGB.woff2 +0 -0
- package/dist/assets/KaTeX_SansSerif-Italic-DN2j7dab.woff +0 -0
- package/dist/assets/KaTeX_SansSerif-Italic-YYjJ1zSn.ttf +0 -0
- package/dist/assets/KaTeX_SansSerif-Regular-BNo7hRIc.ttf +0 -0
- package/dist/assets/KaTeX_SansSerif-Regular-CS6fqUqJ.woff +0 -0
- package/dist/assets/KaTeX_SansSerif-Regular-DDBCnlJ7.woff2 +0 -0
- package/dist/assets/KaTeX_Script-Regular-C5JkGWo-.ttf +0 -0
- package/dist/assets/KaTeX_Script-Regular-D3wIWfF6.woff2 +0 -0
- package/dist/assets/KaTeX_Script-Regular-D5yQViql.woff +0 -0
- package/dist/assets/KaTeX_Size1-Regular-C195tn64.woff +0 -0
- package/dist/assets/KaTeX_Size1-Regular-Dbsnue_I.ttf +0 -0
- package/dist/assets/KaTeX_Size1-Regular-mCD8mA8B.woff2 +0 -0
- package/dist/assets/KaTeX_Size2-Regular-B7gKUWhC.ttf +0 -0
- package/dist/assets/KaTeX_Size2-Regular-Dy4dx90m.woff2 +0 -0
- package/dist/assets/KaTeX_Size2-Regular-oD1tc_U0.woff +0 -0
- package/dist/assets/KaTeX_Size3-Regular-CTq5MqoE.woff +0 -0
- package/dist/assets/KaTeX_Size3-Regular-DgpXs0kz.ttf +0 -0
- package/dist/assets/KaTeX_Size4-Regular-BF-4gkZK.woff +0 -0
- package/dist/assets/KaTeX_Size4-Regular-DWFBv043.ttf +0 -0
- package/dist/assets/KaTeX_Size4-Regular-Dl5lxZxV.woff2 +0 -0
- package/dist/assets/KaTeX_Typewriter-Regular-C0xS9mPB.woff +0 -0
- package/dist/assets/KaTeX_Typewriter-Regular-CO6r4hn1.woff2 +0 -0
- package/dist/assets/KaTeX_Typewriter-Regular-D3Ib7_Hf.ttf +0 -0
- package/dist/assets/MarkdownRendererImpl-DensKOLc.js +6 -0
- package/dist/assets/MultiRunWindow-Bo7THayo.js +1 -0
- package/dist/assets/OnboardingScreen-BDqmzTVR.js +2 -0
- package/dist/assets/SettingsWindow-coz__Ykw.js +1 -0
- package/dist/assets/TerminalView-DrZ-i3Dr.js +1 -0
- package/dist/assets/ToolOutputDialog-Eglzslt3.js +16 -0
- package/dist/assets/es-4o9ciP61.js +15 -0
- package/dist/assets/ibm-plex-mono-latin-400-normal-CvHOgSBP.woff +0 -0
- package/dist/assets/ibm-plex-mono-latin-400-normal-DMJ8VG8y.woff2 +0 -0
- package/dist/assets/ibm-plex-mono-latin-500-normal-CB9ihrfo.woff +0 -0
- package/dist/assets/ibm-plex-mono-latin-500-normal-DSY6xOcd.woff2 +0 -0
- package/dist/assets/ibm-plex-mono-latin-600-normal-BgSNZQsw.woff2 +0 -0
- package/dist/assets/ibm-plex-mono-latin-600-normal-DWFSQ4vo.woff +0 -0
- package/dist/assets/ibm-plex-sans-latin-400-normal-CDDApCn2.woff2 +0 -0
- package/dist/assets/ibm-plex-sans-latin-400-normal-CYLoc0-x.woff +0 -0
- package/dist/assets/ibm-plex-sans-latin-500-normal-6ng42L7E.woff2 +0 -0
- package/dist/assets/ibm-plex-sans-latin-500-normal-BgVn5rGT.woff +0 -0
- package/dist/assets/ibm-plex-sans-latin-600-normal-Cu4Hd6ag.woff +0 -0
- package/dist/assets/ibm-plex-sans-latin-600-normal-CuJfVYMP.woff2 +0 -0
- package/dist/assets/index-DLTDToSP.css +1 -0
- package/dist/assets/index-DgiFEKGN.js +1 -0
- package/dist/assets/ko-B20imCHE.js +15 -0
- package/dist/assets/main-BV3KOtdA.css +1 -0
- package/dist/assets/main-CDKJj0sH.js +226 -0
- package/dist/assets/main-LC-PSNVM.js +2 -0
- package/dist/assets/miniChat-CQUiG_cr.js +2 -0
- package/dist/assets/modelPrefsAutoSave-Dm799vzR.js +6986 -0
- package/dist/assets/pl-DQJ7LSzj.js +15 -0
- package/dist/assets/pt-BR-OmjHUz9y.js +15 -0
- package/dist/assets/renderElectronMiniChatApp-CARbeW0G.js +2 -0
- package/dist/assets/uk-BNFxOlO4.js +15 -0
- package/dist/assets/vendor--DBfsbEis.css +1 -0
- package/dist/assets/vendor-.bun-B9l0ZNi2.js +4094 -0
- package/dist/assets/wasm-CG6Dc4jp.js +1 -0
- package/dist/assets/wasmSttWorker-Dtlxac_K.js +1 -0
- package/dist/assets/wasmSttWorker-oo7Dm_jy.js +1806 -0
- package/dist/assets/worker-CbT6TVo7.js +155 -0
- package/dist/assets/zh-CN-C6T-Ac7F.js +15 -0
- package/dist/favicon-16.png +0 -0
- package/dist/favicon-32.png +0 -0
- package/dist/favicon.png +0 -0
- package/dist/favicon.svg +528 -0
- package/dist/index.html +607 -0
- package/dist/logo-dark-192x192.png +0 -0
- package/dist/logo-dark-512x512.png +0 -0
- package/dist/logo-dark-512x512.svg +528 -0
- package/dist/logo-light-192x192.png +0 -0
- package/dist/logo-light-512x512.png +0 -0
- package/dist/logo-light-512x512.svg +528 -0
- package/dist/mini-chat.html +16 -0
- package/dist/pwa-192.png +0 -0
- package/dist/pwa-512.png +0 -0
- package/dist/pwa-maskable-192.png +0 -0
- package/dist/pwa-maskable-512.png +0 -0
- package/dist/site.webmanifest +21 -0
- package/dist/sw.js +1 -0
- package/package.json +118 -0
- package/public/apple-touch-icon-120x120.png +0 -0
- package/public/apple-touch-icon-152x152.png +0 -0
- package/public/apple-touch-icon-167x167.png +0 -0
- package/public/apple-touch-icon-180x180.png +0 -0
- package/public/apple-touch-icon.png +0 -0
- package/public/apple-touch-icon.svg +528 -0
- package/public/favicon-16.png +0 -0
- package/public/favicon-32.png +0 -0
- package/public/favicon.png +0 -0
- package/public/favicon.svg +528 -0
- package/public/logo-dark-192x192.png +0 -0
- package/public/logo-dark-512x512.png +0 -0
- package/public/logo-dark-512x512.svg +528 -0
- package/public/logo-light-192x192.png +0 -0
- package/public/logo-light-512x512.png +0 -0
- package/public/logo-light-512x512.svg +528 -0
- package/public/pwa-192.png +0 -0
- package/public/pwa-512.png +0 -0
- package/public/pwa-maskable-192.png +0 -0
- package/public/pwa-maskable-512.png +0 -0
- package/public/site.webmanifest +21 -0
- package/server/TERMINAL_WS_PROTOCOL.md +48 -0
- package/server/index.d.ts +39 -0
- package/server/index.js +1311 -0
- package/server/lib/cloudflare-tunnel.js +650 -0
- package/server/lib/event-stream/DOCUMENTATION.md +61 -0
- package/server/lib/event-stream/directory-ws-bridge.js +185 -0
- package/server/lib/event-stream/global-hub.js +158 -0
- package/server/lib/event-stream/global-hub.test.js +140 -0
- package/server/lib/event-stream/global-ws-bridge.js +206 -0
- package/server/lib/event-stream/index.js +25 -0
- package/server/lib/event-stream/protocol.js +131 -0
- package/server/lib/event-stream/protocol.test.js +182 -0
- package/server/lib/event-stream/runtime.js +180 -0
- package/server/lib/event-stream/runtime.test.js +512 -0
- package/server/lib/event-stream/upstream-reader.js +226 -0
- package/server/lib/event-stream/upstream-reader.test.js +276 -0
- package/server/lib/fs/DOCUMENTATION.md +36 -0
- package/server/lib/fs/routes.js +1040 -0
- package/server/lib/fs/search.js +238 -0
- package/server/lib/git/DOCUMENTATION.md +152 -0
- package/server/lib/git/credentials.js +74 -0
- package/server/lib/git/identity-storage.js +112 -0
- package/server/lib/git/index.js +6 -0
- package/server/lib/git/routes.js +972 -0
- package/server/lib/git/service.js +3432 -0
- package/server/lib/git/service.test.js +39 -0
- package/server/lib/github/DOCUMENTATION.md +171 -0
- package/server/lib/github/auth.js +307 -0
- package/server/lib/github/device-flow.js +50 -0
- package/server/lib/github/index.js +24 -0
- package/server/lib/github/octokit.js +10 -0
- package/server/lib/github/pr-status.js +519 -0
- package/server/lib/github/repo/fork-detection.js +102 -0
- package/server/lib/github/repo/index.js +55 -0
- package/server/lib/github/routes.js +1560 -0
- package/server/lib/magic-prompts/routes.js +63 -0
- package/server/lib/magic-prompts/runtime.js +119 -0
- package/server/lib/notifications/DOCUMENTATION.md +122 -0
- package/server/lib/notifications/emitter-runtime.js +102 -0
- package/server/lib/notifications/index.js +4 -0
- package/server/lib/notifications/message.js +52 -0
- package/server/lib/notifications/message.test.js +34 -0
- package/server/lib/notifications/push-runtime.js +304 -0
- package/server/lib/notifications/routes.js +315 -0
- package/server/lib/notifications/runtime.js +566 -0
- package/server/lib/notifications/template-runtime.js +349 -0
- package/server/lib/notifications/template-runtime.test.js +26 -0
- package/server/lib/opencode/DOCUMENTATION.md +362 -0
- package/server/lib/opencode/agents.js +634 -0
- package/server/lib/opencode/auth-state-runtime.js +88 -0
- package/server/lib/opencode/auth.js +83 -0
- package/server/lib/opencode/bootstrap-runtime.js +131 -0
- package/server/lib/opencode/cli-entry-runtime.js +43 -0
- package/server/lib/opencode/cli-options.js +128 -0
- package/server/lib/opencode/commands.js +339 -0
- package/server/lib/opencode/config-entity-routes.js +370 -0
- package/server/lib/opencode/core-routes.js +500 -0
- package/server/lib/opencode/core-routes.test.js +26 -0
- package/server/lib/opencode/env-config.js +74 -0
- package/server/lib/opencode/env-keys.js +68 -0
- package/server/lib/opencode/env-runtime.js +1162 -0
- package/server/lib/opencode/env-runtime.test.js +116 -0
- package/server/lib/opencode/feature-routes-runtime.js +244 -0
- package/server/lib/opencode/hmr-state-runtime.js +85 -0
- package/server/lib/opencode/index.js +66 -0
- package/server/lib/opencode/lifecycle.js +1019 -0
- package/server/lib/opencode/lifecycle.test.js +240 -0
- package/server/lib/opencode/mcp.js +278 -0
- package/server/lib/opencode/network-runtime.js +104 -0
- package/server/lib/opencode/network-runtime.test.js +37 -0
- package/server/lib/opencode/opencode-resolution-runtime.js +71 -0
- package/server/lib/opencode/path-utils.js +100 -0
- package/server/lib/opencode/path-utils.test.js +71 -0
- package/server/lib/opencode/project-directory-runtime.js +124 -0
- package/server/lib/opencode/project-icon-routes.js +399 -0
- package/server/lib/opencode/project-icon-routes.test.js +107 -0
- package/server/lib/opencode/providers.js +96 -0
- package/server/lib/opencode/proxy.js +445 -0
- package/server/lib/opencode/pwa-manifest-routes.js +257 -0
- package/server/lib/opencode/pwa-manifest-routes.test.js +133 -0
- package/server/lib/opencode/routes.js +541 -0
- package/server/lib/opencode/server-startup-runtime.js +156 -0
- package/server/lib/opencode/server-utils-runtime.js +168 -0
- package/server/lib/opencode/server-utils-runtime.test.js +135 -0
- package/server/lib/opencode/session-runtime.js +356 -0
- package/server/lib/opencode/session-runtime.test.js +151 -0
- package/server/lib/opencode/settings-helpers.js +770 -0
- package/server/lib/opencode/settings-helpers.test.js +109 -0
- package/server/lib/opencode/settings-normalization-runtime.js +428 -0
- package/server/lib/opencode/settings-runtime.js +826 -0
- package/server/lib/opencode/settings-runtime.test.js +85 -0
- package/server/lib/opencode/shared.js +615 -0
- package/server/lib/opencode/shutdown-runtime.js +139 -0
- package/server/lib/opencode/shutdown-runtime.test.js +58 -0
- package/server/lib/opencode/skill-routes.js +701 -0
- package/server/lib/opencode/skills.js +548 -0
- package/server/lib/opencode/startup-pipeline-runtime.js +130 -0
- package/server/lib/opencode/static-routes-runtime.js +65 -0
- package/server/lib/opencode/theme-runtime.js +167 -0
- package/server/lib/opencode/tunnel-auth.js +591 -0
- package/server/lib/opencode/tunnel-wiring-runtime.js +94 -0
- package/server/lib/opencode/vinci-routes.js +76 -0
- package/server/lib/opencode/watcher.js +115 -0
- package/server/lib/opencode/watcher.test.js +239 -0
- package/server/lib/preview/proxy-runtime.js +1333 -0
- package/server/lib/preview/proxy-runtime.test.js +144 -0
- package/server/lib/projects/project-config.js +567 -0
- package/server/lib/projects/project-config.test.js +175 -0
- package/server/lib/projects/project-id.js +13 -0
- package/server/lib/quota/DOCUMENTATION.md +58 -0
- package/server/lib/quota/index.js +25 -0
- package/server/lib/quota/providers/claude.js +107 -0
- package/server/lib/quota/providers/codex.js +113 -0
- package/server/lib/quota/providers/copilot.js +165 -0
- package/server/lib/quota/providers/google/api.js +92 -0
- package/server/lib/quota/providers/google/auth.js +108 -0
- package/server/lib/quota/providers/google/index.js +124 -0
- package/server/lib/quota/providers/google/transforms.js +109 -0
- package/server/lib/quota/providers/index.js +168 -0
- package/server/lib/quota/providers/interface.js +55 -0
- package/server/lib/quota/providers/kimi.js +108 -0
- package/server/lib/quota/providers/minimax-cn-coding-plan.js +140 -0
- package/server/lib/quota/providers/minimax-coding-plan.js +139 -0
- package/server/lib/quota/providers/nanogpt.js +124 -0
- package/server/lib/quota/providers/ollama-cloud.js +112 -0
- package/server/lib/quota/providers/openai.js +91 -0
- package/server/lib/quota/providers/openrouter.js +92 -0
- package/server/lib/quota/providers/zai.js +91 -0
- package/server/lib/quota/providers/zhipuai-coding-plan.js +133 -0
- package/server/lib/quota/providers/zhipuai.js +114 -0
- package/server/lib/quota/routes.js +27 -0
- package/server/lib/quota/utils/auth.js +50 -0
- package/server/lib/quota/utils/formatters.js +85 -0
- package/server/lib/quota/utils/formatters.test.js +54 -0
- package/server/lib/quota/utils/index.js +10 -0
- package/server/lib/quota/utils/transformers.js +55 -0
- package/server/lib/scheduled-tasks/DOCUMENTATION.md +44 -0
- package/server/lib/scheduled-tasks/routes.js +235 -0
- package/server/lib/scheduled-tasks/runtime.js +773 -0
- package/server/lib/scheduled-tasks/runtime.test.js +100 -0
- package/server/lib/security/request-security.js +115 -0
- package/server/lib/session-folders/routes.js +63 -0
- package/server/lib/session-folders/routes.test.js +102 -0
- package/server/lib/skills-catalog/DOCUMENTATION.md +178 -0
- package/server/lib/skills-catalog/cache.js +29 -0
- package/server/lib/skills-catalog/clawdhub/api.js +158 -0
- package/server/lib/skills-catalog/clawdhub/index.js +30 -0
- package/server/lib/skills-catalog/clawdhub/install.js +238 -0
- package/server/lib/skills-catalog/clawdhub/scan.js +113 -0
- package/server/lib/skills-catalog/curated-sources.js +21 -0
- package/server/lib/skills-catalog/git.js +77 -0
- package/server/lib/skills-catalog/index.js +42 -0
- package/server/lib/skills-catalog/install.js +294 -0
- package/server/lib/skills-catalog/scan.js +221 -0
- package/server/lib/skills-catalog/source.js +87 -0
- package/server/lib/terminal/DOCUMENTATION.md +76 -0
- package/server/lib/terminal/index.js +31 -0
- package/server/lib/terminal/output-replay-buffer.js +78 -0
- package/server/lib/terminal/output-replay-buffer.test.js +75 -0
- package/server/lib/terminal/runtime.js +850 -0
- package/server/lib/terminal/runtime.test.js +96 -0
- package/server/lib/terminal/terminal-ws-protocol.js +68 -0
- package/server/lib/terminal/terminal-ws-protocol.test.js +145 -0
- package/server/lib/text/DOCUMENTATION.md +35 -0
- package/server/lib/text/summarization.js +138 -0
- package/server/lib/text/summarization.test.js +34 -0
- package/server/lib/tts/DOCUMENTATION.md +146 -0
- package/server/lib/tts/base-url.js +62 -0
- package/server/lib/tts/capability-runtime.js +31 -0
- package/server/lib/tts/index.js +19 -0
- package/server/lib/tts/routes.js +261 -0
- package/server/lib/tts/routes.test.js +53 -0
- package/server/lib/tts/service.js +178 -0
- package/server/lib/tts/stt.js +75 -0
- package/server/lib/tunnels/DOCUMENTATION.md +18 -0
- package/server/lib/tunnels/index.js +166 -0
- package/server/lib/tunnels/managed-config.js +201 -0
- package/server/lib/tunnels/providers/cloudflare.js +260 -0
- package/server/lib/tunnels/registry.js +51 -0
- package/server/lib/tunnels/routes.js +605 -0
- package/server/lib/tunnels/types.js +219 -0
- package/server/lib/ui-auth/DOCUMENTATION.md +38 -0
- package/server/lib/ui-auth/ui-auth.js +673 -0
- package/server/lib/ui-auth/ui-passkeys.js +545 -0
- package/server/opencode-proxy.test.js +151 -0
- package/server/proxy-headers.js +61 -0
- package/server/proxy-headers.test.js +58 -0
- package/server/sse-routes.test.js +152 -0
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
import { sendMessageStreamWsEvent, sendMessageStreamWsFrame } from './protocol.js';
|
|
2
|
+
import { createUpstreamSseReader } from './upstream-reader.js';
|
|
3
|
+
|
|
4
|
+
function shouldTriggerUpstreamHealthCheck(upstream) {
|
|
5
|
+
if (!upstream) {
|
|
6
|
+
return true;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
if (!upstream.body) {
|
|
10
|
+
return upstream.ok || upstream.status >= 500;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
return upstream.status >= 500;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function acceptDirectoryMessageStreamWsConnection({
|
|
17
|
+
socket,
|
|
18
|
+
requestedLastEventId,
|
|
19
|
+
requestedDirectory,
|
|
20
|
+
buildOpenCodeUrl,
|
|
21
|
+
getOpenCodeAuthHeaders,
|
|
22
|
+
processForwardedEventPayload,
|
|
23
|
+
wsClients,
|
|
24
|
+
triggerHealthCheck,
|
|
25
|
+
heartbeatIntervalMs,
|
|
26
|
+
upstreamStallTimeoutMs,
|
|
27
|
+
upstreamReconnectDelayMs,
|
|
28
|
+
fetchImpl,
|
|
29
|
+
}) {
|
|
30
|
+
const controller = new AbortController();
|
|
31
|
+
let upstreamConnected = false;
|
|
32
|
+
let streamReady = false;
|
|
33
|
+
let reader = null;
|
|
34
|
+
|
|
35
|
+
const cleanup = () => {
|
|
36
|
+
if (!controller.signal.aborted) {
|
|
37
|
+
controller.abort();
|
|
38
|
+
}
|
|
39
|
+
reader?.stop();
|
|
40
|
+
wsClients.delete(socket);
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
const pingInterval = setInterval(() => {
|
|
44
|
+
if (socket.readyState !== 1) {
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
try {
|
|
49
|
+
socket.ping();
|
|
50
|
+
} catch {
|
|
51
|
+
}
|
|
52
|
+
}, heartbeatIntervalMs);
|
|
53
|
+
|
|
54
|
+
const heartbeatInterval = setInterval(() => {
|
|
55
|
+
if (!upstreamConnected) {
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
sendMessageStreamWsEvent(socket, { type: 'vinci:heartbeat', timestamp: Date.now() }, { directory: 'global' });
|
|
60
|
+
}, heartbeatIntervalMs);
|
|
61
|
+
|
|
62
|
+
socket.on('close', () => {
|
|
63
|
+
clearInterval(pingInterval);
|
|
64
|
+
clearInterval(heartbeatInterval);
|
|
65
|
+
upstreamConnected = false;
|
|
66
|
+
cleanup();
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
socket.on('error', () => {
|
|
70
|
+
void 0;
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
const run = async () => {
|
|
74
|
+
const forwardEvent = ({ envelope, payload }) => {
|
|
75
|
+
const directory = requestedDirectory || envelope?.directory || 'global';
|
|
76
|
+
|
|
77
|
+
sendMessageStreamWsEvent(socket, payload, {
|
|
78
|
+
directory,
|
|
79
|
+
eventId: typeof envelope?.eventId === 'string' && envelope.eventId.length > 0 ? envelope.eventId : undefined,
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
processForwardedEventPayload(payload, (syntheticPayload) => {
|
|
83
|
+
sendMessageStreamWsEvent(socket, syntheticPayload, { directory: 'global' });
|
|
84
|
+
});
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
try {
|
|
88
|
+
let buildUrlFailed = false;
|
|
89
|
+
const closeWithInitialError = ({ message, closeReason = message, triggerHealthCheckFor = null }) => {
|
|
90
|
+
sendMessageStreamWsFrame(socket, { type: 'error', message });
|
|
91
|
+
socket.close(1011, closeReason);
|
|
92
|
+
if (triggerHealthCheckFor === true || (triggerHealthCheckFor && shouldTriggerUpstreamHealthCheck(triggerHealthCheckFor))) {
|
|
93
|
+
triggerHealthCheck?.();
|
|
94
|
+
}
|
|
95
|
+
reader?.stop();
|
|
96
|
+
cleanup();
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
reader = createUpstreamSseReader({
|
|
100
|
+
initialLastEventId: requestedLastEventId,
|
|
101
|
+
signal: controller.signal,
|
|
102
|
+
stallTimeoutMs: upstreamStallTimeoutMs,
|
|
103
|
+
reconnectDelayMs: upstreamReconnectDelayMs,
|
|
104
|
+
fetchImpl,
|
|
105
|
+
buildUrl: () => {
|
|
106
|
+
buildUrlFailed = false;
|
|
107
|
+
let targetUrl;
|
|
108
|
+
try {
|
|
109
|
+
targetUrl = new URL(buildOpenCodeUrl('/event', ''));
|
|
110
|
+
} catch {
|
|
111
|
+
buildUrlFailed = true;
|
|
112
|
+
throw new Error('OpenCode service unavailable');
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
if (requestedDirectory) {
|
|
116
|
+
targetUrl.searchParams.set('directory', requestedDirectory);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
return targetUrl;
|
|
120
|
+
},
|
|
121
|
+
getHeaders: getOpenCodeAuthHeaders,
|
|
122
|
+
onConnect() {
|
|
123
|
+
if (!streamReady) {
|
|
124
|
+
sendMessageStreamWsFrame(socket, {
|
|
125
|
+
type: 'ready',
|
|
126
|
+
scope: 'directory',
|
|
127
|
+
});
|
|
128
|
+
streamReady = true;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
upstreamConnected = true;
|
|
132
|
+
},
|
|
133
|
+
onDisconnect() {
|
|
134
|
+
upstreamConnected = false;
|
|
135
|
+
},
|
|
136
|
+
onEvent: forwardEvent,
|
|
137
|
+
onError(error) {
|
|
138
|
+
if (controller.signal.aborted) {
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
if (!streamReady) {
|
|
143
|
+
if (error?.type === 'upstream_unavailable') {
|
|
144
|
+
closeWithInitialError({
|
|
145
|
+
message: `OpenCode event stream unavailable (${error.status})`,
|
|
146
|
+
closeReason: 'OpenCode event stream unavailable',
|
|
147
|
+
triggerHealthCheckFor: error.response,
|
|
148
|
+
});
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
closeWithInitialError({
|
|
153
|
+
message: buildUrlFailed ? 'OpenCode service unavailable' : 'Failed to connect to OpenCode event stream',
|
|
154
|
+
closeReason: buildUrlFailed ? 'OpenCode service unavailable' : 'Failed to connect to OpenCode event stream',
|
|
155
|
+
triggerHealthCheckFor: !buildUrlFailed,
|
|
156
|
+
});
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
if (error?.type === 'stream_error') {
|
|
161
|
+
console.warn('Message stream WS proxy error:', error.error);
|
|
162
|
+
}
|
|
163
|
+
},
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
await reader.start();
|
|
167
|
+
} catch (error) {
|
|
168
|
+
if (!controller.signal.aborted) {
|
|
169
|
+
console.warn('Message stream WS proxy error:', error);
|
|
170
|
+
sendMessageStreamWsFrame(socket, { type: 'error', message: 'Message stream proxy error' });
|
|
171
|
+
socket.close(1011, 'Message stream proxy error');
|
|
172
|
+
}
|
|
173
|
+
} finally {
|
|
174
|
+
cleanup();
|
|
175
|
+
try {
|
|
176
|
+
if (socket.readyState === 1 || socket.readyState === 0) {
|
|
177
|
+
socket.close();
|
|
178
|
+
}
|
|
179
|
+
} catch {
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
void run();
|
|
185
|
+
}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import { createUpstreamSseReader } from './upstream-reader.js';
|
|
2
|
+
|
|
3
|
+
// Raised from 512 → 2048 to improve recovery after brief disconnects during
|
|
4
|
+
// long-running agent sessions where many events accumulate quickly.
|
|
5
|
+
export const MESSAGE_STREAM_GLOBAL_REPLAY_LIMIT = 2048;
|
|
6
|
+
|
|
7
|
+
export function createGlobalMessageStreamHub({
|
|
8
|
+
buildOpenCodeUrl,
|
|
9
|
+
getOpenCodeAuthHeaders,
|
|
10
|
+
fetchImpl = fetch,
|
|
11
|
+
upstreamStallTimeoutMs,
|
|
12
|
+
upstreamReconnectDelayMs,
|
|
13
|
+
replayLimit = MESSAGE_STREAM_GLOBAL_REPLAY_LIMIT,
|
|
14
|
+
}) {
|
|
15
|
+
const eventSubscribers = new Set();
|
|
16
|
+
const statusSubscribers = new Set();
|
|
17
|
+
const replay = [];
|
|
18
|
+
|
|
19
|
+
let controller = null;
|
|
20
|
+
let reader = null;
|
|
21
|
+
let connected = false;
|
|
22
|
+
let everConnected = false;
|
|
23
|
+
let buildUrlFailed = false;
|
|
24
|
+
|
|
25
|
+
const notifySubscriber = (kind, subscriber, payload) => {
|
|
26
|
+
try {
|
|
27
|
+
const result = subscriber(payload);
|
|
28
|
+
if (result && typeof result.catch === 'function') {
|
|
29
|
+
result.catch((error) => {
|
|
30
|
+
console.warn(`Global message stream ${kind} subscriber failed:`, error);
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
} catch (error) {
|
|
34
|
+
console.warn(`Global message stream ${kind} subscriber failed:`, error);
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
const notifyStatus = (status) => {
|
|
39
|
+
for (const subscriber of Array.from(statusSubscribers)) {
|
|
40
|
+
notifySubscriber('status', subscriber, status);
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
const normalizeEvent = ({ envelope, payload }) => {
|
|
45
|
+
const directory =
|
|
46
|
+
typeof envelope?.directory === 'string' && envelope.directory.length > 0 ? envelope.directory : 'global';
|
|
47
|
+
const eventId = typeof envelope?.eventId === 'string' && envelope.eventId.length > 0 ? envelope.eventId : undefined;
|
|
48
|
+
return {
|
|
49
|
+
envelope,
|
|
50
|
+
payload,
|
|
51
|
+
directory,
|
|
52
|
+
eventId,
|
|
53
|
+
};
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const start = () => {
|
|
57
|
+
if (reader) {
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
controller = new AbortController();
|
|
62
|
+
reader = createUpstreamSseReader({
|
|
63
|
+
signal: controller.signal,
|
|
64
|
+
stallTimeoutMs: upstreamStallTimeoutMs,
|
|
65
|
+
reconnectDelayMs: upstreamReconnectDelayMs,
|
|
66
|
+
fetchImpl,
|
|
67
|
+
buildUrl: () => {
|
|
68
|
+
buildUrlFailed = false;
|
|
69
|
+
try {
|
|
70
|
+
return new URL(buildOpenCodeUrl('/global/event', ''));
|
|
71
|
+
} catch {
|
|
72
|
+
buildUrlFailed = true;
|
|
73
|
+
throw new Error('OpenCode service unavailable');
|
|
74
|
+
}
|
|
75
|
+
},
|
|
76
|
+
getHeaders: getOpenCodeAuthHeaders,
|
|
77
|
+
onConnect() {
|
|
78
|
+
connected = true;
|
|
79
|
+
const wasReady = everConnected;
|
|
80
|
+
everConnected = true;
|
|
81
|
+
notifyStatus({ type: 'connect', wasReady });
|
|
82
|
+
},
|
|
83
|
+
onDisconnect({ reason }) {
|
|
84
|
+
connected = false;
|
|
85
|
+
notifyStatus({ type: 'disconnect', reason });
|
|
86
|
+
},
|
|
87
|
+
onEvent(event) {
|
|
88
|
+
const normalized = normalizeEvent(event);
|
|
89
|
+
if (normalized.eventId) {
|
|
90
|
+
replay.push(normalized);
|
|
91
|
+
if (replay.length > replayLimit) {
|
|
92
|
+
replay.splice(0, replay.length - replayLimit);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
for (const subscriber of Array.from(eventSubscribers)) {
|
|
97
|
+
notifySubscriber('event', subscriber, normalized);
|
|
98
|
+
}
|
|
99
|
+
},
|
|
100
|
+
onError(error) {
|
|
101
|
+
if (controller?.signal.aborted) {
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
notifyStatus({
|
|
106
|
+
type: everConnected ? 'error' : 'initial-error',
|
|
107
|
+
error,
|
|
108
|
+
buildUrlFailed,
|
|
109
|
+
});
|
|
110
|
+
},
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
void reader.start();
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
const stop = () => {
|
|
117
|
+
connected = false;
|
|
118
|
+
reader?.stop();
|
|
119
|
+
if (controller && !controller.signal.aborted) {
|
|
120
|
+
controller.abort();
|
|
121
|
+
}
|
|
122
|
+
reader = null;
|
|
123
|
+
controller = null;
|
|
124
|
+
everConnected = false;
|
|
125
|
+
buildUrlFailed = false;
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
return {
|
|
129
|
+
start,
|
|
130
|
+
stop,
|
|
131
|
+
isConnected() {
|
|
132
|
+
return connected;
|
|
133
|
+
},
|
|
134
|
+
hasConnected() {
|
|
135
|
+
return everConnected;
|
|
136
|
+
},
|
|
137
|
+
subscribeEvent(subscriber) {
|
|
138
|
+
eventSubscribers.add(subscriber);
|
|
139
|
+
return () => {
|
|
140
|
+
eventSubscribers.delete(subscriber);
|
|
141
|
+
};
|
|
142
|
+
},
|
|
143
|
+
subscribeStatus(subscriber) {
|
|
144
|
+
statusSubscribers.add(subscriber);
|
|
145
|
+
return () => {
|
|
146
|
+
statusSubscribers.delete(subscriber);
|
|
147
|
+
};
|
|
148
|
+
},
|
|
149
|
+
replayAfter(eventId) {
|
|
150
|
+
if (!eventId) {
|
|
151
|
+
return [];
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const index = replay.findIndex((entry) => entry.eventId === eventId);
|
|
155
|
+
return index === -1 ? [] : replay.slice(index + 1);
|
|
156
|
+
},
|
|
157
|
+
};
|
|
158
|
+
}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from 'vitest';
|
|
2
|
+
|
|
3
|
+
import { createGlobalMessageStreamHub } from './global-hub.js';
|
|
4
|
+
|
|
5
|
+
function createSseResponse({ blocks = [] } = {}) {
|
|
6
|
+
const encoder = new TextEncoder();
|
|
7
|
+
let index = 0;
|
|
8
|
+
|
|
9
|
+
return {
|
|
10
|
+
ok: true,
|
|
11
|
+
body: {
|
|
12
|
+
getReader() {
|
|
13
|
+
return {
|
|
14
|
+
async read() {
|
|
15
|
+
if (index < blocks.length) {
|
|
16
|
+
return { value: encoder.encode(blocks[index++]), done: false };
|
|
17
|
+
}
|
|
18
|
+
return { value: undefined, done: true };
|
|
19
|
+
},
|
|
20
|
+
};
|
|
21
|
+
},
|
|
22
|
+
},
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
async function waitForAssertion(assertion) {
|
|
27
|
+
const deadline = Date.now() + 1000;
|
|
28
|
+
let lastError;
|
|
29
|
+
|
|
30
|
+
while (Date.now() < deadline) {
|
|
31
|
+
try {
|
|
32
|
+
assertion();
|
|
33
|
+
return;
|
|
34
|
+
} catch (error) {
|
|
35
|
+
lastError = error;
|
|
36
|
+
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
throw lastError;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
describe('createGlobalMessageStreamHub', () => {
|
|
44
|
+
it('continues fanout when an event subscriber throws', async () => {
|
|
45
|
+
const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
|
|
46
|
+
const received = [];
|
|
47
|
+
const hub = createGlobalMessageStreamHub({
|
|
48
|
+
buildOpenCodeUrl: (pathname) => `http://127.0.0.1:4096${pathname}`,
|
|
49
|
+
getOpenCodeAuthHeaders: () => ({}),
|
|
50
|
+
upstreamReconnectDelayMs: 100,
|
|
51
|
+
fetchImpl: async () => createSseResponse({
|
|
52
|
+
blocks: [
|
|
53
|
+
'id: evt-1\ndata: {"type":"session.updated","properties":{}}\n\n',
|
|
54
|
+
],
|
|
55
|
+
}),
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
hub.subscribeEvent(() => {
|
|
59
|
+
throw new Error('subscriber failed');
|
|
60
|
+
});
|
|
61
|
+
hub.subscribeEvent((event) => {
|
|
62
|
+
received.push(event.eventId);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
try {
|
|
66
|
+
hub.start();
|
|
67
|
+
await waitForAssertion(() => {
|
|
68
|
+
expect(received).toEqual(['evt-1']);
|
|
69
|
+
});
|
|
70
|
+
expect(warnSpy).toHaveBeenCalled();
|
|
71
|
+
} finally {
|
|
72
|
+
hub.stop();
|
|
73
|
+
warnSpy.mockRestore();
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it('continues status fanout when a status subscriber throws', async () => {
|
|
78
|
+
const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
|
|
79
|
+
const received = [];
|
|
80
|
+
const hub = createGlobalMessageStreamHub({
|
|
81
|
+
buildOpenCodeUrl: (pathname) => `http://127.0.0.1:4096${pathname}`,
|
|
82
|
+
getOpenCodeAuthHeaders: () => ({}),
|
|
83
|
+
upstreamReconnectDelayMs: 100,
|
|
84
|
+
fetchImpl: async () => createSseResponse(),
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
hub.subscribeStatus(() => {
|
|
88
|
+
throw new Error('status subscriber failed');
|
|
89
|
+
});
|
|
90
|
+
hub.subscribeStatus((status) => {
|
|
91
|
+
received.push(status.type);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
try {
|
|
95
|
+
hub.start();
|
|
96
|
+
await waitForAssertion(() => {
|
|
97
|
+
expect(received).toContain('connect');
|
|
98
|
+
});
|
|
99
|
+
expect(warnSpy).toHaveBeenCalled();
|
|
100
|
+
} finally {
|
|
101
|
+
hub.stop();
|
|
102
|
+
warnSpy.mockRestore();
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it('continues fanout when an async event subscriber rejects', async () => {
|
|
107
|
+
const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
|
|
108
|
+
const received = [];
|
|
109
|
+
const hub = createGlobalMessageStreamHub({
|
|
110
|
+
buildOpenCodeUrl: (pathname) => `http://127.0.0.1:4096${pathname}`,
|
|
111
|
+
getOpenCodeAuthHeaders: () => ({}),
|
|
112
|
+
upstreamReconnectDelayMs: 100,
|
|
113
|
+
fetchImpl: async () => createSseResponse({
|
|
114
|
+
blocks: [
|
|
115
|
+
'id: evt-1\ndata: {"type":"session.updated","properties":{}}\n\n',
|
|
116
|
+
],
|
|
117
|
+
}),
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
hub.subscribeEvent(async () => {
|
|
121
|
+
throw new Error('async subscriber failed');
|
|
122
|
+
});
|
|
123
|
+
hub.subscribeEvent((event) => {
|
|
124
|
+
received.push(event.eventId);
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
try {
|
|
128
|
+
hub.start();
|
|
129
|
+
await waitForAssertion(() => {
|
|
130
|
+
expect(received).toEqual(['evt-1']);
|
|
131
|
+
});
|
|
132
|
+
await waitForAssertion(() => {
|
|
133
|
+
expect(warnSpy).toHaveBeenCalled();
|
|
134
|
+
});
|
|
135
|
+
} finally {
|
|
136
|
+
hub.stop();
|
|
137
|
+
warnSpy.mockRestore();
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
});
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
import { sendMessageStreamWsEvent, sendMessageStreamWsFrame } from './protocol.js';
|
|
2
|
+
|
|
3
|
+
function shouldTriggerUpstreamHealthCheck(upstream) {
|
|
4
|
+
if (!upstream) {
|
|
5
|
+
return true;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
if (!upstream.body) {
|
|
9
|
+
return upstream.ok || upstream.status >= 500;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
return upstream.status >= 500;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function createGlobalMessageStreamWsBridge({
|
|
16
|
+
globalHub,
|
|
17
|
+
ownsGlobalHub,
|
|
18
|
+
wsClients,
|
|
19
|
+
processForwardedEventPayload,
|
|
20
|
+
triggerHealthCheck,
|
|
21
|
+
heartbeatIntervalMs,
|
|
22
|
+
}) {
|
|
23
|
+
const clients = new Set();
|
|
24
|
+
const clientLastEventIds = new Map();
|
|
25
|
+
const readyClients = new Set();
|
|
26
|
+
|
|
27
|
+
const removeClient = (socket) => {
|
|
28
|
+
clients.delete(socket);
|
|
29
|
+
clientLastEventIds.delete(socket);
|
|
30
|
+
readyClients.delete(socket);
|
|
31
|
+
wsClients.delete(socket);
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const replayEvents = (socket, requestedLastEventId) => {
|
|
35
|
+
for (const entry of globalHub.replayAfter(requestedLastEventId)) {
|
|
36
|
+
const sent = sendMessageStreamWsEvent(socket, entry.payload, {
|
|
37
|
+
directory: entry.directory,
|
|
38
|
+
eventId: entry.eventId,
|
|
39
|
+
});
|
|
40
|
+
if (!sent) {
|
|
41
|
+
removeClient(socket);
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const markReady = (socket, requestedLastEventId) => {
|
|
48
|
+
if (socket.readyState !== 1) {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const sent = sendMessageStreamWsFrame(socket, {
|
|
53
|
+
type: 'ready',
|
|
54
|
+
scope: 'global',
|
|
55
|
+
});
|
|
56
|
+
if (!sent) {
|
|
57
|
+
removeClient(socket);
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
readyClients.add(socket);
|
|
62
|
+
wsClients.add(socket);
|
|
63
|
+
replayEvents(socket, requestedLastEventId);
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
const stopHubIfUnused = () => {
|
|
67
|
+
if (ownsGlobalHub && clients.size === 0) {
|
|
68
|
+
globalHub.stop();
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
const closeClientsWithInitialError = ({ message, closeReason = message, triggerHealthCheckFor = null }) => {
|
|
73
|
+
for (const socket of Array.from(clients)) {
|
|
74
|
+
sendMessageStreamWsFrame(socket, { type: 'error', message });
|
|
75
|
+
try {
|
|
76
|
+
socket.close(1011, closeReason);
|
|
77
|
+
} catch {
|
|
78
|
+
}
|
|
79
|
+
removeClient(socket);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (triggerHealthCheckFor === true || (triggerHealthCheckFor && shouldTriggerUpstreamHealthCheck(triggerHealthCheckFor))) {
|
|
83
|
+
triggerHealthCheck?.();
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (ownsGlobalHub) {
|
|
87
|
+
globalHub.stop();
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
const unsubscribeEvent = globalHub.subscribeEvent(({ payload, directory, eventId }) => {
|
|
92
|
+
for (const socket of Array.from(clients)) {
|
|
93
|
+
if (!readyClients.has(socket)) {
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
96
|
+
const sent = sendMessageStreamWsEvent(socket, payload, {
|
|
97
|
+
directory,
|
|
98
|
+
eventId,
|
|
99
|
+
});
|
|
100
|
+
if (!sent) {
|
|
101
|
+
removeClient(socket);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
processForwardedEventPayload(payload, (syntheticPayload) => {
|
|
106
|
+
for (const socket of Array.from(clients)) {
|
|
107
|
+
if (!readyClients.has(socket)) {
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
const sent = sendMessageStreamWsEvent(socket, syntheticPayload, { directory: 'global' });
|
|
111
|
+
if (!sent) {
|
|
112
|
+
removeClient(socket);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
});
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
const unsubscribeStatus = globalHub.subscribeStatus((status) => {
|
|
119
|
+
if (status.type === 'connect') {
|
|
120
|
+
for (const socket of Array.from(clients)) {
|
|
121
|
+
if (!readyClients.has(socket)) {
|
|
122
|
+
markReady(socket, clientLastEventIds.get(socket) ?? '');
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
if (status.type === 'initial-error') {
|
|
129
|
+
const error = status.error;
|
|
130
|
+
if (error?.type === 'upstream_unavailable') {
|
|
131
|
+
closeClientsWithInitialError({
|
|
132
|
+
message: `OpenCode event stream unavailable (${error.status})`,
|
|
133
|
+
closeReason: 'OpenCode event stream unavailable',
|
|
134
|
+
triggerHealthCheckFor: error.response,
|
|
135
|
+
});
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
closeClientsWithInitialError({
|
|
140
|
+
message: status.buildUrlFailed ? 'OpenCode service unavailable' : 'Failed to connect to OpenCode event stream',
|
|
141
|
+
closeReason: status.buildUrlFailed ? 'OpenCode service unavailable' : 'Failed to connect to OpenCode event stream',
|
|
142
|
+
triggerHealthCheckFor: !status.buildUrlFailed,
|
|
143
|
+
});
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
if (status.type === 'error' && status.error?.type === 'stream_error') {
|
|
148
|
+
console.warn('Message stream WS proxy error:', status.error.error);
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
const accept = (socket, { requestedLastEventId = '' } = {}) => {
|
|
153
|
+
const pingInterval = setInterval(() => {
|
|
154
|
+
if (socket.readyState !== 1) {
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
try {
|
|
159
|
+
socket.ping();
|
|
160
|
+
} catch {
|
|
161
|
+
}
|
|
162
|
+
}, heartbeatIntervalMs);
|
|
163
|
+
|
|
164
|
+
const heartbeatInterval = setInterval(() => {
|
|
165
|
+
if (!globalHub.isConnected()) {
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
sendMessageStreamWsEvent(socket, { type: 'vinci:heartbeat', timestamp: Date.now() }, { directory: 'global' });
|
|
170
|
+
}, heartbeatIntervalMs);
|
|
171
|
+
|
|
172
|
+
socket.on('close', () => {
|
|
173
|
+
clearInterval(pingInterval);
|
|
174
|
+
clearInterval(heartbeatInterval);
|
|
175
|
+
removeClient(socket);
|
|
176
|
+
stopHubIfUnused();
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
socket.on('error', () => {
|
|
180
|
+
void 0;
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
clients.add(socket);
|
|
184
|
+
clientLastEventIds.set(socket, requestedLastEventId);
|
|
185
|
+
globalHub.start();
|
|
186
|
+
if (globalHub.isConnected()) {
|
|
187
|
+
markReady(socket, requestedLastEventId);
|
|
188
|
+
}
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
const close = () => {
|
|
192
|
+
unsubscribeEvent();
|
|
193
|
+
unsubscribeStatus();
|
|
194
|
+
if (ownsGlobalHub) {
|
|
195
|
+
globalHub.stop();
|
|
196
|
+
}
|
|
197
|
+
for (const socket of Array.from(clients)) {
|
|
198
|
+
removeClient(socket);
|
|
199
|
+
}
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
return {
|
|
203
|
+
accept,
|
|
204
|
+
close,
|
|
205
|
+
};
|
|
206
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export {
|
|
2
|
+
MESSAGE_STREAM_GLOBAL_WS_PATH,
|
|
3
|
+
MESSAGE_STREAM_DIRECTORY_WS_PATH,
|
|
4
|
+
MESSAGE_STREAM_WS_HEARTBEAT_INTERVAL_MS,
|
|
5
|
+
parseSseEventEnvelope,
|
|
6
|
+
sendMessageStreamWsFrame,
|
|
7
|
+
sendMessageStreamWsEvent,
|
|
8
|
+
} from './protocol.js';
|
|
9
|
+
|
|
10
|
+
export {
|
|
11
|
+
createGlobalUiEventBroadcaster,
|
|
12
|
+
createMessageStreamWsRuntime,
|
|
13
|
+
} from './runtime.js';
|
|
14
|
+
|
|
15
|
+
export {
|
|
16
|
+
MESSAGE_STREAM_GLOBAL_REPLAY_LIMIT,
|
|
17
|
+
createGlobalMessageStreamHub,
|
|
18
|
+
} from './global-hub.js';
|
|
19
|
+
|
|
20
|
+
export {
|
|
21
|
+
DEFAULT_UPSTREAM_RECONNECT_DELAY_MS,
|
|
22
|
+
DEFAULT_UPSTREAM_STALL_TIMEOUT_MS,
|
|
23
|
+
UPSTREAM_STALL_TIMEOUT_CONCURRENT_MS,
|
|
24
|
+
createUpstreamSseReader,
|
|
25
|
+
} from './upstream-reader.js';
|