@nordsym/apiclaw 1.3.6 → 1.3.8
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 +422 -169
- package/convex/_generated/api.d.ts +16 -0
- package/convex/agents.ts +403 -0
- package/convex/billing.ts +651 -216
- package/convex/crons.ts +17 -0
- package/convex/directCall.ts +80 -0
- package/convex/earnProgress.ts +753 -0
- package/convex/email.ts +135 -82
- package/convex/feedback.ts +265 -0
- package/convex/http.ts +80 -4
- package/convex/logs.ts +304 -0
- package/convex/providerKeys.ts +289 -0
- package/convex/providers.ts +18 -0
- package/convex/schema.ts +185 -1
- package/convex/stripeActions.ts +512 -0
- package/convex/webhooks.ts +494 -0
- package/convex/workspaces.ts +158 -3
- package/dist/adapters/base.d.ts +112 -0
- package/dist/adapters/base.d.ts.map +1 -0
- package/dist/adapters/base.js +247 -0
- package/dist/adapters/base.js.map +1 -0
- package/dist/adapters/claude-desktop.d.ts +12 -0
- package/dist/adapters/claude-desktop.d.ts.map +1 -0
- package/dist/adapters/claude-desktop.js +36 -0
- package/dist/adapters/claude-desktop.js.map +1 -0
- package/dist/adapters/cline.d.ts +20 -0
- package/dist/adapters/cline.d.ts.map +1 -0
- package/dist/adapters/cline.js +77 -0
- package/dist/adapters/cline.js.map +1 -0
- package/dist/adapters/continue.d.ts +26 -0
- package/dist/adapters/continue.d.ts.map +1 -0
- package/dist/adapters/continue.js +68 -0
- package/dist/adapters/continue.js.map +1 -0
- package/dist/adapters/cursor.d.ts +12 -0
- package/dist/adapters/cursor.d.ts.map +1 -0
- package/dist/adapters/cursor.js +38 -0
- package/dist/adapters/cursor.js.map +1 -0
- package/dist/adapters/custom.d.ts +47 -0
- package/dist/adapters/custom.d.ts.map +1 -0
- package/dist/adapters/custom.js +146 -0
- package/dist/adapters/custom.js.map +1 -0
- package/dist/adapters/detect.d.ts +69 -0
- package/dist/adapters/detect.d.ts.map +1 -0
- package/dist/adapters/detect.js +158 -0
- package/dist/adapters/detect.js.map +1 -0
- package/dist/adapters/index.d.ts +21 -0
- package/dist/adapters/index.d.ts.map +1 -0
- package/dist/adapters/index.js +23 -0
- package/dist/adapters/index.js.map +1 -0
- package/dist/adapters/windsurf.d.ts +12 -0
- package/dist/adapters/windsurf.d.ts.map +1 -0
- package/dist/adapters/windsurf.js +39 -0
- package/dist/adapters/windsurf.js.map +1 -0
- package/dist/bin.d.ts +9 -0
- package/dist/bin.d.ts.map +1 -0
- package/dist/bin.js +19 -0
- package/dist/bin.js.map +1 -0
- package/dist/cli/commands/doctor.d.ts +34 -0
- package/dist/cli/commands/doctor.d.ts.map +1 -0
- package/dist/cli/commands/doctor.js +312 -0
- package/dist/cli/commands/doctor.js.map +1 -0
- package/dist/cli/commands/index.d.ts +9 -0
- package/dist/cli/commands/index.d.ts.map +1 -0
- package/dist/cli/commands/index.js +9 -0
- package/dist/cli/commands/index.js.map +1 -0
- package/dist/cli/commands/restore.d.ts +50 -0
- package/dist/cli/commands/restore.d.ts.map +1 -0
- package/dist/cli/commands/restore.js +260 -0
- package/dist/cli/commands/restore.js.map +1 -0
- package/dist/cli/commands/setup.d.ts +19 -0
- package/dist/cli/commands/setup.d.ts.map +1 -0
- package/dist/cli/commands/setup.js +206 -0
- package/dist/cli/commands/setup.js.map +1 -0
- package/dist/cli/commands/uninstall.d.ts +37 -0
- package/dist/cli/commands/uninstall.d.ts.map +1 -0
- package/dist/cli/commands/uninstall.js +189 -0
- package/dist/cli/commands/uninstall.js.map +1 -0
- package/dist/cli/index.d.ts +7 -0
- package/dist/cli/index.d.ts.map +1 -0
- package/dist/cli/index.js +97 -0
- package/dist/cli/index.js.map +1 -0
- package/dist/discovery.d.ts +6 -2
- package/dist/discovery.d.ts.map +1 -1
- package/dist/discovery.js +296 -2
- package/dist/discovery.js.map +1 -1
- package/dist/enterprise/env.d.ts +56 -0
- package/dist/enterprise/env.d.ts.map +1 -0
- package/dist/enterprise/env.js +124 -0
- package/dist/enterprise/env.js.map +1 -0
- package/dist/enterprise/index.d.ts +7 -0
- package/dist/enterprise/index.d.ts.map +1 -0
- package/dist/enterprise/index.js +7 -0
- package/dist/enterprise/index.js.map +1 -0
- package/dist/enterprise/script-generator.d.ts +32 -0
- package/dist/enterprise/script-generator.d.ts.map +1 -0
- package/dist/enterprise/script-generator.js +461 -0
- package/dist/enterprise/script-generator.js.map +1 -0
- package/dist/execute.d.ts +21 -0
- package/dist/execute.d.ts.map +1 -1
- package/dist/execute.js +231 -0
- package/dist/execute.js.map +1 -1
- package/dist/index.js +257 -7
- 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/dist/types.d.ts +29 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/ui/colors.d.ts +111 -0
- package/dist/ui/colors.d.ts.map +1 -0
- package/dist/ui/colors.js +185 -0
- package/dist/ui/colors.js.map +1 -0
- package/dist/ui/errors.d.ts +69 -0
- package/dist/ui/errors.d.ts.map +1 -0
- package/dist/ui/errors.js +334 -0
- package/dist/ui/errors.js.map +1 -0
- package/dist/ui/index.d.ts +10 -0
- package/dist/ui/index.d.ts.map +1 -0
- package/dist/ui/index.js +14 -0
- package/dist/ui/index.js.map +1 -0
- package/dist/ui/prompts.d.ts +88 -0
- package/dist/ui/prompts.d.ts.map +1 -0
- package/dist/ui/prompts.js +295 -0
- package/dist/ui/prompts.js.map +1 -0
- package/dist/ui/spinner.d.ts +112 -0
- package/dist/ui/spinner.d.ts.map +1 -0
- package/dist/ui/spinner.js +229 -0
- package/dist/ui/spinner.js.map +1 -0
- package/dist/utils/backup.d.ts +48 -0
- package/dist/utils/backup.d.ts.map +1 -0
- package/dist/utils/backup.js +182 -0
- package/dist/utils/backup.js.map +1 -0
- package/dist/utils/config.d.ts +80 -0
- package/dist/utils/config.d.ts.map +1 -0
- package/dist/utils/config.js +221 -0
- package/dist/utils/config.js.map +1 -0
- package/dist/utils/os.d.ts +45 -0
- package/dist/utils/os.d.ts.map +1 -0
- package/dist/utils/os.js +106 -0
- package/dist/utils/os.js.map +1 -0
- package/dist/utils/paths.d.ts +38 -0
- package/dist/utils/paths.d.ts.map +1 -0
- package/dist/utils/paths.js +160 -0
- package/dist/utils/paths.js.map +1 -0
- package/docs/PRD-BILLING.md +226 -0
- package/docs/PRD-EARN-SYSTEM.md +261 -0
- package/docs/PRD-MCP-AUTO-SETUP.md +623 -0
- 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/docs/enterprise-deployment.md +728 -0
- package/landing/next.config.mjs +14 -0
- package/landing/public/stats.json +4 -2
- package/landing/scripts/generate-stats.js +12 -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/api/workspace-auth/magic-link/route.ts +6 -3
- package/landing/src/app/auth/verify/page.tsx +31 -9
- package/landing/src/app/docs/page.tsx +1 -1
- package/landing/src/app/earn/page.tsx +6 -6
- package/landing/src/app/join/page.tsx +49 -0
- package/landing/src/app/login/page.tsx +8 -2
- package/landing/src/app/page.tsx +81 -96
- package/landing/src/app/providers/dashboard/page.tsx +1 -1
- package/landing/src/app/providers/register/page.tsx +1 -1
- package/landing/src/app/workspace/page.tsx +3269 -534
- package/landing/src/components/CheckoutButton.tsx +188 -0
- package/landing/src/components/EarnCreditsTab.tsx +842 -0
- package/landing/src/components/Toast.tsx +84 -0
- package/landing/src/lib/stats.json +3 -1
- package/package.json +9 -2
- package/src/adapters/base.ts +363 -0
- package/src/adapters/claude-desktop.ts +41 -0
- package/src/adapters/cline.ts +88 -0
- package/src/adapters/continue.ts +91 -0
- package/src/adapters/cursor.ts +43 -0
- package/src/adapters/custom.ts +188 -0
- package/src/adapters/detect.ts +202 -0
- package/src/adapters/index.ts +47 -0
- package/src/adapters/windsurf.ts +44 -0
- package/src/bin.ts +19 -0
- package/src/cli/commands/doctor.ts +367 -0
- package/src/cli/commands/index.ts +9 -0
- package/src/cli/commands/restore.ts +333 -0
- package/src/cli/commands/setup.ts +276 -0
- package/src/cli/commands/uninstall.ts +240 -0
- package/src/cli/index.ts +107 -0
- package/src/discovery.ts +328 -3
- package/src/enterprise/env.ts +156 -0
- package/src/enterprise/index.ts +7 -0
- package/src/enterprise/script-generator.ts +481 -0
- package/src/execute.ts +256 -0
- package/src/index.ts +290 -7
- package/src/metered.ts +149 -0
- package/src/stripe.ts +253 -0
- package/src/types.ts +32 -0
- package/src/ui/colors.ts +219 -0
- package/src/ui/errors.ts +394 -0
- package/src/ui/index.ts +17 -0
- package/src/ui/prompts.ts +390 -0
- package/src/ui/spinner.ts +325 -0
- package/src/utils/backup.ts +224 -0
- package/src/utils/config.ts +315 -0
- package/src/utils/os.ts +124 -0
- package/src/utils/paths.ts +203 -0
- package/landing/tsconfig.tsbuildinfo +0 -1
|
@@ -8,19 +8,27 @@
|
|
|
8
8
|
* @module
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
|
+
import type * as agents from "../agents.js";
|
|
11
12
|
import type * as analytics from "../analytics.js";
|
|
12
13
|
import type * as billing from "../billing.js";
|
|
13
14
|
import type * as capabilities from "../capabilities.js";
|
|
14
15
|
import type * as credits from "../credits.js";
|
|
16
|
+
import type * as crons from "../crons.js";
|
|
15
17
|
import type * as directCall from "../directCall.js";
|
|
18
|
+
import type * as earnProgress from "../earnProgress.js";
|
|
16
19
|
import type * as email from "../email.js";
|
|
20
|
+
import type * as feedback from "../feedback.js";
|
|
17
21
|
import type * as http from "../http.js";
|
|
22
|
+
import type * as logs from "../logs.js";
|
|
23
|
+
import type * as providerKeys from "../providerKeys.js";
|
|
18
24
|
import type * as providers from "../providers.js";
|
|
19
25
|
import type * as purchases from "../purchases.js";
|
|
20
26
|
import type * as ratelimit from "../ratelimit.js";
|
|
27
|
+
import type * as stripeActions from "../stripeActions.js";
|
|
21
28
|
import type * as telemetry from "../telemetry.js";
|
|
22
29
|
import type * as usage from "../usage.js";
|
|
23
30
|
import type * as waitlist from "../waitlist.js";
|
|
31
|
+
import type * as webhooks from "../webhooks.js";
|
|
24
32
|
import type * as workspaces from "../workspaces.js";
|
|
25
33
|
|
|
26
34
|
import type {
|
|
@@ -30,19 +38,27 @@ import type {
|
|
|
30
38
|
} from "convex/server";
|
|
31
39
|
|
|
32
40
|
declare const fullApi: ApiFromModules<{
|
|
41
|
+
agents: typeof agents;
|
|
33
42
|
analytics: typeof analytics;
|
|
34
43
|
billing: typeof billing;
|
|
35
44
|
capabilities: typeof capabilities;
|
|
36
45
|
credits: typeof credits;
|
|
46
|
+
crons: typeof crons;
|
|
37
47
|
directCall: typeof directCall;
|
|
48
|
+
earnProgress: typeof earnProgress;
|
|
38
49
|
email: typeof email;
|
|
50
|
+
feedback: typeof feedback;
|
|
39
51
|
http: typeof http;
|
|
52
|
+
logs: typeof logs;
|
|
53
|
+
providerKeys: typeof providerKeys;
|
|
40
54
|
providers: typeof providers;
|
|
41
55
|
purchases: typeof purchases;
|
|
42
56
|
ratelimit: typeof ratelimit;
|
|
57
|
+
stripeActions: typeof stripeActions;
|
|
43
58
|
telemetry: typeof telemetry;
|
|
44
59
|
usage: typeof usage;
|
|
45
60
|
waitlist: typeof waitlist;
|
|
61
|
+
webhooks: typeof webhooks;
|
|
46
62
|
workspaces: typeof workspaces;
|
|
47
63
|
}>;
|
|
48
64
|
|
package/convex/agents.ts
ADDED
|
@@ -0,0 +1,403 @@
|
|
|
1
|
+
import { v } from "convex/values";
|
|
2
|
+
import { mutation, query, internalMutation } from "./_generated/server";
|
|
3
|
+
|
|
4
|
+
// ============================================
|
|
5
|
+
// AGENT NAME GENERATION
|
|
6
|
+
// ============================================
|
|
7
|
+
|
|
8
|
+
const ADJECTIVES = [
|
|
9
|
+
"Crimson", "Azure", "Golden", "Silver", "Obsidian",
|
|
10
|
+
"Emerald", "Sapphire", "Violet", "Amber", "Jade",
|
|
11
|
+
"Scarlet", "Cobalt", "Onyx", "Ruby", "Pearl",
|
|
12
|
+
"Shadow", "Storm", "Frost", "Blaze", "Thunder",
|
|
13
|
+
"Swift", "Silent", "Bright", "Dark", "Wild",
|
|
14
|
+
"Noble", "Fierce", "Cosmic", "Quantum", "Neural",
|
|
15
|
+
];
|
|
16
|
+
|
|
17
|
+
const NOUNS = [
|
|
18
|
+
"Phoenix", "Falcon", "Dragon", "Wolf", "Raven",
|
|
19
|
+
"Serpent", "Tiger", "Hawk", "Panther", "Lynx",
|
|
20
|
+
"Cipher", "Vector", "Prism", "Nexus", "Core",
|
|
21
|
+
"Agent", "Oracle", "Sentinel", "Phantom", "Vanguard",
|
|
22
|
+
"Forge", "Spark", "Pulse", "Echo", "Byte",
|
|
23
|
+
"Matrix", "Vertex", "Helix", "Nova", "Zenith",
|
|
24
|
+
];
|
|
25
|
+
|
|
26
|
+
function generateAgentName(): string {
|
|
27
|
+
const adj = ADJECTIVES[Math.floor(Math.random() * ADJECTIVES.length)];
|
|
28
|
+
const noun = NOUNS[Math.floor(Math.random() * NOUNS.length)];
|
|
29
|
+
return `${adj} ${noun}`;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function generateUUID(): string {
|
|
33
|
+
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
|
|
34
|
+
const r = Math.random() * 16 | 0;
|
|
35
|
+
const v = c === 'x' ? r : (r & 0x3 | 0x8);
|
|
36
|
+
return v.toString(16);
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// ============================================
|
|
41
|
+
// MAIN AGENT QUERIES
|
|
42
|
+
// ============================================
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Get main agent info for a workspace
|
|
46
|
+
*/
|
|
47
|
+
export const getMainAgent = query({
|
|
48
|
+
args: { token: v.string() },
|
|
49
|
+
handler: async (ctx, { token }) => {
|
|
50
|
+
const session = await ctx.db
|
|
51
|
+
.query("agentSessions")
|
|
52
|
+
.withIndex("by_sessionToken", (q) => q.eq("sessionToken", token))
|
|
53
|
+
.first();
|
|
54
|
+
|
|
55
|
+
if (!session) {
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const workspace = await ctx.db.get(session.workspaceId);
|
|
60
|
+
if (!workspace) {
|
|
61
|
+
return null;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return {
|
|
65
|
+
workspaceId: workspace._id,
|
|
66
|
+
email: workspace.email,
|
|
67
|
+
mainAgentId: workspace.mainAgentId || null,
|
|
68
|
+
mainAgentName: workspace.mainAgentName || null,
|
|
69
|
+
usageCount: workspace.usageCount,
|
|
70
|
+
createdAt: workspace.createdAt,
|
|
71
|
+
};
|
|
72
|
+
},
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Rename the main agent
|
|
77
|
+
*/
|
|
78
|
+
export const renameMainAgent = mutation({
|
|
79
|
+
args: {
|
|
80
|
+
token: v.string(),
|
|
81
|
+
name: v.string(),
|
|
82
|
+
},
|
|
83
|
+
handler: async (ctx, { token, name }) => {
|
|
84
|
+
const session = await ctx.db
|
|
85
|
+
.query("agentSessions")
|
|
86
|
+
.withIndex("by_sessionToken", (q) => q.eq("sessionToken", token))
|
|
87
|
+
.first();
|
|
88
|
+
|
|
89
|
+
if (!session) {
|
|
90
|
+
throw new Error("Invalid session");
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const workspace = await ctx.db.get(session.workspaceId);
|
|
94
|
+
if (!workspace) {
|
|
95
|
+
throw new Error("Workspace not found");
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// Validate name length
|
|
99
|
+
const trimmedName = name.trim();
|
|
100
|
+
if (trimmedName.length < 2 || trimmedName.length > 50) {
|
|
101
|
+
throw new Error("Name must be between 2 and 50 characters");
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
await ctx.db.patch(workspace._id, {
|
|
105
|
+
mainAgentName: trimmedName,
|
|
106
|
+
updatedAt: Date.now(),
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
return { success: true, name: trimmedName };
|
|
110
|
+
},
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Initialize main agent (auto-generate name and ID if not set)
|
|
115
|
+
* Called on first API call
|
|
116
|
+
*/
|
|
117
|
+
export const ensureMainAgent = mutation({
|
|
118
|
+
args: { workspaceId: v.id("workspaces") },
|
|
119
|
+
handler: async (ctx, { workspaceId }) => {
|
|
120
|
+
const workspace = await ctx.db.get(workspaceId);
|
|
121
|
+
if (!workspace) {
|
|
122
|
+
throw new Error("Workspace not found");
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// Already initialized
|
|
126
|
+
if (workspace.mainAgentId && workspace.mainAgentName) {
|
|
127
|
+
return {
|
|
128
|
+
mainAgentId: workspace.mainAgentId,
|
|
129
|
+
mainAgentName: workspace.mainAgentName,
|
|
130
|
+
created: false,
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const mainAgentId = workspace.mainAgentId || generateUUID();
|
|
135
|
+
const mainAgentName = workspace.mainAgentName || generateAgentName();
|
|
136
|
+
|
|
137
|
+
await ctx.db.patch(workspaceId, {
|
|
138
|
+
mainAgentId,
|
|
139
|
+
mainAgentName,
|
|
140
|
+
updatedAt: Date.now(),
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
return {
|
|
144
|
+
mainAgentId,
|
|
145
|
+
mainAgentName,
|
|
146
|
+
created: true,
|
|
147
|
+
};
|
|
148
|
+
},
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
// ============================================
|
|
152
|
+
// SUBAGENT QUERIES
|
|
153
|
+
// ============================================
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Get all subagents for a workspace
|
|
157
|
+
*/
|
|
158
|
+
export const getSubagents = query({
|
|
159
|
+
args: {
|
|
160
|
+
token: v.string(),
|
|
161
|
+
limit: v.optional(v.number()),
|
|
162
|
+
},
|
|
163
|
+
handler: async (ctx, { token, limit = 50 }) => {
|
|
164
|
+
const session = await ctx.db
|
|
165
|
+
.query("agentSessions")
|
|
166
|
+
.withIndex("by_sessionToken", (q) => q.eq("sessionToken", token))
|
|
167
|
+
.first();
|
|
168
|
+
|
|
169
|
+
if (!session) {
|
|
170
|
+
return { subagents: [], total: 0 };
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
const subagents = await ctx.db
|
|
174
|
+
.query("subagents")
|
|
175
|
+
.withIndex("by_workspaceId", (q) => q.eq("workspaceId", session.workspaceId))
|
|
176
|
+
.order("desc")
|
|
177
|
+
.take(limit);
|
|
178
|
+
|
|
179
|
+
// Sort by lastActiveAt descending
|
|
180
|
+
const sorted = subagents.sort((a, b) => b.lastActiveAt - a.lastActiveAt);
|
|
181
|
+
|
|
182
|
+
return {
|
|
183
|
+
subagents: sorted.map((s) => ({
|
|
184
|
+
id: s._id,
|
|
185
|
+
subagentId: s.subagentId,
|
|
186
|
+
name: s.name || s.subagentId,
|
|
187
|
+
callCount: s.callCount,
|
|
188
|
+
firstSeenAt: s.firstSeenAt,
|
|
189
|
+
lastActiveAt: s.lastActiveAt,
|
|
190
|
+
})),
|
|
191
|
+
total: subagents.length,
|
|
192
|
+
};
|
|
193
|
+
},
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Get stats for a specific subagent
|
|
198
|
+
*/
|
|
199
|
+
export const getSubagentStats = query({
|
|
200
|
+
args: {
|
|
201
|
+
token: v.string(),
|
|
202
|
+
subagentId: v.string(),
|
|
203
|
+
},
|
|
204
|
+
handler: async (ctx, { token, subagentId }) => {
|
|
205
|
+
const session = await ctx.db
|
|
206
|
+
.query("agentSessions")
|
|
207
|
+
.withIndex("by_sessionToken", (q) => q.eq("sessionToken", token))
|
|
208
|
+
.first();
|
|
209
|
+
|
|
210
|
+
if (!session) {
|
|
211
|
+
return null;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
const subagent = await ctx.db
|
|
215
|
+
.query("subagents")
|
|
216
|
+
.withIndex("by_workspaceId_subagentId", (q) =>
|
|
217
|
+
q.eq("workspaceId", session.workspaceId).eq("subagentId", subagentId)
|
|
218
|
+
)
|
|
219
|
+
.first();
|
|
220
|
+
|
|
221
|
+
if (!subagent) {
|
|
222
|
+
return null;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
// Get recent logs for this subagent
|
|
226
|
+
const logs = await ctx.db
|
|
227
|
+
.query("apiLogs")
|
|
228
|
+
.withIndex("by_subagentId", (q) => q.eq("subagentId", subagentId))
|
|
229
|
+
.order("desc")
|
|
230
|
+
.take(100);
|
|
231
|
+
|
|
232
|
+
const successCount = logs.filter((l) => l.status === "success").length;
|
|
233
|
+
const errorCount = logs.filter((l) => l.status === "error").length;
|
|
234
|
+
const avgLatency = logs.length > 0
|
|
235
|
+
? Math.round(logs.reduce((sum, l) => sum + l.latencyMs, 0) / logs.length)
|
|
236
|
+
: 0;
|
|
237
|
+
|
|
238
|
+
// Group by provider
|
|
239
|
+
const byProvider: Record<string, number> = {};
|
|
240
|
+
for (const log of logs) {
|
|
241
|
+
byProvider[log.provider] = (byProvider[log.provider] || 0) + 1;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
return {
|
|
245
|
+
subagentId: subagent.subagentId,
|
|
246
|
+
name: subagent.name || subagent.subagentId,
|
|
247
|
+
callCount: subagent.callCount,
|
|
248
|
+
successCount,
|
|
249
|
+
errorCount,
|
|
250
|
+
successRate: logs.length > 0 ? Math.round((successCount / logs.length) * 100) : 0,
|
|
251
|
+
avgLatency,
|
|
252
|
+
firstSeenAt: subagent.firstSeenAt,
|
|
253
|
+
lastActiveAt: subagent.lastActiveAt,
|
|
254
|
+
byProvider: Object.entries(byProvider)
|
|
255
|
+
.map(([provider, count]) => ({ provider, count }))
|
|
256
|
+
.sort((a, b) => b.count - a.count),
|
|
257
|
+
};
|
|
258
|
+
},
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* Rename a subagent
|
|
263
|
+
*/
|
|
264
|
+
export const renameSubagent = mutation({
|
|
265
|
+
args: {
|
|
266
|
+
token: v.string(),
|
|
267
|
+
subagentId: v.string(),
|
|
268
|
+
name: v.string(),
|
|
269
|
+
},
|
|
270
|
+
handler: async (ctx, { token, subagentId, name }) => {
|
|
271
|
+
const session = await ctx.db
|
|
272
|
+
.query("agentSessions")
|
|
273
|
+
.withIndex("by_sessionToken", (q) => q.eq("sessionToken", token))
|
|
274
|
+
.first();
|
|
275
|
+
|
|
276
|
+
if (!session) {
|
|
277
|
+
throw new Error("Invalid session");
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
const subagent = await ctx.db
|
|
281
|
+
.query("subagents")
|
|
282
|
+
.withIndex("by_workspaceId_subagentId", (q) =>
|
|
283
|
+
q.eq("workspaceId", session.workspaceId).eq("subagentId", subagentId)
|
|
284
|
+
)
|
|
285
|
+
.first();
|
|
286
|
+
|
|
287
|
+
if (!subagent) {
|
|
288
|
+
throw new Error("Subagent not found");
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
const trimmedName = name.trim();
|
|
292
|
+
if (trimmedName.length < 1 || trimmedName.length > 100) {
|
|
293
|
+
throw new Error("Name must be between 1 and 100 characters");
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
await ctx.db.patch(subagent._id, { name: trimmedName });
|
|
297
|
+
|
|
298
|
+
return { success: true, name: trimmedName };
|
|
299
|
+
},
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* Track a subagent call (upsert subagent record)
|
|
304
|
+
* Called when X-APIClaw-Subagent header is present
|
|
305
|
+
*/
|
|
306
|
+
export const trackSubagentCall = mutation({
|
|
307
|
+
args: {
|
|
308
|
+
workspaceId: v.id("workspaces"),
|
|
309
|
+
subagentId: v.string(),
|
|
310
|
+
},
|
|
311
|
+
handler: async (ctx, { workspaceId, subagentId }) => {
|
|
312
|
+
const now = Date.now();
|
|
313
|
+
|
|
314
|
+
// Find existing subagent record
|
|
315
|
+
const existing = await ctx.db
|
|
316
|
+
.query("subagents")
|
|
317
|
+
.withIndex("by_workspaceId_subagentId", (q) =>
|
|
318
|
+
q.eq("workspaceId", workspaceId).eq("subagentId", subagentId)
|
|
319
|
+
)
|
|
320
|
+
.first();
|
|
321
|
+
|
|
322
|
+
if (existing) {
|
|
323
|
+
// Increment call count
|
|
324
|
+
await ctx.db.patch(existing._id, {
|
|
325
|
+
callCount: existing.callCount + 1,
|
|
326
|
+
lastActiveAt: now,
|
|
327
|
+
});
|
|
328
|
+
return { id: existing._id, created: false };
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
// Create new subagent record
|
|
332
|
+
const id = await ctx.db.insert("subagents", {
|
|
333
|
+
workspaceId,
|
|
334
|
+
subagentId,
|
|
335
|
+
callCount: 1,
|
|
336
|
+
firstSeenAt: now,
|
|
337
|
+
lastActiveAt: now,
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
return { id, created: true };
|
|
341
|
+
},
|
|
342
|
+
});
|
|
343
|
+
|
|
344
|
+
// ============================================
|
|
345
|
+
// AGGREGATE STATS
|
|
346
|
+
// ============================================
|
|
347
|
+
|
|
348
|
+
/**
|
|
349
|
+
* Get agent overview for workspace (main + subagents summary)
|
|
350
|
+
*/
|
|
351
|
+
export const getAgentOverview = query({
|
|
352
|
+
args: { token: v.string() },
|
|
353
|
+
handler: async (ctx, { token }) => {
|
|
354
|
+
const session = await ctx.db
|
|
355
|
+
.query("agentSessions")
|
|
356
|
+
.withIndex("by_sessionToken", (q) => q.eq("sessionToken", token))
|
|
357
|
+
.first();
|
|
358
|
+
|
|
359
|
+
if (!session) {
|
|
360
|
+
return null;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
const workspace = await ctx.db.get(session.workspaceId);
|
|
364
|
+
if (!workspace) {
|
|
365
|
+
return null;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
// Get all subagents
|
|
369
|
+
const subagents = await ctx.db
|
|
370
|
+
.query("subagents")
|
|
371
|
+
.withIndex("by_workspaceId", (q) => q.eq("workspaceId", session.workspaceId))
|
|
372
|
+
.collect();
|
|
373
|
+
|
|
374
|
+
// Calculate totals
|
|
375
|
+
const totalSubagentCalls = subagents.reduce((sum, s) => sum + s.callCount, 0);
|
|
376
|
+
const mainAgentCalls = workspace.usageCount - totalSubagentCalls;
|
|
377
|
+
|
|
378
|
+
// Get most active subagents (top 5)
|
|
379
|
+
const topSubagents = subagents
|
|
380
|
+
.sort((a, b) => b.callCount - a.callCount)
|
|
381
|
+
.slice(0, 5)
|
|
382
|
+
.map((s) => ({
|
|
383
|
+
subagentId: s.subagentId,
|
|
384
|
+
name: s.name || s.subagentId,
|
|
385
|
+
callCount: s.callCount,
|
|
386
|
+
lastActiveAt: s.lastActiveAt,
|
|
387
|
+
}));
|
|
388
|
+
|
|
389
|
+
return {
|
|
390
|
+
mainAgent: {
|
|
391
|
+
id: workspace.mainAgentId || null,
|
|
392
|
+
name: workspace.mainAgentName || "Unnamed Agent",
|
|
393
|
+
callCount: Math.max(0, mainAgentCalls), // Ensure non-negative
|
|
394
|
+
},
|
|
395
|
+
subagents: {
|
|
396
|
+
total: subagents.length,
|
|
397
|
+
totalCalls: totalSubagentCalls,
|
|
398
|
+
topActive: topSubagents,
|
|
399
|
+
},
|
|
400
|
+
totalCalls: workspace.usageCount,
|
|
401
|
+
};
|
|
402
|
+
},
|
|
403
|
+
});
|