Use Claude Code, Codex, VSCode, Pi, and OpenCode (and 6 other harnesses) for free (1.3B+ free tokens) from your terminal, app, IDE, or phone, and now from the browser with native browser sessions (multi-harness + multi-model) like OpenClaw (voice supported + ToS friendly)
Python
Restore text-emitted tool calls without model-specific routing (#1407)
Ali Khokharc215e7e38963
diff --git a/ARCHITECTURE.md b/ARCHITECTURE.mdindex 4b82aa87..498ac10d 100644--- a/ARCHITECTURE.md+++ b/ARCHITECTURE.md@@ -719,6 +719,21 @@ original identity before Anthropic tool state or schema validation. Valid namesremain unchanged, while deterministic aliases keep retries, replay, andappend-only prompt prefixes stable. This is target-protocol conversion, never aprovider or model capability switch.+When an Anthropic request declares tools but omits `tool_choice`, the conversion+boundary resolves Anthropic's implicit `auto` intent once. Both upstream OpenAI+Chat and OpenAI Responses encoders materialize that value explicitly, while+preserving every client-supplied choice. OpenAI-origin `/v1/responses` ingress+retains its own omission semantics; providers and models never choose this+default.+Some OpenAI-compatible upstreams expose their documented function-tag protocol+through ordinary content instead of structured tool deltas. The shared+Anthropic tool boundary recognizes that protocol only when tools were declared+and the client did not explicitly choose `none`, and only when the response ends+with one or more exact, schema-valid control blocks with no suffix. Any preceding+content remains visible; all malformed or non-terminal lookalikes remain text.+Native structured tool calls disable textual recovery and remain authoritative.+This normalization is driven by the response grammar rather than provider or+model identity; harness permissions remain the final authority for execution.Specialized provider packages remain only for true upstream quirks such asGemini thought signatures, Groq reasoning-vocabulary negotiation, NIMtool-schema aliases, retry downgrades, and NVCF deployment-failurediff --git a/pyproject.toml b/pyproject.tomlindex fdf8f5e6..55f38730 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -4,7 +4,7 @@ build-backend = "hatchling.build"[project]name = "free-claude-code"-version = "4.30.0"+version = "4.30.1"description = "Local proxy connecting coding agents to OpenAI-compatible AI providers"readme = "README.md"requires-python = ">=3.14.0"diff --git a/smoke/features.py b/smoke/features.pyindex e4221ad6..850ca690 100644--- a/smoke/features.py+++ b/smoke/features.py@@ -145,6 +145,7 @@ FEATURE_INVENTORY: tuple[FeatureCoverage, ...] = ("test_provider_interrupted_tool_turn_resume_e2e","test_gemini_thought_signature_tool_continuation_e2e","test_provider_reasoning_tool_continuation_e2e",+ "test_nvidia_nim_stepfun_tool_use_e2e",),("tools", "providers"),("configured tool-capable provider",),diff --git a/smoke/product/test_nvidia_nim_stepfun_product_live.py b/smoke/product/test_nvidia_nim_stepfun_product_live.pynew file mode 100644index 00000000..123f01a0--- /dev/null+++ b/smoke/product/test_nvidia_nim_stepfun_product_live.py@@ -0,0 +1,64 @@+import pytest++from smoke.lib.config import SmokeConfig+from smoke.lib.e2e import (+ ConversationDriver,+ SmokeServerDriver,+ assert_product_stream,+ tool_use_blocks,+)+from smoke.lib.skips import skip_if_upstream_unavailable_events++pytestmark = [pytest.mark.live, pytest.mark.smoke_target("providers")]++_STEPFUN_MODEL = "nvidia_nim/stepfun-ai/step-3.7-flash"++[email protected]_target("tools")+def test_nvidia_nim_stepfun_tool_use_e2e(+ smoke_config: SmokeConfig,+) -> None:+ """Normalize StepFun tool use when Claude relies on its auto default."""+ if not smoke_config.has_provider_configuration("nvidia_nim"):+ pytest.skip("missing_env: NVIDIA_NIM_API_KEY is not configured")++ payload = {+ "model": "claude-sonnet-4-5-20250929",+ "max_tokens": 512,+ "messages": [+ {+ "role": "user",+ "content": (+ "Use the Bash tool now to run the command printf FCC_STEP_TOOL. "+ "Do not answer in prose."+ ),+ }+ ],+ "tools": [+ {+ "name": "Bash",+ "description": "Run a shell command.",+ "input_schema": {+ "type": "object",+ "properties": {"command": {"type": "string"}},+ "required": ["command"],+ "additionalProperties": False,+ },+ }+ ],+ "thinking": {"type": "adaptive"},+ }+ with SmokeServerDriver(+ smoke_config,+ name="product-nvidia-nim-stepfun-tool-use",+ env_overrides={"MODEL": _STEPFUN_MODEL, "MESSAGING_PLATFORM": "none"},+ ).run() as server:+ turn = ConversationDriver(server, smoke_config).stream(payload)++ skip_if_upstream_unavailable_events(turn.events)+ assert_product_stream(turn.events)+ calls = tool_use_blocks(turn.events)+ assert len(calls) == 1, turn.text+ assert calls[0]["name"] == "Bash"+ assert calls[0]["input"] == {"command": "printf FCC_STEP_TOOL"}+ assert "<tool_call>" not in turn.textdiff --git a/src/free_claude_code/core/anthropic/__init__.py b/src/free_claude_code/core/anthropic/__init__.pyindex 88b17b30..e812230c 100644--- a/src/free_claude_code/core/anthropic/__init__.py+++ b/src/free_claude_code/core/anthropic/__init__.py@@ -48,7 +48,7 @@ from .streaming import ()from .thinking import ContentChunk, ContentType, ThinkTagParserfrom .tokens import get_token_count-from .tools import HeuristicToolParser+from .tools import FunctionTagToolParser, HeuristicToolParserfrom .utils import set_if_not_none__all__ = [@@ -66,6 +66,7 @@ __all__ = ["ContentBlockWebSearchToolResult","ContentChunk","ContentType",+ "FunctionTagToolParser","HeuristicToolParser","Message","MessagesRequest",diff --git a/src/free_claude_code/core/anthropic/conversion.py b/src/free_claude_code/core/anthropic/conversion.pyindex 7d72ee5e..24cc0bb2 100644--- a/src/free_claude_code/core/anthropic/conversion.py+++ b/src/free_claude_code/core/anthropic/conversion.py@@ -25,6 +25,18 @@ class ReasoningReplayMode(StrEnum):REASONING = "reasoning"+def resolve_anthropic_tool_choice(+ tools: list[Any] | None,+ tool_choice: dict[str, Any] | None,+) -> dict[str, Any] | None:+ """Materialize Anthropic's automatic choice when tools are available."""+ if tool_choice is not None:+ return tool_choice+ if tools:+ return {"type": "auto"}+ return None++def _reasoning_replay_field(mode: ReasoningReplayMode) -> str | None:if mode in (ReasoningReplayMode.REASONING_CONTENT,@@ -773,7 +785,7 @@ def build_base_request_body(tools = request_data.toolsif tools:body["tools"] = AnthropicToOpenAIConverter.convert_tools(tools)- tool_choice = request_data.tool_choice+ tool_choice = resolve_anthropic_tool_choice(tools, request_data.tool_choice)if tool_choice:body["tool_choice"] = AnthropicToOpenAIConverter.convert_tool_choice(tool_choicediff --git a/src/free_claude_code/core/anthropic/tool_schema.py b/src/free_claude_code/core/anthropic/tool_schema.pynew file mode 100644index 00000000..b5b9104c--- /dev/null+++ b/src/free_claude_code/core/anthropic/tool_schema.py@@ -0,0 +1,94 @@+"""JSON Schema helpers for text-emitted Anthropic tool input."""++import json+from collections.abc import Mapping+from typing import Any++import jsonschema+++def schema_type(schema: Mapping[str, Any]) -> str | None:+ """Resolve one useful non-null JSON type from a simple or union schema."""+ declared = schema.get("type")+ if isinstance(declared, str):+ return declared+ if isinstance(declared, list):+ non_null = [+ item for item in declared if isinstance(item, str) and item != "null"+ ]+ if len(non_null) == 1:+ return non_null[0]++ for keyword in ("oneOf", "anyOf"):+ alternatives = schema.get(keyword)+ if not isinstance(alternatives, list):+ continue+ types = {+ nested_type+ for alternative in alternatives+ if isinstance(alternative, Mapping)+ if (nested_type := schema_type(alternative)) not in (None, "null")+ }+ if len(types) == 1:+ return types.pop()+ return None+++def coerce_text_argument(value: str, schema: Mapping[str, Any]) -> Any:+ """Decode a textual tool argument according to its declared JSON type."""+ enum_values = schema.get("enum")+ if isinstance(enum_values, list):+ for enum_value in enum_values:+ if str(enum_value) == value:+ return enum_value+ if "const" in schema and str(schema["const"]) == value:+ return schema["const"]++ value_type = schema_type(schema)+ stripped = value.strip()+ try:+ if value_type == "integer":+ return int(stripped)+ if value_type == "number":+ number = json.loads(stripped)+ if isinstance(number, int | float) and not isinstance(number, bool):+ return number+ raise ValueError+ if value_type == "boolean":+ if stripped.lower() == "true":+ return True+ if stripped.lower() == "false":+ return False+ raise ValueError+ if value_type == "null":+ if stripped.lower() == "null":+ return None+ raise ValueError+ if value_type == "array":+ parsed = json.loads(stripped)+ if isinstance(parsed, list):+ return parsed+ raise ValueError+ if value_type == "object":+ parsed = json.loads(stripped)+ if isinstance(parsed, dict):+ return parsed+ raise ValueError+ except json.JSONDecodeError as error:+ raise ValueError from error+ return value+++def arguments_match_schema(+ arguments: dict[str, Any], schema: Mapping[str, Any]+) -> bool:+ """Return whether arguments satisfy a valid JSON Schema."""+ try:+ validator_type = jsonschema.validators.validator_for(schema)+ validator_type.check_schema(schema)+ validator_type(schema).validate(arguments)+ except jsonschema.exceptions.SchemaError:+ return True+ except jsonschema.exceptions.ValidationError:+ return False+ return Truediff --git a/src/free_claude_code/core/anthropic/tools.py b/src/free_claude_code/core/anthropic/tools.pyindex c09beb54..7513e07e 100644--- a/src/free_claude_code/core/anthropic/tools.py+++ b/src/free_claude_code/core/anthropic/tools.py@@ -3,14 +3,264 @@import jsonimport reimport uuid+from collections.abc import Mapping+from dataclasses import dataclassfrom enum import Enumfrom typing import Anyfrom loguru import logger+from .models import MessagesRequest+from .openai_tool_names import OpenAIToolNameCodec+from .tool_schema import arguments_match_schema, coerce_text_argument+_CONTROL_TOKEN_RE = re.compile(r"<\|[^|>]{1,80}\|>")_CONTROL_TOKEN_START = "<|"_CONTROL_TOKEN_END = "|>"+_FUNCTION_TAG_BLOCK_START = "<tool_call>"+_FUNCTION_TAG_BLOCK_END = "</tool_call>"+_FUNCTION_TAG_START = "<function="+_FUNCTION_TAG_END = "</function>"+_PARAMETER_TAG_START = "<parameter="+_PARAMETER_TAG_END = "</parameter>"+_MAX_FUNCTION_TAG_CANDIDATE_CHARS = 4 * 1024 * 1024+++@dataclass(frozen=True, slots=True)+class _RawFunctionTagCall:+ name: str+ arguments: dict[str, str]+++class _FunctionTagState(Enum):+ SEARCHING = 1+ CANDIDATE = 2+ DISABLED = 3+ FINISHED = 4+++class FunctionTagToolParser:+ """Parse an exact terminal function-tag envelope into tool use."""++ def __init__(self, request: MessagesRequest):+ self._tool_names = OpenAIToolNameCodec.from_request(request)+ self._schemas: dict[str, dict[str, Any]] = {}+ for tool in request.tools or ():+ if tool.name:+ self._schemas[tool.name] = (+ tool.input_schema+ if tool.input_schema is not None+ else {"type": "object"}+ )+ tool_choice = request.tool_choice+ tool_choice_type = (+ tool_choice.get("type") if isinstance(tool_choice, dict) else None+ )+ self._state = (+ _FunctionTagState.SEARCHING+ if self._schemas and tool_choice_type != "none"+ else _FunctionTagState.DISABLED+ )+ self._parts: list[str] = []+ self._length = 0+ self._marker_tail = ""++ def feed(self, text: str) -> str:+ """Hold a possible reserved response and return text safe to expose."""+ if not text:+ return ""+ if self._state in {_FunctionTagState.DISABLED, _FunctionTagState.FINISHED}:+ return text++ if self._state is _FunctionTagState.CANDIDATE:+ self._parts.append(text)+ self._length += len(text)+ if self._length > _MAX_FUNCTION_TAG_CANDIDATE_CHARS:+ return self.disable()+ return ""++ candidate = "".join((self._marker_tail, text))+ marker_index = candidate.find(_FUNCTION_TAG_BLOCK_START)+ if marker_index >= 0:+ visible = candidate[:marker_index]+ control = candidate[marker_index:]+ self._state = _FunctionTagState.CANDIDATE+ self._marker_tail = ""+ self._parts.append(control)+ self._length = len(control)+ if self._length > _MAX_FUNCTION_TAG_CANDIDATE_CHARS:+ return "".join((visible, self.disable()))+ return visible++ held_length = _partial_function_tag_marker_suffix_length(candidate)+ if held_length:+ self._marker_tail = candidate[-held_length:]+ return candidate[:-held_length]+ self._marker_tail = ""+ return candidate++ def disable(self) -> str:+ """Disable textual recovery and release any held candidate unchanged."""+ if self._state in {_FunctionTagState.DISABLED, _FunctionTagState.FINISHED}:+ return ""+ self._state = _FunctionTagState.DISABLED+ text = "".join((self._marker_tail, *self._parts))+ self._marker_tail = ""+ self._parts.clear()+ self._length = 0+ return text++ def finish(self) -> tuple[str, tuple[dict[str, Any], ...]]:+ """Finalize one response atomically as visible text or validated tools."""+ if self._state in {_FunctionTagState.DISABLED, _FunctionTagState.FINISHED}:+ return "", ()+ if self._state is _FunctionTagState.SEARCHING:+ self._state = _FunctionTagState.FINISHED+ text = self._marker_tail+ self._marker_tail = ""+ return text, ()+ self._state = _FunctionTagState.FINISHED+ raw = "".join(self._parts)+ self._parts.clear()+ self._length = 0+ try:+ calls = _parse_function_tag_calls(raw)+ tool_uses = self._validated_tool_uses(calls)+ except ValueError:+ return raw, ()+ return "", tool_uses++ def _validated_tool_uses(+ self, calls: tuple[_RawFunctionTagCall, ...]+ ) -> tuple[dict[str, Any], ...]:+ tool_uses: list[dict[str, Any]] = []+ for call in calls:+ name = self._tool_names.decode(call.name)+ schema = self._schemas.get(name)+ if schema is None:+ raise ValueError+ properties = schema.get("properties")+ property_schemas = properties if isinstance(properties, Mapping) else {}+ additional = schema.get("additionalProperties")+ arguments: dict[str, Any] = {}+ for parameter_name, value in call.arguments.items():+ parameter_schema = property_schemas.get(parameter_name)+ if not isinstance(parameter_schema, Mapping):+ parameter_schema = (+ additional if isinstance(additional, Mapping) else {}+ )+ arguments[parameter_name] = coerce_text_argument(+ value,+ parameter_schema,+ )+ if not arguments_match_schema(arguments, schema):+ raise ValueError+ tool_uses.append(+ {+ "type": "tool_use",+ "id": f"toolu_function_tag_{uuid.uuid4().hex[:8]}",+ "name": name,+ "input": arguments,+ }+ )+ return tuple(tool_uses)+++def _parse_function_tag_calls(text: str) -> tuple[_RawFunctionTagCall, ...]:+ cursor = 0+ calls: list[_RawFunctionTagCall] = []+ while True:+ cursor = _skip_function_tag_whitespace(text, cursor)+ if cursor == len(text):+ break+ if not text.startswith(_FUNCTION_TAG_BLOCK_START, cursor):+ raise ValueError++ block_start = cursor + len(_FUNCTION_TAG_BLOCK_START)+ block_end = text.find(_FUNCTION_TAG_BLOCK_END, block_start)+ if block_end < 0:+ raise ValueError+ name, arguments = _parse_function_tag_block(text[block_start:block_end])+ calls.append(_RawFunctionTagCall(name=name, arguments=arguments))+ cursor = block_end + len(_FUNCTION_TAG_BLOCK_END)++ if not calls:+ raise ValueError+ return tuple(calls)+++def _parse_function_tag_block(block: str) -> tuple[str, dict[str, str]]:+ cursor = _skip_function_tag_whitespace(block, 0)+ if not block.startswith(_FUNCTION_TAG_START, cursor):+ raise ValueError+ name_end = block.find(">", cursor + len(_FUNCTION_TAG_START))+ if name_end < 0:+ raise ValueError+ name = block[cursor + len(_FUNCTION_TAG_START) : name_end]+ if not _valid_function_tag_name(name):+ raise ValueError++ arguments: dict[str, str] = {}+ cursor = name_end + 1+ while True:+ cursor = _skip_function_tag_whitespace(block, cursor)+ if block.startswith(_FUNCTION_TAG_END, cursor):+ cursor += len(_FUNCTION_TAG_END)+ break+ if cursor == len(block) or not block.startswith(_PARAMETER_TAG_START, cursor):+ raise ValueError++ parameter_name_end = block.find(">", cursor + len(_PARAMETER_TAG_START))+ if parameter_name_end < 0:+ raise ValueError+ parameter_name = block[cursor + len(_PARAMETER_TAG_START) : parameter_name_end]+ if not _valid_function_tag_name(parameter_name) or parameter_name in arguments:+ raise ValueError++ value_start = parameter_name_end + 1+ value_end = block.find(_PARAMETER_TAG_END, value_start)+ if value_end < 0:+ raise ValueError+ arguments[parameter_name] = _unwrap_function_tag_newlines(+ block[value_start:value_end]+ )+ cursor = value_end + len(_PARAMETER_TAG_END)++ if block[cursor:].strip():+ raise ValueError+ return name, arguments+++def _skip_function_tag_whitespace(text: str, cursor: int) -> int:+ while cursor < len(text) and text[cursor].isspace():+ cursor += 1+ return cursor+++def _valid_function_tag_name(value: str) -> bool:+ return bool(value) and not any(+ character.isspace() or character in "<>" for character in value+ )+++def _unwrap_function_tag_newlines(value: str) -> str:+ if value.startswith("\r\n"):+ value = value[2:]+ elif value.startswith("\n"):+ value = value[1:]+ if value.endswith("\r\n"):+ return value[:-2]+ if value.endswith("\n"):+ return value[:-1]+ return value+++def _partial_function_tag_marker_suffix_length(text: str) -> int:+ max_length = min(len(text), len(_FUNCTION_TAG_BLOCK_START) - 1)+ for length in range(max_length, 0, -1):+ if _FUNCTION_TAG_BLOCK_START.startswith(text[-length:]):+ return length+ return 0class ParserState(Enum):diff --git a/src/free_claude_code/core/openai_responses/provider_input.py b/src/free_claude_code/core/openai_responses/provider_input.pyindex bc4ecea0..f18030e5 100644--- a/src/free_claude_code/core/openai_responses/provider_input.py+++ b/src/free_claude_code/core/openai_responses/provider_input.py@@ -4,6 +4,7 @@ import jsonfrom typing import Anyfrom free_claude_code.core.anthropic.content import get_block_attr, get_block_type+from free_claude_code.core.anthropic.conversion import resolve_anthropic_tool_choicefrom free_claude_code.core.anthropic.models import MessagesRequestfrom free_claude_code.core.anthropic.openai_tool_names import OpenAIToolNameCodecfrom free_claude_code.core.anthropic.request_serialization import (@@ -74,8 +75,9 @@ def build_responses_provider_request(}for tool in request.tools]- if request.tool_choice is not None:- body["tool_choice"] = _tool_choice(request.tool_choice, tool_names=tool_names)+ tool_choice = resolve_anthropic_tool_choice(request.tools, request.tool_choice)+ if tool_choice is not None:+ body["tool_choice"] = _tool_choice(tool_choice, tool_names=tool_names)if reasoning_config := _reasoning_config(reasoning):body["reasoning"] = reasoning_configreturn bodydiff --git a/src/free_claude_code/providers/nvidia_nim/native_tool_stream.py b/src/free_claude_code/providers/nvidia_nim/native_tool_stream.pyindex 7dbe6d6f..e1d239d5 100644--- a/src/free_claude_code/providers/nvidia_nim/native_tool_stream.py+++ b/src/free_claude_code/providers/nvidia_nim/native_tool_stream.py@@ -8,8 +8,11 @@ from enum import StrEnumfrom types import SimpleNamespacefrom typing import Any-import jsonschema-+from free_claude_code.core.anthropic.tool_schema import (+ arguments_match_schema,+ coerce_text_argument,+ schema_type,+)from free_claude_code.providers.failure_policy import RetryableToolProtocolErrorfrom free_claude_code.providers.http import maybe_await_aclose@@ -598,9 +601,9 @@ def _elements_to_object(def _element_value(element: _Element, schema: Mapping[str, Any]) -> Any:- schema_type = _schema_type(schema)+ declared_type = schema_type(schema)if element.children:- if schema_type == "array":+ if declared_type == "array":item_schema = schema.get("items")if not isinstance(item_schema, Mapping):item_schema = {}@@ -614,82 +617,12 @@ def _element_value(element: _Element, schema: Mapping[str, Any]) -> Any:result[mixed_name] = element.textreturn result- return _coerce_leaf(element.text, schema_type, schema)---def _coerce_leaf(- value: str,- schema_type: str | None,- schema: Mapping[str, Any],-) -> Any:- enum_values = schema.get("enum")- if isinstance(enum_values, list):- for enum_value in enum_values:- if str(enum_value) == value:- return enum_value- if "const" in schema and str(schema["const"]) == value:- return schema["const"]-- stripped = value.strip()try:- if schema_type == "integer":- return int(stripped)- if schema_type == "number":- number = json.loads(stripped)- if isinstance(number, int | float) and not isinstance(number, bool):- return number- raise ValueError- if schema_type == "boolean":- if stripped.lower() == "true":- return True- if stripped.lower() == "false":- return False- raise ValueError- if schema_type == "null":- if stripped.lower() == "null":- return None- raise ValueError- if schema_type == "array":- parsed = json.loads(stripped)- if isinstance(parsed, list):- return parsed- raise ValueError- if schema_type == "object":- parsed = json.loads(stripped)- if isinstance(parsed, dict):- return parsed- raise ValueError- except json.JSONDecodeError, ValueError:+ return coerce_text_argument(element.text, schema)+ except ValueError:raise NimNativeToolProtocolError("NVIDIA NIM returned a native MiniMax argument with the wrong type.") from None- return value---def _schema_type(schema: Mapping[str, Any]) -> str | None:- declared = schema.get("type")- if isinstance(declared, str):- return declared- if isinstance(declared, list):- non_null = [- item for item in declared if isinstance(item, str) and item != "null"- ]- if len(non_null) == 1:- return non_null[0]-- for keyword in ("oneOf", "anyOf"):- alternatives = schema.get(keyword)- if not isinstance(alternatives, list):- continue- types = {- nested_type- for alternative in alternatives- if isinstance(alternative, Mapping)- if (nested_type := _schema_type(alternative)) not in (None, "null")- }- if len(types) == 1:- return types.pop()- return Nonedef _validate_arguments(@@ -697,16 +630,10 @@ def _validate_arguments(arguments: dict[str, Any],schema: dict[str, Any],) -> None:- try:- validator_type = jsonschema.validators.validator_for(schema)- validator_type.check_schema(schema)- validator_type(schema).validate(arguments)- except jsonschema.exceptions.SchemaError:- return- except jsonschema.exceptions.ValidationError:+ if not arguments_match_schema(arguments, schema):raise NimNativeToolProtocolError(f"NVIDIA NIM returned schema-invalid arguments for tool {tool_name!r}."- ) from None+ )def _openai_tool_schemas(body: Mapping[str, Any]) -> dict[str, dict[str, Any]]:diff --git a/src/free_claude_code/providers/openai_chat/provider.py b/src/free_claude_code/providers/openai_chat/provider.pyindex 9e32d91c..6e7e811b 100644--- a/src/free_claude_code/providers/openai_chat/provider.py+++ b/src/free_claude_code/providers/openai_chat/provider.py@@ -14,6 +14,7 @@ from openai import AsyncOpenAIfrom free_claude_code.application.model_metadata import ProviderModelInfofrom free_claude_code.core.anthropic import (ContentType,+ FunctionTagToolParser,HeuristicToolParser,OpenAIToolNameCodec,ThinkTagParser,@@ -90,6 +91,47 @@ class _CollectedRecoveryOutput:tool_calls: tuple[dict[str, Any], ...]+def _iter_visible_text_events(+ ledger: AnthropicStreamLedger,+ text: str,+) -> Iterator[str]:+ yield from ledger.ensure_text_block()+ yield ledger.emit_text_delta(text)+++def _iter_text_parser_events(+ ledger: AnthropicStreamLedger,+ parser: HeuristicToolParser,+ text: str,+ *,+ tool_names: OpenAIToolNameCodec,+) -> Iterator[str]:+ """Route visible text through the established heuristic tool parser."""+ filtered_text, detected_tools = parser.feed(text)+ if filtered_text:+ yield from _iter_visible_text_events(ledger, filtered_text)+ for tool_use in detected_tools:+ yield from iter_heuristic_tool_use_sse(+ ledger,+ tool_use,+ tool_names=tool_names,+ )+++def _iter_text_tool_use_events(+ ledger: AnthropicStreamLedger,+ tool_uses: tuple[dict[str, Any], ...] | list[dict[str, Any]],+ *,+ tool_names: OpenAIToolNameCodec,+) -> Iterator[str]:+ for tool_use in tool_uses:+ yield from iter_heuristic_tool_use_sse(+ ledger,+ tool_use,+ tool_names=tool_names,+ )++class OpenAIChatProvider(BaseProvider):"""OpenAI-compatible ``/chat/completions`` provider configured by a profile."""@@ -476,6 +518,7 @@ class _OpenAIChatStreamRunner:)think_parser = ThinkTagParser()+ function_tag_parser = FunctionTagToolParser(self._request)heuristic_parser = HeuristicToolParser()finish_reason = Noneusage_info = None@@ -548,6 +591,18 @@ class _OpenAIChatStreamRunner:for out_event in hold_event(event):yield out_event+ native_tool_calls = delta.tool_calls+ if native_tool_calls:+ released_text = function_tag_parser.disable()+ if released_text:+ for event in hold_events(+ _iter_visible_text_events(+ ledger,+ released_text,+ )+ ):+ yield event+if delta.content:for part in think_parser.feed(delta.content):if part.type == ContentType.THINKING:@@ -562,34 +617,22 @@ class _OpenAIChatStreamRunner:):yield eventelse:- (- filtered_text,- detected_tools,- ) = heuristic_parser.feed(part.content)-- if filtered_text:+ safe_text = function_tag_parser.feed(part.content)+ if safe_text:for event in hold_events(- ledger.ensure_text_block()- ):- yield event- for event in hold_event(- ledger.emit_text_delta(filtered_text)+ _iter_text_parser_events(+ ledger,+ heuristic_parser,+ safe_text,+ tool_names=self._tool_names,+ )):yield event- for tool_use in detected_tools:- for event in iter_heuristic_tool_use_sse(- ledger,- tool_use,- tool_names=self._tool_names,- ):- for out_event in hold_event(event):- yield out_event-- if delta.tool_calls:+ if native_tool_calls:for event in hold_events(ledger.close_content_blocks()):yield event- for tool_call in delta.tool_calls:+ for tool_call in native_tool_calls:extra_content = tool_call_extra_content(tool_call)tool_call_info = {"index": tool_call.index,@@ -623,6 +666,55 @@ class _OpenAIChatStreamRunner:raise TruncatedProviderStreamError("Provider stream ended with an incomplete tool name.")++ remaining = think_parser.flush()+ if remaining:+ if remaining.type == ContentType.THINKING:+ if output_reasoning:+ for event in hold_events(ledger.ensure_thinking_block()):+ yield event+ for event in hold_event(+ ledger.emit_thinking_delta(remaining.content)+ ):+ yield event+ else:+ safe_text = function_tag_parser.feed(remaining.content)+ if safe_text:+ for event in hold_events(+ _iter_text_parser_events(+ ledger,+ heuristic_parser,+ safe_text,+ tool_names=self._tool_names,+ )+ ):+ yield event++ fallback_text, function_tag_tools = function_tag_parser.finish()+ if fallback_text:+ for event in hold_events(+ _iter_visible_text_events(+ ledger,+ fallback_text,+ )+ ):+ yield event+ for event in hold_events(+ _iter_text_tool_use_events(+ ledger,+ function_tag_tools,+ tool_names=self._tool_names,+ )+ ):+ yield event+ for event in hold_events(+ _iter_text_tool_use_events(+ ledger,+ heuristic_parser.flush(),+ tool_names=self._tool_names,+ )+ ):+ yield eventbreakexcept asyncio.CancelledError, GeneratorExit:@@ -664,6 +756,7 @@ class _OpenAIChatStreamRunner:)ledger = self._new_ledger()think_parser = ThinkTagParser()+ function_tag_parser = FunctionTagToolParser(self._request)heuristic_parser = HeuristicToolParser()finish_reason = Noneusage_info = None@@ -750,33 +843,6 @@ class _OpenAIChatStreamRunner:if attempt is not None:await attempt.aclose()- remaining = think_parser.flush()- if remaining:- if remaining.type == ContentType.THINKING:- if not output_reasoning:- remaining = None- else:- for event in hold_events(ledger.ensure_thinking_block()):- yield event- for event in hold_event(- ledger.emit_thinking_delta(remaining.content)- ):- yield event- if remaining and remaining.type == ContentType.TEXT:- for event in hold_events(ledger.ensure_text_block()):- yield event- for event in hold_event(ledger.emit_text_delta(remaining.content)):- yield event-- for tool_use in heuristic_parser.flush():- for event in iter_heuristic_tool_use_sse(- ledger,- tool_use,- tool_names=self._tool_names,- ):- for out_event in hold_event(event):- yield out_event-for event in self._tool_calls.flush_tool_name_buffers(ledger,tool_names=self._tool_names,diff --git a/tests/core/anthropic/test_text_tools.py b/tests/core/anthropic/test_text_tools.pynew file mode 100644index 00000000..82107cf7--- /dev/null+++ b/tests/core/anthropic/test_text_tools.py@@ -0,0 +1,260 @@+import pytest++from free_claude_code.core.anthropic import (+ FunctionTagToolParser,+ OpenAIToolNameCodec,+)+from free_claude_code.core.anthropic import tools as tool_parsers+from tests.providers.request_factory import make_messages_request+++def _request(*tools: dict):+ return make_messages_request(+ "test-model",+ messages=[],+ max_tokens=256,+ tools=list(tools),+ )+++def _tool(name: str, properties: dict, required: list[str] | None = None) -> dict:+ schema = {+ "type": "object",+ "properties": properties,+ "additionalProperties": False,+ }+ if required is not None:+ schema["required"] = required+ return {"name": name, "input_schema": schema}+++def _call(name: str, **arguments: str) -> str:+ parameters = "".join(+ f"<parameter={key}>\n{value}\n</parameter>\n"+ for key, value in arguments.items()+ )+ return f"<tool_call>\n<function={name}>\n{parameters}</function>\n</tool_call>"+++def _parse(parser: FunctionTagToolParser, *chunks: str):+ visible = "".join(parser.feed(chunk) for chunk in chunks)+ tail, calls = parser.finish()+ return visible + tail, calls+++def test_function_tag_parser_accepts_exact_reserved_response() -> None:+ parser = FunctionTagToolParser(+ _request(_tool("Bash", {"command": {"type": "string"}}, ["command"]))+ )++ visible, calls = _parse(parser, _call("Bash", command="printf ok"))++ assert visible == ""+ assert len(calls) == 1+ assert calls[0]["id"].startswith("toolu_function_tag_")+ assert calls[0]["name"] == "Bash"+ assert calls[0]["input"] == {"command": "printf ok"}+++def test_function_tag_parser_handles_every_two_chunk_split() -> None:+ raw = _call("Read", file_path="README.md")+ request = _request(_tool("Read", {"file_path": {"type": "string"}}, ["file_path"]))++ for split in range(len(raw) + 1):+ visible, calls = _parse(+ FunctionTagToolParser(request),+ raw[:split],+ raw[split:],+ )+ assert visible == "", split+ assert [call["input"] for call in calls] == [{"file_path": "README.md"}], split+++def test_function_tag_parser_streams_prefix_and_parses_terminal_call() -> None:+ request = _request(_tool("Bash", {"command": {"type": "string"}}, ["command"]))+ prefix = "I will run the requested command.\n"+ raw_call = _call("Bash", command="printf ok")+ parser = FunctionTagToolParser(request)++ first_visible = parser.feed(prefix + raw_call[:5])+ second_visible = parser.feed(raw_call[5:])+ tail, calls = parser.finish()++ assert first_visible == prefix+ assert second_visible == ""+ assert tail == ""+ assert [call["input"] for call in calls] == [{"command": "printf ok"}]+++def test_function_tag_parser_accepts_multiple_typed_calls_atomically() -> None:+ request = _request(+ _tool(+ "Configure",+ {+ "count": {"type": "integer"},+ "ratio": {"type": "number"},+ "enabled": {"type": "boolean"},+ "nothing": {"type": "null"},+ "items": {"type": "array", "items": {"type": "integer"}},+ "metadata": {"type": "object"},+ "mode": {"enum": ["fast", "safe"]},+ "fixed": {"const": 7},+ },+ [+ "count",+ "ratio",+ "enabled",+ "nothing",+ "items",+ "metadata",+ "mode",+ "fixed",+ ],+ ),+ _tool("Bash", {"command": {"type": "string"}}, ["command"]),+ )+ raw = "\n".join(+ (+ _call(+ "Configure",+ count="2",+ ratio="1.5",+ enabled="true",+ nothing="null",+ items="[1,2]",+ metadata='{"key":"value"}',+ mode="safe",+ fixed="7",+ ),+ _call("Bash", command="printf first\nprintf second"),+ )+ )++ visible, calls = _parse(FunctionTagToolParser(request), raw)++ assert visible == ""+ assert [call["name"] for call in calls] == ["Configure", "Bash"]+ assert calls[0]["input"] == {+ "count": 2,+ "ratio": 1.5,+ "enabled": True,+ "nothing": None,+ "items": [1, 2],+ "metadata": {"key": "value"},+ "mode": "safe",+ "fixed": 7,+ }+ assert calls[1]["input"] == {"command": "printf first\nprintf second"}+++def test_function_tag_parser_decodes_openai_tool_alias() -> None:+ original = "mcp__function_tag__" + "x" * 80+ request = _request(_tool(original, {}, []))+ alias = OpenAIToolNameCodec.from_request(request).encode(original)++ visible, calls = _parse(FunctionTagToolParser(request), _call(alias))++ assert visible == ""+ assert [call["name"] for call in calls] == [original]+++ "raw",+ [+ "Example: {call}\nThat was only an example.",+ "{call}\nDone.",+ "```xml\n{call}\n```",+ "<tool_call><function=Bash></function>",+ (+ "<tool_call><function=Bash>"+ "<parameter=command>x</parameter>"+ "<parameter=command>y</parameter>"+ "</function></tool_call>"+ ),+ "<tool_call><function=Unknown></function></tool_call>",+ "<tool_call><function=Bash></function></tool_call>",+ "{wrong_type}",+ "{extra_parameter}",+ ],+)+def test_function_tag_parser_preserves_non_control_lookalikes(raw: str) -> None:+ valid_call = _call("Bash", command="printf ok")+ wrong_type = _call("Count", value="not-an-integer")+ extra_parameter = _call("Bash", command="printf ok", extra="no")+ raw = raw.format(+ call=valid_call,+ wrong_type=wrong_type,+ extra_parameter=extra_parameter,+ )+ request = _request(+ _tool("Bash", {"command": {"type": "string"}}, ["command"]),+ _tool("Count", {"value": {"type": "integer"}}, ["value"]),+ )++ visible, calls = _parse(FunctionTagToolParser(request), raw)++ assert visible == raw+ assert calls == ()+++def test_function_tag_parser_preserves_whole_multi_call_candidate_on_failure() -> None:+ request = _request(_tool("Bash", {"command": {"type": "string"}}, ["command"]))+ raw = _call("Bash", command="printf ok") + _call("Unknown")++ visible, calls = _parse(FunctionTagToolParser(request), raw)++ assert visible == raw+ assert calls == ()+++def test_function_tag_parser_without_tools_is_immediate_passthrough() -> None:+ raw = _call("Bash", command="printf ok")+ parser = FunctionTagToolParser(_request())++ assert parser.feed(raw) == raw+ assert parser.finish() == ("", ())+++def test_function_tag_parser_respects_explicit_none_choice() -> None:+ raw = _call("Bash", command="printf ok")+ request = make_messages_request(+ "test-model",+ messages=[],+ max_tokens=256,+ tools=[_tool("Bash", {"command": {"type": "string"}}, ["command"])],+ tool_choice={"type": "none"},+ )+ parser = FunctionTagToolParser(request)++ assert parser.feed(raw) == raw+ assert parser.finish() == ("", ())+++def test_function_tag_parser_disable_releases_candidate_once() -> None:+ raw = _call("Bash", command="printf ok")+ parser = FunctionTagToolParser(+ _request(_tool("Bash", {"command": {"type": "string"}}, ["command"]))+ )++ assert parser.feed(raw[:20]) == ""+ assert parser.disable() == raw[:20]+ assert parser.disable() == ""+ assert parser.feed(raw[20:]) == raw[20:]+ assert parser.finish() == ("", ())+++def test_function_tag_parser_releases_oversized_candidate(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ raw = _call("Bash", command="printf ok")+ monkeypatch.setattr(+ tool_parsers,+ "_MAX_FUNCTION_TAG_CANDIDATE_CHARS",+ len(raw) - 1,+ )+ parser = FunctionTagToolParser(+ _request(_tool("Bash", {"command": {"type": "string"}}, ["command"]))+ )++ assert parser.feed(raw) == raw+ assert parser.finish() == ("", ())diff --git a/tests/core/openai_responses/test_provider_input.py b/tests/core/openai_responses/test_provider_input.pyindex 06a9aead..5b2b7978 100644--- a/tests/core/openai_responses/test_provider_input.py+++ b/tests/core/openai_responses/test_provider_input.py@@ -136,6 +136,50 @@ def test_build_responses_provider_request_accepts_claude_client_controls() -> Noassert request.model_dump() == snapshot+def test_build_responses_provider_request_materializes_auto_tool_choice() -> None:+ request = MessagesRequest.model_validate(+ {+ "model": "gpt-test",+ "messages": [{"role": "user", "content": "Use the echo tool."}],+ "tools": [+ {+ "name": "echo",+ "description": "Echo a value.",+ "input_schema": {+ "type": "object",+ "properties": {"value": {"type": "string"}},+ "required": ["value"],+ },+ }+ ],+ }+ )++ body = build_responses_provider_request(+ request,+ reasoning=ReasoningPolicy.off(),+ )++ assert body["tool_choice"] == "auto"+++def test_build_responses_provider_request_omits_choice_without_tools() -> None:+ request = MessagesRequest.model_validate(+ {+ "model": "gpt-test",+ "messages": [{"role": "user", "content": "Hello"}],+ }+ )++ body = build_responses_provider_request(+ request,+ reasoning=ReasoningPolicy.off(),+ )++ assert "tools" not in body+ assert "tool_choice" not in body++def test_build_responses_provider_request_uses_resolved_reasoning_policy() -> None:request = MessagesRequest.model_validate({diff --git a/tests/providers/test_converter.py b/tests/providers/test_converter.pyindex e8ec9c80..b24f2c6f 100644--- a/tests/providers/test_converter.py+++ b/tests/providers/test_converter.py@@ -357,6 +357,44 @@ def test_convert_tool_choice(tool_choice, expected):assert result == expected+def test_openai_build_materializes_auto_tool_choice_when_tools_are_declared():+ request = MessagesRequest.model_validate(+ {+ "model": "model",+ "messages": [{"role": "user", "content": "Use the echo tool."}],+ "tools": [+ {+ "name": "echo",+ "description": "Echo a value.",+ "input_schema": {+ "type": "object",+ "properties": {"value": {"type": "string"}},+ "required": ["value"],+ },+ }+ ],+ }+ )++ body = build_base_request_body(request)++ assert body["tool_choice"] == "auto"+++def test_openai_build_omits_tool_choice_when_tools_are_absent():+ request = MessagesRequest.model_validate(+ {+ "model": "model",+ "messages": [{"role": "user", "content": "Hello"}],+ }+ )++ body = build_base_request_body(request)++ assert "tools" not in body+ assert "tool_choice" not in body++# --- Message Conversion Tests: User ---diff --git a/tests/providers/test_streaming_errors.py b/tests/providers/test_streaming_errors.pyindex 23240a9a..05c48074 100644--- a/tests/providers/test_streaming_errors.py+++ b/tests/providers/test_streaming_errors.py@@ -728,6 +728,235 @@ class TestStreamingExceptionHandling:for event in parsed)+ @pytest.mark.asyncio+ async def test_function_tag_tool_stream_becomes_one_anthropic_tool_use(self):+ """Exact control-only function tags use the established tool lifecycle."""+ provider = _make_provider()+ request = _make_request(+ tools=[+ {+ "name": "Bash",+ "input_schema": {+ "type": "object",+ "properties": {"command": {"type": "string"}},+ "required": ["command"],+ "additionalProperties": False,+ },+ }+ ]+ )+ raw_call = (+ "<think>Use the requested command.</think>"+ "I will invoke Bash now.\n"+ "<tool_call>\n<function=Bash>\n<parameter=command>\n"+ "printf FCC_STEP_TOOL\n</parameter>\n</function>\n</tool_call>"+ )+ stream_mock = AsyncStreamMock(+ [+ _make_chunk(content=raw_call[:43]),+ _make_chunk(content=raw_call[43:]),+ _make_chunk(finish_reason="stop"),+ ]+ )++ with patch.object(+ provider._client.chat.completions,+ "create",+ new_callable=AsyncMock,+ return_value=stream_mock,+ ):+ events = await _collect_stream(provider, request)++ parsed = parse_sse_text("".join(events))+ starts = [+ event.data["content_block"]+ for event in parsed+ if event.event == "content_block_start"+ ]+ tool_starts = [block for block in starts if block["type"] == "tool_use"]+ input_json = "".join(+ event.data.get("delta", {}).get("partial_json", "")+ for event in parsed+ if event.event == "content_block_delta"+ and event.data.get("delta", {}).get("type") == "input_json_delta"+ )+ visible_text = "".join(+ event.data.get("delta", {}).get("text", "")+ for event in parsed+ if event.event == "content_block_delta"+ and event.data.get("delta", {}).get("type") == "text_delta"+ )++ assert [block["name"] for block in tool_starts] == ["Bash"]+ assert json.loads(input_json) == {"command": "printf FCC_STEP_TOOL"}+ assert visible_text == "I will invoke Bash now.\n"+ assert any(+ event.event == "content_block_delta"+ and event.data.get("delta", {}).get("type") == "thinking_delta"+ for event in parsed+ )+ assert any(+ event.event == "message_delta"+ and event.data.get("delta", {}).get("stop_reason") == "tool_use"+ for event in parsed+ )++ @pytest.mark.asyncio+ async def test_native_tool_call_disables_function_tag_recovery(self):+ """Native structured calls win and release earlier candidate text in order."""+ provider = _make_provider()+ request = _make_request(+ tools=[+ {+ "name": "Bash",+ "input_schema": {+ "type": "object",+ "properties": {"command": {"type": "string"}},+ },+ }+ ]+ )+ held_text = "<tool_call>\n<function=Bash>"+ stream_mock = AsyncStreamMock(+ [+ _make_chunk(content=held_text),+ _make_tool_calls_chunk(+ name="Bash",+ arguments='{"command":"printf native"}',+ tool_id="call_native",+ ),+ _make_chunk(finish_reason="tool_calls"),+ ]+ )++ with patch.object(+ provider._client.chat.completions,+ "create",+ new_callable=AsyncMock,+ return_value=stream_mock,+ ):+ events = await _collect_stream(provider, request)++ parsed = parse_sse_text("".join(events))+ visible_text = "".join(+ event.data.get("delta", {}).get("text", "")+ for event in parsed+ if event.event == "content_block_delta"+ and event.data.get("delta", {}).get("type") == "text_delta"+ )+ tool_starts = [+ event.data["content_block"]+ for event in parsed+ if event.event == "content_block_start"+ and event.data.get("content_block", {}).get("type") == "tool_use"+ ]++ assert visible_text == held_text+ assert [block["id"] for block in tool_starts] == ["call_native"]++ @pytest.mark.asyncio+ async def test_rejected_function_tag_candidate_bypasses_legacy_heuristics(self):+ """Rejected reserved text is preserved without a second permissive parse."""+ provider = _make_provider()+ request = _make_request(+ tools=[+ {+ "name": "Bash",+ "input_schema": {+ "type": "object",+ "properties": {"command": {"type": "string"}},+ },+ }+ ]+ )+ raw = (+ "<tool_call><function=Unknown></function></tool_call>"+ "● <function=Bash><parameter=command>printf unsafe</parameter>"+ )+ stream_mock = AsyncStreamMock(+ [_make_chunk(content=raw), _make_chunk(finish_reason="stop")]+ )++ with patch.object(+ provider._client.chat.completions,+ "create",+ new_callable=AsyncMock,+ return_value=stream_mock,+ ):+ events = await _collect_stream(provider, request)++ parsed = parse_sse_text("".join(events))+ visible_text = "".join(+ event.data.get("delta", {}).get("text", "")+ for event in parsed+ if event.event == "content_block_delta"+ and event.data.get("delta", {}).get("type") == "text_delta"+ )+ tool_starts = [+ event+ for event in parsed+ if event.event == "content_block_start"+ and event.data.get("content_block", {}).get("type") == "tool_use"+ ]++ assert visible_text == raw+ assert tool_starts == []++ @pytest.mark.asyncio+ async def test_function_tag_candidate_is_reset_before_early_retry(self):+ """An abandoned textual candidate cannot leak or duplicate after retry."""+ provider = _make_provider()+ request = _make_request(+ tools=[+ {+ "name": "Bash",+ "input_schema": {+ "type": "object",+ "properties": {"command": {"type": "string"}},+ "required": ["command"],+ },+ }+ ]+ )+ abandoned = "<tool_call>\n<function=Bash>"+ complete = (+ "<tool_call>\n<function=Bash>\n<parameter=command>\n"+ "printf retry\n</parameter>\n</function>\n</tool_call>"+ )+ first_stream = AsyncStreamMock(+ [_make_chunk(content=abandoned)],+ error=httpx.ReadError("early cutoff"),+ )+ second_stream = AsyncStreamMock(+ [_make_chunk(content=complete), _make_chunk(finish_reason="stop")]+ )++ with patch.object(+ provider._client.chat.completions,+ "create",+ new_callable=AsyncMock,+ side_effect=[first_stream, second_stream],+ ) as mock_create:+ events = await _collect_stream(provider, request)++ parsed = parse_sse_text("".join(events))+ visible_text = "".join(+ event.data.get("delta", {}).get("text", "")+ for event in parsed+ if event.event == "content_block_delta"+ and event.data.get("delta", {}).get("type") == "text_delta"+ )+ tool_starts = [+ event.data["content_block"]+ for event in parsed+ if event.event == "content_block_start"+ and event.data.get("content_block", {}).get("type") == "tool_use"+ ]++ assert mock_create.await_count == 2+ assert visible_text == ""+ assert [block["name"] for block in tool_starts] == ["Bash"]+@pytest.mark.asyncioasync def test_precommit_retry_emits_one_unduplicated_downstream_lifecycle(self):"""An abandoned attempt contributes no frame to the successful replay."""diff --git a/uv.lock b/uv.lockindex 7add6815..2edce06b 100644--- a/uv.lock+++ b/uv.lock@@ -620,7 +620,7 @@ wheels = [[[package]]name = "free-claude-code"-version = "4.30.0"+version = "4.30.1"source = { editable = "." }dependencies = [{ name = "aiohttp" },