《Cursor文檔》-AI Code Tracking API

AI Code Tracking API 可讓你跟蹤團隊各代碼倉庫中 AI 生成的代碼貢獻,包括每次提交的 AI 用量以及細粒度的已接受 AI 更改。

  • AI Code Tracking API 使用 Basic 認證,以你的 API 密鑰作爲用戶名,與 Admin API 的認證方式相同。
  • 有關創建 API 密鑰、身份驗證方法、速率限制和最佳實踐的詳細信息,請參閱 API Overview
  • 可用性:僅限企業版;如需獲取訪問權限,請聯繫銷售
  • 狀態:Alpha (響應結構和字段可能會更改)
  • 工作區限制:僅計算工作區根目錄頂層 Git 代碼倉庫的指標。目前不支持多根工作區。

端點

獲取 AI 提交指標 (JSON,分頁)

/analytics/ai-code/commits

獲取按提交聚合的指標,用於將代碼行歸因於 TAB、COMPOSER 和非 AI。

參數

startDate string | date

ISO 日期字符串、字面量 “now”,或 “7d” 等相對天數 (表示當前時間減 7 天) 。默認值:now - 7 days

endDate string | date

ISO 日期字符串、字面量 “now”,或 “0d” 等相對天數。默認值:now

page number

頁碼 (從 1 開始) 。默認值:1

pageSize number

每頁結果數。默認值:100,最大值:1000

user string

可選:按單個用戶篩選。接受電子郵件 (例如 developer@company.com) 、編碼 ID (例如 user_abc123…) 或數字 ID (例如 42)

響應字段

字段 類型 描述
commitHash string Git 提交哈希
userId string 編碼用戶 ID (例如 user_abc123)
userEmail string 用戶電子郵件地址
repoName string null 代碼倉庫名稱
branchName string null 分支名稱
isPrimaryBranch boolean null 是否爲主分支
commitSource “ide” “cli” “cloud” 提交來源
totalLinesAdded number 提交中新增的總行數
totalLinesDeleted number 提交中刪除的總行數
tabLinesAdded number 通過 TAB 補全新增的行
tabLinesDeleted number 通過 TAB 補全刪除的行
composerLinesAdded number 通過 Composer 新增的行
composerLinesDeleted number 通過 Composer 刪除的行
nonAiLinesAdded number null 新增的非 AI 代碼行
nonAiLinesDeleted number null 刪除的非 AI 代碼行
message string null 提交說明
commitTs string null 提交時間戳 (ISO 格式)
createdAt string 數據攝取時間戳 (ISO 格式)
curl -X GET "https://api.cursor.com/analytics/ai-code/commits?startDate=7d&endDate=now&page=1&pageSize=100" \
  -u YOUR_API_KEY:

響應:

{
  "items": [
    {
      "commitHash": "a1b2c3d4",
      "userId": "user_3k9x8q...",
      "userEmail": "developer@company.com",
      "repoName": "company/repo",
      "branchName": "main",
      "isPrimaryBranch": true,
      "commitSource": "ide",
      "totalLinesAdded": 120,
      "totalLinesDeleted": 30,
      "tabLinesAdded": 50,
      "tabLinesDeleted": 10,
      "composerLinesAdded": 40,
      "composerLinesDeleted": 5,
      "nonAiLinesAdded": 30,
      "nonAiLinesDeleted": 15,
      "message": "Refactor: extract analytics client",
      "commitTs": "2025-07-30T14:12:03.000Z",
      "createdAt": "2025-07-30T14:12:30.000Z"
    }
  ],
  "totalCount": 42,
  "page": 1,
  "pageSize": 100
}

下載 AI 提交指標 (CSV,流式傳輸)

/analytics/ai-code/commits.csv

以 CSV 格式下載提交指標數據,適用於大規模數據提取。

參數

startDate string | date

ISO 日期字符串、字面量 “now”,或 “7d” 等相對天數 (表示當前時間減 7 天) 。默認值:now - 7 days

endDate string | date

ISO 日期字符串、字面量 “now”,或 “0d” 等相對天數。默認值:now

