All files / src/sdk/runtimes model-selectors.ts

100% Statements 19/19
100% Branches 15/15
100% Functions 5/5
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        43x 43x                                                     6x 6x                   3x 3x 3x                   75x 75x 75x 2x     73x 211x 211x 4x         71x             64x 64x 1x      
/**
 * Return true when a Pi model selector uses provider/model syntax.
 */
export function isPiModelSelector(model: string): boolean {
  const slashIndex = model.indexOf('/');
  return (
    slashIndex > 0 &&
    slashIndex < model.length - 1 &&
    model.indexOf('/', slashIndex + 1) === -1
  );
}
 
export type PiModelSelectorOption = 'model' | 'auxiliaryModel' | 'synthesisModel';
 
export interface PiModelSelectorTarget {
  name?: string;
  runtime?: string;
  model?: string;
  auxiliaryModel?: string;
  synthesisModel?: string;
}
 
export interface InvalidPiModelSelector {
  specName?: string;
  option: PiModelSelectorOption;
  model: string;
}
 
/**
 * Format the user-facing error for an invalid Pi model selector.
 */
export function invalidPiModelSelectorMessage(invalid: InvalidPiModelSelector): string {
  const target = invalid.specName ? ` for ${invalid.specName}` : '';
  return `Pi runtime ${invalid.option}${target} must use provider/model format: ${invalid.model}`;
}
 
/**
 * Preserve invalid Pi selector details through shared error classification.
 */
export class InvalidPiModelSelectorError extends Error {
  invalid: InvalidPiModelSelector;
 
  constructor(invalid: InvalidPiModelSelector) {
    super(invalidPiModelSelectorMessage(invalid));
    this.name = 'InvalidPiModelSelectorError';
    this.invalid = invalid;
  }
}
 
/**
 * Find the first Pi runner option using a model ID that is not provider/model.
 */
export function findInvalidPiModelSelector(
  targets: PiModelSelectorTarget[]
): InvalidPiModelSelector | undefined {
  for (const target of targets) {
    const runtimeName = target.runtime ?? 'pi';
    if (runtimeName !== 'pi') {
      continue;
    }
 
    for (const option of ['model', 'auxiliaryModel', 'synthesisModel'] as const) {
      const model = target[option];
      if (model && !isPiModelSelector(model)) {
        return { specName: target.name, option, model };
      }
    }
  }
 
  return undefined;
}
 
/**
 * Throw when any Pi runner option is not a provider/model selector.
 */
export function assertValidPiModelSelectors(targets: PiModelSelectorTarget[]): void {
  const invalid = findInvalidPiModelSelector(targets);
  if (invalid) {
    throw new InvalidPiModelSelectorError(invalid);
  }
}