@@ -13,7 +13,15 @@ import { Logger } from "../common/log-util"
1313import { workspace } from "vscode"
1414import { AxiosError } from "axios"
1515import { createAuthenticatedHeaders } from "../common/api"
16- import { configCompletion , settings , OPENAI_CLIENT_NOT_INITIALIZED } from "../common/constant"
16+ import {
17+ configCompletion ,
18+ settings ,
19+ OPENAI_CLIENT_NOT_INITIALIZED ,
20+ NOT_PROVIDERED ,
21+ ZGSM_API_KEY ,
22+ ZGSM_BASE_URL ,
23+ ZGSM_COMPLETION_URL ,
24+ } from "../common/constant"
1725import { CompletionPoint } from "./completionPoint"
1826import { CompletionScores } from "./completionScore"
1927import { CompletionTrace } from "./completionTrace"
@@ -32,6 +40,28 @@ export class CompletionClient {
3240 private reqs : Map < string , any > = new Map < string , any > ( )
3341 private betaMode ?: any
3442
43+ private async getApiConfig ( hasView : boolean , apiConfiguration : any ) {
44+ if ( hasView ) {
45+ return {
46+ baseUrl : apiConfiguration . zgsmBaseUrl || defaultZgsmAuthConfig . baseUrl ,
47+ completionUrl : apiConfiguration . zgsmCompletionUrl || defaultZgsmAuthConfig . completionUrl ,
48+ apiKey : apiConfiguration . zgsmApiKey || NOT_PROVIDERED ,
49+ }
50+ }
51+
52+ const context = CompletionClient . providerRef . deref ( ) ?. contextProxy
53+
54+ const contextApiKey = await context ?. getOriginSecrets ( ZGSM_API_KEY )
55+ const contextBaseUrl = await context ?. getGlobalState ( ZGSM_BASE_URL )
56+ const contextCompletionUrl = await context ?. getGlobalState ( ZGSM_COMPLETION_URL )
57+
58+ return {
59+ baseUrl : contextBaseUrl || defaultZgsmAuthConfig . baseUrl ,
60+ completionUrl : contextCompletionUrl || defaultZgsmAuthConfig . completionUrl ,
61+ apiKey : contextApiKey || NOT_PROVIDERED ,
62+ }
63+ }
64+
3565 public static async setProvider ( provider : ClineProvider ) {
3666 CompletionClient . providerRef = new WeakRef ( provider )
3767
@@ -119,14 +149,24 @@ export class CompletionClient {
119149 const provider = CompletionClient . providerRef . deref ( )
120150
121151 const { apiConfiguration } = await provider ! . getState ( )
122- if ( ! apiConfiguration ?. zgsmApiKey ) {
152+
153+ const hasView = ! ! provider ?. hasView
154+
155+ if ( ! apiConfiguration ?. zgsmApiKey && hasView ) {
123156 Logger . error ( "Failed to get login information. Please log in again to use the completion service" )
124157 return false
125158 }
126- const completionUrl = `${ apiConfiguration . zgsmBaseUrl || defaultZgsmAuthConfig . baseUrl } ${ apiConfiguration . zgsmCompletionUrl || defaultZgsmAuthConfig . completionUrl } `
159+
160+ const config = await this . getApiConfig ( hasView , apiConfiguration )
161+ const fullUrl = `${ config . baseUrl } ${ config . completionUrl } `
162+
163+ if ( config . apiKey === NOT_PROVIDERED ) {
164+ return false
165+ }
166+
127167 this . openai = new OpenAI ( {
128- baseURL : completionUrl ,
129- apiKey : apiConfiguration . zgsmApiKey || "not-provided" ,
168+ baseURL : fullUrl ,
169+ apiKey : config . apiKey ,
130170 } )
131171 if ( ! this . openai ) {
132172 // Logger.error("Completion: Configuration error: configuration:", configuration, "openai: ", this.openai);
@@ -136,7 +176,7 @@ export class CompletionClient {
136176 this . stopWords = workspace . getConfiguration ( configCompletion ) . get ( "inlineCompletion" ) ? [ "\n" , "\r" ] : [ ]
137177 this . betaMode = workspace . getConfiguration ( configCompletion ) . get ( "betaMode" )
138178 Logger . info (
139- `Completion: Create OpenAIApi client, URL: ${ completionUrl } , betaMode: ${ this . betaMode } , stopWords: ${ this . stopWords } ` ,
179+ `Completion: Create OpenAIApi client, URL: ${ fullUrl } , betaMode: ${ this . betaMode } , stopWords: ${ this . stopWords } ` ,
140180 )
141181 return true
142182 }
@@ -209,8 +249,12 @@ export class CompletionClient {
209249 Logger . log ( `Completion [${ cp . id } ]: Sending API request` )
210250 const headers = createAuthenticatedHeaders ( )
211251 const repo = workspace ?. name ?. split ( " " ) [ 0 ] ?? ""
212- this . openai . baseURL = `${ apiConfiguration . zgsmBaseUrl || defaultZgsmAuthConfig . baseUrl } ${ apiConfiguration . zgsmCompletionUrl || defaultZgsmAuthConfig . completionUrl } `
213- this . openai . apiKey = apiConfiguration . zgsmApiKey || "not-provided"
252+
253+ const config = await this . getApiConfig ( ! ! provider ?. hasView , apiConfiguration )
254+
255+ this . openai . baseURL = `${ config . baseUrl } ${ config . completionUrl } `
256+ this . openai . apiKey = config . apiKey
257+
214258 return this . openai . completions . create (
215259 {
216260 // no use
0 commit comments