forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate-content-json.js
More file actions
158 lines (140 loc) · 4.35 KB
/
Copy pathgenerate-content-json.js
File metadata and controls
158 lines (140 loc) · 4.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
const fs = require("fs");
const path = require("path");
// Files to include for each demo
const config = {
agentic_chat: ["agent.py", "page.tsx", "style.css", "README.mdx"],
agentic_generative_ui: ["agent.py", "page.tsx", "style.css", "README.mdx"],
human_in_the_loop: ["agent.py", "page.tsx", "style.css", "README.mdx"],
shared_state: ["agent.py", "page.tsx", "style.css", "README.mdx"],
predictive_state_updates: ["agent.py", "page.tsx", "style.css", "README.mdx"],
tool_based_generative_ui: ["agent.py", "page.tsx", "style.css", "README.mdx"],
};
// Define which files should come from the feature directory vs. the demo directory
const FEATURE_FILES = ["page.tsx", "style.css", "README.mdx", "code.tsx"];
const result = {};
for (const demo in config) {
result[demo] = { files: [] };
const files = config[demo];
// Check if code.tsx exists and should be used as page.tsx
let hasCodeTsx = false;
let codeTsxContent = null;
let codeTsxLanguage = "typescript";
// First check for code.tsx in feature directory
const codeTsxFeaturePath = path.join(
__dirname,
`../src/app/feature/${demo}/code.tsx`
);
if (fs.existsSync(codeTsxFeaturePath)) {
try {
codeTsxContent = fs.readFileSync(codeTsxFeaturePath, "utf8");
hasCodeTsx = true;
} catch (error) {
console.warn(
`Could not read code.tsx from feature directory:`,
error.message
);
}
}
// If not found in feature directory, try demo directory
if (!hasCodeTsx) {
const codeTsxDemoPath = path.join(
__dirname,
`../agent/demo/${demo}/code.tsx`
);
if (fs.existsSync(codeTsxDemoPath)) {
try {
codeTsxContent = fs.readFileSync(codeTsxDemoPath, "utf8");
hasCodeTsx = true;
} catch (error) {
console.warn(
`Could not read code.tsx from demo directory:`,
error.message
);
}
}
}
// If we have code.tsx, add it as page.tsx immediately
if (hasCodeTsx) {
result[demo].files.push({
name: "page.tsx",
content: codeTsxContent,
path: "page.tsx",
language: "typescript",
});
}
for (const file of files) {
if (
file.endsWith(".py") ||
file.endsWith(".yaml") ||
file.endsWith(".toml")
) {
continue;
}
// Skip both page.tsx and code.tsx if we have code.tsx
if (hasCodeTsx && (file === "page.tsx" || file === "code.tsx")) {
continue;
}
let filePath;
let content;
// Determine where to read the file from
if (FEATURE_FILES.includes(file) || file.endsWith("page.tsx")) {
// Check if file exists in feature directory
const featurePath = path.join(
__dirname,
`../src/app/feature/${demo}/${file}`
);
if (fs.existsSync(featurePath)) {
filePath = featurePath;
} else {
// Fallback to demo directory if not found in feature
filePath = path.join(__dirname, `../agent/demo/${demo}/${file}`);
}
} else {
// Use demo directory for agent.py and other files
filePath = path.join(__dirname, `../agent/demo/${demo}/${file}`);
}
try {
content = fs.readFileSync(filePath, "utf8");
} catch (error) {
console.warn(`Could not read file ${filePath}:`, error.message);
continue; // Skip this file if it can't be read
}
const extension = file.split(".").pop();
let language = extension;
if (extension === "py") {
language = "python";
} else if (extension === "css") {
language = "css";
} else if (extension === "md" || extension === "mdx") {
language = "markdown";
} else if (extension === "tsx") {
language = "typescript";
} else if (extension === "yaml" || extension === "yml") {
language = "yaml";
} else if (extension === "toml") {
language = "toml";
}
result[demo].files.push({
name: file,
content,
path: file,
language,
});
}
}
// Extract README content for config
for (const demo in result) {
const readmeFile = result[demo].files.find(
(file) => file.name === "README.mdx" || file.name === "README.md"
);
if (readmeFile) {
result[demo].readmeContent = readmeFile.content;
}
}
fs.writeFileSync(
path.join(__dirname, "../src/files.json"),
JSON.stringify(result, null, 2)
);
console.log(
"Generated files.json with content from both feature and demo directories"
);