opencandle 0.2.0 → 0.4.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 (401) hide show
  1. package/README.md +110 -87
  2. package/assets/logo.svg +187 -0
  3. package/dist/analysts/orchestrator.js +1 -2
  4. package/dist/analysts/orchestrator.js.map +1 -1
  5. package/dist/cli.d.ts +1 -1
  6. package/dist/cli.js +38 -2
  7. package/dist/cli.js.map +1 -1
  8. package/dist/config.d.ts +34 -5
  9. package/dist/config.js +29 -8
  10. package/dist/config.js.map +1 -1
  11. package/dist/infra/browser.d.ts +10 -0
  12. package/dist/infra/browser.js +1 -0
  13. package/dist/infra/browser.js.map +1 -1
  14. package/dist/infra/cache.d.ts +4 -0
  15. package/dist/infra/cache.js +4 -0
  16. package/dist/infra/cache.js.map +1 -1
  17. package/dist/infra/native-dependencies.d.ts +1 -0
  18. package/dist/infra/native-dependencies.js +10 -0
  19. package/dist/infra/native-dependencies.js.map +1 -0
  20. package/dist/infra/node-version.d.ts +2 -0
  21. package/dist/infra/node-version.js +23 -0
  22. package/dist/infra/node-version.js.map +1 -0
  23. package/dist/infra/rate-limiter.js +6 -0
  24. package/dist/infra/rate-limiter.js.map +1 -1
  25. package/dist/memory/index.d.ts +2 -0
  26. package/dist/memory/index.js +1 -0
  27. package/dist/memory/index.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 +6 -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/onboarding/connect.d.ts +35 -0
  37. package/dist/onboarding/connect.js +118 -0
  38. package/dist/onboarding/connect.js.map +1 -0
  39. package/dist/onboarding/credential-interceptor.d.ts +44 -0
  40. package/dist/onboarding/credential-interceptor.js +72 -0
  41. package/dist/onboarding/credential-interceptor.js.map +1 -0
  42. package/dist/onboarding/degradation-accumulator.d.ts +21 -0
  43. package/dist/onboarding/degradation-accumulator.js +55 -0
  44. package/dist/onboarding/degradation-accumulator.js.map +1 -0
  45. package/dist/onboarding/prompt-user.d.ts +23 -0
  46. package/dist/onboarding/prompt-user.js +61 -0
  47. package/dist/onboarding/prompt-user.js.map +1 -0
  48. package/dist/onboarding/providers.d.ts +116 -0
  49. package/dist/onboarding/providers.js +239 -0
  50. package/dist/onboarding/providers.js.map +1 -0
  51. package/dist/onboarding/state.d.ts +31 -2
  52. package/dist/onboarding/state.js +141 -13
  53. package/dist/onboarding/state.js.map +1 -1
  54. package/dist/onboarding/tool-helpers.d.ts +34 -0
  55. package/dist/onboarding/tool-helpers.js +80 -0
  56. package/dist/onboarding/tool-helpers.js.map +1 -0
  57. package/dist/onboarding/tool-tags.d.ts +37 -0
  58. package/dist/onboarding/tool-tags.js +149 -0
  59. package/dist/onboarding/tool-tags.js.map +1 -0
  60. package/dist/onboarding/validation.d.ts +19 -0
  61. package/dist/onboarding/validation.js +117 -0
  62. package/dist/onboarding/validation.js.map +1 -0
  63. package/dist/pi/opencandle-extension.d.ts +7 -1
  64. package/dist/pi/opencandle-extension.js +488 -13
  65. package/dist/pi/opencandle-extension.js.map +1 -1
  66. package/dist/pi/session-storage.d.ts +2 -0
  67. package/dist/pi/session-storage.js +5 -0
  68. package/dist/pi/session-storage.js.map +1 -0
  69. package/dist/pi/session.d.ts +4 -1
  70. package/dist/pi/session.js +25 -3
  71. package/dist/pi/session.js.map +1 -1
  72. package/dist/pi/setup.d.ts +1 -2
  73. package/dist/pi/setup.js +67 -120
  74. package/dist/pi/setup.js.map +1 -1
  75. package/dist/pi/tool-adapter.d.ts +2 -2
  76. package/dist/pi/tool-adapter.js +14 -1
  77. package/dist/pi/tool-adapter.js.map +1 -1
  78. package/dist/prompts/context-builder.d.ts +22 -0
  79. package/dist/prompts/context-builder.js +47 -11
  80. package/dist/prompts/context-builder.js.map +1 -1
  81. package/dist/prompts/disclaimer.d.ts +6 -0
  82. package/dist/prompts/disclaimer.js +9 -0
  83. package/dist/prompts/disclaimer.js.map +1 -0
  84. package/dist/prompts/workflow-prompts.d.ts +8 -0
  85. package/dist/prompts/workflow-prompts.js +39 -5
  86. package/dist/prompts/workflow-prompts.js.map +1 -1
  87. package/dist/providers/alpha-vantage.js +20 -1
  88. package/dist/providers/alpha-vantage.js.map +1 -1
  89. package/dist/providers/exa-search.d.ts +39 -0
  90. package/dist/providers/exa-search.js +276 -0
  91. package/dist/providers/exa-search.js.map +1 -0
  92. package/dist/providers/finnhub.d.ts +17 -0
  93. package/dist/providers/finnhub.js +94 -0
  94. package/dist/providers/finnhub.js.map +1 -0
  95. package/dist/providers/fred.js +13 -1
  96. package/dist/providers/fred.js.map +1 -1
  97. package/dist/providers/index.d.ts +2 -0
  98. package/dist/providers/index.js +1 -0
  99. package/dist/providers/index.js.map +1 -1
  100. package/dist/providers/provider-credential-error.d.ts +8 -0
  101. package/dist/providers/provider-credential-error.js +22 -0
  102. package/dist/providers/provider-credential-error.js.map +1 -0
  103. package/dist/providers/reddit.d.ts +8 -0
  104. package/dist/providers/reddit.js +36 -9
  105. package/dist/providers/reddit.js.map +1 -1
  106. package/dist/providers/twitter.js +2 -8
  107. package/dist/providers/twitter.js.map +1 -1
  108. package/dist/providers/web-search.d.ts +17 -0
  109. package/dist/providers/web-search.js +224 -0
  110. package/dist/providers/web-search.js.map +1 -0
  111. package/dist/providers/wrap-provider.d.ts +7 -0
  112. package/dist/providers/wrap-provider.js +15 -0
  113. package/dist/providers/wrap-provider.js.map +1 -1
  114. package/dist/providers/yahoo-finance.js +70 -33
  115. package/dist/providers/yahoo-finance.js.map +1 -1
  116. package/dist/routing/classify-intent.js +22 -0
  117. package/dist/routing/classify-intent.js.map +1 -1
  118. package/dist/routing/defaults.js +1 -1
  119. package/dist/routing/defaults.js.map +1 -1
  120. package/dist/routing/index.d.ts +4 -0
  121. package/dist/routing/index.js +3 -0
  122. package/dist/routing/index.js.map +1 -1
  123. package/dist/routing/router-llm-client.d.ts +11 -0
  124. package/dist/routing/router-llm-client.js +42 -0
  125. package/dist/routing/router-llm-client.js.map +1 -0
  126. package/dist/routing/router-prompt.d.ts +2 -0
  127. package/dist/routing/router-prompt.js +138 -0
  128. package/dist/routing/router-prompt.js.map +1 -0
  129. package/dist/routing/router-types.d.ts +62 -0
  130. package/dist/routing/router-types.js +2 -0
  131. package/dist/routing/router-types.js.map +1 -0
  132. package/dist/routing/router.d.ts +10 -0
  133. package/dist/routing/router.js +194 -0
  134. package/dist/routing/router.js.map +1 -0
  135. package/dist/runtime/session-coordinator.d.ts +63 -4
  136. package/dist/runtime/session-coordinator.js +155 -4
  137. package/dist/runtime/session-coordinator.js.map +1 -1
  138. package/dist/runtime/tool-defaults-wrapper.d.ts +3 -0
  139. package/dist/runtime/tool-defaults-wrapper.js +25 -0
  140. package/dist/runtime/tool-defaults-wrapper.js.map +1 -0
  141. package/dist/sentiment/adapters/finnhub.d.ts +7 -0
  142. package/dist/sentiment/adapters/finnhub.js +39 -0
  143. package/dist/sentiment/adapters/finnhub.js.map +1 -0
  144. package/dist/sentiment/adapters/reddit.d.ts +11 -0
  145. package/dist/sentiment/adapters/reddit.js +54 -0
  146. package/dist/sentiment/adapters/reddit.js.map +1 -0
  147. package/dist/sentiment/adapters/twitter.d.ts +9 -0
  148. package/dist/sentiment/adapters/twitter.js +32 -0
  149. package/dist/sentiment/adapters/twitter.js.map +1 -0
  150. package/dist/sentiment/adapters/web.d.ts +9 -0
  151. package/dist/sentiment/adapters/web.js +40 -0
  152. package/dist/sentiment/adapters/web.js.map +1 -0
  153. package/dist/sentiment/index.d.ts +16 -0
  154. package/dist/sentiment/index.js +44 -0
  155. package/dist/sentiment/index.js.map +1 -0
  156. package/dist/sentiment/keywords.d.ts +2 -0
  157. package/dist/sentiment/keywords.js +9 -0
  158. package/dist/sentiment/keywords.js.map +1 -0
  159. package/dist/sentiment/pipeline.d.ts +9 -0
  160. package/dist/sentiment/pipeline.js +57 -0
  161. package/dist/sentiment/pipeline.js.map +1 -0
  162. package/dist/sentiment/scorer.d.ts +9 -0
  163. package/dist/sentiment/scorer.js +64 -0
  164. package/dist/sentiment/scorer.js.map +1 -0
  165. package/dist/sentiment/store.d.ts +24 -0
  166. package/dist/sentiment/store.js +182 -0
  167. package/dist/sentiment/store.js.map +1 -0
  168. package/dist/sentiment/trends.d.ts +13 -0
  169. package/dist/sentiment/trends.js +73 -0
  170. package/dist/sentiment/trends.js.map +1 -0
  171. package/dist/sentiment/types.d.ts +66 -0
  172. package/dist/sentiment/types.js +54 -0
  173. package/dist/sentiment/types.js.map +1 -0
  174. package/dist/system-prompt.js +29 -13
  175. package/dist/system-prompt.js.map +1 -1
  176. package/dist/tool-kit.d.ts +4 -4
  177. package/dist/tools/fundamentals/company-overview.d.ts +4 -2
  178. package/dist/tools/fundamentals/company-overview.js +27 -27
  179. package/dist/tools/fundamentals/company-overview.js.map +1 -1
  180. package/dist/tools/fundamentals/comps.d.ts +1 -1
  181. package/dist/tools/fundamentals/comps.js +45 -45
  182. package/dist/tools/fundamentals/comps.js.map +1 -1
  183. package/dist/tools/fundamentals/dcf.d.ts +1 -1
  184. package/dist/tools/fundamentals/dcf.js +82 -82
  185. package/dist/tools/fundamentals/dcf.js.map +1 -1
  186. package/dist/tools/fundamentals/earnings.d.ts +4 -2
  187. package/dist/tools/fundamentals/earnings.js +25 -25
  188. package/dist/tools/fundamentals/earnings.js.map +1 -1
  189. package/dist/tools/fundamentals/financials.d.ts +4 -2
  190. package/dist/tools/fundamentals/financials.js +23 -23
  191. package/dist/tools/fundamentals/financials.js.map +1 -1
  192. package/dist/tools/fundamentals/sec-filings.d.ts +1 -1
  193. package/dist/tools/index.d.ts +28 -1
  194. package/dist/tools/index.js +35 -2
  195. package/dist/tools/index.js.map +1 -1
  196. package/dist/tools/interaction/ask-user.d.ts +1 -1
  197. package/dist/tools/interaction/ask-user.js +28 -64
  198. package/dist/tools/interaction/ask-user.js.map +1 -1
  199. package/dist/tools/interaction/twitter-login.d.ts +1 -1
  200. package/dist/tools/macro/fear-greed.d.ts +1 -1
  201. package/dist/tools/macro/fred-data.d.ts +4 -2
  202. package/dist/tools/macro/fred-data.js +26 -26
  203. package/dist/tools/macro/fred-data.js.map +1 -1
  204. package/dist/tools/market/crypto-history.d.ts +1 -1
  205. package/dist/tools/market/crypto-price.d.ts +1 -1
  206. package/dist/tools/market/search-ticker.d.ts +1 -1
  207. package/dist/tools/market/stock-history.d.ts +1 -1
  208. package/dist/tools/market/stock-quote.d.ts +1 -1
  209. package/dist/tools/options/option-chain.d.ts +1 -1
  210. package/dist/tools/options/option-chain.js +4 -1
  211. package/dist/tools/options/option-chain.js.map +1 -1
  212. package/dist/tools/portfolio/correlation.d.ts +1 -1
  213. package/dist/tools/portfolio/predictions.d.ts +1 -1
  214. package/dist/tools/portfolio/risk-analysis.d.ts +1 -1
  215. package/dist/tools/portfolio/tracker.d.ts +1 -1
  216. package/dist/tools/portfolio/watchlist.d.ts +1 -1
  217. package/dist/tools/sentiment/reddit-sentiment.d.ts +4 -2
  218. package/dist/tools/sentiment/reddit-sentiment.js +107 -22
  219. package/dist/tools/sentiment/reddit-sentiment.js.map +1 -1
  220. package/dist/tools/sentiment/sentiment-summary.d.ts +7 -0
  221. package/dist/tools/sentiment/sentiment-summary.js +230 -0
  222. package/dist/tools/sentiment/sentiment-summary.js.map +1 -0
  223. package/dist/tools/sentiment/sentiment-trend.d.ts +22 -0
  224. package/dist/tools/sentiment/sentiment-trend.js +39 -0
  225. package/dist/tools/sentiment/sentiment-trend.js.map +1 -0
  226. package/dist/tools/sentiment/twitter-sentiment.d.ts +1 -1
  227. package/dist/tools/sentiment/twitter-sentiment.js +17 -0
  228. package/dist/tools/sentiment/twitter-sentiment.js.map +1 -1
  229. package/dist/tools/sentiment/web-search.d.ts +11 -0
  230. package/dist/tools/sentiment/web-search.js +115 -0
  231. package/dist/tools/sentiment/web-search.js.map +1 -0
  232. package/dist/tools/sentiment/web-sentiment.d.ts +8 -0
  233. package/dist/tools/sentiment/web-sentiment.js +66 -0
  234. package/dist/tools/sentiment/web-sentiment.js.map +1 -0
  235. package/dist/tools/technical/backtest.d.ts +1 -1
  236. package/dist/tools/technical/indicators.d.ts +1 -1
  237. package/dist/tools/technical/indicators.js +7 -1
  238. package/dist/tools/technical/indicators.js.map +1 -1
  239. package/dist/types/index.d.ts +1 -1
  240. package/dist/types/sentiment.d.ts +21 -0
  241. package/dist/workflows/options-screener.js +7 -2
  242. package/dist/workflows/options-screener.js.map +1 -1
  243. package/dist/workflows/portfolio-builder.js +3 -3
  244. package/dist/workflows/portfolio-builder.js.map +1 -1
  245. package/gui/server/background-quotes.ts +31 -0
  246. package/gui/server/chat-event-adapter.ts +142 -0
  247. package/gui/server/invoke-tool.ts +89 -0
  248. package/gui/server/live-chat-event-adapter.ts +181 -0
  249. package/gui/server/model-setup.ts +100 -0
  250. package/gui/server/package.json +5 -0
  251. package/gui/server/projector.ts +212 -0
  252. package/gui/server/server.ts +592 -0
  253. package/gui/server/session-actions.ts +31 -0
  254. package/gui/server/tool-metadata.ts +88 -0
  255. package/gui/server/websocket.ts +128 -0
  256. package/gui/server/writer-lock.ts +118 -0
  257. package/gui/shared/chat-events.ts +118 -0
  258. package/gui/shared/event-reducer.ts +186 -0
  259. package/gui/web/dist/assets/CatalogOverlay-D1ImSJTe.js +1 -0
  260. package/gui/web/dist/assets/index-DBrWq43L.css +1 -0
  261. package/gui/web/dist/assets/index-RflHaj0y.js +67 -0
  262. package/gui/web/dist/assets/logo-CWpt6Y2a.svg +187 -0
  263. package/gui/web/dist/index.html +17 -0
  264. package/package.json +62 -20
  265. package/src/analysts/contracts.ts +189 -0
  266. package/src/analysts/orchestrator.ts +300 -0
  267. package/src/cli.ts +205 -0
  268. package/src/config.ts +161 -0
  269. package/src/index.ts +5 -0
  270. package/src/infra/browser.ts +111 -0
  271. package/src/infra/cache.ts +103 -0
  272. package/src/infra/http-client.ts +68 -0
  273. package/src/infra/index.ts +18 -0
  274. package/src/infra/native-dependencies.ts +12 -0
  275. package/src/infra/node-version.ts +24 -0
  276. package/src/infra/open-url.ts +28 -0
  277. package/src/infra/opencandle-paths.ts +64 -0
  278. package/src/infra/rate-limiter.ts +64 -0
  279. package/src/memory/index.ts +10 -0
  280. package/src/memory/manager.ts +159 -0
  281. package/src/memory/preference-extractor.ts +106 -0
  282. package/src/memory/retrieval.ts +70 -0
  283. package/src/memory/sqlite.ts +172 -0
  284. package/src/memory/storage.ts +204 -0
  285. package/src/memory/tool-defaults.ts +87 -0
  286. package/src/memory/types.ts +67 -0
  287. package/src/onboarding/connect.ts +184 -0
  288. package/src/onboarding/credential-interceptor.ts +134 -0
  289. package/src/onboarding/degradation-accumulator.ts +79 -0
  290. package/src/onboarding/prompt-user.ts +85 -0
  291. package/src/onboarding/providers.ts +315 -0
  292. package/src/onboarding/state.ts +218 -0
  293. package/src/onboarding/tool-helpers.ts +111 -0
  294. package/src/onboarding/tool-tags.ts +201 -0
  295. package/src/onboarding/validation.ts +158 -0
  296. package/src/pi/opencandle-extension.ts +724 -0
  297. package/src/pi/session-storage.ts +5 -0
  298. package/src/pi/session.ts +81 -0
  299. package/src/pi/setup.ts +371 -0
  300. package/src/pi/tool-adapter.ts +36 -0
  301. package/src/prompts/context-builder.ts +204 -0
  302. package/src/prompts/disclaimer.ts +9 -0
  303. package/src/prompts/sections.ts +46 -0
  304. package/src/prompts/workflow-prompts.ts +279 -0
  305. package/src/providers/alpha-vantage.ts +292 -0
  306. package/src/providers/coingecko.ts +96 -0
  307. package/src/providers/exa-search.ts +373 -0
  308. package/src/providers/fear-greed.ts +45 -0
  309. package/src/providers/finnhub.ts +124 -0
  310. package/src/providers/fred.ts +83 -0
  311. package/src/providers/index.ts +9 -0
  312. package/src/providers/provider-credential-error.ts +23 -0
  313. package/src/providers/reddit.ts +151 -0
  314. package/src/providers/sec-edgar.ts +96 -0
  315. package/src/providers/twitter.ts +173 -0
  316. package/src/providers/web-search.ts +293 -0
  317. package/src/providers/with-fallback.ts +41 -0
  318. package/src/providers/wrap-provider.ts +64 -0
  319. package/src/providers/yahoo-finance.ts +367 -0
  320. package/src/routing/classify-intent.ts +194 -0
  321. package/src/routing/defaults.ts +29 -0
  322. package/src/routing/entity-extractor.ts +140 -0
  323. package/src/routing/index.ts +26 -0
  324. package/src/routing/router-llm-client.ts +51 -0
  325. package/src/routing/router-prompt.ts +159 -0
  326. package/src/routing/router-types.ts +66 -0
  327. package/src/routing/router.ts +213 -0
  328. package/src/routing/slot-resolver.ts +152 -0
  329. package/src/routing/types.ts +63 -0
  330. package/src/runtime/evidence.ts +77 -0
  331. package/src/runtime/index.ts +55 -0
  332. package/src/runtime/prompt-step.ts +75 -0
  333. package/src/runtime/provider-ids.ts +15 -0
  334. package/src/runtime/provider-tracker.ts +40 -0
  335. package/src/runtime/run-context.ts +22 -0
  336. package/src/runtime/session-coordinator.ts +406 -0
  337. package/src/runtime/tool-defaults-wrapper.ts +35 -0
  338. package/src/runtime/validation.ts +214 -0
  339. package/src/runtime/workflow-events.ts +75 -0
  340. package/src/runtime/workflow-runner.ts +188 -0
  341. package/src/runtime/workflow-types.ts +102 -0
  342. package/src/sentiment/adapters/finnhub.ts +44 -0
  343. package/src/sentiment/adapters/reddit.ts +65 -0
  344. package/src/sentiment/adapters/twitter.ts +36 -0
  345. package/src/sentiment/adapters/web.ts +44 -0
  346. package/src/sentiment/index.ts +58 -0
  347. package/src/sentiment/keywords.ts +9 -0
  348. package/src/sentiment/pipeline.ts +68 -0
  349. package/src/sentiment/scorer.ts +78 -0
  350. package/src/sentiment/store.ts +260 -0
  351. package/src/sentiment/trends.ts +90 -0
  352. package/src/sentiment/types.ts +108 -0
  353. package/src/system-prompt.ts +115 -0
  354. package/src/tool-kit.ts +68 -0
  355. package/src/tools/AGENTS.md +36 -0
  356. package/src/tools/fundamentals/company-overview.ts +54 -0
  357. package/src/tools/fundamentals/comps.ts +156 -0
  358. package/src/tools/fundamentals/dcf.ts +267 -0
  359. package/src/tools/fundamentals/earnings.ts +47 -0
  360. package/src/tools/fundamentals/financials.ts +54 -0
  361. package/src/tools/fundamentals/sec-filings.ts +61 -0
  362. package/src/tools/index.ts +88 -0
  363. package/src/tools/interaction/ask-user.ts +81 -0
  364. package/src/tools/interaction/twitter-login.ts +93 -0
  365. package/src/tools/macro/fear-greed.ts +41 -0
  366. package/src/tools/macro/fred-data.ts +54 -0
  367. package/src/tools/market/crypto-history.ts +51 -0
  368. package/src/tools/market/crypto-price.ts +53 -0
  369. package/src/tools/market/search-ticker.ts +53 -0
  370. package/src/tools/market/stock-history.ts +79 -0
  371. package/src/tools/market/stock-quote.ts +64 -0
  372. package/src/tools/options/greeks.ts +82 -0
  373. package/src/tools/options/option-chain.ts +91 -0
  374. package/src/tools/portfolio/correlation.ts +162 -0
  375. package/src/tools/portfolio/predictions.ts +253 -0
  376. package/src/tools/portfolio/risk-analysis.ts +134 -0
  377. package/src/tools/portfolio/tracker.ts +147 -0
  378. package/src/tools/portfolio/watchlist.ts +153 -0
  379. package/src/tools/sentiment/reddit-sentiment.ts +164 -0
  380. package/src/tools/sentiment/sentiment-summary.ts +256 -0
  381. package/src/tools/sentiment/sentiment-trend.ts +58 -0
  382. package/src/tools/sentiment/twitter-sentiment.ts +96 -0
  383. package/src/tools/sentiment/web-search.ts +150 -0
  384. package/src/tools/sentiment/web-sentiment.ts +76 -0
  385. package/src/tools/technical/backtest.ts +246 -0
  386. package/src/tools/technical/indicators.ts +258 -0
  387. package/src/types/fundamentals.ts +46 -0
  388. package/src/types/index.ts +20 -0
  389. package/src/types/macro.ts +27 -0
  390. package/src/types/market.ts +43 -0
  391. package/src/types/options.ts +35 -0
  392. package/src/types/portfolio.ts +41 -0
  393. package/src/types/sentiment.ts +70 -0
  394. package/src/workflows/compare-assets.ts +39 -0
  395. package/src/workflows/index.ts +4 -0
  396. package/src/workflows/options-screener.ts +49 -0
  397. package/src/workflows/portfolio-builder.ts +52 -0
  398. package/src/workflows/types.ts +4 -0
  399. package/dist/tools/sentiment/news-sentiment.d.ts +0 -7
  400. package/dist/tools/sentiment/news-sentiment.js +0 -55
  401. package/dist/tools/sentiment/news-sentiment.js.map +0 -1
