Skip to content

Commit

Permalink
feat (packages/codemod): Add codemod to rm old provider registry expo…
Browse files Browse the repository at this point in the history
…rts. (#3649)
  • Loading branch information
shaper authored Nov 13, 2024
1 parent 29556ed commit 1931f4f
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/slimy-zoos-double.md
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.
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();
}
1 change: 1 addition & 0 deletions packages/codemod/src/lib/upgrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const bundle = [
'replace-nanoid',
'replace-token-usage-types',
'rewrite-framework-imports',
'remove-deprecated-provider-registry-exports',
'remove-experimental-message-types',
'replace-roundtrips-with-maxsteps',
];
Expand Down
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();
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();
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');
});
});

0 comments on commit 1931f4f

Please sign in to comment.