useSelectedModel.ts 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. import { useOpenRouterModelProviders } from "./useOpenRouterModelProviders"
  37. export const useSelectedModel = (apiConfiguration?: ProviderSettings) => {
  38. const provider = apiConfiguration?.apiProvider || "anthropic"
  39. const openRouterModelId = provider === "openrouter" ? apiConfiguration?.openRouterModelId : undefined
  40. const routerModels = useRouterModels()
  41. const openRouterModelProviders = useOpenRouterModelProviders(openRouterModelId)
  42. const { id, info } =
  43. apiConfiguration &&
  44. typeof routerModels.data !== "undefined" &&
  45. typeof openRouterModelProviders.data !== "undefined"
  46. ? getSelectedModel({
  47. provider,
  48. apiConfiguration,
  49. routerModels: routerModels.data,
  50. openRouterModelProviders: openRouterModelProviders.data,
  51. })
  52. : { id: anthropicDefaultModelId, info: undefined }
  53. return {
  54. provider,
  55. id,
  56. info,
  57. isLoading: routerModels.isLoading || openRouterModelProviders.isLoading,
  58. isError: routerModels.isError || openRouterModelProviders.isError,
  59. }
  60. }
  61. function getSelectedModel({
  62. provider,
  63. apiConfiguration,
  64. routerModels,
  65. openRouterModelProviders,
  66. }: {
  67. provider: ProviderName
  68. apiConfiguration: ProviderSettings
  69. routerModels: RouterModels
  70. openRouterModelProviders: Record<string, ModelInfo>
  71. }): { id: string; info: ModelInfo } {
  72. switch (provider) {
  73. case "openrouter": {
  74. const id = apiConfiguration.openRouterModelId ?? openRouterDefaultModelId
  75. let info = routerModels.openrouter[id]
  76. const specificProvider = apiConfiguration.openRouterSpecificProvider
  77. if (specificProvider && openRouterModelProviders[specificProvider]) {
  78. info = openRouterModelProviders[specificProvider]
  79. }
  80. return info
  81. ? { id, info }
  82. : { id: openRouterDefaultModelId, info: routerModels.openrouter[openRouterDefaultModelId] }
  83. }
  84. case "requesty": {
  85. const id = apiConfiguration.requestyModelId ?? requestyDefaultModelId
  86. const info = routerModels.requesty[id]
  87. return info
  88. ? { id, info }
  89. : { id: requestyDefaultModelId, info: routerModels.requesty[requestyDefaultModelId] }
  90. }
  91. case "glama": {
  92. const id = apiConfiguration.glamaModelId ?? glamaDefaultModelId
  93. const info = routerModels.glama[id]
  94. return info ? { id, info } : { id: glamaDefaultModelId, info: routerModels.glama[glamaDefaultModelId] }
  95. }
  96. case "unbound": {
  97. const id = apiConfiguration.unboundModelId ?? unboundDefaultModelId
  98. const info = routerModels.unbound[id]
  99. return info
  100. ? { id, info }
  101. : { id: unboundDefaultModelId, info: routerModels.unbound[unboundDefaultModelId] }
  102. }
  103. case "litellm": {
  104. const id = apiConfiguration.litellmModelId ?? litellmDefaultModelId
  105. const info = routerModels.litellm[id]
  106. return info
  107. ? { id, info }
  108. : { id: litellmDefaultModelId, info: routerModels.litellm[litellmDefaultModelId] }
  109. }
  110. case "xai": {
  111. const id = apiConfiguration.apiModelId ?? xaiDefaultModelId
  112. const info = xaiModels[id as keyof typeof xaiModels]
  113. return info ? { id, info } : { id: xaiDefaultModelId, info: xaiModels[xaiDefaultModelId] }
  114. }
  115. case "groq": {
  116. const id = apiConfiguration.apiModelId ?? groqDefaultModelId
  117. const info = groqModels[id as keyof typeof groqModels]
  118. return info ? { id, info } : { id: groqDefaultModelId, info: groqModels[groqDefaultModelId] }
  119. }
  120. case "chutes": {
  121. const id = apiConfiguration.apiModelId ?? chutesDefaultModelId
  122. const info = chutesModels[id as keyof typeof chutesModels]
  123. return info ? { id, info } : { id: chutesDefaultModelId, info: chutesModels[chutesDefaultModelId] }
  124. }
  125. case "bedrock": {
  126. const id = apiConfiguration.apiModelId ?? bedrockDefaultModelId
  127. const info = bedrockModels[id as keyof typeof bedrockModels]
  128. // Special case for custom ARN.
  129. if (id === "custom-arn") {
  130. return {
  131. id,
  132. info: { maxTokens: 5000, contextWindow: 128_000, supportsPromptCache: false, supportsImages: true },
  133. }
  134. }
  135. return info ? { id, info } : { id: bedrockDefaultModelId, info: bedrockModels[bedrockDefaultModelId] }
  136. }
  137. case "vertex": {
  138. const id = apiConfiguration.apiModelId ?? vertexDefaultModelId
  139. const info = vertexModels[id as keyof typeof vertexModels]
  140. return info ? { id, info } : { id: vertexDefaultModelId, info: vertexModels[vertexDefaultModelId] }
  141. }
  142. case "gemini": {
  143. const id = apiConfiguration.apiModelId ?? geminiDefaultModelId
  144. const info = geminiModels[id as keyof typeof geminiModels]
  145. return info ? { id, info } : { id: geminiDefaultModelId, info: geminiModels[geminiDefaultModelId] }
  146. }
  147. case "deepseek": {
  148. const id = apiConfiguration.apiModelId ?? deepSeekDefaultModelId
  149. const info = deepSeekModels[id as keyof typeof deepSeekModels]
  150. return info ? { id, info } : { id: deepSeekDefaultModelId, info: deepSeekModels[deepSeekDefaultModelId] }
  151. }
  152. case "openai-native": {
  153. const id = apiConfiguration.apiModelId ?? openAiNativeDefaultModelId
  154. const info = openAiNativeModels[id as keyof typeof openAiNativeModels]
  155. return info
  156. ? { id, info }
  157. : { id: openAiNativeDefaultModelId, info: openAiNativeModels[openAiNativeDefaultModelId] }
  158. }
  159. case "mistral": {
  160. const id = apiConfiguration.apiModelId ?? mistralDefaultModelId
  161. const info = mistralModels[id as keyof typeof mistralModels]
  162. return info ? { id, info } : { id: mistralDefaultModelId, info: mistralModels[mistralDefaultModelId] }
  163. }
  164. case "openai": {
  165. const id = apiConfiguration.openAiModelId ?? ""
  166. const info = apiConfiguration?.openAiCustomModelInfo ?? openAiModelInfoSaneDefaults
  167. return { id, info }
  168. }
  169. case "ollama": {
  170. const id = apiConfiguration.ollamaModelId ?? ""
  171. const info = openAiModelInfoSaneDefaults
  172. return { id, info }
  173. }
  174. case "lmstudio": {
  175. const id = apiConfiguration.lmStudioModelId ?? ""
  176. const info = openAiModelInfoSaneDefaults
  177. return { id, info }
  178. }
  179. case "vscode-lm": {
  180. const id = apiConfiguration?.vsCodeLmModelSelector
  181. ? `${apiConfiguration.vsCodeLmModelSelector.vendor}/${apiConfiguration.vsCodeLmModelSelector.family}`
  182. : vscodeLlmDefaultModelId
  183. const modelFamily = apiConfiguration?.vsCodeLmModelSelector?.family ?? vscodeLlmDefaultModelId
  184. const info = vscodeLlmModels[modelFamily as keyof typeof vscodeLlmModels]
  185. return { id, info: { ...openAiModelInfoSaneDefaults, ...info, supportsImages: false } } // VSCode LM API currently doesn't support images.
  186. }
  187. // case "anthropic":
  188. // case "human-relay":
  189. // case "fake-ai":
  190. default: {
  191. const id = apiConfiguration.apiModelId ?? anthropicDefaultModelId
  192. const info = anthropicModels[id as keyof typeof anthropicModels]
  193. return info ? { id, info } : { id: anthropicDefaultModelId, info: anthropicModels[anthropicDefaultModelId] }
  194. }
  195. }
  196. }