@soleri/core 2.11.0 → 7.0.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 (255) hide show
  1. package/data/flows/build.flow.yaml +128 -0
  2. package/data/flows/deliver.flow.yaml +110 -0
  3. package/data/flows/design.flow.yaml +108 -0
  4. package/data/flows/enhance.flow.yaml +90 -0
  5. package/data/flows/explore.flow.yaml +84 -0
  6. package/data/flows/fix.flow.yaml +90 -0
  7. package/data/flows/plan.flow.yaml +87 -0
  8. package/data/flows/review.flow.yaml +90 -0
  9. package/dist/brain/brain.d.ts.map +1 -1
  10. package/dist/brain/brain.js +10 -0
  11. package/dist/brain/brain.js.map +1 -1
  12. package/dist/brain/intelligence.d.ts.map +1 -1
  13. package/dist/brain/intelligence.js +16 -2
  14. package/dist/brain/intelligence.js.map +1 -1
  15. package/dist/capabilities/chain-mapping.d.ts +21 -0
  16. package/dist/capabilities/chain-mapping.d.ts.map +1 -0
  17. package/dist/capabilities/chain-mapping.js +86 -0
  18. package/dist/capabilities/chain-mapping.js.map +1 -0
  19. package/dist/capabilities/index.d.ts +10 -0
  20. package/dist/capabilities/index.d.ts.map +1 -0
  21. package/dist/capabilities/index.js +8 -0
  22. package/dist/capabilities/index.js.map +1 -0
  23. package/dist/capabilities/registry.d.ts +95 -0
  24. package/dist/capabilities/registry.d.ts.map +1 -0
  25. package/dist/capabilities/registry.js +227 -0
  26. package/dist/capabilities/registry.js.map +1 -0
  27. package/dist/capabilities/types.d.ts +106 -0
  28. package/dist/capabilities/types.d.ts.map +1 -0
  29. package/dist/capabilities/types.js +12 -0
  30. package/dist/capabilities/types.js.map +1 -0
  31. package/dist/control/intent-router.d.ts.map +1 -1
  32. package/dist/control/intent-router.js +58 -2
  33. package/dist/control/intent-router.js.map +1 -1
  34. package/dist/domain-packs/index.d.ts +8 -0
  35. package/dist/domain-packs/index.d.ts.map +1 -0
  36. package/dist/domain-packs/index.js +8 -0
  37. package/dist/domain-packs/index.js.map +1 -0
  38. package/dist/domain-packs/inject-rules.d.ts +24 -0
  39. package/dist/domain-packs/inject-rules.d.ts.map +1 -0
  40. package/dist/domain-packs/inject-rules.js +65 -0
  41. package/dist/domain-packs/inject-rules.js.map +1 -0
  42. package/dist/domain-packs/knowledge-installer.d.ts +27 -0
  43. package/dist/domain-packs/knowledge-installer.d.ts.map +1 -0
  44. package/dist/domain-packs/knowledge-installer.js +89 -0
  45. package/dist/domain-packs/knowledge-installer.js.map +1 -0
  46. package/dist/domain-packs/loader.d.ts +28 -0
  47. package/dist/domain-packs/loader.d.ts.map +1 -0
  48. package/dist/domain-packs/loader.js +105 -0
  49. package/dist/domain-packs/loader.js.map +1 -0
  50. package/dist/domain-packs/pack-runtime.d.ts +80 -0
  51. package/dist/domain-packs/pack-runtime.d.ts.map +1 -0
  52. package/dist/domain-packs/pack-runtime.js +36 -0
  53. package/dist/domain-packs/pack-runtime.js.map +1 -0
  54. package/dist/domain-packs/skills-installer.d.ts +21 -0
  55. package/dist/domain-packs/skills-installer.d.ts.map +1 -0
  56. package/dist/domain-packs/skills-installer.js +38 -0
  57. package/dist/domain-packs/skills-installer.js.map +1 -0
  58. package/dist/domain-packs/token-resolver.d.ts +37 -0
  59. package/dist/domain-packs/token-resolver.d.ts.map +1 -0
  60. package/dist/domain-packs/token-resolver.js +109 -0
  61. package/dist/domain-packs/token-resolver.js.map +1 -0
  62. package/dist/domain-packs/types.d.ts +91 -0
  63. package/dist/domain-packs/types.d.ts.map +1 -0
  64. package/dist/domain-packs/types.js +122 -0
  65. package/dist/domain-packs/types.js.map +1 -0
  66. package/dist/engine/bin/soleri-engine.d.ts +12 -0
  67. package/dist/engine/bin/soleri-engine.d.ts.map +1 -0
  68. package/dist/engine/bin/soleri-engine.js +183 -0
  69. package/dist/engine/bin/soleri-engine.js.map +1 -0
  70. package/dist/engine/core-ops.d.ts +27 -0
  71. package/dist/engine/core-ops.d.ts.map +1 -0
  72. package/dist/engine/core-ops.js +159 -0
  73. package/dist/engine/core-ops.js.map +1 -0
  74. package/dist/engine/index.d.ts +19 -0
  75. package/dist/engine/index.d.ts.map +1 -0
  76. package/dist/engine/index.js +17 -0
  77. package/dist/engine/index.js.map +1 -0
  78. package/dist/engine/register-engine.d.ts +54 -0
  79. package/dist/engine/register-engine.d.ts.map +1 -0
  80. package/dist/engine/register-engine.js +270 -0
  81. package/dist/engine/register-engine.js.map +1 -0
  82. package/dist/engine/test-helpers.d.ts +30 -0
  83. package/dist/engine/test-helpers.d.ts.map +1 -0
  84. package/dist/engine/test-helpers.js +59 -0
  85. package/dist/engine/test-helpers.js.map +1 -0
  86. package/dist/flows/context-router.d.ts +39 -0
  87. package/dist/flows/context-router.d.ts.map +1 -0
  88. package/dist/flows/context-router.js +206 -0
  89. package/dist/flows/context-router.js.map +1 -0
  90. package/dist/flows/dispatch-registry.d.ts +24 -0
  91. package/dist/flows/dispatch-registry.d.ts.map +1 -0
  92. package/dist/flows/dispatch-registry.js +70 -0
  93. package/dist/flows/dispatch-registry.js.map +1 -0
  94. package/dist/flows/epilogue.d.ts +24 -0
  95. package/dist/flows/epilogue.d.ts.map +1 -0
  96. package/dist/flows/epilogue.js +52 -0
  97. package/dist/flows/epilogue.js.map +1 -0
  98. package/dist/flows/executor.d.ts +25 -0
  99. package/dist/flows/executor.d.ts.map +1 -0
  100. package/dist/flows/executor.js +153 -0
  101. package/dist/flows/executor.js.map +1 -0
  102. package/dist/flows/gate-evaluator.d.ts +26 -0
  103. package/dist/flows/gate-evaluator.d.ts.map +1 -0
  104. package/dist/flows/gate-evaluator.js +162 -0
  105. package/dist/flows/gate-evaluator.js.map +1 -0
  106. package/dist/flows/index.d.ts +14 -0
  107. package/dist/flows/index.d.ts.map +1 -0
  108. package/dist/flows/index.js +20 -0
  109. package/dist/flows/index.js.map +1 -0
  110. package/dist/flows/loader.d.ts +17 -0
  111. package/dist/flows/loader.d.ts.map +1 -0
  112. package/dist/flows/loader.js +61 -0
  113. package/dist/flows/loader.js.map +1 -0
  114. package/dist/flows/plan-builder.d.ts +40 -0
  115. package/dist/flows/plan-builder.d.ts.map +1 -0
  116. package/dist/flows/plan-builder.js +213 -0
  117. package/dist/flows/plan-builder.js.map +1 -0
  118. package/dist/flows/probes.d.ts +11 -0
  119. package/dist/flows/probes.d.ts.map +1 -0
  120. package/dist/flows/probes.js +62 -0
  121. package/dist/flows/probes.js.map +1 -0
  122. package/dist/flows/types.d.ts +950 -0
  123. package/dist/flows/types.d.ts.map +1 -0
  124. package/dist/flows/types.js +105 -0
  125. package/dist/flows/types.js.map +1 -0
  126. package/dist/index.d.ts +11 -1
  127. package/dist/index.d.ts.map +1 -1
  128. package/dist/index.js +10 -1
  129. package/dist/index.js.map +1 -1
  130. package/dist/intelligence/loader.d.ts +19 -0
  131. package/dist/intelligence/loader.d.ts.map +1 -1
  132. package/dist/intelligence/loader.js +86 -5
  133. package/dist/intelligence/loader.js.map +1 -1
  134. package/dist/intelligence/types.d.ts +1 -0
  135. package/dist/intelligence/types.d.ts.map +1 -1
  136. package/dist/packs/types.d.ts +58 -19
  137. package/dist/packs/types.d.ts.map +1 -1
  138. package/dist/packs/types.js +14 -0
  139. package/dist/packs/types.js.map +1 -1
  140. package/dist/playbooks/generic/onboarding.d.ts +9 -0
  141. package/dist/playbooks/generic/onboarding.d.ts.map +1 -0
  142. package/dist/playbooks/generic/onboarding.js +74 -0
  143. package/dist/playbooks/generic/onboarding.js.map +1 -0
  144. package/dist/playbooks/playbook-registry.d.ts.map +1 -1
  145. package/dist/playbooks/playbook-registry.js +2 -0
  146. package/dist/playbooks/playbook-registry.js.map +1 -1
  147. package/dist/runtime/admin-extra-ops.d.ts.map +1 -1
  148. package/dist/runtime/admin-extra-ops.js +15 -9
  149. package/dist/runtime/admin-extra-ops.js.map +1 -1
  150. package/dist/runtime/admin-ops.js +4 -4
  151. package/dist/runtime/admin-ops.js.map +1 -1
  152. package/dist/runtime/capture-ops.d.ts.map +1 -1
  153. package/dist/runtime/capture-ops.js +33 -1
  154. package/dist/runtime/capture-ops.js.map +1 -1
  155. package/dist/runtime/domain-ops.d.ts +21 -5
  156. package/dist/runtime/domain-ops.d.ts.map +1 -1
  157. package/dist/runtime/domain-ops.js +85 -8
  158. package/dist/runtime/domain-ops.js.map +1 -1
  159. package/dist/runtime/facades/cognee-facade.d.ts.map +1 -1
  160. package/dist/runtime/facades/cognee-facade.js +3 -1
  161. package/dist/runtime/facades/cognee-facade.js.map +1 -1
  162. package/dist/runtime/facades/index.d.ts.map +1 -1
  163. package/dist/runtime/facades/index.js +10 -6
  164. package/dist/runtime/facades/index.js.map +1 -1
  165. package/dist/runtime/facades/vault-facade.d.ts.map +1 -1
  166. package/dist/runtime/facades/vault-facade.js +2 -0
  167. package/dist/runtime/facades/vault-facade.js.map +1 -1
  168. package/dist/runtime/orchestrate-ops.d.ts +8 -7
  169. package/dist/runtime/orchestrate-ops.d.ts.map +1 -1
  170. package/dist/runtime/orchestrate-ops.js +227 -58
  171. package/dist/runtime/orchestrate-ops.js.map +1 -1
  172. package/dist/runtime/runtime.d.ts.map +1 -1
  173. package/dist/runtime/runtime.js +23 -17
  174. package/dist/runtime/runtime.js.map +1 -1
  175. package/dist/runtime/types.d.ts +6 -2
  176. package/dist/runtime/types.d.ts.map +1 -1
  177. package/dist/runtime/vault-linking-ops.d.ts +13 -0
  178. package/dist/runtime/vault-linking-ops.d.ts.map +1 -0
  179. package/dist/runtime/vault-linking-ops.js +367 -0
  180. package/dist/runtime/vault-linking-ops.js.map +1 -0
  181. package/dist/vault/linking.d.ts +46 -0
  182. package/dist/vault/linking.d.ts.map +1 -0
  183. package/dist/vault/linking.js +275 -0
  184. package/dist/vault/linking.js.map +1 -0
  185. package/dist/vault/vault-types.d.ts +37 -0
  186. package/dist/vault/vault-types.d.ts.map +1 -1
  187. package/dist/vault/vault.d.ts +12 -0
  188. package/dist/vault/vault.d.ts.map +1 -1
  189. package/dist/vault/vault.js +85 -6
  190. package/dist/vault/vault.js.map +1 -1
  191. package/package.json +4 -1
  192. package/src/__tests__/admin-extra-ops.test.ts +1 -1
  193. package/src/__tests__/admin-ops.test.ts +2 -1
  194. package/src/__tests__/cognee-client-gaps.test.ts +470 -0
  195. package/src/__tests__/cognee-hybrid-search.test.ts +478 -0
  196. package/src/__tests__/cognee-sync-manager-deep.test.ts +630 -0
  197. package/src/__tests__/cognee-sync-manager.test.ts +1 -0
  198. package/src/__tests__/core-ops.test.ts +9 -61
  199. package/src/__tests__/domain-packs.test.ts +421 -0
  200. package/src/__tests__/flows.test.ts +604 -0
  201. package/src/__tests__/playbook-registry.test.ts +2 -2
  202. package/src/__tests__/playbook-seeder.test.ts +8 -8
  203. package/src/__tests__/playbook.test.ts +5 -5
  204. package/src/__tests__/token-resolver.test.ts +79 -0
  205. package/src/brain/brain.ts +12 -0
  206. package/src/brain/intelligence.ts +21 -2
  207. package/src/capabilities/chain-mapping.ts +93 -0
  208. package/src/capabilities/index.ts +21 -0
  209. package/src/capabilities/registry.ts +290 -0
  210. package/src/capabilities/types.ts +143 -0
  211. package/src/control/intent-router.ts +46 -2
  212. package/src/domain-packs/index.ts +27 -0
  213. package/src/domain-packs/inject-rules.ts +74 -0
  214. package/src/domain-packs/knowledge-installer.ts +116 -0
  215. package/src/domain-packs/loader.ts +124 -0
  216. package/src/domain-packs/pack-runtime.ts +99 -0
  217. package/src/domain-packs/skills-installer.ts +56 -0
  218. package/src/domain-packs/token-resolver.ts +126 -0
  219. package/src/domain-packs/types.ts +229 -0
  220. package/src/engine/__tests__/register-engine.test.ts +104 -0
  221. package/src/engine/bin/soleri-engine.ts +217 -0
  222. package/src/engine/core-ops.ts +178 -0
  223. package/src/engine/index.ts +19 -0
  224. package/src/engine/register-engine.ts +385 -0
  225. package/src/engine/test-helpers.ts +83 -0
  226. package/src/flows/context-router.ts +257 -0
  227. package/src/flows/dispatch-registry.ts +80 -0
  228. package/src/flows/epilogue.ts +65 -0
  229. package/src/flows/executor.ts +182 -0
  230. package/src/flows/gate-evaluator.ts +171 -0
  231. package/src/flows/index.ts +52 -0
  232. package/src/flows/loader.ts +63 -0
  233. package/src/flows/plan-builder.ts +250 -0
  234. package/src/flows/probes.ts +70 -0
  235. package/src/flows/types.ts +217 -0
  236. package/src/index.ts +68 -1
  237. package/src/intelligence/loader.ts +96 -5
  238. package/src/intelligence/types.ts +1 -0
  239. package/src/packs/types.ts +19 -0
  240. package/src/playbooks/generic/onboarding.ts +79 -0
  241. package/src/playbooks/playbook-registry.ts +2 -0
  242. package/src/runtime/admin-extra-ops.ts +14 -8
  243. package/src/runtime/admin-ops.ts +4 -4
  244. package/src/runtime/capture-ops.ts +40 -1
  245. package/src/runtime/domain-ops.ts +92 -7
  246. package/src/runtime/facades/cognee-facade.ts +3 -1
  247. package/src/runtime/facades/index.ts +12 -6
  248. package/src/runtime/facades/vault-facade.ts +2 -0
  249. package/src/runtime/orchestrate-ops.ts +271 -62
  250. package/src/runtime/runtime.ts +27 -18
  251. package/src/runtime/types.ts +6 -2
  252. package/src/runtime/vault-linking-ops.ts +454 -0
  253. package/src/vault/linking.ts +333 -0
  254. package/src/vault/vault-types.ts +46 -0
  255. package/src/vault/vault.ts +94 -7
