opencandle 0.3.0 → 0.5.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 (408) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +106 -14
  3. package/assets/logo.svg +187 -0
  4. package/dist/cli.d.ts +1 -1
  5. package/dist/cli.js +40 -3
  6. package/dist/cli.js.map +1 -1
  7. package/dist/config.d.ts +25 -0
  8. package/dist/config.js +72 -0
  9. package/dist/config.js.map +1 -1
  10. package/dist/infra/browser.d.ts +11 -3
  11. package/dist/infra/browser.js +2 -1
  12. package/dist/infra/browser.js.map +1 -1
  13. package/dist/infra/native-dependencies.d.ts +1 -0
  14. package/dist/infra/native-dependencies.js +10 -0
  15. package/dist/infra/native-dependencies.js.map +1 -0
  16. package/dist/infra/node-version.d.ts +2 -0
  17. package/dist/infra/node-version.js +23 -0
  18. package/dist/infra/node-version.js.map +1 -0
  19. package/dist/infra/rate-limiter.d.ts +4 -0
  20. package/dist/infra/rate-limiter.js +5 -1
  21. package/dist/infra/rate-limiter.js.map +1 -1
  22. package/dist/memory/index.d.ts +2 -0
  23. package/dist/memory/index.js +1 -0
  24. package/dist/memory/index.js.map +1 -1
  25. package/dist/memory/manager.d.ts +9 -0
  26. package/dist/memory/manager.js +28 -11
  27. package/dist/memory/manager.js.map +1 -1
  28. package/dist/memory/sqlite.js +42 -4
  29. package/dist/memory/sqlite.js.map +1 -1
  30. package/dist/memory/storage.d.ts +7 -0
  31. package/dist/memory/storage.js +3 -3
  32. package/dist/memory/storage.js.map +1 -1
  33. package/dist/memory/tool-defaults.d.ts +8 -0
  34. package/dist/memory/tool-defaults.js +59 -0
  35. package/dist/memory/tool-defaults.js.map +1 -0
  36. package/dist/memory/types.js +4 -0
  37. package/dist/memory/types.js.map +1 -1
  38. package/dist/onboarding/connect.d.ts +13 -1
  39. package/dist/onboarding/connect.js +21 -10
  40. package/dist/onboarding/connect.js.map +1 -1
  41. package/dist/onboarding/prompt-user.d.ts +1 -1
  42. package/dist/onboarding/providers.d.ts +7 -0
  43. package/dist/onboarding/providers.js +6 -3
  44. package/dist/onboarding/providers.js.map +1 -1
  45. package/dist/onboarding/tool-helpers.d.ts +1 -1
  46. package/dist/pi/opencandle-extension.d.ts +7 -1
  47. package/dist/pi/opencandle-extension.js +391 -21
  48. package/dist/pi/opencandle-extension.js.map +1 -1
  49. package/dist/pi/session-storage.d.ts +2 -0
  50. package/dist/pi/session-storage.js +5 -0
  51. package/dist/pi/session-storage.js.map +1 -0
  52. package/dist/pi/session.d.ts +4 -1
  53. package/dist/pi/session.js +25 -3
  54. package/dist/pi/session.js.map +1 -1
  55. package/dist/pi/setup.d.ts +1 -1
  56. package/dist/pi/setup.js +11 -1
  57. package/dist/pi/setup.js.map +1 -1
  58. package/dist/pi/tool-adapter.d.ts +2 -2
  59. package/dist/pi/tool-adapter.js +14 -1
  60. package/dist/pi/tool-adapter.js.map +1 -1
  61. package/dist/prompts/context-builder.d.ts +40 -3
  62. package/dist/prompts/context-builder.js +140 -19
  63. package/dist/prompts/context-builder.js.map +1 -1
  64. package/dist/prompts/disclaimer.d.ts +6 -0
  65. package/dist/prompts/disclaimer.js +9 -0
  66. package/dist/prompts/disclaimer.js.map +1 -0
  67. package/dist/prompts/policy-cards.d.ts +13 -0
  68. package/dist/prompts/policy-cards.js +197 -0
  69. package/dist/prompts/policy-cards.js.map +1 -0
  70. package/dist/prompts/sections.js +3 -3
  71. package/dist/prompts/sections.js.map +1 -1
  72. package/dist/prompts/workflow-prompts.d.ts +8 -0
  73. package/dist/prompts/workflow-prompts.js +208 -22
  74. package/dist/prompts/workflow-prompts.js.map +1 -1
  75. package/dist/providers/alpha-vantage.js +23 -1
  76. package/dist/providers/alpha-vantage.js.map +1 -1
  77. package/dist/providers/sec-edgar.d.ts +8 -1
  78. package/dist/providers/sec-edgar.js +172 -5
  79. package/dist/providers/sec-edgar.js.map +1 -1
  80. package/dist/providers/yahoo-finance.d.ts +2 -0
  81. package/dist/providers/yahoo-finance.js +203 -35
  82. package/dist/providers/yahoo-finance.js.map +1 -1
  83. package/dist/routing/classify-intent.d.ts +3 -0
  84. package/dist/routing/classify-intent.js +82 -3
  85. package/dist/routing/classify-intent.js.map +1 -1
  86. package/dist/routing/defaults.js +4 -4
  87. package/dist/routing/defaults.js.map +1 -1
  88. package/dist/routing/entity-extractor.d.ts +1 -0
  89. package/dist/routing/entity-extractor.js +158 -12
  90. package/dist/routing/entity-extractor.js.map +1 -1
  91. package/dist/routing/index.d.ts +10 -0
  92. package/dist/routing/index.js +7 -0
  93. package/dist/routing/index.js.map +1 -1
  94. package/dist/routing/legacy-rule-router.d.ts +9 -0
  95. package/dist/routing/legacy-rule-router.js +12 -0
  96. package/dist/routing/legacy-rule-router.js.map +1 -0
  97. package/dist/routing/planning.d.ts +54 -0
  98. package/dist/routing/planning.js +531 -0
  99. package/dist/routing/planning.js.map +1 -0
  100. package/dist/routing/route-manifest.d.ts +35 -0
  101. package/dist/routing/route-manifest.js +221 -0
  102. package/dist/routing/route-manifest.js.map +1 -0
  103. package/dist/routing/router-llm-client.d.ts +11 -0
  104. package/dist/routing/router-llm-client.js +42 -0
  105. package/dist/routing/router-llm-client.js.map +1 -0
  106. package/dist/routing/router-prompt.d.ts +2 -0
  107. package/dist/routing/router-prompt.js +141 -0
  108. package/dist/routing/router-prompt.js.map +1 -0
  109. package/dist/routing/router-types.d.ts +71 -0
  110. package/dist/routing/router-types.js +2 -0
  111. package/dist/routing/router-types.js.map +1 -0
  112. package/dist/routing/router.d.ts +11 -0
  113. package/dist/routing/router.js +638 -0
  114. package/dist/routing/router.js.map +1 -0
  115. package/dist/routing/slot-resolver.js +46 -6
  116. package/dist/routing/slot-resolver.js.map +1 -1
  117. package/dist/routing/turn-context.d.ts +44 -0
  118. package/dist/routing/turn-context.js +45 -0
  119. package/dist/routing/turn-context.js.map +1 -0
  120. package/dist/routing/types.d.ts +13 -1
  121. package/dist/runtime/answer-contracts.d.ts +82 -0
  122. package/dist/runtime/answer-contracts.js +414 -0
  123. package/dist/runtime/answer-contracts.js.map +1 -0
  124. package/dist/runtime/artifact-contracts.d.ts +14 -0
  125. package/dist/runtime/artifact-contracts.js +57 -0
  126. package/dist/runtime/artifact-contracts.js.map +1 -0
  127. package/dist/runtime/planning-evidence.d.ts +99 -0
  128. package/dist/runtime/planning-evidence.js +445 -0
  129. package/dist/runtime/planning-evidence.js.map +1 -0
  130. package/dist/runtime/session-coordinator.d.ts +81 -3
  131. package/dist/runtime/session-coordinator.js +201 -17
  132. package/dist/runtime/session-coordinator.js.map +1 -1
  133. package/dist/runtime/tool-defaults-wrapper.d.ts +3 -0
  134. package/dist/runtime/tool-defaults-wrapper.js +25 -0
  135. package/dist/runtime/tool-defaults-wrapper.js.map +1 -0
  136. package/dist/sentiment/store.js +5 -0
  137. package/dist/sentiment/store.js.map +1 -1
  138. package/dist/system-prompt.js +23 -12
  139. package/dist/system-prompt.js.map +1 -1
  140. package/dist/tool-kit.d.ts +4 -4
  141. package/dist/tools/fundamentals/company-overview.d.ts +1 -1
  142. package/dist/tools/fundamentals/company-overview.js +1 -1
  143. package/dist/tools/fundamentals/company-overview.js.map +1 -1
  144. package/dist/tools/fundamentals/comps.d.ts +1 -1
  145. package/dist/tools/fundamentals/comps.js +1 -1
  146. package/dist/tools/fundamentals/comps.js.map +1 -1
  147. package/dist/tools/fundamentals/dcf.d.ts +1 -1
  148. package/dist/tools/fundamentals/dcf.js +1 -1
  149. package/dist/tools/fundamentals/dcf.js.map +1 -1
  150. package/dist/tools/fundamentals/earnings.d.ts +1 -1
  151. package/dist/tools/fundamentals/earnings.js +1 -1
  152. package/dist/tools/fundamentals/earnings.js.map +1 -1
  153. package/dist/tools/fundamentals/financials.d.ts +1 -1
  154. package/dist/tools/fundamentals/financials.js +1 -1
  155. package/dist/tools/fundamentals/financials.js.map +1 -1
  156. package/dist/tools/fundamentals/sec-filings.d.ts +2 -1
  157. package/dist/tools/fundamentals/sec-filings.js +19 -2
  158. package/dist/tools/fundamentals/sec-filings.js.map +1 -1
  159. package/dist/tools/index.d.ts +29 -1
  160. package/dist/tools/index.js +30 -0
  161. package/dist/tools/index.js.map +1 -1
  162. package/dist/tools/interaction/ask-user.d.ts +1 -1
  163. package/dist/tools/interaction/twitter-login.d.ts +1 -1
  164. package/dist/tools/macro/fear-greed.d.ts +1 -1
  165. package/dist/tools/macro/fear-greed.js +1 -1
  166. package/dist/tools/macro/fear-greed.js.map +1 -1
  167. package/dist/tools/macro/fred-data.d.ts +1 -1
  168. package/dist/tools/macro/fred-data.js +29 -5
  169. package/dist/tools/macro/fred-data.js.map +1 -1
  170. package/dist/tools/market/crypto-history.d.ts +1 -1
  171. package/dist/tools/market/crypto-history.js +18 -2
  172. package/dist/tools/market/crypto-history.js.map +1 -1
  173. package/dist/tools/market/crypto-price.d.ts +1 -1
  174. package/dist/tools/market/crypto-price.js +1 -1
  175. package/dist/tools/market/crypto-price.js.map +1 -1
  176. package/dist/tools/market/search-ticker.d.ts +1 -1
  177. package/dist/tools/market/search-ticker.js +1 -1
  178. package/dist/tools/market/search-ticker.js.map +1 -1
  179. package/dist/tools/market/stock-history.d.ts +1 -1
  180. package/dist/tools/market/stock-history.js +1 -1
  181. package/dist/tools/market/stock-history.js.map +1 -1
  182. package/dist/tools/market/stock-quote.d.ts +1 -1
  183. package/dist/tools/market/stock-quote.js +1 -1
  184. package/dist/tools/market/stock-quote.js.map +1 -1
  185. package/dist/tools/options/greeks.js +0 -1
  186. package/dist/tools/options/greeks.js.map +1 -1
  187. package/dist/tools/options/option-chain.d.ts +1 -1
  188. package/dist/tools/options/option-chain.js +13 -5
  189. package/dist/tools/options/option-chain.js.map +1 -1
  190. package/dist/tools/portfolio/correlation.d.ts +1 -1
  191. package/dist/tools/portfolio/correlation.js +1 -1
  192. package/dist/tools/portfolio/correlation.js.map +1 -1
  193. package/dist/tools/portfolio/holdings-overlap.d.ts +8 -0
  194. package/dist/tools/portfolio/holdings-overlap.js +105 -0
  195. package/dist/tools/portfolio/holdings-overlap.js.map +1 -0
  196. package/dist/tools/portfolio/predictions.d.ts +1 -1
  197. package/dist/tools/portfolio/predictions.js +1 -1
  198. package/dist/tools/portfolio/predictions.js.map +1 -1
  199. package/dist/tools/portfolio/risk-analysis.d.ts +1 -1
  200. package/dist/tools/portfolio/risk-analysis.js +1 -1
  201. package/dist/tools/portfolio/risk-analysis.js.map +1 -1
  202. package/dist/tools/portfolio/tracker.d.ts +1 -1
  203. package/dist/tools/portfolio/tracker.js +1 -1
  204. package/dist/tools/portfolio/tracker.js.map +1 -1
  205. package/dist/tools/portfolio/watchlist.d.ts +1 -1
  206. package/dist/tools/portfolio/watchlist.js +12 -4
  207. package/dist/tools/portfolio/watchlist.js.map +1 -1
  208. package/dist/tools/sentiment/reddit-sentiment.d.ts +1 -1
  209. package/dist/tools/sentiment/reddit-sentiment.js +1 -1
  210. package/dist/tools/sentiment/reddit-sentiment.js.map +1 -1
  211. package/dist/tools/sentiment/sentiment-summary.d.ts +1 -1
  212. package/dist/tools/sentiment/sentiment-summary.js +57 -2
  213. package/dist/tools/sentiment/sentiment-summary.js.map +1 -1
  214. package/dist/tools/sentiment/sentiment-trend.d.ts +1 -1
  215. package/dist/tools/sentiment/twitter-sentiment.d.ts +1 -1
  216. package/dist/tools/sentiment/twitter-sentiment.js +1 -1
  217. package/dist/tools/sentiment/twitter-sentiment.js.map +1 -1
  218. package/dist/tools/sentiment/web-search.d.ts +1 -1
  219. package/dist/tools/sentiment/web-search.js +32 -3
  220. package/dist/tools/sentiment/web-search.js.map +1 -1
  221. package/dist/tools/sentiment/web-sentiment.d.ts +1 -1
  222. package/dist/tools/sentiment/web-sentiment.js +1 -1
  223. package/dist/tools/sentiment/web-sentiment.js.map +1 -1
  224. package/dist/tools/technical/backtest.d.ts +3 -3
  225. package/dist/tools/technical/backtest.js +41 -27
  226. package/dist/tools/technical/backtest.js.map +1 -1
  227. package/dist/tools/technical/indicators.d.ts +1 -1
  228. package/dist/tools/technical/indicators.js +8 -4
  229. package/dist/tools/technical/indicators.js.map +1 -1
  230. package/dist/types/options.d.ts +10 -0
  231. package/dist/types/portfolio.d.ts +27 -0
  232. package/dist/workflows/compare-assets.js +38 -2
  233. package/dist/workflows/compare-assets.js.map +1 -1
  234. package/dist/workflows/options-screener.js +94 -8
  235. package/dist/workflows/options-screener.js.map +1 -1
  236. package/dist/workflows/portfolio-builder.js +9 -5
  237. package/dist/workflows/portfolio-builder.js.map +1 -1
  238. package/gui/server/ask-user-bridge.ts +82 -0
  239. package/gui/server/background-quotes.ts +31 -0
  240. package/gui/server/chat-event-adapter.ts +142 -0
  241. package/gui/server/gui-session-manager.ts +5 -0
  242. package/gui/server/invoke-tool.ts +89 -0
  243. package/gui/server/live-chat-event-adapter.ts +181 -0
  244. package/gui/server/model-setup.ts +100 -0
  245. package/gui/server/package.json +5 -0
  246. package/gui/server/projector.ts +254 -0
  247. package/gui/server/prompt-observation.ts +61 -0
  248. package/gui/server/server.ts +703 -0
  249. package/gui/server/session-actions.ts +31 -0
  250. package/gui/server/session-entry-wait.ts +81 -0
  251. package/gui/server/tool-metadata.ts +88 -0
  252. package/gui/server/websocket.ts +128 -0
  253. package/gui/server/writer-lock.ts +118 -0
  254. package/gui/shared/chat-events.ts +118 -0
  255. package/gui/shared/event-reducer.ts +186 -0
  256. package/gui/web/dist/assets/CatalogOverlay-Bmp6Knu7.js +1 -0
  257. package/gui/web/dist/assets/index-Bxt9QpLX.css +1 -0
  258. package/gui/web/dist/assets/index-CZ9DHZYy.js +67 -0
  259. package/gui/web/dist/assets/logo-CWpt6Y2a.svg +187 -0
  260. package/gui/web/dist/index.html +17 -0
  261. package/package.json +50 -18
  262. package/src/analysts/contracts.ts +189 -0
  263. package/src/analysts/orchestrator.ts +300 -0
  264. package/src/cli.ts +206 -0
  265. package/src/config.ts +245 -0
  266. package/src/index.ts +5 -0
  267. package/src/infra/browser.ts +111 -0
  268. package/src/infra/cache.ts +103 -0
  269. package/src/infra/http-client.ts +68 -0
  270. package/src/infra/index.ts +18 -0
  271. package/src/infra/native-dependencies.ts +12 -0
  272. package/src/infra/node-version.ts +24 -0
  273. package/src/infra/open-url.ts +28 -0
  274. package/src/infra/opencandle-paths.ts +64 -0
  275. package/src/infra/rate-limiter.ts +73 -0
  276. package/src/memory/index.ts +10 -0
  277. package/src/memory/manager.ts +192 -0
  278. package/src/memory/preference-extractor.ts +106 -0
  279. package/src/memory/retrieval.ts +70 -0
  280. package/src/memory/sqlite.ts +172 -0
  281. package/src/memory/storage.ts +205 -0
  282. package/src/memory/tool-defaults.ts +87 -0
  283. package/src/memory/types.ts +71 -0
  284. package/src/onboarding/connect.ts +184 -0
  285. package/src/onboarding/credential-interceptor.ts +134 -0
  286. package/src/onboarding/degradation-accumulator.ts +79 -0
  287. package/src/onboarding/prompt-user.ts +85 -0
  288. package/src/onboarding/providers.ts +315 -0
  289. package/src/onboarding/state.ts +218 -0
  290. package/src/onboarding/tool-helpers.ts +111 -0
  291. package/src/onboarding/tool-tags.ts +201 -0
  292. package/src/onboarding/validation.ts +158 -0
  293. package/src/pi/opencandle-extension.ts +955 -0
  294. package/src/pi/session-storage.ts +5 -0
  295. package/src/pi/session.ts +81 -0
  296. package/src/pi/setup.ts +381 -0
  297. package/src/pi/tool-adapter.ts +36 -0
  298. package/src/prompts/context-builder.ts +315 -0
  299. package/src/prompts/disclaimer.ts +9 -0
  300. package/src/prompts/policy-cards.ts +220 -0
  301. package/src/prompts/sections.ts +46 -0
  302. package/src/prompts/workflow-prompts.ts +433 -0
  303. package/src/providers/alpha-vantage.ts +315 -0
  304. package/src/providers/coingecko.ts +96 -0
  305. package/src/providers/exa-search.ts +373 -0
  306. package/src/providers/fear-greed.ts +45 -0
  307. package/src/providers/finnhub.ts +124 -0
  308. package/src/providers/fred.ts +83 -0
  309. package/src/providers/index.ts +9 -0
  310. package/src/providers/provider-credential-error.ts +23 -0
  311. package/src/providers/reddit.ts +151 -0
  312. package/src/providers/sec-edgar.ts +312 -0
  313. package/src/providers/twitter.ts +173 -0
  314. package/src/providers/web-search.ts +293 -0
  315. package/src/providers/with-fallback.ts +41 -0
  316. package/src/providers/wrap-provider.ts +64 -0
  317. package/src/providers/yahoo-finance.ts +534 -0
  318. package/src/routing/classify-intent.ts +285 -0
  319. package/src/routing/defaults.ts +29 -0
  320. package/src/routing/entity-extractor.ts +291 -0
  321. package/src/routing/index.ts +70 -0
  322. package/src/routing/legacy-rule-router.ts +13 -0
  323. package/src/routing/planning.ts +732 -0
  324. package/src/routing/route-manifest.ts +287 -0
  325. package/src/routing/router-llm-client.ts +51 -0
  326. package/src/routing/router-prompt.ts +163 -0
  327. package/src/routing/router-types.ts +87 -0
  328. package/src/routing/router.ts +712 -0
  329. package/src/routing/slot-resolver.ts +190 -0
  330. package/src/routing/turn-context.ts +111 -0
  331. package/src/routing/types.ts +75 -0
  332. package/src/runtime/answer-contracts.ts +633 -0
  333. package/src/runtime/artifact-contracts.ts +76 -0
  334. package/src/runtime/evidence.ts +77 -0
  335. package/src/runtime/planning-evidence.ts +591 -0
  336. package/src/runtime/prompt-step.ts +75 -0
  337. package/src/runtime/provider-tracker.ts +40 -0
  338. package/src/runtime/run-context.ts +22 -0
  339. package/src/runtime/session-coordinator.ts +472 -0
  340. package/src/runtime/tool-defaults-wrapper.ts +35 -0
  341. package/src/runtime/validation.ts +214 -0
  342. package/src/runtime/workflow-events.ts +75 -0
  343. package/src/runtime/workflow-runner.ts +188 -0
  344. package/src/runtime/workflow-types.ts +102 -0
  345. package/src/sentiment/adapters/finnhub.ts +44 -0
  346. package/src/sentiment/adapters/reddit.ts +65 -0
  347. package/src/sentiment/adapters/twitter.ts +36 -0
  348. package/src/sentiment/adapters/web.ts +44 -0
  349. package/src/sentiment/index.ts +58 -0
  350. package/src/sentiment/keywords.ts +9 -0
  351. package/src/sentiment/pipeline.ts +68 -0
  352. package/src/sentiment/scorer.ts +78 -0
  353. package/src/sentiment/store.ts +260 -0
  354. package/src/sentiment/trends.ts +90 -0
  355. package/src/sentiment/types.ts +108 -0
  356. package/src/system-prompt.ts +118 -0
  357. package/src/tool-kit.ts +68 -0
  358. package/src/tools/AGENTS.md +36 -0
  359. package/src/tools/fundamentals/company-overview.ts +54 -0
  360. package/src/tools/fundamentals/comps.ts +156 -0
  361. package/src/tools/fundamentals/dcf.ts +267 -0
  362. package/src/tools/fundamentals/earnings.ts +47 -0
  363. package/src/tools/fundamentals/financials.ts +54 -0
  364. package/src/tools/fundamentals/sec-filings.ts +84 -0
  365. package/src/tools/index.ts +91 -0
  366. package/src/tools/interaction/ask-user.ts +81 -0
  367. package/src/tools/interaction/twitter-login.ts +93 -0
  368. package/src/tools/macro/fear-greed.ts +41 -0
  369. package/src/tools/macro/fred-data.ts +80 -0
  370. package/src/tools/market/crypto-history.ts +67 -0
  371. package/src/tools/market/crypto-price.ts +53 -0
  372. package/src/tools/market/search-ticker.ts +53 -0
  373. package/src/tools/market/stock-history.ts +79 -0
  374. package/src/tools/market/stock-quote.ts +64 -0
  375. package/src/tools/options/greeks.ts +81 -0
  376. package/src/tools/options/option-chain.ts +96 -0
  377. package/src/tools/portfolio/correlation.ts +162 -0
  378. package/src/tools/portfolio/holdings-overlap.ts +123 -0
  379. package/src/tools/portfolio/predictions.ts +253 -0
  380. package/src/tools/portfolio/risk-analysis.ts +134 -0
  381. package/src/tools/portfolio/tracker.ts +147 -0
  382. package/src/tools/portfolio/watchlist.ts +159 -0
  383. package/src/tools/sentiment/reddit-sentiment.ts +164 -0
  384. package/src/tools/sentiment/sentiment-summary.ts +316 -0
  385. package/src/tools/sentiment/sentiment-trend.ts +58 -0
  386. package/src/tools/sentiment/twitter-sentiment.ts +96 -0
  387. package/src/tools/sentiment/web-search.ts +183 -0
  388. package/src/tools/sentiment/web-sentiment.ts +76 -0
  389. package/src/tools/technical/backtest.ts +267 -0
  390. package/src/tools/technical/indicators.ts +256 -0
  391. package/src/types/fundamentals.ts +46 -0
  392. package/src/types/index.ts +20 -0
  393. package/src/types/macro.ts +27 -0
  394. package/src/types/market.ts +43 -0
  395. package/src/types/options.ts +52 -0
  396. package/src/types/portfolio.ts +73 -0
  397. package/src/types/sentiment.ts +70 -0
  398. package/src/workflows/compare-assets.ts +75 -0
  399. package/src/workflows/index.ts +4 -0
  400. package/src/workflows/options-screener.ts +127 -0
  401. package/src/workflows/portfolio-builder.ts +56 -0
  402. package/src/workflows/types.ts +4 -0
  403. package/dist/runtime/index.d.ts +0 -16
  404. package/dist/runtime/index.js +0 -10
  405. package/dist/runtime/index.js.map +0 -1
  406. package/dist/runtime/provider-ids.d.ts +0 -14
  407. package/dist/runtime/provider-ids.js +0 -14
  408. package/dist/runtime/provider-ids.js.map +0 -1
