Repository navigation
Expand file tree
/
Copy pathresponses_adapter.py
More file actions
475 lines (402 loc) · 17.4 KB
/
Copy pathresponses_adapter.py
File metadata and controls
475 lines (402 loc) · 17.4 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
"""
responses_adapter.py — OpenAI Responses API ↔ Chat Completions API 适配层。
Codex CLI 使用 Responses API(POST /v1/responses),而 CodeBuddy 后端只支持
Chat Completions 协议。本模块做双向转换:
请求:Responses input/instructions/tools → Chat messages/tools
响应:Chat SSE delta → Responses 语义事件流(response.created / output_text.delta / …)
事件类型参考:https://developers.openai.com/api/docs/guides/streaming-responses
"""
from __future__ import annotations
import json
import os
import time
from typing import Any
# ---------------------------------------------------------------------------
# ID 生成
# ---------------------------------------------------------------------------
def _rand_id(prefix: str = "resp_") -> str:
return prefix + os.urandom(12).hex()
# ---------------------------------------------------------------------------
# 请求转换:Responses → Chat
# ---------------------------------------------------------------------------
def responses_request_to_chat(body: dict) -> dict:
"""将 Responses API 请求体转换为 Chat Completions 请求体。
关键映射:
input → messages
instructions → system message(置顶)
max_output_tokens → max_tokens
tools 格式微调(Responses 用 name,Chat 用 function.name)
"""
messages: list[dict] = []
# instructions → system message
instructions = body.get("instructions")
if instructions:
messages.append({"role": "system", "content": instructions})
# input → messages
inp = body.get("input", [])
if isinstance(inp, str):
messages.append({"role": "user", "content": inp})
elif isinstance(inp, list):
messages.extend(_convert_input_items(inp))
# 构造 Chat body
chat: dict[str, Any] = {"messages": messages, "stream": True}
# model
if "model" in body:
chat["model"] = body["model"]
# tools — Responses 和 Chat 的 function tool 格式略有不同
tools = body.get("tools")
if tools:
chat["tools"] = _convert_tools_for_chat(tools)
if "tool_choice" in body:
chat["tool_choice"] = body["tool_choice"]
# 透传常见参数
for key in ("temperature", "top_p", "stop", "seed",
"presence_penalty", "frequency_penalty",
"response_format", "reasoning_effort"):
if key in body:
chat[key] = body[key]
# max_output_tokens → max_tokens
if "max_output_tokens" in body:
chat["max_tokens"] = body["max_output_tokens"]
elif "max_tokens" in body:
chat["max_tokens"] = body["max_tokens"]
return chat
def _convert_input_items(items: list) -> list[dict]:
"""将 Responses API 的 input 数组转换为 Chat messages。
input 里可能包含:
- {"role": "user/developer", "content": ...} → 直接映射
- {"type": "message", ...} → 助手消息
- {"type": "function_call", ...} → 需合并到前面的助手消息
- {"type": "function_call_output", ...} → tool 角色
"""
messages: list[dict] = []
# 临时缓存:合并相邻的 assistant message 和 function_call
pending_assistant_content: str | None = None
pending_tool_calls: list[dict] = []
def _flush_assistant():
nonlocal pending_assistant_content, pending_tool_calls
if pending_assistant_content is not None or pending_tool_calls:
msg: dict[str, Any] = {"role": "assistant",
"content": pending_assistant_content or ""}
if pending_tool_calls:
msg["tool_calls"] = pending_tool_calls[:]
messages.append(msg)
pending_assistant_content = None
pending_tool_calls.clear()
for item in items:
if not isinstance(item, dict):
continue
item_type = item.get("type")
role = item.get("role", "")
# 简单消息 {"role": "user", "content": "..."}
if item_type is None and role in ("user", "system", "developer"):
_flush_assistant()
mapped_role = "system" if role == "developer" else role
content = _extract_content(item.get("content", ""))
messages.append({"role": mapped_role, "content": content})
continue
# typed message(Responses 里常见)
if item_type == "message" and role in ("user", "system", "developer"):
_flush_assistant()
mapped_role = "system" if role == "developer" else role
content = _extract_content(item.get("content", ""))
messages.append({"role": mapped_role, "content": content})
continue
# assistant 消息(来自前一轮输出)
if item_type == "message" and role == "assistant":
_flush_assistant()
content_parts = item.get("content", [])
text = _extract_output_text(content_parts) if isinstance(content_parts, list) else str(content_parts)
pending_assistant_content = text
continue
# 简单 role=assistant(无 type 标记)
if item_type is None and role == "assistant":
_flush_assistant()
content = _extract_content(item.get("content", ""))
pending_assistant_content = content
continue
# function_call — 合并到前面的 assistant 消息
if item_type == "function_call":
if pending_assistant_content is None:
pending_assistant_content = ""
pending_tool_calls.append({
"id": item.get("call_id", item.get("id", _rand_id("call_"))),
"type": "function",
"function": {
"name": item.get("name", ""),
"arguments": item.get("arguments", "{}"),
},
})
continue
# function_call_output → tool 消息
if item_type == "function_call_output":
_flush_assistant()
messages.append({
"role": "tool",
"tool_call_id": item.get("call_id", ""),
"content": item.get("output", ""),
})
continue
# 其他未知类型 — 尝试当作普通消息
if role:
_flush_assistant()
content = _extract_content(item.get("content", ""))
messages.append({"role": role, "content": content})
_flush_assistant()
return messages
def _extract_content(content) -> str:
"""提取 content(可能是 str / list[{type,text}])。"""
if isinstance(content, str):
return content
if isinstance(content, list):
parts = []
for p in content:
if isinstance(p, dict):
if p.get("type") in ("input_text", "text"):
parts.append(p.get("text", ""))
elif p.get("type") == "output_text":
parts.append(p.get("text", ""))
elif isinstance(p, str):
parts.append(p)
return "".join(parts) or str(content)
return str(content)
def _extract_output_text(content_parts: list) -> str:
"""从 Responses output content parts 提取纯文本。"""
texts = []
for part in content_parts:
if isinstance(part, dict) and part.get("type") == "output_text":
texts.append(part.get("text", ""))
return "".join(texts)
def _convert_tools_for_chat(tools: list) -> list:
"""将 Responses 格式的 tools 转为 Chat 格式。
Responses: {"type": "function", "name": "shell", "description": ..., "parameters": ...}
Chat: {"type": "function", "function": {"name": "shell", "description": ..., "parameters": ...}}
"""
result = []
for t in tools:
if not isinstance(t, dict):
continue
if t.get("type") != "function":
continue
# 已经是 Chat 格式(有 "function" key)
if "function" in t:
result.append(t)
continue
# Responses 扁平格式 → Chat 嵌套格式
fn: dict[str, Any] = {"name": t.get("name", "")}
if "description" in t:
fn["description"] = t["description"]
if "parameters" in t:
fn["parameters"] = t["parameters"]
if "strict" in t:
fn["strict"] = t["strict"]
result.append({"type": "function", "function": fn})
return result
# ---------------------------------------------------------------------------
# 响应转换:Chat → Responses
# ---------------------------------------------------------------------------
class ResponsesStreamConverter:
"""将 Chat SSE 流实时转换为 Responses API 语义事件流。
用法:
converter = ResponsesStreamConverter(model="glm-5.2")
# 对后端返回的每个 SSE 行调 feed_line()
# feed_line 返回要发送给客户端的 Responses 事件字符串(可能多行)
for line in backend_sse:
events = converter.feed_line(line)
if events:
yield events.encode()
# 流结束后调 finish() 获取收尾事件
yield converter.finish().encode()
"""
def __init__(self, model: str = "unknown"):
self.resp_id = _rand_id("resp_")
self.msg_id = _rand_id("msg_")
self.model = model
self.created_at = int(time.time())
# 状态标记
self._emitted_created = False
self._emitted_msg_item = False
self._emitted_content_part = False
# 累积内容
self._content = ""
self._tool_calls: dict[int, dict] = {} # index → {id, name, args, fc_id, output_idx, emitted}
self._finish_reason: str | None = None
self._usage: dict | None = None
# ---- 公开接口 ----
def feed_line(self, line: str) -> str:
"""处理一行 SSE(如 'data: {...}'),返回转换后的 Responses 事件字符串。"""
line = line.strip()
if not line or not line.startswith("data:"):
return ""
data = line[5:].strip()
if data == "[DONE]":
return ""
try:
chunk = json.loads(data)
except json.JSONDecodeError:
return ""
return self._process_chunk(chunk)
def finish(self) -> str:
"""流结束后,发出收尾事件(done + completed)。"""
events: list[str] = []
# 关闭 text content
if self._emitted_content_part:
events.append(self._evt("response.output_text.done", {
"output_index": 0, "content_index": 0, "text": self._content
}))
events.append(self._evt("response.content_part.done", {
"output_index": 0, "content_index": 0,
"part": {"type": "output_text", "text": self._content, "annotations": []}
}))
if self._emitted_msg_item:
events.append(self._evt("response.output_item.done", {
"output_index": 0,
"item": self._msg_item("completed")
}))
# 关闭 function calls
for idx in sorted(self._tool_calls):
tc = self._tool_calls[idx]
if tc.get("emitted"):
oi = tc["output_idx"]
events.append(self._evt("response.function_call_arguments.done", {
"output_index": oi, "arguments": tc["args"]
}))
events.append(self._evt("response.output_item.done", {
"output_index": oi, "item": self._fc_item(tc, "completed")
}))
# response.completed
events.append(self._evt("response.completed", {
"response": self._response_obj("completed")
}))
return "".join(events)
def get_nonstream_response(self) -> dict:
"""流结束后获取完整的非流式 Response 对象。"""
return self._response_obj("completed")
# ---- 内部 ----
def _process_chunk(self, chunk: dict) -> str:
events: list[str] = []
# 模型名
if chunk.get("model"):
self.model = chunk["model"]
# 首次 → 发 created + in_progress
if not self._emitted_created:
resp = self._response_obj("in_progress")
events.append(self._evt("response.created", {"response": resp}))
events.append(self._evt("response.in_progress", {"response": resp}))
self._emitted_created = True
# usage
if chunk.get("usage"):
self._usage = chunk["usage"]
for choice in chunk.get("choices", []):
delta = choice.get("delta", {})
finish = choice.get("finish_reason")
# ---- content delta ----
content = delta.get("content")
if content:
if not self._emitted_msg_item:
events.append(self._evt("response.output_item.added", {
"output_index": 0,
"item": self._msg_item("in_progress", empty=True)
}))
self._emitted_msg_item = True
if not self._emitted_content_part:
events.append(self._evt("response.content_part.added", {
"output_index": 0, "content_index": 0,
"part": {"type": "output_text", "text": "", "annotations": []}
}))
self._emitted_content_part = True
self._content += content
events.append(self._evt("response.output_text.delta", {
"output_index": 0, "content_index": 0, "delta": content
}))
# ---- tool_calls delta ----
for tc in delta.get("tool_calls", []):
idx = tc.get("index", 0)
if idx not in self._tool_calls:
# 计算 output_index:msg 占 0,function_call 从 1 开始(如果有 msg)
base = 1 if (self._emitted_msg_item or self._content) else 0
oi = base + len(self._tool_calls)
self._tool_calls[idx] = {
"id": tc.get("id", ""),
"name": "",
"args": "",
"fc_id": _rand_id("fc_"),
"output_idx": oi,
"emitted": False,
}
slot = self._tool_calls[idx]
if tc.get("id"):
slot["id"] = tc["id"]
fn = tc.get("function", {})
if fn.get("name"):
slot["name"] = fn["name"]
if not slot["emitted"]:
# 确保 msg item 已发出(即使 content 为空)
if not self._emitted_msg_item and (self._content or not self._tool_calls):
pass # 不需要额外处理
events.append(self._evt("response.output_item.added", {
"output_index": slot["output_idx"],
"item": self._fc_item(slot, "in_progress")
}))
slot["emitted"] = True
if fn.get("arguments"):
slot["args"] += fn["arguments"]
events.append(self._evt("response.function_call_arguments.delta", {
"output_index": slot["output_idx"],
"delta": fn["arguments"]
}))
if finish:
self._finish_reason = finish
return "".join(events)
def _evt(self, event_type: str, data: dict) -> str:
"""格式化一个 SSE 事件。"""
payload = {"type": event_type, **data}
return f"data: {json.dumps(payload, ensure_ascii=False)}\n\n"
def _msg_item(self, status: str = "in_progress", empty: bool = False) -> dict:
content = [] if empty else [
{"type": "output_text", "text": self._content, "annotations": []}
]
return {
"type": "message",
"id": self.msg_id,
"status": status,
"role": "assistant",
"content": content,
}
def _fc_item(self, tc: dict, status: str) -> dict:
return {
"type": "function_call",
"id": tc["fc_id"],
"call_id": tc["id"],
"name": tc["name"],
"arguments": tc["args"],
"status": status,
}
def _response_obj(self, status: str) -> dict:
output = []
if self._emitted_msg_item or self._content:
output.append(self._msg_item(status))
for idx in sorted(self._tool_calls):
tc = self._tool_calls[idx]
if tc.get("emitted"):
output.append(self._fc_item(tc, status))
usage = None
if self._usage:
u = self._usage
usage = {
"input_tokens": u.get("prompt_tokens", u.get("input_tokens", 0)),
"input_tokens_details": {"cached_tokens": 0},
"output_tokens": u.get("completion_tokens", u.get("output_tokens", 0)),
"output_tokens_details": {"reasoning_tokens": 0},
"total_tokens": u.get("total_tokens", 0),
}
return {
"id": self.resp_id,
"object": "response",
"created_at": self.created_at,
"status": status,
"model": self.model,
"output": output,
"parallel_tool_calls": True,
"usage": usage,
}