@nordsym/apiclaw 1.3.6 → 1.3.7
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.
- package/README.md +33 -0
- package/convex/_generated/api.d.ts +12 -0
- package/convex/billing.ts +651 -216
- package/convex/crons.ts +17 -0
- package/convex/email.ts +135 -82
- package/convex/feedback.ts +265 -0
- package/convex/http.ts +80 -4
- package/convex/logs.ts +287 -0
- package/convex/providerKeys.ts +209 -0
- package/convex/providers.ts +18 -0
- package/convex/schema.ts +115 -0
- package/convex/stripeActions.ts +512 -0
- package/convex/webhooks.ts +494 -0
- package/convex/workspaces.ts +74 -1
- package/dist/index.js +178 -0
- package/dist/index.js.map +1 -1
- package/dist/metered.d.ts +62 -0
- package/dist/metered.d.ts.map +1 -0
- package/dist/metered.js +81 -0
- package/dist/metered.js.map +1 -0
- package/dist/stripe.d.ts +62 -0
- package/dist/stripe.d.ts.map +1 -1
- package/dist/stripe.js +212 -0
- package/dist/stripe.js.map +1 -1
- package/docs/PRD-final-polish.md +117 -0
- package/docs/PRD-mobile-responsive.md +56 -0
- package/docs/PRD-navigation-expansion.md +295 -0
- package/docs/PRD-stripe-billing.md +312 -0
- package/docs/PRD-workspace-cleanup.md +200 -0
- package/landing/src/app/api/billing/checkout/route.ts +109 -0
- package/landing/src/app/api/billing/payment-method/route.ts +118 -0
- package/landing/src/app/api/billing/portal/route.ts +64 -0
- package/landing/src/app/auth/verify/page.tsx +20 -5
- package/landing/src/app/earn/page.tsx +6 -6
- package/landing/src/app/login/page.tsx +1 -1
- package/landing/src/app/page.tsx +70 -70
- package/landing/src/app/providers/dashboard/page.tsx +1 -1
- package/landing/src/app/workspace/page.tsx +3497 -535
- package/landing/src/components/CheckoutButton.tsx +188 -0
- package/landing/src/components/Toast.tsx +84 -0
- package/landing/src/lib/stats.json +1 -1
- package/landing/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/index.ts +205 -0
- package/src/metered.ts +149 -0
- package/src/stripe.ts +253 -0
|
@@ -0,0 +1,494 @@
|
|
|
1
|
+
import { v } from "convex/values";
|
|
2
|
+
import { mutation, query, action, internalAction, internalQuery, internalMutation } from "./_generated/server";
|
|
3
|
+
import { internal } from "./_generated/api";
|
|
4
|
+
import { Doc, Id } from "./_generated/dataModel";
|
|
5
|
+
|
|
6
|
+
// Event types available for webhooks
|
|
7
|
+
export const WEBHOOK_EVENTS = [
|
|
8
|
+
"usage.threshold.80",
|
|
9
|
+
"usage.threshold.100",
|
|
10
|
+
"api.error",
|
|
11
|
+
"agent.connected",
|
|
12
|
+
"agent.revoked",
|
|
13
|
+
] as const;
|
|
14
|
+
|
|
15
|
+
// Generate a random secret for webhook signature verification
|
|
16
|
+
function generateSecret(): string {
|
|
17
|
+
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
|
18
|
+
let result = "whsec_";
|
|
19
|
+
for (let i = 0; i < 32; i++) {
|
|
20
|
+
result += chars.charAt(Math.floor(Math.random() * chars.length));
|
|
21
|
+
}
|
|
22
|
+
return result;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// ============================================
|
|
26
|
+
// QUERIES
|
|
27
|
+
// ============================================
|
|
28
|
+
|
|
29
|
+
export const getWebhooks = query({
|
|
30
|
+
args: { token: v.string() },
|
|
31
|
+
handler: async (ctx, args) => {
|
|
32
|
+
// Verify session
|
|
33
|
+
const session = await ctx.db
|
|
34
|
+
.query("agentSessions")
|
|
35
|
+
.withIndex("by_sessionToken", (q) => q.eq("sessionToken", args.token))
|
|
36
|
+
.first();
|
|
37
|
+
|
|
38
|
+
if (!session) {
|
|
39
|
+
return { error: "Invalid session" };
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// Get webhooks for workspace
|
|
43
|
+
const webhooks = await ctx.db
|
|
44
|
+
.query("webhooks")
|
|
45
|
+
.withIndex("by_workspaceId", (q) => q.eq("workspaceId", session.workspaceId))
|
|
46
|
+
.collect();
|
|
47
|
+
|
|
48
|
+
// Return webhooks without exposing full secret
|
|
49
|
+
return {
|
|
50
|
+
webhooks: webhooks.map((wh) => ({
|
|
51
|
+
id: wh._id,
|
|
52
|
+
url: wh.url,
|
|
53
|
+
events: wh.events,
|
|
54
|
+
enabled: wh.enabled,
|
|
55
|
+
lastTriggeredAt: wh.lastTriggeredAt,
|
|
56
|
+
lastStatus: wh.lastStatus,
|
|
57
|
+
failCount: wh.failCount,
|
|
58
|
+
createdAt: wh.createdAt,
|
|
59
|
+
// Only show hint of secret
|
|
60
|
+
secretHint: wh.secret.slice(0, 10) + "..." + wh.secret.slice(-4),
|
|
61
|
+
})),
|
|
62
|
+
};
|
|
63
|
+
},
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
// ============================================
|
|
67
|
+
// MUTATIONS
|
|
68
|
+
// ============================================
|
|
69
|
+
|
|
70
|
+
export const createWebhook = mutation({
|
|
71
|
+
args: {
|
|
72
|
+
token: v.string(),
|
|
73
|
+
url: v.string(),
|
|
74
|
+
events: v.array(v.string()),
|
|
75
|
+
},
|
|
76
|
+
handler: async (ctx, args) => {
|
|
77
|
+
// Verify session
|
|
78
|
+
const session = await ctx.db
|
|
79
|
+
.query("agentSessions")
|
|
80
|
+
.withIndex("by_sessionToken", (q) => q.eq("sessionToken", args.token))
|
|
81
|
+
.first();
|
|
82
|
+
|
|
83
|
+
if (!session) {
|
|
84
|
+
return { error: "Invalid session" };
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// Validate URL
|
|
88
|
+
try {
|
|
89
|
+
new URL(args.url);
|
|
90
|
+
} catch {
|
|
91
|
+
return { error: "Invalid URL format" };
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// Validate URL is HTTPS
|
|
95
|
+
if (!args.url.startsWith("https://")) {
|
|
96
|
+
return { error: "Webhook URL must use HTTPS" };
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// Validate events
|
|
100
|
+
const validEvents = args.events.filter((e) =>
|
|
101
|
+
WEBHOOK_EVENTS.includes(e as typeof WEBHOOK_EVENTS[number])
|
|
102
|
+
);
|
|
103
|
+
|
|
104
|
+
if (validEvents.length === 0) {
|
|
105
|
+
return { error: "At least one valid event is required" };
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// Check webhook limit (max 5 per workspace)
|
|
109
|
+
const existingWebhooks = await ctx.db
|
|
110
|
+
.query("webhooks")
|
|
111
|
+
.withIndex("by_workspaceId", (q) => q.eq("workspaceId", session.workspaceId))
|
|
112
|
+
.collect();
|
|
113
|
+
|
|
114
|
+
if (existingWebhooks.length >= 5) {
|
|
115
|
+
return { error: "Maximum 5 webhooks per workspace" };
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// Check for duplicate URL
|
|
119
|
+
const duplicate = existingWebhooks.find((wh) => wh.url === args.url);
|
|
120
|
+
if (duplicate) {
|
|
121
|
+
return { error: "A webhook with this URL already exists" };
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// Generate secret
|
|
125
|
+
const secret = generateSecret();
|
|
126
|
+
|
|
127
|
+
// Create webhook
|
|
128
|
+
const webhookId = await ctx.db.insert("webhooks", {
|
|
129
|
+
workspaceId: session.workspaceId,
|
|
130
|
+
url: args.url,
|
|
131
|
+
events: validEvents,
|
|
132
|
+
secret,
|
|
133
|
+
enabled: true,
|
|
134
|
+
failCount: 0,
|
|
135
|
+
createdAt: Date.now(),
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
return {
|
|
139
|
+
success: true,
|
|
140
|
+
webhookId,
|
|
141
|
+
secret, // Return secret only once on creation
|
|
142
|
+
};
|
|
143
|
+
},
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
export const updateWebhook = mutation({
|
|
147
|
+
args: {
|
|
148
|
+
token: v.string(),
|
|
149
|
+
webhookId: v.id("webhooks"),
|
|
150
|
+
enabled: v.optional(v.boolean()),
|
|
151
|
+
events: v.optional(v.array(v.string())),
|
|
152
|
+
},
|
|
153
|
+
handler: async (ctx, args) => {
|
|
154
|
+
// Verify session
|
|
155
|
+
const session = await ctx.db
|
|
156
|
+
.query("agentSessions")
|
|
157
|
+
.withIndex("by_sessionToken", (q) => q.eq("sessionToken", args.token))
|
|
158
|
+
.first();
|
|
159
|
+
|
|
160
|
+
if (!session) {
|
|
161
|
+
return { error: "Invalid session" };
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// Get webhook
|
|
165
|
+
const webhook = await ctx.db.get(args.webhookId);
|
|
166
|
+
|
|
167
|
+
if (!webhook || webhook.workspaceId !== session.workspaceId) {
|
|
168
|
+
return { error: "Webhook not found" };
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// Build update object
|
|
172
|
+
const updates: Partial<{
|
|
173
|
+
enabled: boolean;
|
|
174
|
+
events: string[];
|
|
175
|
+
}> = {};
|
|
176
|
+
|
|
177
|
+
if (args.enabled !== undefined) {
|
|
178
|
+
updates.enabled = args.enabled;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
if (args.events !== undefined) {
|
|
182
|
+
const validEvents = args.events.filter((e) =>
|
|
183
|
+
WEBHOOK_EVENTS.includes(e as typeof WEBHOOK_EVENTS[number])
|
|
184
|
+
);
|
|
185
|
+
if (validEvents.length === 0) {
|
|
186
|
+
return { error: "At least one valid event is required" };
|
|
187
|
+
}
|
|
188
|
+
updates.events = validEvents;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// Update webhook
|
|
192
|
+
await ctx.db.patch(args.webhookId, updates);
|
|
193
|
+
|
|
194
|
+
return { success: true };
|
|
195
|
+
},
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
export const deleteWebhook = mutation({
|
|
199
|
+
args: {
|
|
200
|
+
token: v.string(),
|
|
201
|
+
webhookId: v.id("webhooks"),
|
|
202
|
+
},
|
|
203
|
+
handler: async (ctx, args) => {
|
|
204
|
+
// Verify session
|
|
205
|
+
const session = await ctx.db
|
|
206
|
+
.query("agentSessions")
|
|
207
|
+
.withIndex("by_sessionToken", (q) => q.eq("sessionToken", args.token))
|
|
208
|
+
.first();
|
|
209
|
+
|
|
210
|
+
if (!session) {
|
|
211
|
+
return { error: "Invalid session" };
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// Get webhook
|
|
215
|
+
const webhook = await ctx.db.get(args.webhookId);
|
|
216
|
+
|
|
217
|
+
if (!webhook || webhook.workspaceId !== session.workspaceId) {
|
|
218
|
+
return { error: "Webhook not found" };
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// Delete webhook
|
|
222
|
+
await ctx.db.delete(args.webhookId);
|
|
223
|
+
|
|
224
|
+
return { success: true };
|
|
225
|
+
},
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
export const regenerateSecret = mutation({
|
|
229
|
+
args: {
|
|
230
|
+
token: v.string(),
|
|
231
|
+
webhookId: v.id("webhooks"),
|
|
232
|
+
},
|
|
233
|
+
handler: async (ctx, args) => {
|
|
234
|
+
// Verify session
|
|
235
|
+
const session = await ctx.db
|
|
236
|
+
.query("agentSessions")
|
|
237
|
+
.withIndex("by_sessionToken", (q) => q.eq("sessionToken", args.token))
|
|
238
|
+
.first();
|
|
239
|
+
|
|
240
|
+
if (!session) {
|
|
241
|
+
return { error: "Invalid session" };
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
// Get webhook
|
|
245
|
+
const webhook = await ctx.db.get(args.webhookId);
|
|
246
|
+
|
|
247
|
+
if (!webhook || webhook.workspaceId !== session.workspaceId) {
|
|
248
|
+
return { error: "Webhook not found" };
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
// Generate new secret
|
|
252
|
+
const newSecret = generateSecret();
|
|
253
|
+
|
|
254
|
+
// Update webhook
|
|
255
|
+
await ctx.db.patch(args.webhookId, { secret: newSecret });
|
|
256
|
+
|
|
257
|
+
return {
|
|
258
|
+
success: true,
|
|
259
|
+
secret: newSecret, // Return new secret
|
|
260
|
+
};
|
|
261
|
+
},
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
// ============================================
|
|
265
|
+
// ACTIONS (for HTTP calls)
|
|
266
|
+
// ============================================
|
|
267
|
+
|
|
268
|
+
export const testWebhook = action({
|
|
269
|
+
args: {
|
|
270
|
+
token: v.string(),
|
|
271
|
+
webhookId: v.id("webhooks"),
|
|
272
|
+
},
|
|
273
|
+
returns: v.union(
|
|
274
|
+
v.object({ error: v.string() }),
|
|
275
|
+
v.object({ success: v.literal(true), status: v.number(), message: v.string() }),
|
|
276
|
+
v.object({ success: v.literal(false), status: v.optional(v.number()), message: v.string() })
|
|
277
|
+
),
|
|
278
|
+
handler: async (ctx, args): Promise<
|
|
279
|
+
| { error: string }
|
|
280
|
+
| { success: true; status: number; message: string }
|
|
281
|
+
| { success: false; status?: number; message: string }
|
|
282
|
+
> => {
|
|
283
|
+
// Get webhook from database
|
|
284
|
+
const queryResult = await ctx.runQuery(internal.webhooks.getWebhookInternal, {
|
|
285
|
+
token: args.token,
|
|
286
|
+
webhookId: args.webhookId,
|
|
287
|
+
});
|
|
288
|
+
|
|
289
|
+
if (!queryResult || "error" in queryResult) {
|
|
290
|
+
return { error: queryResult?.error || "Webhook not found" };
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
const webhook = queryResult.webhook;
|
|
294
|
+
|
|
295
|
+
// Create test payload
|
|
296
|
+
const payload = {
|
|
297
|
+
event: "test",
|
|
298
|
+
workspace: webhook.workspaceId,
|
|
299
|
+
timestamp: new Date().toISOString(),
|
|
300
|
+
data: {
|
|
301
|
+
message: "This is a test webhook from APIClaw",
|
|
302
|
+
webhookId: args.webhookId,
|
|
303
|
+
},
|
|
304
|
+
};
|
|
305
|
+
|
|
306
|
+
// Sign the payload
|
|
307
|
+
const signature = await signPayload(JSON.stringify(payload), webhook.secret);
|
|
308
|
+
|
|
309
|
+
try {
|
|
310
|
+
const response = await fetch(webhook.url, {
|
|
311
|
+
method: "POST",
|
|
312
|
+
headers: {
|
|
313
|
+
"Content-Type": "application/json",
|
|
314
|
+
"X-APIClaw-Signature": signature,
|
|
315
|
+
"X-APIClaw-Event": "test",
|
|
316
|
+
"X-APIClaw-Timestamp": payload.timestamp,
|
|
317
|
+
},
|
|
318
|
+
body: JSON.stringify(payload),
|
|
319
|
+
});
|
|
320
|
+
|
|
321
|
+
if (response.ok) {
|
|
322
|
+
return {
|
|
323
|
+
success: true as const,
|
|
324
|
+
status: response.status,
|
|
325
|
+
message: "Webhook delivered successfully",
|
|
326
|
+
};
|
|
327
|
+
} else {
|
|
328
|
+
return {
|
|
329
|
+
success: false as const,
|
|
330
|
+
status: response.status,
|
|
331
|
+
message: `Webhook returned status ${response.status}`,
|
|
332
|
+
};
|
|
333
|
+
}
|
|
334
|
+
} catch (error) {
|
|
335
|
+
return {
|
|
336
|
+
success: false as const,
|
|
337
|
+
message: error instanceof Error ? error.message : "Failed to deliver webhook",
|
|
338
|
+
};
|
|
339
|
+
}
|
|
340
|
+
},
|
|
341
|
+
});
|
|
342
|
+
|
|
343
|
+
// Internal action to trigger webhooks (called from other parts of the system)
|
|
344
|
+
export const triggerWebhooks = internalAction({
|
|
345
|
+
args: {
|
|
346
|
+
workspaceId: v.id("workspaces"),
|
|
347
|
+
event: v.string(),
|
|
348
|
+
data: v.any(),
|
|
349
|
+
},
|
|
350
|
+
returns: v.object({ triggered: v.number(), total: v.optional(v.number()) }),
|
|
351
|
+
handler: async (ctx, args): Promise<{ triggered: number; total?: number }> => {
|
|
352
|
+
// Get all enabled webhooks for this workspace that subscribe to this event
|
|
353
|
+
const webhooksResult = await ctx.runQuery(internal.webhooks.getWebhooksForEvent, {
|
|
354
|
+
workspaceId: args.workspaceId,
|
|
355
|
+
event: args.event,
|
|
356
|
+
});
|
|
357
|
+
|
|
358
|
+
if (!webhooksResult || webhooksResult.length === 0) {
|
|
359
|
+
return { triggered: 0 };
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
const payload = {
|
|
363
|
+
event: args.event,
|
|
364
|
+
workspace: args.workspaceId,
|
|
365
|
+
timestamp: new Date().toISOString(),
|
|
366
|
+
data: args.data,
|
|
367
|
+
};
|
|
368
|
+
|
|
369
|
+
const payloadString = JSON.stringify(payload);
|
|
370
|
+
let successCount = 0;
|
|
371
|
+
|
|
372
|
+
// Send to each webhook
|
|
373
|
+
for (const webhook of webhooksResult) {
|
|
374
|
+
const signature = await signPayload(payloadString, webhook.secret);
|
|
375
|
+
|
|
376
|
+
try {
|
|
377
|
+
const response = await fetch(webhook.url, {
|
|
378
|
+
method: "POST",
|
|
379
|
+
headers: {
|
|
380
|
+
"Content-Type": "application/json",
|
|
381
|
+
"X-APIClaw-Signature": signature,
|
|
382
|
+
"X-APIClaw-Event": args.event,
|
|
383
|
+
"X-APIClaw-Timestamp": payload.timestamp,
|
|
384
|
+
},
|
|
385
|
+
body: payloadString,
|
|
386
|
+
});
|
|
387
|
+
|
|
388
|
+
// Update webhook status
|
|
389
|
+
await ctx.runMutation(internal.webhooks.updateWebhookStatus, {
|
|
390
|
+
webhookId: webhook._id,
|
|
391
|
+
success: response.ok,
|
|
392
|
+
});
|
|
393
|
+
|
|
394
|
+
if (response.ok) {
|
|
395
|
+
successCount++;
|
|
396
|
+
}
|
|
397
|
+
} catch {
|
|
398
|
+
// Update webhook with failure
|
|
399
|
+
await ctx.runMutation(internal.webhooks.updateWebhookStatus, {
|
|
400
|
+
webhookId: webhook._id,
|
|
401
|
+
success: false,
|
|
402
|
+
});
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
return { triggered: successCount, total: webhooksResult.length };
|
|
407
|
+
},
|
|
408
|
+
});
|
|
409
|
+
|
|
410
|
+
// ============================================
|
|
411
|
+
// INTERNAL QUERIES/MUTATIONS (for actions)
|
|
412
|
+
// ============================================
|
|
413
|
+
|
|
414
|
+
export const getWebhookInternal = internalQuery({
|
|
415
|
+
args: {
|
|
416
|
+
token: v.string(),
|
|
417
|
+
webhookId: v.id("webhooks"),
|
|
418
|
+
},
|
|
419
|
+
handler: async (ctx, args): Promise<{ error: string } | { webhook: Doc<"webhooks"> }> => {
|
|
420
|
+
// Verify session
|
|
421
|
+
const session = await ctx.db
|
|
422
|
+
.query("agentSessions")
|
|
423
|
+
.withIndex("by_sessionToken", (q) => q.eq("sessionToken", args.token))
|
|
424
|
+
.first();
|
|
425
|
+
|
|
426
|
+
if (!session) {
|
|
427
|
+
return { error: "Invalid session" };
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
// Get webhook
|
|
431
|
+
const webhook = await ctx.db.get(args.webhookId);
|
|
432
|
+
|
|
433
|
+
if (!webhook || webhook.workspaceId !== session.workspaceId) {
|
|
434
|
+
return { error: "Webhook not found" };
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
return { webhook };
|
|
438
|
+
},
|
|
439
|
+
});
|
|
440
|
+
|
|
441
|
+
export const getWebhooksForEvent = internalQuery({
|
|
442
|
+
args: {
|
|
443
|
+
workspaceId: v.id("workspaces"),
|
|
444
|
+
event: v.string(),
|
|
445
|
+
},
|
|
446
|
+
handler: async (ctx, args): Promise<Doc<"webhooks">[]> => {
|
|
447
|
+
const webhooks = await ctx.db
|
|
448
|
+
.query("webhooks")
|
|
449
|
+
.withIndex("by_workspaceId", (q) => q.eq("workspaceId", args.workspaceId))
|
|
450
|
+
.collect();
|
|
451
|
+
|
|
452
|
+
// Filter for enabled webhooks that subscribe to this event
|
|
453
|
+
return webhooks.filter(
|
|
454
|
+
(wh) => wh.enabled && wh.events.includes(args.event)
|
|
455
|
+
);
|
|
456
|
+
},
|
|
457
|
+
});
|
|
458
|
+
|
|
459
|
+
export const updateWebhookStatus = internalMutation({
|
|
460
|
+
args: {
|
|
461
|
+
webhookId: v.id("webhooks"),
|
|
462
|
+
success: v.boolean(),
|
|
463
|
+
},
|
|
464
|
+
handler: async (ctx, args) => {
|
|
465
|
+
const webhook = await ctx.db.get(args.webhookId);
|
|
466
|
+
if (!webhook) return;
|
|
467
|
+
|
|
468
|
+
await ctx.db.patch(args.webhookId, {
|
|
469
|
+
lastTriggeredAt: Date.now(),
|
|
470
|
+
lastStatus: args.success ? "success" : "failed",
|
|
471
|
+
failCount: args.success ? 0 : webhook.failCount + 1,
|
|
472
|
+
});
|
|
473
|
+
|
|
474
|
+
// Disable webhook after 5 consecutive failures
|
|
475
|
+
if (!args.success && webhook.failCount + 1 >= 5) {
|
|
476
|
+
await ctx.db.patch(args.webhookId, { enabled: false });
|
|
477
|
+
}
|
|
478
|
+
},
|
|
479
|
+
});
|
|
480
|
+
|
|
481
|
+
// ============================================
|
|
482
|
+
// HELPERS
|
|
483
|
+
// ============================================
|
|
484
|
+
|
|
485
|
+
async function signPayload(payload: string, secret: string): Promise<string> {
|
|
486
|
+
// Simple HMAC-like signature using SHA-256
|
|
487
|
+
// In a production environment, use proper crypto
|
|
488
|
+
const encoder = new TextEncoder();
|
|
489
|
+
const data = encoder.encode(payload + secret);
|
|
490
|
+
const hashBuffer = await crypto.subtle.digest("SHA-256", data);
|
|
491
|
+
const hashArray = Array.from(new Uint8Array(hashBuffer));
|
|
492
|
+
const hashHex = hashArray.map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
493
|
+
return `sha256=${hashHex}`;
|
|
494
|
+
}
|
package/convex/workspaces.ts
CHANGED
|
@@ -67,7 +67,7 @@ export const verifyMagicLink = mutation({
|
|
|
67
67
|
status: "active",
|
|
68
68
|
tier: "free",
|
|
69
69
|
usageCount: 0,
|
|
70
|
-
usageLimit:
|
|
70
|
+
usageLimit: 50, // 50 free API calls
|
|
71
71
|
createdAt: Date.now(),
|
|
72
72
|
updatedAt: Date.now(),
|
|
73
73
|
});
|
|
@@ -219,6 +219,8 @@ export const getConnectedAgents = query({
|
|
|
219
219
|
return agentSessions.map((s) => ({
|
|
220
220
|
id: s._id,
|
|
221
221
|
fingerprint: s.fingerprint || "Unknown",
|
|
222
|
+
customName: s.customName || null,
|
|
223
|
+
name: s.customName || s.fingerprint || "Unknown",
|
|
222
224
|
lastUsedAt: s.lastUsedAt,
|
|
223
225
|
createdAt: s.createdAt,
|
|
224
226
|
isCurrent: s.sessionToken === token,
|
|
@@ -226,6 +228,77 @@ export const getConnectedAgents = query({
|
|
|
226
228
|
},
|
|
227
229
|
});
|
|
228
230
|
|
|
231
|
+
// Admin: Delete session by ID (for cleanup)
|
|
232
|
+
export const adminDeleteSession = mutation({
|
|
233
|
+
args: { sessionId: v.id("agentSessions") },
|
|
234
|
+
handler: async (ctx, { sessionId }) => {
|
|
235
|
+
await ctx.db.delete(sessionId);
|
|
236
|
+
return { success: true };
|
|
237
|
+
},
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
// Debug: Get sessions by workspace email
|
|
241
|
+
export const getSessionsByEmail = query({
|
|
242
|
+
args: { email: v.string() },
|
|
243
|
+
handler: async (ctx, { email }) => {
|
|
244
|
+
const workspace = await ctx.db
|
|
245
|
+
.query("workspaces")
|
|
246
|
+
.withIndex("by_email", (q) => q.eq("email", email.toLowerCase()))
|
|
247
|
+
.first();
|
|
248
|
+
|
|
249
|
+
if (!workspace) {
|
|
250
|
+
return { error: "Workspace not found", sessions: [] };
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
const sessions = await ctx.db
|
|
254
|
+
.query("agentSessions")
|
|
255
|
+
.withIndex("by_workspaceId", (q) => q.eq("workspaceId", workspace._id))
|
|
256
|
+
.collect();
|
|
257
|
+
|
|
258
|
+
return {
|
|
259
|
+
workspaceId: workspace._id,
|
|
260
|
+
email: workspace.email,
|
|
261
|
+
sessions: sessions.map(s => ({
|
|
262
|
+
id: s._id,
|
|
263
|
+
fingerprint: s.fingerprint,
|
|
264
|
+
createdAt: s.createdAt,
|
|
265
|
+
lastUsedAt: s.lastUsedAt,
|
|
266
|
+
})),
|
|
267
|
+
};
|
|
268
|
+
},
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
// Rename an agent session
|
|
272
|
+
export const renameAgent = mutation({
|
|
273
|
+
args: {
|
|
274
|
+
token: v.string(),
|
|
275
|
+
sessionId: v.id("agentSessions"),
|
|
276
|
+
name: v.string(),
|
|
277
|
+
},
|
|
278
|
+
handler: async (ctx, { token, sessionId, name }) => {
|
|
279
|
+
// Verify the requesting session
|
|
280
|
+
const session = await ctx.db
|
|
281
|
+
.query("agentSessions")
|
|
282
|
+
.withIndex("by_sessionToken", (q) => q.eq("sessionToken", token))
|
|
283
|
+
.first();
|
|
284
|
+
|
|
285
|
+
if (!session) {
|
|
286
|
+
throw new Error("Invalid session");
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
// Get the session to rename
|
|
290
|
+
const targetSession = await ctx.db.get(sessionId);
|
|
291
|
+
if (!targetSession || targetSession.workspaceId !== session.workspaceId) {
|
|
292
|
+
throw new Error("Session not found or access denied");
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
// Update the name (stored as customName field)
|
|
296
|
+
await ctx.db.patch(sessionId, { customName: name });
|
|
297
|
+
|
|
298
|
+
return { success: true };
|
|
299
|
+
},
|
|
300
|
+
});
|
|
301
|
+
|
|
229
302
|
// Get usage breakdown by provider
|
|
230
303
|
export const getUsageBreakdown = query({
|
|
231
304
|
args: { token: v.string() },
|