@milaboratories/pl-mcp-server 54.0.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 +15 -25
- 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 +15 -12
- package/dist/instructions.cjs.map +1 -1
- package/dist/instructions.js +15 -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 +58 -8
- package/dist/project-holds.cjs.map +1 -1
- package/dist/project-holds.js +58 -8
- package/dist/project-holds.js.map +1 -1
- 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 +40 -18
- package/dist/server-log.cjs.map +1 -1
- package/dist/server-log.js +40 -19
- package/dist/server-log.js.map +1 -1
- package/dist/server.cjs +48 -18
- package/dist/server.cjs.map +1 -1
- package/dist/server.d.ts +21 -20
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +48 -18
- 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 -73
- package/dist/tools/await.cjs.map +1 -1
- package/dist/tools/await.js +212 -75
- package/dist/tools/await.js.map +1 -1
- package/dist/tools/block-action-context.cjs +3 -0
- package/dist/tools/block-action-context.cjs.map +1 -1
- package/dist/tools/block-action-context.js +3 -0
- 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 +272 -4
- package/dist/tools/block-params.cjs.map +1 -1
- package/dist/tools/block-params.js +270 -6
- package/dist/tools/block-params.js.map +1 -1
- package/dist/tools/block-state.cjs +323 -42
- package/dist/tools/block-state.cjs.map +1 -1
- package/dist/tools/block-state.js +317 -46
- 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 +183 -70
- package/dist/tools/blocks.cjs.map +1 -1
- package/dist/tools/blocks.js +177 -70
- package/dist/tools/blocks.js.map +1 -1
- package/dist/tools/data-query.cjs +451 -113
- package/dist/tools/data-query.cjs.map +1 -1
- package/dist/tools/data-query.js +446 -118
- 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 +56 -0
- package/dist/tools/ids.cjs.map +1 -1
- package/dist/tools/ids.js +51 -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 +98 -19
- package/dist/tools/logs.cjs.map +1 -1
- package/dist/tools/logs.js +95 -20
- 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 +66 -19
- package/dist/tools/projects.cjs.map +1 -1
- package/dist/tools/projects.js +66 -20
- 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 +15 -12
- package/src/project-holds.test.ts +179 -0
- package/src/project-holds.ts +114 -12
- package/src/project-list.test.ts +38 -0
- package/src/project-list.ts +25 -0
- package/src/server-log.test.ts +54 -1
- package/src/server-log.ts +49 -27
- package/src/server.logging.test.ts +38 -46
- package/src/server.test.ts +151 -17
- package/src/server.ts +62 -34
- 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 -123
- package/src/tools/block-action-context.ts +3 -0
- package/src/tools/block-api.ts +19 -4
- package/src/tools/block-params.test.ts +828 -0
- package/src/tools/block-params.ts +623 -8
- package/src/tools/block-state.test.ts +480 -0
- package/src/tools/block-state.ts +659 -66
- 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 +336 -105
- package/src/tools/data-query.test.ts +641 -0
- package/src/tools/data-query.ts +807 -157
- 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 +67 -0
- package/src/tools/imports.ts +304 -0
- package/src/tools/logs.test.ts +70 -0
- package/src/tools/logs.ts +130 -21
- 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 +135 -25
- 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 +77 -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
package/src/tools/logs.ts
CHANGED
|
@@ -1,9 +1,103 @@
|
|
|
1
1
|
import { isAnyLogHandle } from "@milaboratories/pl-model-common";
|
|
2
2
|
import type { AnyLogHandle } from "@milaboratories/pl-model-common";
|
|
3
3
|
import { z } from "zod";
|
|
4
|
+
import { isPlainObject } from "@milaboratories/helpers";
|
|
4
5
|
import { projectIdInput, blockIdInput } from "./ids";
|
|
5
6
|
import type { ToolContext, ToolRegistrar } from "./types";
|
|
6
7
|
import { errorResult, hints, textResult } from "./types";
|
|
8
|
+
import { findBlock } from "./readiness";
|
|
9
|
+
import { page, pagingArgs } from "./paging";
|
|
10
|
+
|
|
11
|
+
/** How many logs get_block_logs reads when the caller does not say. */
|
|
12
|
+
export const LOGS_DEFAULT_LIMIT = 10;
|
|
13
|
+
/** The most logs one get_block_logs call reads. */
|
|
14
|
+
export const LOGS_MAX_LIMIT = 100;
|
|
15
|
+
|
|
16
|
+
/** How deep into an output value log handles are looked for. */
|
|
17
|
+
const MAX_DEPTH = 8;
|
|
18
|
+
|
|
19
|
+
/** The most arrays and objects of one output walked when looking for log handles. */
|
|
20
|
+
export const MAX_NODES = 20_000;
|
|
21
|
+
|
|
22
|
+
/** One log a block's outputs carry. */
|
|
23
|
+
export type LogEntry = {
|
|
24
|
+
/** The output that carries the log handle. */
|
|
25
|
+
outputKey: string;
|
|
26
|
+
/**
|
|
27
|
+
* Where in the output's value: the key of a `{ data: [{ key, value }] }` entry (a sample, a
|
|
28
|
+
* run), or the keys and indices leading to the handle; empty for a handle that is the value.
|
|
29
|
+
*/
|
|
30
|
+
path: string[];
|
|
31
|
+
handle: AnyLogHandle;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
/** The key a log is given in the result: the output key, then its path, joined by `/`. */
|
|
35
|
+
export function logKey(entry: Pick<LogEntry, "outputKey" | "path">): string {
|
|
36
|
+
return [entry.outputKey, ...entry.path].join("/");
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** The log handles of a block's outputs, and the outputs whose walk stopped at a limit. */
|
|
40
|
+
export type FoundLogs = { entries: LogEntry[]; cutOutputs: string[] };
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Finds every log handle in a block's outputs (output key → `{ok, value}`), in output order:
|
|
44
|
+
* the value itself, an entry of a `{ data: [{ key, value }] }` map (named by its key), or any
|
|
45
|
+
* other place in arrays and objects. Failed outputs, entries that are not objects and keys that
|
|
46
|
+
* are not arrays are passed over. The walk of each output stops at {@link MAX_DEPTH} and at
|
|
47
|
+
* {@link MAX_NODES} arrays and objects; values that cannot hold a handle (numbers, other strings)
|
|
48
|
+
* are not counted. An output whose walk stopped is in `cutOutputs`.
|
|
49
|
+
*/
|
|
50
|
+
export function collectLogHandles(outputs: Record<string, unknown>): FoundLogs {
|
|
51
|
+
const entries: LogEntry[] = [];
|
|
52
|
+
const cutOutputs: string[] = [];
|
|
53
|
+
let nodes = 0;
|
|
54
|
+
let cut = false;
|
|
55
|
+
|
|
56
|
+
const walk = (outputKey: string, v: unknown, path: string[], depth: number): void => {
|
|
57
|
+
if (isAnyLogHandle(v)) {
|
|
58
|
+
entries.push({ outputKey, path, handle: v });
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
const isArray = Array.isArray(v);
|
|
62
|
+
if (!isArray && !isPlainObject(v)) return;
|
|
63
|
+
if (++nodes > MAX_NODES || depth > MAX_DEPTH) {
|
|
64
|
+
cut = true;
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
if (isArray) {
|
|
68
|
+
v.forEach((item, i) => walk(outputKey, item, [...path, String(i)], depth + 1));
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
for (const [key, item] of Object.entries(v)) {
|
|
72
|
+
if (key === "data" && Array.isArray(item)) {
|
|
73
|
+
item.forEach((entry, i) => {
|
|
74
|
+
if (isPlainObject(entry) && Array.isArray(entry.key))
|
|
75
|
+
walk(outputKey, entry.value, [...path, ...entry.key.map(String)], depth + 1);
|
|
76
|
+
else walk(outputKey, entry, [...path, "data", String(i)], depth + 1);
|
|
77
|
+
});
|
|
78
|
+
} else walk(outputKey, item, [...path, key], depth + 1);
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
for (const [outputKey, output] of Object.entries(outputs)) {
|
|
83
|
+
if (!isPlainObject(output) || output.ok !== true) continue;
|
|
84
|
+
nodes = 0;
|
|
85
|
+
cut = false;
|
|
86
|
+
walk(outputKey, output.value, [], 0);
|
|
87
|
+
if (cut) cutOutputs.push(outputKey);
|
|
88
|
+
}
|
|
89
|
+
return { entries, cutOutputs };
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** What the result says of outputs whose walk stopped at a limit. */
|
|
93
|
+
export function cutNote(cutOutputs: string[]): string {
|
|
94
|
+
const [noun, verb, owner] =
|
|
95
|
+
cutOutputs.length > 1 ? ["Outputs", "are", "their"] : ["Output", "is", "its"];
|
|
96
|
+
return (
|
|
97
|
+
`${noun} ${cutOutputs.join(", ")} ${verb} too large or too deep to search whole: some of ` +
|
|
98
|
+
`${owner} logs may be missed.`
|
|
99
|
+
);
|
|
100
|
+
}
|
|
7
101
|
|
|
8
102
|
export function registerLogTools(server: ToolRegistrar, ctx: ToolContext): void {
|
|
9
103
|
server.registerTool(
|
|
@@ -12,7 +106,11 @@ export function registerLogTools(server: ToolRegistrar, ctx: ToolContext): void
|
|
|
12
106
|
title: "Get Block Logs",
|
|
13
107
|
annotations: hints.read,
|
|
14
108
|
description:
|
|
15
|
-
"Read execution logs for a block.
|
|
109
|
+
"Read execution logs for a block. Finds the log handles in the block's outputs (an " +
|
|
110
|
+
"output that is a log, or holds logs per sample or run, at any depth) and reads them, " +
|
|
111
|
+
`${LOGS_DEFAULT_LIMIT} logs at a time. Result: \`{ logs, total, offset, hasMore }\`, ` +
|
|
112
|
+
"`logs` keyed `<output key>/<sample or run id>` (the output key alone for an output " +
|
|
113
|
+
"that is one log); page with `offset` and `limit`.",
|
|
16
114
|
inputSchema: z.object({
|
|
17
115
|
projectId: projectIdInput.describe("Project ID"),
|
|
18
116
|
blockId: blockIdInput.describe("Block ID"),
|
|
@@ -20,11 +118,16 @@ export function registerLogTools(server: ToolRegistrar, ctx: ToolContext): void
|
|
|
20
118
|
sampleId: z
|
|
21
119
|
.string()
|
|
22
120
|
.optional()
|
|
23
|
-
.describe(
|
|
121
|
+
.describe(
|
|
122
|
+
"Only the logs of this sample or run id, or of this output key (reads all if omitted)",
|
|
123
|
+
),
|
|
124
|
+
...pagingArgs("logs", LOGS_DEFAULT_LIMIT, LOGS_MAX_LIMIT),
|
|
24
125
|
}),
|
|
25
126
|
},
|
|
26
|
-
async ({ projectId, blockId, lines, sampleId }) => {
|
|
127
|
+
async ({ projectId, blockId, lines, sampleId, limit, offset }) => {
|
|
27
128
|
return await ctx.withProject(projectId, async (project) => {
|
|
129
|
+
const { error } = await findBlock(project, projectId, blockId);
|
|
130
|
+
if (error) return error;
|
|
28
131
|
const state = await project.getBlockState(blockId).getValue();
|
|
29
132
|
if (!state.outputs)
|
|
30
133
|
return errorResult(
|
|
@@ -33,33 +136,28 @@ export function registerLogTools(server: ToolRegistrar, ctx: ToolContext): void
|
|
|
33
136
|
);
|
|
34
137
|
|
|
35
138
|
// Scan all outputs for log handles (log+ready:// or log+live://)
|
|
36
|
-
const
|
|
37
|
-
string,
|
|
38
|
-
{ ok?: boolean; value?: { data?: { key: string[]; value: unknown }[] } }
|
|
39
|
-
>;
|
|
40
|
-
const logEntries: { outputKey: string; key: string[]; handle: AnyLogHandle }[] = [];
|
|
41
|
-
for (const [outputKey, output] of Object.entries(outputs)) {
|
|
42
|
-
if (!output?.ok || !output.value?.data) continue;
|
|
43
|
-
for (const entry of output.value.data) {
|
|
44
|
-
if (isAnyLogHandle(entry.value)) {
|
|
45
|
-
logEntries.push({ outputKey, key: entry.key, handle: entry.value });
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
}
|
|
139
|
+
const { entries: logEntries, cutOutputs } = collectLogHandles(state.outputs);
|
|
49
140
|
|
|
50
141
|
if (logEntries.length === 0) {
|
|
51
142
|
return errorResult(
|
|
52
|
-
"No log handles found in block outputs."
|
|
143
|
+
"No log handles found in block outputs." +
|
|
144
|
+
(cutOutputs.length > 0 ? ` ${cutNote(cutOutputs)}` : ""),
|
|
53
145
|
"This block may not produce logs, or it hasn't run yet. Use get_block_outputs to inspect available output types.",
|
|
54
146
|
);
|
|
55
147
|
}
|
|
56
148
|
|
|
57
149
|
const logDriver = ctx.requireMl().driverKit.logDriver;
|
|
58
150
|
const results: Record<string, string> = {};
|
|
151
|
+
const { items, ...paging } = page(
|
|
152
|
+
sampleId
|
|
153
|
+
? logEntries.filter((e) => e.path.includes(sampleId) || e.outputKey === sampleId)
|
|
154
|
+
: logEntries,
|
|
155
|
+
offset,
|
|
156
|
+
limit ?? LOGS_DEFAULT_LIMIT,
|
|
157
|
+
);
|
|
59
158
|
|
|
60
|
-
for (const entry of
|
|
61
|
-
const key = entry
|
|
62
|
-
if (sampleId && !entry.key.includes(sampleId)) continue;
|
|
159
|
+
for (const entry of items) {
|
|
160
|
+
const key = logKey(entry);
|
|
63
161
|
try {
|
|
64
162
|
const response = await logDriver.lastLines(entry.handle, lines);
|
|
65
163
|
if (response.shouldUpdateHandle) {
|
|
@@ -72,7 +170,18 @@ export function registerLogTools(server: ToolRegistrar, ctx: ToolContext): void
|
|
|
72
170
|
}
|
|
73
171
|
}
|
|
74
172
|
|
|
75
|
-
return textResult(
|
|
173
|
+
return textResult(
|
|
174
|
+
{
|
|
175
|
+
logs: results,
|
|
176
|
+
...paging,
|
|
177
|
+
...(cutOutputs.length > 0 ? { note: cutNote(cutOutputs) } : {}),
|
|
178
|
+
},
|
|
179
|
+
{
|
|
180
|
+
hint:
|
|
181
|
+
"Ask for less: fewer `lines`, a smaller `limit` with `offset` to page, or " +
|
|
182
|
+
"`sampleId` for one sample's log.",
|
|
183
|
+
},
|
|
184
|
+
);
|
|
76
185
|
});
|
|
77
186
|
},
|
|
78
187
|
);
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { describe, expect, test } from "vitest";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { page, pagingArgs } from "./paging";
|
|
4
|
+
|
|
5
|
+
describe("page", () => {
|
|
6
|
+
const items = [1, 2, 3, 4, 5];
|
|
7
|
+
|
|
8
|
+
test("the first page says more follow", () => {
|
|
9
|
+
expect(page(items, undefined, 2)).toEqual({
|
|
10
|
+
items: [1, 2],
|
|
11
|
+
total: 5,
|
|
12
|
+
offset: 0,
|
|
13
|
+
hasMore: true,
|
|
14
|
+
});
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
test("the last page says none follow", () => {
|
|
18
|
+
expect(page(items, 4, 2)).toEqual({ items: [5], total: 5, offset: 4, hasMore: false });
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
test("an offset past the end is an empty page", () => {
|
|
22
|
+
expect(page(items, 9, 2)).toEqual({ items: [], total: 5, offset: 9, hasMore: false });
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
describe("pagingArgs", () => {
|
|
27
|
+
const schema = z.object(pagingArgs("things", 10, 50));
|
|
28
|
+
|
|
29
|
+
test("accepts limits within bounds and no arguments at all", () => {
|
|
30
|
+
expect(schema.parse({})).toEqual({});
|
|
31
|
+
expect(schema.parse({ limit: 50, offset: 3 })).toEqual({ limit: 50, offset: 3 });
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
test("refuses a limit over the most and a negative offset", () => {
|
|
35
|
+
expect(schema.safeParse({ limit: 51 }).success).toBe(false);
|
|
36
|
+
expect(schema.safeParse({ limit: 0 }).success).toBe(false);
|
|
37
|
+
expect(schema.safeParse({ offset: -1 }).success).toBe(false);
|
|
38
|
+
});
|
|
39
|
+
});
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The `limit` and `offset` arguments of a tool that lists items page by page, so a caller can
|
|
5
|
+
* always reach every item (and its id), however many there are.
|
|
6
|
+
*/
|
|
7
|
+
export function pagingArgs(what: string, defaultLimit: number, maxLimit: number) {
|
|
8
|
+
return {
|
|
9
|
+
limit: z
|
|
10
|
+
.number()
|
|
11
|
+
.int()
|
|
12
|
+
.min(1)
|
|
13
|
+
.max(maxLimit)
|
|
14
|
+
.optional()
|
|
15
|
+
.describe(`The most ${what} to return (default ${defaultLimit}, at most ${maxLimit}).`),
|
|
16
|
+
offset: z
|
|
17
|
+
.number()
|
|
18
|
+
.int()
|
|
19
|
+
.min(0)
|
|
20
|
+
.optional()
|
|
21
|
+
.describe(`How many ${what} to skip (default 0); with \`limit\`, pages through them.`),
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** One page of `items`: the items, how many there are in all and whether more follow. */
|
|
26
|
+
export function page<T>(
|
|
27
|
+
items: readonly T[],
|
|
28
|
+
offset: number | undefined,
|
|
29
|
+
limit: number,
|
|
30
|
+
): { items: T[]; total: number; offset: number; hasMore: boolean } {
|
|
31
|
+
const start = offset ?? 0;
|
|
32
|
+
const slice = items.slice(start, start + limit);
|
|
33
|
+
return {
|
|
34
|
+
items: slice,
|
|
35
|
+
total: items.length,
|
|
36
|
+
offset: start,
|
|
37
|
+
hasMore: start + slice.length < items.length,
|
|
38
|
+
};
|
|
39
|
+
}
|
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createColumnDiscoveredId,
|
|
3
|
+
createColumnValueId,
|
|
4
|
+
createGlobalPObjectId,
|
|
5
|
+
} from "@milaboratories/pl-model-common";
|
|
6
|
+
import { describe, expect, test } from "vitest";
|
|
7
|
+
import { mergeThreeWay } from "./params-merge";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The merge of set_block_params over the shapes of real blocks seen in the D-040 round-trip check
|
|
11
|
+
* (research/d040-params-roundtrip.md): `base = init(p0)`, `theirs = init(p1)`, `ours` = the
|
|
12
|
+
* block's data. Each block's `init` and `templateParams` are modelled here after the block's own.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/** Merges and expects no conflicts. */
|
|
16
|
+
function merged(base: unknown, theirs: unknown, ours: unknown): unknown {
|
|
17
|
+
const outcome = mergeThreeWay(base, theirs, ours);
|
|
18
|
+
expect(outcome.conflicts).toEqual([]);
|
|
19
|
+
return outcome.merged;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
describe("Samples & Data: metadata columns hold per-sample values the params strip", () => {
|
|
23
|
+
type Column = { id: string; label: string; valueType: string; data: Record<string, unknown> };
|
|
24
|
+
type SdData = {
|
|
25
|
+
sampleIds: string[];
|
|
26
|
+
sampleLabels: Record<string, string>;
|
|
27
|
+
metadata: Column[];
|
|
28
|
+
datasets: { id: string; label: string; content: { type: string; data: object } }[];
|
|
29
|
+
sampleLabelColumnLabel: string;
|
|
30
|
+
};
|
|
31
|
+
type SdParams = Pick<SdData, "metadata" | "datasets" | "sampleLabelColumnLabel">;
|
|
32
|
+
|
|
33
|
+
const ours: SdData = {
|
|
34
|
+
sampleIds: ["s1", "s2"],
|
|
35
|
+
sampleLabels: { s1: "S1", s2: "S2" },
|
|
36
|
+
metadata: [
|
|
37
|
+
{ id: "c0", label: "Time point", valueType: "String", data: { s1: "T01", s2: "T02" } },
|
|
38
|
+
{ id: "c1", label: "Time point index", valueType: "Long", data: { s1: 1, s2: 2 } },
|
|
39
|
+
{ id: "c2", label: "Donor", valueType: "String", data: { s1: "D1", s2: "D1" } },
|
|
40
|
+
{ id: "c3", label: "Collection date", valueType: "String", data: { s1: "2024-01", s2: "" } },
|
|
41
|
+
],
|
|
42
|
+
datasets: [
|
|
43
|
+
{ id: "d1", label: "Reads", content: { type: "Fastq", data: { s1: { R1: "h1" } } } },
|
|
44
|
+
],
|
|
45
|
+
sampleLabelColumnLabel: "Sample",
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
/** `templateParams`: files, samples and metadata values stripped. */
|
|
49
|
+
function templateParams(d: SdData): SdParams {
|
|
50
|
+
return {
|
|
51
|
+
datasets: d.datasets.map((ds) => ({ ...ds, content: { ...ds.content, data: {} } })),
|
|
52
|
+
metadata: d.metadata.map((m) => ({ ...m, data: {} })),
|
|
53
|
+
sampleLabelColumnLabel: d.sampleLabelColumnLabel,
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** `init`: the params as they are, no samples. */
|
|
58
|
+
function init(p: SdParams): SdData {
|
|
59
|
+
return { sampleIds: [], sampleLabels: {}, ...structuredClone(p) };
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const p0 = templateParams(ours);
|
|
63
|
+
const base = init(p0);
|
|
64
|
+
|
|
65
|
+
function mergeWith(change: (p: SdParams) => void): SdData {
|
|
66
|
+
const p1 = structuredClone(p0);
|
|
67
|
+
change(p1);
|
|
68
|
+
const result = merged(base, init(p1), ours);
|
|
69
|
+
// The check of set_block_params passes too.
|
|
70
|
+
expect(templateParams(result as SdData)).toEqual(templateParams(init(p1)));
|
|
71
|
+
return result as SdData;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** Column label → its per-sample values. */
|
|
75
|
+
function valuesByLabel(d: SdData) {
|
|
76
|
+
return Object.fromEntries(d.metadata.map((m) => [m.label, m.data]));
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
test("renaming a column keeps its values", () => {
|
|
80
|
+
const result = mergeWith((p) => {
|
|
81
|
+
p.metadata[3].label = "Collection date (ISO)";
|
|
82
|
+
});
|
|
83
|
+
expect(result.metadata[3]).toEqual({ ...ours.metadata[3], label: "Collection date (ISO)" });
|
|
84
|
+
expect(result.sampleIds).toEqual(ours.sampleIds);
|
|
85
|
+
expect(result.datasets).toEqual(ours.datasets);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
test("swapping two columns moves their values with them", () => {
|
|
89
|
+
const result = mergeWith((p) => {
|
|
90
|
+
[p.metadata[0], p.metadata[1]] = [p.metadata[1], p.metadata[0]];
|
|
91
|
+
});
|
|
92
|
+
expect(result.metadata.map((m) => m.id)).toEqual(["c1", "c0", "c2", "c3"]);
|
|
93
|
+
expect(valuesByLabel(result)).toEqual(valuesByLabel(ours));
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
test("removing a column drops it and only it, values and all", () => {
|
|
97
|
+
const result = mergeWith((p) => {
|
|
98
|
+
p.metadata.splice(2, 1);
|
|
99
|
+
});
|
|
100
|
+
expect(result.metadata.map((m) => m.id)).toEqual(["c0", "c1", "c3"]);
|
|
101
|
+
expect(result.metadata).toEqual(ours.metadata.filter((m) => m.id !== "c2"));
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
test("adding a column adds it empty and keeps the values of the others", () => {
|
|
105
|
+
const added: Column = { id: "c4", label: "Batch", valueType: "String", data: {} };
|
|
106
|
+
const result = mergeWith((p) => {
|
|
107
|
+
p.metadata.push(added);
|
|
108
|
+
});
|
|
109
|
+
expect(result.metadata).toEqual([...ours.metadata, added]);
|
|
110
|
+
expect(result.sampleLabels).toEqual(ours.sampleLabels);
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
describe("Sequence Diversity: metric rows carry ids, view state sits beside them", () => {
|
|
115
|
+
const metric = (id: string, type: string) => ({
|
|
116
|
+
id,
|
|
117
|
+
type,
|
|
118
|
+
downsampling: { type: "none" },
|
|
119
|
+
isExpanded: false,
|
|
120
|
+
});
|
|
121
|
+
const defaults = { tableState: { sorting: [] }, graphState: { title: "" } };
|
|
122
|
+
const init = (p: { metrics: unknown[] }) => ({ metrics: p.metrics, ...defaults });
|
|
123
|
+
const metrics = ["shannon", "chao1", "gini", "d50", "observed", "simpson", "inverseSimpson"].map(
|
|
124
|
+
(type, i) => metric(`m${i}`, type),
|
|
125
|
+
);
|
|
126
|
+
const ours = {
|
|
127
|
+
metrics,
|
|
128
|
+
tableState: { sorting: [{ column: "shannon", desc: true }] },
|
|
129
|
+
graphState: { title: "Diversity" },
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
test("seven metrics to three, one with another downsampling: only the metrics change", () => {
|
|
133
|
+
const p1 = {
|
|
134
|
+
metrics: [
|
|
135
|
+
metrics[0],
|
|
136
|
+
{ ...metrics[1], downsampling: { type: "hypergeometric", valueChooser: "min" } },
|
|
137
|
+
metrics[2],
|
|
138
|
+
],
|
|
139
|
+
};
|
|
140
|
+
expect(merged(init({ metrics }), init(p1), ours)).toEqual({ ...ours, metrics: p1.metrics });
|
|
141
|
+
});
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
describe("Differential Abundance: thresholds and contrasts", () => {
|
|
145
|
+
const init = (p: Record<string, unknown>) => ({ ...p, tableState: { filters: [] } });
|
|
146
|
+
const p0 = {
|
|
147
|
+
numerators: ["T01", "T02"],
|
|
148
|
+
denominator: "T00",
|
|
149
|
+
log2FcThreshold: 1,
|
|
150
|
+
padjThreshold: 0.05,
|
|
151
|
+
label: "",
|
|
152
|
+
};
|
|
153
|
+
const filters = [
|
|
154
|
+
{ column: "padj", op: "<=", value: 0.05 },
|
|
155
|
+
{ column: "log2foldchange", op: ">=", value: 1 },
|
|
156
|
+
];
|
|
157
|
+
const ours = { ...p0, tableState: { filters } };
|
|
158
|
+
|
|
159
|
+
test("contrast, threshold and label change; table filters stay", () => {
|
|
160
|
+
const p1 = { ...p0, numerators: ["T01"], log2FcThreshold: 2, label: "T01 vs T00" };
|
|
161
|
+
expect(merged(init(p0), init(p1), ours)).toEqual({ ...p1, tableState: { filters } });
|
|
162
|
+
});
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
describe("Miltenyi Clonotyping: resources keys the params leave out", () => {
|
|
166
|
+
const p0 = { chains: ["TCRBeta"], species: "hsa" };
|
|
167
|
+
const init = (p: Record<string, unknown>) => ({ ...p, defaultBlockLabel: "" });
|
|
168
|
+
const ours = { ...p0, defaultBlockLabel: "TCRAlpha, TCRBeta" };
|
|
169
|
+
|
|
170
|
+
test("a resources key in the new params is written; the stored label stays", () => {
|
|
171
|
+
const p1 = { ...p0, chains: ["TCRAlpha", "TCRBeta"], perProcessMemGB: 8 };
|
|
172
|
+
expect(merged(init(p0), init(p1), ours)).toEqual({
|
|
173
|
+
chains: ["TCRAlpha", "TCRBeta"],
|
|
174
|
+
species: "hsa",
|
|
175
|
+
defaultBlockLabel: "TCRAlpha, TCRBeta",
|
|
176
|
+
perProcessMemGB: 8,
|
|
177
|
+
});
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
test("dropping a resources key from the params removes it", () => {
|
|
181
|
+
const withMem = { ...p0, perProcessMemGB: 8 };
|
|
182
|
+
expect(merged(init(withMem), init(p0), { ...ours, perProcessMemGB: 8 })).toEqual(ours);
|
|
183
|
+
});
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
describe("conflicts", () => {
|
|
187
|
+
test("a value where base, theirs and ours all differ is named by its path", () => {
|
|
188
|
+
const outcome = mergeThreeWay(
|
|
189
|
+
{ preset: { type: "name", name: "default" }, n: 1 },
|
|
190
|
+
{ preset: { type: "name", name: "other" }, n: 2 },
|
|
191
|
+
{ preset: { type: "file", file: "f.json" }, n: 1 },
|
|
192
|
+
);
|
|
193
|
+
expect(outcome.conflicts).toEqual(["preset.name"]);
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
test("arrays without ids merge as whole values", () => {
|
|
197
|
+
expect(mergeThreeWay({ xs: [1, 2] }, { xs: [1, 3] }, { xs: [1, 4] }).conflicts).toEqual(["xs"]);
|
|
198
|
+
expect(merged({ xs: [1, 2] }, { xs: [1, 3] }, { xs: [1, 2] })).toEqual({ xs: [1, 3] });
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
test("an element changed on both sides is named by its id", () => {
|
|
202
|
+
const outcome = mergeThreeWay(
|
|
203
|
+
{ cols: [{ id: "a", label: "A" }] },
|
|
204
|
+
{ cols: [{ id: "a", label: "B" }] },
|
|
205
|
+
{ cols: [{ id: "a", label: "C" }] },
|
|
206
|
+
);
|
|
207
|
+
expect(outcome.conflicts).toEqual(['cols[id="a"].label']);
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
test("ids repeated in one array make it a whole value", () => {
|
|
211
|
+
const outcome = mergeThreeWay(
|
|
212
|
+
[{ id: "a", v: 1 }],
|
|
213
|
+
[
|
|
214
|
+
{ id: "a", v: 2 },
|
|
215
|
+
{ id: "a", v: 3 },
|
|
216
|
+
],
|
|
217
|
+
[{ id: "a", v: 4 }],
|
|
218
|
+
);
|
|
219
|
+
expect(outcome.conflicts).toEqual(["(root)"]);
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
test("a numeric id and a string id are different elements", () => {
|
|
223
|
+
expect(merged([{ id: 1, v: 1 }], [{ id: "1", v: 1 }], [{ id: 1, v: 1, open: true }])).toEqual([
|
|
224
|
+
{ id: "1", v: 1 },
|
|
225
|
+
]);
|
|
226
|
+
});
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
describe("what stays as it is", () => {
|
|
230
|
+
test("nothing changes when the params do not", () => {
|
|
231
|
+
const ours = { a: 1, view: { x: 2 } };
|
|
232
|
+
expect(merged({ a: 1 }, { a: 1 }, ours)).toEqual(ours);
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
test("an element only ours has stays after its neighbour", () => {
|
|
236
|
+
const base = [{ id: "a" }, { id: "b" }];
|
|
237
|
+
const theirs = [{ id: "b" }, { id: "a" }];
|
|
238
|
+
const ours = [{ id: "a" }, { id: "x" }, { id: "b" }];
|
|
239
|
+
expect(merged(base, theirs, ours)).toEqual([{ id: "b" }, { id: "a" }, { id: "x" }]);
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
test("a key named like a prototype key is merged as a key", () => {
|
|
243
|
+
const parse = (s: string): unknown => JSON.parse(s);
|
|
244
|
+
const result = merged(
|
|
245
|
+
parse('{"__proto__": {"a": 1}}'),
|
|
246
|
+
parse('{"__proto__": {"a": 2}}'),
|
|
247
|
+
parse('{"__proto__": {"a": 1}, "v": 1}'),
|
|
248
|
+
);
|
|
249
|
+
expect(JSON.stringify(result)).toBe('{"__proto__":{"a":2},"v":1}');
|
|
250
|
+
});
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
describe("column ids pass through as written (D-042)", () => {
|
|
254
|
+
const leaf = createGlobalPObjectId("block1", "clones");
|
|
255
|
+
const discovered = createColumnDiscoveredId({
|
|
256
|
+
column: leaf,
|
|
257
|
+
path: [{ type: "linker", column: createGlobalPObjectId("block2", "linker") }],
|
|
258
|
+
});
|
|
259
|
+
const ids = {
|
|
260
|
+
column: discovered,
|
|
261
|
+
picked: [createColumnValueId({ column: discovered, value: "s1" })],
|
|
262
|
+
input: { __isRef: true, blockId: "block1", name: "clones", requireEnrichments: true },
|
|
263
|
+
};
|
|
264
|
+
|
|
265
|
+
test("ids the new params write are taken byte for byte, never narrowed to their leaf", () => {
|
|
266
|
+
const result = merged({ ids: null, view: 1 }, { ids, view: 1 }, { ids: null, view: 2 });
|
|
267
|
+
expect(JSON.stringify(result)).toBe(JSON.stringify({ ids, view: 2 }));
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
test("ids the block holds stay byte for byte when the params change something else", () => {
|
|
271
|
+
const result = merged({ ids, n: 1 }, { ids, n: 2 }, { ids, n: 1, view: 3 });
|
|
272
|
+
expect(JSON.stringify(result)).toBe(JSON.stringify({ ids, n: 2, view: 3 }));
|
|
273
|
+
});
|
|
274
|
+
});
|