opencandle 0.11.0 → 0.12.0
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 +25 -28
- package/dist/analysts/contracts.js +0 -1
- package/dist/analysts/orchestrator.d.ts +2 -0
- package/dist/analysts/orchestrator.js +19 -1
- package/dist/cli-main.js +13 -7
- package/dist/cli-options.d.ts +4 -0
- package/dist/cli-options.js +33 -0
- package/dist/cli.js +8 -3
- package/dist/config.js +0 -1
- package/dist/doctor/cli-command.js +2 -1
- package/dist/doctor/render.js +0 -1
- package/dist/doctor/report.js +41 -12
- package/dist/gui/server/ask-user-bridge.d.ts +19 -0
- package/dist/gui/server/ask-user-bridge.js +55 -0
- package/dist/gui/server/automation-heartbeat.d.ts +25 -0
- package/dist/gui/server/automation-heartbeat.js +61 -0
- package/dist/gui/server/background-quotes.d.ts +39 -0
- package/dist/gui/server/background-quotes.js +71 -0
- package/dist/gui/server/chat-event-adapter.d.ts +16 -0
- package/dist/gui/server/chat-event-adapter.js +263 -0
- package/dist/gui/server/gui-session-manager.d.ts +2 -0
- package/dist/gui/server/gui-session-manager.js +4 -0
- package/dist/gui/server/http-routes.d.ts +69 -0
- package/dist/gui/server/http-routes.js +1008 -0
- package/dist/gui/server/invoke-tool.d.ts +47 -0
- package/dist/gui/server/invoke-tool.js +373 -0
- package/dist/gui/server/live-chat-event-adapter.d.ts +19 -0
- package/dist/gui/server/live-chat-event-adapter.js +206 -0
- package/dist/gui/server/local-session-coordinator.d.ts +26 -0
- package/dist/gui/server/local-session-coordinator.js +53 -0
- package/dist/gui/server/market-state-api.d.ts +138 -0
- package/dist/gui/server/market-state-api.js +355 -0
- package/dist/gui/server/model-setup.d.ts +64 -0
- package/dist/gui/server/model-setup.js +150 -0
- package/dist/gui/server/private-api-access.d.ts +6 -0
- package/dist/gui/server/private-api-access.js +53 -0
- package/dist/gui/server/projector.d.ts +49 -0
- package/dist/gui/server/projector.js +296 -0
- package/dist/gui/server/prompt-observation.d.ts +8 -0
- package/dist/gui/server/prompt-observation.js +43 -0
- package/dist/gui/server/quote-snapshot-store.d.ts +14 -0
- package/dist/gui/server/quote-snapshot-store.js +49 -0
- package/dist/gui/server/server.d.ts +1 -0
- package/dist/gui/server/server.js +259 -0
- package/dist/gui/server/session-actions.d.ts +60 -0
- package/dist/gui/server/session-actions.js +218 -0
- package/dist/gui/server/session-entry-wait.d.ts +27 -0
- package/dist/gui/server/session-entry-wait.js +129 -0
- package/dist/gui/server/session-list.d.ts +2 -0
- package/dist/gui/server/session-list.js +11 -0
- package/dist/gui/server/shutdown.d.ts +10 -0
- package/dist/gui/server/shutdown.js +29 -0
- package/dist/gui/server/tool-invoke-ack.d.ts +17 -0
- package/dist/gui/server/tool-invoke-ack.js +26 -0
- package/dist/gui/server/tool-metadata.d.ts +93 -0
- package/dist/gui/server/tool-metadata.js +148 -0
- package/dist/gui/server/websocket.d.ts +9 -0
- package/dist/gui/server/websocket.js +124 -0
- package/dist/gui/server/writer-lock.d.ts +1 -0
- package/dist/gui/server/writer-lock.js +1 -0
- package/dist/gui/server/ws-hub.d.ts +50 -0
- package/dist/gui/server/ws-hub.js +284 -0
- package/dist/gui/shared/chat-events.d.ts +174 -0
- package/dist/gui/shared/chat-events.js +13 -0
- package/dist/gui/shared/event-reducer.d.ts +3 -0
- package/dist/gui/shared/event-reducer.js +187 -0
- package/dist/index.js +0 -1
- package/dist/infra/cache.d.ts +6 -0
- package/dist/infra/cache.js +16 -7
- package/dist/infra/freshness.d.ts +21 -0
- package/dist/infra/freshness.js +118 -0
- package/dist/infra/http-client.js +1 -2
- package/dist/infra/index.js +0 -1
- package/dist/infra/market-calendar.d.ts +14 -0
- package/dist/infra/market-calendar.js +136 -0
- package/dist/infra/native-dependencies.js +0 -1
- package/dist/infra/node-version.js +0 -1
- package/dist/infra/open-url.js +0 -1
- package/dist/infra/opencandle-paths.js +0 -1
- package/dist/infra/rate-limiter.js +1 -1
- package/dist/market-state/alert-conditions.js +0 -1
- package/dist/market-state/alert-runner.d.ts +1 -0
- package/dist/market-state/alert-runner.js +5 -4
- package/dist/market-state/daily-report.js +0 -1
- package/dist/market-state/local-automation-service.js +0 -1
- package/dist/market-state/notification-delivery.js +0 -1
- package/dist/market-state/resolve-for-mutation.js +0 -1
- package/dist/market-state/resolve.js +0 -1
- package/dist/market-state/service.d.ts +26 -21
- package/dist/market-state/service.js +175 -36
- package/dist/market-state/summaries.d.ts +8 -0
- package/dist/market-state/summaries.js +58 -0
- package/dist/memory/index.js +0 -1
- package/dist/memory/manager.js +0 -1
- package/dist/memory/preference-extractor.js +0 -1
- package/dist/memory/retrieval.js +0 -1
- package/dist/memory/sqlite.js +60 -11
- package/dist/memory/storage.js +0 -1
- package/dist/memory/tool-defaults.js +0 -1
- package/dist/memory/types.js +0 -1
- package/dist/monitor.js +0 -1
- package/dist/onboarding/connect.js +0 -1
- package/dist/onboarding/credential-interceptor.js +0 -1
- package/dist/onboarding/degradation-accumulator.js +0 -1
- package/dist/onboarding/prompt-user.d.ts +1 -1
- package/dist/onboarding/prompt-user.js +0 -1
- package/dist/onboarding/provider-status.js +0 -1
- package/dist/onboarding/providers.d.ts +25 -1
- package/dist/onboarding/providers.js +26 -1
- package/dist/onboarding/state.js +0 -1
- package/dist/onboarding/tool-helpers.js +0 -1
- package/dist/onboarding/tool-tags.js +0 -1
- package/dist/onboarding/validate-model-key.d.ts +17 -0
- package/dist/onboarding/validate-model-key.js +54 -0
- package/dist/onboarding/validation.js +0 -1
- package/dist/pi/opencandle-extension.js +46 -7
- package/dist/pi/session-action-dedupe.js +0 -1
- package/dist/pi/session-storage.js +0 -1
- package/dist/pi/session-writer-lock.js +0 -1
- package/dist/pi/session.js +0 -1
- package/dist/pi/setup.d.ts +2 -0
- package/dist/pi/setup.js +10 -3
- package/dist/pi/tool-adapter.js +0 -1
- package/dist/pi/tui-session-coordinator.js +0 -1
- package/dist/prompts/context-builder.js +1 -2
- package/dist/prompts/disclaimer.js +0 -1
- package/dist/prompts/policy-cards.js +0 -1
- package/dist/prompts/sections.js +0 -1
- package/dist/prompts/symbol-preflight.js +0 -1
- package/dist/prompts/workflow-prompts.js +0 -1
- package/dist/providers/alpha-vantage.js +8 -3
- package/dist/providers/coingecko.js +1 -1
- package/dist/providers/errors.js +0 -1
- package/dist/providers/exa-search.js +11 -10
- package/dist/providers/external-tool-command.js +0 -1
- package/dist/providers/external-tool-error.js +0 -1
- package/dist/providers/fear-greed.js +0 -1
- package/dist/providers/finnhub.js +0 -1
- package/dist/providers/fred.js +0 -1
- package/dist/providers/index.js +0 -1
- package/dist/providers/polymarket.d.ts +2 -0
- package/dist/providers/polymarket.js +162 -0
- package/dist/providers/provider-credential-error.js +0 -1
- package/dist/providers/reddit-cli.js +0 -1
- package/dist/providers/reddit.js +0 -1
- package/dist/providers/sec-edgar.js +0 -1
- package/dist/providers/tradingview.js +1 -2
- package/dist/providers/twitter-cli.js +0 -1
- package/dist/providers/twitter.js +0 -1
- package/dist/providers/web-search.js +15 -12
- package/dist/providers/with-fallback.js +0 -1
- package/dist/providers/wrap-provider.js +5 -4
- package/dist/providers/yahoo-finance.d.ts +3 -0
- package/dist/providers/yahoo-finance.js +272 -13
- package/dist/routing/classify-intent.js +0 -1
- package/dist/routing/defaults.js +0 -1
- package/dist/routing/entity-extractor.js +18 -3
- package/dist/routing/fund-symbols.js +0 -1
- package/dist/routing/horizon.js +0 -1
- package/dist/routing/index.js +0 -1
- package/dist/routing/legacy-rule-router.js +0 -1
- package/dist/routing/planning.js +0 -1
- package/dist/routing/route-manifest.js +1 -2
- package/dist/routing/router-llm-client.d.ts +2 -2
- package/dist/routing/router-llm-client.js +18 -4
- package/dist/routing/router-prompt.js +0 -1
- package/dist/routing/router-types.js +0 -1
- package/dist/routing/router.d.ts +1 -1
- package/dist/routing/router.js +274 -6
- package/dist/routing/slot-resolver.d.ts +1 -0
- package/dist/routing/slot-resolver.js +1 -2
- package/dist/routing/symbol-disambiguator.js +0 -1
- package/dist/routing/turn-context.js +0 -1
- package/dist/routing/types.js +0 -1
- package/dist/runtime/answer-contracts.js +0 -1
- package/dist/runtime/artifact-contracts.js +0 -1
- package/dist/runtime/evidence.d.ts +1 -0
- package/dist/runtime/evidence.js +0 -1
- package/dist/runtime/numeric-claims.d.ts +23 -0
- package/dist/runtime/numeric-claims.js +99 -0
- package/dist/runtime/planning-evidence.js +1 -77
- package/dist/runtime/prompt-step.d.ts +10 -1
- package/dist/runtime/prompt-step.js +116 -3
- package/dist/runtime/provider-tracker.js +0 -1
- package/dist/runtime/run-context.js +0 -1
- package/dist/runtime/session-coordinator.d.ts +11 -1
- package/dist/runtime/session-coordinator.js +352 -50
- package/dist/runtime/session-title.js +0 -1
- package/dist/runtime/tool-defaults-wrapper.js +0 -1
- package/dist/runtime/validation.d.ts +3 -1
- package/dist/runtime/validation.js +0 -1
- package/dist/runtime/workflow-events.d.ts +1 -1
- package/dist/runtime/workflow-events.js +0 -1
- package/dist/runtime/workflow-runner.d.ts +1 -0
- package/dist/runtime/workflow-runner.js +8 -3
- package/dist/runtime/workflow-types.d.ts +3 -0
- package/dist/runtime/workflow-types.js +0 -1
- package/dist/sentiment/adapters/finnhub.js +0 -1
- package/dist/sentiment/adapters/reddit.js +0 -1
- package/dist/sentiment/adapters/twitter.js +0 -1
- package/dist/sentiment/adapters/web.js +0 -1
- package/dist/sentiment/index.js +4 -2
- package/dist/sentiment/insights.js +0 -1
- package/dist/sentiment/keywords.js +0 -1
- package/dist/sentiment/pipeline.js +0 -1
- package/dist/sentiment/scorer.js +0 -1
- package/dist/sentiment/store.js +0 -1
- package/dist/sentiment/trends.js +0 -1
- package/dist/sentiment/types.js +0 -1
- package/dist/system-prompt.js +0 -1
- package/dist/tool-kit.js +0 -1
- package/dist/tools/fundamentals/company-overview.d.ts +1 -1
- package/dist/tools/fundamentals/company-overview.js +2 -1
- package/dist/tools/fundamentals/comps.d.ts +1 -0
- package/dist/tools/fundamentals/comps.js +25 -12
- package/dist/tools/fundamentals/dcf.js +183 -113
- package/dist/tools/fundamentals/earnings.d.ts +1 -1
- package/dist/tools/fundamentals/earnings.js +2 -1
- package/dist/tools/fundamentals/financials.js +2 -1
- package/dist/tools/fundamentals/sec-filings.js +0 -1
- package/dist/tools/index.d.ts +117 -2
- package/dist/tools/index.js +3 -1
- package/dist/tools/interaction/ask-user.js +11 -1
- package/dist/tools/macro/event-probabilities.d.ts +8 -0
- package/dist/tools/macro/event-probabilities.js +120 -0
- package/dist/tools/macro/fear-greed.d.ts +1 -1
- package/dist/tools/macro/fear-greed.js +0 -1
- package/dist/tools/macro/fred-data.d.ts +1 -1
- package/dist/tools/macro/fred-data.js +2 -1
- package/dist/tools/market/crypto-history.js +0 -1
- package/dist/tools/market/crypto-price.d.ts +4 -1
- package/dist/tools/market/crypto-price.js +10 -2
- package/dist/tools/market/screen-stocks.js +14 -4
- package/dist/tools/market/search-ticker.js +1 -2
- package/dist/tools/market/stock-history.js +0 -1
- package/dist/tools/market/stock-quote.d.ts +4 -1
- package/dist/tools/market/stock-quote.js +9 -5
- package/dist/tools/options/greeks.js +0 -1
- package/dist/tools/options/option-chain.d.ts +4 -1
- package/dist/tools/options/option-chain.js +12 -2
- package/dist/tools/portfolio/alerts.d.ts +2 -1
- package/dist/tools/portfolio/alerts.js +228 -2
- package/dist/tools/portfolio/correlation.js +23 -5
- package/dist/tools/portfolio/daily-report.js +0 -1
- package/dist/tools/portfolio/holdings-overlap.js +0 -1
- package/dist/tools/portfolio/notifications.js +0 -1
- package/dist/tools/portfolio/risk-analysis.d.ts +1 -1
- package/dist/tools/portfolio/risk-analysis.js +1 -2
- package/dist/tools/portfolio/tracker.d.ts +3 -1
- package/dist/tools/portfolio/tracker.js +57 -10
- package/dist/tools/portfolio/watchlist.d.ts +3 -6
- package/dist/tools/portfolio/watchlist.js +101 -110
- package/dist/tools/sentiment/insight-format.js +0 -1
- package/dist/tools/sentiment/query-match.js +0 -1
- package/dist/tools/sentiment/reddit-sentiment.js +0 -1
- package/dist/tools/sentiment/sentiment-summary.js +8 -4
- package/dist/tools/sentiment/sentiment-trend.d.ts +5 -1
- package/dist/tools/sentiment/sentiment-trend.js +6 -2
- package/dist/tools/sentiment/twitter-sentiment.js +0 -1
- package/dist/tools/sentiment/untrusted-text.js +0 -1
- package/dist/tools/sentiment/web-search.d.ts +1 -1
- package/dist/tools/sentiment/web-search.js +0 -1
- package/dist/tools/sentiment/web-sentiment.js +0 -1
- package/dist/tools/technical/backtest.js +0 -1
- package/dist/tools/technical/indicators.js +1 -2
- package/dist/types/fundamentals.d.ts +1 -0
- package/dist/types/fundamentals.js +0 -1
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.js +0 -1
- package/dist/types/macro.js +0 -1
- package/dist/types/market.d.ts +8 -0
- package/dist/types/market.js +0 -1
- package/dist/types/options.d.ts +1 -0
- package/dist/types/options.js +0 -1
- package/dist/types/portfolio.js +0 -1
- package/dist/types/prediction-markets.d.ts +13 -0
- package/dist/types/prediction-markets.js +1 -0
- package/dist/types/sentiment.js +0 -1
- package/dist/workflows/compare-assets.js +0 -1
- package/dist/workflows/index.js +0 -1
- package/dist/workflows/options-screener.js +0 -1
- package/dist/workflows/portfolio-builder.js +0 -1
- package/gui/web/dist/assets/CatalogOverlay-CAc7e3Pf.js +1 -0
- package/gui/web/dist/assets/index-BOEKd9wT.css +2 -0
- package/gui/web/dist/assets/index-C-H05cQ2.js +65 -0
- package/gui/web/dist/index.html +2 -2
- package/package.json +16 -22
- package/dist/analysts/contracts.js.map +0 -1
- package/dist/analysts/orchestrator.js.map +0 -1
- package/dist/cli-main.js.map +0 -1
- package/dist/cli.js.map +0 -1
- package/dist/config.js.map +0 -1
- package/dist/doctor/cli-command.js.map +0 -1
- package/dist/doctor/render.js.map +0 -1
- package/dist/doctor/report.js.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/infra/cache.js.map +0 -1
- package/dist/infra/http-client.js.map +0 -1
- package/dist/infra/index.js.map +0 -1
- package/dist/infra/native-dependencies.js.map +0 -1
- package/dist/infra/node-version.js.map +0 -1
- package/dist/infra/open-url.js.map +0 -1
- package/dist/infra/opencandle-paths.js.map +0 -1
- package/dist/infra/rate-limiter.js.map +0 -1
- package/dist/market-state/alert-conditions.js.map +0 -1
- package/dist/market-state/alert-runner.js.map +0 -1
- package/dist/market-state/daily-report.js.map +0 -1
- package/dist/market-state/local-automation-service.js.map +0 -1
- package/dist/market-state/notification-delivery.js.map +0 -1
- package/dist/market-state/resolve-for-mutation.js.map +0 -1
- package/dist/market-state/resolve.js.map +0 -1
- package/dist/market-state/service.js.map +0 -1
- package/dist/memory/index.js.map +0 -1
- package/dist/memory/manager.js.map +0 -1
- package/dist/memory/preference-extractor.js.map +0 -1
- package/dist/memory/retrieval.js.map +0 -1
- package/dist/memory/sqlite.js.map +0 -1
- package/dist/memory/storage.js.map +0 -1
- package/dist/memory/tool-defaults.js.map +0 -1
- package/dist/memory/types.js.map +0 -1
- package/dist/monitor.js.map +0 -1
- package/dist/onboarding/connect.js.map +0 -1
- package/dist/onboarding/credential-interceptor.js.map +0 -1
- package/dist/onboarding/degradation-accumulator.js.map +0 -1
- package/dist/onboarding/prompt-user.js.map +0 -1
- package/dist/onboarding/provider-status.js.map +0 -1
- package/dist/onboarding/providers.js.map +0 -1
- package/dist/onboarding/state.js.map +0 -1
- package/dist/onboarding/tool-helpers.js.map +0 -1
- package/dist/onboarding/tool-tags.js.map +0 -1
- package/dist/onboarding/validation.js.map +0 -1
- package/dist/pi/opencandle-extension.js.map +0 -1
- package/dist/pi/session-action-dedupe.js.map +0 -1
- package/dist/pi/session-storage.js.map +0 -1
- package/dist/pi/session-writer-lock.js.map +0 -1
- package/dist/pi/session.js.map +0 -1
- package/dist/pi/setup.js.map +0 -1
- package/dist/pi/tool-adapter.js.map +0 -1
- package/dist/pi/tui-session-coordinator.js.map +0 -1
- package/dist/prompts/context-builder.js.map +0 -1
- package/dist/prompts/disclaimer.js.map +0 -1
- package/dist/prompts/policy-cards.js.map +0 -1
- package/dist/prompts/sections.js.map +0 -1
- package/dist/prompts/symbol-preflight.js.map +0 -1
- package/dist/prompts/workflow-prompts.js.map +0 -1
- package/dist/providers/alpha-vantage.js.map +0 -1
- package/dist/providers/coingecko.js.map +0 -1
- package/dist/providers/errors.js.map +0 -1
- package/dist/providers/exa-search.js.map +0 -1
- package/dist/providers/external-tool-command.js.map +0 -1
- package/dist/providers/external-tool-error.js.map +0 -1
- package/dist/providers/fear-greed.js.map +0 -1
- package/dist/providers/finnhub.js.map +0 -1
- package/dist/providers/fred.js.map +0 -1
- package/dist/providers/index.js.map +0 -1
- package/dist/providers/provider-credential-error.js.map +0 -1
- package/dist/providers/reddit-cli.js.map +0 -1
- package/dist/providers/reddit.js.map +0 -1
- package/dist/providers/sec-edgar.js.map +0 -1
- package/dist/providers/tradingview.js.map +0 -1
- package/dist/providers/twitter-cli.js.map +0 -1
- package/dist/providers/twitter.js.map +0 -1
- package/dist/providers/web-search.js.map +0 -1
- package/dist/providers/with-fallback.js.map +0 -1
- package/dist/providers/wrap-provider.js.map +0 -1
- package/dist/providers/yahoo-finance.js.map +0 -1
- package/dist/routing/classify-intent.js.map +0 -1
- package/dist/routing/defaults.js.map +0 -1
- package/dist/routing/entity-extractor.js.map +0 -1
- package/dist/routing/fund-symbols.js.map +0 -1
- package/dist/routing/horizon.js.map +0 -1
- package/dist/routing/index.js.map +0 -1
- package/dist/routing/legacy-rule-router.js.map +0 -1
- package/dist/routing/planning.js.map +0 -1
- package/dist/routing/route-manifest.js.map +0 -1
- package/dist/routing/router-llm-client.js.map +0 -1
- package/dist/routing/router-prompt.js.map +0 -1
- package/dist/routing/router-types.js.map +0 -1
- package/dist/routing/router.js.map +0 -1
- package/dist/routing/slot-resolver.js.map +0 -1
- package/dist/routing/symbol-disambiguator.js.map +0 -1
- package/dist/routing/turn-context.js.map +0 -1
- package/dist/routing/types.js.map +0 -1
- package/dist/runtime/answer-contracts.js.map +0 -1
- package/dist/runtime/artifact-contracts.js.map +0 -1
- package/dist/runtime/evidence.js.map +0 -1
- package/dist/runtime/planning-evidence.js.map +0 -1
- package/dist/runtime/prompt-step.js.map +0 -1
- package/dist/runtime/provider-tracker.js.map +0 -1
- package/dist/runtime/run-context.js.map +0 -1
- package/dist/runtime/session-coordinator.js.map +0 -1
- package/dist/runtime/session-title.js.map +0 -1
- package/dist/runtime/tool-defaults-wrapper.js.map +0 -1
- package/dist/runtime/validation.js.map +0 -1
- package/dist/runtime/workflow-events.js.map +0 -1
- package/dist/runtime/workflow-runner.js.map +0 -1
- package/dist/runtime/workflow-types.js.map +0 -1
- package/dist/sentiment/adapters/finnhub.js.map +0 -1
- package/dist/sentiment/adapters/reddit.js.map +0 -1
- package/dist/sentiment/adapters/twitter.js.map +0 -1
- package/dist/sentiment/adapters/web.js.map +0 -1
- package/dist/sentiment/index.js.map +0 -1
- package/dist/sentiment/insights.js.map +0 -1
- package/dist/sentiment/keywords.js.map +0 -1
- package/dist/sentiment/pipeline.js.map +0 -1
- package/dist/sentiment/scorer.js.map +0 -1
- package/dist/sentiment/store.js.map +0 -1
- package/dist/sentiment/trends.js.map +0 -1
- package/dist/sentiment/types.js.map +0 -1
- package/dist/system-prompt.js.map +0 -1
- package/dist/tool-kit.js.map +0 -1
- package/dist/tools/fundamentals/company-overview.js.map +0 -1
- package/dist/tools/fundamentals/comps.js.map +0 -1
- package/dist/tools/fundamentals/dcf.js.map +0 -1
- package/dist/tools/fundamentals/earnings.js.map +0 -1
- package/dist/tools/fundamentals/financials.js.map +0 -1
- package/dist/tools/fundamentals/sec-filings.js.map +0 -1
- package/dist/tools/index.js.map +0 -1
- package/dist/tools/interaction/ask-user.js.map +0 -1
- package/dist/tools/macro/fear-greed.js.map +0 -1
- package/dist/tools/macro/fred-data.js.map +0 -1
- package/dist/tools/market/crypto-history.js.map +0 -1
- package/dist/tools/market/crypto-price.js.map +0 -1
- package/dist/tools/market/screen-stocks.js.map +0 -1
- package/dist/tools/market/search-ticker.js.map +0 -1
- package/dist/tools/market/stock-history.js.map +0 -1
- package/dist/tools/market/stock-quote.js.map +0 -1
- package/dist/tools/options/greeks.js.map +0 -1
- package/dist/tools/options/option-chain.js.map +0 -1
- package/dist/tools/portfolio/alerts.js.map +0 -1
- package/dist/tools/portfolio/correlation.js.map +0 -1
- package/dist/tools/portfolio/daily-report.js.map +0 -1
- package/dist/tools/portfolio/holdings-overlap.js.map +0 -1
- package/dist/tools/portfolio/notifications.js.map +0 -1
- package/dist/tools/portfolio/risk-analysis.js.map +0 -1
- package/dist/tools/portfolio/tracker.js.map +0 -1
- package/dist/tools/portfolio/watchlist.js.map +0 -1
- package/dist/tools/sentiment/insight-format.js.map +0 -1
- package/dist/tools/sentiment/query-match.js.map +0 -1
- package/dist/tools/sentiment/reddit-sentiment.js.map +0 -1
- package/dist/tools/sentiment/sentiment-summary.js.map +0 -1
- package/dist/tools/sentiment/sentiment-trend.js.map +0 -1
- package/dist/tools/sentiment/twitter-sentiment.js.map +0 -1
- package/dist/tools/sentiment/untrusted-text.js.map +0 -1
- package/dist/tools/sentiment/web-search.js.map +0 -1
- package/dist/tools/sentiment/web-sentiment.js.map +0 -1
- package/dist/tools/technical/backtest.js.map +0 -1
- package/dist/tools/technical/indicators.js.map +0 -1
- package/dist/types/fundamentals.js.map +0 -1
- package/dist/types/index.js.map +0 -1
- package/dist/types/macro.js.map +0 -1
- package/dist/types/market.js.map +0 -1
- package/dist/types/options.js.map +0 -1
- package/dist/types/portfolio.js.map +0 -1
- package/dist/types/sentiment.js.map +0 -1
- package/dist/workflows/compare-assets.js.map +0 -1
- package/dist/workflows/index.js.map +0 -1
- package/dist/workflows/options-screener.js.map +0 -1
- package/dist/workflows/portfolio-builder.js.map +0 -1
- package/gui/server/ask-user-bridge.ts +0 -89
- package/gui/server/automation-heartbeat.ts +0 -97
- package/gui/server/background-quotes.ts +0 -127
- package/gui/server/chat-event-adapter.ts +0 -210
- package/gui/server/chat-run-session.ts +0 -16
- package/gui/server/gui-session-manager.ts +0 -5
- package/gui/server/http-routes.ts +0 -953
- package/gui/server/invoke-tool.ts +0 -489
- package/gui/server/live-chat-event-adapter.ts +0 -208
- package/gui/server/local-session-coordinator.ts +0 -97
- package/gui/server/market-state-api.ts +0 -273
- package/gui/server/model-setup.ts +0 -254
- package/gui/server/package.json +0 -5
- package/gui/server/private-api-access.ts +0 -62
- package/gui/server/projector.ts +0 -263
- package/gui/server/prompt-observation.ts +0 -58
- package/gui/server/quote-snapshot-store.ts +0 -50
- package/gui/server/server.ts +0 -289
- package/gui/server/session-actions.ts +0 -331
- package/gui/server/session-entry-wait.ts +0 -160
- package/gui/server/shutdown.ts +0 -47
- package/gui/server/tool-invoke-ack.ts +0 -49
- package/gui/server/tool-metadata.ts +0 -167
- package/gui/server/websocket.ts +0 -138
- package/gui/server/writer-lock.ts +0 -1
- package/gui/server/ws-hub.ts +0 -391
- package/gui/shared/chat-events.ts +0 -156
- package/gui/shared/event-reducer.ts +0 -219
- package/gui/web/dist/assets/CatalogOverlay-ChRTKNlf.js +0 -1
- package/gui/web/dist/assets/index-BzyqyVnd.css +0 -2
- package/gui/web/dist/assets/index-DvMjkOP9.js +0 -65
- package/src/analysts/contracts.ts +0 -176
- package/src/analysts/orchestrator.ts +0 -224
- package/src/cli-main.ts +0 -510
- package/src/cli.ts +0 -17
- package/src/config.ts +0 -272
- package/src/doctor/cli-command.ts +0 -83
- package/src/doctor/render.ts +0 -37
- package/src/doctor/report.ts +0 -638
- package/src/index.ts +0 -5
- package/src/infra/cache.ts +0 -114
- package/src/infra/http-client.ts +0 -134
- package/src/infra/index.ts +0 -14
- package/src/infra/native-dependencies.ts +0 -84
- package/src/infra/node-version.ts +0 -23
- package/src/infra/open-url.ts +0 -28
- package/src/infra/opencandle-paths.ts +0 -53
- package/src/infra/rate-limiter.ts +0 -76
- package/src/market-state/alert-conditions.ts +0 -82
- package/src/market-state/alert-runner.ts +0 -863
- package/src/market-state/daily-report.ts +0 -237
- package/src/market-state/local-automation-service.ts +0 -162
- package/src/market-state/notification-delivery.ts +0 -158
- package/src/market-state/resolve-for-mutation.ts +0 -24
- package/src/market-state/resolve.ts +0 -112
- package/src/market-state/service.ts +0 -2208
- package/src/memory/index.ts +0 -10
- package/src/memory/manager.ts +0 -190
- package/src/memory/preference-extractor.ts +0 -106
- package/src/memory/retrieval.ts +0 -71
- package/src/memory/sqlite.ts +0 -577
- package/src/memory/storage.ts +0 -195
- package/src/memory/tool-defaults.ts +0 -108
- package/src/memory/types.ts +0 -71
- package/src/monitor.ts +0 -123
- package/src/onboarding/connect.ts +0 -177
- package/src/onboarding/credential-interceptor.ts +0 -122
- package/src/onboarding/degradation-accumulator.ts +0 -77
- package/src/onboarding/prompt-user.ts +0 -85
- package/src/onboarding/provider-status.ts +0 -382
- package/src/onboarding/providers.ts +0 -414
- package/src/onboarding/state.ts +0 -216
- package/src/onboarding/tool-helpers.ts +0 -104
- package/src/onboarding/tool-tags.ts +0 -244
- package/src/onboarding/validation.ts +0 -152
- package/src/pi/opencandle-extension.ts +0 -1244
- package/src/pi/session-action-dedupe.ts +0 -155
- package/src/pi/session-storage.ts +0 -5
- package/src/pi/session-writer-lock.ts +0 -426
- package/src/pi/session.ts +0 -84
- package/src/pi/setup.ts +0 -399
- package/src/pi/tool-adapter.ts +0 -49
- package/src/pi/tui-session-coordinator.ts +0 -351
- package/src/prompts/context-builder.ts +0 -326
- package/src/prompts/disclaimer.ts +0 -9
- package/src/prompts/policy-cards.ts +0 -228
- package/src/prompts/sections.ts +0 -46
- package/src/prompts/symbol-preflight.ts +0 -80
- package/src/prompts/workflow-prompts.ts +0 -504
- package/src/providers/alpha-vantage.ts +0 -334
- package/src/providers/coingecko.ts +0 -93
- package/src/providers/errors.ts +0 -9
- package/src/providers/exa-search.ts +0 -375
- package/src/providers/external-tool-command.ts +0 -164
- package/src/providers/external-tool-error.ts +0 -20
- package/src/providers/fear-greed.ts +0 -45
- package/src/providers/finnhub.ts +0 -125
- package/src/providers/fred.ts +0 -83
- package/src/providers/index.ts +0 -17
- package/src/providers/provider-credential-error.ts +0 -23
- package/src/providers/reddit-cli.ts +0 -286
- package/src/providers/reddit.ts +0 -106
- package/src/providers/sec-edgar.ts +0 -326
- package/src/providers/tradingview.ts +0 -399
- package/src/providers/twitter-cli.ts +0 -202
- package/src/providers/twitter.ts +0 -102
- package/src/providers/web-search.ts +0 -303
- package/src/providers/with-fallback.ts +0 -42
- package/src/providers/wrap-provider.ts +0 -103
- package/src/providers/yahoo-finance.ts +0 -672
- package/src/routing/classify-intent.ts +0 -380
- package/src/routing/defaults.ts +0 -29
- package/src/routing/entity-extractor.ts +0 -540
- package/src/routing/fund-symbols.ts +0 -58
- package/src/routing/horizon.ts +0 -7
- package/src/routing/index.ts +0 -70
- package/src/routing/legacy-rule-router.ts +0 -13
- package/src/routing/planning.ts +0 -829
- package/src/routing/route-manifest.ts +0 -308
- package/src/routing/router-llm-client.ts +0 -52
- package/src/routing/router-prompt.ts +0 -159
- package/src/routing/router-types.ts +0 -84
- package/src/routing/router.ts +0 -997
- package/src/routing/slot-resolver.ts +0 -213
- package/src/routing/symbol-disambiguator.ts +0 -72
- package/src/routing/turn-context.ts +0 -108
- package/src/routing/types.ts +0 -77
- package/src/runtime/answer-contracts.ts +0 -693
- package/src/runtime/artifact-contracts.ts +0 -77
- package/src/runtime/evidence.ts +0 -77
- package/src/runtime/planning-evidence.ts +0 -682
- package/src/runtime/prompt-step.ts +0 -60
- package/src/runtime/provider-tracker.ts +0 -40
- package/src/runtime/run-context.ts +0 -32
- package/src/runtime/session-coordinator.ts +0 -632
- package/src/runtime/session-title.ts +0 -60
- package/src/runtime/tool-defaults-wrapper.ts +0 -43
- package/src/runtime/validation.ts +0 -211
- package/src/runtime/workflow-events.ts +0 -75
- package/src/runtime/workflow-runner.ts +0 -182
- package/src/runtime/workflow-types.ts +0 -102
- package/src/sentiment/adapters/finnhub.ts +0 -49
- package/src/sentiment/adapters/reddit.ts +0 -65
- package/src/sentiment/adapters/twitter.ts +0 -36
- package/src/sentiment/adapters/web.ts +0 -44
- package/src/sentiment/index.ts +0 -49
- package/src/sentiment/insights.ts +0 -269
- package/src/sentiment/keywords.ts +0 -31
- package/src/sentiment/pipeline.ts +0 -91
- package/src/sentiment/scorer.ts +0 -89
- package/src/sentiment/store.ts +0 -260
- package/src/sentiment/trends.ts +0 -96
- package/src/sentiment/types.ts +0 -112
- package/src/system-prompt.ts +0 -115
- package/src/tool-kit.ts +0 -69
- package/src/tools/AGENTS.md +0 -36
- package/src/tools/fundamentals/company-overview.ts +0 -64
- package/src/tools/fundamentals/comps.ts +0 -169
- package/src/tools/fundamentals/dcf.ts +0 -367
- package/src/tools/fundamentals/earnings.ts +0 -57
- package/src/tools/fundamentals/financials.ts +0 -63
- package/src/tools/fundamentals/sec-filings.ts +0 -105
- package/src/tools/index.ts +0 -101
- package/src/tools/interaction/ask-user.ts +0 -93
- package/src/tools/macro/fear-greed.ts +0 -41
- package/src/tools/macro/fred-data.ts +0 -92
- package/src/tools/market/crypto-history.ts +0 -72
- package/src/tools/market/crypto-price.ts +0 -53
- package/src/tools/market/screen-stocks.ts +0 -279
- package/src/tools/market/search-ticker.ts +0 -254
- package/src/tools/market/stock-history.ts +0 -104
- package/src/tools/market/stock-quote.ts +0 -67
- package/src/tools/options/greeks.ts +0 -81
- package/src/tools/options/option-chain.ts +0 -120
- package/src/tools/portfolio/alerts.ts +0 -457
- package/src/tools/portfolio/correlation.ts +0 -189
- package/src/tools/portfolio/daily-report.ts +0 -107
- package/src/tools/portfolio/holdings-overlap.ts +0 -139
- package/src/tools/portfolio/notifications.ts +0 -45
- package/src/tools/portfolio/risk-analysis.ts +0 -173
- package/src/tools/portfolio/tracker.ts +0 -308
- package/src/tools/portfolio/watchlist.ts +0 -288
- package/src/tools/sentiment/insight-format.ts +0 -50
- package/src/tools/sentiment/query-match.ts +0 -117
- package/src/tools/sentiment/reddit-sentiment.ts +0 -403
- package/src/tools/sentiment/sentiment-summary.ts +0 -383
- package/src/tools/sentiment/sentiment-trend.ts +0 -75
- package/src/tools/sentiment/twitter-sentiment.ts +0 -287
- package/src/tools/sentiment/untrusted-text.ts +0 -21
- package/src/tools/sentiment/web-search.ts +0 -186
- package/src/tools/sentiment/web-sentiment.ts +0 -96
- package/src/tools/technical/backtest.ts +0 -336
- package/src/tools/technical/indicators.ts +0 -281
- package/src/types/fundamentals.ts +0 -46
- package/src/types/index.ts +0 -25
- package/src/types/macro.ts +0 -27
- package/src/types/market.ts +0 -44
- package/src/types/options.ts +0 -52
- package/src/types/portfolio.ts +0 -83
- package/src/types/sentiment.ts +0 -129
- package/src/workflows/compare-assets.ts +0 -89
- package/src/workflows/index.ts +0 -3
- package/src/workflows/options-screener.ts +0 -125
- package/src/workflows/portfolio-builder.ts +0 -63
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import { persistProviderCredential } from "../../onboarding/connect.js";
|
|
2
|
+
import { getCredentialSource, isApiKeyProvider, PROVIDERS, } from "../../onboarding/providers.js";
|
|
3
|
+
import { validateModelKey, } from "../../onboarding/validate-model-key.js";
|
|
4
|
+
import { validateCredential } from "../../onboarding/validation.js";
|
|
5
|
+
export const modelSetupProviders = [
|
|
6
|
+
{
|
|
7
|
+
id: "google",
|
|
8
|
+
label: "Google Gemini",
|
|
9
|
+
envVar: "GEMINI_API_KEY",
|
|
10
|
+
defaultProvider: "google",
|
|
11
|
+
defaultModel: "gemini-2.5-flash",
|
|
12
|
+
signupUrl: "https://aistudio.google.com/app/apikey",
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
id: "openai",
|
|
16
|
+
label: "OpenAI",
|
|
17
|
+
envVar: "OPENAI_API_KEY",
|
|
18
|
+
defaultProvider: "openai",
|
|
19
|
+
defaultModel: "gpt-5-mini",
|
|
20
|
+
signupUrl: "https://platform.openai.com/api-keys",
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
id: "anthropic",
|
|
24
|
+
label: "Anthropic",
|
|
25
|
+
envVar: "ANTHROPIC_API_KEY",
|
|
26
|
+
defaultProvider: "anthropic",
|
|
27
|
+
defaultModel: "claude-haiku-4-5",
|
|
28
|
+
signupUrl: "https://console.anthropic.com/settings/keys",
|
|
29
|
+
},
|
|
30
|
+
];
|
|
31
|
+
export function buildModelSetupState(registry, currentModel) {
|
|
32
|
+
registry.refresh();
|
|
33
|
+
const availableModels = sortModels(registry.getAvailable()).map((model) => ({
|
|
34
|
+
provider: model.provider,
|
|
35
|
+
id: model.id,
|
|
36
|
+
label: `${model.provider}/${model.id}`,
|
|
37
|
+
}));
|
|
38
|
+
const requirement = currentModel && registry.hasConfiguredAuth(currentModel)
|
|
39
|
+
? "ready"
|
|
40
|
+
: availableModels.length > 0
|
|
41
|
+
? "select_model"
|
|
42
|
+
: "connect_auth";
|
|
43
|
+
return {
|
|
44
|
+
requirement,
|
|
45
|
+
// A fresh session still carries a placeholder model with no usable
|
|
46
|
+
// credentials; reporting it would render its raw id as the composer label.
|
|
47
|
+
currentModel: currentModel && registry.hasConfiguredAuth(currentModel)
|
|
48
|
+
? `${currentModel.provider}/${currentModel.id}`
|
|
49
|
+
: undefined,
|
|
50
|
+
providers: modelSetupProviders,
|
|
51
|
+
availableModels,
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
export function findPreferredModel(registry, provider) {
|
|
55
|
+
const available = sortModels(registry.getAvailable(), provider.defaultProvider);
|
|
56
|
+
return (available.find((model) => model.provider === provider.defaultProvider && model.id === provider.defaultModel) ?? available.find((model) => model.provider === provider.defaultProvider));
|
|
57
|
+
}
|
|
58
|
+
export function sortModels(models, preferredProvider) {
|
|
59
|
+
return [...models].sort((a, b) => {
|
|
60
|
+
const aPreferred = preferredProvider && a.provider === preferredProvider ? -1 : 0;
|
|
61
|
+
const bPreferred = preferredProvider && b.provider === preferredProvider ? -1 : 0;
|
|
62
|
+
if (aPreferred !== bPreferred)
|
|
63
|
+
return aPreferred - bPreferred;
|
|
64
|
+
const byProvider = a.provider.localeCompare(b.provider);
|
|
65
|
+
return byProvider !== 0 ? byProvider : a.id.localeCompare(b.id);
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
export function createModelSetupController({ role, getSession, getSessionManager, broadcastState, }) {
|
|
69
|
+
function ensureWriter() {
|
|
70
|
+
if (role !== "writer")
|
|
71
|
+
throw new Error("Read-only follower mode");
|
|
72
|
+
}
|
|
73
|
+
function buildCurrentModelSetupState() {
|
|
74
|
+
const session = getSession();
|
|
75
|
+
return buildModelSetupState(session.modelRegistry, session.model);
|
|
76
|
+
}
|
|
77
|
+
async function handleSaveModelApiKey(providerId, apiKey) {
|
|
78
|
+
ensureWriter();
|
|
79
|
+
const provider = modelSetupProviders.find((candidate) => candidate.id === providerId);
|
|
80
|
+
if (!provider)
|
|
81
|
+
throw new Error(`Unknown model provider: ${providerId}`);
|
|
82
|
+
const trimmed = apiKey.trim();
|
|
83
|
+
if (!trimmed)
|
|
84
|
+
throw new Error(`Paste a ${provider.label} API key first.`);
|
|
85
|
+
const validation = await validateModelKey(provider.id, trimmed);
|
|
86
|
+
if (validation.status === "invalid") {
|
|
87
|
+
throw new Error(`Key was rejected by ${validation.providerLabel}. The existing configuration was not changed.`);
|
|
88
|
+
}
|
|
89
|
+
const session = getSession();
|
|
90
|
+
session.modelRegistry.authStorage.set(provider.id, { type: "api_key", key: trimmed });
|
|
91
|
+
session.modelRegistry.refresh();
|
|
92
|
+
const model = findPreferredModel(session.modelRegistry, provider);
|
|
93
|
+
if (!model) {
|
|
94
|
+
throw new Error(`Saved the ${provider.label} key, but no ${provider.label} models are available yet.`);
|
|
95
|
+
}
|
|
96
|
+
await session.setModel(model);
|
|
97
|
+
await session.settingsManager.flush();
|
|
98
|
+
getSessionManager().appendCustomMessageEntry("opencandle-model-setup", validation.status === "transient"
|
|
99
|
+
? `Saved — couldn't verify (network issue). Connected ${provider.label} and selected ${model.provider}/${model.id}.`
|
|
100
|
+
: `Connected ${provider.label} and selected ${model.provider}/${model.id}.`, true, { source: "gui", provider: provider.id, model: `${model.provider}/${model.id}` });
|
|
101
|
+
broadcastState();
|
|
102
|
+
}
|
|
103
|
+
async function handleSaveProviderApiKey(providerId, apiKey) {
|
|
104
|
+
ensureWriter();
|
|
105
|
+
const descriptor = PROVIDERS.find((candidate) => candidate.id === providerId);
|
|
106
|
+
if (!descriptor)
|
|
107
|
+
throw new Error(`Unknown provider: ${providerId}`);
|
|
108
|
+
if (!isApiKeyProvider(descriptor)) {
|
|
109
|
+
throw new Error(`${descriptor.displayName} is not configured with an API key.`);
|
|
110
|
+
}
|
|
111
|
+
if (getCredentialSource(descriptor.id) === "env") {
|
|
112
|
+
throw new Error(`${descriptor.displayName} is set via the ${descriptor.envVar} environment variable. Unset it to override here.`);
|
|
113
|
+
}
|
|
114
|
+
const trimmed = apiKey.trim();
|
|
115
|
+
if (!trimmed)
|
|
116
|
+
throw new Error(`Paste a ${descriptor.displayName} API key first.`);
|
|
117
|
+
const validation = await validateCredential(descriptor.id, trimmed);
|
|
118
|
+
if (validation.status === "invalid") {
|
|
119
|
+
const statusHint = validation.httpStatus !== undefined ? ` (HTTP ${validation.httpStatus})` : "";
|
|
120
|
+
const messageHint = validation.message ? ` — ${validation.message}` : "";
|
|
121
|
+
throw new Error(`${descriptor.displayName} rejected the key${statusHint}${messageHint}. The existing configuration was not changed.`);
|
|
122
|
+
}
|
|
123
|
+
persistProviderCredential(descriptor.id, trimmed);
|
|
124
|
+
const verifiedNote = validation.status === "transient"
|
|
125
|
+
? `Saved ${descriptor.displayName} key but couldn't verify it (${validation.reason}). The next request will surface any issue.`
|
|
126
|
+
: `Connected ${descriptor.displayName}. Key saved to ~/.opencandle/config.json.`;
|
|
127
|
+
getSessionManager().appendCustomMessageEntry("opencandle-provider-setup", verifiedNote, true, {
|
|
128
|
+
source: "gui",
|
|
129
|
+
provider: descriptor.id,
|
|
130
|
+
status: validation.status,
|
|
131
|
+
});
|
|
132
|
+
broadcastState();
|
|
133
|
+
}
|
|
134
|
+
async function handleSelectModel(provider, modelId) {
|
|
135
|
+
ensureWriter();
|
|
136
|
+
const session = getSession();
|
|
137
|
+
session.modelRegistry.refresh();
|
|
138
|
+
const model = session.modelRegistry.find(provider, modelId);
|
|
139
|
+
if (!model)
|
|
140
|
+
throw new Error(`Unknown model: ${provider}/${modelId}`);
|
|
141
|
+
await session.setModel(model);
|
|
142
|
+
await session.settingsManager.flush();
|
|
143
|
+
}
|
|
144
|
+
return {
|
|
145
|
+
buildCurrentModelSetupState,
|
|
146
|
+
handleSaveModelApiKey,
|
|
147
|
+
handleSaveProviderApiKey,
|
|
148
|
+
handleSelectModel,
|
|
149
|
+
};
|
|
150
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { IncomingHttpHeaders } from "node:http";
|
|
2
|
+
export declare function isLoopbackAddress(remoteAddress: string | undefined): boolean;
|
|
3
|
+
export declare function isTrustedPrivateApiRequest(headers: IncomingHttpHeaders, sessionToken: string, remoteAddress: string | undefined, options?: {
|
|
4
|
+
allowRemote?: boolean;
|
|
5
|
+
}): boolean;
|
|
6
|
+
export declare function privateApiCookieHeader(sessionToken: string): string;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
const PRIVATE_API_COOKIE = "opencandle_gui_session";
|
|
2
|
+
export function isLoopbackAddress(remoteAddress) {
|
|
3
|
+
if (remoteAddress == null || remoteAddress === "")
|
|
4
|
+
return false;
|
|
5
|
+
return (remoteAddress === "::1" ||
|
|
6
|
+
remoteAddress === "localhost" ||
|
|
7
|
+
remoteAddress.startsWith("127.") ||
|
|
8
|
+
remoteAddress.startsWith("::ffff:127."));
|
|
9
|
+
}
|
|
10
|
+
export function isTrustedPrivateApiRequest(headers, sessionToken, remoteAddress, options = {}) {
|
|
11
|
+
if (!options.allowRemote && !isLoopbackAddress(remoteAddress))
|
|
12
|
+
return false;
|
|
13
|
+
if (cookieValue(headers.cookie, PRIVATE_API_COOKIE) !== sessionToken)
|
|
14
|
+
return false;
|
|
15
|
+
const fetchSite = headerValue(headers["sec-fetch-site"]);
|
|
16
|
+
if (fetchSite != null && fetchSite !== "same-origin" && fetchSite !== "none")
|
|
17
|
+
return false;
|
|
18
|
+
const origin = headerValue(headers.origin);
|
|
19
|
+
const host = headerValue(headers.host);
|
|
20
|
+
if (origin != null && host != null) {
|
|
21
|
+
try {
|
|
22
|
+
if (new URL(origin).host !== host)
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
catch {
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return true;
|
|
30
|
+
}
|
|
31
|
+
export function privateApiCookieHeader(sessionToken) {
|
|
32
|
+
return `${PRIVATE_API_COOKIE}=${encodeURIComponent(sessionToken)}; Path=/; HttpOnly; SameSite=Strict`;
|
|
33
|
+
}
|
|
34
|
+
function cookieValue(header, name) {
|
|
35
|
+
const value = Array.isArray(header) ? header.join("; ") : header;
|
|
36
|
+
if (value == null)
|
|
37
|
+
return undefined;
|
|
38
|
+
for (const part of value.split(";")) {
|
|
39
|
+
const [rawKey, ...rawValue] = part.trim().split("=");
|
|
40
|
+
if (rawKey === name) {
|
|
41
|
+
try {
|
|
42
|
+
return decodeURIComponent(rawValue.join("="));
|
|
43
|
+
}
|
|
44
|
+
catch {
|
|
45
|
+
return undefined;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return undefined;
|
|
50
|
+
}
|
|
51
|
+
function headerValue(value) {
|
|
52
|
+
return Array.isArray(value) ? value[0] : value;
|
|
53
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { SessionEntry } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
export interface DashboardState {
|
|
3
|
+
knownSymbols: string[];
|
|
4
|
+
watchlist: Array<{
|
|
5
|
+
symbol: string;
|
|
6
|
+
quote: Record<string, unknown> | null;
|
|
7
|
+
pinned: boolean;
|
|
8
|
+
lastSeen: string;
|
|
9
|
+
}>;
|
|
10
|
+
activeAnalyses: Array<{
|
|
11
|
+
workflowId: string;
|
|
12
|
+
workflow: string;
|
|
13
|
+
symbol?: string;
|
|
14
|
+
analystsTotal: number;
|
|
15
|
+
analystsDone: number;
|
|
16
|
+
startedAt: string;
|
|
17
|
+
}>;
|
|
18
|
+
recentResearch: Array<{
|
|
19
|
+
sessionId: string;
|
|
20
|
+
workflow: string;
|
|
21
|
+
symbol?: string;
|
|
22
|
+
completedAt: string;
|
|
23
|
+
}>;
|
|
24
|
+
dataQuality: {
|
|
25
|
+
softGaps: Array<{
|
|
26
|
+
provider: string;
|
|
27
|
+
lastSeen: string;
|
|
28
|
+
}>;
|
|
29
|
+
hardSkips: Array<{
|
|
30
|
+
provider: string;
|
|
31
|
+
lastSeen: string;
|
|
32
|
+
}>;
|
|
33
|
+
};
|
|
34
|
+
lastTurn?: {
|
|
35
|
+
routeKind: string;
|
|
36
|
+
workflow?: string;
|
|
37
|
+
symbols: string[];
|
|
38
|
+
slotSources: Record<string, number>;
|
|
39
|
+
priorTurnCount: number;
|
|
40
|
+
savedStateIncluded?: boolean;
|
|
41
|
+
attachmentCount?: number;
|
|
42
|
+
validation?: {
|
|
43
|
+
passed: boolean;
|
|
44
|
+
mismatchCount: number;
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
export declare function createEmptyDashboardState(): DashboardState;
|
|
49
|
+
export declare function projectDashboard(entries: SessionEntry[], sessionId?: string, savedSymbols?: string[]): DashboardState;
|
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
const DIRECT_TOOL_GAP_PROVIDERS = {
|
|
2
|
+
get_company_overview: "alpha_vantage",
|
|
3
|
+
get_financials: "alpha_vantage",
|
|
4
|
+
get_earnings: "alpha_vantage",
|
|
5
|
+
compute_dcf: "alpha_vantage",
|
|
6
|
+
compare_companies: "alpha_vantage",
|
|
7
|
+
get_economic_data: "fred",
|
|
8
|
+
get_twitter_sentiment: "twitter",
|
|
9
|
+
get_reddit_sentiment: "reddit",
|
|
10
|
+
};
|
|
11
|
+
export function createEmptyDashboardState() {
|
|
12
|
+
return {
|
|
13
|
+
knownSymbols: [],
|
|
14
|
+
watchlist: [],
|
|
15
|
+
activeAnalyses: [],
|
|
16
|
+
recentResearch: [],
|
|
17
|
+
dataQuality: { softGaps: [], hardSkips: [] },
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
export function projectDashboard(entries, sessionId = "local", savedSymbols = []) {
|
|
21
|
+
const state = createEmptyDashboardState();
|
|
22
|
+
let pendingAttachmentCount;
|
|
23
|
+
let pendingAttachmentUserSeen = false;
|
|
24
|
+
for (const entry of entries) {
|
|
25
|
+
if (entry.type === "message") {
|
|
26
|
+
projectMessage(state, entry.message, entry.timestamp, sessionId);
|
|
27
|
+
const message = entry.message;
|
|
28
|
+
if (pendingAttachmentCount !== undefined) {
|
|
29
|
+
if (message.role === "user")
|
|
30
|
+
pendingAttachmentUserSeen = true;
|
|
31
|
+
else if (pendingAttachmentUserSeen) {
|
|
32
|
+
pendingAttachmentCount = undefined;
|
|
33
|
+
pendingAttachmentUserSeen = false;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
continue;
|
|
37
|
+
}
|
|
38
|
+
if (entry.type === "custom" && entry.customType === "opencandle-workflow") {
|
|
39
|
+
const data = asRecord(entry.data);
|
|
40
|
+
const workflow = stringValue(data.workflow) ?? stringValue(data.workflowType) ?? "workflow";
|
|
41
|
+
const slots = asRecord(data.resolvedSlots);
|
|
42
|
+
const symbol = stringValue(slots.symbol) ?? firstString(slots.symbols);
|
|
43
|
+
state.activeAnalyses.push({
|
|
44
|
+
workflowId: stringValue(data.runId) ?? entry.id,
|
|
45
|
+
workflow,
|
|
46
|
+
symbol,
|
|
47
|
+
analystsTotal: numberValue(data.analystsTotal) ?? 0,
|
|
48
|
+
analystsDone: 0,
|
|
49
|
+
startedAt: entry.timestamp,
|
|
50
|
+
});
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
if (entry.type === "custom" && entry.customType === "opencandle-user-input") {
|
|
54
|
+
const attachments = asArray(asRecord(entry.data).attachments);
|
|
55
|
+
pendingAttachmentCount = attachments.length > 0 ? attachments.length : undefined;
|
|
56
|
+
pendingAttachmentUserSeen = false;
|
|
57
|
+
if (state.lastTurn) {
|
|
58
|
+
if (pendingAttachmentCount !== undefined)
|
|
59
|
+
state.lastTurn.attachmentCount = pendingAttachmentCount;
|
|
60
|
+
else
|
|
61
|
+
delete state.lastTurn.attachmentCount;
|
|
62
|
+
}
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
if (entry.type === "custom" && entry.customType === "opencandle-route-context") {
|
|
66
|
+
state.lastTurn = projectRouteContext(entry.data, pendingAttachmentCount);
|
|
67
|
+
addKnownSymbols(state, asArray(asRecord(asRecord(entry.data).entities).symbols));
|
|
68
|
+
pendingAttachmentCount = undefined;
|
|
69
|
+
pendingAttachmentUserSeen = false;
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
if (pendingAttachmentUserSeen) {
|
|
73
|
+
pendingAttachmentCount = undefined;
|
|
74
|
+
pendingAttachmentUserSeen = false;
|
|
75
|
+
}
|
|
76
|
+
if (entry.type === "custom" && entry.customType === "opencandle-validation") {
|
|
77
|
+
if (state.lastTurn) {
|
|
78
|
+
const data = asRecord(entry.data);
|
|
79
|
+
state.lastTurn.validation = {
|
|
80
|
+
passed: Boolean(data.passed),
|
|
81
|
+
mismatchCount: asArray(data.mismatches).length,
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
continue;
|
|
85
|
+
}
|
|
86
|
+
if (entry.type === "custom" && entry.customType === "opencandle-analyst-step") {
|
|
87
|
+
// debate_* stages share this entry type but are not analysts.
|
|
88
|
+
const stage = stringValue(asRecord(entry.data).stage) ?? "";
|
|
89
|
+
const active = state.activeAnalyses[state.activeAnalyses.length - 1];
|
|
90
|
+
if (active && stage.startsWith("analyst_")) {
|
|
91
|
+
const nextDone = active.analystsDone + 1;
|
|
92
|
+
active.analystsDone =
|
|
93
|
+
active.analystsTotal > 0 ? Math.min(nextDone, active.analystsTotal) : nextDone;
|
|
94
|
+
}
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
if (entry.type === "custom" && entry.customType === "opencandle-turn-gap") {
|
|
98
|
+
// The accumulator writes a single combined annotation string with one
|
|
99
|
+
// [OPENCANDLE_SKIPPED ... provider=X ...] tag per fallback provider.
|
|
100
|
+
// Older shapes may carry { softGaps: [...] } or { provider } directly —
|
|
101
|
+
// accept either to stay forward/backward compatible.
|
|
102
|
+
const data = asRecord(entry.data);
|
|
103
|
+
const annotation = stringValue(data.annotation);
|
|
104
|
+
if (annotation) {
|
|
105
|
+
for (const provider of parseSkippedProviders(annotation)) {
|
|
106
|
+
upsertProviderGap(state.dataQuality.softGaps, provider, entry.timestamp);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
for (const gap of asArray(data.softGaps)) {
|
|
110
|
+
const provider = stringValue(asRecord(gap).provider);
|
|
111
|
+
if (provider)
|
|
112
|
+
upsertProviderGap(state.dataQuality.softGaps, provider, entry.timestamp);
|
|
113
|
+
}
|
|
114
|
+
const provider = stringValue(data.provider);
|
|
115
|
+
if (provider)
|
|
116
|
+
upsertProviderGap(state.dataQuality.softGaps, provider, entry.timestamp);
|
|
117
|
+
}
|
|
118
|
+
if (entry.type === "custom" && entry.customType === "opencandle-quote-refresh") {
|
|
119
|
+
const data = asRecord(entry.data);
|
|
120
|
+
projectQuote(state, stringValue(data.symbol), asRecord(data.value), asArray(data.content), entry.timestamp);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
addKnownSymbols(state, savedSymbols);
|
|
124
|
+
return state;
|
|
125
|
+
}
|
|
126
|
+
function projectRouteContext(data, attachmentCount) {
|
|
127
|
+
const record = asRecord(data);
|
|
128
|
+
const entities = asRecord(record.entities);
|
|
129
|
+
const routeKind = stringValue(record.routeKind) ?? "unknown";
|
|
130
|
+
const workflow = stringValue(record.workflow);
|
|
131
|
+
const slots = asRecord(record.slots);
|
|
132
|
+
const slotSources = {};
|
|
133
|
+
for (const slot of Object.values(slots)) {
|
|
134
|
+
const source = stringValue(asRecord(slot).source);
|
|
135
|
+
if (!source)
|
|
136
|
+
continue;
|
|
137
|
+
slotSources[source] = (slotSources[source] ?? 0) + 1;
|
|
138
|
+
}
|
|
139
|
+
const symbols = stringArray(entities.symbols);
|
|
140
|
+
const priorTurnCount = asArray(record.priorTurns).length;
|
|
141
|
+
return {
|
|
142
|
+
routeKind,
|
|
143
|
+
...(workflow ? { workflow } : {}),
|
|
144
|
+
symbols,
|
|
145
|
+
slotSources,
|
|
146
|
+
priorTurnCount,
|
|
147
|
+
...(typeof record.savedStateIncluded === "boolean"
|
|
148
|
+
? { savedStateIncluded: record.savedStateIncluded }
|
|
149
|
+
: {}),
|
|
150
|
+
...(attachmentCount !== undefined ? { attachmentCount } : {}),
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
function projectMessage(state, message, timestamp, sessionId) {
|
|
154
|
+
if (message.role === "toolResult") {
|
|
155
|
+
projectToolResult(state, message, timestamp);
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
if (message.role === "assistant" && message.stopReason === "stop") {
|
|
159
|
+
const active = state.activeAnalyses.shift();
|
|
160
|
+
if (active) {
|
|
161
|
+
state.recentResearch.unshift({
|
|
162
|
+
sessionId,
|
|
163
|
+
workflow: active.workflow,
|
|
164
|
+
symbol: active.symbol,
|
|
165
|
+
completedAt: timestamp,
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
function projectToolResult(state, message, timestamp) {
|
|
171
|
+
if (message.toolName === "get_stock_quote") {
|
|
172
|
+
const rawDetails = asRecord(message.details);
|
|
173
|
+
const nestedValue = asRecord(rawDetails.value);
|
|
174
|
+
const details = Object.keys(nestedValue).length > 0 ? nestedValue : rawDetails;
|
|
175
|
+
projectQuote(state, stringValue(details.symbol), details, message.content, timestamp);
|
|
176
|
+
}
|
|
177
|
+
const text = message.content
|
|
178
|
+
.filter((part) => part.type === "text")
|
|
179
|
+
.map((part) => part.text)
|
|
180
|
+
.join("\n");
|
|
181
|
+
for (const provider of parseSoftGapProviders(text)) {
|
|
182
|
+
upsertProviderGap(state.dataQuality.softGaps, provider, timestamp);
|
|
183
|
+
}
|
|
184
|
+
for (const provider of parseCredentialRequiredProviders(text)) {
|
|
185
|
+
upsertProviderGap(state.dataQuality.hardSkips, provider, timestamp);
|
|
186
|
+
}
|
|
187
|
+
const provider = inferDirectToolGapProvider(message.toolName, text);
|
|
188
|
+
if (provider)
|
|
189
|
+
upsertProviderGap(state.dataQuality.softGaps, provider, timestamp);
|
|
190
|
+
}
|
|
191
|
+
function upsertProviderGap(gaps, provider, lastSeen) {
|
|
192
|
+
const existing = gaps.find((gap) => gap.provider === provider);
|
|
193
|
+
if (!existing) {
|
|
194
|
+
gaps.push({ provider, lastSeen });
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
const existingTime = Date.parse(existing.lastSeen);
|
|
198
|
+
const nextTime = Date.parse(lastSeen);
|
|
199
|
+
if (!Number.isFinite(existingTime) || (Number.isFinite(nextTime) && nextTime > existingTime)) {
|
|
200
|
+
existing.lastSeen = lastSeen;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
function projectQuote(state, symbolHint, details, content, timestamp) {
|
|
204
|
+
const symbol = symbolHint ?? inferSymbolFromContent(content);
|
|
205
|
+
if (!symbol)
|
|
206
|
+
return;
|
|
207
|
+
const normalizedSymbol = normalizeSymbol(symbol);
|
|
208
|
+
if (!normalizedSymbol)
|
|
209
|
+
return;
|
|
210
|
+
addKnownSymbols(state, [normalizedSymbol]);
|
|
211
|
+
const existing = state.watchlist.find((row) => row.symbol === normalizedSymbol);
|
|
212
|
+
const row = {
|
|
213
|
+
symbol: normalizedSymbol,
|
|
214
|
+
quote: Object.keys(details).length > 0 ? details : null,
|
|
215
|
+
pinned: existing?.pinned ?? false,
|
|
216
|
+
lastSeen: timestamp,
|
|
217
|
+
};
|
|
218
|
+
if (existing)
|
|
219
|
+
Object.assign(existing, row);
|
|
220
|
+
else
|
|
221
|
+
state.watchlist.push(row);
|
|
222
|
+
}
|
|
223
|
+
function parseCredentialRequiredProviders(text) {
|
|
224
|
+
const providers = [];
|
|
225
|
+
const re = /\[OPENCANDLE_CREDENTIAL_REQUIRED[^\]]*provider=([a-z0-9_-]+)/gi;
|
|
226
|
+
while (true) {
|
|
227
|
+
const match = re.exec(text);
|
|
228
|
+
if (!match)
|
|
229
|
+
break;
|
|
230
|
+
providers.push(match[1]);
|
|
231
|
+
}
|
|
232
|
+
return providers;
|
|
233
|
+
}
|
|
234
|
+
function parseSkippedProviders(text) {
|
|
235
|
+
return parseSoftGapProviders(text);
|
|
236
|
+
}
|
|
237
|
+
function parseSoftGapProviders(text) {
|
|
238
|
+
const providers = [];
|
|
239
|
+
const re = /\[OPENCANDLE_(?:SKIPPED|SOFT_DEGRADED|EXTERNAL_TOOL_REQUIRED)[^\]]*provider=([a-z0-9_-]+)/gi;
|
|
240
|
+
while (true) {
|
|
241
|
+
const match = re.exec(text);
|
|
242
|
+
if (!match)
|
|
243
|
+
break;
|
|
244
|
+
providers.push(match[1]);
|
|
245
|
+
}
|
|
246
|
+
return providers;
|
|
247
|
+
}
|
|
248
|
+
function inferDirectToolGapProvider(toolName, text) {
|
|
249
|
+
if (!toolName || !/(?:⚠|unavailable|No .*data found|EXTERNAL_TOOL_SETUP)/i.test(text)) {
|
|
250
|
+
return undefined;
|
|
251
|
+
}
|
|
252
|
+
return DIRECT_TOOL_GAP_PROVIDERS[toolName];
|
|
253
|
+
}
|
|
254
|
+
function inferSymbolFromContent(content) {
|
|
255
|
+
const text = content.find((part) => part.type === "text")?.text;
|
|
256
|
+
const match = text?.match(/^([A-Z]{1,8})\b/);
|
|
257
|
+
return match?.[1];
|
|
258
|
+
}
|
|
259
|
+
function addKnownSymbols(state, values) {
|
|
260
|
+
for (const value of values) {
|
|
261
|
+
const symbol = normalizeSymbol(value);
|
|
262
|
+
if (!symbol || state.knownSymbols.includes(symbol))
|
|
263
|
+
continue;
|
|
264
|
+
if (state.knownSymbols.length >= 100)
|
|
265
|
+
return;
|
|
266
|
+
state.knownSymbols.push(symbol);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
function normalizeSymbol(value) {
|
|
270
|
+
if (typeof value !== "string")
|
|
271
|
+
return null;
|
|
272
|
+
const normalized = value.trim().toUpperCase();
|
|
273
|
+
return normalized ? normalized : null;
|
|
274
|
+
}
|
|
275
|
+
function asRecord(value) {
|
|
276
|
+
return typeof value === "object" && value !== null && !Array.isArray(value)
|
|
277
|
+
? value
|
|
278
|
+
: {};
|
|
279
|
+
}
|
|
280
|
+
function asArray(value) {
|
|
281
|
+
return Array.isArray(value) ? value : [];
|
|
282
|
+
}
|
|
283
|
+
function stringValue(value) {
|
|
284
|
+
return typeof value === "string" && value.length > 0 ? value : undefined;
|
|
285
|
+
}
|
|
286
|
+
function firstString(value) {
|
|
287
|
+
return Array.isArray(value) ? value.find((item) => typeof item === "string") : undefined;
|
|
288
|
+
}
|
|
289
|
+
function stringArray(value) {
|
|
290
|
+
return Array.isArray(value)
|
|
291
|
+
? value.filter((item) => typeof item === "string")
|
|
292
|
+
: [];
|
|
293
|
+
}
|
|
294
|
+
function numberValue(value) {
|
|
295
|
+
return typeof value === "number" ? value : undefined;
|
|
296
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { AgentSessionEvent } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
export interface PromptObservation {
|
|
3
|
+
userTexts: string[];
|
|
4
|
+
sawAssistantOrTool: boolean;
|
|
5
|
+
}
|
|
6
|
+
export declare function createPromptObservation(): PromptObservation;
|
|
7
|
+
export declare function observePromptEvent(observation: PromptObservation, event: AgentSessionEvent): void;
|
|
8
|
+
export declare function selectReplayPrompt(observation: PromptObservation, originalPrompt: string): string | undefined;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
export function createPromptObservation() {
|
|
2
|
+
return {
|
|
3
|
+
userTexts: [],
|
|
4
|
+
sawAssistantOrTool: false,
|
|
5
|
+
};
|
|
6
|
+
}
|
|
7
|
+
export function observePromptEvent(observation, event) {
|
|
8
|
+
if (event.type === "message_start") {
|
|
9
|
+
const message = event.message;
|
|
10
|
+
if (message.role === "user") {
|
|
11
|
+
observation.userTexts.push(messageTextFromContent(message.content));
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
14
|
+
if (message.role === "assistant") {
|
|
15
|
+
observation.sawAssistantOrTool = true;
|
|
16
|
+
}
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
if (event.type === "tool_execution_start") {
|
|
20
|
+
observation.sawAssistantOrTool = true;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
export function selectReplayPrompt(observation, originalPrompt) {
|
|
24
|
+
if (observation.sawAssistantOrTool)
|
|
25
|
+
return undefined;
|
|
26
|
+
const original = originalPrompt.trim();
|
|
27
|
+
const text = [...observation.userTexts]
|
|
28
|
+
.reverse()
|
|
29
|
+
.map((candidate) => candidate.trim())
|
|
30
|
+
.find((candidate) => candidate.length > 0 && candidate !== original);
|
|
31
|
+
return text || undefined;
|
|
32
|
+
}
|
|
33
|
+
function messageTextFromContent(content) {
|
|
34
|
+
if (typeof content === "string")
|
|
35
|
+
return content;
|
|
36
|
+
if (!Array.isArray(content))
|
|
37
|
+
return "";
|
|
38
|
+
return content
|
|
39
|
+
.map((part) => typeof part === "object" && part !== null && "text" in part && typeof part.text === "string"
|
|
40
|
+
? part.text
|
|
41
|
+
: "")
|
|
42
|
+
.join("");
|
|
43
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { MarketStateQuoteSnapshot } from "./market-state-api.js";
|
|
2
|
+
export declare class QuoteSnapshotStore {
|
|
3
|
+
private readonly build;
|
|
4
|
+
private readonly maxAgeMs;
|
|
5
|
+
private readonly now;
|
|
6
|
+
private snapshot;
|
|
7
|
+
private fetchedAtMs;
|
|
8
|
+
private inFlight;
|
|
9
|
+
private invalidationVersion;
|
|
10
|
+
constructor(build: () => Promise<MarketStateQuoteSnapshot>, maxAgeMs?: number, now?: () => number);
|
|
11
|
+
get(): Promise<MarketStateQuoteSnapshot>;
|
|
12
|
+
invalidate(): void;
|
|
13
|
+
private refresh;
|
|
14
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
export class QuoteSnapshotStore {
|
|
2
|
+
build;
|
|
3
|
+
maxAgeMs;
|
|
4
|
+
now;
|
|
5
|
+
snapshot = null;
|
|
6
|
+
fetchedAtMs = 0;
|
|
7
|
+
inFlight = null;
|
|
8
|
+
invalidationVersion = 0;
|
|
9
|
+
constructor(build, maxAgeMs = 60_000, now = Date.now) {
|
|
10
|
+
this.build = build;
|
|
11
|
+
this.maxAgeMs = maxAgeMs;
|
|
12
|
+
this.now = now;
|
|
13
|
+
}
|
|
14
|
+
async get() {
|
|
15
|
+
if (this.snapshot == null)
|
|
16
|
+
return this.refresh();
|
|
17
|
+
if (this.now() - this.fetchedAtMs >= this.maxAgeMs) {
|
|
18
|
+
// Stale-while-revalidate: serve the old snapshot now, refresh in the background.
|
|
19
|
+
this.refresh().catch(() => { });
|
|
20
|
+
}
|
|
21
|
+
return this.snapshot;
|
|
22
|
+
}
|
|
23
|
+
invalidate() {
|
|
24
|
+
this.invalidationVersion += 1;
|
|
25
|
+
this.snapshot = null;
|
|
26
|
+
this.fetchedAtMs = 0;
|
|
27
|
+
this.inFlight = null;
|
|
28
|
+
}
|
|
29
|
+
refresh() {
|
|
30
|
+
if (this.inFlight)
|
|
31
|
+
return this.inFlight;
|
|
32
|
+
const version = this.invalidationVersion;
|
|
33
|
+
const inFlight = this.build()
|
|
34
|
+
.then((snapshot) => {
|
|
35
|
+
if (version === this.invalidationVersion) {
|
|
36
|
+
this.snapshot = snapshot;
|
|
37
|
+
this.fetchedAtMs = this.now();
|
|
38
|
+
}
|
|
39
|
+
return snapshot;
|
|
40
|
+
})
|
|
41
|
+
.finally(() => {
|
|
42
|
+
if (this.inFlight === inFlight) {
|
|
43
|
+
this.inFlight = null;
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
this.inFlight = inFlight;
|
|
47
|
+
return this.inFlight;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|