跳到正文
Vercel Blog(vercel.com)· Kishore Devireddy·· 2 天前精選AI 評分62

Vercel Sandbox 命令現已支援 stdin

Vercel Sandbox commands now support stdin

AI 導讀

Vercel Sandbox 現已支援透過 stdin 與執行中的命令互動。

  • 核心功能:在 TypeScript SDK 內使用 stdin: true。
推薦理由

Vercel Sandbox 透過 stdin 支援互動式命令,開發者可直接在 SDK 中寫入與讀取標準輸入,提升工作流程的靈活性。

正文 · 原文

Vercel Sandbox now lets you send input to running commands through standard input (stdin). Use it to exchange messages with stdio MCP servers such as agent-browser, or send data to other command-line programs while they run.

In the TypeScript SDK, set stdin: true and detached: true in runCommand(). Use writeStdin() to send input and closeStdin() when you’re done:

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"

Send input to a running command and read its output with the Sandbox TypeScript SDK.

You can also set stdin to a Node.js readable stream, such as process.stdin. The SDK forwards the stream’s data and closes the command’s standard input when the stream ends.

In the Python SDK, set stdin=subprocess.PIPE and send input with server.stdin.send(). Call communicate() to close stdin, collect remaining output, and wait for the process to exit:

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())

Write to a running process’s stdin and read its output with the Sandbox Python SDK.

For commands that receive all their input at once, you can pass input directly to Python’s run_process().

Read the Sandbox documentation to get started.

來源: Vercel Blog (vercel.com)
開啟原文