synabun 2.2.2 → 2.3.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/CHANGELOG.md +26 -0
- package/README.md +1 -1
- package/docs/whatsapp.md +52 -20
- package/lib/mcp-build.js +519 -0
- package/lib/mcp-dist.js +10 -4
- package/lib/server-supervisor.js +7 -1
- package/lib/virustotal.mjs +4 -2
- package/mcp-server/dist/http.d.ts +38 -0
- package/mcp-server/dist/http.d.ts.map +1 -1
- package/mcp-server/dist/http.js +75 -7
- package/mcp-server/dist/http.js.map +1 -1
- package/mcp-server/dist/services/browser-route-flow.d.ts +3 -2
- package/mcp-server/dist/services/browser-route-flow.d.ts.map +1 -1
- package/mcp-server/dist/services/browser-route-flow.js +4 -3
- package/mcp-server/dist/services/browser-route-flow.js.map +1 -1
- package/mcp-server/dist/services/embedding-service.d.ts.map +1 -1
- package/mcp-server/dist/services/embedding-service.js +2 -1
- package/mcp-server/dist/services/embedding-service.js.map +1 -1
- package/mcp-server/dist/services/neural-interface.d.ts +21 -0
- package/mcp-server/dist/services/neural-interface.d.ts.map +1 -1
- package/mcp-server/dist/services/neural-interface.js +76 -4
- package/mcp-server/dist/services/neural-interface.js.map +1 -1
- package/mcp-server/dist/services/tool-catalog.js +2 -2
- package/mcp-server/dist/services/tool-catalog.js.map +1 -1
- package/mcp-server/dist/tools/agents.d.ts +3 -2
- package/mcp-server/dist/tools/agents.d.ts.map +1 -1
- package/mcp-server/dist/tools/agents.js +6 -4
- package/mcp-server/dist/tools/agents.js.map +1 -1
- package/mcp-server/dist/tools/browser-advanced.d.ts +3 -1
- package/mcp-server/dist/tools/browser-advanced.d.ts.map +1 -1
- package/mcp-server/dist/tools/browser-advanced.js +30 -8
- package/mcp-server/dist/tools/browser-advanced.js.map +1 -1
- package/mcp-server/dist/tools/browser-navigate.d.ts +3 -3
- package/mcp-server/dist/tools/browser-navigate.d.ts.map +1 -1
- package/mcp-server/dist/tools/browser-navigate.js +19 -16
- package/mcp-server/dist/tools/browser-navigate.js.map +1 -1
- package/mcp-server/dist/tools/browser-observe.d.ts +2 -1
- package/mcp-server/dist/tools/browser-observe.d.ts.map +1 -1
- package/mcp-server/dist/tools/browser-observe.js +61 -20
- package/mcp-server/dist/tools/browser-observe.js.map +1 -1
- package/mcp-server/dist/tools/morelogin.d.ts +13 -3
- package/mcp-server/dist/tools/morelogin.d.ts.map +1 -1
- package/mcp-server/dist/tools/morelogin.js +81 -20
- package/mcp-server/dist/tools/morelogin.js.map +1 -1
- package/mcp-server/dist/tools/youtube/upload.d.ts.map +1 -1
- package/mcp-server/dist/tools/youtube/upload.js +225 -88
- package/mcp-server/dist/tools/youtube/upload.js.map +1 -1
- package/mcp-server/package-lock.json +2 -2
- package/mcp-server/package.json +1 -1
- package/mcp-server/src/http.ts +79 -7
- package/mcp-server/src/services/browser-route-flow.ts +4 -3
- package/mcp-server/src/services/embedding-service.ts +2 -1
- package/mcp-server/src/services/neural-interface.ts +77 -4
- package/mcp-server/src/services/tool-catalog.ts +2 -2
- package/mcp-server/src/tools/agents.ts +8 -4
- package/mcp-server/src/tools/browser-advanced.ts +29 -8
- package/mcp-server/src/tools/browser-navigate.ts +19 -16
- package/mcp-server/src/tools/browser-observe.ts +62 -20
- package/mcp-server/src/tools/morelogin.ts +81 -20
- package/mcp-server/src/tools/youtube/upload.ts +218 -99
- package/neural-interface/i18n/en.json +631 -123
- package/neural-interface/i18n/pt-BR.json +518 -10
- package/neural-interface/lib/assistant-api.js +26 -0
- package/neural-interface/lib/assistant-brains/claude.js +5 -0
- package/neural-interface/lib/assistant-catalog.js +114 -1
- package/neural-interface/lib/assistant-dispatch.js +71 -1
- package/neural-interface/lib/assistant-persona.js +14 -7
- package/neural-interface/lib/assistant-router.js +6 -4
- package/neural-interface/lib/assistant-runtime.js +3 -0
- package/neural-interface/lib/assistant-task-prompt.js +5 -0
- package/neural-interface/lib/automation-approval.js +249 -0
- package/neural-interface/lib/automation-browser-owner-access.js +82 -0
- package/neural-interface/lib/backup-service.js +16 -1
- package/neural-interface/lib/browser-background-tab.js +72 -3
- package/neural-interface/lib/browser-extraction.js +106 -45
- package/neural-interface/lib/browser-profile-choices.js +143 -0
- package/neural-interface/lib/browser-profiles.js +142 -0
- package/neural-interface/lib/browser-tab-recovery.js +153 -0
- package/neural-interface/lib/browser-tool-policy.js +2 -0
- package/neural-interface/lib/chromium-on-device-model.js +67 -0
- package/neural-interface/lib/claude-agent-bridge.js +7 -4
- package/neural-interface/lib/claude-model-catalog.js +12 -0
- package/neural-interface/lib/effort-levels.js +28 -2
- package/neural-interface/lib/local-profile-proxy.js +362 -0
- package/neural-interface/lib/local-profiles.js +929 -0
- package/neural-interface/lib/loop-logger.js +25 -4
- package/neural-interface/lib/maritaca/api.js +179 -0
- package/neural-interface/lib/maritaca/attachments.js +44 -0
- package/neural-interface/lib/maritaca/client.js +362 -0
- package/neural-interface/lib/maritaca/key-store.js +122 -0
- package/neural-interface/lib/maritaca/models.js +181 -0
- package/neural-interface/lib/maritaca/modes.js +39 -0
- package/neural-interface/lib/maritaca/opencode-proxy.js +236 -0
- package/neural-interface/lib/maritaca/opencode-stream.js +345 -0
- package/neural-interface/lib/maritaca/opencode.js +218 -0
- package/neural-interface/lib/maritaca/panel-bridge.js +273 -0
- package/neural-interface/lib/maritaca/sessions.js +198 -0
- package/neural-interface/lib/morelogin-agent-profiles.js +149 -0
- package/neural-interface/lib/morelogin.js +278 -14
- package/neural-interface/lib/native-loop-providers.js +3 -1
- package/neural-interface/lib/owner-authority.js +30 -12
- package/neural-interface/lib/page-session.js +43 -17
- package/neural-interface/lib/request-gate.js +2 -0
- package/neural-interface/lib/system-activity.js +1 -1
- package/neural-interface/lib/terminal-tokens.js +82 -5
- package/neural-interface/package-lock.json +3 -2
- package/neural-interface/package.json +2 -1
- package/neural-interface/public/assets/mascot/README.md +11 -0
- package/neural-interface/public/assets/mascot/ask.gif +0 -0
- package/neural-interface/public/assets/mascot/ask.png +0 -0
- package/neural-interface/public/assets/mascot/blocked.gif +0 -0
- package/neural-interface/public/assets/mascot/blocked.png +0 -0
- package/neural-interface/public/assets/mascot/budget.gif +0 -0
- package/neural-interface/public/assets/mascot/budget.png +0 -0
- package/neural-interface/public/assets/mascot/computer.gif +0 -0
- package/neural-interface/public/assets/mascot/computer.png +0 -0
- package/neural-interface/public/assets/mascot/edit.gif +0 -0
- package/neural-interface/public/assets/mascot/edit.png +0 -0
- package/neural-interface/public/assets/mascot/error.gif +0 -0
- package/neural-interface/public/assets/mascot/error.png +0 -0
- package/neural-interface/public/assets/mascot/hover-attention.gif +0 -0
- package/neural-interface/public/assets/mascot/hover-attention.png +0 -0
- package/neural-interface/public/assets/mascot/idle-hero.gif +0 -0
- package/neural-interface/public/assets/mascot/idle-hero.png +0 -0
- package/neural-interface/public/assets/mascot/idle.gif +0 -0
- package/neural-interface/public/assets/mascot/idle.png +0 -0
- package/neural-interface/public/assets/mascot/manifest.json +254 -0
- package/neural-interface/public/assets/mascot/mcp.gif +0 -0
- package/neural-interface/public/assets/mascot/mcp.png +0 -0
- package/neural-interface/public/assets/mascot/memory.gif +0 -0
- package/neural-interface/public/assets/mascot/memory.png +0 -0
- package/neural-interface/public/assets/mascot/offline.gif +0 -0
- package/neural-interface/public/assets/mascot/offline.png +0 -0
- package/neural-interface/public/assets/mascot/pick.gif +0 -0
- package/neural-interface/public/assets/mascot/pick.png +0 -0
- package/neural-interface/public/assets/mascot/plan.gif +0 -0
- package/neural-interface/public/assets/mascot/plan.png +0 -0
- package/neural-interface/public/assets/mascot/present.gif +0 -0
- package/neural-interface/public/assets/mascot/present.png +0 -0
- package/neural-interface/public/assets/mascot/project.gif +0 -0
- package/neural-interface/public/assets/mascot/project.png +0 -0
- package/neural-interface/public/assets/mascot/read.gif +0 -0
- package/neural-interface/public/assets/mascot/read.png +0 -0
- package/neural-interface/public/assets/mascot/runs.gif +0 -0
- package/neural-interface/public/assets/mascot/runs.png +0 -0
- package/neural-interface/public/assets/mascot/scenes/automation.gif +0 -0
- package/neural-interface/public/assets/mascot/scenes/automation.png +0 -0
- package/neural-interface/public/assets/mascot/scenes/browser.gif +0 -0
- package/neural-interface/public/assets/mascot/scenes/browser.png +0 -0
- package/neural-interface/public/assets/mascot/scenes/code.gif +0 -0
- package/neural-interface/public/assets/mascot/scenes/code.png +0 -0
- package/neural-interface/public/assets/mascot/scenes/complex.gif +0 -0
- package/neural-interface/public/assets/mascot/scenes/complex.png +0 -0
- package/neural-interface/public/assets/mascot/scenes/computer.gif +0 -0
- package/neural-interface/public/assets/mascot/scenes/computer.png +0 -0
- package/neural-interface/public/assets/mascot/scenes/design.gif +0 -0
- package/neural-interface/public/assets/mascot/scenes/design.png +0 -0
- package/neural-interface/public/assets/mascot/scenes/edit.gif +0 -0
- package/neural-interface/public/assets/mascot/scenes/edit.png +0 -0
- package/neural-interface/public/assets/mascot/scenes/generic.gif +0 -0
- package/neural-interface/public/assets/mascot/scenes/generic.png +0 -0
- package/neural-interface/public/assets/mascot/scenes/image_gen.gif +0 -0
- package/neural-interface/public/assets/mascot/scenes/image_gen.png +0 -0
- package/neural-interface/public/assets/mascot/scenes/manifest.json +173 -0
- package/neural-interface/public/assets/mascot/scenes/memory.gif +0 -0
- package/neural-interface/public/assets/mascot/scenes/memory.png +0 -0
- package/neural-interface/public/assets/mascot/scenes/quick.gif +0 -0
- package/neural-interface/public/assets/mascot/scenes/quick.png +0 -0
- package/neural-interface/public/assets/mascot/scenes/read.gif +0 -0
- package/neural-interface/public/assets/mascot/scenes/read.png +0 -0
- package/neural-interface/public/assets/mascot/scenes/research.gif +0 -0
- package/neural-interface/public/assets/mascot/scenes/research.png +0 -0
- package/neural-interface/public/assets/mascot/scenes/review.gif +0 -0
- package/neural-interface/public/assets/mascot/scenes/review.png +0 -0
- package/neural-interface/public/assets/mascot/scenes/search.gif +0 -0
- package/neural-interface/public/assets/mascot/scenes/search.png +0 -0
- package/neural-interface/public/assets/mascot/scenes/shell.gif +0 -0
- package/neural-interface/public/assets/mascot/scenes/shell.png +0 -0
- package/neural-interface/public/assets/mascot/scenes/thinking.gif +0 -0
- package/neural-interface/public/assets/mascot/scenes/thinking.png +0 -0
- package/neural-interface/public/assets/mascot/scenes/video_gen.gif +0 -0
- package/neural-interface/public/assets/mascot/scenes/video_gen.png +0 -0
- package/neural-interface/public/assets/mascot/scenes/web.gif +0 -0
- package/neural-interface/public/assets/mascot/scenes/web.png +0 -0
- package/neural-interface/public/assets/mascot/search.gif +0 -0
- package/neural-interface/public/assets/mascot/search.png +0 -0
- package/neural-interface/public/assets/mascot/shell.gif +0 -0
- package/neural-interface/public/assets/mascot/shell.png +0 -0
- package/neural-interface/public/assets/mascot/skill.gif +0 -0
- package/neural-interface/public/assets/mascot/skill.png +0 -0
- package/neural-interface/public/assets/mascot/sleep.gif +0 -0
- package/neural-interface/public/assets/mascot/sleep.png +0 -0
- package/neural-interface/public/assets/mascot/subagent.gif +0 -0
- package/neural-interface/public/assets/mascot/subagent.png +0 -0
- package/neural-interface/public/assets/mascot/success.gif +0 -0
- package/neural-interface/public/assets/mascot/success.png +0 -0
- package/neural-interface/public/assets/mascot/think.gif +0 -0
- package/neural-interface/public/assets/mascot/think.png +0 -0
- package/neural-interface/public/assets/mascot/wait.gif +0 -0
- package/neural-interface/public/assets/mascot/wait.png +0 -0
- package/neural-interface/public/assets/mascot/watch.gif +0 -0
- package/neural-interface/public/assets/mascot/watch.png +0 -0
- package/neural-interface/public/assets/mascot/web.gif +0 -0
- package/neural-interface/public/assets/mascot/web.png +0 -0
- package/neural-interface/public/onboarding.html +14 -1
- package/neural-interface/public/shared/agent-runtime-options.js +49 -8
- package/neural-interface/public/shared/agy/agy-panel.js +2 -2
- package/neural-interface/public/shared/api.js +4 -4
- package/neural-interface/public/shared/assistant/asst-ask-styles.js +8 -8
- package/neural-interface/public/shared/assistant/asst-ask.js +1 -1
- package/neural-interface/public/shared/assistant/asst-brain-picker.js +99 -9
- package/neural-interface/public/shared/assistant/asst-control.js +1 -1
- package/neural-interface/public/shared/assistant/asst-limit-wait.js +4 -4
- package/neural-interface/public/shared/assistant/asst-panel.js +6 -2
- package/neural-interface/public/shared/assistant/asst-rack-styles.js +4 -4
- package/neural-interface/public/shared/assistant/asst-render.js +6 -6
- package/neural-interface/public/shared/assistant/asst-route-models.js +64 -4
- package/neural-interface/public/shared/assistant/asst-route.js +24 -7
- package/neural-interface/public/shared/assistant/asst-routes-editor.js +154 -39
- package/neural-interface/public/shared/assistant/asst-run-stage.js +3 -3
- package/neural-interface/public/shared/assistant/asst-scene-styles.js +8 -111
- package/neural-interface/public/shared/assistant/asst-scenes.js +46 -318
- package/neural-interface/public/shared/assistant/asst-sidepanel.js +2 -2
- package/neural-interface/public/shared/assistant/asst-state.js +72 -2
- package/neural-interface/public/shared/assistant/asst-styles.js +58 -14
- package/neural-interface/public/shared/automation-approval-view.js +75 -0
- package/neural-interface/public/shared/cdx/cdx-panel.js +2 -2
- package/neural-interface/public/shared/context-menu-model.js +2 -0
- package/neural-interface/public/shared/editor/editor-panel-logic.js +10 -0
- package/neural-interface/public/shared/editor/editor-panel.js +10 -2
- package/neural-interface/public/shared/html-shell.js +30 -22
- package/neural-interface/public/shared/morelogin-profile-choices.js +107 -0
- package/neural-interface/public/shared/morelogin-profile-field.js +186 -0
- package/neural-interface/public/shared/mtc/mtc-client.js +181 -0
- package/neural-interface/public/shared/mtc/mtc-compose.js +40 -0
- package/neural-interface/public/shared/mtc/mtc-copy.js +253 -0
- package/neural-interface/public/shared/mtc/mtc-logic.js +478 -0
- package/neural-interface/public/shared/mtc/mtc-panel.js +1354 -0
- package/neural-interface/public/shared/mtc/mtc-render.js +172 -0
- package/neural-interface/public/shared/mtc/mtc-styles.js +437 -0
- package/neural-interface/public/shared/ocp-v2/ocp-v2-panel.js +6 -2
- package/neural-interface/public/shared/provider-icons.js +3 -0
- package/neural-interface/public/shared/schedules-view.js +7 -0
- package/neural-interface/public/shared/server-back.js +50 -0
- package/neural-interface/public/shared/settings/settings-browser-owner-access.js +72 -0
- package/neural-interface/public/shared/settings/settings-data.js +1 -0
- package/neural-interface/public/shared/settings/settings-ia.js +3 -1
- package/neural-interface/public/shared/settings/settings-local-profiles.js +153 -0
- package/neural-interface/public/shared/settings/settings-maritaca-opencode.js +105 -0
- package/neural-interface/public/shared/sidepanel-session-status.js +16 -2
- package/neural-interface/public/shared/state.js +1 -1
- package/neural-interface/public/shared/styles.css +356 -157
- package/neural-interface/public/shared/synabun-mascot-assets.js +67 -0
- package/neural-interface/public/shared/synabun-mascot.js +136 -12
- package/neural-interface/public/shared/synabun-ticker.js +3 -0
- package/neural-interface/public/shared/topright-overflow-logic.js +1 -0
- package/neural-interface/public/shared/ui-automation-approval.js +188 -0
- package/neural-interface/public/shared/ui-automation-studio.js +130 -90
- package/neural-interface/public/shared/ui-claude-panel.js +50 -2
- package/neural-interface/public/shared/ui-maritaca-panel.js +15 -0
- package/neural-interface/public/shared/ui-navbar.js +17 -0
- package/neural-interface/public/shared/ui-panels.js +3 -1
- package/neural-interface/public/shared/ui-schedules-studio.js +118 -17
- package/neural-interface/public/shared/ui-scroll-follow.js +3 -0
- package/neural-interface/public/shared/ui-settings.js +590 -27
- package/neural-interface/public/shared/ui-sidepanel-session-entry.js +8 -2
- package/neural-interface/public/shared/ui-sidepanel-sessions.js +3 -3
- package/neural-interface/public/shared/ui-sidepanel-tray.js +24 -0
- package/neural-interface/public/shared/ui-sidepanel-windows.js +1 -1
- package/neural-interface/scripts/build-mascot-assets.mjs +30 -0
- package/neural-interface/server.js +1784 -195
- package/npm-shrinkwrap.json +5 -17
- package/package.json +5 -6
- package/postinstall.js +38 -39
- package/setup.js +33 -31
- package/skills/synabun/modules/changelog.md +14 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,29 @@
|
|
|
1
|
+
# SynaBun v.2.3.0
|
|
2
|
+
|
|
3
|
+
SynaBun 2.3.0 adds local browser profiles and lets automations use separate profiles in parallel. Browser work stays in the background, with clearer profile choices and tighter control over which accounts agents may use.
|
|
4
|
+
|
|
5
|
+
## ✨ New
|
|
6
|
+
|
|
7
|
+
- **Local browser profiles.** Create separate browser profiles alongside MoreLogin profiles, each with its own storage, proxy, language and timezone. Manage them from Settings and choose them for browser automations.
|
|
8
|
+
- **A browser profile for each automation.** Choose a profile for a schedule, group, quick timer or launch. Runs using different profiles can work in parallel, and sessions can select an allowed profile without changing the default browser.
|
|
9
|
+
- **Animated mascot artwork.** The assistant mascot has new animated artwork for its poses.
|
|
10
|
+
- **Owner access for the automation browser.** A Settings switch lets you grant the automation browser owner access for screenshots and visual checks of your workspace.
|
|
11
|
+
|
|
12
|
+
## 🛠 Fixed
|
|
13
|
+
|
|
14
|
+
- **MoreLogin profile creation.** Creating a profile works with the operating-system fields required by newer MoreLogin versions, and the profile receives its chosen name after creation.
|
|
15
|
+
- **Clearer unsupported-profile messages.** Firefox profiles are marked as unusable for automation in profile pickers. Attempts to run one explain which browser type is needed.
|
|
16
|
+
- **Profile permissions.** Agents can choose only the profiles you allow. Default-browser changes and permission changes require owner authority, and approvals remain tied to the choices presented.
|
|
17
|
+
- **Sabiá tool calls in OpenCode.** Maritaca responses arrive in the streaming order OpenCode expects, resolving errors on tool calls.
|
|
18
|
+
- **Background browser work.** Browser automations avoid bringing windows forward or switching your macOS Space while you work.
|
|
19
|
+
|
|
20
|
+
## ⚙️ Changed
|
|
21
|
+
|
|
22
|
+
- **Faster navigation.** Browser navigation returns when loading begins. Page observations can be requested separately when needed.
|
|
23
|
+
- **Smaller browser storage.** SynaBun’s Chromium disables downloads of its on-device model.
|
|
24
|
+
|
|
25
|
+
---
|
|
26
|
+
|
|
1
27
|
# SynaBun v.2.2.2
|
|
2
28
|
|
|
3
29
|
2.2.2 makes browser automations hold on to their tabs: a lost tab is announced and replaced instead of failing quietly, and one run can no longer close a browser session other runs are using. It also fixes a slow interface on Windows, tightens SynaBun's security on macOS, and tidies how SynaBun tool calls appear in the side panels.
|
package/README.md
CHANGED
|
@@ -178,7 +178,7 @@ Everything SynaBun stores is in one folder outside the app: `~/.synabun` on macO
|
|
|
178
178
|
|
|
179
179
|
## Links
|
|
180
180
|
|
|
181
|
-
- [Changelog](./CHANGELOG.md): what is new in 2.
|
|
181
|
+
- [Changelog](./CHANGELOG.md): what is new in 2.3.0 and before
|
|
182
182
|
- [Security](./SECURITY.md): security model and how to report a vulnerability
|
|
183
183
|
- [Contributing](./CONTRIBUTING.md): bug reports and feature requests are welcome as [issues](https://github.com/danilokhury/Synabun/issues); pull requests are not accepted
|
|
184
184
|
- [Website](https://synabun.ai), [docs](https://synabun.ai/docs) and [blog](https://synabun.ai/blog)
|
package/docs/whatsapp.md
CHANGED
|
@@ -407,26 +407,58 @@ App-wide, every WebSocket upgrade (`/ws/*`) must carry no `Origin` or this serve
|
|
|
407
407
|
|
|
408
408
|
Base path `/api/whatsapp`. Errors are `{ ok: false, code, error, field? }` with the status shown; `429` adds `retryAfterMs` and `Retry-After`, `409 VERSION_CONFLICT` adds `version`.
|
|
409
409
|
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
410
|
+
- **`GET /status`**
|
|
411
|
+
- Answer: The state machine and masked data: `state` (`unavailable` +`reason` (`env`: `SYNABUN_WHATSAPP=off`; `starting`: the server is still starting the Assistant; `error`: the WhatsApp Link failed to load), `not_installed`, `installing` +`stage`, `install_failed` +`code`, `ready`, `linking` +`method` `phase` (`starting`/`waiting`/`scanned`), `link_expired`, `confirm_owner` +`kind` (`self`/`code`), `connected`, `reconnecting`, `paused`, `logged_out` +`reason` (`removed`/`inactive`/`banned`/`invalid_session`), `error` +`code`), `warning` (`replaced` (440) / `forbidden` (403) while the link retries one of them, until 10 minutes connected; else `null`), `mode`, `level`, `levelExpiresAt` (always `null`: Autonomous has no time limit; kept for older readers), `paused`, `pausedBy`, `computerUse` (the [Computer use](#computer-use) switch), `connector {installed, version, pinned, outdated, stage, errorCode}`, `account {masked, name}`, `owner {masked, boundAt}`, `confirm {kind, expiresAt, attemptsLeft}`, `session {id, title}`, `brainLimit {provider, label, text}` (the conversation's brain makes it read-only), `brainChoice {choice, source: 'assistant'|'choice', effective {provider, providerLabel, model, modelLabel, effort}, fallback {reason: 'disabled'|'unknown', provider, model}, readOnly {provider, label}, checked}` (the model selector), `connection {attempt, nextRetryAt}` (the next reconnect, or the next connector start while it is down), `host {state, restarts, retryInMs}`, `transcription {enabled, available, missing, invalid, language, home}` ([Voice notes](#voice-notes)), `counters`, `config`, `version`, `escalation`, `manualCommand`, `paths`. The `whatsapp:status` broadcast carries states, counters, `warning`, `brain` (the stored choice: ids only) and `transcription {enabled, available}`
|
|
412
|
+
- **`POST /setup`**
|
|
413
|
+
- Body: `{ mode: 'self'|'dedicated', method: 'qr'|'code', phone? }`
|
|
414
|
+
- Answer: Records the intent (never the number), installs if needed. `{ ok, next: 'install'|'link'|'none', status }` — the tab then opens `/link`
|
|
415
|
+
- **`POST /connector/install`**
|
|
416
|
+
- Body: `{ update?, reinstall? }`
|
|
417
|
+
- Answer: Starts an install (`409 INSTALL_BUSY`); progress in `/status`
|
|
418
|
+
- **`DELETE /connector/install`**
|
|
419
|
+
- Body: `{}`
|
|
420
|
+
- Answer: Cancels a running install
|
|
421
|
+
- **`DELETE /connector`**
|
|
422
|
+
- Body: `{}`
|
|
423
|
+
- Answer: Removes the connector (`409 STILL_LINKED` while linked)
|
|
424
|
+
- **`GET /connector/log`**
|
|
425
|
+
- Body: `?limit=`
|
|
426
|
+
- Answer: `{ lines }` (redacted)
|
|
427
|
+
- **`POST /link`**
|
|
428
|
+
- Body: `{ method, phone? }`
|
|
429
|
+
- Answer: **NDJSON** to this client only: `{type:'state', state:'linking', phase, method}`, `{type:'qr', svg, expiresAt}`, `{type:'pairing_code', code, expiresAt}`, `{type:'linked', mode}`, `{type:'error', code}`, `{type:'state', state:'link_expired'}`. Closing it cancels linking. One at a time (`409 LINK_BUSY`); `400 BAD_PHONE` with `field:'phone'`; `409 ALREADY_LINKED`, `NOT_INSTALLED`
|
|
430
|
+
- **`POST /owner/confirm`**
|
|
431
|
+
- Body: `{ accept: boolean }`
|
|
432
|
+
- Answer: Self mode: confirm the linked account, or log it out
|
|
433
|
+
- **`POST /owner/claim`**
|
|
434
|
+
- Body: `{}`
|
|
435
|
+
- Answer: **NDJSON**: `{type:'claim', code, sendTo (masked), waMeUrl, qrSvg, expiresAt, attemptsLeft}`, then `{type:'claim', attemptsLeft}` updates, `{type:'bound', masked}` or `{type:'expired'}`. Closing it cancels the claim
|
|
436
|
+
- **`DELETE /owner`**
|
|
437
|
+
- Body: `{}`
|
|
438
|
+
- Answer: Second-number mode: forget the owner, then claim again (`409 WRONG_MODE` in Message yourself)
|
|
439
|
+
- **`POST /test`**
|
|
440
|
+
- Body: `{}`
|
|
441
|
+
- Answer: Sends "SynaBun test message. If you can read this, WhatsApp is working." (`429 RATE_LIMITED`, `409 NOT_CONNECTED`)
|
|
442
|
+
- **`POST /pause`**
|
|
443
|
+
- Body: `{}`
|
|
444
|
+
- Answer: Pause (agents allowed: JSON body required, no Origin/UI header)
|
|
445
|
+
- **`POST /resume`, `/reconnect`, `/unlink`**
|
|
446
|
+
- Body: `{}`
|
|
447
|
+
- **`POST /remove`**
|
|
448
|
+
- Body: `{ deleteConversation? }`
|
|
449
|
+
- Answer: Unlink, uninstall, clear the log and every setting
|
|
450
|
+
- **`GET /config` · `PUT /config`**
|
|
451
|
+
- Body: `{ config: {…}, expectedVersion, confirmEscalation? }`
|
|
452
|
+
- Answer: User fields: `enabled`, `level`, `progress`, `forwardBackground`, `replyLabel`, `maxMessages`, `rotation`, `selfTrigger`, `activityText`, `strictWorkerApprovals`, `computerUse` (a boolean, off by default: [Computer use](#computer-use)), `brain` (`null` = same as the Assistant, or `{ provider, model, effort }`: checked against the Assistant's catalog, `400 CONFIG_INVALID {field:'brain'}` for an unknown or disabled model or an effort it does not run, `503 CATALOG_UNAVAILABLE` when it cannot be checked), `transcriptionEnabled`, `transcriptionLanguage`, `transcriptionEnginePath`, `transcriptionModelPath`, `transcriptionFfmpegPath` ([Voice notes](#settings-and-api): a path is checked on disk, a change re-configures the connector at once). `400 CONFIG_INVALID {field}` (service-owned fields included), `409 VERSION_CONFLICT`, `400 CONFIRM_REQUIRED`, `409 PHONE_REQUIRED`; raising to Autonomous answers `pending: { code, expiresAt, attemptsLeft }`
|
|
453
|
+
- **`GET /activity` · `DELETE /activity`**
|
|
454
|
+
- Body: `?limit=`
|
|
455
|
+
- Answer: The latest rows (`{at, kind, detail, text?}`) and counters; clear
|
|
456
|
+
- **`POST /session/new`**
|
|
457
|
+
- Body: `{}`
|
|
458
|
+
- Answer: A fresh WhatsApp conversation
|
|
459
|
+
- **`POST /__fake`**
|
|
460
|
+
- Body: `{ action, … }`
|
|
461
|
+
- Answer: Only with `SYNABUN_WHATSAPP_FAKE=1` (404 otherwise)
|
|
430
462
|
|
|
431
463
|
## Fake mode (testing)
|
|
432
464
|
|
package/lib/mcp-build.js
ADDED
|
@@ -0,0 +1,519 @@
|
|
|
1
|
+
// The MCP server's build after an install, an update or a pull: mcp-server/src compiled with TypeScript, so the
|
|
2
|
+
// running dist always matches the source on disk.
|
|
3
|
+
//
|
|
4
|
+
// - postinstall.js (every `npm i -g synabun`, `npm update -g`, the in-app updater's npm run, `npm install` in a
|
|
5
|
+
// checkout) builds always (`force`): an update is exactly when the shipped or old dist may not match.
|
|
6
|
+
// - setup.js (every launch, and before the supervisor starts the server again) builds when the stamp in dist says
|
|
7
|
+
// another version or another source built it: that is what catches a `git pull` on a clone.
|
|
8
|
+
// - The Neural Interface's POST /api/setup/build (the onboarding wizard) and the auto-build after POST
|
|
9
|
+
// /api/system/restore use the async entry (buildMcpServerSafelyAsync), so the server keeps answering while tsc runs.
|
|
10
|
+
// - A packaged application (`packaged: true`) has its dist built by the packaging (packaging/lib/stage.mjs writes
|
|
11
|
+
// the same stamp) and is replaced whole: it only asks whether its dist is whole, and never compiles.
|
|
12
|
+
//
|
|
13
|
+
// A build never leaves the user without a server: tsc writes into a fresh folder next to dist
|
|
14
|
+
// (mcp-server/.dist-build-<pid>-<time>), and only a build that exited 0 with every runtime entry of dist
|
|
15
|
+
// (lib/mcp-dist.js MCP_RUNTIME_ENTRIES: index.js, preload.js, preload-http.js) and every module they import present
|
|
16
|
+
// replaces dist. The swap renames the old dist aside (mcp-server/.dist-old-<pid>-<time>), renames the new one in, puts
|
|
17
|
+
// the old one back when the second rename fails, and removes the old one only after the swap succeeded. A build that
|
|
18
|
+
// is stopped between the two renames (or whose rollback failed) leaves dist missing and the old one aside: the next
|
|
19
|
+
// build puts that old one back before it does anything else (recoverDist), so a leftover is never the only good copy
|
|
20
|
+
// that a sweep removes. On any failure the previous dist stays as it was.
|
|
21
|
+
//
|
|
22
|
+
// One build at a time: every build holds mcp-server/.dist-build.lock (written whole to a temp file and linked into
|
|
23
|
+
// place, so it never exists empty; its owner's pid, token and start time inside) from before it sweeps until it
|
|
24
|
+
// swapped or gave up. A lock whose owner is gone, or older than LOCK_STALE_MS, is taken over through one exclusive
|
|
25
|
+
// claim per judged lock, and removed only when it is still the lock that was judged (tryBuildLock). A second builder waits up to `waitMs` (postinstall.js and setup.js wait; the Neural
|
|
26
|
+
// Interface's routes pass 0 and report `busy`). The sweep removes only temp and old folders no live build owns.
|
|
27
|
+
//
|
|
28
|
+
// The stamp (dist/.synabun-build.json) is the package version plus a SHA-256 over mcp-server/src, tsconfig.json and
|
|
29
|
+
// mcp-server/package.json: any change of version or source differs from it, so a check never skips a needed build.
|
|
30
|
+
//
|
|
31
|
+
// Nothing here throws for a build problem: every entry returns { status: 'failed', reason, output } instead, because
|
|
32
|
+
// a throw would exit the Neural Interface (its unhandled-rejection handler), fail setup.js's launch or, in
|
|
33
|
+
// postinstall.js, make npm roll the whole install back. Only plain node: modules: postinstall.js and setup.js import
|
|
34
|
+
// this on any Node.
|
|
35
|
+
|
|
36
|
+
import { execFile, execFileSync } from 'node:child_process';
|
|
37
|
+
import { createHash, randomBytes } from 'node:crypto';
|
|
38
|
+
import { closeSync, existsSync, linkSync, mkdirSync, openSync, readdirSync, readFileSync, renameSync, rmSync, statSync, writeFileSync } from 'node:fs';
|
|
39
|
+
import { join, relative, resolve, sep } from 'node:path';
|
|
40
|
+
import { MCP_RUNTIME_ENTRIES, mcpDistMissing } from './mcp-dist.js';
|
|
41
|
+
import { mcpCompiler } from './runtime-dependencies.js';
|
|
42
|
+
|
|
43
|
+
export const BUILD_STAMP = '.synabun-build.json';
|
|
44
|
+
export const BUILD_LOCK = '.dist-build.lock';
|
|
45
|
+
const TEMP_PREFIX = '.dist-build-';
|
|
46
|
+
const OLD_PREFIX = '.dist-old-';
|
|
47
|
+
/** A lock older than this is taken over even when its owner still lives (a hung compiler). */
|
|
48
|
+
export const LOCK_STALE_MS = 30 * 60_000;
|
|
49
|
+
const LOCK_POLL_MS = 250;
|
|
50
|
+
|
|
51
|
+
export const MCP_RESTART_HINT = 'Restart your AI clients\' SynaBun MCP server (in Claude Code: /mcp) so they load the new build.';
|
|
52
|
+
|
|
53
|
+
function listFiles(dir, out = []) {
|
|
54
|
+
let entries;
|
|
55
|
+
try { entries = readdirSync(dir, { withFileTypes: true }); } catch { return out; }
|
|
56
|
+
for (const entry of entries) {
|
|
57
|
+
const full = join(dir, entry.name);
|
|
58
|
+
if (entry.isDirectory()) listFiles(full, out);
|
|
59
|
+
else if (entry.isFile()) out.push(full);
|
|
60
|
+
}
|
|
61
|
+
return out;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Whether dist (or `distDir`) holds every runtime entry and every module they import. */
|
|
65
|
+
export function mcpDistWhole(packageRoot, distDir = null) {
|
|
66
|
+
return mcpDistMissing(packageRoot, { entries: MCP_RUNTIME_ENTRIES, ...(distDir ? { distDir } : {}) });
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* What the build of this checkout should be stamped with: { version, sourceHash }, or null without a source.
|
|
71
|
+
* Throws when a source file cannot be read (mcpBuildNeeded and the builders turn that into a result).
|
|
72
|
+
*/
|
|
73
|
+
export function mcpSourceStamp(packageRoot) {
|
|
74
|
+
const root = resolve(packageRoot);
|
|
75
|
+
const mcp = join(root, 'mcp-server');
|
|
76
|
+
const src = join(mcp, 'src');
|
|
77
|
+
if (!existsSync(src)) return null;
|
|
78
|
+
let version = null;
|
|
79
|
+
try { version = JSON.parse(readFileSync(join(root, 'package.json'), 'utf8')).version ?? null; } catch {}
|
|
80
|
+
const files = [...listFiles(src), join(mcp, 'tsconfig.json'), join(mcp, 'package.json')]
|
|
81
|
+
.filter(existsSync)
|
|
82
|
+
.map(file => ({ file, name: relative(mcp, file).split(sep).join('/') }))
|
|
83
|
+
.sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
|
|
84
|
+
const hash = createHash('sha256');
|
|
85
|
+
for (const { file, name } of files) {
|
|
86
|
+
hash.update(name); hash.update('\0');
|
|
87
|
+
hash.update(readFileSync(file)); hash.update('\0');
|
|
88
|
+
}
|
|
89
|
+
return { version, sourceHash: hash.digest('hex') };
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** The stamp the current dist carries, or null. */
|
|
93
|
+
export function readBuildStamp(packageRoot) {
|
|
94
|
+
try { return JSON.parse(readFileSync(join(resolve(packageRoot), 'mcp-server', 'dist', BUILD_STAMP), 'utf8')); } catch { return null; }
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Write the stamp of this source into a dist folder (the one of the package root by default). The packaging
|
|
99
|
+
* (packaging/lib/stage.mjs) writes it after its own compile, so the application's dist is "current", never
|
|
100
|
+
* "unstamped". Returns the stamp, or null without a source.
|
|
101
|
+
*/
|
|
102
|
+
export function writeBuildStamp(packageRoot, distDir = null) {
|
|
103
|
+
const root = resolve(packageRoot);
|
|
104
|
+
const stamp = mcpSourceStamp(root);
|
|
105
|
+
if (!stamp) return null;
|
|
106
|
+
const dist = distDir ? resolve(distDir) : join(root, 'mcp-server', 'dist');
|
|
107
|
+
writeFileSync(join(dist, BUILD_STAMP), `${JSON.stringify({ ...stamp, builtAt: new Date().toISOString() }, null, 2)}\n`);
|
|
108
|
+
return stamp;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Whether dist must be built: { needed, reason }. Needed when it is incomplete, or when a source is present and the
|
|
113
|
+
* stamp is missing or names another version or source. A copy with no source keeps its dist. A packaged application
|
|
114
|
+
* (`packaged`) is replaced whole and has no compiler: only completeness counts there. Never throws: a source that
|
|
115
|
+
* cannot be read is `needed` with reason 'unreadable' (the build then fails with the same error, as a result).
|
|
116
|
+
*/
|
|
117
|
+
export function mcpBuildNeeded(packageRoot, { packaged = false } = {}) {
|
|
118
|
+
try {
|
|
119
|
+
const { complete } = mcpDistWhole(packageRoot);
|
|
120
|
+
if (!complete) return { needed: true, reason: 'incomplete' };
|
|
121
|
+
if (packaged) return { needed: false, reason: 'packaged' };
|
|
122
|
+
const want = mcpSourceStamp(packageRoot);
|
|
123
|
+
if (!want) return { needed: false, reason: 'no-source' };
|
|
124
|
+
const have = readBuildStamp(packageRoot);
|
|
125
|
+
if (!have) return { needed: true, reason: 'unstamped' };
|
|
126
|
+
if (have.version !== want.version) return { needed: true, reason: 'version' };
|
|
127
|
+
if (have.sourceHash !== want.sourceHash) return { needed: true, reason: 'source' };
|
|
128
|
+
return { needed: false, reason: 'current' };
|
|
129
|
+
} catch (err) {
|
|
130
|
+
return { needed: true, reason: 'unreadable', error: err?.message || String(err) };
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const COMPILE_OPTIONS = (cwd, windowsHide) => ({ cwd, windowsHide, timeout: 180_000, maxBuffer: 32 * 1024 * 1024 });
|
|
135
|
+
|
|
136
|
+
function defaultCompile({ tsc, project, outDir, cwd, windowsHide }) {
|
|
137
|
+
execFileSync(process.execPath, [tsc, '-p', project, '--outDir', outDir], { ...COMPILE_OPTIONS(cwd, windowsHide), stdio: ['ignore', 'pipe', 'pipe'] });
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** The same compile without blocking the caller's event loop (the Neural Interface's routes). */
|
|
141
|
+
function defaultCompileAsync({ tsc, project, outDir, cwd, windowsHide }) {
|
|
142
|
+
return new Promise((done, fail) => {
|
|
143
|
+
execFile(process.execPath, [tsc, '-p', project, '--outDir', outDir], COMPILE_OPTIONS(cwd, windowsHide), (err, stdout, stderr) => {
|
|
144
|
+
if (err) { err.stdout = stdout; err.stderr = stderr; fail(err); } else done({ stdout, stderr });
|
|
145
|
+
});
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// ── The lock ──
|
|
150
|
+
|
|
151
|
+
function pidAlive(pid) {
|
|
152
|
+
if (!Number.isInteger(pid) || pid <= 0) return false;
|
|
153
|
+
try { process.kill(pid, 0); return true; } catch (err) { return err?.code === 'EPERM'; }
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/** Before any build writes its JSON in: a lock with no readable content younger than this is a build being taken. */
|
|
157
|
+
export const LOCK_EMPTY_GRACE_MS = 5_000;
|
|
158
|
+
const CLAIM_INFIX = '.takeover-';
|
|
159
|
+
/** A takeover claim older than this belongs to a waiter that died mid-takeover. */
|
|
160
|
+
const CLAIM_STALE_MS = 60_000;
|
|
161
|
+
const TEMP_INFIX = '.tmp-';
|
|
162
|
+
|
|
163
|
+
function readLockInfo(path) {
|
|
164
|
+
try { return JSON.parse(readFileSync(path, 'utf8')); } catch { return null; }
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* What is at the lock path right now: null when nothing, else { id, stale }. `id` names this very lock (its token,
|
|
169
|
+
* or for a lock without readable content its inode and change time), so a takeover can tell it from a later one.
|
|
170
|
+
* A lock is stale when its owner is gone or it is older than LOCK_STALE_MS; a lock without readable content (an
|
|
171
|
+
* older SynaBun's lock caught between its create and its write) is held for LOCK_EMPTY_GRACE_MS, then stale.
|
|
172
|
+
*/
|
|
173
|
+
function lockState(path, now = Date.now()) {
|
|
174
|
+
let st;
|
|
175
|
+
try { st = statSync(path); } catch { return null; }
|
|
176
|
+
const info = readLockInfo(path);
|
|
177
|
+
if (!info || typeof info !== 'object' || typeof info.token !== 'string' || !info.token) {
|
|
178
|
+
return { id: `ino-${st.ino}-${st.ctimeMs}`, stale: now - st.mtimeMs > LOCK_EMPTY_GRACE_MS };
|
|
179
|
+
}
|
|
180
|
+
const id = `tok-${info.token.replace(/[^0-9A-Za-z_-]/g, '_')}`;
|
|
181
|
+
let startedAt = Number(info.startedAt);
|
|
182
|
+
if (!Number.isFinite(startedAt)) startedAt = st.mtimeMs;
|
|
183
|
+
if (now - startedAt > LOCK_STALE_MS) return { id, stale: true };
|
|
184
|
+
return { id, stale: !pidAlive(Number(info.pid)) };
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/** The id of the lock at `path` as lockState names it, or null. */
|
|
188
|
+
export const lockId = (path) => lockState(path, 0)?.id ?? null;
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Remove the stale lock `judged` names, and only that one. Every waiter that judged the same lock stale races for
|
|
192
|
+
* one claim file named after it (created exclusively); only the winner removes, and only after it read the lock
|
|
193
|
+
* again and found the very lock it judged: a lock taken meanwhile has another id and is left alone. A claim
|
|
194
|
+
* whose lock is gone is swept by the next build that takes the lock.
|
|
195
|
+
*/
|
|
196
|
+
export function removeStaleLock(path, judged) {
|
|
197
|
+
const claim = `${path}${CLAIM_INFIX}${judged}`;
|
|
198
|
+
try { closeSync(openSync(claim, 'wx')); } catch (err) {
|
|
199
|
+
// Another waiter is taking this one over. A claim older than CLAIM_STALE_MS is one whose waiter died between
|
|
200
|
+
// its create and its removal (a takeover takes milliseconds): cleared, so the lock is not stuck for ever; the
|
|
201
|
+
// next attempt claims again.
|
|
202
|
+
if (err?.code === 'EEXIST') {
|
|
203
|
+
try { if (Date.now() - statSync(claim).mtimeMs > CLAIM_STALE_MS) rmSync(claim, { force: true }); } catch {}
|
|
204
|
+
}
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
try {
|
|
208
|
+
if (lockId(path) === judged) rmSync(path, { force: true });
|
|
209
|
+
} finally {
|
|
210
|
+
try { rmSync(claim, { force: true }); } catch {}
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/** Claims and temp files of the lock left by a process that stopped mid-way, now that this build holds it. */
|
|
215
|
+
function sweepLockLeftovers(mcp, ownId) {
|
|
216
|
+
let names = [];
|
|
217
|
+
try { names = readdirSync(mcp); } catch { return; }
|
|
218
|
+
for (const name of names) {
|
|
219
|
+
if (name.startsWith(`${BUILD_LOCK}${CLAIM_INFIX}`)) {
|
|
220
|
+
if (name === `${BUILD_LOCK}${CLAIM_INFIX}${ownId}`) continue;
|
|
221
|
+
try { if (Date.now() - statSync(join(mcp, name)).mtimeMs > CLAIM_STALE_MS) rmSync(join(mcp, name), { force: true }); } catch {}
|
|
222
|
+
} else if (name.startsWith(`${BUILD_LOCK}${TEMP_INFIX}`)) {
|
|
223
|
+
const pid = Number(/\.tmp-(\d+)-/.exec(name)?.[1]);
|
|
224
|
+
if (pid !== process.pid && pidAlive(pid)) continue;
|
|
225
|
+
try { if (Date.now() - statSync(join(mcp, name)).mtimeMs > LOCK_EMPTY_GRACE_MS) rmSync(join(mcp, name), { force: true }); } catch {}
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Take mcp-server/.dist-build.lock once, without waiting: the lock, or null when another build holds it.
|
|
232
|
+
* The lock appears whole: its JSON is written to a temp file first and linked to the lock path (link fails with
|
|
233
|
+
* EEXIST while one is there). A stale lock is removed (removeStaleLock) and the take tried again.
|
|
234
|
+
* Throws only for an error that is not "already there" (the folder is missing).
|
|
235
|
+
*/
|
|
236
|
+
export function tryBuildLock(mcp) {
|
|
237
|
+
const path = join(mcp, BUILD_LOCK);
|
|
238
|
+
const token = randomBytes(8).toString('hex');
|
|
239
|
+
const temp = `${path}${TEMP_INFIX}${process.pid}-${token}`;
|
|
240
|
+
writeFileSync(temp, JSON.stringify({ pid: process.pid, token, startedAt: Date.now() }), { flag: 'wx' });
|
|
241
|
+
try {
|
|
242
|
+
for (let attempt = 0; attempt < 3; attempt++) {
|
|
243
|
+
try {
|
|
244
|
+
linkSync(temp, path);
|
|
245
|
+
} catch (err) {
|
|
246
|
+
if (err?.code !== 'EEXIST') throw err;
|
|
247
|
+
const state = lockState(path);
|
|
248
|
+
if (state && !state.stale) return null;
|
|
249
|
+
if (state) removeStaleLock(path, state.id);
|
|
250
|
+
continue;
|
|
251
|
+
}
|
|
252
|
+
sweepLockLeftovers(mcp, `tok-${token}`);
|
|
253
|
+
return {
|
|
254
|
+
path,
|
|
255
|
+
pid: process.pid,
|
|
256
|
+
token,
|
|
257
|
+
// Only the very lock this build wrote is removed: never one a later build took over.
|
|
258
|
+
release() { try { if (readLockInfo(path)?.token === token) rmSync(path, { force: true }); } catch {} },
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
return null;
|
|
262
|
+
} finally {
|
|
263
|
+
try { rmSync(temp, { force: true }); } catch {}
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
function sleepSync(ms) {
|
|
268
|
+
try { Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms); } catch { /* a runtime without it: no wait */ }
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/** The lock, waiting up to waitMs (a sync caller: setup.js, postinstall.js). null when still held after that. */
|
|
272
|
+
function lockSync(mcp, waitMs) {
|
|
273
|
+
const until = Date.now() + Math.max(0, waitMs);
|
|
274
|
+
for (;;) {
|
|
275
|
+
const lock = tryBuildLock(mcp);
|
|
276
|
+
if (lock) return lock;
|
|
277
|
+
if (Date.now() >= until) return null;
|
|
278
|
+
sleepSync(Math.min(LOCK_POLL_MS, Math.max(1, until - Date.now())));
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
async function lockAsync(mcp, waitMs) {
|
|
283
|
+
const until = Date.now() + Math.max(0, waitMs);
|
|
284
|
+
for (;;) {
|
|
285
|
+
const lock = tryBuildLock(mcp);
|
|
286
|
+
if (lock) return lock;
|
|
287
|
+
if (Date.now() >= until) return null;
|
|
288
|
+
await new Promise(r => setTimeout(r, Math.min(LOCK_POLL_MS, Math.max(1, until - Date.now()))));
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/** Whether mcp-server/.dist-build.lock is held by a live build right now (a route can say "busy" before it starts). */
|
|
293
|
+
export function mcpBuildRunning(packageRoot) {
|
|
294
|
+
const path = join(resolve(packageRoot), 'mcp-server', BUILD_LOCK);
|
|
295
|
+
const state = lockState(path);
|
|
296
|
+
return !!state && !state.stale;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
// ── Leftovers ──
|
|
300
|
+
|
|
301
|
+
/** The pid in a `.dist-build-<pid>-<time>` or `.dist-old-<pid>-<time>` name, or null. */
|
|
302
|
+
function ownerPid(name) {
|
|
303
|
+
const m = /^\.dist-(?:build|old)-(\d+)-\d+$/.exec(name);
|
|
304
|
+
return m ? Number(m[1]) : null;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
const byMtimeDesc = (dir) => (a, b) => {
|
|
308
|
+
const t = (n) => { try { return statSync(join(dir, n)).mtimeMs; } catch { return 0; } };
|
|
309
|
+
return t(b) - t(a);
|
|
310
|
+
};
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* dist missing or incomplete while a whole old dist lies aside (an update stopped between its two renames, or a
|
|
314
|
+
* rollback that failed): the newest whole old one becomes dist again. Returns true when it did.
|
|
315
|
+
*/
|
|
316
|
+
function recoverDist(root, mcp, dist, log) {
|
|
317
|
+
if (mcpDistWhole(root).complete) return false;
|
|
318
|
+
let names = [];
|
|
319
|
+
try { names = readdirSync(mcp).filter(n => n.startsWith(OLD_PREFIX)); } catch { return false; }
|
|
320
|
+
for (const name of names.sort(byMtimeDesc(mcp))) {
|
|
321
|
+
const old = join(mcp, name);
|
|
322
|
+
if (!mcpDistWhole(root, old).complete) continue;
|
|
323
|
+
try {
|
|
324
|
+
if (existsSync(dist)) rmSync(dist, { recursive: true, force: true });
|
|
325
|
+
renameSync(old, dist);
|
|
326
|
+
} catch (err) {
|
|
327
|
+
log.warn(`Could not put back the previous MCP server build that an interrupted update left aside (${err?.code || err?.message}).`);
|
|
328
|
+
continue;
|
|
329
|
+
}
|
|
330
|
+
log.warn('Put back the previous MCP server build that an interrupted update left aside.');
|
|
331
|
+
return true;
|
|
332
|
+
}
|
|
333
|
+
return false;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/** Leftovers of a build or swap that was stopped (a temp folder, an old dist set aside): never one a live build owns. */
|
|
337
|
+
function sweep(mcp) {
|
|
338
|
+
let entries = [];
|
|
339
|
+
try { entries = readdirSync(mcp); } catch { return; }
|
|
340
|
+
for (const name of entries) {
|
|
341
|
+
if (!name.startsWith(TEMP_PREFIX) && !name.startsWith(OLD_PREFIX)) continue;
|
|
342
|
+
const pid = ownerPid(name);
|
|
343
|
+
// Another process still building (its folder, while it lives). This build holds the lock, so its own earlier
|
|
344
|
+
// folders, and those of a build whose process is gone, are leftovers.
|
|
345
|
+
if (pid !== null && pid !== process.pid && pidAlive(pid)) continue;
|
|
346
|
+
try { rmSync(join(mcp, name), { recursive: true, force: true }); } catch {}
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
// ── The build ──
|
|
351
|
+
|
|
352
|
+
/** The messages of a result, for the callers that print: one sentence each. */
|
|
353
|
+
const FAILED = (reason, keep) => ({
|
|
354
|
+
'no-compiler': `MCP server build failed: TypeScript is not installed (npm install at the package root installs it).${keep}`,
|
|
355
|
+
'no-source': 'MCP server build is incomplete and this copy has no source to build it from',
|
|
356
|
+
busy: 'MCP server build skipped: another SynaBun process is building it right now.',
|
|
357
|
+
lock: `MCP server build failed: the build lock could not be taken.${keep}`,
|
|
358
|
+
error: `MCP server build failed.${keep}`,
|
|
359
|
+
})[reason];
|
|
360
|
+
|
|
361
|
+
/**
|
|
362
|
+
* Everything of a build but the compile itself: the lock, the recovery, the decision, the temp folder, and (`finish`)
|
|
363
|
+
* the check, the stamp and the swap. The compile is the only step that may be awaited, so the sync entry
|
|
364
|
+
* (postinstall.js, setup.js) and the async one (the Neural Interface's routes) share this and differ only in how
|
|
365
|
+
* they take the lock and run `compile`. `lock` is already held (or null: busy). Never throws.
|
|
366
|
+
* @returns {{ done: object } | { args: object, finish: (err: Error|null, compiled?: { stdout?: string }) => object }}
|
|
367
|
+
*/
|
|
368
|
+
function startBuild(packageRoot, { force, packaged, compiler, windowsHide, rename, log, lock }) {
|
|
369
|
+
const root = resolve(packageRoot);
|
|
370
|
+
const mcp = join(root, 'mcp-server');
|
|
371
|
+
const dist = join(mcp, 'dist');
|
|
372
|
+
const release = () => { try { lock?.release(); } catch {} };
|
|
373
|
+
const failed = (reason, extra = {}) => { release(); return { done: { status: 'failed', reason, ...extra } }; };
|
|
374
|
+
try {
|
|
375
|
+
if (!lock) {
|
|
376
|
+
log.warn(FAILED('busy'));
|
|
377
|
+
return failed('busy');
|
|
378
|
+
}
|
|
379
|
+
recoverDist(root, mcp, dist, log);
|
|
380
|
+
sweep(mcp);
|
|
381
|
+
const check = mcpBuildNeeded(root, { packaged });
|
|
382
|
+
if (check.reason === 'unreadable') {
|
|
383
|
+
log.fail(`MCP server build failed: the source could not be read (${check.error}).${mcpDistWhole(root).complete ? ' The previous build is kept.' : ''}`);
|
|
384
|
+
return failed('error', { output: check.error });
|
|
385
|
+
}
|
|
386
|
+
if (packaged) {
|
|
387
|
+
// Replaced whole, no compiler: whole is current; not whole is a damaged application, nothing to build it with.
|
|
388
|
+
if (!check.needed) { log.ok('MCP server build shipped with this application'); release(); return { done: { status: 'current', reason: 'packaged' } }; }
|
|
389
|
+
log.fail('MCP server build is incomplete and this application has no compiler: reinstall SynaBun.');
|
|
390
|
+
return failed('no-source');
|
|
391
|
+
}
|
|
392
|
+
if (!force && !check.needed) {
|
|
393
|
+
log.ok(check.reason === 'no-source' ? 'MCP server build shipped with this copy' : 'MCP server build matches the installed source');
|
|
394
|
+
release();
|
|
395
|
+
return { done: { status: 'current', reason: check.reason } };
|
|
396
|
+
}
|
|
397
|
+
const stamp = mcpSourceStamp(root);
|
|
398
|
+
if (!stamp) {
|
|
399
|
+
if (check.needed) log.fail(FAILED('no-source'));
|
|
400
|
+
release();
|
|
401
|
+
return { done: { status: check.needed ? 'failed' : 'current', reason: 'no-source' } };
|
|
402
|
+
}
|
|
403
|
+
const hadDist = mcpDistWhole(root).complete;
|
|
404
|
+
log.info(force ? 'Building the MCP server from the installed source...'
|
|
405
|
+
: `Building the MCP server (${({ incomplete: 'the build is incomplete', unstamped: 'the build has no record of its source', version: 'the version changed', source: 'the source changed' })[check.reason] || check.reason})...`);
|
|
406
|
+
const tsc = compiler === undefined ? mcpCompiler(root) : compiler;
|
|
407
|
+
const keep = hadDist ? ' The previous build is kept.' : '';
|
|
408
|
+
if (!tsc) {
|
|
409
|
+
log.fail(FAILED('no-compiler', keep));
|
|
410
|
+
return failed('no-compiler');
|
|
411
|
+
}
|
|
412
|
+
const temp = join(mcp, `${TEMP_PREFIX}${process.pid}-${Date.now()}`);
|
|
413
|
+
mkdirSync(temp, { recursive: true }); // this build's folder, visible (and spared by every other build's sweep) from here on
|
|
414
|
+
const discard = () => { try { rmSync(temp, { recursive: true, force: true }); } catch {} };
|
|
415
|
+
const finish = (err, compiled = {}) => {
|
|
416
|
+
try {
|
|
417
|
+
if (err) {
|
|
418
|
+
const output = [err.stdout?.toString().trim(), err.stderr?.toString().trim()].filter(Boolean).join('\n') || err.message;
|
|
419
|
+
log.fail(`MCP server build failed.${keep}`);
|
|
420
|
+
log.error(output);
|
|
421
|
+
discard();
|
|
422
|
+
return { status: 'failed', reason: 'compile', output };
|
|
423
|
+
}
|
|
424
|
+
// The new build must be whole before it replaces anything: every runtime entry, checked as if it were dist.
|
|
425
|
+
const missing = mcpDistWhole(root, temp);
|
|
426
|
+
if (!missing.complete) {
|
|
427
|
+
log.fail(`MCP server build failed: the compiler wrote an incomplete build.${keep}`);
|
|
428
|
+
discard();
|
|
429
|
+
return { status: 'failed', reason: 'incomplete-output', output: missing.missing.join('\n') };
|
|
430
|
+
}
|
|
431
|
+
writeFileSync(join(temp, BUILD_STAMP), `${JSON.stringify({ ...stamp, builtAt: new Date().toISOString() }, null, 2)}\n`);
|
|
432
|
+
const old = join(mcp, `${OLD_PREFIX}${process.pid}-${Date.now()}`);
|
|
433
|
+
let movedAside = false;
|
|
434
|
+
try {
|
|
435
|
+
if (existsSync(dist)) { rename(dist, old); movedAside = true; }
|
|
436
|
+
rename(temp, dist);
|
|
437
|
+
} catch (swapErr) {
|
|
438
|
+
if (movedAside && !existsSync(dist)) { try { rename(old, dist); } catch {} }
|
|
439
|
+
discard();
|
|
440
|
+
log.fail(`MCP server build could not replace the previous one (${swapErr.code || swapErr.message}).${keep}`);
|
|
441
|
+
return { status: 'failed', reason: 'swap', output: swapErr.message };
|
|
442
|
+
}
|
|
443
|
+
// Only now, with the new dist in place, is the old one no longer needed.
|
|
444
|
+
if (movedAside) { try { rmSync(old, { recursive: true, force: true }); } catch {} }
|
|
445
|
+
log.ok('MCP server built');
|
|
446
|
+
if (hadDist) log.info(MCP_RESTART_HINT);
|
|
447
|
+
const output = compiled?.stdout ? String(compiled.stdout).trim() : '';
|
|
448
|
+
return { status: 'built', reason: force ? 'forced' : check.reason, ...(output ? { output } : {}) };
|
|
449
|
+
} catch (unexpected) {
|
|
450
|
+
discard();
|
|
451
|
+
log.fail(`MCP server build failed (${unexpected?.message || unexpected}).${keep}`);
|
|
452
|
+
return { status: 'failed', reason: 'error', output: unexpected?.message || String(unexpected) };
|
|
453
|
+
} finally {
|
|
454
|
+
release();
|
|
455
|
+
}
|
|
456
|
+
};
|
|
457
|
+
return { args: { tsc, project: mcp, outDir: temp, cwd: mcp, windowsHide }, finish };
|
|
458
|
+
} catch (unexpected) {
|
|
459
|
+
log.fail(`MCP server build failed (${unexpected?.message || unexpected}).`);
|
|
460
|
+
return failed('error', { output: unexpected?.message || String(unexpected) });
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
const QUIET = { ok() {}, info() {}, warn() {}, fail() {}, error() {} };
|
|
465
|
+
|
|
466
|
+
/** The lock for a build of `packageRoot`, or null when busy; a lock that cannot be created at all is an error result. */
|
|
467
|
+
function takeLockSync(packageRoot, waitMs) {
|
|
468
|
+
try { return { lock: lockSync(join(resolve(packageRoot), 'mcp-server'), waitMs) }; } catch (err) { return { error: err }; }
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
/**
|
|
472
|
+
* Build mcp-server into a temp folder and swap it in only when it succeeded. Never throws.
|
|
473
|
+
* @param {string} packageRoot
|
|
474
|
+
* @param {{ force?: boolean, packaged?: boolean, waitMs?: number, compile?: Function, compiler?: string|null, windowsHide?: boolean,
|
|
475
|
+
* rename?: Function, log?: { ok: Function, info: Function, warn: Function, fail: Function, error: Function } }} [o]
|
|
476
|
+
* `waitMs`: how long to wait for another build to finish (0: report `busy` at once).
|
|
477
|
+
* @returns {{ status: 'built'|'current'|'failed', reason: string, output?: string }}
|
|
478
|
+
*/
|
|
479
|
+
export function buildMcpServerSafely(packageRoot, {
|
|
480
|
+
force = false,
|
|
481
|
+
packaged = false,
|
|
482
|
+
waitMs = 0,
|
|
483
|
+
compile = defaultCompile,
|
|
484
|
+
compiler,
|
|
485
|
+
windowsHide = false,
|
|
486
|
+
rename = renameSync,
|
|
487
|
+
log = QUIET,
|
|
488
|
+
} = {}) {
|
|
489
|
+
const taken = takeLockSync(packageRoot, waitMs);
|
|
490
|
+
if (taken.error) { log.fail(FAILED('lock', '')); return { status: 'failed', reason: 'lock', output: taken.error.message }; }
|
|
491
|
+
const build = startBuild(packageRoot, { force, packaged, compiler, windowsHide, rename, log, lock: taken.lock });
|
|
492
|
+
if (build.done) return build.done;
|
|
493
|
+
let compiled;
|
|
494
|
+
try { compiled = compile(build.args); } catch (err) { return build.finish(err); }
|
|
495
|
+
return build.finish(null, compiled);
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
/**
|
|
499
|
+
* The same build with the compile awaited (`compile` may return a promise), for a server that must keep answering
|
|
500
|
+
* while the compiler runs. Same result shape; never rejects.
|
|
501
|
+
*/
|
|
502
|
+
export async function buildMcpServerSafelyAsync(packageRoot, {
|
|
503
|
+
force = false,
|
|
504
|
+
packaged = false,
|
|
505
|
+
waitMs = 0,
|
|
506
|
+
compile = defaultCompileAsync,
|
|
507
|
+
compiler,
|
|
508
|
+
windowsHide = false,
|
|
509
|
+
rename = renameSync,
|
|
510
|
+
log = QUIET,
|
|
511
|
+
} = {}) {
|
|
512
|
+
let lock;
|
|
513
|
+
try { lock = await lockAsync(join(resolve(packageRoot), 'mcp-server'), waitMs); } catch (err) { log.fail(FAILED('lock', '')); return { status: 'failed', reason: 'lock', output: err.message }; }
|
|
514
|
+
const build = startBuild(packageRoot, { force, packaged, compiler, windowsHide, rename, log, lock });
|
|
515
|
+
if (build.done) return build.done;
|
|
516
|
+
let compiled;
|
|
517
|
+
try { compiled = await compile(build.args); } catch (err) { return build.finish(err); }
|
|
518
|
+
return build.finish(null, compiled);
|
|
519
|
+
}
|