@wirecat/cli-core 0.648.0 → 0.649.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/CHANGELOG.md +32 -0
- package/dist/background/daily-archive-timer.d.ts +1 -1
- package/dist/domain/providers.d.ts +4 -1
- package/dist/domain/providers.js +9 -2
- package/dist/domain/record-types.d.ts +24 -3
- package/dist/domain/record-types.js +23 -2
- package/dist/domain/records-table.js +4 -3
- package/dist/domain/records.d.ts +1 -1
- package/dist/domain/records.js +3 -2
- package/dist/services/document-source.js +9 -1
- package/dist/services/notes-search.d.ts +10 -5
- package/dist/services/notes-search.js +19 -7
- package/dist/services/search-all.js +18 -10
- package/dist/store/sqlite/added-sources.d.ts +38 -0
- package/dist/store/sqlite/added-sources.js +80 -50
- package/dist/store/sqlite/record-projectors-document.d.ts +12 -0
- package/dist/store/sqlite/record-projectors-document.js +81 -0
- package/dist/store/sqlite/record-projectors.js +2 -0
- package/package.json +1 -1
- package/schemas/records-envelope.v1.schema.json +57 -2
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,38 @@ Notable changes to `@wirecat/cli-core` (`@wirecat/cli-messaging` up to 0.261.0,
|
|
|
5
5
|
minor version may break callers, and says how under "Changed — may break callers". `pnpm docs:check` checks the
|
|
6
6
|
shape of this file.
|
|
7
7
|
|
|
8
|
+
## 0.649.0 — 13.11.2026
|
|
9
|
+
|
|
10
|
+
### Added
|
|
11
|
+
|
|
12
|
+
- **A records envelope carries text search finds: the record type `document`.** Fields `text`, `title`, `created_at`,
|
|
13
|
+
`updated_at`, `url` and `metadata`; relations `about` people, organizations, deals and events (a call, or a meeting's calendar event). It
|
|
14
|
+
becomes a document of the envelope's own account, keyed by the record's external id: the same record again is
|
|
15
|
+
`unchanged`, changed text is the same document's next revision, and a `delete` empties it. Like an SMS or a call, it
|
|
16
|
+
is taken only under an import-only provider. A document's `fields` may take 1 MiB; every other type keeps 64 KiB.
|
|
17
|
+
- **The seeded import-only provider `import`** holds what any other source brings in: `import:crm-notes`,
|
|
18
|
+
`import:obsidian-export`. It holds documents, SMS and calls.
|
|
19
|
+
- **Search reads the documents of import-only accounts under their own name.** An import-only provider (`phone`,
|
|
20
|
+
`import`) holds `documents` beside its other kinds: notes search, `search all` and meaning search read them,
|
|
21
|
+
`--source phone:office-pbx` reads only that account's, and a hit names `phone:office-pbx` with kind `document`.
|
|
22
|
+
The account's SMS and calls stay listed under the same name.
|
|
23
|
+
|
|
24
|
+
### Changed — may break callers
|
|
25
|
+
|
|
26
|
+
- **An import-only provider holds `documents` beside its own kinds.** `kindsIn("phone")` is now
|
|
27
|
+
`["messages", "events", "documents"]`, and `holdsKind(provider, "documents")` is true for `phone` and `import`. So
|
|
28
|
+
`--source documents` reaches their accounts, and `search notes --source phone:<account>` is read where it was refused.
|
|
29
|
+
- **`searchNotesOf(...).accounts` names an import's account by its own key** (`{ provider: "phone", account:
|
|
30
|
+
"office-pbx" }`): its `provider` is a `string`, no longer only `"notes"`. Every import-only account is in the list
|
|
31
|
+
of what an unscoped notes search read, one that holds only SMS and calls included.
|
|
32
|
+
- **`TABLE_RECORD_TYPES` holds `document`**, so a CSV sheet and `records templates` offer it. SMS and calls still
|
|
33
|
+
have no sheet.
|
|
34
|
+
|
|
35
|
+
### Fixed
|
|
36
|
+
|
|
37
|
+
- `searchNotesOf` hands its `env` to the search, so meaning search finds the models of the caller's state folder
|
|
38
|
+
instead of the process's.
|
|
39
|
+
|
|
8
40
|
## 0.648.0 — 13.11.2026
|
|
9
41
|
|
|
10
42
|
### Added
|
|
@@ -24,12 +24,12 @@ export interface DailyArchiveTimerInput {
|
|
|
24
24
|
}
|
|
25
25
|
export declare const dailyArchiveTimer: (input: DailyArchiveTimerInput) => DailyArchiveTimer;
|
|
26
26
|
export declare const manageDailyArchiveTimer: (plan: DailyArchiveTimer, action: "install" | "start" | "stop" | "status" | "uninstall", system: Pick<ServerSystem, "platform" | "run">, env: NodeJS.ProcessEnv, signal?: AbortSignal) => Promise<{
|
|
27
|
-
action?: undefined;
|
|
28
27
|
name: string;
|
|
29
28
|
installed: boolean;
|
|
30
29
|
active: boolean;
|
|
31
30
|
servicePath: string;
|
|
32
31
|
timerPath: string;
|
|
32
|
+
action?: undefined;
|
|
33
33
|
} | {
|
|
34
34
|
active?: undefined;
|
|
35
35
|
name: string;
|
|
@@ -89,7 +89,10 @@ export declare const isKind: (word: string) => word is Kind;
|
|
|
89
89
|
export declare const providerNamed: (word: string) => ProviderEntry | undefined;
|
|
90
90
|
/** The kind of a provider the registry knows; `undefined` for one no entry names, which a search skips. */
|
|
91
91
|
export declare const kindIn: (provider: Provider) => Kind | undefined;
|
|
92
|
-
/**
|
|
92
|
+
/**
|
|
93
|
+
* Every kind a provider's accounts hold, its `kind` first; empty for one no entry names. An import-only provider's
|
|
94
|
+
* accounts hold documents too: a records import writes them beside its calls and SMS.
|
|
95
|
+
*/
|
|
93
96
|
export declare const kindsIn: (provider: Provider) => readonly Kind[];
|
|
94
97
|
/**
|
|
95
98
|
* Whether an entry may carry `imported`, beside one stored before (`held`): never for a built-in a sync writes, only
|
package/dist/domain/providers.js
CHANGED
|
@@ -21,6 +21,8 @@ export const PROVIDERS = [
|
|
|
21
21
|
{ name: "zoom", kind: "meetings", aliases: [] },
|
|
22
22
|
// A phone export's records: its SMS and its calls in the one account the import names.
|
|
23
23
|
{ name: "phone", kind: "messages", kinds: ["messages", "events"], aliases: [], imported: true },
|
|
24
|
+
// Any other import: a folder of notes, a CRM's export, a script's records, under `import:<what it is>`.
|
|
25
|
+
{ name: "import", kind: "documents", kinds: ["documents", "messages", "events"], aliases: [], imported: true },
|
|
24
26
|
];
|
|
25
27
|
const LABELS = {
|
|
26
28
|
telegram: "Telegram",
|
|
@@ -188,12 +190,17 @@ export const providerNamed = (word) => {
|
|
|
188
190
|
};
|
|
189
191
|
/** The kind of a provider the registry knows; `undefined` for one no entry names, which a search skips. */
|
|
190
192
|
export const kindIn = (provider) => isFolderProvider(provider) ? "notes" : registry.find(({ name }) => name === provider)?.kind;
|
|
191
|
-
/**
|
|
193
|
+
/**
|
|
194
|
+
* Every kind a provider's accounts hold, its `kind` first; empty for one no entry names. An import-only provider's
|
|
195
|
+
* accounts hold documents too: a records import writes them beside its calls and SMS.
|
|
196
|
+
*/
|
|
192
197
|
export const kindsIn = (provider) => {
|
|
193
198
|
if (isFolderProvider(provider))
|
|
194
199
|
return ["notes"];
|
|
195
200
|
const entry = registry.find(({ name }) => name === provider);
|
|
196
|
-
|
|
201
|
+
if (!entry)
|
|
202
|
+
return [];
|
|
203
|
+
return entry.imported ? unitedKinds(kindsOfEntry(entry), ["documents"]) : kindsOfEntry(entry);
|
|
197
204
|
};
|
|
198
205
|
/**
|
|
199
206
|
* Whether an entry may carry `imported`, beside one stored before (`held`): never for a built-in a sync writes, only
|
|
@@ -56,6 +56,10 @@ export interface RecordTypeSpec {
|
|
|
56
56
|
* never land in a messenger's chat or a calendar's account.
|
|
57
57
|
*/
|
|
58
58
|
importOnly?: boolean;
|
|
59
|
+
/** A phone's own export carries it, never a hand-made sheet: no template and no CSV. */
|
|
60
|
+
sheet?: false;
|
|
61
|
+
/** How many bytes its `fields` may take as JSON, when more than every type's 64 KiB: a document's text. */
|
|
62
|
+
fieldsBytes?: number;
|
|
59
63
|
/** The type's `fields`: known keys typed, any other key kept as sent. */
|
|
60
64
|
fields: FieldSchema;
|
|
61
65
|
/** The roles the entity is made from; any other role word is kept as the source's own. */
|
|
@@ -440,6 +444,7 @@ export declare const RECORD_TYPES: {
|
|
|
440
444
|
readonly update: "in-place";
|
|
441
445
|
readonly tombstone: true;
|
|
442
446
|
readonly importOnly: true;
|
|
447
|
+
readonly sheet: false;
|
|
443
448
|
readonly fields: FieldSchema;
|
|
444
449
|
readonly roles: {
|
|
445
450
|
readonly counterpart: {
|
|
@@ -454,6 +459,7 @@ export declare const RECORD_TYPES: {
|
|
|
454
459
|
readonly update: "in-place";
|
|
455
460
|
readonly tombstone: true;
|
|
456
461
|
readonly importOnly: true;
|
|
462
|
+
readonly sheet: false;
|
|
457
463
|
readonly fields: FieldSchema;
|
|
458
464
|
readonly roles: {
|
|
459
465
|
readonly counterpart: {
|
|
@@ -462,6 +468,21 @@ export declare const RECORD_TYPES: {
|
|
|
462
468
|
};
|
|
463
469
|
};
|
|
464
470
|
};
|
|
471
|
+
readonly document: {
|
|
472
|
+
readonly table: "documents";
|
|
473
|
+
readonly review: "direct";
|
|
474
|
+
readonly update: "in-place";
|
|
475
|
+
readonly tombstone: true;
|
|
476
|
+
readonly importOnly: true;
|
|
477
|
+
readonly fieldsBytes: 1048576;
|
|
478
|
+
readonly fields: FieldSchema;
|
|
479
|
+
readonly roles: {
|
|
480
|
+
readonly about: {
|
|
481
|
+
readonly targets: readonly ["person", "organization", "deal", "event"];
|
|
482
|
+
readonly many: true;
|
|
483
|
+
};
|
|
484
|
+
};
|
|
485
|
+
};
|
|
465
486
|
readonly pipeline: {
|
|
466
487
|
readonly table: "pipelines";
|
|
467
488
|
readonly review: "direct";
|
|
@@ -487,8 +508,8 @@ type Identity<T> = {
|
|
|
487
508
|
} ? K : never;
|
|
488
509
|
}[keyof T];
|
|
489
510
|
export type IdentityType = Identity<typeof RECORD_TYPES>;
|
|
490
|
-
export type BusinessType = Exclude<RecordType, IdentityType>;
|
|
491
|
-
/** A business record (an invoice, a deal, a wallet): not a person, organization or
|
|
511
|
+
export type BusinessType = Exclude<RecordType, IdentityType | "document">;
|
|
512
|
+
/** A business record (an invoice, a deal, a wallet): not a person, organization, project or document. */
|
|
492
513
|
export declare const isBusinessType: (type: string) => type is BusinessType;
|
|
493
514
|
export declare const isIdentityType: (type: unknown) => type is IdentityType;
|
|
494
515
|
export declare const RECORD_ENTITY_TYPES: RecordType[];
|
|
@@ -528,7 +549,7 @@ export declare const AMOUNT_ROLES: string[];
|
|
|
528
549
|
export declare const FIELD_REFERENCE_ROLES: string[];
|
|
529
550
|
/** The types whose records v1 projected: their v1 roles keep v1's reading. */
|
|
530
551
|
export declare const hasV1Roles: (type: RecordType) => boolean;
|
|
531
|
-
export declare const IDENTITY_TYPES: ("contract" | "deal" | "event" | "invoice" | "lead" | "message" | "order" | "organization" | "payment" | "person" | "pipeline" | "product" | "project" | "subscription" | "transaction" | "wallet")[];
|
|
552
|
+
export declare const IDENTITY_TYPES: ("contract" | "deal" | "document" | "event" | "invoice" | "lead" | "message" | "order" | "organization" | "payment" | "person" | "pipeline" | "product" | "project" | "subscription" | "transaction" | "wallet")[];
|
|
532
553
|
/** `a` or `an` before a type word: `an event`, `an invoice`, `a message`. */
|
|
533
554
|
export declare const article: (word: string) => "a" | "an";
|
|
534
555
|
export {};
|
|
@@ -307,6 +307,7 @@ export const RECORD_TYPES = {
|
|
|
307
307
|
update: "in-place",
|
|
308
308
|
tombstone: true,
|
|
309
309
|
importOnly: true,
|
|
310
|
+
sheet: false,
|
|
310
311
|
fields: objectOf({
|
|
311
312
|
kind: enumOf(["sms"], "sms: a text message of the phone"),
|
|
312
313
|
direction: enumOf(SMS_DIRECTIONS, "in: the other side wrote it; out: the owner did"),
|
|
@@ -324,6 +325,7 @@ export const RECORD_TYPES = {
|
|
|
324
325
|
update: "in-place",
|
|
325
326
|
tombstone: true,
|
|
326
327
|
importOnly: true,
|
|
328
|
+
sheet: false,
|
|
327
329
|
fields: objectOf({
|
|
328
330
|
kind: enumOf(["call"], "call: a phone call"),
|
|
329
331
|
direction: enumOf(CALL_DIRECTIONS, "in, out, or missed: a call the owner did not take"),
|
|
@@ -333,6 +335,25 @@ export const RECORD_TYPES = {
|
|
|
333
335
|
}, { required: ["kind", "direction", "phone", "started_at"] }),
|
|
334
336
|
roles: COUNTERPART,
|
|
335
337
|
},
|
|
338
|
+
document: {
|
|
339
|
+
table: "documents",
|
|
340
|
+
review: "direct",
|
|
341
|
+
update: "in-place",
|
|
342
|
+
tombstone: true,
|
|
343
|
+
importOnly: true,
|
|
344
|
+
fieldsBytes: 1_048_576,
|
|
345
|
+
fields: objectOf({
|
|
346
|
+
title: text("What the document is called; its external id when left out", 500),
|
|
347
|
+
text: { type: "string", maxLength: 1_000_000, description: "The text search reads" },
|
|
348
|
+
created_at: instant("When the source made it"),
|
|
349
|
+
updated_at: instant("When the source last changed it: what date: reads"),
|
|
350
|
+
url: text("Where the original is", 4096),
|
|
351
|
+
metadata: { type: "object", description: "Anything else the source knows about it, kept as sent" },
|
|
352
|
+
}, { required: ["text"] }),
|
|
353
|
+
roles: {
|
|
354
|
+
about: { targets: ["person", "organization", "deal", "event"], many: true },
|
|
355
|
+
},
|
|
356
|
+
},
|
|
336
357
|
pipeline: {
|
|
337
358
|
table: "pipelines",
|
|
338
359
|
review: "direct",
|
|
@@ -353,8 +374,8 @@ export const RECORD_TYPES = {
|
|
|
353
374
|
},
|
|
354
375
|
};
|
|
355
376
|
export const isRecordType = (type) => Object.hasOwn(RECORD_TYPES, type);
|
|
356
|
-
/** A business record (an invoice, a deal, a wallet): not a person, organization or
|
|
357
|
-
export const isBusinessType = (type) => isRecordType(type) && RECORD_TYPES[type].review !== "identity";
|
|
377
|
+
/** A business record (an invoice, a deal, a wallet): not a person, organization, project or document. */
|
|
378
|
+
export const isBusinessType = (type) => isRecordType(type) && RECORD_TYPES[type].review !== "identity" && type !== "document";
|
|
358
379
|
export const isIdentityType = (type) => typeof type === "string" && isRecordType(type) && RECORD_TYPES[type].review === "identity";
|
|
359
380
|
export const RECORD_ENTITY_TYPES = Object.keys(RECORD_TYPES);
|
|
360
381
|
/** Each registered type's table, for the store's polymorphic pointers. */
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { RECORD_TYPES } from "./record-types.js";
|
|
2
2
|
import { pointerOf, RECORDS_SCHEMA_VERSION, } from "./records.js";
|
|
3
3
|
/** The types a table can carry: an SMS and a call come from a phone's own export, never from a hand-made sheet. */
|
|
4
|
-
export const TABLE_RECORD_TYPES = Object.keys(RECORD_TYPES).filter((type) =>
|
|
4
|
+
export const TABLE_RECORD_TYPES = Object.keys(RECORD_TYPES).filter((type) => RECORD_TYPES[type].sheet !== false);
|
|
5
5
|
export class TableError extends Error {
|
|
6
6
|
errors;
|
|
7
7
|
constructor(errors) {
|
|
@@ -63,7 +63,7 @@ const roleColumn = (name, role) => ({
|
|
|
63
63
|
/** A type's columns: the external id, its flat fields, one per role, then one per key of each list of items. */
|
|
64
64
|
export const recordColumns = (type) => {
|
|
65
65
|
const spec = specOf(type);
|
|
66
|
-
if (!spec || spec.
|
|
66
|
+
if (!spec || spec.sheet === false)
|
|
67
67
|
return [];
|
|
68
68
|
const fields = spec.fields.properties ?? {};
|
|
69
69
|
const required = spec.fields.required ?? [];
|
|
@@ -134,7 +134,8 @@ export const recordTemplate = (type, format, { source, separator = "," } = {}) =
|
|
|
134
134
|
const envelope = {
|
|
135
135
|
schema_version: RECORDS_SCHEMA_VERSION,
|
|
136
136
|
source: {
|
|
137
|
-
provider:
|
|
137
|
+
// An import-only type is refused under a sheet's provider: `import` is the one any folder or export may use.
|
|
138
|
+
provider: source?.provider ?? (specOf(type)?.importOnly ? "import" : "sheet"),
|
|
138
139
|
account: source?.account ?? "main",
|
|
139
140
|
batch_id: source?.batch_id ?? "first-import",
|
|
140
141
|
},
|
package/dist/domain/records.d.ts
CHANGED
|
@@ -167,7 +167,7 @@ export declare const RECORDS_ENVELOPE_SCHEMA: {
|
|
|
167
167
|
if: {
|
|
168
168
|
properties: {
|
|
169
169
|
type: {
|
|
170
|
-
const: "contract" | "deal" | "event" | "invoice" | "lead" | "message" | "order" | "organization" | "payment" | "person" | "pipeline" | "product" | "project" | "subscription" | "transaction" | "wallet";
|
|
170
|
+
const: "contract" | "deal" | "document" | "event" | "invoice" | "lead" | "message" | "order" | "organization" | "payment" | "person" | "pipeline" | "product" | "project" | "subscription" | "transaction" | "wallet";
|
|
171
171
|
};
|
|
172
172
|
};
|
|
173
173
|
required: string[];
|
package/dist/domain/records.js
CHANGED
|
@@ -234,8 +234,9 @@ export const recordErrors = (record, index) => {
|
|
|
234
234
|
return withKey([invalid([...at, "fields"], "an upsert carries its fields as an object")]);
|
|
235
235
|
if (record.fields !== undefined && !isObject(record.fields))
|
|
236
236
|
return withKey([invalid([...at, "fields"], "fields is an object")]);
|
|
237
|
-
|
|
238
|
-
|
|
237
|
+
const most = (isRecordType(record.type) && RECORD_TYPES[record.type].fieldsBytes) || MAX_FIELDS_BYTES;
|
|
238
|
+
if (record.fields !== undefined && Buffer.byteLength(JSON.stringify(record.fields)) > most)
|
|
239
|
+
return withKey([invalid([...at, "fields"], `fields take at most ${most} bytes`, "record.too-large")]);
|
|
239
240
|
const secret = record.fields === undefined ? undefined : secretIn(record.fields, [...at, "fields"]);
|
|
240
241
|
if (secret)
|
|
241
242
|
return withKey([
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { isFolderProvider, kindIn } from "../domain/providers.js";
|
|
1
|
+
import { isFolderProvider, isImportedProvider, kindIn } from "../domain/providers.js";
|
|
2
2
|
/**
|
|
3
3
|
* Where a hit came from. The document's preferred file says it: its account's provider and its link.
|
|
4
4
|
* A Drive document under a notes folder (before version 31 moves it) is still known by its front matter.
|
|
@@ -7,6 +7,14 @@ import { isFolderProvider, kindIn } from "../domain/providers.js";
|
|
|
7
7
|
export const documentSource = (note, accounts, profiles, file) => {
|
|
8
8
|
const metadata = note.frontMatter;
|
|
9
9
|
const account = accounts.find(({ id }) => String(id) === note.folderId);
|
|
10
|
+
// An import's document stays in its source's account, beside that source's calls and SMS.
|
|
11
|
+
if (note.source === "file" && account !== undefined && isImportedProvider(account.provider))
|
|
12
|
+
return {
|
|
13
|
+
kind: "document",
|
|
14
|
+
provider: account.provider,
|
|
15
|
+
account: account.account,
|
|
16
|
+
...(file?.url ? { url: file.url } : {}),
|
|
17
|
+
};
|
|
10
18
|
const remote = note.source === "file" &&
|
|
11
19
|
((account !== undefined && kindIn(account.provider) === "documents") || metadata?.source === "google-drive");
|
|
12
20
|
const profile = remote && account
|
|
@@ -9,7 +9,7 @@ import { type ResolvedNode } from "../search/lucene/resolved.js";
|
|
|
9
9
|
import { type Stemmer } from "../search/stem.js";
|
|
10
10
|
import { type SearchTotal } from "../search/total.js";
|
|
11
11
|
import type { Link, NearestNote, Note, NoteHit } from "../store/index.js";
|
|
12
|
-
import type { AccountKey, MessageStore } from "../store/store.js";
|
|
12
|
+
import type { AccountKey, MessageStore, StoredAccount } from "../store/store.js";
|
|
13
13
|
import { type EnrichOptions, type EnrichRun } from "./enrichment.js";
|
|
14
14
|
export interface NotesSearchRequest {
|
|
15
15
|
text: string;
|
|
@@ -203,13 +203,18 @@ export declare const RANK_WITHIN = 2000;
|
|
|
203
203
|
* of `filter` holds for every hit, found by words or by meaning; a query of fields alone is not searched by meaning.
|
|
204
204
|
*/
|
|
205
205
|
export declare const searchNotes: (store: MessageStore, query: string, { limit, offset, tag, filter: expression, folderIds, withInternal, source: given, exact, discover, timezone, env, command, embedders, pictures: withPictures, pictureLimit, pictureEmbedder, }?: NotesQuery) => Promise<NotesFound>;
|
|
206
|
-
/**
|
|
207
|
-
|
|
206
|
+
/**
|
|
207
|
+
* What `notes:internal`, `notes:<folder id>` and an import's own account (`phone:office-pbx`) ask of the store: one
|
|
208
|
+
* query, so a page of it is one page. `stored` gives an import's account its store id.
|
|
209
|
+
*/
|
|
210
|
+
export declare const notesScopeOf: (accounts: readonly AccountKey[], stored?: readonly StoredAccount[]) => Pick<NotesQuery, "folderIds" | "withInternal" | "source">;
|
|
211
|
+
/** The accounts of import-only providers: their documents stay under their own key, `phone:office-pbx`. */
|
|
212
|
+
export declare const importAccounts: (store: MessageStore) => Promise<StoredAccount[]>;
|
|
208
213
|
export interface NotesFoundOf extends NotesFound {
|
|
209
|
-
/** Every notes account read: `internal
|
|
214
|
+
/** Every notes account read: `internal`, each folder's id, and each import's account by its own key. */
|
|
210
215
|
accounts: {
|
|
211
216
|
resource: "notes";
|
|
212
|
-
provider:
|
|
217
|
+
provider: string;
|
|
213
218
|
account: string;
|
|
214
219
|
}[];
|
|
215
220
|
/** A `--source` entry naming nothing `search notes` reads. */
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { CliError } from "@wirecat/cli-base";
|
|
2
2
|
import { dateRange, parseLucene, timezoneOf } from "@wirecat/cli-base/query";
|
|
3
|
-
import { INTERNAL_NOTES } from "../domain/providers.js";
|
|
3
|
+
import { INTERNAL_NOTES, isImportedProvider } from "../domain/providers.js";
|
|
4
4
|
import { normalizeTag } from "../domain/tags.js";
|
|
5
5
|
import { defaultThreads, isTextModelInstalled, openEmbedder, textModelsDirectory, } from "../embeddings/embed.js";
|
|
6
6
|
import { chosenTextModel, meaningFloor, textModel, vectorModelKey } from "../embeddings/models.js";
|
|
@@ -551,13 +551,22 @@ export const searchNotes = async (store, query, { limit = 20, offset = 0, tag, f
|
|
|
551
551
|
...lane,
|
|
552
552
|
};
|
|
553
553
|
};
|
|
554
|
-
/**
|
|
555
|
-
|
|
556
|
-
|
|
554
|
+
/**
|
|
555
|
+
* What `notes:internal`, `notes:<folder id>` and an import's own account (`phone:office-pbx`) ask of the store: one
|
|
556
|
+
* query, so a page of it is one page. `stored` gives an import's account its store id.
|
|
557
|
+
*/
|
|
558
|
+
export const notesScopeOf = (accounts, stored = []) => {
|
|
559
|
+
const folderIds = accounts
|
|
560
|
+
.filter(({ provider, account }) => provider !== "notes" || account !== INTERNAL_NOTES)
|
|
561
|
+
.map(({ provider, account }) => provider === "notes"
|
|
562
|
+
? account
|
|
563
|
+
: String(stored.find((one) => one.provider === provider && one.account === account)?.id ?? 0));
|
|
557
564
|
if (!folderIds.length)
|
|
558
565
|
return { source: "internal" };
|
|
559
566
|
return { folderIds, ...(accounts.length > folderIds.length ? { withInternal: true } : {}) };
|
|
560
567
|
};
|
|
568
|
+
/** The accounts of import-only providers: their documents stay under their own key, `phone:office-pbx`. */
|
|
569
|
+
export const importAccounts = async (store) => (await store.accounts.storedAccounts()).filter(({ provider }) => isImportedProvider(provider));
|
|
561
570
|
/**
|
|
562
571
|
* `search notes` with `--source`: the notes accounts the list names — `notes:internal`, `notes:<folder id>`, `notes`
|
|
563
572
|
* — and every entry naming none of them in `skipped`. Without a list, every notes account `type` leaves in.
|
|
@@ -571,6 +580,7 @@ export const searchNotesOf = async (store, account, text, { accounts: list, app,
|
|
|
571
580
|
...(await store.notes.folders()).map(({ id }) => ({ provider: "notes", account: id })),
|
|
572
581
|
...(await store.notes.documentsAccounts()).map(({ id }) => ({ provider: "notes", account: id })),
|
|
573
582
|
...[await store.notes.addedAccount()].flatMap((added) => (added ? [{ provider: "notes", account: added.id }] : [])),
|
|
583
|
+
...(await importAccounts(store)).map(({ provider, account }) => ({ provider, account })),
|
|
574
584
|
];
|
|
575
585
|
const lock = groupLockOf(app, env);
|
|
576
586
|
const typedScope = list ?? (lock === undefined ? undefined : `group:${lock}`);
|
|
@@ -582,22 +592,24 @@ export const searchNotesOf = async (store, account, text, { accounts: list, app,
|
|
|
582
592
|
? { accounts: held, skipped: [] }
|
|
583
593
|
: fitting(entriesOfList(notesScope, await listContextOf(store, account, app, undefined, env)), ["notes", "documents"], what);
|
|
584
594
|
const source = query.source ?? noteSourceOfQuery(text).source;
|
|
585
|
-
const
|
|
595
|
+
const internal = ({ provider, account }) => provider === "notes" && account === INTERNAL_NOTES;
|
|
596
|
+
const typed = named.accounts.filter((one) => source === undefined || internal(one) === (source === "internal"));
|
|
586
597
|
if (!typed.length && list !== undefined)
|
|
587
598
|
throw new CliError("validation_error", source === "internal"
|
|
588
599
|
? "the query's source:internal keeps the notes written in memo, and the --source list names none of them: " +
|
|
589
600
|
"add notes:internal to --source, or drop source:internal from the query"
|
|
590
601
|
: "the query's source:file keeps the files from notes folders, and the --source list names none of them: " +
|
|
591
602
|
"add notes:<folder id> to --source, or drop source:file from the query");
|
|
592
|
-
const scope = list === undefined ? {} : notesScopeOf(typed);
|
|
603
|
+
const scope = list === undefined ? {} : notesScopeOf(typed, await store.accounts.storedAccounts());
|
|
593
604
|
const found = await searchNotes(store, text, {
|
|
594
605
|
...query,
|
|
595
606
|
...scope,
|
|
596
607
|
...(source ? { source } : {}),
|
|
608
|
+
...(env === undefined ? {} : { env }),
|
|
597
609
|
});
|
|
598
610
|
return {
|
|
599
611
|
...found,
|
|
600
|
-
accounts: typed.map(({ account }) => ({ resource: "notes", provider
|
|
612
|
+
accounts: typed.map(({ provider, account }) => ({ resource: "notes", provider, account })),
|
|
601
613
|
skipped: named.skipped,
|
|
602
614
|
};
|
|
603
615
|
};
|
|
@@ -15,7 +15,7 @@ import { calendarAccounts, searchEvents } from "./events-search.js";
|
|
|
15
15
|
import { excerpt } from "./excerpt.js";
|
|
16
16
|
import { MAX_SEARCH_OFFSET, searchStore } from "./messages-archive-search.js";
|
|
17
17
|
import { nearEligible } from "./messages-discovery.js";
|
|
18
|
-
import { notesScopeOf, RRF_K, searchNotes } from "./notes-search.js";
|
|
18
|
+
import { importAccounts, notesScopeOf, RRF_K, searchNotes } from "./notes-search.js";
|
|
19
19
|
import { BUSINESS_WORDS_ONLY, searchBusiness } from "./search-all-business.js";
|
|
20
20
|
import { meetingLane } from "./search-all-meetings.js";
|
|
21
21
|
import { HARDER_CANDIDATES, reorderHarder } from "./search-harder.js";
|
|
@@ -85,11 +85,16 @@ const sourceScope = async (store, account, source, asked, messenger) => {
|
|
|
85
85
|
? new Set((await store.business.accounts()).map(accountKeyOf))
|
|
86
86
|
: new Set();
|
|
87
87
|
const holdsBusiness = (one) => imported.has(accountKeyOf(one));
|
|
88
|
-
|
|
88
|
+
// An import's account holds documents under its own key, and the notes lane reads them.
|
|
89
|
+
const holdsDocuments = (one) => asked.includes("notes") && holdsKind(one.provider, "documents");
|
|
90
|
+
const { skipped } = fitting(entries, asked.filter((one) => one !== "business"), "search all", (kind) => `--only leaves ${kind} out`, (one) => holdsBusiness(one) || holdsDocuments(one));
|
|
91
|
+
const holds = (resource, one) => resource === "business"
|
|
92
|
+
? holdsBusiness(one)
|
|
93
|
+
: listedHolds(one, resource) || (resource === "notes" && holdsDocuments(one));
|
|
89
94
|
const named = (resource) => [
|
|
90
95
|
...new Map(entries
|
|
91
96
|
.flatMap(({ accounts }) => accounts)
|
|
92
|
-
.filter((one) => (resource
|
|
97
|
+
.filter((one) => holds(resource, one))
|
|
93
98
|
.map(({ provider, account }) => [`${provider}\u0000${account}`, { provider, account }])).values(),
|
|
94
99
|
];
|
|
95
100
|
const scope = Object.fromEntries(asked.map((resource) => [resource, named(resource)]));
|
|
@@ -243,7 +248,7 @@ export const searchAll = async (store, account, asked, messenger = {}, searchMes
|
|
|
243
248
|
limit: depth,
|
|
244
249
|
command,
|
|
245
250
|
embedders,
|
|
246
|
-
...(chosen === undefined ? {} : notesScopeOf(chosen)),
|
|
251
|
+
...(chosen === undefined ? {} : notesScopeOf(chosen, await store.accounts.storedAccounts())),
|
|
247
252
|
...(request.exact ? { exact: true } : {}),
|
|
248
253
|
...(discover ? { discover: true } : {}),
|
|
249
254
|
...(request.timezone === undefined ? {} : { timezone: request.timezone }),
|
|
@@ -268,13 +273,16 @@ export const searchAll = async (store, account, asked, messenger = {}, searchMes
|
|
|
268
273
|
text: hit.excerpt,
|
|
269
274
|
foundBy: hit.found_by,
|
|
270
275
|
})));
|
|
271
|
-
const read = chosen
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
+
const read = chosen ?? [
|
|
277
|
+
...[
|
|
278
|
+
INTERNAL_NOTES,
|
|
279
|
+
...(await store.notes.folders()).map(({ id }) => id),
|
|
280
|
+
...(await store.notes.documentsAccounts()).map(({ id }) => id),
|
|
281
|
+
...[await store.notes.addedAccount()].flatMap((added) => (added ? [added.id] : [])),
|
|
282
|
+
].map((account) => ({ provider: "notes", account })),
|
|
283
|
+
...(await importAccounts(store)).map(({ provider, account }) => ({ provider, account })),
|
|
276
284
|
];
|
|
277
|
-
accounts.push(...read.map((account) => ({ resource, provider
|
|
285
|
+
accounts.push(...read.map(({ provider, account }) => ({ resource, provider, account })));
|
|
278
286
|
}
|
|
279
287
|
else if (resource === "events") {
|
|
280
288
|
const read = scope?.events ?? (await calendarAccounts(store));
|
|
@@ -97,6 +97,43 @@ export interface AddedSourceDetails {
|
|
|
97
97
|
/** Where the caller keeps the copy, when one was kept. */
|
|
98
98
|
copy: string | null;
|
|
99
99
|
}
|
|
100
|
+
type Row = Record<string, unknown>;
|
|
101
|
+
/** What a document's text and description write, whichever account holds it. */
|
|
102
|
+
export interface DocumentText {
|
|
103
|
+
kind: string;
|
|
104
|
+
title: string | null;
|
|
105
|
+
fileName: string | null;
|
|
106
|
+
extension: string | null;
|
|
107
|
+
url: string | null;
|
|
108
|
+
mime: string | null;
|
|
109
|
+
size: number | null;
|
|
110
|
+
/** `null` keeps it out of every match by content: an imported document is its source's, never a folder file's. */
|
|
111
|
+
hash: string | null;
|
|
112
|
+
text: string;
|
|
113
|
+
extraction: "text" | "none";
|
|
114
|
+
metadata: string | null;
|
|
115
|
+
language: string | null;
|
|
116
|
+
}
|
|
117
|
+
/** Where a copy of the file is kept, when one is: `text` keeps the text alone. */
|
|
118
|
+
export interface DocumentStorage {
|
|
119
|
+
storage: "text" | "copy";
|
|
120
|
+
localPath: string | null;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* The writes of a document in a given account: the added sources' (`added:owner`) and a records import's (the
|
|
124
|
+
* envelope's own account) go through the same columns.
|
|
125
|
+
*/
|
|
126
|
+
export declare const documentWritesOver: ({ database }: Pick<StoreContext, "database">) => {
|
|
127
|
+
insert: (accountId: number, externalId: string, document: DocumentText, kept: DocumentStorage, at: number) => number;
|
|
128
|
+
/** Its next revision, the text before kept; `revised: false` changes what describes it and keeps the revision. */
|
|
129
|
+
update: (row: Row, document: DocumentText, at: number, revised?: boolean) => void;
|
|
130
|
+
/** A removed document back, as written now. */
|
|
131
|
+
revive: (id: number, document: DocumentText, kept: DocumentStorage, at: number) => void;
|
|
132
|
+
/** Its `files` row, named as its source names it, not by a hash or a key. */
|
|
133
|
+
place: (id: number, at: number, name: string) => void;
|
|
134
|
+
/** Its text goes, its row stays as deleted: what a folder file's removal does. */
|
|
135
|
+
remove: (id: number, at: number) => void;
|
|
136
|
+
};
|
|
100
137
|
/**
|
|
101
138
|
* The key an added source is stored under: its source key, else `sha256:<hash>` (a file's bytes; pasted text or a page
|
|
102
139
|
* by its normalized text, or its address when it has none), and every hash a folder or Drive document of the same
|
|
@@ -108,3 +145,4 @@ export declare const addedSourceKeyOf: (content: AddedSourceContent) => {
|
|
|
108
145
|
hashes: string[];
|
|
109
146
|
};
|
|
110
147
|
export declare const addedSourcesOver: (context: StoreContext) => AddedSources;
|
|
148
|
+
export {};
|
|
@@ -10,6 +10,61 @@ import { documentOf, folderOf, insertLink, keepRevision, pathParts, requiredThin
|
|
|
10
10
|
import { oneOf, textOf } from "./text-input.js";
|
|
11
11
|
import { write } from "./transaction.js";
|
|
12
12
|
export const ADDED_KINDS = ["text", "file", "page"];
|
|
13
|
+
const TEXT_COLUMNS = "kind = ?, title = ?, file_name = ?, extension = ?, url = ?, mime = ?, size = ?, content_hash = ?, body = ?, " +
|
|
14
|
+
"extraction = ?, metadata = ?, language = ?";
|
|
15
|
+
const textValues = (document) => [
|
|
16
|
+
document.kind,
|
|
17
|
+
document.title,
|
|
18
|
+
document.fileName,
|
|
19
|
+
document.extension,
|
|
20
|
+
document.url,
|
|
21
|
+
document.mime,
|
|
22
|
+
document.size,
|
|
23
|
+
document.hash,
|
|
24
|
+
document.text,
|
|
25
|
+
document.extraction,
|
|
26
|
+
document.metadata,
|
|
27
|
+
document.language,
|
|
28
|
+
];
|
|
29
|
+
/**
|
|
30
|
+
* The writes of a document in a given account: the added sources' (`added:owner`) and a records import's (the
|
|
31
|
+
* envelope's own account) go through the same columns.
|
|
32
|
+
*/
|
|
33
|
+
export const documentWritesOver = ({ database }) => ({
|
|
34
|
+
insert: (accountId, externalId, document, kept, at) => Number(database
|
|
35
|
+
.prepare("INSERT INTO documents (account_id, external_id, kind, title, file_name, extension, url, mime, size, " +
|
|
36
|
+
"content_hash, body, extraction, metadata, language, storage, local_path, revision, created_at, updated_at) " +
|
|
37
|
+
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 1, ?, ?) RETURNING id")
|
|
38
|
+
.get(accountId, externalId, ...textValues(document), kept.storage, kept.localPath, at, at)?.id),
|
|
39
|
+
/** Its next revision, the text before kept; `revised: false` changes what describes it and keeps the revision. */
|
|
40
|
+
update: (row, document, at, revised = true) => {
|
|
41
|
+
if (revised)
|
|
42
|
+
keepRevision(database, row, at);
|
|
43
|
+
database
|
|
44
|
+
.prepare(`UPDATE documents SET ${TEXT_COLUMNS}, revision = revision + ?, updated_at = ? WHERE id = ?`)
|
|
45
|
+
.run(...textValues(document), revised ? 1 : 0, at, Number(row.id));
|
|
46
|
+
},
|
|
47
|
+
/** A removed document back, as written now. */
|
|
48
|
+
revive: (id, document, kept, at) => {
|
|
49
|
+
database
|
|
50
|
+
.prepare(`UPDATE documents SET ${TEXT_COLUMNS}, storage = ?, local_path = ?, updated_at = ?, deleted_at = NULL WHERE id = ?`)
|
|
51
|
+
.run(...textValues(document), kept.storage, kept.localPath, at, id);
|
|
52
|
+
},
|
|
53
|
+
/** Its `files` row, named as its source names it, not by a hash or a key. */
|
|
54
|
+
place: (id, at, name) => {
|
|
55
|
+
syncFile(database, at, id);
|
|
56
|
+
database
|
|
57
|
+
.prepare("UPDATE files SET name = ?, path = ?, path_folded = ? WHERE document_id = ? AND kind <> 'folder'")
|
|
58
|
+
.run(name, name, fold(name), id);
|
|
59
|
+
},
|
|
60
|
+
/** Its text goes, its row stays as deleted: what a folder file's removal does. */
|
|
61
|
+
remove: (id, at) => {
|
|
62
|
+
database
|
|
63
|
+
.prepare("UPDATE files SET deleted_at = ?, updated_at = ? WHERE document_id = ? AND deleted_at IS NULL")
|
|
64
|
+
.run(at, at, id);
|
|
65
|
+
database.prepare("UPDATE documents SET deleted_at = ?, body = '' WHERE id = ?").run(at, id);
|
|
66
|
+
},
|
|
67
|
+
});
|
|
13
68
|
const sha256 = (value) => createHash("sha256").update(value).digest("hex");
|
|
14
69
|
// `sha256:` is the content's own name, so no key may take it.
|
|
15
70
|
const SOURCE_KEY = /^(?!sha256:)[a-z][a-z0-9-]{0,39}:[A-Za-z0-9._~-]{1,200}$/;
|
|
@@ -66,8 +121,23 @@ const checked = (input) => {
|
|
|
66
121
|
extension: fileName === null ? null : pathParts(fileName).extension,
|
|
67
122
|
};
|
|
68
123
|
};
|
|
124
|
+
const documentTextOf = (source, input) => ({
|
|
125
|
+
kind: source.kind,
|
|
126
|
+
title: source.title,
|
|
127
|
+
fileName: source.fileName,
|
|
128
|
+
extension: source.extension,
|
|
129
|
+
url: source.url,
|
|
130
|
+
mime: input.mime ?? null,
|
|
131
|
+
size: input.size ?? input.bytes?.byteLength ?? null,
|
|
132
|
+
hash: source.hash,
|
|
133
|
+
text: source.text,
|
|
134
|
+
extraction: source.read === "extracted" ? "text" : "none",
|
|
135
|
+
metadata: encodeDocumentMetadata(input.metadata, input.sections),
|
|
136
|
+
language: languageOf(input.metadata),
|
|
137
|
+
});
|
|
69
138
|
export const addedSourcesOver = (context) => {
|
|
70
139
|
const { database, now } = context;
|
|
140
|
+
const writes = documentWritesOver(context);
|
|
71
141
|
const accountRow = () => database.prepare("SELECT * FROM accounts WHERE provider = ? AND external_id = ?").get(ADDED_PROVIDER, ADDED_ACCOUNT);
|
|
72
142
|
const documentAt = (id) => documentOf(database.prepare("SELECT * FROM documents WHERE id = ?").get(id));
|
|
73
143
|
const ownRow = (externalId) => {
|
|
@@ -122,13 +192,6 @@ export const addedSourcesOver = (context) => {
|
|
|
122
192
|
.get(JSON.stringify(hashes));
|
|
123
193
|
return row ? Number(row.id) : undefined;
|
|
124
194
|
};
|
|
125
|
-
/** Its `files` row is named as the owner named the source, not by its hash. */
|
|
126
|
-
const placeFile = (id, at, name) => {
|
|
127
|
-
syncFile(database, at, id);
|
|
128
|
-
database
|
|
129
|
-
.prepare("UPDATE files SET name = ?, path = ?, path_folded = ? WHERE document_id = ? AND kind <> 'folder'")
|
|
130
|
-
.run(name, name, fold(name), id);
|
|
131
|
-
};
|
|
132
195
|
return {
|
|
133
196
|
addedAccount: async () => {
|
|
134
197
|
const row = accountRow();
|
|
@@ -177,11 +240,7 @@ export const addedSourcesOver = (context) => {
|
|
|
177
240
|
.get(id, ADDED_PROVIDER);
|
|
178
241
|
if (!row)
|
|
179
242
|
throw new CliError("not_found", `no live added source ${reference} in the local store`);
|
|
180
|
-
|
|
181
|
-
database
|
|
182
|
-
.prepare("UPDATE files SET deleted_at = ?, updated_at = ? WHERE document_id = ? AND deleted_at IS NULL")
|
|
183
|
-
.run(at, at, id);
|
|
184
|
-
database.prepare("UPDATE documents SET deleted_at = ?, body = '' WHERE id = ?").run(at, id);
|
|
243
|
+
writes.remove(id, now());
|
|
185
244
|
return {
|
|
186
245
|
ref: `document:${id}`,
|
|
187
246
|
removed: true,
|
|
@@ -191,7 +250,8 @@ export const addedSourcesOver = (context) => {
|
|
|
191
250
|
},
|
|
192
251
|
saveAddedSource: async (input) => {
|
|
193
252
|
const source = checked(input);
|
|
194
|
-
const
|
|
253
|
+
const document = documentTextOf(source, input);
|
|
254
|
+
const kept = { storage: source.copy === null ? "text" : "copy", localPath: source.copy };
|
|
195
255
|
const subjects = input.subjects ?? [];
|
|
196
256
|
return write(database, () => {
|
|
197
257
|
const at = now();
|
|
@@ -200,24 +260,18 @@ export const addedSourcesOver = (context) => {
|
|
|
200
260
|
const id = Number(own.id);
|
|
201
261
|
const revised = input.sourceKey !== undefined && own.content_hash !== source.hash;
|
|
202
262
|
if (revised) {
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
.prepare("UPDATE documents SET kind = ?, title = ?, file_name = ?, extension = ?, url = ?, mime = ?, size = ?, " +
|
|
206
|
-
"content_hash = ?, body = ?, extraction = ?, metadata = ?, language = ?, revision = revision + 1, " +
|
|
207
|
-
"updated_at = ? WHERE id = ?")
|
|
208
|
-
.run(source.kind, source.title, source.fileName, source.extension, source.url, input.mime ?? null, input.size ?? input.bytes?.byteLength ?? null, source.hash, source.text, source.read === "extracted" ? "text" : "none", metadata, languageOf(input.metadata), at, id);
|
|
209
|
-
placeFile(id, at, source.fileName ?? source.title ?? source.externalId);
|
|
263
|
+
writes.update(own, document, at);
|
|
264
|
+
writes.place(id, at, source.fileName ?? source.title ?? source.externalId);
|
|
210
265
|
}
|
|
211
266
|
if (source.copy !== null && own.storage !== "copy")
|
|
212
267
|
database
|
|
213
268
|
.prepare("UPDATE documents SET storage = 'copy', local_path = ?, updated_at = ? WHERE id = ?")
|
|
214
269
|
.run(source.copy, at, id);
|
|
215
|
-
const kept = source.copy !== null || own.storage === "copy" ? "copy" : "text";
|
|
216
270
|
return {
|
|
217
271
|
note: documentAt(id),
|
|
218
272
|
created: false,
|
|
219
273
|
duplicateOf: null,
|
|
220
|
-
kept,
|
|
274
|
+
kept: source.copy !== null || own.storage === "copy" ? "copy" : "text",
|
|
221
275
|
changed: revised,
|
|
222
276
|
subjects: about(id, subjects, input.by),
|
|
223
277
|
};
|
|
@@ -228,30 +282,10 @@ export const addedSourcesOver = (context) => {
|
|
|
228
282
|
const note = documentAt(duplicate);
|
|
229
283
|
return { note, created: false, duplicateOf: note.ref, kept: null, changed: false, subjects: linked };
|
|
230
284
|
}
|
|
231
|
-
const values = [
|
|
232
|
-
source.kind,
|
|
233
|
-
source.title,
|
|
234
|
-
source.fileName,
|
|
235
|
-
source.extension,
|
|
236
|
-
source.url,
|
|
237
|
-
source.copy === null ? "text" : "copy",
|
|
238
|
-
source.copy,
|
|
239
|
-
input.mime ?? null,
|
|
240
|
-
input.size ?? input.bytes?.byteLength ?? null,
|
|
241
|
-
source.hash,
|
|
242
|
-
source.text,
|
|
243
|
-
source.read === "extracted" ? "text" : "none",
|
|
244
|
-
metadata,
|
|
245
|
-
languageOf(input.metadata),
|
|
246
|
-
];
|
|
247
285
|
let id;
|
|
248
286
|
if (own) {
|
|
249
287
|
id = Number(own.id);
|
|
250
|
-
|
|
251
|
-
.prepare("UPDATE documents SET kind = ?, title = ?, file_name = ?, extension = ?, url = ?, storage = ?, local_path = ?, " +
|
|
252
|
-
"mime = ?, size = ?, content_hash = ?, body = ?, extraction = ?, metadata = ?, language = ?, " +
|
|
253
|
-
"updated_at = ?, deleted_at = NULL WHERE id = ?")
|
|
254
|
-
.run(...values, at, id);
|
|
288
|
+
writes.revive(id, document, kept, at);
|
|
255
289
|
}
|
|
256
290
|
else {
|
|
257
291
|
const holder = accountRow()?.id ??
|
|
@@ -259,18 +293,14 @@ export const addedSourcesOver = (context) => {
|
|
|
259
293
|
.prepare("INSERT INTO accounts (provider, external_id, name, settings, created_at, updated_at) " +
|
|
260
294
|
"VALUES (?, ?, NULL, ?, ?, ?) RETURNING id")
|
|
261
295
|
.get(ADDED_PROVIDER, ADDED_ACCOUNT, JSON.stringify({ format: "markdown" }), at, at)?.id;
|
|
262
|
-
id = Number(
|
|
263
|
-
.prepare("INSERT INTO documents (account_id, external_id, kind, title, file_name, extension, url, storage, local_path, " +
|
|
264
|
-
"mime, size, content_hash, body, extraction, metadata, language, revision, created_at, updated_at) " +
|
|
265
|
-
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 1, ?, ?) RETURNING id")
|
|
266
|
-
.get(Number(holder), source.externalId, ...values, at, at)?.id);
|
|
296
|
+
id = writes.insert(Number(holder), source.externalId, document, kept, at);
|
|
267
297
|
}
|
|
268
|
-
|
|
298
|
+
writes.place(id, at, source.fileName ?? source.title ?? source.externalId);
|
|
269
299
|
return {
|
|
270
300
|
note: documentAt(id),
|
|
271
301
|
created: !own,
|
|
272
302
|
duplicateOf: null,
|
|
273
|
-
kept:
|
|
303
|
+
kept: kept.storage,
|
|
274
304
|
changed: true,
|
|
275
305
|
subjects: about(id, subjects, input.by),
|
|
276
306
|
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { type Projection, type ProjectionTools } from "./record-projection.js";
|
|
2
|
+
/** A document of the envelope's own account under the record's external id: changed text is its next revision. */
|
|
3
|
+
declare const saveDocument: (projection: Projection, { context }: ProjectionTools) => string;
|
|
4
|
+
export declare const DOCUMENT_PROJECTORS: {
|
|
5
|
+
readonly document: {
|
|
6
|
+
readonly create: typeof saveDocument;
|
|
7
|
+
readonly update: (_ref: string, projection: Projection, tools: ProjectionTools) => true;
|
|
8
|
+
readonly remove: (ref: string, { context }: ProjectionTools) => void;
|
|
9
|
+
readonly restore: (_ref: string, projection: Projection, tools: ProjectionTools) => void;
|
|
10
|
+
};
|
|
11
|
+
};
|
|
12
|
+
export {};
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { documentWritesOver } from "./added-sources.js";
|
|
2
|
+
import { linkKind } from "./link-kinds.js";
|
|
3
|
+
import { requiredThing } from "./notes.js";
|
|
4
|
+
import { given } from "./record-projection.js";
|
|
5
|
+
const TEXT_ONLY = { storage: "text", localPath: null };
|
|
6
|
+
const textOf = ({ fields, externalId }) => {
|
|
7
|
+
const text = String(fields.text ?? "");
|
|
8
|
+
return {
|
|
9
|
+
kind: "text",
|
|
10
|
+
title: given(fields, "title") ?? externalId.slice(0, 500),
|
|
11
|
+
fileName: null,
|
|
12
|
+
extension: null,
|
|
13
|
+
url: given(fields, "url") ?? null,
|
|
14
|
+
mime: null,
|
|
15
|
+
size: null,
|
|
16
|
+
hash: null,
|
|
17
|
+
text,
|
|
18
|
+
extraction: text ? "text" : "none",
|
|
19
|
+
metadata: null,
|
|
20
|
+
language: null,
|
|
21
|
+
};
|
|
22
|
+
};
|
|
23
|
+
const momentOf = (fields, name) => {
|
|
24
|
+
const at = Date.parse(given(fields, name) ?? "");
|
|
25
|
+
return Number.isNaN(at) ? null : at;
|
|
26
|
+
};
|
|
27
|
+
/** The import's own `about` links, written again from the record each time; the owner's stay as they are. */
|
|
28
|
+
const relinkAbout = ({ database, now }, id, subjects) => {
|
|
29
|
+
database
|
|
30
|
+
.prepare(`DELETE FROM links WHERE from_type = 'document' AND from_id = ? AND kind = ${linkKind("about")} AND source = 'import'`)
|
|
31
|
+
.run(id);
|
|
32
|
+
const at = now();
|
|
33
|
+
for (const subject of subjects) {
|
|
34
|
+
const thing = requiredThing(database, subject, "a subject");
|
|
35
|
+
database
|
|
36
|
+
.prepare(`INSERT OR IGNORE INTO links (from_type, from_id, to_type, to_id, kind, source, confirmed, created_at, author,
|
|
37
|
+
updated_at) VALUES ('document', ?, ?, ?, ${linkKind("about")}, 'import', 1, ?, 'import', ?)`)
|
|
38
|
+
.run(id, thing.type, thing.id, at, at);
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
/** A document of the envelope's own account under the record's external id: changed text is its next revision. */
|
|
42
|
+
const saveDocument = (projection, { context }) => {
|
|
43
|
+
const { database } = context;
|
|
44
|
+
const writes = documentWritesOver(context);
|
|
45
|
+
const document = textOf(projection);
|
|
46
|
+
const at = context.now();
|
|
47
|
+
const own = database
|
|
48
|
+
.prepare("SELECT * FROM documents WHERE account_id = ? AND external_id = ?")
|
|
49
|
+
.get(projection.accountId, projection.externalId);
|
|
50
|
+
let id;
|
|
51
|
+
if (own === undefined)
|
|
52
|
+
id = writes.insert(projection.accountId, projection.externalId, document, TEXT_ONLY, at);
|
|
53
|
+
else {
|
|
54
|
+
id = Number(own.id);
|
|
55
|
+
if (own.deleted_at != null)
|
|
56
|
+
writes.revive(id, document, TEXT_ONLY, at);
|
|
57
|
+
else
|
|
58
|
+
writes.update(own, document, at, String(own.body ?? "") !== document.text);
|
|
59
|
+
}
|
|
60
|
+
database
|
|
61
|
+
.prepare("UPDATE documents SET external_created_at = ?, external_updated_at = ? WHERE id = ?")
|
|
62
|
+
.run(momentOf(projection.fields, "created_at"), momentOf(projection.fields, "updated_at"), id);
|
|
63
|
+
writes.place(id, at, document.title ?? projection.externalId);
|
|
64
|
+
relinkAbout(context, id, projection.related.get("about") ?? []);
|
|
65
|
+
return `document:${id}`;
|
|
66
|
+
};
|
|
67
|
+
export const DOCUMENT_PROJECTORS = {
|
|
68
|
+
document: {
|
|
69
|
+
create: saveDocument,
|
|
70
|
+
update: (_ref, projection, tools) => {
|
|
71
|
+
saveDocument(projection, tools);
|
|
72
|
+
return true;
|
|
73
|
+
},
|
|
74
|
+
remove: (ref, { context }) => {
|
|
75
|
+
documentWritesOver(context).remove(requiredThing(context.database, ref).id, context.now());
|
|
76
|
+
},
|
|
77
|
+
restore: (_ref, projection, tools) => {
|
|
78
|
+
saveDocument(projection, tools);
|
|
79
|
+
},
|
|
80
|
+
},
|
|
81
|
+
};
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { DIRECT_PROJECTORS } from "./record-projectors-direct.js";
|
|
2
|
+
import { DOCUMENT_PROJECTORS } from "./record-projectors-document.js";
|
|
2
3
|
import { MONEY_PROJECTORS } from "./record-projectors-money.js";
|
|
3
4
|
import { PHONE_PROJECTORS } from "./record-projectors-phone.js";
|
|
4
5
|
/** One projector per registered type; `null` for the types whose entity is matched or made from identifiers. */
|
|
@@ -6,6 +7,7 @@ export const RECORD_PROJECTORS = {
|
|
|
6
7
|
...MONEY_PROJECTORS,
|
|
7
8
|
...DIRECT_PROJECTORS,
|
|
8
9
|
...PHONE_PROJECTORS,
|
|
10
|
+
...DOCUMENT_PROJECTORS,
|
|
9
11
|
person: null,
|
|
10
12
|
organization: null,
|
|
11
13
|
project: null,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wirecat/cli-core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.649.0",
|
|
4
4
|
"description": "The messenger-neutral half of a messaging CLI: domain model, message locators, rendering, name resolution, the SQLite seam, the command skeleton and shared read commands, the send guard, run records and the message store",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": "Viacheslav Ptsarev",
|
|
@@ -74,7 +74,7 @@
|
|
|
74
74
|
},
|
|
75
75
|
"type": {
|
|
76
76
|
"type": "string",
|
|
77
|
-
"pattern": "^(invoice|payment|person|organization|project|order|product|contract|subscription|wallet|transaction|deal|lead|message|event|pipeline|custom:[a-z0-9][a-z0-9_-]{0,62})$"
|
|
77
|
+
"pattern": "^(invoice|payment|person|organization|project|order|product|contract|subscription|wallet|transaction|deal|lead|message|event|document|pipeline|custom:[a-z0-9][a-z0-9_-]{0,62})$"
|
|
78
78
|
},
|
|
79
79
|
"external_id": {
|
|
80
80
|
"type": "string",
|
|
@@ -357,6 +357,23 @@
|
|
|
357
357
|
}
|
|
358
358
|
}
|
|
359
359
|
},
|
|
360
|
+
{
|
|
361
|
+
"if": {
|
|
362
|
+
"properties": {
|
|
363
|
+
"type": {
|
|
364
|
+
"const": "document"
|
|
365
|
+
}
|
|
366
|
+
},
|
|
367
|
+
"required": ["type"]
|
|
368
|
+
},
|
|
369
|
+
"then": {
|
|
370
|
+
"properties": {
|
|
371
|
+
"fields": {
|
|
372
|
+
"$ref": "#/$defs/fields-document"
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
},
|
|
360
377
|
{
|
|
361
378
|
"if": {
|
|
362
379
|
"properties": {
|
|
@@ -395,7 +412,7 @@
|
|
|
395
412
|
},
|
|
396
413
|
"type": {
|
|
397
414
|
"type": "string",
|
|
398
|
-
"pattern": "^(invoice|payment|person|organization|project|order|product|contract|subscription|wallet|transaction|deal|lead|message|event|pipeline|custom:[a-z0-9][a-z0-9_-]{0,62})$"
|
|
415
|
+
"pattern": "^(invoice|payment|person|organization|project|order|product|contract|subscription|wallet|transaction|deal|lead|message|event|document|pipeline|custom:[a-z0-9][a-z0-9_-]{0,62})$"
|
|
399
416
|
},
|
|
400
417
|
"external_id": {
|
|
401
418
|
"type": "string",
|
|
@@ -1215,6 +1232,44 @@
|
|
|
1215
1232
|
"required": ["kind", "direction", "phone", "started_at"],
|
|
1216
1233
|
"additionalProperties": true
|
|
1217
1234
|
},
|
|
1235
|
+
"fields-document": {
|
|
1236
|
+
"type": "object",
|
|
1237
|
+
"properties": {
|
|
1238
|
+
"title": {
|
|
1239
|
+
"type": ["string", "null"],
|
|
1240
|
+
"minLength": 1,
|
|
1241
|
+
"maxLength": 500,
|
|
1242
|
+
"description": "What the document is called; its external id when left out"
|
|
1243
|
+
},
|
|
1244
|
+
"text": {
|
|
1245
|
+
"type": "string",
|
|
1246
|
+
"maxLength": 1000000,
|
|
1247
|
+
"description": "The text search reads"
|
|
1248
|
+
},
|
|
1249
|
+
"created_at": {
|
|
1250
|
+
"type": ["string", "null"],
|
|
1251
|
+
"pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}(:[0-9]{2}(\\.[0-9]{1,9})?)?(Z|[+-][0-9]{2}:[0-9]{2})$",
|
|
1252
|
+
"description": "When the source made it"
|
|
1253
|
+
},
|
|
1254
|
+
"updated_at": {
|
|
1255
|
+
"type": ["string", "null"],
|
|
1256
|
+
"pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}(:[0-9]{2}(\\.[0-9]{1,9})?)?(Z|[+-][0-9]{2}:[0-9]{2})$",
|
|
1257
|
+
"description": "When the source last changed it: what date: reads"
|
|
1258
|
+
},
|
|
1259
|
+
"url": {
|
|
1260
|
+
"type": ["string", "null"],
|
|
1261
|
+
"minLength": 1,
|
|
1262
|
+
"maxLength": 4096,
|
|
1263
|
+
"description": "Where the original is"
|
|
1264
|
+
},
|
|
1265
|
+
"metadata": {
|
|
1266
|
+
"type": ["object", "null"],
|
|
1267
|
+
"description": "Anything else the source knows about it, kept as sent"
|
|
1268
|
+
}
|
|
1269
|
+
},
|
|
1270
|
+
"required": ["text"],
|
|
1271
|
+
"additionalProperties": true
|
|
1272
|
+
},
|
|
1218
1273
|
"fields-pipeline": {
|
|
1219
1274
|
"type": "object",
|
|
1220
1275
|
"properties": {
|