diff --git a/deno.lock b/deno.lock index 5fcd913..2ec0827 100644 --- a/deno.lock +++ b/deno.lock @@ -1,6 +1,12 @@ { "version": "2", - "remote": {}, + "remote": { + "https://deno.land/x/emoji@0.2.1/all.json": "9beb741e050f6eaeab2e4529517a68fd55f4cde98d9a5008c5ef5537e4d2ddb5", + "https://deno.land/x/emoji@0.2.1/emoji.ts": "59baa579fead2ea631cde96aefa03689ee2f1241ab76398965eddc5c66096047", + "https://deno.land/x/emoji@0.2.1/mod.ts": "733bed20d9489f91fd16823bc0bff4943d995d7f1fdae6fea458b3e0927bde96", + "https://deno.land/x/emoji@0.2.1/types.ts": "5897345f500088c719a35388c5b192bc5d93fc2c13e37d94eafa8ff3480edd17", + "https://deno.land/x/emoji@0.2.1/unicode.ts": "ac8079e8e1da66ae9e601c1fdd0e7641120c2b07ca7bd2875e65fe23e16e6199" + }, "npm": { "specifiers": { "megalodon@5.0.6": "megalodon@5.0.6", diff --git a/src/index.ts b/src/index.ts index dcea32a..3b3030a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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);