feat(cli): clean up wiki and sl commands (#65)

* feat(cli): clean up wiki and sl commands

* test(scripts): update package artifact CLI smoke assertion
This commit is contained in:
Andrey Avtomonov 2026-05-13 15:41:10 +02:00 committed by GitHub
parent e1e9c4af91
commit c202202e6b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
19 changed files with 312 additions and 586 deletions

View file

@ -13,10 +13,10 @@ import {
readLocalSlSource,
searchLocalSlSources,
validateLocalSlSource,
writeLocalSlSource,
type LocalSlSourceSearchResult,
type LocalSlSourceSummary,
type SemanticLayerQueryInput,
} from '@ktx/context/sl';
import { writeJsonResult } from './io/print-list.js';
import {
createManagedPythonSemanticLayerComputePort,
type KtxManagedPythonInstallPolicy,
@ -28,10 +28,17 @@ profileMark('module:sl');
type SlQueryFormat = 'json' | 'sql';
export type KtxSlArgs =
| { command: 'list'; projectDir: string; connectionId?: string; query?: string; output?: string; json?: boolean }
| { command: 'read'; projectDir: string; connectionId: string; sourceName: string; json?: boolean }
| { command: 'list'; projectDir: string; connectionId?: string; output?: string; json?: boolean }
| {
command: 'search';
projectDir: string;
connectionId?: string;
query: string;
limit?: number;
output?: string;
json?: boolean;
}
| { command: 'validate'; projectDir: string; connectionId: string; sourceName: string }
| { command: 'write'; projectDir: string; connectionId: string; sourceName: string; yaml: string }
| {
command: 'query';
projectDir: string;
@ -73,6 +80,35 @@ function slSearchEmbeddingService(project: KtxLocalProject, deps: KtxSlDeps): Kt
return provider ? new KtxIngestEmbeddingPortAdapter(provider) : null;
}
async function printSlSources(input: {
rows: ReadonlyArray<LocalSlSourceSummary | LocalSlSourceSearchResult>;
command: 'sl list' | 'sl search';
output?: string;
json?: boolean;
io: KtxSlIo;
emptyMessage: string;
}): Promise<void> {
const { resolveOutputMode } = await import('./io/mode.js');
const { printList } = await import('./io/print-list.js');
const mode = resolveOutputMode({ explicit: input.output, json: input.json, io: input.io });
printList({
rows: input.rows,
columns: [
{ key: 'connectionId', label: 'CONNECTION', plain: '' },
{ key: 'name', label: 'NAME', plain: '' },
{ key: 'columnCount', label: 'COLS', plain: 'columns=', dim: true },
{ key: 'measureCount', label: 'MEASURES', plain: 'measures=', dim: true },
{ key: 'joinCount', label: 'JOINS', plain: 'joins=', dim: true },
{ key: 'description', label: 'DESCRIPTION', plain: false, optional: true, dim: true },
],
groupBy: 'connectionId',
emptyMessage: input.emptyMessage,
command: input.command,
mode,
io: input.io,
});
}
async function readSlQueryFile(path: string): Promise<SemanticLayerQueryInput> {
const parsed = JSON.parse(await readFile(path, 'utf-8')) as unknown;
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
@ -85,51 +121,32 @@ export async function runKtxSl(args: KtxSlArgs, io: KtxSlIo = process, deps: Ktx
try {
const project = await (deps.loadProject ?? loadKtxProject)({ projectDir: args.projectDir });
if (args.command === 'list') {
const sources = args.query
? await searchLocalSlSources(project, {
connectionId: args.connectionId,
query: args.query,
embeddingService: slSearchEmbeddingService(project, deps),
})
: await listLocalSlSources(project, { connectionId: args.connectionId });
const { resolveOutputMode } = await import('./io/mode.js');
const { printList } = await import('./io/print-list.js');
const mode = resolveOutputMode({ explicit: args.output, json: args.json, io });
printList({
const sources = await listLocalSlSources(project, { connectionId: args.connectionId });
await printSlSources({
rows: sources,
columns: [
{ key: 'connectionId', label: 'CONNECTION', plain: '' },
{ key: 'name', label: 'NAME', plain: '' },
{ key: 'columnCount', label: 'COLS', plain: 'columns=', dim: true },
{ key: 'measureCount', label: 'MEASURES', plain: 'measures=', dim: true },
{ key: 'joinCount', label: 'JOINS', plain: 'joins=', dim: true },
{ key: 'description', label: 'DESCRIPTION', plain: false, optional: true, dim: true },
],
groupBy: 'connectionId',
emptyMessage: `No semantic-layer sources found in ${project.projectDir}`,
command: 'sl list',
mode,
output: args.output,
json: args.json,
io,
});
return 0;
}
if (args.command === 'read') {
const source = await readLocalSlSource(project, {
if (args.command === 'search') {
const sources = await searchLocalSlSources(project, {
connectionId: args.connectionId,
sourceName: args.sourceName,
query: args.query,
embeddingService: slSearchEmbeddingService(project, deps),
limit: args.limit,
});
await printSlSources({
rows: sources,
emptyMessage: `No semantic-layer sources matched "${args.query}" in ${project.projectDir}`,
command: 'sl search',
output: args.output,
json: args.json,
io,
});
if (!source) {
throw new Error(`Semantic-layer source "${args.connectionId}/${args.sourceName}" was not found`);
}
if (args.json) {
writeJsonResult(io, {
kind: 'sl.source',
data: source,
meta: { command: 'sl read' },
});
return 0;
}
io.stdout.write(source.yaml);
return 0;
}
if (args.command === 'validate') {
@ -178,14 +195,8 @@ export async function runKtxSl(args: KtxSlArgs, io: KtxSlIo = process, deps: Ktx
io.stdout.write(`${JSON.stringify(result, null, 2)}\n`);
return 0;
}
const write = await writeLocalSlSource(project, {
connectionId: args.connectionId,
sourceName: args.sourceName,
yaml: args.yaml,
});
io.stdout.write(`Wrote ${write.path}\n`);
return 0;
const _exhaustive: never = args;
throw new Error(`Unsupported sl command: ${JSON.stringify(_exhaustive)}`);
} catch (error) {
io.stderr.write(`${error instanceof Error ? error.message : String(error)}\n`);
return 1;