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.

3.0 KB
Raw Blame
import { LobeAgentApiName, LobeAgentIdentifier } from './types';

/**
 * Whether a tool call is `lobe-agent.callSubAgent` β€” the only call whose
 * result carries the sub-agent reference and whose sub-agent can be continued
 * (`callAgent` children and group members cannot).
 */
export const isCallSubAgentCall = (
  plugin?: { apiName?: string | null; identifier?: string | null } | null,
): boolean =>
  plugin?.identifier === LobeAgentIdentifier && plugin.apiName === LobeAgentApiName.callSubAgent;

/**
 * Trailer appended to every server `callSubAgent` tool result. It is the
 * parent model's only handle on the sub-agent it just ran: passing the id back
 * as `callSubAgent({ subAgentId })` sends that same sub-agent a new turn on its
 * preserved history (continue after a failure, hand over partial findings,
 * follow-up questions). The id is the sub-agent's isolation thread id.
 *
 * The chat UI strips the trailer before rendering the result.
 */
/**
 * Starts at the literal tag instead of a leading `\s*`: an unanchored leading
 * `\s*` backtracks quadratically on long whitespace runs (CodeQL
 * js/polynomial-redos). Whitespace before the tag is trimmed in code instead.
 */
const SUB_AGENT_REFERENCE_PATTERN = /<sub_agent id="[^"]*" \/>\s*$/;

export const stripSubAgentReference = (content: string): string => {
  const match = SUB_AGENT_REFERENCE_PATTERN.exec(content);
  if (!match) return content;

  return content.slice(0, match.index).trimEnd();
};

export const appendSubAgentReference = (content: string, subAgentId: string): string => {
  const body = stripSubAgentReference(content);
  const reference = `<sub_agent id="${subAgentId}" />`;

  return body ? `${body}\n\n${reference}` : reference;
};

/**
 * Tool result of a background call (`callSubAgent` / `callAgent` with
 * `runInBackground`) β€” what the parent sees in place of the answer, which
 * arrives later as a message of its own. `subAgentId` is appended as the
 * follow-up handle when the sub-agent can be continued (`callSubAgent` only).
 */
export const buildBackgroundSubAgentReceipt = (subAgentId?: string): string => {
  const receipt =
    'Started in the background. The result will be delivered to you as a new message when it finishes β€” do not wait or poll for it. Continue with other work, or end your turn and tell the user what is running.';
  return subAgentId ? appendSubAgentReference(receipt, subAgentId) : receipt;
};

/**
 * Tool result of `callSubAgent({ subAgentId })` sent to a sub-agent that is
 * still running: the instruction joins that run instead of starting a new one,
 * so the answer comes the way the running call already reports it.
 */
export const buildSteeredSubAgentReceipt = (subAgentId: string): string =>
  appendSubAgentReference(
    'The sub-agent is still running, so this instruction was queued for it. It reads it at its next step and continues with it in mind; its result arrives the way its running call reports it β€” do not wait or poll for it here.',
    subAgentId,
  );