dsh-eplan 0.0.0-stage → 0.14.1

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.
Files changed (89) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +219 -3
  3. package/cordis.patch.yml +182 -0
  4. package/docs/INSTALL.md +86 -0
  5. package/docs/LESSONS.md +224 -0
  6. package/docs/interfaces.md +183 -0
  7. package/docs/merged-skills/eplan-setup.md +63 -0
  8. package/docs/merged-skills/eplan-troubleshooting.md +149 -0
  9. package/icon.png +0 -0
  10. package/icon.svg +13 -0
  11. package/knowledge/KNOWLEDGE.md +413 -0
  12. package/knowledge/symbols/BP.png +0 -0
  13. package/knowledge/symbols/CDPST.png +0 -0
  14. package/knowledge/symbols/CO.png +0 -0
  15. package/knowledge/symbols/FA3.png +0 -0
  16. package/knowledge/symbols/TLRO.png +0 -0
  17. package/knowledge/symbols/TLRU.png +0 -0
  18. package/knowledge/symbols/TOUL.png +0 -0
  19. package/knowledge/symbols/TOUR.png +0 -0
  20. package/knowledge/symbols/manifest.json +91 -0
  21. package/locale/en.json +6 -0
  22. package/locale/zh.json +6 -0
  23. package/native/bridge/build.ps1 +73 -0
  24. package/native/bridge/src/Json.cs +383 -0
  25. package/native/bridge/src/Program.cs +1133 -0
  26. package/native/host/build.ps1 +74 -0
  27. package/native/host/src/ActionsTool.cs +225 -0
  28. package/native/host/src/BoxTool.cs +196 -0
  29. package/native/host/src/CheckTool.cs +186 -0
  30. package/native/host/src/Context.cs +169 -0
  31. package/native/host/src/DarkScheme.cs +111 -0
  32. package/native/host/src/DiagnoseTool.cs +115 -0
  33. package/native/host/src/Dispatcher.cs +51 -0
  34. package/native/host/src/DrawTool.cs +316 -0
  35. package/native/host/src/Entry.cs +111 -0
  36. package/native/host/src/ExportTool.cs +213 -0
  37. package/native/host/src/InspectTool.cs +367 -0
  38. package/native/host/src/KnowledgeTool.cs +166 -0
  39. package/native/host/src/LookTool.cs +257 -0
  40. package/native/host/src/OpLog.cs +289 -0
  41. package/native/host/src/PageCreateTool.cs +521 -0
  42. package/native/host/src/PageOpenTool.cs +62 -0
  43. package/native/host/src/PagesTool.cs +95 -0
  44. package/native/host/src/Paths.cs +91 -0
  45. package/native/host/src/PlacementEditTool.cs +234 -0
  46. package/native/host/src/ProbePropsTool.cs +152 -0
  47. package/native/host/src/ProjectCreateTool.cs +176 -0
  48. package/native/host/src/ProjectTool.cs +167 -0
  49. package/native/host/src/PropWriter.cs +70 -0
  50. package/native/host/src/PropsTool.cs +257 -0
  51. package/native/host/src/StatusTool.cs +168 -0
  52. package/native/host/src/SymbolInfoTool.cs +144 -0
  53. package/native/host/src/SymbolInsertTool.cs +261 -0
  54. package/native/host/src/SymbolsTool.cs +94 -0
  55. package/native/host/src/TagLocTool.cs +108 -0
  56. package/native/script/EplanMcpBootstrap.cs +221 -0
  57. package/native/script/build-symbol-gallery.mjs +231 -0
  58. package/native/script/prepare-install.ps1 +57 -0
  59. package/native/script/prune.mjs +132 -0
  60. package/native/script/verify-bootstrap.ps1 +62 -0
  61. package/package.json +53 -4
  62. package/preset/bootstrap.js +498 -0
  63. package/preset/experience-nudge.js +47 -0
  64. package/preset/image-budget.js +175 -0
  65. package/preset/knowledge-tool.js +180 -0
  66. package/preset/mcp-link.js +100 -0
  67. package/preset/prompt-surface.js +76 -0
  68. package/preset/restrict-tools.js +141 -0
  69. package/preset/skill-docs.js +181 -0
  70. package/preset/video-digest.js +294 -0
  71. package/preset/video-tool.js +92 -0
  72. package/server/compact.mjs +623 -0
  73. package/server/edit-args.mjs +67 -0
  74. package/server/fuzzy.mjs +57 -0
  75. package/server/launch-decision.mjs +26 -0
  76. package/server/measure.mjs +320 -0
  77. package/server/pdf.mjs +185 -0
  78. package/server/pdf2png.ps1 +99 -0
  79. package/server/server.mjs +3797 -0
  80. package/server/video.mjs +857 -0
  81. package/setup.mjs +223 -0
  82. package/skills/eplan-annotations/SKILL.md +55 -0
  83. package/skills/eplan-drawing/SKILL.md +206 -0
  84. package/skills/eplan-project/SKILL.md +41 -0
  85. package/skills/eplan-review/SKILL.md +80 -0
  86. package/skills/eplan-symbols/SKILL.md +142 -0
  87. package/test/check-docs.mjs +106 -0
  88. package/test/foreground.ps1 +21 -0
  89. package/test/smoke-all.mjs +229 -0
