import { type ApplyOrgAgentResponse, type CreateOrgToolServerRequest, type EndUserToolCredential, type OrgAgent, type OrgAgentDefinitionInput, type OrgAgentErrorCode, type OrgAgentManagedBy, type OrgAgentPublishOperation, type OrgAgentSessionEntry, type OrgAgentSummary, type OrgAgentToolCatalog as OrgAgentToolCatalogValue, type OrgAgentVersion, type OrgAgentUsageResponse, type OrgToolServer, type PutEndUserToolCredentialRequest, type UpdateOrgToolServerRequest, type WorkspaceOrgAgent } from "@opengeni/contracts"; import { type Database } from "./database.js"; export type OrgAgentErrorStatus = 403 | 404 | 409 | 422 | 429 | 503; /** A refusal the API renders with `details.code` = {@link code}. */ export declare class OrgAgentError extends Error { readonly status: OrgAgentErrorStatus; readonly code: OrgAgentErrorCode; readonly details: Record; readonly name = "OrgAgentError"; constructor(status: OrgAgentErrorStatus, code: OrgAgentErrorCode, message: string, details?: Record); } /** A secret must be stored but no encryption key is configured. */ export declare class OrgAgentEncryptionUnavailableError extends Error { readonly name = "OrgAgentEncryptionUnavailableError"; constructor(); } /** Who made a change, for the Console audit log (`api_key:` for keys). */ export type OrgAgentActor = { subjectId: string; label: string | null; }; /** * Makes organization agent rows visible for the rest of the current * transaction (or savepoint) and returns the previous setting for * {@link restoreOrgAgentsVisibility}. For callers that already hold a scoped * transaction, such as the turn worker. */ export declare function enableOrgAgentsVisibility(tx: Database): Promise; export declare function restoreOrgAgentsVisibility(tx: Database, previous: string): Promise; /** * Runs `fn` in the organization's RLS scope (or one of its workspaces) with * organization agent rows visible. Nested calls restore the previous setting * when they return, as the RLS scope itself does. */ export declare function withOrgAgentsScope(db: Database, scope: { accountId: string; workspaceId?: string | null; }, fn: (tx: Database) => Promise): Promise; /** `sha256:` + hex sha256 of the canonical definition JSON. */ export declare function orgAgentConfigHash(definition: OrgAgentDefinitionInput): string; export declare function listOrgAgents(db: Database, accountId: string, options?: { includeArchived?: boolean; }): Promise; export declare function getOrgAgent(db: Database, accountId: string, ref: { id: string; } | { key: string; }, options?: { includeArchived?: boolean; }): Promise; export declare function listOrgAgentVersions(db: Database, accountId: string, agentId: string): Promise; /** One version, or null when the agent or version does not exist. */ export declare function getOrgAgentVersion(db: Database, accountId: string, agentId: string, version: number): Promise; type CreateInput = { accountId: string; actor: OrgAgentActor; key: string; definition: OrgAgentDefinitionInput; workspaceIds?: readonly string[]; toolCatalog?: OrgAgentToolCatalogValue | null; }; /** Creates a Console agent with an unpublished version 1. */ export declare function createOrgAgent(db: Database, input: CreateInput): Promise; /** Sets where the agent is available besides the organization's managed workspaces. */ export declare function updateOrgAgent(db: Database, input: { accountId: string; agentId: string; actor: OrgAgentActor; expectedVersion: number; workspaceIds?: readonly string[]; /** * Workspaces the caller cannot see (an organization key limited to some * workspaces): their access rows are kept as they are, and `workspaceIds` * replaces only the rest. Omitted, `workspaceIds` replaces the whole list. */ keepWorkspace?: (workspaceId: string) => boolean; }): Promise; /** * Saves a new version of a Console agent (the newest, unpublished until a * publish). A definition whose config hash equals an existing version saves * nothing and returns that version (`created: false`). */ export declare function saveOrgAgentDraft(db: Database, input: { accountId: string; agentId: string; actor: OrgAgentActor; expectedVersion: number; definition: OrgAgentDefinitionInput; toolCatalog?: OrgAgentToolCatalogValue | null; }): Promise<{ agent: OrgAgent; version: OrgAgentVersion; created: boolean; }>; /** * Publishes a version (an older one is a rollback). Without * `moveRunningChats` the operation succeeds at once; with it the operation is * `pending` until the move workflow runs it. */ export declare function publishOrgAgent(db: Database, input: { accountId: string; agentId: string; actor: OrgAgentActor; version: number; moveRunningChats: boolean; expectedVersion: number; }): Promise<{ agent: OrgAgent; operation: OrgAgentPublishOperation; }>; export declare function listOrgAgentPublishOperations(db: Database, accountId: string, agentId: string, limit?: number): Promise; export type OrgAgentPublishMoveFailure = "move_unavailable" | "move_start_failed"; /** * A `moveRunningChats` operation whose move could not start: marked `failed` * with the reason, unless the move already began. Returns the operation as it * is now (null when it is gone). The version stays published. */ export declare function failPendingOrgAgentPublishOperation(db: Database, input: { accountId: string; operationId: string; reason: OrgAgentPublishMoveFailure; }): Promise; /** * The code deploy: creates a code-managed agent for an unknown key, saves a * version unless one with the same config hash exists (that version is * reused), and publishes it when asked. `dryRun` reports the same outcome * without writing. A Console-managed agent is refused (409 * `agent_managed_in_console`). Applying to an archived code agent restores it. */ export declare function applyOrgAgent(db: Database, input: { accountId: string; key: string; actor: OrgAgentActor; definition: OrgAgentDefinitionInput; publish: boolean; moveRunningChats: boolean; dryRun: boolean; expectedVersion?: number | undefined; toolCatalog?: OrgAgentToolCatalogValue | null; }): Promise; /** Moves an agent between Console and code management. Callers check ownership. */ export declare function setOrgAgentManagement(db: Database, input: { accountId: string; agentId: string; actor: OrgAgentActor; managedBy: OrgAgentManagedBy; expectedVersion: number; }): Promise; /** * Archives an agent: new chats can no longer name it, and it leaves every * list. Its versions and chats are kept; a code deploy of a code agent's key * restores it. */ export declare function archiveOrgAgent(db: Database, input: { accountId: string; agentId: string; actor: OrgAgentActor; expectedVersion: number; }): Promise; /** * Whether chats in `workspaceId` may use the organization agent: the * workspace is one the organization manages (Console test workspace * included), or the agent was made available there. Runs in the caller's * transaction (account context, or that workspace's context). It does not * check that the agent exists, is published or is not archived. */ export declare function orgAgentReachableInWorkspace(tx: Database, input: { accountId: string; agentId: string; workspaceId: string; }): Promise; /** Whether `workspaceId` is the organization's Console test workspace (drafts run only there). */ export declare function isOrgAgentTestWorkspace(tx: Database, input: { accountId: string; workspaceId: string; }): Promise; export declare function findOrgAgentTestWorkspace(db: Database, accountId: string): Promise; /** * The organization's Console test workspace, created (with `create`, in this * transaction) and marked `console_test` when it does not exist yet. */ export declare function ensureOrgAgentTestWorkspace(db: Database, input: { accountId: string; actor: OrgAgentActor; create: (tx: Database) => Promise<{ id: string; }>; }): Promise<{ workspaceId: string; created: boolean; }>; /** Published, live organization agents chats in this workspace can name. */ export declare function listOrgAgentsForWorkspace(db: Database, input: { accountId: string; workspaceId: string; }): Promise; /** Root chats running another version that a publish with `moveRunningChats` would move. */ export declare function countOrgAgentMovableChats(db: Database, input: { accountId: string; agentId: string; toVersion: number; workspaceIds: readonly string[]; }): Promise; /** The organization's workspaces an agent's chats can be in: managed ones and its access list. */ export declare function listOrgAgentChatWorkspaces(db: Database, input: { accountId: string; agentId: string; }): Promise<{ managed: string[]; access: string[]; }>; /** * Newest-first reviewable root chats of one agent across the given (managed) * workspaces, with what each chat changed. `includeContent` is false at the * `metadata` review level: no title and no override values. */ export declare function listOrgAgentSessions(db: Database, input: { accountId: string; agentId: string; workspaceIds: readonly string[]; limit: number; cursor: string | null; version?: number | undefined; includeContent: boolean; }): Promise<{ sessions: OrgAgentSessionEntry[]; nextCursor: string | null; }>; /** Per-agent usage: root chats started in the window and their trees' metered cost, by version. */ export declare function orgAgentUsage(db: Database, input: { accountId: string; agentId: string; workspaceIds: readonly string[]; from: string; to: string; }): Promise; export declare function listOrgToolServers(db: Database, accountId: string): Promise; export declare function getOrgToolServer(db: Database, accountId: string, key: string): Promise; export declare function createOrgToolServer(db: Database, input: { accountId: string; actor: OrgAgentActor; request: CreateOrgToolServerRequest; encryptionKey: Uint8Array | null; }): Promise; export declare function updateOrgToolServer(db: Database, input: { accountId: string; key: string; actor: OrgAgentActor; request: UpdateOrgToolServerRequest; encryptionKey: Uint8Array | null; }): Promise; /** * Deletes a tool server and every end user's login for it. Versions that * name it keep the reference; their chats get it as unavailable. */ export declare function deleteOrgToolServer(db: Database, input: { accountId: string; key: string; actor: OrgAgentActor; expectedVersion?: number; }): Promise; /** A tool server with its decrypted secrets, for the runtime and the live tool list only. */ export type OrgToolServerWithSecrets = { server: OrgToolServer; sharedHeaders: Record | null; oauthRefresh: { tokenUrl: string; clientId: string; clientSecret: string | null; } | null; }; export declare function decodeOrgToolServerSecrets(key: Uint8Array | null, row: { shared_headers_encrypted: string | null; oauth_refresh: unknown; }): Pick; export declare function readOrgToolServersWithSecrets(tx: Database, input: { accountId: string; keys: readonly string[]; encryptionKey: Uint8Array | null; }): Promise; export declare function getOrgToolServerWithSecrets(db: Database, input: { accountId: string; key: string; encryptionKey: Uint8Array | null; }): Promise; /** The decrypted shape of `end_user_tool_credentials.material_encrypted`. */ export type EndUserToolCredentialMaterial = { kind: "headers"; headers: Record; } | { kind: "oauth"; accessToken: string; refreshToken?: string; tokenType: string; }; export declare function encodeEndUserToolCredentialMaterial(key: Uint8Array | null, material: EndUserToolCredentialMaterial): string; export declare function decodeEndUserToolCredentialMaterial(key: Uint8Array | null, stored: string): EndUserToolCredentialMaterial; type CredentialTarget = { accountId: string; workspaceId: string; endUserSubjectId: string; toolServerKey: string; }; export declare function putEndUserToolCredential(db: Database, input: CredentialTarget & { actor: OrgAgentActor; credential: PutEndUserToolCredentialRequest; encryptionKey: Uint8Array | null; }): Promise; /** The stored login's metadata (never its material), or null. */ export declare function getEndUserToolCredential(db: Database, input: CredentialTarget): Promise; export declare function deleteEndUserToolCredential(db: Database, input: CredentialTarget & { actor: OrgAgentActor; }): Promise; export {};