Skip to content

Commit 06ee5e6

Browse files
committed
チャットAPIの呼び出しをfetchからサーバーアクションに変更
1 parent 929763d commit 06ee5e6

File tree

1 file changed

+10
-24
lines changed

1 file changed

+10
-24
lines changed

app/[docs_id]/chatForm.tsx

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
"use client";
22

33
import { useState, FormEvent } from "react";
4-
5-
interface ChatApiResponse {
6-
response: string;
7-
}
4+
import { askAI } from "@/app/actions/chatActions";
85

96
export function ChatForm() {
107
const [inputValue, setInputValue] = useState("");
@@ -17,29 +14,18 @@ export function ChatForm() {
1714
setIsLoading(true);
1815
setResponse("");
1916

20-
try {
21-
const res = await fetch("/api/chat", {
22-
method: "POST",
23-
headers: {
24-
"Content-Type": "application/json",
25-
},
26-
body: JSON.stringify({ message: inputValue }),
27-
});
17+
const formData = new FormData();
18+
formData.append("message", inputValue);
19+
20+
const result = await askAI({ response: "", error: null }, formData);
2821

29-
const data = (await res.json()) as ChatApiResponse;
30-
if (!res.ok) {
31-
throw new Error(data.response || "エラーが発生しました。");
32-
}
33-
setResponse(data.response);
34-
} catch (error: unknown) {
35-
if (error instanceof Error) {
36-
setResponse(`エラー: ${error.message}`);
22+
if (result.error) {
23+
setResponse(`エラー: ${result.error}`);
3724
} else {
38-
setResponse(`エラー: ${String(error)}`);
39-
}
40-
} finally {
41-
setIsLoading(false);
25+
setResponse(result.response);
4226
}
27+
28+
setIsLoading(false);
4329
};
4430
return (
4531
<>

0 commit comments

Comments
 (0)