user string

可選:按單個用戶篩選。接受電子郵件 (例如 developer@company.com) 、編碼 ID (例如 user_abc123…) 或數字 ID (例如 42)

響應頭

  • Content-Type: text/csv; charset=utf-8

CSV 列

類型 描述
commit_hash string Git 提交哈希
user_id string 編碼用戶 ID
user_email string 用戶電子郵件地址
repo_name string 代碼倉庫名稱
branch_name string 分支名稱
is_primary_branch boolean 是否爲主分支
commit_source string 提交來源 (ideclicloud)
total_lines_added number 提交中新增的總行數
total_lines_deleted number 提交中刪除的總行數
tab_lines_added number 通過 Tab 補全新增的行數
tab_lines_deleted number 通過 Tab 補全刪除的行數
composer_lines_added number 通過 Composer 新增的行數
composer_lines_deleted number 通過 Composer 刪除的行數
non_ai_lines_added number 新增的非 AI 代碼行數
non_ai_lines_deleted number 刪除的非 AI 代碼行數
message string 提交信息
commit_ts string 提交時間戳 (ISO 格式)
created_at string 數據攝取時間戳 (ISO 格式)
curl -L "https://api.cursor.com/analytics/ai-code/commits.csv?startDate=2025-07-01T00:00:00Z&endDate=now&user=user_3k9x8q..." \
  -u YOUR_API_KEY: \
  -o commits.csv

CSV 輸出示例:

commit_hash,commit_source,user_id,user_email,repo_name,branch_name,is_primary_branch,total_lines_added,total_lines_deleted,tab_lines_added,tab_lines_deleted,composer_lines_added,composer_lines_deleted,non_ai_lines_added,non_ai_lines_deleted,message,commit_ts,created_at
a1b2c3d4,ide,user_3k9x8q...,developer@company.com,company/repo,main,true,120,30,50,10,40,5,30,15,"Refactor: extract analytics client",2025-07-30T14:12:03.000Z,2025-07-30T14:12:30.000Z
e5f6g7h8,cloud,user_3k9x8q...,developer@company.com,company/repo,feature-branch,false,85,15,30,5,25,3,30,7,"Add error handling",2025-07-30T13:45:21.000Z,2025-07-30T13:45:45.000Z

獲取 AI 代碼更改指標 (JSON,分頁)

/analytics/ai-code/changes

獲取按確定性 changeId 分組的細粒度已接受 AI 更改。可用於獨立於提交分析已接受的 AI 事件。

參數

startDate string | date

ISO 日期字符串、字面量“now”或“7d”等相對天數 (表示當前時間減 7 天) 。默認值:當前時間減 7 天

endDate string | date

ISO 日期字符串、字面量“now”或“0d”等相對天數。默認值:now

page number

頁碼 (從 1 開始) 。默認值:1

pageSize number

每頁結果數。默認值:100,最大值:1000

user string

可選按單個用戶篩選。接受電子郵件 (例如 developer@company.com) 、編碼 ID (例如 user_abc123…) 或數字 ID (例如 42)

響應字段

字段 類型 描述
changeId string 更改的確定性 ID
userId string 編碼的用戶 ID (例如 user_abc123)
userEmail string 用戶的電子郵件地址
source “TAB” “COMPOSER” AI 更改的來源
model string null 使用的 AI 模型
totalLinesAdded number 新增的總行數
totalLinesDeleted number 刪除的總行數
createdAt string 數據攝取時間戳 (ISO 格式)
metadata Array 文件元數據 (隱私模式下可能省略 fileName)
curl -X GET "https://api.cursor.com/analytics/ai-code/changes?startDate=14d&endDate=now&page=1&pageSize=200" \
  -u YOUR_API_KEY:

響應:

