Files
DungeonsDragons/backend/app/llm/json_utils.py
T
Thorsten 624644619c Add character detail, markdown rendering, wider layout, and LLM provider switch
- Character sheets gain structured combat_stats, abilities (name +
  plain-language explanation), and equipment fields, populated by the
  upsert_character_sheet tool; the detail page renders them as their
  own sections instead of one prose block, with a player-card-style
  attribute grid (full ability names + modifiers).
- Fix double-escaped unicode occasionally left in tool-call JSON
  arguments (a model quirk) and stop the app's own json.dumps calls
  from re-introducing it (ensure_ascii=False).
- Render chat messages as markdown (react-markdown + Tailwind
  typography) instead of raw text, and widen the games/characters/
  chat layout to use more of the screen on desktop.
- Sharpen the DM system prompt: state the ability + DC for each
  offered action option before the player commits, and narrate rolls
  as DC → result → pass/fail before the story consequence; also stop
  re-asking Session-0 questions already answered in the game's
  name/description.
- Make the DM's LLM provider configurable (xai default, lmstudio for
  a local OpenAI-compatible server via host.docker.internal) instead
  of hardcoded to x.ai.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-01 15:18:15 +02:00

26 lines
1.0 KiB
Python

import re
# Some models occasionally leave literal backslash-u escape sequences un-decoded inside
# tool-call argument strings (a known JSON-generation quirk) — e.g. the string ends up
# containing the six characters ü instead of the character ü. json.loads() alone
# doesn't catch this because the escaping happened *inside* an already-valid JSON string,
# not at the JSON structural level.
_DOUBLE_ESCAPED_UNICODE_RE = re.compile(r"\\u([0-9a-fA-F]{4})")
def _fix_string(text: str) -> str:
if "\\u" not in text:
return text
return _DOUBLE_ESCAPED_UNICODE_RE.sub(lambda m: chr(int(m.group(1), 16)), text)
def fix_double_escaped_unicode(value):
"""Recursively repair double-escaped unicode in a parsed JSON value (dict/list/str)."""
if isinstance(value, str):
return _fix_string(value)
if isinstance(value, list):
return [fix_double_escaped_unicode(v) for v in value]
if isinstance(value, dict):
return {k: fix_double_escaped_unicode(v) for k, v in value.items()}
return value