-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat (packages/codemod): Add codemod to rm old provider registry expo…
…rts. (#3649)
- Loading branch information
Showing
6 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@ai-sdk/codemod': patch | ||
--- | ||
|
||
feat (providers/codemod): Add codemod to remove deprecated prov reg exports. |
80 changes: 80 additions & 0 deletions
80
packages/codemod/src/codemods/remove-deprecated-provider-registry-exports.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { API, FileInfo } from 'jscodeshift'; | ||
|
||
export default function transformer(file: FileInfo, api: API) { | ||
const j = api.jscodeshift; | ||
const root = j(file.source); | ||
|
||
// Replace imports | ||
root | ||
.find(j.ImportDeclaration) | ||
.filter(path => path.node.source.value === 'ai') | ||
.forEach(path => { | ||
const newSpecifiers = path.node.specifiers | ||
?.map(spec => { | ||
if (spec.type !== 'ImportSpecifier') return spec; | ||
|
||
const oldName = spec.imported.name; | ||
if (oldName === 'experimental_createModelRegistry') { | ||
return j.importSpecifier( | ||
j.identifier('experimental_createProviderRegistry'), | ||
); | ||
} | ||
|
||
if ( | ||
[ | ||
'experimental_Provider', | ||
'experimental_ProviderRegistry', | ||
'experimental_ModelRegistry', | ||
].includes(oldName) | ||
) { | ||
return j.importSpecifier(j.identifier('Provider')); | ||
} | ||
|
||
return spec; | ||
}) | ||
.filter((spec, index, arr) => { | ||
// Deduplicate specifiers | ||
if (!spec) return false; | ||
return ( | ||
arr.findIndex( | ||
s => | ||
s?.type === 'ImportSpecifier' && | ||
spec.type === 'ImportSpecifier' && | ||
s.imported.name === spec.imported.name, | ||
) === index | ||
); | ||
}); | ||
|
||
path.node.specifiers = newSpecifiers; | ||
}); | ||
|
||
// Replace type references | ||
root | ||
.find(j.TSTypeReference) | ||
.filter( | ||
path => | ||
path.node.typeName.type === 'Identifier' && | ||
[ | ||
'experimental_Provider', | ||
'experimental_ProviderRegistry', | ||
'experimental_ModelRegistry', | ||
].includes(path.node.typeName.name), | ||
) | ||
.forEach(path => { | ||
path.node.typeName = j.identifier('Provider'); | ||
}); | ||
|
||
// Replace function calls | ||
root | ||
.find(j.CallExpression, { | ||
callee: { | ||
type: 'Identifier', | ||
name: 'experimental_createModelRegistry', | ||
}, | ||
}) | ||
.forEach(path => { | ||
path.node.callee = j.identifier('experimental_createProviderRegistry'); | ||
}); | ||
|
||
return root.toSource(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
...es/codemod/src/test/__testfixtures__/remove-deprecated-provider-registry-exports.input.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// @ts-nocheck | ||
import { experimental_Provider, experimental_ProviderRegistry, experimental_ModelRegistry, experimental_createModelRegistry } from 'ai'; | ||
|
||
function createProvider(): experimental_Provider { | ||
return { | ||
languageModel: () => null, | ||
textEmbeddingModel: () => null | ||
}; | ||
} | ||
|
||
function createRegistry(): experimental_ProviderRegistry { | ||
return experimental_createModelRegistry({ | ||
test: createProvider() | ||
}); | ||
} | ||
|
||
const registry: experimental_ModelRegistry = createRegistry(); |
17 changes: 17 additions & 0 deletions
17
...s/codemod/src/test/__testfixtures__/remove-deprecated-provider-registry-exports.output.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// @ts-nocheck | ||
import { Provider, experimental_createProviderRegistry } from 'ai'; | ||
|
||
function createProvider(): Provider { | ||
return { | ||
languageModel: () => null, | ||
textEmbeddingModel: () => null | ||
}; | ||
} | ||
|
||
function createRegistry(): Provider { | ||
return experimental_createProviderRegistry({ | ||
test: createProvider() | ||
}); | ||
} | ||
|
||
const registry: Provider = createRegistry(); |
9 changes: 9 additions & 0 deletions
9
packages/codemod/src/test/remove-deprecated-provider-registry-exports.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { describe, it } from 'vitest'; | ||
import transformer from '../codemods/remove-deprecated-provider-registry-exports'; | ||
import { testTransform } from './test-utils'; | ||
|
||
describe('remove-deprecated-provider-registry-exports', () => { | ||
it('transforms correctly', () => { | ||
testTransform(transformer, 'remove-deprecated-provider-registry-exports'); | ||
}); | ||
}); |