Files
Agent-Workbench/src/client/mobile/MobileBrowserObservation.tsx
T

15 lines
2.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { useEffect,useState } from 'react';
import type { MobileTask } from '../companion-types';
import { MobileError } from './api';
export function MobileBrowserObservation({sessionId,observation,onError}:{sessionId:string;observation:NonNullable<MobileTask['browserObservations']>[number];onError:(error:unknown)=>void}) {
const [image,setImage]=useState(''),[failure,setFailure]=useState('');
useEffect(()=>{let alive=true,objectUrl='';const controller=new AbortController();setImage('');setFailure('');
void(async()=>{const response=await fetch('/mobile-api/sessions/'+sessionId+'/browser-observations/'+observation.id+'/image',{credentials:'same-origin',cache:'no-store',signal:controller.signal});if(!response.ok)throw new MobileError('原网页截图读取失败。',response.status);
const bytes=await response.arrayBuffer();if(bytes.byteLength>6*1024*1024)throw new MobileError('原网页截图超过6MiB范围。');const sha=Array.from(new Uint8Array(await crypto.subtle.digest('SHA-256',bytes)),value=>value.toString(16).padStart(2,'0')).join('');
if(sha!==observation.screenshotSha256||sha!==response.headers.get('X-Content-SHA256'))throw new MobileError('原网页截图SHA-256核对失败。');
if(alive){objectUrl=URL.createObjectURL(new Blob([bytes],{type:'image/png'}));setImage(objectUrl);}})().catch(error=>{if(alive){setFailure(error.message);onError(error);}});
return()=>{alive=false;controller.abort();if(objectUrl)URL.revokeObjectURL(objectUrl);};
},[sessionId,observation.id,observation.screenshotSha256]);
return <section className="mobile-input-image"><h2>电脑网页原观察</h2><strong>{observation.title||'所属网页'}</strong><p>{image?'SHA-256 已核对':failure||'正在核对原截图…'} · {new Date(observation.createdAt).toLocaleString('zh-CN')}</p><code>{observation.url}</code>{image&&<details><summary>查看原页面截图</summary><img src={image} alt="电脑上所属标签页的原观察"/><code>{observation.screenshotSha256}</code></details>}<p className="mobile-muted">这是原采样结果。新观察和浏览器控制在电脑执行,动作审批沿用原任务权限。</p></section>;
}