ktx/packages/cli/src/setup.ts

751 lines
28 KiB
TypeScript
Raw Permalink Normal View History

2026-05-10 23:12:26 +02:00
import { existsSync } from 'node:fs';
import { join, resolve } from 'node:path';
import { cancel, isCancel, select } from '@clack/prompts';
import { getLatestLocalIngestStatus, savedMemoryCountsForReport } from '@ktx/context/ingest';
import {
ktxLocalStateDbPath,
ktxSetupCompletedSteps,
loadKtxProject,
readKtxSetupState,
type KtxLocalProject,
} from '@ktx/context/project';
2026-05-10 23:51:24 +02:00
import type { KtxCliIo } from './cli-runtime.js';
2026-05-10 23:12:26 +02:00
import { formatSetupNextStepLines } from './next-steps.js';
2026-05-10 23:51:24 +02:00
import { isKtxSetupExitError, withSetupInterruptConfirmation } from './setup-interrupt.js';
2026-05-10 23:12:26 +02:00
import {
2026-05-10 23:51:24 +02:00
type KtxAgentScope,
type KtxAgentTarget,
type KtxSetupAgentsDeps,
readKtxAgentInstallManifest,
runKtxSetupAgentsStep,
2026-05-10 23:12:26 +02:00
} from './setup-agents.js';
import {
2026-05-10 23:51:24 +02:00
type KtxSetupDatabaseDriver,
type KtxSetupDatabasesDeps,
runKtxSetupDatabasesStep,
2026-05-10 23:12:26 +02:00
} from './setup-databases.js';
2026-05-10 23:51:24 +02:00
import { type KtxSetupEmbeddingsDeps, runKtxSetupEmbeddingsStep } from './setup-embeddings.js';
import { type KtxSetupModelDeps, isKtxSetupLlmConfigReady, runKtxSetupAnthropicModelStep } from './setup-models.js';
2026-05-10 23:51:24 +02:00
import { type KtxSetupProjectDeps, runKtxSetupProjectStep } from './setup-project.js';
2026-05-10 23:13:17 -07:00
import {
isKtxPreAgentSetupReady,
isKtxSetupReady,
type KtxSetupReadyMenuDeps,
runKtxSetupReadyChangeMenu,
} from './setup-ready-menu.js';
2026-05-10 23:51:24 +02:00
import { type KtxSetupSourcesDeps, type KtxSetupSourceType, runKtxSetupSourcesStep } from './setup-sources.js';
2026-05-10 23:12:26 +02:00
import { withMenuOptionsSpacing } from './prompt-navigation.js';
import {
2026-05-10 23:51:24 +02:00
readKtxSetupContextState,
type KtxSetupContextDeps,
type KtxSetupContextResult,
runKtxSetupContextStep,
2026-05-10 23:12:26 +02:00
setupContextStatusFromState,
2026-05-10 23:51:24 +02:00
type KtxSetupContextStatusSummary,
2026-05-10 23:12:26 +02:00
} from './setup-context.js';
2026-05-10 23:51:24 +02:00
export interface KtxSetupStatus {
2026-05-10 23:12:26 +02:00
project: { path: string; ready: boolean; name?: string };
llm: { backend?: string; ready: boolean; model?: string };
embeddings: { backend?: string; ready: boolean; model?: string; dimensions?: number };
databases: Array<{ connectionId: string; ready: boolean }>;
sources: Array<{ connectionId: string; type: string; ready: boolean }>;
2026-05-10 23:51:24 +02:00
context: KtxSetupContextStatusSummary;
2026-05-10 23:12:26 +02:00
agents: Array<{ target: string; scope: string; ready: boolean }>;
}
2026-05-10 23:51:24 +02:00
export type KtxSetupArgs =
2026-05-10 23:12:26 +02:00
| {
command: 'run';
projectDir: string;
mode: 'auto' | 'new' | 'existing';
agents: boolean;
2026-05-10 23:51:24 +02:00
target?: KtxAgentTarget;
agentScope?: KtxAgentScope;
2026-05-10 23:12:26 +02:00
skipAgents?: boolean;
inputMode: 'auto' | 'disabled';
yes: boolean;
feat: npm-managed Python runtime for @kaelio/ktx (#7) * docs: add npm managed python runtime design * build: add bundled python runtime wheel builder * build: make local embedding dependencies optional * build: bundle python runtime wheel in cli artifacts * build: track bundled python runtime release artifact * test: verify bundled python runtime wheel * docs: add plan for bundled python runtime wheel * test: cover managed python runtime lifecycle * feat: add managed python runtime installer * feat: add runtime command runner * feat: expose runtime management commands * test: verify managed python runtime commands * docs: add plan for managed python runtime installer * feat: add managed python command helper * feat: use managed runtime for sl query compute * feat: route sl query managed runtime policy * docs: add plan for managed runtime sl query integration * feat: add managed runtime daemon metadata * feat: manage python daemon lifecycle * feat: add runtime daemon start stop commands * fix: verify managed runtime daemon lifecycle * docs: add plan for managed runtime daemon lifecycle * feat: add managed local embeddings config marker * feat: add managed local embeddings daemon helper * feat: use managed runtime for local embedding setup * feat: pass managed runtime policy through setup * docs: add plan for managed local embeddings runtime * feat: read CLI package metadata dynamically * feat: assemble public kaelio ktx npm package * feat: release one public kaelio ktx npm artifact * test: cover public kaelio ktx package invocations * chore: verify public kaelio ktx package artifacts * docs: add plan for public kaelio ktx npm package * test: verify managed runtime in public package smoke * test: finalize managed runtime release smoke * docs: add plan for managed runtime release smoke * test: specify local embeddings release smoke * feat: add local embeddings runtime smoke * chore: register local embeddings smoke * fix: verify local embeddings smoke * fix: restore artifact smoke python env helper * docs: add plan for managed local embeddings release smoke * refactor: share managed runtime install policy parsing * feat: use managed runtime for agent semantic queries * feat: use managed runtime for MCP semantic compute * docs: add plan for managed agent and MCP semantic runtime * feat(cli): add managed daemon HTTP helpers * feat(cli): route local adapters through managed daemon * feat(cli): use managed daemon for ingest helpers * feat(cli): pass managed daemon options to scan * feat(context): pass MCP ingest pull config options * feat(cli): pass managed daemon options to serve ingest * test: verify managed local ingest daemon runtime * docs: add plan for managed local ingest daemon runtime * docs: align managed runtime examples * docs: add plan for managed runtime docs cleanup * test: cover published package runtime smoke commands * test: validate published package smoke outputs * docs: add plan for published package runtime smoke * build: stamp public npm package version * release: add npm public release policy * release: add guarded npm publish script * release: document public npm release handoff * docs: add plan for public npm release handoff * test: cover managed runtime prune in package smoke * docs: document managed runtime prune * docs: add plan for managed runtime prune smoke and docs * chore: encode uv runtime prerequisite policy * fix: clarify missing uv runtime error * docs: document uv runtime prerequisite * docs: add plan for uv runtime prerequisite contract * refactor: limit release artifacts to public package runtime * chore: align release policy with bundled runtime wheel * docs: describe single public runtime artifact surface * test: verify single public runtime artifact contract * docs: add plan for single public runtime artifact cleanup * fix: align local embeddings smoke with public version * docs: add plan for local embeddings smoke public version * release: soft-launch as @kaelio/ktx@0.1.0-rc.0 on next tag Publish target moves to the pre-release version 0.1.0-rc.0 under the next dist-tag so npm install @kaelio/ktx (which resolves to latest) does not pick up the soft-launch build. Users opt in via @kaelio/ktx@next. * Fix release script boundary checks * Remove PostHog from public package bundle
2026-05-11 15:50:34 +02:00
cliVersion: string;
2026-05-10 23:12:26 +02:00
anthropicApiKeyEnv?: string;
anthropicApiKeyFile?: string;
anthropicModel?: string;
skipLlm: boolean;
embeddingBackend?: 'openai' | 'sentence-transformers';
embeddingApiKeyEnv?: string;
embeddingApiKeyFile?: string;
skipEmbeddings: boolean;
2026-05-10 23:51:24 +02:00
databaseDrivers?: KtxSetupDatabaseDriver[];
2026-05-10 23:12:26 +02:00
databaseConnectionIds?: string[];
databaseConnectionId?: string;
databaseUrl?: string;
databaseSchemas: string[];
enableHistoricSql?: boolean;
disableHistoricSql?: boolean;
historicSqlWindowDays?: number;
historicSqlMinExecutions?: number;
2026-05-10 23:12:26 +02:00
historicSqlMinCalls?: number;
historicSqlServiceAccountPatterns?: string[];
historicSqlRedactionPatterns?: string[];
skipDatabases: boolean;
2026-05-10 23:51:24 +02:00
source?: KtxSetupSourceType;
2026-05-10 23:12:26 +02:00
sourceConnectionId?: string;
sourcePath?: string;
sourceGitUrl?: string;
sourceBranch?: string;
sourceSubpath?: string;
sourceAuthTokenRef?: string;
sourceUrl?: string;
sourceApiKeyRef?: string;
sourceClientId?: string;
sourceClientSecretRef?: string;
sourceWarehouseConnectionId?: string;
sourceProjectName?: string;
sourceProfilesPath?: string;
sourceTarget?: string;
metabaseDatabaseId?: number;
notionCrawlMode?: 'all_accessible' | 'selected_roots';
notionRootPageIds?: string[];
runInitialSourceIngest?: boolean;
skipSources?: boolean;
showEntryMenu?: boolean;
};
2026-05-10 23:12:26 +02:00
2026-05-10 23:51:24 +02:00
export interface KtxSetupDeps {
project?: KtxSetupProjectDeps;
2026-05-10 23:12:26 +02:00
model?: (
2026-05-10 23:51:24 +02:00
args: Parameters<typeof runKtxSetupAnthropicModelStep>[0],
io: KtxCliIo,
) => Promise<Awaited<ReturnType<typeof runKtxSetupAnthropicModelStep>>>;
modelDeps?: KtxSetupModelDeps;
2026-05-10 23:12:26 +02:00
embeddings?: (
2026-05-10 23:51:24 +02:00
args: Parameters<typeof runKtxSetupEmbeddingsStep>[0],
io: KtxCliIo,
) => Promise<Awaited<ReturnType<typeof runKtxSetupEmbeddingsStep>>>;
embeddingsDeps?: KtxSetupEmbeddingsDeps;
2026-05-10 23:12:26 +02:00
databases?: (
2026-05-10 23:51:24 +02:00
args: Parameters<typeof runKtxSetupDatabasesStep>[0],
io: KtxCliIo,
) => Promise<Awaited<ReturnType<typeof runKtxSetupDatabasesStep>>>;
databasesDeps?: KtxSetupDatabasesDeps;
2026-05-10 23:12:26 +02:00
sources?: (
2026-05-10 23:51:24 +02:00
args: Parameters<typeof runKtxSetupSourcesStep>[0],
io: KtxCliIo,
) => Promise<Awaited<ReturnType<typeof runKtxSetupSourcesStep>>>;
sourcesDeps?: KtxSetupSourcesDeps;
2026-05-10 23:12:26 +02:00
agents?: (
2026-05-10 23:51:24 +02:00
args: Parameters<typeof runKtxSetupAgentsStep>[0],
io: KtxCliIo,
) => Promise<Awaited<ReturnType<typeof runKtxSetupAgentsStep>>>;
agentsDeps?: KtxSetupAgentsDeps;
context?: (args: Parameters<typeof runKtxSetupContextStep>[0], io: KtxCliIo) => Promise<KtxSetupContextResult>;
contextDeps?: KtxSetupContextDeps;
readyMenuDeps?: KtxSetupReadyMenuDeps;
entryMenuDeps?: KtxSetupEntryMenuDeps;
2026-05-10 23:12:26 +02:00
}
const SOURCE_DRIVERS = new Set(['dbt', 'metricflow', 'metabase', 'looker', 'lookml', 'notion']);
2026-05-10 23:51:24 +02:00
type KtxSetupEntryAction = 'setup' | 'new-project' | 'agents' | 'status' | 'demo' | 'exit';
type KtxSetupFlowStep = 'models' | 'embeddings' | 'databases' | 'sources' | 'context' | 'agents';
type KtxSetupFlowStatus =
2026-05-10 23:12:26 +02:00
| 'ready'
| 'skipped'
| 'back'
| 'missing-input'
| 'failed'
| 'detached'
| 'paused'
| 'interrupted';
2026-05-10 23:51:24 +02:00
export interface KtxSetupEntryMenuPromptAdapter {
2026-05-10 23:12:26 +02:00
select(options: { message: string; options: Array<{ value: string; label: string }> }): Promise<string>;
cancel(message: string): void;
}
2026-05-10 23:51:24 +02:00
export interface KtxSetupEntryMenuDeps {
prompts?: KtxSetupEntryMenuPromptAdapter;
2026-05-10 23:12:26 +02:00
}
2026-05-10 23:51:24 +02:00
function createEntryMenuPromptAdapter(): KtxSetupEntryMenuPromptAdapter {
2026-05-10 23:12:26 +02:00
return {
async select(options) {
const value = await withSetupInterruptConfirmation(() => select(withMenuOptionsSpacing(options)));
if (isCancel(value)) {
return 'exit';
}
return String(value);
},
cancel(message) {
cancel(message);
},
};
}
2026-05-10 23:51:24 +02:00
async function runKtxSetupEntryMenu(
status: KtxSetupStatus,
deps: KtxSetupEntryMenuDeps = {},
): Promise<{ action: KtxSetupEntryAction }> {
2026-05-10 23:12:26 +02:00
const prompts = deps.prompts ?? createEntryMenuPromptAdapter();
const options = status.project.ready
? [
{ value: 'setup', label: 'Resume or change an existing setup' },
2026-05-10 23:51:24 +02:00
{ value: 'new-project', label: 'Create a new KTX project' },
{ value: 'agents', label: 'Connect a coding agent to KTX' },
2026-05-10 23:12:26 +02:00
{ value: 'status', label: 'Check setup status' },
{ value: 'demo', label: 'Explore a pre-built KTX project' },
2026-05-10 23:12:26 +02:00
{ value: 'exit', label: 'Exit' },
]
: [
2026-05-10 23:51:24 +02:00
{ value: 'setup', label: 'Set up KTX for my data' },
2026-05-10 23:12:26 +02:00
{ value: 'status', label: 'Check setup status' },
{ value: 'demo', label: 'Explore a pre-built KTX project' },
2026-05-10 23:12:26 +02:00
{ value: 'exit', label: 'Exit' },
];
const action = (await prompts.select({
message: 'What do you want to do?',
options,
2026-05-10 23:51:24 +02:00
})) as KtxSetupEntryAction;
2026-05-10 23:12:26 +02:00
return { action };
}
2026-05-10 23:51:24 +02:00
async function runKtxSetupDemoFromEntryMenu(
args: Extract<KtxSetupArgs, { command: 'run' }>,
io: KtxCliIo,
deps: KtxSetupDeps,
2026-05-10 23:12:26 +02:00
): Promise<number> {
const { runDemoTour } = await import('./setup-demo-tour.js');
return await runDemoTour(
{ inputMode: args.inputMode },
2026-05-10 23:12:26 +02:00
io,
{ agents: deps.agents },
);
2026-05-10 23:12:26 +02:00
}
2026-05-10 23:51:24 +02:00
function embeddingsReady(status: KtxSetupStatus['embeddings']): boolean {
2026-05-10 23:12:26 +02:00
return (
status.backend !== undefined &&
status.backend !== 'none' &&
status.backend !== 'deterministic' &&
typeof status.model === 'string' &&
status.model.length > 0 &&
typeof status.dimensions === 'number' &&
status.dimensions > 0
);
}
2026-05-10 23:51:24 +02:00
function sourceConnections(config: Awaited<ReturnType<typeof loadKtxProject>>['config']) {
2026-05-10 23:12:26 +02:00
return Object.entries(config.connections)
.filter(([, connection]) => SOURCE_DRIVERS.has(String(connection.driver ?? '').toLowerCase()))
.map(([connectionId, connection]) => ({
connectionId,
type: String(connection.driver).toLowerCase(),
}))
.sort((left, right) => left.connectionId.localeCompare(right.connectionId));
}
type LocalIngestStatusReport = NonNullable<Awaited<ReturnType<typeof getLatestLocalIngestStatus>>>;
function reportHasSavedContext(report: LocalIngestStatusReport): boolean {
if (report.body.failedWorkUnits.length > 0) {
return false;
}
const counts = savedMemoryCountsForReport(report);
return counts.wikiCount > 0 || counts.slCount > 0;
}
async function readIngestContextStatus(project: KtxLocalProject): Promise<KtxSetupContextStatusSummary | null> {
if (!existsSync(ktxLocalStateDbPath(project))) {
return null;
}
const report = await getLatestLocalIngestStatus(project);
if (!report || !reportHasSavedContext(report)) {
return null;
}
return {
ready: true,
status: 'completed',
runId: report.runId,
};
}
2026-05-10 23:51:24 +02:00
export async function readKtxSetupStatus(projectDir: string): Promise<KtxSetupStatus> {
2026-05-10 23:12:26 +02:00
const resolvedProjectDir = resolve(projectDir);
2026-05-10 23:51:24 +02:00
if (!existsSync(join(resolvedProjectDir, 'ktx.yaml'))) {
2026-05-10 23:12:26 +02:00
return {
project: { path: resolvedProjectDir, ready: false },
llm: { ready: false },
embeddings: { ready: false },
databases: [],
sources: [],
2026-05-10 23:51:24 +02:00
context: setupContextStatusFromState(await readKtxSetupContextState(resolvedProjectDir)),
2026-05-10 23:12:26 +02:00
agents: [],
};
}
2026-05-10 23:51:24 +02:00
const project = await loadKtxProject({ projectDir: resolvedProjectDir });
2026-05-10 23:12:26 +02:00
const llm = {
backend: project.config.llm.provider.backend,
ready: isKtxSetupLlmConfigReady(project.config.llm),
2026-05-10 23:12:26 +02:00
model: project.config.llm.models.default,
};
const embeddings = {
backend: project.config.ingest.embeddings.backend,
ready: false,
model: project.config.ingest.embeddings.model,
dimensions: project.config.ingest.embeddings.dimensions,
};
embeddings.ready = embeddingsReady(embeddings);
const completedSteps = ktxSetupCompletedSteps(project.config, await readKtxSetupState(resolvedProjectDir));
2026-05-10 23:51:24 +02:00
const contextState = await readKtxSetupContextState(resolvedProjectDir);
const setupContextStatus = setupContextStatusFromState(contextState, {
completedStep: completedSteps.includes('context'),
});
const ingestContextStatus = setupContextStatus.ready ? null : await readIngestContextStatus(project);
2026-05-10 23:12:26 +02:00
const databaseIds = project.config.setup?.database_connection_ids ?? Object.keys(project.config.connections);
const databasesComplete = completedSteps.includes('databases');
2026-05-10 23:51:24 +02:00
const manifest = await readKtxAgentInstallManifest(resolvedProjectDir);
2026-05-10 23:12:26 +02:00
const agents =
manifest?.installs.map((install) => ({
target: install.target,
scope: install.scope,
ready: true,
})) ?? [];
return {
project: { path: resolvedProjectDir, ready: true, name: project.config.project },
llm,
embeddings,
databases: databaseIds.map((connectionId) => ({
connectionId,
ready: databasesComplete && Object.hasOwn(project.config.connections, connectionId),
})),
sources: sourceConnections(project.config).map((source) => ({
...source,
ready: completedSteps.includes('sources'),
})),
context: ingestContextStatus ?? setupContextStatus,
2026-05-10 23:12:26 +02:00
agents,
};
}
function formatReady(value: boolean): 'yes' | 'no' {
return value ? 'yes' : 'no';
}
function formatConnectionList(ids: string[]): string {
return ids.length > 0 ? `yes (${ids.join(', ')})` : 'no';
}
2026-05-10 23:51:24 +02:00
function formatContextBuilt(status: KtxSetupContextStatusSummary): string {
2026-05-10 23:12:26 +02:00
if (status.ready) {
return 'yes';
}
if (status.status === 'not_started') {
return 'no';
}
const runSuffix = status.runId ? ` (${status.runId})` : '';
return `${status.status.replaceAll('_', ' ')}${runSuffix}`;
}
2026-05-10 23:51:24 +02:00
export function formatKtxSetupStatus(status: KtxSetupStatus): string {
2026-05-10 23:12:26 +02:00
if (!status.project.ready) {
return [
2026-05-10 23:51:24 +02:00
`No KTX project found at ${status.project.path}.`,
2026-05-10 23:12:26 +02:00
'',
'Check another project: ktx --project-dir <folder> status',
'Or from that folder: ktx status',
2026-05-10 23:51:24 +02:00
'Create a new KTX project here: ktx setup',
2026-05-10 23:12:26 +02:00
'',
].join('\n');
}
const lines = [
2026-05-10 23:51:24 +02:00
`KTX project: ${status.project.path}`,
2026-05-10 23:12:26 +02:00
`Project ready: ${formatReady(status.project.ready)}`,
`LLM ready: ${formatReady(status.llm.ready)}${status.llm.model ? ` (${status.llm.model})` : ''}`,
`Embeddings ready: ${formatReady(status.embeddings.ready)}${
status.embeddings.model ? ` (${status.embeddings.model})` : ''
}`,
`Primary sources configured: ${formatConnectionList(status.databases.map((database) => database.connectionId))}`,
`Context sources configured: ${formatConnectionList(status.sources.map((source) => source.connectionId))}`,
2026-05-10 23:51:24 +02:00
`KTX context built: ${formatContextBuilt(status.context)}`,
2026-05-10 23:12:26 +02:00
`Agent integration ready: ${formatReady(status.agents.some((agent) => agent.ready))}${
status.agents.length > 0 ? ` (${status.agents.map((agent) => `${agent.target}:${agent.scope}`).join(', ')})` : ''
}`,
];
if (!status.context.ready && status.context.watchCommand && status.context.status === 'running') {
lines.push(`Resume: ${status.context.watchCommand}`);
}
if (!status.context.ready && status.context.status === 'failed' && status.context.detail) {
lines.push(`Retry: ${status.context.retryCommand ?? `ktx setup --project-dir ${status.project.path}`}`);
2026-05-10 23:12:26 +02:00
}
return `${lines.join('\n')}\n`;
}
2026-05-10 23:51:24 +02:00
function setupStatusReady(status: KtxSetupStatus): boolean {
2026-05-10 23:12:26 +02:00
if (!status.project.ready) {
return false;
}
if (!setupHasContextTargets(status)) {
return true;
}
return (
status.llm.ready &&
2026-05-10 23:12:26 +02:00
embeddingsReady(status.embeddings) &&
status.databases.every((database) => database.ready) &&
status.sources.every((source) => source.ready)
);
}
2026-05-10 23:51:24 +02:00
function setupHasContextTargets(status: KtxSetupStatus): boolean {
2026-05-10 23:12:26 +02:00
return status.databases.length > 0 || status.sources.length > 0;
}
2026-05-10 23:51:24 +02:00
function setupContextReady(status: KtxSetupStatus): boolean {
2026-05-10 23:12:26 +02:00
return status.context.ready;
}
function setupContextActive(status: KtxSetupStatus): boolean {
return status.context.status === 'running' || status.context.status === 'detached';
}
2026-05-10 23:51:24 +02:00
function writeContextNotReadyForAgents(projectDir: string, io: KtxCliIo): void {
io.stderr.write('KTX context is not ready for agents.\n\n');
io.stderr.write(`Build context first:\n ktx setup --project-dir ${resolve(projectDir)}\n\n`);
2026-05-10 23:51:24 +02:00
io.stderr.write(`Then install agent integration:\n ktx setup --agents --project-dir ${resolve(projectDir)}\n`);
2026-05-10 23:12:26 +02:00
}
feat: npm-managed Python runtime for @kaelio/ktx (#7) * docs: add npm managed python runtime design * build: add bundled python runtime wheel builder * build: make local embedding dependencies optional * build: bundle python runtime wheel in cli artifacts * build: track bundled python runtime release artifact * test: verify bundled python runtime wheel * docs: add plan for bundled python runtime wheel * test: cover managed python runtime lifecycle * feat: add managed python runtime installer * feat: add runtime command runner * feat: expose runtime management commands * test: verify managed python runtime commands * docs: add plan for managed python runtime installer * feat: add managed python command helper * feat: use managed runtime for sl query compute * feat: route sl query managed runtime policy * docs: add plan for managed runtime sl query integration * feat: add managed runtime daemon metadata * feat: manage python daemon lifecycle * feat: add runtime daemon start stop commands * fix: verify managed runtime daemon lifecycle * docs: add plan for managed runtime daemon lifecycle * feat: add managed local embeddings config marker * feat: add managed local embeddings daemon helper * feat: use managed runtime for local embedding setup * feat: pass managed runtime policy through setup * docs: add plan for managed local embeddings runtime * feat: read CLI package metadata dynamically * feat: assemble public kaelio ktx npm package * feat: release one public kaelio ktx npm artifact * test: cover public kaelio ktx package invocations * chore: verify public kaelio ktx package artifacts * docs: add plan for public kaelio ktx npm package * test: verify managed runtime in public package smoke * test: finalize managed runtime release smoke * docs: add plan for managed runtime release smoke * test: specify local embeddings release smoke * feat: add local embeddings runtime smoke * chore: register local embeddings smoke * fix: verify local embeddings smoke * fix: restore artifact smoke python env helper * docs: add plan for managed local embeddings release smoke * refactor: share managed runtime install policy parsing * feat: use managed runtime for agent semantic queries * feat: use managed runtime for MCP semantic compute * docs: add plan for managed agent and MCP semantic runtime * feat(cli): add managed daemon HTTP helpers * feat(cli): route local adapters through managed daemon * feat(cli): use managed daemon for ingest helpers * feat(cli): pass managed daemon options to scan * feat(context): pass MCP ingest pull config options * feat(cli): pass managed daemon options to serve ingest * test: verify managed local ingest daemon runtime * docs: add plan for managed local ingest daemon runtime * docs: align managed runtime examples * docs: add plan for managed runtime docs cleanup * test: cover published package runtime smoke commands * test: validate published package smoke outputs * docs: add plan for published package runtime smoke * build: stamp public npm package version * release: add npm public release policy * release: add guarded npm publish script * release: document public npm release handoff * docs: add plan for public npm release handoff * test: cover managed runtime prune in package smoke * docs: document managed runtime prune * docs: add plan for managed runtime prune smoke and docs * chore: encode uv runtime prerequisite policy * fix: clarify missing uv runtime error * docs: document uv runtime prerequisite * docs: add plan for uv runtime prerequisite contract * refactor: limit release artifacts to public package runtime * chore: align release policy with bundled runtime wheel * docs: describe single public runtime artifact surface * test: verify single public runtime artifact contract * docs: add plan for single public runtime artifact cleanup * fix: align local embeddings smoke with public version * docs: add plan for local embeddings smoke public version * release: soft-launch as @kaelio/ktx@0.1.0-rc.0 on next tag Publish target moves to the pre-release version 0.1.0-rc.0 under the next dist-tag so npm install @kaelio/ktx (which resolves to latest) does not pick up the soft-launch build. Users opt in via @kaelio/ktx@next. * Fix release script boundary checks * Remove PostHog from public package bundle
2026-05-11 15:50:34 +02:00
function setupRuntimeInstallPolicy(args: Extract<KtxSetupArgs, { command: 'run' }>): 'prompt' | 'auto' | 'never' {
if (args.yes) {
return 'auto';
}
return args.inputMode === 'disabled' ? 'never' : 'prompt';
}
async function commitSetupConfigChanges(projectDir: string): Promise<void> {
const project = await loadKtxProject({ projectDir });
await project.git.commitFile('ktx.yaml', 'setup: update KTX project config', 'ktx setup', 'setup@ktx.local');
}
2026-05-10 23:51:24 +02:00
export async function runKtxSetup(args: KtxSetupArgs, io: KtxCliIo, deps: KtxSetupDeps = {}): Promise<number> {
2026-05-10 23:12:26 +02:00
try {
2026-05-10 23:51:24 +02:00
return await runKtxSetupInner(args, io, deps);
2026-05-10 23:12:26 +02:00
} catch (error) {
2026-05-10 23:51:24 +02:00
if (isKtxSetupExitError(error)) {
2026-05-10 23:12:26 +02:00
return 0;
}
throw error;
}
}
2026-05-10 23:51:24 +02:00
async function runKtxSetupInner(args: KtxSetupArgs, io: KtxCliIo, deps: KtxSetupDeps = {}): Promise<number> {
io.stdout.write('KTX setup\n');
let entryAction: KtxSetupEntryAction | undefined;
let projectResult: Awaited<ReturnType<typeof runKtxSetupProjectStep>>;
2026-05-10 23:12:26 +02:00
const canShowEntryMenu =
args.showEntryMenu === true &&
args.inputMode !== 'disabled' &&
!args.agents &&
(io.stdout.isTTY === true || deps.entryMenuDeps?.prompts !== undefined);
let autoWatchActiveBuild = false;
2026-05-10 23:12:26 +02:00
setupLoop: while (true) {
entryAction = undefined;
if (canShowEntryMenu) {
2026-05-10 23:51:24 +02:00
const status = await readKtxSetupStatus(args.projectDir);
if (setupContextActive(status)) {
autoWatchActiveBuild = true;
} else {
entryAction = (await runKtxSetupEntryMenu(status, deps.entryMenuDeps)).action;
if (entryAction === 'exit') {
(deps.entryMenuDeps?.prompts ?? createEntryMenuPromptAdapter()).cancel('Setup cancelled.');
return 0;
}
if (entryAction === 'status') {
io.stdout.write(formatKtxSetupStatus(status));
return 0;
}
if (entryAction === 'demo') {
return await runKtxSetupDemoFromEntryMenu(args, io, deps);
}
2026-05-10 23:12:26 +02:00
}
}
const projectMode = entryAction === 'new-project' ? 'prompt-new' : args.mode;
2026-05-10 23:51:24 +02:00
projectResult = await runKtxSetupProjectStep(
2026-05-10 23:12:26 +02:00
{
projectDir: args.projectDir,
mode: projectMode,
inputMode: args.inputMode,
yes: args.yes,
allowBack: canShowEntryMenu,
},
io,
deps.project,
);
if (projectResult.status === 'back') {
continue;
}
if (projectResult.status !== 'ready') {
return projectResult.status === 'cancelled' ? 0 : 1;
}
const agentsRequested = args.agents || entryAction === 'agents';
2026-05-10 23:51:24 +02:00
const currentStatus = await readKtxSetupStatus(projectResult.projectDir);
2026-05-10 23:12:26 +02:00
let readyAction: string | undefined;
if (args.inputMode !== 'disabled' && !agentsRequested && setupContextActive(currentStatus)) {
const contextRunner =
deps.context ?? ((contextArgs, contextIo) => runKtxSetupContextStep(contextArgs, contextIo, deps.contextDeps));
const contextResult = await contextRunner(
{
projectDir: projectResult.projectDir,
inputMode: args.inputMode,
allowEmpty: true,
...(autoWatchActiveBuild ? { autoWatch: true } : {}),
},
io,
);
autoWatchActiveBuild = false;
if (contextResult.status === 'back') {
continue;
}
if (contextResult.status === 'failed' || contextResult.status === 'missing-input') {
return 1;
}
if (contextResult.status !== 'ready') {
return 0;
}
}
2026-05-10 23:13:17 -07:00
if (args.inputMode !== 'disabled' && !agentsRequested) {
if (isKtxSetupReady(currentStatus)) {
readyAction = (await runKtxSetupReadyChangeMenu(currentStatus, deps.readyMenuDeps)).action;
if (readyAction === 'exit') return 0;
} else if (isKtxPreAgentSetupReady(currentStatus)) {
readyAction = 'agents';
}
2026-05-10 23:12:26 +02:00
}
const runOnly = readyAction;
const shouldRunModels = !runOnly || runOnly === 'models';
const shouldRunEmbeddings = !runOnly || runOnly === 'embeddings';
const shouldRunDatabases = !runOnly || runOnly === 'databases';
const shouldRunSources = !runOnly || runOnly === 'sources';
const shouldRunContext = agentsRequested || !runOnly || runOnly === 'context';
const shouldRunAgents = agentsRequested || !runOnly || runOnly === 'agents';
const showPromptInstructions = projectResult.confirmedCreation !== true;
2026-05-10 23:51:24 +02:00
const setupSteps: KtxSetupFlowStep[] = agentsRequested
2026-05-10 23:12:26 +02:00
? ['context']
: ['models', 'embeddings', 'databases', 'sources', 'context'];
if (shouldRunAgents && args.skipAgents !== true) {
setupSteps.push('agents');
}
2026-05-10 23:51:24 +02:00
const forcePromptSteps = new Set<KtxSetupFlowStep>();
const isNavigableSetupStep = (step: KtxSetupFlowStep): boolean => {
2026-05-10 23:12:26 +02:00
if (step === 'models') return !args.skipLlm && shouldRunModels;
if (step === 'embeddings') return !args.skipEmbeddings && shouldRunEmbeddings;
if (step === 'databases') return !args.skipDatabases && shouldRunDatabases;
if (step === 'sources') return args.skipSources !== true && shouldRunSources;
if (step === 'context') return shouldRunContext;
return shouldRunAgents && args.skipAgents !== true;
};
const previousNavigableStepIndex = (currentIndex: number): number => {
for (let index = currentIndex - 1; index >= 0; index -= 1) {
const previousStep = setupSteps[index];
if (previousStep && isNavigableSetupStep(previousStep)) {
return index;
}
}
return -1;
};
for (let stepIndex = 0; stepIndex < setupSteps.length; ) {
const step = setupSteps[stepIndex];
if (!step) break;
2026-05-10 23:51:24 +02:00
let stepResult: { status: KtxSetupFlowStatus };
2026-05-10 23:12:26 +02:00
if (step === 'models') {
const modelRunner =
2026-05-10 23:51:24 +02:00
deps.model ?? ((modelArgs, modelIo) => runKtxSetupAnthropicModelStep(modelArgs, modelIo, deps.modelDeps));
2026-05-10 23:12:26 +02:00
stepResult = await modelRunner(
{
projectDir: projectResult.projectDir,
inputMode: args.inputMode,
...(args.anthropicApiKeyEnv ? { anthropicApiKeyEnv: args.anthropicApiKeyEnv } : {}),
...(args.anthropicApiKeyFile ? { anthropicApiKeyFile: args.anthropicApiKeyFile } : {}),
...(args.anthropicModel ? { anthropicModel: args.anthropicModel } : {}),
forcePrompt: forcePromptSteps.has('models') || runOnly === 'models',
showPromptInstructions,
skipLlm: args.skipLlm || !shouldRunModels,
},
io,
);
} else if (step === 'embeddings') {
const embeddingsRunner =
deps.embeddings ??
2026-05-10 23:51:24 +02:00
((embeddingArgs, embeddingIo) => runKtxSetupEmbeddingsStep(embeddingArgs, embeddingIo, deps.embeddingsDeps));
2026-05-10 23:12:26 +02:00
stepResult = await embeddingsRunner(
{
projectDir: projectResult.projectDir,
inputMode: args.inputMode,
feat: npm-managed Python runtime for @kaelio/ktx (#7) * docs: add npm managed python runtime design * build: add bundled python runtime wheel builder * build: make local embedding dependencies optional * build: bundle python runtime wheel in cli artifacts * build: track bundled python runtime release artifact * test: verify bundled python runtime wheel * docs: add plan for bundled python runtime wheel * test: cover managed python runtime lifecycle * feat: add managed python runtime installer * feat: add runtime command runner * feat: expose runtime management commands * test: verify managed python runtime commands * docs: add plan for managed python runtime installer * feat: add managed python command helper * feat: use managed runtime for sl query compute * feat: route sl query managed runtime policy * docs: add plan for managed runtime sl query integration * feat: add managed runtime daemon metadata * feat: manage python daemon lifecycle * feat: add runtime daemon start stop commands * fix: verify managed runtime daemon lifecycle * docs: add plan for managed runtime daemon lifecycle * feat: add managed local embeddings config marker * feat: add managed local embeddings daemon helper * feat: use managed runtime for local embedding setup * feat: pass managed runtime policy through setup * docs: add plan for managed local embeddings runtime * feat: read CLI package metadata dynamically * feat: assemble public kaelio ktx npm package * feat: release one public kaelio ktx npm artifact * test: cover public kaelio ktx package invocations * chore: verify public kaelio ktx package artifacts * docs: add plan for public kaelio ktx npm package * test: verify managed runtime in public package smoke * test: finalize managed runtime release smoke * docs: add plan for managed runtime release smoke * test: specify local embeddings release smoke * feat: add local embeddings runtime smoke * chore: register local embeddings smoke * fix: verify local embeddings smoke * fix: restore artifact smoke python env helper * docs: add plan for managed local embeddings release smoke * refactor: share managed runtime install policy parsing * feat: use managed runtime for agent semantic queries * feat: use managed runtime for MCP semantic compute * docs: add plan for managed agent and MCP semantic runtime * feat(cli): add managed daemon HTTP helpers * feat(cli): route local adapters through managed daemon * feat(cli): use managed daemon for ingest helpers * feat(cli): pass managed daemon options to scan * feat(context): pass MCP ingest pull config options * feat(cli): pass managed daemon options to serve ingest * test: verify managed local ingest daemon runtime * docs: add plan for managed local ingest daemon runtime * docs: align managed runtime examples * docs: add plan for managed runtime docs cleanup * test: cover published package runtime smoke commands * test: validate published package smoke outputs * docs: add plan for published package runtime smoke * build: stamp public npm package version * release: add npm public release policy * release: add guarded npm publish script * release: document public npm release handoff * docs: add plan for public npm release handoff * test: cover managed runtime prune in package smoke * docs: document managed runtime prune * docs: add plan for managed runtime prune smoke and docs * chore: encode uv runtime prerequisite policy * fix: clarify missing uv runtime error * docs: document uv runtime prerequisite * docs: add plan for uv runtime prerequisite contract * refactor: limit release artifacts to public package runtime * chore: align release policy with bundled runtime wheel * docs: describe single public runtime artifact surface * test: verify single public runtime artifact contract * docs: add plan for single public runtime artifact cleanup * fix: align local embeddings smoke with public version * docs: add plan for local embeddings smoke public version * release: soft-launch as @kaelio/ktx@0.1.0-rc.0 on next tag Publish target moves to the pre-release version 0.1.0-rc.0 under the next dist-tag so npm install @kaelio/ktx (which resolves to latest) does not pick up the soft-launch build. Users opt in via @kaelio/ktx@next. * Fix release script boundary checks * Remove PostHog from public package bundle
2026-05-11 15:50:34 +02:00
cliVersion: args.cliVersion,
runtimeInstallPolicy: setupRuntimeInstallPolicy(args),
2026-05-10 23:12:26 +02:00
...(args.embeddingBackend ? { embeddingBackend: args.embeddingBackend } : {}),
...(args.embeddingApiKeyEnv ? { embeddingApiKeyEnv: args.embeddingApiKeyEnv } : {}),
...(args.embeddingApiKeyFile ? { embeddingApiKeyFile: args.embeddingApiKeyFile } : {}),
forcePrompt: forcePromptSteps.has('embeddings') || runOnly === 'embeddings',
showPromptInstructions,
skipEmbeddings: args.skipEmbeddings || !shouldRunEmbeddings,
},
io,
);
} else if (step === 'databases') {
const databasesRunner =
deps.databases ??
2026-05-10 23:51:24 +02:00
((databaseArgs, databaseIo) => runKtxSetupDatabasesStep(databaseArgs, databaseIo, deps.databasesDeps));
2026-05-10 23:12:26 +02:00
stepResult = await databasesRunner(
{
projectDir: projectResult.projectDir,
inputMode: args.inputMode,
...(args.databaseDrivers ? { databaseDrivers: args.databaseDrivers } : {}),
...(args.databaseConnectionIds ? { databaseConnectionIds: args.databaseConnectionIds } : {}),
...(args.databaseConnectionId ? { databaseConnectionId: args.databaseConnectionId } : {}),
...(args.databaseUrl ? { databaseUrl: args.databaseUrl } : {}),
databaseSchemas: args.databaseSchemas,
...(args.enableHistoricSql !== undefined ? { enableHistoricSql: args.enableHistoricSql } : {}),
...(args.disableHistoricSql !== undefined ? { disableHistoricSql: args.disableHistoricSql } : {}),
...(args.historicSqlWindowDays !== undefined ? { historicSqlWindowDays: args.historicSqlWindowDays } : {}),
...(args.historicSqlMinExecutions !== undefined
? { historicSqlMinExecutions: args.historicSqlMinExecutions }
: {}),
2026-05-10 23:12:26 +02:00
...(args.historicSqlMinCalls !== undefined ? { historicSqlMinCalls: args.historicSqlMinCalls } : {}),
...(args.historicSqlServiceAccountPatterns
? { historicSqlServiceAccountPatterns: args.historicSqlServiceAccountPatterns }
: {}),
...(args.historicSqlRedactionPatterns
? { historicSqlRedactionPatterns: args.historicSqlRedactionPatterns }
: {}),
skipDatabases: args.skipDatabases || !shouldRunDatabases,
},
io,
);
} else if (step === 'sources') {
const sourcesRunner =
2026-05-10 23:51:24 +02:00
deps.sources ?? ((sourceArgs, sourceIo) => runKtxSetupSourcesStep(sourceArgs, sourceIo, deps.sourcesDeps));
2026-05-10 23:12:26 +02:00
stepResult = await sourcesRunner(
{
projectDir: projectResult.projectDir,
inputMode: args.inputMode,
...(args.source ? { source: args.source } : {}),
...(args.sourceConnectionId ? { sourceConnectionId: args.sourceConnectionId } : {}),
...(args.sourcePath ? { sourcePath: args.sourcePath } : {}),
...(args.sourceGitUrl ? { sourceGitUrl: args.sourceGitUrl } : {}),
...(args.sourceBranch ? { sourceBranch: args.sourceBranch } : {}),
...(args.sourceSubpath ? { sourceSubpath: args.sourceSubpath } : {}),
...(args.sourceAuthTokenRef ? { sourceAuthTokenRef: args.sourceAuthTokenRef } : {}),
...(args.sourceUrl ? { sourceUrl: args.sourceUrl } : {}),
...(args.sourceApiKeyRef ? { sourceApiKeyRef: args.sourceApiKeyRef } : {}),
...(args.sourceClientId ? { sourceClientId: args.sourceClientId } : {}),
...(args.sourceClientSecretRef ? { sourceClientSecretRef: args.sourceClientSecretRef } : {}),
...(args.sourceWarehouseConnectionId ? { sourceWarehouseConnectionId: args.sourceWarehouseConnectionId } : {}),
...(args.sourceProjectName ? { sourceProjectName: args.sourceProjectName } : {}),
...(args.sourceProfilesPath ? { sourceProfilesPath: args.sourceProfilesPath } : {}),
...(args.sourceTarget ? { sourceTarget: args.sourceTarget } : {}),
...(args.metabaseDatabaseId !== undefined ? { metabaseDatabaseId: args.metabaseDatabaseId } : {}),
...(args.notionCrawlMode ? { notionCrawlMode: args.notionCrawlMode } : {}),
...(args.notionRootPageIds ? { notionRootPageIds: args.notionRootPageIds } : {}),
runInitialSourceIngest: args.runInitialSourceIngest ?? false,
skipSources: args.skipSources === true || !shouldRunSources,
},
io,
);
} else if (step === 'context') {
const contextRunner =
deps.context ??
2026-05-10 23:51:24 +02:00
((contextArgs, contextIo) => runKtxSetupContextStep(contextArgs, contextIo, deps.contextDeps));
2026-05-10 23:12:26 +02:00
stepResult = await contextRunner(
{
projectDir: projectResult.projectDir,
inputMode: args.inputMode,
forcePrompt: forcePromptSteps.has('context') || runOnly === 'context',
allowEmpty: true,
},
io,
);
} else {
const agentsRunner =
2026-05-10 23:51:24 +02:00
deps.agents ?? ((agentArgs, agentIo) => runKtxSetupAgentsStep(agentArgs, agentIo, deps.agentsDeps));
2026-05-10 23:12:26 +02:00
stepResult = await agentsRunner(
{
projectDir: projectResult.projectDir,
inputMode: args.inputMode,
yes: args.yes,
agents: true,
...(args.target ? { target: args.target } : {}),
scope: args.agentScope ?? 'project',
2026-05-12 23:51:46 +02:00
mode: 'cli',
2026-05-10 23:12:26 +02:00
skipAgents: false,
},
io,
);
}
if (stepResult.status === 'failed' || stepResult.status === 'missing-input') {
return 1;
}
if (stepResult.status === 'back') {
const previousIndex = previousNavigableStepIndex(stepIndex);
if (previousIndex < 0) {
if (canShowEntryMenu) {
continue setupLoop;
}
return 0;
}
const previousStep = setupSteps[previousIndex];
if (previousStep) {
forcePromptSteps.add(previousStep);
}
stepIndex = previousIndex;
continue;
}
if (step === 'context' && stepResult.status !== 'ready') {
if (shouldRunAgents && args.skipAgents !== true) {
if (agentsRequested) {
writeContextNotReadyForAgents(projectResult.projectDir, io);
return args.inputMode === 'disabled' ? 1 : 0;
}
return 0;
}
}
forcePromptSteps.delete(step);
stepIndex += 1;
}
break;
}
await commitSetupConfigChanges(projectResult.projectDir);
2026-05-10 23:51:24 +02:00
const status = await readKtxSetupStatus(projectResult.projectDir);
io.stdout.write(formatKtxSetupStatus(status));
2026-05-10 23:12:26 +02:00
io.stdout.write('\nWhat you can do next:\n');
io.stdout.write(
`${formatSetupNextStepLines({
setupReady: setupStatusReady(status),
hasContextTargets: setupHasContextTargets(status),
contextReady: setupContextReady(status),
agentIntegrationReady: status.agents.some((agent) => agent.ready),
}).join('\n')}\n`,
);
return 0;
}