Spaces:
Um35h
/
Runtime error

File size: 1,930 Bytes
8bf79eb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python3
import subprocess
import json
import sys
import time

def run_command(cmd):
    try:
        result = subprocess.run(
            cmd, 
            shell=True, 
            capture_output=True, 
            text=True, 
            check=True,
            timeout=10 # 增加超时防止卡死
        )
        return result.stdout.strip()
    except subprocess.CalledProcessError as e:
        # 如果是非零退出码,可能是没有 pending 请求,静默处理
        return None
    except subprocess.TimeoutExpired:
        print("--- [Pending] 命令执行超时 ---")
        return None

def main():
    print("--- [Pending]: 检查设备待批准列表...")
    
    json_output = run_command("openclaw devices list --json")
    
    if not json_output:
        # 可能是还没初始化,或者命令本身不需要输出
        sys.exit(0)

    try:
        data = json.loads(json_output)
    except json.JSONDecodeError:
        # 输出不是有效的 JSON,忽略
        sys.exit(0)

    pending_list = data.get("pending", [])
    
    if not isinstance(pending_list, list):
        sys.exit(0)

    request_ids = []
    
    # 逻辑优化:统一遍历逻辑,不再硬编码索引切片,除非数据结构非常特殊
    # 根据原逻辑,似乎是从索引 3 开始或者是字典类型
    for item in pending_list:
        if isinstance(item, dict) and "requestId" in item:
            request_ids.append(item["requestId"])

    if not request_ids:
        sys.exit(0)

    print(f"--- [Pending] 发现 {len(request_ids)} 个待批准请求 ---")
    
    for req_id in request_ids:
        cmd = f"openclaw devices approve {req_id}"
        print(f"--- [Pending] 正在批准: {req_id} ---")
        subprocess.run(cmd, shell=True, capture_output=True, text=True)
        
    print("--- [Pending] 所有请求处理完毕 ---")

if __name__ == "__main__":
    main()