@wirecat/cli-core 1.0.0-rc.0 → 1.0.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 +97 -0
- package/THIRD_PARTY_NOTICES +4 -0
- package/dist/background/daily-archive-timer.d.ts +1 -1
- package/dist/cli/account-rename.js +1 -1
- package/dist/cli/bot/api-input.d.ts +5 -0
- package/dist/cli/bot/api-input.js +6 -1
- package/dist/cli/bot/api.js +7 -1
- package/dist/cli/documents/words.d.ts +1 -1
- package/dist/cli/service-api.js +3 -1
- package/dist/crm/manifest/map.d.ts +5 -2
- package/dist/crm/manifest/map.js +14 -2
- package/dist/domain/organization-sectors.d.ts +10 -0
- package/dist/domain/organization-sectors.generated.d.ts +11 -0
- package/dist/domain/organization-sectors.generated.js +14 -0
- package/dist/domain/organization-sectors.js +9 -0
- package/dist/domain/person-kinds.d.ts +1 -1
- package/dist/entities/index.d.ts +1 -0
- package/dist/entities/knowledge-lists.d.ts +1 -0
- package/dist/entities/knowledge-lists.js +2 -0
- package/dist/errors/reasons.js +12 -3
- package/dist/mail/score.d.ts +2 -0
- package/dist/mail/score.js +9 -2
- package/dist/services/metadata.d.ts +1 -1
- package/dist/services/search-all-rank.d.ts +44 -0
- package/dist/services/search-all-rank.js +112 -0
- package/dist/services/search-all-tracker.d.ts +58 -0
- package/dist/services/search-all-tracker.js +155 -0
- package/dist/services/search-all.d.ts +1 -8
- package/dist/services/search-all.js +18 -199
- package/dist/services/tracker-meaning.js +1 -1
- package/dist/store/areas.d.ts +1 -0
- package/dist/store/index.d.ts +1 -0
- package/dist/store/people.d.ts +22 -1
- package/dist/store/people.js +15 -1
- package/dist/store/sqlite/domain-organizations.d.ts +33 -0
- package/dist/store/sqlite/domain-organizations.js +230 -0
- package/dist/store/sqlite/knowledge.d.ts +2 -9
- package/dist/store/sqlite/manifest.js +2 -0
- package/dist/store/sqlite/migrations.generated.js +21 -0
- package/dist/store/sqlite/organizations.d.ts +12 -1
- package/dist/store/sqlite/organizations.js +9 -1
- package/dist/store/sqlite/people-list.d.ts +6 -1
- package/dist/store/sqlite/people-list.js +66 -14
- package/dist/store/sqlite/person-match-scan.js +11 -1
- package/dist/store/sqlite/schema/sender-bots.d.ts +288 -0
- package/dist/store/sqlite/schema/sender-bots.js +22 -0
- package/dist/store/sqlite/schema.d.ts +1 -0
- package/dist/store/sqlite/schema.js +2 -0
- package/dist/store/sqlite/sender-bots.d.ts +43 -0
- package/dist/store/sqlite/sender-bots.js +189 -0
- package/dist/store/sqlite/sender-signals.d.ts +5 -2
- package/dist/store/sqlite/sender-signals.js +6 -6
- package/dist/store/sqlite/tracker.d.ts +11 -9
- package/dist/store/sqlite/tracker.js +27 -12
- package/dist/store/sqlite/work-tasks.js +2 -0
- package/dist/tracker/cli/index.d.ts +1 -1
- package/dist/tracker/cli/index.js +83 -24
- package/dist/tracker/sync.js +43 -20
- package/dist/tracker/writes.d.ts +28 -0
- package/dist/tracker/writes.js +141 -19
- package/package.json +3 -3
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { SECTOR_DOMAINS } from "./organization-sectors.generated.js";
|
|
2
|
+
/** Most specific first: an organization holding domains of two sectors takes the first. */
|
|
3
|
+
export const SECTORS = Object.keys(SECTOR_DOMAINS);
|
|
4
|
+
let sectors;
|
|
5
|
+
/** The sector of a registrable domain, lower case as `registrableOf` gives it. */
|
|
6
|
+
export const sectorOfDomain = (domain) => {
|
|
7
|
+
sectors ??= new Map(SECTORS.flatMap((sector) => SECTOR_DOMAINS[sector].split(" ").map((one) => [one, sector])));
|
|
8
|
+
return sectors.get(domain);
|
|
9
|
+
};
|
|
@@ -7,7 +7,7 @@ export declare const PERSON_KINDS: readonly ["person", "organization", "automate
|
|
|
7
7
|
/** @experimental */
|
|
8
8
|
export type PersonKind = (typeof PERSON_KINDS)[number];
|
|
9
9
|
/** What decided a kind, besides the ties that make someone a `person`. @experimental */
|
|
10
|
-
export type KindSignal = "test-data" | "bot" | "no-reply" | "notification-address" | "auto-submitted" | "null-return-path" | "bulk-mail" | "bulk-never-replied" | "automated-name" | "organization" | "role-address" | "company-name" | "no-tie" | "no-name";
|
|
10
|
+
export type KindSignal = "test-data" | "bot" | "no-reply" | "notification-address" | "auto-submitted" | "null-return-path" | "bulk-mail" | "bulk-never-replied" | "automated-name" | "organization" | "role-address" | "company-name" | "no-tie" | "no-name" | "owner-choice";
|
|
11
11
|
/** A sender that is a program: «Notifications», «Рассылка», «no-reply». */
|
|
12
12
|
export declare const isAutomatedName: (name: string | null) => boolean;
|
|
13
13
|
/** A legal form or a word only a business calls itself: «ООО Ромашка», «CZECH AIRLINES», «OpenWeather Team». */
|
package/dist/entities/index.d.ts
CHANGED
|
@@ -4130,6 +4130,7 @@ export declare const ENTITIES: {
|
|
|
4130
4130
|
readonly kind: v.PicklistSchema<["company", "team", "family", "community"], undefined>;
|
|
4131
4131
|
readonly name: v.StringSchema<undefined>;
|
|
4132
4132
|
readonly scope: v.PicklistSchema<["personal", "work"], undefined>;
|
|
4133
|
+
readonly sector: v.NullableSchema<v.PicklistSchema<("airline" | "bank" | "delivery" | "financial" | "government" | "news" | "shop" | "social" | "telecom")[], undefined>, undefined>;
|
|
4133
4134
|
readonly created_at: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.IsoTimestampAction<string, "an ISO 8601 time in UTC">, v.EndsWithAction<string, "Z", "an ISO 8601 time in UTC, ending in `Z`">]>;
|
|
4134
4135
|
}, undefined>;
|
|
4135
4136
|
readonly project: v.StrictObjectSchema<{
|
|
@@ -6,6 +6,7 @@ export declare const organization: v.StrictObjectSchema<{
|
|
|
6
6
|
readonly kind: v.PicklistSchema<["company", "team", "family", "community"], undefined>;
|
|
7
7
|
readonly name: v.StringSchema<undefined>;
|
|
8
8
|
readonly scope: v.PicklistSchema<["personal", "work"], undefined>;
|
|
9
|
+
readonly sector: v.NullableSchema<v.PicklistSchema<("airline" | "bank" | "delivery" | "financial" | "government" | "news" | "shop" | "social" | "telecom")[], undefined>, undefined>;
|
|
9
10
|
readonly created_at: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.IsoTimestampAction<string, "an ISO 8601 time in UTC">, v.EndsWithAction<string, "Z", "an ISO 8601 time in UTC, ending in `Z`">]>;
|
|
10
11
|
}, undefined>;
|
|
11
12
|
/** @experimental */
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import * as v from "valibot";
|
|
2
|
+
import { SECTORS } from "../domain/organization-sectors.js";
|
|
2
3
|
import { id, reference, text, time } from "./fields.js";
|
|
3
4
|
/** @experimental */
|
|
4
5
|
export const organization = v.strictObject({
|
|
@@ -7,6 +8,7 @@ export const organization = v.strictObject({
|
|
|
7
8
|
kind: v.picklist(["company", "team", "family", "community"]),
|
|
8
9
|
name: text,
|
|
9
10
|
scope: v.picklist(["personal", "work"]),
|
|
11
|
+
sector: v.nullable(v.picklist(SECTORS)),
|
|
10
12
|
created_at: time,
|
|
11
13
|
});
|
|
12
14
|
/** @experimental */
|
package/dist/errors/reasons.js
CHANGED
|
@@ -72,7 +72,6 @@ const SIGN_IN = [
|
|
|
72
72
|
["account-rename.unconfirmed", "confirmation_required", "renaming a profile asks first; add --yes"],
|
|
73
73
|
["account-rename.moved-run", "permission_error", "a run with moved folders never renames a profile"],
|
|
74
74
|
["account-rename.unpaired", "configuration_error", "the tool named different paths for the old and new name"],
|
|
75
|
-
["account-rename.config", "configuration_error", "the config file cannot be read"],
|
|
76
75
|
];
|
|
77
76
|
/** Reasons every CLI over a provider's API raises alike: gcal and drive today. */
|
|
78
77
|
const PROVIDER_API = [
|
|
@@ -316,6 +315,7 @@ const OTHERS = [
|
|
|
316
315
|
validation("push.too-many-files", "the folder holds more files than one push takes"),
|
|
317
316
|
validation("push.unreadable", "the folder to push could not be read"),
|
|
318
317
|
validation("spreadsheet.unreadable", "the workbook could not be read"),
|
|
318
|
+
["config.unreadable", "configuration_error", "the config file cannot be read"],
|
|
319
319
|
[
|
|
320
320
|
"config.unrepairable",
|
|
321
321
|
"configuration_error",
|
|
@@ -543,7 +543,7 @@ const OTHERS = [
|
|
|
543
543
|
validation("tracker.unlisted-held", "a tracker's id list would mark too much deleted, or the sign-in read only part; nothing was"),
|
|
544
544
|
validation("tracker.write-unsupported", "the tracker does not take this write, or does not recognise a repeated one"),
|
|
545
545
|
validation("tracker.field", "a field of an issue or a comment the tracker's write does not take as given"),
|
|
546
|
-
validation("tracker.reference", "a team, state, label, project or
|
|
546
|
+
validation("tracker.reference", "a team, state, label, project, user or sprint no single row of the copy has by that name"),
|
|
547
547
|
validation("tracker.team-required", "a new issue needs its team: name one, or sync a copy that holds one"),
|
|
548
548
|
["tracker.no-record", "invalid_response", "the tracker answered a write with no record and no error; sync and look"],
|
|
549
549
|
[
|
|
@@ -604,6 +604,11 @@ const OTHERS = [
|
|
|
604
604
|
validation("order.ref", "the reference names no order, or a shop's number names more than one"),
|
|
605
605
|
validation("order.status", "a rejected or deleted order's status does not change"),
|
|
606
606
|
["model.not-downloaded", "not_found", "the model is not downloaded on this machine"],
|
|
607
|
+
[
|
|
608
|
+
"tracker.no-vectors",
|
|
609
|
+
"not_found",
|
|
610
|
+
"no issue, project or comment is embedded with the model yet; sync --embed builds them",
|
|
611
|
+
],
|
|
607
612
|
["person.no-owner", "not_found", "no person is marked as the owner in the store"],
|
|
608
613
|
["person.unknown", "not_found", "the store has no person by that reference; people list gives person:<uid>"],
|
|
609
614
|
["person.no-doubt", "not_found", "nothing about that person is in doubt; people links check lists who is"],
|
|
@@ -619,7 +624,7 @@ const OTHERS = [
|
|
|
619
624
|
[
|
|
620
625
|
"identity.owner-only",
|
|
621
626
|
"permission_error",
|
|
622
|
-
"only the owner undoes a merge,
|
|
627
|
+
"only the owner undoes or confirms a merge, a split or a link, links an identity to themselves, or moves a sender",
|
|
623
628
|
],
|
|
624
629
|
[
|
|
625
630
|
"identity.stale",
|
|
@@ -657,6 +662,10 @@ const OTHERS = [
|
|
|
657
662
|
validation("identity.common-name", "more than six people have the name, so it proposes no one"),
|
|
658
663
|
validation("identity.not-a-person", "a bot, a program or an organization is never proposed as a person by name"),
|
|
659
664
|
validation("identity.owner", "one side is the owner's person, and only the owner joins anyone to themselves"),
|
|
665
|
+
["sender-bot.not-found", "not_found", "no sender bot has that id; people list kind:automated lists them"],
|
|
666
|
+
validation("sender-bot.owner", "the owner's own person is never a bot"),
|
|
667
|
+
validation("sender-bot.not-a-person", "the person named is a sender bot; name a person"),
|
|
668
|
+
validation("sender-bot.assignee", "a sender bot is no one's assignee; move it to a person first"),
|
|
660
669
|
validation("identity.shared-email", "more than six people share the address, so it proposes no one"),
|
|
661
670
|
validation("identity-link.dedicated-review", "an identity link is staged and decided through the people review"),
|
|
662
671
|
validation("identity-split.dedicated-review", "a split of a person is staged and decided through the people review"),
|
package/dist/mail/score.d.ts
CHANGED
|
@@ -42,6 +42,8 @@ export declare const registrableOf: (address: string) => string;
|
|
|
42
42
|
* looked up; a brand display name sent from one of them is the classic scam.
|
|
43
43
|
*/
|
|
44
44
|
export declare const FREE_MAIL: Set<string>;
|
|
45
|
+
/** A throwaway mail domain (CC0 list); `domain` is lower case, as `registrableOf` gives it. */
|
|
46
|
+
export declare const isDisposableDomain: (domain: string) => boolean;
|
|
45
47
|
/**
|
|
46
48
|
* The brand a display name claims while the address belongs to someone else, if any. "Google via LinkedIn" from
|
|
47
49
|
* LinkedIn's own domain is LinkedIn passing a page on; "PayPal via Desk" from anywhere else is still judged whole.
|
package/dist/mail/score.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { disposableEmailBlocklistSet } from "disposable-email-domains-js";
|
|
2
2
|
import { getDomain } from "tldts";
|
|
3
3
|
/**
|
|
4
4
|
* How much each sign of spam, a scam or a robot adds to a sender's score. Gmail filters spam before it reaches
|
|
@@ -66,6 +66,13 @@ export const FREE_MAIL = new Set([
|
|
|
66
66
|
"rambler.ru",
|
|
67
67
|
"zoho.com",
|
|
68
68
|
]);
|
|
69
|
+
let disposable;
|
|
70
|
+
// `isDisposableEmailDomain` builds its set of about 9,000 domains on every call.
|
|
71
|
+
/** A throwaway mail domain (CC0 list); `domain` is lower case, as `registrableOf` gives it. */
|
|
72
|
+
export const isDisposableDomain = (domain) => {
|
|
73
|
+
disposable ??= disposableEmailBlocklistSet();
|
|
74
|
+
return disposable.has(domain);
|
|
75
|
+
};
|
|
69
76
|
/** A brand and the first labels of the domains it really sends from. */
|
|
70
77
|
const BRANDS = {
|
|
71
78
|
paypal: ["paypal"],
|
|
@@ -160,7 +167,7 @@ export const scoreGroup = (items, context) => {
|
|
|
160
167
|
const stamped = items.filter((item) => item.headers?.auth !== undefined);
|
|
161
168
|
if (stamped.length && most(stamped, (item) => item.headers?.auth?.dkim !== "pass"))
|
|
162
169
|
signals.push("dkim_not_passed");
|
|
163
|
-
if (domain !== undefined &&
|
|
170
|
+
if (domain !== undefined && isDisposableDomain(domain))
|
|
164
171
|
signals.push("disposable_domain");
|
|
165
172
|
if (context.bulk >= 0.8 && !context.replied)
|
|
166
173
|
signals.push("bulk_never_replied");
|
|
@@ -14,12 +14,12 @@ export declare const metadataService: (deps: ServiceDeps) => {
|
|
|
14
14
|
dryRun?: boolean;
|
|
15
15
|
}) => Promise<{
|
|
16
16
|
items: ({
|
|
17
|
+
error?: undefined;
|
|
17
18
|
chatId: string;
|
|
18
19
|
tags: import("../domain/channel-tags.js").ChannelTagMatch[];
|
|
19
20
|
algorithm: string;
|
|
20
21
|
fetchedAt: string | null;
|
|
21
22
|
source: string;
|
|
22
|
-
error?: undefined;
|
|
23
23
|
} | {
|
|
24
24
|
tags?: undefined;
|
|
25
25
|
algorithm?: undefined;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { Period } from "../search/question-period.js";
|
|
2
|
+
import type { SearchAllItem, SearchAllRecordItem } from "./search-all.js";
|
|
3
|
+
/**
|
|
4
|
+
* What a deal or a lead is found by: plain words or phrases, all of them, or any with OR. Anything else is refused,
|
|
5
|
+
* so `search all` skips deals with the reason instead of matching a field they do not have.
|
|
6
|
+
*/
|
|
7
|
+
export declare const titleWords: (text: string, refused?: string) => {
|
|
8
|
+
words: string[];
|
|
9
|
+
anyWord: boolean;
|
|
10
|
+
};
|
|
11
|
+
/** A question finds by any of its words, so «что», «кто» or "the" would match nearly every record. */
|
|
12
|
+
export declare const contentWords: (found: {
|
|
13
|
+
words: string[];
|
|
14
|
+
anyWord: boolean;
|
|
15
|
+
}, discover: boolean) => {
|
|
16
|
+
words: string[];
|
|
17
|
+
anyWord: boolean;
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* What the meaning half embeds: the words and phrases asked for, OR included, as notes take them. `title:` narrows to
|
|
21
|
+
* the title, which a hit by meaning cannot be held to, so such a query is searched by words only.
|
|
22
|
+
*/
|
|
23
|
+
export declare const meaningWordsOf: (text: string) => string | undefined;
|
|
24
|
+
/**
|
|
25
|
+
* One list for a lane found both ways, as notes and messages fuse theirs: each hit's rank in either list votes, a
|
|
26
|
+
* question's hit voting once; a hit found both ways keeps its words' text and is found by meaning too.
|
|
27
|
+
*/
|
|
28
|
+
export declare const fusedLane: (byWords: SearchAllRecordItem[], byMeaning: {
|
|
29
|
+
hit: SearchAllRecordItem;
|
|
30
|
+
score: number;
|
|
31
|
+
}[], discover: boolean, cosines: Map<string, number>) => SearchAllRecordItem[];
|
|
32
|
+
/**
|
|
33
|
+
* Every source's hits as one list: each source's best hit first, then every other hit by cosine, then by its rank in
|
|
34
|
+
* its source. Taking turns by rank (MEMO-37) gave each of two sources half of a ten-hit page however weak its hits;
|
|
35
|
+
* one model scores every source, so cosine compares across them. A hit only words found takes the cosine of
|
|
36
|
+
* the hit above it in its source: its source ranked it there, and with none it would follow every hit meaning found.
|
|
37
|
+
*/
|
|
38
|
+
export declare const oneList: (lists: SearchAllItem[][], cosines: Map<string, number>) => SearchAllItem[];
|
|
39
|
+
export declare const PERIOD_DEPTH = 100;
|
|
40
|
+
/**
|
|
41
|
+
* A hit dated inside the question's period gains 0.5 over its place, 1 - rank/100: by rank, because only some hits have
|
|
42
|
+
* a cosine to compare. One scale for every page, so paging past the first 100 never reorders what came before.
|
|
43
|
+
*/
|
|
44
|
+
export declare const insidePeriodFirst: (ordered: SearchAllItem[], period: Period | undefined) => SearchAllItem[];
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { CliError } from "@wirecat/cli-base";
|
|
2
|
+
import { parseLucene } from "@wirecat/cli-base/query";
|
|
3
|
+
import { RRF_K } from "../search/fusion.js";
|
|
4
|
+
import { meaningWords } from "../search/lucene/meaning-parts.js";
|
|
5
|
+
import { SEARCH_QUERY } from "../search/lucene/types.js";
|
|
6
|
+
import { isFunctionWord, words as plainWords } from "../search/question-plan.js";
|
|
7
|
+
/**
|
|
8
|
+
* What a deal or a lead is found by: plain words or phrases, all of them, or any with OR. Anything else is refused,
|
|
9
|
+
* so `search all` skips deals with the reason instead of matching a field they do not have.
|
|
10
|
+
*/
|
|
11
|
+
export const titleWords = (text, refused = "deals and leads are found by the words of their title, company, contacts and activities only") => {
|
|
12
|
+
const { root } = parseLucene(text, SEARCH_QUERY);
|
|
13
|
+
const clauses = root.kind === "boolean" ? root.clauses : [{ node: root, occur: "must" }];
|
|
14
|
+
const words = clauses.map(({ node, occur }) => occur !== "must_not" &&
|
|
15
|
+
node.kind === "predicate" &&
|
|
16
|
+
(node.field === "text" || node.field === "title") &&
|
|
17
|
+
(node.operator === "term" || node.operator === "phrase")
|
|
18
|
+
? // A title holds "invoice", not the question mark of "invoice?".
|
|
19
|
+
node.value.replace(/\?+$/u, "") || node.value
|
|
20
|
+
: (() => {
|
|
21
|
+
throw new CliError("validation_error", refused);
|
|
22
|
+
})());
|
|
23
|
+
return { words, anyWord: clauses.length > 1 && clauses.every(({ occur }) => occur === "should") };
|
|
24
|
+
};
|
|
25
|
+
/** A question finds by any of its words, so «что», «кто» or "the" would match nearly every record. */
|
|
26
|
+
export const contentWords = (found, discover) => {
|
|
27
|
+
if (!discover)
|
|
28
|
+
return found;
|
|
29
|
+
const content = found.words.filter((word) => !plainWords(word).every(isFunctionWord));
|
|
30
|
+
return content.length ? { ...found, words: content } : found;
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* What the meaning half embeds: the words and phrases asked for, OR included, as notes take them. `title:` narrows to
|
|
34
|
+
* the title, which a hit by meaning cannot be held to, so such a query is searched by words only.
|
|
35
|
+
*/
|
|
36
|
+
export const meaningWordsOf = (text) => {
|
|
37
|
+
try {
|
|
38
|
+
const { root } = parseLucene(text, SEARCH_QUERY);
|
|
39
|
+
const nodes = root.kind === "boolean" ? root.clauses.map(({ node }) => node) : [root];
|
|
40
|
+
if (!nodes.every((node) => node.kind === "predicate" && node.field === "text"))
|
|
41
|
+
return undefined;
|
|
42
|
+
return meaningWords(root) || undefined;
|
|
43
|
+
}
|
|
44
|
+
catch {
|
|
45
|
+
return undefined;
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* One list for a lane found both ways, as notes and messages fuse theirs: each hit's rank in either list votes, a
|
|
50
|
+
* question's hit voting once; a hit found both ways keeps its words' text and is found by meaning too.
|
|
51
|
+
*/
|
|
52
|
+
export const fusedLane = (byWords, byMeaning, discover, cosines) => {
|
|
53
|
+
const fused = new Map();
|
|
54
|
+
byWords.forEach((item, rank) => {
|
|
55
|
+
fused.set(item.ref, { item, score: 1 / (RRF_K + rank + 1) });
|
|
56
|
+
});
|
|
57
|
+
byMeaning.forEach(({ hit, score }, rank) => {
|
|
58
|
+
cosines.set(hit.ref, score);
|
|
59
|
+
const held = fused.get(hit.ref);
|
|
60
|
+
if (held && !held.item.foundBy.includes("meaning"))
|
|
61
|
+
held.item.foundBy = [...held.item.foundBy, "meaning"];
|
|
62
|
+
const vote = 1 / (RRF_K + rank + 1);
|
|
63
|
+
if (held && discover)
|
|
64
|
+
held.score = Math.max(held.score, vote);
|
|
65
|
+
else
|
|
66
|
+
fused.set(hit.ref, { item: held?.item ?? hit, score: (held?.score ?? 0) + vote });
|
|
67
|
+
});
|
|
68
|
+
return [...fused.values()].sort((a, b) => b.score - a.score).map(({ item }) => item);
|
|
69
|
+
};
|
|
70
|
+
/** Passages of one meeting share its `ref`, the reference evidence cites; each is still a hit of its own. */
|
|
71
|
+
const hitKey = (item) => item.kind === "meeting" ? `${item.ref}\u0000${item.scope}\u0000${item.id}` : item.ref;
|
|
72
|
+
/**
|
|
73
|
+
* Every source's hits as one list: each source's best hit first, then every other hit by cosine, then by its rank in
|
|
74
|
+
* its source. Taking turns by rank (MEMO-37) gave each of two sources half of a ten-hit page however weak its hits;
|
|
75
|
+
* one model scores every source, so cosine compares across them. A hit only words found takes the cosine of
|
|
76
|
+
* the hit above it in its source: its source ranked it there, and with none it would follow every hit meaning found.
|
|
77
|
+
*/
|
|
78
|
+
export const oneList = (lists, cosines) => {
|
|
79
|
+
const place = new Map();
|
|
80
|
+
for (const list of lists) {
|
|
81
|
+
let above = Number.NEGATIVE_INFINITY;
|
|
82
|
+
list.forEach((item, rank) => {
|
|
83
|
+
const key = hitKey(item);
|
|
84
|
+
above = cosines.get(item.ref) ?? above;
|
|
85
|
+
const held = place.get(key);
|
|
86
|
+
if (!held || rank < held.rank)
|
|
87
|
+
place.set(key, { item, rank, cosine: above });
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
const head = (rank) => Math.min(rank, 1);
|
|
91
|
+
return [...place.values()]
|
|
92
|
+
.sort((a, b) => head(a.rank) - head(b.rank) || b.cosine - a.cosine || a.rank - b.rank)
|
|
93
|
+
.map(({ item }) => item);
|
|
94
|
+
};
|
|
95
|
+
export const PERIOD_DEPTH = 100;
|
|
96
|
+
const PERIOD_BOOST = 0.5;
|
|
97
|
+
/**
|
|
98
|
+
* A hit dated inside the question's period gains 0.5 over its place, 1 - rank/100: by rank, because only some hits have
|
|
99
|
+
* a cosine to compare. One scale for every page, so paging past the first 100 never reorders what came before.
|
|
100
|
+
*/
|
|
101
|
+
export const insidePeriodFirst = (ordered, period) => {
|
|
102
|
+
if (!period)
|
|
103
|
+
return ordered;
|
|
104
|
+
const inside = ({ timestamp }) => {
|
|
105
|
+
const at = timestamp === null ? Number.NaN : Date.parse(timestamp);
|
|
106
|
+
return at >= period.from && at < period.to;
|
|
107
|
+
};
|
|
108
|
+
return ordered
|
|
109
|
+
.map((item, rank) => ({ item, score: 1 - rank / PERIOD_DEPTH + (inside(item) ? PERIOD_BOOST : 0) }))
|
|
110
|
+
.sort((a, b) => b.score - a.score)
|
|
111
|
+
.map(({ item }) => item);
|
|
112
|
+
};
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { type WarmEmbedders } from "../embeddings/embed.js";
|
|
2
|
+
import type { AccountKey, MessageStore, TrackerItem } from "../store/store.js";
|
|
3
|
+
import type { SearchAllRecordItem } from "./search-all.js";
|
|
4
|
+
interface TrackerLaneRequest {
|
|
5
|
+
text: string;
|
|
6
|
+
accounts: AccountKey[];
|
|
7
|
+
depth: number;
|
|
8
|
+
discover: boolean;
|
|
9
|
+
exact?: boolean;
|
|
10
|
+
/** Filled with each hit found by meaning, by its cosine. */
|
|
11
|
+
cosines: Map<string, number>;
|
|
12
|
+
embedders: Pick<WarmEmbedders, "get">;
|
|
13
|
+
command?: string;
|
|
14
|
+
env?: NodeJS.ProcessEnv;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Issues, projects and comments by words, and by meaning when the model and some vectors are there, fused as notes
|
|
18
|
+
* fuse theirs; `records` gives each hit's record by its ref. A query naming a field an issue lacks is a `validation_error`.
|
|
19
|
+
*/
|
|
20
|
+
export declare const trackerLane: (store: MessageStore, { text, accounts: read, depth, discover, exact, cosines, embedders, command, env }: TrackerLaneRequest) => Promise<{
|
|
21
|
+
hits: SearchAllRecordItem[];
|
|
22
|
+
records: Map<string, TrackerItem<"comment" | "issue" | "label" | "project" | "sprint" | "state" | "team" | "user">>;
|
|
23
|
+
hasMore: boolean;
|
|
24
|
+
tracker: {
|
|
25
|
+
by: "words" | "words and meaning";
|
|
26
|
+
meaningSkipped?: string;
|
|
27
|
+
};
|
|
28
|
+
meaningReason?: string | undefined;
|
|
29
|
+
}>;
|
|
30
|
+
/** One hit of `searchTracker`: the record beside how `search all` shows it. */
|
|
31
|
+
export interface TrackerSearchHit {
|
|
32
|
+
hit: SearchAllRecordItem;
|
|
33
|
+
item: TrackerItem;
|
|
34
|
+
}
|
|
35
|
+
export interface TrackerSearchFound {
|
|
36
|
+
items: TrackerSearchHit[];
|
|
37
|
+
hasMore: boolean;
|
|
38
|
+
by: "words" | "words and meaning";
|
|
39
|
+
/** Why meaning was not searched, when it was not. */
|
|
40
|
+
meaningSkipped?: string;
|
|
41
|
+
/** `model.not-downloaded`, or `tracker.no-vectors` when the model is there but nothing is embedded with it. */
|
|
42
|
+
meaningReason?: string;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* A tracker CLI's own search over its accounts' copies, ranked as the tracker lane of `search all` ranks them: words,
|
|
46
|
+
* and meaning once `sync --embed` built the vectors; a question finds what holds some of its words.
|
|
47
|
+
*/
|
|
48
|
+
export declare const searchTracker: (store: MessageStore, request: {
|
|
49
|
+
text: string;
|
|
50
|
+
accounts: AccountKey[];
|
|
51
|
+
limit: number;
|
|
52
|
+
offset?: number;
|
|
53
|
+
now?: number;
|
|
54
|
+
timezone?: string;
|
|
55
|
+
command?: string;
|
|
56
|
+
env?: NodeJS.ProcessEnv;
|
|
57
|
+
}) => Promise<TrackerSearchFound>;
|
|
58
|
+
export {};
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { CliError } from "@wirecat/cli-base";
|
|
2
|
+
import { timezoneOf } from "@wirecat/cli-base/query";
|
|
3
|
+
import { hasGlobalItemIds } from "../domain/providers.js";
|
|
4
|
+
import { trackerBodyOf, trackerTitleOf } from "../domain/tracker.js";
|
|
5
|
+
import { runEmbedders } from "../embeddings/embed.js";
|
|
6
|
+
import { plainQuery } from "../search/lucene/plain.js";
|
|
7
|
+
import { questionPeriod } from "../search/question-period.js";
|
|
8
|
+
import { questionLookup, questionText } from "../search/question-plan.js";
|
|
9
|
+
import { excerpt } from "./excerpt.js";
|
|
10
|
+
import { contentWords, fusedLane, insidePeriodFirst, meaningWordsOf, oneList, PERIOD_DEPTH, titleWords, } from "./search-all-rank.js";
|
|
11
|
+
import { nearestTracker } from "./tracker-meaning.js";
|
|
12
|
+
const TRACKER_HIT_KINDS = { issue: "issue", project: "project", comment: "issue_comment" };
|
|
13
|
+
/** An issue is titled as people say it, `ENG-12 Fix login`; anything else by its name or its first line. */
|
|
14
|
+
const trackerTitle = ({ kind, record }) => {
|
|
15
|
+
const title = trackerTitleOf(kind, record);
|
|
16
|
+
const key = kind === "issue" ? record.key : undefined;
|
|
17
|
+
return key ? `${key} ${title}` : title;
|
|
18
|
+
};
|
|
19
|
+
const trackerExcerpt = ({ kind, record }, query) => excerpt([trackerTitleOf(kind, record), trackerBodyOf(kind, record)].filter(Boolean).join("\n"), query);
|
|
20
|
+
/**
|
|
21
|
+
* One hit per tracker item, best first: the copies other sign-ins hold of one item (`items` maps a hit's ref to the
|
|
22
|
+
* item, for a provider whose ids name one item across its accounts) join the best-placed one, which names every account and takes the best cosine and every way any was found.
|
|
23
|
+
*/
|
|
24
|
+
const oneHitPerItem = (ordered, cosines, items) => {
|
|
25
|
+
const kept = new Map();
|
|
26
|
+
for (const hit of ordered) {
|
|
27
|
+
const key = items.get(hit.ref) ?? hit.ref;
|
|
28
|
+
const held = kept.get(key);
|
|
29
|
+
if (!held) {
|
|
30
|
+
kept.set(key, hit);
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
held.accounts = [...(held.accounts ?? [held.account]), hit.account];
|
|
34
|
+
held.foundBy = ["words", "meaning"].filter((by) => [held, hit].some(({ foundBy }) => foundBy.includes(by)));
|
|
35
|
+
const cosine = cosines.get(hit.ref);
|
|
36
|
+
if (cosine !== undefined && cosine > (cosines.get(held.ref) ?? Number.NEGATIVE_INFINITY))
|
|
37
|
+
cosines.set(held.ref, cosine);
|
|
38
|
+
}
|
|
39
|
+
return [...kept.values()];
|
|
40
|
+
};
|
|
41
|
+
const TRACKER_DEPTH = 500;
|
|
42
|
+
/**
|
|
43
|
+
* Issues, projects and comments by words, and by meaning when the model and some vectors are there, fused as notes
|
|
44
|
+
* fuse theirs; `records` gives each hit's record by its ref. A query naming a field an issue lacks is a `validation_error`.
|
|
45
|
+
*/
|
|
46
|
+
export const trackerLane = async (store, { text, accounts: read, depth, discover, exact, cosines, embedders, command, env }) => {
|
|
47
|
+
const { words, anyWord } = contentWords(titleWords(text, "issues, projects and comments are found by the words and meaning of their title and text only"), discover);
|
|
48
|
+
// Two sign-ins to one workspace hold each item twice, and the copies fold into one hit: read deep enough that the
|
|
49
|
+
// folded list still fills the page.
|
|
50
|
+
const deep = Math.min(depth * read.length, TRACKER_DEPTH);
|
|
51
|
+
const found = await store.tracker.find({
|
|
52
|
+
words,
|
|
53
|
+
anyWord: anyWord || discover,
|
|
54
|
+
...(exact ? { exact: true } : {}),
|
|
55
|
+
kinds: ["issue", "project", "comment"],
|
|
56
|
+
limit: deep,
|
|
57
|
+
accounts: read,
|
|
58
|
+
});
|
|
59
|
+
const items = new Map();
|
|
60
|
+
const records = new Map();
|
|
61
|
+
const hitOf = (at, item, excerpt, foundBy) => {
|
|
62
|
+
const ref = `tracker:${at.provider}/${at.account}/${item.kind}/${item.record.externalId}`;
|
|
63
|
+
if (hasGlobalItemIds(at.provider))
|
|
64
|
+
items.set(ref, JSON.stringify([at.provider, item.kind, item.record.externalId]));
|
|
65
|
+
records.set(ref, item);
|
|
66
|
+
return {
|
|
67
|
+
kind: TRACKER_HIT_KINDS[item.kind],
|
|
68
|
+
// Two sign-ins see the same issue under the same id, so the account keeps their hits apart.
|
|
69
|
+
ref,
|
|
70
|
+
provider: at.provider,
|
|
71
|
+
account: at.account,
|
|
72
|
+
...(item.record.url ? { url: item.record.url } : {}),
|
|
73
|
+
title: trackerTitle(item),
|
|
74
|
+
timestamp: new Date(item.record.updatedAt ?? item.syncedAt).toISOString(),
|
|
75
|
+
text: excerpt,
|
|
76
|
+
foundBy,
|
|
77
|
+
};
|
|
78
|
+
};
|
|
79
|
+
const byWords = found.items.map(({ account: at, item, excerpt: text }) => hitOf(at, item, text, ["words"]));
|
|
80
|
+
const plain = meaningWordsOf(discover ? questionText(text) : text);
|
|
81
|
+
let near;
|
|
82
|
+
let meaningSkipped;
|
|
83
|
+
let meaningReason;
|
|
84
|
+
if (exact)
|
|
85
|
+
meaningSkipped = "--exact searches words as written";
|
|
86
|
+
else if (plain === undefined)
|
|
87
|
+
meaningSkipped = "the query names a title, which meaning cannot follow";
|
|
88
|
+
else
|
|
89
|
+
try {
|
|
90
|
+
near = await nearestTracker(store, plain, {
|
|
91
|
+
limit: deep,
|
|
92
|
+
accounts: read,
|
|
93
|
+
embedders,
|
|
94
|
+
...(command === undefined ? {} : { command }),
|
|
95
|
+
...(env === undefined ? {} : { env }),
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
catch (error) {
|
|
99
|
+
if (!(error instanceof CliError && error.code === "not_found"))
|
|
100
|
+
throw error;
|
|
101
|
+
meaningSkipped = error.message;
|
|
102
|
+
const { reason } = error.details;
|
|
103
|
+
if (typeof reason === "string")
|
|
104
|
+
meaningReason = reason;
|
|
105
|
+
}
|
|
106
|
+
const tracker = {
|
|
107
|
+
by: near === undefined ? "words" : "words and meaning",
|
|
108
|
+
...(meaningSkipped === undefined ? {} : { meaningSkipped }),
|
|
109
|
+
};
|
|
110
|
+
const hits = near === undefined
|
|
111
|
+
? oneHitPerItem(byWords, cosines, items)
|
|
112
|
+
: oneHitPerItem(fusedLane(byWords, near.map(({ account: at, item, score }) => ({
|
|
113
|
+
hit: hitOf(at, item, trackerExcerpt(item, text), ["meaning"]),
|
|
114
|
+
score,
|
|
115
|
+
})), discover, cosines), cosines, items);
|
|
116
|
+
return { hits, records, hasMore: found.hasMore, tracker, ...(meaningReason ? { meaningReason } : {}) };
|
|
117
|
+
};
|
|
118
|
+
/**
|
|
119
|
+
* A tracker CLI's own search over its accounts' copies, ranked as the tracker lane of `search all` ranks them: words,
|
|
120
|
+
* and meaning once `sync --embed` built the vectors; a question finds what holds some of its words.
|
|
121
|
+
*/
|
|
122
|
+
export const searchTracker = async (store, request) => {
|
|
123
|
+
if (!request.text.trim())
|
|
124
|
+
throw new CliError("validation_error", "say what to find");
|
|
125
|
+
const text = plainQuery(request.text);
|
|
126
|
+
const offset = request.offset ?? 0;
|
|
127
|
+
const window = offset + request.limit;
|
|
128
|
+
const period = questionPeriod(text, request.now ?? Date.now(), timezoneOf(request.timezone));
|
|
129
|
+
const cosines = new Map();
|
|
130
|
+
const embedders = runEmbedders();
|
|
131
|
+
try {
|
|
132
|
+
const found = await trackerLane(store, {
|
|
133
|
+
text,
|
|
134
|
+
accounts: request.accounts,
|
|
135
|
+
depth: Math.max(window, period ? PERIOD_DEPTH : 0),
|
|
136
|
+
discover: questionLookup(text) !== undefined,
|
|
137
|
+
cosines,
|
|
138
|
+
embedders,
|
|
139
|
+
...(request.command === undefined ? {} : { command: request.command }),
|
|
140
|
+
...(request.env === undefined ? {} : { env: request.env }),
|
|
141
|
+
});
|
|
142
|
+
const ordered = insidePeriodFirst(oneList([found.hits], cosines), period);
|
|
143
|
+
return {
|
|
144
|
+
items: ordered.slice(offset, window).map((hit) => ({ hit, item: found.records.get(hit.ref) })),
|
|
145
|
+
// The lane reads at most 500 rows, so a page past them would be empty and say there are more.
|
|
146
|
+
hasMore: window < TRACKER_DEPTH && (found.hasMore || ordered.length > window),
|
|
147
|
+
by: found.tracker.by,
|
|
148
|
+
...(found.tracker.meaningSkipped === undefined ? {} : { meaningSkipped: found.tracker.meaningSkipped }),
|
|
149
|
+
...(found.meaningReason === undefined ? {} : { meaningReason: found.meaningReason }),
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
finally {
|
|
153
|
+
await embedders.close();
|
|
154
|
+
}
|
|
155
|
+
};
|
|
@@ -10,6 +10,7 @@ import type { SearchCoverage } from "./messages-search.js";
|
|
|
10
10
|
import { type MeetingAccountCoverage, type MeetingSearchItem } from "./search-all-meetings.js";
|
|
11
11
|
import { type HarderOptions, type HarderResult } from "./search-harder.js";
|
|
12
12
|
import type { Backend, ServerOptions, ServerSearched } from "./server-search.js";
|
|
13
|
+
export { titleWords } from "./search-all-rank.js";
|
|
13
14
|
export declare const RESOURCES_SEARCHED: readonly ["messages", "mail", "notes", "events", "crm", "business", "meetings", "tracker"];
|
|
14
15
|
export type SearchedResource = (typeof RESOURCES_SEARCHED)[number];
|
|
15
16
|
/** One hit of `search all`: a stored record, or a passage of a meeting. */
|
|
@@ -189,14 +190,6 @@ export type SearchAllMessenger = Partial<Pick<MessengerTraits, "savedChatId" | "
|
|
|
189
190
|
*/
|
|
190
191
|
hintCommand?: string | null;
|
|
191
192
|
};
|
|
192
|
-
/**
|
|
193
|
-
* What a deal or a lead is found by: plain words or phrases, all of them, or any with OR. Anything else is refused,
|
|
194
|
-
* so `search all` skips deals with the reason instead of matching a field they do not have.
|
|
195
|
-
*/
|
|
196
|
-
export declare const titleWords: (text: string, refused?: string) => {
|
|
197
|
-
words: string[];
|
|
198
|
-
anyWord: boolean;
|
|
199
|
-
};
|
|
200
193
|
/** A tracker hit's kind: a comment is named for the issue it is on, beside a CRM's and a message's kinds. */
|
|
201
194
|
export type TrackerHitKind = "issue" | "project" | "issue_comment";
|
|
202
195
|
/** The accounts of every tracker the store holds copies of. */
|