stock-bot/libs/data/duckdb/src/research-copilot-store.ts

49 lines
1.9 KiB
TypeScript

/**
* Real DuckDB `MarketDataStore` factory for the MCP research copilot stdio host
* (`runResearchCopilotStdioHost` in `@stock-bot/research/mcp`).
*
* - Uses `config.vaultCutoffTs` as the single authoritative Vault cutoff shared
* between the host and the underlying DuckDB store, and hardcodes
* `vaultOperatorAuthorized: false` so every query remains strictly
* `'vault_fenced'`.
* - Returns a {@link ManagedMarketDataStore} (`{ store, close }`) whose
* `close()` releases the underlying DuckDB connection and instance.
* - Any failure during open (native connection creation or store validation)
* releases already-acquired DuckDB resources before rejecting.
*/
import type {
ManagedMarketDataStore,
ResearchCopilotHostConfig,
ResearchCopilotStdioHostDeps,
} from '@stock-bot/research/mcp';
import { openDuckDbMarketDataStore } from './index';
export interface OpenResearchCopilotStoreOptions {
/** Optional DuckDB database path; defaults to `':memory:'`. */
readonly databasePath?: string;
/** Optional environment overrides for DuckDB connection budget (`STOCK_BOT_DUCKDB_*`). */
readonly env?: Readonly<Record<string, string | undefined>>;
}
/**
* Opens a Vault-fenced DuckDB-backed {@link ManagedMarketDataStore} from a
* validated {@link ResearchCopilotHostConfig} for `ResearchCopilotStdioHostDeps.openStore`.
*/
export const openResearchCopilotStore = (async (
config: ResearchCopilotHostConfig,
options?: OpenResearchCopilotStoreOptions
): Promise<ManagedMarketDataStore> => {
const opened = await openDuckDbMarketDataStore({
curatedRoot: config.curatedRoot,
vaultCutoffTs: config.vaultCutoffTs,
vaultOperatorAuthorized: false,
...(options?.databasePath !== undefined ? { databasePath: options.databasePath } : {}),
...(options?.env !== undefined ? { env: options.env } : {}),
});
return {
store: opened.store,
close: opened.close,
};
}) satisfies ResearchCopilotStdioHostDeps['openStore'];