repositories / pi-ext
pi-ext
bugabingas pi extensions
owned by bugabinga
extensions/ultra/settings.ts
RawFile outline
- ModelTierName line 6
- ModelTiers line 7
- UltraSettings line 9
- enabled line 11
- concurrency line 13
- maxRetries line 15
- subagentExtensions line 19
- modelTiers line 21
- ULTRA_DEFAULTS line 25
- isPlainObject line 33
- pickModelTiers line 37
- pickUltra line 52
- mergeUltraSettings line 70
- SettingsSources line 92
- global line 93
- project line 94
- loadUltraSettings line 98
// ultra — settings (Task 9, offline-testable half).
//
// Reads the `ultra.*` block from Pi's global and project settings.
/** Resolved `ultra` configuration (design §Settings). */
export type ModelTierName = "small" | "medium" | "big";
export type ModelTiers = Partial<Record<ModelTierName, string>>;
export interface UltraSettings {
/** When false, the tool + command refuse to run. */
enabled: boolean;
/** Max concurrent sub-agents in a `fanout` phase. */
concurrency: number;
/** Schema-validation retries per step. */
maxRetries: number;
/** Load host extensions into sub-agents so their tools are grantable
* (`"all"`, default) or keep sub-agents isolated to core tools (`"none"`).
* The `step.tools` allowlist still gates which tools are actually active. */
subagentExtensions: "none" | "all";
/** Optional local aliases used by step.model: small|medium|big. */
modelTiers: ModelTiers;
}
/** Documented defaults (design §Settings table). */
export const ULTRA_DEFAULTS: UltraSettings = {
enabled: true,
concurrency: 5,
maxRetries: 2,
subagentExtensions: "all",
modelTiers: {},
};
function isPlainObject(v: unknown): v is Record<string, unknown> {
return v !== null && typeof v === "object" && !Array.isArray(v);
}
function pickModelTiers(raw: unknown): ModelTiers | undefined {
if (!isPlainObject(raw)) return undefined;
const out: ModelTiers = {};
for (const name of ["small", "medium", "big"] as const) {
const value = raw[name];
if (typeof value === "string") out[name] = value;
}
return out;
}
/**
* Extract the `ultra` block from a parsed settings object, keeping only known
* keys with the correct primitive type. A missing/non-object block, wrong-typed
* values, and unknown keys are dropped.
*/
export function pickUltra(raw: unknown): Partial<UltraSettings> {
if (!isPlainObject(raw)) return {};
const ultra = raw.ultra;
if (!isPlainObject(ultra)) return {};
const out: Partial<UltraSettings> = {};
if (typeof ultra.enabled === "boolean") out.enabled = ultra.enabled;
if (typeof ultra.concurrency === "number")
out.concurrency = ultra.concurrency;
if (typeof ultra.maxRetries === "number") out.maxRetries = ultra.maxRetries;
if (ultra.subagentExtensions === "none" || ultra.subagentExtensions === "all")
out.subagentExtensions = ultra.subagentExtensions;
const modelTiers = pickModelTiers(ultra.modelTiers);
if (modelTiers) out.modelTiers = modelTiers;
return out;
}
/** Fold partial settings over the defaults; later partials win. Wrong-typed
* fields are ignored (keep the prior value). */
export function mergeUltraSettings(
...partials: Partial<UltraSettings>[]
): UltraSettings {
return partials.reduce<UltraSettings>(
(into, p) => ({
enabled: typeof p.enabled === "boolean" ? p.enabled : into.enabled,
concurrency:
typeof p.concurrency === "number" ? p.concurrency : into.concurrency,
maxRetries:
typeof p.maxRetries === "number" ? p.maxRetries : into.maxRetries,
subagentExtensions:
p.subagentExtensions === "none" || p.subagentExtensions === "all"
? p.subagentExtensions
: into.subagentExtensions,
modelTiers: isPlainObject(p.modelTiers)
? { ...into.modelTiers, ...p.modelTiers }
: into.modelTiers,
}),
{ ...ULTRA_DEFAULTS },
);
}
export interface SettingsSources {
global?: unknown;
project?: unknown;
}
/** Resolve effective settings: defaults < global < project. */
export function loadUltraSettings(sources: SettingsSources): UltraSettings {
return mergeUltraSettings(
pickUltra(sources.global),
pickUltra(sources.project),
);
}