@milaboratories/pl-mcp-server 53.1.0 → 55.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/README.md +30 -37
- package/dist/_virtual/_rolldown/runtime.cjs +22 -0
- package/dist/agent-tables.cjs +116 -0
- package/dist/agent-tables.cjs.map +1 -0
- package/dist/agent-tables.js +115 -0
- package/dist/agent-tables.js.map +1 -0
- package/dist/instructions.cjs +16 -12
- package/dist/instructions.cjs.map +1 -1
- package/dist/instructions.js +16 -12
- package/dist/instructions.js.map +1 -1
- package/dist/package.cjs +1 -1
- package/dist/package.js +1 -1
- package/dist/project-holds.cjs +122 -0
- package/dist/project-holds.cjs.map +1 -0
- package/dist/project-holds.js +122 -0
- package/dist/project-holds.js.map +1 -0
- package/dist/project-list.cjs +16 -0
- package/dist/project-list.cjs.map +1 -0
- package/dist/project-list.js +16 -0
- package/dist/project-list.js.map +1 -0
- package/dist/server-log.cjs +48 -18
- package/dist/server-log.cjs.map +1 -1
- package/dist/server-log.js +48 -19
- package/dist/server-log.js.map +1 -1
- package/dist/server.cjs +71 -18
- package/dist/server.cjs.map +1 -1
- package/dist/server.d.ts +37 -24
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +71 -19
- package/dist/server.js.map +1 -1
- package/dist/tools/agent-tables.cjs +424 -0
- package/dist/tools/agent-tables.cjs.map +1 -0
- package/dist/tools/agent-tables.js +417 -0
- package/dist/tools/agent-tables.js.map +1 -0
- package/dist/tools/await.cjs +216 -72
- package/dist/tools/await.cjs.map +1 -1
- package/dist/tools/await.js +212 -74
- package/dist/tools/await.js.map +1 -1
- package/dist/tools/block-action-context.cjs +14 -9
- package/dist/tools/block-action-context.cjs.map +1 -1
- package/dist/tools/block-action-context.js +14 -9
- package/dist/tools/block-action-context.js.map +1 -1
- package/dist/tools/block-api.cjs +17 -2
- package/dist/tools/block-api.cjs.map +1 -1
- package/dist/tools/block-api.js +17 -3
- package/dist/tools/block-api.js.map +1 -1
- package/dist/tools/block-params.cjs +275 -6
- package/dist/tools/block-params.cjs.map +1 -1
- package/dist/tools/block-params.js +273 -8
- package/dist/tools/block-params.js.map +1 -1
- package/dist/tools/block-state.cjs +343 -57
- package/dist/tools/block-state.cjs.map +1 -1
- package/dist/tools/block-state.js +337 -61
- package/dist/tools/block-state.js.map +1 -1
- package/dist/tools/block-storage.cjs +153 -0
- package/dist/tools/block-storage.cjs.map +1 -0
- package/dist/tools/block-storage.js +147 -0
- package/dist/tools/block-storage.js.map +1 -0
- package/dist/tools/block-updates.cjs +405 -0
- package/dist/tools/block-updates.cjs.map +1 -0
- package/dist/tools/block-updates.js +397 -0
- package/dist/tools/block-updates.js.map +1 -0
- package/dist/tools/blocks.cjs +199 -76
- package/dist/tools/blocks.cjs.map +1 -1
- package/dist/tools/blocks.js +193 -76
- package/dist/tools/blocks.js.map +1 -1
- package/dist/tools/data-query.cjs +470 -101
- package/dist/tools/data-query.cjs.map +1 -1
- package/dist/tools/data-query.js +465 -106
- package/dist/tools/data-query.js.map +1 -1
- package/dist/tools/errors.cjs +21 -0
- package/dist/tools/errors.cjs.map +1 -0
- package/dist/tools/errors.js +20 -0
- package/dist/tools/errors.js.map +1 -0
- package/dist/tools/files.cjs +643 -0
- package/dist/tools/files.cjs.map +1 -0
- package/dist/tools/files.js +613 -0
- package/dist/tools/files.js.map +1 -0
- package/dist/tools/ids.cjs +72 -0
- package/dist/tools/ids.cjs.map +1 -1
- package/dist/tools/ids.js +63 -2
- package/dist/tools/ids.js.map +1 -1
- package/dist/tools/imports.cjs +195 -0
- package/dist/tools/imports.cjs.map +1 -0
- package/dist/tools/imports.js +186 -0
- package/dist/tools/imports.js.map +1 -0
- package/dist/tools/logs.cjs +111 -30
- package/dist/tools/logs.cjs.map +1 -1
- package/dist/tools/logs.js +108 -31
- package/dist/tools/logs.js.map +1 -1
- package/dist/tools/paging.cjs +27 -0
- package/dist/tools/paging.cjs.map +1 -0
- package/dist/tools/paging.js +26 -0
- package/dist/tools/paging.js.map +1 -0
- package/dist/tools/params-merge.cjs +106 -0
- package/dist/tools/params-merge.cjs.map +1 -0
- package/dist/tools/params-merge.js +106 -0
- package/dist/tools/params-merge.js.map +1 -0
- package/dist/tools/project-template.cjs +75 -0
- package/dist/tools/project-template.cjs.map +1 -0
- package/dist/tools/project-template.js +72 -0
- package/dist/tools/project-template.js.map +1 -0
- package/dist/tools/projects.cjs +68 -38
- package/dist/tools/projects.cjs.map +1 -1
- package/dist/tools/projects.js +69 -40
- package/dist/tools/projects.js.map +1 -1
- package/dist/tools/readiness.cjs +104 -0
- package/dist/tools/readiness.cjs.map +1 -0
- package/dist/tools/readiness.js +99 -0
- package/dist/tools/readiness.js.map +1 -0
- package/dist/tools/result-pool.cjs +260 -0
- package/dist/tools/result-pool.cjs.map +1 -0
- package/dist/tools/result-pool.js +252 -0
- package/dist/tools/result-pool.js.map +1 -0
- package/dist/tools/sandbox.cjs +112 -14
- package/dist/tools/sandbox.cjs.map +1 -1
- package/dist/tools/sandbox.js +110 -15
- package/dist/tools/sandbox.js.map +1 -1
- package/dist/tools/sdk-types.cjs +24 -0
- package/dist/tools/sdk-types.cjs.map +1 -0
- package/dist/tools/sdk-types.js +24 -0
- package/dist/tools/sdk-types.js.map +1 -0
- package/dist/tools/settle.cjs +155 -0
- package/dist/tools/settle.cjs.map +1 -0
- package/dist/tools/settle.js +148 -0
- package/dist/tools/settle.js.map +1 -0
- package/dist/tools/tokens.cjs +49 -8
- package/dist/tools/tokens.cjs.map +1 -1
- package/dist/tools/tokens.js +45 -9
- package/dist/tools/tokens.js.map +1 -1
- package/dist/tools/types.cjs +62 -11
- package/dist/tools/types.cjs.map +1 -1
- package/dist/tools/types.js +60 -12
- package/dist/tools/types.js.map +1 -1
- package/dist/tools/value-outline.cjs +119 -0
- package/dist/tools/value-outline.cjs.map +1 -0
- package/dist/tools/value-outline.js +117 -0
- package/dist/tools/value-outline.js.map +1 -0
- package/package.json +9 -8
- package/src/agent-tables.test.ts +246 -0
- package/src/agent-tables.ts +145 -0
- package/src/instructions.ts +16 -12
- package/src/project-holds.test.ts +362 -0
- package/src/project-holds.ts +207 -0
- package/src/project-list.test.ts +38 -0
- package/src/project-list.ts +25 -0
- package/src/server-log.test.ts +71 -6
- package/src/server-log.ts +58 -27
- package/src/server.logging.test.ts +46 -51
- package/src/server.test.ts +166 -22
- package/src/server.ts +97 -37
- package/src/tools/agent-tables.test.ts +650 -0
- package/src/tools/agent-tables.ts +673 -0
- package/src/tools/await.test.ts +287 -0
- package/src/tools/await.ts +393 -122
- package/src/tools/block-action-context.ts +17 -13
- package/src/tools/block-api.ts +19 -4
- package/src/tools/block-params.test.ts +828 -0
- package/src/tools/block-params.ts +626 -10
- package/src/tools/block-state.test.ts +480 -0
- package/src/tools/block-state.ts +678 -83
- package/src/tools/block-storage.test.ts +270 -0
- package/src/tools/block-storage.ts +195 -0
- package/src/tools/block-updates.test.ts +605 -0
- package/src/tools/block-updates.ts +751 -0
- package/src/tools/blocks.test.ts +169 -22
- package/src/tools/blocks.ts +354 -113
- package/src/tools/data-query.test.ts +641 -0
- package/src/tools/data-query.ts +839 -145
- package/src/tools/errors.test.ts +33 -0
- package/src/tools/errors.ts +23 -0
- package/src/tools/files.test.ts +842 -0
- package/src/tools/files.ts +990 -0
- package/src/tools/ids.test.ts +87 -0
- package/src/tools/ids.ts +89 -1
- package/src/tools/imports.ts +304 -0
- package/src/tools/logs.test.ts +70 -0
- package/src/tools/logs.ts +154 -44
- package/src/tools/paging.test.ts +39 -0
- package/src/tools/paging.ts +39 -0
- package/src/tools/params-merge.test.ts +274 -0
- package/src/tools/params-merge.ts +149 -0
- package/src/tools/project-template.test.ts +230 -0
- package/src/tools/project-template.ts +135 -0
- package/src/tools/projects.test.ts +122 -0
- package/src/tools/projects.ts +136 -48
- package/src/tools/readiness.test.ts +195 -0
- package/src/tools/readiness.ts +189 -0
- package/src/tools/result-pool.test.ts +377 -0
- package/src/tools/result-pool.ts +483 -0
- package/src/tools/sandbox.test.ts +128 -10
- package/src/tools/sandbox.ts +132 -18
- package/src/tools/sdk-types.ts +54 -0
- package/src/tools/settle.test.ts +340 -0
- package/src/tools/settle.ts +312 -0
- package/src/tools/tokens.ts +74 -9
- package/src/tools/types.ts +84 -12
- package/src/tools/value-outline.ts +143 -0
- package/dist/tools/screenshot.cjs +0 -44
- package/dist/tools/screenshot.cjs.map +0 -1
- package/dist/tools/screenshot.js +0 -44
- package/dist/tools/screenshot.js.map +0 -1
- package/dist/tools/ui-interaction.cjs +0 -140
- package/dist/tools/ui-interaction.cjs.map +0 -1
- package/dist/tools/ui-interaction.js +0 -140
- package/dist/tools/ui-interaction.js.map +0 -1
- package/src/tools/screenshot.ts +0 -61
- package/src/tools/ui-interaction.ts +0 -184
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import { isDeepStrictEqual } from "node:util";
|
|
2
|
+
import { isPlainObject } from "@milaboratories/helpers";
|
|
3
|
+
import { childPath, ownValue } from "./block-storage";
|
|
4
|
+
|
|
5
|
+
/** The outcome of {@link mergeThreeWay}. */
|
|
6
|
+
export type MergeOutcome = {
|
|
7
|
+
/** The merged value; meaningless when there are conflicts. */
|
|
8
|
+
merged: unknown;
|
|
9
|
+
/** The paths where base, theirs and ours all differ, in the form of reported changes. */
|
|
10
|
+
conflicts: string[];
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
/** A key, or an element, a version does not have. */
|
|
14
|
+
const ABSENT: unique symbol = Symbol("absent");
|
|
15
|
+
type Slot = unknown;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Three-way merge of a block's data for set_block_params (D-040, Q-047): `base` is the data the
|
|
19
|
+
* block's model builds from the current params, `theirs` the data it builds from the new params,
|
|
20
|
+
* `ours` the block's current data.
|
|
21
|
+
*
|
|
22
|
+
* Where `theirs` differs from `base`, the params changed that part, and `theirs` is taken;
|
|
23
|
+
* elsewhere `ours` is kept, so view state and everything params do not drive stays. Objects merge
|
|
24
|
+
* key by key. Arrays of objects that all carry a unique `id` (string or number) merge element by
|
|
25
|
+
* element, matched by `id`: an element follows its id wherever the new params move it. The new
|
|
26
|
+
* params own such an array's membership and order: an element they drop goes even when `ours`
|
|
27
|
+
* changed it (its per-element state, e.g. the per-sample values of a metadata column, goes with
|
|
28
|
+
* it), an element only `ours` has is kept after its neighbour. Other arrays and values merge as
|
|
29
|
+
* whole values.
|
|
30
|
+
*
|
|
31
|
+
* A conflict is a path where all three differ and neither rule applies; the merge then reports
|
|
32
|
+
* every such path and its `merged` is not to be written.
|
|
33
|
+
*/
|
|
34
|
+
export function mergeThreeWay(base: unknown, theirs: unknown, ours: unknown): MergeOutcome {
|
|
35
|
+
const conflicts: string[] = [];
|
|
36
|
+
const merged = mergeAt("", base, theirs, ours, conflicts);
|
|
37
|
+
return { merged: merged === ABSENT ? undefined : merged, conflicts };
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function same(a: Slot, b: Slot): boolean {
|
|
41
|
+
if (a === ABSENT || b === ABSENT) return a === b;
|
|
42
|
+
return isDeepStrictEqual(a, b);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function mergeAt(path: string, base: Slot, theirs: Slot, ours: Slot, conflicts: string[]): Slot {
|
|
46
|
+
if (same(base, theirs)) return ours;
|
|
47
|
+
if (same(base, ours) || same(theirs, ours)) return theirs;
|
|
48
|
+
if (isPlainObject(base) && isPlainObject(theirs) && isPlainObject(ours))
|
|
49
|
+
return mergeRecords(path, base, theirs, ours, conflicts);
|
|
50
|
+
if (Array.isArray(base) && Array.isArray(theirs) && Array.isArray(ours)) {
|
|
51
|
+
const keyed = [base, theirs, ours].map(byId);
|
|
52
|
+
const [b, t, o] = keyed;
|
|
53
|
+
if (b !== undefined && t !== undefined && o !== undefined)
|
|
54
|
+
return mergeById(path, b, t, o, conflicts);
|
|
55
|
+
}
|
|
56
|
+
conflicts.push(path === "" ? "(root)" : path);
|
|
57
|
+
return ours;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** The value of `key` in `obj`, or {@link ABSENT}: a key holding `undefined` is no key in JSON. */
|
|
61
|
+
function slotOf(obj: Record<string, unknown>, key: string): Slot {
|
|
62
|
+
const value = ownValue(obj, key);
|
|
63
|
+
return value === undefined ? ABSENT : value;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function mergeRecords(
|
|
67
|
+
path: string,
|
|
68
|
+
base: Record<string, unknown>,
|
|
69
|
+
theirs: Record<string, unknown>,
|
|
70
|
+
ours: Record<string, unknown>,
|
|
71
|
+
conflicts: string[],
|
|
72
|
+
): Record<string, unknown> {
|
|
73
|
+
// Ours' key order, then the keys the new params add.
|
|
74
|
+
const keys = [
|
|
75
|
+
...Object.keys(ours),
|
|
76
|
+
...Object.keys(theirs).filter((k) => !Object.hasOwn(ours, k)),
|
|
77
|
+
];
|
|
78
|
+
const entries: [string, unknown][] = [];
|
|
79
|
+
for (const key of keys) {
|
|
80
|
+
const value = mergeAt(
|
|
81
|
+
childPath(path, key),
|
|
82
|
+
slotOf(base, key),
|
|
83
|
+
slotOf(theirs, key),
|
|
84
|
+
slotOf(ours, key),
|
|
85
|
+
conflicts,
|
|
86
|
+
);
|
|
87
|
+
if (value !== ABSENT) entries.push([key, value]);
|
|
88
|
+
}
|
|
89
|
+
// `Object.fromEntries` defines own keys, so a key such as `__proto__` stays a key.
|
|
90
|
+
return Object.fromEntries(entries);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** An array of objects keyed by their `id`: ids in array order and the element of each. */
|
|
94
|
+
type Keyed = { order: string[]; ids: Map<string, unknown>; elements: Map<string, unknown> };
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* `xs` keyed by the `id` of its elements, or `undefined` when an element is not an object, has no
|
|
98
|
+
* string or numeric `id`, or shares its id with another. The key tells `1` from `"1"`.
|
|
99
|
+
*/
|
|
100
|
+
function byId(xs: unknown[]): Keyed | undefined {
|
|
101
|
+
const keyed: Keyed = { order: [], ids: new Map(), elements: new Map() };
|
|
102
|
+
for (const x of xs) {
|
|
103
|
+
if (!isPlainObject(x)) return undefined;
|
|
104
|
+
const id = ownValue(x, "id");
|
|
105
|
+
if (typeof id !== "string" && typeof id !== "number") return undefined;
|
|
106
|
+
const key = `${typeof id}:${id}`;
|
|
107
|
+
if (keyed.elements.has(key)) return undefined;
|
|
108
|
+
keyed.order.push(key);
|
|
109
|
+
keyed.ids.set(key, id);
|
|
110
|
+
keyed.elements.set(key, x);
|
|
111
|
+
}
|
|
112
|
+
return keyed;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function mergeById(
|
|
116
|
+
path: string,
|
|
117
|
+
base: Keyed,
|
|
118
|
+
theirs: Keyed,
|
|
119
|
+
ours: Keyed,
|
|
120
|
+
conflicts: string[],
|
|
121
|
+
): unknown[] {
|
|
122
|
+
const result = new Map<string, unknown>();
|
|
123
|
+
for (const key of new Set([...theirs.order, ...ours.order])) {
|
|
124
|
+
const inTheirs = theirs.elements.has(key);
|
|
125
|
+
// Dropped by the new params: the element goes, whatever ours holds in it.
|
|
126
|
+
if (!inTheirs && base.elements.has(key)) continue;
|
|
127
|
+
const id = theirs.ids.get(key) ?? ours.ids.get(key);
|
|
128
|
+
const value = mergeAt(
|
|
129
|
+
`${path}[id=${JSON.stringify(id)}]`,
|
|
130
|
+
base.elements.get(key) ?? ABSENT,
|
|
131
|
+
inTheirs ? theirs.elements.get(key) : ABSENT,
|
|
132
|
+
ours.elements.get(key) ?? ABSENT,
|
|
133
|
+
conflicts,
|
|
134
|
+
);
|
|
135
|
+
if (value !== ABSENT) result.set(key, value);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// Theirs' order; an element only ours has goes after the element before it in ours.
|
|
139
|
+
const order = theirs.order.filter((key) => result.has(key));
|
|
140
|
+
ours.order.forEach((key, i) => {
|
|
141
|
+
if (!result.has(key) || order.includes(key)) return;
|
|
142
|
+
const before = ours.order
|
|
143
|
+
.slice(0, i)
|
|
144
|
+
.reverse()
|
|
145
|
+
.find((k) => order.includes(k));
|
|
146
|
+
order.splice(before === undefined ? 0 : order.indexOf(before) + 1, 0, key);
|
|
147
|
+
});
|
|
148
|
+
return order.map((key) => result.get(key));
|
|
149
|
+
}
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
|
|
2
|
+
import { tmpdir } from "node:os";
|
|
3
|
+
import * as path from "node:path";
|
|
4
|
+
import { afterEach, beforeEach, describe, expect, test } from "vitest";
|
|
5
|
+
import type {
|
|
6
|
+
ProjectTemplateExportOutcome,
|
|
7
|
+
ProjectTemplateV1,
|
|
8
|
+
} from "@milaboratories/pl-middle-layer";
|
|
9
|
+
import { asProjectId, type BlockKindSelectorReference } from "@milaboratories/pl-model-common";
|
|
10
|
+
import type { ProjectId } from "@milaboratories/pl-middle-layer";
|
|
11
|
+
import type { ExportProjectTemplateDeps } from "./project-template";
|
|
12
|
+
import { exportProjectTemplate, templateFileHandles } from "./project-template";
|
|
13
|
+
|
|
14
|
+
const projectId = asProjectId("project-1");
|
|
15
|
+
const kind = "@platforma-open/org.numbers.kind@^1.0.0" as BlockKindSelectorReference;
|
|
16
|
+
|
|
17
|
+
function handleOf(prefix: string, data: Record<string, unknown>): string {
|
|
18
|
+
return `${prefix}${encodeURIComponent(JSON.stringify(data))}`;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const uploadHandle = handleOf("upload://upload/", {
|
|
22
|
+
localPath: "/home/user/data/sample_R1.fastq.gz",
|
|
23
|
+
pathSignature: "signature",
|
|
24
|
+
sizeBytes: "1024",
|
|
25
|
+
modificationTime: "1700000000",
|
|
26
|
+
});
|
|
27
|
+
const indexHandle = handleOf("index://index/", { storageId: "library", path: "runs/r2.fastq" });
|
|
28
|
+
|
|
29
|
+
function deps(outcome: ProjectTemplateExportOutcome) {
|
|
30
|
+
const exported: ProjectId[] = [];
|
|
31
|
+
const value: ExportProjectTemplateDeps = {
|
|
32
|
+
resolveProject: async (id) => {
|
|
33
|
+
if (id !== projectId) throw new Error(`Project ${id} not found`);
|
|
34
|
+
return { id };
|
|
35
|
+
},
|
|
36
|
+
exportProjectAsTemplate: async (id) => {
|
|
37
|
+
exported.push(id);
|
|
38
|
+
return outcome;
|
|
39
|
+
},
|
|
40
|
+
};
|
|
41
|
+
return { deps: value, exported };
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function textOf(result: { content: { text: string }[] }): string {
|
|
45
|
+
return result.content[0].text;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const document: ProjectTemplateV1 = {
|
|
49
|
+
schema: "template-v1",
|
|
50
|
+
blocks: [
|
|
51
|
+
{ id: "numbers", kind, params: { numbers: [1, 2] } },
|
|
52
|
+
{
|
|
53
|
+
id: "reads",
|
|
54
|
+
kind,
|
|
55
|
+
params: {
|
|
56
|
+
files: [{ handle: uploadHandle }, { handle: indexHandle }],
|
|
57
|
+
"odd key": uploadHandle,
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
],
|
|
61
|
+
};
|
|
62
|
+
const yaml = "schema: template-v1\nblocks: []\n# 🧬\n";
|
|
63
|
+
|
|
64
|
+
/** A document whose one block holds `n` upload handles, each of a long path. */
|
|
65
|
+
function documentWithHandles(n: number): ProjectTemplateV1 {
|
|
66
|
+
const files = Array.from({ length: n }, (_, i) => ({
|
|
67
|
+
handle: handleOf("upload://upload/", {
|
|
68
|
+
localPath: `/home/user/a/rather/long/folder/name/for/the/run/${"d".repeat(60)}/s${i}.fastq.gz`,
|
|
69
|
+
pathSignature: "signature".repeat(4),
|
|
70
|
+
sizeBytes: "1024",
|
|
71
|
+
modificationTime: "1700000000",
|
|
72
|
+
}),
|
|
73
|
+
}));
|
|
74
|
+
return { schema: "template-v1", blocks: [{ id: "reads", kind, params: { files } }] };
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
let dir: string;
|
|
78
|
+
beforeEach(async () => {
|
|
79
|
+
dir = await mkdtemp(path.join(tmpdir(), "export-template-"));
|
|
80
|
+
});
|
|
81
|
+
afterEach(async () => {
|
|
82
|
+
await rm(dir, { recursive: true, force: true });
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
describe("export_project_template", () => {
|
|
86
|
+
test("writes the YAML the middle layer wrote, unchanged, and lists the file handles", async () => {
|
|
87
|
+
const { deps: d, exported } = deps({ ok: true, yaml, document });
|
|
88
|
+
const savePath = path.join(dir, "project.yaml");
|
|
89
|
+
|
|
90
|
+
const result = await exportProjectTemplate(d, { projectId, savePath });
|
|
91
|
+
|
|
92
|
+
expect("isError" in result).toBe(false);
|
|
93
|
+
expect(exported).toEqual([projectId]);
|
|
94
|
+
expect(await readFile(savePath, "utf8")).toBe(yaml);
|
|
95
|
+
expect(JSON.parse(textOf(result))).toEqual({
|
|
96
|
+
path: savePath,
|
|
97
|
+
bytes: Buffer.byteLength(yaml, "utf8"),
|
|
98
|
+
fileHandlesTotal: 3,
|
|
99
|
+
offset: 0,
|
|
100
|
+
hasMore: false,
|
|
101
|
+
fileHandles: [
|
|
102
|
+
{
|
|
103
|
+
blockId: "reads",
|
|
104
|
+
paramsPath: "files[0].handle",
|
|
105
|
+
handle: uploadHandle,
|
|
106
|
+
localPath: "/home/user/data/sample_R1.fastq.gz",
|
|
107
|
+
},
|
|
108
|
+
{
|
|
109
|
+
blockId: "reads",
|
|
110
|
+
paramsPath: "files[1].handle",
|
|
111
|
+
handle: indexHandle,
|
|
112
|
+
storage: "library",
|
|
113
|
+
storagePath: "runs/r2.fastq",
|
|
114
|
+
},
|
|
115
|
+
{
|
|
116
|
+
blockId: "reads",
|
|
117
|
+
paramsPath: '["odd key"]',
|
|
118
|
+
handle: uploadHandle,
|
|
119
|
+
localPath: "/home/user/data/sample_R1.fastq.gz",
|
|
120
|
+
},
|
|
121
|
+
],
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
test("a project with no file handles lists none", async () => {
|
|
126
|
+
const { deps: d } = deps({
|
|
127
|
+
ok: true,
|
|
128
|
+
yaml,
|
|
129
|
+
document: { schema: "template-v1", blocks: [document.blocks[0]] },
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
const result = await exportProjectTemplate(d, { projectId });
|
|
133
|
+
|
|
134
|
+
expect(JSON.parse(textOf(result))).toEqual({
|
|
135
|
+
fileHandlesTotal: 0,
|
|
136
|
+
offset: 0,
|
|
137
|
+
hasMore: false,
|
|
138
|
+
fileHandles: [],
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
test("300 file handles come page by page", async () => {
|
|
143
|
+
const { deps: d } = deps({ ok: true, yaml, document: documentWithHandles(300) });
|
|
144
|
+
|
|
145
|
+
const listed: string[] = [];
|
|
146
|
+
for (let offset = 0, more = true; more;) {
|
|
147
|
+
const answer = JSON.parse(textOf(await exportProjectTemplate(d, { projectId, offset })));
|
|
148
|
+
expect(answer.fileHandlesTotal).toBe(300);
|
|
149
|
+
listed.push(...answer.fileHandles.map((h: { paramsPath: string }) => h.paramsPath));
|
|
150
|
+
offset += answer.fileHandles.length;
|
|
151
|
+
more = answer.hasMore;
|
|
152
|
+
}
|
|
153
|
+
expect(listed).toEqual(Array.from({ length: 300 }, (_, i) => `files[${i}].handle`));
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
test("never overwrites a file, and refuses a relative path", async () => {
|
|
157
|
+
const { deps: d, exported } = deps({ ok: true, yaml, document });
|
|
158
|
+
const savePath = path.join(dir, "taken.yaml");
|
|
159
|
+
await writeFile(savePath, "keep me");
|
|
160
|
+
|
|
161
|
+
const taken = await exportProjectTemplate(d, { projectId, savePath });
|
|
162
|
+
expect(taken).toMatchObject({ isError: true });
|
|
163
|
+
expect(textOf(taken)).toContain("Could not write the template");
|
|
164
|
+
expect(await readFile(savePath, "utf8")).toBe("keep me");
|
|
165
|
+
|
|
166
|
+
const relative = await exportProjectTemplate(d, { projectId, savePath: "project.yaml" });
|
|
167
|
+
expect(relative).toMatchObject({ isError: true });
|
|
168
|
+
expect(textOf(relative)).toContain("Not an absolute path: project.yaml");
|
|
169
|
+
expect(exported).toEqual([projectId]);
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
test("refuses with every problem, each with the block's label and id, and exports nothing", async () => {
|
|
173
|
+
const { deps: d } = deps({
|
|
174
|
+
ok: false,
|
|
175
|
+
problems: [
|
|
176
|
+
{
|
|
177
|
+
blockId: "b1",
|
|
178
|
+
blockLabel: "Old Block",
|
|
179
|
+
error:
|
|
180
|
+
"This version of the block cannot be written to a template. Update the block to a " +
|
|
181
|
+
"newer version and export again.",
|
|
182
|
+
},
|
|
183
|
+
{ blockId: "b2", blockLabel: "No Kind", error: "Block declares no kind" },
|
|
184
|
+
],
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
const savePath = path.join(dir, "project.yaml");
|
|
188
|
+
const result = await exportProjectTemplate(d, { projectId, savePath });
|
|
189
|
+
|
|
190
|
+
expect(result).toMatchObject({ isError: true });
|
|
191
|
+
await expect(readFile(savePath)).rejects.toThrow();
|
|
192
|
+
const text = textOf(result);
|
|
193
|
+
expect(text).toMatch(/^Nothing was exported: 2 blocks of project project-1 cannot be written/);
|
|
194
|
+
expect(text).toContain(
|
|
195
|
+
'- "Old Block" (b1): This version of the block cannot be written to a template.',
|
|
196
|
+
);
|
|
197
|
+
expect(text).toContain('- "No Kind" (b2): Block declares no kind');
|
|
198
|
+
expect(text).toContain("update_block");
|
|
199
|
+
expect(text).not.toContain("yaml");
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
test("an unknown project is refused before anything is exported", async () => {
|
|
203
|
+
const { deps: d, exported } = deps({ ok: true, yaml, document });
|
|
204
|
+
|
|
205
|
+
await expect(exportProjectTemplate(d, { projectId: asProjectId("nope") })).rejects.toThrow(
|
|
206
|
+
"Project nope not found",
|
|
207
|
+
);
|
|
208
|
+
expect(exported).toEqual([]);
|
|
209
|
+
});
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
describe("templateFileHandles", () => {
|
|
213
|
+
test("reports a handle that does not parse, with where it is", () => {
|
|
214
|
+
const broken = "upload://upload/%7Bnot-json";
|
|
215
|
+
expect(templateFileHandles([{ id: "b", params: { inputHandle: broken } }])).toEqual([
|
|
216
|
+
{
|
|
217
|
+
blockId: "b",
|
|
218
|
+
paramsPath: "inputHandle",
|
|
219
|
+
handle: broken,
|
|
220
|
+
error: "The handle does not parse.",
|
|
221
|
+
},
|
|
222
|
+
]);
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
test("ignores strings that are not import handles", () => {
|
|
226
|
+
expect(
|
|
227
|
+
templateFileHandles([{ id: "b", params: { note: "upload later", url: "https://x/y" } }]),
|
|
228
|
+
).toEqual([]);
|
|
229
|
+
});
|
|
230
|
+
});
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { writeFile } from "node:fs/promises";
|
|
2
|
+
import * as path from "node:path";
|
|
3
|
+
import type {
|
|
4
|
+
ProjectId,
|
|
5
|
+
ProjectTemplateExportOutcome,
|
|
6
|
+
TemplateExportProblem,
|
|
7
|
+
} from "@milaboratories/pl-middle-layer";
|
|
8
|
+
import { parseIndexHandle, parseUploadHandle } from "@milaboratories/pl-middle-layer";
|
|
9
|
+
import {
|
|
10
|
+
type ImportFileHandle,
|
|
11
|
+
isImportFileHandleIndex,
|
|
12
|
+
isImportFileHandleUpload,
|
|
13
|
+
} from "@milaboratories/pl-model-common";
|
|
14
|
+
import { importHandlesIn } from "./files";
|
|
15
|
+
import { page } from "./paging";
|
|
16
|
+
import { errorResult, textResult } from "./types";
|
|
17
|
+
|
|
18
|
+
/** What `export_project_template` uses of the middle layer and the server. */
|
|
19
|
+
export type ExportProjectTemplateDeps = {
|
|
20
|
+
resolveProject: (projectId: ProjectId) => Promise<{ id: ProjectId }>;
|
|
21
|
+
exportProjectAsTemplate: (projectId: ProjectId) => Promise<ProjectTemplateExportOutcome>;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* One import file handle the exported params hold: the block, where in its params, the handle as
|
|
26
|
+
* the file has it, and the file it names. A handle names a file of the computer that made it
|
|
27
|
+
* (`localPath`) or a file in a storage (`storage`, an id list_files takes, and `storagePath`).
|
|
28
|
+
*/
|
|
29
|
+
export type TemplateFileHandle = {
|
|
30
|
+
blockId: string;
|
|
31
|
+
/** Where in the block's params: `inputHandle`, `files[2].handle`. */
|
|
32
|
+
paramsPath: string;
|
|
33
|
+
handle: ImportFileHandle;
|
|
34
|
+
} & ({ localPath: string } | { storage: string; storagePath: string } | { error: string });
|
|
35
|
+
|
|
36
|
+
/** The file a handle names, decoded by the drivers' own parsers. */
|
|
37
|
+
function fileOf(
|
|
38
|
+
handle: ImportFileHandle,
|
|
39
|
+
): { localPath: string } | { storage: string; storagePath: string } | { error: string } {
|
|
40
|
+
try {
|
|
41
|
+
if (isImportFileHandleUpload(handle)) return { localPath: parseUploadHandle(handle).localPath };
|
|
42
|
+
if (isImportFileHandleIndex(handle)) {
|
|
43
|
+
const { storageId, path } = parseIndexHandle(handle);
|
|
44
|
+
return { storage: storageId, storagePath: path };
|
|
45
|
+
}
|
|
46
|
+
} catch {
|
|
47
|
+
// reported below
|
|
48
|
+
}
|
|
49
|
+
return { error: "The handle does not parse." };
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Every import file handle in the params of the exported blocks, in block order. */
|
|
53
|
+
export function templateFileHandles(
|
|
54
|
+
blocks: readonly { id: string; params: Record<string, unknown> }[],
|
|
55
|
+
): TemplateFileHandle[] {
|
|
56
|
+
return blocks.flatMap((block) =>
|
|
57
|
+
importHandlesIn(block.params).map(({ path, handle }): TemplateFileHandle => ({
|
|
58
|
+
blockId: block.id,
|
|
59
|
+
paramsPath: path,
|
|
60
|
+
handle,
|
|
61
|
+
...fileOf(handle),
|
|
62
|
+
})),
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** One line of a refusal: the block by label and id, and what stands in its way. */
|
|
67
|
+
function problemLine(problem: TemplateExportProblem): string {
|
|
68
|
+
return `- ${JSON.stringify(problem.blockLabel)} (${problem.blockId}): ${problem.error}`;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** File handles one `export_project_template` answer lists by default. */
|
|
72
|
+
export const TEMPLATE_HANDLES_DEFAULT = 50;
|
|
73
|
+
/** The most file handles one `export_project_template` answer lists. */
|
|
74
|
+
export const TEMPLATE_HANDLES_MAX = 100;
|
|
75
|
+
|
|
76
|
+
/** Input of {@link exportProjectTemplate}. */
|
|
77
|
+
export type ExportProjectTemplateInput = {
|
|
78
|
+
projectId: ProjectId;
|
|
79
|
+
/** Absolute path of a new file to write the YAML to; left out, nothing is written. */
|
|
80
|
+
savePath?: string;
|
|
81
|
+
offset?: number;
|
|
82
|
+
limit?: number;
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* `export_project_template`: writes the project, as the `template-v1` YAML the application's
|
|
87
|
+
* "Export Project as Template…" writes, to `savePath`, a new file (one that exists is never
|
|
88
|
+
* overwritten), and lists a page of the import file handles its params hold. When any block
|
|
89
|
+
* cannot be written, nothing is written and the refusal names every such block and why. Each call
|
|
90
|
+
* exports anew; a call without `savePath` only lists the handles.
|
|
91
|
+
*/
|
|
92
|
+
export async function exportProjectTemplate(
|
|
93
|
+
deps: ExportProjectTemplateDeps,
|
|
94
|
+
input: ExportProjectTemplateInput,
|
|
95
|
+
) {
|
|
96
|
+
const { projectId, savePath } = input;
|
|
97
|
+
if (savePath !== undefined && !path.isAbsolute(savePath))
|
|
98
|
+
return errorResult(`Not an absolute path: ${savePath}. Nothing was exported.`);
|
|
99
|
+
const entry = await deps.resolveProject(projectId);
|
|
100
|
+
const outcome = await deps.exportProjectAsTemplate(entry.id);
|
|
101
|
+
if (!outcome.ok) {
|
|
102
|
+
const n = outcome.problems.length;
|
|
103
|
+
return errorResult(
|
|
104
|
+
`Nothing was exported: ${n} block${n === 1 ? "" : "s"} of project ${projectId} cannot be ` +
|
|
105
|
+
`written to a template.\n${outcome.problems.map(problemLine).join("\n")}`,
|
|
106
|
+
"Each line names a block and the reason. A block whose version cannot be written to a " +
|
|
107
|
+
"template, or that declares no kind, needs a newer version: update_block, then export " +
|
|
108
|
+
"again. A block that cannot give its params may need its data fixed (get_block_state).",
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
const { yaml } = outcome;
|
|
112
|
+
if (savePath !== undefined) {
|
|
113
|
+
try {
|
|
114
|
+
await writeFile(savePath, yaml, { flag: "wx" });
|
|
115
|
+
} catch (e: unknown) {
|
|
116
|
+
return errorResult(
|
|
117
|
+
`Could not write the template: ${e instanceof Error ? e.message : String(e)}`,
|
|
118
|
+
"Check that the folder exists and is writable and that no file exists at the path; a " +
|
|
119
|
+
"file is never overwritten.",
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
const handles = templateFileHandles(outcome.document.blocks);
|
|
124
|
+
const { items, total, ...paging } = page(
|
|
125
|
+
handles,
|
|
126
|
+
input.offset,
|
|
127
|
+
input.limit ?? TEMPLATE_HANDLES_DEFAULT,
|
|
128
|
+
);
|
|
129
|
+
return textResult({
|
|
130
|
+
...(savePath !== undefined ? { path: savePath, bytes: Buffer.byteLength(yaml, "utf8") } : {}),
|
|
131
|
+
fileHandlesTotal: total,
|
|
132
|
+
...paging,
|
|
133
|
+
fileHandles: items,
|
|
134
|
+
});
|
|
135
|
+
}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import type { ProjectHolder, ProjectId } from "@milaboratories/pl-middle-layer";
|
|
2
|
+
import { ProjectInUseError } from "@milaboratories/pl-middle-layer";
|
|
3
|
+
import { describe, expect, test } from "vitest";
|
|
4
|
+
import type { Hold, HoldSource } from "../project-holds";
|
|
5
|
+
import { ProjectHolds } from "../project-holds";
|
|
6
|
+
import { deleteProject } from "./projects";
|
|
7
|
+
|
|
8
|
+
const P1 = "p1" as ProjectId;
|
|
9
|
+
const HOLDER = "mcp:test" as ProjectHolder;
|
|
10
|
+
|
|
11
|
+
type FakeProject = { id: ProjectId };
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* A middle layer that knows who holds each project and deletes the way the real one does: refused
|
|
15
|
+
* when `refuseIfHeldBy` says yes to any holder, the holds ended otherwise.
|
|
16
|
+
*/
|
|
17
|
+
class FakeMl implements HoldSource<FakeProject> {
|
|
18
|
+
private readonly holds = new Set<{
|
|
19
|
+
id: ProjectId;
|
|
20
|
+
holder: ProjectHolder;
|
|
21
|
+
controller: AbortController;
|
|
22
|
+
}>();
|
|
23
|
+
readonly deleted: ProjectId[] = [];
|
|
24
|
+
|
|
25
|
+
async acquireProject(id: ProjectId, holder: ProjectHolder): Promise<Hold<FakeProject>> {
|
|
26
|
+
const entry = { id, holder, controller: new AbortController() };
|
|
27
|
+
this.holds.add(entry);
|
|
28
|
+
return {
|
|
29
|
+
project: { id },
|
|
30
|
+
signal: entry.controller.signal,
|
|
31
|
+
get ended() {
|
|
32
|
+
return entry.controller.signal.aborted;
|
|
33
|
+
},
|
|
34
|
+
release: async () => {
|
|
35
|
+
this.holds.delete(entry);
|
|
36
|
+
entry.controller.abort();
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
async deleteProject(id: ProjectId, ops: { refuseIfHeldBy: (holder: ProjectHolder) => boolean }) {
|
|
42
|
+
const holders = [...this.holds].filter((h) => h.id === id).map((h) => h.holder);
|
|
43
|
+
const refused = holders.filter(ops.refuseIfHeldBy);
|
|
44
|
+
if (refused.length > 0) throw new ProjectInUseError(id, refused);
|
|
45
|
+
for (const h of [...this.holds].filter((h) => h.id === id)) {
|
|
46
|
+
this.holds.delete(h);
|
|
47
|
+
h.controller.abort();
|
|
48
|
+
}
|
|
49
|
+
this.deleted.push(id);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function deferred() {
|
|
54
|
+
let resolve = () => {};
|
|
55
|
+
const promise = new Promise<void>((res) => {
|
|
56
|
+
resolve = res;
|
|
57
|
+
});
|
|
58
|
+
return { promise, resolve };
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** One server: its holds and its delete. */
|
|
62
|
+
function server(ml: FakeMl) {
|
|
63
|
+
const holds = new ProjectHolds<FakeProject>({ holder: HOLDER, idleMs: 60_000 });
|
|
64
|
+
const remove = () =>
|
|
65
|
+
deleteProject(
|
|
66
|
+
{
|
|
67
|
+
resolveProject: async (id) => ({ id: id as ProjectId }),
|
|
68
|
+
deleteProject: (id, ops) => ml.deleteProject(id, ops),
|
|
69
|
+
projectHolder: HOLDER,
|
|
70
|
+
callsInFlight: (id) => holds.callsInFlight(id),
|
|
71
|
+
},
|
|
72
|
+
P1,
|
|
73
|
+
);
|
|
74
|
+
return { holds, remove };
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
type Result = { content: { text: string }[]; isError?: boolean };
|
|
78
|
+
|
|
79
|
+
describe("delete_project", () => {
|
|
80
|
+
test("is refused while another call of this server works on the project", async () => {
|
|
81
|
+
const ml = new FakeMl();
|
|
82
|
+
const { holds, remove } = server(ml);
|
|
83
|
+
const gate = deferred();
|
|
84
|
+
// Agent A waits for a block; agent B, through the same server, deletes the project.
|
|
85
|
+
const waiting = holds.use(ml, P1, () => gate.promise);
|
|
86
|
+
await Promise.resolve();
|
|
87
|
+
|
|
88
|
+
const refused: Result = await remove();
|
|
89
|
+
expect(refused.isError).toBe(true);
|
|
90
|
+
expect(refused.content[0].text).toContain("still in use by another agent");
|
|
91
|
+
expect(ml.deleted).toEqual([]);
|
|
92
|
+
|
|
93
|
+
gate.resolve();
|
|
94
|
+
await waiting;
|
|
95
|
+
// Only the hold kept after the call is left: it ends with the delete.
|
|
96
|
+
const done: Result = await remove();
|
|
97
|
+
expect(done.isError).toBeUndefined();
|
|
98
|
+
expect(ml.deleted).toEqual([P1]);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
test("is refused while a call of this server is still getting its hold", async () => {
|
|
102
|
+
const ml = new FakeMl();
|
|
103
|
+
const { holds, remove } = server(ml);
|
|
104
|
+
const slow: HoldSource<FakeProject> = {
|
|
105
|
+
acquireProject: (id, holder) =>
|
|
106
|
+
new Promise((resolve) => setTimeout(() => resolve(ml.acquireProject(id, holder)), 20)),
|
|
107
|
+
};
|
|
108
|
+
const call = holds.use(slow, P1, async () => {});
|
|
109
|
+
const refused: Result = await remove();
|
|
110
|
+
expect(refused.isError).toBe(true);
|
|
111
|
+
await call;
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
test("is refused while anyone else holds the project", async () => {
|
|
115
|
+
const ml = new FakeMl();
|
|
116
|
+
const { remove } = server(ml);
|
|
117
|
+
await ml.acquireProject(P1, "desktop" as ProjectHolder);
|
|
118
|
+
const refused: Result = await remove();
|
|
119
|
+
expect(refused.isError).toBe(true);
|
|
120
|
+
expect(ml.deleted).toEqual([]);
|
|
121
|
+
});
|
|
122
|
+
});
|