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.

11.0 KB
Raw Blame
import { GoalReportApiName, GoalReportIdentifier } from '@lobechat/builtin-tool-goal/report';
import { GOAL_COORDINATOR_ACTOR_ID } from '@lobechat/const/goal';
import type {
  GoalGraphSnapshot,
  GoalReportDispatch,
  GoalReportMetadata,
  GoalReportState,
} from '@lobechat/types';
import { GoalReportMetadataSchema } from '@lobechat/types';
import { TRPCError } from '@trpc/server';
import { eq } from 'drizzle-orm';

import { AgentOperationModel } from '@/database/models/agentOperation';
import { GoalGraphModel } from '@/database/models/goalGraph';
import { TaskModel } from '@/database/models/task';
import { TaskTopicModel } from '@/database/models/taskTopic';
import { WorkModel } from '@/database/models/work';
import { goalEvents } from '@/database/schemas';
import type { LobeChatDatabase } from '@/database/type';

import { reconcileGoalReport, validateGoalReport } from './report';
import { writeGoalReportPage } from './reportPage';

/**
 * How long a wrap-up run may go without submitting before the page stops
 * showing it as in progress. The Goal itself never waits on it.
 */
export const GOAL_REPORT_TIMEOUT_MS = 60 * 60 * 1000;

export interface SubmitGoalReportInput {
  content: string;
  /** The same report as one self-contained HTML page, stored as a `custom/html` document. */
  html?: string;
  metadata: Omit<GoalReportMetadata, 'graphCursor'> & { graphCursor?: string };
}

export interface GoalReportCaller {
  agentId?: string | null;
  operationId?: string | null;
  toolCallId?: string | null;
  topicId?: string | null;
}

/**
 * Writes and reads of the Goal report. Kept free of the task runner (and so of
 * the agent runtime) because the wrap-up agent's tool runtime imports it: the
 * runtime registry sits under the agent runtime, and importing back up into it
 * would be a cycle.
 */
export class GoalReportStore {
  private readonly coordinatorGraph: GoalGraphModel;
  private readonly operationModel: AgentOperationModel;
  private readonly taskModel: TaskModel;
  private readonly taskTopicModel: TaskTopicModel;
  private readonly workModel: WorkModel;

  constructor(
    private readonly db: LobeChatDatabase,
    private readonly userId: string,
    private readonly workspaceId?: string,
  ) {
    this.coordinatorGraph = new GoalGraphModel(db, userId, workspaceId, {
      id: GOAL_COORDINATOR_ACTOR_ID,
      type: 'system',
    });
    this.operationModel = new AgentOperationModel(db, userId, workspaceId);
    this.taskModel = new TaskModel(db, userId, workspaceId);
    this.taskTopicModel = new TaskTopicModel(db, userId, workspaceId);
    this.workModel = new WorkModel(db, userId, workspaceId);
  }

  // -------------------------------------------------------------------------
  // Submission
  // -------------------------------------------------------------------------

  /**
   * Submit on behalf of a running operation β€” the CLI path a heterogeneous
   * wrap-up agent takes, since server tools never reach a device run. The
   * operation is looked up with the caller's own ownership, and its topic must
   * be a run of this Goal's wrap-up Task, exactly as for the tool.
   */
  submitFromOperation = async (
    goalId: string,
    input: SubmitGoalReportInput,
    operationId: string,
  ) => {
    const operation = await this.operationModel.findOwnOperationById(operationId);
    if (!operation) throw new TRPCError({ code: 'NOT_FOUND', message: 'Operation not found' });
    return this.submit(goalId, input, {
      agentId: operation.agentId,
      operationId: operation.id,
      topicId: operation.topicId,
    });
  };

