Files
quality_analyse/scripts/nao_thinking_stream.py
T
lyun ff7bbe3e46 同步:完善质量分析工作台并更新主题字体
同步已上线的智能分析执行协议、来源查询、图表报告与管理页面配套代码。
补齐数据库迁移、运行依赖和回归测试,保留原有指标计算口径。
统一午夜黑及其他主题的控件细节、中文和业务数字字体。
排除本地业务数据、凭据、预览、发布与验收产物。

验证:生产构建通过,320项前端回归通过;本轮仅同步Git,不再次部署。
2026-10-30 22:40:58 +08:00

144 lines
6.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Transport only thinking text explicitly returned by the configured provider."""
import copy
from datetime import datetime, timezone
import json
import re
import threading
import time
def public_thinking_text(text):
text = re.sub(r'quality-turn:[0-9a-f-]{36}', '', text, flags=re.I)
text = re.sub(r'(?i)Bearer\s+[A-Za-z0-9._~+/-]+', 'Bearer [REDACTED]', text)
text = re.sub(r'(?i)(api[_-]?key|password|token|secret|authorization)(\s*[=::]\s*)[^\s,,;;]+', r'\1\2[REDACTED]', text)
return re.sub(r'\bsk-[A-Za-z0-9_-]+', '[REDACTED]', text)
class ThinkingPublisher:
"""Publish full per-request snapshots; never derive thinking from an answer."""
def __init__(self, emit, interval=0.2, max_chars=1_000_000):
self.emit, self.interval, self.max_chars = emit, interval, max_chars
self.blocks, self.last, self.lock = {}, {}, threading.RLock()
self.closed = False
def receive(self, event):
with self.lock:
if self.closed:
return
key = event['id']
block = self.blocks.setdefault(key, {'id':key,'model':event['model'],
'phase':event['phase'],'status':'running','text':'','version':0,
'source':'api','streaming':event.get('streaming',True),'truncated':False,
'startedAt':datetime.now(timezone.utc).isoformat()})
if event['event'] == 'delta':
available = max(0, self.max_chars-len(block['text']))
delta = event.get('text','')
if delta and 'at' not in block:
block['at'] = datetime.now(timezone.utc).isoformat()
block['text'] += delta[:available]
block['truncated'] |= len(delta) > available
elif event['event'] == 'end':
block['finishedAt'] = datetime.now(timezone.utc).isoformat()
block['finishReason'] = event.get('finishReason','')
block['status'] = ('interrupted' if event.get('interrupted') else
'completed' if block['text'] else 'not_returned')
block['version'] += 1
if event['event'] != 'delta' or time.monotonic()-self.last.get(key,0) + 1e-9 >= self.interval:
self._publish(block)
def _publish(self, block):
public = copy.deepcopy(block)
public['text'] = public_thinking_text(public['text'])
self.last[block['id']] = time.monotonic()
self.emit(public)
def close(self):
with self.lock:
if self.closed:
return
self.closed = True
for block in self.blocks.values():
if block['status'] == 'running':
block['status'] = 'interrupted'
block['finishedAt'] = datetime.now(timezone.utc).isoformat()
block['version'] += 1
self._publish(block)
class ProviderStream:
"""Incremental SSE decoding plus a Chat Completions envelope for JSON callers."""
def __init__(self, on_thinking):
self.on_thinking = on_thinking
self.buffer = b''
self.choices, self.metadata, self.usage = {}, {}, None
self.tools = {}
self.done = False
def feed(self, chunk):
self.buffer += chunk
if len(self.buffer) > 2_000_000:
raise ValueError('Provider stream frame too large')
while b'\n' in self.buffer:
line, self.buffer = self.buffer.split(b'\n',1)
self._line(line.rstrip(b'\r'))
def finish(self):
if self.buffer:
self._line(self.buffer.rstrip(b'\r'))
self.buffer = b''
def _line(self, line):
if not line.startswith(b'data:'):
return
data = line[5:].strip()
if data == b'[DONE]':
self.done = True
return
if data:
self.document(json.loads(data))
def document(self, value):
if not isinstance(value,dict) or value.get('error'):
raise ValueError('Provider returned an invalid completion')
for field in ('id','model','created','system_fingerprint'):
if field in value:
self.metadata[field] = value[field]
if isinstance(value.get('usage'),dict):
self.usage = value['usage']
for choice in value.get('choices',[]):
index = choice.get('index',0)
target = self.choices.setdefault(index,{'index':index,'message':{'role':'assistant','content':''},'finish_reason':None})
delta = choice.get('delta') or choice.get('message') or {}
message = target['message']
for key in ('content','reasoning_content','reasoning'):
text = delta.get(key)
if isinstance(text,str):
message[key] = message.get(key,'') + text
# Aliases represent the same provider field; never double-display both.
thought = delta.get('reasoning_content') or delta.get('reasoning')
if isinstance(thought,str) and thought:
self.on_thinking(thought)
for position, call in enumerate(delta.get('tool_calls') or []):
tool_index = call.get('index',position)
tool = self.tools.setdefault(index,{}).setdefault(tool_index,{'type':'function','function':{'name':'','arguments':''}})
if isinstance(call.get('id'),str):tool['id']=call['id']
if isinstance(call.get('type'),str):tool['type']=call['type']
for field in ('name','arguments'):
part = (call.get('function') or {}).get(field)
if isinstance(part,str):tool['function'][field] += part
if choice.get('finish_reason') is not None:
target['finish_reason'] = choice['finish_reason']
def completion(self):
if not self.choices or any(not row['finish_reason'] for row in self.choices.values()):
raise ValueError('Provider stream ended before completion')
result = {**self.metadata,'object':'chat.completion','choices':list(self.choices.values())}
for choice in result['choices']:
tools = self.tools.get(choice['index'])
if tools:
choice['message']['tool_calls'] = [tools[key] for key in sorted(tools)]
if not choice['message']['content']:choice['message']['content']=None
if self.usage is not None:
result['usage'] = self.usage
return result