Skip to content

附录: 逆向工程中使用的工具命令与脚本附录

本文档汇总了针对 /chat SSE 协议进行运行时验证所使用的命令和脚本。 日期: 2026-06-25


1. Daemon 运行时测试

1.1 提取认证 Token

bash
# 方法 1: python3
TOKEN=$(python3 -c "
with open('/home/cc11001100/.atomcode/auth.toml') as f:
    for line in f:
        if 'access_token' in line:
            print(line.split('=')[1].strip().strip('\"'))
            break
")
echo $TOKEN

# 方法 2: grep + cut
TOKEN=$(grep access_token ~/.atomcode/auth.toml | cut -d'"' -f2)

1.2 启动 Daemon

bash
# 不带遥测启动
ATOMCODE_TELEMETRY=0 atomcode daemon &

# 等待启动
sleep 3

# 验证健康状态
curl -s http://localhost:13456/health | jq .

1.3 基础聊天测试

bash
# 简单文本回复(SSE 流)
curl -s -N -X POST http://localhost:13456/chat \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"message":"回复一句话:Hello World"}' \
  --max-time 30

1.4 多轮对话测试

bash
# Turn 1: 设置上下文
RESULT=$(curl -s -N -X POST http://localhost:13456/chat \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"message":"我的名字是张三"}' --max-time 30)

# 提取 session_id
SID=$(echo "$RESULT" | grep '"type":"done"' | \
  python3 -c "import sys,json; print(json.loads(sys.stdin.read().split('data:')[1])['session_id'])")

# Turn 2: 验证上下文
curl -s -N -X POST http://localhost:13456/chat \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d "{\"message\":\"我叫什么名字?\",\"session_id\":\"$SID\"}" --max-time 30

1.5 工具调用测试

bash
# 读文件
curl -s -N -X POST http://localhost:13456/chat \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"message":"列出当前目录下所有.md文件"}' --max-time 60

# 写文件
curl -s -N -X POST http://localhost:13456/chat \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"message":"创建文件 test.md,内容为hello"}' --max-time 60

1.6 认证错误测试

bash
# 无 token
curl -s -X POST http://localhost:13456/chat \
  -H "Content-Type: application/json" \
  -d '{"message":"hi"}'

# 无效请求体
curl -s -X POST http://localhost:13456/chat \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d 'invalid json'

2. SSE 事件解析脚本

2.1 Python 解析器

python
import httpx, json

def chat(messages, session_id=None):
    """调用 daemon /chat 并解析 SSE 事件"""
    body = {"message": messages[-1]["content"]}
    if session_id:
        body["session_id"] = session_id
    
    r = httpx.post('http://localhost:13456/chat',
        headers={'Authorization': f'Bearer {TOKEN}'},
        json=body,
        timeout=60)
    
    events = []
    for line in r.text.split('\n'):
        if line.startswith('data: '):
            events.append(json.loads(line[6:]))
    
    # 提取 session_id
    done = [e for e in events if e['type'] == 'done']
    return events, done[0]['session_id'] if done else None

# 使用示例
events, sid = chat([{"role": "user", "content": "你好"}])
print(f"Session: {sid}")
for e in events:
    if e['type'] == 'text':
        print(f"Text: {e['content']}")
    elif e['type'] == 'tool_start':
        print(f"Tool: {e['name']}({e['arguments']})")

2.2 Bash 解析器

bash
# 逐行解析 SSE 流
curl -s -N -X POST http://localhost:13456/chat \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"message":"hello"}' --max-time 30 | \
while IFS= read -r line; do
    if [[ "$line" == data:* ]]; then
        json="${line#data: }"
        type=$(echo "$json" | python3 -c "import sys,json; print(json.load(sys.stdin).get('type',''))")
        case "$type" in
            text) 
                content=$(echo "$json" | python3 -c "import sys,json; print(json.load(sys.stdin).get('content',''))")
                echo "[TEXT] $content"
                ;;
            tool_start)
                name=$(echo "$json" | python3 -c "import sys,json; print(json.load(sys.stdin).get('name',''))")
                echo "[TOOL] $name"
                ;;
            done)
                echo "[DONE]"
                ;;
            tokens)
                total=$(echo "$json" | python3 -c "import sys,json; print(json.load(sys.stdin).get('total',0))")
                echo "[TOKENS] $total"
                ;;
        esac
    fi
done

3. 验证结果汇总

测试项目结果文档
基础文本回复✅ SSE 流式 text 事件14-RUNTIME_GAPS/04-streaming-vs-nonstreaming.md
多轮对话上下文✅ session_id 正确保留14-RUNTIME_GAPS/02-multi-turn-sessions.md
工具调用✅ tool_start → tool_result 序列14-RUNTIME_GAPS/01-tool-calling-loop.md
工具权限⚠️ 无 permission_request,自动执行14-RUNTIME_GAPS/01-tool-calling-loop.md
非流式支持❌ 不支持,始终 SSE14-RUNTIME_GAPS/04-streaming-vs-nonstreaming.md
用户 session_id❌ 被忽略,daemon 分配新 UUID14-RUNTIME_GAPS/02-multi-turn-sessions.md
认证错误✅ 401 JSON14-RUNTIME_GAPS/03-error-quota-handling.md
版本v4.25.5 (比静态分析版本更新)新发现

📅 最后更新: 2026-06-25

基于 VitePress 构建 · AtomCode v4.25.3 逆向工程分析文档