eventmodeler 0.5.0 → 0.6.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 (150) hide show
  1. package/dist/index.js +6776 -2132
  2. package/package.json +11 -5
  3. package/dist/api/index.d.ts +0 -285
  4. package/dist/api/index.js +0 -323
  5. package/dist/cloud/slices/index.d.ts +0 -276
  6. package/dist/cloud/slices/index.js +0 -406
  7. package/dist/eventmodeler.js +0 -5646
  8. package/dist/formatters.d.ts +0 -17
  9. package/dist/formatters.js +0 -482
  10. package/dist/index.d.ts +0 -2
  11. package/dist/lib/auth.d.ts +0 -24
  12. package/dist/lib/auth.js +0 -331
  13. package/dist/lib/backend.d.ts +0 -43
  14. package/dist/lib/backend.js +0 -73
  15. package/dist/lib/chapter-utils.d.ts +0 -13
  16. package/dist/lib/chapter-utils.js +0 -71
  17. package/dist/lib/cloud-client.d.ts +0 -69
  18. package/dist/lib/cloud-client.js +0 -364
  19. package/dist/lib/config.d.ts +0 -30
  20. package/dist/lib/config.js +0 -95
  21. package/dist/lib/diff/merge-rules.d.ts +0 -45
  22. package/dist/lib/diff/merge-rules.js +0 -210
  23. package/dist/lib/diff/model-differ.d.ts +0 -8
  24. package/dist/lib/diff/model-differ.js +0 -568
  25. package/dist/lib/diff/three-way-merge.d.ts +0 -7
  26. package/dist/lib/diff/three-way-merge.js +0 -390
  27. package/dist/lib/diff/types.d.ts +0 -75
  28. package/dist/lib/diff/types.js +0 -1
  29. package/dist/lib/element-lookup.d.ts +0 -58
  30. package/dist/lib/element-lookup.js +0 -126
  31. package/dist/lib/file-loader.d.ts +0 -8
  32. package/dist/lib/file-loader.js +0 -108
  33. package/dist/lib/flow-utils.d.ts +0 -53
  34. package/dist/lib/flow-utils.js +0 -348
  35. package/dist/lib/format.d.ts +0 -10
  36. package/dist/lib/format.js +0 -23
  37. package/dist/lib/project-config.d.ts +0 -27
  38. package/dist/lib/project-config.js +0 -83
  39. package/dist/lib/slice-utils.d.ts +0 -59
  40. package/dist/lib/slice-utils.js +0 -140
  41. package/dist/local/slices/index.d.ts +0 -11
  42. package/dist/local/slices/index.js +0 -13
  43. package/dist/projection.d.ts +0 -3
  44. package/dist/projection.js +0 -828
  45. package/dist/slices/add-field/index.d.ts +0 -8
  46. package/dist/slices/add-field/index.js +0 -211
  47. package/dist/slices/add-scenario/index.d.ts +0 -27
  48. package/dist/slices/add-scenario/index.js +0 -307
  49. package/dist/slices/codegen-chapter-events/index.d.ts +0 -2
  50. package/dist/slices/codegen-chapter-events/index.js +0 -145
  51. package/dist/slices/codegen-slice/index.d.ts +0 -2
  52. package/dist/slices/codegen-slice/index.js +0 -448
  53. package/dist/slices/create-automation-slice/index.d.ts +0 -2
  54. package/dist/slices/create-automation-slice/index.js +0 -304
  55. package/dist/slices/create-flow/index.d.ts +0 -2
  56. package/dist/slices/create-flow/index.js +0 -183
  57. package/dist/slices/create-state-change-slice/index.d.ts +0 -2
  58. package/dist/slices/create-state-change-slice/index.js +0 -263
  59. package/dist/slices/create-state-view-slice/index.d.ts +0 -2
  60. package/dist/slices/create-state-view-slice/index.js +0 -128
  61. package/dist/slices/diff/index.d.ts +0 -11
  62. package/dist/slices/diff/index.js +0 -293
  63. package/dist/slices/export-eventmodel-to-json/index.d.ts +0 -2
  64. package/dist/slices/export-eventmodel-to-json/index.js +0 -355
  65. package/dist/slices/git/index.d.ts +0 -2
  66. package/dist/slices/git/index.js +0 -125
  67. package/dist/slices/guide/guides/codegen.d.ts +0 -5
  68. package/dist/slices/guide/guides/codegen.js +0 -339
  69. package/dist/slices/guide/guides/connect-slices.d.ts +0 -5
  70. package/dist/slices/guide/guides/connect-slices.js +0 -202
  71. package/dist/slices/guide/guides/create-slices.d.ts +0 -5
  72. package/dist/slices/guide/guides/create-slices.js +0 -303
  73. package/dist/slices/guide/guides/explore.d.ts +0 -5
  74. package/dist/slices/guide/guides/explore.js +0 -251
  75. package/dist/slices/guide/guides/information-flow.d.ts +0 -5
  76. package/dist/slices/guide/guides/information-flow.js +0 -318
  77. package/dist/slices/guide/guides/scenarios.d.ts +0 -5
  78. package/dist/slices/guide/guides/scenarios.js +0 -269
  79. package/dist/slices/guide/index.d.ts +0 -1
  80. package/dist/slices/guide/index.js +0 -40
  81. package/dist/slices/import/index.d.ts +0 -8
  82. package/dist/slices/import/index.js +0 -63
  83. package/dist/slices/init/index.d.ts +0 -5
  84. package/dist/slices/init/index.js +0 -80
  85. package/dist/slices/list-chapters/index.d.ts +0 -3
  86. package/dist/slices/list-chapters/index.js +0 -21
  87. package/dist/slices/list-commands/index.d.ts +0 -3
  88. package/dist/slices/list-commands/index.js +0 -20
  89. package/dist/slices/list-events/index.d.ts +0 -3
  90. package/dist/slices/list-events/index.js +0 -98
  91. package/dist/slices/list-processors/index.d.ts +0 -3
  92. package/dist/slices/list-processors/index.js +0 -20
  93. package/dist/slices/list-readmodels/index.d.ts +0 -3
  94. package/dist/slices/list-readmodels/index.js +0 -21
  95. package/dist/slices/list-scenarios/index.d.ts +0 -3
  96. package/dist/slices/list-scenarios/index.js +0 -35
  97. package/dist/slices/list-screens/index.d.ts +0 -3
  98. package/dist/slices/list-screens/index.js +0 -47
  99. package/dist/slices/list-slices/index.d.ts +0 -3
  100. package/dist/slices/list-slices/index.js +0 -35
  101. package/dist/slices/login/index.d.ts +0 -1
  102. package/dist/slices/login/index.js +0 -20
  103. package/dist/slices/logout/index.d.ts +0 -1
  104. package/dist/slices/logout/index.js +0 -14
  105. package/dist/slices/map-fields/index.d.ts +0 -2
  106. package/dist/slices/map-fields/index.js +0 -269
  107. package/dist/slices/mark-slice-status/index.d.ts +0 -2
  108. package/dist/slices/mark-slice-status/index.js +0 -31
  109. package/dist/slices/merge/index.d.ts +0 -19
  110. package/dist/slices/merge/index.js +0 -147
  111. package/dist/slices/open-app/index.d.ts +0 -1
  112. package/dist/slices/open-app/index.js +0 -36
  113. package/dist/slices/remove-field/index.d.ts +0 -8
  114. package/dist/slices/remove-field/index.js +0 -167
  115. package/dist/slices/remove-scenario/index.d.ts +0 -2
  116. package/dist/slices/remove-scenario/index.js +0 -77
  117. package/dist/slices/search/index.d.ts +0 -3
  118. package/dist/slices/search/index.js +0 -302
  119. package/dist/slices/show-actor/index.d.ts +0 -4
  120. package/dist/slices/show-actor/index.js +0 -115
  121. package/dist/slices/show-aggregate/index.d.ts +0 -3
  122. package/dist/slices/show-aggregate/index.js +0 -108
  123. package/dist/slices/show-aggregate-completeness/index.d.ts +0 -4
  124. package/dist/slices/show-aggregate-completeness/index.js +0 -181
  125. package/dist/slices/show-chapter/index.d.ts +0 -3
  126. package/dist/slices/show-chapter/index.js +0 -195
  127. package/dist/slices/show-command/index.d.ts +0 -3
  128. package/dist/slices/show-command/index.js +0 -133
  129. package/dist/slices/show-completeness/index.d.ts +0 -4
  130. package/dist/slices/show-completeness/index.js +0 -731
  131. package/dist/slices/show-event/index.d.ts +0 -3
  132. package/dist/slices/show-event/index.js +0 -118
  133. package/dist/slices/show-model-summary/index.d.ts +0 -3
  134. package/dist/slices/show-model-summary/index.js +0 -31
  135. package/dist/slices/show-processor/index.d.ts +0 -3
  136. package/dist/slices/show-processor/index.js +0 -111
  137. package/dist/slices/show-readmodel/index.d.ts +0 -3
  138. package/dist/slices/show-readmodel/index.js +0 -158
  139. package/dist/slices/show-scenario/index.d.ts +0 -3
  140. package/dist/slices/show-scenario/index.js +0 -196
  141. package/dist/slices/show-screen/index.d.ts +0 -3
  142. package/dist/slices/show-screen/index.js +0 -139
  143. package/dist/slices/show-slice/index.d.ts +0 -3
  144. package/dist/slices/show-slice/index.js +0 -696
  145. package/dist/slices/update-field/index.d.ts +0 -15
  146. package/dist/slices/update-field/index.js +0 -208
  147. package/dist/slices/whoami/index.d.ts +0 -2
  148. package/dist/slices/whoami/index.js +0 -44
  149. package/dist/types.d.ts +0 -195
  150. package/dist/types.js +0 -1
