@contractspec/lib.example-shared-ui 6.0.17 → 6.0.18

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 (84) hide show
  1. package/.turbo/turbo-build.log +81 -81
  2. package/CHANGELOG.md +17 -0
  3. package/dist/EvolutionDashboard.js +1 -803
  4. package/dist/EvolutionSidebar.js +1 -531
  5. package/dist/LocalDataIndicator.js +1 -68
  6. package/dist/MarkdownView.js +1 -213
  7. package/dist/OverlayContextProvider.js +1 -202
  8. package/dist/PersonalizationInsights.js +1 -455
  9. package/dist/SaveToStudioButton.js +1 -79
  10. package/dist/SpecDrivenTemplateShell.js +1 -203
  11. package/dist/SpecEditorPanel.js +17 -364
  12. package/dist/TemplateShell.js +1 -195
  13. package/dist/browser/EvolutionDashboard.js +1 -803
  14. package/dist/browser/EvolutionSidebar.js +1 -531
  15. package/dist/browser/LocalDataIndicator.js +1 -68
  16. package/dist/browser/MarkdownView.js +1 -213
  17. package/dist/browser/OverlayContextProvider.js +1 -202
  18. package/dist/browser/PersonalizationInsights.js +1 -455
  19. package/dist/browser/SaveToStudioButton.js +1 -79
  20. package/dist/browser/SpecDrivenTemplateShell.js +1 -203
  21. package/dist/browser/SpecEditorPanel.js +17 -364
  22. package/dist/browser/TemplateShell.js +1 -195
  23. package/dist/browser/bundles/ExampleTemplateBundle.js +1 -85
  24. package/dist/browser/bundles/index.js +1 -85
  25. package/dist/browser/hooks/index.js +40 -1151
  26. package/dist/browser/hooks/useBehaviorTracking.js +1 -157
  27. package/dist/browser/hooks/useEvolution.js +1 -260
  28. package/dist/browser/hooks/useRegistryTemplates.js +1 -31
  29. package/dist/browser/hooks/useSpecContent.js +17 -224
  30. package/dist/browser/hooks/useWorkflowComposer.js +24 -483
  31. package/dist/browser/index.js +40 -3122
  32. package/dist/browser/lib/component-registry.js +1 -48
  33. package/dist/browser/lib/runtime-context.js +1 -21
  34. package/dist/browser/markdown/formatPresentationName.js +1 -9
  35. package/dist/browser/markdown/useMarkdownPresentation.js +1 -65
  36. package/dist/browser/utils/fetchPresentationData.js +1 -15
  37. package/dist/browser/utils/generateSpecFromTemplate.js +16 -62
  38. package/dist/browser/utils/index.js +16 -76
  39. package/dist/bundles/ExampleTemplateBundle.js +1 -85
  40. package/dist/bundles/index.js +1 -85
  41. package/dist/hooks/index.js +40 -1151
  42. package/dist/hooks/useBehaviorTracking.js +1 -157
  43. package/dist/hooks/useEvolution.js +1 -260
  44. package/dist/hooks/useRegistryTemplates.js +1 -31
  45. package/dist/hooks/useSpecContent.js +17 -224
  46. package/dist/hooks/useWorkflowComposer.js +24 -483
  47. package/dist/index.js +40 -3122
  48. package/dist/lib/component-registry.js +1 -48
  49. package/dist/lib/runtime-context.js +1 -21
  50. package/dist/markdown/formatPresentationName.js +1 -9
  51. package/dist/markdown/useMarkdownPresentation.js +1 -65
  52. package/dist/node/EvolutionDashboard.js +1 -803
  53. package/dist/node/EvolutionSidebar.js +1 -531
  54. package/dist/node/LocalDataIndicator.js +1 -68
  55. package/dist/node/MarkdownView.js +1 -213
  56. package/dist/node/OverlayContextProvider.js +1 -202
  57. package/dist/node/PersonalizationInsights.js +1 -455
  58. package/dist/node/SaveToStudioButton.js +1 -79
  59. package/dist/node/SpecDrivenTemplateShell.js +1 -203
  60. package/dist/node/SpecEditorPanel.js +17 -364
  61. package/dist/node/TemplateShell.js +1 -195
  62. package/dist/node/bundles/ExampleTemplateBundle.js +1 -85
  63. package/dist/node/bundles/index.js +1 -85
  64. package/dist/node/hooks/index.js +40 -1151
  65. package/dist/node/hooks/useBehaviorTracking.js +1 -157
  66. package/dist/node/hooks/useEvolution.js +1 -260
  67. package/dist/node/hooks/useRegistryTemplates.js +1 -31
  68. package/dist/node/hooks/useSpecContent.js +17 -224
  69. package/dist/node/hooks/useWorkflowComposer.js +24 -483
  70. package/dist/node/index.js +40 -3122
  71. package/dist/node/lib/component-registry.js +1 -48
  72. package/dist/node/lib/runtime-context.js +1 -21
  73. package/dist/node/markdown/formatPresentationName.js +1 -9
  74. package/dist/node/markdown/useMarkdownPresentation.js +1 -65
  75. package/dist/node/utils/fetchPresentationData.js +1 -15
  76. package/dist/node/utils/generateSpecFromTemplate.js +16 -62
  77. package/dist/node/utils/index.js +16 -76
  78. package/dist/utils/fetchPresentationData.js +1 -15
  79. package/dist/utils/generateSpecFromTemplate.js +16 -62
  80. package/dist/utils/index.js +16 -76
  81. package/package.json +13 -13
  82. package/src/lib/component-registry.tsx +2 -1
  83. package/src/lib/runtime-context.tsx +1 -1
  84. package/src/lib/singletons.test.ts +19 -22
package/dist/index.js CHANGED
@@ -1,2147 +1,6 @@
1
1
  // @bun
