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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
- 上传底纸后,纸张设置会直接显示从照片读到的制造商、设备型号、镜头和 ISO,缺少的项目仍可手动补填。

- MCP 新检查单记录首次入档时的稿件版本,历史查单会说明单据能证明的先后关系。
- MCP 首次使用时会按材料介绍打印页、原纸填字和透明字迹。客户端可在导出时取回 512 KiB 以内的 PDF 或 ZIP,用作聊天附件。
- MCP 调参兼容直接指定控件或按控件组提交。
- MCP 客户端可在写入时按需取回一至两个笔迹变化控件的有效旧值和新值。
- 可用 `PANACHE_MCP_PRESERVE_REVIEWS=1` 在改稿和打印期间保留原有检查记录。
- MCP 在多节稿件的逐页复盘中提示节号和节内页码,方便从中断处继续核对。
- MCP 可按稿件版本、节号和页码只读找回已存的整页复盘;缺少记录时明确返回空结果。
Expand Down
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,15 @@ Offline downloads are available for Windows x64, macOS on Apple silicon, Linux x

## Local MCP for AI clients

The Windows, macOS, and Linux desktop packages include a local MCP server. Open **App settings → Connect AI client** and copy the configuration into a client that supports stdio MCP. Try asking:
The Windows, macOS, and Linux desktop packages include a local MCP server. In **App settings → Connect AI client**, copy the configuration, then add Panache in the MCP settings of an AI client that supports local stdio servers. Reload the client’s MCP settings and ask it to call Panache to list the installed handwriting fonts. The connection works if the Panache tool returns font names.

**Don’t see Connect AI client?** It is not available in the web or mobile app. On a supported desktop, make sure you installed or extracted the whole desktop package, not just the app executable. See the [desktop setup notes](crates/panache-mcp/README.md) for platform details.

Once connected, try asking:

> Write my notes and the process diagram on this photo of my stationery. Show me the first page, then export the whole draft as a PDF.

For the photo, give the client a file path accessible on the computer running Panache. A client that passes images to its model can inspect the finished page and review a selected area. In a text-only client, ask for the measurements and open the exported PDF locally to judge the appearance. [MCP tools and configuration](crates/panache-mcp/README.md)
You can also start with just text. Paste a passage and say whether you want a printable page, writing on a photo of your worksheet, or transparent ink to paste elsewhere. For a photo, the client needs a way to make it available on the computer running Panache. [MCP tools and configuration](crates/panache-mcp/README.md)

| Process state diagram | Resource allocation table |
| --- | --- |
Expand Down
8 changes: 6 additions & 2 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,15 @@ Panache 能把正文、公式、表格和电路图排成手写稿。导入文档

## 用 AI 客户端生成手写稿

Windows、macOS 和 Linux 桌面版随附本地 MCP 服务。在 **应用设置 → 连接 AI 客户端** 复制配置,加入支持 stdio MCP 的客户端,就可以对它说:
Windows、macOS 和 Linux 桌面版随附本地 MCP 服务。在 **应用设置 → 连接 AI 客户端** 复制配置,再到支持本地 stdio 服务的 AI 客户端的 MCP 设置里添加 Panache。重新加载客户端的 MCP 配置后,对它说:「请调用 Panache 列出已安装的手写字体。」Panache 工具返回字体名,才说明连上了。

**设置里没有「连接 AI 客户端」?** 网页版和手机版没有这个入口。如果用的是支持的桌面版,请确认已完整安装或解压桌面包,而不是只复制了应用的可执行文件。各系统的注意事项见[桌面连接说明](crates/panache-mcp/README.md)。

连上后,可以对它说:

> 用这张斜拍的信纸排我的笔记,画出进程状态转换图。先给我看第一页,确认后把整份导出成 PDF。

请提供运行 Panache 的电脑能读取的照片路径。能向模型传递图片的客户端可以检查成品、放大复盘选区。在纯文字客户端中,可让 AI 说明测量结果,再打开导出的本地 PDF 看实际效果。[MCP 工具与配置说明](crates/panache-mcp/README.md)
只有文字时也能开始。把一段作业贴给客户端,告诉它想打印成纸页、填在自己的纸张照片上,还是只要能贴到别处的透明字迹。用照片时,客户端需要能把照片交给运行 Panache 的电脑。[MCP 工具与配置说明](crates/panache-mcp/README.md)

