Vercel Sandbox 命令現已支援 stdin
Vercel Sandbox commands now support stdin
Vercel Sandbox 現已支援透過 stdin 與執行中的命令互動。
- 核心功能:在 TypeScript SDK 內使用
stdin: true。
Vercel Sandbox 透過 stdin 支援互動式命令,開發者可直接在 SDK 中寫入與讀取標準輸入,提升工作流程的靈活性。
Vercel Sandbox 現已允許您透過標準輸入(stdin)向正在執行的指令傳送輸入。可用於與 stdio MCP 伺服器(如 agent-browser)交換訊息,或在執行時將資料傳送給其他命令列程式。
在 TypeScript SDK 中,於 runCommand() 設定 stdin: true 與 detached: true。使用 writeStdin() 傳送輸入,並在完成時呼叫 closeStdin():
import { Sandbox } from '@vercel/sandbox';
const sandbox = await Sandbox.create();
const cmd = await sandbox.runCommand({
cmd: 'cat',
stdin: true,
detached: true,
});
await cmd.writeStdin('hello');
await cmd.closeStdin();
const result = await cmd.wait();
console.log(await result.stdout()); // "hello"
使用 Sandbox TypeScript SDK 向正在執行的指令傳送輸入並讀取其輸出。
您亦可將 stdin 設定為 Node.js 可讀串流,例如 process.stdin。SDK 會轉發該串流資料並在串流結束時關閉指令的標準輸入。
在 Python SDK 中,設定 stdin=subprocess.PIPE 並使用 server.stdin.send() 傳送輸入。呼叫 communicate() 以關閉 stdin、收集剩餘輸出,並等待程式結束:
import subprocess
from vercel import sandbox
from vercel.api import session
async with session():
async with sandbox.create_sandbox() as instance:
server = await instance.create_process("cat", stdin=subprocess.PIPE)
await server.stdin.send(b"ping\n")
print(await server.stdout.readline())
print(await server.communicate())
使用 Sandbox Python SDK 向正在執行的程式寫入 stdin 並讀取其輸出。
對於一次性接收所有輸入的指令,您可以直接將 input 傳遞給 Python 的 run_process()。
閱讀 Sandbox 檔案 以開始使用。