import * as fs from 'fs'; import * as path from 'path'; import { AgentCliConfig } from './config'; const EXTENSION_FILE = 'constructive-db-tools.js'; /** * Environment variables that override the stored session at tool-call time. * Without env vars or a stored session the tools load but report "not signed in". */ export const HOST_ENV_VARS = [ 'CONSTRUCTIVE_USER_ID', 'CONSTRUCTIVE_ACCESS_TOKEN', 'CONSTRUCTIVE_API_KEY', 'CONSTRUCTIVE_API_ENDPOINT', 'CONSTRUCTIVE_MODULES_ENDPOINT' ] as const; // pi's TUI loads extensions from files under /extensions (via jiti), // not from factories, so the db tools are wired by materializing a small // generated entry there. Both entry paths are resolved from THIS package at // assembly time and baked in, because node resolution from the appstash agent // dir would not find them. function extensionSource(pkgEntry: string, credentialsEntry: string): string { return `// Generated by @agentic-kit/cli — do not edit (rewritten on every \`agent\` run). // Registers the Constructive typed db tools + confirm gate (@agentic-kit/pi) // with credentials resolved on each call: CONSTRUCTIVE_* env vars first, then // the \`agent login\` session store. const { createDbTools } = require(${JSON.stringify(pkgEntry)}); const { resolveAccount, resolveBackendConfig } = require(${JSON.stringify(credentialsEntry)}); module.exports = createDbTools({ account: resolveAccount, backendConfig: resolveBackendConfig, signInHint: 'Run \`agent login\` to sign in.' }); `; } /** * Write the db-tools extension entry into pi's agent dir. Returns the file * path, or null when @agentic-kit/pi is not installed alongside the CLI. */ export function materializeDbTools( config: AgentCliConfig, log: (msg: string) => void ): string | null { let pkgEntry: string; try { pkgEntry = require.resolve('@agentic-kit/pi'); } catch { log('db tools unavailable (@agentic-kit/pi not installed) — continuing without them'); return null; } const credentialsEntry = require.resolve('./credentials'); const dir = path.join(config.agentDir, 'extensions'); fs.mkdirSync(dir, { recursive: true }); const file = path.join(dir, EXTENSION_FILE); fs.writeFileSync(file, extensionSource(pkgEntry, credentialsEntry)); return file; }