@nordsym/apiclaw 1.7.3 → 1.7.4

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 (216) hide show
  1. package/convex/_generated/api.d.ts +115 -0
  2. package/convex/_generated/api.js +23 -0
  3. package/convex/_generated/dataModel.d.ts +60 -0
  4. package/convex/_generated/server.d.ts +143 -0
  5. package/convex/_generated/server.js +93 -0
  6. package/convex/adminActivate.d.ts +3 -0
  7. package/convex/adminActivate.d.ts.map +1 -0
  8. package/convex/adminActivate.js +47 -0
  9. package/convex/adminActivate.js.map +1 -0
  10. package/convex/adminActivate.ts +54 -0
  11. package/convex/adminStats.d.ts +3 -0
  12. package/convex/adminStats.d.ts.map +1 -0
  13. package/convex/adminStats.js +42 -0
  14. package/convex/adminStats.js.map +1 -0
  15. package/convex/adminStats.ts +44 -0
  16. package/convex/agents.d.ts +76 -0
  17. package/convex/agents.d.ts.map +1 -0
  18. package/convex/agents.js +699 -0
  19. package/convex/agents.js.map +1 -0
  20. package/convex/agents.ts +814 -0
  21. package/convex/analytics.d.ts +5 -0
  22. package/convex/analytics.d.ts.map +1 -0
  23. package/convex/analytics.js +166 -0
  24. package/convex/analytics.js.map +1 -0
  25. package/convex/analytics.ts +186 -0
  26. package/convex/billing.d.ts +88 -0
  27. package/convex/billing.d.ts.map +1 -0
  28. package/convex/billing.js +655 -0
  29. package/convex/billing.js.map +1 -0
  30. package/convex/billing.ts +791 -0
  31. package/convex/capabilities.d.ts +9 -0
  32. package/convex/capabilities.d.ts.map +1 -0
  33. package/convex/capabilities.js +145 -0
  34. package/convex/capabilities.js.map +1 -0
  35. package/convex/capabilities.ts +157 -0
  36. package/convex/chains.d.ts +68 -0
  37. package/convex/chains.d.ts.map +1 -0
  38. package/convex/chains.js +1105 -0
  39. package/convex/chains.js.map +1 -0
  40. package/convex/chains.ts +1318 -0
  41. package/convex/credits.d.ts +25 -0
  42. package/convex/credits.d.ts.map +1 -0
  43. package/convex/credits.js +186 -0
  44. package/convex/credits.js.map +1 -0
  45. package/convex/credits.ts +211 -0
  46. package/convex/crons.d.ts +3 -0
  47. package/convex/crons.d.ts.map +1 -0
  48. package/convex/crons.js +17 -0
  49. package/convex/crons.js.map +1 -0
  50. package/convex/crons.ts +28 -0
  51. package/convex/directCall.d.ts +72 -0
  52. package/convex/directCall.d.ts.map +1 -0
  53. package/convex/directCall.js +627 -0
  54. package/convex/directCall.js.map +1 -0
  55. package/convex/directCall.ts +678 -0
  56. package/convex/earnProgress.d.ts +58 -0
  57. package/convex/earnProgress.d.ts.map +1 -0
  58. package/convex/earnProgress.js +649 -0
  59. package/convex/earnProgress.js.map +1 -0
  60. package/convex/earnProgress.ts +753 -0
  61. package/convex/email.d.ts +14 -0
  62. package/convex/email.d.ts.map +1 -0
  63. package/convex/email.js +300 -0
  64. package/convex/email.js.map +1 -0
  65. package/convex/email.ts +329 -0
  66. package/convex/feedback.d.ts +7 -0
  67. package/convex/feedback.d.ts.map +1 -0
  68. package/convex/feedback.js +227 -0
  69. package/convex/feedback.js.map +1 -0
  70. package/convex/feedback.ts +265 -0
  71. package/convex/http.d.ts +3 -0
  72. package/convex/http.d.ts.map +1 -0
  73. package/convex/http.js +1405 -0
  74. package/convex/http.js.map +1 -0
  75. package/convex/http.ts +1577 -0
  76. package/convex/inbound.d.ts +2 -0
  77. package/convex/inbound.d.ts.map +1 -0
  78. package/convex/inbound.js +32 -0
  79. package/convex/inbound.js.map +1 -0
  80. package/convex/inbound.ts +32 -0
  81. package/convex/logs.d.ts +38 -0
  82. package/convex/logs.d.ts.map +1 -0
  83. package/convex/logs.js +487 -0
  84. package/convex/logs.js.map +1 -0
  85. package/convex/logs.ts +550 -0
  86. package/convex/mou.d.ts +6 -0
  87. package/convex/mou.d.ts.map +1 -0
  88. package/convex/mou.js +82 -0
  89. package/convex/mou.js.map +1 -0
  90. package/convex/mou.ts +91 -0
  91. package/convex/providerKeys.d.ts +31 -0
  92. package/convex/providerKeys.d.ts.map +1 -0
  93. package/convex/providerKeys.js +257 -0
  94. package/convex/providerKeys.js.map +1 -0
  95. package/convex/providerKeys.ts +289 -0
  96. package/convex/providers.d.ts +32 -0
  97. package/convex/providers.d.ts.map +1 -0
  98. package/convex/providers.js +814 -0
  99. package/convex/providers.js.map +1 -0
  100. package/convex/providers.ts +909 -0
  101. package/convex/purchases.d.ts +7 -0
  102. package/convex/purchases.d.ts.map +1 -0
  103. package/convex/purchases.js +157 -0
  104. package/convex/purchases.js.map +1 -0
  105. package/convex/purchases.ts +183 -0
  106. package/convex/ratelimit.d.ts +4 -0
  107. package/convex/ratelimit.d.ts.map +1 -0
  108. package/convex/ratelimit.js +91 -0
  109. package/convex/ratelimit.js.map +1 -0
  110. package/convex/ratelimit.ts +104 -0
  111. package/convex/schema.ts +802 -0
  112. package/convex/searchLogs.d.ts +4 -0
  113. package/convex/searchLogs.d.ts.map +1 -0
  114. package/convex/searchLogs.js +129 -0
  115. package/convex/searchLogs.js.map +1 -0
  116. package/convex/searchLogs.ts +146 -0
  117. package/convex/seedAPILayerAPIs.d.ts +7 -0
  118. package/convex/seedAPILayerAPIs.d.ts.map +1 -0
  119. package/convex/seedAPILayerAPIs.js +177 -0
  120. package/convex/seedAPILayerAPIs.js.map +1 -0
  121. package/convex/seedAPILayerAPIs.ts +191 -0
  122. package/convex/seedDirectCallConfigs.d.ts +2 -0
  123. package/convex/seedDirectCallConfigs.d.ts.map +1 -0
  124. package/convex/seedDirectCallConfigs.js +324 -0
  125. package/convex/seedDirectCallConfigs.js.map +1 -0
  126. package/convex/seedDirectCallConfigs.ts +336 -0
  127. package/convex/seedPratham.d.ts +6 -0
  128. package/convex/seedPratham.d.ts.map +1 -0
  129. package/convex/seedPratham.js +150 -0
  130. package/convex/seedPratham.js.map +1 -0
  131. package/convex/seedPratham.ts +161 -0
  132. package/convex/spendAlerts.d.ts +36 -0
  133. package/convex/spendAlerts.d.ts.map +1 -0
  134. package/convex/spendAlerts.js +380 -0
  135. package/convex/spendAlerts.js.map +1 -0
  136. package/convex/spendAlerts.ts +442 -0
  137. package/convex/stripeActions.d.ts +19 -0
  138. package/convex/stripeActions.d.ts.map +1 -0
  139. package/convex/stripeActions.js +411 -0
  140. package/convex/stripeActions.js.map +1 -0
  141. package/convex/stripeActions.ts +512 -0
  142. package/convex/teams.d.ts +21 -0
  143. package/convex/teams.d.ts.map +1 -0
  144. package/convex/teams.js +215 -0
  145. package/convex/teams.js.map +1 -0
  146. package/convex/teams.ts +243 -0
  147. package/convex/telemetry.d.ts +4 -0
  148. package/convex/telemetry.d.ts.map +1 -0
  149. package/convex/telemetry.js +74 -0
  150. package/convex/telemetry.js.map +1 -0
  151. package/convex/telemetry.ts +81 -0
  152. package/convex/tsconfig.json +25 -0
  153. package/convex/updateAPIStatus.d.ts +6 -0
  154. package/convex/updateAPIStatus.d.ts.map +1 -0
  155. package/convex/updateAPIStatus.js +40 -0
  156. package/convex/updateAPIStatus.js.map +1 -0
  157. package/convex/updateAPIStatus.ts +45 -0
  158. package/convex/usage.d.ts +27 -0
  159. package/convex/usage.d.ts.map +1 -0
  160. package/convex/usage.js +229 -0
  161. package/convex/usage.js.map +1 -0
  162. package/convex/usage.ts +260 -0
  163. package/convex/waitlist.d.ts +4 -0
  164. package/convex/waitlist.d.ts.map +1 -0
  165. package/convex/waitlist.js +49 -0
  166. package/convex/waitlist.js.map +1 -0
  167. package/convex/waitlist.ts +55 -0
  168. package/convex/webhooks.d.ts +12 -0
  169. package/convex/webhooks.d.ts.map +1 -0
  170. package/convex/webhooks.js +410 -0
  171. package/convex/webhooks.js.map +1 -0
  172. package/convex/webhooks.ts +494 -0
  173. package/convex/workspaces.d.ts +31 -0
  174. package/convex/workspaces.d.ts.map +1 -0
  175. package/convex/workspaces.js +975 -0
  176. package/convex/workspaces.js.map +1 -0
  177. package/convex/workspaces.ts +1130 -0
  178. package/dist/bin.js +0 -0
  179. package/package.json +1 -1
  180. package/dist/chain-types.d.ts +0 -187
  181. package/dist/chain-types.d.ts.map +0 -1
  182. package/dist/chain-types.js +0 -33
  183. package/dist/chain-types.js.map +0 -1
  184. package/dist/registry/apis.json.bak +0 -248811
  185. package/dist/src/bin.js +0 -17
  186. package/dist/src/capability-router.js +0 -240
  187. package/dist/src/chainExecutor.js +0 -451
  188. package/dist/src/chainResolver.js +0 -518
  189. package/dist/src/cli/commands/doctor.js +0 -324
  190. package/dist/src/cli/commands/mcp-install.js +0 -255
  191. package/dist/src/cli/commands/restore.js +0 -259
  192. package/dist/src/cli/commands/setup.js +0 -205
  193. package/dist/src/cli/commands/uninstall.js +0 -188
  194. package/dist/src/cli/index.js +0 -111
  195. package/dist/src/cli.js +0 -302
  196. package/dist/src/confirmation.js +0 -240
  197. package/dist/src/credentials.js +0 -357
  198. package/dist/src/credits.js +0 -260
  199. package/dist/src/crypto.js +0 -66
  200. package/dist/src/discovery.js +0 -504
  201. package/dist/src/enterprise/env.js +0 -123
  202. package/dist/src/enterprise/script-generator.js +0 -460
  203. package/dist/src/execute-dynamic.js +0 -473
  204. package/dist/src/execute.js +0 -1727
  205. package/dist/src/index.js +0 -2062
  206. package/dist/src/metered.js +0 -80
  207. package/dist/src/open-apis.js +0 -276
  208. package/dist/src/proxy.js +0 -28
  209. package/dist/src/session.js +0 -86
  210. package/dist/src/stripe.js +0 -407
  211. package/dist/src/telemetry.js +0 -49
  212. package/dist/src/types.js +0 -2
  213. package/dist/src/utils/backup.js +0 -181
  214. package/dist/src/utils/config.js +0 -220
  215. package/dist/src/utils/os.js +0 -105
  216. package/dist/src/utils/paths.js +0 -159
