修复小bug

This commit is contained in:
h88782481 2026-03-13 17:53:51 +08:00
parent ae059ffcb8
commit 406a36af89
5 changed files with 92 additions and 13 deletions

View file

@ -165,6 +165,9 @@ def _handle_openai_stream(
for chunk in iter_openai_sse(resp):
if chunk is None:
_dbg(f'流式响应结束,共 {chunk_count} 个数据片段')
close_chunk = think_extractor.finalize()
if close_chunk:
yield sse_data_message(close_chunk)
yield sse_data_message('[DONE]')
return

View file

@ -93,9 +93,14 @@ def _inject_thinking(data):
def _process_stream(resp):
"""处理 /v1/messages 流式响应,检测并注入 thinking 事件"""
"""处理 /v1/messages 流式响应,检测并注入 thinking 事件
追踪上游 content block index在注入 thinking blocks 时使用独立的 index
并将后续上游 block index 偏移避免冲突
"""
reasoning_buf = ''
injected = False
index_offset = 0
for line in resp.iter_lines():
if not line:
@ -119,7 +124,6 @@ def _process_stream(resp):
modified = False
# 提取 reasoning_content
for container_key in ('message', 'delta'):
container = event_data.get(container_key)
if not container:
@ -129,13 +133,17 @@ def _process_stream(resp):
reasoning_buf += rc
modified = True
# 在首个 text_delta 前注入 thinking blocks
if reasoning_buf and not injected:
if event_data.get('delta', {}).get('type') == 'text_delta':
injected = True
yield from _emit_thinking_blocks(reasoning_buf)
index_offset = 1
reasoning_buf = ''
if index_offset and 'index' in event_data:
event_data['index'] = event_data['index'] + index_offset
modified = True
yield f'data: {json.dumps(event_data)}\n\n' if modified else decoded + '\n\n'