All files / core/blong-kukum operation.ts

95.66% Statements 265/277
87.17% Branches 34/39
100% Functions 9/9
95.66% Lines 265/277

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 2781x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 66x 66x 65x 65x 1x 1x 1x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 1x 1x 1x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 1x 1x 64x 64x 64x 64x 64x 64x 166x 54x 166x 53x 53x 134x 166x 166x 3x 3x 3x 3x 3x 129x 35x 35x 166x 64x 64x 1x 1x 1x 166x 166x 166x 166x 166x 166x 166x 166x 166x 166x 166x 166x 166x 166x 166x 1x 1x 1x 141x 141x 1x 1x 1x 1x 1x 1x 1x 1x 3x 42x 42x 42x 42x 42x 42x 42x 42x 3x 1x 1x 1x 1x 1x 1x 65x 65x 65x 65x 65x 65x 63x 63x 63x 63x 62x 62x 62x 62x 62x 62x 62x 1x 1x 65x 65x 65x 1x 1x 1x 1x 1x 1x                         65x 1x 1x 1x 63x 63x 63x 63x 63x 63x  
import {lintCollect, type Diagnostic} from '@feasibleone/blong-lint';
import type {IRegistry} from '@feasibleone/blong/types';
import {fileURLToPath} from 'node:url';
import {
    capitalize,
    PREDICATES,
    type FileChange,
    type Predicate,
    type PrimitiveContext,
    type PrimitiveDescriptor,
    type PrimitiveHost,
} from './engine.ts';
import {getPrimitive, PRIMITIVES} from './primitives/index.ts';
 
/**
 * What the operation library functions have in common.
 *
 * The operations themselves live in `orchestrator/kukum/` as `library()`
 * functions — one file per API endpoint, reading the platform and the live
 * registry off `this` rather than taking them as arguments. This module holds
 * only the machinery they share: the `this` contract, the argument/result
 * shapes, and the helpers that turn a call into a plan.
 *
 * It is deliberately at the package root and not in `orchestrator/kukum/`:
 * every `.ts` file in a handler *group* folder is loaded as a handler, and a
 * module of shared helpers has nothing for the loader to classify.
 */
 
export interface OperationParams {
    /** Target realm/suite root. Defaults to the current working directory. */
    target?: string;
    subject?: string;
    object?: string;
    kind?: string;
    layer?: string;
    group?: string;
    platform?: 'server' | 'browser';
    params?: Record<string, unknown>;
    instructions?: string[];
    dryRun?: boolean;
    force?: boolean;
    /**
     * `auto` (default) composes with machine-generated files and leaves
     * hand-written ones alone; `replace` regenerates from scratch.
     */
    mode?: 'auto' | 'replace';
    /** For `source.get`: root-relative file path. */
    path?: string;
    /** For `edit`: full replacement content. */
    content?: string;
    /** For `source.check`: the files to lint (root-relative or absolute). */
    files?: string[];
}
 
export interface DiagnosticReport {
    diagnostics: Diagnostic[];
    errors: number;
    warnings: number;
    /** Set when diagnostics were not run, with the reason. */
    skipped?: string;
}
 
export interface OperationResult {
    primitive: string;
    predicate: Predicate;
    kind: string;
    files: Array<{
        path: string;
        content: string;
        action: string;
        handWritten: boolean;
        /** True when the content composes with the file already on disk. */
        merged?: boolean;
    }>;
    written: string[];
    skipped: string[];
    diagnostics?: DiagnosticReport;
    messages?: string[];
    /**
     * Things the caller should know before trusting the result: files that were
     * replaced (which can drop definitions they used to hold), and hand-written
     * files that were refused.
     */
    warnings?: string[];
}
 
/**
 * What `this` is inside a kukum library function.
 *
 * `Registry._createHandlers` seeds the layer's `lib` object with `platform` and
 * `registry`, and the lib proxy hands an attached function back **raw** — so
 * `this` is that object, not the port (the port is only bound in the proxy's
 * not-yet-attached fallback path).
 */
export type KukumLibContext = {
    platform: PrimitiveHost;
    registry?: IRegistry;
};
 
/**
 * The realm scaffolding template, resolved as a monorepo sibling.
 *
 * The full canonical realm (entry points, schema, seeds, models, tap and
 * Playwright tests) lives in `@feasibleone/blong-kopi`. It is read as files
 * rather than imported because `blong-gogo` owns `createRealm` and a realm must
 * never depend on the runtime.
 */
export const REALM_TEMPLATE_ROOT = fileURLToPath(new URL('../blong-kopi', import.meta.url));
 
/**
 * Files that hold more than one entity, so replacing one is lossy.
 *
 * `schema`/`table` and `schema`/`register` both write shared files that every
 * entity in the realm contributes to. `model`, `seed`, `test` and `handler`
 * write per-entity files and are safe to regenerate.
 */
