# Cursor CLI stream-json 格式

## 實際輸出格式（已確認）

NDJSON（每行一個 JSON）。

### 1. System Init
```json
{"type":"system","subtype":"init","apiKeySource":"login","cwd":"/path","session_id":"uuid","model":"Auto","permissionMode":"default"}
```

### 2. User Message
```json
{"type":"user","message":{"role":"user","content":[{"type":"text","text":"prompt text"}]},"session_id":"uuid"}
```

### 3. Assistant Message（可能多次出現）
```json
{"type":"assistant","message":{"role":"assistant","content":[{"type":"text","text":"response text"}]},"session_id":"uuid","timestamp_ms":1776157308323}
```

### 4. Result（最後一行）
```json
{"type":"result","subtype":"success","duration_ms":10208,"duration_api_ms":10208,"is_error":false,"result":"OK","session_id":"uuid","request_id":"uuid","usage":{"inputTokens":0,"outputTokens":122,"cacheReadTokens":5120,"cacheWriteTokens":14063}}
```

## 轉換規則

| Cursor type | 行為 |
|-------------|------|
| system | 忽略（初始化訊息）|
| user | 忽略（echo 回用戶訊息）|
| assistant | 提取 message.content[].text → OpenAI delta.content |
| result (success) | 提取 usage → OpenAI usage，發送 finish_reason:"stop" |
| result (error) | 發送錯誤 chunk |

## CLI 參數

```bash
agent -p "prompt" \
  --output-format stream-json \
  --stream-partial-output \
  --trust \
  --model "model-name"
```

注意：需要 `--trust` 才能在非互動模式執行。
