fix(chat): map customer/seller roles for OpenAI-compat LLM; re-contact = mid-chat time-lapse

- llm.complete_conversation now maps internal roles (customer->assistant, seller->user,
  system->system) before the API call — fixes 'Unknown role: customer' (501).
- Re-contact personality: the customer now chats normally, at turn 2 goes quiet and a
  system time-lapse note is shown (' ผ่านไป 2-3 สัปดาห์...'), then re-engages warmer —
  instead of 'pretending you asked before' at start. Driven by persona.recontact trait.
All 9 backend suites pass. Rebuilt dist.
This commit is contained in:
Macky
2026-08-09 11:25:50 +07:00
parent fd2cae9e62
commit 479d77757f
2 changed files with 30 additions and 1 deletions

View File

@@ -116,12 +116,27 @@ class LLMClient:
temperature: float = 0.6,
max_tokens: int = 1200,
) -> str:
# Normalize internal role labels (customer/seller) to the roles an OpenAI-compatible
# chat endpoint accepts: system/user/assistant. customer=assistant (the persona/LLM),
# seller=user (the trainee). Anything else maps to a safe default.
api_messages = []
for m in messages:
role = (m.get("role") or "").lower()
if role == "system":
mapped = "system"
elif role in ("customer", "assistant"):
mapped = "assistant"
elif role in ("seller", "user"):
mapped = "user"
else:
mapped = "user"
api_messages.append({"role": mapped, "content": m.get("text") or m.get("content") or ""})
try:
resp = self.client.chat.completions.create(
model=self.model,
temperature=temperature,
max_tokens=max_tokens,
messages=messages,
messages=api_messages,
)
except Exception as exc:
raise LLMError(f"LLM call failed: {exc}") from exc