aicommunicator 0.0.0-stage → 0.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.
Files changed (97) hide show
  1. package/LICENSE +55 -0
  2. package/README.md +93 -2
  3. package/bin/aic.js +20427 -0
  4. package/host-ui/host.js +5151 -0
  5. package/host-ui/index.html +483 -0
  6. package/host-ui/lang-en.js +631 -0
  7. package/package.json +30 -4
  8. package/plugin-guide.md +997 -0
  9. package/plugins/plugin-animation/LICENSE +55 -0
  10. package/plugins/plugin-animation/dist/logic.js +2225 -0
  11. package/plugins/plugin-animation/docs/manual.md +217 -0
  12. package/plugins/plugin-animation/manifest.json +31 -0
  13. package/plugins/plugin-animation/ui/icon.svg +6 -0
  14. package/plugins/plugin-animation/ui/index.html +2266 -0
  15. package/plugins/plugin-html-design/LICENSE +55 -0
  16. package/plugins/plugin-html-design/dist/logic.js +972 -0
  17. package/plugins/plugin-html-design/docs/manual.md +100 -0
  18. package/plugins/plugin-html-design/manifest.json +39 -0
  19. package/plugins/plugin-html-design/samples/exhibition-page.json +570 -0
  20. package/plugins/plugin-html-design/ui/icon.svg +5 -0
  21. package/plugins/plugin-html-design/ui/index.html +1241 -0
  22. package/plugins/plugin-html-slides/LICENSE +55 -0
  23. package/plugins/plugin-html-slides/dist/logic.js +1323 -0
  24. package/plugins/plugin-html-slides/docs/manual.md +121 -0
  25. package/plugins/plugin-html-slides/manifest.json +39 -0
  26. package/plugins/plugin-html-slides/samples/first-week-lecture.json +1129 -0
  27. package/plugins/plugin-html-slides/ui/icon.svg +5 -0
  28. package/plugins/plugin-html-slides/ui/index.html +1349 -0
  29. package/plugins/plugin-mindmap/LICENSE +55 -0
  30. package/plugins/plugin-mindmap/dist/logic.js +966 -0
  31. package/plugins/plugin-mindmap/docs/manual.md +145 -0
  32. package/plugins/plugin-mindmap/manifest.json +39 -0
  33. package/plugins/plugin-mindmap/samples/book-club.json +480 -0
  34. package/plugins/plugin-mindmap/ui/icon.svg +5 -0
  35. package/plugins/plugin-mindmap/ui/index.html +1774 -0
  36. package/plugins/plugin-plan/LICENSE +55 -0
  37. package/plugins/plugin-plan/dist/logic.js +1029 -0
  38. package/plugins/plugin-plan/docs/manual.md +261 -0
  39. package/plugins/plugin-plan/manifest.json +51 -0
  40. package/plugins/plugin-plan/samples/kimchi-day.json +1514 -0
  41. package/plugins/plugin-plan/samples/meeting-outline.json +653 -0
  42. package/plugins/plugin-plan/samples/reading-class.json +1577 -0
  43. package/plugins/plugin-plan/ui/icon.svg +7 -0
  44. package/plugins/plugin-plan/ui/index.html +1523 -0
  45. package/plugins/plugin-png/LICENSE +55 -0
  46. package/plugins/plugin-png/dist/logic.js +1758 -0
  47. package/plugins/plugin-png/docs/manual.md +344 -0
  48. package/plugins/plugin-png/manifest.json +35 -0
  49. package/plugins/plugin-png/ui/icon.svg +5 -0
  50. package/plugins/plugin-png/ui/index.html +2885 -0
  51. package/plugins/plugin-process-log/LICENSE +55 -0
  52. package/plugins/plugin-process-log/dist/logic.js +406 -0
  53. package/plugins/plugin-process-log/docs/manual.md +131 -0
  54. package/plugins/plugin-process-log/manifest.json +43 -0
  55. package/plugins/plugin-process-log/samples/design-decisions.json +241 -0
  56. package/plugins/plugin-process-log/ui/icon.svg +7 -0
  57. package/plugins/plugin-process-log/ui/index.html +721 -0
  58. package/plugins/plugin-story/LICENSE +55 -0
  59. package/plugins/plugin-story/dist/logic.js +2201 -0
  60. package/plugins/plugin-story/docs/manual.md +428 -0
  61. package/plugins/plugin-story/manifest.json +49 -0
  62. package/plugins/plugin-story/samples/scene-body.json +758 -0
  63. package/plugins/plugin-story/samples/time-and-groups.json +989 -0
  64. package/plugins/plugin-story/ui/icon.svg +4 -0
  65. package/plugins/plugin-story/ui/index.html +2783 -0
  66. package/plugins/plugin-svg/LICENSE +55 -0
  67. package/plugins/plugin-svg/dist/logic.js +2150 -0
  68. package/plugins/plugin-svg/docs/manual.md +314 -0
  69. package/plugins/plugin-svg/manifest.json +45 -0
  70. package/plugins/plugin-svg/samples/rocket-icon.json +1305 -0
  71. package/plugins/plugin-svg/samples/yard-cat.json +7796 -0
  72. package/plugins/plugin-svg/ui/icon.svg +4 -0
  73. package/plugins/plugin-svg/ui/index.html +2017 -0
  74. package/plugins/plugin-tree/LICENSE +55 -0
  75. package/plugins/plugin-tree/dist/logic.js +505 -0
  76. package/plugins/plugin-tree/docs/manual.md +140 -0
  77. package/plugins/plugin-tree/manifest.json +45 -0
  78. package/plugins/plugin-tree/samples/book-club.json +473 -0
  79. package/plugins/plugin-tree/samples/sort-memos.json +604 -0
  80. package/plugins/plugin-tree/ui/icon.svg +7 -0
  81. package/plugins/plugin-tree/ui/index.html +1305 -0
  82. package/schema/aic_describe.input.json +11 -0
  83. package/schema/aic_describe.output.json +67 -0
  84. package/schema/aic_observe.input.json +14 -0
  85. package/schema/aic_observe.output.json +47 -0
  86. package/schema/aic_send.input.json +15 -0
  87. package/schema/aic_send.output.json +38 -0
  88. package/schema/aic_update.input.json +19 -0
  89. package/schema/aic_update.output.json +39 -0
  90. package/schema/common.json +127 -0
  91. package/template/plugin/LICENSE +55 -0
  92. package/template/plugin/README.md +27 -0
  93. package/template/plugin/docs/manual.md +31 -0
  94. package/template/plugin/logic.js +337 -0
  95. package/template/plugin/manifest.json +27 -0
  96. package/template/plugin/ui/icon.svg +4 -0
  97. package/template/plugin/ui/index.html +269 -0
