Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add A/B Test node #388

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/many-grapes-report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tokens-studio/graph-engine": minor
---

Add an A/B Test node that lets you set weights and seed. The node randomly chooses A or B as output. You can enable/disable if weights are used.
96 changes: 96 additions & 0 deletions packages/graph-engine/src/nodes/logic/abTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// src/nodes/logic/abTest.ts

import { AnySchema, BooleanSchema, NumberSchema, StringSchema } from "../../schemas/index.js";
import { INodeDefinition, Node } from "../../programmatic/node.js";
import { ToInput, ToOutput } from "../../programmatic";

export default class ABTestNode extends Node {
static title = "A/B Test";
static type = "studio.tokens.logic.abTest";
static description = "Randomly selects between two options for A/B testing in design systems";

declare inputs: ToInput<{
optionA: any;
optionB: any;
weightA: number;
seed: string;
useWeights: boolean;
}>;

declare outputs: ToOutput<{
result: any;
isOptionA: boolean;
}>;

constructor(props: INodeDefinition) {
super(props);

this.addInput("optionA", {
type: AnySchema,
});
this.addInput("optionB", {
type: AnySchema,
});
this.addInput("weightA", {
type: {
...NumberSchema,
default: 0.5,
minimum: 0,
maximum: 1,
},
});
this.addInput("seed", {
type: StringSchema,
});
this.addInput("useWeights", {
type: {
...BooleanSchema,
default: false,
},
});

this.addOutput("result", {
type: AnySchema,
});
this.addOutput("isOptionA", {
type: BooleanSchema,
});
}

execute(): void | Promise<void> {
const { optionA, optionB, weightA, seed, useWeights } = this.getAllInputs();

const isOptionA = this.selectOption(weightA, seed, useWeights);

this.setOutput("result", isOptionA ? optionA : optionB);
this.setOutput("isOptionA", isOptionA);
}

private selectOption(weightA: number, seed: string, useWeights: boolean): boolean {
let random: number;

if (seed) {
// Use a simple hash function to generate a number from the seed
const hash = this.hashCode(seed);
random = (hash % 1000) / 1000; // Normalize to 0-1 range
} else {
random = Math.random();
}

if (useWeights) {
return random < weightA;
} else {
return random < 0.5;
}
}

private hashCode(str: string): number {
let hash = 0;
for (let i = 0, len = str.length; i < len; i++) {
const chr = str.charCodeAt(i);
hash = ((hash << 5) - hash) + chr;
hash |= 0; // Convert to 32bit integer
}
return Math.abs(hash);
}
}
3 changes: 2 additions & 1 deletion packages/graph-engine/src/nodes/logic/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import abTest from "./abTest.js";
import and from "./and.js";
import compare from "./compare.js";
import ifNode from "./if.js";
import not from "./not.js";
import or from "./or.js";
import switchNode from "./switch.js";

export const nodes = [and, compare, ifNode, not, or, switchNode];
export const nodes = [abTest, and, compare, ifNode, not, or, switchNode];
Loading