@wirecat/cli-core 0.699.0 → 0.701.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 +57 -0
- package/dist/cli/documents/words.d.ts +1 -1
- package/dist/cli/service-api.js +19 -6
- package/dist/crm/manifest/manifest.d.ts +35 -2
- package/dist/crm/manifest/manifest.js +26 -3
- package/dist/crm/manifest/map.d.ts +4 -1
- package/dist/crm/manifest/map.js +39 -1
- package/dist/crm/manifest/paths.js +7 -4
- package/dist/crm/manifest/source.js +70 -8
- package/dist/crm/testing/manifest-fake.js +96 -41
- package/dist/documents/html-markdown.d.ts +11 -0
- package/dist/documents/html-markdown.js +81 -0
- package/dist/documents/index.d.ts +1 -0
- package/dist/documents/index.js +1 -0
- package/dist/store/sqlite/vectors.js +11 -4
- package/dist/tracker/manifest/manifest.d.ts +2 -0
- package/dist/tracker/manifest/manifest.js +1 -1
- package/dist/tracker/testing/index.d.ts +1 -0
- package/dist/tracker/testing/index.js +1 -0
- package/dist/tracker/testing/manifest-fake.d.ts +51 -0
- package/dist/tracker/testing/manifest-fake.js +450 -0
- package/package.json +8 -3
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,63 @@ 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.701.0 — 18.11.2026
|
|
9
|
+
|
|
10
|
+
### Added
|
|
11
|
+
|
|
12
|
+
- **`trackerManifestContract`, `trackerManifestFixture` and `trackerManifestFake` in `@wirecat/cli-core/tracker/testing`
|
|
13
|
+
run `trackerSourceContract` over a tracker source built from a manifest**, with no code for the service: a CLI made
|
|
14
|
+
from `wirecat.config.ts` (linear-cli first) checks its tracker in one line,
|
|
15
|
+
`for (const { name, run } of trackerManifestContract(SOURCE, { api, definitions })) it(name, run)`. The fake is
|
|
16
|
+
cli-base's strict `graphqlFake` with three rows of each kind behind it, written where the map reads them and linked
|
|
17
|
+
across kinds, paged by the Relay cursor, read for changes by the since, archived rows hidden unless asked for. The
|
|
18
|
+
fixture brings the held ids, a dead cursor and the faults `skewed`, `change`, `archive` and `remove`
|
|
19
|
+
([TRACKER.md](docs/dev/TRACKER.md#the-contract-kit)).
|
|
20
|
+
|
|
21
|
+
### Fixed
|
|
22
|
+
|
|
23
|
+
- **A conversation rebuild no longer spends minutes dropping the old build's vectors.** For each old chunk text the
|
|
24
|
+
check for a current chunk with the same text read every conversation chunk in the store, and the delete that follows
|
|
25
|
+
read the whole vector key; both now use their index. A rebuild of a forum of about 112,000 messages took 889 s and now
|
|
26
|
+
takes 52 s, on the same store and load.
|
|
27
|
+
|
|
28
|
+
## 0.700.0 — 18.11.2026
|
|
29
|
+
|
|
30
|
+
### Added
|
|
31
|
+
|
|
32
|
+
- **`htmlToMarkdown` and `markdownToHtml` in `@wirecat/cli-core/documents`**, for the content category: a blog keeps
|
|
33
|
+
HTML (WordPress, Ghost), and a CLI shows and takes Markdown. WordPress block comments and Ghost card comments go,
|
|
34
|
+
and so do scripts, styles, frames, event attributes, raw HTML from Markdown, and links and images whose scheme is
|
|
35
|
+
not http(s), mailto or tel: a post body is untrusted. Tested on one post as WordPress 6.9 stores and renders it and
|
|
36
|
+
as Ghost 6.78 renders it; Markdown that went through HTML once comes back the same. The converters are unified's
|
|
37
|
+
`hast-util-from-html`, `hast-util-to-mdast`, `mdast-util-to-hast` and `hast-util-to-html`, loaded on first use.
|
|
38
|
+
- **A CRM source from a manifest reads a pipeline answered as a flat list of stages** (`stages.each: "."`, grouped by
|
|
39
|
+
`stages.pipeline` or one pipeline), and a hook `pipelines.<kind>.outcome` decides a stage's outcome the data cannot
|
|
40
|
+
say. Attio's deal stages and Pipedrive's `/stages` answer this way.
|
|
41
|
+
- **`custom.typed` reads the account's own fields from typed entries with their history**, as Attio keeps them: the
|
|
42
|
+
current entry's value by its type (`TYPED_VALUE_PATHS`, overridable), a multiple field or several current entries as a list.
|
|
43
|
+
`fields.keep` takes `{ path, is }`, so a config file can say which fields are the account's own.
|
|
44
|
+
- **`entities.person.search` and `entities.organization.search` give a manifest source a live `list`**, for `people
|
|
45
|
+
list` and `organizations list`: words through the search call's `"{query}"`, none through `list`. A condition it
|
|
46
|
+
cannot send is refused, not dropped; a live list pages by offset, so a cursor-paged search is refused.
|
|
47
|
+
- `hooks.extend` gets the service's `call` as its second argument.
|
|
48
|
+
- **The `api` group checks a key by a field's value, as WordPress publishes by `status: "publish"`.** A manifest
|
|
49
|
+
permission with `values` (cli-base's `permissions.valueKeys`) applies when the call's JSON body or its query holds
|
|
50
|
+
one of those values: `{ key: "posts.publish", values: { status: ["publish", "future"] } }` asks for
|
|
51
|
+
`posts.publish` before `api post-wp-v2-posts --body '{"status":"publish"}'` and lets a draft through. Both places
|
|
52
|
+
count, because WordPress reads the same parameter from either. So a profile that denies publishing also stops an
|
|
53
|
+
agent that publishes through the raw `api` group. `--dry-run` and discovery show the key with
|
|
54
|
+
`with status publish or future`.
|
|
55
|
+
|
|
56
|
+
### Changed — may break callers
|
|
57
|
+
|
|
58
|
+
- **cli-core needs `@wirecat/cli-base` 0.118.0**, which carries the manifest's `values` and WordPress's
|
|
59
|
+
`wpJsonToOpenApi`. A CLI that pins an older cli-base raises it with this cli-core.
|
|
60
|
+
|
|
61
|
+
### Fixed
|
|
62
|
+
|
|
63
|
+
- `crmManifestFake` matches a call whatever page size and offset the source asked for, a live list setting its own, and writes a stage's won or lost flag as a boolean or a word, as its schema takes it.
|
|
64
|
+
|
|
8
65
|
## 0.699.0 — 18.11.2026
|
|
9
66
|
|
|
10
67
|
### Added
|
|
@@ -58,8 +58,8 @@ export interface CopyAge {
|
|
|
58
58
|
}
|
|
59
59
|
export declare const ageOf: (lastSync: string | undefined, now?: number) => CopyAge;
|
|
60
60
|
export declare const staleCopy: (provider: string, lastSync: string | undefined, now?: number) => {
|
|
61
|
-
missing?: undefined;
|
|
62
61
|
next?: undefined;
|
|
62
|
+
missing?: undefined;
|
|
63
63
|
meta?: undefined;
|
|
64
64
|
} | {
|
|
65
65
|
missing: {
|
package/dist/cli/service-api.js
CHANGED
|
@@ -39,12 +39,24 @@ const permissionsOf = (operation) => operation.permissions ?? [];
|
|
|
39
39
|
/** By its method too: an override may have marked a DELETE as a plain write. */
|
|
40
40
|
const destructive = (operation) => operation.effect === "destructive" || (operation.binding.kind === "http" && operation.binding.method === "DELETE");
|
|
41
41
|
const flagsOf = ({ body = [], query = [] }) => [...body, ...query];
|
|
42
|
+
const valuesOf = ({ values = {} }) => Object.entries(values);
|
|
43
|
+
/** Always, unless its flags or values name when; `with status publish or future`, `with archived set`. */
|
|
44
|
+
const whenOf = (permission) => {
|
|
45
|
+
const flags = flagsOf(permission);
|
|
46
|
+
const values = valuesOf(permission).map(([field, listed]) => `${field} ${listed.join(" or ")}`);
|
|
47
|
+
if (flags.length === 0 && values.length === 0)
|
|
48
|
+
return undefined;
|
|
49
|
+
return `with ${[...(flags.length > 0 ? [`${flags.join(" or ")} set`] : []), ...values].join(" or ")}`;
|
|
50
|
+
};
|
|
51
|
+
/** A body field or query parameter holding one of the values: WordPress publishes from either. */
|
|
52
|
+
const holds = (value, listed) => typeof value === "string" && listed.includes(value);
|
|
42
53
|
/** A flag counts unless it is absent or plainly false: a service may read `1` or `True` as true. */
|
|
43
54
|
const set = (value) => value !== undefined && value !== null && value !== false && value !== "false";
|
|
44
|
-
/** Whether a manifest key applies to this call: always, or when one of its flags is set. */
|
|
45
|
-
const applies = (permission, body, input) =>
|
|
55
|
+
/** Whether a manifest key applies to this call: always, or when one of its flags is set or a field holds a value. */
|
|
56
|
+
const applies = (permission, body, input) => whenOf(permission) === undefined ||
|
|
46
57
|
(permission.body ?? []).some((field) => isObject(body) && set(body[field])) ||
|
|
47
|
-
(permission.query ?? []).some((name) => set(input.query[name]))
|
|
58
|
+
(permission.query ?? []).some((name) => set(input.query[name])) ||
|
|
59
|
+
valuesOf(permission).some(([field, listed]) => (isObject(body) && holds(body[field], listed)) || holds(input.query[field], listed));
|
|
48
60
|
/** The keys one call checks beside `api.<operation>`: the manifest's that apply, and the CLI's from the body. */
|
|
49
61
|
export const serviceApiKeys = (input, operation, call) => {
|
|
50
62
|
const body = call.body?.trim() ? parseApiJson(call.body) : undefined;
|
|
@@ -66,9 +78,10 @@ export const serviceApiPermissions = (input) => {
|
|
|
66
78
|
const checks = new Map();
|
|
67
79
|
for (const operation of input.manifest.operations)
|
|
68
80
|
checks.set(operation.command, [
|
|
69
|
-
...permissionsOf(operation).map((permission) =>
|
|
70
|
-
|
|
71
|
-
: { key: permission.key, when
|
|
81
|
+
...permissionsOf(operation).map((permission) => {
|
|
82
|
+
const when = whenOf(permission);
|
|
83
|
+
return when === undefined ? permission.key : { key: permission.key, when };
|
|
84
|
+
}),
|
|
72
85
|
...(input.bodyPermissions?.[operation.command] ?? []).map(({ key, when }) => ({ key, when })),
|
|
73
86
|
]);
|
|
74
87
|
const keys = [...new Set([...checks.values()].flat().map((one) => (typeof one === "string" ? one : one.key)))];
|
|
@@ -66,6 +66,11 @@ export interface CrmManifestEntity<K extends CrmKind = CrmKind> {
|
|
|
66
66
|
ids?: CrmOperationCall;
|
|
67
67
|
/** One row by `{id}`, for a live read of a contact or company; a 403 or 404 is no row. */
|
|
68
68
|
get?: CrmOperationCall;
|
|
69
|
+
/**
|
|
70
|
+
* Contacts or companies whose name holds `{query}`, for a live `people list` and `organizations list`; a list with no
|
|
71
|
+
* words reads `list`. Both page by the operation's own `pagination`. Absent, the source has no live list.
|
|
72
|
+
*/
|
|
73
|
+
search?: CrmOperationCall;
|
|
69
74
|
/**
|
|
70
75
|
* The service names the fields a read returns (HubSpot `properties=`, Bitrix24 `select`): `{select}` becomes the
|
|
71
76
|
* names under `from` that `map` reads, the custom field codes, and `also`.
|
|
@@ -86,6 +91,7 @@ export interface CrmManifestEntity<K extends CrmKind = CrmKind> {
|
|
|
86
91
|
custom?: {
|
|
87
92
|
from: string;
|
|
88
93
|
split?: string;
|
|
94
|
+
typed?: CrmTypedValues;
|
|
89
95
|
};
|
|
90
96
|
/**
|
|
91
97
|
* A kind the plan or the sign-in may not have (HubSpot's leads): the service refusing it is a gap with this text and
|
|
@@ -96,14 +102,30 @@ export interface CrmManifestEntity<K extends CrmKind = CrmKind> {
|
|
|
96
102
|
scope?: string;
|
|
97
103
|
};
|
|
98
104
|
}
|
|
105
|
+
/**
|
|
106
|
+
* Values kept as a list of typed entries, each naming its type (Attio's `values.<slug>`): the entry's type picks where
|
|
107
|
+
* its value is, and a replaced entry, one with a time at `current`, is skipped.
|
|
108
|
+
*/
|
|
109
|
+
export interface CrmTypedValues {
|
|
110
|
+
/** Where an entry names its type: `attribute_type`. */
|
|
111
|
+
by: string;
|
|
112
|
+
/** A time an entry was replaced at; an entry with one is history, not the value. */
|
|
113
|
+
current?: string;
|
|
114
|
+
/** Where the value is for each type, over `TYPED_VALUE_PATHS`. */
|
|
115
|
+
paths?: Readonly<Record<string, string>>;
|
|
116
|
+
}
|
|
99
117
|
export interface CrmManifestPipelines {
|
|
100
118
|
call: CrmOperationCall;
|
|
119
|
+
/** Read from the pipeline's row, or from its first stage when the call answers stages (`stages.each` is `.`). */
|
|
101
120
|
map: {
|
|
102
121
|
externalId: CrmRule;
|
|
103
122
|
name: CrmRule;
|
|
104
123
|
};
|
|
105
124
|
stages: {
|
|
125
|
+
/** The stages in a pipeline's row; `.` when the call answers the stages themselves, one row each. */
|
|
106
126
|
each: string;
|
|
127
|
+
/** With `each` `.`: the stage's pipeline, by which the stages are grouped; absent, they are all one pipeline. */
|
|
128
|
+
pipeline?: string;
|
|
107
129
|
map: {
|
|
108
130
|
externalId: CrmRule;
|
|
109
131
|
name: CrmRule;
|
|
@@ -137,7 +159,10 @@ export interface CrmManifestFields {
|
|
|
137
159
|
}[] | undefined);
|
|
138
160
|
};
|
|
139
161
|
/** Which listed fields are the account's own, not the service's built-in ones the map already reads. */
|
|
140
|
-
keep?:
|
|
162
|
+
keep?: {
|
|
163
|
+
path: string;
|
|
164
|
+
is: unknown;
|
|
165
|
+
} | ((raw: CrmRaw) => boolean);
|
|
141
166
|
}
|
|
142
167
|
/** A CRM account described as data: what to call for each kind and where each of our fields is in the answer. */
|
|
143
168
|
export interface CrmManifest {
|
|
@@ -186,8 +211,16 @@ export interface CrmManifestHooks {
|
|
|
186
211
|
call: ServiceApiCall;
|
|
187
212
|
}) => Promise<CrmRaw[]>;
|
|
188
213
|
};
|
|
214
|
+
/** The outcome of a stage the data cannot say (Attio names a lost stage only by its title); `found` is the data's. */
|
|
215
|
+
pipelines?: {
|
|
216
|
+
readonly [K in DealKind]?: {
|
|
217
|
+
outcome?: (stage: CrmRaw, pipeline: CrmRaw, found: StageOutcome) => StageOutcome;
|
|
218
|
+
};
|
|
219
|
+
};
|
|
189
220
|
/** Adds or replaces source methods (`self`, `activities`, a live `list`); `base` is what the manifest built. */
|
|
190
|
-
extend?: (base: CrmSource
|
|
221
|
+
extend?: (base: CrmSource, service: {
|
|
222
|
+
call: ServiceApiCall;
|
|
223
|
+
}) => Partial<CrmSource>;
|
|
191
224
|
}
|
|
192
225
|
export declare const crmRuleSchema: v.GenericSchema<CrmRule>;
|
|
193
226
|
/**
|
|
@@ -35,20 +35,26 @@ const entity = v.strictObject({
|
|
|
35
35
|
})),
|
|
36
36
|
ids: v.optional(call),
|
|
37
37
|
get: v.optional(call),
|
|
38
|
+
search: v.optional(call),
|
|
38
39
|
select: v.optional(v.strictObject({
|
|
39
40
|
from: path,
|
|
40
41
|
also: v.optional(v.array(v.string())),
|
|
41
42
|
max: v.optional(v.pipe(v.number(), v.integer(), v.minValue(1))),
|
|
42
43
|
})),
|
|
43
44
|
map: v.record(v.string(), rule),
|
|
44
|
-
custom: v.optional(v.strictObject({
|
|
45
|
+
custom: v.optional(v.strictObject({
|
|
46
|
+
from: path,
|
|
47
|
+
split: v.optional(v.string()),
|
|
48
|
+
typed: v.optional(v.strictObject({ by: path, current: v.optional(path), paths: v.optional(v.record(v.string(), path)) })),
|
|
49
|
+
})),
|
|
45
50
|
optional: v.optional(v.strictObject({ message: v.pipe(v.string(), v.minLength(1)), scope: v.optional(v.string()) })),
|
|
46
51
|
});
|
|
47
52
|
const pipelines = v.strictObject({
|
|
48
53
|
call,
|
|
49
54
|
map: v.strictObject({ externalId: rule, name: rule }),
|
|
50
55
|
stages: v.strictObject({
|
|
51
|
-
each: path,
|
|
56
|
+
each: v.union([v.literal("."), path]),
|
|
57
|
+
pipeline: v.optional(path),
|
|
52
58
|
map: v.strictObject({ externalId: rule, name: rule, position: v.optional(rule) }),
|
|
53
59
|
outcome: v.union([
|
|
54
60
|
v.strictObject({ path, won: v.optional(v.array(v.string())), lost: v.optional(v.array(v.string())) }),
|
|
@@ -67,7 +73,7 @@ const fields = v.strictObject({
|
|
|
67
73
|
filterable: v.optional(rule),
|
|
68
74
|
options: v.optional(v.union([v.strictObject({ each: path, id: path, label: path }), v.function()])),
|
|
69
75
|
}),
|
|
70
|
-
keep: v.optional(v.function()),
|
|
76
|
+
keep: v.optional(v.union([v.strictObject({ path, is: v.unknown() }), v.function()])),
|
|
71
77
|
});
|
|
72
78
|
const kind = v.picklist(CRM_KINDS);
|
|
73
79
|
const schema = v.strictObject({
|
|
@@ -138,6 +144,21 @@ export const parseCrmManifest = (input, api) => {
|
|
|
138
144
|
if (!hasPlaceholder([one.get.path, one.get.query, one.get.body], "{id}"))
|
|
139
145
|
throw invalid(`entities.${kind}.get`, 'no "{id}" in its path, query or body');
|
|
140
146
|
}
|
|
147
|
+
if (one.search) {
|
|
148
|
+
if (kind !== "person" && kind !== "organization")
|
|
149
|
+
throw invalid(`entities.${kind}.search`, "only contacts and companies are read live");
|
|
150
|
+
checkCall(`entities.${kind}.search`, one.search, true);
|
|
151
|
+
for (const [name, live] of [
|
|
152
|
+
["search", one.search],
|
|
153
|
+
["list", one.list],
|
|
154
|
+
]) {
|
|
155
|
+
const style = operations.get(live.operation)?.pagination?.style;
|
|
156
|
+
if (style !== undefined && style !== "offset")
|
|
157
|
+
throw invalid(`entities.${kind}.${name}`, `a live list pages by offset or not at all, not by ${style}`);
|
|
158
|
+
}
|
|
159
|
+
if (!hasPlaceholder([one.search.path, one.search.query, one.search.body], "{query}"))
|
|
160
|
+
throw invalid(`entities.${kind}.search`, 'no "{query}" in its path, query or body');
|
|
161
|
+
}
|
|
141
162
|
if (one.map.externalId === undefined)
|
|
142
163
|
throw invalid(`entities.${kind}.map.externalId`, "every kind maps its id");
|
|
143
164
|
const fields = [...Object.keys(FIELDS[kind]), "amount", "currency"];
|
|
@@ -152,6 +173,8 @@ export const parseCrmManifest = (input, api) => {
|
|
|
152
173
|
if (!one)
|
|
153
174
|
throw invalid(`pipelines.${kind}`, `${kind} is in kinds, and every deal or lead stands in a pipeline`);
|
|
154
175
|
checkCall(`pipelines.${kind}.call`, one.call, true);
|
|
176
|
+
if (one.stages.pipeline !== undefined && one.stages.each !== ".")
|
|
177
|
+
throw invalid(`pipelines.${kind}.stages.pipeline`, 'groups stages only when the call answers them: each "."');
|
|
155
178
|
}
|
|
156
179
|
for (const [kind, one] of Object.entries(manifest.fields ?? {}))
|
|
157
180
|
if (one)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { CrmField, CrmKind, CrmRecords } from "../../domain/crm.js";
|
|
2
2
|
import { type CrmRaw, id, number, text } from "../values.js";
|
|
3
|
-
import type { CrmRule } from "./manifest.js";
|
|
3
|
+
import type { CrmRule, CrmTypedValues } from "./manifest.js";
|
|
4
4
|
/** The service sent nothing at this path: the field is left out, so the stored copy keeps what it held. */
|
|
5
5
|
export declare const MISSING: unique symbol;
|
|
6
6
|
type Read = unknown | typeof MISSING;
|
|
@@ -44,11 +44,14 @@ export declare const FIELDS: {
|
|
|
44
44
|
export declare const convert: (conversion: Conversion, value: unknown) => unknown;
|
|
45
45
|
/** Our fields from the rules, each converted by `types`; a rule whose path is missing gives no key. */
|
|
46
46
|
export declare const mapFields: (rules: Readonly<Record<string, CrmRule | undefined>>, types: Readonly<Record<string, Conversion>>, raw: CrmRaw) => Record<string, unknown>;
|
|
47
|
+
/** Where a typed entry keeps its value, by its type, as Attio names them; a manifest's `typed.paths` adds or replaces. */
|
|
48
|
+
export declare const TYPED_VALUE_PATHS: Readonly<Record<string, string>>;
|
|
47
49
|
export interface RecordRules {
|
|
48
50
|
map: Readonly<Record<string, CrmRule | undefined>>;
|
|
49
51
|
custom?: {
|
|
50
52
|
from: string;
|
|
51
53
|
split?: string;
|
|
54
|
+
typed?: CrmTypedValues;
|
|
52
55
|
};
|
|
53
56
|
}
|
|
54
57
|
/** One row as our record of the kind, or null for a row without an id. */
|
package/dist/crm/manifest/map.js
CHANGED
|
@@ -178,6 +178,39 @@ export const mapFields = (rules, types, raw) => {
|
|
|
178
178
|
}
|
|
179
179
|
return out;
|
|
180
180
|
};
|
|
181
|
+
/** Where a typed entry keeps its value, by its type, as Attio names them; a manifest's `typed.paths` adds or replaces. */
|
|
182
|
+
export const TYPED_VALUE_PATHS = {
|
|
183
|
+
text: "value",
|
|
184
|
+
number: "value",
|
|
185
|
+
checkbox: "value",
|
|
186
|
+
date: "value",
|
|
187
|
+
timestamp: "value",
|
|
188
|
+
rating: "value",
|
|
189
|
+
currency: "currency_value",
|
|
190
|
+
status: "status.title",
|
|
191
|
+
select: "option.title",
|
|
192
|
+
"record-reference": "target_record_id",
|
|
193
|
+
"actor-reference": "referenced_actor_id",
|
|
194
|
+
domain: "domain",
|
|
195
|
+
"email-address": "email_address",
|
|
196
|
+
"phone-number": "phone_number",
|
|
197
|
+
"personal-name": "full_name",
|
|
198
|
+
interaction: "interacted_at",
|
|
199
|
+
};
|
|
200
|
+
/** The current entries' values: one alone is the value, several a list, none no value. */
|
|
201
|
+
const typedOf = (entries, typed) => {
|
|
202
|
+
const values = listOf(entries).flatMap((entry) => {
|
|
203
|
+
if (!isRaw(entry))
|
|
204
|
+
return [];
|
|
205
|
+
if (typed.current !== undefined && at(entry, typed.current) != null)
|
|
206
|
+
return [];
|
|
207
|
+
const type = text(at(entry, typed.by));
|
|
208
|
+
const path = type === null ? undefined : (typed.paths?.[type] ?? TYPED_VALUE_PATHS[type]);
|
|
209
|
+
const value = path === undefined ? null : at(entry, path);
|
|
210
|
+
return value === null || value === undefined || value === "" ? [] : [value];
|
|
211
|
+
});
|
|
212
|
+
return values.length > 1 ? values : (values[0] ?? null);
|
|
213
|
+
};
|
|
181
214
|
/** The account's own fields' values under `from`, labelled by their definitions. */
|
|
182
215
|
const customOf = (raw, custom, defined) => {
|
|
183
216
|
const under = at(raw, custom.from);
|
|
@@ -185,7 +218,12 @@ const customOf = (raw, custom, defined) => {
|
|
|
185
218
|
return undefined;
|
|
186
219
|
const values = {};
|
|
187
220
|
for (const field of defined) {
|
|
188
|
-
const
|
|
221
|
+
const typed = custom.typed ? typedOf(under[field.code], custom.typed) : undefined;
|
|
222
|
+
const value = !custom.typed
|
|
223
|
+
? under[field.code]
|
|
224
|
+
: field.multiple && typed !== null && !Array.isArray(typed)
|
|
225
|
+
? [typed]
|
|
226
|
+
: typed;
|
|
189
227
|
if (value === null || value === undefined || value === "")
|
|
190
228
|
continue;
|
|
191
229
|
values[field.code] = field.multiple && custom.split && typeof value === "string" ? value.split(custom.split) : value;
|
|
@@ -133,19 +133,22 @@ export const crmManifestProblems = (manifest, api, definitions) => {
|
|
|
133
133
|
...entity.map,
|
|
134
134
|
...(entity.custom ? { "(custom)": entity.custom.from } : {}),
|
|
135
135
|
};
|
|
136
|
-
for (const call of [entity.list, entity.changes, entity.get])
|
|
136
|
+
for (const call of [entity.list, entity.changes, entity.get, entity.search])
|
|
137
137
|
if (call)
|
|
138
138
|
check(`entities.${kind}.map`, call, rules);
|
|
139
139
|
}
|
|
140
140
|
for (const [kind, pipelines] of Object.entries(manifest.pipelines ?? {})) {
|
|
141
141
|
if (!pipelines)
|
|
142
142
|
continue;
|
|
143
|
-
|
|
144
|
-
|
|
143
|
+
const { outcome, each, pipeline } = pipelines.stages;
|
|
144
|
+
// A call that answers stages has one row per stage: the pipeline's map and the stage's read the same row.
|
|
145
|
+
const flat = each === ".";
|
|
146
|
+
check(`pipelines.${kind}.map`, pipelines.call, flat ? pipelines.map : { ...pipelines.map, "(stages)": each });
|
|
145
147
|
check(`pipelines.${kind}.stages.map`, pipelines.call, {
|
|
146
148
|
...pipelines.stages.map,
|
|
147
149
|
...(typeof outcome === "object" && "path" in outcome ? { outcome: outcome.path } : {}),
|
|
148
|
-
|
|
150
|
+
...(pipeline ? { "(pipeline)": pipeline } : {}),
|
|
151
|
+
}, flat ? undefined : each);
|
|
149
152
|
}
|
|
150
153
|
for (const [kind, fields] of Object.entries(manifest.fields ?? {})) {
|
|
151
154
|
if (!fields)
|
|
@@ -1,13 +1,16 @@
|
|
|
1
1
|
import { CliError, isCliError } from "@wirecat/cli-base";
|
|
2
2
|
import { nextPage, parameterValue, withValue } from "../../cli/service-api-manifest.js";
|
|
3
3
|
import { id, isRaw, number, rawsOf, text } from "../values.js";
|
|
4
|
+
import { crmWindowOf, walk } from "../walk.js";
|
|
4
5
|
import { parseCrmManifest, } from "./manifest.js";
|
|
5
6
|
import { at, convert, evaluate, MISSING, mapFields, mapRecord, selectedOf } from "./map.js";
|
|
6
7
|
const OVERLAP_MS = 10 * 60_000;
|
|
8
|
+
/** The most rows a live list asks for in one request. */
|
|
9
|
+
const MOST_LIVE = 100;
|
|
7
10
|
/** How far one read of every page goes: a service that never stops paging must not hold a sync forever. */
|
|
8
11
|
const MOST_PAGES = 500;
|
|
9
12
|
const DROP = Symbol("drop");
|
|
10
|
-
/** Fills `"{since}"`, `"{select}"` and `"{
|
|
13
|
+
/** Fills `"{since}"`, `"{select}"`, `"{id}"` and `"{query}"`; an empty select drops the parameter it stands for. */
|
|
11
14
|
const fill = (value, slots) => {
|
|
12
15
|
if (value === "{since}")
|
|
13
16
|
return slots.since ?? DROP;
|
|
@@ -15,6 +18,8 @@ const fill = (value, slots) => {
|
|
|
15
18
|
return slots.select?.length ? [...slots.select] : DROP;
|
|
16
19
|
if (value === "{id}")
|
|
17
20
|
return slots.id ?? DROP;
|
|
21
|
+
if (value === "{query}")
|
|
22
|
+
return slots.query ?? DROP;
|
|
18
23
|
if (Array.isArray(value))
|
|
19
24
|
return value.map((one) => fill(one, slots)).filter((one) => one !== DROP);
|
|
20
25
|
if (isRaw(value))
|
|
@@ -252,6 +257,43 @@ export const crmSourceFromManifest = (input, options) => {
|
|
|
252
257
|
});
|
|
253
258
|
return next === undefined ? { items, hasMore: false } : { items, hasMore: true, nextCursor: next };
|
|
254
259
|
};
|
|
260
|
+
const live = ["person", "organization"].filter((kind) => manifest.entities[kind]?.search);
|
|
261
|
+
/**
|
|
262
|
+
* A live page of contacts or companies, newest first as the service sorts them: words read `search`, none `list`.
|
|
263
|
+
* Only the words narrow it; a condition the manifest cannot send is refused rather than left out of the answer.
|
|
264
|
+
*/
|
|
265
|
+
async function readLive(kind, query, context) {
|
|
266
|
+
const entity = entityOf(kind);
|
|
267
|
+
if (!entity.search)
|
|
268
|
+
throw new CliError("validation_error", `${manifest.provider} reads no ${kind} live`, {
|
|
269
|
+
reason: "query.unsupported-field",
|
|
270
|
+
});
|
|
271
|
+
if (query.conditions.length)
|
|
272
|
+
throw new CliError("validation_error", `${manifest.provider} narrows a live list only by words in the name, not by ${query.conditions.map(({ field }) => field).join(", ")}`, { reason: "query.unsupported-field" });
|
|
273
|
+
const words = query.text?.trim() || undefined;
|
|
274
|
+
const call = words ? entity.search : entity.list;
|
|
275
|
+
const send = metered(context);
|
|
276
|
+
const defined = entity.custom ? await fieldsOf(kind, send, context) : [];
|
|
277
|
+
const slots = { select: selectOf(kind, defined, context), ...(words ? { query: words } : {}) };
|
|
278
|
+
const pagination = operations.get(call.operation)?.pagination;
|
|
279
|
+
const largest = pagination?.style === "offset" ? MOST_LIVE : query.limit + 1;
|
|
280
|
+
const page = await walk(crmWindowOf(query), largest, async (offset, size) => {
|
|
281
|
+
let request = inputOf(call, slots);
|
|
282
|
+
if (pagination?.style === "offset") {
|
|
283
|
+
request = withValue(request, pagination.offset, offset);
|
|
284
|
+
if (pagination.limit)
|
|
285
|
+
request = withValue(request, pagination.limit, size);
|
|
286
|
+
}
|
|
287
|
+
const answer = await send(call.operation, request);
|
|
288
|
+
const items = call.items ?? (pagination && "items" in pagination ? pagination.items : undefined);
|
|
289
|
+
const rows = rawsOf(items === undefined ? answer : at(answer, items));
|
|
290
|
+
const all = pagination?.style === "offset" ? rows : rows.slice(offset, offset + size);
|
|
291
|
+
// A short page is not the last: a service may cap a page below the size asked for; only an empty one ends it.
|
|
292
|
+
// That costs one request past the last row, which losing the rows past a cap would not repay.
|
|
293
|
+
return { rows: all, more: pagination?.style === "offset" ? rows.length > 0 : rows.length > offset + size };
|
|
294
|
+
});
|
|
295
|
+
return { ...page, items: await mapped(kind, page.items, defined, send) };
|
|
296
|
+
}
|
|
255
297
|
const base = {
|
|
256
298
|
provider: manifest.provider,
|
|
257
299
|
traits: { kinds: manifest.kinds, removedInChanges: false },
|
|
@@ -259,9 +301,10 @@ export const crmSourceFromManifest = (input, options) => {
|
|
|
259
301
|
const spec = manifest.pipelines?.[kind];
|
|
260
302
|
if (!spec)
|
|
261
303
|
return [];
|
|
304
|
+
const outcome = hooks.pipelines?.[kind]?.outcome;
|
|
262
305
|
try {
|
|
263
306
|
const rows = await readAll(spec.call, metered(context));
|
|
264
|
-
return rows.flatMap((raw) => toPipeline(spec, raw) ?? []);
|
|
307
|
+
return groupsOf(spec, rows).flatMap(({ raw, stages }) => toPipeline(spec, raw, stages, outcome) ?? []);
|
|
265
308
|
}
|
|
266
309
|
catch (error) {
|
|
267
310
|
if (optionalRefused(kind, error, context))
|
|
@@ -290,6 +333,7 @@ export const crmSourceFromManifest = (input, options) => {
|
|
|
290
333
|
throw error;
|
|
291
334
|
}
|
|
292
335
|
},
|
|
336
|
+
...(live.length ? { list: readLive } : {}),
|
|
293
337
|
async get(kind, externalId, context) {
|
|
294
338
|
const entity = entityOf(kind);
|
|
295
339
|
if (!entity.get)
|
|
@@ -325,26 +369,42 @@ export const crmSourceFromManifest = (input, options) => {
|
|
|
325
369
|
}
|
|
326
370
|
: {}),
|
|
327
371
|
};
|
|
328
|
-
return { ...base, ...hooks.extend?.(base) };
|
|
372
|
+
return { ...base, ...hooks.extend?.(base, { call: options.call }) };
|
|
329
373
|
};
|
|
330
374
|
const outcomeOf = (spec, stage, pipeline) => {
|
|
331
375
|
if (typeof spec === "function")
|
|
332
376
|
return spec(stage, pipeline);
|
|
333
377
|
if ("value" in spec)
|
|
334
378
|
return spec.value;
|
|
335
|
-
const
|
|
379
|
+
const value = at(stage, spec.path);
|
|
380
|
+
const found = typeof value === "boolean" ? String(value) : (text(value) ?? "");
|
|
336
381
|
if (spec.won?.includes(found))
|
|
337
382
|
return "won";
|
|
338
383
|
return spec.lost?.includes(found) ? "lost" : "open";
|
|
339
384
|
};
|
|
340
|
-
|
|
385
|
+
/**
|
|
386
|
+
* Each pipeline's row and its stages. A call that answers stages (`each` `.`) is grouped by `stages.pipeline`, or is
|
|
387
|
+
* one pipeline; its first stage is the row the pipeline's own map reads.
|
|
388
|
+
*/
|
|
389
|
+
const groupsOf = (spec, rows) => {
|
|
390
|
+
if (spec.stages.each !== ".")
|
|
391
|
+
return rows.map((raw) => ({ raw, stages: rawsOf(at(raw, spec.stages.each)) }));
|
|
392
|
+
const by = spec.stages.pipeline;
|
|
393
|
+
const groups = new Map();
|
|
394
|
+
for (const row of rows) {
|
|
395
|
+
const key = by === undefined ? "" : (id(at(row, by)) ?? "");
|
|
396
|
+
groups.set(key, [...(groups.get(key) ?? []), row]);
|
|
397
|
+
}
|
|
398
|
+
return [...groups.values()].map((stages) => ({ raw: stages[0], stages }));
|
|
399
|
+
};
|
|
400
|
+
const toPipeline = (spec, raw, stages, hook) => {
|
|
341
401
|
const { externalId, name } = mapFields(spec.map, { externalId: "id", name: "text" }, raw);
|
|
342
402
|
if (typeof externalId !== "string")
|
|
343
403
|
return null;
|
|
344
404
|
return {
|
|
345
405
|
externalId,
|
|
346
406
|
name: typeof name === "string" ? name : externalId,
|
|
347
|
-
stages:
|
|
407
|
+
stages: stages.flatMap((stage, place) => {
|
|
348
408
|
const found = mapFields(spec.stages.map, { externalId: "id", name: "text", position: "text" }, stage);
|
|
349
409
|
if (typeof found.externalId !== "string")
|
|
350
410
|
return [];
|
|
@@ -353,14 +413,16 @@ const toPipeline = (spec, raw) => {
|
|
|
353
413
|
externalId: found.externalId,
|
|
354
414
|
name: typeof found.name === "string" ? found.name : found.externalId,
|
|
355
415
|
position: number(found.position) ?? place,
|
|
356
|
-
outcome:
|
|
416
|
+
outcome: hook
|
|
417
|
+
? hook(stage, raw, outcomeOf(spec.stages.outcome, stage, raw))
|
|
418
|
+
: outcomeOf(spec.stages.outcome, stage, raw),
|
|
357
419
|
},
|
|
358
420
|
];
|
|
359
421
|
}),
|
|
360
422
|
};
|
|
361
423
|
};
|
|
362
424
|
const toField = (spec, raw) => {
|
|
363
|
-
if (spec.keep && !spec.keep(raw))
|
|
425
|
+
if (spec.keep && !(typeof spec.keep === "function" ? spec.keep(raw) : at(raw, spec.keep.path) === spec.keep.is))
|
|
364
426
|
return null;
|
|
365
427
|
const { options: optionsRule, ...rules } = spec.map;
|
|
366
428
|
const found = mapFields(rules, { code: "text", label: "text", type: "text", multiple: "bool", filterable: "bool" }, raw);
|