splithub 0.1.9 → 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.
Files changed (75) hide show
  1. package/dist/chunk-3DUWIEHB.js +561 -0
  2. package/dist/chunk-3DUWIEHB.js.map +1 -0
  3. package/dist/chunk-3VACHXWE.js +319 -0
  4. package/dist/chunk-3VACHXWE.js.map +1 -0
  5. package/dist/chunk-3VY5CP3E.js +383 -0
  6. package/dist/chunk-3VY5CP3E.js.map +1 -0
  7. package/dist/chunk-74NI7BED.js +58 -0
  8. package/dist/chunk-74NI7BED.js.map +1 -0
  9. package/dist/chunk-A2RCSPG6.js +184 -0
  10. package/dist/chunk-A2RCSPG6.js.map +1 -0
  11. package/dist/chunk-AJO3C4BS.js +97 -0
  12. package/dist/chunk-AJO3C4BS.js.map +1 -0
  13. package/dist/chunk-C5S7RPYM.js +54 -0
  14. package/dist/chunk-C5S7RPYM.js.map +1 -0
  15. package/dist/chunk-EUF75FUC.js +160 -0
  16. package/dist/chunk-EUF75FUC.js.map +1 -0
  17. package/dist/chunk-EYYLDI5J.js +195 -0
  18. package/dist/chunk-EYYLDI5J.js.map +1 -0
  19. package/dist/chunk-GVARYUL3.js +28 -0
  20. package/dist/chunk-GVARYUL3.js.map +1 -0
  21. package/dist/chunk-HSH4ZI2V.js +72 -0
  22. package/dist/chunk-HSH4ZI2V.js.map +1 -0
  23. package/dist/chunk-PLXDG4B5.js +134 -0
  24. package/dist/chunk-PLXDG4B5.js.map +1 -0
  25. package/dist/chunk-PWSRKABE.js +113 -0
  26. package/dist/chunk-PWSRKABE.js.map +1 -0
  27. package/dist/chunk-QOXGIB3R.js +393 -0
  28. package/dist/chunk-QOXGIB3R.js.map +1 -0
  29. package/dist/chunk-UUTLLC54.js +158 -0
  30. package/dist/chunk-UUTLLC54.js.map +1 -0
  31. package/dist/chunk-V3DLUVOE.js +112 -0
  32. package/dist/chunk-V3DLUVOE.js.map +1 -0
  33. package/dist/chunk-YSXW2HN6.js +104 -0
  34. package/dist/chunk-YSXW2HN6.js.map +1 -0
  35. package/dist/ci-ZQZ6RBUJ.js +16 -0
  36. package/dist/ci-ZQZ6RBUJ.js.map +1 -0
  37. package/dist/create-K75OY6XK.js +18 -0
  38. package/dist/create-K75OY6XK.js.map +1 -0
  39. package/dist/dev-WQWFEA6I.js +11 -0
  40. package/dist/dev-WQWFEA6I.js.map +1 -0
  41. package/dist/doctor-MFYBZUKO.js +13 -0
  42. package/dist/doctor-MFYBZUKO.js.map +1 -0
  43. package/dist/holdout-PECSNGAD.js +44 -0
  44. package/dist/holdout-PECSNGAD.js.map +1 -0
  45. package/dist/index.js +84 -2239
  46. package/dist/index.js.map +1 -1
  47. package/dist/init-QAAM2GQ7.js +15 -0
  48. package/dist/init-QAAM2GQ7.js.map +1 -0
  49. package/dist/lifecycle-56HM7ITG.js +12 -0
  50. package/dist/lifecycle-56HM7ITG.js.map +1 -0
  51. package/dist/login-TTWSQ7XS.js +18 -0
  52. package/dist/login-TTWSQ7XS.js.map +1 -0
  53. package/dist/menu-UQX3K5QR.js +162 -0
  54. package/dist/menu-UQX3K5QR.js.map +1 -0
  55. package/dist/plan-BSFSMKTU.js +135 -0
  56. package/dist/plan-BSFSMKTU.js.map +1 -0
  57. package/dist/push-YVS25VVD.js +16 -0
  58. package/dist/push-YVS25VVD.js.map +1 -0
  59. package/dist/refs-ZDNH6NRK.js +44 -0
  60. package/dist/refs-ZDNH6NRK.js.map +1 -0
  61. package/dist/remove-KFQUK367.js +54 -0
  62. package/dist/remove-KFQUK367.js.map +1 -0
  63. package/dist/report-TF2BLJMC.js +64 -0
  64. package/dist/report-TF2BLJMC.js.map +1 -0
  65. package/dist/results-TLV5KJWO.js +17 -0
  66. package/dist/results-TLV5KJWO.js.map +1 -0
  67. package/dist/review-EIZDOGDC.js +34 -0
  68. package/dist/review-EIZDOGDC.js.map +1 -0
  69. package/dist/ship-4YX2WCBP.js +450 -0
  70. package/dist/ship-4YX2WCBP.js.map +1 -0
  71. package/dist/skills-HWQT3DTJ.js +13 -0
  72. package/dist/skills-HWQT3DTJ.js.map +1 -0
  73. package/dist/status-CWI7ZCYJ.js +11 -0
  74. package/dist/status-CWI7ZCYJ.js.map +1 -0
  75. package/package.json +22 -5
