context-window.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import type { ProviderSettings } from "@roo-code/types"
  2. import type { RouterModels } from "@/ui/store.js"
  3. const DEFAULT_CONTEXT_WINDOW = 200_000
  4. /**
  5. * Looks up the context window size for the current model from routerModels.
  6. *
  7. * @param routerModels - The router models data containing model info per provider
  8. * @param apiConfiguration - The current API configuration with provider and model ID
  9. * @returns The context window size, or DEFAULT_CONTEXT_WINDOW (200K) if not found
  10. */
  11. export function getContextWindow(routerModels: RouterModels | null, apiConfiguration: ProviderSettings | null): number {
  12. if (!routerModels || !apiConfiguration) {
  13. return DEFAULT_CONTEXT_WINDOW
  14. }
  15. const provider = apiConfiguration.apiProvider
  16. const modelId = getModelIdForProvider(apiConfiguration)
  17. if (!provider || !modelId) {
  18. return DEFAULT_CONTEXT_WINDOW
  19. }
  20. const providerModels = routerModels[provider]
  21. const modelInfo = providerModels?.[modelId]
  22. return modelInfo?.contextWindow ?? DEFAULT_CONTEXT_WINDOW
  23. }
  24. /**
  25. * Gets the model ID from the API configuration based on the provider type.
  26. *
  27. * Different providers store their model ID in different fields of ProviderSettings.
  28. */
  29. function getModelIdForProvider(config: ProviderSettings): string | undefined {
  30. switch (config.apiProvider) {
  31. case "openrouter":
  32. return config.openRouterModelId
  33. case "ollama":
  34. return config.ollamaModelId
  35. case "lmstudio":
  36. return config.lmStudioModelId
  37. case "openai":
  38. return config.openAiModelId
  39. case "requesty":
  40. return config.requestyModelId
  41. case "litellm":
  42. return config.litellmModelId
  43. case "deepinfra":
  44. return config.deepInfraModelId
  45. case "huggingface":
  46. return config.huggingFaceModelId
  47. case "unbound":
  48. return config.unboundModelId
  49. case "vercel-ai-gateway":
  50. return config.vercelAiGatewayModelId
  51. case "io-intelligence":
  52. return config.ioIntelligenceModelId
  53. default:
  54. // For anthropic, bedrock, vertex, gemini, xai, groq, etc.
  55. return config.apiModelId
  56. }
  57. }
  58. export { DEFAULT_CONTEXT_WINDOW }