import { type AzureEntraProviderCredential, type Settings } from "@opengeni/config"; import { ClaudeProviderAccountAuthoritySnapshotV1 } from "@opengeni/contracts"; import { type Database } from "./database.js"; import * as schema from "./schema.js"; import type { ConnectedModelListPricing, OrganizationModelProviderAuthMethod } from "./schema.js"; export type { ConnectedModelListPricing, OrganizationModelProviderAuthMethod } from "./schema.js"; /** Providers whose organization key runs models, with custom models to choose. */ export type OrganizationModelCatalogProviderKind = "vercel_gateway" | "openrouter" | "anthropic" | "claude_subscription" | "opper" | "openai" | "azure_openai"; /** Every organization provider key, including ElevenLabs for realtime voice. */ export type OrganizationModelProviderKind = OrganizationModelCatalogProviderKind | "elevenlabs"; export type OrganizationModelProviderConnection = { providerKind: OrganizationModelProviderKind; status: "active" | "revoked"; version: number; /** Azure OpenAI only: the non-secret resource endpoint; null for every other kind. */ endpoint: string | null; /** How the connection signs in; Microsoft Entra ID methods are Azure OpenAI only. */ authMethod: OrganizationModelProviderAuthMethod; createdAt: Date; updatedAt: Date; }; /** * What an organization connection signs in with. An API key is stored as the * key itself, so every existing row reads unchanged; a Microsoft Entra ID * method is stored as a small versioned JSON document under the same * encryption (the app's client secret is the only secret in it). */ export type OrganizationModelProviderCredential = { method: "api_key"; apiKey: string; } | { method: "entra_client_secret"; tenantId: string; clientId: string; clientSecret: string; } | { method: "entra_deployment_identity"; }; /** The decrypted credential of an active Azure OpenAI or OpenAI connection, with its endpoint. */ export type OrganizationDirectProviderCredential = { apiKey: string; endpoint: string | null; } | { azureEntra: AzureEntraProviderCredential; endpoint: string | null; }; /** The plaintext stored (encrypted) for a credential, and its non-secret method. */ export declare function organizationModelProviderCredentialPlaintext(credential: OrganizationModelProviderCredential): { authMethod: OrganizationModelProviderAuthMethod; plaintext: string; }; export type OrganizationModelProviderCustomModel = { id: string; accountId: string; providerKind: OrganizationModelCatalogProviderKind; upstreamModelId: string; label: string | null; /** OpenAI and Azure OpenAI only: the catalogue model whose list rate prices it. */ listPricingModelId: string | null; /** OpenAI and Azure OpenAI only: the administrator's own list rate. */ listPricing: ConnectedModelListPricing | null; version: number; retiredAt: Date | null; createdAt: Date; updatedAt: Date; }; /** * What a connected model's usage costs: the catalogue model it is priced * from (an Azure deployment's base model), its own rate, or unknown (null). */ export type ConnectedModelListPrice = { basis: "model"; modelId: string; } | { basis: "custom"; pricing: ConnectedModelListPricing; } | null; /** The stored columns of a {@link ConnectedModelListPrice}. */ export declare function connectedModelListPriceColumns(price: ConnectedModelListPrice): { priceBasisModelId: string | null; listPricing: ConnectedModelListPricing | null; }; /** Exact accepted source; never resolve the caller's current pool at this fence. */ export type OrganizationClaudeModelAdmissionAuthority = { sessionId: string; } | { authoritySnapshot: ClaudeProviderAccountAuthoritySnapshotV1; /** * The scheduled task an update re-admits. On Claude's core route its * accepted authority (the task's compatibility record) decides instead * of the v1 snapshot; ignored on the legacy route. */ scheduledTaskId?: string; /** * The accepted source a fresh prompt copies: an edit's source turn, or * the receiving session of agent-submitted work. On Claude's core * route its accepted authority decides; ignored on the legacy route * (the snapshot already carries it). Absent for a human's own new work * and for service actors, which resolve the current source. */ acceptedSource?: { kind: "session_turn"; turnId: string; } | { kind: "receiving_session"; sessionId: string; }; }; export declare class OrganizationModelProviderConflictError extends Error { } export declare class OrganizationModelProviderLimitError extends Error { } export declare function organizationModelProviderCredentialDigest(credential: string): string; /** Runs `use` as an organization administrator (refused otherwise), in the organization's scope. */ export declare function withOrganizationProviderAdministrator(db: Database, input: { organizationId: string; actorSubjectId: string; }, use: (scopedDb: Database) => Promise): Promise; export declare function mapOrganizationModelProviderCustomModel(row: typeof schema.organizationModelProviderCustomModels.$inferSelect): OrganizationModelProviderCustomModel; export declare function getOrganizationModelProviderConnection(db: Database, input: { organizationId: string; actorSubjectId: string; providerKind: OrganizationModelProviderKind; }): Promise; export declare function upsertOrganizationModelProviderConnection(db: Database, input: { organizationId: string; actorSubjectId: string; providerKind: OrganizationModelProviderKind; credentialEncrypted: string; credentialDigest: string; /** * Required for `azure_openai` and refused for every other kind: the * normalized non-secret resource endpoint. The request hash binds it with * the key digest, so a replay naming another endpoint conflicts and an * endpoint change is a new connection version. */ endpoint?: string | null; /** * How the stored credential signs in; defaults to an API key. Microsoft * Entra ID methods are refused for every kind but `azure_openai`. A * non-default method is bound into the request hash. */ authMethod?: OrganizationModelProviderAuthMethod; /** * OpenAI and Azure OpenAI only: models (Azure deployment names) the caller * already checked with this credential, saved with the connection in the * same transaction. One the organization already offers is kept as it is. * Bound into the request hash when present. */ models?: readonly string[]; /** * The price the server detected for a new model in `models` while * checking it (Azure's reported base model). Not part of the request * identity; a model the organization already offers keeps its own. */ modelListPrices?: Readonly>; operationId: string; expectedVersion?: number; }): Promise; export declare function revokeOrganizationModelProviderConnection(db: Database, input: { organizationId: string; actorSubjectId: string; providerKind: OrganizationModelProviderKind; operationId: string; expectedVersion: number; }): Promise; export declare function organizationModelProviderConnectionActiveForWorkspace(db: Database, input: { accountId: string; workspaceId: string; providerKind: OrganizationModelProviderKind; }): Promise; export declare function loadOrganizationModelProviderApiKey(db: Database, settings: Settings, input: { accountId: string; workspaceId: string; providerKind: OrganizationModelProviderKind; }, prepared?: { loadOrganizationCredentials: boolean; encryptedCredentials: Partial>; organizationAuthMethods: Partial>; }): Promise; /** Execution metadata only, under the same tenant scope as the credential loader. */ export declare function getOrganizationModelProviderExecutionConnectionId(db: Database, input: { accountId: string; workspaceId: string; providerKind: OrganizationModelProviderKind; }): Promise; /** * The decrypted key (or Azure OpenAI's Microsoft Entra ID sign-in) and * non-secret endpoint of an active organization connection, read under the * workspace's RLS scope (the connection's access policy decides which of the * organization's shared and Personal workspaces see it). * Null when there is no active connection or no encryption key; callers then * fail closed and never fall back to deployment credentials. */ export declare function loadOrganizationModelProviderCredential(db: Database, settings: Settings, input: { accountId: string; workspaceId: string; providerKind: "openai" | "azure_openai"; }, prepared?: { loadOrganizationCredentials: boolean; encryptedCredentials: Partial>; organizationEndpoints: Partial>; /** Present for an Azure OpenAI connection that signs in with Microsoft Entra ID. */ organizationAuthMethods: Partial>; }): Promise; /** * The administrator's view of an active organization connection credential, * for a setup check (Azure deployment verification) before a custom model is * saved. Never returned to a client. */ export declare function loadOrganizationModelProviderCredentialForAdministrator(db: Database, settings: Settings, input: { organizationId: string; actorSubjectId: string; providerKind: OrganizationModelProviderKind; }): Promise; export declare function listOrganizationModelProviderCustomModelsForWorkspace(db: Database, input: { accountId: string; workspaceId: string; providerKind: OrganizationModelCatalogProviderKind; includeRetired?: boolean; }): Promise; export type OrganizationModelProviderCatalogEntry = { active: boolean; models: OrganizationModelProviderCustomModel[]; /** Azure OpenAI only: the connection's non-secret endpoint (null when never connected). */ endpoint?: string | null; }; /** Read provider readiness and active model definitions without credential material. */ export declare function getOrganizationModelProviderCatalogForWorkspace(db: Database, input: { accountId: string; workspaceId: string; providerKinds: readonly OrganizationModelCatalogProviderKind[]; }): Promise>; /** Internal reader; the caller owns the workspace's tenant scope and shared fence. */ export declare function getOrganizationModelProviderCatalogInScope(scopedDb: Database, input: { accountId: string; workspaceId: string; providerKinds: readonly OrganizationModelCatalogProviderKind[]; }): Promise>; export declare function getOrganizationModelProviderCustomModelForExecution(db: Database, input: { accountId: string; workspaceId: string; providerKind: OrganizationModelCatalogProviderKind; upstreamModelId: string; }): Promise; export declare function withOrganizationModelProviderCustomModelReadLock(db: Database, input: { accountId: string; workspaceId: string; providerKind: OrganizationModelCatalogProviderKind; }, use: (scopedDb: Database) => Promise): Promise; export declare function lockActiveOrganizationModelProviderCustomModelForAdmission(db: Database, input: { accountId: string; workspaceId: string; providerKind: OrganizationModelCatalogProviderKind; upstreamModelId: string; claudeAuthority?: OrganizationClaudeModelAdmissionAuthority; }): Promise; export declare function listOrganizationModelProviderCustomModels(db: Database, input: { organizationId: string; actorSubjectId: string; providerKind: OrganizationModelCatalogProviderKind; }): Promise; /** * First connection of a Claude account for the organization: offer the * default models. Does nothing once the organization has any Claude model * record of this kind, active or removed, so a configured or trimmed list is * never changed. Returns how many models were added. */ export declare function seedOrganizationClaudeDefaultModels(db: Database, input: { organizationId: string; actorSubjectId: string; providerKind: "anthropic" | "claude_subscription"; }): Promise; export declare function createOrganizationModelProviderCustomModel(db: Database, input: { organizationId: string; actorSubjectId: string; providerKind: OrganizationModelCatalogProviderKind; upstreamModelId: string; label?: string; operationId: string; /** * OpenAI and Azure OpenAI only: the price the server detected while * checking the model (Azure's reported base model). Not part of the * request identity, so a replay returns the first result. */ listPrice?: ConnectedModelListPrice; }): Promise; export declare function retireOrganizationModelProviderCustomModel(db: Database, input: { organizationId: string; actorSubjectId: string; providerKind: OrganizationModelCatalogProviderKind; customModelId: string; expectedVersion: number; operationId: string; }): Promise; type CustomModelRow = typeof schema.organizationModelProviderCustomModels.$inferSelect; /** * Set what an organization OpenAI, Azure OpenAI or OpenAI-compatible model's * usage costs, inside the administrator's scope. The version guards * concurrent edits; repeating the change that produced the current version * returns the model unchanged. A missing or retired model is a conflict. */ export declare function setOrganizationCustomModelListPriceInScope(scopedDb: Database, input: { organizationId: string; providerKind: "openai" | "azure_openai" | "openai_compatible"; /** OpenAI-compatible models only: the endpoint the model belongs to. */ connectionId?: string; customModelId: string; expectedVersion: number; price: ConnectedModelListPrice; }): Promise; /** * Whether two stored rates are the same rate, field by field: jsonb keeps * its own key order, so a serialized comparison would miss a repeat. */ export declare function sameListPricing(left: ConnectedModelListPricing | null, right: ConnectedModelListPricing | null): boolean; /** {@link setOrganizationCustomModelListPriceInScope} for an OpenAI model or Azure deployment. */ export declare function setOrganizationModelProviderCustomModelListPrice(db: Database, input: { organizationId: string; actorSubjectId: string; providerKind: "openai" | "azure_openai"; customModelId: string; expectedVersion: number; price: ConnectedModelListPrice; }): Promise;