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.

1.5 KB
Raw Blame
import { z } from 'zod';

import {
  requireWorkspaceRoleWhenScoped,
  wsCompatProcedure,
} from '@/business/server/trpc-middlewares/workspaceAuth';
import { router } from '@/libs/trpc/lambda';
import { serverDatabase } from '@/libs/trpc/lambda/middleware';
import { WebBrowsingDocumentService } from '@/server/services/webBrowsing';

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

export const webBrowsingRouter = router({
  /**
   * Persist a crawled page as a `documents` row, deduping by URL and
   * short-circuiting on byte-identical content. See
   * `WebBrowsingDocumentService.upsertCrawledDocument` for the dispatch.
   */
  upsertCrawledDocument: webBrowsingProcedure
    // Writes a `documents` row — workspace viewers are read-only.
    .use(requireWorkspaceRoleWhenScoped('member'))
    .input(
      z.object({
        content: z.string(),
        /** Crawler content type; `json` bodies get wrapped in a fenced code block before persisting. */
        contentType: z.literal('json').optional(),
        description: z.string().optional(),
        title: z.string(),
        topicId: z.string().optional(),
        url: z.string().url(),
      }),
    )
    .mutation(async ({ ctx, input }) => {
      return ctx.webBrowsingService.upsertCrawledDocument(input);
    }),
});