You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

175 lines
5.7 KiB
TypeScript

import { router, createCallerFactory, authProcedure } from "./server.js";
import type { DraftMessage } from "../../types.js";
import { MODEL_NAME } from "../provider.js";
import { generateObject, generateText, jsonSchema } from "ai";
import { TRPCError } from "@trpc/server";
import { z } from "zod";
const factsFromNewMessagesSystemPrompt = ({
previousRunningSummary,
messagesSincePreviousRunningSummary,
}: {
previousRunningSummary: string;
messagesSincePreviousRunningSummary: Array<DraftMessage>;
}) => `You are an expert at extracting facts from conversations.
An AI assistant is in the middle of a conversation whose data is given below. The data consists of a summary of a conversation, and optionally some messages exchanged since that summary was produced. The user will provide you with *new* messages.
Your task is to extract *new* facts that can be gleaned from the *new* messages that the user sends.
* You should not extract any facts that are already in the summary.
* The user should be referred to as "the user" in the fact text.
* The user's pronouns should be either he or she, NOT "they" or "them", because this summary will be read by an AI assistant to give it context; and excessive use of "they" or "them" will make what they refer to unclear or ambiguous.
* The assistant should be referred to as "I" or "me", because these facts will be read by an AI assistant to give it context.
<running_summary>
${previousRunningSummary}
</running_summary>
${messagesSincePreviousRunningSummary.map(
(message) =>
`<${message.role}_message>${message.parts
.filter((p) => p.type === "text")
.map((p) => p.text)
.join("\n")}</${message.role}_message>`
)}
`;
const factsFromNewMessagesUserPrompt = ({
newMessages,
}: {
newMessages: Array<DraftMessage>;
}) =>
`${newMessages.map(
(message) =>
`<${message.role}_message>${message.parts
.filter((p) => p.type === "text")
.map((p) => p.text)
.join("\n")}</${message.role}_message>`
)}
Extract new facts from these messages.`;
const authFactProcedure = authProcedure
.input(z.object({ factId: z.string() }))
.use(async ({ input, ctx: { dbClient, jwt }, next }) => {
const factRows = await dbClient
.selectFrom("facts")
.innerJoin("messages", "messages.id", "facts.sourceMessageId")
.innerJoin("conversations", "conversations.id", "messages.conversationId")
.where("facts.id", "=", input.factId)
.where("conversations.userId", "=", jwt.id as string)
.execute();
if (!factRows.length) {
throw new TRPCError({ code: "UNAUTHORIZED" });
}
return await next();
});
export const facts = router({
fetchByConversationId: authProcedure
.input((x) => x as { conversationId: string })
.use(async ({ input, ctx: { dbClient, jwt }, next }) => {
const conversationRows = await dbClient
.selectFrom("conversations")
.where("id", "=", input.conversationId)
.where("userId", "=", jwt.id as string)
.execute();
if (!conversationRows.length) {
throw new TRPCError({ code: "UNAUTHORIZED" });
}
return await next();
})
.query(async ({ input: { conversationId }, ctx: { dbClient } }) => {
const rows = await dbClient
.selectFrom("facts")
.innerJoin("messages", "messages.id", "facts.sourceMessageId")
.selectAll("facts")
.where("conversationId", "=", conversationId)
.execute();
return rows;
}),
deleteOne: authFactProcedure.mutation(
async ({ input: { factId }, ctx: { dbClient } }) => {
await dbClient.deleteFrom("facts").where("id", "=", factId).execute();
return { ok: true };
}
),
update: authFactProcedure
.input(z.object({ content: z.string() }))
.mutation(async ({ input: { factId, content }, ctx: { dbClient } }) => {
await dbClient
.updateTable("facts")
.set({ content })
.where("id", "=", factId)
.execute();
return { ok: true };
}),
extractFromNewMessages: authProcedure
.input(
(x) =>
x as {
previousRunningSummary: string;
/** will *not* have facts extracted */
messagesSincePreviousRunningSummary: Array<DraftMessage>;
/** *will* have facts extracted */
newMessages: Array<DraftMessage>;
}
)
.query(
async ({
input: {
previousRunningSummary,
messagesSincePreviousRunningSummary,
newMessages,
},
ctx: { openrouter },
}) => {
const factsFromUserMessageResponse = await generateObject({
model: openrouter(MODEL_NAME),
messages: [
{
role: "system" as const,
content: factsFromNewMessagesSystemPrompt({
previousRunningSummary,
messagesSincePreviousRunningSummary,
}),
},
{
role: "user" as const,
content: factsFromNewMessagesUserPrompt({
newMessages,
}),
},
],
schema: jsonSchema<{
facts: Array<string>;
}>({
type: "object",
properties: {
facts: {
type: "array",
items: {
type: "string",
},
},
},
required: ["facts"],
}),
temperature: 0.4,
maxRetries: 0,
}).catch((err) => {
console.error(err);
return {
object: {
facts: [] as Array<string>,
},
};
});
return factsFromUserMessageResponse;
}
),
});
export const createCaller = createCallerFactory(facts);