opencandle 0.5.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.
Files changed (527) hide show
  1. package/README.md +164 -187
  2. package/dist/analysts/contracts.d.ts +1 -3
  3. package/dist/analysts/contracts.js +1 -11
  4. package/dist/analysts/contracts.js.map +1 -1
  5. package/dist/analysts/orchestrator.d.ts +1 -3
  6. package/dist/analysts/orchestrator.js +1 -26
  7. package/dist/analysts/orchestrator.js.map +1 -1
  8. package/dist/cli.js +30 -7
  9. package/dist/cli.js.map +1 -1
  10. package/dist/config.d.ts +3 -3
  11. package/dist/config.js +12 -5
  12. package/dist/config.js.map +1 -1
  13. package/dist/index.d.ts +1 -1
  14. package/dist/index.js +1 -1
  15. package/dist/index.js.map +1 -1
  16. package/dist/infra/browser.js +3 -1
  17. package/dist/infra/browser.js.map +1 -1
  18. package/dist/infra/cache.d.ts +8 -11
  19. package/dist/infra/cache.js +17 -15
  20. package/dist/infra/cache.js.map +1 -1
  21. package/dist/infra/http-client.d.ts +4 -1
  22. package/dist/infra/http-client.js +59 -6
  23. package/dist/infra/http-client.js.map +1 -1
  24. package/dist/infra/index.d.ts +3 -3
  25. package/dist/infra/index.js +3 -3
  26. package/dist/infra/index.js.map +1 -1
  27. package/dist/infra/native-dependencies.js +2 -2
  28. package/dist/infra/native-dependencies.js.map +1 -1
  29. package/dist/infra/node-version.js.map +1 -1
  30. package/dist/infra/opencandle-paths.d.ts +0 -3
  31. package/dist/infra/opencandle-paths.js +4 -11
  32. package/dist/infra/opencandle-paths.js.map +1 -1
  33. package/dist/infra/rate-limiter.js +12 -9
  34. package/dist/infra/rate-limiter.js.map +1 -1
  35. package/dist/market-state/alert-conditions.d.ts +34 -0
  36. package/dist/market-state/alert-conditions.js +23 -0
  37. package/dist/market-state/alert-conditions.js.map +1 -0
  38. package/dist/market-state/alert-runner.d.ts +55 -0
  39. package/dist/market-state/alert-runner.js +634 -0
  40. package/dist/market-state/alert-runner.js.map +1 -0
  41. package/dist/market-state/daily-report.d.ts +26 -0
  42. package/dist/market-state/daily-report.js +179 -0
  43. package/dist/market-state/daily-report.js.map +1 -0
  44. package/dist/market-state/local-automation-service.d.ts +25 -0
  45. package/dist/market-state/local-automation-service.js +119 -0
  46. package/dist/market-state/local-automation-service.js.map +1 -0
  47. package/dist/market-state/notification-delivery.d.ts +14 -0
  48. package/dist/market-state/notification-delivery.js +139 -0
  49. package/dist/market-state/notification-delivery.js.map +1 -0
  50. package/dist/market-state/resolve-for-mutation.d.ts +10 -0
  51. package/dist/market-state/resolve-for-mutation.js +15 -0
  52. package/dist/market-state/resolve-for-mutation.js.map +1 -0
  53. package/dist/market-state/resolve.d.ts +14 -0
  54. package/dist/market-state/resolve.js +89 -0
  55. package/dist/market-state/resolve.js.map +1 -0
  56. package/dist/market-state/service.d.ts +527 -0
  57. package/dist/market-state/service.js +1099 -0
  58. package/dist/market-state/service.js.map +1 -0
  59. package/dist/memory/index.d.ts +7 -7
  60. package/dist/memory/index.js +6 -6
  61. package/dist/memory/index.js.map +1 -1
  62. package/dist/memory/manager.js +11 -11
  63. package/dist/memory/manager.js.map +1 -1
  64. package/dist/memory/retrieval.js +7 -4
  65. package/dist/memory/retrieval.js.map +1 -1
  66. package/dist/memory/sqlite.js +385 -3
  67. package/dist/memory/sqlite.js.map +1 -1
  68. package/dist/memory/storage.js +1 -2
  69. package/dist/memory/storage.js.map +1 -1
  70. package/dist/memory/tool-defaults.js +64 -28
  71. package/dist/memory/tool-defaults.js.map +1 -1
  72. package/dist/memory/types.js.map +1 -1
  73. package/dist/monitor.d.ts +2 -0
  74. package/dist/monitor.js +104 -0
  75. package/dist/monitor.js.map +1 -0
  76. package/dist/onboarding/connect.js +4 -6
  77. package/dist/onboarding/connect.js.map +1 -1
  78. package/dist/onboarding/credential-interceptor.js +1 -1
  79. package/dist/onboarding/credential-interceptor.js.map +1 -1
  80. package/dist/onboarding/degradation-accumulator.js +1 -3
  81. package/dist/onboarding/degradation-accumulator.js.map +1 -1
  82. package/dist/onboarding/providers.js +3 -16
  83. package/dist/onboarding/providers.js.map +1 -1
  84. package/dist/onboarding/state.js.map +1 -1
  85. package/dist/onboarding/tool-helpers.js +1 -1
  86. package/dist/onboarding/tool-helpers.js.map +1 -1
  87. package/dist/onboarding/tool-tags.js +6 -4
  88. package/dist/onboarding/tool-tags.js.map +1 -1
  89. package/dist/onboarding/validation.js +1 -1
  90. package/dist/onboarding/validation.js.map +1 -1
  91. package/dist/pi/opencandle-extension.d.ts +8 -0
  92. package/dist/pi/opencandle-extension.js +412 -28
  93. package/dist/pi/opencandle-extension.js.map +1 -1
  94. package/dist/pi/session.d.ts +1 -1
  95. package/dist/pi/session.js +3 -1
  96. package/dist/pi/session.js.map +1 -1
  97. package/dist/pi/setup.js +8 -3
  98. package/dist/pi/setup.js.map +1 -1
  99. package/dist/pi/tool-adapter.js +5 -2
  100. package/dist/pi/tool-adapter.js.map +1 -1
  101. package/dist/prompts/context-builder.d.ts +1 -1
  102. package/dist/prompts/context-builder.js +19 -6
  103. package/dist/prompts/context-builder.js.map +1 -1
  104. package/dist/prompts/policy-cards.d.ts +1 -1
  105. package/dist/prompts/policy-cards.js +1 -1
  106. package/dist/prompts/policy-cards.js.map +1 -1
  107. package/dist/prompts/sections.d.ts +1 -1
  108. package/dist/prompts/symbol-preflight.d.ts +20 -0
  109. package/dist/prompts/symbol-preflight.js +49 -0
  110. package/dist/prompts/symbol-preflight.js.map +1 -0
  111. package/dist/prompts/workflow-prompts.d.ts +1 -1
  112. package/dist/prompts/workflow-prompts.js +54 -16
  113. package/dist/prompts/workflow-prompts.js.map +1 -1
  114. package/dist/providers/alpha-vantage.d.ts +1 -1
  115. package/dist/providers/alpha-vantage.js +26 -7
  116. package/dist/providers/alpha-vantage.js.map +1 -1
  117. package/dist/providers/coingecko.js +1 -1
  118. package/dist/providers/coingecko.js.map +1 -1
  119. package/dist/providers/errors.d.ts +5 -0
  120. package/dist/providers/errors.js +11 -0
  121. package/dist/providers/errors.js.map +1 -0
  122. package/dist/providers/exa-search.d.ts +2 -2
  123. package/dist/providers/exa-search.js +19 -11
  124. package/dist/providers/exa-search.js.map +1 -1
  125. package/dist/providers/fear-greed.js +1 -1
  126. package/dist/providers/fear-greed.js.map +1 -1
  127. package/dist/providers/finnhub.js +3 -5
  128. package/dist/providers/finnhub.js.map +1 -1
  129. package/dist/providers/fred.js +2 -2
  130. package/dist/providers/fred.js.map +1 -1
  131. package/dist/providers/index.d.ts +7 -6
  132. package/dist/providers/index.js +6 -5
  133. package/dist/providers/index.js.map +1 -1
  134. package/dist/providers/reddit.js +2 -2
  135. package/dist/providers/reddit.js.map +1 -1
  136. package/dist/providers/sec-edgar.d.ts +1 -0
  137. package/dist/providers/sec-edgar.js +12 -4
  138. package/dist/providers/sec-edgar.js.map +1 -1
  139. package/dist/providers/tradingview.d.ts +47 -0
  140. package/dist/providers/tradingview.js +275 -0
  141. package/dist/providers/tradingview.js.map +1 -0
  142. package/dist/providers/twitter.js +6 -8
  143. package/dist/providers/twitter.js.map +1 -1
  144. package/dist/providers/web-search.js +26 -12
  145. package/dist/providers/web-search.js.map +1 -1
  146. package/dist/providers/with-fallback.js +4 -2
  147. package/dist/providers/with-fallback.js.map +1 -1
  148. package/dist/providers/wrap-provider.d.ts +2 -3
  149. package/dist/providers/wrap-provider.js +14 -8
  150. package/dist/providers/wrap-provider.js.map +1 -1
  151. package/dist/providers/yahoo-finance.d.ts +1 -1
  152. package/dist/providers/yahoo-finance.js +101 -17
  153. package/dist/providers/yahoo-finance.js.map +1 -1
  154. package/dist/routing/classify-intent.d.ts +6 -0
  155. package/dist/routing/classify-intent.js +78 -7
  156. package/dist/routing/classify-intent.js.map +1 -1
  157. package/dist/routing/defaults.d.ts +1 -1
  158. package/dist/routing/entity-extractor.d.ts +1 -0
  159. package/dist/routing/entity-extractor.js +234 -29
  160. package/dist/routing/entity-extractor.js.map +1 -1
  161. package/dist/routing/fund-symbols.d.ts +2 -0
  162. package/dist/routing/fund-symbols.js +55 -0
  163. package/dist/routing/fund-symbols.js.map +1 -0
  164. package/dist/routing/horizon.d.ts +1 -0
  165. package/dist/routing/horizon.js +10 -0
  166. package/dist/routing/horizon.js.map +1 -0
  167. package/dist/routing/index.d.ts +10 -10
  168. package/dist/routing/index.js +6 -6
  169. package/dist/routing/index.js.map +1 -1
  170. package/dist/routing/planning.d.ts +1 -1
  171. package/dist/routing/planning.js +65 -34
  172. package/dist/routing/planning.js.map +1 -1
  173. package/dist/routing/route-manifest.d.ts +2 -2
  174. package/dist/routing/route-manifest.js +25 -4
  175. package/dist/routing/route-manifest.js.map +1 -1
  176. package/dist/routing/router-llm-client.js.map +1 -1
  177. package/dist/routing/router-prompt.js +7 -9
  178. package/dist/routing/router-prompt.js.map +1 -1
  179. package/dist/routing/router-types.d.ts +1 -0
  180. package/dist/routing/router.js +137 -22
  181. package/dist/routing/router.js.map +1 -1
  182. package/dist/routing/slot-resolver.d.ts +1 -1
  183. package/dist/routing/slot-resolver.js +2 -4
  184. package/dist/routing/slot-resolver.js.map +1 -1
  185. package/dist/routing/symbol-disambiguator.d.ts +11 -0
  186. package/dist/routing/symbol-disambiguator.js +52 -0
  187. package/dist/routing/symbol-disambiguator.js.map +1 -0
  188. package/dist/routing/turn-context.d.ts +1 -1
  189. package/dist/routing/turn-context.js +1 -1
  190. package/dist/routing/turn-context.js.map +1 -1
  191. package/dist/routing/types.d.ts +2 -0
  192. package/dist/runtime/answer-contracts.js +36 -8
  193. package/dist/runtime/answer-contracts.js.map +1 -1
  194. package/dist/runtime/artifact-contracts.js.map +1 -1
  195. package/dist/runtime/planning-evidence.js +47 -26
  196. package/dist/runtime/planning-evidence.js.map +1 -1
  197. package/dist/runtime/prompt-step.d.ts +1 -9
  198. package/dist/runtime/prompt-step.js +0 -10
  199. package/dist/runtime/prompt-step.js.map +1 -1
  200. package/dist/runtime/run-context.d.ts +5 -2
  201. package/dist/runtime/run-context.js +8 -1
  202. package/dist/runtime/run-context.js.map +1 -1
  203. package/dist/runtime/session-coordinator.d.ts +13 -5
  204. package/dist/runtime/session-coordinator.js +160 -20
  205. package/dist/runtime/session-coordinator.js.map +1 -1
  206. package/dist/runtime/session-title.d.ts +14 -0
  207. package/dist/runtime/session-title.js +50 -0
  208. package/dist/runtime/session-title.js.map +1 -0
  209. package/dist/runtime/tool-defaults-wrapper.js +1 -3
  210. package/dist/runtime/tool-defaults-wrapper.js.map +1 -1
  211. package/dist/runtime/validation.js.map +1 -1
  212. package/dist/runtime/workflow-events.js.map +1 -1
  213. package/dist/runtime/workflow-runner.d.ts +3 -3
  214. package/dist/runtime/workflow-runner.js +1 -1
  215. package/dist/runtime/workflow-runner.js.map +1 -1
  216. package/dist/sentiment/adapters/finnhub.d.ts +1 -1
  217. package/dist/sentiment/adapters/finnhub.js +6 -1
  218. package/dist/sentiment/adapters/finnhub.js.map +1 -1
  219. package/dist/sentiment/adapters/reddit.d.ts +2 -2
  220. package/dist/sentiment/adapters/twitter.d.ts +1 -1
  221. package/dist/sentiment/adapters/web.d.ts +1 -1
  222. package/dist/sentiment/index.d.ts +9 -11
  223. package/dist/sentiment/index.js +9 -20
  224. package/dist/sentiment/index.js.map +1 -1
  225. package/dist/sentiment/keywords.js +26 -4
  226. package/dist/sentiment/keywords.js.map +1 -1
  227. package/dist/sentiment/pipeline.d.ts +2 -2
  228. package/dist/sentiment/pipeline.js +1 -1
  229. package/dist/sentiment/pipeline.js.map +1 -1
  230. package/dist/sentiment/scorer.js +1 -1
  231. package/dist/sentiment/store.d.ts +1 -1
  232. package/dist/sentiment/store.js +1 -1
  233. package/dist/sentiment/store.js.map +1 -1
  234. package/dist/sentiment/trends.d.ts +1 -1
  235. package/dist/sentiment/trends.js.map +1 -1
  236. package/dist/sentiment/types.js.map +1 -1
  237. package/dist/system-prompt.js +3 -2
  238. package/dist/system-prompt.js.map +1 -1
  239. package/dist/tool-kit.d.ts +7 -7
  240. package/dist/tool-kit.js +4 -4
  241. package/dist/tool-kit.js.map +1 -1
  242. package/dist/tools/fundamentals/company-overview.js +11 -6
  243. package/dist/tools/fundamentals/company-overview.js.map +1 -1
  244. package/dist/tools/fundamentals/comps.js +18 -9
  245. package/dist/tools/fundamentals/comps.js.map +1 -1
  246. package/dist/tools/fundamentals/dcf.js +23 -11
  247. package/dist/tools/fundamentals/dcf.js.map +1 -1
  248. package/dist/tools/fundamentals/earnings.js +8 -3
  249. package/dist/tools/fundamentals/earnings.js.map +1 -1
  250. package/dist/tools/fundamentals/financials.js +8 -3
  251. package/dist/tools/fundamentals/financials.js.map +1 -1
  252. package/dist/tools/fundamentals/sec-filings.js +21 -6
  253. package/dist/tools/fundamentals/sec-filings.js.map +1 -1
  254. package/dist/tools/index.d.ts +23 -19
  255. package/dist/tools/index.js +51 -39
  256. package/dist/tools/index.js.map +1 -1
  257. package/dist/tools/interaction/ask-user.js +15 -3
  258. package/dist/tools/interaction/ask-user.js.map +1 -1
  259. package/dist/tools/interaction/twitter-login.js +13 -3
  260. package/dist/tools/interaction/twitter-login.js.map +1 -1
  261. package/dist/tools/macro/fear-greed.js.map +1 -1
  262. package/dist/tools/macro/fred-data.d.ts +1 -1
  263. package/dist/tools/macro/fred-data.js +17 -6
  264. package/dist/tools/macro/fred-data.js.map +1 -1
  265. package/dist/tools/market/crypto-history.js +3 -1
  266. package/dist/tools/market/crypto-history.js.map +1 -1
  267. package/dist/tools/market/crypto-price.js +3 -1
  268. package/dist/tools/market/crypto-price.js.map +1 -1
  269. package/dist/tools/market/screen-stocks.d.ts +18 -0
  270. package/dist/tools/market/screen-stocks.js +252 -0
  271. package/dist/tools/market/screen-stocks.js.map +1 -0
  272. package/dist/tools/market/search-ticker.js +160 -8
  273. package/dist/tools/market/search-ticker.js.map +1 -1
  274. package/dist/tools/market/stock-history.d.ts +2 -2
  275. package/dist/tools/market/stock-history.js +26 -7
  276. package/dist/tools/market/stock-history.js.map +1 -1
  277. package/dist/tools/market/stock-quote.js +5 -3
  278. package/dist/tools/market/stock-quote.js.map +1 -1
  279. package/dist/tools/options/greeks.js +1 -1
  280. package/dist/tools/options/greeks.js.map +1 -1
  281. package/dist/tools/options/option-chain.js +19 -6
  282. package/dist/tools/options/option-chain.js.map +1 -1
  283. package/dist/tools/portfolio/alerts.d.ts +15 -0
  284. package/dist/tools/portfolio/alerts.js +357 -0
  285. package/dist/tools/portfolio/alerts.js.map +1 -0
  286. package/dist/tools/portfolio/correlation.d.ts +1 -1
  287. package/dist/tools/portfolio/correlation.js +33 -13
  288. package/dist/tools/portfolio/correlation.js.map +1 -1
  289. package/dist/tools/portfolio/daily-report.d.ts +8 -0
  290. package/dist/tools/portfolio/daily-report.js +83 -0
  291. package/dist/tools/portfolio/daily-report.js.map +1 -0
  292. package/dist/tools/portfolio/holdings-overlap.js +10 -3
  293. package/dist/tools/portfolio/holdings-overlap.js.map +1 -1
  294. package/dist/tools/portfolio/notifications.d.ts +7 -0
  295. package/dist/tools/portfolio/notifications.js +43 -0
  296. package/dist/tools/portfolio/notifications.js.map +1 -0
  297. package/dist/tools/portfolio/predictions.d.ts +12 -6
  298. package/dist/tools/portfolio/predictions.js +337 -87
  299. package/dist/tools/portfolio/predictions.js.map +1 -1
  300. package/dist/tools/portfolio/risk-analysis.d.ts +1 -1
  301. package/dist/tools/portfolio/risk-analysis.js +45 -6
  302. package/dist/tools/portfolio/risk-analysis.js.map +1 -1
  303. package/dist/tools/portfolio/tracker.d.ts +4 -3
  304. package/dist/tools/portfolio/tracker.js +246 -101
  305. package/dist/tools/portfolio/tracker.js.map +1 -1
  306. package/dist/tools/portfolio/watchlist.d.ts +6 -4
  307. package/dist/tools/portfolio/watchlist.js +208 -108
  308. package/dist/tools/portfolio/watchlist.js.map +1 -1
  309. package/dist/tools/sentiment/reddit-sentiment.js +23 -10
  310. package/dist/tools/sentiment/reddit-sentiment.js.map +1 -1
  311. package/dist/tools/sentiment/sentiment-summary.js +15 -13
  312. package/dist/tools/sentiment/sentiment-summary.js.map +1 -1
  313. package/dist/tools/sentiment/sentiment-trend.d.ts +1 -1
  314. package/dist/tools/sentiment/sentiment-trend.js +12 -2
  315. package/dist/tools/sentiment/sentiment-trend.js.map +1 -1
  316. package/dist/tools/sentiment/twitter-sentiment.js +12 -5
  317. package/dist/tools/sentiment/twitter-sentiment.js.map +1 -1
  318. package/dist/tools/sentiment/untrusted-text.d.ts +2 -0
  319. package/dist/tools/sentiment/untrusted-text.js +17 -0
  320. package/dist/tools/sentiment/untrusted-text.js.map +1 -0
  321. package/dist/tools/sentiment/web-search.js +9 -13
  322. package/dist/tools/sentiment/web-search.js.map +1 -1
  323. package/dist/tools/sentiment/web-sentiment.js +15 -3
  324. package/dist/tools/sentiment/web-sentiment.js.map +1 -1
  325. package/dist/tools/technical/backtest.d.ts +1 -1
  326. package/dist/tools/technical/backtest.js +27 -20
  327. package/dist/tools/technical/backtest.js.map +1 -1
  328. package/dist/tools/technical/indicators.js +23 -5
  329. package/dist/tools/technical/indicators.js.map +1 -1
  330. package/dist/types/index.d.ts +3 -3
  331. package/dist/types/index.js.map +1 -1
  332. package/dist/types/market.d.ts +1 -0
  333. package/dist/types/portfolio.d.ts +14 -4
  334. package/dist/workflows/compare-assets.d.ts +0 -3
  335. package/dist/workflows/compare-assets.js +20 -11
  336. package/dist/workflows/compare-assets.js.map +1 -1
  337. package/dist/workflows/index.d.ts +3 -4
  338. package/dist/workflows/index.js +3 -3
  339. package/dist/workflows/index.js.map +1 -1
  340. package/dist/workflows/options-screener.d.ts +0 -3
  341. package/dist/workflows/options-screener.js +4 -11
  342. package/dist/workflows/options-screener.js.map +1 -1
  343. package/dist/workflows/portfolio-builder.d.ts +0 -3
  344. package/dist/workflows/portfolio-builder.js +0 -8
  345. package/dist/workflows/portfolio-builder.js.map +1 -1
  346. package/gui/server/ask-user-bridge.ts +1 -1
  347. package/gui/server/automation-heartbeat.ts +97 -0
  348. package/gui/server/background-quotes.ts +97 -1
  349. package/gui/server/chat-event-adapter.ts +32 -10
  350. package/gui/server/chat-run-session.ts +16 -0
  351. package/gui/server/invoke-tool.ts +144 -1
  352. package/gui/server/live-chat-event-adapter.ts +21 -6
  353. package/gui/server/market-state-api.ts +315 -0
  354. package/gui/server/model-setup.ts +149 -2
  355. package/gui/server/private-api-access.ts +62 -0
  356. package/gui/server/projector.ts +12 -7
  357. package/gui/server/prompt-observation.ts +4 -7
  358. package/gui/server/quote-snapshot-store.ts +50 -0
  359. package/gui/server/server.ts +200 -451
  360. package/gui/server/session-actions.ts +186 -1
  361. package/gui/server/shutdown.ts +47 -0
  362. package/gui/server/tool-invoke-ack.ts +49 -0
  363. package/gui/server/tool-metadata.ts +23 -10
  364. package/gui/server/websocket.ts +13 -3
  365. package/gui/server/writer-lock.ts +6 -2
  366. package/gui/server/ws-hub.ts +292 -0
  367. package/gui/shared/chat-events.ts +16 -1
  368. package/gui/shared/event-reducer.ts +24 -6
  369. package/gui/web/dist/assets/CatalogOverlay-eJ2cBk33.js +1 -0
  370. package/gui/web/dist/assets/index-2KZtKBmu.css +1 -0
  371. package/gui/web/dist/assets/index-CveNgtDg.js +69 -0
  372. package/gui/web/dist/index.html +2 -2
  373. package/package.json +5 -1
  374. package/src/analysts/contracts.ts +10 -23
  375. package/src/analysts/orchestrator.ts +8 -43
  376. package/src/cli.ts +35 -12
  377. package/src/config.ts +17 -9
  378. package/src/index.ts +1 -1
  379. package/src/infra/browser.ts +3 -1
  380. package/src/infra/cache.ts +41 -30
  381. package/src/infra/http-client.ts +72 -6
  382. package/src/infra/index.ts +7 -10
  383. package/src/infra/native-dependencies.ts +8 -3
  384. package/src/infra/node-version.ts +3 -1
  385. package/src/infra/opencandle-paths.ts +3 -14
  386. package/src/infra/rate-limiter.ts +22 -19
  387. package/src/market-state/alert-conditions.ts +82 -0
  388. package/src/market-state/alert-runner.ts +863 -0
  389. package/src/market-state/daily-report.ts +247 -0
  390. package/src/market-state/local-automation-service.ts +162 -0
  391. package/src/market-state/notification-delivery.ts +158 -0
  392. package/src/market-state/resolve-for-mutation.ts +24 -0
  393. package/src/market-state/resolve.ts +112 -0
  394. package/src/market-state/service.ts +2344 -0
  395. package/src/memory/index.ts +7 -7
  396. package/src/memory/manager.ts +14 -16
  397. package/src/memory/retrieval.ts +8 -7
  398. package/src/memory/sqlite.ts +407 -6
  399. package/src/memory/storage.ts +5 -15
  400. package/src/memory/tool-defaults.ts +60 -39
  401. package/src/memory/types.ts +3 -3
  402. package/src/monitor.ts +121 -0
  403. package/src/onboarding/connect.ts +10 -33
  404. package/src/onboarding/credential-interceptor.ts +3 -15
  405. package/src/onboarding/degradation-accumulator.ts +1 -3
  406. package/src/onboarding/providers.ts +9 -40
  407. package/src/onboarding/state.ts +4 -15
  408. package/src/onboarding/tool-helpers.ts +2 -9
  409. package/src/onboarding/tool-tags.ts +6 -6
  410. package/src/onboarding/validation.ts +14 -20
  411. package/src/pi/opencandle-extension.ts +529 -85
  412. package/src/pi/session.ts +7 -5
  413. package/src/pi/setup.ts +61 -43
  414. package/src/pi/tool-adapter.ts +5 -2
  415. package/src/prompts/context-builder.ts +23 -12
  416. package/src/prompts/policy-cards.ts +2 -2
  417. package/src/prompts/sections.ts +1 -1
  418. package/src/prompts/symbol-preflight.ts +80 -0
  419. package/src/prompts/workflow-prompts.ts +77 -28
  420. package/src/providers/alpha-vantage.ts +58 -39
  421. package/src/providers/coingecko.ts +2 -5
  422. package/src/providers/errors.ts +9 -0
  423. package/src/providers/exa-search.ts +24 -22
  424. package/src/providers/fear-greed.ts +1 -1
  425. package/src/providers/finnhub.ts +7 -6
  426. package/src/providers/fred.ts +3 -3
  427. package/src/providers/index.ts +14 -6
  428. package/src/providers/reddit.ts +17 -6
  429. package/src/providers/sec-edgar.ts +20 -6
  430. package/src/providers/tradingview.ts +399 -0
  431. package/src/providers/twitter.ts +6 -8
  432. package/src/providers/web-search.ts +30 -20
  433. package/src/providers/with-fallback.ts +8 -7
  434. package/src/providers/wrap-provider.ts +15 -10
  435. package/src/providers/yahoo-finance.ts +140 -35
  436. package/src/routing/classify-intent.ts +101 -10
  437. package/src/routing/defaults.ts +1 -1
  438. package/src/routing/entity-extractor.ts +287 -38
  439. package/src/routing/fund-symbols.ts +58 -0
  440. package/src/routing/horizon.ts +7 -0
  441. package/src/routing/index.ts +48 -48
  442. package/src/routing/planning.ts +144 -53
  443. package/src/routing/route-manifest.ts +37 -15
  444. package/src/routing/router-llm-client.ts +4 -4
  445. package/src/routing/router-prompt.ts +15 -19
  446. package/src/routing/router-types.ts +2 -5
  447. package/src/routing/router.ts +251 -53
  448. package/src/routing/slot-resolver.ts +34 -11
  449. package/src/routing/symbol-disambiguator.ts +72 -0
  450. package/src/routing/turn-context.ts +6 -9
  451. package/src/routing/types.ts +2 -0
  452. package/src/runtime/answer-contracts.ts +82 -43
  453. package/src/runtime/artifact-contracts.ts +2 -1
  454. package/src/runtime/planning-evidence.ts +157 -66
  455. package/src/runtime/prompt-step.ts +1 -16
  456. package/src/runtime/run-context.ts +12 -2
  457. package/src/runtime/session-coordinator.ts +238 -63
  458. package/src/runtime/session-title.ts +60 -0
  459. package/src/runtime/tool-defaults-wrapper.ts +1 -3
  460. package/src/runtime/validation.ts +1 -4
  461. package/src/runtime/workflow-events.ts +7 -7
  462. package/src/runtime/workflow-runner.ts +5 -11
  463. package/src/sentiment/adapters/finnhub.ts +7 -2
  464. package/src/sentiment/adapters/reddit.ts +2 -2
  465. package/src/sentiment/adapters/twitter.ts +1 -1
  466. package/src/sentiment/adapters/web.ts +1 -1
  467. package/src/sentiment/index.ts +16 -26
  468. package/src/sentiment/keywords.ts +26 -4
  469. package/src/sentiment/pipeline.ts +15 -4
  470. package/src/sentiment/scorer.ts +1 -1
  471. package/src/sentiment/store.ts +2 -2
  472. package/src/sentiment/trends.ts +9 -3
  473. package/src/sentiment/types.ts +5 -4
  474. package/src/system-prompt.ts +3 -2
  475. package/src/tool-kit.ts +10 -9
  476. package/src/tools/fundamentals/company-overview.ts +19 -9
  477. package/src/tools/fundamentals/comps.ts +68 -55
  478. package/src/tools/fundamentals/dcf.ts +145 -95
  479. package/src/tools/fundamentals/earnings.ts +16 -6
  480. package/src/tools/fundamentals/financials.ts +16 -7
  481. package/src/tools/fundamentals/sec-filings.ts +37 -16
  482. package/src/tools/index.ts +51 -39
  483. package/src/tools/interaction/ask-user.ts +22 -10
  484. package/src/tools/interaction/twitter-login.ts +17 -5
  485. package/src/tools/macro/fear-greed.ts +1 -1
  486. package/src/tools/macro/fred-data.ts +58 -46
  487. package/src/tools/market/crypto-history.ts +8 -3
  488. package/src/tools/market/crypto-price.ts +6 -6
  489. package/src/tools/market/screen-stocks.ts +279 -0
  490. package/src/tools/market/search-ticker.ts +218 -17
  491. package/src/tools/market/stock-history.ts +37 -12
  492. package/src/tools/market/stock-quote.ts +10 -7
  493. package/src/tools/options/greeks.ts +5 -5
  494. package/src/tools/options/option-chain.ts +41 -17
  495. package/src/tools/portfolio/alerts.ts +457 -0
  496. package/src/tools/portfolio/correlation.ts +47 -20
  497. package/src/tools/portfolio/daily-report.ts +101 -0
  498. package/src/tools/portfolio/holdings-overlap.ts +31 -15
  499. package/src/tools/portfolio/notifications.ts +45 -0
  500. package/src/tools/portfolio/predictions.ts +406 -106
  501. package/src/tools/portfolio/risk-analysis.ts +46 -7
  502. package/src/tools/portfolio/tracker.ts +270 -109
  503. package/src/tools/portfolio/watchlist.ts +250 -121
  504. package/src/tools/sentiment/reddit-sentiment.ts +50 -24
  505. package/src/tools/sentiment/sentiment-summary.ts +62 -41
  506. package/src/tools/sentiment/sentiment-trend.ts +24 -7
  507. package/src/tools/sentiment/twitter-sentiment.ts +22 -15
  508. package/src/tools/sentiment/untrusted-text.ts +21 -0
  509. package/src/tools/sentiment/web-search.ts +21 -18
  510. package/src/tools/sentiment/web-sentiment.ts +26 -10
  511. package/src/tools/technical/backtest.ts +32 -22
  512. package/src/tools/technical/indicators.ts +39 -14
  513. package/src/types/index.ts +8 -3
  514. package/src/types/market.ts +1 -0
  515. package/src/types/portfolio.ts +14 -4
  516. package/src/types/sentiment.ts +2 -2
  517. package/src/workflows/compare-assets.ts +33 -21
  518. package/src/workflows/index.ts +3 -4
  519. package/src/workflows/options-screener.ts +27 -29
  520. package/src/workflows/portfolio-builder.ts +34 -27
  521. package/dist/workflows/types.d.ts +0 -4
  522. package/dist/workflows/types.js +0 -2
  523. package/dist/workflows/types.js.map +0 -1
  524. package/gui/web/dist/assets/CatalogOverlay-Bmp6Knu7.js +0 -1
  525. package/gui/web/dist/assets/index-Bxt9QpLX.css +0 -1
  526. package/gui/web/dist/assets/index-CZ9DHZYy.js +0 -67
  527. package/src/workflows/types.ts +0 -4
