@nordsym/apiclaw 2.2.0 → 2.3.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 (176) hide show
  1. package/README.md +15 -2
  2. package/dist/bin-http.js +0 -0
  3. package/dist/bin.bundled.js +79288 -0
  4. package/dist/gateway-client.d.ts.map +1 -1
  5. package/dist/gateway-client.js +24 -2
  6. package/dist/gateway-client.js.map +1 -1
  7. package/dist/index.bundled.js +61263 -0
  8. package/dist/index.js +2 -2
  9. package/dist/index.js.map +1 -1
  10. package/package.json +7 -2
  11. package/.claude/settings.local.json +0 -13
  12. package/.env.prod +0 -1
  13. package/apiclaw-README.md +0 -494
  14. package/convex/_generated/api.d.ts +0 -145
  15. package/convex/_generated/api.js +0 -23
  16. package/convex/_generated/dataModel.d.ts +0 -60
  17. package/convex/_generated/server.d.ts +0 -143
  18. package/convex/_generated/server.js +0 -93
  19. package/convex/_listWorkspaces.ts +0 -13
  20. package/convex/adminActivate.ts +0 -53
  21. package/convex/adminStats.ts +0 -306
  22. package/convex/agents.ts +0 -939
  23. package/convex/analytics.ts +0 -187
  24. package/convex/apiKeys.ts +0 -220
  25. package/convex/backfillAnalytics.ts +0 -272
  26. package/convex/backfillSearchLogs.ts +0 -35
  27. package/convex/billing.ts +0 -834
  28. package/convex/capabilities.ts +0 -157
  29. package/convex/chains.ts +0 -1318
  30. package/convex/credits.ts +0 -211
  31. package/convex/crons.ts +0 -65
  32. package/convex/debugFilestackLogs.ts +0 -16
  33. package/convex/debugGetToken.ts +0 -18
  34. package/convex/directCall.ts +0 -713
  35. package/convex/earnProgress.ts +0 -753
  36. package/convex/email.ts +0 -329
  37. package/convex/feedback.ts +0 -265
  38. package/convex/funnel.ts +0 -431
  39. package/convex/guards.ts +0 -174
  40. package/convex/http.ts +0 -3756
  41. package/convex/inbound.ts +0 -32
  42. package/convex/logs.ts +0 -701
  43. package/convex/migrateFilestack.ts +0 -81
  44. package/convex/migratePartnersProd.ts +0 -174
  45. package/convex/migratePratham.ts +0 -126
  46. package/convex/migrateProviderWorkspaces.ts +0 -175
  47. package/convex/mou.ts +0 -91
  48. package/convex/nurture.ts +0 -355
  49. package/convex/providerKeys.ts +0 -289
  50. package/convex/providers.ts +0 -1135
  51. package/convex/purchases.ts +0 -183
  52. package/convex/ratelimit.ts +0 -104
  53. package/convex/schema.ts +0 -926
  54. package/convex/searchLogs.ts +0 -265
  55. package/convex/seedAPILayerAPIs.ts +0 -191
  56. package/convex/seedDirectCallConfigs.ts +0 -336
  57. package/convex/seedPratham.ts +0 -149
  58. package/convex/spendAlerts.ts +0 -442
  59. package/convex/stripeActions.ts +0 -607
  60. package/convex/teams.ts +0 -243
  61. package/convex/telemetry.ts +0 -81
  62. package/convex/tsconfig.json +0 -25
  63. package/convex/updateAPIStatus.ts +0 -44
  64. package/convex/usage.ts +0 -260
  65. package/convex/usageReports.ts +0 -357
  66. package/convex/waitlist.ts +0 -55
  67. package/convex/webhooks.ts +0 -494
  68. package/convex/workspaceSettings.ts +0 -143
  69. package/convex/workspaces.ts +0 -1331
  70. package/convex.json +0 -3
  71. package/direct-test.mjs +0 -51
  72. package/email-templates/filestack-provider-outreach.html +0 -162
  73. package/email-templates/partnership-template.html +0 -116
  74. package/email-templates/pratham-draft-preview.txt +0 -57
  75. package/email-templates/pratham-partnership-draft.html +0 -141
  76. package/reports/APIClaw-Session-Report-2026-04-05.pdf +0 -0
  77. package/reports/pipeline/PIPELINE-REPORT.json +0 -153
  78. package/reports/pipeline/acquire_apisguru.json +0 -17
  79. package/reports/pipeline/capabilities.json +0 -38
  80. package/reports/pipeline/discover_azure_recursive.json +0 -1551
  81. package/reports/pipeline/discover_github.json +0 -25
  82. package/reports/pipeline/discover_github_repos.json +0 -49
  83. package/reports/pipeline/discover_swaggerhub.json +0 -24
  84. package/reports/pipeline/discover_well_known.json +0 -23
  85. package/reports/pipeline/fetch_specs.json +0 -19
  86. package/reports/pipeline/generate_providers.json +0 -14
  87. package/reports/pipeline/match_registry.json +0 -11
  88. package/reports/pipeline/parse_specs.json +0 -17
  89. package/reports/pipeline/promote_candidates.json +0 -34
  90. package/reports/pipeline/validate.json +0 -30
  91. package/reports/pipeline/validate_smoke_details.json +0 -3835
  92. package/reports/session-report-2026-04-05.html +0 -433
  93. package/seed-apis-direct.mjs +0 -106
  94. package/src/access-control.ts +0 -174
  95. package/src/adapters/base.ts +0 -364
  96. package/src/adapters/claude-desktop.ts +0 -41
  97. package/src/adapters/cline.ts +0 -88
  98. package/src/adapters/continue.ts +0 -91
  99. package/src/adapters/cursor.ts +0 -43
  100. package/src/adapters/custom.ts +0 -188
  101. package/src/adapters/detect.ts +0 -202
  102. package/src/adapters/index.ts +0 -47
  103. package/src/adapters/windsurf.ts +0 -44
  104. package/src/bin-http.ts +0 -45
  105. package/src/bin.ts +0 -34
  106. package/src/capability-router.ts +0 -331
  107. package/src/chainExecutor.ts +0 -730
  108. package/src/chainResolver.test.ts +0 -246
  109. package/src/chainResolver.ts +0 -658
  110. package/src/cli/commands/demo.ts +0 -109
  111. package/src/cli/commands/doctor.ts +0 -435
  112. package/src/cli/commands/index.ts +0 -9
  113. package/src/cli/commands/login.ts +0 -203
  114. package/src/cli/commands/mcp-install.ts +0 -373
  115. package/src/cli/commands/restore.ts +0 -333
  116. package/src/cli/commands/setup.ts +0 -297
  117. package/src/cli/commands/uninstall.ts +0 -240
  118. package/src/cli/index.ts +0 -148
  119. package/src/cli.ts +0 -370
  120. package/src/confirmation.ts +0 -296
  121. package/src/credentials.ts +0 -455
  122. package/src/credits.ts +0 -329
  123. package/src/crypto.ts +0 -75
  124. package/src/discovery.ts +0 -568
  125. package/src/enterprise/env.ts +0 -156
  126. package/src/enterprise/index.ts +0 -7
  127. package/src/enterprise/script-generator.ts +0 -481
  128. package/src/execute-dynamic.ts +0 -617
  129. package/src/execute.ts +0 -2386
  130. package/src/funnel-client.ts +0 -168
  131. package/src/funnel.test.ts +0 -187
  132. package/src/gateway-client.ts +0 -192
  133. package/src/hivr-whitelist.ts +0 -110
  134. package/src/http-api.ts +0 -286
  135. package/src/http-server-minimal.ts +0 -154
  136. package/src/index.ts +0 -2702
  137. package/src/intelligent-gateway.ts +0 -339
  138. package/src/mcp-analytics.ts +0 -156
  139. package/src/metered.ts +0 -149
  140. package/src/open-apis-generated.ts +0 -157
  141. package/src/open-apis.ts +0 -558
  142. package/src/postinstall.ts +0 -40
  143. package/src/product-whitelist.ts +0 -246
  144. package/src/proxy.ts +0 -36
  145. package/src/registration-guard.ts +0 -117
  146. package/src/session.ts +0 -129
  147. package/src/stripe.ts +0 -497
  148. package/src/telemetry.ts +0 -71
  149. package/src/test.ts +0 -135
  150. package/src/types/convex-api.d.ts +0 -20
  151. package/src/types/convex-api.ts +0 -21
  152. package/src/types.ts +0 -109
  153. package/src/ui/colors.ts +0 -219
  154. package/src/ui/errors.ts +0 -394
  155. package/src/ui/index.ts +0 -17
  156. package/src/ui/prompts.ts +0 -390
  157. package/src/ui/spinner.ts +0 -325
  158. package/src/utils/backup.ts +0 -224
  159. package/src/utils/config.ts +0 -318
  160. package/src/utils/os.ts +0 -124
  161. package/src/utils/paths.ts +0 -203
  162. package/src/webhook.ts +0 -107
  163. package/test-10-working.cjs +0 -97
  164. package/test-14-final.cjs +0 -96
  165. package/test-actual-handlers.ts +0 -92
  166. package/test-apilayer-all-14.ts +0 -249
  167. package/test-apilayer-fixed.ts +0 -248
  168. package/test-direct-endpoints.ts +0 -174
  169. package/test-exact-endpoints.ts +0 -144
  170. package/test-final.ts +0 -83
  171. package/test-full-routing.ts +0 -100
  172. package/test-handlers-correct.ts +0 -217
  173. package/test-numverify-key.ts +0 -41
  174. package/test-via-handlers.ts +0 -92
  175. package/test-worldnews.mjs +0 -26
  176. package/tsconfig.json +0 -20