@@ -0,0 +1,712 @@
1
+ import { extractEntities, isAmbiguousConceptUsage } from "./entity-extractor.js";
2
+ import { classifyWithLegacyRules } from "./legacy-rule-router.js";
3
+ import { buildRouterPrompt } from "./router-prompt.js";
4
+ import {
5
+ computeMissingRequiredSlots,
6
+ isDispatchableWorkflow,
7
+ isRouteKind,
8
+ isToolBundleName,
9
+ legacyRouteForRouteKind,
10
+ routeKindFromLegacyRoute,
11
+ selectToolBundles,
12
+ } from "./route-manifest.js";
13
+ import type {
14
+ RouterDiagnostic,
15
+ RouterInputContext,
16
+ RouterLlmClient,
17
+ RouterOutput,
18
+ RouterPreferenceUpdate,
19
+ RouterRoute,
20
+ RouterRouteKind,
21
+ RouterSlot,
22
+ ToolBundleName,
23
+ } from "./router-types.js";
24
+ import type { ExtractedEntities, WorkflowType } from "./types.js";
25
+
26
+ const VALID_ROUTES: readonly RouterRoute[] = ["workflow", "fallback"];
27
+ const VALID_WORKFLOWS: ReadonlyArray<Exclude<WorkflowType, "unclassified">> = [
28
+ "portfolio_builder",
29
+ "options_screener",
30
+ "compare_assets",
31
+ "single_asset_analysis",
32
+ "watchlist_or_tracking",
33
+ "general_finance_qa",
34
+ ];
35
+ const VALID_SOURCES = new Set(["user", "preference", "default", "prior_context", "memory"]);
36
+ const VALID_CONFIDENCE = new Set(["high", "medium", "low"]);
37
+
38
+ /**
39
+ * Run the LLM router against the given input context. Retries once on
40
+ * validation failure with a corrective message. Falls back to a minimal
41
+ * `route: "fallback"` output on persistent failure.
42
+ *
43
+ * The LLM client is injected so unit tests can supply deterministic responses.
44
+ */
45
+ export async function route(
46
+ input: RouterInputContext,
47
+ client: RouterLlmClient,
48
+ ): Promise<RouterOutput> {
49
+ const prompt = buildRouterPrompt(input);
50
+
51
+ let firstError: string | undefined;
52
+ try {
53
+ const raw = await client.complete(prompt);
54
+ return postProcessRouterOutput(input.text, validateRouterOutput(raw));
55
+ } catch (err) {
56
+ firstError = err instanceof Error ? err.message : String(err);
57
+ }
58
+
59
+ // Retry once with error feedback.
60
+ try {
61
+ const retryPrompt = `${prompt}\n\n(Your previous response failed validation: ${firstError}. Return a valid JSON object conforming to RouterOutput. Nothing else.)`;
62
+ const raw = await client.complete(retryPrompt);
63
+ return postProcessRouterOutput(input.text, validateRouterOutput(raw));
64
+ } catch {
65
+ // Persistent failure — return a minimal fallback with regex-extracted symbols.
66
+ return postProcessRouterOutput(input.text, minimalFallback(input.text));
67
+ }
68
+ }
69
+
70
+ export function validateRouterOutput(raw: string): RouterOutput {
71
+ const parsed = parseJsonPayload(raw);
72
+
73
+ if (!parsed || typeof parsed !== "object") {
74
+ throw new Error("router output was not a JSON object");
75
+ }
76
+ const obj = parsed as Record<string, unknown>;
77
+
78
+ const rawMissingRequired = validateStringArray(obj.missing_required, "missing_required");
79
+
80
+ const explicitRouteKind = obj.routeKind;
81
+ if (
82
+ explicitRouteKind !== undefined &&
83
+ (typeof explicitRouteKind !== "string" || !isRouteKind(explicitRouteKind))
84
+ ) {
85
+ throw new Error(`invalid routeKind: ${JSON.stringify(explicitRouteKind)}`);
86
+ }
87
+
88
+ const rawRoute = obj.route;
89
+ let route: RouterRoute;
90
+ if (typeof rawRoute === "string") {
91
+ if (!VALID_ROUTES.includes(rawRoute as RouterRoute)) {
92
+ throw new Error(`invalid route: ${JSON.stringify(rawRoute)}`);
93
+ }
94
+ route = rawRoute as RouterRoute;
95
+ } else if (typeof explicitRouteKind === "string" && isRouteKind(explicitRouteKind)) {
96
+ route = legacyRouteForRouteKind(explicitRouteKind);
97
+ } else {
98
+ throw new Error(`invalid route: ${JSON.stringify(rawRoute)}`);
99
+ }
100
+
101
+ let workflow: RouterOutput["workflow"];
102
+ const routeKind: RouterRouteKind =
103
+ typeof explicitRouteKind === "string" && isRouteKind(explicitRouteKind)
104
+ ? explicitRouteKind
105
+ : routeKindFromLegacyRoute(route, rawMissingRequired);
106
+
107
+ if (route === "workflow" || routeKind === "workflow_dispatch") {
108
+ if (typeof obj.workflow !== "string" || !VALID_WORKFLOWS.includes(obj.workflow as Exclude<WorkflowType, "unclassified">)) {
109
+ throw new Error(`workflow route requires a valid workflow; got ${JSON.stringify(obj.workflow)}`);
110
+ }
111
+ workflow = obj.workflow as Exclude<WorkflowType, "unclassified">;
112
+ } else if (typeof obj.workflow === "string" && VALID_WORKFLOWS.includes(obj.workflow as Exclude<WorkflowType, "unclassified">)) {
113
+ workflow = obj.workflow as Exclude<WorkflowType, "unclassified">;
114
+ }
115
+
116
+ const entities = validateEntities(obj.entities);
117
+ const slots = validateSlots(obj.slots);
118
+ const preference_updates = validatePreferenceUpdates(obj.preference_updates);
119
+ const missing_required = rawMissingRequired;
120
+ const tool_bundles = validateToolBundles(obj.tool_bundles);
121
+ const diagnostics = validateDiagnostics(obj.diagnostics);
122
+ const reasoning =
123
+ typeof obj.reasoning === "string" ? obj.reasoning : "";
124
+
125
+ return {
126
+ routeKind,
127
+ route: legacyRouteForRouteKind(routeKind),
128
+ workflow,
129
+ entities,
130
+ slots,
131
+ preference_updates,
132
+ missing_required,
133
+ tool_bundles,
134
+ diagnostics,
135
+ reasoning,
136
+ };
137
+ }
138
+
139
+ function parseJsonPayload(raw: string): unknown {
140
+ const trimmed = raw.trim();
141
+ // Tolerate ```json ... ``` fences even though the prompt forbids them.
142
+ const stripped = trimmed
143
+ .replace(/^```(?:json)?\s*/i, "")
144
+ .replace(/\s*```$/i, "")
145
+ .trim();
146
+ try {
147
+ return JSON.parse(stripped);
148
+ } catch (err) {
149
+ const msg = err instanceof Error ? err.message : String(err);
150
+ throw new Error(`router output was not valid JSON: ${msg}`);
151
+ }
152
+ }
153
+
154
+ function validateEntities(raw: unknown): ExtractedEntities {
155
+ if (!raw || typeof raw !== "object") {
156
+ throw new Error("entities must be an object");
157
+ }
158
+ const e = raw as Record<string, unknown>;
159
+ const symbols = validateStringArray(e.symbols, "entities.symbols").map((s) =>
160
+ s.toUpperCase(),
161
+ );
162
+
163
+ const out: ExtractedEntities = { symbols };
164
+ if (typeof e.budget === "number") out.budget = e.budget;
165
+ if (typeof e.maxPremium === "number") out.maxPremium = e.maxPremium;
166
+ if (typeof e.costBasis === "number") out.costBasis = e.costBasis;
167
+ if (typeof e.shareQuantity === "number") out.shareQuantity = e.shareQuantity;
168
+ if (typeof e.timeHorizon === "string") out.timeHorizon = e.timeHorizon;
169
+ if (typeof e.riskProfile === "string") out.riskProfile = e.riskProfile;
170
+ if (e.direction === "bullish" || e.direction === "bearish") out.direction = e.direction;
171
+ if (typeof e.dteHint === "string") out.dteHint = e.dteHint;
172
+ if (e.optionStrategy === "covered_call" || e.optionStrategy === "protective_put") out.optionStrategy = e.optionStrategy;
173
+ if (typeof e.heldSymbol === "string") out.heldSymbol = e.heldSymbol.toUpperCase();
174
+ const catalystSymbols = validateStringArray(e.catalystSymbols, "entities.catalystSymbols").map((s) =>
175
+ s.toUpperCase(),
176
+ );
177
+ if (catalystSymbols.length > 0) out.catalystSymbols = catalystSymbols;
178
+ const compareMetrics = validateStringArray(e.compareMetrics, "entities.compareMetrics");
179
+ if (compareMetrics.length > 0) out.compareMetrics = compareMetrics;
180
+ return out;
181
+ }
182
+
183
+ export function postProcessRouterOutput(text: string, output: RouterOutput): RouterOutput {
184
+ const extracted = extractEntities(text);
185
+ const deterministic = classifyWithLegacyRules(text);
186
+ let diagnostics: RouterDiagnostic[] = [...output.diagnostics];
187
+ let next: RouterOutput = {
188
+ ...output,
189
+ entities: {
190
+ ...output.entities,
191
+ symbols: output.entities.symbols.filter((symbol) =>
192
+ !isAmbiguousConceptUsage(text, symbol),
193
+ ),
194
+ budget: output.entities.budget ?? extracted.budget,
195
+ maxPremium: output.entities.maxPremium ?? extracted.maxPremium,
196
+ timeHorizon: output.entities.timeHorizon ?? extracted.timeHorizon,
197
+ riskProfile: output.entities.riskProfile ?? extracted.riskProfile,
198
+ assetScope: output.entities.assetScope ?? extracted.assetScope,
199
+ compareMetrics: mergeStringArrays(output.entities.compareMetrics, extracted.compareMetrics),
200
+ direction: output.entities.direction ?? extracted.direction,
201
+ optionStrategy: output.entities.optionStrategy ?? extracted.optionStrategy,
202
+ costBasis: output.entities.costBasis ?? extracted.costBasis,
203
+ shareQuantity: output.entities.shareQuantity ?? extracted.shareQuantity,
204
+ heldSymbol: output.entities.heldSymbol ?? extracted.heldSymbol,
205
+ catalystSymbols: output.entities.catalystSymbols ?? extracted.catalystSymbols,
206
+ dteHint: output.entities.dteHint ?? (output.workflow === "options_screener" ? extracted.dteHint : undefined),
207
+ },
208
+ diagnostics,
209
+ };
210
+
211
+ if (next.workflow === "options_screener" && isExistingPositionOptionRequest(text, extracted) && extracted.heldSymbol) {
212
+ const reorderedSymbols = [
213
+ extracted.heldSymbol,
214
+ ...mergeSymbols(next.entities.symbols, extracted.symbols).filter((symbol) => symbol !== extracted.heldSymbol),
215
+ ];
216
+ if (next.entities.symbols[0] !== extracted.heldSymbol) {
217
+ diagnostics.push({
218
+ code: extracted.optionStrategy === "protective_put"
219
+ ? "existing_position_underlying_corrected"
220
+ : "covered_call_underlying_corrected",
221
+ message: `using owned position ${extracted.heldSymbol} as the option-chain underlying`,
222
+ });
223
+ }
224
+ next = {
225
+ ...next,
226
+ entities: {
227
+ ...next.entities,
228
+ symbols: reorderedSymbols,
229
+ optionStrategy: extracted.optionStrategy ?? next.entities.optionStrategy,
230
+ direction: extracted.direction ?? next.entities.direction,
231
+ heldSymbol: extracted.heldSymbol,
232
+ catalystSymbols: reorderedSymbols.filter((symbol) => symbol !== extracted.heldSymbol),
233
+ costBasis: extracted.costBasis ?? next.entities.costBasis,
234
+ shareQuantity: extracted.shareQuantity ?? next.entities.shareQuantity,
235
+ dteHint: extracted.dteHint ?? next.entities.dteHint,
236
+ },
237
+ diagnostics,
238
+ };
239
+ }
240
+
241
+ if (
242
+ next.workflow === "options_screener" &&
243
+ isOptionsEducationOrSuitabilityRequest(text) &&
244
+ !isSpecificOptionContractSelectionRequest(text)
245
+ ) {
246
+ diagnostics.push({
247
+ code: "options_workflow_corrected_to_policy_task",
248
+ message: "options education or suitability prompt should use policy-card synthesis, not contract-screen workflow dispatch",
249
+ });
250
+ next = {
251
+ ...next,
252
+ routeKind: "agent_task",
253
+ route: "fallback",
254
+ workflow: "general_finance_qa",
255
+ missing_required: [],
256
+ diagnostics,
257
+ };
258
+ }
259
+
260
+ // Legacy rules may recover a primary route only when the LLM router path has
261
+ // already failed validation. Otherwise they are limited to enrichment and
262
+ // narrow corrections below.
263
+ if (
264
+ next.diagnostics.some((d) => d.code === "router_validation_failed") &&
265
+ deterministic.workflow !== "unclassified"
266
+ ) {
267
+ next = {
268
+ ...next,
269
+ routeKind: isDispatchableWorkflow(deterministic.workflow)
270
+ ? "workflow_dispatch"
271
+ : "agent_task",
272
+ route: isDispatchableWorkflow(deterministic.workflow) ? "workflow" : "fallback",
273
+ workflow: deterministic.workflow,
274
+ entities: {
275
+ ...deterministic.entities,
276
+ budget: deterministic.entities.budget ?? extracted.budget,
277
+ maxPremium: deterministic.entities.maxPremium ?? extracted.maxPremium,
278
+ timeHorizon: deterministic.entities.timeHorizon ?? extracted.timeHorizon,
279
+ riskProfile: deterministic.entities.riskProfile ?? extracted.riskProfile,
280
+ assetScope: deterministic.entities.assetScope ?? extracted.assetScope,
281
+ compareMetrics: mergeStringArrays(deterministic.entities.compareMetrics, extracted.compareMetrics),
282
+ direction: deterministic.entities.direction ?? extracted.direction,
283
+ costBasis: deterministic.entities.costBasis ?? extracted.costBasis,
284
+ shareQuantity: deterministic.entities.shareQuantity ?? extracted.shareQuantity,
285
+ heldSymbol: deterministic.entities.heldSymbol ?? extracted.heldSymbol,
286
+ catalystSymbols: deterministic.entities.catalystSymbols ?? extracted.catalystSymbols,
287
+ },
288
+ diagnostics: [
289
+ ...diagnostics,
290
+ {
291
+ code: "deterministic_failure_recovery",
292
+ message: `deterministic classifier selected ${deterministic.workflow} after router validation failure`,
293
+ },
294
+ ],
295
+ reasoning: next.reasoning
296
+ ? `${next.reasoning}; deterministic classifier selected ${deterministic.workflow}`
297
+ : `deterministic classifier selected ${deterministic.workflow}`,
298
+ };
299
+ diagnostics = next.diagnostics;
300
+ }
301
+
302
+ if (next.routeKind === "workflow_dispatch" && !isDispatchableWorkflow(next.workflow)) {
303
+ diagnostics.push({
304
+ code: "route_kind_corrected_to_agent_task",
305
+ message: next.workflow
306
+ ? `${next.workflow} is not a dispatchable workflow`
307
+ : "workflow_dispatch requires a dispatchable workflow",
308
+ });
309
+ next = {
310
+ ...next,
311
+ routeKind: "agent_task",
312
+ route: "fallback",
313
+ diagnostics,
314
+ };
315
+ }
316
+
317
+ if (next.routeKind === "agent_task" && isDispatchableWorkflow(next.workflow)) {
318
+ diagnostics.push({
319
+ code: "dispatchable_workflow_corrected_to_workflow_dispatch",
320
+ message: `${next.workflow} is a dispatchable workflow`,
321
+ });
322
+ next = {
323
+ ...next,
324
+ routeKind: "workflow_dispatch",
325
+ route: "workflow",
326
+ diagnostics,
327
+ };
328
+ }
329
+
330
+ if (
331
+ next.workflow === "compare_assets" &&
332
+ next.entities.symbols.length === 0 &&
333
+ isExplicitMacroDataRequest(text)
334
+ ) {
335
+ diagnostics.push({
336
+ code: "compare_route_corrected_to_macro_task",
337
+ message: "macro/source acronyms were not explicit tickers",
338
+ });
339
+ next = {
340
+ ...next,
341
+ routeKind: "agent_task",
342
+ route: "fallback",
343
+ workflow: "general_finance_qa",
344
+ missing_required: [],
345
+ diagnostics,
346
+ };
347
+ }
348
+
349
+ if (next.workflow === "compare_assets" && isPortfolioEvaluationRequest(text)) {
350
+ diagnostics.push({
351
+ code: "portfolio_evaluation_corrected_to_agent_task",
352
+ message: "existing portfolio/allocation risk review should not be reduced to asset comparison",
353
+ });
354
+ next = {
355
+ ...next,
356
+ routeKind: "agent_task",
357
+ route: "fallback",
358
+ workflow: "general_finance_qa",
359
+ missing_required: [],
360
+ diagnostics,
361
+ };
362
+ }
363
+
364
+ if (
365
+ next.routeKind === "agent_task" &&
366
+ !next.workflow &&
367
+ next.entities.symbols.length === 0 &&
368
+ isExplicitMacroDataRequest(text)
369
+ ) {
370
+ diagnostics.push({
371
+ code: "macro_task_inferred_from_prompt",
372
+ message: "macro data terms were present without explicit tickers",
373
+ });
374
+ next = {
375
+ ...next,
376
+ workflow: "general_finance_qa",
377
+ diagnostics,
378
+ };
379
+ }
380
+
381
+ if (next.workflow === "portfolio_builder" && isCryptoSizingRequest(text)) {
382
+ diagnostics.push({
383
+ code: "crypto_sizing_corrected_to_agent_task",
384
+ message: "crypto allocation-range and drawdown questions are advisory tradeoffs, not portfolio construction",
385
+ });
386
+ next = {
387
+ ...next,
388
+ routeKind: "agent_task",
389
+ route: "fallback",
390
+ workflow: "general_finance_qa",
391
+ missing_required: [],
392
+ diagnostics,
393
+ };
394
+ }
395
+
396
+ if (next.workflow === "portfolio_builder" && isPortfolioEvaluationRequest(text)) {
397
+ diagnostics.push({
398
+ code: "portfolio_evaluation_corrected_to_agent_task",
399
+ message: "existing portfolio/allocation evaluation does not require portfolio-construction budget",
400
+ });
401
+ next = {
402
+ ...next,
403
+ routeKind: "agent_task",
404
+ route: "fallback",
405
+ workflow: "general_finance_qa",
406
+ missing_required: [],
407
+ diagnostics,
408
+ };
409
+ }
410
+
411
+ if (
412
+ next.workflow === "portfolio_builder" &&
413
+ next.entities.symbols.length >= 2 &&
414
+ isPortfolioTradeoffComparisonRequest(text)
415
+ ) {
416
+ diagnostics.push({
417
+ code: "portfolio_tradeoff_corrected_to_compare_assets",
418
+ message: "explicit multi-asset tradeoff question should compare the requested assets before constructing a portfolio",
419
+ });
420
+ next = {
421
+ ...next,
422
+ routeKind: "workflow_dispatch",
423
+ route: "workflow",
424
+ workflow: "compare_assets",
425
+ missing_required: [],
426
+ diagnostics,
427
+ };
428
+ }
429
+
430
+ if (
431
+ next.workflow === "single_asset_analysis" &&
432
+ isSpecializedSingleAssetPolicyRequest(text)
433
+ ) {
434
+ diagnostics.push({
435
+ code: "single_asset_workflow_corrected_to_general_policy_task",
436
+ message: "prompt asks for policy-card planning outside a single-asset buy/sell analysis",
437
+ });
438
+ next = {
439
+ ...next,
440
+ workflow: "general_finance_qa",
441
+ diagnostics,
442
+ };
443
+ }
444
+
445
+ const missingRequired = computeMissingRequiredSlots(
446
+ next.workflow,
447
+ next.entities,
448
+ next.slots,
449
+ next.missing_required,
450
+ );
451
+ if (missingRequired.length > 0 && next.routeKind !== "pass_through") {
452
+ if (next.routeKind !== "clarification") {
453
+ diagnostics.push({
454
+ code: "route_kind_corrected_to_clarification",
455
+ message: `missing required slots: ${missingRequired.join(", ")}`,
456
+ });
457
+ }
458
+ next = {
459
+ ...next,
460
+ routeKind: "clarification",
461
+ route: "fallback",
462
+ missing_required: missingRequired,
463
+ diagnostics,
464
+ };
465
+ }
466
+
467
+ const selectedToolBundles = isConceptualEducationRequest(text, next)
468
+ ? []
469
+ : selectToolBundles(next);
470
+ if (selectedToolBundles.length === 0 && isConceptualEducationRequest(text, next)) {
471
+ diagnostics.push({
472
+ code: "conceptual_education_no_tools",
473
+ message: "conceptual education prompt does not need live finance tools",
474
+ });
475
+ }
476
+ const emittedUnsupported = next.tool_bundles.filter((bundle) => !selectedToolBundles.includes(bundle));
477
+ if (emittedUnsupported.length > 0) {
478
+ diagnostics.push({
479
+ code: "tool_bundles_corrected",
480
+ message: `unsupported emitted bundles dropped: ${emittedUnsupported.join(", ")}`,
481
+ });
482
+ }
483
+
484
+ return omitUndefined({
485
+ ...next,
486
+ route: legacyRouteForRouteKind(next.routeKind),
487
+ tool_bundles: selectedToolBundles,
488
+ diagnostics,
489
+ });
490
+ }
491
+
492
+ function isExplicitMacroDataRequest(text: string): boolean {
493
+ return /\b(?:get_economic_data|fred|cpi|inflation|fed\s+funds?|unemployment|gdp|macro)\b/i.test(text);
494
+ }
495
+
496
+ function isConceptualEducationRequest(text: string, output: RouterOutput): boolean {
497
+ if (output.routeKind !== "agent_task") return false;
498
+ if (output.entities.symbols.length > 0) return false;
499
+ if (isForwardLookingMacroContextRequest(text)) return false;
500
+ if (/\b(?:current|recent|today|right now|latest|news|sentiment|build|portfolio|buy|sell|allocate|compare)\b/i.test(text)) {
501
+ return false;
502
+ }
503
+ return /\b(?:explain|what is|define|how (?:do|should|to)|teach me|help me understand)\b/i.test(text);
504
+ }
505
+
506
+ function isForwardLookingMacroContextRequest(text: string): boolean {
507
+ return /\b(?:rates?|rate\s*cuts?|fed|inflation|macro)\b/i.test(text) &&
508
+ /\b(?:next\s+(?:year|12\s*months?)|over\s+the\s+next|outlook|affect|impact|falling|rising)\b/i.test(text);
509
+ }
510
+
511
+ function isCoveredCallRequest(text: string): boolean {
512
+ return /\bcovered\s+calls?\b/i.test(text);
513
+ }
514
+
515
+ function isPortfolioEvaluationRequest(text: string): boolean {
516
+ const lower = text.toLowerCase();
517
+ const hasEvaluationIntent =
518
+ /\b(?:evaluat(?:e|ion)|review|assess|analy[sz]e|prospects?|risks?|risky|opportunities?|mitigat(?:e|ion)|adjustment|rebalance|diversify|concentration|overweight|underweight|target\s+bands?|drift|worried|crash|protect|protection|missing\s+out\s+on\s+growth)\b/.test(lower);
519
+ const hasPortfolioObject =
520
+ /\b(?:portfolio|allocation|asset\s+allocation|60\/40|equity|fixed\s+income|bonds?)\b/.test(lower);
521
+ const hasConstructionIntent =
522
+ /\b(?:build|create|construct|put\s+together|invest|allocate)\b/.test(lower) &&
523
+ (/\$\s*\d|\b\d+(?:\.\d+)?\s*k\b|\bbudget\b|\bcapital\b/.test(lower));
524
+ return hasEvaluationIntent && hasPortfolioObject && !hasConstructionIntent;
525
+ }
526
+
527
+ function isPortfolioTradeoffComparisonRequest(text: string): boolean {
528
+ const lower = text.toLowerCase();
529
+ return /\b(?:prioritize|tradeoffs?|growth[-\s]?oriented|dividend|income|which\s+(?:one|is)\s+better|should\s+i)\b/.test(lower) &&
530
+ /\b(?:or|vs\.?|versus|compare)\b/.test(lower);
531
+ }
532
+
533
+ function isCryptoSizingRequest(text: string): boolean {
534
+ const lower = text.toLowerCase();
535
+ const hasPortfolioConstructionIntent =
536
+ /\b(?:build|create|construct|put\s+together)\b/.test(lower) &&
537
+ /\b(?:portfolio|allocation)\b/.test(lower);
538
+ if (hasPortfolioConstructionIntent) return false;
539
+ return /\b(?:btc|bitcoin|crypto)\b/.test(lower) &&
540
+ /\b(?:allocation|range|position\s+size|sizing|exposure|drawdown)\b/.test(lower);
541
+ }
542
+
543
+ function isSpecializedSingleAssetPolicyRequest(text: string): boolean {
544
+ const lower = text.toLowerCase();
545
+ return /\b(?:ticker|symbol|formerly|old ticker|earnings are|earnings tonight)\b/.test(lower) ||
546
+ /\b(?:today|right now|this morning|after close|moved|catalyst)\b/.test(lower) ||
547
+ /\b(?:sentiment|mood|reddit|twitter|x\/twitter)\b/.test(lower) ||
548
+ /\b(?:filing|10-k|10-q|8-k|sec)\b/.test(lower);
549
+ }
550
+
551
+ function isExistingPositionOptionRequest(text: string, extracted: ExtractedEntities): boolean {
552
+ return isCoveredCallRequest(text) || extracted.optionStrategy === "protective_put";
553
+ }
554
+
555
+ function isOptionsEducationOrSuitabilityRequest(text: string): boolean {
556
+ const lower = text.toLowerCase();
557
+ return /\b(?:how\s+does|how\s+do|explain|what\s+is|good\s+idea|make\s+sense|suitable|suitability|is\s+it\s+(?:good|worth|smart))\b/.test(lower) &&
558
+ /\b(?:covered\s+calls?|protective\s+puts?|options?|selling\s+calls?|option\s+income)\b/.test(lower);
559
+ }
560
+
561
+ function isSpecificOptionContractSelectionRequest(text: string): boolean {
562
+ const lower = text.toLowerCase();
563
+ return /\b(?:best|which|what\s+(?:strike|contract|option)|rank|screen|specific|right\s+now|today|around\s+earnings|expiration|dte|premium\s+under)\b/.test(lower) &&
564
+ /\b(?:sell|buy|trade|contract|strike|expiration|premium|call|put)\b/.test(lower);
565
+ }
566
+
567
+ function mergeSymbols(primary: string[], secondary: string[]): string[] {
568
+ const merged: string[] = [];
569
+ for (const symbol of [...primary, ...secondary]) {
570
+ if (!merged.includes(symbol)) merged.push(symbol);
571
+ }
572
+ return merged;
573
+ }
574
+
575
+ function mergeStringArrays(primary?: string[], secondary?: string[]): string[] | undefined {
576
+ const merged: string[] = [];
577
+ for (const value of [...(primary ?? []), ...(secondary ?? [])]) {
578
+ if (!merged.includes(value)) merged.push(value);
579
+ }
580
+ return merged.length > 0 ? merged : undefined;
581
+ }
582
+
583
+ function omitUndefined<T>(value: T): T {
584
+ if (Array.isArray(value)) return value.map(omitUndefined) as T;
585
+ if (!value || typeof value !== "object") return value;
586
+ const out: Record<string, unknown> = {};
587
+ for (const [key, entry] of Object.entries(value)) {
588
+ if (entry !== undefined) out[key] = omitUndefined(entry);
589
+ }
590
+ return out as T;
591
+ }
592
+
593
+ function validateSlots(raw: unknown): Record<string, RouterSlot> {
594
+ if (raw === undefined || raw === null) return {};
595
+ if (typeof raw !== "object") {
596
+ throw new Error("slots must be an object");
597
+ }
598
+ const out: Record<string, RouterSlot> = {};
599
+ for (const [key, val] of Object.entries(raw as Record<string, unknown>)) {
600
+ if (!val || typeof val !== "object") {
601
+ throw new Error(`slot ${key} must be an object`);
602
+ }
603
+ const s = val as Record<string, unknown>;
604
+ if (!VALID_SOURCES.has(s.source as string)) {
605
+ throw new Error(`slot ${key} has invalid source: ${JSON.stringify(s.source)}`);
606
+ }
607
+ if (!VALID_CONFIDENCE.has(s.confidence as string)) {
608
+ throw new Error(`slot ${key} has invalid confidence: ${JSON.stringify(s.confidence)}`);
609
+ }
610
+ out[key] = {
611
+ value: s.value,
612
+ source: s.source as RouterSlot["source"],
613
+ confidence: s.confidence as RouterSlot["confidence"],
614
+ };
615
+ }
616
+ return out;
617
+ }
618
+
619
+ function validatePreferenceUpdates(raw: unknown): RouterPreferenceUpdate[] {
620
+ if (raw === undefined || raw === null) return [];
621
+ if (!Array.isArray(raw)) {
622
+ throw new Error("preference_updates must be an array");
623
+ }
624
+ return raw.map((item, idx) => {
625
+ if (!item || typeof item !== "object") {
626
+ throw new Error(`preference_updates[${idx}] must be an object`);
627
+ }
628
+ const p = item as Record<string, unknown>;
629
+ if (typeof p.key !== "string" || p.key.length === 0) {
630
+ throw new Error(`preference_updates[${idx}].key must be a non-empty string`);
631
+ }
632
+ if (typeof p.value !== "string") {
633
+ throw new Error(`preference_updates[${idx}].value must be a string`);
634
+ }
635
+ if (!VALID_CONFIDENCE.has(p.confidence as string)) {
636
+ throw new Error(`preference_updates[${idx}].confidence is invalid`);
637
+ }
638
+ // Router-emitted preferences are always inferred — absent is accepted
639
+ // (normalized), but any explicit non-"inferred" value is an invariant
640
+ // violation the caller should see rather than silently lose.
641
+ if (p.source !== undefined && p.source !== "inferred") {
642
+ throw new Error(`preference_updates[${idx}].source must be "inferred" (got ${JSON.stringify(p.source)})`);
643
+ }
644
+ return {
645
+ key: p.key,
646
+ value: p.value,
647
+ confidence: p.confidence as RouterPreferenceUpdate["confidence"],
648
+ source: "inferred",
649
+ };
650
+ });
651
+ }
652
+
653
+ function validateToolBundles(raw: unknown): ToolBundleName[] {
654
+ const bundles = validateStringArray(raw, "tool_bundles");
655
+ return bundles.filter(isToolBundleName);
656
+ }
657
+
658
+ function validateDiagnostics(raw: unknown): RouterDiagnostic[] {
659
+ if (raw === undefined || raw === null) return [];
660
+ if (!Array.isArray(raw)) {
661
+ throw new Error("diagnostics must be an array");
662
+ }
663
+ return raw.map((item, idx) => {
664
+ if (!item || typeof item !== "object") {
665
+ throw new Error(`diagnostics[${idx}] must be an object`);
666
+ }
667
+ const diagnostic = item as Record<string, unknown>;
668
+ if (typeof diagnostic.code !== "string" || diagnostic.code.length === 0) {
669
+ throw new Error(`diagnostics[${idx}].code must be a non-empty string`);
670
+ }
671
+ if (typeof diagnostic.message !== "string") {
672
+ throw new Error(`diagnostics[${idx}].message must be a string`);
673
+ }
674
+ return {
675
+ code: diagnostic.code,
676
+ message: diagnostic.message,
677
+ };
678
+ });
679
+ }
680
+
681
+ function validateStringArray(raw: unknown, field: string): string[] {
682
+ if (raw === undefined || raw === null) return [];
683
+ if (!Array.isArray(raw)) {
684
+ throw new Error(`${field} must be an array`);
685
+ }
686
+ return raw.map((item, idx) => {
687
+ if (typeof item !== "string") {
688
+ throw new Error(`${field}[${idx}] must be a string`);
689
+ }
690
+ return item;
691
+ });
692
+ }
693
+
694
+ function minimalFallback(text: string): RouterOutput {
695
+ const entities = extractEntities(text);
696
+ return {
697
+ routeKind: "agent_task",
698
+ route: "fallback",
699
+ entities,
700
+ slots: {},
701
+ preference_updates: [],
702
+ missing_required: [],
703
+ tool_bundles: [],
704
+ diagnostics: [
705
+ {
706
+ code: "router_validation_failed",
707
+ message: "router validation failed persistently; emitted minimal fallback",
708
+ },
709
+ ],
710
+ reasoning: "router validation failed; emitted minimal fallback",
711
+ };
712
+ }