xonecas.io
53129
Pa

PAT / files

PAT / src / hooks / api / useNewThread.ts

937 bytes
1import { Thread } from "@/src/lib/db/schema";
2import { useMutation, useQueryClient } from "@tanstack/react-query";
3
4export function useNewThread(
5  userMessage: string,
6  model: string,
7  maxPromptLength: "none" | 1024 | 4096,
8  userPrompt: string,
9  time: string,
10  timezone: string,
11  onSuccess?: (threadId: number) => void | undefined
12) {
13  const queryClient = useQueryClient();
14
15  return useMutation<{ thread: Thread }, Error>({
16    mutationFn: async () => {
17      const response = await fetch("/api/threads", {
18        method: "POST",
19        body: JSON.stringify({ userMessage, model, maxPromptLength, userPrompt, time, timezone }),
20      });
21      if (!response.ok) {
22        throw new Error(`Failed to create thread: ${response.statusText}`);
23      }
24      return response.json();
25    },
26    onSuccess: (data) => {
27      queryClient.invalidateQueries({ queryKey: ["threads"] });
28      onSuccess?.(data.thread.id);
29    },
30  });
31}