-
Notifications
You must be signed in to change notification settings - Fork 143
Expand file tree
/
Copy pathconfigDialog.ts
More file actions
263 lines (241 loc) · 7.82 KB
/
configDialog.ts
File metadata and controls
263 lines (241 loc) · 7.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*---------------------------------------------------------------------------------------------
* Copyright (C) 2026 Posit Software, PBC. All rights reserved.
* Licensed under the Elastic License 2.0. See LICENSE.txt for license information.
*--------------------------------------------------------------------------------------------*/
import * as vscode from 'vscode';
import * as positron from 'positron';
import { randomUUID } from 'crypto';
import { AuthProvider } from './authProvider';
import { PositOAuthProvider } from './positOAuthProvider';
import { log } from './log';
import { FOUNDRY_MANAGED_CREDENTIALS, hasManagedCredentials } from './managedCredentials';
export interface ConfigDialogResult {
action: string;
config: positron.ai.LanguageModelConfig;
accountId?: string;
}
export type ApiKeyValidator = (apiKey: string, config: positron.ai.LanguageModelConfig) => Promise<void>;
export type OnSaveCallback = (config: positron.ai.LanguageModelConfig) => Promise<void>;
export interface RegisterAuthProviderOptions {
validateApiKey?: ApiKeyValidator;
onSave?: OnSaveCallback;
}
export const authProviders = new Map<string, AuthProvider>();
const apiKeyValidators = new Map<string, ApiKeyValidator>();
const onSaveCallbacks = new Map<string, OnSaveCallback>();
/**
* Register an auth provider so the config dialog can store/remove
* credentials through it.
*/
export function registerAuthProvider(
providerId: string,
provider: AuthProvider,
options?: RegisterAuthProviderOptions
): void {
authProviders.set(providerId, provider);
if (options?.validateApiKey) {
apiKeyValidators.set(providerId, options.validateApiKey);
} else {
apiKeyValidators.delete(providerId);
}
if (options?.onSave) {
onSaveCallbacks.set(providerId, options.onSave);
} else {
onSaveCallbacks.delete(providerId);
}
}
/**
* Get the auth provider for a given provider ID.
* Used by the migrateApiKey command.
*/
export function getAuthProvider(
providerId: string
): AuthProvider | undefined {
return authProviders.get(providerId);
}
/**
* Enrich sources with credential state from registered auth providers.
*/
async function enrichWithCredentialState(
sources: positron.ai.LanguageModelSource[]
): Promise<positron.ai.LanguageModelSource[]> {
return Promise.all(sources.map(async (source) => {
const provider = authProviders.get(source.provider.id);
if (!provider) {
return source;
}
try {
const sessions = await provider.getSessions();
const signedIn = sessions.length > 0;
if (signedIn && source.provider.id === 'ms-foundry' && hasManagedCredentials(FOUNDRY_MANAGED_CREDENTIALS)) {
return {
...source,
signedIn,
defaults: {
...source.defaults,
autoconfigure: {
type: positron.ai.LanguageModelAutoconfigureType.Custom,
message: FOUNDRY_MANAGED_CREDENTIALS.displayName,
signedIn: true,
},
},
};
}
return { ...source, signedIn };
} catch (err) {
log.error(`Failed to check credential state for ${source.provider.id}: ${err instanceof Error ? err.message : String(err)}`);
return source;
}
}));
}
/**
* Show the language model configuration dialog. Enriches the caller-provided
* sources with credential state from this extension's auth providers, then
* delegates to the core modal.
*
* For providers with a registered auth provider, credential storage and
* removal are handled directly within this callback. For all other
* providers the action is recorded and returned so the caller can handle
* model lifecycle.
*
* Called via `vscode.commands.executeCommand('authentication.configureProviders', sources, options)`.
*/
export async function showConfigurationDialog(
sources: positron.ai.LanguageModelSource[],
options?: positron.ai.ShowLanguageModelConfigOptions
): Promise<ConfigDialogResult[]> {
const enrichedSources = await enrichWithCredentialState(sources);
log.info(`Opening config dialog with ${enrichedSources.length} source(s)`);
const results: ConfigDialogResult[] = [];
const addResult = (result: ConfigDialogResult) => {
const idx = results.findIndex(r => r.config.provider === result.config.provider);
if (idx !== -1) {
results[idx] = result;
} else {
results.push(result);
}
};
await positron.ai.showLanguageModelConfig(
enrichedSources,
async (config, action) => {
log.info(`Config dialog action: "${action}" for provider "${config.provider}"`);
const hasAuthProvider = authProviders.has(config.provider);
// applyConfig is a fallback while we transition providers to the Auth extension.
// It should eventually be removed so that the Auth extension is the single source of truth
// for all provider config actions.
const applyConfig = async () => {
await vscode.commands.executeCommand('positron-assistant.applyConfigAction', config, action, enrichedSources);
};
switch (action) {
case 'save': {
if (hasAuthProvider) {
const accountId = await handleSave(config);
addResult({ action, config, accountId });
} else {
await applyConfig();
}
break;
}
case 'delete':
if (hasAuthProvider) {
await handleDelete(config);
addResult({ action, config });
} else {
await applyConfig();
}
break;
case 'oauth-signin': {
if (hasAuthProvider) {
const accountId = await handleSave(config);
addResult({ action: 'save', config, accountId });
} else {
await applyConfig();
}
break;
}
case 'oauth-signout': {
if (hasAuthProvider) {
await handleDelete(config);
addResult({ action: 'delete', config });
} else {
await applyConfig();
}
break;
}
case 'cancel': {
const provider = authProviders.get(config.provider);
if (provider instanceof PositOAuthProvider) {
provider.cancelSignIn();
}
await applyConfig();
break;
}
default:
throw new Error(
vscode.l10n.t('Invalid action: {0}', action)
);
}
},
options
);
return results;
}
/**
* Store or resolve credentials. For providers with an API key in the
* config, validates and stores it. Otherwise resolves via createSession.
*/
async function handleSave(
config: positron.ai.LanguageModelConfig
): Promise<string> {
const provider = authProviders.get(config.provider);
if (!provider) {
throw new Error(
vscode.l10n.t('No auth provider registered for {0}', config.provider)
);
}
if (config.apiKey?.trim()) {
return handleApiKeySave(config, provider);
}
const session = await provider.createSession([], {});
return session.account.id;
}
async function handleApiKeySave(
config: positron.ai.LanguageModelConfig,
provider: AuthProvider
): Promise<string> {
const apiKey = config.apiKey?.trim();
if (!apiKey) {
throw new Error(vscode.l10n.t('API key is required'));
}
const validateApiKey = apiKeyValidators.get(config.provider);
if (validateApiKey) {
await validateApiKey(apiKey, config);
}
const onSave = onSaveCallbacks.get(config.provider);
if (onSave) {
await onSave(config);
}
// Remove existing sessions so we don't accumulate stale credentials.
const existing = await provider.getSessions();
for (const session of existing) {
await provider.removeSession(session.id);
}
const accountId = randomUUID();
log.info(`Saving credential for provider "${config.provider}", name "${config.name}" (${accountId})`);
await provider.storeKey(accountId, config.name, apiKey);
return accountId;
}
async function handleDelete(
config: positron.ai.LanguageModelConfig
): Promise<void> {
const provider = authProviders.get(config.provider);
if (!provider) {
log.warn(`handleDelete: no auth provider for "${config.provider}"`);
return;
}
const sessions = await provider.getSessions();
log.info(`Deleting ${sessions.length} session(s) for provider "${config.provider}"`);
for (const session of sessions) {
await provider.removeSession(session.id);
}
}