-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathuseChat.ts
129 lines (113 loc) · 3.05 KB
/
useChat.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import { useCallback, useEffect, useRef, useState } from "react";
import { randomId } from "../utilities/messageId";
import { vscode } from "../utilities/vscode";
import { useChatMessages } from "./useChatMessages";
export type ChatMessage = {
role: string;
content: string;
chatMessageId: string;
};
export type Chat = {
messages: ChatMessage[];
chatId: string;
date: number;
title: string;
};
export const useChat = (chatId?: string) => {
const { chatMessages, setChatMessages } = useChatMessages();
const [input, setInput] = useState("");
const [isLoading, setIsLoading] = useState(false);
const [chatIdLocal, setChatIdLocal] = useState<string>(chatId || randomId());
const abortController = useRef(new AbortController());
useEffect(() => {
const getChatHistory = async () => {
if (chatId) {
const chat = await vscode.getChat(chatId);
if (chat) {
setChatMessages(chat.messages);
}
}
};
if (chatId) {
getChatHistory();
}
}, [chatId, setChatMessages]);
const sendMessage = async (chatHistoryLocal: ChatMessage[]) => {
const messageId = randomId();
for await (const newMessage of vscode.startGeneration(chatHistoryLocal, {
signal: abortController.current.signal,
})) {
setChatMessages((chatHistoryLocal) => {
const messages = chatHistoryLocal.filter(
(message) => message.chatMessageId !== messageId
);
const currentChatMessage = chatHistoryLocal.find(
(message) => message.chatMessageId === messageId
);
return [
...messages,
{
role: "ai",
content: (currentChatMessage?.content || "") + newMessage,
chatMessageId: messageId,
},
];
});
}
setChatMessages((chatHistoryLocal) => {
(async () => {
const chat = {
chatId: chatIdLocal,
date: Date.now(),
messages: chatHistoryLocal,
title: "Chat with AI",
} satisfies Chat;
await vscode.saveChatHistory(chatIdLocal, chat);
})();
return chatHistoryLocal;
});
setIsLoading(false);
};
const handleSubmit = () => {
if (isLoading) {
return;
}
if (input === "") {
return;
}
if (abortController.current.signal.aborted) {
abortController.current = new AbortController();
}
setChatMessages((value) => {
const messageId = randomId();
const newChatMessage = [
...value,
{
role: "user",
content: input,
chatMessageId: messageId,
},
];
setIsLoading(true);
sendMessage(newChatMessage);
return newChatMessage;
});
setInput("");
};
const stop = useCallback(() => {
abortController.current.abort();
setIsLoading(false);
}, [abortController]);
const startNewChat = useCallback(() => {
setChatMessages([]);
}, [setChatMessages]);
return {
chatMessages,
input,
isLoading,
setInput,
handleSubmit,
startNewChat,
stop,
};
};