@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.
- package/dist/bin.js +1 -1
- package/dist/cli/commands/mcp-install.js +6 -6
- 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/commands/mcp-install.ts +6 -6
- 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
|
+
}
|
package/dist/src/bin.js
ADDED
|
@@ -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
|
+
}
|