@fengwk/kchart 0.0.0-stage → 0.1.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 (201) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/LICENSE +21 -0
  3. package/NOTICE.md +11 -0
  4. package/README.md +68 -2
  5. package/README.zh-CN.md +69 -0
  6. package/dist/LICENSE +21 -0
  7. package/dist/NOTICE.md +11 -0
  8. package/dist/assets/ICONS-LICENSE.txt +53 -0
  9. package/dist/assets/maps/LICENSE.md +75 -0
  10. package/dist/assets/maps/METADATA.md +9 -0
  11. package/dist/assets/maps/REGIONS.md +28 -0
  12. package/dist/assets/maps/us-states.geojson +1 -0
  13. package/dist/assets/maps/world.geojson +1 -0
  14. package/dist/esm/_virtual/_kchart-families.js +48 -0
  15. package/dist/esm/_virtual/_rolldown/runtime.js +12 -0
  16. package/dist/esm/assets/icons.js +11 -0
  17. package/dist/esm/assets/maps/us-states.js +3 -0
  18. package/dist/esm/assets/maps/world.js +3 -0
  19. package/dist/esm/assets/themes/claude.js +69 -0
  20. package/dist/esm/assets/themes/cloudmusic.js +62 -0
  21. package/dist/esm/assets/themes/kanagawa.js +67 -0
  22. package/dist/esm/assets/themes/obsidian.js +67 -0
  23. package/dist/esm/assets/themes/paper.js +61 -0
  24. package/dist/esm/assets/themes/terminal.js +60 -0
  25. package/dist/esm/assets/themes/tokyo.js +62 -0
  26. package/dist/esm/chart/basic.js +1313 -0
  27. package/dist/esm/chart/catalog.js +476 -0
  28. package/dist/esm/chart/composition.js +844 -0
  29. package/dist/esm/chart/context.js +189 -0
  30. package/dist/esm/chart/distribution.js +442 -0
  31. package/dist/esm/chart/fields.js +67 -0
  32. package/dist/esm/chart/maps.js +563 -0
  33. package/dist/esm/chart/mindmap.js +629 -0
  34. package/dist/esm/chart/multivariate.js +1229 -0
  35. package/dist/esm/chart/registry.js +108 -0
  36. package/dist/esm/chart/relations.js +949 -0
  37. package/dist/esm/chart/spec.js +1415 -0
  38. package/dist/esm/chart/stat-compare.js +302 -0
  39. package/dist/esm/chart/stat-trend.js +181 -0
  40. package/dist/esm/chart/statistics.js +1007 -0
  41. package/dist/esm/chart/unit-variants.js +645 -0
  42. package/dist/esm/chart/variants.js +552 -0
  43. package/dist/esm/core/color.js +197 -0
  44. package/dist/esm/core/context.js +256 -0
  45. package/dist/esm/core/diagnostics.js +23 -0
  46. package/dist/esm/core/format.js +167 -0
  47. package/dist/esm/core/geometry.js +130 -0
  48. package/dist/esm/core/icon-data.js +5 -0
  49. package/dist/esm/core/icons.js +173 -0
  50. package/dist/esm/core/scale.js +112 -0
  51. package/dist/esm/core/scene.js +234 -0
  52. package/dist/esm/core/svg.js +89 -0
  53. package/dist/esm/core/text.js +184 -0
  54. package/dist/esm/core/theme.js +318 -0
  55. package/dist/esm/diagram/agentflow.js +596 -0
  56. package/dist/esm/diagram/architecture.js +729 -0
  57. package/dist/esm/diagram/block.js +988 -0
  58. package/dist/esm/diagram/blocks.js +687 -0
  59. package/dist/esm/diagram/c4.js +1002 -0
  60. package/dist/esm/diagram/class.js +1158 -0
  61. package/dist/esm/diagram/cynefin.js +749 -0
  62. package/dist/esm/diagram/emphasis.js +7 -0
  63. package/dist/esm/diagram/er.js +657 -0
  64. package/dist/esm/diagram/eventmodeling.js +948 -0
  65. package/dist/esm/diagram/gantt-date.js +323 -0
  66. package/dist/esm/diagram/gantt.js +661 -0
  67. package/dist/esm/diagram/gitgraph.js +472 -0
  68. package/dist/esm/diagram/graph-layout.js +4046 -0
  69. package/dist/esm/diagram/graph-parse.js +1366 -0
  70. package/dist/esm/diagram/graph-route.js +490 -0
  71. package/dist/esm/diagram/graph.js +553 -0
  72. package/dist/esm/diagram/ishikawa.js +496 -0
  73. package/dist/esm/diagram/journey.js +512 -0
  74. package/dist/esm/diagram/kanban.js +280 -0
  75. package/dist/esm/diagram/label.js +56 -0
  76. package/dist/esm/diagram/layers.js +385 -0
  77. package/dist/esm/diagram/map-label.js +44 -0
  78. package/dist/esm/diagram/mindmap.js +277 -0
  79. package/dist/esm/diagram/numeric-composition.js +78 -0
  80. package/dist/esm/diagram/numeric-model.js +86 -0
  81. package/dist/esm/diagram/numeric-parse.js +205 -0
  82. package/dist/esm/diagram/numeric-radar.js +355 -0
  83. package/dist/esm/diagram/numeric-sankey.js +201 -0
  84. package/dist/esm/diagram/numeric-xy.js +422 -0
  85. package/dist/esm/diagram/packet.js +175 -0
  86. package/dist/esm/diagram/presentation.js +252 -0
  87. package/dist/esm/diagram/quadrant.js +297 -0
  88. package/dist/esm/diagram/railroad-parse.js +465 -0
  89. package/dist/esm/diagram/railroad.js +294 -0
  90. package/dist/esm/diagram/registry.js +296 -0
  91. package/dist/esm/diagram/relations.js +966 -0
  92. package/dist/esm/diagram/requirement.js +341 -0
  93. package/dist/esm/diagram/sequence.js +1228 -0
  94. package/dist/esm/diagram/shapes.js +366 -0
  95. package/dist/esm/diagram/source.js +538 -0
  96. package/dist/esm/diagram/support.js +153 -0
  97. package/dist/esm/diagram/swimlane-mermaid.js +140 -0
  98. package/dist/esm/diagram/swimlane.js +1186 -0
  99. package/dist/esm/diagram/time.js +748 -0
  100. package/dist/esm/diagram/timeline.js +376 -0
  101. package/dist/esm/diagram/tree-family-icon.js +22 -0
  102. package/dist/esm/diagram/treeview.js +293 -0
  103. package/dist/esm/diagram/usecase.js +852 -0
  104. package/dist/esm/diagram/venn.js +1038 -0
  105. package/dist/esm/diagram/wardley.js +838 -0
  106. package/dist/esm/index.d.ts +133 -0
  107. package/dist/esm/index.js +92 -0
  108. package/dist/esm/interaction.js +561 -0
  109. package/dist/esm/mermaid.d.ts +28 -0
  110. package/dist/esm/mermaid.js +70 -0
  111. package/dist/esm/node.d.ts +68 -0
  112. package/dist/esm/node.js +373 -0
  113. package/dist/esm/package.json +1 -0
  114. package/dist/esm/page-api.js +65 -0
  115. package/dist/esm/runtime.js +352 -0
  116. package/dist/esm/src/families.js +209 -0
  117. package/dist/page-api.js +1 -0
  118. package/dist/units/chart/basic.js +1 -0
  119. package/dist/units/chart/catalog.js +1 -0
  120. package/dist/units/chart/composition.js +1 -0
  121. package/dist/units/chart/context.js +1 -0
  122. package/dist/units/chart/distribution.js +1 -0
  123. package/dist/units/chart/fields.js +1 -0
  124. package/dist/units/chart/maps.js +1 -0
  125. package/dist/units/chart/mindmap.js +1 -0
  126. package/dist/units/chart/multivariate.js +1 -0
  127. package/dist/units/chart/registry.js +1 -0
  128. package/dist/units/chart/relations.js +1 -0
  129. package/dist/units/chart/spec.js +3 -0
  130. package/dist/units/chart/stat-compare.js +1 -0
  131. package/dist/units/chart/stat-trend.js +1 -0
  132. package/dist/units/chart/statistics.js +1 -0
  133. package/dist/units/chart/unit-variants.js +1 -0
  134. package/dist/units/chart/variants.js +1 -0
  135. package/dist/units/core/color.js +1 -0
  136. package/dist/units/core/context.js +1 -0
  137. package/dist/units/core/diagnostics.js +1 -0
  138. package/dist/units/core/format.js +1 -0
  139. package/dist/units/core/geometry.js +1 -0
  140. package/dist/units/core/icon-data.js +1 -0
  141. package/dist/units/core/icons.js +1 -0
  142. package/dist/units/core/scale.js +1 -0
  143. package/dist/units/core/scene.js +1 -0
  144. package/dist/units/core/svg.js +1 -0
  145. package/dist/units/core/text.js +2 -0
  146. package/dist/units/core/theme.js +1 -0
  147. package/dist/units/diagram/agentflow.js +12 -0
  148. package/dist/units/diagram/architecture.js +4 -0
  149. package/dist/units/diagram/block.js +2 -0
  150. package/dist/units/diagram/blocks.js +2 -0
  151. package/dist/units/diagram/c4.js +6 -0
  152. package/dist/units/diagram/class.js +5 -0
  153. package/dist/units/diagram/cynefin.js +4 -0
  154. package/dist/units/diagram/emphasis.js +1 -0
  155. package/dist/units/diagram/er.js +9 -0
  156. package/dist/units/diagram/eventmodeling.js +5 -0
  157. package/dist/units/diagram/gantt-date.js +1 -0
  158. package/dist/units/diagram/gantt.js +3 -0
  159. package/dist/units/diagram/gitgraph.js +2 -0
  160. package/dist/units/diagram/graph-layout.js +4 -0
  161. package/dist/units/diagram/graph-parse.js +9 -0
  162. package/dist/units/diagram/graph-route.js +1 -0
  163. package/dist/units/diagram/graph.js +1 -0
  164. package/dist/units/diagram/ishikawa.js +2 -0
  165. package/dist/units/diagram/journey.js +5 -0
  166. package/dist/units/diagram/kanban.js +2 -0
  167. package/dist/units/diagram/label.js +4 -0
  168. package/dist/units/diagram/layers.js +1 -0
  169. package/dist/units/diagram/map-label.js +1 -0
  170. package/dist/units/diagram/mindmap.js +2 -0
  171. package/dist/units/diagram/numeric-composition.js +1 -0
  172. package/dist/units/diagram/numeric-model.js +1 -0
  173. package/dist/units/diagram/numeric-parse.js +2 -0
  174. package/dist/units/diagram/numeric-radar.js +3 -0
  175. package/dist/units/diagram/numeric-sankey.js +2 -0
  176. package/dist/units/diagram/numeric-xy.js +2 -0
  177. package/dist/units/diagram/packet.js +2 -0
  178. package/dist/units/diagram/presentation.js +2 -0
  179. package/dist/units/diagram/quadrant.js +2 -0
  180. package/dist/units/diagram/railroad-parse.js +4 -0
  181. package/dist/units/diagram/railroad.js +1 -0
  182. package/dist/units/diagram/registry.js +2 -0
  183. package/dist/units/diagram/relations.js +3 -0
  184. package/dist/units/diagram/requirement.js +3 -0
  185. package/dist/units/diagram/sequence.js +2 -0
  186. package/dist/units/diagram/shapes.js +60 -0
  187. package/dist/units/diagram/source.js +20 -0
  188. package/dist/units/diagram/support.js +3 -0
  189. package/dist/units/diagram/swimlane-mermaid.js +3 -0
  190. package/dist/units/diagram/swimlane.js +2 -0
  191. package/dist/units/diagram/time.js +3 -0
  192. package/dist/units/diagram/timeline.js +2 -0
  193. package/dist/units/diagram/tree-family-icon.js +1 -0
  194. package/dist/units/diagram/treeview.js +2 -0
  195. package/dist/units/diagram/usecase.js +3 -0
  196. package/dist/units/diagram/venn.js +2 -0
  197. package/dist/units/diagram/wardley.js +4 -0
  198. package/dist/units/interaction.js +1 -0
  199. package/dist/units/runtime.js +1 -0
  200. package/dist/units.json +712 -0
  201. package/package.json +30 -4
