begeniux 0.1.0 → 0.2.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/index.d.ts CHANGED
@@ -1,10 +1,14 @@
1
- import * as react_jsx_runtime from 'react/jsx-runtime';
2
1
  import * as React$1 from 'react';
3
2
 
4
3
  type BehaviorEvent = {
5
4
  kind: "click";
6
5
  target: string;
7
6
  t: number;
7
+ } | {
8
+ kind: "rage-click";
9
+ target: string;
10
+ count: number;
11
+ t: number;
8
12
  } | {
9
13
  kind: "scroll";
10
14
  depth: number;
@@ -19,76 +23,291 @@ type BehaviorEvent = {
19
23
  target: string;
20
24
  durationMs: number;
21
25
  t: number;
26
+ } | {
27
+ kind: "focus";
28
+ target: string;
29
+ t: number;
30
+ } | {
31
+ kind: "blur";
32
+ target: string;
33
+ durationMs: number;
34
+ t: number;
35
+ } | {
36
+ kind: "input";
37
+ target: string;
38
+ t: number;
39
+ } | {
40
+ kind: "submit";
41
+ target: string;
42
+ t: number;
43
+ } | {
44
+ kind: "viewport-change";
45
+ width: number;
46
+ height: number;
47
+ t: number;
48
+ } | {
49
+ kind: "error";
50
+ message: string;
51
+ t: number;
52
+ } | {
53
+ kind: "custom";
54
+ name: string;
55
+ data: Record<string, unknown>;
56
+ t: number;
22
57
  };
23
- type BehaviorSummary = {
58
+ type BehaviorSummary<TContext = {
59
+ route: string;
60
+ }> = {
24
61
  clicks_per_min: number;
62
+ rage_clicks: number;
25
63
  avg_dwell_ms: number;
26
64
  scroll_depth: number;
27
65
  hover_count: number;
66
+ form_interactions: number;
67
+ errors_seen: number;
28
68
  events_seen: number;
29
- page_context: {
30
- route: string;
31
- visible_product_ids: string[];
69
+ viewport: {
70
+ width: number;
71
+ height: number;
32
72
  };
73
+ custom: Record<string, number | string | boolean>;
74
+ page_context: TContext;
75
+ };
76
+ type Adaptation = {
77
+ kind: "set-css-var";
78
+ selector: string;
79
+ name: string;
80
+ value: string;
81
+ } | {
82
+ kind: "add-class";
83
+ selector: string;
84
+ className: string;
85
+ } | {
86
+ kind: "remove-class";
87
+ selector: string;
88
+ className: string;
89
+ } | {
90
+ kind: "set-style";
91
+ selector: string;
92
+ property: string;
93
+ value: string;
94
+ } | {
95
+ kind: "set-attribute";
96
+ selector: string;
97
+ name: string;
98
+ value: string;
99
+ } | {
100
+ kind: "set-aria-label";
101
+ selector: string;
102
+ value: string;
33
103
  };
34
- type Variant = "decisive" | "deliberate" | "neutral";
35
- type AgentDirective = {
36
- variant: Variant;
104
+ type AdaptationPlan = {
105
+ adaptations: Adaptation[];
37
106
  confidence: number;
38
107
  reasoning: string;
108
+ meta?: Record<string, unknown>;
109
+ };
110
+ type AdaptInput<TContext = {
111
+ route: string;
112
+ }> = {
113
+ summary: BehaviorSummary<TContext>;
114
+ designSystem: DesignSystem;
115
+ dom: {
116
+ visibleSelectors: string[];
117
+ route: string;
118
+ };
119
+ };
120
+ type ClassifyFn<TContext = {
121
+ route: string;
122
+ }> = (input: AdaptInput<TContext>) => Promise<AdaptationPlan>;
123
+ type CssVariableSpec = {
124
+ description: string;
125
+ type: "color";
126
+ defaultValue?: string;
127
+ } | {
128
+ description: string;
129
+ type: "length";
130
+ defaultValue?: string;
131
+ } | {
132
+ description: string;
133
+ type: "number";
134
+ range?: [number, number];
135
+ defaultValue?: string;
136
+ } | {
137
+ description: string;
138
+ type: "string";
139
+ defaultValue?: string;
140
+ } | {
141
+ description: string;
142
+ type: "enum";
143
+ values: string[];
144
+ defaultValue?: string;
145
+ };
146
+ type DesignSystem = {
147
+ cssVariables?: Record<string, CssVariableSpec>;
148
+ classes?: Record<string, string>;
149
+ examples?: AdaptationPlan[];
150
+ };
151
+ type ScopeOpts = {
152
+ allow?: string[];
153
+ deny?: string[];
154
+ };
155
+ type BehaviorListener = {
156
+ attach: (target: HTMLElement, push: (e: BehaviorEvent) => void) => () => void;
157
+ };
158
+ type BeGenContextValue<TContext = {
159
+ route: string;
160
+ }> = {
161
+ summary: BehaviorSummary<TContext> | null;
162
+ lastPlan: AdaptationPlan | null;
163
+ appliedAdaptations: ReadonlyArray<Adaptation>;
164
+ recentEvents: ReadonlyArray<BehaviorEvent>;
165
+ /**
166
+ * Apply an AdaptationPlan via the provider's engine. Used by external
167
+ * adapters (e.g. CopilotKit frontend tool handler) that source plans
168
+ * outside the provider's `classify` loop.
169
+ */
170
+ applyPlan: (plan: AdaptationPlan) => void;
171
+ /** Read-only access to the active design system. */
172
+ getDesignSystem: () => DesignSystem;
39
173
  };
40
- type ClassifyFn = (summary: BehaviorSummary) => Promise<AgentDirective>;
41
-
42
- type BeGenContextValue = {
43
- variant: Variant;
44
- directive: AgentDirective | null;
45
- summary: BehaviorSummary | null;
46
- setVariant: (v: Variant) => void;
47
- setDirective: (d: AgentDirective) => void;
48
- setSummary: (s: BehaviorSummary) => void;
49
- };
50
- declare function BeGenProvider({ children }: {
51
- children: React$1.ReactNode;
52
- }): react_jsx_runtime.JSX.Element;
53
174
 
54
- type BeGenSurfaceProps = {
55
- variants: Record<Variant, React$1.ComponentType<any>>;
56
- classify: ClassifyFn;
57
- variantProps?: Record<string, any>;
58
- pageContext: BehaviorSummary["page_context"];
59
- seedPersona?: "decisive" | "deliberate";
175
+ type BeGenProviderProps<TContext = {
176
+ route: string;
177
+ }> = {
178
+ /** The agent's vocabulary — what CSS variables and classes it may emit. */
179
+ designSystem: DesignSystem;
180
+ /** Per-page state forwarded into every BehaviorSummary. */
181
+ pageContext: TContext;
182
+ /**
183
+ * Single-pass agent. Receives behavior + DOM + design system, returns a plan.
184
+ * If omitted, the provider does NOT call any agent — it only tracks behavior
185
+ * and exposes summaries via context. (Pair with createCopilotKitAdapter or
186
+ * createHttpAdapter for real adaptation.)
187
+ */
188
+ classify?: ClassifyFn<TContext>;
189
+ /** Restrict where the agent's adaptations may target. */
190
+ scope?: ScopeOpts;
191
+ /** Minimum ms between agent calls. Default 5000. */
60
192
  rateLimitMs?: number;
61
- className?: string;
62
- style?: React$1.CSSProperties;
63
- children?: never;
193
+ /** Don't trigger an agent call until at least this many events have flushed since the last call. Default 5. */
194
+ triggerEveryEvents?: number;
195
+ /** Tracker idle-flush window. */
196
+ flushAfterMs?: number;
197
+ /** Tracker count-flush. */
198
+ flushEveryEvents?: number;
199
+ /** Tracker ring-buffer cap. */
200
+ bufferSize?: number;
201
+ /** Plug-in event sources. */
202
+ customListeners?: BehaviorListener[];
203
+ /** Element to scope listeners to. Default: document.body. */
204
+ containerRef?: React$1.RefObject<HTMLElement | null>;
205
+ /** Pre-canned events injected on mount (for demos). */
206
+ seedTrace?: BehaviorEvent[];
207
+ children: React$1.ReactNode;
64
208
  };
65
- declare function BeGenSurface(props: BeGenSurfaceProps): react_jsx_runtime.JSX.Element;
209
+ declare function useBeGenContext<TContext = {
210
+ route: string;
211
+ }>(): BeGenContextValue<TContext>;
212
+ declare function BeGenProvider<TContext = {
213
+ route: string;
214
+ }>(props: BeGenProviderProps<TContext>): React$1.ReactElement;
66
215
 
67
- type UseBehaviorTrackerOpts = {
68
- containerRef: React.RefObject<HTMLElement>;
216
+ type UseBehaviorTrackerOpts<TContext = {
217
+ route: string;
218
+ }> = {
219
+ /** DOM element to scope listeners to. Defaults to document.body. */
220
+ containerRef?: React.RefObject<HTMLElement | null>;
221
+ /** Flush after this many events. Default 10. */
69
222
  flushEveryEvents?: number;
223
+ /** Flush after this many ms of idle. Default 5000. */
70
224
  flushAfterMs?: number;
225
+ /** Ring buffer cap. Default 50. */
71
226
  bufferSize?: number;
72
- onFlush: (summary: BehaviorSummary) => void;
73
- pageContext: BehaviorSummary["page_context"];
227
+ /** Called on every flush with the computed summary. */
228
+ onFlush: (summary: BehaviorSummary<TContext>) => void;
229
+ /** Page context (route, etc.) — included in every summary. */
230
+ pageContext: TContext;
231
+ /** Optional pre-canned events injected on mount. */
74
232
  seedTrace?: BehaviorEvent[];
233
+ /** Plug-in event sources beyond the built-ins. */
234
+ customListeners?: BehaviorListener[];
75
235
  };
76
- declare function useBehaviorTracker(opts: UseBehaviorTrackerOpts): {
236
+ declare function useBehaviorTracker<TContext = {
237
+ route: string;
238
+ }>(opts: UseBehaviorTrackerOpts<TContext>): {
77
239
  recentEvents: BehaviorEvent[];
78
240
  };
79
241
 
80
- declare function useBeGenContext(): BeGenContextValue;
81
-
82
- type CreateGeminiClassifierOpts = {
83
- apiKey: string;
84
- model?: string;
85
- endpoint?: string;
86
- fetchImpl?: typeof fetch;
242
+ type AdaptationEngineEvent = {
243
+ kind: "applied";
244
+ adaptation: Adaptation;
245
+ matched: number;
246
+ } | {
247
+ kind: "skipped";
248
+ adaptation: Adaptation;
249
+ reason: string;
250
+ } | {
251
+ kind: "reverted";
252
+ count: number;
253
+ };
254
+ type AdaptationEngineOpts = {
255
+ root: HTMLElement;
256
+ scope?: ScopeOpts;
257
+ onEvent?: (e: AdaptationEngineEvent) => void;
87
258
  };
88
- declare function createGeminiClassifier(opts: CreateGeminiClassifierOpts): ClassifyFn;
259
+ declare class AdaptationEngine {
260
+ private root;
261
+ private scope;
262
+ private onEvent?;
263
+ private revertLog;
264
+ private appliedSnapshot;
265
+ constructor(opts: AdaptationEngineOpts);
266
+ /** Apply a plan. Reverts the previous plan first so mutations don't accumulate. */
267
+ apply(plan: AdaptationPlan): void;
268
+ /** Roll back every adaptation applied since the last revertAll. */
269
+ revertAll(): void;
270
+ /** Read-only view of currently applied adaptations (for telemetry). */
271
+ getApplied(): ReadonlyArray<Adaptation>;
272
+ private applyOne;
273
+ private resolveTargets;
274
+ private selectorMatchesPattern;
275
+ private applyToElement;
276
+ }
89
277
 
90
- declare function createHeuristicClassifier(): ClassifyFn;
278
+ type DomSnapshotOpts = {
279
+ /** Maximum number of selectors to return (default 50). */
280
+ maxSelectors?: number;
281
+ /** Scope filter — only include elements whose selectors match allow / not deny. */
282
+ scope?: ScopeOpts;
283
+ };
284
+ /**
285
+ * Walk the visible DOM under `root` and return a deduplicated list of
286
+ * stable CSS selectors the agent can use. Prefers semantic anchors
287
+ * (id, data-* attributes, role, semantic tag names) over class soup.
288
+ */
289
+ declare function snapshotVisibleSelectors(root: HTMLElement, opts?: DomSnapshotOpts): string[];
91
290
 
92
- declare const PERSONAS: Record<"decisive" | "deliberate", BehaviorEvent[]>;
291
+ type HttpAdapterOpts = {
292
+ /** Endpoint that accepts POST {AdaptInput} and returns AdaptationPlan. */
293
+ url: string;
294
+ /** Extra headers (auth, etc.). */
295
+ headers?: Record<string, string>;
296
+ /** Optional fetch override (testing / SSR / proxy). */
297
+ fetchImpl?: typeof fetch;
298
+ /** Abort the request after this many ms. Default 8000. */
299
+ timeoutMs?: number;
300
+ /** Custom request body shaper if your endpoint expects a different envelope. */
301
+ bodyTransform?: (input: AdaptInput) => unknown;
302
+ /** Custom response shaper if your endpoint returns a different envelope. */
303
+ responseTransform?: (raw: unknown) => AdaptationPlan;
304
+ };
305
+ /**
306
+ * Generic transport. Posts {summary, designSystem, dom} to the given URL and
307
+ * expects an AdaptationPlan back. Use this when you have a custom API route
308
+ * (Next.js, Hono, Express, anything) that calls your LLM server-side and
309
+ * shapes the result into the contract.
310
+ */
311
+ declare function createHttpAdapter(opts: HttpAdapterOpts): ClassifyFn;
93
312
 
94
- export { type AgentDirective, type BeGenContextValue, BeGenProvider, BeGenSurface, type BeGenSurfaceProps, type BehaviorEvent, type BehaviorSummary, type ClassifyFn, type CreateGeminiClassifierOpts, PERSONAS, type UseBehaviorTrackerOpts, type Variant, createGeminiClassifier, createHeuristicClassifier, useBeGenContext, useBehaviorTracker };
313
+ export { type AdaptInput, type Adaptation, AdaptationEngine, type AdaptationEngineEvent, type AdaptationEngineOpts, type AdaptationPlan, type BeGenContextValue, BeGenProvider, type BeGenProviderProps, type BehaviorEvent, type BehaviorListener, type BehaviorSummary, type ClassifyFn, type CssVariableSpec, type DesignSystem, type DomSnapshotOpts, type HttpAdapterOpts, type ScopeOpts, type UseBehaviorTrackerOpts, createHttpAdapter, snapshotVisibleSelectors, useBeGenContext, useBehaviorTracker };