@wowok/agent-mcp 2.6.0 → 2.6.3

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 (186) hide show
  1. package/README.md +5 -3
  2. package/dist/customer/info-puzzle.d.ts +1 -1
  3. package/dist/customer/info-puzzle.js +4 -2
  4. package/dist/customer/risk-assessment.js +26 -4
  5. package/dist/customer/types.d.ts +2 -0
  6. package/dist/examples/guard-template-balance-check.json +38 -0
  7. package/dist/examples/guard-template-time-lock.json +39 -0
  8. package/dist/examples/machine-template-7node-rental.json +114 -0
  9. package/dist/examples/rental-ziroom-machine-create.json +137 -0
  10. package/dist/examples/rental-ziroom-permission-create.json +35 -0
  11. package/dist/examples/rental-ziroom-service-create.json +80 -0
  12. package/dist/examples/retail-myshop-service-create.json +88 -0
  13. package/dist/extensions/capability-manifest.d.ts +125 -0
  14. package/dist/extensions/capability-manifest.js +594 -0
  15. package/dist/extensions/constraint-registry.d.ts +24 -0
  16. package/dist/extensions/constraint-registry.js +196 -0
  17. package/dist/extensions/index.d.ts +12 -0
  18. package/dist/extensions/index.js +6 -0
  19. package/dist/extensions/metric-registry.d.ts +26 -0
  20. package/dist/extensions/metric-registry.js +257 -0
  21. package/dist/extensions/mode-evaluator.d.ts +15 -0
  22. package/dist/extensions/mode-evaluator.js +170 -0
  23. package/dist/extensions/modes.d.ts +2 -0
  24. package/dist/extensions/modes.js +493 -0
  25. package/dist/extensions/registry.d.ts +50 -0
  26. package/dist/extensions/registry.js +662 -0
  27. package/dist/extensions/types.d.ts +219 -0
  28. package/dist/extensions/types.js +1 -0
  29. package/dist/index.js +50 -0
  30. package/dist/knowledge/deployment-scanner.d.ts +3 -0
  31. package/dist/knowledge/deployment-scanner.js +64 -3
  32. package/dist/knowledge/fund-layer.d.ts +72 -0
  33. package/dist/knowledge/fund-layer.js +420 -0
  34. package/dist/knowledge/guard-render.d.ts +57 -0
  35. package/dist/knowledge/guard-render.js +700 -0
  36. package/dist/knowledge/guard-risk.d.ts +13 -0
  37. package/dist/knowledge/guard-risk.js +57 -0
  38. package/dist/knowledge/guard-submission-prompt.d.ts +31 -0
  39. package/dist/knowledge/guard-submission-prompt.js +171 -0
  40. package/dist/knowledge/guard-templates.js +278 -0
  41. package/dist/knowledge/machine-ledger.js +1 -1
  42. package/dist/knowledge/machine-render.d.ts +41 -0
  43. package/dist/knowledge/machine-render.js +565 -0
  44. package/dist/knowledge/machine-templates.js +24 -5
  45. package/dist/knowledge/reward-confirm.js +2 -2
  46. package/dist/knowledge/reward-puzzle.js +1 -1
  47. package/dist/knowledge/reward-risk.js +9 -9
  48. package/dist/knowledge/reward-templates.js +2 -2
  49. package/dist/knowledge/service-confirm.d.ts +15 -6
  50. package/dist/knowledge/service-confirm.js +119 -11
  51. package/dist/knowledge/service-context.js +1 -1
  52. package/dist/knowledge/service-ledger.js +1 -1
  53. package/dist/knowledge/service-risk.d.ts +1 -1
  54. package/dist/knowledge/service-risk.js +3 -3
  55. package/dist/knowledge/service-templates.js +2 -2
  56. package/dist/knowledge/service-translation.d.ts +1 -1
  57. package/dist/knowledge/service-translation.js +7 -7
  58. package/dist/knowledge/tool-constraints.js +6 -2
  59. package/dist/project/deployment-bridge.d.ts +1 -1
  60. package/dist/project/deployment-bridge.js +31 -7
  61. package/dist/project/deployment-doc.d.ts +3 -0
  62. package/dist/project/deployment-doc.js +76 -14
  63. package/dist/project/edit-planner.d.ts +123 -0
  64. package/dist/project/edit-planner.js +1371 -0
  65. package/dist/project/evaluation.d.ts +2 -0
  66. package/dist/project/evaluation.js +873 -88
  67. package/dist/project/graph-builder.d.ts +4 -1
  68. package/dist/project/graph-builder.js +132 -62
  69. package/dist/project/graph.d.ts +1 -0
  70. package/dist/project/handlers.d.ts +317 -6
  71. package/dist/project/handlers.js +1171 -19
  72. package/dist/project/stage-gate.d.ts +4 -0
  73. package/dist/project/stage-gate.js +64 -5
  74. package/dist/project/task-tracker.d.ts +26 -0
  75. package/dist/project/task-tracker.js +78 -0
  76. package/dist/safety/preview.js +16 -0
  77. package/dist/schema/call/allocation.d.ts +16 -16
  78. package/dist/schema/call/allocation.js +2 -2
  79. package/dist/schema/call/arbitration.js +19 -6
  80. package/dist/schema/call/base.d.ts +21 -13
  81. package/dist/schema/call/base.js +27 -6
  82. package/dist/schema/call/bridge.d.ts +5 -5
  83. package/dist/schema/call/bridge.js +3 -1
  84. package/dist/schema/call/contact.js +2 -2
  85. package/dist/schema/call/demand.d.ts +23 -31
  86. package/dist/schema/call/demand.js +2 -2
  87. package/dist/schema/call/guard.js +2 -2
  88. package/dist/schema/call/machine.d.ts +1976 -752
  89. package/dist/schema/call/machine.js +50 -4
  90. package/dist/schema/call/order.d.ts +149 -228
  91. package/dist/schema/call/order.js +4 -3
  92. package/dist/schema/call/payment.d.ts +183 -3
  93. package/dist/schema/call/payment.js +21 -3
  94. package/dist/schema/call/permission.js +2 -2
  95. package/dist/schema/call/personal.d.ts +241 -52
  96. package/dist/schema/call/progress.d.ts +53 -61
  97. package/dist/schema/call/progress.js +18 -4
  98. package/dist/schema/call/repository.d.ts +23 -31
  99. package/dist/schema/call/repository.js +2 -2
  100. package/dist/schema/call/reward.js +3 -3
  101. package/dist/schema/call/semantic.d.ts +1 -1
  102. package/dist/schema/call/semantic.js +37 -2
  103. package/dist/schema/call/service.d.ts +275 -127
  104. package/dist/schema/call/service.js +89 -13
  105. package/dist/schema/call/treasury.js +3 -3
  106. package/dist/schema/common/index.d.ts +13 -2
  107. package/dist/schema/common/index.js +80 -15
  108. package/dist/schema/local/index.d.ts +32 -35
  109. package/dist/schema/local/index.js +28 -6
  110. package/dist/schema/messenger/index.d.ts +274 -46
  111. package/dist/schema/operations.d.ts +1264 -544
  112. package/dist/schema/operations.js +65 -7
  113. package/dist/schema/project/index.d.ts +2846 -167
  114. package/dist/schema/project/index.js +570 -16
  115. package/dist/schema/query/index.d.ts +922 -349
  116. package/dist/schema/query/index.js +205 -42
  117. package/dist/schema/schema-query/index.d.ts +65 -3
  118. package/dist/schema/schema-query/index.js +38 -5
  119. package/dist/schema/utils/node-parser.js +20 -4
  120. package/dist/schema/utils/object-type-utils.d.ts +12 -0
  121. package/dist/schema/utils/object-type-utils.js +35 -0
  122. package/dist/schema/utils/permission-machine-check.d.ts +49 -0
  123. package/dist/schema/utils/permission-machine-check.js +121 -0
  124. package/dist/schema/utils/skills-recommendation.d.ts +2 -0
  125. package/dist/schema/utils/skills-recommendation.js +76 -0
  126. package/dist/schema-query/index.d.ts +20 -1
  127. package/dist/schema-query/index.js +306 -4
  128. package/dist/schemas/account_operation.output.json +7 -1
  129. package/dist/schemas/account_operation.schema.json +3 -3
  130. package/dist/schemas/bridge_operation.output.json +6 -0
  131. package/dist/schemas/bridge_operation.schema.json +1 -1
  132. package/dist/schemas/guard-templates.json +379 -0
  133. package/dist/schemas/guard2file.schema.json +1 -1
  134. package/dist/schemas/index.json +1 -1
  135. package/dist/schemas/local_info_operation.output.json +6 -0
  136. package/dist/schemas/local_mark_operation.output.json +7 -1
  137. package/dist/schemas/local_mark_operation.schema.json +1 -1
  138. package/dist/schemas/machineNode2file.schema.json +1 -1
  139. package/dist/schemas/messenger_operation.schema.json +4 -4
  140. package/dist/schemas/onchain_events.output.json +1 -1
  141. package/dist/schemas/onchain_operations.output.json +2820 -0
  142. package/dist/schemas/onchain_operations.schema.json +444 -330
  143. package/dist/schemas/onchain_operations_allocation.schema.json +37 -28
  144. package/dist/schemas/onchain_operations_arbitration.schema.json +16 -14
  145. package/dist/schemas/onchain_operations_contact.schema.json +10 -10
  146. package/dist/schemas/onchain_operations_demand.schema.json +10 -10
  147. package/dist/schemas/onchain_operations_gen_passport.schema.json +16 -16
  148. package/dist/schemas/onchain_operations_gen_proof.schema.json +2 -2
  149. package/dist/schemas/onchain_operations_guard.schema.json +2 -2
  150. package/dist/schemas/onchain_operations_machine.schema.json +43 -35
  151. package/dist/schemas/onchain_operations_order.schema.json +72 -78
  152. package/dist/schemas/onchain_operations_payment.schema.json +141 -111
  153. package/dist/schemas/onchain_operations_permission.schema.json +3 -3
  154. package/dist/schemas/onchain_operations_personal.schema.json +7 -7
  155. package/dist/schemas/onchain_operations_progress.schema.json +9 -9
  156. package/dist/schemas/onchain_operations_proof.schema.json +8 -8
  157. package/dist/schemas/onchain_operations_repository.schema.json +10 -10
  158. package/dist/schemas/onchain_operations_reward.schema.json +14 -14
  159. package/dist/schemas/onchain_operations_service.schema.json +119 -48
  160. package/dist/schemas/onchain_operations_treasury.schema.json +11 -11
  161. package/dist/schemas/onchain_table_data.output.json +33 -25
  162. package/dist/schemas/onchain_table_data.schema.json +22 -21
  163. package/dist/schemas/project_operation.output.json +2194 -23
  164. package/dist/schemas/project_operation.schema.json +84 -6
  165. package/dist/schemas/query_toolkit.output.json +108 -80
  166. package/dist/schemas/query_toolkit.schema.json +12 -12
  167. package/dist/schemas/schema_query.output.json +70 -3
  168. package/dist/schemas/schema_query.schema.json +18 -3
  169. package/dist/schemas/wowok_buildin_info.output.json +81 -8
  170. package/dist/schemas/wowok_buildin_info.schema.json +46 -2
  171. package/dist/tools/handlers/local.js +20 -5
  172. package/dist/tools/handlers/onchain.js +594 -6
  173. package/dist/tools/handlers/project.js +76 -4
  174. package/dist/tools/handlers/query.js +27 -0
  175. package/dist/tools/handlers/schema-query.js +43 -1
  176. package/dist/tools/handlers/task-status.d.ts +170 -0
  177. package/dist/tools/handlers/task-status.js +55 -0
  178. package/dist/tools/handlers/wip.js +47 -1
  179. package/dist/tools/index.d.ts +8 -0
  180. package/dist/tools/index.js +387 -12
  181. package/dist/tools/retry.d.ts +8 -0
  182. package/dist/tools/retry.js +85 -0
  183. package/dist/tools/wip-deploy-assist.d.ts +28 -0
  184. package/dist/tools/wip-deploy-assist.js +278 -0
  185. package/package.json +2 -2
  186. package/dist/schemas/guard-node-examples.md +0 -199
