Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
您可以透過 Files API 上傳檔案並將其掛載至工作階段的容器中,藉此提供檔案給您的代理程式。
所有 Managed Agents API 請求都需要 managed-agents-2026-04-01 beta 標頭。SDK 會自動設定 beta 標頭。
首先,使用 Files API 上傳檔案:
file=$(curl --fail-with-body -sS "${auth[@]}" \
"${base_url}/files" \
-F [email protected])
file_id=$(jq -er '.id' <<<"${file}")
printf 'File ID: %s\n' "${file_id}"建立工作階段時,將已上傳的檔案新增至 resources 陣列,即可將其掛載至容器中:
mount_path 為選填項目,但請確保上傳的檔案具有描述性名稱,以便代理程式知道它在尋找什麼。
session=$(
jq -n \
--arg agent_id "${agent_id}" \
--arg environment_id "${environment_id}" \
--arg file_id "${file_id}" \
'{
agent: $agent_id,
environment_id: $environment_id,
resources: [
{
type: "file",
file_id: $file_id,
mount_path: "/workspace/data.csv"
}
]
}' | curl --fail-with-body -sS "${auth[@]}" "${base_url}/sessions" --json @-
)
session_id=$(jq -er '.id' <<<"${session}")系統將建立一個新的 file_id,用於參照工作階段中的檔案實例。這些副本不會計入您的儲存空間限制。
透過在 resources 陣列中新增項目來掛載多個檔案:
"resources": [
{ "type": "file", "file_id": "file_abc123", "mount_path": "/workspace/data.csv" },
{ "type": "file", "file_id": "file_def456", "mount_path": "/workspace/config.json" },
{ "type": "file", "file_id": "file_ghi789", "mount_path": "/workspace/src/main.py" }
]每個工作階段最多支援 100 個檔案。
您可以在建立工作階段後,使用工作階段資源 API 新增或移除檔案。每個資源在新增(或列出)時都會傳回一個 id,您可使用此 id 進行刪除操作。
resource=$(
jq -n --arg file_id "${file_id}" '{type: "file", file_id: $file_id}' \
| curl --fail-with-body -sS "${auth[@]}" \
"${base_url}/sessions/${session_id}/resources" --json @-
)
resource_id=$(jq -er '.id' <<<"${resource}")
printf '%s\n' "${resource_id}" # "sesrsc_01ABC..."使用 resources.list 列出工作階段上的所有資源。若要移除檔案,請使用資源 ID 呼叫 resources.delete:
curl --fail-with-body -sS "${auth[@]}" \
"${base_url}/sessions/${session_id}/resources" \
| jq -r '.data[] | "\(.id) \(.type)"'
curl --fail-with-body -sS "${auth[@]}" -X DELETE \
"${base_url}/sessions/${session_id}/resources/${resource_id}" >/dev/null使用 Files API 列出工作階段範圍內的檔案並下載它們。
# 列出與工作階段相關聯的檔案
curl -fsSL "https://api.anthropic.com/v1/files?scope_id=sess_abc123" \
-H "x-api-key: $ANTHROPIC_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-H "anthropic-beta: files-api-2025-04-14"
# 下載檔案
curl -fsSL "https://api.anthropic.com/v1/files/$FILE_ID/content" \
-H "x-api-key: $ANTHROPIC_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-H "anthropic-beta: files-api-2025-04-14" \
-o output.txt代理程式可以處理任何檔案類型,包括:
.py、.js、.ts、.go、.rs 等).csv、.json、.xml、.yaml).txt、.md).zip、.tar.gz)— 代理程式可使用 bash 解壓縮這些檔案掛載至容器中的檔案為唯讀副本。代理程式可以讀取這些檔案,但無法修改原始上傳的檔案。若要使用修改後的版本,代理程式會在容器內的新路徑中寫入。
/ 開頭)Was this page helpful?