2
- // src/hooks/useEvolution.ts
3
- import { useCallback, useEffect, useMemo, useRef, useState } from "react";
4
- "use client";
5
- var EVOLUTION_STORAGE_KEY = "contractspec-evolution-data";
6
- function useEvolution(templateId) {
7
- const [usageStats, setUsageStats] = useState([]);
8
- const [anomalies, setAnomalies] = useState([]);
9
- const [suggestions, setSuggestions] = useState([]);
10
- const [hints, setHints] = useState([]);
11
- const [loading, setLoading] = useState(false);
12
- const metricsRef = useRef([]);
13
- const [operationCount, setOperationCount] = useState(0);
14
- useEffect(() => {
15
- try {
16
- const stored = localStorage.getItem(`${EVOLUTION_STORAGE_KEY}-${templateId}`);
17
- if (stored) {
18
- const data = JSON.parse(stored);
19
- setSuggestions(data.suggestions.map((s) => ({
20
- ...s,
21
- createdAt: new Date(s.createdAt)
22
- })));
23
- }
24
- } catch {}
25
- }, [templateId]);
26
- useEffect(() => {
27
- try {
28
- localStorage.setItem(`${EVOLUTION_STORAGE_KEY}-${templateId}`, JSON.stringify({ suggestions }));
29
- } catch {}
30
- }, [suggestions, templateId]);
31
- const trackOperation = useCallback((operationName, durationMs, success, errorCode) => {
32
- const sample = {
33
- operation: {
34
- name: operationName,
35
- version: "1.0.0",
36
- tenantId: "sandbox"
37
- },
38
- durationMs,
39
- success,
40
- timestamp: new Date,
41
- errorCode
42
- };
43
- metricsRef.current.push(sample);
44
- setOperationCount((prev) => prev + 1);
45
- }, []);
46
- const analyzeUsage = useCallback(() => {
47
- const samples = metricsRef.current;
48
- if (samples.length < 5)
49
- return;
50
- const groups = new Map;
51
- for (const sample of samples) {
52
- const key = `${sample.operation.name}.v${sample.operation.version}`;
53
- const arr = groups.get(key) ?? [];
54
- arr.push(sample);
55
- groups.set(key, arr);
56
- }
57
- const stats = [];
58
- const detectedAnomalies = [];
59
- groups.forEach((opSamples) => {
60
- if (opSamples.length < 3)
61
- return;
62
- const durations = opSamples.map((s) => s.durationMs).sort((a, b) => a - b);
63
- const errors = opSamples.filter((s) => !s.success);
64
- const totalCalls = opSamples.length;
65
- const errorRate = errors.length / totalCalls;
66
- const averageLatencyMs = durations.reduce((sum, value) => sum + value, 0) / totalCalls;
67
- const timestamps = opSamples.map((s) => s.timestamp.getTime());
68
- const firstSample = opSamples[0];
69
- if (!firstSample)
70
- return;
71
- const stat = {
72
- operation: firstSample.operation,
73
- totalCalls,
74
- successRate: 1 - errorRate,
75
- errorRate,
76
- averageLatencyMs,
77
- p95LatencyMs: percentile(durations, 0.95),
78
- p99LatencyMs: percentile(durations, 0.99),
79
- maxLatencyMs: Math.max(...durations),
80
- lastSeenAt: new Date(Math.max(...timestamps)),
81
- windowStart: new Date(Math.min(...timestamps)),
82
- windowEnd: new Date(Math.max(...timestamps)),
83
- topErrors: errors.reduce((acc, s) => {
84
- if (s.errorCode) {
85
- acc[s.errorCode] = (acc[s.errorCode] ?? 0) + 1;
86
- }
87
- return acc;
88
- }, {})
89
- };
90
- stats.push(stat);
91
- if (errorRate > 0.1) {
92
- detectedAnomalies.push({
93
- operation: stat.operation,
94
- severity: errorRate > 0.3 ? "high" : errorRate > 0.2 ? "medium" : "low",
95
- metric: "error-rate",
96
- description: `Error rate ${(errorRate * 100).toFixed(1)}% exceeds threshold`,
97
- detectedAt: new Date,
98
- threshold: 0.1,
99
- observedValue: errorRate
100
- });
101
- }
102
- if (stat.p99LatencyMs > 500) {
103
- detectedAnomalies.push({
104
- operation: stat.operation,
105
- severity: stat.p99LatencyMs > 1000 ? "high" : stat.p99LatencyMs > 750 ? "medium" : "low",
106
- metric: "latency",
107
- description: `P99 latency ${stat.p99LatencyMs.toFixed(0)}ms exceeds threshold`,
108
- detectedAt: new Date,
109
- threshold: 500,
110
- observedValue: stat.p99LatencyMs
111
- });
112
- }
113
- });
114
- setUsageStats(stats);
115
- setAnomalies(detectedAnomalies);
116
- const newHints = detectedAnomalies.map((anomaly) => ({
117
- operation: anomaly.operation,
118
- category: anomaly.metric === "latency" ? "performance" : "error-handling",
119
- summary: anomaly.metric === "latency" ? "Latency regression detected" : "Error spike detected",
120
- justification: anomaly.description,
121
- recommendedActions: anomaly.metric === "latency" ? [
122
- "Add caching layer",
123
- "Optimize database queries",
124
- "Consider pagination"
125
- ] : [
126
- "Add retry logic",
127
- "Improve error handling",
128
- "Add circuit breaker"
129
- ]
130
- }));
131
- setHints(newHints);
132
- }, []);
133
- const generateSuggestions = useCallback(async () => {
134
- if (anomalies.length === 0)
135
- return;
136
- setLoading(true);
137
- await new Promise((resolve) => setTimeout(resolve, 800));
138
- const newSuggestions = anomalies.map((anomaly) => ({
139
- id: `suggestion-${Date.now()}-${Math.random().toString(36).slice(2, 9)}`,
140
- intent: {
141
- id: `intent-${anomaly.operation.name}`,
142
- type: anomaly.metric === "latency" ? "latency-regression" : anomaly.metric === "error-rate" ? "error-spike" : "throughput-drop",
143
- description: anomaly.description,
144
- operation: anomaly.operation,
145
- confidence: {
146
- score: anomaly.severity === "high" ? 0.9 : anomaly.severity === "medium" ? 0.7 : 0.5,
147
- sampleSize: usageStats.find((s) => s.operation.name === anomaly.operation.name)?.totalCalls ?? 0
148
- }
149
- },
150
- target: anomaly.operation,
151
- proposal: {
152
- summary: generateSuggestionSummary(anomaly),
153
- rationale: generateSuggestionRationale(anomaly),
154
- changeType: anomaly.metric === "error-rate" ? "policy-update" : "revision",
155
- recommendedActions: generateRecommendedActions(anomaly)
156
- },
157
- confidence: anomaly.severity === "high" ? 0.85 : anomaly.severity === "medium" ? 0.7 : 0.55,
158
- createdAt: new Date,
159
- createdBy: "ai-evolution-agent",
160
- status: "pending",
161
- priority: anomaly.severity
162
- }));
163
- setSuggestions((prev) => [...prev, ...newSuggestions]);
164
- setLoading(false);
165
- }, [anomalies, usageStats]);
166
- const approveSuggestion = useCallback((id, _notes) => {
167
- setSuggestions((prev) => prev.map((s) => s.id === id ? { ...s, status: "approved" } : s));
168
- }, []);
169
- const rejectSuggestion = useCallback((id, _notes) => {
170
- setSuggestions((prev) => prev.map((s) => s.id === id ? { ...s, status: "rejected" } : s));
171
- }, []);
172
- const clear = useCallback(() => {
173
- metricsRef.current = [];
174
- setOperationCount(0);
175
- setUsageStats([]);
176
- setAnomalies([]);
177
- setSuggestions([]);
178
- setHints([]);
179
- localStorage.removeItem(`${EVOLUTION_STORAGE_KEY}-${templateId}`);
180
- }, [templateId]);
181
- return useMemo(() => ({
182
- usageStats,
183
- anomalies,
184
- suggestions,
185
- hints,
186
- loading,
187
- trackOperation,
188
- analyzeUsage,
189
- generateSuggestions,
190
- approveSuggestion,
191
- rejectSuggestion,
192
- clear,
193
- operationCount
194
- }), [
195
- usageStats,
196
- anomalies,
197
- suggestions,
198
- hints,
199
- loading,
200
- trackOperation,
201
- analyzeUsage,
202
- generateSuggestions,
203
- approveSuggestion,
204
- rejectSuggestion,
205
- clear,
206
- operationCount
207
- ]);
208
- }
209
- function percentile(values, p) {
210
- if (!values.length)
211
- return 0;
212
- if (values.length === 1)
213
- return values[0] ?? 0;
214
- const idx = Math.min(values.length - 1, Math.floor(p * values.length));
215
- return values[idx] ?? 0;
216
- }
217
- function generateSuggestionSummary(anomaly) {
218
- if (anomaly.metric === "latency") {
219
- return `Add caching and pagination to ${anomaly.operation.name} to reduce latency`;
220
- }
221
- if (anomaly.metric === "error-rate") {
222
- return `Add retry policy and circuit breaker to ${anomaly.operation.name}`;
223
- }
224
- return `Optimize ${anomaly.operation.name} for improved throughput`;
225
- }
226
- function generateSuggestionRationale(anomaly) {
227
- if (anomaly.metric === "latency") {
228
- return `The operation ${anomaly.operation.name} is experiencing P99 latency of ${anomaly.observedValue?.toFixed(0)}ms, which is above the recommended threshold of ${anomaly.threshold}ms. This can impact user experience and downstream operations.`;
229
- }
230
- if (anomaly.metric === "error-rate") {
231
- return `The error rate for ${anomaly.operation.name} is ${((anomaly.observedValue ?? 0) * 100).toFixed(1)}%, indicating potential issues with input validation, external dependencies, or resource limits.`;
232
- }
233
- return `Throughput for ${anomaly.operation.name} has dropped significantly, suggesting potential bottlenecks or reduced demand that should be investigated.`;
234
- }
235
- function generateRecommendedActions(anomaly) {
236
- if (anomaly.metric === "latency") {
237
- return [
238
- "Add response caching for frequently accessed data",
239
- "Implement pagination for large result sets",
240
- "Optimize database queries with proper indexing",
241
- "Consider adding a GraphQL DataLoader for batching"
242
- ];
243
- }
244
- if (anomaly.metric === "error-rate") {
245
- return [
246
- "Add input validation at the contract level",
247
- "Implement retry policy with exponential backoff",
248
- "Add circuit breaker for external dependencies",
249
- "Enhance error logging for better debugging"
250
- ];
251
- }
252
- return [
253
- "Review resource allocation and scaling policies",
254
- "Check for upstream routing or load balancer issues",
255
- "Validate feature flag configurations",
256
- "Monitor dependency health metrics"
257
- ];
258
- }
259
-
260
- // src/EvolutionDashboard.tsx
261
- import { Button, LoaderBlock } from "@contractspec/lib.design-system";
262
- import { Badge } from "@contractspec/lib.ui-kit-web/ui/badge";
263
- import { Card } from "@contractspec/lib.ui-kit-web/ui/card";
264
- import { useCallback as useCallback2, useMemo as useMemo2 } from "react";
265
- import { jsxDEV } from "react/jsx-dev-runtime";
266
- "use client";
267
- function EvolutionDashboard({
268
- templateId,
269
- onLog
270
- }) {
271
- const {
272
- usageStats,
273
- anomalies,
274
- suggestions,
275
- hints,
276
- loading,
277
- trackOperation,
278
- analyzeUsage,
279
- generateSuggestions,
280
- approveSuggestion,
281
- rejectSuggestion,
282
- clear,
283
- operationCount
284
- } = useEvolution(templateId);
285
- const handleSimulateOperations = useCallback2(() => {
286
- const operations = [
287
- { name: `${templateId}.list`, duration: 150, success: true },
288
- { name: `${templateId}.list`, duration: 180, success: true },
289
- { name: `${templateId}.create`, duration: 350, success: true },
290
- {
291
- name: `${templateId}.create`,
292
- duration: 420,
293
- success: false,
294
- error: "VALIDATION_ERROR"
295
- },
296
- { name: `${templateId}.list`, duration: 200, success: true },
297
- { name: `${templateId}.get`, duration: 80, success: true },
298
- { name: `${templateId}.update`, duration: 280, success: true },
299
- { name: `${templateId}.list`, duration: 950, success: true },
300
- {
301
- name: `${templateId}.delete`,
302
- duration: 150,
303
- success: false,
304
- error: "NOT_FOUND"
305
- },
306
- { name: `${templateId}.create`, duration: 380, success: true }
307
- ];
308
- for (const op of operations) {
309
- trackOperation(op.name, op.duration, op.success, op.error);
310
- }
311
- onLog?.(`Simulated ${operations.length} operations`);
312
- setTimeout(() => {
313
- analyzeUsage();
314
- onLog?.("Analysis complete");
315
- }, 100);
316
- }, [templateId, trackOperation, analyzeUsage, onLog]);
317
- const handleGenerateSuggestions = useCallback2(async () => {
318
- await generateSuggestions();
319
- onLog?.("AI suggestions generated");
320
- }, [generateSuggestions, onLog]);
321
- const handleApproveSuggestion = useCallback2((id) => {
322
- approveSuggestion(id);
323
- onLog?.(`Suggestion ${id.slice(0, 8)} approved`);
324
- }, [approveSuggestion, onLog]);
325
- const handleRejectSuggestion = useCallback2((id) => {
326
- rejectSuggestion(id);
327
- onLog?.(`Suggestion ${id.slice(0, 8)} rejected`);
328
- }, [rejectSuggestion, onLog]);
329
- const handleClear = useCallback2(() => {
330
- clear();
331
- onLog?.("Evolution data cleared");
332
- }, [clear, onLog]);
333
- const pendingSuggestions = useMemo2(() => suggestions.filter((s) => s.status === "pending"), [suggestions]);
334
- return /* @__PURE__ */ jsxDEV("div", {
335
- className: "space-y-6",
336
- children: [
337
- /* @__PURE__ */ jsxDEV("div", {
338
- className: "flex items-center justify-between",
339
- children: [
340
- /* @__PURE__ */ jsxDEV("div", {
341
- children: [
342
- /* @__PURE__ */ jsxDEV("h2", {
343
- className: "font-semibold text-xl",
344
- children: "AI Evolution Engine"
345
- }, undefined, false, undefined, this),
346
- /* @__PURE__ */ jsxDEV("p", {
347
- className: "text-muted-foreground text-sm",
348
- children: "Analyze usage patterns and get AI-powered suggestions"
349
- }, undefined, false, undefined, this)
350
- ]
351
- }, undefined, true, undefined, this),
352
- /* @__PURE__ */ jsxDEV("div", {
353
- className: "flex items-center gap-2",
354
- children: [
355
- /* @__PURE__ */ jsxDEV(Badge, {
356
- variant: "secondary",
357
- children: [
358
- operationCount,
359
- " ops tracked"
360
- ]
361
- }, undefined, true, undefined, this),
362
- /* @__PURE__ */ jsxDEV(Button, {
363
- variant: "ghost",
364
- size: "sm",
365
- onPress: handleClear,
366
- children: "Clear"
367
- }, undefined, false, undefined, this)
368
- ]
369
- }, undefined, true, undefined, this)
370
- ]
371
- }, undefined, true, undefined, this),
372
- /* @__PURE__ */ jsxDEV(Card, {
373
- className: "p-4",
374
- children: [
375
- /* @__PURE__ */ jsxDEV("div", {
376
- className: "flex flex-wrap items-center gap-3",
377
- children: [
378
- /* @__PURE__ */ jsxDEV(Button, {
379
- variant: "default",
380
- size: "sm",
381
- onPress: handleSimulateOperations,
382
- children: "Simulate Operations"
383
- }, undefined, false, undefined, this),
384
- /* @__PURE__ */ jsxDEV(Button, {
385
- variant: "outline",
386
- size: "sm",
387
- onPress: analyzeUsage,
388
- disabled: operationCount < 5,
389
- children: "Analyze Usage"
390
- }, undefined, false, undefined, this),
391
- /* @__PURE__ */ jsxDEV(Button, {
392
- variant: "outline",
393
- size: "sm",
394
- onPress: handleGenerateSuggestions,
395
- disabled: anomalies.length === 0 || loading,
396
- children: loading ? "Generating..." : "Generate AI Suggestions"
397
- }, undefined, false, undefined, this)
398
- ]
399
- }, undefined, true, undefined, this),
400
- /* @__PURE__ */ jsxDEV("p", {
401
- className: "mt-2 text-muted-foreground text-xs",
402
- children: "Simulate sandbox operations, analyze patterns, and generate AI improvement suggestions."
403
- }, undefined, false, undefined, this)
404
- ]
405
- }, undefined, true, undefined, this),
406
- loading && /* @__PURE__ */ jsxDEV(LoaderBlock, {
407
- label: "Generating AI suggestions..."
408
- }, undefined, false, undefined, this),
409
- usageStats.length > 0 && /* @__PURE__ */ jsxDEV(Card, {
410
- className: "p-4",
411
- children: [
412
- /* @__PURE__ */ jsxDEV("h3", {
413
- className: "mb-3 font-semibold",
414
- children: "Usage Statistics"
415
- }, undefined, false, undefined, this),
416
- /* @__PURE__ */ jsxDEV("div", {
417
- className: "grid gap-3 md:grid-cols-2 lg:grid-cols-3",
418
- children: usageStats.map((stat) => /* @__PURE__ */ jsxDEV(UsageStatCard, {
419
- stat
420
- }, stat.operation.name, false, undefined, this))
421
- }, undefined, false, undefined, this)
422
- ]
423
- }, undefined, true, undefined, this),
424
- anomalies.length > 0 && /* @__PURE__ */ jsxDEV(Card, {
425
- className: "p-4",
426
- children: [
427
- /* @__PURE__ */ jsxDEV("div", {
428
- className: "mb-3 flex items-center justify-between",
429
- children: [
430
- /* @__PURE__ */ jsxDEV("h3", {
431
- className: "font-semibold",
432
- children: "Detected Anomalies"
433
- }, undefined, false, undefined, this),
434
- /* @__PURE__ */ jsxDEV(Badge, {
435
- variant: "secondary",
436
- className: "border-amber-500/30 bg-amber-500/20 text-amber-400",
437
- children: [
438
- anomalies.length,
439
- " issues"
440
- ]
441
- }, undefined, true, undefined, this)
442
- ]
443
- }, undefined, true, undefined, this),
444
- /* @__PURE__ */ jsxDEV("div", {
445
- className: "space-y-2",
446
- children: anomalies.map((anomaly, index) => /* @__PURE__ */ jsxDEV(AnomalyCard, {
447
- anomaly
448
- }, `${anomaly.operation.name}-${index}`, false, undefined, this))
449
- }, undefined, false, undefined, this)
450
- ]
451
- }, undefined, true, undefined, this),
452
- suggestions.length > 0 && /* @__PURE__ */ jsxDEV(Card, {
453
- className: "p-4",
454
- children: [
455
- /* @__PURE__ */ jsxDEV("div", {
456
- className: "mb-3 flex items-center justify-between",
457
- children: [
458
- /* @__PURE__ */ jsxDEV("h3", {
459
- className: "font-semibold",
460
- children: "AI Suggestions"
461
- }, undefined, false, undefined, this),
462
- /* @__PURE__ */ jsxDEV("div", {
463
- className: "flex items-center gap-2",
464
- children: pendingSuggestions.length > 0 && /* @__PURE__ */ jsxDEV(Badge, {
465
- variant: "secondary",
466
- className: "border-amber-500/30 bg-amber-500/20 text-amber-400",
467
- children: [
468
- pendingSuggestions.length,
469
- " pending"
470
- ]
471
- }, undefined, true, undefined, this)
472
- }, undefined, false, undefined, this)
473
- ]
474
- }, undefined, true, undefined, this),
475
- /* @__PURE__ */ jsxDEV("div", {
476
- className: "space-y-3",
477
- children: suggestions.map((suggestion) => /* @__PURE__ */ jsxDEV(SuggestionCard, {
478
- suggestion,
479
- onApprove: handleApproveSuggestion,
480
- onReject: handleRejectSuggestion
481
- }, suggestion.id, false, undefined, this))
482
- }, undefined, false, undefined, this)
483
- ]
484
- }, undefined, true, undefined, this),
485
- hints.length > 0 && /* @__PURE__ */ jsxDEV(Card, {
486
- className: "p-4",
487
- children: [
488
- /* @__PURE__ */ jsxDEV("h3", {
489
- className: "mb-3 font-semibold",
490
- children: "Optimization Hints"
491
- }, undefined, false, undefined, this),
492
- /* @__PURE__ */ jsxDEV("div", {
493
- className: "space-y-2",
494
- children: hints.map((hint, index) => /* @__PURE__ */ jsxDEV(HintCard, {
495
- hint
496
- }, `${hint.operation.name}-${index}`, false, undefined, this))
497
- }, undefined, false, undefined, this)
498
- ]
499
- }, undefined, true, undefined, this),
500
- usageStats.length === 0 && anomalies.length === 0 && suggestions.length === 0 && /* @__PURE__ */ jsxDEV(Card, {
501
- className: "p-8 text-center",
502
- children: /* @__PURE__ */ jsxDEV("p", {
503
- className: "text-muted-foreground",
504
- children: 'Click "Simulate Operations" to generate sample data for analysis.'
505
- }, undefined, false, undefined, this)
506
- }, undefined, false, undefined, this)
507
- ]
508
- }, undefined, true, undefined, this);
509
- }
510
- function UsageStatCard({ stat }) {
511
- return /* @__PURE__ */ jsxDEV("div", {
512
- className: "rounded-lg border border-violet-500/20 bg-violet-500/5 p-3",
513
- children: [
514
- /* @__PURE__ */ jsxDEV("div", {
515
- className: "mb-2 flex items-center justify-between",
516
- children: [
517
- /* @__PURE__ */ jsxDEV("span", {
518
- className: "font-medium font-mono text-sm",
519
- children: stat.operation.name
520
- }, undefined, false, undefined, this),
521
- /* @__PURE__ */ jsxDEV(Badge, {
522
- variant: stat.errorRate > 0.1 ? "destructive" : "default",
523
- className: stat.errorRate > 0.1 ? "" : "border-green-500/30 bg-green-500/20 text-green-400",
524
- children: [
525
- ((1 - stat.errorRate) * 100).toFixed(0),
526
- "% success"
527
- ]
528
- }, undefined, true, undefined, this)
529
- ]
530
- }, undefined, true, undefined, this),
531
- /* @__PURE__ */ jsxDEV("div", {
532
- className: "grid grid-cols-2 gap-2 text-xs",
533
- children: [
534
- /* @__PURE__ */ jsxDEV("div", {
535
- children: [
536
- /* @__PURE__ */ jsxDEV("span", {
537
- className: "text-muted-foreground",
538
- children: "Total Calls:"
539
- }, undefined, false, undefined, this),
540
- " ",
541
- /* @__PURE__ */ jsxDEV("span", {
542
- className: "font-medium",
543
- children: stat.totalCalls
544
- }, undefined, false, undefined, this)
545
- ]
546
- }, undefined, true, undefined, this),
547
- /* @__PURE__ */ jsxDEV("div", {
548
- children: [
549
- /* @__PURE__ */ jsxDEV("span", {
550
- className: "text-muted-foreground",
551
- children: "Avg Latency:"
552
- }, undefined, false, undefined, this),
553
- " ",
554
- /* @__PURE__ */ jsxDEV("span", {
555
- className: "font-medium",
556
- children: [
557
- stat.averageLatencyMs.toFixed(0),
558
- "ms"
559
- ]
560
- }, undefined, true, undefined, this)
561
- ]
562
- }, undefined, true, undefined, this),
563
- /* @__PURE__ */ jsxDEV("div", {
564
- children: [
565
- /* @__PURE__ */ jsxDEV("span", {
566
- className: "text-muted-foreground",
567
- children: "P95:"
568
- }, undefined, false, undefined, this),
569
- " ",
570
- /* @__PURE__ */ jsxDEV("span", {
571
- className: "font-medium",
572
- children: [
573
- stat.p95LatencyMs.toFixed(0),
574
- "ms"
575
- ]
576
- }, undefined, true, undefined, this)
577
- ]
578
- }, undefined, true, undefined, this),
579
- /* @__PURE__ */ jsxDEV("div", {
580
- children: [
581
- /* @__PURE__ */ jsxDEV("span", {
582
- className: "text-muted-foreground",
583
- children: "P99:"
584
- }, undefined, false, undefined, this),
585
- " ",
586
- /* @__PURE__ */ jsxDEV("span", {
587
- className: "font-medium",
588
- children: [
589
- stat.p99LatencyMs.toFixed(0),
590
- "ms"
591
- ]
592
- }, undefined, true, undefined, this)
593
- ]
594
- }, undefined, true, undefined, this)
595
- ]
596
- }, undefined, true, undefined, this)
597
- ]
598
- }, undefined, true, undefined, this);
599
- }
600
- function AnomalyCard({ anomaly }) {
601
- const severityColors = {
602
- low: "text-amber-400",
603
- medium: "text-orange-400",
604
- high: "text-red-400"
605
- };
606
- return /* @__PURE__ */ jsxDEV("div", {
607
- className: "flex items-center justify-between rounded-lg border border-amber-500/30 bg-amber-500/5 p-3",
608
- children: [
609
- /* @__PURE__ */ jsxDEV("div", {
610
- className: "flex items-center gap-3",
611
- children: [
612
- /* @__PURE__ */ jsxDEV("span", {
613
- className: `text-lg ${severityColors[anomaly.severity]}`,
614
- title: `${anomaly.severity} severity`,
615
- children: anomaly.severity === "high" ? "\uD83D\uDD34" : anomaly.severity === "medium" ? "\uD83D\uDFE0" : "\uD83D\uDFE1"
616
- }, undefined, false, undefined, this),
617
- /* @__PURE__ */ jsxDEV("div", {
618
- children: [
619
- /* @__PURE__ */ jsxDEV("p", {
620
- className: "font-medium text-sm",
621
- children: anomaly.description
622
- }, undefined, false, undefined, this),
623
- /* @__PURE__ */ jsxDEV("p", {
624
- className: "text-muted-foreground text-xs",
625
- children: [
626
- anomaly.operation.name,
627
- " \u2022 ",
628
- anomaly.metric
629
- ]
630
- }, undefined, true, undefined, this)
631
- ]
632
- }, undefined, true, undefined, this)
633
- ]
634
- }, undefined, true, undefined, this),
635
- /* @__PURE__ */ jsxDEV(Badge, {
636
- variant: anomaly.severity === "high" ? "destructive" : "secondary",
637
- className: anomaly.severity === "medium" ? "border-amber-500/30 bg-amber-500/20 text-amber-400" : "",
638
- children: anomaly.severity
639
- }, undefined, false, undefined, this)
640
- ]
641
- }, undefined, true, undefined, this);
642
- }
643
- function SuggestionCard({
644
- suggestion,
645
- onApprove,
646
- onReject
647
- }) {
648
- const getStatusStyles = (status) => {
649
- switch (status) {
650
- case "pending":
651
- return {
652
- variant: "secondary",
653
- className: "bg-amber-500/20 text-amber-400 border-amber-500/30"
654
- };
655
- case "approved":
656
- return {
657
- variant: "default",
658
- className: "bg-green-500/20 text-green-400 border-green-500/30"
659
- };
660
- case "rejected":
661
- return { variant: "destructive", className: "" };
662
- default:
663
- return { variant: "secondary", className: "" };
664
- }
665
- };
666
- const statusStyle = getStatusStyles(suggestion.status);
667
- return /* @__PURE__ */ jsxDEV("div", {
668
- className: "rounded-lg border border-violet-500/30 bg-violet-500/5 p-4",
669
- children: [
670
- /* @__PURE__ */ jsxDEV("div", {
671
- className: "mb-2 flex items-start justify-between",
672
- children: [
673
- /* @__PURE__ */ jsxDEV("div", {
674
- className: "flex-1",
675
- children: [
676
- /* @__PURE__ */ jsxDEV("div", {
677
- className: "flex items-center gap-2",
678
- children: [
679
- /* @__PURE__ */ jsxDEV("span", {
680
- className: "text-lg",
681
- children: suggestion.intent.type === "latency-regression" ? "\u26A1" : suggestion.intent.type === "error-spike" ? "\uD83D\uDD25" : "\uD83D\uDCC9"
682
- }, undefined, false, undefined, this),
683
- /* @__PURE__ */ jsxDEV("h4", {
684
- className: "font-medium",
685
- children: suggestion.proposal.summary
686
- }, undefined, false, undefined, this)
687
- ]
688
- }, undefined, true, undefined, this),
689
- /* @__PURE__ */ jsxDEV("p", {
690
- className: "mt-1 text-muted-foreground text-sm",
691
- children: suggestion.proposal.rationale
692
- }, undefined, false, undefined, this)
693
- ]
694
- }, undefined, true, undefined, this),
695
- /* @__PURE__ */ jsxDEV(Badge, {
696
- variant: statusStyle.variant,
697
- className: statusStyle.className,
698
- children: suggestion.status
699
- }, undefined, false, undefined, this)
700
- ]
701
- }, undefined, true, undefined, this),
702
- suggestion.proposal.recommendedActions && suggestion.proposal.recommendedActions.length > 0 && /* @__PURE__ */ jsxDEV("div", {
703
- className: "mt-3",
704
- children: [
705
- /* @__PURE__ */ jsxDEV("p", {
706
- className: "mb-1 font-semibold text-violet-400 text-xs uppercase",
707
- children: "Recommended Actions"
708
- }, undefined, false, undefined, this),
709
- /* @__PURE__ */ jsxDEV("ul", {
710
- className: "list-inside list-disc space-y-1 text-xs",
711
- children: suggestion.proposal.recommendedActions.slice(0, 3).map((action, i) => /* @__PURE__ */ jsxDEV("li", {
712
- children: action
713
- }, i, false, undefined, this))
714
- }, undefined, false, undefined, this)
715
- ]
716
- }, undefined, true, undefined, this),
717
- /* @__PURE__ */ jsxDEV("div", {
718
- className: "mt-3 flex items-center justify-between",
719
- children: [
720
- /* @__PURE__ */ jsxDEV("div", {
721
- className: "flex items-center gap-2 text-xs",
722
- children: [
723
- /* @__PURE__ */ jsxDEV("span", {
724
- className: "text-muted-foreground",
725
- children: [
726
- "Confidence: ",
727
- (suggestion.confidence * 100).toFixed(0),
728
- "%"
729
- ]
730
- }, undefined, true, undefined, this),
731
- /* @__PURE__ */ jsxDEV("span", {
732
- className: "text-muted-foreground",
733
- children: "\u2022"
734
- }, undefined, false, undefined, this),
735
- /* @__PURE__ */ jsxDEV(Badge, {
736
- variant: "secondary",
737
- children: suggestion.priority
738
- }, undefined, false, undefined, this)
739
- ]
740
- }, undefined, true, undefined, this),
741
- suggestion.status === "pending" && /* @__PURE__ */ jsxDEV("div", {
742
- className: "flex items-center gap-2",
743
- children: [
744
- /* @__PURE__ */ jsxDEV(Button, {
745
- variant: "outline",
746
- size: "sm",
747
- onPress: () => onReject(suggestion.id),
748
- children: "Reject"
749
- }, undefined, false, undefined, this),
750
- /* @__PURE__ */ jsxDEV(Button, {
751
- variant: "default",
752
- size: "sm",
753
- onPress: () => onApprove(suggestion.id),
754
- children: "Approve"
755
- }, undefined, false, undefined, this)
756
- ]
757
- }, undefined, true, undefined, this)
758
- ]
759
- }, undefined, true, undefined, this)
760
- ]
761
- }, undefined, true, undefined, this);
762
- }
763
- function HintCard({ hint }) {
764
- const categoryIcons = {
765
- schema: "\uD83D\uDCD0",
766
- policy: "\uD83D\uDD12",
767
- performance: "\u26A1",
768
- "error-handling": "\uD83D\uDEE1\uFE0F"
769
- };
770
- return /* @__PURE__ */ jsxDEV("div", {
771
- className: "rounded-lg border border-blue-500/20 bg-blue-500/5 p-3",
772
- children: /* @__PURE__ */ jsxDEV("div", {
773
- className: "flex items-start gap-3",
774
- children: [
775
- /* @__PURE__ */ jsxDEV("span", {
776
- className: "text-lg",
777
- children: categoryIcons[hint.category]
778
- }, undefined, false, undefined, this),
779
- /* @__PURE__ */ jsxDEV("div", {
780
- className: "flex-1",
781
- children: [
782
- /* @__PURE__ */ jsxDEV("p", {
783
- className: "font-medium",
784
- children: hint.summary
785
- }, undefined, false, undefined, this),
786
- /* @__PURE__ */ jsxDEV("p", {
787
- className: "mt-1 text-muted-foreground text-xs",
788
- children: hint.justification
789
- }, undefined, false, undefined, this),
790
- hint.recommendedActions.length > 0 && /* @__PURE__ */ jsxDEV("ul", {
791
- className: "mt-2 list-inside list-disc text-xs",
792
- children: hint.recommendedActions.slice(0, 2).map((action, i) => /* @__PURE__ */ jsxDEV("li", {
793
- children: action
794
- }, i, false, undefined, this))
795
- }, undefined, false, undefined, this)
796
- ]
797
- }, undefined, true, undefined, this)
798
- ]
799
- }, undefined, true, undefined, this)
800
- }, undefined, false, undefined, this);
801
- }
802
-
803
- // src/EvolutionSidebar.tsx
804
- import { Button as Button2 } from "@contractspec/lib.design-system";
805
- import { Badge as Badge2 } from "@contractspec/lib.ui-kit-web/ui/badge";
806
- import { Card as Card2 } from "@contractspec/lib.ui-kit-web/ui/card";
807
- import { useCallback as useCallback3, useMemo as useMemo3 } from "react";
808
- import { jsxDEV as jsxDEV2 } from "react/jsx-dev-runtime";
809
- "use client";
810
- function EvolutionSidebar({
811
- templateId,
812
- expanded = false,
813
- onToggle,
814
- onLog,
815
- onOpenEvolution
816
- }) {
817
- const {
818
- anomalies,
819
- suggestions,
820
- loading,
821
- approveSuggestion,
822
- rejectSuggestion,
823
- operationCount
824
- } = useEvolution(templateId);
825
- const pendingSuggestions = useMemo3(() => suggestions.filter((s) => s.status === "pending"), [suggestions]);
826
- const topAnomalies = useMemo3(() => anomalies.sort((a, b) => {
827
- const severityOrder = { high: 0, medium: 1, low: 2 };
828
- return severityOrder[a.severity] - severityOrder[b.severity];
829
- }).slice(0, 3), [anomalies]);
830
- const handleApprove = useCallback3((id) => {
831
- approveSuggestion(id);
832
- onLog?.(`Approved suggestion ${id.slice(0, 8)}`);
833
- }, [approveSuggestion, onLog]);
834
- const handleReject = useCallback3((id) => {
835
- rejectSuggestion(id);
836
- onLog?.(`Rejected suggestion ${id.slice(0, 8)}`);
837
- }, [rejectSuggestion, onLog]);
838
- if (!expanded) {
839
- return /* @__PURE__ */ jsxDEV2("button", {
840
- onClick: onToggle,
841
- className: "flex items-center gap-2 rounded-lg border border-violet-500/30 bg-violet-500/10 px-3 py-2 text-sm transition hover:bg-violet-500/20",
842
- type: "button",
843
- children: [
844
- /* @__PURE__ */ jsxDEV2("span", {
845
- children: "\uD83E\uDD16"
846
- }, undefined, false, undefined, this),
847
- /* @__PURE__ */ jsxDEV2("span", {
848
- children: "Evolution"
849
- }, undefined, false, undefined, this),
850
- pendingSuggestions.length > 0 && /* @__PURE__ */ jsxDEV2(Badge2, {
851
- variant: "secondary",
852
- className: "border-amber-500/30 bg-amber-500/20 text-amber-400",
853
- children: pendingSuggestions.length
854
- }, undefined, false, undefined, this),
855
- anomalies.length > 0 && pendingSuggestions.length === 0 && /* @__PURE__ */ jsxDEV2(Badge2, {
856
- variant: "destructive",
857
- children: anomalies.length
858
- }, undefined, false, undefined, this)
859
- ]
860
- }, undefined, true, undefined, this);
861
- }
862
- return /* @__PURE__ */ jsxDEV2(Card2, {
863
- className: "w-80 overflow-hidden",
864
- children: [
865
- /* @__PURE__ */ jsxDEV2("div", {
866
- className: "flex items-center justify-between border-violet-500/20 border-b bg-violet-500/5 px-3 py-2",
867
- children: [
868
- /* @__PURE__ */ jsxDEV2("div", {
869
- className: "flex items-center gap-2",
870
- children: [
871
- /* @__PURE__ */ jsxDEV2("span", {
872
- children: "\uD83E\uDD16"
873
- }, undefined, false, undefined, this),
874
- /* @__PURE__ */ jsxDEV2("span", {
875
- className: "font-semibold text-sm",
876
- children: "Evolution"
877
- }, undefined, false, undefined, this)
878
- ]
879
- }, undefined, true, undefined, this),
880
- /* @__PURE__ */ jsxDEV2("div", {
881
- className: "flex items-center gap-1",
882
- children: [
883
- onOpenEvolution && /* @__PURE__ */ jsxDEV2(Button2, {
884
- variant: "ghost",
885
- size: "sm",
886
- onPress: onOpenEvolution,
887
- children: "Expand"
888
- }, undefined, false, undefined, this),
889
- /* @__PURE__ */ jsxDEV2("button", {
890
- onClick: onToggle,
891
- className: "p-1 text-muted-foreground hover:text-foreground",
892
- type: "button",
893
- title: "Collapse",
894
- children: "\u2715"
895
- }, undefined, false, undefined, this)
896
- ]
897
- }, undefined, true, undefined, this)
898
- ]
899
- }, undefined, true, undefined, this),
900
- /* @__PURE__ */ jsxDEV2("div", {
901
- className: "max-h-96 overflow-y-auto p-3",
902
- children: [
903
- /* @__PURE__ */ jsxDEV2("div", {
904
- className: "mb-3 flex items-center justify-between text-xs",
905
- children: [
906
- /* @__PURE__ */ jsxDEV2("span", {
907
- className: "text-muted-foreground",
908
- children: [
909
- operationCount,
910
- " ops tracked"
911
- ]
912
- }, undefined, true, undefined, this),
913
- /* @__PURE__ */ jsxDEV2("div", {
914
- className: "flex items-center gap-2",
915
- children: [
916
- anomalies.length > 0 && /* @__PURE__ */ jsxDEV2(Badge2, {
917
- variant: "destructive",
918
- children: [
919
- anomalies.length,
920
- " anomalies"
921
- ]
922
- }, undefined, true, undefined, this),
923
- pendingSuggestions.length > 0 && /* @__PURE__ */ jsxDEV2(Badge2, {
924
- variant: "secondary",
925
- className: "border-amber-500/30 bg-amber-500/20 text-amber-400",
926
- children: [
927
- pendingSuggestions.length,
928
- " pending"
929
- ]
930
- }, undefined, true, undefined, this)
931
- ]
932
- }, undefined, true, undefined, this)
933
- ]
934
- }, undefined, true, undefined, this),
935
- loading && /* @__PURE__ */ jsxDEV2("div", {
936
- className: "py-4 text-center text-muted-foreground text-sm",
937
- children: "Generating suggestions..."
938
- }, undefined, false, undefined, this),
939
- topAnomalies.length > 0 && /* @__PURE__ */ jsxDEV2("div", {
940
- className: "mb-4",
941
- children: [
942
- /* @__PURE__ */ jsxDEV2("p", {
943
- className: "mb-2 font-semibold text-violet-400 text-xs uppercase",
944
- children: "Top Issues"
945
- }, undefined, false, undefined, this),
946
- /* @__PURE__ */ jsxDEV2("div", {
947
- className: "space-y-2",
948
- children: topAnomalies.map((anomaly, index) => /* @__PURE__ */ jsxDEV2("div", {
949
- className: "rounded border border-amber-500/20 bg-amber-500/5 p-2 text-xs",
950
- children: [
951
- /* @__PURE__ */ jsxDEV2("div", {
952
- className: "flex items-center gap-2",
953
- children: [
954
- /* @__PURE__ */ jsxDEV2("span", {
955
- children: anomaly.severity === "high" ? "\uD83D\uDD34" : anomaly.severity === "medium" ? "\uD83D\uDFE0" : "\uD83D\uDFE1"
956
- }, undefined, false, undefined, this),
957
- /* @__PURE__ */ jsxDEV2("span", {
958
- className: "truncate font-medium",
959
- children: anomaly.operation.name
960
- }, undefined, false, undefined, this)
961
- ]
962
- }, undefined, true, undefined, this),
963
- /* @__PURE__ */ jsxDEV2("p", {
964
- className: "mt-1 truncate text-muted-foreground",
965
- children: anomaly.description
966
- }, undefined, false, undefined, this)
967
- ]
968
- }, `${anomaly.operation.name}-${index}`, true, undefined, this))
969
- }, undefined, false, undefined, this)
970
- ]
971
- }, undefined, true, undefined, this),
972
- pendingSuggestions.length > 0 && /* @__PURE__ */ jsxDEV2("div", {
973
- children: [
974
- /* @__PURE__ */ jsxDEV2("p", {
975
- className: "mb-2 font-semibold text-violet-400 text-xs uppercase",
976
- children: "Pending Suggestions"
977
- }, undefined, false, undefined, this),
978
- /* @__PURE__ */ jsxDEV2("div", {
979
- className: "space-y-2",
980
- children: [
981
- pendingSuggestions.slice(0, 3).map((suggestion) => /* @__PURE__ */ jsxDEV2(CompactSuggestionCard, {
982
- suggestion,
983
- onApprove: handleApprove,
984
- onReject: handleReject
985
- }, suggestion.id, false, undefined, this)),
986
- pendingSuggestions.length > 3 && /* @__PURE__ */ jsxDEV2("p", {
987
- className: "text-center text-muted-foreground text-xs",
988
- children: [
989
- "+",
990
- pendingSuggestions.length - 3,
991
- " more suggestions"
992
- ]
993
- }, undefined, true, undefined, this)
994
- ]
995
- }, undefined, true, undefined, this)
996
- ]
997
- }, undefined, true, undefined, this),
998
- anomalies.length === 0 && pendingSuggestions.length === 0 && !loading && /* @__PURE__ */ jsxDEV2("div", {
999
- className: "py-4 text-center text-muted-foreground text-xs",
1000
- children: "No issues detected. Keep coding!"
1001
- }, undefined, false, undefined, this)
1002
- ]
1003
- }, undefined, true, undefined, this),
1004
- onOpenEvolution && /* @__PURE__ */ jsxDEV2("div", {
1005
- className: "border-violet-500/20 border-t p-2",
1006
- children: /* @__PURE__ */ jsxDEV2(Button2, {
1007
- variant: "ghost",
1008
- size: "sm",
1009
- className: "w-full",
1010
- onPress: onOpenEvolution,
1011
- children: "Open Evolution Dashboard \u2192"
1012
- }, undefined, false, undefined, this)
1013
- }, undefined, false, undefined, this)
1014
- ]
1015
- }, undefined, true, undefined, this);
1016
- }
1017
- function CompactSuggestionCard({
1018
- suggestion,
1019
- onApprove,
1020
- onReject
1021
- }) {
1022
- return /* @__PURE__ */ jsxDEV2("div", {
1023
- className: "rounded border border-violet-500/20 bg-violet-500/5 p-2",
1024
- children: [
1025
- /* @__PURE__ */ jsxDEV2("div", {
1026
- className: "flex items-start justify-between gap-2",
1027
- children: /* @__PURE__ */ jsxDEV2("div", {
1028
- className: "min-w-0 flex-1",
1029
- children: [
1030
- /* @__PURE__ */ jsxDEV2("p", {
1031
- className: "truncate font-medium text-xs",
1032
- children: suggestion.proposal.summary
1033
- }, undefined, false, undefined, this),
1034
- /* @__PURE__ */ jsxDEV2("div", {
1035
- className: "mt-1 flex items-center gap-2 text-xs",
1036
- children: [
1037
- /* @__PURE__ */ jsxDEV2(Badge2, {
1038
- variant: "secondary",
1039
- children: suggestion.priority
1040
- }, undefined, false, undefined, this),
1041
- /* @__PURE__ */ jsxDEV2("span", {
1042
- className: "text-muted-foreground",
1043
- children: [
1044
- (suggestion.confidence * 100).toFixed(0),
1045
- "%"
1046
- ]
1047
- }, undefined, true, undefined, this)
1048
- ]
1049
- }, undefined, true, undefined, this)
1050
- ]
1051
- }, undefined, true, undefined, this)
1052
- }, undefined, false, undefined, this),
1053
- /* @__PURE__ */ jsxDEV2("div", {
1054
- className: "mt-2 flex justify-end gap-1",
1055
- children: [
1056
- /* @__PURE__ */ jsxDEV2("button", {
1057
- onClick: () => onReject(suggestion.id),
1058
- className: "rounded px-2 py-0.5 text-red-400 text-xs hover:bg-red-400/10",
1059
- type: "button",
1060
- children: "Reject"
1061
- }, undefined, false, undefined, this),
1062
- /* @__PURE__ */ jsxDEV2("button", {
1063
- onClick: () => onApprove(suggestion.id),
1064
- className: "rounded bg-violet-500/20 px-2 py-0.5 text-violet-400 text-xs hover:bg-violet-500/30",
1065
- type: "button",
1066
- children: "Approve"
1067
- }, undefined, false, undefined, this)
1068
- ]
1069
- }, undefined, true, undefined, this)
1070
- ]
1071
- }, undefined, true, undefined, this);
1072
- }
1073
-
1074
- // src/lib/runtime-context.tsx
1075
- import { createContext, useContext } from "react";
1076
- "use client";
1077
- var TEMPLATE_RUNTIME_CONTEXT_KEY = Symbol.for("@contractspec/lib.example-shared-ui/template-runtime-context");
1078
- function getTemplateRuntimeContextSingleton() {
1079
- const store = globalThis;
1080
- store[TEMPLATE_RUNTIME_CONTEXT_KEY] ??= createContext(null);
1081
- return store[TEMPLATE_RUNTIME_CONTEXT_KEY];
1082
- }
1083
- var TemplateRuntimeContext = getTemplateRuntimeContextSingleton();
1084
- function useTemplateRuntime() {
1085
- const context = useContext(TemplateRuntimeContext);
1086
- if (!context) {
1087
- throw new Error("useTemplateRuntime must be used within a TemplateRuntimeProvider");
1088
- }
1089
- return context;
1090
- }
1091
-
1092
- // src/LocalDataIndicator.tsx
1093
- import { RefreshCw, Shield } from "lucide-react";
1094
- import { useState as useState2 } from "react";
1095
- import { jsxDEV as jsxDEV3 } from "react/jsx-dev-runtime";
1096
- "use client";
1097
- function LocalDataIndicator() {
1098
- const { projectId, templateId, template, installer } = useTemplateRuntime();
1099
- const [isResetting, setIsResetting] = useState2(false);
1100
- const handleReset = async () => {
1101
- setIsResetting(true);
1102
- try {
1103
- await installer.install(templateId, { projectId });
1104
- } finally {
1105
- setIsResetting(false);
1106
- }
1107
- };
1108
- return /* @__PURE__ */ jsxDEV3("div", {
1109
- className: "inline-flex items-center gap-2 rounded-full border border-border bg-muted/40 px-3 py-1 text-muted-foreground text-xs",
1110
- children: [
1111
- /* @__PURE__ */ jsxDEV3(Shield, {
1112
- className: "h-3.5 w-3.5 text-violet-400"
1113
- }, undefined, false, undefined, this),
1114
- /* @__PURE__ */ jsxDEV3("span", {
1115
- children: [
1116
- "Local runtime \xB7",
1117
- " ",
1118
- /* @__PURE__ */ jsxDEV3("span", {
1119
- className: "font-semibold text-foreground",
1120
- children: template.name
1121
- }, undefined, false, undefined, this)
1122
- ]
1123
- }, undefined, true, undefined, this),
1124
- /* @__PURE__ */ jsxDEV3("button", {
1125
- type: "button",
1126
- className: "inline-flex items-center gap-1 rounded-full border border-border px-2 py-0.5 font-semibold text-[11px] text-muted-foreground hover:text-foreground",
1127
- onClick: handleReset,
1128
- disabled: isResetting,
1129
- children: [
1130
- /* @__PURE__ */ jsxDEV3(RefreshCw, {
1131
- className: "h-3 w-3"
1132
- }, undefined, false, undefined, this),
1133
- isResetting ? "Resetting\u2026" : "Reset data"
1134
- ]
1135
- }, undefined, true, undefined, this)
1136
- ]
1137
- }, undefined, true, undefined, this);
1138
- }
1139
-
1140
- // src/markdown/formatPresentationName.ts
1141
- function formatPresentationName(name) {
1142
- const parts = name.split(".");
1143
- const lastPart = parts[parts.length - 1] ?? name;
1144
- return lastPart.split("-").map((word) => word.charAt(0).toUpperCase() + word.slice(1)).join(" ");
1145
- }
1146
-
1147
- // src/markdown/useMarkdownPresentation.ts
1148
- import { useCallback as useCallback4, useEffect as useEffect2, useState as useState3 } from "react";
1149
- "use client";
1150
- function useMarkdownPresentation({
1151
- engine,
1152
- fetchData,
1153
- presentationId,
1154
- presentations,
1155
- resolvePresentation,
1156
- templateId
1157
- }) {
1158
- const [selectedPresentation, setSelectedPresentation] = useState3("");
1159
- const [markdownContent, setMarkdownContent] = useState3("");
1160
- const [loading, setLoading] = useState3(false);
1161
- const [error, setError] = useState3(null);
1162
- useEffect2(() => {
1163
- if (presentationId && presentations.includes(presentationId)) {
1164
- setSelectedPresentation(presentationId);
1165
- return;
1166
- }
1167
- if (presentations.length === 0) {
1168
- setSelectedPresentation("");
1169
- return;
1170
- }
1171
- if (!presentations.includes(selectedPresentation)) {
1172
- setSelectedPresentation(presentations[0] ?? "");
1173
- }
1174
- }, [presentationId, presentations, selectedPresentation, templateId]);
1175
- const renderMarkdown = useCallback4(async () => {
1176
- if (!selectedPresentation || !engine)
1177
- return;
1178
- setLoading(true);
1179
- setError(null);
1180
- try {
1181
- if (!resolvePresentation) {
1182
- throw new Error("resolvePresentation not available in runtime context");
1183
- }
1184
- const descriptor = resolvePresentation(selectedPresentation);
1185
- if (!descriptor) {
1186
- throw new Error(`Presentation descriptor not found: ${selectedPresentation}`);
1187
- }
1188
- const dataResult = await fetchData(selectedPresentation);
1189
- const result = await engine.render("markdown", descriptor, { data: dataResult.data });
1190
- setMarkdownContent(result.body);
1191
- } catch (err) {
1192
- setError(err instanceof Error ? err : new Error("Failed to render markdown"));
1193
- } finally {
1194
- setLoading(false);
1195
- }
1196
- }, [engine, fetchData, resolvePresentation, selectedPresentation]);
1197
- useEffect2(() => {
1198
- renderMarkdown();
1199
- }, [renderMarkdown]);
1200
- return {
1201
- error,
1202
- loading,
1203
- markdownContent,
1204
- renderMarkdown,
1205
- selectedPresentation,
1206
- setSelectedPresentation
1207
- };
1208
- }
1209
-
1210
- // src/MarkdownView.tsx
1211
- import {
1212
- Button as Button3,
1213
- ErrorState,
1214
- LoaderBlock as LoaderBlock2,
1215
- MarkdownRenderer
1216
- } from "@contractspec/lib.design-system";
1217
- import { Badge as Badge3 } from "@contractspec/lib.ui-kit-web/ui/badge";
1218
- import { Card as Card3 } from "@contractspec/lib.ui-kit-web/ui/card";
1219
- import { useCallback as useCallback5 } from "react";
1220
- import { jsxDEV as jsxDEV4 } from "react/jsx-dev-runtime";
1221
- "use client";
1222
- function MarkdownView({
1223
- templateId: propTemplateId,
1224
- presentationId,
1225
- className
1226
- }) {
1227
- const {
1228
- engine,
1229
- template,
1230
- templateId: contextTemplateId,
1231
- resolvePresentation,
1232
- fetchData
1233
- } = useTemplateRuntime();
1234
- const templateId = propTemplateId ?? contextTemplateId;
1235
- const presentations = template?.presentations ?? [];
1236
- const {
1237
- error,
1238
- loading,
1239
- markdownContent,
1240
- renderMarkdown,
1241
- selectedPresentation,
1242
- setSelectedPresentation
1243
- } = useMarkdownPresentation({
1244
- engine,
1245
- fetchData,
1246
- presentationId,
1247
- presentations,
1248
- resolvePresentation,
1249
- templateId
1250
- });
1251
- if (!presentations.length) {
1252
- return /* @__PURE__ */ jsxDEV4(Card3, {
1253
- className,
1254
- children: /* @__PURE__ */ jsxDEV4("div", {
1255
- className: "p-6 text-center",
1256
- children: /* @__PURE__ */ jsxDEV4("p", {
1257
- className: "text-muted-foreground",
1258
- children: "No presentations available for this template."
1259
- }, undefined, false, undefined, this)
1260
- }, undefined, false, undefined, this)
1261
- }, undefined, false, undefined, this);
1262
- }
1263
- const handleCopy = useCallback5(() => {
1264
- if (markdownContent) {
1265
- navigator.clipboard.writeText(markdownContent);
1266
- }
1267
- }, [markdownContent]);
1268
- return /* @__PURE__ */ jsxDEV4("div", {
1269
- className,
1270
- children: [
1271
- /* @__PURE__ */ jsxDEV4("div", {
1272
- className: "mb-4 flex flex-wrap items-center gap-2",
1273
- children: [
1274
- /* @__PURE__ */ jsxDEV4("span", {
1275
- className: "font-medium text-muted-foreground text-sm",
1276
- children: "Presentation:"
1277
- }, undefined, false, undefined, this),
1278
- presentations.map((name) => /* @__PURE__ */ jsxDEV4(Button3, {
1279
- variant: selectedPresentation === name ? "default" : "outline",
1280
- size: "sm",
1281
- onPress: () => setSelectedPresentation(name),
1282
- children: formatPresentationName(name)
1283
- }, name, false, undefined, this)),
1284
- /* @__PURE__ */ jsxDEV4("div", {
1285
- className: "ml-auto flex items-center gap-2",
1286
- children: [
1287
- /* @__PURE__ */ jsxDEV4(Badge3, {
1288
- variant: "secondary",
1289
- children: "LLM-friendly"
1290
- }, undefined, false, undefined, this),
1291
- /* @__PURE__ */ jsxDEV4(Button3, {
1292
- variant: "outline",
1293
- size: "sm",
1294
- onPress: handleCopy,
1295
- disabled: !markdownContent || loading,
1296
- children: "Copy"
1297
- }, undefined, false, undefined, this)
1298
- ]
1299
- }, undefined, true, undefined, this)
1300
- ]
1301
- }, undefined, true, undefined, this),
1302
- /* @__PURE__ */ jsxDEV4(Card3, {
1303
- className: "overflow-hidden",
1304
- children: [
1305
- loading && /* @__PURE__ */ jsxDEV4(LoaderBlock2, {
1306
- label: "Rendering markdown..."
1307
- }, undefined, false, undefined, this),
1308
- error && /* @__PURE__ */ jsxDEV4(ErrorState, {
1309
- title: "Render failed",
1310
- description: error.message,
1311
- onRetry: renderMarkdown,
1312
- retryLabel: "Retry"
1313
- }, undefined, false, undefined, this),
1314
- !loading && !error && markdownContent && /* @__PURE__ */ jsxDEV4("div", {
1315
- className: "p-6",
1316
- children: /* @__PURE__ */ jsxDEV4(MarkdownRenderer, {
1317
- content: markdownContent
1318
- }, undefined, false, undefined, this)
1319
- }, undefined, false, undefined, this),
1320
- !loading && !error && !markdownContent && /* @__PURE__ */ jsxDEV4("div", {
1321
- className: "p-6 text-center",
1322
- children: /* @__PURE__ */ jsxDEV4("p", {
1323
- className: "text-muted-foreground",
1324
- children: "Select a presentation to view its markdown output."
1325
- }, undefined, false, undefined, this)
1326
- }, undefined, false, undefined, this)
1327
- ]
1328
- }, undefined, true, undefined, this)
1329
- ]
1330
- }, undefined, true, undefined, this);
1331
- }
1332
-
1333
- // src/OverlayContextProvider.tsx
1334
- import * as React from "react";
1335
- import { useContext as useContext2, useMemo as useMemo4 } from "react";
1336
- import { jsxDEV as jsxDEV5 } from "react/jsx-dev-runtime";
1337
- "use client";
1338
- var OverlayContext = React.createContext(null);
1339
- function OverlayContextProvider({
1340
- templateId,
1341
- role = "user",
1342
- device = "desktop",
1343
- children
1344
- }) {
1345
- const overlays = useMemo4(() => getTemplateOverlays(templateId, role), [templateId, role]);
1346
- const activeOverlays = useMemo4(() => {
1347
- return overlays.filter((overlay) => {
1348
- const conditions = overlay.conditions;
1349
- if (!conditions)
1350
- return true;
1351
- if (conditions.role && !conditions.role.includes(role)) {
1352
- return false;
1353
- }
1354
- if (conditions.device && conditions.device !== "any" && conditions.device !== device) {
1355
- return false;
1356
- }
1357
- return true;
1358
- });
1359
- }, [overlays, role, device]);
1360
- const overlayMap = useMemo4(() => {
1361
- const map = new Map;
1362
- for (const overlay of activeOverlays) {
1363
- map.set(overlay.target, overlay);
1364
- }
1365
- return map;
1366
- }, [activeOverlays]);
1367
- const applyOverlay = useMemo4(() => (path, target) => {
1368
- const overlay = overlayMap.get(path);
1369
- if (!overlay)
1370
- return target;
1371
- let result = { ...target };
1372
- for (const mod of overlay.modifications) {
1373
- switch (mod.op) {
1374
- case "hide":
1375
- result = { ...result, hidden: true };
1376
- break;
1377
- case "relabel":
1378
- result = { ...result, label: mod.label };
1379
- break;
1380
- case "reorder":
1381
- result = { ...result, position: mod.position };
1382
- break;
1383
- case "restyle":
1384
- result = {
1385
- ...result,
1386
- className: mod.className,
1387
- variant: mod.variant
1388
- };
1389
- break;
1390
- case "set-default":
1391
- result = { ...result, defaultValue: mod.value };
1392
- break;
1393
- }
1394
- }
1395
- return result;
1396
- }, [overlayMap]);
1397
- const isHidden = useMemo4(() => (path) => {
1398
- const overlay = overlayMap.get(path);
1399
- return overlay?.modifications.some((m) => m.op === "hide") ?? false;
1400
- }, [overlayMap]);
1401
- const getLabel = useMemo4(() => (path, defaultLabel) => {
1402
- const overlay = overlayMap.get(path);
1403
- const relabel = overlay?.modifications.find((m) => m.op === "relabel");
1404
- return relabel && relabel.op === "relabel" ? relabel.label : defaultLabel;
1405
- }, [overlayMap]);
1406
- const getPosition = useMemo4(() => (path, defaultPosition) => {
1407
- const overlay = overlayMap.get(path);
1408
- const reorder = overlay?.modifications.find((m) => m.op === "reorder");
1409
- return reorder && reorder.op === "reorder" ? reorder.position : defaultPosition;
1410
- }, [overlayMap]);
1411
- const getStyle = useMemo4(() => (path) => {
1412
- const overlay = overlayMap.get(path);
1413
- const restyle = overlay?.modifications.find((m) => m.op === "restyle");
1414
- if (restyle && restyle.op === "restyle") {
1415
- return {
1416
- className: restyle.className,
1417
- variant: restyle.variant
1418
- };
1419
- }
1420
- return {};
1421
- }, [overlayMap]);
1422
- const getDefault = useMemo4(() => (path, defaultValue) => {
1423
- const overlay = overlayMap.get(path);
1424
- const setDefault = overlay?.modifications.find((m) => m.op === "set-default");
1425
- return setDefault && setDefault.op === "set-default" ? setDefault.value : defaultValue;
1426
- }, [overlayMap]);
1427
- const value = useMemo4(() => ({
1428
- overlays: activeOverlays,
1429
- applyOverlay,
1430
- isHidden,
1431
- getLabel,
1432
- getPosition,
1433
- getStyle,
1434
- getDefault,
1435
- role,
1436
- device
1437
- }), [
1438
- activeOverlays,
1439
- applyOverlay,
1440
- isHidden,
1441
- getLabel,
1442
- getPosition,
1443
- getStyle,
1444
- getDefault,
1445
- role,
1446
- device
1447
- ]);
1448
- return /* @__PURE__ */ jsxDEV5(OverlayContext.Provider, {
1449
- value,
1450
- children
1451
- }, undefined, false, undefined, this);
1452
- }
1453
- function useOverlayContext() {
1454
- const context = useContext2(OverlayContext);
1455
- if (!context) {
1456
- throw new Error("useOverlayContext must be used within an OverlayContextProvider");
1457
- }
1458
- return context;
1459
- }
1460
- function useIsInOverlayContext() {
1461
- return useContext2(OverlayContext) !== null;
1462
- }
1463
- function getTemplateOverlays(templateId, _role) {
1464
- const templateOverlays = {
1465
- "crm-pipeline": [
1466
- {
1467
- id: "crm-hide-internal-fields",
1468
- target: "deal.internalNotes",
1469
- modifications: [{ op: "hide" }],
1470
- conditions: { role: ["viewer", "user"] }
1471
- },
1472
- {
1473
- id: "crm-relabel-value",
1474
- target: "deal.value",
1475
- modifications: [{ op: "relabel", label: "Deal Amount" }]
1476
- }
1477
- ],
1478
- "saas-boilerplate": [
1479
- {
1480
- id: "saas-hide-billing",
1481
- target: "settings.billing",
1482
- modifications: [{ op: "hide" }],
1483
- conditions: { role: ["viewer"] }
1484
- },
1485
- {
1486
- id: "saas-restyle-plan",
1487
- target: "settings.plan",
1488
- modifications: [{ op: "restyle", variant: "premium" }],
1489
- conditions: { role: ["admin"] }
1490
- }
1491
- ],
1492
- "agent-console": [
1493
- {
1494
- id: "agent-hide-cost",
1495
- target: "run.cost",
1496
- modifications: [{ op: "hide" }],
1497
- conditions: { role: ["viewer"] }
1498
- },
1499
- {
1500
- id: "agent-relabel-tokens",
1501
- target: "run.tokens",
1502
- modifications: [{ op: "relabel", label: "Token Usage" }]
1503
- }
1504
- ],
1505
- "todos-app": [
1506
- {
1507
- id: "todos-hide-assignee",
1508
- target: "task.assignee",
1509
- modifications: [{ op: "hide" }],
1510
- conditions: { device: "mobile" }
1511
- }
1512
- ],
1513
- "messaging-app": [
1514
- {
1515
- id: "messaging-reorder-timestamp",
1516
- target: "message.timestamp",
1517
- modifications: [{ op: "reorder", position: 0 }]
1518
- }
1519
- ],
1520
- "recipe-app-i18n": [
1521
- {
1522
- id: "recipe-relabel-servings",
1523
- target: "recipe.servings",
1524
- modifications: [{ op: "relabel", label: "Portions" }]
1525
- }
1526
- ]
1527
- };
1528
- return templateOverlays[templateId] ?? [];
1529
- }
1530
-
1531
- // src/hooks/useBehaviorTracking.ts
1532
- import { useCallback as useCallback6, useEffect as useEffect3, useMemo as useMemo5, useRef as useRef2, useState as useState4 } from "react";
1533
- "use client";
1534
- var BEHAVIOR_STORAGE_KEY = "contractspec-behavior-data";
1535
- var ALL_FEATURES = [
1536
- "playground",
1537
- "specs",
1538
- "builder",
1539
- "markdown",
1540
- "evolution",
1541
- "canvas_add",
1542
- "canvas_delete",
1543
- "spec_save",
1544
- "spec_validate",
1545
- "ai_suggestions"
1546
- ];
1547
- function useBehaviorTracking(templateId) {
1548
- const [events, setEvents] = useState4([]);
1549
- const sessionStartRef = useRef2(new Date);
1550
- const [eventCount, setEventCount] = useState4(0);
1551
- useEffect3(() => {
1552
- try {
1553
- const stored = localStorage.getItem(BEHAVIOR_STORAGE_KEY);
1554
- if (stored) {
1555
- const data = JSON.parse(stored);
1556
- setEvents(data.events.map((e) => ({
1557
- ...e,
1558
- timestamp: new Date(e.timestamp)
1559
- })));
1560
- sessionStartRef.current = new Date(data.sessionStart);
1561
- }
1562
- } catch {}
1563
- }, []);
1564
- useEffect3(() => {
1565
- if (events.length > 0) {
1566
- try {
1567
- localStorage.setItem(BEHAVIOR_STORAGE_KEY, JSON.stringify({
1568
- events: events.map((e) => ({
1569
- ...e,
1570
- timestamp: e.timestamp.toISOString()
1571
- })),
1572
- sessionStart: sessionStartRef.current.toISOString()
1573
- }));
1574
- } catch {}
1575
- }
1576
- }, [events]);
1577
- const trackEvent = useCallback6((type, metadata) => {
1578
- const event = {
1579
- type,
1580
- timestamp: new Date,
1581
- templateId,
1582
- metadata
1583
- };
1584
- setEvents((prev) => [...prev, event]);
1585
- setEventCount((prev) => prev + 1);
1586
- }, [templateId]);
1587
- const getEventsByType = useCallback6((type) => {
1588
- return events.filter((e) => e.type === type);
1589
- }, [events]);
1590
- const getSummary = useCallback6(() => {
1591
- const now = new Date;
1592
- const sessionDuration = now.getTime() - sessionStartRef.current.getTime();
1593
- const templateCounts = new Map;
1594
- for (const event of events) {
1595
- const count = templateCounts.get(event.templateId) ?? 0;
1596
- templateCounts.set(event.templateId, count + 1);
1597
- }
1598
- const mostUsedTemplates = Array.from(templateCounts.entries()).map(([templateId2, count]) => ({ templateId: templateId2, count })).sort((a, b) => b.count - a.count).slice(0, 3);
1599
- const modeCounts = new Map;
1600
- for (const event of events) {
1601
- if (event.type === "mode_change" && event.metadata?.mode) {
1602
- const mode = event.metadata.mode;
1603
- const count = modeCounts.get(mode) ?? 0;
1604
- modeCounts.set(mode, count + 1);
1605
- }
1606
- }
1607
- const mostUsedModes = Array.from(modeCounts.entries()).map(([mode, count]) => ({ mode, count })).sort((a, b) => b.count - a.count);
1608
- const featuresUsed = new Set;
1609
- for (const event of events) {
1610
- if (event.type === "mode_change" && event.metadata?.mode) {
1611
- featuresUsed.add(event.metadata.mode);
1612
- }
1613
- if (event.type === "feature_usage" && event.metadata?.feature) {
1614
- featuresUsed.add(event.metadata.feature);
1615
- }
1616
- if (event.type === "canvas_interaction") {
1617
- const action = event.metadata?.action;
1618
- if (action === "add")
1619
- featuresUsed.add("canvas_add");
1620
- if (action === "delete")
1621
- featuresUsed.add("canvas_delete");
1622
- }
1623
- if (event.type === "spec_edit") {
1624
- const action = event.metadata?.action;
1625
- if (action === "save")
1626
- featuresUsed.add("spec_save");
1627
- if (action === "validate")
1628
- featuresUsed.add("spec_validate");
1629
- }
1630
- }
1631
- const unusedFeatures = ALL_FEATURES.filter((f) => !featuresUsed.has(f));
1632
- const errorCount = events.filter((e) => e.type === "error").length;
1633
- const recommendations = generateRecommendations(Array.from(featuresUsed), unusedFeatures, mostUsedModes, events.length);
1634
- return {
1635
- totalEvents: events.length,
1636
- sessionDuration,
1637
- mostUsedTemplates,
1638
- mostUsedModes,
1639
- featuresUsed: Array.from(featuresUsed),
1640
- unusedFeatures,
1641
- errorCount,
1642
- recommendations
1643
- };
1644
- }, [events]);
1645
- const clear = useCallback6(() => {
1646
- setEvents([]);
1647
- setEventCount(0);
1648
- sessionStartRef.current = new Date;
1649
- localStorage.removeItem(BEHAVIOR_STORAGE_KEY);
1650
- }, []);
1651
- return useMemo5(() => ({
1652
- trackEvent,
1653
- getSummary,
1654
- getEventsByType,
1655
- eventCount,
1656
- sessionStart: sessionStartRef.current,
1657
- clear
1658
- }), [trackEvent, getSummary, getEventsByType, eventCount, clear]);
1659
- }
1660
- function generateRecommendations(featuresUsed, unusedFeatures, mostUsedModes, totalEvents) {
1661
- const recommendations = [];
1662
- if (unusedFeatures.includes("evolution")) {
1663
- recommendations.push("Try the AI Evolution mode to get automated improvement suggestions");
1664
- }
1665
- if (unusedFeatures.includes("markdown")) {
1666
- recommendations.push("Use Markdown preview to see documentation for your specs");
1667
- }
1668
- if (unusedFeatures.includes("builder")) {
1669
- recommendations.push("Explore the Visual Builder to design your UI components");
1670
- }
1671
- if (!featuresUsed.includes("spec_validate") && featuresUsed.includes("specs")) {
1672
- recommendations.push("Don't forget to validate your specs before saving");
1673
- }
1674
- if (featuresUsed.includes("evolution") && !featuresUsed.includes("ai_suggestions")) {
1675
- recommendations.push("Generate AI suggestions to get actionable improvement recommendations");
1676
- }
1677
- if (totalEvents > 50) {
1678
- recommendations.push("Great engagement! Consider saving your work regularly");
1679
- }
1680
- if (mostUsedModes.length === 1) {
1681
- recommendations.push("Try different modes to explore all sandbox capabilities");
1682
- }
1683
- return recommendations;
1684
- }
1685
-
1686
- // src/PersonalizationInsights.tsx
1687
- import { Button as Button4 } from "@contractspec/lib.design-system";
1688
- import { Badge as Badge4 } from "@contractspec/lib.ui-kit-web/ui/badge";
1689
- import { Card as Card4 } from "@contractspec/lib.ui-kit-web/ui/card";
1690
- import { useCallback as useCallback7, useMemo as useMemo6, useState as useState5 } from "react";
1691
- import { jsxDEV as jsxDEV6 } from "react/jsx-dev-runtime";
1692
- "use client";
1693
- function PersonalizationInsights({
1694
- templateId,
1695
- collapsed = false,
1696
- onToggle
1697
- }) {
1698
- const { getSummary, eventCount, clear, sessionStart } = useBehaviorTracking(templateId);
1699
- const [showDetails, setShowDetails] = useState5(false);
1700
- const summary = useMemo6(() => getSummary(), [getSummary]);
1701
- const formatDuration = useCallback7((ms) => {
1702
- const seconds = Math.floor(ms / 1000);
1703
- const minutes = Math.floor(seconds / 60);
1704
- const hours = Math.floor(minutes / 60);
1705
- if (hours > 0) {
1706
- return `${hours}h ${minutes % 60}m`;
1707
- }
1708
- if (minutes > 0) {
1709
- return `${minutes}m ${seconds % 60}s`;
1710
- }
1711
- return `${seconds}s`;
1712
- }, []);
1713
- const handleClear = useCallback7(() => {
1714
- clear();
1715
- }, [clear]);
1716
- if (collapsed) {
1717
- return /* @__PURE__ */ jsxDEV6("button", {
1718
- onClick: onToggle,
1719
- className: "flex items-center gap-2 rounded-lg border border-blue-500/30 bg-blue-500/10 px-3 py-2 text-sm transition hover:bg-blue-500/20",
1720
- type: "button",
1721
- children: [
1722
- /* @__PURE__ */ jsxDEV6("span", {
1723
- children: "\uD83D\uDCCA"
1724
- }, undefined, false, undefined, this),
1725
- /* @__PURE__ */ jsxDEV6("span", {
1726
- children: "Insights"
1727
- }, undefined, false, undefined, this),
1728
- /* @__PURE__ */ jsxDEV6(Badge4, {
1729
- variant: "secondary",
1730
- children: eventCount
1731
- }, undefined, false, undefined, this)
1732
- ]
1733
- }, undefined, true, undefined, this);
1734
- }
1735
- return /* @__PURE__ */ jsxDEV6(Card4, {
1736
- className: "overflow-hidden",
1737
- children: [
1738
- /* @__PURE__ */ jsxDEV6("div", {
1739
- className: "flex items-center justify-between border-blue-500/20 border-b bg-blue-500/5 px-4 py-3",
1740
- children: [
1741
- /* @__PURE__ */ jsxDEV6("div", {
1742
- className: "flex items-center gap-2",
1743
- children: [
1744
- /* @__PURE__ */ jsxDEV6("span", {
1745
- children: "\uD83D\uDCCA"
1746
- }, undefined, false, undefined, this),
1747
- /* @__PURE__ */ jsxDEV6("span", {
1748
- className: "font-semibold",
1749
- children: "Personalization Insights"
1750
- }, undefined, false, undefined, this)
1751
- ]
1752
- }, undefined, true, undefined, this),
1753
- /* @__PURE__ */ jsxDEV6("div", {
1754
- className: "flex items-center gap-2",
1755
- children: [
1756
- /* @__PURE__ */ jsxDEV6(Button4, {
1757
- variant: "ghost",
1758
- size: "sm",
1759
- onPress: () => setShowDetails(!showDetails),
1760
- children: showDetails ? "Hide Details" : "Show Details"
1761
- }, undefined, false, undefined, this),
1762
- onToggle && /* @__PURE__ */ jsxDEV6("button", {
1763
- onClick: onToggle,
1764
- className: "p-1 text-muted-foreground hover:text-foreground",
1765
- type: "button",
1766
- title: "Collapse",
1767
- children: "\u2715"
1768
- }, undefined, false, undefined, this)
1769
- ]
1770
- }, undefined, true, undefined, this)
1771
- ]
1772
- }, undefined, true, undefined, this),
1773
- /* @__PURE__ */ jsxDEV6("div", {
1774
- className: "p-4",
1775
- children: [
1776
- /* @__PURE__ */ jsxDEV6("div", {
1777
- className: "mb-4 grid grid-cols-2 gap-3 md:grid-cols-4",
1778
- children: [
1779
- /* @__PURE__ */ jsxDEV6(StatCard, {
1780
- label: "Session Time",
1781
- value: formatDuration(summary.sessionDuration),
1782
- icon: "\u23F1\uFE0F"
1783
- }, undefined, false, undefined, this),
1784
- /* @__PURE__ */ jsxDEV6(StatCard, {
1785
- label: "Events Tracked",
1786
- value: summary.totalEvents.toString(),
1787
- icon: "\uD83D\uDCC8"
1788
- }, undefined, false, undefined, this),
1789
- /* @__PURE__ */ jsxDEV6(StatCard, {
1790
- label: "Features Used",
1791
- value: `${summary.featuresUsed.length}/${summary.featuresUsed.length + summary.unusedFeatures.length}`,
1792
- icon: "\u2728"
1793
- }, undefined, false, undefined, this),
1794
- /* @__PURE__ */ jsxDEV6(StatCard, {
1795
- label: "Errors",
1796
- value: summary.errorCount.toString(),
1797
- icon: "\u26A0\uFE0F",
1798
- variant: summary.errorCount > 0 ? "warning" : "success"
1799
- }, undefined, false, undefined, this)
1800
- ]
1801
- }, undefined, true, undefined, this),
1802
- summary.recommendations.length > 0 && /* @__PURE__ */ jsxDEV6("div", {
1803
- className: "mb-4",
1804
- children: [
1805
- /* @__PURE__ */ jsxDEV6("h4", {
1806
- className: "mb-2 font-semibold text-blue-400 text-xs uppercase",
1807
- children: "Recommendations"
1808
- }, undefined, false, undefined, this),
1809
- /* @__PURE__ */ jsxDEV6("ul", {
1810
- className: "space-y-1",
1811
- children: summary.recommendations.map((rec, index) => /* @__PURE__ */ jsxDEV6("li", {
1812
- className: "flex items-start gap-2 text-sm",
1813
- children: [
1814
- /* @__PURE__ */ jsxDEV6("span", {
1815
- className: "text-blue-400",
1816
- children: "\uD83D\uDCA1"
1817
- }, undefined, false, undefined, this),
1818
- /* @__PURE__ */ jsxDEV6("span", {
1819
- children: rec
1820
- }, undefined, false, undefined, this)
1821
- ]
1822
- }, index, true, undefined, this))
1823
- }, undefined, false, undefined, this)
1824
- ]
1825
- }, undefined, true, undefined, this),
1826
- summary.unusedFeatures.length > 0 && /* @__PURE__ */ jsxDEV6("div", {
1827
- className: "mb-4",
1828
- children: [
1829
- /* @__PURE__ */ jsxDEV6("h4", {
1830
- className: "mb-2 font-semibold text-blue-400 text-xs uppercase",
1831
- children: "Try These Features"
1832
- }, undefined, false, undefined, this),
1833
- /* @__PURE__ */ jsxDEV6("div", {
1834
- className: "flex flex-wrap gap-2",
1835
- children: summary.unusedFeatures.slice(0, 5).map((feature) => /* @__PURE__ */ jsxDEV6(Badge4, {
1836
- variant: "secondary",
1837
- children: formatFeatureName(feature)
1838
- }, feature, false, undefined, this))
1839
- }, undefined, false, undefined, this)
1840
- ]
1841
- }, undefined, true, undefined, this),
1842
- showDetails && /* @__PURE__ */ jsxDEV6(DetailedInsights, {
1843
- summary,
1844
- sessionStart
1845
- }, undefined, false, undefined, this),
1846
- /* @__PURE__ */ jsxDEV6("div", {
1847
- className: "mt-4 flex justify-end border-blue-500/10 border-t pt-4",
1848
- children: /* @__PURE__ */ jsxDEV6(Button4, {
1849
- variant: "ghost",
1850
- size: "sm",
1851
- onPress: handleClear,
1852
- children: "Clear Data"
1853
- }, undefined, false, undefined, this)
1854
- }, undefined, false, undefined, this)
1855
- ]
1856
- }, undefined, true, undefined, this)
1857
- ]
1858
- }, undefined, true, undefined, this);
1859
- }
1860
- function StatCard({
1861
- label,
1862
- value,
1863
- icon,
1864
- variant = "default"
1865
- }) {
1866
- const bgColors = {
1867
- default: "bg-blue-500/5 border-blue-500/20",
1868
- warning: "bg-amber-500/5 border-amber-500/20",
1869
- success: "bg-green-500/5 border-green-500/20"
1870
- };
1871
- return /* @__PURE__ */ jsxDEV6("div", {
1872
- className: `rounded-lg border p-3 text-center ${bgColors[variant]}`,
1873
- children: [
1874
- /* @__PURE__ */ jsxDEV6("div", {
1875
- className: "mb-1 text-lg",
1876
- children: icon
1877
- }, undefined, false, undefined, this),
1878
- /* @__PURE__ */ jsxDEV6("div", {
1879
- className: "font-bold text-lg",
1880
- children: value
1881
- }, undefined, false, undefined, this),
1882
- /* @__PURE__ */ jsxDEV6("div", {
1883
- className: "text-muted-foreground text-xs",
1884
- children: label
1885
- }, undefined, false, undefined, this)
1886
- ]
1887
- }, undefined, true, undefined, this);
1888
- }
1889
- function DetailedInsights({
1890
- summary,
1891
- sessionStart
1892
- }) {
1893
- return /* @__PURE__ */ jsxDEV6("div", {
1894
- className: "mt-4 space-y-4 border-blue-500/10 border-t pt-4",
1895
- children: [
1896
- summary.mostUsedTemplates.length > 0 && /* @__PURE__ */ jsxDEV6("div", {
1897
- children: [
1898
- /* @__PURE__ */ jsxDEV6("h4", {
1899
- className: "mb-2 font-semibold text-blue-400 text-xs uppercase",
1900
- children: "Most Used Templates"
1901
- }, undefined, false, undefined, this),
1902
- /* @__PURE__ */ jsxDEV6("div", {
1903
- className: "space-y-1",
1904
- children: summary.mostUsedTemplates.map(({ templateId, count }) => /* @__PURE__ */ jsxDEV6("div", {
1905
- className: "flex items-center justify-between text-sm",
1906
- children: [
1907
- /* @__PURE__ */ jsxDEV6("span", {
1908
- children: formatTemplateId(templateId)
1909
- }, undefined, false, undefined, this),
1910
- /* @__PURE__ */ jsxDEV6("span", {
1911
- className: "text-muted-foreground",
1912
- children: [
1913
- count,
1914
- " events"
1915
- ]
1916
- }, undefined, true, undefined, this)
1917
- ]
1918
- }, templateId, true, undefined, this))
1919
- }, undefined, false, undefined, this)
1920
- ]
1921
- }, undefined, true, undefined, this),
1922
- summary.mostUsedModes.length > 0 && /* @__PURE__ */ jsxDEV6("div", {
1923
- children: [
1924
- /* @__PURE__ */ jsxDEV6("h4", {
1925
- className: "mb-2 font-semibold text-blue-400 text-xs uppercase",
1926
- children: "Mode Usage"
1927
- }, undefined, false, undefined, this),
1928
- /* @__PURE__ */ jsxDEV6("div", {
1929
- className: "space-y-1",
1930
- children: summary.mostUsedModes.map(({ mode, count }) => /* @__PURE__ */ jsxDEV6("div", {
1931
- className: "flex items-center justify-between text-sm",
1932
- children: [
1933
- /* @__PURE__ */ jsxDEV6("span", {
1934
- children: formatFeatureName(mode)
1935
- }, undefined, false, undefined, this),
1936
- /* @__PURE__ */ jsxDEV6("span", {
1937
- className: "text-muted-foreground",
1938
- children: [
1939
- count,
1940
- " switches"
1941
- ]
1942
- }, undefined, true, undefined, this)
1943
- ]
1944
- }, mode, true, undefined, this))
1945
- }, undefined, false, undefined, this)
1946
- ]
1947
- }, undefined, true, undefined, this),
1948
- /* @__PURE__ */ jsxDEV6("div", {
1949
- children: [
1950
- /* @__PURE__ */ jsxDEV6("h4", {
1951
- className: "mb-2 font-semibold text-blue-400 text-xs uppercase",
1952
- children: "Features Used"
1953
- }, undefined, false, undefined, this),
1954
- /* @__PURE__ */ jsxDEV6("div", {
1955
- className: "flex flex-wrap gap-2",
1956
- children: summary.featuresUsed.map((feature) => /* @__PURE__ */ jsxDEV6(Badge4, {
1957
- variant: "default",
1958
- className: "border-green-500/30 bg-green-500/20 text-green-400",
1959
- children: [
1960
- "\u2713 ",
1961
- formatFeatureName(feature)
1962
- ]
1963
- }, feature, true, undefined, this))
1964
- }, undefined, false, undefined, this)
1965
- ]
1966
- }, undefined, true, undefined, this),
1967
- /* @__PURE__ */ jsxDEV6("div", {
1968
- className: "text-muted-foreground text-xs",
1969
- children: [
1970
- "Session started: ",
1971
- sessionStart.toLocaleString()
1972
- ]
1973
- }, undefined, true, undefined, this)
1974
- ]
1975
- }, undefined, true, undefined, this);
1976
- }
1977
- function formatFeatureName(feature) {
1978
- return feature.replace(/_/g, " ").replace(/\b\w/g, (c) => c.toUpperCase());
1979
- }
1980
- function formatTemplateId(id) {
1981
- return id.replace(/-/g, " ").replace(/\b\w/g, (c) => c.toUpperCase());
1982
- }
1983
-
1984
- // src/SaveToStudioButton.tsx
1985
- import { Sparkles } from "lucide-react";
1986
- import { useState as useState6 } from "react";
1987
- import { jsxDEV as jsxDEV7 } from "react/jsx-dev-runtime";
1988
- "use client";
1989
- function SaveToStudioButton({
1990
- organizationId = "demo-org",
1991
- projectName,
1992
- endpoint,
1993
- token
1994
- }) {
1995
- const { installer, templateId, template } = useTemplateRuntime();
1996
- const [status, setStatus] = useState6("idle");
1997
- const [error, setError] = useState6(null);
1998
- const handleSave = async () => {
1999
- setStatus("saving");
2000
- setError(null);
2001
- try {
2002
- await installer.saveToStudio({
2003
- templateId,
2004
- projectName: projectName ?? `${template.name} demo`,
2005
- organizationId,
2006
- endpoint,
2007
- token
2008
- });
2009
- setStatus("saved");
2010
- setTimeout(() => setStatus("idle"), 3000);
2011
- } catch (err) {
2012
- setStatus("error");
2013
- setError(err instanceof Error ? err.message : "Unknown error");
2014
- }
2015
- };
2016
- return /* @__PURE__ */ jsxDEV7("div", {
2017
- className: "flex flex-col items-end gap-1",
2018
- children: [
2019
- /* @__PURE__ */ jsxDEV7("button", {
2020
- type: "button",
2021
- className: "btn-primary inline-flex items-center gap-2 text-sm",
2022
- onClick: handleSave,
2023
- disabled: status === "saving",
2024
- children: [
2025
- /* @__PURE__ */ jsxDEV7(Sparkles, {
2026
- className: "h-4 w-4"
2027
- }, undefined, false, undefined, this),
2028
- status === "saving" ? "Publishing\u2026" : "Save to Studio"
2029
- ]
2030
- }, undefined, true, undefined, this),
2031
- status === "error" && error ? /* @__PURE__ */ jsxDEV7("p", {
2032
- className: "text-destructive text-xs",
2033
- children: error
2034
- }, undefined, false, undefined, this) : null,
2035
- status === "saved" ? /* @__PURE__ */ jsxDEV7("p", {
2036
- className: "text-emerald-400 text-xs",
2037
- children: "Template sent to Studio."
2038
- }, undefined, false, undefined, this) : null
2039
- ]
2040
- }, undefined, true, undefined, this);
2041
- }
2042
-
2043
- // src/SpecDrivenTemplateShell.tsx
2044
- import {
2045
- BundleProvider,
2046
- BundleRenderer
2047
- } from "@contractspec/lib.surface-runtime/react";
2048
- import { jsxDEV as jsxDEV8 } from "react/jsx-dev-runtime";
2049
- function SpecDrivenTemplateShell({
2050
- plan,
2051
- title,
2052
- description,
2053
- sidebar,
2054
- actions,
2055
- showSaveAction = true,
2056
- saveProps,
2057
- children
2058
- }) {
2059
- const headerContent = /* @__PURE__ */ jsxDEV8("header", {
2060
- className: "rounded-2xl border border-border bg-card p-6 shadow-sm",
2061
- children: [
2062
- /* @__PURE__ */ jsxDEV8("div", {
2063
- className: "flex flex-wrap items-center justify-between gap-4",
2064
- children: [
2065
- /* @__PURE__ */ jsxDEV8("div", {
2066
- children: [
2067
- /* @__PURE__ */ jsxDEV8("p", {
2068
- className: "font-semibold text-muted-foreground text-sm uppercase tracking-wide",
2069
- children: "ContractSpec Templates"
2070
- }, undefined, false, undefined, this),
2071
- /* @__PURE__ */ jsxDEV8("h1", {
2072
- className: "font-bold text-3xl",
2073
- children: title
2074
- }, undefined, false, undefined, this),
2075
- description ? /* @__PURE__ */ jsxDEV8("p", {
2076
- className: "mt-2 max-w-2xl text-muted-foreground text-sm",
2077
- children: description
2078
- }, undefined, false, undefined, this) : null
2079
- ]
2080
- }, undefined, true, undefined, this),
2081
- /* @__PURE__ */ jsxDEV8("div", {
2082
- className: "flex flex-col items-end gap-2",
2083
- children: [
2084
- /* @__PURE__ */ jsxDEV8(LocalDataIndicator, {}, undefined, false, undefined, this),
2085
- showSaveAction ? /* @__PURE__ */ jsxDEV8(SaveToStudioButton, {
2086
- ...saveProps
2087
- }, undefined, false, undefined, this) : null
2088
- ]
2089
- }, undefined, true, undefined, this)
2090
- ]
2091
- }, undefined, true, undefined, this),
2092
- actions ? /* @__PURE__ */ jsxDEV8("div", {
2093
- className: "mt-4",
2094
- children: actions
2095
- }, undefined, false, undefined, this) : null
2096
- ]
2097
- }, undefined, true, undefined, this);
2098
- const slotContent = {
2099
- header: headerContent,
2100
- primary: /* @__PURE__ */ jsxDEV8("main", {
2101
- className: "space-y-4 p-2",
2102
- children
2103
- }, undefined, false, undefined, this)
2104
- };
2105
- if (sidebar != null) {
2106
- slotContent.sidebar = /* @__PURE__ */ jsxDEV8("aside", {
2107
- className: "rounded-2xl border border-border bg-card p-4",
2108
- children: sidebar
2109
- }, undefined, false, undefined, this);
2110
- }
2111
- return /* @__PURE__ */ jsxDEV8(BundleProvider, {
2112
- plan,
2113
- children: /* @__PURE__ */ jsxDEV8(BundleRenderer, {
2114
- slotContent
2115
- }, undefined, false, undefined, this)
2116
- }, undefined, false, undefined, this);
2117
- }
2118
-
2119
- // src/utils/generateSpecFromTemplate.ts
2120
- function generateSpecFromTemplate(template) {
2121
- const templateId = template?.id ?? "unknown";
2122
- if (!template) {
2123
- return generateDefaultSpec(templateId);
2124
- }
2125
- switch (templateId) {
2126
- case "crm-pipeline":
2127
- return generateCrmPipelineSpec(template.schema.contracts);
2128
- case "saas-boilerplate":
2129
- return generateSaasBoilerplateSpec(template.schema.contracts);
2130
- case "agent-console":
2131
- return generateAgentConsoleSpec(template.schema.contracts);
2132
- case "todos-app":
2133
- return generateTodosSpec(template.schema.contracts);
2134
- case "messaging-app":
2135
- return generateMessagingSpec(template.schema.contracts);
2136
- case "recipe-app-i18n":
2137
- return generateRecipeSpec(template.schema.contracts);
2138
- default:
2139
- return generateDefaultSpec(templateId);
2140
- }
2141
- }
2142
- function generateCrmPipelineSpec(contracts) {
2143
- return `// CRM Pipeline Specs
2144
- // Contracts: ${contracts.join(", ")}
2
+ import{useCallback as d,useEffect as B0,useMemo as $1,useRef as N1,useState as m}from"react";var D0="contractspec-evolution-data";function q0($){let[N,q]=m([]),[J,X]=m([]),[K,z]=m([]),[U,w]=m([]),[V,f]=m(!1),F=N1([]),[Y,b]=m(0);B0(()=>{try{let P=localStorage.getItem(`${D0}-${$}`);if(P){let G=JSON.parse(P);z(G.suggestions.map((M)=>({...M,createdAt:new Date(M.createdAt)})))}}catch{}},[$]),B0(()=>{try{localStorage.setItem(`${D0}-${$}`,JSON.stringify({suggestions:K}))}catch{}},[K,$]);let W=d((P,G,M,E)=>{let u={operation:{name:P,version:"1.0.0",tenantId:"sandbox"},durationMs:G,success:M,timestamp:new Date,errorCode:E};F.current.push(u),b((B)=>B+1)},[]),Q=d(()=>{let P=F.current;if(P.length<5)return;let G=new Map;for(let B of P){let y=`${B.operation.name}.v${B.operation.version}`,a=G.get(y)??[];a.push(B),G.set(y,a)}let M=[],E=[];G.forEach((B)=>{if(B.length<3)return;let y=B.map((k)=>k.durationMs).sort((k,x)=>k-x),a=B.filter((k)=>!k.success),F0=B.length,j=a.length/F0,e0=y.reduce((k,x)=>k+x,0)/F0,Y0=B.map((k)=>k.timestamp.getTime()),O0=B[0];if(!O0)return;let c={operation:O0.operation,totalCalls:F0,successRate:1-j,errorRate:j,averageLatencyMs:e0,p95LatencyMs:L0(y,0.95),p99LatencyMs:L0(y,0.99),maxLatencyMs:Math.max(...y),lastSeenAt:new Date(Math.max(...Y0)),windowStart:new Date(Math.min(...Y0)),windowEnd:new Date(Math.max(...Y0)),topErrors:a.reduce((k,x)=>{if(x.errorCode)k[x.errorCode]=(k[x.errorCode]??0)+1;return k},{})};if(M.push(c),j>0.1)E.push({operation:c.operation,severity:j>0.3?"high":j>0.2?"medium":"low",metric:"error-rate",description:`Error rate ${(j*100).toFixed(1)}% exceeds threshold`,detectedAt:new Date,threshold:0.1,observedValue:j});if(c.p99LatencyMs>500)E.push({operation:c.operation,severity:c.p99LatencyMs>1000?"high":c.p99LatencyMs>750?"medium":"low",metric:"latency",description:`P99 latency ${c.p99LatencyMs.toFixed(0)}ms exceeds threshold`,detectedAt:new Date,threshold:500,observedValue:c.p99LatencyMs})}),q(M),X(E);let u=E.map((B)=>({operation:B.operation,category:B.metric==="latency"?"performance":"error-handling",summary:B.metric==="latency"?"Latency regression detected":"Error spike detected",justification:B.description,recommendedActions:B.metric==="latency"?["Add caching layer","Optimize database queries","Consider pagination"]:["Add retry logic","Improve error handling","Add circuit breaker"]}));w(u)},[]),H=d(async()=>{if(J.length===0)return;f(!0),await new Promise((G)=>setTimeout(G,800));let P=J.map((G)=>({id:`suggestion-${Date.now()}-${Math.random().toString(36).slice(2,9)}`,intent:{id:`intent-${G.operation.name}`,type:G.metric==="latency"?"latency-regression":G.metric==="error-rate"?"error-spike":"throughput-drop",description:G.description,operation:G.operation,confidence:{score:G.severity==="high"?0.9:G.severity==="medium"?0.7:0.5,sampleSize:N.find((M)=>M.operation.name===G.operation.name)?.totalCalls??0}},target:G.operation,proposal:{summary:q1(G),rationale:P1(G),changeType:G.metric==="error-rate"?"policy-update":"revision",recommendedActions:J1(G)},confidence:G.severity==="high"?0.85:G.severity==="medium"?0.7:0.55,createdAt:new Date,createdBy:"ai-evolution-agent",status:"pending",priority:G.severity}));z((G)=>[...G,...P]),f(!1)},[J,N]),Z=d((P,G)=>{z((M)=>M.map((E)=>E.id===P?{...E,status:"approved"}:E))},[]),A=d((P,G)=>{z((M)=>M.map((E)=>E.id===P?{...E,status:"rejected"}:E))},[]),L=d(()=>{F.current=[],b(0),q([]),X([]),z([]),w([]),localStorage.removeItem(`${D0}-${$}`)},[$]);return $1(()=>({usageStats:N,anomalies:J,suggestions:K,hints:U,loading:V,trackOperation:W,analyzeUsage:Q,generateSuggestions:H,approveSuggestion:Z,rejectSuggestion:A,clear:L,operationCount:Y}),[N,J,K,U,V,W,Q,H,Z,A,L,Y])}function L0($,N){if(!$.length)return 0;if($.length===1)return $[0]??0;let q=Math.min($.length-1,Math.floor(N*$.length));return $[q]??0}function q1($){if($.metric==="latency")return`Add caching and pagination to ${$.operation.name} to reduce latency`;if($.metric==="error-rate")return`Add retry policy and circuit breaker to ${$.operation.name}`;return`Optimize ${$.operation.name} for improved throughput`}function P1($){if($.metric==="latency")return`The operation ${$.operation.name} is experiencing P99 latency of ${$.observedValue?.toFixed(0)}ms, which is above the recommended threshold of ${$.threshold}ms. This can impact user experience and downstream operations.`;if($.metric==="error-rate")return`The error rate for ${$.operation.name} is ${(($.observedValue??0)*100).toFixed(1)}%, indicating potential issues with input validation, external dependencies, or resource limits.`;return`Throughput for ${$.operation.name} has dropped significantly, suggesting potential bottlenecks or reduced demand that should be investigated.`}function J1($){if($.metric==="latency")return["Add response caching for frequently accessed data","Implement pagination for large result sets","Optimize database queries with proper indexing","Consider adding a GraphQL DataLoader for batching"];if($.metric==="error-rate")return["Add input validation at the contract level","Implement retry policy with exponential backoff","Add circuit breaker for external dependencies","Enhance error logging for better debugging"];return["Review resource allocation and scaling policies","Check for upstream routing or load balancer issues","Validate feature flag configurations","Monitor dependency health metrics"]}import{Button as n,LoaderBlock as Z1}from"@contractspec/lib.design-system";import{Badge as g}from"@contractspec/lib.ui-kit-web/ui/badge";import{Card as l}from"@contractspec/lib.ui-kit-web/ui/card";import{useCallback as t,useMemo as Q1}from"react";import{jsx as D,jsxs as _}from"react/jsx-runtime";function K2({templateId:$,onLog:N}){let{usageStats:q,anomalies:J,suggestions:X,hints:K,loading:z,trackOperation:U,analyzeUsage:w,generateSuggestions:V,approveSuggestion:f,rejectSuggestion:F,clear:Y,operationCount:b}=q0($),W=t(()=>{let P=[{name:`${$}.list`,duration:150,success:!0},{name:`${$}.list`,duration:180,success:!0},{name:`${$}.create`,duration:350,success:!0},{name:`${$}.create`,duration:420,success:!1,error:"VALIDATION_ERROR"},{name:`${$}.list`,duration:200,success:!0},{name:`${$}.get`,duration:80,success:!0},{name:`${$}.update`,duration:280,success:!0},{name:`${$}.list`,duration:950,success:!0},{name:`${$}.delete`,duration:150,success:!1,error:"NOT_FOUND"},{name:`${$}.create`,duration:380,success:!0}];for(let G of P)U(G.name,G.duration,G.success,G.error);N?.(`Simulated ${P.length} operations`),setTimeout(()=>{w(),N?.("Analysis complete")},100)},[$,U,w,N]),Q=t(async()=>{await V(),N?.("AI suggestions generated")},[V,N]),H=t((P)=>{f(P),N?.(`Suggestion ${P.slice(0,8)} approved`)},[f,N]),Z=t((P)=>{F(P),N?.(`Suggestion ${P.slice(0,8)} rejected`)},[F,N]),A=t(()=>{Y(),N?.("Evolution data cleared")},[Y,N]),L=Q1(()=>X.filter((P)=>P.status==="pending"),[X]);return _("div",{className:"space-y-6",children:[_("div",{className:"flex items-center justify-between",children:[_("div",{children:[D("h2",{className:"font-semibold text-xl",children:"AI Evolution Engine"}),D("p",{className:"text-muted-foreground text-sm",children:"Analyze usage patterns and get AI-powered suggestions"})]}),_("div",{className:"flex items-center gap-2",children:[_(g,{variant:"secondary",children:[b," ops tracked"]}),D(n,{variant:"ghost",size:"sm",onPress:A,children:"Clear"})]})]}),_(l,{className:"p-4",children:[_("div",{className:"flex flex-wrap items-center gap-3",children:[D(n,{variant:"default",size:"sm",onPress:W,children:"Simulate Operations"}),D(n,{variant:"outline",size:"sm",onPress:w,disabled:b<5,children:"Analyze Usage"}),D(n,{variant:"outline",size:"sm",onPress:Q,disabled:J.length===0||z,children:z?"Generating...":"Generate AI Suggestions"})]}),D("p",{className:"mt-2 text-muted-foreground text-xs",children:"Simulate sandbox operations, analyze patterns, and generate AI improvement suggestions."})]}),z&&D(Z1,{label:"Generating AI suggestions..."}),q.length>0&&_(l,{className:"p-4",children:[D("h3",{className:"mb-3 font-semibold",children:"Usage Statistics"}),D("div",{className:"grid gap-3 md:grid-cols-2 lg:grid-cols-3",children:q.map((P)=>D(X1,{stat:P},P.operation.name))})]}),J.length>0&&_(l,{className:"p-4",children:[_("div",{className:"mb-3 flex items-center justify-between",children:[D("h3",{className:"font-semibold",children:"Detected Anomalies"}),_(g,{variant:"secondary",className:"border-amber-500/30 bg-amber-500/20 text-amber-400",children:[J.length," issues"]})]}),D("div",{className:"space-y-2",children:J.map((P,G)=>D(z1,{anomaly:P},`${P.operation.name}-${G}`))})]}),X.length>0&&_(l,{className:"p-4",children:[_("div",{className:"mb-3 flex items-center justify-between",children:[D("h3",{className:"font-semibold",children:"AI Suggestions"}),D("div",{className:"flex items-center gap-2",children:L.length>0&&_(g,{variant:"secondary",className:"border-amber-500/30 bg-amber-500/20 text-amber-400",children:[L.length," pending"]})})]}),D("div",{className:"space-y-3",children:X.map((P)=>D(H1,{suggestion:P,onApprove:H,onReject:Z},P.id))})]}),K.length>0&&_(l,{className:"p-4",children:[D("h3",{className:"mb-3 font-semibold",children:"Optimization Hints"}),D("div",{className:"space-y-2",children:K.map((P,G)=>D(G1,{hint:P},`${P.operation.name}-${G}`))})]}),q.length===0&&J.length===0&&X.length===0&&D(l,{className:"p-8 text-center",children:D("p",{className:"text-muted-foreground",children:'Click "Simulate Operations" to generate sample data for analysis.'})})]})}function X1({stat:$}){return _("div",{className:"rounded-lg border border-violet-500/20 bg-violet-500/5 p-3",children:[_("div",{className:"mb-2 flex items-center justify-between",children:[D("span",{className:"font-medium font-mono text-sm",children:$.operation.name}),_(g,{variant:$.errorRate>0.1?"destructive":"default",className:$.errorRate>0.1?"":"border-green-500/30 bg-green-500/20 text-green-400",children:[((1-$.errorRate)*100).toFixed(0),"% success"]})]}),_("div",{className:"grid grid-cols-2 gap-2 text-xs",children:[_("div",{children:[D("span",{className:"text-muted-foreground",children:"Total Calls:"})," ",D("span",{className:"font-medium",children:$.totalCalls})]}),_("div",{children:[D("span",{className:"text-muted-foreground",children:"Avg Latency:"})," ",_("span",{className:"font-medium",children:[$.averageLatencyMs.toFixed(0),"ms"]})]}),_("div",{children:[D("span",{className:"text-muted-foreground",children:"P95:"})," ",_("span",{className:"font-medium",children:[$.p95LatencyMs.toFixed(0),"ms"]})]}),_("div",{children:[D("span",{className:"text-muted-foreground",children:"P99:"})," ",_("span",{className:"font-medium",children:[$.p99LatencyMs.toFixed(0),"ms"]})]})]})]})}function z1({anomaly:$}){return _("div",{className:"flex items-center justify-between rounded-lg border border-amber-500/30 bg-amber-500/5 p-3",children:[_("div",{className:"flex items-center gap-3",children:[D("span",{className:`text-lg ${{low:"text-amber-400",medium:"text-orange-400",high:"text-red-400"}[$.severity]}`,title:`${$.severity} severity`,children:$.severity==="high"?"\uD83D\uDD34":$.severity==="medium"?"\uD83D\uDFE0":"\uD83D\uDFE1"}),_("div",{children:[D("p",{className:"font-medium text-sm",children:$.description}),_("p",{className:"text-muted-foreground text-xs",children:[$.operation.name," \u2022 ",$.metric]})]})]}),D(g,{variant:$.severity==="high"?"destructive":"secondary",className:$.severity==="medium"?"border-amber-500/30 bg-amber-500/20 text-amber-400":"",children:$.severity})]})}function H1({suggestion:$,onApprove:N,onReject:q}){let X=((K)=>{switch(K){case"pending":return{variant:"secondary",className:"bg-amber-500/20 text-amber-400 border-amber-500/30"};case"approved":return{variant:"default",className:"bg-green-500/20 text-green-400 border-green-500/30"};case"rejected":return{variant:"destructive",className:""};default:return{variant:"secondary",className:""}}})($.status);return _("div",{className:"rounded-lg border border-violet-500/30 bg-violet-500/5 p-4",children:[_("div",{className:"mb-2 flex items-start justify-between",children:[_("div",{className:"flex-1",children:[_("div",{className:"flex items-center gap-2",children:[D("span",{className:"text-lg",children:$.intent.type==="latency-regression"?"\u26A1":$.intent.type==="error-spike"?"\uD83D\uDD25":"\uD83D\uDCC9"}),D("h4",{className:"font-medium",children:$.proposal.summary})]}),D("p",{className:"mt-1 text-muted-foreground text-sm",children:$.proposal.rationale})]}),D(g,{variant:X.variant,className:X.className,children:$.status})]}),$.proposal.recommendedActions&&$.proposal.recommendedActions.length>0&&_("div",{className:"mt-3",children:[D("p",{className:"mb-1 font-semibold text-violet-400 text-xs uppercase",children:"Recommended Actions"}),D("ul",{className:"list-inside list-disc space-y-1 text-xs",children:$.proposal.recommendedActions.slice(0,3).map((K,z)=>D("li",{children:K},z))})]}),_("div",{className:"mt-3 flex items-center justify-between",children:[_("div",{className:"flex items-center gap-2 text-xs",children:[_("span",{className:"text-muted-foreground",children:["Confidence: ",($.confidence*100).toFixed(0),"%"]}),D("span",{className:"text-muted-foreground",children:"\u2022"}),D(g,{variant:"secondary",children:$.priority})]}),$.status==="pending"&&_("div",{className:"flex items-center gap-2",children:[D(n,{variant:"outline",size:"sm",onPress:()=>q($.id),children:"Reject"}),D(n,{variant:"default",size:"sm",onPress:()=>N($.id),children:"Approve"})]})]})]})}function G1({hint:$}){return D("div",{className:"rounded-lg border border-blue-500/20 bg-blue-500/5 p-3",children:_("div",{className:"flex items-start gap-3",children:[D("span",{className:"text-lg",children:{schema:"\uD83D\uDCD0",policy:"\uD83D\uDD12",performance:"\u26A1","error-handling":"\uD83D\uDEE1\uFE0F"}[$.category]}),_("div",{className:"flex-1",children:[D("p",{className:"font-medium",children:$.summary}),D("p",{className:"mt-1 text-muted-foreground text-xs",children:$.justification}),$.recommendedActions.length>0&&D("ul",{className:"mt-2 list-inside list-disc text-xs",children:$.recommendedActions.slice(0,2).map((q,J)=>D("li",{children:q},J))})]})]})})}import{Button as E0}from"@contractspec/lib.design-system";import{Badge as s}from"@contractspec/lib.ui-kit-web/ui/badge";import{Card as K1}from"@contractspec/lib.ui-kit-web/ui/card";import{useCallback as k0,useMemo as C0}from"react";import{jsx as h,jsxs as T}from"react/jsx-runtime";function U2({templateId:$,expanded:N=!1,onToggle:q,onLog:J,onOpenEvolution:X}){let{anomalies:K,suggestions:z,loading:U,approveSuggestion:w,rejectSuggestion:V,operationCount:f}=q0($),F=C0(()=>z.filter((Q)=>Q.status==="pending"),[z]),Y=C0(()=>K.sort((Q,H)=>{let Z={high:0,medium:1,low:2};return Z[Q.severity]-Z[H.severity]}).slice(0,3),[K]),b=k0((Q)=>{w(Q),J?.(`Approved suggestion ${Q.slice(0,8)}`)},[w,J]),W=k0((Q)=>{V(Q),J?.(`Rejected suggestion ${Q.slice(0,8)}`)},[V,J]);if(!N)return T("button",{onClick:q,className:"flex items-center gap-2 rounded-lg border border-violet-500/30 bg-violet-500/10 px-3 py-2 text-sm transition hover:bg-violet-500/20",type:"button",children:[h("span",{children:"\uD83E\uDD16"}),h("span",{children:"Evolution"}),F.length>0&&h(s,{variant:"secondary",className:"border-amber-500/30 bg-amber-500/20 text-amber-400",children:F.length}),K.length>0&&F.length===0&&h(s,{variant:"destructive",children:K.length})]});return T(K1,{className:"w-80 overflow-hidden",children:[T("div",{className:"flex items-center justify-between border-violet-500/20 border-b bg-violet-500/5 px-3 py-2",children:[T("div",{className:"flex items-center gap-2",children:[h("span",{children:"\uD83E\uDD16"}),h("span",{className:"font-semibold text-sm",children:"Evolution"})]}),T("div",{className:"flex items-center gap-1",children:[X&&h(E0,{variant:"ghost",size:"sm",onPress:X,children:"Expand"}),h("button",{onClick:q,className:"p-1 text-muted-foreground hover:text-foreground",type:"button",title:"Collapse",children:"\u2715"})]})]}),T("div",{className:"max-h-96 overflow-y-auto p-3",children:[T("div",{className:"mb-3 flex items-center justify-between text-xs",children:[T("span",{className:"text-muted-foreground",children:[f," ops tracked"]}),T("div",{className:"flex items-center gap-2",children:[K.length>0&&T(s,{variant:"destructive",children:[K.length," anomalies"]}),F.length>0&&T(s,{variant:"secondary",className:"border-amber-500/30 bg-amber-500/20 text-amber-400",children:[F.length," pending"]})]})]}),U&&h("div",{className:"py-4 text-center text-muted-foreground text-sm",children:"Generating suggestions..."}),Y.length>0&&T("div",{className:"mb-4",children:[h("p",{className:"mb-2 font-semibold text-violet-400 text-xs uppercase",children:"Top Issues"}),h("div",{className:"space-y-2",children:Y.map((Q,H)=>T("div",{className:"rounded border border-amber-500/20 bg-amber-500/5 p-2 text-xs",children:[T("div",{className:"flex items-center gap-2",children:[h("span",{children:Q.severity==="high"?"\uD83D\uDD34":Q.severity==="medium"?"\uD83D\uDFE0":"\uD83D\uDFE1"}),h("span",{className:"truncate font-medium",children:Q.operation.name})]}),h("p",{className:"mt-1 truncate text-muted-foreground",children:Q.description})]},`${Q.operation.name}-${H}`))})]}),F.length>0&&T("div",{children:[h("p",{className:"mb-2 font-semibold text-violet-400 text-xs uppercase",children:"Pending Suggestions"}),T("div",{className:"space-y-2",children:[F.slice(0,3).map((Q)=>h(V1,{suggestion:Q,onApprove:b,onReject:W},Q.id)),F.length>3&&T("p",{className:"text-center text-muted-foreground text-xs",children:["+",F.length-3," more suggestions"]})]})]}),K.length===0&&F.length===0&&!U&&h("div",{className:"py-4 text-center text-muted-foreground text-xs",children:"No issues detected. Keep coding!"})]}),X&&h("div",{className:"border-violet-500/20 border-t p-2",children:h(E0,{variant:"ghost",size:"sm",className:"w-full",onPress:X,children:"Open Evolution Dashboard \u2192"})})]})}function V1({suggestion:$,onApprove:N,onReject:q}){return T("div",{className:"rounded border border-violet-500/20 bg-violet-500/5 p-2",children:[h("div",{className:"flex items-start justify-between gap-2",children:T("div",{className:"min-w-0 flex-1",children:[h("p",{className:"truncate font-medium text-xs",children:$.proposal.summary}),T("div",{className:"mt-1 flex items-center gap-2 text-xs",children:[h(s,{variant:"secondary",children:$.priority}),T("span",{className:"text-muted-foreground",children:[($.confidence*100).toFixed(0),"%"]})]})]})}),T("div",{className:"mt-2 flex justify-end gap-1",children:[h("button",{onClick:()=>q($.id),className:"rounded px-2 py-0.5 text-red-400 text-xs hover:bg-red-400/10",type:"button",children:"Reject"}),h("button",{onClick:()=>N($.id),className:"rounded bg-violet-500/20 px-2 py-0.5 text-violet-400 text-xs hover:bg-violet-500/30",type:"button",children:"Approve"})]})]})}import{createContext as F1,useContext as Y1}from"react";var I0=Symbol.for("@contractspec/lib.example-shared-ui/template-runtime-context");function D1(){let $=globalThis;return $[I0]??=F1(null),$[I0]}var f1=D1();function r(){let $=Y1(f1);if(!$)throw Error("useTemplateRuntime must be used within a TemplateRuntimeProvider");return $}import{RefreshCw as W1,Shield as _1}from"lucide-react";import{useState as U1}from"react";import{jsx as f0,jsxs as W0}from"react/jsx-runtime";function P0(){let{projectId:$,templateId:N,template:q,installer:J}=r(),[X,K]=U1(!1),z=async()=>{K(!0);try{await J.install(N,{projectId:$})}finally{K(!1)}};return W0("div",{className:"inline-flex items-center gap-2 rounded-full border border-border bg-muted/40 px-3 py-1 text-muted-foreground text-xs",children:[f0(_1,{className:"h-3.5 w-3.5 text-violet-400"}),W0("span",{children:["Local runtime \xB7"," ",f0("span",{className:"font-semibold text-foreground",children:q.name})]}),W0("button",{type:"button",className:"inline-flex items-center gap-1 rounded-full border border-border px-2 py-0.5 font-semibold text-[11px] text-muted-foreground hover:text-foreground",onClick:z,disabled:X,children:[f0(W1,{className:"h-3 w-3"}),X?"Resetting\u2026":"Reset data"]})]})}function S0($){let N=$.split(".");return(N[N.length-1]??$).split("-").map((J)=>J.charAt(0).toUpperCase()+J.slice(1)).join(" ")}import{useCallback as A1,useEffect as v0,useState as J0}from"react";function y0({engine:$,fetchData:N,presentationId:q,presentations:J,resolvePresentation:X,templateId:K}){let[z,U]=J0(""),[w,V]=J0(""),[f,F]=J0(!1),[Y,b]=J0(null);v0(()=>{if(q&&J.includes(q)){U(q);return}if(J.length===0){U("");return}if(!J.includes(z))U(J[0]??"")},[q,J,z,K]);let W=A1(async()=>{if(!z||!$)return;F(!0),b(null);try{if(!X)throw Error("resolvePresentation not available in runtime context");let Q=X(z);if(!Q)throw Error(`Presentation descriptor not found: ${z}`);let H=await N(z),Z=await $.render("markdown",Q,{data:H.data});V(Z.body)}catch(Q){b(Q instanceof Error?Q:Error("Failed to render markdown"))}finally{F(!1)}},[$,N,X,z]);return v0(()=>{W()},[W]),{error:Y,loading:f,markdownContent:w,renderMarkdown:W,selectedPresentation:z,setSelectedPresentation:U}}import{Button as c0,ErrorState as w1,LoaderBlock as b1,MarkdownRenderer as R1}from"@contractspec/lib.design-system";import{Badge as h1}from"@contractspec/lib.ui-kit-web/ui/badge";import{Card as r0}from"@contractspec/lib.ui-kit-web/ui/card";import{useCallback as M1}from"react";import{jsx as C,jsxs as Z0}from"react/jsx-runtime";function i2({templateId:$,presentationId:N,className:q}){let{engine:J,template:X,templateId:K,resolvePresentation:z,fetchData:U}=r(),w=$??K,V=X?.presentations??[],{error:f,loading:F,markdownContent:Y,renderMarkdown:b,selectedPresentation:W,setSelectedPresentation:Q}=y0({engine:J,fetchData:U,presentationId:N,presentations:V,resolvePresentation:z,templateId:w});if(!V.length)return C(r0,{className:q,children:C("div",{className:"p-6 text-center",children:C("p",{className:"text-muted-foreground",children:"No presentations available for this template."})})});let H=M1(()=>{if(Y)navigator.clipboard.writeText(Y)},[Y]);return Z0("div",{className:q,children:[Z0("div",{className:"mb-4 flex flex-wrap items-center gap-2",children:[C("span",{className:"font-medium text-muted-foreground text-sm",children:"Presentation:"}),V.map((Z)=>C(c0,{variant:W===Z?"default":"outline",size:"sm",onPress:()=>Q(Z),children:S0(Z)},Z)),Z0("div",{className:"ml-auto flex items-center gap-2",children:[C(h1,{variant:"secondary",children:"LLM-friendly"}),C(c0,{variant:"outline",size:"sm",onPress:H,disabled:!Y||F,children:"Copy"})]})]}),Z0(r0,{className:"overflow-hidden",children:[F&&C(b1,{label:"Rendering markdown..."}),f&&C(w1,{title:"Render failed",description:f.message,onRetry:b,retryLabel:"Retry"}),!F&&!f&&Y&&C("div",{className:"p-6",children:C(R1,{content:Y})}),!F&&!f&&!Y&&C("div",{className:"p-6 text-center",children:C("p",{className:"text-muted-foreground",children:"Select a presentation to view its markdown output."})})]})]})}import*as i0 from"react";import{useContext as j0,useMemo as I}from"react";import{jsx as O1}from"react/jsx-runtime";var _0=i0.createContext(null);function p2({templateId:$,role:N="user",device:q="desktop",children:J}){let X=I(()=>T1($,N),[$,N]),K=I(()=>{return X.filter((W)=>{let Q=W.conditions;if(!Q)return!0;if(Q.role&&!Q.role.includes(N))return!1;if(Q.device&&Q.device!=="any"&&Q.device!==q)return!1;return!0})},[X,N,q]),z=I(()=>{let W=new Map;for(let Q of K)W.set(Q.target,Q);return W},[K]),U=I(()=>(W,Q)=>{let H=z.get(W);if(!H)return Q;let Z={...Q};for(let A of H.modifications)switch(A.op){case"hide":Z={...Z,hidden:!0};break;case"relabel":Z={...Z,label:A.label};break;case"reorder":Z={...Z,position:A.position};break;case"restyle":Z={...Z,className:A.className,variant:A.variant};break;case"set-default":Z={...Z,defaultValue:A.value};break}return Z},[z]),w=I(()=>(W)=>{return z.get(W)?.modifications.some((H)=>H.op==="hide")??!1},[z]),V=I(()=>(W,Q)=>{let Z=z.get(W)?.modifications.find((A)=>A.op==="relabel");return Z&&Z.op==="relabel"?Z.label:Q},[z]),f=I(()=>(W,Q)=>{let Z=z.get(W)?.modifications.find((A)=>A.op==="reorder");return Z&&Z.op==="reorder"?Z.position:Q},[z]),F=I(()=>(W)=>{let H=z.get(W)?.modifications.find((Z)=>Z.op==="restyle");if(H&&H.op==="restyle")return{className:H.className,variant:H.variant};return{}},[z]),Y=I(()=>(W,Q)=>{let Z=z.get(W)?.modifications.find((A)=>A.op==="set-default");return Z&&Z.op==="set-default"?Z.value:Q},[z]),b=I(()=>({overlays:K,applyOverlay:U,isHidden:w,getLabel:V,getPosition:f,getStyle:F,getDefault:Y,role:N,device:q}),[K,U,w,V,f,F,Y,N,q]);return O1(_0.Provider,{value:b,children:J})}function u2(){let $=j0(_0);if(!$)throw Error("useOverlayContext must be used within an OverlayContextProvider");return $}function d2(){return j0(_0)!==null}function T1($,N){return{"crm-pipeline":[{id:"crm-hide-internal-fields",target:"deal.internalNotes",modifications:[{op:"hide"}],conditions:{role:["viewer","user"]}},{id:"crm-relabel-value",target:"deal.value",modifications:[{op:"relabel",label:"Deal Amount"}]}],"saas-boilerplate":[{id:"saas-hide-billing",target:"settings.billing",modifications:[{op:"hide"}],conditions:{role:["viewer"]}},{id:"saas-restyle-plan",target:"settings.plan",modifications:[{op:"restyle",variant:"premium"}],conditions:{role:["admin"]}}],"agent-console":[{id:"agent-hide-cost",target:"run.cost",modifications:[{op:"hide"}],conditions:{role:["viewer"]}},{id:"agent-relabel-tokens",target:"run.tokens",modifications:[{op:"relabel",label:"Token Usage"}]}],"todos-app":[{id:"todos-hide-assignee",target:"task.assignee",modifications:[{op:"hide"}],conditions:{device:"mobile"}}],"messaging-app":[{id:"messaging-reorder-timestamp",target:"message.timestamp",modifications:[{op:"reorder",position:0}]}],"recipe-app-i18n":[{id:"recipe-relabel-servings",target:"recipe.servings",modifications:[{op:"relabel",label:"Portions"}]}]}[$]??[]}import{useCallback as Q0,useEffect as x0,useMemo as B1,useRef as L1,useState as g0}from"react";var U0="contractspec-behavior-data",E1=["playground","specs","builder","markdown","evolution","canvas_add","canvas_delete","spec_save","spec_validate","ai_suggestions"];function p0($){let[N,q]=g0([]),J=L1(new Date),[X,K]=g0(0);x0(()=>{try{let f=localStorage.getItem(U0);if(f){let F=JSON.parse(f);q(F.events.map((Y)=>({...Y,timestamp:new Date(Y.timestamp)}))),J.current=new Date(F.sessionStart)}}catch{}},[]),x0(()=>{if(N.length>0)try{localStorage.setItem(U0,JSON.stringify({events:N.map((f)=>({...f,timestamp:f.timestamp.toISOString()})),sessionStart:J.current.toISOString()}))}catch{}},[N]);let z=Q0((f,F)=>{let Y={type:f,timestamp:new Date,templateId:$,metadata:F};q((b)=>[...b,Y]),K((b)=>b+1)},[$]),U=Q0((f)=>{return N.filter((F)=>F.type===f)},[N]),w=Q0(()=>{let F=new Date().getTime()-J.current.getTime(),Y=new Map;for(let P of N){let G=Y.get(P.templateId)??0;Y.set(P.templateId,G+1)}let b=Array.from(Y.entries()).map(([P,G])=>({templateId:P,count:G})).sort((P,G)=>G.count-P.count).slice(0,3),W=new Map;for(let P of N)if(P.type==="mode_change"&&P.metadata?.mode){let G=P.metadata.mode,M=W.get(G)??0;W.set(G,M+1)}let Q=Array.from(W.entries()).map(([P,G])=>({mode:P,count:G})).sort((P,G)=>G.count-P.count),H=new Set;for(let P of N){if(P.type==="mode_change"&&P.metadata?.mode)H.add(P.metadata.mode);if(P.type==="feature_usage"&&P.metadata?.feature)H.add(P.metadata.feature);if(P.type==="canvas_interaction"){let G=P.metadata?.action;if(G==="add")H.add("canvas_add");if(G==="delete")H.add("canvas_delete")}if(P.type==="spec_edit"){let G=P.metadata?.action;if(G==="save")H.add("spec_save");if(G==="validate")H.add("spec_validate")}}let Z=E1.filter((P)=>!H.has(P)),A=N.filter((P)=>P.type==="error").length,L=k1(Array.from(H),Z,Q,N.length);return{totalEvents:N.length,sessionDuration:F,mostUsedTemplates:b,mostUsedModes:Q,featuresUsed:Array.from(H),unusedFeatures:Z,errorCount:A,recommendations:L}},[N]),V=Q0(()=>{q([]),K(0),J.current=new Date,localStorage.removeItem(U0)},[]);return B1(()=>({trackEvent:z,getSummary:w,getEventsByType:U,eventCount:X,sessionStart:J.current,clear:V}),[z,w,U,X,V])}function k1($,N,q,J){let X=[];if(N.includes("evolution"))X.push("Try the AI Evolution mode to get automated improvement suggestions");if(N.includes("markdown"))X.push("Use Markdown preview to see documentation for your specs");if(N.includes("builder"))X.push("Explore the Visual Builder to design your UI components");if(!$.includes("spec_validate")&&$.includes("specs"))X.push("Don't forget to validate your specs before saving");if($.includes("evolution")&&!$.includes("ai_suggestions"))X.push("Generate AI suggestions to get actionable improvement recommendations");if(J>50)X.push("Great engagement! Consider saving your work regularly");if(q.length===1)X.push("Try different modes to explore all sandbox capabilities");return X}import{Button as u0}from"@contractspec/lib.design-system";import{Badge as A0}from"@contractspec/lib.ui-kit-web/ui/badge";import{Card as C1}from"@contractspec/lib.ui-kit-web/ui/card";import{useCallback as d0,useMemo as I1,useState as S1}from"react";import{jsx as R,jsxs as O}from"react/jsx-runtime";function N4({templateId:$,collapsed:N=!1,onToggle:q}){let{getSummary:J,eventCount:X,clear:K,sessionStart:z}=p0($),[U,w]=S1(!1),V=I1(()=>J(),[J]),f=d0((Y)=>{let b=Math.floor(Y/1000),W=Math.floor(b/60),Q=Math.floor(W/60);if(Q>0)return`${Q}h ${W%60}m`;if(W>0)return`${W}m ${b%60}s`;return`${b}s`},[]),F=d0(()=>{K()},[K]);if(N)return O("button",{onClick:q,className:"flex items-center gap-2 rounded-lg border border-blue-500/30 bg-blue-500/10 px-3 py-2 text-sm transition hover:bg-blue-500/20",type:"button",children:[R("span",{children:"\uD83D\uDCCA"}),R("span",{children:"Insights"}),R(A0,{variant:"secondary",children:X})]});return O(C1,{className:"overflow-hidden",children:[O("div",{className:"flex items-center justify-between border-blue-500/20 border-b bg-blue-500/5 px-4 py-3",children:[O("div",{className:"flex items-center gap-2",children:[R("span",{children:"\uD83D\uDCCA"}),R("span",{className:"font-semibold",children:"Personalization Insights"})]}),O("div",{className:"flex items-center gap-2",children:[R(u0,{variant:"ghost",size:"sm",onPress:()=>w(!U),children:U?"Hide Details":"Show Details"}),q&&R("button",{onClick:q,className:"p-1 text-muted-foreground hover:text-foreground",type:"button",title:"Collapse",children:"\u2715"})]})]}),O("div",{className:"p-4",children:[O("div",{className:"mb-4 grid grid-cols-2 gap-3 md:grid-cols-4",children:[R(X0,{label:"Session Time",value:f(V.sessionDuration),icon:"\u23F1\uFE0F"}),R(X0,{label:"Events Tracked",value:V.totalEvents.toString(),icon:"\uD83D\uDCC8"}),R(X0,{label:"Features Used",value:`${V.featuresUsed.length}/${V.featuresUsed.length+V.unusedFeatures.length}`,icon:"\u2728"}),R(X0,{label:"Errors",value:V.errorCount.toString(),icon:"\u26A0\uFE0F",variant:V.errorCount>0?"warning":"success"})]}),V.recommendations.length>0&&O("div",{className:"mb-4",children:[R("h4",{className:"mb-2 font-semibold text-blue-400 text-xs uppercase",children:"Recommendations"}),R("ul",{className:"space-y-1",children:V.recommendations.map((Y,b)=>O("li",{className:"flex items-start gap-2 text-sm",children:[R("span",{className:"text-blue-400",children:"\uD83D\uDCA1"}),R("span",{children:Y})]},b))})]}),V.unusedFeatures.length>0&&O("div",{className:"mb-4",children:[R("h4",{className:"mb-2 font-semibold text-blue-400 text-xs uppercase",children:"Try These Features"}),R("div",{className:"flex flex-wrap gap-2",children:V.unusedFeatures.slice(0,5).map((Y)=>R(A0,{variant:"secondary",children:w0(Y)},Y))})]}),U&&R(v1,{summary:V,sessionStart:z}),R("div",{className:"mt-4 flex justify-end border-blue-500/10 border-t pt-4",children:R(u0,{variant:"ghost",size:"sm",onPress:F,children:"Clear Data"})})]})]})}function X0({label:$,value:N,icon:q,variant:J="default"}){return O("div",{className:`rounded-lg border p-3 text-center ${{default:"bg-blue-500/5 border-blue-500/20",warning:"bg-amber-500/5 border-amber-500/20",success:"bg-green-500/5 border-green-500/20"}[J]}`,children:[R("div",{className:"mb-1 text-lg",children:q}),R("div",{className:"font-bold text-lg",children:N}),R("div",{className:"text-muted-foreground text-xs",children:$})]})}function v1({summary:$,sessionStart:N}){return O("div",{className:"mt-4 space-y-4 border-blue-500/10 border-t pt-4",children:[$.mostUsedTemplates.length>0&&O("div",{children:[R("h4",{className:"mb-2 font-semibold text-blue-400 text-xs uppercase",children:"Most Used Templates"}),R("div",{className:"space-y-1",children:$.mostUsedTemplates.map(({templateId:q,count:J})=>O("div",{className:"flex items-center justify-between text-sm",children:[R("span",{children:y1(q)}),O("span",{className:"text-muted-foreground",children:[J," events"]})]},q))})]}),$.mostUsedModes.length>0&&O("div",{children:[R("h4",{className:"mb-2 font-semibold text-blue-400 text-xs uppercase",children:"Mode Usage"}),R("div",{className:"space-y-1",children:$.mostUsedModes.map(({mode:q,count:J})=>O("div",{className:"flex items-center justify-between text-sm",children:[R("span",{children:w0(q)}),O("span",{className:"text-muted-foreground",children:[J," switches"]})]},q))})]}),O("div",{children:[R("h4",{className:"mb-2 font-semibold text-blue-400 text-xs uppercase",children:"Features Used"}),R("div",{className:"flex flex-wrap gap-2",children:$.featuresUsed.map((q)=>O(A0,{variant:"default",className:"border-green-500/30 bg-green-500/20 text-green-400",children:["\u2713 ",w0(q)]},q))})]}),O("div",{className:"text-muted-foreground text-xs",children:["Session started: ",N.toLocaleString()]})]})}function w0($){return $.replace(/_/g," ").replace(/\b\w/g,(N)=>N.toUpperCase())}function y1($){return $.replace(/-/g," ").replace(/\b\w/g,(N)=>N.toUpperCase())}import{Sparkles as c1}from"lucide-react";import{useState as m0}from"react";import{jsx as b0,jsxs as l0}from"react/jsx-runtime";function z0({organizationId:$="demo-org",projectName:N,endpoint:q,token:J}){let{installer:X,templateId:K,template:z}=r(),[U,w]=m0("idle"),[V,f]=m0(null);return l0("div",{className:"flex flex-col items-end gap-1",children:[l0("button",{type:"button",className:"btn-primary inline-flex items-center gap-2 text-sm",onClick:async()=>{w("saving"),f(null);try{await X.saveToStudio({templateId:K,projectName:N??`${z.name} demo`,organizationId:$,endpoint:q,token:J}),w("saved"),setTimeout(()=>w("idle"),3000)}catch(Y){w("error"),f(Y instanceof Error?Y.message:"Unknown error")}},disabled:U==="saving",children:[b0(c1,{className:"h-4 w-4"}),U==="saving"?"Publishing\u2026":"Save to Studio"]}),U==="error"&&V?b0("p",{className:"text-destructive text-xs",children:V}):null,U==="saved"?b0("p",{className:"text-emerald-400 text-xs",children:"Template sent to Studio."}):null]})}import{BundleProvider as r1,BundleRenderer as i1}from"@contractspec/lib.surface-runtime/react";import{jsx as S,jsxs as H0}from"react/jsx-runtime";function V4({plan:$,title:N,description:q,sidebar:J,actions:X,showSaveAction:K=!0,saveProps:z,children:U}){let V={header:H0("header",{className:"rounded-2xl border border-border bg-card p-6 shadow-sm",children:[H0("div",{className:"flex flex-wrap items-center justify-between gap-4",children:[H0("div",{children:[S("p",{className:"font-semibold text-muted-foreground text-sm uppercase tracking-wide",children:"ContractSpec Templates"}),S("h1",{className:"font-bold text-3xl",children:N}),q?S("p",{className:"mt-2 max-w-2xl text-muted-foreground text-sm",children:q}):null]}),H0("div",{className:"flex flex-col items-end gap-2",children:[S(P0,{}),K?S(z0,{...z}):null]})]}),X?S("div",{className:"mt-4",children:X}):null]}),primary:S("main",{className:"space-y-4 p-2",children:U})};if(J!=null)V.sidebar=S("aside",{className:"rounded-2xl border border-border bg-card p-4",children:J});return S(r1,{plan:$,children:S(i1,{slotContent:V})})}function e($){let N=$?.id??"unknown";if(!$)return n0(N);switch(N){case"crm-pipeline":return j1($.schema.contracts);case"saas-boilerplate":return x1($.schema.contracts);case"agent-console":return g1($.schema.contracts);case"todos-app":return p1($.schema.contracts);case"messaging-app":return u1($.schema.contracts);case"recipe-app-i18n":return d1($.schema.contracts);default:return n0(N)}}function j1($){return`// CRM Pipeline Specs
3
+ // Contracts: ${$.join(", ")}
2145
4
 
2146
5
  contractSpec("crm.deal.updateStage.v1", {
2147
6
  goal: "Move a deal to a different pipeline stage",
@@ -2210,11 +69,8 @@ contractSpec("crm.contact.list.v1", {
2210
69
  hasMore: "boolean"
2211
70
  }
2212
71
  }
2213
- });`;
2214
- }
2215
- function generateSaasBoilerplateSpec(contracts) {
2216
- return `// SaaS Boilerplate Specs
2217
- // Contracts: ${contracts.join(", ")}
72
+ });`}function x1($){return`// SaaS Boilerplate Specs
73
+ // Contracts: ${$.join(", ")}
2218
74
 
