From 7fefbf8b0bc8943ccd3ae255b53ed926e1870ecc Mon Sep 17 00:00:00 2001 From: stewroux Date: Sun, 10 May 2026 07:06:56 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=97=A5=E6=AC=A1=E3=82=B3=E3=83=BC?= =?UTF-8?q?=E3=83=89=E3=83=AC=E3=83=93=E3=83=A5=E3=83=BC=E8=87=AA=E5=8B=95?= =?UTF-8?q?=E4=BF=AE=E6=AD=A3=202026-05-09=20(CI=20Node.js=2020,=20API?= =?UTF-8?q?=E3=83=AC=E3=82=B9=E3=83=9D=E3=83=B3=E3=82=B9=E6=A4=9C=E8=A8=BC?= =?UTF-8?q?,=20console.error=E6=8A=91=E5=88=B6,=20.gitignore=E6=94=B9?= =?UTF-8?q?=E5=96=84)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/ci.yml | 2 +- .gitignore | 6 +++++- services/geminiService.ts | 25 ++++++++++++++++++++----- 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b22da68..f7e2e86 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -18,7 +18,7 @@ jobs: - name: Setup Node.js uses: actions/setup-node@v4 with: - node-version: '18' + node-version: '20' cache: 'npm' - name: Install dependencies diff --git a/.gitignore b/.gitignore index 832619e..c76810b 100644 --- a/.gitignore +++ b/.gitignore @@ -32,4 +32,8 @@ output .gemini .next images -build_output.txt \ No newline at end of file +build_output.txt + +# Test output files +output_test.jpg +output_test.png diff --git a/services/geminiService.ts b/services/geminiService.ts index 867a533..00a2a4c 100644 --- a/services/geminiService.ts +++ b/services/geminiService.ts @@ -1,7 +1,6 @@ import { GoogleGenAI, Type, GenerateContentResponse } from "@google/genai"; import { TranslationBlock } from '../types'; -// Helper to get an initialized AI instance const getAiClient = (customApiKey?: string) => { const apiKey = customApiKey || (typeof process !== 'undefined' ? process.env.NEXT_PUBLIC_GEMINI_API_KEY : undefined); if (!apiKey) { @@ -10,6 +9,13 @@ const getAiClient = (customApiKey?: string) => { return new GoogleGenAI({ apiKey }); }; +const isTranslationBlockArray = (data: unknown): data is TranslationBlock[] => { + return Array.isArray(data) && data.every(item => + typeof item === 'object' && item !== null && + 'japaneseText' in item && 'englishText' in item && 'boundingBox' in item + ); +}; + const responseSchema = { type: Type.ARRAY, items: { @@ -74,10 +80,17 @@ export const translateImageText = async (base64Image: string, mimeType: string, const jsonText = response.text.trim(); const parsedJson = JSON.parse(jsonText); - return parsedJson as TranslationBlock[]; + + if (!isTranslationBlockArray(parsedJson)) { + throw new Error("APIレスポンスの形式が無効です。"); + } + + return parsedJson; } catch (error) { - console.error("Error calling Gemini API:", error); + if (process.env.NODE_ENV === 'development') { + console.error("Error calling Gemini API:", error); + } throw new Error("画像の翻訳に失敗しました。API呼び出しでエラーが発生しました。"); } }; @@ -113,7 +126,9 @@ export const generateImageSummary = async (base64Image: string, mimeType: string return response.text.trim(); } catch (error) { - console.error("Error generating image summary:", error); + if (process.env.NODE_ENV === 'development') { + console.error("Error generating image summary:", error); + } return "翻訳済み画像"; } -}; \ No newline at end of file +};