@lssm/lib.personalization 0.0.0-canary-20251206160926

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/README.md ADDED
@@ -0,0 +1,16 @@
1
+ # @lssm/lib.personalization
2
+
3
+ Composable behavior tracking and personalization primitives for ContractSpec applications. The library provides trackers that capture feature usage, analyzers that transform raw events into adaptation hints, and adapters that feed personalized overlays or workflow tweaks.
4
+
5
+ ## Modules
6
+
7
+ - `tracker` – lightweight tracker helpers that record field/feature/workflow usage.
8
+ - `store` – store abstractions plus an in-memory implementation.
9
+ - `analyzer` – stateless analyzers that convert usage data into insights.
10
+ - `adapter` – bridges insights into OverlaySpecs or WorkflowComposer extensions.
11
+
12
+ See `docs/tech/personalization/behavior-tracking.md` for full usage notes.
13
+
14
+
15
+
16
+
@@ -0,0 +1,21 @@
1
+ import { BehaviorInsights } from "./types.js";
2
+ import { OverlaySpec } from "@lssm/lib.overlay-engine";
3
+
4
+ //#region src/adapter.d.ts
5
+ interface OverlaySuggestionOptions {
6
+ overlayId: string;
7
+ tenantId: string;
8
+ capability: string;
9
+ userId?: string;
10
+ role?: string;
11
+ version?: string;
12
+ }
13
+ interface WorkflowAdaptation {
14
+ workflow: string;
15
+ step: string;
16
+ note: string;
17
+ }
18
+ declare function insightsToOverlaySuggestion(insights: BehaviorInsights, options: OverlaySuggestionOptions): OverlaySpec | null;
19
+ declare function insightsToWorkflowAdaptations(insights: BehaviorInsights): WorkflowAdaptation[];
20
+ //#endregion
21
+ export { OverlaySuggestionOptions, WorkflowAdaptation, insightsToOverlaySuggestion, insightsToWorkflowAdaptations };
@@ -0,0 +1 @@
1
+ function e(e,t){let n=[];return e.suggestedHiddenFields.forEach(e=>{n.push({type:`hideField`,field:e,reason:`Automatically hidden because usage is near zero`})}),e.frequentlyUsedFields.length&&n.push({type:`reorderFields`,fields:e.frequentlyUsedFields}),n.length?{overlayId:t.overlayId,version:t.version??`1.0.0`,appliesTo:{tenantId:t.tenantId,capability:t.capability,userId:t.userId,role:t.role},modifications:n,metadata:{generatedAt:new Date().toISOString(),automated:!0}}:null}function t(e){return e.workflowBottlenecks.map(e=>({workflow:e.workflow,step:e.step,note:`High drop rate (${Math.round(e.dropRate*100)}%) detected`}))}export{e as insightsToOverlaySuggestion,t as insightsToWorkflowAdaptations};
@@ -0,0 +1,22 @@
1
+ import { BehaviorInsights } from "./types.js";
2
+ import { BehaviorStore } from "./store.js";
3
+
4
+ //#region src/analyzer.d.ts
5
+ interface BehaviorAnalyzerOptions {
6
+ fieldInactivityThreshold?: number;
7
+ minSamples?: number;
8
+ }
9
+ interface AnalyzeParams {
10
+ tenantId: string;
11
+ userId?: string;
12
+ role?: string;
13
+ windowMs?: number;
14
+ }
15
+ declare class BehaviorAnalyzer {
16
+ private readonly store;
17
+ private readonly options;
18
+ constructor(store: BehaviorStore, options?: BehaviorAnalyzerOptions);
19
+ analyze(params: AnalyzeParams): Promise<BehaviorInsights>;
20
+ }
21
+ //#endregion
22
+ export { AnalyzeParams, BehaviorAnalyzer, BehaviorAnalyzerOptions };
@@ -0,0 +1 @@
1
+ var e=class{constructor(e,t={}){this.store=e,this.options=t}async analyze(e){let n={tenantId:e.tenantId,userId:e.userId,role:e.role};return e.windowMs&&(n.since=Date.now()-e.windowMs),t(await this.store.summarize(n),this.options)}};function t(e,t){let r=t.fieldInactivityThreshold??3,i=t.minSamples??10,a=[],o=[];for(let[t,n]of Object.entries(e.fieldCounts))n<=r&&a.push(t),n>=r*4&&o.push(t);let s=Object.entries(e.workflowStepCounts).flatMap(([e,t])=>{let n=Object.values(t).reduce((e,t)=>e+t,0);return!n||n<i?[]:Object.entries(t).filter(([,e])=>e/n<.4).map(([t,r])=>({workflow:e,step:t,dropRate:1-r/n}))}),c=n(e);return{unusedFields:a,suggestedHiddenFields:a.slice(0,5),frequentlyUsedFields:o.slice(0,10),workflowBottlenecks:s,layoutPreference:c}}function n(e){let t=Object.keys(e.fieldCounts).length;if(t)return t>=15?`table`:t>=8?`grid`:`form`}export{e as BehaviorAnalyzer};
@@ -0,0 +1,6 @@
1
+ import { BehaviorEvent, BehaviorEventBase, BehaviorEventType, BehaviorInsights, BehaviorQuery, BehaviorSummary, FeatureUsageEvent, FieldAccessEvent, WorkflowStepEvent } from "./types.js";
2
+ import { BehaviorStore, InMemoryBehaviorStore } from "./store.js";
3
+ import { BehaviorTracker, BehaviorTrackerContext, BehaviorTrackerOptions, TrackFeatureUsageInput, TrackFieldAccessInput, TrackWorkflowStepInput, createBehaviorTracker } from "./tracker.js";
4
+ import { AnalyzeParams, BehaviorAnalyzer, BehaviorAnalyzerOptions } from "./analyzer.js";
5
+ import { OverlaySuggestionOptions, WorkflowAdaptation, insightsToOverlaySuggestion, insightsToWorkflowAdaptations } from "./adapter.js";
6
+ export { AnalyzeParams, BehaviorAnalyzer, BehaviorAnalyzerOptions, BehaviorEvent, BehaviorEventBase, BehaviorEventType, BehaviorInsights, BehaviorQuery, BehaviorStore, BehaviorSummary, BehaviorTracker, BehaviorTrackerContext, BehaviorTrackerOptions, FeatureUsageEvent, FieldAccessEvent, InMemoryBehaviorStore, OverlaySuggestionOptions, TrackFeatureUsageInput, TrackFieldAccessInput, TrackWorkflowStepInput, WorkflowAdaptation, WorkflowStepEvent, createBehaviorTracker, insightsToOverlaySuggestion, insightsToWorkflowAdaptations };
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ import{InMemoryBehaviorStore as e}from"./store.js";import{BehaviorTracker as t,createBehaviorTracker as n}from"./tracker.js";import{BehaviorAnalyzer as r}from"./analyzer.js";import{insightsToOverlaySuggestion as i,insightsToWorkflowAdaptations as a}from"./adapter.js";export{r as BehaviorAnalyzer,t as BehaviorTracker,e as InMemoryBehaviorStore,n as createBehaviorTracker,i as insightsToOverlaySuggestion,a as insightsToWorkflowAdaptations};
@@ -0,0 +1,20 @@
1
+ import { BehaviorEvent, BehaviorQuery, BehaviorSummary } from "./types.js";
2
+
3
+ //#region src/store.d.ts
4
+ interface BehaviorStore {
5
+ record(event: BehaviorEvent): Promise<void> | void;
6
+ bulkRecord(events: BehaviorEvent[]): Promise<void> | void;
7
+ query(query: BehaviorQuery): Promise<BehaviorEvent[]>;
8
+ summarize(query: BehaviorQuery): Promise<BehaviorSummary>;
9
+ clear?(): Promise<void> | void;
10
+ }
11
+ declare class InMemoryBehaviorStore implements BehaviorStore {
12
+ private events;
13
+ record(event: BehaviorEvent): Promise<void>;
14
+ bulkRecord(events: BehaviorEvent[]): Promise<void>;
15
+ query(query: BehaviorQuery): Promise<BehaviorEvent[]>;
16
+ summarize(query: BehaviorQuery): Promise<BehaviorSummary>;
17
+ clear(): Promise<void>;
18
+ }
19
+ //#endregion
20
+ export { BehaviorStore, InMemoryBehaviorStore };
package/dist/store.js ADDED
@@ -0,0 +1 @@
1
+ var e=class{events=[];async record(e){this.events.push(e)}async bulkRecord(e){this.events.push(...e)}async query(e){return t(this.events,e)}async summarize(e){let t=await this.query(e),n={fieldCounts:{},featureCounts:{},workflowStepCounts:{},totalEvents:t.length};return t.forEach(e=>{switch(e.type){case`field_access`:n.fieldCounts[e.field]=(n.fieldCounts[e.field]??0)+1;break;case`feature_usage`:n.featureCounts[e.feature]=(n.featureCounts[e.feature]??0)+1;break;case`workflow_step`:{let t=n.workflowStepCounts[e.workflow]??={};t[e.step]=(t[e.step]??0)+1;break}default:break}}),n}async clear(){this.events=[]}};function t(e,t){return e.filter(e=>!(t.tenantId&&e.tenantId!==t.tenantId||t.userId&&e.userId!==t.userId||t.role&&e.role!==t.role||t.since&&e.timestamp<t.since||t.until&&e.timestamp>t.until||t.operation&&e.type===`field_access`&&e.operation!==t.operation||t.feature&&e.type===`feature_usage`&&e.feature!==t.feature||t.workflow&&e.type===`workflow_step`&&e.workflow!==t.workflow))}export{e as InMemoryBehaviorStore};
@@ -0,0 +1,52 @@
1
+ import { BehaviorStore } from "./store.js";
2
+
3
+ //#region src/tracker.d.ts
4
+ interface BehaviorTrackerContext {
5
+ tenantId: string;
6
+ userId?: string;
7
+ role?: string;
8
+ device?: string;
9
+ metadata?: Record<string, unknown>;
10
+ }
11
+ interface BehaviorTrackerOptions {
12
+ store: BehaviorStore;
13
+ context: BehaviorTrackerContext;
14
+ tracerName?: string;
15
+ autoFlushIntervalMs?: number;
16
+ bufferSize?: number;
17
+ }
18
+ interface TrackFieldAccessInput {
19
+ operation: string;
20
+ field: string;
21
+ metadata?: Record<string, unknown>;
22
+ }
23
+ interface TrackFeatureUsageInput {
24
+ feature: string;
25
+ action: 'opened' | 'completed' | 'dismissed';
26
+ metadata?: Record<string, unknown>;
27
+ }
28
+ interface TrackWorkflowStepInput {
29
+ workflow: string;
30
+ step: string;
31
+ status: 'entered' | 'completed' | 'skipped' | 'errored';
32
+ metadata?: Record<string, unknown>;
33
+ }
34
+ declare class BehaviorTracker {
35
+ private readonly store;
36
+ private readonly context;
37
+ private readonly tracer;
38
+ private readonly counter;
39
+ private buffer;
40
+ private readonly bufferSize;
41
+ private flushTimer?;
42
+ constructor(options: BehaviorTrackerOptions);
43
+ trackFieldAccess(input: TrackFieldAccessInput): void;
44
+ trackFeatureUsage(input: TrackFeatureUsageInput): void;
45
+ trackWorkflowStep(input: TrackWorkflowStepInput): void;
46
+ flush(): Promise<void>;
47
+ dispose(): Promise<void>;
48
+ private enqueue;
49
+ }
50
+ declare const createBehaviorTracker: (options: BehaviorTrackerOptions) => BehaviorTracker;
51
+ //#endregion
52
+ export { BehaviorTracker, BehaviorTrackerContext, BehaviorTrackerOptions, TrackFeatureUsageInput, TrackFieldAccessInput, TrackWorkflowStepInput, createBehaviorTracker };
@@ -0,0 +1 @@
1
+ import{metrics as e,trace as t}from"@opentelemetry/api";var n=class{store;context;tracer=t.getTracer(`lssm.personalization`,`1.0.0`);counter=e.getMeter(`lssm.personalization`,`1.0.0`).createCounter(`lssm.personalization.events`,{description:`Behavior events tracked for personalization`});buffer=[];bufferSize;flushTimer;constructor(e){this.store=e.store,this.context=e.context,this.bufferSize=e.bufferSize??25,e.autoFlushIntervalMs&&(this.flushTimer=setInterval(()=>{this.flush()},e.autoFlushIntervalMs))}trackFieldAccess(e){let t={type:`field_access`,operation:e.operation,field:e.field,timestamp:Date.now(),...this.context,metadata:{...this.context.metadata,...e.metadata}};this.enqueue(t)}trackFeatureUsage(e){let t={type:`feature_usage`,feature:e.feature,action:e.action,timestamp:Date.now(),...this.context,metadata:{...this.context.metadata,...e.metadata}};this.enqueue(t)}trackWorkflowStep(e){let t={type:`workflow_step`,workflow:e.workflow,step:e.step,status:e.status,timestamp:Date.now(),...this.context,metadata:{...this.context.metadata,...e.metadata}};this.enqueue(t)}async flush(){if(!this.buffer.length)return;let e=this.buffer;this.buffer=[],await this.store.bulkRecord(e)}async dispose(){this.flushTimer&&clearInterval(this.flushTimer),await this.flush()}enqueue(e){this.buffer.push(e),this.counter.add(1,{tenantId:this.context.tenantId,type:e.type}),this.tracer.startActiveSpan(`personalization.${e.type}`,t=>{t.setAttribute(`tenant.id`,this.context.tenantId),this.context.userId&&t.setAttribute(`user.id`,this.context.userId),t.setAttribute(`personalization.event_type`,e.type),t.end()}),this.buffer.length>=this.bufferSize&&this.flush()}};const r=e=>new n(e);export{n as BehaviorTracker,r as createBehaviorTracker};
@@ -0,0 +1,58 @@
1
+ //#region src/types.d.ts
2
+ type BehaviorEventType = 'field_access' | 'feature_usage' | 'workflow_step';
3
+ interface BehaviorEventBase {
4
+ id?: string;
5
+ tenantId: string;
6
+ userId?: string;
7
+ role?: string;
8
+ device?: string;
9
+ timestamp: number;
10
+ metadata?: Record<string, unknown>;
11
+ }
12
+ interface FieldAccessEvent extends BehaviorEventBase {
13
+ type: 'field_access';
14
+ operation: string;
15
+ field: string;
16
+ }
17
+ interface FeatureUsageEvent extends BehaviorEventBase {
18
+ type: 'feature_usage';
19
+ feature: string;
20
+ action: 'opened' | 'completed' | 'dismissed';
21
+ }
22
+ interface WorkflowStepEvent extends BehaviorEventBase {
23
+ type: 'workflow_step';
24
+ workflow: string;
25
+ step: string;
26
+ status: 'entered' | 'completed' | 'skipped' | 'errored';
27
+ }
28
+ type BehaviorEvent = FieldAccessEvent | FeatureUsageEvent | WorkflowStepEvent;
29
+ interface BehaviorQuery {
30
+ tenantId?: string;
31
+ userId?: string;
32
+ role?: string;
33
+ feature?: string;
34
+ operation?: string;
35
+ workflow?: string;
36
+ since?: number;
37
+ until?: number;
38
+ limit?: number;
39
+ }
40
+ interface BehaviorSummary {
41
+ fieldCounts: Record<string, number>;
42
+ featureCounts: Record<string, number>;
43
+ workflowStepCounts: Record<string, Record<string, number>>;
44
+ totalEvents: number;
45
+ }
46
+ interface BehaviorInsights {
47
+ unusedFields: string[];
48
+ frequentlyUsedFields: string[];
49
+ suggestedHiddenFields: string[];
50
+ workflowBottlenecks: {
51
+ workflow: string;
52
+ step: string;
53
+ dropRate: number;
54
+ }[];
55
+ layoutPreference?: 'form' | 'grid' | 'list' | 'table';
56
+ }
57
+ //#endregion
58
+ export { BehaviorEvent, BehaviorEventBase, BehaviorEventType, BehaviorInsights, BehaviorQuery, BehaviorSummary, FeatureUsageEvent, FieldAccessEvent, WorkflowStepEvent };
package/package.json ADDED
@@ -0,0 +1,46 @@
1
+ {
2
+ "name": "@lssm/lib.personalization",
3
+ "version": "0.0.0-canary-20251206160926",
4
+ "description": "Behavior tracking, analysis, and adaptation helpers for ContractSpec personalization.",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "module": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "files": [
10
+ "dist",
11
+ "README.md"
12
+ ],
13
+ "scripts": {
14
+ "publish:pkg": "bun publish --tolerate-republish --ignore-scripts --verbose",
15
+ "build": "bun build:bundle && bun build:types",
16
+ "build:bundle": "tsdown",
17
+ "build:types": "tsc --noEmit",
18
+ "dev": "bun build:bundle --watch",
19
+ "clean": "rimraf dist .turbo",
20
+ "lint": "bun lint:fix",
21
+ "lint:fix": "eslint src --fix",
22
+ "lint:check": "eslint src",
23
+ "test": "bun run"
24
+ },
25
+ "dependencies": {
26
+ "@lssm/lib.bus": "workspace:*",
27
+ "@lssm/lib.overlay-engine": "workspace:*",
28
+ "@opentelemetry/api": "^1.9.0"
29
+ },
30
+ "peerDependencies": {
31
+ "@opentelemetry/api": "^1.9.0"
32
+ },
33
+ "devDependencies": {
34
+ "@lssm/tool.tsdown": "workspace:*",
35
+ "@lssm/tool.typescript": "workspace:*",
36
+ "tsdown": "^0.17.0",
37
+ "typescript": "^5.9.3"
38
+ },
39
+ "exports": {
40
+ ".": "./dist/index.js",
41
+ "./*": "./*"
42
+ },
43
+ "publishConfig": {
44
+ "access": "public"
45
+ }
46
+ }