File size: 3,655 Bytes
b4f2238
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
"""文书生产 MCP tools"""

from __future__ import annotations

import base64
from typing import Any

from mcp_server.client import client


def list_document_templates(
    template_type: str | None = None,
    case_type: str | None = None,
    is_active: bool | None = None,
) -> list[dict[str, Any]]:
    """获取文件模板列表,支持按类型/案件类型/启用状态过滤。"""
    params: dict[str, Any] = {}
    if template_type:
        params["template_type"] = template_type
    if case_type:
        params["case_type"] = case_type
    if is_active is not None:
        params["is_active"] = is_active
    return client.get("/documents/templates", params=params)  # type: ignore[no-any-return]


def get_document_template(template_id: int) -> dict[str, Any]:
    """获取文件模板详情。"""
    return client.get(f"/documents/templates/{template_id}")  # type: ignore[no-any-return]


def create_document_template(
    name: str,
    template_type: str,
    case_type: str = "",
    content: str = "",
) -> dict[str, Any]:
    """创建文件模板。"""
    payload: dict[str, Any] = {"name": name, "template_type": template_type}
    if case_type:
        payload["case_type"] = case_type
    if content:
        payload["content"] = content
    return client.post("/documents/templates", json=payload)  # type: ignore[no-any-return]


def list_folder_templates(
    template_type: str | None = None,
    case_type: str | None = None,
    is_active: bool | None = None,
) -> list[dict[str, Any]]:
    """获取文件夹模板列表。"""
    params: dict[str, Any] = {}
    if template_type:
        params["template_type"] = template_type
    if case_type:
        params["case_type"] = case_type
    if is_active is not None:
        params["is_active"] = is_active
    return client.get("/documents/folder-templates", params=params)  # type: ignore[no-any-return]


def list_placeholders(is_active: bool | None = None) -> list[dict[str, Any]]:
    """获取替换词列表。"""
    params: dict[str, Any] = {}
    if is_active is not None:
        params["is_active"] = is_active
    return client.get("/documents/placeholders", params=params)  # type: ignore[no-any-return]


def preview_placeholders(
    contract_id: int,
    keys: str = "",
) -> dict[str, Any]:
    """预览合同的替换词值。keys: 逗号分隔的键名,为空则返回全部。"""
    params: dict[str, Any] = {}
    if keys:
        params["keys"] = keys
    return client.get(f"/documents/placeholders/preview/{contract_id}", params=params)  # type: ignore[no-any-return]


def preview_contract_context(contract_id: int) -> dict[str, Any]:
    """预览合同占位符上下文数据。"""
    return client.get(f"/documents/contracts/{contract_id}/preview")  # type: ignore[no-any-return]


def download_contract_document(contract_id: int) -> dict[str, Any]:
    """下载合同文档(DOCX)。返回 {filename, content_type, data_base64}。"""
    content, filename, content_type = client.download(
        f"/documents/contracts/{contract_id}/download"
    )
    return {
        "filename": filename,
        "content_type": content_type,
        "data_base64": base64.b64encode(content).decode(),
    }


def download_contract_folder(contract_id: int) -> dict[str, Any]:
    """下载合同文件夹(ZIP)。返回 {filename, content_type, data_base64}。"""
    content, filename, content_type = client.download(
        f"/documents/contracts/{contract_id}/folder/download"
    )
    return {
        "filename": filename,
        "content_type": content_type,
        "data_base64": base64.b64encode(content).decode(),
    }