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/(docs)/@docs/[lang]/[pageId]/chatForm.tsx b/app/(docs)/@docs/[lang]/[pageId]/chatForm.tsx index 1a1abb6e..c5165cec 100644 --- a/app/(docs)/@docs/[lang]/[pageId]/chatForm.tsx +++ b/app/(docs)/@docs/[lang]/[pageId]/chatForm.tsx @@ -1,6 +1,6 @@ "use client"; -import { useState, FormEvent, useEffect, useRef, useCallback } from "react"; +import { useState, FormEvent, useEffect, useRef, useCallback, useMemo } from "react"; // import useSWR from "swr"; // import { // getQuestionExample, @@ -26,8 +26,6 @@ export function ChatForm({ path, sectionContent, close }: ChatFormProps) { const [isLoading, setIsLoading] = useState(false); const [errorMessage, setErrorMessage] = useState(null); - // const lang = getLanguageName(docs_id); - const { files, replOutputs, execResults } = useEmbedContext(); const router = useRouter(); @@ -64,42 +62,43 @@ export function ChatForm({ path, sectionContent, close }: ChatFormProps) { } }, [pathname]); - // 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 = useMemo( + () => + sectionContent + .filter((s) => s.inView) + .map((s) => s.question) + .filter((qe) => qe !== undefined) + .flat(), + // eslint-disable-next-line react-hooks/exhaustive-deps + [] + ); // 質問フォームを開くたびにランダムに選び直し、 // 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) => { + + let userQuestion = inputValue; + if (!userQuestion && exampleData.length > 0 && exampleChoice) { + // 質問が空欄なら、質問例を使用 + userQuestion = + exampleData[Math.floor(exampleChoice * exampleData.length)]; + setInputValue(userQuestion); + } + if (!userQuestion) { + return; + } + e.preventDefault(); setIsLoading(true); - setErrorMessage(null); - - const userQuestion = inputValue; + setErrorMessage(null); // Clear previous error message let response: Response; try { @@ -214,10 +213,10 @@ export function ChatForm({ path, sectionContent, close }: ChatFormProps) {