@@ -0,0 +1,257 @@
1
+ // look:一次调用同时拿到「图」和「结构化描述」。
2
+ //
3
+ // 为什么要有它:
4
+ // 之前靠截屏 + UIAutomation 点按钮,既慢又脆(模态框一弹就卡死)。
5
+ // EPLAN 自带渲染引擎 Export.GraphicPageEx 可以直接把页面渲染成位图,
6
+ // 不依赖窗口是否可见、是否被对话框挡住,也不需要模拟任何输入。
7
+ // 这是唯一「从软件内部拿渲染结果」的正路。
8
+ //
9
+ // 一次返回三样东西:
10
+ // 1. imagePath —— EPLAN 渲染出来的 PNG,模型直接 read_image 就能看
11
+ // 2. 结构化汇总 —— 元件清单、坐标、符号名,纯文本可推理
12
+ // 3. 渲染元信息 —— 尺寸、像素宽、耗时,便于判断是否成功
13
+ //
14
+ // 用法:
15
+ // look {"project":"test","page":"=MCP/4"}
16
+ // look {"project":"test","page":"=MCP/4","width":3000} 更高分辨率
17
+ // look {"project":"test","page":"=MCP/4","image":false} 只要结构化数据
18
+ using System;
19
+ using System.Collections;
20
+ using System.Collections.Generic;
21
+ using System.Diagnostics;
22
+ using System.IO;
23
+ using Eplan.EplApi.Base;
24
+ using Eplan.EplApi.DataModel;
25
+ using Eplan.EplApi.DataModel.MasterData;
26
+ using Eplan.EplApi.HEServices;
27
+ using EplanMcp;
28
+
29
+ namespace EplanMcpHost
30
+ {
31
+ internal static class LookTool
32
+ {
33
+ public static JsonValue Run(JsonValue args)
34
+ {
35
+ string pageName = args["page"].AsString(null);
36
+ string projectName = args["project"].AsString(null);
37
+ int width = args["width"].AsInt(2000);
38
+ if (width < 400) width = 400;
39
+ if (width > 8000) width = 8000;
40
+ bool wantImage = args["image"].AsBool(true);
41
+
42
+ Page page = Context.FindPage(pageName, projectName);
43
+ Project project = page.Project;
44
+
45
+ JsonValue result = JsonValue.NewObject();
46
+ result.Set("page", page.Name);
47
+ result.Set("project", project.ProjectName);
48
+ result.Set("pageType", page.PageType.ToString());
49
+
50
+ JsonValue size = JsonValue.NewArray();
51
+ size.Add(JsonValue.NewNumber(page.Size.X));
52
+ size.Add(JsonValue.NewNumber(page.Size.Y));
53
+ result.Set("pageSize", size);
54
+
55
+ // ── 1. 用 EPLAN 自己的渲染引擎出图 ──────────────────────
56
+ if (wantImage)
57
+ {
58
+ string outDir = args["outDir"].AsString(null);
59
+ if (string.IsNullOrEmpty(outDir))
60
+ outDir = Path.Combine(Path.GetTempPath(), "eplan-mcp", "look");
61
+ if (!Directory.Exists(outDir)) Directory.CreateDirectory(outDir);
62
+
63
+ string file = outDir;
64
+ // 注意:GraphicPageEx 的第二个参数是**输出目录**,不是文件名。
65
+ // 传 "...\x.png" 时 EPLAN 会把 "...\x.png" 当成目录建出来,
66
+ // 再把 <页名>.png 放进去(实测结果:...2400.png\=MCP_4.png)。
67
+ // 所以这里直接给目录,用返回值拿真实路径。
68
+
69
+ Stopwatch watch = Stopwatch.StartNew();
70
+ Export service = new Export();
71
+ string produced = null;
72
+ try
73
+ {
74
+ // GraphicPageEx 的返回值才是真正写出的路径;
75
+ // 传进去的 fileName 只是"建议",EPLAN 可能自行改写(加序号、换扩展名)。
76
+ produced = service.GraphicPageEx(
77
+ page, file, Export.Format.PNG, 24, width, Export.Compression.None, false);
78
+ }
79
+ catch (Exception ex)
80
+ {
81
+ throw new HostException("RENDER_FAILED",
82
+ "EPLAN 渲染这一页失败:" + ex.Message, ex.ToString());
83
+ }
84
+ finally
85
+ {
86
+ try { service.Dispose(); } catch { }
87
+ }
88
+ watch.Stop();
89
+
90
+ string actual = FirstExisting(produced, file);
91
+ if (actual == null)
92
+ {
93
+ // 有的版本不返回路径也不按建议名写,扫一下目录里最新的 png。
94
+ actual = NewestPng(outDir, watch);
95
+ }
96
+ if (actual == null)
97
+ throw new HostException("RENDER_EMPTY",
98
+ "EPLAN 报告渲染成功,但没有生成文件。建议路径:" + file +
99
+ ";返回值:" + (produced == null ? "(null)" : produced));
100
+
101
+ // 导出永远是 EPLAN 的打印配色(白底/蓝符号),用户屏幕是深色方案,按像素换色。
102
+ string theme = args["theme"].AsString("dark").ToLowerInvariant();
103
+ if (theme == "dark")
104
+ {
105
+ try { DarkScheme.Apply(actual); }
106
+ catch (Exception themeError) { result.Set("themeError", themeError.Message); }
107
+ }
108
+ result.Set("theme", theme);
109
+ result.Set("imagePath", actual);
110
+ result.Set("imageBytes", new FileInfo(actual).Length);
111
+ result.Set("renderMs", watch.ElapsedMilliseconds);
112
+ if (produced != null && produced.Length > 0) result.Set("engineReturned", produced);
113
+ }
114
+
115
+ // ── 2. 结构化描述 ────────────────────────────────────────
116
+ JsonValue functions = JsonValue.NewArray();
117
+ Function[] allFunctions = page.Functions;
118
+ int functionCount = 0;
119
+ if (allFunctions != null)
120
+ {
121
+ functionCount = allFunctions.Length;
122
+ foreach (Function function in allFunctions)
123
+ {
124
+ functions.Add(DescribeFunction(function));
125
+ }
126
+ }
127
+
128
+ Placement[] placements = page.AllPlacements;
129
+ int placementCount = placements == null ? 0 : placements.Length;
130
+
131
+ JsonValue counts = JsonValue.NewObject();
132
+ counts.Set("functions", functionCount);
133
+ counts.Set("placements", placementCount);
134
+ result.Set("counts", counts);
135
+ result.Set("functions", functions);
136
+
137
+ // ── 3. EPLAN 自动生成的连接(红线)─────────────────────
138
+ result.Set("graphicalConnections", DescribeGraphicalConnections(page));
139
+
140
+ return result;
141
+ }
142
+
143
+ private static JsonValue DescribeFunction(Function function)
144
+ {
145
+ JsonValue item = JsonValue.NewObject();
146
+ try { item.Set("name", function.Name); } catch { }
147
+ try { item.Set("type", function.GetType().Name); } catch { }
148
+
149
+ try
150
+ {
151
+ JsonValue at = JsonValue.NewArray();
152
+ at.Add(JsonValue.NewNumber(function.Location.X));
153
+ at.Add(JsonValue.NewNumber(function.Location.Y));
154
+ item.Set("at", at);
155
+ }
156
+ catch { }
157
+
158
+ // 符号名在 SymbolVariant 上,SymbolReference 本身没有。
159
+ SymbolReference reference = function as SymbolReference;
160
+ if (reference != null)
161
+ {
162
+ try
163
+ {
164
+ SymbolVariant variant = reference.SymbolVariant;
165
+ if (variant != null)
166
+ {
167
+ try { item.Set("symbol", variant.SymbolName); } catch { }
168
+ try { item.Set("variant", JsonValue.NewNumber(variant.VariantNr)); } catch { }
169
+ }
170
+ }
171
+ catch { }
172
+ }
173
+ return item;
174
+ }
175
+
176
+ /// <summary>
177
+ /// 每个符号上的 GraphicalConnections 就是 EPLAN 画出来的那几条线。
178
+ /// 它们不是页面上的放置对象,而是随符号渲染的,所以从 AllPlacements 里看不到。
179
+ /// </summary>
180
+ private static JsonValue DescribeGraphicalConnections(Page page)
181
+ {
182
+ JsonValue summary = JsonValue.NewObject();
183
+ int total = 0;
184
+ JsonValue samples = JsonValue.NewArray();
185
+
186
+ Placement[] placements = page.AllPlacements;
187
+ if (placements == null) return summary;
188
+
189
+ foreach (Placement placement in placements)
190
+ {
191
+ SymbolReference reference = placement as SymbolReference;
192
+ if (reference == null) continue;
193
+ try
194
+ {
195
+ IEnumerable connections = reference.GraphicalConnections as IEnumerable;
196
+ if (connections == null) continue;
197
+ foreach (object connection in connections)
198
+ {
199
+ if (connection == null) continue;
200
+ total++;
201
+ if (samples.Count < 40)
202
+ {
203
+ JsonValue item = JsonValue.NewObject();
204
+ item.Set("from", reference.GetType().Name);
205
+ try { item.Set("objectId", reference.ObjectIdentifier.ToString()); } catch { }
206
+ samples.Add(item);
207
+ }
208
+ }
209
+ }
210
+ catch { }
211
+ }
212
+
213
+ summary.Set("count", total);
214
+ summary.Set("samples", samples);
215
+ return summary;
216
+ }
217
+
218
+ /// <summary>优先用 EPLAN 返回的路径,其次用建议路径。</summary>
219
+ private static string FirstExisting(string produced, string suggested)
220
+ {
221
+ if (!string.IsNullOrEmpty(produced) && File.Exists(produced)) return produced;
222
+ if (!string.IsNullOrEmpty(suggested) && File.Exists(suggested)) return suggested;
223
+ return null;
224
+ }
225
+
226
+ /// <summary>兜底:找渲染开始之后新出现的 png。</summary>
227
+ private static string NewestPng(string dir, Stopwatch watch)
228
+ {
229
+ try
230
+ {
231
+ DateTime edge = DateTime.Now.AddSeconds(-Math.Max(5, watch.Elapsed.TotalSeconds + 5));
232
+ string best = null;
233
+ DateTime bestTime = DateTime.MinValue;
234
+ foreach (string candidate in Directory.GetFiles(dir, "*.png"))
235
+ {
236
+ DateTime time = File.GetLastWriteTime(candidate);
237
+ if (time < edge) continue;
238
+ if (time <= bestTime) continue;
239
+ bestTime = time;
240
+ best = candidate;
241
+ }
242
+ return best;
243
+ }
244
+ catch { return null; }
245
+ }
246
+
247
+ /// <summary>文件名里不能有 = + / 这类字符。</summary>
248
+ private static string Safe(string name)
249
+ {
250
+ if (string.IsNullOrEmpty(name)) return "_";
251
+ char[] bad = { '=', '+', '/', '\\', ':', '*', '?', '"', '<', '>', '|' };
252
+ string value = name;
253
+ foreach (char c in bad) value = value.Replace(c, '_');
254
+ return value.Trim();
255
+ }
256
+ }
257
+ }
@@ -0,0 +1,289 @@
1
+ // 操作记录与回退(M2 实现)。
2
+ //
3
+ // 为什么要有这个东西:EPLAN API 的改动**不进 GUI 撤销栈**,Ctrl+Z 对它们无效;
4
+ // 而项目真正的数据在 .edb 目录里,复制 .elk 链接文件根本不算备份。
5
+ // 也就是说模型一旦改错,用户没有任何办法退回去。这个日志是唯一的退路。
6
+ //
7
+ // 设计(见 docs/interfaces.md):
8
+ // - 记录由写操作自己在落盘之后追加,模型不需要调用任何「开始记录」的工具
9
+ // - 一行一条 JSON(JSONL),追加式写入,不需要锁
10
+ // - undo 是描述符而不是操作枚举:{"kind":"delete_page","page":"=X/1"},
11
+ // 新增可回退的操作只要多一种 kind,不用改日志格式
12
+ // - 记不进去也不能影响已经完成的绘图 —— 整个 Append 吞掉所有异常
13
+ //
14
+ // 目前能回退的:建页(删掉那页)、插元件(删掉那个对象)。
15
+ // 不能回退的:删页、画图元、改属性 —— 这些在记录里标 undoable=false 并写明原因,
16
+ // 与其假装能撤,不如让调用方看到真相。
17
+ using System;
18
+ using System.Collections.Generic;
19
+ using System.Globalization;
20
+ using System.IO;
21
+ using System.Text;
22
+ using Eplan.EplApi.DataModel;
23
+ using EplanMcp;
24
+
25
+ namespace EplanMcpHost
26
+ {
27
+ internal static class OpJournal
28
+ {
29
+ /// <summary>日志目录。和备份、渲染图一样放临时目录,避免往用户的项目树里塞东西。</summary>
30
+ internal static string RootDir
31
+ {
32
+ get { return Path.Combine(Path.GetTempPath(), "eplan-mcp", "ops"); }
33
+ }
34
+
35
+ /// <summary>一个项目一个文件,文件名只保留合法字符。</summary>
36
+ internal static string FileFor(string projectName)
37
+ {
38
+ string safe = projectName;
39
+ if (string.IsNullOrEmpty(safe)) safe = "unknown";
40
+ char[] bad = Path.GetInvalidFileNameChars();
41
+ StringBuilder sb = new StringBuilder(safe.Length);
42
+ foreach (char c in safe) sb.Append(Array.IndexOf(bad, c) >= 0 ? '_' : c);
43
+ return Path.Combine(RootDir, sb.ToString() + ".jsonl");
44
+ }
45
+
46
+ private static string NewId()
47
+ {
48
+ return "op" + DateTime.UtcNow.Ticks.ToString(CultureInfo.InvariantCulture) +
49
+ Guid.NewGuid().ToString("N").Substring(0, 4);
50
+ }
51
+
52
+ /// <summary>追加一条记录。失败静默 —— 绘图已经落盘,不能因为写日志失败就让用户看到报错。</summary>
53
+ public static void Append(string op, string projectName, string pageName, JsonValue undo, bool undoable, string reason)
54
+ {
55
+ try
56
+ {
57
+ JsonValue entry = JsonValue.NewObject();
58
+ entry.Set("id", NewId());
59
+ entry.Set("at", DateTime.UtcNow.ToString("o", CultureInfo.InvariantCulture));
60
+ entry.Set("op", op);
61
+ entry.Set("project", projectName == null ? "" : projectName);
62
+ entry.Set("page", pageName == null ? "" : pageName);
63
+ entry.Set("undo", undo == null ? JsonValue.NewNull() : undo);
64
+ entry.Set("undoable", undoable);
65
+ if (!string.IsNullOrEmpty(reason)) entry.Set("reason", reason);
66
+ entry.Set("undone", false);
67
+
68
+ System.IO.Directory.CreateDirectory(RootDir);
69
+ File.AppendAllText(FileFor(projectName), Json.Write(entry) + "\n", new UTF8Encoding(false));
70
+ }
71
+ catch
72
+ {
73
+ }
74
+ }
75
+
76
+ private static List<string> AllFiles()
77
+ {
78
+ List<string> files = new List<string>();
79
+ try
80
+ {
81
+ if (System.IO.Directory.Exists(RootDir))
82
+ files.AddRange(System.IO.Directory.GetFiles(RootDir, "*.jsonl"));
83
+ }
84
+ catch
85
+ {
86
+ }
87
+ return files;
88
+ }
89
+
90
+ private static int IntArg(JsonValue args, string key, int fallback)
91
+ {
92
+ JsonValue value = args[key];
93
+ if (value == null || value.Kind != JsonKind.Number) return fallback;
94
+ return (int)value.NumberValue;
95
+ }
96
+
97
+ /// <summary>查历史。参数:project / page / operation_id / limit。</summary>
98
+ public static JsonValue History(JsonValue args)
99
+ {
100
+ string projectName = args["project"].AsString(null);
101
+ if (string.IsNullOrEmpty(projectName))
102
+ {
103
+ try { projectName = Context.Project.ProjectName; }
104
+ catch { projectName = null; }
105
+ }
106
+
107
+ string pageFilter = args["page"].AsString(null);
108
+ string idFilter = args["operation_id"].AsString(null);
109
+ int limit = IntArg(args, "limit", 30);
110
+ if (limit < 1) limit = 1;
111
+
112
+ List<string> files = new List<string>();
113
+ if (!string.IsNullOrEmpty(projectName))
114
+ {
115
+ string file = FileFor(projectName);
116
+ if (File.Exists(file)) files.Add(file);
117
+ }
118
+ else
119
+ {
120
+ files = AllFiles();
121
+ }
122
+
123
+ List<JsonValue> matched = new List<JsonValue>();
124
+ foreach (string file in files)
125
+ {
126
+ string[] lines;
127
+ try { lines = File.ReadAllLines(file, Encoding.UTF8); }
128
+ catch { continue; }
129
+
130
+ for (int i = 0; i < lines.Length; i++)
131
+ {
132
+ if (lines[i].Length == 0) continue;
133
+ JsonValue entry;
134
+ try { entry = Json.Parse(lines[i]); }
135
+ catch { continue; }
136
+
137
+ if (!string.IsNullOrEmpty(pageFilter) && entry["page"].AsString("") != pageFilter) continue;
138
+ if (!string.IsNullOrEmpty(idFilter) && entry["id"].AsString("") != idFilter) continue;
139
+ matched.Add(entry);
140
+ }
141
+ }
142
+
143
+ int total = matched.Count;
144
+ int start = total > limit ? total - limit : 0;
145
+ JsonValue list = JsonValue.NewArray();
146
+ for (int i = total - 1; i >= start; i--) list.Add(matched[i]);
147
+
148
+ JsonValue result = JsonValue.NewObject();
149
+ result.Set("project", projectName == null ? JsonValue.NewNull() : JsonValue.NewString(projectName));
150
+ result.Set("total", JsonValue.NewNumber(total));
151
+ result.Set("returned", JsonValue.NewNumber(list.Count));
152
+ result.Set("newestFirst", JsonValue.NewBool(true));
153
+ result.Set("dir", JsonValue.NewString(RootDir));
154
+ result.Set("operations", list);
155
+ if (total == 0)
156
+ result.Set("note", "这个项目还没有通过本预设做过写操作。历史只记本工具做的事,用户手画的不在里面。");
157
+ return result;
158
+ }
159
+
160
+ /// <summary>撤回一次操作。参数:operation_id。</summary>
161
+ public static JsonValue Rollback(JsonValue args)
162
+ {
163
+ string id = args["operation_id"].AsString(null);
164
+ if (string.IsNullOrEmpty(id))
165
+ throw new HostException("BAD_ARGUMENT", "要给 operation_id。", "用 op_history 查有哪些 id。");
166
+
167
+ string file = null;
168
+ int index = -1;
169
+ JsonValue entry = null;
170
+
171
+ foreach (string candidate in AllFiles())
172
+ {
173
+ string[] lines;
174
+ try { lines = File.ReadAllLines(candidate, Encoding.UTF8); }
175
+ catch { continue; }
176
+
177
+ for (int i = 0; i < lines.Length; i++)
178
+ {
179
+ if (lines[i].Length == 0) continue;
180
+ JsonValue parsed;
181
+ try { parsed = Json.Parse(lines[i]); }
182
+ catch { continue; }
183
+ if (parsed["id"].AsString("") == id) { file = candidate; index = i; entry = parsed; break; }
184
+ }
185
+ if (entry != null) break;
186
+ }
187
+
188
+ if (entry == null)
189
+ throw new HostException("OP_NOT_FOUND", "没有这个操作记录:" + id,
190
+ "用 op_history 看这个项目有哪些操作 id。");
191
+
192
+ JsonValue result = JsonValue.NewObject();
193
+ result.Set("operation_id", JsonValue.NewString(id));
194
+ result.Set("op", entry["op"]);
195
+ result.Set("project", entry["project"]);
196
+ result.Set("page", entry["page"]);
197
+
198
+ if (entry["undone"].AsBool(false))
199
+ {
200
+ result.Set("undone", JsonValue.NewBool(true));
201
+ result.Set("alreadyUndone", JsonValue.NewBool(true));
202
+ result.Set("note", "这次操作之前已经撤回过了,本次什么都没做。");
203
+ return result;
204
+ }
205
+
206
+ if (!entry["undoable"].AsBool(false))
207
+ {
208
+ result.Set("undone", JsonValue.NewBool(false));
209
+ JsonValue reason = entry["reason"];
210
+ result.Set("reason", reason == null || reason.Kind == JsonKind.Null
211
+ ? JsonValue.NewString("这次操作没有记录可用的回退方式。")
212
+ : reason);
213
+ return result;
214
+ }
215
+
216
+ JsonValue undo = entry["undo"];
217
+ string kind = undo == null ? "" : undo["kind"].AsString("");
218
+ string projectName = entry["project"].AsString(null);
219
+ string pageName = entry["page"].AsString(null);
220
+
221
+ if (kind == "delete_page")
222
+ {
223
+ Page page = Context.FindPage(pageName, projectName);
224
+ int functions = page.Functions == null ? 0 : page.Functions.Length;
225
+ page.Remove();
226
+ result.Set("undone", JsonValue.NewBool(true));
227
+ result.Set("action", JsonValue.NewString("删掉这次建的页"));
228
+ result.Set("removedFunctions", JsonValue.NewNumber(functions));
229
+ }
230
+ else if (kind == "remove_object")
231
+ {
232
+ string objectName = undo["name"].AsString(null);
233
+ Page page = Context.FindPage(pageName, projectName);
234
+ Function target = null;
235
+ if (page.Functions != null)
236
+ {
237
+ foreach (Function function in page.Functions)
238
+ {
239
+ if (function.Name == objectName) { target = function; break; }
240
+ }
241
+ }
242
+ if (target == null)
243
+ throw new HostException("ROLLBACK_TARGET_MISSING",
244
+ "页上已经找不到对象 " + objectName + ",可能被手动删掉了。",
245
+ "这种情况不需要再回退,历史记录保持原样。");
246
+ target.Remove();
247
+ result.Set("undone", JsonValue.NewBool(true));
248
+ result.Set("action", JsonValue.NewString("删掉这次插入的元件"));
249
+ result.Set("removedObject", JsonValue.NewString(objectName));
250
+ }
251
+ else
252
+ {
253
+ result.Set("undone", JsonValue.NewBool(false));
254
+ result.Set("reason", JsonValue.NewString("这个回退类型还没实现:" + kind));
255
+ return result;
256
+ }
257
+
258
+ // 标记已撤回,避免重复回退把别的东西也删了。
259
+ try
260
+ {
261
+ string[] lines = File.ReadAllLines(file, Encoding.UTF8);
262
+ JsonValue updated = Json.Parse(lines[index]);
263
+ updated.Set("undone", JsonValue.NewBool(true));
264
+ updated.Set("undoneAt", JsonValue.NewString(DateTime.UtcNow.ToString("o", CultureInfo.InvariantCulture)));
265
+ lines[index] = Json.Write(updated);
266
+ File.WriteAllLines(file, lines, new UTF8Encoding(false));
267
+ }
268
+ catch
269
+ {
270
+ // 标不上也无所谓:对象已经删了,重复回退最多报「找不到对象」。
271
+ }
272
+
273
+ return result;
274
+ }
275
+ }
276
+
277
+ internal static class OpLogTool
278
+ {
279
+ public static JsonValue History(JsonValue args)
280
+ {
281
+ return OpJournal.History(args);
282
+ }
283
+
284
+ public static JsonValue Rollback(JsonValue args)
285
+ {
286
+ return OpJournal.Rollback(args);
287
+ }
288
+ }
289
+ }