Repository navigation
Expand file tree
/
Copy pathexecutor.py
More file actions
1991 lines (1796 loc) · 83 KB
/
Copy pathexecutor.py
File metadata and controls
1991 lines (1796 loc) · 83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""路由执行器 — 统一的 tier 迭代门控引擎.
封装 ``route_stream`` / ``route_message`` 共享的 tier 循环、
门控判断与错误处理逻辑,消除两个路由方法间的重复代码。
"""
from __future__ import annotations
import html
import json
import logging
import re
import time
import traceback
from collections.abc import AsyncIterator
from typing import TYPE_CHECKING, Any
import httpx
if TYPE_CHECKING:
from ..config.session_policy import TitleVendorBinding
from ..vendors.base import (
NoCompatibleVendorError,
RequestCapabilities,
VendorResponse,
)
from ..vendors.token_manager import TokenAcquireError, TokenErrorKind
from .error_classifier import (
build_request_capabilities,
extract_error_payload_from_http_status,
is_semantic_rejection,
is_structural_validation_error,
)
from .model_level import infer_bound_level, plan_tier_model
from .rate_limit import (
compute_effective_retry_seconds,
compute_rate_limit_deadline,
parse_rate_limit_headers,
)
from .session_manager import RouteSessionManager
from .session_policy import SessionPolicy, SessionPolicyResolver
from .tier import VendorTier
from .usage_parser import (
build_usage_evidence_records,
has_missing_input_usage_signals,
parse_usage_from_chunk,
)
from .usage_recorder import UsageRecorder
# 向后兼容别名
BackendResponse = VendorResponse
NoCompatibleBackendError = NoCompatibleVendorError
from ..compat.canonical import (
CanonicalPartType,
CompatibilityStatus,
build_canonical_request,
)
from ..model.compat import CanonicalMessagePart, CanonicalRequest
logger = logging.getLogger(__name__)
def _no_compatible_message(reasons: list[str], bound_model: str | None) -> str:
"""无兼容供应商终态的报错文案(流式 / 非流式同型).
含能力类拒绝原因 → 历史文案;仅有 ``model_not_supported``(没有 vendor 认识
绑定模型)或为空(全部 tier 被熔断 / 限速 / 配额跳过)→ 点明绑定模型,
便于排障,不套用「客户端/MCP 能力」文案。
"""
capability_reasons = [r for r in reasons if not r.endswith(":model_not_supported")]
if capability_reasons:
return "当前请求包含仅客户端/MCP 可安全承接的能力,未找到兼容供应商"
if bound_model:
if reasons:
return f"没有可用 vendor 认识绑定模型 {bound_model}(未配置映射或归属 vendor 未启用)"
return f"绑定模型 {bound_model} 及其回退链路均无可用 vendor(熔断、限速或配额)"
if reasons:
return "当前请求包含仅客户端/MCP 可安全承接的能力,未找到兼容供应商"
return "无可用供应商层级"
def _log_unexpected(scope: str, tier_name: str, exc: BaseException) -> None:
"""记录未预期异常:类型 + 堆栈帧,不含 ``str(exc)``(I2).
请求头构建失败等异常的消息体会携带密钥明文(如非法 header value 的
bytes repr),只记类型与调用栈即可定位,不落异常消息。
"""
logger.error(
"Tier %s %s unexpected error: %s\n%s",
tier_name,
scope,
type(exc).__name__,
"".join(traceback.format_tb(exc.__traceback__)),
)
_SESSION_TITLE_MAX_LEN = 600
# 回退标题截取长度 — 工具结果等非用户直接输入的摘要上限。
_FALLBACK_TITLE_MAX_LEN = 80
# 注入式载荷标题截取长度 — 家族标签 + 载荷合计上限,保持列表可扫读。
_INJECTED_PAYLOAD_TITLE_MAX_LEN = 160
# Claude Code 注入的"噪声"标签 — 系统级上下文,不应进入 Session 标题。
# 这些标签由 CC harness 在首个 user 消息 content 中拼接,高度同质,
# 直接用作标题会导致跨会话标题无差异化,丧失辨识度。
_NOISE_TAG_PATTERN = re.compile(
r"<(?P<tag>system-reminder|user-preferences|"
r"local-command-stdout|local-command-stderr|"
r"bash-input|bash-stdout|bash-stderr|"
r"ide_selection|ide_opened_file|stdin|system_instruction|session|"
r"artifactMetadata|thinking)\b[^>]*>"
r".*?</(?P=tag)>",
flags=re.DOTALL | re.IGNORECASE,
)
# <session> 标签需要特殊处理:当用户文本在 <session> 标签内部时,
# 完整块剥离会连同用户文本一起删除。此模式仅去除外壳标签(保留内容),
# 用于首轮完整剥离结果为空时的二次回退提取。
_SESSION_TAG_WRAPPER = re.compile(r"</?session\b[^>]*>", flags=re.IGNORECASE)
# Slash command 子标签:用于识别 /commit、/review 等命令式调用,
# 合成"命令 + 参数"式标题。
_CMD_NAME_PATTERN = re.compile(r"<command-name>(.*?)</command-name>", flags=re.DOTALL)
_CMD_ARGS_PATTERN = re.compile(r"<command-args>(.*?)</command-args>", flags=re.DOTALL)
# 残留 command-* 包裹标签清除(command-message/command-stdout 等次要标签)。
_CMD_WRAPPER_PATTERN = re.compile(
r"<command-[\w-]+>.*?</command-[\w-]+>", flags=re.DOTALL
)
# <pasted_content> 仅剥外壳标签保留内容 — 粘贴体本身是真实用户输入。
_PASTED_CONTENT_WRAPPER = re.compile(r"</?pasted_content\b[^>]*>", re.IGNORECASE)
# [Image #N] 粘贴图片占位符 — 剥离后保留周边真实文本。
_IMAGE_PLACEHOLDER_PATTERN = re.compile(r"\[Image #\d+\]\s*", re.IGNORECASE)
# 空白折叠
_WHITESPACE_PATTERN = re.compile(r"\s+")
def _sanitize_user_text(raw: str) -> str:
"""剔除 Claude Code 注入的系统级 XML 块,还原真实用户输入。
处理顺序:
1. Slash command 优先识别 — 若检测到 <command-name>,合成"命令 + 参数"
式标题(因为残留文本通常为空,直接取标签内容更有意义)。
2. 通用噪声剥离 — 移除已知白名单内的 system-reminder 等标签。
3. <session> 二次回退 — 若首轮剥离后为空,说明用户文本可能在 <session>
标签内部;此时仅去除外壳标签,保留内部文本再做噪声剥离。
4. 残留 command-* 包裹清除 — 兜底去除 command-message 等次要标签。
5. 前后空白归一化 — 折叠连续空白为单空格。
"""
if not raw:
return ""
# 阶段一: slash command 短路
cmd = _CMD_NAME_PATTERN.search(raw)
if cmd:
name = cmd.group(1).strip()
args_match = _CMD_ARGS_PATTERN.search(raw)
args = args_match.group(1).strip() if args_match else ""
composed = f"{name} {args}".strip() if args else name
if composed:
return composed
# 阶段二: 通用噪声剥离
cleaned = _NOISE_TAG_PATTERN.sub("", raw)
cleaned = _CMD_WRAPPER_PATTERN.sub("", cleaned)
cleaned = _PASTED_CONTENT_WRAPPER.sub("", cleaned)
cleaned = _IMAGE_PLACEHOLDER_PATTERN.sub("", cleaned)
cleaned = _WHITESPACE_PATTERN.sub(" ", cleaned).strip()
if cleaned:
return cleaned
# 阶段三: <session> 二次回退
# 当首轮全部剥离为空时,用户文本很可能被 <session> 标签完整包裹。
# 此时不去除 <session> 块,而是仅剥掉外壳标签,保留内部文本后重新剥离。
if "<session" in raw.lower():
inner = _SESSION_TAG_WRAPPER.sub("", raw)
cleaned = _NOISE_TAG_PATTERN.sub("", inner)
cleaned = _CMD_WRAPPER_PATTERN.sub("", cleaned)
cleaned = _WHITESPACE_PATTERN.sub(" ", cleaned).strip()
if cleaned:
return cleaned
return ""
# ── Session 标题提取: 多层级回退策略 ──────────────────────────────
#
# Level 1: user TEXT → 噪声剥离 → 首条非空文本 (原有逻辑)
# Level 2: user TOOL_RESULT → text 截取 → "[Tool output] <snippet>"
# Level 3: user IMAGE → 计数 → "[1 Image]" / "[N Images]"
# Level 4: 请求元数据 → tool_names → "[Tool call] Bash, Read"
# (模型名兜底已移除:合成模型名无业务语义)
# ─────────────────────────────────────────────────────────────────
# ── 注入式 Prompt(agent-as-tool 模板)载荷提取 ──────────────────
#
# Agent harness(记忆压缩器、会话命名器等)以子会话形式调用模型,首条 user
# 消息是固定话术模板(如 "You are a memory relevance compressor..."),
# 模板头无区分度,真实业务载荷内嵌在 "User query:" 等分隔符之后。
# 家族表按序匹配:(opener, 家族标签, 载荷分隔符, 尾部样板截断);
# 命中后剥模板头截载荷,产出 "[家族标签] 载荷" 形式标题。
_INJECTED_PROMPT_FAMILIES: tuple[
tuple[re.Pattern[str], str, re.Pattern[str], re.Pattern[str] | None, bool], ...
] = (
(
re.compile(r"(?i)^you are a memory relevance compressor\b"),
"[记忆压缩]",
re.compile(r"(?i)\bUser query\s*[::]\s*"),
re.compile(r"(?i)\bRetrieved\s+\S+\s+context fragments\b"),
False,
),
(
re.compile(r"(?i)^Name this chat\b"),
"[会话命名]",
re.compile(r"(?i)\bFirst request\s*[::]\s*"),
re.compile(r"(?i)\bThis is a naming-only task\b"),
False,
),
(
re.compile(r"(?i)^Generate a short git branch name\b"),
"[分支命名]",
re.compile(r"(?i)\bUser request\s*[::]\s*"),
None,
False,
),
(
re.compile(r"(?i)^Use the \S+ tool to run exactly this one command\b"),
"[单命令]",
re.compile(r"(?i)(?:\band nothing else|The command is)\s*[::]\s*"),
None,
False,
),
# 通用兜底:其余 "You are ..." 注入式模板。分隔符要求句界锚定(防止真实
# persona 消息中句中的 "task:"/"query:" 误截断);无分隔符时按真实输入
# 处理(fallback_real_input=True),避免误伤手打 "You are ..." 的用户消息。
(
re.compile(r"(?i)^you are\b"),
"[注入任务]",
re.compile(
r"(?i)(?:^|(?<=[.。!?\n]))\s*(?:User query|User request|First request)\s*[::]\s*"
),
re.compile(r"(?i)\bRetrieved\s+\S+\s+context fragments\b"),
True,
),
)
# task-notification 载荷凝缩:注入载荷常为 harness 的任务通知 XML 块,
# 其中的 <summary> 行(如 Agent "xx" finished)才是可扫读标识;
# 无 summary 时整块剥离后取残余文本。
_TASK_SUMMARY_PATTERN = re.compile(r"<summary>\s*(.*?)\s*</summary>", re.DOTALL)
_TASK_NOTIFICATION_BLOCK = re.compile(
r"<task-notification\b[^>]*>.*?</task-notification>", re.DOTALL | re.IGNORECASE
)
# 未闭合的 <task-notification ...> 开标签兜底(块正则不生效时剥开头标签)。
_NOTIFICATION_OPEN_TAG = re.compile(r"<task-notification\b[^>]*>", re.IGNORECASE)
# 已知元数据标签:成对剥除时连同内文一起移除(id/路径/状态无扫读价值),
# 避免剥壳留内文产生孤立 id 残段。
_NOTIFICATION_ID_PAIRS = re.compile(
r"<(?P<tag>task-id|tool-use-id|output-file|status|event|note)\b[^>]*>"
r".*?</(?P=tag)>",
re.DOTALL | re.IGNORECASE,
)
# 未成对的残余元数据标签(截断形态)与 summary 外壳:剥标签本身、保留 summary 内文。
_NOTIFICATION_ID_TAGS = re.compile(
r"</?(?:task-id|tool-use-id|output-file|status|event|note|summary)\b[^>]*/?>",
re.IGNORECASE,
)
# 实体转义形态的通知标记(<task-notification / >task-notification):
# 仅命中时才对载荷做全量实体解码(legacy 无分号实体随之解码,属可接受代价),
# 其余载荷只做下方安全五元组的定向解码,避免 ¬→¬ 类改写真实业务文本。
_ESCAPED_NOTIFICATION_MARK = re.compile(
r"&(?:lt|gt|quot);/?(?:task-notification)", re.IGNORECASE
)
_SAFE_ENTITIES_MAP = {
"<": "<",
">": ">",
"&": "&",
""": '"',
"'": "'",
}
_SAFE_ENTITY_PATTERN = re.compile(
"|".join(re.escape(k) for k in _SAFE_ENTITIES_MAP), re.IGNORECASE
)
def _condense_notification_payload(payload: str) -> str:
"""注入载荷为任务通知 XML 块时凝缩为可读文本,否则原样返回.
``<summary>`` 提取限定在 task-notification 块内(载荷正文自身出现的
``<summary>`` 字面量不触发凝缩);块未闭合时退化为剥标签取残余文本。
"""
if "<task-notification" not in payload.lower():
return payload
block = _TASK_NOTIFICATION_BLOCK.search(payload)
if block:
m = _TASK_SUMMARY_PATTERN.search(block.group(0))
if m and m.group(1).strip():
return m.group(1).strip()
# 未闭合/无 summary 兜底:成对元数据标签连同内文剥除,summary 剥壳保内文,
# 使残余文本与闭合块路径口径一致(无孤立 id 残段、无残留标签)。
stripped = _TASK_NOTIFICATION_BLOCK.sub(" ", payload)
stripped = _NOTIFICATION_OPEN_TAG.sub(" ", stripped)
stripped = _NOTIFICATION_ID_PAIRS.sub(" ", stripped)
stripped = _NOTIFICATION_ID_TAGS.sub(" ", stripped)
return _WHITESPACE_PATTERN.sub(" ", stripped).strip()
def _extract_injected_prompt_title(cleaned: str) -> str | None:
"""注入式 Prompt 载荷标题提取.
Returns:
``None`` — 非注入式消息(调用方按真实用户输入处理);
``""`` — 命中注入家族但无载荷(模板全同质、无区分度,调用方应跳过该条);
``str`` — ``[家族标签] 载荷`` 形式的标题。
"""
for opener, tag, delim, tail, fallback_real_input in _INJECTED_PROMPT_FAMILIES:
if not opener.search(cleaned):
continue
m = delim.search(cleaned)
if not m:
# 具名家族模板高度特异,无载荷即整条跳过;通用家族 opener 宽松,
# 无载荷时按真实输入处理以免误伤手打 "You are ..." 的用户消息。
return None if fallback_real_input else ""
payload = cleaned[m.end() :]
if tail:
t = tail.search(payload)
if t:
payload = payload[: t.start()]
# 实体解码分两档:转义形态的通知 XML 全量解码(先于凝缩守卫,转义形态
# 需还原为字面标签才能被识别);其余载荷仅定向解码带分号的安全实体
# (</>/&/"/'),避免 html.unescape 的 HTML5 legacy
# 无分号规则(¬→¬ 等)改写真实业务文本。
has_escaped_notification = bool(_ESCAPED_NOTIFICATION_MARK.search(payload))
if has_escaped_notification:
payload = html.unescape(payload)
payload = _condense_notification_payload(payload)
if not has_escaped_notification:
payload = _SAFE_ENTITY_PATTERN.sub(
lambda m: _SAFE_ENTITIES_MAP[m.group(0).lower()], payload
)
payload = _WHITESPACE_PATTERN.sub(" ", payload).strip()
if not payload:
return ""
return f"{tag} {payload}"[:_INJECTED_PAYLOAD_TITLE_MAX_LEN]
return None
# ── CC 会话标题探测消息(session-title probe)载荷提取 ─────────
#
# Claude Code 在会话早期发送内部标题探测请求(haiku,querySource=
# generate_session_title),user 文本为固定指令模板,形态取自 CC 2.1.325
# 取证:可选未闭合 <session> 壳 + "Write the title in ..." 指令句;
# legacy 变体为裸 <session> 壳直接包裹真实输入。指令内嵌首条用户消息
# 原文时剥离指令取载荷;纯指令无载荷时跳过该条,待延迟补写回填。
# 此前靠 title_exempt_prefixes 配置豁免(且 <session> 前缀形态漏匹配,
# 历史库残留 256 条垃圾标题),现收敛为代码内建识别,配置可清零。
_PROBE_SESSION_TAG_PREFIX = re.compile(r"^\s*<session\b[^>]*>\s*", re.IGNORECASE)
_PROBE_INSTRUCTION_OPEN = re.compile(r"^\s*Write the title in\b", re.IGNORECASE)
# 指令块句尾锚定(当前两代模板 + legacy 逗号变体);未命中的未知变体
# 整条跳过(返回 ""),宁可延迟补写也不产出垃圾标题。
_PROBE_INSTRUCTION_BLOCK = re.compile(
r"Write the title in\b.*?"
r"(?:in their original form\.|the English of these instructions\.|wrote in,)\s*",
re.DOTALL | re.IGNORECASE,
)
def _extract_session_probe_payload(cleaned: str) -> str | None:
"""CC 标题探测消息 → 真实业务载荷.
Returns:
``None`` — 非探测消息(调用方按真实用户输入/注入家族继续处理);
``""`` — 探测指令无载荷(调用方跳过该条,靠延迟补写回填);
``str`` — 剥离壳与指令后的真实载荷(首条用户消息原文)。
"""
tag = _PROBE_SESSION_TAG_PREFIX.match(cleaned)
text = cleaned[tag.end() :] if tag else cleaned
if not _PROBE_INSTRUCTION_OPEN.match(text):
# 裸 <session> 壳包裹真实输入(legacy 形态):标签后即载荷
if tag:
return text.strip()
return None
stripped = _PROBE_INSTRUCTION_BLOCK.sub("", text, count=1)
if stripped == text:
# 指令块句尾锚定未命中(未知模板变体):整条按无载荷跳过
return ""
return stripped.strip()
def _derive_title_from_user_texts(
texts, exempt_prefixes: list[str] | None = None
) -> str:
"""从一批用户原始文本推导标题 — 供请求路径与历史回填共用.
逐条清洗 → 豁免前缀跳过 → 注入式载荷提取 → 首条真实输入。
豁免前缀同时作用于清洗文本与派生的注入式标题(豁免家族标签时
跳过该条继续查找,而非丢弃整个 Level 1)。
全部跳过时返回空串(调用方按无候选处理)。
"""
prefixes = [p for p in (exempt_prefixes or []) if p]
def _exempt(candidate: str) -> bool:
if any(candidate.startswith(p) for p in prefixes):
logger.debug(
"Title candidate skipped by exempt prefix (snippet=%r)",
candidate[:60],
)
return True
return False
for raw in texts:
cleaned = _sanitize_user_text(raw)
if not cleaned:
continue
# 整条消息即 harness 固定话术:背景任务通知 / 会话续接摘要,无区分度。
if cleaned.startswith("<task-notification") or cleaned.startswith(
"This session is being continued"
):
continue
if _exempt(cleaned):
continue
probe = _extract_session_probe_payload(cleaned)
if probe is not None:
# 探测家族命中:载荷即真实首条输入,直接成题;
# 无载荷/未知变体跳过该条,靠延迟补写回填。
if probe and not _exempt(probe):
return probe[:_SESSION_TITLE_MAX_LEN]
continue
injected = _extract_injected_prompt_title(cleaned)
if injected is None:
return cleaned[:_SESSION_TITLE_MAX_LEN]
if injected and not _exempt(injected):
return injected
return ""
def _extract_title_from_user_text(
messages: list[CanonicalMessagePart],
exempt_prefixes: list[str] | None = None,
) -> str:
"""Level 1: 从 user TEXT 部分提取经噪声剥离后的首条非空文本.
``exempt_prefixes`` 非空时,清洗后文本以任一前缀开头则跳过该输入、继续向后
查找——用于过滤注入式 Prompt,避免其被误用作 Session 标题。匹配作用于
``_sanitize_user_text`` 清洗后的纯文本(而非带 XML 标签的原始 raw)。
注入式模板消息(agent-as-tool 话术)经 ``_extract_injected_prompt_title``
剥离模板头、截取业务载荷;无载荷则跳过该条继续查找。
"""
texts = [
part.text
for part in messages
if part.role == "user" and part.type == CanonicalPartType.TEXT
]
return _derive_title_from_user_texts(texts, exempt_prefixes=exempt_prefixes)
def _extract_title_from_tool_results(messages: list[CanonicalMessagePart]) -> str:
"""Level 2: 从 user TOOL_RESULT 部分截取文本摘要."""
for part in messages:
if part.role != "user" or part.type != CanonicalPartType.TOOL_RESULT:
continue
if not part.text:
continue
cleaned = _sanitize_user_text(part.text)
if cleaned:
snippet = cleaned[:_FALLBACK_TITLE_MAX_LEN]
return f"[Tool output] {snippet}"
return ""
def _extract_title_from_images(messages: list[CanonicalMessagePart]) -> str:
"""Level 3: 统计 user IMAGE 部分数量,生成图片描述标题."""
count = sum(
1 for p in messages if p.role == "user" and p.type == CanonicalPartType.IMAGE
)
if count == 0:
return ""
return f"[{count} Image{'s' if count > 1 else ''}]"
def _extract_title_from_metadata(request: CanonicalRequest) -> str:
"""Level 4: 从请求元数据 (tool_names) 合成兜底标题.
模型名兜底(``[Session] <model>``)已移除:合成模型名无业务语义,
与「标题总具备实际识别意义的业务 message」目标冲突;无 user 输入的
会话保持空标题,待延迟补写经 upsert 回填真实输入。
"""
if request.tool_names:
names = ", ".join(request.tool_names[:3])
return f"[Tool call] {names}"
return ""
def _extract_session_title(
request: CanonicalRequest,
exempt_prefixes: list[str] | None = None,
) -> str:
"""从规范化请求中提取 session 标题 — 多层级回退策略。
依次尝试: user TEXT 噪声剥离 → TOOL_RESULT 摘要 → IMAGE 计数 → 元数据兜底。
任一层级产出候选且未被豁免即返回,确保 Dashboard 尽可能展示有辨识度的标题。
``exempt_prefixes`` 对所有层级统一生效:任一层产出的候选标题若以任一前缀
开头,则视为豁免、继续向下一层回退。Level 1 内部仍保留「逐条消息跳过」
语义(清洗后文本以豁免前缀开头则跳过该条、继续查找下一条 user TEXT);
本函数在编排层对每层最终候选再做一次 ``_is_exempt`` 拦截,使 Level 2/3/4
的合成标题(如 ``[Tool output]``、``[N Image(s)]``、``[Tool call]``)
也能被用户显式豁免。全部层级均被豁免时返回空串,调用方
``if title:`` 据此跳过写库,session 标题保持空,待后续请求经
``set_session_title`` 幂等 upsert 回填有意义标题。默认 ``None`` 时行为与历史一致。
"""
# 防御性过滤空串前缀:"" startswith 恒真会误豁免一切。入参虽经配置层与
# 构造器两层归一化,模块级直接调用(如测试)仍可能传入 [""],此处兜底。
prefixes = [p for p in (exempt_prefixes or []) if p]
def _is_exempt(candidate: str) -> bool:
"""候选标题是否命中任一豁免前缀(大小写敏感 startswith)。"""
return bool(prefixes) and any(candidate.startswith(p) for p in prefixes)
messages = request.messages
# Level 1: 内部已对每条 user TEXT 做豁免跳过;外层 _is_exempt 对其最终候选
# 再做一次拦截(正常路径下冗余,作为 _sanitize_user_text 一致性兜底)。
title = _extract_title_from_user_text(messages, exempt_prefixes=prefixes)
if title and not _is_exempt(title):
return title[:_SESSION_TITLE_MAX_LEN]
# Level 2 / Level 3: 候选为合成标题,统一经 _is_exempt 拦截后回退。
for extractor in (
_extract_title_from_tool_results,
_extract_title_from_images,
):
title = extractor(messages)
if title and not _is_exempt(title):
return title[:_SESSION_TITLE_MAX_LEN]
# Level 4: 签名不同(依赖 request 元数据)。被豁免则落到末尾返回空串,
# 使调用方 if title: 跳过写库、保留空标题待后续回填。
title = _extract_title_from_metadata(request)
if title and not _is_exempt(title):
return title[:_SESSION_TITLE_MAX_LEN]
return ""
def _build_semantic_rejection_diagnostic(body: dict[str, Any]) -> str:
"""构建语义拒绝的请求体诊断上下文.
在 semantic rejection 日志中附加请求体的可疑参数快照,
用于定位供应商参数校验失败的具体祸根参数。
覆盖范围:
* 模型 / messages 数(baseline)
* thinking 系列顶层参数 + history thinking_blocks 数
* system 形态(string / blocks,含 cache_control 计数)
* tools 数量 + tool_choice 形态
* 采样参数(max_tokens / temperature / top_p / top_k / stop_sequences)
* stream / metadata 形态
* cache_control 存在性
* messages.content 类型分布
* 请求体大小估算(json.dumps 字节数)
"""
parts: list[str] = []
# ── 模型 + 消息数(baseline,始终输出)──
parts.append(f"model={body.get('model', 'N/A')}")
parts.append(f"messages={len(body.get('messages', []))}")
# ── 顶层 thinking 系列参数 ──
for key in ("thinking", "extended_thinking", "reasoning_effort"):
if key in body:
val = body[key]
parts.append(f"{key}={val!r:.80}")
# ── system 形态 ──
system = body.get("system")
if isinstance(system, str):
parts.append(f"system_kind=string(len={len(system)})")
elif isinstance(system, list):
cc_count = sum(
1 for item in system if isinstance(item, dict) and "cache_control" in item
)
if cc_count:
parts.append(f"system_blocks={len(system)},cc={cc_count}")
else:
parts.append(f"system_blocks={len(system)}")
# ── tools 与 tool_choice ──
tools = body.get("tools")
if isinstance(tools, list):
parts.append(f"tools={len(tools)}")
tool_choice = body.get("tool_choice")
if tool_choice is not None:
parts.append(f"tool_choice={tool_choice!r:.60}")
# ── 采样参数(仅存在时输出)──
for key in ("max_tokens", "temperature", "top_p", "top_k"):
if key in body:
parts.append(f"{key}={body[key]!r:.40}")
stop_sequences = body.get("stop_sequences")
if isinstance(stop_sequences, list) and stop_sequences:
parts.append(f"stop_sequences={len(stop_sequences)}")
# ── stream / metadata ──
if "stream" in body:
parts.append(f"stream={body['stream']}")
metadata = body.get("metadata")
if isinstance(metadata, dict) and metadata:
parts.append(f"metadata_keys={len(metadata)}")
# ── 会话历史中的 thinking blocks 与 content_types 分布 ──
thinking_count = 0
content_type_counts: dict[str, int] = {}
for msg in body.get("messages", []):
content = msg.get("content")
if isinstance(content, str):
content_type_counts["string"] = content_type_counts.get("string", 0) + 1
continue
if not isinstance(content, list):
continue
for block in content:
if not isinstance(block, dict):
continue
btype = block.get("type")
if isinstance(btype, str):
content_type_counts[btype] = content_type_counts.get(btype, 0) + 1
if btype in ("thinking", "redacted_thinking"):
thinking_count += 1
if thinking_count:
parts.append(f"thinking_blocks_in_history={thinking_count}")
if content_type_counts:
type_repr = ",".join(f"{k}:{v}" for k, v in sorted(content_type_counts.items()))
parts.append(f"content_types={{{type_repr}}}")
# ── cache_control 存在检测(messages / tools,不含 system 因已单独统计)──
has_cc = False
sections: list[Any] = []
for m in body.get("messages", []):
if isinstance(m.get("content"), list):
sections.append(m["content"])
if isinstance(body.get("tools"), list):
sections.append(body["tools"])
for section in sections:
for item in section:
if isinstance(item, dict) and "cache_control" in item:
has_cc = True
break
if has_cc:
break
if has_cc:
parts.append("cache_control_fields=present")
# ── 请求体大小估算 ──
try:
body_bytes = len(json.dumps(body, ensure_ascii=False).encode("utf-8"))
parts.append(f"body_bytes={body_bytes}")
except (TypeError, ValueError):
# 极少数情况下 body 含非可序列化对象,跳过
pass
return f" [{', '.join(parts)}]" if parts else ""
def _build_semantic_rejection_diagnostic(body: dict[str, Any]) -> str:
"""构建语义拒绝的请求体诊断上下文.
在 semantic rejection 日志中附加请求体的可疑参数快照,
用于定位供应商参数校验失败的具体祸根参数。
覆盖范围:
* 模型 / messages 数(baseline)
* thinking 系列顶层参数 + history thinking_blocks 数
* system 形态(string / blocks,含 cache_control 计数)
* tools 数量 + tool_choice 形态
* 采样参数(max_tokens / temperature / top_p / top_k / stop_sequences)
* stream / metadata 形态
* cache_control 存在性
* messages.content 类型分布
* 请求体大小估算(json.dumps 字节数)
"""
parts: list[str] = []
# ── 模型 + 消息数(baseline,始终输出)──
parts.append(f"model={body.get('model', 'N/A')}")
parts.append(f"messages={len(body.get('messages', []))}")
# ── 顶层 thinking 系列参数 ──
for key in ("thinking", "extended_thinking", "reasoning_effort"):
if key in body:
val = body[key]
parts.append(f"{key}={val!r:.80}")
# ── system 形态 ──
system = body.get("system")
if isinstance(system, str):
parts.append(f"system_kind=string(len={len(system)})")
elif isinstance(system, list):
cc_count = sum(
1 for item in system if isinstance(item, dict) and "cache_control" in item
)
if cc_count:
parts.append(f"system_blocks={len(system)},cc={cc_count}")
else:
parts.append(f"system_blocks={len(system)}")
# ── tools 与 tool_choice ──
tools = body.get("tools")
if isinstance(tools, list):
parts.append(f"tools={len(tools)}")
tool_choice = body.get("tool_choice")
if tool_choice is not None:
parts.append(f"tool_choice={tool_choice!r:.60}")
# ── 采样参数(仅存在时输出)──
for key in ("max_tokens", "temperature", "top_p", "top_k"):
if key in body:
parts.append(f"{key}={body[key]!r:.40}")
stop_sequences = body.get("stop_sequences")
if isinstance(stop_sequences, list) and stop_sequences:
parts.append(f"stop_sequences={len(stop_sequences)}")
# ── stream / metadata ──
if "stream" in body:
parts.append(f"stream={body['stream']}")
metadata = body.get("metadata")
if isinstance(metadata, dict) and metadata:
parts.append(f"metadata_keys={len(metadata)}")
# ── 会话历史中的 thinking blocks 与 content_types 分布 ──
thinking_count = 0
content_type_counts: dict[str, int] = {}
for msg in body.get("messages", []):
content = msg.get("content")
if isinstance(content, str):
content_type_counts["string"] = content_type_counts.get("string", 0) + 1
continue
if not isinstance(content, list):
continue
for block in content:
if not isinstance(block, dict):
continue
btype = block.get("type")
if isinstance(btype, str):
content_type_counts[btype] = content_type_counts.get(btype, 0) + 1
if btype in ("thinking", "redacted_thinking"):
thinking_count += 1
if thinking_count:
parts.append(f"thinking_blocks_in_history={thinking_count}")
if content_type_counts:
type_repr = ",".join(f"{k}:{v}" for k, v in sorted(content_type_counts.items()))
parts.append(f"content_types={{{type_repr}}}")
# ── cache_control 存在检测(messages / tools,不含 system 因已单独统计)──
has_cc = False
sections: list[Any] = []
for m in body.get("messages", []):
if isinstance(m.get("content"), list):
sections.append(m["content"])
if isinstance(body.get("tools"), list):
sections.append(body["tools"])
for section in sections:
for item in section:
if isinstance(item, dict) and "cache_control" in item:
has_cc = True
break
if has_cc:
break
if has_cc:
parts.append("cache_control_fields=present")
# ── 请求体大小估算 ──
try:
body_bytes = len(json.dumps(body, ensure_ascii=False).encode("utf-8"))
parts.append(f"body_bytes={body_bytes}")
except (TypeError, ValueError):
# 极少数情况下 body 含非可序列化对象,跳过
pass
return f" [{', '.join(parts)}]" if parts else ""
def _log_http_error_detail(
tier_name: str,
exc: Exception,
*,
is_stream: bool = False,
tier: VendorTier | None = None,
) -> None:
"""记录 HTTP 错误的详细信息(状态码 / 响应体摘要 / 异常类型 / 熔断器快照).
替代原先单行 ``logger.warning("Tier %s stream failed: %s", ...)``,
在非 200 响应时输出更丰富的诊断上下文,便于跟踪上游故障根因。
"""
detail_parts = [f"Tier {tier_name} {'stream' if is_stream else 'message'} failed:"]
detail_parts.append(f" exc_type={type(exc).__name__}")
if isinstance(exc, httpx.HTTPStatusError) and exc.response is not None:
resp = exc.response
detail_parts.append(f" status={resp.status_code}")
body_preview = (
(resp.text[:300] if resp.text else "(empty)")
if resp.content
else "(no content)"
)
detail_parts.append(f" response_body={body_preview}")
# 尝试提取 error type / message
try:
payload = resp.json() if resp.content else None
except Exception:
payload = None
if isinstance(payload, dict):
err = payload.get("error", {})
if isinstance(err, dict):
detail_parts.append(f" error_type={err.get('type', 'N/A')}")
detail_parts.append(f" error_msg={err.get('message', 'N/A')[:200]}")
else:
detail_parts.append(f" message={str(exc)[:300]}")
# 熔断器状态快照
if tier and tier.circuit_breaker:
cb = tier.circuit_breaker
cb_info = cb.get_info()
detail_parts.append(
f" circuit_breaker={cb_info['state']} "
f"(failures={cb_info['failure_count']}/{cb._failure_threshold})"
)
logger.warning("\n".join(detail_parts))
def _has_tool_results(body: dict[str, Any]) -> bool:
"""检测请求体是否包含 tool_result 内容块.
用于诊断日志中标记「当前请求是否处于工具执行循环」,
帮助快速定位 vendor 对 tool_result 处理不兼容的问题(如 Zhipu 500).
"""
for msg in body.get("messages", []):
content = msg.get("content")
if not isinstance(content, list):
continue
if any(isinstance(b, dict) and b.get("type") == "tool_result" for b in content):
return True
return False
def _is_likely_request_format_error(
status_code: int,
error_body_text: str | None,
body: dict[str, Any],
) -> bool:
"""判断 HTTP 错误是否可能由请求格式不兼容导致(而非供应商故障).
当请求包含 tool_result 且供应商返回 400 时,极大概率是消息格式转换
问题(如 tool_result 错位、字段缺失等),此类错误不应计入熔断器,
因为重试同一格式的请求必然再次失败。
此函数是 :func:`is_semantic_rejection` 的补充——后者依赖结构化 error body
(JSON),而部分供应商(如 Copilot)的 400 响应可能是纯文本 ``Bad Request``。
"""
if status_code != 400:
return False
if not _has_tool_results(body):
return False
# 400 + 有 tool_result + 无法解析为结构化错误 → 高概率格式问题
if error_body_text is not None:
trimmed = error_body_text.strip().lower()
# 纯文本 400 响应(Copilot 等)或无意义的错误体
if trimmed in ("bad request", "bad request\n", ""):
return True
# 非结构化响应体(非 JSON)
if not trimmed.startswith("{") and len(trimmed) < 200:
return True
# 结构化 JSON 400 但含 tool_call 格式错误码 → 格式不兼容
# (如 Copilot 返回 {"error":{"code":"invalid_tool_call_format",...}})
if "invalid_tool_call_format" in trimmed:
return True
return False
def _log_vendor_response_error(
tier_name: str,
resp: VendorResponse,
body: dict[str, Any],
*,
is_stream: bool = False,
) -> None:
"""记录供应商返回的非 200 VendorResponse 详细信息.
补充 :func:`_log_http_error_detail` 的覆盖盲区:
当 ``send_message()`` 返回 ``VendorResponse(status_code>=400)``
而非抛出 httpx 异常时,该函数提供等价的诊断日志能力。
典型场景:Zhipu 等薄透传供应商将上游 500 原样包装为
VendorResponse 返回,executor 的异常捕获路径不会触发。
"""
mode = "stream" if is_stream else "message"
detail_parts = [f"Tier {tier_name} {mode} vendor error response:"]
detail_parts.append(f" status={resp.status_code}")
detail_parts.append(f" error_type={resp.error_type or 'N/A'}")
detail_parts.append(f" error_msg={(resp.error_message or 'N/A')[:300]}")
# 请求上下文(模型 / 工具 / 工具结果)
model = body.get("model", "unknown")
has_tools = bool(body.get("tools"))
has_tool_results = _has_tool_results(body)
detail_parts.append(f" model={model}")
detail_parts.append(f" has_tools={has_tools}")
detail_parts.append(f" has_tool_results={has_tool_results}")
# 响应体摘要
if resp.raw_body:
try:
raw_text = resp.raw_body.decode("utf-8", errors="replace")[:500]
except (AttributeError, UnicodeDecodeError):
raw_text = f"(binary, {len(resp.raw_body)} bytes)"
detail_parts.append(f" response_body_preview={raw_text}")
logger.warning("\n".join(detail_parts))
# tier.name → 上游 Vendor 协议标签映射(用于 token 用量日志标注)
_VENDOR_PROTOCOL_LABEL_MAP: dict[str, str] = {
"anthropic": "Anthropic",
"zhipu": "Anthropic",
"minimax": "Anthropic",
"kimi": "Anthropic",
"doubao": "Anthropic",
"xiaomi": "Anthropic",
"alibaba": "Anthropic",
"copilot": "OpenAI",
"antigravity": "Gemini",
}
class _RouteExecutor:
"""统一的 tier 迭代门控引擎.
职责:
- 按优先级遍历 tiers,执行能力门控与健康检查
- 委托具体的流式/非流式执行给调用方回调
- 统一处理 TokenAcquireError / HTTP 错误 / 语义拒绝
- 成功后委托 UsageRecorder 记录用量
"""
def __init__(
self,
router: Any, # RequestRouter 引用,用于写入活跃供应商状态
tiers: list[VendorTier],
usage_recorder: UsageRecorder,
session_manager: RouteSessionManager,
reauth_coordinator: Any | None = None,
session_policy_resolver: SessionPolicyResolver | None = None,
title_vendor_bindings: list[TitleVendorBinding] | None = None,
title_exempt_prefixes: list[str] | None = None,
) -> None:
self._router = router
self._tiers = tiers
self._recorder = usage_recorder
self._session_mgr = session_manager
self._reauth_coordinator = reauth_coordinator
self._policy_resolver = session_policy_resolver or SessionPolicyResolver()
self._title_vendor_bindings = title_vendor_bindings or []
# 豁免前缀名单:构造期二次归一化(strip + 去空),双重防御配置层 field_validator。
# 即便绕过配置层直接构造 executor,也能避免空串 startswith 恒真与纯空白失配。
self._title_exempt_prefixes: list[str] = [
stripped
for p in (title_exempt_prefixes or [])
if isinstance(p, str) and (stripped := p.strip())
]
self._validate_title_vendor_bindings()
# Tier 名称 → OAuth provider 名称的映射
self._tier_provider_map: dict[str, str] = {
"copilot": "github",
"antigravity": "google",
}
def _validate_title_vendor_bindings(self) -> None:
"""启动期校验标题绑定引用的 vendor 均存在,缺失则告警.
与手动绑定 API(拒绝未知 vendor)的语义对齐:此处不硬失败,
仅记录警告——避免单条误配置阻断整个代理启动;运行时
`_resolve_effective_tiers` 会静默跳过未知 vendor 回退默认顺序。
"""
if not self._title_vendor_bindings:
return
valid = {t.name for t in self._tiers}
for binding in self._title_vendor_bindings:
if binding.vendor not in valid:
logger.warning(
"title_vendor_bindings 引用了未知 vendor %r(前缀 %r);"
"可用 vendor: %s。该绑定将在运行时被静默跳过。",
binding.vendor,
binding.prefix,
sorted(valid),
)
# ── 公开执行入口 ──────────────────────────────────────
def _pinned_tier_names(self, model: str | None) -> list[str] | None:
"""返回绑定模型的归属 tier 名单(合成目录中该条目 tiers 非空时).
内置目录条目(及 config 覆盖 tiers 的条目)声明了原生归属 vendor,
绑定后该 vendor 置于链头优先承接;tiers 为空(纯自定义透传条目)返回
None = 链头沿用全局顺序。链头之后始终接订阅状态顺序的其余 tier,