@@ -0,0 +1,596 @@
1
+ import { __exportAll } from "../_virtual/_rolldown/runtime.js";
2
+ import { resolveIconRef } from "../core/icons.js";
3
+ import { parseFlow } from "./graph-parse.js";
4
+ import { layoutGraph } from "./graph-layout.js";
5
+ import { implementations as implementations$1 } from "./graph.js";
6
+ import { normalizeLabel } from "./label.js";
7
+ import { presentation } from "./presentation.js";
8
+ import { flowOutlineName } from "./shapes.js";
9
+ import { ignoredConfigWarning, prepareSource } from "./source.js";
10
+ import { DIRECTIONS, FONT, appearance, error, warning, wrapText } from "./support.js";
11
+ // src/diagram/agentflow.js
12
+ var agentflow_exports = /* @__PURE__ */ __exportAll({ implementations: () => implementations });
13
+ var SHAPES = new Map(Object.entries({
14
+ task: "rounded",
15
+ tool: "subroutine",
16
+ input: "lean-r",
17
+ decision: "diamond",
18
+ refdoc: "lin-doc",
19
+ action: "hexagon",
20
+ connector: "rect",
21
+ roundedRect: "rounded",
22
+ roundedrect: "rounded",
23
+ collapsedGroup: "rounded",
24
+ collapsedgroup: "rounded",
25
+ round: "rect"
26
+ }));
27
+ var OUTLINES = /* @__PURE__ */ new Set([
28
+ "rounded",
29
+ "fr-rect",
30
+ "lean-r",
31
+ "diam",
32
+ "lin-doc",
33
+ "hex",
34
+ "rect"
35
+ ]);
36
+ var UNSAFE_KEYS = /* @__PURE__ */ new Set([
37
+ "__proto__",
38
+ "prototype",
39
+ "constructor"
40
+ ]);
41
+ var EDGE_KINDS = /* @__PURE__ */ new Map([
42
+ ["solid:point", "sequence"],
43
+ ["dashed:none", "reference"],
44
+ ["solid:cross", "failure"]
45
+ ]);
46
+ var LIMITS = {
47
+ nodes: 300,
48
+ edges: 420,
49
+ frames: 60,
50
+ depth: 12,
51
+ connectors: 60,
52
+ lines: 2e3,
53
+ metadataKeys: 24,
54
+ metadataString: 400
55
+ };
56
+ var valueText = (value) => typeof value === "string" ? value : JSON.stringify(value);
57
+ var ICON_PLACEHOLDER = "◌";
58
+ function statements(source, firstLine) {
59
+ const result = [];
60
+ let text = "", line = firstLine, start = line, quote = "", pipe = false;
61
+ let brackets = 0, yamlDepth = 0, yamlStart = 0, yamlLine = 0;
62
+ let metadata = [], classes = [];
63
+ const flush = () => {
64
+ if (text.trim() || metadata.length) result.push({
65
+ text: text.trim(),
66
+ line: start,
67
+ metadata,
68
+ classes
69
+ });
70
+ text = "";
71
+ metadata = [];
72
+ classes = [];
73
+ };
74
+ for (let i = 0; i < source.length; i++) {
75
+ const ch = source[i];
76
+ if (quote) {
77
+ if (!yamlDepth) text += ch;
78
+ if (ch === "\\" && quote === "\"" && source[i + 1] !== "\n" && i + 1 < source.length) {
79
+ if (!yamlDepth) text += source[i + 1];
80
+ i++;
81
+ } else if (ch === quote) {
82
+ if (yamlDepth && quote === "'" && source[i + 1] === "'") i++;
83
+ else quote = "";
84
+ }
85
+ } else if (ch === "\"" || ch === "'" && !/[\w]/.test(source[i - 1] || "")) {
86
+ quote = ch;
87
+ if (!yamlDepth) text += ch;
88
+ } else if (yamlDepth) {
89
+ if (ch === "#" && /\s/.test(source[i - 1] || "")) {
90
+ const end = source.indexOf("\n", i);
91
+ i = end < 0 ? source.length : end - 1;
92
+ } else if (ch === "{") yamlDepth++;
93
+ else if (ch === "}" && --yamlDepth === 0) metadata.push({
94
+ prefix: text.trim(),
95
+ raw: source.slice(yamlStart, i),
96
+ line: yamlLine
97
+ });
98
+ } else if (!brackets && !pipe && source.startsWith("@{", i)) {
99
+ yamlDepth = 1;
100
+ yamlStart = i + 2;
101
+ yamlLine = line;
102
+ i++;
103
+ } else if (!brackets && !pipe && source.startsWith(":::", i)) {
104
+ const cls = /^:::([\w]+(?:-[\w]+)*(?:,[\w]+(?:-[\w]+)*)*)/.exec(source.slice(i));
105
+ if (cls) {
106
+ classes.push({
107
+ prefix: text.trim(),
108
+ names: cls[1].split(",")
109
+ });
110
+ i += cls[0].length - 1;
111
+ } else text += ch;
112
+ } else if (!brackets && !pipe && (ch === ";" || ch === "\n")) {
113
+ flush();
114
+ start = line + Number(ch === "\n");
115
+ } else {
116
+ if (ch === "|" && !brackets) pipe = !pipe;
117
+ if (!pipe) {
118
+ if ("[({".includes(ch) || ch === ">" && /[^\s\-=]/.test(source[i - 1] ?? "") && /^[^\n]*\]/.test(source.slice(i + 1))) brackets++;
119
+ else if ("]})".includes(ch)) brackets = Math.max(0, brackets - 1);
120
+ }
121
+ text += ch;
122
+ }
123
+ if (ch === "\n") line++;
124
+ }
125
+ flush();
126
+ return {
127
+ statements: result,
128
+ unclosed: yamlDepth ? yamlLine : null
129
+ };
130
+ }
131
+ function readMetadata(raw, lang) {
132
+ const read = (wrapped, offset) => {
133
+ const prepared = prepareSource(`---\nconfig:\n${wrapped}\n---\nagentflow-beta`, {
134
+ syntax: "native",
135
+ lang
136
+ });
137
+ return {
138
+ value: prepared.config.values?.[0],
139
+ problem: prepared.errors[0],
140
+ offset
141
+ };
142
+ };
143
+ if (!raw.trim()) return { value: {} };
144
+ if (!raw.includes("\n")) return read(` values: [{${raw}}]`, 3);
145
+ const rows = raw.split("\n");
146
+ const indent = Math.min(...rows.filter((row) => row.trim()).map((row) => row.search(/\S/)));
147
+ return read(` values:\n -\n${rows.map((row) => ` ${row.slice(indent).replace(/,([ \t]*(?:#.*)?)$/, "$1")}`).join("\n")}`, 5);
148
+ }
149
+ function clean(value) {
150
+ if (Array.isArray(value)) return value.map(clean);
151
+ if (value && typeof value === "object") return Object.fromEntries(Object.entries(value).filter(([key]) => !UNSAFE_KEYS.has(key)).map(([key, child]) => [key, clean(child)]));
152
+ return value;
153
+ }
154
+ function parse(body, info) {
155
+ const { lang, firstLine, headerLine } = info;
156
+ const errors = [], warnings = /* @__PURE__ */ new Map(), ignored = /* @__PURE__ */ new Map();
157
+ const fail = (line, zh, en) => errors.push(error(lang, line, zh, en, "检查 agentflow 语法", "Check the agentflow syntax"));
158
+ const warn = (code, line, zh, en) => {
159
+ const item = warning(code, lang, line, zh, en), key = `${code}:${item.message}`;
160
+ if (!warnings.has(key) || warnings.get(key).line > line) warnings.set(key, item);
161
+ };
162
+ const ignore = (key, line) => {
163
+ if (!ignored.has(key)) ignored.set(key, line);
164
+ };
165
+ const look = presentation({
166
+ lang,
167
+ classKeyword: "class"
168
+ });
169
+ const nodes = /* @__PURE__ */ new Map(), frames = /* @__PURE__ */ new Map(), edges = [], connectors = /* @__PURE__ */ new Set(), global = /* @__PURE__ */ new Set();
170
+ const attributes = [], stack = [];
171
+ let seq = 0, direction = "TB";
172
+ const candidate = (value, line) => {
173
+ const dir = value.toUpperCase();
174
+ if (dir === "TD") return "TB";
175
+ if (DIRECTIONS.includes(dir)) return dir;
176
+ if ([
177
+ "BR",
178
+ "<",
179
+ ">",
180
+ "^",
181
+ "V"
182
+ ].includes(dir)) ignore(`direction ${value}`, line);
183
+ else fail(line, `无效的方向 ${value}`, `Invalid direction ${value}`);
184
+ return null;
185
+ };
186
+ const normalized = (raw, line, type) => {
187
+ const text = valueText(raw) ?? "";
188
+ const result = normalizeLabel(type === "markdown" && !text.startsWith("`") ? `\`${text}\`` : text, {
189
+ lang,
190
+ line
191
+ });
192
+ for (const item of result.diagnostics) {
193
+ const key = `${item.code}:${item.message}`;
194
+ if (!warnings.has(key) || warnings.get(key).line > item.line) warnings.set(key, item);
195
+ }
196
+ return result;
197
+ };
198
+ const [head, ...inline] = (info.params || "").split(";");
199
+ if (head.trim()) direction = candidate(head.trim(), headerLine) || direction;
200
+ const input = inline.length ? `${inline.join(";")}\n${body}` : body;
201
+ if (body.split("\n").length > LIMITS.lines || input.length > 5e4) {
202
+ fail(firstLine, `Agentflow 最多 ${LIMITS.lines} 行、50000 个字符,请拆分图表`, `Agentflow supports up to ${LIMITS.lines} lines and 50,000 characters; split the diagram`);
203
+ return {
204
+ model: null,
205
+ errors
206
+ };
207
+ }
208
+ const scanned = statements(input, inline.length ? headerLine : firstLine);
209
+ if (scanned.unclosed) fail(scanned.unclosed, "元数据缺少结束括号", "Unclosed metadata block");
210
+ const frameIds = new Set(scanned.statements.map((st) => /^flow\s+([^\s["']+)/i.exec(st.text)?.[1]).filter(Boolean));
211
+ for (const st of scanned.statements) {
212
+ let { text } = st;
213
+ const { line } = st;
214
+ if (look.accept(text, line)) continue;
215
+ const inGlobal = stack.includes(null);
216
+ const parent = inGlobal ? null : stack.at(-1)?.id ?? null;
217
+ const flow = /^flow(?:\s+([\s\S]*))?$/i.exec(text);
218
+ const connector = /^connector\s+([\s\S]+)$/i.exec(text);
219
+ const dir = /^direction\s+(\S+)$/i.exec(text);
220
+ let declared;
221
+ if (flow) {
222
+ const rest = flow[1] || "";
223
+ const match = /^([^\s["']+)(?:\s*\[([\s\S]*)\]|\s+([\s\S]*))?$/.exec(rest);
224
+ const id = match?.[1] ?? `flow-${line}-${seq}`;
225
+ declared = {
226
+ id,
227
+ title: (match?.[2] ?? match?.[3] ?? (rest || id)).replace(/^("|')([\s\S]*)\1$/, "$2"),
228
+ parent,
229
+ line,
230
+ seq: seq++,
231
+ depth: stack.filter(Boolean).length + 1
232
+ };
233
+ if (frames.has(id)) fail(line, `流程重复:${id}`, `Duplicate flow: ${id}`);
234
+ else frames.set(id, declared);
235
+ stack.push(declared);
236
+ if (declared.depth > LIMITS.depth) fail(line, `流程最多嵌套 ${LIMITS.depth} 层,请拆分图表`, `Flow nesting is limited to ${LIMITS.depth} levels; split the diagram`);
237
+ } else if (/^global$/i.test(text)) stack.push(null);
238
+ else if (/^end$/i.test(text)) {
239
+ if (stack.length) stack.pop();
240
+ else fail(line, "多余的 end", "Unexpected end");
241
+ } else if (dir) direction = candidate(dir[1], line) || direction;
242
+ else if (text) {
243
+ if (connector) text = connector[1].replace(/^(\S+)\s+(".*"|'.*')$/, "$1[$2]");
244
+ const parsed = parseFlow([{
245
+ text,
246
+ line
247
+ }], { direction });
248
+ errors.push(...parsed.errors.filter((item) => !/^(?:节点|连线) \d+ (?:个|条),超过上限/.test(item.message)));
249
+ const metadataOnly = !connector && st.metadata.length && parsed.model.edges.length === 0 && parsed.model.nodes.length === 1 && parsed.model.nodes[0].id === text && st.classes.length === 0;
250
+ for (const parsedNode of parsed.model.nodes) {
251
+ const { id, label, shape, explicitLabel } = parsedNode;
252
+ if (frameIds.has(id) || metadataOnly) continue;
253
+ let node = nodes.get(id);
254
+ if (!node) {
255
+ node = {
256
+ id,
257
+ label: id,
258
+ parent,
259
+ seq: seq++,
260
+ line,
261
+ shape: "rounded"
262
+ };
263
+ nodes.set(id, node);
264
+ } else if (node.parent === null && parent !== null) node.parent = parent;
265
+ if (inGlobal) global.add(id);
266
+ if (explicitLabel) Object.assign(node, {
267
+ label,
268
+ explicitLabel,
269
+ shape,
270
+ labelLine: line
271
+ });
272
+ if (connector) {
273
+ connectors.add(id);
274
+ node.connector = true;
275
+ declared = node;
276
+ }
277
+ }
278
+ for (const edge of parsed.model.edges) {
279
+ const semantic = !edge.startArrow && EDGE_KINDS.get(`${edge.style}:${edge.endMark}`);
280
+ if (!semantic || edge.extra) fail(line, "Agentflow 连线使用 -->、-.- 或 --x", "Agentflow edges use -->, -.- or --x");
281
+ edge.edgeSemantic = semantic;
282
+ edge.seq = edges.length;
283
+ edges.push(edge);
284
+ }
285
+ for (const cls of st.classes) {
286
+ const target = lastNode(connector ? cls.prefix.replace(/^connector\s+/i, "") : cls.prefix, line);
287
+ if (target) look.addClasses(target, cls.names, line);
288
+ }
289
+ for (const attr of st.metadata) {
290
+ attr.id = declared?.id || (metadataOnly ? text : lastNode(attr.prefix, line));
291
+ attr.parent = parent;
292
+ if (inGlobal && attr.id) global.add(attr.id);
293
+ }
294
+ }
295
+ for (const attr of st.metadata) attributes.push({
296
+ ...attr,
297
+ id: declared?.id ?? attr.id
298
+ });
299
+ }
300
+ if (stack.length) fail(scanned.statements.at(-1)?.line ?? firstLine, "流程块缺少 end", "Unclosed flow or global block");
301
+ const edgeIds = new Map(edges.filter((edge) => edge.id).map((edge) => [edge.id, edge]));
302
+ for (const attr of attributes) {
303
+ if (!attr.id) {
304
+ fail(attr.line, "元数据缺少目标", "Metadata requires a target");
305
+ continue;
306
+ }
307
+ const { value, problem, offset } = readMetadata(attr.raw, lang);
308
+ if (problem || value == null || typeof value !== "object" || Array.isArray(value)) {
309
+ fail(attr.line + Math.max(0, (problem?.line ?? offset) - offset), "无效的元数据 YAML", "Invalid metadata YAML");
310
+ continue;
311
+ }
312
+ const data = clean(value);
313
+ let target = frames.get(attr.id) ?? edgeIds.get(attr.id) ?? nodes.get(attr.id);
314
+ if (!target) {
315
+ target = {
316
+ id: attr.id,
317
+ label: attr.id,
318
+ parent: attr.parent ?? null,
319
+ seq: seq++,
320
+ line: attr.line,
321
+ shape: "rounded"
322
+ };
323
+ nodes.set(attr.id, target);
324
+ }
325
+ if (nodes.get(attr.id) === target && attr.line < target.line && attr.parent != null) target.parent = attr.parent;
326
+ target.metadata = {
327
+ ...target.metadata,
328
+ ...data
329
+ };
330
+ target.metadataLine ??= attr.line;
331
+ }
332
+ const applyMetadata = (item) => {
333
+ const data = item.metadata;
334
+ if (!data) return;
335
+ const at = item.metadataLine;
336
+ let longString = false;
337
+ JSON.stringify(data, (_, value) => {
338
+ if (typeof value === "string" && value.length > LIMITS.metadataString) longString = true;
339
+ return value;
340
+ });
341
+ if (longString) fail(at, "元数据文字过长", "Metadata string is too long");
342
+ if (Object.keys(data).length > LIMITS.metadataKeys) fail(at, "元数据过多", "Too many metadata keys");
343
+ for (const key of [
344
+ "algorithm",
345
+ "curve",
346
+ "animate",
347
+ "animation"
348
+ ]) if (key in data) ignore(key, at);
349
+ };
350
+ for (const frame of frames.values()) {
351
+ applyMetadata(frame);
352
+ const data = frame.metadata ?? {};
353
+ if (data.view === "collapsed") frame.view = data.view;
354
+ else if (data.view != null && data.view !== "expanded") ignore("view", frame.metadataLine);
355
+ const label = normalized(data.label ?? frame.title, data.label == null ? frame.line : frame.metadataLine, data.labelType);
356
+ frame.title = label.text;
357
+ frame.icon = label.icon;
358
+ frame.class = look.statusOf(frame.id);
359
+ frame.link = look.linkOf(frame.id);
360
+ }
361
+ for (const node of nodes.values()) {
362
+ applyMetadata(node);
363
+ const data = node.metadata ?? {};
364
+ const authored = data.shape ?? node.shape;
365
+ node.shapeAlias = data.shape ?? null;
366
+ const shape = SHAPES.get(authored) ?? (authored === "lean_right" ? "lean-r" : authored);
367
+ if (OUTLINES.has(flowOutlineName(shape))) node.shape = shape;
368
+ else {
369
+ node.shape = "rounded";
370
+ warn("DIAGRAM_HINT", data.shape == null ? node.labelLine : node.metadataLine, `形状 ${valueText(authored)} 改用 task`, `Shape ${valueText(authored)} drawn as task`);
371
+ }
372
+ node.global = global.has(node.id);
373
+ if (node.global) node.parent = null;
374
+ const label = normalized(data.label ?? node.label, data.label == null ? node.labelLine ?? node.line : node.metadataLine, data.labelType);
375
+ node.label = label.text;
376
+ const icon = resolveIconRef(data.icon);
377
+ if (data.icon != null && !icon) warn("IGNORED_ASSET", node.metadataLine, `未知图标 ${valueText(data.icon)}`, `Unknown icon ${valueText(data.icon)}`);
378
+ if ("img" in data) warn("IGNORED_ASSET", node.metadataLine, "忽略远程图片", "Ignored remote image");
379
+ node.icon = icon || label.icon || (node.connector || authored === "connector" ? resolveIconRef("plug") : null);
380
+ node.class = look.statusOf(node.id);
381
+ node.link = look.linkOf(node.id);
382
+ }
383
+ for (const edge of edges) {
384
+ applyMetadata(edge);
385
+ edge.fromFrame = frames.has(edge.from);
386
+ edge.toFrame = frames.has(edge.to);
387
+ if (edge.label != null || edge.metadata?.label != null) {
388
+ const label = normalized(edge.metadata?.label ?? edge.label, edge.metadata?.label == null ? edge.line : edge.metadataLine, edge.metadata?.labelType);
389
+ edge.label = label.text;
390
+ edge.icon = label.icon;
391
+ }
392
+ edge.status = look.edgeStatusOf(edge.seq);
393
+ }
394
+ for (const [size, key] of [
395
+ [nodes.size, "nodes"],
396
+ [edges.length, "edges"],
397
+ [frames.size, "frames"],
398
+ [connectors.size, "connectors"]
399
+ ]) if (size > LIMITS[key]) fail(firstLine, `${key} 超过上限 ${LIMITS[key]},请拆分图表`, `Too many ${key} (limit ${LIMITS[key]}); split the diagram`);
400
+ if (ignored.size) errors.push(ignoredConfigWarning(lang, Math.min(...ignored.values()), [...ignored.keys()].join(", ")));
401
+ return {
402
+ model: {
403
+ type: "flowchart",
404
+ variant: "agentflow",
405
+ direction,
406
+ nodes: [...nodes.values()],
407
+ edges,
408
+ subgraphs: [...frames.values()],
409
+ connectors: [...connectors].map((id) => nodes.get(id))
410
+ },
411
+ errors: [
412
+ ...errors,
413
+ ...warnings.values(),
414
+ ...look.diagnostics()
415
+ ]
416
+ };
417
+ }
418
+ function lastNode(prefix, line) {
419
+ const result = parseFlow([{
420
+ text: prefix.trim(),
421
+ line
422
+ }]).model;
423
+ return result.edges.at(-1)?.to ?? result.nodes.at(-1)?.id;
424
+ }
425
+ function layout(model, options, info) {
426
+ const byId = new Map(model.subgraphs.map((frame) => [frame.id, frame]));
427
+ const counts = /* @__PURE__ */ new Map();
428
+ for (const node of model.nodes) for (let parent = node.parent; parent; parent = byId.get(parent)?.parent) counts.set(parent, (counts.get(parent) ?? 0) + 1);
429
+ const en = /^en\b/i.test(info.lang);
430
+ const config = Object.entries(info.config ?? {}).find(([key]) => key.toLowerCase() === "agentflow")?.[1];
431
+ const wrap = Object.entries(config ?? {}).find(([key]) => key.toLowerCase() === "wrappingwidth")?.[1];
432
+ const wrappingWidth = typeof wrap === "number" && wrap > 0 ? Math.max(68, Math.min(200, wrap)) : 120;
433
+ const nodes = model.nodes.map((node) => {
434
+ const { params, returns } = node.metadata ?? {};
435
+ const value = [params, returns].filter((value) => value != null).map(valueText).join(" → ");
436
+ const desc = value && options.measure(value, FONT.body) <= wrappingWidth ? [value] : void 0;
437
+ const label = wrap == null ? node.label : wrapText(node.label, wrappingWidth, FONT.title, options.measure).join("\n");
438
+ return {
439
+ ...node,
440
+ label,
441
+ desc
442
+ };
443
+ });
444
+ const subgraphs = model.subgraphs.map((frame) => {
445
+ if (frame.view !== "collapsed") return frame.icon && !frame.title ? {
446
+ ...frame,
447
+ title: ICON_PLACEHOLDER,
448
+ iconOnly: true
449
+ } : frame;
450
+ const count = counts.get(frame.id) ?? 0;
451
+ return {
452
+ ...frame,
453
+ desc: [en ? `${count} ${count === 1 ? "node" : "nodes"}` : `${count} 个节点`]
454
+ };
455
+ });
456
+ const edges = model.edges.map((edge) => edge.icon && !edge.label ? {
457
+ ...edge,
458
+ label: ICON_PLACEHOLDER,
459
+ iconOnly: true
460
+ } : edge);
461
+ const edgeIcons = edges.some((edge) => edge.icon), frameIcons = subgraphs.some((frame) => frame.icon);
462
+ const graphOptions = {
463
+ ...options,
464
+ measure: (value, font) => {
465
+ const measured = options.measure(value, font);
466
+ if (font === FONT.title) return Math.max(68, measured + 22);
467
+ if (edgeIcons && font === FONT.label || frameIcons && font.size === 13 && font.weight === 600) return measured + 20;
468
+ return measured;
469
+ }
470
+ };
471
+ return layoutGraph({
472
+ ...model,
473
+ nodes,
474
+ subgraphs,
475
+ edges
476
+ }, graphOptions, info);
477
+ }
478
+ function drawIcon(ctx, icon, x, y, color) {
479
+ const [left, top, w, h] = icon.viewBox.split(/\s+/).map(Number), scale = 14 / Math.max(w, h);
480
+ return ctx.el("g", { transform: `translate(${x - (left + w / 2) * scale} ${y - (top + h / 2) * scale}) scale(${scale})` }, icon.elements.map(({ tag, attrs }) => ctx.shape(tag, {
481
+ ...attrs,
482
+ ...icon.set === "simple" ? { fill: color } : {
483
+ fill: "none",
484
+ stroke: color,
485
+ "stroke-width": 1.6,
486
+ "stroke-linecap": "round",
487
+ "stroke-linejoin": "round"
488
+ }
489
+ })));
490
+ }
491
+ function placeIcon(ctx, group, lines, item, color) {
492
+ const first = lines[0];
493
+ if (!first) return;
494
+ const a = first.attrs, centered = a["text-anchor"] === "middle";
495
+ const x = Number(a.x), y = Number(a.y);
496
+ const half = centered ? ctx.measure(first.text, a["font-size"], a["font-weight"]) / 2 : 0;
497
+ if (item.iconOnly) first.text = "";
498
+ else for (const line of lines) line.attrs.x = Number(line.attrs.x) + (centered ? 10 : 20);
499
+ group.children.push(drawIcon(ctx, item.icon, item.iconOnly ? x + (centered ? 0 : 8) : x + (centered ? -half - 2 : 8), y, color));
500
+ }
501
+ function tooltip(item) {
502
+ return [item.iconOnly ? null : item.label ?? item.title, ...Object.entries(item.metadata ?? {}).map(([key, value]) => `${key}: ${valueText(value)}`)].filter(Boolean).join("\n");
503
+ }
504
+ function scene(geometry, ctx, info) {
505
+ const scene = implementations$1.flowchart.scene(geometry, ctx, info), a = appearance(ctx);
506
+ const nodes = new Map(geometry.model.nodes.map((item) => [item.id, item]));
507
+ const frames = new Map(geometry.model.subgraphs.map((item) => [item.id, item]));
508
+ const bounds = new Map(geometry.nodes.map((item) => [item.id, item]));
509
+ const edgeLayer = scene.children.find((item) => item.meta?.kind === "edges");
510
+ const labelsLayer = scene.children.find((item) => item.meta?.kind === "edge-labels");
511
+ const defs = scene.children.find((item) => item.tag === "defs");
512
+ const edgeModels = new Map(geometry.model.edges.map((edge) => [edge.seq, edge]));
513
+ let index = 0, labelIndex = 0;
514
+ for (const route of geometry.edges) {
515
+ if (!route.segments?.length) continue;
516
+ const edge = edgeModels.get(route.id);
517
+ const group = edgeLayer.children[index++], path = group.children.find((item) => item.tag === "path");
518
+ const failure = edge.edgeSemantic === "failure";
519
+ const color = failure ? a.danger : a[edge.status] || a.fg4;
520
+ path.attrs.stroke = color;
521
+ if (edge.edgeSemantic === "reference") path.attrs["stroke-dasharray"] = "4 4";
522
+ if (failure || edge.edgeSemantic === "sequence" && edge.status) {
523
+ const id = `agentflow-marker-${failure ? "failure" : edge.status}`;
524
+ if (!defs.children.some((item) => item.attrs.id === id)) defs.children.push(ctx.el("marker", {
525
+ id,
526
+ viewBox: failure ? "0 0 18 10" : "0 0 8 8",
527
+ refX: failure ? 18 : 8,
528
+ refY: failure ? 5 : 4,
529
+ markerWidth: failure ? 18 : 8,
530
+ markerHeight: failure ? 10 : 8,
531
+ markerUnits: "userSpaceOnUse",
532
+ orient: "auto"
533
+ }, failure ? [ctx.path("M0 1 L8 5 L0 9 Z", { fill: color }), ctx.path("M10 1 L17 9 M10 9 L17 1", {
534
+ fill: "none",
535
+ stroke: color,
536
+ "stroke-width": 1.5
537
+ })] : [ctx.path("M0 0 L8 4 L0 8 Z", { fill: color })]));
538
+ path.attrs["marker-end"] = `url(#${id})`;
539
+ }
540
+ if (edge.iconOnly) group.children[0].text = `${route.from} → ${route.to}`;
541
+ if (edge.metadata) group.children[0].text += `\n${tooltip(edge)}`;
542
+ if (route.labelRect && route.labelLines?.length) {
543
+ const label = labelsLayer.children[labelIndex++];
544
+ const lines = label.children.filter((item) => item.tag === "text");
545
+ if (failure || edge.status) for (const item of lines) item.attrs.fill = color;
546
+ if (edge.icon) placeIcon(ctx, label, lines, edge, color);
547
+ }
548
+ }
549
+ const decorateNode = (group) => {
550
+ const node = nodes.get(group.meta?.id), box = bounds.get(node?.id);
551
+ if (!node || !box) return group;
552
+ if (node.metadata && group.children[0]?.tag === "title") group.children[0].text = tooltip(node);
553
+ const title = group.children.filter((item) => item.meta?.kind === "node-label");
554
+ for (const label of title) {
555
+ label.attrs["font-size"] = 13;
556
+ label.attrs["font-weight"] = 400;
557
+ }
558
+ const outline = group.children.find((item) => ["rect", "polygon"].includes(item.tag));
559
+ if (outline && !node.class) outline.attrs.stroke = ctx.tint(a.node, a.frameInk || a.fg3, .32);
560
+ if (node.icon) {
561
+ if (title.length) placeIcon(ctx, group, title, node, a.fg3);
562
+ else group.children.push(drawIcon(ctx, node.icon, box.x + box.w / 2, box.y + box.h / 2, a.fg3));
563
+ }
564
+ return node.link ? ctx.link(node.link.href, [group], node.link) : group;
565
+ };
566
+ const nodeLayer = scene.children.find((item) => item.meta?.kind === "nodes");
567
+ if (nodeLayer.children) nodeLayer.children = nodeLayer.children.map(decorateNode);
568
+ scene.children = scene.children.map((item) => {
569
+ if (item.meta?.kind !== "frame") return item;
570
+ const frame = frames.get(item.meta.id);
571
+ if (frame?.metadata) item.children.push({
572
+ ...ctx.el("title"),
573
+ text: tooltip(frame)
574
+ });
575
+ const outline = item.children[0];
576
+ outline.attrs.stroke = frame?.class ? a[frame.class] : a.frameInk || ctx.tint(a.surface, a.fg3, .3);
577
+ return item;
578
+ });
579
+ const frameTitles = scene.children.find((item) => item.meta?.kind === "frame-titles");
580
+ if (frameTitles.children) frameTitles.children = frameTitles.children.map((item) => {
581
+ const frame = frames.get(item.meta.id);
582
+ if (frame?.icon) placeIcon(ctx, item, item.children.filter((part) => part.meta?.kind === "frame-title"), frame, a.fg3);
583
+ const link = frame?.link;
584
+ return link ? ctx.link(link.href, [item], link) : item;
585
+ });
586
+ return scene;
587
+ }
588
+ var implementations = { "agentflow-beta": {
589
+ optionMode: "raw",
590
+ acceptsPresentation: true,
591
+ honoredConfig: ["agentflow.wrappingWidth"],
592
+ parse,
593
+ layout,
594
+ scene
595
+ } };
596
+ export { agentflow_exports, implementations };