conare 0.29.0 → 0.30.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 +38 -4
- package/dist/index.d.ts +131 -11
- package/dist/index.js +89 -11
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -82,7 +82,7 @@ await docs.add({ id, text, user: "tenant-7:u_42", seq: 1759698843000 }); // a us
|
|
|
82
82
|
// than stored is "stale" and writes nothing
|
|
83
83
|
await docs.query("refunds", { userPrefix: "tenant-7:" }); // one user's documents (user) or a group's (userPrefix)
|
|
84
84
|
await docs.get("refunds"); // { id, text, metadata, updatedAt, version, user?, seq? } or null
|
|
85
|
-
for await (const doc of docs.list()) {} // every document, page by page ({ limit } per page: 1-100, default 50)
|
|
85
|
+
for await (const doc of docs.list()) {} // every document, page by page ({ limit } per page: 1-100, default 50; { order: "newest" } newest first)
|
|
86
86
|
await docs.delete("refunds"); // 404 document_not_found if it does not exist
|
|
87
87
|
await docs.deleteMany(["a", "b"]); // up to 1000 ids in one call; missing ids are skipped. Returns the count
|
|
88
88
|
await docs.deleteWhere({ team: "support" }); // every document matching the filter, whole documents. Returns the count
|
|
@@ -161,6 +161,11 @@ order. `docs.create({ regexIndex: true })` before a namespace's first write make
|
|
|
161
161
|
await conare.embed(["some text"], { type: "query" }); // number[][]; type defaults to "document"
|
|
162
162
|
await conare.embed(["some text"], { outputDtype: "int8" }); // { values: Int8Array[], scales }; dequantize() gives floats
|
|
163
163
|
await conare.rerank("refund window", ["a", "b"], { topK: 1 }); // [{ index, score, document }], plus .unscored
|
|
164
|
+
|
|
165
|
+
// Both parts of the model: a vector per text and a vector per token. Blend the two scores with hybridScore().
|
|
166
|
+
const query = await conare.embed("refund window", { type: "query", tokenEmbeddings: true });
|
|
167
|
+
const documents = await conare.embed(["a", "b"], { tokenEmbeddings: true }); // up to 16 texts a call
|
|
168
|
+
hybridScore(query, documents, 0, 1); // the query against documents[1]
|
|
164
169
|
```
|
|
165
170
|
|
|
166
171
|
`outputDtype: "int8"` returns each vector as int8 values plus one scale (vector ≈ values × scale, scale = max|x| / 127):
|
|
@@ -217,7 +222,31 @@ picked from what each sync reads, or on one you set from 5 minutes to 30 days. `
|
|
|
217
222
|
what an estimate of a month at any interval needs: the rows the last sync read, whether syncs read
|
|
218
223
|
only changes, and the prices.
|
|
219
224
|
|
|
220
|
-
|
|
225
|
+
Pages of public websites are a source too:
|
|
226
|
+
|
|
227
|
+
```ts
|
|
228
|
+
const site = await conare.sources.create({
|
|
229
|
+
type: "web",
|
|
230
|
+
namespace: "admissions",
|
|
231
|
+
urls: ["https://www.example.edu/admissions/dates", "https://www.example.edu/admissions/guide.pdf"],
|
|
232
|
+
});
|
|
233
|
+
const { data } = await conare.sources.failures(site.id); // [{ externalId, code: "refused", message, attempts }]
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
Or give a website's address and Conare finds its pages itself, new ones included, at every sync:
|
|
237
|
+
|
|
238
|
+
```ts
|
|
239
|
+
const rival = await conare.sources.create({ type: "web", namespace: "competitors", site: "https://www.example.com" });
|
|
240
|
+
await conare.sources.update<WebSource>(rival.id, { syncIntervalSeconds: 3600 }); // an hour to a week; null is a day
|
|
241
|
+
await conare.namespace("competitors").query("enterprise plan price", { filter: { page_type: "pricing" } });
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
Conare reads each page as the crawler `ConareBot` (robots.txt and a site's Crawl-delay obeyed, a site asked
|
|
245
|
+
only as fast as it answers), keeps it in the namespace with its address as `source_url` and its kind as
|
|
246
|
+
`page_type` (`pricing`, `blog`, `changelog`, `docs`, ...), and reads it again every `syncIntervalSeconds` (a day
|
|
247
|
+
by default), parsing it again only when it changed. Each website is its own source with its own interval.
|
|
248
|
+
|
|
249
|
+
`sources.sync()` answers a connected app's `Source`. A Google Cloud Storage or web source answers the sync
|
|
221
250
|
`Run` it queued (409 `run_active` while one is queued or running), and a custom or files source has
|
|
222
251
|
nothing to sync (409 `push_source`).
|
|
223
252
|
|
|
@@ -255,8 +284,8 @@ await conare.events.list({ cursor: "0", types: ["run.*"] }); // { data: [{ seq,
|
|
|
255
284
|
```
|
|
256
285
|
|
|
257
286
|
`sources.list()` and `sources.get()` answer custom sources too, with `kind: "custom"`, and the
|
|
258
|
-
files source of each namespace you upload to (`kind: "files"`)
|
|
259
|
-
(`kind: "gcs"`); a connected app has no `kind`. A custom source that holds an app keeps
|
|
287
|
+
files source of each namespace you upload to (`kind: "files"`), Google Cloud Storage buckets
|
|
288
|
+
(`kind: "gcs"`) and web sources (`kind: "web"`); a connected app has no `kind`. A custom source that holds an app keeps
|
|
260
289
|
`type: "custom"` and names the app in `app` (`"gmail"`; null otherwise). These carry `health` (`ok`, `degraded`, `failing`),
|
|
261
290
|
`display.state` (`needs_action` with `display.action` when you must act, such as `grant_access` on a
|
|
262
291
|
bucket) and `sync.lastError`. These calls send an `Idempotency-Key` when they create or delete
|
|
@@ -355,6 +384,11 @@ lowercased, with any character other than letters, digits, `_` and `-` as `-` (`
|
|
|
355
384
|
writes `next-js-github`). A namespace that already holds another owner's repository of the same
|
|
356
385
|
name is left alone, and that repository's sync stops with an error.
|
|
357
386
|
|
|
387
|
+
## Changes in 0.30.0
|
|
388
|
+
|
|
389
|
+
- `embed(..., { tokenEmbeddings: true })` returns `HybridEmbeddings`: each text's vector and a vector
|
|
390
|
+
per token, with `denseWeight`; `hybridScore(query, documents, q, d)` blends the two scores.
|
|
391
|
+
|
|
358
392
|
## Changes in 0.29.0
|
|
359
393
|
|
|
360
394
|
- `sources.sync()` returns `Source | Run`: a Google Cloud Storage source's sync answers its `Run`
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export declare const VERSION = "0.
|
|
1
|
+
export declare const VERSION = "0.30.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>;
|
|
@@ -104,7 +104,7 @@ export interface WriteResult {
|
|
|
104
104
|
imageEmbedded?: boolean;
|
|
105
105
|
}
|
|
106
106
|
/** A file type `uploadFile` can declare: the file is read as that type (`jpg` and `tif` are accepted too). */
|
|
107
|
-
export type FileType = "pdf" | "xlsx" | "docx" | "pptx" | "csv" | "tsv" | "txt" | "md" | "png" | "jpeg" | "webp" | "tiff" | "gif" | "bmp" | "heic";
|
|
107
|
+
export type FileType = "pdf" | "xlsx" | "docx" | "pptx" | "csv" | "tsv" | "txt" | "md" | "html" | "png" | "jpeg" | "webp" | "tiff" | "gif" | "bmp" | "heic";
|
|
108
108
|
/**
|
|
109
109
|
* A picture of a Word document or deck that OCR did not turn into text. Every raster picture is read except an
|
|
110
110
|
* icon or a decoration: under 64 pixels on both sides, under 16 pixels on its shorter side, or drawn under half
|
|
@@ -400,6 +400,26 @@ export interface Int8Embeddings {
|
|
|
400
400
|
values: Int8Array[];
|
|
401
401
|
scales: number[];
|
|
402
402
|
}
|
|
403
|
+
/**
|
|
404
|
+
* Both parts of the embedding model (`embed(..., { tokenEmbeddings: true })`): each text's vector and, beside
|
|
405
|
+
* it, a vector per token. Rank a document for a query with `hybridScore`, which blends the two scores by
|
|
406
|
+
* `denseWeight`; either part also ranks alone.
|
|
407
|
+
*/
|
|
408
|
+
export interface HybridEmbeddings {
|
|
409
|
+
/** One vector per text, unit length. */
|
|
410
|
+
embeddings: number[][];
|
|
411
|
+
/** Per text, its token embeddings: rows of `tokenDimensions` floats. */
|
|
412
|
+
tokenEmbeddings: number[][][];
|
|
413
|
+
tokenDimensions: number;
|
|
414
|
+
/** The share of the `embeddings` score in the blended score, between 0 and 1. */
|
|
415
|
+
denseWeight: number;
|
|
416
|
+
}
|
|
417
|
+
/**
|
|
418
|
+
* Text `d` of `documents` scored for text `q` of `query` (one embedded with type "query", the other with
|
|
419
|
+
* "document", both with `tokenEmbeddings: true`): denseWeight × dot(the two vectors) + (1 − denseWeight) ×
|
|
420
|
+
* the mean, over the query's token embeddings, of each one's best dot product among the document's.
|
|
421
|
+
*/
|
|
422
|
+
export declare function hybridScore(query: HybridEmbeddings, documents: HybridEmbeddings, q?: number, d?: number): number;
|
|
403
423
|
/** The float vectors int8 embeddings stand for: values × scale. */
|
|
404
424
|
export declare function dequantize(embeddings: Int8Embeddings): number[][];
|
|
405
425
|
export type SourceStatus = "connecting" | "syncing" | "ready" | "error";
|
|
@@ -515,6 +535,51 @@ export interface GCSSource extends Omit<CustomSource, "kind" | "type" | "app" |
|
|
|
515
535
|
/** Objects it holds no current document for. */
|
|
516
536
|
failures: number;
|
|
517
537
|
}
|
|
538
|
+
/**
|
|
539
|
+
* Pages of public websites Conare keeps in step with their sites: each is read again every
|
|
540
|
+
* `config.syncIntervalSeconds`, and parsed again only when it changed. `failures` counts the pages it could not
|
|
541
|
+
* read (the site refuses crawlers, wants a sign-in, or the page needs a browser to show its text).
|
|
542
|
+
*/
|
|
543
|
+
/** A page of a web source: its address, or its address with your fields, which every document of the page carries. */
|
|
544
|
+
export type WebPage = string | {
|
|
545
|
+
url: string;
|
|
546
|
+
metadata?: Record<string, string | number | boolean | string[]>;
|
|
547
|
+
};
|
|
548
|
+
export interface WebSource extends Omit<CustomSource, "kind" | "type" | "app" | "user"> {
|
|
549
|
+
kind: "web";
|
|
550
|
+
type: "web";
|
|
551
|
+
app: null;
|
|
552
|
+
user: null;
|
|
553
|
+
/**
|
|
554
|
+
* `urls`: the source's pages. For a source of a `site`, the pages its last sync found there, of at most `maxPages`.
|
|
555
|
+
*/
|
|
556
|
+
config: {
|
|
557
|
+
urls: string[];
|
|
558
|
+
syncIntervalSeconds: number;
|
|
559
|
+
site?: string;
|
|
560
|
+
maxPages?: number;
|
|
561
|
+
};
|
|
562
|
+
/** Pages it could not read or holds no current document for. */
|
|
563
|
+
failures: number;
|
|
564
|
+
}
|
|
565
|
+
/** One object of a Google Cloud Storage source, or one page of a web source, it could not keep, whole or in part. */
|
|
566
|
+
export interface SourceFailure {
|
|
567
|
+
/** The object (`gs://...`) or the page's address. */
|
|
568
|
+
externalId: string;
|
|
569
|
+
key: string;
|
|
570
|
+
/** The version that failed; empty for a page that was not read. */
|
|
571
|
+
version: string;
|
|
572
|
+
/**
|
|
573
|
+
* Why: a parse's code (`unsupported_file`, `too_large`, `pages_unread`, ...), or for a page what its site did
|
|
574
|
+
* (`refused`, `slow_down`, `robots_disallowed`, `sign_in_required`, `gone`, `unreachable`, `needs_javascript`).
|
|
575
|
+
*/
|
|
576
|
+
code: string;
|
|
577
|
+
message: string;
|
|
578
|
+
runId: string | null;
|
|
579
|
+
/** How many times in a row it failed this way. */
|
|
580
|
+
attempts: number;
|
|
581
|
+
failedAt: Date;
|
|
582
|
+
}
|
|
518
583
|
/** A document of a custom source to push. */
|
|
519
584
|
export interface CustomDocumentInput {
|
|
520
585
|
/** Your id for the document; its `key` is derived from it. */
|
|
@@ -804,14 +869,21 @@ export declare class Conare {
|
|
|
804
869
|
/**
|
|
805
870
|
* Embed texts. Use type "query" for search queries and "document" (default) for stored text.
|
|
806
871
|
* `outputDtype: "int8"` returns each vector as int8 values and one scale (vector ≈ values × scale),
|
|
807
|
-
* a quarter of the bytes; `dequantize` turns them back into floats.
|
|
872
|
+
* a quarter of the bytes; `dequantize` turns them back into floats. `tokenEmbeddings: true` returns both
|
|
873
|
+
* parts of the model, for up to 16 texts a call (`HybridEmbeddings`, scored with `hybridScore`).
|
|
808
874
|
*/
|
|
809
875
|
embed(input: string | string[], options?: RequestOptions & EmbedOptions & {
|
|
810
876
|
outputDtype?: "float";
|
|
877
|
+
tokenEmbeddings?: false;
|
|
811
878
|
}): Promise<number[][]>;
|
|
812
879
|
embed(input: string | string[], options: RequestOptions & EmbedOptions & {
|
|
813
880
|
outputDtype: "int8";
|
|
881
|
+
tokenEmbeddings?: false;
|
|
814
882
|
}): Promise<Int8Embeddings>;
|
|
883
|
+
embed(input: string | string[], options: RequestOptions & EmbedOptions & {
|
|
884
|
+
outputDtype?: "float";
|
|
885
|
+
tokenEmbeddings: true;
|
|
886
|
+
}): Promise<HybridEmbeddings>;
|
|
815
887
|
/**
|
|
816
888
|
* Rerank 1-100 documents against a query, best first. Each document is 1-24,000 characters, the query at
|
|
817
889
|
* most 8,192, and all of them together at most 256,000; more than 100 documents is a 400 `invalid_request`.
|
|
@@ -929,9 +1001,15 @@ export declare class Namespace {
|
|
|
929
1001
|
}): AsyncGenerator<QueryPage>;
|
|
930
1002
|
/** One document by id, or null if it does not exist. */
|
|
931
1003
|
get(id: string, options?: RequestOptions): Promise<Document | null>;
|
|
932
|
-
/**
|
|
1004
|
+
/**
|
|
1005
|
+
* Every document (id, metadata, updatedAt), fetched page by page as you iterate. `limit` is the page size: 1-100, default 50.
|
|
1006
|
+
* `order` is `"id"` (the default) or `"newest"`: newest first by the document's date (its `date`, else its `created_at`),
|
|
1007
|
+
* else when it was last written. A namespace of more than 10,000 documents that holds documents written before
|
|
1008
|
+
* 2026-11-14 and not indexed by date since throws 400 `order_unavailable`: list it by `"id"`.
|
|
1009
|
+
*/
|
|
933
1010
|
list(options?: RequestOptions & {
|
|
934
1011
|
limit?: number;
|
|
1012
|
+
order?: "id" | "newest";
|
|
935
1013
|
}): AsyncGenerator<DocumentSummary>;
|
|
936
1014
|
/** Delete one document. Throws a ConareError with status 404 (`document_not_found`) if it does not exist. */
|
|
937
1015
|
delete(id: string, options?: RequestOptions): Promise<void>;
|
|
@@ -963,6 +1041,17 @@ export declare class Sources {
|
|
|
963
1041
|
/**
|
|
964
1042
|
* Create a custom source: a set of documents you push into `namespace`, keyed by your own ids.
|
|
965
1043
|
* With `user`, `users.delete(user)` deletes it with the user's documents.
|
|
1044
|
+
*
|
|
1045
|
+
* With `type: "web"`, create a web source instead: Conare reads the pages at `urls` (HTML pages, PDFs and the
|
|
1046
|
+
* other file types it parses), keeps them in `namespace` and reads each again every `syncIntervalSeconds`
|
|
1047
|
+
* (a day by default, an hour at least). `failures(id)` lists the pages it could not read.
|
|
1048
|
+
* A page is never followed: the source is the pages you name.
|
|
1049
|
+
*
|
|
1050
|
+
* With `site` in place of `urls`, the source is a whole website: every sync finds the site's pages itself (its
|
|
1051
|
+
* sitemaps, feeds and links, on the site and its `docs.`, `blog.` and like hosts; an address with a path keeps
|
|
1052
|
+
* to the pages under it), new ones included, and keeps at most `maxPages` (2,000 by default, 5,000 at most).
|
|
1053
|
+
* Each page's documents carry `page_type` (`pricing`, `blog`, `changelog`, `docs` ...) and, when the page states
|
|
1054
|
+
* one, a `published` date, so `filter` can keep a search to them.
|
|
966
1055
|
*/
|
|
967
1056
|
create(params: {
|
|
968
1057
|
type: "custom";
|
|
@@ -970,6 +1059,21 @@ export declare class Sources {
|
|
|
970
1059
|
name?: string;
|
|
971
1060
|
user?: string;
|
|
972
1061
|
}, options?: RequestOptions): Promise<CustomSource>;
|
|
1062
|
+
create(params: {
|
|
1063
|
+
type: "web";
|
|
1064
|
+
namespace: string;
|
|
1065
|
+
urls: WebPage[];
|
|
1066
|
+
name?: string;
|
|
1067
|
+
syncIntervalSeconds?: number;
|
|
1068
|
+
}, options?: RequestOptions): Promise<WebSource>;
|
|
1069
|
+
create(params: {
|
|
1070
|
+
type: "web";
|
|
1071
|
+
namespace: string;
|
|
1072
|
+
site: string;
|
|
1073
|
+
maxPages?: number;
|
|
1074
|
+
name?: string;
|
|
1075
|
+
syncIntervalSeconds?: number;
|
|
1076
|
+
}, options?: RequestOptions): Promise<WebSource>;
|
|
973
1077
|
/**
|
|
974
1078
|
* Start connecting an app. Send the app owner to `connectUrl`; they come back to `redirectUrl`.
|
|
975
1079
|
* For your own users, connect each one into their own namespace, e.g. "user-42".
|
|
@@ -991,23 +1095,28 @@ export declare class Sources {
|
|
|
991
1095
|
source: Source;
|
|
992
1096
|
connectUrl: string | null;
|
|
993
1097
|
}>;
|
|
994
|
-
/**
|
|
995
|
-
|
|
1098
|
+
/**
|
|
1099
|
+
* Set how often a source syncs. A connected app: seconds from 300 (5 minutes) to 2592000 (30 days), or null for
|
|
1100
|
+
* automatic. A web source (`update<WebSource>`): 3600 to 604800, or null for a day; each website is its own
|
|
1101
|
+
* source with its own interval. A Google Cloud Storage source (`update<GCSSource>`): 900 to 604800, or null for
|
|
1102
|
+
* an hour.
|
|
1103
|
+
*/
|
|
1104
|
+
update<T extends Source | GCSSource | WebSource = Source>(id: string, params: {
|
|
996
1105
|
syncIntervalSeconds: number | null;
|
|
997
|
-
}, options?: RequestOptions): Promise<
|
|
1106
|
+
}, options?: RequestOptions): Promise<T>;
|
|
998
1107
|
/**
|
|
999
1108
|
* Every source: the connected apps, then the custom, files and Google Cloud Storage sources, which carry
|
|
1000
1109
|
* their `kind` (`custom`, `files`, `gcs`); a connected app has none.
|
|
1001
1110
|
*/
|
|
1002
|
-
list(options?: RequestOptions): Promise<Array<Source | CustomSource | FilesSource | GCSSource>>;
|
|
1111
|
+
list(options?: RequestOptions): Promise<Array<Source | CustomSource | FilesSource | GCSSource | WebSource>>;
|
|
1003
1112
|
/** One source by id. */
|
|
1004
|
-
get(id: string, options?: RequestOptions): Promise<Source | CustomSource | FilesSource | GCSSource>;
|
|
1113
|
+
get(id: string, options?: RequestOptions): Promise<Source | CustomSource | FilesSource | GCSSource | WebSource>;
|
|
1005
1114
|
/**
|
|
1006
1115
|
* Start a sync now. `full: true` re-reads everything the app holds instead of what changed: documents
|
|
1007
1116
|
* that changed are rewritten and ones Conare no longer keeps are deleted. It is for Gmail and the apps
|
|
1008
1117
|
* Conare reads itself (other apps answer 400), and Gmail's rows are billed again (`cost.usdPerMillionRows`).
|
|
1009
1118
|
*
|
|
1010
|
-
* A connected app answers the `Source`, due to sync now. A Google Cloud Storage source answers the sync
|
|
1119
|
+
* A connected app answers the `Source`, due to sync now. A Google Cloud Storage or web source answers the sync
|
|
1011
1120
|
* `Run` it queued (409 `run_active` while one is queued or running). A custom or files source has nothing
|
|
1012
1121
|
* to sync: 409 `push_source`. A `Run` has `state` and a `Source` has `status`: `"state" in result` tells
|
|
1013
1122
|
* them apart.
|
|
@@ -1015,9 +1124,20 @@ export declare class Sources {
|
|
|
1015
1124
|
sync(id: string, options?: RequestOptions & {
|
|
1016
1125
|
full?: boolean;
|
|
1017
1126
|
}): Promise<Source | Run>;
|
|
1127
|
+
/**
|
|
1128
|
+
* What a Google Cloud Storage or web source could not keep, in key order: `nextCursor` is the next page's
|
|
1129
|
+
* `cursor`, null at the end.
|
|
1130
|
+
*/
|
|
1131
|
+
failures(id: string, params?: {
|
|
1132
|
+
cursor?: string;
|
|
1133
|
+
limit?: number;
|
|
1134
|
+
}, options?: RequestOptions): Promise<{
|
|
1135
|
+
data: SourceFailure[];
|
|
1136
|
+
nextCursor: string | null;
|
|
1137
|
+
}>;
|
|
1018
1138
|
/**
|
|
1019
1139
|
* Disconnect a source. Pass `deleteDocuments: true` to also delete what it synced; without it a
|
|
1020
|
-
* connected app's and a custom source's documents stay. A Google Cloud Storage
|
|
1140
|
+
* connected app's and a custom source's documents stay. A Google Cloud Storage, web or files
|
|
1021
1141
|
* source's documents always go with it. A custom source's delete is a run, which `runs.get` follows
|
|
1022
1142
|
* until the source is `deleted`.
|
|
1023
1143
|
*/
|
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.
|
|
2
|
+
export const VERSION = "0.30.0";
|
|
3
3
|
const DEFAULT_BASE_URL = "https://api.conare.ai";
|
|
4
4
|
const BATCH_SIZE = 100;
|
|
5
5
|
/** Above the API's 110 s deadline for a write: a call given up sooner is retried while the first still runs. */
|
|
@@ -66,6 +66,21 @@ function toFileUpload(body) {
|
|
|
66
66
|
ocrLayerOnly: body.ocr_layer_only ?? [],
|
|
67
67
|
};
|
|
68
68
|
}
|
|
69
|
+
/**
|
|
70
|
+
* Text `d` of `documents` scored for text `q` of `query` (one embedded with type "query", the other with
|
|
71
|
+
* "document", both with `tokenEmbeddings: true`): denseWeight × dot(the two vectors) + (1 − denseWeight) ×
|
|
72
|
+
* the mean, over the query's token embeddings, of each one's best dot product among the document's.
|
|
73
|
+
*/
|
|
74
|
+
export function hybridScore(query, documents, q = 0, d = 0) {
|
|
75
|
+
const dot = (a, b) => a.reduce((sum, x, i) => sum + x * (b[i] ?? 0), 0);
|
|
76
|
+
const queryRows = query.tokenEmbeddings[q] ?? [];
|
|
77
|
+
const documentRows = documents.tokenEmbeddings[d] ?? [];
|
|
78
|
+
const tokens = queryRows.reduce((sum, row) => sum + documentRows.reduce((best, other) => Math.max(best, dot(row, other)), -Infinity), 0);
|
|
79
|
+
const dense = dot(query.embeddings[q] ?? [], documents.embeddings[d] ?? []);
|
|
80
|
+
if (queryRows.length === 0 || documentRows.length === 0)
|
|
81
|
+
return query.denseWeight * dense;
|
|
82
|
+
return query.denseWeight * dense + (1 - query.denseWeight) * (tokens / queryRows.length);
|
|
83
|
+
}
|
|
69
84
|
/** The float vectors int8 embeddings stand for: values × scale. */
|
|
70
85
|
export function dequantize(embeddings) {
|
|
71
86
|
return embeddings.values.map((v, i) => Array.from(v, (x) => x * (embeddings.scales[i] ?? 0)));
|
|
@@ -316,7 +331,17 @@ export class Conare {
|
|
|
316
331
|
const request = { input: Array.isArray(input) ? input : [input], type: options.type ?? "document" };
|
|
317
332
|
if (options.outputDtype)
|
|
318
333
|
request.output_dtype = options.outputDtype;
|
|
334
|
+
if (options.tokenEmbeddings)
|
|
335
|
+
request.token_embeddings = true;
|
|
319
336
|
const body = await this.request("POST", "/embed", request, options);
|
|
337
|
+
if (options.tokenEmbeddings) {
|
|
338
|
+
return {
|
|
339
|
+
embeddings: body.embeddings,
|
|
340
|
+
tokenEmbeddings: body.token_embeddings ?? [],
|
|
341
|
+
tokenDimensions: body.token_dimensions ?? 0,
|
|
342
|
+
denseWeight: body.dense_weight ?? 0,
|
|
343
|
+
};
|
|
344
|
+
}
|
|
320
345
|
if (options.outputDtype === "int8") {
|
|
321
346
|
return { values: body.embeddings.map((v) => Int8Array.from(v)), scales: body.scales ?? [] };
|
|
322
347
|
}
|
|
@@ -624,13 +649,20 @@ export class Namespace {
|
|
|
624
649
|
throw err;
|
|
625
650
|
}
|
|
626
651
|
}
|
|
627
|
-
/**
|
|
652
|
+
/**
|
|
653
|
+
* Every document (id, metadata, updatedAt), fetched page by page as you iterate. `limit` is the page size: 1-100, default 50.
|
|
654
|
+
* `order` is `"id"` (the default) or `"newest"`: newest first by the document's date (its `date`, else its `created_at`),
|
|
655
|
+
* else when it was last written. A namespace of more than 10,000 documents that holds documents written before
|
|
656
|
+
* 2026-11-14 and not indexed by date since throws 400 `order_unavailable`: list it by `"id"`.
|
|
657
|
+
*/
|
|
628
658
|
async *list(options = {}) {
|
|
629
659
|
let cursor = null;
|
|
630
660
|
do {
|
|
631
661
|
const params = new URLSearchParams();
|
|
632
662
|
if (options.limit)
|
|
633
663
|
params.set("limit", String(options.limit));
|
|
664
|
+
if (options.order)
|
|
665
|
+
params.set("order", options.order);
|
|
634
666
|
if (cursor)
|
|
635
667
|
params.set("cursor", cursor);
|
|
636
668
|
const qs = params.toString();
|
|
@@ -687,11 +719,12 @@ export class Sources {
|
|
|
687
719
|
this.client = client;
|
|
688
720
|
this.documents = new SourceDocuments(client);
|
|
689
721
|
}
|
|
690
|
-
/**
|
|
691
|
-
* Create a custom source: a set of documents you push into `namespace`, keyed by your own ids.
|
|
692
|
-
* With `user`, `users.delete(user)` deletes it with the user's documents.
|
|
693
|
-
*/
|
|
694
722
|
async create(params, options) {
|
|
723
|
+
if (params.type === "web") {
|
|
724
|
+
const pages = "site" in params ? { site: params.site, max_pages: params.maxPages } : { urls: params.urls };
|
|
725
|
+
const body = await this.client.request("POST", "/sources", { type: "web", namespace: params.namespace, ...pages, name: params.name, sync_interval_s: params.syncIntervalSeconds }, options);
|
|
726
|
+
return toAnySource(body);
|
|
727
|
+
}
|
|
695
728
|
const body = await this.client.request("POST", "/sources", { type: params.type, namespace: params.namespace, name: params.name, user: params.user }, options);
|
|
696
729
|
return toCustomSource(body);
|
|
697
730
|
}
|
|
@@ -713,9 +746,14 @@ export class Sources {
|
|
|
713
746
|
}, options);
|
|
714
747
|
return { source: toSource(body.source), connectUrl: body.connect_url ?? null };
|
|
715
748
|
}
|
|
716
|
-
/**
|
|
749
|
+
/**
|
|
750
|
+
* Set how often a source syncs. A connected app: seconds from 300 (5 minutes) to 2592000 (30 days), or null for
|
|
751
|
+
* automatic. A web source (`update<WebSource>`): 3600 to 604800, or null for a day; each website is its own
|
|
752
|
+
* source with its own interval. A Google Cloud Storage source (`update<GCSSource>`): 900 to 604800, or null for
|
|
753
|
+
* an hour.
|
|
754
|
+
*/
|
|
717
755
|
async update(id, params, options) {
|
|
718
|
-
return
|
|
756
|
+
return toAnySource(await this.client.request("PATCH", `/sources/${segment(id)}`, { sync_interval_s: params.syncIntervalSeconds }, options));
|
|
719
757
|
}
|
|
720
758
|
/**
|
|
721
759
|
* Every source: the connected apps, then the custom, files and Google Cloud Storage sources, which carry
|
|
@@ -734,7 +772,7 @@ export class Sources {
|
|
|
734
772
|
* that changed are rewritten and ones Conare no longer keeps are deleted. It is for Gmail and the apps
|
|
735
773
|
* Conare reads itself (other apps answer 400), and Gmail's rows are billed again (`cost.usdPerMillionRows`).
|
|
736
774
|
*
|
|
737
|
-
* A connected app answers the `Source`, due to sync now. A Google Cloud Storage source answers the sync
|
|
775
|
+
* A connected app answers the `Source`, due to sync now. A Google Cloud Storage or web source answers the sync
|
|
738
776
|
* `Run` it queued (409 `run_active` while one is queued or running). A custom or files source has nothing
|
|
739
777
|
* to sync: 409 `push_source`. A `Run` has `state` and a `Source` has `status`: `"state" in result` tells
|
|
740
778
|
* them apart.
|
|
@@ -744,9 +782,34 @@ export class Sources {
|
|
|
744
782
|
const body = await this.client.request("POST", `/sources/${segment(id)}/sync`, full ? { full: true } : undefined, request);
|
|
745
783
|
return "run" in body ? toRun(body.run) : toSource(body);
|
|
746
784
|
}
|
|
785
|
+
/**
|
|
786
|
+
* What a Google Cloud Storage or web source could not keep, in key order: `nextCursor` is the next page's
|
|
787
|
+
* `cursor`, null at the end.
|
|
788
|
+
*/
|
|
789
|
+
async failures(id, params = {}, options) {
|
|
790
|
+
const query = [];
|
|
791
|
+
if (params.cursor)
|
|
792
|
+
query.push(`cursor=${encodeURIComponent(params.cursor)}`);
|
|
793
|
+
if (params.limit !== undefined)
|
|
794
|
+
query.push(`limit=${params.limit}`);
|
|
795
|
+
const body = await this.client.request("GET", `/sources/${segment(id)}/failures${query.length ? `?${query.join("&")}` : ""}`, undefined, options);
|
|
796
|
+
return {
|
|
797
|
+
data: body.data.map((f) => ({
|
|
798
|
+
externalId: f.external_id,
|
|
799
|
+
key: f.key,
|
|
800
|
+
version: f.version,
|
|
801
|
+
code: f.code,
|
|
802
|
+
message: f.message,
|
|
803
|
+
runId: f.run_id,
|
|
804
|
+
attempts: f.attempts,
|
|
805
|
+
failedAt: new Date(f.failed_at),
|
|
806
|
+
})),
|
|
807
|
+
nextCursor: body.next_cursor,
|
|
808
|
+
};
|
|
809
|
+
}
|
|
747
810
|
/**
|
|
748
811
|
* Disconnect a source. Pass `deleteDocuments: true` to also delete what it synced; without it a
|
|
749
|
-
* connected app's and a custom source's documents stay. A Google Cloud Storage
|
|
812
|
+
* connected app's and a custom source's documents stay. A Google Cloud Storage, web or files
|
|
750
813
|
* source's documents always go with it. A custom source's delete is a run, which `runs.get` follows
|
|
751
814
|
* until the source is `deleted`.
|
|
752
815
|
*/
|
|
@@ -991,10 +1054,25 @@ export class Events {
|
|
|
991
1054
|
};
|
|
992
1055
|
}
|
|
993
1056
|
}
|
|
994
|
-
/** A source by its `kind`: a connected app has none; a custom, files
|
|
1057
|
+
/** A source by its `kind`: a connected app has none; a custom, files, Google Cloud Storage or web source names it. */
|
|
995
1058
|
function toAnySource(s) {
|
|
996
1059
|
if (!("kind" in s))
|
|
997
1060
|
return toSource(s);
|
|
1061
|
+
if (s.kind === "web") {
|
|
1062
|
+
return {
|
|
1063
|
+
...toCustomSource(s),
|
|
1064
|
+
kind: "web",
|
|
1065
|
+
type: "web",
|
|
1066
|
+
app: null,
|
|
1067
|
+
user: null,
|
|
1068
|
+
config: {
|
|
1069
|
+
urls: s.config?.urls ?? [],
|
|
1070
|
+
syncIntervalSeconds: s.config?.sync_interval_s ?? 0,
|
|
1071
|
+
...(s.config?.site ? { site: s.config.site, maxPages: s.config.max_pages } : {}),
|
|
1072
|
+
},
|
|
1073
|
+
failures: s.failures ?? 0,
|
|
1074
|
+
};
|
|
1075
|
+
}
|
|
998
1076
|
if (s.kind === "gcs") {
|
|
999
1077
|
const c = s.config;
|
|
1000
1078
|
return {
|