import asyncio, sys, os, copy
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT))
from utils.api_client import DeepSeekClient

class FakeClient(DeepSeekClient):
    def __init__(self):
        super().__init__(thinking_mode=True, web_mode=True)
        self.sent = []
        self.call_idx = 0
    async def chat(self, messages, tools=None, stream=True):
        self.sent.append(copy.deepcopy(messages))
        self.call_idx += 1
        if self.call_idx == 1:
            yield {
                "choices": [
                    {"delta": {
                        "reasoning_content": "think1 ",
                        "tool_calls": [
                            {"id": "call_1", "index": 0, "type": "function", "function": {"name": "foo", "arguments": "{}"}}
                        ]
                    }}
                ]
            }
            yield {"choices": [{"delta": {}}]}
        else:
            yield {"choices": [{"delta": {"content": "done"}}]}
            yield {"choices": [{"delta": {}}]}

async def main():
    client = FakeClient()
    messages = [
        {"role": "system", "content": "sys"},
        {"role": "user", "content": "hi"}
    ]
    async def tool_handler(name, args):
        return '{}'
    out = await client.chat_with_tools(messages, tools=[{"type":"function","function":{"name":"foo","parameters":{"type":"object","properties":{}}}}], tool_handler=tool_handler)
    print('final', out)
    import json
    for i, m in enumerate(client.sent, 1):
        print('\ncall', i)
        print(json.dumps(m, ensure_ascii=False, indent=2))

if __name__ == '__main__':
    asyncio.run(main())
