Skip to content

Commit

Permalink
Create tools for component and page creation
Browse files Browse the repository at this point in the history
  • Loading branch information
gramliu committed Dec 8, 2023
1 parent 1525144 commit 77c4493
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
29 changes: 29 additions & 0 deletions apps/agent/src/tools/create_component.ts
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;
}
}
38 changes: 38 additions & 0 deletions apps/agent/src/tools/create_page.ts
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);
}
}

0 comments on commit 77c4493

Please sign in to comment.