coding-proxy 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- coding/__init__.py +0 -0
- coding/proxy/__init__.py +3 -0
- coding/proxy/__main__.py +5 -0
- coding/proxy/auth/__init__.py +13 -0
- coding/proxy/auth/providers/__init__.py +6 -0
- coding/proxy/auth/providers/base.py +35 -0
- coding/proxy/auth/providers/github.py +133 -0
- coding/proxy/auth/providers/google.py +237 -0
- coding/proxy/auth/runtime.py +122 -0
- coding/proxy/auth/store.py +74 -0
- coding/proxy/cli/__init__.py +151 -0
- coding/proxy/cli/auth_commands.py +224 -0
- coding/proxy/compat/__init__.py +30 -0
- coding/proxy/compat/canonical.py +193 -0
- coding/proxy/compat/session_store.py +137 -0
- coding/proxy/config/__init__.py +6 -0
- coding/proxy/config/auth_schema.py +24 -0
- coding/proxy/config/loader.py +139 -0
- coding/proxy/config/resiliency.py +46 -0
- coding/proxy/config/routing.py +279 -0
- coding/proxy/config/schema.py +280 -0
- coding/proxy/config/server.py +23 -0
- coding/proxy/config/vendors.py +53 -0
- coding/proxy/convert/__init__.py +14 -0
- coding/proxy/convert/anthropic_to_gemini.py +352 -0
- coding/proxy/convert/anthropic_to_openai.py +352 -0
- coding/proxy/convert/gemini_sse_adapter.py +169 -0
- coding/proxy/convert/gemini_to_anthropic.py +98 -0
- coding/proxy/convert/openai_to_anthropic.py +88 -0
- coding/proxy/logging/__init__.py +49 -0
- coding/proxy/logging/db.py +308 -0
- coding/proxy/logging/stats.py +129 -0
- coding/proxy/model/__init__.py +93 -0
- coding/proxy/model/auth.py +32 -0
- coding/proxy/model/compat.py +153 -0
- coding/proxy/model/constants.py +21 -0
- coding/proxy/model/pricing.py +70 -0
- coding/proxy/model/token.py +64 -0
- coding/proxy/model/vendor.py +218 -0
- coding/proxy/pricing.py +100 -0
- coding/proxy/routing/__init__.py +47 -0
- coding/proxy/routing/circuit_breaker.py +152 -0
- coding/proxy/routing/error_classifier.py +67 -0
- coding/proxy/routing/executor.py +453 -0
- coding/proxy/routing/model_mapper.py +90 -0
- coding/proxy/routing/quota_guard.py +169 -0
- coding/proxy/routing/rate_limit.py +159 -0
- coding/proxy/routing/retry.py +82 -0
- coding/proxy/routing/router.py +84 -0
- coding/proxy/routing/session_manager.py +62 -0
- coding/proxy/routing/tier.py +171 -0
- coding/proxy/routing/usage_parser.py +193 -0
- coding/proxy/routing/usage_recorder.py +131 -0
- coding/proxy/server/__init__.py +1 -0
- coding/proxy/server/app.py +142 -0
- coding/proxy/server/factory.py +175 -0
- coding/proxy/server/request_normalizer.py +139 -0
- coding/proxy/server/responses.py +74 -0
- coding/proxy/server/routes.py +264 -0
- coding/proxy/streaming/__init__.py +1 -0
- coding/proxy/streaming/anthropic_compat.py +484 -0
- coding/proxy/vendors/__init__.py +29 -0
- coding/proxy/vendors/anthropic.py +44 -0
- coding/proxy/vendors/antigravity.py +328 -0
- coding/proxy/vendors/base.py +353 -0
- coding/proxy/vendors/copilot.py +702 -0
- coding/proxy/vendors/copilot_models.py +438 -0
- coding/proxy/vendors/copilot_token_manager.py +167 -0
- coding/proxy/vendors/copilot_urls.py +16 -0
- coding/proxy/vendors/mixins.py +71 -0
- coding/proxy/vendors/token_manager.py +128 -0
- coding/proxy/vendors/zhipu.py +243 -0
- coding_proxy-0.1.0.dist-info/METADATA +184 -0
- coding_proxy-0.1.0.dist-info/RECORD +77 -0
- coding_proxy-0.1.0.dist-info/WHEEL +4 -0
- coding_proxy-0.1.0.dist-info/entry_points.txt +2 -0
- coding_proxy-0.1.0.dist-info/licenses/LICENSE +201 -0
|
@@ -0,0 +1,484 @@
|
|
|
1
|
+
"""将供应商流式响应收敛为 Anthropic 兼容 SSE."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
import logging
|
|
7
|
+
import uuid
|
|
8
|
+
from typing import Any, AsyncIterator
|
|
9
|
+
|
|
10
|
+
logger = logging.getLogger(__name__)
|
|
11
|
+
|
|
12
|
+
_DIRECT_EVENTS = {
|
|
13
|
+
"message_start",
|
|
14
|
+
"content_block_start",
|
|
15
|
+
"content_block_delta",
|
|
16
|
+
"content_block_stop",
|
|
17
|
+
"message_delta",
|
|
18
|
+
"message_stop",
|
|
19
|
+
"ping",
|
|
20
|
+
"error",
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class _OpenAICompatState:
|
|
25
|
+
"""OpenAI → Anthropic 流式转换状态机.
|
|
26
|
+
|
|
27
|
+
管理消息生命周期(start/close)和内容块生命周期(text / thinking / tool_use),
|
|
28
|
+
将块操作封装为独立方法,使 ``_normalize_openai_chunk`` 成为纯粹的事件分发器。
|
|
29
|
+
"""
|
|
30
|
+
|
|
31
|
+
def __init__(self, model: str) -> None:
|
|
32
|
+
self.model = model
|
|
33
|
+
self.message_id = f"msg_{uuid.uuid4().hex[:24]}"
|
|
34
|
+
self.started = False
|
|
35
|
+
self.stopped = False
|
|
36
|
+
self.input_tokens = 0
|
|
37
|
+
self.output_tokens = 0
|
|
38
|
+
self.cache_creation_tokens = 0
|
|
39
|
+
self.cache_read_tokens = 0
|
|
40
|
+
self.block_index = 0
|
|
41
|
+
self.content_block_open = False
|
|
42
|
+
self.tool_calls: dict[int, dict[str, Any]] = {}
|
|
43
|
+
self.usage_updated = False
|
|
44
|
+
self.thinking_block_open = False
|
|
45
|
+
|
|
46
|
+
# ── 消息生命周期 ────────────────────────────────────────
|
|
47
|
+
|
|
48
|
+
def ensure_started(self) -> list[bytes]:
|
|
49
|
+
if self.started:
|
|
50
|
+
return []
|
|
51
|
+
self.started = True
|
|
52
|
+
return [
|
|
53
|
+
_make_event("message_start", {
|
|
54
|
+
"type": "message_start",
|
|
55
|
+
"message": {
|
|
56
|
+
"id": self.message_id,
|
|
57
|
+
"type": "message",
|
|
58
|
+
"role": "assistant",
|
|
59
|
+
"content": [],
|
|
60
|
+
"model": self.model,
|
|
61
|
+
"usage": {
|
|
62
|
+
"input_tokens": self.input_tokens,
|
|
63
|
+
"output_tokens": 0,
|
|
64
|
+
**(
|
|
65
|
+
{"cache_creation_input_tokens": self.cache_creation_tokens}
|
|
66
|
+
if self.cache_creation_tokens > 0 else {}
|
|
67
|
+
),
|
|
68
|
+
**(
|
|
69
|
+
{"cache_read_input_tokens": self.cache_read_tokens}
|
|
70
|
+
if self.cache_read_tokens > 0 else {}
|
|
71
|
+
),
|
|
72
|
+
},
|
|
73
|
+
},
|
|
74
|
+
}),
|
|
75
|
+
]
|
|
76
|
+
|
|
77
|
+
def close(self, reason: str = "end_turn") -> list[bytes]:
|
|
78
|
+
if self.stopped:
|
|
79
|
+
return []
|
|
80
|
+
self.stopped = True
|
|
81
|
+
chunks: list[bytes] = []
|
|
82
|
+
if self.started and self.content_block_open:
|
|
83
|
+
chunks.append(_make_event("content_block_stop", {
|
|
84
|
+
"type": "content_block_stop",
|
|
85
|
+
"index": self.block_index,
|
|
86
|
+
}))
|
|
87
|
+
self.content_block_open = False
|
|
88
|
+
usage_data = {"output_tokens": self.output_tokens}
|
|
89
|
+
if self.usage_updated and self.input_tokens > 0:
|
|
90
|
+
usage_data["input_tokens"] = self.input_tokens
|
|
91
|
+
if self.cache_creation_tokens > 0:
|
|
92
|
+
usage_data["cache_creation_input_tokens"] = self.cache_creation_tokens
|
|
93
|
+
if self.cache_read_tokens > 0:
|
|
94
|
+
usage_data["cache_read_input_tokens"] = self.cache_read_tokens
|
|
95
|
+
chunks.append(_make_event("message_delta", {
|
|
96
|
+
"type": "message_delta",
|
|
97
|
+
"delta": {"stop_reason": reason, "stop_sequence": None},
|
|
98
|
+
"usage": usage_data,
|
|
99
|
+
}))
|
|
100
|
+
chunks.append(_make_event("message_stop", {"type": "message_stop"}))
|
|
101
|
+
return chunks
|
|
102
|
+
|
|
103
|
+
# ── Token 用量更新 ──────────────────────────────────────
|
|
104
|
+
|
|
105
|
+
def update_usage(self, usage: dict[str, Any]) -> None:
|
|
106
|
+
"""从 OpenAI usage 字典更新 token 计数."""
|
|
107
|
+
if "prompt_tokens" in usage:
|
|
108
|
+
self.input_tokens = usage.get("prompt_tokens", self.input_tokens)
|
|
109
|
+
self.usage_updated = True
|
|
110
|
+
if "completion_tokens" in usage:
|
|
111
|
+
self.output_tokens = usage.get("completion_tokens", self.output_tokens)
|
|
112
|
+
self.usage_updated = True
|
|
113
|
+
crt = _extract_cache_read_tokens(usage)
|
|
114
|
+
if crt > 0:
|
|
115
|
+
self.cache_read_tokens = crt
|
|
116
|
+
self.usage_updated = True
|
|
117
|
+
cct = _extract_cache_creation_tokens(usage)
|
|
118
|
+
if cct > 0:
|
|
119
|
+
self.cache_creation_tokens = cct
|
|
120
|
+
self.usage_updated = True
|
|
121
|
+
|
|
122
|
+
# ── 内容块生命周期 ──────────────────────────────────────
|
|
123
|
+
|
|
124
|
+
def close_content_block(self) -> list[bytes]:
|
|
125
|
+
"""关闭当前打开的内容块(如有),递增 block_index."""
|
|
126
|
+
if not self.content_block_open:
|
|
127
|
+
return []
|
|
128
|
+
self.content_block_open = False
|
|
129
|
+
self.block_index += 1
|
|
130
|
+
return [_make_event("content_block_stop", {
|
|
131
|
+
"type": "content_block_stop",
|
|
132
|
+
"index": self.block_index - 1,
|
|
133
|
+
})]
|
|
134
|
+
|
|
135
|
+
def open_thinking_block(self) -> list[bytes]:
|
|
136
|
+
"""打开 thinking 内容块(如尚未打开)."""
|
|
137
|
+
if self.thinking_block_open:
|
|
138
|
+
return []
|
|
139
|
+
self.thinking_block_open = True
|
|
140
|
+
self.content_block_open = True
|
|
141
|
+
logger.debug("copilot-stream: opening thinking block at index=%d", self.block_index)
|
|
142
|
+
return [_make_event("content_block_start", {
|
|
143
|
+
"type": "content_block_start",
|
|
144
|
+
"index": self.block_index,
|
|
145
|
+
"content_block": {"type": "thinking", "thinking": ""},
|
|
146
|
+
})]
|
|
147
|
+
|
|
148
|
+
def ensure_text_block(self) -> list[bytes]:
|
|
149
|
+
"""确保当前为 text 内容块:先关闭 thinking,再处理工具块冲突,最后打开 text 块."""
|
|
150
|
+
chunks: list[bytes] = []
|
|
151
|
+
# 如果 thinking 块开着,先关闭它
|
|
152
|
+
if self.thinking_block_open:
|
|
153
|
+
logger.debug(
|
|
154
|
+
"copilot-stream: closing thinking block at index=%d before opening text block",
|
|
155
|
+
self.block_index,
|
|
156
|
+
)
|
|
157
|
+
chunks.append(_make_event("content_block_stop", {
|
|
158
|
+
"type": "content_block_stop",
|
|
159
|
+
"index": self.block_index,
|
|
160
|
+
}))
|
|
161
|
+
self.block_index += 1
|
|
162
|
+
self.thinking_block_open = False
|
|
163
|
+
self.content_block_open = False
|
|
164
|
+
# 如果已有工具块占用当前 index,先关闭
|
|
165
|
+
if self.content_block_open and any(
|
|
166
|
+
t.get("anthropic_block_index") == self.block_index
|
|
167
|
+
for t in self.tool_calls.values()
|
|
168
|
+
):
|
|
169
|
+
chunks.extend(self.close_content_block())
|
|
170
|
+
# 打开 text 块
|
|
171
|
+
if not self.content_block_open:
|
|
172
|
+
chunks.append(_make_event("content_block_start", {
|
|
173
|
+
"type": "content_block_start",
|
|
174
|
+
"index": self.block_index,
|
|
175
|
+
"content_block": {"type": "text", "text": ""},
|
|
176
|
+
}))
|
|
177
|
+
self.content_block_open = True
|
|
178
|
+
return chunks
|
|
179
|
+
|
|
180
|
+
def open_tool_block(self, tool_index: int, tool_call: dict[str, Any]) -> list[bytes]:
|
|
181
|
+
"""注册并打开 tool_use 内容块."""
|
|
182
|
+
chunks: list[bytes] = []
|
|
183
|
+
if self.content_block_open:
|
|
184
|
+
chunks.extend(self.close_content_block())
|
|
185
|
+
self.tool_calls[tool_index] = {
|
|
186
|
+
"id": tool_call["id"],
|
|
187
|
+
"name": tool_call["function"]["name"],
|
|
188
|
+
"anthropic_block_index": self.block_index,
|
|
189
|
+
}
|
|
190
|
+
chunks.append(_make_event("content_block_start", {
|
|
191
|
+
"type": "content_block_start",
|
|
192
|
+
"index": self.block_index,
|
|
193
|
+
"content_block": {
|
|
194
|
+
"type": "tool_use",
|
|
195
|
+
"id": tool_call["id"],
|
|
196
|
+
"name": tool_call["function"]["name"],
|
|
197
|
+
"input": {},
|
|
198
|
+
},
|
|
199
|
+
}))
|
|
200
|
+
self.content_block_open = True
|
|
201
|
+
return chunks
|
|
202
|
+
|
|
203
|
+
def feed_tool_arguments(self, tool_index: int, arguments: str) -> list[bytes]:
|
|
204
|
+
"""向已注册的 tool_use 块追加参数 delta."""
|
|
205
|
+
tool_info = self.tool_calls.get(tool_index)
|
|
206
|
+
if not tool_info or not arguments:
|
|
207
|
+
return []
|
|
208
|
+
return [_make_event("content_block_delta", {
|
|
209
|
+
"type": "content_block_delta",
|
|
210
|
+
"index": tool_info["anthropic_block_index"],
|
|
211
|
+
"delta": {
|
|
212
|
+
"type": "input_json_delta",
|
|
213
|
+
"partial_json": arguments,
|
|
214
|
+
},
|
|
215
|
+
})]
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
def _make_event(event_type: str, data: dict[str, Any]) -> bytes:
|
|
219
|
+
return f"event: {event_type}\ndata: {json.dumps(data, ensure_ascii=False)}\n\n".encode()
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
def _extract_text_fragments(delta: Any) -> list[str]:
|
|
223
|
+
if isinstance(delta, str):
|
|
224
|
+
return [delta] if delta else []
|
|
225
|
+
if isinstance(delta, list):
|
|
226
|
+
fragments: list[str] = []
|
|
227
|
+
for item in delta:
|
|
228
|
+
if isinstance(item, str):
|
|
229
|
+
if item:
|
|
230
|
+
fragments.append(item)
|
|
231
|
+
elif isinstance(item, dict) and item.get("type") == "text":
|
|
232
|
+
text = item.get("text", "")
|
|
233
|
+
if text:
|
|
234
|
+
fragments.append(text)
|
|
235
|
+
return fragments
|
|
236
|
+
return []
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
# OpenAI 兼容协议供应商可能使用的非标准内容块类型别名(Copilot / Antigravity 等使用)
|
|
240
|
+
_TOOL_USE_BLOCK_TYPES = {"text", "tool_use", "tool_call", "function_call", "thinking"}
|
|
241
|
+
|
|
242
|
+
# OpenAI 兼容协议供应商可能使用的非标准 delta 类型别名
|
|
243
|
+
_INPUT_JSON_DELTA_TYPES = {"input_json_delta", "arguments_delta", "tool_call_delta"}
|
|
244
|
+
|
|
245
|
+
|
|
246
|
+
def _normalize_direct_event(data: dict[str, Any], event_name: str | None) -> list[bytes]:
|
|
247
|
+
event_type = data.get("type")
|
|
248
|
+
if event_type == "content_block_start":
|
|
249
|
+
block = data.get("content_block", {})
|
|
250
|
+
block_type = block.get("type")
|
|
251
|
+
if block_type not in _TOOL_USE_BLOCK_TYPES:
|
|
252
|
+
logger.debug("Filtered non-standard content_block_start type: %s", block_type)
|
|
253
|
+
return []
|
|
254
|
+
# OpenAI 兼容供应商可能在 content_block_start.input 中内联返回完整工具参数
|
|
255
|
+
if block_type == "tool_use":
|
|
256
|
+
result = [_make_event(event_name or event_type, data)]
|
|
257
|
+
inline_input = block.get("input")
|
|
258
|
+
if isinstance(inline_input, dict) and inline_input:
|
|
259
|
+
logger.debug(
|
|
260
|
+
"Tool_use block with inline input: name=%s args=%s",
|
|
261
|
+
block.get("name", "?"),
|
|
262
|
+
json.dumps(inline_input, ensure_ascii=False)[:200],
|
|
263
|
+
)
|
|
264
|
+
result.append(_make_event("content_block_delta", {
|
|
265
|
+
"type": "content_block_delta",
|
|
266
|
+
"index": data.get("index", 0),
|
|
267
|
+
"delta": {
|
|
268
|
+
"type": "input_json_delta",
|
|
269
|
+
"partial_json": json.dumps(inline_input, ensure_ascii=False),
|
|
270
|
+
},
|
|
271
|
+
}))
|
|
272
|
+
return result
|
|
273
|
+
# 非标准类型归一化为 tool_use
|
|
274
|
+
if block_type in ("tool_call", "function_call"):
|
|
275
|
+
logger.debug("Normalizing non-standard block type '%s' to 'tool_use'", block_type)
|
|
276
|
+
normalized_block = {**block, "type": "tool_use"}
|
|
277
|
+
normalized_data = {**data, "content_block": normalized_block}
|
|
278
|
+
return [_make_event(event_name or event_type, normalized_data)]
|
|
279
|
+
|
|
280
|
+
if event_type == "content_block_delta":
|
|
281
|
+
delta = data.get("delta", {})
|
|
282
|
+
delta_type = delta.get("type")
|
|
283
|
+
# 放行标准 delta 类型
|
|
284
|
+
if delta_type in {"text_delta", "input_json_delta", "thinking_delta"}:
|
|
285
|
+
return [_make_event(event_name or event_type, data)]
|
|
286
|
+
# 归一化非标准 input_json_delta 别名(OpenAI 兼容供应商可能使用 arguments_delta 等)
|
|
287
|
+
if delta_type in _INPUT_JSON_DELTA_TYPES:
|
|
288
|
+
normalized_delta = {**delta, "type": "input_json_delta"}
|
|
289
|
+
if "partial_json" not in normalized_delta and "arguments" in normalized_delta:
|
|
290
|
+
normalized_delta["partial_json"] = normalized_delta.pop("arguments")
|
|
291
|
+
logger.debug("Normalizing non-standard delta type '%s' to 'input_json_delta'", delta_type)
|
|
292
|
+
return [_make_event(event_name or event_type, {**data, "delta": normalized_delta})]
|
|
293
|
+
# 其他 delta 类型过滤
|
|
294
|
+
logger.debug("Filtered non-standard content_block_delta type: %s", delta_type)
|
|
295
|
+
return []
|
|
296
|
+
|
|
297
|
+
if event_type not in _DIRECT_EVENTS:
|
|
298
|
+
logger.debug("Filtered non-standard event type: %s", event_type)
|
|
299
|
+
return []
|
|
300
|
+
return [_make_event(event_name or event_type, data)]
|
|
301
|
+
|
|
302
|
+
|
|
303
|
+
def _normalize_stream_event(data: dict[str, Any], event_name: str | None) -> list[bytes]:
|
|
304
|
+
nested = data.get("event")
|
|
305
|
+
if not isinstance(nested, dict):
|
|
306
|
+
return []
|
|
307
|
+
nested_name = event_name or nested.get("type")
|
|
308
|
+
return _normalize_direct_event(nested, nested_name)
|
|
309
|
+
|
|
310
|
+
|
|
311
|
+
def _extract_prompt_tokens_details(usage: dict[str, Any]) -> dict[str, Any]:
|
|
312
|
+
details = usage.get("prompt_tokens_details")
|
|
313
|
+
return details if isinstance(details, dict) else {}
|
|
314
|
+
|
|
315
|
+
|
|
316
|
+
def _extract_cache_read_tokens(usage: dict[str, Any]) -> int:
|
|
317
|
+
details = _extract_prompt_tokens_details(usage)
|
|
318
|
+
for value in (
|
|
319
|
+
usage.get("cache_read_input_tokens"),
|
|
320
|
+
details.get("cached_tokens"),
|
|
321
|
+
details.get("cache_read_tokens"),
|
|
322
|
+
):
|
|
323
|
+
if isinstance(value, int):
|
|
324
|
+
return value
|
|
325
|
+
return 0
|
|
326
|
+
|
|
327
|
+
|
|
328
|
+
def _extract_cache_creation_tokens(usage: dict[str, Any]) -> int:
|
|
329
|
+
details = _extract_prompt_tokens_details(usage)
|
|
330
|
+
for value in (
|
|
331
|
+
usage.get("cache_creation_input_tokens"),
|
|
332
|
+
details.get("cache_creation_input_tokens"),
|
|
333
|
+
details.get("cache_creation_tokens"),
|
|
334
|
+
):
|
|
335
|
+
if isinstance(value, int):
|
|
336
|
+
return value
|
|
337
|
+
return 0
|
|
338
|
+
|
|
339
|
+
|
|
340
|
+
def _normalize_openai_chunk(data: dict[str, Any], state: _OpenAICompatState) -> list[bytes]:
|
|
341
|
+
"""将 OpenAI 格式 chunk 转换为 Anthropic SSE 事件序列.
|
|
342
|
+
|
|
343
|
+
重构后为纯粹的事件分发器:token 更新、块生命周期管理均委托给 State 方法。
|
|
344
|
+
"""
|
|
345
|
+
chunks: list[bytes] = []
|
|
346
|
+
|
|
347
|
+
# 1. Token 用量更新(委托给 State)
|
|
348
|
+
state.update_usage(data.get("usage", {}))
|
|
349
|
+
|
|
350
|
+
choices = data.get("choices", [])
|
|
351
|
+
if not choices:
|
|
352
|
+
return chunks
|
|
353
|
+
|
|
354
|
+
choice = choices[0]
|
|
355
|
+
delta = choice.get("delta", {})
|
|
356
|
+
finish_reason = choice.get("finish_reason")
|
|
357
|
+
|
|
358
|
+
# 2. Reasoning / thinking 内容 → thinking 块
|
|
359
|
+
reasoning_content = delta.get("reasoning_content")
|
|
360
|
+
if reasoning_content:
|
|
361
|
+
chunks.extend(state.ensure_started())
|
|
362
|
+
chunks.extend(state.open_thinking_block())
|
|
363
|
+
chunks.append(_make_event("content_block_delta", {
|
|
364
|
+
"type": "content_block_delta",
|
|
365
|
+
"index": state.block_index,
|
|
366
|
+
"delta": {"type": "thinking_delta", "thinking": reasoning_content},
|
|
367
|
+
}))
|
|
368
|
+
|
|
369
|
+
# 3. Text 内容 → text 块
|
|
370
|
+
text_fragments = _extract_text_fragments(delta.get("content"))
|
|
371
|
+
if text_fragments:
|
|
372
|
+
chunks.extend(state.ensure_started())
|
|
373
|
+
chunks.extend(state.ensure_text_block())
|
|
374
|
+
for text in text_fragments:
|
|
375
|
+
chunks.append(_make_event("content_block_delta", {
|
|
376
|
+
"type": "content_block_delta",
|
|
377
|
+
"index": state.block_index,
|
|
378
|
+
"delta": {"type": "text_delta", "text": text},
|
|
379
|
+
}))
|
|
380
|
+
|
|
381
|
+
# 4. Tool calls → tool_use 块
|
|
382
|
+
tool_calls = delta.get("tool_calls") or []
|
|
383
|
+
for tool_call in tool_calls:
|
|
384
|
+
if not isinstance(tool_call, dict):
|
|
385
|
+
continue
|
|
386
|
+
tool_index = int(tool_call.get("index", 0))
|
|
387
|
+
# 注册新工具调用
|
|
388
|
+
if tool_call.get("id") and isinstance(tool_call.get("function"), dict) and tool_call["function"].get("name"):
|
|
389
|
+
chunks.extend(state.ensure_started())
|
|
390
|
+
chunks.extend(state.open_tool_block(tool_index, tool_call))
|
|
391
|
+
# 追加工具参数
|
|
392
|
+
function = tool_call.get("function")
|
|
393
|
+
if isinstance(function, dict):
|
|
394
|
+
arguments = function.get("arguments")
|
|
395
|
+
tool_info = state.tool_calls.get(tool_index)
|
|
396
|
+
if arguments:
|
|
397
|
+
chunks.extend(state.feed_tool_arguments(tool_index, arguments))
|
|
398
|
+
elif tool_info and arguments is None and finish_reason in ("tool_calls", "stop"):
|
|
399
|
+
logger.debug(
|
|
400
|
+
"Tool call '%s' has null arguments at finish_reason=%s",
|
|
401
|
+
tool_info.get("name", "?"), finish_reason,
|
|
402
|
+
)
|
|
403
|
+
|
|
404
|
+
# 5. Finish reason → 关闭消息
|
|
405
|
+
if finish_reason:
|
|
406
|
+
stop_reason = "max_tokens" if finish_reason == "length" else "end_turn"
|
|
407
|
+
if finish_reason == "tool_calls":
|
|
408
|
+
stop_reason = "tool_use"
|
|
409
|
+
chunks.extend(state.close(stop_reason))
|
|
410
|
+
|
|
411
|
+
return chunks
|
|
412
|
+
|
|
413
|
+
|
|
414
|
+
async def normalize_anthropic_compatible_stream(
|
|
415
|
+
upstream: AsyncIterator[bytes],
|
|
416
|
+
*,
|
|
417
|
+
model: str,
|
|
418
|
+
) -> AsyncIterator[bytes]:
|
|
419
|
+
"""过滤供应商私有事件,并在需要时把 OpenAI 风格流转成 Anthropic SSE."""
|
|
420
|
+
state = _OpenAICompatState(model)
|
|
421
|
+
|
|
422
|
+
async for raw_chunk in upstream:
|
|
423
|
+
text = raw_chunk.decode("utf-8", errors="ignore")
|
|
424
|
+
current_event: str | None = None
|
|
425
|
+
emitted_any = False
|
|
426
|
+
|
|
427
|
+
for raw_line in text.splitlines():
|
|
428
|
+
line = raw_line.strip()
|
|
429
|
+
if not line:
|
|
430
|
+
current_event = None
|
|
431
|
+
continue
|
|
432
|
+
if line.startswith("event: "):
|
|
433
|
+
current_event = line[7:].strip()
|
|
434
|
+
continue
|
|
435
|
+
if not line.startswith("data: "):
|
|
436
|
+
continue
|
|
437
|
+
|
|
438
|
+
payload = line[6:].strip()
|
|
439
|
+
if not payload:
|
|
440
|
+
continue
|
|
441
|
+
if payload == "[DONE]":
|
|
442
|
+
for chunk in state.close():
|
|
443
|
+
emitted_any = True
|
|
444
|
+
yield chunk
|
|
445
|
+
continue
|
|
446
|
+
|
|
447
|
+
try:
|
|
448
|
+
data = json.loads(payload)
|
|
449
|
+
except json.JSONDecodeError:
|
|
450
|
+
continue
|
|
451
|
+
|
|
452
|
+
chunks: list[bytes] = []
|
|
453
|
+
data_type = data.get("type")
|
|
454
|
+
if data_type in _DIRECT_EVENTS:
|
|
455
|
+
if data_type == "message_start":
|
|
456
|
+
state.started = True
|
|
457
|
+
elif data_type == "message_stop":
|
|
458
|
+
state.stopped = True
|
|
459
|
+
chunks = _normalize_direct_event(data, current_event)
|
|
460
|
+
elif data_type == "stream_event":
|
|
461
|
+
chunks = _normalize_stream_event(data, current_event)
|
|
462
|
+
elif "choices" in data:
|
|
463
|
+
# 诊断:记录含工具调用的 OpenAI 格式 chunk
|
|
464
|
+
choices = data.get("choices", [])
|
|
465
|
+
if choices and any(
|
|
466
|
+
isinstance(c.get("delta", {}).get("tool_calls"), list)
|
|
467
|
+
for c in choices
|
|
468
|
+
if isinstance(c, dict)
|
|
469
|
+
):
|
|
470
|
+
logger.debug(
|
|
471
|
+
"OpenAI tool_call chunk: %s",
|
|
472
|
+
json.dumps(data, ensure_ascii=False)[:500],
|
|
473
|
+
)
|
|
474
|
+
chunks = _normalize_openai_chunk(data, state)
|
|
475
|
+
|
|
476
|
+
for chunk in chunks:
|
|
477
|
+
emitted_any = True
|
|
478
|
+
yield chunk
|
|
479
|
+
|
|
480
|
+
if emitted_any:
|
|
481
|
+
continue
|
|
482
|
+
|
|
483
|
+
for chunk in state.close():
|
|
484
|
+
yield chunk
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"""供应商适配层 — 所有供应商实现的统一入口."""
|
|
2
|
+
|
|
3
|
+
from .base import ( # noqa: F401
|
|
4
|
+
BaseVendor,
|
|
5
|
+
BaseBackend, # 向后兼容别名
|
|
6
|
+
VendorCapabilities,
|
|
7
|
+
VendorResponse,
|
|
8
|
+
NoCompatibleVendorError,
|
|
9
|
+
NoCompatibleBackendError, # 向后兼容别名
|
|
10
|
+
CapabilityLossReason,
|
|
11
|
+
RequestCapabilities,
|
|
12
|
+
UsageInfo,
|
|
13
|
+
CopilotExchangeDiagnostics,
|
|
14
|
+
CopilotMisdirectedRequest,
|
|
15
|
+
CopilotModelCatalog,
|
|
16
|
+
decode_json_body,
|
|
17
|
+
extract_error_message,
|
|
18
|
+
sanitize_headers_for_synthetic_response,
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
__all__ = [
|
|
22
|
+
"BaseVendor", "BaseBackend",
|
|
23
|
+
"VendorCapabilities", "BackendCapabilities",
|
|
24
|
+
"VendorResponse", "BackendResponse",
|
|
25
|
+
"NoCompatibleVendorError", "NoCompatibleBackendError",
|
|
26
|
+
"CapabilityLossReason", "RequestCapabilities", "UsageInfo",
|
|
27
|
+
"CopilotExchangeDiagnostics", "CopilotMisdirectedRequest", "CopilotModelCatalog",
|
|
28
|
+
"decode_json_body", "extract_error_message", "sanitize_headers_for_synthetic_response",
|
|
29
|
+
]
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"""Anthropic 官方供应商 — 透传 OAuth token."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import Any
|
|
6
|
+
|
|
7
|
+
from ..config.schema import AnthropicConfig, FailoverConfig
|
|
8
|
+
from .base import PROXY_SKIP_HEADERS, BaseVendor
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class AnthropicVendor(BaseVendor):
|
|
12
|
+
"""Anthropic 官方 API 供应商.
|
|
13
|
+
|
|
14
|
+
透传 Claude Code 发来的 OAuth token 和请求体到 Anthropic API.
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
def __init__(self, config: AnthropicConfig, failover_config: FailoverConfig) -> None:
|
|
18
|
+
super().__init__(config.base_url, config.timeout_ms, failover_config)
|
|
19
|
+
|
|
20
|
+
def get_name(self) -> str:
|
|
21
|
+
return "anthropic"
|
|
22
|
+
|
|
23
|
+
async def check_health(self) -> bool:
|
|
24
|
+
"""Anthropic 健康检查 — 透明代理被动策略.
|
|
25
|
+
|
|
26
|
+
Anthropic 供应商作为透明代理,不管理凭证(auth 来自客户端请求头),
|
|
27
|
+
无法独立发起 API 探测。健康状态通过 VendorTier 的 Rate Limit
|
|
28
|
+
Deadline 门控判定:仅在服务端声明的 rate limit 重置时间到期后,
|
|
29
|
+
才允许使用下一个真实客户端请求作为探针。
|
|
30
|
+
"""
|
|
31
|
+
return True
|
|
32
|
+
|
|
33
|
+
async def _prepare_request(
|
|
34
|
+
self,
|
|
35
|
+
request_body: dict[str, Any],
|
|
36
|
+
headers: dict[str, str],
|
|
37
|
+
) -> tuple[dict[str, Any], dict[str, str]]:
|
|
38
|
+
"""透传请求体,过滤无关请求头."""
|
|
39
|
+
filtered = {k: v for k, v in headers.items() if k.lower() not in PROXY_SKIP_HEADERS}
|
|
40
|
+
return request_body, filtered
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
# 向后兼容别名
|
|
44
|
+
AnthropicBackend = AnthropicVendor
|