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 replace roundtrips. #3636

Merged
merged 1 commit 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/thick-foxes-judge.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@ai-sdk/codemod': patch
---

feat (package/codemod): Add codemod to replace roundtrips.
63 changes: 63 additions & 0 deletions packages/codemod/src/codemods/replace-roundtrips-with-maxsteps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { API, FileInfo } from 'jscodeshift';

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

root
.find(j.CallExpression)
.filter(
path =>
path.node.callee.type === 'Identifier' &&
['generateText', 'streamText'].includes(path.node.callee.name),
)
.forEach(path => {
const optionsArg = path.node.arguments[0];
if (optionsArg?.type !== 'ObjectExpression') return;

let maxStepsValue = 1;
let foundRoundtrips = false;

optionsArg.properties = optionsArg.properties.filter(prop => {
if (
prop.type === 'ObjectProperty' &&
prop.key.type === 'Identifier' &&
['maxToolRoundtrips', 'maxAutomaticRoundtrips'].includes(
prop.key.name,
)
) {
foundRoundtrips = true;
if (prop.value.type === 'NumericLiteral') {
maxStepsValue = prop.value.value + 1;
}
return false; // Remove the property
}
return true;
});

if (foundRoundtrips) {
optionsArg.properties.push(
j.objectProperty(
j.identifier('maxSteps'),
j.numericLiteral(maxStepsValue),
),
);
}
});

// Replace property access
root
.find(j.MemberExpression)
.filter(
path =>
path.node.property.type === 'Identifier' &&
path.node.property.name === 'roundtrips',
)
.forEach(path => {
if (path.node.property.type === 'Identifier') {
path.node.property.name = 'steps';
}
});

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 @@ -12,6 +12,7 @@ const bundle = [
'replace-token-usage-types',
'rewrite-framework-imports',
'remove-experimental-message-types',
'replace-roundtrips-with-maxsteps',
];

function validatePreconditions(cwd: string) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// @ts-nocheck
import { generateText, streamText } from 'ai';

// Test generateText with both roundtrip types
await generateText({
model,
maxToolRoundtrips: 3,
maxAutomaticRoundtrips: 2
});

// Test streamText with just maxToolRoundtrips
await streamText({
model,
maxToolRoundtrips: 5
});

// Test streamText with subsequent maxToolRoundtrips
await streamText({
model,
maxToolRoundtrips: 67
});

// Test streamText with no roundtrips
await streamText({
model
});

// Test generateText with just maxAutomaticRoundtrips
await generateText({
model,
maxAutomaticRoundtrips: 4
});

// Test generateText with subsequent maxToolRoundtrips
await generateText({
model,
maxToolRoundtrips: 42
});

// Test generateText with no roundtrips
await generateText({
model
});

// Test property access
const result = await generateText({ model });
console.log(result.roundtrips.length);
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// @ts-nocheck
import { generateText, streamText } from 'ai';

// Test generateText with both roundtrip types
await generateText({
model,
maxSteps: 3
});

// Test streamText with just maxToolRoundtrips
await streamText({
model,
maxSteps: 6
});

// Test streamText with subsequent maxToolRoundtrips
await streamText({
model,
maxSteps: 68
});

// Test streamText with no roundtrips
await streamText({
model
});

// Test generateText with just maxAutomaticRoundtrips
await generateText({
model,
maxSteps: 5
});

// Test generateText with subsequent maxToolRoundtrips
await generateText({
model,
maxSteps: 43
});

// Test generateText with no roundtrips
await generateText({
model
});

// Test property access
const result = await generateText({ model });
console.log(result.steps.length);
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { describe, it } from 'vitest';
import transformer from '../codemods/replace-roundtrips-with-maxsteps';
import { testTransform } from './test-utils';

describe('replace-roundtrips-with-maxsteps', () => {
it('transforms correctly', () => {
testTransform(transformer, 'replace-roundtrips-with-maxsteps');
});
});
Loading