UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

185 lines • 8.19 kB
/** * Model resolution, scoping, and initial selection */ import type { ThinkingLevel } from "@earendil-works/pi-agent-core"; import { type Api, type AuthOperationOptions, type Model, type ThinkingSelection } from "@earendil-works/pi-ai"; import type { ServiceTier } from "./extensions/builtin/service-tier.ts"; import type { ModelRegistry } from "./model-registry.ts"; import type { ModelRuntime } from "./model-runtime.ts"; /** * Scope resolution only ever reads the available-model list, so a caller that * already holds a settled availability snapshot can resolve against it instead * of triggering another provider/credential scan. */ export interface AvailableModelsSource { getAvailable(providerId?: string, options?: AuthOperationOptions): readonly Model<Api>[] | Promise<readonly Model<Api>[]>; } type ModelScopeSource = ModelRuntime | ModelRegistry | AvailableModelsSource; /** Default model IDs for each known provider */ export declare const defaultModelPerProvider: Record<string, string>; export interface ScopedModel { model: Model<Api>; /** Thinking level if explicitly specified in pattern (e.g., "model:high"), undefined otherwise */ thinkingLevel?: ThinkingLevel; /** Provenance for an explicit decorator or projected legacy Cursor variant. */ thinkingSelection?: ThinkingSelection; /** Service tier selected by configuration or caller-provided scoped model metadata. */ serviceTier?: ServiceTier; } export declare function getModelNarrowingPatterns(options: { cliPatterns?: string[]; legacyEnabledPatterns?: string[]; }): string[]; /** * Find an exact model reference match. * Supports either a bare model id or a canonical provider/modelId reference. * When matching by bare id, ambiguous matches across providers are rejected. */ export declare function findExactModelReferenceMatch(modelReference: string, availableModels: Model<Api>[]): Model<Api> | undefined; interface ResolvedModelReference { model: Model<Api>; thinkingLevel?: ThinkingLevel; thinkingSelection?: ThinkingSelection; } export declare function resolveStoredModelReference(provider: string, modelId: string, modelSource: { getModel(provider: string, modelId: string): Model<Api> | undefined; }): ResolvedModelReference | undefined; export interface ParsedModelResult { model: Model<Api> | undefined; thinkingLevel?: ThinkingLevel; thinkingSelection?: ThinkingSelection; serviceTier?: ServiceTier; warning: string | undefined; } /** * Parse a pattern to extract model, thinking level, and service tier. * Handles models with colons in their IDs (e.g., OpenRouter's :exacto suffix). * * Grammar: `<model-pattern>[:<auto|flex|priority>][:<thinking-level>]` * * Algorithm: * 1. Try to match the FULL pattern as a model (mandatory first step: real model ids * contain colons, and one may even end in a decorator-looking segment) * 2. If found, return it with no decorators * 3. If not found and the pattern has colons, split on the last colon and consume * recognized decorators right-to-left: * - valid thinking level -> use it and recurse on the prefix * - valid service tier -> use it and recurse on the prefix * - anything else -> warn and recurse on the prefix without decorators * * A decorator parsed further right never overrides one parsed further left, so the * leftmost occurrence (the grammar's slot order) wins. * * @internal Exported for testing */ export declare function parseModelPattern(pattern: string, availableModels: Model<Api>[], options?: { allowInvalidThinkingLevelFallback?: boolean; }): ParsedModelResult; /** * Resolve model patterns to actual Model objects with optional thinking levels * Format: "pattern:level" where :level is optional * For each pattern, finds all matching models and picks the best version: * 1. Prefer alias (e.g., claude-sonnet-4-5) over dated versions (claude-sonnet-4-5-20250929) * 2. If no alias, pick the latest dated version * * Supports models with colons in their IDs (e.g., OpenRouter's model:exacto). * The algorithm tries to match the full pattern first, then progressively * strips colon-suffixes to find a match. */ export interface ModelScopeDiagnostic { type: "warning"; code: "no-match" | "invalid-thinking-level" | "ultrafast-undocumented"; message: string; pattern: string; } /** * Per-stored-pattern ownership record. * * `ownedIds` lists every canonical `provider/id` the pattern resolved to in the CURRENT * registry snapshot, after first-pattern-wins dedupe (a model already claimed by an * earlier pattern is not reported again). Unresolved patterns are reported with an empty * `ownedIds` and `unresolved: true` rather than being dropped. */ export interface PatternResolution { pattern: string; ownedIds: string[]; thinkingLevel?: ThinkingLevel; serviceTier?: ServiceTier; unresolved: boolean; isGlob: boolean; } export interface ResolveModelScopeResult { scopedModels: ScopedModel[]; diagnostics: ModelScopeDiagnostic[]; /** Additive: per-pattern ownership metadata for favorites persistence. */ patternResolutions: PatternResolution[]; } export declare function resolveModelScopeFromModels(patterns: string[], models: readonly Model<Api>[]): ResolveModelScopeResult; export interface ResolveModelScopeOptions extends AuthOperationOptions { onWarning?: (message: string) => void; } export declare function resolveModelScopeWithDiagnostics(patterns: string[], modelRuntime: ModelScopeSource, options?: AuthOperationOptions): Promise<ResolveModelScopeResult>; export declare function resolveModelScope(patterns: string[], modelRuntime: ModelScopeSource, options?: ResolveModelScopeOptions): Promise<ScopedModel[]>; export interface ResolveCliModelResult { model: Model<Api> | undefined; thinkingLevel?: ThinkingLevel; thinkingSelection?: ThinkingSelection; serviceTier?: ServiceTier; warning: string | undefined; error: string | undefined; } /** * Resolve a single model from CLI flags. * * Supports: * - --provider <provider> --model <pattern> * - --model <provider>/<pattern> * - Fuzzy matching (same rules as model scoping: exact id, then partial id/name) * * Note: This does not apply the thinking level by itself, but it may *parse* and * return a thinking level from "<pattern>:<thinking>" so the caller can apply it. */ export declare function resolveCliModel(options: { cliProvider?: string; cliModel?: string; cliThinking?: ThinkingLevel; modelRuntime: ModelRuntime; }): ResolveCliModelResult; export type InitialModelProvenance = "cli" | "scoped" | "settings" | "provider-default" | "first-available"; export interface InitialModelResult { model: Model<Api> | undefined; /** Present only when the selected CLI/scoped pattern explicitly pinned a level. */ thinkingLevel: ThinkingLevel | undefined; thinkingSelection?: ThinkingSelection; fallbackMessage: string | undefined; provenance: InitialModelProvenance; } /** * Find the initial model to use based on priority: * 1. CLI args (provider + model) * 2. First model from scoped models (if not continuing/resuming) * 3. Restored from session (if continuing/resuming) * 4. Saved default from settings * 5. First available model with valid API key */ export declare function findInitialModel(options: { cliProvider?: string; cliModel?: string; scopedModels: ScopedModel[]; isContinuing: boolean; defaultProvider?: string; defaultModelId?: string; defaultThinkingLevel?: ThinkingLevel; modelThinkingLevels?: Record<string, ThinkingLevel>; modelRuntime: ModelRuntime; }): Promise<InitialModelResult>; /** * Restore model from session, with fallback to available models */ export declare function restoreModelFromSession(savedProvider: string, savedModelId: string, currentModel: Model<Api> | undefined, shouldPrintMessages: boolean, modelRuntime: ModelRuntime): Promise<{ model: Model<Api> | undefined; thinkingSelection?: ThinkingSelection; fallbackMessage: string | undefined; }>; export {}; //# sourceMappingURL=model-resolver.d.ts.map