@nordsym/apiclaw 2.1.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
@@ -426,6 +426,33 @@ export default defineSchema({
426
426
  .index("by_workspaceId", ["workspaceId"])
427
427
  .index("by_identifier", ["identifier"]),
428
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
+
429
456
  // MCP Server telemetry (anonymous usage tracking)
430
457
  telemetry: defineTable({
431
458
  type: v.string(), // "startup", "search", "execute", "discovery"
@@ -866,4 +893,34 @@ export default defineSchema({
866
893
  })
867
894
  .index("by_partnerId", ["partnerId"])
868
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"]),
869
926
  });
@@ -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":[],"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vent. Never throws, never
5
+ * blocks. See convex/funnel.ts for schema and classification rules.
6
+ */
7
+ import * as fs from "fs";
8
+ import * as path from "path";
9
+ import * as os from "os";
10
+ const CONVEX_URL = process.env.CONVEX_URL || "https://adventurous-avocet-799.convex.cloud";
11
+ const CI_ENV_KEYS = [
12
+ "CI",
13
+ "GITHUB_ACTIONS",
14
+ "GITLAB_CI",
15
+ "CIRCLECI",
16
+ "BUILDKITE",
17
+ "JENKINS_URL",
18
+ "TEAMCITY_VERSION",
19
+ "TRAVIS",
20
+ "BITBUCKET_BUILD_NUMBER",
21
+ ];
22
+ const BOT_UA_MARKERS = [
23
+ "bot",
24
+ "crawl",
25
+ "spider",
26
+ "scanner",
27
+ "curl/",
28
+ "wget/",
29
+ "httpclient",
30
+ "python-requests",
31
+ "go-http-client",
32
+ "okhttp",
33
+ "java/",
34
+ "httrack",
35
+ "headlesschrome",
36
+ "phantomjs",
37
+ ];
38
+ const INTERNAL_EMAIL_DOMAINS = ["nordsym.com", "apiclaw.cloud"];
39
+ const INTERNAL_EMAIL_EXACT = ["gustav@nordsym.com", "gustavnordsync@gmail.com"];
40
+ export function classifyLocalSource(input) {
41
+ const email = (input.email || "").toLowerCase().trim();
42
+ if (email) {
43
+ if (INTERNAL_EMAIL_EXACT.includes(email))
44
+ return "internal";
45
+ const domain = email.split("@")[1] || "";
46
+ if (INTERNAL_EMAIL_DOMAINS.includes(domain))
47
+ return "internal";
48
+ }
49
+ const env = input.env || process.env;
50
+ for (const key of CI_ENV_KEYS) {
51
+ const val = env[key];
52
+ if (val && val !== "false" && val !== "0")
53
+ return "ci";
54
+ }
55
+ const ua = (input.userAgent || "").toLowerCase();
56
+ if (ua) {
57
+ for (const m of BOT_UA_MARKERS) {
58
+ if (ua.includes(m))
59
+ return "bot";
60
+ }
61
+ }
62
+ return "human";
63
+ }
64
+ // Persistent first-run marker stored alongside the session file.
65
+ const MARKER_DIR = path.join(os.homedir(), ".apiclaw");
66
+ const MARKER_FILE = path.join(MARKER_DIR, "funnel-markers.json");
67
+ function readMarkers() {
68
+ try {
69
+ if (!fs.existsSync(MARKER_FILE))
70
+ return {};
71
+ return JSON.parse(fs.readFileSync(MARKER_FILE, "utf8")) || {};
72
+ }
73
+ catch {
74
+ return {};
75
+ }
76
+ }
77
+ function writeMarkers(m) {
78
+ try {
79
+ if (!fs.existsSync(MARKER_DIR))
80
+ fs.mkdirSync(MARKER_DIR, { mode: 0o700 });
81
+ fs.writeFileSync(MARKER_FILE, JSON.stringify(m), { mode: 0o600 });
82
+ }
83
+ catch {
84
+ /* ignore */
85
+ }
86
+ }
87
+ export function hasLocalMarker(key) {
88
+ return !!readMarkers()[key];
89
+ }
90
+ export function setLocalMarker(key) {
91
+ const m = readMarkers();
92
+ m[key] = Date.now();
93
+ writeMarkers(m);
94
+ }
95
+ export function emitFunnelEvent(args) {
96
+ if (process.env.APICLAW_TELEMETRY === "false")
97
+ return;
98
+ const classification = classifyLocalSource({
99
+ email: args.email,
100
+ fingerprint: args.fingerprint,
101
+ });
102
+ const payload = {
103
+ path: "funnel:recordEvent",
104
+ args: {
105
+ event: args.event,
106
+ classification,
107
+ workspaceId: args.workspaceId,
108
+ fingerprint: args.fingerprint,
109
+ sessionToken: args.sessionToken,
110
+ email: args.email,
111
+ userAgent: `apiclaw-mcp/${args.version || "unknown"}`,
112
+ mcpClient: args.mcpClient,
113
+ platform: args.platform || process.platform,
114
+ version: args.version,
115
+ dedupeKey: args.dedupeKey,
116
+ props: args.props,
117
+ },
118
+ };
119
+ // Fire and forget.
120
+ try {
121
+ fetch(`${CONVEX_URL}/api/mutation`, {
122
+ method: "POST",
123
+ headers: { "Content-Type": "application/json" },
124
+ body: JSON.stringify(payload),
125
+ }).catch(() => { });
126
+ }
127
+ catch {
128
+ /* ignore */
129
+ }
130
+ }
131
+ //# sourceMappingURL=funnel-client.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"funnel-client.js","sourceRoot":"","sources":["../src/funnel-client.ts"],"names":[],"mappings":"AA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