Vercel News

Vercel Sandbox commands now support stdin

8.5内容质量

TL;DR · AI 摘要

Vercel Sandbox 现支持通过 stdin 向运行命令发送输入,提升与 CLI 工具的交互能力。

核心要点

  • TypeScript SDK 通过 stdin: true 和 detached: true 实现输入流控制
  • Python SDK 使用 subprocess.PIPE 并配合 send() 方法实现数据传输
  • 新特性适用于需要实时输入输出交互的自动化测试场景

结构提纲

按章节快速跳转。

  1. 介绍 Vercel Sandbox 新增 stdin 支持的核心价值

  2. 详细说明通过 runCommand() 和 writeStdin() 的使用方法

  3. 展示 subprocess.PIPE 和 send() 方法的具体应用

  4. 解释如何通过 readable stream 实现输入输出同步

  5. 适用于需要实时交互的自动化测试和数据处理场景

  6. 提供官方文档链接和进一步学习资源

思维导图

用一张图看清主题之间的关系。

查看大纲文本(无障碍 / 无 JS 友好)
  • Vercel Sandbox stdin 支持
    • TypeScript 实现
      • runCommand() 配置
      • writeStdin() 方法
    • Python 实现
      • subprocess.PIPE
      • send() 方法
    • 应用场景
      • 自动化测试
      • 实时数据处理

金句 / Highlights

值得收藏与分享的关键句。

#Vercel#Sandbox#stdin#TypeScript#Python
打开原文

Vercel Sandbox 命令现在支持 stdin - Vercel

Vercel Sandbox 现在允许通过标准输入 (stdin) 向正在运行的命令发送输入。可以用于与 agent-browser 等 stdio MCP 服务器交换消息,或在命令行程序运行时向其发送数据。

在 TypeScript SDK 中,通过在 runCommand() 中设置 stdin: true 和 detached: true。使用 writeStdin() 发送输入并在完成时调用 closeStdin():

code
1
import
{
Sandbox
}
from
'@vercel/sandbox'
;
2
3
const
sandbox
=
await
Sandbox
.
create
(
)
;
4
const
cmd
=
await
sandbox
.
runCommand
(
{
5
cmd
:
'cat'
,
6
stdin
:
true
,
7
detached
:
true
,
8
}
)
;
9
10
await
cmd
.
writeStdin
(
'hello'
)
;
11
await
cmd
.
closeStdin
(
)
;
12
13
const
result
=
await
cmd
.
wait
(
)
;
14
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、收集剩余输出并等待进程退出:

code
1
import
subprocess
2
3
from
vercel
import
sandbox
4
from
vercel
.
api
import
session
5
6
async
with
session
(
)
:
7
async
with
sandbox
.
create_sandbox
(
)
as
instance
:
8
server
=
await
instance
.
create_process
(
"cat"
,
stdin
=
subprocess
.
PIPE
)
9
await
server
.
stdin
.
send
(
b"ping\n"
)
10
print
(
await
server
.
stdout
.
readline
(
)
)
11
print
(
await
server
.
communicate
(
)
)

使用 Sandbox Python SDK 向正在运行的进程的 stdin 写入数据并读取其输出。

对于一次性接收所有输入的命令,可以直接将输入传递给 Python 的 run_process()。

阅读 Sandbox 文档以开始使用。

贡献者

Amelia Charles