Newshot
Account

Workflow SDK 在 Vercel 上改用 WebSocket 写入

AI summary

Workflow SDK 现在默认通过 WebSocket 向 Vercel Workflows 发送事件写入和流写入,取代原先每次写入或每组流写入各发一次 HTTP 请求。用户只需升级到 [email protected] 或更高版本,无需改动代码。基准测试显示,v5.2.0 交付完整 AI 响应的速度比 v4.8.12 快 4.2 倍。

Translation(model translation, not the source's own text)

Workflow SDK 现在默认通过 WebSocket 向 Vercel Workflows 发送事件写入和流写入,取代了此前每次写入(流则是每组写入)都要发一次 HTTP 请求的方式。除了升级到 [email protected] 或更高版本之外,无需改动任何代码。

部署升级后,新的运行会使用它。正在进行中的运行会在其启动时所处的部署上完成。


        npm install workflow@latest
      

现有的流式代码可照常工作。某个步骤如果要写入一批数据块(例如 AI token),会在 WebSocket 打开后通过它发送:


        import { getWritable } from "workflow";
async function streamChunks(chunks: string[]) {  "use step";  const writer = getWritable<string>().getWriter();  for (const chunk of chunks) {    await writer.write(chunk);  }  writer.releaseLock();}
export async function streamingWorkflow(chunks: string[]) {  "use workflow";  await streamChunks(chunks);}
      

流式传输跟得上 AI 输出

在回放某个 AI agent 的 token 流的基准测试中,v5.2.0(默认通过 WebSocket 写入流)交付完整响应的速度比 v4.8.12 快 4.2 倍,且没有任何一个数据块被延迟超过一秒。

事件写入的 HTTP 请求更少

每次步骤的启动和完成都是一次事件写入,现在通过 WebSocket 发送,因此步骤较多的工作流发出的 HTTP 请求更少。提升的效率也改善了性能并降低了成本。队列处理器会为每次投递打开连接。

开始使用

阅读 Vercel World 文档即可开始。如果你使用的是 4.x,请先看升级指南,因为 v5 是一个大版本。

The translation is machine-generated and individual terms may be off. Nothing is rewritten or cut; the original is authoritative.

Published by Vercel Blog
This site only summarises and aggregates; copyright stays with the original author. Headlines and summaries follow the source — where they differ, the original wins.
Read the original ↗