{
  "items": [
    {
      "changeId": "749356201",
      "userId": "user_3k9x8q...",
      "userEmail": "developer@company.com",
      "source": "COMPOSER",
      "model": null,
      "totalLinesAdded": 18,
      "totalLinesDeleted": 4,
      "createdAt": "2025-07-30T15:10:12.000Z",
      "metadata": [
        {
          "fileName": "src/analytics/report.ts",
          "fileExtension": "ts",
          "linesAdded": 12,
          "linesDeleted": 3
        },
        {
          "fileName": "src/analytics/ui.tsx",
          "fileExtension": "tsx",
          "linesAdded": 6,
          "linesDeleted": 1
        }
      ]
    }
  ],
  "totalCount": 128,
  "page": 1,
  "pageSize": 200
}

下載 AI 代碼更改指標 (CSV,流式)

/analytics/ai-code/changes.csv

以 CSV 格式下載更改指標數據,適用於大規模數據提取。

參數

startDate string | date

ISO 日期字符串、字面值“now”或“7d”等相對天數 (表示當前時間減 7 天) 。默認值:當前時間減 7 天

endDate string | date

ISO 日期字符串、字面值“now”或“0d”等相對天數。默認值:now

user string

可選:按單個用戶篩選。支持電子郵件 (例如 developer@company.com) 、編碼 ID (例如 user_abc123…) 或數字 ID (例如 42)

響應頭

  • Content-Type: text/csv; charset=utf-8

CSV 列

類型 描述
change_id string 更改的確定性 ID
user_id string 編碼的用戶 ID
user_email string 用戶的電子郵件地址
source string AI 更改的來源 (TAB 或 COMPOSER)
model string 所用的 AI 模型
total_lines_added number 新增的總行數
total_lines_deleted number 刪除的總行數
created_at string 數據攝取時間戳 (ISO 格式)
metadata_json string 元數據條目組成的 JSON 字符串化數組
curl -L "https://api.cursor.com/analytics/ai-code/changes.csv?startDate=30d&endDate=now" \
  -u YOUR_API_KEY: \
  -o changes.csv

CSV 輸出示例:

change_id,user_id,user_email,source,model,total_lines_added,total_lines_deleted,created_at,metadata_json
749356201,user_3k9x8q...,developer@company.com,COMPOSER,gpt-4o,18,4,2025-07-30T15:10:12.000Z,"[{""fileName"":""src/analytics/report.ts"",""fileExtension"":""ts"",""linesAdded"":12,""linesDeleted"":3},{""fileName"":""src/analytics/ui.tsx"",""fileExtension"":""tsx"",""linesAdded"":6,""linesDeleted"":1}]"
749356202,user_3k9x8q...,developer@company.com,TAB,,8,2,2025-07-30T15:08:45.000Z,"[{""fileName"":""src/utils/helpers.ts"",""fileExtension"":""ts"",""linesAdded"":8,""linesDeleted"":2}]"

獲取提交詳情

/analytics/ai-code/commits/:commitHash

獲取一個或多個提交的詳細信息,包括 blame 標註和所引用對話的元數據。

此端點處於有限 Alpha 階段,僅對部分用戶開放。響應結構可能會變更。

路徑參數

commitHash string

單個提交哈希,或以逗號分隔的哈希列表 (例如 abc123,def456)

查詢參數

branch string

可選,按分支名稱篩選

響應字段

返回一個包含 commitsconversations 數組的對象。

字段 類型 描述
commits array 包含 blame 標註的提交對象數組
commits[].commitSource “ide” “cli” “cloud” 提交的來源。
commits[].rangeAnnotations array 該提交的文件級 blame 數據
commits[].rangeAnnotations[].filePath string 代碼倉庫中文件的路徑
commits[].rangeAnnotations[].groups array 標註羣組數組
commits[].rangeAnnotations[].groups[].conversationId string null 生成此代碼的對話 ID
commits[].rangeAnnotations[].groups[].model string null 用於生成代碼的 AI 模型
commits[].rangeAnnotations[].groups[].operationType string 執行的操作類型
commits[].rangeAnnotations[].groups[].ranges array 此標註影響的行範圍數組
commits[].rangeAnnotations[].groups[].ranges[].start number 起始行號
commits[].rangeAnnotations[].groups[].ranges[].end number 結束行號
conversations array 所有被引用對話的元數據
conversations[].id string 唯一對話標識符
conversations[].title string null 對話標題
conversations[].tldr string null 簡短摘要
conversations[].overview string null 詳細概述
conversations[].summaryBullets array null 摘要要點數組