@@ -0,0 +1,863 @@
1
+ import { canUseTradingViewQuote, getQuotes } from "../providers/tradingview.js";
2
+ import { wrapProvider } from "../providers/wrap-provider.js";
3
+ import { getHistory, getQuote } from "../providers/yahoo-finance.js";
4
+ import { computeRSI, computeSMA } from "../tools/technical/indicators.js";
5
+ import type { OHLCV } from "../types/market.js";
6
+ import { ALERT_CONDITION_VERSION } from "./alert-conditions.js";
7
+ import { isZeroFilledQuote } from "./resolve.js";
8
+ import type { AlertRuleRecord, InstrumentRecord, MarketStateService } from "./service.js";
9
+
10
+ export interface AlertQuoteObservation {
11
+ symbol: string;
12
+ value: number;
13
+ sourceProvider: string;
14
+ observedAt: string;
15
+ providerDataAt?: string | null;
16
+ cacheStatus: "live" | "cached" | "stale";
17
+ dataDelayMs?: number | null;
18
+ caveat?: string;
19
+ }
20
+
21
+ export interface AlertRunnerProviders {
22
+ getTradingViewQuotes(symbols: string[]): Promise<AlertQuoteObservation[]>;
23
+ getYahooQuote(symbol: string): Promise<AlertQuoteObservation>;
24
+ getHistory(symbol: string, range: string, interval: string): Promise<OHLCV[]>;
25
+ }
26
+
27
+ export interface AlertRunnerOptions {
28
+ ownerId?: string | null;
29
+ triggerType: "heartbeat" | "manual" | "scheduled" | "resume";
30
+ now?: string;
31
+ providers: AlertRunnerProviders;
32
+ providerBudget?: AlertProviderBudget;
33
+ }
34
+
35
+ export interface AlertRunnerResult {
36
+ checked: number;
37
+ triggered: number;
38
+ unavailable: number;
39
+ runId: number;
40
+ lines: string[];
41
+ }
42
+
43
+ export interface AlertProviderBudgetOptions {
44
+ failureThreshold?: number;
45
+ backoffMs?: number;
46
+ }
47
+
48
+ export interface AlertProviderBudgetSnapshotEntry {
49
+ state: "available" | "open";
50
+ failureCount: number;
51
+ openUntil?: string;
52
+ reason?: string;
53
+ }
54
+
55
+ interface ProviderBudgetState {
56
+ failureCount: number;
57
+ openUntilMs: number;
58
+ reason?: string;
59
+ }
60
+
61
+ export class AlertProviderBudget {
62
+ private readonly state = new Map<string, ProviderBudgetState>();
63
+ private readonly failureThreshold: number;
64
+ private readonly backoffMs: number;
65
+
66
+ constructor(options: AlertProviderBudgetOptions = {}) {
67
+ this.failureThreshold = options.failureThreshold ?? 2;
68
+ this.backoffMs = options.backoffMs ?? 5 * 60_000;
69
+ }
70
+
71
+ unavailableReason(provider: string, now: string): string | null {
72
+ const current = this.state.get(provider);
73
+ if (current == null || current.openUntilMs <= new Date(now).getTime()) return null;
74
+ return `${provider} provider_budget_exhausted until ${new Date(current.openUntilMs).toISOString()}${current.reason ? ` (${current.reason})` : ""}`;
75
+ }
76
+
77
+ recordSuccess(provider: string): void {
78
+ this.state.delete(provider);
79
+ }
80
+
81
+ recordFailure(provider: string, reason: string, now: string): void {
82
+ const current = this.state.get(provider) ?? { failureCount: 0, openUntilMs: 0 };
83
+ const nextFailureCount = current.failureCount + 1;
84
+ this.state.set(provider, {
85
+ failureCount: nextFailureCount,
86
+ openUntilMs:
87
+ nextFailureCount >= this.failureThreshold ? new Date(now).getTime() + this.backoffMs : 0,
88
+ reason,
89
+ });
90
+ }
91
+
92
+ reset(): void {
93
+ this.state.clear();
94
+ }
95
+
96
+ snapshot(now: string): Record<string, AlertProviderBudgetSnapshotEntry> {
97
+ const nowMs = new Date(now).getTime();
98
+ return Object.fromEntries(
99
+ [...this.state.entries()].map(([provider, state]) => [
100
+ provider,
101
+ {
102
+ state: state.openUntilMs > nowMs ? "open" : "available",
103
+ failureCount: state.failureCount,
104
+ ...(state.openUntilMs > nowMs
105
+ ? { openUntil: new Date(state.openUntilMs).toISOString() }
106
+ : {}),
107
+ ...(state.reason ? { reason: state.reason } : {}),
108
+ },
109
+ ]),
110
+ );
111
+ }
112
+ }
113
+
114
+ export const defaultAlertProviderBudget = new AlertProviderBudget();
115
+
116
+ export const defaultAlertRunnerProviders: AlertRunnerProviders = {
117
+ async getTradingViewQuotes(symbols) {
118
+ const result = await wrapProvider("tradingview", () => getQuotes(symbols));
119
+ if (result.status === "unavailable") throw new Error(result.reason);
120
+ if (result.stale) throw new Error("provider returned stale market data");
121
+ return result.data.map((quote) => ({
122
+ symbol: quote.requestedSymbol,
123
+ value: quote.price,
124
+ sourceProvider: "tradingview",
125
+ observedAt: result.timestamp,
126
+ providerDataAt: null,
127
+ cacheStatus: result.stale ? "stale" : "live",
128
+ dataDelayMs: 15 * 60_000,
129
+ caveat: quote.dataCaveat,
130
+ }));
131
+ },
132
+ async getYahooQuote(symbol) {
133
+ const result = await wrapProvider("yahoo", () => getQuote(symbol));
134
+ if (result.status === "unavailable") throw new Error(result.reason);
135
+ if (result.stale) throw new Error("provider returned stale market data");
136
+ if (isZeroFilledQuote(result.data)) throw new Error("Yahoo returned no valid market data.");
137
+ return {
138
+ symbol,
139
+ value: result.data.price,
140
+ sourceProvider: "yahoo",
141
+ observedAt: result.timestamp,
142
+ providerDataAt: new Date(result.data.timestamp).toISOString(),
143
+ cacheStatus: "live",
144
+ };
145
+ },
146
+ async getHistory(symbol, range, interval) {
147
+ const result = await wrapProvider("yahoo", () => getHistory(symbol, range, interval));
148
+ if (result.status === "unavailable") throw new Error(result.reason);
149
+ if (result.stale) throw new Error("provider returned stale market data");
150
+ return result.data;
151
+ },
152
+ };
153
+
154
+ interface ObservationSet {
155
+ observations: Map<string, AlertQuoteObservation>;
156
+ unavailableReasons: Map<string, string>;
157
+ }
158
+
159
+ interface RunnableRule {
160
+ rule: AlertRuleRecord;
161
+ instrument: InstrumentRecord;
162
+ }
163
+
164
+ export async function runAlertChecks(
165
+ service: MarketStateService,
166
+ options: AlertRunnerOptions,
167
+ ): Promise<AlertRunnerResult> {
168
+ const now = options.now ?? new Date().toISOString();
169
+ const providerBudget = options.providerBudget ?? defaultAlertProviderBudget;
170
+ const historyCache = new Map<string, Promise<OHLCV[]>>();
171
+ const run = service.startAlertCheckRun({
172
+ ownerId: options.ownerId,
173
+ triggerType: options.triggerType,
174
+ startedAt: now,
175
+ });
176
+
177
+ let triggered = 0;
178
+ let unavailable = 0;
179
+ const lines: string[] = [];
180
+
181
+ try {
182
+ const rules = service
183
+ .listAlertRules()
184
+ .filter((rule) => rule.enabled && rule.status === "active" && isDue(rule, now));
185
+ const runnable = rules.flatMap((rule): RunnableRule[] => {
186
+ if (rule.conditionVersion !== ALERT_CONDITION_VERSION) {
187
+ unavailable++;
188
+ lines.push(
189
+ `#${rule.id}: needs review (unsupported condition version ${rule.conditionVersion})`,
190
+ );
191
+ return [];
192
+ }
193
+ if (rule.instrumentId == null) {
194
+ unavailable++;
195
+ lines.push(`#${rule.id}: unavailable instrument`);
196
+ return [];
197
+ }
198
+ const instrument = service.getInstrument(rule.instrumentId);
199
+ if (instrument == null) {
200
+ unavailable++;
201
+ lines.push(`#${rule.id}: unavailable instrument`);
202
+ return [];
203
+ }
204
+ return [{ rule, instrument }];
205
+ });
206
+
207
+ const priceRules = runnable.filter(
208
+ ({ rule }) =>
209
+ rule.conditionType === "price_crosses_above" ||
210
+ rule.conditionType === "price_crosses_below",
211
+ );
212
+ const quoteObservations = await loadPriceObservations(
213
+ priceRules,
214
+ options.providers,
215
+ providerBudget,
216
+ now,
217
+ );
218
+
219
+ for (const item of runnable) {
220
+ const observationKey = isPriceRule(item.rule)
221
+ ? quoteObservationKey(item.instrument.symbol, allowsDelayedObservation(item.rule))
222
+ : item.instrument.symbol;
223
+ const observation = isPriceRule(item.rule)
224
+ ? quoteObservations.observations.get(observationKey)
225
+ : await loadHistoricalObservation(
226
+ item,
227
+ options.providers,
228
+ providerBudget,
229
+ historyCache,
230
+ now,
231
+ );
232
+ if (!observation) {
233
+ const reason =
234
+ quoteObservations.unavailableReasons.get(observationKey) ??
235
+ quoteObservations.unavailableReasons.get(item.instrument.symbol) ??
236
+ `no provider observation for ${item.instrument.symbol}`;
237
+ unavailable++;
238
+ service.recordAlertUnavailable({
239
+ ruleId: item.rule.id,
240
+ instrumentId: item.instrument.id,
241
+ reason,
242
+ checkedAt: now,
243
+ });
244
+ lines.push(`${item.instrument.symbol}: unavailable (${reason})`);
245
+ continue;
246
+ }
247
+
248
+ const previous = lastObservedValue(item.rule);
249
+ const conditionState = conditionIsTrue(item.rule, observation.value) ? "true" : "false";
250
+ const shouldTrigger =
251
+ conditionState === "true" &&
252
+ previous != null &&
253
+ crosses(item.rule, previous, observation.value) &&
254
+ outsideCooldown(item.rule, now);
255
+ const observed = {
256
+ value: observation.value,
257
+ field: observationField(item.rule),
258
+ at: now,
259
+ observedAt: now,
260
+ providerDataAt: observation.providerDataAt ?? null,
261
+ sourceProvider: observation.sourceProvider,
262
+ cacheStatus: observation.cacheStatus,
263
+ dataDelayMs: observation.dataDelayMs ?? null,
264
+ caveat: observation.caveat ?? null,
265
+ };
266
+
267
+ const result = service.recordAlertEvaluationResult({
268
+ ruleId: item.rule.id,
269
+ observed,
270
+ checkedAt: now,
271
+ conditionState,
272
+ trigger: shouldTrigger
273
+ ? {
274
+ instrumentId: item.instrument.id,
275
+ title: `${item.instrument.symbol} alert triggered`,
276
+ message: alertTriggerMessage(item.instrument.symbol, item.rule, observation.value),
277
+ triggeredAt: now,
278
+ observedAt: now,
279
+ providerDataAt: observation.providerDataAt ?? null,
280
+ sourceProvider: observation.sourceProvider,
281
+ cacheStatus: observation.cacheStatus,
282
+ dataDelayMs: observation.dataDelayMs ?? null,
283
+ triggerSource: options.triggerType,
284
+ dedupeKey: alertDedupeKey(item.rule, observation, options.triggerType),
285
+ status: options.triggerType === "resume" ? "triggered_late" : "triggered",
286
+ }
287
+ : undefined,
288
+ });
289
+
290
+ if (result.triggered) {
291
+ triggered++;
292
+ lines.push(
293
+ `TRIGGERED: ${item.instrument.symbol} — ${alertTriggerMessage(item.instrument.symbol, item.rule, observation.value)}${observationSourceSuffix(observation)}`,
294
+ );
295
+ } else {
296
+ lines.push(
297
+ `${item.instrument.symbol}: ${previous == null ? "seeded" : "checked"} — observed ${observation.value.toFixed(2)} vs ${conditionTargetLabel(item.rule)} (condition ${conditionState})${observationSourceSuffix(observation)}`,
298
+ );
299
+ }
300
+ }
301
+
302
+ service.completeAlertCheckRun(run.id, {
303
+ completedAt: now,
304
+ status: "completed",
305
+ checkedCount: rules.length,
306
+ triggeredCount: triggered,
307
+ unavailableCount: unavailable,
308
+ providerStatus: {
309
+ checkedSymbols: runnable.map((item) => item.instrument.symbol),
310
+ unavailableReasons: Object.fromEntries(quoteObservations.unavailableReasons),
311
+ providerBudget: providerBudget.snapshot(now),
312
+ },
313
+ });
314
+
315
+ return { checked: rules.length, triggered, unavailable, runId: run.id, lines };
316
+ } catch (error) {
317
+ service.completeAlertCheckRun(run.id, {
318
+ completedAt: now,
319
+ status: "failed",
320
+ checkedCount: 0,
321
+ triggeredCount: triggered,
322
+ unavailableCount: unavailable,
323
+ error: { message: error instanceof Error ? error.message : String(error) },
324
+ });
325
+ throw error;
326
+ }
327
+ }
328
+
329
+ async function loadPriceObservations(
330
+ rules: RunnableRule[],
331
+ providers: AlertRunnerProviders,
332
+ providerBudget: AlertProviderBudget,
333
+ now: string,
334
+ ): Promise<ObservationSet> {
335
+ const symbols = [...new Set(rules.map(({ instrument }) => instrument.symbol))].sort();
336
+ const tradingViewSymbols = symbols.filter(
337
+ (symbol) =>
338
+ canUseTradingViewQuote(symbol) &&
339
+ rules.some(
340
+ ({ rule, instrument }) => instrument.symbol === symbol && allowsDelayedObservation(rule),
341
+ ),
342
+ );
343
+ const yahooSymbols = new Set(
344
+ symbols.filter(
345
+ (symbol) =>
346
+ !canUseTradingViewQuote(symbol) ||
347
+ rules.some(
348
+ ({ rule, instrument }) => instrument.symbol === symbol && !allowsDelayedObservation(rule),
349
+ ),
350
+ ),
351
+ );
352
+ const observations = new Map<string, AlertQuoteObservation>();
353
+ const unavailableReasons = new Map<string, string>();
354
+
355
+ if (tradingViewSymbols.length > 0) {
356
+ const budgetReason = providerBudget.unavailableReason("tradingview", now);
357
+ if (budgetReason) {
358
+ for (const symbol of tradingViewSymbols) {
359
+ yahooSymbols.add(symbol);
360
+ unavailableReasons.set(symbol, budgetReason);
361
+ unavailableReasons.set(quoteObservationKey(symbol, true), budgetReason);
362
+ }
363
+ } else {
364
+ try {
365
+ for (const quote of await providers.getTradingViewQuotes(tradingViewSymbols)) {
366
+ if (quote.cacheStatus === "stale") {
367
+ unavailableReasons.set(
368
+ quoteObservationKey(quote.symbol, true),
369
+ "TradingView returned stale market data",
370
+ );
371
+ continue;
372
+ }
373
+ observations.set(
374
+ quoteObservationKey(quote.symbol, true),
375
+ normalizeObservation(quote, now),
376
+ );
377
+ }
378
+ providerBudget.recordSuccess("tradingview");
379
+ } catch (error) {
380
+ const reason = error instanceof Error ? error.message : "TradingView unavailable";
381
+ providerBudget.recordFailure("tradingview", reason, now);
382
+ for (const symbol of tradingViewSymbols) {
383
+ yahooSymbols.add(symbol);
384
+ unavailableReasons.set(symbol, reason);
385
+ unavailableReasons.set(quoteObservationKey(symbol, true), reason);
386
+ }
387
+ }
388
+ }
389
+ }
390
+
391
+ for (const symbol of tradingViewSymbols) {
392
+ if (!observations.has(quoteObservationKey(symbol, true))) yahooSymbols.add(symbol);
393
+ }
394
+
395
+ const yahooSymbolList = [...yahooSymbols];
396
+ const yahooBudgetReason = providerBudget.unavailableReason("yahoo", now);
397
+ if (yahooBudgetReason) {
398
+ for (const symbol of yahooSymbolList) {
399
+ const prior = unavailableReasons.get(symbol);
400
+ unavailableReasons.set(symbol, prior ? `${prior}; ${yahooBudgetReason}` : yahooBudgetReason);
401
+ unavailableReasons.set(quoteObservationKey(symbol, false), yahooBudgetReason);
402
+ }
403
+ } else {
404
+ const yahooResults = await Promise.allSettled(
405
+ yahooSymbolList.map((symbol) => providers.getYahooQuote(symbol)),
406
+ );
407
+
408
+ for (const [index, result] of yahooResults.entries()) {
409
+ const symbol = yahooSymbolList[index];
410
+ if (symbol == null) continue;
411
+
412
+ if (result.status === "fulfilled") {
413
+ const normalized = normalizeObservation(result.value, now);
414
+ observations.set(quoteObservationKey(symbol, false), normalized);
415
+ if (!observations.has(quoteObservationKey(symbol, true))) {
416
+ observations.set(quoteObservationKey(symbol, true), normalized);
417
+ }
418
+ unavailableReasons.delete(symbol);
419
+ unavailableReasons.delete(quoteObservationKey(symbol, false));
420
+ unavailableReasons.delete(quoteObservationKey(symbol, true));
421
+ providerBudget.recordSuccess("yahoo");
422
+ } else {
423
+ const prior = unavailableReasons.get(symbol);
424
+ const reason = result.reason instanceof Error ? result.reason.message : "Yahoo unavailable";
425
+ if (isProviderWideFailure(reason)) providerBudget.recordFailure("yahoo", reason, now);
426
+ const mergedReason = prior ? `${prior}; Yahoo fallback unavailable: ${reason}` : reason;
427
+ unavailableReasons.set(symbol, mergedReason);
428
+ unavailableReasons.set(quoteObservationKey(symbol, false), reason);
429
+ if (!observations.has(quoteObservationKey(symbol, true))) {
430
+ const delayedKey = quoteObservationKey(symbol, true);
431
+ const delayedPrior = unavailableReasons.get(delayedKey);
432
+ unavailableReasons.set(
433
+ delayedKey,
434
+ delayedPrior ? `${delayedPrior}; Yahoo fallback unavailable: ${reason}` : mergedReason,
435
+ );
436
+ }
437
+ }
438
+ }
439
+ }
440
+
441
+ return { observations, unavailableReasons };
442
+ }
443
+
444
+ async function loadHistoricalObservation(
445
+ item: RunnableRule,
446
+ providers: AlertRunnerProviders,
447
+ providerBudget: AlertProviderBudget,
448
+ historyCache: Map<string, Promise<OHLCV[]>>,
449
+ now: string,
450
+ ): Promise<AlertQuoteObservation | null> {
451
+ try {
452
+ if (item.rule.conditionType === "price_crosses_sma") {
453
+ const condition = item.rule.conditionJson as { period?: unknown };
454
+ const period = typeof condition.period === "number" ? condition.period : 50;
455
+ const bars = await loadHistory(
456
+ item.instrument.symbol,
457
+ "1y",
458
+ "1d",
459
+ providers,
460
+ providerBudget,
461
+ historyCache,
462
+ now,
463
+ );
464
+ const closes = bars.map((bar) => bar.close);
465
+ const sma = computeSMA(closes, period);
466
+ const latestClose = closes.at(-1);
467
+ const latestSma = sma.at(-1);
468
+ if (latestClose == null || latestSma == null) return null;
469
+ return historicalObservation(
470
+ item.instrument.symbol,
471
+ latestClose - latestSma,
472
+ bars.at(-1)?.date ?? null,
473
+ now,
474
+ );
475
+ }
476
+
477
+ if (item.rule.conditionType === "percent_move") {
478
+ const bars = await loadHistory(
479
+ item.instrument.symbol,
480
+ "5d",
481
+ "1d",
482
+ providers,
483
+ providerBudget,
484
+ historyCache,
485
+ now,
486
+ );
487
+ const latest = bars.at(-1);
488
+ const prior = bars.at(-2);
489
+ if (latest == null || prior == null || prior.close === 0) return null;
490
+ const move = (latest.close / prior.close - 1) * 100;
491
+ return historicalObservation(
492
+ item.instrument.symbol,
493
+ roundObservation(move),
494
+ latest.date,
495
+ now,
496
+ );
497
+ }
498
+
499
+ if (item.rule.conditionType === "sma_cross") {
500
+ const condition = item.rule.conditionJson as { fast_period?: unknown; slow_period?: unknown };
501
+ const fastPeriod = typeof condition.fast_period === "number" ? condition.fast_period : 50;
502
+ const slowPeriod = typeof condition.slow_period === "number" ? condition.slow_period : 200;
503
+ const bars = await loadHistory(
504
+ item.instrument.symbol,
505
+ "2y",
506
+ "1d",
507
+ providers,
508
+ providerBudget,
509
+ historyCache,
510
+ now,
511
+ );
512
+ const closes = bars.map((bar) => bar.close);
513
+ const fast = computeSMA(closes, fastPeriod).at(-1);
514
+ const slow = computeSMA(closes, slowPeriod).at(-1);
515
+ if (fast == null || slow == null) return null;
516
+ return historicalObservation(
517
+ item.instrument.symbol,
518
+ roundObservation(fast - slow),
519
+ bars.at(-1)?.date ?? null,
520
+ now,
521
+ );
522
+ }
523
+
524
+ if (item.rule.conditionType === "rsi_threshold") {
525
+ const condition = item.rule.conditionJson as { period?: unknown };
526
+ const period = typeof condition.period === "number" ? condition.period : 14;
527
+ const bars = await loadHistory(
528
+ item.instrument.symbol,
529
+ "6mo",
530
+ "1d",
531
+ providers,
532
+ providerBudget,
533
+ historyCache,
534
+ now,
535
+ );
536
+ const rsi = computeRSI(
537
+ bars.map((bar) => bar.close),
538
+ period,
539
+ );
540
+ const latestRsi = rsi.at(-1);
541
+ if (latestRsi == null) return null;
542
+ return historicalObservation(
543
+ item.instrument.symbol,
544
+ latestRsi,
545
+ bars.at(-1)?.date ?? null,
546
+ now,
547
+ );
548
+ }
549
+
550
+ if (item.rule.conditionType === "volume_spike") {
551
+ const condition = item.rule.conditionJson as { lookback_period?: unknown };
552
+ const period = typeof condition.lookback_period === "number" ? condition.lookback_period : 20;
553
+ const bars = await loadHistory(
554
+ item.instrument.symbol,
555
+ "6mo",
556
+ "1d",
557
+ providers,
558
+ providerBudget,
559
+ historyCache,
560
+ now,
561
+ );
562
+ const latest = bars.at(-1);
563
+ const prior = bars.slice(Math.max(0, bars.length - 1 - period), bars.length - 1);
564
+ if (latest == null || prior.length < period) return null;
565
+ const averageVolume = prior.reduce((sum, bar) => sum + bar.volume, 0) / prior.length;
566
+ if (averageVolume <= 0) return null;
567
+ return historicalObservation(
568
+ item.instrument.symbol,
569
+ latest.volume / averageVolume,
570
+ latest.date,
571
+ now,
572
+ );
573
+ }
574
+ } catch {
575
+ return null;
576
+ }
577
+ return null;
578
+ }
579
+
580
+ function loadHistory(
581
+ symbol: string,
582
+ range: string,
583
+ interval: string,
584
+ providers: AlertRunnerProviders,
585
+ providerBudget: AlertProviderBudget,
586
+ historyCache: Map<string, Promise<OHLCV[]>>,
587
+ now: string,
588
+ ): Promise<OHLCV[]> {
589
+ const budgetReason = providerBudget.unavailableReason("yahoo", now);
590
+ if (budgetReason) return Promise.reject(new Error(budgetReason));
591
+ const key = `${symbol}:${range}:${interval}`;
592
+ const cached = historyCache.get(key);
593
+ if (cached) return cached;
594
+ const promise = providers
595
+ .getHistory(symbol, range, interval)
596
+ .then((bars) => {
597
+ providerBudget.recordSuccess("yahoo");
598
+ return bars;
599
+ })
600
+ .catch((error) => {
601
+ const reason = error instanceof Error ? error.message : "Yahoo history unavailable";
602
+ if (isProviderWideFailure(reason)) providerBudget.recordFailure("yahoo", reason, now);
603
+ throw error;
604
+ });
605
+ historyCache.set(key, promise);
606
+ return promise;
607
+ }
608
+
609
+ function isProviderWideFailure(reason: string): boolean {
610
+ const normalized = reason.toLowerCase();
611
+ return (
612
+ normalized.includes("429") ||
613
+ normalized.includes("rate limit") ||
614
+ normalized.includes("too many requests") ||
615
+ normalized.includes("provider_budget_exhausted") ||
616
+ normalized.includes("timeout") ||
617
+ normalized.includes("timed out") ||
618
+ normalized.includes("network") ||
619
+ normalized.includes("fetch failed") ||
620
+ normalized.includes("econn") ||
621
+ normalized.includes("enotfound")
622
+ );
623
+ }
624
+
625
+ function historicalObservation(
626
+ symbol: string,
627
+ value: number,
628
+ providerDataAt: string | null,
629
+ now: string,
630
+ ): AlertQuoteObservation {
631
+ return {
632
+ symbol,
633
+ value,
634
+ sourceProvider: "yahoo",
635
+ observedAt: now,
636
+ providerDataAt,
637
+ cacheStatus: "live",
638
+ };
639
+ }
640
+
641
+ function observationField(rule: AlertRuleRecord): string {
642
+ if (rule.conditionType === "rsi_threshold") return "rsi";
643
+ if (rule.conditionType === "volume_spike") return "volume_ratio";
644
+ if (rule.conditionType === "price_crosses_sma") return "price_sma_spread";
645
+ if (rule.conditionType === "percent_move") return "percent_move";
646
+ if (rule.conditionType === "sma_cross") return "sma_spread";
647
+ return "last_price";
648
+ }
649
+
650
+ function conditionTargetLabel(rule: AlertRuleRecord): string {
651
+ const c = (rule.conditionJson ?? {}) as Record<string, unknown>;
652
+ switch (rule.conditionType) {
653
+ case "price_crosses_above":
654
+ return `above ${Number(c.threshold).toFixed(2)}`;
655
+ case "price_crosses_below":
656
+ return `below ${Number(c.threshold).toFixed(2)}`;
657
+ case "rsi_threshold":
658
+ return `RSI ${c.direction === "below" ? "below" : "above"} ${c.threshold} (${c.period}-day)`;
659
+ case "percent_move":
660
+ return `${c.direction === "down" ? "down" : "up"} ${c.percent}% in ${c.window ?? "1d"}`;
661
+ case "price_crosses_sma":
662
+ return `price ${c.direction === "below" ? "below" : "above"} ${c.period}-day SMA`;
663
+ case "sma_cross":
664
+ return `${c.fast_period}-day SMA ${c.direction === "below" ? "below" : "above"} ${c.slow_period}-day SMA`;
665
+ case "volume_spike":
666
+ return `${c.multiplier}x the ${c.lookback_period}-day average volume`;
667
+ default:
668
+ return rule.conditionType;
669
+ }
670
+ }
671
+
672
+ function observationSourceSuffix(observation: {
673
+ sourceProvider: string;
674
+ dataDelayMs?: number | null;
675
+ }): string {
676
+ const delay =
677
+ observation.dataDelayMs != null && observation.dataDelayMs > 0
678
+ ? `, ~${Math.round(observation.dataDelayMs / 60_000)}m delayed`
679
+ : "";
680
+ return ` [${observation.sourceProvider}${delay}]`;
681
+ }
682
+
683
+ function alertTriggerMessage(symbol: string, rule: AlertRuleRecord, value: number): string {
684
+ if (rule.conditionType === "rsi_threshold") {
685
+ const direction = (rule.conditionJson as { direction?: unknown }).direction;
686
+ const directionText = direction === "above" ? "above" : "below";
687
+ return `${symbol} RSI ${directionText} threshold at ${value.toFixed(2)}`;
688
+ }
689
+ if (rule.conditionType === "volume_spike") {
690
+ return `${symbol} volume spike at ${value.toFixed(2)}x average volume`;
691
+ }
692
+ if (rule.conditionType === "price_crosses_sma") {
693
+ return `${symbol} price/SMA spread at ${formatSignedCurrency(value)}`;
694
+ }
695
+ if (rule.conditionType === "percent_move") {
696
+ const direction =
697
+ (rule.conditionJson as { direction?: unknown }).direction === "down" ? "down" : "up";
698
+ return `${symbol} percent move ${direction} ${Math.abs(value).toFixed(2)}%`;
699
+ }
700
+ if (rule.conditionType === "sma_cross") {
701
+ const direction =
702
+ (rule.conditionJson as { direction?: unknown }).direction === "below" ? "below" : "above";
703
+ return `${symbol} fast SMA crossed ${direction} slow SMA at ${formatSignedCurrency(value)}`;
704
+ }
705
+ return `${symbol} ${rule.conditionType} at $${value.toFixed(2)}`;
706
+ }
707
+
708
+ function formatSignedCurrency(value: number): string {
709
+ return `${value >= 0 ? "+" : "-"}$${Math.abs(value).toFixed(2)}`;
710
+ }
711
+
712
+ function roundObservation(value: number): number {
713
+ return Math.round(value * 10_000) / 10_000;
714
+ }
715
+
716
+ function normalizeObservation(
717
+ observation: AlertQuoteObservation,
718
+ now: string,
719
+ ): AlertQuoteObservation {
720
+ return {
721
+ ...observation,
722
+ symbol: observation.symbol.toUpperCase(),
723
+ observedAt: observation.observedAt || now,
724
+ cacheStatus: observation.cacheStatus ?? "live",
725
+ };
726
+ }
727
+
728
+ function isDue(rule: AlertRuleRecord, now: string): boolean {
729
+ return (
730
+ rule.nextCheckAt == null || new Date(rule.nextCheckAt).getTime() <= new Date(now).getTime()
731
+ );
732
+ }
733
+
734
+ function isPriceRule(rule: AlertRuleRecord): boolean {
735
+ return (
736
+ rule.conditionType === "price_crosses_above" || rule.conditionType === "price_crosses_below"
737
+ );
738
+ }
739
+
740
+ function allowsDelayedObservation(rule: AlertRuleRecord): boolean {
741
+ const condition = rule.conditionJson as { allow_delayed?: unknown; allowDelayed?: unknown };
742
+ return condition.allow_delayed !== false && condition.allowDelayed !== false;
743
+ }
744
+
745
+ function quoteObservationKey(symbol: string, delayedAllowed: boolean): string {
746
+ return `${symbol.toUpperCase()}:${delayedAllowed ? "delayed-ok" : "fresh-only"}`;
747
+ }
748
+
749
+ function lastObservedValue(rule: AlertRuleRecord): number | null {
750
+ const observed = rule.lastObservedJson as { value?: unknown } | null;
751
+ return typeof observed?.value === "number" ? observed.value : null;
752
+ }
753
+
754
+ function conditionIsTrue(rule: AlertRuleRecord, current: number): boolean {
755
+ const condition = rule.conditionJson as { threshold?: unknown };
756
+ if (rule.conditionType === "price_crosses_above") {
757
+ return typeof condition.threshold === "number" && current > condition.threshold;
758
+ }
759
+ if (rule.conditionType === "price_crosses_below") {
760
+ return typeof condition.threshold === "number" && current < condition.threshold;
761
+ }
762
+ if (rule.conditionType === "price_crosses_sma") {
763
+ const direction = (rule.conditionJson as { direction?: unknown }).direction;
764
+ if (direction === "above") return current > 0;
765
+ if (direction === "below") return current < 0;
766
+ }
767
+ if (rule.conditionType === "percent_move") {
768
+ const percent = (rule.conditionJson as { percent?: unknown }).percent;
769
+ if (typeof percent !== "number") return false;
770
+ const direction = (rule.conditionJson as { direction?: unknown }).direction;
771
+ if (direction === "up") return current > percent;
772
+ if (direction === "down") return current < -percent;
773
+ }
774
+ if (rule.conditionType === "sma_cross") {
775
+ const direction = (rule.conditionJson as { direction?: unknown }).direction;
776
+ if (direction === "above") return current > 0;
777
+ if (direction === "below") return current < 0;
778
+ }
779
+ if (rule.conditionType === "rsi_threshold") {
780
+ if (typeof condition.threshold !== "number") return false;
781
+ const direction = (rule.conditionJson as { direction?: unknown }).direction;
782
+ if (direction === "above") return current > condition.threshold;
783
+ if (direction === "below") return current < condition.threshold;
784
+ }
785
+ if (rule.conditionType === "volume_spike") {
786
+ const multiplier = (rule.conditionJson as { multiplier?: unknown }).multiplier;
787
+ return typeof multiplier === "number" && current > multiplier;
788
+ }
789
+ return false;
790
+ }
791
+
792
+ function crosses(rule: AlertRuleRecord, previous: number, current: number): boolean {
793
+ const condition = rule.conditionJson as { threshold?: unknown };
794
+ if (rule.conditionType === "price_crosses_above") {
795
+ return (
796
+ typeof condition.threshold === "number" &&
797
+ previous <= condition.threshold &&
798
+ current > condition.threshold
799
+ );
800
+ }
801
+ if (rule.conditionType === "price_crosses_below") {
802
+ return (
803
+ typeof condition.threshold === "number" &&
804
+ previous >= condition.threshold &&
805
+ current < condition.threshold
806
+ );
807
+ }
808
+ if (rule.conditionType === "price_crosses_sma") {
809
+ const direction = (rule.conditionJson as { direction?: unknown }).direction;
810
+ if (direction === "above") return previous <= 0 && current > 0;
811
+ if (direction === "below") return previous >= 0 && current < 0;
812
+ }
813
+ if (rule.conditionType === "percent_move") {
814
+ const percent = (rule.conditionJson as { percent?: unknown }).percent;
815
+ if (typeof percent !== "number") return false;
816
+ const direction = (rule.conditionJson as { direction?: unknown }).direction;
817
+ if (direction === "up") return previous <= percent && current > percent;
818
+ if (direction === "down") return previous >= -percent && current < -percent;
819
+ }
820
+ if (rule.conditionType === "sma_cross") {
821
+ const direction = (rule.conditionJson as { direction?: unknown }).direction;
822
+ if (direction === "above") return previous <= 0 && current > 0;
823
+ if (direction === "below") return previous >= 0 && current < 0;
824
+ }
825
+ if (rule.conditionType === "rsi_threshold") {
826
+ if (typeof condition.threshold !== "number") return false;
827
+ const direction = (rule.conditionJson as { direction?: unknown }).direction;
828
+ if (direction === "above")
829
+ return previous <= condition.threshold && current > condition.threshold;
830
+ if (direction === "below")
831
+ return previous >= condition.threshold && current < condition.threshold;
832
+ }
833
+ if (rule.conditionType === "volume_spike") {
834
+ const multiplier = (rule.conditionJson as { multiplier?: unknown }).multiplier;
835
+ return typeof multiplier === "number" && previous <= multiplier && current > multiplier;
836
+ }
837
+ return false;
838
+ }
839
+
840
+ function outsideCooldown(rule: AlertRuleRecord, now: string): boolean {
841
+ if (rule.lastTriggeredAt == null || rule.cooldownSeconds == null) return true;
842
+ return (
843
+ new Date(now).getTime() - new Date(rule.lastTriggeredAt).getTime() >=
844
+ rule.cooldownSeconds * 1000
845
+ );
846
+ }
847
+
848
+ function alertDedupeKey(
849
+ rule: AlertRuleRecord,
850
+ observation: AlertQuoteObservation,
851
+ triggerType: string,
852
+ ): string {
853
+ const bucket = observation.observedAt.slice(0, 16);
854
+ return [
855
+ "alert",
856
+ rule.id,
857
+ rule.ruleRevision,
858
+ rule.armCycleId,
859
+ triggerType,
860
+ bucket,
861
+ observation.value,
862
+ ].join(":");
863
+ }