@@ -0,0 +1,966 @@
1
+ "use strict";
2
+ (() => {
3
+ // packages/plugin-sdk/src/index.ts
4
+ var SEPARATOR = " ";
5
+ var ALPHABET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
6
+ function sortKey(order, id) {
7
+ return `${order}${SEPARATOR}${id}`;
8
+ }
9
+ function boundAfterOrder(order) {
10
+ return `${order}!`;
11
+ }
12
+ function between(lo, hi) {
13
+ const out = [];
14
+ for (const c of ALPHABET) {
15
+ if ((lo === void 0 || c > lo) && (hi === void 0 || c < hi)) out.push(c);
16
+ }
17
+ return out;
18
+ }
19
+ function orderBetween(left, right) {
20
+ if (left === void 0 && right === void 0) return "V";
21
+ const L = left ?? "";
22
+ const U = right;
23
+ if (U !== void 0 && !(L < U)) throw new Error(`orderBetween: \uC67C\uCABD ${L} \uC774 \uC624\uB978\uCABD ${U} \uBCF4\uB2E4 \uC55E\uC774\uC5B4\uC57C \uD55C\uB2E4`);
24
+ let i = 0;
25
+ while (U !== void 0 && i < L.length && i < U.length && L[i] === U[i]) i += 1;
26
+ if (U === void 0) i = 0;
27
+ if (i >= L.length && U !== void 0) {
28
+ let prefix = U.slice(0, i);
29
+ for (let k = i; k < U.length; k += 1) {
30
+ const uc2 = U[k];
31
+ const cands2 = between(void 0, uc2);
32
+ if (cands2.length > 0) return prefix + cands2[cands2.length - 1];
33
+ if (prefix !== "" && !prefix.endsWith(SEPARATOR) && uc2 > SEPARATOR) return prefix;
34
+ prefix += uc2;
35
+ }
36
+ throw new Error(`orderBetween: ${L} \uACFC ${U} \uC0AC\uC774\uC5D0 \uAC12\uC744 \uB9CC\uB4E4 \uC218 \uC5C6\uB2E4`);
37
+ }
38
+ const lc = L[i];
39
+ const uc = U === void 0 ? void 0 : U[i];
40
+ const cands = between(lc, uc);
41
+ if (cands.length > 0) {
42
+ if (uc === void 0) return L.slice(0, i) + cands[0];
43
+ return L.slice(0, i) + cands[Math.floor(cands.length / 2)];
44
+ }
45
+ for (let j = i + 1; j < L.length; j += 1) {
46
+ const up = between(L[j], void 0);
47
+ if (up.length > 0) return L.slice(0, j) + up[0];
48
+ }
49
+ return `${L}V`;
50
+ }
51
+
52
+ // packages/plugin-mindmap/src/layout.js
53
+ var MM_COLORS = ["red", "orange", "yellow", "green", "teal", "blue", "purple", "gray"];
54
+ var MM_FONT = [18, 15, 13];
55
+ var MM_MAX_TEXT = [260, 220, 200];
56
+ var MM_PAD_X = [18, 14, 10];
57
+ var MM_PAD_Y = [12, 8, 6];
58
+ var MM_H_GAP = 56;
59
+ var MM_V_GAP = 14;
60
+ var MM_MARGIN = 32;
61
+ var MM_MAX_LINES = 6;
62
+ function mmCharWidth(ch, size) {
63
+ const c = ch.codePointAt(0);
64
+ const wide = c >= 4352 && c <= 4607 || c >= 11904 && c <= 42191 || c >= 44032 && c <= 55203 || c >= 63744 && c <= 64255 || c >= 65072 && c <= 65103 || c >= 65280 && c <= 65376 || c >= 65504 && c <= 65510 || c >= 126976;
65
+ if (wide) return size;
66
+ if (c === 32) return size * 0.3;
67
+ if ("iljtfrI.,:;'|!()[]".includes(ch)) return size * 0.36;
68
+ if ("mwMW@%".includes(ch)) return size * 0.9;
69
+ if (c >= 65 && c <= 90) return size * 0.7;
70
+ return size * 0.58;
71
+ }
72
+ function mmTextWidth(text, size) {
73
+ let w = 0;
74
+ for (const ch of text) w += mmCharWidth(ch, size);
75
+ return w;
76
+ }
77
+ function mmWrap(title, size, max) {
78
+ const lines = [];
79
+ let line = "";
80
+ let lw = 0;
81
+ for (const word of String(title).split(" ")) {
82
+ const piece = line === "" ? word : ` ${word}`;
83
+ const pw = mmTextWidth(piece, size);
84
+ if (line !== "" && lw + pw <= max) {
85
+ line += piece;
86
+ lw += pw;
87
+ continue;
88
+ }
89
+ if (line !== "") {
90
+ lines.push(line);
91
+ line = "";
92
+ lw = 0;
93
+ }
94
+ for (const ch of word) {
95
+ const cw = mmCharWidth(ch, size);
96
+ if (line !== "" && lw + cw > max) {
97
+ lines.push(line);
98
+ line = "";
99
+ lw = 0;
100
+ }
101
+ line += ch;
102
+ lw += cw;
103
+ }
104
+ }
105
+ if (line !== "" || lines.length === 0) lines.push(line);
106
+ if (lines.length > MM_MAX_LINES) {
107
+ const cut = lines.slice(0, MM_MAX_LINES);
108
+ cut[MM_MAX_LINES - 1] = `${cut[MM_MAX_LINES - 1]}\u2026`;
109
+ return cut;
110
+ }
111
+ return lines;
112
+ }
113
+ var mmRound = (v) => Math.round(v * 10) / 10;
114
+ function layoutMap(nodes, links, opts) {
115
+ const collapsed = new Set(opts && opts.collapsed || []);
116
+ const byId = /* @__PURE__ */ new Map();
117
+ const kids = /* @__PURE__ */ new Map();
118
+ for (const n of nodes) {
119
+ byId.set(n.id, n);
120
+ kids.set(n.id, []);
121
+ }
122
+ let root = null;
123
+ for (const n of nodes) {
124
+ if (n.parent === null) {
125
+ if (!root) root = n;
126
+ } else if (kids.has(n.parent)) kids.get(n.parent).push(n);
127
+ }
128
+ if (!root) return { width: 0, height: 0, nodes: [], edges: [], links: [] };
129
+ const boxes = /* @__PURE__ */ new Map();
130
+ const order = [];
131
+ const countBelow = (id, seen) => {
132
+ let k = 0;
133
+ for (const c of kids.get(id)) {
134
+ if (seen.has(c.id)) continue;
135
+ seen.add(c.id);
136
+ k += 1 + countBelow(c.id, seen);
137
+ }
138
+ return k;
139
+ };
140
+ const measure = (n, depth, side, color) => {
141
+ const d = Math.min(depth, 2);
142
+ const size = MM_FONT[d];
143
+ const lines = mmWrap(n.title, size, MM_MAX_TEXT[d]);
144
+ let tw = 0;
145
+ for (const l of lines) tw = Math.max(tw, mmTextWidth(l, size));
146
+ const lineH = Math.round(size * 1.35);
147
+ const below = kids.get(n.id);
148
+ const box = {
149
+ id: n.id,
150
+ parent: n.parent,
151
+ depth,
152
+ side,
153
+ color,
154
+ lines,
155
+ size,
156
+ lineH,
157
+ x: 0,
158
+ y: 0,
159
+ w: Math.max(depth === 0 ? 96 : 40, Math.ceil(tw) + MM_PAD_X[d] * 2),
160
+ h: lines.length * lineH + MM_PAD_Y[d] * 2,
161
+ childCount: below.length,
162
+ collapsed: depth > 0 && below.length > 0 && collapsed.has(n.id),
163
+ hidden: 0,
164
+ span: 0,
165
+ kidsSpan: 0,
166
+ kids: []
167
+ };
168
+ boxes.set(n.id, box);
169
+ order.push(box);
170
+ if (box.collapsed) box.hidden = countBelow(n.id, /* @__PURE__ */ new Set([n.id]));
171
+ else if (depth > 0) {
172
+ below.forEach((k, i) => {
173
+ const kb = measure(k, depth + 1, side, k.color || color);
174
+ box.kids.push(kb);
175
+ box.kidsSpan += kb.span + (i ? MM_V_GAP : 0);
176
+ });
177
+ }
178
+ box.span = Math.max(box.h, box.kidsSpan);
179
+ return box;
180
+ };
181
+ const place = (box, top, edge) => {
182
+ box.y = top + (box.span - box.h) / 2;
183
+ box.x = box.side === "left" ? edge - MM_H_GAP - box.w : edge + MM_H_GAP;
184
+ let cur = top + (box.span - box.kidsSpan) / 2;
185
+ const next = box.side === "left" ? box.x : box.x + box.w;
186
+ for (const k of box.kids) {
187
+ place(k, cur, next);
188
+ cur += k.span + MM_V_GAP;
189
+ }
190
+ };
191
+ const rootBox = measure(root, 0, null, null);
192
+ rootBox.x = -rootBox.w / 2;
193
+ rootBox.y = -rootBox.h / 2;
194
+ const sides = { left: [], right: [] };
195
+ kids.get(root.id).forEach((k, i) => {
196
+ const side = k.side === "left" ? "left" : "right";
197
+ sides[side].push(measure(k, 1, side, k.color || MM_COLORS[i % MM_COLORS.length]));
198
+ });
199
+ for (const side of ["right", "left"]) {
200
+ const list = sides[side];
201
+ let total = 0;
202
+ list.forEach((b, i) => {
203
+ total += b.span + (i ? MM_V_GAP : 0);
204
+ });
205
+ let cur = -total / 2;
206
+ for (const b of list) {
207
+ place(b, cur, side === "left" ? rootBox.x : rootBox.x + rootBox.w);
208
+ cur += b.span + MM_V_GAP;
209
+ }
210
+ }
211
+ let minX = Infinity;
212
+ let minY = Infinity;
213
+ let maxX = -Infinity;
214
+ let maxY = -Infinity;
215
+ for (const b of order) {
216
+ minX = Math.min(minX, b.x);
217
+ minY = Math.min(minY, b.y);
218
+ maxX = Math.max(maxX, b.x + b.w);
219
+ maxY = Math.max(maxY, b.y + b.h);
220
+ }
221
+ const dx = MM_MARGIN - minX;
222
+ const dy = MM_MARGIN - minY;
223
+ for (const b of order) {
224
+ b.x = mmRound(b.x + dx);
225
+ b.y = mmRound(b.y + dy);
226
+ }
227
+ const edges = [];
228
+ for (const b of order) {
229
+ const p = b.parent === null ? null : boxes.get(b.parent);
230
+ if (!p) continue;
231
+ const x1 = mmRound(b.side === "left" ? p.x : p.x + p.w);
232
+ const x2 = mmRound(b.side === "left" ? b.x + b.w : b.x);
233
+ const y1 = mmRound(p.y + p.h / 2);
234
+ const y2 = mmRound(b.y + b.h / 2);
235
+ const mx = mmRound((x1 + x2) / 2);
236
+ edges.push({ id: b.id, depth: b.depth, color: b.color, d: `M${x1} ${y1} C${mx} ${y1} ${mx} ${y2} ${x2} ${y2}` });
237
+ }
238
+ const shown = (id) => {
239
+ const seen = /* @__PURE__ */ new Set();
240
+ let cur = id;
241
+ while (typeof cur === "string" && !seen.has(cur)) {
242
+ if (boxes.has(cur)) return boxes.get(cur);
243
+ seen.add(cur);
244
+ cur = byId.has(cur) ? byId.get(cur).parent : null;
245
+ }
246
+ return null;
247
+ };
248
+ const edgePoint = (box, tx, ty) => {
249
+ const cx = box.x + box.w / 2;
250
+ const cy = box.y + box.h / 2;
251
+ const vx = tx - cx;
252
+ const vy = ty - cy;
253
+ if (vx === 0 && vy === 0) return { x: cx, y: cy };
254
+ const t = Math.min(vx === 0 ? Infinity : (box.w / 2 + 4) / Math.abs(vx), vy === 0 ? Infinity : (box.h / 2 + 4) / Math.abs(vy));
255
+ return { x: cx + vx * t, y: cy + vy * t };
256
+ };
257
+ const outLinks = [];
258
+ for (const l of links) {
259
+ const a = shown(l.from);
260
+ const b = shown(l.to);
261
+ if (!a || !b || a === b) continue;
262
+ const p1 = edgePoint(a, b.x + b.w / 2, b.y + b.h / 2);
263
+ const p2 = edgePoint(b, a.x + a.w / 2, a.y + a.h / 2);
264
+ const len = Math.sqrt((p2.x - p1.x) ** 2 + (p2.y - p1.y) ** 2);
265
+ if (len < 1) continue;
266
+ const nx = -(p2.y - p1.y) / len;
267
+ const ny = (p2.x - p1.x) / len;
268
+ const bow = Math.min(40, len * 0.18);
269
+ const cx = (p1.x + p2.x) / 2 + nx * bow;
270
+ const cy = (p1.y + p2.y) / 2 + ny * bow;
271
+ const el = Math.sqrt((p2.x - cx) ** 2 + (p2.y - cy) ** 2) || 1;
272
+ const ux = (p2.x - cx) / el;
273
+ const uy = (p2.y - cy) / el;
274
+ const bx = p2.x - ux * 10;
275
+ const by = p2.y - uy * 10;
276
+ outLinks.push({
277
+ id: l.id,
278
+ from: l.from,
279
+ to: l.to,
280
+ label: l.label || "",
281
+ rerouted: a.id !== l.from || b.id !== l.to,
282
+ d: `M${mmRound(p1.x)} ${mmRound(p1.y)} Q${mmRound(cx)} ${mmRound(cy)} ${mmRound(p2.x)} ${mmRound(p2.y)}`,
283
+ arrow: `${mmRound(p2.x)},${mmRound(p2.y)} ${mmRound(bx - uy * 4.5)},${mmRound(by + ux * 4.5)} ${mmRound(bx + uy * 4.5)},${mmRound(by - ux * 4.5)}`,
284
+ lx: mmRound(p1.x * 0.25 + cx * 0.5 + p2.x * 0.25),
285
+ ly: mmRound(p1.y * 0.25 + cy * 0.5 + p2.y * 0.25)
286
+ });
287
+ }
288
+ return {
289
+ width: mmRound(maxX - minX + MM_MARGIN * 2),
290
+ height: mmRound(maxY - minY + MM_MARGIN * 2),
291
+ nodes: order.map((b) => ({
292
+ id: b.id,
293
+ parent: b.parent,
294
+ depth: b.depth,
295
+ side: b.side,
296
+ color: b.color,
297
+ lines: b.lines,
298
+ size: b.size,
299
+ lineH: b.lineH,
300
+ x: b.x,
301
+ y: b.y,
302
+ w: b.w,
303
+ h: b.h,
304
+ childCount: b.childCount,
305
+ collapsed: b.collapsed,
306
+ hidden: b.hidden
307
+ })),
308
+ edges,
309
+ links: outLinks
310
+ };
311
+ }
312
+
313
+ // packages/plugin-mindmap/src/logic.ts
314
+ var PACKAGE_ID = "aic.mindmap";
315
+ var VERSION = "0.1.6";
316
+ var SELECTION_FORMAT = "aic.mindmap/selection-1";
317
+ var OUTPUT_MEDIA_TYPE = "image/svg+xml; charset=utf-8";
318
+ var MAX_TITLE = 200;
319
+ var MAX_LABEL = 100;
320
+ var MEMO_ID = /^memo\d+$/;
321
+ var Reject = class extends Error {
322
+ constructor(code, message) {
323
+ super(message);
324
+ this.code = code;
325
+ }
326
+ code;
327
+ };
328
+ var all = (s, type) => Object.values(s.records).filter((r) => r.type === type);
329
+ var keyOf = (r) => sortKey(r.content.order, r.id);
330
+ var byKey = (a, b) => keyOf(a) < keyOf(b) ? -1 : 1;
331
+ var byIdNumber = (a, b) => {
332
+ const na = Number(a.id.replace(/\D/g, ""));
333
+ const nb = Number(b.id.replace(/\D/g, ""));
334
+ return na - nb || (a.id < b.id ? -1 : a.id > b.id ? 1 : 0);
335
+ };
336
+ var node = (s, id) => s.records[id]?.content;
337
+ var link = (r) => r.content;
338
+ var sticker = (r) => r.content;
339
+ var SIDE_WORD = { left: "left", right: "right" };
340
+ var count = (n, word) => `${n} ${word}${n === 1 ? "" : "s"}`;
341
+ function rootOf(s) {
342
+ return all(s, "node").filter((r) => r.content.parent === null).sort(byIdNumber)[0];
343
+ }
344
+ function childrenOf(s, parent) {
345
+ return all(s, "node").filter((r) => r.content.parent === parent).sort(byKey);
346
+ }
347
+ var linksOf = (s) => all(s, "link").sort(byIdNumber);
348
+ var stickersOf = (s, nodeId) => all(s, "sticker").filter((r) => sticker(r).node === nodeId).sort(byIdNumber);
349
+ var orphanStickers = (s) => all(s, "sticker").sort(byIdNumber).filter((x) => s.records[sticker(x).node]?.type !== "node");
350
+ function subtree(s, id) {
351
+ const out = [];
352
+ const seen = /* @__PURE__ */ new Set();
353
+ const walk = (x) => {
354
+ if (seen.has(x)) return;
355
+ seen.add(x);
356
+ out.push(x);
357
+ for (const c of childrenOf(s, x)) walk(c.id);
358
+ };
359
+ walk(id);
360
+ return out;
361
+ }
362
+ function ancestors(s, id) {
363
+ const out = [];
364
+ const seen = /* @__PURE__ */ new Set([id]);
365
+ let p = node(s, id)?.parent ?? null;
366
+ while (p !== null && s.records[p]?.type === "node" && !seen.has(p)) {
367
+ out.unshift(p);
368
+ seen.add(p);
369
+ p = node(s, p).parent;
370
+ }
371
+ return out;
372
+ }
373
+ function need(s, id, type, what) {
374
+ if (typeof id !== "string" || id === "") throw new Reject("INVALID_INPUT", `An id is required for ${what}`);
375
+ const r = s.records[id];
376
+ if (!r) throw new Reject("TARGET_NOT_FOUND", `Cannot find ${id} (${what})`);
377
+ if (r.type !== type) throw new Reject("INVALID_INPUT", `${id} is a ${r.type}, not ${what}`);
378
+ return r;
379
+ }
380
+ var oneLine = (v) => v.replace(/\s+/g, " ").trim();
381
+ function titleInput(input, ctx) {
382
+ const v = input.title;
383
+ if (typeof v !== "string" || oneLine(v) === "") throw new Reject("INVALID_INPUT", "The name (title) is empty");
384
+ const t = oneLine(v);
385
+ if (ctx.tempNames.includes(t)) throw new Reject("INVALID_INPUT", `"${t}" is the same as a temporary name in this request, so it cannot be used as text`);
386
+ if ([...t].length > MAX_TITLE) throw new Reject("INVALID_INPUT", `A name can be up to ${MAX_TITLE} characters. Attach a long explanation as a sticker`);
387
+ return t;
388
+ }
389
+ function labelInput(input, ctx) {
390
+ const v = input.label;
391
+ if (v === void 0 || v === null) return "";
392
+ if (typeof v !== "string") throw new Reject("INVALID_INPUT", "The name of a connecting line (label) must be text. Leave it out if there is no name");
393
+ const t = oneLine(v);
394
+ if (t !== "" && ctx.tempNames.includes(t)) throw new Reject("INVALID_INPUT", `"${t}" is the same as a temporary name in this request, so it cannot be used as text`);
395
+ if ([...t].length > MAX_LABEL) throw new Reject("INVALID_INPUT", `The name of a connecting line can be up to ${MAX_LABEL} characters`);
396
+ return t;
397
+ }
398
+ function colorInput(input) {
399
+ const v = input.color;
400
+ if (v === void 0) return void 0;
401
+ if (v === null) return null;
402
+ if (typeof v !== "string" || !MM_COLORS.includes(v)) throw new Reject("INVALID_INPUT", `The color must be one of ${MM_COLORS.join(", ")}. Give null to remove the color`);
403
+ return v;
404
+ }
405
+ function sideInput(input) {
406
+ const v = input.side;
407
+ if (v === void 0 || v === null) return void 0;
408
+ if (v !== "left" && v !== "right") throw new Reject("INVALID_INPUT", "The side must be left or right");
409
+ return v;
410
+ }
411
+ function afterInput(input) {
412
+ const v = input.after;
413
+ if (v === void 0 || v === null) return v;
414
+ if (typeof v !== "string") throw new Reject("INVALID_INPUT", "after must be the id of the sibling that comes before");
415
+ return v;
416
+ }
417
+ function newOrder(sibs, afterId, self) {
418
+ const list = sibs.filter((r) => r.id !== self);
419
+ let leftIdx;
420
+ if (afterId === void 0) leftIdx = list.length - 1;
421
+ else if (afterId === null) leftIdx = -1;
422
+ else {
423
+ leftIdx = list.findIndex((r) => r.id === afterId);
424
+ if (leftIdx < 0) throw new Reject("INVALID_INPUT", `${afterId} is not a sibling in the same place`);
425
+ }
426
+ const left = leftIdx >= 0 ? list[leftIdx] : void 0;
427
+ const right = list[leftIdx + 1];
428
+ const leftKey = left ? left.id.startsWith("$") ? boundAfterOrder(left.content.order) : keyOf(left) : void 0;
429
+ const rightKey = right ? right.id.startsWith("$") ? right.content.order : keyOf(right) : void 0;
430
+ return orderBetween(leftKey, rightKey);
431
+ }
432
+ function autoSide(s, rootId, self) {
433
+ const kids = childrenOf(s, rootId).filter((r) => r.id !== self);
434
+ const left = kids.filter((r) => r.content.side === "left").length;
435
+ return left < kids.length - left ? "left" : "right";
436
+ }
437
+ var posRefs = (after) => typeof after === "string" ? { after } : void 0;
438
+ var label = (s, id) => s.records[id]?.type === "node" ? `(${id}) ${node(s, id).title}` : `(${id})`;
439
+ function parentInput(s, v) {
440
+ if (v === void 0 || v === null) {
441
+ const root = rootOf(s);
442
+ if (root) throw new Reject("INVALID_INPUT", `There is only one center topic. ${label(s, root.id)} already exists, so give a node id in parent`);
443
+ return null;
444
+ }
445
+ return need(s, v, "node", "the parent node").id;
446
+ }
447
+ function memoInput(s, input, ctx) {
448
+ const m = input.memo;
449
+ if (typeof m !== "string" || m === "") {
450
+ throw new Reject("INVALID_INPUT", 'A sticker needs a memo. Give a memo id from the memo box (memo3) or the temporary name ($name) given with "as" to core.memo.add in the same request');
451
+ }
452
+ if (MEMO_ID.test(m)) return m;
453
+ if (!m.startsWith("$") || !ctx.tempNames.includes(m)) {
454
+ throw new Reject("INVALID_INPUT", `${m} is not a memo. Give a memo id from the memo box (memo3) or the temporary name given with "as" to core.memo.add in the same request`);
455
+ }
456
+ if (s.records[m]) throw new Reject("INVALID_INPUT", `${m} is a ${s.records[m].type} of this mind map. Give the temporary name of a memo`);
457
+ return m;
458
+ }
459
+ function noTwin(s, memo, target, self) {
460
+ const twin = all(s, "sticker").find((x) => x.id !== self && sticker(x).memo === memo && sticker(x).node === target);
461
+ if (twin) throw new Reject("INVALID_INPUT", `Memo ${memo} is already attached to ${label(s, target)} as sticker ${twin.id}`);
462
+ }
463
+ var OPERATIONS = {
464
+ "node.add": (s, input, id, ctx) => {
465
+ const parent = parentInput(s, input.parent);
466
+ const after = afterInput(input);
467
+ const title = titleInput(input, ctx);
468
+ const color = colorInput(input) ?? null;
469
+ const wanted = sideInput(input);
470
+ const first = parent !== null && node(s, parent).parent === null;
471
+ if (wanted && !first) throw new Reject("INVALID_INPUT", "Give side only to a branch directly under the center. Nodes below it follow the side of the branch above");
472
+ const side = first ? wanted ?? autoSide(s, parent) : null;
473
+ const content = { parent, order: parent === null ? orderBetween() : newOrder(childrenOf(s, parent), after), title, side, color };
474
+ return {
475
+ changes: [{ id: id(0), type: "node", prefix: "n", after: content, refs: posRefs(after) }],
476
+ summary: parent === null ? `Created the center topic (${id(0)}): ${title}` : `Added node (${id(0)}, under ${parent}${side ? `, ${SIDE_WORD[side]}` : ""}): ${title}`
477
+ };
478
+ },
479
+ "node.update": (s, input, _id, ctx) => {
480
+ const r = need(s, input.node, "node", "a node");
481
+ const c = r.content;
482
+ const color = colorInput(input);
483
+ if (input.title === void 0 && color === void 0) throw new Reject("INVALID_INPUT", "There is nothing to change. Give title or color");
484
+ const title = input.title === void 0 ? c.title : titleInput(input, ctx);
485
+ const next = { ...c, title, color: color === void 0 ? c.color : color };
486
+ const parts = [];
487
+ if (title !== c.title) parts.push(`name "${c.title}" to "${title}"`);
488
+ if (next.color !== c.color) parts.push(next.color === null ? "color removed" : `color to ${next.color}`);
489
+ return { changes: [{ id: r.id, type: "node", after: next }], summary: parts.length ? `Node ${r.id}: ${parts.join(", ")}` : `Node ${r.id} (${c.title}): nothing changed` };
490
+ },
491
+ "node.move": (s, input) => {
492
+ const r = need(s, input.node, "node", "a node");
493
+ const c = r.content;
494
+ if (c.parent === null) throw new Reject("INVALID_INPUT", "The center topic cannot be moved");
495
+ const after = afterInput(input);
496
+ const wanted = sideInput(input);
497
+ if (input.parent === void 0 && after === void 0 && !wanted) throw new Reject("INVALID_INPUT", "There is no place to move to. Give at least one of parent, after and side");
498
+ const parent = input.parent === void 0 || input.parent === null ? c.parent : need(s, input.parent, "node", "the new parent node").id;
499
+ if (subtree(s, r.id).includes(parent)) throw new Reject("INVALID_INPUT", `${parent} is ${r.id} itself or a node under it, so the node cannot be moved under it`);
500
+ const first = node(s, parent).parent === null;
501
+ if (wanted && !first) throw new Reject("INVALID_INPUT", "Give side only to a branch directly under the center. Nodes below it follow the side of the branch above");
502
+ const side = first ? wanted ?? (parent === c.parent && c.side ? c.side : autoSide(s, parent, r.id)) : null;
503
+ const keepOrder = parent === c.parent && after === void 0 && wanted !== void 0;
504
+ const next = { ...c, parent, side, order: keepOrder ? c.order : newOrder(childrenOf(s, parent), after, r.id) };
505
+ const where = parent === c.parent ? "within the same parent" : `under ${label(s, parent)}`;
506
+ const sideNote = side && side !== c.side ? `, to the ${SIDE_WORD[side]}` : "";
507
+ return {
508
+ changes: [{ id: r.id, type: "node", after: next, refs: posRefs(after), moved: true }],
509
+ summary: `Moved (${r.id} ${c.title}): ${where}${after ? `, after ${after}` : after === null ? ", to the front" : ""}${sideNote}`
510
+ };
511
+ },
512
+ // 연결선은 두 끝이 있어야 뜻이 있다. 지우는 노드와 그 아래 노드에 닿은 연결선은 함께 지운다.
513
+ // 스티커는 자동으로 떨어지지 않는다(999 2.1절). 지운 노드에 붙은 스티커는 남아 지워진 노드를 가리킨다.
514
+ "node.remove": (s, input) => {
515
+ const r = need(s, input.node, "node", "a node");
516
+ const c = r.content;
517
+ if (c.parent === null) throw new Reject("INVALID_INPUT", "The center topic cannot be deleted. If the topic changed, change its name with node.update");
518
+ const ids = subtree(s, r.id);
519
+ const cut = linksOf(s).filter((x) => ids.includes(link(x).from) || ids.includes(link(x).to));
520
+ const stuck = all(s, "sticker").filter((x) => ids.includes(sticker(x).node)).length;
521
+ return {
522
+ changes: [...ids.map((x) => ({ id: x, type: "node", after: null })), ...cut.map((x) => ({ id: x.id, type: "link", after: null }))],
523
+ summary: `Deleted node (${r.id}): ${c.title}${ids.length > 1 ? `, with ${count(ids.length - 1, "node")} under it` : ""}${cut.length ? `, with ${count(cut.length, "connecting line")} that touched it` : ""}${stuck ? `. ${count(stuck, "sticker")} attached to it remain` : ""}`
524
+ };
525
+ },
526
+ "link.add": (s, input, id, ctx) => {
527
+ const from = need(s, input.from, "node", "the node the connecting line starts from (from)").id;
528
+ const to = need(s, input.to, "node", "the node the connecting line ends at (to)").id;
529
+ if (from === to) throw new Reject("INVALID_INPUT", "A connecting line joins two different nodes");
530
+ const twin = linksOf(s).find((x) => link(x).from === from && link(x).to === to || link(x).from === to && link(x).to === from);
531
+ if (twin) throw new Reject("INVALID_INPUT", `${from} and ${to} are already joined by connecting line ${twin.id}. Use link.update to change its name`);
532
+ const text = labelInput(input, ctx);
533
+ return {
534
+ changes: [{ id: id(0), type: "link", prefix: "ln", after: { from, to, label: text } }],
535
+ summary: `Drew connecting line (${id(0)}): from ${label(s, from)} to ${label(s, to)}${text ? `, "${text}"` : ""}`
536
+ };
537
+ },
538
+ "link.update": (s, input, _id, ctx) => {
539
+ const r = need(s, input.link, "link", "a connecting line");
540
+ const c = link(r);
541
+ if (input.label === void 0) throw new Reject("INVALID_INPUT", "Give the new name of the connecting line (label). Give empty text to remove the name");
542
+ const text = labelInput(input, ctx);
543
+ return {
544
+ changes: [{ id: r.id, type: "link", after: { ...c, label: text } }],
545
+ summary: text === c.label ? `Connecting line ${r.id}: nothing changed` : `Connecting line ${r.id}: name "${c.label}" to "${text}"`
546
+ };
547
+ },
548
+ "link.remove": (s, input) => {
549
+ const r = need(s, input.link, "link", "a connecting line");
550
+ const c = link(r);
551
+ return { changes: [{ id: r.id, type: "link", after: null }], summary: `Deleted connecting line (${r.id}): from ${label(s, c.from)} to ${label(s, c.to)}` };
552
+ },
553
+ "sticker.add": (s, input, id, ctx) => {
554
+ const memo = memoInput(s, input, ctx);
555
+ const target = need(s, input.node, "node", "the node to attach to").id;
556
+ noTwin(s, memo, target);
557
+ return {
558
+ changes: [{ id: id(0), type: "sticker", prefix: "st", after: { memo, node: target } }],
559
+ summary: `Attached sticker (${id(0)}): memo ${memo} to ${label(s, target)}`
560
+ };
561
+ },
562
+ "sticker.attach": (s, input) => {
563
+ const r = need(s, input.sticker, "sticker", "a sticker");
564
+ const target = need(s, input.node, "node", "the node to attach to").id;
565
+ noTwin(s, sticker(r).memo, target, r.id);
566
+ return { changes: [{ id: r.id, type: "sticker", after: { ...sticker(r), node: target } }], summary: `Moved sticker (${r.id}): to ${label(s, target)}` };
567
+ },
568
+ "sticker.remove": (s, input) => {
569
+ const r = need(s, input.sticker, "sticker", "a sticker");
570
+ return { changes: [{ id: r.id, type: "sticker", after: null }], summary: `Removed sticker (${r.id}). Memo ${sticker(r).memo} stays in the memo box` };
571
+ }
572
+ };
573
+ function refsOf(r) {
574
+ const out = [];
575
+ const add = (to, kind, soft = false) => {
576
+ if (to) out.push({ from: r.id, to, kind, ...soft ? { soft: true } : {} });
577
+ };
578
+ if (r.type === "node") add(r.content.parent, "parent");
579
+ else if (r.type === "link") {
580
+ add(link(r).from, "link_from");
581
+ add(link(r).to, "link_to");
582
+ } else if (r.type === "sticker") {
583
+ add(sticker(r).memo, "sticker_memo", true);
584
+ add(sticker(r).node, "sticker_node", true);
585
+ }
586
+ return out;
587
+ }
588
+ function validate(candidate, changed) {
589
+ const out = [];
590
+ const err = (record, code, message) => out.push({ level: "error", code, record, message });
591
+ const changedSet = new Set(changed);
592
+ let structural = false;
593
+ let linked = false;
594
+ for (const id of changed) {
595
+ const r = candidate.records[id];
596
+ if (!r) continue;
597
+ for (const ref of refsOf(r)) {
598
+ if (ref.kind === "sticker_memo") continue;
599
+ const to = candidate.records[ref.to];
600
+ if (!to) err(id, "dangling_ref", `${ref.to}, which ${id} points to, does not exist`);
601
+ else if (to.type !== "node") err(id, "wrong_ref_type", `${ref.kind} of ${id} must point to a node, but ${ref.to} is a ${to.type}`);
602
+ }
603
+ if (r.type === "node") {
604
+ structural = true;
605
+ const c = r.content;
606
+ if (typeof c.title !== "string" || c.title === "") err(id, "no_title", `${id} has no name`);
607
+ if (c.color !== null && !MM_COLORS.includes(c.color)) err(id, "bad_color", `The color ${String(c.color)} of ${id} cannot be used`);
608
+ const up = c.parent === null ? null : candidate.records[c.parent];
609
+ const first = up?.type === "node" && up.content.parent === null;
610
+ if (first && c.side !== "left" && c.side !== "right") err(id, "side_missing", `Branch ${id} directly under the center must have a side (left or right)`);
611
+ if (!first && c.side !== null) err(id, "side_not_allowed", `${id} is not a branch directly under the center, so it cannot have a side`);
612
+ } else if (r.type === "link") {
613
+ linked = true;
614
+ if (link(r).from === link(r).to) err(id, "self_link", `Both ends of connecting line ${id} are the same node`);
615
+ } else if (r.type === "sticker") {
616
+ if (!MEMO_ID.test(sticker(r).memo)) err(id, "sticker_not_memo", `Sticker ${id} must point to a memo in the memo box`);
617
+ }
618
+ }
619
+ if (structural) {
620
+ const nodes = all(candidate, "node");
621
+ const roots = nodes.filter((t) => t.content.parent === null);
622
+ if (roots.length > 1) err(roots.map((t) => t.id).find((x) => changedSet.has(x)) ?? roots[1].id, "second_root", `There is only one center topic: ${roots.map((t) => t.id).join(", ")}`);
623
+ for (const t of nodes) {
624
+ if (!changedSet.has(t.id)) continue;
625
+ const seen = /* @__PURE__ */ new Set([t.id]);
626
+ let p = t.content.parent;
627
+ while (p !== null && candidate.records[p]?.type === "node") {
628
+ if (seen.has(p)) {
629
+ err(t.id, "containment_cycle", `The parents of ${t.id} form a loop`);
630
+ break;
631
+ }
632
+ seen.add(p);
633
+ p = candidate.records[p].content.parent;
634
+ }
635
+ }
636
+ }
637
+ if (linked) {
638
+ const pairs = /* @__PURE__ */ new Map();
639
+ for (const x of linksOf(candidate)) {
640
+ const key = [link(x).from, link(x).to].sort().join(" ");
641
+ const twin = pairs.get(key);
642
+ if (twin) err(changedSet.has(x.id) ? x.id : twin, "duplicate_link", `Connecting lines ${twin} and ${x.id} join the same two nodes`);
643
+ else pairs.set(key, x.id);
644
+ }
645
+ }
646
+ const gone = changed.filter((id) => !candidate.records[id]);
647
+ if (gone.length > 0) {
648
+ const goneSet = new Set(gone);
649
+ for (const r of Object.values(candidate.records)) {
650
+ if (changedSet.has(r.id)) continue;
651
+ for (const ref of refsOf(r)) if (!ref.soft && goneSet.has(ref.to)) err(r.id, "dangling_ref", `${r.id} points to ${ref.to}, which was deleted`);
652
+ }
653
+ }
654
+ return out;
655
+ }
656
+ function nodeScope(s, scope) {
657
+ const ids = scope?.targets ?? (scope?.target !== void 0 ? [scope.target] : null);
658
+ if (ids === null) return null;
659
+ for (const id of ids) {
660
+ const r = s.records[id];
661
+ if (!r) throw new Reject("TARGET_NOT_FOUND", `Cannot find node ${id}`);
662
+ if (r.type !== "node") throw new Reject("INVALID_INPUT", `The scope for a narrowed read must be a node id. ${id} is a ${r.type}`);
663
+ }
664
+ return ids;
665
+ }
666
+ function viewOf(s, ids) {
667
+ if (ids === null) return { full: new Set(all(s, "node").map((t) => t.id)), heads: /* @__PURE__ */ new Set() };
668
+ const full = new Set(ids.flatMap((id) => subtree(s, id)));
669
+ const heads = new Set(ids.flatMap((id) => ancestors(s, id)).filter((x) => !full.has(x)));
670
+ return { full, heads };
671
+ }
672
+ var linkLine = (s, x) => `(${x.id}) ${label(s, link(x).from)} -> ${label(s, link(x).to)}${link(x).label ? `: ${link(x).label}` : ""}`;
673
+ function conceptText(s, ids, mine) {
674
+ const starred = new Set(mine.flatMap((m) => m.changes.filter((c) => c.after !== null).map((c) => c.record)));
675
+ const star = (id) => starred.has(id) ? " *" : "";
676
+ const root = rootOf(s);
677
+ if (!root) return "The mind map is empty. Write the topic the person gave in the center with node.add (without parent).";
678
+ const v = viewOf(s, ids);
679
+ const lines = [];
680
+ const tag = (c) => {
681
+ const parts = [c.side ? SIDE_WORD[c.side] : null, c.color].filter(Boolean);
682
+ return parts.length ? ` [${parts.join(", ")}]` : "";
683
+ };
684
+ const nodeBlock = (id, depth) => {
685
+ const c = node(s, id);
686
+ const pad = " ".repeat(Math.max(0, depth - 1));
687
+ lines.push(depth === 0 ? `Center (${id})${star(id)} ${c.title}` : `${pad}- (${id})${star(id)}${tag(c)} ${c.title}`);
688
+ for (const x of stickersOf(s, id)) lines.push(`${depth === 0 ? " " : `${pad} `}Sticker (${x.id})${star(x.id)}: memo ${sticker(x).memo}`);
689
+ for (const k of childrenOf(s, id)) nodeBlock(k.id, depth + 1);
690
+ };
691
+ if (ids === null) nodeBlock(root.id, 0);
692
+ else {
693
+ lines.push("Nodes above");
694
+ for (const h of [...v.heads].sort((a, b) => ancestors(s, a).length - ancestors(s, b).length)) lines.push(`- (${h})${star(h)} ${node(s, h).title}`);
695
+ for (const id of ids) {
696
+ lines.push("", `Node read (${id})`);
697
+ nodeBlock(id, 1);
698
+ }
699
+ }
700
+ const links = linksOf(s).filter((x) => v.full.has(link(x).from) || v.full.has(link(x).to));
701
+ if (links.length) {
702
+ lines.push("", ids === null ? "Connecting lines" : "Connecting lines that touch the nodes read");
703
+ for (const x of links) lines.push(`- ${linkLine(s, x)}${star(x.id)}`);
704
+ }
705
+ const orphans = orphanStickers(s);
706
+ if (orphans.length) {
707
+ lines.push("", "Stickers left on deleted nodes");
708
+ for (const x of orphans) lines.push(`- Sticker (${x.id})${star(x.id)}: memo ${sticker(x).memo}, deleted node ${sticker(x).node}`);
709
+ }
710
+ return lines.join("\n");
711
+ }
712
+ function scopeRecords(s, ids) {
713
+ const v = viewOf(s, ids);
714
+ const out = /* @__PURE__ */ new Set([...v.full, ...v.heads]);
715
+ for (const x of linksOf(s)) if (v.full.has(link(x).from) || v.full.has(link(x).to)) out.add(x.id);
716
+ for (const id of v.full) for (const x of stickersOf(s, id)) out.add(x.id);
717
+ return [...out];
718
+ }
719
+ function screenData(s) {
720
+ return {
721
+ root: rootOf(s)?.id ?? null,
722
+ nodes: all(s, "node").sort(byKey).map((r) => ({ id: r.id, ...r.content })),
723
+ links: linksOf(s).map((r) => ({ id: r.id, ...link(r) })),
724
+ stickers: all(s, "sticker").sort(byIdNumber).map((r) => ({ id: r.id, ...sticker(r) })),
725
+ colors: MM_COLORS
726
+ };
727
+ }
728
+ function project(s, scope, mine) {
729
+ const ids = nodeScope(s, scope);
730
+ return {
731
+ text: conceptText(s, ids, mine),
732
+ scopeLabel: ids ? `Read node ${ids.join(", ")} and everything under it, the nodes above it up to the center, and the connecting lines and stickers on them` : "Read everything",
733
+ // 화면은 한 번에 받아 그림을 그린다. 좁혀 읽어도 JSON 은 전체다
734
+ json: screenData(s),
735
+ ...ids ? { records: scopeRecords(s, ids) } : {}
736
+ };
737
+ }
738
+ function resolveLabel(s, id) {
739
+ const r = s.records[id];
740
+ if (!r) return id;
741
+ if (r.type === "node") return `Node ${id}: ${r.content.title}`;
742
+ if (r.type === "link") return `Connecting line ${linkLine(s, r)}`;
743
+ if (r.type === "sticker") return `Sticker ${id}: memo ${sticker(r).memo}`;
744
+ return id;
745
+ }
746
+ var idList = (v) => Array.isArray(v) && v.length <= 2e3 && v.every((x) => typeof x === "string");
747
+ function selectionProblem(v) {
748
+ const x = v;
749
+ if (typeof x !== "object" || x === null || x.format !== SELECTION_FORMAT) return `The format of the selection is not ${SELECTION_FORMAT}`;
750
+ if (typeof x.rev !== "string") return "The selection has no revision it was drawn from (rev)";
751
+ if (!idList(x.nodes) || !idList(x.links)) return "nodes and links of the selection must be lists of ids";
752
+ if (x.collapsed !== void 0 && !idList(x.collapsed)) return "collapsed of the selection must be a list of node ids";
753
+ if (x.labels !== void 0 && (typeof x.labels !== "object" || x.labels === null || Array.isArray(x.labels))) return "labels of the selection must be an object from id to text";
754
+ return null;
755
+ }
756
+ var MAX_SELECTION_LINES = 60;
757
+ var MAX_NAMED_CHILDREN = 12;
758
+ function describeSelection(s, input) {
759
+ const problem = selectionProblem(input?.selection);
760
+ if (problem) return { error: "INVALID_INPUT", summary: problem };
761
+ const sel = input.selection;
762
+ const targets = [];
763
+ const gone = [];
764
+ const was = (id) => sel.labels?.[id] ?? "name unknown";
765
+ const lines = [];
766
+ const now = typeof input.current === "string" && input.current !== sel.rev ? ` Something changed after that (now ${input.current}). What follows is the current state.` : "";
767
+ lines.push(`Revision when selected: ${sel.rev}.${now}`);
768
+ const folded = new Set(sel.collapsed ?? []);
769
+ if (sel.nodes.length === 0 && sel.links.length === 0) {
770
+ const root = rootOf(s);
771
+ lines.push(root ? `No node or connecting line is selected. The words are about the whole picture (center ${label(s, root.id)}).` : "No node or connecting line is selected. The picture is empty.");
772
+ }
773
+ const touching = (id) => linksOf(s).filter((x) => link(x).from === id || link(x).to === id);
774
+ if (sel.nodes.length) lines.push(`Selected nodes (${sel.nodes.length}):`);
775
+ for (const id of sel.nodes.slice(0, MAX_SELECTION_LINES)) {
776
+ const r = s.records[id];
777
+ if (r?.type !== "node") {
778
+ gone.push(id);
779
+ lines.push(`- (${id}) ${was(id)} (deleted since)`);
780
+ continue;
781
+ }
782
+ targets.push(id);
783
+ const c = r.content;
784
+ const path = [...ancestors(s, id), id].map((x) => node(s, x).title).join(" > ");
785
+ const kids = childrenOf(s, id);
786
+ const below = subtree(s, id).length - 1;
787
+ const parts = [`- (${id}) ${c.title}: ${c.parent === null ? "it is the center topic" : `path ${path}`}.`];
788
+ if (c.side) parts.push(`It is a ${SIDE_WORD[c.side]} branch.`);
789
+ if (below === 0) parts.push("It has no nodes under it.");
790
+ else {
791
+ const named = kids.slice(0, MAX_NAMED_CHILDREN).map((k) => label(s, k.id)).join(", ");
792
+ parts.push(`${count(below, "node")} under it (${kids.length} directly under: ${named}${kids.length > MAX_NAMED_CHILDREN ? ` and ${kids.length - MAX_NAMED_CHILDREN} more` : ""}).`);
793
+ if (folded.has(id)) parts.push("It was folded on the person's screen, so the nodes under it were not visible.");
794
+ }
795
+ const ls = touching(id);
796
+ if (ls.length) parts.push(`Connecting lines that touch it: ${ls.map((x) => linkLine(s, x)).join("; ")}.`);
797
+ const sts = stickersOf(s, id);
798
+ if (sts.length) parts.push(`Stickers on it: ${sts.map((x) => `(${x.id}) memo ${sticker(x).memo}`).join(", ")}. Read the memo text in the memo box.`);
799
+ lines.push(parts.join(" "));
800
+ }
801
+ if (sel.nodes.length > MAX_SELECTION_LINES) lines.push(`- and ${sel.nodes.length - MAX_SELECTION_LINES} more. Read them with a node id in scope`);
802
+ if (sel.links.length) lines.push(`Selected connecting lines (${sel.links.length}):`);
803
+ for (const id of sel.links.slice(0, MAX_SELECTION_LINES)) {
804
+ const r = s.records[id];
805
+ if (r?.type !== "link") {
806
+ gone.push(id);
807
+ lines.push(`- (${id}) ${was(id)} (deleted since)`);
808
+ continue;
809
+ }
810
+ targets.push(id);
811
+ lines.push(`- ${linkLine(s, r)}${link(r).label ? "" : " (no name)"}`);
812
+ }
813
+ if (sel.nodes.length > 1) lines.push("More than one node is selected. If it is not clear which one the person means, ask the person.");
814
+ return { text: lines.join("\n"), targets, gone };
815
+ }
816
+ var STROKE = { red: "#d64545", orange: "#e07b1f", yellow: "#b98c00", green: "#3a9a4a", teal: "#1f9c95", blue: "#2f7fd6", purple: "#8a55d6", gray: "#6b7280" };
817
+ var TINT = { red: "#fde8e8", orange: "#fdeedd", yellow: "#fbf3cf", green: "#e3f4e6", teal: "#dcf3f1", blue: "#e1edfb", purple: "#eee6fb", gray: "#eceef1" };
818
+ var xml = (v) => v.replace(/[\u0000-\u0008\u000b\u000c\u000e-\u001f]/g, "").replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;");
819
+ function svgText(m, meta) {
820
+ const out = [];
821
+ out.push(`<svg xmlns="http://www.w3.org/2000/svg" width="${m.width}" height="${m.height}" viewBox="0 0 ${m.width} ${m.height}" font-family="system-ui, 'Malgun Gothic', 'Apple SD Gothic Neo', 'Noto Sans KR', sans-serif">`);
822
+ out.push(`<!-- ${xml(meta).replace(/--/g, "- -")} -->`);
823
+ out.push(`<rect width="${m.width}" height="${m.height}" fill="#ffffff"/>`);
824
+ for (const e of m.edges) out.push(`<path d="${e.d}" fill="none" stroke="${STROKE[e.color ?? "gray"] ?? STROKE.gray}" stroke-width="${e.depth === 1 ? 3 : 2}" stroke-linecap="round"/>`);
825
+ for (const l of m.links) {
826
+ out.push(`<path d="${l.d}" fill="none" stroke="#6b7280" stroke-width="1.5" stroke-dasharray="5 4"/>`);
827
+ out.push(`<polygon points="${l.arrow}" fill="#6b7280"/>`);
828
+ if (l.label) out.push(`<text x="${l.lx}" y="${l.ly + 4}" text-anchor="middle" font-size="12" fill="#374151" stroke="#ffffff" stroke-width="4" paint-order="stroke" stroke-linejoin="round">${xml(l.label)}</text>`);
829
+ }
830
+ for (const n of m.nodes) {
831
+ const stroke = STROKE[n.color ?? "gray"] ?? STROKE.gray;
832
+ const fill = n.depth === 0 ? "#1f2937" : n.depth === 1 ? TINT[n.color ?? "gray"] ?? TINT.gray : "#ffffff";
833
+ out.push(`<rect x="${n.x}" y="${n.y}" width="${n.w}" height="${n.h}" rx="${n.depth === 0 ? 14 : 8}" fill="${fill}" stroke="${n.depth === 0 ? "#1f2937" : stroke}" stroke-width="${n.depth === 1 ? 2 : 1.5}"/>`);
834
+ const top = n.y + (n.h - n.lines.length * n.lineH) / 2;
835
+ const spans = n.lines.map((line, i) => `<tspan x="${Math.round((n.x + n.w / 2) * 10) / 10}" y="${Math.round((top + i * n.lineH + (n.lineH - n.size) / 2 + n.size * 0.82) * 10) / 10}">${xml(line)}</tspan>`).join("");
836
+ out.push(`<text text-anchor="middle" font-size="${n.size}"${n.depth === 0 ? ' font-weight="600"' : ""} fill="${n.depth === 0 ? "#ffffff" : "#111827"}">${spans}</text>`);
837
+ }
838
+ out.push("</svg>");
839
+ return `${out.join("\n")}
840
+ `;
841
+ }
842
+ function renderOutput(s, req) {
843
+ if ((req?.format ?? "svg") !== "svg") return { error: "UNSUPPORTED_CAPABILITY", summary: `The mind map has one output format, svg (it does not make ${String(req?.format)})` };
844
+ const root = rootOf(s);
845
+ if (!root) return { error: "INVALID_STATE", summary: "The mind map is empty. Write the center topic first" };
846
+ const data = screenData(s);
847
+ const m = layoutMap(data.nodes, data.links, {});
848
+ const title = root.content.title;
849
+ return {
850
+ ok: true,
851
+ mediaType: OUTPUT_MEDIA_TYPE,
852
+ fileName: `${[...title].slice(0, 40).join("")}.svg`,
853
+ content: svgText(m, `${PACKAGE_ID} ${VERSION}${req?.revision ? `; revision ${req.revision}` : ""}`),
854
+ basis: { root: root.id, revision: req?.revision ?? null, nodes: data.nodes.length, links: data.links.length }
855
+ };
856
+ }
857
+ var AI_GUIDE = [
858
+ "This screen is a mind map. It spreads branches to both sides of a center topic so that a whole line of thinking shows as one picture.",
859
+ "Write names, labels and memo text in the language the person uses.",
860
+ "Write the topic the person gave as the name of the center topic, in the meaning you understood (node.add without parent), and split it into nodes below. The person can create, move and delete nodes at any time. Do not hold on to the shape you first made.",
861
+ "A node holds only a short name. Write one idea per node as a word or a short phrase. Do not put a long explanation, a reason, a task or a question in a node. Attach it as a sticker.",
862
+ 'The content of a sticker is a memo in the core memo box, and the sticker itself has no text. To attach a memo that is already in the memo box, give its memo id (memo3) in memo of sticker.add. Do not copy it by creating a new memo with the same text. To write a new thought and attach it, create the memo with core.memo.add in the same request (give a $name with "as") and give that $name in memo of sticker.add. You can send several sticker.add operations in one request to place several memos at once. The same memo can be attached to other nodes too, but not twice to the same node.',
863
+ "Ask the person before you attach or remove a sticker. Stickers are never attached or removed automatically. When a sticker is removed, the memo stays in the memo box. Change the memo text with core.memo.update. The picture shows only the number of memos on a node that has stickers, and the person selects the node and reads the text in the side panel.",
864
+ "You do not set the position of a node. The screen places it. You decide only which node it is under (parent), its order among siblings (after), and whether a branch directly under the center sits on the left or the right (side). If you leave side out, the branch goes to the side with fewer branches. If the person has chosen a side, do not change it.",
865
+ `The color is one of ${MM_COLORS.join(", ")}. A node without a color follows the color of the branch above it, and the screen draws the branches directly under the center in different colors even when they have none. So give a color only when the person wants one or when meanings must be told apart.`,
866
+ "When two nodes on different branches are related, draw a connecting line with link.add and write the relation briefly in label. A parent and its child are already joined, so do not draw a connecting line between them. Two nodes can have only one connecting line between them.",
867
+ "Deleting a node also deletes the nodes under it and the connecting lines that touch them. Ask the person before you delete a node the person made. Stickers that were on a deleted node remain and point to the deleted node. If a sticker is still useful, move it to another node with sticker.attach. If not, ask the person and remove it.",
868
+ "When the person selects nodes or connecting lines in the picture and sends them with words, read them with read_selection. The description is the picture the person sees now, so it includes what the person has just created or moved. If a node was folded when the person selected it, the person spoke without seeing the nodes under it.",
869
+ "For a narrowed read, use a node id in scope. You get that node and everything under it, the nodes above it up to the center, and the connecting lines and stickers on them."
870
+ ].join(" ");
871
+ var DESCRIPTION = {
872
+ id: PACKAGE_ID,
873
+ version: VERSION,
874
+ title: "Mind Map",
875
+ aiGuide: AI_GUIDE,
876
+ markGuide: 'The names of nodes and connecting lines are plain text. Do not use marks inside the text. For something created in the same request, give a $name with "as" and point to it in the input of another operation.',
877
+ operations: [
878
+ {
879
+ op: "node.add",
880
+ input: {
881
+ parent: "Parent node id or $name. Leave it out only when you create the center topic",
882
+ after: "Id of the sibling that comes before. null for the front, leave it out for the end",
883
+ title: `Name. Up to ${MAX_TITLE} characters`,
884
+ side: "Only for a branch directly under the center. left or right. If left out, the side with fewer branches",
885
+ color: `One of ${MM_COLORS.join(", ")}. If left out, the node follows the color of the branch above`
886
+ },
887
+ summary: "Creates a node. There is only one center topic",
888
+ example: [
889
+ { op: "node.add", as: "$center", input: { title: "World of the new novel" } },
890
+ { op: "node.add", as: "$people", input: { parent: "$center", title: "Characters", side: "right" } },
891
+ { op: "node.add", input: { parent: "$people", title: "Hero" } }
892
+ ]
893
+ },
894
+ { op: "node.update", input: { node: "Node id", title: "New name. Unchanged if left out", color: "New color. null removes the color. Unchanged if left out" }, summary: "Changes the name or the color. The id stays the same" },
895
+ {
896
+ op: "node.move",
897
+ input: { node: "Node id", parent: "New parent node id. The current parent if left out", after: "Id of the sibling that comes before. null for the front, leave it out for the end", side: "Side when moving directly under the center. left or right" },
898
+ summary: "Moves a node under another node, within the same parent, or to the other side. The id stays the same. With side alone, the order stays and only the side changes",
899
+ example: [{ op: "node.move", input: { node: "n2", side: "left" } }]
900
+ },
901
+ { op: "node.remove", input: { node: "Node id" }, summary: "Deletes a node, everything under it, and the connecting lines that touch them. Stickers that were attached remain. The center topic cannot be deleted" },
902
+ {
903
+ op: "link.add",
904
+ input: { from: "Id or $name of the node the connecting line starts from", to: "Id or $name of the node the connecting line ends at", label: `Short name that says what the relation is. Up to ${MAX_LABEL} characters. No name if left out` },
905
+ summary: "Joins two different nodes with a connecting line. The arrow is drawn at the to end",
906
+ example: [{ op: "link.add", input: { from: "n6", to: "n7", label: "rules" } }]
907
+ },
908
+ { op: "link.update", input: { link: "Connecting line id", label: "New name. Empty text removes the name" }, summary: "Changes the name of a connecting line" },
909
+ { op: "link.remove", input: { link: "Connecting line id" }, summary: "Deletes a connecting line. The two nodes stay" },
910
+ {
911
+ op: "sticker.add",
912
+ input: { memo: "Memo id from the memo box (memo3), or the $name of a memo created with core.memo.add in the same request", node: "Id or $name of the node to attach to" },
913
+ summary: "Attaches a sticker to a node. Attach an existing memo, or create a new memo in the same request and attach it. Ask the person first",
914
+ example: [
915
+ { op: "sticker.add", input: { memo: "memo3", node: "n2" } },
916
+ { op: "core.memo.add", as: "$m", input: { text: "Why the villain came to rule the harbor is not decided yet" } },
917
+ { op: "sticker.add", input: { memo: "$m", node: "n5" } }
918
+ ]
919
+ },
920
+ { op: "sticker.attach", input: { sticker: "Sticker id", node: "Id of the node to move it to" }, summary: "Moves a sticker to another node" },
921
+ { op: "sticker.remove", input: { sticker: "Sticker id" }, summary: "Removes a sticker. The memo stays in the memo box" }
922
+ ]
923
+ };
924
+ var run = (f) => {
925
+ try {
926
+ return f();
927
+ } catch (e) {
928
+ if (e instanceof Reject) return { error: e.code, summary: e.message };
929
+ throw e;
930
+ }
931
+ };
932
+ var logic = {
933
+ describe: () => DESCRIPTION,
934
+ initialize: () => [],
935
+ plan(state, op, ctx) {
936
+ const handler = OPERATIONS[op.op];
937
+ if (!handler) return { error: "INVALID_INPUT", summary: `Unknown operation ${op.op}. See the list of operations in describe_document` };
938
+ const id = (k) => k === 0 && op.as ? op.as : `$~${ctx.stepIndex}.${k}`;
939
+ return run(() => {
940
+ const input = op.input ?? {};
941
+ if (typeof input !== "object" || Array.isArray(input)) throw new Reject("INVALID_INPUT", "input must be an object");
942
+ const out = handler(state, input, id, ctx);
943
+ return { ...out, summary: /[.?!]$/.test(out.summary) ? out.summary : `${out.summary}.` };
944
+ });
945
+ },
946
+ validate,
947
+ project: (state, scope, mine) => run(() => project(state, scope, mine)),
948
+ extractRefs: refsOf,
949
+ prepareGeneration: () => ({ error: "UNSUPPORTED_CAPABILITY", summary: "The mind map does not make an output rewritten by the AI. The picture becomes an SVG file through renderOutput" }),
950
+ interpretSelection(state, sel) {
951
+ const text = sel.text.trim();
952
+ const candidates = text === "" ? [] : all(state, "node").sort(byIdNumber).filter((t) => {
953
+ const title = t.content.title;
954
+ return title !== "" && (text.includes(title) || title.includes(text));
955
+ }).map((t) => ({ id: t.id, label: resolveLabel(state, t.id), why: "The selected text overlaps the node name" }));
956
+ return { candidates };
957
+ },
958
+ resolveTarget(state, target) {
959
+ const r = state.records[target.id];
960
+ return r ? { found: true, label: resolveLabel(state, r.id) } : { found: false };
961
+ },
962
+ describeSelection,
963
+ renderOutput
964
+ };
965
+ globalThis.__aicPlugin = logic;
966
+ })();