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,205 @@
|
|
|
1
|
+
import type Database from "better-sqlite3";
|
|
2
|
+
|
|
3
|
+
interface PreferenceInput {
|
|
4
|
+
namespace?: string;
|
|
5
|
+
key: string;
|
|
6
|
+
valueJson: string;
|
|
7
|
+
confidence?: string;
|
|
8
|
+
source?: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface WorkflowPreferences {
|
|
12
|
+
riskProfile?: string;
|
|
13
|
+
timeHorizon?: string;
|
|
14
|
+
assetScope?: string;
|
|
15
|
+
positionCount?: number;
|
|
16
|
+
maxSinglePositionPct?: number;
|
|
17
|
+
dteTarget?: string;
|
|
18
|
+
objective?: string;
|
|
19
|
+
moneynessPreference?: string;
|
|
20
|
+
liquidityMinimum?: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
interface WorkflowRunInput {
|
|
24
|
+
sessionId: string;
|
|
25
|
+
workflowType: string;
|
|
26
|
+
inputSlotsJson: string;
|
|
27
|
+
resolvedSlotsJson: string;
|
|
28
|
+
defaultsUsedJson: string;
|
|
29
|
+
outputSummary?: string;
|
|
30
|
+
/**
|
|
31
|
+
* Router route verbatim. Legacy rows contain `"workflow"` or `"fallback"`;
|
|
32
|
+
* typed-router rows may contain canonical route kinds. Defaults to
|
|
33
|
+
* `"workflow"` at the schema layer so legacy callers (rules-mode cascade)
|
|
34
|
+
* don't need to pass anything. Router-mode callers MUST pass this explicitly.
|
|
35
|
+
*/
|
|
36
|
+
turnType?: string;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
interface RecommendationInput {
|
|
40
|
+
workflowRunId: number;
|
|
41
|
+
recommendationType: string;
|
|
42
|
+
symbol?: string;
|
|
43
|
+
payloadJson?: string;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export class MemoryStorage {
|
|
47
|
+
constructor(private readonly db: Database.Database) {}
|
|
48
|
+
|
|
49
|
+
// --- Preferences ---
|
|
50
|
+
|
|
51
|
+
upsertPreference(input: PreferenceInput): void {
|
|
52
|
+
const now = new Date().toISOString();
|
|
53
|
+
const ns = input.namespace ?? "global";
|
|
54
|
+
const confidence = input.confidence ?? "medium";
|
|
55
|
+
const source = input.source ?? "explicit";
|
|
56
|
+
|
|
57
|
+
this.db
|
|
58
|
+
.prepare(
|
|
59
|
+
`INSERT INTO user_preferences (namespace, key, value_json, confidence, source, created_at, updated_at)
|
|
60
|
+
VALUES (?, ?, ?, ?, ?, ?, ?)
|
|
61
|
+
ON CONFLICT(namespace, key) DO UPDATE SET
|
|
62
|
+
value_json = excluded.value_json,
|
|
63
|
+
confidence = excluded.confidence,
|
|
64
|
+
source = excluded.source,
|
|
65
|
+
updated_at = excluded.updated_at`,
|
|
66
|
+
)
|
|
67
|
+
.run(ns, input.key, input.valueJson, confidence, source, now, now);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
getPreference(
|
|
71
|
+
namespace: string,
|
|
72
|
+
key: string,
|
|
73
|
+
): Record<string, string | number | null> | null {
|
|
74
|
+
return (
|
|
75
|
+
(this.db
|
|
76
|
+
.prepare("SELECT * FROM user_preferences WHERE namespace = ? AND key = ?")
|
|
77
|
+
.get(namespace, key) as Record<string, string | number | null>) ?? null
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
getPreferencesByNamespace(
|
|
82
|
+
namespace: string,
|
|
83
|
+
): Array<Record<string, string | number | null>> {
|
|
84
|
+
return this.db
|
|
85
|
+
.prepare("SELECT * FROM user_preferences WHERE namespace = ?")
|
|
86
|
+
.all(namespace) as Array<Record<string, string | number | null>>;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
getWorkflowPreferences(namespace: string = "global"): WorkflowPreferences {
|
|
90
|
+
const prefs = this.getPreferencesByNamespace(namespace);
|
|
91
|
+
const out: WorkflowPreferences = {};
|
|
92
|
+
|
|
93
|
+
for (const pref of prefs) {
|
|
94
|
+
const key = String(pref.key);
|
|
95
|
+
const raw = pref.value_json == null ? undefined : safeParseJson(String(pref.value_json));
|
|
96
|
+
|
|
97
|
+
switch (key) {
|
|
98
|
+
case "risk_profile":
|
|
99
|
+
if (typeof raw === "string") out.riskProfile = raw;
|
|
100
|
+
break;
|
|
101
|
+
case "time_horizon":
|
|
102
|
+
if (typeof raw === "string") out.timeHorizon = raw;
|
|
103
|
+
break;
|
|
104
|
+
case "asset_scope":
|
|
105
|
+
if (typeof raw === "string") out.assetScope = raw;
|
|
106
|
+
break;
|
|
107
|
+
case "position_count":
|
|
108
|
+
if (typeof raw === "number") out.positionCount = raw;
|
|
109
|
+
break;
|
|
110
|
+
case "max_single_position_pct":
|
|
111
|
+
if (typeof raw === "number") out.maxSinglePositionPct = raw;
|
|
112
|
+
break;
|
|
113
|
+
case "dte_target":
|
|
114
|
+
if (typeof raw === "string") out.dteTarget = raw;
|
|
115
|
+
break;
|
|
116
|
+
case "objective":
|
|
117
|
+
if (typeof raw === "string") out.objective = raw;
|
|
118
|
+
break;
|
|
119
|
+
case "moneyness_preference":
|
|
120
|
+
if (typeof raw === "string") out.moneynessPreference = raw;
|
|
121
|
+
break;
|
|
122
|
+
case "options_liquidity":
|
|
123
|
+
case "liquidity_minimum":
|
|
124
|
+
if (typeof raw === "string") {
|
|
125
|
+
out.liquidityMinimum =
|
|
126
|
+
raw === "high" ? "high_open_interest_and_tight_spread" : raw;
|
|
127
|
+
}
|
|
128
|
+
break;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
return out;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// --- Workflow Runs ---
|
|
136
|
+
|
|
137
|
+
insertWorkflowRun(input: WorkflowRunInput): number {
|
|
138
|
+
const now = new Date().toISOString();
|
|
139
|
+
const result = this.db
|
|
140
|
+
.prepare(
|
|
141
|
+
`INSERT INTO workflow_runs (session_id, workflow_type, input_slots_json, resolved_slots_json, defaults_used_json, output_summary, created_at, turn_type)
|
|
142
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
143
|
+
)
|
|
144
|
+
.run(
|
|
145
|
+
input.sessionId,
|
|
146
|
+
input.workflowType,
|
|
147
|
+
input.inputSlotsJson,
|
|
148
|
+
input.resolvedSlotsJson,
|
|
149
|
+
input.defaultsUsedJson,
|
|
150
|
+
input.outputSummary ?? null,
|
|
151
|
+
now,
|
|
152
|
+
input.turnType ?? "workflow",
|
|
153
|
+
);
|
|
154
|
+
return Number(result.lastInsertRowid);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
getRecentWorkflowRuns(
|
|
158
|
+
limit: number,
|
|
159
|
+
): Array<Record<string, string | number | null>> {
|
|
160
|
+
return this.db
|
|
161
|
+
.prepare("SELECT * FROM workflow_runs ORDER BY id DESC LIMIT ?")
|
|
162
|
+
.all(limit) as Array<Record<string, string | number | null>>;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
updateWorkflowRunOutputSummary(workflowRunId: number, outputSummary: string): void {
|
|
166
|
+
this.db
|
|
167
|
+
.prepare("UPDATE workflow_runs SET output_summary = ? WHERE id = ?")
|
|
168
|
+
.run(outputSummary, workflowRunId);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// --- Recommendations ---
|
|
172
|
+
|
|
173
|
+
insertRecommendation(input: RecommendationInput): number {
|
|
174
|
+
const now = new Date().toISOString();
|
|
175
|
+
const result = this.db
|
|
176
|
+
.prepare(
|
|
177
|
+
`INSERT INTO recommendations (workflow_run_id, recommendation_type, symbol, payload_json, created_at)
|
|
178
|
+
VALUES (?, ?, ?, ?, ?)`,
|
|
179
|
+
)
|
|
180
|
+
.run(
|
|
181
|
+
input.workflowRunId,
|
|
182
|
+
input.recommendationType,
|
|
183
|
+
input.symbol ?? null,
|
|
184
|
+
input.payloadJson ?? null,
|
|
185
|
+
now,
|
|
186
|
+
);
|
|
187
|
+
return Number(result.lastInsertRowid);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
getRecommendationsByRun(
|
|
191
|
+
workflowRunId: number,
|
|
192
|
+
): Array<Record<string, string | number | null>> {
|
|
193
|
+
return this.db
|
|
194
|
+
.prepare("SELECT * FROM recommendations WHERE workflow_run_id = ? ORDER BY id")
|
|
195
|
+
.all(workflowRunId) as Array<Record<string, string | number | null>>;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
function safeParseJson(json: string): unknown {
|
|
200
|
+
try {
|
|
201
|
+
return JSON.parse(json);
|
|
202
|
+
} catch {
|
|
203
|
+
return json;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { initDefaultDatabase } from "./sqlite.js";
|
|
2
|
+
|
|
3
|
+
export type ToolDefaults = Record<string, unknown>;
|
|
4
|
+
type SqliteDb = ReturnType<typeof initDefaultDatabase>;
|
|
5
|
+
|
|
6
|
+
export function getDefaults(toolName: string, db: SqliteDb = initDefaultDatabase()): ToolDefaults {
|
|
7
|
+
const rows = db
|
|
8
|
+
.prepare(
|
|
9
|
+
`SELECT param_path, value_json FROM tool_defaults WHERE tool_name = ? ORDER BY param_path`,
|
|
10
|
+
)
|
|
11
|
+
.all(toolName) as Array<{ param_path: string; value_json: string }>;
|
|
12
|
+
|
|
13
|
+
const defaults: ToolDefaults = {};
|
|
14
|
+
for (const row of rows) {
|
|
15
|
+
setPath(defaults, row.param_path, parseStoredValue(row.value_json));
|
|
16
|
+
}
|
|
17
|
+
return defaults;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function getAllDefaults(db: SqliteDb = initDefaultDatabase()): Map<string, ToolDefaults> {
|
|
21
|
+
const rows = db
|
|
22
|
+
.prepare(
|
|
23
|
+
`SELECT tool_name, param_path, value_json FROM tool_defaults ORDER BY tool_name, param_path`,
|
|
24
|
+
)
|
|
25
|
+
.all() as Array<{ tool_name: string; param_path: string; value_json: string }>;
|
|
26
|
+
|
|
27
|
+
const groups = new Map<string, ToolDefaults>();
|
|
28
|
+
for (const row of rows) {
|
|
29
|
+
const defaults = groups.get(row.tool_name) ?? {};
|
|
30
|
+
setPath(defaults, row.param_path, parseStoredValue(row.value_json));
|
|
31
|
+
groups.set(row.tool_name, defaults);
|
|
32
|
+
}
|
|
33
|
+
return groups;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function setDefault(
|
|
37
|
+
toolName: string,
|
|
38
|
+
paramPath: string,
|
|
39
|
+
value: unknown,
|
|
40
|
+
db: SqliteDb = initDefaultDatabase(),
|
|
41
|
+
): void {
|
|
42
|
+
db.prepare(
|
|
43
|
+
`INSERT INTO tool_defaults (tool_name, param_path, value_json, set_at)
|
|
44
|
+
VALUES (?, ?, ?, ?)
|
|
45
|
+
ON CONFLICT(tool_name, param_path) DO UPDATE SET
|
|
46
|
+
value_json = excluded.value_json,
|
|
47
|
+
set_at = excluded.set_at`,
|
|
48
|
+
).run(toolName, paramPath, JSON.stringify(value), new Date().toISOString());
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function clearDefault(
|
|
52
|
+
toolName: string,
|
|
53
|
+
paramPath: string,
|
|
54
|
+
db: SqliteDb = initDefaultDatabase(),
|
|
55
|
+
): void {
|
|
56
|
+
db.prepare(`DELETE FROM tool_defaults WHERE tool_name = ? AND param_path = ?`).run(
|
|
57
|
+
toolName,
|
|
58
|
+
paramPath,
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function parseStoredValue(valueJson: string): unknown {
|
|
63
|
+
try {
|
|
64
|
+
return JSON.parse(valueJson);
|
|
65
|
+
} catch {
|
|
66
|
+
return valueJson;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function setPath(target: ToolDefaults, path: string, value: unknown): void {
|
|
71
|
+
const parts = path.split(".").filter(Boolean);
|
|
72
|
+
if (parts.length === 0) return;
|
|
73
|
+
|
|
74
|
+
let cursor: Record<string, unknown> = target;
|
|
75
|
+
for (const part of parts.slice(0, -1)) {
|
|
76
|
+
const existing = cursor[part];
|
|
77
|
+
if (!isPlainObject(existing)) {
|
|
78
|
+
cursor[part] = {};
|
|
79
|
+
}
|
|
80
|
+
cursor = cursor[part] as Record<string, unknown>;
|
|
81
|
+
}
|
|
82
|
+
cursor[parts[parts.length - 1]] = value;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function isPlainObject(value: unknown): value is Record<string, unknown> {
|
|
86
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
87
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/** Memory categories for typed, selective retrieval. */
|
|
2
|
+
export type MemoryCategory =
|
|
3
|
+
| "investor_profile"
|
|
4
|
+
| "interaction_feedback"
|
|
5
|
+
| "workflow_history"
|
|
6
|
+
| "references";
|
|
7
|
+
|
|
8
|
+
/** A memory entry with category and freshness metadata. */
|
|
9
|
+
export interface MemoryEntry {
|
|
10
|
+
key: string;
|
|
11
|
+
value: string;
|
|
12
|
+
category: MemoryCategory;
|
|
13
|
+
recordedAt: string;
|
|
14
|
+
confidence?: string;
|
|
15
|
+
source?: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** Staleness thresholds in milliseconds per category. */
|
|
19
|
+
export const STALENESS_THRESHOLDS: Record<MemoryCategory, number> = {
|
|
20
|
+
investor_profile: 90 * 24 * 60 * 60 * 1000, // 90 days
|
|
21
|
+
interaction_feedback: 14 * 24 * 60 * 60 * 1000, // 14 days
|
|
22
|
+
workflow_history: 7 * 24 * 60 * 60 * 1000, // 7 days
|
|
23
|
+
references: 30 * 24 * 60 * 60 * 1000, // 30 days
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
/** Map preference keys to memory categories. */
|
|
27
|
+
export const KEY_TO_CATEGORY: Record<string, MemoryCategory> = {
|
|
28
|
+
risk_profile: "investor_profile",
|
|
29
|
+
time_horizon: "investor_profile",
|
|
30
|
+
asset_scope: "investor_profile",
|
|
31
|
+
position_count: "investor_profile",
|
|
32
|
+
max_single_position_pct: "investor_profile",
|
|
33
|
+
account_type: "investor_profile",
|
|
34
|
+
income_vs_growth: "investor_profile",
|
|
35
|
+
dte_target: "investor_profile",
|
|
36
|
+
objective: "investor_profile",
|
|
37
|
+
moneyness_preference: "investor_profile",
|
|
38
|
+
liquidity_minimum: "investor_profile",
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
/** Categories relevant to each workflow type. */
|
|
42
|
+
export const WORKFLOW_RELEVANT_CATEGORIES: Record<string, MemoryCategory[]> = {
|
|
43
|
+
portfolio_builder: ["investor_profile", "interaction_feedback", "workflow_history"],
|
|
44
|
+
options_screener: ["investor_profile", "interaction_feedback", "workflow_history"],
|
|
45
|
+
compare_assets: ["investor_profile", "workflow_history"],
|
|
46
|
+
comprehensive_analysis: ["investor_profile", "workflow_history"],
|
|
47
|
+
single_asset_analysis: ["investor_profile"],
|
|
48
|
+
general_finance_qa: ["investor_profile"],
|
|
49
|
+
workflow_dispatch: ["investor_profile", "workflow_history"],
|
|
50
|
+
agent_task: ["investor_profile", "workflow_history"],
|
|
51
|
+
clarification: ["investor_profile", "workflow_history"],
|
|
52
|
+
pass_through: [],
|
|
53
|
+
unclassified: ["investor_profile"],
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
/** Check whether a memory entry is stale. */
|
|
57
|
+
export function isStale(entry: MemoryEntry, now: Date = new Date()): boolean {
|
|
58
|
+
const threshold = STALENESS_THRESHOLDS[entry.category];
|
|
59
|
+
const age = now.getTime() - new Date(entry.recordedAt).getTime();
|
|
60
|
+
return age > threshold;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** Keys whose values are market-sensitive and must never be trusted from memory. */
|
|
64
|
+
export const NEVER_TRUST_FROM_MEMORY = new Set([
|
|
65
|
+
"stock_price",
|
|
66
|
+
"crypto_price",
|
|
67
|
+
"market_thesis",
|
|
68
|
+
"target_price",
|
|
69
|
+
"entry_price",
|
|
70
|
+
"stop_loss",
|
|
71
|
+
]);
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
// Shared `runProviderConnect` function — the actual side-effectful flow that
|
|
2
|
+
// opens a browser, collects a pasted API key, validates it against the
|
|
3
|
+
// provider's API, persists it to `~/.opencandle/config.json`, and refreshes
|
|
4
|
+
// the cached Config so the next tool call sees the new credential.
|
|
5
|
+
//
|
|
6
|
+
// Called from two places:
|
|
7
|
+
// 1. The Pi `tool_result` extension handler when the user picks "Connect now"
|
|
8
|
+
// in the just-in-time prompt (Task Group 10).
|
|
9
|
+
// 2. The `/connect` Pi command (Task Group 13).
|
|
10
|
+
//
|
|
11
|
+
// Validation (Task Group 8 final pass): before persisting, the pasted key is
|
|
12
|
+
// validated via `validateCredential`, which makes a single cheap request to
|
|
13
|
+
// the provider's canonical API and classifies the response. A hard
|
|
14
|
+
// auth failure (401/403 or a provider-specific error-in-200-body) is
|
|
15
|
+
// returned as `invalid_key` WITHOUT persisting the bad value; a transient
|
|
16
|
+
// failure (timeout, 5xx, network error) warns the user but still persists
|
|
17
|
+
// the key so they aren't blocked on a provider outage — the next real tool
|
|
18
|
+
// call will surface any lingering issue via the credential-required tag.
|
|
19
|
+
|
|
20
|
+
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
21
|
+
import { openInBrowser } from "../infra/open-url.js";
|
|
22
|
+
import {
|
|
23
|
+
loadFileConfig,
|
|
24
|
+
saveFileConfig,
|
|
25
|
+
loadConfig,
|
|
26
|
+
type OpenCandleFileConfig,
|
|
27
|
+
} from "../config.js";
|
|
28
|
+
import {
|
|
29
|
+
getProvider,
|
|
30
|
+
getCredentialSource,
|
|
31
|
+
type ProviderId,
|
|
32
|
+
} from "./providers.js";
|
|
33
|
+
import {
|
|
34
|
+
loadOnboardingState,
|
|
35
|
+
markProviderCompleted,
|
|
36
|
+
saveOnboardingState,
|
|
37
|
+
} from "./state.js";
|
|
38
|
+
import { validateCredential } from "./validation.js";
|
|
39
|
+
|
|
40
|
+
export type ConnectResult =
|
|
41
|
+
| { status: "connected" }
|
|
42
|
+
| { status: "cancelled" }
|
|
43
|
+
| { status: "blocked_by_env" }
|
|
44
|
+
| { status: "invalid_key"; httpStatus?: number; message?: string };
|
|
45
|
+
|
|
46
|
+
function writeNested(
|
|
47
|
+
obj: Record<string, unknown>,
|
|
48
|
+
path: readonly string[],
|
|
49
|
+
value: string,
|
|
50
|
+
): Record<string, unknown> {
|
|
51
|
+
if (path.length === 0) return obj;
|
|
52
|
+
const [head, ...rest] = path;
|
|
53
|
+
const next = { ...obj };
|
|
54
|
+
if (rest.length === 0) {
|
|
55
|
+
next[head] = value;
|
|
56
|
+
} else {
|
|
57
|
+
const child =
|
|
58
|
+
next[head] && typeof next[head] === "object"
|
|
59
|
+
? (next[head] as Record<string, unknown>)
|
|
60
|
+
: {};
|
|
61
|
+
next[head] = writeNested(child, rest, value);
|
|
62
|
+
}
|
|
63
|
+
return next;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Persist an already-validated provider credential.
|
|
68
|
+
*
|
|
69
|
+
* Writes the key into `~/.opencandle/config.json` (preserving sibling fields),
|
|
70
|
+
* refreshes the cached `Config` so the next tool call sees the new value, and
|
|
71
|
+
* marks the provider as completed in the onboarding state.
|
|
72
|
+
*
|
|
73
|
+
* Shared by the TUI `/connect` flow (`runProviderConnect`) and the GUI
|
|
74
|
+
* provider-setup form. Validation is the caller's responsibility — both
|
|
75
|
+
* call sites run `validateCredential` before invoking this helper.
|
|
76
|
+
*/
|
|
77
|
+
export function persistProviderCredential(
|
|
78
|
+
providerId: ProviderId,
|
|
79
|
+
key: string,
|
|
80
|
+
): void {
|
|
81
|
+
const descriptor = getProvider(providerId);
|
|
82
|
+
const existing = loadFileConfig() as unknown as Record<string, unknown>;
|
|
83
|
+
const updated = writeNested(
|
|
84
|
+
existing,
|
|
85
|
+
descriptor.configPath,
|
|
86
|
+
key,
|
|
87
|
+
) as OpenCandleFileConfig;
|
|
88
|
+
saveFileConfig(updated);
|
|
89
|
+
loadConfig();
|
|
90
|
+
const state = loadOnboardingState();
|
|
91
|
+
saveOnboardingState(markProviderCompleted(state, providerId));
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Run the connect flow for a single provider.
|
|
96
|
+
*
|
|
97
|
+
* Env-precedence short-circuit: if the provider's configured credential
|
|
98
|
+
* already comes from an environment variable, the file-config write would
|
|
99
|
+
* be invisible to the next tool call (env vars win in `resolveConfig`).
|
|
100
|
+
* Notify the user explicitly and return `blocked_by_env` — do not open
|
|
101
|
+
* the browser or collect a pasted value.
|
|
102
|
+
*/
|
|
103
|
+
export async function runProviderConnect(
|
|
104
|
+
ctx: ExtensionContext,
|
|
105
|
+
providerId: ProviderId,
|
|
106
|
+
): Promise<ConnectResult> {
|
|
107
|
+
const descriptor = getProvider(providerId);
|
|
108
|
+
|
|
109
|
+
// Env-precedence check.
|
|
110
|
+
if (getCredentialSource(providerId) === "env") {
|
|
111
|
+
ctx.ui.notify(
|
|
112
|
+
`${descriptor.displayName} is currently set via the ${descriptor.envVar} environment variable. ` +
|
|
113
|
+
`To change it from here, unset that variable first and reopen /connect ${descriptor.aliases[0] ?? descriptor.id}. ` +
|
|
114
|
+
`Otherwise, update ${descriptor.envVar} directly in your shell profile.`,
|
|
115
|
+
"warning",
|
|
116
|
+
);
|
|
117
|
+
return { status: "blocked_by_env" };
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// Open the signup URL in the user's browser. We ignore errors here — if
|
|
121
|
+
// the browser can't be opened, the user can still paste a key they
|
|
122
|
+
// already have, so we continue rather than bailing.
|
|
123
|
+
await openInBrowser(descriptor.signupUrl).catch(() => {});
|
|
124
|
+
ctx.ui.notify(
|
|
125
|
+
`Opening ${descriptor.displayName} signup in your browser...`,
|
|
126
|
+
"info",
|
|
127
|
+
);
|
|
128
|
+
|
|
129
|
+
// Prompt for the key. Uses the provider's instructionsHint as the
|
|
130
|
+
// placeholder text to remind the user what they're pasting.
|
|
131
|
+
const pasted = await ctx.ui.input(
|
|
132
|
+
`Paste your ${descriptor.displayName} API key`,
|
|
133
|
+
descriptor.instructionsHint,
|
|
134
|
+
);
|
|
135
|
+
if (!pasted) {
|
|
136
|
+
return { status: "cancelled" };
|
|
137
|
+
}
|
|
138
|
+
const trimmed = pasted.trim();
|
|
139
|
+
if (trimmed.length === 0) {
|
|
140
|
+
return { status: "cancelled" };
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// Validate the key with the provider BEFORE persisting. Hard auth failures
|
|
144
|
+
// short-circuit here without writing anything; transient failures warn
|
|
145
|
+
// but proceed to persist so users don't get stuck on a provider outage.
|
|
146
|
+
ctx.ui.notify(`Verifying your ${descriptor.displayName} key...`, "info");
|
|
147
|
+
const validation = await validateCredential(providerId, trimmed);
|
|
148
|
+
|
|
149
|
+
if (validation.status === "invalid") {
|
|
150
|
+
const statusHint =
|
|
151
|
+
validation.httpStatus !== undefined ? ` (HTTP ${validation.httpStatus})` : "";
|
|
152
|
+
const messageHint = validation.message ? ` — ${validation.message}` : "";
|
|
153
|
+
ctx.ui.notify(
|
|
154
|
+
`${descriptor.displayName} rejected the key${statusHint}${messageHint}. ` +
|
|
155
|
+
`Your existing configuration was not changed. Re-run /connect ${
|
|
156
|
+
descriptor.aliases[0] ?? descriptor.id
|
|
157
|
+
} to try a different key.`,
|
|
158
|
+
"error",
|
|
159
|
+
);
|
|
160
|
+
return {
|
|
161
|
+
status: "invalid_key",
|
|
162
|
+
httpStatus: validation.httpStatus,
|
|
163
|
+
message: validation.message,
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
if (validation.status === "transient") {
|
|
168
|
+
ctx.ui.notify(
|
|
169
|
+
`Couldn't reach ${descriptor.displayName} to verify the key (${validation.reason}). ` +
|
|
170
|
+
`Saving it anyway — the next request will surface any issue.`,
|
|
171
|
+
"warning",
|
|
172
|
+
);
|
|
173
|
+
// Fall through to persist.
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
persistProviderCredential(providerId, trimmed);
|
|
177
|
+
|
|
178
|
+
ctx.ui.notify(
|
|
179
|
+
`${descriptor.displayName} connected. Your key has been saved.`,
|
|
180
|
+
"info",
|
|
181
|
+
);
|
|
182
|
+
|
|
183
|
+
return { status: "connected" };
|
|
184
|
+
}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
// Pure decision function for handling `[OPENCANDLE_CREDENTIAL_REQUIRED ...]`
|
|
2
|
+
// tool results. This module has NO Pi dependencies so it can be tested in
|
|
3
|
+
// isolation — the Pi extension `tool_result` handler is a thin wrapper that
|
|
4
|
+
// calls this function, inspects the returned `InterceptAction`, and drives
|
|
5
|
+
// the appropriate side effects (promptUser, state mutation, tool rerun).
|
|
6
|
+
//
|
|
7
|
+
// Decision table — see design.md Decision 3 for the canonical version.
|
|
8
|
+
|
|
9
|
+
import type { ProviderId } from "./providers.js";
|
|
10
|
+
import { getProvider } from "./providers.js";
|
|
11
|
+
import type { OnboardingState } from "./state.js";
|
|
12
|
+
import type { CredentialRequiredReason } from "./tool-tags.js";
|
|
13
|
+
|
|
14
|
+
export interface InterceptInput {
|
|
15
|
+
/** The provider id parsed from the tagged tool-result content. */
|
|
16
|
+
provider: ProviderId;
|
|
17
|
+
/** Why the credential was flagged as required. */
|
|
18
|
+
reason: CredentialRequiredReason;
|
|
19
|
+
/** Current persistent onboarding state (providers map, welcome flag). */
|
|
20
|
+
state: OnboardingState;
|
|
21
|
+
/** Providers already prompted at least once in this session (across workflows). */
|
|
22
|
+
sessionPromptedSet: ReadonlySet<ProviderId>;
|
|
23
|
+
/**
|
|
24
|
+
* Whether a hard-tier provider has already fired a prompt earlier in the
|
|
25
|
+
* current workflow invocation. When true, subsequent hard-provider prompts
|
|
26
|
+
* in the same workflow SHALL be silenced per the "at most one prompt per
|
|
27
|
+
* workflow" requirement in `conversational-provider-setup.spec.md`.
|
|
28
|
+
*/
|
|
29
|
+
hardPromptFiredInWorkflow: boolean;
|
|
30
|
+
/** Injected clock for deterministic snooze-expiry tests. */
|
|
31
|
+
now: Date;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Outcome of the interception decision. */
|
|
35
|
+
export type InterceptAction =
|
|
36
|
+
| {
|
|
37
|
+
action: "prompt";
|
|
38
|
+
provider: ProviderId;
|
|
39
|
+
reason: CredentialRequiredReason;
|
|
40
|
+
}
|
|
41
|
+
| {
|
|
42
|
+
action: "skip";
|
|
43
|
+
provider: ProviderId;
|
|
44
|
+
remediation: string;
|
|
45
|
+
/** True when the skip is due to explicit never_ask — downstream gap-note
|
|
46
|
+
* synthesis should suppress the `/connect` remediation in that case. */
|
|
47
|
+
silenced: boolean;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
function buildRemediation(providerId: ProviderId): string {
|
|
51
|
+
const descriptor = getProvider(providerId);
|
|
52
|
+
// Prefer the first alias as the friendly target; fall back to the id.
|
|
53
|
+
const alias = descriptor.aliases[0] ?? providerId;
|
|
54
|
+
return `run /connect ${alias} to unlock`;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function skip(
|
|
58
|
+
providerId: ProviderId,
|
|
59
|
+
silenced: boolean,
|
|
60
|
+
): InterceptAction {
|
|
61
|
+
return {
|
|
62
|
+
action: "skip",
|
|
63
|
+
provider: providerId,
|
|
64
|
+
remediation: buildRemediation(providerId),
|
|
65
|
+
silenced,
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Pure decision function — given an `InterceptInput`, return either a
|
|
71
|
+
* `prompt` action (the extension handler should pause and call `promptUser`)
|
|
72
|
+
* or a `skip` action (the handler should replace the tool result with a
|
|
73
|
+
* `[OPENCANDLE_SKIPPED ...]` placeholder).
|
|
74
|
+
*
|
|
75
|
+
* No side effects. No Pi imports. Trivially unit-testable.
|
|
76
|
+
*/
|
|
77
|
+
export function resolveCredentialRequired(
|
|
78
|
+
input: InterceptInput,
|
|
79
|
+
): InterceptAction {
|
|
80
|
+
const {
|
|
81
|
+
provider,
|
|
82
|
+
reason,
|
|
83
|
+
state,
|
|
84
|
+
sessionPromptedSet,
|
|
85
|
+
hardPromptFiredInWorkflow,
|
|
86
|
+
now,
|
|
87
|
+
} = input;
|
|
88
|
+
|
|
89
|
+
const descriptor = getProvider(provider);
|
|
90
|
+
const entry = state.providers[provider];
|
|
91
|
+
|
|
92
|
+
// 1. Never-ask: always skip, marked silenced so the gap-note copy drops the
|
|
93
|
+
// `/connect` remediation.
|
|
94
|
+
if (entry?.status === "never_ask") {
|
|
95
|
+
return skip(provider, true);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// 2. Completed + missing (defensive — shouldn't normally happen because the
|
|
99
|
+
// tool layer only emits `missing` when `hasCredential` returns false, and
|
|
100
|
+
// a completed state implies the credential was persisted).
|
|
101
|
+
if (entry?.status === "completed" && reason === "missing") {
|
|
102
|
+
return skip(provider, false);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// 3. Session-level dedup: a previous prompt this session already settled
|
|
106
|
+
// the user's answer for this provider. Don't re-ask until next session.
|
|
107
|
+
if (sessionPromptedSet.has(provider)) {
|
|
108
|
+
return skip(provider, false);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// 4. Per-workflow cap: at most one hard-provider prompt per workflow run.
|
|
112
|
+
if (hardPromptFiredInWorkflow && descriptor.tier === "hard") {
|
|
113
|
+
return skip(provider, false);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// 5. Active snooze: skip without decrementing the session set (the user
|
|
117
|
+
// will see the prompt again if they try a workflow after snoozeUntil).
|
|
118
|
+
if (entry?.status === "snoozed") {
|
|
119
|
+
const until = Date.parse(entry.snoozeUntil);
|
|
120
|
+
if (!Number.isNaN(until) && now.getTime() < until) {
|
|
121
|
+
return skip(provider, false);
|
|
122
|
+
}
|
|
123
|
+
// snoozed but expired → fall through to prompt.
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// 6. Completed + stale: re-prompt. This is the ONE stale-credential path
|
|
127
|
+
// this change ships — the full failure-as-invitation story is deferred.
|
|
128
|
+
if (entry?.status === "completed" && reason === "stale") {
|
|
129
|
+
return { action: "prompt", provider, reason };
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// 7. Default: prompt.
|
|
133
|
+
return { action: "prompt", provider, reason };
|
|
134
|
+
}
|