forked from daveyplate/better-auth-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode-sync.ts
125 lines (101 loc) · 3.96 KB
/
code-sync.ts
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
import fs from "node:fs"
import path from "node:path"
import dotenv from "dotenv"
import ignore, { type Ignore } from "ignore"
dotenv.config({ path: ".env" })
type CodeSyncConfig = {
base_url: string
agent_id: string
instructions: string
directory: string
gitignore: boolean
extensions: string[]
ignore: string[]
}
// Load the configuration from code-sync.config.json
async function loadConfig() {
const configPath = path.join(process.cwd(), "code-sync.config.json")
if (!fs.existsSync(configPath)) {
throw new Error("Configuration file not found: code-sync.config.json")
}
const configContent = await fs.promises.readFile(configPath, "utf8")
return JSON.parse(configContent)
}
// Function to read .gitignore files and create an ignore instance
async function getIgnoredFiles(ig: Ignore, cwd: string) {
const gitignorePath = path.join(cwd, ".gitignore")
// Check if .gitignore file exists
if (fs.existsSync(gitignorePath)) {
const gitignoreContent = await fs.promises.readFile(gitignorePath, "utf8")
ig.add(gitignoreContent)
}
return ig
}
// Function to recursively append file contents to a large string
async function appendFilesRecursively(dir: string, config: CodeSyncConfig, ig: Ignore) {
let largeString = ""
// Read directory contents
const files = await fs.promises.readdir(dir, { withFileTypes: true })
for (const file of files) {
const filePath = path.join(dir, file.name)
if (ig.ignores(path.relative(process.cwd(), filePath))) {
continue // Skip ignored files and directories
}
if (file.isDirectory()) {
largeString += await appendFilesRecursively(filePath, config, ig)
} else if (file.isFile()) {
// Check file extension
const ext = path.extname(file.name).slice(1) // Get extension without the dot
if (config.extensions.includes(ext)) {
// Generate relative file path from the current working directory
const relativeFilePath = path.relative(process.cwd(), filePath)
// Append file path and name
largeString += `\n\n/${relativeFilePath}\n\n`
// Read file content and append to large string
const fileContent = await fs.promises.readFile(filePath, "utf8")
largeString += fileContent
}
}
}
return largeString
}
// Function to send a PATCH request
const codeSync = async () => {
try {
const config = await loadConfig()
const currentDir = path.resolve(config.directory)
const ig = ignore()
if (config.gitignore) {
await getIgnoredFiles(ig, currentDir)
}
// Add additional ignore rules from config
ig.add(config.ignore)
ig.add(["code-sync.js", "code-sync.config.json"])
let instructions = `${config.instructions}\n\nContext:`
instructions += await appendFilesRecursively(currentDir, config, ig)
const currentTime = new Date().toLocaleTimeString("en-US", {
hour: "numeric",
minute: "numeric",
second: "numeric"
})
try {
const response = await fetch(`${config.base_url}/api/agents/${config.agent_id}`, {
method: "PATCH",
headers: {
Authorization: `Bearer ${process.env.AQUARION_API_KEY}`,
"Content-Type": "application/json"
},
body: JSON.stringify({ instructions })
})
if (!response.ok) {
throw new Error(`HTTP error ${response.status}: ${response.statusText}`)
}
console.info(currentTime, "Code Synced Successfully")
} catch (error) {
console.error(currentTime, "Code Sync Failed", (error as Error).message)
}
} catch (error) {
console.error("Error:", (error as Error).message)
}
}
codeSync()