11"use client" ;
22
33import { useState , FormEvent } from "react" ;
4+ import { askAI } from "@/app/actions/chatActions" ;
45
5- interface ChatApiResponse {
6- response : string ;
7- }
8-
9- export function ChatForm ( ) {
6+ export function ChatForm ( { documentContent } : { documentContent : string } ) {
107 const [ inputValue , setInputValue ] = useState ( "" ) ;
118 const [ response , setResponse ] = useState ( "" ) ;
129 const [ isLoading , setIsLoading ] = useState ( false ) ;
@@ -17,96 +14,85 @@ 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 result = await askAI ( {
18+ userQuestion : inputValue ,
19+ documentContent : documentContent ,
20+ } ) ;
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 } ` ) ;
37- } else {
38- setResponse ( `エラー: ${ String ( error ) } ` ) ;
39- }
40- } finally {
41- setIsLoading ( false ) ;
22+ if ( result . error ) {
23+ setResponse ( `エラー: ${ result . error } ` ) ;
24+ } else {
25+ setResponse ( result . response ) ;
4226 }
27+
28+ setIsLoading ( false ) ;
4329 } ;
4430 return (
4531 < >
4632 { isFormVisible && (
4733 < form className = "border border-2 border-primary shadow-xl p-6 rounded-lg bg-base-100" style = { { width :"100%" , textAlign :"center" , boxShadow :"-moz-initial" } } onSubmit = { handleSubmit } >
48- < h2 className = "text-xl font-bold mb-4 text-left relative -top-2 font-mono h-2" >
49- AIへ質問
50- </ h2 >
34+ < h2 className = "text-xl font-bold mb-4 text-left relative -top-2 font-mono h-2" >
35+ AIへ質問
36+ </ h2 >
5137 < div className = "input-area" style = { { height :"80px" } } >
52- < textarea
53- className = "textarea textarea-white textarea-md"
54- placeholder = "質問を入力してください"
38+ < textarea
39+ className = "textarea textarea-white textarea-md"
40+ placeholder = "質問を入力してください"
5541 style = { { width : "100%" , height : "110px" , resize : "none" } }
56- value = { inputValue }
57- onChange = { ( e ) => setInputValue ( e . target . value ) }
58- disabled = { isLoading }
59- > </ textarea >
60- </ div >
61- < br />
42+ value = { inputValue }
43+ onChange = { ( e ) => setInputValue ( e . target . value ) }
44+ disabled = { isLoading }
45+ > </ textarea >
46+ </ div >
47+ < br />
6248 < div className = "controls" style = { { position :"relative" , top :"22px" , display :"flex" , alignItems :"center" , justifyContent :"space-between" } } >
63- < div className = "left-icons" >
64- < button
65- className = "btn btn-soft btn-secondary rounded-full"
66- onClick = { ( ) => setIsFormVisible ( false ) }
67- >
49+ < div className = "left-icons" >
50+ < button
51+ className = "btn btn-soft btn-secondary rounded-full"
52+ onClick = { ( ) => setIsFormVisible ( false ) }
53+ >
6854
69- 閉じる
70- </ button >
71- </ div >
72- < div className = "right-controls" >
73- < button
74- type = "submit"
75- className = "btn btn-soft btn-circle btn-primary rounded-full"
76- title = "送信"
55+ 閉じる
56+ </ button >
57+ </ div >
58+ < div className = "right-controls" >
59+ < button
60+ type = "submit"
61+ className = "btn btn-soft btn-circle btn-primary rounded-full"
62+ title = "送信"
7763 style = { { marginTop :"10px" } }
78- disabled = { isLoading }
79- >
80- < span className = "icon" > ➤</ span >
81- </ button >
64+ disabled = { isLoading }
65+ >
66+ < span className = "icon" > ➤</ span >
67+ </ button >
68+ </ div >
8269 </ div >
83- </ div >
84- </ form >
70+ </ form >
8571 ) }
8672 { ! isFormVisible && (
87- < button
88- className = "btn btn-soft btn-secondary rounded-full"
89- onClick = { ( ) => setIsFormVisible ( true ) }
90- >
91- チャットを開く
92- </ button >
73+ < button
74+ className = "btn btn-soft btn-secondary rounded-full"
75+ onClick = { ( ) => setIsFormVisible ( true ) }
76+ >
77+ チャットを開く
78+ </ button >
9379 ) }
9480
9581 { response && (
9682 < article >
9783 < h3 className = "text-lg font-semibold mb-2" > AIの回答</ h3 >
9884 < div className = "chat chat-start" >
9985 < div className = "chat-bubble chat-bubble-primary" >
100- < div className = "response-container" > { response } </ div >
86+ < div className = "response-container" > { response } </ div >
10187 </ div >
10288 </ div >
10389 </ article >
10490 ) }
10591
10692 { isLoading && (
107- < div className = "mt-2 text-l text-gray-500 animate-pulse" >
108- AIが考え中です…
109- </ div >
93+ < div className = "mt-2 text-l text-gray-500 animate-pulse" >
94+ AIが考え中です…
95+ </ div >
11096 ) }
11197
11298 </ >
0 commit comments