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

types.ts

118 lines2.1 KB

1import type { Message, StreamChunk } from "@tanstack/ai";
2​
3export interface ChatMessage extends Message {
4 id: string;
5 createdAt?: Date;
6}
7​
8export interface UseChatOptions {
9 /**
10 * The API endpoint to send messages to
11 * @default "/api/chat"
12 */
13 api?: string;
14​
15 /**
16 * Initial messages to populate the chat
17 */
18 initialMessages?: ChatMessage[];
19​
20 /**
21 * Unique identifier for this chat instance
22 * Used for managing multiple chats
23 */
24 id?: string;
25​
26 /**
27 * Callback when a response is received
28 */
29 onResponse?: (response: Response) => void | Promise<void>;
30​
31 /**
32 * Callback when a stream chunk is received
33 */
34 onChunk?: (chunk: StreamChunk) => void;
35​
36 /**
37 * Callback when the response is finished
38 */
39 onFinish?: (message: ChatMessage) => void;
40​
41 /**
42 * Callback when an error occurs
43 */
44 onError?: (error: Error) => void;
45​
46 /**
47 * Additional headers to send with the request
48 */
49 headers?: Record<string, string> | Headers;
50​
51 /**
52 * Additional body parameters to send
53 */
54 body?: Record<string, any>;
55​
56 /**
57 * Credentials mode for fetch
58 * @default "same-origin"
59 */
60 credentials?: "omit" | "same-origin" | "include";
61​
62 /**
63 * Custom fetch implementation
64 */
65 fetch?: typeof fetch;
67​
68export interface UseChatReturn {
69 /**
70 * Current messages in the conversation
71 */
72 messages: ChatMessage[];
73​
74 /**
75 * Send a message and get a response
76 */
77 sendMessage: (content: string) => Promise<void>;
78​
79 /**
80 * Append a message to the conversation
81 */
82 append: (message: Message | ChatMessage) => Promise<void>;
83​
84 /**
85 * Reload the last assistant message
86 */
87 reload: () => Promise<void>;
88​
89 /**
90 * Stop the current response generation
91 */
92 stop: () => void;
93​
94 /**
95 * Whether a response is currently being generated
96 */
97 isLoading: boolean;
98​
99 /**
100 * Current error, if any
101 */
102 error: Error | undefined;
103​
104 /**
105 * Set messages manually
106 */
107 setMessages: (messages: ChatMessage[]) => void;
108​
109 /**
110 * Clear all messages
111 */
112 clear: () => void;
114​
115export interface ChatRequestBody {
116 messages: Message[];
117 data?: Record<string, any>;