|
|
|
@ -1,5 +1,6 @@
|
|
|
|
|
import generator, { Entity } from "npm:megalodon@5.0.6";
|
|
|
|
|
import { Configuration, OpenAIApi } from "npm:openai@3.2.1";
|
|
|
|
|
import * as emojiConverter from "https://deno.land/x/emoji@0.2.1/mod.ts";
|
|
|
|
|
import config from "../config.ts";
|
|
|
|
|
|
|
|
|
|
const fedi = generator.default(
|
|
|
|
@ -57,7 +58,41 @@ function generatePostPrompt() {
|
|
|
|
|
}, you would post on your personal Twitter account using the following format: {"username": "${config.character.username}", "name": "${config.character.name}", "content": [ALL POST CONTENT]}.`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let respondedNotificationIds: string[] = JSON.parse(
|
|
|
|
|
async function postStatus(
|
|
|
|
|
messagesArray: {
|
|
|
|
|
role: "system" | "assistant" | "user";
|
|
|
|
|
content: string;
|
|
|
|
|
}[],
|
|
|
|
|
visibility: "public" | "unlisted" | "private" | "direct",
|
|
|
|
|
reply_id?: string,
|
|
|
|
|
) {
|
|
|
|
|
const response = await openai.createChatCompletion({
|
|
|
|
|
model: "gpt-3.5-turbo",
|
|
|
|
|
messages: messagesArray,
|
|
|
|
|
temperature: 0.9,
|
|
|
|
|
});
|
|
|
|
|
const postContent = response.data.choices[0].message?.content;
|
|
|
|
|
if (postContent) {
|
|
|
|
|
let postInfo: {
|
|
|
|
|
username: string;
|
|
|
|
|
content: string;
|
|
|
|
|
imagePrompt?: string;
|
|
|
|
|
};
|
|
|
|
|
try {
|
|
|
|
|
postInfo = JSON.parse(postContent);
|
|
|
|
|
fedi.postStatus(emojiConverter.emojify(postInfo.content), {
|
|
|
|
|
visibility,
|
|
|
|
|
in_reply_to_id: reply_id,
|
|
|
|
|
});
|
|
|
|
|
lastPost = Math.floor(Date.now() / 1000);
|
|
|
|
|
console.log("Posted post!");
|
|
|
|
|
} catch {
|
|
|
|
|
console.log("Failed to post post; will try again next time.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const respondedNotificationIds: string[] = JSON.parse(
|
|
|
|
|
Deno.readTextFileSync("_responded_notifs.json"),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
@ -74,29 +109,7 @@ while (true) {
|
|
|
|
|
role: "user",
|
|
|
|
|
content: prompt,
|
|
|
|
|
});
|
|
|
|
|
const response = await openai.createChatCompletion({
|
|
|
|
|
model: "gpt-3.5-turbo",
|
|
|
|
|
messages: newMessages,
|
|
|
|
|
temperature: 0.9,
|
|
|
|
|
});
|
|
|
|
|
const postContent = response.data.choices[0].message?.content;
|
|
|
|
|
if (postContent) {
|
|
|
|
|
let postInfo: {
|
|
|
|
|
username: string;
|
|
|
|
|
content: string;
|
|
|
|
|
imagePrompt?: string;
|
|
|
|
|
};
|
|
|
|
|
try {
|
|
|
|
|
postInfo = JSON.parse(postContent);
|
|
|
|
|
fedi.postStatus(postInfo.content, {
|
|
|
|
|
visibility: "private",
|
|
|
|
|
});
|
|
|
|
|
lastPost = Math.floor(Date.now() / 1000);
|
|
|
|
|
console.log("Posted post!");
|
|
|
|
|
} catch {
|
|
|
|
|
console.log("Failed to post post; will try again next time.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
await postStatus(newMessages, "public");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Reply handling
|
|
|
|
@ -157,47 +170,21 @@ while (true) {
|
|
|
|
|
newMessages.push({
|
|
|
|
|
role: "user",
|
|
|
|
|
content:
|
|
|
|
|
`{"username": ${status.account.username}@${
|
|
|
|
|
`{"username": "${status.account.username}@${
|
|
|
|
|
new URL(status.account.url).host
|
|
|
|
|
}, "name": "${status.account.display_name}", "content": "${status.plain_content}"}`,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
console.log(newMessages);
|
|
|
|
|
const response = await openai.createChatCompletion({
|
|
|
|
|
model: "gpt-3.5-turbo",
|
|
|
|
|
messages: newMessages,
|
|
|
|
|
temperature: 0.9,
|
|
|
|
|
});
|
|
|
|
|
const postContent = response.data.choices[0].message
|
|
|
|
|
?.content;
|
|
|
|
|
console.log(postContent);
|
|
|
|
|
if (postContent) {
|
|
|
|
|
let postInfo: {
|
|
|
|
|
username: string;
|
|
|
|
|
content: string;
|
|
|
|
|
imagePrompt?: string;
|
|
|
|
|
};
|
|
|
|
|
try {
|
|
|
|
|
postInfo = JSON.parse(postContent);
|
|
|
|
|
fedi.postStatus(postInfo.content, {
|
|
|
|
|
visibility: status.visibility,
|
|
|
|
|
in_reply_to_id: notif.status?.id,
|
|
|
|
|
});
|
|
|
|
|
lastPost = Math.floor(Date.now() / 1000);
|
|
|
|
|
console.log("Posted post!");
|
|
|
|
|
respondedNotificationIds.push(notif.id);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.log(
|
|
|
|
|
"Failed to post post; will try again next time.\n" +
|
|
|
|
|
e,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
await postStatus(
|
|
|
|
|
newMessages,
|
|
|
|
|
status.visibility,
|
|
|
|
|
notif.status?.id,
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
// assume top-level
|
|
|
|
|
const prompt =
|
|
|
|
|
`Reply to the following Tweet from your own account: {"username": ${status.account.username}@${
|
|
|
|
|
`Reply to the following Tweet from your own account: {"username": "${status.account.username}@${
|
|
|
|
|
new URL(status.account.url).host
|
|
|
|
|
}, "name": "${status.account.display_name}", "content": "${status.plain_content}"}. Use the same JSON schema.`;
|
|
|
|
|
const newMessages: {
|
|
|
|
@ -208,35 +195,11 @@ while (true) {
|
|
|
|
|
role: "user",
|
|
|
|
|
content: prompt,
|
|
|
|
|
});
|
|
|
|
|
const response = await openai.createChatCompletion({
|
|
|
|
|
model: "gpt-3.5-turbo",
|
|
|
|
|
messages: newMessages,
|
|
|
|
|
temperature: 0.9,
|
|
|
|
|
});
|
|
|
|
|
const postContent = response.data.choices[0].message
|
|
|
|
|
?.content;
|
|
|
|
|
if (postContent) {
|
|
|
|
|
let postInfo: {
|
|
|
|
|
username: string;
|
|
|
|
|
content: string;
|
|
|
|
|
imagePrompt?: string;
|
|
|
|
|
};
|
|
|
|
|
try {
|
|
|
|
|
postInfo = JSON.parse(postContent);
|
|
|
|
|
fedi.postStatus(postInfo.content, {
|
|
|
|
|
visibility: status.visibility,
|
|
|
|
|
in_reply_to_id: status.id,
|
|
|
|
|
});
|
|
|
|
|
lastPost = Math.floor(Date.now() / 1000);
|
|
|
|
|
console.log("Posted post!");
|
|
|
|
|
respondedNotificationIds.push(notif.id);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.log(
|
|
|
|
|
"Failed to post post; will try again next time.\n" +
|
|
|
|
|
e,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
await postStatus(
|
|
|
|
|
newMessages,
|
|
|
|
|
status.visibility,
|
|
|
|
|
status.id,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
respondedNotificationIds.push(notif.id);
|
|
|
|
|