@hfyjq/crannia 1.1.2 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -201
- package/NOTICE +48 -45
- package/README.md +1 -1
- package/THIRD-PARTY-FRONTEND.md +5266 -5266
- package/THIRD-PARTY-PYTHON-linux.md +174 -0
- package/THIRD-PARTY-PYTHON.md +176 -162
- package/THIRD-PARTY.md +323 -316
- package/apps/desktop/quotes.txt +56 -56
- package/bus/core.py +677 -671
- package/bus/loaders/channel_loader.py +64 -64
- package/bus/loaders/provider_loader.py +4 -7
- package/bus/loaders/skill_loader.py +46 -27
- package/bus/loaders/skill_md.py +9 -9
- package/bus/mcp_disabled.py +4 -6
- package/bus/quarantine.py +10 -13
- package/bus/registry.py +2315 -2284
- package/bus/skill_state.py +124 -124
- package/channels/cli_channel/handler.py +3 -3
- package/channels/cli_channel/manifest.yaml +5 -5
- package/channels/dingtalk_channel/handler.py +32 -32
- package/channels/dingtalk_channel/manifest.yaml +12 -12
- package/channels/feishu_channel/handler.py +33 -33
- package/channels/feishu_channel/manifest.yaml +12 -12
- package/channels/qq_bot_channel/__init__.py +1 -1
- package/channels/qq_bot_channel/handler.py +24 -24
- package/channels/qq_bot_channel/manifest.yaml +4 -4
- package/channels/telegram_channel/__init__.py +1 -1
- package/channels/telegram_channel/handler.py +21 -21
- package/channels/telegram_channel/manifest.yaml +4 -4
- package/channels/telegram_channel/media.py +231 -231
- package/channels/textfmt.py +58 -58
- package/channels/wechat_channel/__init__.py +1 -1
- package/channels/wechat_channel/converter.py +1 -1
- package/channels/wechat_channel/handler.py +8 -8
- package/channels/wechat_channel/manifest.yaml +5 -5
- package/channels/wechat_channel/media.py +178 -178
- package/core/activity.py +753 -709
- package/core/agent_loop.py +7673 -7522
- package/core/allowlist.py +2 -3
- package/core/approval.py +34 -24
- package/core/approval_timeout.py +256 -228
- package/core/artifacts.py +3 -3
- package/core/attachment_gc.py +21 -0
- package/core/auto_review.py +2 -2
- package/core/browser/cdp.py +5 -2
- package/core/browser/discover.py +55 -19
- package/core/browser/session.py +4 -4
- package/core/cache_break_detector.py +97 -0
- package/core/cache_breakpoints.py +103 -0
- package/core/cache_policies.py +255 -0
- package/core/cancellation.py +3 -3
- package/core/channel_configs.py +3 -3
- package/core/channel_turns.py +13 -13
- package/core/child_approval.py +2 -3
- package/core/code_intel.py +11 -11
- package/core/command_policy.py +13 -7
- package/core/computer/annotate.py +231 -11
- package/core/computer/capture.py +140 -1
- package/core/computer/guard.py +7 -4
- package/core/computer/input.py +452 -26
- package/core/computer/session.py +563 -381
- package/core/computer/tools.py +2170 -928
- package/core/computer/uia.py +100 -1
- package/core/computer/watch.py +1 -1
- package/core/computer/win32.py +302 -7
- package/core/config_requirements.py +205 -205
- package/core/cron/runner.py +4 -4
- package/core/daily_log.py +2 -2
- package/core/dedup.py +2 -2
- package/core/egress_channels.py +326 -0
- package/core/egress_gate.py +96 -8
- package/core/egress_preapproved.py +59 -3
- package/core/endpoint.py +141 -141
- package/core/events.py +17 -8
- package/core/extension.py +2 -2
- package/core/file_history.py +10 -10
- package/core/file_tools.py +3 -3
- package/core/github_config.py +1 -1
- package/core/group_digest.py +3 -4
- package/core/group_minutes.py +5 -5
- package/core/group_reply.py +24 -0
- package/core/groups.py +5 -9
- package/core/hooks.py +15 -15
- package/core/i18n.py +2 -2
- package/core/ice/canary.py +3 -3
- package/core/ice/net_heuristics.py +2 -2
- package/core/im_routing.py +2 -2
- package/core/image_input.py +183 -43
- package/core/inbound.py +3 -3
- package/core/jobs.py +6 -6
- package/core/last_good.py +150 -150
- package/core/leaf_md.py +3 -3
- package/core/llm_errors.py +36 -27
- package/core/mail_config.py +2 -2
- package/core/memory/compressor.py +8 -8
- package/core/memory/manager.py +30 -45
- package/core/memory/session_memory.py +1 -2
- package/core/memory/short_term.py +4 -4
- package/core/memory_extract.py +1 -1
- package/core/memory_organize.py +8 -8
- package/core/memory_store.py +7 -9
- package/core/memory_transfer.py +2 -2
- package/core/model_capabilities.py +2 -2
- package/core/model_configs.py +219 -68
- package/core/model_profiles.py +333 -0
- package/core/modes.py +12 -11
- package/core/net_proxy.py +3 -3
- package/core/pipeline/scheduler.py +48 -43
- package/core/pipeline/stage.py +3 -0
- package/core/pipeline/stages/agent_loop.py +36 -15
- package/core/pipeline/stages/group_reply.py +22 -20
- package/core/pipeline/stages/post_process.py +7 -7
- package/core/pipeline/stages/pre_process.py +360 -68
- package/core/prompt_assembler.py +13 -4
- package/core/recycle.py +227 -16
- package/core/response_language.py +1 -1
- package/core/retention.py +2 -2
- package/core/rg.py +4 -3
- package/core/security.py +26 -8
- package/core/services.py +8 -8
- package/core/session_model.py +3 -5
- package/core/session_pool.py +35 -5
- package/core/session_recap.py +40 -3
- package/core/session_workspace.py +2 -3
- package/core/settings_baseline.py +2 -2
- package/core/settings_transfer.py +25 -1
- package/core/shell.py +52 -20
- package/core/side_call.py +3 -3
- package/core/skill_config.py +164 -0
- package/core/skill_factory.py +2 -2
- package/core/skill_guidance.py +2 -4
- package/core/skill_index.py +3 -4
- package/core/skill_settings.py +1 -1
- package/core/skill_sources.py +7 -7
- package/core/ssh_config.py +129 -0
- package/core/state_registry.py +67 -5
- package/core/subagent.py +28 -5
- package/core/subagent_defs.py +3 -3
- package/core/summarizer.py +4 -5
- package/core/tasks_notify.py +6 -6
- package/core/tiers.py +2 -2
- package/core/todos.py +1 -1
- package/core/token_calibrator.py +1 -4
- package/core/tool_outcome.py +108 -108
- package/core/touched_files.py +1 -1
- package/core/trace.py +12 -3
- package/core/unlock.py +4 -4
- package/core/update_tui.py +184 -158
- package/core/updater.py +5 -3
- package/core/version.py +14 -14
- package/core/vision_budget.py +235 -0
- package/core/web_cache.py +152 -152
- package/core/web_request.py +474 -0
- package/core/window_estimate.py +1 -1
- package/core/workspace.py +56 -26
- package/crannia_tray.py +278 -278
- package/licenses/GPL-3.0.txt +619 -619
- package/licenses/LGPL-2.1.txt +502 -502
- package/licenses/LGPL-3.0.txt +165 -165
- package/licenses/MIT-chat-on-wechat.txt +18 -18
- package/licenses/MIT-deepseek-harness.txt +21 -21
- package/licenses/Molio-LICENSE.txt +44 -44
- package/locales/en.json +1876 -1755
- package/main.py +154 -154
- package/mcpx/client.py +12 -1
- package/mcpx/manager.py +530 -522
- package/mcpx/servers/web_search.py +5 -13
- package/package.json +15 -23
- package/providers/anthropic/client.py +35 -33
- package/providers/base.py +13 -13
- package/providers/cache_policies.json +294 -0
- package/providers/cost_table.json +3779 -3779
- package/providers/lmstudio/client.py +2 -2
- package/providers/model_capabilities.json +6665 -6665
- package/providers/model_profiles.json +629 -0
- package/providers/ollama/client.py +2 -2
- package/providers/openai_compat.py +37 -16
- package/providers/openrouter/client.py +2 -2
- package/providers/qwen/__init__.py +1 -1
- package/providers/qwen/client.py +2 -2
- package/providers/qwen/manifest.yaml +16 -16
- package/providers/rate_limiter.py +2 -2
- package/providers/siliconflow/client.py +2 -2
- package/providers/validation.py +171 -171
- package/providers/vision_profiles.json +89 -0
- package/providers/window_table.json +8909 -8909
- package/requirements.txt +59 -51
- package/sbom-linux-x64.json +4580 -0
- package/sbom.json +4600 -4413
- package/skills/diagram/SKILL.md +47 -0
- package/skills/diagram/__init__.py +1 -0
- package/skills/diagram/handler.py +294 -0
- package/skills/diagram/layout.py +174 -0
- package/skills/diagram/manifest.yaml +106 -0
- package/skills/diagram/mermaid.py +170 -0
- package/skills/diagram/svg.py +223 -0
- package/skills/entertainment/SKILL.md +35 -35
- package/skills/entertainment/__init__.py +1 -1
- package/skills/entertainment/handler.py +91 -91
- package/skills/entertainment/manifest.yaml +26 -26
- package/skills/file_parser/GUIDANCE.md +10 -10
- package/skills/file_parser/handler.py +33 -11
- package/skills/github/SKILL.md +32 -32
- package/skills/github/__init__.py +1 -1
- package/skills/mail_parser/SKILL.md +68 -68
- package/skills/mail_parser/__init__.py +1 -1
- package/skills/mail_parser/handler.py +1 -1
- package/skills/media_gen/GUIDANCE.md +19 -19
- package/skills/media_gen/SKILL.md +43 -43
- package/skills/media_gen/__init__.py +1 -1
- package/skills/media_gen/handler.py +1 -1
- package/skills/media_gen/manifest.yaml +93 -93
- package/skills/netops/SKILL.md +45 -0
- package/skills/netops/__init__.py +1 -0
- package/skills/netops/handler.py +418 -0
- package/skills/netops/manifest.yaml +118 -0
- package/skills/office_parser/_excel_io.py +3 -3
- package/skills/office_parser/handler.py +2 -2
- package/skills/office_parser/ppt_tools.py +1 -1
- package/skills/office_parser/pptx_export/console_encoding.py +27 -27
- package/skills/office_parser/pptx_export/resource_paths.py +88 -88
- package/skills/office_parser/pptx_export/svg_finalize/__init__.py +12 -12
- package/skills/office_parser/pptx_export/svg_finalize/crop_images.py +366 -366
- package/skills/office_parser/pptx_export/svg_finalize/embed_images.py +288 -288
- package/skills/office_parser/pptx_export/svg_finalize/fix_image_aspect.py +388 -388
- package/skills/office_parser/pptx_export/svg_finalize/flatten_tspan.py +818 -818
- package/skills/office_parser/pptx_export/svg_finalize/svg_rect_to_path.py +345 -345
- package/skills/office_parser/pptx_export/svg_to_pptx/__init__.py +14 -14
- package/skills/office_parser/pptx_export/svg_to_pptx/drawingml/__init__.py +1 -1
- package/skills/office_parser/pptx_export/svg_to_pptx/drawingml/context.py +186 -186
- package/skills/office_parser/pptx_export/svg_to_pptx/drawingml/converter.py +781 -781
- package/skills/office_parser/pptx_export/svg_to_pptx/drawingml/paths.py +429 -429
- package/skills/office_parser/pptx_export/svg_to_pptx/drawingml/utils.py +40 -21
- package/skills/office_parser/pptx_export/svg_to_pptx/pptx_package/__init__.py +1 -1
- package/skills/office_parser/pptx_export/svg_to_pptx/pptx_package/builder.py +1267 -1267
- package/skills/office_parser/pptx_export/svg_to_pptx/pptx_package/dimensions.py +155 -155
- package/skills/office_parser/pptx_export/svg_to_pptx/pptx_package/discovery.py +101 -101
- package/skills/office_parser/pptx_export/svg_to_pptx/pptx_package/narration.py +236 -236
- package/skills/office_parser/pptx_export/svg_to_pptx/pptx_package/notes.py +257 -257
- package/skills/office_parser/pptx_export/svg_to_pptx/pptx_package/slide_xml.py +136 -136
- package/skills/office_parser/pptx_export/svg_to_pptx/tspan_flattener.py +44 -44
- package/skills/office_parser/pptx_export/svg_to_pptx/use_expander.py +129 -129
- package/skills/office_parser/references/LICENSE-Office-Word-MCP-Server.txt +20 -20
- package/skills/office_parser/references/LICENSE-excel-mcp-server.txt +21 -21
- package/skills/office_parser/references/layout_library.md +816 -816
- package/skills/office_parser/references/ppt-master/LICENSE +21 -21
- package/skills/office_parser/references/ppt-master/references/executor-base.md +474 -474
- package/skills/office_parser/references/ppt-master/references/shared-standards.md +1002 -1002
- package/skills/office_parser/references/ppt-master/references/strategist.md +880 -880
- package/skills/office_parser/references/ppt-master/references/template-designer.md +402 -402
- package/skills/office_parser/references/ppt-master/references/visual-review.md +240 -240
- package/skills/office_parser/references/ppt-master/references/visual-styles/_index.md +106 -106
- package/skills/office_parser/references/ppt-master/references/visual-styles/blueprint.md +37 -37
- package/skills/office_parser/references/ppt-master/references/visual-styles/brutalist.md +38 -38
- package/skills/office_parser/references/ppt-master/references/visual-styles/chalkboard.md +36 -36
- package/skills/office_parser/references/ppt-master/references/visual-styles/dark-tech.md +37 -37
- package/skills/office_parser/references/ppt-master/references/visual-styles/data-journalism.md +36 -36
- package/skills/office_parser/references/ppt-master/references/visual-styles/editorial.md +38 -38
- package/skills/office_parser/references/ppt-master/references/visual-styles/glassmorphism.md +36 -36
- package/skills/office_parser/references/ppt-master/references/visual-styles/ink-notes.md +37 -37
- package/skills/office_parser/references/ppt-master/references/visual-styles/ink-wash.md +36 -36
- package/skills/office_parser/references/ppt-master/references/visual-styles/memphis.md +37 -37
- package/skills/office_parser/references/ppt-master/references/visual-styles/paper-cut.md +36 -36
- package/skills/office_parser/references/ppt-master/references/visual-styles/photo-editorial.md +38 -38
- package/skills/office_parser/references/ppt-master/references/visual-styles/pixel-art.md +36 -36
- package/skills/office_parser/references/ppt-master/references/visual-styles/sketch-notes.md +36 -36
- package/skills/office_parser/references/ppt-master/references/visual-styles/soft-rounded.md +37 -37
- package/skills/office_parser/references/ppt-master/references/visual-styles/swiss-minimal.md +38 -38
- package/skills/office_parser/references/ppt-master/references/visual-styles/vintage-poster.md +36 -36
- package/skills/office_parser/references/ppt-master/references/visual-styles/zine.md +37 -37
- package/skills/office_parser/references/spec_lock.md +235 -235
- package/skills/office_parser/soffice.py +68 -68
- package/skills/office_parser/word_tools.py +3 -3
- package/skills/ssh/SKILL.md +81 -0
- package/skills/ssh/__init__.py +1 -0
- package/skills/ssh/handler.py +609 -0
- package/skills/ssh/manifest.yaml +182 -0
- package/tui/crannia.cmd +134 -134
- package/tui/crannia.sh +82 -0
- package/tui/crannia_tui/__init__.py +1 -1
- package/tui/crannia_tui/app.py +240 -17
- package/tui/crannia_tui/app.tcss +21 -0
- package/tui/crannia_tui/clipboard.py +78 -10
- package/tui/crannia_tui/commands.py +37 -0
- package/tui/crannia_tui/events.py +6 -0
- package/tui/crannia_tui/i18n.py +164 -164
- package/tui/crannia_tui/locales/en.json +31 -6
- package/tui/crannia_tui/paths.py +4 -1
- package/tui/crannia_tui/profile.py +178 -169
- package/tui/crannia_tui/settings.py +134 -134
- package/tui/crannia_tui/setup.py +44 -5
- package/tui/crannia_tui/ui.py +156 -16
- package/tui/crannia_tui/update.py +393 -389
- package/vendor/ripgrep/COPYING.txt +3 -3
- package/vendor/ripgrep/LICENSE-MIT.txt +21 -21
- package/vendor/ripgrep/NOTICE-vscode-ripgrep.txt +13 -13
- package/vendor/ripgrep/README.md +30 -14
- package/vendor/ripgrep/UNLICENSE.txt +24 -24
- package/vendor/ripgrep/linux-x64/rg +0 -0
- package/vendor/ripgrep/package-source.json +15 -16
- package/web/__main__.py +12 -15
- package/web/server.py +8481 -8594
- package/web/static/img/channel-telegram.svg +1 -1
- package/web/static/img/channel-wechat.svg +1 -1
- package/web/templates/logs.html +49 -49
- package/web/templates/models.html +46 -46
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: diagram
|
|
3
|
+
description: Create flowcharts, mind maps and structure diagrams as SVG (optionally PNG). Use when the user asks for a flowchart, process diagram, mind map, brain map, org chart, or wants to visualize nodes and connections. Accepts structured nodes/edges or a Mermaid flowchart/mindmap subset. Rendering is local (graph-layout + hand-built SVG); no Mermaid CLI, Graphviz, or browser download.
|
|
4
|
+
metadata:
|
|
5
|
+
kind: skill
|
|
6
|
+
loader: manifest.yaml
|
|
7
|
+
tools: create_flowchart, create_mindmap, render_diagram
|
|
8
|
+
---
|
|
9
|
+
|
|
10
|
+
# 结构化绘图(diagram)
|
|
11
|
+
|
|
12
|
+
把"节点 + 连线"变成**可交付的 SVG**(默认),可选 PNG。
|
|
13
|
+
流程图用分层布局(Sugiyama),思维导用整齐树布局(Reingold-Tilford)。
|
|
14
|
+
|
|
15
|
+
## 什么时候用
|
|
16
|
+
|
|
17
|
+
- 用户要**流程图 / 流程 / 决策树 / 依赖图 / 时序图** ⇒ `create_flowchart`
|
|
18
|
+
- 用户要**思维导图 / 脑图 / 目录树 / 组织架构** ⇒ `create_mindmap`
|
|
19
|
+
- 已经有 SVG 要转 PNG ⇒ `render_diagram`
|
|
20
|
+
- 用户要**统计图表**(柱/折/饼)⇒ **不用这里**,用 `plot_excel_data` / `create_excel_chart`
|
|
21
|
+
- 用户要**从无到有画一张图**(插画)⇒ 用 `media_gen`
|
|
22
|
+
|
|
23
|
+
## 两种输入
|
|
24
|
+
|
|
25
|
+
1. **结构化(推荐)**:`nodes` 数组 + `edges` 数组
|
|
26
|
+
- node: `{id, label, kind}`,`kind` 可为 `rect`/`decision`/`start`/`end`
|
|
27
|
+
- edge: `{source, target}` 或 `["A","B"]`
|
|
28
|
+
2. **Mermaid 文本子集**:`source` 参数
|
|
29
|
+
- `flowchart TD` / `graph LR`:`A[开始] --> B{判断} --> C[结束]`
|
|
30
|
+
- `mindmap`:按缩进建树,第一行是根
|
|
31
|
+
- **只支持这个子集**;`sequenceDiagram`/`classDiagram` 等会明确报错
|
|
32
|
+
|
|
33
|
+
## 输出
|
|
34
|
+
|
|
35
|
+
- 默认 **SVG** 落在工作区,回执给路径,`image/svg+xml` 可直接内联/交付
|
|
36
|
+
- 要 PNG:`render_diagram(file_path, target="png")`
|
|
37
|
+
- 要 PPT:把 SVG 交给 office_parser 的 `svgs_to_pptx`
|
|
38
|
+
|
|
39
|
+
## 依赖与边界
|
|
40
|
+
|
|
41
|
+
- 需要 `graph-layout`(MIT,约 3 MB,**不需要 Graphviz 系统程序**);
|
|
42
|
+
没装时工具会明确说"请 pip install graph-layout",不会假装成功
|
|
43
|
+
- SVG 只用白名单元素:节点是矩形/椭圆/菱形,连线是 line/polygon(箭头自画,
|
|
44
|
+
不用 marker);不支持 filter/shadow 等高级效果
|
|
45
|
+
- 图太复杂(SVG >100 KB)会要求减少节点或简化文字
|
|
46
|
+
- **不引** mermaid-cli(Node+Chromium)、Graphviz(EPL)、PlantUML(Java)——
|
|
47
|
+
依赖链保持干净
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""结构化绘图 skill —— 流程图/思维导图 → SVG。"""
|
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""结构化绘图 skill —— 流程图 / 思维导图 → SVG(默认)→ PNG/PPTX。
|
|
3
|
+
|
|
4
|
+
## 为什么这样分层
|
|
5
|
+
|
|
6
|
+
1. 布局:graph-layout 0.5.0(MIT)一套提供 Sugiyama(分层流程图)与
|
|
7
|
+
Reingold-Tilford(整齐树思维导图)。不引 Graphviz 二进制 / Node+Chromium / JVM。
|
|
8
|
+
2. 渲染:手写 SVG 字符串,只用 `create_svg_slide` 的白名单元素 ——
|
|
9
|
+
箭头用 path 自画,不用 marker(不在白名单)。
|
|
10
|
+
3. 交付:SVG 落工作区(`core.workspace.primary_output_dir` 唯一口径),
|
|
11
|
+
`image/svg+xml` 已在 artifacts mime 白名单;
|
|
12
|
+
PNG 走已装的 resvg_py,PPTX 复用 `svgs_to_pptx`(可选)。
|
|
13
|
+
|
|
14
|
+
## 输入
|
|
15
|
+
|
|
16
|
+
- 结构化:`nodes` + `edges`(JSON),可靠、可校验
|
|
17
|
+
- Mermaid 文本子集:`source` 参数传 `flowchart TD ...` / `mindmap ...`
|
|
18
|
+
——只认常见写法,不调 mermaid-cli;不支持的写法明确报错
|
|
19
|
+
|
|
20
|
+
缺 graph-layout 时给可读提示(不崩、不假装成功)。
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
from __future__ import annotations
|
|
24
|
+
|
|
25
|
+
import re
|
|
26
|
+
import time
|
|
27
|
+
from pathlib import Path
|
|
28
|
+
from typing import Any
|
|
29
|
+
|
|
30
|
+
from . import layout as L
|
|
31
|
+
from . import mermaid as M
|
|
32
|
+
from . import svg as S
|
|
33
|
+
|
|
34
|
+
__all__ = ["init", "create_flowchart", "create_mindmap", "render_diagram"]
|
|
35
|
+
|
|
36
|
+
#: 模型面对的上限。布局是 O(n log n),但 100 KB 的 SVG 早就先撞墙了;
|
|
37
|
+
#: 这里拦在布局之前,免得先算三十秒再报一句"图太复杂"。
|
|
38
|
+
MAX_NODES = 200
|
|
39
|
+
MAX_EDGES = 400
|
|
40
|
+
|
|
41
|
+
_security = None
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def init(security_guard=None) -> None:
|
|
45
|
+
"""装载钩子:只接安全核,不探测依赖、不写文件(每次调用时才查)。"""
|
|
46
|
+
global _security
|
|
47
|
+
_security = security_guard
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
# 内部
|
|
51
|
+
|
|
52
|
+
def _out_dir() -> Path:
|
|
53
|
+
from core.workspace import primary_output_dir
|
|
54
|
+
d = Path(str(primary_output_dir()))
|
|
55
|
+
d.mkdir(parents=True, exist_ok=True)
|
|
56
|
+
return d
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def _stamp() -> str:
|
|
60
|
+
return time.strftime("%Y%m%d-%H%M%S")
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def _safe_name(name: str, default: str) -> str:
|
|
64
|
+
s = re.sub(r'[^\w\u4e00-\u9fff-]+', "_", str(name or "").strip())
|
|
65
|
+
return (s or default)[:60]
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def _parse_nodes_edges(nodes: Any, edges: Any) -> tuple[list[str], list[tuple[str, str]],
|
|
69
|
+
dict[str, str], dict[str, str],
|
|
70
|
+
dict[tuple[str, str], str]]:
|
|
71
|
+
"""把 manifest 的 nodes/edges 数组规整为 (ids, edges, labels, kinds, 边标签)。"""
|
|
72
|
+
out_ids: list[str] = []
|
|
73
|
+
labels: dict[str, str] = {}
|
|
74
|
+
kinds: dict[str, str] = {}
|
|
75
|
+
for item in nodes or []:
|
|
76
|
+
if isinstance(item, str):
|
|
77
|
+
nid, label, kind = item, item, "rect"
|
|
78
|
+
elif isinstance(item, dict):
|
|
79
|
+
nid = str(item.get("id") or item.get("name") or "").strip()
|
|
80
|
+
label = str(item.get("label") or item.get("text") or nid)
|
|
81
|
+
kind = str(item.get("kind") or item.get("shape") or "rect")
|
|
82
|
+
else:
|
|
83
|
+
continue
|
|
84
|
+
if not nid:
|
|
85
|
+
continue
|
|
86
|
+
if nid not in labels:
|
|
87
|
+
out_ids.append(nid)
|
|
88
|
+
labels[nid] = label
|
|
89
|
+
kinds[nid] = kind
|
|
90
|
+
out_edges: list[tuple[str, str]] = []
|
|
91
|
+
edge_labels: dict[tuple[str, str], str] = {}
|
|
92
|
+
for e in edges or []:
|
|
93
|
+
note = ""
|
|
94
|
+
if isinstance(e, (list, tuple)) and len(e) >= 2:
|
|
95
|
+
s, t = str(e[0]), str(e[1])
|
|
96
|
+
if len(e) >= 3 and isinstance(e[2], str):
|
|
97
|
+
note = e[2].strip()
|
|
98
|
+
elif isinstance(e, dict):
|
|
99
|
+
s = str(e.get("source") or e.get("from") or "")
|
|
100
|
+
t = str(e.get("target") or e.get("to") or "")
|
|
101
|
+
note = str(e.get("label") or e.get("text") or "").strip()
|
|
102
|
+
else:
|
|
103
|
+
continue
|
|
104
|
+
if not s or not t:
|
|
105
|
+
continue
|
|
106
|
+
for nid in (s, t):
|
|
107
|
+
if nid not in labels:
|
|
108
|
+
out_ids.append(nid)
|
|
109
|
+
labels[nid] = nid
|
|
110
|
+
kinds[nid] = "rect"
|
|
111
|
+
out_edges.append((s, t))
|
|
112
|
+
if note:
|
|
113
|
+
edge_labels[(s, t)] = note
|
|
114
|
+
return out_ids, out_edges, labels, kinds, edge_labels
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def _too_big(ids: list[str], edges: list[tuple[str, str]]) -> dict | None:
|
|
118
|
+
"""节点/边数量上限。超了先拒,别让布局先跑三十秒再撞 100 KB。"""
|
|
119
|
+
if len(ids) > MAX_NODES:
|
|
120
|
+
return _err("节点太多(%d > %d)——请合并或拆分,分几张图交付"
|
|
121
|
+
% (len(ids), MAX_NODES))
|
|
122
|
+
if len(edges) > MAX_EDGES:
|
|
123
|
+
return _err("连线太多(%d > %d)——请精简关系或分几张图交付"
|
|
124
|
+
% (len(edges), MAX_EDGES))
|
|
125
|
+
return None
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def _write_svg(svg_text: str, name: str) -> dict:
|
|
129
|
+
dest = _out_dir() / ("%s_%s.svg" % (_safe_name(name, "diagram"), _stamp()))
|
|
130
|
+
dest.write_text(svg_text, encoding="utf-8")
|
|
131
|
+
return {
|
|
132
|
+
"success": True,
|
|
133
|
+
"path": str(dest),
|
|
134
|
+
"format": "svg",
|
|
135
|
+
"bytes": dest.stat().st_size,
|
|
136
|
+
"hint": ("SVG 已生成(默认格式,可直接交付/内联)。"
|
|
137
|
+
"用 deliver_file 发给用户;要 PNG/PPTX 调 render_diagram。"),
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
def _err(msg: str) -> dict:
|
|
142
|
+
return {"success": False, "error": msg}
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
def _orphans(ids: list[str], edges: list[tuple[str, str]], root: str) -> list[str]:
|
|
146
|
+
"""从 root 沿着边(父 → 子)走不到的节点。
|
|
147
|
+
|
|
148
|
+
思维导图必须是一棵连通的树。断开的节点会被布局库"随便摆"到同一处,
|
|
149
|
+
叠成一个看不出来的方块 —— 所以宁可报错,也不交一张假图。
|
|
150
|
+
"""
|
|
151
|
+
children: dict[str, list[str]] = {}
|
|
152
|
+
for s, t in edges:
|
|
153
|
+
children.setdefault(s, []).append(t)
|
|
154
|
+
seen = {root}
|
|
155
|
+
queue = [root]
|
|
156
|
+
while queue:
|
|
157
|
+
cur = queue.pop()
|
|
158
|
+
for nxt in children.get(cur, ()):
|
|
159
|
+
if nxt not in seen:
|
|
160
|
+
seen.add(nxt)
|
|
161
|
+
queue.append(nxt)
|
|
162
|
+
return [n for n in ids if n not in seen]
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
def _source_or_nodes(source: str, nodes: Any, edges: Any, want: str) -> dict:
|
|
166
|
+
"""二选一:Mermaid 文本 或 结构化 nodes/edges。返回统一中间结构或 error。"""
|
|
167
|
+
if str(source or "").strip():
|
|
168
|
+
if not M.looks_like_mermaid(source):
|
|
169
|
+
return _err("source 只接受 flowchart/graph/mindmap 开头的 Mermaid 子集;"
|
|
170
|
+
"其他图表请用 nodes/edges 传结构化数据")
|
|
171
|
+
parsed = M.parse_mermaid(source)
|
|
172
|
+
if parsed.kind != want:
|
|
173
|
+
return _err("source 是 %s,但这个工具要 %s —— 请换工具或改用 nodes/edges"
|
|
174
|
+
% (parsed.kind, want))
|
|
175
|
+
return {"parsed": parsed}
|
|
176
|
+
ids, eds, labels, kinds, edge_labels = _parse_nodes_edges(nodes, edges)
|
|
177
|
+
if not ids:
|
|
178
|
+
return _err("至少给一个节点(nodes),或用 source 传 Mermaid 文本")
|
|
179
|
+
return {"ids": ids, "edges": eds, "labels": labels, "kinds": kinds,
|
|
180
|
+
"edge_labels": edge_labels}
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
# 工具
|
|
184
|
+
|
|
185
|
+
def create_flowchart(source: str = "", nodes: Any = None, edges: Any = None,
|
|
186
|
+
orientation: str = "top-to-bottom", title: str = "",
|
|
187
|
+
output_name: str = "flowchart") -> dict:
|
|
188
|
+
"""把节点+边画成分层流程图(默认 SVG)。支持 Mermaid flowchart 子集。"""
|
|
189
|
+
if not L.available():
|
|
190
|
+
return _err("缺少 `graph-layout`(MIT,约 3 MB)。"
|
|
191
|
+
"请 `pip install graph-layout` —— 不需要 Graphviz 系统程序。")
|
|
192
|
+
try:
|
|
193
|
+
data = _source_or_nodes(source, nodes, edges, "flowchart")
|
|
194
|
+
if "error" in data:
|
|
195
|
+
return data
|
|
196
|
+
if "parsed" in data:
|
|
197
|
+
p = data["parsed"]
|
|
198
|
+
ids, eds, labels, kinds = p.ids, p.edges, p.labels, p.kinds
|
|
199
|
+
edge_labels = p.edge_labels or {}
|
|
200
|
+
orientation = p.orientation
|
|
201
|
+
else:
|
|
202
|
+
ids, eds, labels, kinds = (data["ids"], data["edges"],
|
|
203
|
+
data["labels"], data["kinds"])
|
|
204
|
+
edge_labels = data["edge_labels"]
|
|
205
|
+
over = _too_big(ids, eds)
|
|
206
|
+
if over:
|
|
207
|
+
return over
|
|
208
|
+
lay_nodes, lay_edges = L.flowchart_layout(ids, eds, orientation=orientation)
|
|
209
|
+
svg = S.render_svg(lay_nodes, lay_edges, labels, kinds, title=title,
|
|
210
|
+
edge_labels=edge_labels)
|
|
211
|
+
return _write_svg(svg, output_name)
|
|
212
|
+
except Exception as exc: # noqa: BLE001
|
|
213
|
+
return _err("%s: %s" % (type(exc).__name__, exc))
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
def create_mindmap(source: str = "", nodes: Any = None, edges: Any = None,
|
|
217
|
+
root: str = "", orientation: str = "left-to-right",
|
|
218
|
+
title: str = "", output_name: str = "mindmap") -> dict:
|
|
219
|
+
"""把树画成思维导图(整齐树布局,默认 SVG)。支持 Mermaid mindmap 子集。"""
|
|
220
|
+
if not L.available():
|
|
221
|
+
return _err("缺少 `graph-layout`(MIT,约 3 MB)。"
|
|
222
|
+
"请 `pip install graph-layout` —— 不需要 Graphviz 系统程序。")
|
|
223
|
+
try:
|
|
224
|
+
data = _source_or_nodes(source, nodes, edges, "mindmap")
|
|
225
|
+
if "error" in data:
|
|
226
|
+
return data
|
|
227
|
+
if "parsed" in data:
|
|
228
|
+
p = data["parsed"]
|
|
229
|
+
ids, eds, labels, kinds = p.ids, p.edges, p.labels, p.kinds
|
|
230
|
+
root = root or ids[0]
|
|
231
|
+
else:
|
|
232
|
+
ids, eds, labels, kinds = (data["ids"], data["edges"],
|
|
233
|
+
data["labels"], data["kinds"])
|
|
234
|
+
root = root or ids[0]
|
|
235
|
+
if root not in labels:
|
|
236
|
+
return _err("root「%s」不在节点里;可用的 id:%s"
|
|
237
|
+
% (root, "、".join(ids[:8])))
|
|
238
|
+
over = _too_big(ids, eds)
|
|
239
|
+
if over:
|
|
240
|
+
return over
|
|
241
|
+
lost = _orphans(ids, eds, root)
|
|
242
|
+
if lost:
|
|
243
|
+
return _err("这些节点连不到 root「%s」的树上:%s —— "
|
|
244
|
+
"补上它们到父节点的连线(边的方向是 父 → 子),"
|
|
245
|
+
"或改用 Mermaid mindmap 按缩进写(第一行是根)"
|
|
246
|
+
% (root, "、".join(lost[:8])))
|
|
247
|
+
lay_nodes, lay_edges = L.mindmap_layout(ids, eds, root, orientation=orientation)
|
|
248
|
+
svg = S.render_svg(lay_nodes, lay_edges, labels, kinds, title=title)
|
|
249
|
+
return _write_svg(svg, output_name)
|
|
250
|
+
except Exception as exc: # noqa: BLE001
|
|
251
|
+
return _err("%s: %s" % (type(exc).__name__, exc))
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+
def render_diagram(file_path: str, target: str = "png", output_name: str = "") -> dict:
|
|
255
|
+
"""把已有 SVG 转成 PNG 或 PPTX。默认 SVG 不需调用本工具。"""
|
|
256
|
+
if _security is None:
|
|
257
|
+
return _err("安全核未接线:渲染读文件前必须过路径分级,"
|
|
258
|
+
"请确认 skill 是通过框架装载的")
|
|
259
|
+
try:
|
|
260
|
+
p = Path(str(_security.validate_path(str(file_path or ""), "read")))
|
|
261
|
+
except Exception as exc: # noqa: BLE001
|
|
262
|
+
return _err("路径不允许读取:%s: %s" % (type(exc).__name__, exc))
|
|
263
|
+
if not p.is_file():
|
|
264
|
+
return _err("文件不存在:%s" % p)
|
|
265
|
+
if p.suffix.lower() != ".svg":
|
|
266
|
+
return _err("只接受 .svg 文件(拿到的是 %s)" % (p.suffix or "无扩展名"))
|
|
267
|
+
fmt = str(target or "png").lower().lstrip(".")
|
|
268
|
+
if fmt not in ("png", "pptx"):
|
|
269
|
+
return _err("target 只能是 png / pptx(默认 SVG 直接用,不用转)")
|
|
270
|
+
try:
|
|
271
|
+
if fmt == "png":
|
|
272
|
+
# resvg_py 已经在框架里(svgs_to_pptx 用它栅格化);
|
|
273
|
+
# 它读文件字节,不走 PIL(PIL 不认 SVG)
|
|
274
|
+
import resvg_py
|
|
275
|
+
# 用 svg_path(与 office_parser 的 pptx 导出同款),
|
|
276
|
+
# resvg_py 的 svg_string 参数名不是 svg——别猜。
|
|
277
|
+
# font_family 给 resvg 一层兜底:Linux 上没有 Microsoft YaHei,
|
|
278
|
+
# SVG 里的 font-family 栈会挑第一个存在的,这里再显式传一次。
|
|
279
|
+
from .svg import FONT_STACK
|
|
280
|
+
png = resvg_py.svg_to_bytes(
|
|
281
|
+
svg_path=str(p), font_family=", ".join(FONT_STACK))
|
|
282
|
+
dest = _out_dir() / ("%s_%s.%s" % (
|
|
283
|
+
_safe_name(output_name or p.stem + "_图", "diagram"),
|
|
284
|
+
_stamp(), "png"))
|
|
285
|
+
dest.write_bytes(png)
|
|
286
|
+
return {"success": True, "path": str(dest), "format": "png",
|
|
287
|
+
"bytes": dest.stat().st_size,
|
|
288
|
+
"hint": "PNG 已生成。用 deliver_file 发给用户。"}
|
|
289
|
+
# pptx:复用 office_parser 的 svgs_to_pptx 前提是要建 PPT 工程;
|
|
290
|
+
# 这里给明确指引而不是半吊子实现
|
|
291
|
+
return _err("PPTX 请用 office_parser 的 `svgs_to_pptx`(把 SVG 放进"
|
|
292
|
+
" slide 序列再转);单张图建议直接用 SVG 或 PNG")
|
|
293
|
+
except Exception as exc: # noqa: BLE001
|
|
294
|
+
return _err("%s: %s" % (type(exc).__name__, exc))
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""布局适配层 —— 把"节点/边"喂给 graph-layout,取回可画 SVG 的坐标。
|
|
3
|
+
|
|
4
|
+
为什么不是自研:
|
|
5
|
+
流程图要分层(Sugiyama),思维导图要整齐树(Reingold-Tilford)。
|
|
6
|
+
graph-layout 0.5.0(MIT)一套覆盖两种,wheel 自带 Cython 加速、
|
|
7
|
+
缺加速时回退纯 Python —— 不引 Graphviz 二进制、不引 Node/Chromium。
|
|
8
|
+
|
|
9
|
+
依赖缺失时 `available()` 返回 False,工具层给可读提示(不崩、不假装成功)。
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from __future__ import annotations
|
|
13
|
+
|
|
14
|
+
from dataclasses import dataclass
|
|
15
|
+
from typing import Any
|
|
16
|
+
|
|
17
|
+
__all__ = ["available", "LayoutNode", "LayoutEdge", "flowchart_layout",
|
|
18
|
+
"mindmap_layout", "NODE_W", "NODE_H"]
|
|
19
|
+
|
|
20
|
+
#: 渲染层节点框的尺寸。布局的间距按它算,渲染从这一处取 —— 两边各写一份
|
|
21
|
+
#: 就会出现"框 150 宽、间距按 50 排"的叠图(本文件早先那版就是这样)。
|
|
22
|
+
NODE_W = 150.0
|
|
23
|
+
NODE_H = 56.0
|
|
24
|
+
|
|
25
|
+
#: 兄弟 / 层间距:节点框加一点留白。
|
|
26
|
+
_NODE_GAP = NODE_W + 40.0
|
|
27
|
+
_LAYER_GAP = NODE_H + 40.0
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
@dataclass(frozen=True)
|
|
31
|
+
class LayoutNode:
|
|
32
|
+
id: str
|
|
33
|
+
x: float
|
|
34
|
+
y: float
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
@dataclass(frozen=True)
|
|
38
|
+
class LayoutEdge:
|
|
39
|
+
source: str
|
|
40
|
+
target: str
|
|
41
|
+
points: tuple[tuple[float, float], ...] = ()
|
|
42
|
+
#: 连线画不画箭头。思维导图的枝干是"归属"不是"流向",
|
|
43
|
+
#: 与流程图共用一套渲染,靠这个字段分。
|
|
44
|
+
arrow: bool = True
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def available() -> bool:
|
|
48
|
+
try:
|
|
49
|
+
import graph_layout # noqa: F401
|
|
50
|
+
return True
|
|
51
|
+
except Exception: # noqa: BLE001
|
|
52
|
+
return False
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def _require():
|
|
56
|
+
if not available():
|
|
57
|
+
raise RuntimeError(
|
|
58
|
+
"缺少 `graph-layout`(布局库)。请先 `pip install graph-layout`"
|
|
59
|
+
"(MIT,约 3 MB,不需要 Graphviz 系统程序)。")
|
|
60
|
+
from graph_layout import SugiyamaLayout, ReingoldTilfordLayout
|
|
61
|
+
return SugiyamaLayout, ReingoldTilfordLayout
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def _size_for(count: int, orientation: str) -> tuple[float, float]:
|
|
65
|
+
"""画布尺寸:给节点数留够空间,避免挤成一团。"""
|
|
66
|
+
span = max(600.0, min(2400.0, 180.0 * max(1, count)))
|
|
67
|
+
if orientation in ("left-to-right", "right-to-left"):
|
|
68
|
+
return (span, 900.0)
|
|
69
|
+
return (900.0, span)
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def flowchart_layout(ids: list[str], edges: list[tuple[str, str]], *,
|
|
73
|
+
orientation: str = "top-to-bottom") -> tuple[list[LayoutNode], list[LayoutEdge]]:
|
|
74
|
+
"""分层布局:适合流程图 / 时序 / 依赖图。
|
|
75
|
+
|
|
76
|
+
ids 是节点业务 id 的有序列表;edges 是 (source_id, target_id)。
|
|
77
|
+
返回带业务 id 的坐标——graph-layout 内部只给 index,这里映射回来。
|
|
78
|
+
"""
|
|
79
|
+
SugiyamaLayout, _ = _require()
|
|
80
|
+
if orientation not in ("top-to-bottom", "bottom-to-top",
|
|
81
|
+
"left-to-right", "right-to-left"):
|
|
82
|
+
orientation = "top-to-bottom"
|
|
83
|
+
index = {nid: i for i, nid in enumerate(ids)}
|
|
84
|
+
nodes = [{"id": i} for i in range(len(ids))]
|
|
85
|
+
links = [{"source": index[s], "target": index[t]}
|
|
86
|
+
for s, t in edges if s in index and t in index]
|
|
87
|
+
w, h = _size_for(len(ids), orientation)
|
|
88
|
+
lay = SugiyamaLayout(nodes=nodes, links=links, size=(w, h),
|
|
89
|
+
orientation=orientation, crossing_iterations=12,
|
|
90
|
+
node_separation=_NODE_GAP,
|
|
91
|
+
layer_separation=_LAYER_GAP)
|
|
92
|
+
lay.run()
|
|
93
|
+
out = []
|
|
94
|
+
for n in lay.nodes: # Node(index=.., x=.., y=..)
|
|
95
|
+
if 0 <= n.index < len(ids):
|
|
96
|
+
out.append(LayoutNode(ids[n.index], float(n.x), float(n.y)))
|
|
97
|
+
bends = getattr(lay, "edge_bends", None) or {}
|
|
98
|
+
out_edges = []
|
|
99
|
+
for s, t in edges:
|
|
100
|
+
if s not in index or t not in index:
|
|
101
|
+
continue
|
|
102
|
+
pts = tuple((float(p[0]), float(p[1]))
|
|
103
|
+
for p in (bends.get((index[s], index[t])) or []))
|
|
104
|
+
out_edges.append(LayoutEdge(s, t, pts))
|
|
105
|
+
return out, out_edges
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def _tree_shape(ids: list[str], edges: list[tuple[str, str]], root: str
|
|
109
|
+
) -> tuple[int, int]:
|
|
110
|
+
"""树的层数与最宽那一层的节点数。
|
|
111
|
+
|
|
112
|
+
Reingold-Tilford 把节点按给定画布铺开,而画布一旦写死,层数和宽度都
|
|
113
|
+
随节点数变 —— 12 个兄弟塞进 900 高就贴在一起。所以画布按树的形状给。
|
|
114
|
+
"""
|
|
115
|
+
children: dict[str, list[str]] = {}
|
|
116
|
+
for s, t in edges:
|
|
117
|
+
children.setdefault(s, []).append(t)
|
|
118
|
+
seen = {root}
|
|
119
|
+
frontier = [root]
|
|
120
|
+
depth = 0
|
|
121
|
+
level_max = 1
|
|
122
|
+
while frontier:
|
|
123
|
+
nxt: list[str] = []
|
|
124
|
+
for cur in frontier:
|
|
125
|
+
for c in children.get(cur, ()):
|
|
126
|
+
if c not in seen:
|
|
127
|
+
seen.add(c)
|
|
128
|
+
nxt.append(c)
|
|
129
|
+
if not nxt:
|
|
130
|
+
break
|
|
131
|
+
depth += 1
|
|
132
|
+
level_max = max(level_max, len(nxt))
|
|
133
|
+
frontier = nxt
|
|
134
|
+
return depth, level_max
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
def mindmap_layout(ids: list[str], edges: list[tuple[str, str]], root: str, *,
|
|
138
|
+
orientation: str = "left-to-right"
|
|
139
|
+
) -> tuple[list[LayoutNode], list[LayoutEdge]]:
|
|
140
|
+
"""整齐树布局:适合思维导图 / 组织架构 / 目录树。
|
|
141
|
+
|
|
142
|
+
root 必须是 ids 里的一个;不在时退回第一个节点(不崩)。
|
|
143
|
+
返回 (节点坐标, 连线) —— 连线与 flowchart_layout 同一个形状,
|
|
144
|
+
`arrow=False`(枝干不画箭头)。
|
|
145
|
+
"""
|
|
146
|
+
_, ReingoldTilfordLayout = _require()
|
|
147
|
+
if not ids:
|
|
148
|
+
return [], []
|
|
149
|
+
if orientation not in ("top-to-bottom", "bottom-to-top",
|
|
150
|
+
"left-to-right", "right-to-left"):
|
|
151
|
+
orientation = "left-to-right"
|
|
152
|
+
index = {nid: i for i, nid in enumerate(ids)}
|
|
153
|
+
nodes = [{"id": i} for i in range(len(ids))]
|
|
154
|
+
links = [{"source": index[s], "target": index[t]}
|
|
155
|
+
for s, t in edges if s in index and t in index]
|
|
156
|
+
root_i = index.get(root, 0)
|
|
157
|
+
depth, level_max = _tree_shape(ids, edges, root)
|
|
158
|
+
if orientation in ("left-to-right", "right-to-left"):
|
|
159
|
+
# 层沿 x 铺、兄弟沿 y 铺
|
|
160
|
+
w = min(6000.0, max(600.0, _NODE_GAP * (depth + 1)))
|
|
161
|
+
h = min(6000.0, max(360.0, _LAYER_GAP * (level_max + 1)))
|
|
162
|
+
else:
|
|
163
|
+
w = min(6000.0, max(360.0, _NODE_GAP * (level_max + 1)))
|
|
164
|
+
h = min(6000.0, max(600.0, _LAYER_GAP * (depth + 1)))
|
|
165
|
+
lay = ReingoldTilfordLayout(nodes=nodes, links=links, root=root_i,
|
|
166
|
+
size=(w, h), orientation=orientation)
|
|
167
|
+
lay.run()
|
|
168
|
+
out: list[LayoutNode] = []
|
|
169
|
+
for n in lay.nodes:
|
|
170
|
+
if 0 <= n.index < len(ids):
|
|
171
|
+
out.append(LayoutNode(ids[n.index], float(n.x), float(n.y)))
|
|
172
|
+
out_edges = [LayoutEdge(s, t, arrow=False)
|
|
173
|
+
for s, t in edges if s in index and t in index]
|
|
174
|
+
return out, out_edges
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
kind: skill
|
|
2
|
+
name: diagram
|
|
3
|
+
version: "0.1.0"
|
|
4
|
+
draft: false
|
|
5
|
+
load_strategy: on_demand
|
|
6
|
+
description: "结构化绘图 —— 流程图 / 思维导图 → SVG(默认),可选 PNG;支持 Mermaid 子集,不引 Node/Graphviz"
|
|
7
|
+
summary: "流程图与思维导图"
|
|
8
|
+
|
|
9
|
+
api: 1
|
|
10
|
+
requires:
|
|
11
|
+
- artifacts
|
|
12
|
+
- workspace
|
|
13
|
+
|
|
14
|
+
# 绘图是本地确定性操作:写工作区、可逆、不花钱、不出网。
|
|
15
|
+
# 不进自动模式分类器(与 media_gen 同一条理由),否则每画一张图都弹一次窗。
|
|
16
|
+
review_input: none
|
|
17
|
+
|
|
18
|
+
# graph-layout 是纯 Python 包(MIT),不是外部程序 —— 走 requires 的 Python 依赖,
|
|
19
|
+
# 不在这里声明 external:。缺了不阻止装载,工具调用时给可读提示。
|
|
20
|
+
|
|
21
|
+
tools:
|
|
22
|
+
- name: create_flowchart
|
|
23
|
+
permission: write
|
|
24
|
+
destructive: false
|
|
25
|
+
output_kind: structured
|
|
26
|
+
description: >
|
|
27
|
+
把节点和连线画成**分层流程图**(SVG 默认,落工作区)。
|
|
28
|
+
两种输入二选一:(1) `nodes` + `edges` 结构化数据;(2) `source` 传 Mermaid
|
|
29
|
+
`flowchart TD` / `graph LR` 文本子集。节点可写 kind: rect/decision/start/end。
|
|
30
|
+
★ 用于流程/决策/依赖/时序;统计图表(柱折饼)请用 plot_excel_data;
|
|
31
|
+
从无到有画插画请用 generate_image。
|
|
32
|
+
出图后用 deliver_file 发给用户;要 PNG 用 render_diagram。
|
|
33
|
+
上限 200 个节点 / 400 条连线;超了请分几张图。
|
|
34
|
+
inputSchema:
|
|
35
|
+
$schema: "https://json-schema.org/draft/2020-12/schema"
|
|
36
|
+
type: object
|
|
37
|
+
properties:
|
|
38
|
+
source:
|
|
39
|
+
type: string
|
|
40
|
+
description: "Mermaid 子集:flowchart TD / graph LR,如 'A[开始] --> B{判断} -->|是| C[完]'"
|
|
41
|
+
nodes:
|
|
42
|
+
type: array
|
|
43
|
+
description: "节点数组:[{id,label,kind}];kind 可 rect/decision/start/end"
|
|
44
|
+
items: {type: object}
|
|
45
|
+
edges:
|
|
46
|
+
type: array
|
|
47
|
+
description: "边数组:[{source,target,label}] 或 [[source,target,label]];label 可选"
|
|
48
|
+
items: {}
|
|
49
|
+
orientation:
|
|
50
|
+
type: string
|
|
51
|
+
enum: [top-to-bottom, bottom-to-top, left-to-right, right-to-left]
|
|
52
|
+
default: top-to-bottom
|
|
53
|
+
title: {type: string}
|
|
54
|
+
output_name: {type: string, default: "flowchart"}
|
|
55
|
+
|
|
56
|
+
- name: create_mindmap
|
|
57
|
+
permission: write
|
|
58
|
+
destructive: false
|
|
59
|
+
output_kind: structured
|
|
60
|
+
description: >
|
|
61
|
+
把树画成**思维导图**(整齐树布局,SVG 默认,落工作区)。
|
|
62
|
+
两种输入二选一:(1) `nodes` + `edges` + 可选 `root`;(2) `source` 传 Mermaid
|
|
63
|
+
`mindmap` 文本(按缩进建树)。默认从左到右展开。
|
|
64
|
+
★ 用于脑图/目录树/组织架构;流程/依赖图请用 create_flowchart。
|
|
65
|
+
上限 200 个节点;树必须从 root 连得通(边的方向是 父 → 子)。
|
|
66
|
+
inputSchema:
|
|
67
|
+
$schema: "https://json-schema.org/draft/2020-12/schema"
|
|
68
|
+
type: object
|
|
69
|
+
properties:
|
|
70
|
+
source:
|
|
71
|
+
type: string
|
|
72
|
+
description: "Mermaid mindmap 子集:首行根,下面按缩进写子节点"
|
|
73
|
+
nodes:
|
|
74
|
+
type: array
|
|
75
|
+
description: "节点数组:[{id,label}]"
|
|
76
|
+
items: {type: object}
|
|
77
|
+
edges:
|
|
78
|
+
type: array
|
|
79
|
+
description: "边数组:[{source,target}],方向是父 → 子"
|
|
80
|
+
items: {}
|
|
81
|
+
root:
|
|
82
|
+
type: string
|
|
83
|
+
description: "根节点 id;不填取第一个节点(Mermaid 输入时自动取首行)"
|
|
84
|
+
orientation:
|
|
85
|
+
type: string
|
|
86
|
+
enum: [left-to-right, right-to-left, top-to-bottom, bottom-to-top]
|
|
87
|
+
default: left-to-right
|
|
88
|
+
title: {type: string}
|
|
89
|
+
output_name: {type: string, default: "mindmap"}
|
|
90
|
+
|
|
91
|
+
- name: render_diagram
|
|
92
|
+
permission: write
|
|
93
|
+
destructive: false
|
|
94
|
+
output_kind: structured
|
|
95
|
+
description: >
|
|
96
|
+
把已有的 SVG 转成 **PNG**(走框架已有的 resvg_py,不引 cairosvg)。
|
|
97
|
+
默认 SVG 不用转;只有用户明确要位图时才调。
|
|
98
|
+
PPTX 请用 office_parser 的 svgs_to_pptx(要按 slide 序列组织)。
|
|
99
|
+
inputSchema:
|
|
100
|
+
$schema: "https://json-schema.org/draft/2020-12/schema"
|
|
101
|
+
type: object
|
|
102
|
+
properties:
|
|
103
|
+
file_path: {type: string, description: "上一步生成的 .svg 路径"}
|
|
104
|
+
target: {type: string, enum: [png], default: "png"}
|
|
105
|
+
output_name: {type: string}
|
|
106
|
+
required: [file_path]
|