Skip to content

Commit d2ecc09

Browse files
committed
support running without a database
1 parent 3ca0a3f commit d2ecc09

File tree

1 file changed

+33
-19
lines changed

1 file changed

+33
-19
lines changed

packages/main/src/telegram_bot.ts

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -126,15 +126,22 @@ export default class TelegramBot extends TelegramApi {
126126
prompt = "";
127127
}
128128

129-
const { results } = await this.db
130-
.prepare("SELECT * FROM Messages WHERE userId=?")
131-
.bind(update.message?.from.id)
132-
.all();
129+
let _results;
130+
if (this.db) {
131+
_results = await this.db
132+
.prepare("SELECT * FROM Messages WHERE userId=?")
133+
.bind(update.message?.from.id)
134+
.all();
135+
}
136+
const results = _results?.results;
133137

134-
const old_messages = results.map((col) => ({
135-
role: "system",
136-
content: col.content,
137-
}));
138+
let old_messages;
139+
if (results) {
140+
old_messages = results.map((col) => ({
141+
role: "system",
142+
content: col.content,
143+
}));
144+
}
138145

139146
const { response } = await ai.run("@cf/meta/llama-2-7b-chat-int8", {
140147
messages: [
@@ -150,21 +157,28 @@ export default class TelegramBot extends TelegramApi {
150157
role: "system",
151158
content: `your source code is at https://github.com/codebam/cf-workers-telegram-bot`,
152159
},
153-
...old_messages,
160+
...(() => {
161+
if (old_messages) {
162+
return old_messages;
163+
}
164+
return [];
165+
})(),
154166
{ role: "user", content: prompt },
155167
],
156168
});
157169

158-
const { success } = await this.db
159-
.prepare("INSERT INTO Messages (id, userId, content) VALUES (?, ?, ?)")
160-
.bind(
161-
crypto.randomUUID(),
162-
update.message?.from.id,
163-
"[INST] " + prompt + " [/INST]" + "\n" + response
164-
)
165-
.run();
166-
if (!success) {
167-
console.log("failed to insert data into d1");
170+
if (this.db) {
171+
const { success } = await this.db
172+
.prepare("INSERT INTO Messages (id, userId, content) VALUES (?, ?, ?)")
173+
.bind(
174+
crypto.randomUUID(),
175+
update.message?.from.id,
176+
"[INST] " + prompt + " [/INST]" + "\n" + response
177+
)
178+
.run();
179+
if (!success) {
180+
console.log("failed to insert data into d1");
181+
}
168182
}
169183

170184
if (response === "") {

0 commit comments

Comments
 (0)