conare 0.26.0 → 0.26.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/README.md CHANGED
@@ -36,7 +36,7 @@ new Conare({ organization: "org_…" }) // acts for that one (X-Conare-
36
36
  new Conare({ gzip: false }) // JSON bodies of 8 KiB or more go gzip by default
37
37
  ```
38
38
 
39
- Every method also takes `{ signal, timeout }` as its last option.
39
+ Every method also takes `{ signal, timeout }` as its last option. The default timeout is 60 s, and 120 s for a write of documents.
40
40
 
41
41
  A key acts for its own organization. A multi-organization key (made in the console by an admin of
42
42
  each organization it names) acts for any of them: make one client per organization with
@@ -36,8 +36,11 @@ export function readExcluded(file) {
36
36
  try {
37
37
  text = readFileSync(file, "utf8");
38
38
  }
39
- catch {
40
- return new Set();
39
+ catch (err) {
40
+ // Only an absent file means none: an unreadable one would upload what it excludes.
41
+ if (err.code === "ENOENT")
42
+ return new Set();
43
+ throw err;
41
44
  }
42
45
  return new Set(text.split(/\r?\n/).map((l) => l.replace(/#.*/, "").trim()).filter(Boolean));
43
46
  }
@@ -76,6 +76,9 @@ export class GitHub {
76
76
  for (const item of (await res.json()))
77
77
  yield item;
78
78
  url = nextLink(res.headers.get("link"));
79
+ // Every request carries the token: a next page goes nowhere else.
80
+ if (url && new URL(url).origin !== new URL(this.base).origin)
81
+ throw new GitHubError(0, `GitHub's next page is on another host: ${new URL(url).host}`);
79
82
  }
80
83
  }
