From 0aaf2d611f6ed5c3ebf5462c04730b5a4635c419 Mon Sep 17 00:00:00 2001 From: na-trium-144 <100704180+na-trium-144@users.noreply.github.com> Date: Mon, 1 Sep 2025 22:38:08 +0900 Subject: [PATCH 1/2] =?UTF-8?q?=E8=B3=AA=E5=95=8F=E3=81=AE=E4=BE=8B?= =?UTF-8?q?=E3=82=92AI=E3=81=AB=E8=80=83=E3=81=88=E3=81=95=E3=81=9B?= =?UTF-8?q?=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/[docs_id]/chatForm.tsx | 57 +++++++++++++++++++++++++++++++--- app/[docs_id]/page.tsx | 2 +- app/[docs_id]/section.tsx | 10 ++++-- app/actions/questionExample.ts | 43 +++++++++++++++++++++++++ 4 files changed, 104 insertions(+), 8 deletions(-) create mode 100644 app/actions/questionExample.ts diff --git a/app/[docs_id]/chatForm.tsx b/app/[docs_id]/chatForm.tsx index 24cac31..b0488a8 100644 --- a/app/[docs_id]/chatForm.tsx +++ b/app/[docs_id]/chatForm.tsx @@ -3,20 +3,54 @@ import { useState, FormEvent } from "react"; import { askAI } from "@/app/actions/chatActions"; import { StyledMarkdown } from "./markdown"; +import useSWR from "swr"; +import { getQuestionExample } from "../actions/questionExample"; -export function ChatForm({ documentContent }: { documentContent: string }) { +export function ChatForm({ + docs_id, + documentContent, +}: { + docs_id: string; + documentContent: string; +}) { const [inputValue, setInputValue] = useState(""); const [response, setResponse] = useState(""); const [isLoading, setIsLoading] = useState(false); const [isFormVisible, setIsFormVisible] = useState(false); + const lang = docs_id.split("-")[0]; + const { data: exampleData, error: exampleError } = useSWR( + // 質問フォームを開いたときだけで良い + isFormVisible ? { lang, documentContent } : null, + getQuestionExample, + { + // リクエストは古くても構わないので1回でいい + revalidateIfStale: false, + revalidateOnFocus: false, + revalidateOnReconnect: false, + } + ); + if (exampleError) { + console.error("Error getting question example:", exampleError); + } + // 質問フォームを開くたびにランダムに選び直し、 + // exampleData[Math.floor(exampleChoice * exampleData.length)] を採用する + const [exampleChoice, setExampleChoice] = useState(0); // 0〜1 + const handleSubmit = async (e: FormEvent) => { e.preventDefault(); setIsLoading(true); setResponse(""); + let userQuestion = inputValue; + if(!userQuestion && exampleData){ + // 質問が空欄なら、質問例を使用 + userQuestion = exampleData[Math.floor(exampleChoice * exampleData.length)]; + setInputValue(userQuestion); + } + const result = await askAI({ - userQuestion: inputValue, + userQuestion, documentContent: documentContent, }); @@ -35,8 +69,18 @@ export function ChatForm({ documentContent }: { documentContent: string }) {