@nordsym/apiclaw 1.5.13 → 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.
- package/dist/bin.js +1 -1
- package/dist/cli/index.js +7 -0
- package/dist/convex/adminActivate.js +46 -0
- package/dist/convex/adminStats.js +41 -0
- package/dist/convex/agents.js +498 -0
- package/dist/convex/analytics.js +165 -0
- package/dist/convex/billing.js +654 -0
- package/dist/convex/capabilities.js +144 -0
- package/dist/convex/chains.js +1041 -0
- package/dist/convex/credits.js +185 -0
- package/dist/convex/crons.js +16 -0
- package/dist/convex/directCall.js +626 -0
- package/dist/convex/earnProgress.js +648 -0
- package/dist/convex/email.js +299 -0
- package/dist/convex/feedback.js +226 -0
- package/dist/convex/http.js +909 -0
- package/dist/convex/logs.js +486 -0
- package/dist/convex/mou.js +81 -0
- package/dist/convex/providerKeys.js +256 -0
- package/dist/convex/providers.js +755 -0
- package/dist/convex/purchases.js +156 -0
- package/dist/convex/ratelimit.js +90 -0
- package/dist/convex/schema.js +709 -0
- package/dist/convex/searchLogs.js +128 -0
- package/dist/convex/spendAlerts.js +379 -0
- package/dist/convex/stripeActions.js +410 -0
- package/dist/convex/teams.js +214 -0
- package/dist/convex/telemetry.js +73 -0
- package/dist/convex/usage.js +228 -0
- package/dist/convex/waitlist.js +48 -0
- package/dist/convex/webhooks.js +409 -0
- package/dist/convex/workspaces.js +879 -0
- package/dist/src/analytics.js +129 -0
- package/dist/src/bin.js +17 -0
- package/dist/src/capability-router.js +240 -0
- package/dist/src/chainExecutor.js +451 -0
- package/dist/src/chainResolver.js +518 -0
- package/dist/src/cli/commands/doctor.js +324 -0
- package/dist/src/cli/commands/mcp-install.js +255 -0
- package/dist/src/cli/commands/restore.js +259 -0
- package/dist/src/cli/commands/setup.js +205 -0
- package/dist/src/cli/commands/uninstall.js +188 -0
- package/dist/src/cli/index.js +111 -0
- package/dist/src/cli.js +302 -0
- package/dist/src/confirmation.js +240 -0
- package/dist/src/credentials.js +357 -0
- package/dist/src/credits.js +260 -0
- package/dist/src/crypto.js +66 -0
- package/dist/src/discovery.js +504 -0
- package/dist/src/enterprise/env.js +123 -0
- package/dist/src/enterprise/script-generator.js +460 -0
- package/dist/src/execute-dynamic.js +473 -0
- package/dist/src/execute.js +1727 -0
- package/dist/src/index.js +2062 -0
- package/dist/src/metered.js +80 -0
- package/dist/src/open-apis.js +276 -0
- package/dist/src/proxy.js +28 -0
- package/dist/src/session.js +86 -0
- package/dist/src/stripe.js +407 -0
- package/dist/src/telemetry.js +49 -0
- package/dist/src/types.js +2 -0
- package/dist/src/utils/backup.js +181 -0
- package/dist/src/utils/config.js +220 -0
- package/dist/src/utils/os.js +105 -0
- package/dist/src/utils/paths.js +159 -0
- package/package.json +1 -1
- package/src/bin.ts +1 -1
- package/src/cli/index.ts +8 -0
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
import { v } from "convex/values";
|
|
2
|
+
import { mutation, query } from "./_generated/server";
|
|
3
|
+
// Credit packages
|
|
4
|
+
export const CREDIT_PACKAGES = {
|
|
5
|
+
starter: { amountUsd: 10, credits: 100, bonus: 0 },
|
|
6
|
+
growth: { amountUsd: 50, credits: 550, bonus: 50 }, // 10% bonus
|
|
7
|
+
scale: { amountUsd: 100, credits: 1200, bonus: 200 }, // 20% bonus
|
|
8
|
+
};
|
|
9
|
+
// Get or create agent credits account
|
|
10
|
+
export const getOrCreateAgent = mutation({
|
|
11
|
+
args: { agentId: v.string() },
|
|
12
|
+
handler: async (ctx, args) => {
|
|
13
|
+
const existing = await ctx.db
|
|
14
|
+
.query("agentCredits")
|
|
15
|
+
.withIndex("by_agentId", (q) => q.eq("agentId", args.agentId))
|
|
16
|
+
.first();
|
|
17
|
+
if (existing)
|
|
18
|
+
return existing;
|
|
19
|
+
const now = Date.now();
|
|
20
|
+
const id = await ctx.db.insert("agentCredits", {
|
|
21
|
+
agentId: args.agentId,
|
|
22
|
+
balanceUsd: 0,
|
|
23
|
+
currency: "USD",
|
|
24
|
+
createdAt: now,
|
|
25
|
+
updatedAt: now,
|
|
26
|
+
});
|
|
27
|
+
return await ctx.db.get(id);
|
|
28
|
+
},
|
|
29
|
+
});
|
|
30
|
+
// Get agent credits
|
|
31
|
+
export const getAgentCredits = query({
|
|
32
|
+
args: { agentId: v.string() },
|
|
33
|
+
handler: async (ctx, args) => {
|
|
34
|
+
return await ctx.db
|
|
35
|
+
.query("agentCredits")
|
|
36
|
+
.withIndex("by_agentId", (q) => q.eq("agentId", args.agentId))
|
|
37
|
+
.first();
|
|
38
|
+
},
|
|
39
|
+
});
|
|
40
|
+
// Add credits to agent account (called by webhook or admin)
|
|
41
|
+
export const addCredits = mutation({
|
|
42
|
+
args: {
|
|
43
|
+
agentId: v.string(),
|
|
44
|
+
amountUsd: v.number(),
|
|
45
|
+
source: v.optional(v.string()),
|
|
46
|
+
},
|
|
47
|
+
handler: async (ctx, args) => {
|
|
48
|
+
const credits = await ctx.db
|
|
49
|
+
.query("agentCredits")
|
|
50
|
+
.withIndex("by_agentId", (q) => q.eq("agentId", args.agentId))
|
|
51
|
+
.first();
|
|
52
|
+
const now = Date.now();
|
|
53
|
+
if (credits) {
|
|
54
|
+
await ctx.db.patch(credits._id, {
|
|
55
|
+
balanceUsd: credits.balanceUsd + args.amountUsd,
|
|
56
|
+
updatedAt: now,
|
|
57
|
+
});
|
|
58
|
+
return await ctx.db.get(credits._id);
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
const id = await ctx.db.insert("agentCredits", {
|
|
62
|
+
agentId: args.agentId,
|
|
63
|
+
balanceUsd: args.amountUsd,
|
|
64
|
+
currency: "USD",
|
|
65
|
+
createdAt: now,
|
|
66
|
+
updatedAt: now,
|
|
67
|
+
});
|
|
68
|
+
return await ctx.db.get(id);
|
|
69
|
+
}
|
|
70
|
+
},
|
|
71
|
+
});
|
|
72
|
+
// Deduct credits (internal use)
|
|
73
|
+
export const deductCredits = mutation({
|
|
74
|
+
args: {
|
|
75
|
+
agentId: v.string(),
|
|
76
|
+
amountUsd: v.number(),
|
|
77
|
+
},
|
|
78
|
+
handler: async (ctx, args) => {
|
|
79
|
+
const credits = await ctx.db
|
|
80
|
+
.query("agentCredits")
|
|
81
|
+
.withIndex("by_agentId", (q) => q.eq("agentId", args.agentId))
|
|
82
|
+
.first();
|
|
83
|
+
if (!credits) {
|
|
84
|
+
throw new Error(`No credits account for agent: ${args.agentId}`);
|
|
85
|
+
}
|
|
86
|
+
if (credits.balanceUsd < args.amountUsd) {
|
|
87
|
+
throw new Error(`Insufficient balance: have $${credits.balanceUsd.toFixed(2)}, need $${args.amountUsd.toFixed(2)}`);
|
|
88
|
+
}
|
|
89
|
+
await ctx.db.patch(credits._id, {
|
|
90
|
+
balanceUsd: credits.balanceUsd - args.amountUsd,
|
|
91
|
+
updatedAt: Date.now(),
|
|
92
|
+
});
|
|
93
|
+
return await ctx.db.get(credits._id);
|
|
94
|
+
},
|
|
95
|
+
});
|
|
96
|
+
// Record credit top-up from Stripe
|
|
97
|
+
export const recordTopup = mutation({
|
|
98
|
+
args: {
|
|
99
|
+
agentId: v.string(),
|
|
100
|
+
stripeSessionId: v.optional(v.string()),
|
|
101
|
+
stripePaymentIntentId: v.optional(v.string()),
|
|
102
|
+
amountUsd: v.number(),
|
|
103
|
+
creditsGranted: v.number(),
|
|
104
|
+
packageType: v.string(),
|
|
105
|
+
status: v.string(),
|
|
106
|
+
},
|
|
107
|
+
handler: async (ctx, args) => {
|
|
108
|
+
const now = Date.now();
|
|
109
|
+
return await ctx.db.insert("creditTopups", {
|
|
110
|
+
agentId: args.agentId,
|
|
111
|
+
stripeSessionId: args.stripeSessionId,
|
|
112
|
+
stripePaymentIntentId: args.stripePaymentIntentId,
|
|
113
|
+
amountUsd: args.amountUsd,
|
|
114
|
+
creditsGranted: args.creditsGranted,
|
|
115
|
+
packageType: args.packageType,
|
|
116
|
+
status: args.status,
|
|
117
|
+
createdAt: now,
|
|
118
|
+
completedAt: args.status === "completed" ? now : undefined,
|
|
119
|
+
});
|
|
120
|
+
},
|
|
121
|
+
});
|
|
122
|
+
// Complete a pending top-up
|
|
123
|
+
export const completeTopup = mutation({
|
|
124
|
+
args: {
|
|
125
|
+
stripeSessionId: v.optional(v.string()),
|
|
126
|
+
stripePaymentIntentId: v.optional(v.string()),
|
|
127
|
+
},
|
|
128
|
+
handler: async (ctx, args) => {
|
|
129
|
+
let topup;
|
|
130
|
+
if (args.stripeSessionId) {
|
|
131
|
+
topup = await ctx.db
|
|
132
|
+
.query("creditTopups")
|
|
133
|
+
.withIndex("by_stripeSessionId", (q) => q.eq("stripeSessionId", args.stripeSessionId))
|
|
134
|
+
.first();
|
|
135
|
+
}
|
|
136
|
+
else if (args.stripePaymentIntentId) {
|
|
137
|
+
topup = await ctx.db
|
|
138
|
+
.query("creditTopups")
|
|
139
|
+
.withIndex("by_stripePaymentIntentId", (q) => q.eq("stripePaymentIntentId", args.stripePaymentIntentId))
|
|
140
|
+
.first();
|
|
141
|
+
}
|
|
142
|
+
if (!topup) {
|
|
143
|
+
throw new Error("Top-up not found");
|
|
144
|
+
}
|
|
145
|
+
if (topup.status === "completed") {
|
|
146
|
+
return topup; // Already completed, idempotent
|
|
147
|
+
}
|
|
148
|
+
// Update top-up status
|
|
149
|
+
await ctx.db.patch(topup._id, {
|
|
150
|
+
status: "completed",
|
|
151
|
+
completedAt: Date.now(),
|
|
152
|
+
});
|
|
153
|
+
// Add credits to agent
|
|
154
|
+
const credits = await ctx.db
|
|
155
|
+
.query("agentCredits")
|
|
156
|
+
.withIndex("by_agentId", (q) => q.eq("agentId", topup.agentId))
|
|
157
|
+
.first();
|
|
158
|
+
if (credits) {
|
|
159
|
+
await ctx.db.patch(credits._id, {
|
|
160
|
+
balanceUsd: credits.balanceUsd + topup.creditsGranted,
|
|
161
|
+
updatedAt: Date.now(),
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
else {
|
|
165
|
+
await ctx.db.insert("agentCredits", {
|
|
166
|
+
agentId: topup.agentId,
|
|
167
|
+
balanceUsd: topup.creditsGranted,
|
|
168
|
+
currency: "USD",
|
|
169
|
+
createdAt: Date.now(),
|
|
170
|
+
updatedAt: Date.now(),
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
return topup;
|
|
174
|
+
},
|
|
175
|
+
});
|
|
176
|
+
// Get all top-ups for an agent
|
|
177
|
+
export const getTopups = query({
|
|
178
|
+
args: { agentId: v.string() },
|
|
179
|
+
handler: async (ctx, args) => {
|
|
180
|
+
return await ctx.db
|
|
181
|
+
.query("creditTopups")
|
|
182
|
+
.withIndex("by_agentId", (q) => q.eq("agentId", args.agentId))
|
|
183
|
+
.collect();
|
|
184
|
+
},
|
|
185
|
+
});
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { cronJobs } from "convex/server";
|
|
2
|
+
import { internal } from "./_generated/api";
|
|
3
|
+
const crons = cronJobs();
|
|
4
|
+
/**
|
|
5
|
+
* Daily Usage Reporting to Stripe
|
|
6
|
+
* Runs at 00:05 UTC every day
|
|
7
|
+
* Reports metered usage for all active subscriptions
|
|
8
|
+
*/
|
|
9
|
+
crons.daily("report-usage-to-stripe", { hourUTC: 0, minuteUTC: 5 }, internal.billing.reportAllUsageToStripe);
|
|
10
|
+
/**
|
|
11
|
+
* Monthly Spend Reset
|
|
12
|
+
* Runs at 00:01 UTC on the 1st of each month
|
|
13
|
+
* Resets monthlySpendCents and budgetAlertSentAt for all workspaces
|
|
14
|
+
*/
|
|
15
|
+
crons.monthly("reset-monthly-spend", { day: 1, hourUTC: 0, minuteUTC: 1 }, internal.spendAlerts.resetMonthlySpend);
|
|
16
|
+
export default crons;
|