Skip to content

Commit 6026191

Browse files
authored
update cookbook to use SDK BEN-893 (#21)
### TL;DR Replaced custom Benchify implementation with the official Benchify npm package. ### What changed? - Removed the custom `lib/benchify.ts` file that contained a manual implementation for interacting with the Benchify API - Added the official `benchify` npm package (version 0.1.0-alpha.3) as a dependency - Updated the code in `app/api/generate/route.ts` to use the Benchify client from the package - Simplified the code flow for repairing generated files using Benchify's fixer API - Maintained the same functionality of applying patches to the generated code ### How to test? 1. Run `npm install` to install the new Benchify package 2. Ensure the `BENCHIFY_API_KEY` environment variable is set 3. Test the `/api/generate` endpoint with a component description 4. Verify that code generation and repair works as expected 5. Check that the sandbox creation still functions properly with the repaired files ### Why make this change? Using the official Benchify package provides several advantages: - Reduces maintenance burden by eliminating custom API integration code - Ensures compatibility with the latest Benchify API changes - Simplifies error handling and request formatting - Makes future updates easier as the package will be maintained by the Benchify team
2 parents 6514a1d + c1c05c9 commit 6026191

File tree

4 files changed

+35
-105
lines changed

4 files changed

+35
-105
lines changed

app/api/generate/route.ts

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
// app/api/generate/route.ts
22
import { NextRequest, NextResponse } from 'next/server';
33
import { generateApp } from '@/lib/openai';
4-
import { repairCode } from '@/lib/benchify';
54
import { createSandbox } from '@/lib/e2b';
65
import { componentSchema } from '@/lib/schemas';
7-
import { benchifyFileSchema } from '@/lib/schemas';
6+
import { Benchify } from 'benchify';
7+
import { applyPatch } from 'diff';
8+
9+
const benchify = new Benchify({
10+
apiKey: process.env.BENCHIFY_API_KEY,
11+
});
812

913
export async function POST(request: NextRequest) {
1014
try {
@@ -25,15 +29,30 @@ export async function POST(request: NextRequest) {
2529
// Generate the Vue app using OpenAI
2630
const generatedFiles = await generateApp(description);
2731

28-
// Parse through schema before passing to repair
29-
const validatedFiles = benchifyFileSchema.parse(generatedFiles);
32+
// Repair the generated code using Benchify's API
33+
const { data } = await benchify.fixer.run({
34+
files: generatedFiles.map(file => ({
35+
path: file.path,
36+
contents: file.content
37+
}))
38+
});
3039

31-
// // Repair the generated code using Benchify's API
32-
// const { repairedFiles, buildOutput } = await repairCode(validatedFiles);
40+
let repairedFiles = generatedFiles;
41+
if (data) {
42+
const { success, diff } = data;
3343

34-
const { sbxId, template, url, allFiles } = await createSandbox({ files: generatedFiles });
44+
if (success && diff) {
45+
repairedFiles = generatedFiles.map(file => {
46+
const patchResult = applyPatch(file.content, diff);
47+
return {
48+
...file,
49+
content: typeof patchResult === 'string' ? patchResult : file.content
50+
};
51+
});
52+
}
53+
}
3554

36-
console.log("Preview URL: ", url);
55+
const { sbxId, template, url, allFiles } = await createSandbox({ files: repairedFiles });
3756

3857
// Return the results to the client
3958
return NextResponse.json({

lib/benchify.ts

Lines changed: 0 additions & 97 deletions
This file was deleted.

package-lock.json

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"@radix-ui/react-tabs": "^1.1.11",
2121
"@types/react-syntax-highlighter": "^15.5.13",
2222
"ai": "^4.3.15",
23+
"benchify": "^0.1.0-alpha.3",
2324
"class-variance-authority": "^0.7.1",
2425
"clsx": "^2.1.1",
2526
"diff": "^8.0.1",

0 commit comments

Comments
 (0)