Skip to content

🀯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

9.9 KB
Raw Blame
import { BUILTIN_AGENT_SLUGS } from '@lobechat/builtin-agents';
import { TRPCError } from '@trpc/server';
import { z } from 'zod';

import { withScopedPermission } from '@/business/server/trpc-middlewares/rbacPermission';
import { ChatGroupModel } from '@/database/models/chatGroup';
import { ConnectorModel } from '@/database/models/connector';
import { ResourcePermissionModel } from '@/database/models/resourcePermission';
import { TaskModel } from '@/database/models/task';
import type { ResourceAccessLevel } from '@/database/schemas';
import { DEFAULT_RESOURCE_ACCESS_LEVELS, RESOURCE_ACCESS_LEVELS_BY_TYPE } from '@/database/schemas';
import type { LobeChatDatabase } from '@/database/type';
import { router } from '@/libs/trpc/lambda';
import {
  assertCanPerformResourceAction,
  buildResourcePermissionState,
} from '@/server/services/resourcePermission';

import { releasePrivatePin } from '../_helpers/sidebarPin';
import { agentProcedure } from './procedures';

/**
 * A private connector serves only its creator's private agents, and a shared
 * agent's runs leave it out. Publishing an agent that still uses one would
 * drop its configured tool for everyone, or silently answer it with a shared
 * connector of the same identifier running another account. Both publish
 * paths guard it, as group publishing guards private member agents.
 */
const assertNoPrivateConnectorBindings = async (
  ctx: { serverDB: LobeChatDatabase; userId: string; workspaceId?: string | null },
  agentId: string,
) => {
  if (!ctx.workspaceId) return;
  const bindings = await new ConnectorModel(
    ctx.serverDB,
    ctx.userId,
    ctx.workspaceId,
  ).countPrivateAgentBindings(agentId);
  if (bindings > 0)
    throw new TRPCError({
      code: 'BAD_REQUEST',
      message:
        'Cannot publish an agent that still uses private connectors. Remove them from the agent or share them with the workspace first.',
    });
};

