fix: complete streamed patch repair responses
This commit is contained in:
@@ -159,6 +159,15 @@ class EmptyResp:
|
||||
def read(self):
|
||||
return b""
|
||||
|
||||
class SseResp:
|
||||
headers = {"Content-Type": "text/event-stream; charset=utf-8"}
|
||||
def __enter__(self):
|
||||
return self
|
||||
def __exit__(self, exc_type, exc, tb):
|
||||
return False
|
||||
def read(self):
|
||||
return b'data: {"choices":[{"delta":{"content":"CASAN"}}]}\n\ndata: {"choices":[{"delta":{"content":"_OK"}}],"usage":{"prompt_tokens":12,"completion_tokens":2}}\n\ndata: [DONE]\n'
|
||||
|
||||
def fake_urlopen(req, timeout):
|
||||
seen.append((req.full_url, dict(req.header_items()), json.loads(req.data.decode())))
|
||||
if req.full_url.endswith("/v1/responses"):
|
||||
@@ -194,6 +203,7 @@ assert seen[3][0] == "https://api.anthropic.com/v1/messages", seen[3]
|
||||
assert seen[0][2]["temperature"] == 0 and seen[0][2]["max_tokens"] == 16, seen[0][2]
|
||||
assert seen[1][2]["max_output_tokens"] == 1400 and seen[1][2]["store"] is False, seen[1][2]
|
||||
assert seen[2][2]["temperature"] == 0 and seen[2][2]["max_tokens"] == 16, seen[2][2]
|
||||
assert seen[2][2]["stream"] is False, seen[2][2]
|
||||
assert "temperature" not in seen[3][2] and seen[3][2]["max_tokens"] == 16, seen[3][2]
|
||||
|
||||
os.environ["CASAN_OPENAI_COMPATIBLE_BASE_URL"] = "http://169.254.169.254/v1"
|
||||
@@ -224,6 +234,10 @@ except SystemExit as exc:
|
||||
assert exc.code == 2, exc.code
|
||||
else:
|
||||
raise AssertionError("empty provider response was accepted")
|
||||
|
||||
sse = mc.decode_provider_json(SseResp(), "openai-compatible")
|
||||
assert sse["choices"][0]["message"]["content"] == "CASAN_OK", sse
|
||||
assert sse["usage"] == {"prompt_tokens": 12, "completion_tokens": 2}, sse
|
||||
print("ok")
|
||||
PY
|
||||
[[ $? -eq 0 ]] && pass "cloud/gateway provider responses parse real usage and reject malformed payloads" || fail "cloud/gateway provider parser coverage failed"
|
||||
|
||||
Reference in New Issue
Block a user