@vllnt/convex-experiments 0.1.0-canary.0b21dd5
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/LICENSE +21 -0
- package/README.md +161 -0
- package/dist/client/index.d.ts +168 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/client/index.js +154 -0
- package/dist/client/index.js.map +1 -0
- package/dist/client/types.d.ts +75 -0
- package/dist/client/types.d.ts.map +1 -0
- package/dist/client/types.js +3 -0
- package/dist/client/types.js.map +1 -0
- package/dist/component/_generated/api.d.ts +38 -0
- package/dist/component/_generated/api.d.ts.map +1 -0
- package/dist/component/_generated/api.js +31 -0
- package/dist/component/_generated/api.js.map +1 -0
- package/dist/component/_generated/component.d.ts +128 -0
- package/dist/component/_generated/component.d.ts.map +1 -0
- package/dist/component/_generated/component.js +11 -0
- package/dist/component/_generated/component.js.map +1 -0
- package/dist/component/_generated/dataModel.d.ts +46 -0
- package/dist/component/_generated/dataModel.d.ts.map +1 -0
- package/dist/component/_generated/dataModel.js +11 -0
- package/dist/component/_generated/dataModel.js.map +1 -0
- package/dist/component/_generated/server.d.ts +133 -0
- package/dist/component/_generated/server.d.ts.map +1 -0
- package/dist/component/_generated/server.js +80 -0
- package/dist/component/_generated/server.js.map +1 -0
- package/dist/component/convex.config.d.ts +3 -0
- package/dist/component/convex.config.d.ts.map +1 -0
- package/dist/component/convex.config.js +7 -0
- package/dist/component/convex.config.js.map +1 -0
- package/dist/component/mutations.d.ts +93 -0
- package/dist/component/mutations.d.ts.map +1 -0
- package/dist/component/mutations.js +359 -0
- package/dist/component/mutations.js.map +1 -0
- package/dist/component/queries.d.ts +72 -0
- package/dist/component/queries.d.ts.map +1 -0
- package/dist/component/queries.js +134 -0
- package/dist/component/queries.js.map +1 -0
- package/dist/component/schema.d.ts +99 -0
- package/dist/component/schema.d.ts.map +1 -0
- package/dist/component/schema.js +64 -0
- package/dist/component/schema.js.map +1 -0
- package/dist/component/validators.d.ts +109 -0
- package/dist/component/validators.d.ts.map +1 -0
- package/dist/component/validators.js +49 -0
- package/dist/component/validators.js.map +1 -0
- package/dist/react/index.d.ts +57 -0
- package/dist/react/index.d.ts.map +1 -0
- package/dist/react/index.js +39 -0
- package/dist/react/index.js.map +1 -0
- package/dist/shared.d.ts +32 -0
- package/dist/shared.d.ts.map +1 -0
- package/dist/shared.js +52 -0
- package/dist/shared.js.map +1 -0
- package/package.json +116 -0
- package/src/client/index.ts +343 -0
- package/src/client/types.ts +78 -0
- package/src/component/_generated/api.ts +54 -0
- package/src/component/_generated/component.ts +133 -0
- package/src/component/_generated/dataModel.ts +60 -0
- package/src/component/_generated/server.ts +169 -0
- package/src/component/convex.config.ts +9 -0
- package/src/component/mutations.ts +414 -0
- package/src/component/queries.ts +161 -0
- package/src/component/schema.ts +71 -0
- package/src/component/validators.ts +65 -0
- package/src/react/index.test.tsx +85 -0
- package/src/react/index.tsx +68 -0
- package/src/shared.ts +69 -0
- package/src/test.ts +12 -0
- package/src/unit.test.ts +84 -0
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sandboxed tables — the experimentation ledger's own concern. No host tables are
|
|
3
|
+
* touched. An experiment is unique within a `scope`; `subjectRef`, variant keys,
|
|
4
|
+
* and the scope are all opaque host-owned strings.
|
|
5
|
+
*
|
|
6
|
+
* - `experiments` — the experiment definition (variants, weights, lifecycle, salt).
|
|
7
|
+
* - `assignments` — a subject's sticky variant choice, persisted on first `assign`.
|
|
8
|
+
* - `exposures` — a deduped per-subject exposure tally (the measurable surface).
|
|
9
|
+
* - `variantTallies` — one row per `(scope, experiment, variant)` holding running
|
|
10
|
+
* `assigned` / `subjects` (distinct exposed) / `exposures` counts, maintained on
|
|
11
|
+
* write so `results` reads O(variants) rows instead of scanning every exposure.
|
|
12
|
+
*/
|
|
13
|
+
declare const _default: import("convex/server").SchemaDefinition<{
|
|
14
|
+
experiments: import("convex/server").TableDefinition<import("convex/values").VObject<{
|
|
15
|
+
createdAt: number;
|
|
16
|
+
key: string;
|
|
17
|
+
salt: string;
|
|
18
|
+
scope: string;
|
|
19
|
+
status: "draft" | "running" | "stopped";
|
|
20
|
+
variants: {
|
|
21
|
+
key: string;
|
|
22
|
+
weight: number;
|
|
23
|
+
}[];
|
|
24
|
+
}, {
|
|
25
|
+
key: import("convex/values").VString<string, "required">;
|
|
26
|
+
scope: import("convex/values").VString<string, "required">;
|
|
27
|
+
status: import("convex/values").VUnion<"draft" | "running" | "stopped", [import("convex/values").VLiteral<"draft", "required">, import("convex/values").VLiteral<"running", "required">, import("convex/values").VLiteral<"stopped", "required">], "required", never>;
|
|
28
|
+
variants: import("convex/values").VArray<{
|
|
29
|
+
key: string;
|
|
30
|
+
weight: number;
|
|
31
|
+
}[], import("convex/values").VObject<{
|
|
32
|
+
key: string;
|
|
33
|
+
weight: number;
|
|
34
|
+
}, {
|
|
35
|
+
key: import("convex/values").VString<string, "required">;
|
|
36
|
+
weight: import("convex/values").VFloat64<number, "required">;
|
|
37
|
+
}, "required", "key" | "weight">, "required">;
|
|
38
|
+
salt: import("convex/values").VString<string, "required">;
|
|
39
|
+
createdAt: import("convex/values").VFloat64<number, "required">;
|
|
40
|
+
}, "required", "createdAt" | "key" | "salt" | "scope" | "status" | "variants">, {
|
|
41
|
+
by_scope: ["scope", "_creationTime"];
|
|
42
|
+
by_scope_key: ["scope", "key", "_creationTime"];
|
|
43
|
+
}, {}, {}>;
|
|
44
|
+
assignments: import("convex/server").TableDefinition<import("convex/values").VObject<{
|
|
45
|
+
assignedAt: number;
|
|
46
|
+
experimentKey: string;
|
|
47
|
+
scope: string;
|
|
48
|
+
subjectRef: string;
|
|
49
|
+
variant: string;
|
|
50
|
+
}, {
|
|
51
|
+
scope: import("convex/values").VString<string, "required">;
|
|
52
|
+
experimentKey: import("convex/values").VString<string, "required">;
|
|
53
|
+
subjectRef: import("convex/values").VString<string, "required">;
|
|
54
|
+
variant: import("convex/values").VString<string, "required">;
|
|
55
|
+
assignedAt: import("convex/values").VFloat64<number, "required">;
|
|
56
|
+
}, "required", "assignedAt" | "experimentKey" | "scope" | "subjectRef" | "variant">, {
|
|
57
|
+
by_scope_experiment_subject: ["scope", "experimentKey", "subjectRef", "_creationTime"];
|
|
58
|
+
}, {}, {}>;
|
|
59
|
+
exposures: import("convex/server").TableDefinition<import("convex/values").VObject<{
|
|
60
|
+
count: number;
|
|
61
|
+
experimentKey: string;
|
|
62
|
+
firstExposedAt: number;
|
|
63
|
+
lastExposedAt: number;
|
|
64
|
+
scope: string;
|
|
65
|
+
subjectRef: string;
|
|
66
|
+
variant: string;
|
|
67
|
+
}, {
|
|
68
|
+
scope: import("convex/values").VString<string, "required">;
|
|
69
|
+
experimentKey: import("convex/values").VString<string, "required">;
|
|
70
|
+
subjectRef: import("convex/values").VString<string, "required">;
|
|
71
|
+
variant: import("convex/values").VString<string, "required">;
|
|
72
|
+
firstExposedAt: import("convex/values").VFloat64<number, "required">;
|
|
73
|
+
lastExposedAt: import("convex/values").VFloat64<number, "required">;
|
|
74
|
+
count: import("convex/values").VFloat64<number, "required">;
|
|
75
|
+
}, "required", "count" | "experimentKey" | "firstExposedAt" | "lastExposedAt" | "scope" | "subjectRef" | "variant">, {
|
|
76
|
+
by_scope_experiment: ["scope", "experimentKey", "_creationTime"];
|
|
77
|
+
by_scope_experiment_subject: ["scope", "experimentKey", "subjectRef", "_creationTime"];
|
|
78
|
+
}, {}, {}>;
|
|
79
|
+
variantTallies: import("convex/server").TableDefinition<import("convex/values").VObject<{
|
|
80
|
+
assigned: number;
|
|
81
|
+
experimentKey: string;
|
|
82
|
+
exposures: number;
|
|
83
|
+
scope: string;
|
|
84
|
+
subjects: number;
|
|
85
|
+
variant: string;
|
|
86
|
+
}, {
|
|
87
|
+
scope: import("convex/values").VString<string, "required">;
|
|
88
|
+
experimentKey: import("convex/values").VString<string, "required">;
|
|
89
|
+
variant: import("convex/values").VString<string, "required">;
|
|
90
|
+
assigned: import("convex/values").VFloat64<number, "required">;
|
|
91
|
+
subjects: import("convex/values").VFloat64<number, "required">;
|
|
92
|
+
exposures: import("convex/values").VFloat64<number, "required">;
|
|
93
|
+
}, "required", "assigned" | "experimentKey" | "exposures" | "scope" | "subjects" | "variant">, {
|
|
94
|
+
by_scope_experiment: ["scope", "experimentKey", "_creationTime"];
|
|
95
|
+
by_scope_experiment_variant: ["scope", "experimentKey", "variant", "_creationTime"];
|
|
96
|
+
}, {}, {}>;
|
|
97
|
+
}, true>;
|
|
98
|
+
export default _default;
|
|
99
|
+
//# sourceMappingURL=schema.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../../src/component/schema.ts"],"names":[],"mappings":"AAIA;;;;;;;;;;;GAWG"}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { defineSchema, defineTable } from "convex/server";
|
|
2
|
+
import { v } from "convex/values";
|
|
3
|
+
import { variant } from "./validators";
|
|
4
|
+
/**
|
|
5
|
+
* Sandboxed tables — the experimentation ledger's own concern. No host tables are
|
|
6
|
+
* touched. An experiment is unique within a `scope`; `subjectRef`, variant keys,
|
|
7
|
+
* and the scope are all opaque host-owned strings.
|
|
8
|
+
*
|
|
9
|
+
* - `experiments` — the experiment definition (variants, weights, lifecycle, salt).
|
|
10
|
+
* - `assignments` — a subject's sticky variant choice, persisted on first `assign`.
|
|
11
|
+
* - `exposures` — a deduped per-subject exposure tally (the measurable surface).
|
|
12
|
+
* - `variantTallies` — one row per `(scope, experiment, variant)` holding running
|
|
13
|
+
* `assigned` / `subjects` (distinct exposed) / `exposures` counts, maintained on
|
|
14
|
+
* write so `results` reads O(variants) rows instead of scanning every exposure.
|
|
15
|
+
*/
|
|
16
|
+
export default defineSchema({
|
|
17
|
+
experiments: defineTable({
|
|
18
|
+
key: v.string(),
|
|
19
|
+
scope: v.string(),
|
|
20
|
+
status: v.union(v.literal("draft"), v.literal("running"), v.literal("stopped")),
|
|
21
|
+
variants: v.array(variant),
|
|
22
|
+
salt: v.string(),
|
|
23
|
+
createdAt: v.number(),
|
|
24
|
+
})
|
|
25
|
+
.index("by_scope_key", ["scope", "key"])
|
|
26
|
+
.index("by_scope", ["scope"]),
|
|
27
|
+
assignments: defineTable({
|
|
28
|
+
scope: v.string(),
|
|
29
|
+
experimentKey: v.string(),
|
|
30
|
+
subjectRef: v.string(),
|
|
31
|
+
variant: v.string(),
|
|
32
|
+
assignedAt: v.number(),
|
|
33
|
+
}).index("by_scope_experiment_subject", [
|
|
34
|
+
"scope",
|
|
35
|
+
"experimentKey",
|
|
36
|
+
"subjectRef",
|
|
37
|
+
]),
|
|
38
|
+
exposures: defineTable({
|
|
39
|
+
scope: v.string(),
|
|
40
|
+
experimentKey: v.string(),
|
|
41
|
+
subjectRef: v.string(),
|
|
42
|
+
variant: v.string(),
|
|
43
|
+
firstExposedAt: v.number(),
|
|
44
|
+
lastExposedAt: v.number(),
|
|
45
|
+
count: v.number(),
|
|
46
|
+
})
|
|
47
|
+
.index("by_scope_experiment_subject", [
|
|
48
|
+
"scope",
|
|
49
|
+
"experimentKey",
|
|
50
|
+
"subjectRef",
|
|
51
|
+
])
|
|
52
|
+
.index("by_scope_experiment", ["scope", "experimentKey"]),
|
|
53
|
+
variantTallies: defineTable({
|
|
54
|
+
scope: v.string(),
|
|
55
|
+
experimentKey: v.string(),
|
|
56
|
+
variant: v.string(),
|
|
57
|
+
assigned: v.number(),
|
|
58
|
+
subjects: v.number(),
|
|
59
|
+
exposures: v.number(),
|
|
60
|
+
})
|
|
61
|
+
.index("by_scope_experiment_variant", ["scope", "experimentKey", "variant"])
|
|
62
|
+
.index("by_scope_experiment", ["scope", "experimentKey"]),
|
|
63
|
+
});
|
|
64
|
+
//# sourceMappingURL=schema.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schema.js","sourceRoot":"","sources":["../../src/component/schema.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,WAAW,EAAE,MA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AAM,EAAE;QACnB,cAAc,EAAE,CAAC,CAAC,MAAM,EAAE;QAC1B,aAAa,EAAE,CAAC,CAAC,MAAM,EAAE;QACzB,KAAK,EAAE,CAAC,CAAC,MAAM,EAAE;KAClB,CAAC;SACC,KAAK,CAAC,6BAA6B,EAAE;QACpC,OAAO;QACP,eAAe;QACf,YAAY;KACb,CAAC;SACD,KAAK,CAAC,qBAAqB,EAAE,CAAC,OAAO,EAAE,eAAe,CAAC,CAAC;IAE3D,cAAc,EAAE,WAAW,CAAC;QAC1B,KAAK,EAAE,CAAC,CAAC,MAAM,EAAE;QACjB,aAAa,EAAE,CAAC,CAAC,MAAM,EAAE;QACzB,OAAO,EAAE,CAAC,CAAC,MAAM,EAAE;QACnB,QAAQ,EAAE,CAAC,CAAC,MAAM,EAAE;QACpB,QAAQ,EAAE,CAAC,CAAC,MAAM,EAAE;QACpB,SAAS,EAAE,CAAC,CAAC,MAAM,EAAE;KACtB,CAAC;SACC,KAAK,CAAC,6BAA6B,EAAE,CAAC,OAAO,EAAE,eAAe,EAAE,SAAS,CAAC,CAAC;SAC3E,KAAK,CAAC,qBAAqB,EAAE,CAAC,OAAO,EAAE,eAAe,CAAC,CAAC;CAC5D,CAAC,CAAC"}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/** A weighted variant — an opaque variant key and its relative selection weight. */
|
|
2
|
+
export declare const variant: import("convex/values").VObject<{
|
|
3
|
+
key: string;
|
|
4
|
+
weight: number;
|
|
5
|
+
}, {
|
|
6
|
+
key: import("convex/values").VString<string, "required">;
|
|
7
|
+
weight: import("convex/values").VFloat64<number, "required">;
|
|
8
|
+
}, "required", "key" | "weight">;
|
|
9
|
+
/** Experiment lifecycle status. `running` is the only status that enrolls subjects. */
|
|
10
|
+
export declare const experimentStatus: import("convex/values").VUnion<"draft" | "running" | "stopped", [import("convex/values").VLiteral<"draft", "required">, import("convex/values").VLiteral<"running", "required">, import("convex/values").VLiteral<"stopped", "required">], "required", never>;
|
|
11
|
+
/**
|
|
12
|
+
* Result of `assign`. `{ variant: null }` — the experiment is absent or not
|
|
13
|
+
* `running`, so the subject is not enrolled (the host treats this as control).
|
|
14
|
+
* `{ variant, isNew }` — the enrolled variant key, with `isNew` true only on the
|
|
15
|
+
* call that first persisted the assignment.
|
|
16
|
+
*/
|
|
17
|
+
export declare const assignResult: import("convex/values").VUnion<{
|
|
18
|
+
variant: null;
|
|
19
|
+
} | {
|
|
20
|
+
isNew: boolean;
|
|
21
|
+
variant: string;
|
|
22
|
+
}, [import("convex/values").VObject<{
|
|
23
|
+
variant: null;
|
|
24
|
+
}, {
|
|
25
|
+
variant: import("convex/values").VNull<null, "required">;
|
|
26
|
+
}, "required", "variant">, import("convex/values").VObject<{
|
|
27
|
+
isNew: boolean;
|
|
28
|
+
variant: string;
|
|
29
|
+
}, {
|
|
30
|
+
variant: import("convex/values").VString<string, "required">;
|
|
31
|
+
isNew: import("convex/values").VBoolean<boolean, "required">;
|
|
32
|
+
}, "required", "isNew" | "variant">], "required", "isNew" | "variant">;
|
|
33
|
+
/**
|
|
34
|
+
* Result of `logExposure` / `peek`: the enrolled variant, or `null` when the
|
|
35
|
+
* subject is not enrolled (experiment absent or not `running`).
|
|
36
|
+
*/
|
|
37
|
+
export declare const exposureResult: import("convex/values").VUnion<{
|
|
38
|
+
variant: null;
|
|
39
|
+
} | {
|
|
40
|
+
variant: string;
|
|
41
|
+
}, [import("convex/values").VObject<{
|
|
42
|
+
variant: null;
|
|
43
|
+
}, {
|
|
44
|
+
variant: import("convex/values").VNull<null, "required">;
|
|
45
|
+
}, "required", "variant">, import("convex/values").VObject<{
|
|
46
|
+
variant: string;
|
|
47
|
+
}, {
|
|
48
|
+
variant: import("convex/values").VString<string, "required">;
|
|
49
|
+
}, "required", "variant">], "required", "variant">;
|
|
50
|
+
/** Projection of an experiment definition returned by `getExperiment` / `listExperiments`. */
|
|
51
|
+
export declare const experimentDef: import("convex/values").VObject<{
|
|
52
|
+
createdAt: number;
|
|
53
|
+
key: string;
|
|
54
|
+
salt: string;
|
|
55
|
+
scope: string;
|
|
56
|
+
status: "draft" | "running" | "stopped";
|
|
57
|
+
variants: {
|
|
58
|
+
key: string;
|
|
59
|
+
weight: number;
|
|
60
|
+
}[];
|
|
61
|
+
}, {
|
|
62
|
+
key: import("convex/values").VString<string, "required">;
|
|
63
|
+
scope: import("convex/values").VString<string, "required">;
|
|
64
|
+
status: import("convex/values").VUnion<"draft" | "running" | "stopped", [import("convex/values").VLiteral<"draft", "required">, import("convex/values").VLiteral<"running", "required">, import("convex/values").VLiteral<"stopped", "required">], "required", never>;
|
|
65
|
+
variants: import("convex/values").VArray<{
|
|
66
|
+
key: string;
|
|
67
|
+
weight: number;
|
|
68
|
+
}[], import("convex/values").VObject<{
|
|
69
|
+
key: string;
|
|
70
|
+
weight: number;
|
|
71
|
+
}, {
|
|
72
|
+
key: import("convex/values").VString<string, "required">;
|
|
73
|
+
weight: import("convex/values").VFloat64<number, "required">;
|
|
74
|
+
}, "required", "key" | "weight">, "required">;
|
|
75
|
+
salt: import("convex/values").VString<string, "required">;
|
|
76
|
+
createdAt: import("convex/values").VFloat64<number, "required">;
|
|
77
|
+
}, "required", "createdAt" | "key" | "salt" | "scope" | "status" | "variants">;
|
|
78
|
+
/** Projection of a subject's sticky assignment returned by `getAssignment`. */
|
|
79
|
+
export declare const assignmentProjection: import("convex/values").VObject<{
|
|
80
|
+
assignedAt: number;
|
|
81
|
+
variant: string;
|
|
82
|
+
}, {
|
|
83
|
+
variant: import("convex/values").VString<string, "required">;
|
|
84
|
+
assignedAt: import("convex/values").VFloat64<number, "required">;
|
|
85
|
+
}, "required", "assignedAt" | "variant">;
|
|
86
|
+
/**
|
|
87
|
+
* Per-variant tally returned by `results`. `assigned` and `subjects` together with
|
|
88
|
+
* the configured `weight` let an analyst compute sample-ratio-mismatch (observed
|
|
89
|
+
* vs expected split) and the funnel denominator host-side.
|
|
90
|
+
*/
|
|
91
|
+
export declare const variantResult: import("convex/values").VObject<{
|
|
92
|
+
assigned: number;
|
|
93
|
+
exposures: number;
|
|
94
|
+
subjects: number;
|
|
95
|
+
variant: string;
|
|
96
|
+
weight: number;
|
|
97
|
+
}, {
|
|
98
|
+
/** The variant key. */
|
|
99
|
+
variant: import("convex/values").VString<string, "required">;
|
|
100
|
+
/** Subjects assigned to this variant (the realized split for SRM). */
|
|
101
|
+
assigned: import("convex/values").VFloat64<number, "required">;
|
|
102
|
+
/** Distinct subjects exposed to this variant (the funnel denominator). */
|
|
103
|
+
subjects: import("convex/values").VFloat64<number, "required">;
|
|
104
|
+
/** Total exposure events recorded for this variant. */
|
|
105
|
+
exposures: import("convex/values").VFloat64<number, "required">;
|
|
106
|
+
/** The configured weight from the current definition (0 if the variant is gone). */
|
|
107
|
+
weight: import("convex/values").VFloat64<number, "required">;
|
|
108
|
+
}, "required", "assigned" | "exposures" | "subjects" | "variant" | "weight">;
|
|
109
|
+
//# sourceMappingURL=validators.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validators.d.ts","sourceRoot":"","sources":["../../src/component/validators.ts"],"names":[],"mappings":"AAEA,oFAAoF;AACpF,eAAO,MAAM,OAAO;;;;;;gCAAoD,CAAC;AAEzE,uFAAuF;AACvF,eAAO,MAAM,gBAAgB,+PAI5B,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,YAAY;;;;;;;;;;;;;;;sEAGxB,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,cAAc;;;;;;;;;;;;kDAG1B,CAAC;AAEF,8FAA8F;AAC9F,eAAO,MAAM,aAAa;;;;;;;;;;;;;;;;;;;;;;;;;;8EAOxB,CAAC;AAEH,+EAA+E;AAC/E,eAAO,MAAM,oBAAoB;;;;;;wCAG/B,CAAC;AAEH;;;;GAIG;AACH,eAAO,MAAM,aAAa;;;;;;;IACxB,uBAAuB;;IAEvB,sEAAsE;;IAEtE,0EAA0E;;IAE1E,uDAAuD;;IAEvD,oFAAoF;;4EAEpF,CAAC"}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { v } from "convex/values";
|
|
2
|
+
/** A weighted variant — an opaque variant key and its relative selection weight. */
|
|
3
|
+
export const variant = v.object({ key: v.string(), weight: v.number() });
|
|
4
|
+
/** Experiment lifecycle status. `running` is the only status that enrolls subjects. */
|
|
5
|
+
export const experimentStatus = v.union(v.literal("draft"), v.literal("running"), v.literal("stopped"));
|
|
6
|
+
/**
|
|
7
|
+
* Result of `assign`. `{ variant: null }` — the experiment is absent or not
|
|
8
|
+
* `running`, so the subject is not enrolled (the host treats this as control).
|
|
9
|
+
* `{ variant, isNew }` — the enrolled variant key, with `isNew` true only on the
|
|
10
|
+
* call that first persisted the assignment.
|
|
11
|
+
*/
|
|
12
|
+
export const assignResult = v.union(v.object({ variant: v.null() }), v.object({ variant: v.string(), isNew: v.boolean() }));
|
|
13
|
+
/**
|
|
14
|
+
* Result of `logExposure` / `peek`: the enrolled variant, or `null` when the
|
|
15
|
+
* subject is not enrolled (experiment absent or not `running`).
|
|
16
|
+
*/
|
|
17
|
+
export const exposureResult = v.union(v.object({ variant: v.null() }), v.object({ variant: v.string() }));
|
|
18
|
+
/** Projection of an experiment definition returned by `getExperiment` / `listExperiments`. */
|
|
19
|
+
export const experimentDef = v.object({
|
|
20
|
+
key: v.string(),
|
|
21
|
+
scope: v.string(),
|
|
22
|
+
status: experimentStatus,
|
|
23
|
+
variants: v.array(variant),
|
|
24
|
+
salt: v.string(),
|
|
25
|
+
createdAt: v.number(),
|
|
26
|
+
});
|
|
27
|
+
/** Projection of a subject's sticky assignment returned by `getAssignment`. */
|
|
28
|
+
export const assignmentProjection = v.object({
|
|
29
|
+
variant: v.string(),
|
|
30
|
+
assignedAt: v.number(),
|
|
31
|
+
});
|
|
32
|
+
/**
|
|
33
|
+
* Per-variant tally returned by `results`. `assigned` and `subjects` together with
|
|
34
|
+
* the configured `weight` let an analyst compute sample-ratio-mismatch (observed
|
|
35
|
+
* vs expected split) and the funnel denominator host-side.
|
|
36
|
+
*/
|
|
37
|
+
export const variantResult = v.object({
|
|
38
|
+
/** The variant key. */
|
|
39
|
+
variant: v.string(),
|
|
40
|
+
/** Subjects assigned to this variant (the realized split for SRM). */
|
|
41
|
+
assigned: v.number(),
|
|
42
|
+
/** Distinct subjects exposed to this variant (the funnel denominator). */
|
|
43
|
+
subjects: v.number(),
|
|
44
|
+
/** Total exposure events recorded for this variant. */
|
|
45
|
+
exposures: v.number(),
|
|
46
|
+
/** The configured weight from the current definition (0 if the variant is gone). */
|
|
47
|
+
weight: v.number(),
|
|
48
|
+
});
|
|
49
|
+
//# sourceMappingURL=validators.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validators.js","sourceRoot":"","sources":["../../src/component/validators.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,eAAe,CAAC;AAElC,oFAAoF;AACpF,MAAM,CAAC,MAAM,OAAO,GAAG,CAAC,CAAC,MAAM,CAAC,EAAE,GAAG,EAAE,CAAC,CAAC,MAAM,EAAE,EAAE,MAAM,EAAE,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;AAEzE,uFAAuF;AACvF,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAAC,CAAC,KAAK,CACrC,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC,EAClB,CAAC,CAAC,OAAO,CAAC,SAAS,CAAC,EACpB,CAAC,CAAC,OAAO,CAAC,SAAS,CAAC,CACrB,CAAC;AAEF;;;;;GAKG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG,CAAC,CAAC,KAAK,CACjC,CAAC,CAAC,MAAM,CAAC,EAAE,OAAO,EAAE,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,EAC/B,CAAC,CAAC,MAAM,CAAC,EAAE,OAAO,EAAE,CAAC,CAAC,MAAM,EAAE,EAAE,KAAK,EAAE,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,CACtD,CAAC;AAEF;;;GAGG;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iments`.
|
|
3
|
+
*
|
|
4
|
+
* Thin reactive hooks over `useQuery` from `convex/react`. Each takes the HOST's
|
|
5
|
+
* re-exported query reference plus its args — the component never imports the host
|
|
6
|
+
* `api`. `react` and `convex/react` are optional peer deps: a backend-only consumer
|
|
7
|
+
* pulls none of this code.
|
|
8
|
+
*
|
|
9
|
+
* NO-LEAK CONTRACT: these hooks expose only non-secret experiment data — the
|
|
10
|
+
* subject's own variant key, their assignment, and aggregate per-variant tallies.
|
|
11
|
+
* There is no secret or cross-subject payload. `useVariant` reads the deterministic
|
|
12
|
+
* `peek` query, so it paints the correct variant on first render (no flash of
|
|
13
|
+
* control); call the `logExposure` mutation to enroll + tally.
|
|
14
|
+
*/
|
|
15
|
+
import type { FunctionReference } from "convex/server";
|
|
16
|
+
import type { Assignment, VariantResult } from "../client/types.js";
|
|
17
|
+
/**
|
|
18
|
+
* Reactive, flicker-free variant for a subject — wraps the host's re-exported
|
|
19
|
+
* `peek` query (deterministic, read-only). Returns the variant key, `null` when not
|
|
20
|
+
* enrolled, or `undefined` while the query loads.
|
|
21
|
+
*/
|
|
22
|
+
export declare function useVariant(peekRef: FunctionReference<"query", "public", {
|
|
23
|
+
scope?: string;
|
|
24
|
+
key: string;
|
|
25
|
+
subjectRef: string;
|
|
26
|
+
}, {
|
|
27
|
+
variant: string | null;
|
|
28
|
+
}>, args: {
|
|
29
|
+
scope?: string;
|
|
30
|
+
key: string;
|
|
31
|
+
subjectRef: string;
|
|
32
|
+
}): string | null | undefined;
|
|
33
|
+
/**
|
|
34
|
+
* Reactive sticky assignment — wraps the host's re-exported `getAssignment` query.
|
|
35
|
+
* `null` when not enrolled, `undefined` while loading.
|
|
36
|
+
*/
|
|
37
|
+
export declare function useAssignment(getAssignmentRef: FunctionReference<"query", "public", {
|
|
38
|
+
scope?: string;
|
|
39
|
+
key: string;
|
|
40
|
+
subjectRef: string;
|
|
41
|
+
}, Assignment | null>, args: {
|
|
42
|
+
scope?: string;
|
|
43
|
+
key: string;
|
|
44
|
+
subjectRef: string;
|
|
45
|
+
}): Assignment | null | undefined;
|
|
46
|
+
/**
|
|
47
|
+
* Reactive per-variant tallies (`assigned`/`subjects`/`exposures`/`weight`) for a
|
|
48
|
+
* live results dashboard — wraps the host's re-exported `results` query.
|
|
49
|
+
*/
|
|
50
|
+
export declare function useExperimentResults(resultsRef: FunctionReference<"query", "public", {
|
|
51
|
+
scope?: string;
|
|
52
|
+
key: string;
|
|
53
|
+
}, VariantResult[]>, args: {
|
|
54
|
+
scope?: string;
|
|
55
|
+
key: string;
|
|
56
|
+
}): VariantResult[] | undefined;
|
|
57
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/react/index.tsx"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC;AAEvD,OAAO,KAAK,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AAEpE;;;;GAIG;AACH,wBAAgB,UAAU,CACxB,OAAO,EAAE,iBAAiB,CACxB,OAAO,EACP,QAAQ,EACR;IAAE,KAAK,CAAC,EAAE,MAAM,CAAC;IAAC,GAAG,EAAE,MAAM,CAAC;IAAC,UAAU,EAAE,MAAM,CAAA;CAAE,EACnD;IAAE,OAAO,EAAE,MAAM,GAAG,IAAI,CAAA;CAAE,CAC3B,EACD,IAAI,EAAE;IAAE,KAAK,CAAC,EAAE,MAAM,CAAC;IAAC,GAAG,EAAE,MAAM,CAAC;IAAC,UAAU,EAAE,MAAM,CAAA;CAAE,GACxD,MAAM,GAAG,IAAI,GAAG,SAAS,CAG3B;AAED;;;GAGG;AACH,wBAAgB,aAAa,CAC3B,gBAAgB,EAAE,iBAAiB,CACjC,OAAO,EACP,QAAQ,EACR;IAAE,KAAK,CAAC,EAAE,MAAM,CAAC;IAAC,GAAG,EAAE,MAAM,CAAC;IAAC,UAAU,EAAE,MAAM,CAAA;CAAE,EACnD,UAAU,GAAG,IAAI,CAClB,EACD,IAAI,EAAE;IAAE,KAAK,CAAC,EAAE,MAAM,CAAC;IAAC,GAAG,EAAE,MAAM,CAAC;IAAC,UAAU,EAAE,MAAM,CAAA;CAAE,GACxD,UAAU,GAAG,IAAI,GAAG,SAAS,CAE/B;AAED;;;GAGG;AACH,wBAAgB,oBAAoB,CAClC,UAAU,EAAE,iBAAiB,CAC3B,OAAO,EACP,QAAQ,EACR;IAAE,KAAK,CAAC,EAAE,MAAM,CAAC;IAAC,GAAG,EAAE,MAAM,CAAA;CAAE,EAC/B,aAAa,EAAE,CAChB,EACD,IAAI,EAAE;IAAE,KAAK,CAAC,EAAE,MAAM,CAAC;IAAC,GAAG,EAAE,MAAM,CAAA;CAAE,GACpC,aAAa,EAAE,GAAG,SAAS,CAE7B"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Optional, tree-shakeable React front-tooling for `@vllnt/convex-experiments`.
|
|
3
|
+
*
|
|
4
|
+
* Thin reactive hooks over `useQuery` from `convex/react`. Each takes the HOST's
|
|
5
|
+
* re-exported query reference plus its args — the component never imports the host
|
|
6
|
+
* `api`. `react` and `convex/react` are optional peer deps: a backend-only consumer
|
|
7
|
+
* pulls none of this code.
|
|
8
|
+
*
|
|
9
|
+
* NO-LEAK CONTRACT: these hooks expose only non-secret experiment data — the
|
|
10
|
+
* subject's own variant key, their assignment, and aggregate per-variant tallies.
|
|
11
|
+
* There is no secret or cross-subject payload. `useVariant` reads the deterministic
|
|
12
|
+
* `peek` query, so it paints the correct variant on first render (no flash of
|
|
13
|
+
* control); call the `logExposure` mutation to enroll + tally.
|
|
14
|
+
*/
|
|
15
|
+
import { useQuery } from "convex/react";
|
|
16
|
+
/**
|
|
17
|
+
* Reactive, flicker-free variant for a subject — wraps the host's re-exported
|
|
18
|
+
* `peek` query (deterministic, read-only). Returns the variant key, `null` when not
|
|
19
|
+
* enrolled, or `undefined` while the query loads.
|
|
20
|
+
*/
|
|
21
|
+
export function useVariant(peekRef, args) {
|
|
22
|
+
const result = useQuery(peekRef, args);
|
|
23
|
+
return result === undefined ? undefined : result.variant;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Reactive sticky assignment — wraps the host's re-exported `getAssignment` query.
|
|
27
|
+
* `null` when not enrolled, `undefined` while loading.
|
|
28
|
+
*/
|
|
29
|
+
export function useAssignment(getAssignmentRef, args) {
|
|
30
|
+
return useQuery(getAssignmentRef, args);
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Reactive per-variant tallies (`assigned`/`subjects`/`exposures`/`weight`) for a
|
|
34
|
+
* live results dashboard — wraps the host's re-exported `results` query.
|
|
35
|
+
*/
|
|
36
|
+
export function useExperimentResults(resultsRef, args) {
|
|
37
|
+
return useQuery(resultsRef, args);
|
|
38
|
+
}
|
|
39
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/react/index.tsx"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAGH,OAAO,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAGxC;;;;GAIG;AACH,MAAM,UAAU,UAAU,CACxB,OAKC,EACD,IAAyD;IAEzD,MAAM,MAAM,GAAG,QAAQ,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;IACvC,OAAO,MAAM,KAAK,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC;AAC3D,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,aAAa,CAC3B,gBAKC,EACD,IAAyD;IAEzD,OAAO,QAAQ,CAAC,gBAAgB,EAAE,IAAI,CAAC,CAAC;AAC1C,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,oBAAoB,CAClC,UAKC,EACD,IAAqC;IAErC,OAAO,QAAQ,CAAC,UAAU,EAAE,IAAI,CAAC,CAAC;AACpC,CAAC"}
|
package/dist/shared.d.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/** Shared constants + pure deterministic assignment used by both `client/` and `component/`. */
|
|
2
|
+
export declare const COMPONENT_NAME = "experiments";
|
|
3
|
+
/** Default namespace when the host does not scope an experiment. */
|
|
4
|
+
export declare const DEFAULT_SCOPE = "global";
|
|
5
|
+
/** Lifecycle status the client applies to `define` when the caller omits one. */
|
|
6
|
+
export declare const DEFAULT_STATUS = "running";
|
|
7
|
+
/** Default page size for a `deleteExperiment` cascade pass before it self-reschedules. */
|
|
8
|
+
export declare const DEFAULT_DELETE_BATCH = 200;
|
|
9
|
+
/** A weighted variant: an opaque variant key and its relative selection weight. */
|
|
10
|
+
export interface Variant {
|
|
11
|
+
/** The host's opaque variant key (e.g. `"control"`, `"treatment"`). */
|
|
12
|
+
key: string;
|
|
13
|
+
/** Relative selection weight; the bucket width is `weight / sum(weights)`. */
|
|
14
|
+
weight: number;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* FNV-1a 32-bit hash of `input`, returned as an unsigned integer. Deterministic,
|
|
18
|
+
* synchronous, and dependency-free — the component runs in a V8 isolate, so a
|
|
19
|
+
* pure string hash (rather than async `crypto.subtle`) is what keeps variant
|
|
20
|
+
* assignment a plain function callable inside a mutation.
|
|
21
|
+
*/
|
|
22
|
+
export declare function fnv1a(input: string): number;
|
|
23
|
+
/**
|
|
24
|
+
* Deterministically pick a variant for `subjectRef` from `variants`, weighted by
|
|
25
|
+
* each variant's `weight`. The same `(salt, subjectRef)` always maps to the same
|
|
26
|
+
* variant — sticky assignment with no storage — and the distribution matches the
|
|
27
|
+
* weights. The component folds `scope` into the salt so the same subject buckets
|
|
28
|
+
* independently per scope. Assumes a non-empty `variants` with positive weights;
|
|
29
|
+
* the caller validates that upstream.
|
|
30
|
+
*/
|
|
31
|
+
export declare function pickVariant(variants: readonly Variant[], salt: string, subjectRef: string): string;
|
|
32
|
+
//# sourceMappingURL=shared.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shared.d.ts","sourceRoot":"","sources":["../src/shared.ts"],"names":[],"mappings":"AAAA,gGAAgG;AAEhG,eAAO,MAAM,cAAc,gBAAgB,CAAC;AAE5C,oEAAoE;AACpE,eAAO,MAAM,aAAa,WAAW,CAAC;AAEtC,iFAAiF;AACjF,eAAO,MAAM,cAAc,YAAY,CAAC;AAExC,0FAA0F;AAC1F,eAAO,MAAM,oBAAoB,MAAM,CAAC;AAExC,mFAAmF;AACnF,MAAM,WAAW,OAAO;IACtB,uEAAuE;IACvE,GAAG,EAAE,MAAM,CAAC;IACZ,8EAA8E;IAC9E,MAAM,EAAE,MAAM,CAAC;CAChB;AAED;;;;;GAKG;AACH,wBAAgB,KAAK,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAO3C;AAED;;;;;;;GAOG;AACH,wBAAgB,WAAW,CACzB,QAAQ,EAAE,SAAS,OAAO,EAAE,EAC5B,IAAI,EAAE,MAAM,EACZ,UAAU,EAAE,MAAM,GACjB,MAAM,CAoBR"}
|
package/dist/shared.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/** Shared constants + pure deterministic assignment used by both `client/` and `component/`. */
|
|
2
|
+
export const COMPONENT_NAME = "experiments";
|
|
3
|
+
/** Default namespace when the host does not scope an experiment. */
|
|
4
|
+
export const DEFAULT_SCOPE = "global";
|
|
5
|
+
/** Lifecycle status the client applies to `define` when the caller omits one. */
|
|
6
|
+
export const DEFAULT_STATUS = "running";
|
|
7
|
+
/** Default page size for a `deleteExperiment` cascade pass before it self-reschedules. */
|
|
8
|
+
export const DEFAULT_DELETE_BATCH = 200;
|
|
9
|
+
/**
|
|
10
|
+
* FNV-1a 32-bit hash of `input`, returned as an unsigned integer. Deterministic,
|
|
11
|
+
* synchronous, and dependency-free — the component runs in a V8 isolate, so a
|
|
12
|
+
* pure string hash (rather than async `crypto.subtle`) is what keeps variant
|
|
13
|
+
* assignment a plain function callable inside a mutation.
|
|
14
|
+
*/
|
|
15
|
+
export function fnv1a(input) {
|
|
16
|
+
let hash = 0x811c9dc5;
|
|
17
|
+
for (let i = 0; i < input.length; i++) {
|
|
18
|
+
hash ^= input.charCodeAt(i);
|
|
19
|
+
hash = Math.imul(hash, 0x01000193);
|
|
20
|
+
}
|
|
21
|
+
return hash >>> 0;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Deterministically pick a variant for `subjectRef` from `variants`, weighted by
|
|
25
|
+
* each variant's `weight`. The same `(salt, subjectRef)` always maps to the same
|
|
26
|
+
* variant — sticky assignment with no storage — and the distribution matches the
|
|
27
|
+
* weights. The component folds `scope` into the salt so the same subject buckets
|
|
28
|
+
* independently per scope. Assumes a non-empty `variants` with positive weights;
|
|
29
|
+
* the caller validates that upstream.
|
|
30
|
+
*/
|
|
31
|
+
export function pickVariant(variants, salt, subjectRef) {
|
|
32
|
+
const lastVariant = variants[variants.length - 1];
|
|
33
|
+
if (lastVariant === undefined) {
|
|
34
|
+
throw new Error("pickVariant requires at least one variant");
|
|
35
|
+
}
|
|
36
|
+
let total = 0;
|
|
37
|
+
for (const variant of variants) {
|
|
38
|
+
total += variant.weight;
|
|
39
|
+
}
|
|
40
|
+
// Map the hash into [0, total): a point on the cumulative-weight line.
|
|
41
|
+
let target = (fnv1a(`${salt}:${subjectRef}`) / 0x100000000) * total;
|
|
42
|
+
// Walk every variant but the last, subtracting its bucket width.
|
|
43
|
+
for (const variant of variants.slice(0, -1)) {
|
|
44
|
+
target -= variant.weight;
|
|
45
|
+
if (target < 0) {
|
|
46
|
+
return variant.key;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
// The point fell in the final variant's bucket (also the single-variant case).
|
|
50
|
+
return lastVariant.key;
|
|
51
|
+
}
|
|
52
|
+
//# sourceMappingURL=shared.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"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