@vibe-cafe/vibe-usage 0.1.4 → 0.1.6
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/package.json +1 -1
- package/src/api.js +30 -4
- package/src/sync.js +13 -4
package/package.json
CHANGED
package/src/api.js
CHANGED
|
@@ -2,15 +2,39 @@ import https from 'node:https';
|
|
|
2
2
|
import http from 'node:http';
|
|
3
3
|
import { URL } from 'node:url';
|
|
4
4
|
|
|
5
|
+
const MAX_RETRIES = 3;
|
|
6
|
+
const INITIAL_DELAY = 1000;
|
|
7
|
+
|
|
5
8
|
/**
|
|
6
9
|
* POST buckets to the vibecafe ingest API.
|
|
7
10
|
* Uses native http/https — zero dependencies.
|
|
11
|
+
* Retries up to 3 times with exponential backoff on transient failures.
|
|
8
12
|
* @param {string} apiUrl - Base URL (e.g. "https://vibecafe.ai")
|
|
9
13
|
* @param {string} apiKey - Bearer token (vbu_xxx)
|
|
10
14
|
* @param {Array} buckets - Array of usage bucket objects
|
|
11
15
|
* @returns {Promise<{ingested: number}>}
|
|
12
16
|
*/
|
|
13
|
-
export function ingest(apiUrl, apiKey, buckets) {
|
|
17
|
+
export async function ingest(apiUrl, apiKey, buckets) {
|
|
18
|
+
let lastError;
|
|
19
|
+
for (let attempt = 0; attempt < MAX_RETRIES; attempt++) {
|
|
20
|
+
try {
|
|
21
|
+
return await _send(apiUrl, apiKey, buckets);
|
|
22
|
+
} catch (err) {
|
|
23
|
+
lastError = err;
|
|
24
|
+
// Don't retry auth errors or client errors
|
|
25
|
+
if (err.message === 'UNAUTHORIZED' || err.statusCode >= 400 && err.statusCode < 500) {
|
|
26
|
+
throw err;
|
|
27
|
+
}
|
|
28
|
+
if (attempt < MAX_RETRIES - 1) {
|
|
29
|
+
const delay = INITIAL_DELAY * 2 ** attempt;
|
|
30
|
+
await new Promise(r => setTimeout(r, delay));
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
throw lastError;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function _send(apiUrl, apiKey, buckets) {
|
|
14
38
|
return new Promise((resolve, reject) => {
|
|
15
39
|
const url = new URL('/api/usage/ingest', apiUrl);
|
|
16
40
|
const body = JSON.stringify({ buckets });
|
|
@@ -18,7 +42,7 @@ export function ingest(apiUrl, apiKey, buckets) {
|
|
|
18
42
|
|
|
19
43
|
const req = mod.request(url, {
|
|
20
44
|
method: 'POST',
|
|
21
|
-
timeout:
|
|
45
|
+
timeout: 60_000,
|
|
22
46
|
headers: {
|
|
23
47
|
'Content-Type': 'application/json',
|
|
24
48
|
'Authorization': `Bearer ${apiKey}`,
|
|
@@ -33,7 +57,9 @@ export function ingest(apiUrl, apiKey, buckets) {
|
|
|
33
57
|
return;
|
|
34
58
|
}
|
|
35
59
|
if (res.statusCode < 200 || res.statusCode >= 300) {
|
|
36
|
-
|
|
60
|
+
const err = new Error(`HTTP ${res.statusCode}: ${data}`);
|
|
61
|
+
err.statusCode = res.statusCode;
|
|
62
|
+
reject(err);
|
|
37
63
|
return;
|
|
38
64
|
}
|
|
39
65
|
try {
|
|
@@ -47,7 +73,7 @@ export function ingest(apiUrl, apiKey, buckets) {
|
|
|
47
73
|
req.on('error', (err) => reject(err));
|
|
48
74
|
req.on('timeout', () => {
|
|
49
75
|
req.destroy();
|
|
50
|
-
reject(new Error('Request timed out (
|
|
76
|
+
reject(new Error('Request timed out (60s)'));
|
|
51
77
|
});
|
|
52
78
|
req.write(body);
|
|
53
79
|
req.end();
|
package/src/sync.js
CHANGED
|
@@ -32,22 +32,31 @@ export async function runSync() {
|
|
|
32
32
|
|
|
33
33
|
const apiUrl = config.apiUrl || 'https://vibecafe.ai';
|
|
34
34
|
let totalIngested = 0;
|
|
35
|
+
const totalBatches = Math.ceil(allBuckets.length / BATCH_SIZE);
|
|
36
|
+
|
|
37
|
+
console.log(`Uploading ${allBuckets.length} buckets (${totalBatches} batch${totalBatches > 1 ? 'es' : ''})...`);
|
|
35
38
|
|
|
36
39
|
try {
|
|
37
40
|
for (let i = 0; i < allBuckets.length; i += BATCH_SIZE) {
|
|
38
41
|
const batch = allBuckets.slice(i, i + BATCH_SIZE);
|
|
42
|
+
const batchNum = Math.floor(i / BATCH_SIZE) + 1;
|
|
43
|
+
const uploaded = Math.min(i + BATCH_SIZE, allBuckets.length);
|
|
44
|
+
|
|
45
|
+
if (totalBatches > 1) {
|
|
46
|
+
process.stdout.write(` [${batchNum}/${totalBatches}] ${uploaded}/${allBuckets.length} buckets...\r`);
|
|
47
|
+
}
|
|
48
|
+
|
|
39
49
|
const result = await ingest(apiUrl, config.apiKey, batch);
|
|
40
50
|
totalIngested += result.ingested ?? batch.length;
|
|
41
51
|
|
|
42
52
|
// Save progress after each successful batch so partial uploads survive interruptions
|
|
43
53
|
config.lastSync = new Date().toISOString();
|
|
44
54
|
saveConfig(config);
|
|
45
|
-
|
|
46
|
-
if (allBuckets.length > BATCH_SIZE) {
|
|
47
|
-
process.stdout.write(` ${Math.min(i + BATCH_SIZE, allBuckets.length)}/${allBuckets.length} buckets...\r`);
|
|
48
|
-
}
|
|
49
55
|
}
|
|
50
56
|
|
|
57
|
+
if (totalBatches > 1) {
|
|
58
|
+
process.stdout.write('\n');
|
|
59
|
+
}
|
|
51
60
|
console.log(`Synced ${totalIngested} buckets.`);
|
|
52
61
|
return totalIngested;
|
|
53
62
|
} catch (err) {
|