@principal-ai/principal-view-core 0.5.6

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 (204) hide show
  1. package/README.md +126 -0
  2. package/dist/ConfigurationLoader.d.ts +76 -0
  3. package/dist/ConfigurationLoader.d.ts.map +1 -0
  4. package/dist/ConfigurationLoader.js +144 -0
  5. package/dist/ConfigurationLoader.js.map +1 -0
  6. package/dist/ConfigurationValidator.d.ts +31 -0
  7. package/dist/ConfigurationValidator.d.ts.map +1 -0
  8. package/dist/ConfigurationValidator.js +242 -0
  9. package/dist/ConfigurationValidator.js.map +1 -0
  10. package/dist/EventProcessor.d.ts +49 -0
  11. package/dist/EventProcessor.d.ts.map +1 -0
  12. package/dist/EventProcessor.js +215 -0
  13. package/dist/EventProcessor.js.map +1 -0
  14. package/dist/EventRecorderService.d.ts +305 -0
  15. package/dist/EventRecorderService.d.ts.map +1 -0
  16. package/dist/EventRecorderService.js +463 -0
  17. package/dist/EventRecorderService.js.map +1 -0
  18. package/dist/LibraryLoader.d.ts +63 -0
  19. package/dist/LibraryLoader.d.ts.map +1 -0
  20. package/dist/LibraryLoader.js +188 -0
  21. package/dist/LibraryLoader.js.map +1 -0
  22. package/dist/PathBasedEventProcessor.d.ts +90 -0
  23. package/dist/PathBasedEventProcessor.d.ts.map +1 -0
  24. package/dist/PathBasedEventProcessor.js +239 -0
  25. package/dist/PathBasedEventProcessor.js.map +1 -0
  26. package/dist/SessionManager.d.ts +194 -0
  27. package/dist/SessionManager.d.ts.map +1 -0
  28. package/dist/SessionManager.js +299 -0
  29. package/dist/SessionManager.js.map +1 -0
  30. package/dist/ValidationEngine.d.ts +31 -0
  31. package/dist/ValidationEngine.d.ts.map +1 -0
  32. package/dist/ValidationEngine.js +158 -0
  33. package/dist/ValidationEngine.js.map +1 -0
  34. package/dist/helpers/GraphInstrumentationHelper.d.ts +93 -0
  35. package/dist/helpers/GraphInstrumentationHelper.d.ts.map +1 -0
  36. package/dist/helpers/GraphInstrumentationHelper.js +248 -0
  37. package/dist/helpers/GraphInstrumentationHelper.js.map +1 -0
  38. package/dist/index.d.ts +33 -0
  39. package/dist/index.d.ts.map +1 -0
  40. package/dist/index.js +34 -0
  41. package/dist/index.js.map +1 -0
  42. package/dist/rules/config.d.ts +57 -0
  43. package/dist/rules/config.d.ts.map +1 -0
  44. package/dist/rules/config.js +382 -0
  45. package/dist/rules/config.js.map +1 -0
  46. package/dist/rules/engine.d.ts +70 -0
  47. package/dist/rules/engine.d.ts.map +1 -0
  48. package/dist/rules/engine.js +252 -0
  49. package/dist/rules/engine.js.map +1 -0
  50. package/dist/rules/implementations/connection-type-references.d.ts +7 -0
  51. package/dist/rules/implementations/connection-type-references.d.ts.map +1 -0
  52. package/dist/rules/implementations/connection-type-references.js +104 -0
  53. package/dist/rules/implementations/connection-type-references.js.map +1 -0
  54. package/dist/rules/implementations/dead-end-states.d.ts +17 -0
  55. package/dist/rules/implementations/dead-end-states.d.ts.map +1 -0
  56. package/dist/rules/implementations/dead-end-states.js +72 -0
  57. package/dist/rules/implementations/dead-end-states.js.map +1 -0
  58. package/dist/rules/implementations/index.d.ts +24 -0
  59. package/dist/rules/implementations/index.d.ts.map +1 -0
  60. package/dist/rules/implementations/index.js +62 -0
  61. package/dist/rules/implementations/index.js.map +1 -0
  62. package/dist/rules/implementations/library-node-type-match.d.ts +17 -0
  63. package/dist/rules/implementations/library-node-type-match.d.ts.map +1 -0
  64. package/dist/rules/implementations/library-node-type-match.js +123 -0
  65. package/dist/rules/implementations/library-node-type-match.js.map +1 -0
  66. package/dist/rules/implementations/minimum-node-sources.d.ts +22 -0
  67. package/dist/rules/implementations/minimum-node-sources.d.ts.map +1 -0
  68. package/dist/rules/implementations/minimum-node-sources.js +54 -0
  69. package/dist/rules/implementations/minimum-node-sources.js.map +1 -0
  70. package/dist/rules/implementations/no-unknown-fields.d.ts +7 -0
  71. package/dist/rules/implementations/no-unknown-fields.d.ts.map +1 -0
  72. package/dist/rules/implementations/no-unknown-fields.js +211 -0
  73. package/dist/rules/implementations/no-unknown-fields.js.map +1 -0
  74. package/dist/rules/implementations/orphaned-edge-types.d.ts +7 -0
  75. package/dist/rules/implementations/orphaned-edge-types.d.ts.map +1 -0
  76. package/dist/rules/implementations/orphaned-edge-types.js +47 -0
  77. package/dist/rules/implementations/orphaned-edge-types.js.map +1 -0
  78. package/dist/rules/implementations/orphaned-node-types.d.ts +7 -0
  79. package/dist/rules/implementations/orphaned-node-types.d.ts.map +1 -0
  80. package/dist/rules/implementations/orphaned-node-types.js +50 -0
  81. package/dist/rules/implementations/orphaned-node-types.js.map +1 -0
  82. package/dist/rules/implementations/required-metadata.d.ts +7 -0
  83. package/dist/rules/implementations/required-metadata.d.ts.map +1 -0
  84. package/dist/rules/implementations/required-metadata.js +57 -0
  85. package/dist/rules/implementations/required-metadata.js.map +1 -0
  86. package/dist/rules/implementations/state-transition-references.d.ts +7 -0
  87. package/dist/rules/implementations/state-transition-references.d.ts.map +1 -0
  88. package/dist/rules/implementations/state-transition-references.js +135 -0
  89. package/dist/rules/implementations/state-transition-references.js.map +1 -0
  90. package/dist/rules/implementations/unreachable-states.d.ts +7 -0
  91. package/dist/rules/implementations/unreachable-states.d.ts.map +1 -0
  92. package/dist/rules/implementations/unreachable-states.js +80 -0
  93. package/dist/rules/implementations/unreachable-states.js.map +1 -0
  94. package/dist/rules/implementations/valid-action-patterns.d.ts +17 -0
  95. package/dist/rules/implementations/valid-action-patterns.d.ts.map +1 -0
  96. package/dist/rules/implementations/valid-action-patterns.js +109 -0
  97. package/dist/rules/implementations/valid-action-patterns.js.map +1 -0
  98. package/dist/rules/implementations/valid-color-format.d.ts +7 -0
  99. package/dist/rules/implementations/valid-color-format.d.ts.map +1 -0
  100. package/dist/rules/implementations/valid-color-format.js +91 -0
  101. package/dist/rules/implementations/valid-color-format.js.map +1 -0
  102. package/dist/rules/implementations/valid-edge-types.d.ts +7 -0
  103. package/dist/rules/implementations/valid-edge-types.d.ts.map +1 -0
  104. package/dist/rules/implementations/valid-edge-types.js +244 -0
  105. package/dist/rules/implementations/valid-edge-types.js.map +1 -0
  106. package/dist/rules/implementations/valid-node-types.d.ts +7 -0
  107. package/dist/rules/implementations/valid-node-types.d.ts.map +1 -0
  108. package/dist/rules/implementations/valid-node-types.js +175 -0
  109. package/dist/rules/implementations/valid-node-types.js.map +1 -0
  110. package/dist/rules/index.d.ts +28 -0
  111. package/dist/rules/index.d.ts.map +1 -0
  112. package/dist/rules/index.js +45 -0
  113. package/dist/rules/index.js.map +1 -0
  114. package/dist/rules/types.d.ts +309 -0
  115. package/dist/rules/types.d.ts.map +1 -0
  116. package/dist/rules/types.js +35 -0
  117. package/dist/rules/types.js.map +1 -0
  118. package/dist/types/canvas.d.ts +409 -0
  119. package/dist/types/canvas.d.ts.map +1 -0
  120. package/dist/types/canvas.js +70 -0
  121. package/dist/types/canvas.js.map +1 -0
  122. package/dist/types/index.d.ts +311 -0
  123. package/dist/types/index.d.ts.map +1 -0
  124. package/dist/types/index.js +13 -0
  125. package/dist/types/index.js.map +1 -0
  126. package/dist/types/library.d.ts +185 -0
  127. package/dist/types/library.d.ts.map +1 -0
  128. package/dist/types/library.js +15 -0
  129. package/dist/types/library.js.map +1 -0
  130. package/dist/types/path-based-config.d.ts +230 -0
  131. package/dist/types/path-based-config.d.ts.map +1 -0
  132. package/dist/types/path-based-config.js +9 -0
  133. package/dist/types/path-based-config.js.map +1 -0
  134. package/dist/utils/CanvasConverter.d.ts +118 -0
  135. package/dist/utils/CanvasConverter.d.ts.map +1 -0
  136. package/dist/utils/CanvasConverter.js +315 -0
  137. package/dist/utils/CanvasConverter.js.map +1 -0
  138. package/dist/utils/GraphConverter.d.ts +18 -0
  139. package/dist/utils/GraphConverter.d.ts.map +1 -0
  140. package/dist/utils/GraphConverter.js +61 -0
  141. package/dist/utils/GraphConverter.js.map +1 -0
  142. package/dist/utils/LibraryConverter.d.ts +113 -0
  143. package/dist/utils/LibraryConverter.d.ts.map +1 -0
  144. package/dist/utils/LibraryConverter.js +166 -0
  145. package/dist/utils/LibraryConverter.js.map +1 -0
  146. package/dist/utils/PathMatcher.d.ts +55 -0
  147. package/dist/utils/PathMatcher.d.ts.map +1 -0
  148. package/dist/utils/PathMatcher.js +172 -0
  149. package/dist/utils/PathMatcher.js.map +1 -0
  150. package/dist/utils/YamlParser.d.ts +36 -0
  151. package/dist/utils/YamlParser.d.ts.map +1 -0
  152. package/dist/utils/YamlParser.js +63 -0
  153. package/dist/utils/YamlParser.js.map +1 -0
  154. package/package.json +47 -0
  155. package/src/ConfigurationLoader.test.ts +490 -0
  156. package/src/ConfigurationLoader.ts +185 -0
  157. package/src/ConfigurationValidator.test.ts +200 -0
  158. package/src/ConfigurationValidator.ts +283 -0
  159. package/src/EventProcessor.test.ts +405 -0
  160. package/src/EventProcessor.ts +250 -0
  161. package/src/EventRecorderService.test.ts +541 -0
  162. package/src/EventRecorderService.ts +744 -0
  163. package/src/LibraryLoader.ts +215 -0
  164. package/src/PathBasedEventProcessor.test.ts +567 -0
  165. package/src/PathBasedEventProcessor.ts +332 -0
  166. package/src/SessionManager.test.ts +424 -0
  167. package/src/SessionManager.ts +470 -0
  168. package/src/ValidationEngine.test.ts +371 -0
  169. package/src/ValidationEngine.ts +196 -0
  170. package/src/helpers/GraphInstrumentationHelper.test.ts +340 -0
  171. package/src/helpers/GraphInstrumentationHelper.ts +326 -0
  172. package/src/index.ts +85 -0
  173. package/src/rules/config.test.ts +278 -0
  174. package/src/rules/config.ts +459 -0
  175. package/src/rules/engine.test.ts +332 -0
  176. package/src/rules/engine.ts +318 -0
  177. package/src/rules/implementations/connection-type-references.ts +117 -0
  178. package/src/rules/implementations/dead-end-states.ts +101 -0
  179. package/src/rules/implementations/index.ts +73 -0
  180. package/src/rules/implementations/library-node-type-match.ts +148 -0
  181. package/src/rules/implementations/minimum-node-sources.ts +82 -0
  182. package/src/rules/implementations/no-unknown-fields.ts +342 -0
  183. package/src/rules/implementations/orphaned-edge-types.ts +55 -0
  184. package/src/rules/implementations/orphaned-node-types.ts +58 -0
  185. package/src/rules/implementations/required-metadata.ts +64 -0
  186. package/src/rules/implementations/state-transition-references.ts +151 -0
  187. package/src/rules/implementations/unreachable-states.ts +94 -0
  188. package/src/rules/implementations/valid-action-patterns.ts +136 -0
  189. package/src/rules/implementations/valid-color-format.ts +140 -0
  190. package/src/rules/implementations/valid-edge-types.ts +258 -0
  191. package/src/rules/implementations/valid-node-types.ts +189 -0
  192. package/src/rules/index.ts +95 -0
  193. package/src/rules/types.ts +426 -0
  194. package/src/types/canvas.ts +496 -0
  195. package/src/types/index.ts +382 -0
  196. package/src/types/library.ts +233 -0
  197. package/src/types/path-based-config.ts +281 -0
  198. package/src/utils/CanvasConverter.ts +431 -0
  199. package/src/utils/GraphConverter.test.ts +195 -0
  200. package/src/utils/GraphConverter.ts +71 -0
  201. package/src/utils/LibraryConverter.ts +245 -0
  202. package/src/utils/PathMatcher.test.ts +148 -0
  203. package/src/utils/PathMatcher.ts +183 -0
  204. package/src/utils/YamlParser.ts +75 -0
