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,955 @@
1
+ import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
2
+ import {
3
+ isAnalysisRequest,
4
+ normalizeSymbol,
5
+ } from "../analysts/orchestrator.js";
6
+ import { buildComprehensiveAnalysisDefinition } from "../analysts/orchestrator.js";
7
+ import { getConfig } from "../config.js";
8
+ import {
9
+ classifyWithLegacyRules,
10
+ createPiAiRouterClient,
11
+ resolveOptionsScreenerSlots,
12
+ resolvePortfolioSlots,
13
+ route as routeLlm,
14
+ buildResolvedTurnContext,
15
+ } from "../routing/index.js";
16
+ import type {
17
+ RouterInputContext,
18
+ RouterLlmClient,
19
+ RouterOutput,
20
+ } from "../routing/router-types.js";
21
+ import type { ResolvedTurnContext } from "../routing/turn-context.js";
22
+ import type {
23
+ CompareAssetsSlots,
24
+ ExtractedEntities,
25
+ SlotResolution,
26
+ SlotSource,
27
+ } from "../routing/types.js";
28
+ import { buildAssumptionsBlockFromRouter } from "../prompts/workflow-prompts.js";
29
+ import {
30
+ buildPortfolioWorkflowDefinition,
31
+ buildOptionsScreenerWorkflowDefinition,
32
+ buildCompareAssetsWorkflowDefinition,
33
+ } from "../workflows/index.js";
34
+ import { getOpenCandleToolDefinitions } from "./tool-adapter.js";
35
+ import { registerAskUserTool } from "../tools/interaction/ask-user.js";
36
+ import { registerTwitterLoginTool } from "../tools/interaction/twitter-login.js";
37
+ import { SessionCoordinator } from "../runtime/session-coordinator.js";
38
+ import {
39
+ getProvider,
40
+ type ProviderId,
41
+ } from "../onboarding/providers.js";
42
+ import {
43
+ loadOnboardingState,
44
+ saveOnboardingState,
45
+ markProviderSnoozed,
46
+ markProviderNeverAsk,
47
+ markWelcomeShown,
48
+ shouldShowWelcome,
49
+ } from "../onboarding/state.js";
50
+ import { parseToolTag, buildSkippedTag, buildConnectedTag } from "../onboarding/tool-tags.js";
51
+ import { resolveCredentialRequired } from "../onboarding/credential-interceptor.js";
52
+ import { createDegradationAccumulator } from "../onboarding/degradation-accumulator.js";
53
+ import { promptUser } from "../onboarding/prompt-user.js";
54
+ import { runProviderConnect } from "../onboarding/connect.js";
55
+ import type { AskUserHandler } from "../types/index.js";
56
+ import { DISCLAIMER_TEXT } from "../prompts/disclaimer.js";
57
+
58
+ export interface OpenCandleExtensionOptions {
59
+ askUserHandler?: AskUserHandler;
60
+ /**
61
+ * Optional router LLM client. When provided, this instance is used instead
62
+ * of the pi-ai-backed default. Intended for tests + offline eval runners.
63
+ */
64
+ routerLlmClient?: RouterLlmClient;
65
+ }
66
+
67
+ export default function openCandleExtension(pi: ExtensionAPI, options?: OpenCandleExtensionOptions): void {
68
+ const coordinator = new SessionCoordinator();
69
+
70
+ // Credential-interception state. Lifetime:
71
+ // `sessionPromptedSet` — cleared on session_start, persists across turns
72
+ // within a session so users don't get re-prompted after picking
73
+ // "continue without".
74
+ // `hardPromptFiredInWorkflow` — reset on turn_start (the nearest clean
75
+ // boundary for a single user request). Enforces the "at most one hard
76
+ // prompt per workflow" cap.
77
+ // `degradationAccumulator` — per-turn record of soft-tier providers that
78
+ // fell back to their keyless alternative. Reset on turn_start; flushed
79
+ // on turn_end via `pi.appendEntry("opencandle-turn-gap", ...)` for
80
+ // session observability. Does NOT pause the workflow or mutate tool
81
+ // results inline — soft-degraded tags remain visible to the LLM so its
82
+ // final answer can surface them in a **Data gaps** section.
83
+ const sessionPromptedSet = new Set<ProviderId>();
84
+ let hardPromptFiredInWorkflow = false;
85
+ const degradationAccumulator = createDegradationAccumulator();
86
+ let activeToolSnapshot: string[] | null = null;
87
+ let currentRouteToolContext: ResolvedTurnContext | null = null;
88
+
89
+ // Register tools
90
+ for (const tool of getOpenCandleToolDefinitions()) {
91
+ pi.registerTool(tool);
92
+ }
93
+ registerAskUserTool(pi, options?.askUserHandler);
94
+ registerTwitterLoginTool(pi);
95
+
96
+ // /analyze command
97
+ pi.registerCommand("analyze", {
98
+ description: "Run the multi-analyst OpenCandle workflow for a ticker symbol",
99
+ handler: async (args, ctx) => {
100
+ const symbol = normalizeSymbol(args);
101
+ if (!symbol) {
102
+ ctx.ui.notify("Usage: /analyze <ticker>", "warning");
103
+ return;
104
+ }
105
+ const definition = buildComprehensiveAnalysisDefinition(symbol, { debate: getConfig().debate });
106
+ coordinator.executeWorkflow(pi, definition, ctx);
107
+ },
108
+ });
109
+
110
+ // /setup command — reconfigure the OpenCandle AI model (sign-in / API key).
111
+ // Data providers are configured separately via `/connect`.
112
+ pi.registerCommand("setup", {
113
+ description: "Reconfigure the OpenCandle AI model (sign-in or API key)",
114
+ handler: async (_args, ctx) => {
115
+ const result = await coordinator.runSetup(pi, ctx, { mode: "manual" });
116
+ if (result === "ready") {
117
+ ctx.ui.notify("OpenCandle setup complete.", "info");
118
+ }
119
+ },
120
+ });
121
+
122
+ // /connect command — reconfigurable sectioned setup for data providers.
123
+ // `/connect` with no args opens a picker listing all providers.
124
+ // `/connect <alias|id|category>` routes to a specific provider (or a
125
+ // sub-picker for multi-provider categories like "search").
126
+ pi.registerCommand("connect", {
127
+ description: "Connect a data provider (Alpha Vantage, FRED, Finnhub, Brave, Exa)",
128
+ handler: async (args, ctx) => {
129
+ const { listAllProviders, resolveProviderFromArgument, hasCredential } = await import(
130
+ "../onboarding/providers.js"
131
+ );
132
+
133
+ const formatState = (id: ProviderId): string => {
134
+ const state = loadOnboardingState().providers[id];
135
+ if (state?.status === "completed") return "Configured";
136
+ if (state?.status === "snoozed") {
137
+ return `Snoozed until ${state.snoozeUntil.slice(0, 10)}`;
138
+ }
139
+ if (state?.status === "never_ask") return "Never-ask";
140
+ if (hasCredential(id)) return "Configured (via env)";
141
+ return "Not configured";
142
+ };
143
+
144
+ const pickProvider = async (
145
+ providers: readonly ReturnType<typeof getProvider>[],
146
+ ): Promise<ProviderId | undefined> => {
147
+ const labels = providers.map(
148
+ (p) => `${p.displayName} — ${p.unlocks.slice(0, 2).join(", ")} [${formatState(p.id)}]`,
149
+ );
150
+ const choice = await ctx.ui.select("Which provider would you like to connect?", labels);
151
+ if (choice === undefined) return undefined;
152
+ const index = labels.indexOf(choice);
153
+ return providers[index]?.id;
154
+ };
155
+
156
+ const trimmed = args.trim();
157
+ let targetId: ProviderId | undefined;
158
+
159
+ if (trimmed === "") {
160
+ // Bare /connect → full picker.
161
+ targetId = await pickProvider(listAllProviders());
162
+ } else {
163
+ const resolved = resolveProviderFromArgument(trimmed);
164
+ if (!resolved) {
165
+ const all = listAllProviders()
166
+ .map((p) => ` ${p.displayName} (${p.aliases.join(", ")})`)
167
+ .join("\n");
168
+ ctx.ui.notify(
169
+ `Unknown provider: "${trimmed}". Available:\n${all}`,
170
+ "warning",
171
+ );
172
+ return;
173
+ }
174
+ if (Array.isArray(resolved)) {
175
+ // Multi-provider category — show a sub-picker.
176
+ targetId = await pickProvider(resolved as readonly ReturnType<typeof getProvider>[]);
177
+ } else {
178
+ targetId = (resolved as ReturnType<typeof getProvider>).id;
179
+ }
180
+ }
181
+
182
+ if (!targetId) {
183
+ ctx.ui.notify("Connect cancelled.", "info");
184
+ return;
185
+ }
186
+
187
+ const result = await runProviderConnect(ctx, targetId);
188
+ if (result.status === "connected") {
189
+ ctx.ui.notify(`${getProvider(targetId).displayName} is now connected.`, "info");
190
+ } else if (result.status === "cancelled") {
191
+ ctx.ui.notify("Connect cancelled.", "info");
192
+ }
193
+ // "blocked_by_env" already notifies from inside runProviderConnect.
194
+ },
195
+ });
196
+
197
+ // Session start
198
+ pi.on("session_start", async (_event, ctx) => {
199
+ coordinator.initSession(ctx.sessionManager.getSessionId());
200
+ sessionPromptedSet.clear();
201
+ hardPromptFiredInWorkflow = false;
202
+
203
+ if (!ctx.hasUI) return;
204
+ // Pin the user-facing disclaimer in the UI footer for the entire session.
205
+ // Using `setStatus` keeps it always visible to the user without ever
206
+ // entering the LLM's conversation context (unlike `sendMessage`, which Pi
207
+ // reinjects as a `role:"user"` message every turn).
208
+ ctx.ui.setStatus("opencandle-disclaimer", DISCLAIMER_TEXT);
209
+
210
+ const result = await coordinator.runSetup(pi, ctx, { mode: "startup" });
211
+ if (result === "shutdown") {
212
+ return;
213
+ }
214
+
215
+ // One-shot welcome on the very first session (gated on welcomeShownAt).
216
+ // Uses `pi.sendMessage` with `display: true` so the welcome lands in the
217
+ // chat transcript (persistent, scrollable) rather than a transient
218
+ // `ctx.ui.notify` banner.
219
+ const state = loadOnboardingState();
220
+ if (shouldShowWelcome(state, ctx.hasUI)) {
221
+ const WELCOME_BODY =
222
+ "Welcome to OpenCandle. I'm your AI copilot for market analysis.\n\n" +
223
+ "Try something like:\n" +
224
+ " • analyze NVDA — full deep-dive on a ticker\n" +
225
+ " • quote TSLA — just the price and daily move\n" +
226
+ " • how's bitcoin? — crypto\n" +
227
+ " • what's r/wallstreetbets saying about META? — social sentiment\n\n" +
228
+ "You're running with just an LLM right now, which covers most of what\n" +
229
+ "people want. For fundamentals, economic data, or premium news you'll\n" +
230
+ "need a few free API keys — I'll offer to help when they'd actually\n" +
231
+ "make a difference, or run /connect anytime.";
232
+
233
+ pi.sendMessage({
234
+ customType: "opencandle-welcome",
235
+ content: [{ type: "text", text: WELCOME_BODY }],
236
+ display: true,
237
+ });
238
+ saveOnboardingState(markWelcomeShown(state));
239
+ } else {
240
+ ctx.ui.notify(
241
+ "OpenCandle ready. Try /analyze NVDA or /connect to add data providers.",
242
+ "info",
243
+ );
244
+ }
245
+ });
246
+
247
+ // Reset the per-workflow prompt cap AND the degradation accumulator on each
248
+ // new turn. One user request = one workflow invocation = at most one hard
249
+ // prompt and one combined soft-degradation annotation.
250
+ pi.on("turn_start", async () => {
251
+ hardPromptFiredInWorkflow = false;
252
+ degradationAccumulator.reset();
253
+ });
254
+
255
+ // At turn_end, flush the soft-degradation accumulator to a session entry so
256
+ // downstream consumers (UI renderers, debug inspectors, later turns) can see
257
+ // which soft providers fell back during this turn. The LLM has already
258
+ // emitted its answer by the time this fires, so the per-tool-result
259
+ // soft-degraded tags remain the primary carrier for the in-turn gap note.
260
+ //
261
+ // Also persist a per-turn disclaimer entry via `appendEntry`. `CustomEntry`
262
+ // is NOT sent to LLM context (unlike `sendMessage`/`CustomMessage`, which
263
+ // Pi's `convertToLlm` reinjects as a `role:"user"` message), so this keeps
264
+ // the stance instruction-free while still producing a session record that
265
+ // downstream renderers / exporters / tests can surface. The always-visible
266
+ // footer status pinned at session_start is the primary user-visible channel.
267
+ pi.on("turn_end", async (event) => {
268
+ const msg = event.message;
269
+ const isFinalAssistantTurn =
270
+ msg.role === "assistant" && msg.stopReason === "stop";
271
+ if (isFinalAssistantTurn) {
272
+ pi.appendEntry("opencandle-disclaimer", { text: DISCLAIMER_TEXT });
273
+ restoreRouteToolScope();
274
+ }
275
+
276
+ if (degradationAccumulator.isEmpty()) return;
277
+ const state = loadOnboardingState();
278
+ const annotation = degradationAccumulator.buildCombinedAnnotation(state);
279
+ if (annotation !== null) {
280
+ pi.appendEntry("opencandle-turn-gap", { annotation });
281
+ }
282
+ degradationAccumulator.reset();
283
+ });
284
+
285
+ // Intercept tool results for credential-required and soft-degraded tags.
286
+ pi.on("tool_result", async (event, ctx) => {
287
+ // First pass: record any soft-degradation tags in the per-turn accumulator
288
+ // WITHOUT mutating the tool result (the inline tag stays visible to the
289
+ // LLM so it can surface the gap in its final answer). Multiple tags per
290
+ // tool-result block are deduplicated by the accumulator's Set.
291
+ for (const block of event.content) {
292
+ if (block.type !== "text") continue;
293
+ const parsed = parseToolTag(block.text);
294
+ if (parsed?.kind === "soft_degraded") {
295
+ degradationAccumulator.record(parsed.provider);
296
+ }
297
+ }
298
+
299
+ // Second pass: look for a credential-required tag; on match, run the
300
+ // interception decision and either replace the tool result or prompt
301
+ // the user. Only the first credential_required tag in the content list
302
+ // is acted on — subsequent hard-tier prompts are silenced by the
303
+ // per-workflow cap at the decision-function level.
304
+ for (const block of event.content) {
305
+ if (block.type !== "text") continue;
306
+ const parsed = parseToolTag(block.text);
307
+ if (!parsed || parsed.kind !== "credential_required") continue;
308
+
309
+ const state = loadOnboardingState();
310
+ const action = resolveCredentialRequired({
311
+ provider: parsed.provider,
312
+ reason: parsed.reason,
313
+ state,
314
+ sessionPromptedSet,
315
+ hardPromptFiredInWorkflow,
316
+ now: new Date(),
317
+ });
318
+
319
+ if (action.action === "skip") {
320
+ // Replace content with a skipped placeholder so the LLM sees a
321
+ // neutral gap note instead of the credential-required tag.
322
+ const descriptor = getProvider(parsed.provider);
323
+ const remediation = action.silenced
324
+ ? `${action.remediation} (silenced)`
325
+ : action.remediation;
326
+ const tag = buildSkippedTag({
327
+ provider: parsed.provider,
328
+ reason: "credential_not_provided",
329
+ remediation,
330
+ silenced: action.silenced,
331
+ });
332
+ return {
333
+ content: [
334
+ {
335
+ type: "text",
336
+ text:
337
+ `${tag}\n\n${descriptor.displayName} data was not fetched for this request. ` +
338
+ (action.silenced
339
+ ? `You previously asked not to be reminded about this provider.`
340
+ : `To unlock ${descriptor.unlocks.join(", ")}, ${action.remediation}.`),
341
+ },
342
+ ],
343
+ };
344
+ }
345
+
346
+ // action === "prompt": pause and ask the user via promptUser.
347
+ const descriptor = getProvider(parsed.provider);
348
+ const connectLabel = `Connect now — ${descriptor.instructionsHint}`;
349
+ const continueLabel =
350
+ descriptor.fallbackDescription
351
+ ? `Continue with ${descriptor.fallbackDescription} for this run`
352
+ : `Continue without ${descriptor.displayName} for this run`;
353
+ const snoozeLabel = `Snooze ${descriptor.snoozeDurationDays} days`;
354
+ const neverLabel = `Never ask again`;
355
+ const questionBody =
356
+ `${descriptor.displayName} unlocks ${descriptor.unlocks.join(", ")}. ` +
357
+ `Free signup takes about 30 seconds. How would you like to proceed?`;
358
+
359
+ // Mark that a hard-tier prompt has now fired in this workflow (for the cap).
360
+ if (descriptor.tier === "hard") {
361
+ hardPromptFiredInWorkflow = true;
362
+ }
363
+ sessionPromptedSet.add(parsed.provider);
364
+
365
+ const promptResult = await promptUser(
366
+ ctx,
367
+ {
368
+ question: questionBody,
369
+ questionType: "select",
370
+ options: [connectLabel, continueLabel, snoozeLabel, neverLabel],
371
+ },
372
+ options?.askUserHandler,
373
+ );
374
+
375
+ if (promptResult.cancelled) {
376
+ // Treat cancel like "continue without".
377
+ return {
378
+ content: [
379
+ {
380
+ type: "text",
381
+ text:
382
+ `${buildSkippedTag({
383
+ provider: parsed.provider,
384
+ reason: "credential_not_provided",
385
+ remediation: `run /connect ${descriptor.aliases[0] ?? descriptor.id} to unlock`,
386
+ })}\n\nPrompt was cancelled.`,
387
+ },
388
+ ],
389
+ };
390
+ }
391
+
392
+ const answer = promptResult.answer ?? "";
393
+
394
+ if (answer.startsWith("Connect")) {
395
+ const connectResult = await runProviderConnect(ctx, parsed.provider);
396
+ if (connectResult.status === "connected") {
397
+ // Pi has no tool re-dispatch API — emit a CONNECTED placeholder so
398
+ // the LLM knows the key was just saved and can retry on the next
399
+ // turn (or use whatever partial data is in context).
400
+ return {
401
+ content: [
402
+ {
403
+ type: "text",
404
+ text:
405
+ `${buildConnectedTag({ provider: parsed.provider })}\n\n` +
406
+ `${descriptor.displayName} was just connected. Please re-run the previous request to fetch the data now that the credential is available.`,
407
+ },
408
+ ],
409
+ };
410
+ }
411
+ // Cancelled, blocked-by-env, or invalid_key: fall through to skipped
412
+ // with a result-specific explanation so the LLM can describe what
413
+ // just happened in its final answer.
414
+ const connectOutcomeDescription =
415
+ connectResult.status === "blocked_by_env"
416
+ ? "blocked by an existing environment variable"
417
+ : connectResult.status === "invalid_key"
418
+ ? `rejected by ${descriptor.displayName} (the key was invalid and nothing was saved)`
419
+ : "cancelled";
420
+ return {
421
+ content: [
422
+ {
423
+ type: "text",
424
+ text:
425
+ `${buildSkippedTag({
426
+ provider: parsed.provider,
427
+ reason: "credential_not_provided",
428
+ remediation: `run /connect ${descriptor.aliases[0] ?? descriptor.id} to unlock`,
429
+ })}\n\n${descriptor.displayName} connect was ${connectOutcomeDescription}.`,
430
+ },
431
+ ],
432
+ };
433
+ }
434
+
435
+ if (answer.startsWith("Snooze")) {
436
+ saveOnboardingState(
437
+ markProviderSnoozed(state, parsed.provider, descriptor.snoozeDurationDays),
438
+ );
439
+ } else if (answer.startsWith("Never")) {
440
+ saveOnboardingState(markProviderNeverAsk(state, parsed.provider));
441
+ }
442
+ // "Continue" / fallthrough: no state mutation, just skip.
443
+ const silenced = answer.startsWith("Never");
444
+ const remediation = silenced
445
+ ? `run /connect ${descriptor.aliases[0] ?? descriptor.id} to unlock (silenced)`
446
+ : `run /connect ${descriptor.aliases[0] ?? descriptor.id} to unlock`;
447
+ return {
448
+ content: [
449
+ {
450
+ type: "text",
451
+ text:
452
+ `${buildSkippedTag({
453
+ provider: parsed.provider,
454
+ reason: "credential_not_provided",
455
+ remediation,
456
+ silenced,
457
+ })}\n\n${descriptor.displayName} data was omitted per your choice.`,
458
+ },
459
+ ],
460
+ };
461
+ }
462
+
463
+ // No OpenCandle tag in this tool result — pass through.
464
+ return undefined;
465
+ });
466
+
467
+ pi.on("tool_call", async (event) => {
468
+ if (!currentRouteToolContext) return undefined;
469
+ const allowed = new Set(currentRouteToolContext.activeToolNames);
470
+ if (allowed.has(event.toolName)) return undefined;
471
+
472
+ const diagnostic = {
473
+ routeKind: currentRouteToolContext.routeKind,
474
+ workflow: currentRouteToolContext.workflow,
475
+ toolName: event.toolName,
476
+ toolBundles: currentRouteToolContext.toolBundles,
477
+ activeToolNames: currentRouteToolContext.activeToolNames,
478
+ };
479
+ pi.appendEntry("opencandle-tool-scope-violation", diagnostic);
480
+
481
+ if (getConfig().toolScopeMode === "enforce") {
482
+ return {
483
+ block: true,
484
+ reason: `Tool ${event.toolName} is outside the route-selected OpenCandle tool bundle.`,
485
+ };
486
+ }
487
+ return undefined;
488
+ });
489
+
490
+ // Input handling — branches on OPENCANDLE_ROUTER_MODE.
491
+ pi.on("input", async (event, ctx) => {
492
+ if (event.source === "extension") return;
493
+
494
+ // Check for comprehensive analysis pattern — same in both modes.
495
+ const analysis = isAnalysisRequest(event.text);
496
+ if (analysis.match && analysis.symbol) {
497
+ const definition = buildComprehensiveAnalysisDefinition(analysis.symbol, { debate: getConfig().debate });
498
+ const prompt = coordinator.transformWorkflowInput(pi, definition, ctx);
499
+ return prompt ? { action: "transform", text: prompt } : { action: "handled" };
500
+ }
501
+
502
+ const mode = getConfig().routerMode;
503
+ if (mode === "llm") {
504
+ const dispatched = await handleLlmRouterTurn(event.text, ctx);
505
+ // Dispatched a workflow → the original user turn is now represented by
506
+ // the workflow's queued prompts; tell Pi not to also forward it.
507
+ // Fallback path (no dispatch) → let Pi pass the user turn through to the
508
+ // main agent, which will run under the router-supplied fallback context.
509
+ return dispatched || undefined;
510
+ }
511
+
512
+ // --- explicit legacy rules mode (`OPENCANDLE_ROUTER_MODE=rules`) ---
513
+ // Extract and persist user preferences (legacy regex path)
514
+ coordinator.extractAndStorePreferences(event.text);
515
+ const storage = coordinator.getStorage();
516
+ const workflowPrefs = storage?.getWorkflowPreferences("global") ?? {};
517
+
518
+ // Classify intent
519
+ const classification = classifyWithLegacyRules(event.text);
520
+
521
+ if (classification.workflow === "portfolio_builder") {
522
+ const resolution = resolvePortfolioSlots(classification.entities, workflowPrefs);
523
+ coordinator.recordWorkflowRun("portfolio_builder", classification.entities, resolution.resolved, resolution.defaultsUsed);
524
+ pi.appendEntry("opencandle-workflow", { workflow: "portfolio_builder", entities: classification.entities, resolved: resolution.resolved });
525
+ const definition = buildPortfolioWorkflowDefinition(resolution);
526
+ const prompt = coordinator.transformWorkflowInput(pi, definition, ctx);
527
+ return prompt ? { action: "transform", text: prompt } : { action: "handled" };
528
+ }
529
+
530
+ if (classification.workflow === "options_screener") {
531
+ const resolution = resolveOptionsScreenerSlots(classification.entities, workflowPrefs);
532
+ if (resolution.missingRequired.length === 0) {
533
+ coordinator.recordWorkflowRun("options_screener", classification.entities, resolution.resolved, resolution.defaultsUsed);
534
+ pi.appendEntry("opencandle-workflow", { workflow: "options_screener", entities: classification.entities, resolved: resolution.resolved });
535
+ const definition = buildOptionsScreenerWorkflowDefinition(resolution);
536
+ const prompt = coordinator.transformWorkflowInput(pi, definition, ctx);
537
+ return prompt ? { action: "transform", text: prompt } : { action: "handled" };
538
+ }
539
+ }
540
+
541
+ if (classification.workflow === "compare_assets" && classification.entities.symbols.length >= 2) {
542
+ const resolution: SlotResolution<CompareAssetsSlots> = {
543
+ resolved: {
544
+ symbols: classification.entities.symbols,
545
+ metrics: classification.entities.compareMetrics,
546
+ timeHorizon: classification.entities.timeHorizon,
547
+ },
548
+ sources: {
549
+ symbols: "user",
550
+ ...(classification.entities.timeHorizon ? { timeHorizon: "user" as const } : {}),
551
+ ...(classification.entities.compareMetrics ? { metrics: "user" as const } : {}),
552
+ },
553
+ defaultsUsed: [],
554
+ missingRequired: [],
555
+ };
556
+ coordinator.recordWorkflowRun("compare_assets", classification.entities, resolution.resolved, resolution.defaultsUsed);
557
+ pi.appendEntry("opencandle-workflow", { workflow: "compare_assets", symbols: classification.entities.symbols });
558
+ const definition = buildCompareAssetsWorkflowDefinition(resolution);
559
+ const prompt = coordinator.transformWorkflowInput(pi, definition, ctx);
560
+ return prompt ? { action: "transform", text: prompt } : { action: "handled" };
561
+ }
562
+ });
563
+
564
+ /**
565
+ * LLM-mode input handler. In this mode `classifyIntent` and
566
+ * `extractPreferences` are NOT called — the router is the single source of
567
+ * classification + preference extraction. Mirrors rule-mode dispatch for
568
+ * identified workflows; for `fallback` turns, stashes a fallback context
569
+ * for the next `before_agent_start` to inject.
570
+ */
571
+ async function handleLlmRouterTurn(
572
+ text: string,
573
+ ctx: Parameters<Parameters<ExtensionAPI["on"]>[1]>[1],
574
+ ): Promise<{ action: "transform"; text: string } | false> {
575
+ const storage = coordinator.getStorage();
576
+ const { profileSnapshot, recentWorkflowRuns, priorTurns } =
577
+ coordinator.buildRouterContextBase(ctx.sessionManager);
578
+ // priorTurns is not scrubbed for /forget — tracked in proposal.md follow-ups.
579
+ const input: RouterInputContext = {
580
+ text,
581
+ priorTurns,
582
+ profileSnapshot,
583
+ recentWorkflowRuns,
584
+ };
585
+
586
+ const client = options?.routerLlmClient ?? resolveRouterLlmClient(ctx);
587
+ if (!client) {
588
+ pi.appendEntry("opencandle-router-error", {
589
+ reason: "no_llm_client_available",
590
+ text,
591
+ });
592
+ return false;
593
+ }
594
+
595
+ let output: RouterOutput;
596
+ try {
597
+ output = await routeLlm(input, client);
598
+ } catch (err) {
599
+ pi.appendEntry("opencandle-router-error", {
600
+ reason: "route_failed",
601
+ text,
602
+ message: err instanceof Error ? err.message : String(err),
603
+ });
604
+ return false;
605
+ }
606
+
607
+ const availableToolNames = safeGetAllToolNames();
608
+ const memory = coordinator.retrieveMemoryForRoute(
609
+ output.routeKind,
610
+ output.workflow,
611
+ Object.keys(output.slots),
612
+ );
613
+ const resolvedTurnContext = buildResolvedTurnContext(input, output, {
614
+ availableToolNames,
615
+ memoryEntries: memory.entries,
616
+ filteredMemory: memory.filtered.map(({ entry, reason }) => ({
617
+ category: entry.category,
618
+ key: entry.key,
619
+ source: entry.source,
620
+ recordedAt: entry.recordedAt,
621
+ confidence: entry.confidence,
622
+ filtered: true,
623
+ filterReason: reason,
624
+ })),
625
+ planning: {
626
+ migrationStatuses: getConfig().planningMigrationStatuses,
627
+ },
628
+ });
629
+
630
+ pi.appendEntry("opencandle-router", { output });
631
+ pi.appendEntry("opencandle-route-context", resolvedTurnContext);
632
+ coordinator.setPendingResolvedTurnContext(resolvedTurnContext);
633
+ applyRouteToolScope(resolvedTurnContext);
634
+
635
+ // Preference writes: HIGH-confidence only. Medium/low are logged for
636
+ // observability even when no storage is available.
637
+ const dropped: typeof output.preference_updates = [];
638
+ for (const pref of output.preference_updates) {
639
+ if (pref.confidence === "high") {
640
+ storage?.upsertPreference({
641
+ key: pref.key,
642
+ valueJson: JSON.stringify(pref.value),
643
+ confidence: pref.confidence,
644
+ source: pref.source,
645
+ });
646
+ } else {
647
+ dropped.push(pref);
648
+ }
649
+ }
650
+ if (dropped.length > 0) {
651
+ pi.appendEntry("opencandle-router-prefs-dropped", { dropped });
652
+ }
653
+
654
+ // Workflow dispatch for recognised workflows.
655
+ if (output.routeKind === "workflow_dispatch" && output.workflow) {
656
+ return dispatchRouterWorkflow(output, ctx);
657
+ }
658
+
659
+ if (output.routeKind === "pass_through") {
660
+ return false;
661
+ }
662
+
663
+ // Fallback: record the turn and stash the fallback context for the
664
+ // upcoming `before_agent_start` hook to render into the system prompt.
665
+ coordinator.recordWorkflowRun(
666
+ "fallback",
667
+ output.entities,
668
+ Object.fromEntries(Object.entries(output.slots).map(([k, v]) => [k, v.value])),
669
+ [],
670
+ output.routeKind,
671
+ );
672
+
673
+ const assumptionsBlock = buildAssumptionsBlockFromRouter(output.slots);
674
+ coordinator.setPendingFallbackContext({
675
+ assumptionsBlock,
676
+ missingRequired: output.missing_required,
677
+ extraContext: output.entities.symbols.length > 0
678
+ ? `Router-extracted symbols: ${output.entities.symbols.join(", ")}.`
679
+ + ` Route kind: ${output.routeKind}. Tool bundles: ${output.tool_bundles.join(", ") || "(none)"}.`
680
+ : undefined,
681
+ });
682
+ return false;
683
+ }
684
+
685
+ function dispatchRouterWorkflow(
686
+ output: RouterOutput,
687
+ ctx: Parameters<Parameters<ExtensionAPI["on"]>[1]>[1],
688
+ ): { action: "transform"; text: string } | false {
689
+ const workflow = output.workflow!;
690
+ const storage = coordinator.getStorage();
691
+ const workflowPrefs = storage?.getWorkflowPreferences("global") ?? {};
692
+ const entities = mergeRouterSlotsIntoEntities(output);
693
+
694
+ if (workflow === "portfolio_builder") {
695
+ const resolution = withRouterSlotSources(
696
+ resolvePortfolioSlots(entities, workflowPrefs),
697
+ output,
698
+ );
699
+ coordinator.recordWorkflowRun(
700
+ "portfolio_builder",
701
+ entities,
702
+ resolution.resolved,
703
+ resolution.defaultsUsed,
704
+ output.routeKind,
705
+ );
706
+ pi.appendEntry("opencandle-workflow", {
707
+ workflow: "portfolio_builder",
708
+ entities,
709
+ resolved: resolution.resolved,
710
+ });
711
+ const definition = buildPortfolioWorkflowDefinition(resolution);
712
+ const prompt = coordinator.transformWorkflowInput(pi, definition, ctx);
713
+ return prompt ? { action: "transform", text: prompt } : false;
714
+ }
715
+ if (workflow === "options_screener") {
716
+ const resolution = withRouterSlotSources(
717
+ resolveOptionsScreenerSlots(entities, workflowPrefs),
718
+ output,
719
+ );
720
+ // Router may emit missing_required; main agent handles via ask_user.
721
+ // Still dispatch the workflow when symbol is present.
722
+ if (resolution.missingRequired.length === 0) {
723
+ coordinator.recordWorkflowRun(
724
+ "options_screener",
725
+ entities,
726
+ resolution.resolved,
727
+ resolution.defaultsUsed,
728
+ output.routeKind,
729
+ );
730
+ pi.appendEntry("opencandle-workflow", {
731
+ workflow: "options_screener",
732
+ entities,
733
+ resolved: resolution.resolved,
734
+ });
735
+ const definition = buildOptionsScreenerWorkflowDefinition(resolution);
736
+ const prompt = coordinator.transformWorkflowInput(pi, definition, ctx);
737
+ return prompt ? { action: "transform", text: prompt } : false;
738
+ }
739
+ // Missing required symbol — treat as fallback with ask_user directive.
740
+ }
741
+ if (workflow === "compare_assets" && entities.symbols.length >= 2) {
742
+ const resolution: SlotResolution<CompareAssetsSlots> = {
743
+ resolved: {
744
+ symbols: entities.symbols,
745
+ metrics: entities.compareMetrics,
746
+ timeHorizon: entities.timeHorizon,
747
+ },
748
+ sources: {
749
+ symbols: sourceForRouterSlot(output, "symbols", "user"),
750
+ ...(entities.timeHorizon ? { timeHorizon: "user" as const } : {}),
751
+ ...(entities.compareMetrics ? { metrics: "user" as const } : {}),
752
+ },
753
+ defaultsUsed: [],
754
+ missingRequired: [],
755
+ };
756
+ coordinator.recordWorkflowRun(
757
+ "compare_assets",
758
+ entities,
759
+ resolution.resolved,
760
+ [],
761
+ output.routeKind,
762
+ );
763
+ pi.appendEntry("opencandle-workflow", {
764
+ workflow: "compare_assets",
765
+ symbols: entities.symbols,
766
+ });
767
+ const definition = buildCompareAssetsWorkflowDefinition(resolution);
768
+ const prompt = coordinator.transformWorkflowInput(pi, definition, ctx);
769
+ return prompt ? { action: "transform", text: prompt } : false;
770
+ }
771
+
772
+ // single_asset_analysis / watchlist / general_qa + any workflow with
773
+ // unmet required slots: fall through to fallback handling so the main
774
+ // agent still gets an Assumptions block + ask_user directive.
775
+ coordinator.recordWorkflowRun(
776
+ "fallback",
777
+ output.entities,
778
+ Object.fromEntries(Object.entries(output.slots).map(([k, v]) => [k, v.value])),
779
+ [],
780
+ output.routeKind,
781
+ );
782
+ const assumptionsBlock = buildAssumptionsBlockFromRouter(output.slots);
783
+ coordinator.setPendingFallbackContext({
784
+ assumptionsBlock,
785
+ missingRequired: output.missing_required,
786
+ extraContext: `Router classified as ${workflow} but declined to dispatch. Symbols: ${entities.symbols.join(", ") || "(none)"}.`,
787
+ });
788
+ return false;
789
+ }
790
+
791
+ function mergeRouterSlotsIntoEntities(output: RouterOutput): ExtractedEntities {
792
+ const entities: ExtractedEntities = {
793
+ ...output.entities,
794
+ symbols: output.entities.symbols,
795
+ };
796
+
797
+ if (entities.budget === undefined && typeof output.slots.budget?.value === "number") {
798
+ entities.budget = output.slots.budget.value;
799
+ }
800
+
801
+ const slotSymbols = symbolsFromRouterSlots(output);
802
+ if (slotSymbols.length > 0 && slotSymbols.length > entities.symbols.length) {
803
+ entities.symbols = mergeSymbols(slotSymbols, entities.symbols);
804
+ }
805
+
806
+ return entities;
807
+ }
808
+
809
+ function withRouterSlotSources<T extends object>(
810
+ resolution: SlotResolution<T>,
811
+ output: RouterOutput,
812
+ ): SlotResolution<T> {
813
+ const sources: Record<string, SlotSource | undefined> = { ...resolution.sources };
814
+ if (output.entities.budget === undefined && output.slots.budget) {
815
+ sources.budget = output.slots.budget.source;
816
+ }
817
+ if (output.entities.symbols.length === 0 && output.slots.symbol) {
818
+ sources.symbol = output.slots.symbol.source;
819
+ }
820
+ if (output.entities.symbols.length < 2 && output.slots.symbols) {
821
+ sources.symbols = output.slots.symbols.source;
822
+ }
823
+ return { ...resolution, sources: sources as SlotResolution<T>["sources"] };
824
+ }
825
+
826
+ function sourceForRouterSlot(
827
+ output: RouterOutput,
828
+ slotName: "symbol" | "symbols" | "budget",
829
+ fallback: SlotSource,
830
+ ): SlotSource {
831
+ return output.slots[slotName]?.source ?? fallback;
832
+ }
833
+
834
+ function symbolsFromRouterSlots(output: RouterOutput): string[] {
835
+ const symbols: string[] = [];
836
+ const symbol = output.slots.symbol?.value;
837
+ if (typeof symbol === "string" && symbol.trim() !== "") {
838
+ symbols.push(symbol.toUpperCase());
839
+ }
840
+ const symbolList = output.slots.symbols?.value;
841
+ if (Array.isArray(symbolList)) {
842
+ for (const value of symbolList) {
843
+ if (typeof value === "string" && value.trim() !== "") {
844
+ symbols.push(value.toUpperCase());
845
+ }
846
+ }
847
+ }
848
+ return symbols;
849
+ }
850
+
851
+ function mergeSymbols(primary: string[], secondary: string[]): string[] {
852
+ const merged: string[] = [];
853
+ for (const symbol of [...primary, ...secondary]) {
854
+ if (!merged.includes(symbol)) merged.push(symbol);
855
+ }
856
+ return merged;
857
+ }
858
+
859
+ function safeGetAllToolNames(): string[] {
860
+ try {
861
+ return pi.getAllTools().map((tool) => tool.name);
862
+ } catch {
863
+ return [];
864
+ }
865
+ }
866
+
867
+ function applyRouteToolScope(context: ResolvedTurnContext): void {
868
+ const mode = getConfig().toolScopeMode;
869
+ currentRouteToolContext = context;
870
+ pi.appendEntry("opencandle-tool-scope", {
871
+ mode,
872
+ routeKind: context.routeKind,
873
+ workflow: context.workflow,
874
+ toolBundles: context.toolBundles,
875
+ activeToolNames: context.activeToolNames,
876
+ enforced: false,
877
+ });
878
+
879
+ if (mode !== "enforce") return;
880
+ if (context.activeToolNames.length === 0) return;
881
+
882
+ try {
883
+ if (activeToolSnapshot === null) {
884
+ activeToolSnapshot = pi.getActiveTools();
885
+ }
886
+ pi.setActiveTools(context.activeToolNames);
887
+ pi.appendEntry("opencandle-tool-scope", {
888
+ mode,
889
+ routeKind: context.routeKind,
890
+ workflow: context.workflow,
891
+ toolBundles: context.toolBundles,
892
+ activeToolNames: context.activeToolNames,
893
+ enforced: true,
894
+ });
895
+ } catch (err) {
896
+ pi.appendEntry("opencandle-tool-scope", {
897
+ mode,
898
+ routeKind: context.routeKind,
899
+ workflow: context.workflow,
900
+ toolBundles: context.toolBundles,
901
+ activeToolNames: context.activeToolNames,
902
+ enforced: false,
903
+ diagnostic: err instanceof Error ? err.message : String(err),
904
+ });
905
+ }
906
+ }
907
+
908
+ function restoreRouteToolScope(): void {
909
+ currentRouteToolContext = null;
910
+ if (activeToolSnapshot === null) return;
911
+ try {
912
+ pi.setActiveTools(activeToolSnapshot);
913
+ pi.appendEntry("opencandle-tool-scope", {
914
+ mode: getConfig().toolScopeMode,
915
+ restored: true,
916
+ activeToolNames: activeToolSnapshot,
917
+ });
918
+ } catch (err) {
919
+ pi.appendEntry("opencandle-tool-scope", {
920
+ mode: getConfig().toolScopeMode,
921
+ restored: false,
922
+ diagnostic: err instanceof Error ? err.message : String(err),
923
+ });
924
+ } finally {
925
+ activeToolSnapshot = null;
926
+ }
927
+ }
928
+
929
+ function resolveRouterLlmClient(
930
+ ctx: Parameters<Parameters<ExtensionAPI["on"]>[1]>[1],
931
+ ): RouterLlmClient | null {
932
+ // `ctx.model` is the currently selected pi-ai model. When unset (no auth
933
+ // configured yet), we skip the router and the main agent will run with
934
+ // its default unrouted flow (legacy rules path is the safer default).
935
+ const model = (ctx as { model?: unknown }).model;
936
+ if (!model) return null;
937
+ // biome-ignore lint/suspicious/noExplicitAny: Pi typings keep Model generic
938
+ return createPiAiRouterClient(model as any);
939
+ }
940
+
941
+ // System prompt assembly — delegate to coordinator. When a fallback context
942
+ // is pending (router-mode fallback turns), inject it into the prompt.
943
+ pi.on("before_agent_start", async (event) => {
944
+ const fallbackContext = coordinator.consumePendingFallbackContext() ?? undefined;
945
+ const resolvedTurnContext = coordinator.consumePendingResolvedTurnContext() ?? undefined;
946
+ return {
947
+ systemPrompt: coordinator.buildSystemPrompt(
948
+ event.systemPrompt,
949
+ undefined,
950
+ fallbackContext,
951
+ resolvedTurnContext,
952
+ ),
953
+ };
954
+ });
955
+ }