-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create tools for component and page creation
- Loading branch information
Showing
2 changed files
with
67 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,29 @@ | ||
import { StructuredTool } from "langchain/tools"; | ||
import { z } from "zod"; | ||
import WriteFileTool from "./write_file"; | ||
|
||
const parameterSchema = z.object({ | ||
name: z.string().describe("The name of the React component to create"), | ||
content: z.string().describe("The contents of the React file to write"), | ||
}); | ||
|
||
export default class CreateComponentTool extends StructuredTool< | ||
typeof parameterSchema | ||
> { | ||
name = "create_react_component"; | ||
description = "Create a React component file."; | ||
schema = parameterSchema; | ||
|
||
constructor(private readonly fileWriter: WriteFileTool) { | ||
super(); | ||
} | ||
|
||
async _call({ name, content }: z.input<this["schema"]>): Promise<string> { | ||
const path = `src/components/${name}.tsx`; | ||
await this.fileWriter.call({ | ||
path, | ||
content, | ||
}); | ||
return path; | ||
} | ||
} |
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,38 @@ | ||
import { StructuredTool } from "langchain/tools"; | ||
import { z } from "zod"; | ||
import WriteFileTool from "./write_file"; | ||
|
||
const parameterSchema = z.object({ | ||
http_path: z.string().describe("The path to the page to create. Use square brackets to indicate dynamic segments, e.g. \"/users/[id]\""), | ||
layout_content: z.string().describe("The contents of the layout file to write"), | ||
page_content: z.string().describe("The contents of the page file to write") | ||
}); | ||
|
||
export default class CreateComponentTool extends StructuredTool< | ||
typeof parameterSchema | ||
> { | ||
name = "create_page"; | ||
description = "Create a new page using Next.js App Router"; | ||
schema = parameterSchema; | ||
|
||
constructor(private readonly fileWriter: WriteFileTool) { | ||
super(); | ||
} | ||
|
||
async _call({ http_path, layout_content, page_content }: z.input<this["schema"]>): Promise<string> { | ||
const basePath = `src/app${http_path}`; | ||
const layoutPath = `${basePath}/layout.tsx`; | ||
const pagePath = `${basePath}/index.tsx`; | ||
await Promise.all([ | ||
this.fileWriter.call({ | ||
path: layoutPath, | ||
content: layout_content, | ||
}), | ||
this.fileWriter.call({ | ||
path: pagePath, | ||
content: page_content, | ||
}) | ||
]) | ||
return JSON.stringify({ layoutPath, pagePath }, null, 2); | ||
} | ||
} |