"""Conservative DSH SSE compatibility for a provider reusing tool-call indices. Enable only at the DSH SSE boundary, before both the policy observer and SDK. feed() returns complete checked lines immediately; finish() handles an optional unterminated final line. Forward exactly these returned bytes to both consumers. Normal ordered streams retain their wire bytes. A changed tool index may cause its JSON line to be reserialized. The observed DSH quality-tool namespace alias is canonicalized only when that exact tool was offered. Argument strings and IDs are never rewritten. Non-SSE data and message snapshots pass through unchanged. """ from __future__ import annotations from dataclasses import dataclass, field import json import re from typing import Iterable class ToolStreamProtocolError(ValueError): """A safe framing diagnostic: never include provider text or arguments.""" def __init__(self, code: str, *, names=None): self.code = code # Only bounded protocol identifiers; never retain arguments or content. self.tool_names = {key: value if isinstance(value, str) and re.fullmatch(r'[A-Za-z0-9_.:-]{0,160}', value) else '[invalid identifier]' for key, value in (names or {}).items()} super().__init__('Invalid tool stream framing: ' + code) def _unique_object(pairs): value = {} for key, item in pairs: if key in value: raise ValueError('duplicate key') value[key] = item return value def _invalid_constant(_value): raise ValueError('non-JSON constant') def _json_value(value): return json.loads(value, object_pairs_hook=_unique_object, parse_constant=_invalid_constant) @dataclass class _Call: canonical_index: int wire_index: int identity: str | None = None name: str = '' arguments: str = '' @dataclass class _Choice: next_index: int = 0 active: dict[int, _Call] = field(default_factory=dict) identities: dict[str, _Call] = field(default_factory=dict) finished: bool = False class ToolStreamNormalizer: """Normalize indices without inferring a tool's identity from its values. A fresh ID at an occupied wire index is admitted only after the former call has a complete advertised name and a nonempty ID. Argument errors are preserved for the SDK's tool-error handling; they do not abort transport. Retired IDs can never resume. Canonical indices follow first appearance independently for each choice; later wire indices cannot collide with an earlier normalized call. Missing IDs on ordinary argument deltas continue the current call; missing tool indices are not guessed. Errors poison the instance. Do not forward a failing feed's input or emit a synthetic successful terminal event. Completion and argument/schema checks in the existing observer/SDK still apply; an incomplete final call is not repaired here. """ MAX_FRAME_BYTES = 2_000_000 def __init__(self, offered: Iterable[str]): self.offered = frozenset(offered) if any(not isinstance(name, str) or not name for name in self.offered): raise ValueError('offered must contain nonempty tool names') self._buffer = b'' self._choices: dict[int, _Choice] = {} self._done = False self._closed = False self._failed = False def feed(self, chunk: bytes) -> bytes: if self._failed or self._closed: raise ToolStreamProtocolError('stream_closed') if not isinstance(chunk, bytes): raise TypeError('SSE input must be bytes') try: self._buffer += chunk output = [] while b'\n' in self._buffer: line, self._buffer = self._buffer.split(b'\n', 1) if len(line) > self.MAX_FRAME_BYTES: raise ToolStreamProtocolError('frame_too_large') output.append(self._line(line + b'\n')) if len(self._buffer) > self.MAX_FRAME_BYTES: raise ToolStreamProtocolError('frame_too_large') return b''.join(output) except Exception: self._failed = True self._buffer = b'' raise def finish(self) -> bytes: if self._failed or self._closed: raise ToolStreamProtocolError('stream_closed') try: output = self._line(self._buffer) if self._buffer else b'' self._buffer = b'' self._require_finished() self._closed = True return output except Exception: self._failed = True self._buffer = b'' raise def _require_finished(self): if any(not choice.finished for choice in self._choices.values()): raise ToolStreamProtocolError('missing_finish_reason') def _line(self, line: bytes) -> bytes: if not line.startswith(b'data:'): return line data = line[5:].strip() if not data: return line if self._done: raise ToolStreamProtocolError('data_after_done') if data == b'[DONE]': self._require_finished() self._done = True return line try: value = _json_value(data) except (ValueError, UnicodeError, RecursionError): raise ToolStreamProtocolError('invalid_json_frame') from None if not isinstance(value, dict): raise ToolStreamProtocolError('invalid_completion_frame') # The policy observer owns upstream error handling; preserve it verbatim. if value.get('error'): return line changed = self._document(value) if not changed: return line ending = b'\r\n' if line.endswith(b'\r\n') else b'\n' if line.endswith(b'\n') else b'' return b'data: ' + json.dumps(value, ensure_ascii=True, separators=(',', ':')).encode('ascii') + ending def _document(self, value): choices = value.get('choices', []) if not isinstance(choices, list): raise ToolStreamProtocolError('invalid_choices') changed = False for incoming in choices: if not isinstance(incoming, dict): raise ToolStreamProtocolError('invalid_choice') # Full-message responses are outside this delta-stream workaround. if 'message' in incoming: continue index = incoming.get('index', 0) if type(index) is not int or index < 0: raise ToolStreamProtocolError('invalid_choice_index') choice = self._choices.setdefault(index, _Choice()) delta = incoming.get('delta') or {} if not isinstance(delta, dict): raise ToolStreamProtocolError('invalid_delta') calls = delta.get('tool_calls') or [] if not isinstance(calls, list): raise ToolStreamProtocolError('invalid_tool_calls') if calls and choice.finished: raise ToolStreamProtocolError('call_after_finish') for call in calls: changed |= self._call(choice, call) if incoming.get('finish_reason') is not None: for active in choice.active.values(): if active.name not in self.offered: raise ToolStreamProtocolError('unadvertised_tool', names={'assembled': active.name}) choice.finished = True return changed def _call(self, choice, incoming): if not isinstance(incoming, dict): raise ToolStreamProtocolError('invalid_tool_call') wire_index = incoming.get('index') if type(wire_index) is not int or not 0 <= wire_index < 2**53: raise ToolStreamProtocolError('invalid_tool_index') active = choice.active.get(wire_index) changed = False # MiMo explicitly sends null ID/name placeholders on argument deltas. # They can only continue an already identified call at the same index; # no ID, name, arguments or successful terminal event is fabricated. if incoming.get('id', False) is None and active is not None and active.identity: del incoming['id'] changed = True identity = incoming.get('id') if 'id' in incoming and (not isinstance(identity, str) or not identity): raise ToolStreamProtocolError('invalid_call_id') if 'type' in incoming and incoming['type'] != 'function': raise ToolStreamProtocolError('invalid_tool_type') if 'custom' in incoming: raise ToolStreamProtocolError('invalid_tool_type') function = incoming.get('function', {}) if isinstance(function, dict) and function.get('name', False) is None and active is not None and active.identity: del function['name'] changed = True if not isinstance(function, dict) or any(key in function and not isinstance(function[key], str) for key in ('name', 'arguments')): raise ToolStreamProtocolError('invalid_tool_function') known = choice.identities.get(identity) if identity else None if known is not None and known is not active: raise ToolStreamProtocolError('reused_call_id') if active is not None and identity and active.identity and identity != active.identity: if not active.identity or active.name not in self.offered: raise ToolStreamProtocolError('incomplete_previous_call') active = None if active is None: # Preserve ordinary ordered/sparse wire indices where possible; # every later first-seen call advances beyond all assigned indices. canonical = max(wire_index, choice.next_index) if canonical >= 2**53: raise ToolStreamProtocolError('invalid_tool_index') choice.next_index = canonical + 1 active = _Call(canonical, wire_index) choice.active[wire_index] = active if identity: active.identity = identity choice.identities[identity] = active # Real MiMo Flash responses sometimes omit the "mcp__" namespace. # Canonicalize this one observed alias, including fragmented names, # before either observer or SDK sees it. This never grants a new tool: # the complete assembled name must still equal the offered canonical # name, and IDs/arguments retain their original bytes. fragment = function.get('name', '') if (not active.name and fragment and 'mcp__quality__quality_data' in self.offered and 'quality__quality_data'.startswith(fragment)): function['name'] = 'mcp__' + fragment changed = True previous_name = active.name active.name += function.get('name', '') if active.name and not any(allowed.startswith(active.name) for allowed in self.offered): raise ToolStreamProtocolError('unadvertised_tool', names={ 'previous': previous_name, 'fragment': function.get('name', ''), 'assembled': active.name}) active.arguments += function.get('arguments', '') incoming['index'] = active.canonical_index return changed or active.canonical_index != wire_index