/** * @license * Copyright 2025 Steven Roussey * SPDX-License-Identifier: Apache-2.0 */ import type { ToolCallingTaskInput } from "../task/ToolCallingTask"; import type { ToolCalls } from "../task/ToolCallingUtils"; export interface ToolCall { readonly name: string; readonly arguments: Record; readonly id: string | null; } export interface ToolCallParserResult { readonly tool_calls: ReadonlyArray; readonly content: string; readonly parser: string; } export interface ParseToolCallsOptions { readonly tokenizer?: TokenizerLike | null; readonly model?: string | null; readonly parser?: string | null; } /** * Minimal tokenizer shape used for model-family detection. * Compatible with `PreTrainedTokenizer` from `@huggingface/transformers`. */ export interface TokenizerLike { readonly config?: { readonly name_or_path?: string; readonly _name_or_path?: string; readonly model_type?: string; }; readonly name_or_path?: string; } export type ParserFn = (text: string) => ToolCallParserResult | null; /** * Strip thinking blocks (`...`) and HFT special tokens * (`<|im_end|>`, `<|end_of_turn|>`, etc.) from model output. * Used to clean up content text returned alongside tool calls. */ export declare function stripModelArtifacts(text: string): string; /** * Extract text from a content block that may be a string, array of content * blocks, or other structure. */ export declare function extractMessageText(content: unknown): string; export declare function makeToolCall(name: string, args: Record, id?: string | null): ToolCall; export declare function tryParseJson(text: string): unknown | undefined; export declare function parseJsonToolCallArray(jsonStr: string, nameKey?: string, argsKeys?: ReadonlyArray): ReadonlyArray | undefined; /** * Parse key=value argument syntax used by Gorilla, NexusRaven, and Gemma. * Handles quoted strings (`"val"`, `'val'`) and bare values. */ export declare function parseKeyValueArgs(argsStr: string): Record; export declare function coerceArgValue(value: string): unknown; export declare function toolChoiceForcesToolCall(toolChoice: ToolCallingTaskInput["toolChoice"]): boolean; export declare function forcedToolSelection(input: ToolCallingTaskInput): string | undefined; export declare function resolveParsedToolName(name: string, input: ToolCallingTaskInput): string; /** * Convert a low-level parser result to the workglow `ToolCalls` type. * When `input` is provided, tool names are resolved against the available * tools list (and forced selection is applied for unrecognized names). */ export declare function adaptParserResult(result: ToolCallParserResult, input?: ToolCallingTaskInput): { text: string; toolCalls: ToolCalls; }; /** * Llama 3.1/3.2/3.3 (Meta) * * Formats: * - `<|python_tag|>{"name": "func", "parameters": {"arg": "val"}}` * - `{"arg": "val"}` (3.2 lightweight 1B/3B) * - `{"name": "func", "parameters": {...}}` (bare JSON) */ export declare const parseLlama: ParserFn; /** * Mistral / Mixtral (Mistral AI) * * Format: `[TOOL_CALLS] [{"name": "func", "arguments": {...}, "id": "9charID"}]` */ export declare const parseMistral: ParserFn; /** * Hermes (NousResearch) — also used by Qwen 2.5, Qwen 3, SOLAR, and others * * Format: `\n{"name": "func", "arguments": {...}}\n` */ export declare const parseHermes: ParserFn; /** * Cohere Command-R / Command-R+ * * Formats: * - `Action: ```json\n[{"tool_name": "func", "parameters": {...}}]\n```` * - `Action: [{"tool_name": ..., "parameters": ...}]` */ export declare const parseCohere: ParserFn; export declare const parseDeepSeek: ParserFn; /** * Phi-4 / Phi-4-mini (Microsoft) * * Format: `<|tool_calls|>[{"name": "func", "arguments": {...}}]<|/tool_calls|>` */ export declare const parsePhi: ParserFn; /** * Phi-3 functools format (legacy) * * Format: `functools[{"name": "func", "arguments": {...}}]` */ export declare const parsePhiFunctools: ParserFn; /** * InternLM 2 / 2.5 (Shanghai AI Lab) * * Format: `<|action_start|><|plugin|>\n{"name": "func", "parameters": {...}}<|action_end|>` */ export declare const parseInternLM: ParserFn; /** * ChatGLM / GLM-4 (Zhipu AI) * * Format: function name followed by newline and JSON arguments. * `func_name\n{"arg": "val"}` */ export declare const parseChatGLM: ParserFn; /** * Functionary (MeetKai) * * Format: `>>>func_name\n{"arg": "val"}` * Uses `all` as a special function name for regular text. */ export declare const parseFunctionary: ParserFn; /** * Gorilla (Berkeley) * * Format: `<>func_name(arg1="val1", arg2=val2)` */ export declare const parseGorilla: ParserFn; /** * NexusRaven (Nexusflow) * * Format: `Call: func_name(arg1="val1", arg2=val2)\nThought: reasoning...` */ export declare const parseNexusRaven: ParserFn; /** * xLAM (Salesforce) * * Format: Raw JSON array of tool calls: `[{"name": "func", "arguments": {...}}]` * May be wrapped in ```json code blocks. */ export declare const parseXLAM: ParserFn; /** * FireFunction (Fireworks AI) * * Format: `{"tool_calls": [{"function": {"name": "...", "arguments": "..."}}]}` */ export declare const parseFireFunction: ParserFn; /** * Granite (IBM) * * Format: `<|tool_call|>{"name": "func", "arguments": {...}}<|/tool_call|>` or `<|end_of_text|>` */ export declare const parseGranite: ParserFn; /** * Gemma 2/3 (Google) — prompt-based, no dedicated tokens * * Formats: * - ```tool_code\nfunc(arg=val)\n``` * - `{"name": "func", "parameters": {...}}` */ export declare const parseGemma: ParserFn; /** * LiquidAI LFM / LFM2 / LFM2.5 * * Formats: * - `<|tool_call_start|>[func_name(key="value", key2=123)]<|tool_call_end|>` * - `[func_name(params={"key": "val"})]` (bracket-only, no special tokens) * Parallel calls: `<|tool_call_start|>[func1(a="b"), func2(c="d")]<|tool_call_end|>` * Uses Pythonic function call syntax. */ export declare const parseLiquid: ParserFn; /** * Jamba (AI21) * * Format: `[{"name": "func", "arguments": {...}}]` * Also supports OpenAI-compatible format via FireFunction fallback. */ export declare const parseJamba: ParserFn; /** * Qwen 3.5 XML format * * Format: * ``` * * * value * ... * * * ``` * * The special `params` parameter may contain a JSON object to be spread * into the arguments. */ export declare const parseQwen35Xml: ParserFn; /** * Parse tool calls from LLM output text. * * Automatically detects the model family from the tokenizer and applies the * appropriate parser(s). Falls back to trying all known formats if the model * family cannot be determined. */ export declare function parseToolCalls(text: string, { tokenizer, model, parser }?: ParseToolCallsOptions): ToolCallParserResult; /** * Check if text likely contains tool calls without fully parsing them. * Faster than `parseToolCalls` when you only need presence detection. */ export declare function hasToolCalls(text: string): boolean; /** * Get the list of available parser names. */ export declare function getAvailableParsers(): ReadonlyArray; /** * Get a model-family-specific generation prefix that guides the model to * produce tool calls. Appended to the prompt before generation and prepended * to the decoded output before parsing. * * @param family - The detected model family (from `getAvailableParsers` / `detectModelFamily`). * @param forcedToolName - When a specific tool is forced, include its name in the prefix. * @returns The prefix string, or `undefined` if no prefix is needed. */ export declare function getGenerationPrefix(family: string | null, _forcedToolName: string | undefined): string | undefined; /** * Parse tool calls from model-generated text, returning the workglow `ToolCalls` * type directly (with `input` field instead of `arguments`). * * Tries, in order: * 1. `JSON` tags (Qwen/Hermes) * 2. Bare JSON objects with `name` + `arguments`/`parameters` keys * 3. `{"function": {"name": ..., "arguments": ...}}` format * * Returns both the cleaned text (with tool-call markup removed) and the parsed * ToolCall array. */ export declare function parseToolCallsFromText(responseText: string): { text: string; toolCalls: ToolCalls; }; //# sourceMappingURL=ToolCallParsers.d.ts.map