2026-05-11 15:50:34 +02:00
|
|
|
import { createPythonSemanticLayerComputePort, type KtxSemanticLayerComputePort } from '@ktx/context/daemon';
|
|
|
|
|
import type { KtxCliIo } from './cli-runtime.js';
|
2026-05-17 19:15:09 +02:00
|
|
|
import { createClackPromptAdapter, createStaticCliSpinner, type KtxCliSpinner } from './clack.js';
|
2026-05-11 15:50:34 +02:00
|
|
|
import {
|
|
|
|
|
installManagedPythonRuntime,
|
|
|
|
|
readManagedPythonRuntimeStatus,
|
|
|
|
|
type InstalledKtxRuntimeManifest,
|
|
|
|
|
type KtxRuntimeFeature,
|
|
|
|
|
type ManagedPythonRuntimeInstallOptions,
|
|
|
|
|
type ManagedPythonRuntimeInstallResult,
|
|
|
|
|
type ManagedPythonRuntimeLayout,
|
|
|
|
|
type ManagedPythonRuntimeLayoutOptions,
|
|
|
|
|
type ManagedPythonRuntimeStatus,
|
|
|
|
|
} from './managed-python-runtime.js';
|
|
|
|
|
|
|
|
|
|
export type KtxManagedPythonInstallPolicy = 'prompt' | 'auto' | 'never';
|
|
|
|
|
|
|
|
|
|
export function runtimeInstallPolicyFromFlags(options: {
|
|
|
|
|
yes?: boolean;
|
|
|
|
|
input?: boolean;
|
|
|
|
|
}): KtxManagedPythonInstallPolicy {
|
|
|
|
|
if (options.yes === true && options.input === false) {
|
|
|
|
|
throw new Error('Choose only one runtime install mode: --yes or --no-input');
|
|
|
|
|
}
|
|
|
|
|
if (options.yes === true) {
|
|
|
|
|
return 'auto';
|
|
|
|
|
}
|
|
|
|
|
return options.input === false ? 'never' : 'prompt';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface ManagedPythonCommandRuntime {
|
|
|
|
|
layout: ManagedPythonRuntimeLayout;
|
|
|
|
|
manifest: InstalledKtxRuntimeManifest;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface ManagedPythonCommandDeps {
|
|
|
|
|
readStatus?: (options: ManagedPythonRuntimeLayoutOptions) => Promise<ManagedPythonRuntimeStatus>;
|
|
|
|
|
installRuntime?: (options: ManagedPythonRuntimeInstallOptions) => Promise<ManagedPythonRuntimeInstallResult>;
|
2026-05-12 11:31:43 +02:00
|
|
|
confirmInstall?: (message: string, io: KtxCliIo) => Promise<boolean>;
|
2026-05-17 19:15:09 +02:00
|
|
|
spinner?: () => KtxCliSpinner;
|
2026-05-11 15:50:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface ManagedPythonCommandOptions extends ManagedPythonCommandDeps {
|
|
|
|
|
cliVersion: string;
|
|
|
|
|
installPolicy: KtxManagedPythonInstallPolicy;
|
|
|
|
|
io: KtxCliIo;
|
|
|
|
|
feature?: KtxRuntimeFeature;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface ManagedPythonSemanticLayerComputeOptions extends ManagedPythonCommandOptions {
|
|
|
|
|
createPythonCompute?: typeof createPythonSemanticLayerComputePort;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function managedRuntimeInstallCommand(feature: KtxRuntimeFeature): string {
|
|
|
|
|
return feature === 'local-embeddings'
|
2026-05-20 01:36:54 +02:00
|
|
|
? 'ktx admin runtime install --feature local-embeddings --yes'
|
|
|
|
|
: 'ktx admin runtime install --yes';
|
2026-05-11 15:50:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function installPrompt(feature: KtxRuntimeFeature): string {
|
|
|
|
|
const label = feature === 'local-embeddings' ? 'local embeddings Python runtime' : 'core Python runtime';
|
|
|
|
|
return `KTX needs to install the ${label}. This downloads Python dependencies with uv. Continue?`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function runtimeRequiredMessage(feature: KtxRuntimeFeature): string {
|
|
|
|
|
return `KTX Python runtime is required for this command. Run: ${managedRuntimeInstallCommand(feature)}`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function hasFeature(manifest: InstalledKtxRuntimeManifest, feature: KtxRuntimeFeature): boolean {
|
|
|
|
|
return manifest.features.includes(feature);
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-12 11:31:43 +02:00
|
|
|
async function defaultConfirmInstall(message: string, io: KtxCliIo): Promise<boolean> {
|
|
|
|
|
if (io.stdout.isTTY !== true) {
|
2026-05-11 15:50:34 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
2026-05-12 11:31:43 +02:00
|
|
|
const prompts = createClackPromptAdapter();
|
|
|
|
|
return await prompts.confirm({ message, initialValue: true });
|
2026-05-11 15:50:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function ensureManagedPythonCommandRuntime(
|
|
|
|
|
options: ManagedPythonCommandOptions,
|
|
|
|
|
): Promise<ManagedPythonCommandRuntime> {
|
|
|
|
|
const feature = options.feature ?? 'core';
|
|
|
|
|
const readStatus = options.readStatus ?? readManagedPythonRuntimeStatus;
|
|
|
|
|
const installRuntime = options.installRuntime ?? installManagedPythonRuntime;
|
|
|
|
|
const status = await readStatus({ cliVersion: options.cliVersion });
|
|
|
|
|
|
|
|
|
|
if (status.kind === 'ready' && status.manifest && hasFeature(status.manifest, feature)) {
|
|
|
|
|
return { layout: status.layout, manifest: status.manifest };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (options.installPolicy === 'never') {
|
|
|
|
|
throw new Error(runtimeRequiredMessage(feature));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (options.installPolicy === 'prompt') {
|
|
|
|
|
const confirmInstall = options.confirmInstall ?? defaultConfirmInstall;
|
2026-05-12 11:31:43 +02:00
|
|
|
const confirmed = await confirmInstall(installPrompt(feature), options.io);
|
2026-05-11 15:50:34 +02:00
|
|
|
if (!confirmed) {
|
|
|
|
|
throw new Error(`KTX Python runtime installation was cancelled. Run: ${managedRuntimeInstallCommand(feature)}`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-17 19:15:09 +02:00
|
|
|
const progress = (options.spinner ?? (() => createStaticCliSpinner(options.io)))();
|
|
|
|
|
progress.start(`Installing KTX Python runtime (${feature}) with uv...`);
|
|
|
|
|
try {
|
|
|
|
|
const installed = await installRuntime({
|
|
|
|
|
cliVersion: options.cliVersion,
|
|
|
|
|
features: [feature],
|
|
|
|
|
force: false,
|
|
|
|
|
});
|
|
|
|
|
progress.stop(`KTX Python runtime ready: ${installed.layout.versionDir}`);
|
|
|
|
|
return { layout: installed.layout, manifest: installed.manifest };
|
|
|
|
|
} catch (error) {
|
|
|
|
|
progress.error(`KTX Python runtime install failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
2026-05-11 15:50:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function createManagedPythonSemanticLayerComputePort(
|
|
|
|
|
options: ManagedPythonSemanticLayerComputeOptions,
|
|
|
|
|
): Promise<KtxSemanticLayerComputePort> {
|
|
|
|
|
const runtime = await ensureManagedPythonCommandRuntime({
|
|
|
|
|
cliVersion: options.cliVersion,
|
|
|
|
|
installPolicy: options.installPolicy,
|
|
|
|
|
io: options.io,
|
|
|
|
|
feature: 'core',
|
|
|
|
|
...(options.readStatus ? { readStatus: options.readStatus } : {}),
|
|
|
|
|
...(options.installRuntime ? { installRuntime: options.installRuntime } : {}),
|
|
|
|
|
...(options.confirmInstall ? { confirmInstall: options.confirmInstall } : {}),
|
2026-05-17 19:15:09 +02:00
|
|
|
...(options.spinner ? { spinner: options.spinner } : {}),
|
2026-05-11 15:50:34 +02:00
|
|
|
});
|
|
|
|
|
const createPythonCompute = options.createPythonCompute ?? createPythonSemanticLayerComputePort;
|
|
|
|
|
return createPythonCompute({
|
|
|
|
|
command: runtime.manifest.python.daemonExecutable,
|
|
|
|
|
args: [],
|
|
|
|
|
});
|
|
|
|
|
}
|