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.2 KB
Raw Blame
import type { AgentState, ToolRunResult } from '@lobechat/agent-runtime';
import { selectToolSourceMap } from '@lobechat/agent-runtime';
import type { SerializedAgentHook } from '@lobechat/types';
import { isRecord } from '@lobechat/utils/object';

import type { AgentOperationModel } from '@/database/models/agentOperation';
import type { MessageModel } from '@/database/models/message';
import { resolveRunActiveDeviceId } from '@/server/modules/AgentRuntime/executors/resolveRunActiveDeviceId';

import { type HookDispatcher, parseSerializedHooks } from './HookDispatcher';
import { controlToolResult, type ToolResultControlOutcome } from './toolResultControl';

export interface DeferredToolResultControlInput {
  /** Parent tool whose arguments produced a member anchor's result. */
  contextToolMessageId?: string;
  /** A following continuation round can evaluate the current environment instead. */
  includeServerHooks?: boolean;
  operationId?: string;
  /** Only server-built child usage, never arbitrary tool/user result fields. */
  preserveUsage?: boolean;
  result: ToolRunResult;
  signal?: AbortSignal;
  toolMessageId: string;
}

/** Reuse the existing private operation hook storage; never project it onto messages. */
export async function loadDurableToolResultHooks(
  operations: Pick<AgentOperationModel, 'findById'>,
  operationId: string,
): Promise<SerializedAgentHook[] | undefined> {
  const operation = await operations.findById(operationId);
  const hooks = operation?.metadata?._hooks;
  return Array.isArray(hooks) ? parseSerializedHooks(hooks) : undefined;
}

/**
 * Deferred completions do not return through ServerToolTransport. Recover their
 * authoritative call and parent hooks before writing the result into history.
 * No notification is added: this only extends explicitly configured controls.
 */
export async function controlDeferredToolResult(
  deps: {
    dispatcher: HookDispatcher;
    loadState: (operationId: string) => Promise<AgentState | null>;
    loadDurableHooks: (operationId: string) => Promise<SerializedAgentHook[] | undefined>;
    messageModel: Pick<MessageModel, 'findById' | 'findMessagePlugin'>;
    userId: string;
    workspaceId?: string;
  },
  input: DeferredToolResultControlInput,
): Promise<ToolResultControlOutcome> {
  const contextMessageId = input.contextToolMessageId ?? input.toolMessageId;
  const [message, persisted] = await Promise.all([
    deps.messageModel.findById(contextMessageId),
    deps.messageModel.findMessagePlugin(contextMessageId),
  ]);
  // Approval continuations have a new operation; the existing intervention
  // identity still locates the caller hooks of the original parked call.
  const operationIds = [
    ...new Set(
      [persisted?.intervention?.operationId, input.operationId].filter((id): id is string =>
        Boolean(id),
      ),
    ),
  ];
  let outcome: ToolResultControlOutcome = {
    blocked: false,
    cancelled: false,
    result: input.result,
  };
  const rounds = operationIds.length ? operationIds : [undefined];
  for (const [index, operationId] of rounds.entries()) {
    outcome = await evaluate(
      operationId,
      input.includeServerHooks !== false && index === rounds.length - 1,
    );
    if (outcome.blocked || outcome.cancelled) return outcome;
  }
  return outcome;

  async function evaluate(
    operationId: string | undefined,
    includeServerHooks: boolean,
  ): Promise<ToolResultControlOutcome> {
    const state = operationId ? await deps.loadState(operationId) : null;
    let hooks = state?.host?.hooks;
    if (!state && operationId) {
      hooks = await deps.loadDurableHooks(operationId);
    }
    if (!deps.dispatcher.hasAfterToolCallControl(operationId ?? '', hooks, includeServerHooks))
      return {
        blocked: false,
        cancelled: false,
        result: input.result,
      };
    const plugin = persisted;
    if (
      !operationId ||
      !message?.parentId ||
      !plugin?.toolCallId ||
      !plugin.identifier ||
      !plugin.apiName
    ) {
      // Do not acknowledge an unreviewed backfill when its authoritative call
      // cannot be reconstructed. A durable callback can retry the same result.
      throw new Error('Cannot evaluate afterToolCall: tool call context is unavailable');
    }
    let args: Record<string, unknown> = {};
    try {
      const parsed: unknown = JSON.parse(plugin.arguments ?? '{}');
      if (isRecord(parsed)) args = parsed;
    } catch {
      // Match the synchronous tool context: malformed arguments have an empty preview.
    }
    const toolName = `${plugin.identifier}/${plugin.apiName}`;
    const controlled = await controlToolResult(
      deps.dispatcher,
      {
        activeDeviceId: state ? resolveRunActiveDeviceId(state) : undefined,
        agentId: state?.origin?.agentId ?? message.agentId ?? undefined,
        apiName: plugin.apiName,
        args,
        assistantMessageId: message.parentId,
        callIndex:
          (state?.usage?.tools?.byTool?.find((tool) => tool.name === toolName)?.calls ?? 0) + 1,
        documentId: state?.origin?.documentId,
        executionTarget: state?.plan?.execution?.target,
        executor: 'server',
        groupId: state?.origin?.groupId ?? message.groupId ?? undefined,
        identifier: plugin.identifier,
        mocked: false,
        operationId,
        parentOperationId: state?.origin?.lineage?.parentOperationId,
        result: input.result,
        sessionId: state?.origin?.sessionId ?? message.sessionId ?? undefined,
        sourceMessageId: state?.origin?.sourceMessageId,
        stepIndex: Math.max(0, (state?.stepCount ?? 1) - 1),
        taskId: state?.origin?.taskId,
        threadId: state?.origin?.threadId ?? message.threadId ?? undefined,
        toolCallId: plugin.toolCallId,
        toolMessageId: input.toolMessageId,
        toolSource: state ? selectToolSourceMap(state)[plugin.identifier] : undefined,
        topicId: state?.origin?.topicId ?? message.topicId ?? undefined,
        userId: deps.userId,
        workspaceId: state?.origin?.workspaceId ?? deps.workspaceId,
      },
      hooks,
      input.signal,
      { includeServerHooks, preserveUsage: input.preserveUsage },
    );
    if (controlled.blocked && plugin.state?.onComplete === 'finish') {
      controlled.result.state!.onComplete = 'finish';
    }
    return controlled;
  }
}