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,79 @@
|
|
|
1
|
+
// Per-turn accumulator for soft-tier provider degradations. When a soft
|
|
2
|
+
// provider falls back to a keyless alternative (e.g. Brave → DuckDuckGo, Exa →
|
|
3
|
+
// keyless MCP, Finnhub → cached/free source), the tool result content carries
|
|
4
|
+
// a `[OPENCANDLE_SOFT_DEGRADED ...]` tag. The extension's `tool_result` hook
|
|
5
|
+
// records the provider id in this accumulator WITHOUT mutating the tool
|
|
6
|
+
// result; at `turn_end` the accumulator builds a single combined
|
|
7
|
+
// `[OPENCANDLE_SKIPPED ...]`-style annotation that gets appended to the
|
|
8
|
+
// session as a sidecar entry for observability.
|
|
9
|
+
//
|
|
10
|
+
// Per-provider state (never_ask / snoozed / completed) lives in the persistent
|
|
11
|
+
// OnboardingState on disk — the accumulator stays pure and consults the
|
|
12
|
+
// caller-supplied state snapshot at `buildCombinedAnnotation` time. This keeps
|
|
13
|
+
// the accumulator trivially testable without mocking the filesystem.
|
|
14
|
+
|
|
15
|
+
import type { ProviderId } from "./providers.js";
|
|
16
|
+
import { getProvider } from "./providers.js";
|
|
17
|
+
import type { OnboardingState } from "./state.js";
|
|
18
|
+
import { buildSkippedTag } from "./tool-tags.js";
|
|
19
|
+
|
|
20
|
+
export interface DegradationAccumulator {
|
|
21
|
+
/** Record that a soft-tier provider fell back during this turn. Idempotent. */
|
|
22
|
+
record(provider: ProviderId): void;
|
|
23
|
+
/** Number of distinct providers recorded since the last reset. */
|
|
24
|
+
size(): number;
|
|
25
|
+
/** True when nothing has been recorded since the last reset. */
|
|
26
|
+
isEmpty(): boolean;
|
|
27
|
+
/**
|
|
28
|
+
* Build a newline-delimited list of `[OPENCANDLE_SKIPPED ...]` tags — one
|
|
29
|
+
* per distinct recorded provider. Providers whose `state.providers[id]`
|
|
30
|
+
* entry has `status: "never_ask"` get the `(silenced)` suffix in their
|
|
31
|
+
* remediation so the system prompt instruction suppresses the `/connect`
|
|
32
|
+
* link in the final answer. Returns `null` when no providers are recorded.
|
|
33
|
+
*/
|
|
34
|
+
buildCombinedAnnotation(state: OnboardingState): string | null;
|
|
35
|
+
/** Clear all recorded providers. Call this at turn_start. */
|
|
36
|
+
reset(): void;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function createDegradationAccumulator(): DegradationAccumulator {
|
|
40
|
+
const recorded = new Set<ProviderId>();
|
|
41
|
+
|
|
42
|
+
return {
|
|
43
|
+
record(provider: ProviderId): void {
|
|
44
|
+
recorded.add(provider);
|
|
45
|
+
},
|
|
46
|
+
size(): number {
|
|
47
|
+
return recorded.size;
|
|
48
|
+
},
|
|
49
|
+
isEmpty(): boolean {
|
|
50
|
+
return recorded.size === 0;
|
|
51
|
+
},
|
|
52
|
+
buildCombinedAnnotation(state: OnboardingState): string | null {
|
|
53
|
+
if (recorded.size === 0) return null;
|
|
54
|
+
const lines: string[] = [];
|
|
55
|
+
for (const provider of recorded) {
|
|
56
|
+
const descriptor = getProvider(provider);
|
|
57
|
+
const entry = state.providers[provider];
|
|
58
|
+
const silenced = entry?.status === "never_ask";
|
|
59
|
+
const alias = descriptor.aliases[0] ?? descriptor.id;
|
|
60
|
+
const baseRemediation = `run /connect ${alias} to unlock`;
|
|
61
|
+
const remediation = silenced
|
|
62
|
+
? `${baseRemediation} (silenced)`
|
|
63
|
+
: baseRemediation;
|
|
64
|
+
lines.push(
|
|
65
|
+
buildSkippedTag({
|
|
66
|
+
provider,
|
|
67
|
+
reason: "credential_not_provided",
|
|
68
|
+
remediation,
|
|
69
|
+
silenced: silenced || undefined,
|
|
70
|
+
}),
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
return lines.join("\n");
|
|
74
|
+
},
|
|
75
|
+
reset(): void {
|
|
76
|
+
recorded.clear();
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
// Shared prompt primitive used by both the `ask_user` tool and the
|
|
2
|
+
// credential-interception handler in the Pi `tool_result` hook.
|
|
3
|
+
//
|
|
4
|
+
// The original `ask_user` tool embedded its UI routing in a closure inside
|
|
5
|
+
// `execute()`. Extracting it here means the `tool_result` handler can call
|
|
6
|
+
// the same logic without synthesizing a fake tool call (Pi has no "execute
|
|
7
|
+
// a tool now" API), and the headless harness's `askUserHandler` injection
|
|
8
|
+
// point is preserved for automated flows.
|
|
9
|
+
|
|
10
|
+
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
11
|
+
import type { AskUserHandler } from "../types/index.js";
|
|
12
|
+
|
|
13
|
+
export interface PromptOptions {
|
|
14
|
+
question: string;
|
|
15
|
+
questionType: "select" | "text" | "confirm";
|
|
16
|
+
options?: string[];
|
|
17
|
+
placeholder?: string;
|
|
18
|
+
reason?: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface PromptResult {
|
|
22
|
+
answer: string | null;
|
|
23
|
+
cancelled: boolean;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Ask the user a structured question, routing to the appropriate UI primitive
|
|
28
|
+
* (`ctx.ui.select` / `ctx.ui.input` / `ctx.ui.confirm`).
|
|
29
|
+
*
|
|
30
|
+
* When an `askUserHandler` is injected (test harness, programmatic runs), the
|
|
31
|
+
* handler takes precedence over `ctx.ui` and provides the answer directly.
|
|
32
|
+
*
|
|
33
|
+
* When neither a handler nor a UI is available, returns `{answer: null, cancelled: true}`.
|
|
34
|
+
*/
|
|
35
|
+
export async function promptUser(
|
|
36
|
+
ctx: ExtensionContext,
|
|
37
|
+
opts: PromptOptions,
|
|
38
|
+
handler?: AskUserHandler,
|
|
39
|
+
): Promise<PromptResult> {
|
|
40
|
+
// Priority: injected handler > UI > no-UI fallback.
|
|
41
|
+
if (handler) {
|
|
42
|
+
const result = await handler({
|
|
43
|
+
question: opts.question,
|
|
44
|
+
questionType: opts.questionType,
|
|
45
|
+
options: opts.options,
|
|
46
|
+
placeholder: opts.placeholder,
|
|
47
|
+
reason: opts.reason,
|
|
48
|
+
});
|
|
49
|
+
if (result.cancelled) {
|
|
50
|
+
return { answer: null, cancelled: true };
|
|
51
|
+
}
|
|
52
|
+
return { answer: result.answer ?? null, cancelled: false };
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (!ctx?.hasUI) {
|
|
56
|
+
return { answer: null, cancelled: true };
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
switch (opts.questionType) {
|
|
60
|
+
case "select": {
|
|
61
|
+
const options = opts.options ?? [];
|
|
62
|
+
if (options.length === 0) {
|
|
63
|
+
return { answer: null, cancelled: true };
|
|
64
|
+
}
|
|
65
|
+
const choice = await ctx.ui.select(opts.question, options);
|
|
66
|
+
if (choice === undefined) {
|
|
67
|
+
return { answer: null, cancelled: true };
|
|
68
|
+
}
|
|
69
|
+
return { answer: choice, cancelled: false };
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
case "text": {
|
|
73
|
+
const input = await ctx.ui.input(opts.question, opts.placeholder ?? "");
|
|
74
|
+
if (input === undefined || input.trim() === "") {
|
|
75
|
+
return { answer: null, cancelled: true };
|
|
76
|
+
}
|
|
77
|
+
return { answer: input.trim(), cancelled: false };
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
case "confirm": {
|
|
81
|
+
const confirmed = await ctx.ui.confirm(opts.question, opts.reason ?? "");
|
|
82
|
+
return { answer: confirmed ? "Yes" : "No", cancelled: false };
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
// Provider registry — single source of truth for OpenCandle's credentialed
|
|
2
|
+
// third-party data providers. Every setup pathway iterates this registry:
|
|
3
|
+
// first-run startup, the `/connect` command, the `tool_result` credential
|
|
4
|
+
// interception handler, and the gap-note generator all read from here.
|
|
5
|
+
//
|
|
6
|
+
// Adding a new credentialed provider is a two-step change: add its `ProviderId`
|
|
7
|
+
// to the literal union below, and add its descriptor to the `PROVIDERS` array.
|
|
8
|
+
// The `satisfies` check ensures TypeScript fails the build if the union and
|
|
9
|
+
// the array ever disagree.
|
|
10
|
+
|
|
11
|
+
import { getConfig, loadFileConfig } from "../config.js";
|
|
12
|
+
|
|
13
|
+
export type ProviderId =
|
|
14
|
+
| "alpha_vantage"
|
|
15
|
+
| "fred"
|
|
16
|
+
| "finnhub"
|
|
17
|
+
| "brave"
|
|
18
|
+
| "exa";
|
|
19
|
+
|
|
20
|
+
export type ProviderCategory =
|
|
21
|
+
| "fundamentals"
|
|
22
|
+
| "macro"
|
|
23
|
+
| "news"
|
|
24
|
+
| "web_search";
|
|
25
|
+
|
|
26
|
+
export type ProviderTier = "hard" | "soft";
|
|
27
|
+
|
|
28
|
+
export interface ProviderDescriptor {
|
|
29
|
+
readonly id: ProviderId;
|
|
30
|
+
readonly displayName: string;
|
|
31
|
+
readonly category: ProviderCategory;
|
|
32
|
+
/**
|
|
33
|
+
* `hard` providers pause the workflow with a just-in-time prompt when their
|
|
34
|
+
* credential is missing; they have no meaningful fallback.
|
|
35
|
+
*
|
|
36
|
+
* `soft` providers silently use a fallback path and surface a post-answer
|
|
37
|
+
* gap note in the final output.
|
|
38
|
+
*/
|
|
39
|
+
readonly tier: ProviderTier;
|
|
40
|
+
/** Lowercase friendly aliases accepted by `/connect` in addition to the id. */
|
|
41
|
+
readonly aliases: readonly string[];
|
|
42
|
+
readonly signupUrl: string;
|
|
43
|
+
readonly freeTier: boolean;
|
|
44
|
+
readonly envVar: string;
|
|
45
|
+
/** Nested key path into `OpenCandleFileConfig` where the key is persisted. */
|
|
46
|
+
readonly configPath: readonly string[];
|
|
47
|
+
readonly unlocks: readonly string[];
|
|
48
|
+
/**
|
|
49
|
+
* Human copy describing the degraded experience when missing, or `null`
|
|
50
|
+
* when there is no fallback (hard tier).
|
|
51
|
+
*/
|
|
52
|
+
readonly fallbackDescription: string | null;
|
|
53
|
+
readonly snoozeDurationDays: number;
|
|
54
|
+
readonly instructionsHint: string;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// Declaration order matters: picker display order, per-workflow prompt priority,
|
|
58
|
+
// getProvidersByCategory/getProvidersByTier iteration order.
|
|
59
|
+
export const PROVIDERS = [
|
|
60
|
+
{
|
|
61
|
+
id: "alpha_vantage",
|
|
62
|
+
displayName: "Alpha Vantage",
|
|
63
|
+
category: "fundamentals",
|
|
64
|
+
tier: "hard",
|
|
65
|
+
aliases: ["financials", "fundamentals", "company-financials", "alphavantage"],
|
|
66
|
+
signupUrl: "https://www.alphavantage.co/support/#api-key",
|
|
67
|
+
freeTier: true,
|
|
68
|
+
envVar: "ALPHA_VANTAGE_API_KEY",
|
|
69
|
+
configPath: ["providers", "alphaVantage", "apiKey"],
|
|
70
|
+
unlocks: [
|
|
71
|
+
"company fundamentals",
|
|
72
|
+
"income/balance/cashflow statements",
|
|
73
|
+
"DCF valuation",
|
|
74
|
+
"earnings history",
|
|
75
|
+
],
|
|
76
|
+
fallbackDescription: null,
|
|
77
|
+
snoozeDurationDays: 7,
|
|
78
|
+
instructionsHint: "Free, about 30 seconds, signup opens in your browser",
|
|
79
|
+
},
|
|
80
|
+
{
|
|
81
|
+
id: "fred",
|
|
82
|
+
displayName: "FRED",
|
|
83
|
+
category: "macro",
|
|
84
|
+
tier: "hard",
|
|
85
|
+
aliases: ["economy", "macro", "economic-data", "st-louis-fed"],
|
|
86
|
+
signupUrl: "https://fredaccount.stlouisfed.org/apikeys",
|
|
87
|
+
freeTier: true,
|
|
88
|
+
envVar: "FRED_API_KEY",
|
|
89
|
+
configPath: ["providers", "fred", "apiKey"],
|
|
90
|
+
unlocks: [
|
|
91
|
+
"interest rates",
|
|
92
|
+
"inflation data",
|
|
93
|
+
"yield curve",
|
|
94
|
+
"economic indicators",
|
|
95
|
+
],
|
|
96
|
+
fallbackDescription: null,
|
|
97
|
+
snoozeDurationDays: 7,
|
|
98
|
+
instructionsHint: "Free, about 30 seconds, requires a St. Louis Fed account",
|
|
99
|
+
},
|
|
100
|
+
{
|
|
101
|
+
id: "finnhub",
|
|
102
|
+
displayName: "Finnhub",
|
|
103
|
+
category: "news",
|
|
104
|
+
tier: "soft",
|
|
105
|
+
aliases: ["news", "company-news", "finnhub-news"],
|
|
106
|
+
signupUrl: "https://finnhub.io/register",
|
|
107
|
+
freeTier: true,
|
|
108
|
+
envVar: "FINNHUB_API_KEY",
|
|
109
|
+
configPath: ["providers", "finnhub", "apiKey"],
|
|
110
|
+
unlocks: [
|
|
111
|
+
"ticker-tagged company news",
|
|
112
|
+
"sentiment enrichment with a dedicated news source",
|
|
113
|
+
],
|
|
114
|
+
// Finnhub is a soft enrichment source — sentiment-summary continues to work
|
|
115
|
+
// with Twitter/Reddit/web search when Finnhub is missing. The fallback is
|
|
116
|
+
// "the other sentiment sources still run".
|
|
117
|
+
fallbackDescription:
|
|
118
|
+
"Other sentiment sources (Reddit, Twitter, web search) continue to work without Finnhub",
|
|
119
|
+
snoozeDurationDays: 7,
|
|
120
|
+
instructionsHint: "Free, about 30 seconds, signup opens in your browser",
|
|
121
|
+
},
|
|
122
|
+
{
|
|
123
|
+
id: "brave",
|
|
124
|
+
displayName: "Brave Search",
|
|
125
|
+
category: "web_search",
|
|
126
|
+
tier: "soft",
|
|
127
|
+
aliases: ["brave", "brave-search"],
|
|
128
|
+
signupUrl: "https://brave.com/search/api/",
|
|
129
|
+
freeTier: true,
|
|
130
|
+
envVar: "BRAVE_API_KEY",
|
|
131
|
+
configPath: ["providers", "brave", "apiKey"],
|
|
132
|
+
unlocks: [
|
|
133
|
+
"tier-2 web search with freshness control",
|
|
134
|
+
"independent search index outside of DuckDuckGo",
|
|
135
|
+
],
|
|
136
|
+
fallbackDescription:
|
|
137
|
+
"Web search continues to work via DuckDuckGo (free, no key needed, lower-quality freshness)",
|
|
138
|
+
snoozeDurationDays: 7,
|
|
139
|
+
instructionsHint: "Free tier available, signup opens in your browser",
|
|
140
|
+
},
|
|
141
|
+
{
|
|
142
|
+
id: "exa",
|
|
143
|
+
displayName: "Exa",
|
|
144
|
+
category: "web_search",
|
|
145
|
+
tier: "soft",
|
|
146
|
+
// Note: "search" is a multi-provider alias shared with Brave. The registry
|
|
147
|
+
// exposes it via resolveProviderFromArgument as a sub-picker case, not as a
|
|
148
|
+
// single-provider alias — so it intentionally does NOT appear in either
|
|
149
|
+
// provider's `aliases` array here. Keeping aliases unique-per-provider lets
|
|
150
|
+
// resolveProviderFromArgument cleanly distinguish the alias case from the
|
|
151
|
+
// sub-picker case.
|
|
152
|
+
aliases: ["exa", "exa-search"],
|
|
153
|
+
signupUrl: "https://dashboard.exa.ai/",
|
|
154
|
+
freeTier: false,
|
|
155
|
+
envVar: "EXA_API_KEY",
|
|
156
|
+
configPath: ["providers", "exa", "apiKey"],
|
|
157
|
+
unlocks: [
|
|
158
|
+
"tier-1 semantic web search",
|
|
159
|
+
"full article text and highlights",
|
|
160
|
+
"higher freshness accuracy than DuckDuckGo",
|
|
161
|
+
],
|
|
162
|
+
fallbackDescription:
|
|
163
|
+
"Exa search continues to work via the keyless Exa MCP endpoint, which has lower rate limits but similar quality",
|
|
164
|
+
snoozeDurationDays: 7,
|
|
165
|
+
instructionsHint: "Paid with free tier, signup opens in your browser",
|
|
166
|
+
},
|
|
167
|
+
] as const satisfies readonly ProviderDescriptor[];
|
|
168
|
+
|
|
169
|
+
// -----------------------------------------------------------------------------
|
|
170
|
+
// Lookup helpers
|
|
171
|
+
// -----------------------------------------------------------------------------
|
|
172
|
+
|
|
173
|
+
// Lazy-built index — populated on first helper call so module import has no
|
|
174
|
+
// side effects. The test `importing the registry does not read the filesystem`
|
|
175
|
+
// relies on this.
|
|
176
|
+
let providersById: Map<ProviderId, ProviderDescriptor> | undefined;
|
|
177
|
+
|
|
178
|
+
function byId(): Map<ProviderId, ProviderDescriptor> {
|
|
179
|
+
if (!providersById) {
|
|
180
|
+
providersById = new Map(PROVIDERS.map((p) => [p.id, p]));
|
|
181
|
+
}
|
|
182
|
+
return providersById;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
export function listAllProviders(): readonly ProviderDescriptor[] {
|
|
186
|
+
return PROVIDERS;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
export function getProvider(id: ProviderId): ProviderDescriptor {
|
|
190
|
+
const found = byId().get(id);
|
|
191
|
+
if (!found) {
|
|
192
|
+
throw new Error(`Unknown provider id: "${id}"`);
|
|
193
|
+
}
|
|
194
|
+
return found;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
export function getProvidersByCategory(
|
|
198
|
+
category: ProviderCategory,
|
|
199
|
+
): readonly ProviderDescriptor[] {
|
|
200
|
+
return PROVIDERS.filter((p) => p.category === category);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
export function getProvidersByTier(
|
|
204
|
+
tier: ProviderTier,
|
|
205
|
+
): readonly ProviderDescriptor[] {
|
|
206
|
+
return PROVIDERS.filter((p) => p.tier === tier);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// -----------------------------------------------------------------------------
|
|
210
|
+
// Credential source helpers
|
|
211
|
+
// -----------------------------------------------------------------------------
|
|
212
|
+
|
|
213
|
+
function readConfigValueByPath(
|
|
214
|
+
obj: Record<string, unknown>,
|
|
215
|
+
path: readonly string[],
|
|
216
|
+
): string | undefined {
|
|
217
|
+
let cursor: unknown = obj;
|
|
218
|
+
for (const segment of path) {
|
|
219
|
+
if (cursor && typeof cursor === "object" && segment in (cursor as object)) {
|
|
220
|
+
cursor = (cursor as Record<string, unknown>)[segment];
|
|
221
|
+
} else {
|
|
222
|
+
return undefined;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
return typeof cursor === "string" && cursor.length > 0 ? cursor : undefined;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// Provider-id → `Config` field mapping. `Config` (in src/config.ts) is the
|
|
229
|
+
// canonical env-or-file resolved shape that tool implementations already use.
|
|
230
|
+
// `hasCredential` reads from `getConfig()` so that tests mocking `getConfig`
|
|
231
|
+
// see a consistent view; `getCredentialSource` reads `process.env` +
|
|
232
|
+
// `loadFileConfig` directly because it needs to distinguish env from file.
|
|
233
|
+
const CONFIG_FIELD_BY_ID: Record<ProviderId, keyof ReturnType<typeof getConfig>> = {
|
|
234
|
+
alpha_vantage: "alphaVantageApiKey",
|
|
235
|
+
fred: "fredApiKey",
|
|
236
|
+
finnhub: "finnhubApiKey",
|
|
237
|
+
brave: "braveApiKey",
|
|
238
|
+
exa: "exaApiKey",
|
|
239
|
+
};
|
|
240
|
+
|
|
241
|
+
export function hasCredential(id: ProviderId): boolean {
|
|
242
|
+
const field = CONFIG_FIELD_BY_ID[id];
|
|
243
|
+
const value = getConfig()[field];
|
|
244
|
+
return typeof value === "string" && value.length > 0;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
export function getCredentialSource(
|
|
248
|
+
id: ProviderId,
|
|
249
|
+
): "env" | "file" | "absent" {
|
|
250
|
+
return getCredential(id).source;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
export function getCredential(
|
|
254
|
+
id: ProviderId,
|
|
255
|
+
): { source: "env" | "file"; value: string } | { source: "absent"; value?: undefined } {
|
|
256
|
+
const descriptor = getProvider(id);
|
|
257
|
+
const envValue = process.env[descriptor.envVar];
|
|
258
|
+
if (envValue && envValue.length > 0) return { source: "env", value: envValue };
|
|
259
|
+
|
|
260
|
+
// Lazy file-config read — only invoked when env is absent.
|
|
261
|
+
const fileConfig = loadFileConfig() as unknown as Record<string, unknown>;
|
|
262
|
+
const fileValue = readConfigValueByPath(fileConfig, descriptor.configPath);
|
|
263
|
+
if (fileValue) return { source: "file", value: fileValue };
|
|
264
|
+
|
|
265
|
+
return { source: "absent" };
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
// -----------------------------------------------------------------------------
|
|
269
|
+
// /connect argument resolution
|
|
270
|
+
// -----------------------------------------------------------------------------
|
|
271
|
+
|
|
272
|
+
export function resolveProviderFromArgument(
|
|
273
|
+
arg: string,
|
|
274
|
+
):
|
|
275
|
+
| ProviderDescriptor
|
|
276
|
+
| readonly ProviderDescriptor[]
|
|
277
|
+
| undefined {
|
|
278
|
+
const needle = arg.trim().toLowerCase();
|
|
279
|
+
if (!needle) return undefined;
|
|
280
|
+
|
|
281
|
+
// 1. Exact provider id match (case-insensitive).
|
|
282
|
+
for (const p of PROVIDERS) {
|
|
283
|
+
if (p.id === needle) return p;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
// 2. Exact alias match.
|
|
287
|
+
for (const p of PROVIDERS) {
|
|
288
|
+
if ((p.aliases as readonly string[]).includes(needle)) return p;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
// 3. Category match: if the needle matches a category name, return the
|
|
292
|
+
// providers in that category. One match → single descriptor. Multiple
|
|
293
|
+
// matches → array (triggers the sub-picker in the /connect handler).
|
|
294
|
+
const categories: readonly ProviderCategory[] = [
|
|
295
|
+
"fundamentals",
|
|
296
|
+
"macro",
|
|
297
|
+
"news",
|
|
298
|
+
"web_search",
|
|
299
|
+
];
|
|
300
|
+
const normalizedCategory = needle.replace("-", "_");
|
|
301
|
+
if ((categories as readonly string[]).includes(normalizedCategory)) {
|
|
302
|
+
const group = getProvidersByCategory(normalizedCategory as ProviderCategory);
|
|
303
|
+
if (group.length === 1) return group[0];
|
|
304
|
+
if (group.length > 1) return group;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
// 4. Special shared alias: "search" → both web_search providers.
|
|
308
|
+
if (needle === "search" || needle === "web" || needle === "web-search") {
|
|
309
|
+
const searchProviders = getProvidersByCategory("web_search");
|
|
310
|
+
if (searchProviders.length === 1) return searchProviders[0];
|
|
311
|
+
if (searchProviders.length > 1) return searchProviders;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
return undefined;
|
|
315
|
+
}
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
// Onboarding state schema and pure helpers.
|
|
2
|
+
//
|
|
3
|
+
// Shape: one flag for the welcome message (`welcomeShownAt`) plus a partial
|
|
4
|
+
// per-provider map with a proper discriminated union for status + snooze.
|
|
5
|
+
//
|
|
6
|
+
// All transition helpers are PURE — they return a new state object rather
|
|
7
|
+
// than mutating. The caller is responsible for persisting via saveOnboardingState.
|
|
8
|
+
|
|
9
|
+
import { existsSync, readFileSync, writeFileSync } from "node:fs";
|
|
10
|
+
import { ensureParentDir, getOnboardingPath } from "../infra/opencandle-paths.js";
|
|
11
|
+
import type { ProviderId } from "./providers.js";
|
|
12
|
+
|
|
13
|
+
export const ONBOARDING_VERSION = 2;
|
|
14
|
+
|
|
15
|
+
export type ProviderOnboardingEntry =
|
|
16
|
+
| { status: "completed"; lastPromptAt: string }
|
|
17
|
+
| { status: "snoozed"; lastPromptAt: string; snoozeUntil: string }
|
|
18
|
+
| { status: "never_ask"; lastPromptAt: string };
|
|
19
|
+
|
|
20
|
+
export interface OnboardingState {
|
|
21
|
+
version: number;
|
|
22
|
+
/** ISO 8601 timestamp the welcome message was first seeded, or undefined if never. */
|
|
23
|
+
welcomeShownAt?: string;
|
|
24
|
+
providers: Partial<Record<ProviderId, ProviderOnboardingEntry>>;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function getDefaultOnboardingState(): OnboardingState {
|
|
28
|
+
return { version: ONBOARDING_VERSION, providers: {} };
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// -----------------------------------------------------------------------------
|
|
32
|
+
// Load / save
|
|
33
|
+
// -----------------------------------------------------------------------------
|
|
34
|
+
|
|
35
|
+
const STATUS_VALUES: ReadonlySet<string> = new Set(["completed", "snoozed", "never_ask"]);
|
|
36
|
+
|
|
37
|
+
function parseEntry(raw: unknown): ProviderOnboardingEntry | undefined {
|
|
38
|
+
if (!raw || typeof raw !== "object") return undefined;
|
|
39
|
+
const obj = raw as Record<string, unknown>;
|
|
40
|
+
const status = obj.status;
|
|
41
|
+
if (typeof status !== "string" || !STATUS_VALUES.has(status)) return undefined;
|
|
42
|
+
const lastPromptAt = obj.lastPromptAt;
|
|
43
|
+
if (typeof lastPromptAt !== "string") return undefined;
|
|
44
|
+
|
|
45
|
+
if (status === "snoozed") {
|
|
46
|
+
const snoozeUntil = obj.snoozeUntil;
|
|
47
|
+
if (typeof snoozeUntil !== "string") return undefined;
|
|
48
|
+
return { status: "snoozed", lastPromptAt, snoozeUntil };
|
|
49
|
+
}
|
|
50
|
+
if (status === "completed") {
|
|
51
|
+
return { status: "completed", lastPromptAt };
|
|
52
|
+
}
|
|
53
|
+
return { status: "never_ask", lastPromptAt };
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function parseProvidersMap(
|
|
57
|
+
raw: unknown,
|
|
58
|
+
): Partial<Record<ProviderId, ProviderOnboardingEntry>> {
|
|
59
|
+
if (!raw || typeof raw !== "object") return {};
|
|
60
|
+
const result: Partial<Record<ProviderId, ProviderOnboardingEntry>> = {};
|
|
61
|
+
for (const [key, value] of Object.entries(raw as Record<string, unknown>)) {
|
|
62
|
+
const entry = parseEntry(value);
|
|
63
|
+
if (entry) result[key as ProviderId] = entry;
|
|
64
|
+
}
|
|
65
|
+
return result;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function loadOnboardingState(path = getOnboardingPath()): OnboardingState {
|
|
69
|
+
if (!existsSync(path)) {
|
|
70
|
+
return getDefaultOnboardingState();
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
let raw: string;
|
|
74
|
+
try {
|
|
75
|
+
raw = readFileSync(path, "utf-8");
|
|
76
|
+
} catch {
|
|
77
|
+
return getDefaultOnboardingState();
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
let parsed: unknown;
|
|
81
|
+
try {
|
|
82
|
+
parsed = JSON.parse(raw);
|
|
83
|
+
} catch {
|
|
84
|
+
return getDefaultOnboardingState();
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
if (!parsed || typeof parsed !== "object") {
|
|
88
|
+
return getDefaultOnboardingState();
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const obj = parsed as Record<string, unknown>;
|
|
92
|
+
const version = typeof obj.version === "number" ? obj.version : ONBOARDING_VERSION;
|
|
93
|
+
const welcomeShownAt = typeof obj.welcomeShownAt === "string" ? obj.welcomeShownAt : undefined;
|
|
94
|
+
const providers = parseProvidersMap(obj.providers);
|
|
95
|
+
|
|
96
|
+
return { version, welcomeShownAt, providers };
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export function saveOnboardingState(
|
|
100
|
+
state: OnboardingState,
|
|
101
|
+
path = getOnboardingPath(),
|
|
102
|
+
): void {
|
|
103
|
+
ensureParentDir(path);
|
|
104
|
+
// Strip undefined fields for cleaner on-disk output.
|
|
105
|
+
const serializable: Record<string, unknown> = {
|
|
106
|
+
version: state.version,
|
|
107
|
+
providers: state.providers,
|
|
108
|
+
};
|
|
109
|
+
if (state.welcomeShownAt !== undefined) {
|
|
110
|
+
serializable.welcomeShownAt = state.welcomeShownAt;
|
|
111
|
+
}
|
|
112
|
+
writeFileSync(path, `${JSON.stringify(serializable, null, 2)}\n`, "utf-8");
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// -----------------------------------------------------------------------------
|
|
116
|
+
// Pure transitions
|
|
117
|
+
// -----------------------------------------------------------------------------
|
|
118
|
+
|
|
119
|
+
function nowIso(): string {
|
|
120
|
+
return new Date().toISOString();
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export function markProviderCompleted(
|
|
124
|
+
state: OnboardingState,
|
|
125
|
+
id: ProviderId,
|
|
126
|
+
): OnboardingState {
|
|
127
|
+
return {
|
|
128
|
+
...state,
|
|
129
|
+
providers: {
|
|
130
|
+
...state.providers,
|
|
131
|
+
[id]: { status: "completed", lastPromptAt: nowIso() },
|
|
132
|
+
},
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
export function markProviderSnoozed(
|
|
137
|
+
state: OnboardingState,
|
|
138
|
+
id: ProviderId,
|
|
139
|
+
days: number,
|
|
140
|
+
): OnboardingState {
|
|
141
|
+
const now = new Date();
|
|
142
|
+
const snoozeUntil = new Date(now.getTime() + days * 24 * 3600 * 1000);
|
|
143
|
+
return {
|
|
144
|
+
...state,
|
|
145
|
+
providers: {
|
|
146
|
+
...state.providers,
|
|
147
|
+
[id]: {
|
|
148
|
+
status: "snoozed",
|
|
149
|
+
lastPromptAt: now.toISOString(),
|
|
150
|
+
snoozeUntil: snoozeUntil.toISOString(),
|
|
151
|
+
},
|
|
152
|
+
},
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export function markProviderNeverAsk(
|
|
157
|
+
state: OnboardingState,
|
|
158
|
+
id: ProviderId,
|
|
159
|
+
): OnboardingState {
|
|
160
|
+
return {
|
|
161
|
+
...state,
|
|
162
|
+
providers: {
|
|
163
|
+
...state.providers,
|
|
164
|
+
[id]: { status: "never_ask", lastPromptAt: nowIso() },
|
|
165
|
+
},
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
export function markWelcomeShown(state: OnboardingState): OnboardingState {
|
|
170
|
+
return { ...state, welcomeShownAt: nowIso() };
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// -----------------------------------------------------------------------------
|
|
174
|
+
// Pure queries
|
|
175
|
+
// -----------------------------------------------------------------------------
|
|
176
|
+
|
|
177
|
+
export function getProviderEntry(
|
|
178
|
+
state: OnboardingState,
|
|
179
|
+
id: ProviderId,
|
|
180
|
+
): ProviderOnboardingEntry | undefined {
|
|
181
|
+
return state.providers[id];
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
export interface ShouldPromptOptions {
|
|
185
|
+
/**
|
|
186
|
+
* When true, treats a `completed` entry as eligible for re-prompt. This is
|
|
187
|
+
* how stale credentials (401 after a previously-connected key) get a fresh
|
|
188
|
+
* offer.
|
|
189
|
+
*/
|
|
190
|
+
stale?: boolean;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
export function shouldPrompt(
|
|
194
|
+
state: OnboardingState,
|
|
195
|
+
id: ProviderId,
|
|
196
|
+
now: Date,
|
|
197
|
+
options: ShouldPromptOptions = {},
|
|
198
|
+
): boolean {
|
|
199
|
+
const entry = state.providers[id];
|
|
200
|
+
if (!entry) return true;
|
|
201
|
+
|
|
202
|
+
switch (entry.status) {
|
|
203
|
+
case "never_ask":
|
|
204
|
+
return false;
|
|
205
|
+
case "completed":
|
|
206
|
+
return options.stale === true;
|
|
207
|
+
case "snoozed": {
|
|
208
|
+
const until = Date.parse(entry.snoozeUntil);
|
|
209
|
+
if (Number.isNaN(until)) return true;
|
|
210
|
+
return now.getTime() >= until;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
export function shouldShowWelcome(state: OnboardingState, hasUI: boolean): boolean {
|
|
216
|
+
if (!hasUI) return false;
|
|
217
|
+
return state.welcomeShownAt === undefined;
|
|
218
|
+
}
|