Skip to content

Commit 5565d6a

Browse files
committed
feat: add absolute path handling and read file content in CreateUiTool
1 parent 5cd8ff2 commit 5565d6a

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/tools/create-ui.ts

+14-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { z } from "zod";
22
import { BaseTool } from "../utils/base-tool.js";
33
import { twentyFirstClient } from "../utils/http-client.js";
4-
import { readdir } from "fs/promises";
4+
import { readdir, readFile } from "fs/promises";
55

66
const UI_TOOL_NAME = "21st_magic_component_builder";
77
const UI_TOOL_DESCRIPTION = `
@@ -25,6 +25,11 @@ export class CreateUiTool extends BaseTool {
2525
.describe(
2626
"Generate a search query for 21st.dev (library for searching UI components) to find a UI component that matches the user's message. Must be a two-four words max or phrase"
2727
),
28+
absolutePathToCurrentFile: z
29+
.string()
30+
.describe(
31+
"Absolute path to the current file to which we want to apply changes"
32+
),
2833
absolutePathToProjectDirectory: z
2934
.string()
3035
.describe("Absolute path to the project root directory"),
@@ -34,6 +39,7 @@ export class CreateUiTool extends BaseTool {
3439
message,
3540
searchQuery,
3641
absolutePathToProjectDirectory,
42+
absolutePathToCurrentFile,
3743
}: z.infer<typeof this.schema>) {
3844
try {
3945
const { data } = await twentyFirstClient.post<CreateUiResponse>(
@@ -48,13 +54,19 @@ export class CreateUiTool extends BaseTool {
4854
withFileTypes: true,
4955
});
5056

57+
const currentFileContent = await readFile(
58+
absolutePathToCurrentFile,
59+
"utf-8"
60+
);
61+
5162
return {
5263
content: [
5364
{
5465
type: "text" as const,
5566
text: JSON.stringify({
5667
dirFiles: currentDirFiles,
57-
component: data.text,
68+
currentFileContent,
69+
component: data.text + "\n\n" + data.,
5870
}),
5971
},
6072
],

0 commit comments

Comments
 (0)