| 进程状态转换图 | 资源分配表 |
| --- | --- |
Expand Down
16 changes: 14 additions & 2 deletions crates/panache-mcp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
2. 在支持**本地 stdio 服务**的 AI 客户端中注册名为 `panache` 的服务。使用 `mcpServers` 格式的 JSON 配置可直接合并其中的 `panache` 条目,保留已有服务。若客户端使用表单、TOML 或其他格式,按其格式填入复制结果中的 `command` 和可选的 `args`。程序路径填作一项,每个参数各占一项。
3. 在客户端重新加载 MCP 配置或重新连接,问它「请调用 Panache 列出可用字体」。收到本机的字体文件名就表示连接成功。若客户端只接受服务器网址,请选择能在本机启动 stdio 程序的客户端。

**连不上时,先看卡在哪一步。** 如果桌面版一打开就提示「内置手写引擎无法运行」,这是进入设置前的启动问题,和是否在 AI 客户端填写了配置无关。记下 Panache 版本和系统版本。若配置已复制,但 AI 客户端没有收到 Panache 工具返回的字体名,请查看客户端的 MCP 启动日志。若提示找不到文件,从当前安装重新复制配置;若提示系统库版本不匹配,请记下完整错误,改 JSON 不会解决。

客户端通过标准输入输出启动 Panache,关闭工作台后也能使用。移动安装目录后可重新复制。

桌面包包含 MCP、字体和 Markdown 转换器,JavaScript 运行时已编入 MCP 程序。Windows 新安装使用根目录启动器,可自动跟随应用更新;从旧启动器升级的安装会使用当前版本的 MCP 路径,更新后请重新复制。
Expand Down Expand Up @@ -44,11 +46,19 @@ cargo build --locked --release -p panache-mcp

## 写稿、预览和复查

第一次可以直接说:「把‘西湖春早,柳色正新。’排成一页手写稿,用默认纸张,先让我看一眼。」客户端会用 `save_section` 建稿,再把返回的 `draft_id` 和 `revision` 交给 `preview_handwriting`。满意后可以说「把刚才那一版导出成 PDF」。导出工具返回本机文件路径。
只有一句「帮我做一份手写作业」时,先贴要写的内容。想打印,就用普通纸样做 PDF;想填在老师发的练习纸上,就用那张纸的照片;想把字放进海报,就用透明字迹,解开 ZIP 后取出 PNG。已经说清「用这段话做打印版」就可以直接开始。

稿件会原样保存嵌入的图片,正文里的插图会画成手绘线稿,无法保留原彩。颜色很重要时,请先看这一页的小样,别把插图改成整页底纸。

可以先说「用这段话给我看第一页」,看过后说「字大一点」,再说「就用这一版」。客户端按同一稿件的版本号取预览和导出。查看小样使用 `preview_handwriting(view="image")`,无需创建检查单。

想在聊天里拿到文件时,能处理 MCP 内嵌文件的客户端可在导出时传 `include_file=true`,把收到的 PDF 或 ZIP 作为附件显示给用户。512 KiB 以内的完整文件可以随回复传送。更大的文件仍在运行 Panache 的电脑上;两端共用磁盘时可直接打开返回的路径。聊天客户端给出用户可打开的下载链接时,直接把原链接交给用户;确认已展示附件但没有链接时,告诉用户在聊天附件中取件。`file_transfer.status=included` 表示工具回复带有文件内容。纯文字窗口或尚无客户端确认时,说明文件已导出,并按本机路径给出取件位置;这个路径和内嵌资源 URI 都不能当作下载链接。

客户端从内嵌资源取出文件后,应由客户端生成自己的下载入口,不依赖模型在回复里拼链接。资源里的 `panache-export:///…` 仅用于标识,不是下载地址。把交付状态和已经可用的下载入口交给模型即可,不要把文件的 Base64 内容放进模型上下文或普通日志。

使用自己的纸张时,在第一次调用 `save_section` 时传入 `paper_path`,例如 `{"markdown":"第一题","paper_path":"C:/作业/南昌大学横线纸.jpg"}`。路径指向运行 MCP 的电脑上的图片。默认保留原照片,按识别到的横线投影字迹和插图。`paper` 返回排版尺寸、边距、行距、识线结果,投影时另有原照尺寸。可用 `preview_handwriting(view="paper")` 单独看底纸,或在导入时通过 `paper_guides` 指定参考线。需要校平底纸时传 `paper_mode="flatten"`。无可靠横线时保留底图,使用普通行距。纸张随稿件保存一次,后续追加、回查、预览和导出可在原图移动后继续使用。