@@ -0,0 +1,215 @@
1
+ /**
2
+ * Library Loader
3
+ *
4
+ * Loads component library files (.yaml, .yml, or .json) from the .vgc/ folder.
5
+ * Component libraries contain reusable node and edge type definitions.
6
+ */
7
+
8
+ import type { FileSystemAdapter } from '@principal-ai/repository-abstraction';
9
+ import type { ComponentLibrary, LibraryLoadResult } from './types/library';
10
+ import * as yaml from 'js-yaml';
11
+
12
+ /**
13
+ * Default library file names to search for (in order of preference)
14
+ */
15
+ const DEFAULT_LIBRARY_FILES = ['library.yaml', 'library.yml', 'library.json'];
16
+
17
+ /**
18
+ * Configuration directory name
19
+ */
20
+ const CONFIG_DIR = '.vgc';
21
+
22
+ /**
23
+ * Loader for component library files
24
+ */
25
+ export class LibraryLoader {
26
+ constructor(private fsAdapter: FileSystemAdapter) {}
27
+
28
+ /**
29
+ * Load the component library from the .vgc/ folder
30
+ *
31
+ * Searches for library.yaml, library.yml, or library.json (in that order).
32
+ *
33
+ * @param baseDir - Base directory containing .vgc/ folder
34
+ * @returns Library load result
35
+ */
36
+ load(baseDir: string): LibraryLoadResult {
37
+ const configPath = this.fsAdapter.join(baseDir, CONFIG_DIR);
38
+
39
+ // Check if .vgc directory exists
40
+ if (!this.fsAdapter.exists(configPath) || !this.fsAdapter.isDirectory(configPath)) {
41
+ return {
42
+ success: false,
43
+ error: 'Configuration directory .vgc/ not found',
44
+ path: configPath,
45
+ };
46
+ }
47
+
48
+ // Try each default library file name
49
+ for (const fileName of DEFAULT_LIBRARY_FILES) {
50
+ const fullPath = this.fsAdapter.join(configPath, fileName);
51
+
52
+ if (this.fsAdapter.exists(fullPath)) {
53
+ return this.loadFromPath(fullPath);
54
+ }
55
+ }
56
+
57
+ return {
58
+ success: false,
59
+ error: `No library file found. Expected one of: ${DEFAULT_LIBRARY_FILES.join(', ')}`,
60
+ path: configPath,
61
+ };
62
+ }
63
+
64
+ /**
65
+ * Load a library from a specific file path
66
+ *
67
+ * @param filePath - Full path to the library file
68
+ * @returns Library load result
69
+ */
70
+ loadFromPath(filePath: string): LibraryLoadResult {
71
+ try {
72
+ const content = this.fsAdapter.readFile(filePath);
73
+ const isJson = filePath.endsWith('.json');
74
+
75
+ const library = isJson ? this.parseJson(content, filePath) : this.parseYaml(content, filePath);
76
+
77
+ const validationError = this.validate(library, filePath);
78
+ if (validationError) {
79
+ return {
80
+ success: false,
81
+ error: validationError,
82
+ path: filePath,
83
+ };
84
+ }
85
+
86
+ return {
87
+ success: true,
88
+ library,
89
+ path: filePath,
90
+ };
91
+ } catch (error) {
92
+ const errorMessage = error instanceof Error ? error.message : String(error);
93
+ return {
94
+ success: false,
95
+ error: `Failed to load library: ${errorMessage}`,
96
+ path: filePath,
97
+ };
98
+ }
99
+ }
100
+
101
+ /**
102
+ * Check if a library file exists in the .vgc/ folder
103
+ *
104
+ * @param baseDir - Base directory containing .vgc/ folder
105
+ * @returns True if a library file exists
106
+ */
107
+ hasLibrary(baseDir: string): boolean {
108
+ const configPath = this.fsAdapter.join(baseDir, CONFIG_DIR);
109
+
110
+ if (!this.fsAdapter.exists(configPath) || !this.fsAdapter.isDirectory(configPath)) {
111
+ return false;
112
+ }
113
+
114
+ return DEFAULT_LIBRARY_FILES.some((fileName) => this.fsAdapter.exists(this.fsAdapter.join(configPath, fileName)));
115
+ }
116
+
117
+ /**
118
+ * Get the path where the library file would be located
119
+ *
120
+ * @param baseDir - Base directory
121
+ * @param format - Preferred format ('yaml' or 'json')
122
+ * @returns Full path to the library file
123
+ */
124
+ getLibraryPath(baseDir: string, format: 'yaml' | 'json' = 'yaml'): string {
125
+ const fileName = format === 'json' ? 'library.json' : 'library.yaml';
126
+ return this.fsAdapter.join(baseDir, CONFIG_DIR, fileName);
127
+ }
128
+
129
+ /**
130
+ * Parse YAML content into a library object
131
+ */
132
+ private parseYaml(content: string, filePath: string): ComponentLibrary {
133
+ const data = yaml.load(content);
134
+
135
+ if (!data || typeof data !== 'object') {
136
+ throw new Error(`Empty or invalid YAML in ${filePath}`);
137
+ }
138
+
139
+ return data as ComponentLibrary;
140
+ }
141
+
142
+ /**
143
+ * Parse JSON content into a library object
144
+ */
145
+ private parseJson(content: string, filePath: string): ComponentLibrary {
146
+ try {
147
+ const data = JSON.parse(content);
148
+ return data as ComponentLibrary;
149
+ } catch (error) {
150
+ const errorMessage = error instanceof Error ? error.message : String(error);
151
+ throw new Error(`Invalid JSON in ${filePath}: ${errorMessage}`);
152
+ }
153
+ }
154
+
155
+ /**
156
+ * Validate the library structure
157
+ *
158
+ * @param library - Parsed library object
159
+ * @param filePath - File path for error messages
160
+ * @returns Error message if invalid, undefined if valid
161
+ */
162
+ private validate(library: ComponentLibrary, filePath: string): string | undefined {
163
+ if (!library.version) {
164
+ return `Missing required field 'version' in ${filePath}`;
165
+ }
166
+
167
+ if (!library.name) {
168
+ return `Missing required field 'name' in ${filePath}`;
169
+ }
170
+
171
+ if (!library.nodeComponents || typeof library.nodeComponents !== 'object') {
172
+ return `Missing or invalid 'nodeComponents' in ${filePath}`;
173
+ }
174
+
175
+ if (!library.edgeComponents || typeof library.edgeComponents !== 'object') {
176
+ return `Missing or invalid 'edgeComponents' in ${filePath}`;
177
+ }
178
+
179
+ // Validate node components
180
+ for (const [key, node] of Object.entries(library.nodeComponents)) {
181
+ if (!node.shape) {
182
+ return `Node component '${key}' is missing required field 'shape' in ${filePath}`;
183
+ }
184
+ }
185
+
186
+ // Validate edge components
187
+ for (const [key, edge] of Object.entries(library.edgeComponents)) {
188
+ if (!edge.style) {
189
+ return `Edge component '${key}' is missing required field 'style' in ${filePath}`;
190
+ }
191
+ }
192
+
193
+ // Validate connection rules if present
194
+ if (library.connectionRules) {
195
+ for (const [index, rule] of library.connectionRules.entries()) {
196
+ if (!rule.from || !rule.to || !rule.via) {
197
+ return `Connection rule at index ${index} is missing required fields (from, to, via) in ${filePath}`;
198
+ }
199
+
200
+ // Check that referenced types exist
201
+ if (!library.nodeComponents[rule.from]) {
202
+ return `Connection rule references unknown node type '${rule.from}' in ${filePath}`;
203
+ }
204
+ if (!library.nodeComponents[rule.to]) {
205
+ return `Connection rule references unknown node type '${rule.to}' in ${filePath}`;
206
+ }
207
+ if (!library.edgeComponents[rule.via]) {
208
+ return `Connection rule references unknown edge type '${rule.via}' in ${filePath}`;
209
+ }
210
+ }
211
+ }
212
+
213
+ return undefined;
214
+ }
215
+ }