@@ -1,276 +0,0 @@
1
- export declare function listSlices(modelId: string, format?: OutputFormat, chapterFilter?: string): Promise<string>;
2
- export declare function listEvents(modelId: string, format?: OutputFormat): Promise<string>;
3
- export declare function listCommands(modelId: string, format?: OutputFormat): Promise<string>;
4
- export declare function listChapters(modelId: string, format?: OutputFormat): Promise<string>;
5
- export declare function listActors(modelId: string, format?: OutputFormat): Promise<string>;
6
- export interface SearchResult {
7
- type: string;
8
- id: string;
9
- name: string;
10
- chapterId?: string;
11
- chapterName?: string;
12
- sliceId?: string;
13
- sliceName?: string;
14
- sliceStatus?: string;
15
- parentChapterId?: string;
16
- parentChapterName?: string;
17
- }
18
- export interface SearchResponse {
19
- query: string;
20
- count: number;
21
- results: SearchResult[];
22
- }
23
- export declare function search(modelId: string, term: string): Promise<SearchResponse>;
24
- export interface ModelSummary {
25
- modelName: string;
26
- slices: {
27
- total: number;
28
- byStatus: {
29
- created: number;
30
- inProgress: number;
31
- blocked: number;
32
- done: number;
33
- };
34
- byType: {
35
- stateChange: number;
36
- automation: number;
37
- stateView: number;
38
- unknown: number;
39
- };
40
- };
41
- events: number;
42
- commands: number;
43
- readModels: number;
44
- screens: number;
45
- processors: number;
46
- chapters: number;
47
- aggregates: number;
48
- actors: number;
49
- flows: number;
50
- scenarios: number;
51
- }
52
- export declare function getSummary(modelId: string): Promise<ModelSummary>;
53
- export type SliceStatus = 'created' | 'in-progress' | 'blocked' | 'done';
54
- export declare function markSliceStatus(modelId: string, sliceName: string, status: SliceStatus): Promise<{
55
- success: boolean;
56
- }>;
57
- export type ElementType = 'command' | 'event' | 'readModel' | 'screen' | 'processor';
58
- export interface FieldInput {
59
- name: string;
60
- fieldType: string;
61
- isList?: boolean;
62
- isOptional?: boolean;
63
- isGenerated?: boolean;
64
- isUserInput?: boolean;
65
- subfields?: FieldInput[];
66
- }
67
- export declare function addField(modelId: string, elementType: ElementType, elementName: string, field: FieldInput): Promise<{
68
- success: boolean;
69
- }>;
70
- export declare function removeField(modelId: string, elementType: ElementType, elementName: string, fieldName: string): Promise<{
71
- success: boolean;
72
- }>;
73
- export interface FieldUpdates {
74
- name?: string;
75
- fieldType?: string;
76
- isList?: boolean;
77
- isOptional?: boolean;
78
- isGenerated?: boolean;
79
- isUserInput?: boolean;
80
- }
81
- export declare function updateField(modelId: string, elementType: ElementType, elementName: string, fieldName: string, updates: FieldUpdates): Promise<{
82
- success: boolean;
83
- }>;
84
- export interface FieldMappingInput {
85
- from: string;
86
- to: string;
87
- }
88
- export declare function mapFields(modelId: string, sourceName: string, targetName: string, mappings: FieldMappingInput[]): Promise<{
89
- success: boolean;
90
- }>;
91
- export interface ScenarioInput {
92
- name: string;
93
- description?: string;
94
- given?: Array<{
95
- event: string;
96
- fieldValues?: Record<string, unknown>;
97
- }>;
98
- when?: {
99
- command?: {
100
- command: string;
101
- fieldValues?: Record<string, unknown>;
102
- };
103
- events?: Array<{
104
- event: string;
105
- fieldValues?: Record<string, unknown>;
106
- }>;
107
- };
108
- then: {
109
- type: 'events' | 'error' | 'command' | 'readModelAssertion' | 'noCommand';
110
- events?: Array<{
111
- event: string;
112
- fieldValues?: Record<string, unknown>;
113
- }>;
114
- errorMessage?: string;
115
- errorType?: string;
116
- command?: {
117
- command: string;
118
- fieldValues?: Record<string, unknown>;
119
- };
120
- readModelAssertion?: {
121
- readModel: string;
122
- givenEvents?: Array<{
123
- event: string;
124
- fieldValues?: Record<string, unknown>;
125
- }>;
126
- expectedFieldValues?: Record<string, unknown>;
127
- };
128
- };
129
- }
130
- export declare function createScenario(modelId: string, sliceName: string, scenario: ScenarioInput): Promise<{
131
- success: boolean;
132
- scenarioId: string;
133
- }>;
134
- export declare function removeScenario(modelId: string, scenarioName: string, sliceName?: string): Promise<{
135
- success: boolean;
136
- }>;
137
- export declare function createFlow(modelId: string, fromName: string, toName: string): Promise<{
138
- success: boolean;
139
- }>;
140
- export interface CompoundFieldInput {
141
- name: string;
142
- fieldType: string;
143
- isList?: boolean;
144
- isOptional?: boolean;
145
- isGenerated?: boolean;
146
- isUserInput?: boolean;
147
- subfields?: CompoundFieldInput[];
148
- }
149
- export interface StateChangeSliceInput {
150
- sliceName: string;
151
- after?: string;
152
- before?: string;
153
- screen: {
154
- name: string;
155
- fields: CompoundFieldInput[];
156
- };
157
- command: {
158
- name: string;
159
- fields: CompoundFieldInput[];
160
- };
161
- event: {
162
- name: string;
163
- fields: CompoundFieldInput[];
164
- };
165
- }
166
- export interface StateChangeSliceResult {
167
- success: boolean;
168
- sliceId: string;
169
- screenId: string;
170
- commandId: string;
171
- eventId: string;
172
- screenToCommandMappings: number;
173
- commandToEventMappings: number;
174
- }
175
- export declare function createStateChangeSlice(modelId: string, input: StateChangeSliceInput): Promise<StateChangeSliceResult>;
176
- export interface AutomationSliceInput {
177
- sliceName: string;
178
- after?: string;
179
- before?: string;
180
- readModel: {
181
- name: string;
182
- fields: CompoundFieldInput[];
183
- };
184
- processor: {
185
- name: string;
186
- };
187
- command: {
188
- name: string;
189
- fields: CompoundFieldInput[];
190
- };
191
- event: {
192
- name: string;
193
- fields: CompoundFieldInput[];
194
- };
195
- }
196
- export interface AutomationSliceResult {
197
- success: boolean;
198
- sliceId: string;
199
- readModelId: string;
200
- processorId: string;
201
- commandId: string;
202
- eventId: string;
203
- readModelToCommandMappings: number;
204
- commandToEventMappings: number;
205
- }
206
- export declare function createAutomationSlice(modelId: string, input: AutomationSliceInput): Promise<AutomationSliceResult>;
207
- export interface StateViewSliceInput {
208
- sliceName: string;
209
- after?: string;
210
- before?: string;
211
- readModel: {
212
- name: string;
213
- fields: CompoundFieldInput[];
214
- };
215
- }
216
- export interface StateViewSliceResult {
217
- success: boolean;
218
- sliceId: string;
219
- readModelId: string;
220
- }
221
- export declare function createStateViewSlice(modelId: string, input: StateViewSliceInput): Promise<StateViewSliceResult>;
222
- export type OutputFormat = 'xml' | 'json';
223
- export declare function showSlice(modelId: string, sliceName: string, format?: OutputFormat): Promise<string>;
224
- export declare function showEvent(modelId: string, eventName: string, format?: OutputFormat): Promise<string>;
225
- export declare function showCommand(modelId: string, commandName: string, format?: OutputFormat): Promise<string>;
226
- export declare function showChapter(modelId: string, chapterName: string, format?: OutputFormat): Promise<string>;
227
- export declare function showActor(modelId: string, actorName: string, format?: OutputFormat): Promise<string>;
228
- export declare function exportJson(modelId: string): Promise<string>;
229
- export declare function codegenSlice(modelId: string, sliceName: string): Promise<string>;
230
- export interface CodegenChapterEventsField {
231
- name: string;
232
- type: string;
233
- list?: boolean;
234
- generated?: boolean;
235
- optional?: boolean;
236
- userInput?: boolean;
237
- subfields?: CodegenChapterEventsField[];
238
- }
239
- export interface CodegenChapterEventsChapter {
240
- id: string;
241
- name: string;
242
- parent?: CodegenChapterEventsChapter | null;
243
- }
244
- export interface CodegenChapterEventsSlice {
245
- id: string;
246
- name: string;
247
- sliceType: string;
248
- }
249
- export interface CodegenChapterEventsEvent {
250
- id: string;
251
- name: string;
252
- fields: CodegenChapterEventsField[];
253
- aggregate?: string;
254
- sourceSlices: string[];
255
- }
256
- export interface CodegenChapterEventsResponse {
257
- chapter: CodegenChapterEventsChapter | null;
258
- sliceCount: number;
259
- eventCount: number;
260
- slices: CodegenChapterEventsSlice[];
261
- events: CodegenChapterEventsEvent[];
262
- }
263
- export declare function codegenEvents(modelId: string, chapterName?: string): Promise<CodegenChapterEventsResponse>;
264
- export declare function listAggregates(modelId: string, format?: OutputFormat): Promise<string>;
265
- export declare function listReadModels(modelId: string, format?: OutputFormat): Promise<string>;
266
- export declare function listScreens(modelId: string, format?: OutputFormat): Promise<string>;
267
- export declare function listProcessors(modelId: string, format?: OutputFormat): Promise<string>;
268
- export declare function showCompleteness(modelId: string, elementName: string, format?: OutputFormat): Promise<string>;
269
- export declare function showModelCompleteness(modelId: string, format?: OutputFormat): Promise<string>;
270
- export declare function showAggregateCompleteness(modelId: string, aggregateName: string, format?: OutputFormat): Promise<string>;
271
- export declare function showReadModel(modelId: string, name: string, format?: OutputFormat): Promise<string>;
272
- export declare function showScreen(modelId: string, name: string, format?: OutputFormat): Promise<string>;
273
- export declare function showProcessor(modelId: string, name: string, format?: OutputFormat): Promise<string>;
274
- export declare function showAggregate(modelId: string, name: string, format?: OutputFormat): Promise<string>;
275
- export declare function showScenario(modelId: string, name: string, format?: OutputFormat): Promise<string>;
276
- export declare function listScenarios(modelId: string, format?: OutputFormat): Promise<string>;
@@ -1,406 +0,0 @@
1
- // Cloud slice implementations - thin wrappers that call backend CLI endpoints
2
- // The backend does all the heavy lifting (name resolution, data denormalization)
3
- import { getBackendUrl } from '../../lib/config.js';
4
- import { getValidAccessToken } from '../../lib/auth.js';
5
- // =============================================================================
6
- // HTTP Client for CLI endpoints
7
- // =============================================================================
8
- /**
9
- * Handle HTTP error responses with user-friendly messages.
10
- * Tries to extract error detail from response body, falls back to status-based messages.
11
- */
12
- async function handleHttpError(response, path) {
13
- const text = await response.text().catch(() => '');
14
- const errorDetail = extractErrorDetail(text);
15
- switch (response.status) {
16
- case 401:
17
- throw new Error(`Authentication required. Run 'eventmodeler login' to authenticate.` +
18
- (errorDetail ? `\n\nOriginal error: ${errorDetail}` : ''));
19
- case 403:
20
- throw new Error(`Permission denied. You may not have access to this model, or you need editor permission for this action.` +
21
- (errorDetail ? `\n\nOriginal error: ${errorDetail}` : ''));
22
- case 404:
23
- throw new Error(errorDetail || `Not found: ${path}`);
24
- default:
25
- throw new Error(errorDetail || `HTTP ${response.status}: ${response.statusText}`);
26
- }
27
- }
28
- const GENERIC_ERROR_MESSAGES = new Set([
29
- 'internal server error',
30
- 'bad request',
31
- 'unauthorized',
32
- 'forbidden',
33
- 'not found',
34
- ]);
35
- function isGenericErrorMessage(value) {
36
- return GENERIC_ERROR_MESSAGES.has(value.trim().toLowerCase());
37
- }
38
- function toStringValue(value) {
39
- return typeof value === 'string' ? value.trim() : '';
40
- }
41
- function collectCandidateMessages(node, out) {
42
- if (!node || typeof node !== 'object')
43
- return;
44
- const obj = node;
45
- // Prefer richer fields before generic "error".
46
- for (const key of ['message', 'detail', 'reason', 'description', 'title', 'error']) {
47
- const value = toStringValue(obj[key]);
48
- if (value)
49
- out.push(value);
50
- }
51
- const errors = obj.errors;
52
- if (Array.isArray(errors)) {
53
- for (const entry of errors) {
54
- if (typeof entry === 'string') {
55
- const value = entry.trim();
56
- if (value)
57
- out.push(value);
58
- }
59
- else if (entry && typeof entry === 'object') {
60
- collectCandidateMessages(entry, out);
61
- }
62
- }
63
- }
64
- const cause = obj.cause;
65
- if (cause && typeof cause === 'object') {
66
- collectCandidateMessages(cause, out);
67
- }
68
- }
69
- function extractErrorDetail(text) {
70
- const body = text.trim();
71
- if (!body)
72
- return '';
73
- try {
74
- const parsed = JSON.parse(body);
75
- if (typeof parsed === 'string') {
76
- return parsed.trim();
77
- }
78
- const candidates = [];
79
- collectCandidateMessages(parsed, candidates);
80
- if (candidates.length > 0) {
81
- const specific = candidates.find((value) => !isGenericErrorMessage(value));
82
- return specific ?? candidates[0];
83
- }
84
- }
85
- catch {
86
- // Not JSON. Fall back to plain text below.
87
- }
88
- // Avoid dumping entire HTML pages from reverse proxies.
89
- if (body.startsWith('<')) {
90
- const title = body.match(/<title>([^<]+)<\/title>/i)?.[1]?.trim();
91
- return title && !isGenericErrorMessage(title) ? title : '';
92
- }
93
- return body;
94
- }
95
- async function cliGet(path, params) {
96
- const backendUrl = getBackendUrl();
97
- const accessToken = await getValidAccessToken();
98
- // Build query string manually using encodeURIComponent (uses %20 for spaces, not +)
99
- const queryParts = Object.entries(params)
100
- .filter(([_, value]) => value !== undefined)
101
- .map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`);
102
- const queryString = queryParts.length > 0 ? `?${queryParts.join('&')}` : '';
103
- const fullUrl = `${backendUrl}${path}${queryString}`;
104
- const response = await fetch(fullUrl, {
105
- method: 'GET',
106
- headers: {
107
- Accept: 'application/json',
108
- ...(accessToken ? { Authorization: `Bearer ${accessToken}` } : {}),
109
- },
110
- });
111
- if (!response.ok) {
112
- await handleHttpError(response, path);
113
- }
114
- const text = await response.text();
115
- if (!text.trim()) {
116
- throw new Error(`Empty response from ${path}`);
117
- }
118
- try {
119
- return JSON.parse(text);
120
- }
121
- catch (error) {
122
- const message = error instanceof Error ? error.message : String(error);
123
- const snippet = text.slice(0, 200);
124
- throw new Error(`Invalid JSON from ${path}: ${message}. Body: ${snippet}`);
125
- }
126
- }
127
- async function cliPost(path, body) {
128
- const backendUrl = getBackendUrl();
129
- const accessToken = await getValidAccessToken();
130
- const response = await fetch(`${backendUrl}${path}`, {
131
- method: 'POST',
132
- headers: {
133
- 'Content-Type': 'application/json',
134
- Accept: 'application/json',
135
- ...(accessToken ? { Authorization: `Bearer ${accessToken}` } : {}),
136
- },
137
- body: JSON.stringify(body),
138
- });
139
- if (!response.ok) {
140
- await handleHttpError(response, path);
141
- }
142
- const text = await response.text();
143
- if (!text.trim()) {
144
- throw new Error(`Empty response from ${path}`);
145
- }
146
- try {
147
- return JSON.parse(text);
148
- }
149
- catch (error) {
150
- const message = error instanceof Error ? error.message : String(error);
151
- const snippet = text.slice(0, 200);
152
- throw new Error(`Invalid JSON from ${path}: ${message}. Body: ${snippet}`);
153
- }
154
- }
155
- // =============================================================================
156
- // Query Functions (read-only, return denormalized data)
157
- // =============================================================================
158
- export async function listSlices(modelId, format = 'xml', chapterFilter) {
159
- const params = { modelId };
160
- if (chapterFilter)
161
- params.chapter = chapterFilter;
162
- return cliGetText('/cli/list-slices', params, format);
163
- }
164
- export async function listEvents(modelId, format = 'xml') {
165
- return cliGetText('/cli/list-events', { modelId }, format);
166
- }
167
- export async function listCommands(modelId, format = 'xml') {
168
- return cliGetText('/cli/list-commands', { modelId }, format);
169
- }
170
- export async function listChapters(modelId, format = 'xml') {
171
- return cliGetText('/cli/list-chapters', { modelId }, format);
172
- }
173
- export async function listActors(modelId, format = 'xml') {
174
- return cliGetText('/cli/list-actors', { modelId }, format);
175
- }
176
- export async function search(modelId, term) {
177
- return cliGet('/cli/search', { modelId, term });
178
- }
179
- export async function getSummary(modelId) {
180
- return cliGet('/cli/summary', { modelId });
181
- }
182
- export async function markSliceStatus(modelId, sliceName, status) {
183
- return cliPost('/cli/mark-slice-status', { modelId, sliceName, status });
184
- }
185
- export async function addField(modelId, elementType, elementName, field) {
186
- return cliPost('/cli/add-field', { modelId, elementType, elementName, field });
187
- }
188
- export async function removeField(modelId, elementType, elementName, fieldName) {
189
- return cliPost('/cli/remove-field', { modelId, elementType, elementName, fieldName });
190
- }
191
- export async function updateField(modelId, elementType, elementName, fieldName, updates) {
192
- return cliPost('/cli/update-field', { modelId, elementType, elementName, fieldName, updates });
193
- }
194
- export async function mapFields(modelId, sourceName, targetName, mappings) {
195
- return cliPost('/cli/map-fields', { modelId, sourceName, targetName, mappings });
196
- }
197
- export async function createScenario(modelId, sliceName, scenario) {
198
- return cliPost('/cli/create-scenario', { modelId, sliceName, scenario });
199
- }
200
- export async function removeScenario(modelId, scenarioName, sliceName) {
201
- return cliPost('/cli/remove-scenario', { modelId, scenarioName, sliceName });
202
- }
203
- export async function createFlow(modelId, fromName, toName) {
204
- return cliPost('/cli/create-flow', { modelId, fromName, toName });
205
- }
206
- export async function createStateChangeSlice(modelId, input) {
207
- return cliPost('/cli/create-state-change-slice', { modelId, ...input });
208
- }
209
- export async function createAutomationSlice(modelId, input) {
210
- return cliPost('/cli/create-automation-slice', { modelId, ...input });
211
- }
212
- export async function createStateViewSlice(modelId, input) {
213
- return cliPost('/cli/create-state-view-slice', { modelId, ...input });
214
- }
215
- // =============================================================================
216
- // View Functions (read-only, return formatted XML/JSON strings)
217
- // =============================================================================
218
- // Maps format to Accept header media type for content negotiation with the backend
219
- function formatToMediaType(format) {
220
- return format === 'xml' ? 'application/xml' : 'application/json';
221
- }
222
- // Helper for view queries that return text (XML or JSON string).
223
- // Uses Accept header for content negotiation instead of a format query param.
224
- async function cliGetText(path, params, format) {
225
- const backendUrl = getBackendUrl();
226
- const accessToken = await getValidAccessToken();
227
- // Build query string manually using encodeURIComponent (uses %20 for spaces, not +)
228
- // Some servers don't decode + as space in query parameters
229
- const queryParts = Object.entries(params)
230
- .filter(([_, value]) => value !== undefined)
231
- .map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`);
232
- const queryString = queryParts.length > 0 ? `?${queryParts.join('&')}` : '';
233
- const fullUrl = `${backendUrl}${path}${queryString}`;
234
- const response = await fetch(fullUrl, {
235
- method: 'GET',
236
- headers: {
237
- Accept: formatToMediaType(format),
238
- ...(accessToken ? { Authorization: `Bearer ${accessToken}` } : {}),
239
- },
240
- });
241
- if (!response.ok) {
242
- await handleHttpError(response, path);
243
- }
244
- const text = await response.text();
245
- return text.trimEnd();
246
- }
247
- export async function showSlice(modelId, sliceName, format = 'xml') {
248
- return cliGetText('/cli/show-slice', { modelId, name: sliceName }, format);
249
- }
250
- export async function showEvent(modelId, eventName, format = 'xml') {
251
- return cliGetText('/cli/show-event', { modelId, name: eventName }, format);
252
- }
253
- export async function showCommand(modelId, commandName, format = 'xml') {
254
- return cliGetText('/cli/show-command', { modelId, name: commandName }, format);
255
- }
256
- export async function showChapter(modelId, chapterName, format = 'xml') {
257
- return cliGetText('/cli/show-chapter', { modelId, name: chapterName }, format);
258
- }
259
- export async function showActor(modelId, actorName, format = 'xml') {
260
- return cliGetText('/cli/show-actor', { modelId, name: actorName }, format);
261
- }
262
- export async function exportJson(modelId) {
263
- return cliGetText('/cli/export-json', { modelId });
264
- }
265
- export async function codegenSlice(modelId, sliceName) {
266
- return cliGetText('/cli/codegen-slice', { modelId, name: sliceName });
267
- }
268
- function countFieldsDeep(fields) {
269
- if (!fields || fields.length === 0) {
270
- return 0;
271
- }
272
- return fields.reduce((count, field) => count + 1 + countFieldsDeep(field.subfields), 0);
273
- }
274
- function normalizeName(name) {
275
- return (name ?? '').trim().toLowerCase();
276
- }
277
- export async function codegenEvents(modelId, chapterName) {
278
- const slicesOutput = await listSlices(modelId, 'json', chapterName);
279
- let slicesPayload;
280
- try {
281
- slicesPayload = JSON.parse(slicesOutput);
282
- }
283
- catch (error) {
284
- const message = error instanceof Error ? error.message : String(error);
285
- throw new Error(`Invalid JSON from /cli/list-slices: ${message}`);
286
- }
287
- const slices = (slicesPayload.slices ?? [])
288
- .filter((slice) => typeof slice.id === 'string' && typeof slice.name === 'string');
289
- if (slices.length === 0) {
290
- return {
291
- chapter: null,
292
- sliceCount: 0,
293
- eventCount: 0,
294
- slices: [],
295
- events: [],
296
- };
297
- }
298
- const codegenOutputs = await Promise.all(slices.map(async (slice) => {
299
- const raw = await codegenSlice(modelId, slice.name);
300
- try {
301
- return JSON.parse(raw);
302
- }
303
- catch (error) {
304
- const message = error instanceof Error ? error.message : String(error);
305
- throw new Error(`Invalid JSON from /cli/codegen-slice for "${slice.name}": ${message}`);
306
- }
307
- }));
308
- const aggregatedSlices = [];
309
- const eventsByName = new Map();
310
- let chapter = null;
311
- for (let i = 0; i < codegenOutputs.length; i++) {
312
- const codegenOutput = codegenOutputs[i];
313
- const fallbackSlice = slices[i];
314
- const sliceName = codegenOutput.slice?.name ?? fallbackSlice.name;
315
- aggregatedSlices.push({
316
- id: codegenOutput.slice?.id ?? fallbackSlice.id,
317
- name: sliceName,
318
- sliceType: codegenOutput.sliceType ?? 'STATE_CHANGE',
319
- });
320
- if (chapterName && !chapter && codegenOutput.chapter) {
321
- chapter = codegenOutput.chapter;
322
- }
323
- for (const event of codegenOutput.elements?.events ?? []) {
324
- if (!event.name) {
325
- continue;
326
- }
327
- const key = normalizeName(event.name);
328
- if (!key) {
329
- continue;
330
- }
331
- const existing = eventsByName.get(key);
332
- if (!existing) {
333
- eventsByName.set(key, {
334
- id: event.id ?? '',
335
- name: event.name,
336
- fields: event.fields ?? [],
337
- aggregate: event.aggregate,
338
- sourceSlices: [sliceName],
339
- });
340
- continue;
341
- }
342
- if (!existing.sourceSlices.includes(sliceName)) {
343
- existing.sourceSlices.push(sliceName);
344
- }
345
- if (!existing.aggregate && event.aggregate) {
346
- existing.aggregate = event.aggregate;
347
- }
348
- if (countFieldsDeep(event.fields) > countFieldsDeep(existing.fields)) {
349
- existing.fields = event.fields ?? [];
350
- }
351
- if (!existing.id && event.id) {
352
- existing.id = event.id;
353
- }
354
- }
355
- }
356
- const events = [...eventsByName.values()]
357
- .map(event => ({ ...event, sourceSlices: [...event.sourceSlices].sort((a, b) => a.localeCompare(b)) }))
358
- .sort((a, b) => a.name.localeCompare(b.name));
359
- const sortedSlices = [...aggregatedSlices].sort((a, b) => a.name.localeCompare(b.name));
360
- return {
361
- chapter,
362
- sliceCount: sortedSlices.length,
363
- eventCount: events.length,
364
- slices: sortedSlices,
365
- events,
366
- };
367
- }
368
- export async function listAggregates(modelId, format = 'xml') {
369
- return cliGetText('/cli/list-aggregates', { modelId }, format);
370
- }
371
- export async function listReadModels(modelId, format = 'xml') {
372
- return cliGetText('/cli/list-readmodels', { modelId }, format);
373
- }
374
- export async function listScreens(modelId, format = 'xml') {
375
- return cliGetText('/cli/list-screens', { modelId }, format);
376
- }
377
- export async function listProcessors(modelId, format = 'xml') {
378
- return cliGetText('/cli/list-processors', { modelId }, format);
379
- }
380
- export async function showCompleteness(modelId, elementName, format = 'xml') {
381
- return cliGetText('/cli/show-completeness', { modelId, name: elementName }, format);
382
- }
383
- export async function showModelCompleteness(modelId, format = 'xml') {
384
- return cliGetText('/cli/show-model-completeness', { modelId }, format);
385
- }
386
- export async function showAggregateCompleteness(modelId, aggregateName, format = 'xml') {
387
- return cliGetText('/cli/show-aggregate-completeness', { modelId, name: aggregateName }, format);
388
- }
389
- export async function showReadModel(modelId, name, format = 'xml') {
390
- return cliGetText('/cli/show-readmodel', { modelId, name }, format);
391
- }
392
- export async function showScreen(modelId, name, format = 'xml') {
393
- return cliGetText('/cli/show-screen', { modelId, name }, format);
394
- }
395
- export async function showProcessor(modelId, name, format = 'xml') {
396
- return cliGetText('/cli/show-processor', { modelId, name }, format);
397
- }
398
- export async function showAggregate(modelId, name, format = 'xml') {
399
- return cliGetText('/cli/show-aggregate', { modelId, name }, format);
400
- }
401
- export async function showScenario(modelId, name, format = 'xml') {
402
- return cliGetText('/cli/show-scenario', { modelId, name }, format);
403
- }
404
- export async function listScenarios(modelId, format = 'xml') {
405
- return cliGetText('/cli/list-scenarios', { modelId }, format);
406
- }