All files / src/apps/user/pages/rules ruleLayer.ts

100% Statements 22/22
95.83% Branches 23/24
100% Functions 6/6
100% Lines 19/19

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                                                                  31x             31x               31x 124x             31x                   1878x 1875x   3x         31x                               176x 175x 134x   41x 35x 33x 22x 22x 33x       2x    
import type { RuleLayer } from "@/objects/Rule";
import type { LucideIcon } from "lucide-react";
import { FileInput, FileOutput, KeyRound, ShieldCheck, Wrench } from "lucide-react";
 
/**
 * How a rule's layer looks, in one place.
 *
 * A layer is where in a request a rule acts — whether it may run at all, what
 * the model reads, where it may look things up, what people receive — and the
 * Rules page lays rules out in that order. A layer is named by its **icon, label and place in the flow**,
 * never by a tint: cells stay neutral, and colour on these pages is kept for
 * meanings (a `StatusBadge` scale — active, off, blocked). The rule types this
 * replaced painted each cell red, blue or amber with a saturated rail, which
 * read louder than the rules themselves.
 *
 * The label and the question each layer answers are the catalogue's
 * (`GET /app/rules/catalogue`, `layers`); the label here is what a page shows
 * without it (a list, a trace). Adding a layer means adding one entry here —
 * never a class string at a call site.
 *
 * Every class string is whole and static: Tailwind ships only the classes it
 * can see in the source.
 */
export interface RuleLayerMeta {
  label: string;
  Icon: LucideIcon;
  /** 1-based place in the flow. */
  step: number;
  /** Acts on what the model wrote, rather than before it is asked. */
  afterModel: boolean;
}
 
/** Every layer, in the order a request meets them. */
export const RULE_LAYERS: readonly RuleLayer[] = [
  "access",
  "input",
  "tools",
  "output",
];
 
const ICONS: Record<RuleLayer, Omit<RuleLayerMeta, "step">> = {
  access: { label: "Access", Icon: KeyRound, afterModel: false },
  input: { label: "Input", Icon: FileInput, afterModel: false },
  tools: { label: "Tools", Icon: Wrench, afterModel: false },
  output: { label: "Output", Icon: FileOutput, afterModel: true },
};
 
export const RULE_LAYER_META: Record<RuleLayer, RuleLayerMeta> =
  Object.fromEntries(
    RULE_LAYERS.map((layer, index) => [
      layer,
      { ...ICONS[layer], step: index + 1 },
    ]),
  ) as Record<RuleLayer, RuleLayerMeta>;
 
/** The one class set every layer shares. */
export const LAYER_CLASSES = {
  /** The icon tile on a card, a row or a step. */
  iconTile: "bg-gray-100 dark:bg-gray-700/60 text-gray-600 dark:text-gray-300",
  /** The layer named as a quiet tag. */
  tag: "bg-gray-50 dark:bg-gray-800 text-gray-600 dark:text-gray-300 border-gray-200 dark:border-gray-600",
} as const;
 
/** A layer this build does not know: named by its own key, with a generic
 *  glyph, rather than borrowing another layer's. */
export function ruleLayerMeta(layer: string | null | undefined): RuleLayerMeta {
  if (layer && layer in RULE_LAYER_META) {
    return RULE_LAYER_META[layer as RuleLayer];
  }
  return { label: layer || "Rule", Icon: ShieldCheck, step: 0, afterModel: false };
}
 
/** The actions an Access rule carries — how a record from before layers is
 *  read, as svc-core's migration 105 sorted the rules themselves. */
const ACCESS_ACTIONS = new Set(["rate_limit", "cost_cap", "require_evaluation"]);
 
type Layered = {
  layer?: string | null;
  /** Records from before layers (svc-core migration 105) name a type. */
  rule_type?: string | null;
  actions?: ReadonlyArray<string | { type?: string } | null> | null;
};
 
/**
 * The layer of a rule, an activity row or a traced evaluation — its own, or
 * for a record stored before layers, the one its type and actions became: an
 * audit rule is Output; a policy rule Access when every action it ran is a
 * quota or the evaluation gate, else Input.
 */
export function layerOf(item: Layered | null | undefined): RuleLayer | null {
  if (!item) return null;
  if (item.layer && item.layer in RULE_LAYER_META) {
    return item.layer as RuleLayer;
  }
  if (item.rule_type === "audit") return "output";
  if (item.rule_type === "policy") {
    const types = (item.actions ?? [])
      .map((action) => (typeof action === "string" ? action : action?.type))
      .filter((type): type is string => !!type);
    return types.length > 0 && types.every((type) => ACCESS_ACTIONS.has(type))
      ? "access"
      : "input";
  }
  return null;
}