diff --git a/evaluation/scripts/check-extension-smoke.ts b/evaluation/scripts/check-extension-smoke.ts index 89d135d..eaf63a7 100644 --- a/evaluation/scripts/check-extension-smoke.ts +++ b/evaluation/scripts/check-extension-smoke.ts @@ -157,9 +157,7 @@ export async function checkExtensionSmoke(args: string[]) { const summary = frame.locator('.tdnet-digest-summary-row'); await row.getByRole('button', { name: '要約', exact: true }).click({ timeout: 20000 }); await summary.waitFor({ timeout: 30000 }); - await row - .getByRole('button', { name: outcome === 'failure' ? '要約' : '非表示', exact: true }) - .waitFor({ timeout: 30000 }); + await row.getByRole('button', { name: '閉じる', exact: true }).waitFor({ timeout: 30000 }); const readStored = () => worker.evaluate(async (traceKey: string) => { const data = await chrome.storage.local.get(); diff --git a/evaluation/scripts/check-extension.ts b/evaluation/scripts/check-extension.ts index 0702705..48df557 100644 --- a/evaluation/scripts/check-extension.ts +++ b/evaluation/scripts/check-extension.ts @@ -50,19 +50,25 @@ async function sourceBuildDigest(): Promise { return digest.digest('hex'); } /** Verify both the selectable JSON and the actual clipboard, after the user's click. */ -async function copiedDiagnostic(row: any, page: any, blocked = false) { - await row.getByRole('button', { name: '診断をコピー', exact: true }).click(); +async function copiedDiagnostic(summary: any, page: any, blocked = false) { + const info = summary.locator('[data-generation-info]'); + if (!(await info.evaluate((element: HTMLDetailsElement) => element.open))) + await info.locator('summary').first().click(); + await summary.getByRole('button', { name: '診断JSONをコピー', exact: true }).click(); if (blocked) - await row + await summary .getByRole('alert') .filter({ hasText: 'コピーできませんでした' }) .waitFor({ timeout: 10000 }); else - await row.getByRole('status').filter({ hasText: 'コピーしました' }).waitFor({ timeout: 10000 }); - if (!blocked) await row.getByText('診断JSONを表示', { exact: true }).click(); - const text = await row.getByRole('textbox', { name: '診断JSON', exact: true }).inputValue(); + await summary + .getByRole('status') + .filter({ hasText: 'コピーしました' }) + .waitFor({ timeout: 10000 }); + if (!blocked) await summary.getByText('診断JSONを表示', { exact: true }).click(); + const text = await summary.getByRole('textbox', { name: '診断JSON', exact: true }).inputValue(); if (blocked) { - const box = row.getByRole('textbox', { name: '診断JSON', exact: true }); + const box = summary.getByRole('textbox', { name: '診断JSON', exact: true }); await box.focus(); assert.equal( await box.evaluate((el: HTMLTextAreaElement) => @@ -642,7 +648,7 @@ export async function checkExtension(item: BrowserCase, config: LLMConfig, args: assert.equal(trace.resultId, null); assert.deepEqual(trace.attempts, []); assert.deepEqual(trace.usage, []); - assert.deepEqual(await copiedDiagnostic(row, page, copyBlocked), trace); + assert.deepEqual(await copiedDiagnostic(summary, page, copyBlocked), trace); assert.equal(apiCalls, 0); assert.equal(pdfRequests, 0); evidence.trace = trace; @@ -756,7 +762,7 @@ export async function checkExtension(item: BrowserCase, config: LLMConfig, args: trace.attempts.map((a: any) => a.phase), ['first', 'repair'] ); - assert.deepEqual(await copiedDiagnostic(row, page), trace); + assert.deepEqual(await copiedDiagnostic(summary, page), trace); assert.equal(apiCalls, 2); evidence.stages.push( reviewCase === 'assertion-conflict' @@ -898,7 +904,7 @@ export async function checkExtension(item: BrowserCase, config: LLMConfig, args: await toggles.evaluateAll((nodes: HTMLDetailsElement[]) => nodes.forEach((n) => (n.open = false)) ); - await row.getByRole('button', { name: '非表示', exact: true }).click(); + await row.getByRole('button', { name: '閉じる', exact: true }).click(); assert.equal(await frame.locator('.tdnet-digest-summary-row').count(), 0); await row.getByRole('button', { name: '表示', exact: true }).click(); await summary.waitFor(); @@ -926,7 +932,7 @@ export async function checkExtension(item: BrowserCase, config: LLMConfig, args: trace.attempts.map((a: any) => a.phase), narrativeReplay.attempts.map((a: any) => a.phase) ); - assert.deepEqual(await copiedDiagnostic(row, page), trace); + assert.deepEqual(await copiedDiagnostic(summary, page), trace); evidence.trace = trace; evidence.stages.push( 'public PDF → Offscreen → replayed extraction/synthesis/review → exact facts/presentation → closed source toggles → full original → cache restore without API → diagnostic clipboard' @@ -1245,7 +1251,7 @@ export async function checkExtension(item: BrowserCase, config: LLMConfig, args: 'real normal generation PDF/input/model/mode/contract/request limits match CLI conditions' ); } - const exported = await copiedDiagnostic(row, page); + const exported = await copiedDiagnostic(summary, page); assert.deepEqual(exported, trace); evidence.stages.push('phase/raw/diagnostics/hash/usage trace copied from UI'); evidence.sourceHash = stored.value.metadata.documentHash; @@ -1410,7 +1416,7 @@ export async function checkExtension(item: BrowserCase, config: LLMConfig, args: ); evidence.stages.push('current stored facts pass ordinary source meaning verification'); } else assert.deepEqual(expectedErrors(item, stored.value.facts), []); - await row.getByRole('button', { name: '非表示', exact: true }).click(); + await row.getByRole('button', { name: '閉じる', exact: true }).click(); const callsBefore = apiCalls; await row.getByRole('button', { name: '表示', exact: true }).click(); await summary @@ -1459,14 +1465,14 @@ export async function checkExtension(item: BrowserCase, config: LLMConfig, args: if (reviewDiagnostics) { // A cached result has no current request ID, but its exact result ID must match. - assert.deepEqual(await copiedDiagnostic(row, page), trace); + assert.deepEqual(await copiedDiagnostic(summary, page), trace); evidence.stages.push('cached result ID matches diagnostic export after reload'); await worker.evaluate(async () => chrome.storage.sync.set({ apiKey: '' })); await summary.getByRole('button', { name: '再要約', exact: true }).click(); await summary .getByText('APIキーが設定されていません', { exact: false }) .waitFor({ timeout: 10000 }); - const failed = await copiedDiagnostic(row, page); + const failed = await copiedDiagnostic(summary, page); assert.equal(apiCalls, callsBefore); const failure = await worker.evaluate( async () => (await chrome.storage.local.get('summaryLastRunV1')).summaryLastRunV1 @@ -1493,7 +1499,7 @@ export async function checkExtension(item: BrowserCase, config: LLMConfig, args: if (reviewFixture) { const priorCalls = apiCalls; - assert.deepEqual(await copiedDiagnostic(row, page), trace); + assert.deepEqual(await copiedDiagnostic(summary, page), trace); evidence.stages.push('restored result ID matches exported diagnostic'); const altered = structuredClone(stored.value.facts); if (reviewCase === 'attributes') { @@ -1528,13 +1534,16 @@ export async function checkExtension(item: BrowserCase, config: LLMConfig, args: evidence.stages.push('altered saved facts refused before followup API'); } if ((!fixed || reviewFixture) && args.includes('--live-followups')) { - await summary.getByRole('button', { name: '追加分析', exact: true }).click(); + await summary.getByRole('button', { name: '追加分析する', exact: true }).click(); await page.waitForFunction( () => { const result = document .querySelector('#main_list') ?.contentDocument?.querySelector('#analysis-result'); - return !!result?.querySelector('h5') || !!result?.textContent?.includes('追加分析失敗'); + return ( + !!result?.textContent?.includes('解釈:') || + !!result?.textContent?.includes('追加分析失敗') + ); }, {}, { timeout: 330000 } @@ -1578,7 +1587,7 @@ export async function checkExtension(item: BrowserCase, config: LLMConfig, args: ); } if (fixed && !reviewFixture) { - await summary.getByRole('button', { name: '追加分析', exact: true }).click(); + await summary.getByRole('button', { name: '追加分析する', exact: true }).click(); await page.waitForFunction( () => document @@ -1625,7 +1634,7 @@ export async function checkExtension(item: BrowserCase, config: LLMConfig, args: : 'score retry refuses missing comparisons' ); await worker.evaluate(async () => chrome.storage.sync.set({ experimentalScoring: false })); - await row.getByRole('button', { name: '非表示', exact: true }).click(); + await row.getByRole('button', { name: '閉じる', exact: true }).click(); await worker.evaluate( async (request: any) => { const settings = await chrome.storage.sync.get(['provider', 'model', 'extractionMode']); diff --git a/src/content/SummaryButton.tsx b/src/content/SummaryButton.tsx index 199b86e..4ff79c0 100644 --- a/src/content/SummaryButton.tsx +++ b/src/content/SummaryButton.tsx @@ -1,8 +1,9 @@ import React, { useEffect, useState, useCallback, useMemo, useRef } from 'react'; +import { createPortal } from 'react-dom'; import { useSummarize } from './hooks/useSummarize'; import { useSummaryRow } from './hooks/useSummaryRow'; import { SUMMARY_TRACE_KEY, matchingSummaryTrace } from '@/lib/summary-trace'; -import { BUTTON_STYLES } from './constants/styles'; +import { ACTION_BUTTON_STYLE, BUTTON_STYLES, FOCUS_STYLE } from './constants/styles'; interface RowData { time: string; @@ -55,6 +56,10 @@ const SummaryButton: React.FC = ({ rowData, row, iframeDoc } rowData: summaryRowData, }); + const [diagnosticHost, setDiagnosticHost] = useState(null); + const listButton = useRef(null); + const summaryPending = useRef | null>(null); + // 行挿入・削除後にisVisibleを再評価するための再レンダリングトリガー const [, setForceUpdate] = useState(0); const triggerUpdate = useCallback(() => setForceUpdate((v) => v + 1), []); @@ -66,7 +71,9 @@ const SummaryButton: React.FC = ({ rowData, row, iframeDoc } useEffect(() => { if (priorCacheKey.current && priorCacheKey.current !== cacheKey) { + summaryPending.current = null; removeSummaryRow(); + setDiagnosticHost(null); triggerUpdate(); } priorCacheKey.current = cacheKey; @@ -78,15 +85,19 @@ const SummaryButton: React.FC = ({ rowData, row, iframeDoc } useEffect(() => { if (result) { removeSummaryRow(); - insertSummaryRow( + const host = insertSummaryRow( result.summary, result.error, result.metadata, () => { + setDiagnosticHost(null); + listButton.current?.focus(); reset(); summarize('full'); }, () => { + setDiagnosticHost(null); + listButton.current?.focus(); reset(); summarize(); }, @@ -97,8 +108,12 @@ const SummaryButton: React.FC = ({ rowData, row, iframeDoc } result.retryExtractionMode === 'full', result.facts?.facts ); + setDiagnosticHost(host); triggerUpdate(); + } else { + setDiagnosticHost(null); } + return removeSummaryRow; }, [result, removeSummaryRow, insertSummaryRow, reset, summarize, triggerUpdate]); useEffect(() => { @@ -115,6 +130,10 @@ const SummaryButton: React.FC = ({ rowData, row, iframeDoc } setDiagnosticError(null); setDiagnosticText(null); setDiagnosticCopied(false); + const pendingRequest = diagnosticRequest; + return () => { + pendingRequest.current++; + }; }, [result]); const copyDiagnostic = async () => { const request = ++diagnosticRequest.current; @@ -150,6 +169,7 @@ const SummaryButton: React.FC = ({ rowData, row, iframeDoc } if (isVisible) { removeSummaryRow(); + setDiagnosticHost(null); reset(); triggerUpdate(); return; @@ -160,28 +180,36 @@ const SummaryButton: React.FC = ({ rowData, row, iframeDoc } return; } - summarize(); + // 同一描画内の連打でも要約要求は一度だけ送る。 + if (summaryPending.current) return; + const request = summarize(); + summaryPending.current = request; + void request.finally(() => { + if (summaryPending.current === request) summaryPending.current = null; + }); }; // ボタンテキスト - const buttonText = loading ? '...' : hasCached ? (isVisible ? '非表示' : '表示') : '要約'; + const buttonText = loading ? '要約中' : isVisible ? '閉じる' : hasCached ? '表示' : '要約'; // スタイル: キャッシュ済みかどうかで分岐 const containerStyle = - hasCached && !loading + (hasCached || isVisible) && !loading ? BUTTON_STYLES.containerCached(isVisible) : BUTTON_STYLES.container(loading); const buttonStyle = - hasCached && !loading ? BUTTON_STYLES.buttonCached(isVisible) : BUTTON_STYLES.button(loading); + (hasCached || isVisible) && !loading + ? BUTTON_STYLES.buttonCached(isVisible) + : BUTTON_STYLES.button(loading); const hoverBackground = - hasCached && !loading + (hasCached || isVisible) && !loading ? isVisible ? BUTTON_STYLES.buttonCachedHover : BUTTON_STYLES.buttonCachedShowHover : BUTTON_STYLES.buttonHover; const normalBackground = - hasCached && !loading + (hasCached || isVisible) && !loading ? isVisible ? BUTTON_STYLES.buttonCached(true).background : BUTTON_STYLES.buttonCached(false).background @@ -191,8 +219,14 @@ const SummaryButton: React.FC = ({ rowData, row, iframeDoc }
- {result && ( - - )} - {diagnosticCopied && ( - - コピーしました - - )} - {diagnosticText !== null && ( -
- 診断JSONを表示 -