Enforce session workspace boundaries
This commit is contained in:
+13
-1
@@ -48,6 +48,7 @@ from src.token_budget import calculate_token_budget
|
||||
|
||||
|
||||
STATIC_DIR = Path(__file__).resolve().parents[2] / 'frontend' / 'legacy-static'
|
||||
API_TOOL_CONTENT_MAX_CHARS = 20000
|
||||
|
||||
VALIDATED_CHAT_MODEL_PROVIDERS = {
|
||||
# 这些 provider 已验证可以在当前 WebUI 中使用。
|
||||
@@ -1293,9 +1294,12 @@ def _serialize_run_result(result: Any) -> dict[str, Any]:
|
||||
|
||||
|
||||
def _normalize_transcript_entry(entry: dict[str, Any]) -> dict[str, Any]:
|
||||
content = entry.get('content', '')
|
||||
if entry.get('role') == 'tool' and isinstance(content, str):
|
||||
content = _truncate_api_text(content, API_TOOL_CONTENT_MAX_CHARS)
|
||||
out: dict[str, Any] = {
|
||||
'role': entry.get('role', ''),
|
||||
'content': entry.get('content', ''),
|
||||
'content': content,
|
||||
}
|
||||
for key in ('name', 'tool_call_id', 'tool_calls', 'metadata', 'message_id'):
|
||||
if key in entry and entry[key] not in (None, '', [], {}):
|
||||
@@ -1306,6 +1310,14 @@ def _normalize_transcript_entry(entry: dict[str, Any]) -> dict[str, Any]:
|
||||
return out
|
||||
|
||||
|
||||
def _truncate_api_text(text: str, limit: int) -> str:
|
||||
if len(text) <= limit:
|
||||
return text
|
||||
head = text[: limit // 2]
|
||||
tail = text[-(limit // 2) :]
|
||||
return f'{head}\n...[truncated for api response]...\n{tail}'
|
||||
|
||||
|
||||
def _serialize_stored_session(stored: StoredAgentSession) -> dict[str, Any]:
|
||||
return {
|
||||
'session_id': stored.session_id,
|
||||
|
||||
Reference in New Issue
Block a user