@cultured_computer/sight 0.2.0 → 0.2.1
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/index.cjs +60 -4
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +22 -1
- package/dist/index.d.ts +22 -1
- package/dist/index.js +60 -4
- package/dist/index.js.map +1 -1
- package/package.json +13 -5
package/dist/index.cjs
CHANGED
|
@@ -5,6 +5,7 @@ var api = require('@opentelemetry/api');
|
|
|
5
5
|
|
|
6
6
|
// src/client.ts
|
|
7
7
|
var DEFAULT_DASHBOARD_URL = "https://field.cultured.computer/dashboard";
|
|
8
|
+
var MAX_PENDING_RETRIES = 50;
|
|
8
9
|
var IngestClient = class {
|
|
9
10
|
apiKey;
|
|
10
11
|
endpoint;
|
|
@@ -20,6 +21,15 @@ var IngestClient = class {
|
|
|
20
21
|
* arriving before any turn is not a captured session).
|
|
21
22
|
*/
|
|
22
23
|
firstCaptureAnnounced = false;
|
|
24
|
+
/**
|
|
25
|
+
* Rate-limit (429) handling: the first 429 per process prints one stderr
|
|
26
|
+
* warning (not debug-gated, same channel and lifetime as the first-capture
|
|
27
|
+
* announce); subsequent 429s only debug-log. Each rate-limited request is
|
|
28
|
+
* retried exactly once after Retry-After; pendingRetries counts retries
|
|
29
|
+
* waiting on their timers so the cap can shed load.
|
|
30
|
+
*/
|
|
31
|
+
rateLimitAnnounced = false;
|
|
32
|
+
pendingRetries = 0;
|
|
23
33
|
constructor(opts) {
|
|
24
34
|
this.apiKey = opts.apiKey;
|
|
25
35
|
this.endpoint = opts.endpoint.replace(/\/$/, "");
|
|
@@ -32,7 +42,7 @@ var IngestClient = class {
|
|
|
32
42
|
*/
|
|
33
43
|
async sendTurn(turn) {
|
|
34
44
|
try {
|
|
35
|
-
const
|
|
45
|
+
const send = () => fetch(`${this.endpoint}/v1/turns`, {
|
|
36
46
|
method: "POST",
|
|
37
47
|
headers: {
|
|
38
48
|
Authorization: `Bearer ${this.apiKey}`,
|
|
@@ -40,6 +50,8 @@ var IngestClient = class {
|
|
|
40
50
|
},
|
|
41
51
|
body: JSON.stringify(turn)
|
|
42
52
|
});
|
|
53
|
+
const response = await this.retryIfRateLimited(await send(), send);
|
|
54
|
+
if (response === null) return;
|
|
43
55
|
if (!response.ok) {
|
|
44
56
|
this.log(`[probe] ingest failed status=${response.status}`);
|
|
45
57
|
return;
|
|
@@ -65,7 +77,7 @@ var IngestClient = class {
|
|
|
65
77
|
*/
|
|
66
78
|
async sendFlag(deploymentSlug, flag) {
|
|
67
79
|
try {
|
|
68
|
-
const
|
|
80
|
+
const send = () => fetch(`${this.endpoint}/v1/flags`, {
|
|
69
81
|
method: "POST",
|
|
70
82
|
headers: {
|
|
71
83
|
Authorization: `Bearer ${this.apiKey}`,
|
|
@@ -79,6 +91,8 @@ var IngestClient = class {
|
|
|
79
91
|
turnIndex: flag.turnIndex
|
|
80
92
|
})
|
|
81
93
|
});
|
|
94
|
+
const response = await this.retryIfRateLimited(await send(), send);
|
|
95
|
+
if (response === null) return;
|
|
82
96
|
if (!response.ok) {
|
|
83
97
|
this.log(`[probe] flag ingest failed status=${response.status}`);
|
|
84
98
|
return;
|
|
@@ -97,7 +111,7 @@ var IngestClient = class {
|
|
|
97
111
|
*/
|
|
98
112
|
async sendSessionEnd(deploymentSlug, end) {
|
|
99
113
|
try {
|
|
100
|
-
const
|
|
114
|
+
const send = () => fetch(`${this.endpoint}/v1/sessions/end`, {
|
|
101
115
|
method: "POST",
|
|
102
116
|
headers: {
|
|
103
117
|
Authorization: `Bearer ${this.apiKey}`,
|
|
@@ -109,6 +123,8 @@ var IngestClient = class {
|
|
|
109
123
|
endedReason: end.endedReason
|
|
110
124
|
})
|
|
111
125
|
});
|
|
126
|
+
const response = await this.retryIfRateLimited(await send(), send);
|
|
127
|
+
if (response === null) return;
|
|
112
128
|
if (!response.ok) {
|
|
113
129
|
this.log(`[probe] session-end ingest failed status=${response.status}`);
|
|
114
130
|
return;
|
|
@@ -127,7 +143,7 @@ var IngestClient = class {
|
|
|
127
143
|
*/
|
|
128
144
|
async sendOutcomeEvent(deploymentSlug, event) {
|
|
129
145
|
try {
|
|
130
|
-
const
|
|
146
|
+
const send = () => fetch(`${this.endpoint}/v1/outcome-events`, {
|
|
131
147
|
method: "POST",
|
|
132
148
|
headers: {
|
|
133
149
|
Authorization: `Bearer ${this.apiKey}`,
|
|
@@ -142,6 +158,8 @@ var IngestClient = class {
|
|
|
142
158
|
detail: event.detail
|
|
143
159
|
})
|
|
144
160
|
});
|
|
161
|
+
const response = await this.retryIfRateLimited(await send(), send);
|
|
162
|
+
if (response === null) return;
|
|
145
163
|
if (!response.ok) {
|
|
146
164
|
this.log(`[probe] outcome-event ingest failed status=${response.status}`);
|
|
147
165
|
return;
|
|
@@ -156,6 +174,44 @@ var IngestClient = class {
|
|
|
156
174
|
this.log(`[probe] outcome-event ingest error: ${msg}`);
|
|
157
175
|
}
|
|
158
176
|
}
|
|
177
|
+
/**
|
|
178
|
+
* Route a send response through the 429 retry policy. Non-429 responses
|
|
179
|
+
* pass through untouched. A 429 is re-sent exactly once after Retry-After
|
|
180
|
+
* seconds (default 1s when the header is missing or unparseable; clamped
|
|
181
|
+
* to [1, 60]s); the retry's response is returned as-is, so a second 429 is
|
|
182
|
+
* never retried again — callers log and drop it like any other failed
|
|
183
|
+
* send. Returns null when the retry was dropped at the pending-retry cap.
|
|
184
|
+
* The timer is unref'd (where supported) so a pending retry never holds
|
|
185
|
+
* the process open.
|
|
186
|
+
*/
|
|
187
|
+
async retryIfRateLimited(response, resend) {
|
|
188
|
+
if (response.status !== 429) return response;
|
|
189
|
+
if (!this.rateLimitAnnounced) {
|
|
190
|
+
this.rateLimitAnnounced = true;
|
|
191
|
+
console.error(
|
|
192
|
+
"Sight: ingest rate limit (429) \u2014 retrying once per Retry-After; sustained limiting drops captures. See https://field.cultured.computer/sight/sdk-reference#rate-limits"
|
|
193
|
+
);
|
|
194
|
+
} else {
|
|
195
|
+
this.log("[probe] ingest rate limited (429), retrying once");
|
|
196
|
+
}
|
|
197
|
+
if (this.pendingRetries >= MAX_PENDING_RETRIES) {
|
|
198
|
+
this.log("[probe] retry buffer full, dropped");
|
|
199
|
+
return null;
|
|
200
|
+
}
|
|
201
|
+
const raw = response.headers.get("retry-after");
|
|
202
|
+
const parsed = raw === null ? Number.NaN : Number(raw);
|
|
203
|
+
const seconds = Number.isFinite(parsed) ? Math.min(Math.max(parsed, 1), 60) : 1;
|
|
204
|
+
this.pendingRetries += 1;
|
|
205
|
+
try {
|
|
206
|
+
await new Promise((resolve) => {
|
|
207
|
+
const timer = setTimeout(resolve, seconds * 1e3);
|
|
208
|
+
timer.unref?.();
|
|
209
|
+
});
|
|
210
|
+
return await resend();
|
|
211
|
+
} finally {
|
|
212
|
+
this.pendingRetries -= 1;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
159
215
|
log(msg) {
|
|
160
216
|
if (this.debug) {
|
|
161
217
|
console.error(msg);
|