This commit is contained in:
Mikan
2026-06-21 09:24:42 +03:00
parent 7cbe8da103
commit c45ab1ddd5
24 changed files with 1438 additions and 148 deletions

View File

@@ -88,7 +88,8 @@ async def run_iteration(
sse_emitter=sse.emit)
tools = registry.to_openai_format("orchestrator_phase2")
phase2_msg: dict[str, Any] = {}
for retry in range(3):
phase2_retries = int(settings.get("llm.tool_retry_attempts", 3))
for retry in range(phase2_retries + 1):
resp = await llm.complete(
stage="orchestrator_phase2",
messages=messages,
@@ -158,12 +159,13 @@ async def run_iteration(
db=db, world=world, step=step, llm=llm, sse=sse, settings=settings,
)
# 3.3 Suggest actions
# 3.3 Suggest actions — retry up to tool_retry_attempts if no tools returned
suggest_msgs = await build_orchestrator_phase3_suggest_context(
db=db, world=world, scene_text=step.scene_text or "", settings=settings,
)
suggest_tools = registry.to_openai_format("orchestrator_phase3_suggest")
for retry in range(2):
tool_retry_limit = int(settings.get("llm.tool_retry_attempts", 3))
for retry in range(tool_retry_limit + 1):
resp = await llm.complete(
stage="orchestrator_phase3_suggest",
messages=suggest_msgs,
@@ -173,9 +175,19 @@ async def run_iteration(
world_id=world.id, step_id=step.id, session=db,
)
msg = resp.get("message", {})
# Apply text replacements
content = msg.get("content", "") or ""
if content:
from app.core.settings_service import apply_text_replacements
content = await apply_text_replacements(db, content)
msg = dict(msg)
msg["content"] = content
tcs = msg.get("tool_calls") or []
found = False
for tc in tcs:
fn = tc.get("function", {})
fn = tc.get("function", {}) if isinstance(tc, dict) else {}
if not isinstance(fn, dict):
fn = {"name": tc.get("name", ""), "arguments": tc.get("arguments", "{}")}
if fn.get("name") == "suggest_actions":
try:
args = json.loads(fn.get("arguments") or "{}")
@@ -185,11 +197,24 @@ async def run_iteration(
if result.ok:
step.suggested_actions = result.data.get("actions", [])
await sse.emit("suggested_actions", {"actions": step.suggested_actions})
found = True
break
if step.suggested_actions:
if found:
break
# No suggest_actions call — retry with nudge
suggest_msgs.append(msg)
suggest_msgs.append({"role": "user", "content": "Call suggest_actions with 1-3 actions."})
suggest_msgs.append({
"role": "user",
"content": (
"You did not call suggest_actions. "
"You MUST call the suggest_actions tool with 1-3 short action strings. "
"If you tried before and it didn't work, try again with proper JSON arguments."
),
})
# If still no actions after retries, provide defaults
if not step.suggested_actions:
step.suggested_actions = ["Continue exploring", "Talk to someone nearby"]
await sse.emit("suggested_actions", {"actions": step.suggested_actions})
await db.commit()
await sse.emit("iteration_complete", {