/** * Agents: reads and writes (docs/agents.md). * * An agent row belongs to its HOME workspace, where its chats run. Its * Instructions and First message are immutable revisions in the same * workspace; where it answers in Slack is one `agent_slack_installs` row per * Slack installation, also home-owned, recording the installation's own * workspace (`install_workspace_id`). An agent may be in any number of Slack * installations; the Slack tables (channel routes, interactions, user states, * proposals, intro records, suggestions) point at its installation row. * * The legacy `bot_templates` and `slack_agents` tables are gone (the contract, * migrations 1380 to 1399, and the legacy drop, 1437 to 1447). * * Authorization (who may read, change or enable what) is decided by callers * from live grants. Reads filter Only me agents explicitly; nothing here * grants anything. */ import { type SessionAvatar } from "@opengeni/contracts"; import { type Database } from "./database.js"; export type AgentHome = { accountId: string; workspaceId: string; }; export type AgentVisibilityValue = "private" | "workspace"; export type AgentOriginValue = "slack_agent" | "bot_template" | "app" | "slack"; /** The current revision's content, read from the agent's home workspace. */ export type AgentContent = { instructions: string; createdBySubjectId: string | null; createdAt: Date; }; export type AgentRecord = { id: string; accountId: string; workspaceId: string; visibility: AgentVisibilityValue; ownerSubjectId: string | null; name: string; description: string; /** As stored; read it back through the contract's schema. */ avatar: unknown; memoryEnabled: boolean; memoryCollectionId: string | null; /** * Who can use it: anyone in the organization from chat integrations, not * only the workspace. Always false for an Only me agent. The only source * since migration 1448; each Slack installation's `open` is kept equal to * it for the previous release, which still reads that. */ organizationAccess: boolean; /** Can use rules as stored (1517); read them through the contract's schema. */ toolPermissions: unknown[]; /** Skills listed on its page, as stored (1517). */ skills: unknown[]; /** The ready-made agent it was added from. */ templateKey: string | null; currentRevision: number; origin: AgentOriginValue; sourceSessionId: string | null; createdBySubjectId: string | null; version: number; createdAt: Date; updatedAt: Date; /** Null in lists and where the revision is not readable. */ content: AgentContent | null; }; export type AgentSlackInstallRecord = { id: string; agentId: string; accountId: string; /** The agent's home workspace. */ workspaceId: string; connectionId: string; slackTeamId: string; nameKey: string; open: boolean; enabledBySubjectId: string | null; version: number; createdAt: Date; updatedAt: Date; /** * The Slack installation's own workspace (where the installation binding * and its routes live). */ installWorkspaceId: string; }; export type AgentRevisionRecord = { revision: number; name: string; description: string; instructions: string; createdBySubjectId: string | null; createdAt: Date; }; /** * The avatar an agent posts with in Slack (it cannot be empty): the agent's * own, or its initials. */ export declare function agentSlackAvatar(agent: { id: string; name: string; avatar: unknown; }): SessionAvatar; /** * The home workspace's agents a reader sees, oldest first: the shared ones * and the reader's own Only me ones. `yours` keeps those the reader made or * owns. */ export declare function listAgents(db: Database, home: AgentHome, input: { viewerSubjectId: string | null; filter?: "all" | "yours"; /** Also read each current revision's content. */ withContent?: boolean; }): Promise; /** * One agent of the home workspace with its current revision, or null. No * visibility filter: the caller decides who may see an Only me agent. */ export declare function getAgent(db: Database, home: AgentHome, agentId: string): Promise; /** Where these agents of the home workspace are available in Slack. */ export declare function listAgentSlackInstalls(db: Database, home: AgentHome, agentIds: readonly string[]): Promise; /** Saved revisions of an agent, newest first, older than `beforeRevision`. */ export declare function listAgentRevisions(db: Database, home: AgentHome, input: { agentId: string; beforeRevision?: number | null; limit: number; }): Promise; export type CreateAgentInput = { /** A fixed id makes a retried create find the agent it already made. */ id?: string; visibility: AgentVisibilityValue; ownerSubjectId: string | null; name: string; description: string; avatar: SessionAvatar | null; instructions: string; /** Older field: joins the Instructions as its own paragraph. */ firstMessage?: string; memoryEnabled: boolean; /** Anyone in the organization can use it (shared agents only). */ organizationAccess?: boolean; /** Can use rules, already validated. */ toolPermissions?: readonly unknown[]; /** Skills listed on its page, already validated. */ skills?: readonly unknown[]; /** The ready-made agent it is added from. */ templateKey?: string | null; origin: AgentOriginValue; sourceSessionId: string | null; createdBySubjectId: string | null; /** Refuse past this many agents in the home workspace. */ maxPerWorkspace: number; }; /** Creating another agent would pass the workspace's limit. */ export declare class AgentLimitError extends Error { readonly limit: number; readonly name = "AgentLimitError"; constructor(limit: number); } /** * An Only me agent's owner must be someone the database knows as a member of * the organization (the private-agent policy reads memberships), like an Only * me chat's. */ export declare class AgentPrivateOwnerError extends Error { readonly name = "AgentPrivateOwnerError"; constructor(); } /** * Create an agent with revision 1. With a fixed id, a retry returns the agent * it already made (`created: false`), unchanged. */ export declare function createAgent(db: Database, home: AgentHome, input: CreateAgentInput): Promise<{ agent: AgentRecord; created: boolean; }>; export type AgentPatch = { name?: string; description?: string; avatar?: SessionAvatar | null; visibility?: AgentVisibilityValue; /** Required with `visibility`: the owner of an Only me agent, null otherwise. */ ownerSubjectId?: string | null; instructions?: string; /** Older field: joins the Instructions as its own paragraph. */ firstMessage?: string; memoryEnabled?: boolean; /** Replaces every Can use rule, already validated. */ toolPermissions?: readonly unknown[]; /** Replaces the Skills list, already validated. */ skills?: readonly unknown[]; /** * Anyone in the organization can use it. Written to the agent and, for the * previous release during the rollout, as `open` on every Slack * installation it is in. Making the agent Only me turns it off. */ organizationAccess?: boolean; /** Forget the current Memory collection (for example after a visibility change). */ clearMemoryCollection?: boolean; }; export type UpdateAgentOutcome = { kind: "updated"; agent: AgentRecord; revised: boolean; } | { kind: "not_found"; } | { kind: "version_conflict"; current: AgentRecord; } /** The new name clashes with another agent in a Slack installation it is in. */ | { kind: "slack_name_taken"; connectionId: string; }; /** * Lock an agent of the home workspace for the rest of the caller's * transaction and read it, or null when it does not exist. */ export declare function lockAgent(tx: Database, home: AgentHome, agentId: string): Promise; /** * Change an agent at an expected version (null skips the check, for the * compatibility endpoints that never had one). Changing the Instructions or * First message adds a revision made by `actorSubjectId`; renaming an agent * that is in Slack renames its Slack handle too. */ export declare function updateAgent(db: Database, home: AgentHome, input: { agentId: string; expectedVersion: number | null; patch: AgentPatch; actorSubjectId: string | null; }): Promise; /** Remember the agent's Memory collection once it exists. Version-free and idempotent. */ export declare function setAgentMemoryCollection(db: Database, home: AgentHome, input: { agentId: string; collectionId: string; }): Promise; /** * Delete an agent, its revisions and Slack availability (its channel * settings and other Slack records go with its installation rows). Its chats * stay. */ export declare function deleteAgent(db: Database, home: AgentHome, agentId: string): Promise; export type PutAgentSlackInstallOutcome = { kind: "saved"; install: AgentSlackInstallRecord; agent: AgentRecord; created: boolean; } | { kind: "not_found"; } | { kind: "version_conflict"; install: AgentSlackInstallRecord; } | { kind: "name_taken"; } /** An Only me agent cannot be in Slack. */ | { kind: "requires_workspace_visibility"; }; /** * Make an agent available in one Slack installation. An agent may be in * several installations. Its `open` there is the agent's own "Anyone in the * organization" (change that with `updateAgent`). Callers check the name * rules, visibility and permissions first. */ export declare function putAgentSlackInstall(db: Database, home: AgentHome, input: { agentId: string; connectionId: string; slackTeamId: string; /** The installation's own workspace (its binding's workspace). */ installWorkspaceId: string; expectedVersion?: number; /** Null when Opengeni made it available on its own (every linked Slack workspace). */ actorSubjectId: string | null; /** * Opengeni adds it on its own (a shared agent is in every linked Slack * workspace): the agent's version stays, so nobody's open edit of it * conflicts. A person turning it on bumps it. */ automatic?: boolean; }): Promise; /** * Stop an agent answering in one Slack installation. Its channel routes, user * states, intro records and suggestions there go with it, and that * installation's threads and proposals lose their link to it. The agent, its * chats and its other installations stay. */ export declare function deleteAgentSlackInstall(db: Database, home: AgentHome, input: { agentId: string; connectionId: string; }): Promise; export type AgentChatRecord = { sessionId: string; title: string | null; visibility: "private" | "workspace"; createdBySubjectId: string | null; ownerSubjectId: string | null; slack: { connectionId: string; channelId: string; threadTs: string; initiatingSlackUserId: string | null; } | null; createdAt: Date; /** `created_at` to the microsecond, for a keyset cursor. */ cursorAt: string; updatedAt: Date; }; /** * An agent's chats in its home workspace, newest first: chats bound to it * (`sessions.agent_id`), plus chats an older release started for it in Slack * that only its Slack interaction names. Only me chats and Slack DMs are listed * only to their owner; `yours` keeps the reader's own chats; `sharedOnly` lists * neither (for an agent's other chats). */ export declare function listAgentChats(db: Database, home: AgentHome, input: { agentId: string; viewerSubjectId: string | null; filter: "yours" | "everyone"; sharedOnly?: boolean; /** * Only open chats that are with the agent itself (what an automation can * post into): no Slack threads that only mentioned it, no cancelled chats. */ direct?: boolean; excludeSessionId?: string | null; /** Keyset cursor: chats created before this one (`cursorAt` of the last chat read). */ before?: { createdAt: string; sessionId: string; } | null; limit: number; }): Promise; /** * The agent a chat was started with: `sessions.agent_id`, or for a chat an * older release started in Slack, the agent its Slack interaction names. * Null for an ordinary chat. */ export declare function getSessionAgentId(db: Database, input: { workspaceId: string; sessionId: string; }): Promise; /** A chat bound to an Only me agent stays Only me. */ export declare class SessionAgentRequiresPrivateChatError extends Error { readonly name = "SessionAgentRequiresPrivateChatError"; constructor(); } /** * Whether a chat is bound to an Only me agent, whose chats stay Only me * (making one shared would hand its private Instructions to others). */ export declare function sessionAgentRequiresPrivateChat(db: Database, input: { workspaceId: string; sessionId: string; }): Promise; /** * An agent as the Slack pipeline sees it in one installation: the shape the * Slack agent code was written against, read from the agents tables. * `accountId`/`workspaceId` are the installation's own (home) tenancy; * `targetWorkspaceId` is the agent's home workspace, where its chats run. */ export type SlackAgentProjection = { id: string; accountId: string; workspaceId: string; connectionId: string; slackTeamId: string; targetAccountId: string; targetWorkspaceId: string; name: string; nameKey: string; description: string; avatar: SessionAvatar; /** "" when the reader's workspace cannot read the agent's revisions. */ instructions: string; /** Null while Memory is off. */ knowledgeCollectionId: string | null; /** The Memory switch; on with no collection while one could not be made. */ memoryEnabled: boolean; open: boolean; createdBySubjectId: string; version: number; createdAt: Date; updatedAt: Date; }; /** * The agents one Slack installation answers to, read from its own workspace, * by name. An installation with none stays one indexed read. */ export declare function listSlackAgentProjectionsForConnection(db: Database, installHome: AgentHome, connectionId: string): Promise; /** A shared agent of the organization that one Slack installation doesn't answer to yet. */ export type AgentMissingFromSlack = { id: string; /** Its home workspace. */ workspaceId: string; name: string; createdBySubjectId: string | null; }; /** * Shared agents in the installation's organization that are not available * in this Slack installation (`connectionId`) yet, oldest first, so that when * two share a name the older one keeps it. Read from the installation's own * workspace through the organization read. The caller decides which of them * belong here (never a personal workspace's, only agents whose workspace is * linked to this Slack workspace). */ export declare function listAgentsMissingFromSlack(db: Database, installHome: AgentHome, input: { connectionId: string; }): Promise; /** Every Slack-available agent of an organization, by name. */ export declare function listOrganizationSlackAgentProjections(db: Database, input: { accountId: string; }): Promise; /** * One Slack-available agent of an organization by id, as one installation * sees it, or null. An agent may be in several installations: `connectionId` * picks that installation, `installWorkspaceId` the one whose own workspace * it is; with neither, the installation it joined first. */ export declare function getSlackAgentProjection(db: Database, input: { accountId: string; agentId: string; connectionId?: string | null; installWorkspaceId?: string | null; }): Promise; export type ResolveAgentRefOutcome = { kind: "found"; agent: AgentRecord; } | { kind: "not_found"; } /** Several agents the reader can use share this name: name it by id. */ | { kind: "ambiguous"; agentIds: string[]; }; /** * An agent of the home workspace named by id or by name (case and outer * spaces ignored), among those the reader can use: shared ones and the * reader's own Only me ones. The one lookup behind delegation by name * (`session_create`'s `agent`), automations and outside agents' tools. */ export declare function resolveWorkspaceAgentRef(db: Database, home: AgentHome, input: { ref: string; viewerSubjectId: string | null; /** Only agents anyone in the organization can use (an open Slack chat). */ organizationAccessOnly?: boolean; }): Promise; /** * Every agent up a chat's chain (its own, then each chat above it with one), * nearest first. Can use applies the strictest of their rules, so handing * work to another agent never drops a restriction set on the caller. */ export declare function getSessionAgentChain(db: Database, input: { workspaceId: string; sessionId: string; }): Promise; /** * The agent a chat was started with, read from the chat's workspace (the * agent's home). `agentChat` stays true when the agent was deleted since, so * callers can post as plain Opengeni rather than as the chat's own title. */ export declare function getSessionAgent(db: Database, input: { workspaceId: string; sessionId: string; }): Promise<{ agentChat: boolean; agent: AgentRecord | null; }>; /** * A chat's agent for its Memory rules, with the chat's audience. A worker a * chat started has no agent of its own, so it is checked as the nearest * chat's above it that has one (its root chat's, unless a chat in between * was delegated to another agent): it may not write to a Memory that chat * could not. Privacy and Memory * scope are the asking chat's own (a private chat's workers are private too). */ export declare function getSessionAgentMemoryState(db: Database, input: { workspaceId: string; sessionId: string; }): Promise<{ agentChat: boolean; agent: AgentRecord | null; chat: { private: boolean; /** The session row itself is Only me (a Slack DM's row is shared). */ onlyMe: boolean; ownerSubjectId: string | null; memoryOff: boolean; slackDm: boolean; }; } | null>;