@ai-sdk/codemod

中文文档
4.0.4发布于 2026/11/05Apache-2.0构建来源已验证近 30 天下载 1.7万
原始使用文档
文档目录21 节
  1. Quick Start
  2. Run All Codemods (Recommended)
  3. Run Version-Specific Codemods
  4. Run Individual Codemods
  5. Available Codemods
  6. v4 Codemods (v3 → v4 Migration)
  7. v5 Codemods (v4 → v5 Migration)
  8. v6 Codemods (v5 → v6 Migration)
  9. CLI Options
  10. Commands
  11. Global Options
  12. Examples
  13. Best Practices
  14. Before Running Codemods
  15. After Running Codemods
  16. Troubleshooting
  17. Contributing
  18. Adding New Codemods
  19. Testing Codemods
  20. Support
  21. Version Compatibility

AI SDK Codemods

The AI SDK provides automated code transformations (codemods) to help upgrade your codebase when features are deprecated, removed, or changed between versions.

Codemods are transformations that run on your codebase programmatically, allowing you to easily apply many changes without manually editing every file.

Quick Start

To run all codemods:

npx @ai-sdk/codemod upgrade

This will automatically detect and transform all applicable code patterns in your project.

Run Version-Specific Codemods

To run codemods for a specific version:

npx @ai-sdk/codemod v4

npx @ai-sdk/codemod v5

npx @ai-sdk/codemod v6

npx @ai-sdk/codemod upgrade

Run Individual Codemods

To run a specific codemod:

npx @ai-sdk/codemod <codemod-name> <path>

Examples:

# Transform a specific file
npx @ai-sdk/codemod v4/remove-experimental-ai-fn-exports src/app/api/chat/route.ts

# Transform a directory
npx @ai-sdk/codemod v4/replace-baseurl src/lib/

# Transform entire project
npx @ai-sdk/codemod v5/rename-format-stream-part .

Available Codemods

v4 Codemods (v3 → v4 Migration)

CodemodDescription
v4/remove-ai-stream-methods-from-stream-text-resultTransforms v4/remove ai stream methods from stream text result
v4/remove-anthropic-facadeTransforms v4/remove anthropic facade
v4/remove-await-streamobjectTransforms v4/remove await streamobject
v4/remove-await-streamtextTransforms v4/remove await streamtext
v4/remove-deprecated-provider-registry-exportsTransforms v4/remove deprecated provider registry exports
v4/remove-experimental-ai-fn-exportsTransforms v4/remove experimental ai fn exports
v4/remove-experimental-message-typesTransforms v4/remove experimental message types
v4/remove-experimental-streamdataTransforms v4/remove experimental streamdata
v4/remove-experimental-toolTransforms v4/remove experimental tool
v4/remove-experimental-useassistantTransforms v4/remove experimental useassistant
v4/remove-google-facadeTransforms v4/remove google facade
v4/remove-isxxxerrorTransforms v4/remove isxxxerror
v4/remove-metadata-with-headersTransforms v4/remove metadata with headers
v4/remove-mistral-facadeTransforms v4/remove mistral facade
v4/remove-openai-facadeTransforms v4/remove openai facade
v4/rename-format-stream-partTransforms v4/rename format stream part
v4/rename-parse-stream-partTransforms v4/rename parse stream part
v4/replace-baseurlTransforms v4/replace baseurl
v4/replace-continuation-stepsTransforms v4/replace continuation steps
v4/replace-langchain-toaistreamTransforms v4/replace langchain toaistream
v4/replace-nanoidTransforms v4/replace nanoid
v4/replace-roundtrips-with-maxstepsTransforms v4/replace roundtrips with maxsteps
v4/replace-token-usage-typesTransforms v4/replace token usage types
v4/rewrite-framework-importsTransforms v4/rewrite framework imports

v5 Codemods (v4 → v5 Migration)