2219
75
  contractSpec("saas.project.create.v1", {
2220
76
  goal: "Create a new project in an organization",
@@ -2277,11 +133,8 @@ contractSpec("saas.settings.update.v1", {
2277
133
  }
2278
134
  },
2279
135
  events: ["settings.updated"]
2280
- });`;
2281
- }
2282
- function generateAgentConsoleSpec(contracts) {
2283
- return `// Agent Console Specs
2284
- // Contracts: ${contracts.join(", ")}
136
+ });`}function g1($){return`// Agent Console Specs
137
+ // Contracts: ${$.join(", ")}
2285
138
 
2286
139
  contractSpec("agent.run.execute.v1", {
2287
140
  goal: "Execute an agent run with specified tools",
@@ -2347,11 +200,8 @@ contractSpec("agent.agent.create.v1", {
2347
200
  }
2348
201
  },
2349
202
  events: ["agent.created"]
2350
- });`;
2351
- }
2352
- function generateTodosSpec(contracts) {
2353
- return `// To-dos App Specs
2354
- // Contracts: ${contracts.join(", ")}
203
+ });`}function p1($){return`// To-dos App Specs
204
+ // Contracts: ${$.join(", ")}
2355
205
 
2356
206
  contractSpec("tasks.board.v1", {
2357
207
  goal: "Assign and approve craft work",
@@ -2410,11 +260,8 @@ contractSpec("tasks.complete.v1", {
2410
260
  }
2411
261
  },
2412
262
  events: ["task.completed"]
2413
- });`;
2414
- }
2415
- function generateMessagingSpec(contracts) {
2416
- return `// Messaging App Specs
2417
- // Contracts: ${contracts.join(", ")}
263
+ });`}function u1($){return`// Messaging App Specs
264
+ // Contracts: ${$.join(", ")}
2418
265
 
