conare 0.12.0 → 0.13.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
@@ -48,6 +48,7 @@ await docs.delete("refunds"); // 404 document_not_found if it d
48
48
  await docs.deleteMany(["a", "b"]); // up to 1000 ids in one call; missing ids are skipped. Returns the count
49
49
  await docs.deleteWhere({ team: "support" }); // every document matching the filter, whole documents. Returns the count
50
50
  await docs.drop(); // delete the namespace and its documents (also succeeds if it is empty)
51
+ await docs.create({ sparse: true }); // create it with a sparse (SPLADE) index, before its first write
51
52
 
52
53
  await conare.namespaces(); // [{ name, documents }]
53
54
  ```
@@ -62,10 +63,13 @@ await docs.query("how do refunds work?", {
62
63
  topK: 10, // 1-100, default 10
63
64
  filter: { team: "support" }, // optional metadata filter
64
65
  rerank: true, // default true
66
+ retrievers: ["vector", "keyword"], // default; add "sparse" in a namespace created with a sparse index
65
67
  });
66
68
  ```
67
69
 
68
- Hybrid search (vector and keyword), reranked. One result per document: its best passage.
70
+ Hybrid search, reranked. One result per document: its best passage. `retrievers` picks what it
71
+ fuses: `"vector"` (the embedding model), `"keyword"` (BM25; alone, BM25 alone) and `"sparse"` (the
72
+ learned-sparse SPLADE model, in a namespace created with `create({ sparse: true })`).
69
73
  `score` is the reranker's when `reranked` is true, else the fused rank score of a result past the
70
74
  rerank window (or of a query not reranked); the two scales do not compare.
71
75
 
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export declare const VERSION = "0.12.0";
1
+ export declare const VERSION = "0.13.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>;
@@ -56,6 +56,8 @@ export interface QueryResult {
56
56
  reranked: boolean;
57
57
  metadata: Metadata;
58
58
  }
59
+ /** What a query fuses: the embedding model, BM25 keywords, or the learned-sparse (SPLADE) model. */
60
+ export type Retriever = "vector" | "keyword" | "sparse";
59
61
  export interface NamespaceInfo {
60
62
  name: string;
61
63
  documents: number;
@@ -144,16 +146,31 @@ export declare class Namespace {
144
146
  readonly name: string;
145
147
  constructor(client: Conare, name: string);
146
148
  private path;
149
+ /**
150
+ * Create the namespace before its first write. Only a namespace with a sparse (SPLADE) index
151
+ * needs this: a write creates one without. Its index never changes: creating it again with the
152
+ * other setting is a 409. Returns whether it has a sparse index.
153
+ */
154
+ create(options?: RequestOptions & {
155
+ sparse?: boolean;
156
+ }): Promise<{
157
+ sparse: boolean;
158
+ }>;
147
159
  /**
148
160
  * Add or replace documents. Accepts one or many; sends them in batches of 100. Returns the ids.
149
161
  * A document without an id gets one here, so a retried batch replaces instead of duplicating.
150
162
  */
151
163
  add(documents: DocumentInput | DocumentInput[], options?: RequestOptions): Promise<string[]>;
152
- /** Hybrid search, reranked. One result per document: its best passage. */
164
+ /**
165
+ * Hybrid search, reranked. One result per document: its best passage. `retrievers` picks what
166
+ * the search fuses (default `["vector", "keyword"]`); `"sparse"` needs a namespace created with
167
+ * a sparse index.
168
+ */
153
169
  query(query: string, options?: RequestOptions & {
154
170
  topK?: number;
155
171
  filter?: Filter;
156
172
  rerank?: boolean;
173
+ retrievers?: Retriever[];
157
174
  }): Promise<QueryResult[]>;
158
175
  /** One document by id, or null if it does not exist. */
159
176
  get(id: string, options?: RequestOptions): Promise<Document | null>;
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.12.0";
2
+ export const VERSION = "0.13.1";
3
3
  const DEFAULT_BASE_URL = "https://api.conare.ai";
4
4
  const BATCH_SIZE = 100;
5
5
  // ---- Errors ----------------------------------------------------------------
@@ -16,6 +16,18 @@ export class ConareError extends Error {
16
16
  this.requestId = requestId;
17
17
  }
18
18
  }
19
+ /**
20
+ * One path segment (a namespace, document id or source id), percent-encoded. A value holding "/"
21
+ * or "\\", or one that is "." or "..", is refused here: the API's ingress decodes %2F and resolves
22
+ * dot segments before routing, so such a value would reach another route, even another namespace.
23
+ * No valid name or id contains them.
24
+ */
25
+ function segment(value) {
26
+ if (/[\\/]/.test(value) || value === "." || value === "..") {
27
+ throw new ConareError(400, "invalid_id", `${JSON.stringify(value)} cannot be a path segment: it holds "/" or "\\" or is a dot segment.`);
28
+ }
29
+ return encodeURIComponent(value);
30
+ }
19
31
  // ---- Client ----------------------------------------------------------------
20
32
  /**
21
33
  * The Conare client.
@@ -148,7 +160,16 @@ export class Namespace {
148
160
  this.name = name;
149
161
  }
150
162
  path(suffix = "") {
151
- return `/namespaces/${encodeURIComponent(this.name)}${suffix}`;
163
+ return `/namespaces/${segment(this.name)}${suffix}`;
164
+ }
165
+ /**
166
+ * Create the namespace before its first write. Only a namespace with a sparse (SPLADE) index
167
+ * needs this: a write creates one without. Its index never changes: creating it again with the
168
+ * other setting is a 409. Returns whether it has a sparse index.
169
+ */
170
+ async create(options = {}) {
171
+ const body = await this.client.request("POST", "/namespaces", { name: this.name, sparse: options.sparse ?? false }, options, true);
172
+ return { sparse: body.sparse };
152
173
  }
153
174
  /**
154
175
  * Add or replace documents. Accepts one or many; sends them in batches of 100. Returns the ids.
@@ -164,15 +185,19 @@ export class Namespace {
164
185
  }
165
186
  return ids;
166
187
  }
167
- /** Hybrid search, reranked. One result per document: its best passage. */
188
+ /**
189
+ * Hybrid search, reranked. One result per document: its best passage. `retrievers` picks what
190
+ * the search fuses (default `["vector", "keyword"]`); `"sparse"` needs a namespace created with
191
+ * a sparse index.
192
+ */
168
193
  async query(query, options = {}) {
169
- const body = await this.client.request("POST", this.path("/query"), { query, top_k: options.topK, filter: options.filter, rerank: options.rerank }, options, true);
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);
170
195
  return body.results;
171
196
  }
