Skip to content

Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active

2.1 KB
Raw Blame
import { Constants, ContentTypes } from 'librechat-data-provider';
import type { SubagentIdentity, TMessage } from 'librechat-data-provider';
import { parseSubagentBackgroundHandle } from '../parts';

export type SubagentDispatch = {
  message: TMessage;
  subagentType: string;
  identity?: SubagentIdentity;
  agentId?: string;
};

const indexes = new WeakMap<TMessage[], Map<string, SubagentDispatch>>();

/** Public transcripts carry dispatch metadata instead of an authenticated child
 *  index. Match the host-issued handle, sharing one scan per transcript snapshot. */
export function findSubagentDispatch(
  messages: TMessage[] | undefined,
  threadId: string | undefined,
): SubagentDispatch | undefined {
  if (messages == null || !threadId) return undefined;
  let index = indexes.get(messages);
  if (index == null) {
    index = new Map();
    for (const message of messages) {
      if (message.isCreatedByUser === true) continue;
      for (const part of message.content ?? []) {
        if (part?.type !== ContentTypes.TOOL_CALL) continue;
        const call = part.tool_call;
        if (!('name' in call) && !('function' in call)) continue;
        const name = 'function' in call ? call.function.name : call.name;
        if (name !== Constants.SUBAGENT) continue;
        const output = 'function' in call ? call.function.output : call.output;
        let args: Parameters<typeof parseSubagentBackgroundHandle>[1];
        if ('function' in call) {
          args =
            typeof call.function.arguments === 'string'
              ? call.function.arguments
              : JSON.stringify(call.function.arguments);
        } else {
          args = call.args;
        }
        const handle = parseSubagentBackgroundHandle(output, args);
        if (handle == null || index.has(handle.subagent_thread_id)) continue;
        index.set(handle.subagent_thread_id, {
          message,
          subagentType: handle.subagent_type,
          identity: call.subagentIdentity,
          agentId: part.agentId,
        });
      }
    }
    indexes.set(messages, index);
  }
  return index.get(threadId);
}