ktx/packages/cli/src/memory-flow-interactive.ts
Andrey Avtomonov 34d4a1e9e1 refactor(cli): delete internal barrel index.ts files
The 34 `index.ts` re-export barrels inside `packages/cli/src/` were
holdovers from the pre-fold multi-workspace structure. Post-fold-in they
served no production purpose: external consumers go through the single
package main entry, and in-repo callers mostly imported through them
only because the path was short. Internally, knip flagged most barrel
re-exports as production-dead (only reached via tests).

This change:
- Deletes every internal barrel except `packages/cli/src/index.ts`
  (the published package entry).
- Rewrites ~270 source/test files to import each name directly from
  the file that defines it.
- Moves `tools/warehouse-verification/index.ts` to
  `create-warehouse-verification-tools.ts` (the function it defined
  locally) and updates its single consumer.
- Renames `search/backend-conformance.ts` → `.test-utils.ts` to match
  the existing test-helper file convention.
- Deletes 13 dead test-only chains (dbt-descriptions/*,
  live-database/extracted-schema, live-database/structural-sync,
  relationship-* feedback/review chain) plus their tests and a
  cascading orphan integration test.
- Updates test mocks that pointed at deleted barrel paths
  (notion-client, connector barrels in scan/local-scan-connectors
  tests) to mock the source files instead.
- Points the maintainer benchmark script
  (`scripts/relationship-benchmark-report.mjs`) at source files
  instead of `dist/context/scan/index.js`.
- Drops the barrel `!` entries from `knip.json`; adds explicit
  production entries only for the benchmark code reached via dist by
  the maintainer script.

Net: 413 files changed, ~1.2k insertions, ~9.4k deletions.

`pnpm run dead-code` (Biome + knip default + knip production) and
`pnpm run type-check` are clean; 2277 tests pass.
2026-05-21 12:41:20 +02:00

139 lines
4.5 KiB
TypeScript

import { emitKeypressEvents } from 'node:readline';
import { buildMemoryFlowViewModel } from './context/ingest/memory-flow/view-model.js';
import { createInitialMemoryFlowInteractionState, reduceMemoryFlowInteractionState } from './context/ingest/memory-flow/interaction.js';
import { renderMemoryFlowInteractive } from './context/ingest/memory-flow/interactive-render.js';
import type { MemoryFlowInteractionCommand, MemoryFlowInteractionState, MemoryFlowReplayInput } from './context/ingest/memory-flow/types.js';
interface KtxMemoryFlowKey {
name?: string;
ctrl?: boolean;
}
export interface KtxMemoryFlowStdin {
isTTY?: boolean;
isRaw?: boolean;
setRawMode?(value: boolean): void;
resume?(): void;
pause?(): void;
on(event: 'keypress', listener: (chunk: string, key: KtxMemoryFlowKey) => void): this;
off?(event: 'keypress', listener: (chunk: string, key: KtxMemoryFlowKey) => void): this;
removeListener?(event: 'keypress', listener: (chunk: string, key: KtxMemoryFlowKey) => void): this;
}
interface KtxMemoryFlowInteractiveIo {
stdin?: KtxMemoryFlowStdin;
stdout: {
isTTY?: boolean;
columns?: number;
write(chunk: string): void;
};
}
interface RenderMemoryFlowInteractiveOptions {
prepareKeypressEvents?(stdin: KtxMemoryFlowStdin): void;
}
function defaultPrepareKeypressEvents(stdin: KtxMemoryFlowStdin): void {
emitKeypressEvents(stdin as Parameters<typeof emitKeypressEvents>[0]);
}
/** @internal */
export function memoryFlowCommandForKey(
chunk: string,
search: MemoryFlowInteractionState['search'],
key: KtxMemoryFlowKey,
): MemoryFlowInteractionCommand | null {
if (search.editing) {
if (key.name === 'escape') return 'search-clear';
if (key.name === 'return' || key.name === 'enter') return 'search-submit';
if (key.name === 'backspace') return 'search-backspace';
if (chunk.length === 1 && chunk >= ' ' && chunk !== '\u007f') {
return { type: 'search-input', value: chunk };
}
return null;
}
if (key.ctrl === true && key.name === 'c') {
return 'quit';
}
if (key.name === '/') return 'search-start';
if (key.name === 'left') return 'left';
if (key.name === 'right') return 'right';
if (key.name === 'up') return 'up';
if (key.name === 'down') return 'down';
if (key.name === 'return' || key.name === 'enter') return 'enter';
if (key.name === 'tab') return 'tab';
if (key.name === 'f') return 'filter';
if (key.name === 'p') return 'provenance';
if (key.name === 't') return 'transcript';
if (key.name === 'q' || key.name === 'escape') return 'quit';
return null;
}
function removeKeypressListener(
stdin: KtxMemoryFlowStdin,
handler: (chunk: string, key: KtxMemoryFlowKey) => void,
): void {
if (stdin.off) {
stdin.off('keypress', handler);
return;
}
stdin.removeListener?.('keypress', handler);
}
function repaint(input: MemoryFlowReplayInput, state: MemoryFlowInteractionState, io: KtxMemoryFlowInteractiveIo): void {
const view = buildMemoryFlowViewModel(input);
io.stdout.write('\u001b[2J\u001b[H');
io.stdout.write(renderMemoryFlowInteractive(view, state, { terminalWidth: io.stdout.columns }));
}
export async function renderMemoryFlowInteractively(
input: MemoryFlowReplayInput,
io: KtxMemoryFlowInteractiveIo,
options: RenderMemoryFlowInteractiveOptions = {},
): Promise<void> {
const stdin = io.stdin;
if (stdin?.isTTY !== true) {
const view = buildMemoryFlowViewModel(input);
io.stdout.write(
renderMemoryFlowInteractive(view, createInitialMemoryFlowInteractionState(view), {
terminalWidth: io.stdout.columns,
}),
);
return;
}
const view = buildMemoryFlowViewModel(input);
let state = createInitialMemoryFlowInteractionState(view);
const previousRawMode = stdin.isRaw === true;
return new Promise((resolve) => {
const cleanup = (): void => {
removeKeypressListener(stdin, handleKeypress);
stdin.setRawMode?.(previousRawMode);
stdin.pause?.();
};
const handleKeypress = (_chunk: string, key: KtxMemoryFlowKey): void => {
const command = memoryFlowCommandForKey(_chunk, state.search, key);
if (!command) {
return;
}
state = reduceMemoryFlowInteractionState(state, command, view);
repaint(input, state, io);
if (state.shouldQuit) {
cleanup();
resolve();
}
};
(options.prepareKeypressEvents ?? defaultPrepareKeypressEvents)(stdin);
stdin.setRawMode?.(true);
stdin.resume?.();
stdin.on('keypress', handleKeypress);
repaint(input, state, io);
});
}