diff --git a/src/ai/index.ts b/src/ai/index.ts index 0fdebf7..074562d 100644 --- a/src/ai/index.ts +++ b/src/ai/index.ts @@ -9,6 +9,8 @@ const requesty = createRequesty({ const getSystem = (bossInfo: string, userInfo: string) => ` Todays date: ${new Date().toISOString()} +Your name is Janet, you should have a nice personality, like Janet from the Good Place. + # Boss Info ${bossInfo} @@ -48,6 +50,13 @@ const createTools = () => ({ throw new Error("unreachable"); }, }), + listTasks: tool({ + description: "List all the tasks given to John", + parameters: z.object({}), + async execute(): Promise { + throw new Error("unreachable"); + }, + }), }); type Tools = ReturnType; @@ -59,7 +68,7 @@ export type ToolActions = { }; export const getResponse = async ( - actions: ToolActions, + actions: ToolActions & { onMessage: (msg: string) => void }, messages: CoreMessage[], bossInfo: string, userInfo: string, @@ -80,6 +89,7 @@ export const getResponse = async ( const response = result.toolResults[0]!; return [...messages, { role: "assistant", content: response.result }]; } else { + actions.onMessage(result.text); return [...messages, { role: "assistant", content: result.text }]; } }; diff --git a/src/telegram/index.ts b/src/telegram/index.ts index 27e37f2..613081f 100644 --- a/src/telegram/index.ts +++ b/src/telegram/index.ts @@ -41,6 +41,23 @@ const createChat = () => { }; }; +type Task = { name: string; dueDate: Date; done: boolean }; + +const createTasks = () => { + const tasks: Array = []; + + return { + createTask(t: Task) { + tasks.push(t); + }, + listTasks() { + return tasks + .filter((t) => !t.done) + .map((t) => `${t.name} due on ${t.dueDate.toISOString()}`); + }, + }; +}; + type TelegramOptions = { bossChatId: string; config: Config; @@ -56,6 +73,7 @@ export const setupTelegram = ({ bossChatId, config }: TelegramOptions) => { const whitelistUsers = Object.keys(config.whitelist); const chats = createChat(); + const tasks = createTasks(); bot.on("message", async (msg, meta) => { console.log(msg); @@ -88,16 +106,28 @@ export const setupTelegram = ({ bossChatId, config }: TelegramOptions) => { chats.addMessage(msg.chat.id, text); + const onMessage = (m: string) => { + return bot.sendMessage(msg.chat.id, m); + }; + const messages = await getResponse( { - async createTasks({ response, name, date }) { - bot.sendMessage(bossChatId, `Task: ${name} | Date: ${date}`); + onMessage, + async listTasks() { + tasks.listTasks().forEach(onMessage); + return "Tasks listed!"; + }, + async createTasks({ response, name, date }) { + tasks.createTask({ name, dueDate: new Date(date), done: false }); + + onMessage(response); return response; }, async forwardMessage({ message, response }) { bot.sendMessage(bossChatId, message); + onMessage(response); return response; }, }, @@ -108,10 +138,5 @@ export const setupTelegram = ({ bossChatId, config }: TelegramOptions) => { ); chats.setMessages(msg.chat.id, messages); - - bot.sendMessage( - msg.chat.id, - chats.getLatest(msg.chat.id).content as string, - ); }); };