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.

6.0 KB
Raw Blame
import { randomUUID } from 'node:crypto';

import { parseTopicScheduledRun } from '@lobechat/types';
import debug from 'debug';

import { TopicModel } from '@/database/models/topic';
import { getServerDB } from '@/database/server';
import { appEnv } from '@/envs/app';
import { QueueService } from '@/server/services/queue';
import { LocalQueueServiceImpl } from '@/server/services/queue/impls';

import { dispatchWorktreeTurn } from './dispatchWorktreeTurn';
import { dispatchScheduledRun } from './scheduledRunKinds';

const log = debug('lobe-server:workflows:task:scheduled-topic-dispatch');

/** How long a claim lease is held before another tick may re-claim the topic. */
const CLAIM_LEASE_MS = 5 * 60 * 1000;

export interface ScheduledTopicDispatchPayload {
  /** When true, only report which topics are due without claiming/dispatching. */
  dryRun?: boolean;
  /** An immediate wakeup can target one topic; a scheduled sweep targets all. */
  topicId?: string;
}

/**
 * Cron-style dispatcher for deferred runs on pure (non-Task) topics. Registered
 * as a QStash Schedule (e.g. `*\/10 * * * *`) pointing at this endpoint. On each
 * tick:
 *
 *   1. Loads topics with `status = 'scheduled'` whose `scheduledRun.runAt` has
 *      passed and that aren't under a live claim.
 *   2. Atomically claims each (a lease on `metadata.scheduledRun.claim`) so two
 *      ticks / replicas never dispatch the same run twice.
 *   3. Hands the run to its kind handler, which re-enters
 *      `AiAgentService.execAgent` β€” the same execution engine as a normal agent
 *      run, which already routes remote runs to the topic's bound device via the
 *      device gateway.
 *
 * Everything here is kind-agnostic: scanning, the lease and clearing state are
 * shared, and a {@link dispatchScheduledRun} handler decides only *what* to run.
 *
 * This intentionally does NOT enter TaskLifecycle β€” a pure topic just runs its
 * agent. On success the scheduled state is cleared; on failure (e.g. the device
 * is offline) the topic stays `scheduled` and the claim lease expires so the next
 * tick retries β€” the scheduled state is never lost.
 */
export async function dispatchDueScheduledTopics({
  dryRun = false,
  topicId,
}: ScheduledTopicDispatchPayload = {}) {
  const db = await getServerDB();
  const now = new Date();
  const due = topicId
    ? await TopicModel.getDueScheduledTopics(db, now, topicId)
    : await TopicModel.getDueScheduledTopics(db, now);

  log('scan: due=%d dryRun=%s', due.length, dryRun);

  if (dryRun || due.length === 0) {
    return { claimed: 0, dispatched: 0, dryRun, due: due.length, success: true };
  }

  let claimed = 0;
  let dispatched = 0;
  let discarded = 0;
  for (const topic of due) {
    // Reads the current payload and the pre-`kind` legacy one alike β€” the due
    // query selects both, so this must dispatch both.
    const run = parseTopicScheduledRun(topic.metadata?.scheduledRun);
    if (!run) {
      // A due topic whose payload we can't dispatch would re-surface on every
      // tick forever, so drop the schedule instead of skipping it. Back to
      // `active`: the topic itself is intact, only the schedule is unusable.
      console.error(
        '[scheduled-topic-dispatch] discarding unparseable scheduledRun topic=%s: %O',
        topic.id,
        topic.metadata?.scheduledRun,
      );
      await TopicModel.clearScheduledRun(db, topic.id, 'active').catch(() => undefined);
      discarded += 1;
      continue;
    }

    const claim = {
      claimedAt: now.toISOString(),
      expiresAt: new Date(now.getTime() + CLAIM_LEASE_MS).toISOString(),
      id: randomUUID(),
    };

    const won = await TopicModel.claimScheduledTopic(db, topic.id, claim, now).catch((err) => {
      console.error('[scheduled-topic-dispatch] claim failed topic=%s: %O', topic.id, err);
      return false;
    });
    if (!won) continue;
    claimed += 1;

    const workspaceId = topic.workspaceId ?? undefined;
    try {
      if (run.kind === 'worktree_start') {
        const result = await dispatchWorktreeTurn({ ...run, claim }, { db, topic, workspaceId });
        await TopicModel.finishWorktreeStart(db, topic.id, claim.id, result);
        if (result.success) dispatched += 1;
        if (result.retryAt) await wakeWorktreeTurn(topic.id, result.retryAt);
        continue;
      }
      const result = await dispatchScheduledRun(run, {
        claimId: claim.id,
        db,
        topic,
        workspaceId,
      });
      if (!result.success) {
        throw new Error(result.error || result.message || 'Scheduled run dispatch failed');
      }

      // Success β€” execAgent now owns the run; clear the scheduled state.
      await TopicModel.clearScheduledRun(db, topic.id, 'running', claim.id);
      dispatched += 1;
      log(
        'dispatched topic=%s kind=%s device=%s',
        topic.id,
        run.kind,
        topic.metadata?.boundDeviceId,
      );
    } catch (err) {
      // Leave the topic `scheduled` (e.g. device offline). The claim lease
      // expires and the next tick retries β€” scheduled state is never lost.
      console.error('[scheduled-topic-dispatch] dispatch failed topic=%s: %O', topic.id, err);
    }
  }

  return { claimed, discarded, dispatched, due: due.length, success: true };
}

/** Fast path. The scheduled-topic scan can also recover a committed send after an enqueue failure. */
export const wakeWorktreeTurn = async (topicId: string, runAt?: string): Promise<void> => {
  const queue = new QueueService();
  const impl = queue.getImpl();
  if (impl instanceof LocalQueueServiceImpl) {
    impl.setExecutionCallback(async () => {
      await dispatchDueScheduledTopics({ topicId });
    });
  }
  await queue.scheduleMessage({
    deduplicationId: `first-send-worktree:${topicId}:${runAt ?? 'now'}`,
    delay: runAt ? Math.max(1000, new Date(runAt).getTime() - Date.now() + 1000) : undefined,
    endpoint: new URL(
      '/api/workflows/task/scheduled-topic-dispatch',
      appEnv.INTERNAL_APP_URL || appEnv.APP_URL,
    ).toString(),
    operationId: topicId,
    payload: { topicId },
    retries: 3,
    stepIndex: 0,
  });
};