15 Commits
Author SHA1 Message Date
admin 9b746db452 Merge feature/admin-console: 管理后台 + 用户端鉴权(登录/授权/数据隔离/日志) 2026-10-16 16:01:13 +08:00
admin d9abd0b86a feat(auth): 用户端鉴权(登录/授权清单/逐次校验/功能门禁 + owner 数据隔离) 2026-10-16 14:42:23 +08:00
admin 282415fcb1 feat(admin): 行为日志记录 hub_id(便于区分不同中枢) 2026-10-15 23:56:42 +08:00
admin 1ca2241a97 feat(admin): 增加中枢设置(自定义 ID 与名称,右上角设置入口) 2026-10-15 23:52:17 +08:00
admin 8b5095f70f Merge pull request 'feat(review): P5 科研汇报助手(论文 → PPT 大纲 → PPTX)' (#5) from wjq/PlantAI-rag-new:feature/p5-ppt-assistant into main
Reviewed-on: http://8.163.92.8:3000/admin/PlantAI-rag-new/pulls/5
2026-10-15 20:19:30 +08:00
potaser f5a951f47a docs: add P5 PR report 2026-10-15 19:35:51 +08:00
potaser 7ced30fa96 feat(review): P5 科研汇报助手 + 14 风格 + 真图嵌入 2026-10-15 18:51:59 +08:00
admin c78df3a6a6 feat(admin): 新增管理后台(登录/用户管理/日志查看) + 行为日志文件采集(请求中间件 + log_event) 2026-10-15 18:43:19 +08:00
admin 69e3f35a43 Merge pull request 'feat(review): P3b+P4 论文重排版支持编译 PDF(含 zip 项目)' (#4) from wjq/PlantAI-rag-new:feature/p3b-compile-ui into main
Reviewed-on: http://8.163.92.8:3000/admin/PlantAI-rag-new/pulls/4
2026-10-15 18:30:14 +08:00
potaser de0b9f92be fix(review): EPS batch conversion in zip compile (v2)
- v1 fell through because it required pre-converted PDF in zip
- v2: convert all .eps files post-extract via gs -dSAFER
- 15s timeout per file; fail per-file, not blocking
- Size guards: 20MB per file warning, 100MB total abort
- Conversion log written to output.log
- Verified on real MDPI official zip → 7-page PDF

v1 lesson: test sample had pre-converted PDF but real zip does not
2026-10-14 09:47:49 +08:00
potaser 2797145104 feat(review): P4 two-phase zip compile + XDV→PDF diagnostics + EPS fallback
- inspect/upload zip then compile project (main-file detection, zip safety,
  single-slot queue, failed-envelope carries compile_id + log_url)
- keep failed project/ scene 24h (failed_project_ttl_s; KEEP_FAILED_PROJECTS=1
  forever), success deletes immediately; two-tier compile_jobs sweep
- add tectonic --print and persist full engine chatter to output.log so
  XDV→PDF (xdvipdfmx) errors are visible (MDPI: logo EPS inclusion failure)
- EPS fallback: swap X.eps refs for *-eps-converted-to.pdf when present and
  materialize X.pdf for extension-less graphicx lookup; friendly error when
  no converted PDF exists
- frontend: ReformatPanel zip upload/inspect/run flow; one-off
  scripts/debug_compile.py (gitignored)
- tests: backend 169 passed (22 zip_safety + 34 compile_zip + P3a regression),
  frontend edu vitest 43 passed
2026-10-13 18:42:04 +08:00
potaser 4e19350115 docs: add P3b PR report 2026-10-13 11:23:01 +08:00
potaser e036301e1a feat(review): show format rules summary per target journal
- New summary row: shows the journal's rules (even if no diff applies)
- Fixes user confusion when switching journals yields same diff
- When all rules are null: 'no explicit format requirements'
- Vitest: add case for summary rendering
2026-10-13 11:22:52 +08:00
potaser da84dff9bb fix(review): persist uploaded .tex file in reformat panel
- File now stored in edu.js store (reformatFile)
- Switching target journal no longer requires re-upload
- Explicit reset button clears file
- Vitest: 4 cases
2026-10-13 11:20:33 +08:00
potaser 3a72d3a861 feat(review): P3b enable compile PDF in reformat panel
- ReformatPanel: compile tab state machine (idle/loading/success/error)
- Blob URL for PDF preview; backend path hidden
- First-compile 1-3min hint banner (dismissible)
- QUEUE_FULL 429 with fixed message
- Store: compileStatus/compileResult/compileError + actions
- prewarm_tectonic.sh: minimal .tex compilation to pre-cache TeX bundles
- Vitest: 6 new cases (guide / button / loading / success / error / queue-full)
2026-10-13 10:24:55 +08:00
48 changed files with 6419 additions and 99 deletions
+7 -4
View File
@@ -54,14 +54,17 @@ review_service/data/reformat_jobs/
review_service/tools/tectonic
review_service/data/tectonic-cache/
review_service/data/format_cache/
review_service/data/compile_jobs/
review_service/data/compile_inspect/
review_service/prewarm.log
# 部署方案文档(含主机凭据,不入库)
docs/DEPLOY_PAPER_176.md
# P5 e2e 临时产物
review_service/data/compile_jobs/
review_service/prewarm.log
scripts/
# 一次性诊断脚本(不入库)
scripts/debug_compile.py
# P5 e2e 临时产物(compile_jobs/ 与 prewarm.log 已在上方「review_service 运行时产物」段,此处不重复)
# P5 ppt job 运行数据(大纲/pptx/真图/预览,不入库)
review_service/data/ppt_jobs/
+12
View File
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="zh-CN" data-theme="dark">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
<title>蜂群中枢 · PlantAI 管理后台</title>
</head>
<body>
<div id="app"></div>
<script type="module" src="/src/main.js"></script>
</body>
</html>
+1147
View File
File diff suppressed because it is too large Load Diff
+18
View File
@@ -0,0 +1,18 @@
{
"name": "plantai-admin",
"version": "0.1.0",
"private": true,
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview"
},
"dependencies": {
"vue": "3.5.13"
},
"devDependencies": {
"vite": "5.4.11",
"@vitejs/plugin-vue": "5.2.1"
}
}
+143
View File
@@ -0,0 +1,143 @@
<script setup>
import { ref, onMounted } from 'vue';
import { getToken, setToken, api } from './api/admin.js';
import Login from './views/Login.vue';
import Users from './views/Users.vue';
import Logs from './views/Logs.vue';
import HubSettings from './components/HubSettings.vue';
const authed = ref(false);
const page = ref('overview');
const user = ref(null);
const stats = ref({ users: 0, total: 0, denied: 0, p95: null });
const hub = ref({ hub_id: '', hub_name: '蜂群中枢' });
const settingsOpen = ref(false);
const PAGES = { overview: '概览', users: '用户管理', logs: '日志查看' };
function onUnauthorized() { setToken(''); authed.value = false; }
async function loadHub() {
try {
const r = await api.settings();
hub.value = r.settings;
} catch (e) {
if (e.status === 401) onUnauthorized();
}
}
async function loadStats() {
try {
const [u, l] = await Promise.all([api.users(), api.logs({ page: 1, page_size: 1 })]);
stats.value.users = u.users.length;
stats.value.total = l.total;
stats.value.denied = (l.aggregates && l.aggregates.denied) || 0;
stats.value.p95 = (l.aggregates && l.aggregates.p95_latency_ms) || null;
} catch (e) {
if (e.status === 401) onUnauthorized();
}
}
async function init() {
if (!getToken()) { authed.value = false; return; }
try {
const r = await api.me();
user.value = r.user;
authed.value = true;
loadHub();
loadStats();
} catch (e) {
setToken('');
authed.value = false;
}
}
function onLogin(u) { user.value = u; authed.value = true; page.value = 'overview'; loadHub(); loadStats(); }
async function onLogout() { try { await api.logout(); } catch {} setToken(''); authed.value = false; }
function onSettingsSaved(s) { hub.value = s; settingsOpen.value = false; }
onMounted(init);
</script>
<template>
<Login v-if="!authed" @login="onLogin" />
<div v-else class="shell">
<aside class="sidebar">
<div class="side-brand">
<div class="hex">蜂</div>
<div><div class="brand-name">{{ hub.hub_name }}</div><div class="brand-sub">{{ hub.hub_id.slice(0, 8) }}</div></div>
</div>
<nav class="nav">
<div class="nav-item" :class="{ active: page === 'overview' }" @click="page = 'overview'"><span>◈</span> 概览</div>
<div class="nav-item" :class="{ active: page === 'users' }" @click="page = 'users'"><span>◉</span> 用户管理</div>
<div class="nav-item" :class="{ active: page === 'logs' }" @click="page = 'logs'"><span>≣</span> 日志查看</div>
</nav>
<div class="side-foot">
<div class="who">
<div class="avatar">蜂</div>
<div><div class="who-name">{{ user?.username }}</div><div class="who-role">系统管理员</div></div>
<span class="logout" @click="onLogout">退出</span>
</div>
</div>
</aside>
<main class="main">
<div class="topbar">
<h1>{{ PAGES[page] }}</h1>
<span style="color: var(--text-3); font-size: 13px">{{ hub.hub_name }}</span>
<button class="btn btn-ghost" style="margin-left: auto" @click="settingsOpen = true">⚙ 设置</button>
</div>
<div class="content">
<section v-if="page === 'overview'">
<div class="stats">
<div class="stat"><div class="k">{{ stats.users }}</div><div class="l">注册用户</div></div>
<div class="stat"><div class="k amber">{{ stats.total }}</div><div class="l">累计请求</div></div>
<div class="stat"><div class="k">{{ stats.denied }}</div><div class="l">越权 / 失败</div></div>
<div class="stat"><div class="k">{{ stats.p95 ?? '—' }}</div><div class="l">P95 耗时 ms</div></div>
</div>
</section>
<Users v-else-if="page === 'users'" @unauthorized="onUnauthorized" />
<Logs v-else @unauthorized="onUnauthorized" />
</div>
</main>
<HubSettings :open="settingsOpen" :hub="hub" @close="settingsOpen = false" @saved="onSettingsSaved" />
</div>
</template>
<style>
.shell { display: flex; height: 100vh; }
.sidebar { width: 218px; flex: none; background: var(--bg-2); border-right: 1px solid var(--border); display: flex; flex-direction: column; }
.side-brand { display: flex; align-items: center; gap: 10px; padding: 20px 18px 18px; }
.hex { width: 30px; height: 30px; flex: none; background: linear-gradient(135deg, #f2b95a, #d98f22); clip-path: polygon(50% 0, 100% 25%, 100% 75%, 50% 100%, 0 75%, 0 25%); display: grid; place-items: center; color: #1a1206; font-weight: 700; font-size: 12px; }
.brand-name { font-size: 15px; font-weight: 600; }
.brand-sub { font-size: 11px; color: var(--text-3); }
.nav { padding: 6px 10px; flex: 1; }
.nav-item { display: flex; align-items: center; gap: 9px; padding: 9px 12px; margin-bottom: 3px; border-radius: var(--radius-sm); color: var(--text-2); font-size: 13.5px; cursor: pointer; }
.nav-item:hover { background: var(--surface); color: var(--text); }
.nav-item.active { background: var(--accent-soft); color: var(--accent); }
.side-foot { padding: 14px 18px; border-top: 1px solid var(--border); }
.who { display: flex; align-items: center; gap: 10px; }
.avatar { width: 30px; height: 30px; border-radius: 50%; flex: none; background: linear-gradient(135deg, #3a4a5e, #243146); display: grid; place-items: center; font-size: 12px; color: var(--text); }
.who-name { font-size: 13px; }
.who-role { font-size: 11px; color: var(--text-3); }
.logout { margin-left: auto; color: var(--text-3); cursor: pointer; font-size: 12px; }
.logout:hover { color: var(--red); }
.main { flex: 1; overflow-y: auto; display: flex; flex-direction: column; }
.topbar { display: flex; align-items: center; gap: 14px; padding: 18px 28px; border-bottom: 1px solid var(--border); position: sticky; top: 0; background: rgba(12, 14, 20, 0.9); backdrop-filter: blur(6px); z-index: 5; }
.topbar h1 { font-size: 17px; margin: 0; font-weight: 600; }
.content { padding: 26px 28px 40px; }
.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 16px; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px 20px; }
.stat .k { font-size: 26px; font-weight: 600; letter-spacing: -0.5px; }
.stat .k.amber { color: var(--accent); }
.stat .l { color: var(--text-2); font-size: 12.5px; margin-top: 3px; }
.toolbar { display: flex; align-items: center; gap: 10px; margin-bottom: 16px; }
.toolbar .spacer { flex: 1; }
.search { display: flex; align-items: center; gap: 8px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 8px 12px; width: 240px; }
.search input { background: none; border: none; outline: none; color: var(--text); font-size: 13px; width: 100%; }
select { background: var(--surface); border: 1px solid var(--border); color: var(--text); border-radius: var(--radius-sm); padding: 8px 10px; font-size: 13px; outline: none; }
.pag { display: flex; align-items: center; gap: 8px; padding: 12px 18px; border-top: 1px solid var(--border); color: var(--text-3); font-size: 12.5px; }
.pag .spacer { flex: 1; }
.pag button { background: var(--surface-2); border: 1px solid var(--border); color: var(--text-2); border-radius: 6px; padding: 5px 11px; font-size: 12.5px; }
.pag button.on { color: var(--accent); border-color: var(--accent); }
.pag button:disabled { opacity: 0.4; cursor: default; }
</style>
+48
View File
@@ -0,0 +1,48 @@
const TOKEN_KEY = 'pa-admin-token';
let token = localStorage.getItem(TOKEN_KEY) || '';
export function getToken() { return token; }
export function setToken(t) {
token = t || '';
if (t) localStorage.setItem(TOKEN_KEY, t);
else localStorage.removeItem(TOKEN_KEY);
}
async function request(path, { method = 'GET', body } = {}) {
const res = await fetch('/api/admin' + path, {
method,
headers: {
...(body ? { 'Content-Type': 'application/json' } : {}),
...(token ? { Authorization: 'Bearer ' + token } : {}),
},
body: body ? JSON.stringify(body) : undefined,
});
let data = null;
try { data = await res.json(); } catch { data = null; }
if (!res.ok) {
const err = new Error((data && data.error && data.error.message) || ('请求失败 HTTP ' + res.status));
err.status = res.status;
err.code = data && data.error && data.error.code;
throw err;
}
return data;
}
export const api = {
login: (username, password) => request('/login', { method: 'POST', body: { username, password } }),
logout: () => request('/logout', { method: 'POST' }),
me: () => request('/me'),
units: () => request('/units'),
users: () => request('/users'),
createUser: (body) => request('/users', { method: 'POST', body }),
updateUser: (id, body) => request('/users/' + id, { method: 'PATCH', body }),
deleteUser: (id) => request('/users/' + id, { method: 'DELETE' }),
logs: (params = {}) => {
const q = new URLSearchParams();
Object.entries(params).forEach(([k, v]) => { if (v !== undefined && v !== null && v !== '') q.set(k, v); });
const qs = q.toString();
return request('/logs' + (qs ? '?' + qs : ''));
},
settings: () => request('/settings'),
updateSettings: (body) => request('/settings', { method: 'PUT', body }),
};
+57
View File
@@ -0,0 +1,57 @@
<script setup>
import { ref, watch } from 'vue';
import { api } from '../api/admin.js';
const props = defineProps({ open: Boolean, hub: Object });
const emit = defineEmits(['close', 'saved']);
const hubId = ref('');
const hubName = ref('');
const busy = ref(false);
const error = ref('');
watch(() => props.open, (open) => {
if (open) {
hubId.value = (props.hub && props.hub.hub_id) || '';
hubName.value = (props.hub && props.hub.hub_name) || '';
error.value = '';
busy.value = false;
}
});
async function save() {
if (busy.value) return;
if (!hubName.value.trim()) { error.value = '中枢名称不能为空'; return; }
busy.value = true;
error.value = '';
try {
const r = await api.updateSettings({ hub_id: hubId.value.trim(), hub_name: hubName.value.trim() });
emit('saved', r.settings);
} catch (e) {
error.value = e.message || '保存失败';
} finally {
busy.value = false;
}
}
</script>
<template>
<div v-if="open" class="overlay" @click.self="emit('close')">
<div class="modal">
<div class="modal-head">中枢设置</div>
<div class="modal-body">
<div class="field"><label>中枢名称</label><input v-model="hubName" placeholder="例如:湘湖实验室中枢"></div>
<div class="field" style="margin-bottom: 0"><label>中枢 ID</label><input v-model="hubId" class="mono" placeholder="唯一标识,留空则由系统生成"></div>
<p v-if="error" class="err">{{ error }}</p>
</div>
<div class="modal-foot">
<button class="btn btn-ghost" @click="emit('close')">取消</button>
<button class="btn btn-primary" :disabled="busy" @click="save">{{ busy ? '保存中…' : '保存' }}</button>
</div>
</div>
</div>
</template>
<style scoped>
.err { color: var(--red); font-size: 12px; margin: 12px 0 0; }
</style>
+101
View File
@@ -0,0 +1,101 @@
<script setup>
import { ref, watch } from 'vue';
import { api } from '../api/admin.js';
const props = defineProps({
open: Boolean,
mode: { type: String, default: 'create' },
user: Object,
});
const emit = defineEmits(['close', 'saved']);
const UNITS = [
['qa', '问答'], ['kg', '知识图谱'], ['extraction', '文献抽取'], ['docgen', '文档生成'],
['figures', '图片图库'], ['private_docs', '私有资料'], ['bioflow', '生信分析'],
['paper', '论文撰写'], ['legacy', '旧版接口'],
];
const username = ref('');
const password = ref('');
const role = ref('researcher');
const authorizations = ref([]);
const busy = ref(false);
const error = ref('');
watch(() => props.open, (open) => {
if (open) {
error.value = '';
busy.value = false;
if (props.mode === 'edit' && props.user) {
username.value = props.user.username;
password.value = '';
role.value = props.user.role === 'admin' ? 'admin' : (props.user.role === 'guest' ? 'guest' : 'researcher');
authorizations.value = [...(props.user.authorizations || [])];
} else {
username.value = '';
password.value = '';
role.value = 'researcher';
authorizations.value = ['qa'];
}
}
});
async function save() {
if (busy.value) return;
if (props.mode === 'create' && (!username.value.trim() || !password.value)) {
error.value = '用户名和密码必填';
return;
}
busy.value = true;
error.value = '';
try {
if (props.mode === 'create') {
await api.createUser({ username: username.value.trim(), password: password.value, role: role.value, authorizations: authorizations.value });
} else {
const body = { role: role.value, authorizations: authorizations.value };
if (password.value) body.password = password.value;
await api.updateUser(props.user.id, body);
}
emit('saved');
} catch (e) {
error.value = e.message || '保存失败';
} finally {
busy.value = false;
}
}
</script>
<template>
<div v-if="open" class="overlay" @click.self="emit('close')">
<div class="modal">
<div class="modal-head">{{ mode === 'create' ? '新建用户' : '编辑用户' }}</div>
<div class="modal-body">
<div class="field"><label>用户名</label><input v-model="username" :disabled="mode === 'edit'"></div>
<div class="field"><label>{{ mode === 'create' ? '初始密码' : '重置密码(留空不改)' }}</label><input v-model="password" type="password"></div>
<div class="field">
<label>角色</label>
<div class="seg">
<button :class="{ on: role === 'researcher' }" @click="role = 'researcher'">研究员</button>
<button :class="{ on: role === 'guest' }" @click="role = 'guest'">访客</button>
<button :class="{ on: role === 'admin' }" @click="role = 'admin'">管理员</button>
</div>
</div>
<div class="field" style="margin-bottom: 0">
<label>授权清单</label>
<div class="checks">
<label v-for="[k, label] in UNITS" :key="k" class="check"><input type="checkbox" :value="k" v-model="authorizations"> {{ label }}</label>
</div>
</div>
<p v-if="error" class="err">{{ error }}</p>
</div>
<div class="modal-foot">
<button class="btn btn-ghost" @click="emit('close')">取消</button>
<button class="btn btn-primary" :disabled="busy" @click="save">{{ busy ? '保存中…' : (mode === 'create' ? '创建用户' : '保存') }}</button>
</div>
</div>
</div>
</template>
<style scoped>
.err { color: var(--red); font-size: 12px; margin: 12px 0 0; }
</style>
+5
View File
@@ -0,0 +1,5 @@
import { createApp } from 'vue';
import App from './App.vue';
import './style.css';
createApp(App).mount('#app');
+96
View File
@@ -0,0 +1,96 @@
:root {
--bg: #0c0e14;
--bg-2: #10131b;
--surface: #151924;
--surface-2: #1a1f2c;
--border: #232a38;
--border-2: #2e3648;
--text: #e9edf5;
--text-2: #9aa4b6;
--text-3: #5f6a7d;
--accent: #e8a33d;
--accent-ink: #14100a;
--accent-soft: rgba(232, 163, 61, 0.14);
--green: #3ecf8e;
--green-soft: rgba(62, 207, 142, 0.14);
--red: #ef6b6b;
--red-soft: rgba(239, 107, 107, 0.14);
--blue: #6ea8ff;
--radius: 10px;
--radius-sm: 7px;
}
* { box-sizing: border-box; }
html, body, #app { margin: 0; height: 100%; }
body {
background: var(--bg);
color: var(--text);
font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Microsoft YaHei", "Segoe UI", Roboto, sans-serif;
font-size: 14px;
line-height: 1.5;
-webkit-font-smoothing: antialiased;
}
.mono { font-family: ui-monospace, "SF Mono", "JetBrains Mono", Menlo, Consolas, monospace; }
button { font-family: inherit; cursor: pointer; }
.btn {
display: inline-flex; align-items: center; justify-content: center; gap: 7px;
padding: 9px 15px; font-size: 13.5px; border-radius: var(--radius-sm);
border: 1px solid transparent; transition: filter .15s; background: transparent;
}
.btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.btn-primary { background: var(--accent); color: var(--accent-ink); font-weight: 600; }
.btn-primary:hover { filter: brightness(1.06); }
.btn-ghost { border-color: var(--border-2); color: var(--text-2); }
.btn-ghost:hover { color: var(--text); border-color: var(--text-3); }
.btn-danger { background: var(--red-soft); color: var(--red); }
.btn-block { width: 100%; }
.field { margin-bottom: 15px; }
.field label { display: block; font-size: 12px; color: var(--text-2); margin-bottom: 7px; }
.field input, .field select {
width: 100%; padding: 10px 12px; font-size: 14px; color: var(--text);
background: var(--bg-2); border: 1px solid var(--border); border-radius: var(--radius-sm); outline: none;
}
.field input:focus, .field select:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.chip {
display: inline-block; padding: 3px 9px; border-radius: 20px; font-size: 11.5px;
border: 1px solid var(--border-2); color: var(--text-2); margin: 1.5px 3px 1.5px 0;
}
.chip.amber { border-color: transparent; background: var(--accent-soft); color: var(--accent); }
.badge { display: inline-block; padding: 2px 8px; border-radius: 5px; font-size: 11.5px; font-family: ui-monospace, Menlo, monospace; }
.badge.green { background: var(--green-soft); color: var(--green); }
.badge.red { background: var(--red-soft); color: var(--red); }
.badge.amber { background: var(--accent-soft); color: var(--accent); }
.badge.muted { background: var(--surface-2); color: var(--text-3); }
.panel { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }
table { width: 100%; border-collapse: collapse; }
th { text-align: left; font-size: 11.5px; font-weight: 500; color: var(--text-3); padding: 10px 18px; border-bottom: 1px solid var(--border); }
td { padding: 13px 18px; border-bottom: 1px solid var(--border); font-size: 13.5px; }
tr:last-child td { border-bottom: none; }
tbody tr:hover { background: rgba(255,255,255,0.015); }
.cell-id { color: var(--text-2); }
.row-actions { display: flex; gap: 6px; }
.row-actions .act { color: var(--blue); cursor: pointer; font-size: 13px; padding: 3px 6px; border-radius: 5px; }
.row-actions .act:hover { background: rgba(110,168,255,0.1); }
.row-actions .act.danger { color: var(--red); }
.row-actions .act.danger:hover { background: var(--red-soft); }
.role { font-weight: 600; }
.role.admin { color: var(--accent); }
.role.researcher { color: var(--blue); }
.role.guest { color: var(--text-2); }
.overlay { position: fixed; inset: 0; background: rgba(0,0,0,0.55); display: flex; align-items: center; justify-content: center; z-index: 50; }
.modal { width: 460px; max-width: calc(100vw - 32px); background: var(--surface); border: 1px solid var(--border-2); border-radius: 14px; }
.modal-head { padding: 18px 22px 0; font-size: 15px; font-weight: 600; }
.modal-body { padding: 18px 22px; }
.modal-foot { padding: 14px 22px 20px; display: flex; justify-content: flex-end; gap: 10px; border-top: 1px solid var(--border); }
.checks { display: grid; grid-template-columns: 1fr 1fr; gap: 9px 14px; margin-top: 8px; }
.check { display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--text-2); }
.check input { accent-color: var(--accent); width: 15px; height: 15px; }
.seg { display: flex; gap: 6px; }
.seg button { background: transparent; border: 1px solid var(--border-2); color: var(--text-2); border-radius: 7px; padding: 7px 14px; font-size: 13px; }
.seg button.on { background: var(--accent-soft); border-color: transparent; color: var(--accent); }
+53
View File
@@ -0,0 +1,53 @@
<script setup>
import { ref } from 'vue';
import { api, setToken } from '../api/admin.js';
const emit = defineEmits(['login']);
const username = ref('');
const password = ref('');
const error = ref('');
const busy = ref(false);
async function submit() {
if (busy.value) return;
if (!username.value || !password.value) { error.value = '请输入用户名和密码'; return; }
busy.value = true;
error.value = '';
try {
const r = await api.login(username.value, password.value);
setToken(r.token);
emit('login', r.user);
} catch (e) {
error.value = e.message || '登录失败';
} finally {
busy.value = false;
}
}
</script>
<template>
<div class="login">
<div class="card">
<div class="brand">
<div class="hex">蜂</div>
<div><div class="brand-name">蜂群中枢</div><div class="brand-sub">PlantAI 管理后台</div></div>
</div>
<div class="field"><label>用户名</label><input v-model="username" autocomplete="username" @keyup.enter="submit"></div>
<div class="field"><label>密码</label><input v-model="password" type="password" autocomplete="current-password" @keyup.enter="submit"></div>
<button class="btn btn-primary btn-block" :disabled="busy" @click="submit">{{ busy ? '登录中…' : '登录' }}</button>
<p v-if="error" class="err">{{ error }}</p>
<div class="hint">仅限中枢管理员。登录与所有操作均会留痕。</div>
</div>
</div>
</template>
<style scoped>
.login { height: 100vh; display: flex; align-items: center; justify-content: center; background: radial-gradient(1200px 600px at 70% -10%, rgba(232, 163, 61, 0.08), transparent 60%), var(--bg); }
.card { width: 360px; background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 34px 32px 30px; }
.brand { display: flex; align-items: center; gap: 11px; margin-bottom: 26px; }
.hex { width: 34px; height: 34px; flex: none; background: linear-gradient(135deg, #f2b95a, #d98f22); clip-path: polygon(50% 0, 100% 25%, 100% 75%, 50% 100%, 0 75%, 0 25%); display: grid; place-items: center; color: #1a1206; font-weight: 700; font-size: 14px; }
.brand-name { font-size: 16px; font-weight: 600; }
.brand-sub { color: var(--text-3); font-size: 12px; }
.err { color: var(--red); font-size: 12px; margin: 10px 0 0; }
.hint { margin-top: 20px; padding-top: 16px; border-top: 1px solid var(--border); font-size: 12px; color: var(--text-3); }
</style>
+102
View File
@@ -0,0 +1,102 @@
<script setup>
import { ref, onMounted } from 'vue';
import { api } from '../api/admin.js';
const emit = defineEmits(['unauthorized']);
const from = ref('');
const event = ref('');
const q = ref('');
const page = ref(1);
const pageSize = 30;
const items = ref([]);
const total = ref(0);
const aggregates = ref({});
const loading = ref(false);
const EVENT_LABEL = {
http_request: '调用', admin_login_ok: '登录成功', admin_login_fail: '登录失败',
call_denied: '越权', user_created: '建用户', user_updated: '改用户', user_deleted: '删用户',
};
function statusBadge(s) {
if (!s) return 'muted';
if (s >= 200 && s < 300) return 'green';
if (s >= 500) return 'red';
return 'amber';
}
async function load() {
loading.value = true;
try {
const r = await api.logs({
from_date: from.value || undefined,
event: event.value || undefined,
q: q.value || undefined,
page: page.value,
page_size: pageSize,
});
items.value = r.items;
total.value = r.total;
aggregates.value = r.aggregates || {};
} catch (e) {
if (e.status === 401) emit('unauthorized');
} finally {
loading.value = false;
}
}
function search() { page.value = 1; load(); }
function prev() { if (page.value > 1) { page.value -= 1; load(); } }
function next() { if (page.value * pageSize < total.value) { page.value += 1; load(); } }
onMounted(load);
</script>
<template>
<div>
<div class="stats" style="margin-bottom: 16px">
<div class="stat"><div class="k">{{ total }}</div><div class="l">匹配日志</div></div>
<div class="stat"><div class="k amber">{{ aggregates.denied ?? 0 }}</div><div class="l">越权 / 失败</div></div>
<div class="stat"><div class="k">{{ aggregates.avg_latency_ms ?? '—' }}</div><div class="l">平均耗时 ms</div></div>
<div class="stat"><div class="k">{{ aggregates.p95_latency_ms ?? '—' }}</div><div class="l">P95 耗时 ms</div></div>
</div>
<div class="toolbar">
<input class="date" v-model="from" type="date">
<select v-model="event">
<option value="">全部事件</option>
<option v-for="(l, k) in EVENT_LABEL" :key="k" :value="k">{{ l }}</option>
</select>
<div class="search"><input v-model="q" placeholder="按用户 / 路径过滤…" @keyup.enter="search"></div>
<button class="btn btn-ghost" @click="search">查询</button>
</div>
<div class="panel">
<table>
<thead><tr><th>时间</th><th>用户</th><th>事件</th><th>方法</th><th>路径</th><th>状态</th><th>耗时</th></tr></thead>
<tbody>
<tr v-if="loading"><td colspan="7" class="cell-id">加载中…</td></tr>
<tr v-else-if="!items.length"><td colspan="7" class="cell-id">暂无日志</td></tr>
<tr v-for="(e, i) in items" :key="i">
<td class="cell-id mono">{{ (e.ts || '').replace('T', ' ').slice(0, 19) }}</td>
<td class="mono cell-id">{{ e.user_id ? e.user_id.slice(0, 8) : '—' }}</td>
<td>{{ EVENT_LABEL[e.event] || e.event }}</td>
<td class="cell-id mono">{{ e.method || '—' }}</td>
<td class="mono">{{ e.path || '—' }}</td>
<td><span v-if="e.status" class="badge" :class="statusBadge(e.status)">{{ e.status }}</span><span v-else class="badge muted">—</span></td>
<td class="cell-id mono">{{ e.latency_ms != null ? e.latency_ms + 'ms' : '—' }}</td>
</tr>
</tbody>
</table>
<div class="pag">
<span>共 {{ total }} 条</span><span class="spacer"></span>
<button :disabled="page <= 1" @click="prev">上一页</button>
<button class="on">{{ page }}</button>
<button :disabled="page * pageSize >= total" @click="next">下一页</button>
</div>
</div>
</div>
</template>
<style scoped>
input.date { background: var(--surface); border: 1px solid var(--border); color: var(--text); border-radius: var(--radius-sm); padding: 8px 10px; font-size: 13px; outline: none; }
</style>
+72
View File
@@ -0,0 +1,72 @@
<script setup>
import { ref, onMounted } from 'vue';
import { api } from '../api/admin.js';
import UserForm from '../components/UserForm.vue';
const emit = defineEmits(['unauthorized']);
const users = ref([]);
const loading = ref(true);
const formOpen = ref(false);
const formMode = ref('create');
const editing = ref(null);
const ROLE_LABEL = { admin: '管理员', researcher: '研究员', guest: '访客' };
const UNIT_LABEL = { qa: '问答', kg: '知识图谱', extraction: '文献抽取', docgen: '文档生成', figures: '图片图库', private_docs: '私有资料', bioflow: '生信分析', paper: '论文撰写', legacy: '旧版接口' };
async function load() {
loading.value = true;
try {
const r = await api.users();
users.value = r.users;
} catch (e) {
if (e.status === 401) emit('unauthorized');
} finally {
loading.value = false;
}
}
function create() { formMode.value = 'create'; editing.value = null; formOpen.value = true; }
function edit(u) { formMode.value = 'edit'; editing.value = u; formOpen.value = true; }
async function del(u) {
if (!confirm(`删除用户「${u.username}」?此操作不可撤销。`)) return;
try { await api.deleteUser(u.id); load(); } catch (e) { alert(e.message || '删除失败'); }
}
function onSaved() { formOpen.value = false; load(); }
onMounted(load);
</script>
<template>
<div>
<div class="toolbar">
<div class="spacer"></div>
<button class="btn btn-primary" @click="create">+ 新建用户</button>
</div>
<div class="panel">
<table>
<thead><tr><th>用户</th><th>角色</th><th>授权范围</th><th>创建时间</th><th style="width: 120px"></th></tr></thead>
<tbody>
<tr v-if="loading"><td colspan="5" class="cell-id">加载中…</td></tr>
<tr v-else-if="!users.length"><td colspan="5" class="cell-id">暂无用户</td></tr>
<tr v-for="u in users" :key="u.id">
<td>
<div style="display: flex; align-items: center; gap: 10px">
<div class="avatar">{{ u.username[0] }}</div>
<div><div>{{ u.username }}</div><div class="cell-id" style="font-size: 12px">{{ u.id.slice(0, 8) }}</div></div>
</div>
</td>
<td><span class="role" :class="u.role">{{ ROLE_LABEL[u.role] || u.role }}</span></td>
<td>
<span v-if="u.role === 'admin'" class="chip amber">全部权限</span>
<template v-else><span v-for="a in u.authorizations" :key="a" class="chip">{{ UNIT_LABEL[a] || a }}</span></template>
</td>
<td class="cell-id mono">{{ (u.created_at || '').slice(0, 10) }}</td>
<td><div class="row-actions"><span class="act" @click="edit(u)">编辑</span><span class="act danger" @click="del(u)">删除</span></div></td>
</tr>
</tbody>
</table>
</div>
<UserForm :open="formOpen" :mode="formMode" :user="editing" @close="formOpen = false" @saved="onSaved" />
</div>
</template>
+15
View File
@@ -0,0 +1,15 @@
import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
export default defineConfig({
base: '/admin/',
plugins: [vue()],
server: {
host: '127.0.0.1',
port: 5181,
proxy: {
'/api': { target: 'http://127.0.0.1:8090', changeOrigin: false },
},
},
build: { outDir: 'dist', target: 'es2020' },
});
@@ -0,0 +1,111 @@
# PR 报告:P3b 编译 PDF Tab(论文重排版面板)
- 日期:2026-10-13
- 分支:`feature/p3b-compile-ui`(自最新 main 新开;未 commit,按用户约定统一处理)
- 范围:`ui/` 面板 + store/api 扩展;`review_service/` 仅 restart.sh 调起预热脚本 + 新增
scripts/prewarm_tectonic.sh。**后端 compile.py / latex_compile.py(P3a)零改动**;
无 sdk/、agent/、vstore/ 改动;无新增依赖。
## 概要
P3b:论文重排版面板启用"编译 PDF" Tab,实现"上传 .tex → 服务器编译 → 下载 PDF"的完整闭环。
## 修改内容
| 文件 | 变更 |
|---|---|
| ui/src/components/edu/panels/ReformatPanel.vue | 编译 Tab 状态机:无 job_id 引导 / 编译按钮 / loading 动画 + 首次 1-3 分钟黄条(可关闭)/ 成功 blob URL iframe 预览 + 下载 + 耗时 + 重新编译 / 失败错误摘要 + log 尾 200 行折叠 + 完整 log 下载;按钮不 await,状态全由 store 驱动(切 Tab/切面板编译继续) |
| ui/src/stores/edu.js | compileStatus/compileResult/compileError/compileLogTail + compilePdfUrl(blob URL,fetch 失败静默回退 pdf_url 直链)+ compileFirstHint;submitCompile(loading 重入保护)/ clearCompile(revoke blob);重新排版自动 clearCompile |
| ui/src/api/review.js | CODE_HINTS 增 QUEUE_FULL 固定文案"编译队列已满(最多 5 个任务),请稍后重试"(后端 message 带动态 pending 数,规范统一口径);compileTex() 为 P3a 已有封装 |
| review_service/restart.sh | 启动后后台调起 prewarm_tectonic.sh(nohup 非阻塞,日志 prewarm.log) |
| review_service/scripts/prewarm_tectonic.sh(新增) | 最小 hello.tex 实编译以缓存 TeX 环境。**注**:tectonic v0.17.0 无 `download` 子命令(实测 `no subcommand download`),改用 tools/README 的最小 .tex 预编译法;失败仅告警不影响服务启动 |
| tests/frontend/unit/edu/reformat.compile.test.js(新增) | 6 个用例(真实 reviewErrorMessage 经 importOriginal 保留,QUEUE_FULL 走 CODE_HINTS) |
## 关键实现决策
1. **无异步轮询**:后端编译同步阻塞返回(单槽队列,常规 10-30s,上限 `compile_queue_limit=5`),
无 stage 可显示 → loading 只做不确定时长旋转动画 + 文案说明。
2. **QUEUE_FULL**:HTTP 429 + 同款信封,http.js 抛 ApiError(code=QUEUE_FULL);
前端 CODE_HINTS 固定文案,报错后"重新编译"按钮立即可用(store finally 解锁)。
3. **PDF blob URL**:成功后异步 `fetch(pdf_url) → blob → createObjectURL`,
预览/下载不暴露后端路径;jsdom/网络失败回退 pdf_url 直链,不阻塞渲染。
4. **失败 log**:信封 detail = log 尾 200 行(折叠 `<pre>`);完整 log 下载兜底拼
`/api/review/compile/{job_id}/log`(失败信封不带 log_url,但 job_id 即 anchor_id)。
5. **首次编译提示条**:`compileFirstHint` 在首次 submitCompile 置位,loading 期间显示,
可手动关闭;`clearCompile`(重新排版)复位。
6. **预热命令修正**:任务原文 `./tools/tectonic -X download default` 在钉死版本
v0.17.0 不存在——实测后改为最小 .tex 实编译预热(同 tools/README.md 建议)。
## 测试结果
- 后端 pytest(review_service,后端未动):**113 passed**
- 前端 vitest 全量:**403 passed / 5 failed**(5F = 既有 labels.js 基线失败
benchpanel×4 + Composer pill×1,与本 PR 无关)
- vitest 新增 reformat.compile.test.js:**6/6 passed**
(无 job_id 引导 / 按钮可点 / 编译中+黄条+重入保护 / 成功 iframe+下载+耗时 /
失败 log 折叠 / QUEUE_FULL 文案+重试可用)
- npm run build:✓ 3.12s
## 手工冒烟(8100 真实链路)
- reformat /tmp/opencode/reformat_sample.tex → ok=True job_id=b3631727af68
- POST /compile {job_id=b3631727af68} → ok=True seconds=0.61 pdf_bytes=3596
- GET /compile/{cid}/pdf → 200,3596 bytes,application/pdf(file: PDF 1.5)
- GET /compile/{cid}/log → 200,3533 bytes
- job_id 不存在 / 格式非法 → VALIDATION 信封
- 坏 tex(\thisCommandDoesNotExist)→ COMPILE_FAILED + detail 尾 200 行
(含 "! Undefined control sequence")
- QUEUE_FULL → 429 + 信封(后端路径实测于 P3a;前端文案 vitest 6/6 覆盖)
- scripts/prewarm_tectonic.sh → exit=0 OK,宏包缓存就绪
## EPS 兼容性修复 v2(2026-10-14 追加)
背景:真实 MDPI 官方 zip(v6.5a)上传编译在 XDV→PDF 阶段失败:
`xdvipdfmx: image inclusion failed for "Definitions/logo-mdpi.eps"`。
v1 回退逻辑 gate 在"zip 自带 `*-eps-converted-to.pdf` 预转换版"——真实官方 zip
**不带**该文件(v1 本地 e2e 通过是因自造样本带了;测试样本与真实样本不一致,教训已记录)。
v2 方案:**不依赖扫描引用与 zip 自带预转换版**,解压后扫描项目内所有 `.eps`
批量现场转同名 `.pdf`(`latex_compile.process_eps_images`):
1. 工具优先级 **gs → epstopdf → ps2pdf**(`shutil.which` 逐个 fallback);gs 命令
约束1 强制 `-dSAFER -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dPDFSETTINGS=/prepress`
(-dSAFER 防 EPS 内嵌任意 PostScript 执行);无新 Python 依赖(subprocess)
2. 约束2:单文件转换 **15s 超时** → warning 行 `[eps-convert] xxx.eps 转换超时`
并跳过该文件继续
3. 约束3:单文件转换后 >20MB → warning;累计转换产物 >100MB →
`InspectError("ZIP_TOO_LARGE")` 中止(信封原样透出该 code)
4. 约束4:`[eps-convert] {src} → {dst} ({in} → {out}, {elapsed}s, {tool})`
写入 `output.log`(正常/超时两条路径都落盘,引擎 log 之后、chatter 之前)
5. 转换成功 → 显式 `X.eps` 引用字节替换为 `X.pdf`(.tex/.cls/.sty/.cfg/.def 全扫,
不碰编码、不改正文);无扩展名引用 `{.../X}` 由 graphicx 命中新 `X.pdf`
(解析顺序 .pdf→…→.eps,规避 .eps)
6. 全工具失败 → log 行 `[eps-convert] 全部工具失败,图片 xxx.eps 未转换` →
编译自然失败 → 前端提示「项目包含 EPS 图片,服务器转换失败,请转成 PDF/PNG 后重试」
真实官方 zip 实测(`scripts/debug_compile.py` 重打包 fba37ae3c853 现场 + 8100 HTTP 链路):
| 指标 | 结果 |
|---|---|
| 转换 EPS 数 | 2(logo-mdpi.eps、logo-updates.eps,均 gs) |
| 每个耗时 | 563678→13057B 0.27s;287998→16588B 0.27s(合计 0.54s) |
| 显式引用改写 | 1 个文件(Definitions/mdpi.cls) |
| 编译 | 2.83s(debug 脚本)/ 2.99s(HTTP) |
| 产出 PDF | 70688 / 70687 bytes,7 页,`%PDF-` ✓ |
测试:pytest **175 passed**(原 169 全保留,移除 3 个 v1 语义已变的 EPS 用例,
新增 9 个 v2 用例:preset 免转换 / 真实 gs 转换 / 工具 fallback / 全失败 /
超时 warning / 20MB warning / 100MB 中止 / 端到端成功 / 友好报错)。
教训(关键):**外部下载的模板类场景,e2e 必须用真实官方文件**——v1 的自造样本
带了预转换版,真实官方 zip 不带,导致修复上线后失效。
## 部署验证(部署后执行)
```bash
bash review_service/restart.sh
# 观察日志:prewarm_tectonic.sh 后台跑(非阻塞),prewarm.log 出现 "[prewarm] OK"
# 浏览器 → 教育科研 → 论文自动重排版 → 上传 .tex → 选期刊 → 重排版
# → 切"编译 PDF" → 点编译
# 期望:首次 1-3 分钟(若预热已完成则秒级)、后续秒回;失败可见 log 尾 200 行
```
@@ -0,0 +1,131 @@
# P5 科研汇报助手 — PR 报告(2026-10-14,v4 更新 2026-10-15)
## 概要
新增「科研汇报助手」(P5):论文 PDF/粘贴文本 → vstore 解析 → map-reduce LLM 大纲
(5 场景 × 4 时长 × **14 版式**,逐条证据可溯)→ 逐页编辑 → python-pptx 本地生成
16:9 PPTX 初稿(论文真图卡片化嵌入、分隔页主副标题、讲者备注携带证据引用)。
全程只新增 `review_service/` 内代码 + BFF 既有 `REVIEW_ENABLED` 条件反代,
禁区(RAG 核心、既有服务、前端 router/db)零改动。
**v4 迭代(2026-10-15)**:① 副标题风格数改为动态读取 `GET /ppt/styles`(缺数据回落
「多风格·图文混排」)② 章节分隔页加副标题(prompt 强制 `subtitle`、渲染 40pt 主标题 +
20pt muted 副标题 + 底部 accent 装饰线、编辑器可改)③ 论文白底原图与页面底色融合
(surface_2 圆角卡片 + 图注 + 宽/高上限 + 单图页居中留白)。
## 修改内容
### 后端(`review_service/`)
| 类别 | 文件 | 说明 |
| --- | --- | --- |
| 改 | `app/config.py` | +`ppt_jobs_dir` / `ppt_styles_dir` 及 abs 属性(+14 行) |
| 改 | `app/main.py` | 导入 ppt 路由、lifespan `ppt_ensure_table()`、挂载(+8 行) |
| 改 | `tooling/requirements.lock` | **+3 行**:`pillow==12.3.0` / `python-pptx==1.0.2` / `xlsxwriter==3.2.9`(三者均已实装于统一 venv) |
| 改 | `.gitignore` | +9 行:`ppt_jobs/` 运行数据、`prewarm.log`、`data/compile_jobs/`、`scripts/`、`data/review.db` |
| 新 | `app/routers/ppt.py`(317 行) | **9 端点**:`GET /styles`、`POST /jobs`、`POST /jobs/pdf`、`GET/PATCH /jobs/{id}`、`POST /jobs/{id}/build`、`GET /jobs/{id}/pptx`、`GET /jobs/{id}/preview/{page}`、`DELETE /jobs/{id}` |
| 新 | `app/services/ppt_outline.py`(917 行) | map-reduce 大纲核心(索引/配额/重验/裁剪/膨胀回修/分隔页 subtitle 规则) |
| 新 | `app/services/ppt_build.py`(1176 行) | python-pptx 生成器(14 版式 token、字体栈、真图卡片容器、分隔页主副标题、讲者备注) |
| 新 | `app/services/ppt_figure_index.py`(264 行) | 论文真图索引(para/图号 → 图片落盘与状态) |
| 新 | `app/services/ppt_preview.py`(230 行) | 逐页 PNG 预览(供面板缩略图,失败不阻断) |
| 新 | `app/services/ppt_store.py`(155 行) | `ppt_jobs` SQLite 持久化(约定 DDL 逐字实现) |
| 新 | `data/ppt_styles/*.json`(14 个) | 版式 token 源码资产(随 git 走,`GET /styles` 的唯一数据源) |
| 新 | `tests/test_ppt_*.py`(6 个 / 102 例) | outline / build / figure_index / preview / router / store |
### 前端(`ui/` + `tests/`)
| 类别 | 文件 | 说明 |
| --- | --- | --- |
| 改 | `ui/src/api/review.js`(+50 行) | PPT API 段 + `pptStyles()` |
| 改 | `ui/src/stores/edu.js`(+147 行) | `ppt` 状态块 + actions + `pptLoadStyles` + `SECTIONS` 增列 |
| 改 | `ui/src/components/edu/EduSideNav.vue` / `EduWorkspace.vue` | 菜单项 + PptPanel 注册(各 +2 行) |
| 新 | `ui/src/components/edu/panels/PptPanel.vue`(578 行) | 生成表单 + 编辑器(动态风格数、分隔页副标题输入、逐页预览) |
| 新 | `ui/src/components/edu/panels/ppt-styles.css`(40 行) | 面板样式 |
| 新 | `ui/src/assets/ppt-style-cards/*.png`(14 个,约 21KB/个) | 版式卡面截图 |
| 新 | `tests/frontend/unit/edu/ppt.panel.test.js`(12 例)/ `ppt.store.test.js`(7 例) | P5 前端单测 |
| 改 | `tests/frontend/unit/b2/components.test.js`、`b3/benchpanel.test.js`、`bioflow/benchpanel.bioflow.test.js`、`tests/e2e/bench.spec.js` | **预存失败清零**:① tab 文案「提取任务」→「上传文献」(对齐 `51532a3`)② 模式 pill 断言改为「已随 `43a7c6c` 移除」 |
**依赖偏差声明**:`python-pptx==1.0.2`(PyPI 最高 1.0.2;需求中 1.1.2 实为
python-docx 版本,系记混,已在实现时更正)。`xlsxwriter==3.2.9` 当前无代码直接引用,
属统一 venv `pip freeze` 的真实记录,一并同步锁文件——**合并前仍需人工确认这 3 行锁变更**。
**DDL**(与约定逐字一致):
```sql
ppt_jobs(id, status /*outline_ready|building|done|error*/, source_kind, filename,
raw_text, scenario, minutes, style_id, pages_json, pptx_path,
warnings_json, created_at, updated_at)
```
**API 前缀** `/api/review/ppt`(9 端点;业务错误一律 200 信封
`{ok:false, error:{code,message,degraded}}`;二进制下载缺失时回同款信封 JSON)。
### 关键渲染约定(v4)
- **分隔页**:`section_bg` 底(缺则 `surface_2`→`cover_bg`→`bg`),40pt 居中粗体主标题 +
20pt muted 副标题(`bullets[0]` → `subtitle` → 该章第一页 title 三级兜底,浅底自动换深字)+
底部 30% 宽 accent 短线。
- **真图卡片**:`surface_2` 圆角矩形(8px)容器 + 5mm 内边距,图居中、图注
`Fig N(论文 S?P?)` 10–12pt muted、与图区留 3mm;混排页图区 ≤55% 页宽且 ≤60% 页高,
单独图页居中、上下留白 ≥5%。
## 测试结果(2026-10-15)
```
pytest(review_service 全库): 215 passed in 13.7s (P5 新增 102 → 113 + 102)
vitest edu(tests/frontend/unit/edu): 43 passed (P5 新增 19:panel 12 + store 7)
vitest 全量(tests/frontend/unit): 32 files / 421 passed / 0 failed
—— 修掉 b2/b3/bioflow 共 5 个预存失败(文案改名 + 模式按钮移除)
npm run build: ✓ built(chunk > 500kB 警告为预存)
版式目检: 3 风格 × 10 页预览 PNG 渲染核对(分隔页主副标题/对比度回退、
混排图卡 ≤55%、单独图页居中留白)均符合预期
```
## 真实 e2e(v1 阶段记录:官方 MDPI 模板实例 PDF,16813 字符,真实 DeepSeek)
```
PDF 模式创建: 21.77s → 12 页(目标 [11,14])warnings=0 llm_calls=7 unverified_evidence=0
PATCH 编辑: 改写要点 + 新增无证据行 → evidence 同长重验、结构清洗 ✓
build: 0.13s → 12 slides / 64157 B / 16:9(13.33×7.50in)逐页讲者备注 ✓
python-pptx 回读: 12 页,S3 首条 = 人工改写文本 ✓
编辑后下载: ok=false NOT_FOUND(盘上旧文件存在但拒发,pptx_path 清空生效)✓
text 模式: 15.0s → 9 页(目标 [8,10])group_meeting / 10min / botanic-forest ✓
短文本: 200 信封 VALIDATION「正文过短(<300 字),无法生成汇报大纲」✓
清理: 双 DELETE → ppt_jobs 0 行、产物目录空 ✓
```
## 部署验证步骤
1. 依赖:同步 `tooling/requirements.lock`(+3 行)并安装到统一 venv。
2. 重启服务:`cd review_service && bash restart.sh`(8100)。
3. 健康检查:`curl http://127.0.0.1:8100/health` → `status=ok`、`vstore connected=true`。
4. 路由:`curl .../openapi.json` 确认 9 个 `/api/review/ppt/*` 路径在列(含 `/styles`)。
5. BFF:`sdk/.env` 确认 `REVIEW_ENABLED=1`,走 8090 反代冒烟
`POST /api/review/ppt/jobs`(短文本应得 200 VALIDATION 信封,不打 LLM)。
6. 真实链路:上传一篇论文 PDF → 出大纲 → 编辑分隔页副标题并保存 → build → 下载 PPTX →
用 python-pptx 核对页数、16:9 尺寸与分隔页副标题文本。
7. 回归:`cd review_service && <venv>/bin/python -m pytest -q`;
`cd ui && npx vitest run` + `npm run build`。
## 部署决策
**建议:只合并,下次批量部署。** 本次改动对存量服务零影响(纯新增模块 +
开关化反代),无数据迁移、无 schema 破坏性变更;单点重启 8100 即可生效,
不值得为它单独发版,随下一次批量部署一并上即可。
## 风险与回滚
| 风险 | 影响 | 缓解 / 回滚 |
| --- | --- | --- |
| `python-pptx==1.0.2` 版本与需求(1.1.2)不一致 | 依赖锁未按原文落库 | 已声明偏差,合并前人工确认 3 行锁变更;如需回退直接还原 lock |
| `xlsxwriter==3.2.9` 无代码引用 | 锁文件带入未用依赖 | venv 实况记录,可保留;若要求最小化可单独剔除该行 |
| LLM 大纲偶发超时/无效 JSON | 创建接口报 `LLM_ERROR`/`LLM_INVALID` | 已分片 reduce + 单次膨胀回修 + 警告降级;前端可重试,无脏数据落库 |
| quote 反查失败残留 evidence(2 条) | 个别要点 `unverified` | **Phase 2**:最长公共子串(≥20 字符)匹配补 `para_id`,失败保持 unverified(边际价值低,v4 不做) |
| 编辑后旧 PPTX 残留盘上 | 无(`pptx_path` 清空后拒发,仅占磁盘) | 删除 job 时 `rmtree` 一并清理 |
| 回滚 | — | 反向还原 12 个已跟踪文件改动 + 删除 44 个新增文件(后端 26 + 前端 18)即可;`ppt_jobs` 表与产物目录可留可删,不被其他模块引用 |
## 与 `feature/p3b-compile-ui#4f9c077` 的关系
v1 版报告曾以同一路径提交于该分支(`docs: add P5 PR report`),内容停留在 3 版式 /
7 端点 / 208 pytest 阶段。本文件为 v4 权威版本;若两分支先后合入,同路径会产生一次
文本冲突,取本文件(v4)内容即可。
+29 -1
View File
@@ -62,6 +62,23 @@ class Settings(BaseSettings):
compile_queue_limit: int = 5 # 等待槽位上限(>5 → QUEUE_FULL 429)
compile_retention_days: int = 7 # 编译产物保留天数(后台清扫)
# ---- LaTeX 项目 zip 编译(P4:两段式 inspect + run)----
max_zip_bytes: int = 50 * 1024 * 1024 # 上传 zip 上限 50MB
max_extract_bytes: int = 200 * 1024 * 1024 # 单项目解压总大小硬顶 200MB(实写字节计数)
max_zip_entries: int = 2000 # 条目数上限(预扫第一件事)
max_zip_entry_bytes: int = 50 * 1024 * 1024 # 单条目解压上限 50MB
max_zip_ratio: float = 100.0 # 单条目压缩比 file_size/compress_size 上限
max_zip_path_depth: int = 16 # 路径深度上限(深层嵌套拒绝)
max_zip_path_len: int = 200 # 单条目路径长度上限
compile_project_timeout_s: float = 120.0 # 项目 zip 编译墙钟上限(单文件仍 compile_timeout_s)
inspect_dir: str = "data/compile_inspect" # zip 解压暂存(1 小时 TTL,相对 review_service/)
inspect_ttl_s: int = 3600 # inspect 暂存过期秒数
inspect_sweep_interval_s: float = 300.0 # inspect 后台清扫周期(lifespan 定时任务)
compile_jobs_dir: str = "data/compile_jobs" # 项目编译产物(pdf/log/meta,7 天 sweep)
failed_project_ttl_s: int = 24 * 3600 # 编译失败保留 project/ 现场秒数(诊断用)
keep_failed_projects: bool = False # 失败 project/ 永久保留(调试开关;
# 亦读裸环境变量 KEEP_FAILED_PROJECTS=1)
# ---- 科研汇报 PPT(P5:map-reduce 大纲 + python-pptx 初稿)----
ppt_jobs_dir: str = "data/ppt_jobs" # 产物(draft.pptx + meta,相对 review_service/)
ppt_styles_dir: str = "data/ppt_styles" # 风格 tokens JSON(源码资产,随 git 走)
@@ -105,10 +122,21 @@ class Settings(BaseSettings):
return p if p.is_absolute() else self._service_root / p
@property
def tectonic_cache_abs(self) -> "Path":
def tectonic_cache_abs(self) -> Path:
p = Path(self.tectonic_cache_dir)
return p if p.is_absolute() else self._service_root / p
@property
def inspect_dir_abs(self) -> Path:
p = Path(self.inspect_dir)
return p if p.is_absolute() else self._service_root / p
@property
def compile_jobs_dir_abs(self) -> Path:
p = Path(self.compile_jobs_dir)
return p if p.is_absolute() else self._service_root / p
@property
def vstore_base(self) -> str:
return self.vstore_base_url.rstrip("/")
+35
View File
@@ -68,6 +68,33 @@ def create_app() -> FastAPI:
sweep_expired()
except Exception: # noqa: BLE001
logger.warning("compile sweep 启动清扫失败(忽略)", exc_info=True)
# P4:inspect 暂存(1h TTL)+ 项目编译产物(7 天)启动清扫
try:
from .services.latex_compile import (
sweep_compile_jobs_expired,
sweep_inspect_expired,
)
sweep_inspect_expired()
sweep_compile_jobs_expired()
except Exception: # noqa: BLE001
logger.warning("P4 sweep 启动清扫失败(忽略)", exc_info=True)
# P4:inspect 暂存后台定时清扫(默认 300s;补充 1——上传不点编译不堆积)
sweep_task: asyncio.Task[None] | None = None
try:
from .services.latex_compile import sweep_inspect_expired as _sweep_insp
async def _inspect_sweep_loop() -> None:
interval = get_settings().inspect_sweep_interval_s
while True:
await asyncio.sleep(interval)
try:
await asyncio.to_thread(_sweep_insp)
except Exception: # noqa: BLE001
logger.warning("inspect 后台清扫失败(忽略)", exc_info=True)
sweep_task = asyncio.create_task(_inspect_sweep_loop())
except Exception: # noqa: BLE001
logger.warning("inspect 后台清扫任务启动失败(忽略)", exc_info=True)
# journal_directory 自动 seed:空表才导(xlsx 解析约 1-3s,不堵 event loop;
# 路径沿用 db.path 注入链——生产=settings.db_path_abs,测试=tmp)
if settings.seed_on_startup:
@@ -95,6 +122,14 @@ def create_app() -> FastAPI:
try:
yield
finally:
if sweep_task is not None:
sweep_task.cancel()
try:
await sweep_task
except asyncio.CancelledError:
pass
except Exception: # noqa: BLE001
pass
await close_db()
app.router.lifespan_context = _lifespan
+158
View File
@@ -132,6 +132,107 @@ async def compile_tex(
)
# ================================================================ P4:项目 zip(两段式)
def _p4_error(code: str, message: str, detail: str = "",
compile_id: str | None = None,
log_url: str | None = None) -> CompileResponse:
"""P4 错误信封(补充 3:失败也带 compile_id + log_url,前端可下载完整 log)。"""
err: dict[str, Any] = {"code": code, "message": message, "degraded": False}
if detail:
err["detail"] = detail
return CompileResponse(
ok=False, compile_id=compile_id, log_url=log_url, error=err)
def _project_pdf_url(cid: str) -> str:
return f"/api/review/compile/zip/{cid}/pdf"
def _project_log_url(cid: str) -> str:
return f"/api/review/compile/zip/{cid}/log"
@router.post("/compile/inspect", response_model=CompileResponse)
async def inspect_zip_project(
file: UploadFile = File(..., description="LaTeX 项目 zip(含 main.tex/.cls/图片/.bib)"),
) -> CompileResponse:
"""两段式之①:安全解压 + 主文件识别 → job_id(1 小时 TTL,run 前不占编译队列)。
暂用 request.body() 整体缓冲(≤50MB);Phase 2 改流式上传
"""
settings = get_settings()
filename = file.filename or "project.zip"
if Path(filename).name != filename or ".." in filename:
return _p4_error("VALIDATION", "非法文件名")
if not filename.lower().endswith(".zip"):
return _p4_error("VALIDATION", "仅支持 .zip 文件")
# 暂用 request.body() 整体缓冲(≤50MB);Phase 2 改流式上传
content = await file.read()
if not content:
return _p4_error("VALIDATION", "上传文件为空")
if len(content) > settings.max_zip_bytes:
return _p4_error(
"VALIDATION",
f"zip 超过 {settings.max_zip_bytes // (1024 * 1024)}MB 上限")
try:
meta = await lc.inspect_zip(content)
except lc.InspectError as exc:
return _p4_error(exc.code, exc.message)
return CompileResponse(ok=True, meta=meta)
class RunZipRequest(BaseModel):
job_id: str
@router.post("/compile/run", response_model=CompileResponse)
async def run_zip_compile(payload: RunZipRequest) -> CompileResponse:
"""两段式之②:原子消费 inspect(rename 防并发双跑)→ tectonic 编译 → pdf/log。
队列/工具失败发生在消费**之前** → inspect 保留可重试;
编译失败(COMPILE_FAILED/TIMEOUT)信封仍带 compile_id + log_url(补充 3)。
"""
jid = (payload.job_id or "").strip()
if not _CID_RE.fullmatch(jid):
return _p4_error(
"VALIDATION", "job_id 格式非法(应为 12 位 hex)")
try:
result = await lc.compile_project_job(jid)
except lc.InspectError as exc:
return _p4_error(exc.code, exc.message)
except lc.ToolUnavailableError as exc:
return _p4_error("TOOL_UNAVAILABLE", str(exc))
except lc.QueueFullError as exc:
# 队列满发生在 consume_inspect 之前 → job 未消费,前端可直接重试
return JSONResponse(
status_code=429,
content={
"ok": False,
"compile_id": None, "pdf_url": None, "log_url": None, "meta": None,
"error": {"code": "QUEUE_FULL", "message": str(exc),
"degraded": False},
},
)
except lc.CompileError as exc:
# 补充 3:失败也带 compile_id + log_url(cid 由 compile_project_job 附加)
cid = getattr(exc, "compile_id", None)
return _p4_error(
exc.code, str(exc), detail=exc.log_tail,
compile_id=cid,
log_url=_project_log_url(cid) if cid else None)
return CompileResponse(
ok=True,
compile_id=result["compile_id"],
pdf_url=_project_pdf_url(result["compile_id"]),
log_url=_project_log_url(result["compile_id"]),
meta=json.loads((lc.compile_job_dir(result["compile_id"]) / "meta.json")
.read_text("utf-8"))
if (lc.compile_job_dir(result["compile_id"]) / "meta.json").is_file()
else {"compile_id": result["compile_id"], "seconds": result["seconds"]},
)
def _cid_dir(cid: str) -> Path | None:
if not _CID_RE.fullmatch(cid or ""):
return None
@@ -139,6 +240,63 @@ def _cid_dir(cid: str) -> Path | None:
return d if d.is_dir() else None
def _zip_cid_dir(cid: str) -> Path | None:
"""P4 项目产物目录 data/compile_jobs/{cid}/(独立于 P3a reformat_dir 树)。"""
if not _CID_RE.fullmatch(cid or ""):
return None
d = lc.compile_job_dir(cid)
return d if d.is_dir() else None
@router.get("/compile/zip/{compile_id}", response_model=CompileResponse)
async def get_zip_compile(compile_id: str) -> CompileResponse:
cdir = _zip_cid_dir(compile_id)
meta_path = cdir / "meta.json" if cdir else None
if meta_path is None or not meta_path.is_file():
return _p4_error("NOT_FOUND", "编译任务不存在或已过期,请重新编译")
try:
meta = json.loads(meta_path.read_text("utf-8"))
except (OSError, ValueError):
return _p4_error("NOT_FOUND", "编译产物损坏,请重新编译")
return CompileResponse(
ok=True,
compile_id=compile_id,
pdf_url=_project_pdf_url(compile_id),
log_url=_project_log_url(compile_id),
meta=meta,
)
@router.get("/compile/zip/{compile_id}/pdf", response_model=None)
async def download_zip_pdf(compile_id: str) -> FileResponse | JSONResponse:
cdir = _zip_cid_dir(compile_id)
pdf = cdir / "output.pdf" if cdir else None
if pdf is None or not pdf.is_file():
return JSONResponse(
status_code=200,
content={"ok": False, "error": {
"code": "NOT_FOUND", "message": "PDF 不存在或已过期",
"degraded": False}},
)
return FileResponse(pdf, media_type="application/pdf",
filename=f"{compile_id}.pdf")
@router.get("/compile/zip/{compile_id}/log", response_model=None)
async def download_zip_log(compile_id: str) -> PlainTextResponse | JSONResponse:
"""补充 3:成功/失败都可下载 output.log。"""
cdir = _zip_cid_dir(compile_id)
log = cdir / "output.log" if cdir else None
if log is None or not log.is_file():
return JSONResponse(
status_code=200,
content={"ok": False, "error": {
"code": "NOT_FOUND", "message": "日志不存在或已过期",
"degraded": False}},
)
return PlainTextResponse(log.read_text("utf-8", errors="replace"))
@router.get("/compile/{compile_id}", response_model=CompileResponse)
async def get_compile(compile_id: str) -> CompileResponse:
cdir = _cid_dir(compile_id)
+634 -1
View File
@@ -1,4 +1,4 @@
"""services/latex_compile.py —— tectonic 编译 .tex → PDF(P3a)。
"""services/latex_compile.py —— tectonic 编译 .tex → PDF(P3a + P4 项目 zip)。
隔离与安全(对齐设计清单):
1. 一编译一目录:{reformat_dir}/{anchor_id}/compile/(input.tex/output.pdf/output.log/meta.json)
@@ -9,6 +9,13 @@
6. 产物 PDF > max_compile_pdf_bytes → 删文件 + OutputTooLargeError
7. 启动/每次编译前机会性清扫 retention_days 天前的 job 目录(只删 12 位 hex 锚点目录)
P4 项目 zip(两段式 inspect + run,与 P3a 共享单槽队列/清扫/错误类):
- inspect_zip:安全解压(zip_safety)→ 主文件识别 → compile_inspect/{job_id}/(1h TTL)
- compile_project_job:原子 rename 消费 inspect → compile_jobs/{cid}/ → tectonic →
finally 删 project 树(只留 output.pdf/output.log/meta.json)
- 失败也保证 output.log 落盘(补充 3);classify_compile_error 给出用户友好文案
- P3a 单文件路径(prepare_input/compile_tex/_run_once)零改动
tectonic 二进制缺失 → ToolUnavailableError(路由层回安装指引)。
"""
from __future__ import annotations
@@ -20,7 +27,10 @@ import os
import re
import shutil
import signal
import subprocess
import time
import uuid
from dataclasses import dataclass
from datetime import datetime, timezone
from pathlib import Path
from typing import Any
@@ -40,6 +50,9 @@ _pending = 0
class CompileError(Exception):
"""编译失败(含 log 尾)。code ∈ {COMPILE_FAILED, TIMEOUT, OUTPUT_TOO_LARGE}。"""
# compile_id:P4 项目编译失败时由 compile_project_job 附加(P3a 单文件为 None),
# 路由层据此在失败信封里回 compile_id + log_url(补充 3)。
compile_id: str | None = None
def __init__(self, code: str, message: str, log_tail: str = "") -> None:
super().__init__(message)
@@ -234,3 +247,623 @@ def sweep_expired() -> int:
logger.info("compile sweep: 清理 %d 个过期任务目录(>%d 天)",
removed, settings.compile_retention_days)
return removed
# ================================================================ P4:项目 zip(inspect + run)
#
# 与 P3a 共用:_lock/_pending 单槽队列、_ensure_tool、CompileError/QueueFullError/
# ToolUnavailableError、_kill_group、_log_tail、12-hex 目录与 mtime sweep 约定。
# P3a 函数(prepare_input/compile_tex/_run_once)零改动。
class InspectError(Exception):
"""inspect/消费阶段业务失败。
code ∈ {VALIDATION, ZIP_UNSAFE, ZIP_BOMB, ZIP_INVALID, ZIP_TOO_LARGE}。
"""
def __init__(self, code: str, message: str) -> None:
super().__init__(message)
self.code = code
self.message = message
_DOCCLASS_RE = re.compile(r"(?<!\\)\\documentclass\s*(\[|\{)")
_IMG_LIKE_EXT = {".png", ".jpg", ".jpeg", ".pdf", ".eps", ".svg"}
def inspect_root() -> Path:
"""data/compile_inspect/(1 小时 TTL 暂存根,存在性由调用方按需 mkdir)。"""
return get_settings().inspect_dir_abs
def compile_job_dir(cid: str) -> Path:
"""data/compile_jobs/{cid}/(产物根;不走 reformat_dir,P4 独立清扫)。"""
return get_settings().compile_jobs_dir_abs / cid
def _strip_tex_comments(text: str) -> str:
"""粗剥注释:行内 % 截断(\\% 误伤可接受,MVP 设计决策)。"""
return "\n".join(ln.split("%", 1)[0] for ln in text.splitlines())
def _read_tex(path: Path) -> str:
"""utf-8 → gb18030 → latin-1 降级读(中文模板常见 GBK)。"""
raw = path.read_bytes()
for enc in ("utf-8", "gb18030", "latin-1"):
try:
return raw.decode(enc)
except UnicodeDecodeError:
continue
return raw.decode("utf-8", errors="replace")
def find_main_tex(project_dir: Path) -> tuple[str | None, list[str], int]:
"""主文件识别(补充 2)→ (main_rel | None, 含 documentclass 的相对路径清单, .tex 总数)。
场景:唯一候选 → main;0 个 .tex / 0 候选 / ≥2 候选 → None + 清单(调用方报错)。
返回的相对路径用 posix 分隔符(与 zip 条目名一致)。
"""
tex_files = sorted(
p for p in project_dir.rglob("*")
if p.is_file() and p.suffix.lower() == ".tex"
)
if not tex_files:
return None, [], 0
candidates: list[str] = []
for p in tex_files:
if _DOCCLASS_RE.search(_strip_tex_comments(_read_tex(p))):
candidates.append(p.relative_to(project_dir).as_posix())
if len(candidates) == 1:
return candidates[0], candidates, len(tex_files)
return None, candidates, len(tex_files)
_MISSING_FILE_RE = re.compile(
r"LaTeX Error: File [`']([^`']+)' not found"
r"|I can't find file [`']([^`']+)'"
)
_IMG_INCLUSION_RE = re.compile(r"""image inclusion failed for ["']([^"']+)["']""")
_EPS_CONVERTED_SUFFIX = "-eps-converted-to.pdf"
_REF_TEXT_EXTS = {".tex", ".cls", ".sty", ".cfg", ".def"}
# EPS 现场转换 v2(约束 1-4):xdvipdfmx 无法内嵌 EPS → 解压后批量转同名 .pdf
_EPS_CONVERT_TIMEOUT_S = 15 # 约束2:单文件转换超时(秒)
_EPS_CONVERT_MAX_FILE = 20 * 1024 * 1024 # 约束3:单文件转换后上限(warning)
_EPS_CONVERT_MAX_TOTAL = 100 * 1024 * 1024 # 约束3:转换产物总上限(中止)
def _eps_tool_commands(eps: Path, pdf: Path) -> list[tuple[str, list[str]]]:
"""转换工具优先级:gs(约束1 必须 -dSAFER)→ epstopdf → ps2pdf;只列 PATH 里可用的。"""
tools: list[tuple[str, list[str]]] = []
gs = shutil.which("gs")
if gs:
tools.append(("gs", [
gs, "-dSAFER", "-dNOPAUSE", "-dBATCH", "-sDEVICE=pdfwrite",
"-dPDFSETTINGS=/prepress",
f"-sOutputFile={pdf}", str(eps),
]))
epstopdf = shutil.which("epstopdf")
if epstopdf:
# epstopdf 默认输出到同目录同名 .pdf(即 pdf 路径)
tools.append(("epstopdf", [epstopdf, str(eps)]))
ps2pdf = shutil.which("ps2pdf")
if ps2pdf:
tools.append(("ps2pdf", [ps2pdf, str(eps), str(pdf)]))
return tools
def _convert_eps_file(eps: Path, pdf: Path) -> tuple[str, str]:
"""逐工具尝试把 eps 转成同名 pdf → ("ok", tool) | ("timeout", tool) | ("fail", "")。
约束2:单工具 15s 超时 → 放弃该文件(继续下一个 eps,不再试后续工具)。
成功判定:退出后 pdf 存在且非空(防半成品)。
"""
tools = _eps_tool_commands(eps, pdf)
if not tools:
return "fail", ""
for tool, cmd in tools:
pdf.unlink(missing_ok=True) # 清上一个工具的半成品
try:
subprocess.run(
cmd, timeout=_EPS_CONVERT_TIMEOUT_S,
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
check=False,
)
except subprocess.TimeoutExpired:
pdf.unlink(missing_ok=True)
return "timeout", tool
except OSError:
continue
if pdf.is_file() and pdf.stat().st_size > 0:
return "ok", tool
pdf.unlink(missing_ok=True)
return "fail", ""
def process_eps_images(project: Path) -> list[str]:
"""EPS 兼容 v2:解压后**批量转换项目内所有 .eps**(不依赖 zip 自带预转换版)。
对每个 X.eps(原 .eps 保留):
1. 已有同名 X.pdf → 跳过转换(引用照常切换为 .pdf);
2. zip 自带 X-eps-converted-to.pdf 预转换版 → 复制为 X.pdf(免转换);
3. 否则现场转换:gs(-dSAFER) → epstopdf → ps2pdf 逐个 fallback(约束1),
单文件 15s 超时 → warning 后继续下一个(约束2)。
转换成功 → 显式 `X.eps` 引用字节替换为 `X.pdf`(.tex/.cls/.sty/.cfg/.def 全扫,
纯字节 ASCII/UTF-8 替换不碰编码);无扩展名引用 `{.../X}` 由 graphicx 直接
命中新生成的 X.pdf(解析顺序 .pdf→…→.eps)。
约束3:单文件转换后 >20MB → warning;转换产物累计 >100MB → 抛
InspectError("ZIP_TOO_LARGE") 中止;约束4:返回值为写入 output.log 的
"[eps-convert]" 日志行(由 _run_project 落盘)。
→ 日志行列表(无 .eps 项目 → 空)。
"""
eps_files = sorted(
p for p in project.rglob("*")
if p.is_file() and p.suffix.lower() == ".eps"
)
if not eps_files:
return []
lines: list[str] = []
handled: list[tuple[bytes, bytes]] = []
total_out = 0
def _rel(p: Path) -> str:
return p.relative_to(project).as_posix()
for eps in eps_files:
pdf = eps.with_suffix(".pdf")
src_rel, dst_rel = _rel(eps), _rel(pdf)
if pdf.is_file():
handled.append((eps.name.encode("utf-8"), pdf.name.encode("utf-8")))
continue
converted = eps.with_name(eps.stem + _EPS_CONVERTED_SUFFIX)
if converted.is_file():
t0 = time.monotonic()
shutil.copyfile(converted, pdf)
elapsed = time.monotonic() - t0
tool = "preset"
else:
t0 = time.monotonic()
status, tool = _convert_eps_file(eps, pdf)
elapsed = time.monotonic() - t0
if status == "timeout":
lines.append(f"[eps-convert] {src_rel} 转换超时") # 约束2
continue
if status != "ok":
lines.append(
f"[eps-convert] 全部工具失败,图片 {src_rel} 未转换")
continue
# 约束4:转换日志(成功路径)
lines.append(
f"[eps-convert] {src_rel} → {dst_rel} "
f"({eps.stat().st_size} → {pdf.stat().st_size}, "
f"{elapsed:.2f}s, {tool})")
size_out = pdf.stat().st_size
if size_out > _EPS_CONVERT_MAX_FILE: # 约束3
lines.append(
f"[eps-convert] 警告:{dst_rel} 转换后 {size_out} 字节"
f"超过 {_EPS_CONVERT_MAX_FILE // (1024 * 1024)}MB 上限")
total_out += size_out
if total_out > _EPS_CONVERT_MAX_TOTAL: # 约束3
raise InspectError(
"ZIP_TOO_LARGE",
f"EPS 转换后总大小超过 {_EPS_CONVERT_MAX_TOTAL // (1024 * 1024)}MB 上限"
f"(已达 {total_out // (1024 * 1024)}MB),请压缩图片后重试")
handled.append((eps.name.encode("utf-8"), pdf.name.encode("utf-8")))
# 显式 .eps 引用 → .pdf(仅对转换成功者;失败的保持 .eps → 编译兜底友好报错)
changed = 0
if handled:
for f in project.rglob("*"):
if not (f.is_file() and f.suffix.lower() in _REF_TEXT_EXTS):
continue
data = f.read_bytes()
new = data
for old_b, new_b in handled:
new = new.replace(old_b, new_b)
if new != data:
f.write_bytes(new)
changed += 1
if changed:
lines.append(f"[eps-convert] {changed} 个文件的显式 .eps 引用已改写为 .pdf")
if lines:
logger.info("eps v2: %s", " | ".join(lines))
return lines
def _append_eps_lines(log_path: Path, lines: list[str] | None) -> None:
"""约束4:把 [eps-convert] 转换日志写入 output.log(引擎 log 之后、chatter 之前)。"""
if not lines:
return
with log_path.open("a", encoding="utf-8") as fh:
fh.write("\n".join(lines) + "\n")
def classify_compile_error(log_tail: str) -> str:
"""COMPILE_FAILED 的日志 → 用户友好文案(Q4/§4.4)。"""
m = _MISSING_FILE_RE.search(log_tail)
if m:
name = next(g for g in m.groups() if g)
ext = Path(name).suffix.lower()
if ext == ".cls":
return f"项目缺少 {name},请从期刊官网下载模板后重新上传"
if ext in (".sty", ".def", ".clo"):
return f"缺少 LaTeX 宏包 {name},可能是模板不完整"
if ext in _IMG_LIKE_EXT:
return f"图片 {name} 未在 zip 里,请检查引用路径与大小写"
if ext == ".bib":
return f"缺少文献库 {name},请确认 .bib 已包含在 zip 中"
return f"编译找不到文件 {name},请检查 zip 内文件路径"
m = _IMG_INCLUSION_RE.search(log_tail)
if m:
name = m.group(1)
if Path(name).suffix.lower() == ".eps":
# EPS v2 已现场转换;仍失败 = 服务器转换没成功(工具缺失/超时/格式怪异)
return "项目包含 EPS 图片,服务器转换失败,请转成 PDF/PNG 后重试"
return f"图片 {name} 无法内嵌,请检查图片格式与路径(建议使用 PDF/PNG)"
if re.search(r"Emergency stop|Undefined control sequence", log_tail):
return "LaTeX 源码有语法错误,请展开日志定位出错行"
return "编译失败,请查看日志定位 LaTeX 错误"
async def inspect_zip(zip_bytes: bytes) -> dict[str, Any]:
"""两段式之①:安全解压 + 主文件识别 + 落盘 inspect 暂存(1h TTL)。
→ {job_id, main, stats{files,tex,images,cls,bib,other,bytes}, skipped[], skipped_count}
失败抛 InspectError(路由层转信封);任何失败 → rmtree 暂存目录,磁盘零残留。
"""
from .zip_safety import ZipSafetyError, safe_extract
settings = get_settings()
sweep_inspect_expired() # 机会性清扫(同 P3a 模式)
job_id = uuid.uuid4().hex[:12]
jdir = inspect_root() / job_id
project = jdir / "project"
try:
try:
result = await asyncio.to_thread(safe_extract, zip_bytes, project, settings)
except ZipSafetyError as exc:
raise InspectError(exc.code, exc.message) from exc
main, candidates, tex_total = find_main_tex(project)
if tex_total == 0:
raise InspectError("VALIDATION", "zip 中没有 .tex 文件")
if not candidates:
raise InspectError(
"VALIDATION", "未找到含 \\documentclass 的 .tex 文件")
if main is None:
shown = ", ".join(Path(c).name for c in candidates)
raise InspectError(
"VALIDATION",
f"检测到多个主文件({shown}),请合并为单一入口后重新上传")
meta = {
"job_id": job_id,
"main": main,
"stats": {
"files": result.files, "tex": result.tex, "images": result.images,
"cls": result.cls, "bib": result.bib, "other": result.other,
"bytes": result.bytes,
},
"skipped": [{"name": s.name, "reason": s.reason} for s in result.skipped],
"skipped_count": result.skipped_count,
"created_at": datetime.now(timezone.utc).isoformat(timespec="seconds"),
}
jdir.mkdir(parents=True, exist_ok=True)
(jdir / "inspect.json").write_text(
json.dumps(meta, ensure_ascii=False, indent=2), encoding="utf-8")
return meta
except BaseException:
shutil.rmtree(jdir, ignore_errors=True)
raise
def consume_inspect(job_id: str, cid: str) -> tuple[Path, dict[str, Any]]:
"""两段式之②前置:**原子 rename 消费** inspect 暂存 → compile_jobs/{cid}/project。
并发正确性(防同一 job 双跑):
os.rename(src, dst) 在同一文件系统内是原子的,且 src 不存在 → FileNotFoundError。
两个并发 run 拿同一 job_id 时,只有一个能成功 rename;后到者拿到
FileNotFoundError → 路由层转 VALIDATION"已过期/不存在",**绝不可能双编译**。
inspect 残壳(inspect.json)在 rename 成功后立即删除,也杜绝二跑读到半状态。
"""
settings = get_settings()
if not _ANCHOR_ID_RE.fullmatch(job_id or ""):
raise InspectError("VALIDATION", "job_id 格式非法(应为 12 位 hex)")
src_root = inspect_root() / job_id
meta_path = src_root / "inspect.json"
src_project = src_root / "project"
if not (meta_path.is_file() and src_project.is_dir()):
raise InspectError(
"VALIDATION", "上传任务不存在或已过期(1 小时),请重新上传 zip")
try:
meta = json.loads(meta_path.read_text("utf-8"))
except (OSError, ValueError) as exc:
raise InspectError(
"VALIDATION", "上传任务数据损坏,请重新上传 zip") from exc
dst_root = compile_job_dir(cid)
dst_project = dst_root / "project"
dst_root.mkdir(parents=True, exist_ok=True)
try:
os.rename(src_project, dst_project)
except FileNotFoundError as exc:
raise InspectError(
"VALIDATION", "上传任务不存在或已过期(1 小时),请重新上传 zip") from exc
except OSError as exc: # 跨设备等罕见失败 → 保持 inspect 原样可重试
raise InspectError(
"VALIDATION", "任务消费失败,请重试或重新上传") from exc
shutil.rmtree(src_root, ignore_errors=True) # 消费成功 → 删残壳
return dst_project, meta
async def compile_project_job(job_id: str) -> dict[str, Any]:
"""两段式之②:原子消费 inspect → tectonic 编译 → 产物留 pdf/log/meta。
队列/工具检查**先于消费**(inspect 保留 → 失败可直接重试);
EPS v2:解压后批量转换所有 .eps → 同名 .pdf(gs -dSAFER → epstopdf → ps2pdf,
单文件 15s 超时;产物累计 >100MB → ZIP_TOO_LARGE 中止),转换日志进 output.log;
成功 → 立即删 project/;失败(COMPILE_FAILED/TIMEOUT)→ **保留 project/ 现场 24h**
供诊断(sweep_compile_jobs_expired 按 failed_project_ttl_s 清理;
KEEP_FAILED_PROJECTS=1 永久保留);
失败必落完整 output.log(含 --print 的 XDV→PDF 阶段输出,补充 3)。
→ {compile_id, pdf_path, log_path, seconds, pdf_bytes}
"""
global _pending
settings = get_settings()
_ensure_tool() # 快速失败:不消费 inspect、不占队列
sweep_inspect_expired()
sweep_compile_jobs_expired()
if _pending >= settings.compile_queue_limit:
raise QueueFullError(
f"编译队列已满({_pending} 个任务排队),请稍后重试")
_pending += 1 # 占位先于消费:QUEUE_FULL 时 inspect 未动
cid = uuid.uuid4().hex[:12]
project: Path | None = None
meta: dict[str, Any] = {}
try:
project, meta = await asyncio.to_thread(consume_inspect, job_id, cid)
try:
# EPS v2:解压后批量转换所有 .eps → 同名 .pdf 并切换显式引用
# (不依赖 zip 自带预转换版;失败现场保留转换后的改写结果)
eps_lines = await asyncio.to_thread(process_eps_images, project)
async with _lock:
result = await _run_project(
cid, project, str(meta.get("main", "")), eps_lines)
# 成功 → 立即清理 project 树(失败路径在 finally 保留现场)
shutil.rmtree(project, ignore_errors=True)
project = None
return result
except CompileError as exc:
# 补充 3:失败信封也要能下载 log → 异常携带产物 cid(log 落盘于 compile_jobs/{cid}/)
exc.compile_id = cid
raise
finally:
_pending -= 1
if project is not None:
# 编译失败 → 保留 compile_jobs/{cid}/project/ 现场供诊断;
# touch 目录使 24h 保留窗从失败时刻起算
try:
os.utime(project, None)
except OSError:
pass
async def _run_project(
cid: str,
project: Path,
main_rel: str,
eps_log_lines: list[str] | None = None,
) -> dict[str, Any]:
"""单槽内执行项目编译:cwd=main 所在目录(相对路径解析),outdir=compile_jobs/{cid}。
eps_log_lines:EPS v2 的 [eps-convert] 转换日志(约束4),正常/超时两条路径
都会写进 output.log(引擎 log 之后、chatter/[timeout] 之前)。
"""
settings = get_settings()
outdir = compile_job_dir(cid)
outdir.mkdir(parents=True, exist_ok=True)
main_path = project / main_rel
if not main_rel or not main_path.is_file():
raise InspectError("VALIDATION", "主文件缺失,请重新上传 zip")
cwd = main_path.parent
stem = main_path.stem
env = dict(os.environ)
env["TECTONIC_CACHE_DIR"] = str(settings.tectonic_cache_abs)
# --print:打印引擎完整 chatter(含 XDV→PDF/xdvipdfmx 阶段输出);
# 不加时 xdvipdfmx 报错只进 tectonic stderr、不落 {stem}.log,导致"LaTeX 成功但
# 无 PDF"且 log 无错误行(MDPI 模板实测)。stdout/stderr 统一落盘 output.log。
cmd = [
str(_tectonic_bin()), "-X", "compile", main_path.name,
"--untrusted", "--keep-logs", "--print", "--outdir", str(outdir),
]
timeout = settings.compile_project_timeout_s
log_path = outdir / "output.log"
started = time.monotonic()
try:
proc = await asyncio.create_subprocess_exec(
*cmd, cwd=str(cwd), env=env,
stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.STDOUT,
start_new_session=True,
)
try:
stdout_bytes, _ = await asyncio.wait_for(
proc.communicate(), timeout=timeout)
except asyncio.TimeoutError:
_kill_group(proc)
try:
await asyncio.wait_for(proc.wait(), timeout=5.0)
except asyncio.TimeoutError:
pass
# 超时后 communicate 已取消,chatter 不可取;.log(--keep-logs)仍完整
_ensure_project_log(outdir, stem, cwd)
# 约束4:转换日志先落(转换发生在编译之前),再追加超时标记
_append_eps_lines(log_path, eps_log_lines)
# 追加而非覆盖:保留完整引擎输出(诊断 XDV→PDF 阶段必需)
with log_path.open("a", encoding="utf-8") as fh:
fh.write(f"\n[timeout] 编译超时({timeout:g}s),进程已终止\n")
raise CompileError(
"TIMEOUT",
f"编译超过 {timeout:g} 秒未完成,已终止"
"(项目首次编译需下载宏包,可能较慢,请重试)",
_log_tail(log_path),
) from None
except FileNotFoundError as exc:
raise ToolUnavailableError(f"tectonic 不可执行:{exc}") from exc
finally:
seconds = round(time.monotonic() - started, 2)
_ensure_project_log(outdir, stem, cwd)
# 产物归一:{stem}.pdf/.log → output.pdf/.log(补充 3:失败也必有 output.log)
produced_pdf = outdir / f"{stem}.pdf"
produced_log = outdir / f"{stem}.log"
if produced_log.is_file():
shutil.move(str(produced_log), str(log_path))
if not log_path.is_file():
log_path.write_text(
"[no log] tectonic 未产出日志(进程异常退出)", encoding="utf-8")
# 约束4:[eps-convert] 转换日志(引擎 log 之后、chatter 之前)
_append_eps_lines(log_path, eps_log_lines)
# --print 的进程输出(含 XDV→PDF 阶段)追加到 output.log 全文之后;
# 信封 detail 仍只取尾 200 行,磁盘保留完整 log 供"查看完整 log"下载
_append_chatter(log_path, stdout_bytes)
if not produced_pdf.is_file():
raise CompileError(
"COMPILE_FAILED",
classify_compile_error(_log_tail(log_path)),
_log_tail(log_path),
)
pdf_bytes = produced_pdf.stat().st_size
if pdf_bytes > settings.max_compile_pdf_bytes:
produced_pdf.unlink(missing_ok=True)
raise CompileError(
"OUTPUT_TOO_LARGE",
f"产出 PDF 超过 {settings.max_compile_pdf_bytes // (1024 * 1024)}MB 上限,已删除",
_log_tail(log_path),
)
produced_pdf.rename(outdir / "output.pdf")
meta = {
"compile_id": cid,
"project": True,
"main": main_rel,
"engine": _ENGINE,
"seconds": seconds,
"pdf_bytes": pdf_bytes,
"returncode": 0,
"compiled_at": datetime.now(timezone.utc).isoformat(timespec="seconds"),
}
(outdir / "meta.json").write_text(
json.dumps(meta, ensure_ascii=False, indent=2), encoding="utf-8")
logger.info("compile(project): %s main=%s → PDF %d bytes, %.2fs",
cid, main_rel, pdf_bytes, seconds)
return {
"compile_id": cid,
"pdf_path": outdir / "output.pdf",
"log_path": log_path,
"seconds": seconds,
"pdf_bytes": pdf_bytes,
}
def _ensure_project_log(outdir: Path, stem: str, cwd: Path) -> None:
"""tectonic 落 log 到 outdir 或 cwd → 统一挪到 outdir/output.log;都没有则占位。"""
log_path = outdir / "output.log"
if log_path.is_file():
return
for cand in (outdir / f"{stem}.log", cwd / f"{stem}.log"):
if cand.is_file():
shutil.move(str(cand), str(log_path))
return
def _append_chatter(log_path: Path, stdout_bytes: bytes | None) -> None:
"""把 tectonic --print 的进程 stdout(含 XDV→PDF/xdvipdfmx 阶段)追加到 output.log。
{stem}.log 只有 LaTeX 引擎内容(止于 "Output written on … .xdv");
XDV→PDF 转换错误只出现在进程 chatter 里——不落盘则"无 PDF 且 log 无错误行"。
"""
if not stdout_bytes:
return
chatter = stdout_bytes.decode("utf-8", errors="replace").strip()
if not chatter:
return
with log_path.open("a", encoding="utf-8") as fh:
fh.write("\n\n===== tectonic --print(进程输出,含 XDV→PDF 阶段)=====\n")
fh.write(chatter)
fh.write("\n")
def sweep_inspect_expired() -> int:
"""删 mtime 超 inspect_ttl_s(默认 1h)的 12-hex inspect 暂存目录。"""
settings = get_settings()
base = settings.inspect_dir_abs
if not base.is_dir():
return 0
cutoff = time.time() - settings.inspect_ttl_s
removed = 0
for child in base.iterdir():
if not child.is_dir() or not _ANCHOR_ID_RE.fullmatch(child.name):
continue
try:
if child.stat().st_mtime < cutoff:
shutil.rmtree(child, ignore_errors=True)
removed += 1
except OSError:
continue
if removed:
logger.info("inspect sweep: 清理 %d 个过期 inspect 暂存(>%d 秒)",
removed, settings.inspect_ttl_s)
return removed
def _keep_failed_projects() -> bool:
"""失败现场是否永久保留(调试):REVIEW_KEEP_FAILED_PROJECTS=1 或裸 KEEP_FAILED_PROJECTS=1。"""
if get_settings().keep_failed_projects:
return True
return os.environ.get("KEEP_FAILED_PROJECTS", "").strip() == "1"
def sweep_compile_jobs_expired() -> int:
"""编译产物清扫,两级:
① 失败现场 project/ 子目录:mtime 超 failed_project_ttl_s(默认 24h)→ 删子目录
(compile_jobs/{cid} 本体与 output.log/meta 保留,供"查看完整 log");
KEEP_FAILED_PROJECTS=1 跳过(永久保留,调试用)。
② 整个 compile_jobs/{cid}:mtime 超 compile_retention_days(7 天)→ 整目录删。
"""
settings = get_settings()
base = settings.compile_jobs_dir_abs
if not base.is_dir():
return 0
removed = 0
keep_failed = _keep_failed_projects()
# ① 失败现场 24h 清理(成功编译的 project/ 已在 compile_project_job 立即删)
if not keep_failed:
proj_cutoff = time.time() - settings.failed_project_ttl_s
for child in base.iterdir():
if not child.is_dir() or not _ANCHOR_ID_RE.fullmatch(child.name):
continue
proj = child / "project"
if not proj.is_dir():
continue
try:
if proj.stat().st_mtime < proj_cutoff:
shutil.rmtree(proj, ignore_errors=True)
removed += 1
except OSError:
continue
# ② 整目录 7 天清理
cutoff = time.time() - settings.compile_retention_days * 86400
for child in base.iterdir():
if not child.is_dir() or not _ANCHOR_ID_RE.fullmatch(child.name):
continue
try:
if child.stat().st_mtime < cutoff:
shutil.rmtree(child, ignore_errors=True)
removed += 1
except OSError:
continue
if removed:
logger.info("compile_jobs sweep: 清理 %d 个过期项目产物/失败现场", removed)
return removed
+288
View File
@@ -0,0 +1,288 @@
r"""services/zip_safety.py —— LaTeX 项目 zip 安全校验与解压(P4,纯函数模块)。
安全清单(每条攻击 → 防御位置,对应设计 §2):
#1 路径穿越 ../../etc/passwd → sanitize_entry_name:拒绝 .. 段/绝对路径/盘符/空段
(\ 已先归一为 /,Windows 反斜杠变体同规则)
#2 反斜杠变体 ..\\..\\x → 同上(name.replace("\\\\", "/") 后再校验)
#3 符号链接条目 → _is_symlink:external_attr 的 S_IFMT == S_IFLNK → 整包拒绝
#4 可执行扩展名 .exe/.sh/.so → classify_entry:DENY_EXT → 整包拒绝(点名条目)
#5 Unix 可执行位 0o111 → _has_exec_bit:普通文件带 x 位 → 整包拒绝(目录 0o755 例外)
#6 无害非白名单 .docx/.log/.zip → classify_entry:skip(记 skipped[];.zip 跳过=天然防嵌套递归)
#7 zip bomb 压缩比 → 预扫 file_size/compress_size > max_zip_ratio → ZIP_BOMB
#8 zip bomb 总量 → 预扫声明累计 > max_extract_bytes → ZIP_BOMB;
流式写盘按**实际字节**再计数(不信头部声明)超限即中止
#9 zip bomb 单条目 → 预扫 file_size > max_entry_bytes → ZIP_BOMB;实写计数兜底
#10 zip bomb 条目数 → 预扫 len(infolist) > max_zip_entries → ZIP_BOMB(先于一切写盘)
#11 深嵌套/超长路径 → 路径深度 > max_path_depth 或总长 > max_path_len → ZIP_UNSAFE
#12 损坏/加密 zip → zipfile.BadZipFile / RuntimeError → ZIP_INVALID
#13 头部谎报小文件 → 实写计数兜底(#8);CRC 错 → ZIP_INVALID
#14 恶意 .tex shell escape → 不在本模块范围(tectonic --untrusted 防护,P3a 已实证)
#15 解压阻塞事件循环 → 调用方 asyncio.to_thread 包裹;本模块上限 200MB/2000 条封顶
设计要点:
- 两段式:①纯预扫(所有结构性检查,全部通过才开始写)→ ②流式写盘(8KB 块 + 实字节计数)。
任何异常 → 调用方 rmtree(dest),磁盘零残留。
- 绝不 extractall;绝不保留可执行位(写盘一律 0o644)。
- 目录条目只建不校验扩展名;SKIP_DIRS 整目录跳过(补充 4 提示对应)。
纯函数:输入 bytes/路径/Settings,输出 ExtractResult 或抛 ZipSafetyError,无 FastAPI/asyncio 依赖。
"""
from __future__ import annotations
import io
import re
import stat
import zipfile
from dataclasses import dataclass, field
from pathlib import Path, PurePosixPath
from typing import Literal
# 危险类型(deny → 整包拒绝)
DENY_EXT = {
".exe", ".com", ".bat", ".cmd", ".sh", ".bash", ".ps1", ".vbs",
".dll", ".so", ".dylib", ".msi", ".jar", ".py", ".rb", ".pl", ".php", ".js",
}
# LaTeX 项目白名单(allow → 解压)
ALLOW_EXT = {
".tex", ".cls", ".sty", ".bst", ".bib", ".bbl", ".blg", ".def", ".clo",
".fd", ".cfg", ".ist", ".dtx", ".ins",
".png", ".jpg", ".jpeg", ".pdf", ".eps", ".svg",
".txt", ".md", ".csv", ".dat", ".ttf", ".otf",
}
# 无扩展名白名单
ALLOW_BARE = {"README", "LICENSE", "Makefile", ".gitignore"}
# 整目录跳过(不进 skipped 计数之外的拒绝路径,直接 skip)
SKIP_DIRS = {".git", "node_modules", "__MACOSX"}
class ZipSafetyError(Exception):
"""zip 不安全/超限/损坏。code ∈ {ZIP_UNSAFE, ZIP_BOMB, ZIP_INVALID}。"""
def __init__(self, code: str, message: str, entry: str = "") -> None:
super().__init__(message)
self.code = code
self.message = message
self.entry = entry
@dataclass
class SkippedEntry:
name: str
reason: str
@dataclass
class ExtractResult:
files: int = 0
tex: int = 0
images: int = 0
cls: int = 0
bib: int = 0
other: int = 0
bytes: int = 0
skipped: list[SkippedEntry] = field(default_factory=list)
skipped_count: int = 0
_IMG_EXT = {".png", ".jpg", ".jpeg", ".eps", ".svg"}
_BARE_RE = re.compile(r"^[A-Za-z]:") # 盘符
_CTRL_RE = re.compile(r"[\x00-\x1f]") # 控制字符
def sanitize_entry_name(raw: str) -> str:
"""归一 + 校验条目名,返回 posix 相对路径;不安全 → ZIP_UNSAFE。
拒绝:绝对路径(/ 或 \\ 开头)、盘符(C:)、任一段 `..`、空段、NUL/控制字符。
"""
name = raw.replace("\\", "/")
if not name or name in ("/", "."):
raise ZipSafetyError("ZIP_UNSAFE", f"zip 含空条目名:{raw!r}", raw)
if name.startswith("/") or _BARE_RE.match(name):
raise ZipSafetyError("ZIP_UNSAFE", f"zip 含绝对路径条目:{raw}", raw)
if _CTRL_RE.search(name):
raise ZipSafetyError("ZIP_UNSAFE", f"zip 条目名含控制字符:{raw!r}", raw)
segs = name.split("/")
if any(s == ".." for s in segs):
raise ZipSafetyError("ZIP_UNSAFE", f"zip 含路径穿越条目:{raw}", raw)
# 空段(//)拒绝;尾段为 ""(目录 / 结尾)在除末位外的段上已由 split 覆盖
if any(s == "" for s in segs[:-1]) or (segs and segs[0] == ""):
raise ZipSafetyError("ZIP_UNSAFE", f"zip 含非法路径条目:{raw}", raw)
depth = len([s for s in segs if s])
if depth > 16:
raise ZipSafetyError("ZIP_UNSAFE", f"zip 条目路径过深({depth} 层):{raw}", raw)
if len(name) > 200:
raise ZipSafetyError("ZIP_UNSAFE", f"zip 条目路径过长({len(name)} 字符):{raw}", raw)
return "/".join(s for s in segs if s)
def classify_entry(name: str, is_dir: bool) -> Literal["allow", "skip", "deny"]:
"""条目分类:目录 → allow(只建);DENY_EXT → deny(整包拒绝);白名单 → allow;其余 → skip。"""
if is_dir:
return "allow"
segs = name.replace("\\", "/").split("/")
# 中间目录段命中 SKIP_DIRS → skip(.git/node_modules 整树跳过)
if any(seg in SKIP_DIRS for seg in segs[:-1]):
return "skip"
p = PurePosixPath(name)
ext = p.suffix.lower()
# 多重扩展如 .synctex.gz:取最后一段即可(gz 不在白名单 → skip)
if ext in DENY_EXT:
return "deny"
if ext in ALLOW_EXT:
return "allow"
if p.name.upper() in {b.upper() for b in ALLOW_BARE}:
return "allow"
return "skip"
def _is_symlink(info: zipfile.ZipInfo) -> bool:
"""external_attr 高 16 位为 Unix mode;S_IFMT == S_IFLNK(0xA000) → 符号链接。"""
mode = (info.external_attr >> 16) & 0xFFFF
return stat.S_IFMT(mode) == stat.S_IFLNK
def _has_exec_bit(info: zipfile.ZipInfo) -> bool:
mode = (info.external_attr >> 16) & 0xFFFF
# Windows 造的 zip(external_attr 无 Unix mode)mode 可能为 0 → 无 x 位
return bool(mode & 0o111)
def _prescan(zf: zipfile.ZipFile, settings) -> list[tuple[zipfile.ZipInfo, str]]:
"""预扫中央目录:条目数 → 路径 → 符号链接/可执行位 → 分类 → 声明大小/压缩比。
全部通过才允许写盘(两段式第一段)。
"""
infos = zf.infolist()
if len(infos) > settings.max_zip_entries:
raise ZipSafetyError(
"ZIP_BOMB", f"zip 条目数超限({len(infos)} > {settings.max_zip_entries})")
plan: list[tuple[zipfile.ZipInfo, str]] = []
declared_total = 0
for info in infos:
is_dir = info.is_dir()
name = sanitize_entry_name(info.filename)
if is_dir:
continue
# #3 符号链接 → 整包拒绝
if _is_symlink(info):
raise ZipSafetyError("ZIP_UNSAFE", f"zip 含符号链接条目:{info.filename}", info.filename)
# #4/#6 分类
kind = classify_entry(name, is_dir=False)
if kind == "deny":
raise ZipSafetyError("ZIP_UNSAFE", f"zip 含危险条目:{info.filename}", info.filename)
if kind == "skip":
continue
# #5 可执行位 → 整包拒绝(仅普通文件;目录已 continue)
if _has_exec_bit(info):
raise ZipSafetyError(
"ZIP_UNSAFE", f"zip 含可执行权限条目:{info.filename}", info.filename)
# #9 单条目声明大小
if info.file_size > settings.max_zip_entry_bytes:
raise ZipSafetyError(
"ZIP_BOMB",
f"zip 条目过大:{info.filename}({info.file_size} 字节)", info.filename)
# #7 压缩比(compress_size=0 且 file_size>0 视为异常)
if info.file_size > 0:
cs = info.compress_size
if cs == 0 or info.file_size / cs > settings.max_zip_ratio:
raise ZipSafetyError(
"ZIP_BOMB",
f"zip 条目压缩比异常(疑似 zip bomb):{info.filename}", info.filename)
declared_total += info.file_size
# #8 声明累计
if declared_total > settings.max_extract_bytes:
raise ZipSafetyError(
"ZIP_BOMB",
f"zip 解压总大小超限(声明 {declared_total} > "
f"{settings.max_extract_bytes} 字节)")
plan.append((info, name))
return plan
def safe_extract(zip_bytes: bytes, dest: Path, settings) -> ExtractResult:
"""安全解压 zip_bytes → dest(新建目录)。
两段式:①预扫(结构性检查全过)②流式写盘(8KB 块 + 实字节计数)。
任何 ZipSafetyError → 调用方负责 rmtree(dest) 清残留。
"""
try:
zf = zipfile.ZipFile(io.BytesIO(zip_bytes))
except zipfile.BadZipFile as exc:
raise ZipSafetyError("ZIP_INVALID", f"zip 文件损坏或不是有效 zip:{exc}") from exc
result = ExtractResult()
with zf:
# ---- 第一段:纯预扫 ----
plan = _prescan(zf, settings)
# ---- 第二段:流式写盘 ----
dest.mkdir(parents=True, exist_ok=True)
written_total = 0
for info, rel in plan:
target = dest / rel
# #1 双保险:resolve 后必须仍在 dest 内
resolved_parent = target.parent.resolve()
if not str(resolved_parent).startswith(str(dest.resolve())):
raise ZipSafetyError(
"ZIP_UNSAFE", f"zip 条目逃逸目标目录:{info.filename}", info.filename)
target.parent.mkdir(parents=True, exist_ok=True)
try:
src = zf.open(info)
except RuntimeError as exc: # 加密条目
raise ZipSafetyError("ZIP_INVALID", f"zip 条目受密码保护:{info.filename}") from exc
try:
with src, open(target, "wb") as out:
while True:
chunk = src.read(8192)
if not chunk:
break
# #8/#13 实写字节计数(不信头部声明)
written_total += len(chunk)
if written_total > settings.max_extract_bytes:
raise ZipSafetyError(
"ZIP_BOMB",
f"zip 实际解压超限(> {settings.max_extract_bytes} 字节),已中止")
out.write(chunk)
except zipfile.BadZipFile as exc: # CRC 校验失败(#12/#13)
raise ZipSafetyError(
"ZIP_INVALID", f"zip 条目损坏(CRC 校验失败):{info.filename}",
info.filename) from exc
# #5 绝不保留可执行位
target.chmod(0o644)
result.files += 1
_tally(result, rel)
result.bytes = written_total # 实写累计(#8 计数器终值)
# 跳过统计(限 20 条 + 总数)
skipped: list[SkippedEntry] = []
skipped_count = 0
for info in zf.infolist():
if info.is_dir():
continue
try:
name = sanitize_entry_name(info.filename)
except ZipSafetyError:
continue # 预扫已拒绝,不会到这里
if classify_entry(name, is_dir=False) == "skip":
skipped_count += 1
if len(skipped) < 20:
skipped.append(SkippedEntry(name=name, reason="unsupported"))
result.skipped = skipped
result.skipped_count = skipped_count
return result
def _tally(result: ExtractResult, rel: str) -> None:
ext = PurePosixPath(rel).suffix.lower()
if ext == ".tex":
result.tex += 1
elif ext in _IMG_EXT or ext == ".png":
result.images += 1
elif ext == ".cls":
result.cls += 1
elif ext == ".bib":
result.bib += 1
else:
result.other += 1
+4 -1
View File
@@ -30,4 +30,7 @@ set +a
echo "[restart] 启动审稿服务 :$PORT($PYTHON)"
nohup "$PYTHON" -m uvicorn "$APP_MODULE" --host "$HOST" --port "$PORT" \
> "$SCRIPT_DIR/review.log" 2>&1 &
echo "[restart] 完成;日志 review_service/review.log"
# P3b:后台预热 tectonic TeX bundle(非阻塞;仅首次需 1-3 分钟,失败不影响服务)
nohup bash "$SCRIPT_DIR/scripts/prewarm_tectonic.sh" \
> "$SCRIPT_DIR/prewarm.log" 2>&1 &
echo "[restart] 完成;日志 review_service/review.log(预热 prewarm.log)"
@@ -0,0 +1,34 @@
#!/usr/bin/env bash
# prewarm_tectonic.sh —— P3b:预下载 tectonic TeX bundle(宏包缓存)
# 目的:服务启动时预热 data/tectonic-cache/,用户首次编译不必等 1-3 分钟下载。
# 用法: bash scripts/prewarm_tectonic.sh (后台跑,非阻塞;restart.sh 自动调起)
# 注:tectonic v0.17.0 无 `download` 子命令——预热 = 用最小 hello.tex 实编译一次,
# 让 bundle/宏包落进 TECTONIC_CACHE_DIR(同 tools/README.md 预热法)。
set -uo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
cd "$SCRIPT_DIR"
TECTONIC="$SCRIPT_DIR/tools/tectonic"
[[ -x "$TECTONIC" ]] || { echo "[prewarm] tectonic 不存在,跳过:$TECTONIC"; exit 0; }
export TECTONIC_CACHE_DIR="${REVIEW_TECTONIC_CACHE_DIR:-$SCRIPT_DIR/data/tectonic-cache}"
mkdir -p "$TECTONIC_CACHE_DIR"
TMPDIR_PREWARM="$(mktemp -d)"
trap 'rm -rf "$TMPDIR_PREWARM"' EXIT
cat > "$TMPDIR_PREWARM/hello.tex" <<'EOF'
\documentclass{article}
\begin{document}
prewarm
\end{document}
EOF
echo "[prewarm] 预下载 TeX bundle 至 $TECTONIC_CACHE_DIR(首次 1-3 分钟,仅一次)"
if "$TECTONIC" -X compile "$TMPDIR_PREWARM/hello.tex" --untrusted \
--outdir "$TMPDIR_PREWARM" >/dev/null 2>&1; then
echo "[prewarm] OK:宏包缓存就绪,用户首编可秒级命中"
else
echo "[prewarm] 预热失败(网络/磁盘):不影响服务启动,首编时仍会现场拉取" >&2
exit 0
fi
+639
View File
@@ -0,0 +1,639 @@
"""tests/test_compile_zip.py —— P4 项目 zip 两段式编译集成测试。
真实调用 tools/tectonic(宏包已预热缓存);二进制缺失时集成用例 skipif 跳过,
校验/消费/清扫等纯逻辑用例照常运行。
运行: cd review_service && <venv>/bin/python -m pytest -q
"""
from __future__ import annotations
import io
import json
import os
import shutil
import time
import zipfile
from pathlib import Path
import pytest
from fastapi.testclient import TestClient
from app.config import get_settings
from app.main import app
from app.services import latex_compile as lc
MINIMAL = "\\documentclass[11pt]{article}\n\\begin{document}\nhello p4\n\\end{document}\n"
_TECTONIC_OK = get_settings().tectonic_abs.is_file()
needs_tectonic = pytest.mark.skipif(
not _TECTONIC_OK, reason="tools/tectonic 未安装(bash tools/install_tectonic.sh)"
)
def _zip_bytes(entries: dict[str, str | bytes]) -> bytes:
buf = io.BytesIO()
with zipfile.ZipFile(buf, "w", zipfile.ZIP_DEFLATED) as zf:
for name, data in entries.items():
zf.writestr(name, data)
return buf.getvalue()
def _upload(client: TestClient, payload: bytes, name: str = "project.zip"):
return client.post(
"/api/review/compile/inspect",
files={"file": (name, payload, "application/zip")},
)
def _tiny_pdf() -> bytes:
"""极小合法 PDF(1 页 20x20 蓝色方块),供图片内嵌集成用例。"""
stream = b"0 0 1 rg 2 2 10 10 re f\n"
objs = [
b"<< /Type /Catalog /Pages 2 0 R >>",
b"<< /Type /Pages /Kids [3 0 R] /Count 1 >>",
b"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 20 20]"
b" /Contents 4 0 R /Resources << >> >>",
b"<< /Length " + str(len(stream)).encode() + b" >>\nstream\n"
+ stream + b"endstream",
]
out = bytearray(b"%PDF-1.4\n")
offsets = []
for i, body in enumerate(objs, 1):
offsets.append(len(out))
out += f"{i} 0 obj\n".encode() + body + b"\nendobj\n"
xref_pos = len(out)
out += f"xref\n0 {len(objs) + 1}\n".encode()
out += b"0000000000 65535 f \n"
for off in offsets:
out += f"{off:010d} 00000 n \n".encode()
out += (f"trailer\n<< /Size {len(objs) + 1} /Root 1 0 R >>\n"
f"startxref\n{xref_pos}\n%%EOF\n").encode()
return bytes(out)
@pytest.fixture()
def client(tmp_path, monkeypatch):
settings = get_settings()
monkeypatch.setattr(settings, "reformat_dir", str(tmp_path / "jobs"))
monkeypatch.setattr(settings, "inspect_dir", str(tmp_path / "inspect"))
monkeypatch.setattr(settings, "compile_jobs_dir", str(tmp_path / "compile_jobs"))
monkeypatch.setattr(settings, "compile_timeout_s", 60.0)
monkeypatch.setattr(settings, "compile_project_timeout_s", 60.0)
monkeypatch.setattr(lc, "_pending", 0)
with TestClient(app) as c:
yield c
# ================================================================ inspect 校验
def test_inspect_not_zip(client: TestClient):
r = client.post(
"/api/review/compile/inspect",
files={"file": ("x.zip", b"notazip", "application/zip")},
)
assert r.status_code == 200 and r.json()["error"]["code"] == "ZIP_INVALID"
def test_inspect_empty(client: TestClient):
r = _upload(client, b"")
assert r.json()["error"]["code"] == "VALIDATION"
def test_inspect_oversize(client: TestClient, monkeypatch):
monkeypatch.setattr(get_settings(), "max_zip_bytes", 10)
r = _upload(client, _zip_bytes({"main.tex": MINIMAL}))
assert r.json()["error"]["code"] == "VALIDATION"
assert "上限" in r.json()["error"]["message"]
def test_inspect_bad_filename(client: TestClient):
r = _upload(client, _zip_bytes({"main.tex": MINIMAL}), name="../evil.zip")
assert r.json()["error"]["code"] == "VALIDATION"
def test_inspect_path_traversal(client: TestClient):
buf = io.BytesIO()
with zipfile.ZipFile(buf, "w") as zf:
zf.writestr("main.tex", MINIMAL)
info = zipfile.ZipInfo("../../evil.txt")
zf.writestr(info, "x")
r = _upload(client, buf.getvalue())
assert r.json()["error"]["code"] == "ZIP_UNSAFE"
def test_inspect_denied_ext(client: TestClient):
r = _upload(client, _zip_bytes({"main.tex": MINIMAL, "evil.sh": "#!/bin/sh\n"}))
assert r.json()["error"]["code"] == "ZIP_UNSAFE"
assert "evil.sh" in r.json()["error"]["message"]
def test_inspect_ratio_bomb(client: TestClient, monkeypatch):
monkeypatch.setattr(get_settings(), "max_zip_ratio", 1.5)
r = _upload(client, _zip_bytes({"main.tex": "a" * 100_000}))
assert r.json()["error"]["code"] == "ZIP_BOMB"
def test_inspect_no_tex(client: TestClient):
r = _upload(client, _zip_bytes({"readme.txt": "hi"}))
assert r.json()["error"]["code"] == "VALIDATION"
assert "没有 .tex" in r.json()["error"]["message"]
def test_inspect_no_documentclass(client: TestClient):
r = _upload(client, _zip_bytes({"chapters/a.tex": "just text\n"}))
assert r.json()["error"]["code"] == "VALIDATION"
assert "documentclass" in r.json()["error"]["message"]
def test_inspect_multiple_mains(client: TestClient):
r = _upload(client, _zip_bytes({
"main.tex": MINIMAL, "appendix.tex": MINIMAL}))
body = r.json()
assert body["error"]["code"] == "VALIDATION"
assert "多个主文件" in body["error"]["message"]
# ================================================================ inspect 正常 + 卫生
def test_inspect_happy_path(client: TestClient):
payload = _zip_bytes({
"main.tex": MINIMAL,
"figs/fig1.png": b"\x89PNG\r\n\x1a\nfake",
"refs.bib": "@article{a,\n title={t}\n}\n",
"journal.cls": "% fake cls\n",
"notes.docx": "skip me",
".DS_Store": "junk",
"__MACOSX/._main.tex": "resource fork junk",
})
r = _upload(client, payload)
body = r.json()
assert body["ok"] is True, body
meta = body["meta"]
assert meta["main"] == "main.tex"
stats = meta["stats"]
assert stats["tex"] == 1 and stats["images"] == 1
assert stats["cls"] == 1 and stats["bib"] == 1
assert meta["skipped_count"] >= 2
names = {s["name"] for s in meta["skipped"]}
assert any("notes.docx" in n for n in names)
job_id = meta["job_id"]
assert len(job_id) == 12
jdir = get_settings().inspect_dir_abs / job_id
assert (jdir / "inspect.json").is_file()
assert (jdir / "project" / "main.tex").is_file()
def test_inspect_fail_leaves_no_residue(client: TestClient):
r = _upload(client, _zip_bytes({"main.tex": MINIMAL, "evil.sh": "x"}))
assert r.json()["ok"] is False
base = get_settings().inspect_dir_abs
if base.is_dir():
assert not any(base.iterdir()), "inspect 失败后磁盘有残留"
# ================================================================ run:校验 / 原子消费
def test_run_bad_job_id(client: TestClient):
r = client.post("/api/review/compile/run", json={"job_id": "zz"})
assert r.json()["error"]["code"] == "VALIDATION"
def test_run_unknown_job(client: TestClient):
r = client.post("/api/review/compile/run", json={"job_id": "0123456789ab"})
assert r.json()["error"]["code"] == "VALIDATION"
def test_run_double_consume_rejected(client: TestClient):
"""补充:原子消费——同一 job 第二次 run 必须 VALIDATION(防并发双跑)。"""
job_id = _upload(client, _zip_bytes({"main.tex": MINIMAL})).json()["meta"]["job_id"]
first = client.post("/api/review/compile/run", json={"job_id": job_id}).json()
first_err = first.get("error") or {}
if first_err.get("code") == "TOOL_UNAVAILABLE":
pytest.skip("tectonic 未安装,消费逻辑未触达")
# 首次无论编译成败(消费已发生),二次必失败
assert first["ok"] is True or first["error"]["code"] in (
"COMPILE_FAILED", "TIMEOUT")
second = client.post("/api/review/compile/run", json={"job_id": job_id}).json()
assert second["ok"] is False
assert second["error"]["code"] == "VALIDATION"
# inspect 残壳也不应存在
assert not (get_settings().inspect_dir_abs / job_id / "project").exists()
def test_run_tool_unavailable_keeps_job(client: TestClient, monkeypatch):
"""队列/工具检查先于消费 → TOOL_UNAVAILABLE 时 inspect 保留可重试。"""
job_id = _upload(client, _zip_bytes({"main.tex": MINIMAL})).json()["meta"]["job_id"]
with monkeypatch.context() as m:
m.setattr(get_settings(), "tectonic_path", "tools/definitely-missing")
r = client.post("/api/review/compile/run", json={"job_id": job_id}).json()
assert r["error"]["code"] == "TOOL_UNAVAILABLE"
assert (get_settings().inspect_dir_abs / job_id / "inspect.json").is_file()
def test_run_queue_full_keeps_job(client: TestClient, monkeypatch):
job_id = _upload(client, _zip_bytes({"main.tex": MINIMAL})).json()["meta"]["job_id"]
monkeypatch.setattr(lc, "_pending", 5)
monkeypatch.setattr(get_settings(), "compile_queue_limit", 5)
r = client.post("/api/review/compile/run", json={"job_id": job_id})
assert r.status_code == 429
assert r.json()["error"]["code"] == "QUEUE_FULL"
assert (get_settings().inspect_dir_abs / job_id / "inspect.json").is_file()
# ================================================================ run:真实 tectonic
@needs_tectonic
def test_run_project_happy_path(client: TestClient):
payload = _zip_bytes({"main.tex": MINIMAL})
job_id = _upload(client, payload).json()["meta"]["job_id"]
r = client.post("/api/review/compile/run", json={"job_id": job_id})
body = r.json()
assert body["ok"] is True, body
cid = body["compile_id"]
assert len(cid) == 12
pdf = client.get(body["pdf_url"])
assert pdf.status_code == 200
assert pdf.headers["content-type"].startswith("application/pdf")
assert pdf.content[:5] == b"%PDF-"
log = client.get(body["log_url"])
assert log.status_code == 200
assert "LaTeX2e" in log.text and "Output written" in log.text
meta = client.get(f"/api/review/compile/zip/{cid}")
m = meta.json()
assert m["ok"] is True
assert m["meta"]["project"] is True
assert m["meta"]["main"] == "main.tex"
assert m["meta"]["pdf_bytes"] > 0
# project 树已删,只留产物
cdir = get_settings().compile_jobs_dir_abs / cid
assert not (cdir / "project").exists()
assert (cdir / "output.pdf").is_file()
@needs_tectonic
def test_run_failed_envelope_has_log_url(client: TestClient):
"""补充 3:编译失败信封带 compile_id + log_url,且 log 可下载。"""
bad = "\\documentclass{article}\n\\begin{document}\n\\undefinedcommandxyz\n"
job_id = _upload(client, _zip_bytes({"main.tex": bad})).json()["meta"]["job_id"]
body = client.post("/api/review/compile/run", json={"job_id": job_id}).json()
assert body["ok"] is False
assert body["error"]["code"] == "COMPILE_FAILED"
cid = body.get("compile_id")
assert cid and len(cid) == 12
assert body["log_url"] == f"/api/review/compile/zip/{cid}/log"
log = client.get(body["log_url"])
assert log.status_code == 200 and log.text.strip()
@needs_tectonic
def test_run_failed_keeps_project_scene(client: TestClient):
"""失败保留现场(诊断):COMPILE_FAILED → compile_jobs/{cid}/project/ 不删。"""
bad = "\\documentclass{article}\n\\begin{document}\n\\undefinedcommandxyz\n"
job_id = _upload(client, _zip_bytes({"main.tex": bad})).json()["meta"]["job_id"]
body = client.post("/api/review/compile/run", json={"job_id": job_id}).json()
assert body["ok"] is False and body["error"]["code"] == "COMPILE_FAILED"
cid = body["compile_id"]
cdir = get_settings().compile_jobs_dir_abs / cid
assert (cdir / "project" / "main.tex").is_file() # 现场保留
assert (cdir / "output.log").is_file()
@needs_tectonic
def test_run_missing_cls_friendly_message(client: TestClient):
"""classify_compile_error:缺 .cls → 期刊模板提示(Q4/§4.4)。"""
tex = "\\documentclass{noexist-cls}\n\\begin{document}\nx\n"
job_id = _upload(client, _zip_bytes({"main.tex": tex})).json()["meta"]["job_id"]
body = client.post("/api/review/compile/run", json={"job_id": job_id}).json()
assert body["ok"] is False and body["error"]["code"] == "COMPILE_FAILED"
assert "noexist-cls" in body["error"]["message"]
@needs_tectonic
def test_run_project_malicious_write18(client: TestClient, tmp_path):
canary = tmp_path / "canary_p4"
evil = (
"\\documentclass{article}\n"
f"\\write18{{touch {canary}}}\n"
"\\begin{document}\nno shell please\n\\end{document}\n"
)
job_id = _upload(client, _zip_bytes({"main.tex": evil})).json()["meta"]["job_id"]
client.post("/api/review/compile/run", json={"job_id": job_id})
assert not canary.exists(), r"\write18 竟然执行了!"
@needs_tectonic
def test_run_project_timeout(client: TestClient, monkeypatch):
monkeypatch.setattr(get_settings(), "compile_project_timeout_s", 5.0)
loop_tex = (
"\\documentclass{article}\n\\begin{document}\n"
"\\count255=0\n\\loop\\iftrue\\advance\\count255 by 1\\repeat\n"
"\\end{document}\n"
)
job_id = _upload(client, _zip_bytes({"main.tex": loop_tex})).json()["meta"]["job_id"]
started = time.monotonic()
body = client.post("/api/review/compile/run", json={"job_id": job_id}).json()
elapsed = time.monotonic() - started
assert body["ok"] is False and body["error"]["code"] == "TIMEOUT"
assert elapsed < 15.0, f"超时回收过慢:{elapsed:.1f}s"
# 超时也必落 log(补充 3)
log = client.get(body["log_url"])
assert log.status_code == 200
assert "[timeout]" in log.text
# ================================================================ EPS 兼容 v2(批量现场转换)
_MIN_EPS = (
b"%!PS-Adobe-3.0 EPSF-3.0\n%%BoundingBox: 0 0 10 10\n"
b"0 0 1 setrgbcolor 0 0 10 10 rectfill\nshowpage\n%%EOF\n"
)
def _eps_project(tmp_path, tex_body: str = "") -> Path:
proj = tmp_path / "p"
proj.mkdir()
(proj / "fig.eps").write_bytes(_MIN_EPS)
(proj / "main.tex").write_text(
"\\documentclass{article}\n\\usepackage{graphicx}\n"
"\\begin{document}\n" + tex_body + "\n\\end{document}\n",
encoding="utf-8")
return proj
def test_eps_v2_preset_converted(tmp_path, monkeypatch):
"""zip 自带 *-eps-converted-to.pdf → 免转换复制为 X.pdf + 显式引用改写。"""
# other.eps 无转换版 → mock 工具为空使其失败,隔离 preset 逻辑(不真跑 gs)
monkeypatch.setattr(lc, "_eps_tool_commands", lambda eps, pdf: [])
proj = _eps_project(tmp_path)
defs = proj / "Definitions"
defs.mkdir()
(defs / "logo-mdpi.eps").write_bytes(_MIN_EPS)
(defs / "logo-mdpi-eps-converted-to.pdf").write_bytes(_tiny_pdf())
(defs / "other.eps").write_bytes(b"%!PS\n") # 无转换版 → 走现场转换(被 mock 挡下)
(defs / "mdpi.cls").write_text(
"\\includegraphics{Definitions/logo-mdpi.eps}\n"
"\\includegraphics{Definitions/other.eps}\n", encoding="utf-8")
lines = lc.process_eps_images(proj)
cls = (defs / "mdpi.cls").read_text(encoding="utf-8")
assert "Definitions/logo-mdpi.pdf" in cls # 显式引用已改写为 .pdf
assert "Definitions/other.eps" in cls # 转换失败的保持原样
assert (defs / "logo-mdpi.eps").is_file() # 原文件保留
assert (defs / "logo-mdpi.pdf").is_file() # 免转换复制
assert not (defs / "other.pdf").exists()
assert any(", preset)" in l for l in lines), lines
assert any("全部工具失败,图片 Definitions/other.eps 未转换" in l
for l in lines), lines
def test_eps_v2_real_gs_convert(tmp_path):
"""无预转换版 → 现场 gs 转换(真实工具链;测试点:显式 + 缺省扩展名引用都就位)。"""
if not shutil.which("gs"):
pytest.skip("gs 未安装")
proj = _eps_project(tmp_path, "\\includegraphics{fig.eps}\n\\includegraphics{fig}")
lines = lc.process_eps_images(proj)
assert any("[eps-convert] fig.eps → fig.pdf" in l and ", gs)" in l for l in lines)
pdf = proj / "fig.pdf"
assert pdf.is_file() and pdf.read_bytes()[:5] == b"%PDF-"
tex = (proj / "main.tex").read_text(encoding="utf-8")
assert "{fig.pdf}" in tex # 显式 .eps 引用已改写
assert "{fig}" in tex # 缺省扩展名引用由 X.pdf 命中
assert (proj / "fig.eps").is_file() # 原文件保留
def test_eps_v2_tool_fallback(tmp_path, monkeypatch):
"""工具优先级 gs → epstopdf → ps2pdf:gs 失败 → epstopdf 成功。"""
def fake_tools(eps: Path, pdf: Path):
return [
("gs", ["sh", "-c", "exit 1"]),
("epstopdf", ["sh", "-c", f"echo %PDF-fake > {pdf}"]),
]
monkeypatch.setattr(lc, "_eps_tool_commands", fake_tools)
proj = _eps_project(tmp_path, "\\includegraphics{fig.eps}")
lines = lc.process_eps_images(proj)
assert any(l.startswith("[eps-convert] fig.eps → fig.pdf") and ", epstopdf)" in l
for l in lines), lines
assert (proj / "fig.pdf").is_file()
assert "{fig.pdf}" in (proj / "main.tex").read_text(encoding="utf-8")
def test_eps_v2_all_tools_fail(tmp_path, monkeypatch):
"""全工具失败 → log 行「全部工具失败…未转换」,引用保持 .eps。"""
monkeypatch.setattr(
lc, "_eps_tool_commands",
lambda eps, pdf: [("gs", ["sh", "-c", "exit 1"])])
proj = _eps_project(tmp_path, "\\includegraphics{fig.eps}")
lines = lc.process_eps_images(proj)
assert "[eps-convert] 全部工具失败,图片 fig.eps 未转换" in lines
assert not (proj / "fig.pdf").exists()
assert "{fig.eps}" in (proj / "main.tex").read_text(encoding="utf-8")
def test_eps_v2_timeout_warns(tmp_path, monkeypatch):
"""约束2:单文件转换 15s 超时 → warning 行,放弃该文件继续。"""
monkeypatch.setattr(lc, "_EPS_CONVERT_TIMEOUT_S", 1)
monkeypatch.setattr(
lc, "_eps_tool_commands",
lambda eps, pdf: [("gs", ["sh", "-c", "sleep 5"])])
proj = _eps_project(tmp_path)
lines = lc.process_eps_images(proj)
assert "[eps-convert] fig.eps 转换超时" in lines
assert not (proj / "fig.pdf").exists()
def test_eps_v2_file_size_warning(tmp_path, monkeypatch):
"""约束3a:单文件转换后超上限 → warning 行(不中止)。"""
monkeypatch.setattr(lc, "_EPS_CONVERT_MAX_FILE", 10)
monkeypatch.setattr(
lc, "_eps_tool_commands",
lambda eps, pdf: [("gs", ["sh", "-c", f"head -c 1024 /dev/zero > {pdf}"])])
proj = _eps_project(tmp_path)
lines = lc.process_eps_images(proj)
assert any("警告" in l and "超过" in l for l in lines), lines
def test_eps_v2_total_size_aborts(tmp_path, monkeypatch):
"""约束3b:转换产物累计超上限 → InspectError(ZIP_TOO_LARGE) 中止。"""
monkeypatch.setattr(lc, "_EPS_CONVERT_MAX_TOTAL", 100)
monkeypatch.setattr(
lc, "_eps_tool_commands",
lambda eps, pdf: [("gs", ["sh", "-c", f"head -c 1024 /dev/zero > {pdf}"])])
proj = _eps_project(tmp_path)
with pytest.raises(lc.InspectError) as ei:
lc.process_eps_images(proj)
assert ei.value.code == "ZIP_TOO_LARGE"
assert "MB" in ei.value.message
@needs_tectonic
def test_run_eps_with_converted_pdf_succeeds(client: TestClient):
"""EPS + 预转换 PDF(preset 路径)→ 显式与缺省扩展名引用都编译成功。"""
tex = (
"\\documentclass{article}\n\\usepackage{graphicx}\n"
"\\begin{document}\n"
"\\includegraphics[width=1cm]{fig.eps}\n" # 显式 .eps → 文本替换
"\\includegraphics[width=1cm]{fig}\n" # 缺省扩展名 → X.pdf 命中
"\\end{document}\n"
)
payload = _zip_bytes({
"main.tex": tex,
"fig.eps": _MIN_EPS,
"fig-eps-converted-to.pdf": _tiny_pdf(),
})
job_id = _upload(client, payload).json()["meta"]["job_id"]
body = client.post("/api/review/compile/run", json={"job_id": job_id}).json()
assert body["ok"] is True, body
pdf = client.get(body["pdf_url"])
assert pdf.content[:5] == b"%PDF-"
@needs_tectonic
def test_run_eps_real_gs_conversion_succeeds(client: TestClient):
"""核心 v2 场景:真实 EPS 无预转换版 → 服务器现场转换 → 端到端编译成功。"""
if not shutil.which("gs"):
pytest.skip("gs 未安装")
tex = (
"\\documentclass{article}\n\\usepackage{graphicx}\n"
"\\begin{document}\n"
"\\includegraphics[width=1cm]{fig.eps}\n"
"\\includegraphics[width=1cm]{fig}\n"
"\\end{document}\n"
)
payload = _zip_bytes({"main.tex": tex, "fig.eps": _MIN_EPS})
job_id = _upload(client, payload).json()["meta"]["job_id"]
body = client.post("/api/review/compile/run", json={"job_id": job_id}).json()
assert body["ok"] is True, body
assert client.get(body["pdf_url"]).content[:5] == b"%PDF-"
# 约束4:转换日志落 output.log
log = client.get(body["log_url"]).text
assert "[eps-convert] fig.eps → fig.pdf" in log and ", gs)" in log
@needs_tectonic
def test_run_eps_all_tools_fail_friendly_message(
client: TestClient, monkeypatch):
"""测试点2+3:全工具失败 → log 记「全部工具失败」→ 编译失败 → 前端友好提示。"""
monkeypatch.setattr(lc, "_eps_tool_commands", lambda eps, pdf: [])
tex = (
"\\documentclass{article}\n\\usepackage{graphicx}\n"
"\\begin{document}\n\\includegraphics[width=1cm]{fig.eps}\n\\end{document}\n"
)
payload = _zip_bytes({"main.tex": tex, "fig.eps": _MIN_EPS})
job_id = _upload(client, payload).json()["meta"]["job_id"]
body = client.post("/api/review/compile/run", json={"job_id": job_id}).json()
assert body["ok"] is False and body["error"]["code"] == "COMPILE_FAILED"
assert body["error"]["message"] == \
"项目包含 EPS 图片,服务器转换失败,请转成 PDF/PNG 后重试"
log = client.get(body["log_url"]).text
assert "[eps-convert] 全部工具失败,图片 fig.eps 未转换" in log
# ================================================================ zip 只读端点 404
def test_zip_meta_404(client: TestClient):
r = client.get("/api/review/compile/zip/0123456789ab")
assert r.json()["error"]["code"] == "NOT_FOUND"
def test_zip_pdf_404(client: TestClient):
r = client.get("/api/review/compile/zip/0123456789ab/pdf")
assert r.json()["error"]["code"] == "NOT_FOUND"
def test_zip_log_404(client: TestClient):
r = client.get("/api/review/compile/zip/0123456789ab/log")
assert r.json()["error"]["code"] == "NOT_FOUND"
# ================================================================ 清扫
def test_sweep_inspect_expired(tmp_path, monkeypatch):
settings = get_settings()
monkeypatch.setattr(settings, "inspect_dir", str(tmp_path / "inspect"))
base = tmp_path / "inspect"
stale = base / "aabbccddeeff"
stale.mkdir(parents=True)
(stale / "inspect.json").write_text("{}", encoding="utf-8")
(stale / "project").mkdir()
old = time.time() - 2 * 3600
os.utime(stale, (old, old))
fresh = base / "112233445566"
fresh.mkdir()
noise = base / "not-a-job"
noise.mkdir()
removed = lc.sweep_inspect_expired()
assert removed == 1
assert not stale.exists()
assert fresh.exists()
assert noise.exists()
def test_sweep_compile_jobs_expired(tmp_path, monkeypatch):
settings = get_settings()
monkeypatch.setattr(settings, "compile_jobs_dir", str(tmp_path / "cj"))
base = tmp_path / "cj"
stale = base / "aabbccddeeff"
stale.mkdir(parents=True)
(stale / "output.pdf").write_bytes(b"%PDF-fake")
old = time.time() - 8 * 86400
os.utime(stale, (old, old))
fresh = base / "112233445566"
fresh.mkdir()
(fresh / "output.pdf").write_bytes(b"%PDF-fake")
noise = base / "not-a-job"
noise.mkdir()
removed = lc.sweep_compile_jobs_expired()
assert removed == 1
assert not stale.exists()
assert fresh.exists()
assert noise.exists()
def test_sweep_failed_project_ttl(tmp_path, monkeypatch):
"""两级清扫①:失败现场 project/ 超 failed_project_ttl_s → 只删子目录,log 保留。"""
settings = get_settings()
monkeypatch.setattr(settings, "compile_jobs_dir", str(tmp_path / "cj"))
monkeypatch.delenv("KEEP_FAILED_PROJECTS", raising=False)
base = tmp_path / "cj"
stale = base / "aabbccddeeff"
(stale / "project").mkdir(parents=True)
(stale / "output.log").write_text("log", encoding="utf-8")
old = time.time() - settings.failed_project_ttl_s - 3600
os.utime(stale / "project", (old, old))
os.utime(stale, (old, old)) # 整目录也旧,但未超 7 天 → ② 不触发
fresh = base / "112233445566"
(fresh / "project").mkdir(parents=True)
(fresh / "output.log").write_text("log", encoding="utf-8")
removed = lc.sweep_compile_jobs_expired()
assert removed == 1
assert not (stale / "project").exists() # 现场删
assert (stale / "output.log").is_file() # log 留(完整 log 下载)
assert (fresh / "project").exists()
def test_sweep_failed_project_kept_when_flag(tmp_path, monkeypatch):
"""KEEP_FAILED_PROJECTS=1:失败现场永久保留(跳过①)。"""
settings = get_settings()
monkeypatch.setattr(settings, "compile_jobs_dir", str(tmp_path / "cj"))
monkeypatch.setenv("KEEP_FAILED_PROJECTS", "1")
base = tmp_path / "cj"
stale = base / "aabbccddeeff"
(stale / "project").mkdir(parents=True)
old = time.time() - settings.failed_project_ttl_s - 3600
os.utime(stale / "project", (old, old))
removed = lc.sweep_compile_jobs_expired()
assert removed == 0
assert (stale / "project").exists()
+195
View File
@@ -0,0 +1,195 @@
"""tests/test_zip_safety.py —— P4 zip 安全模块纯逻辑测试(无需 tectonic)。
覆盖设计 §2 安全清单 #1-#13(#14 tectonic --untrusted 归 test_compile.py,
#15 由调用方 to_thread 承担,模块本身只测上限封顶行为)。
运行: cd review_service && <venv>/bin/python -m pytest -q tests/test_zip_safety.py
"""
from __future__ import annotations
import io
import stat
import zipfile
from pathlib import Path
import pytest
from app.config import get_settings
from app.services.zip_safety import (
ExtractResult,
ZipSafetyError,
classify_entry,
safe_extract,
sanitize_entry_name,
)
def _settings(**over):
"""取真实 settings 并覆盖上限(dataclass 风格 monkeypatch)。"""
s = get_settings()
for k, v in over.items():
setattr(s, k, v)
return s
def _make_zip(entries: dict[str, bytes], exec_bits: dict[str, int] | None = None) -> bytes:
"""entries: name → content;exec_bits: name → unix mode(如 0o100755)。
必须显式 ZIP_DEFLATED:手动构造的 ZipInfo 默认 ZIP_STORED(ratio=1,测不出压缩比炸弹)。
"""
buf = io.BytesIO()
with zipfile.ZipFile(buf, "w", zipfile.ZIP_DEFLATED) as zf:
for name, data in entries.items():
info = zipfile.ZipInfo(name)
info.compress_type = zipfile.ZIP_DEFLATED
if exec_bits and name in exec_bits:
info.external_attr = exec_bits[name] << 16
zf.writestr(info, data)
return buf.getvalue()
def _symlink_zip(name: str = "link") -> bytes:
buf = io.BytesIO()
with zipfile.ZipFile(buf, "w") as zf:
info = zipfile.ZipInfo(name)
info.external_attr = (stat.S_IFLNK | 0o777) << 16 # 0xA1FF
zf.writestr(info, "../../etc/passwd")
return buf.getvalue()
# ================================================================ sanitize(#1/#2/#11)
class TestSanitize:
def test_normal_path_ok(self):
assert sanitize_entry_name("main.tex") == "main.tex"
assert sanitize_entry_name("figs/a.png") == "figs/a.png"
assert sanitize_entry_name("a/b/c/") == "a/b/c" # 目录尾斜杠归一
def test_path_traversal_rejected(self): # #1
with pytest.raises(ZipSafetyError) as e:
sanitize_entry_name("../../etc/passwd")
assert e.value.code == "ZIP_UNSAFE"
def test_backslash_traversal_rejected(self): # #2 Windows 变体
with pytest.raises(ZipSafetyError) as e:
sanitize_entry_name("..\\..\\evil.sh")
assert e.value.code == "ZIP_UNSAFE"
def test_absolute_path_rejected(self):
with pytest.raises(ZipSafetyError):
sanitize_entry_name("/etc/passwd")
with pytest.raises(ZipSafetyError):
sanitize_entry_name("C:\\Windows\\evil")
def test_control_char_rejected(self):
with pytest.raises(ZipSafetyError):
sanitize_entry_name("ma\x00in.tex")
def test_depth_limit(self): # #11
deep = "/".join(f"d{i}" for i in range(20)) + "/x.tex"
with pytest.raises(ZipSafetyError) as e:
sanitize_entry_name(deep)
assert e.value.code == "ZIP_UNSAFE"
def test_length_limit(self):
with pytest.raises(ZipSafetyError):
sanitize_entry_name("a" * 250 + ".tex")
# ================================================================ classify(#4/#6)
class TestClassify:
def test_allow(self):
for n in ("main.tex", "journal.cls", "refs.bib", "figs/a.png", "x.sty"):
assert classify_entry(n, False) == "allow"
assert classify_entry("figs/", True) == "allow"
def test_deny_dangerous(self): # #4
for n in ("run.sh", "evil.exe", "x.so", "x.dll", "a.py", "b.js"):
assert classify_entry(n, False) == "deny"
def test_skip_harmless_non_whitelist(self): # #6
for n in ("notes.docx", "old.log", "archive.zip", "img.gif"):
assert classify_entry(n, False) == "skip"
def test_skip_dirs(self): # #6 附属
assert classify_entry("node_modules/x.js", False) == "skip"
assert classify_entry(".git/config", False) == "skip"
assert classify_entry("__MACOSX/._a.tex", False) == "skip"
# ================================================================ safe_extract(#3/#5/#7-#10/#12/#13)
class TestSafeExtract:
def test_happy_path(self, tmp_path: Path):
zb = _make_zip({"main.tex": b"hi", "figs/a.png": b"\x89PNG"})
r = safe_extract(zb, tmp_path / "p", _settings())
assert isinstance(r, ExtractResult)
assert r.files == 2 and r.tex == 1 and r.images == 1
assert (tmp_path / "p" / "main.tex").read_text() == "hi"
assert (tmp_path / "p" / "figs" / "a.png").exists()
def test_symlink_rejected(self, tmp_path: Path): # #3
with pytest.raises(ZipSafetyError) as e:
safe_extract(_symlink_zip(), tmp_path / "p", _settings())
assert e.value.code == "ZIP_UNSAFE"
assert not (tmp_path / "p").exists() # 预扫拒绝 → 未写盘
def test_deny_ext_rejects_whole_zip(self, tmp_path: Path): # #4
zb = _make_zip({"main.tex": b"x", "run.sh": b"#!/bin/sh"})
with pytest.raises(ZipSafetyError) as e:
safe_extract(zb, tmp_path / "p", _settings())
assert e.value.code == "ZIP_UNSAFE" and "run.sh" in e.value.message
assert not (tmp_path / "p").exists()
def test_exec_bit_rejects(self, tmp_path: Path): # #5
zb = _make_zip({"main.tex": b"x"}, exec_bits={"main.tex": 0o100755})
with pytest.raises(ZipSafetyError) as e:
safe_extract(zb, tmp_path / "p", _settings())
assert e.value.code == "ZIP_UNSAFE"
def test_harmless_skipped_not_rejected(self, tmp_path: Path): # #6 决策1
zb = _make_zip({"main.tex": b"x", "notes.docx": b"DOC", "old.zip": b"PK"})
r = safe_extract(zb, tmp_path / "p", _settings())
assert r.files == 1
assert r.skipped_count == 2
assert {s.name for s in r.skipped} == {"notes.docx", "old.zip"}
assert not (tmp_path / "p" / "notes.docx").exists()
def test_ratio_bomb_rejected(self, tmp_path: Path): # #7
zb = _make_zip({"big.txt": b"\x00" * (2 * 1024 * 1024)}) # 全零 → ratio ≫100
with pytest.raises(ZipSafetyError) as e:
safe_extract(zb, tmp_path / "p", _settings())
assert e.value.code == "ZIP_BOMB"
def test_total_bytes_rejected_runtime(self, tmp_path: Path): # #8 声明上限
zb = _make_zip({"a.txt": b"x" * 5000, "b.txt": b"y" * 5000})
with pytest.raises(ZipSafetyError) as e:
safe_extract(zb, tmp_path / "p", _settings(max_extract_bytes=6000))
assert e.value.code == "ZIP_BOMB"
def test_entry_bytes_rejected(self, tmp_path: Path): # #9
zb = _make_zip({"a.txt": b"x" * 10000})
with pytest.raises(ZipSafetyError) as e:
safe_extract(zb, tmp_path / "p", _settings(max_zip_entry_bytes=1000))
assert e.value.code == "ZIP_BOMB"
def test_entry_count_rejected(self, tmp_path: Path): # #10
zb = _make_zip({f"f{i}.txt": b"x" for i in range(30)})
with pytest.raises(ZipSafetyError) as e:
safe_extract(zb, tmp_path / "p", _settings(max_zip_entries=10))
assert e.value.code == "ZIP_BOMB"
assert not (tmp_path / "p").exists() # 预扫先于写盘
def test_corrupt_zip(self, tmp_path: Path):
with pytest.raises(ZipSafetyError) as e:
safe_extract(b"not a zip at all", tmp_path / "p", _settings())
assert e.value.code == "ZIP_INVALID"
def test_traversal_entry_rejected_end_to_end(self, tmp_path: Path): # #1 端到端
zb = _make_zip({"../evil.txt": b"pwned"})
with pytest.raises(ZipSafetyError) as e:
safe_extract(zb, tmp_path / "p", _settings())
assert e.value.code == "ZIP_UNSAFE"
assert not (tmp_path / "evil.txt").exists() # 磁盘无残留
assert not (tmp_path / "p").exists()
+75
View File
@@ -0,0 +1,75 @@
"""auth.py —— 鉴权依赖(管理员 + 用户级)+ 鉴权异常。
- `get_current_user`:受 auth_enabled 开关控制;关=匿名(None),开=校验 token(401 失败)。
- `require_user`:强制登录(不受开关控制),用于 /api/auth/me、/logout 等必须登录的端点。
- `require_admin`:管理员;复用 require_user + role 校验。
- `require_authorization(unit)`:功能门禁;关=放行,开=admin 或 unit 在 authorizations 才放行。
"""
from __future__ import annotations
from fastapi import Depends, Request
from .config import get_settings
from .db import Database, get_db
from .logwriter import log_event
from .services.auth import resolve_token
class AuthError(Exception):
def __init__(self, status: int, code: str, message: str) -> None:
super().__init__(message)
self.status = status
self.code = code
self.message = message
def _bearer(request: Request) -> str:
header = request.headers.get("Authorization", "")
return header[7:] if header.startswith("Bearer ") else ""
async def get_current_user(request: Request, db: Database = Depends(get_db)) -> dict | None:
"""用户级鉴权;auth_enabled 为假时返回 None(匿名放行)。"""
if not get_settings().auth_enabled:
return None
token = _bearer(request)
user = await resolve_token(db, token) if token else None
if user is None:
log_event("call_denied", method=request.method, path=request.url.path, detail={"reason": "unauthorized"})
raise AuthError(401, "UNAUTHORIZED", "未登录或登录已过期")
request.state.user = user
return user
async def require_user(request: Request, db: Database = Depends(get_db)) -> dict:
"""强制登录(不受 auth_enabled 开关控制)。"""
token = _bearer(request)
user = await resolve_token(db, token) if token else None
if user is None:
log_event("call_denied", method=request.method, path=request.url.path, detail={"reason": "unauthorized"})
raise AuthError(401, "UNAUTHORIZED", "未登录或登录已过期")
request.state.user = user
return user
async def require_admin(request: Request, db: Database = Depends(get_db)) -> dict:
user = await require_user(request, db)
if user["role"] != "admin":
log_event("call_denied", user_id=user["id"], method=request.method, path=request.url.path, detail={"reason": "not_admin"})
raise AuthError(403, "FORBIDDEN", "非管理员账号")
return user
def require_authorization(unit: str):
"""功能门禁依赖工厂;auth_enabled 关=放行,开=admin 或 unit 在 authorizations 放行。"""
async def _dep(request: Request, db: Database = Depends(get_db)) -> dict | None:
if not get_settings().auth_enabled:
return None
user = await require_user(request, db)
if user["role"] != "admin" and unit not in (user.get("authorizations") or []):
log_event("call_denied", user_id=user["id"], method=request.method, path=request.url.path, detail={"reason": "forbidden", "unit": unit})
raise AuthError(403, "FORBIDDEN", "无权访问该功能")
return user
return _dep
+12
View File
@@ -86,6 +86,18 @@ class Settings(BaseSettings):
review_service_url: str = "http://127.0.0.1:8100"
review_read_timeout: float = 360.0 # presubmit 上传+LLM 长调用读超时(秒)
# ---- 管理后台(admin console + 行为日志)----
admin_seed_username: str = "admin" # 种子管理员用户名(启动时若无 admin 则创建)
admin_seed_password: str = "admin" # 种子管理员初始密码
admin_token_ttl_seconds: int = 86400 # 管理员会话凭据有效期(秒)
admin_dist_dir: str = "admin/dist" # 管理后台前端构建产物(BFF 托管 /admin)
log_dir: str = "data/logs" # 行为日志目录(每日一个 JSONL 文件)
log_retention_days: int = 0 # 日志保留天数(0=不清理)
# ---- 用户端鉴权(普通用户登录;AUTH_ENABLED 为假=匿名放行,兼容浸泡期)----
auth_enabled: bool = False
user_token_ttl_seconds: int = 86400 # 普通用户会话凭据有效期(秒)
# ---- 生产形态:BFF 静态托管 ui/dist(ARCHITECTURE.md §3,M6 挂载)----
frontend_dist_dir: str = "ui/dist" # 生产形态:静态托管 ui/dist
+42
View File
@@ -125,6 +125,35 @@ _DOCGEN_MIGRATIONS = (
)
# 管理后台/鉴权增量表(users + auth_tokens;行为日志走文件,不建表)
_ADMIN_SCHEMA = """
CREATE TABLE IF NOT EXISTS users (
id TEXT PRIMARY KEY,
username TEXT NOT NULL UNIQUE,
password_hash TEXT NOT NULL,
salt TEXT NOT NULL,
role TEXT NOT NULL DEFAULT 'user',
authorizations TEXT NOT NULL DEFAULT '[]',
created_at TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS auth_tokens (
token TEXT PRIMARY KEY,
user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
expires_at TEXT NOT NULL,
created_at TEXT NOT NULL,
revoked INTEGER NOT NULL DEFAULT 0
);
CREATE INDEX IF NOT EXISTS idx_auth_tokens_user ON auth_tokens(user_id);
CREATE TABLE IF NOT EXISTS settings (
key TEXT PRIMARY KEY,
value TEXT NOT NULL
);
"""
def _now_iso() -> str:
return datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
@@ -175,6 +204,19 @@ class Database:
)
except Exception:
logger.exception("docgen 表迁移失败(docgen 端点将不可用,其余不受影响)")
# 管理后台/鉴权增量表(users + auth_tokens):单独执行;失败只记日志
try:
await self._conn.executescript(_ADMIN_SCHEMA)
except Exception:
logger.exception("admin 表迁移失败(管理后台将不可用,其余不受影响)")
# sessions 加 owner_id(用户级数据隔离,幂等 ALTER)
try:
cur = await self._conn.execute("PRAGMA table_info(sessions)")
cols = {row[1] for row in await cur.fetchall()}
if "owner_id" not in cols:
await self._conn.execute("ALTER TABLE sessions ADD COLUMN owner_id TEXT")
except Exception:
logger.exception("sessions owner_id 迁移失败")
await self._conn.commit()
async def close(self) -> None:
+159
View File
@@ -0,0 +1,159 @@
"""logwriter.py —— 行为日志异步文件写入器(JSONL,固定目录,每日一个文件)。
设计:请求/业务只入队(不落盘),后台单任务批量 append,避免日志拖慢接口;
文件按日期命名 audit-YYYY-MM-DD.log,跨天自动切文件。写失败仅记 stderr,绝不影响业务。
"""
from __future__ import annotations
import asyncio
import json
import logging
from datetime import datetime, timezone
from pathlib import Path
from typing import Any
logger = logging.getLogger(__name__)
_writer: "LogWriter | None" = None # 全局单例
def _now_iso() -> str:
return datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3] + "Z"
def _today() -> str:
return datetime.now(timezone.utc).strftime("%Y-%m-%d")
class LogWriter:
def __init__(self, log_dir: str, retention_days: int = 0, hub_id: str = "") -> None:
self.log_dir = Path(log_dir)
self.retention_days = retention_days
self.hub_id = hub_id
self._queue: asyncio.Queue[dict[str, Any]] = asyncio.Queue(maxsize=20000)
self._task: asyncio.Task | None = None
self._date = ""
self._fh = None
def set_hub_id(self, hub_id: str) -> None:
self.hub_id = hub_id or ""
async def start(self) -> None:
self.log_dir.mkdir(parents=True, exist_ok=True)
self._task = asyncio.create_task(self._run())
async def stop(self) -> None:
if self._task is not None:
self._task.cancel()
try:
await self._task
except asyncio.CancelledError:
pass
self._task = None
self._close_fh()
def enqueue(self, entry: dict[str, Any]) -> None:
try:
self._queue.put_nowait(entry)
except asyncio.QueueFull:
logger.warning("行为日志队列已满,丢弃一条")
def _close_fh(self) -> None:
if self._fh is not None:
try:
self._fh.close()
except Exception:
pass
self._fh = None
def _open_for_date(self, date_str: str) -> None:
self._close_fh()
path = self.log_dir / f"audit-{date_str}.log"
self._fh = open(path, "a", encoding="utf-8")
self._date = date_str
self._cleanup()
async def _run(self) -> None:
try:
while True:
batch: list[dict[str, Any]] = []
try:
batch.append(await asyncio.wait_for(self._queue.get(), timeout=1.0))
except asyncio.TimeoutError:
continue
while len(batch) < 200:
try:
batch.append(self._queue.get_nowait())
except asyncio.QueueEmpty:
break
await asyncio.to_thread(self._write_sync, batch)
except asyncio.CancelledError:
raise
def _write_sync(self, entries: list[dict[str, Any]]) -> None:
today = _today()
if self._date != today:
self._open_for_date(today)
for e in entries:
self._fh.write(json.dumps(e, ensure_ascii=False) + "\n")
self._fh.flush()
def _cleanup(self) -> None:
if not self.retention_days:
return
try:
import time as _time
cutoff = _time.time() - self.retention_days * 86400
for p in self.log_dir.glob("audit-*.log"):
try:
if p.stat().st_mtime < cutoff:
p.unlink()
except OSError:
continue
except Exception:
logger.exception("日志清理失败")
def init_writer(log_dir: str, retention_days: int = 0, hub_id: str = "") -> None:
global _writer
_writer = LogWriter(log_dir, retention_days, hub_id=hub_id)
async def start_writer() -> None:
if _writer is not None:
await _writer.start()
async def stop_writer() -> None:
global _writer
if _writer is not None:
await _writer.stop()
_writer = None
def log_event(
event: str,
*,
user_id: str | None = None,
method: str | None = None,
path: str | None = None,
status: int | None = None,
latency_ms: float | None = None,
detail: dict[str, Any] | None = None,
) -> None:
"""通用业务埋点;writer 未初始化时静默 no-op。"""
if _writer is None:
return
entry = {
"ts": _now_iso(),
"hub_id": _writer.hub_id,
"user_id": user_id,
"event": event,
"method": method,
"path": path,
"status": status,
"latency_ms": latency_ms,
"detail": detail or {},
}
_writer.enqueue(entry)
+57 -14
View File
@@ -11,10 +11,11 @@
from __future__ import annotations
import logging
import time
from contextlib import asynccontextmanager
from pathlib import Path
from fastapi import FastAPI, Request
from fastapi import Depends, FastAPI, Request
from fastapi.exceptions import RequestValidationError
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
@@ -27,6 +28,12 @@ from .routers import bioflow_proxy
from .routers import paper
from .routers import private_docs
from .routers import review_proxy
from .routers import admin
from .routers import auth
from . import logwriter
from .auth import AuthError, get_current_user, require_authorization
from .services.auth import seed_admin
from .services.settings import get_hub_settings
from .upstream import close_client
logging.basicConfig(level=logging.INFO)
@@ -49,22 +56,49 @@ def create_app() -> FastAPI:
allow_headers=["*"],
)
# 行为日志请求中间件:自动采集所有 /api/* 请求(user_id 待鉴权上线后补)
@app.middleware("http")
async def _log_requests(request: Request, call_next):
start = time.monotonic()
response = await call_next(request)
if request.url.path.startswith("/api/"):
user = getattr(request.state, "user", None)
logwriter.log_event(
"http_request",
user_id=user["id"] if user else None,
method=request.method,
path=request.url.path,
status=response.status_code,
latency_ms=round((time.monotonic() - start) * 1000, 1),
)
return response
# 鉴权异常 → 统一错误信封
@app.exception_handler(AuthError)
async def _auth_error_handler(_: Request, exc: AuthError) -> JSONResponse:
return JSONResponse(
status_code=exc.status,
content={"ok": False, "error": {"code": exc.code, "message": exc.message, "degraded": False}},
)
app.include_router(health.router)
app.include_router(sessions.router)
app.include_router(qa.router)
app.include_router(capabilities.router)
app.include_router(kg.router)
app.include_router(extraction.router)
app.include_router(figures.router)
app.include_router(legacy.router)
app.include_router(docgen.router) # docgen(P2)
app.include_router(private_docs.router) # 私有资料问答(FR-PRIV-004)
app.include_router(auth.router) # /api/auth/*(login/status 免鉴权,me/logout 需登录)
app.include_router(sessions.router, dependencies=[Depends(get_current_user)])
app.include_router(qa.router, dependencies=[Depends(require_authorization("qa"))])
app.include_router(capabilities.router, dependencies=[Depends(get_current_user)])
app.include_router(kg.router, dependencies=[Depends(require_authorization("kg"))])
app.include_router(extraction.router, dependencies=[Depends(require_authorization("extraction"))])
app.include_router(figures.router, dependencies=[Depends(require_authorization("figures"))])
app.include_router(legacy.router, dependencies=[Depends(require_authorization("legacy"))])
app.include_router(docgen.router, dependencies=[Depends(require_authorization("docgen"))]) # docgen(P2)
app.include_router(private_docs.router, dependencies=[Depends(require_authorization("private_docs"))]) # 私有资料问答(FR-PRIV-004)
if get_settings().bioflow_enabled:
app.include_router(bioflow_proxy.router) # 生信反代(BIOFLOW_ENABLED 开关,FR-BIO-028)
app.include_router(bioflow_proxy.router, dependencies=[Depends(require_authorization("bioflow"))]) # 生信反代(BIOFLOW_ENABLED 开关,FR-BIO-028)
if get_settings().paper_writer_enabled:
app.include_router(paper.router) # 论文撰写(PAPER_WRITER_ENABLED 开关)
app.include_router(paper.router, dependencies=[Depends(require_authorization("paper"))]) # 论文撰写(PAPER_WRITER_ENABLED 开关)
if get_settings().review_enabled:
app.include_router(review_proxy.router) # 科研审稿反代(REVIEW_ENABLED 开关)
app.include_router(review_proxy.router, dependencies=[Depends(get_current_user)]) # 科研审稿反代(REVIEW_ENABLED 开关)
app.include_router(admin.router) # 管理后台(/api/admin/*)
@app.exception_handler(RequestValidationError)
async def _validation_handler(_: Request, exc: RequestValidationError) -> JSONResponse:
@@ -79,10 +113,15 @@ def create_app() -> FastAPI:
@asynccontextmanager
async def _lifespan(_app: FastAPI):
await init_db()
_db = await init_db()
await seed_admin(_db)
hub = await get_hub_settings(_db)
logwriter.init_writer(settings.log_dir, settings.log_retention_days, hub_id=hub["hub_id"])
await logwriter.start_writer()
try:
yield
finally:
await logwriter.stop_writer()
await close_client()
try:
from .services.neo4j import close_neo4j # A3 提供;缺失则跳过
@@ -98,6 +137,10 @@ def create_app() -> FastAPI:
# 2026-08-25 PM 修复:dist 路径必须基于项目根解析(BFF 工作目录是 sdk/,
# 相对路径会解析成 sdk/ui/dist);sync.sh 排除 dist,需单独同步。
project_root = Path(__file__).resolve().parent.parent.parent
# 管理后台静态托管(/admin;须在 "/" 之前挂载)
admin_dist = project_root / settings.admin_dist_dir if not Path(settings.admin_dist_dir).is_absolute() else Path(settings.admin_dist_dir)
if admin_dist.is_dir():
app.mount("/admin", StaticFiles(directory=str(admin_dist), html=True), name="admin")
dist = project_root / settings.frontend_dist_dir if not Path(settings.frontend_dist_dir).is_absolute() else Path(settings.frontend_dist_dir)
if dist.is_dir():
app.mount("/", StaticFiles(directory=str(dist), html=True), name="frontend")
+168
View File
@@ -0,0 +1,168 @@
"""routers/admin.py —— 管理后台端点(登录/用户管理/日志查看)。"""
from __future__ import annotations
from fastapi import APIRouter, Depends, Query, Request
from fastapi.responses import JSONResponse
from pydantic import BaseModel
from ..auth import require_admin
from ..config import get_settings
from ..db import Database, get_db
from ..logwriter import log_event
from .. import logwriter
from ..services.auth import (
AUTHORIZATION_UNITS,
verify_login,
issue_token,
revoke_token,
list_users,
get_user_by_username,
create_user,
update_user,
delete_user,
)
from ..services.logview import query_logs
from ..services.settings import get_hub_settings, set_hub_settings
from ..upstream import envelope
router = APIRouter(prefix="/api/admin", tags=["admin"])
class LoginBody(BaseModel):
username: str
password: str
class UserCreate(BaseModel):
username: str
password: str
role: str = "user"
authorizations: list[str] = []
class UserPatch(BaseModel):
role: str | None = None
authorizations: list[str] | None = None
password: str | None = None
class HubSettingsBody(BaseModel):
hub_id: str | None = None
hub_name: str | None = None
@router.post("/login")
async def login(body: LoginBody, db: Database = Depends(get_db)):
user = await verify_login(db, body.username, body.password)
if user is None:
log_event("admin_login_fail", detail={"username": body.username})
content, status = envelope("UNAUTHORIZED", "用户名或密码错误", status=401)
return JSONResponse(status_code=status, content=content)
if user["role"] != "admin":
log_event("admin_login_fail", detail={"username": body.username, "reason": "not_admin"})
content, status = envelope("FORBIDDEN", "非管理员账号", status=403)
return JSONResponse(status_code=status, content=content)
token = await issue_token(db, user["id"])
log_event("admin_login_ok", user_id=user["id"], detail={"username": body.username})
return {"ok": True, "token": token, "user": user}
@router.post("/logout")
async def logout(request: Request, db: Database = Depends(get_db), _user: dict = Depends(require_admin)):
header = request.headers.get("Authorization", "")
token = header[7:] if header.startswith("Bearer ") else ""
if token:
await revoke_token(db, token)
return {"ok": True}
@router.get("/me")
async def me(_user: dict = Depends(require_admin)):
return {"ok": True, "user": _user}
@router.get("/units")
async def units(_user: dict = Depends(require_admin)):
return {"ok": True, "units": list(AUTHORIZATION_UNITS)}
@router.get("/settings")
async def get_settings_api(db: Database = Depends(get_db), _user: dict = Depends(require_admin)):
return {"ok": True, "settings": await get_hub_settings(db)}
@router.put("/settings")
async def put_settings_api(body: HubSettingsBody, db: Database = Depends(get_db), _user: dict = Depends(require_admin)):
settings = await set_hub_settings(db, body.hub_id, body.hub_name)
logwriter.set_hub_id(settings["hub_id"])
return {"ok": True, "settings": settings}
@router.get("/users")
async def users(db: Database = Depends(get_db), _user: dict = Depends(require_admin)):
return {"ok": True, "users": await list_users(db)}
@router.post("/users")
async def create_user_api(body: UserCreate, db: Database = Depends(get_db), _user: dict = Depends(require_admin)):
username = body.username.strip()
if not username:
content, _ = envelope("VALIDATION", "用户名不能为空", status=422)
return JSONResponse(status_code=422, content=content)
if not body.password:
content, _ = envelope("VALIDATION", "密码不能为空", status=422)
return JSONResponse(status_code=422, content=content)
if await get_user_by_username(db, username):
return JSONResponse(
status_code=409,
content={"ok": False, "error": {"code": "CONFLICT", "message": "用户名已存在", "degraded": False}},
)
user = await create_user(db, username, body.password, body.role, body.authorizations)
return {"ok": True, "user": user}
@router.patch("/users/{user_id}")
async def patch_user(user_id: str, body: UserPatch, db: Database = Depends(get_db), _user: dict = Depends(require_admin)):
user = await update_user(db, user_id, role=body.role, authorizations=body.authorizations, password=body.password)
if user is None:
content, _ = envelope("NOT_FOUND", status=404)
return JSONResponse(status_code=404, content=content)
return {"ok": True, "user": user}
@router.delete("/users/{user_id}")
async def delete_user_api(user_id: str, db: Database = Depends(get_db), _user: dict = Depends(require_admin)):
if user_id == _user["id"]:
return JSONResponse(
status_code=422,
content={"ok": False, "error": {"code": "VALIDATION", "message": "不能删除当前登录账号", "degraded": False}},
)
await delete_user(db, user_id)
return {"ok": True}
@router.get("/logs")
async def logs(
from_date: str | None = Query(None),
to_date: str | None = Query(None),
event: str | None = Query(None),
user_id: str | None = Query(None),
q: str | None = Query(None),
status: str | None = Query(None),
page: int = Query(1, ge=1),
page_size: int = Query(50, ge=1, le=200),
_user: dict = Depends(require_admin),
):
settings = get_settings()
result = await query_logs(
settings.log_dir,
from_date=from_date,
to_date=to_date,
event=event,
user_id=user_id,
q=q,
status=status,
page=page,
page_size=page_size,
)
return {"ok": True, **result}
+52
View File
@@ -0,0 +1,52 @@
"""routers/auth.py —— 用户端鉴权端点(登录/登出/me/status)。"""
from __future__ import annotations
from fastapi import APIRouter, Depends, Request
from fastapi.responses import JSONResponse
from pydantic import BaseModel
from ..auth import require_user
from ..config import get_settings
from ..db import Database, get_db
from ..logwriter import log_event
from ..services.auth import verify_login, issue_token, revoke_token
from ..upstream import envelope
router = APIRouter(prefix="/api/auth", tags=["auth"])
class LoginBody(BaseModel):
username: str
password: str
@router.get("/status")
async def status():
"""免鉴权:告知前端是否开启鉴权(决定是否显示登录页)。"""
return {"ok": True, "auth_enabled": get_settings().auth_enabled}
@router.post("/login")
async def login(body: LoginBody, db: Database = Depends(get_db)):
user = await verify_login(db, body.username, body.password)
if user is None:
log_event("login_fail", detail={"username": body.username})
content, status = envelope("UNAUTHORIZED", "用户名或密码错误", status=401)
return JSONResponse(status_code=status, content=content)
token = await issue_token(db, user["id"], ttl_seconds=get_settings().user_token_ttl_seconds)
log_event("login_ok", user_id=user["id"], detail={"username": body.username})
return {"ok": True, "token": token, "user": user}
@router.post("/logout")
async def logout(request: Request, db: Database = Depends(get_db), _user: dict = Depends(require_user)):
header = request.headers.get("Authorization", "")
token = header[7:] if header.startswith("Bearer ") else ""
if token:
await revoke_token(db, token)
return {"ok": True}
@router.get("/me")
async def me(_user: dict = Depends(require_user)):
return {"ok": True, "user": _user}
+6
View File
@@ -138,6 +138,12 @@ async def register_report(request: Request, db: Database = Depends(get_db)) -> A
import uuid
data = payload.get("data", payload) if isinstance(payload, dict) else {}
sid = str(data.get("session_id") or "")
user = getattr(request.state, "user", None)
if user and sid:
owner_row = await db.fetchone("SELECT owner_id FROM sessions WHERE id = ?", (sid,))
if owner_row is None or owner_row["owner_id"] != user["id"]:
return proxied # 归属不符:不落库,仍透传生信响应
artifact_id = uuid.uuid4().hex[:16]
await db.execute(
"""INSERT INTO doc_artifacts (id, session_id, status, title, template_name,
+6 -1
View File
@@ -36,7 +36,12 @@ async def list_capabilities(
session_caps: Optional[dict] = None
if session_id:
try:
row = await db.fetchone("SELECT meta FROM sessions WHERE id = ?", (session_id,))
user = getattr(request.state, "user", None)
owner_id = user["id"] if user else None
if owner_id is None:
row = await db.fetchone("SELECT meta FROM sessions WHERE id = ?", (session_id,))
else:
row = await db.fetchone("SELECT meta FROM sessions WHERE id = ? AND owner_id = ?", (session_id, owner_id))
if row is not None:
meta = loads_json(row["meta"], default={})
if isinstance(meta, dict):
+22 -1
View File
@@ -49,7 +49,7 @@ from datetime import datetime, timezone
from pathlib import Path
from typing import Any
from fastapi import APIRouter, Depends, File, Form, Query, Response, UploadFile
from fastapi import APIRouter, Depends, File, Form, Query, Request, Response, UploadFile
from fastapi.responses import FileResponse, JSONResponse
from ..config import get_settings
@@ -109,6 +109,13 @@ def _envelope(code: str, message: str | None = None, degraded: bool | None = Non
return JSONResponse(status_code=http, content=body)
async def _session_owned(db: Database, session_id: str, owner_id: str | None) -> bool:
if owner_id is None:
return True
row = await db.fetchone("SELECT owner_id FROM sessions WHERE id = ?", (session_id,))
return row is not None and row["owner_id"] == owner_id
def _validation(message: str) -> JSONResponse:
return _envelope("VALIDATION", message=message, status=422)
@@ -646,6 +653,7 @@ async def _run_docgen_task(
@router.post("/generate")
async def generate_doc(
payload: DocgenGenerateRequest,
request: Request,
db: Database = Depends(get_db),
):
"""异步生成(缺陷 #52,2026-09-10):快路径校验 → 插任务行 → 后台生成 → 202。
@@ -657,6 +665,9 @@ async def generate_doc(
202 {ok, task_id, status:"queued"}(契约见 docs/acceptance-r3.md 附录 B)。
chat 消息落库由前端走现有 sessions 机制,本端点不碰。
"""
user = getattr(request.state, "user", None)
if user and payload.session_id and not await _session_owned(db, payload.session_id, user["id"]):
return _envelope("NOT_FOUND", message="会话不存在", status=404)
bad = _check_description(payload.description)
if bad is not None:
return bad
@@ -767,15 +778,25 @@ async def get_docgen_task(task_id: str, db: Database = Depends(get_db)):
@router.get("/tasks")
async def list_docgen_tasks(
request: Request,
session_id: str = Query("", description="按会话过滤;空=全部(上限 100)"),
db: Database = Depends(get_db),
):
"""会话任务列表(缺陷 #52 契约,附录 B)。"""
user = getattr(request.state, "user", None)
if user and session_id and not await _session_owned(db, session_id, user["id"]):
return _envelope("NOT_FOUND", message="会话不存在", status=404)
if session_id:
rows = await db.fetchall(
"SELECT id FROM docgen_tasks WHERE session_id = ? ORDER BY created_at DESC LIMIT 100",
(session_id,),
)
elif user:
rows = await db.fetchall(
"SELECT t.id FROM docgen_tasks t JOIN sessions s ON s.id = t.session_id "
"WHERE s.owner_id = ? ORDER BY t.created_at DESC LIMIT 100",
(user["id"],),
)
else:
rows = await db.fetchall(
"SELECT id FROM docgen_tasks ORDER BY created_at DESC LIMIT 100"
+22 -4
View File
@@ -9,13 +9,15 @@ import asyncio
from typing import Any
import httpx
from fastapi import APIRouter, Depends
from fastapi import APIRouter, Depends, Request
from fastapi.responses import JSONResponse
from pydantic import BaseModel
from ..config import get_settings
from ..db import Database, get_db
from ..services.paper import agent
from ..services.paper.mcp_client import PaperMcpClient
from ..upstream import envelope
router = APIRouter(prefix="/api/paper", tags=["paper"])
@@ -36,6 +38,13 @@ def _client() -> PaperMcpClient:
return PaperMcpClient(s.paper_writer_url, s.paper_writer_read_timeout)
async def _session_owned(db: Database, session_id: str, owner_id: str | None) -> bool:
if owner_id is None:
return True
row = await db.fetchone("SELECT owner_id FROM sessions WHERE id = ?", (session_id,))
return row is not None and row["owner_id"] == owner_id
@router.get("/health")
async def paper_health() -> dict[str, Any]:
"""探活反代 8097 /health;离线返回 offline 标记(前端据此隐藏 paper 入口)。"""
@@ -50,19 +59,28 @@ async def paper_health() -> dict[str, Any]:
@router.post("/turns")
async def paper_turn(payload: PaperTurnRequest, db: Database = Depends(get_db)) -> dict[str, Any]:
async def paper_turn(payload: PaperTurnRequest, request: Request, db: Database = Depends(get_db)) -> dict[str, Any]:
user = getattr(request.state, "user", None)
if user and not await _session_owned(db, payload.session_id, user["id"]):
return JSONResponse(status_code=404, content=envelope("NOT_FOUND", message="会话不存在", status=404)[0])
blocks = await agent.handle_turn(db, payload.session_id, payload.text, payload.paper_id, payload.template_key)
return {"ok": True, "blocks": blocks}
@router.post("/papers/{paper_id}/confirm")
async def paper_confirm(paper_id: str, payload: PaperConfirmRequest, db: Database = Depends(get_db)) -> dict[str, Any]:
async def paper_confirm(paper_id: str, payload: PaperConfirmRequest, request: Request, db: Database = Depends(get_db)) -> dict[str, Any]:
user = getattr(request.state, "user", None)
if user and not await _session_owned(db, payload.session_id, user["id"]):
return JSONResponse(status_code=404, content=envelope("NOT_FOUND", message="会话不存在", status=404)[0])
asyncio.create_task(agent.run_writing_loop(db, paper_id, payload.session_id))
return {"ok": True, "paper_id": paper_id}
@router.post("/papers/{paper_id}/resume")
async def paper_resume(paper_id: str, payload: PaperConfirmRequest, db: Database = Depends(get_db)) -> dict[str, Any]:
async def paper_resume(paper_id: str, payload: PaperConfirmRequest, request: Request, db: Database = Depends(get_db)) -> dict[str, Any]:
user = getattr(request.state, "user", None)
if user and not await _session_owned(db, payload.session_id, user["id"]):
return JSONResponse(status_code=404, content=envelope("NOT_FOUND", message="会话不存在", status=404)[0])
asyncio.create_task(agent.run_writing_loop(db, paper_id, payload.session_id))
return {"ok": True, "paper_id": paper_id}
+20 -8
View File
@@ -100,13 +100,14 @@ async def _proxy(
retry_invalid_citation: bool,
mode: str,
normalizer=None,
owner_id: str | None = None,
) -> Any:
"""非流式上游代理:重试、错误信封、归一化。返回 (JSONResponse)。"""
client = sse_relay.get_qa_client()
normalizer = normalizer or sse_relay.get_qa_normalizer()
url = f"{sse_relay.qa_base_url()}{_upstream_path(endpoint)}"
timeout = _read_timeout(read_timeout)
json_body, _humanize = await _inject_history(body)
json_body, _humanize = await _inject_history(body, owner_id=owner_id)
for attempt in (1, 2):
try:
@@ -206,8 +207,10 @@ async def qa_info():
async def qa_chat(request: Request):
"""非流式问答:上游 /chat + 归一化 + invalid_llm_citation 重试一次。"""
body = await request.json()
user = getattr(request.state, "user", None)
owner_id = user["id"] if user else None
return await _proxy(
"chat", body, read_timeout=CHAT_READ_TIMEOUT, retry_invalid_citation=True, mode="chat"
"chat", body, read_timeout=CHAT_READ_TIMEOUT, retry_invalid_citation=True, mode="chat", owner_id=owner_id
)
@@ -224,7 +227,9 @@ async def qa_chat_stream(request: Request):
except Exception:
return _envelope("VALIDATION", 422)
evidence_first = bool(body.pop("evidence_first", False))
body, humanize_on = await _inject_history(body)
user = getattr(request.state, "user", None)
owner_id = user["id"] if user else None
body, humanize_on = await _inject_history(body, owner_id=owner_id)
return StreamingResponse(
sse_relay.relay_stream(body, evidence_first=evidence_first, humanize_enabled=humanize_on),
media_type="text/event-stream",
@@ -242,8 +247,10 @@ async def qa_search(request: Request):
"""
body = await request.json()
body = {**body, "use_llm": False, "native_chain_enabled": False}
user = getattr(request.state, "user", None)
owner_id = user["id"] if user else None
return await _proxy(
"search", body, read_timeout=SEARCH_READ_TIMEOUT, retry_invalid_citation=False, mode="search"
"search", body, read_timeout=SEARCH_READ_TIMEOUT, retry_invalid_citation=False, mode="search", owner_id=owner_id
)
@@ -251,7 +258,7 @@ async def qa_search(request: Request):
# 多轮记忆(S2):剥离 session_id → 组装 history 注入上游
# ---------------------------------------------------------------------------
async def _inject_history(body: Optional[dict]) -> tuple[dict, bool]:
async def _inject_history(body: Optional[dict], owner_id: str | None = None) -> tuple[dict, bool]:
"""剥离 BFF 契约字段 session_id,读会话 meta 注入 history/capabilities。
- 不传 session_id / 会话不存在 → 行为与现役一致(history/capabilities 缺省)
@@ -270,9 +277,14 @@ async def _inject_history(body: Optional[dict]) -> tuple[dict, bool]:
if body.get("history"):
return body, False # 显式传入的历史优先(上游直传场景;capabilities 由请求体自带)
try:
row = await bff_db.fetchone(
"SELECT meta FROM sessions WHERE id = ?", (session_id,)
)
if owner_id is None:
row = await bff_db.fetchone(
"SELECT meta FROM sessions WHERE id = ?", (session_id,)
)
else:
row = await bff_db.fetchone(
"SELECT meta FROM sessions WHERE id = ? AND owner_id = ?", (session_id, owner_id)
)
if row is None:
return body, False
meta = loads_json(row["meta"], default={})
+47 -28
View File
@@ -8,7 +8,8 @@
- GET /api/sessions/{id}/sources —— 全会话来源聚合:按 paper_id 去重(无 paper_id 按 id),
带 count(出现次数)。
404 → NOT_FOUND 信封;时间 ISO 8601 UTC。
用户级数据隔离:auth_enabled 开启时按 sessions.owner_id 过滤/校验(get_current_user 注入当前用户;
匿名模式下 user=None 则不过滤,兼容旧形态)。404 → NOT_FOUND 信封;时间 ISO 8601 UTC。
"""
from __future__ import annotations
@@ -20,6 +21,7 @@ from typing import Any
from fastapi import APIRouter, Depends, Response, status as http_status
from fastapi.responses import JSONResponse
from ..auth import get_current_user
from ..db import Database, dumps_json, get_db, loads_json
from ..schemas import (
MessageCreate,
@@ -80,10 +82,14 @@ def _row_to_message(row: Any) -> MessageOut:
async def _get_session_or_none(
db: Database, session_id: str, *, with_count: bool = True
db: Database, session_id: str, *, with_count: bool = True, owner_id: str | None = None
) -> SessionOut | None:
sql = _SESSION_SELECT + " WHERE s.id = ?"
row = await db.fetchone(sql, (session_id,))
params: list[Any] = [session_id]
if owner_id is not None:
sql += " AND s.owner_id = ?"
params.append(owner_id)
row = await db.fetchone(sql, tuple(params))
return _row_to_session(row) if row else None
@@ -101,31 +107,40 @@ async def _get_message_or_none(
# ---------------------------------------------------------------------------
@router.get("", response_model=list[SessionOut])
async def list_sessions(db: Database = Depends(get_db)) -> list[SessionOut]:
rows = await db.fetchall(_SESSION_SELECT + " ORDER BY s.updated_at DESC")
async def list_sessions(db: Database = Depends(get_db), user: dict | None = Depends(get_current_user)) -> list[SessionOut]:
owner_id = user["id"] if user else None
sql = _SESSION_SELECT
params: list[Any] = []
if owner_id is not None:
sql += " WHERE s.owner_id = ?"
params.append(owner_id)
sql += " ORDER BY s.updated_at DESC"
rows = await db.fetchall(sql, tuple(params))
return [_row_to_session(r) for r in rows]
@router.post("", response_model=SessionOut, status_code=http_status.HTTP_201_CREATED)
async def create_session(
payload: SessionCreate, db: Database = Depends(get_db)
payload: SessionCreate, db: Database = Depends(get_db), user: dict | None = Depends(get_current_user)
) -> SessionOut:
now = _db_now()
session_id = str(uuid.uuid4())
title = payload.normalized_title()
owner_id = user["id"] if user else None
await db.execute(
"INSERT INTO sessions (id, title, status, meta, created_at, updated_at) "
"VALUES (?, ?, 'idle', ?, ?, ?)",
(session_id, title, dumps_json({"title_manual": False}), now, now),
"INSERT INTO sessions (id, title, status, meta, owner_id, created_at, updated_at) "
"VALUES (?, ?, 'idle', ?, ?, ?, ?)",
(session_id, title, dumps_json({"title_manual": False}), owner_id, now, now),
)
out = await _get_session_or_none(db, session_id)
out = await _get_session_or_none(db, session_id, owner_id=owner_id)
assert out is not None # 刚插入必存在
return out
@router.get("/{session_id}", response_model=SessionDetail)
async def get_session(session_id: str, db: Database = Depends(get_db)):
session = await _get_session_or_none(db, session_id)
async def get_session(session_id: str, db: Database = Depends(get_db), user: dict | None = Depends(get_current_user)):
owner_id = user["id"] if user else None
session = await _get_session_or_none(db, session_id, owner_id=owner_id)
if session is None:
return _not_found(session_id)
rows = await db.fetchall(
@@ -136,9 +151,10 @@ async def get_session(session_id: str, db: Database = Depends(get_db)):
@router.patch("/{session_id}", response_model=SessionOut)
async def patch_session(
session_id: str, payload: SessionPatch, db: Database = Depends(get_db)
session_id: str, payload: SessionPatch, db: Database = Depends(get_db), user: dict | None = Depends(get_current_user)
):
session = await _get_session_or_none(db, session_id)
owner_id = user["id"] if user else None
session = await _get_session_or_none(db, session_id, owner_id=owner_id)
if session is None:
return _not_found(session_id)
@@ -169,14 +185,15 @@ async def patch_session(
(dumps_json(meta), _db_now(), session_id),
)
out = await _get_session_or_none(db, session_id)
out = await _get_session_or_none(db, session_id, owner_id=owner_id)
assert out is not None
return out
@router.delete("/{session_id}", status_code=http_status.HTTP_204_NO_CONTENT)
async def delete_session(session_id: str, db: Database = Depends(get_db)) -> Response:
session = await _get_session_or_none(db, session_id)
async def delete_session(session_id: str, db: Database = Depends(get_db), user: dict | None = Depends(get_current_user)) -> Response:
owner_id = user["id"] if user else None
session = await _get_session_or_none(db, session_id, owner_id=owner_id)
if session is None:
return _not_found_response(session_id)
await db.execute("DELETE FROM sessions WHERE id = ?", (session_id,))
@@ -189,9 +206,10 @@ async def delete_session(session_id: str, db: Database = Depends(get_db)) -> Res
@router.post("/{session_id}/messages", response_model=MessageOut, status_code=201)
async def create_message(
session_id: str, payload: MessageCreate, db: Database = Depends(get_db)
session_id: str, payload: MessageCreate, db: Database = Depends(get_db), user: dict | None = Depends(get_current_user)
):
session = await _get_session_or_none(db, session_id)
owner_id = user["id"] if user else None
session = await _get_session_or_none(db, session_id, owner_id=owner_id)
if session is None:
return _not_found(session_id)
@@ -206,7 +224,6 @@ async def create_message(
(message_id, session_id, payload.kind, text, now, now),
)
else:
# 助手流式骨架:text 置空,blocks 落库初始块
blocks = [b.model_dump() for b in payload.blocks]
await db.execute(
"INSERT INTO messages (id, session_id, role, kind, status, blocks, raw, text, "
@@ -215,7 +232,6 @@ async def create_message(
dumps_json(blocks), now, now),
)
# 消息活动 → 会话 updated_at 前移、idle → running
await db.execute(
"UPDATE sessions SET status = CASE WHEN status = 'idle' THEN 'running' ELSE status END, "
"updated_at = ? WHERE id = ?",
@@ -233,7 +249,13 @@ async def patch_message(
message_id: str,
payload: MessagePatch,
db: Database = Depends(get_db),
user: dict | None = Depends(get_current_user),
):
owner_id = user["id"] if user else None
session = await _get_session_or_none(db, session_id, owner_id=owner_id)
if session is None:
return _not_found(session_id)
message = await _get_message_or_none(db, session_id, message_id)
if message is None:
return _not_found(session_id, message_id)
@@ -258,7 +280,6 @@ async def patch_message(
await db.execute(
f"UPDATE messages SET {', '.join(sets)} WHERE id = ?", params
)
# 同步前移会话 updated_at
await db.execute(
"UPDATE sessions SET updated_at = ? WHERE id = ?",
(_db_now(), session_id),
@@ -274,12 +295,10 @@ async def patch_message(
# ---------------------------------------------------------------------------
@router.get("/{session_id}/sources")
async def get_session_sources(session_id: str, db: Database = Depends(get_db)):
"""按 paper_id 去重(无 paper_id 时按 id),带 count(出现次数)。
来源来自该会话全部助手消息 blocks 里的 sources 块(§2 块联合)。
"""
session = await _get_session_or_none(db, session_id)
async def get_session_sources(session_id: str, db: Database = Depends(get_db), user: dict | None = Depends(get_current_user)):
"""按 paper_id 去重(无 paper_id 时按 id),带 count(出现次数)。"""
owner_id = user["id"] if user else None
session = await _get_session_or_none(db, session_id, owner_id=owner_id)
if session is None:
return _not_found(session_id)
+190
View File
@@ -0,0 +1,190 @@
"""services/auth.py —— 管理员鉴权:密码哈希、会话凭据、用户 CRUD、种子管理员。
密码用 stdlib pbkdf2_hmac + 每用户随机盐(不新增依赖);
会话凭据用不透明 token 存 auth_tokens,逐次查库(吊销/过期即时生效)。
所有 DB 访问经 `db` 参数注入(与 services/history.py 一致,避免依赖全局单例)。
"""
from __future__ import annotations
import hashlib
import logging
import secrets
import uuid
from datetime import datetime, timedelta, timezone
from typing import Any
from ..config import get_settings
from ..db import Database, dumps_json, loads_json
from ..logwriter import log_event
logger = logging.getLogger(__name__)
# 授权清单功能单元(9 项,与管理后台界面一致;后续可调)
AUTHORIZATION_UNITS = (
"qa", "kg", "extraction", "docgen", "figures",
"private_docs", "bioflow", "paper", "legacy",
)
_PBKDF2_ITERATIONS = 200_000
def _now_iso() -> str:
return datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
def hash_password(password: str, salt: str | None = None) -> tuple[str, str]:
salt = salt or secrets.token_hex(16)
digest = hashlib.pbkdf2_hmac(
"sha256", password.encode("utf-8"), salt.encode("utf-8"), _PBKDF2_ITERATIONS
)
return digest.hex(), salt
def verify_password(password: str, salt: str, expected_hex: str) -> bool:
digest, _ = hash_password(password, salt)
return secrets.compare_digest(digest, expected_hex)
def _row_to_user(row: Any) -> dict[str, Any]:
return {
"id": row["id"],
"username": row["username"],
"role": row["role"],
"authorizations": loads_json(row["authorizations"], default=[]),
"created_at": row["created_at"],
}
# ---- 登录校验 ----
async def verify_login(db: Database, username: str, password: str) -> dict[str, Any] | None:
row = await db.fetchone(
"SELECT id, username, password_hash, salt, role, authorizations, created_at "
"FROM users WHERE username = ?",
(username,),
)
if row is None:
return None
if not verify_password(password, row["salt"], row["password_hash"]):
return None
return _row_to_user(row)
# ---- 会话凭据 ----
async def issue_token(db: Database, user_id: str, ttl_seconds: int | None = None) -> str:
settings = get_settings()
ttl = ttl_seconds if ttl_seconds is not None else settings.admin_token_ttl_seconds
token = secrets.token_urlsafe(32)
now = _now_iso()
expires = datetime.now(timezone.utc) + timedelta(seconds=ttl)
await db.execute(
"INSERT INTO auth_tokens (token, user_id, expires_at, created_at, revoked) "
"VALUES (?, ?, ?, ?, 0)",
(token, user_id, expires.strftime("%Y-%m-%dT%H:%M:%SZ"), now),
)
return token
async def resolve_token(db: Database, token: str) -> dict[str, Any] | None:
row = await db.fetchone(
"SELECT t.expires_at, t.revoked, u.id, u.username, u.role, u.authorizations, u.created_at "
"FROM auth_tokens t JOIN users u ON u.id = t.user_id WHERE t.token = ?",
(token,),
)
if row is None or row["revoked"]:
return None
try:
expires = datetime.strptime(row["expires_at"], "%Y-%m-%dT%H:%M:%SZ").replace(tzinfo=timezone.utc)
except ValueError:
return None
if datetime.now(timezone.utc) >= expires:
return None
return _row_to_user(row)
async def revoke_token(db: Database, token: str) -> None:
await db.execute("UPDATE auth_tokens SET revoked = 1 WHERE token = ?", (token,))
# ---- 用户 CRUD ----
async def list_users(db: Database) -> list[dict[str, Any]]:
rows = await db.fetchall(
"SELECT id, username, role, authorizations, created_at FROM users ORDER BY created_at ASC"
)
return [_row_to_user(r) for r in rows]
async def get_user(db: Database, user_id: str) -> dict[str, Any] | None:
row = await db.fetchone(
"SELECT id, username, role, authorizations, created_at FROM users WHERE id = ?", (user_id,)
)
return _row_to_user(row) if row else None
async def get_user_by_username(db: Database, username: str) -> dict[str, Any] | None:
row = await db.fetchone(
"SELECT id, username, role, authorizations, created_at FROM users WHERE username = ?", (username,)
)
return _row_to_user(row) if row else None
async def create_user(
db: Database, username: str, password: str, role: str, authorizations: list[str]
) -> dict[str, Any]:
user_id = str(uuid.uuid4())
ph, salt = hash_password(password)
await db.execute(
"INSERT INTO users (id, username, password_hash, salt, role, authorizations, created_at) "
"VALUES (?, ?, ?, ?, ?, ?, ?)",
(user_id, username, ph, salt, role, dumps_json(authorizations), _now_iso()),
)
log_event("user_created", detail={"target": username, "role": role})
return {"id": user_id, "username": username, "role": role, "authorizations": authorizations, "created_at": _now_iso()}
async def update_user(
db: Database,
user_id: str,
*,
role: str | None = None,
authorizations: list[str] | None = None,
password: str | None = None,
) -> dict[str, Any] | None:
fields: list[str] = []
params: list[Any] = []
if role is not None:
fields.append("role = ?")
params.append(role)
if authorizations is not None:
fields.append("authorizations = ?")
params.append(dumps_json(authorizations))
if password is not None:
ph, salt = hash_password(password)
fields.append("password_hash = ?")
params.append(ph)
fields.append("salt = ?")
params.append(salt)
if fields:
params.append(user_id)
await db.execute(f"UPDATE users SET {', '.join(fields)} WHERE id = ?", params)
log_event("user_updated", detail={"target_id": user_id, "role": role, "password_reset": password is not None})
return await get_user(db, user_id)
async def delete_user(db: Database, user_id: str) -> None:
await db.execute("DELETE FROM users WHERE id = ?", (user_id,))
log_event("user_deleted", detail={"target_id": user_id})
# ---- 种子管理员 ----
async def seed_admin(db: Database) -> None:
"""启动时确保存在至少一个 admin 用户(默认 admin/admin)。"""
row = await db.fetchone("SELECT id FROM users WHERE role = 'admin' LIMIT 1")
if row is not None:
return
settings = get_settings()
await create_user(db, settings.admin_seed_username, settings.admin_seed_password, "admin", list(AUTHORIZATION_UNITS))
logger.info("已创建种子管理员 %s", settings.admin_seed_username)
+100
View File
@@ -0,0 +1,100 @@
"""services/logview.py —— 读取行为日志文件(JSONL)+ 内存聚合,供管理后台日志查看。"""
from __future__ import annotations
import asyncio
import json
from pathlib import Path
from typing import Any
def _select_files(log_dir: Path, from_date: str | None, to_date: str | None) -> list[Path]:
"""按日期范围选出 audit-YYYY-MM-DD.log(升序)。"""
if not log_dir.is_dir():
return []
out: list[Path] = []
for p in sorted(log_dir.glob("audit-*.log")):
name = p.name
if not (name.startswith("audit-") and name.endswith(".log")):
continue
stem = name[len("audit-"):-len(".log")]
if from_date and stem < from_date:
continue
if to_date and stem > to_date:
continue
out.append(p)
return out
def _read_lines(path: Path) -> list[dict[str, Any]]:
try:
with open(path, "r", encoding="utf-8") as f:
text = f.read()
except (FileNotFoundError, OSError):
return []
out: list[dict[str, Any]] = []
for line in text.splitlines():
line = line.strip()
if not line:
continue
try:
out.append(json.loads(line))
except (json.JSONDecodeError, TypeError):
continue
return out
def _match(e: dict[str, Any], event, user_id, q, status) -> bool:
if event and e.get("event") != event:
return False
if user_id and e.get("user_id") != user_id:
return False
if status is not None and str(e.get("status")) != str(status):
return False
if q:
q = str(q)
if q not in str(e.get("path") or "") and q not in str(e.get("user_id") or ""):
return False
return True
async def query_logs(
log_dir: str,
*,
from_date: str | None = None,
to_date: str | None = None,
event: str | None = None,
user_id: str | None = None,
q: str | None = None,
status: str | None = None,
page: int = 1,
page_size: int = 50,
) -> dict[str, Any]:
files = _select_files(Path(log_dir), from_date, to_date)
# 文件 I/O 放线程池,不阻塞事件循环
all_lines = await asyncio.to_thread(lambda: [ln for p in files for ln in _read_lines(p)])
items = [e for e in all_lines if _match(e, event, user_id, q, status)]
items.sort(key=lambda e: str(e.get("ts") or ""), reverse=True)
total = len(items)
by_status: dict[str, int] = {}
latencies: list[float] = []
denied = 0
for e in items:
st = e.get("status")
if st is not None:
key = str(st)
by_status[key] = by_status.get(key, 0) + 1
if e.get("event") in ("call_denied", "login_fail", "admin_login_fail"):
denied += 1
lm = e.get("latency_ms")
if isinstance(lm, (int, float)):
latencies.append(float(lm))
latencies.sort()
start = (page - 1) * page_size
aggregates = {
"by_status": by_status,
"denied": denied,
"avg_latency_ms": round(sum(latencies) / len(latencies), 1) if latencies else None,
"p95_latency_ms": latencies[int(len(latencies) * 0.95)] if latencies else None,
}
return {"items": items[start:start + page_size], "total": total, "aggregates": aggregates}
+40
View File
@@ -0,0 +1,40 @@
"""services/settings.py —— 中枢级键值设置(hub_id / hub_name)。"""
from __future__ import annotations
import uuid
from ..db import Database
_HUB_ID_KEY = "hub_id"
_HUB_NAME_KEY = "hub_name"
DEFAULT_HUB_NAME = "蜂群中枢"
async def get_setting(db: Database, key: str, default: str | None = None) -> str | None:
row = await db.fetchone("SELECT value FROM settings WHERE key = ?", (key,))
return row["value"] if row else default
async def set_setting(db: Database, key: str, value: str) -> None:
row = await db.fetchone("SELECT 1 FROM settings WHERE key = ?", (key,))
if row is not None:
await db.execute("UPDATE settings SET value = ? WHERE key = ?", (value, key))
else:
await db.execute("INSERT INTO settings (key, value) VALUES (?, ?)", (key, value))
async def get_hub_settings(db: Database) -> dict:
hub_id = await get_setting(db, _HUB_ID_KEY)
if not hub_id:
hub_id = str(uuid.uuid4())
await set_setting(db, _HUB_ID_KEY, hub_id)
hub_name = (await get_setting(db, _HUB_NAME_KEY, DEFAULT_HUB_NAME)) or DEFAULT_HUB_NAME
return {"hub_id": hub_id, "hub_name": hub_name}
async def set_hub_settings(db: Database, hub_id: str | None, hub_name: str | None) -> dict:
if hub_id and hub_id.strip():
await set_setting(db, _HUB_ID_KEY, hub_id.strip())
if hub_name and hub_name.strip():
await set_setting(db, _HUB_NAME_KEY, hub_name.strip())
return await get_hub_settings(db)
@@ -0,0 +1,178 @@
// tests/frontend/unit/edu/reformat.compile.test.js —— ReformatPanel 编译 PDF Tab(P3b)
// 1) 无 job_id → "请先完成重排版再编译" 引导
// 2) 有 job_id → "编译 PDF" 按钮可点
// 3) 点击后按钮变 "编译中…"(loading 期间禁用;store 状态驱动,切面板不中断)
// 4) 成功状态:iframe 预览 + 下载按钮 + 耗时
// 5) 失败状态:错误摘要 + log 尾折叠 <pre> + 查看完整 log
// 6) QUEUE_FULL → 固定文案"编译队列已满(最多 5 个任务)"
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { mount } from '@vue/test-utils';
import { createPinia, setActivePinia } from 'pinia';
import ReformatPanel from '../../../../ui/src/components/edu/panels/ReformatPanel.vue';
const compileTexMock = vi.fn();
vi.mock('../../../../ui/src/api/review.js', async (importOriginal) => {
const actual = await importOriginal(); // 保留真实 reviewErrorMessage(QUEUE_FULL 文案走 CODE_HINTS)
return {
...actual,
getJournals: vi.fn().mockResolvedValue({ ok: true, items: [] }),
getDirectory: vi.fn(),
reformatTex: vi.fn(),
compileTex: (...args) => compileTexMock(...args),
};
});
import { useEduStore } from '../../../../ui/src/stores/edu.js';
import { ApiError } from '../../../../ui/src/api/http.js';
function seedResult(edu, job_id = 'abc123def456') {
edu.reformatResult = {
ok: true,
job_id,
modified_tex: '\\documentclass{article}\\begin{document}Hi\\end{document}',
changes: [],
warnings: [],
meta: { filename: 'paper.tex', journal_name: 'Plant Cell', rules_source: 'official' },
};
edu.reformatStatus = 'ready';
}
async function mountPdfTab(edu) {
const pinia = createPinia();
setActivePinia(pinia);
edu = useEduStore();
const wrapper = mount(ReformatPanel, { global: { plugins: [pinia] } });
// 切到"编译 PDF" Tab
const tabs = wrapper.findAll('.rf-tab');
const pdfTab = tabs.find((t) => t.text() === '编译 PDF');
await pdfTab.trigger('click');
return { wrapper, edu };
}
describe('ReformatPanel 编译 PDF Tab(P3b)', () => {
beforeEach(() => {
compileTexMock.mockReset();
// jsdom 无 URL.createObjectURL:store blob 加载走 catch 回退,不致挂
if (!URL.createObjectURL) URL.createObjectURL = vi.fn(() => 'blob:mock');
if (!URL.revokeObjectURL) URL.revokeObjectURL = vi.fn();
global.fetch = vi.fn().mockRejectedValue(new Error('no network'));
});
it('1) 无 job_id → "请先完成重排版再编译" 引导', async () => {
const pinia = createPinia();
setActivePinia(pinia);
const edu = useEduStore();
edu.reformatResult = { ok: true, job_id: null, changes: [], meta: {} };
edu.reformatStatus = 'ready';
const wrapper = mount(ReformatPanel, { global: { plugins: [pinia] } });
await wrapper.findAll('.rf-tab').find((t) => t.text() === '编译 PDF').trigger('click');
const guide = wrapper.find('[data-testid="rf-compile-guide"]');
expect(guide.exists()).toBe(true);
expect(guide.text()).toContain('请先完成重排版再编译');
expect(wrapper.find('[data-testid="rf-compile-btn"]').exists()).toBe(false);
});
it('2) 有 job_id → "编译 PDF" 按钮可点', async () => {
const pinia = createPinia();
setActivePinia(pinia);
const edu = useEduStore();
seedResult(edu);
const wrapper = mount(ReformatPanel, { global: { plugins: [pinia] } });
await wrapper.findAll('.rf-tab').find((t) => t.text() === '编译 PDF').trigger('click');
const btn = wrapper.find('[data-testid="rf-compile-btn"]');
expect(btn.exists()).toBe(true);
expect(btn.text()).toBe('编译 PDF');
expect(btn.attributes('disabled')).toBeUndefined();
});
it('3) 点击后按钮变"编译中…"并禁用;首次编译黄条出现', async () => {
const pinia = createPinia();
setActivePinia(pinia);
const edu = useEduStore();
seedResult(edu);
compileTexMock.mockReturnValue(new Promise(() => {})); // 挂起,模拟编译中
const wrapper = mount(ReformatPanel, { global: { plugins: [pinia] } });
await wrapper.findAll('.rf-tab').find((t) => t.text() === '编译 PDF').trigger('click');
await wrapper.find('[data-testid="rf-compile-btn"]').trigger('click');
await wrapper.vm.$nextTick();
const btn = wrapper.find('[data-testid="rf-compile-btn"]');
expect(btn.text()).toContain('编译中');
expect(btn.attributes('disabled')).toBeDefined();
expect(wrapper.find('[data-testid="rf-compile-loading"]').exists()).toBe(true);
expect(wrapper.find('[data-testid="rf-first-hint"]').exists()).toBe(true);
expect(compileTexMock).toHaveBeenCalledTimes(1);
// loading 期间再点不重复提交
await wrapper.find('[data-testid="rf-compile-btn"]').trigger('click');
expect(compileTexMock).toHaveBeenCalledTimes(1);
});
it('4) 成功状态:iframe 预览 + 下载按钮 + 耗时', async () => {
const pinia = createPinia();
setActivePinia(pinia);
const edu = useEduStore();
seedResult(edu);
edu.compileResult = {
ok: true, compile_id: 'abc123def456',
pdf_url: '/api/review/compile/abc123def456/pdf',
log_url: '/api/review/compile/abc123def456/log',
meta: { seconds: 12.34, pdf_bytes: 1048576, engine: 'tectonic' },
};
edu.compileStatus = 'ready';
edu.compilePdfUrl = ''; // blob 失败回退直链
const wrapper = mount(ReformatPanel, { global: { plugins: [pinia] } });
await wrapper.findAll('.rf-tab').find((t) => t.text() === '编译 PDF').trigger('click');
await wrapper.vm.$nextTick();
const frame = wrapper.find('[data-testid="rf-pdf-frame"]');
expect(frame.exists()).toBe(true);
expect(frame.attributes('src')).toContain('/pdf');
expect(wrapper.find('[data-testid="rf-download-pdf"]').exists()).toBe(true);
expect(wrapper.find('[data-testid="rf-pdf-size"]').text()).toContain('MB');
expect(wrapper.find('[data-testid="rf-compile-secs"]').text()).toContain('12.3 秒');
expect(wrapper.find('[data-testid="rf-recompile"]').exists()).toBe(true);
expect(wrapper.find('[data-testid="rf-compile-btn"]').exists()).toBe(false); // 操作行换成下载/重编
});
it('5) 失败状态:错误摘要 + log 尾折叠区 + 完整 log 按钮', async () => {
const pinia = createPinia();
setActivePinia(pinia);
const edu = useEduStore();
seedResult(edu);
edu.compileStatus = 'error';
edu.compileError = 'LaTeX 编译失败:缺少宏包';
edu.compileLogTail = 'This is tectonic\n! LaTeX Error: File `foo.sty\' not found.\n68 lines omitted...';
const wrapper = mount(ReformatPanel, { global: { plugins: [pinia] } });
await wrapper.findAll('.rf-tab').find((t) => t.text() === '编译 PDF').trigger('click');
await wrapper.vm.$nextTick();
const err = wrapper.find('[data-testid="rf-compile-error"]');
expect(err.exists()).toBe(true);
expect(err.text()).toContain('缺少宏包');
const details = wrapper.find('[data-testid="rf-log-details"]');
expect(details.exists()).toBe(true);
expect(wrapper.find('[data-testid="rf-log-body"]').text()).toContain('foo.sty');
expect(wrapper.find('[data-testid="rf-download-log"]').exists()).toBe(true);
expect(wrapper.find('[data-testid="rf-recompile"]').exists()).toBe(true);
expect(wrapper.find('[data-testid="rf-pdf-frame"]').exists()).toBe(false);
});
it('6) QUEUE_FULL → 固定文案"编译队列已满(最多 5 个任务)"', async () => {
const pinia = createPinia();
setActivePinia(pinia);
const edu = useEduStore();
seedResult(edu);
compileTexMock.mockRejectedValue(new ApiError('编译队列已满(7 个任务排队),请稍后重试', {
code: 'QUEUE_FULL', status: 429,
}));
const wrapper = mount(ReformatPanel, { global: { plugins: [pinia] } });
await wrapper.findAll('.rf-tab').find((t) => t.text() === '编译 PDF').trigger('click');
await wrapper.find('[data-testid="rf-compile-btn"]').trigger('click');
await wrapper.vm.$nextTick();
await wrapper.vm.$nextTick();
const err = wrapper.find('[data-testid="rf-compile-error"]');
expect(err.exists()).toBe(true);
expect(err.text()).toContain('编译队列已满(最多 5 个任务),请稍后重试');
// 报错后按钮立即恢复可点(重新编译)
const retry = wrapper.find('[data-testid="rf-recompile"]');
expect(retry.exists()).toBe(true);
expect(retry.attributes('disabled')).toBeUndefined();
});
});
@@ -0,0 +1,99 @@
// tests/frontend/unit/edu/reformat.file.test.js —— ReformatPanel 文件持久化(P3b 修复)
// bug:上传 .tex → 重排版 → 点"重新排版"换期刊 → 本地 file 被清空 → 要求重传。
// 修复:File 进 store(reformatFile,同 presubmitFile 模式);reset() 不清 file;
// 仅"×" removeFile 清空。
// 1) store 有 reformatFile → 挂载即回显文件名
// 2) 选择文件 → 切换目标期刊 → 文件仍在(store + DOM)
// 3) 有结果时点"重新排版"(reset)→ 回上传区且文件保留
// 4) "×" 移除 → store + 本地都清空
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { mount } from '@vue/test-utils';
import { createPinia, setActivePinia } from 'pinia';
import ReformatPanel from '../../../../ui/src/components/edu/panels/ReformatPanel.vue';
vi.mock('../../../../ui/src/api/review.js', async (importOriginal) => {
const actual = await importOriginal();
return {
...actual,
getJournals: vi.fn().mockResolvedValue({
ok: true,
items: [{ id: 'gb', name: 'Genome Biology' }, { id: 'mp', name: 'Molecular Plant' }],
}),
getDirectory: vi.fn(),
reformatTex: vi.fn(),
compileTex: vi.fn(),
};
});
import { useEduStore } from '../../../../ui/src/stores/edu.js';
const TEX = new File(['\\documentclass{article}\\begin{document}Hi\\end{document}'], 'paper.tex', {
type: 'application/x-tex',
});
/** 先建 pinia → seed store → 再挂载(保证 mount 时读到 store 初值) */
function setup(seedFn) {
const pinia = createPinia();
setActivePinia(pinia);
const edu = useEduStore();
if (seedFn) seedFn(edu);
const wrapper = mount(ReformatPanel, { global: { plugins: [pinia] } });
return { wrapper, edu };
}
describe('ReformatPanel 文件持久化(换期刊不丢文件)', () => {
beforeEach(() => {
global.fetch = vi.fn().mockRejectedValue(new Error('no network'));
if (!URL.createObjectURL) URL.createObjectURL = vi.fn(() => 'blob:mock');
if (!URL.revokeObjectURL) URL.revokeObjectURL = vi.fn();
});
it('1) store 有 reformatFile → 挂载即回显文件名', async () => {
const { wrapper } = setup((edu) => { edu.reformatFile = TEX; });
await wrapper.vm.$nextTick();
expect(wrapper.find('.rf-file-name').text()).toContain('paper.tex');
});
it('2) 选择文件后切目标期刊 → 文件仍在(store + DOM)', async () => {
const { wrapper, edu } = setup();
const input = wrapper.find('input.rf-file-input');
Object.defineProperty(input.element, 'files', { value: [TEX], configurable: true });
await input.trigger('change');
await wrapper.vm.$nextTick();
expect(edu.reformatFile).toBe(TEX);
expect(wrapper.find('.rf-file-name').text()).toContain('paper.tex');
// 切换目标期刊(watch 只写 drafts,不清 file)
await wrapper.find('#rf-target').setValue('mp');
await wrapper.vm.$nextTick();
expect(edu.reformatFile).toBe(TEX);
expect(wrapper.find('.rf-file-name').text()).toContain('paper.tex');
expect(edu.drafts.reformatTarget).toBe('mp');
});
it('3) 有结果时点"重新排版" → 回上传区且文件保留', async () => {
const { wrapper, edu } = setup((e) => {
e.reformatFile = TEX;
e.reformatResult = {
ok: true, job_id: 'abc123def456', modified_tex: 'x', changes: [], warnings: [],
meta: { filename: 'paper.tex' },
};
e.reformatStatus = 'ready';
});
await wrapper.vm.$nextTick();
const again = wrapper.findAll('button').find((b) => b.text() === '重新排版');
expect(again).toBeTruthy();
await again.trigger('click');
await wrapper.vm.$nextTick();
expect(edu.reformatResult).toBeNull();
expect(edu.reformatFile).toBe(TEX); // store 保留
expect(wrapper.find('.rf-file-name').text()).toContain('paper.tex'); // DOM 回显
});
it('4) "×" 移除 → store + 本地都清空', async () => {
const { wrapper, edu } = setup((e) => { e.reformatFile = TEX; });
await wrapper.vm.$nextTick();
await wrapper.find('.rf-remove').trigger('click');
expect(edu.reformatFile).toBeNull();
expect(wrapper.find('.rf-file-name').exists()).toBe(false);
});
});
@@ -0,0 +1,123 @@
// tests/frontend/unit/edu/reformat.rules-summary.test.js
// 目标期刊规则摘要行(data-testid="rf-rules-summary"):
// 1) 有规则字段 + 有 diff → 摘要 + "已修改 N 处"
// 2) 规则全 null + diff=0 → "无强制约束" + "样本已满足该刊要求,无需修改"
// 背景:GB/BMC PB/Rice 规则相同、Frontiers 几乎无规则,用户切换期刊看不到 diff 差异,
// 即使 diff=0 也要展示"该刊要求什么",消除"没生效"错觉。
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { mount } from '@vue/test-utils';
import { createPinia, setActivePinia } from 'pinia';
import ReformatPanel from '../../../../ui/src/components/edu/panels/ReformatPanel.vue';
vi.mock('../../../../ui/src/api/review.js', async (importOriginal) => {
const actual = await importOriginal();
return {
...actual,
getJournals: vi.fn().mockResolvedValue({ ok: true, items: [] }),
getDirectory: vi.fn(),
reformatTex: vi.fn(),
compileTex: vi.fn(),
};
});
import { useEduStore } from '../../../../ui/src/stores/edu.js';
function mountWithResult(eduSeed) {
const pinia = createPinia();
setActivePinia(pinia);
const edu = useEduStore();
Object.assign(edu, eduSeed);
const wrapper = mount(ReformatPanel, { global: { plugins: [pinia] } });
return { wrapper, edu };
}
function resultWith(rules, changes) {
return {
ok: true,
job_id: 'abc123def456',
modified_tex: 'x',
changes,
warnings: [],
meta: {
filename: 'paper.tex',
journal_name: 'Molecular Plant',
rules_source: 'official',
rules_note: 'journals.json 期刊专属规则',
rules,
},
};
}
describe('ReformatPanel 目标期刊规则摘要', () => {
beforeEach(() => {
global.fetch = vi.fn().mockRejectedValue(new Error('no network'));
if (!URL.createObjectURL) URL.createObjectURL = vi.fn(() => 'blob:mock');
if (!URL.revokeObjectURL) URL.revokeObjectURL = vi.fn();
});
it('1) 有规则字段 + 有 diff → 摘要列出字段且标注"已修改 N 处"', async () => {
// Molecular Plant 真实规则形态:12pt / 1.5 / author_year(font/margin null 不显示)
const { wrapper } = mountWithResult({
reformatStatus: 'ready',
reformatResult: resultWith(
{
min_font_pt: 12, min_margin_mm: null, min_line_spacing: 1.5,
refs_style: 'author_year', max_pages: null, min_figures: null, max_fonts: null,
},
[
{ line: 1, original: 'a', modified: 'b', reason: 'r', rule_key: 'font_pt', action: 'modified' },
{ line: 3, original: 'a', modified: 'b', reason: 'r', rule_key: 'line_spacing', action: 'modified' },
{ line: 4, original: 'a', modified: 'b', reason: 'r', rule_key: 'bib_style', action: 'modified' },
],
),
});
await wrapper.vm.$nextTick();
const el = wrapper.find('[data-testid="rf-rules-summary"]');
expect(el.exists()).toBe(true);
const text = el.text();
expect(text).toContain('1.5 倍行距');
expect(text).toContain('作者-年份引用');
expect(text).toContain('字号 ≥12pt');
expect(text).not.toContain('null');
expect(text).not.toContain('边距'); // min_margin_mm=null → 不显示
expect(text).toContain('已修改 3 处');
});
it('2) 规则全 null + diff=0 → "无强制约束" + "样本已满足该刊要求,无需修改"', async () => {
const { wrapper } = mountWithResult({
reformatStatus: 'ready',
reformatResult: resultWith(
{
min_font_pt: null, min_margin_mm: null, min_line_spacing: null,
refs_style: null, max_pages: null, min_figures: null, max_fonts: null,
},
[],
),
});
await wrapper.vm.$nextTick();
const el = wrapper.find('[data-testid="rf-rules-summary"]');
expect(el.exists()).toBe(true);
const text = el.text();
expect(text).toContain('该刊官网未规定明确格式要求,无强制约束');
expect(text).toContain('样本已满足该刊要求,无需修改');
expect(text).not.toContain('已修改');
});
it('3) 双倍行距 + Vancouver(GB/BMC/Rice 形态)→ 正确映射中文标签', async () => {
const { wrapper } = mountWithResult({
reformatStatus: 'ready',
reformatResult: resultWith(
{
min_font_pt: null, min_margin_mm: null, min_line_spacing: 2.0,
refs_style: 'vancouver', max_pages: null, min_figures: null, max_fonts: null,
},
[{ line: 3, original: 'a', modified: 'b', reason: 'r', rule_key: 'line_spacing', action: 'modified' }],
),
});
await wrapper.vm.$nextTick();
const text = wrapper.find('[data-testid="rf-rules-summary"]').text();
expect(text).toContain('双倍行距');
expect(text).toContain('Vancouver 引用');
expect(text).toContain('已修改 1 处');
});
});
@@ -0,0 +1,188 @@
// tests/frontend/unit/edu/reformat.zip.test.js —— ReformatPanel 项目 zip 两段式(P4)
// 1) 默认 .tex 模式;切"项目 .zip"→ zip 上传区 + 打包建议文案 + inspect 按钮
// 2) 选 .zip → inspect 成功 → 摘要(main/统计/跳过)+ 编译按钮
// 3) inspect 失败(ZIP_UNSAFE 后端原话)→ 错误展示
// 4) 点编译 → loading → 成功:iframe 预览 + 耗时
// 5) 编译失败:错误摘要 + log 折叠区 + 查看完整 log(失败信封带 log_url)
// 6) 模式互斥:切回 .tex 清 zip 状态
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { mount } from '@vue/test-utils';
import { createPinia, setActivePinia } from 'pinia';
import ReformatPanel from '../../../../ui/src/components/edu/panels/ReformatPanel.vue';
const inspectZipMock = vi.fn();
const runZipCompileMock = vi.fn();
vi.mock('../../../../ui/src/api/review.js', async (importOriginal) => {
const actual = await importOriginal();
return {
...actual,
getJournals: vi.fn().mockResolvedValue({ ok: true, items: [] }),
getDirectory: vi.fn(),
reformatTex: vi.fn(),
compileTex: vi.fn(),
inspectZip: (...args) => inspectZipMock(...args),
runZipCompile: (...args) => runZipCompileMock(...args),
};
});
import { useEduStore } from '../../../../ui/src/stores/edu.js';
import { ApiError } from '../../../../ui/src/api/http.js';
const INSPECT_OK = {
ok: true,
meta: {
job_id: 'abc123def456',
main: 'main.tex',
stats: { files: 5, tex: 1, images: 1, cls: 1, bib: 1, other: 1, bytes: 4096 },
skipped: [{ name: '__MACOSX/._main.tex', reason: 'skip_dir' }],
skipped_count: 1,
created_at: '2026-10-13T00:00:00+00:00',
},
};
function zipFile(name = 'project.zip') {
return new File([new Uint8Array([0x50, 0x4b, 0x03, 0x04])], name, {
type: 'application/zip',
});
}
async function mountZipPanel(edu) {
const pinia = createPinia();
setActivePinia(pinia);
edu = useEduStore();
const wrapper = mount(ReformatPanel, { global: { plugins: [pinia] } });
await wrapper.find('[data-testid="rf-mode-zip"]').trigger('click');
return { wrapper, edu };
}
async function pickZip(wrapper, edu, file = zipFile()) {
const input = wrapper.find('input[type="file"]');
Object.defineProperty(input.element, 'files', { value: [file], configurable: true });
await input.trigger('change');
await wrapper.vm.$nextTick();
}
describe('ReformatPanel 项目 zip 两段式(P4)', () => {
beforeEach(() => {
inspectZipMock.mockReset();
runZipCompileMock.mockReset();
if (!URL.createObjectURL) URL.createObjectURL = vi.fn(() => 'blob:mock');
if (!URL.revokeObjectURL) URL.revokeObjectURL = vi.fn();
global.fetch = vi.fn().mockRejectedValue(new Error('no network'));
});
it('1) 默认 .tex 模式;切 zip → zip 上传区 + 打包建议 + inspect 按钮', async () => {
const pinia = createPinia();
setActivePinia(pinia);
const wrapper = mount(ReformatPanel, { global: { plugins: [pinia] } });
expect(wrapper.find('[data-testid="rf-mode-tex"]').classes()).toContain('active');
expect(wrapper.find('[data-testid="rf-zip-inspect-btn"]').exists()).toBe(false);
// tex 提交按钮仍在
expect(wrapper.findAll('button').some((b) => b.text() === '开始自动重排版')).toBe(true);
await wrapper.find('[data-testid="rf-mode-zip"]').trigger('click');
const zipBtn = wrapper.find('[data-testid="rf-zip-inspect-btn"]');
expect(zipBtn.exists()).toBe(true);
expect(zipBtn.attributes('disabled')).toBeDefined(); // 未选文件不可点
expect(wrapper.text()).toContain('打包建议:main.tex 放 zip 根目录');
// tex 提交按钮消失(互斥)
expect(wrapper.findAll('button').some((b) => b.text() === '开始自动重排版')).toBe(false);
});
it('2) 选 .zip → inspect 成功 → 摘要 + 编译按钮', async () => {
inspectZipMock.mockResolvedValue(INSPECT_OK);
const { wrapper, edu } = await mountZipPanel(null);
await pickZip(wrapper, edu);
await wrapper.find('[data-testid="rf-zip-inspect-btn"]').trigger('click');
await vi.waitFor(() => {
expect(wrapper.find('[data-testid="rf-zip-meta"]').exists()).toBe(true);
});
expect(inspectZipMock).toHaveBeenCalledTimes(1);
const meta = wrapper.find('[data-testid="rf-zip-meta"]');
expect(meta.text()).toContain('main.tex');
expect(meta.text()).toContain('文件 5 个');
expect(meta.text()).toContain('1 个文件已跳过');
expect(wrapper.find('[data-testid="rf-zip-skipped"]').text()).toContain('__MACOSX');
expect(wrapper.find('[data-testid="rf-zip-compile-btn"]').exists()).toBe(true);
});
it('3) inspect 失败(ZIP_UNSAFE)→ 错误展示', async () => {
inspectZipMock.mockRejectedValue(
new ApiError('zip 含危险条目:evil.sh', { code: 'ZIP_UNSAFE', status: 200 }),
);
const { wrapper, edu } = await mountZipPanel(null);
await pickZip(wrapper, edu);
await wrapper.find('[data-testid="rf-zip-inspect-btn"]').trigger('click');
await vi.waitFor(() => {
expect(wrapper.find('[data-testid="rf-zip-error"]').exists()).toBe(true);
});
expect(wrapper.find('[data-testid="rf-zip-error"]').text()).toContain('evil.sh');
expect(wrapper.find('[data-testid="rf-zip-meta"]').exists()).toBe(false);
});
it('4) 编译 → loading → 成功:iframe 预览 + 耗时', async () => {
inspectZipMock.mockResolvedValue(INSPECT_OK);
let resolveRun;
runZipCompileMock.mockReturnValue(new Promise((r) => { resolveRun = r; }));
const { wrapper, edu } = await mountZipPanel(null);
await pickZip(wrapper, edu);
await wrapper.find('[data-testid="rf-zip-inspect-btn"]').trigger('click');
await vi.waitFor(() => {
expect(wrapper.find('[data-testid="rf-zip-compile-btn"]').exists()).toBe(true);
});
await wrapper.find('[data-testid="rf-zip-compile-btn"]').trigger('click');
await wrapper.vm.$nextTick();
expect(wrapper.find('[data-testid="rf-compile-loading"]').exists()).toBe(true);
resolveRun({
ok: true,
compile_id: 'aabbccddeeff',
pdf_url: '/api/review/compile/zip/aabbccddeeff/pdf',
log_url: '/api/review/compile/zip/aabbccddeeff/log',
meta: { seconds: 3.2, pdf_bytes: 2048, project: true },
});
await vi.waitFor(() => {
expect(wrapper.find('[data-testid="rf-pdf-frame"]').exists()).toBe(true);
});
expect(wrapper.find('[data-testid="rf-compile-secs"]').text()).toContain('3.2 秒');
expect(wrapper.find('[data-testid="rf-download-pdf"]').exists()).toBe(true);
});
it('5) 编译失败:错误摘要 + log 折叠区 + 查看完整 log', async () => {
inspectZipMock.mockResolvedValue(INSPECT_OK);
const compileErr = new ApiError('LaTeX 源码有语法错误', {
code: 'COMPILE_FAILED', status: 200,
});
compileErr.detail = 'l.12 \\badcmd'; // unwrap 从 error.detail 挂载(log 尾)
runZipCompileMock.mockRejectedValue(compileErr);
const { wrapper, edu } = await mountZipPanel(null);
await pickZip(wrapper, edu);
await wrapper.find('[data-testid="rf-zip-inspect-btn"]').trigger('click');
await vi.waitFor(() => {
expect(wrapper.find('[data-testid="rf-zip-compile-btn"]').exists()).toBe(true);
});
await wrapper.find('[data-testid="rf-zip-compile-btn"]').trigger('click');
await vi.waitFor(() => {
expect(wrapper.find('[data-testid="rf-compile-error"]').exists()).toBe(true);
});
expect(wrapper.find('[data-testid="rf-compile-error"]').text()).toContain('语法错误');
expect(wrapper.find('[data-testid="rf-log-body"]').text()).toContain('l.12');
expect(wrapper.find('[data-testid="rf-download-log"]').exists()).toBe(true);
});
it('6) 模式互斥:切回 .tex 清 zip 状态(inspect 摘要消失)', async () => {
inspectZipMock.mockResolvedValue(INSPECT_OK);
const { wrapper, edu } = await mountZipPanel(null);
await pickZip(wrapper, edu);
await wrapper.find('[data-testid="rf-zip-inspect-btn"]').trigger('click');
await vi.waitFor(() => {
expect(wrapper.find('[data-testid="rf-zip-meta"]').exists()).toBe(true);
});
await wrapper.find('[data-testid="rf-mode-tex"]').trigger('click');
expect(wrapper.find('[data-testid="rf-zip-meta"]').exists()).toBe(false);
expect(edu.zipInspect).toBeNull();
expect(edu.zipFile).toBeNull();
// tex 上传区回来了
expect(wrapper.findAll('button').some((b) => b.text() === '开始自动重排版')).toBe(true);
});
});
+23 -1
View File
@@ -19,6 +19,9 @@ function unwrap(promise) {
degraded: !!payload.error.degraded,
});
if (payload.error.detail) err.detail = payload.error.detail; // 编译失败的 log 尾等
// P4:项目编译失败信封带 compile_id + log_url(补充 3,前端"查看完整 log"用)
if (payload.compile_id) err.compileId = payload.compile_id;
if (payload.log_url) err.logUrl = payload.log_url;
throw err;
}
return payload;
@@ -104,6 +107,23 @@ export function compileTex(formData, opts = {}) {
return unwrap(post(`${BASE}/compile`, formData, opts));
}
/** P4 POST /api/review/compile/inspect(multipart .zip ≤50MB,两段式之①)
* → {ok, meta:{job_id, main, stats:{files,tex,images,cls,bib,other,bytes},
* skipped:[{name,reason}], skipped_count, created_at}}
* 错误信封:VALIDATION / ZIP_UNSAFE / ZIP_BOMB / ZIP_INVALID(HTTP 200)。
* job_id 1 小时 TTL;run 前不占编译队列。 */
export function inspectZip(formData, opts = {}) {
return unwrap(post(`${BASE}/compile/inspect`, formData, opts));
}
/** P4 POST /api/review/compile/run(JSON {job_id},两段式之②)
* → 与 compileTex 同构信封 {ok, compile_id, pdf_url, log_url, meta};
* pdf/log 路径带 /zip/ 前缀。失败(COMPILE_FAILED/TIMEOUT)也带 compile_id+log_url。
* QUEUE_FULL → HTTP 429(此时 job 未消费,可直接重试)。 */
export function runZipCompile(payload, opts = {}) {
return unwrap(post(`${BASE}/compile/run`, payload, opts));
}
/** ⑤ format-check 专用错误文案:解档/校验类 PARSE_FAILED/VALIDATION 用后端原话
* (与 presubmit 的"扫描件"提示是不同语境,不能复用 CODE_HINTS),其余走统一口径。 */
export function formatErrorMessage(err) {
@@ -119,9 +139,11 @@ const CODE_HINTS = {
VALIDATION: null,
PARSE_FAILED: '论文解析失败:可能是扫描件/图片型 PDF,请换文本型 PDF。',
UPSTREAM_DOWN: '审稿模型暂时不可用,请稍后重试。',
SEED_UNAVAILABLE: '期刊库未就绪(种子数据缺失),请联系管理员。',
SEED_UNAVAILABLE: '期刊库就绪中(种子数据缺失),请稍后再试。',
TIMEOUT: '请求超时,请稍后重试。',
NETWORK: '网络异常,请检查连接后重试。',
// P3b 编译:固定口径(后端 message 带动态 pending 数,此处按规范统一为"最多 5 个")
QUEUE_FULL: '编译队列已满(最多 5 个任务),请稍后重试',
};
/** ApiError(或任意错误)→ 用户可读文案。degraded 由调用方读 err.degraded 走降级横幅样式。 */
+567 -31
View File
@@ -9,10 +9,14 @@
选定后 target = 目录 id,后端经 journal_directory 回链)。
结果三 Tab:修改后源码(Blob 下载)/ Diff 报告(按 rule_key 着色、
按 action 计数:modified=已修改、warning=建议·橙色·不计入)/
编译 PDF(P3 决策暂缓:仅「开发中」占位,不触发编译;后端 tectonic
API(P3a)保留备用,前端不暴露编译入口,引导下载 .tex 去 Overleaf/
本地编译)。
结果跨菜单保留(store reformatResult);样式 rf- 前缀,零新增依赖。 -->
编译 PDF(P3b):POST /compile {job_id}(tectonic 单槽队列,约 10-30 秒,
首次拉 bundle 1-3 分钟——store.compileFirstHint 黄条提示可切面板后台跑);
成功→blob URL 内嵌 iframe 预览 + 下载 + 耗时/重编;失败→错误摘要 +
log 尾 200 行折叠 + 完整 log 下载;QUEUE_FULL→固定文案。
P4 项目 zip(上传区模式切换,与单文件互斥):POST /compile/inspect(≤50MB,
1h TTL,run 前不占编译队列)→ 主文件/统计/跳过文件摘要 → POST /compile/run
(原子消费 inspect;信封与 P3b 同构复用 compile* 字段;失败也带 log_url)。
结果跨菜单保留(store reformatResult/compile*);样式 rf- 前缀,零新增依赖。 -->
<script setup>
import { computed, onMounted, ref, watch } from 'vue';
import { getDirectory, reviewErrorMessage } from '../../../api/review.js';
@@ -28,7 +32,7 @@ const SOURCE_LABEL = {
};
const fileInput = ref(null);
const file = ref(null);
const file = ref(edu.reformatFile || null); // File 存 store(换期刊重排不丢;× 移除才清空)
const fileError = ref('');
const dragging = ref(false);
const tab = ref('source'); // 'source' | 'diff' | 'pdf'
@@ -117,6 +121,31 @@ const structWarnings = computed(() => result.value?.warnings || []);
const modifiedCount = computed(() => changes.value.filter((c) => c.action === 'modified').length);
const suggestCount = computed(() => changes.value.filter((c) => c.action === 'warning').length);
const sourceLabel = computed(() => SOURCE_LABEL[meta.value?.rules_source] || meta.value?.rules_source || '—');
// 目标期刊规则摘要:只显示有值字段(null 不显示);全 null → 明确说明无强制约束。
// 目的:diff=0 时(如 Frontiers / GB↔BMC 互切)也能看到"该刊要求什么",消除"没生效"错觉。
const _REFS_LABEL = { vancouver: 'Vancouver 引用', apa: 'APA 引用', author_year: '作者-年份引用' };
const rulesSummary = computed(() => {
const r = meta.value?.rules;
if (!r || typeof r !== 'object') return null;
const parts = [];
const ls = r.min_line_spacing;
if (typeof ls === 'number' && ls > 0) {
if (ls <= 1.05) parts.push('单倍行距');
else if (Math.abs(ls - 1.5) < 0.01) parts.push('1.5 倍行距');
else if (ls >= 1.95) parts.push('双倍行距');
else parts.push(`行距 ≥${ls} 倍`);
}
if (r.refs_style && _REFS_LABEL[r.refs_style]) parts.push(_REFS_LABEL[r.refs_style]);
if (typeof r.min_font_pt === 'number') parts.push(`字号 ≥${r.min_font_pt}pt`);
if (typeof r.min_margin_mm === 'number') parts.push(`边距 ≥${r.min_margin_mm}mm`);
if (!parts.length) return '该刊官网未规定明确格式要求,无强制约束';
return parts.join(' · ');
});
// 与 diff 的关系:有修改 → "已修改 N 处";0 修改 → "样本已满足该刊要求,无需修改"
const rulesStatusText = computed(() => {
if (modifiedCount.value > 0) return `已修改 ${modifiedCount.value} 处`;
return '样本已满足该刊要求,无需修改';
});
// ISO 时间戳 → "10-12 15:25"(内部字段不裸露;helper 规则见 UI 注释)
const generatedShort = computed(() => {
const s = meta.value?.generated_at;
@@ -129,6 +158,137 @@ function copyJobId() {
if (id && navigator.clipboard) navigator.clipboard.writeText(String(id)).catch(() => {});
}
/* —— P3b 编译 PDF(状态全在 store → 切菜单/切 Tab 编译继续跑)—— */
const compiling = computed(() => edu.compileStatus === 'loading');
const compileReady = computed(() => edu.compileStatus === 'ready' && !!edu.compileResult);
const compileErrored = computed(() => edu.compileStatus === 'error');
const compileMeta = computed(() => edu.compileResult?.meta || null);
const pdfSrc = computed(() => edu.compilePdfUrl || edu.compileResult?.pdf_url || '');
const pdfSizeText = computed(() => {
const b = compileMeta.value?.pdf_bytes;
if (b == null) return '';
return b >= 1048576 ? `${(b / 1048576).toFixed(1)} MB` : `${Math.ceil(b / 1024)} KB`;
});
const compileLog = computed(() => edu.compileLogTail || '');
const logCollapsed = ref(true); // log 折叠区(默认收起)
const firstHintDismissed = ref(false);
const showFirstHint = computed(() =>
edu.compileFirstHint && compiling.value && !firstHintDismissed.value,
);
function startCompile() {
const jid = result.value?.job_id;
if (!jid || compiling.value) return;
logCollapsed.value = true;
edu.submitCompile(jid); // 不 await:按钮/进度由 store 状态驱动,切面板不中断
}
function downloadPdf() {
const href = pdfSrc.value;
if (!href) return;
const a = document.createElement('a');
a.href = href;
a.download = `${result.value?.job_id || 'paper'}.pdf`;
document.body.appendChild(a);
a.click();
a.remove();
}
function downloadFullLog() {
// 成功:信封带 log_url;失败信封不带——job_id 即 anchor_id,log 同目录可拼路径
const jid = result.value?.job_id;
const href = edu.compileResult?.log_url || (jid ? `/api/review/compile/${jid}/log` : '');
if (!href) return;
const a = document.createElement('a');
a.href = href;
a.download = `${jid || 'compile'}.log`;
document.body.appendChild(a);
a.click();
a.remove();
}
/* —— P4 项目 zip(两段式;与单文件模式互斥,切换即清对侧状态)—— */
const uploadMode = ref('tex'); // 'tex' | 'zip'
const zipInput = ref(null);
const zipInspecting = computed(() => edu.zipInspectStatus === 'loading');
const zipInspected = computed(() => edu.zipInspectStatus === 'ready' && !!edu.zipInspect);
const zipError = computed(() => edu.zipInspectError || '');
const MAX_ZIP_SIZE = 50 * 1024 * 1024; // 与后端 REVIEW_MAX_ZIP_BYTES 一致
function switchMode(mode) {
if (uploadMode.value === mode) return;
uploadMode.value = mode;
fileError.value = '';
if (mode === 'zip') {
edu.clearReformat(); // 互斥:zip 编译结果与单文件编译共用 compile* 字段
} else {
edu.clearZip();
}
}
function acceptZip(f) {
if (!f) return;
if (!String(f.name || '').toLowerCase().endsWith('.zip')) {
edu.zipInspectStatus = 'error';
edu.zipInspectError = '仅支持 .zip 项目包';
return;
}
if (!f.size) {
edu.zipInspectStatus = 'error';
edu.zipInspectError = '上传文件为空';
return;
}
if (f.size > MAX_ZIP_SIZE) {
edu.zipInspectStatus = 'error';
edu.zipInspectError = 'zip 超过 50MB 上限';
return;
}
edu.zipFile = f;
edu.zipInspectStatus = 'idle'; // 换文件 → 旧 inspect 失效
edu.zipInspect = null;
edu.zipInspectError = null;
edu.clearCompile();
}
function onZipPick(e) {
acceptZip(e.target.files?.[0]);
e.target.value = '';
}
function onZipDrop(e) {
dragging.value = false;
acceptZip(e.dataTransfer?.files?.[0]);
}
function removeZip() {
edu.clearZip(); // 唯一清空入口:"×" 移除
}
async function startZipInspect() {
if (!edu.zipFile || zipInspecting.value) return;
await edu.submitZipInspect(); // 不 await 也可,但保持与其他面板一致写法
}
function startZipCompile() {
logCollapsed.value = true;
edu.submitZipCompile(); // 不 await:状态由 store 驱动,切面板不中断
}
function downloadZipPdf() {
const href = pdfSrc.value;
if (!href) return;
const cid = edu.compileResult?.compile_id || 'project';
const a = document.createElement('a');
a.href = href;
a.download = `${cid}.pdf`;
document.body.appendChild(a);
a.click();
a.remove();
}
function downloadZipLog() {
const cid = edu.compileResult?.compile_id;
const href = edu.compileResult?.log_url || (cid ? `/api/review/compile/zip/${cid}/log` : '');
if (!href) return;
const a = document.createElement('a');
a.href = href;
a.download = `${cid || 'compile'}.log`;
document.body.appendChild(a);
a.click();
a.remove();
}
onMounted(() => {
if (edu.journalsStatus === 'idle') edu.fetchJournals(); // 精选期刊下拉数据源
// 恢复的 target 是目录刊但无选中记录(历史草稿)→ 回落通用规则
@@ -154,6 +314,7 @@ function acceptFile(f) {
return;
}
file.value = f;
edu.reformatFile = f; // 进 store:换期刊/切菜单重排不丢
}
function onPick(e) {
acceptFile(e.target.files?.[0]);
@@ -165,6 +326,7 @@ function onDrop(e) {
}
function removeFile() {
file.value = null;
edu.reformatFile = null; // 唯一清空入口:"×" 移除
}
async function submit() {
@@ -177,7 +339,7 @@ async function submit() {
}
function reset() {
edu.clearReformat();
file.value = null;
// 不清 file——换期刊重排时保留已上传文件(仅"×"移除才清)
fileError.value = '';
tab.value = 'source';
}
@@ -216,6 +378,11 @@ function download() {
复制任务 ID
</button>
</div>
<!-- 目标期刊规则摘要:diff=0 时也能看到"该刊要求什么"(次级文字,不抢 diff 主视觉) -->
<p v-if="rulesSummary" class="rf-rules" data-testid="rf-rules-summary">
📋 该刊规则:{{ rulesSummary }}
<span class="rf-rules-status">({{ rulesStatusText }})</span>
</p>
<p v-if="meta?.rules_note" class="rf-note" role="note">{{ meta.rules_note }}</p>
<!-- 汇总:已修改 / 建议(橙,不计入修改)/ 结构提示 -->
@@ -269,21 +436,95 @@ function download() {
</article>
</div>
<!-- 编译 PDF(P3 暂缓):「开发中」占位,不触发编译;后端 P3a API 保留备用 -->
<!-- ========== 编译 PDF(P3b) ========== -->
<div v-else class="rf-compile">
<div class="rf-wip" role="note">
<p class="rf-wip-main">
PDF 编译功能开发中,暂时请下载修改后的 .tex 文件,在 Overleaf 或本地编译。
后续将支持多文件工程和图片上传。
</p>
<p class="rf-wip-sub">已提供逐行修改对照(Diff 报告)与修改后源码下载。</p>
<a
class="rf-wip-feedback"
href="http://192.168.0.175:3000/admin/PlantAI-rag-new/issues"
target="_blank"
rel="noopener noreferrer"
>反馈需求 →</a>
<!-- 无 job_id(未完成重排版)→ 引导 -->
<div v-if="!result?.job_id" class="rf-wip" role="note" data-testid="rf-compile-guide">
<p class="rf-wip-main">请先完成重排版再编译。</p>
<p class="rf-wip-sub">编译基于重排版后的 .tex 源码,重排版成功后此页会出现「编译 PDF」按钮。</p>
</div>
<template v-else>
<!-- 首次编译慢提示(loading 期间显示,可手动关闭) -->
<div v-if="showFirstHint" class="rf-first-hint" role="note" data-testid="rf-first-hint">
<span>
首次编译需要 1-3 分钟(下载 TeX 环境),后续编译秒级。
可以切到其他面板,编译会在后台继续。
</span>
<button class="rf-hint-x" type="button" aria-label="关闭提示"
@click="firstHintDismissed = true">×</button>
</div>
<!-- 操作行:编译按钮 / 下载 / 重新编译 -->
<div class="rf-compile-bar">
<button
v-if="!compileReady && !compileErrored"
class="rf-compile-btn" type="button"
data-testid="rf-compile-btn"
:disabled="compiling"
@click="startCompile"
>
{{ compiling ? '编译中…(首次约 1-3 分钟)' : '编译 PDF' }}
</button>
<template v-if="compileReady">
<button class="rf-compile-btn" type="button" data-testid="rf-download-pdf"
@click="downloadPdf">下载 PDF</button>
<button class="rf-compile-btn rf-compile-ghost" type="button"
data-testid="rf-recompile"
:disabled="compiling" @click="startCompile">
{{ compiling ? '编译中…' : '重新编译' }}
</button>
</template>
<template v-if="compileErrored">
<button class="rf-compile-btn" type="button" data-testid="rf-recompile"
:disabled="compiling" @click="startCompile">
{{ compiling ? '编译中…' : '重新编译' }}
</button>
<button class="rf-compile-btn rf-compile-ghost" type="button"
data-testid="rf-download-log" @click="downloadFullLog">查看完整 log</button>
</template>
<span v-if="compileReady && pdfSizeText" class="rf-compile-size"
data-testid="rf-pdf-size">{{ pdfSizeText }}</span>
</div>
<!-- loading:动画进度(后端无 stage,不确定时长) -->
<div v-if="compiling" class="rf-compile-loading" role="status"
data-testid="rf-compile-loading">
<span class="rf-compile-spin" aria-hidden="true" />
<p class="rf-state-msg">
编译中…(tectonic 单槽队列,常规 10-30 秒;首次 1-3 分钟)
可切换到其他面板,编译在后台继续。
</p>
</div>
<!-- 成功:内嵌 PDF 预览(blob URL) -->
<div v-else-if="compileReady" class="rf-compile-ready">
<iframe
class="rf-compile-frame" :src="pdfSrc" title="PDF 预览"
data-testid="rf-pdf-frame"
/>
<div class="rf-compile-foot">
<span class="rf-compile-secs" data-testid="rf-compile-secs">
编译耗时 {{ compileMeta?.seconds != null ? `${Number(compileMeta.seconds).toFixed(1)} 秒` : '—' }}
</span>
</div>
</div>
<!-- 失败:错误摘要 + log 尾折叠 + 完整 log 下载 -->
<div v-else-if="compileErrored" class="rf-compile-error" role="alert"
data-testid="rf-compile-error">
<p class="rf-compile-msg">{{ edu.compileError }}</p>
<details
v-if="compileLog" class="rf-log" :open="!logCollapsed"
data-testid="rf-log-details"
>
<summary class="rf-log-sum" @click="logCollapsed = !logCollapsed">
编译日志(尾 200 行)
</summary>
<pre class="rf-log-body" data-testid="rf-log-body">{{ compileLog }}</pre>
</details>
</div>
</template>
</div>
</template>
@@ -304,6 +545,145 @@ function download() {
<!-- ========== ④ 上传区 ========== -->
<div v-else class="rf-upload">
<!-- P4 模式切换:单文件 .tex(P3 重排版链路)| 项目 .zip(两段式编译,不经重排版) -->
<div class="rf-modes" role="tablist" aria-label="上传模式">
<button
class="rf-mode" :class="{ active: uploadMode === 'tex' }" type="button"
role="tab" :aria-selected="uploadMode === 'tex'" data-testid="rf-mode-tex"
@click="switchMode('tex')"
>单文件 .tex</button>
<button
class="rf-mode" :class="{ active: uploadMode === 'zip' }" type="button"
role="tab" :aria-selected="uploadMode === 'zip'" data-testid="rf-mode-zip"
@click="switchMode('zip')"
>项目 .zip</button>
</div>
<!-- ========== ④-zip 项目上传(P4 两段式) ========== -->
<template v-if="uploadMode === 'zip'">
<div
class="rf-drop"
:class="{ dragging, filled: !!edu.zipFile }"
role="button"
tabindex="0"
@click="zipInput.click()"
@keydown.enter="zipInput.click()"
@dragover.prevent="dragging = true"
@dragleave.prevent="dragging = false"
@drop.prevent="onZipDrop"
>
<input
ref="zipInput"
class="rf-file-input"
type="file"
accept=".zip"
@click.stop
@change="onZipPick"
/>
<template v-if="edu.zipFile">
<span class="rf-file-name">🗜 {{ edu.zipFile.name }} · {{ (edu.zipFile.size / 1024 / 1024).toFixed(1) }} MB</span>
<button class="rf-remove" type="button" title="移除文件" @click.stop="removeZip">×</button>
</template>
<template v-else>
<span class="rf-drop-main">拖拽 LaTeX 项目(.zip)到此处,或点击选择</span>
<span class="rf-hint">打包建议:main.tex 放 zip 根目录,图片用相对路径引用,不要包含 .git / node_modules / 无关大文件。</span>
</template>
</div>
<p v-if="zipError" class="rf-file-error" data-testid="rf-zip-error">{{ zipError }}</p>
<button
class="rf-submit"
type="button"
:disabled="!edu.zipFile || zipInspecting"
data-testid="rf-zip-inspect-btn"
@click="startZipInspect"
>
{{ zipInspecting ? '检查中…' : '上传并检查' }}
</button>
<!-- inspect 通过:项目摘要 + 编译入口 + 编译结果(复用 P3b compile* 状态) -->
<template v-if="zipInspected">
<div class="rf-zip-meta" data-testid="rf-zip-meta">
📦 主文件:<code>{{ edu.zipInspect?.main }}</code>
· 文件 {{ edu.zipInspect?.stats?.files }} 个
· .tex {{ edu.zipInspect?.stats?.tex }}
<template v-if="edu.zipInspect?.stats?.images"> · 图片 {{ edu.zipInspect.stats.images }}</template>
<template v-if="edu.zipInspect?.stats?.cls"> · .cls {{ edu.zipInspect.stats.cls }}</template>
<template v-if="edu.zipInspect?.stats?.bib"> · .bib {{ edu.zipInspect.stats.bib }}</template>
<template v-if="edu.zipInspect?.skipped_count">
· <span class="rf-zip-skipped">{{ edu.zipInspect.skipped_count }} 个文件已跳过</span>
</template>
</div>
<p v-if="edu.zipInspect?.skipped_count" class="rf-zip-skipped-list" data-testid="rf-zip-skipped">
已跳过(不参与编译):{{ (edu.zipInspect?.skipped || []).map((s) => s.name).join('、') }}
</p>
<div class="rf-compile-bar">
<button
v-if="!compileReady && !compileErrored"
class="rf-compile-btn" type="button"
data-testid="rf-zip-compile-btn"
:disabled="compiling"
@click="startZipCompile"
>
{{ compiling ? '编译中…' : '编译 PDF' }}
</button>
<template v-if="compileReady">
<button class="rf-compile-btn" type="button" data-testid="rf-download-pdf"
@click="downloadZipPdf">下载 PDF</button>
<button class="rf-compile-btn rf-compile-ghost" type="button"
data-testid="rf-recompile" :disabled="compiling" @click="startZipCompile">
{{ compiling ? '编译中…' : '重新编译' }}
</button>
</template>
<template v-if="compileErrored">
<button class="rf-compile-btn" type="button" data-testid="rf-recompile"
:disabled="compiling" @click="startZipCompile">
{{ compiling ? '编译中…' : '重新编译' }}
</button>
<button class="rf-compile-btn rf-compile-ghost" type="button"
data-testid="rf-download-log" @click="downloadZipLog">查看完整 log</button>
</template>
<span v-if="compileReady && pdfSizeText" class="rf-compile-size"
data-testid="rf-pdf-size">{{ pdfSizeText }}</span>
</div>
<div v-if="compiling" class="rf-compile-loading" role="status"
data-testid="rf-compile-loading">
<span class="rf-compile-spin" aria-hidden="true" />
<p class="rf-state-msg">编译中…(tectonic 单槽队列,常规 10-30 秒;项目首次编译需下载宏包可能更慢)</p>
</div>
<div v-else-if="compileReady" class="rf-compile-ready">
<iframe
class="rf-compile-frame" :src="pdfSrc" title="PDF 预览"
data-testid="rf-pdf-frame"
/>
<div class="rf-compile-foot">
<span class="rf-compile-secs" data-testid="rf-compile-secs">
编译耗时 {{ compileMeta?.seconds != null ? `${Number(compileMeta.seconds).toFixed(1)} 秒` : '—' }}
</span>
</div>
</div>
<div v-else-if="compileErrored" class="rf-compile-error" role="alert"
data-testid="rf-compile-error">
<p class="rf-compile-msg">{{ edu.compileError }}</p>
<details
v-if="compileLog" class="rf-log" :open="!logCollapsed"
data-testid="rf-log-details"
>
<summary class="rf-log-sum" @click="logCollapsed = !logCollapsed">
编译日志(尾 200 行)
</summary>
<pre class="rf-log-body" data-testid="rf-log-body">{{ compileLog }}</pre>
</details>
</div>
</template>
</template>
<!-- ========== ④-tex 单文件上传(P3 链路,零改动) ========== -->
<template v-else>
<div
class="rf-drop"
:class="{ dragging, filled: !!file }"
@@ -387,6 +767,7 @@ function download() {
开始自动重排版
</button>
<p v-if="dirMode && !actualTarget" class="rf-dirhint">请从搜索结果中选择期刊后提交</p>
</template>
</div>
</section>
</template>
@@ -416,6 +797,15 @@ function download() {
font-size: 12.5px;
color: var(--pa-ink-muted);
}
/* 规则摘要:次级文字(同 rf-meta 色阶),与汇总条的计数视觉区分 */
.rf-rules {
margin: 4px 0 0;
font-size: 12.5px;
color: var(--pa-ink-muted);
}
.rf-rules-status {
color: var(--pa-ink-subtle);
}
.rf-copy-id {
margin-left: 8px;
padding: 1px 8px;
@@ -625,12 +1015,6 @@ function download() {
color: var(--pa-warning);
}
/* 编译 PDF(P3 暂缓:开发中占位) */
.rf-compile {
display: flex;
flex-direction: column;
gap: 10px;
}
.rf-wip {
display: flex;
flex-direction: column;
@@ -652,13 +1036,126 @@ function download() {
font-size: 12.5px;
color: var(--pa-ink-subtle);
}
.rf-wip-feedback {
font-size: 12.5px;
font-weight: 600;
color: var(--pa-g-from);
text-decoration: none;
/* 编译 PDF(P3b) */
.rf-compile {
display: flex;
flex-direction: column;
gap: 10px;
}
.rf-first-hint {
display: flex;
justify-content: space-between;
align-items: flex-start;
gap: 10px;
padding: 10px 14px;
border: 1px solid color-mix(in srgb, var(--pa-warning) 45%, transparent);
border-radius: var(--pa-r-md);
background: color-mix(in srgb, var(--pa-warning) 12%, transparent);
color: var(--pa-ink);
font-size: 12.5px;
line-height: 1.6;
}
.rf-hint-x {
border: none;
background: transparent;
color: var(--pa-ink-muted);
font-size: 15px;
line-height: 1;
cursor: pointer;
padding: 0 2px;
}
.rf-hint-x:hover { color: var(--pa-ink); }
.rf-compile-bar {
display: flex;
gap: 8px;
align-items: center;
flex-wrap: wrap;
}
.rf-compile-btn {
padding: 7px 18px;
border: none;
border-radius: var(--pa-r-md);
background: var(--pa-gradient);
color: #fff;
font-size: 13px;
font-weight: 600;
cursor: pointer;
}
.rf-compile-btn:disabled { opacity: .6; cursor: not-allowed; }
.rf-compile-btn:not(:disabled):hover { filter: brightness(1.08); }
.rf-compile-ghost {
background: transparent;
border: 1px solid var(--pa-hairline-strong);
color: var(--pa-ink);
}
.rf-compile-size {
font-size: 12px;
color: var(--pa-ink-subtle);
}
.rf-compile-loading {
display: flex;
gap: 12px;
align-items: center;
padding: 14px 16px;
background: var(--pa-surface-2);
border: 1px solid var(--pa-hairline);
border-radius: var(--pa-r-lg);
}
.rf-compile-spin {
flex: none;
width: 18px;
height: 18px;
border: 2px solid var(--pa-hairline-strong);
border-top-color: var(--pa-g-from);
border-radius: 50%;
animation: rf-spin .8s linear infinite;
}
@keyframes rf-spin { to { transform: rotate(360deg); } }
.rf-compile-ready { display: flex; flex-direction: column; gap: 8px; }
.rf-compile-frame {
width: 100%;
height: 560px;
border: 1px solid var(--pa-hairline);
border-radius: var(--pa-r-lg);
background: var(--pa-surface-2);
}
.rf-compile-foot { display: flex; gap: 12px; align-items: center; }
.rf-compile-secs { font-size: 12px; color: var(--pa-ink-subtle); }
.rf-compile-error {
display: flex;
flex-direction: column;
gap: 8px;
padding: 12px 16px;
background: color-mix(in srgb, var(--pa-error) 8%, transparent);
border: 1px solid color-mix(in srgb, var(--pa-error) 40%, transparent);
border-radius: var(--pa-r-lg);
}
.rf-compile-msg {
margin: 0;
font-size: 13px;
color: var(--pa-error);
}
.rf-log { border-top: 1px dashed var(--pa-hairline-strong); padding-top: 8px; }
.rf-log-sum {
font-size: 12.5px;
color: var(--pa-ink-muted);
cursor: pointer;
user-select: none;
}
.rf-log-body {
margin: 8px 0 0;
max-height: 300px;
overflow: auto;
padding: 10px 12px;
background: var(--pa-code-bg);
border-radius: var(--pa-r-md);
font-size: 12px;
line-height: 1.55;
color: var(--pa-ink-muted);
white-space: pre-wrap;
word-break: break-word;
}
.rf-wip-feedback:hover { text-decoration: underline; }
/* 状态区 */
.rf-state {
@@ -755,6 +1252,45 @@ function download() {
font-size: 12.5px;
color: var(--pa-error);
}
/* P4 模式切换(单文件 .tex | 项目 .zip) */
.rf-modes {
display: flex;
gap: 6px;
}
.rf-mode {
padding: 5px 14px;
border: 1px solid var(--pa-hairline);
border-radius: var(--pa-r-pill);
background: transparent;
color: var(--pa-ink-muted);
font-size: 12px;
cursor: pointer;
}
.rf-mode.active {
border-color: var(--pa-g-from);
background: var(--pa-surface-2);
color: var(--pa-ink);
font-weight: 600;
}
/* P4 zip 项目摘要(次级文字,与 rf-meta 同色阶) */
.rf-zip-meta {
margin: 4px 0 0;
font-size: 12.5px;
color: var(--pa-ink-muted);
}
.rf-zip-meta code {
padding: 1px 6px;
border: 1px solid var(--pa-hairline);
border-radius: var(--pa-r-sm);
font-size: 12px;
}
.rf-zip-skipped { color: var(--pa-warning); }
.rf-zip-skipped-list {
margin: 0;
font-size: 12px;
color: var(--pa-ink-subtle);
word-break: break-all;
}
.rf-label {
font-size: 12px;
font-weight: 500;
+89 -4
View File
@@ -4,7 +4,7 @@
// - 进行中的请求由 store action 持有 → 面板卸载不中断;
// - 表单草稿存 drafts → 切菜单不丢。
import { defineStore } from 'pinia';
import { compileTex, formatCheck, formatErrorMessage, getConferences, getDirectory, getJournals, presubmit, recommend, reformatTex, reviewErrorMessage, rebuttalConfirm, rebuttalCreateFromPdf, rebuttalCreateJob, rebuttalDeleteJob, rebuttalDraft, rebuttalLetter, rebuttalSaveResponse, pptBuildJob, pptCreateFromPdf, pptCreateJob, pptDeleteJob, pptSavePages, pptStyles } from '../api/review.js';
import { compileTex, formatCheck, formatErrorMessage, getConferences, getDirectory, getJournals, inspectZip, pptBuildJob, pptCreateFromPdf, pptCreateJob, pptDeleteJob, pptSavePages, pptStyles, presubmit, recommend, reformatTex, reviewErrorMessage, rebuttalConfirm, rebuttalCreateFromPdf, rebuttalCreateJob, rebuttalDeleteJob, rebuttalDraft, rebuttalLetter, rebuttalSaveResponse, runZipCompile } from '../api/review.js';
const STORAGE_KEY = 'plantai.edu.submissions'; // D3 台账持久化(tracker 面板已解除映射,state 保留)
// tracker 已砍(2026-10-12,仅解除 UI 映射;TrackerPanel.vue 保留在 panels/ 下可复用)
@@ -72,6 +72,9 @@ export const useEduStore = defineStore('edu', {
// ③ presubmit 已选 PDF(File 对象,仅内存:面板卸载/切菜单不丢,error 态可直接重试;
// 不可序列化 → 不进 localStorage;clearReport 一并清空)
presubmitFile: null,
// ⑤' reformat 已选 .tex(同 presubmitFile 模式:换期刊重排不丢文件;
// 面板内"×"移除才清空。File 不可序列化 → 不进 localStorage)
reformatFile: null,
recommend: null, // ② 推荐响应 {ok, tiers, meta}
recommendStatus: 'idle',
recommendError: null,
@@ -83,11 +86,19 @@ export const useEduStore = defineStore('edu', {
reformatResult: null,
reformatStatus: 'idle',
reformatError: null,
/* —— P3a 编译 PDF(tectonic;与 reformatResult 同生命周期,重新排版即失效)—— */
compileResult: null, // {ok, compile_id, pdf_url, log_url, meta}
/* —— P3a/P3b 编译 PDF(tectonic;与 reformatResult 同生命周期,重新排版即失效)—— */
compileResult: null, // {ok, compile_id, pdf_url, log_url, meta:{seconds,pdf_bytes,...}}
compileStatus: 'idle', // 'idle' | 'loading' | 'ready' | 'error'
compileError: null,
compileLogTail: '', // COMPILE_FAILED/TIMEOUT 的 log 尾 200 行(err.detail)
compilePdfUrl: null, // PDF blob URL(预览/下载,不暴露 API 路径;失败回退 pdf_url)
compileFirstHint: false, // 首次编译黄色提示条(编译开始到 clearCompile 前保留)
/* —— P4 项目 zip 两段式(inspect 暂存 1h + run 编译;run 信封与 P3b 同构,
复用 compile* 字段展示 PDF/log——zip 编译结果与单文件编译互斥,切换模式即清)—— */
zipFile: null, // File(内存态:切菜单不丢,× 移除才清空)
zipInspect: null, // inspect meta {job_id, main, stats{}, skipped[], skipped_count}
zipInspectStatus: 'idle', // 'idle' | 'loading' | 'ready' | 'error'
zipInspectError: null,
/* —— 表单草稿(切菜单不丢;四字段并存:recommend 用 abstract+prefs、
presubmit 用 targetJournal、format 用 formatJournalId(期刊 id 下拉))—— */
@@ -569,18 +580,20 @@ export const useEduStore = defineStore('edu', {
this.reformatError = null;
this.clearCompile();
},
/* —— P3a 编译 PDF(tectonic 单槽队列;约 10-30 秒)—— */
/* —— P3b 编译 PDF(tectonic 单槽队列;约 10-30 秒,首次拉 bundle 1-3 分钟)—— */
async submitCompile(jobId) {
if (this.compileStatus === 'loading') return null;
this.compileStatus = 'loading';
this.compileError = null;
this.compileLogTail = '';
if (!this.compileResult) this.compileFirstHint = true; // 首次编译提示条
const fd = new FormData();
fd.append('job_id', jobId);
try {
const payload = await compileTex(fd);
this.compileResult = payload;
this.compileStatus = 'ready';
this._loadCompileBlob(payload.pdf_url); // 异步换 blob URL,失败回退直链
return payload;
} catch (err) {
this.compileStatus = 'error';
@@ -589,11 +602,83 @@ export const useEduStore = defineStore('edu', {
return null;
}
},
/** PDF → blob URL(预览/下载不暴露后端路径);失败静默回退 pdf_url 直链 */
async _loadCompileBlob(url) {
if (!url) return;
try {
const resp = await fetch(url);
if (!resp.ok) return;
const blob = await resp.blob();
if (this.compilePdfUrl) URL.revokeObjectURL(this.compilePdfUrl);
this.compilePdfUrl = URL.createObjectURL(blob);
} catch { /* 保持 pdf_url 直链 */ }
},
clearCompile() {
if (this.compilePdfUrl) {
try { URL.revokeObjectURL(this.compilePdfUrl); } catch { /* noop */ }
}
this.compileResult = null;
this.compileStatus = 'idle';
this.compileError = null;
this.compileLogTail = '';
this.compilePdfUrl = null;
this.compileFirstHint = false;
},
/* —— P4 项目 zip 两段式 —— */
/** 之① inspect:安全解压 + 主文件识别(1h TTL,run 前不占编译队列) */
async submitZipInspect() {
if (!this.zipFile || this.zipInspectStatus === 'loading') return null;
this.zipInspectStatus = 'loading';
this.zipInspectError = null;
this.zipInspect = null;
this.clearCompile(); // 新 zip 使旧 PDF 失效
const fd = new FormData();
fd.append('file', this.zipFile);
try {
const payload = await inspectZip(fd);
this.zipInspect = payload.meta;
this.zipInspectStatus = 'ready';
return payload;
} catch (err) {
this.zipInspectStatus = 'error';
this.zipInspectError = reviewErrorMessage(err); // ZIP_UNSAFE/BOMB/INVALID 走后端原话
return null;
}
},
/** 之② run:原子消费 inspect → 编译(信封与 P3b 同构 → 复用 compile* 字段) */
async submitZipCompile() {
const jid = this.zipInspect?.job_id;
if (!jid || this.compileStatus === 'loading') return null;
this.compileStatus = 'loading';
this.compileError = null;
this.compileLogTail = '';
if (!this.compileResult) this.compileFirstHint = true;
try {
const payload = await runZipCompile({ job_id: jid });
this.compileResult = payload;
this.compileStatus = 'ready';
this._loadCompileBlob(payload.pdf_url);
return payload;
} catch (err) {
this.compileStatus = 'error';
this.compileError = reviewErrorMessage(err);
// 失败信封带 compile_id + log_url(补充 3)→ log 尾仍走 err.detail
this.compileLogTail = err.detail || '';
// 失败也挂结果壳(仅 id/url):下载完整 log 用 /zip/{cid}/log(成功信封同构)
if (err.compileId) {
this.compileResult = {
ok: false, compile_id: err.compileId, log_url: err.logUrl || null,
};
}
return null;
}
},
clearZip() {
this.zipFile = null;
this.zipInspect = null;
this.zipInspectStatus = 'idle';
this.zipInspectError = null;
this.clearCompile();
},
},
});