@@ -0,0 +1,257 @@
1
+ /**
2
+ * Context-sensitive chain routing — the same intent routes to different chain
3
+ * sequences depending on what's being built/fixed/reviewed.
4
+ *
5
+ * Building a Button follows a different workflow than building a Page layout.
6
+ */
7
+
8
+ import type { PlanStep, ProbeName } from './types.js';
9
+ import { chainToToolName, chainToRequires } from './plan-builder.js';
10
+
11
+ // ---------------------------------------------------------------------------
12
+ // Types
13
+ // ---------------------------------------------------------------------------
14
+
15
+ export interface ContextOverride {
16
+ /** Pattern to match against prompt or entities */
17
+ match: RegExp;
18
+ /** Context label */
19
+ context: string;
20
+ /** Chain substitutions: original chain → replacement chain */
21
+ chainOverrides?: Record<string, string>;
22
+ /** Additional chains to inject before specific steps */
23
+ injectBefore?: Record<string, string[]>;
24
+ /** Additional chains to inject after specific steps */
25
+ injectAfter?: Record<string, string[]>;
26
+ /** Steps to skip in this context */
27
+ skipSteps?: string[];
28
+ }
29
+
30
+ // ---------------------------------------------------------------------------
31
+ // Context override definitions
32
+ // ---------------------------------------------------------------------------
33
+
34
+ const BUILD_OVERRIDES: ContextOverride[] = [
35
+ {
36
+ match: /\b(button|icon|badge|chip|tag|pill)\b/i,
37
+ context: 'small-component',
38
+ skipSteps: ['get-architecture'],
39
+ injectBefore: {
40
+ validate: ['button-semantics-check'],
41
+ },
42
+ },
43
+ {
44
+ match: /\b(page|layout|dashboard|screen|view)\b/i,
45
+ context: 'large-component',
46
+ injectBefore: {
47
+ validate: ['responsive-patterns'],
48
+ },
49
+ injectAfter: {
50
+ validate: ['performance-check'],
51
+ },
52
+ },
53
+ {
54
+ match: /\b(form|input|select|textarea|checkbox|radio|switch|dropdown)\b/i,
55
+ context: 'form-component',
56
+ injectBefore: {
57
+ validate: ['defensive-design', 'accessibility-precheck'],
58
+ },
59
+ },
60
+ {
61
+ match: /\b(modal|dialog|sheet|drawer|popover|overlay|tooltip)\b/i,
62
+ context: 'container-component',
63
+ injectBefore: {
64
+ validate: ['container-pattern-check', 'dialog-patterns'],
65
+ },
66
+ },
67
+ ];
68
+
69
+ const FIX_OVERRIDES: ContextOverride[] = [
70
+ {
71
+ match: /\b(styl(e|ing)|color|token|theme|palette|css)\b/i,
72
+ context: 'design-fix',
73
+ injectBefore: {
74
+ validate: ['contrast-check', 'token-validation'],
75
+ },
76
+ },
77
+ {
78
+ match: /\b(accessib|a11y|aria|screen.?reader|keyboard|focus)\b/i,
79
+ context: 'a11y-fix',
80
+ injectBefore: {
81
+ validate: ['accessibility-audit'],
82
+ },
83
+ },
84
+ ];
85
+
86
+ const REVIEW_OVERRIDES: ContextOverride[] = [
87
+ {
88
+ match: /\b(pr|pull.?request|diff|merge)\b/i,
89
+ context: 'pr-review',
90
+ injectAfter: {
91
+ 'check-rules': ['review-pr-design'],
92
+ },
93
+ },
94
+ {
95
+ match: /\b(architecture|import|dependency|structure)\b/i,
96
+ context: 'architecture-review',
97
+ injectAfter: {
98
+ 'check-rules': ['check-architecture'],
99
+ },
100
+ },
101
+ ];
102
+
103
+ /**
104
+ * Registry mapping flow IDs to their context overrides.
105
+ */
106
+ const FLOW_OVERRIDES: Record<string, ContextOverride[]> = {
107
+ 'BUILD-flow': BUILD_OVERRIDES,
108
+ 'FIX-flow': FIX_OVERRIDES,
109
+ 'REVIEW-flow': REVIEW_OVERRIDES,
110
+ };
111
+
112
+ // ---------------------------------------------------------------------------
113
+ // Public API
114
+ // ---------------------------------------------------------------------------
115
+
116
+ /**
117
+ * Detect which contexts apply to a prompt and entity set.
118
+ * Returns an array of matching context labels.
119
+ */
120
+ export function detectContext(
121
+ prompt: string,
122
+ entities: { components: string[]; actions: string[] },
123
+ ): string[] {
124
+ const contexts: string[] = [];
125
+ const searchText = [prompt, ...entities.components, ...entities.actions].join(' ');
126
+
127
+ // Check all flow overrides — a prompt might match contexts across flows
128
+ for (const overrides of Object.values(FLOW_OVERRIDES)) {
129
+ for (const override of overrides) {
130
+ if (override.match.test(searchText) && !contexts.includes(override.context)) {
131
+ contexts.push(override.context);
132
+ }
133
+ }
134
+ }
135
+
136
+ return contexts;
137
+ }
138
+
139
+ /**
140
+ * Apply context overrides to a set of plan steps: chain substitutions,
141
+ * injections (before/after), and step skipping.
142
+ */
143
+ export function applyContextOverrides(
144
+ steps: PlanStep[],
145
+ contexts: string[],
146
+ flowId: string,
147
+ agentId: string = 'agent',
148
+ ): PlanStep[] {
149
+ const overrides = FLOW_OVERRIDES[flowId];
150
+ if (!overrides || contexts.length === 0) return steps;
151
+
152
+ // Collect active overrides for the detected contexts
153
+ const active = overrides.filter((o) => contexts.includes(o.context));
154
+ if (active.length === 0) return steps;
155
+
156
+ // Aggregate all skip, inject-before, inject-after, and chain overrides
157
+ const skipSet = new Set<string>();
158
+ const injectBefore = new Map<string, string[]>();
159
+ const injectAfter = new Map<string, string[]>();
160
+ const chainSubs = new Map<string, string>();
161
+
162
+ for (const ov of active) {
163
+ if (ov.skipSteps) ov.skipSteps.forEach((s) => skipSet.add(s));
164
+ if (ov.injectBefore) {
165
+ for (const [stepId, chains] of Object.entries(ov.injectBefore)) {
166
+ const existing = injectBefore.get(stepId) ?? [];
167
+ injectBefore.set(stepId, [...existing, ...chains]);
168
+ }
169
+ }
170
+ if (ov.injectAfter) {
171
+ for (const [stepId, chains] of Object.entries(ov.injectAfter)) {
172
+ const existing = injectAfter.get(stepId) ?? [];
173
+ injectAfter.set(stepId, [...existing, ...chains]);
174
+ }
175
+ }
176
+ if (ov.chainOverrides) {
177
+ for (const [from, to] of Object.entries(ov.chainOverrides)) {
178
+ chainSubs.set(from, to);
179
+ }
180
+ }
181
+ }
182
+
183
+ const result: PlanStep[] = [];
184
+
185
+ for (const step of steps) {
186
+ // 1. Skip steps
187
+ if (skipSet.has(step.id)) continue;
188
+
189
+ // 2. Inject before
190
+ const beforeChains = injectBefore.get(step.id);
191
+ if (beforeChains) {
192
+ result.push(buildInjectedStep(step.id, 'before', beforeChains, agentId));
193
+ }
194
+
195
+ // 3. Apply chain substitutions to existing step
196
+ let processed = step;
197
+ if (chainSubs.size > 0) {
198
+ const newTools = step.tools.map((tool) => {
199
+ for (const [from, to] of chainSubs) {
200
+ const fromTool = chainToToolName(from, agentId);
201
+ if (tool === fromTool) return chainToToolName(to, agentId);
202
+ }
203
+ return tool;
204
+ });
205
+ if (newTools.some((t, i) => t !== step.tools[i])) {
206
+ processed = { ...step, tools: newTools };
207
+ }
208
+ }
209
+
210
+ result.push(processed);
211
+
212
+ // 4. Inject after
213
+ const afterChains = injectAfter.get(step.id);
214
+ if (afterChains) {
215
+ result.push(buildInjectedStep(step.id, 'after', afterChains, agentId));
216
+ }
217
+ }
218
+
219
+ return result;
220
+ }
221
+
222
+ /**
223
+ * Get all registered context overrides for a flow (useful for introspection).
224
+ */
225
+ export function getFlowOverrides(flowId: string): ContextOverride[] {
226
+ return FLOW_OVERRIDES[flowId] ?? [];
227
+ }
228
+
229
+ // ---------------------------------------------------------------------------
230
+ // Helpers
231
+ // ---------------------------------------------------------------------------
232
+
233
+ /**
234
+ * Build a synthetic PlanStep from injected chains.
235
+ */
236
+ function buildInjectedStep(
237
+ anchorStepId: string,
238
+ position: 'before' | 'after',
239
+ chains: string[],
240
+ agentId: string,
241
+ ): PlanStep {
242
+ const tools = chains.map((c) => chainToToolName(c, agentId));
243
+ const requires: ProbeName[] = [];
244
+ for (const chain of chains) {
245
+ const req = chainToRequires(chain);
246
+ if (req && !requires.includes(req)) requires.push(req);
247
+ }
248
+
249
+ return {
250
+ id: `ctx-${position}-${anchorStepId}`,
251
+ name: `Context: ${chains.join(', ')} (${position} ${anchorStepId})`,
252
+ tools,
253
+ parallel: chains.length > 1,
254
+ requires,
255
+ status: 'pending',
256
+ };
257
+ }
@@ -0,0 +1,80 @@
1
+ /**
2
+ * Dispatch registry — maps tool names to real facade op handlers.
3
+ *
4
+ * Tool names follow the convention: `{agentId}_{facadeName}_{opName}`
5
+ * e.g. "myagent_vault_search" → facade "myagent_vault", op "search".
6
+ */
7
+
8
+ import type { FacadeConfig } from '../facades/types.js';
9
+
10
+ type DispatchResult = { tool: string; status: string; data?: unknown; error?: string };
11
+ type DispatchFn = (toolName: string, params: Record<string, unknown>) => Promise<DispatchResult>;
12
+
13
+ /**
14
+ * Create a dispatcher function that routes tool calls to the correct facade op.
15
+ *
16
+ * @param agentId - Agent identifier prefix (e.g. "myagent")
17
+ * @param facades - Array of registered facade configs
18
+ * @returns A dispatch function that takes (toolName, params) and calls the matching handler
19
+ */
20
+ export function createDispatcher(agentId: string, facades: FacadeConfig[]): DispatchFn {
21
+ // Build a lookup map: facadeName → { opName → handler }
22
+ const facadeMap = new Map<string, FacadeConfig>();
23
+ for (const facade of facades) {
24
+ facadeMap.set(facade.name, facade);
25
+ }
26
+
27
+ return async (toolName: string, params: Record<string, unknown>): Promise<DispatchResult> => {
28
+ const prefix = `${agentId}_`;
29
+
30
+ // Strip agent prefix if present
31
+ const unprefixed = toolName.startsWith(prefix) ? toolName.slice(prefix.length) : toolName;
32
+
33
+ // Try progressively longer facade name prefixes.
34
+ // E.g. for "vault_extra_search": try "vault_extra_search", "vault_extra", "vault"
35
+ const parts = unprefixed.split('_');
36
+
37
+ for (let splitAt = parts.length - 1; splitAt >= 1; splitAt--) {
38
+ const facadeSuffix = parts.slice(0, splitAt).join('_');
39
+ const opName = parts.slice(splitAt).join('_');
40
+ const facadeFullName = `${prefix}${facadeSuffix}`;
41
+
42
+ const facade = facadeMap.get(facadeFullName);
43
+ if (!facade) continue;
44
+
45
+ const op = facade.ops.find((o) => o.name === opName);
46
+ if (!op) continue;
47
+
48
+ try {
49
+ const result = await op.handler(params);
50
+ return { tool: toolName, status: 'ok', data: result };
51
+ } catch (err) {
52
+ return {
53
+ tool: toolName,
54
+ status: 'error',
55
+ error: err instanceof Error ? err.message : String(err),
56
+ };
57
+ }
58
+ }
59
+
60
+ // Also try matching the full unprefixed name as a facade with "op" from params
61
+ const facade = facadeMap.get(`${prefix}${unprefixed}`);
62
+ if (facade && params.op && typeof params.op === 'string') {
63
+ const op = facade.ops.find((o) => o.name === params.op);
64
+ if (op) {
65
+ try {
66
+ const result = await op.handler(params);
67
+ return { tool: toolName, status: 'ok', data: result };
68
+ } catch (err) {
69
+ return {
70
+ tool: toolName,
71
+ status: 'error',
72
+ error: err instanceof Error ? err.message : String(err),
73
+ };
74
+ }
75
+ }
76
+ }
77
+
78
+ return { tool: toolName, status: 'unregistered' };
79
+ };
80
+ }
@@ -0,0 +1,65 @@
1
+ /**
2
+ * Epilogue — post-execution cleanup: capture knowledge and session summary.
3
+ * All operations are resilient (errors are caught, never propagated).
4
+ */
5
+
6
+ import type { ProbeResults } from './types.js';
7
+
8
+ type DispatchFn = (
9
+ toolName: string,
10
+ params: Record<string, unknown>,
11
+ ) => Promise<{ tool: string; status: string; data?: unknown; error?: string }>;
12
+
13
+ /**
14
+ * Run post-execution epilogue steps.
15
+ * - Captures knowledge to vault (if available)
16
+ * - Captures session summary (if session store available)
17
+ *
18
+ * @returns Whether anything was captured and an optional session ID.
19
+ */
20
+ export async function runEpilogue(
21
+ dispatch: DispatchFn,
22
+ probes: ProbeResults,
23
+ projectPath: string,
24
+ summary: string,
25
+ ): Promise<{ captured: boolean; sessionId?: string }> {
26
+ let captured = false;
27
+ let sessionId: string | undefined;
28
+
29
+ // Capture knowledge to vault
30
+ if (probes.vault) {
31
+ try {
32
+ await dispatch('capture_knowledge', {
33
+ title: 'Flow execution summary',
34
+ content: summary,
35
+ type: 'workflow',
36
+ tags: ['flow-engine', 'auto-captured'],
37
+ projectPath,
38
+ });
39
+ captured = true;
40
+ } catch {
41
+ // Silently ignore — vault capture is best-effort
42
+ }
43
+ }
44
+
45
+ // Capture session
46
+ if (probes.sessionStore) {
47
+ try {
48
+ const result = await dispatch('session_capture', {
49
+ summary,
50
+ projectPath,
51
+ });
52
+ captured = true;
53
+ if (result.data && typeof result.data === 'object') {
54
+ const data = result.data as Record<string, unknown>;
55
+ if (typeof data.sessionId === 'string') {
56
+ sessionId = data.sessionId;
57
+ }
58
+ }
59
+ } catch {
60
+ // Silently ignore — session capture is best-effort
61
+ }
62
+ }
63
+
64
+ return { captured, sessionId };
65
+ }
@@ -0,0 +1,182 @@
1
+ /**
2
+ * Flow executor — runs an orchestration plan step-by-step,
3
+ * evaluating gates and handling branching.
4
+ */
5
+
6
+ import type { OrchestrationPlan, ExecutionResult, StepResult } from './types.js';
7
+ import { evaluateGate } from './gate-evaluator.js';
8
+
9
+ /** Maximum iterations for BRANCH loops to prevent infinite cycles. */
10
+ const MAX_BRANCH_ITERATIONS = 10;
11
+
12
+ type DispatchFn = (
13
+ toolName: string,
14
+ params: Record<string, unknown>,
15
+ ) => Promise<{ tool: string; status: string; data?: unknown; error?: string }>;
16
+
17
+ /**
18
+ * Executes an orchestration plan sequentially (with parallel inner steps).
19
+ */
20
+ export class FlowExecutor {
21
+ private dispatch: DispatchFn;
22
+
23
+ constructor(dispatch: DispatchFn) {
24
+ this.dispatch = dispatch;
25
+ }
26
+
27
+ /**
28
+ * Execute a full orchestration plan. Returns an ExecutionResult
29
+ * summarizing what happened.
30
+ */
31
+ async execute(plan: OrchestrationPlan): Promise<ExecutionResult> {
32
+ const startTime = Date.now();
33
+ const stepResults: StepResult[] = [];
34
+ const toolsCalled: string[] = [];
35
+ let branchIterations = 0;
36
+ let currentIndex = 0;
37
+
38
+ while (currentIndex < plan.steps.length) {
39
+ const step = plan.steps[currentIndex];
40
+ const stepStart = Date.now();
41
+ step.status = 'running';
42
+
43
+ const toolResults: StepResult['toolResults'] = {};
44
+
45
+ try {
46
+ if (step.parallel && step.tools.length > 1) {
47
+ // Execute tools in parallel
48
+ const results = await Promise.allSettled(
49
+ step.tools.map((tool) => this.dispatch(tool, { stepId: step.id, planId: plan.planId })),
50
+ );
51
+ for (let i = 0; i < step.tools.length; i++) {
52
+ const toolName = step.tools[i];
53
+ const result = results[i];
54
+ if (result.status === 'fulfilled') {
55
+ toolResults[toolName] = result.value;
56
+ } else {
57
+ toolResults[toolName] = {
58
+ tool: toolName,
59
+ status: 'error',
60
+ error:
61
+ result.reason instanceof Error ? result.reason.message : String(result.reason),
62
+ };
63
+ }
64
+ toolsCalled.push(toolName);
65
+ }
66
+ } else {
67
+ // Execute tools sequentially
68
+ for (const toolName of step.tools) {
69
+ try {
70
+ toolResults[toolName] = await this.dispatch(toolName, {
71
+ stepId: step.id,
72
+ planId: plan.planId,
73
+ });
74
+ } catch (_err) {
75
+ toolResults[toolName] = {
76
+ tool: toolName,
77
+ status: 'error',
78
+ error: _err instanceof Error ? _err.message : String(_err),
79
+ };
80
+ }
81
+ toolsCalled.push(toolName);
82
+ }
83
+ }
84
+ } catch (_err) {
85
+ // Entire step failed
86
+ const stepResult: StepResult = {
87
+ stepId: step.id,
88
+ status: 'failed',
89
+ toolResults,
90
+ durationMs: Date.now() - stepStart,
91
+ };
92
+ stepResults.push(stepResult);
93
+ step.status = 'failed';
94
+ break;
95
+ }
96
+
97
+ // Evaluate gate
98
+ const flatData: Record<string, unknown> = {};
99
+ for (const [key, val] of Object.entries(toolResults)) {
100
+ flatData[key] = val;
101
+ if (val.data && typeof val.data === 'object') {
102
+ Object.assign(flatData, val.data as Record<string, unknown>);
103
+ }
104
+ }
105
+
106
+ const verdict = evaluateGate(step.gate, flatData);
107
+
108
+ const stepResult: StepResult = {
109
+ stepId: step.id,
110
+ status: verdict.passed ? 'passed' : 'failed',
111
+ toolResults,
112
+ durationMs: Date.now() - stepStart,
113
+ };
114
+
115
+ if (!verdict.passed || verdict.action !== 'CONTINUE') {
116
+ stepResult.gateResult = {
117
+ action: verdict.action,
118
+ message: verdict.message,
119
+ };
120
+ }
121
+
122
+ stepResults.push(stepResult);
123
+
124
+ // Handle gate action
125
+ switch (verdict.action) {
126
+ case 'STOP':
127
+ step.status = 'failed';
128
+ // Stop execution
129
+ return buildResult(plan, stepResults, toolsCalled, startTime, 'partial');
130
+
131
+ case 'BRANCH': {
132
+ step.status = verdict.passed ? 'passed' : 'gate-paused';
133
+ branchIterations++;
134
+ if (branchIterations >= MAX_BRANCH_ITERATIONS) {
135
+ return buildResult(plan, stepResults, toolsCalled, startTime, 'partial');
136
+ }
137
+ if (verdict.goto) {
138
+ const targetIdx = plan.steps.findIndex((s) => s.id === verdict.goto);
139
+ if (targetIdx >= 0) {
140
+ currentIndex = targetIdx;
141
+ continue;
142
+ }
143
+ }
144
+ // No valid goto — continue to next step
145
+ step.status = 'passed';
146
+ currentIndex++;
147
+ break;
148
+ }
149
+
150
+ case 'CONTINUE':
151
+ default:
152
+ step.status = verdict.passed ? 'passed' : 'failed';
153
+ currentIndex++;
154
+ break;
155
+ }
156
+ }
157
+
158
+ const allPassed = stepResults.every((r) => r.status === 'passed');
159
+ const anyFailed = stepResults.some((r) => r.status === 'failed');
160
+ const status = allPassed ? 'completed' : anyFailed ? 'partial' : 'completed';
161
+
162
+ return buildResult(plan, stepResults, toolsCalled, startTime, status);
163
+ }
164
+ }
165
+
166
+ function buildResult(
167
+ plan: OrchestrationPlan,
168
+ stepResults: StepResult[],
169
+ toolsCalled: string[],
170
+ startTime: number,
171
+ status: ExecutionResult['status'],
172
+ ): ExecutionResult {
173
+ return {
174
+ planId: plan.planId,
175
+ status,
176
+ stepsCompleted: stepResults.filter((r) => r.status === 'passed').length,
177
+ totalSteps: plan.steps.length,
178
+ toolsCalled: [...new Set(toolsCalled)],
179
+ durationMs: Date.now() - startTime,
180
+ stepResults,
181
+ };
182
+ }