package/convex/nurture.ts DELETED
@@ -1,355 +0,0 @@
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
- });
@@ -1,289 +0,0 @@
1
- import { v } from "convex/values";
2
- import { mutation, query, internalQuery } from "./_generated/server";
3
-
4
- // ============================================
5
- // BYOK - Bring Your Own Key
6
- // ============================================
7
-
8
- // Supported providers for BYOK
9
- export const BYOK_PROVIDERS = [
10
- { id: "brave_search", name: "Brave Search", icon: "🔍" },
11
- { id: "openrouter", name: "OpenRouter", icon: "🤖" },
12
- { id: "elevenlabs", name: "ElevenLabs", icon: "🎙️" },
13
- { id: "twilio", name: "Twilio", icon: "📞" },
14
- { id: "resend", name: "Resend", icon: "📧" },
15
- { id: "e2b", name: "E2B", icon: "💻" },
16
- ] as const;
17
-
18
- // Simple base64 encoding for MVP (proper encryption in production)
19
- function encryptKey(key: string): string {
20
- return Buffer.from(key).toString("base64");
21
- }
22
-
23
- function decryptKey(encryptedKey: string): string {
24
- return Buffer.from(encryptedKey, "base64").toString("utf-8");
25
- }
26
-
27
- function getKeyHint(key: string): string {
28
- if (key.length <= 4) return "••••";
29
- return key.slice(-4);
30
- }
31
-
32
- // ============================================
33
- // ADD KEY
34
- // ============================================
35
-
36
- export const addKey = mutation({
37
- args: {
38
- token: v.string(),
39
- provider: v.string(),
40
- apiKey: v.string(),
41
- },
42
- handler: async (ctx, args) => {
43
- // Validate session
44
- const session = await ctx.db
45
- .query("agentSessions")
46
- .withIndex("by_sessionToken", (q) => q.eq("sessionToken", args.token))
47
- .first();
48
-
49
- if (!session) {
50
- throw new Error("Invalid session");
51
- }
52
-
53
- const workspaceId = session.workspaceId;
54
-
55
- // Check if key already exists for this provider
56
- const existingKey = await ctx.db
57
- .query("providerKeys")
58
- .withIndex("by_provider", (q) =>
59
- q.eq("workspaceId", workspaceId).eq("provider", args.provider)
60
- )
61
- .first();
62
-
63
- const now = Date.now();
64
- const encryptedKey = encryptKey(args.apiKey);
65
- const keyHint = getKeyHint(args.apiKey);
66
-
67
- let isFirstKey = false;
68
-
69
- if (existingKey) {
70
- // Update existing key
71
- await ctx.db.patch(existingKey._id, {
72
- encryptedKey,
73
- keyHint,
74
- updatedAt: now,
75
- });
76
- } else {
77
- // Create new key
78
- await ctx.db.insert("providerKeys", {
79
- workspaceId,
80
- provider: args.provider,
81
- encryptedKey,
82
- keyHint,
83
- isCustom: false,
84
- createdAt: now,
85
- updatedAt: now,
86
- });
87
-
88
- // Check if this is the first BYOK key for earn progress
89
- const allKeys = await ctx.db
90
- .query("providerKeys")
91
- .withIndex("by_workspaceId", (q) => q.eq("workspaceId", workspaceId))
92
- .collect();
93
-
94
- // If this is the only key (the one we just created), mark BYOK setup
95
- if (allKeys.length === 1) {
96
- isFirstKey = true;
97
- // Import and call markByokSetup
98
- const earnProgress = await ctx.db
99
- .query("earnProgress")
100
- .withIndex("by_workspaceId", (q) => q.eq("workspaceId", workspaceId))
101
- .first();
102
-
103
- if (earnProgress && !earnProgress.byokSetup) {
104
- const newTotal = calculateEarnTotal({ ...earnProgress, byokSetup: true });
105
- await ctx.db.patch(earnProgress._id, {
106
- byokSetup: true,
107
- byokSetupAt: now,
108
- totalEarned: newTotal,
109
- updatedAt: now,
110
- });
111
- // Add 5 calls to workspace limit
112
- const workspace = await ctx.db.get(workspaceId);
113
- if (workspace) {
114
- await ctx.db.patch(workspaceId, {
115
- usageLimit: workspace.usageLimit + 5,
116
- updatedAt: now,
117
- });
118
- }
119
- } else if (!earnProgress) {
120
- // Create earn progress with byokSetup
121
- await ctx.db.insert("earnProgress", {
122
- workspaceId,
123
- firstDirectCall: false,
124
- apisUsed: [],
125
- apisUsedComplete: false,
126
- agentListed: false,
127
- apiListed: false,
128
- byokSetup: true,
129
- byokSetupAt: now,
130
- githubStarred: false,
131
- twitterFollowed: false,
132
- referralCount: 0,
133
- totalEarned: 5, // BYOK reward
134
- createdAt: now,
135
- updatedAt: now,
136
- });
137
- // Add 5 calls to workspace limit
138
- const workspace = await ctx.db.get(workspaceId);
139
- if (workspace) {
140
- await ctx.db.patch(workspaceId, {
141
- usageLimit: workspace.usageLimit + 5,
142
- updatedAt: now,
143
- });
144
- }
145
- }
146
- }
147
- }
148
-
149
- return {
150
- success: true,
151
- action: existingKey ? "updated" : "created",
152
- earnedByok: isFirstKey,
153
- };
154
- },
155
- });
156
-
157
- // Helper to calculate earn total (duplicated to avoid circular import)
158
- function calculateEarnTotal(progress: any): number {
159
- let total = 0;
160
- if (progress.firstDirectCall) total += 15;
161
- if (progress.apisUsedComplete) total += 10;
162
- if (progress.agentListed) total += 10;
163
- if (progress.apiListed) total += 10;
164
- if (progress.byokSetup) total += 5;
165
- if (progress.githubStarred) total += 10;
166
- if (progress.twitterFollowed) total += 5;
167
- total += (progress.referralCount || 0) * 10;
168
- return total;
169
- }
170
-
171
- // ============================================
172
- // REMOVE KEY
173
- // ============================================
174
-
175
- export const removeKey = mutation({
176
- args: {
177
- token: v.string(),
178
- provider: v.string(),
179
- },
180
- handler: async (ctx, args) => {
181
- // Validate session
182
- const session = await ctx.db
183
- .query("agentSessions")
184
- .withIndex("by_sessionToken", (q) => q.eq("sessionToken", args.token))
185
- .first();
186
-
187
- if (!session) {
188
- throw new Error("Invalid session");
189
- }
190
-
191
- const workspaceId = session.workspaceId;
192
-
193
- // Find and delete the key
194
- const existingKey = await ctx.db
195
- .query("providerKeys")
196
- .withIndex("by_provider", (q) =>
197
- q.eq("workspaceId", workspaceId).eq("provider", args.provider)
198
- )
199
- .first();
200
-
201
- if (!existingKey) {
202
- throw new Error("Key not found");
203
- }
204
-
205
- await ctx.db.delete(existingKey._id);
206
- return { success: true };
207
- },
208
- });
209
-
210
- // ============================================
211
- // GET KEYS (for display - no actual key values)
212
- // ============================================
213
-
214
- export const getKeys = query({
215
- args: {
216
- token: v.string(),
217
- },
218
- handler: async (ctx, args) => {
219
- // Validate session
220
- const session = await ctx.db
221
- .query("agentSessions")
222
- .withIndex("by_sessionToken", (q) => q.eq("sessionToken", args.token))
223
- .first();
224
-
225
- if (!session) {
226
- return { keys: [] };
227
- }
228
-
229
- const workspaceId = session.workspaceId;
230
-
231
- // Get all keys for this workspace
232
- const keys = await ctx.db
233
- .query("providerKeys")
234
- .withIndex("by_workspaceId", (q) => q.eq("workspaceId", workspaceId))
235
- .collect();
236
-
237
- // Return without actual key values
238
- return {
239
- keys: keys.map((key) => ({
240
- provider: key.provider,
241
- keyHint: key.keyHint,
242
- isCustom: key.isCustom,
243
- customConfig: key.customConfig,
244
- createdAt: key.createdAt,
245
- updatedAt: key.updatedAt,
246
- })),
247
- };
248
- },
249
- });
250
-
251
- // ============================================
252
- // GET KEY FOR EXECUTION (internal use only)
253
- // ============================================
254
-
255
- export const getKeyForExecution = internalQuery({
256
- args: {
257
- workspaceId: v.id("workspaces"),
258
- provider: v.string(),
259
- },
260
- handler: async (ctx, args) => {
261
- const key = await ctx.db
262
- .query("providerKeys")
263
- .withIndex("by_provider", (q) =>
264
- q.eq("workspaceId", args.workspaceId).eq("provider", args.provider)
265
- )
266
- .first();
267
-
268
- if (!key) {
269
- return null;
270
- }
271
-
272
- return {
273
- apiKey: decryptKey(key.encryptedKey),
274
- isCustom: key.isCustom,
275
- customConfig: key.customConfig,
276
- };
277
- },
278
- });
279
-
280
- // ============================================
281
- // GET SUPPORTED PROVIDERS
282
- // ============================================
283
-
284
- export const getSupportedProviders = query({
285
- args: {},
286
- handler: async () => {
287
- return BYOK_PROVIDERS;
288
- },
289
- });