@@ -111,14 +111,35 @@ export class VsCodeLmHandler extends BaseProvider implements SingleCompletionHan
111111 */
112112 async createClient ( selector : vscode . LanguageModelChatSelector ) : Promise < vscode . LanguageModelChat > {
113113 try {
114+ // If we have an ID, try to find the specific model by ID first
115+ if ( selector . id ) {
116+ // Get all available models
117+ const allModels = await vscode . lm . selectChatModels ( { } )
118+
119+ // Find the model with the matching ID
120+ const modelById = allModels . find ( ( model ) => model . id === selector . id )
121+
122+ if ( modelById ) {
123+ console . debug ( `Roo Code <Language Model API>: Found model by ID: ${ modelById . id } ` )
124+ return modelById
125+ } else {
126+ console . warn (
127+ `Roo Code <Language Model API>: Model with ID '${ selector . id } ' not found, falling back to selector` ,
128+ )
129+ }
130+ }
131+
132+ // Fallback to selector-based selection
114133 const models = await vscode . lm . selectChatModels ( selector )
115134
116135 // Use first available model or create a minimal model object
117136 if ( models && Array . isArray ( models ) && models . length > 0 ) {
137+ console . debug ( `Roo Code <Language Model API>: Selected model: ${ models [ 0 ] . id } ` )
118138 return models [ 0 ]
119139 }
120140
121141 // Create a minimal model if no models are available
142+ console . warn ( `Roo Code <Language Model API>: No models available, creating fallback model` )
122143 return {
123144 id : "default-lm" ,
124145 name : "Default Language Model" ,
@@ -363,17 +384,38 @@ export class VsCodeLmHandler extends BaseProvider implements SingleCompletionHan
363384 try {
364385 // Create the response stream with minimal required options
365386 const requestOptions : vscode . LanguageModelChatRequestOptions = {
366- justification : `Roo Code would like to use '${ client . name } ' from '${ client . vendor } ', Click 'Allow' to proceed.` ,
387+ justification : `Roo Code would like to use '${ client . name || client . id } ' from '${ client . vendor } '. Click 'Allow' to proceed.` ,
367388 }
368389
369390 // Note: Tool support is currently provided by the VSCode Language Model API directly
370391 // Extensions can register tools using vscode.lm.registerTool()
371392
372- const response : vscode . LanguageModelChatResponse = await client . sendRequest (
373- vsCodeLmMessages ,
374- requestOptions ,
375- this . currentRequestCancellation . token ,
376- )
393+ let response : vscode . LanguageModelChatResponse
394+ try {
395+ response = await client . sendRequest (
396+ vsCodeLmMessages ,
397+ requestOptions ,
398+ this . currentRequestCancellation . token ,
399+ )
400+ } catch ( error ) {
401+ // Check if this is a model approval error
402+ if ( error instanceof Error ) {
403+ if (
404+ error . message . includes ( "model_not_supported" ) ||
405+ error . message . includes ( "Model is not supported" )
406+ ) {
407+ throw new Error (
408+ "Model not approved. Please select the model in settings, then click 'Allow' when prompted by VS Code to approve access to the language model." ,
409+ )
410+ } else if ( error . message . includes ( "cancelled" ) || error . message . includes ( "Cancelled" ) ) {
411+ throw new Error (
412+ "Model access was cancelled. Please approve access to use the VS Code Language Model API." ,
413+ )
414+ }
415+ }
416+ // Re-throw the original error if it's not a known approval issue
417+ throw error
418+ }
377419
378420 // Consume the stream and handle both text and tool call chunks
379421 for await ( const chunk of response . stream ) {
@@ -566,7 +608,16 @@ const VSCODE_LM_STATIC_BLACKLIST: string[] = ["claude-3.7-sonnet", "claude-3.7-s
566608export async function getVsCodeLmModels ( ) {
567609 try {
568610 const models = ( await vscode . lm . selectChatModels ( { } ) ) || [ ]
569- return models . filter ( ( model ) => ! VSCODE_LM_STATIC_BLACKLIST . includes ( model . id ) )
611+ // Filter blacklisted models and ensure all required fields are present
612+ return models
613+ . filter ( ( model ) => ! VSCODE_LM_STATIC_BLACKLIST . includes ( model . id ) )
614+ . map ( ( model ) => ( {
615+ id : model . id ,
616+ vendor : model . vendor ,
617+ family : model . family ,
618+ name : model . name ,
619+ version : model . version ,
620+ } ) )
570621 } catch ( error ) {
571622 console . error (
572623 `Error fetching VS Code LM models: ${ JSON . stringify ( error , Object . getOwnPropertyNames ( error ) , 2 ) } ` ,
0 commit comments