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.

12.9 KB
Raw Blame
import { HOME_FEED_EXCLUDED_BRIEF_TYPES, NEWS_BRIEF_TYPES } from '@lobechat/types';
import { and, desc, eq, gte, inArray, isNull, lt, notInArray, type SQL, sql } from 'drizzle-orm';

import { agents } from '../schemas/agent';
import type { BriefItem, NewBrief } from '../schemas/task';
import { briefs, tasks } from '../schemas/task';
import type { LobeChatDatabase } from '../type';
import { normalizeInboxAgentAvatar, normalizeInboxAgentTitle } from '../utils/inboxAgent';
import { notTrashed } from '../utils/softDelete';
import { buildWorkspacePayload } from '../utils/workspace';

export interface UnresolvedBriefRow {
  agentAvatar: string | null;
  agentBackgroundColor: string | null;
  agentName: string | null;
  agentRowId: string | null;
  agentTitle: string | null;
  brief: BriefItem;
  /** Workspace-scoped task ref (`T-12`), so an inbox row can name the task it belongs to. */
  taskIdentifier: string | null;
  taskName: string | null;
  taskStatus: string | null;
}

export class BriefModel {
  private readonly userId: string;
  private readonly db: LobeChatDatabase;
  private readonly workspaceId?: string;

  constructor(db: LobeChatDatabase, userId: string, workspaceId?: string) {
    this.db = db;
    this.userId = userId;
    this.workspaceId = workspaceId;
  }

  // Briefs are per-user notifications (owner-only `readAt` / `resolvedAction` /
  // `resolvedAt`), not workspace-shared content. The standard `buildWorkspaceWhere`
  // helper drops the `user_id` constraint in workspace mode by design (members
  // share content rows), which would leak each member's briefs to everyone else
  // in the workspace. Brief ownership therefore always requires `user_id` to
  // match, in both personal and workspace mode.
  ownership = (): SQL =>
    this.workspaceId
      ? (and(eq(briefs.userId, this.userId), eq(briefs.workspaceId, this.workspaceId)) as SQL)
      : (and(eq(briefs.userId, this.userId), isNull(briefs.workspaceId)) as SQL);

  async create(data: Omit<NewBrief, 'id' | 'userId'>): Promise<BriefItem> {
    const result = await this.db
      .insert(briefs)
      .values(
        buildWorkspacePayload({ userId: this.userId, workspaceId: this.workspaceId }, { ...data }),
      )
      .returning();

    return result[0];
  }

  async findById(id: string): Promise<BriefItem | null> {
    const result = await this.db
      .select()
      .from(briefs)
      .where(and(eq(briefs.id, id), this.ownership()))
      .limit(1);

    return result[0] || null;
  }

  async list(options?: {
    limit?: number;
    offset?: number;
    type?: string;
  }): Promise<{ briefs: BriefItem[]; total: number }> {
    const { type, limit = 50, offset = 0 } = options || {};

    const conditions = [this.ownership()];
    if (type) conditions.push(eq(briefs.type, type));

    const where = and(...conditions);

    const countResult = await this.db
      .select({ count: sql<number>`count(*)` })
      .from(briefs)
      .where(where);

    const items = await this.db
      .select()
      .from(briefs)
      .where(where)
      .orderBy(desc(briefs.createdAt))
      .limit(limit)
      .offset(offset);

    return { briefs: items, total: Number(countResult[0].count) };
  }

  /**
   * Home Daily Brief feed: unresolved briefs sorted by priority, joined
   * with the producing agent + parent task in a single SQL. Capped at 20
   * so heavy-inbox users don't pay the full enrich cost on every home
   * render — the rest is reachable via the task list page.
   */
  async listUnresolvedEnriched(options?: { limit?: number }): Promise<UnresolvedBriefRow[]> {
    const { limit = 20 } = options ?? {};
    const rows = await this.db
      .select(this.enrichedSelection())
      .from(briefs)
      .leftJoin(agents, eq(briefs.agentId, agents.id))
      .leftJoin(tasks, eq(briefs.taskId, tasks.id))
      .where(
        and(
          this.ownership(),
          isNull(briefs.resolvedAt),
          notInArray(briefs.type, HOME_FEED_EXCLUDED_BRIEF_TYPES),
          // Briefs hide with their task while it sits in the recycle bin
          // (a brief without a task joins NULL, which passes).
          notTrashed(tasks.isDeleted),
        ),
      )
      .orderBy(
        sql`CASE
          WHEN ${briefs.priority} = 'urgent' THEN 0
          WHEN ${briefs.priority} = 'normal' THEN 1
          ELSE 2
        END`,
        desc(briefs.createdAt),
      )
      .limit(limit);

    return this.normalizeEnrichedRows(rows);
  }

