conare 0.13.1 → 0.15.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 CHANGED
@@ -7,7 +7,7 @@ No dependencies. Works on Node 18+, Bun, Deno and edge runtimes.
7
7
 
8
8
  ```bash
9
9
  npm install conare
10
- export CONARE_API_KEY=sk-conare-... # conare.ai/console/keys
10
+ export CONARE_API_KEY=sk-conare-... # conare.ai/dashboard/keys
11
11
  ```
12
12
 
13
13
  ```ts
@@ -42,15 +42,19 @@ const docs = conare.namespace("docs");
42
42
 
43
43
  await docs.add({ id, text, metadata }); // one document
44
44
  await docs.add([...]); // or many; sent 100 at a time. Returns the ids.
45
- await docs.get("refunds"); // { id, text, metadata, updatedAt } or null
45
+ await docs.write({ id, text, ifVersion: 7 }); // add, only if the document is at version 7 (0: only if
46
+ // it does not exist), else 409 version_conflict.
47
+ // Returns [{ id, version }]
48
+ await docs.get("refunds"); // { id, text, metadata, updatedAt, version } or null
46
49
  for await (const doc of docs.list()) {} // every document, page by page ({ limit } per page: 1-100, default 50)
47
50
  await docs.delete("refunds"); // 404 document_not_found if it does not exist
48
51
  await docs.deleteMany(["a", "b"]); // up to 1000 ids in one call; missing ids are skipped. Returns the count
49
52
  await docs.deleteWhere({ team: "support" }); // every document matching the filter, whole documents. Returns the count
50
- await docs.drop(); // delete the namespace and its documents (also succeeds if it is empty)
53
+ await docs.drop(); // delete the namespace and its documents (succeeds if already emptied; 404 namespace_not_found if never written)
51
54
  await docs.create({ sparse: true }); // create it with a sparse (SPLADE) index, before its first write
52
55
 
53
56
  await conare.namespaces(); // [{ name, documents }]
57
+ await conare.namespaces({ prefix: "u7f3a9c-" }); // one user's namespaces
54
58
  ```
55
59
 
56
60
  Namespace names: 1-64 characters of `a-z 0-9 _ -`. Ids: up to 256 characters of
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export declare const VERSION = "0.13.1";
1
+ export declare const VERSION = "0.15.0";
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>;
@@ -34,17 +34,25 @@ export interface DocumentInput {
34
34
  id?: string;
35
35
  text: string;
36
36
  metadata?: Metadata;
37
+ /**
38
+ * Write only if the document holds this version (`0`: only if it does not exist), else a
39
+ * ConareError 409 `version_conflict`. Checked atomically with the write.
40
+ */
41
+ ifVersion?: number;
37
42
  }
38
43
  export interface Document {
39
44
  id: string;
40
45
  text: string;
41
46
  metadata: Metadata;
42
47
  updatedAt: Date;
48
+ /** Changes on every write of the document and only grows: pass it as `ifVersion`. */
49
+ version: number;
43
50
  }
44
51
  export interface DocumentSummary {
45
52
  id: string;
46
53
  metadata: Metadata;
47
54
  updatedAt: Date;
55
+ version: number;
48
56
  }
