import { aiModelResource, aiProviderResource, credentialResource } from '@undefineds.co/models'; import type { EncryptedCredentialSecret } from '../credentials/KeyWrapper'; import type { CredentialVault, ProviderSecret } from '../credentials/CredentialVault'; import type { GatewayDeployment } from '../auth/InvocationTokenCodec'; import { type ProviderOfferingEndpointDescriptor, type ProviderCapabilities, type ProviderRegistry } from '../providers/ProviderRegistry'; import type { OfferingAuthorizationMethod } from '../providers/OfferingAuthorization'; import type { AuthContext } from '../../auth/AuthContext'; import type { InternalPodAccessTokenProvider } from '../pod/HostedPodDataAccess'; import { type PodBaseUrlResolver } from '../pod/PodBaseUrlResolver'; import type { AuthorizationCodeCallbackReceiver } from './LoopbackAuthorizationCallbackReceiver'; import type { AuthorizationCodeOAuthIntegration, DeviceCodeOAuthIntegration, OAuthConnectMode } from './DeviceCodeProtocol'; import type { LocalSessionImportAdapter } from './OpenAiSubscriptionSessionImportAdapter'; export { OAuthConnectCredentialStore } from './OAuthConnectAdapter'; export { OAuthIntegrationRegistry, requireTrustedOAuthIntegration, type AuthorizationCodeOAuthIntegration, type DeviceCodeOAuthIntegration, type DeviceCodeProtocolDescriptor, type OAuthConnectMode, type OAuthIntegration, } from './OAuthIntegrationRegistry'; export { OpenAiSubscriptionSessionImportAdapter, type LocalSessionImportAdapter, type LocalSessionImportResult, } from './OpenAiSubscriptionSessionImportAdapter'; export { LoopbackAuthorizationCallbackReceiver, type AuthorizationCodeCallbackReceiver, } from './LoopbackAuthorizationCallbackReceiver'; export type ConnectMode = 'browserAssistedApiKey' | OAuthConnectMode | 'connectUnsupported'; export type ConnectAttemptStatus = 'pending' | 'authorization_pending' | 'slow_down' | 'completed' | 'expired' | 'denied' | 'cancelled' | 'unsupported'; export interface ConnectBeginInput { webId: string; deployment: GatewayDeployment; provider: string; offeringId?: string; authorizationMethodId?: string; requestedMode: ConnectMode; expectedCredentialVersion?: number; auth?: AuthContext; } export interface ConnectBeginResult { mode: ConnectMode; status: ConnectAttemptStatus; provider: string; offeringId?: string; deployment: GatewayDeployment; attemptId?: string; state?: string; signature?: string; expiresAt?: string; authorizationUrl?: string; pkceChallenge?: string; deviceCode?: string; userCode?: string; verificationUri?: string; verificationUriComplete?: string; intervalSeconds?: number; apiKeyManagementSupported?: boolean; credentialId?: string; oauthCredential?: OneTimeOAuthCredential; message?: string; } export interface OneTimeOAuthCredential { accessToken: string; refreshToken: string; expiresAt?: string; scope?: string; idToken?: string; accountSubject?: string; accountLabel?: string; accountId?: string; offeringId?: string; authorizationMethodId?: string; expectedVersion?: number; } export interface CompleteApiKeyInput { webId: string; deployment: GatewayDeployment; provider: string; offeringId?: string; attemptId: string; state: string; signature: string; apiKey: string; accountLabel?: string; baseUrl?: string; auth?: AuthContext; } export interface PollDeviceInput { webId: string; deployment: GatewayDeployment; provider: string; offeringId?: string; mode?: OAuthConnectMode; attemptId: string; state: string; signature: string; auth?: AuthContext; } export interface RefreshInput { webId: string; deployment: GatewayDeployment; provider: string; offeringId?: string; mode?: OAuthConnectMode; credentialId?: string; auth?: AuthContext; } export interface CallerOwnedOAuthRefreshInput extends RefreshInput { credentialId: string; refreshToken: string; expectedVersion: number; authorizationMethodId?: string; } export interface DisconnectInput { webId: string; deployment: GatewayDeployment; provider: string; offeringId?: string; credentialId?: string; auth?: AuthContext; } export interface ConnectCredentialRecord { id: string; credentialIri: string; webId: string; provider: string; deployment: GatewayDeployment; authMode: 'apiKey' | 'deviceCodeOAuth' | 'local'; encryptedSecret: EncryptedCredentialSecret; status: 'active' | 'revoked'; accountLabel?: string; expiresAt?: Date; scopes?: string[]; expectedVersion?: number; version?: number; reauthRequired?: boolean; offeringId?: string; proxyUrl?: string; priority?: number; enabled?: boolean; health?: 'healthy' | 'reauthRequired' | 'disabled' | 'error' | 'invalid' | 'unknown'; selectedModels?: AiGatewayModelSummary[]; metadata?: Record; } export type CreateConnectCredentialRecord = Omit & { id?: string; }; export interface ProviderCredentialQuery { webId: string; provider: string; deployment: GatewayDeployment; auth?: AuthContext; } export interface PodCredentialRepository { listProviderCredentials(input: ProviderCredentialQuery): Promise; getCredentialById(input: ProviderCredentialQuery & { credentialId: string; keyVersion?: number; }): Promise; createCredential(record: CreateConnectCredentialRecord, context?: { auth?: AuthContext; }): Promise; updateCredential(input: ProviderCredentialQuery & { credentialId: string; keyVersion?: number; expectedVersion?: number; patch: Partial; }): Promise; revokeCredential(input: ProviderCredentialQuery & { credentialId: string; keyVersion?: number; expectedVersion?: number; }): Promise; getCredential?(input: { webId: string; provider: string; deployment: GatewayDeployment; auth?: AuthContext; }): Promise; getActiveCredential(input: { webId: string; provider: string; deployment: GatewayDeployment; auth?: AuthContext; }): Promise; upsertConnectedCredential(record: ConnectCredentialRecord, context?: { auth?: AuthContext; }): Promise; rewrapCredential?(input: { webId: string; deployment: GatewayDeployment; credentialId: string; keyVersion?: number; expectedVersion?: number; encryptedSecret: EncryptedCredentialSecret; auth?: AuthContext; }): Promise; markReauthRequired(input: { webId: string; provider: string; deployment: GatewayDeployment; reason: string; expectedVersion?: number; auth?: AuthContext; }): Promise; disconnect(input: DisconnectInput): Promise; } type ConnectedCredentialDb = { init?: (...resources: unknown[]) => Promise; insert(resource: typeof credentialResource): { values(value: unknown): { execute(): Promise; }; }; select(): { from(resource: typeof credentialResource | typeof aiProviderResource | typeof aiModelResource): { execute?(): Promise[]>; where(condition: unknown): { execute(): Promise[]>; }; }; }; findById(resource: typeof credentialResource | typeof aiProviderResource | typeof aiModelResource, id: string): Promise; findByIri?(resource: typeof credentialResource | typeof aiProviderResource | typeof aiModelResource, iri: string): Promise; updateById(resource: typeof credentialResource, id: string, patch: unknown): Promise; update(resource: typeof credentialResource): { set(patch: unknown): { where(condition: unknown): { returning(): { execute(): Promise[]>; }; }; }; }; }; export interface PodConnectedCredentialRepositoryOptions { internalPodAccess?: InternalPodAccessTokenProvider; podBaseUrlResolver?: PodBaseUrlResolver; providerIds?: string[]; dbFactory?: (input: { owner: string; auth?: AuthContext; fetch: typeof fetch; podUrl: string; credential?: typeof credentialResource; aiProvider?: typeof aiProviderResource; aiModel?: typeof aiModelResource; }) => Promise; } export declare class PodConnectedCredentialRepository implements PodCredentialRepository { private readonly dbFactory; private readonly internalPodAccess?; private readonly podBaseUrlResolver?; private readonly providerIds; private readonly credentialTemplate; private readonly aiProviderTemplate; constructor(options?: PodConnectedCredentialRepositoryOptions); getCredential(input: { webId: string; provider: string; deployment: GatewayDeployment; auth?: AuthContext; }): Promise; getActiveCredential(input: { webId: string; provider: string; deployment: GatewayDeployment; auth?: AuthContext; }): Promise; listCredentials(input: { webId: string; deployment: GatewayDeployment; auth?: AuthContext; }): Promise; customModels?: CustomProviderModel[]; defaultModel?: string; health?: 'healthy' | 'reauthRequired' | 'disabled' | 'error' | 'invalid' | 'unknown'; quota?: { status: 'available' | 'unsupported' | 'exhausted' | 'error'; }; encryptedSecret: EncryptedCredentialSecret; version?: number; runtimeCredential?: Record; metadata?: Record; }>>; listProviderCredentials(input: ProviderCredentialQuery): Promise; getCredentialById(input: ProviderCredentialQuery & { credentialId: string; keyVersion?: number; }): Promise; createCredential(record: CreateConnectCredentialRecord, context?: { auth?: AuthContext; }): Promise; updateCredential(input: ProviderCredentialQuery & { credentialId: string; keyVersion?: number; expectedVersion?: number; patch: Partial; }): Promise; revokeCredential(input: ProviderCredentialQuery & { credentialId: string; keyVersion?: number; expectedVersion?: number; }): Promise; private findCredentialRows; private dbForOwnerRows; private selectCredentialRows; upsertConnectedCredential(record: ConnectCredentialRecord, context?: { auth?: AuthContext; }): Promise; rewrapCredential(input: { webId: string; deployment: GatewayDeployment; credentialId: string; expectedVersion?: number; encryptedSecret: EncryptedCredentialSecret; auth?: AuthContext; }): Promise; markReauthRequired(input: { webId: string; provider: string; deployment: GatewayDeployment; reason: string; expectedVersion?: number; auth?: AuthContext; }): Promise; disconnect(input: DisconnectInput): Promise; private withSelectedModels; private dbForOwner; private resolveTrustedFetch; private wrapPodFetch; } export interface ProviderConnectAdapter { readonly provider: string; readonly offeringId?: string; readonly mode?: ConnectMode; readonly authorizationMethodId?: string; begin(input: ConnectBeginInput): Promise; status?(input: PollDeviceInput): Promise; completeApiKey?(input: CompleteApiKeyInput): Promise; pollDevice?(input: PollDeviceInput): Promise; refresh?(input: RefreshInput, current: ConnectCredentialRecord, secret: ProviderSecret): Promise; refreshCallerOwned?(input: CallerOwnedOAuthRefreshInput): Promise; disconnect?(input: DisconnectInput): Promise; cancel?(input: PollDeviceInput): Promise; } interface ConnectAttempt { id: string; provider: string; deployment: GatewayDeployment; webId: string; mode: ConnectMode; offeringId?: string; authorizationMethodId?: string; state: string; signature: string; expiresAt: Date; consumedAt?: Date; expectedCredentialVersion?: number; codeVerifier?: string; deviceCode?: string; userCode?: string; intervalSeconds?: number; currentPollIntervalSeconds?: number; nextPollAt?: Date; pollClaimedAt?: Date; lastPollStatus?: ConnectAttemptStatus; terminalStatus?: Extract; } interface PollClaimResult { attempt: ConnectAttempt; claimed: boolean; } export declare class InMemoryConnectAttemptStore { private readonly attempts; private readonly maxAttempts; create(attempt: ConnectAttempt): Promise; get(id: string, now?: Date): Promise; consume(id: string, now: Date, terminalStatus?: Extract): Promise; claimPoll(id: string, now: Date): Promise; updatePollSchedule(id: string, patch: { intervalSeconds: number; nextPollAt: Date; lastPollStatus: ConnectAttemptStatus; }): Promise; releasePollClaim(id: string): Promise; private pruneExpired; private pruneBounded; } export interface SignedConnectAttemptAdapterOptions { provider: string; attempts: InMemoryConnectAttemptStore; credentialRepository: PodCredentialRepository; vault: CredentialVault; deployment: GatewayDeployment; now?: () => Date; randomBytes?: (bytes: number) => Buffer; signingSecret: string; } declare abstract class SignedConnectAttemptAdapterBase { readonly provider: string; protected readonly attempts: InMemoryConnectAttemptStore; protected readonly credentialRepository: PodCredentialRepository; protected readonly vault: CredentialVault; protected readonly deployment: GatewayDeployment; protected readonly now: () => Date; protected readonly randomBytes: (bytes: number) => Buffer; private readonly signingSecret; protected constructor(options: SignedConnectAttemptAdapterOptions); protected createAttempt(input: ConnectBeginInput, expiresAt: Date, extra?: Partial): Promise; protected loadAttemptForStatus(input: PollDeviceInput, mode: ConnectMode): Promise; protected loadConsumableAttempt(input: PollDeviceInput, mode: ConnectMode): Promise; protected statusForAttempt(attempt: ConnectAttempt): ConnectAttemptStatus; protected assertInput(input: ConnectBeginInput, mode: ConnectMode): void; } export interface BrowserAssistedApiKeyConnectAdapterOptions extends SignedConnectAttemptAdapterOptions { consoleUrl: string; } export declare class BrowserAssistedApiKeyConnectAdapter extends SignedConnectAttemptAdapterBase implements ProviderConnectAdapter { readonly mode: ConnectMode; private readonly consoleUrl; constructor(options: BrowserAssistedApiKeyConnectAdapterOptions); begin(input: ConnectBeginInput): Promise; completeApiKey(input: CompleteApiKeyInput): Promise; status(input: PollDeviceInput): Promise; disconnect(input: DisconnectInput): Promise; } export interface DeviceCodeConnectAdapterOptions extends Omit { fetch?: typeof fetch; integration: DeviceCodeOAuthIntegration; requestTimeoutMs?: number; } export declare class DeviceCodeConnectAdapter extends SignedConnectAttemptAdapterBase { readonly offeringId: string; readonly mode: ConnectMode; readonly authorizationMethodId = "device-code"; private readonly fetchImpl; private readonly integration; private readonly clientId; private readonly protocol; private readonly oauthCredentials; private readonly requestTimeoutMs; constructor(options: DeviceCodeConnectAdapterOptions); begin(input: ConnectBeginInput): Promise; pollDevice(input: PollDeviceInput): Promise; private pendingPollStatus; status(input: PollDeviceInput): Promise; cancel(input: PollDeviceInput): Promise; refresh(input: RefreshInput, current: ConnectCredentialRecord, secret: ProviderSecret): Promise; refreshCallerOwned(input: CallerOwnedOAuthRefreshInput): Promise; disconnect(input: DisconnectInput): Promise; private nowForConsume; private updateOAuthCredential; private findOAuthCredential; private resolveTokenBody; private providerRequest; private assertOfferingInput; private assertAuthorizationMethodInput; private assertAttemptOffering; private assertStoredOffering; private isProviderOAuthCredential; } export interface AuthorizationCodeConnectAdapterOptions extends Omit { fetch?: typeof fetch; integration: AuthorizationCodeOAuthIntegration; callbackReceiver: AuthorizationCodeCallbackReceiver; requestTimeoutMs?: number; } export declare class AuthorizationCodeConnectAdapter extends SignedConnectAttemptAdapterBase { readonly offeringId: string; readonly mode: ConnectMode; readonly authorizationMethodId = "browser-oauth"; private readonly fetchImpl; private readonly integration; private readonly clientId; private readonly callbackReceiver; private readonly oauthCredentials; private readonly requestTimeoutMs; private readonly callbacks; constructor(options: AuthorizationCodeConnectAdapterOptions); begin(input: ConnectBeginInput): Promise; pollDevice(input: PollDeviceInput): Promise; status(input: PollDeviceInput): Promise; cancel(input: PollDeviceInput): Promise; refreshCallerOwned(input: CallerOwnedOAuthRefreshInput): Promise; refresh(input: RefreshInput, current: ConnectCredentialRecord, secret: ProviderSecret): Promise; disconnect(input: DisconnectInput): Promise; private authorizationUrl; private exchangeAuthorizationCode; private updateOAuthCredential; private findOAuthCredential; private providerRequest; private assertOfferingInput; private assertAuthorizationMethodInput; private assertAttemptOffering; private assertStoredOffering; private isProviderOAuthCredential; private loadAuthorizationAttempt; private closeCallback; } export declare class DeepSeekConnectAdapter implements ProviderConnectAdapter { readonly provider = "deepseek"; begin(input: ConnectBeginInput): Promise; } export interface ProviderConnectServiceOptions { registry: ProviderRegistry; adapters: ProviderConnectAdapter[]; localSessionImporters?: LocalSessionImportAdapter[]; credentialRepository?: PodCredentialRepository; vault?: CredentialVault; } export interface ProviderConnectionSummary { provider: string; status: 'connected' | 'disconnected' | 'reauthRequired'; authMode?: 'apiKey' | 'deviceCodeOAuth' | 'local'; accountLabel?: string; baseUrl?: string; proxyUrl?: string; expiresAt?: string; reauthRequired?: boolean; credentialIri?: string; version?: number; connect: { modes: string[]; configured: boolean; message?: string; }; } export interface AiProviderCredentialSummary { id: string; provider: string; offeringId: string; authMode: 'oauth' | 'deviceCode' | 'apiKey' | 'local'; label?: string; enabled: boolean; priority: number; health: 'healthy' | 'expired' | 'invalid' | 'unknown'; maskedHint?: string; baseUrl?: string; proxyUrl?: string; expiresAt?: string; version: number; quota?: unknown; } export interface AiGatewayModelSummary { id: string; provider: string; offeringId?: string; resourceId?: string; displayName?: string; custom?: boolean; inputModalities?: string[]; outputModalities?: string[]; /** Custom models declare their own tokens; catalog models carry the object flags. */ capabilities?: string[] | ProviderCapabilities; /** Token list the client reads first, so custom declarations survive normalisation. */ custom_capabilities?: string[]; modalities?: { input?: string[]; output?: string[]; }; } export interface AiProviderPoolSummary { id: string; name: string; status: 'unconfigured' | 'configured' | 'available' | 'attention' | 'unavailable'; offerings: Array<{ id: string; label: string; kind?: string; lifecycle: 'active' | 'legacy' | 'unavailable'; authModes?: string[]; authorizationMethods?: OfferingAuthorizationMethod[]; runtimeProviderIds?: string[]; productLabel: string; credentialPrefixHints: string[]; consoleUrl: string; subscriptionUrl: string; endpoints: Array<{ protocol: string; baseUrl: string; region?: string; }>; modelDiscovery: { strategy: string; path: string; endpointProtocol: string; }; quota: { strategy: string; url: string; }; usagePolicyUrl: string; region: string; }>; credentials: AiProviderCredentialSummary[]; selectedModels: AiGatewayModelSummary[]; } export interface ProviderOfferingAuthorizationMethodsSummary { provider: string; offeringId: string; authorizationMethods: OfferingAuthorizationMethod[]; endpoints?: ProviderOfferingEndpointDescriptor[]; } export interface ProviderCredentialTestModelsService { list(input: { webId: string; deployment: GatewayDeployment; provider: string; credentialIri?: string; }): Promise<{ models: Array<{ id: string; displayName?: string; capabilities?: string[]; }>; observedAt: string; }>; } export declare class ProviderConnectService { /** Cloud settings only manage providers this deployment provides. */ private requireProvidedProvider; /** * Cloud owns the endpoint: a caller may not point a provided provider at its * own base URL or proxy. Supplying the provided endpoint verbatim (what the * applet does when it fills the offering default) stays valid. */ private assertEndpointIsProvided; private providedEndpoints; private static readonly localImportLocks; private readonly registry; private readonly credentialRepository?; private readonly vault?; private readonly adapters; private readonly localSessionImporters; constructor(options: ProviderConnectServiceOptions); begin(input: ConnectBeginInput): Promise; getAuthorizationMethods(): ProviderOfferingAuthorizationMethodsSummary[]; listProviders(input: { webId: string; deployment: GatewayDeployment; auth?: AuthContext; }): Promise; listProviderCredentialPools(input: { webId: string; deployment: GatewayDeployment; auth?: AuthContext; }): Promise; createApiKeyCredential(input: { webId: string; deployment: GatewayDeployment; provider: string; offeringId?: string; apiKey: string; label?: string; baseUrl?: string; proxyUrl?: string; priority?: number; auth?: AuthContext; }): Promise; createLocalCredential(input: { webId: string; deployment: GatewayDeployment; provider: string; offeringId?: string; label?: string; baseUrl?: string; priority?: number; auth?: AuthContext; }): Promise; private importLocalCredential; private refreshImportedSecret; updateCredential(input: ProviderCredentialQuery & { credentialId: string; expectedVersion: number; patch: { label?: string; enabled?: boolean; priority?: number; baseUrl?: string; proxyUrl?: string; }; }): Promise; revokeCredential(input: ProviderCredentialQuery & { credentialId: string; }): Promise; testCredential(input: ProviderCredentialQuery & { credentialId?: string; apiKey?: string; modelsService?: ProviderCredentialTestModelsService; }): Promise<{ status: 'ok'; checkedAt: string; models: Array<{ id: string; displayName?: string; capabilities?: string[]; }>; }>; private markCredentialHealth; private requireLocalOffering; completeApiKey(input: CompleteApiKeyInput): Promise; pollDevice(input: PollDeviceInput): Promise; status(input: PollDeviceInput): Promise; refresh(input: RefreshInput): Promise; refreshCallerOwned(input: CallerOwnedOAuthRefreshInput): Promise; private refreshWithRetry; disconnect(input: DisconnectInput): Promise; cancel(input: PollDeviceInput): Promise; private runAttemptOperation; private requireOAuthRefreshAdapter; private getRefreshCredential; private requireAdapter; private requireAdapterForAttemptStatus; private requireAdapterForDisconnect; private findAdapter; private findAdapterCandidates; private requireConnectOffering; private offeringSupportsConnectMode; private authorizationMethodUnavailableReason; } export interface CustomProviderModel { id: string; displayName?: string; inputModalities?: string[]; outputModalities?: string[]; capabilities?: string[]; } export declare function customModelsFromMetadata(metadata: Record | undefined): CustomProviderModel[];