mirror of
https://github.com/Routstr/routstr-core.git
synced 2026-10-05 12:28:22 +00:00
display correct provider
This commit is contained in:
@@ -3928,10 +3928,9 @@ class BaseUpstreamProvider:
|
|||||||
data_json = json.loads(line[6:])
|
data_json = json.loads(line[6:])
|
||||||
if not isinstance(data_json, dict):
|
if not isinstance(data_json, dict):
|
||||||
continue
|
continue
|
||||||
changed = False
|
provider_before = data_json.get("provider")
|
||||||
if "provider" not in data_json:
|
self._apply_provider_field(data_json)
|
||||||
self._apply_provider_field(data_json)
|
changed = data_json.get("provider") != provider_before
|
||||||
changed = True
|
|
||||||
if cost_data and "usage" in data_json and data_json["usage"]:
|
if cost_data and "usage" in data_json and data_json["usage"]:
|
||||||
_inject_cost_into_usage(data_json, cost_data)
|
_inject_cost_into_usage(data_json, cost_data)
|
||||||
changed = True
|
changed = True
|
||||||
@@ -4937,10 +4936,9 @@ class BaseUpstreamProvider:
|
|||||||
continue
|
continue
|
||||||
if not isinstance(data_json, dict):
|
if not isinstance(data_json, dict):
|
||||||
continue
|
continue
|
||||||
changed = False
|
provider_before = data_json.get("provider")
|
||||||
if "provider" not in data_json:
|
self._apply_provider_field(data_json)
|
||||||
self._apply_provider_field(data_json)
|
changed = data_json.get("provider") != provider_before
|
||||||
changed = True
|
|
||||||
payload = _responses_usage_payload(data_json)
|
payload = _responses_usage_payload(data_json)
|
||||||
if cost_data and isinstance(payload.get("usage"), dict):
|
if cost_data and isinstance(payload.get("usage"), dict):
|
||||||
_inject_cost_into_usage(payload, cost_data)
|
_inject_cost_into_usage(payload, cost_data)
|
||||||
|
|||||||
@@ -0,0 +1,54 @@
|
|||||||
|
import json
|
||||||
|
from typing import Any
|
||||||
|
from unittest.mock import AsyncMock, patch
|
||||||
|
|
||||||
|
import httpx
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from routstr.upstream.openrouter import OpenRouterUpstreamProvider
|
||||||
|
|
||||||
|
|
||||||
|
async def _body(response: Any) -> bytes:
|
||||||
|
chunks: list[bytes] = []
|
||||||
|
async for chunk in response.body_iterator:
|
||||||
|
chunks.append(chunk if isinstance(chunk, bytes) else chunk.encode())
|
||||||
|
return b"".join(chunks)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_x_cashu_chat_stream_reports_complete_provider_path() -> None:
|
||||||
|
provider = OpenRouterUpstreamProvider(api_key="test-key")
|
||||||
|
payload = {"model": "glm-4.5", "provider": "z.ai"}
|
||||||
|
content = f"data: {json.dumps(payload)}\n"
|
||||||
|
|
||||||
|
response = await provider.handle_x_cashu_streaming_response(
|
||||||
|
content,
|
||||||
|
httpx.Response(200, headers={"content-type": "text/event-stream"}),
|
||||||
|
amount=1,
|
||||||
|
unit="sat",
|
||||||
|
max_cost_for_model=1,
|
||||||
|
)
|
||||||
|
|
||||||
|
event = json.loads((await _body(response)).decode().removeprefix("data: "))
|
||||||
|
assert event["provider"] == "openrouter:z.ai"
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_x_cashu_responses_stream_reports_complete_provider_path() -> None:
|
||||||
|
provider = OpenRouterUpstreamProvider(api_key="test-key")
|
||||||
|
event = {"type": "response.created", "provider": "z.ai"}
|
||||||
|
content = f"data: {json.dumps(event)}\n\n"
|
||||||
|
|
||||||
|
with patch.object(
|
||||||
|
provider, "get_x_cashu_cost", new=AsyncMock(return_value=None)
|
||||||
|
):
|
||||||
|
response = await provider.handle_x_cashu_streaming_responses_response(
|
||||||
|
content,
|
||||||
|
httpx.Response(200, headers={"content-type": "text/event-stream"}),
|
||||||
|
amount=1,
|
||||||
|
unit="sat",
|
||||||
|
max_cost_for_model=1,
|
||||||
|
)
|
||||||
|
|
||||||
|
payload = json.loads((await _body(response)).decode().removeprefix("data: "))
|
||||||
|
assert payload["provider"] == "openrouter:z.ai"
|
||||||
Reference in New Issue
Block a user