Spaces:
Sleeping
Sleeping
File size: 21,877 Bytes
c1ae554 |
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 |
from __future__ import annotations
import json
import uuid
from typing import Any, AsyncGenerator, Dict
import httpx
from .logging import logger
from .config import BRIDGE_BASE_URL
from .helpers import _get
async def stream_openai_sse(packet: Dict[str, Any], completion_id: str, created_ts: int, model_id: str) -> AsyncGenerator[str, None]:
try:
first = {
"id": completion_id,
"object": "chat.completion.chunk",
"created": created_ts,
"model": model_id,
"choices": [{"index": 0, "delta": {"role": "assistant"}}],
}
# 打印转换后的首个 SSE 事件(OpenAI 格式)
try:
logger.info("[OpenAI Compat] 转换后的 SSE(emit): %s", json.dumps(first, ensure_ascii=False))
except Exception:
pass
yield f"data: {json.dumps(first, ensure_ascii=False)}\n\n"
timeout = httpx.Timeout(60.0)
async with httpx.AsyncClient(http2=True, timeout=timeout, trust_env=True) as client:
def _do_stream():
return client.stream(
"POST",
f"{BRIDGE_BASE_URL}/api/warp/send_stream_sse",
headers={"accept": "text/event-stream"},
json={"json_data": packet, "message_type": "warp.multi_agent.v1.Request"},
)
# 首次请求
response_cm = _do_stream()
async with response_cm as response:
if response.status_code == 429:
try:
r = await client.post(f"{BRIDGE_BASE_URL}/api/auth/refresh", timeout=10.0)
logger.warning("[OpenAI Compat] Bridge returned 429. Tried JWT refresh -> HTTP %s", r.status_code)
except Exception as _e:
logger.warning("[OpenAI Compat] JWT refresh attempt failed after 429: %s", _e)
# 重试一次
response_cm2 = _do_stream()
async with response_cm2 as response2:
response = response2
if response.status_code != 200:
error_text = await response.aread()
error_content = error_text.decode("utf-8") if error_text else ""
logger.error(f"[OpenAI Compat] Bridge HTTP error {response.status_code}: {error_content[:300]}")
raise RuntimeError(f"bridge error: {error_content}")
current = ""
tool_calls_emitted = False
async for line in response.aiter_lines():
if line.startswith("data:"):
payload = line[5:].strip()
if not payload:
continue
# 打印接收到的 Protobuf SSE 原始事件片段
try:
logger.info("[OpenAI Compat] 接收到的 Protobuf SSE(data): %s", payload)
except Exception:
pass
if payload == "[DONE]":
break
current += payload
continue
if (line.strip() == "") and current:
try:
ev = json.loads(current)
except Exception:
current = ""
continue
current = ""
event_data = (ev or {}).get("parsed_data") or {}
# 打印接收到的 Protobuf 事件(解析后)
try:
logger.info("[OpenAI Compat] 接收到的 Protobuf 事件(parsed): %s", json.dumps(event_data, ensure_ascii=False))
except Exception:
pass
if "init" in event_data:
pass
client_actions = _get(event_data, "client_actions", "clientActions")
if isinstance(client_actions, dict):
actions = _get(client_actions, "actions", "Actions") or []
for action in actions:
append_data = _get(action, "append_to_message_content", "appendToMessageContent")
if isinstance(append_data, dict):
message = append_data.get("message", {})
agent_output = _get(message, "agent_output", "agentOutput") or {}
text_content = agent_output.get("text", "")
if text_content:
delta = {
"id": completion_id,
"object": "chat.completion.chunk",
"created": created_ts,
"model": model_id,
"choices": [{"index": 0, "delta": {"content": text_content}}],
}
# 打印转换后的 OpenAI SSE 事件
try:
logger.info("[OpenAI Compat] 转换后的 SSE(emit): %s", json.dumps(delta, ensure_ascii=False))
except Exception:
pass
yield f"data: {json.dumps(delta, ensure_ascii=False)}\n\n"
messages_data = _get(action, "add_messages_to_task", "addMessagesToTask")
if isinstance(messages_data, dict):
messages = messages_data.get("messages", [])
for message in messages:
tool_call = _get(message, "tool_call", "toolCall") or {}
call_mcp = _get(tool_call, "call_mcp_tool", "callMcpTool") or {}
if isinstance(call_mcp, dict) and call_mcp.get("name"):
try:
args_obj = call_mcp.get("args", {}) or {}
args_str = json.dumps(args_obj, ensure_ascii=False)
except Exception:
args_str = "{}"
tool_call_id = tool_call.get("tool_call_id") or str(uuid.uuid4())
delta = {
"id": completion_id,
"object": "chat.completion.chunk",
"created": created_ts,
"model": model_id,
"choices": [{
"index": 0,
"delta": {
"tool_calls": [{
"index": 0,
"id": tool_call_id,
"type": "function",
"function": {"name": call_mcp.get("name"), "arguments": args_str},
}]
}
}],
}
# 打印转换后的 OpenAI 工具调用事件
try:
logger.info("[OpenAI Compat] 转换后的 SSE(emit tool_calls): %s", json.dumps(delta, ensure_ascii=False))
except Exception:
pass
yield f"data: {json.dumps(delta, ensure_ascii=False)}\n\n"
tool_calls_emitted = True
else:
agent_output = _get(message, "agent_output", "agentOutput") or {}
text_content = agent_output.get("text", "")
if text_content:
delta = {
"id": completion_id,
"object": "chat.completion.chunk",
"created": created_ts,
"model": model_id,
"choices": [{"index": 0, "delta": {"content": text_content}}],
}
try:
logger.info("[OpenAI Compat] 转换后的 SSE(emit): %s", json.dumps(delta, ensure_ascii=False))
except Exception:
pass
yield f"data: {json.dumps(delta, ensure_ascii=False)}\n\n"
if "finished" in event_data:
done_chunk = {
"id": completion_id,
"object": "chat.completion.chunk",
"created": created_ts,
"model": model_id,
"choices": [{"index": 0, "delta": {}, "finish_reason": ("tool_calls" if tool_calls_emitted else "stop")}],
}
try:
logger.info("[OpenAI Compat] 转换后的 SSE(emit done): %s", json.dumps(done_chunk, ensure_ascii=False))
except Exception:
pass
yield f"data: {json.dumps(done_chunk, ensure_ascii=False)}\n\n"
# 打印完成标记
try:
logger.info("[OpenAI Compat] 转换后的 SSE(emit): [DONE]")
except Exception:
pass
yield "data: [DONE]\n\n"
return
if response.status_code != 200:
error_text = await response.aread()
error_content = error_text.decode("utf-8") if error_text else ""
logger.error(f"[OpenAI Compat] Bridge HTTP error {response.status_code}: {error_content[:300]}")
raise RuntimeError(f"bridge error: {error_content}")
current = ""
tool_calls_emitted = False
async for line in response.aiter_lines():
if line.startswith("data:"):
payload = line[5:].strip()
if not payload:
continue
# 打印接收到的 Protobuf SSE 原始事件片段
try:
logger.info("[OpenAI Compat] 接收到的 Protobuf SSE(data): %s", payload)
except Exception:
pass
if payload == "[DONE]":
break
current += payload
continue
if (line.strip() == "") and current:
try:
ev = json.loads(current)
except Exception:
current = ""
continue
current = ""
event_data = (ev or {}).get("parsed_data") or {}
# 打印接收到的 Protobuf 事件(解析后)
try:
logger.info("[OpenAI Compat] 接收到的 Protobuf 事件(parsed): %s", json.dumps(event_data, ensure_ascii=False))
except Exception:
pass
if "init" in event_data:
pass
client_actions = _get(event_data, "client_actions", "clientActions")
if isinstance(client_actions, dict):
actions = _get(client_actions, "actions", "Actions") or []
for action in actions:
append_data = _get(action, "append_to_message_content", "appendToMessageContent")
if isinstance(append_data, dict):
message = append_data.get("message", {})
agent_output = _get(message, "agent_output", "agentOutput") or {}
text_content = agent_output.get("text", "")
if text_content:
delta = {
"id": completion_id,
"object": "chat.completion.chunk",
"created": created_ts,
"model": model_id,
"choices": [{"index": 0, "delta": {"content": text_content}}],
}
# 打印转换后的 OpenAI SSE 事件
try:
logger.info("[OpenAI Compat] 转换后的 SSE(emit): %s", json.dumps(delta, ensure_ascii=False))
except Exception:
pass
yield f"data: {json.dumps(delta, ensure_ascii=False)}\n\n"
messages_data = _get(action, "add_messages_to_task", "addMessagesToTask")
if isinstance(messages_data, dict):
messages = messages_data.get("messages", [])
for message in messages:
tool_call = _get(message, "tool_call", "toolCall") or {}
call_mcp = _get(tool_call, "call_mcp_tool", "callMcpTool") or {}
if isinstance(call_mcp, dict) and call_mcp.get("name"):
try:
args_obj = call_mcp.get("args", {}) or {}
args_str = json.dumps(args_obj, ensure_ascii=False)
except Exception:
args_str = "{}"
tool_call_id = tool_call.get("tool_call_id") or str(uuid.uuid4())
delta = {
"id": completion_id,
"object": "chat.completion.chunk",
"created": created_ts,
"model": model_id,
"choices": [{
"index": 0,
"delta": {
"tool_calls": [{
"index": 0,
"id": tool_call_id,
"type": "function",
"function": {"name": call_mcp.get("name"), "arguments": args_str},
}]
}
}],
}
# 打印转换后的 OpenAI 工具调用事件
try:
logger.info("[OpenAI Compat] 转换后的 SSE(emit tool_calls): %s", json.dumps(delta, ensure_ascii=False))
except Exception:
pass
yield f"data: {json.dumps(delta, ensure_ascii=False)}\n\n"
tool_calls_emitted = True
else:
agent_output = _get(message, "agent_output", "agentOutput") or {}
text_content = agent_output.get("text", "")
if text_content:
delta = {
"id": completion_id,
"object": "chat.completion.chunk",
"created": created_ts,
"model": model_id,
"choices": [{"index": 0, "delta": {"content": text_content}}],
}
try:
logger.info("[OpenAI Compat] 转换后的 SSE(emit): %s", json.dumps(delta, ensure_ascii=False))
except Exception:
pass
yield f"data: {json.dumps(delta, ensure_ascii=False)}\n\n"
if "finished" in event_data:
done_chunk = {
"id": completion_id,
"object": "chat.completion.chunk",
"created": created_ts,
"model": model_id,
"choices": [{"index": 0, "delta": {}, "finish_reason": ("tool_calls" if tool_calls_emitted else "stop")}],
}
try:
logger.info("[OpenAI Compat] 转换后的 SSE(emit done): %s", json.dumps(done_chunk, ensure_ascii=False))
except Exception:
pass
yield f"data: {json.dumps(done_chunk, ensure_ascii=False)}\n\n"
# 打印完成标记
try:
logger.info("[OpenAI Compat] 转换后的 SSE(emit): [DONE]")
except Exception:
pass
yield "data: [DONE]\n\n"
except Exception as e:
logger.error(f"[OpenAI Compat] Stream processing failed: {e}")
error_chunk = {
"id": completion_id,
"object": "chat.completion.chunk",
"created": created_ts,
"model": model_id,
"choices": [{"index": 0, "delta": {}, "finish_reason": "error"}],
"error": {"message": str(e)},
}
try:
logger.info("[OpenAI Compat] 转换后的 SSE(emit error): %s", json.dumps(error_chunk, ensure_ascii=False))
except Exception:
pass
yield f"data: {json.dumps(error_chunk, ensure_ascii=False)}\n\n"
yield "data: [DONE]\n\n" |