triqai 0.3.0 → 0.4.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/CHANGELOG.md CHANGED
@@ -7,6 +7,32 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [0.4.1] - 2026-11-29
11
+
12
+ ### Fixed
13
+
14
+ - In Node.js, a response whose body the connection cut off now rejects with `ConnectionError` instead of a raw `TypeError: terminated`, and an enrichment with an `Idempotency-Key` is retried like any other network failure.
15
+
16
+ ## [0.4.0] - 2026-11-23
17
+
18
+ ### Added
19
+
20
+ - Client-side pacing with the `pacing` option and the `PacingOptions` type. Calls on a client wait their turn within the rate and concurrency limits the API reports, instead of being refused with `429`. `pacing: { maxConcurrency, requestsPerSecond }` sets lower limits for a client, for example when several processes share an organization; `pacing: false` sends every request immediately.
21
+ - `pacing.maxWait`: the longest a call waits for its turn before its first attempt (default: `maxRetryTime`). It then rejects with the new `PacingTimeoutError`, a `TimeoutError` with code `pacing_timeout`, without being sent or charged.
22
+ - A `signal` option (`AbortSignal`) on every request method, including reads, `health()`, `apiInfo()` and the raw methods, through the new `RequestOptions` type. It cancels a call while it waits, between retries or in flight, and rejects with the signal's reason.
23
+
24
+ ### Changed
25
+
26
+ - Pacing is on by default in Node.js and Bun. Enrichments in flight stay within `X-RateLimit-Concurrency-Limit`, and all requests within the per-second rate in `X-RateLimit-Limit`. Until the API has reported its limits, at most five requests are in flight.
27
+ - After a `429`, every call on the client waits for the limit it names (`rps`, or `concurrency` for enrichments) until `Retry-After`, instead of each call retrying on its own. The refused call keeps its place and goes first when the pause ends.
28
+ - With pacing, `maxRetryTime` starts when a call's first attempt is sent, and a retry is only sent with at least `min(1s, timeout / 2)` of it left; otherwise the last server error is returned.
29
+
30
+ ### Notes
31
+
32
+ - Cloudflare Workers, Deno, edge runtimes and browsers: pacing is off by default. Workers bind each request's work to that request, so create a client per request there or keep pacing off. If you opt in with a client shared across requests, a grant that its request no longer takes up returns to the pool within a second.
33
+ - Tests with fake timers: pacing uses `Date.now()` and `setTimeout`, so advance a faked clock with both faked, or set `pacing: false` in those tests.
34
+ - Unpaced clients (`pacing: false`) keep the 0.3.0 retry and budget behaviour.
35
+
10
36
  ## [0.3.0] - 2026-11-16
11
37
 
12
38
  ### Added
