@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,129 @@
1
+ /**
2
+ * APIClaw Analytics - Track all API usage
3
+ * Both Direct Call and Open API calls are logged here
4
+ */
5
+ import { appendFileSync, existsSync, mkdirSync } from 'fs';
6
+ import { homedir } from 'os';
7
+ import { join } from 'path';
8
+ import { ConvexHttpClient } from 'convex/browser';
9
+ import { api } from '../convex/_generated/api.js';
10
+ // Log directory
11
+ const LOG_DIR = join(homedir(), '.apiclaw', 'logs');
12
+ const LOG_FILE = join(LOG_DIR, 'api-calls.jsonl');
13
+ // Convex client (lazy init)
14
+ let convexClient = null;
15
+ function getConvexClient() {
16
+ if (convexClient)
17
+ return convexClient;
18
+ const convexUrl = process.env.APICLAW_CONVEX_URL || process.env.NEXT_PUBLIC_CONVEX_URL;
19
+ if (!convexUrl)
20
+ return null;
21
+ try {
22
+ convexClient = new ConvexHttpClient(convexUrl);
23
+ return convexClient;
24
+ }
25
+ catch (e) {
26
+ console.error('[APIClaw Analytics] Failed to init Convex client:', e);
27
+ return null;
28
+ }
29
+ }
30
+ // Ensure log directory exists
31
+ function ensureLogDir() {
32
+ if (!existsSync(LOG_DIR)) {
33
+ mkdirSync(LOG_DIR, { recursive: true });
34
+ }
35
+ }
36
+ /**
37
+ * Log an API call to local file AND Convex
38
+ */
39
+ export function logAPICall(log) {
40
+ // 1. Local file (existing behavior)
41
+ try {
42
+ ensureLogDir();
43
+ const line = JSON.stringify(log) + '\n';
44
+ appendFileSync(LOG_FILE, line);
45
+ }
46
+ catch (e) {
47
+ console.error('[APIClaw Analytics] Failed to log to file:', e);
48
+ }
49
+ // 2. Send to Convex (new - includes anonymous usage)
50
+ sendToConvex(log).catch(() => { }); // Fire and forget
51
+ }
52
+ /**
53
+ * Send analytics event to Convex
54
+ * Works for both authenticated and anonymous users
55
+ */
56
+ async function sendToConvex(log) {
57
+ const client = getConvexClient();
58
+ if (!client)
59
+ return;
60
+ try {
61
+ await client.mutation(api.analytics.log, {
62
+ event: 'api_call',
63
+ provider: log.provider,
64
+ query: log.action, // Store action as query field
65
+ identifier: log.userId || 'anonymous',
66
+ metadata: {
67
+ type: log.type,
68
+ success: log.success,
69
+ latencyMs: log.latencyMs,
70
+ error: log.error,
71
+ timestamp: log.timestamp,
72
+ ...log.metadata, // Include product and any other metadata
73
+ },
74
+ });
75
+ }
76
+ catch (e) {
77
+ // Silent fail - don't break API calls for logging errors
78
+ console.error('[APIClaw Analytics] Failed to send to Convex:', e);
79
+ }
80
+ }
81
+ /**
82
+ * Webhook for real-time analytics (optional)
83
+ * Set APICLAW_ANALYTICS_WEBHOOK env var to enable
84
+ */
85
+ export async function sendToWebhook(log) {
86
+ const webhookUrl = process.env.APICLAW_ANALYTICS_WEBHOOK;
87
+ if (!webhookUrl)
88
+ return;
89
+ try {
90
+ await fetch(webhookUrl, {
91
+ method: 'POST',
92
+ headers: { 'Content-Type': 'application/json' },
93
+ body: JSON.stringify(log),
94
+ });
95
+ }
96
+ catch (e) {
97
+ // Silent fail
98
+ }
99
+ }
100
+ /**
101
+ * Track API call with timing
102
+ */
103
+ export async function trackAPICall(provider, action, type, userId, fn) {
104
+ const start = Date.now();
105
+ let success = true;
106
+ let error;
107
+ try {
108
+ return await fn();
109
+ }
110
+ catch (e) {
111
+ success = false;
112
+ error = e.message;
113
+ throw e;
114
+ }
115
+ finally {
116
+ const log = {
117
+ timestamp: new Date().toISOString(),
118
+ provider,
119
+ action,
120
+ type,
121
+ userId,
122
+ success,
123
+ latencyMs: Date.now() - start,
124
+ error,
125
+ };
126
+ logAPICall(log);
127
+ sendToWebhook(log).catch(() => { }); // Fire and forget
128
+ }
129
+ }
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * APIClaw Entry Point
4
+ *
5
+ * - No args or MCP-related args → Run MCP server
6
+ * - setup/doctor/restore/uninstall → Run CLI
7
+ */
8
+ const cliCommands = ['setup', 'mcp-install', 'mcp-uninstall', 'doctor', 'restore', 'uninstall', 'help', '--help', '-h', '--version', '-V'];
9
+ const firstArg = process.argv[2];
10
+ if (!firstArg || !cliCommands.includes(firstArg)) {
11
+ // Run MCP server
12
+ import('./index.js');
13
+ }
14
+ else {
15
+ // Run CLI
16
+ import('./cli/index.js');
17
+ }
@@ -0,0 +1,240 @@
1
+ /**
2
+ * APIClaw Capability Router
3
+ * Routes capability requests to the best available provider
4
+ */
5
+ import { executeAPICall } from './execute.js';
6
+ import { logAPICall } from './analytics.js';
7
+ // Convex HTTP API for capability queries
8
+ const CONVEX_URL = process.env.NEXT_PUBLIC_CONVEX_URL || 'https://adventurous-avocet-799.convex.cloud';
9
+ /**
10
+ * Query Convex for providers that support a capability
11
+ */
12
+ async function getProvidersForCapability(capabilityId, region) {
13
+ try {
14
+ const res = await fetch(`${CONVEX_URL}/api/query`, {
15
+ method: 'POST',
16
+ headers: { 'Content-Type': 'application/json' },
17
+ body: JSON.stringify({
18
+ path: 'capabilities:getProviders',
19
+ args: { capabilityId, region },
20
+ }),
21
+ });
22
+ if (!res.ok)
23
+ return [];
24
+ const data = await res.json();
25
+ if (Array.isArray(data))
26
+ return data;
27
+ return (data.value || []);
28
+ }
29
+ catch (e) {
30
+ console.error('Failed to fetch capability providers:', e);
31
+ return [];
32
+ }
33
+ }
34
+ /**
35
+ * Map capability params to provider-specific params
36
+ */
37
+ function mapParams(params, mapping) {
38
+ const result = {};
39
+ for (const [capParam, value] of Object.entries(params)) {
40
+ const providerParam = mapping[capParam] || capParam;
41
+ result[providerParam] = value;
42
+ }
43
+ return result;
44
+ }
45
+ /**
46
+ * Log capability usage to Convex
47
+ */
48
+ async function logCapabilityUsage(params) {
49
+ try {
50
+ await fetch(`${CONVEX_URL}/api/mutation`, {
51
+ method: 'POST',
52
+ headers: { 'Content-Type': 'application/json' },
53
+ body: JSON.stringify({
54
+ path: 'capabilities:logUsage',
55
+ args: params,
56
+ }),
57
+ });
58
+ }
59
+ catch (e) {
60
+ console.error('Failed to log capability usage:', e);
61
+ }
62
+ }
63
+ /**
64
+ * Execute a capability request with automatic provider selection and fallback
65
+ */
66
+ export async function executeCapability(capabilityId, action, params, userId, preferences = {}) {
67
+ const startTime = Date.now();
68
+ const enableFallback = preferences.fallback !== false; // Default true
69
+ // Get providers for this capability
70
+ const providers = await getProvidersForCapability(capabilityId, preferences.region);
71
+ if (providers.length === 0) {
72
+ return {
73
+ success: false,
74
+ capability: capabilityId,
75
+ action,
76
+ fallbackAttempted: false,
77
+ error: `No providers available for capability: ${capabilityId}`,
78
+ };
79
+ }
80
+ // Filter by max price if specified
81
+ let filteredProviders = providers;
82
+ if (preferences.maxPrice !== undefined) {
83
+ filteredProviders = providers.filter(p => p.pricePerUnit <= preferences.maxPrice);
84
+ }
85
+ // Prefer specific provider if requested
86
+ if (preferences.preferredProvider) {
87
+ const preferred = filteredProviders.find(p => p.providerId === preferences.preferredProvider);
88
+ if (preferred) {
89
+ filteredProviders = [preferred, ...filteredProviders.filter(p => p.providerId !== preferences.preferredProvider)];
90
+ }
91
+ }
92
+ if (filteredProviders.length === 0) {
93
+ return {
94
+ success: false,
95
+ capability: capabilityId,
96
+ action,
97
+ fallbackAttempted: false,
98
+ error: 'No providers match your preferences (region/price)',
99
+ };
100
+ }
101
+ // Try providers in order
102
+ let fallbackAttempted = false;
103
+ let lastError = '';
104
+ for (let i = 0; i < filteredProviders.length; i++) {
105
+ const provider = filteredProviders[i];
106
+ const isFirstAttempt = i === 0;
107
+ if (!isFirstAttempt) {
108
+ fallbackAttempted = true;
109
+ }
110
+ try {
111
+ // Map params to provider-specific format
112
+ const mappedParams = mapParams(params, provider.paramMapping || {});
113
+ // Execute via existing executeAPICall
114
+ const result = await executeAPICall(provider.providerId, action, mappedParams, userId);
115
+ const latencyMs = Date.now() - startTime;
116
+ if (result.success) {
117
+ // Log successful usage
118
+ logCapabilityUsage({
119
+ capabilityId,
120
+ providerId: provider.providerId,
121
+ userId,
122
+ action,
123
+ success: true,
124
+ fallbackUsed: fallbackAttempted,
125
+ fallbackReason: fallbackAttempted ? lastError : undefined,
126
+ latencyMs,
127
+ cost: provider.pricePerUnit,
128
+ currency: provider.currency,
129
+ });
130
+ // Also log to file-based analytics
131
+ logAPICall({
132
+ timestamp: new Date().toISOString(),
133
+ provider: provider.providerId,
134
+ action,
135
+ type: 'direct',
136
+ userId,
137
+ success: true,
138
+ latencyMs,
139
+ });
140
+ return {
141
+ success: true,
142
+ capability: capabilityId,
143
+ action,
144
+ providerUsed: provider.providerId,
145
+ fallbackAttempted,
146
+ fallbackReason: fallbackAttempted ? lastError : undefined,
147
+ data: result.data,
148
+ cost: provider.pricePerUnit,
149
+ currency: provider.currency,
150
+ latencyMs,
151
+ };
152
+ }
153
+ // Provider returned error, try next
154
+ lastError = result.error || 'Unknown error';
155
+ if (!enableFallback) {
156
+ break;
157
+ }
158
+ }
159
+ catch (e) {
160
+ lastError = e.message || 'Provider execution failed';
161
+ if (!enableFallback) {
162
+ break;
163
+ }
164
+ }
165
+ }
166
+ // All providers failed
167
+ const latencyMs = Date.now() - startTime;
168
+ logCapabilityUsage({
169
+ capabilityId,
170
+ providerId: filteredProviders[0].providerId,
171
+ userId,
172
+ action,
173
+ success: false,
174
+ fallbackUsed: fallbackAttempted,
175
+ fallbackReason: lastError,
176
+ latencyMs,
177
+ cost: 0,
178
+ currency: 'SEK',
179
+ });
180
+ return {
181
+ success: false,
182
+ capability: capabilityId,
183
+ action,
184
+ fallbackAttempted,
185
+ error: `All providers failed. Last error: ${lastError}`,
186
+ latencyMs,
187
+ };
188
+ }
189
+ /**
190
+ * List available capabilities
191
+ */
192
+ export async function listCapabilities() {
193
+ try {
194
+ const res = await fetch(`${CONVEX_URL}/api/query`, {
195
+ method: 'POST',
196
+ headers: { 'Content-Type': 'application/json' },
197
+ body: JSON.stringify({
198
+ path: 'capabilities:list',
199
+ args: {},
200
+ }),
201
+ });
202
+ if (!res.ok)
203
+ return [];
204
+ const data = await res.json();
205
+ const capabilities = Array.isArray(data) ? data : (data.value || []);
206
+ return capabilities.map(c => ({
207
+ id: c.id,
208
+ name: c.name,
209
+ category: c.category,
210
+ }));
211
+ }
212
+ catch (e) {
213
+ return [];
214
+ }
215
+ }
216
+ /**
217
+ * Check if a capability exists
218
+ */
219
+ export async function hasCapability(capabilityId) {
220
+ try {
221
+ const res = await fetch(`${CONVEX_URL}/api/query`, {
222
+ method: 'POST',
223
+ headers: { 'Content-Type': 'application/json' },
224
+ body: JSON.stringify({
225
+ path: 'capabilities:getById',
226
+ args: { id: capabilityId },
227
+ }),
228
+ });
229
+ if (!res.ok)
230
+ return false;
231
+ const data = await res.json();
232
+ if (data && typeof data === 'object' && 'value' in data) {
233
+ return !!data.value;
234
+ }
235
+ return !!data;
236
+ }
237
+ catch (e) {
238
+ return false;
239
+ }
240
+ }