#!/usr/bin/env python3 # sim_round2.py — полная симуляция двух раундов как у бота: stream(tools) -> run_code -> stream(result). import asyncio, json, sys sys.path.insert(0, "/opt/grok-bot") async def main(): import iishko.legacy as L model = "deepseek-v4.1-flash-1017" messages = [{"role": "user", "content": "ОБЯЗАТЕЛЬНО вызови run_code: print('SANDBOX_OK', 2**64). Не отвечай по памяти."}] # раунд 1: получаем tool_calls через тот же _gw путь, что и бот acc = {} async def send(txt): return None # прямой вызов стрима бота с перехватом payload = {"model": model, "messages": messages, "max_tokens": 400, "stream": True, **L._thinking_off(model), "tools": L.BOT_TOOLS} if model not in L.TOOL_CHOICE_REJECTED_MODELS: payload["tool_choice"] = "auto" import aiohttp base = L.GW_BASE_TOOLS if payload.get("tools") else L.GW_BASE print("base:", base) async with aiohttp.ClientSession() as s: async with s.post(base + "/chat/completions", json=payload, headers={"Authorization": "Bearer " + L.GW_KEY}, timeout=aiohttp.ClientTimeout(total=90)) as r: print("status:", r.status) async for raw in r.content: line = raw.decode("utf-8", "replace").strip() if not line.startswith("data:"): continue d0 = line[5:].strip() if d0 == "[DONE]": break try: ch = json.loads(d0) except ValueError: continue c0 = (ch.get("choices") or [{}])[0] for tc in ((c0.get("delta") or {}).get("tool_calls") or []): i = tc.get("index", 0) a = acc.setdefault(i, {"id": "", "type": "function", "function": {"name": "", "arguments": ""}}) if tc.get("id"): a["id"] = tc["id"] fn = tc.get("function") or {} if fn.get("name"): a["function"]["name"] = fn["name"] if fn.get("arguments"): a["function"]["arguments"] += fn["arguments"] tcs = list(acc.values()) print("round1 tool_calls:", [(t["function"]["name"], t["function"]["arguments"][:60]) for t in tcs]) if not tcs: print("NO_TOOL_CALLS"); return # исполняем res = await L._bot_tool_exec(tcs[0]["function"]["name"], json.loads(tcs[0]["function"]["arguments"] or "{}")) print("TOOL_RESULT:", res[:300].replace("\n", " | ")) # раунд 2: отдаём результат модели messages2 = messages + [{"role": "assistant", "content": None, "tool_calls": tcs}, {"role": "tool", "tool_call_id": tcs[0].get("id", ""), "content": res}] payload2 = {"model": model, "messages": messages2, "max_tokens": 400, "stream": True, **L._thinking_off(model)} txt = [] async with aiohttp.ClientSession() as s: async with s.post(base + "/chat/completions", json=payload2, headers={"Authorization": "Bearer " + L.GW_KEY}, timeout=aiohttp.ClientTimeout(total=90)) as r: print("round2 status:", r.status) async for raw in r.content: line = raw.decode("utf-8", "replace").strip() if not line.startswith("data:"): continue d0 = line[5:].strip() if d0 == "[DONE]": break try: ch = json.loads(d0) except ValueError: continue c0 = (ch.get("choices") or [{}])[0] t = (c0.get("delta") or {}).get("content") if t: txt.append(t) print("ROUND2_TEXT:", "".join(txt)[:400].replace("\n", " | ")) asyncio.run(main())