收敛 document-photo-to-pdf 的 AI 契约:默认审核页、精简返回值
SKILL.md 只保留 images/output;默认 review=always;stdout 只回 ok/summary/pdf,完整审核地址由 visit_url 追加;内部协议移到 REVIEW.md;审核页补充警告/状态展示,手机默认进预览。
This commit is contained in:
@@ -0,0 +1,158 @@
|
||||
# 审核页内部协议(维护者文档)
|
||||
|
||||
> 本文件刻意不在 SKILL.md 里引用,避免模型顺着指针来读;只有维护者需要看。
|
||||
|
||||
`SKILL.md` 是给 AI 的调用契约;本文件记录审核页/重生成/数据表的实现细节。**AI 日常调用
|
||||
不需要读这里**,`skill get` 也只返回 SKILL.md。
|
||||
|
||||
## 文件与目录
|
||||
|
||||
```
|
||||
skills/document-photo-to-pdf/
|
||||
SKILL.md ← AI 契约(只有 images/output)
|
||||
REVIEW.md ← 本文件
|
||||
run.py ← 入口
|
||||
doc_deskew/ ← 检测/几何/出页/检验
|
||||
review_store.py ← data.db 的 doc_review_* 表
|
||||
review_page.py ← 模板副本 + 缩略图发布
|
||||
web/index.html ← 审核页模板的单一事实源
|
||||
|
||||
tasks/<session-id>/
|
||||
.app/doc-review/
|
||||
index.html ← 每次发布覆盖的模板副本(不含数据注入)
|
||||
<batch_id>/p1.jpg ... ← 本批缩略图(长边 1200,JPEG)
|
||||
data.db ← doc_review_batch / doc_review_page / doc_review_job
|
||||
<output>.pdf
|
||||
<output>.state.json ← state v2,供恢复自动/调试
|
||||
```
|
||||
|
||||
core 只托管会话 `.app/`,不托管 skill 目录,所以模板必须复制一份到会话;源仍只有
|
||||
`web/index.html` 一份,副本不是第二份源码。
|
||||
|
||||
## 对 AI 隐藏的参数
|
||||
|
||||
`run.py` 仍接受以下参数,但不出现在 SKILL.md / input_schema 里;审核页和 apply 接口使用:
|
||||
|
||||
| 参数 | 用途 |
|
||||
|---|---|
|
||||
| `paper` | a4/letter/16k/b5/数字比例/auto |
|
||||
| `orientation` | auto/none/portrait/landscape |
|
||||
| `enhance` | original/normal/strong/bw |
|
||||
| `dpi` | standard/high/72~600 |
|
||||
| `review` | `always`(默认)/`auto`(仅有 needs 时发布)/`off`(自动化用) |
|
||||
| `batch_id` | 重生成:从 data.db 读源图/输出/编辑,覆盖同名 PDF |
|
||||
| `edits` | `review/1` 对象或 JSON 路径;优先级高于显式参数 |
|
||||
| `state` | 上一份 state.json;仅用于恢复自动/调试 |
|
||||
|
||||
优先级:`edits.batch` > 显式参数 > 默认值。
|
||||
|
||||
## data.db 表
|
||||
|
||||
所有表以 `doc_review_` 前缀,避免和用户业务表混淆。技能自己 `CREATE TABLE IF NOT EXISTS`;
|
||||
`busy_timeout=5000`,WAL 由 core 建库时设置,技能侧 best-effort。
|
||||
|
||||
### doc_review_batch
|
||||
|
||||
```sql
|
||||
id TEXT PRIMARY KEY, -- drYYYYMMDD-HHMMSS-xxxx,不含点/斜杠
|
||||
output TEXT, -- 相对会话目录的 PDF 路径
|
||||
paper TEXT, enhance TEXT, dpi TEXT, orientation TEXT,
|
||||
status TEXT, -- running/ready/review/error
|
||||
summary TEXT, warnings TEXT,-- warnings 是 JSON 数组
|
||||
schema_version INTEGER,
|
||||
created_at TEXT, updated_at TEXT
|
||||
```
|
||||
|
||||
### doc_review_page
|
||||
|
||||
```sql
|
||||
batch_id TEXT, idx INTEGER, -- idx 永远是原始 images 的 1-based 下标
|
||||
source TEXT, -- 相对会话目录的原图路径
|
||||
source_w INTEGER, source_h INTEGER,
|
||||
w INTEGER, h INTEGER, -- 缩略图尺寸(页面画布用)
|
||||
auto_corners TEXT, -- 原图像素,自动基线
|
||||
auto_corners_norm TEXT, -- 归一化,页面“恢复自动”用
|
||||
corners TEXT, -- 归一化,当前角点(未旋转)
|
||||
auto_rotate INTEGER, -- 自动方向基线(0/90)
|
||||
rotate INTEGER, -- 当前应用旋转(用户覆盖后是绝对值)
|
||||
deleted INTEGER, sort INTEGER,
|
||||
status TEXT, reason TEXT, thumb TEXT, error TEXT,
|
||||
PRIMARY KEY(batch_id, idx)
|
||||
```
|
||||
|
||||
### doc_review_job
|
||||
|
||||
```sql
|
||||
batch_id TEXT PRIMARY KEY,
|
||||
edits TEXT, -- review/1 完整快照
|
||||
status TEXT, -- queued/running/done/error
|
||||
message TEXT,
|
||||
requested_at TEXT, started_at TEXT, finished_at TEXT
|
||||
```
|
||||
|
||||
## 审核页如何工作
|
||||
|
||||
1. 页面从 `/app/<task_id>/doc-review/` 加载;无 `?b=` 时查 `doc_review_batch` 最新一行。
|
||||
2. 通过 `POST /api/db/<task_id>/query` 读 `doc_review_batch` / `doc_review_page`。
|
||||
3. 用户拖角/旋转/删页/排序/改四项参数,只改浏览器内状态并写 localStorage 草稿。
|
||||
4. 点「生成 PDF」:
|
||||
- `INSERT ... ON CONFLICT` 写 `doc_review_job`(`status=queued`,`edits` 是完整快照);
|
||||
- 调通用执行接口
|
||||
`POST /api/sessions/<task_id>/skills/document-photo-to-pdf/run`,body `{args:{batch_id}}`;
|
||||
- 技能读 job → 应用 edits → 覆盖同名 PDF → 更新 batch/page/job;
|
||||
- 页面重新读 data.db,刷新摘要、状态、PDF 链接。
|
||||
5. 全程不经过模型,也不需要用户复制 JSON。
|
||||
|
||||
通用接口实现在 `app/server/routes/skill-run.js`:core 只做会话/并发/错误映射,执行仍走
|
||||
`tools/skills.runSkill`,不感知技能名或参数语义。
|
||||
|
||||
## edits / 旋转语义
|
||||
|
||||
```json
|
||||
{"schema":"review/1",
|
||||
"batch":{"paper":"a4","enhance":"normal","dpi":"standard","orientation":"auto"},
|
||||
"pages":[{"i":3,"c":[[0.03,0.05],[0.97,0.04],[0.96,0.95],[0.04,0.96]],"r":90}],
|
||||
"order":[1,2,3],"deleted":[]}
|
||||
```
|
||||
|
||||
- `i` 是原始 images 下标,不随页序变化。
|
||||
- `c` 是 exif_transpose 后原图的归一化坐标,顺序左上/右上/右下/左下。
|
||||
- `r` 是该页最终顺时针旋转(0/90/180/270),覆盖自动方向;页面总会为“人工页”带上
|
||||
`r`(包括 0),服务端用 `user_r is not None` 判断是否覆盖。
|
||||
- `pages` 只放与自动基线不同的页;未改的页由服务端重新检测/评级,避免把 fallback 页
|
||||
误升成 ready。
|
||||
- `order` 缺省按 images 原序;`deleted` 的页不进出件。
|
||||
- 人工旋转 90/270 时,`orientation=auto` 下该页画布按旋转后的有效方向走,避免把横向内容
|
||||
压进竖向画布;`portrait`/`landscape` 强制方向时仍按强制值。
|
||||
|
||||
## state v2
|
||||
|
||||
每次成功出件写 `<output>.state.json`:
|
||||
|
||||
```json
|
||||
{"schema":"document-photo-to-pdf/2",
|
||||
"batch":{"doc_type":null,"paper":"auto","enhance":"normal","dpi":150,"orientation":"auto"},
|
||||
"pages":[{"index":1,"source":"p1.jpg","source_size":[1400,1700],
|
||||
"auto_corners":[[...]],"corners_norm":[[...]],
|
||||
"rotate":0,"rotate_source":"auto","status":"ready","reason":null}]}
|
||||
```
|
||||
|
||||
`rotate` 是自动基线(不是用户覆盖值);用户覆盖值在 data.db 的 `rotate` 列。state 只用于
|
||||
恢复自动/调试,审核页不读它。
|
||||
|
||||
## 状态与失败
|
||||
|
||||
- `ready`:可直接用。
|
||||
- `review`:建议看一眼(掩膜回退、覆盖率低、纸型存疑)。
|
||||
- `needs_corners`:检测为空或校验不过,已用“画面内缩 3%”兜底框出件,需人工补角。
|
||||
- `needs_orientation`:接近正方形,横竖判不出来,需人在页面旋转。
|
||||
- `render_failed` / `failed`:出页失败/原图打不开;后者会被跳过。
|
||||
- `deleted`:用户删除,不进出件。
|
||||
|
||||
单页检测/校验失败不再让整批失败;只有一页都出不了才 `ok:false`。检测到的无效四边形会
|
||||
退回兜底框,并把 `auto_corners` 一起换成兜底框,避免“恢复自动”把无效角点拖回来。
|
||||
|
||||
## 清理
|
||||
|
||||
审核页发布只保留最近 5 个批次的缩略图目录;新批次完成后删除更早的目录和 DB 记录。
|
||||
`batch_id` 重生成(apply)不会触发清理,避免把正在编辑的旧批次裁掉。
|
||||
@@ -1,188 +1,52 @@
|
||||
---
|
||||
name: document-photo-to-pdf
|
||||
description: 把手机拍摄的纸质文档照片逐页裁剪、拉正、判定方向,合并为一个 PDF;可选发布可拖拽角点的审核页
|
||||
description: 把手机拍摄的纸质文档照片裁剪、拉正,合并为 PDF,并给出可预览/微调的审核页
|
||||
visit_url: /app/{task_id}/doc-review/
|
||||
input_schema:
|
||||
type: object
|
||||
properties:
|
||||
images:
|
||||
type: array
|
||||
items: {type: string}
|
||||
description: 图片路径,按合并顺序;新批次必填,传 batch_id 时可省略
|
||||
description: 图片路径,按合并顺序;可只传一张
|
||||
output:
|
||||
type: string
|
||||
description: 输出 PDF 路径,默认 文档照片_校正.pdf;batch_id 模式忽略此参数,沿用批次原输出
|
||||
paper:
|
||||
type: string
|
||||
description: a4/letter/16k/b5/数字宽高比/auto;默认 auto
|
||||
orientation:
|
||||
type: string
|
||||
enum: [auto, none, portrait, landscape]
|
||||
description: 方向策略;默认 auto(逐页按纸张宽高比判横竖)
|
||||
enhance:
|
||||
type: string
|
||||
enum: [original, normal, strong, bw]
|
||||
description: 调色档;默认 normal
|
||||
dpi:
|
||||
type: [string, integer]
|
||||
description: 清晰度;standard(150)/high(300) 或 72~600 的数字
|
||||
review:
|
||||
type: string
|
||||
enum: [off, always, auto]
|
||||
description: 审核页策略;off(默认)不发布,always 总是发布,auto 只在有页需要人看时发布
|
||||
batch_id:
|
||||
type: string
|
||||
description: 重生成用;从 data.db 读源图/输出/编辑,覆盖同名 PDF。忽略 images/output
|
||||
edits:
|
||||
type: [string, object]
|
||||
description: review/1 修改对象或 JSON 路径;batch_id 模式不传时读 doc_review_job.edits
|
||||
state:
|
||||
type: string
|
||||
description: 上一份 state.json 路径;可选,仅用于恢复自动角点/调试
|
||||
description: 输出 PDF 文件名,默认 文档照片_校正.pdf
|
||||
---
|
||||
|
||||
# 文档照片转 PDF
|
||||
|
||||
手机拍的纸质材料 → 一个版正字清的 PDF。自动检测纸张四角、自动拉正,不读图、不传坐标。
|
||||
需要人工确认时,可以发布一个可拖拽角点的审核页,改完点「生成 PDF」直接覆盖重出。
|
||||
|
||||
## 两种用法
|
||||
|
||||
### 全自动(默认)
|
||||
|
||||
```json
|
||||
{"images": ["图1.jpg", "图2.jpg"], "output": "合同_校正.pdf"}
|
||||
```
|
||||
|
||||
只出 PDF,不写 `.app/`、不碰 `data.db`。适合“直接出件、不用看”的场景。
|
||||
`images` 必填,按顺序合并;`output` 可选,默认 `文档照片_校正.pdf`。纸张、方向、调色、
|
||||
清晰度不要传,交给审核页。
|
||||
|
||||
### 半自动(审核页)
|
||||
## 返回值
|
||||
|
||||
- `ok:true`:`summary` 一句话摘要,`pdf` 文件名;末尾附完整「访问地址」。把 PDF 和地址
|
||||
一起给用户。
|
||||
- `ok:false`:转述 `error.message` / `error.hint`,忽略访问地址。
|
||||
|
||||
```json
|
||||
{"images": ["图1.jpg", "图2.jpg"], "review": "always"}
|
||||
{"ok":true,"summary":"3 页已生成;1 页建议在审核页确认","pdf":"合同_校正.pdf"}
|
||||
访问地址:https://agent.ai.foresh.com/app/task-20261023-001/doc-review/
|
||||
```
|
||||
|
||||
在出 PDF 的同时:
|
||||
审核页可拖角、旋转、删页/排序、改纸张与清晰度;点「生成 PDF」覆盖同名文件。
|
||||
|
||||
1. 把源图缩略图发布到会话 `.app/doc-review/<batch_id>/`;
|
||||
2. 把批次/页面/任务记录写进会话 `data.db` 的 `doc_review_*` 表;
|
||||
3. 返回 `review.url`(站内路径 `/app/<task_id>/doc-review/?b=<batch_id>`)。
|
||||
## 失败与拍摄
|
||||
|
||||
用户拖角点、旋转、删页、调页序、改四项参数后,点页面右下角「生成 PDF」:
|
||||
页面把修改写进 `doc_review_job`,再调用通用执行接口
|
||||
`POST /api/sessions/<task_id>/skills/document-photo-to-pdf/run`(只传 `batch_id`),
|
||||
服务端按同一份 `run.py` 覆盖同名 PDF,页面自动刷新。**不需要复制 JSON,也不经过模型。**
|
||||
|
||||
`review=auto` 是两者之间的桥:只有存在 `needs_attention` 的页(检测不到角点、方向存疑、
|
||||
覆盖率低等)才发布审核页;全部就绪时不发布。
|
||||
|
||||
## 参数
|
||||
|
||||
| 参数 | 必填 | 说明 |
|
||||
|---|---|---|
|
||||
| `images` | 新批次必填 | 图片路径,按传入顺序合并;相对路径按会话目录解析 |
|
||||
| `output` | 否 | 默认 `文档照片_校正.pdf`;`batch_id` 模式沿用批次原输出并覆盖 |
|
||||
| `paper` | 否 | 默认 `auto`:按同批中位比例统一版式,填满 A4 内容区;偏差超过 20% 的页保持原比例。整批都是 A4 时可传 `a4`;票据/异形纸保持 `auto` 或传数字比例(宽/高) |
|
||||
| `orientation` | 否 | 默认 `auto`:**逐页**按本页纸张宽高比定横竖,不看批次多数票。`none` 保持每页原方向;`portrait`/`landscape` 强制整批对齐 |
|
||||
| `enhance` | 否 | 调色档,默认 `normal`。`original` 原样不提亮;`strong` 加强;`bw` 黑白(保留灰阶,不二值化) |
|
||||
| `dpi` | 否 | `standard`(150) / `high`(300) / 72~600 的数字,默认 150 |
|
||||
| `review` | 否 | `off`(默认)/`always`/`auto`;传 `batch_id` 且不传 review 时按 `always` |
|
||||
| `batch_id` | 否 | 重生成:读 `data.db` 里的源图、输出、编辑,覆盖同名 PDF;忽略 `images`/`output` |
|
||||
| `edits` | 否 | `review/1` 对象或路径;`batch_id` 模式不传时读 `doc_review_job.edits` |
|
||||
| `state` | 否 | 上一份 `state.json` 路径;可选,只用于恢复自动角点/调试 |
|
||||
|
||||
优先级:`edits.batch` > 显式参数 > 默认值。`edits` 里的角点会跳过检测,直接用给定坐标。
|
||||
|
||||
## 看返回值
|
||||
|
||||
stdout 是一个 JSON 对象,**先看 `ok`**:
|
||||
|
||||
- `ok:true`:`output.path` 是 PDF;`summary` 可直接转述;`warnings` 非空时要一起告诉用户。
|
||||
- `ok:false`:不要重试,把 `error.message` 和 `error.hint` 转述给用户。
|
||||
|
||||
**单页拍得不好不再等于失败**:检测不到角点、角点校验不过的页会用“画面内缩 3%”的兜底框
|
||||
照常出件,该页 `status` 为 `needs_corners`;原图打不开的页标记 `failed` 并跳过。只有一页都
|
||||
出不了时才返回 `ok:false`。
|
||||
|
||||
成功示例(半自动):
|
||||
|
||||
```json
|
||||
{"ok":true,"summary":"3 页已生成;1 页需人工确认",
|
||||
"output":{"path":"/.../合同_校正.pdf","pages":3},
|
||||
"review":{"batch_id":"dr20261023-120000-a1b2","published":true,
|
||||
"url":"/app/task-20261023-001/doc-review/?b=dr20261023-120000-a1b2",
|
||||
"ready":2,"needs_attention":[{"index":2,"status":"needs_corners","reason":"detect_failed"}]},
|
||||
"warnings":[]}
|
||||
```
|
||||
|
||||
- `review.published=false` 时没有 `url`,不要给用户审核入口。
|
||||
- `review.url` 是站内路径:Web 里直接输出成 Markdown 链接即可;微信/外部端需要拼上站点
|
||||
地址才能打开。审核页是拖拽页面,建议让用户在浏览器里用。
|
||||
- `review.ready` 是 `status=ready` 的页数;`needs_attention` 含 `review`、`needs_corners`、
|
||||
`needs_orientation`、`render_failed`、`failed`。
|
||||
|
||||
`warnings` 里常见的是:某页已旋转 90°、某页用兜底框出件、某页透视校正失败已重试、
|
||||
纸面短边偏低。这些表示“已出件但需确认”,按原话转述,不要自己判断“应该没事”。
|
||||
`checks.pages` 是逐页数值指标,用户追问时再展开,不必主动倒给用户。
|
||||
|
||||
## 审核页与数据
|
||||
|
||||
- 页面模板的单一事实源:`skills/document-photo-to-pdf/web/index.html`。
|
||||
- 运行时副本:会话 `.app/doc-review/index.html`(每次发布覆盖;不含任何数据注入)。
|
||||
- 缩略图:会话 `.app/doc-review/<batch_id>/p<idx>.jpg`(长边 1200,JPEG)。
|
||||
- 运行记录:会话 `data.db` 的 `doc_review_batch` / `doc_review_page` / `doc_review_job`。
|
||||
页面通过通用 `/api/db/<task_id>/query` 读,技能自己写;core 不认识这些表。
|
||||
|
||||
页面点「生成 PDF」的流程:
|
||||
|
||||
1. 把当前修改写成 `doc_review_job.edits`(完整快照,状态 `queued`);
|
||||
2. 调 `POST /api/sessions/<task_id>/skills/document-photo-to-pdf/run`,args 只传 `{"batch_id":"..."}`;
|
||||
3. 技能读 job → 应用 edits → 覆盖同名 PDF → 更新 batch/page/job;
|
||||
4. 页面重新读 `data.db`,刷新摘要、状态和 PDF 链接。
|
||||
|
||||
`edits` 的结构(`review/1`):
|
||||
|
||||
```json
|
||||
{"schema":"review/1",
|
||||
"batch":{"paper":"a4","enhance":"normal","dpi":"standard","orientation":"auto"},
|
||||
"pages":[{"i":3,"c":[[0.03,0.05],[0.97,0.04],[0.96,0.95],[0.04,0.96]],"r":90}],
|
||||
"order":[1,2,3],"deleted":[]}
|
||||
```
|
||||
|
||||
- `i` 永远是**原始 images 的 1-based 下标**,不随页序变化;
|
||||
- `c` 是归一化 0~1,相对 exif_transpose 后的原图,顺序左上、右上、右下、左下;
|
||||
- `r` 是该页的**最终顺时针旋转**(0/90/180/270),覆盖自动方向判断;不传则用自动结果;
|
||||
- `pages` 只需要放与自动基线不同的页(人工角点/旋转);未改的页由服务端重新检测/评级;
|
||||
- `order` 是出件页序,`deleted` 是删除的页;缺省按 images 原序。
|
||||
|
||||
技能每次出件会在 PDF 同目录写 `<名字>.state.json`(schema `document-photo-to-pdf/2`):
|
||||
`batch` 是本次四项参数,`pages[]` 记 `auto_corners`(原图像素)、`corners_norm`、
|
||||
`rotate`(自动基线)、`rotate_source`、`status`、`reason`。坐标一律归一化,换预览分辨率不会错位。
|
||||
|
||||
审核页发布的旧批次只保留最近 5 个;更早的缩略图目录会随下一次新批次自动清理。
|
||||
|
||||
## 内容安全
|
||||
|
||||
只做纯几何透视变换 + 全局等比提亮,不调用生成式模型,文字像素不会被重绘。
|
||||
|
||||
## 拍摄要求(失败时这样建议用户)
|
||||
|
||||
- 一次只拍一张纸,整张纸完整入画,四边各留 3%~5% 背景;
|
||||
- 桌面与纸张反差明显,避开强阴影和反光;
|
||||
- 同批材料尽量同方向拍摄;横竖混拍不会自动翻转,需要人确认方向。
|
||||
- 图片打不开:确认路径与格式(jpg/png/webp/bmp/tif)。
|
||||
- 检测不到纸张:整张纸入画,四边留 3%~5% 背景,换反差大的桌面。
|
||||
- 只有全部页都出不了才 `ok:false`;单页失败会用兜底框出件,审核页会标出来。
|
||||
|
||||
## 边界
|
||||
|
||||
- 纸张出画面的部分无法还原;角点可以外扩到画面外。
|
||||
- 只保证页面拉正,不保证文字朝上;自动方向只判 90°,180° 需要人在审核页旋转。
|
||||
- 检测不到角点的页用“画面内缩 3%”的兜底框出件,标 `needs_corners`,需人补角才是准确裁切。
|
||||
- 默认 `paper=auto` 会把同批页面统一到中位比例(偏差 20% 以内),因此各页尺寸与留白一致;
|
||||
偏差更大的页按原比例出件。要严格 A4 用 `paper=a4`。
|
||||
- 票据等窄纸建议单独成批,避免与 A4 混批时留白显得很宽。
|
||||
- 低分辨率照片会被放大到统一版式,不会因原图像素少而变小;清晰度仍受原图限制。
|
||||
- 数字比例只改纸面比例,PDF 画布仍是 A4;换画布请用 a4/letter/16k/b5。
|
||||
- 支持 jpg/png/webp/bmp/tif;HEIC 需要额外依赖,当前不支持。
|
||||
- 低分辨率、边缘背景残留、内容贴边只在 `warnings`/`checks` 里给指标,不会自动修正。
|
||||
- 深色纸、彩色纸、纸张与桌面颜色接近时,自动检测可能不稳,建议换对比明显的背景重拍。
|
||||
- 单次建议不超过 20 页;页数更多或原图很大时可能触发技能 120s 超时,可分批处理或调高
|
||||
`skills.timeout_ms`。
|
||||
- 一图只处理一张纸;同框多张纸只取最大的一张。
|
||||
- 纸出画面无法还原;角点可外扩。
|
||||
- 自动方向只判 90°,180° 在审核页旋转。
|
||||
- 默认按同批中位比例;严格 A4/16K/B5 在审核页选。
|
||||
- 支持 jpg/png/webp/bmp/tif;HEIC 不支持。
|
||||
- 一图一纸;同框多纸只取最大。
|
||||
- 建议 ≤20 页;更多或 300dpi 可能超 120s,可分批。
|
||||
|
||||
@@ -116,7 +116,7 @@ def write_state(path, data):
|
||||
def _review_mode(args):
|
||||
if args.get("batch_id") and "review" not in args:
|
||||
return "always"
|
||||
v = args.get("review", "off")
|
||||
v = args.get("review", "always")
|
||||
if isinstance(v, bool):
|
||||
return "always" if v else "off"
|
||||
s = str(v).strip().lower()
|
||||
@@ -492,7 +492,7 @@ def process(args, t0):
|
||||
|
||||
# ---------------- 批次方向
|
||||
aspects = [m["aspect"] for (_, _, m) in plan]
|
||||
batch_orientation, orientation_source, median_aspect = \
|
||||
batch_orientation, _, median_aspect = \
|
||||
compose.decide_orientation(aspects, orientation, P)
|
||||
dead = P["gate"]["aspect_deadband"]
|
||||
if batch_orientation in ("portrait", "landscape"):
|
||||
@@ -721,45 +721,24 @@ def process(args, t0):
|
||||
if m.get("status") in ("review", "needs_corners", "needs_orientation",
|
||||
"render_failed", "failed"):
|
||||
needs.append(m)
|
||||
ready = sum(1 for m in pages if m.get("status") == "ready")
|
||||
|
||||
# 发布审核页:always 总是发布;auto 只在有页需要人看时发布
|
||||
publish = review_mode == "always" or (review_mode == "auto" and bool(needs))
|
||||
url = None
|
||||
thumbs = {}
|
||||
if publish and batch_id:
|
||||
url, thumbs = review_page.publish(
|
||||
_, thumbs = review_page.publish(
|
||||
session_root, batch_id,
|
||||
[{"idx": m["index"], "source_abs": m.get("source_abs")}
|
||||
for m in pages
|
||||
if not m.get("deleted") and m.get("source_abs")])
|
||||
|
||||
# ---------------- state v2 + 返回体
|
||||
# ---------------- state v2
|
||||
order_all = sorted(pages, key=lambda m: (sort_map.get(m["index"], m["index"]),
|
||||
m["index"]))
|
||||
pages_out = []
|
||||
state_pages = []
|
||||
for m in order_all:
|
||||
src_rel = _rel(session_root, m.get("source_abs") or "")
|
||||
applied = int(m.get("rotated_deg") or 0)
|
||||
auto_r = int(m.get("auto_rotate") or 0)
|
||||
pages_out.append({
|
||||
"index": m["index"],
|
||||
"source": src_rel,
|
||||
"source_size": m.get("source_size"),
|
||||
"corners": m.get("corners"),
|
||||
"corners_norm": m.get("corners_norm"),
|
||||
"auto_corners": m.get("auto_corners"),
|
||||
"auto_corners_norm": m.get("auto_corners_norm"),
|
||||
"rotate": auto_r,
|
||||
"applied_rotate": applied,
|
||||
"status": m.get("status"),
|
||||
"reason": m.get("reason"),
|
||||
"warnings": m.get("warnings") or [],
|
||||
"out_size": m.get("out_size"),
|
||||
"page_orientation": m.get("page_orientation"),
|
||||
"error": m.get("error"),
|
||||
})
|
||||
state_pages.append({
|
||||
"index": m["index"],
|
||||
"source": src_rel,
|
||||
@@ -782,60 +761,16 @@ def process(args, t0):
|
||||
if not state_ok:
|
||||
warnings.append("state 文件写入失败:%s" % state_path)
|
||||
|
||||
canvas_set = {tuple(c) for c in ordered_canvases}
|
||||
canvas_px = list(next(iter(canvas_set))) if len(canvas_set) == 1 else None
|
||||
batch = {
|
||||
"paper": str(paper_input),
|
||||
"orientation": "mixed" if orientation == "none" else batch_orientation,
|
||||
"orientation_source": orientation_source,
|
||||
"median_aspect": round(float(batch_median), 4),
|
||||
"rotated_pages": [m["index"] for m in pages
|
||||
if int(m.get("rotated_deg") or 0) != 0],
|
||||
"canvas_px": canvas_px,
|
||||
}
|
||||
if canvas_px:
|
||||
margin = int(P["render"]["margin_px"])
|
||||
batch["content_px"] = [int(canvas_px[0] - 2 * margin),
|
||||
int(canvas_px[1] - 2 * margin)]
|
||||
|
||||
if checks.get("ok") is False:
|
||||
summary = "%d 页已生成;自检有未通过项,见 warnings" % len(ordered_pages)
|
||||
summary = "%d 页已生成;自检有未通过项" % len(ordered_pages)
|
||||
elif needs:
|
||||
summary = "%d 页已生成;%d 页需人工确认" % (len(ordered_pages), len(needs))
|
||||
summary = "%d 页已生成;%d 页建议在审核页确认" % (
|
||||
len(ordered_pages), len(needs))
|
||||
elif warnings:
|
||||
summary = "%d 页已生成;%d 项提示,请查看 warnings" % (
|
||||
summary = "%d 页已生成;%d 项提示,见审核页" % (
|
||||
len(ordered_pages), len(warnings))
|
||||
else:
|
||||
summary = "%d 页已生成;自检全部通过" % len(ordered_pages)
|
||||
|
||||
res = {
|
||||
"schema": P["schema"],
|
||||
"ok": True,
|
||||
"summary": summary,
|
||||
"output": {
|
||||
"path": output,
|
||||
"state": state_path,
|
||||
"bytes": int(os.path.getsize(output)),
|
||||
"pages": len(ordered_pages),
|
||||
"dpi": int(P["render"]["dpi"]),
|
||||
},
|
||||
"batch": batch,
|
||||
"pages": pages_out,
|
||||
"checks": checks,
|
||||
"warnings": warnings,
|
||||
"elapsed_ms": int((time.time() - t0) * 1000),
|
||||
}
|
||||
if use_db:
|
||||
res["review"] = {
|
||||
"batch_id": batch_id,
|
||||
"published": bool(publish),
|
||||
"url": url,
|
||||
"ready": ready,
|
||||
"needs_attention": [
|
||||
{"index": m["index"], "status": m.get("status"),
|
||||
"reason": m.get("reason")} for m in needs
|
||||
],
|
||||
}
|
||||
summary = "%d 页已生成" % len(ordered_pages)
|
||||
|
||||
# ---------------- 写 data.db
|
||||
if use_db:
|
||||
@@ -880,13 +815,13 @@ def process(args, t0):
|
||||
if old:
|
||||
review_page.prune_dirs(session_root, kept)
|
||||
|
||||
# 重生成路径给模型的回执要短:页面从 data.db 读详情,模型只需摘要/状态
|
||||
if batch_id_arg:
|
||||
res["pages"] = [{"index": m["index"], "status": m.get("status"),
|
||||
"reason": m.get("reason")} for m in pages_out]
|
||||
res.pop("checks", None)
|
||||
res["compact"] = True
|
||||
return res
|
||||
# AI 侧只回三件事:是否成功、一句摘要、PDF 文件名。
|
||||
# 完整访问地址由 core 的 visit_url 追加;pages/checks/批次 ID 都在 data.db/state 里。
|
||||
return {
|
||||
"ok": True,
|
||||
"summary": summary,
|
||||
"pdf": _rel(session_root, output),
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
_mark_error(conn, batch_id, e)
|
||||
@@ -901,21 +836,11 @@ def process(args, t0):
|
||||
|
||||
def finish_failure(pages, failures, output, state_path, t0):
|
||||
err = failures[0] if failures else _err("INTERNAL", "未知错误")
|
||||
warnings = []
|
||||
for m in pages:
|
||||
warnings.extend(m.get("warnings", []))
|
||||
res = {
|
||||
"schema": P["schema"],
|
||||
return {
|
||||
"ok": False,
|
||||
"summary": "失败:%s" % err.get("message", "未知错误"),
|
||||
"error": err,
|
||||
"pages": pages,
|
||||
"warnings": warnings,
|
||||
"elapsed_ms": int((time.time() - t0) * 1000),
|
||||
}
|
||||
if output:
|
||||
res["output"] = {"path": output, "state": state_path}
|
||||
return res
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- 入口
|
||||
@@ -941,13 +866,9 @@ def main():
|
||||
hint = ("检查参数后重试" if code == "BAD_ARGS"
|
||||
else "可用 DOC_DESKEW_DEBUG=1 重跑查看堆栈")
|
||||
res = {
|
||||
"schema": P["schema"],
|
||||
"ok": False,
|
||||
"summary": "失败:%s" % e,
|
||||
"error": _err(code, str(e), None, hint),
|
||||
"pages": [],
|
||||
"warnings": [],
|
||||
"elapsed_ms": int((time.time() - t0) * 1000),
|
||||
}
|
||||
if os.environ.get("DOC_DESKEW_DEBUG"):
|
||||
res["logs"] = traceback.format_exc().splitlines()
|
||||
|
||||
@@ -90,6 +90,7 @@ body{display:flex;flex-direction:column;height:100%;height:100dvh}
|
||||
background:#1b2536;color:#e8edf5;font-size:15px}
|
||||
.sheet .row button.pri{background:#1f6feb;color:#fff}
|
||||
.hint{color:#6f8098;font-size:11px;margin:12px 0 0}
|
||||
#warnings div{margin-top:4px;line-height:1.45}
|
||||
|
||||
#toast{position:fixed;left:50%;bottom:calc(20px + env(safe-area-inset-bottom));
|
||||
transform:translateX(-50%);background:rgba(24,34,50,.96);color:#e8edf5;
|
||||
@@ -199,6 +200,7 @@ body{display:flex;flex-direction:column;height:100%;height:100dvh}
|
||||
</div>
|
||||
</div>
|
||||
<p id="summary" class="hint"></p>
|
||||
<div id="warnings" class="hint"></div>
|
||||
<div class="row"><button id="pdf" class="pri">下载当前 PDF</button></div>
|
||||
<div class="row"><button id="setClose" class="pri">完成</button></div>
|
||||
</div>
|
||||
@@ -251,6 +253,21 @@ function paintSummary() {
|
||||
var b = S.batch || {};
|
||||
var el = document.getElementById("summary");
|
||||
if (el) el.textContent = b.summary || "";
|
||||
var w = parseJSON(b.warnings, []);
|
||||
var we = document.getElementById("warnings");
|
||||
if (we) {
|
||||
we.innerHTML = "";
|
||||
(w || []).slice(0, 12).forEach(function(t){
|
||||
var d = document.createElement("div");
|
||||
d.textContent = "· " + t;
|
||||
we.appendChild(d);
|
||||
});
|
||||
if (w && w.length > 12) {
|
||||
var more = document.createElement("div");
|
||||
more.textContent = "· 还有 " + (w.length - 12) + " 条";
|
||||
we.appendChild(more);
|
||||
}
|
||||
}
|
||||
var pdf = document.getElementById("pdf");
|
||||
if (pdf) {
|
||||
if (b.output) {
|
||||
@@ -680,10 +697,18 @@ function paintChrome() {
|
||||
if (!S.pages.length) return;
|
||||
var p = S.pages[S.cur];
|
||||
if (!p) return;
|
||||
var badge = "";
|
||||
if (p.deleted) badge = "已标记删除";
|
||||
else if (p.status === "needs_corners") badge = "需补角";
|
||||
else if (p.status === "needs_orientation") badge = "方向存疑";
|
||||
else if (p.status === "review") badge = "建议确认";
|
||||
else if (p.status === "render_failed") badge = "出页失败";
|
||||
else if (p.status === "failed") badge = "图片不可读";
|
||||
if (!badge && !p.img) badge = "图片不可读";
|
||||
document.getElementById("cur").textContent = S.cur + 1;
|
||||
document.getElementById("total").textContent = S.pages.length;
|
||||
document.getElementById("name").textContent = p.name +
|
||||
(p.deleted ? "(已标记删除)" : (!p.img ? "(图片不可读)" : ""));
|
||||
(badge ? "(" + badge + ")" : "");
|
||||
document.getElementById("prev").disabled = (S.cur === 0);
|
||||
document.getElementById("navPrev").disabled = (S.cur === 0);
|
||||
document.getElementById("next").disabled = (S.cur === S.pages.length - 1);
|
||||
@@ -852,6 +877,9 @@ function toast(msg) {
|
||||
|
||||
/* ---------------------------------------------------------------- 启动 */
|
||||
function boot() {
|
||||
/* 手机上先看校正预览:打开链接第一眼就是成品效果;点左上角再切到裁切。 */
|
||||
var mobile = window.matchMedia("(max-width:899.98px)").matches;
|
||||
if (mobile) document.body.classList.add("view-prev");
|
||||
document.getElementById("total").textContent = S.pages.length;
|
||||
loadImages();
|
||||
|
||||
@@ -888,6 +916,7 @@ function boot() {
|
||||
|
||||
/* --- 移动端:原图 / 预览 二选一,按钮在左上角。桌面端两个都显示,按钮隐藏 --- */
|
||||
var viewBtn = document.getElementById("view");
|
||||
if (mobile) viewBtn.innerHTML = ICON_CROP;
|
||||
viewBtn.onclick = function(){
|
||||
var on = document.body.classList.toggle("view-prev");
|
||||
viewBtn.innerHTML = on ? ICON_CROP : ICON_EYE;
|
||||
|
||||
Reference in New Issue
Block a user