All files / realm/blong-commander/orchestrator/commander nodeViewer.ts

38.23% Statements 13/34
100% Branches 1/1
0% Functions 0/1
38.23% Lines 13/34

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 351x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                           1x  
import {handler} from '@feasibleone/blong';
import type {ICommanderSource} from '../../types.ts';
import {sources as defaultSources} from '../../config/sources.ts';
 
/**
 * commanderNodeViewer — resolve the viewer for a leaf node.
 * Returns the explicit `viewer` type, `model` when the `{subject}.{object}`
 * is recognized by the model system (`subject.model.list`), else `json`.
 */
export default handler(
    ({config, handler: h}) =>
        async function commanderNodeViewer(
            params: {source: string; level: number},
            $meta: Record<string, unknown>,
        ) {
            const sources = (config as {sources?: ICommanderSource[]}).sources ?? defaultSources;
            const source = sources.find(s => s.name === params.source);
            if (!source) throw new Error(`Unknown commander source: ${params.source}`);
            const level = source.levels[params.level];
            if (level?.viewer) return {viewer: level.viewer};
            if (level?.model) {
                const models = (await h['subject.model.list']({}, $meta)) as
                    | Array<{subject?: string; object?: string}>
                    | undefined;
                const recognized = (models ?? []).some(
                    model =>
                        model.subject === level.model?.subject &&
                        model.object === level.model?.object,
                );
                if (recognized) return {viewer: 'model', model: level.model};
            }
            return {viewer: 'json'};
        },
);