Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

534 B
Raw Blame
const OPENERS = { "[": "]", "(": ")" };

export const utilityOf = (token) => {
  const closers = [];
  const lastTopLevelColon = [...token].reduce((found, ch, i) => {
    if (closers.length > 0 && ch === closers[closers.length - 1]) {
      closers.pop();
      return found;
    }
    if (ch in OPENERS) {
      closers.push(OPENERS[ch]);
      return found;
    }
    return ch === ":" && closers.length === 0 ? i : found;
  }, -1);
  return token
    .slice(lastTopLevelColon + 1)
    .replace(/^!/, "")
    .replace(/!$/, "");
};