如果照片是聊天附件,请先确认客户端能提供 MCP 进程可读的本机文件路径,必要时先把图片存到这台电脑。带视觉能力的模型可以查看客户端转交的预览图。客户端或模型只能处理文字时,可读取版面和像素测量,并请用户打开导出文件确认外观,再按反馈修改。
如果照片是聊天附件,先用客户端支持的文件传送功能,把照片交给 Panache。云端没有这项功能时,可在照片所在电脑连接桌面版再使用。复制手机或另一台电脑上的路径不能传照片。带视觉能力的模型可以查看客户端转交的预览图。客户端或模型只能处理文字时,可读取版面和像素测量,并请用户打开导出文件确认外观,再按反馈修改。

| 工具 | 用法 |
| --- | --- |
Expand Down Expand Up @@ -118,6 +128,8 @@ cargo build --locked --release -p panache-mcp

如果字像贴在照片上,在照片整页复查的 `photo_appearance` 中取 `preview_arguments` 再调用一次 `preview_handwriting`。有页内文字基线样本时,这组参数会给出该版本一处字迹附近的原像素底纸和成品裁图;也可以自己圈选。对照邻近印字或横线的边缘清晰度、墨色深浅,再看字号与可写空间是否相称。用 `describe_controls` 指定稿件、版本和 `ink`、`layout` 或 `typography` 参数组可查当前值。正文的柔化和粗细以投影前纸面像素为单位,透明度用百分比。仅修改墨迹组参数时会保留已完成的单字移位和透视调整;更换字体、字号或参考线后应在新版本重新检查选区。改一版后,对同一选区加入 `compare_revision` 比较。只有文字结果的客户端可转述测量,请用户看图判断观感。

客户端已知目标值时,可在 `tune_handwriting` 中同时传 `changes={"perturb_theta_sigma":0.07}` 和 `readback_variation=["perturb_theta_sigma"]`。成功回执的 `effective_changes` 给出写入前后的有效值,可省去写前、写后各一次查询。最多点名本次提交的两个 `variation` 控件。提交 `null` 时读回继承值,有效值相同的键会省略。若要根据旧值决定改多少,仍应先用 `describe_controls` 读取该稿当前版本的值。

需要保留手动移字时,在 `tune_handwriting` 顶层传入 `preserve_local_adjustments=true`。例如,稿件中仍有字形调整时,更改 `variation.perturb_theta_sigma` 会返回一个受影响的节号,稿件停留在原版本。只更改墨迹或相机元数据仍可保存。被拒绝后应保持 `true`。只有用户明确同意新布局不再应用这些手动调整时,才可传 `false`;旧版仍可回看。

例如,先前已确认行距 56、左边距 67,后来要求恢复导入时的参考线并保留这两个数值。在同一次调用中传 `paper_guides=[]` 和 `changes={"line_spacing":56,"left_margin":67}`;想让文字重新跟随底纸时,省略相应数值。如果之前摆好的字也必须留在原位,再传 `preserve_local_adjustments=true`。补回行距和边距仍会触发重排;若保护生效,整次请求停在原版本。
Expand Down
95 changes: 95 additions & 0 deletions crates/panache-mcp/src/delivery.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
use crate::inputs::Format;
use base64::{Engine as _, engine::general_purpose::STANDARD};
use rmcp::model::{CallToolResult, ContentBlock, ResourceContents};
use serde_json::json;
use std::{
fs::File,
io::{self, Read, Seek},
path::Path,
};

// Applied to the file before base64 expands it by about a third.
const MAX_FILE_BYTES: u64 = 512 * 1024;

fn file_bytes(mut file: File) -> io::Result<Option<Vec<u8>>> {
if file.metadata()?.len() > MAX_FILE_BYTES {
return Ok(None);
}
file.rewind()?;
let mut bytes = Vec::new();
file.take(MAX_FILE_BYTES + 1).read_to_end(&mut bytes)?;
Ok((bytes.len() as u64 <= MAX_FILE_BYTES).then_some(bytes))
}