  /**
   * Day-scoped news digest (`insight` + `result`): every brief created in
   * `[startAt, endAt)`, resolved or not. A day's digest is a record, not a
   * queue — dropping resolved rows would make every already-read day come
   * back empty. Plain chronological order: within one day the priority
   * buckets of the unresolved feed carry no meaning.
   *
   * The 50 cap is a deliberate scope cut, newest-first: a single user
   * producing 50+ news briefs in one local day is far outside current
   * product reality, and the day pager carries no same-day cursor. Revisit
   * with real pagination if daily volumes ever approach it.
   */
  async listNewsEnriched(options: {
    endAt: Date;
    limit?: number;
    startAt: Date;
  }): Promise<UnresolvedBriefRow[]> {
    const { endAt, limit = 50, startAt } = options;
    const rows = await this.db
      .select(this.enrichedSelection())
      .from(briefs)
      .leftJoin(agents, eq(briefs.agentId, agents.id))
      .leftJoin(tasks, eq(briefs.taskId, tasks.id))
      .where(
        and(
          this.ownership(),
          inArray(briefs.type, NEWS_BRIEF_TYPES),
          notTrashed(tasks.isDeleted),
          gte(briefs.createdAt, startAt),
          lt(briefs.createdAt, endAt),
        ),
      )
      .orderBy(desc(briefs.createdAt))
      .limit(limit);

    return this.normalizeEnrichedRows(rows);
  }

  /**
   * Whether any news brief exists before `date` — lets the day pager disable
   * its "older" arrow at the true start of history instead of paging into
   * empty days forever.
   */
  async hasNewsBefore(date: Date): Promise<boolean> {
    const rows = await this.db
      .select({ id: briefs.id })
      .from(briefs)
      .where(
        and(this.ownership(), inArray(briefs.type, NEWS_BRIEF_TYPES), lt(briefs.createdAt, date)),
      )
      .limit(1);

    return rows.length > 0;
  }

  private enrichedSelection = () => ({
    agentAvatar: agents.avatar,
    agentBackgroundColor: agents.backgroundColor,
    agentRowId: agents.id,
    agentName: agents.name,
    agentSlug: agents.slug,
    agentTitle: agents.title,
    brief: briefs,
    taskIdentifier: tasks.identifier,
    taskName: tasks.name,
    taskStatus: tasks.status,
  });

  private normalizeEnrichedRows = (
    rows: (UnresolvedBriefRow & { agentSlug: string | null })[],
  ): UnresolvedBriefRow[] =>
    rows.map(({ agentSlug, ...row }) => ({
      ...row,
      agentAvatar: normalizeInboxAgentAvatar(row.agentAvatar, {
        slug: agentSlug,
      }),
      agentTitle: normalizeInboxAgentTitle(row.agentTitle, {
        slug: agentSlug,
      }),
    }));

  /**
   * Lists unresolved briefs for one agent and trigger before applying the read cap.
   *
   * Use when:
   * - Server-side collectors need a bounded, purpose-specific Daily Brief read
   * - Callers must not let unrelated unresolved briefs consume the limit
   *
   * Expects:
   * - `agentId` and `trigger` identify the proposal or signal boundary
   * - `limit` is a small bounded read budget
   *
   * Returns:
   * - Matching unresolved brief rows ordered newest first
   */
  async listUnresolvedByAgentAndTrigger({
    agentId,
    limit = 20,
    trigger,
  }: {
    agentId: string;
    limit?: number;
    trigger: string;
  }): Promise<BriefItem[]> {
    return this.db
      .select()
      .from(briefs)
      .where(
        and(
          this.ownership(),
          eq(briefs.agentId, agentId),
          eq(briefs.trigger, trigger),
          isNull(briefs.resolvedAt),
        ),
      )
      .orderBy(desc(briefs.createdAt))
      .limit(limit);
  }

  async findByTaskId(taskId: string): Promise<BriefItem[]> {
    return this.db
      .select()
      .from(briefs)
      .where(and(eq(briefs.taskId, taskId), this.ownership()))
      .orderBy(desc(briefs.createdAt));
  }

