import { useEffect, useState } from 'react'; import { Archive, BrainCircuit, FileText, LoaderCircle } from 'lucide-react'; import { api } from './api'; import { Modal } from './components'; import type { ContextHistory, ContextHistoryGroup, ContextPlan, Session } from './types'; const number = (value: number | undefined) => (value ?? 0).toLocaleString('zh-CN'); const states: Record = { prepared: '已准备', requesting: '正在请求', completed: '已收到响应', failed: '请求失败', interrupted: '请求已中断' }; const toolNames: Record = { read_file: '读取文件', read_artifact: '读取保存工件', inspect_project: '识别工程', inspect_session: '核对运行记录', index_project: '建立符号索引', query_code: '定位代码', propose_patch: '精确补丁提案', propose_file: '文件提案', run_command: '终端命令', run_checks: '工程检查', plan_checks: '检查计划', prepare_change_set: '封存变更集', plan_delivery: 'Git交付预览', delivery_report: '交付报告', list_files: '查看文件', search_files: '搜索文件' }; export function RequestContextPanel({ session, runId, openArtifact, onError }: { session: Session; runId: string; openArtifact: (id: string) => void; onError: (error: unknown) => void; }) { const [historical, setHistorical] = useState([]); const [selected, setSelected] = useState(''); const [historyId, setHistoryId] = useState(''); useEffect(() => { let alive = true; setHistorical([]); setSelected(''); setHistoryId(''); if (runId && runId !== session.execution?.run?.id) { api('/sessions/' + session.id + '/context-plans?run=' + runId).then(value => { if (alive) setHistorical(value); }) .catch(error => { if (alive) onError(error); }); } return () => { alive = false; }; }, [session.id, runId, session.execution?.run?.id]); const plans = runId === session.execution?.run?.id ? session.execution?.contextPlans?.filter(plan => plan.runId === runId) ?? [] : historical.filter(plan => plan.runId === runId && plan.sessionId === session.id); const plan = plans.find(plan => plan.id === selected) ?? plans.at(-1); if (!plan) return null; const total = plan.estimatedInputTokens + plan.budget.outputReservation + plan.budget.safetyTokens; return

请求上下文

{states[plan.status] ?? plan.status}
{plans.length > 1 && }
请求输入估算
{number(plan.estimatedInputTokens)} tokens
输出 / 安全余量
{number(plan.budget.outputReservation)} / {number(plan.budget.safetyTokens)} tokens
模型上下文窗口
{number(plan.budget.contextLimit)} tokens
已报告输入
{plan.actualUsage?.prompt_tokens === undefined ? '服务未返回' : number(plan.actualUsage.prompt_tokens) + ' tokens'}
历史记录
保留 {plan.retainedMessages} 条 · 保存 {plan.archivedGroups} 组原记录
{!!plan.workingSetEntries && <>
当前任务依据
{plan.workingSetEntries} 项代码与执行结果
} {!!plan.projectMemoryEntries && <>
项目记忆
{plan.projectMemoryEntries} 条来源已核对的记录
} {!!plan.skillCatalogEntries && <>
技能目录
{plan.skillCatalogEntries} 项本次目录记录
}
发送内容
{number(plan.requestBytes)} B{plan.requestSha256.slice(0, 16)}

估算包含系统提示、工具定义和历史。任务要求完整保留;省略的原记录可从本机工件检索。实际用量以模型服务为准。

{plan.error &&

{plan.error}

}
{plan.workingSetArtifactId && } {plan.latestToolsArtifactId && } {plan.projectMemoryArtifactId && } {plan.skillCatalogArtifactId && } {!!plan.archivedGroups && }
{historyId && setHistoryId('')} openArtifact={id => { setHistoryId(''); openArtifact(id); }} />}
; } function groupTitle(group: ContextHistoryGroup) { if (group.tools?.length) return group.tools.map(tool => toolNames[tool.name] ?? tool.name).join(' · '); return group.roles[0] === 'user' ? '用户要求' : '助手记录'; } function HistoryModal({ sessionId, planId, onClose, openArtifact, onError }: { sessionId: string; planId: string; onClose: () => void; openArtifact: (id: string) => void; onError: (error: unknown) => void; }) { const [history, setHistory] = useState(null), [offset, setOffset] = useState(0), [loading, setLoading] = useState(false); useEffect(() => { let alive = true; setLoading(true); api('/sessions/' + sessionId + '/context-plans/' + planId + '/history?offset=' + offset + '&limit=12') .then(value => { if (alive) setHistory(value); }).catch(error => { if (alive) onError(error); }).finally(() => { if (alive) setLoading(false); }); return () => { alive = false; }; }, [sessionId, planId, offset]); return
{loading ?

正在核对历史工件

: history?.groups.map(group =>
{groupTitle(group)}原消息 {group.startMessage + 1}–{group.endMessage + 1}
SHA256 {group.sha256.slice(0, 20)} {!!group.archive.chunks &&

原记录较大,已保存 {group.archive.chunks} 份可核验分片。

} {group.tools?.map(tool =>
{toolNames[tool.name] ?? tool.name}{tool.call_id} {(tool.requires_verification || tool.result.requires_verification || tool.result.status === 'unknown') && 结果需核对} {tool.result.path && {tool.result.path}}{tool.result_artifact_id && }
)}
)}
记录 {history ? Math.min(history.totalGroups, history.offset + 1) + '–' + Math.min(history.totalGroups, history.offset + history.groups.length) + ' / ' + history.totalGroups : '正在读取'}
; }