@supen-ai/cli 0.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +235 -0
- package/daemon/dist/acp-client.d.ts +42 -0
- package/daemon/dist/acp-client.d.ts.map +1 -0
- package/daemon/dist/acp-client.js +149 -0
- package/daemon/dist/acp-client.js.map +1 -0
- package/daemon/dist/acp-types.d.ts +98 -0
- package/daemon/dist/acp-types.d.ts.map +1 -0
- package/daemon/dist/acp-types.js +2 -0
- package/daemon/dist/acp-types.js.map +1 -0
- package/daemon/dist/agent-sdk/app-server-approvals.d.ts +24 -0
- package/daemon/dist/agent-sdk/app-server-approvals.d.ts.map +1 -0
- package/daemon/dist/agent-sdk/app-server-approvals.js +99 -0
- package/daemon/dist/agent-sdk/app-server-approvals.js.map +1 -0
- package/daemon/dist/agent-sdk/app-server-stream.d.ts +8 -0
- package/daemon/dist/agent-sdk/app-server-stream.d.ts.map +1 -0
- package/daemon/dist/agent-sdk/app-server-stream.js +328 -0
- package/daemon/dist/agent-sdk/app-server-stream.js.map +1 -0
- package/daemon/dist/agent-sdk/driver-output-ui.d.ts +9 -0
- package/daemon/dist/agent-sdk/driver-output-ui.d.ts.map +1 -0
- package/daemon/dist/agent-sdk/driver-output-ui.js +290 -0
- package/daemon/dist/agent-sdk/driver-output-ui.js.map +1 -0
- package/daemon/dist/agent-sdk/drivers/acpx-driver.d.ts +21 -0
- package/daemon/dist/agent-sdk/drivers/acpx-driver.d.ts.map +1 -0
- package/daemon/dist/agent-sdk/drivers/acpx-driver.js +488 -0
- package/daemon/dist/agent-sdk/drivers/acpx-driver.js.map +1 -0
- package/daemon/dist/agent-sdk/drivers/claude-acpx-driver.d.ts +5 -0
- package/daemon/dist/agent-sdk/drivers/claude-acpx-driver.d.ts.map +1 -0
- package/daemon/dist/agent-sdk/drivers/claude-acpx-driver.js +7 -0
- package/daemon/dist/agent-sdk/drivers/claude-acpx-driver.js.map +1 -0
- package/daemon/dist/agent-sdk/drivers/claude-cli-direct-driver.d.ts +20 -0
- package/daemon/dist/agent-sdk/drivers/claude-cli-direct-driver.d.ts.map +1 -0
- package/daemon/dist/agent-sdk/drivers/claude-cli-direct-driver.js +264 -0
- package/daemon/dist/agent-sdk/drivers/claude-cli-direct-driver.js.map +1 -0
- package/daemon/dist/agent-sdk/drivers/claude-code-driver.d.ts +29 -0
- package/daemon/dist/agent-sdk/drivers/claude-code-driver.d.ts.map +1 -0
- package/daemon/dist/agent-sdk/drivers/claude-code-driver.js +24 -0
- package/daemon/dist/agent-sdk/drivers/claude-code-driver.js.map +1 -0
- package/daemon/dist/agent-sdk/drivers/claude-code-events.d.ts +25 -0
- package/daemon/dist/agent-sdk/drivers/claude-code-events.d.ts.map +1 -0
- package/daemon/dist/agent-sdk/drivers/claude-code-events.js +58 -0
- package/daemon/dist/agent-sdk/drivers/claude-code-events.js.map +1 -0
- package/daemon/dist/agent-sdk/drivers/claude-code-items.d.ts +41 -0
- package/daemon/dist/agent-sdk/drivers/claude-code-items.d.ts.map +1 -0
- package/daemon/dist/agent-sdk/drivers/claude-code-items.js +77 -0
- package/daemon/dist/agent-sdk/drivers/claude-code-items.js.map +1 -0
- package/daemon/dist/agent-sdk/drivers/codex-acpx-driver.d.ts +5 -0
- package/daemon/dist/agent-sdk/drivers/codex-acpx-driver.d.ts.map +1 -0
- package/daemon/dist/agent-sdk/drivers/codex-acpx-driver.js +7 -0
- package/daemon/dist/agent-sdk/drivers/codex-acpx-driver.js.map +1 -0
- package/daemon/dist/agent-sdk/drivers/codex-app-server-driver.d.ts +12 -0
- package/daemon/dist/agent-sdk/drivers/codex-app-server-driver.d.ts.map +1 -0
- package/daemon/dist/agent-sdk/drivers/codex-app-server-driver.js +484 -0
- package/daemon/dist/agent-sdk/drivers/codex-app-server-driver.js.map +1 -0
- package/daemon/dist/agent-sdk/drivers/codex-exec-driver.d.ts +28 -0
- package/daemon/dist/agent-sdk/drivers/codex-exec-driver.d.ts.map +1 -0
- package/daemon/dist/agent-sdk/drivers/codex-exec-driver.js +219 -0
- package/daemon/dist/agent-sdk/drivers/codex-exec-driver.js.map +1 -0
- package/daemon/dist/agent-sdk/drivers/codex-exec-events.d.ts +9 -0
- package/daemon/dist/agent-sdk/drivers/codex-exec-events.d.ts.map +1 -0
- package/daemon/dist/agent-sdk/drivers/codex-exec-events.js +82 -0
- package/daemon/dist/agent-sdk/drivers/codex-exec-events.js.map +1 -0
- package/daemon/dist/agent-sdk/drivers/codex-exec-items.d.ts +9 -0
- package/daemon/dist/agent-sdk/drivers/codex-exec-items.d.ts.map +1 -0
- package/daemon/dist/agent-sdk/drivers/codex-exec-items.js +86 -0
- package/daemon/dist/agent-sdk/drivers/codex-exec-items.js.map +1 -0
- package/daemon/dist/agent-sdk/drivers/driver.d.ts +60 -0
- package/daemon/dist/agent-sdk/drivers/driver.d.ts.map +1 -0
- package/daemon/dist/agent-sdk/drivers/driver.js +2 -0
- package/daemon/dist/agent-sdk/drivers/driver.js.map +1 -0
- package/daemon/dist/agent-sdk/drivers/gemini-cli-driver.d.ts +17 -0
- package/daemon/dist/agent-sdk/drivers/gemini-cli-driver.d.ts.map +1 -0
- package/daemon/dist/agent-sdk/drivers/gemini-cli-driver.js +255 -0
- package/daemon/dist/agent-sdk/drivers/gemini-cli-driver.js.map +1 -0
- package/daemon/dist/agent-sdk/drivers/registry.d.ts +21 -0
- package/daemon/dist/agent-sdk/drivers/registry.d.ts.map +1 -0
- package/daemon/dist/agent-sdk/drivers/registry.js +167 -0
- package/daemon/dist/agent-sdk/drivers/registry.js.map +1 -0
- package/daemon/dist/agent-sdk/index.d.ts +24 -0
- package/daemon/dist/agent-sdk/index.d.ts.map +1 -0
- package/daemon/dist/agent-sdk/index.js +19 -0
- package/daemon/dist/agent-sdk/index.js.map +1 -0
- package/daemon/dist/agent-sdk/intelligence/contracts.d.ts +17 -0
- package/daemon/dist/agent-sdk/intelligence/contracts.d.ts.map +1 -0
- package/daemon/dist/agent-sdk/intelligence/contracts.js +2 -0
- package/daemon/dist/agent-sdk/intelligence/contracts.js.map +1 -0
- package/daemon/dist/agent-sdk/memory/filesystem.d.ts +7 -0
- package/daemon/dist/agent-sdk/memory/filesystem.d.ts.map +1 -0
- package/daemon/dist/agent-sdk/memory/filesystem.js +133 -0
- package/daemon/dist/agent-sdk/memory/filesystem.js.map +1 -0
- package/daemon/dist/agent-sdk/memory/subsystem.d.ts +54 -0
- package/daemon/dist/agent-sdk/memory/subsystem.d.ts.map +1 -0
- package/daemon/dist/agent-sdk/memory/subsystem.js +106 -0
- package/daemon/dist/agent-sdk/memory/subsystem.js.map +1 -0
- package/daemon/dist/agent-sdk/session-events.d.ts +59 -0
- package/daemon/dist/agent-sdk/session-events.d.ts.map +1 -0
- package/daemon/dist/agent-sdk/session-events.js +104 -0
- package/daemon/dist/agent-sdk/session-events.js.map +1 -0
- package/daemon/dist/agent-sdk/session-manager.d.ts +28 -0
- package/daemon/dist/agent-sdk/session-manager.d.ts.map +1 -0
- package/daemon/dist/agent-sdk/session-manager.js +54 -0
- package/daemon/dist/agent-sdk/session-manager.js.map +1 -0
- package/daemon/dist/agent-sdk/types.d.ts +110 -0
- package/daemon/dist/agent-sdk/types.d.ts.map +1 -0
- package/daemon/dist/agent-sdk/types.js +2 -0
- package/daemon/dist/agent-sdk/types.js.map +1 -0
- package/daemon/dist/automation-event-listener.d.ts +27 -0
- package/daemon/dist/automation-event-listener.d.ts.map +1 -0
- package/daemon/dist/automation-event-listener.js +342 -0
- package/daemon/dist/automation-event-listener.js.map +1 -0
- package/daemon/dist/automation-runner.d.ts +17 -0
- package/daemon/dist/automation-runner.d.ts.map +1 -0
- package/daemon/dist/automation-runner.js +592 -0
- package/daemon/dist/automation-runner.js.map +1 -0
- package/daemon/dist/autonomy/feedback-memory.d.ts +8 -0
- package/daemon/dist/autonomy/feedback-memory.d.ts.map +1 -0
- package/daemon/dist/autonomy/feedback-memory.js +23 -0
- package/daemon/dist/autonomy/feedback-memory.js.map +1 -0
- package/daemon/dist/autonomy/intent-router.d.ts +10 -0
- package/daemon/dist/autonomy/intent-router.d.ts.map +1 -0
- package/daemon/dist/autonomy/intent-router.js +72 -0
- package/daemon/dist/autonomy/intent-router.js.map +1 -0
- package/daemon/dist/autonomy/memory-rules.d.ts +35 -0
- package/daemon/dist/autonomy/memory-rules.d.ts.map +1 -0
- package/daemon/dist/autonomy/memory-rules.js +117 -0
- package/daemon/dist/autonomy/memory-rules.js.map +1 -0
- package/daemon/dist/autonomy/proof-packet.d.ts +17 -0
- package/daemon/dist/autonomy/proof-packet.d.ts.map +1 -0
- package/daemon/dist/autonomy/proof-packet.js +28 -0
- package/daemon/dist/autonomy/proof-packet.js.map +1 -0
- package/daemon/dist/autonomy/session-autonomy.d.ts +18 -0
- package/daemon/dist/autonomy/session-autonomy.d.ts.map +1 -0
- package/daemon/dist/autonomy/session-autonomy.js +71 -0
- package/daemon/dist/autonomy/session-autonomy.js.map +1 -0
- package/daemon/dist/autonomy/source-inventory.d.ts +3 -0
- package/daemon/dist/autonomy/source-inventory.d.ts.map +1 -0
- package/daemon/dist/autonomy/source-inventory.js +84 -0
- package/daemon/dist/autonomy/source-inventory.js.map +1 -0
- package/daemon/dist/autonomy/types.d.ts +20 -0
- package/daemon/dist/autonomy/types.d.ts.map +1 -0
- package/daemon/dist/autonomy/types.js +2 -0
- package/daemon/dist/autonomy/types.js.map +1 -0
- package/daemon/dist/bin/mcp-os.d.ts +3 -0
- package/daemon/dist/bin/mcp-os.d.ts.map +1 -0
- package/daemon/dist/bin/mcp-os.js +63 -0
- package/daemon/dist/bin/mcp-os.js.map +1 -0
- package/daemon/dist/bin/mcp-scheduler.d.ts +3 -0
- package/daemon/dist/bin/mcp-scheduler.d.ts.map +1 -0
- package/daemon/dist/bin/mcp-scheduler.js +90 -0
- package/daemon/dist/bin/mcp-scheduler.js.map +1 -0
- package/daemon/dist/bin/supen-sys.d.ts +3 -0
- package/daemon/dist/bin/supen-sys.d.ts.map +1 -0
- package/daemon/dist/bin/supen-sys.js +38 -0
- package/daemon/dist/bin/supen-sys.js.map +1 -0
- package/daemon/dist/bootstrap/hub-bootstrap.d.ts +2 -0
- package/daemon/dist/bootstrap/hub-bootstrap.d.ts.map +1 -0
- package/daemon/dist/bootstrap/hub-bootstrap.js +409 -0
- package/daemon/dist/bootstrap/hub-bootstrap.js.map +1 -0
- package/daemon/dist/bootstrap/skill-bootstrap.d.ts +2 -0
- package/daemon/dist/bootstrap/skill-bootstrap.d.ts.map +1 -0
- package/daemon/dist/bootstrap/skill-bootstrap.js +92 -0
- package/daemon/dist/bootstrap/skill-bootstrap.js.map +1 -0
- package/daemon/dist/channels/acp.d.ts +23 -0
- package/daemon/dist/channels/acp.d.ts.map +1 -0
- package/daemon/dist/channels/acp.js +916 -0
- package/daemon/dist/channels/acp.js.map +1 -0
- package/daemon/dist/channels/base.d.ts +38 -0
- package/daemon/dist/channels/base.d.ts.map +1 -0
- package/daemon/dist/channels/base.js +56 -0
- package/daemon/dist/channels/base.js.map +1 -0
- package/daemon/dist/channels/http-routes.d.ts +44 -0
- package/daemon/dist/channels/http-routes.d.ts.map +1 -0
- package/daemon/dist/channels/http-routes.js +688 -0
- package/daemon/dist/channels/http-routes.js.map +1 -0
- package/daemon/dist/channels/http.d.ts +16 -0
- package/daemon/dist/channels/http.d.ts.map +1 -0
- package/daemon/dist/channels/http.js +84 -0
- package/daemon/dist/channels/http.js.map +1 -0
- package/daemon/dist/channels/index.d.ts +3 -0
- package/daemon/dist/channels/index.d.ts.map +1 -0
- package/daemon/dist/channels/index.js +7 -0
- package/daemon/dist/channels/index.js.map +1 -0
- package/daemon/dist/channels/registry.d.ts +18 -0
- package/daemon/dist/channels/registry.d.ts.map +1 -0
- package/daemon/dist/channels/registry.js +11 -0
- package/daemon/dist/channels/registry.js.map +1 -0
- package/daemon/dist/commands/builtin.d.ts +7 -0
- package/daemon/dist/commands/builtin.d.ts.map +1 -0
- package/daemon/dist/commands/builtin.js +111 -0
- package/daemon/dist/commands/builtin.js.map +1 -0
- package/daemon/dist/commands/catalog.d.ts +47 -0
- package/daemon/dist/commands/catalog.d.ts.map +1 -0
- package/daemon/dist/commands/catalog.js +487 -0
- package/daemon/dist/commands/catalog.js.map +1 -0
- package/daemon/dist/core/app.d.ts +18 -0
- package/daemon/dist/core/app.d.ts.map +1 -0
- package/daemon/dist/core/app.js +49 -0
- package/daemon/dist/core/app.js.map +1 -0
- package/daemon/dist/core/automation-timing.d.ts +10 -0
- package/daemon/dist/core/automation-timing.d.ts.map +1 -0
- package/daemon/dist/core/automation-timing.js +211 -0
- package/daemon/dist/core/automation-timing.js.map +1 -0
- package/daemon/dist/core/codex-subscription.d.ts +8 -0
- package/daemon/dist/core/codex-subscription.d.ts.map +1 -0
- package/daemon/dist/core/codex-subscription.js +150 -0
- package/daemon/dist/core/codex-subscription.js.map +1 -0
- package/daemon/dist/core/command-hub.d.ts +22 -0
- package/daemon/dist/core/command-hub.d.ts.map +1 -0
- package/daemon/dist/core/command-hub.js +59 -0
- package/daemon/dist/core/command-hub.js.map +1 -0
- package/daemon/dist/core/config.d.ts +146 -0
- package/daemon/dist/core/config.d.ts.map +1 -0
- package/daemon/dist/core/config.js +663 -0
- package/daemon/dist/core/config.js.map +1 -0
- package/daemon/dist/core/control-commands.d.ts +17 -0
- package/daemon/dist/core/control-commands.d.ts.map +1 -0
- package/daemon/dist/core/control-commands.js +35 -0
- package/daemon/dist/core/control-commands.js.map +1 -0
- package/daemon/dist/core/control-log.d.ts +17 -0
- package/daemon/dist/core/control-log.d.ts.map +1 -0
- package/daemon/dist/core/control-log.js +67 -0
- package/daemon/dist/core/control-log.js.map +1 -0
- package/daemon/dist/core/cortex.d.ts +53 -0
- package/daemon/dist/core/cortex.d.ts.map +1 -0
- package/daemon/dist/core/cortex.js +1690 -0
- package/daemon/dist/core/cortex.js.map +1 -0
- package/daemon/dist/core/daemon-lock.d.ts +16 -0
- package/daemon/dist/core/daemon-lock.d.ts.map +1 -0
- package/daemon/dist/core/daemon-lock.js +285 -0
- package/daemon/dist/core/daemon-lock.js.map +1 -0
- package/daemon/dist/core/dispatcher.d.ts +42 -0
- package/daemon/dist/core/dispatcher.d.ts.map +1 -0
- package/daemon/dist/core/dispatcher.js +173 -0
- package/daemon/dist/core/dispatcher.js.map +1 -0
- package/daemon/dist/core/enrollment.d.ts +41 -0
- package/daemon/dist/core/enrollment.d.ts.map +1 -0
- package/daemon/dist/core/enrollment.js +195 -0
- package/daemon/dist/core/enrollment.js.map +1 -0
- package/daemon/dist/core/env.d.ts +109 -0
- package/daemon/dist/core/env.d.ts.map +1 -0
- package/daemon/dist/core/env.js +329 -0
- package/daemon/dist/core/env.js.map +1 -0
- package/daemon/dist/core/gateway-config.d.ts +16 -0
- package/daemon/dist/core/gateway-config.d.ts.map +1 -0
- package/daemon/dist/core/gateway-config.js +103 -0
- package/daemon/dist/core/gateway-config.js.map +1 -0
- package/daemon/dist/core/gateway-protocol.d.ts +80 -0
- package/daemon/dist/core/gateway-protocol.d.ts.map +1 -0
- package/daemon/dist/core/gateway-protocol.js +2 -0
- package/daemon/dist/core/gateway-protocol.js.map +1 -0
- package/daemon/dist/core/gateway-routing-config.d.ts +21 -0
- package/daemon/dist/core/gateway-routing-config.d.ts.map +1 -0
- package/daemon/dist/core/gateway-routing-config.js +56 -0
- package/daemon/dist/core/gateway-routing-config.js.map +1 -0
- package/daemon/dist/core/gateway.d.ts +124 -0
- package/daemon/dist/core/gateway.d.ts.map +1 -0
- package/daemon/dist/core/gateway.js +887 -0
- package/daemon/dist/core/gateway.js.map +1 -0
- package/daemon/dist/core/hub-snapshot.d.ts +42 -0
- package/daemon/dist/core/hub-snapshot.d.ts.map +1 -0
- package/daemon/dist/core/hub-snapshot.js +125 -0
- package/daemon/dist/core/hub-snapshot.js.map +1 -0
- package/daemon/dist/core/interrupts.d.ts +7 -0
- package/daemon/dist/core/interrupts.d.ts.map +1 -0
- package/daemon/dist/core/interrupts.js +28 -0
- package/daemon/dist/core/interrupts.js.map +1 -0
- package/daemon/dist/core/logger.d.ts +3 -0
- package/daemon/dist/core/logger.d.ts.map +1 -0
- package/daemon/dist/core/logger.js +91 -0
- package/daemon/dist/core/logger.js.map +1 -0
- package/daemon/dist/core/loop-guard.d.ts +27 -0
- package/daemon/dist/core/loop-guard.d.ts.map +1 -0
- package/daemon/dist/core/loop-guard.js +47 -0
- package/daemon/dist/core/loop-guard.js.map +1 -0
- package/daemon/dist/core/observable-logging.d.ts +43 -0
- package/daemon/dist/core/observable-logging.d.ts.map +1 -0
- package/daemon/dist/core/observable-logging.js +77 -0
- package/daemon/dist/core/observable-logging.js.map +1 -0
- package/daemon/dist/core/pairing.d.ts +51 -0
- package/daemon/dist/core/pairing.d.ts.map +1 -0
- package/daemon/dist/core/pairing.js +207 -0
- package/daemon/dist/core/pairing.js.map +1 -0
- package/daemon/dist/core/progress.d.ts +32 -0
- package/daemon/dist/core/progress.d.ts.map +1 -0
- package/daemon/dist/core/progress.js +145 -0
- package/daemon/dist/core/progress.js.map +1 -0
- package/daemon/dist/core/protocol-adapter.d.ts +14 -0
- package/daemon/dist/core/protocol-adapter.d.ts.map +1 -0
- package/daemon/dist/core/protocol-adapter.js +472 -0
- package/daemon/dist/core/protocol-adapter.js.map +1 -0
- package/daemon/dist/core/sdk-wrapper.d.ts +36 -0
- package/daemon/dist/core/sdk-wrapper.d.ts.map +1 -0
- package/daemon/dist/core/sdk-wrapper.js +533 -0
- package/daemon/dist/core/sdk-wrapper.js.map +1 -0
- package/daemon/dist/core/security.d.ts +10 -0
- package/daemon/dist/core/security.d.ts.map +1 -0
- package/daemon/dist/core/security.js +95 -0
- package/daemon/dist/core/security.js.map +1 -0
- package/daemon/dist/core/space-env.d.ts +15 -0
- package/daemon/dist/core/space-env.d.ts.map +1 -0
- package/daemon/dist/core/space-env.js +182 -0
- package/daemon/dist/core/space-env.js.map +1 -0
- package/daemon/dist/core/status-inspector.d.ts +31 -0
- package/daemon/dist/core/status-inspector.d.ts.map +1 -0
- package/daemon/dist/core/status-inspector.js +35 -0
- package/daemon/dist/core/status-inspector.js.map +1 -0
- package/daemon/dist/core/storage-paths.d.ts +30 -0
- package/daemon/dist/core/storage-paths.d.ts.map +1 -0
- package/daemon/dist/core/storage-paths.js +84 -0
- package/daemon/dist/core/storage-paths.js.map +1 -0
- package/daemon/dist/core/store.d.ts +256 -0
- package/daemon/dist/core/store.d.ts.map +1 -0
- package/daemon/dist/core/store.js +2956 -0
- package/daemon/dist/core/store.js.map +1 -0
- package/daemon/dist/core/streaming.d.ts +24 -0
- package/daemon/dist/core/streaming.d.ts.map +1 -0
- package/daemon/dist/core/streaming.js +57 -0
- package/daemon/dist/core/streaming.js.map +1 -0
- package/daemon/dist/core/task-artifacts.d.ts +17 -0
- package/daemon/dist/core/task-artifacts.d.ts.map +1 -0
- package/daemon/dist/core/task-artifacts.js +104 -0
- package/daemon/dist/core/task-artifacts.js.map +1 -0
- package/daemon/dist/core/thread-event-log.d.ts +54 -0
- package/daemon/dist/core/thread-event-log.d.ts.map +1 -0
- package/daemon/dist/core/thread-event-log.js +218 -0
- package/daemon/dist/core/thread-event-log.js.map +1 -0
- package/daemon/dist/core/thread-runtime-state.d.ts +53 -0
- package/daemon/dist/core/thread-runtime-state.d.ts.map +1 -0
- package/daemon/dist/core/thread-runtime-state.js +271 -0
- package/daemon/dist/core/thread-runtime-state.js.map +1 -0
- package/daemon/dist/core/types.d.ts +552 -0
- package/daemon/dist/core/types.d.ts.map +1 -0
- package/daemon/dist/core/types.js +2 -0
- package/daemon/dist/core/types.js.map +1 -0
- package/daemon/dist/core/utils.d.ts +5 -0
- package/daemon/dist/core/utils.d.ts.map +1 -0
- package/daemon/dist/core/utils.js +35 -0
- package/daemon/dist/core/utils.js.map +1 -0
- package/daemon/dist/http/command-catalog.d.ts +3 -0
- package/daemon/dist/http/command-catalog.d.ts.map +1 -0
- package/daemon/dist/http/command-catalog.js +2 -0
- package/daemon/dist/http/command-catalog.js.map +1 -0
- package/daemon/dist/http/context.d.ts +18 -0
- package/daemon/dist/http/context.d.ts.map +1 -0
- package/daemon/dist/http/context.js +121 -0
- package/daemon/dist/http/context.js.map +1 -0
- package/daemon/dist/http/office-preview.d.ts +10 -0
- package/daemon/dist/http/office-preview.d.ts.map +1 -0
- package/daemon/dist/http/office-preview.js +234 -0
- package/daemon/dist/http/office-preview.js.map +1 -0
- package/daemon/dist/http/response.d.ts +5 -0
- package/daemon/dist/http/response.d.ts.map +1 -0
- package/daemon/dist/http/response.js +37 -0
- package/daemon/dist/http/response.js.map +1 -0
- package/daemon/dist/http/router.d.ts +4 -0
- package/daemon/dist/http/router.d.ts.map +1 -0
- package/daemon/dist/http/router.js +57 -0
- package/daemon/dist/http/router.js.map +1 -0
- package/daemon/dist/http/routes/agents.d.ts +4 -0
- package/daemon/dist/http/routes/agents.d.ts.map +1 -0
- package/daemon/dist/http/routes/agents.js +747 -0
- package/daemon/dist/http/routes/agents.js.map +1 -0
- package/daemon/dist/http/routes/automations.d.ts +6 -0
- package/daemon/dist/http/routes/automations.d.ts.map +1 -0
- package/daemon/dist/http/routes/automations.js +530 -0
- package/daemon/dist/http/routes/automations.js.map +1 -0
- package/daemon/dist/http/routes/autonomy.d.ts +4 -0
- package/daemon/dist/http/routes/autonomy.d.ts.map +1 -0
- package/daemon/dist/http/routes/autonomy.js +78 -0
- package/daemon/dist/http/routes/autonomy.js.map +1 -0
- package/daemon/dist/http/routes/chat-input.d.ts +18 -0
- package/daemon/dist/http/routes/chat-input.d.ts.map +1 -0
- package/daemon/dist/http/routes/chat-input.js +122 -0
- package/daemon/dist/http/routes/chat-input.js.map +1 -0
- package/daemon/dist/http/routes/plugins.d.ts +5 -0
- package/daemon/dist/http/routes/plugins.d.ts.map +1 -0
- package/daemon/dist/http/routes/plugins.js +221 -0
- package/daemon/dist/http/routes/plugins.js.map +1 -0
- package/daemon/dist/http/routes/rpc.d.ts +28 -0
- package/daemon/dist/http/routes/rpc.d.ts.map +1 -0
- package/daemon/dist/http/routes/rpc.js +790 -0
- package/daemon/dist/http/routes/rpc.js.map +1 -0
- package/daemon/dist/http/routes/sessions.d.ts +11 -0
- package/daemon/dist/http/routes/sessions.d.ts.map +1 -0
- package/daemon/dist/http/routes/sessions.js +963 -0
- package/daemon/dist/http/routes/sessions.js.map +1 -0
- package/daemon/dist/http/routes/skills.d.ts +5 -0
- package/daemon/dist/http/routes/skills.d.ts.map +1 -0
- package/daemon/dist/http/routes/skills.js +420 -0
- package/daemon/dist/http/routes/skills.js.map +1 -0
- package/daemon/dist/http/routes/system.d.ts +64 -0
- package/daemon/dist/http/routes/system.d.ts.map +1 -0
- package/daemon/dist/http/routes/system.js +2676 -0
- package/daemon/dist/http/routes/system.js.map +1 -0
- package/daemon/dist/http/stream.d.ts +11 -0
- package/daemon/dist/http/stream.d.ts.map +1 -0
- package/daemon/dist/http/stream.js +100 -0
- package/daemon/dist/http/stream.js.map +1 -0
- package/daemon/dist/http/thread-stream.d.ts +10 -0
- package/daemon/dist/http/thread-stream.d.ts.map +1 -0
- package/daemon/dist/http/thread-stream.js +50 -0
- package/daemon/dist/http/thread-stream.js.map +1 -0
- package/daemon/dist/http/thread-title.d.ts +12 -0
- package/daemon/dist/http/thread-title.d.ts.map +1 -0
- package/daemon/dist/http/thread-title.js +122 -0
- package/daemon/dist/http/thread-title.js.map +1 -0
- package/daemon/dist/http/utils.d.ts +2 -0
- package/daemon/dist/http/utils.d.ts.map +1 -0
- package/daemon/dist/http/utils.js +18 -0
- package/daemon/dist/http/utils.js.map +1 -0
- package/daemon/dist/http/websocket.d.ts +5 -0
- package/daemon/dist/http/websocket.d.ts.map +1 -0
- package/daemon/dist/http/websocket.js +100 -0
- package/daemon/dist/http/websocket.js.map +1 -0
- package/daemon/dist/index.d.ts +35 -0
- package/daemon/dist/index.d.ts.map +1 -0
- package/daemon/dist/index.js +1582 -0
- package/daemon/dist/index.js.map +1 -0
- package/daemon/dist/mcp/aggregate-config.d.ts +16 -0
- package/daemon/dist/mcp/aggregate-config.d.ts.map +1 -0
- package/daemon/dist/mcp/aggregate-config.js +97 -0
- package/daemon/dist/mcp/aggregate-config.js.map +1 -0
- package/daemon/dist/mcp/client.d.ts +94 -0
- package/daemon/dist/mcp/client.d.ts.map +1 -0
- package/daemon/dist/mcp/client.js +207 -0
- package/daemon/dist/mcp/client.js.map +1 -0
- package/daemon/dist/mcp/default-servers.d.ts +35 -0
- package/daemon/dist/mcp/default-servers.d.ts.map +1 -0
- package/daemon/dist/mcp/default-servers.js +209 -0
- package/daemon/dist/mcp/default-servers.js.map +1 -0
- package/daemon/dist/mcp/gateway-client.d.ts +58 -0
- package/daemon/dist/mcp/gateway-client.d.ts.map +1 -0
- package/daemon/dist/mcp/gateway-client.js +181 -0
- package/daemon/dist/mcp/gateway-client.js.map +1 -0
- package/daemon/dist/mcp/index.d.ts +26 -0
- package/daemon/dist/mcp/index.d.ts.map +1 -0
- package/daemon/dist/mcp/index.js +50 -0
- package/daemon/dist/mcp/index.js.map +1 -0
- package/daemon/dist/mcp/settings.d.ts +3 -0
- package/daemon/dist/mcp/settings.d.ts.map +1 -0
- package/daemon/dist/mcp/settings.js +60 -0
- package/daemon/dist/mcp/settings.js.map +1 -0
- package/daemon/dist/mcp/tools.d.ts +21 -0
- package/daemon/dist/mcp/tools.d.ts.map +1 -0
- package/daemon/dist/mcp/tools.js +136 -0
- package/daemon/dist/mcp/tools.js.map +1 -0
- package/daemon/dist/plugins/catalog.d.ts +10 -0
- package/daemon/dist/plugins/catalog.d.ts.map +1 -0
- package/daemon/dist/plugins/catalog.js +304 -0
- package/daemon/dist/plugins/catalog.js.map +1 -0
- package/daemon/dist/plugins/hub.d.ts +42 -0
- package/daemon/dist/plugins/hub.d.ts.map +1 -0
- package/daemon/dist/plugins/hub.js +812 -0
- package/daemon/dist/plugins/hub.js.map +1 -0
- package/daemon/dist/plugins/types.d.ts +144 -0
- package/daemon/dist/plugins/types.d.ts.map +1 -0
- package/daemon/dist/plugins/types.js +2 -0
- package/daemon/dist/plugins/types.js.map +1 -0
- package/daemon/dist/router.d.ts +13 -0
- package/daemon/dist/router.d.ts.map +1 -0
- package/daemon/dist/router.js +43 -0
- package/daemon/dist/router.js.map +1 -0
- package/daemon/dist/skills/adapter.d.ts +4 -0
- package/daemon/dist/skills/adapter.d.ts.map +1 -0
- package/daemon/dist/skills/adapter.js +141 -0
- package/daemon/dist/skills/adapter.js.map +1 -0
- package/daemon/dist/skills/allowlist.d.ts +20 -0
- package/daemon/dist/skills/allowlist.d.ts.map +1 -0
- package/daemon/dist/skills/allowlist.js +52 -0
- package/daemon/dist/skills/allowlist.js.map +1 -0
- package/daemon/dist/skills/catalog.d.ts +26 -0
- package/daemon/dist/skills/catalog.d.ts.map +1 -0
- package/daemon/dist/skills/catalog.js +274 -0
- package/daemon/dist/skills/catalog.js.map +1 -0
- package/daemon/dist/skills/claude_code.d.ts +25 -0
- package/daemon/dist/skills/claude_code.d.ts.map +1 -0
- package/daemon/dist/skills/claude_code.js +49 -0
- package/daemon/dist/skills/claude_code.js.map +1 -0
- package/daemon/dist/skills/commands.d.ts +3 -0
- package/daemon/dist/skills/commands.d.ts.map +1 -0
- package/daemon/dist/skills/commands.js +689 -0
- package/daemon/dist/skills/commands.js.map +1 -0
- package/daemon/dist/skills/enabled.d.ts +7 -0
- package/daemon/dist/skills/enabled.d.ts.map +1 -0
- package/daemon/dist/skills/enabled.js +37 -0
- package/daemon/dist/skills/enabled.js.map +1 -0
- package/daemon/dist/skills/hub.d.ts +35 -0
- package/daemon/dist/skills/hub.d.ts.map +1 -0
- package/daemon/dist/skills/hub.js +574 -0
- package/daemon/dist/skills/hub.js.map +1 -0
- package/daemon/dist/skills/loader.d.ts +24 -0
- package/daemon/dist/skills/loader.d.ts.map +1 -0
- package/daemon/dist/skills/loader.js +693 -0
- package/daemon/dist/skills/loader.js.map +1 -0
- package/daemon/dist/skills/mcp-config.d.ts +8 -0
- package/daemon/dist/skills/mcp-config.d.ts.map +1 -0
- package/daemon/dist/skills/mcp-config.js +110 -0
- package/daemon/dist/skills/mcp-config.js.map +1 -0
- package/daemon/dist/skills/parser.d.ts +3 -0
- package/daemon/dist/skills/parser.d.ts.map +1 -0
- package/daemon/dist/skills/parser.js +279 -0
- package/daemon/dist/skills/parser.js.map +1 -0
- package/daemon/dist/skills/registry.d.ts +2 -0
- package/daemon/dist/skills/registry.d.ts.map +1 -0
- package/daemon/dist/skills/registry.js +2 -0
- package/daemon/dist/skills/registry.js.map +1 -0
- package/daemon/dist/skills/runtime-contract.d.ts +3 -0
- package/daemon/dist/skills/runtime-contract.d.ts.map +1 -0
- package/daemon/dist/skills/runtime-contract.js +92 -0
- package/daemon/dist/skills/runtime-contract.js.map +1 -0
- package/daemon/dist/skills/runtime.d.ts +28 -0
- package/daemon/dist/skills/runtime.d.ts.map +1 -0
- package/daemon/dist/skills/runtime.js +286 -0
- package/daemon/dist/skills/runtime.js.map +1 -0
- package/daemon/dist/skills/types.d.ts +228 -0
- package/daemon/dist/skills/types.d.ts.map +1 -0
- package/daemon/dist/skills/types.js +2 -0
- package/daemon/dist/skills/types.js.map +1 -0
- package/daemon/dist/start.d.ts +6 -0
- package/daemon/dist/start.d.ts.map +1 -0
- package/daemon/dist/start.js +19 -0
- package/daemon/dist/start.js.map +1 -0
- package/daemon/dist/sub-agent.d.ts +46 -0
- package/daemon/dist/sub-agent.d.ts.map +1 -0
- package/daemon/dist/sub-agent.js +120 -0
- package/daemon/dist/sub-agent.js.map +1 -0
- package/daemon/dist/sync/supabase-sync.d.ts +16 -0
- package/daemon/dist/sync/supabase-sync.d.ts.map +1 -0
- package/daemon/dist/sync/supabase-sync.js +240 -0
- package/daemon/dist/sync/supabase-sync.js.map +1 -0
- package/daemon/dist/task-executor.d.ts +6 -0
- package/daemon/dist/task-executor.d.ts.map +1 -0
- package/daemon/dist/task-executor.js +31 -0
- package/daemon/dist/task-executor.js.map +1 -0
- package/daemon/dist/tools/automations.d.ts +57 -0
- package/daemon/dist/tools/automations.d.ts.map +1 -0
- package/daemon/dist/tools/automations.js +94 -0
- package/daemon/dist/tools/automations.js.map +1 -0
- package/daemon/dist/tools/built-ins.d.ts +112 -0
- package/daemon/dist/tools/built-ins.d.ts.map +1 -0
- package/daemon/dist/tools/built-ins.js +251 -0
- package/daemon/dist/tools/built-ins.js.map +1 -0
- package/daemon/dist/tools/index.d.ts +287 -0
- package/daemon/dist/tools/index.d.ts.map +1 -0
- package/daemon/dist/tools/index.js +86 -0
- package/daemon/dist/tools/index.js.map +1 -0
- package/daemon/dist/tools/shell.d.ts +15 -0
- package/daemon/dist/tools/shell.d.ts.map +1 -0
- package/daemon/dist/tools/shell.js +46 -0
- package/daemon/dist/tools/shell.js.map +1 -0
- package/daemon/dist/tools/skill-tools.d.ts +23 -0
- package/daemon/dist/tools/skill-tools.d.ts.map +1 -0
- package/daemon/dist/tools/skill-tools.js +64 -0
- package/daemon/dist/tools/skill-tools.js.map +1 -0
- package/daemon/dist/tools/system.d.ts +36 -0
- package/daemon/dist/tools/system.d.ts.map +1 -0
- package/daemon/dist/tools/system.js +54 -0
- package/daemon/dist/tools/system.js.map +1 -0
- package/daemon/dist/tools/types.d.ts +11 -0
- package/daemon/dist/tools/types.d.ts.map +1 -0
- package/daemon/dist/tools/types.js +2 -0
- package/daemon/dist/tools/types.js.map +1 -0
- package/daemon/scripts/browser-smoke.mjs +125 -0
- package/daemon/scripts/supen-daemon.js +15 -0
- package/dist/agent.d.ts +11 -0
- package/dist/agent.js +159 -0
- package/dist/agent.js.map +1 -0
- package/dist/auth/login.d.ts +20 -0
- package/dist/auth/login.js +151 -0
- package/dist/auth/login.js.map +1 -0
- package/dist/auth/logout.d.ts +5 -0
- package/dist/auth/logout.js +19 -0
- package/dist/auth/logout.js.map +1 -0
- package/dist/auth/store.d.ts +37 -0
- package/dist/auth/store.js +80 -0
- package/dist/auth/store.js.map +1 -0
- package/dist/auth/whoami.d.ts +5 -0
- package/dist/auth/whoami.js +24 -0
- package/dist/auth/whoami.js.map +1 -0
- package/dist/backend.d.ts +8 -0
- package/dist/backend.js +148 -0
- package/dist/backend.js.map +1 -0
- package/dist/bootstrap.d.ts +13 -0
- package/dist/bootstrap.js +230 -0
- package/dist/bootstrap.js.map +1 -0
- package/dist/chat.d.ts +13 -0
- package/dist/chat.js +255 -0
- package/dist/chat.js.map +1 -0
- package/dist/commands.d.ts +48 -0
- package/dist/commands.js +273 -0
- package/dist/commands.js.map +1 -0
- package/dist/computer.d.ts +2 -0
- package/dist/computer.js +510 -0
- package/dist/computer.js.map +1 -0
- package/dist/config.d.ts +11 -0
- package/dist/config.js +149 -0
- package/dist/config.js.map +1 -0
- package/dist/daemon-manage.d.ts +29 -0
- package/dist/daemon-manage.js +255 -0
- package/dist/daemon-manage.js.map +1 -0
- package/dist/daemon.d.ts +15 -0
- package/dist/daemon.js +205 -0
- package/dist/daemon.js.map +1 -0
- package/dist/doctor.d.ts +13 -0
- package/dist/doctor.js +323 -0
- package/dist/doctor.js.map +1 -0
- package/dist/enroll.d.ts +7 -0
- package/dist/enroll.js +154 -0
- package/dist/enroll.js.map +1 -0
- package/dist/env.d.ts +10 -0
- package/dist/env.js +363 -0
- package/dist/env.js.map +1 -0
- package/dist/index.d.ts +54 -0
- package/dist/index.js +141 -0
- package/dist/index.js.map +1 -0
- package/dist/knowledge.d.ts +3 -0
- package/dist/knowledge.js +1581 -0
- package/dist/knowledge.js.map +1 -0
- package/dist/mcp.d.ts +11 -0
- package/dist/mcp.js +137 -0
- package/dist/mcp.js.map +1 -0
- package/dist/model.d.ts +8 -0
- package/dist/model.js +192 -0
- package/dist/model.js.map +1 -0
- package/dist/pairing.d.ts +21 -0
- package/dist/pairing.js +376 -0
- package/dist/pairing.js.map +1 -0
- package/dist/repl-events.d.ts +60 -0
- package/dist/repl-events.js +89 -0
- package/dist/repl-events.js.map +1 -0
- package/dist/repl-renderer.d.ts +37 -0
- package/dist/repl-renderer.js +140 -0
- package/dist/repl-renderer.js.map +1 -0
- package/dist/repl.d.ts +52 -0
- package/dist/repl.js +624 -0
- package/dist/repl.js.map +1 -0
- package/dist/service.d.ts +8 -0
- package/dist/service.js +238 -0
- package/dist/service.js.map +1 -0
- package/dist/skills.d.ts +14 -0
- package/dist/skills.js +423 -0
- package/dist/skills.js.map +1 -0
- package/dist/sse.d.ts +15 -0
- package/dist/sse.js +166 -0
- package/dist/sse.js.map +1 -0
- package/dist/thread.d.ts +9 -0
- package/dist/thread.js +152 -0
- package/dist/thread.js.map +1 -0
- package/dist/transport/computer-api.d.ts +1 -0
- package/dist/transport/computer-api.js +20 -0
- package/dist/transport/computer-api.js.map +1 -0
- package/dist/transport/gateway.d.ts +23 -0
- package/dist/transport/gateway.js +161 -0
- package/dist/transport/gateway.js.map +1 -0
- package/dist/transport/index.d.ts +27 -0
- package/dist/transport/index.js +77 -0
- package/dist/transport/index.js.map +1 -0
- package/dist/transport/local.d.ts +20 -0
- package/dist/transport/local.js +138 -0
- package/dist/transport/local.js.map +1 -0
- package/dist/transport/types.d.ts +42 -0
- package/dist/transport/types.js +11 -0
- package/dist/transport/types.js.map +1 -0
- package/dist/ui/app.d.ts +7 -0
- package/dist/ui/app.js +192 -0
- package/dist/ui/app.js.map +1 -0
- package/dist/ui/history-item.d.ts +9 -0
- package/dist/ui/history-item.js +35 -0
- package/dist/ui/history-item.js.map +1 -0
- package/dist/ui/input-bar.d.ts +17 -0
- package/dist/ui/input-bar.js +67 -0
- package/dist/ui/input-bar.js.map +1 -0
- package/dist/ui/streaming-view.d.ts +6 -0
- package/dist/ui/streaming-view.js +6 -0
- package/dist/ui/streaming-view.js.map +1 -0
- package/dist/ui/thread-input-history.d.ts +18 -0
- package/dist/ui/thread-input-history.js +67 -0
- package/dist/ui/thread-input-history.js.map +1 -0
- package/dist/utils.d.ts +17 -0
- package/dist/utils.js +80 -0
- package/dist/utils.js.map +1 -0
- package/package.json +55 -0
|
@@ -0,0 +1,887 @@
|
|
|
1
|
+
import WebSocket from 'ws';
|
|
2
|
+
import http from 'http';
|
|
3
|
+
import crypto from 'crypto';
|
|
4
|
+
import os from 'os';
|
|
5
|
+
import fs from 'fs';
|
|
6
|
+
import { Agent } from 'undici';
|
|
7
|
+
import { logger } from './logger.js';
|
|
8
|
+
import { readGatewayConfig } from './gateway-config.js';
|
|
9
|
+
import { assertSafePathSegment, validateOutboundEndpoint } from './security.js';
|
|
10
|
+
import { readEnrollmentState, verifyEnrollmentToken } from './enrollment.js';
|
|
11
|
+
import { DAEMON_HOSTNAME, HTTP_API_KEY, HTTP_PORT, SUPEN_HOME } from './config.js';
|
|
12
|
+
import { getSessionForAgent, resolveFromChatJid, } from './store.js';
|
|
13
|
+
// Direct dispatcher that bypasses any http_proxy/https_proxy env vars.
|
|
14
|
+
// Without this, Node.js global fetch routes loopback traffic through the
|
|
15
|
+
// system proxy, causing 504 Gateway Timeouts on every API relay.
|
|
16
|
+
const directAgent = new Agent();
|
|
17
|
+
const GATEWAY_JID_PREFIX = 'gateway:';
|
|
18
|
+
const DEFAULT_CONNECT_TIMEOUT_MS = 15_000;
|
|
19
|
+
const DEFAULT_HEARTBEAT_INTERVAL_MS = 30_000;
|
|
20
|
+
const DEFAULT_HEARTBEAT_TIMEOUT_MS = 90_000;
|
|
21
|
+
function gatewayConnectTimeoutMs() {
|
|
22
|
+
const parsed = Number.parseInt(process.env.SUPEN_GATEWAY_CONNECT_TIMEOUT_MS || '', 10);
|
|
23
|
+
if (Number.isFinite(parsed) && parsed > 0)
|
|
24
|
+
return parsed;
|
|
25
|
+
return DEFAULT_CONNECT_TIMEOUT_MS;
|
|
26
|
+
}
|
|
27
|
+
function gatewayHeartbeatIntervalMs() {
|
|
28
|
+
const parsed = Number.parseInt(process.env.SUPEN_GATEWAY_HEARTBEAT_INTERVAL_MS || '', 10);
|
|
29
|
+
if (Number.isFinite(parsed) && parsed > 0)
|
|
30
|
+
return parsed;
|
|
31
|
+
return DEFAULT_HEARTBEAT_INTERVAL_MS;
|
|
32
|
+
}
|
|
33
|
+
function gatewayHeartbeatTimeoutMs() {
|
|
34
|
+
const parsed = Number.parseInt(process.env.SUPEN_GATEWAY_HEARTBEAT_TIMEOUT_MS || '', 10);
|
|
35
|
+
if (Number.isFinite(parsed) && parsed > 0)
|
|
36
|
+
return parsed;
|
|
37
|
+
return DEFAULT_HEARTBEAT_TIMEOUT_MS;
|
|
38
|
+
}
|
|
39
|
+
export function parseGatewaySseFrame(frame) {
|
|
40
|
+
const dataLines = [];
|
|
41
|
+
let id;
|
|
42
|
+
for (const line of frame.split(/\r?\n/)) {
|
|
43
|
+
if (!line || line.startsWith(':'))
|
|
44
|
+
continue;
|
|
45
|
+
if (line.startsWith('id:')) {
|
|
46
|
+
id = line.slice(3).trimStart();
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
if (line.startsWith('data:')) {
|
|
50
|
+
dataLines.push(line.slice(5).trimStart());
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
if (dataLines.length === 0)
|
|
54
|
+
return null;
|
|
55
|
+
return {
|
|
56
|
+
...(id !== undefined ? { id } : {}),
|
|
57
|
+
data: dataLines.join('\n'),
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
function normalizeGatewayThreadId(rawThreadId) {
|
|
61
|
+
const candidate = typeof rawThreadId === 'string' ? rawThreadId.trim() : '';
|
|
62
|
+
if (!candidate) {
|
|
63
|
+
return `gwcs_${crypto.randomBytes(20).toString('hex')}`;
|
|
64
|
+
}
|
|
65
|
+
try {
|
|
66
|
+
return assertSafePathSegment(candidate, 'thread_id');
|
|
67
|
+
}
|
|
68
|
+
catch {
|
|
69
|
+
return `gwcs_${crypto.createHash('sha1').update(candidate).digest('hex')}`;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Gateway — the daemon's outbound uplink to the Supen Gateway.
|
|
74
|
+
*
|
|
75
|
+
* Architecture:
|
|
76
|
+
* - Daemons connect outward to the gateway and receive instructions from it.
|
|
77
|
+
* - The gateway accepts connections from controller platforms (Supen, etc.)
|
|
78
|
+
* that drive the daemons via channels (Discord, HTTP, ACP, …).
|
|
79
|
+
*
|
|
80
|
+
* This is core infrastructure, not a consumer channel.
|
|
81
|
+
* Instantiated directly in index.ts, not via the channel registry.
|
|
82
|
+
*/
|
|
83
|
+
export class Gateway {
|
|
84
|
+
ws = null;
|
|
85
|
+
_connected = false;
|
|
86
|
+
config;
|
|
87
|
+
callbacks;
|
|
88
|
+
reconnectTimeout = null;
|
|
89
|
+
reportingInterval = null;
|
|
90
|
+
connectionTimeout = null;
|
|
91
|
+
heartbeatInterval = null;
|
|
92
|
+
heartbeatTimeout = null;
|
|
93
|
+
activeSseSubscriptions = new Map();
|
|
94
|
+
llmToken = null;
|
|
95
|
+
llmTokenExpiresAt = null;
|
|
96
|
+
tokenRenewalTimeout = null;
|
|
97
|
+
intentionalShutdown = false;
|
|
98
|
+
socketCloseHandled = false;
|
|
99
|
+
channelReplySessions = new Map();
|
|
100
|
+
pendingReplyStarts = new Map();
|
|
101
|
+
constructor(callbacks) {
|
|
102
|
+
this.callbacks = callbacks;
|
|
103
|
+
this.config = readGatewayConfig();
|
|
104
|
+
}
|
|
105
|
+
getConfig() {
|
|
106
|
+
return { ...this.config };
|
|
107
|
+
}
|
|
108
|
+
async reloadConfigAndReconnect() {
|
|
109
|
+
await this.disconnect();
|
|
110
|
+
this.config = readGatewayConfig();
|
|
111
|
+
gatewayConfigured = !!this.config.gateway_url;
|
|
112
|
+
await this.connect();
|
|
113
|
+
}
|
|
114
|
+
async connect() {
|
|
115
|
+
if (!this.config.gateway_url) {
|
|
116
|
+
logger.debug('No gateway_url configured, Gateway uplink disabled');
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
this.intentionalShutdown = false;
|
|
120
|
+
await this.initiateConnection();
|
|
121
|
+
}
|
|
122
|
+
async initiateConnection() {
|
|
123
|
+
if (this.reconnectTimeout)
|
|
124
|
+
clearTimeout(this.reconnectTimeout);
|
|
125
|
+
if (this.connectionTimeout)
|
|
126
|
+
clearTimeout(this.connectionTimeout);
|
|
127
|
+
this.stopHeartbeat();
|
|
128
|
+
this.socketCloseHandled = false;
|
|
129
|
+
const rawUrl = this.config.gateway_url;
|
|
130
|
+
let url;
|
|
131
|
+
try {
|
|
132
|
+
url = validateOutboundEndpoint(rawUrl, {
|
|
133
|
+
allowedProtocols: ['ws:', 'wss:'],
|
|
134
|
+
label: 'gateway_url',
|
|
135
|
+
}).toString();
|
|
136
|
+
}
|
|
137
|
+
catch (err) {
|
|
138
|
+
logger.error({ err: err.message, gateway_url: rawUrl }, 'Refusing to connect to gateway: untrusted endpoint');
|
|
139
|
+
this._connected = false;
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
logger.info({ url }, 'Connecting to gateway...');
|
|
143
|
+
this.ws = new WebSocket(url);
|
|
144
|
+
this.connectionTimeout = setTimeout(() => {
|
|
145
|
+
if (this.ws && this.ws.readyState !== WebSocket.OPEN) {
|
|
146
|
+
logger.warn({ url, timeout_ms: gatewayConnectTimeoutMs() }, 'Gateway connection timed out');
|
|
147
|
+
this.ws.terminate();
|
|
148
|
+
}
|
|
149
|
+
}, gatewayConnectTimeoutMs());
|
|
150
|
+
this.connectionTimeout.unref?.();
|
|
151
|
+
this.ws.on('open', () => {
|
|
152
|
+
if (this.connectionTimeout) {
|
|
153
|
+
clearTimeout(this.connectionTimeout);
|
|
154
|
+
this.connectionTimeout = null;
|
|
155
|
+
}
|
|
156
|
+
logger.info({ url }, 'Connected to gateway');
|
|
157
|
+
this._connected = true;
|
|
158
|
+
this.authenticate();
|
|
159
|
+
this.startHeartbeat();
|
|
160
|
+
this.startReporting();
|
|
161
|
+
});
|
|
162
|
+
this.ws.on('message', (data) => this.handleMessage(data));
|
|
163
|
+
this.ws.on('close', () => {
|
|
164
|
+
if (this.connectionTimeout) {
|
|
165
|
+
clearTimeout(this.connectionTimeout);
|
|
166
|
+
this.connectionTimeout = null;
|
|
167
|
+
}
|
|
168
|
+
this.handleSocketClosed('Gateway disconnected');
|
|
169
|
+
});
|
|
170
|
+
this.ws.on('error', (err) => {
|
|
171
|
+
logger.error({ err }, 'Gateway connection error');
|
|
172
|
+
if (this.ws && this.ws.readyState === WebSocket.CONNECTING) {
|
|
173
|
+
this.ws.terminate();
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
authenticate() {
|
|
178
|
+
const state = readEnrollmentState(DAEMON_HOSTNAME || undefined);
|
|
179
|
+
const gatewayToken = this.buildGatewayToken(state.daemon_id);
|
|
180
|
+
if (this.config.trust_token && state.trust_state !== 'trusted') {
|
|
181
|
+
logger.info('Attempting enrollment with gateway using trust_token');
|
|
182
|
+
this.ws?.send(JSON.stringify({
|
|
183
|
+
type: 'enroll',
|
|
184
|
+
token: this.config.trust_token,
|
|
185
|
+
gateway_token: gatewayToken,
|
|
186
|
+
daemon_id: state.daemon_id,
|
|
187
|
+
daemon_fingerprint: state.daemon_fingerprint,
|
|
188
|
+
}));
|
|
189
|
+
}
|
|
190
|
+
else {
|
|
191
|
+
this.ws?.send(JSON.stringify({
|
|
192
|
+
type: 'auth',
|
|
193
|
+
token: gatewayToken,
|
|
194
|
+
daemon_id: state.daemon_id,
|
|
195
|
+
daemon_fingerprint: state.daemon_fingerprint,
|
|
196
|
+
}));
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Build a HMAC token for gateway authentication.
|
|
201
|
+
* Format: `${daemonId}.${timestampMs}.${hmacHex}`
|
|
202
|
+
* Only when SUPEN_GATEWAY_SECRET env var is set on the daemon side.
|
|
203
|
+
*/
|
|
204
|
+
buildGatewayToken(daemonId) {
|
|
205
|
+
const secret = process.env.SUPEN_GATEWAY_SECRET;
|
|
206
|
+
if (!secret)
|
|
207
|
+
return undefined;
|
|
208
|
+
const ts = Date.now().toString();
|
|
209
|
+
const hmac = crypto.createHmac('sha256', secret).update(`${daemonId}:${ts}`).digest('hex');
|
|
210
|
+
return `${daemonId}.${ts}.${hmac}`;
|
|
211
|
+
}
|
|
212
|
+
startReporting() {
|
|
213
|
+
this.stopReporting();
|
|
214
|
+
const interval = this.config.reporting_interval || 60000;
|
|
215
|
+
this.reportingInterval = setInterval(() => {
|
|
216
|
+
if (this._connected && this.ws?.readyState === WebSocket.OPEN) {
|
|
217
|
+
const state = readEnrollmentState(DAEMON_HOSTNAME || undefined);
|
|
218
|
+
// System Telemetry
|
|
219
|
+
const cpus = os.cpus();
|
|
220
|
+
const memTotal = os.totalmem();
|
|
221
|
+
const memFree = os.freemem();
|
|
222
|
+
let disk_total;
|
|
223
|
+
let disk_free;
|
|
224
|
+
let disk_used;
|
|
225
|
+
try {
|
|
226
|
+
const stats = fs.statfsSync(SUPEN_HOME);
|
|
227
|
+
disk_total = stats.bsize * stats.blocks;
|
|
228
|
+
disk_free = stats.bsize * stats.bavail;
|
|
229
|
+
disk_used = disk_total - disk_free;
|
|
230
|
+
}
|
|
231
|
+
catch { /* ignore */ }
|
|
232
|
+
this.ws.send(JSON.stringify({
|
|
233
|
+
type: 'report',
|
|
234
|
+
status: 'online',
|
|
235
|
+
trust_state: state.trust_state,
|
|
236
|
+
timestamp: new Date().toISOString(),
|
|
237
|
+
telemetry: {
|
|
238
|
+
os: {
|
|
239
|
+
platform: os.platform(),
|
|
240
|
+
arch: os.arch(),
|
|
241
|
+
cpus: cpus.length,
|
|
242
|
+
cpu_model: cpus[0]?.model || 'Unknown',
|
|
243
|
+
load_avg: os.loadavg(),
|
|
244
|
+
mem_total: memTotal,
|
|
245
|
+
mem_free: memFree,
|
|
246
|
+
mem_used: memTotal - memFree,
|
|
247
|
+
uptime: os.uptime(),
|
|
248
|
+
disk_total,
|
|
249
|
+
disk_free,
|
|
250
|
+
disk_used,
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
}));
|
|
254
|
+
}
|
|
255
|
+
}, interval);
|
|
256
|
+
this.reportingInterval.unref?.();
|
|
257
|
+
}
|
|
258
|
+
stopReporting() {
|
|
259
|
+
if (this.reportingInterval) {
|
|
260
|
+
clearInterval(this.reportingInterval);
|
|
261
|
+
this.reportingInterval = null;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
startHeartbeat() {
|
|
265
|
+
this.stopHeartbeat();
|
|
266
|
+
const intervalMs = gatewayHeartbeatIntervalMs();
|
|
267
|
+
const timeoutMs = gatewayHeartbeatTimeoutMs();
|
|
268
|
+
const clearHeartbeatTimeout = () => {
|
|
269
|
+
if (this.heartbeatTimeout) {
|
|
270
|
+
clearTimeout(this.heartbeatTimeout);
|
|
271
|
+
this.heartbeatTimeout = null;
|
|
272
|
+
}
|
|
273
|
+
};
|
|
274
|
+
const armHeartbeatTimeout = () => {
|
|
275
|
+
clearHeartbeatTimeout();
|
|
276
|
+
this.heartbeatTimeout = setTimeout(() => {
|
|
277
|
+
if (this._connected && this.ws?.readyState === WebSocket.OPEN) {
|
|
278
|
+
logger.warn({ timeout_ms: timeoutMs }, 'Gateway heartbeat timed out; terminating stale socket');
|
|
279
|
+
this.ws.terminate();
|
|
280
|
+
}
|
|
281
|
+
}, timeoutMs);
|
|
282
|
+
this.heartbeatTimeout.unref?.();
|
|
283
|
+
};
|
|
284
|
+
this.ws?.on('pong', clearHeartbeatTimeout);
|
|
285
|
+
this.ws?.on('ping', clearHeartbeatTimeout);
|
|
286
|
+
this.heartbeatInterval = setInterval(() => {
|
|
287
|
+
if (!this._connected || !this.ws || this.ws.readyState !== WebSocket.OPEN)
|
|
288
|
+
return;
|
|
289
|
+
if (this.heartbeatTimeout)
|
|
290
|
+
return;
|
|
291
|
+
try {
|
|
292
|
+
this.ws.ping();
|
|
293
|
+
armHeartbeatTimeout();
|
|
294
|
+
}
|
|
295
|
+
catch (err) {
|
|
296
|
+
logger.warn({ err }, 'Gateway heartbeat ping failed; terminating stale socket');
|
|
297
|
+
this.ws.terminate();
|
|
298
|
+
}
|
|
299
|
+
}, intervalMs);
|
|
300
|
+
this.heartbeatInterval.unref?.();
|
|
301
|
+
}
|
|
302
|
+
stopHeartbeat() {
|
|
303
|
+
if (this.heartbeatInterval) {
|
|
304
|
+
clearInterval(this.heartbeatInterval);
|
|
305
|
+
this.heartbeatInterval = null;
|
|
306
|
+
}
|
|
307
|
+
if (this.heartbeatTimeout) {
|
|
308
|
+
clearTimeout(this.heartbeatTimeout);
|
|
309
|
+
this.heartbeatTimeout = null;
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
rejectPendingReplyStarts(reason) {
|
|
313
|
+
for (const [requestId, pending] of this.pendingReplyStarts.entries()) {
|
|
314
|
+
clearTimeout(pending.timeout);
|
|
315
|
+
pending.reject(new Error(reason));
|
|
316
|
+
this.pendingReplyStarts.delete(requestId);
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
handleSocketClosed(reason) {
|
|
320
|
+
if (this.socketCloseHandled)
|
|
321
|
+
return;
|
|
322
|
+
this.socketCloseHandled = true;
|
|
323
|
+
this._connected = false;
|
|
324
|
+
this.rejectPendingReplyStarts(reason);
|
|
325
|
+
// Clear stale token — server revokes it on disconnect, so holding it
|
|
326
|
+
// would cause 401 errors on any LLM request before the new token arrives.
|
|
327
|
+
this.llmToken = null;
|
|
328
|
+
this.llmTokenExpiresAt = null;
|
|
329
|
+
if (this.tokenRenewalTimeout) {
|
|
330
|
+
clearTimeout(this.tokenRenewalTimeout);
|
|
331
|
+
this.tokenRenewalTimeout = null;
|
|
332
|
+
}
|
|
333
|
+
this.stopReporting();
|
|
334
|
+
this.stopHeartbeat();
|
|
335
|
+
if (this.intentionalShutdown) {
|
|
336
|
+
this.intentionalShutdown = false;
|
|
337
|
+
return;
|
|
338
|
+
}
|
|
339
|
+
if (this.reconnectTimeout)
|
|
340
|
+
return;
|
|
341
|
+
logger.warn('Gateway connection closed, reconnecting in 5s...');
|
|
342
|
+
this.reconnectTimeout = setTimeout(() => this.initiateConnection(), 5000);
|
|
343
|
+
}
|
|
344
|
+
handleMessage(data) {
|
|
345
|
+
try {
|
|
346
|
+
const payload = JSON.parse(data.toString());
|
|
347
|
+
logger.debug({ payload }, 'Received message from gateway');
|
|
348
|
+
if (payload.type === 'enrollment_result') {
|
|
349
|
+
if (payload.ok) {
|
|
350
|
+
logger.info('Gateway enrollment successful');
|
|
351
|
+
verifyEnrollmentToken({
|
|
352
|
+
token: this.config.trust_token,
|
|
353
|
+
daemonFingerprint: payload.daemon_fingerprint,
|
|
354
|
+
daemonId: DAEMON_HOSTNAME || undefined,
|
|
355
|
+
});
|
|
356
|
+
// Notify caller so it can send configure_channels
|
|
357
|
+
this.callbacks.onEnrollmentSuccess?.(payload.daemon_id);
|
|
358
|
+
}
|
|
359
|
+
else {
|
|
360
|
+
logger.error({ code: payload.code }, 'Gateway enrollment failed');
|
|
361
|
+
}
|
|
362
|
+
return;
|
|
363
|
+
}
|
|
364
|
+
if (payload.type === 'auth_result') {
|
|
365
|
+
if (payload.ok) {
|
|
366
|
+
const daemonId = (typeof payload.daemon_id === 'string' && payload.daemon_id.trim()) ||
|
|
367
|
+
readEnrollmentState(DAEMON_HOSTNAME || undefined).daemon_id;
|
|
368
|
+
logger.info('Gateway authentication successful');
|
|
369
|
+
// Notify caller so it can send configure_channels after reconnect/auth.
|
|
370
|
+
this.callbacks.onEnrollmentSuccess?.(daemonId);
|
|
371
|
+
}
|
|
372
|
+
else {
|
|
373
|
+
logger.error({ code: payload.code }, 'Gateway authentication failed');
|
|
374
|
+
}
|
|
375
|
+
return;
|
|
376
|
+
}
|
|
377
|
+
if (payload.type === 'message') {
|
|
378
|
+
const { agent_id, thread_id, content, sender, sender_name, task_id } = payload;
|
|
379
|
+
const normalizedThreadId = normalizeGatewayThreadId(thread_id);
|
|
380
|
+
const chatJid = `${GATEWAY_JID_PREFIX}${agent_id}:${normalizedThreadId}`;
|
|
381
|
+
const msg = {
|
|
382
|
+
id: payload.id || `gateway-${Date.now()}-${Math.random().toString(36).slice(2, 6)}`,
|
|
383
|
+
chat_jid: chatJid,
|
|
384
|
+
sender: sender || 'gateway-user',
|
|
385
|
+
sender_name: sender_name || 'Gateway User',
|
|
386
|
+
content,
|
|
387
|
+
timestamp: payload.timestamp || new Date().toISOString(),
|
|
388
|
+
is_from_me: false,
|
|
389
|
+
agent_id,
|
|
390
|
+
session_id: normalizedThreadId,
|
|
391
|
+
task_id: task_id || `gateway-task-${Date.now()}`,
|
|
392
|
+
};
|
|
393
|
+
this.callbacks.onChatMetadata(chatJid, msg.timestamp, agent_id, 'gateway', false);
|
|
394
|
+
this.callbacks.onMessage(chatJid, msg);
|
|
395
|
+
return;
|
|
396
|
+
}
|
|
397
|
+
if (payload.type === 'channel_message') {
|
|
398
|
+
const { trace_id, channel, jid: channelSession, sender, sender_name, content, timestamp, agent_id, task_id, session_id, thread_id, metadata, } = payload;
|
|
399
|
+
const normalizedMetadata = {
|
|
400
|
+
...(metadata || {}),
|
|
401
|
+
channel,
|
|
402
|
+
channel_thread: channelSession,
|
|
403
|
+
...(trace_id ? { trace_id } : {}),
|
|
404
|
+
};
|
|
405
|
+
const normalizedThreadId = normalizeGatewayThreadId(thread_id || task_id || session_id || channelSession);
|
|
406
|
+
const chatJid = `${GATEWAY_JID_PREFIX}${agent_id}:${normalizedThreadId}`;
|
|
407
|
+
const normalizedTaskId = task_id || session_id || thread_id || normalizedThreadId;
|
|
408
|
+
const msg = {
|
|
409
|
+
id: payload.id || `gateway-${Date.now()}-${Math.random().toString(36).slice(2, 6)}`,
|
|
410
|
+
chat_jid: chatJid,
|
|
411
|
+
sender: sender || 'channel-user',
|
|
412
|
+
sender_name: sender_name || 'Channel User',
|
|
413
|
+
content,
|
|
414
|
+
timestamp: timestamp || new Date().toISOString(),
|
|
415
|
+
is_from_me: false,
|
|
416
|
+
agent_id,
|
|
417
|
+
session_id: normalizedThreadId,
|
|
418
|
+
task_id: normalizedTaskId,
|
|
419
|
+
metadata: normalizedMetadata,
|
|
420
|
+
};
|
|
421
|
+
this.channelReplySessions.set(chatJid, {
|
|
422
|
+
channel,
|
|
423
|
+
channelSession,
|
|
424
|
+
traceId: typeof trace_id === 'string' && trace_id.trim() ? trace_id.trim() : undefined,
|
|
425
|
+
});
|
|
426
|
+
this.callbacks.onChatMetadata(chatJid, msg.timestamp, agent_id, channel, false);
|
|
427
|
+
this.callbacks.onMessage(chatJid, msg);
|
|
428
|
+
return;
|
|
429
|
+
}
|
|
430
|
+
if (payload.type === 'channel_reply_started') {
|
|
431
|
+
const { request_id, reply_handle } = payload;
|
|
432
|
+
const pending = this.pendingReplyStarts.get(request_id);
|
|
433
|
+
if (!pending)
|
|
434
|
+
return;
|
|
435
|
+
clearTimeout(pending.timeout);
|
|
436
|
+
this.pendingReplyStarts.delete(request_id);
|
|
437
|
+
pending.resolve(reply_handle);
|
|
438
|
+
return;
|
|
439
|
+
}
|
|
440
|
+
if (payload.type === 'api_request') {
|
|
441
|
+
this.handleApiRequest(payload);
|
|
442
|
+
return;
|
|
443
|
+
}
|
|
444
|
+
if (payload.type === 'sse_subscribe') {
|
|
445
|
+
this.handleSseSubscribe(payload);
|
|
446
|
+
return;
|
|
447
|
+
}
|
|
448
|
+
if (payload.type === 'chat_stream_request') {
|
|
449
|
+
this.handleChatStreamRequest(payload);
|
|
450
|
+
return;
|
|
451
|
+
}
|
|
452
|
+
if (payload.type === 'llm_token') {
|
|
453
|
+
this.handleLlmToken(payload);
|
|
454
|
+
return;
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
catch (err) {
|
|
458
|
+
logger.error({ err }, 'Failed to handle gateway message');
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
handleLlmToken(payload) {
|
|
462
|
+
const previousToken = this.llmToken;
|
|
463
|
+
this.llmToken = payload.token;
|
|
464
|
+
this.llmTokenExpiresAt = payload.expires_at;
|
|
465
|
+
process.env.SUPEN_LLM_TOKEN = payload.token;
|
|
466
|
+
process.env.SUPEN_LLM_API_KEY = payload.token;
|
|
467
|
+
logger.info({ expires_at: new Date(payload.expires_at).toISOString() }, 'Received LLM token from gateway');
|
|
468
|
+
if (previousToken && previousToken !== payload.token) {
|
|
469
|
+
// Claude SDK sessions receive the LLM token through process env at
|
|
470
|
+
// creation time. If the gateway restarts or renews the token, cached SDK
|
|
471
|
+
// sessions keep retrying with the stale credential until they are closed.
|
|
472
|
+
void import('./sdk-wrapper.js')
|
|
473
|
+
.then(({ resetAgentSessionCache }) => {
|
|
474
|
+
resetAgentSessionCache();
|
|
475
|
+
logger.info('Reset cached SDK sessions after gateway LLM token rotation');
|
|
476
|
+
})
|
|
477
|
+
.catch((err) => {
|
|
478
|
+
logger.warn({ err }, 'Failed to reset SDK sessions after gateway LLM token rotation');
|
|
479
|
+
});
|
|
480
|
+
}
|
|
481
|
+
// Schedule renewal at 80% of TTL
|
|
482
|
+
this.scheduleTokenRenewal(payload.ttl_ms || (payload.expires_at - Date.now()));
|
|
483
|
+
}
|
|
484
|
+
scheduleTokenRenewal(ttlMs) {
|
|
485
|
+
if (this.tokenRenewalTimeout)
|
|
486
|
+
clearTimeout(this.tokenRenewalTimeout);
|
|
487
|
+
const renewAt = Math.max(ttlMs * 0.8, 30_000); // At least 30s before attempting renewal
|
|
488
|
+
this.tokenRenewalTimeout = setTimeout(() => {
|
|
489
|
+
if (this._connected && this.ws?.readyState === WebSocket.OPEN) {
|
|
490
|
+
logger.debug('Renewing LLM token');
|
|
491
|
+
this.ws.send(JSON.stringify({ type: 'llm_token_renew' }));
|
|
492
|
+
}
|
|
493
|
+
}, renewAt);
|
|
494
|
+
}
|
|
495
|
+
/** Get the current LLM token (null if not yet received from gateway). */
|
|
496
|
+
getLlmToken() {
|
|
497
|
+
return this.llmToken;
|
|
498
|
+
}
|
|
499
|
+
async handleApiRequest(payload) {
|
|
500
|
+
const localUrl = `http://127.0.0.1:${HTTP_PORT}${payload.path}`;
|
|
501
|
+
try {
|
|
502
|
+
const headers = {
|
|
503
|
+
'Content-Type': 'application/json',
|
|
504
|
+
};
|
|
505
|
+
if (HTTP_API_KEY.trim()) {
|
|
506
|
+
headers['X-API-Key'] = HTTP_API_KEY.trim();
|
|
507
|
+
}
|
|
508
|
+
const fetchOpts = {
|
|
509
|
+
method: payload.method || 'GET',
|
|
510
|
+
headers,
|
|
511
|
+
};
|
|
512
|
+
if (payload.body && payload.method !== 'GET')
|
|
513
|
+
fetchOpts.body = JSON.stringify(payload.body);
|
|
514
|
+
const res = await fetch(localUrl, { ...fetchOpts, dispatcher: directAgent });
|
|
515
|
+
const contentType = res.headers.get('content-type') || '';
|
|
516
|
+
if (contentType.includes('application/json')) {
|
|
517
|
+
const body = await res.text();
|
|
518
|
+
let parsed;
|
|
519
|
+
try {
|
|
520
|
+
parsed = JSON.parse(body);
|
|
521
|
+
}
|
|
522
|
+
catch {
|
|
523
|
+
parsed = body;
|
|
524
|
+
}
|
|
525
|
+
this.ws?.send(JSON.stringify({ type: 'api_response', request_id: payload.request_id, status: res.status, headers: {}, body: parsed }));
|
|
526
|
+
}
|
|
527
|
+
else {
|
|
528
|
+
const buffer = Buffer.from(await res.arrayBuffer());
|
|
529
|
+
this.ws?.send(JSON.stringify({
|
|
530
|
+
type: 'api_response', request_id: payload.request_id, status: res.status, encoding: 'base64',
|
|
531
|
+
headers: { 'content-type': contentType, 'content-disposition': res.headers.get('content-disposition') || '', 'content-length': String(buffer.length), 'cache-control': res.headers.get('cache-control') || '' },
|
|
532
|
+
body: buffer.toString('base64'),
|
|
533
|
+
}));
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
catch (err) {
|
|
537
|
+
logger.error({ err, path: payload.path }, 'Failed to relay API request to daemon');
|
|
538
|
+
this.ws?.send(JSON.stringify({ type: 'api_response', request_id: payload.request_id, status: 502, headers: {}, body: { error: 'relay_failed', message: String(err) } }));
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
handleSseSubscribe(payload) {
|
|
542
|
+
const streamKey = payload.path;
|
|
543
|
+
const clientKey = payload.client_key || streamKey;
|
|
544
|
+
const existing = this.activeSseSubscriptions.get(streamKey);
|
|
545
|
+
if (existing) {
|
|
546
|
+
existing.destroy();
|
|
547
|
+
this.activeSseSubscriptions.delete(streamKey);
|
|
548
|
+
}
|
|
549
|
+
const localUrl = `http://127.0.0.1:${HTTP_PORT}${streamKey}`;
|
|
550
|
+
logger.info({ path: streamKey }, 'SSE relay: subscribing to local stream');
|
|
551
|
+
const headers = {};
|
|
552
|
+
if (HTTP_API_KEY.trim()) {
|
|
553
|
+
headers['X-API-Key'] = HTTP_API_KEY.trim();
|
|
554
|
+
}
|
|
555
|
+
const req = http.get(localUrl, { headers }, (res) => {
|
|
556
|
+
let buf = '';
|
|
557
|
+
res.on('data', (chunk) => {
|
|
558
|
+
buf += chunk.toString();
|
|
559
|
+
const parts = buf.split('\n\n');
|
|
560
|
+
buf = parts.pop() || '';
|
|
561
|
+
for (const part of parts) {
|
|
562
|
+
if (!part.trim())
|
|
563
|
+
continue;
|
|
564
|
+
const frame = parseGatewaySseFrame(part);
|
|
565
|
+
if (!frame)
|
|
566
|
+
continue;
|
|
567
|
+
try {
|
|
568
|
+
this.ws?.send(JSON.stringify({
|
|
569
|
+
type: 'sse_event',
|
|
570
|
+
stream_key: streamKey,
|
|
571
|
+
client_key: clientKey,
|
|
572
|
+
event_id: frame.id,
|
|
573
|
+
event: JSON.parse(frame.data),
|
|
574
|
+
}));
|
|
575
|
+
}
|
|
576
|
+
catch {
|
|
577
|
+
this.ws?.send(JSON.stringify({
|
|
578
|
+
type: 'sse_event',
|
|
579
|
+
stream_key: streamKey,
|
|
580
|
+
client_key: clientKey,
|
|
581
|
+
event_id: frame.id,
|
|
582
|
+
event: { raw: frame.data },
|
|
583
|
+
}));
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
});
|
|
587
|
+
res.on('end', () => this.activeSseSubscriptions.delete(streamKey));
|
|
588
|
+
res.on('error', (err) => { logger.debug({ err, path: streamKey }, 'SSE relay: stream error'); this.activeSseSubscriptions.delete(streamKey); });
|
|
589
|
+
});
|
|
590
|
+
req.on('error', (err) => logger.error({ err, path: streamKey }, 'SSE relay: failed to connect'));
|
|
591
|
+
this.activeSseSubscriptions.set(streamKey, { destroy: () => req.destroy() });
|
|
592
|
+
}
|
|
593
|
+
handleChatStreamRequest(payload) {
|
|
594
|
+
const streamKey = payload.path;
|
|
595
|
+
const existing = this.activeSseSubscriptions.get(streamKey);
|
|
596
|
+
if (existing) {
|
|
597
|
+
existing.destroy();
|
|
598
|
+
this.activeSseSubscriptions.delete(streamKey);
|
|
599
|
+
}
|
|
600
|
+
const localUrl = `http://127.0.0.1:${HTTP_PORT}${streamKey}`;
|
|
601
|
+
logger.info({ path: streamKey }, 'Chat relay: POST to local stream');
|
|
602
|
+
const headers = { 'Content-Type': 'application/json' };
|
|
603
|
+
if (HTTP_API_KEY.trim()) {
|
|
604
|
+
headers['X-API-Key'] = HTTP_API_KEY.trim();
|
|
605
|
+
}
|
|
606
|
+
const parsedUrl = new URL(localUrl);
|
|
607
|
+
const req = http.request({
|
|
608
|
+
hostname: parsedUrl.hostname,
|
|
609
|
+
port: parsedUrl.port,
|
|
610
|
+
path: parsedUrl.pathname + parsedUrl.search,
|
|
611
|
+
method: 'POST',
|
|
612
|
+
headers,
|
|
613
|
+
}, (res) => {
|
|
614
|
+
logger.info({ statusCode: res.statusCode, headers: res.headers }, 'Chat relay: got response from local stream');
|
|
615
|
+
res.on('data', (chunk) => {
|
|
616
|
+
logger.debug({ chunkLength: chunk.length }, 'Chat relay: received chunk from local stream');
|
|
617
|
+
this.ws?.send(JSON.stringify({ type: 'stream_raw_chunk', stream_key: streamKey, chunk: chunk.toString() }));
|
|
618
|
+
});
|
|
619
|
+
res.on('end', () => {
|
|
620
|
+
logger.info('Chat relay: local stream ended');
|
|
621
|
+
this.ws?.send(JSON.stringify({ type: 'stream_raw_end', stream_key: streamKey }));
|
|
622
|
+
this.activeSseSubscriptions.delete(streamKey);
|
|
623
|
+
});
|
|
624
|
+
res.on('error', (err) => {
|
|
625
|
+
logger.debug({ err, path: streamKey }, 'Chat relay: stream error');
|
|
626
|
+
this.ws?.send(JSON.stringify({ type: 'stream_raw_end', stream_key: streamKey }));
|
|
627
|
+
this.activeSseSubscriptions.delete(streamKey);
|
|
628
|
+
});
|
|
629
|
+
});
|
|
630
|
+
req.on('error', (err) => logger.error({ err, path: streamKey }, 'Chat relay: failed to connect'));
|
|
631
|
+
req.write(payload.body || '');
|
|
632
|
+
req.end();
|
|
633
|
+
this.activeSseSubscriptions.set(streamKey, { destroy: () => req.destroy() });
|
|
634
|
+
}
|
|
635
|
+
/** Send a message back to the gateway (from a local agent reply). */
|
|
636
|
+
async sendMessage(chatJid, text) {
|
|
637
|
+
if (!this._connected || !this.ws || this.ws.readyState !== WebSocket.OPEN) {
|
|
638
|
+
logger.warn({ chatJid }, 'Cannot send message: Gateway not connected');
|
|
639
|
+
return;
|
|
640
|
+
}
|
|
641
|
+
const parts = chatJid.slice(GATEWAY_JID_PREFIX.length).split(':');
|
|
642
|
+
this.ws.send(JSON.stringify({ type: 'message', agent_id: parts[0], thread_id: parts[1], content: text, timestamp: new Date().toISOString() }));
|
|
643
|
+
}
|
|
644
|
+
/**
|
|
645
|
+
* Send a reply to be delivered via an external channel (Discord, DingTalk, Feishu).
|
|
646
|
+
* The gateway routes this to the appropriate channel instance.
|
|
647
|
+
*/
|
|
648
|
+
async sendChannelReply(channel, jid, content, inReplyTo, traceId) {
|
|
649
|
+
if (!this._connected || !this.ws || this.ws.readyState !== WebSocket.OPEN) {
|
|
650
|
+
logger.warn({ channel, jid }, 'Cannot send channel reply: Gateway not connected');
|
|
651
|
+
return;
|
|
652
|
+
}
|
|
653
|
+
this.ws.send(JSON.stringify({
|
|
654
|
+
type: 'channel_reply',
|
|
655
|
+
...(traceId ? { trace_id: traceId } : {}),
|
|
656
|
+
channel,
|
|
657
|
+
jid,
|
|
658
|
+
content,
|
|
659
|
+
in_reply_to: inReplyTo,
|
|
660
|
+
}));
|
|
661
|
+
}
|
|
662
|
+
getChannelReplySession(chatJid) {
|
|
663
|
+
const existing = this.channelReplySessions.get(chatJid);
|
|
664
|
+
if (existing)
|
|
665
|
+
return existing;
|
|
666
|
+
const resolved = resolveFromChatJid(chatJid);
|
|
667
|
+
if (!resolved)
|
|
668
|
+
return null;
|
|
669
|
+
const session = getSessionForAgent(resolved.agentId, resolved.sessionId);
|
|
670
|
+
if (!session?.channel ||
|
|
671
|
+
!session.channel_session ||
|
|
672
|
+
session.channel === 'unknown' ||
|
|
673
|
+
session.channel === 'gateway') {
|
|
674
|
+
return null;
|
|
675
|
+
}
|
|
676
|
+
const recovered = {
|
|
677
|
+
channel: session.channel,
|
|
678
|
+
channelSession: session.channel_session,
|
|
679
|
+
};
|
|
680
|
+
this.channelReplySessions.set(chatJid, recovered);
|
|
681
|
+
return recovered;
|
|
682
|
+
}
|
|
683
|
+
async startChannelReplyForChat(chatJid, content) {
|
|
684
|
+
const channelCtx = this.getChannelReplySession(chatJid);
|
|
685
|
+
if (!channelCtx) {
|
|
686
|
+
throw new Error(`No gateway channel context for ${chatJid}`);
|
|
687
|
+
}
|
|
688
|
+
if (!this._connected || !this.ws || this.ws.readyState !== WebSocket.OPEN) {
|
|
689
|
+
throw new Error('Gateway not connected');
|
|
690
|
+
}
|
|
691
|
+
const requestId = `reply-start-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;
|
|
692
|
+
return new Promise((resolve, reject) => {
|
|
693
|
+
const timeout = setTimeout(() => {
|
|
694
|
+
this.pendingReplyStarts.delete(requestId);
|
|
695
|
+
reject(new Error(`Timed out waiting for reply handle: ${requestId}`));
|
|
696
|
+
}, 10_000);
|
|
697
|
+
this.pendingReplyStarts.set(requestId, { resolve, reject, timeout });
|
|
698
|
+
this.ws?.send(JSON.stringify({
|
|
699
|
+
type: 'channel_reply_start',
|
|
700
|
+
...(channelCtx.traceId ? { trace_id: channelCtx.traceId } : {}),
|
|
701
|
+
channel: channelCtx.channel,
|
|
702
|
+
jid: channelCtx.channelSession,
|
|
703
|
+
request_id: requestId,
|
|
704
|
+
content,
|
|
705
|
+
}));
|
|
706
|
+
});
|
|
707
|
+
}
|
|
708
|
+
async updateChannelReply(replyHandle, content, traceId) {
|
|
709
|
+
if (!this._connected || !this.ws || this.ws.readyState !== WebSocket.OPEN) {
|
|
710
|
+
throw new Error('Gateway not connected');
|
|
711
|
+
}
|
|
712
|
+
this.ws.send(JSON.stringify({
|
|
713
|
+
type: 'channel_reply_update',
|
|
714
|
+
...(traceId ? { trace_id: traceId } : {}),
|
|
715
|
+
reply_handle: replyHandle,
|
|
716
|
+
content,
|
|
717
|
+
}));
|
|
718
|
+
}
|
|
719
|
+
async finalizeChannelReply(replyHandle, traceId) {
|
|
720
|
+
if (!this._connected || !this.ws || this.ws.readyState !== WebSocket.OPEN) {
|
|
721
|
+
throw new Error('Gateway not connected');
|
|
722
|
+
}
|
|
723
|
+
this.ws.send(JSON.stringify({
|
|
724
|
+
type: 'channel_reply_finalize',
|
|
725
|
+
...(traceId ? { trace_id: traceId } : {}),
|
|
726
|
+
reply_handle: replyHandle,
|
|
727
|
+
}));
|
|
728
|
+
}
|
|
729
|
+
async failChannelReply(replyHandle, content, traceId) {
|
|
730
|
+
if (!this._connected || !this.ws || this.ws.readyState !== WebSocket.OPEN) {
|
|
731
|
+
throw new Error('Gateway not connected');
|
|
732
|
+
}
|
|
733
|
+
this.ws.send(JSON.stringify({
|
|
734
|
+
type: 'channel_reply_error',
|
|
735
|
+
...(traceId ? { trace_id: traceId } : {}),
|
|
736
|
+
reply_handle: replyHandle,
|
|
737
|
+
content,
|
|
738
|
+
}));
|
|
739
|
+
}
|
|
740
|
+
/**
|
|
741
|
+
* Send a reply for a chat JID. If the chat originated from a channel message,
|
|
742
|
+
* uses sendChannelReply; otherwise uses sendMessage.
|
|
743
|
+
*/
|
|
744
|
+
async sendReplyForChat(chatJid, text, inReplyTo, traceId) {
|
|
745
|
+
const channelCtx = this.getChannelReplySession(chatJid);
|
|
746
|
+
if (channelCtx) {
|
|
747
|
+
await this.sendChannelReply(channelCtx.channel, channelCtx.channelSession, text, inReplyTo, traceId || channelCtx.traceId);
|
|
748
|
+
return;
|
|
749
|
+
}
|
|
750
|
+
await this.sendMessage(chatJid, text);
|
|
751
|
+
}
|
|
752
|
+
emitTraceEvent(event) {
|
|
753
|
+
if (!this._connected || !this.ws || this.ws.readyState !== WebSocket.OPEN) {
|
|
754
|
+
return;
|
|
755
|
+
}
|
|
756
|
+
const traceId = typeof event.trace_id === 'string' ? event.trace_id.trim() : '';
|
|
757
|
+
if (!traceId)
|
|
758
|
+
return;
|
|
759
|
+
this.ws.send(JSON.stringify({
|
|
760
|
+
type: 'trace_event',
|
|
761
|
+
trace_id: traceId,
|
|
762
|
+
kind: event.kind,
|
|
763
|
+
message: event.message,
|
|
764
|
+
...(event.level ? { level: event.level } : {}),
|
|
765
|
+
...(event.channel ? { channel: event.channel } : {}),
|
|
766
|
+
...(event.jid ? { jid: event.jid } : {}),
|
|
767
|
+
...(event.agent_id ? { agent_id: event.agent_id } : {}),
|
|
768
|
+
...(event.daemon_id ? { daemon_id: event.daemon_id } : {}),
|
|
769
|
+
...(event.details ? { details: event.details } : {}),
|
|
770
|
+
}));
|
|
771
|
+
}
|
|
772
|
+
reportUsage(event) {
|
|
773
|
+
if (!this._connected || !this.ws || this.ws.readyState !== WebSocket.OPEN) {
|
|
774
|
+
return;
|
|
775
|
+
}
|
|
776
|
+
this.ws.send(JSON.stringify({
|
|
777
|
+
type: 'usage_report',
|
|
778
|
+
...event,
|
|
779
|
+
}));
|
|
780
|
+
}
|
|
781
|
+
/**
|
|
782
|
+
* Clear channel context for a thread (e.g., when a thread ends).
|
|
783
|
+
*/
|
|
784
|
+
clearChannelThread(chatJid) {
|
|
785
|
+
this.channelReplySessions.delete(chatJid);
|
|
786
|
+
}
|
|
787
|
+
/** Check if this JID belongs to a gateway thread. */
|
|
788
|
+
ownsJid(jid) { return jid.startsWith(GATEWAY_JID_PREFIX); }
|
|
789
|
+
isConnected() { return this._connected; }
|
|
790
|
+
async publishRoutingConfig(input = {}) {
|
|
791
|
+
if (!this._connected || !this.ws || this.ws.readyState !== WebSocket.OPEN) {
|
|
792
|
+
logger.warn('Cannot publish gateway routing config: Gateway not connected');
|
|
793
|
+
return;
|
|
794
|
+
}
|
|
795
|
+
const routeTargetIds = input.routeTargetIds ?? [];
|
|
796
|
+
const channelThreads = input.channelThreads ?? [];
|
|
797
|
+
const channelThreadOwners = input.channelThreadOwners;
|
|
798
|
+
const commands = input.commands;
|
|
799
|
+
this.ws.send(JSON.stringify({
|
|
800
|
+
type: 'configure_channels',
|
|
801
|
+
agent_ids: routeTargetIds,
|
|
802
|
+
channel_threads: channelThreads,
|
|
803
|
+
...(channelThreadOwners?.length
|
|
804
|
+
? { channel_thread_owners: channelThreadOwners }
|
|
805
|
+
: {}),
|
|
806
|
+
...(commands?.length ? { commands } : {}),
|
|
807
|
+
}));
|
|
808
|
+
logger.debug({
|
|
809
|
+
routeTargetCount: routeTargetIds.length,
|
|
810
|
+
channelThreadCount: channelThreads.length,
|
|
811
|
+
commandCount: commands?.length || 0,
|
|
812
|
+
}, 'Sent gateway routing config');
|
|
813
|
+
}
|
|
814
|
+
async disconnect() {
|
|
815
|
+
this.intentionalShutdown = true;
|
|
816
|
+
this._connected = false;
|
|
817
|
+
this.rejectPendingReplyStarts('Gateway disconnected');
|
|
818
|
+
this.stopReporting();
|
|
819
|
+
this.stopHeartbeat();
|
|
820
|
+
this.llmToken = null;
|
|
821
|
+
this.llmTokenExpiresAt = null;
|
|
822
|
+
if (this.connectionTimeout) {
|
|
823
|
+
clearTimeout(this.connectionTimeout);
|
|
824
|
+
this.connectionTimeout = null;
|
|
825
|
+
}
|
|
826
|
+
if (this.tokenRenewalTimeout)
|
|
827
|
+
clearTimeout(this.tokenRenewalTimeout);
|
|
828
|
+
if (this.reconnectTimeout)
|
|
829
|
+
clearTimeout(this.reconnectTimeout);
|
|
830
|
+
if (this.ws) {
|
|
831
|
+
this.ws.close();
|
|
832
|
+
this.ws = null;
|
|
833
|
+
}
|
|
834
|
+
}
|
|
835
|
+
}
|
|
836
|
+
// ── Module-level singleton for LLM token access ──
|
|
837
|
+
let gatewayInstance = null;
|
|
838
|
+
let gatewayConfigured = false;
|
|
839
|
+
/** Set the gateway singleton (called once in index.ts after construction). */
|
|
840
|
+
export function setGatewayInstance(gw) {
|
|
841
|
+
gatewayInstance = gw;
|
|
842
|
+
gatewayConfigured = !!gw && !!gw.config?.gateway_url;
|
|
843
|
+
}
|
|
844
|
+
/** Check if a gateway URL was configured (regardless of connection state). */
|
|
845
|
+
export function isGatewayConfigured() {
|
|
846
|
+
return gatewayConfigured;
|
|
847
|
+
}
|
|
848
|
+
/** Get the gateway singleton (for routing channel replies). */
|
|
849
|
+
export function getGatewayInstance() {
|
|
850
|
+
return gatewayInstance;
|
|
851
|
+
}
|
|
852
|
+
/** Get the current LLM token. Returns null if gateway hasn't issued one yet. */
|
|
853
|
+
export function getLlmToken() {
|
|
854
|
+
return gatewayInstance?.getLlmToken() ?? null;
|
|
855
|
+
}
|
|
856
|
+
/**
|
|
857
|
+
* Wait for an LLM token to become available.
|
|
858
|
+
* Polls every 500ms up to timeoutMs (default 10s).
|
|
859
|
+
* Useful at startup when tasks may queue before the gateway connection is established.
|
|
860
|
+
* Rejects immediately if no gateway is configured (standalone mode).
|
|
861
|
+
*/
|
|
862
|
+
export function getLlmTokenOrWait(timeoutMs = 10_000) {
|
|
863
|
+
if (!gatewayConfigured) {
|
|
864
|
+
return Promise.reject(new Error('No gateway configured — cannot wait for LLM token'));
|
|
865
|
+
}
|
|
866
|
+
return new Promise((resolve, reject) => {
|
|
867
|
+
const token = getLlmToken();
|
|
868
|
+
if (token) {
|
|
869
|
+
resolve(token);
|
|
870
|
+
return;
|
|
871
|
+
}
|
|
872
|
+
const deadline = Date.now() + timeoutMs;
|
|
873
|
+
const interval = setInterval(() => {
|
|
874
|
+
const t = getLlmToken();
|
|
875
|
+
if (t) {
|
|
876
|
+
clearInterval(interval);
|
|
877
|
+
resolve(t);
|
|
878
|
+
return;
|
|
879
|
+
}
|
|
880
|
+
if (Date.now() > deadline) {
|
|
881
|
+
clearInterval(interval);
|
|
882
|
+
reject(new Error('Timed out waiting for LLM token'));
|
|
883
|
+
}
|
|
884
|
+
}, 500);
|
|
885
|
+
});
|
|
886
|
+
}
|
|
887
|
+
//# sourceMappingURL=gateway.js.map
|