|
|
@ -1,8 +1,15 @@
|
|
|
|
import { router, publicProcedure, createCallerFactory } from "./server.js";
|
|
|
|
import {
|
|
|
|
|
|
|
|
router,
|
|
|
|
|
|
|
|
publicProcedure,
|
|
|
|
|
|
|
|
createCallerFactory,
|
|
|
|
|
|
|
|
authProcedure,
|
|
|
|
|
|
|
|
} from "./server.js";
|
|
|
|
import type { DraftMessage } from "../../types.js";
|
|
|
|
import type { DraftMessage } from "../../types.js";
|
|
|
|
import { MODEL_NAME } from "../provider.js";
|
|
|
|
import { MODEL_NAME } from "../provider.js";
|
|
|
|
import { generateObject, generateText, jsonSchema } from "ai";
|
|
|
|
import { generateObject, generateText, jsonSchema } from "ai";
|
|
|
|
import type { Fact } from "@database/common.js";
|
|
|
|
import type { Fact } from "@database/common.js";
|
|
|
|
|
|
|
|
import { TRPCError } from "@trpc/server";
|
|
|
|
|
|
|
|
import { z } from "zod";
|
|
|
|
|
|
|
|
|
|
|
|
const factTriggersSystemPrompt = ({
|
|
|
|
const factTriggersSystemPrompt = ({
|
|
|
|
previousRunningSummary,
|
|
|
|
previousRunningSummary,
|
|
|
@ -53,52 +60,71 @@ ${factContent}
|
|
|
|
|
|
|
|
|
|
|
|
Generate a list of situations in which the fact is useful.`;
|
|
|
|
Generate a list of situations in which the fact is useful.`;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const authFactTriggerProcedure = authProcedure
|
|
|
|
|
|
|
|
.input(z.object({ factTriggerId: z.string() }))
|
|
|
|
|
|
|
|
.use(async ({ input, ctx: { dbClient, jwt }, next }) => {
|
|
|
|
|
|
|
|
const factTriggerRows = await dbClient
|
|
|
|
|
|
|
|
.selectFrom("fact_triggers")
|
|
|
|
|
|
|
|
.innerJoin("facts", "facts.id", "fact_triggers.sourceFactId")
|
|
|
|
|
|
|
|
.innerJoin("messages", "messages.id", "facts.sourceMessageId")
|
|
|
|
|
|
|
|
.innerJoin("conversations", "conversations.id", "messages.conversationId")
|
|
|
|
|
|
|
|
.where("fact_triggers.id", "=", input.factTriggerId)
|
|
|
|
|
|
|
|
.where("conversations.userId", "=", jwt.id as string)
|
|
|
|
|
|
|
|
.execute();
|
|
|
|
|
|
|
|
if (!factTriggerRows.length) {
|
|
|
|
|
|
|
|
throw new TRPCError({ code: "UNAUTHORIZED" });
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return await next();
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
export const factTriggers = router({
|
|
|
|
export const factTriggers = router({
|
|
|
|
fetchByFactId: publicProcedure
|
|
|
|
fetchByFactId: authProcedure
|
|
|
|
.input((x) => x as { factId: string })
|
|
|
|
.input((x) => x as { factId: string })
|
|
|
|
.query(async ({ input: { factId }, ctx: { dbClient } }) => {
|
|
|
|
.query(async ({ input: { factId }, ctx: { dbClient, jwt } }) => {
|
|
|
|
const rows = await dbClient
|
|
|
|
const rows = await dbClient
|
|
|
|
.selectFrom("fact_triggers")
|
|
|
|
.selectFrom("fact_triggers")
|
|
|
|
.innerJoin("facts", "facts.id", "fact_triggers.sourceFactId")
|
|
|
|
.innerJoin("facts", "facts.id", "fact_triggers.sourceFactId")
|
|
|
|
|
|
|
|
.innerJoin("messages", "messages.id", "facts.sourceMessageId")
|
|
|
|
|
|
|
|
.innerJoin(
|
|
|
|
|
|
|
|
"conversations",
|
|
|
|
|
|
|
|
"conversations.id",
|
|
|
|
|
|
|
|
"messages.conversationId"
|
|
|
|
|
|
|
|
)
|
|
|
|
.selectAll("fact_triggers")
|
|
|
|
.selectAll("fact_triggers")
|
|
|
|
.where("sourceFactId", "=", factId)
|
|
|
|
.where("sourceFactId", "=", factId)
|
|
|
|
|
|
|
|
.where("conversations.userId", "=", jwt.id as string)
|
|
|
|
.execute();
|
|
|
|
.execute();
|
|
|
|
return rows;
|
|
|
|
return rows;
|
|
|
|
}),
|
|
|
|
}),
|
|
|
|
fetchByConversationId: publicProcedure
|
|
|
|
fetchByConversationId: authProcedure
|
|
|
|
.input((x) => x as { conversationId: string })
|
|
|
|
.input((x) => x as { conversationId: string })
|
|
|
|
.query(async ({ input: { conversationId }, ctx: { dbClient } }) => {
|
|
|
|
.query(async ({ input: { conversationId }, ctx: { dbClient, jwt } }) => {
|
|
|
|
const rows = await dbClient
|
|
|
|
const rows = await dbClient
|
|
|
|
.selectFrom("fact_triggers")
|
|
|
|
.selectFrom("fact_triggers")
|
|
|
|
.innerJoin("facts", "facts.id", "fact_triggers.sourceFactId")
|
|
|
|
.innerJoin("facts", "facts.id", "fact_triggers.sourceFactId")
|
|
|
|
.innerJoin("messages", "messages.id", "facts.sourceMessageId")
|
|
|
|
.innerJoin("messages", "messages.id", "facts.sourceMessageId")
|
|
|
|
|
|
|
|
.innerJoin(
|
|
|
|
|
|
|
|
"conversations",
|
|
|
|
|
|
|
|
"conversations.id",
|
|
|
|
|
|
|
|
"messages.conversationId"
|
|
|
|
|
|
|
|
)
|
|
|
|
.selectAll("fact_triggers")
|
|
|
|
.selectAll("fact_triggers")
|
|
|
|
.where("messages.conversationId", "=", conversationId)
|
|
|
|
.where("messages.conversationId", "=", conversationId)
|
|
|
|
|
|
|
|
.where("conversations.userId", "=", jwt.id as string)
|
|
|
|
.execute();
|
|
|
|
.execute();
|
|
|
|
return rows;
|
|
|
|
return rows;
|
|
|
|
}),
|
|
|
|
}),
|
|
|
|
deleteOne: publicProcedure
|
|
|
|
deleteOne: authFactTriggerProcedure.mutation(
|
|
|
|
.input(
|
|
|
|
async ({ input: { factTriggerId }, ctx: { dbClient, jwt } }) => {
|
|
|
|
(x) =>
|
|
|
|
|
|
|
|
x as {
|
|
|
|
|
|
|
|
factTriggerId: string;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
.mutation(async ({ input: { factTriggerId }, ctx: { dbClient } }) => {
|
|
|
|
|
|
|
|
await dbClient
|
|
|
|
await dbClient
|
|
|
|
.deleteFrom("fact_triggers")
|
|
|
|
.deleteFrom("fact_triggers")
|
|
|
|
.where("id", "=", factTriggerId)
|
|
|
|
.where("id", "=", factTriggerId)
|
|
|
|
.execute();
|
|
|
|
.execute();
|
|
|
|
return { ok: true };
|
|
|
|
return { ok: true };
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
update: publicProcedure
|
|
|
|
),
|
|
|
|
.input(
|
|
|
|
update: authFactTriggerProcedure
|
|
|
|
(x) =>
|
|
|
|
.input(z.object({ content: z.string() }))
|
|
|
|
x as {
|
|
|
|
|
|
|
|
factTriggerId: string;
|
|
|
|
|
|
|
|
content: string;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
.mutation(
|
|
|
|
.mutation(
|
|
|
|
async ({ input: { factTriggerId, content }, ctx: { dbClient } }) => {
|
|
|
|
async ({ input: { factTriggerId, content }, ctx: { dbClient } }) => {
|
|
|
|
await dbClient
|
|
|
|
await dbClient
|
|
|
|