diff --git a/.github/workflows/question-example.yml b/.github/workflows/question-example.yml new file mode 100644 index 00000000..b5a567ff --- /dev/null +++ b/.github/workflows/question-example.yml @@ -0,0 +1,37 @@ +name: Generate Question Examples + +on: + pull_request: + branches: [ "main" ] + # paths: + # - 'public/docs/**' + +jobs: + question-example: + runs-on: ubuntu-latest + # Push権限を付与 + permissions: + contents: write + + steps: + - uses: actions/checkout@v6 + with: + ref: ${{ github.head_ref }} + - uses: actions/setup-node@v4 + with: + node-version: ${{ matrix.node-version }} + cache: 'npm' + - run: npm ci + + - run: npx tsx ./scripts/questionExample.ts 4 + env: + API_KEY: ${{ secrets.API_KEY }} + + - name: Commit and Push changes + # 前のステップが成功・失敗どちらでも必ず実行 + if: always() + run: | + git config user.name "github-actions[bot]" + git config user.email "41898282+github-actions[bot]@users.noreply.github.com" + git add . + git diff --staged --exit-code || (git commit -m "[ci] generate question examples" && git push) diff --git a/app/[lang]/[pageId]/chatForm.tsx b/app/[lang]/[pageId]/chatForm.tsx index 0231dc86..5e0a3011 100644 --- a/app/[lang]/[pageId]/chatForm.tsx +++ b/app/[lang]/[pageId]/chatForm.tsx @@ -27,75 +27,60 @@ export function ChatForm({ path, sectionContent, close }: ChatFormProps) { const { addChat } = useChatHistoryContext(); - // const lang = getLanguageName(docs_id); - const { files, replOutputs, execResults } = useEmbedContext(); - // const documentContentInView = sectionContent - // .filter((s) => s.inView) - // .map((s) => s.rawContent) - // .join("\n\n"); - // const { data: exampleData, error: exampleError } = useSWR( - // // 質問フォームを開いたときだけで良い - // { - // lang, - // documentContent: documentContentInView, - // } satisfies QuestionExampleParams, - // getQuestionExample, - // { - // // リクエストは古くても構わないので1回でいい - // revalidateIfStale: false, - // revalidateOnFocus: false, - // revalidateOnReconnect: false, - // } - // ); - // if (exampleError) { - // console.error("Error getting question example:", exampleError); - // } + const exampleData = sectionContent + .filter((s) => s.inView) + .map((s) => s.question) + .filter((qe) => qe !== undefined) + .flat(); // 質問フォームを開くたびにランダムに選び直し、 // exampleData[Math.floor(exampleChoice * exampleData.length)] を採用する - const [exampleChoice, setExampleChoice] = useState(0); // 0〜1 + const [exampleChoice, setExampleChoice] = useState( + undefined + ); // 0〜1 useEffect(() => { - if (exampleChoice === 0) { + if (exampleChoice === undefined) { setExampleChoice(Math.random()); } }, [exampleChoice]); const handleSubmit = async (e: FormEvent) => { - e.preventDefault(); - setIsLoading(true); - setErrorMessage(null); // Clear previous error message + let userQuestion = inputValue; + if (!userQuestion && exampleData.length > 0 && exampleChoice) { + // 質問が空欄なら、質問例を使用 + userQuestion = + exampleData[Math.floor(exampleChoice * exampleData.length)]; + setInputValue(userQuestion); + } + if (userQuestion) { + e.preventDefault(); + setIsLoading(true); + setErrorMessage(null); // Clear previous error message - const userQuestion = inputValue; - // if (!userQuestion && exampleData) { - // // 質問が空欄なら、質問例を使用 - // userQuestion = - // exampleData[Math.floor(exampleChoice * exampleData.length)]; - // setInputValue(userQuestion); - // } + const result = await askAI({ + path, + userQuestion, + sectionContent, + replOutputs, + files, + execResults, + }); - const result = await askAI({ - path, - userQuestion, - sectionContent, - replOutputs, - files, - execResults, - }); + if (result.error !== null) { + setErrorMessage(result.error); + console.log(result.error); + } else { + addChat(result.chat); + document.getElementById(result.chat.sectionId)?.scrollIntoView({ + behavior: "smooth", + }); + setInputValue(""); + close(); + } - if (result.error !== null) { - setErrorMessage(result.error); - console.log(result.error); - } else { - addChat(result.chat); - document.getElementById(result.chat.sectionId)?.scrollIntoView({ - behavior: "smooth", - }); - setInputValue(""); - close(); + setIsLoading(false); } - - setIsLoading(false); }; return ( @@ -110,10 +95,10 @@ export function ChatForm({ path, sectionContent, close }: ChatFormProps) {