authoring: dedicated llm provider for skill title/description

Move new category/skill draft authoring off SemIfEngine.generate onto a
separate small OpenAI-compatible provider, deliberately distinct from
codegen. Extract the shared transport (SSE/budget/idle/degeneration) into
provider.OpenAICompatClient; LLMClient and CodegenClient subclass it with
their own endpoints, sampler defaults, and error types.

Draft authoring now runs on its own async single-slot worker: a create
request queues, the gate stays free, a category job chains into its skill
job, and the original request re-runs when the body lands. An unreachable
llm endpoint is graceful (draft_failed trace, user notified, no
re-dispatch); only SemIf availability is fatal. Elicitation is deferred on
every front end (REPL included) now that authoring is async.
This commit is contained in:
Denton Social committed 2026-09-29 22:18:01 -05:00
1 parent ea3d79be99
commit fb5303928b
14 files changed
+1140 -957

No files matched your search

+69 -28
View File
@@ -1,41 +1,82 @@
"""OpenAI-compatible LLM client — retained but dormant.
"""OpenAI-compatible provider for skill title + description authoring.
Decision-making (assessment, requeue, fidelity) now belongs to SemIf; this
client no longer participates in any decision path. The `llm` endpoint stays
configured for now (retirement is backlogged), so the class remains as the
stdlib transport plus `_parse_json`, which the authoring parsers borrow. Uses
only the stdlib HTTP client so the core stays dependency-free.
New categories and skills are named by a small, local OpenAI-compatible model
on the `llm` endpoint, deliberately separate from the larger `codegen` model
that writes runnable bodies. It uses the same provider machinery as
`CodegenClient` (`provider.OpenAICompatClient`): stdlib-only HTTP, a real SSE
stream, a token budget, and an idle watchdog — just with its own endpoint,
model, sampler defaults, and error type. `_parse_json` is also borrowed by the
authoring parsers.
"""
from __future__ import annotations
import json
import urllib.error
import urllib.request
from .provider import OpenAICompatClient, ProviderError
class LLMClient:
def __init__(self, base_url: str, model: str, timeout: float = 120.0):
self.base_url = base_url.rstrip("/")
self.model = model
self.timeout = timeout
class LLMError(ProviderError):
"""The configured `llm` endpoint could not be reached or stalled."""
def _chat(self, messages: list[dict], temperature: float = 0.0) -> str:
url = f"{self.base_url}/chat/completions"
body = json.dumps(
{"model": self.model, "messages": messages, "temperature": temperature}
).encode("utf-8")
request = urllib.request.Request(
url, data=body, headers={"Content-Type": "application/json"}
class LLMClient(OpenAICompatClient):
"""Small-model author of skill/category titles + descriptions.
Short JSON replies, so the defaults are conservative: low temperature, no
presence penalty, a modest timeout, and single-shot generation. Every value
is overridable from the `llm` block in `config.json`.
"""
error_class = LLMError
degeneration_error_class = LLMError
label = "llm"
def __init__(
self,
base_url: str,
model: str,
timeout: float = 600.0,
stream: bool = False,
idle_warn: float = 30.0,
idle_timeout: float = 120.0,
context_window: float = 0.0,
smart_limit: int = 250000,
warn_limit: int = 500000,
max_fill_ratio: float = 0.9,
warn_fill_ratio: float = 0.7,
max_output: float = 0.85,
chars_per_token: float = 4.0,
degeneration_interval: int = 8000,
degeneration_window: int = 2000,
degeneration_min_chars: int = 4000,
temperature: float = 0.2,
top_p: float = 0.9,
presence_penalty: float = 0.0,
frequency_penalty: float = 0.0,
):
super().__init__(
base_url=base_url,
model=model,
timeout=timeout,
stream=stream,
idle_warn=idle_warn,
idle_timeout=idle_timeout,
context_window=context_window,
smart_limit=smart_limit,
warn_limit=warn_limit,
max_fill_ratio=max_fill_ratio,
warn_fill_ratio=warn_fill_ratio,
max_output=max_output,
chars_per_token=chars_per_token,
degeneration_interval=degeneration_interval,
degeneration_window=degeneration_window,
degeneration_min_chars=degeneration_min_chars,
temperature=temperature,
top_p=top_p,
presence_penalty=presence_penalty,
frequency_penalty=frequency_penalty,
)
try:
with urllib.request.urlopen(request, timeout=self.timeout) as response:
payload = json.loads(response.read().decode("utf-8"))
except urllib.error.URLError as exc:
raise RuntimeError(
f"LLM endpoint unreachable at {url}: {exc}. Is your local server running?"
) from exc
return payload["choices"][0]["message"]["content"]
@staticmethod
def _parse_json(raw: str) -> dict: