Skip to content
Source
Sign inSign up
TanStack/cli
Open History to see the last commit

ai-hook.ts

25 lines729 Bytes

1import {
2 fetchServerSentEvents,
3 useChat,
4 createChatClientOptions,
5} from "@tanstack/ai-react";
6import type { InferChatMessages } from "@tanstack/ai-react";
7import { clientTools } from "@tanstack/ai-client";
8​
9import { recommendProductToolDef } from "@/lib/product-tools";
10​
11const recommendProductToolClient = recommendProductToolDef.client((args) => {
12 const input = args as { id: string | number };
13 return {
14 id: +input.id,
15 };
16});
17​
18const chatOptions = createChatClientOptions({
19 connection: fetchServerSentEvents("/api/product-chat"),
20 tools: clientTools(recommendProductToolClient),
21});
22​
23export type ChatMessages = InferChatMessages<typeof chatOptions>;
24​
25export const useProductChat = () => useChat(chatOptions);