#!/usr/bin/env python3
import json
import sys
import urllib.request


base_url = sys.argv[1] if len(sys.argv) > 1 else "http://127.0.0.1:9000"
streaming = "--stream" in sys.argv
parameters = {
    "type": "object",
    "properties": {"text": {"type": "string"}},
    "required": ["text"],
}
cases = [
    (
        "chat",
        "/v1/chat/completions",
        {
            "model": "deepseek-v4-flash",
            "messages": [
                {
                    "role": "user",
                    "content": "Call echo with text hi. Do not answer normally.",
                }
            ],
            "tools": [
                {
                    "type": "function",
                    "function": {
                        "name": "echo",
                        "description": "Echo text",
                        "parameters": parameters,
                    },
                }
            ],
            "reasoning_effort": "none",
            "temperature": 0,
            "max_tokens": 128,
        },
    ),
    (
        "anthropic",
        "/v1/messages",
        {
            "model": "deepseek-v4-flash",
            "messages": [
                {
                    "role": "user",
                    "content": "Call echo with text hi. Do not answer normally.",
                }
            ],
            "tools": [
                {
                    "name": "echo",
                    "description": "Echo text",
                    "input_schema": parameters,
                }
            ],
            "thinking": {"type": "disabled"},
            "temperature": 0,
            "max_tokens": 128,
        },
    ),
    (
        "responses",
        "/v1/responses",
        {
            "model": "deepseek-v4-flash",
            "input": "Call echo with text hi. Do not answer normally.",
            "tools": [
                {
                    "type": "function",
                    "name": "echo",
                    "description": "Echo text",
                    "parameters": parameters,
                }
            ],
            "reasoning": {"effort": "none"},
            "temperature": 0,
            "max_output_tokens": 128,
        },
    ),
]

for name, path, payload in cases:
    if streaming:
        payload["stream"] = True
        if name == "chat":
            payload["stream_options"] = {"include_usage": True}
    request = urllib.request.Request(
        base_url + path,
        data=json.dumps(payload).encode(),
        headers={"Content-Type": "application/json"},
    )
    with urllib.request.urlopen(request, timeout=120) as response:
        if streaming:
            for line in response:
                if line.strip():
                    print(name, line.decode().rstrip())
        else:
            print(name, json.dumps(json.load(response), separators=(",", ":")))
