kiln_ai.sandbox.entrypoint

Shared entry-point caller for sandbox children.

Stdlib only — no Pydantic / Kiln-model / DB / UI imports.

Handles both def and async def entry points transparently: if the call returns a coroutine, it is driven to completion with asyncio.run().

 1"""Shared entry-point caller for sandbox children.
 2
 3Stdlib only — no Pydantic / Kiln-model / DB / UI imports.
 4
 5Handles both ``def`` and ``async def`` entry points transparently:
 6if the call returns a coroutine, it is driven to completion with
 7``asyncio.run()``.
 8"""
 9
10import asyncio
11import inspect
12from typing import Any, Callable
13
14
15def call_entrypoint(fn: Callable, kwargs: dict) -> Any:
16    """Invoke *fn* with *kwargs*, transparently awaiting if async.
17
18    If *fn* is a regular function its return value is returned as-is.
19    If *fn* is an ``async def`` (or returns a coroutine for any other
20    reason, e.g. a decorated/partial wrapper), ``asyncio.run()`` is
21    used to drive the coroutine in the child's main thread.
22
23    This helper is shared between code-eval scorers and code-tool
24    ``run()`` entry points.
25    """
26    result = fn(**kwargs)
27    if inspect.iscoroutine(result):
28        result = asyncio.run(result)
29    return result
def call_entrypoint(fn: Callable, kwargs: dict) -> Any:
16def call_entrypoint(fn: Callable, kwargs: dict) -> Any:
17    """Invoke *fn* with *kwargs*, transparently awaiting if async.
18
19    If *fn* is a regular function its return value is returned as-is.
20    If *fn* is an ``async def`` (or returns a coroutine for any other
21    reason, e.g. a decorated/partial wrapper), ``asyncio.run()`` is
22    used to drive the coroutine in the child's main thread.
23
24    This helper is shared between code-eval scorers and code-tool
25    ``run()`` entry points.
26    """
27    result = fn(**kwargs)
28    if inspect.iscoroutine(result):
29        result = asyncio.run(result)
30    return result

Invoke fn with kwargs, transparently awaiting if async.

If fn is a regular function its return value is returned as-is. If fn is an async def (or returns a coroutine for any other reason, e.g. a decorated/partial wrapper), asyncio.run() is used to drive the coroutine in the child's main thread.

This helper is shared between code-eval scorers and code-tool run() entry points.