@cortex-agent/server 2026.5.19-1
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/.claude/settings.json +13 -0
- package/defaults/CORTEX.md +15 -0
- package/defaults/config/budget.json +4 -0
- package/defaults/config/session-hooks.json +7 -0
- package/defaults/config/thread-templates.json +528 -0
- package/defaults/context/CORTEX.md +40 -0
- package/defaults/context/decisions/DR-0012.md +54 -0
- package/defaults/context/ideas/CORTEX.md +10 -0
- package/defaults/context/projects/CORTEX.md +25 -0
- package/defaults/context/scans/CORTEX.md +13 -0
- package/defaults/context/user/CORTEX.md +21 -0
- package/defaults/data/schedules.json +52 -0
- package/defaults/gitignore +44 -0
- package/defaults/hooks/ask-user-question-hook.mjs +66 -0
- package/defaults/hooks/cortex-md-injector.mjs +237 -0
- package/defaults/hooks/exit-plan-mode-hook.mjs +84 -0
- package/defaults/hooks/memory-ref-tracker.mjs +154 -0
- package/defaults/hooks/new-session-hook.mjs +83 -0
- package/defaults/hooks/post-task-hook.mjs +63 -0
- package/defaults/hooks/rules-loader.mjs +212 -0
- package/defaults/hooks/sensitive-file-edit.mjs +84 -0
- package/defaults/hooks/session-activity-tracker.mjs +254 -0
- package/defaults/hooks/task-status-check.mjs +218 -0
- package/defaults/hooks/tasks-yaml-guard.mjs +230 -0
- package/defaults/plugins/cortex-coder/.claude-plugin/plugin.json +5 -0
- package/defaults/plugins/cortex-coder/skills/cli-standards/SKILL.md +208 -0
- package/defaults/plugins/cortex-coder/skills/code-standards/SKILL.md +146 -0
- package/defaults/plugins/cortex-coder/skills/develop/SKILL.md +164 -0
- package/defaults/plugins/cortex-common/.claude-plugin/plugin.json +5 -0
- package/defaults/plugins/cortex-common/skills/compound/SKILL.md +349 -0
- package/defaults/plugins/cortex-common/skills/compound-simple/SKILL.md +104 -0
- package/defaults/plugins/cortex-common/skills/critique/SKILL.md +88 -0
- package/defaults/plugins/cortex-common/skills/debug-campaign/SKILL.md +508 -0
- package/defaults/plugins/cortex-common/skills/diagnose/SKILL.md +169 -0
- package/defaults/plugins/cortex-common/skills/postmortem/SKILL.md +135 -0
- package/defaults/plugins/cortex-common/skills/solution-design/SKILL.md +183 -0
- package/defaults/plugins/cortex-common/skills/synthesize/SKILL.md +120 -0
- package/defaults/plugins/cortex-stage-gate/.claude-plugin/plugin.json +5 -0
- package/defaults/plugins/cortex-stage-gate/skills/need-approval/SKILL.md +89 -0
- package/defaults/plugins/cortex-stage-gate/skills/reorient/SKILL.md +163 -0
- package/defaults/plugins/cortex-stage-gate/skills/task/SKILL.md +425 -0
- package/defaults/plugins/cortex-system/.claude-plugin/plugin.json +5 -0
- package/defaults/plugins/cortex-system/skills/approval/SKILL.md +84 -0
- package/defaults/plugins/cortex-system/skills/client-manage/SKILL.md +199 -0
- package/defaults/plugins/cortex-system/skills/deep-retrospective/CORTEX.md +8 -0
- package/defaults/plugins/cortex-system/skills/deep-retrospective/SKILL.md +201 -0
- package/defaults/plugins/cortex-system/skills/deep-retrospective/scripts/CORTEX.md +8 -0
- package/defaults/plugins/cortex-system/skills/deep-retrospective/scripts/extract_session.py +165 -0
- package/defaults/plugins/cortex-system/skills/deep-retrospective/scripts/scan_logs.py +402 -0
- package/defaults/plugins/cortex-system/skills/evolve/SKILL.md +115 -0
- package/defaults/plugins/cortex-system/skills/feedback/SKILL.md +262 -0
- package/defaults/plugins/cortex-system/skills/gravity/SKILL.md +125 -0
- package/defaults/plugins/cortex-system/skills/orient/SKILL.md +310 -0
- package/defaults/plugins/cortex-system/skills/orient-project/SKILL.md +85 -0
- package/defaults/plugins/cortex-system/skills/project-init/SKILL.md +167 -0
- package/defaults/plugins/cortex-system/skills/refresh-skills/SKILL.md +153 -0
- package/defaults/plugins/cortex-system/skills/schedule/SKILL.md +209 -0
- package/defaults/plugins/cortex-system/skills/thread/SKILL.md +478 -0
- package/defaults/plugins/cortex-system/skills/user-learn/SKILL.md +95 -0
- package/defaults/prompts/directives/coder-reviewer.md +106 -0
- package/defaults/prompts/directives/coder.md +90 -0
- package/defaults/prompts/directives/director.md +97 -0
- package/defaults/prompts/directives/doc-reviewer.md +93 -0
- package/defaults/prompts/directives/doc-writer.md +85 -0
- package/defaults/prompts/directives/executor-reviewer.md +96 -0
- package/defaults/prompts/directives/executor.md +85 -0
- package/defaults/prompts/directives/milestone-executor.md +119 -0
- package/defaults/prompts/directives/milestone-reviewer.md +108 -0
- package/defaults/prompts/directives/plan-executor.md +121 -0
- package/defaults/prompts/directives/plan-reviewer.md +99 -0
- package/defaults/prompts/systemPrompts/coder.md +64 -0
- package/defaults/prompts/systemPrompts/direct.md +92 -0
- package/defaults/prompts/systemPrompts/web-opus-4-6.md +68 -0
- package/defaults/prompts/systemPrompts/web-sonnet-4-6.md +76 -0
- package/defaults/prompts/systemPrompts/web.md +50 -0
- package/defaults/prompts/systemPrompts/worker.md +67 -0
- package/defaults/rules/correction-propagation.md +31 -0
- package/defaults/rules/decision-records.md +49 -0
- package/defaults/rules/experiment-format.md +165 -0
- package/defaults/rules/issues-md.md +37 -0
- package/defaults/rules/meta-conventions.md +45 -0
- package/defaults/rules/mission-md.md +47 -0
- package/defaults/rules/overview-md.md +41 -0
- package/defaults/rules/roadmap-md.md +55 -0
- package/defaults/rules/status-md.md +49 -0
- package/defaults/rules/task-system.md +53 -0
- package/dist/agent-adapter/capabilities.d.ts +12 -0
- package/dist/agent-adapter/capabilities.js +52 -0
- package/dist/agent-adapter/claude/adapter-tui.d.ts +140 -0
- package/dist/agent-adapter/claude/adapter-tui.js +422 -0
- package/dist/agent-adapter/claude/adapter.d.ts +85 -0
- package/dist/agent-adapter/claude/adapter.js +771 -0
- package/dist/agent-adapter/claude/cost-from-usage.d.ts +62 -0
- package/dist/agent-adapter/claude/cost-from-usage.js +97 -0
- package/dist/agent-adapter/claude/defaults.d.ts +30 -0
- package/dist/agent-adapter/claude/defaults.js +49 -0
- package/dist/agent-adapter/claude/event-parser.d.ts +28 -0
- package/dist/agent-adapter/claude/event-parser.js +173 -0
- package/dist/agent-adapter/claude/hooks-builder.d.ts +36 -0
- package/dist/agent-adapter/claude/hooks-builder.js +57 -0
- package/dist/agent-adapter/claude/jsonl-tail.d.ts +68 -0
- package/dist/agent-adapter/claude/jsonl-tail.js +344 -0
- package/dist/agent-adapter/claude/spawn-args.d.ts +38 -0
- package/dist/agent-adapter/claude/spawn-args.js +83 -0
- package/dist/agent-adapter/claude/tmux-control.d.ts +69 -0
- package/dist/agent-adapter/claude/tmux-control.js +127 -0
- package/dist/agent-adapter/claude/tool-summarizers.d.ts +2 -0
- package/dist/agent-adapter/claude/tool-summarizers.js +24 -0
- package/dist/agent-adapter/codex/adapter.d.ts +42 -0
- package/dist/agent-adapter/codex/adapter.js +916 -0
- package/dist/agent-adapter/codex/event-parser.d.ts +17 -0
- package/dist/agent-adapter/codex/event-parser.js +41 -0
- package/dist/agent-adapter/codex/spawn-args.d.ts +6 -0
- package/dist/agent-adapter/codex/spawn-args.js +30 -0
- package/dist/agent-adapter/index.d.ts +8 -0
- package/dist/agent-adapter/index.js +33 -0
- package/dist/agent-adapter/normalize/event-stream.d.ts +6 -0
- package/dist/agent-adapter/normalize/event-stream.js +47 -0
- package/dist/agent-adapter/normalize/event-types.d.ts +58 -0
- package/dist/agent-adapter/normalize/event-types.js +5 -0
- package/dist/agent-adapter/normalize/hooks.d.ts +26 -0
- package/dist/agent-adapter/normalize/hooks.js +5 -0
- package/dist/agent-adapter/normalize/prompt-builder.d.ts +6 -0
- package/dist/agent-adapter/normalize/prompt-builder.js +23 -0
- package/dist/agent-adapter/normalize/tool-names.d.ts +6 -0
- package/dist/agent-adapter/normalize/tool-names.js +73 -0
- package/dist/agent-adapter/pi/adapter.d.ts +60 -0
- package/dist/agent-adapter/pi/adapter.js +626 -0
- package/dist/agent-adapter/pi/agent-dir.d.ts +11 -0
- package/dist/agent-adapter/pi/agent-dir.js +34 -0
- package/dist/agent-adapter/pi/event-parser.d.ts +26 -0
- package/dist/agent-adapter/pi/event-parser.js +313 -0
- package/dist/agent-adapter/pi/framing.d.ts +6 -0
- package/dist/agent-adapter/pi/framing.js +35 -0
- package/dist/agent-adapter/pi/hook-bridge.d.ts +59 -0
- package/dist/agent-adapter/pi/hook-bridge.js +233 -0
- package/dist/agent-adapter/pi/mcp-bridge.d.ts +26 -0
- package/dist/agent-adapter/pi/mcp-bridge.js +140 -0
- package/dist/agent-adapter/pi/pi-ext-types.d.ts +90 -0
- package/dist/agent-adapter/pi/pi-ext-types.js +5 -0
- package/dist/agent-adapter/pi/spawn-args.d.ts +20 -0
- package/dist/agent-adapter/pi/spawn-args.js +54 -0
- package/dist/agent-adapter/pi/tool-shims.d.ts +2 -0
- package/dist/agent-adapter/pi/tool-shims.js +181 -0
- package/dist/agent-adapter/types.d.ts +94 -0
- package/dist/agent-adapter/types.js +5 -0
- package/dist/core/async-mutex.d.ts +6 -0
- package/dist/core/async-mutex.js +36 -0
- package/dist/core/cli-utils.d.ts +37 -0
- package/dist/core/cli-utils.js +69 -0
- package/dist/core/config-generator.d.ts +9 -0
- package/dist/core/config-generator.js +58 -0
- package/dist/core/gateway-generator.d.ts +46 -0
- package/dist/core/gateway-generator.js +336 -0
- package/dist/core/log.d.ts +7 -0
- package/dist/core/log.js +103 -0
- package/dist/core/paths.d.ts +32 -0
- package/dist/core/paths.js +49 -0
- package/dist/core/profile-generator.d.ts +65 -0
- package/dist/core/profile-generator.js +310 -0
- package/dist/core/running-executions.d.ts +88 -0
- package/dist/core/running-executions.js +186 -0
- package/dist/core/status-format.d.ts +18 -0
- package/dist/core/status-format.js +31 -0
- package/dist/core/task-parser.d.ts +69 -0
- package/dist/core/task-parser.js +423 -0
- package/dist/core/types/agent-types.d.ts +31 -0
- package/dist/core/types/agent-types.js +5 -0
- package/dist/core/types/thread-types.d.ts +261 -0
- package/dist/core/types/thread-types.js +5 -0
- package/dist/core/utils.d.ts +29 -0
- package/dist/core/utils.js +98 -0
- package/dist/core/version.d.ts +3 -0
- package/dist/core/version.js +12 -0
- package/dist/domain/agents/config.d.ts +58 -0
- package/dist/domain/agents/config.js +313 -0
- package/dist/domain/agents/facade.d.ts +52 -0
- package/dist/domain/agents/facade.js +264 -0
- package/dist/domain/agents/index.d.ts +2 -0
- package/dist/domain/agents/index.js +4 -0
- package/dist/domain/agents/profile-manager.d.ts +54 -0
- package/dist/domain/agents/profile-manager.js +161 -0
- package/dist/domain/costs/codex-event-format.d.ts +4 -0
- package/dist/domain/costs/codex-event-format.js +66 -0
- package/dist/domain/costs/codex-usage-monitor.d.ts +7 -0
- package/dist/domain/costs/codex-usage-monitor.js +72 -0
- package/dist/domain/costs/cost-tracker.d.ts +115 -0
- package/dist/domain/costs/cost-tracker.js +327 -0
- package/dist/domain/costs/gateway-manager.d.ts +6 -0
- package/dist/domain/costs/gateway-manager.js +228 -0
- package/dist/domain/costs/rate-limit-parser.d.ts +11 -0
- package/dist/domain/costs/rate-limit-parser.js +87 -0
- package/dist/domain/costs/rate-limit-throttle.d.ts +32 -0
- package/dist/domain/costs/rate-limit-throttle.js +129 -0
- package/dist/domain/executions/registry.d.ts +20 -0
- package/dist/domain/executions/registry.js +124 -0
- package/dist/domain/mcp/core-server.d.ts +2 -0
- package/dist/domain/mcp/core-server.js +34 -0
- package/dist/domain/mcp/server.d.ts +3 -0
- package/dist/domain/mcp/server.js +78 -0
- package/dist/domain/mcp/tools/context.d.ts +23 -0
- package/dist/domain/mcp/tools/context.js +51 -0
- package/dist/domain/mcp/tools/cortex-md.d.ts +17 -0
- package/dist/domain/mcp/tools/cortex-md.js +36 -0
- package/dist/domain/mcp/tools/cost.d.ts +2 -0
- package/dist/domain/mcp/tools/cost.js +54 -0
- package/dist/domain/mcp/tools/executions.d.ts +2 -0
- package/dist/domain/mcp/tools/executions.js +94 -0
- package/dist/domain/mcp/tools/schedule.d.ts +23 -0
- package/dist/domain/mcp/tools/schedule.js +245 -0
- package/dist/domain/mcp/tools/slack.d.ts +21 -0
- package/dist/domain/mcp/tools/slack.js +124 -0
- package/dist/domain/mcp/tools/task-ops.d.ts +2 -0
- package/dist/domain/mcp/tools/task-ops.js +509 -0
- package/dist/domain/mcp/tools/tui-ask.d.ts +31 -0
- package/dist/domain/mcp/tools/tui-ask.js +108 -0
- package/dist/domain/mcp/tools/tui-plan.d.ts +28 -0
- package/dist/domain/mcp/tools/tui-plan.js +118 -0
- package/dist/domain/mcp/tui-server.d.ts +2 -0
- package/dist/domain/mcp/tui-server.js +69 -0
- package/dist/domain/memory/consolidate.d.ts +55 -0
- package/dist/domain/memory/consolidate.js +471 -0
- package/dist/domain/memory/cortex-md-injector.d.ts +30 -0
- package/dist/domain/memory/cortex-md-injector.js +123 -0
- package/dist/domain/memory/cortex-md-scanner.d.ts +9 -0
- package/dist/domain/memory/cortex-md-scanner.js +67 -0
- package/dist/domain/memory/index-regen.d.ts +19 -0
- package/dist/domain/memory/index-regen.js +398 -0
- package/dist/domain/memory/rules-loader.d.ts +27 -0
- package/dist/domain/memory/rules-loader.js +97 -0
- package/dist/domain/memory/skill-scanner.d.ts +5 -0
- package/dist/domain/memory/skill-scanner.js +132 -0
- package/dist/domain/memory/user-context.d.ts +5 -0
- package/dist/domain/memory/user-context.js +34 -0
- package/dist/domain/memory/watcher.d.ts +2 -0
- package/dist/domain/memory/watcher.js +81 -0
- package/dist/domain/monitor/disk-monitor.d.ts +20 -0
- package/dist/domain/monitor/disk-monitor.js +97 -0
- package/dist/domain/monitor/gpu-monitor.d.ts +26 -0
- package/dist/domain/monitor/gpu-monitor.js +135 -0
- package/dist/domain/remote/client-bootstrap.d.ts +1 -0
- package/dist/domain/remote/client-bootstrap.js +139 -0
- package/dist/domain/remote/client-hot-reload.d.ts +18 -0
- package/dist/domain/remote/client-hot-reload.js +142 -0
- package/dist/domain/remote/client-manager.d.ts +50 -0
- package/dist/domain/remote/client-manager.js +452 -0
- package/dist/domain/remote/cortex-client.d.ts +1 -0
- package/dist/domain/remote/cortex-client.js +704 -0
- package/dist/domain/scheduling/job-registry.d.ts +31 -0
- package/dist/domain/scheduling/job-registry.js +34 -0
- package/dist/domain/scheduling/jobs/_shared.d.ts +14 -0
- package/dist/domain/scheduling/jobs/_shared.js +45 -0
- package/dist/domain/scheduling/jobs/memory-index-regen.d.ts +1 -0
- package/dist/domain/scheduling/jobs/memory-index-regen.js +16 -0
- package/dist/domain/scheduling/jobs/scheduled-task.d.ts +11 -0
- package/dist/domain/scheduling/jobs/scheduled-task.js +186 -0
- package/dist/domain/scheduling/jobs/target-dispatch.d.ts +41 -0
- package/dist/domain/scheduling/jobs/target-dispatch.js +48 -0
- package/dist/domain/scheduling/jobs/task-archive.d.ts +1 -0
- package/dist/domain/scheduling/jobs/task-archive.js +19 -0
- package/dist/domain/scheduling/jobs/task-dispatch.d.ts +12 -0
- package/dist/domain/scheduling/jobs/task-dispatch.js +214 -0
- package/dist/domain/scheduling/runner.d.ts +13 -0
- package/dist/domain/scheduling/runner.js +24 -0
- package/dist/domain/scheduling/schedule-cli.d.ts +16 -0
- package/dist/domain/scheduling/schedule-cli.js +268 -0
- package/dist/domain/scheduling/schedule-command.d.ts +4 -0
- package/dist/domain/scheduling/schedule-command.js +274 -0
- package/dist/domain/scheduling/scheduler.d.ts +86 -0
- package/dist/domain/scheduling/scheduler.js +544 -0
- package/dist/domain/sessions/session-backup.d.ts +49 -0
- package/dist/domain/sessions/session-backup.js +217 -0
- package/dist/domain/sessions/session-hooks.d.ts +103 -0
- package/dist/domain/sessions/session-hooks.js +311 -0
- package/dist/domain/sessions/session-registry.d.ts +21 -0
- package/dist/domain/sessions/session-registry.js +31 -0
- package/dist/domain/sessions/session.d.ts +3 -0
- package/dist/domain/sessions/session.js +14 -0
- package/dist/domain/tasks/archiver.d.ts +9 -0
- package/dist/domain/tasks/archiver.js +123 -0
- package/dist/domain/tasks/dispatch-utils.d.ts +37 -0
- package/dist/domain/tasks/dispatch-utils.js +143 -0
- package/dist/domain/tasks/dispatcher.d.ts +48 -0
- package/dist/domain/tasks/dispatcher.js +285 -0
- package/dist/domain/tasks/lint.d.ts +12 -0
- package/dist/domain/tasks/lint.js +102 -0
- package/dist/domain/tasks/mutator.d.ts +21 -0
- package/dist/domain/tasks/mutator.js +208 -0
- package/dist/domain/tasks/parser.d.ts +1 -0
- package/dist/domain/tasks/parser.js +6 -0
- package/dist/domain/tasks/pending-tracker.d.ts +67 -0
- package/dist/domain/tasks/pending-tracker.js +163 -0
- package/dist/domain/tasks/recommendation/extractor.d.ts +12 -0
- package/dist/domain/tasks/recommendation/extractor.js +563 -0
- package/dist/domain/tasks/recommendation/patterns.d.ts +11 -0
- package/dist/domain/tasks/recommendation/patterns.js +99 -0
- package/dist/domain/tasks/recommendation/types.d.ts +67 -0
- package/dist/domain/tasks/recommendation/types.js +5 -0
- package/dist/domain/tasks/store.d.ts +1 -0
- package/dist/domain/tasks/store.js +3 -0
- package/dist/domain/tasks/system/cortex-run.d.ts +24 -0
- package/dist/domain/tasks/system/cortex-run.js +342 -0
- package/dist/domain/tasks/system/task-cli.d.ts +49 -0
- package/dist/domain/tasks/system/task-cli.js +664 -0
- package/dist/domain/tasks/system/task-completion.d.ts +22 -0
- package/dist/domain/tasks/system/task-completion.js +89 -0
- package/dist/domain/tasks/system/task-id-utils.d.ts +24 -0
- package/dist/domain/tasks/system/task-id-utils.js +84 -0
- package/dist/domain/tasks/system/task-lifecycle-edit.d.ts +34 -0
- package/dist/domain/tasks/system/task-lifecycle-edit.js +208 -0
- package/dist/domain/tasks/system/task-lock.d.ts +25 -0
- package/dist/domain/tasks/system/task-lock.js +139 -0
- package/dist/domain/tasks/system/task-mutations.d.ts +31 -0
- package/dist/domain/tasks/system/task-mutations.js +110 -0
- package/dist/domain/tasks/system/task-process.d.ts +37 -0
- package/dist/domain/tasks/system/task-process.js +119 -0
- package/dist/domain/tasks/system/task-state.d.ts +70 -0
- package/dist/domain/tasks/system/task-state.js +176 -0
- package/dist/domain/threads/artifact-io.d.ts +18 -0
- package/dist/domain/threads/artifact-io.js +182 -0
- package/dist/domain/threads/auto-thread.d.ts +3 -0
- package/dist/domain/threads/auto-thread.js +19 -0
- package/dist/domain/threads/hook-runner.d.ts +4 -0
- package/dist/domain/threads/hook-runner.js +250 -0
- package/dist/domain/threads/index.d.ts +5 -0
- package/dist/domain/threads/index.js +7 -0
- package/dist/domain/threads/prompt-builder.d.ts +25 -0
- package/dist/domain/threads/prompt-builder.js +176 -0
- package/dist/domain/threads/runner.d.ts +83 -0
- package/dist/domain/threads/runner.js +434 -0
- package/dist/domain/threads/state-machine.d.ts +61 -0
- package/dist/domain/threads/state-machine.js +440 -0
- package/dist/domain/threads/template-loader.d.ts +20 -0
- package/dist/domain/threads/template-loader.js +189 -0
- package/dist/domain/threads/template-resolver.d.ts +1 -0
- package/dist/domain/threads/template-resolver.js +95 -0
- package/dist/domain/threads/utils.d.ts +19 -0
- package/dist/domain/threads/utils.js +49 -0
- package/dist/entry/app.d.ts +1 -0
- package/dist/entry/app.js +234 -0
- package/dist/entry/cli.d.ts +12 -0
- package/dist/entry/cli.js +254 -0
- package/dist/entry/daemon.d.ts +11 -0
- package/dist/entry/daemon.js +521 -0
- package/dist/entry/init.d.ts +93 -0
- package/dist/entry/init.js +1299 -0
- package/dist/entry/startup-helpers.d.ts +2 -0
- package/dist/entry/startup-helpers.js +56 -0
- package/dist/entry/startup-notify.d.ts +10 -0
- package/dist/entry/startup-notify.js +20 -0
- package/dist/events/event-bus.d.ts +42 -0
- package/dist/events/event-bus.js +84 -0
- package/dist/events/event-logger.d.ts +12 -0
- package/dist/events/event-logger.js +135 -0
- package/dist/events/event-replay.d.ts +1 -0
- package/dist/events/event-replay.js +51 -0
- package/dist/events/event-types.d.ts +145 -0
- package/dist/events/event-types.js +5 -0
- package/dist/events/index.d.ts +5 -0
- package/dist/events/index.js +6 -0
- package/dist/hooks/ask-user-question-hook.mjs +66 -0
- package/dist/hooks/cortex-md-injector.mjs +237 -0
- package/dist/hooks/exit-plan-mode-hook.mjs +84 -0
- package/dist/hooks/memory-ref-tracker.mjs +154 -0
- package/dist/hooks/new-session-hook.mjs +83 -0
- package/dist/hooks/post-task-hook.mjs +63 -0
- package/dist/hooks/rules-loader.mjs +212 -0
- package/dist/hooks/sensitive-file-edit.mjs +84 -0
- package/dist/hooks/session-activity-tracker.mjs +254 -0
- package/dist/hooks/task-status-check.mjs +218 -0
- package/dist/hooks/tasks-yaml-guard.mjs +230 -0
- package/dist/orchestration/agent-runner.d.ts +72 -0
- package/dist/orchestration/agent-runner.js +289 -0
- package/dist/orchestration/busy-tracker.d.ts +12 -0
- package/dist/orchestration/busy-tracker.js +35 -0
- package/dist/orchestration/channel-queue.d.ts +18 -0
- package/dist/orchestration/channel-queue.js +32 -0
- package/dist/orchestration/dispatch-reconciler.d.ts +1 -0
- package/dist/orchestration/dispatch-reconciler.js +16 -0
- package/dist/orchestration/durable-helpers.d.ts +4 -0
- package/dist/orchestration/durable-helpers.js +42 -0
- package/dist/orchestration/interactions/ask-user-question.d.ts +32 -0
- package/dist/orchestration/interactions/ask-user-question.js +182 -0
- package/dist/orchestration/interactions/command-action-router.d.ts +47 -0
- package/dist/orchestration/interactions/command-action-router.js +91 -0
- package/dist/orchestration/interactions/interaction-handlers.d.ts +5 -0
- package/dist/orchestration/interactions/interaction-handlers.js +279 -0
- package/dist/orchestration/interactions/plan-approvals.d.ts +47 -0
- package/dist/orchestration/interactions/plan-approvals.js +77 -0
- package/dist/orchestration/interactions/plan-handler.d.ts +6 -0
- package/dist/orchestration/interactions/plan-handler.js +12 -0
- package/dist/orchestration/lifecycle.d.ts +50 -0
- package/dist/orchestration/lifecycle.js +308 -0
- package/dist/orchestration/orchestrator.d.ts +29 -0
- package/dist/orchestration/orchestrator.js +25 -0
- package/dist/orchestration/routing/commands/cancel.d.ts +10 -0
- package/dist/orchestration/routing/commands/cancel.js +136 -0
- package/dist/orchestration/routing/commands/channel.d.ts +11 -0
- package/dist/orchestration/routing/commands/channel.js +219 -0
- package/dist/orchestration/routing/commands/command-context.d.ts +29 -0
- package/dist/orchestration/routing/commands/command-context.js +4 -0
- package/dist/orchestration/routing/commands/cost.d.ts +3 -0
- package/dist/orchestration/routing/commands/cost.js +22 -0
- package/dist/orchestration/routing/commands/device.d.ts +6 -0
- package/dist/orchestration/routing/commands/device.js +83 -0
- package/dist/orchestration/routing/commands/dispatch.d.ts +2 -0
- package/dist/orchestration/routing/commands/dispatch.js +49 -0
- package/dist/orchestration/routing/commands/index.d.ts +15 -0
- package/dist/orchestration/routing/commands/index.js +130 -0
- package/dist/orchestration/routing/commands/mode.d.ts +13 -0
- package/dist/orchestration/routing/commands/mode.js +290 -0
- package/dist/orchestration/routing/commands/nvtop.d.ts +3 -0
- package/dist/orchestration/routing/commands/nvtop.js +103 -0
- package/dist/orchestration/routing/commands/orient.d.ts +2 -0
- package/dist/orchestration/routing/commands/orient.js +3 -0
- package/dist/orchestration/routing/commands/schedule.d.ts +4 -0
- package/dist/orchestration/routing/commands/schedule.js +132 -0
- package/dist/orchestration/routing/commands/sendfile.d.ts +2 -0
- package/dist/orchestration/routing/commands/sendfile.js +92 -0
- package/dist/orchestration/routing/commands/session.d.ts +9 -0
- package/dist/orchestration/routing/commands/session.js +153 -0
- package/dist/orchestration/routing/commands/status.d.ts +7 -0
- package/dist/orchestration/routing/commands/status.js +180 -0
- package/dist/orchestration/routing/commands/tail.d.ts +2 -0
- package/dist/orchestration/routing/commands/tail.js +73 -0
- package/dist/orchestration/routing/commands/task.d.ts +6 -0
- package/dist/orchestration/routing/commands/task.js +142 -0
- package/dist/orchestration/routing/commands/thread-handlers.d.ts +2 -0
- package/dist/orchestration/routing/commands/thread-handlers.js +138 -0
- package/dist/orchestration/routing/commands/thread.d.ts +2 -0
- package/dist/orchestration/routing/commands/thread.js +2 -0
- package/dist/orchestration/routing/edit-handler.d.ts +24 -0
- package/dist/orchestration/routing/edit-handler.js +161 -0
- package/dist/orchestration/routing/file-handler.d.ts +6 -0
- package/dist/orchestration/routing/file-handler.js +23 -0
- package/dist/orchestration/routing/hook-bridge-subscribers.d.ts +4 -0
- package/dist/orchestration/routing/hook-bridge-subscribers.js +79 -0
- package/dist/orchestration/routing/hook-bridge.d.ts +30 -0
- package/dist/orchestration/routing/hook-bridge.js +112 -0
- package/dist/orchestration/routing/message-router.d.ts +6 -0
- package/dist/orchestration/routing/message-router.js +70 -0
- package/dist/orchestration/routing/webhook.d.ts +10 -0
- package/dist/orchestration/routing/webhook.js +239 -0
- package/dist/orchestration/status-helpers.d.ts +66 -0
- package/dist/orchestration/status-helpers.js +222 -0
- package/dist/orchestration/superseded-edits.d.ts +22 -0
- package/dist/orchestration/superseded-edits.js +31 -0
- package/dist/orchestration/thread-executor.d.ts +31 -0
- package/dist/orchestration/thread-executor.js +234 -0
- package/dist/platform/adapter.d.ts +28 -0
- package/dist/platform/adapter.js +5 -0
- package/dist/platform/adapters/feishu.d.ts +66 -0
- package/dist/platform/adapters/feishu.js +558 -0
- package/dist/platform/adapters/index.d.ts +16 -0
- package/dist/platform/adapters/index.js +65 -0
- package/dist/platform/adapters/slack.d.ts +90 -0
- package/dist/platform/adapters/slack.js +630 -0
- package/dist/platform/index.d.ts +12 -0
- package/dist/platform/index.js +10 -0
- package/dist/platform/interactive-builder.d.ts +27 -0
- package/dist/platform/interactive-builder.js +120 -0
- package/dist/platform/testing.d.ts +100 -0
- package/dist/platform/testing.js +190 -0
- package/dist/platform/tool-trace.d.ts +33 -0
- package/dist/platform/tool-trace.js +165 -0
- package/dist/platform/types.d.ts +176 -0
- package/dist/platform/types.js +5 -0
- package/dist/platform/utils/rate-limiter.d.ts +92 -0
- package/dist/platform/utils/rate-limiter.js +213 -0
- package/dist/platform/virtual-message.d.ts +68 -0
- package/dist/platform/virtual-message.js +464 -0
- package/dist/store/atomic-write.d.ts +6 -0
- package/dist/store/atomic-write.js +18 -0
- package/dist/store/channel-repo.d.ts +21 -0
- package/dist/store/channel-repo.js +55 -0
- package/dist/store/conversation-ledger-repo.d.ts +86 -0
- package/dist/store/conversation-ledger-repo.js +220 -0
- package/dist/store/cost-repo.d.ts +57 -0
- package/dist/store/cost-repo.js +135 -0
- package/dist/store/execution-repo.d.ts +172 -0
- package/dist/store/execution-repo.js +343 -0
- package/dist/store/in-memory-repository.d.ts +14 -0
- package/dist/store/in-memory-repository.js +31 -0
- package/dist/store/json-repository.d.ts +51 -0
- package/dist/store/json-repository.js +118 -0
- package/dist/store/outbound-queue.d.ts +65 -0
- package/dist/store/outbound-queue.js +269 -0
- package/dist/store/profile-repo.d.ts +36 -0
- package/dist/store/profile-repo.js +144 -0
- package/dist/store/project-dir-repo.d.ts +22 -0
- package/dist/store/project-dir-repo.js +60 -0
- package/dist/store/schedule-repo.d.ts +92 -0
- package/dist/store/schedule-repo.js +88 -0
- package/dist/store/session-registry-repo.d.ts +38 -0
- package/dist/store/session-registry-repo.js +96 -0
- package/dist/store/session-repo.d.ts +14 -0
- package/dist/store/session-repo.js +56 -0
- package/dist/store/task-repo.d.ts +41 -0
- package/dist/store/task-repo.js +137 -0
- package/dist/store/thread-repo.d.ts +50 -0
- package/dist/store/thread-repo.js +190 -0
- package/package.json +58 -0
- package/scripts/postinstall-restart-trigger.mjs +39 -0
|
@@ -0,0 +1,558 @@
|
|
|
1
|
+
// input: @larksuiteoapi/node-sdk, ../adapter.js, ../types.js
|
|
2
|
+
// output: FeishuAdapter + FeishuAdapterConfig
|
|
3
|
+
// pos: Feishu platform PlatformAdapter implementation
|
|
4
|
+
// >>> If I am updated, update my header comment and the parent folder's CORTEX.md <<<
|
|
5
|
+
import * as lark from '@larksuiteoapi/node-sdk';
|
|
6
|
+
import * as fs from 'fs';
|
|
7
|
+
import * as path from 'path';
|
|
8
|
+
export class FeishuAdapter {
|
|
9
|
+
name = 'feishu';
|
|
10
|
+
capabilities = {
|
|
11
|
+
threads: true, // via reply API with reply_in_thread
|
|
12
|
+
messageEdit: false, // Feishu has no message_changed event
|
|
13
|
+
modals: false, // No native modal; openModal() degrades to card form
|
|
14
|
+
reactions: true, // im.v1.messageReaction.create
|
|
15
|
+
ephemeral: false, // Feishu has no ephemeral messages
|
|
16
|
+
fileUpload: true, // im.v1.file.create + message
|
|
17
|
+
richFormatting: true, // Message cards with markdown
|
|
18
|
+
maxMessageLength: 4000, // Card 30KB limit, ~4000 chars safe
|
|
19
|
+
maxThreadDepth: 1, // UI shows 1 level
|
|
20
|
+
};
|
|
21
|
+
client;
|
|
22
|
+
wsClient;
|
|
23
|
+
eventDispatcher;
|
|
24
|
+
config;
|
|
25
|
+
messageHandler = null;
|
|
26
|
+
actionHandlers = new Map();
|
|
27
|
+
modalHandlers = new Map();
|
|
28
|
+
constructor(config) {
|
|
29
|
+
this.config = config;
|
|
30
|
+
const domain = config.domain === 'lark' ? lark.Domain.Lark : lark.Domain.Feishu;
|
|
31
|
+
this.client = new lark.Client({
|
|
32
|
+
appId: config.appId,
|
|
33
|
+
appSecret: config.appSecret,
|
|
34
|
+
appType: lark.AppType.SelfBuild,
|
|
35
|
+
domain,
|
|
36
|
+
});
|
|
37
|
+
// EventDispatcher handles both regular events and card action callbacks
|
|
38
|
+
// via WebSocket long connection.
|
|
39
|
+
this.eventDispatcher = new lark.EventDispatcher({
|
|
40
|
+
encryptKey: config.encryptKey || '',
|
|
41
|
+
verificationToken: config.verificationToken || '',
|
|
42
|
+
});
|
|
43
|
+
// Register message receive event
|
|
44
|
+
this.eventDispatcher.register({
|
|
45
|
+
'im.message.receive_v1': async (data) => {
|
|
46
|
+
if (!this.messageHandler)
|
|
47
|
+
return;
|
|
48
|
+
await this.handleIncomingMessage(data);
|
|
49
|
+
},
|
|
50
|
+
});
|
|
51
|
+
// Register card action callback — routes to actionHandlers or modalHandlers
|
|
52
|
+
this.eventDispatcher.register({
|
|
53
|
+
'card.action.trigger': async (data) => {
|
|
54
|
+
return await this.handleCardAction(data);
|
|
55
|
+
},
|
|
56
|
+
});
|
|
57
|
+
this.wsClient = new lark.WSClient({
|
|
58
|
+
appId: config.appId,
|
|
59
|
+
appSecret: config.appSecret,
|
|
60
|
+
domain,
|
|
61
|
+
loggerLevel: lark.LoggerLevel.info,
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
// --- Lifecycle ---
|
|
65
|
+
async start() {
|
|
66
|
+
await this.wsClient.start({
|
|
67
|
+
eventDispatcher: this.eventDispatcher,
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
async stop() {
|
|
71
|
+
this.wsClient.close();
|
|
72
|
+
}
|
|
73
|
+
// --- Event registration ---
|
|
74
|
+
onMessage(handler) {
|
|
75
|
+
this.messageHandler = handler;
|
|
76
|
+
}
|
|
77
|
+
onMessageEdit(_handler) {
|
|
78
|
+
// Feishu does not support message edit events — no-op.
|
|
79
|
+
}
|
|
80
|
+
onAction(actionId, handler) {
|
|
81
|
+
this.actionHandlers.set(actionId, handler);
|
|
82
|
+
}
|
|
83
|
+
onModalSubmit(callbackId, handler) {
|
|
84
|
+
this.modalHandlers.set(callbackId, handler);
|
|
85
|
+
}
|
|
86
|
+
// --- Outbound messaging ---
|
|
87
|
+
async postMessage(channel, content, opts) {
|
|
88
|
+
const threadId = opts?.threadId;
|
|
89
|
+
// If replying in a thread, use the reply API
|
|
90
|
+
if (threadId) {
|
|
91
|
+
return this.replyInThread(threadId, content);
|
|
92
|
+
}
|
|
93
|
+
const { msgType, msgContent } = this.buildMessagePayload(content);
|
|
94
|
+
const res = await this.client.im.v1.message.create({
|
|
95
|
+
params: { receive_id_type: 'chat_id' },
|
|
96
|
+
data: {
|
|
97
|
+
receive_id: channel,
|
|
98
|
+
msg_type: msgType,
|
|
99
|
+
content: msgContent,
|
|
100
|
+
},
|
|
101
|
+
});
|
|
102
|
+
const messageId = res?.data?.message_id || '';
|
|
103
|
+
return { channel, messageId };
|
|
104
|
+
}
|
|
105
|
+
async updateMessage(ref, content) {
|
|
106
|
+
// Feishu only supports updating interactive (card) messages via PATCH.
|
|
107
|
+
const cardJson = this.buildCardJson(content);
|
|
108
|
+
await this.client.im.v1.message.patch({
|
|
109
|
+
path: { message_id: ref.messageId },
|
|
110
|
+
data: { content: JSON.stringify(cardJson) },
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
async deleteMessage(ref) {
|
|
114
|
+
await this.client.im.v1.message.delete({
|
|
115
|
+
path: { message_id: ref.messageId },
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
// --- Interactive messages ---
|
|
119
|
+
async postInteractive(channel, content, opts) {
|
|
120
|
+
const elements = content.richBlocks
|
|
121
|
+
? this.richBlocksToFeishuElements(content.richBlocks)
|
|
122
|
+
: [];
|
|
123
|
+
// Add action buttons
|
|
124
|
+
elements.push({
|
|
125
|
+
tag: 'action',
|
|
126
|
+
actions: content.actions.map(a => this.actionElementToFeishu(a)),
|
|
127
|
+
});
|
|
128
|
+
const cardJson = {
|
|
129
|
+
schema: '2.0',
|
|
130
|
+
header: {
|
|
131
|
+
title: { tag: 'plain_text', content: content.text },
|
|
132
|
+
},
|
|
133
|
+
body: { elements },
|
|
134
|
+
};
|
|
135
|
+
const threadId = opts?.threadId;
|
|
136
|
+
if (threadId) {
|
|
137
|
+
const res = await this.client.im.v1.message.reply({
|
|
138
|
+
path: { message_id: threadId },
|
|
139
|
+
data: {
|
|
140
|
+
msg_type: 'interactive',
|
|
141
|
+
content: JSON.stringify(cardJson),
|
|
142
|
+
},
|
|
143
|
+
});
|
|
144
|
+
const messageId = res?.data?.message_id || '';
|
|
145
|
+
return { channel, messageId, threadId };
|
|
146
|
+
}
|
|
147
|
+
const res = await this.client.im.v1.message.create({
|
|
148
|
+
params: { receive_id_type: 'chat_id' },
|
|
149
|
+
data: {
|
|
150
|
+
receive_id: channel,
|
|
151
|
+
msg_type: 'interactive',
|
|
152
|
+
content: JSON.stringify(cardJson),
|
|
153
|
+
},
|
|
154
|
+
});
|
|
155
|
+
const messageId = res?.data?.message_id || '';
|
|
156
|
+
return { channel, messageId };
|
|
157
|
+
}
|
|
158
|
+
// --- Modals (degraded to card forms) ---
|
|
159
|
+
async openModal(triggerId, modal) {
|
|
160
|
+
// Feishu has no native modal. Degrade to posting a card with an embedded
|
|
161
|
+
// form container to the channel. triggerId format: "chatId:messageId"
|
|
162
|
+
const [channel] = triggerId.split(':');
|
|
163
|
+
if (!channel)
|
|
164
|
+
return;
|
|
165
|
+
const cardJson = this.modalToFeishuCard(modal);
|
|
166
|
+
await this.client.im.v1.message.create({
|
|
167
|
+
params: { receive_id_type: 'chat_id' },
|
|
168
|
+
data: {
|
|
169
|
+
receive_id: channel,
|
|
170
|
+
msg_type: 'interactive',
|
|
171
|
+
content: JSON.stringify(cardJson),
|
|
172
|
+
},
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
// --- Reactions ---
|
|
176
|
+
async addReaction(ref, emoji) {
|
|
177
|
+
try {
|
|
178
|
+
await this.client.im.v1.messageReaction.create({
|
|
179
|
+
path: { message_id: ref.messageId },
|
|
180
|
+
data: { reaction_type: { emoji_type: emoji } },
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
catch {
|
|
184
|
+
// Best-effort: emoji name may not map to Feishu's emoji set
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
// --- Files ---
|
|
188
|
+
async uploadFile(channel, filePath, opts) {
|
|
189
|
+
const { resolved, size } = this.resolveFilePath(filePath);
|
|
190
|
+
const fileName = opts?.filename || path.basename(resolved);
|
|
191
|
+
// Upload file to get file_key
|
|
192
|
+
const uploadRes = await this.client.im.v1.file.create({
|
|
193
|
+
data: {
|
|
194
|
+
file_type: this.inferFeishuFileType(fileName),
|
|
195
|
+
file_name: fileName,
|
|
196
|
+
file: fs.readFileSync(resolved),
|
|
197
|
+
},
|
|
198
|
+
});
|
|
199
|
+
const fileKey = uploadRes?.data?.file_key;
|
|
200
|
+
if (!fileKey)
|
|
201
|
+
throw new Error('Feishu file upload failed: no file_key returned');
|
|
202
|
+
// Send file message
|
|
203
|
+
const msgContent = JSON.stringify({ file_key: fileKey });
|
|
204
|
+
const threadId = opts?.threadId;
|
|
205
|
+
if (threadId) {
|
|
206
|
+
await this.client.im.v1.message.reply({
|
|
207
|
+
path: { message_id: threadId },
|
|
208
|
+
data: { msg_type: 'file', content: msgContent },
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
else {
|
|
212
|
+
await this.client.im.v1.message.create({
|
|
213
|
+
params: { receive_id_type: 'chat_id' },
|
|
214
|
+
data: {
|
|
215
|
+
receive_id: channel,
|
|
216
|
+
msg_type: 'file',
|
|
217
|
+
content: msgContent,
|
|
218
|
+
},
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
async downloadFile(fileRef, destDir) {
|
|
223
|
+
const resp = await this.client.im.v1.file.get({
|
|
224
|
+
path: { file_key: fileRef.id },
|
|
225
|
+
});
|
|
226
|
+
const ext = path.extname(fileRef.name) || '';
|
|
227
|
+
const localPath = path.join(destDir, `${fileRef.id}${ext}`);
|
|
228
|
+
await resp.writeFile(localPath);
|
|
229
|
+
return { localPath, mimetype: fileRef.mimetype, name: fileRef.name };
|
|
230
|
+
}
|
|
231
|
+
// --- Misc ---
|
|
232
|
+
async getPermalink(_ref) {
|
|
233
|
+
// Feishu has no public permalink API.
|
|
234
|
+
return null;
|
|
235
|
+
}
|
|
236
|
+
async postEphemeral(_channel, _userId, _text) {
|
|
237
|
+
// Feishu does not support ephemeral messages — no-op.
|
|
238
|
+
}
|
|
239
|
+
getAdminChannel() {
|
|
240
|
+
return this.config.adminChannel || null;
|
|
241
|
+
}
|
|
242
|
+
getRawClient() {
|
|
243
|
+
return this.client;
|
|
244
|
+
}
|
|
245
|
+
// =========================================================================
|
|
246
|
+
// Internal: Inbound event handlers
|
|
247
|
+
// =========================================================================
|
|
248
|
+
async handleIncomingMessage(data) {
|
|
249
|
+
if (!this.messageHandler)
|
|
250
|
+
return;
|
|
251
|
+
const message = data.message || data;
|
|
252
|
+
const sender = data.sender || {};
|
|
253
|
+
const senderId = sender.sender_id?.open_id || sender.sender_id?.user_id || '';
|
|
254
|
+
const isBot = sender.sender_type === 'app';
|
|
255
|
+
// Parse message content (JSON string)
|
|
256
|
+
let text = '';
|
|
257
|
+
try {
|
|
258
|
+
const parsed = JSON.parse(message.content || '{}');
|
|
259
|
+
text = parsed.text || '';
|
|
260
|
+
}
|
|
261
|
+
catch {
|
|
262
|
+
text = message.content || '';
|
|
263
|
+
}
|
|
264
|
+
const chatId = message.chat_id || '';
|
|
265
|
+
const messageId = message.message_id || '';
|
|
266
|
+
const rootId = message.root_id || undefined;
|
|
267
|
+
const parentId = message.parent_id || undefined;
|
|
268
|
+
const ref = {
|
|
269
|
+
channel: chatId,
|
|
270
|
+
messageId,
|
|
271
|
+
threadId: rootId || parentId || undefined,
|
|
272
|
+
};
|
|
273
|
+
// Extract file references if present
|
|
274
|
+
const files = message.file_key
|
|
275
|
+
? [{ id: message.file_key, name: message.file_name || '', mimetype: message.file_type || '', url: '', raw: message }]
|
|
276
|
+
: undefined;
|
|
277
|
+
// TODO: Parse Feishu forwarded/merged messages (merge_forward msg type) into
|
|
278
|
+
// IncomingAttachment[]. Feishu's format differs from Slack's msg.attachments.
|
|
279
|
+
const incoming = {
|
|
280
|
+
ref,
|
|
281
|
+
text,
|
|
282
|
+
senderId,
|
|
283
|
+
isBot,
|
|
284
|
+
files,
|
|
285
|
+
subtype: message.message_type,
|
|
286
|
+
raw: data,
|
|
287
|
+
};
|
|
288
|
+
const adapter = this;
|
|
289
|
+
await this.messageHandler({
|
|
290
|
+
message: incoming,
|
|
291
|
+
async reply(content, replyOpts) {
|
|
292
|
+
return adapter.postMessage(ref.channel, content, {
|
|
293
|
+
threadId: replyOpts?.threadId || ref.threadId,
|
|
294
|
+
});
|
|
295
|
+
},
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
async handleCardAction(data) {
|
|
299
|
+
const event = data.event || data;
|
|
300
|
+
const action = event.action || {};
|
|
301
|
+
const operator = event.operator || {};
|
|
302
|
+
const context = event.context || {};
|
|
303
|
+
const chatId = context.open_chat_id || '';
|
|
304
|
+
const messageId = context.open_message_id || '';
|
|
305
|
+
const userId = operator.open_id || '';
|
|
306
|
+
// Check if this is a form submission (has form_value)
|
|
307
|
+
if (action.form_value && typeof action.form_value === 'object') {
|
|
308
|
+
// Form submission → route to modalHandlers
|
|
309
|
+
// The form name serves as the callbackId
|
|
310
|
+
const formName = action.name || '';
|
|
311
|
+
const handler = this.modalHandlers.get(formName);
|
|
312
|
+
if (handler) {
|
|
313
|
+
const normalizedValues = this.normalizeFormValues(action.form_value);
|
|
314
|
+
let ackCalled = false;
|
|
315
|
+
await handler({
|
|
316
|
+
callbackId: formName,
|
|
317
|
+
privateMetadata: JSON.stringify(action.value || {}),
|
|
318
|
+
values: normalizedValues,
|
|
319
|
+
userId,
|
|
320
|
+
async ack(_response) {
|
|
321
|
+
ackCalled = true;
|
|
322
|
+
// Feishu ack is implicit via response — nothing to do
|
|
323
|
+
},
|
|
324
|
+
});
|
|
325
|
+
// Return toast or updated card if needed
|
|
326
|
+
return { toast: { type: 'success', content: 'OK' } };
|
|
327
|
+
}
|
|
328
|
+
return undefined;
|
|
329
|
+
}
|
|
330
|
+
// Button click → route to actionHandlers
|
|
331
|
+
// actionId can be in action.value.actionId (our convention) or action.name
|
|
332
|
+
const actionId = action.value?.actionId || action.name || '';
|
|
333
|
+
const actionValue = action.value?.value ?? JSON.stringify(action.value || {});
|
|
334
|
+
const handler = this.actionHandlers.get(actionId);
|
|
335
|
+
if (handler) {
|
|
336
|
+
await handler({
|
|
337
|
+
actionId,
|
|
338
|
+
value: typeof actionValue === 'string' ? actionValue : JSON.stringify(actionValue),
|
|
339
|
+
triggerId: `${chatId}:${messageId}`,
|
|
340
|
+
messageRef: messageId ? { channel: chatId, messageId } : undefined,
|
|
341
|
+
userId,
|
|
342
|
+
channelId: chatId,
|
|
343
|
+
});
|
|
344
|
+
return { toast: { type: 'success', content: 'OK' } };
|
|
345
|
+
}
|
|
346
|
+
return undefined;
|
|
347
|
+
}
|
|
348
|
+
// =========================================================================
|
|
349
|
+
// Internal: Outbound converters
|
|
350
|
+
// =========================================================================
|
|
351
|
+
buildMessagePayload(content) {
|
|
352
|
+
if (content.richBlocks && content.richBlocks.length > 0) {
|
|
353
|
+
const cardJson = this.buildCardJson(content);
|
|
354
|
+
return { msgType: 'interactive', msgContent: JSON.stringify(cardJson) };
|
|
355
|
+
}
|
|
356
|
+
return { msgType: 'text', msgContent: JSON.stringify({ text: content.text }) };
|
|
357
|
+
}
|
|
358
|
+
buildCardJson(content) {
|
|
359
|
+
const elements = content.richBlocks
|
|
360
|
+
? this.richBlocksToFeishuElements(content.richBlocks)
|
|
361
|
+
: [{ tag: 'markdown', content: content.text }];
|
|
362
|
+
return {
|
|
363
|
+
schema: '2.0',
|
|
364
|
+
body: { elements },
|
|
365
|
+
};
|
|
366
|
+
}
|
|
367
|
+
async replyInThread(threadMessageId, content) {
|
|
368
|
+
const { msgType, msgContent } = this.buildMessagePayload(content);
|
|
369
|
+
const res = await this.client.im.v1.message.reply({
|
|
370
|
+
path: { message_id: threadMessageId },
|
|
371
|
+
data: {
|
|
372
|
+
msg_type: msgType,
|
|
373
|
+
content: msgContent,
|
|
374
|
+
},
|
|
375
|
+
});
|
|
376
|
+
const messageId = res?.data?.message_id || '';
|
|
377
|
+
return { channel: '', messageId, threadId: threadMessageId };
|
|
378
|
+
}
|
|
379
|
+
/** Convert RichBlock[] to Feishu card schema 2.0 elements. */
|
|
380
|
+
richBlocksToFeishuElements(blocks) {
|
|
381
|
+
return blocks.map(block => {
|
|
382
|
+
switch (block.type) {
|
|
383
|
+
case 'markdown':
|
|
384
|
+
return { tag: 'markdown', content: block.text };
|
|
385
|
+
case 'section':
|
|
386
|
+
return {
|
|
387
|
+
tag: 'div',
|
|
388
|
+
text: {
|
|
389
|
+
tag: block.format === 'plain' ? 'plain_text' : 'lark_md',
|
|
390
|
+
content: block.text,
|
|
391
|
+
},
|
|
392
|
+
};
|
|
393
|
+
case 'context':
|
|
394
|
+
return {
|
|
395
|
+
tag: 'note',
|
|
396
|
+
elements: [{ tag: 'plain_text', content: block.text }],
|
|
397
|
+
};
|
|
398
|
+
case 'divider':
|
|
399
|
+
return { tag: 'hr' };
|
|
400
|
+
case 'actions':
|
|
401
|
+
return {
|
|
402
|
+
tag: 'action',
|
|
403
|
+
actions: block.elements.map(e => this.actionElementToFeishu(e)),
|
|
404
|
+
};
|
|
405
|
+
default:
|
|
406
|
+
return { tag: 'markdown', content: block.text || '' };
|
|
407
|
+
}
|
|
408
|
+
});
|
|
409
|
+
}
|
|
410
|
+
/** Convert a ButtonElement to Feishu card button JSON. */
|
|
411
|
+
actionElementToFeishu(el) {
|
|
412
|
+
return {
|
|
413
|
+
tag: 'button',
|
|
414
|
+
text: { tag: 'plain_text', content: el.text },
|
|
415
|
+
type: el.style === 'danger' ? 'danger' : el.style === 'primary' ? 'primary' : 'default',
|
|
416
|
+
name: el.actionId,
|
|
417
|
+
value: { actionId: el.actionId, value: el.value },
|
|
418
|
+
};
|
|
419
|
+
}
|
|
420
|
+
/**
|
|
421
|
+
* Convert ModalDefinition to a Feishu card with embedded form container.
|
|
422
|
+
* Since Feishu has no native modal, we post a card message containing a
|
|
423
|
+
* form that users can fill out and submit inline.
|
|
424
|
+
*/
|
|
425
|
+
modalToFeishuCard(modal) {
|
|
426
|
+
const formElements = [];
|
|
427
|
+
for (const field of modal.fields) {
|
|
428
|
+
switch (field.type) {
|
|
429
|
+
case 'section':
|
|
430
|
+
formElements.push({
|
|
431
|
+
tag: 'div',
|
|
432
|
+
text: { tag: 'lark_md', content: field.text },
|
|
433
|
+
});
|
|
434
|
+
break;
|
|
435
|
+
case 'select':
|
|
436
|
+
formElements.push({
|
|
437
|
+
tag: 'select_static',
|
|
438
|
+
name: `${field.blockId}::${field.actionId}`,
|
|
439
|
+
placeholder: field.placeholder
|
|
440
|
+
? { tag: 'plain_text', content: field.placeholder }
|
|
441
|
+
: undefined,
|
|
442
|
+
options: field.options.map(o => ({
|
|
443
|
+
text: { tag: 'plain_text', content: o.label },
|
|
444
|
+
value: o.value,
|
|
445
|
+
})),
|
|
446
|
+
});
|
|
447
|
+
break;
|
|
448
|
+
case 'multi_select':
|
|
449
|
+
formElements.push({
|
|
450
|
+
tag: 'multi_select_static',
|
|
451
|
+
name: `${field.blockId}::${field.actionId}`,
|
|
452
|
+
placeholder: field.placeholder
|
|
453
|
+
? { tag: 'plain_text', content: field.placeholder }
|
|
454
|
+
: undefined,
|
|
455
|
+
options: field.options.map(o => ({
|
|
456
|
+
text: { tag: 'plain_text', content: o.label },
|
|
457
|
+
value: o.value,
|
|
458
|
+
})),
|
|
459
|
+
});
|
|
460
|
+
break;
|
|
461
|
+
case 'text_input':
|
|
462
|
+
formElements.push({
|
|
463
|
+
tag: 'input',
|
|
464
|
+
name: `${field.blockId}::${field.actionId}`,
|
|
465
|
+
placeholder: field.placeholder
|
|
466
|
+
? { tag: 'plain_text', content: field.placeholder }
|
|
467
|
+
: undefined,
|
|
468
|
+
});
|
|
469
|
+
break;
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
// Add submit button inside form
|
|
473
|
+
formElements.push({
|
|
474
|
+
tag: 'button',
|
|
475
|
+
text: { tag: 'plain_text', content: modal.submitLabel || 'Submit' },
|
|
476
|
+
type: 'primary',
|
|
477
|
+
name: 'submit',
|
|
478
|
+
action_type: 'form_submit',
|
|
479
|
+
});
|
|
480
|
+
return {
|
|
481
|
+
schema: '2.0',
|
|
482
|
+
header: {
|
|
483
|
+
title: { tag: 'plain_text', content: modal.title },
|
|
484
|
+
},
|
|
485
|
+
body: {
|
|
486
|
+
elements: [{
|
|
487
|
+
tag: 'form',
|
|
488
|
+
name: modal.callbackId,
|
|
489
|
+
elements: formElements,
|
|
490
|
+
}],
|
|
491
|
+
},
|
|
492
|
+
config: {
|
|
493
|
+
update_multi: true, // Allow card updates after interaction
|
|
494
|
+
},
|
|
495
|
+
};
|
|
496
|
+
}
|
|
497
|
+
/**
|
|
498
|
+
* Normalize Feishu form_value into the platform-agnostic ModalFieldValue format.
|
|
499
|
+
* Feishu form_value is a flat dict { field_name: value }.
|
|
500
|
+
* We reconstruct the two-level { blockId: { actionId: ModalFieldValue } } structure.
|
|
501
|
+
*/
|
|
502
|
+
normalizeFormValues(formValue) {
|
|
503
|
+
const normalized = {};
|
|
504
|
+
for (const [fieldName, rawValue] of Object.entries(formValue)) {
|
|
505
|
+
// Field names follow our convention: "{blockId}::{actionId}"
|
|
506
|
+
const sepIdx = fieldName.indexOf('::');
|
|
507
|
+
const blockId = sepIdx >= 0 ? fieldName.substring(0, sepIdx) : fieldName;
|
|
508
|
+
const actionId = sepIdx >= 0 ? fieldName.substring(sepIdx + 2) : fieldName;
|
|
509
|
+
if (!normalized[blockId])
|
|
510
|
+
normalized[blockId] = {};
|
|
511
|
+
if (Array.isArray(rawValue)) {
|
|
512
|
+
// Multi-select: array of selected values
|
|
513
|
+
normalized[blockId][actionId] = {
|
|
514
|
+
selectedOptions: rawValue.map(v => ({ value: String(v) })),
|
|
515
|
+
};
|
|
516
|
+
}
|
|
517
|
+
else if (typeof rawValue === 'string') {
|
|
518
|
+
// Could be a text input value or a single select value
|
|
519
|
+
// Heuristic: if it looks like a select option (short, no spaces), treat as selectedOption
|
|
520
|
+
// Otherwise treat as text input
|
|
521
|
+
normalized[blockId][actionId] = { value: rawValue };
|
|
522
|
+
}
|
|
523
|
+
else if (rawValue && typeof rawValue === 'object') {
|
|
524
|
+
// Selected option object
|
|
525
|
+
normalized[blockId][actionId] = {
|
|
526
|
+
selectedOption: { value: String(rawValue.value || rawValue) },
|
|
527
|
+
};
|
|
528
|
+
}
|
|
529
|
+
else {
|
|
530
|
+
normalized[blockId][actionId] = { value: String(rawValue ?? '') };
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
return normalized;
|
|
534
|
+
}
|
|
535
|
+
// =========================================================================
|
|
536
|
+
// Internal: Utilities
|
|
537
|
+
// =========================================================================
|
|
538
|
+
resolveFilePath(filePath) {
|
|
539
|
+
const resolved = path.isAbsolute(filePath)
|
|
540
|
+
? filePath
|
|
541
|
+
: path.resolve(process.cwd(), filePath);
|
|
542
|
+
if (!fs.existsSync(resolved))
|
|
543
|
+
throw new Error(`File not found: ${resolved}`);
|
|
544
|
+
const stat = fs.statSync(resolved);
|
|
545
|
+
if (!stat.isFile())
|
|
546
|
+
throw new Error(`Not a file: ${resolved}`);
|
|
547
|
+
return { resolved, size: stat.size };
|
|
548
|
+
}
|
|
549
|
+
inferFeishuFileType(fileName) {
|
|
550
|
+
const ext = path.extname(fileName).toLowerCase();
|
|
551
|
+
const typeMap = {
|
|
552
|
+
'.opus': 'opus', '.mp4': 'mp4', '.pdf': 'pdf', '.doc': 'doc',
|
|
553
|
+
'.docx': 'doc', '.xls': 'xls', '.xlsx': 'xls', '.ppt': 'ppt',
|
|
554
|
+
'.pptx': 'ppt',
|
|
555
|
+
};
|
|
556
|
+
return typeMap[ext] || 'stream';
|
|
557
|
+
}
|
|
558
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { PlatformAdapter } from '../adapter.js';
|
|
2
|
+
import type { SlackAdapterConfig } from './slack.js';
|
|
3
|
+
import type { FeishuAdapterConfig } from './feishu.js';
|
|
4
|
+
export type PlatformType = 'slack' | 'discord' | 'telegram' | 'feishu' | 'test';
|
|
5
|
+
export interface AdapterConfig {
|
|
6
|
+
platform: PlatformType;
|
|
7
|
+
slack?: SlackAdapterConfig;
|
|
8
|
+
feishu?: FeishuAdapterConfig;
|
|
9
|
+
}
|
|
10
|
+
export declare function createAdapter(config: AdapterConfig): PlatformAdapter;
|
|
11
|
+
/** Create adapter from environment variables (auto-detect platform). */
|
|
12
|
+
export declare function createAdapterFromEnv(): PlatformAdapter;
|
|
13
|
+
export { SlackAdapter } from './slack.js';
|
|
14
|
+
export type { SlackAdapterConfig } from './slack.js';
|
|
15
|
+
export { FeishuAdapter } from './feishu.js';
|
|
16
|
+
export type { FeishuAdapterConfig } from './feishu.js';
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
// input: ../adapter.js, ./slack.js, ./feishu.js, ../testing.js
|
|
2
|
+
// output: PlatformType + AdapterConfig + createAdapter factory
|
|
3
|
+
// pos: Select the specific adapter based on CORTEX_PLATFORM
|
|
4
|
+
// >>> If I am updated, update my header comment and the parent folder's CORTEX.md <<<
|
|
5
|
+
import { SlackAdapter } from './slack.js';
|
|
6
|
+
import { FeishuAdapter } from './feishu.js';
|
|
7
|
+
import { MockAdapter } from '../testing.js';
|
|
8
|
+
export function createAdapter(config) {
|
|
9
|
+
switch (config.platform) {
|
|
10
|
+
case 'slack':
|
|
11
|
+
if (!config.slack) {
|
|
12
|
+
throw new Error('Slack adapter requires slack config (botToken, signingSecret, appToken)');
|
|
13
|
+
}
|
|
14
|
+
return new SlackAdapter(config.slack);
|
|
15
|
+
case 'feishu':
|
|
16
|
+
if (!config.feishu) {
|
|
17
|
+
throw new Error('Feishu adapter requires feishu config (appId, appSecret)');
|
|
18
|
+
}
|
|
19
|
+
return new FeishuAdapter(config.feishu);
|
|
20
|
+
case 'discord':
|
|
21
|
+
case 'telegram':
|
|
22
|
+
throw new Error(`Platform "${config.platform}" is not yet implemented. Contributions welcome!`);
|
|
23
|
+
default:
|
|
24
|
+
throw new Error(`Unknown platform: ${config.platform}`);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
/** Create adapter from environment variables (auto-detect platform). */
|
|
28
|
+
export function createAdapterFromEnv() {
|
|
29
|
+
const platform = (process.env.CORTEX_PLATFORM || 'slack');
|
|
30
|
+
if (platform === 'slack') {
|
|
31
|
+
const botToken = process.env.SLACK_BOT_TOKEN;
|
|
32
|
+
const signingSecret = process.env.SLACK_SIGNING_SECRET;
|
|
33
|
+
const appToken = process.env.SLACK_APP_TOKEN;
|
|
34
|
+
if (!botToken || !signingSecret || !appToken) {
|
|
35
|
+
throw new Error('Missing required Slack env vars: SLACK_BOT_TOKEN, SLACK_SIGNING_SECRET, SLACK_APP_TOKEN');
|
|
36
|
+
}
|
|
37
|
+
return new SlackAdapter({
|
|
38
|
+
botToken,
|
|
39
|
+
signingSecret,
|
|
40
|
+
appToken,
|
|
41
|
+
adminChannel: process.env.CORTEX_ADMIN_CHANNEL || undefined,
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
if (platform === 'feishu') {
|
|
45
|
+
const appId = process.env.FEISHU_APP_ID;
|
|
46
|
+
const appSecret = process.env.FEISHU_APP_SECRET;
|
|
47
|
+
if (!appId || !appSecret) {
|
|
48
|
+
throw new Error('Missing required Feishu env vars: FEISHU_APP_ID, FEISHU_APP_SECRET');
|
|
49
|
+
}
|
|
50
|
+
return new FeishuAdapter({
|
|
51
|
+
appId,
|
|
52
|
+
appSecret,
|
|
53
|
+
encryptKey: process.env.FEISHU_ENCRYPT_KEY || undefined,
|
|
54
|
+
verificationToken: process.env.FEISHU_VERIFICATION_TOKEN || undefined,
|
|
55
|
+
adminChannel: process.env.CORTEX_ADMIN_CHANNEL || undefined,
|
|
56
|
+
domain: process.env.FEISHU_DOMAIN || undefined,
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
if (platform === 'test') {
|
|
60
|
+
return new MockAdapter({ adminChannel: process.env.CORTEX_ADMIN_CHANNEL || 'test-admin' });
|
|
61
|
+
}
|
|
62
|
+
return createAdapter({ platform });
|
|
63
|
+
}
|
|
64
|
+
export { SlackAdapter } from './slack.js';
|
|
65
|
+
export { FeishuAdapter } from './feishu.js';
|