即使請求單個提交,響應格式也保持一致。

單次提交:

curl -X GET "https://api.cursor.com/analytics/ai-code/commits/0aabf603dc906e05bf5e4d9fd423fdd517f2e43f?branch=main" \
  -u YOUR_API_KEY:

多個提交:

curl -X GET "https://api.cursor.com/analytics/ai-code/commits/abc123,def456,ghi789" \
  -u YOUR_API_KEY:

響應:

{
  "commits": [
    {
      "commitHash": "0aabf603dc906e05bf5e4d9fd423fdd517f2e43f",
      "commitSource": "ide",
      "rangeAnnotations": [
        {
          "filePath": "src/analytics/report.ts",
          "groups": [
            {
              "conversationId": "conv_abc123",
              "model": "gpt-4o",
              "operationType": "insert",
              "ranges": [
                { "start": 10, "end": 25 },
                { "start": 42, "end": 58 }
              ]
            }
          ]
        }
      ]
    }
  ],
  "conversations": [
    {
      "id": "conv_abc123",
      "title": "Refactor analytics module",
      "tldr": "Extracted report generation into separate functions",
      "overview": "Refactored the analytics module to improve maintainability by extracting report generation logic.",
      "summaryBullets": [
        "Created dedicated report generator class",
        "Added unit tests for new functions",
        "Updated imports across affected files"
      ]
    }
  ]
}

通用查詢參數

所有端點均通過查詢字符串接受相同的查詢參數:

參數 類型 必填 描述
startDate string date ISO 日期 string、字面量“now”或“7d”等相對天數 (表示當前時間 - 7 天) 。默認值:當前時間 - 7 天
endDate string date ISO 日期 string、字面量“now”或“0d”等相對天數。默認值:當前時間
page number 頁碼 (從 1 開始) 。默認值:1
pageSize number 每頁結果數。默認值:100,最大值:1000
user string 可選按單個用戶篩選。接受電子郵件 (例如 developer@company.com) 、編碼 ID (例如 user_abc123…) 或數字 ID (例如 42)

響應中的 userId 爲帶有 user_ 前綴的編碼外部 ID。該 ID
可穩定用於 API 調用。

語義與指標計算方式

  • 來源:“TAB”表示已接受的內聯補全;“COMPOSER”表示已接受的智能體編輯 diff
  • 行數指標:tabLinesAdded/Deleted 和 composerLinesAdded/Deleted 分別計數;nonAiLinesAdded/Deleted 計算爲 max(0, totalLines - AI 行數)
  • 隱私模式:如果在客戶端中啓用,部分元數據 (如 fileName) 可能會被省略
  • 分支信息:當前分支等於倉庫默認分支時,isPrimaryBranch 爲 true;如果倉庫信息不可用,該值可能爲 undefined

你可以掃描該文件,瞭解如何檢測和報告提交與更改。

提示

  • 使用 user 參數可快速在所有端點中篩選特定用戶
  • 對於大規模數據提取,建議使用 CSV 端點——服務器會以每頁 10,000 條記錄的形式流式傳輸數據
  • 如果客戶端無法解析默認分支,isPrimaryBranch 可能爲未定義
  • commitTs 是提交時間戳;createdAt 是數據在我們服務器上的接收時間
  • 客戶端啓用隱私模式時,某些字段可能缺失
  • 提交哈希並非唯一且不可變更。例如,如果你通過添加額外信息修訂提交,可能會看到同一提交出現兩次。
  • 即使提交被修訂,提交時間戳也不會改變。

更新日誌

  • Alpha 版本:提供提交和更改的初始端點。響應結構可能會根據反饋調整

AI Code Tracking 適用於企業版方案

聯繫我們的團隊,獲取詳細的 AI 用量指標。

羽毛球分组比赛记分
小程序二维码

欢迎使用《羽毛球分组比赛记分》微信小程序

小夜