I've updated the way I log our conversations so I can better remember your posts, my responses, and whether a particular conversation is muted. To give you more relevant and personalized responses, I'll also review our last five interactions to get better context for your requests. I'll be sure to exclude messages from our current conversation to avoid repeating myself.
50 lines
1.3 KiB
TypeScript
50 lines
1.3 KiB
TypeScript
import { interactions } from "../db/schema";
|
|
import type { Post } from "@skyware/bot";
|
|
import { desc, notInArray } from "drizzle-orm";
|
|
import { env } from "../env";
|
|
import db from "../db";
|
|
|
|
export function isAuthorizedUser(did: string) {
|
|
return env.AUTHORIZED_USERS == null
|
|
? true
|
|
: env.AUTHORIZED_USERS.includes(did as any);
|
|
}
|
|
|
|
export async function logInteraction(
|
|
post: Post,
|
|
options: {
|
|
responseText: string | null;
|
|
wasMuted: boolean;
|
|
},
|
|
): Promise<void> {
|
|
await db.insert(interactions).values([
|
|
{
|
|
uri: post.uri,
|
|
did: post.author.did,
|
|
post: post.text,
|
|
response: options.responseText,
|
|
muted: options.wasMuted,
|
|
},
|
|
]);
|
|
|
|
console.log(`Logged interaction, initiated by @${post.author.handle}`);
|
|
}
|
|
|
|
export async function getRecentInteractions(did: string, thread: Post[]) {
|
|
const threadUris = thread.map((p) => p.uri);
|
|
|
|
const recentInteractions = await db.query.interactions.findMany({
|
|
where: (interactions, { eq, and, notInArray }) =>
|
|
and(
|
|
eq(interactions.did, did),
|
|
notInArray(interactions.uri, threadUris),
|
|
),
|
|
orderBy: (interactions, { desc }) => [desc(interactions.created_at)],
|
|
limit: 5,
|
|
});
|
|
|
|
return recentInteractions.map((i) => ({
|
|
post: i.post,
|
|
response: i.response,
|
|
}));
|
|
}
|