|
| 1 | +import type {ReasoningEffort, ServiceTier} from "./app-server"; |
| 2 | +import type {Model} from "./app-server/v2"; |
| 3 | + |
| 4 | +/** |
| 5 | + * ACP Model ID, combining the base model ID, reasoning effort level, and optional service tier. |
| 6 | + * @example |
| 7 | + * const id = ModelId.fromString("gpt-5.2[high]"); |
| 8 | + * const fastId = ModelId.fromString("gpt-5.2[high]@fast"); |
| 9 | + */ |
| 10 | +export class ModelId { |
| 11 | + private constructor( |
| 12 | + public readonly model: string, |
| 13 | + public readonly effort: string, |
| 14 | + public readonly serviceTier: ServiceTier | null = null |
| 15 | + ) {} |
| 16 | + |
| 17 | + static fromComponents(model: Model, effort: ReasoningEffort, serviceTier: ServiceTier | null = null): ModelId { |
| 18 | + return new ModelId(model.id, effort, serviceTier); |
| 19 | + } |
| 20 | + |
| 21 | + static create(modelId: string, effort: ReasoningEffort, serviceTier: ServiceTier | null = null): ModelId { |
| 22 | + return new ModelId(modelId, effort, serviceTier); |
| 23 | + } |
| 24 | + |
| 25 | + static fromString(modelId: string): ModelId { |
| 26 | + const bracketMatch = modelId.match(/^(?<model>[^\[]+)\[(?<effort>[^\]]+)](?:@(?<serviceTier>.+))?$/); |
| 27 | + const model = bracketMatch?.groups?.["model"]; |
| 28 | + const effort = bracketMatch?.groups?.["effort"]; |
| 29 | + const serviceTier = bracketMatch?.groups?.["serviceTier"] ?? null; |
| 30 | + |
| 31 | + if (!model || !effort) { |
| 32 | + throw new Error(`Unsupported format of modelId: ${modelId}. Expected: modelId[effort] or modelId[effort]@fast.`); |
| 33 | + } |
| 34 | + |
| 35 | + // The generated app-server ServiceTier type also includes "flex", but ACP model IDs |
| 36 | + // only expose Fast variants for now because model/list advertises Fast support. |
| 37 | + if (serviceTier !== null && serviceTier !== "fast") { |
| 38 | + throw new Error(`Unsupported service tier ${serviceTier} for modelId: ${modelId}.`); |
| 39 | + } |
| 40 | + |
| 41 | + return new ModelId(model, effort, serviceTier); |
| 42 | + } |
| 43 | + |
| 44 | + toString(): string { |
| 45 | + const suffix = this.serviceTier === null ? "" : `@${this.serviceTier}`; |
| 46 | + return `${this.model}[${this.effort}]${suffix}`; |
| 47 | + } |
| 48 | +} |
0 commit comments