Skip to content

Commit

Permalink
Refactor authentication and connection handling for Azure integration
Browse files Browse the repository at this point in the history
  • Loading branch information
pelikhan committed Sep 5, 2024
1 parent 4c3acb7 commit 3e09693
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 100 deletions.
5 changes: 5 additions & 0 deletions packages/cli/src/nodehost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,11 @@ export class NodeHost implements RuntimeHost {
if (!this._azureToken) throw new Error("Azure token not available")
tok.token = "Bearer " + this._azureToken.token
}
if (!tok) {
const { provider } = parseModelIdentifier(modelId)
if (provider === MODEL_PROVIDER_AZURE)
throw new Error("Azure end point not configured")
}

Check failure on line 182 in packages/cli/src/nodehost.ts

View workflow job for this annotation

GitHub Actions / build

The error "Azure end point not configured" is thrown but not caught. This could lead to unexpected application termination. Consider handling this error to improve the application's robustness. 🛠️
if (!tok && this.clientLanguageModel) {
return <LanguageModelConfiguration>{
model: modelId,
Expand Down
19 changes: 3 additions & 16 deletions packages/core/src/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,20 +363,7 @@ export async function updateConnectionConfiguration(
}

// update .env
const { config, model } = dotEnvTemplate(provider, apiType)
let src = config
const current = await tryReadText(DOT_ENV_FILENAME)
if (current) {
if (!current.includes("GENAISCRIPT_DEFAULT_MODEL"))
src =
dedent`
## GenAIScript defaults
GENAISCRIPT_DEFAULT_MODEL="${model}"
# GENAISCRIPT_DEFAULT_TEMPERATURE=${DEFAULT_TEMPERATURE}
` + src
src = current + "\n" + src
}
await writeText(DOT_ENV_FILENAME, src)
// const { config } = dotEnvTemplate(provider, apiType)
// const current = await tryReadText(DOT_ENV_FILENAME)
//await writeText(DOT_ENV_FILENAME, config)

Check failure on line 368 in packages/core/src/connection.ts

View workflow job for this annotation

GitHub Actions / build

There is a block of code that has been commented out. If this code is not needed, it is better to remove it to keep the codebase clean and maintainable. 🧹
}
53 changes: 0 additions & 53 deletions packages/vscode/src/azuremanager.ts

This file was deleted.

7 changes: 3 additions & 4 deletions packages/vscode/src/lmaccess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
MODEL_PROVIDER_OPENAI,
MODEL_PROVIDER_CLIENT,
MODEL_PROVIDER_GITHUB,
TOOL_NAME,
} from "../../core/src/constants"
import { APIType } from "../../core/src/host"
import { parseModelIdentifier } from "../../core/src/models"
Expand Down Expand Up @@ -143,10 +144,8 @@ export async function pickLanguageModel(

if (res.model) return res.model
else {
await vscode.commands.executeCommand(
"genaiscript.connection.configure",
res.provider,
res.apiType
vscode.window.showWarningMessage(
`${TOOL_NAME} - model connection not configured.`
)

Check failure on line 149 in packages/vscode/src/lmaccess.ts

View workflow job for this annotation

GitHub Actions / build

The function `vscode.window.showWarningMessage` is called without error handling. If the function fails, the error will not be caught, which could lead to unexpected behavior. Consider adding error handling. 🚀
return undefined
}
Expand Down
29 changes: 2 additions & 27 deletions packages/vscode/src/vshost.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
import * as vscode from "vscode"
import { createVSPath } from "./vspath"
import { TerminalServerManager } from "./servermanager"
import { AzureManager } from "./azuremanager"
import { Uri } from "vscode"
import { ExtensionState } from "./state"
import { Utils } from "vscode-uri"
import { checkFileExists, readFileText } from "./fs"
import { filterGitIgnore } from "../../core/src/gitignore"
import {
parseDefaultsFromEnv,
parseTokenFromEnv,
} from "../../core/src/connection"
import { parseDefaultsFromEnv } from "../../core/src/connection"
import {
DEFAULT_EMBEDDINGS_MODEL,
DEFAULT_MODEL,
DEFAULT_TEMPERATURE,
DOT_ENV_FILENAME,
MODEL_PROVIDER_AZURE,
} from "../../core/src/constants"
import { dotEnvTryParse } from "../../core/src/dotenv"
import {
Expand All @@ -26,15 +21,14 @@ import {
Host,
} from "../../core/src/host"
import { TraceOptions, AbortSignalOptions } from "../../core/src/trace"
import { arrayify, logVerbose, unique } from "../../core/src/util"
import { arrayify, unique } from "../../core/src/util"
import { LanguageModel } from "../../core/src/chat"

export class VSCodeHost extends EventTarget implements Host {
dotEnvPath: string = DOT_ENV_FILENAME
userState: any = {}
readonly path = createVSPath()
readonly server: TerminalServerManager
private _azure: AzureManager
readonly defaultModelOptions = {
model: DEFAULT_MODEL,
temperature: DEFAULT_TEMPERATURE,
Expand All @@ -56,11 +50,6 @@ export class VSCodeHost extends EventTarget implements Host {
await parseDefaultsFromEnv(env)
}

get azure() {
if (!this._azure) this._azure = new AzureManager(this.state)
return this._azure
}

get context() {
return this.state.context
}
Expand Down Expand Up @@ -198,20 +187,6 @@ export class VSCodeHost extends EventTarget implements Host {
modelId,
options
)
const { token: askToken } = options || {}
if (
askToken &&
tok &&
!tok.token &&
tok.provider === MODEL_PROVIDER_AZURE
) {
const azureToken = await this.azure.getOpenAIToken()
if (!azureToken) throw new Error("Azure token not available")
tok.token = "Bearer " + azureToken
tok.curlHeaders = {
Authorization: "Bearer ***",
}
}
return tok
}

Expand Down

0 comments on commit 3e09693

Please sign in to comment.