@@ -9,6 +9,7 @@ import copy from "copy-to-clipboard";
99import { v4 as uuidv4 } from "uuid" ;
1010import { APIKeyManager } from "./api_key" ;
1111import { copyIconSvg , sendIconSvg , stopIconSvg } from "./constants" ;
12+ import { getModelsFromProvider } from "./utils" ;
1213
1314const fs = acode . require ( "fs" ) ;
1415const select = acode . require ( "select" ) ;
@@ -287,7 +288,7 @@ class AIAssistant {
287288 }
288289
289290 try {
290- const settings = this . getSettings ( ) ;
291+ const settings = await this . getSettings ( ) ;
291292 if ( ! settings . apiKey || ! settings . baseUrl || ! settings . model ) {
292293 window . toast ( "Please configure API settings first" , 3000 ) ;
293294 return ;
@@ -376,7 +377,7 @@ class AIAssistant {
376377 if ( sendBtn ) sendBtn . innerHTML = stopIconSvg ;
377378
378379 try {
379- const settings = this . getSettings ( ) ;
380+ const settings = await this . getSettings ( ) ;
380381 if ( ! settings . apiKey || ! settings . baseUrl || ! settings . model ) {
381382 window . toast ( "Please configure API settings first" , 3000 ) ;
382383 this . switchView ( "settings" , app ) ;
@@ -521,11 +522,19 @@ class AIAssistant {
521522 return response ;
522523 }
523524
524- getSettings ( ) {
525+ async getSettings ( ) {
526+ let creds = null ;
527+ if ( this . apiKeyManager && typeof this . apiKeyManager . getCredentials === "function" ) {
528+ try {
529+ creds = await this . apiKeyManager . getCredentials ( "default" ) ;
530+ } catch ( e ) {
531+ creds = null ;
532+ }
533+ }
525534 return {
526- apiKey : localStorage . getItem ( "ai-api-key" ) || "" ,
527- baseUrl : localStorage . getItem ( "ai-base-url" ) || "https://api.openai.com/v1" ,
528- model : localStorage . getItem ( "ai-model" ) || "gpt-3.5-turbo" ,
535+ apiKey : ( creds && creds . apiKey ) || ( localStorage . getItem ( "ai-api-key" ) || "" ) ,
536+ baseUrl : ( creds && creds . baseUrl ) || ( localStorage . getItem ( "ai-base-url" ) || "https://api.openai.com/v1" ) ,
537+ model : ( creds && creds . model ) || ( localStorage . getItem ( "ai-model" ) || "gpt-3.5-turbo" ) ,
529538 } ;
530539 }
531540
@@ -542,8 +551,7 @@ class AIAssistant {
542551
543552 if ( apiKey && this . apiKeyManager && typeof this . apiKeyManager . saveAPIKey === "function" ) {
544553 try {
545- await this . apiKeyManager . saveAPIKey ( "default" , apiKey ) ;
546- localStorage . setItem ( "ai-api-key" , "saved" ) ;
554+ await this . apiKeyManager . saveAPIKey ( "default" , { apiKey, baseUrl, model } ) ;
547555 } catch ( e ) {
548556 console . error ( "saveAPIKey error:" , e ) ;
549557 }
@@ -557,29 +565,16 @@ class AIAssistant {
557565
558566 async loadSettings ( app ) {
559567 if ( ! app ) return ;
560- const settings = this . getSettings ( ) ;
568+ const settings = await this . getSettings ( ) ;
561569 const settingsForm = app . querySelector ( ".ai-settings-form" ) ;
562570 if ( ! settingsForm ) return ;
563571
564572 const baseUrlInput = settingsForm . querySelector ( "#base-url" ) ;
565573 const modelInput = settingsForm . querySelector ( "#model-input" ) ;
574+ const apiKeyInput = settingsForm . querySelector ( "#api-key" ) ;
566575 if ( baseUrlInput ) baseUrlInput . value = settings . baseUrl || "" ;
567576 if ( modelInput ) modelInput . value = settings . model || "" ;
568-
569- if ( this . apiKeyManager && typeof this . apiKeyManager . getAPIKey === "function" ) {
570- try {
571- const apiKey = await this . apiKeyManager . getAPIKey ( "default" ) ;
572- if ( apiKey ) {
573- const apiKeyInput = settingsForm . querySelector ( "#api-key" ) ;
574- if ( apiKeyInput ) apiKeyInput . value = apiKey ;
575- }
576- } catch ( e ) {
577- console . error ( "getAPIKey error:" , e ) ;
578- }
579- } else {
580- const apiKeyInput = settingsForm . querySelector ( "#api-key" ) ;
581- if ( apiKeyInput ) apiKeyInput . value = "" ;
582- }
577+ if ( apiKeyInput ) apiKeyInput . value = settings . apiKey || "" ;
583578 }
584579
585580 async loadChatHistory ( app ) {
0 commit comments