Skip to content

Commit 17593d5

Browse files
committed
fix: add register tools
1 parent f87e730 commit 17593d5

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

src/lightrace/client.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
_get_tool_registry,
2020
_set_client_defaults,
2121
_set_otel_exporter,
22+
_tool_registry,
2223
)
2324
from .utils import generate_id, json_serializable
2425

@@ -164,6 +165,57 @@ def _do_register() -> None:
164165

165166
threading.Thread(target=_do_register, daemon=True, name="lightrace-register").start()
166167

168+
def register_tools(self, *tools: Any) -> None:
169+
"""Register tools for invocation from the dashboard.
170+
171+
Accepts LangChain BaseTool objects or plain callables.
172+
After registration, re-syncs with the backend via HTTP.
173+
"""
174+
from .utils import build_json_schema
175+
176+
for t in tools:
177+
if hasattr(t, "name") and hasattr(t, "args_schema"):
178+
name = t.name
179+
func = getattr(t, "coroutine", None) or getattr(t, "func", None)
180+
if func is None:
181+
func = getattr(t, "_arun", None) or getattr(t, "_run", None)
182+
if func is None:
183+
logger.warning("Cannot extract callable from tool %r — skipping", name)
184+
continue
185+
186+
input_schema = None
187+
args_schema = getattr(t, "args_schema", None)
188+
if args_schema is not None:
189+
try:
190+
input_schema = args_schema.model_json_schema()
191+
except Exception:
192+
try:
193+
input_schema = args_schema.schema()
194+
except Exception:
195+
pass
196+
197+
description = getattr(t, "description", None)
198+
_tool_registry[name] = {
199+
"func": func,
200+
"input_schema": input_schema,
201+
"description": description,
202+
}
203+
logger.debug("Registered LangChain tool: %s", name)
204+
205+
elif callable(t):
206+
name = getattr(t, "__name__", str(t))
207+
_tool_registry[name] = {
208+
"func": t,
209+
"input_schema": build_json_schema(t),
210+
"description": None,
211+
}
212+
logger.debug("Registered callable tool: %s", name)
213+
else:
214+
logger.warning("Cannot register tool %r — not a callable or BaseTool", t)
215+
216+
if _tool_registry and self._enabled:
217+
self._register_tools_http()
218+
167219
# ── Flush / shutdown ──────────────────────────────────────────────
168220

169221
def flush(self) -> None:

0 commit comments

Comments
 (0)