ai-sdk-core-v7
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseSkill: ai-sdk-core
Skill: ai-sdk-core
Scope
适用范围
- Applies to: Backend AI with Vercel AI SDK v7 (package)
ai - Does NOT cover: React chat UIs (see ai-sdk-ui)
- 适用场景:使用Vercel AI SDK v7(包)开发后端AI
ai - 不包含内容:React聊天UI(详见ai-sdk-ui)
Assumptions
前提假设
- Folder major tracks the installed package in
ai, not a skill-file revisionpackage.json - Node.js 22+, ESM; Zod 4 for
inputSchema - Never write AI SDK from memory. Grep and
node_modules/ai/docs/for the installed version. Else https://ai-sdk.dev/docs (appendnode_modules/ai/src/) or https://ai-sdk.dev/api/search-docs?q=.md - v6 leftovers: https://ai-sdk.dev/docs/migration-guides/migration-guide-7-0
- 文件夹版本以中安装的
package.json包版本为准,而非技能文件修订版本ai - Node.js 22+,ESM;使用Zod 4定义
inputSchema - 切勿凭记忆编写AI SDK代码。请在和
node_modules/ai/docs/中搜索已安装版本的相关内容。否则可访问https://ai-sdk.dev/docs(追加`.md`)或https://ai-sdk.dev/api/search-docs?q=node_modules/ai/src/ - v6迁移遗留内容:https://ai-sdk.dev/docs/migration-guides/migration-guide-7-0
Principles
核心原则
- /
generateTextfor request handlers and one-shot API routes;streamTextfor system-style promptinstructions - Request-handler tool loops: on
stopWhen: isStepCount(n)/generateText— notstreamText, notmaxStepsstepCountIs - +
ToolLoopAgent/generate()for durable agents — not hand-rolled loops, notstream()new Agent() - UI streams: +
createUIMessageStreamResponsefromtoUIMessageStream({ stream: result.stream }). Use these for new handlers and for dedicated stream/SDK updates'ai' - still works in v7 (deprecated). Leave it only when the task is unrelated to streaming or the SDK
result.toUIMessageStreamResponse() - Tool results must be JSON-serializable (no )
Date - System prompt lives in . Client
instructionsmust not includemessagesunless the server fully trusts the payload{ role: 'system' }
- 在请求处理器和一次性API路由中使用/
generateText;使用streamText定义系统风格提示词instructions - 请求处理器工具循环:在/
generateText上使用streamText— 不要使用stopWhen: isStepCount(n)或maxStepsstepCountIs - 构建持久化Agent使用+
ToolLoopAgent/generate()— 不要手动编写循环,也不要使用stream()new Agent() - UI流处理:从包中使用
'ai'+createUIMessageStreamResponse。新处理器和专用流/SDK更新均需使用这些方法toUIMessageStream({ stream: result.stream }) - 在v7中仍可使用(已废弃)。仅当任务与流处理或SDK无关时才保留该写法
result.toUIMessageStreamResponse() - 工具返回结果必须可JSON序列化(不能包含类型)
Date - 系统提示词需放在中。客户端
instructions中不得包含messages,除非服务器完全信任该负载{ role: 'system' }
Constraints
约束条件
MUST
必须遵守
- Read bundled docs before writing calls
- when the client sends UI messages
convertToModelMessages - Reject untrusted in
{ role: 'system' }. Do not setmessagesfor client-supplied chatallowSystemInMessages: true
- 编写调用代码前先阅读捆绑文档
- 当客户端发送UI消息时,使用进行转换
convertToModelMessages - 拒绝客户端消息中不可信的。对于客户端提供的聊天内容,请勿设置
{ role: 'system' }allowSystemInMessages: true
AVOID
需避免
- /
toDataStreamResponsepipeDataStreamToResponse - /
stepCountIs/maxSteps/ top-levelresult.fullStream(usesystem:,isStepCount,result.stream)instructions - Guessing model IDs
- 使用/
toDataStreamResponsepipeDataStreamToResponse - 使用/
stepCountIs/maxSteps/ 顶层result.fullStream(请使用system:、isStepCount、result.stream)instructions - 猜测模型ID
Interactions
关联内容
- Complements ai-sdk-ui, fastify, next
- Upstream: vercel/ai use-ai-sdk
- 补充内容:ai-sdk-ui、fastify、next
- 上游文档:vercel/ai use-ai-sdk
Templates
模板
- generate-text-basic.ts
- stream-text-chat.ts
- agent-with-tools.ts
- generate-text-basic.ts
- stream-text-chat.ts
- agent-with-tools.ts
References
参考资料
- Request handlers — tools + , Fastify SSE, system-message reject
isStepCount
- Request handlers — 工具 + 、Fastify SSE、系统消息拦截
isStepCount