diff --git a/apps/buddy/service/src/providers/ProviderExecutionModelResolver.ts b/apps/buddy/service/src/providers/ProviderExecutionModelResolver.ts index e097594d6..74079b22e 100644 --- a/apps/buddy/service/src/providers/ProviderExecutionModelResolver.ts +++ b/apps/buddy/service/src/providers/ProviderExecutionModelResolver.ts @@ -2,6 +2,7 @@ import type { Api, Model } from '@earendil-works/pi-ai' import type { ModelRuntime } from '@earendil-works/pi-coding-agent' import type { BuddyServiceTierOption } from '../../../shared/conversation/modelSelection' import type { BuiltinProviderConfigRepository } from '../storage/builtinProviderConfigRepository' +import type { ProviderConfigRepository } from '../storage/providerConfigRepository' import type { ProviderStateRepository } from '../storage/providerStateRepository' import type { ModelMetadataCatalog } from './ModelsDevCatalog' import type { ProviderCredentialStatus } from './ProviderCredentialStatus' @@ -30,6 +31,7 @@ export interface ResolvedProviderSessionModel { export interface ProviderExecutionModelResolverOptions { isPluginAvailable?: (providerId: string) => boolean builtins: Pick + configs?: Pick credentialStatus: ProviderCredentialStatus metadata: Pick modelCatalog: Pick< @@ -43,6 +45,7 @@ export interface ProviderExecutionModelResolverOptions { export class ProviderExecutionModelResolver { readonly #isPluginAvailable: NonNullable readonly #builtins: ProviderExecutionModelResolverOptions['builtins'] + readonly #configs: ProviderExecutionModelResolverOptions['configs'] readonly #credentialStatus: ProviderCredentialStatus readonly #metadata: ProviderExecutionModelResolverOptions['metadata'] readonly #modelCatalog: ProviderExecutionModelResolverOptions['modelCatalog'] @@ -52,6 +55,7 @@ export class ProviderExecutionModelResolver { constructor(options: ProviderExecutionModelResolverOptions) { this.#isPluginAvailable = options.isPluginAvailable ?? (() => false) this.#builtins = options.builtins + this.#configs = options.configs this.#credentialStatus = options.credentialStatus this.#metadata = options.metadata this.#modelCatalog = options.modelCatalog @@ -87,9 +91,9 @@ export class ProviderExecutionModelResolver { if (!this.#isPluginAvailable(input.providerId)) throw new ProviderUnavailableError() } - else { + else if (this.#configs?.findById(input.providerId)?.authentication !== 'none') { const credentials = await this.#credentialStatus.list() - if (!credentials.some(credential => credential.providerId === input.providerId)) + if (this.#configs?.findById(input.providerId)?.credentialRef === null || !credentials.some(credential => credential.providerId === input.providerId)) throw new ProviderAuthenticationRequiredError() } if (!this.#states.findByProviderId(input.providerId)?.enabled || !this.#modelCatalog.isEnabledAvailable(input.providerId, input.modelId)) diff --git a/apps/buddy/service/src/providers/ProviderModelCatalog.ts b/apps/buddy/service/src/providers/ProviderModelCatalog.ts index 09c870b5d..c57a39e2a 100644 --- a/apps/buddy/service/src/providers/ProviderModelCatalog.ts +++ b/apps/buddy/service/src/providers/ProviderModelCatalog.ts @@ -38,6 +38,7 @@ import { createSourceMetadata, emptyModelCost, nextSourceRevision, sourceMetadat import { buddyModelSchema, modelParametersOverrideSchema } from './providerSchemas' export interface ProviderRegistration { + authentication?: 'api_key' | 'none' api: string baseUrl: string models: Array<{ @@ -154,6 +155,7 @@ export class ProviderModelCatalog { registerCustomProvider(provider: ProviderConfigRecord): void { this.#modelRuntime.registerProvider(provider.id, { + authentication: provider.authentication ?? 'api_key', api: provider.api, baseUrl: provider.baseUrl, models: this.#models.list(provider.id).filter(model => effectiveType(model) === 'chat').map(model => this.#sourceModel(model)), diff --git a/apps/buddy/service/src/providers/ProviderModelDiscovery.ts b/apps/buddy/service/src/providers/ProviderModelDiscovery.ts index 68a4dc846..006598656 100644 --- a/apps/buddy/service/src/providers/ProviderModelDiscovery.ts +++ b/apps/buddy/service/src/providers/ProviderModelDiscovery.ts @@ -28,6 +28,8 @@ export interface ProviderModelDiscoveryResult { export interface ProviderModelDiscoveryInput { readonly signal?: AbortSignal + readonly authentication?: 'api_key' | 'none' + readonly credentialRef?: string | null readonly api: string readonly baseUrl: string readonly providerId: string @@ -71,8 +73,9 @@ export class OpenAiCompatibleModelDiscovery implements ProviderModelDiscovery { ): Promise { if (!this.supports(input.api)) throw new ProviderModelSyncUnsupportedError() - const credential = await this.#credentials.read(input.providerId) - if (credential?.type !== 'api_key' || !credential.key) + const credential = input.authentication === 'none' || input.credentialRef === null ? undefined : await this.#credentials.read(input.credentialRef ?? input.providerId) + const apiKey = credential?.type === 'api_key' ? credential.key : undefined + if (input.authentication !== 'none' && !apiKey) throw new ProviderAuthenticationRequiredError() const context = { ...diagnosticContext.getStore(), providerId: input.providerId, requestId: crypto.randomUUID(), component: 'runtime.providers' } @@ -81,8 +84,8 @@ export class OpenAiCompatibleModelDiscovery implements ProviderModelDiscovery { let failureStage: ProviderRequestDiagnostic['failureStage'] = 'request' this.#record({ ...context, event: 'provider.request.started', level: 'info' }) try { - const defaults = { Authorization: `Bearer ${credential.key}` } - const resolved = await this.#requestHeaders?.resolve(input.providerId, defaults, credential.key) ?? defaults + const defaults: Record = apiKey ? { Authorization: `Bearer ${apiKey}` } : {} + const resolved = this.#requestHeaders?.resolve(input.providerId, defaults, apiKey) ?? defaults const headers = new Headers() for (const [name, value] of Object.entries(resolved)) { if (value !== null) diff --git a/apps/buddy/service/src/providers/ProviderService.ts b/apps/buddy/service/src/providers/ProviderService.ts index 0d2eae12b..4d6823cf4 100644 --- a/apps/buddy/service/src/providers/ProviderService.ts +++ b/apps/buddy/service/src/providers/ProviderService.ts @@ -172,6 +172,7 @@ export class ProviderService { this.executionModels = new ProviderExecutionModelResolver({ isPluginAvailable: id => this.#plugins.has(id) && Boolean(this.#modelRuntime.getProvider(id)), builtins: options.providers.builtins, + configs: options.providers.configs, credentialStatus: options.credentialStatus, metadata: this.#modelSnapshot, modelCatalog: this.#modelCatalog, @@ -318,7 +319,8 @@ export class ProviderService { const custom = customProviders.get(id) const template = builtin ? this.#builtinTemplates.get(builtin.builtinProviderId) : undefined const state = providerStates.get(id) - const storedCredentialType = credentials.get(id) ?? null + const storedCredentialType = custom && !custom.credentialRef ? null : credentials.get(id) ?? null + const unauthenticated = custom?.authentication === 'none' const modelSummary = modelSummaries.get(id) const enabled = state?.enabled ?? custom?.enabled ?? Boolean(storedCredentialType) const enabledModelCount = modelSummary?.enabledModelCount ?? 0 @@ -334,15 +336,16 @@ export class ProviderService { baseUrl: custom?.baseUrl ?? provider?.baseUrl ?? null, canSyncModels: !plugin && Boolean(provider && syncUnavailableReason === null), authTypes: plugin ? [] : provider ? providerAuthTypes(provider) : custom ? ['api_key'] : [], + authentication: custom?.authentication ?? storedCredentialType ?? 'api_key', storedCredentialType, - status: plugin ? provider || capabilities.some(item => item.available) ? 'available' : 'unavailable' : !provider || this.#credentialStatus.availability === 'unknown' ? 'unavailable' : storedCredentialType ? 'available' : 'authentication_required', + status: plugin ? provider || capabilities.some(item => item.available) ? 'available' : 'unavailable' : !provider ? 'unavailable' : unauthenticated ? 'available' : this.#credentialStatus.availability === 'unknown' ? 'unavailable' : storedCredentialType ? 'available' : 'authentication_required', custom: Boolean(custom), activeRunCount: this.#activeRunsForProvider(id).length, added: true, enabled, enabledModelCount, modelCount: modelSummary?.modelCount ?? 0, - setupComplete: Boolean((provider || capabilities.length) && (plugin || storedCredentialType !== null)), + setupComplete: Boolean((provider || capabilities.length) && (plugin || unauthenticated || storedCredentialType !== null)), syncUnavailableReason: plugin ? 'unsupported_api' : !provider && custom ? 'unsupported_api' : syncUnavailableReason, }) }) @@ -603,9 +606,9 @@ export class ProviderService { this.#assertProviderIdle(providerId) if (enabled) { const capabilities = this.#capabilityModels.filter(item => item.providerId === providerId && item.available) - if (!this.#plugins.has(providerId) && !capabilities.some(item => item.plugin)) { + if (!this.#plugins.has(providerId) && !capabilities.some(item => item.plugin) && this.#configs.findById(providerId)?.authentication !== 'none') { const credentials = await this.#credentialStatus.list() - if (!credentials.some(credential => credential.providerId === providerId)) + if ((this.#configs.findById(providerId)?.credentialRef === null) || !credentials.some(credential => credential.providerId === providerId)) throw new ProviderAuthenticationRequiredError() } if (!this.#modelCatalog.hasEnabledAvailableModel(providerId)) @@ -689,6 +692,8 @@ export class ProviderService { const discovery = await this.#modelDiscovery.discover({ signal: this.#shutdown.signal, api: custom.api, + authentication: custom.authentication, + credentialRef: custom.credentialRef, baseUrl: custom.baseUrl, providerId, }) @@ -712,7 +717,14 @@ export class ProviderService { const handle = this.#authInteractions.beginLogin(providerId) let outcome: 'completed' | 'failed' | 'cancelled' = 'failed' try { + const custom = this.#configs.findById(providerId) await this.#modelRuntime.login(providerId, type, handle.interaction, this.#loginOptions) + if (custom) { + if (JSON.stringify(this.#configs.findById(providerId)) !== JSON.stringify(custom)) + throw new ProviderUnavailableError() + const updated = this.#state.commit('configuration', () => this.#configs.upsert({ ...custom, authentication: 'api_key', credentialRef: providerId, updatedAt: new Date().toISOString() })) + this.#applyCatalog(providerId, () => this.#modelCatalog.registerCustomProvider(updated)) + } const instance = this.#builtins.findById(providerId) if (instance) { if (provider.refreshModels) @@ -811,7 +823,12 @@ export class ProviderService { api: parsed.data.api, baseUrl: parsed.data.baseUrl, models, - credentialRef: parsed.data.id, + authentication: parsed.data.authentication, + credentialRef: existing + ? (existing.authentication === 'none' || parsed.data.authentication === 'none') && (existing.api !== parsed.data.api || existing.baseUrl !== parsed.data.baseUrl) + ? null + : existing.credentialRef + : parsed.data.id, enabled: parsed.data.enabled, createdAt: existing?.createdAt ?? now, updatedAt: now, @@ -942,7 +959,7 @@ export class ProviderService { id, enabled: this.#states.findByProviderId(id)?.enabled ?? false, presentation: { name: custom.get(id)?.displayName ?? builtin.get(id)?.displayName, description: custom.get(id)?.description, capabilities: this.#capabilityModels.filter(model => model.providerId === id).map(model => ({ modelId: model.modelId, available: model.available, plugin: model.plugin })) }, - execution: { plugin: this.#plugins.get(id), api: custom.get(id)?.api, baseUrl: custom.get(id)?.baseUrl, builtin: builtin.get(id)?.builtinProviderId, headers: this.#requestHeaders.list(id) }, + execution: { authentication: custom.get(id)?.authentication, credentialRef: custom.get(id)?.credentialRef, plugin: this.#plugins.get(id), api: custom.get(id)?.api, baseUrl: custom.get(id)?.baseUrl, builtin: builtin.get(id)?.builtinProviderId, headers: this.#requestHeaders.list(id) }, })), models: this.#modelCatalog.capture().map(({ model, execution }) => ({ model, execution: { ...execution, serviceTiers: this.executionModels.getServiceTiers({ providerId: model.providerId, modelId: model.id, api: model.api }) } })), defaultModel: (() => { @@ -1044,7 +1061,7 @@ export class ProviderService { return provider ? storedCredentialType ? null : 'authentication_required' : 'unsupported_api' if (!this.#modelDiscovery.supports(custom.api)) return 'unsupported_api' - return storedCredentialType ? null : 'authentication_required' + return custom.authentication === 'none' || storedCredentialType ? null : 'authentication_required' } async #requireListedProvider(providerId: string): Promise { diff --git a/apps/buddy/service/src/providers/createProviderCredentialAccess.ts b/apps/buddy/service/src/providers/createProviderCredentialAccess.ts new file mode 100644 index 000000000..b8d968da5 --- /dev/null +++ b/apps/buddy/service/src/providers/createProviderCredentialAccess.ts @@ -0,0 +1,16 @@ +import type { CredentialStore } from '@earendil-works/pi-ai' +import type { ProviderConfigRepository } from '../storage/providerConfigRepository' + +export function createProviderCredentialAccess(credentials: CredentialStore, configs: Pick): CredentialStore { + function canRead(providerId: string): boolean { + const config = configs.findById(providerId) + return !config || (config.authentication !== 'none' && config.credentialRef !== null) + } + + return { + read: (providerId, options) => canRead(providerId) ? credentials.read(providerId, options) : Promise.resolve(undefined), + list: async options => (await credentials.list(options)).filter(credential => canRead(credential.providerId)), + modify: (providerId, operation, options) => credentials.modify(providerId, current => operation(canRead(providerId) ? current : undefined), options), + delete: (providerId, options) => credentials.delete(providerId, options), + } +} diff --git a/apps/buddy/service/src/providers/createProviderModelRuntime.ts b/apps/buddy/service/src/providers/createProviderModelRuntime.ts index e0726e00c..739422bef 100644 --- a/apps/buddy/service/src/providers/createProviderModelRuntime.ts +++ b/apps/buddy/service/src/providers/createProviderModelRuntime.ts @@ -25,11 +25,18 @@ export function createProviderModelRuntime(runtime: ModelRuntime, headers: Provi refresh: runtime.refresh.bind(runtime), unregisterProvider: runtime.unregisterProvider.bind(runtime), registerNativeProvider: provider => runtime.registerNativeProvider(withProviderRequestHeaders(withProviderStream(provider, record), headers)), - registerProvider: (id, config) => { + registerProvider: (id, { authentication, ...config }) => { runtime.registerProvider(id, config) const provider = runtime.getProvider(id) - if (provider) - runtime.registerNativeProvider(withProviderRequestHeaders(withProviderStream(withProviderRequestContext(provider), record), headers)) + if (provider) { + const configured = authentication === 'none' + ? { + ...provider, + auth: { apiKey: { ...provider.auth.apiKey, name: provider.name, check: undefined, resolve: async () => ({ auth: {}, source: 'explicitly unauthenticated' }) } }, + } + : provider + runtime.registerNativeProvider(withProviderRequestHeaders(withProviderStream(withProviderRequestContext(configured), record), headers, authentication)) + } }, } } diff --git a/apps/buddy/service/src/providers/createProviderService.ts b/apps/buddy/service/src/providers/createProviderService.ts index eab481a29..3bcc30394 100644 --- a/apps/buddy/service/src/providers/createProviderService.ts +++ b/apps/buddy/service/src/providers/createProviderService.ts @@ -12,6 +12,7 @@ import { createProviderRepository } from '../storage/providerRepository' import { createProviderResolutionRepository } from '../storage/providerResolutionRepository' import { createWorkspaceRepository } from '../storage/workspaceRepository' import { AuthInteractionService } from './AuthInteractionService' +import { createProviderCredentialAccess } from './createProviderCredentialAccess' import { createProviderLoginOptions } from './createProviderLoginOptions' import { createProviderModelRuntime } from './createProviderModelRuntime' import { createProviderCredentialStatus } from './ProviderCredentialStatus' @@ -44,14 +45,15 @@ export async function createProviderService( await chmod(modelsPath, 0o600) const credentials = options.platform.createCredentials() + const providers = options.providers ?? createProviderRepository(options.database) + const credentialAccess = createProviderCredentialAccess(credentials, providers.configs) const modelRuntime = await ModelRuntime.create({ allowModelNetwork: false, refreshOnCreate: false, - credentials, + credentials: credentialAccess, modelsPath, modelsStorePath, }) - const providers = options.providers ?? createProviderRepository(options.database) const requestHeaders = new ProviderRequestHeaders(providers.states) const providerRuntime = createProviderModelRuntime(modelRuntime, requestHeaders, options.record) const authInteractions = new AuthInteractionService({ openExternal: options.platform.openExternal }) @@ -63,7 +65,7 @@ export async function createProviderService( credentials, credentialStatus: createProviderCredentialStatus(credentials), getActiveRuns: options.getActiveRuns, - modelDiscovery: new OpenAiCompatibleModelDiscovery({ credentials, requestHeaders, record: options.record }), + modelDiscovery: new OpenAiCompatibleModelDiscovery({ credentials: credentialAccess, requestHeaders, record: options.record }), modelRuntime: providerRuntime, requestHeaders, providers, diff --git a/apps/buddy/service/src/providers/providerSchemas.ts b/apps/buddy/service/src/providers/providerSchemas.ts index 1df73c824..df00fc72e 100644 --- a/apps/buddy/service/src/providers/providerSchemas.ts +++ b/apps/buddy/service/src/providers/providerSchemas.ts @@ -25,6 +25,7 @@ export const credentialSchema = z.union([ ]) export const buddyProviderSchema = z.object({ + authentication: z.enum(['api_key', 'oauth', 'none']).default('api_key'), plugin: z.object({ id: z.string(), managementPage: z.boolean() }).strict().optional(), requestHeaders: providerRequestHeadersSchema.default([]), activeRunCount: z.number().int().nonnegative(), diff --git a/apps/buddy/service/src/providers/withProviderRequestHeaders.ts b/apps/buddy/service/src/providers/withProviderRequestHeaders.ts index 50ff3e72c..3cedca8de 100644 --- a/apps/buddy/service/src/providers/withProviderRequestHeaders.ts +++ b/apps/buddy/service/src/providers/withProviderRequestHeaders.ts @@ -2,11 +2,16 @@ import type { Provider, ProviderRequestOptions } from '@earendil-works/pi-ai' import type { ProviderRequestHeaders } from './ProviderRequestHeaders' import { lazyStream } from '@earendil-works/pi-ai' -export function withProviderRequestHeaders(provider: Provider, headers: ProviderRequestHeaders): Provider { +export function withProviderRequestHeaders(provider: Provider, headers: ProviderRequestHeaders, authentication?: 'api_key' | 'none'): Provider { async function prepare>(options?: T): Promise { + const unauthenticated = authentication === 'none' + const apiKey = unauthenticated ? undefined : options?.apiKey + const defaults = unauthenticated ? { authorization: null, ...options?.headers } : options?.headers + const resolvedHeaders = headers.resolve(provider.id, defaults, apiKey) return { ...options, - headers: await headers.resolve(provider.id, options?.headers, options?.apiKey), + apiKey: unauthenticated ? 'unused' : apiKey, + headers: resolvedHeaders, } as T } return { diff --git a/apps/buddy/service/src/storage/__tests__/schema.spec.ts b/apps/buddy/service/src/storage/__tests__/schema.spec.ts index b16031d3c..1eb805056 100644 --- a/apps/buddy/service/src/storage/__tests__/schema.spec.ts +++ b/apps/buddy/service/src/storage/__tests__/schema.spec.ts @@ -353,7 +353,7 @@ describe('buddy schema', { timeout: MIGRATION_TEST_TIMEOUT }, () => { `) const configs = previous.prepare('SELECT * FROM builtin_provider_configs ORDER BY id').all() const tables = ['runs', 'provider_states', 'provider_model_states', 'provider_configs', 'default_model_setting'] - const before = tables.map(table => previous.prepare(`SELECT * FROM ${table}`).all()) + const before = tables.map(table => previous.prepare(`SELECT * FROM ${table}`).all().map(row => table === 'provider_configs' ? { ...row, authentication: 'api_key' } : row)) previous.close() const upgraded = openBuddyDatabase({ databasePath }) expect(upgraded.prepare('SELECT * FROM builtin_provider_configs ORDER BY id').all()).toEqual(configs.map(config => ({ @@ -507,7 +507,7 @@ describe('buddy schema', { timeout: MIGRATION_TEST_TIMEOUT }, () => { const upgraded = openBuddyDatabase({ databasePath }) expect(upgraded.prepare('SELECT * FROM provider_states ORDER BY provider_id').all()).toEqual(states.map(state => ({ ...state, request_headers_json: '[]' }))) - expect(upgraded.prepare('SELECT * FROM provider_configs').all()).toEqual(configs) + expect(upgraded.prepare('SELECT * FROM provider_configs').all()).toEqual(configs.map(config => ({ ...config, authentication: 'api_key' }))) expect(upgraded.prepare('SELECT * FROM runs').all()).toEqual(runs.map(run => ({ ...run, buddy_id: 'default', actor_snapshot_json: null, model_override_json: null, root_run_id: run.id, parent_run_id: null, execution_kind: 'root', thread_id: 'actor:["conversation-1","default"]', generation: 0, scheduling_lane: 'conversation' }))) expect(upgraded.prepare('SELECT id, builtin_provider_id, display_name FROM builtin_provider_configs').all()).toEqual([ { id: 'anthropic', builtin_provider_id: 'anthropic', display_name: null }, diff --git a/apps/buddy/service/src/storage/database.ts b/apps/buddy/service/src/storage/database.ts index 3b72276a3..9a9fe9920 100644 --- a/apps/buddy/service/src/storage/database.ts +++ b/apps/buddy/service/src/storage/database.ts @@ -9,6 +9,7 @@ import { MANAGED_FILES_INDEX_NAMES, MANAGED_FILES_TRIGGER_NAMES } from './migrat import { PRIVATE_NETWORK_TABLE_COLUMNS, PRIVATE_NETWORK_TRIGGER_NAMES } from './migrations/privateNetworkSchema' import { PRODUCT_CONTENT_REFERENCES_TRIGGER_NAMES } from './migrations/productContentReferencesSchema' import { SCOPED_QUERY_REVISION_SOURCES, SCOPED_QUERY_REVISION_TRIGGER_NAMES } from './migrations/productQueryRevisionsSchema' +import { completeProviderAuthenticationSchema } from './migrations/providerAuthenticationSchema' import { REMOTE_FILES_INDEX_NAMES, REMOTE_FILES_TABLE_COLUMNS, REMOTE_FILES_TRIGGER_NAMES } from './migrations/remoteFilesSchema' import { RESOURCE_EXPORT_INDEX_NAMES, RESOURCE_EXPORT_TABLE_COLUMNS, RESOURCE_EXPORT_TRIGGER_NAMES } from './migrations/resourceExportsSchema' import { BUDDY_V15_CAPABILITY_OVERRIDES_SCHEMA_SQL, BUDDY_V15_CATALOG_MODEL_ID_SCHEMA_SQL, BUDDY_V15_CATALOG_SELECTION_SCHEMA_SQL, BUDDY_V15_PROVIDER_INSTANCES_SCHEMA_SQL, BUDDY_V15_REQUEST_HEADERS_SCHEMA_SQL } from './migrations/v15ModelServices' @@ -240,6 +241,7 @@ function migrateBuddyDatabase(database: DatabaseSync, currentVersion: number, ta continue applyMigration(database, migration) } + completeProviderAuthenticationSchema(database) assertSchemaLineage(database, targetVersion === 42 ? BUDDY_SCHEMA_LINEAGE : BUDDY_PERSISTENCE_SCHEMA_LINEAGE) assertCurrentSchema(database) if (targetVersion >= BUDDY_MIGRATION_SCHEMA_VERSION) { diff --git a/apps/buddy/service/src/storage/migrations/providerAuthenticationSchema.ts b/apps/buddy/service/src/storage/migrations/providerAuthenticationSchema.ts new file mode 100644 index 000000000..11051863b --- /dev/null +++ b/apps/buddy/service/src/storage/migrations/providerAuthenticationSchema.ts @@ -0,0 +1,7 @@ +import type { DatabaseSync } from 'node:sqlite' + +export function completeProviderAuthenticationSchema(database: DatabaseSync): void { + const columns = database.prepare('PRAGMA table_info(provider_configs)').all() as Array<{ name: string }> + if (!columns.some(column => column.name === 'authentication')) + database.exec('ALTER TABLE provider_configs ADD COLUMN authentication TEXT NOT NULL DEFAULT \'api_key\' CHECK (authentication IN (\'api_key\', \'none\'))') +} diff --git a/apps/buddy/service/src/storage/providerConfigRepository.ts b/apps/buddy/service/src/storage/providerConfigRepository.ts index e6bbf6aae..8bead27c8 100644 --- a/apps/buddy/service/src/storage/providerConfigRepository.ts +++ b/apps/buddy/service/src/storage/providerConfigRepository.ts @@ -1,6 +1,7 @@ import type { DatabaseSync } from 'node:sqlite' export interface ProviderConfigRecord { + authentication?: 'api_key' | 'none' id: string displayName: string description: string | null @@ -21,6 +22,7 @@ export interface ProviderConfigRepository { } interface ProviderConfigRow { + authentication: 'api_key' | 'none' id: string display_name: string description: string | null @@ -42,8 +44,8 @@ export function createProviderConfigRepository( const upsert = database.prepare(` INSERT INTO provider_configs ( id, display_name, description, api, base_url, models_json, - credential_ref, enabled, created_at, updated_at - ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) + credential_ref, enabled, created_at, updated_at, authentication + ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ON CONFLICT (id) DO UPDATE SET display_name = excluded.display_name, description = excluded.description, @@ -51,6 +53,7 @@ export function createProviderConfigRepository( base_url = excluded.base_url, models_json = excluded.models_json, credential_ref = excluded.credential_ref, + authentication = excluded.authentication, enabled = excluded.enabled, updated_at = excluded.updated_at `) @@ -78,6 +81,7 @@ export function createProviderConfigRepository( Number(record.enabled), record.createdAt, record.updatedAt, + record.authentication ?? 'api_key', ) return requireProviderConfig(find.get(record.id), record.id) }, @@ -97,6 +101,7 @@ function toProviderConfig(row: ProviderConfigRow): ProviderConfigRecord { throw new Error(`Lexora Buddy provider models are invalid: ${row.id}`) return { + authentication: row.authentication, id: row.id, displayName: row.display_name, description: row.description, diff --git a/apps/buddy/shared/providers/providerApi.ts b/apps/buddy/shared/providers/providerApi.ts index e2867b905..0a36099d2 100644 --- a/apps/buddy/shared/providers/providerApi.ts +++ b/apps/buddy/shared/providers/providerApi.ts @@ -7,10 +7,11 @@ import { idSchema, timestampSchema, validationRequestSchemas, validationResponse import { modelCapabilitiesSchema, modelCapabilityOverridesSchema } from './providerCapabilities' import { modelCatalogReferenceSchema, modelCatalogResolutionSchema } from './providerCatalog' import { providerRequestHeadersSchema } from './providerHeaders' -import { customProviderInputSchema, defaultModelSchema, providerBaseUrlSchema, providerDisplayNameSchema, providerModelInputSchema } from './providerInput' +import { customProviderAuthenticationSchema, customProviderInputSchema, defaultModelSchema, providerBaseUrlSchema, providerDisplayNameSchema, providerModelInputSchema, supportsUnauthenticatedProviderApi } from './providerInput' import { providerModelTypeSchema } from './providerModelType' export const providerSchema = z.object({ + authentication: z.enum(['api_key', 'oauth', 'none']).default('api_key'), plugin: z.object({ id: z.string(), managementPage: z.boolean() }).strict().optional(), requestHeaders: providerRequestHeadersSchema.default([]), activeRunCount: z.number().int().nonnegative(), @@ -145,6 +146,7 @@ export const customProviderModelSchema = z.object({ )) export const customProviderSchema = z.object({ + authentication: customProviderAuthenticationSchema, requestHeaders: providerRequestHeadersSchema.optional(), api: z.enum([ 'anthropic-messages', @@ -165,6 +167,8 @@ export const customProviderSchema = z.object({ id: z.string().trim().regex(/^[a-z0-9][a-z0-9._-]{0,99}$/), models: z.array(customProviderModelSchema).default([]), }).strict().superRefine((provider, context) => { + if (provider.authentication === 'none' && !supportsUnauthenticatedProviderApi(provider.api)) + context.addIssue({ code: 'custom', message: 'This API requires authentication', path: ['authentication'] }) const ids = new Set() for (const [index, model] of provider.models.entries()) { if (ids.has(model.id)) { diff --git a/apps/buddy/shared/providers/providerInput.ts b/apps/buddy/shared/providers/providerInput.ts index c7d3b6c8d..23bc941e4 100644 --- a/apps/buddy/shared/providers/providerInput.ts +++ b/apps/buddy/shared/providers/providerInput.ts @@ -14,6 +14,12 @@ export const providerDisplayNameSchema = z.string().trim().min(1).max(100) export const providerBaseUrlSchema = z.url().refine(isHttpEndpointUrl) +export const customProviderAuthenticationSchema = z.enum(['api_key', 'none']).default('api_key') + +export function supportsUnauthenticatedProviderApi(api: string | null): boolean { + return api === 'openai-completions' || api === 'openai-responses' +} + const thinkingLevelMapSchema = z.partialRecord( z.enum(BUDDY_THINKING_LEVELS), z.string().nullable(), @@ -72,6 +78,7 @@ export const storedCustomProviderModelSchema = customProviderModelBaseSchema.ext }).strict().transform(withCustomModelDefaults) export const customProviderInputSchema = z.object({ + authentication: customProviderAuthenticationSchema, requestHeaders: providerRequestHeadersSchema.optional(), id: z.string().trim().min(1).max(100).regex(/^[a-z0-9][a-z0-9._-]*$/), displayName: providerDisplayNameSchema, @@ -92,6 +99,8 @@ export const customProviderInputSchema = z.object({ models: z.array(customProviderModelSchema).default([]), enabled: z.boolean().default(false), }).strict().superRefine((provider, context) => { + if (provider.authentication === 'none' && !supportsUnauthenticatedProviderApi(provider.api)) + context.addIssue({ code: 'custom', message: 'This API requires authentication', path: ['authentication'] }) const modelIds = new Set() for (const [index, model] of provider.models.entries()) { if (modelIds.has(model.id)) { diff --git a/apps/buddy/src/i18n/locales/en-US/models.ts b/apps/buddy/src/i18n/locales/en-US/models.ts index 3dacb3f31..7e56108c3 100644 --- a/apps/buddy/src/i18n/locales/en-US/models.ts +++ b/apps/buddy/src/i18n/locales/en-US/models.ts @@ -75,6 +75,9 @@ export default { 'desktop.providers.clearAuthenticationConfirmation': 'Clear authentication saved on this device.', 'desktop.providers.configureApiKey': 'Configure API key', 'desktop.providers.configureConnection': 'Configure connection', + 'desktop.providers.continueWithoutAuthentication': 'Continue without authentication', + 'desktop.providers.noAuthentication': 'No authentication', + 'desktop.providers.changeAuthentication': 'Change authentication', 'desktop.providers.configureModels': 'Configure models', 'desktop.providers.configureBuiltinModelsDescription': 'Choose and enable built-in models. Lexora Buddy maintains this catalog automatically.', 'desktop.providers.configureModelsDescription': 'Sync or add models manually, then explicitly enable the models you want to use.', @@ -83,7 +86,7 @@ export default { 'desktop.providers.connectionStep': 'Connection', 'desktop.providers.continue': 'Continue', 'desktop.providers.continueSetup': 'Continue setup', - 'desktop.providers.customDescription': 'Add a compatible API endpoint and model. Credentials are encrypted separately.', + 'desktop.providers.customDescription': 'Add a compatible API endpoint and model. Configure an API key or continue without authentication in the next step.', 'desktop.providers.customProviderDescription': 'Description', 'desktop.providers.customProviderDescriptionPlaceholder': 'Model service description', 'desktop.providers.customTitle': 'Custom provider', @@ -159,7 +162,7 @@ export default { 'desktop.providers.serviceStep': 'Service', 'desktop.providers.serviceDefaultParameters': 'Service defaults', 'desktop.providers.setupIncomplete': 'Setup incomplete', - 'desktop.providers.setupIncompleteDescription': 'Configure authentication and enable at least one model before enabling this service.', + 'desktop.providers.setupIncompleteDescription': 'Configure the connection and enable at least one model before enabling this service.', 'desktop.providers.sourceParametersUpdated': 'Model defaults updated', 'desktop.providers.sourceParametersUpdatedDescription': 'Your custom values are still active.', 'desktop.providers.snapshotRefreshPartial': 'Could not update models.dev. The previous snapshot is preserved.', diff --git a/apps/buddy/src/i18n/locales/zh-CN/models.ts b/apps/buddy/src/i18n/locales/zh-CN/models.ts index dd8778471..cbe85631b 100644 --- a/apps/buddy/src/i18n/locales/zh-CN/models.ts +++ b/apps/buddy/src/i18n/locales/zh-CN/models.ts @@ -73,6 +73,9 @@ export default { 'desktop.providers.clearAuthenticationConfirmation': '清除本机保存的认证信息。', 'desktop.providers.configureApiKey': '配置 API Key', 'desktop.providers.configureConnection': '配置连接', + 'desktop.providers.continueWithoutAuthentication': '无需认证,继续', + 'desktop.providers.noAuthentication': '无需认证', + 'desktop.providers.changeAuthentication': '修改认证', 'desktop.providers.configureModels': '配置模型', 'desktop.providers.configureBuiltinModelsDescription': '选择并启用需要使用的内置模型。模型目录由 Lexora Buddy 自动维护。', 'desktop.providers.configureModelsDescription': '同步或手动添加模型,并显式启用需要使用的模型。', @@ -81,7 +84,7 @@ export default { 'desktop.providers.connectionStep': '连接', 'desktop.providers.continue': '继续', 'desktop.providers.continueSetup': '继续配置', - 'desktop.providers.customDescription': '添加兼容 API endpoint 与模型;凭据会在下一步单独加密保存。', + 'desktop.providers.customDescription': '添加兼容 API endpoint 与模型;下一步可配置 API Key 或选择无需认证。', 'desktop.providers.customProviderDescription': '描述', 'desktop.providers.customProviderDescriptionPlaceholder': '模型服务说明', 'desktop.providers.customTitle': '自定义 Provider', @@ -157,7 +160,7 @@ export default { 'desktop.providers.serviceStep': '服务', 'desktop.providers.serviceDefaultParameters': '服务默认值', 'desktop.providers.setupIncomplete': '配置未完成', - 'desktop.providers.setupIncompleteDescription': '完成认证并至少启用一个模型后才能启用服务。', + 'desktop.providers.setupIncompleteDescription': '完成连接配置并至少启用一个模型后才能启用服务。', 'desktop.providers.sourceParametersUpdated': '模型默认参数已更新', 'desktop.providers.sourceParametersUpdatedDescription': '当前仍使用自定义值。', 'desktop.providers.snapshotRefreshPartial': 'models.dev 更新失败,已保留原快照。', diff --git a/apps/buddy/src/modules/models/state/__tests__/useModelProvidersStore.spec.ts b/apps/buddy/src/modules/models/state/__tests__/useModelProvidersStore.spec.ts index 911274685..1482be4e2 100644 --- a/apps/buddy/src/modules/models/state/__tests__/useModelProvidersStore.spec.ts +++ b/apps/buddy/src/modules/models/state/__tests__/useModelProvidersStore.spec.ts @@ -351,6 +351,7 @@ function provider( ): LocalProvider { return { activeRunCount: 0, + authentication: 'api_key', requestHeaders: [], added: true, api: null, diff --git a/apps/buddy/src/modules/models/state/useProviderSetupWizard.ts b/apps/buddy/src/modules/models/state/useProviderSetupWizard.ts index 37b281636..aa758d6ea 100644 --- a/apps/buddy/src/modules/models/state/useProviderSetupWizard.ts +++ b/apps/buddy/src/modules/models/state/useProviderSetupWizard.ts @@ -3,7 +3,7 @@ import type { ProviderRequestHeader } from '@buddy-shared/providers/providerHead import type { ModelProvidersStore } from '@/modules/models/state/typing' import { customProviderSchema } from '@buddy-shared/providers/providerApi' -import { providerDisplayNameSchema } from '@buddy-shared/providers/providerInput' +import { providerDisplayNameSchema, supportsUnauthenticatedProviderApi } from '@buddy-shared/providers/providerInput' import { computed, onScopeDispose, reactive, shallowRef, watch } from 'vue' import { desktopProviderApiOptions } from '@/modules/models/model/desktopProviderApiOptions' @@ -19,6 +19,7 @@ interface UseProviderSetupWizardOptions { providerSettings: () => ModelProvidersStore resumeProviderId: ValueRef show: WritableValueRef + startAtConnection?: ValueRef } export function useProviderSetupWizard(options: UseProviderSetupWizardOptions) { @@ -34,6 +35,7 @@ export function useProviderSetupWizard(options: UseProviderSetupWizardOptions) { const showManualModelDialog = shallowRef(false) const savingManualModel = shallowRef(false) const creatingCustom = shallowRef(false) + const savingAuthentication = shallowRef(false) const customIdConflict = shallowRef(null) const builtinDisplayName = shallowRef('') const builtinHeaders = reactive({ requestHeaders: [] as ProviderRequestHeader[] }) @@ -96,15 +98,25 @@ export function useProviderSetupWizard(options: UseProviderSetupWizardOptions) { models: [], }).success) const canLogin = computed(() => selectedProvider.value?.custom || providerDisplayNameSchema.safeParse(builtinDisplayName.value).success) - const canComplete = computed(() => ( - selectedProvider.value?.storedCredentialType !== null - && enabledModels.value.length > 0 + const canContinueWithoutAuthentication = computed(() => ( + selectedProvider.value?.custom === true + && supportsUnauthenticatedProviderApi(selectedProvider.value.api) )) + const connectionBusy = computed(() => ( + savingAuthentication.value + || providerSettings().isAuthenticating.value + || (selectedProvider.value !== null && providerSettings().mutatingProviderId.value === selectedProvider.value.id) + )) + const hasAuthentication = computed(() => ( + selectedProvider.value?.authentication === 'none' || !!selectedProvider.value?.storedCredentialType + )) + const canComplete = computed(() => hasAuthentication.value && enabledModels.value.length > 0) - watch([show, providerSettings, () => resumeProviderId.value, resumeProviderAvailable], ([visible], [wasVisible]) => { + watch([show, providerSettings, () => resumeProviderId.value, resumeProviderAvailable, () => options.startAtConnection?.value], ([visible], [wasVisible]) => { generation += 1 savingManualModel.value = false creatingCustom.value = false + savingAuthentication.value = false customIdConflict.value = null showManualModelDialog.value = false if (visible || wasVisible) @@ -126,7 +138,9 @@ export function useProviderSetupWizard(options: UseProviderSetupWizardOptions) { builtinDisplayName.value = provider.displayName builtinHeaders.requestHeaders = (provider.requestHeaders ?? []).map(header => ({ ...header })) } - resetSteps(provider.storedCredentialType ? 3 : 2) + resetSteps(hasAuthentication.value ? 3 : 2) + if (options.startAtConnection?.value) + step.value = 2 }, { flush: 'sync', immediate: true }) watch(show, (visible, wasVisible) => { @@ -186,6 +200,7 @@ export function useProviderSetupWizard(options: UseProviderSetupWizardOptions) { const requestGeneration = generation const input: LocalCustomProvider = { api: customForm.api as LocalCustomProvider['api'], + authentication: selectedProvider.value?.authentication === 'none' && supportsUnauthenticatedProviderApi(customForm.api) ? 'none' : 'api_key', baseUrl: customForm.baseUrl.trim(), description: customForm.description.trim() || undefined, displayName: customForm.displayName.trim(), @@ -222,7 +237,7 @@ export function useProviderSetupWizard(options: UseProviderSetupWizardOptions) { async function login(authType: 'api_key' | 'oauth') { const provider = selectedProvider.value - if (!provider || !canLogin.value) + if (!provider || !canLogin.value || connectionBusy.value || provider.activeRunCount > 0) return const requestGeneration = generation if (!provider.custom) { @@ -234,6 +249,33 @@ export function useProviderSetupWizard(options: UseProviderSetupWizardOptions) { advanceToStep(3) } + async function continueWithoutAuthentication() { + const provider = selectedProvider.value + if (!provider || !canContinueWithoutAuthentication.value || connectionBusy.value || provider.activeRunCount > 0) + return + const requestGeneration = generation + providerSettings().clearModelProviderError() + savingAuthentication.value = true + try { + const saved = await providerSettings().upsertCustomProvider({ + id: provider.id, + api: provider.api as LocalCustomProvider['api'], + authentication: 'none', + baseUrl: provider.baseUrl ?? '', + displayName: provider.displayName, + description: provider.description ?? undefined, + enabled: provider.enabled, + requestHeaders: (provider.requestHeaders ?? []).map(header => ({ ...header })), + }) + if (saved && requestGeneration === generation && selectedProviderId.value === provider.id) + advanceToStep(3) + } + finally { + if (requestGeneration === generation) + savingAuthentication.value = false + } + } + async function saveManualModel(model: LocalCustomProviderModel) { const provider = selectedProvider.value if (!provider || savingManualModel.value) @@ -304,7 +346,7 @@ export function useProviderSetupWizard(options: UseProviderSetupWizardOptions) { } function navigateToReachedStep(nextStep: number) { - if (nextStep < 1 || nextStep > Math.min(furthestStep.value, stepCount.value)) + if (connectionBusy.value || nextStep < 1 || nextStep > Math.min(furthestStep.value, stepCount.value)) return providerSettings().clearModelProviderError() if (nextStep === 1 && selectedProvider.value?.custom) @@ -346,6 +388,10 @@ export function useProviderSetupWizard(options: UseProviderSetupWizardOptions) { builtinDisplayName, builtinHeaders, canLogin, + canContinueWithoutAuthentication, + connectionBusy, + continueWithoutAuthentication, + savingAuthentication, addBuiltin, canComplete, canContinueCustom, diff --git a/apps/buddy/src/modules/models/widgets/model-selector/__tests__/modelSelectorRemoval.spec.ts b/apps/buddy/src/modules/models/widgets/model-selector/__tests__/modelSelectorRemoval.spec.ts index fc543dcc6..2f0a61d79 100644 --- a/apps/buddy/src/modules/models/widgets/model-selector/__tests__/modelSelectorRemoval.spec.ts +++ b/apps/buddy/src/modules/models/widgets/model-selector/__tests__/modelSelectorRemoval.spec.ts @@ -121,6 +121,7 @@ describe('model selector provider icon', () => { function provider(id: string, builtinProviderId: string | null): LocalProvider { return { activeRunCount: 0, + authentication: 'api_key', added: true, api: null, authTypes: ['api_key'], diff --git a/apps/buddy/src/modules/models/widgets/providers/DesktopModelsSettings.vue b/apps/buddy/src/modules/models/widgets/providers/DesktopModelsSettings.vue index 8b1465bb3..4326bc14f 100644 --- a/apps/buddy/src/modules/models/widgets/providers/DesktopModelsSettings.vue +++ b/apps/buddy/src/modules/models/widgets/providers/DesktopModelsSettings.vue @@ -35,8 +35,10 @@ function manageProvider(providerId: string) { emit('manageProvider', providerId) } -function providerAuthenticationLabel(type: NonNullable) { - return type === 'api_key' ? 'API Key' : 'OAuth' +function providerAuthenticationLabel(provider: LocalProvider) { + if (provider.authentication === 'none') + return t('desktop.providers.noAuthentication') + return provider.storedCredentialType === 'api_key' ? 'API Key' : 'OAuth' } @@ -68,10 +70,10 @@ function providerAuthenticationLabel(type: NonNullable - {{ providerAuthenticationLabel(provider.storedCredentialType) }} + {{ providerAuthenticationLabel(provider) }}
diff --git a/apps/buddy/src/modules/models/widgets/providers/DesktopProviderAddDialog.vue b/apps/buddy/src/modules/models/widgets/providers/DesktopProviderAddDialog.vue index afa09df79..bb80c28a0 100644 --- a/apps/buddy/src/modules/models/widgets/providers/DesktopProviderAddDialog.vue +++ b/apps/buddy/src/modules/models/widgets/providers/DesktopProviderAddDialog.vue @@ -16,6 +16,7 @@ import DesktopProviderHeadersEditor from './DesktopProviderHeadersEditor.vue' const props = defineProps<{ providerSettings: ModelProvidersStore resumeProviderId: string | null + startAtConnection?: boolean }>() const show = defineModel('show', { required: true }) const language = computed(() => props.providerSettings.language.value) @@ -27,6 +28,10 @@ const { builtinHeaders, canComplete, canLogin, + canContinueWithoutAuthentication, + connectionBusy, + continueWithoutAuthentication, + savingAuthentication, canClearVisibleModels, canSelectVisibleModels, closeDialog, @@ -61,6 +66,7 @@ const { } = useProviderSetupWizard({ providerSettings: () => props.providerSettings, resumeProviderId: toRef(props, 'resumeProviderId'), + startAtConnection: toRef(props, 'startAtConnection'), show, }) const connectionForm = useTemplateRef('connectionForm') @@ -173,7 +179,7 @@ async function connect(authType: 'api_key' | 'oauth') { v-for="authType in selectedProvider?.authTypes ?? []" :key="authType" type="primary" - :disabled="!canLogin || providerSettings.isAuthenticating.value" + :disabled="!canLogin || connectionBusy || (selectedProvider?.activeRunCount ?? 0) > 0" :loading="providerSettings.isAuthenticating.value && !providerSettings.authChallenge.value" @click="connect(authType)" > @@ -286,9 +292,17 @@ async function connect(authType: 'api_key' | 'oauth') {