Skip to content

Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors

1.3 KB
Raw Blame
import { resolveRequestToolIdentity } from "./requestToolIdentity.ts";
import { plaintextCollaborationFields } from "./collaborationPlaintextMarker.ts";

interface FunctionCallItemLike {
  namespace?: string;
  name?: string;
  encrypted_function_args?: unknown[];
  [key: string]: unknown;
}

/**
 * #7936 identity closure: rewrite a function_call item's `name` back to its
 * bare leaf and stamp the original `namespace` alongside it, matching the
 * codex ResponseItem::FunctionCall schema (independent `namespace` field, NOT
 * a `__` split on `name`). #14154: once the identity is restored, also stamp
 * the `encrypted_function_args: []` plaintext-delivery marker Codex requires
 * for collaboration spawn_agent/send_message/followup_task calls.
 *
 * Mutates `funcItem` in place and returns it for convenience at call sites.
 */
export function applyFunctionCallIdentity<T extends FunctionCallItemLike>(
  funcItem: T,
  identityMap: unknown,
  toolName: string,
  options: { explicitNames?: Iterable<string> | null } = {}
): T {
  const identity = resolveRequestToolIdentity(identityMap, toolName, options);
  if (identity) {
    funcItem.namespace = identity.namespace;
    funcItem.name = identity.name;
  }
  Object.assign(funcItem, plaintextCollaborationFields(funcItem.namespace, funcItem.name));
  return funcItem;
}