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.2 KB
Raw Blame
import type { LobeChatDatabase } from '@lobechat/database';
import { createTimingHelpers } from '@lobechat/utils';

import { MessageModel } from '@/database/models/message';
import { TopicModel } from '@/database/models/topic';
import type { WorkTypeOptIn } from '@/database/models/work/registry';
import { FileService } from '@/server/services/file';

const { createPrefixedTimingContext, runTimedStage, toTimingContext } = createTimingHelpers(
  'lobe-server:chat:lobehub:timing',
);

interface GetMessagesAndTopicsParams {
  agentId?: string;
  current?: number;
  groupId?: string;
  includeTopic?: boolean;
  pageSize?: number;
  sessionId?: string;
  threadId?: string;
  timingRequestId?: string;
  timingStartedAt?: number;
  topicFilter?: {
    excludeStatuses?: string[];
    excludeTriggers?: string[];
    includeTriggers?: string[];
    onlyMine?: boolean;
  };
  topicId?: string;
  topicPageSize?: number;
}

export class AiChatService {
  private messageModel: MessageModel;
  private fileService: FileService;
  private topicModel: TopicModel;
  private workTypeOptIn: WorkTypeOptIn;

  constructor(
    serverDB: LobeChatDatabase,
    userId: string,
    workspaceId?: string,
    options: { workTypeOptIn?: WorkTypeOptIn } = {},
  ) {
    this.workTypeOptIn = options.workTypeOptIn ?? {};
    this.messageModel = new MessageModel(serverDB, userId, workspaceId);
    this.topicModel = new TopicModel(serverDB, userId, workspaceId);
    this.fileService = new FileService(serverDB, userId, workspaceId);
  }

  async getMessagesAndTopics(params: GetMessagesAndTopicsParams) {
    const { topicFilter, topicPageSize, timingRequestId, timingStartedAt, ...messageParams } =
      params;
    const timingContext = toTimingContext({ timingRequestId, timingStartedAt });
    const messageTiming = createPrefixedTimingContext(
      timingContext,
      'lambda.aiChat.messagesAndTopics.messageModel.query',
    );
    const topicTiming = createPrefixedTimingContext(
      timingContext,
      'lambda.aiChat.messagesAndTopics.topicModel.query',
    );
    const messageQueryPromise = runTimedStage(
      timingContext,
      'lambda.aiChat.messagesAndTopics.messageModel.query',
      () =>
        this.messageModel.query(
          { ...messageParams, ...this.workTypeOptIn },
          {
            postProcessUrl: (path, file) =>
              this.fileService.getFileAccessUrl({ id: file.id, url: path }),
            ...(messageTiming ? { timing: messageTiming } : {}),
          },
        ),
      {
        hasAgentId: !!params.agentId,
        hasThreadId: !!params.threadId,
        hasTopicId: !!params.topicId,
      },
    );
    const [messages, topics] = await Promise.all([
      messageQueryPromise,
      params.includeTopic
        ? runTimedStage(
            timingContext,
            'lambda.aiChat.messagesAndTopics.topicModel.query',
            () =>
              this.topicModel.query({
                agentId: params.agentId,
                groupId: params.groupId,
                pageSize: topicPageSize,
                ...(topicTiming ? { timing: topicTiming } : {}),
                ...topicFilter,
              }),
            { hasAgentId: !!params.agentId, hasGroupId: !!params.groupId },
          )
        : undefined,
    ]);

    return { messages, topics };
  }
}