From c5f10d11158f002f50ffd639651152121b2509d8 Mon Sep 17 00:00:00 2001 From: Hirokazu Tanaka Date: Thu, 7 Aug 2025 01:01:58 +0900 Subject: [PATCH 1/3] =?UTF-8?q?=E3=83=81=E3=83=A3=E3=83=83=E3=83=88?= =?UTF-8?q?=E3=83=95=E3=82=A9=E3=83=BC=E3=83=A0=E3=81=AE=E3=82=AF=E3=83=A9?= =?UTF-8?q?=E3=82=A4=E3=82=A2=E3=83=B3=E3=83=88=E5=81=B4=E3=81=A8=E3=82=B5?= =?UTF-8?q?=E3=83=BC=E3=83=90=E3=83=BC=E5=81=B4API=E3=82=92=E9=80=A3?= =?UTF-8?q?=E6=90=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/[docs_id]/chatForm.tsx | 198 ++++++++++++++++++++++--------------- 1 file changed, 121 insertions(+), 77 deletions(-) diff --git a/app/[docs_id]/chatForm.tsx b/app/[docs_id]/chatForm.tsx index 14dc441..a6b9867 100644 --- a/app/[docs_id]/chatForm.tsx +++ b/app/[docs_id]/chatForm.tsx @@ -1,112 +1,156 @@ "use client"; -import { hello } from "./chatServer"; +import { useState, FormEvent } from "react"; -export function ChatForm() {return ( +interface ChatApiResponse { + response: string; +} + +export function ChatForm() { + const [inputValue, setInputValue] = useState(""); + const [response, setResponse] = useState(""); + const [isLoading, setIsLoading] = useState(false); + + const handleSubmit = async (e: FormEvent) => { + e.preventDefault(); + setIsLoading(true); + setResponse(""); + + try { + const res = await fetch("/api/chat", { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ message: inputValue }), + }); + + const data = (await res.json()) as ChatApiResponse; + if (!res.ok) { + throw new Error(data.response || "エラーが発生しました。"); + } + setResponse(data.response); + } catch (error: any) { + setResponse(`エラー: ${error.message}`); + } finally { + setIsLoading(false); + } + }; + return ( <> - + `} -
+
- +
-
-
- -
-
- -
+
+
+ +
-
+ + {response &&
{response}
} -)} \ No newline at end of file + ); +} From 8581fb78e38217e45b74430476bed70f4c2e21e1 Mon Sep 17 00:00:00 2001 From: Hirokazu Tanaka Date: Thu, 7 Aug 2025 09:43:35 +0900 Subject: [PATCH 2/3] =?UTF-8?q?=E3=83=95=E3=82=A1=E3=82=A4=E3=83=AB?= =?UTF-8?q?=E3=81=AE=E4=BD=8D=E7=BD=AE=E3=81=A8lint=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/{ => api}/chat/route.js | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) rename app/{ => api}/chat/route.js (86%) diff --git a/app/chat/route.js b/app/api/chat/route.js similarity index 86% rename from app/chat/route.js rename to app/api/chat/route.js index 7d2d89a..64127c9 100644 --- a/app/chat/route.js +++ b/app/api/chat/route.js @@ -1,13 +1,11 @@ -import { NextResponse } from 'next/server'; -import { GoogleGenerativeAI } from '@google/generative-ai'; - +import { NextResponse } from "next/server"; +import { GoogleGenerativeAI } from "@google/generative-ai"; const genAI = new GoogleGenerativeAI(process.env.API_KEY); export async function POST(request) { const { message } = await request.json(); - if (!message) { return NextResponse.json( { error: "メッセージがありません。" }, @@ -17,19 +15,17 @@ export async function POST(request) { try { const model = genAI.getGenerativeModel({ model: "gemini-1.5-flash" }); - + const result = await model.generateContent(message); const response = result.response; const text = response.text(); return NextResponse.json({ response: text }); - } catch (e) { - console.error("Error:", e); return NextResponse.json( { response: "エラーが発生しました。" }, { status: 500 } ); } -} \ No newline at end of file +} From 29d0e0acd64ae56e8147f74e8319ed4d1429788b Mon Sep 17 00:00:00 2001 From: Hirokazu Tanaka Date: Thu, 7 Aug 2025 19:00:40 +0900 Subject: [PATCH 3/3] =?UTF-8?q?=E3=82=A8=E3=83=A9=E3=83=BC=E3=83=8F?= =?UTF-8?q?=E3=83=B3=E3=83=89=E3=83=AA=E3=83=B3=E3=82=B0=E3=82=92=E6=94=B9?= =?UTF-8?q?=E5=96=84=E3=81=97=E3=80=81unknown=E5=9E=8B=E3=81=AE=E3=82=A8?= =?UTF-8?q?=E3=83=A9=E3=83=BC=E3=82=92=E9=81=A9=E5=88=87=E3=81=AB=E5=87=A6?= =?UTF-8?q?=E7=90=86=E3=81=99=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB=E4=BF=AE?= =?UTF-8?q?=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/[docs_id]/chatForm.tsx | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/app/[docs_id]/chatForm.tsx b/app/[docs_id]/chatForm.tsx index a6b9867..0b86077 100644 --- a/app/[docs_id]/chatForm.tsx +++ b/app/[docs_id]/chatForm.tsx @@ -30,8 +30,12 @@ export function ChatForm() { throw new Error(data.response || "エラーが発生しました。"); } setResponse(data.response); - } catch (error: any) { - setResponse(`エラー: ${error.message}`); + } catch (error: unknown) { + if (error instanceof Error) { + setResponse(`エラー: ${error.message}`); + } else { + setResponse(`エラー: ${String(error)}`); + } } finally { setIsLoading(false); }