package/README.md CHANGED
@@ -61,6 +61,9 @@ const triqai = new Triqai("triq_your_api_key", {
61
61
  // Per-attempt timeout, including response body, in ms (default: 60000)
62
62
  timeout: 60_000,
63
63
 
64
+ // Pacing within your rate limits (default: on in Node.js and Bun). See "Pacing" below.
65
+ pacing: true,
66
+
64
67
  // Extra headers for every request
65
68
  defaultHeaders: {
66
69
  "X-Custom-Header": "value",
@@ -124,6 +127,24 @@ const result = await triqai.transactions.enrich(
124
127
 
125
128
  The `force` option is deprecated and has no effect. The API has no cache bypass, so the SDK no longer sends it.
126
129
 
130
+ #### Cancel a call
131
+
132
+ Every method accepts a `signal` in its last options argument:
133
+
134
+ ```typescript
135
+ const controller = new AbortController();
136
+ const result = await triqai.transactions.enrich(
137
+ { title: "STARBUCKS", country: "US", type: "expense" },
138
+ { signal: controller.signal },
139
+ );
140
+
141
+ const page = await triqai.transactions.list({ size: 50 }, { signal: AbortSignal.timeout(30_000) });
142
+ const merchant = await triqai.merchants.get("merchant-uuid", { signal: controller.signal });
143
+ const raw = await triqai.rawGet("/v1/categories", undefined, { signal: controller.signal });
144
+ ```
145
+
146
+ Aborting rejects the call with the signal's reason (an `AbortError` by default), including while it waits its turn, between retries, or in flight.
147
+
127
148
  #### List transactions
128
149
 
129
150
  ```typescript
@@ -314,6 +335,7 @@ try {
314
335
  | `GatewayTimeoutError` | 504 | Upstream timeout |
315
336
  | `ConnectionError` | - | Network/DNS failure |
316
337
  | `TimeoutError` | - | Request exceeded timeout |
338
+ | `PacingTimeoutError` | - | A call waited longer than `pacing.maxWait` for its turn; it was never sent (a `TimeoutError` with code `pacing_timeout`) |
317
339
 
318
340
  ## Automatic Retries
319
341
 
@@ -323,7 +345,8 @@ The SDK automatically retries on transient errors (429, 500, 503, 504, and netwo
323
345
  - Enrichment calls generate an idempotency key and reuse it across automatic retries. Supply your own `idempotencyKey` to safely retry across separate calls
324
346
  - A `409` for the same enrichment that is still in progress is retried after `Retry-After` and returns the stored result. A `409` with `reason: "payload_mismatch"` is not retried
325
347
  - The `Retry-After` header is a minimum delay; the SDK never retries early to fit a limit
326
- - Calls have a 5-minute total budget (`maxRetryTime`), including attempts, response bodies, and retry waits. If a retry delay cannot fit, the SDK throws the last error with its retry information so you can schedule a later call using the same idempotency key
348
+ - With pacing, after a `429` other calls on the same client wait too (see [Pacing](#pacing))
349
+ - Calls have a 5-minute total budget (`maxRetryTime`), including attempts, response bodies, and retry waits. If a retry delay cannot fit, the SDK throws the last error with its retry information so you can schedule a later call using the same idempotency key. With pacing, the budget starts when the first attempt is sent, and a retry is only sent with at least `min(1s, timeout / 2)` of it left
327
350
  - Default: 3 retries, 500ms base delay, 30s max delay
328
351
 
329
352
  Disable retries:
@@ -332,9 +355,47 @@ Disable retries:
332
355
  const triqai = new Triqai("triq_your_api_key", { maxRetries: 0 });
333
356
  ```
334
357
 
358
+ ## Pacing
359
+
360
+ Pacing keeps a client's requests within your organization's rate limits, so a burst of calls, such as `Promise.all` over a batch, waits its turn on the client instead of being refused with `429`. It is on by default in Node.js and Bun, and off by default elsewhere (see [Cloudflare Workers and other runtimes](#cloudflare-workers-and-other-runtimes)).
361
+
362
+ - **Concurrency.** Enrichments in flight stay within `X-RateLimit-Concurrency-Limit`. Only enrichments wait for one of these slots, so reads are not held behind a queued batch.
363
+ - **Rate.** All requests are spaced to the per-second rate in `X-RateLimit-Limit` on responses whose `X-RateLimit-Scope` is `rps` or absent. When `X-RateLimit-Remaining` shows fewer requests left than the client expects, for example while another process uses the same organization, the client slows down to match.
364
+ - **Before the limits are known.** Until a response reports them, at most 5 requests are in flight, enrichments included. A proxy that strips the `X-RateLimit-*` headers keeps a client at this limit; set `pacing: false` or explicit limits in that case.
365
+ - **After a `429`.** Every call on the client waits for the limit it names: `X-RateLimit-Scope: rps`, or no scope, pauses all requests; `concurrency` pauses enrichments, and enrichments that hold a slot but have not been sent give it back. The pause lasts the `Retry-After` time plus up to 20% (at most 1 second) of jitter. Without `Retry-After`, it lasts until `X-RateLimit-Reset`, measured against the response's `Date` header, when that is at most 10 seconds away; otherwise the normal backoff applies. Other calls wait at most 60 seconds before trying again.
366
+ - **Fewer enrichments after a `concurrency` 429.** The client keeps fewer enrichments in flight than were running when it was refused, then adds one back after as many successful enrichments as its current limit, counting only enrichments sent after the decrease. Refusals of enrichments sent before the limit was known do not lower it.
367
+ - **Retries first.** A call that is retried keeps its place, and its enrichment slot while the limit allows, and goes ahead of calls that have not been sent yet.
368
+ - **Budgets.** Waiting for a turn before the first attempt is limited by `pacing.maxWait` (default: `maxRetryTime`), not by `timeout` or `maxRetryTime`. A call that waits longer rejects with `PacingTimeoutError` and is never sent or charged. `maxRetryTime` starts when the first attempt is sent. Pass a [`signal`](#cancel-a-call) to cancel a call at any point.
369
+
370
+ Pacing is per client, so share one client across a batch. Processes that share an organization also share its limits: give each one lower limits. Your limits apply when they are lower than the API's:
371
+
372
+ ```typescript
373
+ const triqai = new Triqai("triq_your_api_key", {
374
+ pacing: {
375
+ maxConcurrency: 5, // enrichments in flight
376
+ requestsPerSecond: 3, // all requests, retries included
377
+ maxWait: 600_000, // wait up to 10 minutes for a turn
378
+ },
379
+ });
380
+ ```
381
+
382
+ To send every request as soon as it is made, as before pacing, disable it:
383
+
384
+ ```typescript
385
+ const triqai = new Triqai("triq_your_api_key", { pacing: false });
386
+ ```
387
+
388
+ ### Cloudflare Workers and other runtimes
389
+
390
+ Pacing is off by default in Cloudflare Workers, Deno, edge runtimes and browsers. Workers bind the work of each request to that request, so one request cannot reliably wait for another's call to finish. In Workers, create the client inside each request handler, or keep pacing off. Within one request, `pacing: true` still paces a batch of calls on that request's client. If you opt in with a client shared across requests, a slot granted to a request that no longer runs returns to the pool within a second; pass `{ signal: request.signal }` to each call, so a call whose client has gone is not sent.
391
+
392
+ ### Tests with fake timers
393
+
394
+ Pacing uses `Date.now()` and `setTimeout`. In tests that fake timers, fake both and advance the clock, or set `pacing: false`.
395
+
335
396
  ## Rate Limiting
336
397
 
337
- Rate limit information is available on successful responses. The SDK handles 429 responses automatically, but you can monitor usage via the `onResponse` hook or `rawGet`/`rawPost`/`rawDelete` methods:
398
+ Rate limit information is available on responses from rate-limited endpoints. The SDK paces requests and handles 429 responses automatically, but you can monitor usage via the `onResponse` hook or `rawGet`/`rawPost`/`rawDelete` methods:
338
399
 
339
400
  ```typescript
340
401
  // Option 1: Debug hook