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.
- package/LICENSE +1 -1
- package/README.md +106 -14
- package/assets/logo.svg +187 -0
- package/dist/cli.d.ts +1 -1
- package/dist/cli.js +40 -3
- package/dist/cli.js.map +1 -1
- package/dist/config.d.ts +25 -0
- package/dist/config.js +72 -0
- package/dist/config.js.map +1 -1
- package/dist/infra/browser.d.ts +11 -3
- package/dist/infra/browser.js +2 -1
- package/dist/infra/browser.js.map +1 -1
- package/dist/infra/native-dependencies.d.ts +1 -0
- package/dist/infra/native-dependencies.js +10 -0
- package/dist/infra/native-dependencies.js.map +1 -0
- package/dist/infra/node-version.d.ts +2 -0
- package/dist/infra/node-version.js +23 -0
- package/dist/infra/node-version.js.map +1 -0
- package/dist/infra/rate-limiter.d.ts +4 -0
- package/dist/infra/rate-limiter.js +5 -1
- package/dist/infra/rate-limiter.js.map +1 -1
- package/dist/memory/index.d.ts +2 -0
- package/dist/memory/index.js +1 -0
- package/dist/memory/index.js.map +1 -1
- package/dist/memory/manager.d.ts +9 -0
- package/dist/memory/manager.js +28 -11
- package/dist/memory/manager.js.map +1 -1
- package/dist/memory/sqlite.js +42 -4
- package/dist/memory/sqlite.js.map +1 -1
- package/dist/memory/storage.d.ts +7 -0
- package/dist/memory/storage.js +3 -3
- package/dist/memory/storage.js.map +1 -1
- package/dist/memory/tool-defaults.d.ts +8 -0
- package/dist/memory/tool-defaults.js +59 -0
- package/dist/memory/tool-defaults.js.map +1 -0
- package/dist/memory/types.js +4 -0
- package/dist/memory/types.js.map +1 -1
- package/dist/onboarding/connect.d.ts +13 -1
- package/dist/onboarding/connect.js +21 -10
- package/dist/onboarding/connect.js.map +1 -1
- package/dist/onboarding/prompt-user.d.ts +1 -1
- package/dist/onboarding/providers.d.ts +7 -0
- package/dist/onboarding/providers.js +6 -3
- package/dist/onboarding/providers.js.map +1 -1
- package/dist/onboarding/tool-helpers.d.ts +1 -1
- package/dist/pi/opencandle-extension.d.ts +7 -1
- package/dist/pi/opencandle-extension.js +391 -21
- package/dist/pi/opencandle-extension.js.map +1 -1
- package/dist/pi/session-storage.d.ts +2 -0
- package/dist/pi/session-storage.js +5 -0
- package/dist/pi/session-storage.js.map +1 -0
- package/dist/pi/session.d.ts +4 -1
- package/dist/pi/session.js +25 -3
- package/dist/pi/session.js.map +1 -1
- package/dist/pi/setup.d.ts +1 -1
- package/dist/pi/setup.js +11 -1
- package/dist/pi/setup.js.map +1 -1
- package/dist/pi/tool-adapter.d.ts +2 -2
- package/dist/pi/tool-adapter.js +14 -1
- package/dist/pi/tool-adapter.js.map +1 -1
- package/dist/prompts/context-builder.d.ts +40 -3
- package/dist/prompts/context-builder.js +140 -19
- package/dist/prompts/context-builder.js.map +1 -1
- package/dist/prompts/disclaimer.d.ts +6 -0
- package/dist/prompts/disclaimer.js +9 -0
- package/dist/prompts/disclaimer.js.map +1 -0
- package/dist/prompts/policy-cards.d.ts +13 -0
- package/dist/prompts/policy-cards.js +197 -0
- package/dist/prompts/policy-cards.js.map +1 -0
- package/dist/prompts/sections.js +3 -3
- package/dist/prompts/sections.js.map +1 -1
- package/dist/prompts/workflow-prompts.d.ts +8 -0
- package/dist/prompts/workflow-prompts.js +208 -22
- package/dist/prompts/workflow-prompts.js.map +1 -1
- package/dist/providers/alpha-vantage.js +23 -1
- package/dist/providers/alpha-vantage.js.map +1 -1
- package/dist/providers/sec-edgar.d.ts +8 -1
- package/dist/providers/sec-edgar.js +172 -5
- package/dist/providers/sec-edgar.js.map +1 -1
- package/dist/providers/yahoo-finance.d.ts +2 -0
- package/dist/providers/yahoo-finance.js +203 -35
- package/dist/providers/yahoo-finance.js.map +1 -1
- package/dist/routing/classify-intent.d.ts +3 -0
- package/dist/routing/classify-intent.js +82 -3
- package/dist/routing/classify-intent.js.map +1 -1
- package/dist/routing/defaults.js +4 -4
- package/dist/routing/defaults.js.map +1 -1
- package/dist/routing/entity-extractor.d.ts +1 -0
- package/dist/routing/entity-extractor.js +158 -12
- package/dist/routing/entity-extractor.js.map +1 -1
- package/dist/routing/index.d.ts +10 -0
- package/dist/routing/index.js +7 -0
- package/dist/routing/index.js.map +1 -1
- package/dist/routing/legacy-rule-router.d.ts +9 -0
- package/dist/routing/legacy-rule-router.js +12 -0
- package/dist/routing/legacy-rule-router.js.map +1 -0
- package/dist/routing/planning.d.ts +54 -0
- package/dist/routing/planning.js +531 -0
- package/dist/routing/planning.js.map +1 -0
- package/dist/routing/route-manifest.d.ts +35 -0
- package/dist/routing/route-manifest.js +221 -0
- package/dist/routing/route-manifest.js.map +1 -0
- package/dist/routing/router-llm-client.d.ts +11 -0
- package/dist/routing/router-llm-client.js +42 -0
- package/dist/routing/router-llm-client.js.map +1 -0
- package/dist/routing/router-prompt.d.ts +2 -0
- package/dist/routing/router-prompt.js +141 -0
- package/dist/routing/router-prompt.js.map +1 -0
- package/dist/routing/router-types.d.ts +71 -0
- package/dist/routing/router-types.js +2 -0
- package/dist/routing/router-types.js.map +1 -0
- package/dist/routing/router.d.ts +11 -0
- package/dist/routing/router.js +638 -0
- package/dist/routing/router.js.map +1 -0
- package/dist/routing/slot-resolver.js +46 -6
- package/dist/routing/slot-resolver.js.map +1 -1
- package/dist/routing/turn-context.d.ts +44 -0
- package/dist/routing/turn-context.js +45 -0
- package/dist/routing/turn-context.js.map +1 -0
- package/dist/routing/types.d.ts +13 -1
- package/dist/runtime/answer-contracts.d.ts +82 -0
- package/dist/runtime/answer-contracts.js +414 -0
- package/dist/runtime/answer-contracts.js.map +1 -0
- package/dist/runtime/artifact-contracts.d.ts +14 -0
- package/dist/runtime/artifact-contracts.js +57 -0
- package/dist/runtime/artifact-contracts.js.map +1 -0
- package/dist/runtime/planning-evidence.d.ts +99 -0
- package/dist/runtime/planning-evidence.js +445 -0
- package/dist/runtime/planning-evidence.js.map +1 -0
- package/dist/runtime/session-coordinator.d.ts +81 -3
- package/dist/runtime/session-coordinator.js +201 -17
- package/dist/runtime/session-coordinator.js.map +1 -1
- package/dist/runtime/tool-defaults-wrapper.d.ts +3 -0
- package/dist/runtime/tool-defaults-wrapper.js +25 -0
- package/dist/runtime/tool-defaults-wrapper.js.map +1 -0
- package/dist/sentiment/store.js +5 -0
- package/dist/sentiment/store.js.map +1 -1
- package/dist/system-prompt.js +23 -12
- package/dist/system-prompt.js.map +1 -1
- package/dist/tool-kit.d.ts +4 -4
- package/dist/tools/fundamentals/company-overview.d.ts +1 -1
- package/dist/tools/fundamentals/company-overview.js +1 -1
- package/dist/tools/fundamentals/company-overview.js.map +1 -1
- package/dist/tools/fundamentals/comps.d.ts +1 -1
- package/dist/tools/fundamentals/comps.js +1 -1
- package/dist/tools/fundamentals/comps.js.map +1 -1
- package/dist/tools/fundamentals/dcf.d.ts +1 -1
- package/dist/tools/fundamentals/dcf.js +1 -1
- package/dist/tools/fundamentals/dcf.js.map +1 -1
- package/dist/tools/fundamentals/earnings.d.ts +1 -1
- package/dist/tools/fundamentals/earnings.js +1 -1
- package/dist/tools/fundamentals/earnings.js.map +1 -1
- package/dist/tools/fundamentals/financials.d.ts +1 -1
- package/dist/tools/fundamentals/financials.js +1 -1
- package/dist/tools/fundamentals/financials.js.map +1 -1
- package/dist/tools/fundamentals/sec-filings.d.ts +2 -1
- package/dist/tools/fundamentals/sec-filings.js +19 -2
- package/dist/tools/fundamentals/sec-filings.js.map +1 -1
- package/dist/tools/index.d.ts +29 -1
- package/dist/tools/index.js +30 -0
- package/dist/tools/index.js.map +1 -1
- package/dist/tools/interaction/ask-user.d.ts +1 -1
- package/dist/tools/interaction/twitter-login.d.ts +1 -1
- package/dist/tools/macro/fear-greed.d.ts +1 -1
- package/dist/tools/macro/fear-greed.js +1 -1
- package/dist/tools/macro/fear-greed.js.map +1 -1
- package/dist/tools/macro/fred-data.d.ts +1 -1
- package/dist/tools/macro/fred-data.js +29 -5
- package/dist/tools/macro/fred-data.js.map +1 -1
- package/dist/tools/market/crypto-history.d.ts +1 -1
- package/dist/tools/market/crypto-history.js +18 -2
- package/dist/tools/market/crypto-history.js.map +1 -1
- package/dist/tools/market/crypto-price.d.ts +1 -1
- package/dist/tools/market/crypto-price.js +1 -1
- package/dist/tools/market/crypto-price.js.map +1 -1
- package/dist/tools/market/search-ticker.d.ts +1 -1
- package/dist/tools/market/search-ticker.js +1 -1
- package/dist/tools/market/search-ticker.js.map +1 -1
- package/dist/tools/market/stock-history.d.ts +1 -1
- package/dist/tools/market/stock-history.js +1 -1
- package/dist/tools/market/stock-history.js.map +1 -1
- package/dist/tools/market/stock-quote.d.ts +1 -1
- package/dist/tools/market/stock-quote.js +1 -1
- package/dist/tools/market/stock-quote.js.map +1 -1
- package/dist/tools/options/greeks.js +0 -1
- package/dist/tools/options/greeks.js.map +1 -1
- package/dist/tools/options/option-chain.d.ts +1 -1
- package/dist/tools/options/option-chain.js +13 -5
- package/dist/tools/options/option-chain.js.map +1 -1
- package/dist/tools/portfolio/correlation.d.ts +1 -1
- package/dist/tools/portfolio/correlation.js +1 -1
- package/dist/tools/portfolio/correlation.js.map +1 -1
- package/dist/tools/portfolio/holdings-overlap.d.ts +8 -0
- package/dist/tools/portfolio/holdings-overlap.js +105 -0
- package/dist/tools/portfolio/holdings-overlap.js.map +1 -0
- package/dist/tools/portfolio/predictions.d.ts +1 -1
- package/dist/tools/portfolio/predictions.js +1 -1
- package/dist/tools/portfolio/predictions.js.map +1 -1
- package/dist/tools/portfolio/risk-analysis.d.ts +1 -1
- package/dist/tools/portfolio/risk-analysis.js +1 -1
- package/dist/tools/portfolio/risk-analysis.js.map +1 -1
- package/dist/tools/portfolio/tracker.d.ts +1 -1
- package/dist/tools/portfolio/tracker.js +1 -1
- package/dist/tools/portfolio/tracker.js.map +1 -1
- package/dist/tools/portfolio/watchlist.d.ts +1 -1
- package/dist/tools/portfolio/watchlist.js +12 -4
- package/dist/tools/portfolio/watchlist.js.map +1 -1
- package/dist/tools/sentiment/reddit-sentiment.d.ts +1 -1
- package/dist/tools/sentiment/reddit-sentiment.js +1 -1
- package/dist/tools/sentiment/reddit-sentiment.js.map +1 -1
- package/dist/tools/sentiment/sentiment-summary.d.ts +1 -1
- package/dist/tools/sentiment/sentiment-summary.js +57 -2
- package/dist/tools/sentiment/sentiment-summary.js.map +1 -1
- package/dist/tools/sentiment/sentiment-trend.d.ts +1 -1
- package/dist/tools/sentiment/twitter-sentiment.d.ts +1 -1
- package/dist/tools/sentiment/twitter-sentiment.js +1 -1
- package/dist/tools/sentiment/twitter-sentiment.js.map +1 -1
- package/dist/tools/sentiment/web-search.d.ts +1 -1
- package/dist/tools/sentiment/web-search.js +32 -3
- package/dist/tools/sentiment/web-search.js.map +1 -1
- package/dist/tools/sentiment/web-sentiment.d.ts +1 -1
- package/dist/tools/sentiment/web-sentiment.js +1 -1
- package/dist/tools/sentiment/web-sentiment.js.map +1 -1
- package/dist/tools/technical/backtest.d.ts +3 -3
- package/dist/tools/technical/backtest.js +41 -27
- package/dist/tools/technical/backtest.js.map +1 -1
- package/dist/tools/technical/indicators.d.ts +1 -1
- package/dist/tools/technical/indicators.js +8 -4
- package/dist/tools/technical/indicators.js.map +1 -1
- package/dist/types/options.d.ts +10 -0
- package/dist/types/portfolio.d.ts +27 -0
- package/dist/workflows/compare-assets.js +38 -2
- package/dist/workflows/compare-assets.js.map +1 -1
- package/dist/workflows/options-screener.js +94 -8
- package/dist/workflows/options-screener.js.map +1 -1
- package/dist/workflows/portfolio-builder.js +9 -5
- package/dist/workflows/portfolio-builder.js.map +1 -1
- package/gui/server/ask-user-bridge.ts +82 -0
- package/gui/server/background-quotes.ts +31 -0
- package/gui/server/chat-event-adapter.ts +142 -0
- package/gui/server/gui-session-manager.ts +5 -0
- package/gui/server/invoke-tool.ts +89 -0
- package/gui/server/live-chat-event-adapter.ts +181 -0
- package/gui/server/model-setup.ts +100 -0
- package/gui/server/package.json +5 -0
- package/gui/server/projector.ts +254 -0
- package/gui/server/prompt-observation.ts +61 -0
- package/gui/server/server.ts +703 -0
- package/gui/server/session-actions.ts +31 -0
- package/gui/server/session-entry-wait.ts +81 -0
- package/gui/server/tool-metadata.ts +88 -0
- package/gui/server/websocket.ts +128 -0
- package/gui/server/writer-lock.ts +118 -0
- package/gui/shared/chat-events.ts +118 -0
- package/gui/shared/event-reducer.ts +186 -0
- package/gui/web/dist/assets/CatalogOverlay-Bmp6Knu7.js +1 -0
- package/gui/web/dist/assets/index-Bxt9QpLX.css +1 -0
- package/gui/web/dist/assets/index-CZ9DHZYy.js +67 -0
- package/gui/web/dist/assets/logo-CWpt6Y2a.svg +187 -0
- package/gui/web/dist/index.html +17 -0
- package/package.json +50 -18
- package/src/analysts/contracts.ts +189 -0
- package/src/analysts/orchestrator.ts +300 -0
- package/src/cli.ts +206 -0
- package/src/config.ts +245 -0
- package/src/index.ts +5 -0
- package/src/infra/browser.ts +111 -0
- package/src/infra/cache.ts +103 -0
- package/src/infra/http-client.ts +68 -0
- package/src/infra/index.ts +18 -0
- package/src/infra/native-dependencies.ts +12 -0
- package/src/infra/node-version.ts +24 -0
- package/src/infra/open-url.ts +28 -0
- package/src/infra/opencandle-paths.ts +64 -0
- package/src/infra/rate-limiter.ts +73 -0
- package/src/memory/index.ts +10 -0
- package/src/memory/manager.ts +192 -0
- package/src/memory/preference-extractor.ts +106 -0
- package/src/memory/retrieval.ts +70 -0
- package/src/memory/sqlite.ts +172 -0
- package/src/memory/storage.ts +205 -0
- package/src/memory/tool-defaults.ts +87 -0
- package/src/memory/types.ts +71 -0
- package/src/onboarding/connect.ts +184 -0
- package/src/onboarding/credential-interceptor.ts +134 -0
- package/src/onboarding/degradation-accumulator.ts +79 -0
- package/src/onboarding/prompt-user.ts +85 -0
- package/src/onboarding/providers.ts +315 -0
- package/src/onboarding/state.ts +218 -0
- package/src/onboarding/tool-helpers.ts +111 -0
- package/src/onboarding/tool-tags.ts +201 -0
- package/src/onboarding/validation.ts +158 -0
- package/src/pi/opencandle-extension.ts +955 -0
- package/src/pi/session-storage.ts +5 -0
- package/src/pi/session.ts +81 -0
- package/src/pi/setup.ts +381 -0
- package/src/pi/tool-adapter.ts +36 -0
- package/src/prompts/context-builder.ts +315 -0
- package/src/prompts/disclaimer.ts +9 -0
- package/src/prompts/policy-cards.ts +220 -0
- package/src/prompts/sections.ts +46 -0
- package/src/prompts/workflow-prompts.ts +433 -0
- package/src/providers/alpha-vantage.ts +315 -0
- package/src/providers/coingecko.ts +96 -0
- package/src/providers/exa-search.ts +373 -0
- package/src/providers/fear-greed.ts +45 -0
- package/src/providers/finnhub.ts +124 -0
- package/src/providers/fred.ts +83 -0
- package/src/providers/index.ts +9 -0
- package/src/providers/provider-credential-error.ts +23 -0
- package/src/providers/reddit.ts +151 -0
- package/src/providers/sec-edgar.ts +312 -0
- package/src/providers/twitter.ts +173 -0
- package/src/providers/web-search.ts +293 -0
- package/src/providers/with-fallback.ts +41 -0
- package/src/providers/wrap-provider.ts +64 -0
- package/src/providers/yahoo-finance.ts +534 -0
- package/src/routing/classify-intent.ts +285 -0
- package/src/routing/defaults.ts +29 -0
- package/src/routing/entity-extractor.ts +291 -0
- package/src/routing/index.ts +70 -0
- package/src/routing/legacy-rule-router.ts +13 -0
- package/src/routing/planning.ts +732 -0
- package/src/routing/route-manifest.ts +287 -0
- package/src/routing/router-llm-client.ts +51 -0
- package/src/routing/router-prompt.ts +163 -0
- package/src/routing/router-types.ts +87 -0
- package/src/routing/router.ts +712 -0
- package/src/routing/slot-resolver.ts +190 -0
- package/src/routing/turn-context.ts +111 -0
- package/src/routing/types.ts +75 -0
- package/src/runtime/answer-contracts.ts +633 -0
- package/src/runtime/artifact-contracts.ts +76 -0
- package/src/runtime/evidence.ts +77 -0
- package/src/runtime/planning-evidence.ts +591 -0
- package/src/runtime/prompt-step.ts +75 -0
- package/src/runtime/provider-tracker.ts +40 -0
- package/src/runtime/run-context.ts +22 -0
- package/src/runtime/session-coordinator.ts +472 -0
- package/src/runtime/tool-defaults-wrapper.ts +35 -0
- package/src/runtime/validation.ts +214 -0
- package/src/runtime/workflow-events.ts +75 -0
- package/src/runtime/workflow-runner.ts +188 -0
- package/src/runtime/workflow-types.ts +102 -0
- package/src/sentiment/adapters/finnhub.ts +44 -0
- package/src/sentiment/adapters/reddit.ts +65 -0
- package/src/sentiment/adapters/twitter.ts +36 -0
- package/src/sentiment/adapters/web.ts +44 -0
- package/src/sentiment/index.ts +58 -0
- package/src/sentiment/keywords.ts +9 -0
- package/src/sentiment/pipeline.ts +68 -0
- package/src/sentiment/scorer.ts +78 -0
- package/src/sentiment/store.ts +260 -0
- package/src/sentiment/trends.ts +90 -0
- package/src/sentiment/types.ts +108 -0
- package/src/system-prompt.ts +118 -0
- package/src/tool-kit.ts +68 -0
- package/src/tools/AGENTS.md +36 -0
- package/src/tools/fundamentals/company-overview.ts +54 -0
- package/src/tools/fundamentals/comps.ts +156 -0
- package/src/tools/fundamentals/dcf.ts +267 -0
- package/src/tools/fundamentals/earnings.ts +47 -0
- package/src/tools/fundamentals/financials.ts +54 -0
- package/src/tools/fundamentals/sec-filings.ts +84 -0
- package/src/tools/index.ts +91 -0
- package/src/tools/interaction/ask-user.ts +81 -0
- package/src/tools/interaction/twitter-login.ts +93 -0
- package/src/tools/macro/fear-greed.ts +41 -0
- package/src/tools/macro/fred-data.ts +80 -0
- package/src/tools/market/crypto-history.ts +67 -0
- package/src/tools/market/crypto-price.ts +53 -0
- package/src/tools/market/search-ticker.ts +53 -0
- package/src/tools/market/stock-history.ts +79 -0
- package/src/tools/market/stock-quote.ts +64 -0
- package/src/tools/options/greeks.ts +81 -0
- package/src/tools/options/option-chain.ts +96 -0
- package/src/tools/portfolio/correlation.ts +162 -0
- package/src/tools/portfolio/holdings-overlap.ts +123 -0
- package/src/tools/portfolio/predictions.ts +253 -0
- package/src/tools/portfolio/risk-analysis.ts +134 -0
- package/src/tools/portfolio/tracker.ts +147 -0
- package/src/tools/portfolio/watchlist.ts +159 -0
- package/src/tools/sentiment/reddit-sentiment.ts +164 -0
- package/src/tools/sentiment/sentiment-summary.ts +316 -0
- package/src/tools/sentiment/sentiment-trend.ts +58 -0
- package/src/tools/sentiment/twitter-sentiment.ts +96 -0
- package/src/tools/sentiment/web-search.ts +183 -0
- package/src/tools/sentiment/web-sentiment.ts +76 -0
- package/src/tools/technical/backtest.ts +267 -0
- package/src/tools/technical/indicators.ts +256 -0
- package/src/types/fundamentals.ts +46 -0
- package/src/types/index.ts +20 -0
- package/src/types/macro.ts +27 -0
- package/src/types/market.ts +43 -0
- package/src/types/options.ts +52 -0
- package/src/types/portfolio.ts +73 -0
- package/src/types/sentiment.ts +70 -0
- package/src/workflows/compare-assets.ts +75 -0
- package/src/workflows/index.ts +4 -0
- package/src/workflows/options-screener.ts +127 -0
- package/src/workflows/portfolio-builder.ts +56 -0
- package/src/workflows/types.ts +4 -0
- package/dist/runtime/index.d.ts +0 -16
- package/dist/runtime/index.js +0 -10
- package/dist/runtime/index.js.map +0 -1
- package/dist/runtime/provider-ids.d.ts +0 -14
- package/dist/runtime/provider-ids.js +0 -14
- package/dist/runtime/provider-ids.js.map +0 -1
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ExtractedEntities,
|
|
3
|
+
WorkflowType,
|
|
4
|
+
} from "./types.js";
|
|
5
|
+
import type {
|
|
6
|
+
RouterOutput,
|
|
7
|
+
RouterRoute,
|
|
8
|
+
RouterRouteKind,
|
|
9
|
+
ToolBundleName,
|
|
10
|
+
} from "./router-types.js";
|
|
11
|
+
import type { MemoryCategory } from "../memory/types.js";
|
|
12
|
+
|
|
13
|
+
export const ROUTE_KINDS: readonly RouterRouteKind[] = [
|
|
14
|
+
"workflow_dispatch",
|
|
15
|
+
"agent_task",
|
|
16
|
+
"clarification",
|
|
17
|
+
"pass_through",
|
|
18
|
+
];
|
|
19
|
+
|
|
20
|
+
export const TOOL_BUNDLE_TOOLS: Record<ToolBundleName, readonly string[]> = {
|
|
21
|
+
core_market: [
|
|
22
|
+
"search_ticker",
|
|
23
|
+
"get_stock_quote",
|
|
24
|
+
"get_stock_history",
|
|
25
|
+
"get_crypto_price",
|
|
26
|
+
"get_crypto_history",
|
|
27
|
+
"get_company_overview",
|
|
28
|
+
"get_financials",
|
|
29
|
+
"get_earnings",
|
|
30
|
+
"compare_companies",
|
|
31
|
+
"compute_dcf",
|
|
32
|
+
"get_technical_indicators",
|
|
33
|
+
"backtest_strategy",
|
|
34
|
+
"analyze_risk",
|
|
35
|
+
"analyze_correlation",
|
|
36
|
+
"analyze_holdings_overlap",
|
|
37
|
+
"track_portfolio",
|
|
38
|
+
"manage_watchlist",
|
|
39
|
+
"track_prediction",
|
|
40
|
+
"search_web",
|
|
41
|
+
],
|
|
42
|
+
options: ["get_option_chain", "get_stock_quote", "search_ticker", "search_web"],
|
|
43
|
+
macro: ["get_economic_data", "get_fear_greed", "search_web"],
|
|
44
|
+
sentiment: [
|
|
45
|
+
"get_reddit_sentiment",
|
|
46
|
+
"get_twitter_sentiment",
|
|
47
|
+
"get_web_sentiment",
|
|
48
|
+
"get_sentiment_trend",
|
|
49
|
+
"get_sentiment_summary",
|
|
50
|
+
"get_fear_greed",
|
|
51
|
+
"search_web",
|
|
52
|
+
],
|
|
53
|
+
sec: ["get_sec_filings", "get_company_overview", "search_web"],
|
|
54
|
+
clarification: ["ask_user"],
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
export type PromptPlaybookId =
|
|
58
|
+
| "workflow_dispatch"
|
|
59
|
+
| "agent_task"
|
|
60
|
+
| "clarification"
|
|
61
|
+
| "pass_through";
|
|
62
|
+
|
|
63
|
+
interface RouteCapability {
|
|
64
|
+
routeKind: RouterRouteKind;
|
|
65
|
+
legacyRoute: RouterRoute;
|
|
66
|
+
promptPlaybook: PromptPlaybookId;
|
|
67
|
+
toolBundles: ToolBundleName[];
|
|
68
|
+
memoryScopes: MemoryCategory[];
|
|
69
|
+
allowWorkflow: boolean;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
interface WorkflowCapability {
|
|
73
|
+
workflow: Exclude<WorkflowType, "unclassified">;
|
|
74
|
+
dispatchable: boolean;
|
|
75
|
+
requiredSlots: string[];
|
|
76
|
+
toolBundles: ToolBundleName[];
|
|
77
|
+
memoryScopes: MemoryCategory[];
|
|
78
|
+
promptPlaybook: PromptPlaybookId;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export const ROUTE_CAPABILITY_MANIFEST: Record<RouterRouteKind, RouteCapability> = {
|
|
82
|
+
workflow_dispatch: {
|
|
83
|
+
routeKind: "workflow_dispatch",
|
|
84
|
+
legacyRoute: "workflow",
|
|
85
|
+
promptPlaybook: "workflow_dispatch",
|
|
86
|
+
toolBundles: ["core_market"],
|
|
87
|
+
memoryScopes: ["investor_profile", "workflow_history"],
|
|
88
|
+
allowWorkflow: true,
|
|
89
|
+
},
|
|
90
|
+
agent_task: {
|
|
91
|
+
routeKind: "agent_task",
|
|
92
|
+
legacyRoute: "fallback",
|
|
93
|
+
promptPlaybook: "agent_task",
|
|
94
|
+
toolBundles: ["core_market"],
|
|
95
|
+
memoryScopes: ["investor_profile", "workflow_history"],
|
|
96
|
+
allowWorkflow: false,
|
|
97
|
+
},
|
|
98
|
+
clarification: {
|
|
99
|
+
routeKind: "clarification",
|
|
100
|
+
legacyRoute: "fallback",
|
|
101
|
+
promptPlaybook: "clarification",
|
|
102
|
+
toolBundles: ["clarification"],
|
|
103
|
+
memoryScopes: ["investor_profile", "workflow_history"],
|
|
104
|
+
allowWorkflow: true,
|
|
105
|
+
},
|
|
106
|
+
pass_through: {
|
|
107
|
+
routeKind: "pass_through",
|
|
108
|
+
legacyRoute: "fallback",
|
|
109
|
+
promptPlaybook: "pass_through",
|
|
110
|
+
toolBundles: [],
|
|
111
|
+
memoryScopes: [],
|
|
112
|
+
allowWorkflow: false,
|
|
113
|
+
},
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
export const WORKFLOW_CAPABILITY_MANIFEST: Record<
|
|
117
|
+
Exclude<WorkflowType, "unclassified">,
|
|
118
|
+
WorkflowCapability
|
|
119
|
+
> = {
|
|
120
|
+
portfolio_builder: {
|
|
121
|
+
workflow: "portfolio_builder",
|
|
122
|
+
dispatchable: true,
|
|
123
|
+
requiredSlots: ["budget"],
|
|
124
|
+
toolBundles: ["core_market", "macro", "sentiment", "clarification"],
|
|
125
|
+
memoryScopes: ["investor_profile", "interaction_feedback", "workflow_history"],
|
|
126
|
+
promptPlaybook: "workflow_dispatch",
|
|
127
|
+
},
|
|
128
|
+
options_screener: {
|
|
129
|
+
workflow: "options_screener",
|
|
130
|
+
dispatchable: true,
|
|
131
|
+
requiredSlots: ["symbol"],
|
|
132
|
+
toolBundles: ["core_market", "options", "sentiment", "clarification"],
|
|
133
|
+
memoryScopes: ["investor_profile", "interaction_feedback", "workflow_history"],
|
|
134
|
+
promptPlaybook: "workflow_dispatch",
|
|
135
|
+
},
|
|
136
|
+
compare_assets: {
|
|
137
|
+
workflow: "compare_assets",
|
|
138
|
+
dispatchable: true,
|
|
139
|
+
requiredSlots: ["symbols"],
|
|
140
|
+
toolBundles: ["core_market", "macro", "sentiment", "clarification"],
|
|
141
|
+
memoryScopes: ["investor_profile", "workflow_history"],
|
|
142
|
+
promptPlaybook: "workflow_dispatch",
|
|
143
|
+
},
|
|
144
|
+
single_asset_analysis: {
|
|
145
|
+
workflow: "single_asset_analysis",
|
|
146
|
+
dispatchable: false,
|
|
147
|
+
requiredSlots: ["symbol"],
|
|
148
|
+
toolBundles: ["core_market", "options", "sentiment", "sec", "clarification"],
|
|
149
|
+
memoryScopes: ["investor_profile", "workflow_history"],
|
|
150
|
+
promptPlaybook: "agent_task",
|
|
151
|
+
},
|
|
152
|
+
watchlist_or_tracking: {
|
|
153
|
+
workflow: "watchlist_or_tracking",
|
|
154
|
+
dispatchable: false,
|
|
155
|
+
requiredSlots: [],
|
|
156
|
+
toolBundles: ["core_market", "clarification"],
|
|
157
|
+
memoryScopes: ["investor_profile", "workflow_history"],
|
|
158
|
+
promptPlaybook: "agent_task",
|
|
159
|
+
},
|
|
160
|
+
general_finance_qa: {
|
|
161
|
+
workflow: "general_finance_qa",
|
|
162
|
+
dispatchable: false,
|
|
163
|
+
requiredSlots: [],
|
|
164
|
+
toolBundles: ["core_market", "macro", "sentiment", "sec", "clarification"],
|
|
165
|
+
memoryScopes: ["investor_profile", "workflow_history"],
|
|
166
|
+
promptPlaybook: "agent_task",
|
|
167
|
+
},
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
export function isRouteKind(value: string): value is RouterRouteKind {
|
|
171
|
+
return ROUTE_KINDS.includes(value as RouterRouteKind);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
export function isToolBundleName(value: string): value is ToolBundleName {
|
|
175
|
+
return Object.hasOwn(TOOL_BUNDLE_TOOLS, value);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export function legacyRouteForRouteKind(routeKind: RouterRouteKind): RouterRoute {
|
|
179
|
+
return ROUTE_CAPABILITY_MANIFEST[routeKind].legacyRoute;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
export function isDispatchableWorkflow(
|
|
183
|
+
workflow: Exclude<WorkflowType, "unclassified"> | undefined,
|
|
184
|
+
): boolean {
|
|
185
|
+
if (!workflow) return false;
|
|
186
|
+
return WORKFLOW_CAPABILITY_MANIFEST[workflow]?.dispatchable === true;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
export function routeKindFromLegacyRoute(
|
|
190
|
+
route: RouterRoute,
|
|
191
|
+
missingRequired: readonly string[] = [],
|
|
192
|
+
): RouterRouteKind {
|
|
193
|
+
if (missingRequired.length > 0) return "clarification";
|
|
194
|
+
return route === "workflow" ? "workflow_dispatch" : "agent_task";
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
export function workflowRequiredSlots(
|
|
198
|
+
workflow: Exclude<WorkflowType, "unclassified"> | undefined,
|
|
199
|
+
): string[] {
|
|
200
|
+
if (!workflow) return [];
|
|
201
|
+
return WORKFLOW_CAPABILITY_MANIFEST[workflow]?.requiredSlots ?? [];
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
export function computeMissingRequiredSlots(
|
|
205
|
+
workflow: Exclude<WorkflowType, "unclassified"> | undefined,
|
|
206
|
+
entities: ExtractedEntities,
|
|
207
|
+
slots: RouterOutput["slots"] = {},
|
|
208
|
+
existingMissing: readonly string[] = [],
|
|
209
|
+
): string[] {
|
|
210
|
+
const missing = new Set<string>();
|
|
211
|
+
const existing = new Set(existingMissing);
|
|
212
|
+
for (const slot of workflowRequiredSlots(workflow)) {
|
|
213
|
+
if (slot === "budget" && entities.budget === undefined && !slotHasValue(slots.budget)) {
|
|
214
|
+
missing.add("budget");
|
|
215
|
+
}
|
|
216
|
+
if (slot === "symbol" && entities.symbols.length === 0 && !slotHasValue(slots.symbol)) {
|
|
217
|
+
missing.add("symbol");
|
|
218
|
+
}
|
|
219
|
+
if (slot === "symbols" && entities.symbols.length < 2 && !slotHasValue(slots.symbols)) {
|
|
220
|
+
missing.add("symbols");
|
|
221
|
+
}
|
|
222
|
+
existing.delete(slot);
|
|
223
|
+
}
|
|
224
|
+
for (const slot of existing) {
|
|
225
|
+
if (!slotHasValue(slots[slot])) missing.add(slot);
|
|
226
|
+
}
|
|
227
|
+
return Array.from(missing);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
function slotHasValue(slot: RouterOutput["slots"][string] | undefined): boolean {
|
|
231
|
+
if (!slot) return false;
|
|
232
|
+
if (Array.isArray(slot.value)) return slot.value.length > 0;
|
|
233
|
+
return slot.value !== undefined && slot.value !== null && slot.value !== "";
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
export function selectToolBundles(output: Pick<RouterOutput, "routeKind" | "workflow" | "entities">): ToolBundleName[] {
|
|
237
|
+
if (output.routeKind === "pass_through") return [];
|
|
238
|
+
if (output.routeKind === "clarification") return ["clarification"];
|
|
239
|
+
|
|
240
|
+
const bundles = new Set<ToolBundleName>();
|
|
241
|
+
const routeBundles = ROUTE_CAPABILITY_MANIFEST[output.routeKind]?.toolBundles ?? [];
|
|
242
|
+
routeBundles.forEach((bundle) => bundles.add(bundle));
|
|
243
|
+
|
|
244
|
+
if (output.workflow) {
|
|
245
|
+
const workflow = WORKFLOW_CAPABILITY_MANIFEST[output.workflow];
|
|
246
|
+
workflow?.toolBundles.forEach((bundle) => bundles.add(bundle));
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
const metrics = output.entities.compareMetrics ?? [];
|
|
250
|
+
const horizon = output.entities.timeHorizon ?? "";
|
|
251
|
+
if (metrics.includes("macro_hedge") || metrics.includes("interest_rates") || /\b(?:macro|rate|inflation)\b/i.test(horizon)) {
|
|
252
|
+
bundles.add("macro");
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
if (output.entities.symbols.length > 0) {
|
|
256
|
+
bundles.add("core_market");
|
|
257
|
+
}
|
|
258
|
+
if (output.entities.optionStrategy) {
|
|
259
|
+
bundles.add("options");
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
return Array.from(bundles);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
export function activeToolsForBundles(
|
|
266
|
+
bundles: readonly ToolBundleName[],
|
|
267
|
+
availableToolNames?: readonly string[],
|
|
268
|
+
): string[] {
|
|
269
|
+
const selected = new Set<string>();
|
|
270
|
+
for (const bundle of bundles) {
|
|
271
|
+
for (const tool of TOOL_BUNDLE_TOOLS[bundle] ?? []) {
|
|
272
|
+
selected.add(tool);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
if (!availableToolNames) return Array.from(selected);
|
|
277
|
+
const available = new Set(availableToolNames);
|
|
278
|
+
return Array.from(selected).filter((tool) => available.has(tool));
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
export function memoryScopesForRoute(
|
|
282
|
+
routeKind: RouterRouteKind,
|
|
283
|
+
workflow?: Exclude<WorkflowType, "unclassified">,
|
|
284
|
+
): MemoryCategory[] {
|
|
285
|
+
if (workflow) return WORKFLOW_CAPABILITY_MANIFEST[workflow]?.memoryScopes ?? [];
|
|
286
|
+
return ROUTE_CAPABILITY_MANIFEST[routeKind]?.memoryScopes ?? [];
|
|
287
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { completeSimple, type Model } from "@earendil-works/pi-ai";
|
|
2
|
+
import type { RouterLlmClient } from "./router-types.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Build a router LLM client backed by pi-ai's `completeSimple`. The client
|
|
6
|
+
* is intentionally thin: prompt in, raw text out. Schema validation and
|
|
7
|
+
* retry logic live in `router.ts`.
|
|
8
|
+
*
|
|
9
|
+
* Zero tools are passed — the router operates on text alone. Temperature
|
|
10
|
+
* is pinned low for structured-output stability.
|
|
11
|
+
*/
|
|
12
|
+
export function createPiAiRouterClient(model: Model<"anthropic-messages"> | Model<any>): RouterLlmClient {
|
|
13
|
+
return {
|
|
14
|
+
async complete(prompt: string): Promise<string> {
|
|
15
|
+
const response = await completeSimple(
|
|
16
|
+
model,
|
|
17
|
+
{
|
|
18
|
+
messages: [
|
|
19
|
+
{
|
|
20
|
+
role: "user",
|
|
21
|
+
content: prompt,
|
|
22
|
+
timestamp: Date.now(),
|
|
23
|
+
},
|
|
24
|
+
],
|
|
25
|
+
// Explicitly no tools — spec requirement.
|
|
26
|
+
tools: [],
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
temperature: 0,
|
|
30
|
+
maxTokens: 2000,
|
|
31
|
+
reasoning: "minimal",
|
|
32
|
+
},
|
|
33
|
+
);
|
|
34
|
+
|
|
35
|
+
if (response.stopReason === "error" || response.stopReason === "aborted") {
|
|
36
|
+
throw new Error(
|
|
37
|
+
`router LLM call failed: ${response.errorMessage ?? response.stopReason}`,
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const text = response.content
|
|
42
|
+
.filter((c): c is { type: "text"; text: string } => c.type === "text")
|
|
43
|
+
.map((c) => c.text)
|
|
44
|
+
.join("");
|
|
45
|
+
if (!text) {
|
|
46
|
+
throw new Error("router LLM call returned no text content");
|
|
47
|
+
}
|
|
48
|
+
return text;
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
}
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ROUTE_CAPABILITY_MANIFEST,
|
|
3
|
+
WORKFLOW_CAPABILITY_MANIFEST,
|
|
4
|
+
} from "./route-manifest.js";
|
|
5
|
+
import type { RouterInputContext } from "./router-types.js";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Privacy note — priorTurns rendering:
|
|
9
|
+
* Conversational text rendered into the router prompt via `priorTurns` is NOT
|
|
10
|
+
* filtered by `src/memory/types.ts::NEVER_TRUST_FROM_MEMORY` (which governs
|
|
11
|
+
* structured market-sensitive memory keys such as `stock_price` and
|
|
12
|
+
* `target_price`). A future `/forget` command is the designated scrubbing
|
|
13
|
+
* primitive for removing or masking matching entries from the session branch
|
|
14
|
+
* so they no longer reach the router. See
|
|
15
|
+
* `openspec/changes/router-context-and-observability/` for the follow-up.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
function renderCatalog(): string {
|
|
19
|
+
const descriptions: Record<string, string> = {
|
|
20
|
+
portfolio_builder: "user asks to build/allocate a portfolio, invest a budget across positions",
|
|
21
|
+
options_screener: "user asks for options trades / calls / puts on a specific ticker",
|
|
22
|
+
compare_assets: "user asks to compare two or more symbols (vs / versus / which is better)",
|
|
23
|
+
single_asset_analysis: "user asks for a full analysis / deep dive / 'is X attractive' on ONE symbol",
|
|
24
|
+
watchlist_or_tracking: "user manages or asks about their saved watchlist / prediction history",
|
|
25
|
+
general_finance_qa: "definitional / conceptual questions plus broad market structure, sector, industry, monetary policy, and emerging markets research",
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
return Object.values(WORKFLOW_CAPABILITY_MANIFEST).map((w) => {
|
|
29
|
+
const required = w.requiredSlots.length > 0
|
|
30
|
+
? ` [required: ${w.requiredSlots.join(", ")}]`
|
|
31
|
+
: "";
|
|
32
|
+
const mode = w.dispatchable ? "dispatchable workflow" : "agent-task workflow label";
|
|
33
|
+
return `- "${w.workflow}" (${mode}): ${descriptions[w.workflow]}${required}`;
|
|
34
|
+
}).join("\n");
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function renderRouteKinds(): string {
|
|
38
|
+
return Object.values(ROUTE_CAPABILITY_MANIFEST)
|
|
39
|
+
.map((route) => `- "${route.routeKind}" -> legacy route "${route.legacyRoute}"`)
|
|
40
|
+
.join("\n");
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function renderProfile(profile: Record<string, unknown>): string {
|
|
44
|
+
const entries = Object.entries(profile);
|
|
45
|
+
if (entries.length === 0) return "(empty)";
|
|
46
|
+
return entries.map(([k, v]) => `- ${k}: ${JSON.stringify(v)}`).join("\n");
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function renderPriorTurns(
|
|
50
|
+
turns: Array<{ role: "user" | "assistant"; text: string }>,
|
|
51
|
+
): string {
|
|
52
|
+
if (turns.length === 0) return "(none)";
|
|
53
|
+
return turns
|
|
54
|
+
.map((t) => `[${t.role}] ${t.text.replace(/\n+/g, " ").slice(0, 400)}`)
|
|
55
|
+
.join("\n");
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function renderRecentRuns(
|
|
59
|
+
runs: Array<{
|
|
60
|
+
workflowType: string;
|
|
61
|
+
turnType: string;
|
|
62
|
+
resolvedSlots?: Record<string, unknown>;
|
|
63
|
+
createdAt: string;
|
|
64
|
+
}>,
|
|
65
|
+
): string {
|
|
66
|
+
if (runs.length === 0) return "(none)";
|
|
67
|
+
return runs
|
|
68
|
+
.map(
|
|
69
|
+
(r) =>
|
|
70
|
+
`- ${r.createdAt} ${r.turnType}/${r.workflowType} ${r.resolvedSlots ? JSON.stringify(r.resolvedSlots) : ""}`,
|
|
71
|
+
)
|
|
72
|
+
.join("\n");
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const SCHEMA_SPEC = `You MUST respond with a SINGLE JSON object and nothing else (no markdown fences, no prose outside the JSON). The object MUST conform to this TypeScript interface exactly:
|
|
76
|
+
|
|
77
|
+
interface RouterOutput {
|
|
78
|
+
routeKind: "workflow_dispatch" | "agent_task" | "clarification" | "pass_through";
|
|
79
|
+
route: "workflow" | "fallback";
|
|
80
|
+
workflow?: "portfolio_builder" | "options_screener" | "compare_assets" | "single_asset_analysis" | "watchlist_or_tracking" | "general_finance_qa";
|
|
81
|
+
entities: {
|
|
82
|
+
symbols: string[]; // UPPERCASE tickers the user mentioned or implied
|
|
83
|
+
budget?: number; // dollar amount if user stated one
|
|
84
|
+
maxPremium?: number;
|
|
85
|
+
timeHorizon?: string; // e.g. "6mo", "1y_plus", "short", "long"
|
|
86
|
+
riskProfile?: string; // "conservative" | "balanced" | "aggressive"
|
|
87
|
+
direction?: "bullish" | "bearish";
|
|
88
|
+
dteHint?: string;
|
|
89
|
+
optionStrategy?: "covered_call" | "protective_put"; // set when user explicitly asks for a known option strategy
|
|
90
|
+
heldSymbol?: string; // for covered calls/protective puts: ticker the user owns/holds
|
|
91
|
+
catalystSymbols?: string[]; // tickers mentioned as event/catalyst context, not the option-chain underlying
|
|
92
|
+
costBasis?: number; // per-share basis when user says "cost basis is $X"
|
|
93
|
+
shareQuantity?: number; // number of shares owned when stated, e.g. "200 shares"
|
|
94
|
+
compareMetrics?: string[]; // optional compare focus tags, e.g. "sentiment", "macro_hedge"
|
|
95
|
+
};
|
|
96
|
+
slots: Record<string, {
|
|
97
|
+
value: unknown;
|
|
98
|
+
source: "user" | "preference" | "default" | "prior_context" | "memory";
|
|
99
|
+
confidence: "high" | "medium" | "low";
|
|
100
|
+
}>;
|
|
101
|
+
preference_updates: Array<{
|
|
102
|
+
key: string; // e.g. "risk_profile", "time_horizon", "asset_scope", "options_liquidity"
|
|
103
|
+
value: string;
|
|
104
|
+
confidence: "high" | "medium" | "low";
|
|
105
|
+
source: "inferred";
|
|
106
|
+
}>;
|
|
107
|
+
missing_required: string[]; // required slot names the turn/profile/defaults did not fill
|
|
108
|
+
tool_bundles: Array<"core_market" | "options" | "macro" | "sentiment" | "sec" | "clarification">;
|
|
109
|
+
diagnostics: Array<{ code: string; message: string }>;
|
|
110
|
+
reasoning: string; // one or two short sentences; used for debugging only
|
|
111
|
+
}`;
|
|
112
|
+
|
|
113
|
+
const ROUTING_RULES = `Routing rules:
|
|
114
|
+
- Choose routeKind = "workflow_dispatch" ONLY when the turn clearly matches a dispatchable workflow (portfolio_builder, options_screener, or compare_assets) AND required slots are filled from the turn, trusted prior context, or profile snapshot.
|
|
115
|
+
- For single_asset_analysis, watchlist_or_tracking, and general_finance_qa, set routeKind = "agent_task" even when you set the workflow label. These are workflow labels for prompt/tool policy, not structured workflow dispatch.
|
|
116
|
+
- Choose routeKind = "agent_task" for in-scope finance work that should be answered by the main agent, including simple data fetches like "AAPL quote" and open-ended questions like "entry levels on ASTS for 6 months".
|
|
117
|
+
- Choose routeKind = "clarification" when required slots are missing (e.g. options workflow needs a symbol, portfolio needs a budget). List specific slot names in missing_required. The main agent will use ask_user to collect them.
|
|
118
|
+
- Choose routeKind = "pass_through" when the request is outside OpenCandle's finance task surface.
|
|
119
|
+
- Set legacy route = "workflow" only for routeKind = "workflow_dispatch"; otherwise set legacy route = "fallback".
|
|
120
|
+
- DO NOT invent a "direct_tool" route. Tool execution belongs to the main agent.
|
|
121
|
+
- For covered call prompts, distinguish the owned underlying from catalyst tickers. Put the owned symbol first in symbols, set heldSymbol to the owned symbol, put event/context tickers in catalystSymbols, set workflow="options_screener", and preserve costBasis if stated.
|
|
122
|
+
- For protective put prompts, treat the owned/held ticker as the option-chain underlying, set optionStrategy="protective_put", direction="bearish", and preserve shareQuantity if stated. This is a hedge on an existing long share position, not a bullish call screen.
|
|
123
|
+
- Source attribution rules (per-slot source field):
|
|
124
|
+
- source = "user": the value came from THIS turn's text.
|
|
125
|
+
- source = "preference": the value came from profileSnapshot (not this turn).
|
|
126
|
+
- source = "default": a sensible default was applied (workflow fallback).
|
|
127
|
+
- source = "prior_context": the value came from prior conversation turns.
|
|
128
|
+
- source = "memory": the value came from retrieved non-profile memory.
|
|
129
|
+
- Preference updates:
|
|
130
|
+
- Emit preference_updates ONLY for stable user-dispositions stated (or very strongly implied) in the current turn. E.g. "I'm aggressive" → risk_profile=aggressive, high.
|
|
131
|
+
- Do NOT emit preference_updates that merely echo profileSnapshot.
|
|
132
|
+
- Only confidence="high" updates will be persisted; medium/low are logged but dropped.
|
|
133
|
+
- You have NO tools. Do not request tool execution. Classify on text alone.`;
|
|
134
|
+
|
|
135
|
+
export function buildRouterPrompt(input: RouterInputContext): string {
|
|
136
|
+
return `You are OpenCandle's routing agent. Your job is to classify the user's turn into one of the known workflows (or fallback), extract entities + per-slot provenance, and surface any stable preferences the user expressed. Your output feeds the main analyst agent — it does NOT go to the user.
|
|
137
|
+
|
|
138
|
+
ROUTE KINDS:
|
|
139
|
+
${renderRouteKinds()}
|
|
140
|
+
|
|
141
|
+
WORKFLOW CATALOG:
|
|
142
|
+
${renderCatalog()}
|
|
143
|
+
|
|
144
|
+
${SCHEMA_SPEC}
|
|
145
|
+
|
|
146
|
+
${ROUTING_RULES}
|
|
147
|
+
|
|
148
|
+
--- CONTEXT ---
|
|
149
|
+
|
|
150
|
+
Profile snapshot (persisted preferences from prior sessions):
|
|
151
|
+
${renderProfile(input.profileSnapshot)}
|
|
152
|
+
|
|
153
|
+
Recent workflow runs (most recent last):
|
|
154
|
+
${renderRecentRuns(input.recentWorkflowRuns)}
|
|
155
|
+
|
|
156
|
+
Prior conversation turns (most recent last):
|
|
157
|
+
${renderPriorTurns(input.priorTurns)}
|
|
158
|
+
|
|
159
|
+
--- CURRENT TURN ---
|
|
160
|
+
${input.text}
|
|
161
|
+
|
|
162
|
+
Respond with the JSON object. Nothing else.`;
|
|
163
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import type { ExtractedEntities, SlotSource, WorkflowType } from "./types.js";
|
|
2
|
+
|
|
3
|
+
export type RouterRoute = "workflow" | "fallback";
|
|
4
|
+
export type RouterRouteKind =
|
|
5
|
+
| "workflow_dispatch"
|
|
6
|
+
| "agent_task"
|
|
7
|
+
| "clarification"
|
|
8
|
+
| "pass_through";
|
|
9
|
+
|
|
10
|
+
export type ToolBundleName =
|
|
11
|
+
| "core_market"
|
|
12
|
+
| "options"
|
|
13
|
+
| "macro"
|
|
14
|
+
| "sentiment"
|
|
15
|
+
| "sec"
|
|
16
|
+
| "clarification";
|
|
17
|
+
|
|
18
|
+
export type RouterConfidence = "high" | "medium" | "low";
|
|
19
|
+
|
|
20
|
+
export interface RouterSlot {
|
|
21
|
+
value: unknown;
|
|
22
|
+
source: SlotSource;
|
|
23
|
+
confidence: RouterConfidence;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface RouterPreferenceUpdate {
|
|
27
|
+
key: string;
|
|
28
|
+
value: string;
|
|
29
|
+
confidence: RouterConfidence;
|
|
30
|
+
source: "inferred";
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface RouterDiagnostic {
|
|
34
|
+
code: string;
|
|
35
|
+
message: string;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Structured output from the LLM router. Mirrors existing types
|
|
40
|
+
* (`ClassificationResult`, `ExtractedEntities`, `SlotSource`) so downstream
|
|
41
|
+
* consumers can branch without a new vocabulary.
|
|
42
|
+
*
|
|
43
|
+
* `workflow` is only meaningful when `route === "workflow"`. For `fallback`
|
|
44
|
+
* routes, `workflow_type` at the storage layer is the sentinel `"fallback"`.
|
|
45
|
+
*/
|
|
46
|
+
export interface RouterOutput {
|
|
47
|
+
routeKind: RouterRouteKind;
|
|
48
|
+
route: RouterRoute;
|
|
49
|
+
workflow?: Exclude<WorkflowType, "unclassified">;
|
|
50
|
+
entities: ExtractedEntities;
|
|
51
|
+
slots: Record<string, RouterSlot>;
|
|
52
|
+
preference_updates: RouterPreferenceUpdate[];
|
|
53
|
+
missing_required: string[];
|
|
54
|
+
tool_bundles: ToolBundleName[];
|
|
55
|
+
diagnostics: RouterDiagnostic[];
|
|
56
|
+
reasoning: string;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** Context passed into the router on each turn. */
|
|
60
|
+
export interface RouterInputContext {
|
|
61
|
+
/** Raw user text from `pi.on("input")`. */
|
|
62
|
+
text: string;
|
|
63
|
+
/** Last 5 user/assistant turns (most recent last). */
|
|
64
|
+
priorTurns: Array<{ role: "user" | "assistant"; text: string }>;
|
|
65
|
+
/** Current investor_profile snapshot retrieved from preferences storage. */
|
|
66
|
+
profileSnapshot: Record<string, unknown>;
|
|
67
|
+
/** Last 3 workflow_runs, compact summaries. */
|
|
68
|
+
recentWorkflowRuns: Array<{
|
|
69
|
+
workflowType: string;
|
|
70
|
+
turnType: string;
|
|
71
|
+
resolvedSlots?: Record<string, unknown>;
|
|
72
|
+
createdAt: string;
|
|
73
|
+
}>;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Abstract LLM client used by the router. Injected by callers so unit tests
|
|
78
|
+
* can supply a deterministic mock. The real implementation (see
|
|
79
|
+
* `src/routing/router-llm-client.ts`) wraps pi-ai's `completeSimple`.
|
|
80
|
+
*/
|
|
81
|
+
export interface RouterLlmClient {
|
|
82
|
+
/**
|
|
83
|
+
* Run a single prompt → text completion. Router parses the returned text
|
|
84
|
+
* as JSON; the client is not responsible for structured-output parsing.
|
|
85
|
+
*/
|
|
86
|
+
complete(prompt: string): Promise<string>;
|
|
87
|
+
}
|