export const visibilityRouter = router({
  /**
   * Publish a private agent into the workspace. Only the creator of a
   * still-private agent can run this; the underlying SQL enforces both rules.
   * The inverse transition (public β†’ private) goes through
   * `setAgentVisibility`, which is gated to the creator only.
   */
  publishAgentToWorkspace: agentProcedure
    .use(withScopedPermission('agent:update'))
    .input(
      z.object({
        accessLevel: z.enum(RESOURCE_ACCESS_LEVELS_BY_TYPE.agent).optional(),
        id: z.string(),
      }),
    )
    .mutation(async ({ input, ctx }) => {
      await assertNoPrivateConnectorBindings(ctx, input.id);
      // Hand a private pin to the creator's personal list BEFORE the row goes
      // public: done afterwards, a failure in between would leave a public
      // pinned row β€” a team pin no owner/admin chose.
      if (ctx.workspaceId) {
        const meta = await ctx.agentModel.getAgentVisibilityMeta(input.id);
        if (meta?.pinned && meta.visibility === 'private') {
          await releasePrivatePin({
            clearPinned: () => ctx.agentModel.update(input.id, { pinned: false }),
            creatorId: meta.userId,
            db: ctx.serverDB,
            itemId: input.id,
            workspaceId: ctx.workspaceId,
          });
        }
      }
      const result = await ctx.agentModel.publishToWorkspace(input.id);
      if (ctx.workspaceId) {
        const permissionModel = new ResourcePermissionModel(ctx.serverDB, ctx.workspaceId);
        // An explicit request wins; otherwise keep whatever the creator already
        // chose on the Permission page while the agent was still private β€”
        // rewriting the default here would silently discard that decision.
        const accessLevel =
          input.accessLevel ??
          (await permissionModel.getAccessLevel('agent', input.id)) ??
          DEFAULT_RESOURCE_ACCESS_LEVELS.agent;
        await permissionModel.setAccessLevel('agent', input.id, accessLevel, ctx.userId);
      }
      return result;
    }),
  /**
   * Bidirectional visibility switch. Rules:
   * - builtin agents (LobeAI etc., identified by slug) can never change
   *   visibility β€” the workspace copy must stay shared;
   * - only the agent's creator may pull a published agent back to private
   *: a workspace owner demoting another member's agent would
   *   effectively appropriate it, so everyone else gets FORBIDDEN. The UI
   *   hides the entry for them, this is the server-side backstop.
   */
  setAgentVisibility: agentProcedure
    .use(withScopedPermission('agent:update'))
    .input(
      z.object({
        accessLevel: z.enum(RESOURCE_ACCESS_LEVELS_BY_TYPE.agent).optional(),
        id: z.string(),
        visibility: z.enum(['private', 'public']),
      }),
    )
    .mutation(async ({ input, ctx }) => {
      const meta = await ctx.agentModel.getAgentVisibilityMeta(input.id);
      if (!meta) throw new TRPCError({ code: 'NOT_FOUND', message: 'Agent not found' });

      if (meta.slug && Object.values(BUILTIN_AGENT_SLUGS).includes(meta.slug as any)) {
        throw new TRPCError({
          code: 'FORBIDDEN',
          message: 'Builtin agents cannot change visibility',
        });
      }

      if (!ctx.workspaceId) {
        throw new TRPCError({
          code: 'BAD_REQUEST',
          message: 'Agent visibility only applies inside a workspace',
        });
      }
      const permissionModel = new ResourcePermissionModel(ctx.serverDB, ctx.workspaceId);
      const permissionMeta = { ...meta, workspaceId: ctx.workspaceId };
      const blockingTasksPromise =
        input.visibility === 'private' && meta.visibility !== input.visibility
          ? new TaskModel(
              ctx.serverDB,
              ctx.userId,
              ctx.workspaceId,
            ).countTasksBlockingAgentDemotion(input.id, meta.userId)
          : Promise.resolve(0);
      const [, blockingTasks] = await Promise.all([
        assertCanPerformResourceAction({
          action: 'changeVisibility',
          db: ctx.serverDB,
          grantedPermissions: (ctx as { workspacePermissionCodes?: string[] })
            .workspacePermissionCodes,
          meta: permissionMeta,
          resourceId: input.id,
          resourceType: 'agent',
          userId: ctx.userId,
          workspaceId: ctx.workspaceId,
        }),
        blockingTasksPromise,
      ]);

      if (meta.visibility === input.visibility) {
        const accessLevel =
          input.visibility === 'public'
            ? (input.accessLevel ??
              (await permissionModel.getEffectiveAccessLevel('agent', input.id)))
            : 'edit';
        if (input.visibility === 'public' && input.accessLevel) {
          await permissionModel.setAccessLevel('agent', input.id, input.accessLevel, ctx.userId);
        }
        return buildResourcePermissionState({
          accessLevel,
          canManage: true,
          creatorId: meta.userId,
          visibility: input.visibility,
        });
      }

      // Demoting an agent must not strand tasks that depend on it: public
      // tasks would violate the `assertAgentVisibilityCompat` invariant
      // (members keep seeing the task but can no longer see or run the
      // assignee), and other members' tasks β€” private ones included β€” would
      // fail future runs/updates because their creators can no longer
      // resolve the agent. Reject early β€” reassign or demote those tasks
      // first.
      if (blockingTasks > 0) {
        throw new TRPCError({
          code: 'BAD_REQUEST',
          message:
            'Cannot make this agent private while workspace tasks still depend on it. Reassign those tasks or make them private first.',
        });
      }

      // Same source-level guard for group chats, but only for the supervisor
      // role: a private supervisor is unresolvable for every other viewer and
      // bricks the whole group. Regular members are not blocked β€” roster
      // reads drop a non-visible member per viewer instead.
      if (input.visibility === 'private') {
        const chatGroupModel = new ChatGroupModel(ctx.serverDB, ctx.userId, ctx.workspaceId);
        const blockingGroups = await chatGroupModel.countGroupsBlockingAgentDemotion(
          input.id,
          meta.userId,
        );
        if (blockingGroups > 0) {
          throw new TRPCError({
            code: 'BAD_REQUEST',
            message:
              'Cannot make this agent private while it supervises workspace group chats. Remove it as supervisor first.',
          });
        }
      }

      if (input.visibility === 'public') await assertNoPrivateConnectorBindings(ctx, input.id);

      // Before the row turns public, so a failure never leaves a team pin
      // nobody with the role set (see `publishAgentToWorkspace`).
      if (input.visibility === 'public' && meta.pinned) {
        await releasePrivatePin({
          clearPinned: () => ctx.agentModel.update(input.id, { pinned: false }),
          creatorId: meta.userId,
          db: ctx.serverDB,
          itemId: input.id,
          workspaceId: ctx.workspaceId,
        });
      }

      const updated = await ctx.agentModel.setVisibility(input.id, input.visibility);
      if (!updated) throw new TRPCError({ code: 'NOT_FOUND', message: 'Agent not found' });

      let accessLevel: ResourceAccessLevel;
      if (input.visibility === 'private') {
        accessLevel = 'edit';
        await permissionModel.removeAll('agent', input.id);
      } else {
        // Same rule as `publishAgentToWorkspace`: promotion keeps a level the
        // creator already set while private instead of resetting to the default.
        accessLevel =
          input.accessLevel ??
          (await permissionModel.getAccessLevel('agent', input.id)) ??
          DEFAULT_RESOURCE_ACCESS_LEVELS.agent;
        await permissionModel.setAccessLevel('agent', input.id, accessLevel, ctx.userId);
      }

      return buildResourcePermissionState({
        accessLevel,
        canManage: true,
        creatorId: meta.userId,
        visibility: input.visibility,
      });
    }),
});