Last active
August 29, 2024 14:22
-
-
Save morion4000/6d8ca74afdb9c2e3fedfed8f91922f6c to your computer and use it in GitHub Desktop.
getsite-ssh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const axios = require("axios"); | |
const UNDERLINE = "\x1b[4m"; | |
const ITALIC = "\x1b[3m"; | |
const BOLD = "\x1b[1m"; | |
const RESET = "\x1b[0m"; | |
const RUN_CHECK_INTERVAL = 1000; | |
const API_URL = "https://api.getsite.ai/v1"; | |
const MAX_INPUT_LENGTH = 1000; | |
let threadId = null; | |
let runInterval = null; | |
let running = false; | |
let lastMessageId = null; | |
process.stdout.write("\x1Bc"); | |
process.stdout.write(` | |
_______ _______ _______ _______ ___ _______ _______ _______ ___ | |
| || || || || | | || | | _ || | | |
| ___|| ___||_ _|| _____|| | |_ _|| ___| | |_| || | | |
| | __ | |___ | | | |_____ | | | | | |___ | || | | |
| || || ___| | | |_____ || | | | | ___| ___ | || | | |
| |_| || |___ | | _____| || | | | | |___ | | | _ || | | |
|_______||_______| |___| |_______||___| |___| |_______||___| |__| |__||___| | |
`); | |
process.stdout.write( | |
`Chatbot that helps you create one-page websites. Learn more at ${UNDERLINE}https://www.getsite.ai${RESET}.\n` | |
); | |
process.stdout.write(`Type ${BOLD}exit${RESET} to quit.\n\n`); | |
process.stdin.setEncoding("utf8"); | |
async function processRun() { | |
const response = await axios.get( | |
`${API_URL}/public/threads/${threadId}/messages?last_message_id=${lastMessageId}` | |
); | |
const messages = response.data; | |
let content = "Unable to process your request."; | |
let thinking = true; | |
let generated = false; | |
for (const message of messages) { | |
if (message.role === "user") { | |
continue; | |
} | |
if (message.content) { | |
lastMessageId = message._id; | |
content = message.content; | |
if (thinking) { | |
process.stdout.moveCursor(0, -1); | |
process.stdout.clearLine(1); | |
thinking = false; | |
} | |
process.stdout.write(`${BOLD}Bot:${RESET} ${content}\n`); | |
} | |
if (message.tool_calls && message.tool_calls.length > 0) { | |
const call = message.tool_calls[0]; | |
const func = call.function.name; | |
const args = JSON.parse(call.function.arguments); | |
switch (func) { | |
case "ask_question": | |
content = args.question; | |
break; | |
case "generate_name": | |
content = `Generated names: ${BOLD}${args.names.join(", ")}${RESET}`; | |
break; | |
case "generate_design": | |
content = `${ITALIC}Generated a design${RESET}`; | |
break; | |
case "pick_template": | |
content = `${ITALIC}Found a template${RESET}`; | |
break; | |
case "check_domain": | |
content = `Checking domain (${BOLD}${args.name}${RESET})...`; | |
break; | |
case "generate_site": | |
content = `${BOLD}Congratulations!${RESET} Your site is almost ready: ${UNDERLINE}https://app.getsite.ai/chat?thread=${threadId}${RESET}`; | |
generated = true; | |
break; | |
default: | |
content = `${ITALIC}Unable to process the response.${RESET}`; | |
break; | |
} | |
lastMessageId = message._id; | |
if (thinking) { | |
process.stdout.moveCursor(0, -1); | |
process.stdout.clearLine(1); | |
thinking = false; | |
} | |
if (!generated) { | |
process.stdout.write(`${BOLD}Bot:${RESET} ${content}\n`); | |
} | |
} | |
} | |
if (generated) { | |
process.stdout.write(`\n${content}\n\n`); | |
process.exit(0); | |
} | |
showPrompt(); | |
} | |
function poolRun(runId) { | |
if (runInterval) { | |
clearInterval(runInterval); | |
} | |
runInterval = setInterval(async () => { | |
const run = await axios.get(`${API_URL}/public/runs/${runId}`); | |
if (["completed", "failed"].includes(run.data.status)) { | |
if (runInterval) { | |
clearInterval(runInterval); | |
} | |
running = false; | |
processRun(); | |
} | |
}, RUN_CHECK_INTERVAL); | |
} | |
async function createThread() { | |
const thread = await axios.post(`${API_URL}/public/threads`, { | |
type: "chat", | |
}); | |
const message = `Welcome! How can I help you?`; | |
const newMessage = await axios.post(`${API_URL}/public/messages`, { | |
thread_id: thread.data._id, | |
message, | |
role: "assistant", | |
}); | |
if (!newMessage.data) { | |
throw new Error("Failed to create message"); | |
} | |
lastMessageId = newMessage.data._id; | |
threadId = thread.data._id; | |
process.stdout.write(`${BOLD}Bot:${RESET} ${message}\n`); | |
} | |
async function sendMessage(message) { | |
try { | |
const newMessage = await axios.post(`${API_URL}/public/messages`, { | |
thread_id: threadId, | |
message: message, | |
role: "user", | |
}); | |
if (!newMessage.data._id) { | |
throw new Error("Failed to send message"); | |
} | |
const newRun = await axios.post(`${API_URL}/public/runs`, { | |
thread_id: threadId, | |
message_id: newMessage.data._id, | |
agent: "chat", | |
choices: 1, | |
mode: "quick", | |
}); | |
if (!newRun.data._id) { | |
throw new Error("Failed to create run"); | |
} | |
running = true; | |
poolRun(newRun.data._id); | |
process.stdout.write(`${BOLD}Bot:${RESET} ${ITALIC}Thinking...${RESET}\n`); | |
} catch (error) { | |
process.stdout.write( | |
`${BOLD}Bot:${RESET} I'm having trouble connecting to my brain right now.\n` | |
); | |
} | |
} | |
function showPrompt(prompt = `${BOLD}You:${RESET} `) { | |
process.stdout.write(prompt); | |
} | |
process.stdin.on("data", async function (input) { | |
const userInput = input.trim(); | |
if (running) { | |
return; | |
} | |
if (userInput.length > MAX_INPUT_LENGTH) { | |
process.stdout.write( | |
`${BOLD}Bot:${RESET} Input too long. Limit your message to ${MAX_INPUT_LENGTH} characters.\n` | |
); | |
showPrompt(); | |
return; | |
} | |
if (userInput.toLowerCase() === "exit") { | |
process.stdout.write(`${BOLD}Goodbye!${RESET}\n`); | |
process.exit(0); | |
} else if (userInput !== "") { | |
await sendMessage(userInput); | |
} else { | |
showPrompt(); | |
} | |
}); | |
process.stdin.on("end", function () { | |
process.stdout.write(`${BOLD}Session ended.${RESET}\n`); | |
}); | |
process.stdin.on("error", function (error) { | |
process.stdout.write(`${BOLD}Error:${RESET} ${error}\n`); | |
process.exit(1); | |
}); | |
process.on("SIGINT", function () { | |
process.stdout.write(`${BOLD}Received SIGINT.${RESET} Exiting...\n`); | |
process.exit(0); | |
}); | |
createThread().then(showPrompt).catch(console.error); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment