feat: 后台任务系统 + JSON容错解析 + SSE心跳保活 + 多项Bug修复

新功能:
- 大纲/章节生成改为服务端后台任务,支持断线续传
- 后台任务队列排队执行,按用户排队(同用户串行不同用户并发)
- 章节管理页面添加后台任务列表弹窗和进度面板
- 章节状态添加 pending(待处理)选项
- 集成json5容错解析器 + 上下文感知JSON修复
- SSE流式生成添加心跳保活,防止连接超时
- SSEPostClient添加credentials:include修复network error
- 每章最大伏笔数从2调整为5
- 添加大纲读区伏笔的功能

Bug修复:
- 修复AI生成JSON中未转义引号/中文标点/多对象属性值未合并
- 修复JSON非法转义字符清洗和中文引号处理
- 修复MCP插件TimeoutError/连接失败上下文清理
- MCP插件后台注册添加重试机制
- 续写模式添加缺失的mcp_references参数
- 修复Alembic迁移链分叉
- 使用torch CPU版本加速Docker构建
This commit is contained in:
未来
2026-04-29 08:31:07 +08:00
parent 1f80a58994
commit 2bd8b61e91
20 changed files with 2873 additions and 151 deletions
+339 -89
View File
@@ -26,15 +26,101 @@ _QUOTE_MAP = {
}
def _is_content_quote(text: str, pos: int) -> bool:
"""
判断字符串值内的 '"' 是否为内容引号(需转义)而非 JSON 结束引号。
合法 JSON 中,字符串结束引号之后的非空白字符必须是:
',' (值分隔) / '}' (关闭对象) / ']' (关闭数组)
如果 '"' 后面不符合这些模式,则是 AI 写入的内容引号,需要转义。
"""
j = pos + 1
# 跳过空格和制表符
while j < len(text) and text[j] in ' \t':
j += 1
if j >= len(text):
return False # 文本末尾,视为结束引号
ch = text[j]
# } 或 ] → 结束引号
if ch in ('}', ']'):
return False
# 换行 → 检查下一行开头判断
if ch == '\n' or ch == '\r':
k = j + (2 if (ch == '\r' and j + 1 < len(text) and text[j + 1] == '\n') else 1)
while k < len(text) and text[k] in ' \t':
k += 1
if k >= len(text):
return False
# 下一行以 " (JSON key) 或 } 或 ] 开头 → 结束引号
if text[k] == '"' or text[k] in ('}', ']'):
return False
return True
# , → 需要检查逗号后面是什么
if ch == ',':
k = j + 1
while k < len(text) and text[k] in ' \t':
k += 1
if k >= len(text):
return False
# 逗号后跟换行 → 检查下一行
if text[k] in ('\n', '\r'):
k2 = k + (2 if (text[k] == '\r' and k + 1 < len(text) and text[k + 1] == '\n') else 1)
while k2 < len(text) and text[k2] in ' \t\n\r':
k2 += 1
if k2 >= len(text):
return False
if text[k2] == '"' or text[k2] in ('}', ']'):
return False
return True
after_comma = text[k]
# 结构性逗号后应为 JSON 值的开头
if after_comma == '"':
return False # 字符串值或 key
if after_comma.isdigit() or after_comma == '-':
return False # 数字
if after_comma in ('{', '['):
return False # 对象/数组
if text[k:k+4] in ('true', 'null'):
return False
if text[k:k+5] == 'false':
return False
# 逗号后不是 JSON 值开头 → 内容逗号,引号是内容引号
return True
# : → 通常在字符串结束后不可能出现,保守处理为结束引号
if ch == ':':
return False
# 其他字符(中文、字母等)→ 内容引号
return True
def _fix_json_string_values(text: str) -> str:
"""
修复JSON字符串值中的常见问题:
1. 裸换行符/制表符 → 转义
2. 字符串值内的中文引号 → 转义为ASCII引号(避免破坏JSON结构)
3. 结构位置的中文引号 → 直接替换为ASCII引号
上下文感知的 JSON 修复,区分字符串内外分别处理。
AI生成的JSON常在字符串值中插入未转义的换行符和中文引号。
此函数遍历文本,区分字符串内外,分别处理。
字符串值内:
1. 裸换行符/制表符 → 转义
2. 中文引号(""等) → 转义为 \\"
3. 未转义的 ASCII 双引号 → 智能检测:内容引号转义,结束引号保留
4. 中文逗号/冒号 → 保留原样(是内容字符)
结构位置(字符串外):
1. 中文引号 → ASCII 引号
2. 中文逗号 → ASCII 逗号
3. 中文冒号 → ASCII 冒号
"""
if not text or '"' not in text:
return text
@@ -47,111 +133,234 @@ def _fix_json_string_values(text: str) -> str:
while i < len(text):
c = text[i]
if c == '"' and not in_string:
# 进入字符串
in_string = True
# === 非字符串内(结构位置)===
if not in_string:
# 结构位置的中文标点 → ASCII
if c == '\uff0c': # ,→ ,
result.append(',')
fixed_count += 1
i += 1
continue
if c == '\uff1a': # :→ :
result.append(':')
fixed_count += 1
i += 1
continue
if c in _QUOTE_MAP:
result.append(_QUOTE_MAP[c])
fixed_count += 1
i += 1
continue
# ASCII 双引号 → 进入字符串
if c == '"':
in_string = True
result.append(c)
i += 1
continue
result.append(c)
i += 1
continue
if in_string:
if c == '\\':
# 转义字符,检查下一个字符是否合法
if i + 1 < len(text):
next_c = text[i + 1]
# JSON 合法转义:\" \\ \/ \b \f \n \r \t \uXXXX
if next_c in ('"', '\\', '/', 'b', 'f', 'n', 'r', 't'):
# 合法转义,直接保留
result.append(c)
result.append(next_c)
i += 2
# === 字符串值内 ===
# 转义字符处理
if c == '\\':
if i + 1 < len(text):
next_c = text[i + 1]
if next_c in ('"', '\\', '/', 'b', 'f', 'n', 'r', 't'):
result.append(c)
result.append(next_c)
i += 2
continue
elif next_c == 'u':
if i + 5 < len(text) and all(text[i+2+k] in '0123456789abcdefABCDEF' for k in range(4)):
result.append(text[i:i+6])
i += 6
continue
elif next_c == 'u':
# Unicode 转义 \uXXXX,检查是否有4个十六进制字符
if i + 5 < len(text) and all(text[i+2+k] in '0123456789abcdefABCDEF' for k in range(4)):
result.append(text[i:i+6])
i += 6
continue
else:
# 不完整的unicode转义,去掉反斜杠
result.append(next_c)
fixed_count += 1
i += 2
continue
else:
# 非法转义字符(如 \c \p \d 等),去掉反斜杠只保留字符
result.append(next_c)
fixed_count += 1
i += 2
continue
else:
# 末尾孤立的反斜杠,去掉
result.append(next_c)
fixed_count += 1
i += 1
i += 2
continue
if c == '"':
# 字符串结束
else:
fixed_count += 1
i += 1
continue
# ASCII 双引号 → 智能判断是结束引号还是内容引号
if c == '"':
if _is_content_quote(text, i):
# 内容引号,需要转义
result.append('\\')
result.append('"')
fixed_count += 1
i += 1
continue
else:
# 结束引号
in_string = False
result.append(c)
i += 1
continue
if c == '\n':
# 裸换行符 → 替换为转义换行
result.append('\\')
result.append('n')
fixed_count += 1
i += 1
continue
if c == '\r':
# 裸回车符 → 忽略或替换
if i + 1 < len(text) and text[i + 1] == '\n':
result.append('\\')
result.append('n')
fixed_count += 1
i += 2
else:
result.append('\\')
result.append('n')
fixed_count += 1
i += 1
continue
if c == '\t':
# 裸制表符 → 替换为转义制表符
result.append('\\')
result.append('t')
fixed_count += 1
i += 1
continue
# 字符串值内的中文引号 → 转义为 \"(避免破坏JSON结构)
if c in _QUOTE_MAP:
result.append('\\')
result.append(_QUOTE_MAP[c])
fixed_count += 1
i += 1
continue
# 非字符串内的字符
# 结构位置的中文引号 → 直接替换
if not in_string and c in _QUOTE_MAP:
result.append(_QUOTE_MAP[c])
# 裸换行符 → 转义
if c == '\n':
result.append('\\')
result.append('n')
fixed_count += 1
i += 1
continue
if c == '\r':
if i + 1 < len(text) and text[i + 1] == '\n':
result.append('\\')
result.append('n')
fixed_count += 1
i += 2
else:
result.append('\\')
result.append('n')
fixed_count += 1
i += 1
continue
if c == '\t':
result.append('\\')
result.append('t')
fixed_count += 1
i += 1
continue
# 中文引号处理
if c in _QUOTE_MAP:
mapped = _QUOTE_MAP[c]
if mapped == '"':
# 中文双引号在字符串内需要转义
result.append('\\')
result.append('"')
else:
# 中文单引号在双引号字符串内不需要转义,直接替换
result.append(mapped)
fixed_count += 1
i += 1
continue
# 其他字符(包括中文逗号、中文冒号)→ 保留原样
result.append(c)
i += 1
if fixed_count > 0:
logger.debug(f"✅ 修复了{fixed_count}个JSON问题(控制字符/中文引号")
logger.debug(f"✅ 修复了{fixed_count}个JSON问题(引号/控制字符/中文标点")
return ''.join(result)
def _fix_all_invalid_escapes(text: str) -> str:
"""
兜底修复:扫描整个文本中的无效JSON转义序列。
当 _fix_json_string_values 因字符串边界追踪错误而遗漏某些无效转义时,
此函数作为兜底,不依赖字符串状态追踪,扫描整个文本修复所有无效转义。
有效JSON转义:\\" \\\\ \\/ \\b \\f \\n \\r \\t \\uXXXX
其他 \\X 均为无效转义,修复方式为去掉反斜杠只保留字符。
"""
if '\\' not in text:
return text
result = []
i = 0
fixed = 0
while i < len(text):
if text[i] == '\\' and i + 1 < len(text):
next_c = text[i + 1]
if next_c in ('"', '\\', '/', 'b', 'f', 'n', 'r', 't'):
# 有效转义,保留
result.append(text[i])
result.append(next_c)
i += 2
continue
elif next_c == 'u':
# Unicode 转义,检查是否有4个十六进制字符
if i + 5 < len(text) and all(
text[i + 2 + k] in '0123456789abcdefABCDEF'
for k in range(4)
):
result.append(text[i:i + 6])
i += 6
continue
else:
# 不完整的unicode转义,去掉反斜杠
result.append(next_c)
fixed += 1
i += 2
continue
else:
# 无效转义(如 \引 \影 \某种 等),去掉反斜杠只保留字符
result.append(next_c)
fixed += 1
i += 2
continue
else:
result.append(text[i])
i += 1
if fixed > 0:
logger.info(f"✅ 兜底修复了{fixed}个无效JSON转义序列")
return ''.join(result)
def _fix_multiple_objects_as_value(text: str) -> str:
"""
修复AI生成的JSON中,多个对象作为属性值但未合并的问题。
示例:
"key": {"a": "1"}, {"b": "2"} → "key": {"a": "1", "b": "2"}
AI有时在输出对象类型的属性值时,输出了多个独立的对象而不是合并为一个。
例如 relationship_changes 字段输出多个角色关系变化时可能出现此问题。
此函数检测并合并这些对象。
"""
if '{' not in text or '}' not in text:
return text
# 匹配嵌套层级不超过2的对象: { ... } 其中 ... 不含 { 或仅含单层嵌套
nested_obj = r'\{(?:[^{}]|\{(?:[^{}]|\{[^{}]*\})*\})*\}'
# 模式:属性冒号后跟一个对象,然后逗号和另一个对象(没有属性名)
# 即 "key": {obj1}, {obj2} → "key": {obj1, obj2}
pattern = r'(":)\s*(' + nested_obj + r')\s*,\s*(' + nested_obj + r')'
def merge_objects(match):
colon = match.group(1)
obj1_content = match.group(2)[1:-1] # 去掉外层的 { }
obj2_content = match.group(3)[1:-1] # 去掉外层的 { }
# 合并为一个对象
return f'{colon} {{{obj1_content}, {obj2_content}}}'
prev = None
count = 0
max_iterations = 10
while prev != text and count < max_iterations:
prev = text
text = re.sub(pattern, merge_objects, text)
count += 1
if count > 1:
logger.info(f"✅ 修复了{count - 1}处多对象属性值合并")
return text
def clean_json_response(text: str) -> str:
"""清洗 AI 返回的 JSON(改进版 - 流式安全)"""
try:
@@ -162,11 +371,8 @@ def clean_json_response(text: str) -> str:
original_length = len(text)
logger.debug(f"🔍 开始清洗JSON,原始长度: {original_length}")
# 替换中文逗号/冒号(AI可能在JSON结构位置使用,全局替换是安全的)
text = text.replace('\uff0c', ',') # ,→ ,
text = text.replace('\uff1a', ':') # :→ :
# 修复JSON中的中文引号和裸控制字符(上下文感知,区分字符串内外)
# 上下文感知修复:中文引号/逗号/冒号、裸控制字符、未转义的内容引号
# (区分字符串内外:结构位置替换为ASCII,字符串内保留或转义)
text = _fix_json_string_values(text)
# 去除 markdown 代码块
@@ -286,9 +492,35 @@ def clean_json_response(text: str) -> str:
json.loads(result)
logger.debug(f"✅ 清洗后JSON验证成功")
except json.JSONDecodeError as e:
logger.error(f" 清洗后JSON仍然无效: {e}")
logger.debug(f" 结果预览: {result[:500]}")
logger.debug(f" 结果结尾: ...{result[-200:]}")
logger.warning(f"⚠️ 清洗后JSON仍然无效: {e},尝试修复结构性问题...")
# 修复1:合并多对象属性值(AI可能输出 "key": {a:1}, {b:2}
result = _fix_multiple_objects_as_value(result)
try:
json.loads(result)
logger.info(f"✅ 修复多对象属性值后JSON验证成功")
except json.JSONDecodeError:
pass # 继续尝试其他修复
else:
return result
# 修复2:兜底修复无效转义序列(不依赖字符串边界追踪)
logger.warning(f"⚠️ 继续尝试兜底修复无效转义...")
result = _fix_all_invalid_escapes(result)
try:
json.loads(result)
logger.info(f"✅ 兜底修复后JSON验证成功")
except json.JSONDecodeError as e2:
# 修复3:再次尝试合并多对象属性值(转义修复后可能产生新的合并机会)
result = _fix_multiple_objects_as_value(result)
try:
json.loads(result)
logger.info(f"✅ 二次修复后JSON验证成功")
except json.JSONDecodeError as e3:
logger.error(f"❌ 所有修复后JSON仍然无效: {e3}")
logger.debug(f" 结果预览: {result[:500]}")
logger.debug(f" 结果结尾: ...{result[-200:]}")
return result
@@ -339,6 +571,16 @@ def loads_json(text: str) -> Any:
except (json.JSONDecodeError, Exception):
pass
# 兜底修复无效转义序列后重试
fixed_text = _fix_all_invalid_escapes(text)
if fixed_text != text:
try:
result = json.loads(fixed_text)
logger.info("✅ 兜底修复无效转义后json.loads成功")
return result
except (json.JSONDecodeError, Exception):
pass
# json5 容错解析
if HAS_JSON5:
try:
@@ -347,6 +589,14 @@ def loads_json(text: str) -> Any:
logger.info("✅ json5容错解析成功")
return result
except Exception as e5:
# json5也失败,尝试对修复后的文本使用json5
if fixed_text != text:
try:
result = json5.loads(fixed_text)
logger.info("✅ 兜底修复无效转义后json5容错解析成功")
return result
except Exception:
pass
logger.error(f"❌ json5容错解析也失败: {e5}")
# 最终失败,抛出标准异常