-
Notifications
You must be signed in to change notification settings - Fork 1
Migrate to AI SDK Gateway #151
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,8 @@ | ||
| import { createGateway } from "@ai-sdk/gateway"; | ||
|
|
||
| export const gateway = createGateway({ | ||
| baseURL: process.env.AI_GATEWAY_BASE_URL || "https://gateway.ai.vercel.sh/v1", | ||
| headers: { | ||
| Authorization: `Bearer ${process.env.AI_GATEWAY_API_KEY}`, | ||
| }, | ||
| }); | ||
This file contains hidden or 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 @@ | ||
| export { gateway } from "./gateway"; | ||
| export { createCodeAgentTools } from "./tools"; | ||
| export type { | ||
| AgentState, | ||
| AgentResult, | ||
| Framework, | ||
| AgentContext, | ||
| ToolResult, | ||
| } from "./types"; |
This file contains hidden or 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,137 @@ | ||
| import { tool } from "ai"; | ||
| import { z } from "zod"; | ||
| import { Sandbox } from "@e2b/code-interpreter"; | ||
| import type { AgentState, ToolResult } from "./types"; | ||
|
|
||
| async function getSandboxFromId(sandboxId: string): Promise<Sandbox> { | ||
| return await Sandbox.connect(sandboxId, { | ||
| apiKey: process.env.E2B_API_KEY, | ||
| }); | ||
| } | ||
|
|
||
| export function createCodeAgentTools( | ||
| sandboxId: string, | ||
| stateRef: { current: AgentState } | ||
| ) { | ||
| const terminalTool = tool({ | ||
| description: "Run a terminal command in the sandbox environment", | ||
| parameters: z.object({ | ||
| command: z.string().describe("The command to execute in the terminal"), | ||
| }), | ||
| execute: async ({ command }): Promise<ToolResult> => { | ||
| const buffers = { stdout: "", stderr: "" }; | ||
|
|
||
| try { | ||
| const sandbox = await getSandboxFromId(sandboxId); | ||
| const result = await sandbox.commands.run(command, { | ||
| onStdout: (data: string) => { | ||
| buffers.stdout += data; | ||
| }, | ||
| onStderr: (data: string) => { | ||
| buffers.stderr += data; | ||
| }, | ||
| }); | ||
| return { | ||
| success: true, | ||
| data: { | ||
| stdout: result.stdout || buffers.stdout, | ||
| stderr: buffers.stderr, | ||
| exitCode: result.exitCode, | ||
| }, | ||
| }; | ||
| } catch (e) { | ||
| const errorMessage = e instanceof Error ? e.message : String(e); | ||
| return { | ||
| success: false, | ||
| error: `Command failed: ${errorMessage}\nstdout: ${buffers.stdout}\nstderr: ${buffers.stderr}`, | ||
| }; | ||
| } | ||
| }, | ||
| }); | ||
|
|
||
| const createOrUpdateFilesTool = tool({ | ||
| description: | ||
| "Create or update files in the sandbox. Use this to write code files.", | ||
| parameters: z.object({ | ||
| files: z | ||
| .array( | ||
| z.object({ | ||
| path: z.string().describe("The file path relative to the sandbox"), | ||
| content: z.string().describe("The content to write to the file"), | ||
| }) | ||
| ) | ||
| .describe("Array of files to create or update"), | ||
| }), | ||
| execute: async ({ files }): Promise<ToolResult> => { | ||
| try { | ||
| const sandbox = await getSandboxFromId(sandboxId); | ||
| const updatedFiles = stateRef.current.files || {}; | ||
|
|
||
| for (const file of files) { | ||
| await sandbox.files.write(file.path, file.content); | ||
| updatedFiles[file.path] = file.content; | ||
| } | ||
|
|
||
| stateRef.current.files = updatedFiles; | ||
|
|
||
| return { | ||
| success: true, | ||
| data: { | ||
| filesWritten: files.map((f) => f.path), | ||
| totalFiles: Object.keys(updatedFiles).length, | ||
| }, | ||
| }; | ||
| } catch (e) { | ||
| const errorMessage = e instanceof Error ? e.message : String(e); | ||
| return { | ||
| success: false, | ||
| error: `Failed to write files: ${errorMessage}`, | ||
| }; | ||
| } | ||
| }, | ||
| }); | ||
|
|
||
| const readFilesTool = tool({ | ||
| description: "Read files from the sandbox to understand existing code", | ||
| parameters: z.object({ | ||
| files: z | ||
| .array(z.string()) | ||
| .describe("Array of file paths to read from the sandbox"), | ||
| }), | ||
| execute: async ({ files }): Promise<ToolResult> => { | ||
| try { | ||
| const sandbox = await getSandboxFromId(sandboxId); | ||
| const contents: Array<{ path: string; content: string }> = []; | ||
|
|
||
| for (const filePath of files) { | ||
| try { | ||
| const content = await sandbox.files.read(filePath); | ||
| contents.push({ path: filePath, content }); | ||
| } catch { | ||
| contents.push({ | ||
| path: filePath, | ||
| content: `[Error: Could not read file ${filePath}]`, | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| return { | ||
| success: true, | ||
| data: contents, | ||
| }; | ||
| } catch (e) { | ||
| const errorMessage = e instanceof Error ? e.message : String(e); | ||
| return { | ||
| success: false, | ||
| error: `Failed to read files: ${errorMessage}`, | ||
| }; | ||
| } | ||
| }, | ||
| }); | ||
|
|
||
| return { | ||
| terminal: terminalTool, | ||
| createOrUpdateFiles: createOrUpdateFilesTool, | ||
| readFiles: readFilesTool, | ||
| }; | ||
| } |
This file contains hidden or 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,28 @@ | ||
| import type { CoreMessage } from "ai"; | ||
|
|
||
| export type Framework = "nextjs" | "angular" | "react" | "vue" | "svelte"; | ||
|
|
||
| export interface AgentState { | ||
| summary: string; | ||
| files: Record<string, string>; | ||
| selectedFramework?: Framework; | ||
| summaryRetryCount: number; | ||
| } | ||
|
|
||
| export interface AgentContext { | ||
| sandboxId: string; | ||
| state: AgentState; | ||
| messages: CoreMessage[]; | ||
| } | ||
|
|
||
| export interface AgentResult { | ||
| state: AgentState; | ||
| messages: CoreMessage[]; | ||
| output: string; | ||
| } | ||
|
|
||
| export interface ToolResult { | ||
| success: boolean; | ||
| data?: unknown; | ||
| error?: string; | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.