@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
|
@@ -61,6 +61,13 @@ const SINCE = {
|
|
|
61
61
|
iso: (value) => Date.parse(value),
|
|
62
62
|
datetime: (value) => Date.parse(`${value.replace(" ", "T")}Z`),
|
|
63
63
|
};
|
|
64
|
+
const pageParameter = (pagination) => pagination.style === "cursor"
|
|
65
|
+
? pagination.cursor
|
|
66
|
+
: pagination.style === "offset"
|
|
67
|
+
? pagination.offset
|
|
68
|
+
: pagination.style === "page"
|
|
69
|
+
? pagination.page
|
|
70
|
+
: undefined;
|
|
64
71
|
/** Every fixed value of a call's template by its dotted path; a placeholder is not fixed. */
|
|
65
72
|
const fixedOf = (value, trail = []) => {
|
|
66
73
|
if (Array.isArray(value) || isRaw(value))
|
|
@@ -69,7 +76,9 @@ const fixedOf = (value, trail = []) => {
|
|
|
69
76
|
return [];
|
|
70
77
|
return [[trail.join("."), value]];
|
|
71
78
|
};
|
|
72
|
-
const fixedCount = (call) => fixedOf(call.path).length +
|
|
79
|
+
const fixedCount = (call, paging) => fixedOf(call.path).length +
|
|
80
|
+
fixedOf(call.query).filter(([key]) => !paging("query", key)).length +
|
|
81
|
+
fixedOf(call.body).filter(([key]) => !paging("body", key)).length;
|
|
73
82
|
/**
|
|
74
83
|
* A CRM service in memory, built from a source manifest and the generated API it names: rows sampled from each
|
|
75
84
|
* operation's answer schema with distinct ids and the values the map reads, paged by each operation's own pagination,
|
|
@@ -109,8 +118,9 @@ export const crmManifestFake = (input, options) => {
|
|
|
109
118
|
const typed = (node, value) => {
|
|
110
119
|
const found = scalarOf(node);
|
|
111
120
|
const type = found?.type ?? "string";
|
|
121
|
+
// A place whose schema the walk cannot name keeps a flag a flag: the strict fake still checks it.
|
|
112
122
|
if (typeof value === "boolean")
|
|
113
|
-
return type === "boolean" ? value : undefined;
|
|
123
|
+
return type === "boolean" || found === undefined ? value : undefined;
|
|
114
124
|
if (typeof value === "object")
|
|
115
125
|
return type === "integer" || type === "number" ? value.ms : new Date(value.ms).toISOString();
|
|
116
126
|
if (type === "integer" || type === "number")
|
|
@@ -123,24 +133,29 @@ export const crmManifestFake = (input, options) => {
|
|
|
123
133
|
* The schema with each union on the way to `keys` cut to the branch that has the rest of the path: a sample of the
|
|
124
134
|
* first branch of Attio's seventeen value types would carry another type's fields, and the strict fake refuses it.
|
|
125
135
|
*/
|
|
126
|
-
const narrowed = (node, keys) => {
|
|
136
|
+
const narrowed = (node, keys, value) => {
|
|
127
137
|
if (keys.length === 0)
|
|
128
138
|
return node;
|
|
129
139
|
const found = resolve(node, definitions);
|
|
130
140
|
const [key, ...rest] = keys;
|
|
131
|
-
|
|
141
|
+
// A branch fits when it has the path, and, with a value to write, takes it there: Attio's `value` is text in one
|
|
142
|
+
// branch and a flag in another.
|
|
143
|
+
const fits = (one) => {
|
|
144
|
+
const leaf = walk(one, keys.join("."), definitions);
|
|
145
|
+
return "node" in leaf && (value === undefined || typed(leaf.node, value) !== undefined);
|
|
146
|
+
};
|
|
132
147
|
switch (found.type) {
|
|
133
148
|
case "union": {
|
|
134
|
-
const branch = found.of.find(fits);
|
|
135
|
-
return branch ? narrowed(branch, keys) : node;
|
|
149
|
+
const branch = found.of.find(fits) ?? found.of.find((one) => "node" in walk(one, keys.join("."), definitions));
|
|
150
|
+
return branch ? narrowed(branch, keys, value) : node;
|
|
136
151
|
}
|
|
137
152
|
case "allOf":
|
|
138
|
-
return { ...found, of: found.of.map((one) => (fits(one) ? narrowed(one, keys) : one)) };
|
|
153
|
+
return { ...found, of: found.of.map((one) => (fits(one) ? narrowed(one, keys, value) : one)) };
|
|
139
154
|
case "array":
|
|
140
|
-
return /^\d+$/.test(key) ? { ...found, items: narrowed(found.items, rest) } : node;
|
|
155
|
+
return /^\d+$/.test(key) ? { ...found, items: narrowed(found.items, rest, value) } : node;
|
|
141
156
|
case "object": {
|
|
142
157
|
const inner = found.properties[key] ?? found.additionalProperties;
|
|
143
|
-
return inner ? { ...found, properties: { ...found.properties, [key]: narrowed(inner, rest) } } : node;
|
|
158
|
+
return inner ? { ...found, properties: { ...found.properties, [key]: narrowed(inner, rest, value) } } : node;
|
|
144
159
|
}
|
|
145
160
|
default:
|
|
146
161
|
return node;
|
|
@@ -195,34 +210,35 @@ export const crmManifestFake = (input, options) => {
|
|
|
195
210
|
}
|
|
196
211
|
};
|
|
197
212
|
const rowOf = (node, rules, types, n, given) => {
|
|
198
|
-
|
|
199
|
-
const paths = Object.values(rules).flatMap((rule) => pathOf(rule) ?? []);
|
|
200
|
-
const made = sample(node && paths.reduce((cut, path) => narrowed(cut, path.split(".")), node));
|
|
201
|
-
const row = isRaw(made) ? made : {};
|
|
202
|
-
for (const [field, rule] of Object.entries(rules)) {
|
|
203
|
-
const path = pathOf(rule);
|
|
204
|
-
if (path === undefined)
|
|
205
|
-
continue;
|
|
213
|
+
const sampledValue = (field, rule) => {
|
|
206
214
|
const mapped = typeof rule === "object" && "map" in rule ? Object.keys(rule.map)[0] : undefined;
|
|
207
215
|
const reverse = typeof rule === "object" && "map" in rule && field in given
|
|
208
216
|
? Object.keys(rule.map).find((key) => String(rule.map[key]) === String(given[field]))
|
|
209
217
|
: undefined;
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
218
|
+
if (reverse !== undefined)
|
|
219
|
+
return reverse;
|
|
220
|
+
if (field in given)
|
|
221
|
+
return given[field];
|
|
222
|
+
if (mapped !== undefined)
|
|
223
|
+
return mapped;
|
|
224
|
+
if (types[field] === "text")
|
|
225
|
+
return `${field} ${n + 1}`;
|
|
226
|
+
if (types[field] === "epoch")
|
|
227
|
+
return { ms: SEEDED + (n + 1) * 60_000 };
|
|
228
|
+
return types[field] === "day" ? `2026-11-0${n + 1}` : undefined;
|
|
229
|
+
};
|
|
230
|
+
const writes = Object.entries(rules).flatMap(([field, rule]) => {
|
|
231
|
+
const path = pathOf(rule);
|
|
232
|
+
return path === undefined ? [] : [{ path, value: sampledValue(field, rule) }];
|
|
233
|
+
});
|
|
234
|
+
// Sampled from the schema cut to the branches the rules write into, each one that takes its value: Attio's
|
|
235
|
+
// `value` is text in one branch and a flag in another, and a list the first sample holds must have the right type.
|
|
236
|
+
const cut = node && writes.reduce((one, { path, value }) => narrowed(one, path.split("."), value), node);
|
|
237
|
+
const made = sample(cut);
|
|
238
|
+
const row = isRaw(made) ? made : {};
|
|
239
|
+
for (const { path, value } of writes)
|
|
223
240
|
if (value !== undefined)
|
|
224
|
-
put(row, path, value,
|
|
225
|
-
}
|
|
241
|
+
put(row, path, value, cut);
|
|
226
242
|
return row;
|
|
227
243
|
};
|
|
228
244
|
const userSpec = manifest.users;
|
|
@@ -239,7 +255,8 @@ export const crmManifestFake = (input, options) => {
|
|
|
239
255
|
const rule = manifest.entities[kind]?.map.pipeline;
|
|
240
256
|
const id = typeof rule === "object" && "value" in rule ? String(rule.value) : String(9001 + index);
|
|
241
257
|
const node = rowNodeOf(spec.call);
|
|
242
|
-
const
|
|
258
|
+
const flat = spec.stages.each === ".";
|
|
259
|
+
const stageNode = flat ? node : itemOf(nodeAt(node, spec.stages.each), definitions);
|
|
243
260
|
const outcome = spec.stages.outcome;
|
|
244
261
|
const stages = Array.from({ length: ROWS }, (_, n) => {
|
|
245
262
|
const stage = rowOf(stageNode, spec.stages.map, { name: "text" }, n, {
|
|
@@ -248,12 +265,24 @@ export const crmManifestFake = (input, options) => {
|
|
|
248
265
|
});
|
|
249
266
|
const won = typeof outcome === "object" && "path" in outcome ? [outcome.won?.[0], outcome.lost?.[0]][n - 1] : undefined;
|
|
250
267
|
if (won !== undefined && typeof outcome === "object" && "path" in outcome)
|
|
251
|
-
|
|
268
|
+
for (const value of won === "true" || won === "false" ? [won === "true", won] : [won]) {
|
|
269
|
+
// A flag the schema keeps as a boolean takes one; a text field the word.
|
|
270
|
+
put(stage, outcome.path, value, stageNode);
|
|
271
|
+
if (at(stage, outcome.path) === value)
|
|
272
|
+
break;
|
|
273
|
+
}
|
|
274
|
+
if (flat && spec.stages.pipeline)
|
|
275
|
+
put(stage, spec.stages.pipeline, id, stageNode);
|
|
252
276
|
return stage;
|
|
253
277
|
});
|
|
278
|
+
const ids = stages.map((_, n) => `${id}0${n + 1}`);
|
|
279
|
+
if (flat) {
|
|
280
|
+
pipelines.set(kind, { rows: stages, id, stages: ids });
|
|
281
|
+
continue;
|
|
282
|
+
}
|
|
254
283
|
const row = rowOf(node, spec.map, { name: "text" }, 0, { externalId: id });
|
|
255
284
|
putList(row, spec.stages.each, stages, node);
|
|
256
|
-
pipelines.set(kind, { rows: [row], id, stages:
|
|
285
|
+
pipelines.set(kind, { rows: [row], id, stages: ids });
|
|
257
286
|
}
|
|
258
287
|
const rows = {};
|
|
259
288
|
const changeAt = {};
|
|
@@ -313,6 +342,8 @@ export const crmManifestFake = (input, options) => {
|
|
|
313
342
|
routes.push({ call, kind, rows: all });
|
|
314
343
|
if (entity.get)
|
|
315
344
|
routes.push({ call: entity.get, kind, rows: all, get: placeOf(entity.get, "{id}") });
|
|
345
|
+
if (entity.search)
|
|
346
|
+
routes.push({ call: entity.search, kind, rows: all, query: placeOf(entity.search, "{query}") });
|
|
316
347
|
}
|
|
317
348
|
for (const [kind, one] of pipelines) {
|
|
318
349
|
const spec = manifest.pipelines?.[kind];
|
|
@@ -324,7 +355,13 @@ export const crmManifestFake = (input, options) => {
|
|
|
324
355
|
continue;
|
|
325
356
|
const node = rowNodeOf(spec.call);
|
|
326
357
|
const { options: _, ...map } = spec.map;
|
|
327
|
-
const
|
|
358
|
+
const keep = typeof spec.keep === "object" ? spec.keep : undefined;
|
|
359
|
+
const fields = Array.from({ length: PAGE }, (_, n) => {
|
|
360
|
+
const field = rowOf(node, map, { code: "text", label: "text" }, n, { code: `${kind}_field_${n + 1}` });
|
|
361
|
+
if (keep && typeof keep.is !== "object")
|
|
362
|
+
putList(field, keep.path, keep.is, node);
|
|
363
|
+
return field;
|
|
364
|
+
});
|
|
328
365
|
routes.push({ call: spec.call, rows: () => fields });
|
|
329
366
|
}
|
|
330
367
|
if (manifest.users)
|
|
@@ -334,14 +371,28 @@ export const crmManifestFake = (input, options) => {
|
|
|
334
371
|
own.push(message);
|
|
335
372
|
return { status, body: { message } };
|
|
336
373
|
};
|
|
374
|
+
/** A page's size and place are the engine's to set: a live list asks for its own, whatever the template says. */
|
|
375
|
+
const paging = (operation, where, key) => {
|
|
376
|
+
const pagination = operations.get(operation)?.pagination;
|
|
377
|
+
if (!pagination)
|
|
378
|
+
return false;
|
|
379
|
+
const parameters = [pageParameter(pagination), "limit" in pagination ? pagination.limit : undefined];
|
|
380
|
+
return parameters.some((one) => one?.in === where && one.name === key);
|
|
381
|
+
};
|
|
337
382
|
const matches = (route, call) => route.call.operation === call.operation &&
|
|
338
383
|
fixedOf(route.call.path).every(([key, value]) => call.path[key] === String(value)) &&
|
|
339
|
-
fixedOf(route.call.query).every(([key, value]) => readPlace(call, { in: "query", path: key }) === String(value)) &&
|
|
340
|
-
fixedOf(route.call.body).every(([key, value]) => at(call.body, key) === value);
|
|
341
|
-
/**
|
|
384
|
+
fixedOf(route.call.query).every(([key, value]) => paging(route.call.operation, "query", key) || readPlace(call, { in: "query", path: key }) === String(value)) &&
|
|
385
|
+
fixedOf(route.call.body).every(([key, value]) => paging(route.call.operation, "body", key) || at(call.body, key) === value);
|
|
386
|
+
/**
|
|
387
|
+
* The route whose template the call matches most closely; a list wins a tie with a change read, and a search a tie
|
|
388
|
+
* with a list only when its words were sent.
|
|
389
|
+
*/
|
|
342
390
|
const routeOf = (call) => routes
|
|
343
|
-
.filter((route) => matches(route, call))
|
|
344
|
-
.sort((a, b) => fixedCount(b.call)
|
|
391
|
+
.filter((route) => matches(route, call) && (!route.query || readPlace(call, route.query) !== undefined))
|
|
392
|
+
.sort((a, b) => fixedCount(b.call, (where, key) => paging(b.call.operation, where, key)) -
|
|
393
|
+
fixedCount(a.call, (where, key) => paging(a.call.operation, where, key)) ||
|
|
394
|
+
Number(!!b.query) - Number(!!a.query) ||
|
|
395
|
+
Number(!!a.since) - Number(!!b.since))[0];
|
|
345
396
|
const page = (route, call, listed) => {
|
|
346
397
|
const operation = operations.get(route.call.operation);
|
|
347
398
|
const pagination = operation?.pagination;
|
|
@@ -429,6 +480,10 @@ export const crmManifestFake = (input, options) => {
|
|
|
429
480
|
.sort((a, b) => (timeAt(a) ?? 0) - (timeAt(b) ?? 0));
|
|
430
481
|
return page(changes, call, listed);
|
|
431
482
|
}
|
|
483
|
+
if (route.query) {
|
|
484
|
+
const words = String(readPlace(call, route.query)).toLowerCase();
|
|
485
|
+
return page(route, call, route.rows().filter((row) => JSON.stringify(row).toLowerCase().includes(words)));
|
|
486
|
+
}
|
|
432
487
|
return page(route, call, route.rows());
|
|
433
488
|
};
|
|
434
489
|
const fake = fakeApi({
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HTML as Markdown (CommonMark + GFM tables): a WordPress post's `content.rendered` or its raw `content` with block
|
|
3
|
+
* comments, a Ghost post's `html`. Comments, scripts, styles, frames and event attributes are dropped, and so are links
|
|
4
|
+
* and images with a scheme other than http(s), mailto and tel.
|
|
5
|
+
*/
|
|
6
|
+
export declare const htmlToMarkdown: (html: string) => Promise<string>;
|
|
7
|
+
/**
|
|
8
|
+
* Markdown (CommonMark + GFM) as HTML for a service that stores HTML, such as WordPress or Ghost. Raw HTML in the
|
|
9
|
+
* Markdown is not passed through, and links and images with an unsafe scheme are dropped, as `htmlToMarkdown` does.
|
|
10
|
+
*/
|
|
11
|
+
export declare const markdownToHtml: (markdown: string) => Promise<string>;
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
// Loaded on first use, as markdown-blocks.ts loads its parser: most commands never convert a body.
|
|
2
|
+
let converters;
|
|
3
|
+
const loaded = () => (converters ??= importAll());
|
|
4
|
+
const importAll = async () => {
|
|
5
|
+
const [fromHtml, toMdast, toMarkdown, fromMarkdown, gfmMdast, gfmMicromark, toHast, toHtml] = await Promise.all([
|
|
6
|
+
import("hast-util-from-html"),
|
|
7
|
+
import("hast-util-to-mdast"),
|
|
8
|
+
import("mdast-util-to-markdown"),
|
|
9
|
+
import("mdast-util-from-markdown"),
|
|
10
|
+
import("mdast-util-gfm"),
|
|
11
|
+
import("micromark-extension-gfm"),
|
|
12
|
+
import("mdast-util-to-hast"),
|
|
13
|
+
import("hast-util-to-html"),
|
|
14
|
+
]);
|
|
15
|
+
return {
|
|
16
|
+
fromHtml: fromHtml.fromHtml,
|
|
17
|
+
toMdast: toMdast.toMdast,
|
|
18
|
+
toMarkdown: toMarkdown.toMarkdown,
|
|
19
|
+
fromMarkdown: fromMarkdown.fromMarkdown,
|
|
20
|
+
gfmToMarkdown: gfmMdast.gfmToMarkdown,
|
|
21
|
+
gfmFromMarkdown: gfmMdast.gfmFromMarkdown,
|
|
22
|
+
gfm: gfmMicromark.gfm,
|
|
23
|
+
toHast: toHast.toHast,
|
|
24
|
+
toHtml: toHtml.toHtml,
|
|
25
|
+
};
|
|
26
|
+
};
|
|
27
|
+
// A post body is untrusted: a link or image that runs code or reads a file must not survive into what a CLI shows
|
|
28
|
+
// or writes back. A URL with no scheme (relative, `#anchor`) stays.
|
|
29
|
+
const SAFE_URL = /^(?:https?:|mailto:|tel:|[^:]*$|[^:]*[/?#])/i;
|
|
30
|
+
const unsafe = (url) => !SAFE_URL.test(url.trim());
|
|
31
|
+
// The first definition of a label wins in CommonMark: dropping only an unsafe first one would hand its references to
|
|
32
|
+
// a later definition the author never meant.
|
|
33
|
+
const unsafeLabels = (node, first = new Map()) => {
|
|
34
|
+
if (node.type === "definition" && !first.has(node.identifier))
|
|
35
|
+
first.set(node.identifier, unsafe(node.url));
|
|
36
|
+
if ("children" in node)
|
|
37
|
+
for (const child of node.children)
|
|
38
|
+
unsafeLabels(child, first);
|
|
39
|
+
return first;
|
|
40
|
+
};
|
|
41
|
+
const safeChildren = (node, labels) => {
|
|
42
|
+
node.children = node.children.flatMap((child) => {
|
|
43
|
+
// Raw HTML, including the comments WordPress and Ghost wrap their blocks and cards in.
|
|
44
|
+
if (child.type === "html")
|
|
45
|
+
return [];
|
|
46
|
+
if (child.type === "definition" && (unsafe(child.url) || labels.get(child.identifier)))
|
|
47
|
+
return [];
|
|
48
|
+
if (child.type === "link" && unsafe(child.url)) {
|
|
49
|
+
safeChildren(child, labels);
|
|
50
|
+
return child.children;
|
|
51
|
+
}
|
|
52
|
+
if (child.type === "image" && unsafe(child.url))
|
|
53
|
+
return child.alt ? [{ type: "text", value: child.alt }] : [];
|
|
54
|
+
if ("children" in child)
|
|
55
|
+
safeChildren(child, labels);
|
|
56
|
+
return [child];
|
|
57
|
+
});
|
|
58
|
+
};
|
|
59
|
+
const makeSafe = (tree) => {
|
|
60
|
+
safeChildren(tree, unsafeLabels(tree));
|
|
61
|
+
return tree;
|
|
62
|
+
};
|
|
63
|
+
/**
|
|
64
|
+
* HTML as Markdown (CommonMark + GFM tables): a WordPress post's `content.rendered` or its raw `content` with block
|
|
65
|
+
* comments, a Ghost post's `html`. Comments, scripts, styles, frames and event attributes are dropped, and so are links
|
|
66
|
+
* and images with a scheme other than http(s), mailto and tel.
|
|
67
|
+
*/
|
|
68
|
+
export const htmlToMarkdown = async (html) => {
|
|
69
|
+
const { fromHtml, toMdast, toMarkdown, gfmToMarkdown } = await loaded();
|
|
70
|
+
const tree = makeSafe(toMdast(fromHtml(html, { fragment: true })));
|
|
71
|
+
return toMarkdown(tree, { extensions: [gfmToMarkdown()] });
|
|
72
|
+
};
|
|
73
|
+
/**
|
|
74
|
+
* Markdown (CommonMark + GFM) as HTML for a service that stores HTML, such as WordPress or Ghost. Raw HTML in the
|
|
75
|
+
* Markdown is not passed through, and links and images with an unsafe scheme are dropped, as `htmlToMarkdown` does.
|
|
76
|
+
*/
|
|
77
|
+
export const markdownToHtml = async (markdown) => {
|
|
78
|
+
const { fromMarkdown, gfmFromMarkdown, gfm, toHast, toHtml } = await loaded();
|
|
79
|
+
const tree = makeSafe(fromMarkdown(markdown, { extensions: [gfm()], mdastExtensions: [gfmFromMarkdown()] }));
|
|
80
|
+
return toHtml(toHast(tree));
|
|
81
|
+
};
|
|
@@ -12,6 +12,7 @@ export { type PushConflict, type PushOptions, type PushResult, planPush, pushDoc
|
|
|
12
12
|
export { type SyncResult, syncDocuments } from "../services/documents/sync.js";
|
|
13
13
|
export { type Block, blockKey, blockShape, type Inline, type ListKind, normalizeInlines } from "./blocks.js";
|
|
14
14
|
export { catalogFiles } from "./catalog.js";
|
|
15
|
+
export { htmlToMarkdown, markdownToHtml } from "./html-markdown.js";
|
|
15
16
|
export { markdownBlocks, type PlacedBlock, placedMarkdownBlocks } from "./markdown-blocks.js";
|
|
16
17
|
export type { DocumentSource, FeedlessDocumentSource, IndexedSource, PictureWriter, SourceChange, SourceContent, SourceFile, SourceIndex, SourceSkip, SourceState, SourceStatePort, SourceWalk, SourceWork, TabText, } from "./model.js";
|
|
17
18
|
export { feedOf } from "./model.js";
|
package/dist/documents/index.js
CHANGED
|
@@ -11,6 +11,7 @@ export { planPush, pushDocuments, scanLocal, } from "../services/documents/push.
|
|
|
11
11
|
export { syncDocuments } from "../services/documents/sync.js";
|
|
12
12
|
export { blockKey, blockShape, normalizeInlines } from "./blocks.js";
|
|
13
13
|
export { catalogFiles } from "./catalog.js";
|
|
14
|
+
export { htmlToMarkdown, markdownToHtml } from "./html-markdown.js";
|
|
14
15
|
export { markdownBlocks, placedMarkdownBlocks } from "./markdown-blocks.js";
|
|
15
16
|
export { feedOf } from "./model.js";
|
|
16
17
|
export { inScope, within } from "./scope.js";
|
|
@@ -56,14 +56,19 @@ export const chunkFreshness = ({ orm }, { conversationPk, firstMessagePk, lastMe
|
|
|
56
56
|
*/
|
|
57
57
|
export const purgeVectorHashes = (context, hashes) => {
|
|
58
58
|
const { orm } = context;
|
|
59
|
-
|
|
59
|
+
const unique = new Set(hashes);
|
|
60
|
+
if (unique.size === 0)
|
|
61
|
+
return;
|
|
62
|
+
const models = orm.all(sql `SELECT DISTINCT model FROM embeddings`).map(({ model }) => model);
|
|
63
|
+
for (const hash of unique) {
|
|
60
64
|
let offset = 0;
|
|
61
65
|
let valid = false;
|
|
62
66
|
for (;;) {
|
|
67
|
+
// `+` keeps SQLite on the hash index: by chunk type it read every conversation chunk for each hash.
|
|
63
68
|
const candidates = orm.all(sql `SELECT k.chunkable_id AS conversation, r.first_message_id AS first,
|
|
64
69
|
r.last_message_id AS last, r.text_start AS start, r.text_end AS end
|
|
65
70
|
FROM chunks k JOIN chunk_messages r ON r.chunk_id = k.id
|
|
66
|
-
JOIN conversations c ON k.chunkable_type = 'conversation' AND c.id = k.chunkable_id
|
|
71
|
+
JOIN conversations c ON +k.chunkable_type = 'conversation' AND c.id = k.chunkable_id
|
|
67
72
|
JOIN conversation_state s ON s.chat_id = c.chat_id AND s.current_build = c.build
|
|
68
73
|
WHERE k.content_hash = ${hash} ORDER BY k.chunkable_id, k.position LIMIT 100 OFFSET ${offset}`);
|
|
69
74
|
valid = candidates.some(({ conversation, first, last, start, end }) => {
|
|
@@ -78,9 +83,11 @@ export const purgeVectorHashes = (context, hashes) => {
|
|
|
78
83
|
break;
|
|
79
84
|
}
|
|
80
85
|
}
|
|
86
|
+
// By the full key, model first: by hash alone each delete read the whole key.
|
|
81
87
|
if (!valid)
|
|
82
|
-
|
|
83
|
-
|
|
88
|
+
for (const model of models)
|
|
89
|
+
orm.run(sql `DELETE FROM embeddings WHERE model = ${model} AND content_hash = ${hash}
|
|
90
|
+
AND NOT EXISTS (SELECT 1 FROM chunks WHERE content_hash = ${hash} AND chunkable_type <> 'conversation')`);
|
|
84
91
|
}
|
|
85
92
|
};
|
|
86
93
|
export const purgeVectorsOf = (context, messagePk) => {
|
|
@@ -68,6 +68,8 @@ export interface TrackerManifestHooks {
|
|
|
68
68
|
}
|
|
69
69
|
/** The selection a call sends, as a tree: its own, else the operation's default; none for an operation over HTTP. */
|
|
70
70
|
export declare const selectionOf: (one: TrackerOperationCall, api: ServiceApiManifest) => Selection | undefined;
|
|
71
|
+
/** The selection of one row: under the operation's `items` (a connection's `nodes`) or the call's own. */
|
|
72
|
+
export declare const rowSelection: (tree: Selection, items: string | undefined) => Selection | undefined;
|
|
71
73
|
/**
|
|
72
74
|
* The manifest checked against its shape and the generated API: each operation exists and reads, a list pages or
|
|
73
75
|
* says where its rows are, a change call places `{since}`, and over GraphQL every path the map reads is selected, so
|
|
@@ -55,7 +55,7 @@ const selects = (tree, steps) => {
|
|
|
55
55
|
return found === true ? rest.every((step) => /^\d+$/.test(step)) : selects(found, rest);
|
|
56
56
|
};
|
|
57
57
|
/** The selection of one row: under the operation's `items` (a connection's `nodes`) or the call's own. */
|
|
58
|
-
const rowSelection = (tree, items) => {
|
|
58
|
+
export const rowSelection = (tree, items) => {
|
|
59
59
|
let inside = tree;
|
|
60
60
|
for (const step of items?.split(".") ?? [])
|
|
61
61
|
inside = inside === true ? undefined : inside?.[step];
|
|
@@ -1,2 +1,3 @@
|
|
|
1
|
+
export { type TrackerManifestFake, type TrackerManifestFakeOptions, type TrackerManifestFixtureOptions, trackerManifestContract, trackerManifestFake, trackerManifestFixture, } from "./manifest-fake.js";
|
|
1
2
|
export { exampleTrackerRows, type FakeTrackerRows, type FakeTrackerSource, type FakeTrackerSourceOptions, fakeTrackerSource, } from "./source.js";
|
|
2
3
|
export { type TrackerSourceFaults, type TrackerSourceFixture, trackerSourceContract, } from "./source-contract.js";
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { type GraphqlFake, type SchemaNode } from "@wirecat/cli-base/codegen";
|
|
2
|
+
import { type ServiceApiManifest } from "../../cli/service-api-manifest.js";
|
|
3
|
+
import { crmValues } from "../../crm/index.js";
|
|
4
|
+
import { type TrackerKind } from "../../domain/tracker.js";
|
|
5
|
+
import type { ContractCase } from "../../meetings/index.js";
|
|
6
|
+
import { type TrackerManifestHooks } from "../manifest/manifest.js";
|
|
7
|
+
import { type TrackerSourceFixture } from "./source-contract.js";
|
|
8
|
+
type Definitions = Readonly<Record<string, SchemaNode>>;
|
|
9
|
+
export interface TrackerManifestFakeOptions {
|
|
10
|
+
/** The generated API manifest the source manifest names its operations in. */
|
|
11
|
+
api: ServiceApiManifest;
|
|
12
|
+
/** The generated schemas: rows are sampled from them, and every call and answer is checked against them. */
|
|
13
|
+
definitions: Definitions;
|
|
14
|
+
/** In place of the API manifest's first server. */
|
|
15
|
+
baseUrl?: string;
|
|
16
|
+
}
|
|
17
|
+
export interface TrackerManifestFake extends GraphqlFake {
|
|
18
|
+
/** The rows of each kind as the tracker holds them, every field any call of the kind selects; a test may change them. */
|
|
19
|
+
rows: Partial<Record<TrackerKind, crmValues.CrmRaw[]>>;
|
|
20
|
+
/** Moves one row's change time to now, for the next read of changes to list it. */
|
|
21
|
+
touch(kind: TrackerKind, id: string): void;
|
|
22
|
+
/** Archives one row now: listed only to a call that asks for archived rows, and read as changed. */
|
|
23
|
+
archive(kind: TrackerKind, id: string): void;
|
|
24
|
+
/** Deletes one row outright. */
|
|
25
|
+
remove(kind: TrackerKind, id: string): void;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* A GraphQL tracker in memory, built from a source manifest and the generated API it names: cli-base's strict
|
|
29
|
+
* `graphqlFake` with three rows of each kind behind it. Each row holds the values the map reads where it reads them,
|
|
30
|
+
* links to rows of the kinds it points at, and is answered within each call's selection, Relay-paged two a page. A
|
|
31
|
+
* change read keeps the rows changed at or after its since, oldest first. A cursor the fake never gave, an unreadable
|
|
32
|
+
* since and a call no route of the manifest makes are refused and recorded.
|
|
33
|
+
*/
|
|
34
|
+
export declare const trackerManifestFake: (input: unknown, options: TrackerManifestFakeOptions) => TrackerManifestFake;
|
|
35
|
+
export interface TrackerManifestFixtureOptions extends TrackerManifestFakeOptions {
|
|
36
|
+
hooks?: TrackerManifestHooks;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* A `trackerSourceContract` fixture for a source built from a manifest, over `trackerManifestFake`: the source, the
|
|
40
|
+
* rows it holds, a dead cursor, the faults the manifest allows, and the fake itself, whose `refusals` a test asserts
|
|
41
|
+
* empty.
|
|
42
|
+
*/
|
|
43
|
+
export declare const trackerManifestFixture: (manifest: unknown, options: TrackerManifestFixtureOptions) => TrackerSourceFixture & {
|
|
44
|
+
fake: TrackerManifestFake;
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* `trackerSourceContract` over `trackerManifestFixture`, each case failing too when the fake refused a call: what a
|
|
48
|
+
* generated test runs, `for (const { name, run } of trackerManifestContract(SOURCE, { api, definitions })) it(name, run)`.
|
|
49
|
+
*/
|
|
50
|
+
export declare const trackerManifestContract: (manifest: unknown, options: TrackerManifestFixtureOptions) => ContractCase[];
|
|
51
|
+
export {};
|