@wirecat/cli-core 0.683.0 → 0.685.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 +89 -0
- package/dist/cli/bot/api-http.d.ts +10 -2
- package/dist/cli/bot/api-http.js +50 -45
- package/dist/cli/context.d.ts +6 -1
- package/dist/cli/context.js +16 -2
- package/dist/cli/documents/testing/index.d.ts +1 -1
- package/dist/cli/documents/testing/index.js +1 -1
- package/dist/cli/documents/testing/source-contract.d.ts +34 -1
- package/dist/cli/documents/testing/source-contract.js +153 -1
- package/dist/cli/documents/testing/source.d.ts +2 -0
- package/dist/cli/documents/testing/source.js +8 -1
- package/dist/cli/index.d.ts +5 -2
- package/dist/cli/index.js +5 -2
- package/dist/cli/profile-token.js +13 -3
- package/dist/cli/program.js +6 -4
- package/dist/cli/service-api-manifest.d.ts +40 -0
- package/dist/cli/service-api-manifest.js +106 -0
- package/dist/cli/service-api.d.ts +73 -0
- package/dist/cli/service-api.js +260 -0
- package/dist/cli/shared-command-exports.d.ts +1 -0
- package/dist/cli/shared-command-exports.js +2 -0
- package/dist/crm/cli/index.js +25 -38
- package/dist/crm/digest.d.ts +1 -1
- package/dist/crm/digest.js +9 -1
- package/dist/crm/index.d.ts +1 -1
- package/dist/crm/index.js +1 -1
- package/dist/crm/testing/index.d.ts +1 -1
- package/dist/crm/testing/source-contract.d.ts +26 -1
- package/dist/crm/testing/source-contract.js +102 -0
- package/dist/crm/testing/source.d.ts +2 -0
- package/dist/crm/testing/source.js +6 -0
- package/dist/crm/viewer.d.ts +14 -1
- package/dist/crm/viewer.js +38 -1
- package/dist/http/link-fetch.d.ts +10 -0
- package/dist/http/link-fetch.js +28 -0
- package/dist/meetings/import.d.ts +1 -1
- package/dist/meetings/import.js +2 -0
- package/dist/meetings/index.d.ts +1 -0
- package/dist/meetings/index.js +1 -0
- package/dist/meetings/pull.d.ts +2 -0
- package/dist/meetings/pull.js +18 -0
- package/dist/meetings/search-store.d.ts +2 -0
- package/dist/meetings/search-store.js +16 -1
- package/dist/meetings/store.d.ts +3 -0
- package/dist/oauth/requests.js +48 -13
- package/dist/services/deal-context.d.ts +1 -1
- package/dist/services/deal-context.js +2 -1
- package/dist/services/meeting-hit-search.d.ts +2 -3
- package/dist/services/meeting-hit-search.js +2 -17
- package/dist/services/organization-context.js +22 -12
- package/dist/services/search-all-meetings.d.ts +1 -1
- package/dist/store/sqlite/crm-activities.js +9 -3
- package/dist/store/sqlite/deals.d.ts +0 -2
- package/dist/store/sqlite/deals.js +0 -1
- package/package.json +3 -3
package/dist/oauth/requests.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { CliError, isCliError } from "@wirecat/cli-base";
|
|
1
|
+
import { CliError, isCliError, realSleep } from "@wirecat/cli-base";
|
|
2
2
|
import { withTimeLimit } from "../http/time-limit.js";
|
|
3
3
|
const REQUEST_LIMIT_MS = 30_000;
|
|
4
4
|
export const TOKEN_LOG_EVENT = "oauth.token";
|
|
@@ -15,20 +15,55 @@ const cancelledBy = (signal) => {
|
|
|
15
15
|
return;
|
|
16
16
|
throw isCliError(signal.reason) ? signal.reason : new CliError("cancelled", "sign-in was cancelled");
|
|
17
17
|
};
|
|
18
|
-
const
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
18
|
+
const TRANSIENT = new Set([
|
|
19
|
+
"EAI_AGAIN",
|
|
20
|
+
"ENOTFOUND",
|
|
21
|
+
"ECONNRESET",
|
|
22
|
+
"ECONNREFUSED",
|
|
23
|
+
"ETIMEDOUT",
|
|
24
|
+
"EPIPE",
|
|
25
|
+
"UND_ERR_SOCKET",
|
|
26
|
+
]);
|
|
27
|
+
const ATTEMPTS = 3;
|
|
28
|
+
/** A dropped network or a DNS lookup that failed: Node's fetch puts the system's code on the cause. */
|
|
29
|
+
const transientNetworkError = (error) => {
|
|
30
|
+
if (isCliError(error))
|
|
31
|
+
return error.code === "network_error";
|
|
32
|
+
for (let at = error, depth = 0; at instanceof Error && depth < 4; at = at.cause, depth++) {
|
|
33
|
+
const code = at.code;
|
|
34
|
+
if (typeof code === "string" && TRANSIENT.has(code))
|
|
35
|
+
return true;
|
|
28
36
|
}
|
|
29
|
-
|
|
37
|
+
return false;
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* A token request lost to a network blip (a DNS lookup that failed, a dropped connection) is sent again while the time
|
|
41
|
+
* limit lasts, or one blip stops a whole sync. Sending it again is safe: a refresh the provider already took is
|
|
42
|
+
* refused as the next run's would be, a used code is refused and the owner signs in again, as after a lost answer, and
|
|
43
|
+
* a revoke of a revoked token succeeds (RFC 7009).
|
|
44
|
+
*/
|
|
45
|
+
const post = async (url, body, context, deadline) => {
|
|
46
|
+
const signal = requestSignal(deadline, context.signal);
|
|
47
|
+
for (let attempt = 1;; attempt++) {
|
|
30
48
|
cancelledBy(context.signal);
|
|
31
|
-
|
|
49
|
+
try {
|
|
50
|
+
return await context.fetch(url, {
|
|
51
|
+
method: "POST",
|
|
52
|
+
headers: { "content-type": "application/x-www-form-urlencoded", accept: "application/json" },
|
|
53
|
+
body,
|
|
54
|
+
signal,
|
|
55
|
+
redirect: "error",
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
catch (error) {
|
|
59
|
+
cancelledBy(context.signal);
|
|
60
|
+
if (attempt >= ATTEMPTS || signal.aborted || !transientNetworkError(error))
|
|
61
|
+
return undefined;
|
|
62
|
+
const waited = await realSleep(500 * attempt, signal).then(() => true, () => false);
|
|
63
|
+
cancelledBy(context.signal);
|
|
64
|
+
if (!waited)
|
|
65
|
+
return undefined;
|
|
66
|
+
}
|
|
32
67
|
}
|
|
33
68
|
};
|
|
34
69
|
const clientFields = ({ client }) => ({
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type CrmViewing } from "../crm/index.js";
|
|
2
2
|
import type { DealKind } from "../domain/deals.js";
|
|
3
3
|
import { type Standing } from "../domain/relations.js";
|
|
4
4
|
import type { AccountKey, Deal, MessageStore, StageChange } from "../store/index.js";
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { CliError } from "@wirecat/cli-base";
|
|
2
|
+
import { withReadableDeal } from "../crm/index.js";
|
|
2
3
|
import { isUsable } from "../domain/relations.js";
|
|
3
4
|
import { isClosedState } from "../tasks/index.js";
|
|
4
5
|
import { coverageFor, edgesOf, grouped, namesOf, notRejected, organizationCategories, SECTION_LIMIT, STORE_PAGE, scopeOf, sectionOf, } from "./business-context.js";
|
|
@@ -19,7 +20,7 @@ export const dealContext = async (store, reference, { accounts, limit = SECTION_
|
|
|
19
20
|
reason: "deal.denied",
|
|
20
21
|
});
|
|
21
22
|
const row = await store.deals.get(reference);
|
|
22
|
-
const deal = row.deal !== null && !scope.allows(row.deal) ? { ...row, deal: null } : row;
|
|
23
|
+
const deal = await withReadableDeal(store, scope.viewers, row.deal !== null && !scope.allows(row.deal) ? { ...row, deal: null } : row);
|
|
23
24
|
if (!scope.has(deal.account))
|
|
24
25
|
throw new CliError("not_found", `${deal.ref} is a copy from an account outside the accounts asked for`, {
|
|
25
26
|
reason: "deal.out-of-scope",
|
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import { type MeetingHitSearchRequest } from "../meetings/index.js";
|
|
1
|
+
import { type MeetingHitSearchRequest, refreshMeetingIndex } from "../meetings/index.js";
|
|
2
2
|
import type { VocabularyKind } from "../store/index.js";
|
|
3
3
|
import type { MessageStore } from "../store/store.js";
|
|
4
4
|
export type { MeetingHitSearchRequest } from "../meetings/index.js";
|
|
5
|
-
|
|
6
|
-
export declare const refreshMeetingIndex: (meetings: Pick<MessageStore["meetings"], "indexSearch">, accountId: number, signal?: AbortSignal) => Promise<void>;
|
|
5
|
+
export { refreshMeetingIndex };
|
|
7
6
|
/** Name resolution changes selection only; it never assigns projects or labels from transcript words. */
|
|
8
7
|
export declare const resolveMeetingSelection: (store: {
|
|
9
8
|
knowledge?: MessageStore["knowledge"];
|
|
@@ -1,22 +1,7 @@
|
|
|
1
1
|
import { CliError } from "@wirecat/cli-base";
|
|
2
|
-
import { liftMeetingFields, MEETING_FIELD_NAMES } from "../meetings/index.js";
|
|
2
|
+
import { liftMeetingFields, MEETING_FIELD_NAMES, refreshMeetingIndex, } from "../meetings/index.js";
|
|
3
3
|
import { plainQuery } from "../search/lucene/plain.js";
|
|
4
|
-
|
|
5
|
-
export const refreshMeetingIndex = async (meetings, accountId, signal) => {
|
|
6
|
-
let before = Number.POSITIVE_INFINITY;
|
|
7
|
-
for (;;) {
|
|
8
|
-
signal?.throwIfAborted();
|
|
9
|
-
const { indexed, remaining } = await meetings.indexSearch({
|
|
10
|
-
accountId,
|
|
11
|
-
limit: 1000,
|
|
12
|
-
maxReadBytes: 64 * 1024 ** 2,
|
|
13
|
-
signal,
|
|
14
|
-
});
|
|
15
|
-
if (remaining === 0 || indexed === 0 || remaining >= before)
|
|
16
|
-
return;
|
|
17
|
-
before = remaining;
|
|
18
|
-
}
|
|
19
|
-
};
|
|
4
|
+
export { refreshMeetingIndex };
|
|
20
5
|
/** Name resolution changes selection only; it never assigns projects or labels from transcript words. */
|
|
21
6
|
export const resolveMeetingSelection = async (store, request) => {
|
|
22
7
|
const { project, tag, topic, ...input } = request;
|
|
@@ -3,7 +3,7 @@ import { parseLucene } from "@wirecat/cli-base/query";
|
|
|
3
3
|
import { DEAL_KINDS } from "../domain/deals.js";
|
|
4
4
|
import { isUsable } from "../domain/relations.js";
|
|
5
5
|
import { isClosedState } from "../tasks/index.js";
|
|
6
|
-
import { coverageFor, edgesOf, grouped, namesOf, notRejected, organizationCategories, SECTION_LIMIT, STORE_PAGE, scopeOf, sectionOf, } from "./business-context.js";
|
|
6
|
+
import { coverageFor, edgesOf, grouped, keyOf, namesOf, notRejected, organizationCategories, SECTION_LIMIT, STORE_PAGE, scopeOf, sectionOf, } from "./business-context.js";
|
|
7
7
|
import { communicationsOf } from "./communications.js";
|
|
8
8
|
import { moneyOf } from "./money-context.js";
|
|
9
9
|
export const dealLine = (deal) => ({
|
|
@@ -70,18 +70,28 @@ export const organizationContext = async (store, reference, { accounts, limit =
|
|
|
70
70
|
const partners = grouped(edges, (edge) => (edge.role.kind !== "partner-of" ? undefined : edge.from === ref ? edge.to : edge.from), names);
|
|
71
71
|
const deals = [];
|
|
72
72
|
let dealsFull = false;
|
|
73
|
+
// A viewer's `deals.list` filter hides every row not assigned to it, so each limited copy is read on its own and
|
|
74
|
+
// the rest without it: a page then holds only rows the profile may read.
|
|
75
|
+
const limited = new Set(scope.viewers.map(({ account }) => keyOf(account)));
|
|
76
|
+
const reads = limited.size
|
|
77
|
+
? [
|
|
78
|
+
{ accounts: (accounts ?? (await store.accounts.accounts())).filter((one) => !limited.has(keyOf(one))) },
|
|
79
|
+
...scope.viewers
|
|
80
|
+
.filter(({ account }) => scope.has(account))
|
|
81
|
+
.map((viewer) => ({ account: viewer.account, viewer })),
|
|
82
|
+
]
|
|
83
|
+
: [accounts === undefined ? {} : { accounts }];
|
|
73
84
|
for (const kind of DEAL_KINDS.filter((one) => !scope.denied.has(one))) {
|
|
74
|
-
const
|
|
75
|
-
|
|
76
|
-
organization: ref,
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
for (const deal of
|
|
83
|
-
|
|
84
|
-
deals.push(deal);
|
|
85
|
+
const ofKind = [];
|
|
86
|
+
for (const read of reads) {
|
|
87
|
+
const found = await store.deals.list({ kind, organization: ref, status: "open", limit: STORE_PAGE, ...read });
|
|
88
|
+
dealsFull ||= found.hasMore;
|
|
89
|
+
for (const deal of found.items)
|
|
90
|
+
ofKind.push(deal);
|
|
91
|
+
}
|
|
92
|
+
ofKind.sort((a, b) => b.createdAt.localeCompare(a.createdAt) || Number(b.id) - Number(a.id));
|
|
93
|
+
for (const deal of ofKind)
|
|
94
|
+
deals.push(deal);
|
|
85
95
|
}
|
|
86
96
|
const catalog = new Map((await store.knowledge.projects()).map((project) => [project.ref, project]));
|
|
87
97
|
const projects = new Map();
|
|
@@ -35,7 +35,7 @@ export interface MeetingSearchItem {
|
|
|
35
35
|
export interface MeetingAccountCoverage {
|
|
36
36
|
provider: string;
|
|
37
37
|
account: string;
|
|
38
|
-
/** `pending`: passages saved since the index was last built are not found yet;
|
|
38
|
+
/** `pending`: passages saved since the index was last built are not found yet; a sync builds it. */
|
|
39
39
|
indexing: "ready" | "pending";
|
|
40
40
|
hasMore: boolean;
|
|
41
41
|
}
|
|
@@ -21,6 +21,8 @@ export const forgetActivitiesOn = (database, accountId, kind, externalIds, at) =
|
|
|
21
21
|
/** A CRM's text field holds what people typed or pasted, a whole e-mail thread too. */
|
|
22
22
|
const MOST_BODY = 20_000;
|
|
23
23
|
const MOST_SUBJECT = 500;
|
|
24
|
+
/** The kinds that record something already done rather than ask for it. */
|
|
25
|
+
const LOGGED = new Set(["comment", "call", "email", "meeting"]);
|
|
24
26
|
const activityOf = (externalId, fields) => ({
|
|
25
27
|
externalId,
|
|
26
28
|
kind: fields.kind,
|
|
@@ -65,15 +67,19 @@ export const crmActivitiesOver = (context) => {
|
|
|
65
67
|
.prepare(`SELECT 1 FROM ${TABLES[kind]} WHERE account_id = ? AND external_id = ? AND deleted_at IS NULL`)
|
|
66
68
|
.get(accountId, externalId)) !== undefined;
|
|
67
69
|
/**
|
|
68
|
-
* When it was done, as near as the copy can tell: the CRM's change that closed it, kept once seen.
|
|
69
|
-
*
|
|
70
|
+
* When it was done, as near as the copy can tell: the CRM's change that closed it, kept once seen. A record of
|
|
71
|
+
* something already done — a comment, a logged call, mail or meeting — that arrives done with no due date was done
|
|
72
|
+
* when it was made, and `changes list` lists it once. A task or to-do may be closed long after it was made, often
|
|
73
|
+
* with no deadline in Bitrix24, so it keeps the CRM's change.
|
|
70
74
|
*/
|
|
71
75
|
const doneAt = (activity, held) => {
|
|
72
76
|
if (!activity.done)
|
|
73
77
|
return null;
|
|
74
78
|
if (held?.done)
|
|
75
79
|
return held.done_at ?? null;
|
|
76
|
-
return held === undefined && activity.dueAt == null
|
|
80
|
+
return held === undefined && activity.dueAt == null && LOGGED.has(activity.kind)
|
|
81
|
+
? activity.createdAt
|
|
82
|
+
: activity.updatedAt;
|
|
77
83
|
};
|
|
78
84
|
const save = (key, activities) => write(database, () => {
|
|
79
85
|
const accountId = accountPk(context, key);
|
|
@@ -40,8 +40,6 @@ export interface PipelineReport extends Omit<Pipeline, "stages">, Omit<PipelineP
|
|
|
40
40
|
since: string | null;
|
|
41
41
|
until: string | null;
|
|
42
42
|
};
|
|
43
|
-
/** `YYYY-MM-DD`: the UTC day `window.since` falls on; `null` for all. */
|
|
44
|
-
since: string | null;
|
|
45
43
|
}
|
|
46
44
|
/**
|
|
47
45
|
* One stage a deal or a lead entered: from the CRM's own history (`exact`), else as the store saw it, a CRM copy's
|
|
@@ -247,7 +247,6 @@ export const dealOperationsOver = (context) => {
|
|
|
247
247
|
...period,
|
|
248
248
|
stages: pipeline.stages.map((stage, index) => ({ ...stage, ...stages[index] })),
|
|
249
249
|
window: { since: at(window.since), until: at(window.until) },
|
|
250
|
-
since: at(window.since)?.slice(0, 10) ?? null,
|
|
251
250
|
};
|
|
252
251
|
};
|
|
253
252
|
/** Sets the stage and, from its outcome, the status; a stage that ends the row closes it today. */
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wirecat/cli-core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.685.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",
|
|
@@ -300,7 +300,7 @@
|
|
|
300
300
|
"@types/pngjs": "6.0.5",
|
|
301
301
|
"@types/ssh2": "1.15.5",
|
|
302
302
|
"@vitest/coverage-v8": "^5.0.5",
|
|
303
|
-
"@wirecat/cli-base": "0.
|
|
303
|
+
"@wirecat/cli-base": "0.108.0",
|
|
304
304
|
"@wirecat/cli-core-legacy-tags": "npm:@wirecat/cli-core@0.414.0",
|
|
305
305
|
"ajv": "8.17.1",
|
|
306
306
|
"commander": "15.0.0",
|
|
@@ -324,7 +324,7 @@
|
|
|
324
324
|
},
|
|
325
325
|
"peerDependencies": {
|
|
326
326
|
"@napi-rs/canvas": ">=0.1.69 <2",
|
|
327
|
-
"@wirecat/cli-base": ">=0.
|
|
327
|
+
"@wirecat/cli-base": ">=0.108.0 <1.0.0",
|
|
328
328
|
"commander": ">=15",
|
|
329
329
|
"mysql2": "^3.24.6",
|
|
330
330
|
"pg": "^8.23.1",
|