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