package/convex/mou.ts ADDED
@@ -0,0 +1,91 @@
1
+ import { v } from "convex/values";
2
+ import { query, mutation } from "./_generated/server";
3
+
4
+ // Get MOU by partnerId
5
+ export const getByPartnerId = query({
6
+ args: { partnerId: v.string() },
7
+ handler: async (ctx, args) => {
8
+ return await ctx.db
9
+ .query("mouDocuments")
10
+ .withIndex("by_partnerId", (q) => q.eq("partnerId", args.partnerId))
11
+ .first();
12
+ },
13
+ });
14
+
15
+ // Create new MOU document
16
+ export const create = mutation({
17
+ args: {
18
+ partnerId: v.string(),
19
+ partnerName: v.string(),
20
+ partnerEmail: v.string(),
21
+ documentHtml: v.string(),
22
+ },
23
+ handler: async (ctx, args) => {
24
+ return await ctx.db.insert("mouDocuments", {
25
+ ...args,
26
+ status: "pending",
27
+ createdAt: Date.now(),
28
+ });
29
+ },
30
+ });
31
+
32
+ // Sign MOU
33
+ export const sign = mutation({
34
+ args: {
35
+ partnerId: v.string(),
36
+ signatureDataUrl: v.string(),
37
+ signerName: v.string(),
38
+ signerTitle: v.string(),
39
+ signerIp: v.optional(v.string()),
40
+ },
41
+ handler: async (ctx, args) => {
42
+ const mou = await ctx.db
43
+ .query("mouDocuments")
44
+ .withIndex("by_partnerId", (q) => q.eq("partnerId", args.partnerId))
45
+ .first();
46
+
47
+ if (!mou) {
48
+ throw new Error("MOU not found");
49
+ }
50
+
51
+ if (mou.status === "signed") {
52
+ throw new Error("MOU already signed");
53
+ }
54
+
55
+ await ctx.db.patch(mou._id, {
56
+ status: "signed",
57
+ signedAt: Date.now(),
58
+ signatureDataUrl: args.signatureDataUrl,
59
+ signerName: args.signerName,
60
+ signerTitle: args.signerTitle,
61
+ signerIp: args.signerIp,
62
+ });
63
+
64
+ return { success: true };
65
+ },
66
+ });
67
+
68
+ // List all MOUs (admin)
69
+ export const list = query({
70
+ args: {},
71
+ handler: async (ctx) => {
72
+ return await ctx.db.query("mouDocuments").collect();
73
+ },
74
+ });
75
+
76
+ // Delete MOU (admin)
77
+ export const remove = mutation({
78
+ args: { partnerId: v.string() },
79
+ handler: async (ctx, args) => {
80
+ const mou = await ctx.db
81
+ .query("mouDocuments")
82
+ .withIndex("by_partnerId", (q) => q.eq("partnerId", args.partnerId))
83
+ .first();
84
+
85
+ if (mou) {
86
+ await ctx.db.delete(mou._id);
87
+ return { success: true, deleted: args.partnerId };
88
+ }
89
+ return { success: false, message: "MOU not found" };
90
+ },
91
+ });
@@ -0,0 +1,31 @@
1
+ export declare const BYOK_PROVIDERS: readonly [{
2
+ readonly id: "brave_search";
3
+ readonly name: "Brave Search";
4
+ readonly icon: "🔍";
5
+ }, {
6
+ readonly id: "openrouter";
7
+ readonly name: "OpenRouter";
8
+ readonly icon: "🤖";
9
+ }, {
10
+ readonly id: "elevenlabs";
11
+ readonly name: "ElevenLabs";
12
+ readonly icon: "🎙️";
13
+ }, {
14
+ readonly id: "twilio";
15
+ readonly name: "Twilio";
16
+ readonly icon: "📞";
17
+ }, {
18
+ readonly id: "resend";
19
+ readonly name: "Resend";
20
+ readonly icon: "📧";
21
+ }, {
22
+ readonly id: "e2b";
23
+ readonly name: "E2B";
24
+ readonly icon: "💻";
25
+ }];
26
+ export declare const addKey: any;
27
+ export declare const removeKey: any;
28
+ export declare const getKeys: any;
29
+ export declare const getKeyForExecution: any;
30
+ export declare const getSupportedProviders: any;
31
+ //# sourceMappingURL=providerKeys.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"providerKeys.d.ts","sourceRoot":"","sources":["providerKeys.ts"],"names":[],"mappings":"AAQA,eAAO,MAAM,cAAc;;;;;;;;;;;;;;;;;;;;;;;;EAOjB,CAAC;AAoBX,eAAO,MAAM,MAAM,KAuHjB,CAAC;AAoBH,eAAO,MAAM,SAAS,KAiCpB,CAAC;AAMH,eAAO,MAAM,OAAO,KAmClB,CAAC;AAMH,eAAO,MAAM,kBAAkB,KAuB7B,CAAC;AAMH,eAAO,MAAM,qBAAqB,KAKhC,CAAC"}
@@ -0,0 +1,257 @@
1
+ import { v } from "convex/values";
2
+ import { mutation, query, internalQuery } from "./_generated/server";
3
+ // ============================================
4
+ // BYOK - Bring Your Own Key
5
+ // ============================================
6
+ // Supported providers for BYOK
7
+ export const BYOK_PROVIDERS = [
8
+ { id: "brave_search", name: "Brave Search", icon: "🔍" },
9
+ { id: "openrouter", name: "OpenRouter", icon: "🤖" },
10
+ { id: "elevenlabs", name: "ElevenLabs", icon: "🎙️" },
11
+ { id: "twilio", name: "Twilio", icon: "📞" },
12
+ { id: "resend", name: "Resend", icon: "📧" },
13
+ { id: "e2b", name: "E2B", icon: "💻" },
14
+ ];
15
+ // Simple base64 encoding for MVP (proper encryption in production)
16
+ function encryptKey(key) {
17
+ return Buffer.from(key).toString("base64");
18
+ }
19
+ function decryptKey(encryptedKey) {
20
+ return Buffer.from(encryptedKey, "base64").toString("utf-8");
21
+ }
22
+ function getKeyHint(key) {
23
+ if (key.length <= 4)
24
+ return "••••";
25
+ return key.slice(-4);
26
+ }
27
+ // ============================================
28
+ // ADD KEY
29
+ // ============================================
30
+ export const addKey = mutation({
31
+ args: {
32
+ token: v.string(),
33
+ provider: v.string(),
34
+ apiKey: v.string(),
35
+ },
36
+ handler: async (ctx, args) => {
37
+ // Validate session
38
+ const session = await ctx.db
39
+ .query("agentSessions")
40
+ .withIndex("by_sessionToken", (q) => q.eq("sessionToken", args.token))
41
+ .first();
42
+ if (!session) {
43
+ throw new Error("Invalid session");
44
+ }
45
+ const workspaceId = session.workspaceId;
46
+ // Check if key already exists for this provider
47
+ const existingKey = await ctx.db
48
+ .query("providerKeys")
49
+ .withIndex("by_provider", (q) => q.eq("workspaceId", workspaceId).eq("provider", args.provider))
50
+ .first();
51
+ const now = Date.now();
52
+ const encryptedKey = encryptKey(args.apiKey);
53
+ const keyHint = getKeyHint(args.apiKey);
54
+ let isFirstKey = false;
55
+ if (existingKey) {
56
+ // Update existing key
57
+ await ctx.db.patch(existingKey._id, {
58
+ encryptedKey,
59
+ keyHint,
60
+ updatedAt: now,
61
+ });
62
+ }
63
+ else {
64
+ // Create new key
65
+ await ctx.db.insert("providerKeys", {
66
+ workspaceId,
67
+ provider: args.provider,
68
+ encryptedKey,
69
+ keyHint,
70
+ isCustom: false,
71
+ createdAt: now,
72
+ updatedAt: now,
73
+ });
74
+ // Check if this is the first BYOK key for earn progress
75
+ const allKeys = await ctx.db
76
+ .query("providerKeys")
77
+ .withIndex("by_workspaceId", (q) => q.eq("workspaceId", workspaceId))
78
+ .collect();
79
+ // If this is the only key (the one we just created), mark BYOK setup
80
+ if (allKeys.length === 1) {
81
+ isFirstKey = true;
82
+ // Import and call markByokSetup
83
+ const earnProgress = await ctx.db
84
+ .query("earnProgress")
85
+ .withIndex("by_workspaceId", (q) => q.eq("workspaceId", workspaceId))
86
+ .first();
87
+ if (earnProgress && !earnProgress.byokSetup) {
88
+ const newTotal = calculateEarnTotal({ ...earnProgress, byokSetup: true });
89
+ await ctx.db.patch(earnProgress._id, {
90
+ byokSetup: true,
91
+ byokSetupAt: now,
92
+ totalEarned: newTotal,
93
+ updatedAt: now,
94
+ });
95
+ // Add 5 calls to workspace limit
96
+ const workspace = await ctx.db.get(workspaceId);
97
+ if (workspace) {
98
+ await ctx.db.patch(workspaceId, {
99
+ usageLimit: workspace.usageLimit + 5,
100
+ updatedAt: now,
101
+ });
102
+ }
103
+ }
104
+ else if (!earnProgress) {
105
+ // Create earn progress with byokSetup
106
+ await ctx.db.insert("earnProgress", {
107
+ workspaceId,
108
+ firstDirectCall: false,
109
+ apisUsed: [],
110
+ apisUsedComplete: false,
111
+ agentListed: false,
112
+ apiListed: false,
113
+ byokSetup: true,
114
+ byokSetupAt: now,
115
+ githubStarred: false,
116
+ twitterFollowed: false,
117
+ referralCount: 0,
118
+ totalEarned: 5, // BYOK reward
119
+ createdAt: now,
120
+ updatedAt: now,
121
+ });
122
+ // Add 5 calls to workspace limit
123
+ const workspace = await ctx.db.get(workspaceId);
124
+ if (workspace) {
125
+ await ctx.db.patch(workspaceId, {
126
+ usageLimit: workspace.usageLimit + 5,
127
+ updatedAt: now,
128
+ });
129
+ }
130
+ }
131
+ }
132
+ }
133
+ return {
134
+ success: true,
135
+ action: existingKey ? "updated" : "created",
136
+ earnedByok: isFirstKey,
137
+ };
138
+ },
139
+ });
140
+ // Helper to calculate earn total (duplicated to avoid circular import)
141
+ function calculateEarnTotal(progress) {
142
+ let total = 0;
143
+ if (progress.firstDirectCall)
144
+ total += 15;
145
+ if (progress.apisUsedComplete)
146
+ total += 10;
147
+ if (progress.agentListed)
148
+ total += 10;
149
+ if (progress.apiListed)
150
+ total += 10;
151
+ if (progress.byokSetup)
152
+ total += 5;
153
+ if (progress.githubStarred)
154
+ total += 10;
155
+ if (progress.twitterFollowed)
156
+ total += 5;
157
+ total += (progress.referralCount || 0) * 10;
158
+ return total;
159
+ }
160
+ // ============================================
161
+ // REMOVE KEY
162
+ // ============================================
163
+ export const removeKey = mutation({
164
+ args: {
165
+ token: v.string(),
166
+ provider: v.string(),
167
+ },
168
+ handler: async (ctx, args) => {
169
+ // Validate session
170
+ const session = await ctx.db
171
+ .query("agentSessions")
172
+ .withIndex("by_sessionToken", (q) => q.eq("sessionToken", args.token))
173
+ .first();
174
+ if (!session) {
175
+ throw new Error("Invalid session");
176
+ }
177
+ const workspaceId = session.workspaceId;
178
+ // Find and delete the key
179
+ const existingKey = await ctx.db
180
+ .query("providerKeys")
181
+ .withIndex("by_provider", (q) => q.eq("workspaceId", workspaceId).eq("provider", args.provider))
182
+ .first();
183
+ if (!existingKey) {
184
+ throw new Error("Key not found");
185
+ }
186
+ await ctx.db.delete(existingKey._id);
187
+ return { success: true };
188
+ },
189
+ });
190
+ // ============================================
191
+ // GET KEYS (for display - no actual key values)
192
+ // ============================================
193
+ export const getKeys = query({
194
+ args: {
195
+ token: v.string(),
196
+ },
197
+ handler: async (ctx, args) => {
198
+ // Validate session
199
+ const session = await ctx.db
200
+ .query("agentSessions")
201
+ .withIndex("by_sessionToken", (q) => q.eq("sessionToken", args.token))
202
+ .first();
203
+ if (!session) {
204
+ return { keys: [] };
205
+ }
206
+ const workspaceId = session.workspaceId;
207
+ // Get all keys for this workspace
208
+ const keys = await ctx.db
209
+ .query("providerKeys")
210
+ .withIndex("by_workspaceId", (q) => q.eq("workspaceId", workspaceId))
211
+ .collect();
212
+ // Return without actual key values
213
+ return {
214
+ keys: keys.map((key) => ({
215
+ provider: key.provider,
216
+ keyHint: key.keyHint,
217
+ isCustom: key.isCustom,
218
+ customConfig: key.customConfig,
219
+ createdAt: key.createdAt,
220
+ updatedAt: key.updatedAt,
221
+ })),
222
+ };
223
+ },
224
+ });
225
+ // ============================================
226
+ // GET KEY FOR EXECUTION (internal use only)
227
+ // ============================================
228
+ export const getKeyForExecution = internalQuery({
229
+ args: {
230
+ workspaceId: v.id("workspaces"),
231
+ provider: v.string(),
232
+ },
233
+ handler: async (ctx, args) => {
234
+ const key = await ctx.db
235
+ .query("providerKeys")
236
+ .withIndex("by_provider", (q) => q.eq("workspaceId", args.workspaceId).eq("provider", args.provider))
237
+ .first();
238
+ if (!key) {
239
+ return null;
240
+ }
241
+ return {
242
+ apiKey: decryptKey(key.encryptedKey),
243
+ isCustom: key.isCustom,
244
+ customConfig: key.customConfig,
245
+ };
246
+ },
247
+ });
248
+ // ============================================
249
+ // GET SUPPORTED PROVIDERS
250
+ // ============================================
251
+ export const getSupportedProviders = query({
252
+ args: {},
253
+ handler: async () => {
254
+ return BYOK_PROVIDERS;
255
+ },
256
+ });
257
+ //# sourceMappingURL=providerKeys.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"providerKeys.js","sourceRoot":"","sources":["providerKeys.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,eAAe,CAAC;AAClC,OAAO,EAAE,QAAQ,EAAE,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAErE,+CAA+C;AAC/C,4BAA4B;AAC5B,+CAA+C;AAE/C,+BAA+B;AAC/B,MAAM,CAAC,MAAM,cAAc,GAAG;IAC5B,EAAE,EAAE,EAAE,cAAc,EAAE,IAAI,EAAE,cAAc,EAAE,IAAI,EAAE,IAAI,EAAE;IACxD,EAAE,EAAE,EAAE,YAAY,EAAE,IAAI,EAAE,YAAY,EAAE,IAAI,EAAE,IAAI,EAAE;IACpD,EAAE,EAAE,EAAE,YAAY,EAAE,IAAI,EAAE,YAAY,EAAE,IAAI,EAAE,KAAK,EAAE;IACrD,EAAE,EAAE,EAAE,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AClB,OAAO,cAAc,CAAC;IACxB,CAAC;CACF,CAAC,CAAC"}
@@ -0,0 +1,289 @@
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
+ });