172
197
  /** One document by id, or null if it does not exist. */
173
198
  async get(id, options) {
174
199
  try {
175
- const d = await this.client.request("GET", this.path(`/documents/${encodeURIComponent(id)}`), undefined, options);
200
+ const d = await this.client.request("GET", this.path(`/documents/${segment(id)}`), undefined, options);
176
201
  return { id: d.id, text: d.text, metadata: d.metadata, updatedAt: new Date(d.updated_at) };
177
202
  }
178
203
  catch (err) {
@@ -200,7 +225,7 @@ export class Namespace {
200
225
  }
201
226
  /** Delete one document. Throws a ConareError with status 404 (`document_not_found`) if it does not exist. */
202
227
  async delete(id, options) {
203
- await this.client.request("DELETE", this.path(`/documents/${encodeURIComponent(id)}`), undefined, options);
228
+ await this.client.request("DELETE", this.path(`/documents/${segment(id)}`), undefined, options);
204
229
  }
205
230
  /** Delete up to 1000 documents by id in one call. Ids that do not exist are skipped. Returns how many were deleted. */
206
231
  async deleteMany(ids, options) {
@@ -244,16 +269,16 @@ export class Sources {
244
269
  }
245
270
  /** One source by id. */
246
271
  async get(id, options) {
247
- return toSource(await this.client.request("GET", `/sources/${encodeURIComponent(id)}`, undefined, options));
272
+ return toSource(await this.client.request("GET", `/sources/${segment(id)}`, undefined, options));
248
273
  }
249
274
  /** Start a sync now. */
250
275
  async sync(id, options) {
251
- return toSource(await this.client.request("POST", `/sources/${encodeURIComponent(id)}/sync`, undefined, options, true));
276
+ return toSource(await this.client.request("POST", `/sources/${segment(id)}/sync`, undefined, options, true));
252
277
  }
253
278
  /** Disconnect a source. Pass `deleteDocuments: true` to also delete what it synced. */
254
279
  async delete(id, options = {}) {
255
280
  const qs = options.deleteDocuments ? "?delete_documents=true" : "";
256
- await this.client.request("DELETE", `/sources/${encodeURIComponent(id)}${qs}`, undefined, options);
281
+ await this.client.request("DELETE", `/sources/${segment(id)}${qs}`, undefined, options);
257
282
  }
258
283
  /** The apps you can connect. */
259
284
  async types(options) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "conare",
3
- "version": "0.12.0",
3
+ "version": "0.13.1",
4
4
  "description": "TypeScript client for Conare: add documents, query them, connect apps.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",