export const SHARED_FILES = new Set(['meta/type/schema.ts', 'meta/db/db.ts']);
 
/** The descriptor for a primitive, or a clear error naming the unknown id. */
export function descriptorFor(id: string): PrimitiveDescriptor {
    const descriptor = getPrimitive(id);
    if (!descriptor) throw new Error(`Unknown primitive '${id}'`);
    return descriptor;
}
 
/** The primitive context a descriptor's templates are rendered against. */
export function contextOf(id: string, params: OperationParams): PrimitiveContext {
    return {
        subject: params.subject ?? 'realm',
        object: params.object ?? 'entry',
        kind: params.kind ?? getPrimitive(id)?.defaultKind ?? 'default',
        layer: params.layer,
        group: params.group,
        platform: params.platform ?? 'server',
        params: params.params ?? {},
    };
}
 
/** The result skeleton every predicate extends. */
export function baseResult(
    id: string,
    predicate: Predicate,
    ctx: PrimitiveContext,
): OperationResult {
    return {
        primitive: id,
        predicate,
        kind: ctx.kind,
        files: [],
        written: [],
        skipped: [],
    };
}
 
export function collectWarnings(
    primitive: string,
    changes: FileChange[],
    options: {applied: boolean; force?: boolean},
): string[] {
    const warnings: string[] = [];
    for (const change of changes) {
        if (change.action !== 'overwrite') continue;
        // A hand-written file is skipped unless forced, so nothing is at risk.
        if (change.handWritten && !options.force) continue;
        // Composed into the existing file: the neighbouring entities survive, so
        // there is nothing to warn about.
        if (change.merged) continue;
        const verb = options.applied ? 'replaced' : 'would replace';
        if (SHARED_FILES.has(change.path)) {
            warnings.push(
                `${primitive} add ${verb} ${change.path}, which holds every entity in the ` +
                    'realm — definitions for other entities are lost. Re-add them, or keep ' +
                    'this file under version control and merge by hand.',
            );
        } else {
            warnings.push(`${primitive} add ${verb} ${change.path}`);
        }
    }
    return warnings;
}
 
/** Project a merge-engine `FileChange` down to what the result reports. */
export function toResultFile(change: {
    path: string;
    content: string;
    action: string;
    handWritten: boolean;
    merged?: boolean;
    notices?: string[];
}) {
    return {
        path: change.path,
        content: change.content,
        action: change.action,
        handWritten: change.handWritten,
        merged: change.merged,
    };
}
 
/** Predicate → method-name fragment, so the handler map can be generated. */
export function methodName(id: string, predicate: Predicate): string {
    return `kukum${capitalize(id)}${capitalize(predicate)}`;
}
 
/**
 * The `kukum.primitive.find` payload: every primitive with its kinds.
 *
 * Also what the derived validation schema is built from, so the catalogue an
 * agent discovers through the API is the catalogue the schema accepts.
 */
export function describePrimitives() {
    return PRIMITIVES.map(primitive => ({
        id: primitive.id,
        title: primitive.title,
        skill: primitive.skill,
        summary: primitive.summary,
        kinds: primitive.kinds,
        defaultKind: primitive.defaultKind,
        predicates: PREDICATES,
    }));
}
 
/**
 * Lint a set of files (or a whole package) so an agent gets machine-readable
 * feedback from the same tsc/cspell/eslint pipeline `blong-dev lint` uses.
 */
export async function diagnoseFor(
    host: PrimitiveHost,
    root: string,
    files: string[],
    scope: 'changed' | 'package' = 'changed',
): Promise<DiagnosticReport | undefined> {
    if (!files.length) return undefined;
    // A freshly scaffolded package has no installed dependencies, so tsc would
    // report hundreds of unresolved-import errors that say nothing about the
    // generated code. Report that honestly instead of a wall of noise.
    if (!host.existsSync(host.join(root, 'node_modules'))) {
        return {
            diagnostics: [],
            errors: 0,
            warnings: 0,
            skipped: 'node_modules is not installed in the target package',
        };
    }
    try {
        const {diagnostics} = await lintCollect(root, {
            ...(scope === 'changed' ? {files} : {}),
            scope,
        });
        return {
            diagnostics,
            errors: diagnostics.filter(d => d.severity === 'error').length,
            warnings: diagnostics.filter(d => d.severity === 'warning').length,
        };
    } catch (error) {
        return {
            diagnostics: [
                {
                    tool: 'tsc',
                    severity: 'warning',
                    message: `diagnostics unavailable: ${(error as Error).message}`,
                },
            ],
            errors: 0,
            warnings: 1,
        };
    }
}
 
/** Lint the files a scaffold or edit just touched. */
export async function diagnose(
    host: PrimitiveHost,
    root: string,
    files: string[],
): Promise<DiagnosticReport | undefined> {
    return diagnoseFor(host, root, files, 'changed');
}