acp_openai_bridge.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. """OpenAI-shape bridge shared by Hermes' ACP clients.
  2. ACP has no OpenAI-style ``tools``/``tool_calls`` channel, so Hermes' tool schemas travel INTO the
  3. prompt as text (:func:`render_tool_bridge_sections`) and calls are parsed back OUT of the response
  4. text (:func:`extract_tool_calls_from_text`). Clients differ only in WHICH tools they forward
  5. (``allowlist``): a CLI with no tools of its own forwards everything; an autonomous agent with its own
  6. read/edit/execute tools forwards only Hermes' agent-level tools, since re-offering overlapping ones
  7. makes Hermes redo finished work.
  8. """
  9. from __future__ import annotations
  10. import json
  11. import re
  12. from types import SimpleNamespace
  13. from typing import Any, Iterable
  14. from openai.types.chat.chat_completion_message_tool_call import ChatCompletionMessageToolCall, Function
  15. TOOL_CALL_BLOCK_RE = re.compile(r"<tool_call>\s*(\{.*?\})\s*</tool_call>", re.DOTALL)
  16. TOOL_CALL_JSON_RE = re.compile(
  17. r"\{\s*\"id\"\s*:\s*\"[^\"]+\"\s*,\s*\"type\"\s*:\s*\"function\"\s*,\s*\"function\"\s*:\s*\{.*?\}\s*\}", re.DOTALL
  18. )
  19. TOOL_CALL_CONTRACT = (
  20. "Available tools (OpenAI function schema). "
  21. "When using a tool, emit ONLY <tool_call>{...}</tool_call> with one JSON object "
  22. "containing id/type/function{name,arguments}. arguments must be a JSON string."
  23. )
  24. __all__ = [
  25. "TOOL_CALL_BLOCK_RE",
  26. "TOOL_CALL_CONTRACT",
  27. "TOOL_CALL_JSON_RE",
  28. "StreamChunks",
  29. "build_openai_tool_call",
  30. "completion_to_stream_chunks",
  31. "extract_tool_calls_from_text",
  32. "render_tool_bridge_sections",
  33. "tool_specs_from_openai_tools",
  34. ]
  35. class StreamChunks(list):
  36. """Chunk list that also carries response-level attributes (e.g. ``hermes_projected_messages``)
  37. Hermes reads off the ``create`` result; a plain list would drop them on the stream path."""
  38. def completion_to_stream_chunks(completion: SimpleNamespace) -> StreamChunks:
  39. """Re-shape a one-shot ACP response as OpenAI stream chunks (data chunk + usage chunk); response-level
  40. attributes other than choices/usage/model are copied onto the result."""
  41. choice = completion.choices[0]
  42. message = choice.message
  43. tool_call_deltas = None
  44. if message.tool_calls:
  45. tool_call_deltas = [
  46. SimpleNamespace(
  47. index=index, id=getattr(tool_call, "id", None), type=getattr(tool_call, "type", "function"),
  48. function=SimpleNamespace(name=getattr(tool_call.function, "name", None),
  49. arguments=getattr(tool_call.function, "arguments", None)),
  50. )
  51. for index, tool_call in enumerate(message.tool_calls)
  52. ]
  53. delta = SimpleNamespace(
  54. role="assistant", content=message.content or None, tool_calls=tool_call_deltas,
  55. reasoning_content=getattr(message, "reasoning_content", None), reasoning=getattr(message, "reasoning", None),
  56. )
  57. data_chunk = SimpleNamespace(
  58. choices=[SimpleNamespace(index=0, delta=delta, finish_reason=choice.finish_reason)],
  59. model=completion.model, usage=None,
  60. )
  61. usage_chunk = SimpleNamespace(choices=[], model=completion.model, usage=completion.usage)
  62. chunks = StreamChunks([data_chunk, usage_chunk])
  63. for key, value in vars(completion).items():
  64. if key not in ("choices", "usage", "model"):
  65. setattr(chunks, key, value)
  66. return chunks
  67. def build_openai_tool_call(*, call_id: str, name: str, arguments: str) -> ChatCompletionMessageToolCall:
  68. """Build an OpenAI-compatible tool-call object for downstream handling."""
  69. return ChatCompletionMessageToolCall(
  70. id=call_id, call_id=call_id, response_item_id=None, type="function",
  71. function=Function(name=name, arguments=arguments),
  72. )
  73. def _named_function(container: Any) -> tuple[dict[str, Any], str] | None:
  74. """``(fn, stripped name)`` from ``container["function"]`` when it is a dict with a non-blank name, else None."""
  75. fn = container.get("function") if isinstance(container, dict) else None
  76. name = fn.get("name") if isinstance(fn, dict) else None
  77. return (fn, name.strip()) if isinstance(name, str) and name.strip() else None
  78. def tool_specs_from_openai_tools(
  79. tools: list[dict[str, Any]] | None, *, allowlist: Iterable[str] | None = None,
  80. ) -> list[dict[str, Any]]:
  81. """Flatten OpenAI ``tools`` into ``{name, description, parameters}`` specs; malformed entries are skipped."""
  82. allowed = {str(n).strip() for n in allowlist} if allowlist is not None else None
  83. specs: list[dict[str, Any]] = []
  84. for t in tools or []:
  85. named = _named_function(t)
  86. if named is None or (allowed is not None and named[1] not in allowed):
  87. continue
  88. fn, name = named
  89. specs.append({"name": name, "description": fn.get("description", ""), "parameters": fn.get("parameters", {})})
  90. return specs
  91. def render_tool_bridge_sections(
  92. tools: list[dict[str, Any]] | None, tool_choice: Any = None, *, allowlist: Iterable[str] | None = None,
  93. ) -> list[str]:
  94. """Prompt sections carrying the forwarded tool schemas + choice hint (empty list when neither applies)."""
  95. specs = tool_specs_from_openai_tools(tools, allowlist=allowlist)
  96. sections: list[str] = []
  97. if specs:
  98. sections.append(TOOL_CALL_CONTRACT + "\n" + json.dumps(specs, ensure_ascii=False))
  99. if tool_choice is not None:
  100. sections.append(f"Tool choice hint: {json.dumps(tool_choice, ensure_ascii=False)}")
  101. return sections
  102. def _parse_tool_call(raw_json: str, ordinal: int) -> ChatCompletionMessageToolCall | None:
  103. """One ``<tool_call>`` JSON body → tool call, or None when malformed. Missing id → ``acp_call_<ordinal>``."""
  104. try:
  105. obj = json.loads(raw_json)
  106. except Exception:
  107. return None
  108. named = _named_function(obj)
  109. if named is None:
  110. return None
  111. fn, fn_name = named
  112. fn_args = fn.get("arguments", "{}")
  113. if not isinstance(fn_args, str):
  114. fn_args = json.dumps(fn_args, ensure_ascii=False)
  115. call_id = obj.get("id")
  116. if not isinstance(call_id, str) or not call_id.strip():
  117. call_id = f"acp_call_{ordinal}"
  118. return build_openai_tool_call(call_id=call_id, name=fn_name, arguments=fn_args)
  119. def extract_tool_calls_from_text(text: str) -> tuple[list[ChatCompletionMessageToolCall], str]:
  120. """Pull ``<tool_call>`` blocks out of an ACP response → ``(tool_calls, cleaned_text)`` with the consumed blocks
  121. removed so the assistant message doesn't show raw JSON. Bare-JSON fallback runs only when no XML block parsed."""
  122. if not isinstance(text, str) or not text.strip():
  123. return [], ""
  124. extracted: list[ChatCompletionMessageToolCall] = []
  125. consumed_spans: list[tuple[int, int]] = []
  126. for pattern, group in ((TOOL_CALL_BLOCK_RE, 1), (TOOL_CALL_JSON_RE, 0)):
  127. for m in pattern.finditer(text):
  128. call = _parse_tool_call(m.group(group), len(extracted) + 1)
  129. if call is not None:
  130. extracted.append(call)
  131. consumed_spans.append((m.start(), m.end()))
  132. if extracted:
  133. break
  134. if not consumed_spans:
  135. return extracted, text.strip()
  136. consumed_spans.sort()
  137. merged: list[tuple[int, int]] = []
  138. for start, end in consumed_spans:
  139. if not merged or start > merged[-1][1]:
  140. merged.append((start, end))
  141. else:
  142. merged[-1] = (merged[-1][0], max(merged[-1][1], end))
  143. parts: list[str] = []
  144. cursor = 0
  145. for start, end in merged:
  146. if cursor < start:
  147. parts.append(text[cursor:start])
  148. cursor = max(cursor, end)
  149. if cursor < len(text):
  150. parts.append(text[cursor:])
  151. cleaned = "\n".join(p.strip() for p in parts if p and p.strip()).strip()
  152. return extracted, cleaned