Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/Aquaticat/Monochromatic. TypeScript and Rust monorepo: Claude Code and Pi coding-agent plugins, a m
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343/** Advisor model selection paired with model-budgeted serialized context. @module */
import type { ModelRegistry, SessionEntry,} from '@earendil-works/pi-coding-agent';import type { ReadonlyDeep, } from 'type-fest';import type { ForeignBorrowed, } from '@monochromatic-dev/ownership-marker-foreign-borrowed/ts';import { buildAdvisorContext, maxContextCharsForAdvisorModel,} from './context.ts';import { selectDefaultModelFromContextEstimates, } from '@monochromatic-dev/pi-shared-model-selection/ts';import { type CurrentMainModelIdentity, scopeAvoidingCurrentMainModel, selectAdvisorModel,} from './advisor-selection.ts';import { requireAdvisorScopeWithOutputCapacity, } from './output-eligibility.ts';import type { AdvisorConfig, AdvisorContext, AdvisorModelSelection, EffectiveModelScope, ScopedAdvisorModel,} from './types.ts';
//region Types
/** Selected model paired with serialized context built for that model. */export type AdvisorSelectionContext = { /** Advisor model selection. */ readonly selection: AdvisorModelSelection; /** Serialized context using selected model budget. */ readonly advisorContext: AdvisorContext; /** Candidate evidence snapshots used by default fallback without re-reading the session. */ readonly candidates?: readonly AdvisorContextCandidate[];};
/** Options for selecting model and model-budgeted context together. */export type SelectAdvisorRunContextOptions = ForeignBorrowed<{ /** Session branch entries from pi. */ readonly branch: readonly ForeignBorrowed<SessionEntry>[]; /** Runtime Advisor configuration. */ readonly config: AdvisorConfig; /** Advisor model system prompt. */ readonly advisorSystemPrompt: string; /** Effective scoped model set. */ readonly scope: ReadonlyDeep<EffectiveModelScope>; /** Global model registry for explicit slug validation. */ readonly modelRegistry: ModelRegistry; /** Active primary model to avoid for default selection when possible. */ readonly currentMainModel?: CurrentMainModelIdentity; /** Optional user-requested model slug. */ readonly requestedSlug?: string; /** Focused question supplied by the primary agent. */ readonly question?: string; /** Current Advisor tool call id to omit. */ readonly toolCallId?: string;}>;
/** Options for building context for one scoped Advisor model. */type BuildContextForScopedModelOptions = ForeignBorrowed<Readonly<{ /** Session branch entries from pi. */ readonly branch: readonly SessionEntry[]; /** Runtime Advisor configuration. */ readonly config: AdvisorConfig; /** Advisor model system prompt. */ readonly advisorSystemPrompt: string; /** Scoped Advisor model. */ readonly scopedModel: ScopedAdvisorModel; /** Focused question supplied by the primary agent. */ readonly question?: string; /** Current Advisor tool call id to omit. */ readonly toolCallId?: string;}>>;
/** Context candidate for a scoped Advisor model. */export type AdvisorContextCandidate = { /** Scoped Advisor model. */ readonly scopedModel: ScopedAdvisorModel; /** Serialized context using scoped model budget. */ readonly advisorContext: AdvisorContext;};
//endregion Types
//region Candidate lookup
/** Return context candidate for selected canonical model slug. @param candidates - model-budgeted context candidates @param selectedSlug - selected canonical model slug @returns matching context candidate @throws when selected candidate disappeared @example ```typescript selectedContextCandidate({ candidates, selectedSlug: 'provider/model' }); ``` */function selectedContextCandidate( { candidates, selectedSlug, }: { readonly candidates: readonly AdvisorContextCandidate[]; readonly selectedSlug: string; },): AdvisorContextCandidate { for (const candidate of candidates) { if (candidate.scopedModel .canonicalSlug === selectedSlug) return candidate; } throw new Error( `advisor: selected model ${selectedSlug} context disappeared`, );}
//endregion Candidate lookup
//region Public API
/** Select Advisor model and build context with that model's context budget. @param options - branch, config, scope, and model-selection inputs @returns selected model and serialized context @example ```typescript const selectionContext = selectAdvisorRunContext({ branch, config, advisorSystemPrompt, scope, modelRegistry }); ``` */export function selectAdvisorRunContext( options: SelectAdvisorRunContextOptions,): AdvisorSelectionContext { if (options.requestedSlug !== undefined) { /** Explicit Advisor model selection. */ const selection = selectAdvisorModel({ scope: options.scope, requestedSlug: options.requestedSlug, config: options.config, estimatedInputTokens: 0, modelRegistry: options.modelRegistry, ...(options.currentMainModel === undefined ? {} : { currentMainModel: options.currentMainModel, }), },); return { selection, advisorContext: buildContextForScopedModel({ branch: options.branch, config: options.config, advisorSystemPrompt: options.advisorSystemPrompt, scopedModel: selection.selected, ...(options.question === undefined ? {} : { question: options.question, }), ...(options.toolCallId === undefined ? {} : { toolCallId: options.toolCallId, }), },), }; }
/** Scoped models whose endpoints advertise configured output capacity. */ const eligibleScope = requireAdvisorScopeWithOutputCapacity({ scope: options.scope, maxAdvisorOutputTokens: options.config .maxAdvisorOutputTokens, },); /** Default-selection scope with current main model removed when alternatives exist. */ const defaultScope = scopeAvoidingCurrentMainModel({ scope: eligibleScope, ...(options.currentMainModel === undefined ? {} : { currentMainModel: options.currentMainModel, }), },); /** Context candidates using each scoped model's effective context budget. */ const candidates: AdvisorContextCandidate[] = []; for (const scopedModel of defaultScope.entries) { candidates.push({ scopedModel, advisorContext: buildContextForScopedModel({ branch: options.branch, config: options.config, advisorSystemPrompt: options.advisorSystemPrompt, scopedModel, ...(options.question === undefined ? {} : { question: options.question, }), ...(options.toolCallId === undefined ? {} : { toolCallId: options.toolCallId, }), },), },); } /** Input token estimates keyed by canonical scoped model slug. */ const estimatedInputTokensBySlug = new Map<string, number>(); for (const candidate of candidates) { estimatedInputTokensBySlug.set( candidate.scopedModel .canonicalSlug, candidate.advisorContext .estimatedInputTokens, ); } /** Default Advisor model selection using each candidate's own estimate. */ const defaultSelection = selectDefaultModelFromContextEstimates({ scope: defaultScope, estimatedInputTokensBySlug, maxOutputTokens: options.config .maxAdvisorOutputTokens, },); /** Context candidate matching selected default model. */ const selectedCandidate = selectedContextCandidate({ candidates, selectedSlug: defaultSelection.selected .canonicalSlug, },); return { selection: { selected: defaultSelection.selected, defaultSelection, }, advisorContext: selectedCandidate.advisorContext, candidates: defaultSelection.ranking .map(function rankedCandidate(score): AdvisorContextCandidate { return selectedContextCandidate({ candidates, selectedSlug: score.slug, },); },), };}
//endregion Public API
//region Internal helpers
/** Build serialized Advisor context for one scoped model. @param options - branch, config, prompt, scoped model, and current tool call @returns serialized context truncated for scoped model */function buildContextForScopedModel( options: BuildContextForScopedModelOptions,): AdvisorContext { /** Effective serialized-context character budget for selected model. */ const maxContextChars = maxContextCharsForAdvisorModel({ config: options.config, model: options.scopedModel .model, advisorSystemPrompt: options.advisorSystemPrompt, ...(options.question === undefined ? {} : { question: options.question, }), },); return buildAdvisorContext({ branch: options.branch, config: options.config, advisorSystemPrompt: options.advisorSystemPrompt, maxContextChars, ...(options.question === undefined ? {} : { question: options.question, }), ...(options.toolCallId === undefined ? {} : { toolCallId: options.toolCallId, }), },);}
//endregion Internal helpers