@@ -0,0 +1,724 @@
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
+ classifyIntent,
10
+ createPiAiRouterClient,
11
+ resolveOptionsScreenerSlots,
12
+ resolvePortfolioSlots,
13
+ route as routeLlm,
14
+ } from "../routing/index.js";
15
+ import type {
16
+ RouterInputContext,
17
+ RouterLlmClient,
18
+ RouterOutput,
19
+ } from "../routing/router-types.js";
20
+ import type { CompareAssetsSlots, SlotResolution } from "../routing/types.js";
21
+ import { buildAssumptionsBlockFromRouter } from "../prompts/workflow-prompts.js";
22
+ import {
23
+ buildPortfolioWorkflowDefinition,
24
+ buildOptionsScreenerWorkflowDefinition,
25
+ buildCompareAssetsWorkflowDefinition,
26
+ } from "../workflows/index.js";
27
+ import { getOpenCandleToolDefinitions } from "./tool-adapter.js";
28
+ import { registerAskUserTool } from "../tools/interaction/ask-user.js";
29
+ import { registerTwitterLoginTool } from "../tools/interaction/twitter-login.js";
30
+ import { SessionCoordinator } from "../runtime/session-coordinator.js";
31
+ import {
32
+ getProvider,
33
+ type ProviderId,
34
+ } from "../onboarding/providers.js";
35
+ import {
36
+ loadOnboardingState,
37
+ saveOnboardingState,
38
+ markProviderSnoozed,
39
+ markProviderNeverAsk,
40
+ markWelcomeShown,
41
+ shouldShowWelcome,
42
+ } from "../onboarding/state.js";
43
+ import { parseToolTag, buildSkippedTag, buildConnectedTag } from "../onboarding/tool-tags.js";
44
+ import { resolveCredentialRequired } from "../onboarding/credential-interceptor.js";
45
+ import { createDegradationAccumulator } from "../onboarding/degradation-accumulator.js";
46
+ import { promptUser } from "../onboarding/prompt-user.js";
47
+ import { runProviderConnect } from "../onboarding/connect.js";
48
+ import type { AskUserHandler } from "../types/index.js";
49
+ import { DISCLAIMER_TEXT } from "../prompts/disclaimer.js";
50
+
51
+ export interface OpenCandleExtensionOptions {
52
+ askUserHandler?: AskUserHandler;
53
+ /**
54
+ * Optional router LLM client. When provided, this instance is used instead
55
+ * of the pi-ai-backed default. Intended for tests + offline eval runners.
56
+ */
57
+ routerLlmClient?: RouterLlmClient;
58
+ }
59
+
60
+ export default function openCandleExtension(pi: ExtensionAPI, options?: OpenCandleExtensionOptions): void {
61
+ const coordinator = new SessionCoordinator();
62
+
63
+ // Credential-interception state. Lifetime:
64
+ // `sessionPromptedSet` — cleared on session_start, persists across turns
65
+ // within a session so users don't get re-prompted after picking
66
+ // "continue without".
67
+ // `hardPromptFiredInWorkflow` — reset on turn_start (the nearest clean
68
+ // boundary for a single user request). Enforces the "at most one hard
69
+ // prompt per workflow" cap.
70
+ // `degradationAccumulator` — per-turn record of soft-tier providers that
71
+ // fell back to their keyless alternative. Reset on turn_start; flushed
72
+ // on turn_end via `pi.appendEntry("opencandle-turn-gap", ...)` for
73
+ // session observability. Does NOT pause the workflow or mutate tool
74
+ // results inline — soft-degraded tags remain visible to the LLM so its
75
+ // final answer can surface them in a **Data gaps** section.
76
+ const sessionPromptedSet = new Set<ProviderId>();
77
+ let hardPromptFiredInWorkflow = false;
78
+ const degradationAccumulator = createDegradationAccumulator();
79
+
80
+ // Register tools
81
+ for (const tool of getOpenCandleToolDefinitions()) {
82
+ pi.registerTool(tool);
83
+ }
84
+ registerAskUserTool(pi, options?.askUserHandler);
85
+ registerTwitterLoginTool(pi);
86
+
87
+ // /analyze command
88
+ pi.registerCommand("analyze", {
89
+ description: "Run the multi-analyst OpenCandle workflow for a ticker symbol",
90
+ handler: async (args, ctx) => {
91
+ const symbol = normalizeSymbol(args);
92
+ if (!symbol) {
93
+ ctx.ui.notify("Usage: /analyze <ticker>", "warning");
94
+ return;
95
+ }
96
+ const definition = buildComprehensiveAnalysisDefinition(symbol, { debate: getConfig().debate });
97
+ coordinator.executeWorkflow(pi, definition, ctx);
98
+ },
99
+ });
100
+
101
+ // /setup command — reconfigure the OpenCandle AI model (sign-in / API key).
102
+ // Data providers are configured separately via `/connect`.
103
+ pi.registerCommand("setup", {
104
+ description: "Reconfigure the OpenCandle AI model (sign-in or API key)",
105
+ handler: async (_args, ctx) => {
106
+ const result = await coordinator.runSetup(pi, ctx, { mode: "manual" });
107
+ if (result === "ready") {
108
+ ctx.ui.notify("OpenCandle setup complete.", "info");
109
+ }
110
+ },
111
+ });
112
+
113
+ // /connect command — reconfigurable sectioned setup for data providers.
114
+ // `/connect` with no args opens a picker listing all providers.
115
+ // `/connect <alias|id|category>` routes to a specific provider (or a
116
+ // sub-picker for multi-provider categories like "search").
117
+ pi.registerCommand("connect", {
118
+ description: "Connect a data provider (Alpha Vantage, FRED, Finnhub, Brave, Exa)",
119
+ handler: async (args, ctx) => {
120
+ const { listAllProviders, resolveProviderFromArgument, hasCredential } = await import(
121
+ "../onboarding/providers.js"
122
+ );
123
+
124
+ const formatState = (id: ProviderId): string => {
125
+ const state = loadOnboardingState().providers[id];
126
+ if (state?.status === "completed") return "Configured";
127
+ if (state?.status === "snoozed") {
128
+ return `Snoozed until ${state.snoozeUntil.slice(0, 10)}`;
129
+ }
130
+ if (state?.status === "never_ask") return "Never-ask";
131
+ if (hasCredential(id)) return "Configured (via env)";
132
+ return "Not configured";
133
+ };
134
+
135
+ const pickProvider = async (
136
+ providers: readonly ReturnType<typeof getProvider>[],
137
+ ): Promise<ProviderId | undefined> => {
138
+ const labels = providers.map(
139
+ (p) => `${p.displayName} — ${p.unlocks.slice(0, 2).join(", ")} [${formatState(p.id)}]`,
140
+ );
141
+ const choice = await ctx.ui.select("Which provider would you like to connect?", labels);
142
+ if (choice === undefined) return undefined;
143
+ const index = labels.indexOf(choice);
144
+ return providers[index]?.id;
145
+ };
146
+
147
+ const trimmed = args.trim();
148
+ let targetId: ProviderId | undefined;
149
+
150
+ if (trimmed === "") {
151
+ // Bare /connect → full picker.
152
+ targetId = await pickProvider(listAllProviders());
153
+ } else {
154
+ const resolved = resolveProviderFromArgument(trimmed);
155
+ if (!resolved) {
156
+ const all = listAllProviders()
157
+ .map((p) => ` ${p.displayName} (${p.aliases.join(", ")})`)
158
+ .join("\n");
159
+ ctx.ui.notify(
160
+ `Unknown provider: "${trimmed}". Available:\n${all}`,
161
+ "warning",
162
+ );
163
+ return;
164
+ }
165
+ if (Array.isArray(resolved)) {
166
+ // Multi-provider category — show a sub-picker.
167
+ targetId = await pickProvider(resolved as readonly ReturnType<typeof getProvider>[]);
168
+ } else {
169
+ targetId = (resolved as ReturnType<typeof getProvider>).id;
170
+ }
171
+ }
172
+
173
+ if (!targetId) {
174
+ ctx.ui.notify("Connect cancelled.", "info");
175
+ return;
176
+ }
177
+
178
+ const result = await runProviderConnect(ctx, targetId);
179
+ if (result.status === "connected") {
180
+ ctx.ui.notify(`${getProvider(targetId).displayName} is now connected.`, "info");
181
+ } else if (result.status === "cancelled") {
182
+ ctx.ui.notify("Connect cancelled.", "info");
183
+ }
184
+ // "blocked_by_env" already notifies from inside runProviderConnect.
185
+ },
186
+ });
187
+
188
+ // Session start
189
+ pi.on("session_start", async (_event, ctx) => {
190
+ coordinator.initSession(ctx.sessionManager.getSessionId());
191
+ sessionPromptedSet.clear();
192
+ hardPromptFiredInWorkflow = false;
193
+
194
+ if (!ctx.hasUI) return;
195
+ // Pin the user-facing disclaimer in the UI footer for the entire session.
196
+ // Using `setStatus` keeps it always visible to the user without ever
197
+ // entering the LLM's conversation context (unlike `sendMessage`, which Pi
198
+ // reinjects as a `role:"user"` message every turn).
199
+ ctx.ui.setStatus("opencandle-disclaimer", DISCLAIMER_TEXT);
200
+
201
+ const result = await coordinator.runSetup(pi, ctx, { mode: "startup" });
202
+ if (result === "shutdown") {
203
+ return;
204
+ }
205
+
206
+ // One-shot welcome on the very first session (gated on welcomeShownAt).
207
+ // Uses `pi.sendMessage` with `display: true` so the welcome lands in the
208
+ // chat transcript (persistent, scrollable) rather than a transient
209
+ // `ctx.ui.notify` banner.
210
+ const state = loadOnboardingState();
211
+ if (shouldShowWelcome(state, ctx.hasUI)) {
212
+ const WELCOME_BODY =
213
+ "Welcome to OpenCandle. I'm your AI copilot for market analysis.\n\n" +
214
+ "Try something like:\n" +
215
+ " • analyze NVDA — full deep-dive on a ticker\n" +
216
+ " • quote TSLA — just the price and daily move\n" +
217
+ " • how's bitcoin? — crypto\n" +
218
+ " • what's r/wallstreetbets saying about META? — social sentiment\n\n" +
219
+ "You're running with just an LLM right now, which covers most of what\n" +
220
+ "people want. For fundamentals, economic data, or premium news you'll\n" +
221
+ "need a few free API keys — I'll offer to help when they'd actually\n" +
222
+ "make a difference, or run /connect anytime.";
223
+
224
+ pi.sendMessage({
225
+ customType: "opencandle-welcome",
226
+ content: [{ type: "text", text: WELCOME_BODY }],
227
+ display: true,
228
+ });
229
+ saveOnboardingState(markWelcomeShown(state));
230
+ } else {
231
+ ctx.ui.notify(
232
+ "OpenCandle ready. Try /analyze NVDA or /connect to add data providers.",
233
+ "info",
234
+ );
235
+ }
236
+ });
237
+
238
+ // Reset the per-workflow prompt cap AND the degradation accumulator on each
239
+ // new turn. One user request = one workflow invocation = at most one hard
240
+ // prompt and one combined soft-degradation annotation.
241
+ pi.on("turn_start", async () => {
242
+ hardPromptFiredInWorkflow = false;
243
+ degradationAccumulator.reset();
244
+ });
245
+
246
+ // At turn_end, flush the soft-degradation accumulator to a session entry so
247
+ // downstream consumers (UI renderers, debug inspectors, later turns) can see
248
+ // which soft providers fell back during this turn. The LLM has already
249
+ // emitted its answer by the time this fires, so the per-tool-result
250
+ // soft-degraded tags remain the primary carrier for the in-turn gap note.
251
+ //
252
+ // Also persist a per-turn disclaimer entry via `appendEntry`. `CustomEntry`
253
+ // is NOT sent to LLM context (unlike `sendMessage`/`CustomMessage`, which
254
+ // Pi's `convertToLlm` reinjects as a `role:"user"` message), so this keeps
255
+ // the stance instruction-free while still producing a session record that
256
+ // downstream renderers / exporters / tests can surface. The always-visible
257
+ // footer status pinned at session_start is the primary user-visible channel.
258
+ pi.on("turn_end", async (event) => {
259
+ const msg = event.message;
260
+ const isFinalAssistantTurn =
261
+ msg.role === "assistant" && msg.stopReason === "stop";
262
+ if (isFinalAssistantTurn) {
263
+ pi.appendEntry("opencandle-disclaimer", { text: DISCLAIMER_TEXT });
264
+ }
265
+
266
+ if (degradationAccumulator.isEmpty()) return;
267
+ const state = loadOnboardingState();
268
+ const annotation = degradationAccumulator.buildCombinedAnnotation(state);
269
+ if (annotation !== null) {
270
+ pi.appendEntry("opencandle-turn-gap", { annotation });
271
+ }
272
+ degradationAccumulator.reset();
273
+ });
274
+
275
+ // Intercept tool results for credential-required and soft-degraded tags.
276
+ pi.on("tool_result", async (event, ctx) => {
277
+ // First pass: record any soft-degradation tags in the per-turn accumulator
278
+ // WITHOUT mutating the tool result (the inline tag stays visible to the
279
+ // LLM so it can surface the gap in its final answer). Multiple tags per
280
+ // tool-result block are deduplicated by the accumulator's Set.
281
+ for (const block of event.content) {
282
+ if (block.type !== "text") continue;
283
+ const parsed = parseToolTag(block.text);
284
+ if (parsed?.kind === "soft_degraded") {
285
+ degradationAccumulator.record(parsed.provider);
286
+ }
287
+ }
288
+
289
+ // Second pass: look for a credential-required tag; on match, run the
290
+ // interception decision and either replace the tool result or prompt
291
+ // the user. Only the first credential_required tag in the content list
292
+ // is acted on — subsequent hard-tier prompts are silenced by the
293
+ // per-workflow cap at the decision-function level.
294
+ for (const block of event.content) {
295
+ if (block.type !== "text") continue;
296
+ const parsed = parseToolTag(block.text);
297
+ if (!parsed || parsed.kind !== "credential_required") continue;
298
+
299
+ const state = loadOnboardingState();
300
+ const action = resolveCredentialRequired({
301
+ provider: parsed.provider,
302
+ reason: parsed.reason,
303
+ state,
304
+ sessionPromptedSet,
305
+ hardPromptFiredInWorkflow,
306
+ now: new Date(),
307
+ });
308
+
309
+ if (action.action === "skip") {
310
+ // Replace content with a skipped placeholder so the LLM sees a
311
+ // neutral gap note instead of the credential-required tag.
312
+ const descriptor = getProvider(parsed.provider);
313
+ const remediation = action.silenced
314
+ ? `${action.remediation} (silenced)`
315
+ : action.remediation;
316
+ const tag = buildSkippedTag({
317
+ provider: parsed.provider,
318
+ reason: "credential_not_provided",
319
+ remediation,
320
+ silenced: action.silenced,
321
+ });
322
+ return {
323
+ content: [
324
+ {
325
+ type: "text",
326
+ text:
327
+ `${tag}\n\n${descriptor.displayName} data was not fetched for this request. ` +
328
+ (action.silenced
329
+ ? `You previously asked not to be reminded about this provider.`
330
+ : `To unlock ${descriptor.unlocks.join(", ")}, ${action.remediation}.`),
331
+ },
332
+ ],
333
+ };
334
+ }
335
+
336
+ // action === "prompt": pause and ask the user via promptUser.
337
+ const descriptor = getProvider(parsed.provider);
338
+ const connectLabel = `Connect now — ${descriptor.instructionsHint}`;
339
+ const continueLabel =
340
+ descriptor.fallbackDescription
341
+ ? `Continue with ${descriptor.fallbackDescription} for this run`
342
+ : `Continue without ${descriptor.displayName} for this run`;
343
+ const snoozeLabel = `Snooze ${descriptor.snoozeDurationDays} days`;
344
+ const neverLabel = `Never ask again`;
345
+ const questionBody =
346
+ `${descriptor.displayName} unlocks ${descriptor.unlocks.join(", ")}. ` +
347
+ `Free signup takes about 30 seconds. How would you like to proceed?`;
348
+
349
+ // Mark that a hard-tier prompt has now fired in this workflow (for the cap).
350
+ if (descriptor.tier === "hard") {
351
+ hardPromptFiredInWorkflow = true;
352
+ }
353
+ sessionPromptedSet.add(parsed.provider);
354
+
355
+ const promptResult = await promptUser(
356
+ ctx,
357
+ {
358
+ question: questionBody,
359
+ questionType: "select",
360
+ options: [connectLabel, continueLabel, snoozeLabel, neverLabel],
361
+ },
362
+ options?.askUserHandler,
363
+ );
364
+
365
+ if (promptResult.cancelled) {
366
+ // Treat cancel like "continue without".
367
+ return {
368
+ content: [
369
+ {
370
+ type: "text",
371
+ text:
372
+ `${buildSkippedTag({
373
+ provider: parsed.provider,
374
+ reason: "credential_not_provided",
375
+ remediation: `run /connect ${descriptor.aliases[0] ?? descriptor.id} to unlock`,
376
+ })}\n\nPrompt was cancelled.`,
377
+ },
378
+ ],
379
+ };
380
+ }
381
+
382
+ const answer = promptResult.answer ?? "";
383
+
384
+ if (answer.startsWith("Connect")) {
385
+ const connectResult = await runProviderConnect(ctx, parsed.provider);
386
+ if (connectResult.status === "connected") {
387
+ // Pi has no tool re-dispatch API — emit a CONNECTED placeholder so
388
+ // the LLM knows the key was just saved and can retry on the next
389
+ // turn (or use whatever partial data is in context).
390
+ return {
391
+ content: [
392
+ {
393
+ type: "text",
394
+ text:
395
+ `${buildConnectedTag({ provider: parsed.provider })}\n\n` +
396
+ `${descriptor.displayName} was just connected. Please re-run the previous request to fetch the data now that the credential is available.`,
397
+ },
398
+ ],
399
+ };
400
+ }
401
+ // Cancelled, blocked-by-env, or invalid_key: fall through to skipped
402
+ // with a result-specific explanation so the LLM can describe what
403
+ // just happened in its final answer.
404
+ const connectOutcomeDescription =
405
+ connectResult.status === "blocked_by_env"
406
+ ? "blocked by an existing environment variable"
407
+ : connectResult.status === "invalid_key"
408
+ ? `rejected by ${descriptor.displayName} (the key was invalid and nothing was saved)`
409
+ : "cancelled";
410
+ return {
411
+ content: [
412
+ {
413
+ type: "text",
414
+ text:
415
+ `${buildSkippedTag({
416
+ provider: parsed.provider,
417
+ reason: "credential_not_provided",
418
+ remediation: `run /connect ${descriptor.aliases[0] ?? descriptor.id} to unlock`,
419
+ })}\n\n${descriptor.displayName} connect was ${connectOutcomeDescription}.`,
420
+ },
421
+ ],
422
+ };
423
+ }
424
+
425
+ if (answer.startsWith("Snooze")) {
426
+ saveOnboardingState(
427
+ markProviderSnoozed(state, parsed.provider, descriptor.snoozeDurationDays),
428
+ );
429
+ } else if (answer.startsWith("Never")) {
430
+ saveOnboardingState(markProviderNeverAsk(state, parsed.provider));
431
+ }
432
+ // "Continue" / fallthrough: no state mutation, just skip.
433
+ const silenced = answer.startsWith("Never");
434
+ const remediation = silenced
435
+ ? `run /connect ${descriptor.aliases[0] ?? descriptor.id} to unlock (silenced)`
436
+ : `run /connect ${descriptor.aliases[0] ?? descriptor.id} to unlock`;
437
+ return {
438
+ content: [
439
+ {
440
+ type: "text",
441
+ text:
442
+ `${buildSkippedTag({
443
+ provider: parsed.provider,
444
+ reason: "credential_not_provided",
445
+ remediation,
446
+ silenced,
447
+ })}\n\n${descriptor.displayName} data was omitted per your choice.`,
448
+ },
449
+ ],
450
+ };
451
+ }
452
+
453
+ // No OpenCandle tag in this tool result — pass through.
454
+ return undefined;
455
+ });
456
+
457
+ // Input handling — branches on OPENCANDLE_ROUTER_MODE.
458
+ pi.on("input", async (event, ctx) => {
459
+ if (event.source === "extension") return;
460
+
461
+ // Check for comprehensive analysis pattern — same in both modes.
462
+ const analysis = isAnalysisRequest(event.text);
463
+ if (analysis.match && analysis.symbol) {
464
+ const definition = buildComprehensiveAnalysisDefinition(analysis.symbol, { debate: getConfig().debate });
465
+ coordinator.executeWorkflow(pi, definition, ctx);
466
+ return { action: "handled" };
467
+ }
468
+
469
+ const mode = getConfig().routerMode;
470
+ if (mode === "llm") {
471
+ const dispatched = await handleLlmRouterTurn(event.text, ctx);
472
+ // Dispatched a workflow → the original user turn is now represented by
473
+ // the workflow's queued prompts; tell Pi not to also forward it.
474
+ // Fallback path (no dispatch) → let Pi pass the user turn through to the
475
+ // main agent, which will run under the router-supplied fallback context.
476
+ return dispatched ? { action: "handled" } : undefined;
477
+ }
478
+
479
+ // --- rules mode (default) ---
480
+ // Extract and persist user preferences (legacy regex path)
481
+ coordinator.extractAndStorePreferences(event.text);
482
+ const storage = coordinator.getStorage();
483
+ const workflowPrefs = storage?.getWorkflowPreferences("global") ?? {};
484
+
485
+ // Classify intent
486
+ const classification = classifyIntent(event.text);
487
+
488
+ if (classification.workflow === "portfolio_builder") {
489
+ const resolution = resolvePortfolioSlots(classification.entities, workflowPrefs);
490
+ coordinator.recordWorkflowRun("portfolio_builder", classification.entities, resolution.resolved, resolution.defaultsUsed);
491
+ pi.appendEntry("opencandle-workflow", { workflow: "portfolio_builder", entities: classification.entities, resolved: resolution.resolved });
492
+ const definition = buildPortfolioWorkflowDefinition(resolution);
493
+ coordinator.executeWorkflow(pi, definition, ctx);
494
+ return { action: "handled" };
495
+ }
496
+
497
+ if (classification.workflow === "options_screener") {
498
+ const resolution = resolveOptionsScreenerSlots(classification.entities, workflowPrefs);
499
+ if (resolution.missingRequired.length === 0) {
500
+ coordinator.recordWorkflowRun("options_screener", classification.entities, resolution.resolved, resolution.defaultsUsed);
501
+ pi.appendEntry("opencandle-workflow", { workflow: "options_screener", entities: classification.entities, resolved: resolution.resolved });
502
+ const definition = buildOptionsScreenerWorkflowDefinition(resolution);
503
+ coordinator.executeWorkflow(pi, definition, ctx);
504
+ return { action: "handled" };
505
+ }
506
+ }
507
+
508
+ if (classification.workflow === "compare_assets" && classification.entities.symbols.length >= 2) {
509
+ const resolution: SlotResolution<CompareAssetsSlots> = {
510
+ resolved: { symbols: classification.entities.symbols },
511
+ sources: { symbols: "user" },
512
+ defaultsUsed: [],
513
+ missingRequired: [],
514
+ };
515
+ coordinator.recordWorkflowRun("compare_assets", classification.entities, resolution.resolved, resolution.defaultsUsed);
516
+ pi.appendEntry("opencandle-workflow", { workflow: "compare_assets", symbols: classification.entities.symbols });
517
+ const definition = buildCompareAssetsWorkflowDefinition(resolution);
518
+ coordinator.executeWorkflow(pi, definition, ctx);
519
+ return { action: "handled" };
520
+ }
521
+ });
522
+
523
+ /**
524
+ * LLM-mode input handler. In this mode `classifyIntent` and
525
+ * `extractPreferences` are NOT called — the router is the single source of
526
+ * classification + preference extraction. Mirrors rule-mode dispatch for
527
+ * identified workflows; for `fallback` turns, stashes a fallback context
528
+ * for the next `before_agent_start` to inject.
529
+ */
530
+ async function handleLlmRouterTurn(
531
+ text: string,
532
+ ctx: Parameters<Parameters<ExtensionAPI["on"]>[1]>[1],
533
+ ): Promise<boolean> {
534
+ const storage = coordinator.getStorage();
535
+ const { profileSnapshot, recentWorkflowRuns, priorTurns } =
536
+ coordinator.buildRouterContextBase(ctx.sessionManager);
537
+ // priorTurns is not scrubbed for /forget — tracked in proposal.md follow-ups.
538
+ const input: RouterInputContext = {
539
+ text,
540
+ priorTurns,
541
+ profileSnapshot,
542
+ recentWorkflowRuns,
543
+ };
544
+
545
+ const client = options?.routerLlmClient ?? resolveRouterLlmClient(ctx);
546
+ if (!client) {
547
+ pi.appendEntry("opencandle-router-error", {
548
+ reason: "no_llm_client_available",
549
+ text,
550
+ });
551
+ return false;
552
+ }
553
+
554
+ let output: RouterOutput;
555
+ try {
556
+ output = await routeLlm(input, client);
557
+ } catch (err) {
558
+ pi.appendEntry("opencandle-router-error", {
559
+ reason: "route_failed",
560
+ text,
561
+ message: err instanceof Error ? err.message : String(err),
562
+ });
563
+ return false;
564
+ }
565
+
566
+ pi.appendEntry("opencandle-router", { output });
567
+
568
+ // Preference writes: HIGH-confidence only. Medium/low are logged for
569
+ // observability even when no storage is available.
570
+ const dropped: typeof output.preference_updates = [];
571
+ for (const pref of output.preference_updates) {
572
+ if (pref.confidence === "high") {
573
+ storage?.upsertPreference({
574
+ key: pref.key,
575
+ valueJson: JSON.stringify(pref.value),
576
+ confidence: pref.confidence,
577
+ source: pref.source,
578
+ });
579
+ } else {
580
+ dropped.push(pref);
581
+ }
582
+ }
583
+ if (dropped.length > 0) {
584
+ pi.appendEntry("opencandle-router-prefs-dropped", { dropped });
585
+ }
586
+
587
+ // Workflow dispatch for recognised workflows.
588
+ if (output.route === "workflow" && output.workflow) {
589
+ return dispatchRouterWorkflow(output, ctx);
590
+ }
591
+
592
+ // Fallback: record the turn and stash the fallback context for the
593
+ // upcoming `before_agent_start` hook to render into the system prompt.
594
+ coordinator.recordWorkflowRun(
595
+ "fallback",
596
+ output.entities,
597
+ Object.fromEntries(Object.entries(output.slots).map(([k, v]) => [k, v.value])),
598
+ [],
599
+ "fallback",
600
+ );
601
+
602
+ const assumptionsBlock = buildAssumptionsBlockFromRouter(output.slots);
603
+ coordinator.setPendingFallbackContext({
604
+ assumptionsBlock,
605
+ missingRequired: output.missing_required,
606
+ extraContext: output.entities.symbols.length > 0
607
+ ? `Router-extracted symbols: ${output.entities.symbols.join(", ")}.`
608
+ : undefined,
609
+ });
610
+ return false;
611
+ }
612
+
613
+ function dispatchRouterWorkflow(
614
+ output: RouterOutput,
615
+ ctx: Parameters<Parameters<ExtensionAPI["on"]>[1]>[1],
616
+ ): boolean {
617
+ const workflow = output.workflow!;
618
+ const storage = coordinator.getStorage();
619
+ const workflowPrefs = storage?.getWorkflowPreferences("global") ?? {};
620
+
621
+ if (workflow === "portfolio_builder") {
622
+ const resolution = resolvePortfolioSlots(output.entities, workflowPrefs);
623
+ coordinator.recordWorkflowRun(
624
+ "portfolio_builder",
625
+ output.entities,
626
+ resolution.resolved,
627
+ resolution.defaultsUsed,
628
+ "workflow",
629
+ );
630
+ pi.appendEntry("opencandle-workflow", {
631
+ workflow: "portfolio_builder",
632
+ entities: output.entities,
633
+ resolved: resolution.resolved,
634
+ });
635
+ const definition = buildPortfolioWorkflowDefinition(resolution);
636
+ coordinator.executeWorkflow(pi, definition, ctx);
637
+ return true;
638
+ }
639
+ if (workflow === "options_screener") {
640
+ const resolution = resolveOptionsScreenerSlots(output.entities, workflowPrefs);
641
+ // Router may emit missing_required; main agent handles via ask_user.
642
+ // Still dispatch the workflow when symbol is present.
643
+ if (resolution.missingRequired.length === 0) {
644
+ coordinator.recordWorkflowRun(
645
+ "options_screener",
646
+ output.entities,
647
+ resolution.resolved,
648
+ resolution.defaultsUsed,
649
+ "workflow",
650
+ );
651
+ pi.appendEntry("opencandle-workflow", {
652
+ workflow: "options_screener",
653
+ entities: output.entities,
654
+ resolved: resolution.resolved,
655
+ });
656
+ const definition = buildOptionsScreenerWorkflowDefinition(resolution);
657
+ coordinator.executeWorkflow(pi, definition, ctx);
658
+ return true;
659
+ }
660
+ // Missing required symbol — treat as fallback with ask_user directive.
661
+ }
662
+ if (workflow === "compare_assets" && output.entities.symbols.length >= 2) {
663
+ const resolution: SlotResolution<CompareAssetsSlots> = {
664
+ resolved: { symbols: output.entities.symbols },
665
+ sources: { symbols: "user" },
666
+ defaultsUsed: [],
667
+ missingRequired: [],
668
+ };
669
+ coordinator.recordWorkflowRun(
670
+ "compare_assets",
671
+ output.entities,
672
+ resolution.resolved,
673
+ [],
674
+ "workflow",
675
+ );
676
+ pi.appendEntry("opencandle-workflow", {
677
+ workflow: "compare_assets",
678
+ symbols: output.entities.symbols,
679
+ });
680
+ const definition = buildCompareAssetsWorkflowDefinition(resolution);
681
+ coordinator.executeWorkflow(pi, definition, ctx);
682
+ return true;
683
+ }
684
+
685
+ // single_asset_analysis / watchlist / general_qa + any workflow with
686
+ // unmet required slots: fall through to fallback handling so the main
687
+ // agent still gets an Assumptions block + ask_user directive.
688
+ coordinator.recordWorkflowRun(
689
+ "fallback",
690
+ output.entities,
691
+ Object.fromEntries(Object.entries(output.slots).map(([k, v]) => [k, v.value])),
692
+ [],
693
+ "fallback",
694
+ );
695
+ const assumptionsBlock = buildAssumptionsBlockFromRouter(output.slots);
696
+ coordinator.setPendingFallbackContext({
697
+ assumptionsBlock,
698
+ missingRequired: output.missing_required,
699
+ extraContext: `Router classified as ${workflow} but declined to dispatch. Symbols: ${output.entities.symbols.join(", ") || "(none)"}.`,
700
+ });
701
+ return false;
702
+ }
703
+
704
+ function resolveRouterLlmClient(
705
+ ctx: Parameters<Parameters<ExtensionAPI["on"]>[1]>[1],
706
+ ): RouterLlmClient | null {
707
+ // `ctx.model` is the currently selected pi-ai model. When unset (no auth
708
+ // configured yet), we skip the router and the main agent will run with
709
+ // its default unrouted flow (legacy rules path is the safer default).
710
+ const model = (ctx as { model?: unknown }).model;
711
+ if (!model) return null;
712
+ // biome-ignore lint/suspicious/noExplicitAny: Pi typings keep Model generic
713
+ return createPiAiRouterClient(model as any);
714
+ }
715
+
716
+ // System prompt assembly — delegate to coordinator. When a fallback context
717
+ // is pending (router-mode fallback turns), inject it into the prompt.
718
+ pi.on("before_agent_start", async (event) => {
719
+ const fallbackContext = coordinator.consumePendingFallbackContext() ?? undefined;
720
+ return {
721
+ systemPrompt: coordinator.buildSystemPrompt(event.systemPrompt, undefined, fallbackContext),
722
+ };
723
+ });
724
+ }