2026-05-21 02:21:22 +02:00
|
|
|
import { KtxIngestEmbeddingPortAdapter, type KtxEmbeddingPort } from '@ktx/context';
|
2026-05-10 23:51:24 +02:00
|
|
|
import { loadKtxProject } from '@ktx/context/project';
|
2026-05-13 16:05:58 +02:00
|
|
|
import {
|
2026-05-14 15:15:20 +02:00
|
|
|
type LocalKnowledgeSearchResult,
|
|
|
|
|
type LocalKnowledgeSummary,
|
2026-05-13 16:05:58 +02:00
|
|
|
listLocalKnowledgePages,
|
2026-05-21 02:21:22 +02:00
|
|
|
searchLocalKnowledgePages as defaultSearchLocalKnowledgePages,
|
2026-05-13 16:05:58 +02:00
|
|
|
} from '@ktx/context/wiki';
|
2026-05-21 02:21:22 +02:00
|
|
|
import {
|
|
|
|
|
resolveProjectEmbeddingProvider,
|
|
|
|
|
type EmbeddingProviderResolution,
|
|
|
|
|
} from './embedding-resolution.js';
|
2026-05-14 15:15:20 +02:00
|
|
|
import { resolveOutputMode } from './io/mode.js';
|
2026-05-17 02:32:41 +02:00
|
|
|
import { createRankBadgeFormatter, printList, type PrintListColumn } from './io/print-list.js';
|
2026-05-10 23:12:26 +02:00
|
|
|
|
2026-05-10 23:51:24 +02:00
|
|
|
export type KtxKnowledgeArgs =
|
2026-05-21 02:21:22 +02:00
|
|
|
| { command: 'list'; projectDir: string; userId: string; output?: string; json?: boolean; cliVersion: string }
|
2026-05-14 15:15:20 +02:00
|
|
|
| {
|
|
|
|
|
command: 'search';
|
|
|
|
|
projectDir: string;
|
|
|
|
|
query: string;
|
|
|
|
|
userId: string;
|
|
|
|
|
output?: string;
|
|
|
|
|
json?: boolean;
|
|
|
|
|
limit?: number;
|
2026-05-17 02:32:41 +02:00
|
|
|
debug?: boolean;
|
2026-05-21 02:21:22 +02:00
|
|
|
cliVersion: string;
|
2026-05-13 16:05:58 +02:00
|
|
|
};
|
2026-05-10 23:12:26 +02:00
|
|
|
|
2026-05-14 15:15:20 +02:00
|
|
|
type KtxKnowledgeIo = import('./cli-runtime.js').KtxCliIo;
|
|
|
|
|
|
|
|
|
|
const WIKI_LIST_COLUMNS: ReadonlyArray<PrintListColumn<LocalKnowledgeSummary>> = [
|
|
|
|
|
{ key: 'scope', label: 'SCOPE', plain: '' },
|
|
|
|
|
{ key: 'key', label: 'KEY', plain: '' },
|
|
|
|
|
{ key: 'summary', label: 'SUMMARY', plain: '', optional: true, dim: true },
|
|
|
|
|
];
|
|
|
|
|
|
2026-05-17 02:32:41 +02:00
|
|
|
function wikiSearchColumns(
|
|
|
|
|
rows: ReadonlyArray<LocalKnowledgeSearchResult>,
|
|
|
|
|
): ReadonlyArray<PrintListColumn<LocalKnowledgeSearchResult>> {
|
|
|
|
|
return [
|
|
|
|
|
{
|
|
|
|
|
key: 'score',
|
|
|
|
|
label: 'SCORE',
|
|
|
|
|
plain: 'score=',
|
|
|
|
|
role: 'badge',
|
|
|
|
|
prettyFormat: createRankBadgeFormatter(rows),
|
|
|
|
|
dim: true,
|
|
|
|
|
},
|
|
|
|
|
{ key: 'scope', label: 'SCOPE', plain: '' },
|
|
|
|
|
{ key: 'key', label: 'KEY', plain: '' },
|
|
|
|
|
{ key: 'summary', label: 'SUMMARY', plain: '', optional: true, dim: true },
|
|
|
|
|
];
|
|
|
|
|
}
|
2026-05-10 23:12:26 +02:00
|
|
|
|
2026-05-11 22:59:45 +02:00
|
|
|
interface KtxKnowledgeDeps {
|
|
|
|
|
embeddingService?: KtxEmbeddingPort | null;
|
2026-05-21 02:21:22 +02:00
|
|
|
resolveEmbeddingProvider?: typeof resolveProjectEmbeddingProvider;
|
|
|
|
|
searchLocalKnowledgePages?: typeof defaultSearchLocalKnowledgePages;
|
2026-05-11 22:59:45 +02:00
|
|
|
}
|
|
|
|
|
|
2026-05-21 02:21:22 +02:00
|
|
|
function resolutionToEmbeddingPort(resolution: EmbeddingProviderResolution): KtxEmbeddingPort | null {
|
|
|
|
|
if (
|
|
|
|
|
resolution.kind === 'configured' ||
|
|
|
|
|
resolution.kind === 'managed-running' ||
|
|
|
|
|
resolution.kind === 'managed-started'
|
|
|
|
|
) {
|
|
|
|
|
return new KtxIngestEmbeddingPortAdapter(resolution.provider);
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function wikiSearchEmbeddingService(
|
2026-05-11 22:59:45 +02:00
|
|
|
project: Awaited<ReturnType<typeof loadKtxProject>>,
|
|
|
|
|
deps: KtxKnowledgeDeps,
|
2026-05-21 02:21:22 +02:00
|
|
|
args: { cliVersion: string },
|
|
|
|
|
io: KtxKnowledgeIo,
|
|
|
|
|
): Promise<KtxEmbeddingPort | null> {
|
2026-05-11 22:59:45 +02:00
|
|
|
if ('embeddingService' in deps) {
|
|
|
|
|
return deps.embeddingService ?? null;
|
|
|
|
|
}
|
2026-05-21 02:21:22 +02:00
|
|
|
const resolution = await (deps.resolveEmbeddingProvider ?? resolveProjectEmbeddingProvider)(project, {
|
|
|
|
|
mode: 'use-if-running',
|
|
|
|
|
cliVersion: args.cliVersion,
|
|
|
|
|
io,
|
|
|
|
|
});
|
|
|
|
|
return resolutionToEmbeddingPort(resolution);
|
2026-05-11 22:59:45 +02:00
|
|
|
}
|
|
|
|
|
|
2026-05-17 02:32:41 +02:00
|
|
|
function writeWikiSearchDebug(
|
|
|
|
|
io: KtxKnowledgeIo,
|
|
|
|
|
input: {
|
|
|
|
|
mode: string;
|
|
|
|
|
embeddingConfigured: boolean;
|
|
|
|
|
results: LocalKnowledgeSearchResult[];
|
|
|
|
|
},
|
|
|
|
|
): void {
|
|
|
|
|
io.stderr.write(
|
|
|
|
|
`[debug] wiki search mode=${input.mode} embedding=${input.embeddingConfigured ? 'configured' : 'unconfigured'} results=${input.results.length}\n`,
|
|
|
|
|
);
|
|
|
|
|
const lanes = input.results[0]?.lanes ?? [];
|
|
|
|
|
for (const lane of lanes) {
|
|
|
|
|
const reason = lane.reason ? ` reason=${lane.reason}` : '';
|
|
|
|
|
io.stderr.write(
|
|
|
|
|
`[debug] wiki search lane=${lane.lane} status=${lane.status} returned=${lane.returnedCandidateCount} weight=${lane.weight}${reason}\n`,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-11 22:59:45 +02:00
|
|
|
export async function runKtxKnowledge(
|
|
|
|
|
args: KtxKnowledgeArgs,
|
|
|
|
|
io: KtxKnowledgeIo = process,
|
|
|
|
|
deps: KtxKnowledgeDeps = {},
|
|
|
|
|
): Promise<number> {
|
2026-05-10 23:12:26 +02:00
|
|
|
try {
|
2026-05-10 23:51:24 +02:00
|
|
|
const project = await loadKtxProject({ projectDir: args.projectDir });
|
2026-05-10 23:12:26 +02:00
|
|
|
if (args.command === 'list') {
|
|
|
|
|
const pages = await listLocalKnowledgePages(project, { userId: args.userId });
|
2026-05-14 15:15:20 +02:00
|
|
|
const mode = resolveOutputMode({ explicit: args.output, json: args.json, io });
|
|
|
|
|
printList<LocalKnowledgeSummary>({
|
|
|
|
|
rows: pages,
|
|
|
|
|
columns: WIKI_LIST_COLUMNS,
|
|
|
|
|
groupBy: 'scope',
|
|
|
|
|
emptyMessage: `No local wiki pages found in ${project.projectDir}`,
|
|
|
|
|
emptyHint: 'Add Markdown files under wiki/ or run `ktx ingest <connectionId>`.',
|
|
|
|
|
unit: 'page',
|
|
|
|
|
command: 'wiki list',
|
|
|
|
|
mode,
|
|
|
|
|
io,
|
|
|
|
|
});
|
2026-05-10 23:12:26 +02:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
if (args.command === 'search') {
|
2026-05-21 02:21:22 +02:00
|
|
|
const embeddingService = await wikiSearchEmbeddingService(project, deps, { cliVersion: args.cliVersion }, io);
|
|
|
|
|
const search = deps.searchLocalKnowledgePages ?? defaultSearchLocalKnowledgePages;
|
|
|
|
|
const results = await search(project, {
|
2026-05-11 22:59:45 +02:00
|
|
|
query: args.query,
|
|
|
|
|
userId: args.userId,
|
2026-05-17 02:32:41 +02:00
|
|
|
embeddingService,
|
2026-05-13 13:01:56 +02:00
|
|
|
limit: args.limit,
|
2026-05-11 22:59:45 +02:00
|
|
|
});
|
2026-05-17 02:32:41 +02:00
|
|
|
if (args.debug) {
|
|
|
|
|
writeWikiSearchDebug(io, {
|
|
|
|
|
mode: project.config.storage.search,
|
|
|
|
|
embeddingConfigured: embeddingService !== null,
|
|
|
|
|
results,
|
|
|
|
|
});
|
|
|
|
|
}
|
2026-05-14 15:15:20 +02:00
|
|
|
const mode = resolveOutputMode({ explicit: args.output, json: args.json, io });
|
|
|
|
|
let emptyMessage = `No local wiki pages matched "${args.query}"`;
|
2026-05-20 01:52:37 +02:00
|
|
|
let emptyHint = 'Run `ktx wiki` to inspect available pages.';
|
2026-05-14 15:15:20 +02:00
|
|
|
if (results.length === 0 && mode !== 'json') {
|
2026-05-10 23:12:26 +02:00
|
|
|
const pages = await listLocalKnowledgePages(project, { userId: args.userId });
|
|
|
|
|
if (pages.length === 0) {
|
2026-05-14 15:15:20 +02:00
|
|
|
emptyMessage = `No local wiki pages found in ${project.projectDir}`;
|
|
|
|
|
emptyHint = 'Add Markdown files under wiki/ or run `ktx ingest <connectionId>`.';
|
2026-05-10 23:12:26 +02:00
|
|
|
}
|
|
|
|
|
}
|
2026-05-14 15:15:20 +02:00
|
|
|
printList<LocalKnowledgeSearchResult>({
|
|
|
|
|
rows: results,
|
2026-05-17 02:32:41 +02:00
|
|
|
columns: wikiSearchColumns(results),
|
2026-05-14 15:15:20 +02:00
|
|
|
groupBy: 'scope',
|
|
|
|
|
emptyMessage,
|
|
|
|
|
emptyHint,
|
|
|
|
|
unit: 'page',
|
|
|
|
|
command: 'wiki search',
|
|
|
|
|
mode,
|
|
|
|
|
io,
|
|
|
|
|
});
|
2026-05-10 23:12:26 +02:00
|
|
|
return 0;
|
|
|
|
|
}
|
2026-05-13 16:05:58 +02:00
|
|
|
return 0;
|
2026-05-10 23:12:26 +02:00
|
|
|
} catch (error) {
|
|
|
|
|
io.stderr.write(`${error instanceof Error ? error.message : String(error)}\n`);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
}
|