upgrade `ai` sdk to v5

master
Avraham Sakal 2 months ago
parent 3aa252e248
commit 341cf5bff1

@ -0,0 +1,19 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Vite: debug full stack",
"type": "node-terminal",
"request": "launch",
"command": "pnpm dev",
"serverReadyAction": {
"pattern": "Local: http://localhost:3000/",
"uriFormat": "http://localhost:%s/",
"action": "debugWithChrome"
}
}
]
}

@ -1,7 +1,3 @@
+ Break-out functionality in `sendMessage` into separate procedures,
namespaced by domain (e.g. `messages`, `facts`, `factTriggers`).
+ Make a `withDbWrite` function to wrap the calls to these procedures, so
as to separate the generation of data from persisting it.
+ Somehow subscribe the UI to events from the `sendMessage` procedure,
so the user knows what's going on.
+ Parallelize the generation of data, so that the UI doesn't freeze for

@ -1,5 +1,6 @@
import { Low } from "lowdb";
import { JSONFile } from "lowdb/node";
import type { CommittedMessage } from "../types";
export type Conversation = {
id: string;
@ -27,7 +28,7 @@ export type FactTrigger = {
type DB = {
conversations: Array<Conversation>;
messages: Array<{
messages: Array</*{
id: string;
conversationId: string;
content: string;
@ -35,7 +36,7 @@ type DB = {
index: number;
createdAt: string;
runningSummary?: string;
}>;
}*/ CommittedMessage>;
facts: Array<Fact>;
factTriggers: Array<FactTrigger>;
};

@ -28,7 +28,7 @@ export default function ChatPage() {
const pageContext = usePageContext();
const conversationId = pageContext.routeParams.id;
const conversationTitle = useStore(
(state) => state.conversations.find((c) => c.id === conversationId)?.title,
(state) => state.conversations.find((c) => c.id === conversationId)?.title
);
const messages = useStore((state) => state.messages);
const message = useStore((state) => state.message);
@ -54,30 +54,33 @@ export default function ChatPage() {
const [editingFactContent, setEditingFactContent] = useState("");
// State for editing fact triggers
const [editingFactTriggerId, setEditingFactTriggerId] = useState<string | null>(null);
const [editingFactTriggerContent, setEditingFactTriggerContent] = useState("");
const [editingFactTriggerId, setEditingFactTriggerId] = useState<
string | null
>(null);
const [editingFactTriggerContent, setEditingFactTriggerContent] =
useState("");
// Handle clicking outside to cancel editing
useEffect(() => {
function handleClickOutside(event: MouseEvent) {
if (editingFactId && event.target instanceof Element) {
const editingElement = event.target.closest('.editing-fact');
const editingElement = event.target.closest(".editing-fact");
if (!editingElement) {
setEditingFactId(null);
}
}
if (editingFactTriggerId && event.target instanceof Element) {
const editingElement = event.target.closest('.editing-fact-trigger');
const editingElement = event.target.closest(".editing-fact-trigger");
if (!editingElement) {
setEditingFactTriggerId(null);
}
}
}
document.addEventListener('mousedown', handleClickOutside);
document.addEventListener("mousedown", handleClickOutside);
return () => {
document.removeEventListener('mousedown', handleClickOutside);
document.removeEventListener("mousedown", handleClickOutside);
};
}, [editingFactId, editingFactTriggerId]);
@ -123,10 +126,10 @@ export default function ChatPage() {
async function handleUpdateFact(factId: string, content: string) {
// Update the local state first
setFacts(facts.map(fact =>
fact.id === factId ? { ...fact, content } : fact
));
setFacts(
facts.map((fact) => (fact.id === factId ? { ...fact, content } : fact))
);
// Then update the database
await trpc.chat.facts.update.mutate({ factId, content });
}
@ -136,12 +139,19 @@ export default function ChatPage() {
await trpc.chat.factTriggers.deleteOne.mutate({ factTriggerId });
}
async function handleUpdateFactTrigger(factTriggerId: string, content: string) {
async function handleUpdateFactTrigger(
factTriggerId: string,
content: string
) {
// Update the local state first
setFactTriggers(factTriggers.map(factTrigger =>
factTrigger.id === factTriggerId ? { ...factTrigger, content } : factTrigger
));
setFactTriggers(
factTriggers.map((factTrigger) =>
factTrigger.id === factTriggerId
? { ...factTrigger, content }
: factTrigger
)
);
// Then update the database
await trpc.chat.factTriggers.update.mutate({ factTriggerId, content });
}
@ -185,7 +195,10 @@ export default function ChatPage() {
e.preventDefault();
const messagesWithNewUserMessage = [
...messages,
{ role: "user" as const, content: message } as DraftMessage,
{
role: "user" as const,
parts: [{ type: "text", text: message }],
} as DraftMessage,
];
setMessages(messagesWithNewUserMessage);
setLoading(true);
@ -201,7 +214,8 @@ export default function ChatPage() {
id: response.insertedUserMessage?.id,
conversationId,
role: "user" as const,
content: message,
// content: message,
parts: [{ type: "text", text: message }],
index: response.insertedUserMessage?.index,
runningSummary: undefined,
} as CommittedMessage,
@ -209,7 +223,9 @@ export default function ChatPage() {
id: response.insertedAssistantMessage?.id,
conversationId,
role: "assistant" as const,
content: response.insertedAssistantMessage?.content,
// content: response.insertedAssistantMessage?.content,
// parts: [{ type: "text", text: response.insertedAssistantMessage?.content }],
parts: response.insertedAssistantMessage?.parts,
index: response.insertedAssistantMessage?.index,
runningSummary:
response.insertedAssistantMessage?.runningSummary ||
@ -270,9 +286,7 @@ export default function ChatPage() {
>
<IconCheck size={16} />
</ActionIcon>
<ActionIcon
onClick={() => setEditingFactId(null)}
>
<ActionIcon onClick={() => setEditingFactId(null)}>
<IconX size={16} />
</ActionIcon>
</Group>
@ -310,11 +324,16 @@ export default function ChatPage() {
<Group wrap="nowrap" className="editing-fact-trigger">
<Textarea
value={editingFactTriggerContent}
onChange={(e) => setEditingFactTriggerContent(e.target.value)}
onChange={(e) =>
setEditingFactTriggerContent(e.target.value)
}
onKeyDown={(e) => {
if (e.key === "Enter") {
e.preventDefault();
handleUpdateFactTrigger(factTrigger.id, editingFactTriggerContent);
handleUpdateFactTrigger(
factTrigger.id,
editingFactTriggerContent
);
setEditingFactTriggerId(null);
} else if (e.key === "Escape") {
setEditingFactTriggerId(null);
@ -325,15 +344,16 @@ export default function ChatPage() {
/>
<ActionIcon
onClick={() => {
handleUpdateFactTrigger(factTrigger.id, editingFactTriggerContent);
handleUpdateFactTrigger(
factTrigger.id,
editingFactTriggerContent
);
setEditingFactTriggerId(null);
}}
>
<IconCheck size={16} />
</ActionIcon>
<ActionIcon
onClick={() => setEditingFactTriggerId(null)}
>
<ActionIcon onClick={() => setEditingFactTriggerId(null)}>
<IconX size={16} />
</ActionIcon>
</Group>
@ -396,7 +416,12 @@ function Messages({
p="md"
bdrs="md"
>
<Markdown>{message.content}</Markdown>
<Markdown>
{message.parts
.filter((p) => p.type === "text")
.map((p) => p.text)
.join("\n")}
</Markdown>
</Box>
</HoverCard.Target>
<HoverCard.Dropdown>

@ -34,16 +34,22 @@ export const conversations = router({
.mutation(async ({ input: { id } }) => {
db.data.conversations.splice(
db.data.conversations.findIndex((c) => c.id === id),
1,
1
);
const deletedMessageIds = db.data.messages
.filter((m) => m.conversationId === id)
.map((m) => m.id);
db.data.messages = db.data.messages.filter(
(m) => m.conversationId !== id,
(m) => m.conversationId !== id
);
const deletedFactIds = db.data.facts
.filter((fact) => deletedMessageIds.includes(fact.sourceMessageId))
.map((fact) => fact.id);
db.data.facts = db.data.facts.filter(
(fact) => !deletedMessageIds.includes(fact.sourceMessageId),
(fact) => !deletedFactIds.includes(fact.id)
);
db.data.factTriggers = db.data.factTriggers.filter(
(factTrigger) => !deletedFactIds.includes(factTrigger.sourceFactId)
);
db.write();
return { ok: true };
@ -54,7 +60,7 @@ export const conversations = router({
x as {
id: string;
title: string;
},
}
)
.mutation(async ({ input: { id, title } }) => {
const conversation = await db.data.conversations.find((c) => c.id === id);
@ -67,7 +73,7 @@ export const conversations = router({
.input((x) => x as { conversationId: string })
.query(async ({ input: { conversationId } }) => {
const rows = await db.data.messages.filter(
(m) => m.conversationId === conversationId,
(m) => m.conversationId === conversationId
);
return rows as Array<CommittedMessage>;
}),

@ -37,7 +37,10 @@ ${previousRunningSummary}
${messagesSincePreviousRunningSummary.map(
(message) =>
`<${message.role}_message>${message.content}</${message.role}_message>`,
`<${message.role}_message>${message.parts
.filter((p) => p.type === "text")
.map((p) => p.text)
.join("\n")}</${message.role}_message>`
)}
<assistant_response>
${mainResponseContent}
@ -59,7 +62,7 @@ export const factTriggers = router({
.input((x) => x as { factId: string })
.query(async ({ input: { factId } }) => {
return db.data.factTriggers.filter(
(factTrigger) => factTrigger.sourceFactId === factId,
(factTrigger) => factTrigger.sourceFactId === factId
);
}),
deleteOne: publicProcedure
@ -67,13 +70,14 @@ export const factTriggers = router({
(x) =>
x as {
factTriggerId: string;
},
}
)
.mutation(async ({ input: { factTriggerId } }) => {
const deletedFactTriggerIndex = db.data.factTriggers.findIndex(
(factTrigger) => factTrigger.id === factTriggerId,
(factTrigger) => factTrigger.id === factTriggerId
);
if (deletedFactTriggerIndex === -1) throw new Error("Fact trigger not found");
if (deletedFactTriggerIndex === -1)
throw new Error("Fact trigger not found");
db.data.factTriggers.splice(deletedFactTriggerIndex, 1);
await db.write();
return { ok: true };
@ -84,11 +88,11 @@ export const factTriggers = router({
x as {
factTriggerId: string;
content: string;
},
}
)
.mutation(async ({ input: { factTriggerId, content } }) => {
const factTriggerIndex = db.data.factTriggers.findIndex(
(factTrigger) => factTrigger.id === factTriggerId,
(factTrigger) => factTrigger.id === factTriggerId
);
if (factTriggerIndex === -1) throw new Error("Fact trigger not found");
db.data.factTriggers[factTriggerIndex].content = content;
@ -103,7 +107,7 @@ export const factTriggers = router({
messagesSincePreviousRunningSummary: Array<DraftMessage>;
mainResponseContent: string;
fact: Fact;
},
}
)
.mutation(
async ({
@ -114,9 +118,7 @@ export const factTriggers = router({
fact,
},
}) => {
const factTriggers = await generateObject<{
factTriggers: Array<string>;
}>({
const factTriggers = await generateObject({
model: openrouter("mistralai/mistral-nemo"),
messages: [
{
@ -134,7 +136,9 @@ export const factTriggers = router({
}),
},
],
schema: jsonSchema({
schema: jsonSchema<{
factTriggers: Array<string>;
}>({
type: "object",
properties: {
factTriggers: {
@ -149,7 +153,7 @@ export const factTriggers = router({
// tools: undefined,
});
return factTriggers;
},
}
),
});

@ -31,7 +31,10 @@ Your task is to extract *new* facts that can be gleaned from the *new* messages
${messagesSincePreviousRunningSummary.map(
(message) =>
`<${message.role}_message>${message.content}</${message.role}_message>`,
`<${message.role}_message>${message.parts
.filter((p) => p.type === "text")
.map((p) => p.text)
.join("\n")}</${message.role}_message>`
)}
`;
@ -42,7 +45,10 @@ const factsFromNewMessagesUserPrompt = ({
}) =>
`${newMessages.map(
(message) =>
`<${message.role}_message>${message.content}</${message.role}_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.`;
@ -55,7 +61,7 @@ export const facts = router({
.filter((m) => m.conversationId === conversationId)
.map((m) => m.id);
const rows = await db.data.facts.filter((f) =>
conversationMessageIds.includes(f.sourceMessageId),
conversationMessageIds.includes(f.sourceMessageId)
);
return rows as Array<Fact>;
}),
@ -64,11 +70,11 @@ export const facts = router({
(x) =>
x as {
factId: string;
},
}
)
.mutation(async ({ input: { factId } }) => {
const deletedFactId = db.data.facts.findIndex(
(fact) => fact.id === factId,
(fact) => fact.id === factId
);
if (deletedFactId === -1) throw new Error("Fact not found");
db.data.facts.splice(deletedFactId, 1);
@ -81,12 +87,10 @@ export const facts = router({
x as {
factId: string;
content: string;
},
}
)
.mutation(async ({ input: { factId, content } }) => {
const factIndex = db.data.facts.findIndex(
(fact) => fact.id === factId,
);
const factIndex = db.data.facts.findIndex((fact) => fact.id === factId);
if (factIndex === -1) throw new Error("Fact not found");
db.data.facts[factIndex].content = content;
await db.write();
@ -101,7 +105,7 @@ export const facts = router({
messagesSincePreviousRunningSummary: Array<DraftMessage>;
/** *will* have facts extracted */
newMessages: Array<DraftMessage>;
},
}
)
.query(
async ({
@ -111,9 +115,7 @@ export const facts = router({
newMessages,
},
}) => {
const factsFromUserMessageResponse = await generateObject<{
facts: Array<string>;
}>({
const factsFromUserMessageResponse = await generateObject({
model: openrouter("mistralai/mistral-nemo"),
messages: [
{
@ -130,7 +132,9 @@ export const facts = router({
}),
},
],
schema: jsonSchema({
schema: jsonSchema<{
facts: Array<string>;
}>({
type: "object",
properties: {
facts: {
@ -144,7 +148,7 @@ export const facts = router({
temperature: 0.4,
});
return factsFromUserMessageResponse;
},
}
),
});

@ -37,7 +37,10 @@ const runningSummaryUserPrompt = ({
}) =>
`${messagesSincePreviousRunningSummary.map(
(message) =>
`<${message.role}_message>${message.content}</${message.role}_message>`,
`<${message.role}_message>${message.parts
.filter((p) => p.type === "text")
.map((p) => p.text)
.join("\n")}</${message.role}_message>`
)}
<assistant_response>
${mainResponseContent}
@ -61,7 +64,7 @@ export const messages = router({
previousRunningSummary: string;
messagesSincePreviousRunningSummary: Array<DraftMessage>;
mainResponseContent: string;
},
}
)
.mutation(
async ({
@ -88,11 +91,10 @@ export const messages = router({
}),
},
],
maxSteps: 3,
tools: undefined,
});
return runningSummaryResponse;
},
}
),
});

@ -31,7 +31,10 @@ const factTriggerCaller = createCallerFactTriggers({});
const mainSystemPrompt = ({
systemPrompt,
previousRunningSummary,
}: { systemPrompt: string; previousRunningSummary: string }) => `${systemPrompt}
}: {
systemPrompt: string;
previousRunningSummary: string;
}) => `${systemPrompt}
This is a summary of the conversation so far, from your point-of-view (so "I" and "me" refer to you):
<running_summary>
@ -52,7 +55,7 @@ export const chat = router({
messages: Array<DraftMessage | CommittedMessage>;
systemPrompt: string;
parameters: OtherParameters;
},
}
)
.mutation(
async ({
@ -65,7 +68,7 @@ export const chat = router({
* anyone can freely do. */
const previousRunningSummaryIndex = messages.findLastIndex(
(message) =>
typeof (message as CommittedMessage).runningSummary !== "undefined",
typeof (message as CommittedMessage).runningSummary !== "undefined"
);
const previousRunningSummary =
previousRunningSummaryIndex >= 0
@ -73,14 +76,15 @@ export const chat = router({
.runningSummary as string)
: "";
const messagesSincePreviousRunningSummary = messages.slice(
previousRunningSummaryIndex + 1,
previousRunningSummaryIndex + 1
);
/** Save the incoming message to the database. */
const insertedUserMessage: CommittedMessage = {
id: nanoid(),
conversationId,
content: messages[messages.length - 1].content,
role: "user" as const,
// content: messages[messages.length - 1].content,
// role: "user" as const,
...messages[messages.length - 1],
index: messages.length - 1,
createdAt: new Date().toISOString(),
};
@ -98,7 +102,10 @@ export const chat = router({
model: openrouter("mistralai/mistral-nemo"),
messages: [
previousRunningSummary === ""
? { role: "system" as const, content: systemPrompt }
? {
role: "system" as const,
content: systemPrompt,
}
: {
role: "system" as const,
content: mainSystemPrompt({
@ -106,9 +113,14 @@ export const chat = router({
previousRunningSummary,
}),
},
...messagesSincePreviousRunningSummary,
...messagesSincePreviousRunningSummary.map((m) => ({
role: m.role,
content: m.parts
.filter((p) => p.type === "text")
.map((p) => p.text)
.join(""),
})),
],
maxSteps: 3,
tools: undefined,
...parameters,
});
@ -149,7 +161,8 @@ export const chat = router({
const insertedAssistantMessage: CommittedMessage = {
id: nanoid(),
conversationId,
content: mainResponse.text,
// content: mainResponse.text,
parts: [{ type: "text", text: mainResponse.text }],
runningSummary: runningSummaryResponse.text,
role: "assistant" as const,
index: messages.length,
@ -165,7 +178,8 @@ export const chat = router({
newMessages: [
{
role: "assistant" as const,
content: mainResponse.text,
// content: mainResponse.text,
parts: [{ type: "text", text: mainResponse.text }],
},
],
});
@ -217,7 +231,7 @@ export const chat = router({
insertedUserMessage,
insertedFacts,
};
},
}
),
});

@ -1,4 +1,4 @@
import type { Message as UIMessage } from "ai";
import type { UIMessage } from "ai";
import type { generateText } from "ai";
import type { Conversation, Fact, FactTrigger } from "./database/lowdb.js";

Loading…
Cancel
Save