@nordsym/apiclaw 1.5.12 → 1.5.14

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 (70) hide show
  1. package/dist/bin.js +1 -1
  2. package/dist/cli/commands/mcp-install.js +6 -6
  3. package/dist/cli/index.js +7 -0
  4. package/dist/convex/adminActivate.js +46 -0
  5. package/dist/convex/adminStats.js +41 -0
  6. package/dist/convex/agents.js +498 -0
  7. package/dist/convex/analytics.js +165 -0
  8. package/dist/convex/billing.js +654 -0
  9. package/dist/convex/capabilities.js +144 -0
  10. package/dist/convex/chains.js +1041 -0
  11. package/dist/convex/credits.js +185 -0
  12. package/dist/convex/crons.js +16 -0
  13. package/dist/convex/directCall.js +626 -0
  14. package/dist/convex/earnProgress.js +648 -0
  15. package/dist/convex/email.js +299 -0
  16. package/dist/convex/feedback.js +226 -0
  17. package/dist/convex/http.js +909 -0
  18. package/dist/convex/logs.js +486 -0
  19. package/dist/convex/mou.js +81 -0
  20. package/dist/convex/providerKeys.js +256 -0
  21. package/dist/convex/providers.js +755 -0
  22. package/dist/convex/purchases.js +156 -0
  23. package/dist/convex/ratelimit.js +90 -0
  24. package/dist/convex/schema.js +709 -0
  25. package/dist/convex/searchLogs.js +128 -0
  26. package/dist/convex/spendAlerts.js +379 -0
  27. package/dist/convex/stripeActions.js +410 -0
  28. package/dist/convex/teams.js +214 -0
  29. package/dist/convex/telemetry.js +73 -0
  30. package/dist/convex/usage.js +228 -0
  31. package/dist/convex/waitlist.js +48 -0
  32. package/dist/convex/webhooks.js +409 -0
  33. package/dist/convex/workspaces.js +879 -0
  34. package/dist/src/analytics.js +129 -0
  35. package/dist/src/bin.js +17 -0
  36. package/dist/src/capability-router.js +240 -0
  37. package/dist/src/chainExecutor.js +451 -0
  38. package/dist/src/chainResolver.js +518 -0
  39. package/dist/src/cli/commands/doctor.js +324 -0
  40. package/dist/src/cli/commands/mcp-install.js +255 -0
  41. package/dist/src/cli/commands/restore.js +259 -0
  42. package/dist/src/cli/commands/setup.js +205 -0
  43. package/dist/src/cli/commands/uninstall.js +188 -0
  44. package/dist/src/cli/index.js +111 -0
  45. package/dist/src/cli.js +302 -0
  46. package/dist/src/confirmation.js +240 -0
  47. package/dist/src/credentials.js +357 -0
  48. package/dist/src/credits.js +260 -0
  49. package/dist/src/crypto.js +66 -0
  50. package/dist/src/discovery.js +504 -0
  51. package/dist/src/enterprise/env.js +123 -0
  52. package/dist/src/enterprise/script-generator.js +460 -0
  53. package/dist/src/execute-dynamic.js +473 -0
  54. package/dist/src/execute.js +1727 -0
  55. package/dist/src/index.js +2062 -0
  56. package/dist/src/metered.js +80 -0
  57. package/dist/src/open-apis.js +276 -0
  58. package/dist/src/proxy.js +28 -0
  59. package/dist/src/session.js +86 -0
  60. package/dist/src/stripe.js +407 -0
  61. package/dist/src/telemetry.js +49 -0
  62. package/dist/src/types.js +2 -0
  63. package/dist/src/utils/backup.js +181 -0
  64. package/dist/src/utils/config.js +220 -0
  65. package/dist/src/utils/os.js +105 -0
  66. package/dist/src/utils/paths.js +159 -0
  67. package/package.json +1 -1
  68. package/src/bin.ts +1 -1
  69. package/src/cli/commands/mcp-install.ts +6 -6
  70. package/src/cli/index.ts +8 -0
@@ -0,0 +1,256 @@
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
+ });