@contractspec/lib.personalization 1.56.1 → 1.58.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/adapter.d.ts +15 -19
- package/dist/adapter.d.ts.map +1 -1
- package/dist/adapter.js +43 -38
- package/dist/analyzer.d.ts +15 -19
- package/dist/analyzer.d.ts.map +1 -1
- package/dist/analyzer.js +69 -51
- package/dist/browser/adapter.js +46 -0
- package/dist/browser/analyzer.js +72 -0
- package/dist/browser/docs/behavior-tracking.docblock.js +97 -0
- package/dist/browser/docs/index.js +271 -0
- package/dist/browser/docs/overlay-engine.docblock.js +98 -0
- package/dist/browser/docs/workflow-composition.docblock.js +83 -0
- package/dist/browser/index.js +555 -0
- package/dist/browser/store.js +75 -0
- package/dist/browser/tracker.js +94 -0
- package/dist/browser/types.js +0 -0
- package/dist/docs/behavior-tracking.docblock.d.ts +2 -6
- package/dist/docs/behavior-tracking.docblock.d.ts.map +1 -1
- package/dist/docs/behavior-tracking.docblock.js +95 -15
- package/dist/docs/index.d.ts +4 -3
- package/dist/docs/index.d.ts.map +1 -0
- package/dist/docs/index.js +272 -3
- package/dist/docs/overlay-engine.docblock.d.ts +2 -6
- package/dist/docs/overlay-engine.docblock.d.ts.map +1 -1
- package/dist/docs/overlay-engine.docblock.js +96 -15
- package/dist/docs/workflow-composition.docblock.d.ts +2 -6
- package/dist/docs/workflow-composition.docblock.d.ts.map +1 -1
- package/dist/docs/workflow-composition.docblock.js +81 -15
- package/dist/index.d.ts +7 -7
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +555 -6
- package/dist/node/adapter.js +46 -0
- package/dist/node/analyzer.js +72 -0
- package/dist/node/docs/behavior-tracking.docblock.js +97 -0
- package/dist/node/docs/index.js +271 -0
- package/dist/node/docs/overlay-engine.docblock.js +98 -0
- package/dist/node/docs/workflow-composition.docblock.js +83 -0
- package/dist/node/index.js +555 -0
- package/dist/node/store.js +75 -0
- package/dist/node/tracker.js +94 -0
- package/dist/node/types.js +0 -0
- package/dist/store.d.ts +14 -18
- package/dist/store.d.ts.map +1 -1
- package/dist/store.js +74 -57
- package/dist/tracker.d.ts +42 -46
- package/dist/tracker.d.ts.map +1 -1
- package/dist/tracker.js +93 -91
- package/dist/types.d.ts +48 -51
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +1 -0
- package/package.json +104 -37
- package/dist/adapter.js.map +0 -1
- package/dist/analyzer.js.map +0 -1
- package/dist/docs/behavior-tracking.docblock.js.map +0 -1
- package/dist/docs/overlay-engine.docblock.js.map +0 -1
- package/dist/docs/workflow-composition.docblock.js.map +0 -1
- package/dist/store.js.map +0 -1
- package/dist/tracker.js.map +0 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
import
|
|
7
|
-
|
|
1
|
+
export * from './types';
|
|
2
|
+
export * from './store';
|
|
3
|
+
export * from './tracker';
|
|
4
|
+
export * from './analyzer';
|
|
5
|
+
export * from './adapter';
|
|
6
|
+
import './docs';
|
|
7
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAC;AACxB,cAAc,SAAS,CAAC;AACxB,cAAc,WAAW,CAAC;AAC1B,cAAc,YAAY,CAAC;AAC3B,cAAc,WAAW,CAAC;AAC1B,OAAO,QAAQ,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,556 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
// @bun
|
|
2
|
+
// src/adapter.ts
|
|
3
|
+
function insightsToOverlaySuggestion(insights, options) {
|
|
4
|
+
const modifications = [];
|
|
5
|
+
insights.suggestedHiddenFields.forEach((field) => {
|
|
6
|
+
modifications.push({
|
|
7
|
+
type: "hideField",
|
|
8
|
+
field,
|
|
9
|
+
reason: "Automatically hidden because usage is near zero"
|
|
10
|
+
});
|
|
11
|
+
});
|
|
12
|
+
if (insights.frequentlyUsedFields.length) {
|
|
13
|
+
modifications.push({
|
|
14
|
+
type: "reorderFields",
|
|
15
|
+
fields: insights.frequentlyUsedFields
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
if (!modifications.length) {
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
return {
|
|
22
|
+
overlayId: options.overlayId,
|
|
23
|
+
version: options.version ?? "1.0.0",
|
|
24
|
+
appliesTo: {
|
|
25
|
+
tenantId: options.tenantId,
|
|
26
|
+
capability: options.capability,
|
|
27
|
+
userId: options.userId,
|
|
28
|
+
role: options.role
|
|
29
|
+
},
|
|
30
|
+
modifications,
|
|
31
|
+
metadata: {
|
|
32
|
+
generatedAt: new Date().toISOString(),
|
|
33
|
+
automated: true
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
function insightsToWorkflowAdaptations(insights) {
|
|
38
|
+
return insights.workflowBottlenecks.map((bottleneck) => ({
|
|
39
|
+
workflow: bottleneck.workflow,
|
|
40
|
+
step: bottleneck.step,
|
|
41
|
+
note: `High drop rate (${Math.round(bottleneck.dropRate * 100)}%) detected`
|
|
42
|
+
}));
|
|
43
|
+
}
|
|
6
44
|
|
|
7
|
-
|
|
45
|
+
// src/analyzer.ts
|
|
46
|
+
var DEFAULT_THRESHOLD = 3;
|
|
47
|
+
|
|
48
|
+
class BehaviorAnalyzer {
|
|
49
|
+
store;
|
|
50
|
+
options;
|
|
51
|
+
constructor(store, options = {}) {
|
|
52
|
+
this.store = store;
|
|
53
|
+
this.options = options;
|
|
54
|
+
}
|
|
55
|
+
async analyze(params) {
|
|
56
|
+
const query = {
|
|
57
|
+
tenantId: params.tenantId,
|
|
58
|
+
userId: params.userId,
|
|
59
|
+
role: params.role
|
|
60
|
+
};
|
|
61
|
+
if (params.windowMs) {
|
|
62
|
+
query.since = Date.now() - params.windowMs;
|
|
63
|
+
}
|
|
64
|
+
const summary = await this.store.summarize(query);
|
|
65
|
+
return buildInsights(summary, this.options);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
function buildInsights(summary, options) {
|
|
69
|
+
const threshold = options.fieldInactivityThreshold ?? DEFAULT_THRESHOLD;
|
|
70
|
+
const minSamples = options.minSamples ?? 10;
|
|
71
|
+
const ignoredFields = [];
|
|
72
|
+
const frequentlyUsedFields = [];
|
|
73
|
+
for (const [field, count] of Object.entries(summary.fieldCounts)) {
|
|
74
|
+
if (count <= threshold) {
|
|
75
|
+
ignoredFields.push(field);
|
|
76
|
+
}
|
|
77
|
+
if (count >= threshold * 4) {
|
|
78
|
+
frequentlyUsedFields.push(field);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
const workflowBottlenecks = Object.entries(summary.workflowStepCounts).flatMap(([workflow, steps]) => {
|
|
82
|
+
const total = Object.values(steps).reduce((acc, value) => acc + value, 0);
|
|
83
|
+
if (!total || total < minSamples) {
|
|
84
|
+
return [];
|
|
85
|
+
}
|
|
86
|
+
return Object.entries(steps).filter(([, count]) => count / total < 0.4).map(([step, count]) => ({
|
|
87
|
+
workflow,
|
|
88
|
+
step,
|
|
89
|
+
dropRate: 1 - count / total
|
|
90
|
+
}));
|
|
91
|
+
});
|
|
92
|
+
const layoutPreference = detectLayout(summary);
|
|
93
|
+
return {
|
|
94
|
+
unusedFields: ignoredFields,
|
|
95
|
+
suggestedHiddenFields: ignoredFields.slice(0, 5),
|
|
96
|
+
frequentlyUsedFields: frequentlyUsedFields.slice(0, 10),
|
|
97
|
+
workflowBottlenecks,
|
|
98
|
+
layoutPreference
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
function detectLayout(summary) {
|
|
102
|
+
const fieldCount = Object.keys(summary.fieldCounts).length;
|
|
103
|
+
if (!fieldCount) {
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
if (fieldCount >= 15) {
|
|
107
|
+
return "table";
|
|
108
|
+
}
|
|
109
|
+
if (fieldCount >= 8) {
|
|
110
|
+
return "grid";
|
|
111
|
+
}
|
|
112
|
+
return "form";
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// src/docs/behavior-tracking.docblock.ts
|
|
116
|
+
import { registerDocBlocks } from "@contractspec/lib.contracts/docs";
|
|
117
|
+
var personalization_behavior_tracking_DocBlocks = [
|
|
118
|
+
{
|
|
119
|
+
id: "docs.personalization.behavior-tracking",
|
|
120
|
+
title: "Behavior Tracking",
|
|
121
|
+
summary: "`@contractspec/lib.personalization` provides primitives to observe how tenants/users interact with specs and turn that telemetry into personalization insights.",
|
|
122
|
+
kind: "reference",
|
|
123
|
+
visibility: "public",
|
|
124
|
+
route: "/docs/personalization/behavior-tracking",
|
|
125
|
+
tags: ["personalization", "behavior-tracking"],
|
|
126
|
+
body: `# Behavior Tracking
|
|
127
|
+
|
|
128
|
+
\`@contractspec/lib.personalization\` provides primitives to observe how tenants/users interact with specs and turn that telemetry into personalization insights.
|
|
129
|
+
|
|
130
|
+
## Tracker
|
|
131
|
+
|
|
132
|
+
\`\`\`ts
|
|
133
|
+
import { createBehaviorTracker } from '@contractspec/lib.personalization';
|
|
134
|
+
import { InMemoryBehaviorStore } from '@contractspec/lib.personalization/store';
|
|
135
|
+
|
|
136
|
+
const tracker = createBehaviorTracker({
|
|
137
|
+
store: new InMemoryBehaviorStore(),
|
|
138
|
+
context: { tenantId: ctx.tenant.id, userId: ctx.identity.userId },
|
|
139
|
+
autoFlushIntervalMs: 5000,
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
tracker.trackFieldAccess({ operation: 'billing.createOrder', field: 'internalNotes' });
|
|
143
|
+
tracker.trackFeatureUsage({ feature: 'workflow-editor', action: 'opened' });
|
|
144
|
+
tracker.trackWorkflowStep({ workflow: 'invoice-approval', step: 'review', status: 'entered' });
|
|
145
|
+
\`\`\`
|
|
146
|
+
|
|
147
|
+
All events are buffered and flushed either when the buffer hits 25 entries or when \`autoFlushIntervalMs\` elapses. Tracked metrics flow to OpenTelemetry via the meter/counter built into the tracker.
|
|
148
|
+
|
|
149
|
+
## Analyzer
|
|
150
|
+
|
|
151
|
+
\`\`\`ts
|
|
152
|
+
import { BehaviorAnalyzer } from '@contractspec/lib.personalization/analyzer';
|
|
153
|
+
|
|
154
|
+
const analyzer = new BehaviorAnalyzer(store, { fieldInactivityThreshold: 2 });
|
|
155
|
+
const insights = await analyzer.analyze({ tenantId: 'acme', userId: 'manager-42', windowMs: 7 * 24 * 60 * 60 * 1000 });
|
|
156
|
+
|
|
157
|
+
/*
|
|
158
|
+
{
|
|
159
|
+
unusedFields: ['internalNotes'],
|
|
160
|
+
suggestedHiddenFields: ['internalNotes'],
|
|
161
|
+
frequentlyUsedFields: ['customerReference', 'items'],
|
|
162
|
+
workflowBottlenecks: [{ workflow: 'invoice-approval', step: 'finance-review', dropRate: 0.6 }],
|
|
163
|
+
layoutPreference: 'table'
|
|
164
|
+
}
|
|
165
|
+
*/
|
|
166
|
+
\`\`\`
|
|
167
|
+
|
|
168
|
+
Use the analyzer output with the overlay adapter to generate suggestions automatically.
|
|
169
|
+
|
|
170
|
+
## Adapter
|
|
171
|
+
|
|
172
|
+
\`\`\`ts
|
|
173
|
+
import { insightsToOverlaySuggestion } from '@contractspec/lib.personalization/adapter';
|
|
174
|
+
|
|
175
|
+
const overlay = insightsToOverlaySuggestion(insights, {
|
|
176
|
+
overlayId: 'acme-order-form',
|
|
177
|
+
tenantId: 'acme',
|
|
178
|
+
capability: 'billing.createOrder',
|
|
179
|
+
});
|
|
180
|
+
\`\`\`
|
|
181
|
+
|
|
182
|
+
When the adapter returns an overlay spec, pass it to the overlay engine to register or sign it.
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
`
|
|
206
|
+
}
|
|
207
|
+
];
|
|
208
|
+
registerDocBlocks(personalization_behavior_tracking_DocBlocks);
|
|
209
|
+
|
|
210
|
+
// src/docs/overlay-engine.docblock.ts
|
|
211
|
+
import { registerDocBlocks as registerDocBlocks2 } from "@contractspec/lib.contracts/docs";
|
|
212
|
+
var personalization_overlay_engine_DocBlocks = [
|
|
213
|
+
{
|
|
214
|
+
id: "docs.personalization.overlay-engine",
|
|
215
|
+
title: "Overlay Engine",
|
|
216
|
+
summary: "`@contractspec/lib.overlay-engine` is the canonical runtime for OverlaySpecs. It validates specs, tracks scope precedence, and exposes hooks for React renderers.",
|
|
217
|
+
kind: "reference",
|
|
218
|
+
visibility: "public",
|
|
219
|
+
route: "/docs/personalization/overlay-engine",
|
|
220
|
+
tags: ["personalization", "overlay-engine"],
|
|
221
|
+
body: `# Overlay Engine
|
|
222
|
+
|
|
223
|
+
\`@contractspec/lib.overlay-engine\` is the canonical runtime for OverlaySpecs. It validates specs, tracks scope precedence, and exposes hooks for React renderers.
|
|
224
|
+
|
|
225
|
+
## Key APIs
|
|
226
|
+
|
|
227
|
+
- \`defineOverlay(spec)\` \u2013 helper to keep specs typed.
|
|
228
|
+
- \`OverlayRegistry\` \u2013 register signed overlays and retrieve them per context.
|
|
229
|
+
- \`OverlayEngine\` \u2013 apply overlays to renderable targets, emit audit events, and merge modifications deterministically.
|
|
230
|
+
- \`signOverlay(spec, privateKey)\` \u2013 Ed25519/RSA-PSS signer.
|
|
231
|
+
- \`verifyOverlaySignature(overlay)\` \u2013 verify public key signatures.
|
|
232
|
+
- \`useOverlay(engine, params)\` \u2013 client hook that returns \`{ target, overlaysApplied }\`.
|
|
233
|
+
|
|
234
|
+
## Scope Precedence
|
|
235
|
+
|
|
236
|
+
Registrations are sorted by specificity:
|
|
237
|
+
|
|
238
|
+
1. Tenant overlays
|
|
239
|
+
2. Role overlays
|
|
240
|
+
3. User overlays
|
|
241
|
+
4. Device overlays
|
|
242
|
+
|
|
243
|
+
Less specific overlays run first; more specific overlays override later.
|
|
244
|
+
|
|
245
|
+
## Example
|
|
246
|
+
|
|
247
|
+
\`\`\`ts
|
|
248
|
+
const registry = new OverlayRegistry();
|
|
249
|
+
const engine = new OverlayEngine({
|
|
250
|
+
registry,
|
|
251
|
+
audit: (event) => auditLogService.record(event),
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
const overlay = defineOverlay({
|
|
255
|
+
overlayId: 'acme-order-form',
|
|
256
|
+
version: '1.0.0',
|
|
257
|
+
appliesTo: {
|
|
258
|
+
capability: 'billing.createOrder',
|
|
259
|
+
tenantId: 'acme',
|
|
260
|
+
},
|
|
261
|
+
modifications: [
|
|
262
|
+
{ type: 'hideField', field: 'internalNotes' },
|
|
263
|
+
{ type: 'renameLabel', field: 'customerReference', newLabel: 'PO Number' },
|
|
264
|
+
],
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
const signedOverlay = await signOverlay(overlay, privateKeyPem);
|
|
268
|
+
registry.register(signedOverlay);
|
|
269
|
+
|
|
270
|
+
const result = engine.apply({
|
|
271
|
+
target: { fields: baseFields },
|
|
272
|
+
capability: 'billing.createOrder',
|
|
273
|
+
tenantId: 'acme',
|
|
274
|
+
userId: 'manager-7',
|
|
275
|
+
});
|
|
276
|
+
\`\`\`
|
|
277
|
+
|
|
278
|
+
\`result.target.fields\` now carries the hidden and renamed outputs ready for rendering.
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
|
|
282
|
+
|
|
283
|
+
|
|
284
|
+
|
|
285
|
+
|
|
286
|
+
|
|
287
|
+
|
|
288
|
+
|
|
289
|
+
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
|
|
293
|
+
|
|
294
|
+
|
|
295
|
+
|
|
296
|
+
|
|
297
|
+
|
|
298
|
+
|
|
299
|
+
|
|
300
|
+
|
|
301
|
+
`
|
|
302
|
+
}
|
|
303
|
+
];
|
|
304
|
+
registerDocBlocks2(personalization_overlay_engine_DocBlocks);
|
|
305
|
+
|
|
306
|
+
// src/docs/workflow-composition.docblock.ts
|
|
307
|
+
import { registerDocBlocks as registerDocBlocks3 } from "@contractspec/lib.contracts/docs";
|
|
308
|
+
var personalization_workflow_composition_DocBlocks = [
|
|
309
|
+
{
|
|
310
|
+
id: "docs.personalization.workflow-composition",
|
|
311
|
+
title: "Workflow Composition",
|
|
312
|
+
summary: "`@contractspec/lib.workflow-composer` composes base WorkflowSpecs with tenant/role/device-specific extensions.",
|
|
313
|
+
kind: "reference",
|
|
314
|
+
visibility: "public",
|
|
315
|
+
route: "/docs/personalization/workflow-composition",
|
|
316
|
+
tags: ["personalization", "workflow-composition"],
|
|
317
|
+
body: `# Workflow Composition
|
|
318
|
+
|
|
319
|
+
\`@contractspec/lib.workflow-composer\` composes base WorkflowSpecs with tenant/role/device-specific extensions.
|
|
320
|
+
|
|
321
|
+
## Extensions
|
|
322
|
+
|
|
323
|
+
\`\`\`ts
|
|
324
|
+
import { WorkflowComposer } from '@contractspec/lib.workflow-composer';
|
|
325
|
+
import { approvalStepTemplate } from '@contractspec/lib.workflow-composer/templates';
|
|
326
|
+
|
|
327
|
+
const composer = new WorkflowComposer();
|
|
328
|
+
|
|
329
|
+
composer.register({
|
|
330
|
+
workflow: 'billing.invoiceApproval',
|
|
331
|
+
tenantId: 'acme',
|
|
332
|
+
priority: 10,
|
|
333
|
+
customSteps: [
|
|
334
|
+
{
|
|
335
|
+
after: 'validate-invoice',
|
|
336
|
+
inject: approvalStepTemplate({
|
|
337
|
+
id: 'acme-legal-review',
|
|
338
|
+
label: 'Legal Review (ACME)',
|
|
339
|
+
description: 'Tenant-specific compliance step.',
|
|
340
|
+
}),
|
|
341
|
+
transitionTo: 'final-approval',
|
|
342
|
+
},
|
|
343
|
+
],
|
|
344
|
+
hiddenSteps: ['internal-audit'],
|
|
345
|
+
});
|
|
346
|
+
\`\`\`
|
|
347
|
+
|
|
348
|
+
## Compose
|
|
349
|
+
|
|
350
|
+
\`\`\`ts
|
|
351
|
+
const runtimeSpec = composer.compose({
|
|
352
|
+
base: BaseInvoiceWorkflow,
|
|
353
|
+
tenantId: 'acme',
|
|
354
|
+
});
|
|
355
|
+
|
|
356
|
+
workflowRunner.execute(runtimeSpec, ctx);
|
|
357
|
+
\`\`\`
|
|
358
|
+
|
|
359
|
+
The composer uses anchor references (\`after\`/\`before\`) to place injected steps and cleans up transitions when steps are hidden.
|
|
360
|
+
|
|
361
|
+
|
|
362
|
+
|
|
363
|
+
|
|
364
|
+
|
|
365
|
+
|
|
366
|
+
|
|
367
|
+
|
|
368
|
+
|
|
369
|
+
|
|
370
|
+
|
|
371
|
+
|
|
372
|
+
|
|
373
|
+
|
|
374
|
+
|
|
375
|
+
|
|
376
|
+
|
|
377
|
+
|
|
378
|
+
|
|
379
|
+
|
|
380
|
+
|
|
381
|
+
|
|
382
|
+
`
|
|
383
|
+
}
|
|
384
|
+
];
|
|
385
|
+
registerDocBlocks3(personalization_workflow_composition_DocBlocks);
|
|
386
|
+
// src/store.ts
|
|
387
|
+
class InMemoryBehaviorStore {
|
|
388
|
+
events = [];
|
|
389
|
+
async record(event) {
|
|
390
|
+
this.events.push(event);
|
|
391
|
+
}
|
|
392
|
+
async bulkRecord(events) {
|
|
393
|
+
this.events.push(...events);
|
|
394
|
+
}
|
|
395
|
+
async query(query) {
|
|
396
|
+
return filterEvents(this.events, query);
|
|
397
|
+
}
|
|
398
|
+
async summarize(query) {
|
|
399
|
+
const events = await this.query(query);
|
|
400
|
+
const summary = {
|
|
401
|
+
fieldCounts: {},
|
|
402
|
+
featureCounts: {},
|
|
403
|
+
workflowStepCounts: {},
|
|
404
|
+
totalEvents: events.length
|
|
405
|
+
};
|
|
406
|
+
events.forEach((event) => {
|
|
407
|
+
switch (event.type) {
|
|
408
|
+
case "field_access":
|
|
409
|
+
summary.fieldCounts[event.field] = (summary.fieldCounts[event.field] ?? 0) + 1;
|
|
410
|
+
break;
|
|
411
|
+
case "feature_usage":
|
|
412
|
+
summary.featureCounts[event.feature] = (summary.featureCounts[event.feature] ?? 0) + 1;
|
|
413
|
+
break;
|
|
414
|
+
case "workflow_step": {
|
|
415
|
+
const workflow = summary.workflowStepCounts[event.workflow] ??= {};
|
|
416
|
+
workflow[event.step] = (workflow[event.step] ?? 0) + 1;
|
|
417
|
+
break;
|
|
418
|
+
}
|
|
419
|
+
default:
|
|
420
|
+
break;
|
|
421
|
+
}
|
|
422
|
+
});
|
|
423
|
+
return summary;
|
|
424
|
+
}
|
|
425
|
+
async clear() {
|
|
426
|
+
this.events = [];
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
function filterEvents(events, query) {
|
|
430
|
+
return events.filter((event) => {
|
|
431
|
+
if (query.tenantId && event.tenantId !== query.tenantId) {
|
|
432
|
+
return false;
|
|
433
|
+
}
|
|
434
|
+
if (query.userId && event.userId !== query.userId) {
|
|
435
|
+
return false;
|
|
436
|
+
}
|
|
437
|
+
if (query.role && event.role !== query.role) {
|
|
438
|
+
return false;
|
|
439
|
+
}
|
|
440
|
+
if (query.since && event.timestamp < query.since) {
|
|
441
|
+
return false;
|
|
442
|
+
}
|
|
443
|
+
if (query.until && event.timestamp > query.until) {
|
|
444
|
+
return false;
|
|
445
|
+
}
|
|
446
|
+
if (query.operation && event.type === "field_access" && event.operation !== query.operation) {
|
|
447
|
+
return false;
|
|
448
|
+
}
|
|
449
|
+
if (query.feature && event.type === "feature_usage" && event.feature !== query.feature) {
|
|
450
|
+
return false;
|
|
451
|
+
}
|
|
452
|
+
if (query.workflow && event.type === "workflow_step" && event.workflow !== query.workflow) {
|
|
453
|
+
return false;
|
|
454
|
+
}
|
|
455
|
+
return true;
|
|
456
|
+
});
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
// src/tracker.ts
|
|
460
|
+
import { trace, metrics } from "@opentelemetry/api";
|
|
461
|
+
var DEFAULT_BUFFER_SIZE = 25;
|
|
462
|
+
|
|
463
|
+
class BehaviorTracker {
|
|
464
|
+
store;
|
|
465
|
+
context;
|
|
466
|
+
tracer = trace.getTracer("lssm.personalization", "1.0.0");
|
|
467
|
+
counter = metrics.getMeter("lssm.personalization", "1.0.0").createCounter("lssm.personalization.events", {
|
|
468
|
+
description: "Behavior events tracked for personalization"
|
|
469
|
+
});
|
|
470
|
+
buffer = [];
|
|
471
|
+
bufferSize;
|
|
472
|
+
flushTimer;
|
|
473
|
+
constructor(options) {
|
|
474
|
+
this.store = options.store;
|
|
475
|
+
this.context = options.context;
|
|
476
|
+
this.bufferSize = options.bufferSize ?? DEFAULT_BUFFER_SIZE;
|
|
477
|
+
if (options.autoFlushIntervalMs) {
|
|
478
|
+
this.flushTimer = setInterval(() => {
|
|
479
|
+
this.flush();
|
|
480
|
+
}, options.autoFlushIntervalMs);
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
trackFieldAccess(input) {
|
|
484
|
+
const event = {
|
|
485
|
+
type: "field_access",
|
|
486
|
+
operation: input.operation,
|
|
487
|
+
field: input.field,
|
|
488
|
+
timestamp: Date.now(),
|
|
489
|
+
...this.context,
|
|
490
|
+
metadata: { ...this.context.metadata, ...input.metadata }
|
|
491
|
+
};
|
|
492
|
+
this.enqueue(event);
|
|
493
|
+
}
|
|
494
|
+
trackFeatureUsage(input) {
|
|
495
|
+
const event = {
|
|
496
|
+
type: "feature_usage",
|
|
497
|
+
feature: input.feature,
|
|
498
|
+
action: input.action,
|
|
499
|
+
timestamp: Date.now(),
|
|
500
|
+
...this.context,
|
|
501
|
+
metadata: { ...this.context.metadata, ...input.metadata }
|
|
502
|
+
};
|
|
503
|
+
this.enqueue(event);
|
|
504
|
+
}
|
|
505
|
+
trackWorkflowStep(input) {
|
|
506
|
+
const event = {
|
|
507
|
+
type: "workflow_step",
|
|
508
|
+
workflow: input.workflow,
|
|
509
|
+
step: input.step,
|
|
510
|
+
status: input.status,
|
|
511
|
+
timestamp: Date.now(),
|
|
512
|
+
...this.context,
|
|
513
|
+
metadata: { ...this.context.metadata, ...input.metadata }
|
|
514
|
+
};
|
|
515
|
+
this.enqueue(event);
|
|
516
|
+
}
|
|
517
|
+
async flush() {
|
|
518
|
+
if (!this.buffer.length)
|
|
519
|
+
return;
|
|
520
|
+
const events = this.buffer;
|
|
521
|
+
this.buffer = [];
|
|
522
|
+
await this.store.bulkRecord(events);
|
|
523
|
+
}
|
|
524
|
+
async dispose() {
|
|
525
|
+
if (this.flushTimer) {
|
|
526
|
+
clearInterval(this.flushTimer);
|
|
527
|
+
}
|
|
528
|
+
await this.flush();
|
|
529
|
+
}
|
|
530
|
+
enqueue(event) {
|
|
531
|
+
this.buffer.push(event);
|
|
532
|
+
this.counter.add(1, {
|
|
533
|
+
tenantId: this.context.tenantId,
|
|
534
|
+
type: event.type
|
|
535
|
+
});
|
|
536
|
+
this.tracer.startActiveSpan(`personalization.${event.type}`, (span) => {
|
|
537
|
+
span.setAttribute("tenant.id", this.context.tenantId);
|
|
538
|
+
if (this.context.userId)
|
|
539
|
+
span.setAttribute("user.id", this.context.userId);
|
|
540
|
+
span.setAttribute("personalization.event_type", event.type);
|
|
541
|
+
span.end();
|
|
542
|
+
});
|
|
543
|
+
if (this.buffer.length >= this.bufferSize) {
|
|
544
|
+
this.flush();
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
var createBehaviorTracker = (options) => new BehaviorTracker(options);
|
|
549
|
+
export {
|
|
550
|
+
insightsToWorkflowAdaptations,
|
|
551
|
+
insightsToOverlaySuggestion,
|
|
552
|
+
createBehaviorTracker,
|
|
553
|
+
InMemoryBehaviorStore,
|
|
554
|
+
BehaviorTracker,
|
|
555
|
+
BehaviorAnalyzer
|
|
556
|
+
};
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// src/adapter.ts
|
|
2
|
+
function insightsToOverlaySuggestion(insights, options) {
|
|
3
|
+
const modifications = [];
|
|
4
|
+
insights.suggestedHiddenFields.forEach((field) => {
|
|
5
|
+
modifications.push({
|
|
6
|
+
type: "hideField",
|
|
7
|
+
field,
|
|
8
|
+
reason: "Automatically hidden because usage is near zero"
|
|
9
|
+
});
|
|
10
|
+
});
|
|
11
|
+
if (insights.frequentlyUsedFields.length) {
|
|
12
|
+
modifications.push({
|
|
13
|
+
type: "reorderFields",
|
|
14
|
+
fields: insights.frequentlyUsedFields
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
if (!modifications.length) {
|
|
18
|
+
return null;
|
|
19
|
+
}
|
|
20
|
+
return {
|
|
21
|
+
overlayId: options.overlayId,
|
|
22
|
+
version: options.version ?? "1.0.0",
|
|
23
|
+
appliesTo: {
|
|
24
|
+
tenantId: options.tenantId,
|
|
25
|
+
capability: options.capability,
|
|
26
|
+
userId: options.userId,
|
|
27
|
+
role: options.role
|
|
28
|
+
},
|
|
29
|
+
modifications,
|
|
30
|
+
metadata: {
|
|
31
|
+
generatedAt: new Date().toISOString(),
|
|
32
|
+
automated: true
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
function insightsToWorkflowAdaptations(insights) {
|
|
37
|
+
return insights.workflowBottlenecks.map((bottleneck) => ({
|
|
38
|
+
workflow: bottleneck.workflow,
|
|
39
|
+
step: bottleneck.step,
|
|
40
|
+
note: `High drop rate (${Math.round(bottleneck.dropRate * 100)}%) detected`
|
|
41
|
+
}));
|
|
42
|
+
}
|
|
43
|
+
export {
|
|
44
|
+
insightsToWorkflowAdaptations,
|
|
45
|
+
insightsToOverlaySuggestion
|
|
46
|
+
};
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
// src/analyzer.ts
|
|
2
|
+
var DEFAULT_THRESHOLD = 3;
|
|
3
|
+
|
|
4
|
+
class BehaviorAnalyzer {
|
|
5
|
+
store;
|
|
6
|
+
options;
|
|
7
|
+
constructor(store, options = {}) {
|
|
8
|
+
this.store = store;
|
|
9
|
+
this.options = options;
|
|
10
|
+
}
|
|
11
|
+
async analyze(params) {
|
|
12
|
+
const query = {
|
|
13
|
+
tenantId: params.tenantId,
|
|
14
|
+
userId: params.userId,
|
|
15
|
+
role: params.role
|
|
16
|
+
};
|
|
17
|
+
if (params.windowMs) {
|
|
18
|
+
query.since = Date.now() - params.windowMs;
|
|
19
|
+
}
|
|
20
|
+
const summary = await this.store.summarize(query);
|
|
21
|
+
return buildInsights(summary, this.options);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
function buildInsights(summary, options) {
|
|
25
|
+
const threshold = options.fieldInactivityThreshold ?? DEFAULT_THRESHOLD;
|
|
26
|
+
const minSamples = options.minSamples ?? 10;
|
|
27
|
+
const ignoredFields = [];
|
|
28
|
+
const frequentlyUsedFields = [];
|
|
29
|
+
for (const [field, count] of Object.entries(summary.fieldCounts)) {
|
|
30
|
+
if (count <= threshold) {
|
|
31
|
+
ignoredFields.push(field);
|
|
32
|
+
}
|
|
33
|
+
if (count >= threshold * 4) {
|
|
34
|
+
frequentlyUsedFields.push(field);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
const workflowBottlenecks = Object.entries(summary.workflowStepCounts).flatMap(([workflow, steps]) => {
|
|
38
|
+
const total = Object.values(steps).reduce((acc, value) => acc + value, 0);
|
|
39
|
+
if (!total || total < minSamples) {
|
|
40
|
+
return [];
|
|
41
|
+
}
|
|
42
|
+
return Object.entries(steps).filter(([, count]) => count / total < 0.4).map(([step, count]) => ({
|
|
43
|
+
workflow,
|
|
44
|
+
step,
|
|
45
|
+
dropRate: 1 - count / total
|
|
46
|
+
}));
|
|
47
|
+
});
|
|
48
|
+
const layoutPreference = detectLayout(summary);
|
|
49
|
+
return {
|
|
50
|
+
unusedFields: ignoredFields,
|
|
51
|
+
suggestedHiddenFields: ignoredFields.slice(0, 5),
|
|
52
|
+
frequentlyUsedFields: frequentlyUsedFields.slice(0, 10),
|
|
53
|
+
workflowBottlenecks,
|
|
54
|
+
layoutPreference
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
function detectLayout(summary) {
|
|
58
|
+
const fieldCount = Object.keys(summary.fieldCounts).length;
|
|
59
|
+
if (!fieldCount) {
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
if (fieldCount >= 15) {
|
|
63
|
+
return "table";
|
|
64
|
+
}
|
|
65
|
+
if (fieldCount >= 8) {
|
|
66
|
+
return "grid";
|
|
67
|
+
}
|
|
68
|
+
return "form";
|
|
69
|
+
}
|
|
70
|
+
export {
|
|
71
|
+
BehaviorAnalyzer
|
|
72
|
+
};
|