-
Notifications
You must be signed in to change notification settings - Fork 2
/
script.js
78 lines (63 loc) · 1.76 KB
/
script.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
import { createRenderer } from "./renderer.js";
import { createAgent } from "./agent.js";
import { isLive } from "./ableton.js";
document.getElementById("enterChat").addEventListener(
"click",
async function () {
const key = document.getElementById("apiKey").value.trim();
if (key === "") {
alert("Please enter a key.");
return;
}
const live = await isLive();
if (!live) {
alert(
"Ableton is not live. Please make sure AbletonOSC is installed. See the Vroom Live Github README for more information.",
);
return;
}
const messages = [
{
role: "system",
content:
"You are a music producer controlling Ableton Live. Use the tools available to respond to the user as best as possible. It's critical you respond in exclusively lower case.",
},
];
document.getElementById("auth-section").style.display = "none";
document.getElementById("chat-section").style.display = "flex";
const renderMessages = createRenderer({
getMessages: () => messages,
element: document.querySelector("#messages"),
});
const runAgent = createAgent({
getMessages: () => messages,
appendMessage: (message) => {
messages.push(message);
renderMessages();
},
openai: new OpenAI({
apiKey: key,
dangerouslyAllowBrowser: true,
}),
});
async function sendMessage() {
const content = $messageInput.value.trim();
$messageInput.value = "";
if (!content) return;
messages.push({
role: "user",
content,
});
renderMessages();
await runAgent();
}
const $messageInput = document.getElementById("messageInput");
// allow enter
$messageInput.addEventListener("keydown", async (event) => {
if (event.key === "Enter") {
event.preventDefault();
await sendMessage();
}
});
},
);