-
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): Added codemod to rm metadata w/ headers. (#3691
- Loading branch information
Showing
6 changed files
with
149 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 (packages/codemod): Added codemod to rm metadata w/ headers. |
50 changes: 50 additions & 0 deletions
50
packages/codemod/src/codemods/remove-metadata-with-headers.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,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(); | ||
} |
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
42 changes: 42 additions & 0 deletions
42
packages/codemod/src/test/__testfixtures__/remove-metadata-with-headers.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,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: {} | ||
}; |
42 changes: 42 additions & 0 deletions
42
packages/codemod/src/test/__testfixtures__/remove-metadata-with-headers.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,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: {} | ||
}; |
9 changes: 9 additions & 0 deletions
9
packages/codemod/src/test/remove-metadata-with-headers.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-metadata-with-headers'; | ||
import { testTransform } from './test-utils'; | ||
|
||
describe('remove-metadata-with-headers', () => { | ||
it('transforms correctly', () => { | ||
testTransform(transformer, 'remove-metadata-with-headers'); | ||
}); | ||
}); |