  /**
   * Validate and store one report version.
   *
   * When `caller` is given (the tool path), it must be a run of this Goal's
   * wrap-up Task β€” no other conversation can rewrite a Goal's report.
   */
  submit = async (goalId: string, input: SubmitGoalReportInput, caller?: GoalReportCaller) => {
    const graph = await this.coordinatorGraph.getGraph(goalId);
    if (!graph) throw new TRPCError({ code: 'NOT_FOUND', message: 'Goal not found' });
    const dispatch = graph.goal.config?.report;
    if (!dispatch)
      throw new TRPCError({
        code: 'PRECONDITION_FAILED',
        message: 'No wrap-up was dispatched for this Goal',
      });

    if (caller) {
      // Only the run of the CURRENT dispatch may write. A re-dispatch reuses the
      // Task and starts a new run, and the previous one can outlive a failed
      // cancel β€” accepting any run in the Task's history would let it land stale
      // content as the newest version.
      //
      // The dispatch records its operation only once the new run has started.
      // Until then the newest run may still be the superseded one, so a run
      // counts only if it was created after this dispatch was claimed; with
      // neither, nothing is current and every run is refused.
      const runs = dispatch.taskId ? await this.taskTopicModel.findByTaskId(dispatch.taskId) : [];
      const dispatchedAt = Date.parse(dispatch.dispatchedAt);
      const current = dispatch.operationId
        ? runs.find((run) => run.operationId === dispatch.operationId)
        : runs.find((run) => new Date(run.createdAt).getTime() >= dispatchedAt);
      if (!caller.topicId || current?.topicId !== caller.topicId)
        throw new TRPCError({
          code: 'FORBIDDEN',
          message: 'This conversation is not the current wrap-up run of this Goal',
        });
    }

    const eventRows = await this.db
      .select({ createdAt: goalEvents.createdAt, id: goalEvents.id })
      .from(goalEvents)
      .where(eq(goalEvents.goalId, goalId));
    const eventIds = new Set(eventRows.map((row) => row.id));
    const newestEvent = eventRows.sort(
      (a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime(),
    )[0];

    const parsed = GoalReportMetadataSchema.safeParse({
      ...input.metadata,
      graphCursor: input.metadata.graphCursor ?? newestEvent?.id,
    });
    if (!parsed.success)
      throw new TRPCError({
        code: 'BAD_REQUEST',
        message: `Invalid report: ${parsed.error.issues
          .map((issue) => `${issue.path.join('.')}: ${issue.message}`)
          .join('; ')}`,
      });
    const content = typeof input.content === 'string' ? input.content.trim() : '';
    if (!content)
      throw new TRPCError({
        code: 'BAD_REQUEST',
        message: 'Invalid report: content: required β€” the full markdown report is missing or empty',
      });

    // Reconcile the storyline with the graph before judging it: a weak default
    // model can narrate the whole path and still drop every detour, hang a
    // detour off a chapter's own nodeIds, or mark a mainline node it never
    // narrates. Those are recovered deterministically (see reconcileGoalReport);
    // any remaining invalid reference is still the agent's to fix.
    //
    // Reconciliation appends to `chapters[].detours`, so its result is parsed
    // again rather than trusted: a version that no longer satisfies the schema
    // is one `GoalReportStore.state` could never read back, and storing it would
    // hide the whole storyline behind a report that silently never loads.
    const reconciled = GoalReportMetadataSchema.safeParse(reconcileGoalReport(graph, parsed.data));
    if (!reconciled.success)
      throw new TRPCError({
        code: 'BAD_REQUEST',
        message: `Invalid report after reconciliation: ${reconciled.error.issues
          .map((issue) => `${issue.path.join('.')}: ${issue.message}`)
          .join('; ')}`,
      });
    const metadata = reconciled.data;
    const errors = validateGoalReport(graph, metadata, { eventIds });
    if (errors.length > 0)
      throw new TRPCError({
        code: 'BAD_REQUEST',
        message: `Invalid report references:\n${errors.join('\n')}`,
      });

    // The page is written only once the report itself is valid, and reused
    // across versions so a Goal keeps one report page.
    const html = typeof input.html === 'string' ? input.html.trim() : '';
    const pageDocumentId = html
      ? await writeGoalReportPage(
          { db: this.db, userId: this.userId, workspaceId: this.workspaceId },
          {
            html,
            previousPageId: (await this.workModel.findLatestGoalReport(goalId))?.metadata
              ?.goalReportPageId,
            title: `${graph.goal.title} Β· Report`,
          },
        )
      : undefined;

    const work = await this.workModel.registerGoalReport({
      agentId: caller?.agentId,
      content,
      goalId,
      metadata,
      pageDocumentId,
      rootOperationId: caller?.operationId,
      title: `Goal report: ${graph.goal.title}`,
      toolCallId: caller?.toolCallId,
      toolIdentifier: GoalReportIdentifier,
      toolName: GoalReportApiName.submitGoalReport,
      topicId: caller?.topicId,
    });
    if (work.currentVersionId) {
      await this.coordinatorGraph.attachWorkVersion(
        goalId,
        dispatch.nodeId,
        work.currentVersionId,
        'produced',
      );
    }
    await this.coordinatorGraph.updateNodeStatus(
      goalId,
      dispatch.nodeId,
      'resolved',
      'Goal report submitted',
    );
    const latest = await this.workModel.findLatestGoalReport(goalId);
    return { version: latest?.version, workId: work.id, workVersionId: work.currentVersionId };
  };

  // -------------------------------------------------------------------------
  // Read
  // -------------------------------------------------------------------------

  /** The wrap-up state for a graph read: in progress, completed or failed. */
  state = async (graph: GoalGraphSnapshot): Promise<GoalReportState | undefined> => {
    const dispatch = graph.goal.config?.report;
    if (!dispatch) return undefined;

    const latestRow = await this.workModel.findLatestGoalReport(graph.goal.id);
    const parsed = latestRow
      ? GoalReportMetadataSchema.safeParse(latestRow.metadata?.goalReport)
      : undefined;
    const latest =
      latestRow && parsed?.success
        ? {
            content: latestRow.content,
            createdAt: latestRow.createdAt,
            metadata: parsed.data,
            pageDocumentId: latestRow.metadata?.goalReportPageId,
            version: latestRow.version,
            workId: latestRow.workId,
            workVersionId: latestRow.workVersionId,
          }
        : undefined;

    return { dispatch, latest, status: await this.resolveStatus(dispatch, latest?.createdAt) };
  };

  private resolveStatus = async (
    dispatch: GoalReportDispatch,
    latestAt?: Date,
  ): Promise<GoalReportState['status']> => {
    const dispatchedAt = new Date(dispatch.dispatchedAt).getTime();
    if (latestAt && new Date(latestAt).getTime() >= dispatchedAt) return 'completed';
    if (dispatch.error) return 'failed';
    if (Date.now() - dispatchedAt > GOAL_REPORT_TIMEOUT_MS) return 'failed';
    if (!dispatch.taskId) return 'running';
    const task = await this.taskModel.findById(dispatch.taskId);
    if (!task) return 'failed';
    // The run ended β€” completed, failed, paused on an error β€” without a report.
    if (['completed', 'failed', 'canceled', 'paused'].includes(task.status)) return 'failed';
    return 'running';
  };
}