@telemai/sdk 0.4.0 → 0.6.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 +26 -15
- package/dist/{chunk-SQ3ZZA72.js → chunk-TST6J3VA.js} +32 -8
- package/dist/index.cjs +284 -25
- package/dist/index.d.cts +322 -19
- package/dist/index.d.ts +322 -19
- package/dist/index.js +253 -18
- package/dist/vercel/index.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -207,30 +207,35 @@ results of each search step.
|
|
|
207
207
|
|
|
208
208
|
## Monitors
|
|
209
209
|
|
|
210
|
-
A monitor runs a query on a schedule
|
|
211
|
-
|
|
210
|
+
A monitor runs a query on a schedule, and its agent checks every result against the
|
|
211
|
+
query and keeps the ones that match. A monitor has the agent unless you create it with
|
|
212
|
+
`agent: false`, and a monitor with the agent needs a `ttl`, at most `"30d"`: without
|
|
213
|
+
one, `create` throws a `TelemError` with the code `invalid_request`.
|
|
212
214
|
|
|
213
215
|
```ts
|
|
214
|
-
const monitor = await telem.monitors.create("Nvidia files a new 8-K with the SEC", { frequency: "1h", ttl: "
|
|
215
|
-
|
|
216
|
+
const monitor = await telem.monitors.create("Nvidia files a new 8-K with the SEC", { frequency: "1h", ttl: "30d" })
|
|
217
|
+
console.log(monitor.agentState?.status) // what its agent does: compiling, then ready
|
|
216
218
|
for await (const change of telem.monitors.iterateChanges(monitor.id)) {
|
|
217
219
|
console.log(change.seq, change.kind, change.subject.name)
|
|
218
220
|
}
|
|
221
|
+
const agent = await telem.monitors.agent(monitor.id)
|
|
219
222
|
await telem.monitors.update(monitor.id, { status: "paused" })
|
|
223
|
+
await telem.monitors.update(monitor.id, { ttl: "30d" }) // renew: it ends 30 days from now
|
|
220
224
|
await telem.monitors.delete(monitor.id)
|
|
221
225
|
```
|
|
222
226
|
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
`
|
|
229
|
-
|
|
230
|
-
`iterateChanges` with `for await`.
|
|
227
|
+
`iterateChanges` reads what the agent kept, oldest first, until you are caught up,
|
|
228
|
+
and then stops. Keep the `seq` of the last change you handled and pass it as `cursor`
|
|
229
|
+
next time: no change comes twice, and none is skipped. `changes` returns one page
|
|
230
|
+
(`nextCursor`, `watermark`) and takes `cursor`, `since`, `kinds` and `limit`. `agent`
|
|
231
|
+
returns the agent itself: its status, query, frequency and plan version.
|
|
232
|
+
`monitor.agentState` shows it on the monitor; when the agent did not start, the
|
|
233
|
+
create's `monitor.agentNote` says why. Read `iterateChanges` with `for await`.
|
|
231
234
|
|
|
232
|
-
`telem.monitors` also has `get` and `list`.
|
|
233
|
-
|
|
235
|
+
`telem.monitors` also has `get` and `list`. `poll` is deprecated and warns once:
|
|
236
|
+
read the agent's changes with `iterateChanges` instead.
|
|
237
|
+
See [Monitors](https://docs.telem.ai/resources/monitors/) for the fields, the errors and
|
|
238
|
+
the older reads.
|
|
234
239
|
|
|
235
240
|
## Vercel AI SDK
|
|
236
241
|
|
|
@@ -381,12 +386,18 @@ try {
|
|
|
381
386
|
|
|
382
387
|
| Situation | Result |
|
|
383
388
|
|---|---|
|
|
384
|
-
| Bad input (empty query, empty URL list) | `TelemError`, `code: "invalid_request"`, nothing sent |
|
|
389
|
+
| Bad input (empty query, empty URL list, a monitor with an agent and no `ttl`) | `TelemError`, `code: "invalid_request"`, nothing sent |
|
|
385
390
|
| Network failure | `TelemError`, `code: "connection_error"`, after the retries |
|
|
386
391
|
| 5xx or 429 | Retried with backoff (`Retry-After` applies), then `TelemError` with the status |
|
|
387
392
|
| Other 4xx | `TelemError` with the status and the code from the server, not retried |
|
|
388
393
|
| Attempt longer than `timeoutMs` | `TimeoutError`, not retried |
|
|
389
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
|
+
|
|
390
401
|
See [Resources → Errors](https://docs.telem.ai/resources/errors/) for how the plugins show a failed call.
|
|
391
402
|
|
|
392
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.6.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) {
|
|
@@ -726,6 +762,16 @@ async function search(config, query, options) {
|
|
|
726
762
|
}
|
|
727
763
|
|
|
728
764
|
// src/methods/monitors.ts
|
|
765
|
+
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.';
|
|
766
|
+
var NEEDS_TTL_INPUTS = 'A monitor with inputs must end: pass ttl, such as ttl: "30d" (at most 365 days).';
|
|
767
|
+
var IDEMPOTENCY_KEY = /^[\x21-\x7e]{1,255}$/;
|
|
768
|
+
var POLL_DEPRECATED = "@telemai/sdk: monitors.poll() is deprecated: read what the monitor agent finds with monitors.iterateChanges() or monitors.changes().";
|
|
769
|
+
var pollWarned = false;
|
|
770
|
+
function warnPollDeprecated() {
|
|
771
|
+
if (pollWarned) return;
|
|
772
|
+
pollWarned = true;
|
|
773
|
+
console.warn(POLL_DEPRECATED);
|
|
774
|
+
}
|
|
729
775
|
function agentStateFrom(data) {
|
|
730
776
|
return {
|
|
731
777
|
engine: String(data.engine ?? ""),
|
|
@@ -755,6 +801,8 @@ function monitorFrom(body) {
|
|
|
755
801
|
expiresAt: data.expires_at ?? null,
|
|
756
802
|
webhookUrl: data.webhook_url ?? null,
|
|
757
803
|
webhookSecret: data.webhook_secret ?? null,
|
|
804
|
+
agentNote: typeof data.agent_note === "string" ? data.agent_note : null,
|
|
805
|
+
tags: asArray(data.tags).map(String),
|
|
758
806
|
raw: data
|
|
759
807
|
};
|
|
760
808
|
if (data.agent_state !== void 0) {
|
|
@@ -800,10 +848,116 @@ function changesPageFrom(body) {
|
|
|
800
848
|
watermark: Number(data.watermark ?? 0)
|
|
801
849
|
};
|
|
802
850
|
}
|
|
851
|
+
function monitorsChangesPageFrom(body) {
|
|
852
|
+
const data = asObject(body);
|
|
853
|
+
return {
|
|
854
|
+
changes: asArray(data.changes).filter(isObject).map(changeRecordFrom),
|
|
855
|
+
nextCursor: typeof data.next_cursor === "string" ? data.next_cursor : "",
|
|
856
|
+
more: data.more === true,
|
|
857
|
+
monitors: asArray(data.monitors).map(String),
|
|
858
|
+
pending: asArray(data.pending).map(String),
|
|
859
|
+
skipped: asArray(data.skipped).map(String)
|
|
860
|
+
};
|
|
861
|
+
}
|
|
862
|
+
var stringOrNull = (value) => typeof value === "string" ? value : null;
|
|
863
|
+
var countOf = (value) => Array.isArray(value) ? value.length : typeof value === "number" ? value : 0;
|
|
864
|
+
function setFields(fields) {
|
|
865
|
+
return Object.fromEntries(Object.entries(fields).filter(([key, value]) => key !== "signal" && value != null));
|
|
866
|
+
}
|
|
867
|
+
function exampleFrom(data) {
|
|
868
|
+
if (!isObject(data)) return null;
|
|
869
|
+
return { name: stringOrNull(data.name), quote: stringOrNull(data.quote), url: stringOrNull(data.url) };
|
|
870
|
+
}
|
|
871
|
+
function candidateFrom(entry) {
|
|
872
|
+
return {
|
|
873
|
+
id: String(entry.id ?? ""),
|
|
874
|
+
what: stringOrNull(entry.what),
|
|
875
|
+
label: String(entry.label ?? ""),
|
|
876
|
+
mappedTo: stringOrNull(entry.mapped_to),
|
|
877
|
+
isNew: entry.is_new === true,
|
|
878
|
+
count: countOf(entry.count),
|
|
879
|
+
origins: countOf(entry.origins),
|
|
880
|
+
example: exampleFrom(entry.example),
|
|
881
|
+
raw: entry
|
|
882
|
+
};
|
|
883
|
+
}
|
|
884
|
+
function companySheetFrom(data) {
|
|
885
|
+
return {
|
|
886
|
+
companyId: stringOrNull(data.company_id),
|
|
887
|
+
pages: countOf(data.pages),
|
|
888
|
+
candidates: asArray(data.candidates).filter(isObject).map(candidateFrom),
|
|
889
|
+
note: stringOrNull(data.note),
|
|
890
|
+
raw: data
|
|
891
|
+
};
|
|
892
|
+
}
|
|
893
|
+
function onboardingCompanyFrom(entry) {
|
|
894
|
+
return {
|
|
895
|
+
companyId: String(entry.company_id ?? ""),
|
|
896
|
+
status: String(entry.status ?? ""),
|
|
897
|
+
runId: stringOrNull(entry.run_id),
|
|
898
|
+
sheet: isObject(entry.sheet) ? companySheetFrom(entry.sheet) : null,
|
|
899
|
+
marks: isObject(entry.marks) ? entry.marks : null,
|
|
900
|
+
at: stringOrNull(entry.at),
|
|
901
|
+
appliedAt: stringOrNull(entry.applied_at),
|
|
902
|
+
raw: entry
|
|
903
|
+
};
|
|
904
|
+
}
|
|
905
|
+
function onboardingSheetFrom(body) {
|
|
906
|
+
const data = asObject(body);
|
|
907
|
+
return {
|
|
908
|
+
monitorId: stringOrNull(data.monitor_id),
|
|
909
|
+
companies: asArray(data.companies).filter(isObject).map(onboardingCompanyFrom),
|
|
910
|
+
raw: data
|
|
911
|
+
};
|
|
912
|
+
}
|
|
913
|
+
function applyResultFrom(body) {
|
|
914
|
+
const data = asObject(body);
|
|
915
|
+
return { jobId: String(data.job_id ?? ""), raw: data };
|
|
916
|
+
}
|
|
917
|
+
function expansionFrom(entry) {
|
|
918
|
+
return {
|
|
919
|
+
id: String(entry.id ?? ""),
|
|
920
|
+
kind: String(entry.kind ?? ""),
|
|
921
|
+
target: String(entry.target ?? ""),
|
|
922
|
+
status: String(entry.status ?? ""),
|
|
923
|
+
reason: asObject(entry.reason),
|
|
924
|
+
delta: asObject(entry.delta),
|
|
925
|
+
counts: asObject(entry.counts),
|
|
926
|
+
note: stringOrNull(entry.note),
|
|
927
|
+
runId: stringOrNull(entry.run_id),
|
|
928
|
+
createdAt: stringOrNull(entry.created_at),
|
|
929
|
+
updatedAt: stringOrNull(entry.updated_at),
|
|
930
|
+
raw: entry
|
|
931
|
+
};
|
|
932
|
+
}
|
|
933
|
+
function expansionsPageFrom(body) {
|
|
934
|
+
const data = asObject(body);
|
|
935
|
+
return {
|
|
936
|
+
expansions: asArray(data.expansions).filter(isObject).map(expansionFrom),
|
|
937
|
+
nextCursor: stringOrNull(data.next_cursor)
|
|
938
|
+
};
|
|
939
|
+
}
|
|
940
|
+
function feedbackResultFrom(body) {
|
|
941
|
+
const data = asObject(body);
|
|
942
|
+
return { status: String(data.status ?? ""), jobId: stringOrNull(data.job_id), raw: data };
|
|
943
|
+
}
|
|
944
|
+
function dataMapFrom(body) {
|
|
945
|
+
const data = asObject(body);
|
|
946
|
+
return {
|
|
947
|
+
// A kind named bare, as a monitor created without inputs may have it.
|
|
948
|
+
kinds: asArray(data.kinds).map((kind) => typeof kind === "string" ? { kind } : kind).filter(isObject),
|
|
949
|
+
categories: asArray(data.categories).filter(isObject),
|
|
950
|
+
inducedCategories: asArray(data.induced_categories).filter(isObject),
|
|
951
|
+
people: asObject(data.people),
|
|
952
|
+
note: stringOrNull(data.note),
|
|
953
|
+
raw: data
|
|
954
|
+
};
|
|
955
|
+
}
|
|
803
956
|
function listQuery(options) {
|
|
804
957
|
const params = new URLSearchParams();
|
|
805
958
|
if (options.limit !== void 0) params.set("limit", String(options.limit));
|
|
806
959
|
if (options.cursor !== void 0) params.set("cursor", options.cursor);
|
|
960
|
+
if (options.tag !== void 0) params.set("tag", options.tag);
|
|
807
961
|
const query = params.toString();
|
|
808
962
|
return query ? `?${query}` : "";
|
|
809
963
|
}
|
|
@@ -815,12 +969,28 @@ function sinceParam(since) {
|
|
|
815
969
|
function changesQuery(options) {
|
|
816
970
|
const params = new URLSearchParams();
|
|
817
971
|
if (options.cursor !== void 0) params.set("cursor", String(options.cursor));
|
|
972
|
+
return readQuery(params, options);
|
|
973
|
+
}
|
|
974
|
+
function readQuery(params, options) {
|
|
818
975
|
if (options.since !== void 0) params.set("since", sinceParam(options.since));
|
|
819
976
|
if (options.kinds !== void 0) params.set("kinds", options.kinds.join(","));
|
|
820
977
|
if (options.limit !== void 0) params.set("limit", String(options.limit));
|
|
821
978
|
const query = params.toString();
|
|
822
979
|
return query ? `?${query}` : "";
|
|
823
980
|
}
|
|
981
|
+
function acrossQuery(scope, options) {
|
|
982
|
+
const { tag, ids } = asObject(scope);
|
|
983
|
+
if (tag == null === (ids == null)) throw invalidRequest("send tag or ids, one of them");
|
|
984
|
+
const params = new URLSearchParams();
|
|
985
|
+
if (tag != null) params.set("tag", String(tag));
|
|
986
|
+
else {
|
|
987
|
+
const named = typeof ids === "string" ? ids : asArray(ids).join(",");
|
|
988
|
+
if (!named) throw invalidRequest("ids needs at least one monitor id");
|
|
989
|
+
params.set("ids", named);
|
|
990
|
+
}
|
|
991
|
+
if (options.cursor !== void 0) params.set("cursor", options.cursor);
|
|
992
|
+
return readQuery(params, options);
|
|
993
|
+
}
|
|
824
994
|
function createMonitors(config) {
|
|
825
995
|
const path = (monitorId, suffix = "") => {
|
|
826
996
|
if (!monitorId || /^\.+$/.test(monitorId)) throw invalidRequest("a monitor id is required");
|
|
@@ -836,21 +1006,51 @@ function createMonitors(config) {
|
|
|
836
1006
|
const route = `${path(monitorId, "/agent/changes")}${changesQuery(options)}`;
|
|
837
1007
|
return changesPageFrom(await request(config, "GET", route, void 0, options.signal));
|
|
838
1008
|
};
|
|
1009
|
+
const changesAcross = async (scope, options = {}) => {
|
|
1010
|
+
const route = `/v1/monitors/changes${acrossQuery(scope, options)}`;
|
|
1011
|
+
return monitorsChangesPageFrom(await request(config, "GET", route, void 0, options.signal));
|
|
1012
|
+
};
|
|
1013
|
+
const expansionPath = (monitorId, expansionId, action) => {
|
|
1014
|
+
if (!expansionId || /^\.+$/.test(expansionId)) throw invalidRequest("an expansion id is required");
|
|
1015
|
+
return path(monitorId, `/agent/expansions/${encodeURIComponent(expansionId)}:${action}`);
|
|
1016
|
+
};
|
|
1017
|
+
const write = async (route, body, signal) => request(config, "POST", route, body, signal, "rate_limit");
|
|
839
1018
|
return {
|
|
840
|
-
/** Create a monitor. Telem runs `query` on the schedule. */
|
|
841
|
-
async create(query, options
|
|
1019
|
+
/** Create a monitor. Telem runs `query` on the schedule, and its agent keeps what matches. */
|
|
1020
|
+
async create(query, options) {
|
|
1021
|
+
const given = options ?? {};
|
|
1022
|
+
const agent = given.agent == null ? true : Boolean(given.agent);
|
|
1023
|
+
if (agent && !given.ttl) throw invalidRequest(given.inputs == null ? NEEDS_TTL : NEEDS_TTL_INPUTS);
|
|
1024
|
+
const key = given.idempotencyKey ?? globalThis.crypto.randomUUID();
|
|
1025
|
+
if (typeof key !== "string" || !IDEMPOTENCY_KEY.test(key)) {
|
|
1026
|
+
throw invalidRequest("idempotencyKey must be 1 to 255 visible ASCII characters");
|
|
1027
|
+
}
|
|
842
1028
|
const monitorOptions = {};
|
|
843
|
-
if (
|
|
844
|
-
if (
|
|
845
|
-
monitorOptions.include_domains =
|
|
1029
|
+
if (given.frequency !== void 0) monitorOptions.frequency = given.frequency;
|
|
1030
|
+
if (given.includeDomains !== void 0) {
|
|
1031
|
+
monitorOptions.include_domains = given.includeDomains;
|
|
846
1032
|
}
|
|
847
|
-
if (
|
|
1033
|
+
if (given.search !== void 0) monitorOptions.search = searchBlock({}, given.search);
|
|
848
1034
|
const body = { query, options: monitorOptions };
|
|
849
|
-
if (
|
|
850
|
-
if (
|
|
851
|
-
if (
|
|
852
|
-
if (
|
|
853
|
-
|
|
1035
|
+
if (given.metadata !== void 0) body.metadata = given.metadata;
|
|
1036
|
+
if (given.webhookUrl !== void 0) body.webhook_url = given.webhookUrl;
|
|
1037
|
+
if (given.ttl !== void 0) body.ttl = given.ttl;
|
|
1038
|
+
if (given.inputs != null) {
|
|
1039
|
+
body.inputs = given.inputs;
|
|
1040
|
+
const vertical = config.defaults.vertical;
|
|
1041
|
+
if (vertical !== void 0 && !("vertical" in given.inputs)) {
|
|
1042
|
+
body.inputs = { ...given.inputs, vertical };
|
|
1043
|
+
}
|
|
1044
|
+
}
|
|
1045
|
+
body.agent = agent;
|
|
1046
|
+
if (given.preset != null) body.preset = given.preset;
|
|
1047
|
+
if (given.tags != null) body.tags = given.tags;
|
|
1048
|
+
const extra = {
|
|
1049
|
+
headers: { "Idempotency-Key": key },
|
|
1050
|
+
maxRetries: config.createMaxRetries,
|
|
1051
|
+
budgetMs: CREATE_RETRY_BUDGET_MS
|
|
1052
|
+
};
|
|
1053
|
+
const data = await request(config, "POST", "/v1/monitors", body, given.signal, "idempotent", extra).catch(
|
|
854
1054
|
(error) => {
|
|
855
1055
|
if (error instanceof TimeoutError) {
|
|
856
1056
|
throw new TimeoutError(
|
|
@@ -872,17 +1072,21 @@ function createMonitors(config) {
|
|
|
872
1072
|
async get(monitorId, options = {}) {
|
|
873
1073
|
return monitorFrom(await request(config, "GET", path(monitorId), void 0, options.signal));
|
|
874
1074
|
},
|
|
875
|
-
/** Pause, resume or change a monitor. Only the fields given are sent. */
|
|
1075
|
+
/** Pause, resume, renew or change a monitor. Only the fields given are sent. */
|
|
876
1076
|
async update(monitorId, options) {
|
|
877
1077
|
const body = {};
|
|
878
1078
|
if (options.status !== void 0) body.status = options.status;
|
|
879
1079
|
if (options.frequency !== void 0) body.frequency = options.frequency;
|
|
1080
|
+
if (options.ttl !== void 0) body.ttl = options.ttl;
|
|
880
1081
|
if (options.webhookUrl !== void 0) body.webhook_url = options.webhookUrl || null;
|
|
1082
|
+
if (options.tags != null) body.tags = options.tags;
|
|
881
1083
|
const route = path(monitorId);
|
|
882
1084
|
return monitorFrom(await request(config, "PATCH", route, body, options.signal));
|
|
883
1085
|
},
|
|
884
|
-
/** Get the results that are new since the last poll.
|
|
1086
|
+
/** Get the monitor's own results that are new since the last poll. Deprecated: it
|
|
1087
|
+
* warns once per process. */
|
|
885
1088
|
async poll(monitorId, options = {}) {
|
|
1089
|
+
warnPollDeprecated();
|
|
886
1090
|
const body = {};
|
|
887
1091
|
if (options.session !== void 0) body.session_id = options.session;
|
|
888
1092
|
if (options.metadata !== void 0) body.metadata = options.metadata;
|
|
@@ -910,6 +1114,58 @@ function createMonitors(config) {
|
|
|
910
1114
|
if (!page.changes.length || page.nextCursor <= cursor || page.nextCursor >= page.watermark) return;
|
|
911
1115
|
cursor = page.nextCursor;
|
|
912
1116
|
}
|
|
1117
|
+
},
|
|
1118
|
+
/** One page of the changes of up to 100 monitors. Keep `nextCursor` and pass it back as `cursor`. */
|
|
1119
|
+
changesAcross,
|
|
1120
|
+
/** The changes of up to 100 monitors, page by page while `more` is true. `since` goes
|
|
1121
|
+
* with the first page only; the scope, `kinds` and `limit` with every page. */
|
|
1122
|
+
async *iterateChangesAcross(scope, options = {}) {
|
|
1123
|
+
let { cursor, since } = options;
|
|
1124
|
+
for (; ; ) {
|
|
1125
|
+
const page = await changesAcross(scope, { ...options, cursor, since });
|
|
1126
|
+
for (const change of page.changes) yield change;
|
|
1127
|
+
if (!page.more || !page.changes.length || page.nextCursor === cursor) return;
|
|
1128
|
+
cursor = page.nextCursor;
|
|
1129
|
+
since = void 0;
|
|
1130
|
+
}
|
|
1131
|
+
},
|
|
1132
|
+
/** Label one fact of the monitor; with `category`, file a refuted fact there. */
|
|
1133
|
+
async feedback(monitorId, options) {
|
|
1134
|
+
const body = { fact_id: options.factId, verdict: options.verdict };
|
|
1135
|
+
if (options.category != null) body.category = options.category;
|
|
1136
|
+
return feedbackResultFrom(await write(path(monitorId, "/agent/feedback"), body, options.signal));
|
|
1137
|
+
},
|
|
1138
|
+
/** The onboarding sheet of each company of a monitor created with `inputs`. */
|
|
1139
|
+
async onboarding(monitorId, options = {}) {
|
|
1140
|
+
const route = path(monitorId, "/agent/onboarding");
|
|
1141
|
+
return onboardingSheetFrom(await request(config, "GET", route, void 0, options.signal));
|
|
1142
|
+
},
|
|
1143
|
+
/** Apply your review of one company's sheet: `companyId` goes as `company_id`. */
|
|
1144
|
+
async applyOnboarding(monitorId, marks) {
|
|
1145
|
+
const { companyId, ...rest } = marks;
|
|
1146
|
+
const body = setFields({ company_id: companyId, ...rest });
|
|
1147
|
+
return applyResultFrom(await write(path(monitorId, "/agent/onboarding:apply"), body, marks.signal));
|
|
1148
|
+
},
|
|
1149
|
+
/** What the monitor proposes to add, or added; `status` filters. */
|
|
1150
|
+
async expansions(monitorId, options = {}) {
|
|
1151
|
+
const query = options.status !== void 0 ? `?${new URLSearchParams({ status: options.status })}` : "";
|
|
1152
|
+
const route = `${path(monitorId, "/agent/expansions")}${query}`;
|
|
1153
|
+
return expansionsPageFrom(await request(config, "GET", route, void 0, options.signal));
|
|
1154
|
+
},
|
|
1155
|
+
/** Use a proposed addition. */
|
|
1156
|
+
async confirmExpansion(monitorId, expansionId, fields = {}) {
|
|
1157
|
+
const route = expansionPath(monitorId, expansionId, "confirm");
|
|
1158
|
+
return applyResultFrom(await write(route, setFields(fields), fields.signal));
|
|
1159
|
+
},
|
|
1160
|
+
/** Remove an addition. */
|
|
1161
|
+
async revertExpansion(monitorId, expansionId, options = {}) {
|
|
1162
|
+
const route = expansionPath(monitorId, expansionId, "revert");
|
|
1163
|
+
return applyResultFrom(await write(route, {}, options.signal));
|
|
1164
|
+
},
|
|
1165
|
+
/** Every kind and category the monitor collects. */
|
|
1166
|
+
async dataMap(monitorId, options = {}) {
|
|
1167
|
+
const route = path(monitorId, "/agent/data-map");
|
|
1168
|
+
return dataMapFrom(await request(config, "GET", route, void 0, options.signal));
|
|
913
1169
|
}
|
|
914
1170
|
};
|
|
915
1171
|
}
|
|
@@ -1012,8 +1268,11 @@ var Telem = class {
|
|
|
1012
1268
|
maxRetries;
|
|
1013
1269
|
/** The session read surface: `list` and `results(id)`. */
|
|
1014
1270
|
sessions;
|
|
1015
|
-
/** Monitors: `create`, `list`, `listPage`, `get`, `update
|
|
1016
|
-
*
|
|
1271
|
+
/** Monitors: `create`, `list`, `listPage`, `get`, `update` and `delete`, the agent reads
|
|
1272
|
+
* `agent`, `changes` and `iterateChanges`, the read across monitors `changesAcross` and
|
|
1273
|
+
* `iterateChangesAcross`, `feedback`, what a monitor created with
|
|
1274
|
+
* `inputs` collects (`onboarding`, `applyOnboarding`, `expansions`, `confirmExpansion`,
|
|
1275
|
+
* `revertExpansion`, `dataMap`), and the deprecated `poll`. */
|
|
1017
1276
|
monitors;
|
|
1018
1277
|
#config;
|
|
1019
1278
|
/**
|