import { type Database } from "./database.js"; import { type OrgAgentRunsOnTarget } from "./organization-agent-compute.js"; /** The transaction setting that makes organization agents visible (migration 1505). */ export declare const ORG_AGENTS_VISIBILITY_SETTING = "opengeni.org_agents_v1"; /** Run `fn` with organization agents visible in this transaction, then restore the setting. */ export declare function withOrgAgentsVisible(tx: Database, fn: () => Promise): Promise; /** * A system read or write in a workspace (or, with `workspaceId: null`, the * account) with organization agents visible. The caller has already checked * the person's permission; the ambient session actor is not used. */ export declare function withOrgAgentScope(db: Database, scope: { accountId: string; workspaceId: string | null; }, fn: (tx: Database) => Promise): Promise; export type OrgAgentRuntimeAgent = { id: string; accountId: string; key: string; name: string; managedBy: "console" | "code"; /** The published version; null until the agent is published. */ publishedVersion: number | null; /** The newest version. */ currentVersion: number; archived: boolean; }; export type OrgAgentRuntimeVersion = { agentId: string; version: number; name: string; description: string; instructions: string; /** `agent_revisions.config` (OrgAgentVersionConfig), unparsed. */ config: unknown; /** `agent_revisions.tool_catalog` ({serverKey: [{name, description?}]}), unparsed. */ toolCatalog: unknown; configHash: string | null; source: string | null; /** * The machine or sandbox environment `runsOn` named when the version was * saved (`agent_revisions.runs_on_target`), by id; null for a version saved * before Runs on pinned ids or without a machine/environment. */ runsOnTarget: OrgAgentRunsOnTarget | null; }; /** Where an organization agent can run: the workspace's managed marker and access row. */ export type OrgAgentWorkspaceReach = { /** `organization_managed_workspaces.source` for this workspace, or null. */ managedSource: string | null; /** An `agent_workspace_access` row names this workspace. */ access: boolean; }; /** * The organization agent a session create names (by id or key) and how it * reaches the workspace. `agent` is null when the organization has none. */ export declare function readOrgAgentForSessionCreate(db: Database, input: { accountId: string; workspaceId: string; ref: { id: string; } | { key: string; }; }): Promise<{ agent: OrgAgentRuntimeAgent | null; reach: OrgAgentWorkspaceReach; }>; /** One version of an organization agent, or null. */ export declare function readOrgAgentVersion(db: Database, input: { accountId: string; workspaceId: string | null; agentId: string; version: number; }): Promise; /** {@link readOrgAgentVersion} in a transaction that already shows organization agents. */ export declare function readOrgAgentVersionInTransaction(tx: Database, input: { accountId: string; agentId: string; version: number; }): Promise; export type OrgToolServerRuntime = { id: string; key: string; name: string; url: string; authMode: "per_user" | "shared_key" | "none"; /** encrypt(JSON of Record); only for `shared_key`. */ sharedHeadersEncrypted: string | null; /** `{tokenUrl, clientId, clientSecretEncrypted?}` for end-user OAuth refresh. */ oauthRefresh: { tokenUrl: string; clientId: string; clientSecretEncrypted?: string; } | null; version: number; }; /** The organization's tool servers with these keys (unknown keys are absent). */ export declare function listOrgToolServersByKey(db: Database, input: { accountId: string; workspaceId: string | null; keys: readonly string[]; }): Promise>; export type InlineOrgAgentVersionResult = { outcome: "created_agent" | "created_version" | "reused"; agent: OrgAgentRuntimeAgent; version: OrgAgentRuntimeVersion; } | { outcome: "managed_in_console" | "archived"; agent: OrgAgentRuntimeAgent; } | { outcome: "not_found" | "forbidden"; }; /** * An inline `agent.config` on session create: the version of a code-managed * agent with this config hash, adding the agent (unknown key) or a version * (new hash) when `mayWrite`. Inline versions are never published. */ export declare function saveInlineOrgAgentVersion(db: Database, input: { accountId: string; workspaceId: string; ref: { id: string; } | { key: string; }; definition: { name: string; description: string; instructions: string; config: unknown; }; toolCatalog: unknown; configHash: string; /** The ids `runsOn` resolved to (`resolveOrgAgentRunsOnInAccount`). */ runsOnTarget?: OrgAgentRunsOnTarget | null; subjectId: string | null; /** Shown in Console's audit log beside the subject. */ actorLabel?: string | null; /** * Whether the caller may write (create the agent or add a version): only * with organization-level `agents:manage` (the caller checks it). */ mayWrite: boolean; }): Promise; /** The organization agent state a chat is pinned to. */ export type SessionOrgAgentState = { agentId: string; version: number; promptVersion: number | null; overrides: Record | null; }; /** * Whose chat this is, for its own tool server login headers: who created it * (`sessions.created_by_subject_id`) and the end user it acts for * (`scope_subject_id`); null when the chat cannot be read. */ export declare function readSessionLoginOwners(db: Database, input: { accountId: string; workspaceId: string; sessionId: string; }): Promise<{ creatorSubjectId: string | null; scopeSubjectId: string | null; } | null>; /** * The organization agent binding of the chat an earlier create made with this * idempotency key, as the caller sees it; null when there is none or it runs * no organization agent. A retry binds the same agent version instead of * resolving the agent again (it may have been republished or archived since). */ export declare function readOrgAgentBindingForCreateKey(db: Database, input: { accountId: string; workspaceId: string; createIdempotencyKey: string; /** The subject the replay reads as; null reads workspace-shared chats only. */ subjectId: string | null; }): Promise<{ sessionId: string; agentId: string; version: number; } | null>; /** The organization agent version a chat runs, or null for every other chat. */ export declare function readSessionOrgAgentState(db: Database, input: { accountId: string; workspaceId: string; sessionId: string; }): Promise; /** What a child chat records when its parent runs an organization agent. */ export type InheritedSessionOrgAgent = { agentId: string; version: number; /** Null unless the version's `workers.shareInstructions` is on: no agent layer. */ promptVersion: number | null; overrides: Record | null; }; /** Whether a version config shares the agent's Instructions with worker chats. */ export declare function orgAgentConfigSharesInstructions(config: unknown): boolean; /** * The organization agent a child chat inherits from its parent, read in the * create transaction: the parent's agent and version, and the version's * Instructions only when it shares them with workers. Null when the parent * runs no organization agent (every other chat). */ export declare function inheritedSessionOrgAgentInTransaction(tx: Database, input: { accountId: string; workspaceId: string; parentSessionId: string; }): Promise; /** Keys of these organization agents, read in an existing scoped transaction. */ export declare function readOrgAgentKeysInTransaction(tx: Database, accountId: string, agentIds: readonly string[]): Promise>; export type EndUserToolCredentialRow = { id: string; kind: "headers" | "oauth"; materialEncrypted: string; expiresAt: Date | null; refreshFailedAt: Date | null; version: number; }; /** One end user's stored login for an organization tool server in a workspace. */ export declare function readEndUserToolCredential(db: Database, input: { accountId: string; workspaceId: string; subjectId: string; toolServerId: string; }): Promise; /** How long a refresh lease keeps other workers from refreshing the same login. */ export declare const END_USER_LOGIN_REFRESH_LEASE_MS = 60000; /** * Refresh one stored OAuth login without holding a row lock or a database * connection during the token request: * * 1. A short transaction takes a refresh lease on the row * (`refresh_lease_until`), only while the login still needs a refresh and * no other worker holds an unexpired lease. Otherwise it returns `locked` * (another worker is refreshing: re-read shortly), `current` (already * refreshed) or `missing`. * 2. `refresh` runs outside any transaction. It returns the new encrypted * material and expiry, or null when the provider refused. * 3. A second short transaction writes the result only if the row is still * the version that was leased (compare-and-set), and clears the lease. A * row that changed meanwhile (for example the end user signed in again) * wins and is returned as `current`; a refusal sets `refresh_failed_at`. */ export declare function refreshEndUserToolCredential(db: Database, input: { accountId: string; workspaceId: string; credentialId: string; /** Refresh only while the stored expiry is before this. */ refreshBefore: Date; refresh: (current: EndUserToolCredentialRow) => Promise<{ materialEncrypted: string; expiresAt: Date | null; } | null>; /** Test seam: the lease length. */ leaseMs?: number; }): Promise<{ outcome: "locked" | "missing"; } | { outcome: "refreshed" | "current" | "failed"; credential: EndUserToolCredentialRow; }>; /** Record that these stored logins were used for a turn. */ export declare function markEndUserToolCredentialsUsed(db: Database, input: { accountId: string; workspaceId: string; credentialIds: readonly string[]; }): Promise; export type OrgAgentPublishOperationRow = { id: string; accountId: string; agentId: string; fromVersion: number | null; toVersion: number; moveRunningChats: boolean; status: "pending" | "running" | "succeeded" | "failed"; movedCount: number; failedCount: number; }; export declare function readOrgAgentPublishOperation(db: Database, input: { accountId: string; operationId: string; }): Promise; /** Add to an operation's counts and, optionally, set its status. */ export declare function updateOrgAgentPublishOperation(db: Database, input: { accountId: string; operationId: string; status?: "running" | "succeeded" | "failed"; moved?: number; failed?: number; /** Why the operation ended early, e.g. `superseded` when a newer publish took over a running move. */ failureReason?: "superseded"; }): Promise; /** The agent's published version now (null when unpublished, archived or gone). */ export declare function readOrgAgentPublishedVersion(db: Database, input: { accountId: string; agentId: string; }): Promise; /** * Whether a chat's organization agent may still use the organization's * secrets in the chat's workspace, read in one statement each turn: the agent * is not archived (a missing agent counts as archived) and still reaches the * workspace (a managed workspace, or an `agent_workspace_access` row). */ export declare function readOrgAgentRuntimeStanding(db: Database, input: { accountId: string; workspaceId: string; agentId: string; }): Promise<{ archived: boolean; reaches: boolean; }>; /** * Workspaces whose chats a publish may move: the organization's managed * workspaces (except its Console test workspace, whose chats try drafts) and * the workspaces the agent was added to. */ export declare function listOrgAgentMoveWorkspaces(db: Database, input: { accountId: string; agentId: string; }): Promise; export type OrgAgentMoveCandidate = { sessionId: string; version: number; /** A turn is running or waits on a person (an approval or a question). */ busy: boolean; }; /** Chats in a workspace that run the agent on another version, in id order. */ export declare function listOrgAgentMoveCandidates(db: Database, input: { accountId: string; workspaceId: string; agentId: string; toVersion: number; afterSessionId: string | null; limit: number; }): Promise;