opencandle 0.4.0 → 0.6.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/LICENSE +1 -1
- package/README.md +186 -117
- package/dist/analysts/contracts.d.ts +1 -3
- package/dist/analysts/contracts.js +1 -11
- package/dist/analysts/contracts.js.map +1 -1
- package/dist/analysts/orchestrator.d.ts +1 -3
- package/dist/analysts/orchestrator.js +1 -26
- package/dist/analysts/orchestrator.js.map +1 -1
- package/dist/cli.js +32 -8
- package/dist/cli.js.map +1 -1
- package/dist/config.d.ts +19 -3
- package/dist/config.js +69 -3
- package/dist/config.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/infra/browser.d.ts +1 -3
- package/dist/infra/browser.js +4 -2
- package/dist/infra/browser.js.map +1 -1
- package/dist/infra/cache.d.ts +8 -11
- package/dist/infra/cache.js +17 -15
- package/dist/infra/cache.js.map +1 -1
- package/dist/infra/http-client.d.ts +4 -1
- package/dist/infra/http-client.js +59 -6
- package/dist/infra/http-client.js.map +1 -1
- package/dist/infra/index.d.ts +3 -3
- package/dist/infra/index.js +3 -3
- package/dist/infra/index.js.map +1 -1
- package/dist/infra/native-dependencies.js +2 -2
- package/dist/infra/native-dependencies.js.map +1 -1
- package/dist/infra/node-version.js.map +1 -1
- package/dist/infra/opencandle-paths.d.ts +0 -3
- package/dist/infra/opencandle-paths.js +4 -11
- package/dist/infra/opencandle-paths.js.map +1 -1
- package/dist/infra/rate-limiter.d.ts +4 -0
- package/dist/infra/rate-limiter.js +17 -10
- package/dist/infra/rate-limiter.js.map +1 -1
- package/dist/market-state/alert-conditions.d.ts +34 -0
- package/dist/market-state/alert-conditions.js +23 -0
- package/dist/market-state/alert-conditions.js.map +1 -0
- package/dist/market-state/alert-runner.d.ts +55 -0
- package/dist/market-state/alert-runner.js +634 -0
- package/dist/market-state/alert-runner.js.map +1 -0
- package/dist/market-state/daily-report.d.ts +26 -0
- package/dist/market-state/daily-report.js +179 -0
- package/dist/market-state/daily-report.js.map +1 -0
- package/dist/market-state/local-automation-service.d.ts +25 -0
- package/dist/market-state/local-automation-service.js +119 -0
- package/dist/market-state/local-automation-service.js.map +1 -0
- package/dist/market-state/notification-delivery.d.ts +14 -0
- package/dist/market-state/notification-delivery.js +139 -0
- package/dist/market-state/notification-delivery.js.map +1 -0
- package/dist/market-state/resolve-for-mutation.d.ts +10 -0
- package/dist/market-state/resolve-for-mutation.js +15 -0
- package/dist/market-state/resolve-for-mutation.js.map +1 -0
- package/dist/market-state/resolve.d.ts +14 -0
- package/dist/market-state/resolve.js +89 -0
- package/dist/market-state/resolve.js.map +1 -0
- package/dist/market-state/service.d.ts +527 -0
- package/dist/market-state/service.js +1099 -0
- package/dist/market-state/service.js.map +1 -0
- package/dist/memory/index.d.ts +7 -7
- package/dist/memory/index.js +6 -6
- package/dist/memory/index.js.map +1 -1
- package/dist/memory/manager.d.ts +9 -0
- package/dist/memory/manager.js +39 -22
- package/dist/memory/manager.js.map +1 -1
- package/dist/memory/retrieval.js +7 -4
- package/dist/memory/retrieval.js.map +1 -1
- package/dist/memory/sqlite.js +385 -3
- package/dist/memory/sqlite.js.map +1 -1
- package/dist/memory/storage.d.ts +3 -2
- package/dist/memory/storage.js +1 -2
- package/dist/memory/storage.js.map +1 -1
- package/dist/memory/tool-defaults.js +64 -28
- package/dist/memory/tool-defaults.js.map +1 -1
- package/dist/memory/types.js +4 -0
- package/dist/memory/types.js.map +1 -1
- package/dist/monitor.d.ts +2 -0
- package/dist/monitor.js +104 -0
- package/dist/monitor.js.map +1 -0
- package/dist/onboarding/connect.js +4 -6
- package/dist/onboarding/connect.js.map +1 -1
- package/dist/onboarding/credential-interceptor.js +1 -1
- package/dist/onboarding/credential-interceptor.js.map +1 -1
- package/dist/onboarding/degradation-accumulator.js +1 -3
- package/dist/onboarding/degradation-accumulator.js.map +1 -1
- package/dist/onboarding/providers.js +3 -16
- package/dist/onboarding/providers.js.map +1 -1
- package/dist/onboarding/state.js.map +1 -1
- package/dist/onboarding/tool-helpers.js +1 -1
- package/dist/onboarding/tool-helpers.js.map +1 -1
- package/dist/onboarding/tool-tags.js +6 -4
- package/dist/onboarding/tool-tags.js.map +1 -1
- package/dist/onboarding/validation.js +1 -1
- package/dist/onboarding/validation.js.map +1 -1
- package/dist/pi/opencandle-extension.d.ts +8 -0
- package/dist/pi/opencandle-extension.js +637 -59
- package/dist/pi/opencandle-extension.js.map +1 -1
- package/dist/pi/session.d.ts +1 -1
- package/dist/pi/session.js +3 -1
- package/dist/pi/session.js.map +1 -1
- package/dist/pi/setup.js +17 -2
- package/dist/pi/setup.js.map +1 -1
- package/dist/pi/tool-adapter.js +5 -2
- package/dist/pi/tool-adapter.js.map +1 -1
- package/dist/prompts/context-builder.d.ts +18 -3
- package/dist/prompts/context-builder.js +117 -18
- package/dist/prompts/context-builder.js.map +1 -1
- package/dist/prompts/disclaimer.js +1 -1
- package/dist/prompts/disclaimer.js.map +1 -1
- package/dist/prompts/policy-cards.d.ts +13 -0
- package/dist/prompts/policy-cards.js +197 -0
- package/dist/prompts/policy-cards.js.map +1 -0
- package/dist/prompts/sections.d.ts +1 -1
- package/dist/prompts/sections.js +3 -3
- package/dist/prompts/sections.js.map +1 -1
- package/dist/prompts/symbol-preflight.d.ts +20 -0
- package/dist/prompts/symbol-preflight.js +49 -0
- package/dist/prompts/symbol-preflight.js.map +1 -0
- package/dist/prompts/workflow-prompts.d.ts +1 -1
- package/dist/prompts/workflow-prompts.js +209 -19
- package/dist/prompts/workflow-prompts.js.map +1 -1
- package/dist/providers/alpha-vantage.d.ts +1 -1
- package/dist/providers/alpha-vantage.js +49 -8
- package/dist/providers/alpha-vantage.js.map +1 -1
- package/dist/providers/coingecko.js +1 -1
- package/dist/providers/coingecko.js.map +1 -1
- package/dist/providers/errors.d.ts +5 -0
- package/dist/providers/errors.js +11 -0
- package/dist/providers/errors.js.map +1 -0
- package/dist/providers/exa-search.d.ts +2 -2
- package/dist/providers/exa-search.js +19 -11
- package/dist/providers/exa-search.js.map +1 -1
- package/dist/providers/fear-greed.js +1 -1
- package/dist/providers/fear-greed.js.map +1 -1
- package/dist/providers/finnhub.js +3 -5
- package/dist/providers/finnhub.js.map +1 -1
- package/dist/providers/fred.js +2 -2
- package/dist/providers/fred.js.map +1 -1
- package/dist/providers/index.d.ts +7 -6
- package/dist/providers/index.js +6 -5
- package/dist/providers/index.js.map +1 -1
- package/dist/providers/reddit.js +2 -2
- package/dist/providers/reddit.js.map +1 -1
- package/dist/providers/sec-edgar.d.ts +9 -1
- package/dist/providers/sec-edgar.js +181 -6
- package/dist/providers/sec-edgar.js.map +1 -1
- package/dist/providers/tradingview.d.ts +47 -0
- package/dist/providers/tradingview.js +275 -0
- package/dist/providers/tradingview.js.map +1 -0
- package/dist/providers/twitter.js +6 -8
- package/dist/providers/twitter.js.map +1 -1
- package/dist/providers/web-search.js +26 -12
- package/dist/providers/web-search.js.map +1 -1
- package/dist/providers/with-fallback.js +4 -2
- package/dist/providers/with-fallback.js.map +1 -1
- package/dist/providers/wrap-provider.d.ts +2 -3
- package/dist/providers/wrap-provider.js +14 -8
- package/dist/providers/wrap-provider.js.map +1 -1
- package/dist/providers/yahoo-finance.d.ts +3 -1
- package/dist/providers/yahoo-finance.js +226 -11
- package/dist/providers/yahoo-finance.js.map +1 -1
- package/dist/routing/classify-intent.d.ts +9 -0
- package/dist/routing/classify-intent.js +153 -3
- package/dist/routing/classify-intent.js.map +1 -1
- package/dist/routing/defaults.d.ts +1 -1
- package/dist/routing/defaults.js +3 -3
- package/dist/routing/defaults.js.map +1 -1
- package/dist/routing/entity-extractor.d.ts +2 -0
- package/dist/routing/entity-extractor.js +377 -26
- package/dist/routing/entity-extractor.js.map +1 -1
- package/dist/routing/fund-symbols.d.ts +2 -0
- package/dist/routing/fund-symbols.js +55 -0
- package/dist/routing/fund-symbols.js.map +1 -0
- package/dist/routing/horizon.d.ts +1 -0
- package/dist/routing/horizon.js +10 -0
- package/dist/routing/horizon.js.map +1 -0
- package/dist/routing/index.d.ts +12 -6
- package/dist/routing/index.js +8 -4
- package/dist/routing/index.js.map +1 -1
- package/dist/routing/legacy-rule-router.d.ts +9 -0
- package/dist/routing/legacy-rule-router.js +12 -0
- package/dist/routing/legacy-rule-router.js.map +1 -0
- package/dist/routing/planning.d.ts +54 -0
- package/dist/routing/planning.js +562 -0
- package/dist/routing/planning.js.map +1 -0
- package/dist/routing/route-manifest.d.ts +35 -0
- package/dist/routing/route-manifest.js +242 -0
- package/dist/routing/route-manifest.js.map +1 -0
- package/dist/routing/router-llm-client.js.map +1 -1
- package/dist/routing/router-prompt.js +46 -45
- package/dist/routing/router-prompt.js.map +1 -1
- package/dist/routing/router-types.d.ts +10 -0
- package/dist/routing/router.d.ts +1 -0
- package/dist/routing/router.js +572 -13
- package/dist/routing/router.js.map +1 -1
- package/dist/routing/slot-resolver.d.ts +1 -1
- package/dist/routing/slot-resolver.js +45 -7
- package/dist/routing/slot-resolver.js.map +1 -1
- package/dist/routing/symbol-disambiguator.d.ts +11 -0
- package/dist/routing/symbol-disambiguator.js +52 -0
- package/dist/routing/symbol-disambiguator.js.map +1 -0
- package/dist/routing/turn-context.d.ts +44 -0
- package/dist/routing/turn-context.js +45 -0
- package/dist/routing/turn-context.js.map +1 -0
- package/dist/routing/types.d.ts +15 -1
- package/dist/runtime/answer-contracts.d.ts +82 -0
- package/dist/runtime/answer-contracts.js +442 -0
- package/dist/runtime/answer-contracts.js.map +1 -0
- package/dist/runtime/artifact-contracts.d.ts +14 -0
- package/dist/runtime/artifact-contracts.js +57 -0
- package/dist/runtime/artifact-contracts.js.map +1 -0
- package/dist/runtime/planning-evidence.d.ts +99 -0
- package/dist/runtime/planning-evidence.js +466 -0
- package/dist/runtime/planning-evidence.js.map +1 -0
- package/dist/runtime/prompt-step.d.ts +1 -9
- package/dist/runtime/prompt-step.js +0 -10
- package/dist/runtime/prompt-step.js.map +1 -1
- package/dist/runtime/run-context.d.ts +5 -2
- package/dist/runtime/run-context.js +8 -1
- package/dist/runtime/run-context.js.map +1 -1
- package/dist/runtime/session-coordinator.d.ts +29 -3
- package/dist/runtime/session-coordinator.js +204 -31
- package/dist/runtime/session-coordinator.js.map +1 -1
- package/dist/runtime/session-title.d.ts +14 -0
- package/dist/runtime/session-title.js +50 -0
- package/dist/runtime/session-title.js.map +1 -0
- package/dist/runtime/tool-defaults-wrapper.js +1 -3
- package/dist/runtime/tool-defaults-wrapper.js.map +1 -1
- package/dist/runtime/validation.js.map +1 -1
- package/dist/runtime/workflow-events.js.map +1 -1
- package/dist/runtime/workflow-runner.d.ts +3 -3
- package/dist/runtime/workflow-runner.js +1 -1
- package/dist/runtime/workflow-runner.js.map +1 -1
- package/dist/sentiment/adapters/finnhub.d.ts +1 -1
- package/dist/sentiment/adapters/finnhub.js +6 -1
- package/dist/sentiment/adapters/finnhub.js.map +1 -1
- package/dist/sentiment/adapters/reddit.d.ts +2 -2
- package/dist/sentiment/adapters/twitter.d.ts +1 -1
- package/dist/sentiment/adapters/web.d.ts +1 -1
- package/dist/sentiment/index.d.ts +9 -11
- package/dist/sentiment/index.js +9 -20
- package/dist/sentiment/index.js.map +1 -1
- package/dist/sentiment/keywords.js +26 -4
- package/dist/sentiment/keywords.js.map +1 -1
- package/dist/sentiment/pipeline.d.ts +2 -2
- package/dist/sentiment/pipeline.js +1 -1
- package/dist/sentiment/pipeline.js.map +1 -1
- package/dist/sentiment/scorer.js +1 -1
- package/dist/sentiment/store.d.ts +1 -1
- package/dist/sentiment/store.js +1 -1
- package/dist/sentiment/store.js.map +1 -1
- package/dist/sentiment/trends.d.ts +1 -1
- package/dist/sentiment/trends.js.map +1 -1
- package/dist/sentiment/types.js.map +1 -1
- package/dist/system-prompt.js +7 -3
- package/dist/system-prompt.js.map +1 -1
- package/dist/tool-kit.d.ts +7 -7
- package/dist/tool-kit.js +4 -4
- package/dist/tool-kit.js.map +1 -1
- package/dist/tools/fundamentals/company-overview.js +12 -7
- package/dist/tools/fundamentals/company-overview.js.map +1 -1
- package/dist/tools/fundamentals/comps.js +19 -10
- package/dist/tools/fundamentals/comps.js.map +1 -1
- package/dist/tools/fundamentals/dcf.js +24 -12
- package/dist/tools/fundamentals/dcf.js.map +1 -1
- package/dist/tools/fundamentals/earnings.js +9 -4
- package/dist/tools/fundamentals/earnings.js.map +1 -1
- package/dist/tools/fundamentals/financials.js +9 -4
- package/dist/tools/fundamentals/financials.js.map +1 -1
- package/dist/tools/fundamentals/sec-filings.d.ts +1 -0
- package/dist/tools/fundamentals/sec-filings.js +36 -4
- package/dist/tools/fundamentals/sec-filings.js.map +1 -1
- package/dist/tools/index.d.ts +23 -18
- package/dist/tools/index.js +53 -38
- package/dist/tools/index.js.map +1 -1
- package/dist/tools/interaction/ask-user.js +15 -3
- package/dist/tools/interaction/ask-user.js.map +1 -1
- package/dist/tools/interaction/twitter-login.js +13 -3
- package/dist/tools/interaction/twitter-login.js.map +1 -1
- package/dist/tools/macro/fear-greed.js +1 -1
- package/dist/tools/macro/fear-greed.js.map +1 -1
- package/dist/tools/macro/fred-data.d.ts +1 -1
- package/dist/tools/macro/fred-data.js +44 -9
- package/dist/tools/macro/fred-data.js.map +1 -1
- package/dist/tools/market/crypto-history.js +21 -3
- package/dist/tools/market/crypto-history.js.map +1 -1
- package/dist/tools/market/crypto-price.js +4 -2
- package/dist/tools/market/crypto-price.js.map +1 -1
- package/dist/tools/market/screen-stocks.d.ts +18 -0
- package/dist/tools/market/screen-stocks.js +252 -0
- package/dist/tools/market/screen-stocks.js.map +1 -0
- package/dist/tools/market/search-ticker.js +161 -9
- package/dist/tools/market/search-ticker.js.map +1 -1
- package/dist/tools/market/stock-history.d.ts +2 -2
- package/dist/tools/market/stock-history.js +27 -8
- package/dist/tools/market/stock-history.js.map +1 -1
- package/dist/tools/market/stock-quote.js +6 -4
- package/dist/tools/market/stock-quote.js.map +1 -1
- package/dist/tools/options/greeks.js +1 -2
- package/dist/tools/options/greeks.js.map +1 -1
- package/dist/tools/options/option-chain.js +27 -9
- package/dist/tools/options/option-chain.js.map +1 -1
- package/dist/tools/portfolio/alerts.d.ts +15 -0
- package/dist/tools/portfolio/alerts.js +357 -0
- package/dist/tools/portfolio/alerts.js.map +1 -0
- package/dist/tools/portfolio/correlation.d.ts +1 -1
- package/dist/tools/portfolio/correlation.js +34 -14
- package/dist/tools/portfolio/correlation.js.map +1 -1
- package/dist/tools/portfolio/daily-report.d.ts +8 -0
- package/dist/tools/portfolio/daily-report.js +83 -0
- package/dist/tools/portfolio/daily-report.js.map +1 -0
- package/dist/tools/portfolio/holdings-overlap.d.ts +8 -0
- package/dist/tools/portfolio/holdings-overlap.js +112 -0
- package/dist/tools/portfolio/holdings-overlap.js.map +1 -0
- package/dist/tools/portfolio/notifications.d.ts +7 -0
- package/dist/tools/portfolio/notifications.js +43 -0
- package/dist/tools/portfolio/notifications.js.map +1 -0
- package/dist/tools/portfolio/predictions.d.ts +12 -6
- package/dist/tools/portfolio/predictions.js +338 -88
- package/dist/tools/portfolio/predictions.js.map +1 -1
- package/dist/tools/portfolio/risk-analysis.d.ts +1 -1
- package/dist/tools/portfolio/risk-analysis.js +46 -7
- package/dist/tools/portfolio/risk-analysis.js.map +1 -1
- package/dist/tools/portfolio/tracker.d.ts +4 -3
- package/dist/tools/portfolio/tracker.js +247 -102
- package/dist/tools/portfolio/tracker.js.map +1 -1
- package/dist/tools/portfolio/watchlist.d.ts +6 -4
- package/dist/tools/portfolio/watchlist.js +209 -101
- package/dist/tools/portfolio/watchlist.js.map +1 -1
- package/dist/tools/sentiment/reddit-sentiment.js +24 -11
- package/dist/tools/sentiment/reddit-sentiment.js.map +1 -1
- package/dist/tools/sentiment/sentiment-summary.js +71 -14
- package/dist/tools/sentiment/sentiment-summary.js.map +1 -1
- package/dist/tools/sentiment/sentiment-trend.d.ts +1 -1
- package/dist/tools/sentiment/sentiment-trend.js +12 -2
- package/dist/tools/sentiment/sentiment-trend.js.map +1 -1
- package/dist/tools/sentiment/twitter-sentiment.js +13 -6
- package/dist/tools/sentiment/twitter-sentiment.js.map +1 -1
- package/dist/tools/sentiment/untrusted-text.d.ts +2 -0
- package/dist/tools/sentiment/untrusted-text.js +17 -0
- package/dist/tools/sentiment/untrusted-text.js.map +1 -0
- package/dist/tools/sentiment/web-search.js +37 -12
- package/dist/tools/sentiment/web-search.js.map +1 -1
- package/dist/tools/sentiment/web-sentiment.js +16 -4
- package/dist/tools/sentiment/web-sentiment.js.map +1 -1
- package/dist/tools/technical/backtest.d.ts +3 -3
- package/dist/tools/technical/backtest.js +65 -44
- package/dist/tools/technical/backtest.js.map +1 -1
- package/dist/tools/technical/indicators.js +24 -8
- package/dist/tools/technical/indicators.js.map +1 -1
- package/dist/types/index.d.ts +3 -3
- package/dist/types/index.js.map +1 -1
- package/dist/types/market.d.ts +1 -0
- package/dist/types/options.d.ts +10 -0
- package/dist/types/portfolio.d.ts +41 -4
- package/dist/workflows/compare-assets.d.ts +0 -3
- package/dist/workflows/compare-assets.js +55 -10
- package/dist/workflows/compare-assets.js.map +1 -1
- package/dist/workflows/index.d.ts +3 -4
- package/dist/workflows/index.js +3 -3
- package/dist/workflows/index.js.map +1 -1
- package/dist/workflows/options-screener.d.ts +0 -3
- package/dist/workflows/options-screener.js +88 -14
- package/dist/workflows/options-screener.js.map +1 -1
- package/dist/workflows/portfolio-builder.d.ts +0 -3
- package/dist/workflows/portfolio-builder.js +7 -11
- package/dist/workflows/portfolio-builder.js.map +1 -1
- package/gui/server/ask-user-bridge.ts +82 -0
- package/gui/server/automation-heartbeat.ts +97 -0
- package/gui/server/background-quotes.ts +97 -1
- package/gui/server/chat-event-adapter.ts +32 -10
- package/gui/server/chat-run-session.ts +16 -0
- package/gui/server/gui-session-manager.ts +5 -0
- package/gui/server/invoke-tool.ts +144 -1
- package/gui/server/live-chat-event-adapter.ts +21 -6
- package/gui/server/market-state-api.ts +315 -0
- package/gui/server/model-setup.ts +149 -2
- package/gui/server/private-api-access.ts +62 -0
- package/gui/server/projector.ts +58 -11
- package/gui/server/prompt-observation.ts +58 -0
- package/gui/server/quote-snapshot-store.ts +50 -0
- package/gui/server/server.ts +236 -376
- package/gui/server/session-actions.ts +186 -1
- package/gui/server/session-entry-wait.ts +81 -0
- package/gui/server/shutdown.ts +47 -0
- package/gui/server/tool-invoke-ack.ts +49 -0
- package/gui/server/tool-metadata.ts +23 -10
- package/gui/server/websocket.ts +13 -3
- package/gui/server/writer-lock.ts +6 -2
- package/gui/server/ws-hub.ts +292 -0
- package/gui/shared/chat-events.ts +16 -1
- package/gui/shared/event-reducer.ts +24 -6
- package/gui/web/dist/assets/CatalogOverlay-eJ2cBk33.js +1 -0
- package/gui/web/dist/assets/index-2KZtKBmu.css +1 -0
- package/gui/web/dist/assets/index-CveNgtDg.js +69 -0
- package/gui/web/dist/index.html +2 -2
- package/package.json +22 -12
- package/src/analysts/contracts.ts +10 -23
- package/src/analysts/orchestrator.ts +8 -43
- package/src/cli.ts +37 -13
- package/src/config.ts +99 -7
- package/src/index.ts +1 -1
- package/src/infra/browser.ts +4 -2
- package/src/infra/cache.ts +41 -30
- package/src/infra/http-client.ts +72 -6
- package/src/infra/index.ts +7 -10
- package/src/infra/native-dependencies.ts +8 -3
- package/src/infra/node-version.ts +3 -1
- package/src/infra/opencandle-paths.ts +3 -14
- package/src/infra/rate-limiter.ts +32 -20
- package/src/market-state/alert-conditions.ts +82 -0
- package/src/market-state/alert-runner.ts +863 -0
- package/src/market-state/daily-report.ts +247 -0
- package/src/market-state/local-automation-service.ts +162 -0
- package/src/market-state/notification-delivery.ts +158 -0
- package/src/market-state/resolve-for-mutation.ts +24 -0
- package/src/market-state/resolve.ts +112 -0
- package/src/market-state/service.ts +2344 -0
- package/src/memory/index.ts +7 -7
- package/src/memory/manager.ts +57 -26
- package/src/memory/retrieval.ts +8 -7
- package/src/memory/sqlite.ts +407 -6
- package/src/memory/storage.ts +8 -17
- package/src/memory/tool-defaults.ts +60 -39
- package/src/memory/types.ts +7 -3
- package/src/monitor.ts +121 -0
- package/src/onboarding/connect.ts +10 -33
- package/src/onboarding/credential-interceptor.ts +3 -15
- package/src/onboarding/degradation-accumulator.ts +1 -3
- package/src/onboarding/providers.ts +9 -40
- package/src/onboarding/state.ts +4 -15
- package/src/onboarding/tool-helpers.ts +2 -9
- package/src/onboarding/tool-tags.ts +6 -6
- package/src/onboarding/validation.ts +14 -20
- package/src/pi/opencandle-extension.ts +795 -120
- package/src/pi/session.ts +7 -5
- package/src/pi/setup.ts +61 -33
- package/src/pi/tool-adapter.ts +5 -2
- package/src/prompts/context-builder.ts +143 -21
- package/src/prompts/disclaimer.ts +1 -1
- package/src/prompts/policy-cards.ts +220 -0
- package/src/prompts/sections.ts +4 -4
- package/src/prompts/symbol-preflight.ts +80 -0
- package/src/prompts/workflow-prompts.ts +231 -28
- package/src/providers/alpha-vantage.ts +82 -40
- package/src/providers/coingecko.ts +2 -5
- package/src/providers/errors.ts +9 -0
- package/src/providers/exa-search.ts +24 -22
- package/src/providers/fear-greed.ts +1 -1
- package/src/providers/finnhub.ts +7 -6
- package/src/providers/fred.ts +3 -3
- package/src/providers/index.ts +14 -6
- package/src/providers/reddit.ts +17 -6
- package/src/providers/sec-edgar.ts +235 -5
- package/src/providers/tradingview.ts +399 -0
- package/src/providers/twitter.ts +6 -8
- package/src/providers/web-search.ts +30 -20
- package/src/providers/with-fallback.ts +8 -7
- package/src/providers/wrap-provider.ts +15 -10
- package/src/providers/yahoo-finance.ts +292 -20
- package/src/routing/classify-intent.ts +186 -4
- package/src/routing/defaults.ts +4 -4
- package/src/routing/entity-extractor.ts +428 -28
- package/src/routing/fund-symbols.ts +58 -0
- package/src/routing/horizon.ts +7 -0
- package/src/routing/index.ts +60 -16
- package/src/routing/legacy-rule-router.ts +13 -0
- package/src/routing/planning.ts +823 -0
- package/src/routing/route-manifest.ts +309 -0
- package/src/routing/router-llm-client.ts +4 -4
- package/src/routing/router-prompt.ts +52 -52
- package/src/routing/router-types.ts +18 -0
- package/src/routing/router.ts +717 -20
- package/src/routing/slot-resolver.ts +75 -14
- package/src/routing/symbol-disambiguator.ts +72 -0
- package/src/routing/turn-context.ts +108 -0
- package/src/routing/types.ts +15 -1
- package/src/runtime/answer-contracts.ts +672 -0
- package/src/runtime/artifact-contracts.ts +77 -0
- package/src/runtime/planning-evidence.ts +682 -0
- package/src/runtime/prompt-step.ts +1 -16
- package/src/runtime/run-context.ts +12 -2
- package/src/runtime/session-coordinator.ts +297 -56
- package/src/runtime/session-title.ts +60 -0
- package/src/runtime/tool-defaults-wrapper.ts +1 -3
- package/src/runtime/validation.ts +1 -4
- package/src/runtime/workflow-events.ts +7 -7
- package/src/runtime/workflow-runner.ts +5 -11
- package/src/sentiment/adapters/finnhub.ts +7 -2
- package/src/sentiment/adapters/reddit.ts +2 -2
- package/src/sentiment/adapters/twitter.ts +1 -1
- package/src/sentiment/adapters/web.ts +1 -1
- package/src/sentiment/index.ts +16 -26
- package/src/sentiment/keywords.ts +26 -4
- package/src/sentiment/pipeline.ts +15 -4
- package/src/sentiment/scorer.ts +1 -1
- package/src/sentiment/store.ts +2 -2
- package/src/sentiment/trends.ts +9 -3
- package/src/sentiment/types.ts +5 -4
- package/src/system-prompt.ts +7 -3
- package/src/tool-kit.ts +10 -9
- package/src/tools/fundamentals/company-overview.ts +20 -10
- package/src/tools/fundamentals/comps.ts +69 -56
- package/src/tools/fundamentals/dcf.ts +146 -96
- package/src/tools/fundamentals/earnings.ts +17 -7
- package/src/tools/fundamentals/financials.ts +17 -8
- package/src/tools/fundamentals/sec-filings.ts +52 -8
- package/src/tools/index.ts +53 -38
- package/src/tools/interaction/ask-user.ts +22 -10
- package/src/tools/interaction/twitter-login.ts +17 -5
- package/src/tools/macro/fear-greed.ts +2 -2
- package/src/tools/macro/fred-data.ts +80 -42
- package/src/tools/market/crypto-history.ts +25 -4
- package/src/tools/market/crypto-price.ts +7 -7
- package/src/tools/market/screen-stocks.ts +279 -0
- package/src/tools/market/search-ticker.ts +219 -18
- package/src/tools/market/stock-history.ts +38 -13
- package/src/tools/market/stock-quote.ts +11 -8
- package/src/tools/options/greeks.ts +5 -6
- package/src/tools/options/option-chain.ts +47 -18
- package/src/tools/portfolio/alerts.ts +457 -0
- package/src/tools/portfolio/correlation.ts +48 -21
- package/src/tools/portfolio/daily-report.ts +101 -0
- package/src/tools/portfolio/holdings-overlap.ts +139 -0
- package/src/tools/portfolio/notifications.ts +45 -0
- package/src/tools/portfolio/predictions.ts +407 -107
- package/src/tools/portfolio/risk-analysis.ts +47 -8
- package/src/tools/portfolio/tracker.ts +271 -110
- package/src/tools/portfolio/watchlist.ts +251 -116
- package/src/tools/sentiment/reddit-sentiment.ts +51 -25
- package/src/tools/sentiment/sentiment-summary.ts +116 -35
- package/src/tools/sentiment/sentiment-trend.ts +24 -7
- package/src/tools/sentiment/twitter-sentiment.ts +23 -16
- package/src/tools/sentiment/untrusted-text.ts +21 -0
- package/src/tools/sentiment/web-search.ts +52 -16
- package/src/tools/sentiment/web-sentiment.ts +27 -11
- package/src/tools/technical/backtest.ts +78 -47
- package/src/tools/technical/indicators.ts +40 -17
- package/src/types/index.ts +8 -3
- package/src/types/market.ts +1 -0
- package/src/types/options.ts +17 -0
- package/src/types/portfolio.ts +46 -4
- package/src/types/sentiment.ts +2 -2
- package/src/workflows/compare-assets.ts +67 -19
- package/src/workflows/index.ts +3 -4
- package/src/workflows/options-screener.ts +98 -22
- package/src/workflows/portfolio-builder.ts +40 -29
- package/dist/runtime/index.d.ts +0 -16
- package/dist/runtime/index.js +0 -10
- package/dist/runtime/index.js.map +0 -1
- package/dist/runtime/provider-ids.d.ts +0 -14
- package/dist/runtime/provider-ids.js +0 -14
- package/dist/runtime/provider-ids.js.map +0 -1
- package/dist/workflows/types.d.ts +0 -4
- package/dist/workflows/types.js +0 -2
- package/dist/workflows/types.js.map +0 -1
- package/gui/web/dist/assets/CatalogOverlay-D1ImSJTe.js +0 -1
- package/gui/web/dist/assets/index-DBrWq43L.css +0 -1
- package/gui/web/dist/assets/index-RflHaj0y.js +0 -67
- package/src/runtime/index.ts +0 -55
- package/src/runtime/provider-ids.ts +0 -15
- package/src/workflows/types.ts +0 -4
|
@@ -5,6 +5,8 @@ import type { TSchema } from "@sinclair/typebox";
|
|
|
5
5
|
import { Value } from "@sinclair/typebox/value";
|
|
6
6
|
import { getDefaults } from "../../src/memory/tool-defaults.js";
|
|
7
7
|
import { wrapWithDefaults } from "../../src/runtime/tool-defaults-wrapper.js";
|
|
8
|
+
import { getAllTools } from "../../src/tools/index.js";
|
|
9
|
+
import { buildToolInvokeAckMessage } from "./tool-invoke-ack.js";
|
|
8
10
|
|
|
9
11
|
export interface InvokeToolResult {
|
|
10
12
|
toolCallId: string;
|
|
@@ -12,6 +14,81 @@ export interface InvokeToolResult {
|
|
|
12
14
|
isError: boolean;
|
|
13
15
|
}
|
|
14
16
|
|
|
17
|
+
interface ToolInvokeClient {
|
|
18
|
+
send(message: unknown): void;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface ToolInvokeController {
|
|
22
|
+
handleToolInvoke(toolName: string, args: Record<string, unknown>): Promise<InvokeToolResult>;
|
|
23
|
+
handleToolInvokeMessage(client: ToolInvokeClient, data: Record<string, unknown>): Promise<void>;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface ToolInvokeControllerOptions {
|
|
27
|
+
role: string;
|
|
28
|
+
getSessionManager: () => SessionManager;
|
|
29
|
+
broadcastState: () => void;
|
|
30
|
+
onMarketStateChanged?: () => void;
|
|
31
|
+
getTools?: typeof getAllTools;
|
|
32
|
+
invokeTool?: typeof invokeToolFromUi;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function createToolInvokeController({
|
|
36
|
+
role,
|
|
37
|
+
getSessionManager,
|
|
38
|
+
broadcastState,
|
|
39
|
+
onMarketStateChanged,
|
|
40
|
+
getTools = getAllTools,
|
|
41
|
+
invokeTool = invokeToolFromUi,
|
|
42
|
+
}: ToolInvokeControllerOptions): ToolInvokeController {
|
|
43
|
+
async function handleToolInvoke(
|
|
44
|
+
toolName: string,
|
|
45
|
+
args: Record<string, unknown>,
|
|
46
|
+
): Promise<InvokeToolResult> {
|
|
47
|
+
if (role !== "writer") throw new Error("Read-only follower mode");
|
|
48
|
+
const tool = getTools().find((candidate) => candidate.name === toolName);
|
|
49
|
+
if (!tool) throw new Error(`Unknown tool: ${toolName}`);
|
|
50
|
+
const result = await invokeTool(getSessionManager(), tool, args, "ui");
|
|
51
|
+
if (!result.isError && marketStateToolMapping(toolName) != null) {
|
|
52
|
+
onMarketStateChanged?.();
|
|
53
|
+
}
|
|
54
|
+
broadcastState();
|
|
55
|
+
return result;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
async function handleToolInvokeMessage(
|
|
59
|
+
client: ToolInvokeClient,
|
|
60
|
+
data: Record<string, unknown>,
|
|
61
|
+
): Promise<void> {
|
|
62
|
+
const requestId = typeof data.requestId === "string" ? data.requestId : "";
|
|
63
|
+
const toolName = String(data.toolName ?? "");
|
|
64
|
+
try {
|
|
65
|
+
const result = await handleToolInvoke(toolName, requestArgs(data.args));
|
|
66
|
+
if (requestId) {
|
|
67
|
+
client.send(buildToolInvokeAckMessage(requestId, toolName, result));
|
|
68
|
+
}
|
|
69
|
+
} catch (error) {
|
|
70
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
71
|
+
if (requestId) {
|
|
72
|
+
client.send({
|
|
73
|
+
type: "tool.invoke.result",
|
|
74
|
+
requestId,
|
|
75
|
+
ok: false,
|
|
76
|
+
toolName,
|
|
77
|
+
error: { message },
|
|
78
|
+
});
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
throw error;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return { handleToolInvoke, handleToolInvokeMessage };
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function requestArgs(value: unknown): Record<string, unknown> {
|
|
89
|
+
return typeof value === "object" && value !== null ? (value as Record<string, unknown>) : {};
|
|
90
|
+
}
|
|
91
|
+
|
|
15
92
|
export async function invokeToolFromUi(
|
|
16
93
|
sessionManager: SessionManager,
|
|
17
94
|
tool: AgentTool<TSchema, unknown>,
|
|
@@ -68,7 +145,12 @@ export async function invokeToolFromUi(
|
|
|
68
145
|
toolCallId,
|
|
69
146
|
toolName: tool.name,
|
|
70
147
|
content: result.content,
|
|
71
|
-
details: {
|
|
148
|
+
details: {
|
|
149
|
+
source,
|
|
150
|
+
args,
|
|
151
|
+
value: result.details,
|
|
152
|
+
...stateChangeDetails(tool.name, args, result.details, source),
|
|
153
|
+
},
|
|
72
154
|
isError,
|
|
73
155
|
timestamp: Date.now(),
|
|
74
156
|
});
|
|
@@ -77,6 +159,67 @@ export async function invokeToolFromUi(
|
|
|
77
159
|
return { toolCallId, result, isError };
|
|
78
160
|
}
|
|
79
161
|
|
|
162
|
+
function stateChangeDetails(
|
|
163
|
+
toolName: string,
|
|
164
|
+
args: Record<string, unknown>,
|
|
165
|
+
value: unknown,
|
|
166
|
+
source: "ui" | "background",
|
|
167
|
+
): { stateChange: Record<string, unknown> } | Record<string, never> {
|
|
168
|
+
const mapping = marketStateToolMapping(toolName);
|
|
169
|
+
if (mapping == null) return {};
|
|
170
|
+
|
|
171
|
+
const valueRecord = asRecord(value);
|
|
172
|
+
return {
|
|
173
|
+
stateChange: {
|
|
174
|
+
source,
|
|
175
|
+
domain: mapping.domain,
|
|
176
|
+
action: typeof args.action === "string" ? args.action : "invoke",
|
|
177
|
+
targetType: mapping.targetType,
|
|
178
|
+
targetId: numericField(valueRecord, "id"),
|
|
179
|
+
targetIds: numericArrayField(valueRecord, "removedLotIds"),
|
|
180
|
+
instrumentId: numericField(valueRecord, "instrumentId"),
|
|
181
|
+
instrumentIds: numericArrayField(valueRecord, "instrumentIds"),
|
|
182
|
+
toolName,
|
|
183
|
+
},
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
function marketStateToolMapping(toolName: string): { domain: string; targetType: string } | null {
|
|
188
|
+
switch (toolName) {
|
|
189
|
+
case "manage_watchlist":
|
|
190
|
+
return { domain: "watchlist", targetType: "watchlist_item" };
|
|
191
|
+
case "track_portfolio":
|
|
192
|
+
return { domain: "portfolio", targetType: "portfolio_lot" };
|
|
193
|
+
case "track_prediction":
|
|
194
|
+
return { domain: "predictions", targetType: "prediction" };
|
|
195
|
+
case "manage_alerts":
|
|
196
|
+
return { domain: "alerts", targetType: "alert_rule" };
|
|
197
|
+
case "daily_watchlist_report":
|
|
198
|
+
return { domain: "reports", targetType: "report_run" };
|
|
199
|
+
default:
|
|
200
|
+
return null;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
function asRecord(value: unknown): Record<string, unknown> | null {
|
|
205
|
+
return typeof value === "object" && value !== null ? (value as Record<string, unknown>) : null;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function numericField(record: Record<string, unknown> | null, key: string): number | undefined {
|
|
209
|
+
const value = record?.[key];
|
|
210
|
+
return typeof value === "number" ? value : undefined;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
function numericArrayField(
|
|
214
|
+
record: Record<string, unknown> | null,
|
|
215
|
+
key: string,
|
|
216
|
+
): number[] | undefined {
|
|
217
|
+
const value = record?.[key];
|
|
218
|
+
if (!Array.isArray(value)) return undefined;
|
|
219
|
+
const numbers = value.filter((item): item is number => typeof item === "number");
|
|
220
|
+
return numbers.length > 0 ? numbers : undefined;
|
|
221
|
+
}
|
|
222
|
+
|
|
80
223
|
function emptyUsage(): Usage {
|
|
81
224
|
return {
|
|
82
225
|
input: 0,
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { AgentSessionEvent } from "@earendil-works/pi-coding-agent";
|
|
2
1
|
import type { AssistantMessage, Message } from "@earendil-works/pi-ai";
|
|
2
|
+
import type { AgentSessionEvent } from "@earendil-works/pi-coding-agent";
|
|
3
3
|
import type { ChatEvent, MessageContent, ToolOutput } from "../shared/chat-events.js";
|
|
4
4
|
|
|
5
5
|
export interface LiveChatEventAdapterOptions {
|
|
@@ -7,6 +7,11 @@ export interface LiveChatEventAdapterOptions {
|
|
|
7
7
|
sessionId: string;
|
|
8
8
|
startSeq: number;
|
|
9
9
|
emit: (event: ChatEvent) => void;
|
|
10
|
+
/**
|
|
11
|
+
* The user's words as typed. Workflow transforms can replace the first user
|
|
12
|
+
* message with an expanded prompt; the live view renders this instead.
|
|
13
|
+
*/
|
|
14
|
+
originalPrompt?: string;
|
|
10
15
|
}
|
|
11
16
|
|
|
12
17
|
export interface LiveChatEventAdapter {
|
|
@@ -14,7 +19,9 @@ export interface LiveChatEventAdapter {
|
|
|
14
19
|
nextSeq(): number;
|
|
15
20
|
}
|
|
16
21
|
|
|
17
|
-
export function createLiveChatEventAdapter(
|
|
22
|
+
export function createLiveChatEventAdapter(
|
|
23
|
+
options: LiveChatEventAdapterOptions,
|
|
24
|
+
): LiveChatEventAdapter {
|
|
18
25
|
let seq = options.startSeq;
|
|
19
26
|
let userCount = 0;
|
|
20
27
|
let assistantCount = 0;
|
|
@@ -55,11 +62,15 @@ export function createLiveChatEventAdapter(options: LiveChatEventAdapterOptions)
|
|
|
55
62
|
const message = event.message as Message;
|
|
56
63
|
if (message.role === "user") {
|
|
57
64
|
const messageId = `${options.runId}-user-${++userCount}`;
|
|
65
|
+
const text =
|
|
66
|
+
userCount === 1 && options.originalPrompt
|
|
67
|
+
? options.originalPrompt
|
|
68
|
+
: messageText(message.content);
|
|
58
69
|
emit({ type: "message.created", messageId, role: "user" });
|
|
59
70
|
emit({
|
|
60
71
|
type: "message.completed",
|
|
61
72
|
messageId,
|
|
62
|
-
content: [{ type: "text", text
|
|
73
|
+
content: [{ type: "text", text }],
|
|
63
74
|
});
|
|
64
75
|
return;
|
|
65
76
|
}
|
|
@@ -161,14 +172,18 @@ function messageText(content: unknown): string {
|
|
|
161
172
|
if (typeof content === "string") return content;
|
|
162
173
|
if (!Array.isArray(content)) return "";
|
|
163
174
|
return content
|
|
164
|
-
.map((part) =>
|
|
175
|
+
.map((part) =>
|
|
176
|
+
typeof part === "object" && part !== null && "text" in part && typeof part.text === "string"
|
|
177
|
+
? part.text
|
|
178
|
+
: "",
|
|
179
|
+
)
|
|
165
180
|
.join("");
|
|
166
181
|
}
|
|
167
182
|
|
|
168
183
|
function toolOutput(result: unknown, isError: boolean): ToolOutput {
|
|
169
184
|
const record = asRecord(result);
|
|
170
185
|
return {
|
|
171
|
-
content: Array.isArray(record.content) ? record.content as ToolOutput["content"] : [],
|
|
186
|
+
content: Array.isArray(record.content) ? (record.content as ToolOutput["content"]) : [],
|
|
172
187
|
details: record.details,
|
|
173
188
|
isError,
|
|
174
189
|
};
|
|
@@ -176,6 +191,6 @@ function toolOutput(result: unknown, isError: boolean): ToolOutput {
|
|
|
176
191
|
|
|
177
192
|
function asRecord(value: unknown): Record<string, unknown> {
|
|
178
193
|
return typeof value === "object" && value !== null && !Array.isArray(value)
|
|
179
|
-
? value as Record<string, unknown>
|
|
194
|
+
? (value as Record<string, unknown>)
|
|
180
195
|
: {};
|
|
181
196
|
}
|
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
import type Database from "better-sqlite3";
|
|
2
|
+
import { isZeroFilledQuote, searchYahooInstruments } from "../../src/market-state/resolve.js";
|
|
3
|
+
import { MarketStateService } from "../../src/market-state/service.js";
|
|
4
|
+
import { initDefaultDatabase } from "../../src/memory/sqlite.js";
|
|
5
|
+
import { wrapProvider } from "../../src/providers/wrap-provider.js";
|
|
6
|
+
import { getQuote } from "../../src/providers/yahoo-finance.js";
|
|
7
|
+
|
|
8
|
+
export interface MarketStateSnapshot {
|
|
9
|
+
instruments: Array<NonNullable<ReturnType<MarketStateService["getInstrument"]>>>;
|
|
10
|
+
watchlist: ReturnType<MarketStateService["listWatchlistItems"]>;
|
|
11
|
+
portfolio: ReturnType<MarketStateService["listPortfolioLots"]>;
|
|
12
|
+
predictions: ReturnType<MarketStateService["listPredictions"]>;
|
|
13
|
+
alerts: ReturnType<MarketStateService["listAlertRules"]>;
|
|
14
|
+
alertEvents: ReturnType<MarketStateService["listAlertEvents"]>;
|
|
15
|
+
alertCheckRuns: ReturnType<MarketStateService["listAlertCheckRuns"]>;
|
|
16
|
+
reportTemplates: ReturnType<MarketStateService["listReportTemplates"]>;
|
|
17
|
+
reportRuns: ReturnType<MarketStateService["listReportRuns"]>;
|
|
18
|
+
runnerLease: ReturnType<MarketStateService["getAutomationRunnerLease"]>;
|
|
19
|
+
notifications: ReturnType<MarketStateService["listNotificationEvents"]>;
|
|
20
|
+
notificationDeliveryAttempts: ReturnType<MarketStateService["listNotificationDeliveryAttempts"]>;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface MarketStateQuoteSnapshot {
|
|
24
|
+
generatedAt: string;
|
|
25
|
+
watchlistQuotes: Array<{
|
|
26
|
+
itemId: number;
|
|
27
|
+
instrumentId: number;
|
|
28
|
+
symbol: string;
|
|
29
|
+
status: "ok" | "unavailable";
|
|
30
|
+
price?: number;
|
|
31
|
+
changePercent?: number;
|
|
32
|
+
fetchedAt?: string;
|
|
33
|
+
stale?: boolean;
|
|
34
|
+
reason?: string;
|
|
35
|
+
}>;
|
|
36
|
+
portfolioQuotes: Array<{
|
|
37
|
+
lotId: number;
|
|
38
|
+
instrumentId: number;
|
|
39
|
+
symbol: string;
|
|
40
|
+
status: "ok" | "unavailable";
|
|
41
|
+
currentPrice?: number;
|
|
42
|
+
changePercent?: number;
|
|
43
|
+
marketValue?: number;
|
|
44
|
+
totalCost: number;
|
|
45
|
+
pnl?: number;
|
|
46
|
+
pnlPercent?: number;
|
|
47
|
+
allocationPercent?: number;
|
|
48
|
+
currency: string;
|
|
49
|
+
includedInTotals: boolean;
|
|
50
|
+
fetchedAt?: string;
|
|
51
|
+
stale?: boolean;
|
|
52
|
+
reason?: string;
|
|
53
|
+
}>;
|
|
54
|
+
predictionQuotes: Array<{
|
|
55
|
+
predictionId: number;
|
|
56
|
+
instrumentId: number;
|
|
57
|
+
symbol: string;
|
|
58
|
+
status: "ok" | "unavailable";
|
|
59
|
+
currentPrice?: number;
|
|
60
|
+
changePercent?: number;
|
|
61
|
+
fetchedAt?: string;
|
|
62
|
+
stale?: boolean;
|
|
63
|
+
reason?: string;
|
|
64
|
+
}>;
|
|
65
|
+
portfolioSummary: {
|
|
66
|
+
baseCurrency: string;
|
|
67
|
+
totalValue: number;
|
|
68
|
+
totalCost: number;
|
|
69
|
+
totalPnl: number;
|
|
70
|
+
totalPnlPercent: number;
|
|
71
|
+
excludedFromTotals: Array<{ symbol: string; currency: string; reason: string }>;
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export function buildMarketStateSnapshot(db?: Database.Database): MarketStateSnapshot {
|
|
76
|
+
const ownedDb = db ?? initDefaultDatabase();
|
|
77
|
+
const service = new MarketStateService(ownedDb);
|
|
78
|
+
try {
|
|
79
|
+
const alerts = service.listAlertRules();
|
|
80
|
+
const instrumentIds = [
|
|
81
|
+
...new Set(alerts.map((rule) => rule.instrumentId).filter((id) => id != null)),
|
|
82
|
+
];
|
|
83
|
+
return {
|
|
84
|
+
instruments: instrumentIds
|
|
85
|
+
.map((id) => service.getInstrument(id))
|
|
86
|
+
.filter((instrument) => instrument != null),
|
|
87
|
+
watchlist: service.listWatchlistItems(),
|
|
88
|
+
portfolio: service.listPortfolioLots(),
|
|
89
|
+
predictions: service.listPredictions(),
|
|
90
|
+
alerts,
|
|
91
|
+
alertEvents: service.listAlertEvents(),
|
|
92
|
+
alertCheckRuns: service.listAlertCheckRuns(),
|
|
93
|
+
reportTemplates: service.listReportTemplates(),
|
|
94
|
+
reportRuns: service.listReportRuns(),
|
|
95
|
+
runnerLease: service.getAutomationRunnerLease(),
|
|
96
|
+
notifications: service.listNotificationEvents(),
|
|
97
|
+
notificationDeliveryAttempts: service.listNotificationDeliveryAttempts(),
|
|
98
|
+
};
|
|
99
|
+
} finally {
|
|
100
|
+
if (!db) ownedDb.close();
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export async function buildMarketStateQuoteSnapshot(
|
|
105
|
+
db?: Database.Database,
|
|
106
|
+
): Promise<MarketStateQuoteSnapshot> {
|
|
107
|
+
const ownedDb = db ?? initDefaultDatabase();
|
|
108
|
+
const service = new MarketStateService(ownedDb);
|
|
109
|
+
try {
|
|
110
|
+
const watchlist = service.listWatchlistItems();
|
|
111
|
+
const portfolio = service.listPortfolioLots();
|
|
112
|
+
const predictions = service.listPredictions();
|
|
113
|
+
const symbols = [
|
|
114
|
+
...new Set([
|
|
115
|
+
...watchlist.map((item) => item.symbol),
|
|
116
|
+
...portfolio.map((lot) => lot.symbol),
|
|
117
|
+
...predictions.map((prediction) => prediction.symbol),
|
|
118
|
+
]),
|
|
119
|
+
];
|
|
120
|
+
const quoteMap = new Map<string, Awaited<ReturnType<typeof fetchQuoteSnapshot>>>();
|
|
121
|
+
for (const symbol of symbols) {
|
|
122
|
+
quoteMap.set(symbol, await fetchQuoteSnapshot(symbol));
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const generatedAt = new Date().toISOString();
|
|
126
|
+
const watchlistQuotes = watchlist.map((item) => {
|
|
127
|
+
const quote = quoteMap.get(item.symbol);
|
|
128
|
+
if (quote == null || quote.status === "unavailable") {
|
|
129
|
+
return {
|
|
130
|
+
itemId: item.id,
|
|
131
|
+
instrumentId: item.instrumentId,
|
|
132
|
+
symbol: item.symbol,
|
|
133
|
+
status: "unavailable" as const,
|
|
134
|
+
reason: quote?.reason ?? "quote unavailable",
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
return {
|
|
138
|
+
itemId: item.id,
|
|
139
|
+
instrumentId: item.instrumentId,
|
|
140
|
+
symbol: item.symbol,
|
|
141
|
+
status: "ok" as const,
|
|
142
|
+
price: quote.price,
|
|
143
|
+
changePercent: quote.changePercent,
|
|
144
|
+
fetchedAt: quote.fetchedAt,
|
|
145
|
+
stale: quote.stale,
|
|
146
|
+
};
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
const baseCurrency = service.getDefaultPortfolio().baseCurrency ?? "USD";
|
|
150
|
+
const portfolioQuotes = portfolio.map((lot) => {
|
|
151
|
+
const quote = quoteMap.get(lot.symbol);
|
|
152
|
+
const lotCurrency = lot.currency || baseCurrency;
|
|
153
|
+
const quoteCurrency = lot.instrumentCurrency || lotCurrency;
|
|
154
|
+
const includedInTotals = lotCurrency === baseCurrency && quoteCurrency === baseCurrency;
|
|
155
|
+
const totalCost = lot.avgCost * lot.quantity;
|
|
156
|
+
if (quote == null || quote.status === "unavailable") {
|
|
157
|
+
return {
|
|
158
|
+
lotId: lot.id,
|
|
159
|
+
instrumentId: lot.instrumentId,
|
|
160
|
+
symbol: lot.symbol,
|
|
161
|
+
status: "unavailable" as const,
|
|
162
|
+
totalCost,
|
|
163
|
+
currency: lotCurrency,
|
|
164
|
+
includedInTotals: false,
|
|
165
|
+
reason: quote?.reason ?? "quote unavailable",
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
const resolvedQuoteCurrency = quote.currency ?? quoteCurrency;
|
|
169
|
+
if (resolvedQuoteCurrency !== lotCurrency) {
|
|
170
|
+
return {
|
|
171
|
+
lotId: lot.id,
|
|
172
|
+
instrumentId: lot.instrumentId,
|
|
173
|
+
symbol: lot.symbol,
|
|
174
|
+
status: "unavailable" as const,
|
|
175
|
+
currentPrice: null,
|
|
176
|
+
marketValue: null,
|
|
177
|
+
totalCost,
|
|
178
|
+
pnl: null,
|
|
179
|
+
pnlPercent: null,
|
|
180
|
+
currency: lotCurrency,
|
|
181
|
+
includedInTotals: false,
|
|
182
|
+
reason: `No FX conversion from ${resolvedQuoteCurrency} to ${lotCurrency}`,
|
|
183
|
+
fetchedAt: quote.fetchedAt,
|
|
184
|
+
stale: quote.stale,
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
const marketValue = quote.price * lot.quantity;
|
|
188
|
+
return {
|
|
189
|
+
lotId: lot.id,
|
|
190
|
+
instrumentId: lot.instrumentId,
|
|
191
|
+
symbol: lot.symbol,
|
|
192
|
+
status: "ok" as const,
|
|
193
|
+
currentPrice: quote.price,
|
|
194
|
+
changePercent: quote.changePercent,
|
|
195
|
+
marketValue,
|
|
196
|
+
totalCost,
|
|
197
|
+
pnl: marketValue - totalCost,
|
|
198
|
+
pnlPercent: totalCost > 0 ? ((marketValue - totalCost) / totalCost) * 100 : 0,
|
|
199
|
+
currency: lotCurrency,
|
|
200
|
+
includedInTotals,
|
|
201
|
+
fetchedAt: quote.fetchedAt,
|
|
202
|
+
stale: quote.stale,
|
|
203
|
+
reason: includedInTotals
|
|
204
|
+
? undefined
|
|
205
|
+
: `No FX conversion from ${lotCurrency === baseCurrency ? quoteCurrency : lotCurrency} to ${baseCurrency}`,
|
|
206
|
+
};
|
|
207
|
+
});
|
|
208
|
+
const included = portfolioQuotes.filter(
|
|
209
|
+
(quote) => quote.status === "ok" && quote.includedInTotals,
|
|
210
|
+
);
|
|
211
|
+
const totalValue = included.reduce((sum, quote) => sum + (quote.marketValue ?? 0), 0);
|
|
212
|
+
const totalCost = included.reduce((sum, quote) => sum + quote.totalCost, 0);
|
|
213
|
+
const portfolioQuotesWithAllocation = portfolioQuotes.map((quote) => {
|
|
214
|
+
if (quote.status !== "ok" || !quote.includedInTotals || totalValue <= 0) return quote;
|
|
215
|
+
return {
|
|
216
|
+
...quote,
|
|
217
|
+
allocationPercent: ((quote.marketValue ?? 0) / totalValue) * 100,
|
|
218
|
+
};
|
|
219
|
+
});
|
|
220
|
+
const excludedFromTotals = portfolioQuotesWithAllocation
|
|
221
|
+
.filter((quote) => quote.status !== "ok" || !quote.includedInTotals)
|
|
222
|
+
.map((quote) => ({
|
|
223
|
+
symbol: quote.symbol,
|
|
224
|
+
currency: quote.currency,
|
|
225
|
+
reason: quote.reason ?? "quote unavailable",
|
|
226
|
+
}));
|
|
227
|
+
const predictionQuotes = predictions.map((prediction) => {
|
|
228
|
+
const quote = quoteMap.get(prediction.symbol);
|
|
229
|
+
if (quote == null || quote.status === "unavailable") {
|
|
230
|
+
return {
|
|
231
|
+
predictionId: prediction.id,
|
|
232
|
+
instrumentId: prediction.instrumentId,
|
|
233
|
+
symbol: prediction.symbol,
|
|
234
|
+
status: "unavailable" as const,
|
|
235
|
+
reason: quote?.reason ?? "quote unavailable",
|
|
236
|
+
};
|
|
237
|
+
}
|
|
238
|
+
return {
|
|
239
|
+
predictionId: prediction.id,
|
|
240
|
+
instrumentId: prediction.instrumentId,
|
|
241
|
+
symbol: prediction.symbol,
|
|
242
|
+
status: "ok" as const,
|
|
243
|
+
currentPrice: quote.price,
|
|
244
|
+
changePercent: quote.changePercent,
|
|
245
|
+
fetchedAt: quote.fetchedAt,
|
|
246
|
+
stale: quote.stale,
|
|
247
|
+
};
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
return {
|
|
251
|
+
generatedAt,
|
|
252
|
+
watchlistQuotes,
|
|
253
|
+
portfolioQuotes: portfolioQuotesWithAllocation,
|
|
254
|
+
predictionQuotes,
|
|
255
|
+
portfolioSummary: {
|
|
256
|
+
baseCurrency,
|
|
257
|
+
totalValue,
|
|
258
|
+
totalCost,
|
|
259
|
+
totalPnl: totalValue - totalCost,
|
|
260
|
+
totalPnlPercent: totalCost > 0 ? ((totalValue - totalCost) / totalCost) * 100 : 0,
|
|
261
|
+
excludedFromTotals,
|
|
262
|
+
},
|
|
263
|
+
};
|
|
264
|
+
} finally {
|
|
265
|
+
if (!db) ownedDb.close();
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
export async function searchInstrumentCandidates(query: string): Promise<{
|
|
270
|
+
query: string;
|
|
271
|
+
candidates: Awaited<ReturnType<typeof searchYahooInstruments>>;
|
|
272
|
+
error?: string;
|
|
273
|
+
}> {
|
|
274
|
+
const trimmed = query.trim();
|
|
275
|
+
if (!trimmed) return { query: "", candidates: [] };
|
|
276
|
+
try {
|
|
277
|
+
return {
|
|
278
|
+
query: trimmed,
|
|
279
|
+
candidates: await searchYahooInstruments(trimmed),
|
|
280
|
+
};
|
|
281
|
+
} catch (err) {
|
|
282
|
+
return {
|
|
283
|
+
query: trimmed,
|
|
284
|
+
candidates: [],
|
|
285
|
+
error: err instanceof Error ? err.message : String(err),
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
async function fetchQuoteSnapshot(symbol: string): Promise<
|
|
291
|
+
| {
|
|
292
|
+
status: "ok";
|
|
293
|
+
price: number;
|
|
294
|
+
changePercent: number;
|
|
295
|
+
fetchedAt: string;
|
|
296
|
+
stale?: boolean;
|
|
297
|
+
currency: string | null;
|
|
298
|
+
}
|
|
299
|
+
| { status: "unavailable"; reason: string }
|
|
300
|
+
> {
|
|
301
|
+
const result = await wrapProvider("yahoo", () => getQuote(symbol));
|
|
302
|
+
if (result.status === "unavailable") return { status: "unavailable", reason: result.reason };
|
|
303
|
+
if (result.stale) return { status: "unavailable", reason: "provider returned stale market data" };
|
|
304
|
+
if (isZeroFilledQuote(result.data)) {
|
|
305
|
+
return { status: "unavailable", reason: "Yahoo returned no valid market data." };
|
|
306
|
+
}
|
|
307
|
+
return {
|
|
308
|
+
status: "ok",
|
|
309
|
+
price: result.data.price,
|
|
310
|
+
changePercent: result.data.changePercent,
|
|
311
|
+
fetchedAt: result.timestamp,
|
|
312
|
+
stale: result.stale,
|
|
313
|
+
currency: result.data.currency ?? null,
|
|
314
|
+
};
|
|
315
|
+
}
|