@@ -0,0 +1,393 @@
1
+ #!/usr/bin/env node
2
+ import {
3
+ deviceLogin
4
+ } from "./chunk-PWSRKABE.js";
5
+ import {
6
+ cloudTarget,
7
+ loadCredential
8
+ } from "./chunk-EUF75FUC.js";
9
+ import {
10
+ installSkills
11
+ } from "./chunk-AJO3C4BS.js";
12
+ import {
13
+ configTemplate,
14
+ nextMiddlewareTemplate,
15
+ nextRouteTemplate
16
+ } from "./chunk-3DUWIEHB.js";
17
+ import {
18
+ writeWorkflow
19
+ } from "./chunk-EYYLDI5J.js";
20
+ import {
21
+ detectProject,
22
+ installCommand
23
+ } from "./chunk-PLXDG4B5.js";
24
+ import {
25
+ c
26
+ } from "./chunk-V3DLUVOE.js";
27
+
28
+ // src/commands/init.ts
29
+ import * as p from "@clack/prompts";
30
+ import { execSync } from "child_process";
31
+ import { existsSync, mkdirSync, writeFileSync } from "fs";
32
+ import { dirname, join, relative } from "path";
33
+
34
+ // src/templates-store.ts
35
+ var DB_KINDS = ["sqlite", "postgres", "mysql", "custom"];
36
+ var PG_STORE = `import { countsFromRow, variantCountsSql, type ExperimentSummary, type Store, type StoredEvent, type StoreVariantCounts } from '@splithub/node';
37
+ import { Pool } from 'pg';
38
+
39
+ // SplitHub events in your Postgres. Run once (or add to your migrations):
40
+ export const SCHEMA_SQL = \`
41
+ CREATE TABLE IF NOT EXISTS splithub_events (
42
+ id text PRIMARY KEY,
43
+ type text NOT NULL, -- exposure | goal
44
+ experiment text,
45
+ variant text,
46
+ goal text,
47
+ visitor_id text NOT NULL,
48
+ session_id text NOT NULL,
49
+ ts bigint NOT NULL, -- client clock, unix ms
50
+ url text,
51
+ props jsonb,
52
+ received_at bigint NOT NULL, -- server clock, unix ms
53
+ host text,
54
+ country text,
55
+ device text,
56
+ browser text
57
+ );
58
+ CREATE INDEX IF NOT EXISTS splithub_events_exp ON splithub_events (type, experiment, visitor_id, ts);
59
+ CREATE INDEX IF NOT EXISTS splithub_events_goal ON splithub_events (type, goal, visitor_id, ts);
60
+ \`;
61
+
62
+ // The attribution rule shared by every SplitHub store (first exposure, 60 s clock skew, 50 goals per visitor and day).
63
+ const counts = variantCountsSql({ dialect: 'postgres', table: 'splithub_events' });
64
+
65
+ export function pgStore(pool: Pool = new Pool({ connectionString: process.env.DATABASE_URL })): Store {
66
+ return {
67
+ async insertEvents(events: StoredEvent[]) {
68
+ if (events.length === 0) return 0;
69
+ const cols = 15;
70
+ const values: unknown[] = [];
71
+ const rows = events.map((e, i) => {
72
+ values.push(e.id, e.type, e.experiment, e.variant, e.goal, e.visitorId, e.sessionId, e.ts, e.url, e.props ? JSON.stringify(e.props) : null, e.receivedAt, e.host ?? null, e.country ?? null, e.device ?? null, e.browser ?? null);
73
+ return '(' + Array.from({ length: cols }, (_, j) => '$' + (i * cols + j + 1)).join(', ') + ')';
74
+ });
75
+ const r = await pool.query(
76
+ 'INSERT INTO splithub_events (id, type, experiment, variant, goal, visitor_id, session_id, ts, url, props, received_at, host, country, device, browser) VALUES ' + rows.join(', ') + ' ON CONFLICT (id) DO NOTHING',
77
+ values
78
+ );
79
+ return r.rowCount ?? 0;
80
+ },
81
+ async variantCounts(experiment: string, goal: string): Promise<StoreVariantCounts[]> {
82
+ const r = await pool.query(counts.sql, counts.bind({ experiment, goal }));
83
+ return r.rows.map(countsFromRow);
84
+ },
85
+ async summaries(): Promise<ExperimentSummary[]> {
86
+ const r = await pool.query(
87
+ \`SELECT experiment, COUNT(*)::int AS exposures, COUNT(DISTINCT visitor_id)::int AS visitors, MIN(ts)::bigint AS first_ts, MAX(ts)::bigint AS last_ts
88
+ FROM splithub_events WHERE type = 'exposure' GROUP BY experiment\`
89
+ );
90
+ return r.rows.map((x) => ({ experiment: String(x.experiment), exposures: Number(x.exposures), visitors: Number(x.visitors), firstExposureAt: x.first_ts == null ? null : Number(x.first_ts), lastExposureAt: x.last_ts == null ? null : Number(x.last_ts) }));
91
+ },
92
+ close: () => pool.end(),
93
+ };
94
+ }
95
+ `;
96
+ var MYSQL_STORE = `import { countsFromRow, variantCountsSql, type ExperimentSummary, type Store, type StoredEvent, type StoreVariantCounts } from '@splithub/node';
97
+ import mysql, { type Pool } from 'mysql2/promise';
98
+
99
+ // SplitHub events in your MySQL / MariaDB / PlanetScale. Run once (or add to your migrations):
100
+ export const SCHEMA_SQL = \`
101
+ CREATE TABLE IF NOT EXISTS splithub_events (
102
+ id VARCHAR(64) PRIMARY KEY,
103
+ type VARCHAR(16) NOT NULL,
104
+ experiment VARCHAR(64) NULL,
105
+ variant VARCHAR(64) NULL,
106
+ goal VARCHAR(64) NULL,
107
+ visitor_id VARCHAR(64) NOT NULL,
108
+ session_id VARCHAR(64) NOT NULL,
109
+ ts BIGINT NOT NULL,
110
+ url VARCHAR(512) NULL,
111
+ props JSON NULL,
112
+ received_at BIGINT NOT NULL,
113
+ host VARCHAR(255) NULL,
114
+ country VARCHAR(8) NULL,
115
+ device VARCHAR(16) NULL,
116
+ browser VARCHAR(32) NULL,
117
+ INDEX splithub_events_exp (type, experiment, visitor_id, ts),
118
+ INDEX splithub_events_goal (type, goal, visitor_id, ts)
119
+ );
120
+ \`;
121
+
122
+ // The attribution rule shared by every SplitHub store (first exposure, 60 s clock skew, 50 goals per visitor and day).
123
+ const counts = variantCountsSql({ dialect: 'mysql', table: 'splithub_events' });
124
+
125
+ export function mysqlStore(pool: Pool = mysql.createPool(process.env.DATABASE_URL ?? '')): Store {
126
+ return {
127
+ async insertEvents(events: StoredEvent[]) {
128
+ if (events.length === 0) return 0;
129
+ const values = events.map((e) => [e.id, e.type, e.experiment, e.variant, e.goal, e.visitorId, e.sessionId, e.ts, e.url, e.props ? JSON.stringify(e.props) : null, e.receivedAt, e.host ?? null, e.country ?? null, e.device ?? null, e.browser ?? null]);
130
+ const [r] = await pool.query(
131
+ 'INSERT IGNORE INTO splithub_events (id, type, experiment, variant, goal, visitor_id, session_id, ts, url, props, received_at, host, country, device, browser) VALUES ?',
132
+ [values]
133
+ );
134
+ return (r as { affectedRows: number }).affectedRows;
135
+ },
136
+ async variantCounts(experiment: string, goal: string): Promise<StoreVariantCounts[]> {
137
+ const [rows] = await pool.query(counts.sql, counts.bind({ experiment, goal }));
138
+ return (rows as Array<Record<string, unknown>>).map(countsFromRow);
139
+ },
140
+ async summaries(): Promise<ExperimentSummary[]> {
141
+ const [rows] = await pool.query(
142
+ \`SELECT experiment, COUNT(*) AS exposures, COUNT(DISTINCT visitor_id) AS visitors, MIN(ts) AS first_ts, MAX(ts) AS last_ts
143
+ FROM splithub_events WHERE type = 'exposure' GROUP BY experiment\`
144
+ );
145
+ return (rows as Array<Record<string, unknown>>).map((x) => ({ experiment: String(x.experiment), exposures: Number(x.exposures), visitors: Number(x.visitors), firstExposureAt: x.first_ts == null ? null : Number(x.first_ts), lastExposureAt: x.last_ts == null ? null : Number(x.last_ts) }));
146
+ },
147
+ close: () => pool.end(),
148
+ };
149
+ }
150
+ `;
151
+ var CUSTOM_STORE = `import { attributeVariants, type ExperimentSummary, type Store, type StoredEvent, type StoreVariantCounts } from '@splithub/node';
152
+
153
+ /**
154
+ * Your own store (Prisma, Drizzle, Supabase, Mongo, anything). Three methods.
155
+ * Attribution rule (keep it, so results match the dashboard):
156
+ * a visitor counts once per experiment, in the variant of their earliest
157
+ * exposure (same ts: smallest variant key), and converts when they sent the
158
+ * goal at or after that exposure (60 s of clock skew allowed); only the first
159
+ * 50 events of one goal per visitor and UTC day count; count unique visitors.
160
+ * SQL database: run variantCountsSql({ dialect }) from @splithub/node.
161
+ * Anything else: load the experiment's exposures and the goal's events and
162
+ * return attributeVariants(events, experiment, goal).
163
+ * Columns worth keeping: id (unique, dedupe on it), type, experiment, variant,
164
+ * goal, visitor_id, session_id, ts, url, props (json), received_at, host,
165
+ * country, device, browser.
166
+ */
167
+ export function customStore(): Store {
168
+ return {
169
+ async insertEvents(events: StoredEvent[]): Promise<number> {
170
+ // Insert, skipping ids that already exist. Return how many were inserted.
171
+ throw new Error('implement insertEvents ' + events.length);
172
+ },
173
+ async variantCounts(experiment: string, goal: string): Promise<StoreVariantCounts[]> {
174
+ // Per variant: exposures, conversions, revenueSum and revenueSumSq (per-visitor revenue).
175
+ // Load the exposures of this experiment and the events of this goal, then apply the shared rule.
176
+ const events: StoredEvent[] = [];
177
+ if (events.length === 0) throw new Error('implement variantCounts ' + experiment + ' ' + goal);
178
+ return attributeVariants(events, experiment, goal);
179
+ },
180
+ async summaries(): Promise<ExperimentSummary[]> {
181
+ // Per experiment: exposures, unique visitors, first and last exposure ts (unix ms).
182
+ throw new Error('implement summaries');
183
+ },
184
+ };
185
+ }
186
+ `;
187
+ function storeTemplate(db) {
188
+ return db === "postgres" ? PG_STORE : db === "mysql" ? MYSQL_STORE : db === "custom" ? CUSTOM_STORE : null;
189
+ }
190
+ function storeUsage(db, storeImport) {
191
+ switch (db) {
192
+ case "postgres":
193
+ return { imports: `import { pgStore } from '${storeImport}';`, factory: "pgStore()" };
194
+ case "mysql":
195
+ return { imports: `import { mysqlStore } from '${storeImport}';`, factory: "mysqlStore()" };
196
+ case "custom":
197
+ return { imports: `import { customStore } from '${storeImport}';`, factory: "customStore()" };
198
+ default:
199
+ return { imports: `import { sqliteStore } from '@splithub/adapter-sqlite';`, factory: `sqliteStore({ file: process.env.SPLITHUB_DB ?? './splithub.db' })` };
200
+ }
201
+ }
202
+ function runtimePackages(db) {
203
+ return db === "postgres" ? ["pg"] : db === "mysql" ? ["mysql2"] : db === "sqlite" ? ["@splithub/adapter-sqlite"] : [];
204
+ }
205
+
206
+ // src/commands/init.ts
207
+ function str(v) {
208
+ return typeof v === "string" ? v : void 0;
209
+ }
210
+ function writeIfMissing(file, content, created, skipped) {
211
+ if (existsSync(file)) {
212
+ skipped.push(file);
213
+ return;
214
+ }
215
+ mkdirSync(dirname(file), { recursive: true });
216
+ writeFileSync(file, content, "utf8");
217
+ created.push(file);
218
+ }
219
+ function importPath(fromFile, toFile) {
220
+ let rel = relative(dirname(fromFile), toFile).replace(/\\/g, "/").replace(/\.(ts|mts|js|mjs)$/, "");
221
+ if (!rel.startsWith(".")) rel = `./${rel}`;
222
+ return rel;
223
+ }
224
+ var VITE_PLUGIN_HINT = `// vite.config.ts
225
+ import { splithub } from '@splithub/core/vite';
226
+
227
+ export default defineConfig({ plugins: [react(), splithub()] });`;
228
+ async function init(flags) {
229
+ const yes = Boolean(flags.yes);
230
+ const json = Boolean(flags.json);
231
+ const project = detectProject();
232
+ const created = [];
233
+ const skipped = [];
234
+ if (!json) p.intro(c.bold("splithub init"));
235
+ let mode = str(flags.mode);
236
+ if (!mode) {
237
+ if (yes) mode = "self";
238
+ else {
239
+ const picked = await p.select({
240
+ message: "Where should events be stored?",
241
+ options: [
242
+ { value: "self", label: "My own database (self-hosted)", hint: "free forever, nothing leaves your infra" },
243
+ { value: "cloud", label: "SplitHub Cloud", hint: "hosted dashboard, no server code" }
244
+ ]
245
+ });
246
+ if (p.isCancel(picked)) return cancel2();
247
+ mode = picked;
248
+ }
249
+ }
250
+ let db = DB_KINDS.includes(str(flags.db) ?? "") ? str(flags.db) : "sqlite";
251
+ if (mode === "self" && !flags.db && !yes) {
252
+ const picked = await p.select({
253
+ message: "Where should events be stored?",
254
+ options: [
255
+ { value: "sqlite", label: "SQLite file (zero setup)", hint: "./splithub.db via node:sqlite" },
256
+ { value: "postgres", label: "Postgres", hint: "pg + DATABASE_URL, table splithub_events" },
257
+ { value: "mysql", label: "MySQL / MariaDB / PlanetScale", hint: "mysql2 + DATABASE_URL" },
258
+ { value: "custom", label: "My own store (Prisma, Drizzle, Supabase, Mongo\u2026)", hint: "a stub with the three methods to fill in" }
259
+ ]
260
+ });
261
+ if (p.isCancel(picked)) return cancel2();
262
+ db = picked;
263
+ }
264
+ let key = str(flags.key);
265
+ let connected = null;
266
+ if (mode === "cloud") {
267
+ const api = cloudTarget(flags).url;
268
+ const saved = loadCredential(api, project.root);
269
+ if (saved) {
270
+ key ??= saved.publicKey ?? void 0;
271
+ connected = { projectName: saved.projectName, projectId: saved.projectId };
272
+ } else if (!process.env.SPLITHUB_API_KEY && !flags["no-login"]) {
273
+ if (!json) p.log.step("Connect to SplitHub Cloud");
274
+ try {
275
+ const cred = await deviceLogin({
276
+ api,
277
+ root: project.root,
278
+ json,
279
+ onCode: json ? (s) => console.log(JSON.stringify({ step: "approve", verifyUrl: s.verifyUrl, userCode: s.userCode, expiresIn: s.expiresIn, hint: "Show this link to the user and wait; init continues once approved." })) : (s) => p.note(`${c.cyan(s.verifyUrl)}
280
+
281
+ Code ${c.green(s.userCode)} \xB7 sign in, pick or create the project, approve.`, "Open this link")
282
+ });
283
+ key ??= cred.publicKey ?? void 0;
284
+ connected = { projectName: cred.projectName, projectId: cred.projectId };
285
+ if (!json) p.log.success(`connected to ${c.cyan(cred.projectName ?? cred.projectId ?? "project")}`);
286
+ } catch (error) {
287
+ if (!json) p.log.warn(`not connected: ${error instanceof Error ? error.message : String(error)}. Run ${c.cyan("npx splithub login")} later.`);
288
+ }
289
+ }
290
+ if (!key && process.env.SPLITHUB_API_KEY) {
291
+ try {
292
+ const res = await fetch(`${api}/config`, { headers: { authorization: `Bearer ${process.env.SPLITHUB_API_KEY}` } });
293
+ const body = await res.json().catch(() => ({}));
294
+ if (typeof body.publicKey === "string" && body.publicKey.startsWith("pk_")) {
295
+ key = body.publicKey;
296
+ connected ??= { projectName: null, projectId: null };
297
+ }
298
+ } catch {
299
+ }
300
+ }
301
+ if (!key && !yes) {
302
+ const k = await p.text({ message: "Cloud public key (pk_...), leave empty to set later via NEXT_PUBLIC_SPLITHUB_KEY", placeholder: "pk_" });
303
+ if (p.isCancel(k)) return cancel2();
304
+ key = k || void 0;
305
+ }
306
+ }
307
+ const configFile = project.configFile ?? join(project.root, "splithub.config.ts");
308
+ writeIfMissing(configFile, configTemplate(project.name, mode, key), created, skipped);
309
+ const storeBody = mode === "self" ? storeTemplate(db) : null;
310
+ const storeFile = storeBody ? join(project.root, "splithub", "store.ts") : null;
311
+ if (storeFile && storeBody) writeIfMissing(storeFile, storeBody, created, skipped);
312
+ if (project.framework === "next") {
313
+ const mwFile = join(project.root, project.hasSrc ? "src/middleware.ts" : "middleware.ts");
314
+ writeIfMissing(mwFile, nextMiddlewareTemplate(importPath(mwFile, configFile)), created, skipped);
315
+ if (mode === "self" && project.appDir) {
316
+ const routeFile = join(project.root, project.appDir, "api/splithub/[[...path]]/route.ts");
317
+ writeIfMissing(routeFile, nextRouteTemplate(importPath(routeFile, configFile), storeUsage(db, storeFile ? importPath(routeFile, storeFile) : "")), created, skipped);
318
+ }
319
+ }
320
+ let mcp = !flags["no-mcp"];
321
+ if (!yes && !json && mcp) {
322
+ const want = await p.confirm({ message: "Register the SplitHub MCP server for Claude Code / Cursor (.mcp.json, .cursor/mcp.json)?", initialValue: true });
323
+ if (p.isCancel(want)) return cancel2();
324
+ mcp = want;
325
+ }
326
+ const skillsResult = installSkills(project.root, { mcp });
327
+ created.push(...skillsResult.written, ...skillsResult.mcp.map((f) => `${f} (MCP server)`));
328
+ skipped.push(...skillsResult.skipped);
329
+ if (flags["github-action"]) {
330
+ const wf = writeWorkflow(project.root, Boolean(flags.force));
331
+ (wf.written ? created : skipped).push(join(project.root, wf.file));
332
+ }
333
+ const runtime = ["@splithub/react", "@splithub/core"];
334
+ if (project.framework === "next") runtime.push("@splithub/next");
335
+ if (mode === "self") runtime.push("@splithub/node", ...runtimePackages(db));
336
+ const installRuntime = installCommand(project.packageManager, runtime);
337
+ const installDev = installCommand(project.packageManager, ["splithub"], true);
338
+ let installed = false;
339
+ if (!flags["no-install"]) {
340
+ const go = yes ? true : await p.confirm({ message: `Install packages now? (${installRuntime})` });
341
+ if (p.isCancel(go)) return cancel2();
342
+ if (go) {
343
+ const s = json ? null : p.spinner();
344
+ s?.start("Installing");
345
+ try {
346
+ execSync(installRuntime, { cwd: project.root, stdio: json ? "ignore" : "inherit" });
347
+ execSync(installDev, { cwd: project.root, stdio: json ? "ignore" : "inherit" });
348
+ installed = true;
349
+ s?.stop("Installed");
350
+ } catch (error) {
351
+ s?.stop("Install failed; run it manually");
352
+ if (!json) p.log.error(error instanceof Error ? error.message : String(error));
353
+ }
354
+ }
355
+ }
356
+ const providerHint = project.framework === "next" ? `// ${project.appDir ?? "app"}/layout.tsx
357
+ import { SplitHubNextProvider } from '@splithub/next/client';
358
+ import { getSplitHub } from '@splithub/next/server';
359
+ import config from '${importPath(join(project.root, project.appDir ?? "app", "layout.tsx"), configFile)}';
360
+
361
+ export default async function RootLayout({ children }) {
362
+ const sh = await getSplitHub(config);
363
+ return <html><body><SplitHubNextProvider config={config} visitorId={sh.visitorId ?? undefined} remoteOverrides={sh.overrides}>{children}</SplitHubNextProvider></body></html>;
364
+ }` : `// main.tsx
365
+ import { SplitHubProvider } from '@splithub/react';
366
+ import config from './splithub.config';
367
+
368
+ <SplitHubProvider config={config}><App /></SplitHubProvider>`;
369
+ const warning = mode === "cloud" && !key ? "No public key yet: storage.key falls back to process.env.NEXT_PUBLIC_SPLITHUB_KEY. It is written into the config by the first `npx splithub push`, by `npx splithub login` (saved with the credentials), or pass --key pk_\u2026 (dashboard \u2192 Setup); SPLITHUB_API_KEY in the environment also resolves it." : null;
370
+ const summary = { project: project.name, framework: project.framework, mode, db: mode === "self" ? db : null, connected, publicKey: key ?? null, warning, created, skipped, installed, installCommands: installed ? [] : [installRuntime, installDev], nextStep: providerHint, ...project.framework === "vite" ? { vitePlugin: VITE_PLUGIN_HINT } : {} };
371
+ if (json) {
372
+ console.log(JSON.stringify(summary));
373
+ return;
374
+ }
375
+ if (warning) p.log.warn(warning);
376
+ if (mode === "self" && db !== "sqlite") p.note(db === "custom" ? "Fill in the three methods in splithub/store.ts (the comments say what each must return)." : `Set DATABASE_URL and run the SCHEMA_SQL from splithub/store.ts once (or add it to your migrations).`, `Database: ${db}`);
377
+ if (created.length) p.note(created.map((f) => relative(project.root, f) || f).join("\n"), "Created");
378
+ if (skipped.length) p.note(skipped.map((f) => relative(project.root, f)).join("\n"), "Already present, left untouched");
379
+ if (!installed) p.note(`${installRuntime}
380
+ ${installDev}`, "Install");
381
+ p.note(providerHint, "Wrap your app once");
382
+ if (project.framework === "vite") p.note(VITE_PLUGIN_HINT, "Notes in your dev terminal");
383
+ p.outro(`Next: ${c.cyan("npx splithub create")} to add your first experiment, ${c.cyan("npx splithub push")} to sync it, ${c.cyan("npx splithub doctor")} to verify.`);
384
+ }
385
+ function cancel2() {
386
+ p.cancel("Cancelled.");
387
+ process.exit(0);
388
+ }
389
+
390
+ export {
391
+ init
392
+ };
393
+ //# sourceMappingURL=chunk-QOXGIB3R.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/commands/init.ts","../src/templates-store.ts"],"sourcesContent":["import * as p from '@clack/prompts';\nimport { execSync } from 'node:child_process';\nimport { existsSync, mkdirSync, writeFileSync } from 'node:fs';\nimport { dirname, join, relative } from 'node:path';\nimport { cloudTarget } from '../api';\nimport { loadCredential } from '../credentials';\nimport { detectProject, installCommand } from '../project';\nimport { deviceLogin } from './login';\nimport { configTemplate, nextMiddlewareTemplate, nextRouteTemplate } from '../templates';\nimport { DB_KINDS, runtimePackages, storeTemplate, storeUsage, type DbKind } from '../templates-store';\nimport { installSkills } from './skills';\nimport { writeWorkflow } from './ci';\nimport { c } from '../ui';\n\nexport type Flags = Record<string, string | boolean | string[] | undefined>;\n\nfunction str(v: unknown): string | undefined {\n return typeof v === 'string' ? v : undefined;\n}\n\nfunction writeIfMissing(file: string, content: string, created: string[], skipped: string[]): void {\n if (existsSync(file)) {\n skipped.push(file);\n return;\n }\n mkdirSync(dirname(file), { recursive: true });\n writeFileSync(file, content, 'utf8');\n created.push(file);\n}\n\nfunction importPath(fromFile: string, toFile: string): string {\n let rel = relative(dirname(fromFile), toFile).replace(/\\\\/g, '/').replace(/\\.(ts|mts|js|mjs)$/, '');\n if (!rel.startsWith('.')) rel = `./${rel}`;\n return rel;\n}\n\n// Vite apps run the SDK in the browser; this plugin prints what each experiment does in the dev server's terminal.\nconst VITE_PLUGIN_HINT = `// vite.config.ts\nimport { splithub } from '@splithub/core/vite';\n\nexport default defineConfig({ plugins: [react(), splithub()] });`;\n\nexport async function init(flags: Flags): Promise<void> {\n const yes = Boolean(flags.yes);\n const json = Boolean(flags.json);\n const project = detectProject();\n const created: string[] = [];\n const skipped: string[] = [];\n\n if (!json) p.intro(c.bold('splithub init'));\n\n let mode = str(flags.mode) as 'self' | 'cloud' | undefined;\n if (!mode) {\n if (yes) mode = 'self';\n else {\n const picked = await p.select({\n message: 'Where should events be stored?',\n options: [\n { value: 'self', label: 'My own database (self-hosted)', hint: 'free forever, nothing leaves your infra' },\n { value: 'cloud', label: 'SplitHub Cloud', hint: 'hosted dashboard, no server code' },\n ],\n });\n if (p.isCancel(picked)) return cancel();\n mode = picked as 'self' | 'cloud';\n }\n }\n\n // Cloud: connect this directory to a project. A saved login or SPLITHUB_API_KEY\n // is enough; otherwise run the device flow — print a link, the human signs in\n // and picks (or creates) the project, the CLI gets the keys. Works when an\n // agent runs init: it relays the link, the user clicks it.\n // Self mode: which database keeps the events. sqlite needs nothing; postgres / mysql\n // get a ready Store in splithub/store.ts; custom gets a documented stub.\n let db: DbKind = (DB_KINDS as string[]).includes(str(flags.db) ?? '') ? (str(flags.db) as DbKind) : 'sqlite';\n if (mode === 'self' && !flags.db && !yes) {\n const picked = await p.select({\n message: 'Where should events be stored?',\n options: [\n { value: 'sqlite', label: 'SQLite file (zero setup)', hint: './splithub.db via node:sqlite' },\n { value: 'postgres', label: 'Postgres', hint: 'pg + DATABASE_URL, table splithub_events' },\n { value: 'mysql', label: 'MySQL / MariaDB / PlanetScale', hint: 'mysql2 + DATABASE_URL' },\n { value: 'custom', label: 'My own store (Prisma, Drizzle, Supabase, Mongo…)', hint: 'a stub with the three methods to fill in' },\n ],\n });\n if (p.isCancel(picked)) return cancel();\n db = picked as DbKind;\n }\n\n let key = str(flags.key);\n let connected: { projectName: string | null; projectId: string | null } | null = null;\n if (mode === 'cloud') {\n const api = cloudTarget(flags).url;\n const saved = loadCredential(api, project.root);\n if (saved) {\n key ??= saved.publicKey ?? undefined;\n connected = { projectName: saved.projectName, projectId: saved.projectId };\n } else if (!process.env.SPLITHUB_API_KEY && !flags['no-login']) {\n if (!json) p.log.step('Connect to SplitHub Cloud');\n try {\n const cred = await deviceLogin({\n api,\n root: project.root,\n json,\n onCode: json\n ? (s) => console.log(JSON.stringify({ step: 'approve', verifyUrl: s.verifyUrl, userCode: s.userCode, expiresIn: s.expiresIn, hint: 'Show this link to the user and wait; init continues once approved.' }))\n : (s) => p.note(`${c.cyan(s.verifyUrl)}\\n\\nCode ${c.green(s.userCode)} · sign in, pick or create the project, approve.`, 'Open this link'),\n });\n key ??= cred.publicKey ?? undefined;\n connected = { projectName: cred.projectName, projectId: cred.projectId };\n if (!json) p.log.success(`connected to ${c.cyan(cred.projectName ?? cred.projectId ?? 'project')}`);\n } catch (error) {\n if (!json) p.log.warn(`not connected: ${error instanceof Error ? error.message : String(error)}. Run ${c.cyan('npx splithub login')} later.`);\n }\n }\n // An API key in the environment (CI, agents) identifies the project: ask the cloud for its public key.\n if (!key && process.env.SPLITHUB_API_KEY) {\n try {\n const res = await fetch(`${api}/config`, { headers: { authorization: `Bearer ${process.env.SPLITHUB_API_KEY}` } });\n const body = (await res.json().catch(() => ({}))) as { publicKey?: string };\n if (typeof body.publicKey === 'string' && body.publicKey.startsWith('pk_')) {\n key = body.publicKey;\n connected ??= { projectName: null, projectId: null };\n }\n } catch {\n // offline: the first push writes the key into the config anyway\n }\n }\n if (!key && !yes) {\n const k = await p.text({ message: 'Cloud public key (pk_...), leave empty to set later via NEXT_PUBLIC_SPLITHUB_KEY', placeholder: 'pk_' });\n if (p.isCancel(k)) return cancel();\n key = k || undefined;\n }\n }\n\n // 1. config\n const configFile = project.configFile ?? join(project.root, 'splithub.config.ts');\n writeIfMissing(configFile, configTemplate(project.name, mode, key), created, skipped);\n\n // 2a. store for self mode (postgres / mysql / custom); sqlite needs no file.\n const storeBody = mode === 'self' ? storeTemplate(db) : null;\n const storeFile = storeBody ? join(project.root, 'splithub', 'store.ts') : null;\n if (storeFile && storeBody) writeIfMissing(storeFile, storeBody, created, skipped);\n\n // 2. framework files\n if (project.framework === 'next') {\n const mwFile = join(project.root, project.hasSrc ? 'src/middleware.ts' : 'middleware.ts');\n writeIfMissing(mwFile, nextMiddlewareTemplate(importPath(mwFile, configFile)), created, skipped);\n if (mode === 'self' && project.appDir) {\n const routeFile = join(project.root, project.appDir, 'api/splithub/[[...path]]/route.ts');\n writeIfMissing(routeFile, nextRouteTemplate(importPath(routeFile, configFile), storeUsage(db, storeFile ? importPath(routeFile, storeFile) : '')), created, skipped);\n }\n }\n\n // 3. agent skills (+ MCP server registration): the agent learns the library, how to test\n // well, the API and the MCP tools from .claude/skills/*; Cursor gets the rule file.\n let mcp = !flags['no-mcp'];\n if (!yes && !json && mcp) {\n const want = await p.confirm({ message: 'Register the SplitHub MCP server for Claude Code / Cursor (.mcp.json, .cursor/mcp.json)?', initialValue: true });\n if (p.isCancel(want)) return cancel();\n mcp = want;\n }\n const skillsResult = installSkills(project.root, { mcp });\n created.push(...skillsResult.written, ...skillsResult.mcp.map((f) => `${f} (MCP server)`));\n skipped.push(...skillsResult.skipped);\n\n // 3b. GitHub workflow: checks on PRs, results comment on a schedule.\n if (flags['github-action']) {\n const wf = writeWorkflow(project.root, Boolean(flags.force));\n (wf.written ? created : skipped).push(join(project.root, wf.file));\n }\n\n // 4. dependencies\n const runtime = ['@splithub/react', '@splithub/core'];\n if (project.framework === 'next') runtime.push('@splithub/next');\n if (mode === 'self') runtime.push('@splithub/node', ...runtimePackages(db));\n const installRuntime = installCommand(project.packageManager, runtime);\n const installDev = installCommand(project.packageManager, ['splithub'], true);\n let installed = false;\n if (!flags['no-install']) {\n const go = yes ? true : await p.confirm({ message: `Install packages now? (${installRuntime})` });\n if (p.isCancel(go)) return cancel();\n if (go) {\n const s = json ? null : p.spinner();\n s?.start('Installing');\n try {\n execSync(installRuntime, { cwd: project.root, stdio: json ? 'ignore' : 'inherit' });\n execSync(installDev, { cwd: project.root, stdio: json ? 'ignore' : 'inherit' });\n installed = true;\n s?.stop('Installed');\n } catch (error) {\n s?.stop('Install failed; run it manually');\n if (!json) p.log.error(error instanceof Error ? error.message : String(error));\n }\n }\n }\n\n const providerHint =\n project.framework === 'next'\n ? `// ${project.appDir ?? 'app'}/layout.tsx\\nimport { SplitHubNextProvider } from '@splithub/next/client';\\nimport { getSplitHub } from '@splithub/next/server';\\nimport config from '${importPath(join(project.root, project.appDir ?? 'app', 'layout.tsx'), configFile)}';\\n\\nexport default async function RootLayout({ children }) {\\n const sh = await getSplitHub(config);\\n return <html><body><SplitHubNextProvider config={config} visitorId={sh.visitorId ?? undefined} remoteOverrides={sh.overrides}>{children}</SplitHubNextProvider></body></html>;\\n}`\n : `// main.tsx\\nimport { SplitHubProvider } from '@splithub/react';\\nimport config from './splithub.config';\\n\\n<SplitHubProvider config={config}><App /></SplitHubProvider>`;\n\n const warning = mode === 'cloud' && !key ? 'No public key yet: storage.key falls back to process.env.NEXT_PUBLIC_SPLITHUB_KEY. It is written into the config by the first `npx splithub push`, by `npx splithub login` (saved with the credentials), or pass --key pk_… (dashboard → Setup); SPLITHUB_API_KEY in the environment also resolves it.' : null;\n const summary = { project: project.name, framework: project.framework, mode, db: mode === 'self' ? db : null, connected, publicKey: key ?? null, warning, created, skipped, installed, installCommands: installed ? [] : [installRuntime, installDev], nextStep: providerHint, ...(project.framework === 'vite' ? { vitePlugin: VITE_PLUGIN_HINT } : {}) };\n\n if (json) {\n console.log(JSON.stringify(summary));\n return;\n }\n if (warning) p.log.warn(warning);\n if (mode === 'self' && db !== 'sqlite') p.note(db === 'custom' ? 'Fill in the three methods in splithub/store.ts (the comments say what each must return).' : `Set DATABASE_URL and run the SCHEMA_SQL from splithub/store.ts once (or add it to your migrations).`, `Database: ${db}`);\n if (created.length) p.note(created.map((f) => relative(project.root, f) || f).join('\\n'), 'Created');\n if (skipped.length) p.note(skipped.map((f) => relative(project.root, f)).join('\\n'), 'Already present, left untouched');\n if (!installed) p.note(`${installRuntime}\\n${installDev}`, 'Install');\n p.note(providerHint, 'Wrap your app once');\n if (project.framework === 'vite') p.note(VITE_PLUGIN_HINT, 'Notes in your dev terminal');\n p.outro(`Next: ${c.cyan('npx splithub create')} to add your first experiment, ${c.cyan('npx splithub push')} to sync it, ${c.cyan('npx splithub doctor')} to verify.`);\n}\n\n\nfunction cancel(): void {\n p.cancel('Cancelled.');\n process.exit(0);\n}\n","// Store templates for self-hosted mode: `splithub init --mode self --db postgres|mysql|custom`\n// writes splithub/store.ts from one of these. Each implements the Store interface\n// from @splithub/node (insertEvents, variantCounts, summaries) with the same\n// attribution rule as the cloud, through variantCountsSql from @splithub/node.\n\nexport type DbKind = 'sqlite' | 'postgres' | 'mysql' | 'custom';\nexport const DB_KINDS: DbKind[] = ['sqlite', 'postgres', 'mysql', 'custom'];\n\nexport const PG_STORE = `import { countsFromRow, variantCountsSql, type ExperimentSummary, type Store, type StoredEvent, type StoreVariantCounts } from '@splithub/node';\nimport { Pool } from 'pg';\n\n// SplitHub events in your Postgres. Run once (or add to your migrations):\nexport const SCHEMA_SQL = \\`\nCREATE TABLE IF NOT EXISTS splithub_events (\n id text PRIMARY KEY,\n type text NOT NULL, -- exposure | goal\n experiment text,\n variant text,\n goal text,\n visitor_id text NOT NULL,\n session_id text NOT NULL,\n ts bigint NOT NULL, -- client clock, unix ms\n url text,\n props jsonb,\n received_at bigint NOT NULL, -- server clock, unix ms\n host text,\n country text,\n device text,\n browser text\n);\nCREATE INDEX IF NOT EXISTS splithub_events_exp ON splithub_events (type, experiment, visitor_id, ts);\nCREATE INDEX IF NOT EXISTS splithub_events_goal ON splithub_events (type, goal, visitor_id, ts);\n\\`;\n\n// The attribution rule shared by every SplitHub store (first exposure, 60 s clock skew, 50 goals per visitor and day).\nconst counts = variantCountsSql({ dialect: 'postgres', table: 'splithub_events' });\n\nexport function pgStore(pool: Pool = new Pool({ connectionString: process.env.DATABASE_URL })): Store {\n return {\n async insertEvents(events: StoredEvent[]) {\n if (events.length === 0) return 0;\n const cols = 15;\n const values: unknown[] = [];\n const rows = events.map((e, i) => {\n values.push(e.id, e.type, e.experiment, e.variant, e.goal, e.visitorId, e.sessionId, e.ts, e.url, e.props ? JSON.stringify(e.props) : null, e.receivedAt, e.host ?? null, e.country ?? null, e.device ?? null, e.browser ?? null);\n return '(' + Array.from({ length: cols }, (_, j) => '$' + (i * cols + j + 1)).join(', ') + ')';\n });\n const r = await pool.query(\n 'INSERT INTO splithub_events (id, type, experiment, variant, goal, visitor_id, session_id, ts, url, props, received_at, host, country, device, browser) VALUES ' + rows.join(', ') + ' ON CONFLICT (id) DO NOTHING',\n values\n );\n return r.rowCount ?? 0;\n },\n async variantCounts(experiment: string, goal: string): Promise<StoreVariantCounts[]> {\n const r = await pool.query(counts.sql, counts.bind({ experiment, goal }));\n return r.rows.map(countsFromRow);\n },\n async summaries(): Promise<ExperimentSummary[]> {\n const r = await pool.query(\n \\`SELECT experiment, COUNT(*)::int AS exposures, COUNT(DISTINCT visitor_id)::int AS visitors, MIN(ts)::bigint AS first_ts, MAX(ts)::bigint AS last_ts\n FROM splithub_events WHERE type = 'exposure' GROUP BY experiment\\`\n );\n return r.rows.map((x) => ({ experiment: String(x.experiment), exposures: Number(x.exposures), visitors: Number(x.visitors), firstExposureAt: x.first_ts == null ? null : Number(x.first_ts), lastExposureAt: x.last_ts == null ? null : Number(x.last_ts) }));\n },\n close: () => pool.end(),\n };\n}\n`;\n\nexport const MYSQL_STORE = `import { countsFromRow, variantCountsSql, type ExperimentSummary, type Store, type StoredEvent, type StoreVariantCounts } from '@splithub/node';\nimport mysql, { type Pool } from 'mysql2/promise';\n\n// SplitHub events in your MySQL / MariaDB / PlanetScale. Run once (or add to your migrations):\nexport const SCHEMA_SQL = \\`\nCREATE TABLE IF NOT EXISTS splithub_events (\n id VARCHAR(64) PRIMARY KEY,\n type VARCHAR(16) NOT NULL,\n experiment VARCHAR(64) NULL,\n variant VARCHAR(64) NULL,\n goal VARCHAR(64) NULL,\n visitor_id VARCHAR(64) NOT NULL,\n session_id VARCHAR(64) NOT NULL,\n ts BIGINT NOT NULL,\n url VARCHAR(512) NULL,\n props JSON NULL,\n received_at BIGINT NOT NULL,\n host VARCHAR(255) NULL,\n country VARCHAR(8) NULL,\n device VARCHAR(16) NULL,\n browser VARCHAR(32) NULL,\n INDEX splithub_events_exp (type, experiment, visitor_id, ts),\n INDEX splithub_events_goal (type, goal, visitor_id, ts)\n);\n\\`;\n\n// The attribution rule shared by every SplitHub store (first exposure, 60 s clock skew, 50 goals per visitor and day).\nconst counts = variantCountsSql({ dialect: 'mysql', table: 'splithub_events' });\n\nexport function mysqlStore(pool: Pool = mysql.createPool(process.env.DATABASE_URL ?? '')): Store {\n return {\n async insertEvents(events: StoredEvent[]) {\n if (events.length === 0) return 0;\n const values = events.map((e) => [e.id, e.type, e.experiment, e.variant, e.goal, e.visitorId, e.sessionId, e.ts, e.url, e.props ? JSON.stringify(e.props) : null, e.receivedAt, e.host ?? null, e.country ?? null, e.device ?? null, e.browser ?? null]);\n const [r] = await pool.query(\n 'INSERT IGNORE INTO splithub_events (id, type, experiment, variant, goal, visitor_id, session_id, ts, url, props, received_at, host, country, device, browser) VALUES ?',\n [values]\n );\n return (r as { affectedRows: number }).affectedRows;\n },\n async variantCounts(experiment: string, goal: string): Promise<StoreVariantCounts[]> {\n const [rows] = await pool.query(counts.sql, counts.bind({ experiment, goal }));\n return (rows as Array<Record<string, unknown>>).map(countsFromRow);\n },\n async summaries(): Promise<ExperimentSummary[]> {\n const [rows] = await pool.query(\n \\`SELECT experiment, COUNT(*) AS exposures, COUNT(DISTINCT visitor_id) AS visitors, MIN(ts) AS first_ts, MAX(ts) AS last_ts\n FROM splithub_events WHERE type = 'exposure' GROUP BY experiment\\`\n );\n return (rows as Array<Record<string, unknown>>).map((x) => ({ experiment: String(x.experiment), exposures: Number(x.exposures), visitors: Number(x.visitors), firstExposureAt: x.first_ts == null ? null : Number(x.first_ts), lastExposureAt: x.last_ts == null ? null : Number(x.last_ts) }));\n },\n close: () => pool.end(),\n };\n}\n`;\n\nexport const CUSTOM_STORE = `import { attributeVariants, type ExperimentSummary, type Store, type StoredEvent, type StoreVariantCounts } from '@splithub/node';\n\n/**\n * Your own store (Prisma, Drizzle, Supabase, Mongo, anything). Three methods.\n * Attribution rule (keep it, so results match the dashboard):\n * a visitor counts once per experiment, in the variant of their earliest\n * exposure (same ts: smallest variant key), and converts when they sent the\n * goal at or after that exposure (60 s of clock skew allowed); only the first\n * 50 events of one goal per visitor and UTC day count; count unique visitors.\n * SQL database: run variantCountsSql({ dialect }) from @splithub/node.\n * Anything else: load the experiment's exposures and the goal's events and\n * return attributeVariants(events, experiment, goal).\n * Columns worth keeping: id (unique, dedupe on it), type, experiment, variant,\n * goal, visitor_id, session_id, ts, url, props (json), received_at, host,\n * country, device, browser.\n */\nexport function customStore(): Store {\n return {\n async insertEvents(events: StoredEvent[]): Promise<number> {\n // Insert, skipping ids that already exist. Return how many were inserted.\n throw new Error('implement insertEvents ' + events.length);\n },\n async variantCounts(experiment: string, goal: string): Promise<StoreVariantCounts[]> {\n // Per variant: exposures, conversions, revenueSum and revenueSumSq (per-visitor revenue).\n // Load the exposures of this experiment and the events of this goal, then apply the shared rule.\n const events: StoredEvent[] = [];\n if (events.length === 0) throw new Error('implement variantCounts ' + experiment + ' ' + goal);\n return attributeVariants(events, experiment, goal);\n },\n async summaries(): Promise<ExperimentSummary[]> {\n // Per experiment: exposures, unique visitors, first and last exposure ts (unix ms).\n throw new Error('implement summaries');\n },\n };\n}\n`;\n\nexport function storeTemplate(db: DbKind): string | null {\n return db === 'postgres' ? PG_STORE : db === 'mysql' ? MYSQL_STORE : db === 'custom' ? CUSTOM_STORE : null;\n}\n\n/** Import line + factory call used by the route template. */\nexport function storeUsage(db: DbKind, storeImport: string): { imports: string; factory: string } {\n switch (db) {\n case 'postgres':\n return { imports: `import { pgStore } from '${storeImport}';`, factory: 'pgStore()' };\n case 'mysql':\n return { imports: `import { mysqlStore } from '${storeImport}';`, factory: 'mysqlStore()' };\n case 'custom':\n return { imports: `import { customStore } from '${storeImport}';`, factory: 'customStore()' };\n default:\n return { imports: `import { sqliteStore } from '@splithub/adapter-sqlite';`, factory: `sqliteStore({ file: process.env.SPLITHUB_DB ?? './splithub.db' })` };\n }\n}\n\nexport function runtimePackages(db: DbKind): string[] {\n return db === 'postgres' ? ['pg'] : db === 'mysql' ? ['mysql2'] : db === 'sqlite' ? ['@splithub/adapter-sqlite'] : [];\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,YAAY,OAAO;AACnB,SAAS,gBAAgB;AACzB,SAAS,YAAY,WAAW,qBAAqB;AACrD,SAAS,SAAS,MAAM,gBAAgB;;;ACGjC,IAAM,WAAqB,CAAC,UAAU,YAAY,SAAS,QAAQ;AAEnE,IAAM,WAAW;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AA6DjB,IAAM,cAAc;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;A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AAK,OAAO,WAAW,6FAA6F,uGAAuG,aAAa,EAAE,EAAE;AACtR,MAAI,QAAQ,OAAQ,CAAE,OAAK,QAAQ,IAAI,CAAC,MAAM,SAAS,QAAQ,MAAM,CAAC,KAAK,CAAC,EAAE,KAAK,IAAI,GAAG,SAAS;AACnG,MAAI,QAAQ,OAAQ,CAAE,OAAK,QAAQ,IAAI,CAAC,MAAM,SAAS,QAAQ,MAAM,CAAC,CAAC,EAAE,KAAK,IAAI,GAAG,iCAAiC;AACtH,MAAI,CAAC,UAAW,CAAE,OAAK,GAAG,cAAc;AAAA,EAAK,UAAU,IAAI,SAAS;AACpE,EAAE,OAAK,cAAc,oBAAoB;AACzC,MAAI,QAAQ,cAAc,OAAQ,CAAE,OAAK,kBAAkB,4BAA4B;AACvF,EAAE,QAAM,SAAS,EAAE,KAAK,qBAAqB,CAAC,kCAAkC,EAAE,KAAK,mBAAmB,CAAC,gBAAgB,EAAE,KAAK,qBAAqB,CAAC,aAAa;AACvK;AAGA,SAASA,UAAe;AACtB,EAAE,SAAO,YAAY;AACrB,UAAQ,KAAK,CAAC;AAChB;","names":["cancel"]}
@@ -0,0 +1,158 @@
1
+ #!/usr/bin/env node
2
+ import {
3
+ fetchResults,
4
+ resolveTarget
5
+ } from "./chunk-EUF75FUC.js";
6
+ import {
7
+ detectProject
8
+ } from "./chunk-PLXDG4B5.js";
9
+ import {
10
+ bar,
11
+ c,
12
+ fail,
13
+ pct,
14
+ printJson,
15
+ statusColor,
16
+ table,
17
+ verdictColor,
18
+ verdictIcon
19
+ } from "./chunk-V3DLUVOE.js";
20
+
21
+ // src/commands/results.ts
22
+ async function results(key, flags) {
23
+ if (!key) fail("USAGE", "Which experiment? npx splithub results <id>");
24
+ const target = resolveTarget(flags, detectProject());
25
+ const r = await fetchResults(target, key);
26
+ if (flags.json) {
27
+ printJson({ ...r, daysToVerdict: r.daysToVerdict ?? null, ...r.status === "finished" ? { next: FINISHED_NEXT } : {} });
28
+ return;
29
+ }
30
+ const v = r.verdict;
31
+ const title = r.experiment === HOLDOUT ? "Holdout: long-term impact" : r.experiment;
32
+ console.log(`${c.bold(title)} ${c.dim(`\xB7 ${r.kind} \xB7 `)}${statusColor(r.status)} ${c.dim(`\xB7 goal: ${r.goal ?? "none"}`)}`);
33
+ if (r.description) console.log(c.dim(r.description));
34
+ console.log();
35
+ console.log(verdictColor(v.status, `${label(v.status)} ${v.message}`));
36
+ const eta = daysLeft(r.daysToVerdict);
37
+ if (eta && r.status === "running") console.log(c.dim(eta));
38
+ for (const note of v.notes ?? []) console.log(c.dim(`\xB7 ${note}`));
39
+ console.log();
40
+ const rates = /* @__PURE__ */ new Map();
41
+ for (const cnt of v.sample.variants) rates.set(cnt.variant, cnt.exposures > 0 ? cnt.conversions / cnt.exposures : 0);
42
+ console.log(
43
+ table(
44
+ ["VARIANT", "VISITORS", "CONV", "RATE", "LIFT", "P(BEAT)", "P-VALUE", "95% CI"],
45
+ // Always-valid (sequential) p-value and interval: the ones the verdict uses, safe to read any day.
46
+ v.sample.variants.map((cnt, i) => {
47
+ const cmp = v.comparisons.find((x) => x.variant === cnt.variant);
48
+ const name = i === 0 ? `${cnt.variant} ${c.dim("(control)")}` : cnt.variant;
49
+ const marked = v.winner === cnt.variant ? c.green(`\u2605 ${name}`) : name;
50
+ return [
51
+ marked,
52
+ String(cnt.exposures),
53
+ String(cnt.conversions),
54
+ pct(rates.get(cnt.variant)),
55
+ cmp ? pct(cmp.relativeLift, true) : "\u2013",
56
+ cmp ? pct(cmp.probabilityToBeat) : "\u2013",
57
+ cmp ? (cmp.sequential?.pValue ?? cmp.pValue).toFixed(4) : "\u2013",
58
+ cmp ? `${pct((cmp.sequential?.ci ?? cmp.ci).low, true)} \u2026 ${pct((cmp.sequential?.ci ?? cmp.ci).high, true)}` : "\u2013"
59
+ ];
60
+ })
61
+ )
62
+ );
63
+ const maxRate = Math.max(1e-4, ...v.sample.variants.map((cnt) => rates.get(cnt.variant) ?? 0));
64
+ const nameWidth = Math.max(...v.sample.variants.map((cnt) => cnt.variant.length));
65
+ console.log();
66
+ console.log(c.dim("RATE"));
67
+ for (const cnt of v.sample.variants) {
68
+ const cmp = v.comparisons.find((x) => x.variant === cnt.variant);
69
+ const paint = v.winner === cnt.variant ? c.green : cmp && (cmp.relativeLift ?? 0) < 0 ? c.red : c.cyan;
70
+ const lift = cmp ? (cmp.relativeLift ?? 0) >= 0 ? c.green(`\u25B2 ${pct(cmp.relativeLift, true)}`) : c.red(`\u25BC ${pct(cmp.relativeLift, true)}`) : c.dim("baseline");
71
+ console.log(`${cnt.variant.padEnd(nameWidth)} ${bar(rates.get(cnt.variant) ?? 0, maxRate, 24, paint)} ${pct(rates.get(cnt.variant)).padStart(6)} ${lift}`);
72
+ }
73
+ if (v.comparisons.length) {
74
+ console.log();
75
+ console.log(c.dim("CHANCE TO BEAT THE CONTROL"));
76
+ for (const cmp of v.comparisons) {
77
+ const paint = cmp.probabilityToBeat >= 0.95 ? c.green : cmp.probabilityToBeat <= 0.05 ? c.red : c.yellow;
78
+ console.log(`${cmp.variant.padEnd(nameWidth)} ${bar(cmp.probabilityToBeat, 1, 24, paint)} ${pct(cmp.probabilityToBeat).padStart(6)}`);
79
+ }
80
+ }
81
+ if (v.srm) {
82
+ console.log();
83
+ console.log(c.dim(`SRM check: chi\xB2=${v.srm.chiSquare.toFixed(2)} p=${v.srm.pValue.toExponential(2)} ${v.srm.detected ? c.red("DETECTED") : "ok"}`));
84
+ }
85
+ if (v.status === "insufficient_sample" && v.sample.neededPerVariant) {
86
+ console.log(c.dim(`Estimated ${v.sample.neededPerVariant} visitors per variant needed to confirm the current lift.`));
87
+ }
88
+ if (r.factors?.length) {
89
+ console.log();
90
+ console.log(c.bold("Each factor on its own") + c.dim(" (a level sums every combination that uses it, compared with the first level)"));
91
+ for (const f of r.factors) {
92
+ console.log(
93
+ table(
94
+ [f.key.toUpperCase(), "VISITORS", "CONV.", "RATE", "LIFT", "P-VALUE"],
95
+ f.levels.map((l) => [l.level + (f.best === l.level ? c.green(" \u2714") : ""), String(l.exposures), String(l.conversions), pct(l.rate), l.relativeLift === null ? "\u2013" : pct(l.relativeLift, true), l.pValue === null ? "\u2013" : l.pValue.toFixed(3)])
96
+ )
97
+ );
98
+ }
99
+ }
100
+ if (v.metrics?.length) {
101
+ console.log();
102
+ console.log(metricsText(v));
103
+ }
104
+ if (r.status === "finished" && r.experiment !== HOLDOUT) {
105
+ console.log();
106
+ console.log(c.dim(FINISHED_NEXT));
107
+ }
108
+ }
109
+ var FINISHED_NEXT = "This test is finished but its variants are still in the code. Decide which variant to keep, then run npx splithub ship <id> --keep <variant> (agents: the splithub-ship skill).";
110
+ function daysLeft(days) {
111
+ if (days === null || days === void 0 || !Number.isFinite(days) || days <= 0) return null;
112
+ const d = Math.ceil(days);
113
+ return d === 1 ? "About 1 more day to a verdict at this traffic." : `About ${d} more days to a verdict at this traffic.`;
114
+ }
115
+ function label(status) {
116
+ const icon = verdictIcon(status);
117
+ switch (status) {
118
+ case "winner_found":
119
+ return `${icon} WINNER`;
120
+ case "srm_detected":
121
+ return `${icon} SRM`;
122
+ case "insufficient_sample":
123
+ return `${icon} TOO EARLY`;
124
+ case "no_significant_difference":
125
+ return `${icon} NO DIFFERENCE`;
126
+ case "guardrail_breached":
127
+ return `${icon} DON'T SHIP`;
128
+ default:
129
+ return `${icon} NO DATA`;
130
+ }
131
+ }
132
+ var HOLDOUT = "__holdout";
133
+ function metricsText(v) {
134
+ const out = [];
135
+ for (const m of v.metrics ?? []) {
136
+ const head = m.role === "guardrail" ? `GUARDRAIL ${m.metric}${m.worse === "higher" ? " (a rise is bad)" : ""}` : `ALSO ${m.metric}`;
137
+ out.push(c.dim(head));
138
+ out.push(
139
+ table(
140
+ ["VARIANT", "CONV", "RATE", "LIFT", "P-VALUE", ""],
141
+ m.variants.map((cnt) => {
142
+ const cmp = m.comparisons.find((x) => x.variant === cnt.variant);
143
+ const breach = v.guardrailBreached?.find((b) => b.metric === m.metric && b.variant === cnt.variant);
144
+ return [cnt.variant, String(cnt.conversions), pct(cnt.exposures ? cnt.conversions / cnt.exposures : 0), cmp ? pct(cmp.relativeLift, true) : "\u2013", cmp ? (cmp.sequential?.pValue ?? cmp.pValue).toFixed(4) : "\u2013", breach ? c.red("\u2716 worse") : ""];
145
+ })
146
+ )
147
+ );
148
+ }
149
+ return out.join("\n");
150
+ }
151
+
152
+ export {
153
+ results,
154
+ daysLeft,
155
+ HOLDOUT,
156
+ metricsText
157
+ };
158
+ //# sourceMappingURL=chunk-UUTLLC54.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/commands/results.ts"],"sourcesContent":["import type { ExperimentResults } from '@splithub/node';\nimport { fetchResults, resolveTarget } from '../api';\nimport { fail, printJson } from '../errors';\nimport { detectProject } from '../project';\nimport { bar, c, pct, statusColor, table, verdictColor, verdictIcon } from '../ui';\n\nexport async function results(key: string | undefined, flags: Record<string, unknown>): Promise<void> {\n if (!key) fail('USAGE', 'Which experiment? npx splithub results <id>');\n const target = resolveTarget(flags, detectProject());\n const r = await fetchResults(target, key);\n if (flags.json) {\n printJson({ ...r, daysToVerdict: r.daysToVerdict ?? null, ...(r.status === 'finished' ? { next: FINISHED_NEXT } : {}) });\n return;\n }\n const v = r.verdict;\n const title = r.experiment === HOLDOUT ? 'Holdout: long-term impact' : r.experiment;\n console.log(`${c.bold(title)} ${c.dim(`· ${r.kind} · `)}${statusColor(r.status)} ${c.dim(`· goal: ${r.goal ?? 'none'}`)}`);\n if (r.description) console.log(c.dim(r.description));\n console.log();\n console.log(verdictColor(v.status, `${label(v.status)} ${v.message}`));\n const eta = daysLeft(r.daysToVerdict);\n if (eta && r.status === 'running') console.log(c.dim(eta));\n for (const note of v.notes ?? []) console.log(c.dim(`· ${note}`));\n console.log();\n\n const rates = new Map<string, number>();\n for (const cnt of v.sample.variants) rates.set(cnt.variant, cnt.exposures > 0 ? cnt.conversions / cnt.exposures : 0);\n\n console.log(\n table(\n ['VARIANT', 'VISITORS', 'CONV', 'RATE', 'LIFT', 'P(BEAT)', 'P-VALUE', '95% CI'],\n // Always-valid (sequential) p-value and interval: the ones the verdict uses, safe to read any day.\n v.sample.variants.map((cnt, i) => {\n const cmp = v.comparisons.find((x) => x.variant === cnt.variant);\n const name = i === 0 ? `${cnt.variant} ${c.dim('(control)')}` : cnt.variant;\n const marked = v.winner === cnt.variant ? c.green(`★ ${name}`) : name;\n return [\n marked,\n String(cnt.exposures),\n String(cnt.conversions),\n pct(rates.get(cnt.variant)),\n cmp ? pct(cmp.relativeLift, true) : '–',\n cmp ? pct(cmp.probabilityToBeat) : '–',\n cmp ? (cmp.sequential?.pValue ?? cmp.pValue).toFixed(4) : '–',\n cmp ? `${pct((cmp.sequential?.ci ?? cmp.ci).low, true)} … ${pct((cmp.sequential?.ci ?? cmp.ci).high, true)}` : '–',\n ];\n })\n )\n );\n // Rates as bars, lift next to them; P(beat) as a second bar. Reads at a glance in a terminal.\n const maxRate = Math.max(0.0001, ...v.sample.variants.map((cnt) => rates.get(cnt.variant) ?? 0));\n const nameWidth = Math.max(...v.sample.variants.map((cnt) => cnt.variant.length));\n console.log();\n console.log(c.dim('RATE'));\n for (const cnt of v.sample.variants) {\n const cmp = v.comparisons.find((x) => x.variant === cnt.variant);\n const paint = v.winner === cnt.variant ? c.green : cmp && (cmp.relativeLift ?? 0) < 0 ? c.red : c.cyan;\n const lift = cmp ? (cmp.relativeLift ?? 0) >= 0 ? c.green(`▲ ${pct(cmp.relativeLift, true)}`) : c.red(`▼ ${pct(cmp.relativeLift, true)}`) : c.dim('baseline');\n console.log(`${cnt.variant.padEnd(nameWidth)} ${bar(rates.get(cnt.variant) ?? 0, maxRate, 24, paint)} ${pct(rates.get(cnt.variant)).padStart(6)} ${lift}`);\n }\n if (v.comparisons.length) {\n console.log();\n console.log(c.dim('CHANCE TO BEAT THE CONTROL'));\n for (const cmp of v.comparisons) {\n const paint = cmp.probabilityToBeat >= 0.95 ? c.green : cmp.probabilityToBeat <= 0.05 ? c.red : c.yellow;\n console.log(`${cmp.variant.padEnd(nameWidth)} ${bar(cmp.probabilityToBeat, 1, 24, paint)} ${pct(cmp.probabilityToBeat).padStart(6)}`);\n }\n }\n if (v.srm) {\n console.log();\n console.log(c.dim(`SRM check: chi²=${v.srm.chiSquare.toFixed(2)} p=${v.srm.pValue.toExponential(2)} ${v.srm.detected ? c.red('DETECTED') : 'ok'}`));\n }\n if (v.status === 'insufficient_sample' && v.sample.neededPerVariant) {\n console.log(c.dim(`Estimated ${v.sample.neededPerVariant} visitors per variant needed to confirm the current lift.`));\n }\n if (r.factors?.length) {\n console.log();\n console.log(c.bold('Each factor on its own') + c.dim(' (a level sums every combination that uses it, compared with the first level)'));\n for (const f of r.factors) {\n console.log(\n table(\n [f.key.toUpperCase(), 'VISITORS', 'CONV.', 'RATE', 'LIFT', 'P-VALUE'],\n f.levels.map((l) => [l.level + (f.best === l.level ? c.green(' ✔') : ''), String(l.exposures), String(l.conversions), pct(l.rate), l.relativeLift === null ? '–' : pct(l.relativeLift, true), l.pValue === null ? '–' : l.pValue.toFixed(3)])\n )\n );\n }\n }\n if (v.metrics?.length) {\n console.log();\n console.log(metricsText(v));\n }\n if (r.status === 'finished' && r.experiment !== HOLDOUT) {\n console.log();\n console.log(c.dim(FINISHED_NEXT));\n }\n}\n\n/** Printed for finished tests, so a human or an agent knows the code still carries every variant. */\nconst FINISHED_NEXT = 'This test is finished but its variants are still in the code. Decide which variant to keep, then run npx splithub ship <id> --keep <variant> (agents: the splithub-ship skill).';\n\n/** \"About 6 more days\" for a verdict ETA; null when unknown or already there. */\nexport function daysLeft(days: number | null | undefined): string | null {\n if (days === null || days === undefined || !Number.isFinite(days) || days <= 0) return null;\n const d = Math.ceil(days);\n return d === 1 ? 'About 1 more day to a verdict at this traffic.' : `About ${d} more days to a verdict at this traffic.`;\n}\n\nfunction label(status: string): string {\n const icon = verdictIcon(status);\n switch (status) {\n case 'winner_found':\n return `${icon} WINNER`;\n case 'srm_detected':\n return `${icon} SRM`;\n case 'insufficient_sample':\n return `${icon} TOO EARLY`;\n case 'no_significant_difference':\n return `${icon} NO DIFFERENCE`;\n case 'guardrail_breached':\n return `${icon} DON'T SHIP`;\n default:\n return `${icon} NO DATA`;\n }\n}\n\nexport const HOLDOUT = '__holdout';\n\ntype V = ExperimentResults['verdict'];\n\n/** Secondary metrics and guardrails, one small table each. A breached guardrail is marked. */\nexport function metricsText(v: V): string {\n const out: string[] = [];\n for (const m of v.metrics ?? []) {\n const head = m.role === 'guardrail' ? `GUARDRAIL ${m.metric}${m.worse === 'higher' ? ' (a rise is bad)' : ''}` : `ALSO ${m.metric}`;\n out.push(c.dim(head));\n out.push(\n table(\n ['VARIANT', 'CONV', 'RATE', 'LIFT', 'P-VALUE', ''],\n m.variants.map((cnt) => {\n const cmp = m.comparisons.find((x) => x.variant === cnt.variant);\n const breach = v.guardrailBreached?.find((b) => b.metric === m.metric && b.variant === cnt.variant);\n return [cnt.variant, String(cnt.conversions), pct(cnt.exposures ? cnt.conversions / cnt.exposures : 0), cmp ? pct(cmp.relativeLift, true) : '–', cmp ? (cmp.sequential?.pValue ?? cmp.pValue).toFixed(4) : '–', breach ? c.red('✖ worse') : ''];\n })\n )\n );\n }\n return out.join('\\n');\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAMA,eAAsB,QAAQ,KAAyB,OAA+C;AACpG,MAAI,CAAC,IAAK,MAAK,SAAS,6CAA6C;AACrE,QAAM,SAAS,cAAc,OAAO,cAAc,CAAC;AACnD,QAAM,IAAI,MAAM,aAAa,QAAQ,GAAG;AACxC,MAAI,MAAM,MAAM;AACd,cAAU,EAAE,GAAG,GAAG,eAAe,EAAE,iBAAiB,MAAM,GAAI,EAAE,WAAW,aAAa,EAAE,MAAM,cAAc,IAAI,CAAC,EAAG,CAAC;AACvH;AAAA,EACF;AACA,QAAM,IAAI,EAAE;AACZ,QAAM,QAAQ,EAAE,eAAe,UAAU,8BAA8B,EAAE;AACzE,UAAQ,IAAI,GAAG,EAAE,KAAK,K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