synabun 2.4.1 → 2.4.3
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 +50 -0
- package/README.md +1 -1
- package/docs/browser-extension-bridge.md +46 -6
- package/lib/data-home-files.js +4 -2
- package/lib/first-launch-protection.js +8 -2
- package/lib/release-npm-ci.mjs +23 -1
- package/lib/update-snapshot.js +11 -4
- package/mcp-server/dist/http.d.ts.map +1 -1
- package/mcp-server/dist/http.js +25 -2
- package/mcp-server/dist/http.js.map +1 -1
- package/mcp-server/dist/index.d.ts.map +1 -1
- package/mcp-server/dist/index.js +62 -17
- package/mcp-server/dist/index.js.map +1 -1
- package/mcp-server/dist/services/browser-bench.d.ts +22 -22
- package/mcp-server/dist/services/memory-maintenance.d.ts +1 -0
- package/mcp-server/dist/services/memory-maintenance.d.ts.map +1 -1
- package/mcp-server/dist/services/memory-maintenance.js +34 -45
- package/mcp-server/dist/services/memory-maintenance.js.map +1 -1
- package/mcp-server/dist/services/memory-migration-copy.d.ts +144 -0
- package/mcp-server/dist/services/memory-migration-copy.d.ts.map +1 -0
- package/mcp-server/dist/services/memory-migration-copy.js +467 -0
- package/mcp-server/dist/services/memory-migration-copy.js.map +1 -0
- package/mcp-server/dist/services/memory-migration-worker.d.ts +2 -0
- package/mcp-server/dist/services/memory-migration-worker.d.ts.map +1 -0
- package/mcp-server/dist/services/memory-migration-worker.js +60 -0
- package/mcp-server/dist/services/memory-migration-worker.js.map +1 -0
- package/mcp-server/dist/services/memory-retrieval.d.ts +4 -0
- package/mcp-server/dist/services/memory-retrieval.d.ts.map +1 -1
- package/mcp-server/dist/services/memory-retrieval.js +188 -40
- package/mcp-server/dist/services/memory-retrieval.js.map +1 -1
- package/mcp-server/dist/services/memory-schema.d.ts +58 -2
- package/mcp-server/dist/services/memory-schema.d.ts.map +1 -1
- package/mcp-server/dist/services/memory-schema.js +190 -13
- package/mcp-server/dist/services/memory-schema.js.map +1 -1
- package/mcp-server/dist/services/memory-sessions.d.ts +19 -1
- package/mcp-server/dist/services/memory-sessions.d.ts.map +1 -1
- package/mcp-server/dist/services/memory-sessions.js +56 -20
- package/mcp-server/dist/services/memory-sessions.js.map +1 -1
- package/mcp-server/dist/services/memory-verification.d.ts.map +1 -1
- package/mcp-server/dist/services/memory-verification.js +8 -5
- package/mcp-server/dist/services/memory-verification.js.map +1 -1
- package/mcp-server/dist/services/neural-interface.d.ts +5 -0
- package/mcp-server/dist/services/neural-interface.d.ts.map +1 -1
- package/mcp-server/dist/services/neural-interface.js +19 -2
- package/mcp-server/dist/services/neural-interface.js.map +1 -1
- package/mcp-server/dist/services/retrieval-reader-worker.d.ts +2 -0
- package/mcp-server/dist/services/retrieval-reader-worker.d.ts.map +1 -0
- package/mcp-server/dist/services/retrieval-reader-worker.js +76 -0
- package/mcp-server/dist/services/retrieval-reader-worker.js.map +1 -0
- package/mcp-server/dist/services/retrieval-reader.d.ts +38 -0
- package/mcp-server/dist/services/retrieval-reader.d.ts.map +1 -0
- package/mcp-server/dist/services/retrieval-reader.js +145 -0
- package/mcp-server/dist/services/retrieval-reader.js.map +1 -0
- package/mcp-server/dist/services/sqlite-write.d.ts +14 -0
- package/mcp-server/dist/services/sqlite-write.d.ts.map +1 -0
- package/mcp-server/dist/services/sqlite-write.js +94 -0
- package/mcp-server/dist/services/sqlite-write.js.map +1 -0
- package/mcp-server/dist/services/sqlite.d.ts +39 -0
- package/mcp-server/dist/services/sqlite.d.ts.map +1 -1
- package/mcp-server/dist/services/sqlite.js +249 -8
- package/mcp-server/dist/services/sqlite.js.map +1 -1
- package/mcp-server/dist/services/tool-catalog.d.ts +1 -1
- package/mcp-server/dist/services/typesafe-config.d.ts +7 -1
- package/mcp-server/dist/services/typesafe-config.d.ts.map +1 -1
- package/mcp-server/dist/services/typesafe-config.js +32 -10
- package/mcp-server/dist/services/typesafe-config.js.map +1 -1
- package/mcp-server/dist/services/typesafe.d.ts.map +1 -1
- package/mcp-server/dist/services/typesafe.js +9 -7
- package/mcp-server/dist/services/typesafe.js.map +1 -1
- package/mcp-server/dist/services/vector-cache.d.ts +32 -0
- package/mcp-server/dist/services/vector-cache.d.ts.map +1 -1
- package/mcp-server/dist/services/vector-cache.js +161 -8
- package/mcp-server/dist/services/vector-cache.js.map +1 -1
- package/mcp-server/dist/tools/agent-definition.d.ts +2 -2
- package/mcp-server/dist/tools/agents.d.ts +11 -7
- package/mcp-server/dist/tools/agents.d.ts.map +1 -1
- package/mcp-server/dist/tools/agents.js +27 -9
- package/mcp-server/dist/tools/agents.js.map +1 -1
- package/mcp-server/dist/tools/browser-advanced.d.ts +1 -1
- package/mcp-server/dist/tools/browser-advanced.d.ts.map +1 -1
- package/mcp-server/dist/tools/browser-advanced.js +10 -3
- package/mcp-server/dist/tools/browser-advanced.js.map +1 -1
- package/mcp-server/dist/tools/browser-extension-setup.d.ts +5 -0
- package/mcp-server/dist/tools/browser-extension-setup.d.ts.map +1 -0
- package/mcp-server/dist/tools/browser-extension-setup.js +29 -0
- package/mcp-server/dist/tools/browser-extension-setup.js.map +1 -0
- package/mcp-server/dist/tools/browser-interact.d.ts.map +1 -1
- package/mcp-server/dist/tools/browser-interact.js +6 -1
- package/mcp-server/dist/tools/browser-interact.js.map +1 -1
- package/mcp-server/dist/tools/fb-groups.d.ts +2 -2
- package/mcp-server/dist/tools/skill-tools.d.ts +4 -4
- package/mcp-server/dist/tools/tool-groups.d.ts +1 -1
- package/mcp-server/package-lock.json +5 -5
- package/mcp-server/package.json +1 -1
- package/mcp-server/src/http.ts +20 -2
- package/mcp-server/src/index.ts +59 -17
- package/mcp-server/src/services/memory-maintenance.ts +31 -28
- package/mcp-server/src/services/memory-migration-copy.ts +419 -0
- package/mcp-server/src/services/memory-migration-worker.ts +55 -0
- package/mcp-server/src/services/memory-retrieval.ts +173 -32
- package/mcp-server/src/services/memory-schema.ts +194 -12
- package/mcp-server/src/services/memory-sessions.ts +54 -21
- package/mcp-server/src/services/memory-verification.ts +7 -4
- package/mcp-server/src/services/neural-interface.ts +22 -2
- package/mcp-server/src/services/retrieval-reader-worker.ts +67 -0
- package/mcp-server/src/services/retrieval-reader.ts +131 -0
- package/mcp-server/src/services/sqlite-write.ts +60 -0
- package/mcp-server/src/services/sqlite.ts +246 -8
- package/mcp-server/src/services/typesafe-config.ts +25 -6
- package/mcp-server/src/services/typesafe.ts +9 -7
- package/mcp-server/src/services/vector-cache.ts +154 -9
- package/mcp-server/src/tools/agents.ts +26 -9
- package/mcp-server/src/tools/browser-advanced.ts +10 -3
- package/mcp-server/src/tools/browser-extension-setup.ts +36 -0
- package/mcp-server/src/tools/browser-interact.ts +5 -1
- package/neural-interface/i18n/en.json +313 -2
- package/neural-interface/i18n/pt-BR.json +321 -10
- package/neural-interface/lib/agents/split.js +32 -4
- package/neural-interface/lib/assistant-api.js +52 -8
- package/neural-interface/lib/assistant-brains/claude.js +2 -0
- package/neural-interface/lib/assistant-brains/opencode.js +121 -7
- package/neural-interface/lib/assistant-catalog.js +2 -2
- package/neural-interface/lib/assistant-config.js +0 -7
- package/neural-interface/lib/assistant-dispatch.js +281 -95
- package/neural-interface/lib/assistant-envelope.js +40 -0
- package/neural-interface/lib/assistant-persona.js +16 -4
- package/neural-interface/lib/assistant-pricing.js +65 -14
- package/neural-interface/lib/assistant-router.js +131 -47
- package/neural-interface/lib/assistant-run-wait.js +45 -28
- package/neural-interface/lib/assistant-runtime.js +133 -42
- package/neural-interface/lib/backup-contents.js +158 -0
- package/neural-interface/lib/backup-crypto-io.js +330 -0
- package/neural-interface/lib/backup-crypto-worker.js +130 -0
- package/neural-interface/lib/backup-crypto.js +525 -0
- package/neural-interface/lib/backup-download.js +268 -0
- package/neural-interface/lib/backup-encryption.js +318 -0
- package/neural-interface/lib/backup-groups.js +287 -0
- package/neural-interface/lib/backup-restore-session.js +176 -0
- package/neural-interface/lib/backup-restore-stage.js +60 -0
- package/neural-interface/lib/backup-service.js +788 -233
- package/neural-interface/lib/backup-zip-reader.js +30 -18
- package/neural-interface/lib/backup-zip-writer.js +34 -10
- package/neural-interface/lib/browser-extension-adapter.js +89 -15
- package/neural-interface/lib/browser-extension-archive.js +149 -0
- package/neural-interface/lib/browser-extension-bridge.js +24 -10
- package/neural-interface/lib/browser-extension-folder.js +596 -76
- package/neural-interface/lib/browser-extension-package.js +200 -0
- package/neural-interface/lib/browser-extension-pin.js +84 -0
- package/neural-interface/lib/browser-extension-pin.json +33 -0
- package/{browser-extension/protocol.js → neural-interface/lib/browser-extension-protocol.js} +91 -6
- package/neural-interface/lib/browser-extension-setup.js +105 -18
- package/neural-interface/lib/claude-agent-bridge.js +1 -0
- package/neural-interface/lib/claude-session-cleanup.js +5 -1
- package/neural-interface/lib/cli-install.js +23 -3
- package/neural-interface/lib/cli-update-planner.js +171 -9
- package/neural-interface/lib/client-diagnostics.js +42 -0
- package/neural-interface/lib/confined-fs.js +7 -2
- package/neural-interface/lib/coordination/README.md +8 -2
- package/neural-interface/lib/coordination/contract.js +161 -0
- package/neural-interface/lib/coordination/database.js +999 -0
- package/neural-interface/lib/coordination/operations.js +436 -0
- package/neural-interface/lib/coordination/service.js +68 -537
- package/neural-interface/lib/coordination/storage-worker.js +101 -0
- package/neural-interface/lib/coordination/store.js +219 -1111
- package/neural-interface/lib/db.js +70 -32
- package/neural-interface/lib/health-count.js +37 -0
- package/neural-interface/lib/loop-stall.js +139 -0
- package/neural-interface/lib/maritaca/client.js +2 -2
- package/neural-interface/lib/maritaca/models.js +12 -17
- package/neural-interface/lib/maritaca/opencode.js +4 -2
- package/neural-interface/lib/memory-api.js +9 -1
- package/neural-interface/lib/memory-session-capture.js +42 -10
- package/neural-interface/lib/memory-store-gate.js +313 -0
- package/neural-interface/lib/memory-store-migrate.js +65 -0
- package/neural-interface/lib/memory-timers.js +7 -7
- package/neural-interface/lib/native-loop-providers.js +23 -17
- package/neural-interface/lib/native-loop-runtime.js +48 -9
- package/neural-interface/lib/opencode-build-preflight.js +18 -1
- package/neural-interface/lib/opencode-native-boundary.js +69 -8
- package/neural-interface/lib/opencode-provider-view.js +14 -3
- package/neural-interface/lib/opencode-shell-read.js +107 -0
- package/neural-interface/lib/opencode-stop-failures.js +40 -0
- package/neural-interface/lib/opencode-turn-release.js +173 -0
- package/neural-interface/lib/opencode-v2-client.js +4 -1
- package/neural-interface/lib/opencode-v2-ws-requests.js +65 -1
- package/neural-interface/lib/opencode2-client.js +343 -7
- package/neural-interface/lib/opencode2-translate.js +161 -4
- package/neural-interface/lib/recall-impact.js +122 -0
- package/neural-interface/lib/scratch-evidence.js +420 -0
- package/neural-interface/lib/scratch-process-guard.js +139 -0
- package/neural-interface/lib/scratch-references.js +279 -0
- package/neural-interface/lib/scratch-retention-core.js +385 -0
- package/neural-interface/lib/scratch-retention-worker.js +37 -0
- package/neural-interface/lib/scratch-retention.js +151 -0
- package/neural-interface/lib/session-cache-builder.js +31 -43
- package/neural-interface/lib/session-cache-worker.js +157 -0
- package/neural-interface/lib/session-indexer.js +28 -21
- package/neural-interface/lib/session-search-index.js +85 -0
- package/neural-interface/lib/skills/creation.js +1 -1
- package/neural-interface/lib/sqlite-integrity-worker.js +3 -1
- package/neural-interface/lib/style-guide/store.js +43 -3
- package/neural-interface/lib/system-restore.js +50 -0
- package/neural-interface/lib/teams/api.js +15 -6
- package/neural-interface/lib/teams/claims.js +67 -31
- package/neural-interface/lib/teams/index.js +7 -7
- package/neural-interface/lib/teams/integration.js +40 -40
- package/neural-interface/lib/teams/landing.js +6 -6
- package/neural-interface/lib/teams/recovery.js +24 -23
- package/neural-interface/lib/teams/scheduler.js +194 -95
- package/neural-interface/lib/teams/state.js +14 -14
- package/neural-interface/lib/teams/worktrees.js +27 -27
- package/neural-interface/lib/terminal-token-redaction.js +83 -0
- package/neural-interface/lib/whatsapp/bridge.js +54 -7
- package/neural-interface/lib/worker-confinement.js +47 -2
- package/neural-interface/lib/worker-mcp-channel.js +2 -0
- package/neural-interface/lib/worker-resume.js +5 -0
- package/neural-interface/lib/worker-stop.js +86 -0
- package/neural-interface/package-lock.json +5 -5
- package/neural-interface/package.json +1 -1
- package/neural-interface/public/extension-setup.html +5 -2
- package/neural-interface/public/index.html +233 -24
- package/neural-interface/public/onboarding.html +259 -34
- package/neural-interface/public/shared/agents/studio/ax-library.js +0 -6
- package/neural-interface/public/shared/agents/studio/ax-main.js +14 -7
- package/neural-interface/public/shared/agy/agy-options.js +11 -0
- package/neural-interface/public/shared/agy/agy-panel.js +67 -20
- package/neural-interface/public/shared/agy/agy-styles.js +14 -12
- package/neural-interface/public/shared/agy/agy-tabs-logic.js +4 -1
- package/neural-interface/public/shared/api.js +1 -1
- package/neural-interface/public/shared/assistant/asst-api.js +28 -3
- package/neural-interface/public/shared/assistant/asst-brain-picker.js +13 -6
- package/neural-interface/public/shared/assistant/asst-control.js +2 -0
- package/neural-interface/public/shared/assistant/asst-motion-gate.js +78 -4
- package/neural-interface/public/shared/assistant/asst-panel.js +87 -3
- package/neural-interface/public/shared/assistant/asst-rack-styles.js +27 -0
- package/neural-interface/public/shared/assistant/asst-render.js +243 -44
- package/neural-interface/public/shared/assistant/asst-route.js +100 -21
- package/neural-interface/public/shared/assistant/asst-routes-editor.js +1 -1
- package/neural-interface/public/shared/assistant/asst-shell-logic.js +200 -0
- package/neural-interface/public/shared/assistant/asst-shell.js +111 -0
- package/neural-interface/public/shared/assistant/asst-sidepanel.js +4 -2
- package/neural-interface/public/shared/assistant/asst-state.js +11 -0
- package/neural-interface/public/shared/assistant/asst-styles.js +11 -23
- package/neural-interface/public/shared/assistant/route-identity.js +2 -1
- package/neural-interface/public/shared/cdx/cdx-render.js +9 -0
- package/neural-interface/public/shared/cdx/cdx-settings.js +3 -1
- package/neural-interface/public/shared/cdx/cdx-styles.js +6 -0
- package/neural-interface/public/shared/cdx/cdx-tabs.js +54 -9
- package/neural-interface/public/shared/client-diagnostics.js +94 -0
- package/neural-interface/public/shared/cp/cp-markdown.js +7 -2
- package/neural-interface/public/shared/cp/cp-styles.js +1 -1
- package/neural-interface/public/shared/effort-i18n.js +118 -0
- package/neural-interface/public/shared/extension-setup/es-page.js +10 -4
- package/neural-interface/public/shared/extension-setup/es-view.js +25 -10
- package/neural-interface/public/shared/html-shell.js +4 -52
- package/neural-interface/public/shared/loading-diagnostic-entry.js +64 -0
- package/neural-interface/public/shared/memory-store-wait.js +52 -0
- package/neural-interface/public/shared/morelogin-profile-field.js +1 -1
- package/neural-interface/public/shared/mtc/mtc-logic.js +2 -1
- package/neural-interface/public/shared/mtc/mtc-panel.js +25 -3
- package/neural-interface/public/shared/mtc/mtc-styles.js +13 -12
- package/neural-interface/public/shared/ocp-v2/ocp-v2-childpanel.js +7 -0
- package/neural-interface/public/shared/ocp-v2/ocp-v2-events.js +5 -0
- package/neural-interface/public/shared/ocp-v2/ocp-v2-location-logic.js +109 -0
- package/neural-interface/public/shared/ocp-v2/ocp-v2-manager.js +1 -0
- package/neural-interface/public/shared/ocp-v2/ocp-v2-modelpicker.js +8 -1
- package/neural-interface/public/shared/ocp-v2/ocp-v2-panel.js +152 -8
- package/neural-interface/public/shared/ocp-v2/ocp-v2-rehydrate.js +7 -1
- package/neural-interface/public/shared/ocp-v2/ocp-v2-render.js +53 -4
- package/neural-interface/public/shared/ocp-v2/ocp-v2-send.js +52 -6
- package/neural-interface/public/shared/ocp-v2/ocp-v2-session-actions.js +22 -2
- package/neural-interface/public/shared/ocp-v2/ocp-v2-shell-block.js +165 -0
- package/neural-interface/public/shared/ocp-v2/ocp-v2-shell-logic.js +346 -0
- package/neural-interface/public/shared/ocp-v2/ocp-v2-shell.js +108 -0
- package/neural-interface/public/shared/ocp-v2/ocp-v2-state.js +49 -3
- package/neural-interface/public/shared/ocp-v2/ocp-v2-styles.js +67 -93
- package/neural-interface/public/shared/ocp-v2/ocp-v2-tools-logic.js +20 -5
- package/neural-interface/public/shared/ocp-v2/ocp-v2-tools.js +7 -1
- package/neural-interface/public/shared/ocp-v2/ocp-v2-variantpicker.js +17 -6
- package/neural-interface/public/shared/ocp-v2/ocp-v2-ws.js +7 -2
- package/neural-interface/public/shared/output-format.js +294 -0
- package/neural-interface/public/shared/provider-icons.js +6 -0
- package/neural-interface/public/shared/schedules-view.js +2 -1
- package/neural-interface/public/shared/server-presence.js +77 -0
- package/neural-interface/public/shared/settings/settings-backup-contents.js +241 -0
- package/neural-interface/public/shared/settings/settings-backup-download.js +298 -0
- package/neural-interface/public/shared/settings/settings-backup-encryption-pane.js +335 -0
- package/neural-interface/public/shared/settings/settings-backup-encryption.js +545 -0
- package/neural-interface/public/shared/settings/settings-browser-extension.js +54 -12
- package/neural-interface/public/shared/settings/settings-ia.js +1 -1
- package/neural-interface/public/shared/shell-stop-view.js +83 -0
- package/neural-interface/public/shared/skills/studio/sx-geometry.js +12 -3
- package/neural-interface/public/shared/skills/studio/sx-main.js +14 -7
- package/neural-interface/public/shared/studio-attention.js +6 -0
- package/neural-interface/public/shared/styles.css +341 -437
- package/neural-interface/public/shared/tour/tour-actor.js +14 -3
- package/neural-interface/public/shared/transcript-rows.js +28 -3
- package/neural-interface/public/shared/tray-auto-close.js +49 -6
- package/neural-interface/public/shared/ui-agents.js +1 -2
- package/neural-interface/public/shared/ui-automation-studio.js +168 -108
- package/neural-interface/public/shared/ui-claude-panel.js +62 -65
- package/neural-interface/public/shared/ui-loading.js +124 -17
- package/neural-interface/public/shared/ui-native-loop-router.js +52 -0
- package/neural-interface/public/shared/ui-navbar.js +2 -1
- package/neural-interface/public/shared/ui-output.js +533 -0
- package/neural-interface/public/shared/ui-schedules-studio.js +165 -128
- package/neural-interface/public/shared/ui-scroll-follow.js +101 -6
- package/neural-interface/public/shared/ui-sessions.js +2 -2
- package/neural-interface/public/shared/ui-settings-entry.js +18 -0
- package/neural-interface/public/shared/ui-settings.js +224 -65
- package/neural-interface/public/shared/ui-sidepanel-tray.js +3 -2
- package/neural-interface/public/shared/ui-tooltip.js +16 -0
- package/neural-interface/public/shared/ui-tutorial.js +5 -6
- package/neural-interface/public/shared/ui-worker-end.js +27 -8
- package/neural-interface/public/shared/worker-end.js +9 -4
- package/neural-interface/public/variant/2d/main.js +5 -0
- package/neural-interface/public/variant/3d/app.js +10 -0
- package/neural-interface/public/variant/3d/main.js +5 -1
- package/neural-interface/scripts/scratch-retention.mjs +20 -0
- package/neural-interface/server.js +725 -173
- package/npm-shrinkwrap.json +5 -5
- package/package.json +1 -2
- package/scripts/restore-data-home-from-backup.mjs +175 -8
- package/setup.js +18 -4
- package/browser-extension/content.js +0 -189
- package/browser-extension/engine.js +0 -508
- package/browser-extension/icons/icon-128.png +0 -0
- package/browser-extension/icons/icon-16.png +0 -0
- package/browser-extension/icons/icon-32.png +0 -0
- package/browser-extension/icons/icon-48.png +0 -0
- package/browser-extension/icons/status-off-16.png +0 -0
- package/browser-extension/icons/status-off-32.png +0 -0
- package/browser-extension/icons/status-off-48.png +0 -0
- package/browser-extension/icons/status-on-16.png +0 -0
- package/browser-extension/icons/status-on-32.png +0 -0
- package/browser-extension/icons/status-on-48.png +0 -0
- package/browser-extension/manifest.json +0 -56
- package/browser-extension/popup-strings.js +0 -250
- package/browser-extension/popup-view.js +0 -151
- package/browser-extension/popup.css +0 -166
- package/browser-extension/popup.html +0 -108
- package/browser-extension/popup.js +0 -359
- package/browser-extension/service-worker.js +0 -275
- package/browser-extension/setup-content.js +0 -36
- package/browser-extension/tokens.css +0 -326
|
@@ -26,6 +26,7 @@
|
|
|
26
26
|
|
|
27
27
|
import { basicAuthHeader } from './opencode-protocol.js';
|
|
28
28
|
import { opencodeMcpServerEntry } from './opencode-config.js';
|
|
29
|
+
import { applyOpencodeCostOverride } from './assistant-pricing.js';
|
|
29
30
|
import {
|
|
30
31
|
answersToForm,
|
|
31
32
|
createEventTranslator,
|
|
@@ -45,6 +46,10 @@ const SYSTEM_ENTRY = 'synabun-system';
|
|
|
45
46
|
const PAGE_LIMIT = 200;
|
|
46
47
|
const MAX_PAGES = 50;
|
|
47
48
|
const WAIT_TIMEOUT_MS = 30 * 60 * 1000;
|
|
49
|
+
/** How many checks of a turn that is not held waitForTurn makes before it gives up (an error, never success). */
|
|
50
|
+
const WAIT_ROUNDS_MAX = 1000;
|
|
51
|
+
/** How often a held turn is looked at again. */
|
|
52
|
+
const HELD_POLL_MS = 500;
|
|
48
53
|
|
|
49
54
|
const str = (v) => (typeof v === 'string' ? v : v == null ? '' : String(v));
|
|
50
55
|
const obj = (v) => (v && typeof v === 'object' && !Array.isArray(v) ? v : {});
|
|
@@ -64,7 +69,11 @@ export function vcsModeToV2(mode) {
|
|
|
64
69
|
* `baseUrl` of the serve and the launch's `password`. `version` is the serve's
|
|
65
70
|
* (sessions carry it), `fetchImpl` a fetch (tests pass a fixture's).
|
|
66
71
|
*/
|
|
67
|
-
export function createOpencode2Client({ baseUrl, password, fetchImpl = globalThis.fetch, version = '' } = {}) {
|
|
72
|
+
export function createOpencode2Client({ baseUrl, password, fetchImpl = globalThis.fetch, version = '', timing = {} } = {}) {
|
|
73
|
+
// `timing`: tests shorten the waits (heldPollMs, waitRoundsMax, waitPauseMs, heldSettleMs, heldRecheckMs, heldConfirmMs).
|
|
74
|
+
const HELD_POLL = Number(timing.heldPollMs) > 0 ? Number(timing.heldPollMs) : HELD_POLL_MS;
|
|
75
|
+
const ROUNDS_MAX = Number(timing.waitRoundsMax) > 0 ? Number(timing.waitRoundsMax) : WAIT_ROUNDS_MAX;
|
|
76
|
+
const WAIT_PAUSE = Number(timing.waitPauseMs) > 0 ? Number(timing.waitPauseMs) : 200;
|
|
68
77
|
if (!baseUrl) throw new Error('createOpencode2Client: baseUrl required');
|
|
69
78
|
const root = String(baseUrl).replace(/\/+$/, '');
|
|
70
79
|
const auth = password ? basicAuthHeader(password) : null;
|
|
@@ -253,6 +262,8 @@ export function createOpencode2Client({ baseUrl, password, fetchImpl = globalThi
|
|
|
253
262
|
async function sendPrompt(params = {}, options = {}) {
|
|
254
263
|
const { sessionID, model, variant, agent, system, noReply } = params;
|
|
255
264
|
if (!sessionID) return { result: { data: undefined, error: { message: 'sessionID required' }, response: { status: 400, ok: false } } };
|
|
265
|
+
// The user's turn: a wake-up a Stop left pending must not interrupt it.
|
|
266
|
+
if (noReply !== true) forgetStoppedWake(sessionID);
|
|
256
267
|
const failed = await prepare(sessionID, { model, variant, agent, system });
|
|
257
268
|
if (failed) return { result: failed };
|
|
258
269
|
const { body, synthetic } = promptBody(params);
|
|
@@ -285,10 +296,19 @@ export function createOpencode2Client({ baseUrl, password, fetchImpl = globalThi
|
|
|
285
296
|
*/
|
|
286
297
|
async function waitForTurn(sessionID, { since, signal, directory, quietEnds = false } = {}) {
|
|
287
298
|
const base = `/api/session/${encodeURIComponent(sessionID)}`;
|
|
288
|
-
for
|
|
299
|
+
// `round` counts the checks of a turn that is not held: time held for a command the model waits for (a long
|
|
300
|
+
// test run) is bounded by `signal` alone, never by this budget (review B4: 1,000 held polls of 500 ms ran it
|
|
301
|
+
// out after 8 min 20 s, and running out read as the end of the turn).
|
|
302
|
+
for (let round = 0; round < ROUNDS_MAX;) {
|
|
289
303
|
const waited = await call('POST', `/api/experimental/session/${encodeURIComponent(sessionID)}/wait`, { signal });
|
|
290
304
|
if (waited.error) return waited;
|
|
291
305
|
if (!since) return null;
|
|
306
|
+
// At rest for OpenCode, but a command the model waits for still runs: the turn is not over (a held turn).
|
|
307
|
+
if (translator.held(sessionID)) {
|
|
308
|
+
await pause(HELD_POLL, signal);
|
|
309
|
+
continue;
|
|
310
|
+
}
|
|
311
|
+
round += 1;
|
|
292
312
|
const info = await call('GET', base, { signal });
|
|
293
313
|
if (info.error) return info;
|
|
294
314
|
if (Number(info.data?.data?.time?.idle) >= since) return null;
|
|
@@ -300,9 +320,10 @@ export function createOpencode2Client({ baseUrl, password, fetchImpl = globalThi
|
|
|
300
320
|
const pending = Array.isArray(queued.data?.data) && queued.data.data.length > 0;
|
|
301
321
|
if (!queued.error && !active.error && !pending && !obj(active.data?.data)[sessionID]) return null;
|
|
302
322
|
}
|
|
303
|
-
await
|
|
323
|
+
await pause(WAIT_PAUSE, signal);
|
|
304
324
|
}
|
|
305
|
-
|
|
325
|
+
// Out of checks with the turn still not over: said as a failure, never taken for its end.
|
|
326
|
+
return { data: undefined, error: { name: 'TurnWaitExhausted', message: 'OpenCode did not say the turn was over: SynaBun stopped waiting for it.' }, response: { status: 504, ok: false } };
|
|
306
327
|
}
|
|
307
328
|
|
|
308
329
|
/**
|
|
@@ -344,6 +365,7 @@ export function createOpencode2Client({ baseUrl, password, fetchImpl = globalThi
|
|
|
344
365
|
* the command's prompt carries.
|
|
345
366
|
*/
|
|
346
367
|
async function runCommand({ sessionID, command, arguments: args, agent, model, variant, parts, directory } = {}, options = {}) {
|
|
368
|
+
forgetStoppedWake(sessionID);
|
|
347
369
|
const failed = await prepare(sessionID, { model, variant, agent });
|
|
348
370
|
if (failed) return failed;
|
|
349
371
|
const { body: attached } = promptBody({ parts });
|
|
@@ -359,6 +381,17 @@ export function createOpencode2Client({ baseUrl, password, fetchImpl = globalThi
|
|
|
359
381
|
return replyOf(sessionID, { directory, before, latest: false });
|
|
360
382
|
}
|
|
361
383
|
|
|
384
|
+
/** A wait `signal` ends early (its rejection is the caller's, as a call's would be). */
|
|
385
|
+
function pause(ms, signal) {
|
|
386
|
+
return new Promise((resolveWait, rejectWait) => {
|
|
387
|
+
if (signal?.aborted) { rejectWait(signal.reason ?? new Error('aborted')); return; }
|
|
388
|
+
const done = () => { signal?.removeEventListener?.('abort', stop); resolveWait(); };
|
|
389
|
+
const timer = setTimeout(done, ms);
|
|
390
|
+
const stop = () => { clearTimeout(timer); rejectWait(signal.reason ?? new Error('aborted')); };
|
|
391
|
+
signal?.addEventListener?.('abort', stop, { once: true });
|
|
392
|
+
});
|
|
393
|
+
}
|
|
394
|
+
|
|
362
395
|
// ── Events ──
|
|
363
396
|
|
|
364
397
|
/** `global.event()`: OpenCode 2's /api/event, translated, as `{ stream }` of `{ directory, payload }`. */
|
|
@@ -374,11 +407,33 @@ export function createOpencode2Client({ baseUrl, password, fetchImpl = globalThi
|
|
|
374
407
|
}
|
|
375
408
|
const reader = res.body.getReader();
|
|
376
409
|
const decoder = new TextDecoder();
|
|
410
|
+
// What this client says by itself (the end of a held turn: releaseHeld) reaches the reader between two events
|
|
411
|
+
// of OpenCode's, never out of a read that is under way.
|
|
412
|
+
const queue = [];
|
|
413
|
+
let wake = null;
|
|
414
|
+
const push = (events) => {
|
|
415
|
+
queue.push(...events);
|
|
416
|
+
const resume = wake;
|
|
417
|
+
wake = null;
|
|
418
|
+
resume?.();
|
|
419
|
+
};
|
|
420
|
+
streams.add(push);
|
|
421
|
+
// A stream (re)opened: what OpenCode said while none was open is unknown, so no wake-up a Stop left pending can
|
|
422
|
+
// be told from the user's next start any more (review S2).
|
|
423
|
+
stoppedWakes.clear();
|
|
377
424
|
async function* stream() {
|
|
378
425
|
let buffer = '';
|
|
426
|
+
let pending = null;
|
|
379
427
|
try {
|
|
380
428
|
while (true) {
|
|
381
|
-
|
|
429
|
+
while (queue.length) yield queue.shift();
|
|
430
|
+
if (!pending) pending = reader.read();
|
|
431
|
+
const woken = new Promise((resolveWake) => { wake = resolveWake; });
|
|
432
|
+
const result = await Promise.race([pending, woken.then(() => null)]);
|
|
433
|
+
wake = null;
|
|
434
|
+
if (!result) continue;
|
|
435
|
+
pending = null;
|
|
436
|
+
const { value, done } = result;
|
|
382
437
|
if (done) break;
|
|
383
438
|
buffer += decoder.decode(value, { stream: true });
|
|
384
439
|
let cut;
|
|
@@ -389,16 +444,281 @@ export function createOpencode2Client({ baseUrl, password, fetchImpl = globalThi
|
|
|
389
444
|
if (!data) continue;
|
|
390
445
|
let event;
|
|
391
446
|
try { event = JSON.parse(data); } catch { continue; }
|
|
447
|
+
if (event && typeof event.type === 'string') stopWoken(event);
|
|
392
448
|
for (const translated of translator.translate(event)) yield translated;
|
|
449
|
+
if (event?.type === 'shell.exited' || event?.type === 'shell.deleted') watchHeld(HELD_SETTLE_MS);
|
|
450
|
+
else if (event?.type === 'session.execution.succeeded') watchHeld();
|
|
393
451
|
}
|
|
394
452
|
}
|
|
395
453
|
} finally {
|
|
454
|
+
streams.delete(push);
|
|
396
455
|
try { reader.releaseLock(); } catch { /* already released */ }
|
|
397
456
|
}
|
|
398
457
|
}
|
|
399
458
|
return { stream: stream() };
|
|
400
459
|
}
|
|
401
460
|
|
|
461
|
+
// ── Commands that run on after their tool call (lib/opencode2-translate.js: a held turn) ──
|
|
462
|
+
|
|
463
|
+
const streams = new Set();
|
|
464
|
+
const inject = (events) => { if (events.length) for (const push of streams) push(events); };
|
|
465
|
+
|
|
466
|
+
/**
|
|
467
|
+
* Is OpenCode done with the held turn of `sessionID`: no execution, none of the commands the turn waits for still
|
|
468
|
+
* running, and no notification of one queued for the model? null when it cannot be asked. Only the waited
|
|
469
|
+
* commands count (review S1): a server the model sent to the background itself runs on for good and holds
|
|
470
|
+
* nothing; and between a command's end and the start OpenCode makes for it, its notification sits in the inbox.
|
|
471
|
+
*/
|
|
472
|
+
async function sessionSettled(sessionID) {
|
|
473
|
+
const directory = translator.sessionDirectory(sessionID);
|
|
474
|
+
const waited = new Set(translator.heldShellIds(sessionID));
|
|
475
|
+
const [active, jobs, inbox] = await Promise.all([
|
|
476
|
+
call('GET', '/api/session/active', { query: located(directory) }),
|
|
477
|
+
call('GET', '/api/experimental/job', { query: located(directory, { sessionID }) }),
|
|
478
|
+
call('GET', `/api/session/${encodeURIComponent(sessionID)}/inbox`, { query: located(directory) }),
|
|
479
|
+
]).catch(() => [null, null, null]);
|
|
480
|
+
if (!active || active.error || !jobs || jobs.error || !inbox || inbox.error) return null;
|
|
481
|
+
if (obj(active.data?.data)[sessionID]) return false;
|
|
482
|
+
const queued = Array.isArray(inbox.data?.data) ? inbox.data.data : [];
|
|
483
|
+
if (queued.length) return false;
|
|
484
|
+
const jobId = (job) => str(job?.shellID || job?.jobID || job?.id);
|
|
485
|
+
return !(Array.isArray(jobs.data?.data) ? jobs.data.data : []).some((job) => job?.status === 'running' && waited.has(jobId(job)));
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
/**
|
|
489
|
+
* A held turn ends when its command exits and OpenCode starts the session again. Should that start never come
|
|
490
|
+
* (OpenCode itself says the session is at rest and holds no command of it), the turn is over: said here.
|
|
491
|
+
*/
|
|
492
|
+
async function releaseStale(sessionID) {
|
|
493
|
+
if (disposed || !translator.held(sessionID)) return false;
|
|
494
|
+
if (await sessionSettled(sessionID) !== true) return false;
|
|
495
|
+
// Between a command's end and the start OpenCode makes for it the session is at rest for a moment: asked twice.
|
|
496
|
+
await new Promise((resolveWait) => { const timer = setTimeout(resolveWait, HELD_CONFIRM_MS); timer.unref?.(); });
|
|
497
|
+
if (disposed || !translator.held(sessionID) || await sessionSettled(sessionID) !== true || !translator.held(sessionID) || disposed) return false;
|
|
498
|
+
inject(translator.release(sessionID));
|
|
499
|
+
return true;
|
|
500
|
+
}
|
|
501
|
+
// While a turn is held, OpenCode is asked now and then whether it still backs it (an event the stream lost must
|
|
502
|
+
// not leave a session busy for good), and sooner after a command's end that should have started the session.
|
|
503
|
+
const HELD_SETTLE_MS = Number(timing.heldSettleMs) > 0 ? Number(timing.heldSettleMs) : 4000;
|
|
504
|
+
const HELD_RECHECK_MS = Number(timing.heldRecheckMs) > 0 ? Number(timing.heldRecheckMs) : 30000;
|
|
505
|
+
const HELD_CONFIRM_MS = Number(timing.heldConfirmMs) > 0 ? Number(timing.heldConfirmMs) : 1500;
|
|
506
|
+
let heldWatch = null;
|
|
507
|
+
let heldWatchAt = 0;
|
|
508
|
+
// A client that was let go (its serve stopped, its wrapper stopped) asks OpenCode nothing more (review S1).
|
|
509
|
+
let disposed = false;
|
|
510
|
+
function dispose() {
|
|
511
|
+
disposed = true;
|
|
512
|
+
if (heldWatch) clearTimeout(heldWatch);
|
|
513
|
+
heldWatch = null;
|
|
514
|
+
stoppedWakes.clear();
|
|
515
|
+
stoppedShells.clear();
|
|
516
|
+
for (const id of [...abandonedShells.keys()]) dropAbandoned(id);
|
|
517
|
+
userQueued.clear();
|
|
518
|
+
}
|
|
519
|
+
function watchHeld(ms = HELD_RECHECK_MS) {
|
|
520
|
+
if (disposed || !translator.heldSessions().length) return;
|
|
521
|
+
const at = Date.now() + ms;
|
|
522
|
+
if (heldWatch) {
|
|
523
|
+
if (at >= heldWatchAt) return;
|
|
524
|
+
clearTimeout(heldWatch);
|
|
525
|
+
}
|
|
526
|
+
heldWatchAt = at;
|
|
527
|
+
heldWatch = setTimeout(async () => {
|
|
528
|
+
heldWatch = null;
|
|
529
|
+
if (disposed) return;
|
|
530
|
+
for (const sessionID of translator.heldSessions()) await releaseStale(sessionID).catch(() => false);
|
|
531
|
+
watchHeld();
|
|
532
|
+
}, ms);
|
|
533
|
+
heldWatch.unref?.();
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
/**
|
|
537
|
+
* Stop a session. OpenCode's interrupt ends an execution and leaves the commands in the background running
|
|
538
|
+
* (checked on 2.3.2: `{ interrupted: false }` on a session at rest, the command goes on), so those are ended
|
|
539
|
+
* here, one by one, and a turn held for them is ended as a stopped one. OpenCode then tells the model that its
|
|
540
|
+
* command is gone and starts the session for that, a moment or seconds later: that execution is the stopped
|
|
541
|
+
* turn's too, and is interrupted when its start arrives on the event stream (`stopWoken`).
|
|
542
|
+
*/
|
|
543
|
+
async function abortSession(sessionID, { force = false } = {}) {
|
|
544
|
+
const base = `/api/session/${encodeURIComponent(sessionID)}`;
|
|
545
|
+
// An execution still at work with a command in the background: the commands are ended first, and the execution
|
|
546
|
+
// is interrupted only once they are. Interrupted first, its end would reach the panels as the end of the turn
|
|
547
|
+
// even when a removal then fails (the turn must stay on while its command runs). A held turn has no execution
|
|
548
|
+
// to interrupt: as before, the interrupt goes first.
|
|
549
|
+
const deleteFirst = !force && stopNeedsKill(sessionID) && !translator.held(sessionID);
|
|
550
|
+
let first = deleteFirst ? null : await call('POST', `${base}/interrupt`);
|
|
551
|
+
const running = translator.backgroundShells(sessionID);
|
|
552
|
+
if (!running.length) {
|
|
553
|
+
if (!first) first = await call('POST', `${base}/interrupt`);
|
|
554
|
+
return map(first, (body) => body?.interrupted !== false);
|
|
555
|
+
}
|
|
556
|
+
const directory = translator.sessionDirectory(sessionID);
|
|
557
|
+
const until = Date.now() + STOPPED_SHELL_MS;
|
|
558
|
+
for (const shell of running) if (!abandonedShells.has(shell.id)) stoppedShells.set(shell.id, { until, sessionID });
|
|
559
|
+
// Each removal is checked: a command whose end is not confirmed (an HTTP error, a network failure) still runs,
|
|
560
|
+
// so it is never marked killed, the turn stays held for it, and the caller is told which (review B3). A 404 is
|
|
561
|
+
// a command OpenCode no longer holds: ended.
|
|
562
|
+
const removals = await Promise.all(running.map((shell) => removeShell(shell.id, directory)));
|
|
563
|
+
const why = new Map(running.map((shell, i) => [shell.id, removals[i]]));
|
|
564
|
+
// A command an earlier "Stop anyway" gave up on holds no turn: its removal is tried again here, but it never
|
|
565
|
+
// fails the Stop of a later turn.
|
|
566
|
+
const left = running.filter((shell, i) => removals[i] !== true && !abandonedShells.has(shell.id));
|
|
567
|
+
for (const shell of left) stoppedShells.delete(shell.id);
|
|
568
|
+
if (left.length && force) {
|
|
569
|
+
// "Stop anyway" (offered after a second failed Stop): the turn ends here while the command may still run on
|
|
570
|
+
// the machine. It holds nothing from now on, its removal is tried again in the background, and the start
|
|
571
|
+
// OpenCode makes for its notification (when it ends, or when a removal works) is interrupted like a Stop's.
|
|
572
|
+
for (const shell of left) abandonShell(shell.id, sessionID, directory);
|
|
573
|
+
const ids = left.map((shell) => shell.id);
|
|
574
|
+
inject([...translator.unhold(ids), ...translator.release(sessionID, { aborted: true, running: ids })]);
|
|
575
|
+
return { data: { stopped: true, abandoned: left.map((shell) => ({ id: shell.id, command: shell.command, reason: why.get(shell.id) })) }, error: undefined, response: { status: 200, ok: true } };
|
|
576
|
+
}
|
|
577
|
+
if (left.length) {
|
|
578
|
+
const names = left.map((shell) => shell.command || shell.id);
|
|
579
|
+
const reasons = [...new Set(left.map((shell) => why.get(shell.id)))].join('; ');
|
|
580
|
+
return {
|
|
581
|
+
data: undefined,
|
|
582
|
+
error: { name: 'ShellStopFailed', message: `Stop could not end ${left.length === 1 ? 'a command' : `${left.length} commands`} still running in the background: ${names.join(', ')} (${reasons}). Press Stop again.`, data: { shells: left.map((shell) => ({ id: shell.id, command: shell.command, reason: why.get(shell.id) })) } },
|
|
583
|
+
response: { status: 502, ok: false },
|
|
584
|
+
};
|
|
585
|
+
}
|
|
586
|
+
if (!first) first = await call('POST', `${base}/interrupt`);
|
|
587
|
+
inject(translator.release(sessionID, { aborted: true }));
|
|
588
|
+
return first.error ? first : { ...first, data: true };
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
// The commands a Stop removed, until OpenCode has told the model (shellID → { until, sessionID }), and the
|
|
592
|
+
// notification OpenCode queued for each (sessionID → { inboxID, until }): the execution that starts while that
|
|
593
|
+
// notification is still undelivered, with nothing of the user's queued after it, is the stopped turn's and is
|
|
594
|
+
// interrupted. Anything else ends the entry (review S2): the notification delivered, a prompt of the user's (queued
|
|
595
|
+
// in OpenCode or sent through this client), a lapse, a new event stream (what it missed is unknown).
|
|
596
|
+
const STOPPED_SHELL_MS = 60_000;
|
|
597
|
+
const STOPPED_WAKE_MS = 30_000;
|
|
598
|
+
const stoppedShells = new Map();
|
|
599
|
+
const stoppedWakes = new Map();
|
|
600
|
+
// The commands a "Stop anyway" gave up on (shellID → { sessionID, until, timer, attempt }): they may run for
|
|
601
|
+
// hours, so they wait for their notification until they end (then STOPPED_SHELL_MS more), at most ABANDONED_MS.
|
|
602
|
+
// Unlike a Stop's, a prompt of the user's does not forget them: the turn they belonged to is over either way.
|
|
603
|
+
const ABANDONED_MS = 24 * 60 * 60_000;
|
|
604
|
+
const KILL_RETRY_MS = Array.isArray(timing.killRetryMs) && timing.killRetryMs.length ? timing.killRetryMs.map(Number) : [2_000, 5_000, 15_000, 30_000, 60_000];
|
|
605
|
+
const abandonedShells = new Map();
|
|
606
|
+
// Prompts of the user's queued in OpenCode and not delivered yet (sessionID → inbox ids, '' for one sent through
|
|
607
|
+
// this client): a notification queued after one of them wakes nothing of its own (the next start is the user's).
|
|
608
|
+
const userQueued = new Map();
|
|
609
|
+
const noteUserQueued = (sessionID, inboxID) => userQueued.set(sessionID, new Set([...(userQueued.get(sessionID) || []), inboxID]));
|
|
610
|
+
/** Would a Stop of `sessionID` have commands to end (one it could fail to end)? Those a "Stop anyway" left do not count. */
|
|
611
|
+
function stopNeedsKill(sessionID) {
|
|
612
|
+
return translator.backgroundShells(str(sessionID)).some((shell) => !abandonedShells.has(shell.id));
|
|
613
|
+
}
|
|
614
|
+
/**
|
|
615
|
+
* A removal of a command: true once OpenCode says it is gone (a 404 too); otherwise why that is not confirmed (a
|
|
616
|
+
* short line: OpenCode's own message and status, or the network error), for the panel to say.
|
|
617
|
+
*/
|
|
618
|
+
async function removeShell(shellID, directory) {
|
|
619
|
+
try {
|
|
620
|
+
const r = await call('DELETE', `/api/shell/${encodeURIComponent(shellID)}`, { query: located(directory) });
|
|
621
|
+
const status = Number(r.response?.status);
|
|
622
|
+
if (!r.error || status === 404) return true;
|
|
623
|
+
const said = str(r.error?.message || r.error?._tag || r.error?.name).trim().slice(0, 200);
|
|
624
|
+
return [said, status ? `HTTP ${status}` : ''].filter(Boolean).join(', ') || 'OpenCode did not confirm the end';
|
|
625
|
+
} catch (error) { return str(error?.cause?.code || error?.message || error).slice(0, 200) || 'OpenCode could not be reached'; }
|
|
626
|
+
}
|
|
627
|
+
/** Give up on ending `shellID` now; keep trying in the background until it ends or OpenCode removes it. */
|
|
628
|
+
function abandonShell(shellID, sessionID, directory) {
|
|
629
|
+
const entry = { sessionID, until: Date.now() + ABANDONED_MS, timer: null, attempt: 0 };
|
|
630
|
+
abandonedShells.set(shellID, entry);
|
|
631
|
+
const retry = () => {
|
|
632
|
+
if (disposed || abandonedShells.get(shellID) !== entry) return;
|
|
633
|
+
const delay = KILL_RETRY_MS[Math.min(entry.attempt, KILL_RETRY_MS.length - 1)];
|
|
634
|
+
entry.attempt += 1;
|
|
635
|
+
entry.timer = setTimeout(async () => {
|
|
636
|
+
entry.timer = null;
|
|
637
|
+
if (disposed || abandonedShells.get(shellID) !== entry) return;
|
|
638
|
+
// Ended by itself meanwhile: nothing to remove (its notification is still awaited).
|
|
639
|
+
if (translator.shell(shellID)?.status !== 'running') return;
|
|
640
|
+
if (Date.now() > entry.until) { abandonedShells.delete(shellID); return; }
|
|
641
|
+
if ((await removeShell(shellID, directory)) !== true) retry();
|
|
642
|
+
}, delay);
|
|
643
|
+
entry.timer.unref?.();
|
|
644
|
+
};
|
|
645
|
+
retry();
|
|
646
|
+
}
|
|
647
|
+
function dropAbandoned(shellID) {
|
|
648
|
+
const entry = abandonedShells.get(shellID);
|
|
649
|
+
if (!entry) return;
|
|
650
|
+
if (entry.timer) clearTimeout(entry.timer);
|
|
651
|
+
abandonedShells.delete(shellID);
|
|
652
|
+
}
|
|
653
|
+
/** A turn of the user's begins on `sessionID`: no wake-up of a Stop before it may interrupt it. */
|
|
654
|
+
function forgetStoppedWake(sessionID) {
|
|
655
|
+
const id = str(sessionID);
|
|
656
|
+
stoppedWakes.delete(id);
|
|
657
|
+
for (const [shellID, entry] of stoppedShells) if (entry.sessionID === id) stoppedShells.delete(shellID);
|
|
658
|
+
if (abandonedShells.size) noteUserQueued(id, '');
|
|
659
|
+
}
|
|
660
|
+
/** Called with every event of OpenCode's, as it arrives. */
|
|
661
|
+
function stopWoken(event) {
|
|
662
|
+
if (!stoppedShells.size && !stoppedWakes.size && !abandonedShells.size) return;
|
|
663
|
+
const data = obj(event?.data);
|
|
664
|
+
const sessionID = str(data.sessionID);
|
|
665
|
+
const now = Date.now();
|
|
666
|
+
for (const [id, entry] of stoppedShells) if (now > entry.until) stoppedShells.delete(id);
|
|
667
|
+
for (const [id, wake] of stoppedWakes) if (now > wake.until) stoppedWakes.delete(id);
|
|
668
|
+
for (const [id, entry] of abandonedShells) if (now > entry.until) dropAbandoned(id);
|
|
669
|
+
if ((event.type === 'shell.exited' || event.type === 'shell.deleted') && abandonedShells.has(str(data.id))) {
|
|
670
|
+
// Its end: the notification follows soon, or never (a serve that tells nothing).
|
|
671
|
+
const entry = abandonedShells.get(str(data.id));
|
|
672
|
+
if (entry.timer) { clearTimeout(entry.timer); entry.timer = null; }
|
|
673
|
+
entry.until = Math.min(entry.until, now + STOPPED_SHELL_MS);
|
|
674
|
+
return;
|
|
675
|
+
}
|
|
676
|
+
if (event.type === 'session.inbox.enqueued') {
|
|
677
|
+
const item = obj(data.item);
|
|
678
|
+
const shellID = item.type === 'synthetic' ? str(obj(obj(item.payload).metadata).shellID) : '';
|
|
679
|
+
if (shellID && abandonedShells.has(shellID)) {
|
|
680
|
+
dropAbandoned(shellID);
|
|
681
|
+
// A prompt of the user's waits in OpenCode before it: the start is theirs, and takes the notification along.
|
|
682
|
+
if (userQueued.get(sessionID)?.size) return;
|
|
683
|
+
stoppedWakes.set(sessionID, { inboxID: str(data.inboxID), until: now + STOPPED_WAKE_MS });
|
|
684
|
+
return;
|
|
685
|
+
}
|
|
686
|
+
const entry = shellID ? stoppedShells.get(shellID) : undefined;
|
|
687
|
+
if (entry === undefined) {
|
|
688
|
+
// Something else queued after the notification (the user's prompt): the next start may be theirs.
|
|
689
|
+
if (item.type !== 'synthetic') {
|
|
690
|
+
stoppedWakes.delete(sessionID);
|
|
691
|
+
if (abandonedShells.size) noteUserQueued(sessionID, str(data.inboxID));
|
|
692
|
+
}
|
|
693
|
+
return;
|
|
694
|
+
}
|
|
695
|
+
stoppedShells.delete(shellID);
|
|
696
|
+
stoppedWakes.set(sessionID, { inboxID: str(data.inboxID), until: now + STOPPED_WAKE_MS });
|
|
697
|
+
} else if (event.type === 'session.execution.started') {
|
|
698
|
+
userQueued.delete(sessionID);
|
|
699
|
+
if (!stoppedWakes.delete(sessionID)) return;
|
|
700
|
+
call('POST', `/api/session/${encodeURIComponent(sessionID)}/interrupt`).catch(() => {});
|
|
701
|
+
} else if (event.type === 'session.inbox.delivered') {
|
|
702
|
+
// Delivered without a start this client saw: the wake-up went by; nothing later is it.
|
|
703
|
+
if (stoppedWakes.get(sessionID)?.inboxID === str(data.inboxID)) stoppedWakes.delete(sessionID);
|
|
704
|
+
userQueued.get(sessionID)?.delete(str(data.inboxID));
|
|
705
|
+
} else if (event.type === 'session.deleted') {
|
|
706
|
+
forgetStoppedWake(sessionID);
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
/** A page of a command's output (stdout and stderr as written, in order), from the byte `cursor`. */
|
|
711
|
+
async function shellOutput({ shellID, directory, cursor = 0, limit } = {}) {
|
|
712
|
+
const id = str(shellID);
|
|
713
|
+
if (!id) return { data: undefined, error: { message: 'shellID required' }, response: { status: 400, ok: false } };
|
|
714
|
+
const where = directory || translator.sessionDirectory(translator.shell(id)?.sessionID);
|
|
715
|
+
const read = await call('GET', `/api/shell/${encodeURIComponent(id)}/output`, { query: located(where, { cursor: Math.max(0, Number(cursor) || 0), ...(Number(limit) > 0 ? { limit: Math.floor(Number(limit)) } : {}) }) });
|
|
716
|
+
return map(read, (body) => {
|
|
717
|
+
const page = obj(body?.data);
|
|
718
|
+
return { output: str(page.output), cursor: Number(page.cursor) || 0, size: Number(page.size) || 0, truncated: page.truncated === true };
|
|
719
|
+
});
|
|
720
|
+
}
|
|
721
|
+
|
|
402
722
|
// ── Asks ──
|
|
403
723
|
|
|
404
724
|
async function permissionSessionOf(requestID, directory) {
|
|
@@ -446,7 +766,7 @@ export function createOpencode2Client({ baseUrl, password, fetchImpl = globalThi
|
|
|
446
766
|
output: flags(output),
|
|
447
767
|
interleaved: false,
|
|
448
768
|
},
|
|
449
|
-
cost: { input: Number(cost.input) || 0, output: Number(cost.output) || 0, cache: { read: Number(cost.cache?.read) || 0, write: Number(cost.cache?.write) || 0 } },
|
|
769
|
+
cost: applyOpencodeCostOverride(str(model.providerID), str(model.id), { input: Number(cost.input) || 0, output: Number(cost.output) || 0, cache: { read: Number(cost.cache?.read) || 0, write: Number(cost.cache?.write) || 0 } }),
|
|
450
770
|
limit: { context: Number(model.limit?.context) || 0, ...(model.limit?.input ? { input: Number(model.limit.input) } : {}), output: Number(model.limit?.output) || 0 },
|
|
451
771
|
status: str(model.status) || 'active',
|
|
452
772
|
options: {},
|
|
@@ -567,7 +887,8 @@ export function createOpencode2Client({ baseUrl, password, fetchImpl = globalThi
|
|
|
567
887
|
if (!removed.error) sessionState.delete(sessionID);
|
|
568
888
|
return ok(removed, true);
|
|
569
889
|
},
|
|
570
|
-
|
|
890
|
+
// `force`: "Stop anyway", the turn ends even when a command in the background could not be ended.
|
|
891
|
+
abort: ({ sessionID, force } = {}) => abortSession(str(sessionID), { force: force === true }),
|
|
571
892
|
messages: ({ sessionID, directory, limit } = {}) => messagesV1(sessionID, { directory, limit }),
|
|
572
893
|
message: async ({ sessionID, messageID, directory } = {}) => {
|
|
573
894
|
const read = await call('GET', `/api/session/${encodeURIComponent(sessionID)}/message/${encodeURIComponent(messageID)}`);
|
|
@@ -575,11 +896,16 @@ export function createOpencode2Client({ baseUrl, password, fetchImpl = globalThi
|
|
|
575
896
|
},
|
|
576
897
|
prompt: (p, o) => prompt(p, o),
|
|
577
898
|
promptAsync: (p, o) => promptAsync(p, o),
|
|
899
|
+
// A session held for a command in the background (OpenCode lists it as at rest) is busy here too: a reader
|
|
900
|
+
// that asks instead of listening (a reloaded panel) is told what the events said. A hold OpenCode no longer
|
|
901
|
+
// backs is ended first.
|
|
578
902
|
status: async ({ directory } = {}) => {
|
|
903
|
+
for (const sessionID of translator.heldSessions()) await releaseStale(sessionID).catch(() => false);
|
|
579
904
|
const active = await call('GET', '/api/session/active', { query: located(directory) });
|
|
580
905
|
return map(active, (body) => {
|
|
581
906
|
const out = {};
|
|
582
907
|
for (const [id, status] of Object.entries(obj(body?.data))) out[id] = status?.type === 'running' ? { type: 'busy' } : { type: 'idle' };
|
|
908
|
+
for (const id of translator.heldSessions()) if (out[id]?.type !== 'busy') out[id] = { type: 'busy', background: true };
|
|
583
909
|
return out;
|
|
584
910
|
});
|
|
585
911
|
},
|
|
@@ -604,6 +930,7 @@ export function createOpencode2Client({ baseUrl, password, fetchImpl = globalThi
|
|
|
604
930
|
init: async ({ sessionID } = {}) => ok(await call('POST', `/api/session/${encodeURIComponent(sessionID)}/command`, { body: { name: 'init', text: '' } }), true),
|
|
605
931
|
command: (p, o) => runCommand(p, o),
|
|
606
932
|
shell: async ({ sessionID, command, agent, model } = {}) => {
|
|
933
|
+
forgetStoppedWake(sessionID);
|
|
607
934
|
const failed = await prepare(sessionID, { model, agent });
|
|
608
935
|
if (failed) return failed;
|
|
609
936
|
return ok(await call('POST', `/api/session/${encodeURIComponent(sessionID)}/shell`, { body: { command: String(command || '') } }), { info: null, parts: [] });
|
|
@@ -611,6 +938,10 @@ export function createOpencode2Client({ baseUrl, password, fetchImpl = globalThi
|
|
|
611
938
|
};
|
|
612
939
|
|
|
613
940
|
const client = {
|
|
941
|
+
/** Let this client go: no more checks of held turns are scheduled, none that is due runs. */
|
|
942
|
+
dispose,
|
|
943
|
+
/** Would a Stop of the session have a command in the background to end (and so could it fail)? */
|
|
944
|
+
stopNeedsKill,
|
|
614
945
|
global: {
|
|
615
946
|
event: globalEvent,
|
|
616
947
|
health: async () => map(await call('GET', '/api/info'), (body) => ({ healthy: true, version: str(body?.version) })),
|
|
@@ -784,6 +1115,11 @@ export function createOpencode2Client({ baseUrl, password, fetchImpl = globalThi
|
|
|
784
1115
|
return map(info, () => ({ home: '', state: '', config: '', worktree: str(directory), directory: str(directory) }));
|
|
785
1116
|
},
|
|
786
1117
|
},
|
|
1118
|
+
// The shell commands of tool calls: which a session has (as its event stream told), and a command's output.
|
|
1119
|
+
shell: {
|
|
1120
|
+
list: async ({ sessionID } = {}) => ({ data: translator.shells(sessionID), error: undefined, response: { status: 200, ok: true } }),
|
|
1121
|
+
output: (params) => shellOutput(params),
|
|
1122
|
+
},
|
|
787
1123
|
// A stateless one-shot (editor autocomplete, titles): OpenCode 2's generator.
|
|
788
1124
|
generate: async ({ prompt: text, model, directory, signal } = {}) => {
|
|
789
1125
|
const ref = modelRefToV2(model);
|