nazdridoy commited on
Commit
9aff9bb
·
verified ·
1 Parent(s): bde6cbf

update(utils): dynamic reasoning toggle behavior

Browse files

- [refactor] Replace `open_block` with `open_block_open` and `open_block_closed` (utils.py:328-330)
- [update] Expand reasoning block when `</think>` tag is missing (render_with_reasoning_toggle)
- [update] Collapse reasoning block by default when `</think>` tag is present (render_with_reasoning_toggle)

Files changed (1) hide show
  1. utils.py +15 -7
utils.py CHANGED
@@ -325,15 +325,23 @@ def render_with_reasoning_toggle(text: str, show_reasoning: bool) -> str:
325
  return pattern_strip.sub("", text)
326
 
327
  # Show reasoning: stream it as it arrives by converting tags into a collapsible details block
328
- open_block = "<details><summary>Reasoning</summary>\n\n```text\n"
 
329
  close_block = "\n```\n</details>\n"
330
 
331
- # Convert opening tag when first seen; idempotent if it's already converted
332
- if "<think>" in text:
333
- text = re.sub(r"<think>", open_block, text, flags=re.IGNORECASE)
 
 
 
 
334
 
335
- # Convert closing tag when it appears
336
- if "</think>" in text:
337
- text = re.sub(r"</think>", close_block, text, flags=re.IGNORECASE)
 
 
 
338
 
339
  return text
 
325
  return pattern_strip.sub("", text)
326
 
327
  # Show reasoning: stream it as it arrives by converting tags into a collapsible details block
328
+ open_block_open = "<details open><summary>Reasoning</summary>\n\n```text\n"
329
+ open_block_closed = "<details><summary>Reasoning</summary>\n\n```text\n"
330
  close_block = "\n```\n</details>\n"
331
 
332
+ # If the closing tag is not present yet, keep the block expanded while streaming
333
+ if "</think>" not in text:
334
+ # Replace any raw <think> with an expanded details block
335
+ text = re.sub(r"<think>", open_block_open, text, flags=re.IGNORECASE)
336
+ # If for any reason a closed details opening exists, switch it to open (expanded)
337
+ text = text.replace(open_block_closed, open_block_open)
338
+ return text
339
 
340
+ # If the closing tag is present, render a collapsed block by default
341
+ # 1) Ensure opening is the closed variant
342
+ text = re.sub(r"<think>", open_block_closed, text, flags=re.IGNORECASE)
343
+ text = text.replace(open_block_open, open_block_closed)
344
+ # 2) Close the block
345
+ text = re.sub(r"</think>", close_block, text, flags=re.IGNORECASE)
346
 
347
  return text