@nordsym/apiclaw 1.8.5 → 1.8.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 +4 -4
- package/apiclaw-README.md +12 -12
- package/convex/_generated/api.d.ts +16 -0
- package/convex/analytics.d.ts.map +1 -1
- package/convex/analytics.js.map +1 -1
- package/convex/analytics.ts +1 -0
- package/convex/apiKeys.ts +220 -0
- package/convex/backfillAnalytics.d.ts.map +1 -0
- package/convex/backfillAnalytics.js.map +1 -0
- package/convex/backfillAnalytics.ts +22 -0
- package/convex/backfillSearchLogs.d.ts.map +1 -0
- package/convex/backfillSearchLogs.js.map +1 -0
- package/convex/backfillSearchLogs.ts +35 -0
- package/convex/debugFilestackLogs.d.ts.map +1 -0
- package/convex/debugFilestackLogs.js.map +1 -0
- package/convex/debugFilestackLogs.ts +16 -0
- package/convex/debugGetToken.d.ts.map +1 -0
- package/convex/debugGetToken.js.map +1 -0
- package/convex/debugGetToken.ts +18 -0
- package/convex/directCall.ts +49 -14
- package/convex/email.ts +5 -5
- package/convex/http.d.ts.map +1 -1
- package/convex/http.js.map +1 -1
- package/convex/http.ts +611 -49
- package/convex/logs.ts +26 -22
- package/convex/migrateFilestack.d.ts.map +1 -1
- package/convex/migrateFilestack.js.map +1 -1
- package/convex/migrateFilestack.ts +65 -101
- package/convex/migratePartnersProd.d.ts.map +1 -0
- package/convex/migratePartnersProd.js.map +1 -0
- package/convex/migratePartnersProd.ts +174 -0
- package/convex/migrateProviderWorkspaces.d.ts.map +1 -0
- package/convex/migrateProviderWorkspaces.js.map +1 -0
- package/convex/migrateProviderWorkspaces.ts +175 -0
- package/convex/providers.js.map +1 -1
- package/convex/providers.ts +313 -203
- package/convex/schema.ts +50 -1
- package/convex/searchLogs.d.ts.map +1 -1
- package/convex/searchLogs.js.map +1 -1
- package/convex/searchLogs.ts +11 -3
- package/convex/seedPratham.ts +1 -1
- package/convex/spendAlerts.ts +2 -2
- package/convex/stripeActions.ts +1 -1
- package/convex/updateAPIStatus.ts +2 -3
- package/convex/workspaceSettings.ts +136 -0
- package/dist/bin-http.js +0 -0
- package/dist/bin.js +0 -0
- package/dist/cli/commands/demo.js +2 -2
- package/dist/cli/commands/doctor.js +1 -1
- package/dist/cli/commands/login.js +1 -1
- package/dist/cli/commands/setup.js +2 -2
- package/dist/discovery.js +1 -1
- package/dist/execute.js +3 -3
- package/dist/index.js +13 -13
- package/dist/registry/apis.json +1 -1
- package/dist/ui/errors.js +16 -16
- package/dist/ui/prompts.js +1 -1
- package/email-templates/filestack-provider-outreach.html +1 -1
- package/email-templates/partnership-template.html +1 -1
- package/email-templates/pratham-partnership-draft.html +2 -2
- package/package.json +2 -2
- package/reports/APIClaw-Session-Report-2026-04-05.pdf +0 -0
- package/reports/session-report-2026-04-05.html +433 -0
- package/src/cli/commands/demo.ts +2 -2
- package/src/cli/commands/doctor.ts +1 -1
- package/src/cli/commands/login.ts +1 -1
- package/src/cli/commands/setup.ts +2 -2
- package/src/discovery.ts +1 -1
- package/src/execute.ts +3 -3
- package/src/index.ts +14 -14
- package/src/registry/apis.json +1 -1
- package/src/ui/errors.ts +16 -16
- package/src/ui/prompts.ts +1 -1
- package/convex/adminActivate.d.ts +0 -3
- package/convex/adminActivate.js +0 -47
- package/convex/adminStats.d.ts +0 -9
- package/convex/adminStats.js +0 -280
- package/convex/agents.d.ts +0 -84
- package/convex/agents.js +0 -809
- package/convex/analytics.d.ts +0 -5
- package/convex/analytics.js +0 -166
- package/convex/billing.d.ts +0 -88
- package/convex/billing.js +0 -655
- package/convex/capabilities.d.ts +0 -9
- package/convex/capabilities.js +0 -145
- package/convex/chains.d.ts +0 -68
- package/convex/chains.js +0 -1105
- package/convex/credits.d.ts +0 -25
- package/convex/credits.js +0 -186
- package/convex/crons.d.ts +0 -3
- package/convex/crons.js +0 -17
- package/convex/directCall.d.ts +0 -72
- package/convex/directCall.js +0 -627
- package/convex/earnProgress.d.ts +0 -58
- package/convex/earnProgress.js +0 -649
- package/convex/email.d.ts +0 -14
- package/convex/email.js +0 -300
- package/convex/feedback.d.ts +0 -7
- package/convex/feedback.js +0 -227
- package/convex/http.d.ts +0 -3
- package/convex/http.js +0 -1405
- package/convex/inbound.d.ts +0 -2
- package/convex/inbound.js +0 -32
- package/convex/logs.d.ts +0 -48
- package/convex/logs.js +0 -621
- package/convex/migrateFilestack.d.ts +0 -2
- package/convex/migrateFilestack.js +0 -113
- package/convex/migratePratham.d.ts +0 -2
- package/convex/migratePratham.js +0 -121
- package/convex/mou.d.ts +0 -6
- package/convex/mou.js +0 -82
- package/convex/providerKeys.d.ts +0 -31
- package/convex/providerKeys.js +0 -257
- package/convex/providers.d.ts +0 -35
- package/convex/providers.js +0 -922
- package/convex/purchases.d.ts +0 -7
- package/convex/purchases.js +0 -157
- package/convex/ratelimit.d.ts +0 -4
- package/convex/ratelimit.js +0 -91
- package/convex/searchLogs.d.ts +0 -13
- package/convex/searchLogs.js +0 -232
- package/convex/seedAPILayerAPIs.d.ts +0 -7
- package/convex/seedAPILayerAPIs.js +0 -177
- package/convex/seedDirectCallConfigs.d.ts +0 -2
- package/convex/seedDirectCallConfigs.js +0 -324
- package/convex/seedPratham.d.ts +0 -6
- package/convex/seedPratham.js +0 -150
- package/convex/spendAlerts.d.ts +0 -36
- package/convex/spendAlerts.js +0 -380
- package/convex/stripeActions.d.ts +0 -19
- package/convex/stripeActions.js +0 -411
- package/convex/teams.d.ts +0 -21
- package/convex/teams.js +0 -215
- package/convex/telemetry.d.ts +0 -4
- package/convex/telemetry.js +0 -74
- package/convex/updateAPIStatus.d.ts +0 -6
- package/convex/updateAPIStatus.js +0 -40
- package/convex/usage.d.ts +0 -27
- package/convex/usage.js +0 -229
- package/convex/waitlist.d.ts +0 -4
- package/convex/waitlist.js +0 -49
- package/convex/webhooks.d.ts +0 -12
- package/convex/webhooks.js +0 -410
- package/convex/workspaces.d.ts +0 -33
- package/convex/workspaces.js +0 -991
package/convex/purchases.d.ts
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
export declare const purchaseAccess: any;
|
|
2
|
-
export declare const getAgentPurchases: any;
|
|
3
|
-
export declare const getActivePurchase: any;
|
|
4
|
-
export declare const getUsage: any;
|
|
5
|
-
export declare const recordUsage: any;
|
|
6
|
-
export declare const getBalanceSummary: any;
|
|
7
|
-
//# sourceMappingURL=purchases.d.ts.map
|
package/convex/purchases.js
DELETED
|
@@ -1,157 +0,0 @@
|
|
|
1
|
-
import { v } from "convex/values";
|
|
2
|
-
import { mutation, query } from "./_generated/server";
|
|
3
|
-
// Provider pricing (credits per dollar)
|
|
4
|
-
const CREDITS_PER_DOLLAR = {
|
|
5
|
-
"46elks": 30, // ~30 SMS per dollar
|
|
6
|
-
twilio: 25, // ~25 SMS per dollar
|
|
7
|
-
resend: 1000, // ~1000 emails per dollar
|
|
8
|
-
brave_search: 200, // ~200 searches per dollar
|
|
9
|
-
openrouter: 100, // ~100k tokens per dollar
|
|
10
|
-
elevenlabs: 3333, // ~3333 characters per dollar
|
|
11
|
-
};
|
|
12
|
-
// Calculate credits for a provider purchase
|
|
13
|
-
function calculateCredits(providerId, amountUsd) {
|
|
14
|
-
const rate = CREDITS_PER_DOLLAR[providerId] || 100;
|
|
15
|
-
return Math.floor(amountUsd * rate);
|
|
16
|
-
}
|
|
17
|
-
// Purchase API access
|
|
18
|
-
export const purchaseAccess = mutation({
|
|
19
|
-
args: {
|
|
20
|
-
agentId: v.string(),
|
|
21
|
-
providerId: v.string(),
|
|
22
|
-
amountUsd: v.number(),
|
|
23
|
-
credentials: v.any(), // Credentials passed from server side
|
|
24
|
-
},
|
|
25
|
-
handler: async (ctx, args) => {
|
|
26
|
-
// Check balance
|
|
27
|
-
const credits = await ctx.db
|
|
28
|
-
.query("agentCredits")
|
|
29
|
-
.withIndex("by_agentId", (q) => q.eq("agentId", args.agentId))
|
|
30
|
-
.first();
|
|
31
|
-
if (!credits || credits.balanceUsd < args.amountUsd) {
|
|
32
|
-
throw new Error(`Insufficient balance: have $${(credits?.balanceUsd || 0).toFixed(2)}, need $${args.amountUsd.toFixed(2)}`);
|
|
33
|
-
}
|
|
34
|
-
// Deduct credits
|
|
35
|
-
await ctx.db.patch(credits._id, {
|
|
36
|
-
balanceUsd: credits.balanceUsd - args.amountUsd,
|
|
37
|
-
updatedAt: Date.now(),
|
|
38
|
-
});
|
|
39
|
-
// Calculate credits granted
|
|
40
|
-
const creditsGranted = calculateCredits(args.providerId, args.amountUsd);
|
|
41
|
-
// Create purchase record
|
|
42
|
-
const purchaseId = await ctx.db.insert("purchases", {
|
|
43
|
-
agentId: args.agentId,
|
|
44
|
-
providerId: args.providerId,
|
|
45
|
-
amountUsd: args.amountUsd,
|
|
46
|
-
creditsGranted,
|
|
47
|
-
status: "active",
|
|
48
|
-
credentials: args.credentials,
|
|
49
|
-
createdAt: Date.now(),
|
|
50
|
-
});
|
|
51
|
-
// Initialize usage tracking
|
|
52
|
-
await ctx.db.insert("usage", {
|
|
53
|
-
purchaseId,
|
|
54
|
-
providerId: args.providerId,
|
|
55
|
-
unitsUsed: 0,
|
|
56
|
-
unitsRemaining: creditsGranted,
|
|
57
|
-
costIncurredUsd: 0,
|
|
58
|
-
lastUsedAt: Date.now(),
|
|
59
|
-
});
|
|
60
|
-
return await ctx.db.get(purchaseId);
|
|
61
|
-
},
|
|
62
|
-
});
|
|
63
|
-
// Get all purchases for an agent
|
|
64
|
-
export const getAgentPurchases = query({
|
|
65
|
-
args: { agentId: v.string() },
|
|
66
|
-
handler: async (ctx, args) => {
|
|
67
|
-
return await ctx.db
|
|
68
|
-
.query("purchases")
|
|
69
|
-
.withIndex("by_agentId", (q) => q.eq("agentId", args.agentId))
|
|
70
|
-
.collect();
|
|
71
|
-
},
|
|
72
|
-
});
|
|
73
|
-
// Get active purchase for a provider
|
|
74
|
-
export const getActivePurchase = query({
|
|
75
|
-
args: {
|
|
76
|
-
agentId: v.string(),
|
|
77
|
-
providerId: v.string(),
|
|
78
|
-
},
|
|
79
|
-
handler: async (ctx, args) => {
|
|
80
|
-
const purchases = await ctx.db
|
|
81
|
-
.query("purchases")
|
|
82
|
-
.withIndex("by_agentId_providerId", (q) => q.eq("agentId", args.agentId).eq("providerId", args.providerId))
|
|
83
|
-
.collect();
|
|
84
|
-
return purchases.find((p) => p.status === "active") || null;
|
|
85
|
-
},
|
|
86
|
-
});
|
|
87
|
-
// Get usage for a purchase
|
|
88
|
-
export const getUsage = query({
|
|
89
|
-
args: { purchaseId: v.id("purchases") },
|
|
90
|
-
handler: async (ctx, args) => {
|
|
91
|
-
return await ctx.db
|
|
92
|
-
.query("usage")
|
|
93
|
-
.withIndex("by_purchaseId", (q) => q.eq("purchaseId", args.purchaseId))
|
|
94
|
-
.first();
|
|
95
|
-
},
|
|
96
|
-
});
|
|
97
|
-
// Record usage
|
|
98
|
-
export const recordUsage = mutation({
|
|
99
|
-
args: {
|
|
100
|
-
purchaseId: v.id("purchases"),
|
|
101
|
-
unitsUsed: v.number(),
|
|
102
|
-
costUsd: v.number(),
|
|
103
|
-
},
|
|
104
|
-
handler: async (ctx, args) => {
|
|
105
|
-
const usage = await ctx.db
|
|
106
|
-
.query("usage")
|
|
107
|
-
.withIndex("by_purchaseId", (q) => q.eq("purchaseId", args.purchaseId))
|
|
108
|
-
.first();
|
|
109
|
-
if (!usage) {
|
|
110
|
-
throw new Error("Usage record not found");
|
|
111
|
-
}
|
|
112
|
-
const newUnitsRemaining = Math.max(0, usage.unitsRemaining - args.unitsUsed);
|
|
113
|
-
await ctx.db.patch(usage._id, {
|
|
114
|
-
unitsUsed: usage.unitsUsed + args.unitsUsed,
|
|
115
|
-
unitsRemaining: newUnitsRemaining,
|
|
116
|
-
costIncurredUsd: usage.costIncurredUsd + args.costUsd,
|
|
117
|
-
lastUsedAt: Date.now(),
|
|
118
|
-
});
|
|
119
|
-
// Update purchase status if depleted
|
|
120
|
-
if (newUnitsRemaining === 0) {
|
|
121
|
-
const purchase = await ctx.db.get(args.purchaseId);
|
|
122
|
-
if (purchase) {
|
|
123
|
-
await ctx.db.patch(args.purchaseId, { status: "exhausted" });
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
return await ctx.db
|
|
127
|
-
.query("usage")
|
|
128
|
-
.withIndex("by_purchaseId", (q) => q.eq("purchaseId", args.purchaseId))
|
|
129
|
-
.first();
|
|
130
|
-
},
|
|
131
|
-
});
|
|
132
|
-
// Get balance summary for an agent
|
|
133
|
-
export const getBalanceSummary = query({
|
|
134
|
-
args: { agentId: v.string() },
|
|
135
|
-
handler: async (ctx, args) => {
|
|
136
|
-
const credits = await ctx.db
|
|
137
|
-
.query("agentCredits")
|
|
138
|
-
.withIndex("by_agentId", (q) => q.eq("agentId", args.agentId))
|
|
139
|
-
.first();
|
|
140
|
-
const purchases = await ctx.db
|
|
141
|
-
.query("purchases")
|
|
142
|
-
.withIndex("by_agentId", (q) => q.eq("agentId", args.agentId))
|
|
143
|
-
.collect();
|
|
144
|
-
const activePurchases = purchases.filter((p) => p.status === "active");
|
|
145
|
-
const totalSpent = purchases.reduce((sum, p) => sum + p.amountUsd, 0);
|
|
146
|
-
return {
|
|
147
|
-
credits: credits || {
|
|
148
|
-
agentId: args.agentId,
|
|
149
|
-
balanceUsd: 0,
|
|
150
|
-
currency: "USD",
|
|
151
|
-
},
|
|
152
|
-
activePurchases,
|
|
153
|
-
totalSpentUsd: totalSpent,
|
|
154
|
-
};
|
|
155
|
-
},
|
|
156
|
-
});
|
|
157
|
-
//# sourceMappingURL=purchases.js.map
|
package/convex/ratelimit.d.ts
DELETED
package/convex/ratelimit.js
DELETED
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
import { mutation, query } from "./_generated/server";
|
|
2
|
-
import { v } from "convex/values";
|
|
3
|
-
// Rate limit config per tier
|
|
4
|
-
const LIMITS = {
|
|
5
|
-
free: {
|
|
6
|
-
discovery: 100, // searches per hour
|
|
7
|
-
instant: 10, // API calls per hour
|
|
8
|
-
},
|
|
9
|
-
subscriber: {
|
|
10
|
-
discovery: 1000,
|
|
11
|
-
instant: 100,
|
|
12
|
-
},
|
|
13
|
-
provider: {
|
|
14
|
-
discovery: 10000,
|
|
15
|
-
instant: 1000,
|
|
16
|
-
},
|
|
17
|
-
};
|
|
18
|
-
// Check and increment rate limit
|
|
19
|
-
export const checkLimit = mutation({
|
|
20
|
-
args: {
|
|
21
|
-
identifier: v.string(), // IP or agentId
|
|
22
|
-
action: v.union(v.literal("discovery"), v.literal("instant")),
|
|
23
|
-
tier: v.optional(v.union(v.literal("free"), v.literal("subscriber"), v.literal("provider"))),
|
|
24
|
-
},
|
|
25
|
-
handler: async (ctx, args) => {
|
|
26
|
-
const tier = args.tier || "free";
|
|
27
|
-
const limit = LIMITS[tier][args.action];
|
|
28
|
-
const hourKey = Math.floor(Date.now() / 3600000); // Hour bucket
|
|
29
|
-
const key = `${args.identifier}:${args.action}:${hourKey}`;
|
|
30
|
-
// Get current count
|
|
31
|
-
const existing = await ctx.db
|
|
32
|
-
.query("rateLimits")
|
|
33
|
-
.withIndex("by_key", (q) => q.eq("key", key))
|
|
34
|
-
.first();
|
|
35
|
-
if (existing) {
|
|
36
|
-
if (existing.count >= limit) {
|
|
37
|
-
return { allowed: false, remaining: 0, resetIn: 3600 - (Date.now() % 3600000) / 1000 };
|
|
38
|
-
}
|
|
39
|
-
await ctx.db.patch(existing._id, { count: existing.count + 1 });
|
|
40
|
-
return { allowed: true, remaining: limit - existing.count - 1 };
|
|
41
|
-
}
|
|
42
|
-
// Create new entry
|
|
43
|
-
await ctx.db.insert("rateLimits", {
|
|
44
|
-
key,
|
|
45
|
-
identifier: args.identifier,
|
|
46
|
-
action: args.action,
|
|
47
|
-
count: 1,
|
|
48
|
-
hourBucket: hourKey,
|
|
49
|
-
createdAt: Date.now(),
|
|
50
|
-
});
|
|
51
|
-
return { allowed: true, remaining: limit - 1 };
|
|
52
|
-
},
|
|
53
|
-
});
|
|
54
|
-
// Get current usage stats
|
|
55
|
-
export const getUsage = query({
|
|
56
|
-
args: {
|
|
57
|
-
identifier: v.string(),
|
|
58
|
-
},
|
|
59
|
-
handler: async (ctx, args) => {
|
|
60
|
-
const hourKey = Math.floor(Date.now() / 3600000);
|
|
61
|
-
const discovery = await ctx.db
|
|
62
|
-
.query("rateLimits")
|
|
63
|
-
.withIndex("by_key", (q) => q.eq("key", `${args.identifier}:discovery:${hourKey}`))
|
|
64
|
-
.first();
|
|
65
|
-
const instant = await ctx.db
|
|
66
|
-
.query("rateLimits")
|
|
67
|
-
.withIndex("by_key", (q) => q.eq("key", `${args.identifier}:instant:${hourKey}`))
|
|
68
|
-
.first();
|
|
69
|
-
return {
|
|
70
|
-
discovery: discovery?.count || 0,
|
|
71
|
-
instant: instant?.count || 0,
|
|
72
|
-
limits: LIMITS.free,
|
|
73
|
-
};
|
|
74
|
-
},
|
|
75
|
-
});
|
|
76
|
-
// Cleanup old rate limit entries (run via cron)
|
|
77
|
-
export const cleanup = mutation({
|
|
78
|
-
args: {},
|
|
79
|
-
handler: async (ctx) => {
|
|
80
|
-
const hourAgo = Math.floor(Date.now() / 3600000) - 2; // Keep 2 hours
|
|
81
|
-
const old = await ctx.db
|
|
82
|
-
.query("rateLimits")
|
|
83
|
-
.filter((q) => q.lt(q.field("hourBucket"), hourAgo))
|
|
84
|
-
.take(100);
|
|
85
|
-
for (const entry of old) {
|
|
86
|
-
await ctx.db.delete(entry._id);
|
|
87
|
-
}
|
|
88
|
-
return { deleted: old.length };
|
|
89
|
-
},
|
|
90
|
-
});
|
|
91
|
-
//# sourceMappingURL=ratelimit.js.map
|
package/convex/searchLogs.d.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Public mutation called by MCP server (path: searchLogs:log)
|
|
3
|
-
*/
|
|
4
|
-
export declare const log: any;
|
|
5
|
-
/**
|
|
6
|
-
* Get recent searches for a workspace (path: searchLogs:getRecent)
|
|
7
|
-
*/
|
|
8
|
-
export declare const getRecent: any;
|
|
9
|
-
export declare const logSearch: any;
|
|
10
|
-
export declare const getTopQueries: any;
|
|
11
|
-
export declare const getStats: any;
|
|
12
|
-
export declare const getZeroResultQueries: any;
|
|
13
|
-
//# sourceMappingURL=searchLogs.d.ts.map
|
package/convex/searchLogs.js
DELETED
|
@@ -1,232 +0,0 @@
|
|
|
1
|
-
import { internalMutation, mutation, query } from "./_generated/server";
|
|
2
|
-
import { v } from "convex/values";
|
|
3
|
-
/**
|
|
4
|
-
* Public mutation called by MCP server (path: searchLogs:log)
|
|
5
|
-
*/
|
|
6
|
-
export const log = mutation({
|
|
7
|
-
args: {
|
|
8
|
-
sessionToken: v.string(),
|
|
9
|
-
query: v.string(),
|
|
10
|
-
resultCount: v.number(),
|
|
11
|
-
matchedProviders: v.optional(v.array(v.string())),
|
|
12
|
-
responseTimeMs: v.optional(v.number()),
|
|
13
|
-
subagentId: v.optional(v.string()),
|
|
14
|
-
},
|
|
15
|
-
handler: async (ctx, args) => {
|
|
16
|
-
const session = await ctx.db
|
|
17
|
-
.query("agentSessions")
|
|
18
|
-
.withIndex("by_sessionToken", (q) => q.eq("sessionToken", args.sessionToken))
|
|
19
|
-
.first();
|
|
20
|
-
if (!session)
|
|
21
|
-
return { success: false };
|
|
22
|
-
await ctx.db.insert("searchLogs", {
|
|
23
|
-
workspaceId: session.workspaceId,
|
|
24
|
-
subagentId: args.subagentId,
|
|
25
|
-
query: args.query,
|
|
26
|
-
resultCount: args.resultCount,
|
|
27
|
-
hasResults: args.resultCount > 0,
|
|
28
|
-
matchedProviders: args.matchedProviders,
|
|
29
|
-
responseTimeMs: args.responseTimeMs || 0,
|
|
30
|
-
timestamp: Date.now(),
|
|
31
|
-
});
|
|
32
|
-
return { success: true };
|
|
33
|
-
},
|
|
34
|
-
});
|
|
35
|
-
/**
|
|
36
|
-
* Get recent searches for a workspace (path: searchLogs:getRecent)
|
|
37
|
-
*/
|
|
38
|
-
export const getRecent = query({
|
|
39
|
-
args: {
|
|
40
|
-
token: v.string(),
|
|
41
|
-
limit: v.optional(v.number()),
|
|
42
|
-
},
|
|
43
|
-
handler: async (ctx, { token, limit = 50 }) => {
|
|
44
|
-
const session = await ctx.db
|
|
45
|
-
.query("agentSessions")
|
|
46
|
-
.withIndex("by_sessionToken", (q) => q.eq("sessionToken", token))
|
|
47
|
-
.first();
|
|
48
|
-
if (!session)
|
|
49
|
-
return [];
|
|
50
|
-
const logs = await ctx.db
|
|
51
|
-
.query("searchLogs")
|
|
52
|
-
.withIndex("by_workspaceId", (q) => q.eq("workspaceId", session.workspaceId))
|
|
53
|
-
.order("desc")
|
|
54
|
-
.take(limit);
|
|
55
|
-
return logs.map((l) => ({
|
|
56
|
-
id: l._id,
|
|
57
|
-
query: l.query,
|
|
58
|
-
resultCount: l.resultCount,
|
|
59
|
-
hasResults: l.hasResults,
|
|
60
|
-
matchedProviders: l.matchedProviders || [],
|
|
61
|
-
responseTimeMs: l.responseTimeMs,
|
|
62
|
-
timestamp: l.timestamp,
|
|
63
|
-
subagentId: l.subagentId,
|
|
64
|
-
}));
|
|
65
|
-
},
|
|
66
|
-
});
|
|
67
|
-
// Log a search query (uses existing searchLogs table schema)
|
|
68
|
-
export const logSearch = internalMutation({
|
|
69
|
-
args: {
|
|
70
|
-
query: v.string(),
|
|
71
|
-
resultsCount: v.number(),
|
|
72
|
-
matchedProviders: v.optional(v.array(v.string())),
|
|
73
|
-
sessionToken: v.optional(v.string()),
|
|
74
|
-
userAgent: v.optional(v.string()),
|
|
75
|
-
responseTimeMs: v.optional(v.number()),
|
|
76
|
-
},
|
|
77
|
-
handler: async (ctx, args) => {
|
|
78
|
-
// Try to get workspaceId from session token
|
|
79
|
-
let workspaceId = undefined;
|
|
80
|
-
let subagentId = undefined;
|
|
81
|
-
if (args.sessionToken) {
|
|
82
|
-
try {
|
|
83
|
-
const token = args.sessionToken;
|
|
84
|
-
const session = await ctx.db
|
|
85
|
-
.query("agentSessions")
|
|
86
|
-
.withIndex("by_sessionToken", (q) => q.eq("sessionToken", token))
|
|
87
|
-
.first();
|
|
88
|
-
if (session) {
|
|
89
|
-
workspaceId = session.workspaceId;
|
|
90
|
-
// No agentId in agentSessions, subagentId stays undefined
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
catch (e) {
|
|
94
|
-
// Ignore - just skip workspace linking
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
// Only log if we have a workspace (existing schema requires it)
|
|
98
|
-
if (workspaceId) {
|
|
99
|
-
await ctx.db.insert("searchLogs", {
|
|
100
|
-
workspaceId,
|
|
101
|
-
subagentId,
|
|
102
|
-
query: args.query,
|
|
103
|
-
resultCount: args.resultsCount,
|
|
104
|
-
hasResults: args.resultsCount > 0,
|
|
105
|
-
matchedProviders: args.matchedProviders,
|
|
106
|
-
responseTimeMs: args.responseTimeMs || 0,
|
|
107
|
-
timestamp: Date.now(),
|
|
108
|
-
});
|
|
109
|
-
}
|
|
110
|
-
// TODO: Also log anonymous searches somewhere (for product insights)
|
|
111
|
-
},
|
|
112
|
-
});
|
|
113
|
-
// Get top search queries (for analytics)
|
|
114
|
-
export const getTopQueries = query({
|
|
115
|
-
args: {
|
|
116
|
-
limit: v.optional(v.number()),
|
|
117
|
-
since: v.optional(v.number()), // timestamp
|
|
118
|
-
},
|
|
119
|
-
handler: async (ctx, args) => {
|
|
120
|
-
const limit = args.limit || 50;
|
|
121
|
-
const since = args.since || Date.now() - 7 * 24 * 60 * 60 * 1000; // Last 7 days
|
|
122
|
-
const logs = await ctx.db
|
|
123
|
-
.query("searchLogs")
|
|
124
|
-
.withIndex("by_timestamp")
|
|
125
|
-
.filter((q) => q.gte(q.field("timestamp"), since))
|
|
126
|
-
.collect();
|
|
127
|
-
// Aggregate by query
|
|
128
|
-
const queryCounts = {};
|
|
129
|
-
for (const log of logs) {
|
|
130
|
-
const q = log.query.toLowerCase().trim();
|
|
131
|
-
if (!q)
|
|
132
|
-
continue;
|
|
133
|
-
if (!queryCounts[q]) {
|
|
134
|
-
queryCounts[q] = { count: 0, avgResults: 0, totalResults: 0 };
|
|
135
|
-
}
|
|
136
|
-
queryCounts[q].count++;
|
|
137
|
-
queryCounts[q].totalResults += log.resultCount;
|
|
138
|
-
}
|
|
139
|
-
// Calculate averages and sort
|
|
140
|
-
const sorted = Object.entries(queryCounts)
|
|
141
|
-
.map(([query, data]) => ({
|
|
142
|
-
query,
|
|
143
|
-
count: data.count,
|
|
144
|
-
avgResults: Math.round(data.totalResults / data.count * 10) / 10,
|
|
145
|
-
noResults: data.totalResults === 0,
|
|
146
|
-
}))
|
|
147
|
-
.sort((a, b) => b.count - a.count)
|
|
148
|
-
.slice(0, limit);
|
|
149
|
-
return {
|
|
150
|
-
queries: sorted,
|
|
151
|
-
totalSearches: logs.length,
|
|
152
|
-
uniqueQueries: Object.keys(queryCounts).length,
|
|
153
|
-
period: {
|
|
154
|
-
since: new Date(since).toISOString(),
|
|
155
|
-
until: new Date().toISOString(),
|
|
156
|
-
},
|
|
157
|
-
};
|
|
158
|
-
},
|
|
159
|
-
});
|
|
160
|
-
// Get search stats for a workspace (path: searchLogs:getStats)
|
|
161
|
-
export const getStats = query({
|
|
162
|
-
args: {
|
|
163
|
-
token: v.string(),
|
|
164
|
-
hoursBack: v.optional(v.number()),
|
|
165
|
-
},
|
|
166
|
-
handler: async (ctx, { token, hoursBack }) => {
|
|
167
|
-
const session = await ctx.db
|
|
168
|
-
.query("agentSessions")
|
|
169
|
-
.withIndex("by_sessionToken", (q) => q.eq("sessionToken", token))
|
|
170
|
-
.first();
|
|
171
|
-
if (!session)
|
|
172
|
-
return { totalSearches: 0, zeroResultSearches: 0, avgResponseTimeMs: 0, successRate: 0, byDay: [] };
|
|
173
|
-
const since = hoursBack ? Date.now() - hoursBack * 60 * 60 * 1000 : 0;
|
|
174
|
-
const logs = await ctx.db
|
|
175
|
-
.query("searchLogs")
|
|
176
|
-
.withIndex("by_workspaceId", (q) => q.eq("workspaceId", session.workspaceId))
|
|
177
|
-
.collect();
|
|
178
|
-
const filtered = since > 0 ? logs.filter((l) => l.timestamp >= since) : logs;
|
|
179
|
-
const totalSearches = filtered.length;
|
|
180
|
-
const zeroResultSearches = filtered.filter((l) => !l.hasResults).length;
|
|
181
|
-
const avgResponseTimeMs = totalSearches > 0
|
|
182
|
-
? Math.round(filtered.reduce((sum, l) => sum + (l.responseTimeMs || 0), 0) / totalSearches)
|
|
183
|
-
: 0;
|
|
184
|
-
const successRate = totalSearches > 0
|
|
185
|
-
? Math.round(((totalSearches - zeroResultSearches) / totalSearches) * 1000) / 10
|
|
186
|
-
: 0;
|
|
187
|
-
// Per-day breakdown for chart
|
|
188
|
-
const byDayMap = {};
|
|
189
|
-
for (const log of filtered) {
|
|
190
|
-
const day = new Date(log.timestamp).toISOString().split("T")[0];
|
|
191
|
-
byDayMap[day] = (byDayMap[day] || 0) + 1;
|
|
192
|
-
}
|
|
193
|
-
const byDay = Object.entries(byDayMap)
|
|
194
|
-
.map(([date, searches]) => ({ date, searches }))
|
|
195
|
-
.sort((a, b) => a.date.localeCompare(b.date));
|
|
196
|
-
return { totalSearches, zeroResultSearches, avgResponseTimeMs, successRate, byDay };
|
|
197
|
-
},
|
|
198
|
-
});
|
|
199
|
-
// Get searches with no results (API gaps)
|
|
200
|
-
export const getZeroResultQueries = query({
|
|
201
|
-
args: {
|
|
202
|
-
limit: v.optional(v.number()),
|
|
203
|
-
since: v.optional(v.number()),
|
|
204
|
-
},
|
|
205
|
-
handler: async (ctx, args) => {
|
|
206
|
-
const limit = args.limit || 20;
|
|
207
|
-
const since = args.since || Date.now() - 7 * 24 * 60 * 60 * 1000;
|
|
208
|
-
const logs = await ctx.db
|
|
209
|
-
.query("searchLogs")
|
|
210
|
-
.withIndex("by_hasResults")
|
|
211
|
-
.filter((q) => q.and(q.eq(q.field("hasResults"), false), q.gte(q.field("timestamp"), since)))
|
|
212
|
-
.collect();
|
|
213
|
-
// Aggregate
|
|
214
|
-
const queryCounts = {};
|
|
215
|
-
for (const log of logs) {
|
|
216
|
-
const q = log.query.toLowerCase().trim();
|
|
217
|
-
if (!q)
|
|
218
|
-
continue;
|
|
219
|
-
queryCounts[q] = (queryCounts[q] || 0) + 1;
|
|
220
|
-
}
|
|
221
|
-
const sorted = Object.entries(queryCounts)
|
|
222
|
-
.map(([query, count]) => ({ query, count }))
|
|
223
|
-
.sort((a, b) => b.count - a.count)
|
|
224
|
-
.slice(0, limit);
|
|
225
|
-
return {
|
|
226
|
-
gaps: sorted,
|
|
227
|
-
totalZeroResults: logs.length,
|
|
228
|
-
message: "These queries returned no results - potential APIs to add",
|
|
229
|
-
};
|
|
230
|
-
},
|
|
231
|
-
});
|
|
232
|
-
//# sourceMappingURL=searchLogs.js.map
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Seed all 27 APILayer APIs for a workspace
|
|
3
|
-
* Run with: npx convex run seedAPILayerAPIs:seedAll '{"email":"gustav_hemmingsson@hotmail.com"}'
|
|
4
|
-
*/
|
|
5
|
-
export declare const cleanAndReseed: any;
|
|
6
|
-
export declare const seedAll: any;
|
|
7
|
-
//# sourceMappingURL=seedAPILayerAPIs.d.ts.map
|