Files
wehub-resource-sync 26382a7ac6
CI / Clippy (push) Failing after 15m13s
CI / Test (ubuntu-latest) (push) Failing after 16m1s
CI / Test (macos-latest) (push) Has been cancelled
CI / Test (windows-latest) (push) Has been cancelled
CI / Build (no embeddings / no ORT) (push) Has been cancelled
CI / Format (push) Has been cancelled
CI / Cookbook (Node) (push) Has been cancelled
CI / Pi Extension (Node) (push) Has been cancelled
CI / Rust SDK (lean-ctx-client) (push) Has been cancelled
CI / Embed SDK (lean-ctx-sdk) (push) Has been cancelled
CI / Python SDK (leanctx) (push) Has been cancelled
CI / Hermes Plugin (Python) (push) Has been cancelled
CI / SDK Conformance Matrix (push) Has been cancelled
CI / Coverage (push) Has been cancelled
CI / cargo-deny (push) Has been cancelled
CI / Adversarial Safety (push) Has been cancelled
CI / Benchmarks (push) Has been cancelled
CI / Output-Quality Gate (eval A/B) (push) Has been cancelled
CI / Documentation (push) Has been cancelled
CI / CI Green (push) Has been cancelled
JetBrains Plugin / Actionlint (push) Has been cancelled
CodeQL / Analyze (actions) (push) Has been cancelled
CodeQL / Analyze (javascript-typescript) (push) Has been cancelled
CodeQL / Analyze (rust) (push) Has been cancelled
JetBrains Plugin / Validation (push) Has been cancelled
JetBrains Plugin / Build (push) Has been cancelled
JetBrains Plugin / Test (push) Has been cancelled
Security Check / Security Scan (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:35:30 +08:00

47 lines
1.7 KiB
Python

"""OpenAI function-calling adapter (EPIC 12.6).
Pure transformation — no `openai` package required. Turns the lean-ctx tool
surface into OpenAI ``tools=[...]`` specs and executes the tool calls the model
returns. Works with both the Chat Completions and Responses tool-call shapes.
"""
from __future__ import annotations
from typing import Any, Dict, List
from ..client import LeanCtxClient
from ._common import coerce_arguments, normalized_tool_specs
def to_openai_tools(client: LeanCtxClient) -> List[Dict[str, Any]]:
"""Return lean-ctx tools as OpenAI function-tool specs."""
return [
{
"type": "function",
"function": {
"name": spec.name,
"description": spec.description,
"parameters": spec.parameters,
},
}
for spec in normalized_tool_specs(client)
]
def run_openai_tool_call(client: LeanCtxClient, tool_call: Any) -> str:
"""Execute one OpenAI tool call and return the tool's text result.
Accepts either the dict shape (``{"function": {"name", "arguments"}}``) or
an SDK object exposing ``.function.name`` / ``.function.arguments``.
"""
function = tool_call.get("function") if isinstance(tool_call, dict) else getattr(tool_call, "function", None)
if function is None:
raise ValueError("tool_call has no 'function'")
name = function.get("name") if isinstance(function, dict) else getattr(function, "name", None)
if not name:
raise ValueError("tool_call.function has no 'name'")
raw_args = (
function.get("arguments") if isinstance(function, dict) else getattr(function, "arguments", None)
)
return client.call_tool_text(name, coerce_arguments(raw_args))