81
84
  async all(path) {
@@ -264,7 +264,10 @@ async function fetchDocs(gh, repo, commit, files) {
264
264
  for (const [j, f] of chunk.entries()) {
265
265
  const blob = data.repository[`b${j}`];
266
266
  const node = data.repository.head?.[`h${j}`]?.nodes?.[0];
267
- if (!blob || blob.isBinary) {
267
+ // No blob is not a binary file: recorded as one, it is never fetched again and its document goes.
268
+ if (!blob)
269
+ throw new Error(`${repo}: GitHub answered no blob for ${f.path}`);
270
+ if (blob.isBinary) {
268
271
  out.set(f.path, "binary");
269
272
  continue;
270
273
  }
package/dist/cli/main.js CHANGED
@@ -166,9 +166,13 @@ async function syncSessions(args, paths, apiKey, say, fail) {
166
166
  say(report.documents > 0 || report.deleted > 0
167
167
  ? `Synced ${report.sessions} sessions: ${report.documents} documents, ${formatBytes(report.bytes)}. ${report.unchanged} unchanged.`
168
168
  : `Up to date: ${report.unchanged} sessions.`);
169
- code = report.rejected.length > 0 ? INCOMPLETE : 0;
169
+ if (report.unread > 0)
170
+ say(`${report.unread} transcripts hold no message this version can read; they are read again next run.`);
171
+ for (const path of report.unreadable)
172
+ fail(`Could not read, tried again next run: ${path}`);
173
+ code = report.rejected.length > 0 || report.unreadable.length > 0 ? INCOMPLETE : 0;
170
174
  if (code)
171
- error = `${report.rejected.length} documents not accepted`;
175
+ error = `${report.rejected.length} documents not accepted, ${report.unreadable.length} transcripts not read`;
172
176
  }
173
177
  catch (err) {
174
178
  if (err instanceof ConareError && err.status === 401) {
package/dist/cli/sync.js CHANGED
@@ -18,7 +18,7 @@ export async function runSync(opts) {
18
18
  const ns = opts.client?.namespace(NAMESPACE);
19
19
  const log = opts.log ?? (() => { });
20
20
  const state = loadState(opts.stateFile, opts.account);
21
- const report = { sessions: 0, documents: 0, bytes: 0, unchanged: 0, excluded: 0, deleted: 0, rejected: [], planned: [] };
21
+ const report = { sessions: 0, documents: 0, bytes: 0, unchanged: 0, excluded: 0, unread: 0, unreadable: [], deleted: 0, rejected: [], planned: [] };
22
22
  // The layout the server holds for a session: what the state said this tool stored last;
23
23
  // on a fresh state (the first sync, after the migration of older history), or for a session
24
24
  // whose upload an earlier run cut short after some of its documents were accepted (0: any
@@ -128,15 +128,22 @@ export async function runSync(opts) {
128
128
  const transcript = file.read();
129
129
  const project = projectName(transcript.cwd);
130
130
  // Not stamped: taken off the list, the project's sessions upload on the next run.
131
- if (project !== undefined && opts.exclude?.has(project)) {
131
+ // With a list, a session of no known project may be on it: held back too.
132
+ if (project === undefined ? opts.exclude?.size : opts.exclude?.has(project)) {
132
133
  report.excluded++;
133
134
  return;
134
135
  }
136
+ // A new record format reads as no messages, and a stamp would keep the file from the CLI that can read it.
137
+ if (transcript.messages.length === 0) {
138
+ report.unread++;
139
+ return;
140
+ }
135
141
  base = baseId(transcript);
136
- docs = transcript.messages.length > 0 ? toDocuments(transcript, project) : [];
142
+ docs = toDocuments(transcript, project);
137
143
  }
138
144
  catch (err) {
139
145
  log(`Skipped ${file.path}: ${err instanceof Error ? err.message : String(err)}`);
146
+ report.unreadable.push(file.path);
140
147
  return;
141
148
  }
142
149
  // Two files of one session (a session copied between projects), even in different
@@ -9,7 +9,6 @@ export const BATCH_DOCUMENTS = 100;
9
9
  export const BATCH_BYTES = 1_000_000;
10
10
  /** Batches sent at once (sync.ts). */
11
11
  export const UPLOADS_IN_FLIGHT = 4;
12
- const WRITE_TIMEOUT_MS = 120_000;
13
12
  /** Groups documents in order into batches within both limits. */
14
13
  export function batches(docs, maxDocs = BATCH_DOCUMENTS, maxBytes = BATCH_BYTES) {
15
14
  const out = [];
@@ -41,7 +40,7 @@ function isDocumentError(err) {
41
40
  export async function sendBatch(ns, batch) {
42
41
  const unchanged = (results) => results.filter((r) => r.result === "unchanged").length;
43
42
  try {
44
- return { accepted: batch, unchanged: unchanged(await ns.write(batch, { timeout: WRITE_TIMEOUT_MS })), rejected: [] };
43
+ return { accepted: batch, unchanged: unchanged(await ns.write(batch)), rejected: [] };
45
44
  }
46
45
  catch (err) {
47
46
  if (!isDocumentError(err))
@@ -52,7 +51,7 @@ export async function sendBatch(ns, batch) {
52
51
  const result = { accepted: [], unchanged: 0, rejected: [] };
53
52
  for (const doc of batch) {
54
53
  try {
55
- result.unchanged += unchanged(await ns.write(doc, { timeout: WRITE_TIMEOUT_MS }));
54
+ result.unchanged += unchanged(await ns.write(doc));
56
55
  result.accepted.push(doc);
57
56
  }
58
57
  catch (err) {
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export declare const VERSION = "0.26.0";
1
+ export declare const VERSION = "0.26.1";
2
2
  /** A metadata value: string, number, boolean or an array of strings. */
3
3
  export type MetadataValue = string | number | boolean | string[];
4
4
  export type Metadata = Record<string, MetadataValue>;
@@ -528,7 +528,7 @@ export interface ConareOptions {
528
528
  apiKey?: string;
529
529
  /** Defaults to CONARE_BASE_URL, then https://api.conare.ai. */
530
530
  baseURL?: string;
531
- /** Per-request timeout in milliseconds. Default 60000. */
531
+ /** Per-request timeout in milliseconds. Default 60000, and 120000 for a write of documents, which the API may take 110 s over. */
532
532
  timeout?: number;
533
533
  /** Retries on 429, 5xx, 409 `idempotency_in_progress`, network errors and timeouts, every method: each write carries an Idempotency-Key, so a retry never applies it twice. Default 2. */
534
534
  maxRetries?: number;
@@ -600,6 +600,7 @@ export declare class Conare {
600
600
  readonly organization: string | undefined;
601
601
  private readonly apiKey;
602
602
  private readonly timeout;
603
+ readonly writeTimeout: number;
603
604
  private readonly maxRetries;
604
605
  private readonly fetchFn;
605
606
  private gzip;
package/dist/index.js CHANGED
@@ -1,7 +1,9 @@
1
1
  // conare: TypeScript client for the Conare API. Zero dependencies; uses global fetch.
2
- export const VERSION = "0.26.0";
2
+ export const VERSION = "0.26.1";
3
3
  const DEFAULT_BASE_URL = "https://api.conare.ai";
4
4
  const BATCH_SIZE = 100;
5
+ /** Above the API's 110 s deadline for a write: a call given up sooner is retried while the first still runs. */
6
+ const WRITE_TIMEOUT_MS = 120_000;
5
7
  // A JSON body this long or longer goes gzip-encoded: past the first ~14 KB a new connection may send,
6
8
  // each doubling of the window costs a round trip, and JSON of documents is 3-4x smaller in gzip.
7
9
  const GZIP_MIN_BYTES = 8 * 1024;
@@ -127,6 +129,7 @@ export class Conare {
127
129
  organization;
128
130
  apiKey;
129
131
  timeout;
132
+ writeTimeout;
130
133
  maxRetries;
131
134
  fetchFn;
132
135
  gzip;
@@ -143,6 +146,7 @@ export class Conare {
143
146
  end--;
144
147
  this.baseURL = baseURL.slice(0, end);
145
148
  this.timeout = options.timeout ?? 60_000;
149
+ this.writeTimeout = options.timeout ?? WRITE_TIMEOUT_MS;
146
150
  this.maxRetries = options.maxRetries ?? 2;
147
151
  this.fetchFn = options.fetch ?? ((url, init) => fetch(url, init));
148
152
  this.organization = options.organization || undefined;
@@ -369,7 +373,7 @@ export class Namespace {
369
373
  seq: d.seq,
370
374
  if_version: d.ifVersion,
371
375
  })),
372
- }, { ...options, idempotencyKey: stepKey(key, i / BATCH_SIZE) });
376
+ }, { timeout: this.client.writeTimeout, ...options, idempotencyKey: stepKey(key, i / BATCH_SIZE) });
373
377
  body.ids.forEach((id, j) => {
374
378
  const r = body.results?.[j];
375
379
  const result = { id, version: body.versions?.[j] ?? null };
@@ -510,7 +514,7 @@ export class Namespace {
510
514
  * does not exist throws ConareError 404 `namespace_not_found`. Returns how many were deleted.
511
515
  */
512
516
  async deleteMany(ids, options) {
513
- const out = await this.client.request("POST", this.path("/documents/delete"), { ids }, options);
517
+ const out = await this.client.request("POST", this.path("/documents/delete"), { ids }, { timeout: this.client.writeTimeout, ...options });
514
518
  return out.deleted;
515
519
  }
516
520
  /** Delete every document matching filter (at least one condition), calling until none match. Returns how many were deleted. */
@@ -519,7 +523,7 @@ export class Namespace {
519
523
  const key = options?.idempotencyKey;
520
524
  for (let round = 0;; round++) {
521
525
  // Each round is its own call (the same body deletes the next batch): its own key.
522
- const out = await this.client.request("POST", this.path("/documents/delete"), { filter }, { ...options, idempotencyKey: stepKey(key, round) });
526
+ const out = await this.client.request("POST", this.path("/documents/delete"), { filter }, { timeout: this.client.writeTimeout, ...options, idempotencyKey: stepKey(key, round) });
523
527
  deleted += out.deleted;
524
528
  if (!out.more)
525
529
  return deleted;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "conare",
3
- "version": "0.26.0",
3
+ "version": "0.26.1",
4
4
  "description": "TypeScript client for Conare: add documents, query them, connect apps.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",