useSelectedModel.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. import {
  2. type ProviderName,
  3. type ProviderSettings,
  4. type RouterModels,
  5. type ModelInfo,
  6. anthropicDefaultModelId,
  7. anthropicModels,
  8. bedrockDefaultModelId,
  9. bedrockModels,
  10. deepSeekDefaultModelId,
  11. deepSeekModels,
  12. geminiDefaultModelId,
  13. geminiModels,
  14. mistralDefaultModelId,
  15. mistralModels,
  16. openAiModelInfoSaneDefaults,
  17. openAiNativeDefaultModelId,
  18. openAiNativeModels,
  19. vertexDefaultModelId,
  20. vertexModels,
  21. xaiDefaultModelId,
  22. xaiModels,
  23. groqModels,
  24. groqDefaultModelId,
  25. chutesModels,
  26. chutesDefaultModelId,
  27. vscodeLlmModels,
  28. vscodeLlmDefaultModelId,
  29. openRouterDefaultModelId,
  30. requestyDefaultModelId,
  31. glamaDefaultModelId,
  32. unboundDefaultModelId,
  33. litellmDefaultModelId,
  34. } from "@roo/shared/api"
  35. import { useRouterModels } from "./useRouterModels"
  36. export const useSelectedModel = (apiConfiguration?: ProviderSettings) => {
  37. const { data: routerModels, isLoading, isError } = useRouterModels()
  38. const provider = apiConfiguration?.apiProvider || "anthropic"
  39. const { id, info } =
  40. apiConfiguration && routerModels
  41. ? getSelectedModel({ provider, apiConfiguration, routerModels })
  42. : { id: anthropicDefaultModelId, info: undefined }
  43. return { provider, id, info, isLoading, isError }
  44. }
  45. function getSelectedModel({
  46. provider,
  47. apiConfiguration,
  48. routerModels,
  49. }: {
  50. provider: ProviderName
  51. apiConfiguration: ProviderSettings
  52. routerModels: RouterModels
  53. }): { id: string; info: ModelInfo } {
  54. switch (provider) {
  55. case "openrouter": {
  56. const id = apiConfiguration.openRouterModelId ?? openRouterDefaultModelId
  57. const info = routerModels.openrouter[id]
  58. return info
  59. ? { id, info }
  60. : { id: openRouterDefaultModelId, info: routerModels.openrouter[openRouterDefaultModelId] }
  61. }
  62. case "requesty": {
  63. const id = apiConfiguration.requestyModelId ?? requestyDefaultModelId
  64. const info = routerModels.requesty[id]
  65. return info
  66. ? { id, info }
  67. : { id: requestyDefaultModelId, info: routerModels.requesty[requestyDefaultModelId] }
  68. }
  69. case "glama": {
  70. const id = apiConfiguration.glamaModelId ?? glamaDefaultModelId
  71. const info = routerModels.glama[id]
  72. return info ? { id, info } : { id: glamaDefaultModelId, info: routerModels.glama[glamaDefaultModelId] }
  73. }
  74. case "unbound": {
  75. const id = apiConfiguration.unboundModelId ?? unboundDefaultModelId
  76. const info = routerModels.unbound[id]
  77. return info
  78. ? { id, info }
  79. : { id: unboundDefaultModelId, info: routerModels.unbound[unboundDefaultModelId] }
  80. }
  81. case "litellm": {
  82. const id = apiConfiguration.litellmModelId ?? litellmDefaultModelId
  83. const info = routerModels.litellm[id]
  84. return info
  85. ? { id, info }
  86. : { id: litellmDefaultModelId, info: routerModels.litellm[litellmDefaultModelId] }
  87. }
  88. case "xai": {
  89. const id = apiConfiguration.apiModelId ?? xaiDefaultModelId
  90. const info = xaiModels[id as keyof typeof xaiModels]
  91. return info ? { id, info } : { id: xaiDefaultModelId, info: xaiModels[xaiDefaultModelId] }
  92. }
  93. case "groq": {
  94. const id = apiConfiguration.apiModelId ?? groqDefaultModelId
  95. const info = groqModels[id as keyof typeof groqModels]
  96. return info ? { id, info } : { id: groqDefaultModelId, info: groqModels[groqDefaultModelId] }
  97. }
  98. case "chutes": {
  99. const id = apiConfiguration.apiModelId ?? chutesDefaultModelId
  100. const info = chutesModels[id as keyof typeof chutesModels]
  101. return info ? { id, info } : { id: chutesDefaultModelId, info: chutesModels[chutesDefaultModelId] }
  102. }
  103. case "bedrock": {
  104. const id = apiConfiguration.apiModelId ?? bedrockDefaultModelId
  105. const info = bedrockModels[id as keyof typeof bedrockModels]
  106. // Special case for custom ARN.
  107. if (id === "custom-arn") {
  108. return {
  109. id,
  110. info: { maxTokens: 5000, contextWindow: 128_000, supportsPromptCache: false, supportsImages: true },
  111. }
  112. }
  113. return info ? { id, info } : { id: bedrockDefaultModelId, info: bedrockModels[bedrockDefaultModelId] }
  114. }
  115. case "vertex": {
  116. const id = apiConfiguration.apiModelId ?? vertexDefaultModelId
  117. const info = vertexModels[id as keyof typeof vertexModels]
  118. return info ? { id, info } : { id: vertexDefaultModelId, info: vertexModels[vertexDefaultModelId] }
  119. }
  120. case "gemini": {
  121. const id = apiConfiguration.apiModelId ?? geminiDefaultModelId
  122. const info = geminiModels[id as keyof typeof geminiModels]
  123. return info ? { id, info } : { id: geminiDefaultModelId, info: geminiModels[geminiDefaultModelId] }
  124. }
  125. case "deepseek": {
  126. const id = apiConfiguration.apiModelId ?? deepSeekDefaultModelId
  127. const info = deepSeekModels[id as keyof typeof deepSeekModels]
  128. return info ? { id, info } : { id: deepSeekDefaultModelId, info: deepSeekModels[deepSeekDefaultModelId] }
  129. }
  130. case "openai-native": {
  131. const id = apiConfiguration.apiModelId ?? openAiNativeDefaultModelId
  132. const info = openAiNativeModels[id as keyof typeof openAiNativeModels]
  133. return info
  134. ? { id, info }
  135. : { id: openAiNativeDefaultModelId, info: openAiNativeModels[openAiNativeDefaultModelId] }
  136. }
  137. case "mistral": {
  138. const id = apiConfiguration.apiModelId ?? mistralDefaultModelId
  139. const info = mistralModels[id as keyof typeof mistralModels]
  140. return info ? { id, info } : { id: mistralDefaultModelId, info: mistralModels[mistralDefaultModelId] }
  141. }
  142. case "openai": {
  143. const id = apiConfiguration.openAiModelId ?? ""
  144. const info = apiConfiguration?.openAiCustomModelInfo ?? openAiModelInfoSaneDefaults
  145. return { id, info }
  146. }
  147. case "ollama": {
  148. const id = apiConfiguration.ollamaModelId ?? ""
  149. const info = openAiModelInfoSaneDefaults
  150. return { id, info }
  151. }
  152. case "lmstudio": {
  153. const id = apiConfiguration.lmStudioModelId ?? ""
  154. const info = openAiModelInfoSaneDefaults
  155. return { id, info }
  156. }
  157. case "vscode-lm": {
  158. const id = apiConfiguration?.vsCodeLmModelSelector
  159. ? `${apiConfiguration.vsCodeLmModelSelector.vendor}/${apiConfiguration.vsCodeLmModelSelector.family}`
  160. : vscodeLlmDefaultModelId
  161. const modelFamily = apiConfiguration?.vsCodeLmModelSelector?.family ?? vscodeLlmDefaultModelId
  162. const info = vscodeLlmModels[modelFamily as keyof typeof vscodeLlmModels]
  163. return { id, info: { ...openAiModelInfoSaneDefaults, ...info, supportsImages: false } } // VSCode LM API currently doesn't support images.
  164. }
  165. // case "anthropic":
  166. // case "human-relay":
  167. // case "fake-ai":
  168. default: {
  169. const id = apiConfiguration.apiModelId ?? anthropicDefaultModelId
  170. const info = anthropicModels[id as keyof typeof anthropicModels]
  171. return info ? { id, info } : { id: anthropicDefaultModelId, info: anthropicModels[anthropicDefaultModelId] }
  172. }
  173. }
  174. }