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 = client.beta.files.upload(file=Path("data.csv"))
print(f"File ID: {file.id}")在建立工作階段時,將已上傳的檔案新增至 resources 陣列,即可將其掛載到沙箱中:
mount_path 是選填的,但請確保上傳的檔案具有描述性的名稱,以便代理程式能夠識別它。
session = client.beta.sessions.create(
agent=agent.id,
environment_id=environment.id,
resources=[
{
"type": "file",
"file_id": file.id,
"mount_path": "/workspace/data.csv",
},
],
)系統會建立一個新的 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 = client.beta.sessions.resources.add(
session.id,
type="file",
file_id=file.id,
)
print(resource.id) # "sesrsc_01ABC..."使用 resources.list 列出工作階段中的所有資源。若要移除檔案,請使用資源 ID 呼叫 resources.delete:
listed = client.beta.sessions.resources.list(session.id)
for entry in listed.data:
print(entry.id, entry.type)
client.beta.sessions.resources.delete(resource.id, session_id=session.id)使用 Files API 列出限定於某個工作階段的檔案並下載它們。
# 列出與工作階段相關聯的檔案
files = client.beta.files.list(
scope_id="sesn_abc123",
betas=["managed-agents-2026-04-01"],
)
for f in files:
print(f.id, f.filename)
# 下載檔案
content = client.beta.files.download(files.data[0].id)
content.write_to_file("output.txt")代理程式可以處理任何檔案類型,包括:
.py、.js、.ts、.go、.rs 等).csv、.json、.xml、.yaml).txt、.md).zip、.tar.gz)— 代理程式可以使用 bash 解壓縮這些檔案掛載在沙箱中的檔案是唯讀副本。代理程式可以讀取它們,但無法修改原始上傳的檔案。若要處理修改後的版本,代理程式會寫入沙箱內的新路徑。
/ 開頭)Was this page helpful?