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.

3.1 KB
Raw Blame
import { AGENT_CHAT_TOPIC_URL, GROUP_CHAT_TOPIC_URL } from '@lobechat/const';
import type { ChatTopicMetadata, RecentItem } from '@lobechat/types';
import { z } from 'zod';

import { wsCompatProcedure } from '@/business/server/trpc-middlewares/workspaceAuth';
import { RecentModel } from '@/database/models/recent';
import { router } from '@/libs/trpc/lambda';
import { serverDatabase } from '@/libs/trpc/lambda/middleware';

const recentProcedure = wsCompatProcedure.use(serverDatabase).use(async (opts) => {
  const { ctx } = opts;
  return opts.next({
    ctx: {
      recentModel: new RecentModel(ctx.serverDB, ctx.userId, ctx.workspaceId ?? undefined),
    },
  });
});

export const recentRouter = router({
  getAll: recentProcedure
    .input(
      z
        .object({
          excludeArchived: z.boolean().optional(),
          limit: z.number().optional(),
          /** Restrict a workspace feed to the viewer's own items (mine/team toggle). */
          mineOnly: z.boolean().optional(),
          /**
           * Restrict a workspace feed to conversations the whole team can see —
           * topics owned by a private agent/group are dropped even for their
           * own creator. Set by the home "team" tab.
           */
          sharedOnly: z.boolean().optional(),
          types: z.array(z.enum(['topic', 'document', 'task'])).optional(),
          withTopicPreview: z.boolean().optional(),
        })
        .optional(),
    )
    .query(async ({ ctx, input }): Promise<RecentItem[]> => {
      const limit = input?.limit ?? 10;

      const items = await ctx.recentModel.queryRecent(
        limit,
        input?.types,
        input?.withTopicPreview,
        input?.mineOnly,
        input?.sharedOnly,
        input?.excludeArchived,
      );

      return items.map((item) => {
        let routePath: string;

        switch (item.type) {
          case 'topic': {
            if (item.routeGroupId) {
              routePath = GROUP_CHAT_TOPIC_URL(item.routeGroupId, item.id);
            } else if (item.routeId) {
              routePath = AGENT_CHAT_TOPIC_URL(item.routeId, item.id);
            } else {
              routePath = '/';
            }
            break;
          }
          case 'document': {
            routePath = `/resource/page/${item.id}`;
            break;
          }
          case 'task': {
            routePath = item.routeId
              ? `/agent/${item.routeId}/task/${item.taskIdentifier ?? item.id}`
              : `/task/${item.taskIdentifier ?? item.id}`;
            break;
          }
        }

        return {
          agentId: item.routeId,
          description: item.description,
          icon: item.type,
          id: item.id,
          taskIdentifier: item.taskIdentifier,
          lastAssistantMessage: item.lastAssistantMessage,
          metadata: item.metadata as ChatTopicMetadata | undefined,
          routePath,
          slugTitle: item.slugTitle,
          status: item.status,
          title: item.title,
          type: item.type,
          updatedAt: item.updatedAt,
          userId: item.userId,
        };
      });
    }),
});

export type RecentRouter = typeof recentRouter;