CodemodDescription
v5/flatten-streamtext-file-propertiesTransforms v5/flatten streamtext file properties
v5/import-LanguageModelV2-from-provider-packageTransforms v5/import LanguageModelV2 from provider package
v5/migrate-to-data-stream-protocol-v2Transforms v5/migrate to data stream protocol v2
v5/move-image-model-maxImagesPerCallTransforms v5/move image model maxImagesPerCall
v5/move-langchain-adapterTransforms v5/move langchain adapter
v5/move-maxsteps-to-stopwhenTransforms v5/move maxsteps to stopwhen
v5/move-provider-optionsTransforms v5/move provider options
v5/move-react-to-ai-sdkTransforms v5/move react to ai sdk
v5/move-ui-utils-to-aiTransforms v5/move ui utils to ai
v5/remove-experimental-wrap-language-modelTransforms v5/remove experimental wrap language model
v5/remove-get-ui-textTransforms v5/remove get ui text
v5/remove-openai-compatibilityTransforms v5/remove openai compatibility
v5/remove-sendExtraMessageFieldsTransforms v5/remove sendExtraMessageFields
v5/rename-IDGenerator-to-IdGeneratorTransforms v5/rename IDGenerator to IdGenerator
v5/rename-addtoolresult-to-addtooloutputTransforms v5/rename addtoolresult to addtooloutput
v5/rename-converttocoremessages-to-converttomodelmessagesTransforms v5/rename converttocoremessages to converttomodelmessages
v5/rename-core-message-to-model-messageTransforms v5/rename core message to model message
v5/rename-datastream-methods-to-uimessageTransforms v5/rename datastream methods to uimessage
v5/rename-datastream-transform-streamTransforms v5/rename datastream transform stream
v5/rename-languagemodelv1providermetadataTransforms v5/rename languagemodelv1providermetadata
v5/rename-max-tokens-to-max-output-tokensTransforms v5/rename max tokens to max output tokens
v5/rename-message-to-ui-messageTransforms v5/rename message to ui message
v5/rename-mime-type-to-media-typeTransforms v5/rename mime type to media type
v5/rename-pipedatastreamtoresponse-to-pipeuimessagestreamtoresponseTransforms v5/rename pipedatastreamtoresponse to pipeuimessagestreamtoresponse
v5/rename-reasoning-propertiesTransforms v5/rename reasoning properties
v5/rename-reasoning-to-reasoningTextTransforms v5/rename reasoning to reasoningText
v5/rename-request-optionsTransforms v5/rename request options
v5/rename-todatastreamresponse-to-touimessagestreamresponseTransforms v5/rename todatastreamresponse to touimessagestreamresponse
v5/rename-tool-parameters-to-inputschemaTransforms v5/rename tool parameters to inputschema
v5/replace-bedrock-snake-caseTransforms v5/replace bedrock snake case
v5/replace-content-with-partsTransforms v5/replace content with parts
v5/replace-datastream-to-uimessagestreamTransforms v5/replace datastream to uimessagestream
v5/replace-experimental-provider-metadataTransforms v5/replace experimental provider metadata
v5/replace-fal-snake-caseTransforms v5/replace fal snake case
v5/replace-image-type-with-file-typeTransforms v5/replace image type with file type
v5/replace-llamaindex-adapterTransforms v5/replace llamaindex adapter
v5/replace-oncompletion-with-onfinalTransforms v5/replace oncompletion with onfinal
v5/replace-provider-metadata-with-provider-optionsTransforms v5/replace provider metadata with provider options
v5/replace-rawresponse-with-responseTransforms v5/replace rawresponse with response
v5/replace-redacted-reasoning-typeTransforms v5/replace redacted reasoning type
v5/replace-simulate-streamingTransforms v5/replace simulate streaming
v5/replace-textdelta-with-textTransforms v5/replace textdelta with text
v5/replace-usage-token-propertiesTransforms v5/replace usage token properties
v5/replace-usechat-api-with-transportTransforms v5/replace usechat api with transport
v5/replace-usechat-input-with-stateTransforms v5/replace usechat input with state
v5/replace-zod-import-with-v3Transforms v5/replace zod import with v3
v5/require-createIdGenerator-size-argumentTransforms v5/require createIdGenerator size argument
v5/restructure-file-stream-partsTransforms v5/restructure file stream parts
v5/restructure-source-stream-partsTransforms v5/restructure source stream parts
v5/rsc-packageTransforms v5/rsc package

v6 Codemods (v5 → v6 Migration)

CodemodDescription
v6/add-await-converttomodelmessagesTransforms v6/add await converttomodelmessages
v6/rename-converttocoremessages-to-converttomodelmessagesTransforms v6/rename converttocoremessages to converttomodelmessages
v6/rename-core-message-to-model-messageTransforms v6/rename core message to model message
v6/rename-mock-v2-to-v3Transforms v6/rename mock v2 to v3
v6/rename-text-embedding-to-embeddingTransforms v6/rename text embedding to embedding
v6/rename-tool-call-options-to-tool-execution-optionsTransforms v6/rename tool call options to tool execution options
v6/rename-vertex-provider-metadata-keyTransforms v6/rename vertex provider metadata key
v6/wrap-tomodeloutput-parameterTransforms v6/wrap tomodeloutput parameter

