Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| beb4fb6a76 |
@@ -64,12 +64,35 @@ class OpenClawClient:
|
|||||||
raise Exception(f"CLI error: {result.stderr}")
|
raise Exception(f"CLI error: {result.stderr}")
|
||||||
|
|
||||||
# 解析 JSON 输出(跳过警告)
|
# 解析 JSON 输出(跳过警告)
|
||||||
lines = result.stdout.strip().split("\n")
|
# OpenClaw 输出格式化的多行 JSON,需要提取完整的 JSON 对象
|
||||||
json_lines = [l for l in lines if l.startswith("{") or l.startswith("[")]
|
output = result.stdout.strip()
|
||||||
|
|
||||||
|
# 找到 JSON 开始位置
|
||||||
|
json_start = -1
|
||||||
|
for i, char in enumerate(output):
|
||||||
|
if char == '{' or char == '[':
|
||||||
|
json_start = i
|
||||||
|
break
|
||||||
|
|
||||||
|
if json_start == -1:
|
||||||
|
return {}
|
||||||
|
|
||||||
|
# 提取 JSON 部分
|
||||||
|
json_str = output[json_start:]
|
||||||
|
|
||||||
|
try:
|
||||||
|
return json.loads(json_str)
|
||||||
|
except json.JSONDecodeError:
|
||||||
|
# 如果失败,尝试其他方法
|
||||||
|
lines = output.split("\n")
|
||||||
|
json_lines = [l for l in lines if l.strip().startswith("{") or l.strip().startswith("[")]
|
||||||
if json_lines:
|
if json_lines:
|
||||||
return json.loads(json_lines[-1])
|
# 尝试解析每一行
|
||||||
|
for line in json_lines:
|
||||||
|
try:
|
||||||
|
return json.loads(line)
|
||||||
|
except:
|
||||||
|
continue
|
||||||
return {}
|
return {}
|
||||||
except subprocess.TimeoutExpired:
|
except subprocess.TimeoutExpired:
|
||||||
raise Exception(f"Timeout running: {cmd}")
|
raise Exception(f"Timeout running: {cmd}")
|
||||||
|
|||||||
Reference in New Issue
Block a user