Skip to content

Commit

Permalink
feat (packages/codemod): Added codemod to rm metadata w/ headers. (#3691
Browse files Browse the repository at this point in the history
)
  • Loading branch information
shaper authored Nov 15, 2024
1 parent 977eb23 commit 7c205ba
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/cold-ducks-change.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@ai-sdk/codemod': patch
---

feat (packages/codemod): Added codemod to rm metadata w/ headers.
50 changes: 50 additions & 0 deletions packages/codemod/src/codemods/remove-metadata-with-headers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { API, FileInfo } from 'jscodeshift';

export default function transformer(file: FileInfo, api: API) {
const j = api.jscodeshift;
const root = j(file.source);

// Track imports from 'ai' package
const targetImports = new Set<string>();

// Replace imports
root
.find(j.ImportDeclaration)
.filter(path => path.node.source.value === 'ai')
.forEach(path => {
path.node.specifiers = path.node.specifiers?.filter(spec => {
if (
spec.type === 'ImportSpecifier' &&
spec.imported.type === 'Identifier' &&
spec.imported.name === 'LanguageModelResponseMetadataWithHeaders'
) {
// Track local name
targetImports.add(spec.local?.name || spec.imported.name);

// Replace with new type
spec.imported.name = 'LanguageModelResponseMetadata';
if (spec.local) {
spec.local.name = 'LanguageModelResponseMetadata';
}
return true;
}
return true;
});
});

// Replace type references
root
.find(j.TSTypeReference)
.filter(
path =>
path.node.typeName.type === 'Identifier' &&
targetImports.has(path.node.typeName.name),
)
.forEach(path => {
if (path.node.typeName.type === 'Identifier') {
path.node.typeName.name = 'LanguageModelResponseMetadata';
}
});

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 @@ -13,6 +13,7 @@ const bundle = [
'remove-experimental-message-types',
'remove-experimental-tool',
'remove-google-facade',
'remove-metadata-with-headers',
'remove-mistral-facade',
'remove-openai-facade',
'rename-format-stream-part',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// @ts-nocheck
import { generateObject, LanguageModelResponseMetadataWithHeaders } from 'ai';
import { LanguageModelResponseMetadataWithHeaders as MetadataWithHeaders } from 'other-pkg';

// Direct type usage
interface Config {
metadata: LanguageModelResponseMetadataWithHeaders;
}

// Usage with generateObject result
async function processResult() {
const result = await generateObject({
model,
schema: schema,
prompt: 'test'
});

// Save response metadata to variable
const metadata: LanguageModelResponseMetadataWithHeaders = result.response;

// Destructured access
const { headers, timestamp }: LanguageModelResponseMetadataWithHeaders = result.response;

// Direct property access
const responseData: LanguageModelResponseMetadataWithHeaders = {
id: result.response.id,
timestamp: result.response.timestamp,
headers: result.response.headers
};

return { metadata, headers, responseData };
}

// Should NOT rename - different package
type OtherMetadata = MetadataWithHeaders;

// Should rename
const data: LanguageModelResponseMetadataWithHeaders = {
id: 'test',
timestamp: new Date(),
headers: {}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// @ts-nocheck
import { generateObject, LanguageModelResponseMetadata } from 'ai';
import { LanguageModelResponseMetadataWithHeaders as MetadataWithHeaders } from 'other-pkg';

// Direct type usage
interface Config {
metadata: LanguageModelResponseMetadata;
}

// Usage with generateObject result
async function processResult() {
const result = await generateObject({
model,
schema: schema,
prompt: 'test'
});

// Save response metadata to variable
const metadata: LanguageModelResponseMetadata = result.response;

// Destructured access
const { headers, timestamp }: LanguageModelResponseMetadata = result.response;

// Direct property access
const responseData: LanguageModelResponseMetadata = {
id: result.response.id,
timestamp: result.response.timestamp,
headers: result.response.headers
};

return { metadata, headers, responseData };
}

// Should NOT rename - different package
type OtherMetadata = MetadataWithHeaders;

// Should rename
const data: LanguageModelResponseMetadata = {
id: 'test',
timestamp: new Date(),
headers: {}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { describe, it } from 'vitest';
import transformer from '../codemods/remove-metadata-with-headers';
import { testTransform } from './test-utils';

describe('remove-metadata-with-headers', () => {
it('transforms correctly', () => {
testTransform(transformer, 'remove-metadata-with-headers');
});
});

0 comments on commit 7c205ba

Please sign in to comment.