@gokulvenkatareddy/cortex 0.1.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1295 -0
- package/apps/octogent/.github/workflows/ci.yml +40 -0
- package/apps/octogent/.shims/claude +4 -0
- package/apps/octogent/AGENTS.md +71 -0
- package/apps/octogent/CONTRIBUTING.md +72 -0
- package/apps/octogent/LICENSE +21 -0
- package/apps/octogent/README.md +184 -0
- package/apps/octogent/apps/api/AGENTS.md +32 -0
- package/apps/octogent/apps/api/package.json +19 -0
- package/apps/octogent/apps/api/src/agentStateDetection.ts +181 -0
- package/apps/octogent/apps/api/src/claudeSessionScanner.ts +235 -0
- package/apps/octogent/apps/api/src/claudeSkills.ts +182 -0
- package/apps/octogent/apps/api/src/claudeUsage.ts +922 -0
- package/apps/octogent/apps/api/src/cli.ts +595 -0
- package/apps/octogent/apps/api/src/codeIntelStore.ts +46 -0
- package/apps/octogent/apps/api/src/codexUsage.ts +278 -0
- package/apps/octogent/apps/api/src/createApiServer/codeIntelRoutes.ts +60 -0
- package/apps/octogent/apps/api/src/createApiServer/conversationRoutes.ts +128 -0
- package/apps/octogent/apps/api/src/createApiServer/deckRoutes.ts +873 -0
- package/apps/octogent/apps/api/src/createApiServer/gitParsers.ts +140 -0
- package/apps/octogent/apps/api/src/createApiServer/gitRoutes.ts +214 -0
- package/apps/octogent/apps/api/src/createApiServer/miscRoutes.ts +316 -0
- package/apps/octogent/apps/api/src/createApiServer/monitorParsers.ts +137 -0
- package/apps/octogent/apps/api/src/createApiServer/monitorRoutes.ts +95 -0
- package/apps/octogent/apps/api/src/createApiServer/requestHandler.ts +311 -0
- package/apps/octogent/apps/api/src/createApiServer/requestParsers.ts +25 -0
- package/apps/octogent/apps/api/src/createApiServer/routeHelpers.ts +97 -0
- package/apps/octogent/apps/api/src/createApiServer/security.ts +70 -0
- package/apps/octogent/apps/api/src/createApiServer/terminalParsers.ts +167 -0
- package/apps/octogent/apps/api/src/createApiServer/terminalRoutes.ts +315 -0
- package/apps/octogent/apps/api/src/createApiServer/types.ts +24 -0
- package/apps/octogent/apps/api/src/createApiServer/uiStateParsers.ts +255 -0
- package/apps/octogent/apps/api/src/createApiServer/upgradeHandler.ts +38 -0
- package/apps/octogent/apps/api/src/createApiServer/usageRoutes.ts +84 -0
- package/apps/octogent/apps/api/src/createApiServer.ts +176 -0
- package/apps/octogent/apps/api/src/deck/readDeckTentacles.ts +595 -0
- package/apps/octogent/apps/api/src/githubRepoSummary.ts +397 -0
- package/apps/octogent/apps/api/src/logging.ts +9 -0
- package/apps/octogent/apps/api/src/monitor/defaults.ts +3 -0
- package/apps/octogent/apps/api/src/monitor/index.ts +8 -0
- package/apps/octogent/apps/api/src/monitor/repository.ts +303 -0
- package/apps/octogent/apps/api/src/monitor/service.ts +349 -0
- package/apps/octogent/apps/api/src/monitor/types.ts +120 -0
- package/apps/octogent/apps/api/src/monitor/xProvider.ts +587 -0
- package/apps/octogent/apps/api/src/projectPersistence.ts +377 -0
- package/apps/octogent/apps/api/src/prompts/index.ts +10 -0
- package/apps/octogent/apps/api/src/prompts/promptResolver.ts +145 -0
- package/apps/octogent/apps/api/src/runtimeMetadata.ts +69 -0
- package/apps/octogent/apps/api/src/server.ts +80 -0
- package/apps/octogent/apps/api/src/setupState.ts +80 -0
- package/apps/octogent/apps/api/src/setupStatus.ts +174 -0
- package/apps/octogent/apps/api/src/startupPrerequisites.ts +146 -0
- package/apps/octogent/apps/api/src/terminalRuntime/channelMessaging.ts +87 -0
- package/apps/octogent/apps/api/src/terminalRuntime/claudeTranscript.ts +279 -0
- package/apps/octogent/apps/api/src/terminalRuntime/constants.ts +15 -0
- package/apps/octogent/apps/api/src/terminalRuntime/conversations.ts +492 -0
- package/apps/octogent/apps/api/src/terminalRuntime/gitOperations.ts +341 -0
- package/apps/octogent/apps/api/src/terminalRuntime/hookProcessor.ts +405 -0
- package/apps/octogent/apps/api/src/terminalRuntime/protocol.ts +46 -0
- package/apps/octogent/apps/api/src/terminalRuntime/ptyEnvironment.ts +50 -0
- package/apps/octogent/apps/api/src/terminalRuntime/registry.ts +423 -0
- package/apps/octogent/apps/api/src/terminalRuntime/sessionRuntime.ts +671 -0
- package/apps/octogent/apps/api/src/terminalRuntime/systemClients.ts +432 -0
- package/apps/octogent/apps/api/src/terminalRuntime/types.ts +157 -0
- package/apps/octogent/apps/api/src/terminalRuntime/worktreeManager.ts +135 -0
- package/apps/octogent/apps/api/src/terminalRuntime.ts +567 -0
- package/apps/octogent/apps/api/src/usageUtils.ts +16 -0
- package/apps/octogent/apps/api/src/ws-shim.d.ts +28 -0
- package/apps/octogent/apps/api/tests/agentStateDetection.test.ts +67 -0
- package/apps/octogent/apps/api/tests/claudeUsage.test.ts +583 -0
- package/apps/octogent/apps/api/tests/codexUsage.test.ts +107 -0
- package/apps/octogent/apps/api/tests/createApiServer.test.ts +3207 -0
- package/apps/octogent/apps/api/tests/githubRepoSummary.test.ts +100 -0
- package/apps/octogent/apps/api/tests/logging.test.ts +33 -0
- package/apps/octogent/apps/api/tests/monitorApi.test.ts +467 -0
- package/apps/octogent/apps/api/tests/monitorCore.test.ts +104 -0
- package/apps/octogent/apps/api/tests/promptResolver.test.ts +109 -0
- package/apps/octogent/apps/api/tests/protocol.test.ts +14 -0
- package/apps/octogent/apps/api/tests/sessionRuntime.test.ts +608 -0
- package/apps/octogent/apps/api/tests/startupPrerequisites.test.ts +70 -0
- package/apps/octogent/apps/api/tests/upgradeHandler.test.ts +40 -0
- package/apps/octogent/apps/api/tests/xMonitorProvider.test.ts +109 -0
- package/apps/octogent/apps/api/tsconfig.json +7 -0
- package/apps/octogent/apps/api/vitest.config.ts +7 -0
- package/apps/octogent/apps/web/AGENTS.md +38 -0
- package/apps/octogent/apps/web/index.html +13 -0
- package/apps/octogent/apps/web/package.json +32 -0
- package/apps/octogent/apps/web/public/octopus-favicon.svg +26 -0
- package/apps/octogent/apps/web/src/App.tsx +646 -0
- package/apps/octogent/apps/web/src/app/canvas/types.ts +34 -0
- package/apps/octogent/apps/web/src/app/codeIntelAggregation.ts +278 -0
- package/apps/octogent/apps/web/src/app/constants.ts +28 -0
- package/apps/octogent/apps/web/src/app/conversationNormalizers.ts +135 -0
- package/apps/octogent/apps/web/src/app/formatTimestamp.ts +18 -0
- package/apps/octogent/apps/web/src/app/githubMetrics.ts +76 -0
- package/apps/octogent/apps/web/src/app/githubNormalizers.ts +91 -0
- package/apps/octogent/apps/web/src/app/hooks/useAgentRuntimeStates.ts +18 -0
- package/apps/octogent/apps/web/src/app/hooks/useBackendLivenessPolling.ts +53 -0
- package/apps/octogent/apps/web/src/app/hooks/useCanvasGraphData.ts +449 -0
- package/apps/octogent/apps/web/src/app/hooks/useCanvasTransform.ts +260 -0
- package/apps/octogent/apps/web/src/app/hooks/useClaudeUsagePolling.ts +40 -0
- package/apps/octogent/apps/web/src/app/hooks/useClickOutside.ts +30 -0
- package/apps/octogent/apps/web/src/app/hooks/useCodeIntelRuntime.ts +83 -0
- package/apps/octogent/apps/web/src/app/hooks/useCodexUsagePolling.ts +35 -0
- package/apps/octogent/apps/web/src/app/hooks/useConsoleKeyboardShortcuts.ts +31 -0
- package/apps/octogent/apps/web/src/app/hooks/useConversationsRuntime.ts +377 -0
- package/apps/octogent/apps/web/src/app/hooks/useForceSimulation.ts +319 -0
- package/apps/octogent/apps/web/src/app/hooks/useGitHubPrimaryViewModel.ts +143 -0
- package/apps/octogent/apps/web/src/app/hooks/useGithubSummaryPolling.ts +28 -0
- package/apps/octogent/apps/web/src/app/hooks/useInitialColumnsHydration.ts +64 -0
- package/apps/octogent/apps/web/src/app/hooks/useMonitorRuntime.ts +220 -0
- package/apps/octogent/apps/web/src/app/hooks/usePersistedUiState.ts +536 -0
- package/apps/octogent/apps/web/src/app/hooks/usePollingData.ts +79 -0
- package/apps/octogent/apps/web/src/app/hooks/usePromptLibrary.ts +185 -0
- package/apps/octogent/apps/web/src/app/hooks/useTentacleGitLifecycle.ts +530 -0
- package/apps/octogent/apps/web/src/app/hooks/useTerminalCompletionNotification.ts +94 -0
- package/apps/octogent/apps/web/src/app/hooks/useTerminalMutations.ts +266 -0
- package/apps/octogent/apps/web/src/app/hooks/useTerminalStateReconciliation.ts +23 -0
- package/apps/octogent/apps/web/src/app/hooks/useUsageHeatmapPolling.ts +43 -0
- package/apps/octogent/apps/web/src/app/hooks/useWorkspaceSetup.ts +80 -0
- package/apps/octogent/apps/web/src/app/hotkeys.ts +31 -0
- package/apps/octogent/apps/web/src/app/monitorNormalizers.ts +145 -0
- package/apps/octogent/apps/web/src/app/notificationSounds.ts +164 -0
- package/apps/octogent/apps/web/src/app/terminalRuntimeStateStore.ts +261 -0
- package/apps/octogent/apps/web/src/app/terminalState.ts +21 -0
- package/apps/octogent/apps/web/src/app/types.ts +42 -0
- package/apps/octogent/apps/web/src/app/uiStateNormalizers.ts +113 -0
- package/apps/octogent/apps/web/src/app/usageNormalizers.ts +58 -0
- package/apps/octogent/apps/web/src/components/ActiveAgentsSidebar.tsx +60 -0
- package/apps/octogent/apps/web/src/components/ActivityPrimaryView.tsx +21 -0
- package/apps/octogent/apps/web/src/components/AgentStateBadge.tsx +47 -0
- package/apps/octogent/apps/web/src/components/CanvasPrimaryView.tsx +1532 -0
- package/apps/octogent/apps/web/src/components/ClearAllConversationsDialog.tsx +33 -0
- package/apps/octogent/apps/web/src/components/CodeIntelArcDiagram.tsx +245 -0
- package/apps/octogent/apps/web/src/components/CodeIntelPrimaryView.tsx +104 -0
- package/apps/octogent/apps/web/src/components/CodeIntelTreemap.tsx +138 -0
- package/apps/octogent/apps/web/src/components/ConsolePrimaryNav.tsx +31 -0
- package/apps/octogent/apps/web/src/components/ConversationsPrimaryView.tsx +243 -0
- package/apps/octogent/apps/web/src/components/DeckPrimaryView.tsx +613 -0
- package/apps/octogent/apps/web/src/components/DeleteTentacleDialog.tsx +91 -0
- package/apps/octogent/apps/web/src/components/EmptyOctopus.tsx +715 -0
- package/apps/octogent/apps/web/src/components/GitHubPrimaryView.tsx +494 -0
- package/apps/octogent/apps/web/src/components/MonitorPrimaryView.tsx +475 -0
- package/apps/octogent/apps/web/src/components/PrimaryViewRouter.tsx +99 -0
- package/apps/octogent/apps/web/src/components/PromptsPrimaryView.tsx +243 -0
- package/apps/octogent/apps/web/src/components/RuntimeStatusStrip.tsx +273 -0
- package/apps/octogent/apps/web/src/components/SettingsPrimaryView.tsx +92 -0
- package/apps/octogent/apps/web/src/components/SidebarActionPanel.tsx +124 -0
- package/apps/octogent/apps/web/src/components/SidebarConversationsList.tsx +279 -0
- package/apps/octogent/apps/web/src/components/SidebarPromptsList.tsx +116 -0
- package/apps/octogent/apps/web/src/components/TelemetryTape.tsx +106 -0
- package/apps/octogent/apps/web/src/components/TentacleGitActionsDialog.tsx +341 -0
- package/apps/octogent/apps/web/src/components/Terminal.tsx +524 -0
- package/apps/octogent/apps/web/src/components/TerminalPromptPicker.tsx +140 -0
- package/apps/octogent/apps/web/src/components/UsageHeatmap.tsx +702 -0
- package/apps/octogent/apps/web/src/components/canvas/CanvasTentaclePanel.tsx +485 -0
- package/apps/octogent/apps/web/src/components/canvas/CanvasTerminalColumn.tsx +89 -0
- package/apps/octogent/apps/web/src/components/canvas/DeleteAllTerminalsDialog.tsx +221 -0
- package/apps/octogent/apps/web/src/components/canvas/OctopusNode.tsx +307 -0
- package/apps/octogent/apps/web/src/components/canvas/SessionNode.tsx +185 -0
- package/apps/octogent/apps/web/src/components/deck/ActionCards.tsx +118 -0
- package/apps/octogent/apps/web/src/components/deck/AddTentacleForm.tsx +269 -0
- package/apps/octogent/apps/web/src/components/deck/DeckBottomActions.tsx +56 -0
- package/apps/octogent/apps/web/src/components/deck/TentaclePod.tsx +334 -0
- package/apps/octogent/apps/web/src/components/deck/WorkspaceSetupCard.tsx +105 -0
- package/apps/octogent/apps/web/src/components/deck/octopusVisuals.ts +72 -0
- package/apps/octogent/apps/web/src/components/terminalReplay.ts +62 -0
- package/apps/octogent/apps/web/src/components/terminalWheel.ts +54 -0
- package/apps/octogent/apps/web/src/components/ui/ActionButton.tsx +34 -0
- package/apps/octogent/apps/web/src/components/ui/ConfirmationDialog.tsx +86 -0
- package/apps/octogent/apps/web/src/components/ui/MarkdownContent.tsx +43 -0
- package/apps/octogent/apps/web/src/components/ui/SettingsToggle.tsx +34 -0
- package/apps/octogent/apps/web/src/components/ui/StatusBadge.tsx +24 -0
- package/apps/octogent/apps/web/src/main.tsx +17 -0
- package/apps/octogent/apps/web/src/runtime/HttpTerminalSnapshotReader.ts +87 -0
- package/apps/octogent/apps/web/src/runtime/runtimeEndpoints.ts +412 -0
- package/apps/octogent/apps/web/src/styles/chrome-and-buttons.css +272 -0
- package/apps/octogent/apps/web/src/styles/console-canvas-activity.css +358 -0
- package/apps/octogent/apps/web/src/styles/console-canvas-canvas.css +1843 -0
- package/apps/octogent/apps/web/src/styles/console-canvas-code-intel.css +227 -0
- package/apps/octogent/apps/web/src/styles/console-canvas-conversations.css +705 -0
- package/apps/octogent/apps/web/src/styles/console-canvas-deck.css +1524 -0
- package/apps/octogent/apps/web/src/styles/console-canvas-github.css +541 -0
- package/apps/octogent/apps/web/src/styles/console-canvas-monitor.css +595 -0
- package/apps/octogent/apps/web/src/styles/console-canvas-pixpack.css +81 -0
- package/apps/octogent/apps/web/src/styles/console-canvas-prompts.css +474 -0
- package/apps/octogent/apps/web/src/styles/console-canvas-settings.css +207 -0
- package/apps/octogent/apps/web/src/styles/console-chrome-status-nav.css +441 -0
- package/apps/octogent/apps/web/src/styles/console-overrides-telemetry.css +320 -0
- package/apps/octogent/apps/web/src/styles/console-theme-tokens.css +25 -0
- package/apps/octogent/apps/web/src/styles/cortex-theme.css +412 -0
- package/apps/octogent/apps/web/src/styles/foundation.css +100 -0
- package/apps/octogent/apps/web/src/styles/sidebar-and-scrollbars.css +447 -0
- package/apps/octogent/apps/web/src/styles/terminal-and-status.css +356 -0
- package/apps/octogent/apps/web/src/styles.css +25 -0
- package/apps/octogent/apps/web/src/types/ws.d.ts +23 -0
- package/apps/octogent/apps/web/tests/CanvasPrimaryView.test.tsx +347 -0
- package/apps/octogent/apps/web/tests/HttpTerminalSnapshotReader.test.tsx +54 -0
- package/apps/octogent/apps/web/tests/RuntimeStatusStrip.test.tsx +70 -0
- package/apps/octogent/apps/web/tests/Terminal.test.tsx +87 -0
- package/apps/octogent/apps/web/tests/add-tentacle-form.test.tsx +48 -0
- package/apps/octogent/apps/web/tests/app-github-runtime.test.tsx +162 -0
- package/apps/octogent/apps/web/tests/app-monitor-runtime.test.tsx +657 -0
- package/apps/octogent/apps/web/tests/app-shell-navigation.test.tsx +109 -0
- package/apps/octogent/apps/web/tests/app-swarm-refresh.test.tsx +268 -0
- package/apps/octogent/apps/web/tests/app-ui-state-persistence.test.tsx +116 -0
- package/apps/octogent/apps/web/tests/app-workspace-setup.test.tsx +217 -0
- package/apps/octogent/apps/web/tests/canvas-tentacle-panel.test.tsx +195 -0
- package/apps/octogent/apps/web/tests/delete-all-terminals-dialog.test.tsx +76 -0
- package/apps/octogent/apps/web/tests/githubMetrics.test.tsx +52 -0
- package/apps/octogent/apps/web/tests/hotkeys.test.tsx +44 -0
- package/apps/octogent/apps/web/tests/runtimeEndpoints.test.tsx +240 -0
- package/apps/octogent/apps/web/tests/setup.ts +39 -0
- package/apps/octogent/apps/web/tests/tentacle-pod.test.tsx +62 -0
- package/apps/octogent/apps/web/tests/terminalReplay.test.ts +71 -0
- package/apps/octogent/apps/web/tests/terminalState.test.tsx +49 -0
- package/apps/octogent/apps/web/tests/terminalWheel.test.tsx +51 -0
- package/apps/octogent/apps/web/tests/test-utils/appTestHarness.ts +48 -0
- package/apps/octogent/apps/web/tests/uiPrimitives.test.tsx +31 -0
- package/apps/octogent/apps/web/tests/useAgentRuntimeStates.test.tsx +47 -0
- package/apps/octogent/apps/web/tsconfig.json +8 -0
- package/apps/octogent/apps/web/vite.api.bundle.config.mts +32 -0
- package/apps/octogent/apps/web/vite.config.ts +22 -0
- package/apps/octogent/bin/octogent +3 -0
- package/apps/octogent/biome.json +21 -0
- package/apps/octogent/docs/concepts/mental-model.md +79 -0
- package/apps/octogent/docs/concepts/runtime-and-api.md +60 -0
- package/apps/octogent/docs/concepts/tentacles.md +85 -0
- package/apps/octogent/docs/getting-started/installation.md +54 -0
- package/apps/octogent/docs/getting-started/quickstart.md +79 -0
- package/apps/octogent/docs/guides/inter-agent-messaging.md +43 -0
- package/apps/octogent/docs/guides/orchestrating-child-agents.md +49 -0
- package/apps/octogent/docs/guides/working-with-todos.md +56 -0
- package/apps/octogent/docs/index.md +40 -0
- package/apps/octogent/docs/reference/api.md +103 -0
- package/apps/octogent/docs/reference/cli.md +71 -0
- package/apps/octogent/docs/reference/experimental-features.md +28 -0
- package/apps/octogent/docs/reference/filesystem-layout.md +62 -0
- package/apps/octogent/docs/reference/troubleshooting.md +49 -0
- package/apps/octogent/package.json +35 -0
- package/apps/octogent/packages/core/AGENTS.md +31 -0
- package/apps/octogent/packages/core/package.json +12 -0
- package/apps/octogent/packages/core/src/adapters/InMemoryTerminalSnapshotReader.ts +10 -0
- package/apps/octogent/packages/core/src/application/buildTerminalList.ts +13 -0
- package/apps/octogent/packages/core/src/domain/agentRuntime.ts +18 -0
- package/apps/octogent/packages/core/src/domain/channel.ts +8 -0
- package/apps/octogent/packages/core/src/domain/completionSound.ts +14 -0
- package/apps/octogent/packages/core/src/domain/conversation.ts +48 -0
- package/apps/octogent/packages/core/src/domain/deck.ts +33 -0
- package/apps/octogent/packages/core/src/domain/git.ts +32 -0
- package/apps/octogent/packages/core/src/domain/monitor.ts +62 -0
- package/apps/octogent/packages/core/src/domain/setup.ts +27 -0
- package/apps/octogent/packages/core/src/domain/terminal.ts +17 -0
- package/apps/octogent/packages/core/src/domain/uiState.ts +22 -0
- package/apps/octogent/packages/core/src/domain/usage.ts +60 -0
- package/apps/octogent/packages/core/src/index.ts +15 -0
- package/apps/octogent/packages/core/src/ports/TerminalSnapshotReader.ts +5 -0
- package/apps/octogent/packages/core/src/util/typeCoercion.ts +20 -0
- package/apps/octogent/packages/core/tests/buildTerminalList.test.ts +75 -0
- package/apps/octogent/packages/core/tsconfig.json +7 -0
- package/apps/octogent/packages/core/tsconfig.tsbuildinfo +1 -0
- package/apps/octogent/packages/core/vitest.config.ts +7 -0
- package/apps/octogent/pnpm-lock.yaml +3212 -0
- package/apps/octogent/pnpm-workspace.yaml +3 -0
- package/apps/octogent/prompts/meta-prompt-generator.md +223 -0
- package/apps/octogent/prompts/octoboss-clean-contexts.md +30 -0
- package/apps/octogent/prompts/octoboss-reorganize-tentacles.md +29 -0
- package/apps/octogent/prompts/octoboss-reorganize-todos.md +27 -0
- package/apps/octogent/prompts/sandbox-init.md +3 -0
- package/apps/octogent/prompts/swarm-parent.md +83 -0
- package/apps/octogent/prompts/swarm-worker.md +50 -0
- package/apps/octogent/prompts/tentacle-context-init.md +1 -0
- package/apps/octogent/prompts/tentacle-planner.md +110 -0
- package/apps/octogent/prompts/tentacle-reorganize-todos.md +20 -0
- package/apps/octogent/prompts/tentacle-update-tentacle.md +18 -0
- package/apps/octogent/scripts/build-package.mjs +23 -0
- package/apps/octogent/scripts/dev.mjs +158 -0
- package/apps/octogent/scripts/smoke-public-install.mjs +271 -0
- package/apps/octogent/static/images/octogent-header.png +0 -0
- package/apps/octogent/static/images/preview_1.jpg +0 -0
- package/apps/octogent/static/images/preview_2.jpg +0 -0
- package/apps/octogent/static/images/preview_3.jpg +0 -0
- package/apps/octogent/static/images/preview_4.jpg +0 -0
- package/apps/octogent/static/images/preview_5.jpg +0 -0
- package/apps/octogent/static/images/preview_6.jpg +0 -0
- package/apps/octogent/tsconfig.base.json +16 -0
- package/bin/AGI +3 -0
- package/bin/AGI-install-app +71 -0
- package/bin/AGI-ui +16 -0
- package/bin/AGI-voice +15 -0
- package/bin/AGI-web +16 -0
- package/bin/cortex +109 -0
- package/bin/cortex-octogent +99 -0
- package/bin/import-specifier.mjs +13 -0
- package/bin/import-specifier.test.mjs +13 -0
- package/bin/octo +150 -0
- package/dist/cli.mjs +555650 -0
- package/package.json +157 -0
- package/scripts/setup-wizard.ts +390 -0
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { dirname, join } from "node:path";
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
DEFAULT_MONITOR_MAX_CACHE_AGE_MS,
|
|
6
|
+
DEFAULT_MONITOR_MAX_POSTS,
|
|
7
|
+
DEFAULT_MONITOR_SEARCH_WINDOW_DAYS,
|
|
8
|
+
} from "./defaults";
|
|
9
|
+
import type { MonitorRepository, PersistedMonitorCache, PersistedMonitorConfig } from "./types";
|
|
10
|
+
|
|
11
|
+
const MONITOR_CONFIG_VERSION = 1 as const;
|
|
12
|
+
const MONITOR_CACHE_VERSION = 1 as const;
|
|
13
|
+
const MONITOR_CONFIG_RELATIVE_PATH = ".octogent/state/monitor-config.json";
|
|
14
|
+
const MONITOR_CACHE_RELATIVE_PATH = ".octogent/state/monitor-cache.json";
|
|
15
|
+
const VALID_MONITOR_SEARCH_WINDOW_DAYS = new Set([1, 3, 7]);
|
|
16
|
+
|
|
17
|
+
const asRecord = (value: unknown): Record<string, unknown> | null =>
|
|
18
|
+
value !== null && typeof value === "object" && !Array.isArray(value)
|
|
19
|
+
? (value as Record<string, unknown>)
|
|
20
|
+
: null;
|
|
21
|
+
|
|
22
|
+
const readJsonDocument = (path: string): unknown => {
|
|
23
|
+
const raw = readFileSync(path, "utf8");
|
|
24
|
+
return JSON.parse(raw);
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const normalizeStringArray = (value: unknown): string[] => {
|
|
28
|
+
if (!Array.isArray(value)) {
|
|
29
|
+
return [];
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return value
|
|
33
|
+
.filter((item): item is string => typeof item === "string")
|
|
34
|
+
.map((item) => item.trim())
|
|
35
|
+
.filter((item) => item.length > 0);
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
const normalizeMonitorPost = (value: unknown): PersistedMonitorCache["posts"][number] | null => {
|
|
39
|
+
const record = asRecord(value);
|
|
40
|
+
if (!record) {
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const source = record.source;
|
|
45
|
+
const id = typeof record.id === "string" ? record.id : null;
|
|
46
|
+
const text = typeof record.text === "string" ? record.text : null;
|
|
47
|
+
const author = typeof record.author === "string" ? record.author : null;
|
|
48
|
+
const createdAt = typeof record.createdAt === "string" ? record.createdAt : null;
|
|
49
|
+
const likeCount =
|
|
50
|
+
typeof record.likeCount === "number" && Number.isFinite(record.likeCount)
|
|
51
|
+
? Math.max(0, Math.floor(record.likeCount))
|
|
52
|
+
: null;
|
|
53
|
+
const permalink = typeof record.permalink === "string" ? record.permalink : null;
|
|
54
|
+
const matchedQueryTerm =
|
|
55
|
+
typeof record.matchedQueryTerm === "string" ? record.matchedQueryTerm : null;
|
|
56
|
+
|
|
57
|
+
if (
|
|
58
|
+
source !== "x" ||
|
|
59
|
+
id === null ||
|
|
60
|
+
text === null ||
|
|
61
|
+
author === null ||
|
|
62
|
+
createdAt === null ||
|
|
63
|
+
likeCount === null ||
|
|
64
|
+
permalink === null
|
|
65
|
+
) {
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return {
|
|
70
|
+
source,
|
|
71
|
+
id,
|
|
72
|
+
text,
|
|
73
|
+
author,
|
|
74
|
+
createdAt,
|
|
75
|
+
likeCount,
|
|
76
|
+
permalink,
|
|
77
|
+
matchedQueryTerm,
|
|
78
|
+
};
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
const normalizeUsage = (value: unknown): PersistedMonitorCache["usage"] => {
|
|
82
|
+
const record = asRecord(value);
|
|
83
|
+
if (!record) {
|
|
84
|
+
return null;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const status = record.status;
|
|
88
|
+
if (status !== "ok" && status !== "unavailable" && status !== "error") {
|
|
89
|
+
return null;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const source = record.source === "x-api" ? "x-api" : "none";
|
|
93
|
+
const fetchedAt =
|
|
94
|
+
typeof record.fetchedAt === "string" ? record.fetchedAt : new Date().toISOString();
|
|
95
|
+
const message = typeof record.message === "string" ? record.message : null;
|
|
96
|
+
|
|
97
|
+
const cap =
|
|
98
|
+
typeof record.cap === "number" && Number.isFinite(record.cap) ? Math.max(0, record.cap) : null;
|
|
99
|
+
const used =
|
|
100
|
+
typeof record.used === "number" && Number.isFinite(record.used)
|
|
101
|
+
? Math.max(0, record.used)
|
|
102
|
+
: null;
|
|
103
|
+
const remaining =
|
|
104
|
+
typeof record.remaining === "number" && Number.isFinite(record.remaining)
|
|
105
|
+
? Math.max(0, record.remaining)
|
|
106
|
+
: null;
|
|
107
|
+
const resetAt = typeof record.resetAt === "string" ? record.resetAt : null;
|
|
108
|
+
|
|
109
|
+
return {
|
|
110
|
+
status,
|
|
111
|
+
source,
|
|
112
|
+
fetchedAt,
|
|
113
|
+
message,
|
|
114
|
+
cap,
|
|
115
|
+
used,
|
|
116
|
+
remaining,
|
|
117
|
+
resetAt,
|
|
118
|
+
};
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
const normalizeCredentials = (
|
|
122
|
+
value: unknown,
|
|
123
|
+
): PersistedMonitorConfig["providers"]["x"]["credentials"] => {
|
|
124
|
+
const record = asRecord(value);
|
|
125
|
+
if (!record) {
|
|
126
|
+
return null;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const bearerToken = typeof record.bearerToken === "string" ? record.bearerToken.trim() : "";
|
|
130
|
+
if (bearerToken.length === 0) {
|
|
131
|
+
return null;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const normalizeOptional = (input: unknown) => {
|
|
135
|
+
if (typeof input !== "string") {
|
|
136
|
+
return null;
|
|
137
|
+
}
|
|
138
|
+
const trimmed = input.trim();
|
|
139
|
+
return trimmed.length > 0 ? trimmed : null;
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
return {
|
|
143
|
+
bearerToken,
|
|
144
|
+
apiKey: normalizeOptional(record.apiKey),
|
|
145
|
+
apiSecret: normalizeOptional(record.apiSecret),
|
|
146
|
+
accessToken: normalizeOptional(record.accessToken),
|
|
147
|
+
accessTokenSecret: normalizeOptional(record.accessTokenSecret),
|
|
148
|
+
updatedAt:
|
|
149
|
+
typeof record.updatedAt === "string" && record.updatedAt.length > 0
|
|
150
|
+
? record.updatedAt
|
|
151
|
+
: new Date().toISOString(),
|
|
152
|
+
};
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
const defaultConfig = (): PersistedMonitorConfig => ({
|
|
156
|
+
version: MONITOR_CONFIG_VERSION,
|
|
157
|
+
providerId: "x",
|
|
158
|
+
queryTerms: [],
|
|
159
|
+
refreshPolicy: {
|
|
160
|
+
maxCacheAgeMs: DEFAULT_MONITOR_MAX_CACHE_AGE_MS,
|
|
161
|
+
maxPosts: DEFAULT_MONITOR_MAX_POSTS,
|
|
162
|
+
searchWindowDays: DEFAULT_MONITOR_SEARCH_WINDOW_DAYS,
|
|
163
|
+
},
|
|
164
|
+
providers: {
|
|
165
|
+
x: {
|
|
166
|
+
credentials: null,
|
|
167
|
+
},
|
|
168
|
+
},
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
const defaultCache = (): PersistedMonitorCache => ({
|
|
172
|
+
version: MONITOR_CACHE_VERSION,
|
|
173
|
+
providerId: "x",
|
|
174
|
+
queryTerms: defaultConfig().queryTerms,
|
|
175
|
+
fetchedAt: null,
|
|
176
|
+
lastError: null,
|
|
177
|
+
posts: [],
|
|
178
|
+
usage: null,
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
const normalizeConfig = (value: unknown): PersistedMonitorConfig => {
|
|
182
|
+
const fallback = defaultConfig();
|
|
183
|
+
const record = asRecord(value);
|
|
184
|
+
if (!record || record.version !== MONITOR_CONFIG_VERSION) {
|
|
185
|
+
return fallback;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
const queryTerms = normalizeStringArray(record.queryTerms);
|
|
189
|
+
const refreshPolicyRecord = asRecord(record.refreshPolicy);
|
|
190
|
+
const maxCacheAgeMs =
|
|
191
|
+
refreshPolicyRecord &&
|
|
192
|
+
typeof refreshPolicyRecord.maxCacheAgeMs === "number" &&
|
|
193
|
+
Number.isFinite(refreshPolicyRecord.maxCacheAgeMs) &&
|
|
194
|
+
refreshPolicyRecord.maxCacheAgeMs > 0
|
|
195
|
+
? Math.floor(refreshPolicyRecord.maxCacheAgeMs)
|
|
196
|
+
: fallback.refreshPolicy.maxCacheAgeMs;
|
|
197
|
+
const maxPosts =
|
|
198
|
+
refreshPolicyRecord &&
|
|
199
|
+
typeof refreshPolicyRecord.maxPosts === "number" &&
|
|
200
|
+
Number.isFinite(refreshPolicyRecord.maxPosts) &&
|
|
201
|
+
refreshPolicyRecord.maxPosts > 0
|
|
202
|
+
? Math.floor(refreshPolicyRecord.maxPosts)
|
|
203
|
+
: fallback.refreshPolicy.maxPosts;
|
|
204
|
+
const searchWindowDaysRaw =
|
|
205
|
+
refreshPolicyRecord &&
|
|
206
|
+
typeof refreshPolicyRecord.searchWindowDays === "number" &&
|
|
207
|
+
Number.isFinite(refreshPolicyRecord.searchWindowDays)
|
|
208
|
+
? Math.floor(refreshPolicyRecord.searchWindowDays)
|
|
209
|
+
: null;
|
|
210
|
+
const searchWindowDays =
|
|
211
|
+
searchWindowDaysRaw !== null && VALID_MONITOR_SEARCH_WINDOW_DAYS.has(searchWindowDaysRaw)
|
|
212
|
+
? (searchWindowDaysRaw as 1 | 3 | 7)
|
|
213
|
+
: fallback.refreshPolicy.searchWindowDays;
|
|
214
|
+
|
|
215
|
+
const providersRecord = asRecord(record.providers);
|
|
216
|
+
const xProviderRecord = providersRecord ? asRecord(providersRecord.x) : null;
|
|
217
|
+
|
|
218
|
+
return {
|
|
219
|
+
version: MONITOR_CONFIG_VERSION,
|
|
220
|
+
providerId: record.providerId === "x" ? "x" : fallback.providerId,
|
|
221
|
+
queryTerms,
|
|
222
|
+
refreshPolicy: {
|
|
223
|
+
maxCacheAgeMs,
|
|
224
|
+
maxPosts,
|
|
225
|
+
searchWindowDays,
|
|
226
|
+
},
|
|
227
|
+
providers: {
|
|
228
|
+
x: {
|
|
229
|
+
credentials: normalizeCredentials(xProviderRecord?.credentials),
|
|
230
|
+
},
|
|
231
|
+
},
|
|
232
|
+
};
|
|
233
|
+
};
|
|
234
|
+
|
|
235
|
+
const normalizeCache = (value: unknown): PersistedMonitorCache => {
|
|
236
|
+
const fallback = defaultCache();
|
|
237
|
+
const record = asRecord(value);
|
|
238
|
+
if (!record || record.version !== MONITOR_CACHE_VERSION) {
|
|
239
|
+
return fallback;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
const posts = Array.isArray(record.posts)
|
|
243
|
+
? record.posts
|
|
244
|
+
.map((post) => normalizeMonitorPost(post))
|
|
245
|
+
.filter((post): post is NonNullable<typeof post> => post !== null)
|
|
246
|
+
: [];
|
|
247
|
+
|
|
248
|
+
const queryTerms = normalizeStringArray(record.queryTerms);
|
|
249
|
+
|
|
250
|
+
return {
|
|
251
|
+
version: MONITOR_CACHE_VERSION,
|
|
252
|
+
providerId: record.providerId === "x" ? "x" : fallback.providerId,
|
|
253
|
+
queryTerms,
|
|
254
|
+
fetchedAt: typeof record.fetchedAt === "string" ? record.fetchedAt : null,
|
|
255
|
+
lastError: typeof record.lastError === "string" ? record.lastError : null,
|
|
256
|
+
posts,
|
|
257
|
+
usage: normalizeUsage(record.usage),
|
|
258
|
+
};
|
|
259
|
+
};
|
|
260
|
+
|
|
261
|
+
const writeJsonDocument = (path: string, value: unknown) => {
|
|
262
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
263
|
+
writeFileSync(path, `${JSON.stringify(value, null, 2)}\n`, "utf8");
|
|
264
|
+
};
|
|
265
|
+
|
|
266
|
+
export const createFileMonitorRepository = (projectStateDir: string): MonitorRepository => {
|
|
267
|
+
const configPath = join(projectStateDir, "state", "monitor-config.json");
|
|
268
|
+
const cachePath = join(projectStateDir, "state", "monitor-cache.json");
|
|
269
|
+
|
|
270
|
+
return {
|
|
271
|
+
readConfig() {
|
|
272
|
+
if (!existsSync(configPath)) {
|
|
273
|
+
return defaultConfig();
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
try {
|
|
277
|
+
return normalizeConfig(readJsonDocument(configPath));
|
|
278
|
+
} catch {
|
|
279
|
+
return defaultConfig();
|
|
280
|
+
}
|
|
281
|
+
},
|
|
282
|
+
|
|
283
|
+
writeConfig(config) {
|
|
284
|
+
writeJsonDocument(configPath, config);
|
|
285
|
+
},
|
|
286
|
+
|
|
287
|
+
readCache() {
|
|
288
|
+
if (!existsSync(cachePath)) {
|
|
289
|
+
return defaultCache();
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
try {
|
|
293
|
+
return normalizeCache(readJsonDocument(cachePath));
|
|
294
|
+
} catch {
|
|
295
|
+
return defaultCache();
|
|
296
|
+
}
|
|
297
|
+
},
|
|
298
|
+
|
|
299
|
+
writeCache(cache) {
|
|
300
|
+
writeJsonDocument(cachePath, cache);
|
|
301
|
+
},
|
|
302
|
+
};
|
|
303
|
+
};
|
|
@@ -0,0 +1,349 @@
|
|
|
1
|
+
import { createFileMonitorRepository } from "./repository";
|
|
2
|
+
import type {
|
|
3
|
+
MonitorConfigPatchInput,
|
|
4
|
+
MonitorFeedSnapshot,
|
|
5
|
+
MonitorPost,
|
|
6
|
+
MonitorProviderAdapter,
|
|
7
|
+
MonitorProviderId,
|
|
8
|
+
MonitorReadFeedOptions,
|
|
9
|
+
MonitorRepository,
|
|
10
|
+
MonitorService,
|
|
11
|
+
PersistedMonitorCache,
|
|
12
|
+
PersistedMonitorConfig,
|
|
13
|
+
SanitizedMonitorConfig,
|
|
14
|
+
} from "./types";
|
|
15
|
+
import { createXMonitorProvider } from "./xProvider";
|
|
16
|
+
|
|
17
|
+
export class MonitorInputError extends Error {}
|
|
18
|
+
|
|
19
|
+
const normalizeQueryTerms = (queryTerms: string[]): string[] => {
|
|
20
|
+
const normalized = queryTerms.map((term) => term.trim()).filter((term) => term.length > 0);
|
|
21
|
+
return [...new Set(normalized)];
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const normalizeDateMs = (value: string | null): number | null => {
|
|
25
|
+
if (!value) {
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const ms = Date.parse(value);
|
|
30
|
+
return Number.isFinite(ms) ? ms : null;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
const queryTermsMatch = (left: string[], right: string[]): boolean => {
|
|
34
|
+
if (left.length !== right.length) {
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return left.every((term, index) => term === right[index]);
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
export const isMonitorCacheStale = ({
|
|
42
|
+
now,
|
|
43
|
+
maxCacheAgeMs,
|
|
44
|
+
lastFetchedAt,
|
|
45
|
+
cachedQueryTerms,
|
|
46
|
+
currentQueryTerms,
|
|
47
|
+
}: {
|
|
48
|
+
now: Date;
|
|
49
|
+
maxCacheAgeMs: number;
|
|
50
|
+
lastFetchedAt: string | null;
|
|
51
|
+
cachedQueryTerms: string[];
|
|
52
|
+
currentQueryTerms: string[];
|
|
53
|
+
}): boolean => {
|
|
54
|
+
if (!queryTermsMatch(cachedQueryTerms, currentQueryTerms)) {
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const fetchedAtMs = normalizeDateMs(lastFetchedAt);
|
|
59
|
+
if (fetchedAtMs === null) {
|
|
60
|
+
return true;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return now.getTime() - fetchedAtMs >= maxCacheAgeMs;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
const toRankDedupeKey = (post: MonitorPost) => `${post.source}:${post.id}`;
|
|
67
|
+
|
|
68
|
+
export const rankAndLimitPostsByLikes = (posts: MonitorPost[], limit = 30): MonitorPost[] => {
|
|
69
|
+
const dedupedById = new Map<string, MonitorPost>();
|
|
70
|
+
|
|
71
|
+
for (const post of posts) {
|
|
72
|
+
const key = toRankDedupeKey(post);
|
|
73
|
+
const existing = dedupedById.get(key);
|
|
74
|
+
if (!existing) {
|
|
75
|
+
dedupedById.set(key, post);
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (post.likeCount > existing.likeCount) {
|
|
80
|
+
dedupedById.set(key, post);
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if (post.likeCount === existing.likeCount) {
|
|
85
|
+
const postCreatedAtMs = normalizeDateMs(post.createdAt) ?? 0;
|
|
86
|
+
const existingCreatedAtMs = normalizeDateMs(existing.createdAt) ?? 0;
|
|
87
|
+
if (postCreatedAtMs > existingCreatedAtMs) {
|
|
88
|
+
dedupedById.set(key, post);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return [...dedupedById.values()]
|
|
94
|
+
.sort((left, right) => {
|
|
95
|
+
if (left.likeCount !== right.likeCount) {
|
|
96
|
+
return right.likeCount - left.likeCount;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const leftMs = normalizeDateMs(left.createdAt) ?? 0;
|
|
100
|
+
const rightMs = normalizeDateMs(right.createdAt) ?? 0;
|
|
101
|
+
return rightMs - leftMs;
|
|
102
|
+
})
|
|
103
|
+
.slice(0, Math.max(1, limit));
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
const buildStaleAfter = (lastFetchedAt: string | null, maxCacheAgeMs: number): string | null => {
|
|
107
|
+
const fetchedAtMs = normalizeDateMs(lastFetchedAt);
|
|
108
|
+
if (fetchedAtMs === null) {
|
|
109
|
+
return null;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return new Date(fetchedAtMs + maxCacheAgeMs).toISOString();
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
const sanitizeConfig = (
|
|
116
|
+
config: PersistedMonitorConfig,
|
|
117
|
+
providers: Map<MonitorProviderId, MonitorProviderAdapter>,
|
|
118
|
+
): SanitizedMonitorConfig => {
|
|
119
|
+
const provider = providers.get(config.providerId);
|
|
120
|
+
if (!provider) {
|
|
121
|
+
throw new MonitorInputError(`Unsupported monitor provider: ${config.providerId}`);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
return {
|
|
125
|
+
providerId: config.providerId,
|
|
126
|
+
queryTerms: [...config.queryTerms],
|
|
127
|
+
refreshPolicy: {
|
|
128
|
+
maxCacheAgeMs: config.refreshPolicy.maxCacheAgeMs,
|
|
129
|
+
maxPosts: config.refreshPolicy.maxPosts,
|
|
130
|
+
searchWindowDays: config.refreshPolicy.searchWindowDays,
|
|
131
|
+
},
|
|
132
|
+
providers: {
|
|
133
|
+
x: {
|
|
134
|
+
credentials: provider.summarizeCredentials(config.providers.x.credentials),
|
|
135
|
+
},
|
|
136
|
+
},
|
|
137
|
+
};
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
const toFeedSnapshot = (
|
|
141
|
+
config: PersistedMonitorConfig,
|
|
142
|
+
cache: PersistedMonitorCache,
|
|
143
|
+
now: Date,
|
|
144
|
+
): MonitorFeedSnapshot => {
|
|
145
|
+
const isStale = isMonitorCacheStale({
|
|
146
|
+
now,
|
|
147
|
+
maxCacheAgeMs: config.refreshPolicy.maxCacheAgeMs,
|
|
148
|
+
lastFetchedAt: cache.fetchedAt,
|
|
149
|
+
cachedQueryTerms: cache.queryTerms,
|
|
150
|
+
currentQueryTerms: config.queryTerms,
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
return {
|
|
154
|
+
providerId: config.providerId,
|
|
155
|
+
queryTerms: [...config.queryTerms],
|
|
156
|
+
refreshPolicy: {
|
|
157
|
+
maxCacheAgeMs: config.refreshPolicy.maxCacheAgeMs,
|
|
158
|
+
maxPosts: config.refreshPolicy.maxPosts,
|
|
159
|
+
searchWindowDays: config.refreshPolicy.searchWindowDays,
|
|
160
|
+
},
|
|
161
|
+
lastFetchedAt: cache.fetchedAt,
|
|
162
|
+
staleAfter: buildStaleAfter(cache.fetchedAt, config.refreshPolicy.maxCacheAgeMs),
|
|
163
|
+
isStale,
|
|
164
|
+
lastError: cache.lastError,
|
|
165
|
+
posts: [...cache.posts],
|
|
166
|
+
usage: cache.usage,
|
|
167
|
+
};
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
export const createMonitorService = ({
|
|
171
|
+
projectStateDir,
|
|
172
|
+
repository = createFileMonitorRepository(projectStateDir),
|
|
173
|
+
providers = [createXMonitorProvider()],
|
|
174
|
+
}: {
|
|
175
|
+
projectStateDir: string;
|
|
176
|
+
repository?: MonitorRepository;
|
|
177
|
+
providers?: MonitorProviderAdapter[];
|
|
178
|
+
}): MonitorService => {
|
|
179
|
+
const providersById = new Map<MonitorProviderId, MonitorProviderAdapter>(
|
|
180
|
+
providers.map((provider) => [provider.providerId, provider]),
|
|
181
|
+
);
|
|
182
|
+
|
|
183
|
+
const resolveProvider = (providerId: MonitorProviderId): MonitorProviderAdapter => {
|
|
184
|
+
const provider = providersById.get(providerId);
|
|
185
|
+
if (!provider) {
|
|
186
|
+
throw new MonitorInputError(`Unsupported monitor provider: ${providerId}`);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
return provider;
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
return {
|
|
193
|
+
async readConfig() {
|
|
194
|
+
const config = repository.readConfig();
|
|
195
|
+
return sanitizeConfig(config, providersById);
|
|
196
|
+
},
|
|
197
|
+
|
|
198
|
+
async patchConfig(patch: MonitorConfigPatchInput) {
|
|
199
|
+
const now = new Date();
|
|
200
|
+
const config = repository.readConfig();
|
|
201
|
+
|
|
202
|
+
if (patch.providerId) {
|
|
203
|
+
config.providerId = patch.providerId;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
if (patch.queryTerms) {
|
|
207
|
+
const queryTerms = normalizeQueryTerms(patch.queryTerms);
|
|
208
|
+
if (queryTerms.length === 0) {
|
|
209
|
+
throw new MonitorInputError("At least one monitor query term is required.");
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
config.queryTerms = queryTerms;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
if (patch.refreshPolicy?.maxCacheAgeMs !== undefined) {
|
|
216
|
+
const maxCacheAgeMs = Math.floor(patch.refreshPolicy.maxCacheAgeMs);
|
|
217
|
+
if (!Number.isFinite(maxCacheAgeMs) || maxCacheAgeMs <= 0) {
|
|
218
|
+
throw new MonitorInputError("refreshPolicy.maxCacheAgeMs must be a positive number.");
|
|
219
|
+
}
|
|
220
|
+
config.refreshPolicy.maxCacheAgeMs = maxCacheAgeMs;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
if (patch.refreshPolicy?.maxPosts !== undefined) {
|
|
224
|
+
const maxPosts = Math.floor(patch.refreshPolicy.maxPosts);
|
|
225
|
+
if (!Number.isFinite(maxPosts) || maxPosts <= 0) {
|
|
226
|
+
throw new MonitorInputError("refreshPolicy.maxPosts must be a positive number.");
|
|
227
|
+
}
|
|
228
|
+
config.refreshPolicy.maxPosts = maxPosts;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
if (patch.refreshPolicy?.searchWindowDays !== undefined) {
|
|
232
|
+
const searchWindowDays = Math.floor(patch.refreshPolicy.searchWindowDays);
|
|
233
|
+
if (searchWindowDays !== 1 && searchWindowDays !== 3 && searchWindowDays !== 7) {
|
|
234
|
+
throw new MonitorInputError("refreshPolicy.searchWindowDays must be one of: 1, 3, 7.");
|
|
235
|
+
}
|
|
236
|
+
config.refreshPolicy.searchWindowDays = searchWindowDays;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
if (patch.credentials !== undefined) {
|
|
240
|
+
const provider = resolveProvider(config.providerId);
|
|
241
|
+
let credentials: unknown;
|
|
242
|
+
try {
|
|
243
|
+
({ credentials } = provider.saveCredentials(patch.credentials, now));
|
|
244
|
+
} catch (error) {
|
|
245
|
+
const message =
|
|
246
|
+
error instanceof Error ? error.message : "Unable to save monitor credentials.";
|
|
247
|
+
throw new MonitorInputError(message);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
if (patch.validateCredentials !== false) {
|
|
251
|
+
const validation = await provider.validateCredentials(credentials);
|
|
252
|
+
if (!validation.ok) {
|
|
253
|
+
throw new MonitorInputError(validation.error ?? "Monitor credentials are invalid.");
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
config.providers.x.credentials =
|
|
258
|
+
credentials as PersistedMonitorConfig["providers"]["x"]["credentials"];
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
repository.writeConfig(config);
|
|
262
|
+
return sanitizeConfig(config, providersById);
|
|
263
|
+
},
|
|
264
|
+
|
|
265
|
+
async readFeed(options: MonitorReadFeedOptions = {}) {
|
|
266
|
+
const now = new Date();
|
|
267
|
+
const config = repository.readConfig();
|
|
268
|
+
const cache = repository.readCache();
|
|
269
|
+
const provider = resolveProvider(config.providerId);
|
|
270
|
+
|
|
271
|
+
const shouldRefresh =
|
|
272
|
+
options.forceRefresh === true ||
|
|
273
|
+
(options.refreshIfStale !== false &&
|
|
274
|
+
isMonitorCacheStale({
|
|
275
|
+
now,
|
|
276
|
+
maxCacheAgeMs: config.refreshPolicy.maxCacheAgeMs,
|
|
277
|
+
lastFetchedAt: cache.fetchedAt,
|
|
278
|
+
cachedQueryTerms: cache.queryTerms,
|
|
279
|
+
currentQueryTerms: config.queryTerms,
|
|
280
|
+
}));
|
|
281
|
+
|
|
282
|
+
if (!shouldRefresh) {
|
|
283
|
+
return toFeedSnapshot(config, cache, now);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
if (!config.providers.x.credentials) {
|
|
287
|
+
const nextCache: PersistedMonitorCache = {
|
|
288
|
+
...cache,
|
|
289
|
+
providerId: config.providerId,
|
|
290
|
+
queryTerms: [...config.queryTerms],
|
|
291
|
+
lastError: "X credentials are not configured.",
|
|
292
|
+
};
|
|
293
|
+
repository.writeCache(nextCache);
|
|
294
|
+
return toFeedSnapshot(config, nextCache, now);
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
if (config.queryTerms.length === 0) {
|
|
298
|
+
const nextCache: PersistedMonitorCache = {
|
|
299
|
+
...cache,
|
|
300
|
+
providerId: config.providerId,
|
|
301
|
+
queryTerms: [...config.queryTerms],
|
|
302
|
+
lastError: "At least one monitor query term is required.",
|
|
303
|
+
};
|
|
304
|
+
repository.writeCache(nextCache);
|
|
305
|
+
return toFeedSnapshot(config, nextCache, now);
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
try {
|
|
309
|
+
const [posts, usage] = await Promise.all([
|
|
310
|
+
provider.fetchRecentPosts({
|
|
311
|
+
credentials: config.providers.x.credentials,
|
|
312
|
+
queryTerms: config.queryTerms,
|
|
313
|
+
postLimit: config.refreshPolicy.maxPosts,
|
|
314
|
+
searchWindowDays: config.refreshPolicy.searchWindowDays,
|
|
315
|
+
now,
|
|
316
|
+
}),
|
|
317
|
+
provider.fetchUsage({
|
|
318
|
+
credentials: config.providers.x.credentials,
|
|
319
|
+
now,
|
|
320
|
+
}),
|
|
321
|
+
]);
|
|
322
|
+
|
|
323
|
+
const nextCache: PersistedMonitorCache = {
|
|
324
|
+
version: 1,
|
|
325
|
+
providerId: config.providerId,
|
|
326
|
+
queryTerms: [...config.queryTerms],
|
|
327
|
+
fetchedAt: now.toISOString(),
|
|
328
|
+
lastError: null,
|
|
329
|
+
posts: rankAndLimitPostsByLikes(posts, config.refreshPolicy.maxPosts),
|
|
330
|
+
usage,
|
|
331
|
+
};
|
|
332
|
+
|
|
333
|
+
repository.writeCache(nextCache);
|
|
334
|
+
return toFeedSnapshot(config, nextCache, now);
|
|
335
|
+
} catch (error) {
|
|
336
|
+
const message = error instanceof Error ? error.message : "Unable to refresh monitor feed.";
|
|
337
|
+
const nextCache: PersistedMonitorCache = {
|
|
338
|
+
...cache,
|
|
339
|
+
providerId: config.providerId,
|
|
340
|
+
queryTerms: [...config.queryTerms],
|
|
341
|
+
lastError: message,
|
|
342
|
+
};
|
|
343
|
+
|
|
344
|
+
repository.writeCache(nextCache);
|
|
345
|
+
return toFeedSnapshot(config, nextCache, now);
|
|
346
|
+
}
|
|
347
|
+
},
|
|
348
|
+
};
|
|
349
|
+
};
|