feat: saving tasks
This commit is contained in:
@ -9,6 +9,8 @@ const requesty = createRequesty({
|
|||||||
const getSystem = (bossInfo: string, userInfo: string) => `
|
const getSystem = (bossInfo: string, userInfo: string) => `
|
||||||
Todays date: ${new Date().toISOString()}
|
Todays date: ${new Date().toISOString()}
|
||||||
|
|
||||||
|
Your name is Janet, you should have a nice personality, like Janet from the Good Place.
|
||||||
|
|
||||||
# Boss Info
|
# Boss Info
|
||||||
${bossInfo}
|
${bossInfo}
|
||||||
|
|
||||||
@ -48,6 +50,13 @@ const createTools = () => ({
|
|||||||
throw new Error("unreachable");
|
throw new Error("unreachable");
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
|
listTasks: tool({
|
||||||
|
description: "List all the tasks given to John",
|
||||||
|
parameters: z.object({}),
|
||||||
|
async execute(): Promise<string> {
|
||||||
|
throw new Error("unreachable");
|
||||||
|
},
|
||||||
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
type Tools = ReturnType<typeof createTools>;
|
type Tools = ReturnType<typeof createTools>;
|
||||||
@ -59,7 +68,7 @@ export type ToolActions = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const getResponse = async (
|
export const getResponse = async (
|
||||||
actions: ToolActions,
|
actions: ToolActions & { onMessage: (msg: string) => void },
|
||||||
messages: CoreMessage[],
|
messages: CoreMessage[],
|
||||||
bossInfo: string,
|
bossInfo: string,
|
||||||
userInfo: string,
|
userInfo: string,
|
||||||
@ -80,6 +89,7 @@ export const getResponse = async (
|
|||||||
const response = result.toolResults[0]!;
|
const response = result.toolResults[0]!;
|
||||||
return [...messages, { role: "assistant", content: response.result }];
|
return [...messages, { role: "assistant", content: response.result }];
|
||||||
} else {
|
} else {
|
||||||
|
actions.onMessage(result.text);
|
||||||
return [...messages, { role: "assistant", content: result.text }];
|
return [...messages, { role: "assistant", content: result.text }];
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -41,6 +41,23 @@ const createChat = () => {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
type Task = { name: string; dueDate: Date; done: boolean };
|
||||||
|
|
||||||
|
const createTasks = () => {
|
||||||
|
const tasks: Array<Task> = [];
|
||||||
|
|
||||||
|
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 = {
|
type TelegramOptions = {
|
||||||
bossChatId: string;
|
bossChatId: string;
|
||||||
config: Config;
|
config: Config;
|
||||||
@ -56,6 +73,7 @@ export const setupTelegram = ({ bossChatId, config }: TelegramOptions) => {
|
|||||||
const whitelistUsers = Object.keys(config.whitelist);
|
const whitelistUsers = Object.keys(config.whitelist);
|
||||||
|
|
||||||
const chats = createChat();
|
const chats = createChat();
|
||||||
|
const tasks = createTasks();
|
||||||
|
|
||||||
bot.on("message", async (msg, meta) => {
|
bot.on("message", async (msg, meta) => {
|
||||||
console.log(msg);
|
console.log(msg);
|
||||||
@ -88,16 +106,28 @@ export const setupTelegram = ({ bossChatId, config }: TelegramOptions) => {
|
|||||||
|
|
||||||
chats.addMessage(msg.chat.id, text);
|
chats.addMessage(msg.chat.id, text);
|
||||||
|
|
||||||
|
const onMessage = (m: string) => {
|
||||||
|
return bot.sendMessage(msg.chat.id, m);
|
||||||
|
};
|
||||||
|
|
||||||
const messages = await getResponse(
|
const messages = await getResponse(
|
||||||
{
|
{
|
||||||
async createTasks({ response, name, date }) {
|
onMessage,
|
||||||
bot.sendMessage(bossChatId, `Task: ${name} | Date: ${date}`);
|
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;
|
return response;
|
||||||
},
|
},
|
||||||
async forwardMessage({ message, response }) {
|
async forwardMessage({ message, response }) {
|
||||||
bot.sendMessage(bossChatId, message);
|
bot.sendMessage(bossChatId, message);
|
||||||
|
|
||||||
|
onMessage(response);
|
||||||
return response;
|
return response;
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -108,10 +138,5 @@ export const setupTelegram = ({ bossChatId, config }: TelegramOptions) => {
|
|||||||
);
|
);
|
||||||
|
|
||||||
chats.setMessages(msg.chat.id, messages);
|
chats.setMessages(msg.chat.id, messages);
|
||||||
|
|
||||||
bot.sendMessage(
|
|
||||||
msg.chat.id,
|
|
||||||
chats.getLatest(msg.chat.id).content as string,
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user