All files / src/providers/openai Capabilities.ts

76.52% Statements 88/115
71.59% Branches 63/88
95.23% Functions 20/21
84.61% Lines 66/78

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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177            433x 433x   431x 371x 364x 355x       434x 434x   434x 425x 390x       438x 438x   435x       435x 435x 3x   432x       433x 433x   431x               871x 871x   871x       866x 866x           866x       867x 867x   867x       434x 434x   434x       3x 3x   1x       30x       22x                         4x                     434x 434x   434x 434x   434x 434x   434x 434x 434x   434x       431x 431x   431x             431x 431x 431x 431x   431x 431x   431x             37x 35x 35x       441x 441x   2140x       435x      
import { ModelRegistry } from "../../models/ModelRegistry.js";
import { PricingRegistry } from "../../models/PricingRegistry.js";
import { ModelPricing } from "../../models/types.js";
 
export class Capabilities {
  static getContextWindow(modelId: string): number | null {
    const val = ModelRegistry.getContextWindow(modelId, "openai");
    if (val) return val;
 
    if (/gpt-4.*(preview|turbo|vision|o)/.test(modelId) || /o1|o3/.test(modelId)) return 128_000;
    if (/gpt-4/.test(modelId)) return 8_192;
    if (/gpt-3\.5/.test(modelId)) return 16_385;
    return 128_000;
  }
 
  static getMaxOutputTokens(modelId: string): number | null {
    const val = ModelRegistry.getMaxOutputTokens(modelId, "openai");
    Iif (val) return val;
 
    if (/o1.*(pro|mini)|o3/.test(modelId)) return 65_536;
    if (/gpt-4o/.test(modelId)) return 16_384;
    return 4_096;
  }
 
  static supportsVision(modelId: string): boolean {
    const model = ModelRegistry.find(modelId, "openai");
    if (model?.modalities?.input?.includes("image")) return true;
 
    return /gpt-4(?!-3)|o1/.test(modelId) && !/audio|realtime|voice/.test(modelId);
  }
 
  static supportsTools(modelId: string): boolean {
    const model = ModelRegistry.find(modelId, "openai");
    if (model?.capabilities?.includes("function_calling") || model?.capabilities?.includes("tools"))
      return true;
 
    return !/embedding|moderation|dall-e|tts|whisper/.test(modelId);
  }
 
  static supportsStructuredOutput(modelId: string): boolean {
    const model = ModelRegistry.find(modelId, "openai");
    if (model?.capabilities?.includes("structured_output")) return true;
 
    return /gpt-4|o1|o3/.test(modelId);
  }
 
  static supportsJsonMode(modelId: string): boolean {
    return this.supportsStructuredOutput(modelId);
  }
 
  static supportsEmbeddings(modelId: string): boolean {
    const model = ModelRegistry.find(modelId, "openai");
    Iif (model?.modalities?.output?.includes("embeddings")) return true;
 
    return /embedding/.test(modelId);
  }
 
  static supportsImageGeneration(modelId: string): boolean {
    const model = ModelRegistry.find(modelId, "openai");
    Iif (
      model?.capabilities?.includes("image_generation") ||
      model?.modalities?.output?.includes("image")
    )
      return true;
 
    return /dall-e|image/.test(modelId);
  }
 
  static supportsTranscription(modelId: string): boolean {
    const model = ModelRegistry.find(modelId, "openai");
    Iif (model?.modalities?.input?.includes("audio")) return true;
 
    return /whisper|audio|transcribe/.test(modelId);
  }
 
  static supportsModeration(modelId: string): boolean {
    const model = ModelRegistry.find(modelId, "openai");
    Iif (model?.modalities?.output?.includes("moderation")) return true;
 
    return /moderation/.test(modelId);
  }
 
  static supportsReasoning(modelId: string): boolean {
    const model = ModelRegistry.find(modelId, "openai");
    if (model?.capabilities?.includes("reasoning")) return true;
 
    return /o\d|gpt-5/.test(modelId);
  }
 
  static supportsDeveloperRole(modelId: string): boolean {
    return /gpt-4o|o1|o3/.test(modelId);
  }
 
  static needsMaxCompletionTokens(modelId: string): boolean {
    return /o1|o3/.test(modelId);
  }
 
  static getModelType(
    modelId: string
  ):
    | "embeddings"
    | "audio"
    | "moderation"
    | "image"
    | "chat"
    | "audio_transcription"
    | "audio_speech" {
    Eif (this.supportsEmbeddings(modelId)) return "embeddings";
    if (/moderation/.test(modelId)) return "moderation";
    if (/embedding/.test(modelId)) return "embeddings";
    if (/dall-e|image/.test(modelId)) return "image";
    if (/whisper|transcribe/.test(modelId)) return "audio_transcription";
    if (/tts|speech/.test(modelId)) return "audio_speech";
    if (/audio/.test(modelId)) return "audio";
    return "chat";
  }
 
  static getModalities(modelId: string): { input: string[]; output: string[] } {
    const input = ["text"];
    const output = ["text"];
 
    const model = ModelRegistry.find(modelId, "openai");
    Iif (model?.modalities) return model.modalities;
 
    if (this.supportsVision(modelId)) input.push("image", "pdf");
    if (this.supportsTranscription(modelId)) input.push("audio");
 
    if (this.supportsImageGeneration(modelId)) output.push("image");
    Iif (this.supportsEmbeddings(modelId)) output.push("embeddings");
    if (this.supportsModeration(modelId)) output.push("moderation");
 
    return { input, output };
  }
 
  static getCapabilities(modelId: string): string[] {
    const caps = ["streaming"];
    const model = ModelRegistry.find(modelId, "openai");
 
    Iif (model) {
      model.capabilities.forEach((c) => {
        if (!caps.includes(c)) caps.push(c);
      });
      return caps;
    }
 
    if (this.supportsTools(modelId)) caps.push("function_calling");
    if (this.supportsStructuredOutput(modelId)) caps.push("structured_output");
    Iif (this.supportsEmbeddings(modelId)) caps.push("batch");
    if (/o\d|gpt-5/.test(modelId)) caps.push("reasoning");
 
    if (this.supportsImageGeneration(modelId)) caps.push("image_generation");
    if (this.supportsTranscription(modelId)) caps.push("transcription");
 
    return caps;
  }
 
  static normalizeTemperature(
    temperature: number | undefined,
    modelId: string
  ): number | undefined | null {
    if (/^(o\d|gpt-5)/.test(modelId)) return 1.0;
    Iif (/-search/.test(modelId)) return null;
    return temperature;
  }
 
  static formatDisplayName(modelId: string): string {
    const model = ModelRegistry.find(modelId, "openai");
    if (model?.name && model.name !== modelId) return model.name;
 
    return modelId.replace(/-/g, " ").replace(/\b\w/g, (c) => c.toUpperCase());
  }
 
  static getPricing(modelId: string): ModelPricing | undefined {
    return PricingRegistry.getPricing(modelId, "openai");
  }
}