@@ -0,0 +1,700 @@
1
+ function categorizeNodeType(type) {
2
+ if (type.startsWith("logic_"))
3
+ return "decision";
4
+ if (type === "context")
5
+ return "context";
6
+ if (type === "identifier")
7
+ return "leaf";
8
+ if (type === "query")
9
+ return "query";
10
+ if (type.startsWith("calc_") || type.startsWith("convert_"))
11
+ return "computation";
12
+ if (type.startsWith("vec_"))
13
+ return "computation";
14
+ if (type.startsWith("query_reward_") || type.startsWith("query_progress_"))
15
+ return "query";
16
+ return "computation";
17
+ }
18
+ function shortNodeLabel(node) {
19
+ const t = node.type;
20
+ switch (t) {
21
+ case "logic_and": return "AND";
22
+ case "logic_or": return "OR";
23
+ case "logic_not": return "NOT";
24
+ case "logic_equal": return "==";
25
+ case "logic_as_u256_equal": return "==#";
26
+ case "logic_as_u256_greater": return ">";
27
+ case "logic_as_u256_greater_or_equal": return ">=";
28
+ case "logic_as_u256_lesser": return "<";
29
+ case "logic_as_u256_lesser_or_equal": return "<=";
30
+ case "logic_string_contains": return "contains";
31
+ case "logic_string_nocase_contains": return "contains~";
32
+ case "logic_string_nocase_equal": return "==~";
33
+ case "calc_number_add": return "+";
34
+ case "calc_number_subtract": return "-";
35
+ case "calc_number_multiply": return "*";
36
+ case "calc_number_divide": return "/";
37
+ case "calc_number_mod": return "%";
38
+ case "calc_string_length": return "len";
39
+ case "calc_string_contains": return "contains";
40
+ case "calc_string_nocase_contains": return "contains~";
41
+ case "calc_string_nocase_equal": return "==~";
42
+ case "calc_string_indexof": return "indexOf";
43
+ case "calc_string_nocase_indexof": return "indexOf~";
44
+ case "convert_number_address": return "→Addr";
45
+ case "convert_address_number": return "→Num";
46
+ case "convert_number_string": return "→Str";
47
+ case "convert_string_number": return "→Num";
48
+ case "convert_safe_u8": return "→U8";
49
+ case "convert_safe_u16": return "→U16";
50
+ case "convert_safe_u32": return "→U32";
51
+ case "convert_safe_u64": return "→U64";
52
+ case "convert_safe_u128": return "→U128";
53
+ case "convert_safe_u256": return "→U256";
54
+ case "value_type": return "typeOf";
55
+ case "vec_length": return "len";
56
+ case "vec_contains_bool": return "∈?";
57
+ case "vec_contains_address": return "∈?";
58
+ case "vec_contains_string": return "∈?";
59
+ case "vec_contains_string_nocase": return "∈?~";
60
+ case "vec_contains_number": return "∈?";
61
+ case "vec_indexof_bool": return "idx";
62
+ case "vec_indexof_address": return "idx";
63
+ case "vec_indexof_string": return "idx";
64
+ case "vec_indexof_string_nocase": return "idx~";
65
+ case "vec_indexof_number": return "idx";
66
+ case "query":
67
+ return `Q:${typeof node.query === "number" ? `#${node.query}` : node.query}`;
68
+ case "query_reward_record_count": return "reward.count";
69
+ case "query_reward_record_exists": return "reward.exists";
70
+ case "query_reward_record_find": return "reward.find";
71
+ case "query_progress_history_find": return "history.find";
72
+ case "query_progress_history_session_find": return "session.find";
73
+ case "query_progress_history_session_forward_find": return "forward.find";
74
+ case "query_progress_history_session_count": return "session.count";
75
+ case "query_progress_history_session_forward_count": return "forward.count";
76
+ case "query_progress_history_session_forward_retained_submission_count":
77
+ return "retained.count";
78
+ case "identifier":
79
+ return `id[${node.identifier ?? "?"}]`;
80
+ case "context":
81
+ return node.context ?? "ctx";
82
+ default:
83
+ return t;
84
+ }
85
+ }
86
+ let nodeIdCounter = 0;
87
+ function makeNodeId(prefix) {
88
+ nodeIdCounter++;
89
+ return `${prefix}_${nodeIdCounter}`;
90
+ }
91
+ function sanitizeMermaidLabel(text) {
92
+ if (/[<>"\[\]{}()|&]/.test(text)) {
93
+ return `"${text.replace(/"/g, "'")}"`;
94
+ }
95
+ return text;
96
+ }
97
+ const GUARD_NODE_COLORS = {
98
+ control: { fill: "#3b82f6", stroke: "#1e40af" },
99
+ decision: { fill: "#f59e0b", stroke: "#92400e" },
100
+ computation: { fill: "#8b5cf6", stroke: "#6d28d9" },
101
+ query: { fill: "#06b6d4", stroke: "#0e7490" },
102
+ leaf: { fill: "#6b7280", stroke: "#374151" },
103
+ context: { fill: "#10b981", stroke: "#065f46" },
104
+ };
105
+ function colorCategoryForNodeType(type) {
106
+ if (type === "logic_and" || type === "logic_or")
107
+ return "control";
108
+ if (type === "logic_not" || type.startsWith("logic_"))
109
+ return "decision";
110
+ if (type.startsWith("calc_") || type.startsWith("convert_") || type.startsWith("vec_"))
111
+ return "computation";
112
+ if (type === "query" || type.startsWith("query_"))
113
+ return "query";
114
+ if (type === "identifier")
115
+ return "leaf";
116
+ if (type === "context")
117
+ return "context";
118
+ return "computation";
119
+ }
120
+ export function renderGuardMermaid(root, options) {
121
+ const colored = options?.colored !== false;
122
+ nodeIdCounter = 0;
123
+ const lines = ["flowchart TD"];
124
+ if (colored) {
125
+ lines.push(" %% Color Legend (BehaviorTree.CPP-inspired semantic roles):");
126
+ lines.push(" %% blue #3b82f6 - control (logic_and, logic_or)");
127
+ lines.push(" %% amber #f59e0b - decision (logic_not, comparisons)");
128
+ lines.push(" %% purple #8b5cf6 - computation (calc_*, convert_*, vec_*)");
129
+ lines.push(" %% cyan #06b6d4 - query (query, query_*)");
130
+ lines.push(" %% gray #6b7280 - leaf (identifier)");
131
+ lines.push(" %% green #10b981 - context (Signer, Clock, Guard)");
132
+ lines.push(" %% Node shapes: {}=decision, []=computation/query, ([])=leaf, {{}}=context");
133
+ }
134
+ const nodeDecls = [];
135
+ const edgeLines = [];
136
+ const nodeColorMap = [];
137
+ function walk(node, parentId, edgeLabel) {
138
+ const id = makeNodeId("n");
139
+ const category = categorizeNodeType(node.type);
140
+ const label = shortNodeLabel(node);
141
+ let shapeOpen = "[", shapeClose = "]";
142
+ switch (category) {
143
+ case "decision":
144
+ shapeOpen = "{";
145
+ shapeClose = "}";
146
+ break;
147
+ case "context":
148
+ shapeOpen = "{{";
149
+ shapeClose = "}}";
150
+ break;
151
+ case "leaf":
152
+ shapeOpen = "([";
153
+ shapeClose = "])";
154
+ break;
155
+ case "query":
156
+ case "computation":
157
+ default:
158
+ shapeOpen = "[";
159
+ shapeClose = "]";
160
+ break;
161
+ }
162
+ nodeDecls.push(`${id}${shapeOpen}${sanitizeMermaidLabel(label)}${shapeClose}`);
163
+ nodeColorMap.push({ id, type: node.type });
164
+ if (parentId) {
165
+ if (edgeLabel) {
166
+ edgeLines.push(` ${parentId} --> ${id}: ${sanitizeMermaidLabel(edgeLabel)}`);
167
+ }
168
+ else {
169
+ edgeLines.push(` ${parentId} --> ${id}`);
170
+ }
171
+ }
172
+ if (node.nodes && Array.isArray(node.nodes)) {
173
+ node.nodes.forEach((child, i) => {
174
+ walk(child, id, `[${i}]`);
175
+ });
176
+ }
177
+ if (node.node) {
178
+ walk(node.node, id, "child");
179
+ }
180
+ if (node.nodeLeft) {
181
+ walk(node.nodeLeft, id, "left");
182
+ }
183
+ if (node.nodeRight) {
184
+ walk(node.nodeRight, id, "right");
185
+ }
186
+ if (node.parameters && Array.isArray(node.parameters)) {
187
+ node.parameters.forEach((child, i) => {
188
+ walk(child, id, `param[${i}]`);
189
+ });
190
+ }
191
+ if (node.historyIdx)
192
+ walk(node.historyIdx, id, "historyIdx");
193
+ if (node.sessionIdx)
194
+ walk(node.sessionIdx, id, "sessionIdx");
195
+ if (node.forwardIdx)
196
+ walk(node.forwardIdx, id, "forwardIdx");
197
+ if (node.where)
198
+ walk(node.where, id, "where");
199
+ if (node.find)
200
+ walk(node.find, id, "find");
201
+ if (node.objectLeft)
202
+ walk(node.objectLeft, id, "objLeft");
203
+ if (node.objectRight)
204
+ walk(node.objectRight, id, "objRight");
205
+ if (node.recipient)
206
+ walk(node.recipient, id, "recipient");
207
+ return id;
208
+ }
209
+ walk(root);
210
+ lines.push(...nodeDecls.map(d => ` ${d}`));
211
+ lines.push(...edgeLines);
212
+ if (colored) {
213
+ lines.push("");
214
+ lines.push(" %% Node colors (BehaviorTree.CPP-inspired semantic roles)");
215
+ for (const { id, type } of nodeColorMap) {
216
+ const category = colorCategoryForNodeType(type);
217
+ const color = GUARD_NODE_COLORS[category];
218
+ lines.push(` style ${id} fill:${color.fill},stroke:${color.stroke},color:#fff`);
219
+ }
220
+ }
221
+ return lines.join("\n");
222
+ }
223
+ export function renderGuardTreeIndented(input) {
224
+ const { root, table, guardName, guardAddress, rely } = input;
225
+ const lines = [];
226
+ function describeLeaf(node) {
227
+ const t = node.type;
228
+ if (t === "identifier") {
229
+ const tableEntry = table?.find(e => e.identifier === node.identifier);
230
+ const nameHint = tableEntry?.name ? ` "${tableEntry.name}"` : "";
231
+ return `identifier[${node.identifier ?? "?"}]${nameHint}`;
232
+ }
233
+ if (t === "context") {
234
+ return `context(${node.context ?? "?"})`;
235
+ }
236
+ if (t === "query") {
237
+ const witness = node.object?.convert_witness;
238
+ const objId = node.object?.identifier;
239
+ const witnessStr = witness !== undefined ? `, witness=${witness}` : "";
240
+ const objStr = objId !== undefined ? `, id[${objId}]` : "";
241
+ const paramStr = node.parameters && node.parameters.length > 0
242
+ ? `, params=[${node.parameters.map(p => shortNodeLabel(p)).join(", ")}]`
243
+ : "";
244
+ return `query(${typeof node.query === "number" ? `#${node.query}` : node.query}${objStr}${witnessStr}${paramStr})`;
245
+ }
246
+ return shortNodeLabel(node);
247
+ }
248
+ function walk(node, depth, label) {
249
+ const indent = " ".repeat(depth);
250
+ const prefix = depth === 0 ? "- " : "- ";
251
+ const labelStr = label ? `${label}: ` : "";
252
+ const isLeaf = !node.nodes && !node.node && !node.nodeLeft && !node.nodeRight
253
+ && !node.parameters?.length && !node.historyIdx && !node.sessionIdx
254
+ && !node.forwardIdx && !node.where && !node.find && !node.objectLeft
255
+ && !node.objectRight && !node.recipient;
256
+ if (isLeaf) {
257
+ lines.push(`${indent}${prefix}${labelStr}${describeLeaf(node)}`);
258
+ }
259
+ else {
260
+ lines.push(`${indent}${prefix}${labelStr}${node.type}`);
261
+ }
262
+ if (node.nodes && Array.isArray(node.nodes)) {
263
+ node.nodes.forEach((child, i) => walk(child, depth + 1, `child[${i}]`));
264
+ }
265
+ if (node.node) {
266
+ walk(node.node, depth + 1, "child");
267
+ }
268
+ if (node.nodeLeft) {
269
+ walk(node.nodeLeft, depth + 1, "left");
270
+ }
271
+ if (node.nodeRight) {
272
+ walk(node.nodeRight, depth + 1, "right");
273
+ }
274
+ if (node.parameters && Array.isArray(node.parameters)) {
275
+ node.parameters.forEach((child, i) => walk(child, depth + 1, `param[${i}]`));
276
+ }
277
+ if (node.historyIdx)
278
+ walk(node.historyIdx, depth + 1, "historyIdx");
279
+ if (node.sessionIdx)
280
+ walk(node.sessionIdx, depth + 1, "sessionIdx");
281
+ if (node.forwardIdx)
282
+ walk(node.forwardIdx, depth + 1, "forwardIdx");
283
+ if (node.where)
284
+ walk(node.where, depth + 1, "where");
285
+ if (node.find)
286
+ walk(node.find, depth + 1, "find");
287
+ if (node.objectLeft)
288
+ walk(node.objectLeft, depth + 1, "objLeft");
289
+ if (node.objectRight)
290
+ walk(node.objectRight, depth + 1, "objRight");
291
+ if (node.recipient)
292
+ walk(node.recipient, depth + 1, "recipient");
293
+ }
294
+ lines.push("# Guard Tree Definition");
295
+ lines.push("");
296
+ if (guardName) {
297
+ lines.push(`**Guard:** ${guardName}`);
298
+ }
299
+ if (guardAddress) {
300
+ lines.push(`**Address:** ${guardAddress}`);
301
+ }
302
+ if (table) {
303
+ lines.push(`**Table Entries:** ${table.length}`);
304
+ }
305
+ if (rely && rely.guards.length > 0) {
306
+ lines.push(`**Rely:** ${rely.guards.length} guard(s)${rely.logic_or ? " (logic_or)" : " (logic_and)"}`);
307
+ }
308
+ lines.push("");
309
+ lines.push("## Root");
310
+ walk(root, 0);
311
+ if (table && table.length > 0) {
312
+ lines.push("");
313
+ lines.push("## Table");
314
+ lines.push("");
315
+ lines.push("| identifier | b_submission | value_type | value | name |");
316
+ lines.push("|------------|-------------|------------|-------|------|");
317
+ for (const entry of table) {
318
+ const rawVal = entry.value === undefined ? "-" : String(entry.value);
319
+ const val = rawVal.replace(/\|/g, "\\|").replace(/\n/g, " ");
320
+ const name = entry.name ?? "-";
321
+ lines.push(`| ${entry.identifier} | ${entry.b_submission} | ${entry.value_type} | ${val} | ${name} |`);
322
+ }
323
+ lines.push("");
324
+ lines.push("## Table JSON");
325
+ lines.push("");
326
+ lines.push("```json");
327
+ lines.push(JSON.stringify(table, null, 2));
328
+ lines.push("```");
329
+ }
330
+ if (rely && rely.guards.length > 0) {
331
+ lines.push("");
332
+ lines.push("## Rely");
333
+ lines.push("");
334
+ lines.push(`Logic: ${rely.logic_or ? "OR" : "AND"}`);
335
+ for (const g of rely.guards) {
336
+ lines.push(`- ${g}`);
337
+ }
338
+ }
339
+ lines.push("");
340
+ lines.push("## JSON Definition");
341
+ lines.push("");
342
+ lines.push("```json");
343
+ lines.push(JSON.stringify({ root, table, guardName, guardAddress, rely }, null, 2));
344
+ lines.push("```");
345
+ return lines.join("\n");
346
+ }
347
+ export function parseGuardTreeIndented(text) {
348
+ const errors = [];
349
+ const jsonBlocks = text.matchAll(/```json\s*\n([\s\S]*?)```/g);
350
+ let lastJsonBlock = null;
351
+ for (const match of jsonBlocks) {
352
+ lastJsonBlock = match[1];
353
+ }
354
+ if (lastJsonBlock) {
355
+ try {
356
+ const data = JSON.parse(lastJsonBlock.trim());
357
+ if (data && typeof data === "object") {
358
+ if ("root" in data && data.root && typeof data.root === "object" && "type" in data.root) {
359
+ return { success: true, data: data.root, errors: [] };
360
+ }
361
+ if ("type" in data) {
362
+ return { success: true, data, errors: [] };
363
+ }
364
+ }
365
+ }
366
+ catch (e) {
367
+ errors.push(`JSON code block parse error: ${e.message}`);
368
+ }
369
+ }
370
+ const lines = text.split("\n").filter(l => l.trim().startsWith("- "));
371
+ if (lines.length === 0) {
372
+ return { success: false, errors: ["No outline entries found. Use '- ' prefixed lines or a JSON code block."] };
373
+ }
374
+ const stack = [];
375
+ let root = null;
376
+ function getDepth(line) {
377
+ const match = line.match(/^(\s*)- /);
378
+ return match ? match[1].length / 2 : 0;
379
+ }
380
+ function parseLine(line) {
381
+ const content = line.replace(/^\s*- /, "").trim();
382
+ const labelMatch = content.match(/^([^:]+):\s*(.*)$/);
383
+ const label = labelMatch ? labelMatch[1].trim() : undefined;
384
+ const rest = labelMatch ? labelMatch[2].trim() : content;
385
+ const idMatch = rest.match(/^identifier\[(\d+)\](?:\s+"(.+)")?$/);
386
+ if (idMatch) {
387
+ return {
388
+ label,
389
+ node: { type: "identifier", identifier: parseInt(idMatch[1], 10) },
390
+ };
391
+ }
392
+ const ctxMatch = rest.match(/^context\((\w+)\)$/);
393
+ if (ctxMatch) {
394
+ return { label, node: { type: "context", context: ctxMatch[1] } };
395
+ }
396
+ const queryMatch = rest.match(/^query\((.+?)\)$/);
397
+ if (queryMatch) {
398
+ const inner = queryMatch[1];
399
+ const parts = inner.split(",").map(s => s.trim());
400
+ const queryName = parts[0].replace(/^#/, "");
401
+ const isNumeric = /^\d+$/.test(queryName);
402
+ const node = {
403
+ type: "query",
404
+ query: isNumeric ? parseInt(queryName, 10) : queryName,
405
+ };
406
+ for (const part of parts.slice(1)) {
407
+ const idPart = part.match(/^id\[(\d+)\]$/);
408
+ if (idPart) {
409
+ node.object = { identifier: parseInt(idPart[1], 10) };
410
+ }
411
+ const witnessPart = part.match(/^witness=(\d+)$/);
412
+ if (witnessPart) {
413
+ node.object = { ...(node.object || {}), convert_witness: parseInt(witnessPart[1], 10) };
414
+ }
415
+ }
416
+ return { label, node };
417
+ }
418
+ return { label, node: { type: rest } };
419
+ }
420
+ for (const line of lines) {
421
+ const depth = getDepth(line);
422
+ const { label, node } = parseLine(line);
423
+ if (depth === 0) {
424
+ root = node;
425
+ stack.length = 0;
426
+ stack.push({ node, depth });
427
+ }
428
+ else {
429
+ while (stack.length > 0 && stack[stack.length - 1].depth >= depth) {
430
+ stack.pop();
431
+ }
432
+ const parent = stack[stack.length - 1];
433
+ if (!parent) {
434
+ errors.push(`Orphaned node at depth ${depth}: ${line}`);
435
+ continue;
436
+ }
437
+ if (label) {
438
+ const childIdxMatch = label.match(/^child\[(\d+)\]$/);
439
+ const paramIdxMatch = label.match(/^param\[(\d+)\]$/);
440
+ if (childIdxMatch) {
441
+ if (!parent.node.nodes)
442
+ parent.node.nodes = [];
443
+ parent.node.nodes.push(node);
444
+ }
445
+ else if (paramIdxMatch) {
446
+ if (!parent.node.parameters)
447
+ parent.node.parameters = [];
448
+ parent.node.parameters.push(node);
449
+ }
450
+ else if (label === "child") {
451
+ parent.node.node = node;
452
+ }
453
+ else if (label === "left") {
454
+ parent.node.nodeLeft = node;
455
+ }
456
+ else if (label === "right") {
457
+ parent.node.nodeRight = node;
458
+ }
459
+ else if (label === "historyIdx") {
460
+ parent.node.historyIdx = node;
461
+ }
462
+ else if (label === "sessionIdx") {
463
+ parent.node.sessionIdx = node;
464
+ }
465
+ else if (label === "forwardIdx") {
466
+ parent.node.forwardIdx = node;
467
+ }
468
+ else if (label === "where") {
469
+ parent.node.where = node;
470
+ }
471
+ else if (label === "find") {
472
+ parent.node.find = node;
473
+ }
474
+ else if (label === "objLeft") {
475
+ parent.node.objectLeft = node;
476
+ }
477
+ else if (label === "objRight") {
478
+ parent.node.objectRight = node;
479
+ }
480
+ else if (label === "recipient") {
481
+ parent.node.recipient = node;
482
+ }
483
+ }
484
+ stack.push({ node, depth });
485
+ }
486
+ }
487
+ if (!root) {
488
+ return { success: false, errors: ["No root node found in outline."] };
489
+ }
490
+ return { success: true, data: root, errors };
491
+ }
492
+ export function renderGuardAscii(input) {
493
+ const { root, table, guardName, guardAddress, rely } = input;
494
+ const lines = [];
495
+ if (guardName)
496
+ lines.push(`Guard: ${guardName}`);
497
+ if (guardAddress)
498
+ lines.push(`Address: ${guardAddress}`);
499
+ if (guardName || guardAddress)
500
+ lines.push("");
501
+ function describeNode(node) {
502
+ const t = node.type;
503
+ const baseLabel = shortNodeLabel(node);
504
+ if (t === "identifier") {
505
+ const tableEntry = table?.find(e => e.identifier === node.identifier);
506
+ const typeStr = tableEntry ? ` [${typeof tableEntry.value_type === "string" ? tableEntry.value_type : `type=${tableEntry.value_type}`}]` : "";
507
+ const valueStr = (tableEntry && tableEntry.value !== undefined && !tableEntry.b_submission)
508
+ ? `, value=${String(tableEntry.value)}` : "";
509
+ const submissionStr = tableEntry?.b_submission ? " (submission)" : "";
510
+ return `${baseLabel}${typeStr}${valueStr}${submissionStr}`;
511
+ }
512
+ if (t === "context") {
513
+ return `${baseLabel} [${node.context ?? "?"}]`;
514
+ }
515
+ if (t === "query") {
516
+ const witness = node.object?.convert_witness;
517
+ const witnessStr = witness !== undefined ? `, witness=${witness}` : "";
518
+ return `${baseLabel}${witnessStr}`;
519
+ }
520
+ return baseLabel;
521
+ }
522
+ function collectChildren(node) {
523
+ const children = [];
524
+ if (node.nodes && Array.isArray(node.nodes)) {
525
+ node.nodes.forEach((child, i) => children.push({ node: child, label: `child[${i}]` }));
526
+ }
527
+ if (node.node)
528
+ children.push({ node: node.node, label: "child" });
529
+ if (node.nodeLeft)
530
+ children.push({ node: node.nodeLeft, label: "left" });
531
+ if (node.nodeRight)
532
+ children.push({ node: node.nodeRight, label: "right" });
533
+ if (node.parameters && Array.isArray(node.parameters)) {
534
+ node.parameters.forEach((child, i) => children.push({ node: child, label: `param[${i}]` }));
535
+ }
536
+ if (node.historyIdx)
537
+ children.push({ node: node.historyIdx, label: "historyIdx" });
538
+ if (node.sessionIdx)
539
+ children.push({ node: node.sessionIdx, label: "sessionIdx" });
540
+ if (node.forwardIdx)
541
+ children.push({ node: node.forwardIdx, label: "forwardIdx" });
542
+ if (node.where)
543
+ children.push({ node: node.where, label: "where" });
544
+ if (node.find)
545
+ children.push({ node: node.find, label: "find" });
546
+ if (node.objectLeft)
547
+ children.push({ node: node.objectLeft, label: "objLeft" });
548
+ if (node.objectRight)
549
+ children.push({ node: node.objectRight, label: "objRight" });
550
+ if (node.recipient)
551
+ children.push({ node: node.recipient, label: "recipient" });
552
+ return children;
553
+ }
554
+ function walk(node, prefix, isLast, edgeLabel) {
555
+ const connector = isLast ? "└─ " : "├─ ";
556
+ const labelStr = edgeLabel ? `${edgeLabel}: ` : "";
557
+ lines.push(`${prefix}${connector}${labelStr}${describeNode(node)}`);
558
+ const children = collectChildren(node);
559
+ const childPrefix = prefix + (isLast ? " " : "│ ");
560
+ children.forEach((child, i) => {
561
+ walk(child.node, childPrefix, i === children.length - 1, child.label);
562
+ });
563
+ }
564
+ lines.push(describeNode(root));
565
+ const children = collectChildren(root);
566
+ children.forEach((child, i) => {
567
+ walk(child.node, "", i === children.length - 1, child.label);
568
+ });
569
+ if (table && table.length > 0) {
570
+ lines.push("");
571
+ lines.push("Table:");
572
+ for (const entry of table) {
573
+ const typeStr = typeof entry.value_type === "string" ? entry.value_type : `type=${entry.value_type}`;
574
+ const valueStr = (entry.value !== undefined && !entry.b_submission) ? `, value=${String(entry.value)}` : "";
575
+ const submissionStr = entry.b_submission ? " (submission)" : "";
576
+ const nameStr = entry.name ? ` "${entry.name}"` : "";
577
+ lines.push(` id[${entry.identifier}]${nameStr}: ${typeStr}${valueStr}${submissionStr}`);
578
+ }
579
+ }
580
+ if (rely && rely.guards.length > 0) {
581
+ lines.push("");
582
+ lines.push(`Rely (${rely.logic_or ? "OR" : "AND"}):`);
583
+ for (const g of rely.guards) {
584
+ lines.push(` - ${g}`);
585
+ }
586
+ }
587
+ return lines.join("\n");
588
+ }
589
+ export function renderGuardMarkmap(input) {
590
+ const { root, table, guardName, guardAddress, rely } = input;
591
+ const lines = [];
592
+ const title = guardName ? `Guard: ${guardName}` : "Guard";
593
+ lines.push(`# ${title}`);
594
+ if (guardAddress)
595
+ lines.push("");
596
+ if (guardAddress)
597
+ lines.push(`**Address:** ${guardAddress}`);
598
+ lines.push("");
599
+ function heading(depth, text) {
600
+ return `${"#".repeat(Math.min(depth + 1, 6))} ${text}`;
601
+ }
602
+ function describeNode(node) {
603
+ const t = node.type;
604
+ const baseLabel = shortNodeLabel(node);
605
+ if (t === "identifier") {
606
+ const tableEntry = table?.find(e => e.identifier === node.identifier);
607
+ const typeStr = tableEntry ? ` [${typeof tableEntry.value_type === "string" ? tableEntry.value_type : `type=${tableEntry.value_type}`}]` : "";
608
+ const valueStr = (tableEntry && tableEntry.value !== undefined && !tableEntry.b_submission)
609
+ ? `, value=${String(tableEntry.value)}` : "";
610
+ const submissionStr = tableEntry?.b_submission ? " (submission)" : "";
611
+ return `${baseLabel}${typeStr}${valueStr}${submissionStr}`;
612
+ }
613
+ if (t === "context") {
614
+ return `${baseLabel} [${node.context ?? "?"}]`;
615
+ }
616
+ if (t === "query") {
617
+ const witness = node.object?.convert_witness;
618
+ const witnessStr = witness !== undefined ? `, witness=${witness}` : "";
619
+ return `${baseLabel}${witnessStr}`;
620
+ }
621
+ return baseLabel;
622
+ }
623
+ function walk(node, depth, edgeLabel) {
624
+ const labelStr = edgeLabel ? `${edgeLabel}: ` : "";
625
+ lines.push(heading(depth, `${labelStr}${describeNode(node)}`));
626
+ if (node.nodes && Array.isArray(node.nodes)) {
627
+ node.nodes.forEach((child, i) => walk(child, depth + 1, `child[${i}]`));
628
+ }
629
+ if (node.node)
630
+ walk(node.node, depth + 1, "child");
631
+ if (node.nodeLeft)
632
+ walk(node.nodeLeft, depth + 1, "left");
633
+ if (node.nodeRight)
634
+ walk(node.nodeRight, depth + 1, "right");
635
+ if (node.parameters && Array.isArray(node.parameters)) {
636
+ node.parameters.forEach((child, i) => walk(child, depth + 1, `param[${i}]`));
637
+ }
638
+ if (node.historyIdx)
639
+ walk(node.historyIdx, depth + 1, "historyIdx");
640
+ if (node.sessionIdx)
641
+ walk(node.sessionIdx, depth + 1, "sessionIdx");
642
+ if (node.forwardIdx)
643
+ walk(node.forwardIdx, depth + 1, "forwardIdx");
644
+ if (node.where)
645
+ walk(node.where, depth + 1, "where");
646
+ if (node.find)
647
+ walk(node.find, depth + 1, "find");
648
+ if (node.objectLeft)
649
+ walk(node.objectLeft, depth + 1, "objLeft");
650
+ if (node.objectRight)
651
+ walk(node.objectRight, depth + 1, "objRight");
652
+ if (node.recipient)
653
+ walk(node.recipient, depth + 1, "recipient");
654
+ }
655
+ lines.push(heading(1, "Root"));
656
+ walk(root, 2);
657
+ if (table && table.length > 0) {
658
+ lines.push("");
659
+ lines.push("## Table");
660
+ for (const entry of table) {
661
+ const typeStr = typeof entry.value_type === "string" ? entry.value_type : `type=${entry.value_type}`;
662
+ const valueStr = (entry.value !== undefined && !entry.b_submission) ? `, value=${String(entry.value)}` : "";
663
+ const submissionStr = entry.b_submission ? " (submission)" : "";
664
+ const nameStr = entry.name ? ` "${entry.name}"` : "";
665
+ lines.push(`- id[${entry.identifier}]${nameStr}: ${typeStr}${valueStr}${submissionStr}`);
666
+ }
667
+ }
668
+ if (rely && rely.guards.length > 0) {
669
+ lines.push("");
670
+ lines.push(`## Rely (${rely.logic_or ? "OR" : "AND"})`);
671
+ for (const g of rely.guards) {
672
+ lines.push(`- ${g}`);
673
+ }
674
+ }
675
+ return lines.join("\n");
676
+ }
677
+ export function parseGuardTableJSON(text) {
678
+ const sectionMatch = text.match(/##\s*Table JSON[^\n]*\n\s*```json\s*\n([\s\S]*?)```/i);
679
+ if (!sectionMatch)
680
+ return null;
681
+ try {
682
+ const data = JSON.parse(sectionMatch[1].trim());
683
+ if (!Array.isArray(data))
684
+ return null;
685
+ return data.filter((entry) => {
686
+ return entry && typeof entry === "object" &&
687
+ typeof entry.identifier === "number" &&
688
+ typeof entry.b_submission === "boolean";
689
+ }).map((entry) => ({
690
+ identifier: entry.identifier,
691
+ name: entry.name,
692
+ value_type: entry.value_type,
693
+ b_submission: entry.b_submission,
694
+ value: entry.value,
695
+ }));
696
+ }
697
+ catch {
698
+ return null;
699
+ }
700
+ }