Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -30,6 +31,7 @@ export interface ResolvedProviderSessionModel {
export interface ProviderExecutionModelResolverOptions {
isPluginAvailable?: (providerId: string) => boolean
builtins: Pick<BuiltinProviderConfigRepository, 'findById'>
configs?: Pick<ProviderConfigRepository, 'findById'>
credentialStatus: ProviderCredentialStatus
metadata: Pick<ModelMetadataCatalog, 'getModels'>
modelCatalog: Pick<
Expand All @@ -43,6 +45,7 @@ export interface ProviderExecutionModelResolverOptions {
export class ProviderExecutionModelResolver {
readonly #isPluginAvailable: NonNullable<ProviderExecutionModelResolverOptions['isPluginAvailable']>
readonly #builtins: ProviderExecutionModelResolverOptions['builtins']
readonly #configs: ProviderExecutionModelResolverOptions['configs']
readonly #credentialStatus: ProviderCredentialStatus
readonly #metadata: ProviderExecutionModelResolverOptions['metadata']
readonly #modelCatalog: ProviderExecutionModelResolverOptions['modelCatalog']
Expand All @@ -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
Expand Down Expand Up @@ -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))
Expand Down
2 changes: 2 additions & 0 deletions apps/buddy/service/src/providers/ProviderModelCatalog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<{
Expand Down Expand Up @@ -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)),
Expand Down
11 changes: 7 additions & 4 deletions apps/buddy/service/src/providers/ProviderModelDiscovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -71,8 +73,9 @@ export class OpenAiCompatibleModelDiscovery implements ProviderModelDiscovery {
): Promise<ProviderModelDiscoveryResult> {
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' }
Expand All @@ -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<string, string> = 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)
Expand Down
33 changes: 25 additions & 8 deletions apps/buddy/service/src/providers/ProviderService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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,
})
})
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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,
})
Expand All @@ -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)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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: (() => {
Expand Down Expand Up @@ -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<BuddyProvider> {
Expand Down
16 changes: 16 additions & 0 deletions apps/buddy/service/src/providers/createProviderCredentialAccess.ts
Original file line number Diff line number Diff line change
@@ -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<ProviderConfigRepository, 'findById'>): 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),
}
}
13 changes: 10 additions & 3 deletions apps/buddy/service/src/providers/createProviderModelRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
},
}
}
8 changes: 5 additions & 3 deletions apps/buddy/service/src/providers/createProviderService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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 })
Expand All @@ -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,
Expand Down
1 change: 1 addition & 0 deletions apps/buddy/service/src/providers/providerSchemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<T extends Pick<ProviderRequestOptions, 'headers' | 'apiKey'>>(options?: T): Promise<T> {
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 {
Expand Down
Loading
Loading