49
57
  export interface QueryResult {
50
58
  id: string;
@@ -128,8 +136,13 @@ export declare class Conare {
128
136
  constructor(options?: ConareOptions);
129
137
  /** A handle to one namespace. Nothing is sent until you call a method; the first write creates it. */
130
138
  namespace(name: string): Namespace;
131
- /** Every namespace in the organization with its document count. */
132
- namespaces(options?: RequestOptions): Promise<NamespaceInfo[]>;
139
+ /**
140
+ * Every namespace in the organization (a scoped key: every one it reaches) with its document
141
+ * count; with `prefix`, only those whose name starts with it, e.g. one user's `"u7f3a9c-"`.
142
+ */
143
+ namespaces(options?: RequestOptions & {
144
+ prefix?: string;
145
+ }): Promise<NamespaceInfo[]>;
133
146
  /** Embed texts. Use type "query" for search queries and "document" (default) for stored text. */
134
147
  embed(input: string | string[], options?: RequestOptions & {
135
148
  type?: "document" | "query";
@@ -161,15 +174,27 @@ export declare class Namespace {
161
174
  * A document without an id gets one here, so a retried batch replaces instead of duplicating.
162
175
  */
163
176
  add(documents: DocumentInput | DocumentInput[], options?: RequestOptions): Promise<string[]>;
177
+ /**
178
+ * `add` that returns each document's new version, to pass as `ifVersion` on its next write.
179
+ * A conditional write retried after a lost response can answer 409 `version_conflict` for its
180
+ * own first attempt: `get()` the document to tell. `version` is null when the server did not
181
+ * answer one (a server older than versions): never pass it as `ifVersion`.
182
+ */
183
+ write(documents: DocumentInput | DocumentInput[], options?: RequestOptions): Promise<{
184
+ id: string;
185
+ version: number | null;
186
+ }[]>;
164
187
  /**
165
188
  * Hybrid search, reranked. One result per document: its best passage. `retrievers` picks what
166
189
  * the search fuses (default `["vector", "keyword"]`); `"sparse"` needs a namespace created with
167
- * a sparse index.
190
+ * a sparse index. `rerankDepth` (1-100) has the reranker score every passage among the first
191
+ * that many, 20 at a time (about 100 ms each); unset, it reads one window of 20.
168
192
  */
169
193
  query(query: string, options?: RequestOptions & {
170
194
  topK?: number;
171
195
  filter?: Filter;
172
196
  rerank?: boolean;
197
+ rerankDepth?: number;
173
198
  retrievers?: Retriever[];
174
199
  }): Promise<QueryResult[]>;
175
200
  /** One document by id, or null if it does not exist. */
@@ -184,7 +209,7 @@ export declare class Namespace {
184
209
  deleteMany(ids: string[], options?: RequestOptions): Promise<number>;
185
210
  /** Delete every document matching filter (at least one condition), calling until none match. Returns how many were deleted. */
186
211
  deleteWhere(filter: Filter, options?: RequestOptions): Promise<number>;
187
- /** Delete the namespace and every document in it. Succeeds as well when it holds no documents (already deleted or never written). */
212
+ /** Delete the namespace and every document in it. Succeeds as well when it was already deleted or emptied; throws ConareError 404 `namespace_not_found` for a name never written. */
188
213
  drop(options?: RequestOptions): Promise<void>;
189
214
  }
190
215
  /** Apps connected through Conare that keep syncing into a namespace. */
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  // conare: TypeScript client for the Conare API. Zero dependencies; uses global fetch.
2
- export const VERSION = "0.13.1";
2
+ export const VERSION = "0.15.0";
3
3
  const DEFAULT_BASE_URL = "https://api.conare.ai";
4
4
  const BATCH_SIZE = 100;
5
5
  // ---- Errors ----------------------------------------------------------------
@@ -63,9 +63,13 @@ export class Conare {
63
63
  namespace(name) {
64
64
  return new Namespace(this, name);
65
65
  }
66
- /** Every namespace in the organization with its document count. */
67
- async namespaces(options) {
68
- const body = await this.request("GET", "/namespaces", undefined, options);
66
+ /**
67
+ * Every namespace in the organization (a scoped key: every one it reaches) with its document
68
+ * count; with `prefix`, only those whose name starts with it, e.g. one user's `"u7f3a9c-"`.
69
+ */
70
+ async namespaces(options = {}) {
71
+ const path = options.prefix ? `/namespaces?prefix=${encodeURIComponent(options.prefix)}` : "/namespaces";
72
+ const body = await this.request("GET", path, undefined, options);
69
73
  return body.namespaces;
70
74
  }
71
75
  /** Embed texts. Use type "query" for search queries and "document" (default) for stored text. */
@@ -176,29 +180,46 @@ export class Namespace {
176
180
  * A document without an id gets one here, so a retried batch replaces instead of duplicating.
177
181
  */
178
182
  async add(documents, options) {
183
+ return (await this.write(documents, options)).map((d) => d.id);
184
+ }
185
+ /**
186
+ * `add` that returns each document's new version, to pass as `ifVersion` on its next write.
187
+ * A conditional write retried after a lost response can answer 409 `version_conflict` for its
188
+ * own first attempt: `get()` the document to tell. `version` is null when the server did not
189
+ * answer one (a server older than versions): never pass it as `ifVersion`.
190
+ */
191
+ async write(documents, options) {
179
192
  const all = Array.isArray(documents) ? documents : [documents];
180
- const ids = [];
193
+ const out = [];
181
194
  for (let i = 0; i < all.length; i += BATCH_SIZE) {
182
195
  const batch = all.slice(i, i + BATCH_SIZE);
183
- const body = await this.client.request("POST", this.path("/documents"), { documents: batch.map((d) => ({ id: d.id ?? newDocumentId(), text: d.text, metadata: d.metadata })) }, options, true);
184
- ids.push(...body.ids);
196
+ const body = await this.client.request("POST", this.path("/documents"), {
197
+ documents: batch.map((d) => ({
198
+ id: d.id ?? newDocumentId(),
199
+ text: d.text,
200
+ metadata: d.metadata,
201
+ if_version: d.ifVersion,
202
+ })),
203
+ }, options, true);
204
+ body.ids.forEach((id, j) => out.push({ id, version: body.versions?.[j] ?? null }));
185
205
  }
186
- return ids;
206
+ return out;
187
207
  }
188
208
  /**
189
209
  * Hybrid search, reranked. One result per document: its best passage. `retrievers` picks what
190
210
  * the search fuses (default `["vector", "keyword"]`); `"sparse"` needs a namespace created with
191
- * a sparse index.
211
+ * a sparse index. `rerankDepth` (1-100) has the reranker score every passage among the first
212
+ * that many, 20 at a time (about 100 ms each); unset, it reads one window of 20.
192
213
  */
193
214
  async query(query, options = {}) {
194
- const body = await this.client.request("POST", this.path("/query"), { query, top_k: options.topK, filter: options.filter, rerank: options.rerank, retrievers: options.retrievers }, options, true);
215
+ const body = await this.client.request("POST", this.path("/query"), { query, top_k: options.topK, filter: options.filter, rerank: options.rerank, rerank_depth: options.rerankDepth, retrievers: options.retrievers }, options, true);
195
216
  return body.results;
196
217
  }
197
218
  /** One document by id, or null if it does not exist. */
198
219
  async get(id, options) {
199
220
  try {
200
221
  const d = await this.client.request("GET", this.path(`/documents/${segment(id)}`), undefined, options);
201
- return { id: d.id, text: d.text, metadata: d.metadata, updatedAt: new Date(d.updated_at) };
222
+ return { id: d.id, text: d.text, metadata: d.metadata, updatedAt: new Date(d.updated_at), version: d.version };
202
223
  }
203
224
  catch (err) {
204
225
  if (err instanceof ConareError && err.status === 404)
@@ -218,7 +239,7 @@ export class Namespace {
218
239
  const qs = params.toString();
219
240
  const page = await this.client.request("GET", this.path(`/documents${qs ? `?${qs}` : ""}`), undefined, options);
220
241
  for (const d of page.documents) {
221
- yield { id: d.id, metadata: d.metadata, updatedAt: new Date(d.updated_at) };
242
+ yield { id: d.id, metadata: d.metadata, updatedAt: new Date(d.updated_at), version: d.version };
222
243
  }
223
244
  cursor = page.next_cursor;
224
245
  } while (cursor);
@@ -242,7 +263,7 @@ export class Namespace {
242
263
  return deleted;
243
264
  }
244
265
  }
245
- /** Delete the namespace and every document in it. Succeeds as well when it holds no documents (already deleted or never written). */
266
+ /** Delete the namespace and every document in it. Succeeds as well when it was already deleted or emptied; throws ConareError 404 `namespace_not_found` for a name never written. */
246
267
  async drop(options) {
247
268
  await this.client.request("DELETE", this.path(), undefined, options);
248
269
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "conare",
3
- "version": "0.13.1",
3
+ "version": "0.15.0",
4
4
  "description": "TypeScript client for Conare: add documents, query them, connect apps.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",