  // Used by heartbeat re-arm to skip rescheduling when a task is already
  // waiting on user action (review max-iter etc). Optionally exclude brief
  // types — heartbeat callers exclude `error` because transient errors are
  // governed by the fuse counter, not by the existence of the error brief
  // itself (otherwise the very first error would block all retries).
  async hasUnresolvedUrgentByTask(
    taskId: string,
    options?: { excludeTypes?: string[] },
  ): Promise<boolean> {
    const excludeTypes = options?.excludeTypes ?? [];
    const conditions = [
      this.ownership(),
      eq(briefs.taskId, taskId),
      eq(briefs.priority, 'urgent'),
      isNull(briefs.resolvedAt),
    ];
    if (excludeTypes.length > 0) {
      conditions.push(notInArray(briefs.type, excludeTypes));
    }

    const rows = await this.db
      .select({ id: briefs.id })
      .from(briefs)
      .where(and(...conditions))
      .limit(1);
    return rows.length > 0;
  }

  /**
   * Which of `taskIds` have an unresolved urgent brief — the batch form of
   * {@link hasUnresolvedUrgentByTask}, for lists that mark the tasks whose
   * automation is held back until the user answers.
   */
  async findTaskIdsWithUnresolvedUrgent(
    taskIds: string[],
    options?: { excludeTypes?: string[] },
  ): Promise<Set<string>> {
    if (taskIds.length === 0) return new Set();

    const excludeTypes = options?.excludeTypes ?? [];
    const conditions = [
      this.ownership(),
      inArray(briefs.taskId, taskIds),
      eq(briefs.priority, 'urgent'),
      isNull(briefs.resolvedAt),
    ];
    if (excludeTypes.length > 0) {
      conditions.push(notInArray(briefs.type, excludeTypes));
    }

    const rows = await this.db
      .selectDistinct({ taskId: briefs.taskId })
      .from(briefs)
      .where(and(...conditions));
    return new Set(rows.map((row) => row.taskId).filter((id): id is string => !!id));
  }

  async findByCronJobId(cronJobId: string): Promise<BriefItem[]> {
    return this.db
      .select()
      .from(briefs)
      .where(and(eq(briefs.cronJobId, cronJobId), this.ownership()))
      .orderBy(desc(briefs.createdAt));
  }

  async markRead(id: string): Promise<BriefItem | null> {
    const result = await this.db
      .update(briefs)
      .set({ readAt: new Date() })
      .where(and(eq(briefs.id, id), this.ownership()))
      .returning();

    return result[0] || null;
  }

  async resolve(
    id: string,
    options?: { action?: string; comment?: string },
  ): Promise<BriefItem | null> {
    const result = await this.db
      .update(briefs)
      .set({
        readAt: new Date(),
        resolvedAction: options?.action,
        resolvedAt: new Date(),
        resolvedComment: options?.comment,
      })
      .where(and(eq(briefs.id, id), this.ownership()))
      .returning();

    return result[0] || null;
  }

  /**
   * Bulk "mark all read": resolves the given briefs with a neutral `read`
   * action so they leave the unresolved feed. Deliberately bypasses
   * `BriefService.resolve()` — a bulk dismissal must never trigger the
   * approve-completes-task lifecycle; only a single explicit `approve` may.
   * Already-resolved briefs are skipped so a stale client list cannot
   * overwrite an earlier, more meaningful resolution.
   *
   * Returns the ids that were actually resolved.
   */
  async resolveManyAsRead(ids: string[]): Promise<string[]> {
    if (ids.length === 0) return [];

    const now = new Date();
    const rows = await this.db
      .update(briefs)
      .set({
        // Keep the first-read timestamp when the brief was already opened.
        readAt: sql`COALESCE(${briefs.readAt}, ${now})`,
        resolvedAction: 'read',
        resolvedAt: now,
      })
      .where(and(inArray(briefs.id, ids), this.ownership(), isNull(briefs.resolvedAt)))
      .returning({ id: briefs.id });

    return rows.map((row) => row.id);
  }

  /**
   * Updates freeform brief metadata without resolving the brief.
   *
   * Use when:
   * - Server workflows need to persist intermediate Daily Brief state
   * - A proposal approve attempt must remain visible after stale or failed preflight
   *
   * Expects:
   * - `metadata` is already validated by the caller-owned feature boundary
   *
   * Returns:
   * - The updated brief row, or `null` when the brief no longer exists
   */
  async updateMetadata(id: string, metadata: BriefItem['metadata']): Promise<BriefItem | null> {
    const result = await this.db
      .update(briefs)
      .set({ metadata })
      .where(and(eq(briefs.id, id), this.ownership()))
      .returning();

    return result[0] || null;
  }

  async delete(id: string): Promise<boolean> {
    const result = await this.db
      .delete(briefs)
      .where(and(eq(briefs.id, id), this.ownership()))
      .returning();

    return result.length > 0;
  }
}