@telemai/sdk 0.5.0 → 0.7.0
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 +6 -0
- package/dist/{chunk-SQ3ZZA72.js → chunk-TST6J3VA.js} +32 -8
- package/dist/index.cjs +315 -22
- package/dist/index.d.cts +335 -7
- package/dist/index.d.ts +335 -7
- package/dist/index.js +284 -15
- package/dist/vercel/index.cjs +10 -4
- package/dist/vercel/index.d.cts +6 -1
- package/dist/vercel/index.d.ts +6 -1
- package/dist/vercel/index.js +11 -5
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -392,6 +392,12 @@ try {
|
|
|
392
392
|
| Other 4xx | `TelemError` with the status and the code from the server, not retried |
|
|
393
393
|
| Attempt longer than `timeoutMs` | `TimeoutError`, not retried |
|
|
394
394
|
|
|
395
|
+
`monitors.create` sends an `Idempotency-Key` header, a new one per call unless you
|
|
396
|
+
pass `idempotencyKey`. It retries a 429, a 409 `idempotency_in_progress`, a network
|
|
397
|
+
failure and a timeout for up to about 2 minutes, with the same key. For 24 hours, a
|
|
398
|
+
retry with the same key returns the first monitor instead of creating a second one.
|
|
399
|
+
A different API key makes a new monitor. A 5xx is not retried.
|
|
400
|
+
|
|
395
401
|
See [Resources → Errors](https://docs.telem.ai/resources/errors/) for how the plugins show a failed call.
|
|
396
402
|
|
|
397
403
|
## Embedding and edge runtimes
|
|
@@ -22,6 +22,7 @@ var TimeoutError = class extends TelemError {
|
|
|
22
22
|
var MAX_DELAY_MS = 3e4;
|
|
23
23
|
var MAX_TIMEOUT_MS = 2 ** 31 - 1;
|
|
24
24
|
var BASE_DELAY_MS = 500;
|
|
25
|
+
var MAX_IDEMPOTENT_BACKOFF_MS = 1e4;
|
|
25
26
|
function isObject(value) {
|
|
26
27
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
27
28
|
}
|
|
@@ -100,6 +101,11 @@ function connectionError(url, cause) {
|
|
|
100
101
|
function isRetryable(status) {
|
|
101
102
|
return status >= 500 || status === 429;
|
|
102
103
|
}
|
|
104
|
+
function shouldRetry(mode, status, body) {
|
|
105
|
+
if (mode === "all") return isRetryable(status);
|
|
106
|
+
if (status === 429) return true;
|
|
107
|
+
return mode === "idempotent" && status === 409 && codeFromBody(body) === "idempotency_in_progress";
|
|
108
|
+
}
|
|
103
109
|
function backoff(attempt) {
|
|
104
110
|
return Math.min(BASE_DELAY_MS * 2 ** attempt, MAX_DELAY_MS);
|
|
105
111
|
}
|
|
@@ -109,9 +115,20 @@ function retryDelay(retryAfter, attempt) {
|
|
|
109
115
|
}
|
|
110
116
|
return backoff(attempt);
|
|
111
117
|
}
|
|
112
|
-
|
|
118
|
+
function jittered(ms) {
|
|
119
|
+
return ms + Math.random() * Math.max(1e3, ms / 2);
|
|
120
|
+
}
|
|
121
|
+
function idempotentDelay(retryAfter, attempt) {
|
|
122
|
+
if (retryAfter !== null && /^\s*\d+\s*$/.test(retryAfter)) return jittered(Number(retryAfter) * 1e3);
|
|
123
|
+
return Math.min(jittered(BASE_DELAY_MS * 2 ** attempt), MAX_IDEMPOTENT_BACKOFF_MS);
|
|
124
|
+
}
|
|
125
|
+
async function request(config, method, path, body, abort, retry = "all", extra = {}) {
|
|
113
126
|
const url = `${config.baseUrl}/${path.replace(/^\/+/, "")}`;
|
|
114
|
-
const
|
|
127
|
+
const maxRetries = extra.maxRetries ?? config.maxRetries;
|
|
128
|
+
const started = Date.now();
|
|
129
|
+
const fits = (ms) => Date.now() - started + ms <= (extra.budgetMs ?? Infinity);
|
|
130
|
+
const delay = (retryAfter, attempt) => retry === "idempotent" ? idempotentDelay(retryAfter, attempt) : retryDelay(retryAfter, attempt);
|
|
131
|
+
const headers = { ...extra.headers, "User-Agent": config.userAgent };
|
|
115
132
|
if (config.apiKey) headers.Authorization = `Bearer ${config.apiKey}`;
|
|
116
133
|
const init = { method, headers };
|
|
117
134
|
if (body !== void 0) {
|
|
@@ -171,20 +188,27 @@ async function request(config, method, path, body, abort, retry = "all") {
|
|
|
171
188
|
});
|
|
172
189
|
}
|
|
173
190
|
if (abort?.aborted) throw abort.reason ?? cause;
|
|
191
|
+
const wait = delay(null, attempt);
|
|
174
192
|
if (signal?.aborted) {
|
|
193
|
+
if (retry === "idempotent" && attempt < maxRetries && fits(wait)) {
|
|
194
|
+
await pause(wait);
|
|
195
|
+
continue;
|
|
196
|
+
}
|
|
175
197
|
throw new TimeoutError(`request to ${url} timed out after ${config.timeoutMs}ms`, cause);
|
|
176
198
|
}
|
|
177
|
-
if (retry === "rate_limit" || attempt >=
|
|
178
|
-
await pause(
|
|
199
|
+
if (retry === "rate_limit" || attempt >= maxRetries || !fits(wait)) throw connectionError(url, cause);
|
|
200
|
+
await pause(wait);
|
|
179
201
|
continue;
|
|
180
202
|
} finally {
|
|
181
203
|
signal?.removeEventListener("abort", onAbort2);
|
|
182
204
|
}
|
|
183
205
|
if (received.status >= 200 && received.status < 300) return received.body;
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
206
|
+
if (shouldRetry(retry, received.status, received.body) && attempt < maxRetries) {
|
|
207
|
+
const wait = delay(received.retryAfter, attempt);
|
|
208
|
+
if (fits(wait)) {
|
|
209
|
+
await pause(wait);
|
|
210
|
+
continue;
|
|
211
|
+
}
|
|
188
212
|
}
|
|
189
213
|
throw statusError(received.status, received.body);
|
|
190
214
|
}
|
package/dist/index.cjs
CHANGED
|
@@ -60,12 +60,14 @@ var TimeoutError = class extends TelemError {
|
|
|
60
60
|
};
|
|
61
61
|
|
|
62
62
|
// src/version.ts
|
|
63
|
-
var VERSION = "0.
|
|
63
|
+
var VERSION = "0.7.0";
|
|
64
64
|
|
|
65
65
|
// src/config.ts
|
|
66
66
|
var DEFAULT_BASE_URL = "https://router.telem.ai";
|
|
67
67
|
var DEFAULT_TIMEOUT_MS = 6e4;
|
|
68
68
|
var DEFAULT_MAX_RETRIES = 2;
|
|
69
|
+
var CREATE_RETRY_BUDGET_MS = 12e4;
|
|
70
|
+
var DEFAULT_CREATE_MAX_RETRIES = 60;
|
|
69
71
|
var BLANK = /^[ \t\n\r\v\f\u00a0\ufeff\u3000]+|[ \t\n\r\v\f\u00a0\ufeff\u3000]+$/g;
|
|
70
72
|
function trimBlank(value) {
|
|
71
73
|
return value.replace(BLANK, "");
|
|
@@ -128,6 +130,12 @@ function resolveConfig(options) {
|
|
|
128
130
|
apiKey = apiKey ?? credentials.apiKey;
|
|
129
131
|
baseUrl = baseUrl ?? credentials.baseUrl;
|
|
130
132
|
}
|
|
133
|
+
if (opts.defaultVertical !== void 0 && opts.vertical !== void 0 && opts.defaultVertical !== opts.vertical) {
|
|
134
|
+
throw invalidOption(
|
|
135
|
+
`vertical ${JSON.stringify(opts.vertical)} and defaultVertical ${JSON.stringify(opts.defaultVertical)} are the same option; pass one of them`
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
const vertical = opts.defaultVertical ?? opts.vertical ?? (trimBlank(env.TELEM_VERTICAL ?? "") || void 0);
|
|
131
139
|
const resolvedBaseUrl = (baseUrl || DEFAULT_BASE_URL).replace(/\/+$/, "");
|
|
132
140
|
let scheme;
|
|
133
141
|
try {
|
|
@@ -143,6 +151,7 @@ function resolveConfig(options) {
|
|
|
143
151
|
baseUrl: resolvedBaseUrl,
|
|
144
152
|
timeoutMs,
|
|
145
153
|
maxRetries,
|
|
154
|
+
createMaxRetries: opts.maxRetries ?? DEFAULT_CREATE_MAX_RETRIES,
|
|
146
155
|
userAgent: opts.userAgent || `telem-sdk-js/${VERSION}`,
|
|
147
156
|
fetch: send,
|
|
148
157
|
sleep: opts.sleep ?? ((ms) => new Promise((resolve) => setTimeout(resolve, ms))),
|
|
@@ -158,7 +167,8 @@ function resolveConfig(options) {
|
|
|
158
167
|
includeFullContent: opts.includeFullContent ?? (env.TELEM_FULL_CONTENT === "1" || void 0),
|
|
159
168
|
autoRouting: opts.autoRouting ?? (trimBlank(env.TELEM_AUTO_ROUTING ?? "") || void 0),
|
|
160
169
|
maxRoutingProviders: opts.maxRoutingProviders ?? intEnv(env.TELEM_MAX_ROUTING_PROVIDERS),
|
|
161
|
-
topic: opts.topic ?? (trimBlank(env.TELEM_TOPIC ?? "") || void 0)
|
|
170
|
+
topic: opts.topic ?? (trimBlank(env.TELEM_TOPIC ?? "") || void 0),
|
|
171
|
+
vertical
|
|
162
172
|
},
|
|
163
173
|
// Fetch defaults: env var only, parsed as the shared option table parses it (CSV,
|
|
164
174
|
// a trimmed name, a "1"-only flag); the tier is lowercased, as the agent plugins
|
|
@@ -175,6 +185,7 @@ function resolveConfig(options) {
|
|
|
175
185
|
var MAX_DELAY_MS = 3e4;
|
|
176
186
|
var MAX_TIMEOUT_MS = 2 ** 31 - 1;
|
|
177
187
|
var BASE_DELAY_MS = 500;
|
|
188
|
+
var MAX_IDEMPOTENT_BACKOFF_MS = 1e4;
|
|
178
189
|
function isObject(value) {
|
|
179
190
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
180
191
|
}
|
|
@@ -253,6 +264,11 @@ function connectionError(url, cause) {
|
|
|
253
264
|
function isRetryable(status) {
|
|
254
265
|
return status >= 500 || status === 429;
|
|
255
266
|
}
|
|
267
|
+
function shouldRetry(mode, status, body) {
|
|
268
|
+
if (mode === "all") return isRetryable(status);
|
|
269
|
+
if (status === 429) return true;
|
|
270
|
+
return mode === "idempotent" && status === 409 && codeFromBody(body) === "idempotency_in_progress";
|
|
271
|
+
}
|
|
256
272
|
function backoff(attempt) {
|
|
257
273
|
return Math.min(BASE_DELAY_MS * 2 ** attempt, MAX_DELAY_MS);
|
|
258
274
|
}
|
|
@@ -262,9 +278,20 @@ function retryDelay(retryAfter, attempt) {
|
|
|
262
278
|
}
|
|
263
279
|
return backoff(attempt);
|
|
264
280
|
}
|
|
265
|
-
|
|
281
|
+
function jittered(ms) {
|
|
282
|
+
return ms + Math.random() * Math.max(1e3, ms / 2);
|
|
283
|
+
}
|
|
284
|
+
function idempotentDelay(retryAfter, attempt) {
|
|
285
|
+
if (retryAfter !== null && /^\s*\d+\s*$/.test(retryAfter)) return jittered(Number(retryAfter) * 1e3);
|
|
286
|
+
return Math.min(jittered(BASE_DELAY_MS * 2 ** attempt), MAX_IDEMPOTENT_BACKOFF_MS);
|
|
287
|
+
}
|
|
288
|
+
async function request(config, method, path, body, abort, retry = "all", extra = {}) {
|
|
266
289
|
const url = `${config.baseUrl}/${path.replace(/^\/+/, "")}`;
|
|
267
|
-
const
|
|
290
|
+
const maxRetries = extra.maxRetries ?? config.maxRetries;
|
|
291
|
+
const started = Date.now();
|
|
292
|
+
const fits = (ms) => Date.now() - started + ms <= (extra.budgetMs ?? Infinity);
|
|
293
|
+
const delay = (retryAfter, attempt) => retry === "idempotent" ? idempotentDelay(retryAfter, attempt) : retryDelay(retryAfter, attempt);
|
|
294
|
+
const headers = { ...extra.headers, "User-Agent": config.userAgent };
|
|
268
295
|
if (config.apiKey) headers.Authorization = `Bearer ${config.apiKey}`;
|
|
269
296
|
const init = { method, headers };
|
|
270
297
|
if (body !== void 0) {
|
|
@@ -324,20 +351,27 @@ async function request(config, method, path, body, abort, retry = "all") {
|
|
|
324
351
|
});
|
|
325
352
|
}
|
|
326
353
|
if (abort?.aborted) throw abort.reason ?? cause;
|
|
354
|
+
const wait = delay(null, attempt);
|
|
327
355
|
if (signal?.aborted) {
|
|
356
|
+
if (retry === "idempotent" && attempt < maxRetries && fits(wait)) {
|
|
357
|
+
await pause(wait);
|
|
358
|
+
continue;
|
|
359
|
+
}
|
|
328
360
|
throw new TimeoutError(`request to ${url} timed out after ${config.timeoutMs}ms`, cause);
|
|
329
361
|
}
|
|
330
|
-
if (retry === "rate_limit" || attempt >=
|
|
331
|
-
await pause(
|
|
362
|
+
if (retry === "rate_limit" || attempt >= maxRetries || !fits(wait)) throw connectionError(url, cause);
|
|
363
|
+
await pause(wait);
|
|
332
364
|
continue;
|
|
333
365
|
} finally {
|
|
334
366
|
signal?.removeEventListener("abort", onAbort2);
|
|
335
367
|
}
|
|
336
368
|
if (received.status >= 200 && received.status < 300) return received.body;
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
369
|
+
if (shouldRetry(retry, received.status, received.body) && attempt < maxRetries) {
|
|
370
|
+
const wait = delay(received.retryAfter, attempt);
|
|
371
|
+
if (fits(wait)) {
|
|
372
|
+
await pause(wait);
|
|
373
|
+
continue;
|
|
374
|
+
}
|
|
341
375
|
}
|
|
342
376
|
throw statusError(received.status, received.body);
|
|
343
377
|
}
|
|
@@ -616,6 +650,8 @@ function searchBlock(defaults, options) {
|
|
|
616
650
|
if (options.publishedAfter !== void 0) block.published_after = isoDate(options.publishedAfter);
|
|
617
651
|
if (options.publishedBefore !== void 0) block.published_before = isoDate(options.publishedBefore);
|
|
618
652
|
if (options.includeDomains !== void 0) block.include_domains = options.includeDomains;
|
|
653
|
+
const vertical = options.vertical ?? defaults.vertical;
|
|
654
|
+
if (vertical !== void 0) block.vertical = vertical;
|
|
619
655
|
return block;
|
|
620
656
|
}
|
|
621
657
|
function isoDate(value) {
|
|
@@ -674,6 +710,9 @@ function searchResults(envelope, provider) {
|
|
|
674
710
|
source: row.source ?? null,
|
|
675
711
|
enrichments: row.enrichments ?? null,
|
|
676
712
|
fetchMeta: row.fetch_meta ?? null,
|
|
713
|
+
imageUrl: row.image_url ?? null,
|
|
714
|
+
imageWidth: row.image_width ?? null,
|
|
715
|
+
imageHeight: row.image_height ?? null,
|
|
677
716
|
provider,
|
|
678
717
|
raw: row
|
|
679
718
|
}));
|
|
@@ -727,6 +766,8 @@ async function search(config, query, options) {
|
|
|
727
766
|
|
|
728
767
|
// src/methods/monitors.ts
|
|
729
768
|
var NEEDS_TTL = 'A monitor with an agent must end: pass ttl, such as ttl: "30d" (at most 30 days), or agent: false for a monitor without an agent.';
|
|
769
|
+
var NEEDS_TTL_INPUTS = 'A monitor with inputs must end: pass ttl, such as ttl: "30d" (at most 365 days).';
|
|
770
|
+
var IDEMPOTENCY_KEY = /^[\x21-\x7e]{1,255}$/;
|
|
730
771
|
var POLL_DEPRECATED = "@telemai/sdk: monitors.poll() is deprecated: read what the monitor agent finds with monitors.iterateChanges() or monitors.changes().";
|
|
731
772
|
var pollWarned = false;
|
|
732
773
|
function warnPollDeprecated() {
|
|
@@ -764,6 +805,7 @@ function monitorFrom(body) {
|
|
|
764
805
|
webhookUrl: data.webhook_url ?? null,
|
|
765
806
|
webhookSecret: data.webhook_secret ?? null,
|
|
766
807
|
agentNote: typeof data.agent_note === "string" ? data.agent_note : null,
|
|
808
|
+
tags: asArray(data.tags).map(String),
|
|
767
809
|
raw: data
|
|
768
810
|
};
|
|
769
811
|
if (data.agent_state !== void 0) {
|
|
@@ -809,10 +851,151 @@ function changesPageFrom(body) {
|
|
|
809
851
|
watermark: Number(data.watermark ?? 0)
|
|
810
852
|
};
|
|
811
853
|
}
|
|
854
|
+
function monitorsChangesPageFrom(body) {
|
|
855
|
+
const data = asObject(body);
|
|
856
|
+
return {
|
|
857
|
+
changes: asArray(data.changes).filter(isObject).map(changeRecordFrom),
|
|
858
|
+
nextCursor: typeof data.next_cursor === "string" ? data.next_cursor : "",
|
|
859
|
+
more: data.more === true,
|
|
860
|
+
monitors: asArray(data.monitors).map(String),
|
|
861
|
+
pending: asArray(data.pending).map(String),
|
|
862
|
+
skipped: asArray(data.skipped).map(String)
|
|
863
|
+
};
|
|
864
|
+
}
|
|
865
|
+
var stringOrNull = (value) => typeof value === "string" ? value : null;
|
|
866
|
+
var countOf = (value) => Array.isArray(value) ? value.length : typeof value === "number" ? value : 0;
|
|
867
|
+
function setFields(fields) {
|
|
868
|
+
return Object.fromEntries(Object.entries(fields).filter(([key, value]) => key !== "signal" && value != null));
|
|
869
|
+
}
|
|
870
|
+
function exampleFrom(data) {
|
|
871
|
+
if (!isObject(data)) return null;
|
|
872
|
+
return { name: stringOrNull(data.name), quote: stringOrNull(data.quote), url: stringOrNull(data.url) };
|
|
873
|
+
}
|
|
874
|
+
function candidateFrom(entry) {
|
|
875
|
+
return {
|
|
876
|
+
id: String(entry.id ?? ""),
|
|
877
|
+
what: stringOrNull(entry.what),
|
|
878
|
+
label: String(entry.label ?? ""),
|
|
879
|
+
mappedTo: stringOrNull(entry.mapped_to),
|
|
880
|
+
isNew: entry.is_new === true,
|
|
881
|
+
count: countOf(entry.count),
|
|
882
|
+
origins: countOf(entry.origins),
|
|
883
|
+
example: exampleFrom(entry.example),
|
|
884
|
+
raw: entry
|
|
885
|
+
};
|
|
886
|
+
}
|
|
887
|
+
function companySheetFrom(data) {
|
|
888
|
+
return {
|
|
889
|
+
companyId: stringOrNull(data.company_id),
|
|
890
|
+
pages: countOf(data.pages),
|
|
891
|
+
candidates: asArray(data.candidates).filter(isObject).map(candidateFrom),
|
|
892
|
+
note: stringOrNull(data.note),
|
|
893
|
+
raw: data
|
|
894
|
+
};
|
|
895
|
+
}
|
|
896
|
+
function onboardingCompanyFrom(entry) {
|
|
897
|
+
return {
|
|
898
|
+
companyId: String(entry.company_id ?? ""),
|
|
899
|
+
status: String(entry.status ?? ""),
|
|
900
|
+
runId: stringOrNull(entry.run_id),
|
|
901
|
+
sheet: isObject(entry.sheet) ? companySheetFrom(entry.sheet) : null,
|
|
902
|
+
marks: isObject(entry.marks) ? entry.marks : null,
|
|
903
|
+
at: stringOrNull(entry.at),
|
|
904
|
+
appliedAt: stringOrNull(entry.applied_at),
|
|
905
|
+
raw: entry
|
|
906
|
+
};
|
|
907
|
+
}
|
|
908
|
+
function onboardingSheetFrom(body) {
|
|
909
|
+
const data = asObject(body);
|
|
910
|
+
return {
|
|
911
|
+
monitorId: stringOrNull(data.monitor_id),
|
|
912
|
+
companies: asArray(data.companies).filter(isObject).map(onboardingCompanyFrom),
|
|
913
|
+
raw: data
|
|
914
|
+
};
|
|
915
|
+
}
|
|
916
|
+
function applyResultFrom(body) {
|
|
917
|
+
const data = asObject(body);
|
|
918
|
+
return { jobId: String(data.job_id ?? ""), raw: data };
|
|
919
|
+
}
|
|
920
|
+
function expansionFrom(entry) {
|
|
921
|
+
return {
|
|
922
|
+
id: String(entry.id ?? ""),
|
|
923
|
+
kind: String(entry.kind ?? ""),
|
|
924
|
+
target: String(entry.target ?? ""),
|
|
925
|
+
status: String(entry.status ?? ""),
|
|
926
|
+
reason: asObject(entry.reason),
|
|
927
|
+
delta: asObject(entry.delta),
|
|
928
|
+
counts: asObject(entry.counts),
|
|
929
|
+
note: stringOrNull(entry.note),
|
|
930
|
+
runId: stringOrNull(entry.run_id),
|
|
931
|
+
createdAt: stringOrNull(entry.created_at),
|
|
932
|
+
updatedAt: stringOrNull(entry.updated_at),
|
|
933
|
+
raw: entry
|
|
934
|
+
};
|
|
935
|
+
}
|
|
936
|
+
function expansionsPageFrom(body) {
|
|
937
|
+
const data = asObject(body);
|
|
938
|
+
return {
|
|
939
|
+
expansions: asArray(data.expansions).filter(isObject).map(expansionFrom),
|
|
940
|
+
nextCursor: stringOrNull(data.next_cursor)
|
|
941
|
+
};
|
|
942
|
+
}
|
|
943
|
+
function feedbackResultFrom(body) {
|
|
944
|
+
const data = asObject(body);
|
|
945
|
+
return { status: String(data.status ?? ""), jobId: stringOrNull(data.job_id), raw: data };
|
|
946
|
+
}
|
|
947
|
+
function dataMapFrom(body) {
|
|
948
|
+
const data = asObject(body);
|
|
949
|
+
return {
|
|
950
|
+
// A kind named bare, as a monitor created without inputs may have it.
|
|
951
|
+
kinds: asArray(data.kinds).map((kind) => typeof kind === "string" ? { kind } : kind).filter(isObject),
|
|
952
|
+
categories: asArray(data.categories).filter(isObject),
|
|
953
|
+
inducedCategories: asArray(data.induced_categories).filter(isObject),
|
|
954
|
+
people: asObject(data.people),
|
|
955
|
+
note: stringOrNull(data.note),
|
|
956
|
+
raw: data
|
|
957
|
+
};
|
|
958
|
+
}
|
|
959
|
+
function companyFrom(entry) {
|
|
960
|
+
return {
|
|
961
|
+
companyId: String(entry.company_id ?? ""),
|
|
962
|
+
name: String(entry.name ?? ""),
|
|
963
|
+
domain: String(entry.domain ?? ""),
|
|
964
|
+
priority: entry.priority === true,
|
|
965
|
+
aliases: asArray(entry.aliases).map(String),
|
|
966
|
+
ticker: String(entry.ticker ?? ""),
|
|
967
|
+
xHandle: String(entry.x_handle ?? ""),
|
|
968
|
+
newsroomUrl: String(entry.newsroom_url ?? ""),
|
|
969
|
+
teamPage: String(entry.team_page ?? ""),
|
|
970
|
+
tier: String(entry.tier ?? ""),
|
|
971
|
+
rank: Number(entry.rank ?? 0),
|
|
972
|
+
raw: entry
|
|
973
|
+
};
|
|
974
|
+
}
|
|
975
|
+
function companyListFrom(body) {
|
|
976
|
+
const data = asObject(body);
|
|
977
|
+
return {
|
|
978
|
+
total: Number(data.total ?? 0),
|
|
979
|
+
priorityCount: Number(data.priority_count ?? 0),
|
|
980
|
+
offset: Number(data.offset ?? 0),
|
|
981
|
+
limit: Number(data.limit ?? 0),
|
|
982
|
+
nextOffset: typeof data.next_offset === "number" ? data.next_offset : null,
|
|
983
|
+
companies: asArray(data.companies).filter(isObject).map(companyFrom),
|
|
984
|
+
raw: data
|
|
985
|
+
};
|
|
986
|
+
}
|
|
987
|
+
function companiesQuery(options) {
|
|
988
|
+
const params = new URLSearchParams();
|
|
989
|
+
if (options.offset !== void 0) params.set("offset", String(options.offset));
|
|
990
|
+
if (options.limit !== void 0) params.set("limit", String(options.limit));
|
|
991
|
+
const query = params.toString();
|
|
992
|
+
return query ? `?${query}` : "";
|
|
993
|
+
}
|
|
812
994
|
function listQuery(options) {
|
|
813
995
|
const params = new URLSearchParams();
|
|
814
996
|
if (options.limit !== void 0) params.set("limit", String(options.limit));
|
|
815
997
|
if (options.cursor !== void 0) params.set("cursor", options.cursor);
|
|
998
|
+
if (options.tag !== void 0) params.set("tag", options.tag);
|
|
816
999
|
const query = params.toString();
|
|
817
1000
|
return query ? `?${query}` : "";
|
|
818
1001
|
}
|
|
@@ -824,12 +1007,28 @@ function sinceParam(since) {
|
|
|
824
1007
|
function changesQuery(options) {
|
|
825
1008
|
const params = new URLSearchParams();
|
|
826
1009
|
if (options.cursor !== void 0) params.set("cursor", String(options.cursor));
|
|
1010
|
+
return readQuery(params, options);
|
|
1011
|
+
}
|
|
1012
|
+
function readQuery(params, options) {
|
|
827
1013
|
if (options.since !== void 0) params.set("since", sinceParam(options.since));
|
|
828
1014
|
if (options.kinds !== void 0) params.set("kinds", options.kinds.join(","));
|
|
829
1015
|
if (options.limit !== void 0) params.set("limit", String(options.limit));
|
|
830
1016
|
const query = params.toString();
|
|
831
1017
|
return query ? `?${query}` : "";
|
|
832
1018
|
}
|
|
1019
|
+
function acrossQuery(scope, options) {
|
|
1020
|
+
const { tag, ids } = asObject(scope);
|
|
1021
|
+
if (tag == null === (ids == null)) throw invalidRequest("send tag or ids, one of them");
|
|
1022
|
+
const params = new URLSearchParams();
|
|
1023
|
+
if (tag != null) params.set("tag", String(tag));
|
|
1024
|
+
else {
|
|
1025
|
+
const named = typeof ids === "string" ? ids : asArray(ids).join(",");
|
|
1026
|
+
if (!named) throw invalidRequest("ids needs at least one monitor id");
|
|
1027
|
+
params.set("ids", named);
|
|
1028
|
+
}
|
|
1029
|
+
if (options.cursor !== void 0) params.set("cursor", options.cursor);
|
|
1030
|
+
return readQuery(params, options);
|
|
1031
|
+
}
|
|
833
1032
|
function createMonitors(config) {
|
|
834
1033
|
const path = (monitorId, suffix = "") => {
|
|
835
1034
|
if (!monitorId || /^\.+$/.test(monitorId)) throw invalidRequest("a monitor id is required");
|
|
@@ -845,12 +1044,25 @@ function createMonitors(config) {
|
|
|
845
1044
|
const route = `${path(monitorId, "/agent/changes")}${changesQuery(options)}`;
|
|
846
1045
|
return changesPageFrom(await request(config, "GET", route, void 0, options.signal));
|
|
847
1046
|
};
|
|
1047
|
+
const changesAcross = async (scope, options = {}) => {
|
|
1048
|
+
const route = `/v1/monitors/changes${acrossQuery(scope, options)}`;
|
|
1049
|
+
return monitorsChangesPageFrom(await request(config, "GET", route, void 0, options.signal));
|
|
1050
|
+
};
|
|
1051
|
+
const expansionPath = (monitorId, expansionId, action) => {
|
|
1052
|
+
if (!expansionId || /^\.+$/.test(expansionId)) throw invalidRequest("an expansion id is required");
|
|
1053
|
+
return path(monitorId, `/agent/expansions/${encodeURIComponent(expansionId)}:${action}`);
|
|
1054
|
+
};
|
|
1055
|
+
const write = async (route, body, signal) => request(config, "POST", route, body, signal, "rate_limit");
|
|
848
1056
|
return {
|
|
849
1057
|
/** Create a monitor. Telem runs `query` on the schedule, and its agent keeps what matches. */
|
|
850
1058
|
async create(query, options) {
|
|
851
1059
|
const given = options ?? {};
|
|
852
1060
|
const agent = given.agent == null ? true : Boolean(given.agent);
|
|
853
|
-
if (agent && !given.ttl) throw invalidRequest(NEEDS_TTL);
|
|
1061
|
+
if (agent && !given.ttl) throw invalidRequest(given.inputs == null ? NEEDS_TTL : NEEDS_TTL_INPUTS);
|
|
1062
|
+
const key = given.idempotencyKey ?? globalThis.crypto.randomUUID();
|
|
1063
|
+
if (typeof key !== "string" || !IDEMPOTENCY_KEY.test(key)) {
|
|
1064
|
+
throw invalidRequest("idempotencyKey must be 1 to 255 visible ASCII characters");
|
|
1065
|
+
}
|
|
854
1066
|
const monitorOptions = {};
|
|
855
1067
|
if (given.frequency !== void 0) monitorOptions.frequency = given.frequency;
|
|
856
1068
|
if (given.includeDomains !== void 0) {
|
|
@@ -861,8 +1073,22 @@ function createMonitors(config) {
|
|
|
861
1073
|
if (given.metadata !== void 0) body.metadata = given.metadata;
|
|
862
1074
|
if (given.webhookUrl !== void 0) body.webhook_url = given.webhookUrl;
|
|
863
1075
|
if (given.ttl !== void 0) body.ttl = given.ttl;
|
|
1076
|
+
if (given.inputs != null) {
|
|
1077
|
+
body.inputs = given.inputs;
|
|
1078
|
+
const vertical = config.defaults.vertical;
|
|
1079
|
+
if (vertical !== void 0 && !("vertical" in given.inputs)) {
|
|
1080
|
+
body.inputs = { ...given.inputs, vertical };
|
|
1081
|
+
}
|
|
1082
|
+
}
|
|
864
1083
|
body.agent = agent;
|
|
865
|
-
|
|
1084
|
+
if (given.preset != null) body.preset = given.preset;
|
|
1085
|
+
if (given.tags != null) body.tags = given.tags;
|
|
1086
|
+
const extra = {
|
|
1087
|
+
headers: { "Idempotency-Key": key },
|
|
1088
|
+
maxRetries: config.createMaxRetries,
|
|
1089
|
+
budgetMs: CREATE_RETRY_BUDGET_MS
|
|
1090
|
+
};
|
|
1091
|
+
const data = await request(config, "POST", "/v1/monitors", body, given.signal, "idempotent", extra).catch(
|
|
866
1092
|
(error) => {
|
|
867
1093
|
if (error instanceof TimeoutError) {
|
|
868
1094
|
throw new TimeoutError(
|
|
@@ -891,6 +1117,7 @@ function createMonitors(config) {
|
|
|
891
1117
|
if (options.frequency !== void 0) body.frequency = options.frequency;
|
|
892
1118
|
if (options.ttl !== void 0) body.ttl = options.ttl;
|
|
893
1119
|
if (options.webhookUrl !== void 0) body.webhook_url = options.webhookUrl || null;
|
|
1120
|
+
if (options.tags != null) body.tags = options.tags;
|
|
894
1121
|
const route = path(monitorId);
|
|
895
1122
|
return monitorFrom(await request(config, "PATCH", route, body, options.signal));
|
|
896
1123
|
},
|
|
@@ -925,25 +1152,88 @@ function createMonitors(config) {
|
|
|
925
1152
|
if (!page.changes.length || page.nextCursor <= cursor || page.nextCursor >= page.watermark) return;
|
|
926
1153
|
cursor = page.nextCursor;
|
|
927
1154
|
}
|
|
1155
|
+
},
|
|
1156
|
+
/** One page of the changes of up to 100 monitors. Keep `nextCursor` and pass it back as `cursor`. */
|
|
1157
|
+
changesAcross,
|
|
1158
|
+
/** The changes of up to 100 monitors, page by page while `more` is true. `since` goes
|
|
1159
|
+
* with the first page only; the scope, `kinds` and `limit` with every page. */
|
|
1160
|
+
async *iterateChangesAcross(scope, options = {}) {
|
|
1161
|
+
let { cursor, since } = options;
|
|
1162
|
+
for (; ; ) {
|
|
1163
|
+
const page = await changesAcross(scope, { ...options, cursor, since });
|
|
1164
|
+
for (const change of page.changes) yield change;
|
|
1165
|
+
if (!page.more || !page.changes.length || page.nextCursor === cursor) return;
|
|
1166
|
+
cursor = page.nextCursor;
|
|
1167
|
+
since = void 0;
|
|
1168
|
+
}
|
|
1169
|
+
},
|
|
1170
|
+
/** Label one fact of the monitor; with `category`, file a refuted fact there. */
|
|
1171
|
+
async feedback(monitorId, options) {
|
|
1172
|
+
const body = { fact_id: options.factId, verdict: options.verdict };
|
|
1173
|
+
if (options.category != null) body.category = options.category;
|
|
1174
|
+
return feedbackResultFrom(await write(path(monitorId, "/agent/feedback"), body, options.signal));
|
|
1175
|
+
},
|
|
1176
|
+
/** The onboarding sheet of each company of a monitor created with `inputs`. */
|
|
1177
|
+
async onboarding(monitorId, options = {}) {
|
|
1178
|
+
const route = path(monitorId, "/agent/onboarding");
|
|
1179
|
+
return onboardingSheetFrom(await request(config, "GET", route, void 0, options.signal));
|
|
1180
|
+
},
|
|
1181
|
+
/** Apply your review of one company's sheet: `companyId` goes as `company_id`. */
|
|
1182
|
+
async applyOnboarding(monitorId, marks) {
|
|
1183
|
+
const { companyId, ...rest } = marks;
|
|
1184
|
+
const body = setFields({ company_id: companyId, ...rest });
|
|
1185
|
+
return applyResultFrom(await write(path(monitorId, "/agent/onboarding:apply"), body, marks.signal));
|
|
1186
|
+
},
|
|
1187
|
+
/** What the monitor proposes to add, or added; `status` filters. */
|
|
1188
|
+
async expansions(monitorId, options = {}) {
|
|
1189
|
+
const query = options.status !== void 0 ? `?${new URLSearchParams({ status: options.status })}` : "";
|
|
1190
|
+
const route = `${path(monitorId, "/agent/expansions")}${query}`;
|
|
1191
|
+
return expansionsPageFrom(await request(config, "GET", route, void 0, options.signal));
|
|
1192
|
+
},
|
|
1193
|
+
/** Use a proposed addition. */
|
|
1194
|
+
async confirmExpansion(monitorId, expansionId, fields = {}) {
|
|
1195
|
+
const route = expansionPath(monitorId, expansionId, "confirm");
|
|
1196
|
+
return applyResultFrom(await write(route, setFields(fields), fields.signal));
|
|
1197
|
+
},
|
|
1198
|
+
/** Remove an addition. */
|
|
1199
|
+
async revertExpansion(monitorId, expansionId, options = {}) {
|
|
1200
|
+
const route = expansionPath(monitorId, expansionId, "revert");
|
|
1201
|
+
return applyResultFrom(await write(route, {}, options.signal));
|
|
1202
|
+
},
|
|
1203
|
+
/** Every kind and category the monitor collects. */
|
|
1204
|
+
async dataMap(monitorId, options = {}) {
|
|
1205
|
+
const route = path(monitorId, "/agent/data-map");
|
|
1206
|
+
return dataMapFrom(await request(config, "GET", route, void 0, options.signal));
|
|
1207
|
+
},
|
|
1208
|
+
/** One page of the monitor's companies, in rank order. */
|
|
1209
|
+
async companies(monitorId, options = {}) {
|
|
1210
|
+
const route = `${path(monitorId, "/agent/companies")}${companiesQuery(options)}`;
|
|
1211
|
+
return companyListFrom(await request(config, "GET", route, void 0, options.signal));
|
|
928
1212
|
}
|
|
929
1213
|
};
|
|
930
1214
|
}
|
|
931
1215
|
async function verifyWebhook(body, header, secret, toleranceSeconds = 300) {
|
|
932
|
-
const parts =
|
|
933
|
-
|
|
934
|
-
);
|
|
935
|
-
|
|
936
|
-
const
|
|
1216
|
+
const parts = header.split(",").map((item) => item.trim()).filter((item) => item.includes("=")).map((item) => [item.slice(0, item.indexOf("=")), item.slice(item.indexOf("=") + 1)]);
|
|
1217
|
+
const timestamps = parts.filter(([name]) => name === "t").map(([, value]) => value);
|
|
1218
|
+
const signatures = parts.filter(([name, value]) => name === "v1" && /^[0-9a-f]{64}$/.test(value)).map(([, value]) => value);
|
|
1219
|
+
if (timestamps.length !== 1 || signatures.length === 0) return false;
|
|
1220
|
+
const t = timestamps[0];
|
|
937
1221
|
if (!/^\d+$/.test(t) || Math.abs(Date.now() / 1e3 - Number(t)) > toleranceSeconds) return false;
|
|
938
|
-
if (!/^([0-9a-f]{2})+$/.test(v1)) return false;
|
|
939
1222
|
const encoder = new TextEncoder();
|
|
940
1223
|
const raw = typeof body === "string" ? encoder.encode(body) : body;
|
|
941
1224
|
const signed = new Uint8Array([...encoder.encode(`${t}.`), ...raw]);
|
|
942
1225
|
const key = await crypto.subtle.importKey("raw", encoder.encode(secret), { name: "HMAC", hash: "SHA-256" }, false, [
|
|
943
|
-
"
|
|
1226
|
+
"sign"
|
|
944
1227
|
]);
|
|
945
|
-
const
|
|
946
|
-
|
|
1228
|
+
const mac = new Uint8Array(await crypto.subtle.sign("HMAC", key, signed));
|
|
1229
|
+
const expected = Array.from(mac, (byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
1230
|
+
return signatures.some((signature) => sameText(signature, expected));
|
|
1231
|
+
}
|
|
1232
|
+
function sameText(a, b) {
|
|
1233
|
+
if (a.length !== b.length) return false;
|
|
1234
|
+
let diff = 0;
|
|
1235
|
+
for (let i = 0; i < a.length; i++) diff |= a.charCodeAt(i) ^ b.charCodeAt(i);
|
|
1236
|
+
return diff === 0;
|
|
947
1237
|
}
|
|
948
1238
|
|
|
949
1239
|
// src/methods/providers.ts
|
|
@@ -1028,7 +1318,10 @@ var Telem = class {
|
|
|
1028
1318
|
/** The session read surface: `list` and `results(id)`. */
|
|
1029
1319
|
sessions;
|
|
1030
1320
|
/** Monitors: `create`, `list`, `listPage`, `get`, `update` and `delete`, the agent reads
|
|
1031
|
-
* `agent`, `changes` and `iterateChanges`,
|
|
1321
|
+
* `agent`, `changes` and `iterateChanges`, the read across monitors `changesAcross` and
|
|
1322
|
+
* `iterateChangesAcross`, `feedback`, what a monitor created with
|
|
1323
|
+
* `inputs` collects (`onboarding`, `applyOnboarding`, `expansions`, `confirmExpansion`,
|
|
1324
|
+
* `revertExpansion`, `dataMap`, `companies`), and the deprecated `poll`. */
|
|
1032
1325
|
monitors;
|
|
1033
1326
|
#config;
|
|
1034
1327
|
/**
|