@@ -18,60 +18,50 @@ export class MistralHandler extends BaseProvider implements SingleCompletionHand
1818
1919 constructor ( options : ApiHandlerOptions ) {
2020 super ( )
21+
2122 if ( ! options . mistralApiKey ) {
2223 throw new Error ( "Mistral API key is required" )
2324 }
2425
25- // Set default model ID if not provided
26- this . options = {
27- ...options ,
28- apiModelId : options . apiModelId || mistralDefaultModelId ,
29- }
26+ // Set default model ID if not provided.
27+ const apiModelId = options . apiModelId || mistralDefaultModelId
28+ this . options = { ...options , apiModelId }
3029
31- const baseUrl = this . getBaseUrl ( )
32- console . debug ( `[Roo Code] MistralHandler using baseUrl: ${ baseUrl } ` )
3330 this . client = new Mistral ( {
34- serverURL : baseUrl ,
31+ serverURL : apiModelId . startsWith ( "codestral-" )
32+ ? this . options . mistralCodestralUrl || "https://codestral.mistral.ai"
33+ : "https://api.mistral.ai" ,
3534 apiKey : this . options . mistralApiKey ,
3635 } )
3736 }
3837
39- private getBaseUrl ( ) : string {
40- const modelId = this . options . apiModelId ?? mistralDefaultModelId
41- console . debug ( `[Roo Code] MistralHandler using modelId: ${ modelId } ` )
42- if ( modelId ?. startsWith ( "codestral-" ) ) {
43- return this . options . mistralCodestralUrl || "https://codestral.mistral.ai"
44- }
45- return "https://api.mistral.ai"
46- }
47-
4838 override async * createMessage (
4939 systemPrompt : string ,
5040 messages : Anthropic . Messages . MessageParam [ ] ,
5141 metadata ?: ApiHandlerCreateMessageMetadata ,
5242 ) : ApiStream {
53- const { id : model } = this . getModel ( )
43+ const { id : model , maxTokens , temperature } = this . getModel ( )
5444
5545 const response = await this . client . chat . stream ( {
56- model : this . options . apiModelId || mistralDefaultModelId ,
46+ model,
5747 messages : [ { role : "system" , content : systemPrompt } , ...convertToMistralMessages ( messages ) ] ,
58- maxTokens : this . options . includeMaxTokens ? this . getModel ( ) . info . maxTokens : undefined ,
59- temperature : this . options . modelTemperature ?? MISTRAL_DEFAULT_TEMPERATURE ,
48+ maxTokens,
49+ temperature,
6050 } )
6151
6252 for await ( const chunk of response ) {
6353 const delta = chunk . data . choices [ 0 ] ?. delta
54+
6455 if ( delta ?. content ) {
6556 let content : string = ""
57+
6658 if ( typeof delta . content === "string" ) {
6759 content = delta . content
6860 } else if ( Array . isArray ( delta . content ) ) {
6961 content = delta . content . map ( ( c ) => ( c . type === "text" ? c . text : "" ) ) . join ( "" )
7062 }
71- yield {
72- type : "text" ,
73- text : content ,
74- }
63+
64+ yield { type : "text" , text : content }
7565 }
7666
7767 if ( chunk . data . usage ) {
@@ -84,35 +74,39 @@ export class MistralHandler extends BaseProvider implements SingleCompletionHand
8474 }
8575 }
8676
87- override getModel ( ) : { id : MistralModelId ; info : ModelInfo } {
88- const modelId = this . options . apiModelId
89- if ( modelId && modelId in mistralModels ) {
90- const id = modelId as MistralModelId
91- return { id, info : mistralModels [ id ] }
92- }
93- return {
94- id : mistralDefaultModelId ,
95- info : mistralModels [ mistralDefaultModelId ] ,
96- }
77+ override getModel ( ) {
78+ const id = this . options . apiModelId ?? mistralDefaultModelId
79+ const info = mistralModels [ id as MistralModelId ] ?? mistralModels [ mistralDefaultModelId ]
80+
81+ // @TODO : Move this to the `getModelParams` function.
82+ const maxTokens = this . options . includeMaxTokens ? info . maxTokens : undefined
83+ const temperature = this . options . modelTemperature ?? MISTRAL_DEFAULT_TEMPERATURE
84+
85+ return { id, info, maxTokens, temperature }
9786 }
9887
9988 async completePrompt ( prompt : string ) : Promise < string > {
10089 try {
90+ const { id : model , temperature } = this . getModel ( )
91+
10192 const response = await this . client . chat . complete ( {
102- model : this . options . apiModelId || mistralDefaultModelId ,
93+ model,
10394 messages : [ { role : "user" , content : prompt } ] ,
104- temperature : this . options . modelTemperature ?? MISTRAL_DEFAULT_TEMPERATURE ,
95+ temperature,
10596 } )
10697
10798 const content = response . choices ?. [ 0 ] ?. message . content
99+
108100 if ( Array . isArray ( content ) ) {
109101 return content . map ( ( c ) => ( c . type === "text" ? c . text : "" ) ) . join ( "" )
110102 }
103+
111104 return content || ""
112105 } catch ( error ) {
113106 if ( error instanceof Error ) {
114107 throw new Error ( `Mistral completion error: ${ error . message } ` )
115108 }
109+
116110 throw error
117111 }
118112 }
0 commit comments