implemenation on the ai agents and registry

This commit is contained in:
2026-09-30 14:56:08 +05:30
parent aa3bb35733
commit 5a7d32cc04
15 changed files with 803 additions and 72 deletions

View File

@@ -102,7 +102,25 @@ _STALL_SCHEMA = {
}
async def _decide(system: str, schema: dict, valid_actions, context: str):
def request_params(model: Optional[str] = None) -> dict:
"""The model-dependent part of a decision request.
`model` is a per-agent override from the agent registry; None means the
engine default (LLM_MODEL). Claude Haiku 4.5 rejects adaptive thinking and
`effort`, so both are left out for it — otherwise a Haiku pin set from the
console would make every decision fail and fall back to the heuristic.
"""
m = model or LLM_MODEL
output_config = {}
params = {"model": m}
if "haiku" not in m:
params["thinking"] = {"type": "adaptive"}
output_config["effort"] = LLM_EFFORT
params["output_config"] = output_config
return params
async def _decide(system: str, schema: dict, valid_actions, context: str, model: Optional[str] = None):
"""Run one structured-output decision against Claude.
Returns (action, reasoning, confidence), or None on any failure (network,
@@ -111,20 +129,17 @@ async def _decide(system: str, schema: dict, valid_actions, context: str):
"""
resp = None
last_err = None
params = request_params(model)
params["output_config"]["format"] = {"type": "json_schema", "schema": schema}
for attempt in range(2): # initial try + one retry on transient failure
try:
client = _get_client()
resp = await client.messages.create(
model=LLM_MODEL,
max_tokens=LLM_MAX_TOKENS,
system=system,
thinking={"type": "adaptive"},
output_config={
"effort": LLM_EFFORT,
"format": {"type": "json_schema", "schema": schema},
},
messages=[{"role": "user", "content": context}],
timeout=LLM_TIMEOUT_S,
**params,
)
break
except Exception as e:
@@ -154,10 +169,10 @@ async def _decide(system: str, schema: dict, valid_actions, context: str):
return None
async def decide_stall_response(context: str) -> Optional[StallDecision]:
async def decide_stall_response(context: str, model: Optional[str] = None) -> Optional[StallDecision]:
"""Ask Claude how to handle a stalled miler. None on failure (caller falls
back to deterministic behaviour)."""
r = await _decide(_STALL_SYSTEM, _STALL_SCHEMA, _VALID_ACTIONS, context)
back to deterministic behaviour). `model` overrides LLM_MODEL."""
r = await _decide(_STALL_SYSTEM, _STALL_SCHEMA, _VALID_ACTIONS, context, model)
return StallDecision(*r) if r else None
@@ -224,8 +239,8 @@ def build_assignment_failure_context(facts, *, now=None) -> str:
return "\n".join(lines)
async def decide_assignment_failure(context: str) -> Optional[AssignmentDecision]:
async def decide_assignment_failure(context: str, model: Optional[str] = None) -> Optional[AssignmentDecision]:
"""Ask Claude how to react to a failed assignment. None on failure (caller
falls back to deterministic behaviour)."""
r = await _decide(_ASSIGNMENT_SYSTEM, _ASSIGNMENT_SCHEMA, _ASSIGNMENT_ACTIONS, context)
falls back to deterministic behaviour). `model` overrides LLM_MODEL."""
r = await _decide(_ASSIGNMENT_SYSTEM, _ASSIGNMENT_SCHEMA, _ASSIGNMENT_ACTIONS, context, model)
return AssignmentDecision(*r) if r else None