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.

4.5 KB
Raw Blame
import { type PlanDocument, type PlanRuntimeService } from '@lobechat/builtin-tool-lobe-agent';
import { AGENT_DOCUMENT_SOURCE_TYPE, AGENT_PLAN_FILE_TYPE } from '@lobechat/const';
import { type LobeChatDatabase } from '@lobechat/database';

import { DocumentModel } from '@/database/models/document';
import { TopicDocumentModel } from '@/database/models/topicDocument';
import { assertDocumentWriteWithinLimit } from '@/server/services/document/writeLimit';

/**
 * Build a server-side `PlanRuntimeService` backed by the application database.
 *
 * The factory is consumed by `lobeAgent.ts`'s `LobeAgentExecutionRuntime`,
 * which folds plan/todo execution into the lobe-agent server runtime so the
 * registry has a single runtime per identifier.
 */
export const createServerPlanRuntimeService = (
  serverDB: LobeChatDatabase,
  userId: string,
  workspaceId?: string,
  callerAgentVisibility?: 'private' | 'public' | null,
): PlanRuntimeService => {
  const documentModel = new DocumentModel(serverDB, userId, workspaceId, callerAgentVisibility);
  const topicDocumentModel = new TopicDocumentModel(serverDB, userId, workspaceId);

  const toPlanDocument = (doc: {
    content: string | null;
    createdAt: Date;
    description: string | null;
    id: string;
    metadata: Record<string, any> | null;
    title: string | null;
    updatedAt: Date;
  }): PlanDocument => ({
    content: doc.content,
    createdAt: doc.createdAt,
    description: doc.description,
    id: doc.id,
    metadata: doc.metadata,
    title: doc.title,
    updatedAt: doc.updatedAt,
  });

  const loadPlanOrThrow = async (id: string) => {
    const doc = await documentModel.findById(id);
    if (!doc) throw new Error(`Plan not found after update: ${id}`);
    return toPlanDocument(doc);
  };

  return {
    createPlan: async ({ topicId, goal, description, content }) => {
      await assertDocumentWriteWithinLimit({
        charCount: content.length,
        source: 'agent',
        userId,
        workspaceId,
      });

      const doc = await documentModel.create({
        content,
        description,
        fileType: AGENT_PLAN_FILE_TYPE,
        source: `lobe-agent:${topicId}`,
        // A plan is a machine-generated artifact; 'api' would make it
        // indistinguishable from a user-authored Page in resource listings.
        sourceType: AGENT_DOCUMENT_SOURCE_TYPE,
        title: goal,
        totalCharCount: content.length,
        totalLineCount: content.split('\n').length,
        // Private-agent plans stay in the owner's private Pages; public
        // agents' plans stay visible to the workspace. When null (no agent
        // context resolvable) default workspace plans to private — the
        // `sourceType='api'` fallback in `DocumentModel.create` that used to
        // cover this no longer applies to 'agent' rows.
        ...(callerAgentVisibility === 'private' || callerAgentVisibility === 'public'
          ? { visibility: callerAgentVisibility }
          : workspaceId
            ? { visibility: 'private' as const }
            : {}),
      });

      await topicDocumentModel.associate({ documentId: doc.id, topicId });

      return toPlanDocument(doc);
    },

    findPlanById: async (id) => {
      const doc = await documentModel.findById(id);
      if (!doc || doc.fileType !== AGENT_PLAN_FILE_TYPE) return null;
      return toPlanDocument(doc);
    },

    findPlanByTopic: async (topicId) => {
      const docs = await topicDocumentModel.findByTopicId(topicId, { type: AGENT_PLAN_FILE_TYPE });
      const first = docs[0];
      return first ? toPlanDocument(first) : null;
    },

    updatePlan: async (id, { goal, description, content }) => {
      const updateData: Record<string, any> = {};
      if (goal !== undefined) updateData.title = goal;
      if (description !== undefined) updateData.description = description;
      if (content !== undefined) {
        const existing = await documentModel.findById(id);
        await assertDocumentWriteWithinLimit({
          charCount: content.length,
          previousCharCount: existing?.content?.length ?? 0,
          source: 'agent',
          userId,
          workspaceId,
        });
        updateData.content = content;
        updateData.totalCharCount = content.length;
        updateData.totalLineCount = content.split('\n').length;
      }

      if (Object.keys(updateData).length > 0) {
        await documentModel.update(id, updateData);
      }

      return loadPlanOrThrow(id);
    },

    updatePlanMetadata: async (id, metadata) => {
      await documentModel.update(id, { metadata });
    },
  };
};