pub(super) fn include_file(result: &mut CallToolResult, file: File, path: &Path, format: Format) {
// Keep the published handle. Reopening a pathname could read a replacement file.
let (status, message) = match file_bytes(file) {
Ok(Some(bytes)) => {
let mime = match format {
Format::Pdf => "application/pdf",
Format::Zip => "application/zip",
};
let name = path.file_name().unwrap_or_default().to_string_lossy();
let resource =
ResourceContents::blob(STANDARD.encode(bytes), format!("panache-export:///{name}"))
.with_mime_type(mime);
result.content.push(ContentBlock::resource(resource));
(
"included",
"文件内容已写入工具回复。聊天客户端给出用户可用的下载链接时,在答复中原样贴出;只有聊天客户端确认附件已展示且未提供链接时,才请用户打开附件。没有这项确认时,请告知文件已导出,并用纯文本给出运行 Panache 的电脑上的取件路径。",
)
}
Ok(None) => (
"too_large",
"文件已生成,超过随回复传送的 512 KiB。请从运行 Panache 的电脑取回。",
),
Err(_) => (
"unavailable",
"文件已生成,但本次回复未能读取文件内容。请从运行 Panache 的电脑取回。",
),
};
result.content.push(ContentBlock::text(message));
if let Some(metadata) = &mut result.structured_content {
metadata["file_transfer"] = json!({"status": status, "max_bytes": MAX_FILE_BYTES});
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn unreadable_bytes_preserve_export_receipt() -> io::Result<()> {
let dir = tempfile::tempdir()?;
let path = dir.path().join("sample.pdf");
let write_only = File::create(&path)?;
let mut result = crate::reply("exported".into(), json!({"pages": 1}));
include_file(&mut result, write_only, &path, Format::Pdf);
assert_eq!(
result.structured_content.as_ref().unwrap()["file_transfer"]["status"],
"unavailable"
);
assert_eq!(result.structured_content.as_ref().unwrap()["pages"], 1);
assert!(
result
.content
.iter()
.all(|block| matches!(block, ContentBlock::Text(_)))
);
assert!(path.is_file());
Ok(())
}

#[test]
fn attachment_size_boundary() -> io::Result<()> {
for size in [MAX_FILE_BYTES, MAX_FILE_BYTES + 1] {
let file = tempfile::tempfile()?;
file.set_len(size)?;
assert_eq!(
file_bytes(file)?.map(|b| b.len() as u64),
(size == MAX_FILE_BYTES).then_some(size)
);
}
Ok(())
}
}
13 changes: 12 additions & 1 deletion crates/panache-mcp/src/inputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,17 @@ pub struct Guide {
#[derive(Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct Save {
/// Optional absolute local image path for a new draft. Imports and aligns the paper once.
/// Optional absolute path to an image file readable on the computer running Panache. Imports and aligns the paper once.
/// A chat attachment or URL alone is not a file path. If the client cannot transfer the photo to that computer, explain this instead of guessing a path.
/// Use with the default plain preset. All revisions share the saved image.
pub paper_path: Option<std::path::PathBuf>,
/// With paper_path: photo (default) preserves camera perspective; flatten aligns the background.
pub paper_mode: Option<PaperMode>,
/// Optional photo-space baselines overriding auto detection on import. Give 2..32 guides, first row 0, last row the last writable line; intermediate guides refine the fit.
pub paper_guides: Option<Vec<Guide>>,
/// Original Markdown of just this section. A new section starts on a fresh page.
/// Embedded images remain intact in the source. Pages render them as hand-drawn outlines without their original colors.
/// If color matters, explain this and offer a preview. Keep the image in the text rather than using it as the paper.
pub markdown: String,
/// Omit to create a draft. Supply the returned ID to append or edit.
pub draft_id: Option<String>,
Expand All @@ -112,6 +115,10 @@ pub struct Describe {
#[derive(Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct Tune {
/// Request before/after values for up to two variation keys in changes. effective_changes omits keys whose effective value stayed the same.
#[serde(default)]
#[schemars(length(max = 2))]
pub readback_variation: Vec<String>,
/// Set to true only when the user wants to preserve manual glyph adjustments. Reject reflow that would stop applying them. Ink and camera changes can still be saved.
#[serde(default)]
pub preserve_local_adjustments: bool,
Expand Down Expand Up @@ -271,6 +278,10 @@ pub struct Export {
pub revision: Option<u32>,
/// PDF includes the paper. ZIP contains page PNGs and respects the transparent_background ink control.
pub format: Option<Format>,
/// Include the exported bytes as an embedded MCP resource for clients that can offer attachments.
/// Up to 512 KiB before base64 encoding. Larger files stay available at the returned server path.
#[serde(default)]
pub include_file: bool,
}

#[derive(Clone, Copy, Deserialize, JsonSchema, PartialEq, serde::Serialize)]
Expand Down
Loading