All files / core/blong-kukum/orchestrator/kukum edit.ts

41.81% Statements 23/55
100% Branches 1/1
0% Functions 0/1
41.81% Lines 23/55

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 561x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x                                                                 1x  
import {library} from '@feasibleone/blong';
import {
    apply,
    applyInstructions,
    extractInstructions,
    readSource,
    withMarker,
} from '../../engine.ts';
import {
    baseResult,
    contextOf,
    diagnose,
    toResultFile,
    type KukumLibContext,
    type OperationParams,
    type OperationResult,
} from '../../operation.ts';
 
/** `kukum.<primitive>.edit` — rewrite one file, preserving its agent instructions. */
export default library(
    () =>
        async function edit(
            this: KukumLibContext,
            id: string,
            params: OperationParams,
        ): Promise<OperationResult> {
            const host = this.platform;
            const root = host.resolve(params.target ?? '.');
            const base = baseResult(id, 'edit', contextOf(id, params));
            if (!params.path) throw new Error('edit requires a `path`');
            const existing = readSource(host, root, params.path);
            const instructions = params.instructions ?? extractInstructions(existing.source);
            const content = withMarker(
                params.content ?? applyInstructions(existing.source, instructions),
            );
            const changes = [
                {
                    path: params.path,
                    absolute: host.join(root, params.path),
                    content,
                    action: existing.source === content ? 'unchanged' : 'overwrite',
                    generated: existing.generated,
                    handWritten: !existing.generated,
                } as const,
            ];
            if (!params.dryRun) {
                const {written, skipped} = apply(host, [...changes], {force: params.force});
                base.written = written;
                base.skipped = skipped;
                const diagnostics = await diagnose(host, root, written);
                return {...base, files: changes.map(toResultFile), diagnostics};
            }
            return {...base, files: changes.map(toResultFile)};
        },
);