Pagination and cancelling in the TypeScript SDK

Go through every page of a list, stop a call with a signal, and set its timeout.

Pages

A list call, such as client.documents.list, returns a list you can use in three ways:

import { ByteRover } from "@byterover/sdk";

const client = new ByteRover();
const driveId = "drv_4h7k2m9q8r1t5v6w3x0y";

// Every document, page after page.
for await (const document of client.documents.list(driveId)) console.log(document.path);

// One page: the API's response, with next_cursor.
const first = await client.documents.list(driveId, { limit: 50 });

// Every page.
for await (const page of client.documents.list(driveId).pages()) console.log(page.documents.length);

// The next page yourself, from a cursor you kept.
if (first.next_cursor) {
  const second = await client.documents.list(driveId, { cursor: first.next_cursor });
  console.log(second.documents.length);
}

The SDK asks for the next page only when your loop needs it. Leaving the loop early stops there.

Cancel a call

Every call takes a signal. Aborting it stops the SDK at once: the call rejects with your signal's reason, and any retry waiting to start is dropped.

const controller = new AbortController();
setTimeout(() => controller.abort(new Error("The user left.")), 5_000);

try {
  await client.search(
    { query: "Which policy section covers EU refunds?", drive_ids: [driveId], effort: "high" },
    { signal: controller.signal },
  );
} catch (error) {
  if (!controller.signal.aborted) throw error;
}

Aborting stops your side only. The API may still finish what it started, and charge a search that ran. There is no call that cancels work on the server.

Timeouts

timeout is the time for each attempt, in milliseconds: 60 seconds by default, 105 seconds for a search at medium and 135 seconds at high. Set it on the client or on one call; a search keeps its own unless you pass timeout on that call. A download also fails when nothing arrives for that long.

const quick = await client.drives.list({ timeout: 5_000 });

const everything = await client.operations.wait("op_3g6j9m2p5s8v1y4b7e0h", {
  timeout: 900_000, // how long to keep waiting
  request: { timeout: 10_000 }, // each poll
});
console.log(quick.drives.length, everything.status);

A call that runs out of time throws an APITimeoutError. A wait that runs out of time throws a WaitTimeoutError; the operation goes on, and you can wait for it again.

On this page