#!/usr/bin/env python3 """ qq-agent → Nix 求助工具(同步 Gateway API 调用) 直接发问题到 Gateway,等 Nix 回复,不需要文件桥接。 模仿 openclaw_bridge 插件的 `_send_to_openclaw` 实现。 用法: python3 ask_nix.py "问题内容" 返回: Nix 的回复,或超时错误 """ import sys import json import requests GATEWAY_URL = "http://127.0.0.1:18789/v1/chat/completions" GATEWAY_TOKEN = "YOUR_GATEWAY_TOKEN" NIX_MODEL = "openclaw/ops-manager" def ask(question: str, timeout: int = 120) -> str: """同步调用 Gateway,等 Nix 回复""" payload = { "model": NIX_MODEL, "messages": [{"role": "user", "content": question}], "stream": False, } headers = { "Content-Type": "application/json", "Authorization": f"Bearer {GATEWAY_TOKEN}", } try: resp = requests.post( GATEWAY_URL, json=payload, headers=headers, timeout=timeout, ) if resp.status_code == 200: data = resp.json() choices = data.get("choices", []) if choices: return choices[0].get("message", {}).get("content", "") return "Nix 返回为空" return f"请求失败({resp.status_code}): {resp.text[:200]}" except requests.exceptions.Timeout: return "[timeout] Nix 没回 超时了 要么他不在线 要么忙着呢 老板稍后再试吧" except requests.exceptions.ConnectionError: return "Nix 连接失败,请检查 Gateway 服务状态" except Exception as e: return f"请求异常: {str(e)}" if __name__ == "__main__": question = sys.argv[1] if len(sys.argv) > 1 else sys.stdin.read().strip() if not question: print("错误:未提供问题", file=sys.stderr) sys.exit(1) print(ask(question))