Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat (packages/codemod): Add codemod to remove provider facades. #3642

Merged
merged 2 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/healthy-squids-hear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@ai-sdk/codemod': patch
---

feat (packages/codemod): Add codemod to remove provider facades.
53 changes: 53 additions & 0 deletions packages/codemod/src/codemods/lib/remove-facade.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { API, FileInfo, JSCodeshift } from 'jscodeshift';

interface FacadeConfig {
packageName: string; // e.g. 'openai'
className: string; // e.g. 'OpenAI'
createFnName: string; // e.g. 'createOpenAI'
}

export function removeFacade(
fileInfo: FileInfo,
api: API,
config: FacadeConfig,
) {
const j: JSCodeshift = api.jscodeshift;
const root = j(fileInfo.source);
const importPath = `@ai-sdk/${config.packageName}`;

// Replace imports
root.find(j.ImportDeclaration).forEach(path => {
const sourceValue = path.node.source.value;
if (sourceValue === importPath) {
const hasClassSpecifier = path.node.specifiers?.some(
spec =>
spec.type === 'ImportSpecifier' &&
spec.imported.name === config.className,
);

if (hasClassSpecifier) {
path.node.source.value = importPath;
path.node.specifiers = [
j.importSpecifier(j.identifier(config.createFnName)),
];
}
}
});

// Replace new Class() with createFn()
root.find(j.NewExpression).forEach(path => {
if (
path.node.callee.type === 'Identifier' &&
path.node.callee.name === config.className
) {
j(path).replaceWith(
j.callExpression(
j.identifier(config.createFnName),
path.node.arguments,
),
);
}
});

return root.toSource();
}
10 changes: 10 additions & 0 deletions packages/codemod/src/codemods/remove-anthropic-facade.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { API, FileInfo } from 'jscodeshift';
import { removeFacade } from './lib/remove-facade';

export default function transformer(fileInfo: FileInfo, api: API) {
return removeFacade(fileInfo, api, {
packageName: 'anthropic',
className: 'Anthropic',
createFnName: 'createAnthropic',
});
}
10 changes: 10 additions & 0 deletions packages/codemod/src/codemods/remove-google-facade.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { API, FileInfo } from 'jscodeshift';
import { removeFacade } from './lib/remove-facade';

export default function transformer(fileInfo: FileInfo, api: API) {
return removeFacade(fileInfo, api, {
packageName: 'google',
className: 'Google',
createFnName: 'createGoogleGenerativeAI',
});
}
10 changes: 10 additions & 0 deletions packages/codemod/src/codemods/remove-mistral-facade.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { API, FileInfo } from 'jscodeshift';
import { removeFacade } from './lib/remove-facade';

export default function transformer(fileInfo: FileInfo, api: API) {
return removeFacade(fileInfo, api, {
packageName: 'mistral',
className: 'Mistral',
createFnName: 'createMistral',
});
}
10 changes: 10 additions & 0 deletions packages/codemod/src/codemods/remove-openai-facade.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { API, FileInfo } from 'jscodeshift';
import { removeFacade } from './lib/remove-facade';

export default function transformer(fileInfo: FileInfo, api: API) {
return removeFacade(fileInfo, api, {
packageName: 'openai',
className: 'OpenAI',
createFnName: 'createOpenAI',
});
}
4 changes: 4 additions & 0 deletions packages/codemod/src/lib/upgrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import { TransformOptions } from './transform-options';

const bundle = [
'remove-ai-stream-methods-from-stream-text-result',
'remove-anthropic-facade',
'remove-google-facade',
'remove-mistral-facade',
'remove-openai-facade',
'remove-experimental-ai-fn-exports',
'replace-baseurl',
'replace-langchain-toaistream',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// @ts-nocheck
import { Anthropic } from '@ai-sdk/anthropic';

const anthropic = new Anthropic({
apiKey: 'key',
baseURL: 'url',
headers: { 'custom': 'header' }
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// @ts-nocheck
import { createAnthropic } from '@ai-sdk/anthropic';

const anthropic = createAnthropic({
apiKey: 'key',
baseURL: 'url',
headers: { 'custom': 'header' }
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// @ts-nocheck
import { Google } from '@ai-sdk/google';

const google = new Google({
apiKey: 'key',
baseURL: 'url',
headers: { 'custom': 'header' }
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// @ts-nocheck
import { createGoogleGenerativeAI } from '@ai-sdk/google';

const google = createGoogleGenerativeAI({
apiKey: 'key',
baseURL: 'url',
headers: { 'custom': 'header' }
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// @ts-nocheck
import { Mistral } from '@ai-sdk/mistral';

const mistral = new Mistral({
apiKey: 'key',
baseURL: 'url',
headers: { 'custom': 'header' }
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// @ts-nocheck
import { createMistral } from '@ai-sdk/mistral';

const mistral = createMistral({
apiKey: 'key',
baseURL: 'url',
headers: { 'custom': 'header' }
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// @ts-nocheck
import { OpenAI } from '@ai-sdk/openai';

const openai = new OpenAI({
apiKey: 'key',
baseURL: 'url',
headers: { 'custom': 'header' }
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// @ts-nocheck
import { createOpenAI } from '@ai-sdk/openai';

const openai = createOpenAI({
apiKey: 'key',
baseURL: 'url',
headers: { 'custom': 'header' }
});
9 changes: 9 additions & 0 deletions packages/codemod/src/test/remove-anthropic-facade.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { describe, it } from 'vitest';
import transformer from '../codemods/remove-anthropic-facade';
import { testTransform } from './test-utils';

describe('remove-anthropic-facade', () => {
it('transforms correctly', () => {
testTransform(transformer, 'remove-anthropic-facade');
});
});
9 changes: 9 additions & 0 deletions packages/codemod/src/test/remove-google-facade.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { describe, it } from 'vitest';
import transformer from '../codemods/remove-google-facade';
import { testTransform } from './test-utils';

describe('remove-google-facade', () => {
it('transforms correctly', () => {
testTransform(transformer, 'remove-google-facade');
});
});
9 changes: 9 additions & 0 deletions packages/codemod/src/test/remove-mistral-facade.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { describe, it } from 'vitest';
import transformer from '../codemods/remove-mistral-facade';
import { testTransform } from './test-utils';

describe('remove-mistral-facade', () => {
it('transforms correctly', () => {
testTransform(transformer, 'remove-mistral-facade');
});
});
9 changes: 9 additions & 0 deletions packages/codemod/src/test/remove-openai-facade.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { describe, it } from 'vitest';
import transformer from '../codemods/remove-openai-facade';
import { testTransform } from './test-utils';

describe('remove-openai-facade', () => {
it('transforms correctly', () => {
testTransform(transformer, 'remove-openai-facade');
});
});
Loading