@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
package/dist/tools/await.js
CHANGED
|
@@ -1,90 +1,228 @@
|
|
|
1
1
|
import { errorResult, hints, textResult, timedOutResult, transformFailedResult } from "./types.js";
|
|
2
2
|
import { blockIdInput, projectIdInput } from "./ids.js";
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
3
|
+
import { awaitBlockSettled, clampWait, pendingOutputKeys, progressReporter } from "./settle.js";
|
|
4
|
+
import { awaitImportsDone } from "./imports.js";
|
|
5
|
+
import { expressionTimeoutText, safeEval } from "./sandbox.js";
|
|
6
|
+
import { blockView, readStorageView } from "./block-storage.js";
|
|
7
|
+
import { findBlock, staleResults } from "./readiness.js";
|
|
8
|
+
import { readinessOf } from "./block-state.js";
|
|
5
9
|
import { z } from "zod";
|
|
6
|
-
import { isTimeoutError } from "@milaboratories/pl-middle-layer";
|
|
7
|
-
import { deriveDataFromStorage } from "@platforma-sdk/model";
|
|
8
10
|
// src/tools/await.ts
|
|
11
|
+
/**
|
|
12
|
+
* The default wait of `await_block_done`, in milliseconds: under the 60 s after which common MCP
|
|
13
|
+
* clients give up on a request (D-018).
|
|
14
|
+
*/
|
|
15
|
+
const AWAIT_DEFAULT_MS = 5e4;
|
|
16
|
+
/** The shortest wait `await_block_done` takes. */
|
|
17
|
+
const AWAIT_MIN_MS = 1e3;
|
|
18
|
+
/** The longest wait `await_block_done` takes, for clients whose request timeout is that long. */
|
|
19
|
+
const AWAIT_MAX_MS = 6e5;
|
|
20
|
+
/** Default timeout of a `transform` of `await_block_done`, in milliseconds. */
|
|
21
|
+
const DEFAULT_TRANSFORM_TIMEOUT = 5e3;
|
|
22
|
+
/** What a block in limbo means, and what brings it back. */
|
|
23
|
+
const LIMBO_NOTE = "The block is in limbo: its results are from before a block above it changed. That happens when a block above failed or was stopped, and also when a block above was run again: a run of an upstream block moves the settled blocks below it to limbo. run_block on this block runs it again, together with the stale blocks above it.";
|
|
24
|
+
/** What a block that is not calculated means. */
|
|
25
|
+
const NOT_CALCULATED_NOTE = "The block has not been run, or its run was reset before it finished. There is nothing to wait for: run_block starts it, then call await_block_done again.";
|
|
26
|
+
/** The block's overview fields every answer of `await_block_done` carries. */
|
|
27
|
+
function blockInfo(block) {
|
|
28
|
+
return {
|
|
29
|
+
id: block.id,
|
|
30
|
+
title: block.title ?? block.label,
|
|
31
|
+
calculationStatus: block.calculationStatus,
|
|
32
|
+
canRun: block.canRun,
|
|
33
|
+
stale: block.stale,
|
|
34
|
+
outputErrors: block.outputErrors
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
/** The answer for a block whose status ends the wait without results: not calculated, in limbo. */
|
|
38
|
+
async function stoppedAnswer(project, projectId, blockId, status, waitedMs, upstreamHalt) {
|
|
39
|
+
const { block, error } = await findBlock(project, projectId, blockId);
|
|
40
|
+
if (error) return error;
|
|
41
|
+
const state = await project.getBlockState(blockId).getValue();
|
|
42
|
+
const pendingOutputs = pendingOutputKeys(state.outputs);
|
|
43
|
+
const notes = [status === "Limbo" ? LIMBO_NOTE : NOT_CALCULATED_NOTE];
|
|
44
|
+
if (upstreamHalt !== void 0 && pendingOutputs.length > 0) notes.push(`Some outputs wait for block ${upstreamHalt.waitingOn} above, which ` + (upstreamHalt.running ? "is running: await_block_done on it, then on this block." : "has not run: run_block on this block runs it with the stale blocks above."));
|
|
45
|
+
return textResult({
|
|
46
|
+
status,
|
|
47
|
+
note: notes.join(" "),
|
|
48
|
+
block: blockInfo(block),
|
|
49
|
+
waitedMs,
|
|
50
|
+
readiness: await readinessOf(project, blockId),
|
|
51
|
+
...blockView(state),
|
|
52
|
+
outputsSettled: pendingOutputs.length === 0,
|
|
53
|
+
...upstreamHalt !== void 0 && pendingOutputs.length > 0 ? {
|
|
54
|
+
pendingOutputs,
|
|
55
|
+
waitingOn: upstreamHalt.waitingOn,
|
|
56
|
+
waitingOnStatus: upstreamHalt.waitingOnStatus
|
|
57
|
+
} : {}
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Waits for a block's run to finish and its outputs to settle, and with `waitForImports` for its
|
|
62
|
+
* files to upload; returns the result of `await_block_done`. One clock and one progress reporter
|
|
63
|
+
* run across both waits, so the progress a client sees only grows. A block that was never run or
|
|
64
|
+
* is in limbo has no run to wait for, but its imports are still waited for when asked: its
|
|
65
|
+
* pre-run may be uploading files. Its outputs are waited for first, within the same deadline: an
|
|
66
|
+
* output still computing may be the one that names the files, so no imports count as done only
|
|
67
|
+
* once the outputs settled (or wait for a block above that is running or has not run). A block
|
|
68
|
+
* somebody runs while that wait goes on is answered as such (not finished, call again), so the
|
|
69
|
+
* answer never says a running block has not run.
|
|
70
|
+
*/
|
|
71
|
+
async function awaitBlockDone(project, input, options = {}) {
|
|
72
|
+
const { projectId, blockId, transform, waitForImports } = input;
|
|
73
|
+
const { call } = options;
|
|
74
|
+
const found = await findBlock(project, projectId, blockId);
|
|
75
|
+
if (found.error) return found.error;
|
|
76
|
+
const timeoutMs = clampWait(input.timeout, AWAIT_DEFAULT_MS, AWAIT_MIN_MS, AWAIT_MAX_MS);
|
|
77
|
+
const start = Date.now();
|
|
78
|
+
const report = progressReporter(call, "Waiting for the block to finish");
|
|
79
|
+
const progress = (message) => report === void 0 ? void 0 : () => report(Date.now() - start, timeoutMs, message);
|
|
80
|
+
const signal = call?.mcpReq.signal;
|
|
81
|
+
const timeLeft = () => Math.max(0, timeoutMs - (Date.now() - start));
|
|
82
|
+
/** The block's calculation status now; `undefined` when it cannot be read. */
|
|
83
|
+
const statusNow = async () => {
|
|
84
|
+
return (await findBlock(project, projectId, blockId)).block?.calculationStatus;
|
|
85
|
+
};
|
|
86
|
+
/** Waits for the block's imports with what is left of the time; `undefined` when they are in. */
|
|
87
|
+
const importsLeft = async (status) => {
|
|
88
|
+
const imports = await awaitImportsDone(project.getBlockState(blockId), {
|
|
89
|
+
timeoutMs: timeLeft(),
|
|
90
|
+
signal,
|
|
91
|
+
onProgress: progress("Waiting for the block's imports")
|
|
92
|
+
});
|
|
93
|
+
if (imports.done) return { state: imports.state };
|
|
94
|
+
const now = await statusNow() ?? status;
|
|
95
|
+
return { result: timedOutResult({
|
|
96
|
+
status: now,
|
|
97
|
+
settled: now === "Done",
|
|
98
|
+
waitedMs: Date.now() - start,
|
|
99
|
+
imports: imports.imports,
|
|
100
|
+
hint: (now === "Done" ? "The block's run is over but some of its files " : now === "Running" ? "The block is running; some of its files " : "The block has not run; some of the files its data names ") + "are still uploading or being indexed. Call await_block_done with waitForImports again."
|
|
101
|
+
}) };
|
|
102
|
+
};
|
|
103
|
+
const stopped = async (status) => {
|
|
104
|
+
if (waitForImports !== true) return await stoppedAnswer(project, projectId, blockId, status, Date.now() - start);
|
|
105
|
+
const outputs = await awaitBlockSettled(project, blockId, {
|
|
106
|
+
timeoutMs: timeLeft(),
|
|
107
|
+
signal,
|
|
108
|
+
waitForRun: false,
|
|
109
|
+
onProgress: progress("Waiting for the block's outputs to settle")
|
|
110
|
+
});
|
|
111
|
+
if (outputs.stoppedBy === "block-gone") return blockGone();
|
|
112
|
+
if (outputs.stoppedBy === "timeout" || outputs.stoppedBy === "aborted") return timedOutResult({
|
|
113
|
+
status,
|
|
114
|
+
settled: false,
|
|
115
|
+
waitedMs: Date.now() - start,
|
|
116
|
+
pendingOutputs: outputs.pendingOutputs,
|
|
117
|
+
hint: "The block has not run; some of its outputs are still computing, and they may name files to upload. Call await_block_done with waitForImports again."
|
|
118
|
+
});
|
|
119
|
+
const afterOutputs = await stillStopped(status);
|
|
120
|
+
if (afterOutputs.result !== void 0) return afterOutputs.result;
|
|
121
|
+
const left = await importsLeft(afterOutputs.status);
|
|
122
|
+
if (left.result !== void 0) return left.result;
|
|
123
|
+
const halt = outputs.waitingOn === void 0 ? void 0 : {
|
|
124
|
+
waitingOn: outputs.waitingOn,
|
|
125
|
+
waitingOnStatus: outputs.waitingOnStatus,
|
|
126
|
+
running: outputs.stoppedBy === "upstream-running"
|
|
127
|
+
};
|
|
128
|
+
const afterImports = await stillStopped(afterOutputs.status);
|
|
129
|
+
if (afterImports.result !== void 0) return afterImports.result;
|
|
130
|
+
return await stoppedAnswer(project, projectId, blockId, afterImports.status, Date.now() - start, halt);
|
|
131
|
+
};
|
|
132
|
+
/**
|
|
133
|
+
* After a wait on a block that had not run, its status now: still not calculated or in limbo,
|
|
134
|
+
* or, when somebody ran it meanwhile, an answer saying so, for the agent to wait for the run.
|
|
135
|
+
*/
|
|
136
|
+
const stillStopped = async (status) => {
|
|
137
|
+
const now = await findBlock(project, projectId, blockId);
|
|
138
|
+
if (now.error) return { result: now.error };
|
|
139
|
+
const current = now.block.calculationStatus;
|
|
140
|
+
if (current === "NotCalculated" || current === "Limbo") return { status: current };
|
|
141
|
+
if (current === void 0) return { status };
|
|
142
|
+
return { result: timedOutResult({
|
|
143
|
+
status: current,
|
|
144
|
+
settled: false,
|
|
145
|
+
waitedMs: Date.now() - start,
|
|
146
|
+
hint: "The block was run while waiting. Call await_block_done again to wait for the run."
|
|
147
|
+
}) };
|
|
148
|
+
};
|
|
149
|
+
const blockGone = () => errorResult(`Block ${blockId} was removed from project ${projectId} while waiting.`, "Use get_project_overview to list the block ids of the project.");
|
|
150
|
+
/** Waits for the block's run with what is left of the time, then answers. */
|
|
151
|
+
const awaitRun = async () => {
|
|
152
|
+
const settle = await awaitBlockSettled(project, blockId, {
|
|
153
|
+
timeoutMs: timeLeft(),
|
|
154
|
+
signal,
|
|
155
|
+
waitForRun: true,
|
|
156
|
+
onProgress: progress("Waiting for the block to finish")
|
|
157
|
+
});
|
|
158
|
+
if (settle.stoppedBy === "block-gone") return blockGone();
|
|
159
|
+
if (settle.stoppedBy === "not-calculated") return await stopped("NotCalculated");
|
|
160
|
+
if (settle.stoppedBy === "limbo") return await stopped("Limbo");
|
|
161
|
+
if (!settle.settled) return timedOutResult({
|
|
162
|
+
status: settle.calculationStatus ?? "Unknown",
|
|
163
|
+
settled: false,
|
|
164
|
+
waitedMs: Date.now() - start,
|
|
165
|
+
pendingOutputs: settle.pendingOutputs,
|
|
166
|
+
...settle.waitingOn !== void 0 ? { waitingOn: settle.waitingOn } : {},
|
|
167
|
+
runningUpstreams: settle.runningUpstreams,
|
|
168
|
+
hint: settle.stoppedBy === "upstream-running" && settle.waitingOn !== void 0 ? `The outputs wait for block ${settle.waitingOn} above, which is running: await_block_done on it, then on this block.` : settle.calculationStatus === "Running" ? "Not finished: the block is still running. Call await_block_done again." : "Not finished: some outputs are still computing. Call await_block_done again."
|
|
169
|
+
});
|
|
170
|
+
let state = settle.state;
|
|
171
|
+
if (waitForImports === true) {
|
|
172
|
+
const left = await importsLeft("Done");
|
|
173
|
+
if (left.result !== void 0) return left.result;
|
|
174
|
+
state = left.state;
|
|
175
|
+
}
|
|
176
|
+
const { block, error } = await findBlock(project, projectId, blockId);
|
|
177
|
+
if (error) return error;
|
|
178
|
+
const info = blockInfo(block);
|
|
179
|
+
if (transform) try {
|
|
180
|
+
const result = await safeEval(transform, {
|
|
181
|
+
data: readStorageView(state.blockStorage).data,
|
|
182
|
+
outputs: state.outputs,
|
|
183
|
+
block: info
|
|
184
|
+
}, input.transformTimeout ?? DEFAULT_TRANSFORM_TIMEOUT);
|
|
185
|
+
return textResult({
|
|
186
|
+
status: "Done",
|
|
187
|
+
block: info,
|
|
188
|
+
...staleResults(block),
|
|
189
|
+
result
|
|
190
|
+
});
|
|
191
|
+
} catch (e) {
|
|
192
|
+
return transformFailedResult(e, "Check your JS expression syntax. Available variables: data, outputs, block.");
|
|
193
|
+
}
|
|
194
|
+
return textResult({
|
|
195
|
+
status: "Done",
|
|
196
|
+
...staleResults(block),
|
|
197
|
+
block: info,
|
|
198
|
+
waitedMs: Date.now() - start,
|
|
199
|
+
readiness: await readinessOf(project, blockId),
|
|
200
|
+
...blockView(state)
|
|
201
|
+
});
|
|
202
|
+
};
|
|
203
|
+
const initial = found.block.calculationStatus;
|
|
204
|
+
if (initial === "NotCalculated" || initial === "Limbo") return await stopped(initial);
|
|
205
|
+
return await awaitRun();
|
|
206
|
+
}
|
|
9
207
|
function registerAwaitTools(server, ctx) {
|
|
10
208
|
server.registerTool("await_block_done", {
|
|
11
209
|
title: "Wait for Block",
|
|
12
210
|
annotations: hints.read,
|
|
13
|
-
description:
|
|
211
|
+
description: `Wait for a block's run to finish and its outputs to settle. Returns the block status and the block view of get_block_state (data, plugins, output summary, readiness); large data comes as an outline, as there. Use \`transform\` to extract specific data server-side on completion.
|
|
212
|
+
|
|
213
|
+
Waits up to \`timeout\` (default ${AWAIT_DEFAULT_MS} ms, under the 60 s after which most clients give up; raise it only when your client waits longer). A wait that runs out is not an error: \`{ timedOut: true, status, pendingOutputs, waitingOn?, runningUpstreams }\`, the block is not finished: call again. A block that was never run (status NotCalculated) or is in limbo (results from before a block above changed: it failed, was stopped, or ran again) returns with a \`note\`; run_block starts it. A block whose results are from an earlier configuration (its data, or the data of a block above, changed after the run) is Done with \`stale: true\` and a \`note\`. \`waitForImports\` also waits for the files the block imports (its \`imports\` summary), for a block that has not run too, once its pre-run outputs settle: its pre-run uploads the files its data names.`,
|
|
14
214
|
inputSchema: z.object({
|
|
15
215
|
projectId: projectIdInput.describe("Project ID"),
|
|
16
216
|
blockId: blockIdInput.describe("Block ID to wait for"),
|
|
17
|
-
timeout: z.number().optional().default(
|
|
18
|
-
transform: z.string().optional().describe("JS expression evaluated server-side when block completes. Available variables: `data` (block
|
|
19
|
-
transformTimeout: z.number().optional().default(
|
|
217
|
+
timeout: z.number().optional().default(AWAIT_DEFAULT_MS).describe(`The longest wait in ms (default ${AWAIT_DEFAULT_MS}, from ${AWAIT_MIN_MS} to ${AWAIT_MAX_MS}).`),
|
|
218
|
+
transform: z.string().optional().describe("JS expression evaluated server-side when block completes. Available variables: `data` (block data), `outputs` (raw outputs), `block` (status info). Omit for default concise summary."),
|
|
219
|
+
transformTimeout: z.number().optional().default(DEFAULT_TRANSFORM_TIMEOUT).describe(expressionTimeoutText("transform evaluation", DEFAULT_TRANSFORM_TIMEOUT)),
|
|
220
|
+
waitForImports: z.boolean().optional().describe("Also wait until the files the block imports are uploaded or indexed (its `imports` summary has nothing left to finish), within the same `timeout` (default false).")
|
|
20
221
|
})
|
|
21
|
-
}, async (
|
|
22
|
-
|
|
23
|
-
const deadline = Date.now() + timeout;
|
|
24
|
-
while (Date.now() < deadline) {
|
|
25
|
-
const overview = await project.overview.getValue();
|
|
26
|
-
if (!overview) continue;
|
|
27
|
-
const block = overview.blocks.find((b) => b.id === blockId);
|
|
28
|
-
if (!block) return errorResult(`Block ${blockId} not found in project ${projectId}.`, "Use get_project_overview to list all block IDs in this project.");
|
|
29
|
-
if (block.calculationStatus === "Limbo") return errorResult("Block entered Limbo state (upstream failed or was stopped).", "Check upstream blocks with get_project_overview. Fix or re-run the failed upstream, then retry.");
|
|
30
|
-
if (block.calculationStatus === "NotCalculated") return errorResult("Block has not been started.", "Use run_block to start it first, then call await_block_done.");
|
|
31
|
-
if (block.calculationStatus === "Done") {
|
|
32
|
-
const remaining = Math.max(deadline - Date.now(), 1e3);
|
|
33
|
-
let state;
|
|
34
|
-
try {
|
|
35
|
-
state = await project.getBlockState(blockId).awaitStableValue(AbortSignal.timeout(remaining));
|
|
36
|
-
} catch (e) {
|
|
37
|
-
if (isTimeoutError(e)) return timedOutResult({
|
|
38
|
-
status: "Done",
|
|
39
|
-
note: "Computation done but outputs did not stabilize in time. Retry with a longer timeout."
|
|
40
|
-
});
|
|
41
|
-
return errorResult(`Failed to get block state: ${e instanceof Error ? e.message : String(e)}`);
|
|
42
|
-
}
|
|
43
|
-
const data = deriveDataFromStorage(state.blockStorage);
|
|
44
|
-
const blockInfo = {
|
|
45
|
-
id: block.id,
|
|
46
|
-
title: block.title ?? block.label,
|
|
47
|
-
calculationStatus: block.calculationStatus,
|
|
48
|
-
canRun: block.canRun,
|
|
49
|
-
stale: block.stale,
|
|
50
|
-
outputErrors: block.outputErrors
|
|
51
|
-
};
|
|
52
|
-
if (transform) try {
|
|
53
|
-
const result = await safeEval(transform, {
|
|
54
|
-
data,
|
|
55
|
-
outputs: state.outputs,
|
|
56
|
-
block: blockInfo
|
|
57
|
-
}, transformTimeout);
|
|
58
|
-
return textResult({
|
|
59
|
-
status: "Done",
|
|
60
|
-
block: blockInfo,
|
|
61
|
-
result
|
|
62
|
-
});
|
|
63
|
-
} catch (e) {
|
|
64
|
-
return transformFailedResult(e, "Check your JS expression syntax. Available variables: data, outputs, block.");
|
|
65
|
-
}
|
|
66
|
-
return textResult({
|
|
67
|
-
status: "Done",
|
|
68
|
-
block: blockInfo,
|
|
69
|
-
data,
|
|
70
|
-
outputs: summarizeOutputs(state.outputs)
|
|
71
|
-
});
|
|
72
|
-
}
|
|
73
|
-
const pollStart = Date.now();
|
|
74
|
-
try {
|
|
75
|
-
const result = await project.overview.getFullValue();
|
|
76
|
-
await project.overview.awaitChange(AbortSignal.timeout(5e3), result.uTag);
|
|
77
|
-
} catch {}
|
|
78
|
-
const elapsed = Date.now() - pollStart;
|
|
79
|
-
if (elapsed < 500) await new Promise((r) => setTimeout(r, 500 - elapsed));
|
|
80
|
-
}
|
|
81
|
-
const block = (await project.overview.getValue())?.blocks.find((b) => b.id === blockId);
|
|
82
|
-
return timedOutResult({
|
|
83
|
-
status: block?.calculationStatus ?? "Unknown",
|
|
84
|
-
hint: "The block is still running. Call await_block_done again with a longer timeout."
|
|
85
|
-
});
|
|
222
|
+
}, async (input, call) => {
|
|
223
|
+
return await ctx.withProject(input.projectId, (project) => awaitBlockDone(project, input, { call }));
|
|
86
224
|
});
|
|
87
225
|
}
|
|
88
|
-
export { registerAwaitTools };
|
|
226
|
+
export { AWAIT_DEFAULT_MS, AWAIT_MAX_MS, AWAIT_MIN_MS, LIMBO_NOTE, NOT_CALCULATED_NOTE, awaitBlockDone, registerAwaitTools };
|
|
89
227
|
|
|
90
228
|
//# sourceMappingURL=await.js.map
|
package/dist/tools/await.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"await.js","names":[],"sources":["../../src/tools/await.ts"],"sourcesContent":["import { isTimeoutError } from \"@milaboratories/pl-middle-layer\";\nimport { deriveDataFromStorage } from \"@platforma-sdk/model\";\nimport { z } from \"zod\";\nimport { projectIdInput, blockIdInput } from \"./ids\";\nimport type { ToolContext, ToolRegistrar } from \"./types\";\nimport { summarizeOutputs } from \"./tokens\";\nimport { safeEval } from \"./sandbox\";\nimport { errorResult, hints, textResult, timedOutResult, transformFailedResult } from \"./types\";\n\nexport function registerAwaitTools(server: ToolRegistrar, ctx: ToolContext): void {\n server.registerTool(\n \"await_block_done\",\n {\n title: \"Wait for Block\",\n annotations: hints.read,\n description:\n \"Wait for a block to finish computation and outputs to stabilize. \" +\n \"Returns block status, data, and concise output summary with token estimates. \" +\n \"Use `transform` to extract specific data server-side on completion.\",\n inputSchema: z.object({\n projectId: projectIdInput.describe(\"Project ID\"),\n blockId: blockIdInput.describe(\"Block ID to wait for\"),\n timeout: z.number().optional().default(120000).describe(\"Timeout in ms (default 120000)\"),\n transform: z\n .string()\n .optional()\n .describe(\n \"JS expression evaluated server-side when block completes. \" +\n \"Available variables: `data` (block args), `outputs` (raw outputs), `block` (status info). \" +\n \"Omit for default concise summary.\",\n ),\n transformTimeout: z\n .number()\n .optional()\n .default(5000)\n .describe(\"Timeout in ms for transform evaluation (default 5000).\"),\n }),\n },\n async ({ projectId, blockId, timeout, transform, transformTimeout }) => {\n const project = await ctx.getOpenedProject(projectId);\n const deadline = Date.now() + timeout;\n\n while (Date.now() < deadline) {\n const overview = await project.overview.getValue();\n if (!overview) continue;\n const block = overview.blocks.find((b) => b.id === blockId);\n if (!block)\n return errorResult(\n `Block ${blockId} not found in project ${projectId}.`,\n \"Use get_project_overview to list all block IDs in this project.\",\n );\n\n // Terminal error states — return immediately\n if (block.calculationStatus === \"Limbo\") {\n return errorResult(\n \"Block entered Limbo state (upstream failed or was stopped).\",\n \"Check upstream blocks with get_project_overview. Fix or re-run the failed upstream, then retry.\",\n );\n }\n\n if (block.calculationStatus === \"NotCalculated\") {\n return errorResult(\n \"Block has not been started.\",\n \"Use run_block to start it first, then call await_block_done.\",\n );\n }\n\n if (block.calculationStatus === \"Done\") {\n // Await stable block state with remaining time budget\n const remaining = Math.max(deadline - Date.now(), 1000);\n let state;\n try {\n state = await project\n .getBlockState(blockId)\n .awaitStableValue(AbortSignal.timeout(remaining));\n } catch (e: unknown) {\n if (isTimeoutError(e)) {\n return timedOutResult({\n status: \"Done\",\n note: \"Computation done but outputs did not stabilize in time. Retry with a longer timeout.\",\n });\n }\n return errorResult(\n `Failed to get block state: ${e instanceof Error ? e.message : String(e)}`,\n );\n }\n\n const data = deriveDataFromStorage(state.blockStorage);\n\n const blockInfo = {\n id: block.id,\n title: block.title ?? block.label,\n calculationStatus: block.calculationStatus,\n canRun: block.canRun,\n stale: block.stale,\n outputErrors: block.outputErrors,\n };\n\n if (transform) {\n try {\n const result = await safeEval(\n transform,\n {\n data,\n outputs: state.outputs,\n block: blockInfo,\n },\n transformTimeout,\n );\n return textResult({ status: \"Done\", block: blockInfo, result });\n } catch (e: unknown) {\n return transformFailedResult(\n e,\n \"Check your JS expression syntax. Available variables: data, outputs, block.\",\n );\n }\n }\n\n return textResult({\n status: \"Done\",\n block: blockInfo,\n data,\n outputs: summarizeOutputs(state.outputs as Record<string, unknown> | undefined),\n });\n }\n\n // Still running — wait up to 5s for overview to change, then re-poll.\n // Minimum 500ms delay to avoid busy-looping if awaitChange resolves immediately.\n const pollStart = Date.now();\n try {\n const result = await project.overview.getFullValue();\n await project.overview.awaitChange(AbortSignal.timeout(5000), result.uTag);\n } catch {\n // timeout or abort — just re-poll\n }\n const elapsed = Date.now() - pollStart;\n if (elapsed < 500) {\n await new Promise((r) => setTimeout(r, 500 - elapsed));\n }\n }\n\n // Timed out while running\n const overview = await project.overview.getValue();\n const block = overview?.blocks.find((b) => b.id === blockId);\n return timedOutResult({\n status: block?.calculationStatus ?? \"Unknown\",\n hint: \"The block is still running. Call await_block_done again with a longer timeout.\",\n });\n },\n );\n}\n"],"mappings":";;;;;;;;AASA,SAAgB,mBAAmB,QAAuB,KAAwB;CAChF,OAAO,aACL,oBACA;EACE,OAAO;EACP,aAAa,MAAM;EACnB,aACE;EAGF,aAAa,EAAE,OAAO;GACpB,WAAW,eAAe,SAAS,YAAY;GAC/C,SAAS,aAAa,SAAS,sBAAsB;GACrD,SAAS,EAAE,OAAO,CAAC,CAAC,SAAS,CAAC,CAAC,QAAQ,IAAM,CAAC,CAAC,SAAS,gCAAgC;GACxF,WAAW,EACR,OAAO,CAAC,CACR,SAAS,CAAC,CACV,SACC,uLAGF;GACF,kBAAkB,EACf,OAAO,CAAC,CACR,SAAS,CAAC,CACV,QAAQ,GAAI,CAAC,CACb,SAAS,wDAAwD;EACtE,CAAC;CACH,GACA,OAAO,EAAE,WAAW,SAAS,SAAS,WAAW,uBAAuB;EACtE,MAAM,UAAU,MAAM,IAAI,iBAAiB,SAAS;EACpD,MAAM,WAAW,KAAK,IAAI,IAAI;EAE9B,OAAO,KAAK,IAAI,IAAI,UAAU;GAC5B,MAAM,WAAW,MAAM,QAAQ,SAAS,SAAS;GACjD,IAAI,CAAC,UAAU;GACf,MAAM,QAAQ,SAAS,OAAO,MAAM,MAAM,EAAE,OAAO,OAAO;GAC1D,IAAI,CAAC,OACH,OAAO,YACL,SAAS,QAAQ,wBAAwB,UAAU,IACn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I,SAAS,MAAM,WAAW,GAAG,MAAM,OAAO,CAAC;EAEzD;EAIA,MAAM,SAAQ,MADS,QAAQ,SAAS,SAAS,EAAA,EACzB,OAAO,MAAM,MAAM,EAAE,OAAO,OAAO;EAC3D,OAAO,eAAe;GACpB,QAAQ,OAAO,qBAAqB;GACpC,MAAM;EACR,CAAC;CACH,CACF;AACF"}
|
|
1
|
+
{"version":3,"file":"await.js","names":[],"sources":["../../src/tools/await.ts"],"sourcesContent":["import type { BlockId, ProjectId } from \"@milaboratories/pl-model-common\";\nimport type { BlockReadiness, BlockStateOverview } from \"@milaboratories/pl-middle-layer\";\nimport { z } from \"zod\";\nimport { blockIdInput, projectIdInput } from \"./ids\";\nimport type { ToolContext, ToolRegistrar } from \"./types\";\nimport { blockView, readStorageView } from \"./block-storage\";\nimport { readinessOf } from \"./block-state\";\nimport { awaitImportsDone } from \"./imports\";\nimport { findBlock, staleResults } from \"./readiness\";\nimport { expressionTimeoutText, safeEval } from \"./sandbox\";\nimport {\n awaitBlockSettled,\n clampWait,\n pendingOutputKeys,\n progressReporter,\n type SettleStateComputable,\n type WaitContext,\n} from \"./settle\";\nimport { errorResult, hints, textResult, timedOutResult, transformFailedResult } from \"./types\";\n\n/**\n * The default wait of `await_block_done`, in milliseconds: under the 60 s after which common MCP\n * clients give up on a request (D-018).\n */\nexport const AWAIT_DEFAULT_MS = 50_000;\n/** The shortest wait `await_block_done` takes. */\nexport const AWAIT_MIN_MS = 1_000;\n/** The longest wait `await_block_done` takes, for clients whose request timeout is that long. */\nexport const AWAIT_MAX_MS = 600_000;\n\n/** Default timeout of a `transform` of `await_block_done`, in milliseconds. */\nconst DEFAULT_TRANSFORM_TIMEOUT = 5000;\n\n/** What a block in limbo means, and what brings it back. */\nexport const LIMBO_NOTE =\n \"The block is in limbo: its results are from before a block above it changed. That happens \" +\n \"when a block above failed or was stopped, and also when a block above was run again: a run \" +\n \"of an upstream block moves the settled blocks below it to limbo. run_block on this block \" +\n \"runs it again, together with the stale blocks above it.\";\n\n/** What a block that is not calculated means. */\nexport const NOT_CALCULATED_NOTE =\n \"The block has not been run, or its run was reset before it finished. There is nothing to \" +\n \"wait for: run_block starts it, then call await_block_done again.\";\n\n/** What `await_block_done` reads of a block of the project overview. */\nexport type AwaitOverviewBlock = Pick<\n BlockStateOverview,\n | \"id\"\n | \"title\"\n | \"label\"\n | \"calculationStatus\"\n | \"canRun\"\n | \"stale\"\n | \"outputErrors\"\n | \"missingReference\"\n | \"upstreams\"\n>;\n\n/** What `await_block_done` reads of a block state. */\nexport type AwaitBlockState = {\n outputs?: Record<string, unknown> | undefined;\n blockStorage: unknown;\n};\n\n/** What `await_block_done` needs of a project; a middle-layer `Project` has it. */\nexport type AwaitProject = {\n overview: { getValue(): Promise<{ blocks: AwaitOverviewBlock[] } | undefined> };\n getBlockState(blockId: BlockId): SettleStateComputable<AwaitBlockState> & {\n getValue(): Promise<AwaitBlockState>;\n };\n getBlockReadiness(blockId: BlockId): { getValue(): Promise<BlockReadiness | undefined> };\n};\n\n/** What {@link awaitBlockDone} returns: the result of `await_block_done`. */\ntype AwaitResult = ReturnType<typeof textResult> | ReturnType<typeof errorResult>;\n\n/** Input of {@link awaitBlockDone}: the arguments of `await_block_done`. */\nexport type AwaitInput = {\n projectId: ProjectId;\n blockId: BlockId;\n timeout?: number;\n transform?: string;\n transformTimeout?: number;\n waitForImports?: boolean;\n};\n\n/** The block's overview fields every answer of `await_block_done` carries. */\nfunction blockInfo(block: AwaitOverviewBlock) {\n return {\n id: block.id,\n title: block.title ?? block.label,\n calculationStatus: block.calculationStatus,\n canRun: block.canRun,\n stale: block.stale,\n outputErrors: block.outputErrors,\n };\n}\n\n/** The answer for a block whose status ends the wait without results: not calculated, in limbo. */\nasync function stoppedAnswer(\n project: AwaitProject,\n projectId: ProjectId,\n blockId: BlockId,\n status: \"NotCalculated\" | \"Limbo\",\n waitedMs: number,\n upstreamHalt?: { waitingOn: BlockId; waitingOnStatus: string | undefined; running: boolean },\n) {\n const { block, error } = await findBlock(project, projectId, blockId);\n if (error) return error;\n const state = await project.getBlockState(blockId).getValue();\n const pendingOutputs = pendingOutputKeys(state.outputs);\n const notes = [status === \"Limbo\" ? LIMBO_NOTE : NOT_CALCULATED_NOTE];\n if (upstreamHalt !== undefined && pendingOutputs.length > 0)\n notes.push(\n `Some outputs wait for block ${upstreamHalt.waitingOn} above, which ` +\n (upstreamHalt.running\n ? \"is running: await_block_done on it, then on this block.\"\n : \"has not run: run_block on this block runs it with the stale blocks above.\"),\n );\n return textResult({\n status,\n note: notes.join(\" \"),\n block: blockInfo(block),\n waitedMs,\n readiness: await readinessOf(project, blockId),\n ...blockView(state),\n outputsSettled: pendingOutputs.length === 0,\n ...(upstreamHalt !== undefined && pendingOutputs.length > 0\n ? {\n pendingOutputs,\n waitingOn: upstreamHalt.waitingOn,\n waitingOnStatus: upstreamHalt.waitingOnStatus,\n }\n : {}),\n });\n}\n\n/**\n * Waits for a block's run to finish and its outputs to settle, and with `waitForImports` for its\n * files to upload; returns the result of `await_block_done`. One clock and one progress reporter\n * run across both waits, so the progress a client sees only grows. A block that was never run or\n * is in limbo has no run to wait for, but its imports are still waited for when asked: its\n * pre-run may be uploading files. Its outputs are waited for first, within the same deadline: an\n * output still computing may be the one that names the files, so no imports count as done only\n * once the outputs settled (or wait for a block above that is running or has not run). A block\n * somebody runs while that wait goes on is answered as such (not finished, call again), so the\n * answer never says a running block has not run.\n */\nexport async function awaitBlockDone(\n project: AwaitProject,\n input: AwaitInput,\n options: { call?: WaitContext } = {},\n): Promise<AwaitResult> {\n const { projectId, blockId, transform, waitForImports } = input;\n const { call } = options;\n const found = await findBlock(project, projectId, blockId);\n if (found.error) return found.error;\n\n const timeoutMs = clampWait(input.timeout, AWAIT_DEFAULT_MS, AWAIT_MIN_MS, AWAIT_MAX_MS);\n const start = Date.now();\n const report = progressReporter(call, \"Waiting for the block to finish\");\n const progress = (message: string) =>\n report === undefined ? undefined : () => report(Date.now() - start, timeoutMs, message);\n const signal = call?.mcpReq.signal;\n\n const timeLeft = () => Math.max(0, timeoutMs - (Date.now() - start));\n\n /** The block's calculation status now; `undefined` when it cannot be read. */\n const statusNow = async () => {\n const now = await findBlock(project, projectId, blockId);\n return now.block?.calculationStatus;\n };\n\n /** Waits for the block's imports with what is left of the time; `undefined` when they are in. */\n const importsLeft = async (status: string) => {\n const imports = await awaitImportsDone(project.getBlockState(blockId), {\n timeoutMs: timeLeft(),\n signal,\n onProgress: progress(\"Waiting for the block's imports\"),\n });\n if (imports.done) return { state: imports.state };\n // The status may have changed while the imports were waited for: somebody ran the block.\n const now = (await statusNow()) ?? status;\n return {\n result: timedOutResult({\n status: now,\n settled: now === \"Done\",\n waitedMs: Date.now() - start,\n imports: imports.imports,\n hint:\n (now === \"Done\"\n ? \"The block's run is over but some of its files \"\n : now === \"Running\"\n ? \"The block is running; some of its files \"\n : \"The block has not run; some of the files its data names \") +\n \"are still uploading or being indexed. Call await_block_done with waitForImports again.\",\n }),\n };\n };\n\n const stopped = async (status: \"NotCalculated\" | \"Limbo\"): Promise<AwaitResult> => {\n if (waitForImports !== true)\n return await stoppedAnswer(project, projectId, blockId, status, Date.now() - start);\n\n // The pre-run outputs first: until they settle, an empty progress map may only mean the\n // output that names the files has not computed yet.\n const outputs = await awaitBlockSettled(project, blockId, {\n timeoutMs: timeLeft(),\n signal,\n waitForRun: false,\n onProgress: progress(\"Waiting for the block's outputs to settle\"),\n });\n if (outputs.stoppedBy === \"block-gone\") return blockGone();\n if (outputs.stoppedBy === \"timeout\" || outputs.stoppedBy === \"aborted\")\n return timedOutResult({\n status,\n settled: false,\n waitedMs: Date.now() - start,\n pendingOutputs: outputs.pendingOutputs,\n hint:\n \"The block has not run; some of its outputs are still computing, and they may name \" +\n \"files to upload. Call await_block_done with waitForImports again.\",\n });\n\n // Somebody may have run the block while its outputs were waited for.\n const afterOutputs = await stillStopped(status);\n if (afterOutputs.result !== undefined) return afterOutputs.result;\n\n const left = await importsLeft(afterOutputs.status);\n if (left.result !== undefined) return left.result;\n const halt =\n outputs.waitingOn === undefined\n ? undefined\n : {\n waitingOn: outputs.waitingOn,\n waitingOnStatus: outputs.waitingOnStatus,\n running: outputs.stoppedBy === \"upstream-running\",\n };\n\n // ... or while its imports were.\n const afterImports = await stillStopped(afterOutputs.status);\n if (afterImports.result !== undefined) return afterImports.result;\n return await stoppedAnswer(\n project,\n projectId,\n blockId,\n afterImports.status,\n Date.now() - start,\n halt,\n );\n };\n\n /**\n * After a wait on a block that had not run, its status now: still not calculated or in limbo,\n * or, when somebody ran it meanwhile, an answer saying so, for the agent to wait for the run.\n */\n const stillStopped = async (\n status: \"NotCalculated\" | \"Limbo\",\n ): Promise<\n | { status: \"NotCalculated\" | \"Limbo\"; result?: undefined }\n | { status?: undefined; result: AwaitResult }\n > => {\n const now = await findBlock(project, projectId, blockId);\n if (now.error) return { result: now.error };\n const current = now.block.calculationStatus;\n if (current === \"NotCalculated\" || current === \"Limbo\") return { status: current };\n if (current === undefined) return { status };\n return {\n result: timedOutResult({\n status: current,\n settled: false,\n waitedMs: Date.now() - start,\n hint: \"The block was run while waiting. Call await_block_done again to wait for the run.\",\n }),\n };\n };\n\n const blockGone = () =>\n errorResult(\n `Block ${blockId} was removed from project ${projectId} while waiting.`,\n \"Use get_project_overview to list the block ids of the project.\",\n );\n\n /** Waits for the block's run with what is left of the time, then answers. */\n const awaitRun = async (): Promise<AwaitResult> => {\n const settle = await awaitBlockSettled(project, blockId, {\n timeoutMs: timeLeft(),\n signal,\n waitForRun: true,\n onProgress: progress(\"Waiting for the block to finish\"),\n });\n\n if (settle.stoppedBy === \"block-gone\") return blockGone();\n if (settle.stoppedBy === \"not-calculated\") return await stopped(\"NotCalculated\");\n if (settle.stoppedBy === \"limbo\") return await stopped(\"Limbo\");\n\n if (!settle.settled)\n return timedOutResult({\n status: settle.calculationStatus ?? \"Unknown\",\n settled: false,\n waitedMs: Date.now() - start,\n pendingOutputs: settle.pendingOutputs,\n ...(settle.waitingOn !== undefined ? { waitingOn: settle.waitingOn } : {}),\n runningUpstreams: settle.runningUpstreams,\n hint:\n settle.stoppedBy === \"upstream-running\" && settle.waitingOn !== undefined\n ? `The outputs wait for block ${settle.waitingOn} above, which is running: ` +\n \"await_block_done on it, then on this block.\"\n : settle.calculationStatus === \"Running\"\n ? \"Not finished: the block is still running. Call await_block_done again.\"\n : \"Not finished: some outputs are still computing. Call await_block_done again.\",\n });\n\n let state = settle.state;\n if (waitForImports === true) {\n const left = await importsLeft(\"Done\");\n if (left.result !== undefined) return left.result;\n state = left.state;\n }\n\n const { block, error } = await findBlock(project, projectId, blockId);\n if (error) return error;\n const info = blockInfo(block);\n\n if (transform) {\n try {\n const result = await safeEval(\n transform,\n {\n data: readStorageView(state.blockStorage).data,\n outputs: state.outputs,\n block: info,\n },\n input.transformTimeout ?? DEFAULT_TRANSFORM_TIMEOUT,\n );\n return textResult({ status: \"Done\", block: info, ...staleResults(block), result });\n } catch (e: unknown) {\n return transformFailedResult(\n e,\n \"Check your JS expression syntax. Available variables: data, outputs, block.\",\n );\n }\n }\n\n return textResult({\n status: \"Done\",\n ...staleResults(block),\n block: info,\n waitedMs: Date.now() - start,\n readiness: await readinessOf(project, blockId),\n ...blockView(state),\n });\n };\n\n const initial = found.block.calculationStatus;\n if (initial === \"NotCalculated\" || initial === \"Limbo\") return await stopped(initial);\n return await awaitRun();\n}\n\nexport function registerAwaitTools(server: ToolRegistrar, ctx: ToolContext): void {\n server.registerTool(\n \"await_block_done\",\n {\n title: \"Wait for Block\",\n annotations: hints.read,\n description:\n \"Wait for a block's run to finish and its outputs to settle. Returns the block status \" +\n \"and the block view of get_block_state (data, plugins, output summary, readiness); large \" +\n \"data comes as an outline, as there. Use `transform` to extract specific data \" +\n \"server-side on completion.\\n\\n\" +\n `Waits up to \\`timeout\\` (default ${AWAIT_DEFAULT_MS} ms, under the 60 s after which ` +\n \"most clients give up; raise it only when your client waits longer). A wait that runs \" +\n \"out is not an error: `{ timedOut: true, status, pendingOutputs, waitingOn?, \" +\n \"runningUpstreams }`, the block is not finished: call again. A block that was never run \" +\n \"(status NotCalculated) or is in limbo (results from before a block above changed: it \" +\n \"failed, was stopped, or ran again) returns with a `note`; run_block starts it. A \" +\n \"block whose results are from an earlier configuration (its data, or the data of a \" +\n \"block above, changed after the run) is Done with `stale: true` and a `note`. \" +\n \"`waitForImports` also waits for the files the block imports (its `imports` summary), \" +\n \"for a block that has not run too, once its pre-run outputs settle: its pre-run \" +\n \"uploads the files its data names.\",\n inputSchema: z.object({\n projectId: projectIdInput.describe(\"Project ID\"),\n blockId: blockIdInput.describe(\"Block ID to wait for\"),\n timeout: z\n .number()\n .optional()\n .default(AWAIT_DEFAULT_MS)\n .describe(\n `The longest wait in ms (default ${AWAIT_DEFAULT_MS}, from ${AWAIT_MIN_MS} to ` +\n `${AWAIT_MAX_MS}).`,\n ),\n transform: z\n .string()\n .optional()\n .describe(\n \"JS expression evaluated server-side when block completes. \" +\n \"Available variables: `data` (block data), `outputs` (raw outputs), `block` (status info). \" +\n \"Omit for default concise summary.\",\n ),\n transformTimeout: z\n .number()\n .optional()\n .default(DEFAULT_TRANSFORM_TIMEOUT)\n .describe(expressionTimeoutText(\"transform evaluation\", DEFAULT_TRANSFORM_TIMEOUT)),\n waitForImports: z\n .boolean()\n .optional()\n .describe(\n \"Also wait until the files the block imports are uploaded or indexed (its `imports` \" +\n \"summary has nothing left to finish), within the same `timeout` (default false).\",\n ),\n }),\n },\n async (input, call) => {\n return await ctx.withProject(input.projectId, (project) =>\n awaitBlockDone(project, input, { call }),\n );\n },\n );\n}\n"],"mappings":";;;;;;;;;;;;;;AAwBA,MAAa,mBAAmB;;AAEhC,MAAa,eAAe;;AAE5B,MAAa,eAAe;;AAG5B,MAAM,4BAA4B;;AAGlC,MAAa,aACX;;AAMF,MAAa,sBACX;;AA8CF,SAAS,UAAU,OAA2B;CAC5C,OAAO;EACL,IAAI,MAAM;EACV,OAAO,MAAM,SAAS,MAAM;EAC5B,mBAAmB,MAAM;EACzB,Q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GACrD,SAAS,EACN,OAAO,CAAC,CACR,SAAS,CAAC,CACV,QAAQ,gBAAgB,CAAC,CACzB,SACC,mCAAmC,iBAAiB,SAAS,aAAa,MACrE,aAAa,GACpB;GACF,WAAW,EACR,OAAO,CAAC,CACR,SAAS,CAAC,CACV,SACC,uLAGF;GACF,kBAAkB,EACf,OAAO,CAAC,CACR,SAAS,CAAC,CACV,QAAQ,yBAAyB,CAAC,CAClC,SAAS,sBAAsB,wBAAwB,yBAAyB,CAAC;GACpF,gBAAgB,EACb,QAAQ,CAAC,CACT,SAAS,CAAC,CACV,SACC,oKAEF;EACJ,CAAC;CACH,GACA,OAAO,OAAO,SAAS;EACrB,OAAO,MAAM,IAAI,YAAY,MAAM,YAAY,YAC7C,eAAe,SAAS,OAAO,EAAE,KAAK,CAAC,CACzC;CACF,CACF;AACF"}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
const require_types = require("./types.cjs");
|
|
2
2
|
const require_ids = require("./ids.cjs");
|
|
3
|
+
const require_readiness = require("./readiness.cjs");
|
|
3
4
|
let zod = require("zod");
|
|
4
5
|
// src/tools/block-action-context.ts
|
|
5
6
|
/** Finds the node with the context ID in the tree, or undefined. */
|
|
@@ -31,7 +32,7 @@ function registerBlockActionContextTools(server, ctx) {
|
|
|
31
32
|
annotations: require_types.hints.read,
|
|
32
33
|
description: "Get the block action context tree of a block run: one node per template render, each with its baggage (name, block-pack, block-id, run-id), the render it belongs to (resource), its action transition in time order (namespace, reporting resource, state, detail, reason), its readings (namespace, resource, key, value) and its child renders. Use it to see which step a block is in or where it waits. The result is cut to maxDepth levels and the latest maxTransitions per node; a node with childrenOmitted or transitionsOmitted has more. Pass its contextId to read below it. Reads the current state; call again to refresh.",
|
|
33
34
|
inputSchema: zod.z.object({
|
|
34
|
-
projectId: require_ids.projectIdInput.describe("Project ID
|
|
35
|
+
projectId: require_ids.projectIdInput.describe("Project ID"),
|
|
35
36
|
blockId: require_ids.blockIdInput.describe("Block ID"),
|
|
36
37
|
mode: zod.z.enum(["prod", "staging"]).optional().describe("Which render of the block: 'prod' (default) or 'staging'"),
|
|
37
38
|
contextId: zod.z.string().optional().describe("Return the subtree of this action context instead of the root"),
|
|
@@ -39,14 +40,18 @@ function registerBlockActionContextTools(server, ctx) {
|
|
|
39
40
|
maxTransitions: zod.z.number().int().min(0).optional().default(20).describe("Latest transitions to include per node (default 20)")
|
|
40
41
|
})
|
|
41
42
|
}, async ({ projectId, blockId, mode, contextId, maxDepth, maxTransitions }) => {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
43
|
+
return await ctx.withProject(projectId, async (project) => {
|
|
44
|
+
const { error } = await require_readiness.findBlock(project, projectId, blockId);
|
|
45
|
+
if (error) return error;
|
|
46
|
+
const tree = await project.getBlockActionContext(blockId, mode ?? "prod");
|
|
47
|
+
if (tree === void 0) return require_types.errorResult("Block has no action context tree: it was not rendered yet, or the backend does not support block action context.");
|
|
48
|
+
const node = contextId === void 0 ? tree : findBlockActionContextNode(tree, contextId);
|
|
49
|
+
if (node === void 0) return require_types.errorResult(`Action context ${contextId} is not in the action context tree of the block. Call without contextId to see the tree.`);
|
|
50
|
+
return require_types.textResult(limitBlockActionContextTree(node, {
|
|
51
|
+
maxDepth,
|
|
52
|
+
maxTransitions
|
|
53
|
+
}));
|
|
54
|
+
});
|
|
50
55
|
});
|
|
51
56
|
}
|
|
52
57
|
exports.findBlockActionContextNode = findBlockActionContextNode;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"block-action-context.cjs","names":["hints","z","projectIdInput","blockIdInput","errorResult","textResult"],"sources":["../../src/tools/block-action-context.ts"],"sourcesContent":["import type { BlockActionContextNode } from \"@milaboratories/pl-middle-layer\";\nimport { z } from \"zod\";\nimport { projectIdInput, blockIdInput } from \"./ids\";\nimport type { ToolContext, ToolRegistrar } from \"./types\";\nimport { errorResult, hints, textResult } from \"./types\";\n\n/** An action context tree node as the tool returns it: cut to the requested depth and history. */\nexport type LimitedBlockActionContextNode = Omit<BlockActionContextNode, \"children\"> & {\n children: LimitedBlockActionContextNode[];\n /** Older transitions left out of this node. */\n transitionsOmitted?: number;\n /** Child renders left out below the depth limit. Request this node by contextId to see them. */\n childrenOmitted?: number;\n};\n\n/** Options of {@link limitBlockActionContextTree}. */\nexport type BlockActionContextTreeLimits = {\n /** Levels of children to include under the returned node. 0 returns the node only. */\n maxDepth: number;\n /** The latest transitions to keep per node. */\n maxTransitions: number;\n};\n\n/** Finds the node with the context ID in the tree, or undefined. */\nexport function findBlockActionContextNode(\n tree: BlockActionContextNode,\n contextId: string,\n): BlockActionContextNode | undefined {\n if (tree.contextId === contextId) return tree;\n for (const child of tree.children) {\n const found = findBlockActionContextNode(child, contextId);\n if (found !== undefined) return found;\n }\n return undefined;\n}\n\n/** Cuts an action context tree to maxDepth levels of children and the latest maxTransitions per node. */\nexport function limitBlockActionContextTree(\n node: BlockActionContextNode,\n limits: BlockActionContextTreeLimits,\n): LimitedBlockActionContextNode {\n const transitionsOmitted = Math.max(0, node.transitions.length - limits.maxTransitions);\n const limited: LimitedBlockActionContextNode = {\n ...node,\n transitions: node.transitions.slice(transitionsOmitted),\n children:\n limits.maxDepth > 0\n ? node.children.map((child) =>\n limitBlockActionContextTree(child, { ...limits, maxDepth: limits.maxDepth - 1 }),\n )\n : [],\n };\n if (transitionsOmitted > 0) limited.transitionsOmitted = transitionsOmitted;\n if (limits.maxDepth <= 0 && node.children.length > 0)\n limited.childrenOmitted = node.children.length;\n return limited;\n}\n\nexport function registerBlockActionContextTools(server: ToolRegistrar, ctx: ToolContext): void {\n server.registerTool(\n \"get_block_action_context\",\n {\n title: \"Get Block Action Context\",\n annotations: hints.read,\n description:\n \"Get the block action context tree of a block run: one node per template render, each with its baggage \" +\n \"(name, block-pack, block-id, run-id), the render it belongs to (resource), its action transition in time order \" +\n \"(namespace, reporting resource, state, detail, reason), its readings (namespace, resource, key, \" +\n \"value) and its child renders. Use it to see which step a block is in or where it waits. \" +\n \"The result is cut to maxDepth levels and the latest maxTransitions per node; a node with \" +\n \"childrenOmitted or transitionsOmitted has more. Pass its contextId to read below it. \" +\n \"Reads the current state; call again to refresh.\",\n inputSchema: z.object({\n projectId: projectIdInput.describe(\"Project ID
|
|
1
|
+
{"version":3,"file":"block-action-context.cjs","names":["hints","z","projectIdInput","blockIdInput","findBlock","errorResult","textResult"],"sources":["../../src/tools/block-action-context.ts"],"sourcesContent":["import type { BlockActionContextNode } from \"@milaboratories/pl-middle-layer\";\nimport { z } from \"zod\";\nimport { projectIdInput, blockIdInput } from \"./ids\";\nimport type { ToolContext, ToolRegistrar } from \"./types\";\nimport { errorResult, hints, textResult } from \"./types\";\nimport { findBlock } from \"./readiness\";\n\n/** An action context tree node as the tool returns it: cut to the requested depth and history. */\nexport type LimitedBlockActionContextNode = Omit<BlockActionContextNode, \"children\"> & {\n children: LimitedBlockActionContextNode[];\n /** Older transitions left out of this node. */\n transitionsOmitted?: number;\n /** Child renders left out below the depth limit. Request this node by contextId to see them. */\n childrenOmitted?: number;\n};\n\n/** Options of {@link limitBlockActionContextTree}. */\nexport type BlockActionContextTreeLimits = {\n /** Levels of children to include under the returned node. 0 returns the node only. */\n maxDepth: number;\n /** The latest transitions to keep per node. */\n maxTransitions: number;\n};\n\n/** Finds the node with the context ID in the tree, or undefined. */\nexport function findBlockActionContextNode(\n tree: BlockActionContextNode,\n contextId: string,\n): BlockActionContextNode | undefined {\n if (tree.contextId === contextId) return tree;\n for (const child of tree.children) {\n const found = findBlockActionContextNode(child, contextId);\n if (found !== undefined) return found;\n }\n return undefined;\n}\n\n/** Cuts an action context tree to maxDepth levels of children and the latest maxTransitions per node. */\nexport function limitBlockActionContextTree(\n node: BlockActionContextNode,\n limits: BlockActionContextTreeLimits,\n): LimitedBlockActionContextNode {\n const transitionsOmitted = Math.max(0, node.transitions.length - limits.maxTransitions);\n const limited: LimitedBlockActionContextNode = {\n ...node,\n transitions: node.transitions.slice(transitionsOmitted),\n children:\n limits.maxDepth > 0\n ? node.children.map((child) =>\n limitBlockActionContextTree(child, { ...limits, maxDepth: limits.maxDepth - 1 }),\n )\n : [],\n };\n if (transitionsOmitted > 0) limited.transitionsOmitted = transitionsOmitted;\n if (limits.maxDepth <= 0 && node.children.length > 0)\n limited.childrenOmitted = node.children.length;\n return limited;\n}\n\nexport function registerBlockActionContextTools(server: ToolRegistrar, ctx: ToolContext): void {\n server.registerTool(\n \"get_block_action_context\",\n {\n title: \"Get Block Action Context\",\n annotations: hints.read,\n description:\n \"Get the block action context tree of a block run: one node per template render, each with its baggage \" +\n \"(name, block-pack, block-id, run-id), the render it belongs to (resource), its action transition in time order \" +\n \"(namespace, reporting resource, state, detail, reason), its readings (namespace, resource, key, \" +\n \"value) and its child renders. Use it to see which step a block is in or where it waits. \" +\n \"The result is cut to maxDepth levels and the latest maxTransitions per node; a node with \" +\n \"childrenOmitted or transitionsOmitted has more. Pass its contextId to read below it. \" +\n \"Reads the current state; call again to refresh.\",\n inputSchema: z.object({\n projectId: projectIdInput.describe(\"Project ID\"),\n blockId: blockIdInput.describe(\"Block ID\"),\n mode: z\n .enum([\"prod\", \"staging\"])\n .optional()\n .describe(\"Which render of the block: 'prod' (default) or 'staging'\"),\n contextId: z\n .string()\n .optional()\n .describe(\"Return the subtree of this action context instead of the root\"),\n maxDepth: z\n .number()\n .int()\n .min(0)\n .optional()\n .default(3)\n .describe(\"Levels of child renders to include (default 3)\"),\n maxTransitions: z\n .number()\n .int()\n .min(0)\n .optional()\n .default(20)\n .describe(\"Latest transitions to include per node (default 20)\"),\n }),\n },\n async ({ projectId, blockId, mode, contextId, maxDepth, maxTransitions }) => {\n return await ctx.withProject(projectId, async (project) => {\n const { error } = await findBlock(project, projectId, blockId);\n if (error) return error;\n const tree = await project.getBlockActionContext(blockId, mode ?? \"prod\");\n if (tree === undefined)\n return errorResult(\n \"Block has no action context tree: it was not rendered yet, or the backend does not support block action context.\",\n );\n\n const node = contextId === undefined ? tree : findBlockActionContextNode(tree, contextId);\n if (node === undefined)\n return errorResult(\n `Action context ${contextId} is not in the action context tree of the block. Call without contextId to see the tree.`,\n );\n\n return textResult(limitBlockActionContextTree(node, { maxDepth, maxTransitions }));\n });\n },\n );\n}\n"],"mappings":";;;;;;AAyBA,SAAgB,2BACd,MACA,WACoC;CACpC,IAAI,KAAK,cAAc,WAAW,OAAO;CACzC,KAAK,MAAM,SAAS,KAAK,UAAU;EACjC,MAAM,QAAQ,2BAA2B,OAAO,SAAS;EACzD,IAAI,UAAU,KAAA,GAAW,OAAO;CAClC;AAEF;;AAGA,SAAgB,4BACd,MACA,QAC+B;CAC/B,MAAM,qBAAqB,KAAK,IAAI,GAAG,KAAK,YAAY,SAAS,OAAO,cAAc;CACtF,MAAM,UAAyC;EAC7C,GAAG;EACH,aAAa,KAAK,YAAY,MAAM,kBAAkB;EACtD,UACE,OAAO,WAAW,IACd,KAAK,SAAS,KAAK,UACjB,4BAA4B,OAAO;GAAE,GAAG;GAAQ,UAAU,OAAO,WAAW;EAAE,CAAC,CACjF,IACA,CAAC;CACT;CACA,IAAI,qBAAqB,GAAG,QAAQ,qBAAqB;CACzD,IAAI,OAAO,YAAY,KAAK,KAAK,SAAS,SAAS,GACjD,QAAQ,kBAAkB,KAAK,SAAS;CAC1C,OAAO;AACT;AAEA,SAAgB,gCAAgC,QAAuB,KAAwB;CAC7F,OAAO,aACL,4BACA;EACE,OAAO;EACP,aAAaA,cAAAA,MAAM;EACnB,aACE;EAOF,aAAaC,IAAAA,EAAE,OAAO;GACpB,WAAWC,YAAAA,eAAe,SAAS,YAAY;GAC/C,SAASC,YAAAA,aAAa,SAAS,UAAU;GACzC,MAAMF,IAAAA,EACH,KAAK,CAAC,QAAQ,SAAS,CAAC,CAAC,CACzB,SAAS,CAAC,CACV,SAAS,0DAA0D;GACtE,WAAWA,IAAAA,EACR,OAAO,CAAC,CACR,SAAS,CAAC,CACV,SAAS,+DAA+D;GAC3E,UAAUA,IAAAA,EACP,OAAO,CAAC,CACR,IAAI,CAAC,CACL,IAAI,CAAC,CAAC,CACN,SAAS,CAAC,CACV,QAAQ,CAAC,CAAC,CACV,SAAS,gDAAgD;GAC5D,gBAAgBA,IAAAA,EACb,OAAO,CAAC,CACR,IAAI,CAAC,CACL,IAAI,CAAC,CAAC,CACN,SAAS,CAAC,CACV,QAAQ,EAAE,CAAC,CACX,SAAS,qDAAqD;EACnE,CAAC;CACH,GACA,OAAO,EAAE,WAAW,SAAS,MAAM,WAAW,UAAU,qBAAqB;EAC3E,OAAO,MAAM,IAAI,YAAY,WAAW,OAAO,YAAY;GACzD,MAAM,EAAE,UAAU,MAAMG,kBAAAA,UAAU,SAAS,WAAW,OAAO;GAC7D,IAAI,OAAO,OAAO;GAClB,MAAM,OAAO,MAAM,QAAQ,sBAAsB,SAAS,QAAQ,MAAM;GACxE,IAAI,SAAS,KAAA,GACX,OAAOC,cAAAA,YACL,kHACF;GAEF,MAAM,OAAO,cAAc,KAAA,IAAY,OAAO,2BAA2B,MAAM,SAAS;GACxF,IAAI,SAAS,KAAA,GACX,OAAOA,cAAAA,YACL,kBAAkB,UAAU,yFAC9B;GAEF,OAAOC,cAAAA,WAAW,4BAA4B,MAAM;IAAE;IAAU;GAAe,CAAC,CAAC;EACnF,CAAC;CACH,CACF;AACF"}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { errorResult, hints, textResult } from "./types.js";
|
|
2
2
|
import { blockIdInput, projectIdInput } from "./ids.js";
|
|
3
|
+
import { findBlock } from "./readiness.js";
|
|
3
4
|
import { z } from "zod";
|
|
4
5
|
// src/tools/block-action-context.ts
|
|
5
6
|
/** Finds the node with the context ID in the tree, or undefined. */
|
|
@@ -31,7 +32,7 @@ function registerBlockActionContextTools(server, ctx) {
|
|
|
31
32
|
annotations: hints.read,
|
|
32
33
|
description: "Get the block action context tree of a block run: one node per template render, each with its baggage (name, block-pack, block-id, run-id), the render it belongs to (resource), its action transition in time order (namespace, reporting resource, state, detail, reason), its readings (namespace, resource, key, value) and its child renders. Use it to see which step a block is in or where it waits. The result is cut to maxDepth levels and the latest maxTransitions per node; a node with childrenOmitted or transitionsOmitted has more. Pass its contextId to read below it. Reads the current state; call again to refresh.",
|
|
33
34
|
inputSchema: z.object({
|
|
34
|
-
projectId: projectIdInput.describe("Project ID
|
|
35
|
+
projectId: projectIdInput.describe("Project ID"),
|
|
35
36
|
blockId: blockIdInput.describe("Block ID"),
|
|
36
37
|
mode: z.enum(["prod", "staging"]).optional().describe("Which render of the block: 'prod' (default) or 'staging'"),
|
|
37
38
|
contextId: z.string().optional().describe("Return the subtree of this action context instead of the root"),
|
|
@@ -39,14 +40,18 @@ function registerBlockActionContextTools(server, ctx) {
|
|
|
39
40
|
maxTransitions: z.number().int().min(0).optional().default(20).describe("Latest transitions to include per node (default 20)")
|
|
40
41
|
})
|
|
41
42
|
}, async ({ projectId, blockId, mode, contextId, maxDepth, maxTransitions }) => {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
43
|
+
return await ctx.withProject(projectId, async (project) => {
|
|
44
|
+
const { error } = await findBlock(project, projectId, blockId);
|
|
45
|
+
if (error) return error;
|
|
46
|
+
const tree = await project.getBlockActionContext(blockId, mode ?? "prod");
|
|
47
|
+
if (tree === void 0) return errorResult("Block has no action context tree: it was not rendered yet, or the backend does not support block action context.");
|
|
48
|
+
const node = contextId === void 0 ? tree : findBlockActionContextNode(tree, contextId);
|
|
49
|
+
if (node === void 0) return errorResult(`Action context ${contextId} is not in the action context tree of the block. Call without contextId to see the tree.`);
|
|
50
|
+
return textResult(limitBlockActionContextTree(node, {
|
|
51
|
+
maxDepth,
|
|
52
|
+
maxTransitions
|
|
53
|
+
}));
|
|
54
|
+
});
|
|
50
55
|
});
|
|
51
56
|
}
|
|
52
57
|
export { findBlockActionContextNode, limitBlockActionContextTree, registerBlockActionContextTools };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"block-action-context.js","names":[],"sources":["../../src/tools/block-action-context.ts"],"sourcesContent":["import type { BlockActionContextNode } from \"@milaboratories/pl-middle-layer\";\nimport { z } from \"zod\";\nimport { projectIdInput, blockIdInput } from \"./ids\";\nimport type { ToolContext, ToolRegistrar } from \"./types\";\nimport { errorResult, hints, textResult } from \"./types\";\n\n/** An action context tree node as the tool returns it: cut to the requested depth and history. */\nexport type LimitedBlockActionContextNode = Omit<BlockActionContextNode, \"children\"> & {\n children: LimitedBlockActionContextNode[];\n /** Older transitions left out of this node. */\n transitionsOmitted?: number;\n /** Child renders left out below the depth limit. Request this node by contextId to see them. */\n childrenOmitted?: number;\n};\n\n/** Options of {@link limitBlockActionContextTree}. */\nexport type BlockActionContextTreeLimits = {\n /** Levels of children to include under the returned node. 0 returns the node only. */\n maxDepth: number;\n /** The latest transitions to keep per node. */\n maxTransitions: number;\n};\n\n/** Finds the node with the context ID in the tree, or undefined. */\nexport function findBlockActionContextNode(\n tree: BlockActionContextNode,\n contextId: string,\n): BlockActionContextNode | undefined {\n if (tree.contextId === contextId) return tree;\n for (const child of tree.children) {\n const found = findBlockActionContextNode(child, contextId);\n if (found !== undefined) return found;\n }\n return undefined;\n}\n\n/** Cuts an action context tree to maxDepth levels of children and the latest maxTransitions per node. */\nexport function limitBlockActionContextTree(\n node: BlockActionContextNode,\n limits: BlockActionContextTreeLimits,\n): LimitedBlockActionContextNode {\n const transitionsOmitted = Math.max(0, node.transitions.length - limits.maxTransitions);\n const limited: LimitedBlockActionContextNode = {\n ...node,\n transitions: node.transitions.slice(transitionsOmitted),\n children:\n limits.maxDepth > 0\n ? node.children.map((child) =>\n limitBlockActionContextTree(child, { ...limits, maxDepth: limits.maxDepth - 1 }),\n )\n : [],\n };\n if (transitionsOmitted > 0) limited.transitionsOmitted = transitionsOmitted;\n if (limits.maxDepth <= 0 && node.children.length > 0)\n limited.childrenOmitted = node.children.length;\n return limited;\n}\n\nexport function registerBlockActionContextTools(server: ToolRegistrar, ctx: ToolContext): void {\n server.registerTool(\n \"get_block_action_context\",\n {\n title: \"Get Block Action Context\",\n annotations: hints.read,\n description:\n \"Get the block action context tree of a block run: one node per template render, each with its baggage \" +\n \"(name, block-pack, block-id, run-id), the render it belongs to (resource), its action transition in time order \" +\n \"(namespace, reporting resource, state, detail, reason), its readings (namespace, resource, key, \" +\n \"value) and its child renders. Use it to see which step a block is in or where it waits. \" +\n \"The result is cut to maxDepth levels and the latest maxTransitions per node; a node with \" +\n \"childrenOmitted or transitionsOmitted has more. Pass its contextId to read below it. \" +\n \"Reads the current state; call again to refresh.\",\n inputSchema: z.object({\n projectId: projectIdInput.describe(\"Project ID
|
|
1
|
+
{"version":3,"file":"block-action-context.js","names":[],"sources":["../../src/tools/block-action-context.ts"],"sourcesContent":["import type { BlockActionContextNode } from \"@milaboratories/pl-middle-layer\";\nimport { z } from \"zod\";\nimport { projectIdInput, blockIdInput } from \"./ids\";\nimport type { ToolContext, ToolRegistrar } from \"./types\";\nimport { errorResult, hints, textResult } from \"./types\";\nimport { findBlock } from \"./readiness\";\n\n/** An action context tree node as the tool returns it: cut to the requested depth and history. */\nexport type LimitedBlockActionContextNode = Omit<BlockActionContextNode, \"children\"> & {\n children: LimitedBlockActionContextNode[];\n /** Older transitions left out of this node. */\n transitionsOmitted?: number;\n /** Child renders left out below the depth limit. Request this node by contextId to see them. */\n childrenOmitted?: number;\n};\n\n/** Options of {@link limitBlockActionContextTree}. */\nexport type BlockActionContextTreeLimits = {\n /** Levels of children to include under the returned node. 0 returns the node only. */\n maxDepth: number;\n /** The latest transitions to keep per node. */\n maxTransitions: number;\n};\n\n/** Finds the node with the context ID in the tree, or undefined. */\nexport function findBlockActionContextNode(\n tree: BlockActionContextNode,\n contextId: string,\n): BlockActionContextNode | undefined {\n if (tree.contextId === contextId) return tree;\n for (const child of tree.children) {\n const found = findBlockActionContextNode(child, contextId);\n if (found !== undefined) return found;\n }\n return undefined;\n}\n\n/** Cuts an action context tree to maxDepth levels of children and the latest maxTransitions per node. */\nexport function limitBlockActionContextTree(\n node: BlockActionContextNode,\n limits: BlockActionContextTreeLimits,\n): LimitedBlockActionContextNode {\n const transitionsOmitted = Math.max(0, node.transitions.length - limits.maxTransitions);\n const limited: LimitedBlockActionContextNode = {\n ...node,\n transitions: node.transitions.slice(transitionsOmitted),\n children:\n limits.maxDepth > 0\n ? node.children.map((child) =>\n limitBlockActionContextTree(child, { ...limits, maxDepth: limits.maxDepth - 1 }),\n )\n : [],\n };\n if (transitionsOmitted > 0) limited.transitionsOmitted = transitionsOmitted;\n if (limits.maxDepth <= 0 && node.children.length > 0)\n limited.childrenOmitted = node.children.length;\n return limited;\n}\n\nexport function registerBlockActionContextTools(server: ToolRegistrar, ctx: ToolContext): void {\n server.registerTool(\n \"get_block_action_context\",\n {\n title: \"Get Block Action Context\",\n annotations: hints.read,\n description:\n \"Get the block action context tree of a block run: one node per template render, each with its baggage \" +\n \"(name, block-pack, block-id, run-id), the render it belongs to (resource), its action transition in time order \" +\n \"(namespace, reporting resource, state, detail, reason), its readings (namespace, resource, key, \" +\n \"value) and its child renders. Use it to see which step a block is in or where it waits. \" +\n \"The result is cut to maxDepth levels and the latest maxTransitions per node; a node with \" +\n \"childrenOmitted or transitionsOmitted has more. Pass its contextId to read below it. \" +\n \"Reads the current state; call again to refresh.\",\n inputSchema: z.object({\n projectId: projectIdInput.describe(\"Project ID\"),\n blockId: blockIdInput.describe(\"Block ID\"),\n mode: z\n .enum([\"prod\", \"staging\"])\n .optional()\n .describe(\"Which render of the block: 'prod' (default) or 'staging'\"),\n contextId: z\n .string()\n .optional()\n .describe(\"Return the subtree of this action context instead of the root\"),\n maxDepth: z\n .number()\n .int()\n .min(0)\n .optional()\n .default(3)\n .describe(\"Levels of child renders to include (default 3)\"),\n maxTransitions: z\n .number()\n .int()\n .min(0)\n .optional()\n .default(20)\n .describe(\"Latest transitions to include per node (default 20)\"),\n }),\n },\n async ({ projectId, blockId, mode, contextId, maxDepth, maxTransitions }) => {\n return await ctx.withProject(projectId, async (project) => {\n const { error } = await findBlock(project, projectId, blockId);\n if (error) return error;\n const tree = await project.getBlockActionContext(blockId, mode ?? \"prod\");\n if (tree === undefined)\n return errorResult(\n \"Block has no action context tree: it was not rendered yet, or the backend does not support block action context.\",\n );\n\n const node = contextId === undefined ? tree : findBlockActionContextNode(tree, contextId);\n if (node === undefined)\n return errorResult(\n `Action context ${contextId} is not in the action context tree of the block. Call without contextId to see the tree.`,\n );\n\n return textResult(limitBlockActionContextTree(node, { maxDepth, maxTransitions }));\n });\n },\n );\n}\n"],"mappings":";;;;;;AAyBA,SAAgB,2BACd,MACA,WACoC;CACpC,IAAI,KAAK,cAAc,WAAW,OAAO;CACzC,KAAK,MAAM,SAAS,KAAK,UAAU;EACjC,MAAM,QAAQ,2BAA2B,OAAO,SAAS;EACzD,IAAI,UAAU,KAAA,GAAW,OAAO;CAClC;AAEF;;AAGA,SAAgB,4BACd,MACA,QAC+B;CAC/B,MAAM,qBAAqB,KAAK,IAAI,GAAG,KAAK,YAAY,SAAS,OAAO,cAAc;CACtF,MAAM,UAAyC;EAC7C,GAAG;EACH,aAAa,KAAK,YAAY,MAAM,kBAAkB;EACtD,UACE,OAAO,WAAW,IACd,KAAK,SAAS,KAAK,UACjB,4BAA4B,OAAO;GAAE,GAAG;GAAQ,UAAU,OAAO,WAAW;EAAE,CAAC,CACjF,IACA,CAAC;CACT;CACA,IAAI,qBAAqB,GAAG,QAAQ,qBAAqB;CACzD,IAAI,OAAO,YAAY,KAAK,KAAK,SAAS,SAAS,GACjD,QAAQ,kBAAkB,KAAK,SAAS;CAC1C,OAAO;AACT;AAEA,SAAgB,gCAAgC,QAAuB,KAAwB;CAC7F,OAAO,aACL,4BACA;EACE,OAAO;EACP,aAAa,MAAM;EACnB,aACE;EAOF,aAAa,EAAE,OAAO;GACpB,WAAW,eAAe,SAAS,YAAY;GAC/C,SAAS,aAAa,SAAS,UAAU;GACzC,MAAM,EACH,KAAK,CAAC,QAAQ,SAAS,CAAC,CAAC,CACzB,SAAS,CAAC,CACV,SAAS,0DAA0D;GACtE,WAAW,EACR,OAAO,CAAC,CACR,SAAS,CAAC,CACV,SAAS,+DAA+D;GAC3E,UAAU,EACP,OAAO,CAAC,CACR,IAAI,CAAC,CACL,IAAI,CAAC,CAAC,CACN,SAAS,CAAC,CACV,QAAQ,CAAC,CAAC,CACV,SAAS,gDAAgD;GAC5D,gBAAgB,EACb,OAAO,CAAC,CACR,IAAI,CAAC,CACL,IAAI,CAAC,CAAC,CACN,SAAS,CAAC,CACV,QAAQ,EAAE,CAAC,CACX,SAAS,qDAAqD;EACnE,CAAC;CACH,GACA,OAAO,EAAE,WAAW,SAAS,MAAM,WAAW,UAAU,qBAAqB;EAC3E,OAAO,MAAM,IAAI,YAAY,WAAW,OAAO,YAAY;GACzD,MAAM,EAAE,UAAU,MAAM,UAAU,SAAS,WAAW,OAAO;GAC7D,IAAI,OAAO,OAAO;GAClB,MAAM,OAAO,MAAM,QAAQ,sBAAsB,SAAS,QAAQ,MAAM;GACxE,IAAI,SAAS,KAAA,GACX,OAAO,YACL,kHACF;GAEF,MAAM,OAAO,cAAc,KAAA,IAAY,OAAO,2BAA2B,MAAM,SAAS;GACxF,IAAI,SAAS,KAAA,GACX,OAAO,YACL,kBAAkB,UAAU,yFAC9B;GAEF,OAAO,WAAW,4BAA4B,MAAM;IAAE;IAAU;GAAe,CAAC,CAAC;EACnF,CAAC;CACH,CACF;AACF"}
|
package/dist/tools/block-api.cjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
const require_types = require("./types.cjs");
|
|
2
|
-
|
|
2
|
+
const require_errors = require("./errors.cjs");
|
|
3
|
+
let _milaboratories_pl_model_common = require("@milaboratories/pl-model-common");
|
|
3
4
|
// src/tools/block-api.ts
|
|
4
5
|
/**
|
|
5
6
|
* The block API MCP creates and writes blocks on (BlockModelV3): the model API version a create
|
|
@@ -14,10 +15,24 @@ const UNSUPPORTED_BLOCK_API_TEXT = "Blocks on block API 1 are not supported; not
|
|
|
14
15
|
* other error.
|
|
15
16
|
*/
|
|
16
17
|
function unsupportedBlockApiResult(e) {
|
|
17
|
-
return e
|
|
18
|
+
return require_errors.isModelApiVersionMismatchError(e) ? require_types.errorResult(UNSUPPORTED_BLOCK_API_TEXT) : void 0;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* The same guard asked before a write is planned, from the block's entry in the project overview:
|
|
22
|
+
* the error result when its feature flags put it on another block API, `undefined` when they put
|
|
23
|
+
* it on {@link SUPPORTED_BLOCK_API}. A block whose pack is not loaded yet has no flags; it is not
|
|
24
|
+
* refused here, and the middle layer's own check decides at the write.
|
|
25
|
+
*
|
|
26
|
+
* So a block on block API 1 is told it is not supported, rather than being refused for whatever
|
|
27
|
+
* its storage, which looks nothing like BlockModelV3 data, fails first.
|
|
28
|
+
*/
|
|
29
|
+
function blockApiGuardResult(block) {
|
|
30
|
+
if (block.featureFlags === void 0) return void 0;
|
|
31
|
+
return (0, _milaboratories_pl_model_common.checkBlockFlag)(block.featureFlags, "requiresModelAPIVersion", SUPPORTED_BLOCK_API) ? void 0 : require_types.errorResult(UNSUPPORTED_BLOCK_API_TEXT);
|
|
18
32
|
}
|
|
19
33
|
exports.SUPPORTED_BLOCK_API = SUPPORTED_BLOCK_API;
|
|
20
34
|
exports.UNSUPPORTED_BLOCK_API_TEXT = UNSUPPORTED_BLOCK_API_TEXT;
|
|
35
|
+
exports.blockApiGuardResult = blockApiGuardResult;
|
|
21
36
|
exports.unsupportedBlockApiResult = unsupportedBlockApiResult;
|
|
22
37
|
|
|
23
38
|
//# sourceMappingURL=block-api.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"block-api.cjs","names":["BLOCK_STORAGE_FACADE_VERSION","
|
|
1
|
+
{"version":3,"file":"block-api.cjs","names":["BLOCK_STORAGE_FACADE_VERSION","isModelApiVersionMismatchError","errorResult","checkBlockFlag"],"sources":["../../src/tools/block-api.ts"],"sourcesContent":["import { type BlockCodeFeatureFlags, checkBlockFlag } from \"@milaboratories/pl-model-common\";\nimport { BLOCK_STORAGE_FACADE_VERSION } from \"@platforma-sdk/model\";\nimport { isModelApiVersionMismatchError } from \"./errors\";\nimport { errorResult } from \"./types\";\n\n/**\n * The block API MCP creates and writes blocks on (BlockModelV3): the model API version a create\n * or an update requires of the block pack.\n */\nexport const SUPPORTED_BLOCK_API = BLOCK_STORAGE_FACADE_VERSION;\n\n/** What a tool answers when the block it would create or write is on another block API. */\nexport const UNSUPPORTED_BLOCK_API_TEXT =\n \"Blocks on block API 1 are not supported; nothing was changed.\";\n\n/**\n * The guard of every create and write: the error result for `e` when it is the middle layer's\n * refusal of a block on another block API (`ModelAPIVersionMismatchError`), `undefined` for any\n * other error.\n */\nexport function unsupportedBlockApiResult(e: unknown) {\n return isModelApiVersionMismatchError(e) ? errorResult(UNSUPPORTED_BLOCK_API_TEXT) : undefined;\n}\n\n/**\n * The same guard asked before a write is planned, from the block's entry in the project overview:\n * the error result when its feature flags put it on another block API, `undefined` when they put\n * it on {@link SUPPORTED_BLOCK_API}. A block whose pack is not loaded yet has no flags; it is not\n * refused here, and the middle layer's own check decides at the write.\n *\n * So a block on block API 1 is told it is not supported, rather than being refused for whatever\n * its storage, which looks nothing like BlockModelV3 data, fails first.\n */\nexport function blockApiGuardResult(block: { featureFlags: BlockCodeFeatureFlags | undefined }) {\n if (block.featureFlags === undefined) return undefined;\n return checkBlockFlag(block.featureFlags, \"requiresModelAPIVersion\", SUPPORTED_BLOCK_API)\n ? undefined\n : errorResult(UNSUPPORTED_BLOCK_API_TEXT);\n}\n"],"mappings":";;;;;;;;AASA,MAAa,sB,8BAAsBA,CAAAA,CAAAA;;AAGnC,MAAa,6BACX;;;;;;AAOF,SAAgB,0BAA0B,GAAY;CACpD,OAAOC,eAAAA,+BAA+B,CAAC,IAAIC,cAAAA,YAAY,0BAA0B,IAAI,KAAA;AACvF;;;;;;;;;;AAWA,SAAgB,oBAAoB,OAA4D;CAC9F,IAAI,MAAM,iBAAiB,KAAA,GAAW,OAAO,KAAA;CAC7C,QAAA,GAAOC,gCAAAA,eAAAA,CAAe,MAAM,cAAc,2BAA2B,mBAAmB,IACpF,KAAA,IACAD,cAAAA,YAAY,0BAA0B;AAC5C"}
|
package/dist/tools/block-api.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { errorResult } from "./types.js";
|
|
2
|
-
import {
|
|
2
|
+
import { isModelApiVersionMismatchError } from "./errors.js";
|
|
3
|
+
import { checkBlockFlag } from "@milaboratories/pl-model-common";
|
|
3
4
|
import { BLOCK_STORAGE_FACADE_VERSION } from "@platforma-sdk/model";
|
|
4
5
|
// src/tools/block-api.ts
|
|
5
6
|
/**
|
|
@@ -15,8 +16,21 @@ const UNSUPPORTED_BLOCK_API_TEXT = "Blocks on block API 1 are not supported; not
|
|
|
15
16
|
* other error.
|
|
16
17
|
*/
|
|
17
18
|
function unsupportedBlockApiResult(e) {
|
|
18
|
-
return e
|
|
19
|
+
return isModelApiVersionMismatchError(e) ? errorResult(UNSUPPORTED_BLOCK_API_TEXT) : void 0;
|
|
19
20
|
}
|
|
20
|
-
|
|
21
|
+
/**
|
|
22
|
+
* The same guard asked before a write is planned, from the block's entry in the project overview:
|
|
23
|
+
* the error result when its feature flags put it on another block API, `undefined` when they put
|
|
24
|
+
* it on {@link SUPPORTED_BLOCK_API}. A block whose pack is not loaded yet has no flags; it is not
|
|
25
|
+
* refused here, and the middle layer's own check decides at the write.
|
|
26
|
+
*
|
|
27
|
+
* So a block on block API 1 is told it is not supported, rather than being refused for whatever
|
|
28
|
+
* its storage, which looks nothing like BlockModelV3 data, fails first.
|
|
29
|
+
*/
|
|
30
|
+
function blockApiGuardResult(block) {
|
|
31
|
+
if (block.featureFlags === void 0) return void 0;
|
|
32
|
+
return checkBlockFlag(block.featureFlags, "requiresModelAPIVersion", SUPPORTED_BLOCK_API) ? void 0 : errorResult(UNSUPPORTED_BLOCK_API_TEXT);
|
|
33
|
+
}
|
|
34
|
+
export { SUPPORTED_BLOCK_API, UNSUPPORTED_BLOCK_API_TEXT, blockApiGuardResult, unsupportedBlockApiResult };
|
|
21
35
|
|
|
22
36
|
//# sourceMappingURL=block-api.js.map
|