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.

7.6 KB
Raw Blame
import { llmRelayChannelScope } from '@lobechat/agent-gateway-client';
import type { SubAgentExecutionResult, SubAgentTransport } from '@lobechat/agent-runtime';
import type {
  ExecSubAgentParams,
  ExecSubAgentResult,
  ExecVirtualSubAgentParams,
} from '@lobechat/types';

import { getBusinessTrpcHeaders } from '@/business/client/trpc-headers';
import type { ExecSubAgentTaskParams } from '@/services/aiAgent';
import { aiAgentService } from '@/services/aiAgent';
import { buildLlmExecutorDeclaration, oneShotRelay } from '@/services/llmRelay';
import type { ChatStore } from '@/store/chat/store';

const DEFAULT_TIMEOUT_MS = 1_800_000;
const POLL_INTERVAL_MS = 3000;

const getErrorMessage = (error: unknown): string => {
  if (error instanceof Error) return error.message;
  if (typeof error === 'string') return error;
  return 'Unknown error';
};

const waitForNextPoll = (delayMs: number, signal: AbortSignal): Promise<void> => {
  if (signal.aborted) return Promise.resolve();

  return new Promise((resolve) => {
    const onAbort = () => {
      clearTimeout(timer);
      resolve();
    };
    const timer = setTimeout(() => {
      signal.removeEventListener('abort', onAbort);
      resolve();
    }, delayMs);

    signal.addEventListener('abort', onAbort, { once: true });
  });
};

/** Client adapter that waits for a server-backed sub-agent task to finish. */
export class ClientSubAgentTransport implements SubAgentTransport {
  constructor(
    private readonly get: () => ChatStore,
    private readonly operationId: string,
    private readonly options: {
      /** The server accepted the child; called before waiting on it. */
      onDispatched?: (dispatch: ExecSubAgentResult) => void;
    } = {},
  ) {}

  execSubAgent(params: ExecSubAgentParams): Promise<SubAgentExecutionResult> {
    return this.execute(params);
  }

  execVirtualSubAgent(params: ExecVirtualSubAgentParams): Promise<SubAgentExecutionResult> {
    return this.execute(params);
  }

  /**
   * The child runs on the server, but a device-only model (a local Ollama, any
   * `fetchOnClient` provider) can only run on this tab — and no server run
   * here has an executor for the child to inherit. So this tab stands by on a
   * relay channel of its own for as long as it waits on the child, and names
   * it in the dispatch; the server relays the child's LLM calls on it. Which
   * model the child uses is the server's call, so any device provider this tab
   * runs is reason enough. Both are read once the provider runtime state has
   * loaded, so a mention sent right after the page opens still stands by.
   */
  private async execute(params: ExecSubAgentParams): Promise<SubAgentExecutionResult> {
    // The server accepts only a channel bound to the workspace it runs the
    // dispatch in — the one the business headers name.
    const workspaceId = (await getBusinessTrpcHeaders())['X-Workspace-Id'];

    return oneShotRelay.run(
      () => buildLlmExecutorDeclaration()?.providers ?? [],
      async (relay) => {
        const llmExecutor = relay && buildLlmExecutorDeclaration();
        if (!relay || !llmExecutor) return this.dispatchAndWait(params);

        try {
          return await this.dispatchAndWait({
            ...params,
            llmExecutor,
            llmRelayChannel: relay.channel,
          });
        } finally {
          void aiAgentService
            .releaseSubAgentLlmRelay({ channel: relay.channel })
            .catch(() => undefined);
        }
      },
      { scope: llmRelayChannelScope(workspaceId) },
    );
  }

  private async dispatchAndWait(params: ExecSubAgentTaskParams): Promise<SubAgentExecutionResult> {
    let dispatchResult: Awaited<ReturnType<typeof aiAgentService.execSubAgentTask>> | undefined;
    let interruptPromise: Promise<void> | undefined;
    const interrupt = () => {
      if (!interruptPromise) {
        interruptPromise = (async () => {
          if (!dispatchResult?.threadId) return;

          try {
            await aiAgentService.interruptTask({ threadId: dispatchResult.threadId });
          } catch {
            // The terminal result still needs to reach the parent when interruption fails.
          }
        })();
      }

      return interruptPromise;
    };

    try {
      dispatchResult = await aiAgentService.execSubAgentTask(params);
      if (!dispatchResult.success) {
        return {
          ...dispatchResult,
          error: dispatchResult.error ?? 'Sub-agent dispatch failed',
          status: 'failed',
        };
      }

      const operation = this.get().operations[this.operationId];
      if (!operation) throw new Error(`Operation not found: ${this.operationId}`);

      this.options.onDispatched?.(dispatchResult);

      const signal = operation.abortController.signal;
      const timeoutMs = params.timeout ?? DEFAULT_TIMEOUT_MS;
      const startedAt = Date.now();

      const onAbort = () => void interrupt();
      signal.addEventListener('abort', onAbort, { once: true });

      try {
        while (Date.now() - startedAt < timeoutMs) {
          const currentOperation = this.get().operations[this.operationId];
          if (signal.aborted || currentOperation?.status === 'cancelled') {
            await interrupt();
            return {
              ...dispatchResult,
              error: 'Operation cancelled',
              status: 'cancelled',
              success: false,
            };
          }

          const taskStatus = await aiAgentService.getSubAgentTaskStatus({
            threadId: dispatchResult.threadId,
          });

          if (signal.aborted || this.get().operations[this.operationId]?.status === 'cancelled') {
            await interrupt();
            return {
              ...dispatchResult,
              error: 'Operation cancelled',
              status: 'cancelled',
              success: false,
            };
          }

          if (taskStatus.taskDetail) {
            this.get().internal_dispatchMessage(
              {
                id: params.parentMessageId,
                type: 'updateMessage',
                value: { taskDetail: taskStatus.taskDetail },
              },
              { operationId: this.operationId },
            );
          }

          if (taskStatus.status === 'completed') {
            return {
              ...dispatchResult,
              result: taskStatus.result,
              status: 'completed',
              success: true,
            };
          }

          if (taskStatus.status === 'failed') {
            return {
              ...dispatchResult,
              error: taskStatus.error ?? 'Unknown error',
              status: 'failed',
              success: false,
            };
          }

          if (taskStatus.status === 'cancel') {
            return {
              ...dispatchResult,
              error: 'Task was cancelled',
              status: 'cancelled',
              success: false,
            };
          }

          const remainingMs = timeoutMs - (Date.now() - startedAt);
          await waitForNextPoll(Math.min(POLL_INTERVAL_MS, remainingMs), signal);
        }

        await interrupt();
        return {
          ...dispatchResult,
          error: `Task timeout after ${timeoutMs}ms`,
          status: 'timed_out',
          success: false,
        };
      } finally {
        signal.removeEventListener('abort', onAbort);
      }
    } catch (error) {
      if (dispatchResult?.success) await interrupt();

      return {
        assistantMessageId: dispatchResult?.assistantMessageId ?? '',
        error: getErrorMessage(error),
        operationId: dispatchResult?.operationId ?? '',
        status: 'failed',
        success: false,
        threadId: dispatchResult?.threadId ?? '',
      };
    }
  }
}