2419
266
  contractSpec("messaging.send.v1", {
2420
267
  goal: "Deliver intent-rich updates",
@@ -2467,11 +314,8 @@ contractSpec("messaging.read.v1", {
2467
314
  }
2468
315
  },
2469
316
  events: ["message.read"]
2470
- });`;
2471
- }
2472
- function generateRecipeSpec(contracts) {
2473
- return `// Recipe App (i18n) Specs
2474
- // Contracts: ${contracts.join(", ")}
317
+ });`}function d1($){return`// Recipe App (i18n) Specs
318
+ // Contracts: ${$.join(", ")}
2475
319
 
2476
320
  contractSpec("recipes.lookup.v1", {
2477
321
  goal: "Serve bilingual rituals",
@@ -2518,13 +362,10 @@ contractSpec("recipes.favorite.toggle.v1", {
2518
362
  }
2519
363
  },
2520
364
  events: ["recipe.favorited", "recipe.unfavorited"]
2521
- });`;
2522
- }
2523
- function generateDefaultSpec(templateId) {
2524
- return `// ${templateId} Specs
365
+ });`}function n0($){return`// ${$} Specs
2525
366
 
2526
- contractSpec("${templateId}.main.v1", {
2527
- goal: "Main operation for ${templateId}",
367
+ contractSpec("${$}.main.v1", {
368
+ goal: "Main operation for ${$}",
2528
369
  transport: { gql: { query: "main" } },
2529
370
  io: {
2530
371
  input: {
@@ -2534,961 +375,38 @@ contractSpec("${templateId}.main.v1", {
2534
375
  result: "unknown"
2535
376
  }
2536
377
  }
2537
- });`;
2538
- }
2539
-
2540
- // src/hooks/useSpecContent.ts
2541
- import { useCallback as useCallback8, useEffect as useEffect4, useState as useState7 } from "react";
2542
- "use client";
2543
- var SPEC_STORAGE_KEY = "contractspec-spec-content";
2544
- function useSpecContent(templateId) {
2545
- const { template } = useTemplateRuntime();
2546
- const [content, setContentState] = useState7("");
2547
- const [savedContent, setSavedContent] = useState7("");
2548
- const [loading, setLoading] = useState7(true);
2549
- const [validation, setValidation] = useState7(null);
2550
- const [lastSaved, setLastSaved] = useState7(null);
2551
- useEffect4(() => {
2552
- setLoading(true);
2553
- try {
2554
- const stored = localStorage.getItem(`${SPEC_STORAGE_KEY}-${templateId}`);
2555
- if (stored) {
2556
- const parsed = JSON.parse(stored);
2557
- if (parsed.content) {
2558
- setContentState(parsed.content);
2559
- setSavedContent(parsed.content);
2560
- setLastSaved(parsed.savedAt);
2561
- } else {
2562
- const generated = generateSpecFromTemplate(template);
2563
- setContentState(generated);
2564
- setSavedContent(generated);
2565
- }
2566
- } else {
2567
- const generated = generateSpecFromTemplate(template);
2568
- setContentState(generated);
2569
- setSavedContent(generated);
2570
- }
2571
- } catch {
2572
- const generated = generateSpecFromTemplate(template);
2573
- setContentState(generated);
2574
- setSavedContent(generated);
2575
- }
2576
- setLoading(false);
2577
- }, [templateId]);
2578
- const setContent = useCallback8((newContent) => {
2579
- setContentState(newContent);
2580
- setValidation(null);
2581
- }, []);
2582
- const save = useCallback8(() => {
2583
- try {
2584
- const savedAt = new Date().toISOString();
2585
- localStorage.setItem(`${SPEC_STORAGE_KEY}-${templateId}`, JSON.stringify({
2586
- content,
2587
- savedAt
2588
- }));
2589
- setSavedContent(content);
2590
- setLastSaved(savedAt);
2591
- } catch {}
2592
- }, [content, templateId]);
2593
- const validate = useCallback8(() => {
2594
- const errors = [];
2595
- const lines = content.split(`
2596
- `);
2597
- if (!content.includes("contractSpec(")) {
2598
- errors.push({
2599
- line: 1,
2600
- message: "Spec must contain a contractSpec() definition",
2601
- severity: "error"
2602
- });
2603
- }
2604
- if (!content.includes("goal:")) {
2605
- errors.push({
2606
- line: 1,
2607
- message: "Spec should have a goal field",
2608
- severity: "warning"
2609
- });
2610
- }
2611
- if (!content.includes("io:")) {
2612
- errors.push({
2613
- line: 1,
2614
- message: "Spec should define io (input/output)",
2615
- severity: "warning"
2616
- });
2617
- }
2618
- const openBraces = (content.match(/{/g) ?? []).length;
2619
- const closeBraces = (content.match(/}/g) ?? []).length;
2620
- if (openBraces !== closeBraces) {
2621
- errors.push({
2622
- line: lines.length,
2623
- message: `Unbalanced braces: ${openBraces} opening, ${closeBraces} closing`,
2624
- severity: "error"
2625
- });
2626
- }
2627
- const openParens = (content.match(/\(/g) ?? []).length;
2628
- const closeParens = (content.match(/\)/g) ?? []).length;
2629
- if (openParens !== closeParens) {
2630
- errors.push({
2631
- line: lines.length,
2632
- message: `Unbalanced parentheses: ${openParens} opening, ${closeParens} closing`,
2633
- severity: "error"
2634
- });
2635
- }
2636
- lines.forEach((line, index) => {
2637
- const singleQuotes = (line.match(/'/g) ?? []).length;
2638
- const doubleQuotes = (line.match(/"/g) ?? []).length;
2639
- if (singleQuotes % 2 !== 0) {
2640
- errors.push({
2641
- line: index + 1,
2642
- message: "Unclosed single quote",
2643
- severity: "error"
2644
- });
2645
- }
2646
- if (doubleQuotes % 2 !== 0) {
2647
- errors.push({
2648
- line: index + 1,
2649
- message: "Unclosed double quote",
2650
- severity: "error"
2651
- });
2652
- }
2653
- });
2654
- const result = {
2655
- valid: errors.filter((e) => e.severity === "error").length === 0,
2656
- errors
2657
- };
2658
- setValidation(result);
2659
- return result;
2660
- }, [content]);
2661
- const reset = useCallback8(() => {
2662
- const generated = generateSpecFromTemplate(template);
2663
- setContentState(generated);
2664
- setSavedContent(generated);
2665
- setValidation(null);
2666
- setLastSaved(null);
2667
- try {
2668
- localStorage.removeItem(`${SPEC_STORAGE_KEY}-${templateId}`);
2669
- } catch {}
2670
- }, [templateId]);
2671
- return {
2672
- content,
2673
- loading,
2674
- isDirty: content !== savedContent,
2675
- validation,
2676
- setContent,
2677
- save,
2678
- validate,
2679
- reset,
2680
- lastSaved
2681
- };
2682
- }
2683
-
2684
- // src/SpecEditorPanel.tsx
2685
- import { Button as Button5, LoaderBlock as LoaderBlock3 } from "@contractspec/lib.design-system";
2686
- import { Badge as Badge5 } from "@contractspec/lib.ui-kit-web/ui/badge";
2687
- import { useCallback as useCallback9, useEffect as useEffect5 } from "react";
2688
- import { jsxDEV as jsxDEV9 } from "react/jsx-dev-runtime";
2689
- "use client";
2690
- function SpecEditorPanel({
2691
- templateId,
2692
- SpecEditor,
2693
- onLog
2694
- }) {
2695
- const {
2696
- content,
2697
- loading,
2698
- isDirty,
2699
- validation,
2700
- setContent,
2701
- save,
2702
- validate,
2703
- reset,
2704
- lastSaved
2705
- } = useSpecContent(templateId);
2706
- useEffect5(() => {
2707
- if (!loading && content) {
2708
- onLog?.(`Spec loaded for ${templateId}`);
2709
- }
2710
- }, [loading, content, templateId, onLog]);
2711
- const handleSave = useCallback9(() => {
2712
- save();
2713
- onLog?.("Spec saved locally");
2714
- }, [save, onLog]);
2715
- const handleValidate = useCallback9(() => {
2716
- const result = validate();
2717
- if (result.valid) {
2718
- onLog?.("Spec validation passed");
2719
- } else {
2720
- const errorCount = result.errors.filter((e) => e.severity === "error").length;
2721
- const warnCount = result.errors.filter((e) => e.severity === "warning").length;
2722
- onLog?.(`Spec validation: ${errorCount} errors, ${warnCount} warnings`);
2723
- }
2724
- }, [validate, onLog]);
2725
- const handleReset = useCallback9(() => {
2726
- reset();
2727
- onLog?.("Spec reset to template defaults");
2728
- }, [reset, onLog]);
2729
- if (loading) {
2730
- return /* @__PURE__ */ jsxDEV9(LoaderBlock3, {
2731
- label: "Loading spec..."
2732
- }, undefined, false, undefined, this);
2733
- }
2734
- return /* @__PURE__ */ jsxDEV9("div", {
2735
- className: "space-y-4",
2736
- children: [
2737
- /* @__PURE__ */ jsxDEV9("div", {
2738
- className: "flex items-center justify-between",
2739
- children: [
2740
- /* @__PURE__ */ jsxDEV9("div", {
2741
- className: "flex items-center gap-2",
2742
- children: [
2743
- /* @__PURE__ */ jsxDEV9(Button5, {
2744
- variant: "default",
2745
- size: "sm",
2746
- onClick: handleSave,
2747
- children: "Save"
2748
- }, undefined, false, undefined, this),
2749
- /* @__PURE__ */ jsxDEV9(Button5, {
2750
- variant: "outline",
2751
- size: "sm",
2752
- onClick: handleValidate,
2753
- children: "Validate"
2754
- }, undefined, false, undefined, this),
2755
- isDirty && /* @__PURE__ */ jsxDEV9(Badge5, {
2756
- variant: "secondary",
2757
- className: "border-amber-500/30 bg-amber-500/20 text-amber-400",
2758
- children: "Unsaved changes"
2759
- }, undefined, false, undefined, this),
2760
- validation && /* @__PURE__ */ jsxDEV9(Badge5, {
2761
- variant: validation.valid ? "default" : "destructive",
2762
- className: validation.valid ? "border-green-500/30 bg-green-500/20 text-green-400" : "",
2763
- children: validation.valid ? "Valid" : `${validation.errors.filter((e) => e.severity === "error").length} errors`
2764
- }, undefined, false, undefined, this)
2765
- ]
2766
- }, undefined, true, undefined, this),
2767
- /* @__PURE__ */ jsxDEV9("div", {
2768
- className: "flex items-center gap-2",
2769
- children: [
2770
- lastSaved && /* @__PURE__ */ jsxDEV9("span", {
2771
- className: "text-muted-foreground text-xs",
2772
- children: [
2773
- "Last saved: ",
2774
- new Date(lastSaved).toLocaleTimeString()
2775
- ]
2776
- }, undefined, true, undefined, this),
2777
- /* @__PURE__ */ jsxDEV9(Button5, {
2778
- variant: "ghost",
2779
- size: "sm",
2780
- onPress: handleReset,
2781
- children: "Reset"
2782
- }, undefined, false, undefined, this)
2783
- ]
2784
- }, undefined, true, undefined, this)
2785
- ]
2786
- }, undefined, true, undefined, this),
2787
- validation && validation.errors.length > 0 && /* @__PURE__ */ jsxDEV9("div", {
2788
- className: "rounded-lg border border-amber-500/50 bg-amber-500/10 p-3",
2789
- children: [
2790
- /* @__PURE__ */ jsxDEV9("p", {
2791
- className: "mb-2 font-semibold text-amber-400 text-xs uppercase",
2792
- children: "Validation Issues"
2793
- }, undefined, false, undefined, this),
2794
- /* @__PURE__ */ jsxDEV9("ul", {
2795
- className: "space-y-1",
2796
- children: validation.errors.map((error, index) => /* @__PURE__ */ jsxDEV9("li", {
2797
- className: `text-xs ${error.severity === "error" ? "text-red-400" : "text-amber-400"}`,
2798
- children: [
2799
- "Line ",
2800
- error.line,
2801
- ": ",
2802
- error.message
2803
- ]
2804
- }, `${error.line}-${error.message}-${index}`, true, undefined, this))
2805
- }, undefined, false, undefined, this)
2806
- ]
2807
- }, undefined, true, undefined, this),
2808
- /* @__PURE__ */ jsxDEV9("div", {
2809
- className: "rounded-2xl border border-border bg-card p-4",
2810
- children: /* @__PURE__ */ jsxDEV9(SpecEditor, {
2811
- projectId: "sandbox",
2812
- type: "CAPABILITY",
2813
- content,
2814
- onChange: setContent,
2815
- metadata: { template: templateId },
2816
- onSave: handleSave,
2817
- onValidate: handleValidate
2818
- }, undefined, false, undefined, this)
2819
- }, undefined, false, undefined, this)
2820
- ]
2821
- }, undefined, true, undefined, this);
2822
- }
2823
-
2824
- // src/TemplateShell.tsx
2825
- import { jsxDEV as jsxDEV10 } from "react/jsx-dev-runtime";
2826
- var TemplateShell = ({
2827
- title,
2828
- description,
2829
- sidebar,
2830
- actions,
2831
- showSaveAction = true,
2832
- saveProps,
2833
- children
2834
- }) => /* @__PURE__ */ jsxDEV10("div", {
2835
- className: "space-y-6",
2836
- children: [
2837
- /* @__PURE__ */ jsxDEV10("header", {
2838
- className: "rounded-2xl border border-border bg-card p-6 shadow-sm",
2839
- children: [
2840
- /* @__PURE__ */ jsxDEV10("div", {
2841
- className: "flex flex-wrap items-center justify-between gap-4",
2842
- children: [
2843
- /* @__PURE__ */ jsxDEV10("div", {
2844
- children: [
2845
- /* @__PURE__ */ jsxDEV10("p", {
2846
- className: "font-semibold text-muted-foreground text-sm uppercase tracking-wide",
2847
- children: "ContractSpec Templates"
2848
- }, undefined, false, undefined, this),
2849
- /* @__PURE__ */ jsxDEV10("h1", {
2850
- className: "font-bold text-3xl",
2851
- children: title
2852
- }, undefined, false, undefined, this),
2853
- description ? /* @__PURE__ */ jsxDEV10("p", {
2854
- className: "mt-2 max-w-2xl text-muted-foreground text-sm",
2855
- children: description
2856
- }, undefined, false, undefined, this) : null
2857
- ]
2858
- }, undefined, true, undefined, this),
2859
- /* @__PURE__ */ jsxDEV10("div", {
2860
- className: "flex flex-col items-end gap-2",
2861
- children: [
2862
- /* @__PURE__ */ jsxDEV10(LocalDataIndicator, {}, undefined, false, undefined, this),
2863
- showSaveAction ? /* @__PURE__ */ jsxDEV10(SaveToStudioButton, {
2864
- ...saveProps
2865
- }, undefined, false, undefined, this) : null
2866
- ]
2867
- }, undefined, true, undefined, this)
2868
- ]
2869
- }, undefined, true, undefined, this),
2870
- actions ? /* @__PURE__ */ jsxDEV10("div", {
2871
- className: "mt-4",
2872
- children: actions
2873
- }, undefined, false, undefined, this) : null
2874
- ]
2875
- }, undefined, true, undefined, this),
2876
- /* @__PURE__ */ jsxDEV10("div", {
2877
- className: sidebar ? "grid gap-6 lg:grid-cols-[minmax(0,1fr)_320px]" : "w-full",
2878
- children: [
2879
- /* @__PURE__ */ jsxDEV10("main", {
2880
- className: "space-y-4 p-2",
2881
- children
2882
- }, undefined, false, undefined, this),
2883
- sidebar ? /* @__PURE__ */ jsxDEV10("aside", {
2884
- className: "rounded-2xl border border-border bg-card p-4",
2885
- children: sidebar
2886
- }, undefined, false, undefined, this) : null
2887
- ]
2888
- }, undefined, true, undefined, this)
2889
- ]
2890
- }, undefined, true, undefined, this);
2891
-
2892
- // src/hooks/useRegistryTemplates.ts
2893
- import { useQuery } from "@tanstack/react-query";
2894
- function useRegistryTemplates() {
2895
- return useQuery({
2896
- queryKey: ["registryTemplates"],
2897
- queryFn: async () => {
2898
- const registryUrl = process.env.NEXT_PUBLIC_CONTRACTSPEC_REGISTRY_URL ?? "";
2899
- if (!registryUrl)
2900
- return [];
2901
- const res = await fetch(`${registryUrl.replace(/\/$/, "")}/r/contractspec.json`, {
2902
- method: "GET",
2903
- headers: { Accept: "application/json" }
2904
- });
2905
- if (!res.ok)
2906
- return [];
2907
- const json = await res.json();
2908
- const items = json.items ?? [];
2909
- return items.filter((i) => i.type === "contractspec:template").map((i) => ({
2910
- id: i.name,
2911
- name: i.title ?? i.name,
2912
- description: i.description,
2913
- tags: i.meta?.tags ?? [],
2914
- source: "registry",
2915
- registryUrl
2916
- }));
2917
- }
2918
- });
2919
- }
2920
-
2921
- // src/hooks/useWorkflowComposer.ts
2922
- import { useCallback as useCallback10, useEffect as useEffect6, useMemo as useMemo7, useState as useState8 } from "react";
2923
- "use client";
2924
- function useWorkflowComposer(templateId) {
2925
- const [selectedWorkflow, setSelectedWorkflow] = useState8(null);
2926
- const [extensions, setExtensions] = useState8([]);
2927
- const [loading, _setLoading] = useState8(false);
2928
- const [error, _setError] = useState8(null);
2929
- const baseWorkflows = useMemo7(() => getTemplateWorkflows(templateId), [templateId]);
2930
- useEffect6(() => {
2931
- const firstWorkflow = baseWorkflows[0];
2932
- if (baseWorkflows.length > 0 && !selectedWorkflow && firstWorkflow) {
2933
- setSelectedWorkflow(firstWorkflow.meta.key);
2934
- }
2935
- }, [baseWorkflows, selectedWorkflow]);
2936
- const currentBase = useMemo7(() => {
2937
- return baseWorkflows.find((w) => w.meta.key === selectedWorkflow) ?? null;
2938
- }, [baseWorkflows, selectedWorkflow]);
2939
- const compose = useCallback10((scope) => {
2940
- if (!currentBase)
2941
- return null;
2942
- const applicableExtensions = extensions.filter((ext) => ext.workflow === currentBase.meta.key).filter((ext) => matchesScope(ext, scope)).sort((a, b) => (a.priority ?? 0) - (b.priority ?? 0));
2943
- if (applicableExtensions.length === 0) {
2944
- return currentBase;
2945
- }
2946
- let composedWorkflow = { ...currentBase, steps: [...currentBase.steps] };
2947
- for (const extension of applicableExtensions) {
2948
- composedWorkflow = applyExtension(composedWorkflow, extension);
2949
- }
2950
- return composedWorkflow;
2951
- }, [currentBase, extensions]);
2952
- const workflow = useMemo7(() => compose(), [compose]);
2953
- const selectWorkflow = useCallback10((workflowName) => {
2954
- setSelectedWorkflow(workflowName);
2955
- }, []);
2956
- const addExtension = useCallback10((extension) => {
2957
- setExtensions((prev) => [...prev, extension]);
2958
- }, []);
2959
- const removeExtension = useCallback10((workflowName, index) => {
2960
- setExtensions((prev) => {
2961
- const forWorkflow = prev.filter((e) => e.workflow === workflowName);
2962
- const others = prev.filter((e) => e.workflow !== workflowName);
2963
- forWorkflow.splice(index, 1);
2964
- return [...others, ...forWorkflow];
2965
- });
2966
- }, []);
2967
- const generateSpecCode = useCallback10(() => {
2968
- const composed = workflow;
2969
- if (!composed) {
2970
- return "// No workflow selected";
2971
- }
2972
- const stepsCode = composed.steps.map((step) => ` {
2973
- id: '${step.id}',
2974
- name: '${step.name}',
2975
- type: '${step.type}',${step.description ? `
2976
- description: '${step.description}',` : ""}${step.next ? `
2977
- next: ${JSON.stringify(step.next)},` : ""}${step.condition ? `
2978
- condition: '${step.condition}',` : ""}${step.timeout ? `
2979
- timeout: ${step.timeout},` : ""}${step.retries ? `
2980
- retries: ${step.retries},` : ""}${step.onError ? `
2981
- onError: '${step.onError}',` : ""}
378
+ });`}import{useCallback as G0,useEffect as m1,useState as $0}from"react";var R0="contractspec-spec-content";function o0($){let{template:N}=r(),[q,J]=$0(""),[X,K]=$0(""),[z,U]=$0(!0),[w,V]=$0(null),[f,F]=$0(null);m1(()=>{U(!0);try{let H=localStorage.getItem(`${R0}-${$}`);if(H){let Z=JSON.parse(H);if(Z.content)J(Z.content),K(Z.content),F(Z.savedAt);else{let A=e(N);J(A),K(A)}}else{let Z=e(N);J(Z),K(Z)}}catch{let H=e(N);J(H),K(H)}U(!1)},[$]);let Y=G0((H)=>{J(H),V(null)},[]),b=G0(()=>{try{let H=new Date().toISOString();localStorage.setItem(`${R0}-${$}`,JSON.stringify({content:q,savedAt:H})),K(q),F(H)}catch{}},[q,$]),W=G0(()=>{let H=[],Z=q.split(`
379
+ `);if(!q.includes("contractSpec("))H.push({line:1,message:"Spec must contain a contractSpec() definition",severity:"error"});if(!q.includes("goal:"))H.push({line:1,message:"Spec should have a goal field",severity:"warning"});if(!q.includes("io:"))H.push({line:1,message:"Spec should define io (input/output)",severity:"warning"});let A=(q.match(/{/g)??[]).length,L=(q.match(/}/g)??[]).length;if(A!==L)H.push({line:Z.length,message:`Unbalanced braces: ${A} opening, ${L} closing`,severity:"error"});let P=(q.match(/\(/g)??[]).length,G=(q.match(/\)/g)??[]).length;if(P!==G)H.push({line:Z.length,message:`Unbalanced parentheses: ${P} opening, ${G} closing`,severity:"error"});Z.forEach((E,u)=>{let B=(E.match(/'/g)??[]).length,y=(E.match(/"/g)??[]).length;if(B%2!==0)H.push({line:u+1,message:"Unclosed single quote",severity:"error"});if(y%2!==0)H.push({line:u+1,message:"Unclosed double quote",severity:"error"})});let M={valid:H.filter((E)=>E.severity==="error").length===0,errors:H};return V(M),M},[q]),Q=G0(()=>{let H=e(N);J(H),K(H),V(null),F(null);try{localStorage.removeItem(`${R0}-${$}`)}catch{}},[$]);return{content:q,loading:z,isDirty:q!==X,validation:w,setContent:Y,save:b,validate:W,reset:Q,lastSaved:f}}import{Button as h0,LoaderBlock as l1}from"@contractspec/lib.design-system";import{Badge as a0}from"@contractspec/lib.ui-kit-web/ui/badge";import{useCallback as M0,useEffect as n1}from"react";import{jsx as v,jsxs as p}from"react/jsx-runtime";function h4({templateId:$,SpecEditor:N,onLog:q}){let{content:J,loading:X,isDirty:K,validation:z,setContent:U,save:w,validate:V,reset:f,lastSaved:F}=o0($);n1(()=>{if(!X&&J)q?.(`Spec loaded for ${$}`)},[X,J,$,q]);let Y=M0(()=>{w(),q?.("Spec saved locally")},[w,q]),b=M0(()=>{let Q=V();if(Q.valid)q?.("Spec validation passed");else{let H=Q.errors.filter((A)=>A.severity==="error").length,Z=Q.errors.filter((A)=>A.severity==="warning").length;q?.(`Spec validation: ${H} errors, ${Z} warnings`)}},[V,q]),W=M0(()=>{f(),q?.("Spec reset to template defaults")},[f,q]);if(X)return v(l1,{label:"Loading spec..."});return p("div",{className:"space-y-4",children:[p("div",{className:"flex items-center justify-between",children:[p("div",{className:"flex items-center gap-2",children:[v(h0,{variant:"default",size:"sm",onClick:Y,children:"Save"}),v(h0,{variant:"outline",size:"sm",onClick:b,children:"Validate"}),K&&v(a0,{variant:"secondary",className:"border-amber-500/30 bg-amber-500/20 text-amber-400",children:"Unsaved changes"}),z&&v(a0,{variant:z.valid?"default":"destructive",className:z.valid?"border-green-500/30 bg-green-500/20 text-green-400":"",children:z.valid?"Valid":`${z.errors.filter((Q)=>Q.severity==="error").length} errors`})]}),p("div",{className:"flex items-center gap-2",children:[F&&p("span",{className:"text-muted-foreground text-xs",children:["Last saved: ",new Date(F).toLocaleTimeString()]}),v(h0,{variant:"ghost",size:"sm",onPress:W,children:"Reset"})]})]}),z&&z.errors.length>0&&p("div",{className:"rounded-lg border border-amber-500/50 bg-amber-500/10 p-3",children:[v("p",{className:"mb-2 font-semibold text-amber-400 text-xs uppercase",children:"Validation Issues"}),v("ul",{className:"space-y-1",children:z.errors.map((Q,H)=>p("li",{className:`text-xs ${Q.severity==="error"?"text-red-400":"text-amber-400"}`,children:["Line ",Q.line,": ",Q.message]},`${Q.line}-${Q.message}-${H}`))})]}),v("div",{className:"rounded-2xl border border-border bg-card p-4",children:v(N,{projectId:"sandbox",type:"CAPABILITY",content:J,onChange:U,metadata:{template:$},onSave:Y,onValidate:b})})]})}import{jsx as i,jsxs as o}from"react/jsx-runtime";var L4=({title:$,description:N,sidebar:q,actions:J,showSaveAction:X=!0,saveProps:K,children:z})=>o("div",{className:"space-y-6",children:[o("header",{className:"rounded-2xl border border-border bg-card p-6 shadow-sm",children:[o("div",{className:"flex flex-wrap items-center justify-between gap-4",children:[o("div",{children:[i("p",{className:"font-semibold text-muted-foreground text-sm uppercase tracking-wide",children:"ContractSpec Templates"}),i("h1",{className:"font-bold text-3xl",children:$}),N?i("p",{className:"mt-2 max-w-2xl text-muted-foreground text-sm",children:N}):null]}),o("div",{className:"flex flex-col items-end gap-2",children:[i(P0,{}),X?i(z0,{...K}):null]})]}),J?i("div",{className:"mt-4",children:J}):null]}),o("div",{className:q?"grid gap-6 lg:grid-cols-[minmax(0,1fr)_320px]":"w-full",children:[i("main",{className:"space-y-4 p-2",children:z}),q?i("aside",{className:"rounded-2xl border border-border bg-card p-4",children:q}):null]})]});import{useQuery as o1}from"@tanstack/react-query";function I4(){return o1({queryKey:["registryTemplates"],queryFn:async()=>{let $=process.env.NEXT_PUBLIC_CONTRACTSPEC_REGISTRY_URL??"";if(!$)return[];let N=await fetch(`${$.replace(/\/$/,"")}/r/contractspec.json`,{method:"GET",headers:{Accept:"application/json"}});if(!N.ok)return[];return((await N.json()).items??[]).filter((X)=>X.type==="contractspec:template").map((X)=>({id:X.name,name:X.title??X.name,description:X.description,tags:X.meta?.tags??[],source:"registry",registryUrl:$}))}})}import{useCallback as N0,useEffect as a1,useMemo as T0,useState as K0}from"react";function y4($){let[N,q]=K0(null),[J,X]=K0([]),[K,z]=K0(!1),[U,w]=K0(null),V=T0(()=>$2($),[$]);a1(()=>{let Z=V[0];if(V.length>0&&!N&&Z)q(Z.meta.key)},[V,N]);let f=T0(()=>{return V.find((Z)=>Z.meta.key===N)??null},[V,N]),F=N0((Z)=>{if(!f)return null;let A=J.filter((P)=>P.workflow===f.meta.key).filter((P)=>t1(P,Z)).sort((P,G)=>(P.priority??0)-(G.priority??0));if(A.length===0)return f;let L={...f,steps:[...f.steps]};for(let P of A)L=s1(L,P);return L},[f,J]),Y=T0(()=>F(),[F]),b=N0((Z)=>{q(Z)},[]),W=N0((Z)=>{X((A)=>[...A,Z])},[]),Q=N0((Z,A)=>{X((L)=>{let P=L.filter((M)=>M.workflow===Z),G=L.filter((M)=>M.workflow!==Z);return P.splice(A,1),[...G,...P]})},[]),H=N0(()=>{let Z=Y;if(!Z)return"// No workflow selected";let A=Z.steps.map((P)=>` {
380
+ id: '${P.id}',
381
+ name: '${P.name}',
382
+ type: '${P.type}',${P.description?`
383
+ description: '${P.description}',`:""}${P.next?`
384
+ next: ${JSON.stringify(P.next)},`:""}${P.condition?`
385
+ condition: '${P.condition}',`:""}${P.timeout?`
386
+ timeout: ${P.timeout},`:""}${P.retries?`
387
+ retries: ${P.retries},`:""}${P.onError?`
388
+ onError: '${P.onError}',`:""}
2982
389
  }`).join(`,
2983
- `);
2984
- const extensionsCode = extensions.length > 0 ? `
390
+ `),L=J.length>0?`
2985
391
 
2986
392
  // Extensions applied:
2987
- ${extensions.map((ext) => `// - ${ext.workflow} (priority: ${ext.priority ?? 0})${ext.customSteps?.length ? ` +${ext.customSteps.length} steps` : ""}${ext.hiddenSteps?.length ? ` -${ext.hiddenSteps.length} hidden` : ""}`).join(`
2988
- `)}` : "";
2989
- return `// Workflow Spec: ${composed.meta.key} v${composed.meta.version}
2990
- // Generated from ${templateId} template
2991
- ${extensionsCode}
393
+ ${J.map((P)=>`// - ${P.workflow} (priority: ${P.priority??0})${P.customSteps?.length?` +${P.customSteps.length} steps`:""}${P.hiddenSteps?.length?` -${P.hiddenSteps.length} hidden`:""}`).join(`
394
+ `)}`:"";return`// Workflow Spec: ${Z.meta.key} v${Z.meta.version}
395
+ // Generated from ${$} template
396
+ ${L}
2992
397
 
2993
398
  import { workflowSpec } from '@contractspec/lib.contracts-spec/workflow';
2994
399
 
2995
- export const ${toCamelCase(composed.meta.key)}Workflow = workflowSpec({
400
+ export const ${e1(Z.meta.key)}Workflow = workflowSpec({
2996
401
  meta: {
2997
- key: '${composed.meta.key}',
2998
- version: ${composed.meta.version},${composed.meta.description ? `
2999
- description: '${composed.meta.description}',` : ""}
402
+ key: '${Z.meta.key}',
403
+ version: ${Z.meta.version},${Z.meta.description?`
404
+ description: '${Z.meta.description}',`:""}
3000
405
  },
3001
- start: '${composed.start}',
406
+ start: '${Z.start}',
3002
407
  steps: [
3003
- ${stepsCode}
3004
- ],${composed.context ? `
3005
- context: ${JSON.stringify(composed.context, null, 2)},` : ""}
408
+ ${A}
409
+ ],${Z.context?`
410
+ context: ${JSON.stringify(Z.context,null,2)},`:""}
3006
411
  });
3007
- `;
3008
- }, [workflow, extensions, templateId]);
3009
- return {
3010
- workflow,
3011
- baseWorkflows,
3012
- extensions,
3013
- selectWorkflow,
3014
- addExtension,
3015
- removeExtension,
3016
- compose,
3017
- generateSpecCode,
3018
- loading,
3019
- error
3020
- };
3021
- }
3022
- function matchesScope(extension, scope) {
3023
- if (!scope)
3024
- return true;
3025
- if (extension.tenantId && extension.tenantId !== scope.tenantId) {
3026
- return false;
3027
- }
3028
- if (extension.role && extension.role !== scope.role) {
3029
- return false;
3030
- }
3031
- if (extension.device && extension.device !== scope.device) {
3032
- return false;
3033
- }
3034
- return true;
3035
- }
3036
- function applyExtension(workflow, extension) {
3037
- let steps = [...workflow.steps];
3038
- if (extension.hiddenSteps) {
3039
- steps = steps.filter((s) => !extension.hiddenSteps?.includes(s.id));
3040
- }
3041
- if (extension.customSteps) {
3042
- for (const injection of extension.customSteps) {
3043
- const stepToInject = {
3044
- ...injection.inject,
3045
- id: injection.id ?? injection.inject.id
3046
- };
3047
- if (injection.after) {
3048
- const afterIndex = steps.findIndex((s) => s.id === injection.after);
3049
- if (afterIndex !== -1) {
3050
- steps.splice(afterIndex + 1, 0, stepToInject);
3051
- }
3052
- } else if (injection.before) {
3053
- const beforeIndex = steps.findIndex((s) => s.id === injection.before);
3054
- if (beforeIndex !== -1) {
3055
- steps.splice(beforeIndex, 0, stepToInject);
3056
- }
3057
- } else {
3058
- steps.push(stepToInject);
3059
- }
3060
- }
3061
- }
3062
- return { ...workflow, steps };
3063
- }
3064
- function toCamelCase(str) {
3065
- return str.replace(/[^a-zA-Z0-9]+(.)/g, (_, c) => c.toUpperCase()).replace(/^./, (c) => c.toLowerCase());
3066
- }
3067
- function getTemplateWorkflows(templateId) {
3068
- const templateWorkflows = {
3069
- "crm-pipeline": [
3070
- {
3071
- meta: {
3072
- key: "deal.qualification",
3073
- version: "1.0.0",
3074
- description: "Deal qualification workflow"
3075
- },
3076
- start: "lead-received",
3077
- steps: [
3078
- {
3079
- id: "lead-received",
3080
- name: "Lead Received",
3081
- type: "action",
3082
- description: "New lead enters the pipeline",
3083
- next: "qualify-lead"
3084
- },
3085
- {
3086
- id: "qualify-lead",
3087
- name: "Qualify Lead",
3088
- type: "decision",
3089
- description: "Determine if lead meets qualification criteria",
3090
- next: ["qualified", "disqualified"],
3091
- condition: "lead.score >= threshold"
3092
- },
3093
- {
3094
- id: "qualified",
3095
- name: "Lead Qualified",
3096
- type: "action",
3097
- next: "assign-rep"
3098
- },
3099
- {
3100
- id: "disqualified",
3101
- name: "Lead Disqualified",
3102
- type: "end"
3103
- },
3104
- {
3105
- id: "assign-rep",
3106
- name: "Assign Sales Rep",
3107
- type: "action",
3108
- next: "complete"
3109
- },
3110
- {
3111
- id: "complete",
3112
- name: "Workflow Complete",
3113
- type: "end"
3114
- }
3115
- ]
3116
- },
3117
- {
3118
- meta: {
3119
- key: "deal.closing",
3120
- version: "1.0.0",
3121
- description: "Deal closing workflow"
3122
- },
3123
- start: "proposal-sent",
3124
- steps: [
3125
- {
3126
- id: "proposal-sent",
3127
- name: "Proposal Sent",
3128
- type: "action",
3129
- next: "wait-response"
3130
- },
3131
- {
3132
- id: "wait-response",
3133
- name: "Wait for Response",
3134
- type: "wait",
3135
- timeout: 604800000,
3136
- next: "negotiate",
3137
- onError: "follow-up"
3138
- },
3139
- {
3140
- id: "follow-up",
3141
- name: "Follow Up",
3142
- type: "action",
3143
- next: "wait-response",
3144
- retries: 3
3145
- },
3146
- {
3147
- id: "negotiate",
3148
- name: "Negotiation",
3149
- type: "action",
3150
- next: "finalize"
3151
- },
3152
- {
3153
- id: "finalize",
3154
- name: "Finalize Deal",
3155
- type: "decision",
3156
- next: ["won", "lost"],
3157
- condition: "deal.accepted"
3158
- },
3159
- {
3160
- id: "won",
3161
- name: "Deal Won",
3162
- type: "end"
3163
- },
3164
- {
3165
- id: "lost",
3166
- name: "Deal Lost",
3167
- type: "end"
3168
- }
3169
- ]
3170
- }
3171
- ],
3172
- "saas-boilerplate": [
3173
- {
3174
- meta: {
3175
- key: "user.onboarding",
3176
- version: "1.0.0",
3177
- description: "User onboarding workflow"
3178
- },
3179
- start: "signup",
3180
- steps: [
3181
- {
3182
- id: "signup",
3183
- name: "User Signup",
3184
- type: "action",
3185
- next: "verify-email"
3186
- },
3187
- {
3188
- id: "verify-email",
3189
- name: "Verify Email",
3190
- type: "wait",
3191
- timeout: 86400000,
3192
- next: "profile-setup",
3193
- onError: "resend-verification"
3194
- },
3195
- {
3196
- id: "resend-verification",
3197
- name: "Resend Verification",
3198
- type: "action",
3199
- next: "verify-email",
3200
- retries: 2
3201
- },
3202
- {
3203
- id: "profile-setup",
3204
- name: "Setup Profile",
3205
- type: "action",
3206
- next: "onboarding-tour"
3207
- },
3208
- {
3209
- id: "onboarding-tour",
3210
- name: "Onboarding Tour",
3211
- type: "action",
3212
- next: "complete"
3213
- },
3214
- {
3215
- id: "complete",
3216
- name: "Onboarding Complete",
3217
- type: "end"
3218
- }
3219
- ]
3220
- }
3221
- ],
3222
- "agent-console": [
3223
- {
3224
- meta: {
3225
- key: "agent.execution",
3226
- version: "1.0.0",
3227
- description: "Agent execution workflow"
3228
- },
3229
- start: "receive-task",
3230
- steps: [
3231
- {
3232
- id: "receive-task",
3233
- name: "Receive Task",
3234
- type: "action",
3235
- next: "plan-execution"
3236
- },
3237
- {
3238
- id: "plan-execution",
3239
- name: "Plan Execution",
3240
- type: "action",
3241
- next: "execute-steps"
3242
- },
3243
- {
3244
- id: "execute-steps",
3245
- name: "Execute Steps",
3246
- type: "parallel",
3247
- next: ["tool-call", "observe", "reason"]
3248
- },
3249
- {
3250
- id: "tool-call",
3251
- name: "Tool Call",
3252
- type: "action",
3253
- next: "evaluate"
3254
- },
3255
- {
3256
- id: "observe",
3257
- name: "Observe",
3258
- type: "action",
3259
- next: "evaluate"
3260
- },
3261
- {
3262
- id: "reason",
3263
- name: "Reason",
3264
- type: "action",
3265
- next: "evaluate"
3266
- },
3267
- {
3268
- id: "evaluate",
3269
- name: "Evaluate Result",
3270
- type: "decision",
3271
- condition: "task.isComplete",
3272
- next: ["complete", "execute-steps"]
3273
- },
3274
- {
3275
- id: "complete",
3276
- name: "Task Complete",
3277
- type: "end"
3278
- }
3279
- ]
3280
- }
3281
- ],
3282
- "todos-app": [
3283
- {
3284
- meta: {
3285
- key: "task.lifecycle",
3286
- version: "1.0.0",
3287
- description: "Task lifecycle workflow"
3288
- },
3289
- start: "created",
3290
- steps: [
3291
- {
3292
- id: "created",
3293
- name: "Task Created",
3294
- type: "action",
3295
- next: "in-progress"
3296
- },
3297
- {
3298
- id: "in-progress",
3299
- name: "In Progress",
3300
- type: "action",
3301
- next: "review"
3302
- },
3303
- {
3304
- id: "review",
3305
- name: "Review",
3306
- type: "decision",
3307
- condition: "task.approved",
3308
- next: ["done", "in-progress"]
3309
- },
3310
- {
3311
- id: "done",
3312
- name: "Done",
3313
- type: "end"
3314
- }
3315
- ]
3316
- }
3317
- ],
3318
- "messaging-app": [
3319
- {
3320
- meta: {
3321
- key: "message.delivery",
3322
- version: "1.0.0",
3323
- description: "Message delivery workflow"
3324
- },
3325
- start: "compose",
3326
- steps: [
3327
- {
3328
- id: "compose",
3329
- name: "Compose Message",
3330
- type: "action",
3331
- next: "send"
3332
- },
3333
- {
3334
- id: "send",
3335
- name: "Send Message",
3336
- type: "action",
3337
- next: "deliver"
3338
- },
3339
- {
3340
- id: "deliver",
3341
- name: "Deliver",
3342
- type: "decision",
3343
- condition: "recipient.online",
3344
- next: ["delivered", "queue"]
3345
- },
3346
- {
3347
- id: "queue",
3348
- name: "Queue for Delivery",
3349
- type: "wait",
3350
- next: "deliver"
3351
- },
3352
- {
3353
- id: "delivered",
3354
- name: "Message Delivered",
3355
- type: "action",
3356
- next: "read"
3357
- },
3358
- {
3359
- id: "read",
3360
- name: "Message Read",
3361
- type: "end"
3362
- }
3363
- ]
3364
- }
3365
- ],
3366
- "recipe-app-i18n": [
3367
- {
3368
- meta: {
3369
- key: "recipe.creation",
3370
- version: "1.0.0",
3371
- description: "Recipe creation workflow"
3372
- },
3373
- start: "draft",
3374
- steps: [
3375
- {
3376
- id: "draft",
3377
- name: "Draft Recipe",
3378
- type: "action",
3379
- next: "add-ingredients"
3380
- },
3381
- {
3382
- id: "add-ingredients",
3383
- name: "Add Ingredients",
3384
- type: "action",
3385
- next: "add-steps"
3386
- },
3387
- {
3388
- id: "add-steps",
3389
- name: "Add Steps",
3390
- type: "action",
3391
- next: "review"
3392
- },
3393
- {
3394
- id: "review",
3395
- name: "Review Recipe",
3396
- type: "decision",
3397
- condition: "recipe.isComplete",
3398
- next: ["publish", "draft"]
3399
- },
3400
- {
3401
- id: "publish",
3402
- name: "Publish Recipe",
3403
- type: "end"
3404
- }
3405
- ]
3406
- }
3407
- ]
3408
- };
3409
- return templateWorkflows[templateId] ?? [];
3410
- }
3411
- // src/lib/component-registry.tsx
3412
- import { useEffect as useEffect7, useState as useState9 } from "react";
3413
- "use client";
3414
-
3415
- class TemplateComponentRegistry {
3416
- components = new Map;
3417
- listeners = new Set;
3418
- register(templateId, registration) {
3419
- this.components.set(templateId, registration);
3420
- this.listeners.forEach((l) => l(templateId));
3421
- }
3422
- get(templateId) {
3423
- return this.components.get(templateId);
3424
- }
3425
- subscribe(listener) {
3426
- this.listeners.add(listener);
3427
- return () => {
3428
- this.listeners.delete(listener);
3429
- };
3430
- }
3431
- }
3432
- var TEMPLATE_COMPONENT_REGISTRY_KEY = Symbol.for("@contractspec/lib.example-shared-ui/template-component-registry");
3433
- function getTemplateComponentRegistrySingleton() {
3434
- const store = globalThis;
3435
- store[TEMPLATE_COMPONENT_REGISTRY_KEY] ??= new TemplateComponentRegistry;
3436
- return store[TEMPLATE_COMPONENT_REGISTRY_KEY];
3437
- }
3438
- var templateComponentRegistry = getTemplateComponentRegistrySingleton();
3439
- function registerTemplateComponents(templateId, components) {
3440
- templateComponentRegistry.register(templateId, components);
3441
- }
3442
- function useTemplateComponents(templateId) {
3443
- const [components, setComponents] = useState9(() => templateComponentRegistry.get(templateId));
3444
- useEffect7(() => {
3445
- return templateComponentRegistry.subscribe((updatedId) => {
3446
- if (updatedId === templateId) {
3447
- setComponents(templateComponentRegistry.get(templateId));
3448
- }
3449
- });
3450
- }, [templateId]);
3451
- return components;
3452
- }
3453
- // src/utils/fetchPresentationData.ts
3454
- async function fetchPresentationData(_presentationName, _templateId) {
3455
- throw new Error("fetchPresentationData is deprecated. Use fetchData from TemplateRuntimeContext.");
3456
- }
3457
- function hasPresentationDataFetcher(_presentationName) {
3458
- return false;
3459
- }
3460
- function getRegisteredPresentationFetchers() {
3461
- return [];
3462
- }
3463
- // src/index.ts
3464
- import { MarkdownRenderer as MarkdownRenderer2 } from "@contractspec/lib.design-system";
3465
- export {
3466
- useWorkflowComposer,
3467
- useTemplateRuntime,
3468
- useTemplateComponents,
3469
- useSpecContent,
3470
- useRegistryTemplates,
3471
- useOverlayContext,
3472
- useIsInOverlayContext,
3473
- useEvolution,
3474
- useBehaviorTracking,
3475
- templateComponentRegistry,
3476
- registerTemplateComponents,
3477
- hasPresentationDataFetcher,
3478
- getRegisteredPresentationFetchers,
3479
- generateSpecFromTemplate,
3480
- fetchPresentationData,
3481
- TemplateShell,
3482
- TemplateRuntimeContext,
3483
- TemplateComponentRegistry,
3484
- SpecEditorPanel,
3485
- SpecDrivenTemplateShell,
3486
- SaveToStudioButton,
3487
- PersonalizationInsights,
3488
- OverlayContextProvider,
3489
- MarkdownView,
3490
- MarkdownRenderer2 as MarkdownRenderer,
3491
- LocalDataIndicator,
3492
- EvolutionSidebar,
3493
- EvolutionDashboard
3494
- };
412
+ `},[Y,J,$]);return{workflow:Y,baseWorkflows:V,extensions:J,selectWorkflow:b,addExtension:W,removeExtension:Q,compose:F,generateSpecCode:H,loading:K,error:U}}function t1($,N){if(!N)return!0;if($.tenantId&&$.tenantId!==N.tenantId)return!1;if($.role&&$.role!==N.role)return!1;if($.device&&$.device!==N.device)return!1;return!0}function s1($,N){let q=[...$.steps];if(N.hiddenSteps)q=q.filter((J)=>!N.hiddenSteps?.includes(J.id));if(N.customSteps)for(let J of N.customSteps){let X={...J.inject,id:J.id??J.inject.id};if(J.after){let K=q.findIndex((z)=>z.id===J.after);if(K!==-1)q.splice(K+1,0,X)}else if(J.before){let K=q.findIndex((z)=>z.id===J.before);if(K!==-1)q.splice(K,0,X)}else q.push(X)}return{...$,steps:q}}function e1($){return $.replace(/[^a-zA-Z0-9]+(.)/g,(N,q)=>q.toUpperCase()).replace(/^./,(N)=>N.toLowerCase())}function $2($){return{"crm-pipeline":[{meta:{key:"deal.qualification",version:"1.0.0",description:"Deal qualification workflow"},start:"lead-received",steps:[{id:"lead-received",name:"Lead Received",type:"action",description:"New lead enters the pipeline",next:"qualify-lead"},{id:"qualify-lead",name:"Qualify Lead",type:"decision",description:"Determine if lead meets qualification criteria",next:["qualified","disqualified"],condition:"lead.score >= threshold"},{id:"qualified",name:"Lead Qualified",type:"action",next:"assign-rep"},{id:"disqualified",name:"Lead Disqualified",type:"end"},{id:"assign-rep",name:"Assign Sales Rep",type:"action",next:"complete"},{id:"complete",name:"Workflow Complete",type:"end"}]},{meta:{key:"deal.closing",version:"1.0.0",description:"Deal closing workflow"},start:"proposal-sent",steps:[{id:"proposal-sent",name:"Proposal Sent",type:"action",next:"wait-response"},{id:"wait-response",name:"Wait for Response",type:"wait",timeout:604800000,next:"negotiate",onError:"follow-up"},{id:"follow-up",name:"Follow Up",type:"action",next:"wait-response",retries:3},{id:"negotiate",name:"Negotiation",type:"action",next:"finalize"},{id:"finalize",name:"Finalize Deal",type:"decision",next:["won","lost"],condition:"deal.accepted"},{id:"won",name:"Deal Won",type:"end"},{id:"lost",name:"Deal Lost",type:"end"}]}],"saas-boilerplate":[{meta:{key:"user.onboarding",version:"1.0.0",description:"User onboarding workflow"},start:"signup",steps:[{id:"signup",name:"User Signup",type:"action",next:"verify-email"},{id:"verify-email",name:"Verify Email",type:"wait",timeout:86400000,next:"profile-setup",onError:"resend-verification"},{id:"resend-verification",name:"Resend Verification",type:"action",next:"verify-email",retries:2},{id:"profile-setup",name:"Setup Profile",type:"action",next:"onboarding-tour"},{id:"onboarding-tour",name:"Onboarding Tour",type:"action",next:"complete"},{id:"complete",name:"Onboarding Complete",type:"end"}]}],"agent-console":[{meta:{key:"agent.execution",version:"1.0.0",description:"Agent execution workflow"},start:"receive-task",steps:[{id:"receive-task",name:"Receive Task",type:"action",next:"plan-execution"},{id:"plan-execution",name:"Plan Execution",type:"action",next:"execute-steps"},{id:"execute-steps",name:"Execute Steps",type:"parallel",next:["tool-call","observe","reason"]},{id:"tool-call",name:"Tool Call",type:"action",next:"evaluate"},{id:"observe",name:"Observe",type:"action",next:"evaluate"},{id:"reason",name:"Reason",type:"action",next:"evaluate"},{id:"evaluate",name:"Evaluate Result",type:"decision",condition:"task.isComplete",next:["complete","execute-steps"]},{id:"complete",name:"Task Complete",type:"end"}]}],"todos-app":[{meta:{key:"task.lifecycle",version:"1.0.0",description:"Task lifecycle workflow"},start:"created",steps:[{id:"created",name:"Task Created",type:"action",next:"in-progress"},{id:"in-progress",name:"In Progress",type:"action",next:"review"},{id:"review",name:"Review",type:"decision",condition:"task.approved",next:["done","in-progress"]},{id:"done",name:"Done",type:"end"}]}],"messaging-app":[{meta:{key:"message.delivery",version:"1.0.0",description:"Message delivery workflow"},start:"compose",steps:[{id:"compose",name:"Compose Message",type:"action",next:"send"},{id:"send",name:"Send Message",type:"action",next:"deliver"},{id:"deliver",name:"Deliver",type:"decision",condition:"recipient.online",next:["delivered","queue"]},{id:"queue",name:"Queue for Delivery",type:"wait",next:"deliver"},{id:"delivered",name:"Message Delivered",type:"action",next:"read"},{id:"read",name:"Message Read",type:"end"}]}],"recipe-app-i18n":[{meta:{key:"recipe.creation",version:"1.0.0",description:"Recipe creation workflow"},start:"draft",steps:[{id:"draft",name:"Draft Recipe",type:"action",next:"add-ingredients"},{id:"add-ingredients",name:"Add Ingredients",type:"action",next:"add-steps"},{id:"add-steps",name:"Add Steps",type:"action",next:"review"},{id:"review",name:"Review Recipe",type:"decision",condition:"recipe.isComplete",next:["publish","draft"]},{id:"publish",name:"Publish Recipe",type:"end"}]}]}[$]??[]}import{useEffect as N2,useState as q2}from"react";class s0{components=new Map;listeners=new Set;register($,N){this.components.set($,N),this.listeners.forEach((q)=>q($))}get($){return this.components.get($)}subscribe($){return this.listeners.add($),()=>{this.listeners.delete($)}}}var t0=Symbol.for("@contractspec/lib.example-shared-ui/template-component-registry");function P2(){let $=globalThis;return $[t0]??=new s0,$[t0]}var V0=P2();function m4($,N){V0.register($,N)}function l4($){let[N,q]=q2(()=>V0.get($));return N2(()=>{return V0.subscribe((J)=>{if(J===$)q(V0.get($))})},[$]),N}async function o4($,N){throw Error("fetchPresentationData is deprecated. Use fetchData from TemplateRuntimeContext.")}function a4($){return!1}function t4(){return[]}import{MarkdownRenderer as J3}from"@contractspec/lib.design-system";export{y4 as useWorkflowComposer,r as useTemplateRuntime,l4 as useTemplateComponents,o0 as useSpecContent,I4 as useRegistryTemplates,u2 as useOverlayContext,d2 as useIsInOverlayContext,q0 as useEvolution,p0 as useBehaviorTracking,V0 as templateComponentRegistry,m4 as registerTemplateComponents,a4 as hasPresentationDataFetcher,t4 as getRegisteredPresentationFetchers,e as generateSpecFromTemplate,o4 as fetchPresentationData,L4 as TemplateShell,f1 as TemplateRuntimeContext,s0 as TemplateComponentRegistry,h4 as SpecEditorPanel,V4 as SpecDrivenTemplateShell,z0 as SaveToStudioButton,N4 as PersonalizationInsights,p2 as OverlayContextProvider,i2 as MarkdownView,J3 as MarkdownRenderer,P0 as LocalDataIndicator,U2 as EvolutionSidebar,K2 as EvolutionDashboard};