@cortex-agent/server 2026.5.25 → 2026.6.15
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/defaults/config/thread-templates.json +28 -1
- package/defaults/hooks/cortex-md-injector.mjs +47 -31
- package/defaults/hooks/post-task-hook.mjs +6 -0
- package/defaults/hooks/task-status-check.mjs +8 -0
- package/defaults/mcp-config-slack.json +11 -0
- package/defaults/plugins/cortex-stage-gate/skills/task/SKILL.md +18 -0
- package/defaults/plugins/cortex-system/skills/schedule/SKILL.md +5 -6
- package/defaults/prompts/directives/manager.md +67 -0
- package/dist/agent-adapter/claude/adapter-tui.d.ts +35 -3
- package/dist/agent-adapter/claude/adapter-tui.js +91 -12
- package/dist/agent-adapter/claude/adapter.js +11 -0
- package/dist/agent-adapter/claude/defaults.d.ts +30 -0
- package/dist/agent-adapter/claude/defaults.js +30 -0
- package/dist/agent-adapter/claude/jsonl-tail.d.ts +2 -2
- package/dist/agent-adapter/claude/jsonl-tail.js +25 -20
- package/dist/agent-adapter/claude/spawn-args.d.ts +11 -0
- package/dist/agent-adapter/claude/spawn-args.js +49 -7
- package/dist/agent-adapter/claude/tmux-control.d.ts +20 -0
- package/dist/agent-adapter/claude/tmux-control.js +54 -8
- package/dist/agent-adapter/codex/adapter.js +1 -1
- package/dist/agent-adapter/normalize/event-types.d.ts +1 -0
- package/dist/agent-adapter/pi/adapter.js +46 -19
- package/dist/agent-adapter/pi/agent-dir.d.ts +21 -0
- package/dist/agent-adapter/pi/agent-dir.js +53 -2
- package/dist/agent-adapter/pi/event-parser.js +13 -1
- package/dist/agent-adapter/pi/mcp-bridge.d.ts +12 -0
- package/dist/agent-adapter/pi/mcp-bridge.js +47 -9
- package/dist/agent-adapter/pi/tool-shims.d.ts +14 -0
- package/dist/agent-adapter/pi/tool-shims.js +167 -140
- package/dist/agent-adapter/types.d.ts +7 -2
- package/dist/core/atomic-write.js +21 -0
- package/dist/core/config-generator.d.ts +10 -0
- package/dist/core/config-generator.js +28 -0
- package/dist/core/gateway-generator.d.ts +77 -7
- package/dist/core/gateway-generator.js +323 -30
- package/dist/core/i18n.d.ts +19 -0
- package/dist/core/i18n.js +46 -0
- package/dist/core/icons.d.ts +68 -0
- package/dist/core/icons.js +67 -0
- package/dist/core/locales/en.d.ts +438 -0
- package/dist/core/locales/en.js +24 -0
- package/dist/core/locales/slices/commands.d.ts +214 -0
- package/dist/core/locales/slices/commands.js +462 -0
- package/dist/core/locales/slices/init.d.ts +121 -0
- package/dist/core/locales/slices/init.js +380 -0
- package/dist/core/locales/slices/interactions.d.ts +47 -0
- package/dist/core/locales/slices/interactions.js +102 -0
- package/dist/core/locales/slices/lang.d.ts +8 -0
- package/dist/core/locales/slices/lang.js +18 -0
- package/dist/core/locales/slices/scheduling.d.ts +36 -0
- package/dist/core/locales/slices/scheduling.js +74 -0
- package/dist/core/locales/slices/startup.d.ts +6 -0
- package/dist/core/locales/slices/startup.js +14 -0
- package/dist/core/locales/slices/status.d.ts +20 -0
- package/dist/core/locales/slices/status.js +48 -0
- package/dist/core/locales/zh.d.ts +5 -0
- package/dist/core/locales/zh.js +23 -0
- package/dist/core/profile-generator.d.ts +6 -0
- package/dist/core/profile-generator.js +35 -7
- package/dist/core/running-executions.d.ts +51 -41
- package/dist/core/running-executions.js +120 -73
- package/dist/core/singleton-lock.d.ts +23 -0
- package/dist/core/singleton-lock.js +53 -0
- package/dist/core/status-format.js +4 -2
- package/dist/core/task-parser.d.ts +3 -0
- package/dist/core/task-parser.js +3 -1
- package/dist/core/types/thread-types.d.ts +54 -8
- package/dist/core/utils.d.ts +9 -1
- package/dist/core/utils.js +9 -1
- package/dist/domain/agents/config.d.ts +2 -1
- package/dist/domain/agents/config.js +25 -6
- package/dist/domain/agents/facade.d.ts +18 -0
- package/dist/domain/agents/facade.js +26 -3
- package/dist/domain/agents/profile-manager.d.ts +11 -0
- package/dist/domain/agents/profile-manager.js +21 -2
- package/dist/domain/costs/codex-usage-monitor.js +2 -1
- package/dist/domain/costs/rate-limit-throttle.js +14 -2
- package/dist/domain/executions/registry.d.ts +24 -0
- package/dist/domain/executions/registry.js +38 -0
- package/dist/domain/mcp/core-server.js +15 -3
- package/dist/domain/mcp/feishu/bitable.d.ts +3 -0
- package/dist/domain/mcp/feishu/bitable.js +168 -0
- package/dist/domain/mcp/feishu/block-types.d.ts +44 -0
- package/dist/domain/mcp/feishu/block-types.js +127 -0
- package/dist/domain/mcp/feishu/blocks-to-text.d.ts +19 -0
- package/dist/domain/mcp/feishu/blocks-to-text.js +43 -0
- package/dist/domain/mcp/feishu/client.d.ts +46 -0
- package/dist/domain/mcp/feishu/client.js +87 -0
- package/dist/domain/mcp/feishu/docx.d.ts +3 -0
- package/dist/domain/mcp/feishu/docx.js +163 -0
- package/dist/domain/mcp/feishu/drive.d.ts +23 -0
- package/dist/domain/mcp/feishu/drive.js +66 -0
- package/dist/domain/mcp/feishu/file.d.ts +14 -0
- package/dist/domain/mcp/feishu/file.js +107 -0
- package/dist/domain/mcp/feishu/index.d.ts +6 -0
- package/dist/domain/mcp/feishu/index.js +61 -0
- package/dist/domain/mcp/feishu/markdown-to-blocks.d.ts +40 -0
- package/dist/domain/mcp/feishu/markdown-to-blocks.js +191 -0
- package/dist/domain/mcp/feishu/sheets.d.ts +3 -0
- package/dist/domain/mcp/feishu/sheets.js +119 -0
- package/dist/domain/mcp/feishu/types.d.ts +26 -0
- package/dist/domain/mcp/feishu/types.js +38 -0
- package/dist/domain/mcp/feishu/user-auth.d.ts +140 -0
- package/dist/domain/mcp/feishu/user-auth.js +286 -0
- package/dist/domain/mcp/feishu/wiki.d.ts +3 -0
- package/dist/domain/mcp/feishu/wiki.js +77 -0
- package/dist/domain/mcp/feishu-server.d.ts +2 -0
- package/dist/domain/mcp/feishu-server.js +33 -0
- package/dist/domain/mcp/server.d.ts +0 -1
- package/dist/domain/mcp/server.js +6 -25
- package/dist/domain/mcp/slack-server.d.ts +3 -0
- package/dist/domain/mcp/slack-server.js +46 -0
- package/dist/domain/mcp/tools/context.js +1 -1
- package/dist/domain/mcp/tools/schedule.d.ts +1 -1
- package/dist/domain/mcp/tools/schedule.js +6 -20
- package/dist/domain/mcp/tools/slack.js +20 -7
- package/dist/domain/mcp/tools/thread-ops.d.ts +2 -0
- package/dist/domain/mcp/tools/thread-ops.js +121 -0
- package/dist/domain/mcp/tools/time.d.ts +2 -0
- package/dist/domain/mcp/tools/time.js +66 -0
- package/dist/domain/mcp/tools/tui-ask.js +1 -1
- package/dist/domain/mcp/tools/tui-plan.js +2 -2
- package/dist/domain/mcp/tui-server.js +1 -0
- package/dist/domain/monitor/disk-monitor.js +2 -1
- package/dist/domain/monitor/gpu-monitor.js +2 -1
- package/dist/domain/remote/client-hot-reload.js +3 -2
- package/dist/domain/remote/client-manager.js +8 -6
- package/dist/domain/scheduling/job-registry.d.ts +4 -0
- package/dist/domain/scheduling/job-registry.js +1 -0
- package/dist/domain/scheduling/jobs/_shared.js +2 -1
- package/dist/domain/scheduling/jobs/memory-index-regen.js +3 -2
- package/dist/domain/scheduling/jobs/scheduled-task.js +9 -23
- package/dist/domain/scheduling/jobs/sync-public.js +4 -3
- package/dist/domain/scheduling/jobs/target-dispatch.d.ts +1 -8
- package/dist/domain/scheduling/jobs/target-dispatch.js +0 -7
- package/dist/domain/scheduling/jobs/task-archive.js +3 -2
- package/dist/domain/scheduling/jobs/task-dispatch.js +126 -20
- package/dist/domain/scheduling/schedule-command.js +45 -43
- package/dist/domain/scheduling/scheduler.d.ts +1 -1
- package/dist/domain/scheduling/scheduler.js +2 -1
- package/dist/domain/sessions/session-hooks.d.ts +2 -2
- package/dist/domain/sessions/session-hooks.js +19 -18
- package/dist/domain/sessions/session-lifecycle.d.ts +38 -0
- package/dist/domain/sessions/session-lifecycle.js +44 -0
- package/dist/domain/system/github-release.d.ts +18 -0
- package/dist/domain/system/github-release.js +106 -0
- package/dist/domain/system/install-cli.d.ts +7 -0
- package/dist/domain/system/install-cli.js +106 -0
- package/dist/domain/system/preferences.d.ts +12 -0
- package/dist/domain/system/preferences.js +36 -0
- package/dist/domain/system/server-update-check.js +2 -2
- package/dist/domain/tasks/dispatch-utils.d.ts +58 -1
- package/dist/domain/tasks/dispatch-utils.js +53 -4
- package/dist/domain/tasks/dispatcher.d.ts +11 -2
- package/dist/domain/tasks/dispatcher.js +63 -7
- package/dist/domain/tasks/lint.js +28 -0
- package/dist/domain/tasks/mutator.d.ts +16 -3
- package/dist/domain/tasks/mutator.js +41 -12
- package/dist/domain/tasks/pending-tracker.js +6 -5
- package/dist/domain/tasks/system/task-cli.d.ts +2 -0
- package/dist/domain/tasks/system/task-cli.js +37 -6
- package/dist/domain/tasks/system/task-lifecycle-edit.js +6 -1
- package/dist/domain/tasks/system/task-mutations.d.ts +41 -3
- package/dist/domain/tasks/system/task-mutations.js +166 -8
- package/dist/domain/threads/contract.d.ts +17 -0
- package/dist/domain/threads/contract.js +59 -0
- package/dist/domain/threads/hook-runner.js +7 -6
- package/dist/domain/threads/index.d.ts +2 -0
- package/dist/domain/threads/index.js +3 -1
- package/dist/domain/threads/prompt-builder.d.ts +21 -1
- package/dist/domain/threads/prompt-builder.js +60 -4
- package/dist/domain/threads/runner.d.ts +6 -5
- package/dist/domain/threads/runner.js +128 -79
- package/dist/domain/threads/state-machine.d.ts +23 -21
- package/dist/domain/threads/state-machine.js +113 -54
- package/dist/domain/threads/template-loader.js +3 -2
- package/dist/domain/threads/tree.d.ts +45 -0
- package/dist/domain/threads/tree.js +171 -0
- package/dist/domain/tui-session/index.d.ts +2 -0
- package/dist/domain/tui-session/index.js +3 -0
- package/dist/domain/tui-session/tui-session-service.d.ts +2 -0
- package/dist/domain/tui-session/tui-session-service.js +103 -0
- package/dist/domain/tui-session/types.d.ts +67 -0
- package/dist/domain/tui-session/types.js +4 -0
- package/dist/domain/ui-service/index.d.ts +5 -0
- package/dist/domain/ui-service/index.js +3 -0
- package/dist/domain/ui-service/mutate/executions.d.ts +4 -0
- package/dist/domain/ui-service/mutate/executions.js +15 -0
- package/dist/domain/ui-service/mutate/schedules.d.ts +10 -0
- package/dist/domain/ui-service/mutate/schedules.js +39 -0
- package/dist/domain/ui-service/mutate/tasks.d.ts +23 -0
- package/dist/domain/ui-service/mutate/tasks.js +59 -0
- package/dist/domain/ui-service/mutate/threads.d.ts +4 -0
- package/dist/domain/ui-service/mutate/threads.js +21 -0
- package/dist/domain/ui-service/query/cost.d.ts +3 -0
- package/dist/domain/ui-service/query/cost.js +6 -0
- package/dist/domain/ui-service/query/executions.d.ts +2 -0
- package/dist/domain/ui-service/query/executions.js +35 -0
- package/dist/domain/ui-service/query/projects.d.ts +2 -0
- package/dist/domain/ui-service/query/projects.js +30 -0
- package/dist/domain/ui-service/query/schedules.d.ts +2 -0
- package/dist/domain/ui-service/query/schedules.js +23 -0
- package/dist/domain/ui-service/query/sessions.d.ts +2 -0
- package/dist/domain/ui-service/query/sessions.js +35 -0
- package/dist/domain/ui-service/query/tasks.d.ts +2 -0
- package/dist/domain/ui-service/query/tasks.js +30 -0
- package/dist/domain/ui-service/query/threads.d.ts +2 -0
- package/dist/domain/ui-service/query/threads.js +27 -0
- package/dist/domain/ui-service/subscribe.d.ts +5 -0
- package/dist/domain/ui-service/subscribe.js +118 -0
- package/dist/domain/ui-service/types.d.ts +237 -0
- package/dist/domain/ui-service/types.js +5 -0
- package/dist/domain/ui-service/ui-service.d.ts +2 -0
- package/dist/domain/ui-service/ui-service.js +83 -0
- package/dist/entry/app.js +130 -8
- package/dist/entry/cli.d.ts +12 -0
- package/dist/entry/cli.js +122 -12
- package/dist/entry/daemon.js +12 -30
- package/dist/entry/feishu-login.d.ts +28 -0
- package/dist/entry/feishu-login.js +218 -0
- package/dist/entry/init.d.ts +29 -5
- package/dist/entry/init.js +323 -233
- package/dist/entry/startup-notify.js +6 -3
- package/dist/events/event-types.d.ts +14 -0
- package/dist/hooks/cortex-md-injector.mjs +47 -31
- package/dist/hooks/post-task-hook.mjs +6 -0
- package/dist/hooks/task-status-check.mjs +8 -0
- package/dist/orchestration/agent-runner.d.ts +2 -1
- package/dist/orchestration/agent-runner.js +55 -55
- package/dist/orchestration/conduit-queue.d.ts +18 -0
- package/dist/orchestration/conduit-queue.js +32 -0
- package/dist/orchestration/conversation-runner.d.ts +48 -0
- package/dist/orchestration/conversation-runner.js +109 -0
- package/dist/orchestration/dispatch-reconciler.js +8 -1
- package/dist/orchestration/interactions/ask-user-question.d.ts +1 -1
- package/dist/orchestration/interactions/ask-user-question.js +3 -3
- package/dist/orchestration/interactions/interaction-handlers.js +55 -23
- package/dist/orchestration/interactions/plan-handler.d.ts +2 -2
- package/dist/orchestration/interactions/plan-handler.js +4 -3
- package/dist/orchestration/interactions/update-prompt-slack.js +11 -10
- package/dist/orchestration/interactions/update-prompt.d.ts +6 -0
- package/dist/orchestration/interactions/update-prompt.js +155 -0
- package/dist/orchestration/lifecycle.d.ts +4 -4
- package/dist/orchestration/lifecycle.js +25 -22
- package/dist/orchestration/orchestrator.d.ts +1 -1
- package/dist/orchestration/routing/commands/cancel.js +45 -52
- package/dist/orchestration/routing/commands/channel.js +41 -27
- package/dist/orchestration/routing/commands/cost.js +11 -2
- package/dist/orchestration/routing/commands/device.js +12 -9
- package/dist/orchestration/routing/commands/dispatch.js +11 -10
- package/dist/orchestration/routing/commands/index.d.ts +1 -1
- package/dist/orchestration/routing/commands/index.js +15 -10
- package/dist/orchestration/routing/commands/lang.d.ts +3 -0
- package/dist/orchestration/routing/commands/lang.js +31 -0
- package/dist/orchestration/routing/commands/mode.js +32 -30
- package/dist/orchestration/routing/commands/nvtop.js +15 -13
- package/dist/orchestration/routing/commands/orient.js +2 -1
- package/dist/orchestration/routing/commands/schedule.js +23 -22
- package/dist/orchestration/routing/commands/sendfile.js +9 -7
- package/dist/orchestration/routing/commands/session.d.ts +1 -1
- package/dist/orchestration/routing/commands/session.js +24 -39
- package/dist/orchestration/routing/commands/status.js +78 -71
- package/dist/orchestration/routing/commands/tail.js +7 -5
- package/dist/orchestration/routing/commands/task.js +25 -23
- package/dist/orchestration/routing/commands/thread-handlers.js +55 -44
- package/dist/orchestration/routing/edit-handler.js +8 -7
- package/dist/orchestration/routing/hook-bridge-subscribers.js +17 -1
- package/dist/orchestration/routing/message-router.d.ts +1 -1
- package/dist/orchestration/routing/message-router.js +9 -9
- package/dist/orchestration/routing/webhook.js +233 -2
- package/dist/orchestration/status-helpers.d.ts +5 -1
- package/dist/orchestration/status-helpers.js +13 -11
- package/dist/orchestration/thread-callback.d.ts +67 -0
- package/dist/orchestration/thread-callback.js +465 -0
- package/dist/orchestration/thread-executor.d.ts +22 -1
- package/dist/orchestration/thread-executor.js +85 -36
- package/dist/platform/adapter.d.ts +6 -2
- package/dist/platform/adapters/composite-adapter.d.ts +89 -0
- package/dist/platform/adapters/composite-adapter.js +318 -0
- package/dist/platform/adapters/feishu-output-stream.d.ts +26 -4
- package/dist/platform/adapters/feishu-output-stream.js +274 -36
- package/dist/platform/adapters/feishu.d.ts +50 -5
- package/dist/platform/adapters/feishu.js +370 -116
- package/dist/platform/adapters/index.d.ts +24 -3
- package/dist/platform/adapters/index.js +89 -11
- package/dist/platform/adapters/project-conduits.d.ts +9 -0
- package/dist/platform/adapters/project-conduits.js +42 -0
- package/dist/platform/adapters/slack-project-conduits.d.ts +1 -9
- package/dist/platform/adapters/slack-project-conduits.js +6 -40
- package/dist/platform/adapters/slack.d.ts +9 -2
- package/dist/platform/adapters/slack.js +83 -39
- package/dist/platform/adapters/tui/index.d.ts +9 -0
- package/dist/platform/adapters/tui/index.js +10 -0
- package/dist/platform/adapters/tui/ports.d.ts +20 -0
- package/dist/platform/adapters/tui/ports.js +4 -0
- package/dist/platform/adapters/tui/tui-conduit-state.d.ts +10 -0
- package/dist/platform/adapters/tui/tui-conduit-state.js +17 -0
- package/dist/platform/adapters/tui/tui-connection.d.ts +30 -0
- package/dist/platform/adapters/tui/tui-connection.js +53 -0
- package/dist/platform/adapters/tui/tui-gateway.d.ts +120 -0
- package/dist/platform/adapters/tui/tui-gateway.js +919 -0
- package/dist/platform/adapters/tui/tui-notifications.d.ts +12 -0
- package/dist/platform/adapters/tui/tui-notifications.js +60 -0
- package/dist/platform/adapters/tui/tui-output-stream.d.ts +36 -0
- package/dist/platform/adapters/tui/tui-output-stream.js +99 -0
- package/dist/platform/adapters/tui/tui-transcript.d.ts +8 -0
- package/dist/platform/adapters/tui/tui-transcript.js +56 -0
- package/dist/platform/index.d.ts +1 -0
- package/dist/platform/index.js +2 -1
- package/dist/platform/interactive-builder.js +21 -19
- package/dist/platform/output-stream.d.ts +5 -0
- package/dist/platform/testing.d.ts +7 -11
- package/dist/platform/testing.js +18 -15
- package/dist/platform/tool-trace.js +15 -5
- package/dist/platform/tui/protocol.d.ts +355 -0
- package/dist/platform/tui/protocol.js +199 -0
- package/dist/platform/types.d.ts +12 -3
- package/dist/store/execution-repo.d.ts +6 -1
- package/dist/store/execution-repo.js +13 -3
- package/dist/store/hook-sync.d.ts +18 -0
- package/dist/store/hook-sync.js +92 -0
- package/dist/store/outbound-queue.js +2 -2
- package/dist/store/profile-repo.js +3 -2
- package/dist/store/schedule-repo.d.ts +1 -8
- package/dist/store/session-registry-repo.d.ts +9 -1
- package/dist/store/session-registry-repo.js +19 -3
- package/dist/store/session-repo.d.ts +14 -0
- package/dist/store/session-repo.js +23 -0
- package/dist/store/thread-repo.d.ts +3 -0
- package/dist/store/thread-repo.js +49 -2
- package/dist/store/version-migrations.d.ts +7 -0
- package/dist/store/version-migrations.js +183 -0
- package/dist/tui/App.d.ts +27 -0
- package/dist/tui/App.js +219 -0
- package/dist/tui/components/AskUserModal.d.ts +11 -0
- package/dist/tui/components/AskUserModal.js +364 -0
- package/dist/tui/components/ConfirmModal.d.ts +12 -0
- package/dist/tui/components/ConfirmModal.js +31 -0
- package/dist/tui/components/Dashboard.d.ts +23 -0
- package/dist/tui/components/Dashboard.js +80 -0
- package/dist/tui/components/DashboardCostTab.d.ts +7 -0
- package/dist/tui/components/DashboardCostTab.js +16 -0
- package/dist/tui/components/DashboardExecutionsTab.d.ts +11 -0
- package/dist/tui/components/DashboardExecutionsTab.js +94 -0
- package/dist/tui/components/DashboardSchedulesTab.d.ts +11 -0
- package/dist/tui/components/DashboardSchedulesTab.js +124 -0
- package/dist/tui/components/DashboardTasksTab.d.ts +12 -0
- package/dist/tui/components/DashboardTasksTab.js +200 -0
- package/dist/tui/components/DashboardThreadsTab.d.ts +11 -0
- package/dist/tui/components/DashboardThreadsTab.js +81 -0
- package/dist/tui/components/Header.d.ts +11 -0
- package/dist/tui/components/Header.js +5 -0
- package/dist/tui/components/InputBox.d.ts +10 -0
- package/dist/tui/components/InputBox.js +19 -0
- package/dist/tui/components/MessageRow.d.ts +7 -0
- package/dist/tui/components/MessageRow.js +10 -0
- package/dist/tui/components/Notifications.d.ts +19 -0
- package/dist/tui/components/Notifications.js +72 -0
- package/dist/tui/components/PlanFeedbackModal.d.ts +11 -0
- package/dist/tui/components/PlanFeedbackModal.js +161 -0
- package/dist/tui/components/ProjectSwitcher.d.ts +18 -0
- package/dist/tui/components/ProjectSwitcher.js +43 -0
- package/dist/tui/components/SessionPicker.d.ts +14 -0
- package/dist/tui/components/SessionPicker.js +34 -0
- package/dist/tui/components/SidePanel.d.ts +19 -0
- package/dist/tui/components/SidePanel.js +8 -0
- package/dist/tui/components/StatusLine.d.ts +10 -0
- package/dist/tui/components/StatusLine.js +29 -0
- package/dist/tui/components/Transcript.d.ts +13 -0
- package/dist/tui/components/Transcript.js +56 -0
- package/dist/tui/hooks/useDashboardData.d.ts +32 -0
- package/dist/tui/hooks/useDashboardData.js +104 -0
- package/dist/tui/hooks/useKeybindings.d.ts +19 -0
- package/dist/tui/hooks/useKeybindings.js +87 -0
- package/dist/tui/hooks/useMutate.d.ts +22 -0
- package/dist/tui/hooks/useMutate.js +56 -0
- package/dist/tui/hooks/useNotifications.d.ts +28 -0
- package/dist/tui/hooks/useNotifications.js +70 -0
- package/dist/tui/hooks/useTranscript.d.ts +39 -0
- package/dist/tui/hooks/useTranscript.js +265 -0
- package/dist/tui/hooks/useWsClient.d.ts +29 -0
- package/dist/tui/hooks/useWsClient.js +69 -0
- package/dist/tui/index.d.ts +2 -0
- package/dist/tui/index.js +210 -0
- package/dist/tui/logic.d.ts +16 -0
- package/dist/tui/logic.js +49 -0
- package/dist/tui/render/markdown.d.ts +10 -0
- package/dist/tui/render/markdown.js +38 -0
- package/dist/tui/render/rich-blocks.d.ts +11 -0
- package/dist/tui/render/rich-blocks.js +24 -0
- package/dist/tui/ws-client.d.ts +44 -0
- package/dist/tui/ws-client.js +157 -0
- package/package.json +11 -3
- package/dist/orchestration/channel-queue.d.ts +0 -18
- package/dist/orchestration/channel-queue.js +0 -32
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
// input: src/platform/tui/protocol.js
|
|
2
|
+
// output: Transcript state hook — Map<messageId, RenderedMessage> for O(1) updates, parallel id[] for insertion order
|
|
3
|
+
// pos: Core data model for M5 Ink client transcript rendering
|
|
4
|
+
import { useRef, useCallback, useState } from 'react';
|
|
5
|
+
import { isChatPost, isChatUpdate, isChatDelete, isChatMarkQueued, isStreamText, isStreamMutableOpen, isStreamMutableUpdate, isStreamFlush, isTranscriptReplay, isInteractivePost, isModalOpen, isModalAck } from '../../platform/tui/protocol.js';
|
|
6
|
+
// ── Batch coalescing config ──
|
|
7
|
+
const BATCH_WINDOW_MS = 30;
|
|
8
|
+
// ── Hook ──
|
|
9
|
+
export function useTranscript(opts) {
|
|
10
|
+
const [state, setState] = useState(() => ({
|
|
11
|
+
messages: new Map(),
|
|
12
|
+
ids: [],
|
|
13
|
+
}));
|
|
14
|
+
// Modal callbacks stored in refs for stable dispatch identity
|
|
15
|
+
const onModalOpenRef = useRef(opts?.onModalOpen);
|
|
16
|
+
onModalOpenRef.current = opts?.onModalOpen;
|
|
17
|
+
const onModalAckRef = useRef(opts?.onModalAck);
|
|
18
|
+
onModalAckRef.current = opts?.onModalAck;
|
|
19
|
+
// Batcher refs
|
|
20
|
+
const batchTimerRef = useRef(null);
|
|
21
|
+
const pendingBatch = useRef(null);
|
|
22
|
+
// Flush batched updates
|
|
23
|
+
const flushBatch = useCallback(() => {
|
|
24
|
+
if (batchTimerRef.current !== null) {
|
|
25
|
+
clearTimeout(batchTimerRef.current);
|
|
26
|
+
batchTimerRef.current = null;
|
|
27
|
+
}
|
|
28
|
+
if (pendingBatch.current) {
|
|
29
|
+
pendingBatch.current();
|
|
30
|
+
pendingBatch.current = null;
|
|
31
|
+
}
|
|
32
|
+
}, []);
|
|
33
|
+
// Schedule a batched state update (for stream.text coalescing)
|
|
34
|
+
const scheduleBatch = useCallback((fn) => {
|
|
35
|
+
pendingBatch.current = fn;
|
|
36
|
+
if (batchTimerRef.current === null) {
|
|
37
|
+
batchTimerRef.current = setTimeout(() => {
|
|
38
|
+
batchTimerRef.current = null;
|
|
39
|
+
if (pendingBatch.current) {
|
|
40
|
+
pendingBatch.current();
|
|
41
|
+
pendingBatch.current = null;
|
|
42
|
+
}
|
|
43
|
+
}, BATCH_WINDOW_MS);
|
|
44
|
+
}
|
|
45
|
+
}, []);
|
|
46
|
+
// ── Frame dispatch ──
|
|
47
|
+
const dispatch = useCallback((frame) => {
|
|
48
|
+
if (isChatPost(frame)) {
|
|
49
|
+
setState(prev => _handleChatPost(prev, frame));
|
|
50
|
+
flushBatch(); // flush any pending stream batches on new message
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
if (isChatUpdate(frame)) {
|
|
54
|
+
setState(prev => _handleChatUpdate(prev, frame));
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
if (isChatDelete(frame)) {
|
|
58
|
+
setState(prev => _handleChatDelete(prev, frame));
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
if (isChatMarkQueued(frame)) {
|
|
62
|
+
setState(prev => _handleChatMarkQueued(prev, frame));
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
if (isStreamText(frame)) {
|
|
66
|
+
scheduleBatch(() => {
|
|
67
|
+
setState(prev => _handleStreamText(prev, frame));
|
|
68
|
+
});
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
if (isStreamMutableOpen(frame)) {
|
|
72
|
+
flushBatch(); // render immediately
|
|
73
|
+
setState(prev => _handleStreamMutableOpen(prev, frame));
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
if (isStreamMutableUpdate(frame)) {
|
|
77
|
+
flushBatch(); // render immediately
|
|
78
|
+
setState(prev => _handleStreamMutableUpdate(prev, frame));
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
if (isStreamFlush(frame)) {
|
|
82
|
+
// no-op marker — ordering signal for tests
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
if (isTranscriptReplay(frame)) {
|
|
86
|
+
flushBatch();
|
|
87
|
+
setState(prev => {
|
|
88
|
+
let next = prev;
|
|
89
|
+
for (const item of frame.items) {
|
|
90
|
+
next = _handleChatPost(next, item);
|
|
91
|
+
}
|
|
92
|
+
return next;
|
|
93
|
+
});
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
if (isInteractivePost(frame)) {
|
|
97
|
+
setState(prev => _handleChatPost(prev, frame));
|
|
98
|
+
flushBatch();
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
if (isModalOpen(frame)) {
|
|
102
|
+
onModalOpenRef.current?.(frame);
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
if (isModalAck(frame)) {
|
|
106
|
+
onModalAckRef.current?.(frame);
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
}, [flushBatch, scheduleBatch]);
|
|
110
|
+
// ── Clear ──
|
|
111
|
+
const clear = useCallback(() => {
|
|
112
|
+
flushBatch();
|
|
113
|
+
setState({ messages: new Map(), ids: [] });
|
|
114
|
+
}, [flushBatch]);
|
|
115
|
+
// ── Message count (for status line) ──
|
|
116
|
+
const messageCount = state.ids.length;
|
|
117
|
+
return {
|
|
118
|
+
messages: state.messages,
|
|
119
|
+
ids: state.ids,
|
|
120
|
+
messageCount,
|
|
121
|
+
dispatch,
|
|
122
|
+
clear,
|
|
123
|
+
flushBatch,
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
// ── Pure state helpers (exported for testing) ──
|
|
127
|
+
export function _handleChatPost(prev, frame) {
|
|
128
|
+
const messageId = frame.ref.messageId;
|
|
129
|
+
if (prev.messages.has(messageId))
|
|
130
|
+
return prev; // dedup
|
|
131
|
+
const msg = {
|
|
132
|
+
messageId,
|
|
133
|
+
text: frame.content.text,
|
|
134
|
+
richBlocks: frame.content.richBlocks,
|
|
135
|
+
queued: false,
|
|
136
|
+
streams: new Map(),
|
|
137
|
+
};
|
|
138
|
+
const messages = new Map(prev.messages);
|
|
139
|
+
messages.set(messageId, msg);
|
|
140
|
+
const ids = [...prev.ids, messageId];
|
|
141
|
+
return { messages, ids };
|
|
142
|
+
}
|
|
143
|
+
export function _handleChatUpdate(prev, frame) {
|
|
144
|
+
const messageId = frame.ref.messageId;
|
|
145
|
+
const existing = prev.messages.get(messageId);
|
|
146
|
+
if (!existing)
|
|
147
|
+
return prev;
|
|
148
|
+
const msg = {
|
|
149
|
+
...existing,
|
|
150
|
+
text: frame.content.text,
|
|
151
|
+
richBlocks: frame.content.richBlocks,
|
|
152
|
+
};
|
|
153
|
+
const messages = new Map(prev.messages);
|
|
154
|
+
messages.set(messageId, msg);
|
|
155
|
+
return { messages, ids: prev.ids };
|
|
156
|
+
}
|
|
157
|
+
export function _handleChatDelete(prev, frame) {
|
|
158
|
+
const messageId = frame.ref.messageId;
|
|
159
|
+
if (!prev.messages.has(messageId))
|
|
160
|
+
return prev;
|
|
161
|
+
const messages = new Map(prev.messages);
|
|
162
|
+
messages.delete(messageId);
|
|
163
|
+
const ids = prev.ids.filter(id => id !== messageId);
|
|
164
|
+
return { messages, ids };
|
|
165
|
+
}
|
|
166
|
+
export function _handleChatMarkQueued(prev, frame) {
|
|
167
|
+
const messageId = frame.ref.messageId;
|
|
168
|
+
const existing = prev.messages.get(messageId);
|
|
169
|
+
if (!existing)
|
|
170
|
+
return prev;
|
|
171
|
+
const msg = { ...existing, queued: true };
|
|
172
|
+
const messages = new Map(prev.messages);
|
|
173
|
+
messages.set(messageId, msg);
|
|
174
|
+
return { messages, ids: prev.ids };
|
|
175
|
+
}
|
|
176
|
+
export function _handleStreamText(prev, frame) {
|
|
177
|
+
const streamId = frame.streamId;
|
|
178
|
+
// Find the message that owns this stream (last message with this streamId)
|
|
179
|
+
const messages = new Map(prev.messages);
|
|
180
|
+
let found = false;
|
|
181
|
+
for (const [mid, msg] of messages) {
|
|
182
|
+
if (msg.streams.has(streamId)) {
|
|
183
|
+
const stream = msg.streams.get(streamId);
|
|
184
|
+
stream.segments = [...stream.segments, frame.text];
|
|
185
|
+
messages.set(mid, { ...msg, streams: new Map(msg.streams) });
|
|
186
|
+
found = true;
|
|
187
|
+
break;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
if (!found) {
|
|
191
|
+
// No existing stream — attach to the last message if one exists, otherwise
|
|
192
|
+
// create a synthetic message keyed by streamId so streamed replies aren't
|
|
193
|
+
// dropped on an empty transcript (no chat.post anchors TUI streams).
|
|
194
|
+
const lastId = prev.ids[prev.ids.length - 1];
|
|
195
|
+
if (lastId) {
|
|
196
|
+
const lastMsg = messages.get(lastId);
|
|
197
|
+
const streams = new Map(lastMsg.streams);
|
|
198
|
+
streams.set(streamId, { segments: [frame.text], mutable: new Map() });
|
|
199
|
+
messages.set(lastId, { ...lastMsg, streams });
|
|
200
|
+
return { messages, ids: prev.ids };
|
|
201
|
+
}
|
|
202
|
+
return _appendSyntheticStreamMessage(prev, streamId, { segments: [frame.text], mutable: new Map() });
|
|
203
|
+
}
|
|
204
|
+
return { messages, ids: prev.ids };
|
|
205
|
+
}
|
|
206
|
+
/** Create a new message owned by an orphan stream (empty-transcript safety net). */
|
|
207
|
+
function _appendSyntheticStreamMessage(prev, streamId, stream) {
|
|
208
|
+
const messageId = `stream:${streamId}`;
|
|
209
|
+
const msg = {
|
|
210
|
+
messageId,
|
|
211
|
+
text: '',
|
|
212
|
+
queued: false,
|
|
213
|
+
streams: new Map([[streamId, stream]]),
|
|
214
|
+
};
|
|
215
|
+
const messages = new Map(prev.messages);
|
|
216
|
+
messages.set(messageId, msg);
|
|
217
|
+
return { messages, ids: [...prev.ids, messageId] };
|
|
218
|
+
}
|
|
219
|
+
export function _handleStreamMutableOpen(prev, frame) {
|
|
220
|
+
const streamId = frame.streamId;
|
|
221
|
+
const messages = new Map(prev.messages);
|
|
222
|
+
for (const [mid, msg] of messages) {
|
|
223
|
+
const streams = new Map(msg.streams);
|
|
224
|
+
const existing = streams.get(streamId);
|
|
225
|
+
if (existing) {
|
|
226
|
+
// Ensure region exists
|
|
227
|
+
existing.mutable.set(frame.regionId, frame.text);
|
|
228
|
+
streams.set(streamId, { ...existing });
|
|
229
|
+
messages.set(mid, { ...msg, streams });
|
|
230
|
+
return { messages, ids: prev.ids };
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
// No existing stream — attach to last message, or create a synthetic one
|
|
234
|
+
// when the transcript is empty so the streamed reply is not lost.
|
|
235
|
+
const lastId = prev.ids[prev.ids.length - 1];
|
|
236
|
+
if (lastId) {
|
|
237
|
+
const lastMsg = messages.get(lastId);
|
|
238
|
+
const streams = new Map(lastMsg.streams);
|
|
239
|
+
const mutable = new Map();
|
|
240
|
+
mutable.set(frame.regionId, frame.text);
|
|
241
|
+
streams.set(streamId, { segments: [], mutable });
|
|
242
|
+
messages.set(lastId, { ...lastMsg, streams });
|
|
243
|
+
return { messages, ids: prev.ids };
|
|
244
|
+
}
|
|
245
|
+
return _appendSyntheticStreamMessage(prev, streamId, {
|
|
246
|
+
segments: [],
|
|
247
|
+
mutable: new Map([[frame.regionId, frame.text]]),
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
export function _handleStreamMutableUpdate(prev, frame) {
|
|
251
|
+
const streamId = frame.streamId;
|
|
252
|
+
const messages = new Map(prev.messages);
|
|
253
|
+
for (const [mid, msg] of messages) {
|
|
254
|
+
const stream = msg.streams.get(streamId);
|
|
255
|
+
if (stream) {
|
|
256
|
+
const newMutable = new Map(stream.mutable);
|
|
257
|
+
newMutable.set(frame.regionId, frame.text);
|
|
258
|
+
const newStreams = new Map(msg.streams);
|
|
259
|
+
newStreams.set(streamId, { segments: stream.segments, mutable: newMutable });
|
|
260
|
+
messages.set(mid, { ...msg, streams: newStreams });
|
|
261
|
+
return { messages, ids: prev.ids };
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
return prev;
|
|
265
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { WsState } from '../ws-client.js';
|
|
2
|
+
import type { TuiFrame, HandshakeAck } from '../../platform/tui/protocol.js';
|
|
3
|
+
export interface UseWsClientOpts {
|
|
4
|
+
/** WebSocket address, e.g. ws://127.0.0.1:3003 */
|
|
5
|
+
address: string;
|
|
6
|
+
/** Session ID to resume (null for fresh) */
|
|
7
|
+
resumeSessionId?: string | null;
|
|
8
|
+
/** Project to join (null for default) */
|
|
9
|
+
project?: string | null;
|
|
10
|
+
/** Called for every parsed frame */
|
|
11
|
+
onFrame?: (frame: TuiFrame) => void;
|
|
12
|
+
/** Called on successful connection after handshake.ack */
|
|
13
|
+
onConnected?: (ack: HandshakeAck) => void;
|
|
14
|
+
/** Called on cap-exceeded (all retries exhausted) */
|
|
15
|
+
onCapExceeded?: () => void;
|
|
16
|
+
}
|
|
17
|
+
export interface UseWsClientResult {
|
|
18
|
+
/** Current connection state */
|
|
19
|
+
state: WsState;
|
|
20
|
+
/** Connected handshake ack (null until connected) */
|
|
21
|
+
ack: HandshakeAck | null;
|
|
22
|
+
/** Send a frame to the server */
|
|
23
|
+
sendFrame: (frame: TuiFrame) => void;
|
|
24
|
+
/** Manually reconnect */
|
|
25
|
+
reconnect: () => void;
|
|
26
|
+
/** Disconnect */
|
|
27
|
+
disconnect: () => void;
|
|
28
|
+
}
|
|
29
|
+
export declare function useWsClient(opts: UseWsClientOpts): UseWsClientResult;
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
// input: ws-client.ts + src/platform/tui/protocol.js
|
|
2
|
+
// output: React hook wrapping WsClient — connection lifecycle + frame stream
|
|
3
|
+
// pos: Thin React wrapper so components don't manage WS lifecycle directly
|
|
4
|
+
import { useEffect, useRef, useState, useCallback } from 'react';
|
|
5
|
+
import { WsClient } from '../ws-client.js';
|
|
6
|
+
import { CORTEX_VERSION } from '../../core/version.js';
|
|
7
|
+
export function useWsClient(opts) {
|
|
8
|
+
const clientRef = useRef(null);
|
|
9
|
+
const [state, setState] = useState('disconnected');
|
|
10
|
+
const [ack, setAck] = useState(null);
|
|
11
|
+
// Stable callbacks in refs
|
|
12
|
+
const onFrameRef = useRef(opts.onFrame);
|
|
13
|
+
onFrameRef.current = opts.onFrame;
|
|
14
|
+
const onConnectedRef = useRef(opts.onConnected);
|
|
15
|
+
onConnectedRef.current = opts.onConnected;
|
|
16
|
+
const onCapExceededRef = useRef(opts.onCapExceeded);
|
|
17
|
+
onCapExceededRef.current = opts.onCapExceeded;
|
|
18
|
+
const connect = useCallback(() => {
|
|
19
|
+
const client = new WsClient();
|
|
20
|
+
clientRef.current = client;
|
|
21
|
+
// Set resume session before connecting so auto-hello includes it
|
|
22
|
+
if (opts.resumeSessionId) {
|
|
23
|
+
client.markSessionId(opts.resumeSessionId);
|
|
24
|
+
}
|
|
25
|
+
client.connect(opts.address, {
|
|
26
|
+
clientVersion: CORTEX_VERSION,
|
|
27
|
+
project: opts.project ?? null,
|
|
28
|
+
onStateChange: (s) => {
|
|
29
|
+
setState(s);
|
|
30
|
+
if (s === 'connected') {
|
|
31
|
+
const currentAck = client.ack;
|
|
32
|
+
if (currentAck) {
|
|
33
|
+
setAck(currentAck);
|
|
34
|
+
onConnectedRef.current?.(currentAck);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
onFrame: (frame) => {
|
|
39
|
+
onFrameRef.current?.(frame);
|
|
40
|
+
},
|
|
41
|
+
onClose: () => { },
|
|
42
|
+
onCapExceeded: () => {
|
|
43
|
+
onCapExceededRef.current?.();
|
|
44
|
+
},
|
|
45
|
+
});
|
|
46
|
+
}, [opts.address, opts.resumeSessionId, opts.project]);
|
|
47
|
+
useEffect(() => {
|
|
48
|
+
connect();
|
|
49
|
+
return () => {
|
|
50
|
+
clientRef.current?.close();
|
|
51
|
+
clientRef.current = null;
|
|
52
|
+
};
|
|
53
|
+
}, [connect]);
|
|
54
|
+
const sendFrame = useCallback((frame) => {
|
|
55
|
+
clientRef.current?.send(frame);
|
|
56
|
+
}, []);
|
|
57
|
+
const reconnect = useCallback(() => {
|
|
58
|
+
clientRef.current?.close();
|
|
59
|
+
setAck(null);
|
|
60
|
+
connect();
|
|
61
|
+
}, [connect]);
|
|
62
|
+
const disconnect = useCallback(() => {
|
|
63
|
+
clientRef.current?.close();
|
|
64
|
+
clientRef.current = null;
|
|
65
|
+
setState('disconnected');
|
|
66
|
+
setAck(null);
|
|
67
|
+
}, []);
|
|
68
|
+
return { state, ack, sendFrame, reconnect, disconnect };
|
|
69
|
+
}
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// input: argv → connect → render <App>
|
|
3
|
+
// output: M5 Ink TUI client entry point
|
|
4
|
+
// pos: Entry point for cortex-tui
|
|
5
|
+
import React from 'react';
|
|
6
|
+
import { render } from 'ink';
|
|
7
|
+
import { App } from './App.js';
|
|
8
|
+
import { WsClient } from './ws-client.js';
|
|
9
|
+
import { isHandshakeAck, isSessionSwitched, isUiQueryResult } from '../platform/tui/protocol.js';
|
|
10
|
+
import { CORTEX_VERSION } from '../core/version.js';
|
|
11
|
+
function parseArgs(argv) {
|
|
12
|
+
const args = { resume: false, project: null, port: 3003 };
|
|
13
|
+
for (let i = 2; i < argv.length; i++) {
|
|
14
|
+
switch (argv[i]) {
|
|
15
|
+
case '--resume':
|
|
16
|
+
args.resume = true;
|
|
17
|
+
break;
|
|
18
|
+
case '--project':
|
|
19
|
+
args.project = argv[++i] ?? null;
|
|
20
|
+
break;
|
|
21
|
+
case '--port':
|
|
22
|
+
args.port = parseInt(argv[++i] ?? '3003', 10);
|
|
23
|
+
break;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return args;
|
|
27
|
+
}
|
|
28
|
+
// ── Main ──
|
|
29
|
+
async function main() {
|
|
30
|
+
const args = parseArgs(process.argv);
|
|
31
|
+
const client = new WsClient();
|
|
32
|
+
let dispatchFrame = null;
|
|
33
|
+
let ackData = null;
|
|
34
|
+
let lastSessionId = null;
|
|
35
|
+
let currentProjectId = null;
|
|
36
|
+
let currentSessionName = null;
|
|
37
|
+
// Resume state: store sessions for interactive picker
|
|
38
|
+
let resumableSessions = null;
|
|
39
|
+
let resumePending = false;
|
|
40
|
+
// Connection state
|
|
41
|
+
let connectionState = 'disconnected';
|
|
42
|
+
let errorMessage = null;
|
|
43
|
+
// Track rerender
|
|
44
|
+
let rerender = null;
|
|
45
|
+
let unmount = null;
|
|
46
|
+
const doRender = () => {
|
|
47
|
+
const app = React.createElement(App, {
|
|
48
|
+
sendFrame: (frame) => client.send(frame),
|
|
49
|
+
connectionState,
|
|
50
|
+
ack: ackData,
|
|
51
|
+
onReconnect: () => {
|
|
52
|
+
errorMessage = null;
|
|
53
|
+
connectionState = 'disconnected';
|
|
54
|
+
client.close();
|
|
55
|
+
doConnect();
|
|
56
|
+
},
|
|
57
|
+
onDisconnect: () => {
|
|
58
|
+
process.exit(0);
|
|
59
|
+
},
|
|
60
|
+
onSendCancel: () => {
|
|
61
|
+
const id = `msg-${Date.now()}`;
|
|
62
|
+
client.send({ type: 'msg.user', id, text: '!cancel' });
|
|
63
|
+
},
|
|
64
|
+
serverVersion: ackData?.serverVersion ?? null,
|
|
65
|
+
projectId: currentProjectId ?? ackData?.defaultProjectId ?? null,
|
|
66
|
+
sessionName: currentSessionName,
|
|
67
|
+
errorMessage,
|
|
68
|
+
onSetDispatch: (dispatch) => { dispatchFrame = dispatch; },
|
|
69
|
+
resumableSessions,
|
|
70
|
+
resumePending,
|
|
71
|
+
onResumeSelect: (sessionId, _projectId) => {
|
|
72
|
+
resumableSessions = null;
|
|
73
|
+
resumePending = false;
|
|
74
|
+
client.send({
|
|
75
|
+
type: 'session.switch',
|
|
76
|
+
id: 'sess-resume',
|
|
77
|
+
projectId: currentProjectId ?? ackData?.defaultProjectId ?? 'general',
|
|
78
|
+
sessionId,
|
|
79
|
+
});
|
|
80
|
+
doRender();
|
|
81
|
+
},
|
|
82
|
+
onResumeCancel: () => {
|
|
83
|
+
resumableSessions = null;
|
|
84
|
+
resumePending = false;
|
|
85
|
+
client.send({
|
|
86
|
+
type: 'session.switch',
|
|
87
|
+
id: 'sess-fallback',
|
|
88
|
+
projectId: currentProjectId ?? ackData?.defaultProjectId ?? 'general',
|
|
89
|
+
sessionId: null,
|
|
90
|
+
});
|
|
91
|
+
doRender();
|
|
92
|
+
},
|
|
93
|
+
});
|
|
94
|
+
if (rerender) {
|
|
95
|
+
rerender(app);
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
const instance = render(app, { exitOnCtrlC: false });
|
|
99
|
+
rerender = instance.rerender;
|
|
100
|
+
unmount = instance.unmount;
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
const doConnect = () => {
|
|
104
|
+
connectionState = 'connecting';
|
|
105
|
+
doRender();
|
|
106
|
+
// Set resume session before connect so auto-hello includes it
|
|
107
|
+
if (lastSessionId) {
|
|
108
|
+
client.markSessionId(lastSessionId);
|
|
109
|
+
}
|
|
110
|
+
client.connect(`ws://127.0.0.1:${args.port}`, {
|
|
111
|
+
clientVersion: CORTEX_VERSION,
|
|
112
|
+
project: args.project,
|
|
113
|
+
onStateChange: (s) => {
|
|
114
|
+
connectionState = s;
|
|
115
|
+
if (s === 'connected') {
|
|
116
|
+
ackData = client.ack;
|
|
117
|
+
errorMessage = null; // clear any prior cap-exceeded error on success
|
|
118
|
+
}
|
|
119
|
+
doRender();
|
|
120
|
+
},
|
|
121
|
+
onFrame: (frame) => {
|
|
122
|
+
if (isHandshakeAck(frame)) {
|
|
123
|
+
ackData = {
|
|
124
|
+
conduitId: frame.conduitId,
|
|
125
|
+
serverVersion: frame.serverVersion,
|
|
126
|
+
defaultProjectId: frame.defaultProjectId,
|
|
127
|
+
};
|
|
128
|
+
client.markAck(frame);
|
|
129
|
+
client.markConnected();
|
|
130
|
+
// Determine initial session
|
|
131
|
+
const project = args.project ?? frame.defaultProjectId;
|
|
132
|
+
currentProjectId = project;
|
|
133
|
+
if (args.resume) {
|
|
134
|
+
resumePending = true;
|
|
135
|
+
client.send({
|
|
136
|
+
type: 'ui.query',
|
|
137
|
+
id: 'sess-list',
|
|
138
|
+
scope: 'sessions.list',
|
|
139
|
+
params: { resumable: true },
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
else {
|
|
143
|
+
// Fresh session
|
|
144
|
+
client.send({
|
|
145
|
+
type: 'session.switch',
|
|
146
|
+
id: 'sess-init',
|
|
147
|
+
projectId: project,
|
|
148
|
+
sessionId: null,
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
connectionState = 'connected';
|
|
152
|
+
doRender();
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
if (isSessionSwitched(frame)) {
|
|
156
|
+
lastSessionId = frame.sessionId;
|
|
157
|
+
client.markSessionId(frame.sessionId);
|
|
158
|
+
currentProjectId = frame.projectId;
|
|
159
|
+
currentSessionName = frame.sessionName;
|
|
160
|
+
resumePending = false;
|
|
161
|
+
resumableSessions = null;
|
|
162
|
+
doRender();
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
// Handle resume: ui.queryResult for sessions.list
|
|
166
|
+
if (isUiQueryResult(frame) && frame.id === 'sess-list' && resumePending) {
|
|
167
|
+
if (frame.ok && Array.isArray(frame.data) && frame.data.length > 0) {
|
|
168
|
+
// Store sessions for interactive picker
|
|
169
|
+
resumableSessions = frame.data.map((s) => ({
|
|
170
|
+
sessionId: s.sessionId ?? s.id,
|
|
171
|
+
name: s.name,
|
|
172
|
+
projectId: s.projectId,
|
|
173
|
+
label: s.label ?? null,
|
|
174
|
+
}));
|
|
175
|
+
}
|
|
176
|
+
// If no sessions, leave resumableSessions as empty array — App renders "no sessions" state
|
|
177
|
+
// Then auto-fallback to fresh session
|
|
178
|
+
if (!frame.ok || !Array.isArray(frame.data) || frame.data.length === 0) {
|
|
179
|
+
resumePending = false;
|
|
180
|
+
client.send({
|
|
181
|
+
type: 'session.switch',
|
|
182
|
+
id: 'sess-fallback',
|
|
183
|
+
projectId: currentProjectId ?? ackData?.defaultProjectId ?? 'general',
|
|
184
|
+
sessionId: null,
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
doRender();
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
// Dispatch frame to App for routing (transcript, dashboard, notifications).
|
|
191
|
+
// The App's hooks update React state, which triggers re-render — no extra
|
|
192
|
+
// doRender() here (that caused a full-tree reconcile on every stream token).
|
|
193
|
+
dispatchFrame?.(frame);
|
|
194
|
+
},
|
|
195
|
+
onClose: (reason) => {
|
|
196
|
+
connectionState = 'reconnecting';
|
|
197
|
+
doRender();
|
|
198
|
+
},
|
|
199
|
+
onCapExceeded: () => {
|
|
200
|
+
errorMessage = 'Connection failed — press R to retry, Ctrl+C to exit';
|
|
201
|
+
doRender();
|
|
202
|
+
},
|
|
203
|
+
});
|
|
204
|
+
};
|
|
205
|
+
doConnect();
|
|
206
|
+
}
|
|
207
|
+
main().catch((e) => {
|
|
208
|
+
console.error('Fatal error:', e);
|
|
209
|
+
process.exit(1);
|
|
210
|
+
});
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { TuiFrame } from '../platform/tui/protocol.js';
|
|
2
|
+
export type FocusZone = 'modal' | 'dashboard' | 'input';
|
|
3
|
+
export declare function computeFocusZone(opts: {
|
|
4
|
+
modalOpen: boolean;
|
|
5
|
+
sidePanelVisible: boolean;
|
|
6
|
+
}): FocusZone;
|
|
7
|
+
export declare function isAgentResponseFrame(frame: TuiFrame): boolean;
|
|
8
|
+
export interface StreamLike {
|
|
9
|
+
segments: string[];
|
|
10
|
+
mutable: Map<string, string>;
|
|
11
|
+
}
|
|
12
|
+
export declare function collectStreamText(streams: Map<string, StreamLike>): string;
|
|
13
|
+
export declare function computeVisibleWindow(idsLength: number, visibleCount: number, scrollOffset: number): {
|
|
14
|
+
start: number;
|
|
15
|
+
end: number;
|
|
16
|
+
};
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// input: protocol frame types (structural)
|
|
2
|
+
// output: Pure UI logic helpers for the M5 Ink client — focus zone, response-frame
|
|
3
|
+
// detection, stream-text collection, visible-window computation
|
|
4
|
+
// pos: Testable logic extracted out of React components/hooks
|
|
5
|
+
export function computeFocusZone(opts) {
|
|
6
|
+
if (opts.modalOpen)
|
|
7
|
+
return 'modal';
|
|
8
|
+
if (opts.sidePanelVisible)
|
|
9
|
+
return 'dashboard';
|
|
10
|
+
return 'input';
|
|
11
|
+
}
|
|
12
|
+
// ── Agent-response detection ──
|
|
13
|
+
// The M4 protocol has no explicit turn-end frame. The arrival of any agent reply
|
|
14
|
+
// or stream frame is the signal that the agent has started responding — used to
|
|
15
|
+
// re-enable sending and clear the queued counter.
|
|
16
|
+
const AGENT_RESPONSE_TYPES = new Set([
|
|
17
|
+
'chat.post',
|
|
18
|
+
'chat.update',
|
|
19
|
+
'interactive.post',
|
|
20
|
+
'stream.text',
|
|
21
|
+
'stream.mutableOpen',
|
|
22
|
+
'stream.mutableUpdate',
|
|
23
|
+
'transcript.replay',
|
|
24
|
+
]);
|
|
25
|
+
export function isAgentResponseFrame(frame) {
|
|
26
|
+
return AGENT_RESPONSE_TYPES.has(frame.type);
|
|
27
|
+
}
|
|
28
|
+
export function collectStreamText(streams) {
|
|
29
|
+
let out = '';
|
|
30
|
+
for (const [, stream] of streams) {
|
|
31
|
+
out += stream.segments.join('');
|
|
32
|
+
for (const [, regionText] of stream.mutable) {
|
|
33
|
+
out += regionText;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return out;
|
|
37
|
+
}
|
|
38
|
+
// ── Visible window ──
|
|
39
|
+
// Bottom-anchored viewport over the transcript id list. scrollOffset counts
|
|
40
|
+
// messages scrolled up from the bottom (0 = pinned to bottom).
|
|
41
|
+
export function computeVisibleWindow(idsLength, visibleCount, scrollOffset) {
|
|
42
|
+
if (idsLength <= 0)
|
|
43
|
+
return { start: 0, end: 0 };
|
|
44
|
+
const maxOffset = Math.max(0, idsLength - 1);
|
|
45
|
+
const effectiveOffset = Math.min(Math.max(0, scrollOffset), maxOffset);
|
|
46
|
+
const end = idsLength - effectiveOffset;
|
|
47
|
+
const start = Math.max(0, end - Math.max(1, visibleCount));
|
|
48
|
+
return { start, end };
|
|
49
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export interface MarkdownSegment {
|
|
2
|
+
type: 'text' | 'bold' | 'italic' | 'code' | 'link';
|
|
3
|
+
text: string;
|
|
4
|
+
url?: string;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Parse inline markdown into typed segments.
|
|
8
|
+
* Supports: **bold**, *italic*, `code`, [text](url)
|
|
9
|
+
*/
|
|
10
|
+
export declare function parseMarkdown(text: string): MarkdownSegment[];
|