1- import { readFile } from "node:fs/promises"
2- import { homedir } from "node:os"
3- import { join } from "node:path"
41import { InstallationVersion } from "@opencode-ai/core/installation/version"
52import { which } from "@opencode-ai/core/util/which"
63import type { Hooks } from "@opencode-ai/plugin"
7- import type { Provider } from "@opencode-ai/sdk/v2"
84import { Schema } from "effect"
95import { OAUTH_DUMMY_KEY } from "../auth"
106import { Process } from "../util/process"
@@ -19,58 +15,16 @@ const AzureCliToken = Schema.Struct({
1915 expiresOn : Schema . optional ( Schema . NonEmptyString ) ,
2016} )
2117const decodeAzureCliToken = Schema . decodeUnknownPromise ( AzureCliToken )
22- const decodeAzureProfile = Schema . decodeUnknownPromise (
23- Schema . fromJsonString ( Schema . Struct ( { subscriptions : Schema . Array ( Schema . Unknown ) } ) ) ,
24- )
25-
26- const decodeAzureAccounts = Schema . decodeUnknownPromise (
27- Schema . Array (
28- Schema . Struct ( {
29- name : Schema . NonEmptyString ,
30- resourceGroup : Schema . NonEmptyString ,
31- } ) ,
32- ) ,
33- )
34-
35- const decodeAzureDeployments = Schema . decodeUnknownPromise (
36- Schema . Array (
37- Schema . Struct ( {
38- name : Schema . NonEmptyString ,
39- properties : Schema . Struct ( {
40- model : Schema . Struct ( {
41- name : Schema . NonEmptyString ,
42- } ) ,
43- provisioningState : Schema . NonEmptyString ,
44- } ) ,
45- } ) ,
46- ) ,
47- )
48-
4918type AzureCommand = ( args : string [ ] ) => Promise < unknown >
50- type AzureAccount = { readonly name : string ; readonly resourceGroup : string }
5119
5220export async function AzureAuthPlugin ( ) : Promise < Hooks > {
5321 const available = Boolean ( which ( "az" ) )
54- // Avoid launching Azure CLI on unrelated commands just because the executable is installed.
55- const signedIn = available
56- ? await readFile ( join ( process . env . AZURE_CONFIG_DIR ?? join ( homedir ( ) , ".azure" ) , "azureProfile.json" ) , "utf8" )
57- . then ( ( text ) => decodeAzureProfile ( text . replace ( / ^ \uFEFF / , "" ) ) )
58- . then ( ( profile ) => profile . subscriptions . length > 0 )
59- . catch ( ( ) => false )
60- : false
61- const accounts =
62- ! process . env . AZURE_RESOURCE_NAME && ! process . env . AZURE_RESOURCE_GROUP && signedIn
63- ? await runAzure ( [ "cognitiveservices" , "account" , "list" , "--output" , "json" , "--only-show-errors" ] )
64- . then ( decodeAzureAccounts )
65- . catch ( ( ) => [ ] )
66- : [ ]
67- return createAzureAuthHooks ( runAzure , fetch , accounts , available )
22+ return createAzureAuthHooks ( runAzure , fetch , available )
6823}
6924
7025export function createAzureAuthHooks (
7126 run : AzureCommand ,
7227 request : ( input : RequestInfo | URL , init ?: RequestInit ) => Promise < Response > ,
73- accounts : readonly AzureAccount [ ] ,
7428 available : boolean ,
7529) : Hooks {
7630 const tokens = new Map < string , { token : string ; expires : number } > ( )
@@ -97,46 +51,7 @@ export function createAzureAuthHooks(
9751 placeholder : "e.g. my-models" ,
9852 } )
9953 }
100- const oauthPrompts =
101- accounts . length > 0 && ! process . env . AZURE_RESOURCE_NAME
102- ? [
103- {
104- type : "select" as const ,
105- key : "resourceSelection" ,
106- message : "Select Azure resource" ,
107- options : [
108- ...accounts . map ( ( account ) => ( {
109- label : account . name ,
110- value : account . name ,
111- hint : account . resourceGroup ,
112- } ) ) ,
113- { label : "Enter another resource name" , value : "__manual__" } ,
114- ] ,
115- } ,
116- {
117- type : "text" as const ,
118- key : "resourceName" ,
119- message : "Enter Azure Resource Name" ,
120- placeholder : "e.g. my-models" ,
121- when : { key : "resourceSelection" , op : "eq" as const , value : "__manual__" } ,
122- } ,
123- ]
124- : prompts
125-
12654 const hooks : Hooks = {
127- provider : {
128- id : "azure" ,
129- async models ( provider , context ) {
130- if ( context . auth ?. type !== "oauth" ) return provider . models
131- // Discovery shells out to the Azure CLI, so skip it when the CLI is missing.
132- if ( ! available ) return provider . models
133- const resource = context . auth . accountId
134- if ( ! resource ) return { }
135- // This hook runs outside the app's Effect runtime, so logging here would go to the
136- // console. Fall back to the configured models silently.
137- return discoverAzureModels ( provider . models , resource , run ) . catch ( ( ) => provider . models )
138- } ,
139- } ,
14055 auth : {
14156 provider : "azure" ,
14257 async loader ( getAuth ) {
@@ -164,17 +79,14 @@ export function createAzureAuthHooks(
16479 {
16580 type : "oauth" ,
16681 label : "Microsoft Entra ID (Azure CLI)" ,
167- prompts : oauthPrompts ,
82+ prompts,
16883 async authorize ( inputs ) {
16984 return {
17085 url : "" ,
17186 instructions : "Sign in with `az login` before continuing." ,
17287 method : "auto" ,
17388 callback : async ( ) => {
174- const resourceName =
175- inputs ?. resourceName ??
176- ( inputs ?. resourceSelection === "__manual__" ? undefined : inputs ?. resourceSelection ) ??
177- process . env . AZURE_RESOURCE_NAME
89+ const resourceName = inputs ?. resourceName ?? process . env . AZURE_RESOURCE_NAME
17890 if ( ! resourceName ) throw new Error ( "Azure Resource Name is required" )
17991
18092 await token ( AZURE_COGNITIVE_SERVICES_SCOPE )
@@ -201,53 +113,6 @@ async function runAzure(args: string[]): Promise<unknown> {
201113 return JSON . parse ( result . stdout . toString ( ) )
202114}
203115
204- async function discoverAzureModels ( models : Provider [ "models" ] , resourceName : string , run : AzureCommand ) {
205- const resourceGroup = process . env . AZURE_RESOURCE_GROUP
206- const account = resourceGroup
207- ? { name : resourceName , resourceGroup }
208- : (
209- await decodeAzureAccounts (
210- await run ( [ "cognitiveservices" , "account" , "list" , "--output" , "json" , "--only-show-errors" ] ) ,
211- )
212- ) . find ( ( account ) => account . name . toLowerCase ( ) === resourceName . toLowerCase ( ) )
213- if ( ! account ) throw new Error ( `Azure resource "${ resourceName } " was not found in the active subscription` )
214-
215- const deployments = await decodeAzureDeployments (
216- await run ( [
217- "cognitiveservices" ,
218- "account" ,
219- "deployment" ,
220- "list" ,
221- "--name" ,
222- account . name ,
223- "--resource-group" ,
224- account . resourceGroup ,
225- "--output" ,
226- "json" ,
227- "--only-show-errors" ,
228- ] ) ,
229- )
230- const found = new Map < string , Provider [ "models" ] [ string ] > ( )
231- deployments . forEach ( ( deployment ) => {
232- if ( deployment . properties . provisioningState !== "Succeeded" ) return
233- const modelID = Object . keys ( models ) . find (
234- ( modelID ) => modelID . toLowerCase ( ) === deployment . properties . model . name . toLowerCase ( ) ,
235- )
236- if ( ! modelID ) return
237- const id = found . has ( modelID ) ? deployment . name : modelID
238- found . set ( id , {
239- ...models [ modelID ] ,
240- id,
241- name : id === modelID ? models [ modelID ] . name : `${ models [ modelID ] . name } (${ deployment . name } )` ,
242- api : {
243- ...models [ modelID ] . api ,
244- id : deployment . name ,
245- } ,
246- } )
247- } )
248- return Object . fromEntries ( found )
249- }
250-
251116function scopeForRequest ( input : RequestInfo | URL ) {
252117 const url = new URL ( input instanceof Request ? input . url : input )
253118 if ( url . hostname . endsWith ( ".services.ai.azure.com" ) && ! url . pathname . startsWith ( "/models" ) ) {
0 commit comments