@nordsym/apiclaw 2.0.0 → 2.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.
@@ -0,0 +1,355 @@
1
+ import { internalMutation, mutation, query } from "./_generated/server";
2
+ import { v } from "convex/values";
3
+ import type { Id, Doc } from "./_generated/dataModel";
4
+
5
+ /**
6
+ * APIClaw nurture system.
7
+ *
8
+ * Lifecycle stages:
9
+ * new — <48h since signup, no meaningful activity
10
+ * activating — some discovery/searches, no apiCalls
11
+ * active — recent apiCalls
12
+ * power — >50 calls in last 14d
13
+ * dormant — no activity 7d+
14
+ * lost — no activity 30d+
15
+ * partner-locked — explicit partner workspace, NEVER nurture
16
+ * excluded — internal/test/opted-out
17
+ *
18
+ * Emails (sent via symbot-gmail webhook):
19
+ * welcome — day 0-1 after signup (stage=new)
20
+ * try-discover — day 2-3 if no searches yet (stage=new)
21
+ * first-call — day 5-7 after first search, no calls (stage=activating)
22
+ * upgrade — day 14 for active users (stage=active)
23
+ * power-upgrade — power users, upsell to scale/pro
24
+ * reactivate-7d — dormant workspace, soft nudge
25
+ * reactivate-30d — lost workspace, last-chance nudge
26
+ */
27
+
28
+ const DAY = 86400000;
29
+ const HOUR = 3600000;
30
+
31
+ // Permanent no-email list — partner domains, tests, disposable
32
+ const DOMAIN_BLOCKLIST = [
33
+ "apilayer.com",
34
+ "filestack.com",
35
+ "nordsym.com",
36
+ "cqtinvest.com",
37
+ "apiclaw.local", // synthetic anonymous workspaces from trafficGenerator
38
+ "example.com",
39
+ "wnbaldwy.com", // known disposable
40
+ ];
41
+
42
+ const EMAIL_BLOCKLIST = new Set<string>([
43
+ "pratham.kumar@apilayer.com",
44
+ "marketing@filestack.com",
45
+ "gustav@nordsym.com",
46
+ "symbot@nordsym.com",
47
+ "molle@nordsym.com",
48
+ "molle@cqtinvest.com",
49
+ "gustav_hemmingsson@hotmail.com",
50
+ "test@example.com",
51
+ "m6jgi9d8i1@wnbaldwy.com",
52
+ "maxence.dabrowski81@gmail.com", // real external user — opt-out default
53
+ "andylopeslindao@gmail.com",
54
+ ]);
55
+
56
+ function domainOf(email: string): string {
57
+ const at = email.lastIndexOf("@");
58
+ return at === -1 ? "" : email.slice(at + 1).toLowerCase();
59
+ }
60
+
61
+ function isBlocked(email: string): boolean {
62
+ const lower = email.toLowerCase();
63
+ if (EMAIL_BLOCKLIST.has(lower)) return true;
64
+ const dom = domainOf(lower);
65
+ if (DOMAIN_BLOCKLIST.includes(dom)) return true;
66
+ return false;
67
+ }
68
+
69
+ export const getByWorkspaceId = query({
70
+ args: { workspaceId: v.id("workspaces") },
71
+ handler: async (ctx, args) => {
72
+ return await ctx.db
73
+ .query("nurture")
74
+ .withIndex("by_workspaceId", (q) => q.eq("workspaceId", args.workspaceId))
75
+ .first();
76
+ },
77
+ });
78
+
79
+ export const list = query({
80
+ args: {},
81
+ handler: async (ctx) => {
82
+ return await ctx.db.query("nurture").collect();
83
+ },
84
+ });
85
+
86
+ export const stats = query({
87
+ args: {},
88
+ handler: async (ctx) => {
89
+ const all = await ctx.db.query("nurture").collect();
90
+ const byStage: Record<string, number> = {};
91
+ let totalSent = 0;
92
+ for (const n of all) {
93
+ byStage[n.stage] = (byStage[n.stage] || 0) + 1;
94
+ totalSent += n.emailsSent;
95
+ }
96
+ return { total: all.length, byStage, totalSent };
97
+ },
98
+ });
99
+
100
+ export const optOut = mutation({
101
+ args: { workspaceId: v.id("workspaces") },
102
+ handler: async (ctx, args) => {
103
+ const n = await ctx.db
104
+ .query("nurture")
105
+ .withIndex("by_workspaceId", (q) => q.eq("workspaceId", args.workspaceId))
106
+ .first();
107
+ if (!n) return { success: false };
108
+ await ctx.db.patch(n._id, { unsubscribed: true, stage: "excluded", updatedAt: Date.now() });
109
+ return { success: true };
110
+ },
111
+ });
112
+
113
+ // ═══════════════════════════════════════════════════════════════
114
+ // CLASSIFIER — runs daily, upserts nurture row per workspace
115
+ // ═══════════════════════════════════════════════════════════════
116
+ export const classifyAllWorkspaces = internalMutation({
117
+ args: {},
118
+ handler: async (ctx) => {
119
+ const now = Date.now();
120
+ const workspaces = await ctx.db.query("workspaces").collect();
121
+
122
+ let upserted = 0;
123
+ let lockedCount = 0;
124
+ let excludedCount = 0;
125
+
126
+ for (const w of workspaces) {
127
+ const email = (w.email || "").toLowerCase();
128
+ const existing = await ctx.db
129
+ .query("nurture")
130
+ .withIndex("by_workspaceId", (q) => q.eq("workspaceId", w._id))
131
+ .first();
132
+
133
+ // Partner / excluded classification takes precedence only if email IS blocked
134
+ // (Missing email = anonymous workspace → stays in lifecycle, just unreachable by sender)
135
+ let stage: Doc<"nurture">["stage"] = "new";
136
+ const hasBlockedEmail = email && isBlocked(email);
137
+ if (hasBlockedEmail) {
138
+ const dom = domainOf(email);
139
+ stage = (dom === "apilayer.com" || dom === "filestack.com") ? "partner-locked" : "excluded";
140
+ } else {
141
+ // Compute activity
142
+ const ageMs = now - w.createdAt;
143
+ const lastActive = w.lastActiveAt || 0;
144
+ const inactivityMs = lastActive ? now - lastActive : ageMs;
145
+
146
+ // Calls in last 14d
147
+ const calls14d = await ctx.db
148
+ .query("apiCalls")
149
+ .withIndex("by_workspaceId", (q) => q.eq("workspaceId", w._id))
150
+ .collect();
151
+ const recent14dCalls = calls14d.filter((c) => now - c.timestamp < 14 * DAY).length;
152
+
153
+ // Searches ever
154
+ const searches = await ctx.db
155
+ .query("searchLogs")
156
+ .withIndex("by_workspaceId", (q) => q.eq("workspaceId", w._id))
157
+ .collect();
158
+ const totalSearches = searches.length;
159
+ const recent7dSearches = searches.filter((s) => now - s.timestamp < 7 * DAY).length;
160
+
161
+ if (inactivityMs > 30 * DAY) stage = "lost";
162
+ else if (inactivityMs > 7 * DAY) stage = "dormant";
163
+ else if (recent14dCalls >= 50) stage = "power";
164
+ else if (recent14dCalls > 0 || (w.usageCount || 0) > 0) stage = "active";
165
+ else if (totalSearches > 0 || recent7dSearches > 0) stage = "activating";
166
+ else stage = "new";
167
+ }
168
+
169
+ if (stage === "partner-locked") lockedCount++;
170
+ if (stage === "excluded") excludedCount++;
171
+
172
+ if (existing) {
173
+ // Don't demote unsubscribed users
174
+ if (existing.unsubscribed) {
175
+ await ctx.db.patch(existing._id, { updatedAt: now });
176
+ continue;
177
+ }
178
+ await ctx.db.patch(existing._id, {
179
+ stage,
180
+ email: email || undefined,
181
+ lastActivityAt: w.lastActiveAt,
182
+ updatedAt: now,
183
+ });
184
+ } else {
185
+ await ctx.db.insert("nurture", {
186
+ workspaceId: w._id,
187
+ email: email || undefined,
188
+ stage,
189
+ lastActivityAt: w.lastActiveAt,
190
+ emailsSent: 0,
191
+ unsubscribed: false,
192
+ createdAt: now,
193
+ updatedAt: now,
194
+ });
195
+ upserted++;
196
+ }
197
+ }
198
+
199
+ return {
200
+ workspaceCount: workspaces.length,
201
+ newlyTrackedCount: upserted,
202
+ partnerLocked: lockedCount,
203
+ excluded: excludedCount,
204
+ };
205
+ },
206
+ });
207
+
208
+ // ═══════════════════════════════════════════════════════════════
209
+ // SENDER — daily cron picks up to N sendable nurture rows
210
+ // ═══════════════════════════════════════════════════════════════
211
+ const SYMBOT_GMAIL = "https://nordsym.app.n8n.cloud/webhook/symbot-gmail";
212
+
213
+ function bodyFor(kind: string, firstName: string): { subject: string; html: string } {
214
+ const hi = firstName ? `Hi ${firstName},` : "Hi,";
215
+ const footer = `<p style="font-size:11px;color:#999;margin-top:32px;">APIClaw — The API layer for AI agents. <a href="https://apiclaw.cloud" style="color:#dc2626;">apiclaw.cloud</a></p>`;
216
+
217
+ switch (kind) {
218
+ case "welcome":
219
+ return {
220
+ subject: "Welcome to APIClaw — 26k APIs ready for your agents",
221
+ html: `<p>${hi}</p><p>Your APIClaw workspace is ready. You've got access to 26,704 discoverable APIs and 1,654 callable ones via a single endpoint.</p><p>Easiest first step: <a href="https://apiclaw.cloud/catalog">browse the catalog</a> or run <code>discover_apis</code> from your agent.</p><p>— Gustav, APIClaw</p>${footer}`,
222
+ };
223
+ case "try-discover":
224
+ return {
225
+ subject: "Try one search — see what APIClaw knows",
226
+ html: `<p>${hi}</p><p>Haven't tried discovery yet? One search shows you why this is worth it.</p><p>Try: <code>discover_apis("weather forecast")</code> or hit the <a href="https://apiclaw.cloud/catalog">catalog</a>. Weather, currency, flight data, PDFs, images — agents get a working API in one call.</p><p>— Gustav</p>${footer}`,
227
+ };
228
+ case "first-call":
229
+ return {
230
+ subject: "Make your first API call — takes 30 seconds",
231
+ html: `<p>${hi}</p><p>You've searched the catalog — next step is calling an API. No key management, no SDK integration:</p><pre style="background:#f5f5f5;padding:12px;border-radius:6px;font-size:12px;">call_api("apilayer", "weatherstack", { query: "Stockholm" })</pre><p>The <a href="https://apiclaw.cloud/docs">docs</a> have copy-paste examples.</p><p>— Gustav</p>${footer}`,
232
+ };
233
+ case "upgrade":
234
+ return {
235
+ subject: "Two weeks in — worth going Pro?",
236
+ html: `<p>${hi}</p><p>Your agent has been busy. Free tier is 50 calls/week — Pro is unlimited + priority routing + deeper analytics.</p><p><a href="https://apiclaw.cloud/upgrade" style="display:inline-block;background:#dc2626;color:white;padding:10px 20px;border-radius:6px;text-decoration:none;">See Pro pricing</a></p><p>— Gustav</p>${footer}`,
237
+ };
238
+ case "power-upgrade":
239
+ return {
240
+ subject: "You're a heavy user — Scale tier saves you money",
241
+ html: `<p>${hi}</p><p>You're making 50+ calls every two weeks. At that rate, Scale tier ($49/mo for 10k calls) beats per-call pricing.</p><p><a href="https://apiclaw.cloud/upgrade" style="display:inline-block;background:#dc2626;color:white;padding:10px 20px;border-radius:6px;text-decoration:none;">Move to Scale</a></p><p>— Gustav</p>${footer}`,
242
+ };
243
+ case "reactivate-7d":
244
+ return {
245
+ subject: "Quiet week — anything I can help unblock?",
246
+ html: `<p>${hi}</p><p>No calls this week. If something's broken or confusing, tell me — reply goes straight to me.</p><p>Or jump back in: <a href="https://apiclaw.cloud/catalog">apiclaw.cloud/catalog</a></p><p>— Gustav</p>${footer}`,
247
+ };
248
+ case "reactivate-30d":
249
+ return {
250
+ subject: "Still here? Free to stay free.",
251
+ html: `<p>${hi}</p><p>Your workspace is still live. If APIClaw isn't the right fit, no worries — reply STOP and I'll opt you out.</p><p>If it is: <a href="https://apiclaw.cloud/catalog">one search gets you back in</a>.</p><p>— Gustav</p>${footer}`,
252
+ };
253
+ default:
254
+ return { subject: "APIClaw update", html: `<p>${hi}</p>${footer}` };
255
+ }
256
+ }
257
+
258
+ async function sendEmail(to: string, subject: string, html: string): Promise<boolean> {
259
+ try {
260
+ const res = await fetch(SYMBOT_GMAIL, {
261
+ method: "POST",
262
+ headers: { "Content-Type": "application/json" },
263
+ body: JSON.stringify({ action: "send", to, subject, message: html, safeMode: true }),
264
+ });
265
+ return res.ok;
266
+ } catch {
267
+ return false;
268
+ }
269
+ }
270
+
271
+ function pickEmailKind(n: Doc<"nurture">, wsCreatedAt: number): string | null {
272
+ const now = Date.now();
273
+ const ageMs = now - wsCreatedAt;
274
+ const lastEmailMs = n.lastEmailSentAt ? now - n.lastEmailSentAt : Infinity;
275
+
276
+ // Never stack emails closer than 72h except for the onboarding welcome which can follow signup quickly
277
+ if (lastEmailMs < 72 * HOUR && n.lastEmailKind !== null && n.lastEmailKind !== undefined) return null;
278
+
279
+ if (n.unsubscribed) return null;
280
+
281
+ if (n.stage === "partner-locked" || n.stage === "excluded") return null;
282
+
283
+ // Welcome (day 0-2)
284
+ if (n.emailsSent === 0 && ageMs < 2 * DAY) return "welcome";
285
+
286
+ // Try-discover (day 2-4, stage still "new")
287
+ if (n.stage === "new" && ageMs >= 2 * DAY && ageMs < 5 * DAY && n.lastEmailKind !== "try-discover") return "try-discover";
288
+
289
+ // First-call (stage activating, day 5-10)
290
+ if (n.stage === "activating" && ageMs >= 4 * DAY && n.lastEmailKind !== "first-call") return "first-call";
291
+
292
+ // Upgrade nudge (stage active, day 12+, only once)
293
+ if (n.stage === "active" && ageMs >= 12 * DAY && n.lastEmailKind !== "upgrade") return "upgrade";
294
+
295
+ // Power upgrade
296
+ if (n.stage === "power" && n.lastEmailKind !== "power-upgrade") return "power-upgrade";
297
+
298
+ // Reactivation
299
+ if (n.stage === "dormant" && n.lastEmailKind !== "reactivate-7d") return "reactivate-7d";
300
+ if (n.stage === "lost" && n.lastEmailKind !== "reactivate-30d") return "reactivate-30d";
301
+
302
+ return null;
303
+ }
304
+
305
+ export const sendDailyNurture = internalMutation({
306
+ args: { maxSends: v.optional(v.number()), dryRun: v.optional(v.boolean()) },
307
+ handler: async (ctx, args) => {
308
+ const cap = args.maxSends ?? 12; // conservative daily send cap
309
+ const dryRun = args.dryRun ?? false;
310
+
311
+ const rows = await ctx.db.query("nurture").collect();
312
+ let sent = 0;
313
+ let considered = 0;
314
+ const sentLog: Array<{ email: string; kind: string }> = [];
315
+
316
+ for (const n of rows) {
317
+ if (sent >= cap) break;
318
+ if (!n.email) continue;
319
+ if (n.unsubscribed) continue;
320
+ if (n.stage === "partner-locked" || n.stage === "excluded") continue;
321
+ if (isBlocked(n.email)) continue;
322
+
323
+ const ws = await ctx.db.get(n.workspaceId);
324
+ if (!ws) continue;
325
+
326
+ const kind = pickEmailKind(n, ws.createdAt);
327
+ considered++;
328
+ if (!kind) continue;
329
+
330
+ const firstName = (n.email.split("@")[0] || "").split(/[._-]/)[0];
331
+ const firstNamePretty = firstName.charAt(0).toUpperCase() + firstName.slice(1);
332
+ const { subject, html } = bodyFor(kind, firstNamePretty);
333
+
334
+ if (dryRun) {
335
+ sentLog.push({ email: n.email, kind });
336
+ sent++;
337
+ continue;
338
+ }
339
+
340
+ const ok = await sendEmail(n.email, subject, html);
341
+ if (!ok) continue;
342
+
343
+ await ctx.db.patch(n._id, {
344
+ emailsSent: n.emailsSent + 1,
345
+ lastEmailSentAt: Date.now(),
346
+ lastEmailKind: kind,
347
+ updatedAt: Date.now(),
348
+ });
349
+ sentLog.push({ email: n.email, kind });
350
+ sent++;
351
+ }
352
+
353
+ return { sent, considered, capacity: cap, dryRun, sentLog };
354
+ },
355
+ });
package/convex/schema.ts CHANGED
@@ -364,6 +364,19 @@ export default defineSchema({
364
364
  .index("by_workspaceId", ["workspaceId"])
365
365
  .index("by_status", ["status"]),
366
366
 
367
+ // OTP codes for terminal-native email verification
368
+ otpCodes: defineTable({
369
+ email: v.string(),
370
+ code: v.string(), // 6-digit code
371
+ fingerprint: v.optional(v.string()),
372
+ expiresAt: v.number(),
373
+ usedAt: v.optional(v.number()),
374
+ attempts: v.number(), // failed attempts counter
375
+ createdAt: v.number(),
376
+ })
377
+ .index("by_email", ["email"])
378
+ .index("by_email_code", ["email", "code"]),
379
+
367
380
  // Magic link tokens for email auth
368
381
  magicLinks: defineTable({
369
382
  email: v.string(),
@@ -413,6 +426,33 @@ export default defineSchema({
413
426
  .index("by_workspaceId", ["workspaceId"])
414
427
  .index("by_identifier", ["identifier"]),
415
428
 
429
+ // ============================================
430
+ // FUNNEL EVENTS — canonical conversion truth
431
+ // install -> first_run -> register_owner -> verify_code -> first_call_api_success
432
+ // ============================================
433
+ funnelEvents: defineTable({
434
+ event: v.string(), // one of FUNNEL_EVENTS (see convex/funnel.ts)
435
+ classification: v.string(), // "human" | "ci" | "bot" | "internal"
436
+ workspaceId: v.optional(v.id("workspaces")),
437
+ fingerprint: v.optional(v.string()),
438
+ sessionToken: v.optional(v.string()),
439
+ email: v.optional(v.string()),
440
+ userAgent: v.optional(v.string()),
441
+ mcpClient: v.optional(v.string()),
442
+ platform: v.optional(v.string()),
443
+ version: v.optional(v.string()),
444
+ dedupeKey: v.optional(v.string()), // for first-time events (install/first_run/first_call)
445
+ props: v.optional(v.any()),
446
+ timestamp: v.number(),
447
+ })
448
+ .index("by_event", ["event"])
449
+ .index("by_classification", ["classification"])
450
+ .index("by_workspaceId", ["workspaceId"])
451
+ .index("by_fingerprint", ["fingerprint"])
452
+ .index("by_dedupeKey", ["dedupeKey"])
453
+ .index("by_timestamp", ["timestamp"])
454
+ .index("by_event_timestamp", ["event", "timestamp"]),
455
+
416
456
  // MCP Server telemetry (anonymous usage tracking)
417
457
  telemetry: defineTable({
418
458
  type: v.string(), // "startup", "search", "execute", "discovery"
@@ -853,4 +893,34 @@ export default defineSchema({
853
893
  })
854
894
  .index("by_partnerId", ["partnerId"])
855
895
  .index("by_status", ["status"]),
896
+
897
+ // ═══════════════════════════════════════════════════════════════
898
+ // NURTURE - Workspace lifecycle + email nurture state
899
+ // ═══════════════════════════════════════════════════════════════
900
+ nurture: defineTable({
901
+ workspaceId: v.id("workspaces"),
902
+ email: v.optional(v.string()), // mirrored for easy dedupe
903
+ stage: v.union(
904
+ v.literal("new"), // <48h since signup, no activity yet
905
+ v.literal("activating"), // seen some discovery, no calls
906
+ v.literal("active"), // has made API calls recently
907
+ v.literal("power"), // >50 calls in last 14d
908
+ v.literal("dormant"), // no activity 7d+
909
+ v.literal("lost"), // no activity 30d+
910
+ v.literal("partner-locked"), // partner workspace — never nurture
911
+ v.literal("excluded") // explicit opt-out / internal / test
912
+ ),
913
+ lastActivityAt: v.optional(v.number()),
914
+ emailsSent: v.number(), // total nurture emails sent
915
+ lastEmailSentAt: v.optional(v.number()),
916
+ lastEmailKind: v.optional(v.string()), // "welcome", "try-discover", "first-call", "upgrade", "reactivate-7d", "reactivate-30d", "power-upgrade"
917
+ unsubscribed: v.boolean(),
918
+ notes: v.optional(v.string()),
919
+ createdAt: v.number(),
920
+ updatedAt: v.number(),
921
+ })
922
+ .index("by_workspaceId", ["workspaceId"])
923
+ .index("by_email", ["email"])
924
+ .index("by_stage", ["stage"])
925
+ .index("by_lastActivityAt", ["lastActivityAt"]),
856
926
  });
@@ -2,6 +2,191 @@ import { mutation, query } from "./_generated/server";
2
2
  import { internal } from "./_generated/api";
3
3
  import { v } from "convex/values";
4
4
 
5
+ // ============================================
6
+ // OTP AUTH FOR WORKSPACES (terminal-native)
7
+ // ============================================
8
+
9
+ function generateOTP(): string {
10
+ const digits = "0123456789";
11
+ let code = "";
12
+ for (let i = 0; i < 6; i++) {
13
+ code += digits.charAt(Math.floor(Math.random() * digits.length));
14
+ }
15
+ return code;
16
+ }
17
+
18
+ // Create OTP code and return it (MCP server sends the email)
19
+ export const createOTP = mutation({
20
+ args: {
21
+ email: v.string(),
22
+ fingerprint: v.optional(v.string()),
23
+ },
24
+ handler: async (ctx, { email, fingerprint }) => {
25
+ const normalizedEmail = email.toLowerCase().trim();
26
+
27
+ // Invalidate any existing unused OTPs for this email
28
+ const existing = await ctx.db
29
+ .query("otpCodes")
30
+ .withIndex("by_email", (q) => q.eq("email", normalizedEmail))
31
+ .collect();
32
+ for (const otp of existing) {
33
+ if (!otp.usedAt && otp.expiresAt > Date.now()) {
34
+ await ctx.db.patch(otp._id, { expiresAt: 0 }); // expire it
35
+ }
36
+ }
37
+
38
+ const code = generateOTP();
39
+ const expiresAt = Date.now() + 10 * 60 * 1000; // 10 minutes
40
+
41
+ await ctx.db.insert("otpCodes", {
42
+ email: normalizedEmail,
43
+ code,
44
+ fingerprint,
45
+ expiresAt,
46
+ usedAt: undefined,
47
+ attempts: 0,
48
+ createdAt: Date.now(),
49
+ });
50
+
51
+ return { code, expiresAt };
52
+ },
53
+ });
54
+
55
+ // Verify OTP code, create/activate workspace, return session
56
+ export const verifyOTP = mutation({
57
+ args: {
58
+ email: v.string(),
59
+ code: v.string(),
60
+ fingerprint: v.optional(v.string()),
61
+ },
62
+ handler: async (ctx, { email, code, fingerprint }) => {
63
+ const normalizedEmail = email.toLowerCase().trim();
64
+
65
+ const otpRecord = await ctx.db
66
+ .query("otpCodes")
67
+ .withIndex("by_email_code", (q) =>
68
+ q.eq("email", normalizedEmail).eq("code", code)
69
+ )
70
+ .first();
71
+
72
+ if (!otpRecord) {
73
+ return { success: false, error: "invalid_code", message: "Invalid verification code." };
74
+ }
75
+
76
+ if (otpRecord.usedAt) {
77
+ return { success: false, error: "code_used", message: "Code already used." };
78
+ }
79
+
80
+ if (otpRecord.expiresAt < Date.now()) {
81
+ return { success: false, error: "code_expired", message: "Code expired. Run register_owner again to get a new code." };
82
+ }
83
+
84
+ if (otpRecord.attempts >= 5) {
85
+ return { success: false, error: "too_many_attempts", message: "Too many failed attempts. Run register_owner again." };
86
+ }
87
+
88
+ // Mark OTP as used
89
+ await ctx.db.patch(otpRecord._id, { usedAt: Date.now() });
90
+
91
+ // Find or create workspace
92
+ let workspace = await ctx.db
93
+ .query("workspaces")
94
+ .withIndex("by_email", (q) => q.eq("email", normalizedEmail))
95
+ .first();
96
+
97
+ let isNewUser = false;
98
+ if (!workspace) {
99
+ isNewUser = true;
100
+ // Generate referral code
101
+ let referralCode: string;
102
+ let attempts = 0;
103
+ do {
104
+ const chars = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789";
105
+ let rc = "";
106
+ for (let i = 0; i < 6; i++) {
107
+ rc += chars.charAt(Math.floor(Math.random() * chars.length));
108
+ }
109
+ referralCode = `CLAW-${rc}`;
110
+ const existingRef = await ctx.db
111
+ .query("workspaces")
112
+ .withIndex("by_referralCode", (q) => q.eq("referralCode", referralCode))
113
+ .first();
114
+ if (!existingRef) break;
115
+ attempts++;
116
+ } while (attempts < 10);
117
+
118
+ const workspaceId = await ctx.db.insert("workspaces", {
119
+ email: normalizedEmail,
120
+ status: "active",
121
+ tier: "free",
122
+ usageCount: 0,
123
+ usageLimit: 50,
124
+ weeklyUsageCount: 0,
125
+ weeklyUsageLimit: 50,
126
+ lastWeeklyResetAt: Date.now(),
127
+ hourlyUsageCount: 0,
128
+ lastHourlyResetAt: Date.now(),
129
+ referralCode: referralCode!,
130
+ createdAt: Date.now(),
131
+ updatedAt: Date.now(),
132
+ });
133
+ workspace = await ctx.db.get(workspaceId);
134
+ } else if (workspace.status === "pending") {
135
+ // Activate pending workspace
136
+ await ctx.db.patch(workspace._id, { status: "active" });
137
+ workspace = await ctx.db.get(workspace._id);
138
+ }
139
+
140
+ if (!workspace) {
141
+ return { success: false, error: "workspace_error", message: "Failed to create workspace." };
142
+ }
143
+
144
+ // Create agent session
145
+ const sessionToken = generateToken();
146
+ await ctx.db.insert("agentSessions", {
147
+ workspaceId: workspace._id,
148
+ sessionToken,
149
+ fingerprint: fingerprint || "unknown",
150
+ lastUsedAt: Date.now(),
151
+ createdAt: Date.now(),
152
+ });
153
+
154
+ return {
155
+ success: true,
156
+ isNewUser,
157
+ sessionToken,
158
+ workspace: {
159
+ id: workspace._id,
160
+ email: workspace.email,
161
+ tier: workspace.tier,
162
+ status: "active",
163
+ usageCount: workspace.usageCount,
164
+ usageLimit: workspace.usageLimit,
165
+ },
166
+ };
167
+ },
168
+ });
169
+
170
+ // Increment failed OTP attempt counter
171
+ export const incrementOTPAttempt = mutation({
172
+ args: {
173
+ email: v.string(),
174
+ code: v.string(),
175
+ },
176
+ handler: async (ctx, { email, code }) => {
177
+ const normalizedEmail = email.toLowerCase().trim();
178
+ const otpRecord = await ctx.db
179
+ .query("otpCodes")
180
+ .withIndex("by_email_code", (q) =>
181
+ q.eq("email", normalizedEmail).eq("code", code)
182
+ )
183
+ .first();
184
+ if (otpRecord && !otpRecord.usedAt) {
185
+ await ctx.db.patch(otpRecord._id, { attempts: otpRecord.attempts + 1 });
186
+ }
187
+ },
188
+ });
189
+
5
190
  // ============================================
6
191
  // MAGIC LINK AUTH FOR WORKSPACES
7
192
  // ============================================
@@ -0,0 +1,24 @@
1
+ export type FunnelEventName = "install" | "first_run" | "register_owner" | "verify_code" | "first_call_api_success" | "register_owner_failed" | "verify_code_failed" | "call_api_blocked" | "call_api_error" | "quota_hit" | "gateway_retry";
2
+ export type Classification = "human" | "ci" | "bot" | "internal";
3
+ export declare function classifyLocalSource(input: {
4
+ userAgent?: string | null;
5
+ email?: string | null;
6
+ fingerprint?: string | null;
7
+ env?: NodeJS.ProcessEnv;
8
+ }): Classification;
9
+ export declare function hasLocalMarker(key: string): boolean;
10
+ export declare function setLocalMarker(key: string): void;
11
+ export interface EmitArgs {
12
+ event: FunnelEventName;
13
+ workspaceId?: string;
14
+ fingerprint?: string;
15
+ sessionToken?: string;
16
+ email?: string;
17
+ mcpClient?: string;
18
+ platform?: string;
19
+ version?: string;
20
+ dedupeKey?: string;
21
+ props?: Record<string, unknown>;
22
+ }
23
+ export declare function emitFunnelEvent(args: EmitArgs): void;
24
+ //# sourceMappingURL=funnel-client.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"funnel-client.d.ts","sourceRoot":"","sources":["../src/funnel-client.ts"],"names":[],"mappings":"AAYA,MAAM,MAAM,eAAe,GACvB,SAAS,GACT,WAAW,GACX,gBAAgB,GAChB,aAAa,GACb,wBAAwB,GAExB,uBAAuB,GACvB,oBAAoB,GACpB,kBAAkB,GAClB,gBAAgB,GAChB,WAAW,GACX,eAAe,CAAC;AAEpB,MAAM,MAAM,cAAc,GAAG,OAAO,GAAG,IAAI,GAAG,KAAK,GAAG,UAAU,CAAC;AAkCjE,wBAAgB,mBAAmB,CAAC,KAAK,EAAE;IACzC,SAAS,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC1B,KAAK,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACtB,WAAW,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC5B,GAAG,CAAC,EAAE,MAAM,CAAC,UAAU,CAAC;CACzB,GAAG,cAAc,CAmBjB;AAwBD,wBAAgB,cAAc,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAEnD;AAED,wBAAgB,cAAc,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI,CAIhD;AAED,MAAM,WAAW,QAAQ;IACvB,KAAK,EAAE,eAAe,CAAC;IACvB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACjC;AAED,wBAAgB,eAAe,CAAC,IAAI,EAAE,QAAQ,GAAG,IAAI,CAoCpD"}