chenge commited on
Commit
90bf4d7
·
1 Parent(s): 8c18698

增加自动折叠,图片功能

Browse files
Files changed (1) hide show
  1. app.py +19 -13
app.py CHANGED
@@ -473,9 +473,8 @@ class Gradio_Events:
473
  state: gr.update(value=state_value)
474
  }
475
 
476
- # 然后清除just_finished_thinking标记(如果有内容的话)
477
- if history[-1]["content"] and history[-1]["content"].strip():
478
- history[-1]["meta"]["just_finished_thinking"] = False
479
  continue
480
 
481
  # 如果在thinking模式中,只更新thinking内容,不修改content
@@ -491,8 +490,8 @@ class Gradio_Events:
491
  history[-1]["content"] = ""
492
  history[-1]["content"] += content
493
  # 清除"刚完成thinking"标记,因为现在在正常输出内容
494
- # 只在第一次输出内容时清除标记
495
- if history[-1]["meta"].get("just_finished_thinking") and history[-1]["content"].strip():
496
  history[-1]["meta"]["just_finished_thinking"] = False
497
 
498
  yield {
@@ -1649,20 +1648,27 @@ with gr.Blocks(css=css, fill_width=True, head=drag_and_paste_js) as demo:
1649
  const thinking_done = bubble?.meta?.thinking_done || false
1650
  const just_finished_thinking = bubble?.meta?.just_finished_thinking || false
1651
 
1652
- // 只在刚完成thinking且有回答内容时自动折叠
 
 
1653
  const shouldAutoCollapse = just_finished_thinking && content && content.trim().length > 0
1654
 
1655
- // 使用组合的key和状态策略
1656
  let collapseKey = 'thinking'
1657
  let collapseProps = {}
1658
 
1659
  if (shouldAutoCollapse) {
1660
- // 刚完成thinking且有内容时,强制折叠一次
1661
- collapseKey = 'thinking-auto-collapsed'
1662
- collapseProps.active_key = []
 
 
 
 
 
1663
  } else {
1664
- // 其他情况使用默认行为,允许用户控制
1665
- collapseKey = thinking_done ? 'thinking-done' : 'thinking-active'
1666
  collapseProps.default_active_key = ['1']
1667
  }
1668
 
@@ -1673,7 +1679,7 @@ with gr.Blocks(css=css, fill_width=True, head=drag_and_paste_js) as demo:
1673
  display: (thinking_content || is_thinking) ? 'block' : 'none',
1674
  marginBottom: thinking_content || is_thinking ? '12px' : '0'
1675
  }
1676
- }, collapseProps), // 动态属性:active_key 或 default_active_key
1677
  thinking_label: is_thinking ? '🤔 正在思考...' : '🤔 思考过程',
1678
  thinking_markdown: {
1679
  value: thinking_content || '思考中...'
 
473
  state: gr.update(value=state_value)
474
  }
475
 
476
+ # 延迟清除just_finished_thinking标记的逻辑移到下次yield时处理
477
+ # 这样确保前端有足够时间检测并执行自动折叠
 
478
  continue
479
 
480
  # 如果在thinking模式中,只更新thinking内容,不修改content
 
490
  history[-1]["content"] = ""
491
  history[-1]["content"] += content
492
  # 清除"刚完成thinking"标记,因为现在在正常输出内容
493
+ # 在输出后续内容时自动清除标记,让用户可以重新控制折叠状态
494
+ if history[-1]["meta"].get("just_finished_thinking"):
495
  history[-1]["meta"]["just_finished_thinking"] = False
496
 
497
  yield {
 
1648
  const thinking_done = bubble?.meta?.thinking_done || false
1649
  const just_finished_thinking = bubble?.meta?.just_finished_thinking || false
1650
 
1651
+ // 改进的自动折叠逻辑:
1652
+ // 1. 刚完成thinking且有回答内容时自动折叠
1653
+ // 2. 考虑用户交互状态,避免频繁重置
1654
  const shouldAutoCollapse = just_finished_thinking && content && content.trim().length > 0
1655
 
1656
+ // 动态生成唯一key以触发组件重新渲染,但保持用户控制能力
1657
  let collapseKey = 'thinking'
1658
  let collapseProps = {}
1659
 
1660
  if (shouldAutoCollapse) {
1661
+ // 刚完成thinking且有内容时,设置为折叠状态
1662
+ // 使用时间戳确保key的唯一性,触发折叠
1663
+ collapseKey = 'thinking-auto-collapsed-' + Date.now()
1664
+ collapseProps.active_key = [] // 强制折叠
1665
+ } else if (thinking_done) {
1666
+ // thinking完成但用户可能已经手动展开,使用稳定key
1667
+ collapseKey = 'thinking-user-controlled'
1668
+ // 不设置active_key,让用户控制
1669
  } else {
1670
+ // thinking进行中,默认展开
1671
+ collapseKey = 'thinking-active'
1672
  collapseProps.default_active_key = ['1']
1673
  }
1674
 
 
1679
  display: (thinking_content || is_thinking) ? 'block' : 'none',
1680
  marginBottom: thinking_content || is_thinking ? '12px' : '0'
1681
  }
1682
+ }, collapseProps),
1683
  thinking_label: is_thinking ? '🤔 正在思考...' : '🤔 思考过程',
1684
  thinking_markdown: {
1685
  value: thinking_content || '思考中...'