CLI Options

Commands

npx @ai-sdk/codemod@beta <command> [options]

Available Commands:

  • upgrade - Apply all codemods (v4 + v5 + v6)
  • v4 - Apply v4 codemods (v3 → v4 migration)
  • v5 - Apply v5 codemods (v4 → v5 migration)
  • v6 - Apply v6 codemods (v5 → v6 migration)
  • <codemod-name> <path> - Apply specific codemod

Global Options

  • --dry - Preview changes without applying them
  • --print - Print transformed code to stdout
  • --verbose - Show detailed transformation logs

Examples

# Preview all changes without applying
npx @ai-sdk/codemod@beta --dry upgrade

# Preview v4 changes only
npx @ai-sdk/codemod@beta --dry v4

# Preview v5 changes only
npx @ai-sdk/codemod@beta --dry v5

# Preview v6 changes only
npx @ai-sdk/codemod@beta --dry v6

# Show verbose output for specific codemod
npx @ai-sdk/codemod@beta --verbose v4/remove-experimental-ai-fn-exports src/

# Print transformed code for specific codemod
npx @ai-sdk/codemod@beta --print v4/replace-baseurl src/config.ts

Best Practices

Before Running Codemods

  1. Backup your code - Commit all changes to version control
  2. Review current deprecation warnings - Fix any obvious issues first
  3. Update dependencies - Ensure you're on the target AI SDK version

After Running Codemods

  1. Review changes - Check the diff to understand what was transformed
  2. Test your application - Ensure everything works as expected
  3. Handle edge cases - Some complex patterns may need manual fixes
  4. Run type checking - Fix any TypeScript errors that remain

Troubleshooting

If a codemod doesn't transform some code:

  1. Check file extensions - Codemods work on .ts, .tsx, .js, .jsx files
  2. Review patterns - Complex or unusual code patterns may need manual updates
  3. Run specific codemods - Try running individual codemods for targeted fixes
  4. Check documentation - Some changes may not have automated codemods

Contributing

Adding New Codemods

  1. Create the codemod in src/codemods/
  2. Add test fixtures in src/test/__testfixtures__/
  3. Create tests in src/test/
  4. Update the bundle in src/lib/upgrade.ts

Testing Codemods

First, navigate to the codemod directory:

cd packages/codemod

Then run the tests:

# Run all tests
pnpm test

# Run specific codemod tests
pnpm test <codemod-name>

# Test in development
pnpm test:watch

Support

Version Compatibility

  • AI SDK 6.0: All codemods in this package
  • AI SDK 5.0: Use v4 + v5 codemods
  • AI SDK 4.x: Use @ai-sdk/[email protected]
  • AI SDK 3.x: Manual migration required

For more detailed migration information, see:

版本与发布记录

@ai-sdk/codemod 共发布 74 个版本,首次发布于 2024/11/12;过去 12 个月发布了 37 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
4.0.4latest2026/11/051 天前
4.0.32026/09/301 个月前
4.0.22026/09/241 个月前
2.0.17ai-v52026/08/272 个月前
3.0.8ai-v62026/08/272 个月前
4.0.12026/08/252 个月前

其他发布通道:alpha → 2.0.0-alpha.0,snapshot → 0.0.0-bf6e4b15-20260402200305,canary → 4.0.0-canary.6,beta → 4.0.0-beta.8,ai-v6 → 3.0.8,ai-v5 → 2.0.17。

关于 @ai-sdk/codemod 的常见问题

如何安装 @ai-sdk/codemod?

在项目目录运行 npm install @ai-sdk/codemod,或使用 pnpm add / yarn add / bun add。它还提供命令行工具 codemod,可以用 npx codemod 直接运行。

@ai-sdk/codemod 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/ai-sdk__codemod 是否存在。

@ai-sdk/codemod 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=22。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@ai-sdk/codemod 可以商用吗?

@ai-sdk/codemod 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@ai-sdk/codemod 的下载量有多少?

根据 npm 官方下载统计,@ai-sdk/codemod 在 2026/10/05 – 2026/11/03 这 30 天内被下载 16,660 次,日均约 555 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@ai-sdk/codemod 还在维护吗?

最新版本 4.0.4 发布于 2026/11/05(1 天前);至今共发布 74 个版本,其中过去 12 个月发布了 37 个;首次发布于 2024/11/12。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。