feat: 完善策略优选项目工作流

This commit is contained in:
Administrator committed 2026-09-09 20:49:59 +08:00
1 parent a26fd32eba
commit 5d6627af50
45 files changed
+7235 -768

No files matched your search

@@ -21,9 +21,10 @@ class InlineThinkTagFilter:
_OPEN_TAG = "<think>"
_CLOSE_TAG = "</think>"
def __init__(self) -> None:
def __init__(self, *, allow_implicit_close: bool = False) -> None:
self._inside_think = False
self._pending = ""
self._channel_decided = not allow_implicit_close
@staticmethod
def _possible_tag_prefix_size(content: str, tag: str) -> int:
@@ -44,6 +45,23 @@ class InlineThinkTagFilter:
visible: list[str] = []
thinking: list[str] = []
if not self._channel_decided:
lowered = remaining.lower()
open_at = lowered.find(self._OPEN_TAG)
close_at = lowered.find(self._CLOSE_TAG)
if close_at >= 0 and (open_at < 0 or close_at < open_at):
# Some templates inject the opening tag outside returned
# content. Keep the ambiguous prefix private until its close
# marker arrives, even when that marker spans chunks.
thinking.append(remaining[:close_at])
remaining = remaining[close_at + len(self._CLOSE_TAG) :]
self._channel_decided = True
elif open_at >= 0:
self._channel_decided = True
else:
self._pending = remaining
return "", ""
while remaining:
tag = self._CLOSE_TAG if self._inside_think else self._OPEN_TAG
marker_at = remaining.lower().find(tag)
@@ -65,8 +83,27 @@ class InlineThinkTagFilter:
"""Compatibility helper returning only confirmed visible text."""
return self.push_parts(chunk)[0]
def confirm_visible_channel(self) -> str:
"""Stop close-only probing and return any buffered visible prelude.
A provider that emits a dedicated reasoning field has already told us
how to classify its trace. Its ordinary ``content`` channel can then
stream immediately even when the model supports thinking.
"""
if self._channel_decided:
return ""
self._channel_decided = True
pending = self._pending
self._pending = ""
return pending
def finish_parts(self) -> tuple[str, str]:
"""Flush the remaining visible or thinking text at end of stream."""
if not self._channel_decided:
tail = self._pending
self._pending = ""
self._channel_decided = True
return tail, ""
if self._inside_think:
thinking = self._pending
self._pending = ""