-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathAgents.js
220 lines (193 loc) · 6.25 KB
/
Agents.js
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
const { Anthropic } = require("@anthropic-ai/sdk")
const fs = require("fs")
const path = require("path")
const OpenAI = require("openai")
const { Particle } = require("scrollsdk/products/Particle.js")
class FolderPrompt {
constructor(userPrompt, existingFolders, agent, whatKind, domainSuffix) {
this.userPrompt = userPrompt
this.existingFolders = existingFolders
this.agent = agent
this.what = whatKind
this.domainSuffix = "." + domainSuffix.replace(/^\./, "")
this.systemPrompt = this.makePrompt(userPrompt, domainSuffix)
}
setResponse(response) {
this.response = response
return this
}
makePrompt(userPrompt, domainSuffix) {
const domainExpression = `(domain${domainSuffix} here)`
const domainPrompt = `First suggest a short, memorable domain name ending in ${domainSuffix} that represents this website. Then provide the website files. Use this exact format:
---domain---
${domainExpression}`
let basePrompt = fs.readFileSync(path.join(__dirname, "prompts", this.what + ".scroll"), "utf8")
basePrompt = basePrompt.replaceAll("USER_PROMPT", userPrompt)
basePrompt = basePrompt.replaceAll("DOMAIN_PROMPT", domainPrompt)
basePrompt = basePrompt.replaceAll("DOMAIN_EXPRESSION", domainExpression)
return basePrompt
}
setDebugLog(completion) {
this.completion = completion
}
get parsedResponse() {
const { response } = this
const files = {}
let currentFile = null
let currentContent = []
let suggestedDomain = ""
for (const line of response.split("\n")) {
if (line.startsWith("---") && line.endsWith("---")) {
if (currentFile === "domain" && currentContent.length > 0) {
suggestedDomain = currentContent.join("").trim()
} else if (currentFile && currentContent.length > 0) {
files[currentFile] = currentContent.join("\n")
}
currentContent = []
const fileName = line.replace(/---/g, "")
if (fileName === "end") break
currentFile = fileName
} else if (currentFile) {
currentContent.push(line)
}
}
if (!suggestedDomain) suggestedDomain = "error"
const { domainSuffix } = this
// Ensure the suggested domain ends with domainSuffix
if (!suggestedDomain.endsWith(domainSuffix)) suggestedDomain = suggestedDomain.replace(domainSuffix, "") + domainSuffix
// If domain is taken, add numbers until we find a free one
let finalDomain = suggestedDomain
let counter = 1
while (this.existingFolders[finalDomain]) {
const baseName = suggestedDomain.replace(domainSuffix, "")
finalDomain = `${baseName}${counter}${domainSuffix}`
counter++
}
// Add a default README
files["readme.scroll"] = `# ${finalDomain}
Prompt: ${this.what}
Agent: ${this.agent.name}
Model: ${this.agent.model}
## User prompt
${this.userPrompt}
## System prompt
${this.systemPrompt}`
return {
folderName: finalDomain,
files
}
}
}
class AbstractAgent {
constructor(apiKey, hubFolder) {
this.apiKey = apiKey
this.hubFolder = hubFolder
}
}
class Claude extends AbstractAgent {
get client() {
if (!this._client)
this._client = new Anthropic({
apiKey: this.apiKey
})
return this._client
}
name = "claude"
model = "claude-3-5-sonnet-20241022"
async do(prompt) {
console.log("Sending prompt to claude")
const { client } = this
// Call Claude API
const completion = await client.messages.create({
model: this.model,
max_tokens: 4000,
temperature: 0.7,
messages: [{ role: "user", content: prompt.systemPrompt }]
})
// Parse Claude's response into domain and files
const response = completion.content[0].text
prompt.setDebugLog(completion)
return prompt.setResponse(response)
}
}
class DeepSeek extends AbstractAgent {
get client() {
if (!this._client)
this._client = new OpenAI({
baseURL: "https://api.deepseek.com",
apiKey: this.apiKey
})
return this._client
}
model = "deepseek-chat"
name = "deepseek"
async do(prompt) {
console.log("Sending prompt to deepseek")
const completion = await this.client.chat.completions.create({
messages: this.getMessages(prompt),
model: this.model
})
const response = completion.choices[0].message.content
prompt.setDebugLog(completion)
return prompt.setResponse(response)
}
getMessages(prompt) {
return [{ role: "system", content: prompt.systemPrompt }]
}
}
class DeepSeekReasoner extends DeepSeek {
model = "deepseek-reasoner"
name = "deepseekreasoner"
getMessages(prompt) {
return [
{ role: "system", content: prompt.systemPrompt },
{ role: "user", content: prompt.userPrompt }
]
}
}
class Agents {
constructor(hub) {
this.hubFolder = hub.hubFolder
this.config = hub.config
this.agents = {}
const availableAgents = "claude deepseek".split(" ")
availableAgents.forEach(agent => this.loadAgent(agent))
}
loadAgent(name) {
const { hubFolder } = this
const apiKey = this.config.get(name)
if (!apiKey) {
console.log(`No ${name} API key found. Skipping ${name} agent`)
return
} else {
console.log(`${name} agent loaded.`)
}
const AgentClasses = { claude: [Claude], deepseek: [DeepSeek, DeepSeekReasoner] }
const agentConstructors = AgentClasses[name]
agentConstructors.forEach(con => {
const agent = new con(apiKey, hubFolder)
this.agents[agent.name] = agent
})
}
get allAgents() {
return Object.values(this.agents)
}
async createFolderNameAndFilesFromPrompt(userPrompt, existingFolders, agentName, promptTemplate, domainSuffix) {
const agent = this.agents[agentName] || this.allAgents[0]
const prompt = new FolderPrompt(userPrompt, existingFolders, agent, promptTemplate, domainSuffix)
if (!agent) throw new Error(`Agent ${agentName} not found. Is API key set?`)
await agent.do(prompt)
return prompt
}
// todo: wire this up
async createMultipleFoldersFromPrompt(userPrompt, existingFolders) {
return await Promise.all(
this.allAgents.map(async agent => {
const prompt = new SimpleCreationPrompt(userPrompt, existingFolders)
await agent.do(prompt)
return prompt
})
)
}
}
module.exports = { Agents }