Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions evaluation/scripts/check-extension-smoke.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
49 changes: 29 additions & 20 deletions evaluation/scripts/check-extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,25 @@ async function sourceBuildDigest(): Promise<string> {
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) =>
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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') {
Expand Down Expand Up @@ -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<HTMLIFrameElement>('#main_list')
?.contentDocument?.querySelector('#analysis-result');
return !!result?.querySelector('h5') || !!result?.textContent?.includes('追加分析失敗');
return (
!!result?.textContent?.includes('解釈:') ||
!!result?.textContent?.includes('追加分析失敗')
);
},
{},
{ timeout: 330000 }
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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']);
Expand Down
134 changes: 98 additions & 36 deletions src/content/SummaryButton.tsx
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -55,6 +56,10 @@ const SummaryButton: React.FC<SummaryButtonProps> = ({ rowData, row, iframeDoc }
rowData: summaryRowData,
});

const [diagnosticHost, setDiagnosticHost] = useState<HTMLElement | null>(null);
const listButton = useRef<HTMLButtonElement>(null);
const summaryPending = useRef<Promise<void> | null>(null);

// 行挿入・削除後にisVisibleを再評価するための再レンダリングトリガー
const [, setForceUpdate] = useState(0);
const triggerUpdate = useCallback(() => setForceUpdate((v) => v + 1), []);
Expand All @@ -66,7 +71,9 @@ const SummaryButton: React.FC<SummaryButtonProps> = ({ rowData, row, iframeDoc }

useEffect(() => {
if (priorCacheKey.current && priorCacheKey.current !== cacheKey) {
summaryPending.current = null;
removeSummaryRow();
setDiagnosticHost(null);
triggerUpdate();
}
priorCacheKey.current = cacheKey;
Expand All @@ -78,15 +85,19 @@ const SummaryButton: React.FC<SummaryButtonProps> = ({ 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();
},
Expand All @@ -97,8 +108,12 @@ const SummaryButton: React.FC<SummaryButtonProps> = ({ rowData, row, iframeDoc }
result.retryExtractionMode === 'full',
result.facts?.facts
);
setDiagnosticHost(host);
triggerUpdate();
} else {
setDiagnosticHost(null);
}
return removeSummaryRow;
}, [result, removeSummaryRow, insertSummaryRow, reset, summarize, triggerUpdate]);

useEffect(() => {
Expand All @@ -115,6 +130,10 @@ const SummaryButton: React.FC<SummaryButtonProps> = ({ rowData, row, iframeDoc }
setDiagnosticError(null);
setDiagnosticText(null);
setDiagnosticCopied(false);
const pendingRequest = diagnosticRequest;
return () => {
pendingRequest.current++;
};
}, [result]);
const copyDiagnostic = async () => {
const request = ++diagnosticRequest.current;
Expand Down Expand Up @@ -150,6 +169,7 @@ const SummaryButton: React.FC<SummaryButtonProps> = ({ rowData, row, iframeDoc }

if (isVisible) {
removeSummaryRow();
setDiagnosticHost(null);
reset();
triggerUpdate();
return;
Expand All @@ -160,28 +180,36 @@ const SummaryButton: React.FC<SummaryButtonProps> = ({ 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
Expand All @@ -191,8 +219,14 @@ const SummaryButton: React.FC<SummaryButtonProps> = ({ rowData, row, iframeDoc }
<div>
<div style={containerStyle}>
<button
ref={listButton}
type="button"
onClick={handleClick}
onFocus={(e) => Object.assign(e.currentTarget.style, FOCUS_STYLE)}
onBlur={(e) => {
e.currentTarget.style.removeProperty('outline');
e.currentTarget.style.removeProperty('outline-offset');
}}
disabled={loading}
style={buttonStyle}
onMouseEnter={(e) => {
Expand All @@ -209,34 +243,62 @@ const SummaryButton: React.FC<SummaryButtonProps> = ({ rowData, row, iframeDoc }
{buttonText}
</button>
</div>
{result && (
<button type="button" onClick={() => void copyDiagnostic()} style={{ fontSize: 10 }}>
診断をコピー
</button>
)}
{diagnosticCopied && (
<span role="status" style={{ fontSize: 10 }}>
コピーしました
</span>
)}
{diagnosticText !== null && (
<details open={diagnosticError !== null} style={{ fontSize: 10 }}>
<summary>診断JSONを表示</summary>
<textarea
aria-label="診断JSON"
readOnly
value={diagnosticText}
rows={6}
onFocus={(e) => e.currentTarget.select()}
style={{ width: 320, maxWidth: '80vw', fontSize: 10 }}
/>
</details>
)}
{diagnosticError && (
<span role="alert" style={{ fontSize: 10 }}>
{diagnosticError}
</span>
)}
{result &&
diagnosticHost &&
createPortal(
<div style={{ marginTop: 12, fontSize: 12, lineHeight: 1.6 }}>
<div style={{ display: 'flex', flexWrap: 'wrap', alignItems: 'center', gap: 8 }}>
<button
type="button"
onClick={() => void copyDiagnostic()}
style={ACTION_BUTTON_STYLE}
>
診断JSONをコピー
</button>
<span role="status" aria-live="polite" style={{ color: '#6b7280' }}>
{diagnosticCopied ? 'コピーしました' : ''}
</span>
</div>
{diagnosticText !== null && (
<details open={diagnosticError !== null} style={{ marginTop: 8 }}>
<summary style={{ cursor: 'pointer', padding: '4px 0' }}>診断JSONを表示</summary>
<textarea
aria-label="診断JSON"
readOnly
value={diagnosticText}
rows={6}
onFocus={(e) => e.currentTarget.select()}
style={{
display: 'block',
width: '100%',
maxWidth: '100%',
minWidth: 0,
boxSizing: 'border-box',
resize: 'vertical',
marginTop: 8,
padding: 8,
fontFamily: 'monospace',
fontSize: 12,
lineHeight: 1.5,
border: '1px solid #cbd5e1',
borderRadius: 4,
background: '#ffffff',
color: '#374151',
}}
/>
</details>
)}
{diagnosticError && (
<p
role="alert"
style={{ margin: '8px 0 0', color: '#991b1b', overflowWrap: 'anywhere' }}
>
{diagnosticError}
</p>
)}
</div>,
diagnosticHost
)}
</div>
);
};
Expand Down
Loading
Loading