@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/await.ts
CHANGED
|
@@ -1,12 +1,363 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import type { BlockId, ProjectId } from "@milaboratories/pl-model-common";
|
|
2
|
+
import type { BlockReadiness, BlockStateOverview } from "@milaboratories/pl-middle-layer";
|
|
3
3
|
import { z } from "zod";
|
|
4
|
-
import {
|
|
4
|
+
import { blockIdInput, projectIdInput } from "./ids";
|
|
5
5
|
import type { ToolContext, ToolRegistrar } from "./types";
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
6
|
+
import { blockView, readStorageView } from "./block-storage";
|
|
7
|
+
import { readinessOf } from "./block-state";
|
|
8
|
+
import { awaitImportsDone } from "./imports";
|
|
9
|
+
import { findBlock, staleResults } from "./readiness";
|
|
10
|
+
import { expressionTimeoutText, safeEval } from "./sandbox";
|
|
11
|
+
import {
|
|
12
|
+
awaitBlockSettled,
|
|
13
|
+
clampWait,
|
|
14
|
+
pendingOutputKeys,
|
|
15
|
+
progressReporter,
|
|
16
|
+
type SettleStateComputable,
|
|
17
|
+
type WaitContext,
|
|
18
|
+
} from "./settle";
|
|
8
19
|
import { errorResult, hints, textResult, timedOutResult, transformFailedResult } from "./types";
|
|
9
20
|
|
|
21
|
+
/**
|
|
22
|
+
* The default wait of `await_block_done`, in milliseconds: under the 60 s after which common MCP
|
|
23
|
+
* clients give up on a request (D-018).
|
|
24
|
+
*/
|
|
25
|
+
export const AWAIT_DEFAULT_MS = 50_000;
|
|
26
|
+
/** The shortest wait `await_block_done` takes. */
|
|
27
|
+
export const AWAIT_MIN_MS = 1_000;
|
|
28
|
+
/** The longest wait `await_block_done` takes, for clients whose request timeout is that long. */
|
|
29
|
+
export const AWAIT_MAX_MS = 600_000;
|
|
30
|
+
|
|
31
|
+
/** Default timeout of a `transform` of `await_block_done`, in milliseconds. */
|
|
32
|
+
const DEFAULT_TRANSFORM_TIMEOUT = 5000;
|
|
33
|
+
|
|
34
|
+
/** What a block in limbo means, and what brings it back. */
|
|
35
|
+
export const LIMBO_NOTE =
|
|
36
|
+
"The block is in limbo: its results are from before a block above it changed. That happens " +
|
|
37
|
+
"when a block above failed or was stopped, and also when a block above was run again: a run " +
|
|
38
|
+
"of an upstream block moves the settled blocks below it to limbo. run_block on this block " +
|
|
39
|
+
"runs it again, together with the stale blocks above it.";
|
|
40
|
+
|
|
41
|
+
/** What a block that is not calculated means. */
|
|
42
|
+
export const NOT_CALCULATED_NOTE =
|
|
43
|
+
"The block has not been run, or its run was reset before it finished. There is nothing to " +
|
|
44
|
+
"wait for: run_block starts it, then call await_block_done again.";
|
|
45
|
+
|
|
46
|
+
/** What `await_block_done` reads of a block of the project overview. */
|
|
47
|
+
export type AwaitOverviewBlock = Pick<
|
|
48
|
+
BlockStateOverview,
|
|
49
|
+
| "id"
|
|
50
|
+
| "title"
|
|
51
|
+
| "label"
|
|
52
|
+
| "calculationStatus"
|
|
53
|
+
| "canRun"
|
|
54
|
+
| "stale"
|
|
55
|
+
| "outputErrors"
|
|
56
|
+
| "missingReference"
|
|
57
|
+
| "upstreams"
|
|
58
|
+
>;
|
|
59
|
+
|
|
60
|
+
/** What `await_block_done` reads of a block state. */
|
|
61
|
+
export type AwaitBlockState = {
|
|
62
|
+
outputs?: Record<string, unknown> | undefined;
|
|
63
|
+
blockStorage: unknown;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
/** What `await_block_done` needs of a project; a middle-layer `Project` has it. */
|
|
67
|
+
export type AwaitProject = {
|
|
68
|
+
overview: { getValue(): Promise<{ blocks: AwaitOverviewBlock[] } | undefined> };
|
|
69
|
+
getBlockState(blockId: BlockId): SettleStateComputable<AwaitBlockState> & {
|
|
70
|
+
getValue(): Promise<AwaitBlockState>;
|
|
71
|
+
};
|
|
72
|
+
getBlockReadiness(blockId: BlockId): { getValue(): Promise<BlockReadiness | undefined> };
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
/** What {@link awaitBlockDone} returns: the result of `await_block_done`. */
|
|
76
|
+
type AwaitResult = ReturnType<typeof textResult> | ReturnType<typeof errorResult>;
|
|
77
|
+
|
|
78
|
+
/** Input of {@link awaitBlockDone}: the arguments of `await_block_done`. */
|
|
79
|
+
export type AwaitInput = {
|
|
80
|
+
projectId: ProjectId;
|
|
81
|
+
blockId: BlockId;
|
|
82
|
+
timeout?: number;
|
|
83
|
+
transform?: string;
|
|
84
|
+
transformTimeout?: number;
|
|
85
|
+
waitForImports?: boolean;
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
/** The block's overview fields every answer of `await_block_done` carries. */
|
|
89
|
+
function blockInfo(block: AwaitOverviewBlock) {
|
|
90
|
+
return {
|
|
91
|
+
id: block.id,
|
|
92
|
+
title: block.title ?? block.label,
|
|
93
|
+
calculationStatus: block.calculationStatus,
|
|
94
|
+
canRun: block.canRun,
|
|
95
|
+
stale: block.stale,
|
|
96
|
+
outputErrors: block.outputErrors,
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** The answer for a block whose status ends the wait without results: not calculated, in limbo. */
|
|
101
|
+
async function stoppedAnswer(
|
|
102
|
+
project: AwaitProject,
|
|
103
|
+
projectId: ProjectId,
|
|
104
|
+
blockId: BlockId,
|
|
105
|
+
status: "NotCalculated" | "Limbo",
|
|
106
|
+
waitedMs: number,
|
|
107
|
+
upstreamHalt?: { waitingOn: BlockId; waitingOnStatus: string | undefined; running: boolean },
|
|
108
|
+
) {
|
|
109
|
+
const { block, error } = await findBlock(project, projectId, blockId);
|
|
110
|
+
if (error) return error;
|
|
111
|
+
const state = await project.getBlockState(blockId).getValue();
|
|
112
|
+
const pendingOutputs = pendingOutputKeys(state.outputs);
|
|
113
|
+
const notes = [status === "Limbo" ? LIMBO_NOTE : NOT_CALCULATED_NOTE];
|
|
114
|
+
if (upstreamHalt !== undefined && pendingOutputs.length > 0)
|
|
115
|
+
notes.push(
|
|
116
|
+
`Some outputs wait for block ${upstreamHalt.waitingOn} above, which ` +
|
|
117
|
+
(upstreamHalt.running
|
|
118
|
+
? "is running: await_block_done on it, then on this block."
|
|
119
|
+
: "has not run: run_block on this block runs it with the stale blocks above."),
|
|
120
|
+
);
|
|
121
|
+
return textResult({
|
|
122
|
+
status,
|
|
123
|
+
note: notes.join(" "),
|
|
124
|
+
block: blockInfo(block),
|
|
125
|
+
waitedMs,
|
|
126
|
+
readiness: await readinessOf(project, blockId),
|
|
127
|
+
...blockView(state),
|
|
128
|
+
outputsSettled: pendingOutputs.length === 0,
|
|
129
|
+
...(upstreamHalt !== undefined && pendingOutputs.length > 0
|
|
130
|
+
? {
|
|
131
|
+
pendingOutputs,
|
|
132
|
+
waitingOn: upstreamHalt.waitingOn,
|
|
133
|
+
waitingOnStatus: upstreamHalt.waitingOnStatus,
|
|
134
|
+
}
|
|
135
|
+
: {}),
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Waits for a block's run to finish and its outputs to settle, and with `waitForImports` for its
|
|
141
|
+
* files to upload; returns the result of `await_block_done`. One clock and one progress reporter
|
|
142
|
+
* run across both waits, so the progress a client sees only grows. A block that was never run or
|
|
143
|
+
* is in limbo has no run to wait for, but its imports are still waited for when asked: its
|
|
144
|
+
* pre-run may be uploading files. Its outputs are waited for first, within the same deadline: an
|
|
145
|
+
* output still computing may be the one that names the files, so no imports count as done only
|
|
146
|
+
* once the outputs settled (or wait for a block above that is running or has not run). A block
|
|
147
|
+
* somebody runs while that wait goes on is answered as such (not finished, call again), so the
|
|
148
|
+
* answer never says a running block has not run.
|
|
149
|
+
*/
|
|
150
|
+
export async function awaitBlockDone(
|
|
151
|
+
project: AwaitProject,
|
|
152
|
+
input: AwaitInput,
|
|
153
|
+
options: { call?: WaitContext } = {},
|
|
154
|
+
): Promise<AwaitResult> {
|
|
155
|
+
const { projectId, blockId, transform, waitForImports } = input;
|
|
156
|
+
const { call } = options;
|
|
157
|
+
const found = await findBlock(project, projectId, blockId);
|
|
158
|
+
if (found.error) return found.error;
|
|
159
|
+
|
|
160
|
+
const timeoutMs = clampWait(input.timeout, AWAIT_DEFAULT_MS, AWAIT_MIN_MS, AWAIT_MAX_MS);
|
|
161
|
+
const start = Date.now();
|
|
162
|
+
const report = progressReporter(call, "Waiting for the block to finish");
|
|
163
|
+
const progress = (message: string) =>
|
|
164
|
+
report === undefined ? undefined : () => report(Date.now() - start, timeoutMs, message);
|
|
165
|
+
const signal = call?.mcpReq.signal;
|
|
166
|
+
|
|
167
|
+
const timeLeft = () => Math.max(0, timeoutMs - (Date.now() - start));
|
|
168
|
+
|
|
169
|
+
/** The block's calculation status now; `undefined` when it cannot be read. */
|
|
170
|
+
const statusNow = async () => {
|
|
171
|
+
const now = await findBlock(project, projectId, blockId);
|
|
172
|
+
return now.block?.calculationStatus;
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
/** Waits for the block's imports with what is left of the time; `undefined` when they are in. */
|
|
176
|
+
const importsLeft = async (status: string) => {
|
|
177
|
+
const imports = await awaitImportsDone(project.getBlockState(blockId), {
|
|
178
|
+
timeoutMs: timeLeft(),
|
|
179
|
+
signal,
|
|
180
|
+
onProgress: progress("Waiting for the block's imports"),
|
|
181
|
+
});
|
|
182
|
+
if (imports.done) return { state: imports.state };
|
|
183
|
+
// The status may have changed while the imports were waited for: somebody ran the block.
|
|
184
|
+
const now = (await statusNow()) ?? status;
|
|
185
|
+
return {
|
|
186
|
+
result: timedOutResult({
|
|
187
|
+
status: now,
|
|
188
|
+
settled: now === "Done",
|
|
189
|
+
waitedMs: Date.now() - start,
|
|
190
|
+
imports: imports.imports,
|
|
191
|
+
hint:
|
|
192
|
+
(now === "Done"
|
|
193
|
+
? "The block's run is over but some of its files "
|
|
194
|
+
: now === "Running"
|
|
195
|
+
? "The block is running; some of its files "
|
|
196
|
+
: "The block has not run; some of the files its data names ") +
|
|
197
|
+
"are still uploading or being indexed. Call await_block_done with waitForImports again.",
|
|
198
|
+
}),
|
|
199
|
+
};
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
const stopped = async (status: "NotCalculated" | "Limbo"): Promise<AwaitResult> => {
|
|
203
|
+
if (waitForImports !== true)
|
|
204
|
+
return await stoppedAnswer(project, projectId, blockId, status, Date.now() - start);
|
|
205
|
+
|
|
206
|
+
// The pre-run outputs first: until they settle, an empty progress map may only mean the
|
|
207
|
+
// output that names the files has not computed yet.
|
|
208
|
+
const outputs = await awaitBlockSettled(project, blockId, {
|
|
209
|
+
timeoutMs: timeLeft(),
|
|
210
|
+
signal,
|
|
211
|
+
waitForRun: false,
|
|
212
|
+
onProgress: progress("Waiting for the block's outputs to settle"),
|
|
213
|
+
});
|
|
214
|
+
if (outputs.stoppedBy === "block-gone") return blockGone();
|
|
215
|
+
if (outputs.stoppedBy === "timeout" || outputs.stoppedBy === "aborted")
|
|
216
|
+
return timedOutResult({
|
|
217
|
+
status,
|
|
218
|
+
settled: false,
|
|
219
|
+
waitedMs: Date.now() - start,
|
|
220
|
+
pendingOutputs: outputs.pendingOutputs,
|
|
221
|
+
hint:
|
|
222
|
+
"The block has not run; some of its outputs are still computing, and they may name " +
|
|
223
|
+
"files to upload. Call await_block_done with waitForImports again.",
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
// Somebody may have run the block while its outputs were waited for.
|
|
227
|
+
const afterOutputs = await stillStopped(status);
|
|
228
|
+
if (afterOutputs.result !== undefined) return afterOutputs.result;
|
|
229
|
+
|
|
230
|
+
const left = await importsLeft(afterOutputs.status);
|
|
231
|
+
if (left.result !== undefined) return left.result;
|
|
232
|
+
const halt =
|
|
233
|
+
outputs.waitingOn === undefined
|
|
234
|
+
? undefined
|
|
235
|
+
: {
|
|
236
|
+
waitingOn: outputs.waitingOn,
|
|
237
|
+
waitingOnStatus: outputs.waitingOnStatus,
|
|
238
|
+
running: outputs.stoppedBy === "upstream-running",
|
|
239
|
+
};
|
|
240
|
+
|
|
241
|
+
// ... or while its imports were.
|
|
242
|
+
const afterImports = await stillStopped(afterOutputs.status);
|
|
243
|
+
if (afterImports.result !== undefined) return afterImports.result;
|
|
244
|
+
return await stoppedAnswer(
|
|
245
|
+
project,
|
|
246
|
+
projectId,
|
|
247
|
+
blockId,
|
|
248
|
+
afterImports.status,
|
|
249
|
+
Date.now() - start,
|
|
250
|
+
halt,
|
|
251
|
+
);
|
|
252
|
+
};
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* After a wait on a block that had not run, its status now: still not calculated or in limbo,
|
|
256
|
+
* or, when somebody ran it meanwhile, an answer saying so, for the agent to wait for the run.
|
|
257
|
+
*/
|
|
258
|
+
const stillStopped = async (
|
|
259
|
+
status: "NotCalculated" | "Limbo",
|
|
260
|
+
): Promise<
|
|
261
|
+
| { status: "NotCalculated" | "Limbo"; result?: undefined }
|
|
262
|
+
| { status?: undefined; result: AwaitResult }
|
|
263
|
+
> => {
|
|
264
|
+
const now = await findBlock(project, projectId, blockId);
|
|
265
|
+
if (now.error) return { result: now.error };
|
|
266
|
+
const current = now.block.calculationStatus;
|
|
267
|
+
if (current === "NotCalculated" || current === "Limbo") return { status: current };
|
|
268
|
+
if (current === undefined) return { status };
|
|
269
|
+
return {
|
|
270
|
+
result: timedOutResult({
|
|
271
|
+
status: current,
|
|
272
|
+
settled: false,
|
|
273
|
+
waitedMs: Date.now() - start,
|
|
274
|
+
hint: "The block was run while waiting. Call await_block_done again to wait for the run.",
|
|
275
|
+
}),
|
|
276
|
+
};
|
|
277
|
+
};
|
|
278
|
+
|
|
279
|
+
const blockGone = () =>
|
|
280
|
+
errorResult(
|
|
281
|
+
`Block ${blockId} was removed from project ${projectId} while waiting.`,
|
|
282
|
+
"Use get_project_overview to list the block ids of the project.",
|
|
283
|
+
);
|
|
284
|
+
|
|
285
|
+
/** Waits for the block's run with what is left of the time, then answers. */
|
|
286
|
+
const awaitRun = async (): Promise<AwaitResult> => {
|
|
287
|
+
const settle = await awaitBlockSettled(project, blockId, {
|
|
288
|
+
timeoutMs: timeLeft(),
|
|
289
|
+
signal,
|
|
290
|
+
waitForRun: true,
|
|
291
|
+
onProgress: progress("Waiting for the block to finish"),
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
if (settle.stoppedBy === "block-gone") return blockGone();
|
|
295
|
+
if (settle.stoppedBy === "not-calculated") return await stopped("NotCalculated");
|
|
296
|
+
if (settle.stoppedBy === "limbo") return await stopped("Limbo");
|
|
297
|
+
|
|
298
|
+
if (!settle.settled)
|
|
299
|
+
return timedOutResult({
|
|
300
|
+
status: settle.calculationStatus ?? "Unknown",
|
|
301
|
+
settled: false,
|
|
302
|
+
waitedMs: Date.now() - start,
|
|
303
|
+
pendingOutputs: settle.pendingOutputs,
|
|
304
|
+
...(settle.waitingOn !== undefined ? { waitingOn: settle.waitingOn } : {}),
|
|
305
|
+
runningUpstreams: settle.runningUpstreams,
|
|
306
|
+
hint:
|
|
307
|
+
settle.stoppedBy === "upstream-running" && settle.waitingOn !== undefined
|
|
308
|
+
? `The outputs wait for block ${settle.waitingOn} above, which is running: ` +
|
|
309
|
+
"await_block_done on it, then on this block."
|
|
310
|
+
: settle.calculationStatus === "Running"
|
|
311
|
+
? "Not finished: the block is still running. Call await_block_done again."
|
|
312
|
+
: "Not finished: some outputs are still computing. Call await_block_done again.",
|
|
313
|
+
});
|
|
314
|
+
|
|
315
|
+
let state = settle.state;
|
|
316
|
+
if (waitForImports === true) {
|
|
317
|
+
const left = await importsLeft("Done");
|
|
318
|
+
if (left.result !== undefined) return left.result;
|
|
319
|
+
state = left.state;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
const { block, error } = await findBlock(project, projectId, blockId);
|
|
323
|
+
if (error) return error;
|
|
324
|
+
const info = blockInfo(block);
|
|
325
|
+
|
|
326
|
+
if (transform) {
|
|
327
|
+
try {
|
|
328
|
+
const result = await safeEval(
|
|
329
|
+
transform,
|
|
330
|
+
{
|
|
331
|
+
data: readStorageView(state.blockStorage).data,
|
|
332
|
+
outputs: state.outputs,
|
|
333
|
+
block: info,
|
|
334
|
+
},
|
|
335
|
+
input.transformTimeout ?? DEFAULT_TRANSFORM_TIMEOUT,
|
|
336
|
+
);
|
|
337
|
+
return textResult({ status: "Done", block: info, ...staleResults(block), result });
|
|
338
|
+
} catch (e: unknown) {
|
|
339
|
+
return transformFailedResult(
|
|
340
|
+
e,
|
|
341
|
+
"Check your JS expression syntax. Available variables: data, outputs, block.",
|
|
342
|
+
);
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
return textResult({
|
|
347
|
+
status: "Done",
|
|
348
|
+
...staleResults(block),
|
|
349
|
+
block: info,
|
|
350
|
+
waitedMs: Date.now() - start,
|
|
351
|
+
readiness: await readinessOf(project, blockId),
|
|
352
|
+
...blockView(state),
|
|
353
|
+
});
|
|
354
|
+
};
|
|
355
|
+
|
|
356
|
+
const initial = found.block.calculationStatus;
|
|
357
|
+
if (initial === "NotCalculated" || initial === "Limbo") return await stopped(initial);
|
|
358
|
+
return await awaitRun();
|
|
359
|
+
}
|
|
360
|
+
|
|
10
361
|
export function registerAwaitTools(server: ToolRegistrar, ctx: ToolContext): void {
|
|
11
362
|
server.registerTool(
|
|
12
363
|
"await_block_done",
|
|
@@ -14,139 +365,58 @@ export function registerAwaitTools(server: ToolRegistrar, ctx: ToolContext): voi
|
|
|
14
365
|
title: "Wait for Block",
|
|
15
366
|
annotations: hints.read,
|
|
16
367
|
description:
|
|
17
|
-
"Wait for a block to finish
|
|
18
|
-
"
|
|
19
|
-
"Use `transform` to extract specific data
|
|
368
|
+
"Wait for a block's run to finish and its outputs to settle. Returns the block status " +
|
|
369
|
+
"and the block view of get_block_state (data, plugins, output summary, readiness); large " +
|
|
370
|
+
"data comes as an outline, as there. Use `transform` to extract specific data " +
|
|
371
|
+
"server-side on completion.\n\n" +
|
|
372
|
+
`Waits up to \`timeout\` (default ${AWAIT_DEFAULT_MS} ms, under the 60 s after which ` +
|
|
373
|
+
"most clients give up; raise it only when your client waits longer). A wait that runs " +
|
|
374
|
+
"out is not an error: `{ timedOut: true, status, pendingOutputs, waitingOn?, " +
|
|
375
|
+
"runningUpstreams }`, the block is not finished: call again. A block that was never run " +
|
|
376
|
+
"(status NotCalculated) or is in limbo (results from before a block above changed: it " +
|
|
377
|
+
"failed, was stopped, or ran again) returns with a `note`; run_block starts it. A " +
|
|
378
|
+
"block whose results are from an earlier configuration (its data, or the data of a " +
|
|
379
|
+
"block above, changed after the run) is Done with `stale: true` and a `note`. " +
|
|
380
|
+
"`waitForImports` also waits for the files the block imports (its `imports` summary), " +
|
|
381
|
+
"for a block that has not run too, once its pre-run outputs settle: its pre-run " +
|
|
382
|
+
"uploads the files its data names.",
|
|
20
383
|
inputSchema: z.object({
|
|
21
384
|
projectId: projectIdInput.describe("Project ID"),
|
|
22
385
|
blockId: blockIdInput.describe("Block ID to wait for"),
|
|
23
|
-
timeout: z
|
|
386
|
+
timeout: z
|
|
387
|
+
.number()
|
|
388
|
+
.optional()
|
|
389
|
+
.default(AWAIT_DEFAULT_MS)
|
|
390
|
+
.describe(
|
|
391
|
+
`The longest wait in ms (default ${AWAIT_DEFAULT_MS}, from ${AWAIT_MIN_MS} to ` +
|
|
392
|
+
`${AWAIT_MAX_MS}).`,
|
|
393
|
+
),
|
|
24
394
|
transform: z
|
|
25
395
|
.string()
|
|
26
396
|
.optional()
|
|
27
397
|
.describe(
|
|
28
398
|
"JS expression evaluated server-side when block completes. " +
|
|
29
|
-
"Available variables: `data` (block
|
|
399
|
+
"Available variables: `data` (block data), `outputs` (raw outputs), `block` (status info). " +
|
|
30
400
|
"Omit for default concise summary.",
|
|
31
401
|
),
|
|
32
402
|
transformTimeout: z
|
|
33
403
|
.number()
|
|
34
404
|
.optional()
|
|
35
|
-
.default(
|
|
36
|
-
.describe("
|
|
405
|
+
.default(DEFAULT_TRANSFORM_TIMEOUT)
|
|
406
|
+
.describe(expressionTimeoutText("transform evaluation", DEFAULT_TRANSFORM_TIMEOUT)),
|
|
407
|
+
waitForImports: z
|
|
408
|
+
.boolean()
|
|
409
|
+
.optional()
|
|
410
|
+
.describe(
|
|
411
|
+
"Also wait until the files the block imports are uploaded or indexed (its `imports` " +
|
|
412
|
+
"summary has nothing left to finish), within the same `timeout` (default false).",
|
|
413
|
+
),
|
|
37
414
|
}),
|
|
38
415
|
},
|
|
39
|
-
async (
|
|
40
|
-
return await ctx.withProject(projectId,
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
while (Date.now() < deadline) {
|
|
44
|
-
const overview = await project.overview.getValue();
|
|
45
|
-
if (!overview) continue;
|
|
46
|
-
const block = overview.blocks.find((b) => b.id === blockId);
|
|
47
|
-
if (!block)
|
|
48
|
-
return errorResult(
|
|
49
|
-
`Block ${blockId} not found in project ${projectId}.`,
|
|
50
|
-
"Use get_project_overview to list all block IDs in this project.",
|
|
51
|
-
);
|
|
52
|
-
|
|
53
|
-
// Terminal error states — return immediately
|
|
54
|
-
if (block.calculationStatus === "Limbo") {
|
|
55
|
-
return errorResult(
|
|
56
|
-
"Block entered Limbo state (upstream failed or was stopped).",
|
|
57
|
-
"Check upstream blocks with get_project_overview. Fix or re-run the failed upstream, then retry.",
|
|
58
|
-
);
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
if (block.calculationStatus === "NotCalculated") {
|
|
62
|
-
return errorResult(
|
|
63
|
-
"Block has not been started.",
|
|
64
|
-
"Use run_block to start it first, then call await_block_done.",
|
|
65
|
-
);
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
if (block.calculationStatus === "Done") {
|
|
69
|
-
// Await stable block state with remaining time budget
|
|
70
|
-
const remaining = Math.max(deadline - Date.now(), 1000);
|
|
71
|
-
let state;
|
|
72
|
-
try {
|
|
73
|
-
state = await project
|
|
74
|
-
.getBlockState(blockId)
|
|
75
|
-
.awaitStableValue(AbortSignal.timeout(remaining));
|
|
76
|
-
} catch (e: unknown) {
|
|
77
|
-
if (isTimeoutError(e)) {
|
|
78
|
-
return timedOutResult({
|
|
79
|
-
status: "Done",
|
|
80
|
-
note: "Computation done but outputs did not stabilize in time. Retry with a longer timeout.",
|
|
81
|
-
});
|
|
82
|
-
}
|
|
83
|
-
return errorResult(
|
|
84
|
-
`Failed to get block state: ${e instanceof Error ? e.message : String(e)}`,
|
|
85
|
-
);
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
const data = deriveDataFromStorage(state.blockStorage);
|
|
89
|
-
|
|
90
|
-
const blockInfo = {
|
|
91
|
-
id: block.id,
|
|
92
|
-
title: block.title ?? block.label,
|
|
93
|
-
calculationStatus: block.calculationStatus,
|
|
94
|
-
canRun: block.canRun,
|
|
95
|
-
stale: block.stale,
|
|
96
|
-
outputErrors: block.outputErrors,
|
|
97
|
-
};
|
|
98
|
-
|
|
99
|
-
if (transform) {
|
|
100
|
-
try {
|
|
101
|
-
const result = await safeEval(
|
|
102
|
-
transform,
|
|
103
|
-
{
|
|
104
|
-
data,
|
|
105
|
-
outputs: state.outputs,
|
|
106
|
-
block: blockInfo,
|
|
107
|
-
},
|
|
108
|
-
transformTimeout,
|
|
109
|
-
);
|
|
110
|
-
return textResult({ status: "Done", block: blockInfo, result });
|
|
111
|
-
} catch (e: unknown) {
|
|
112
|
-
return transformFailedResult(
|
|
113
|
-
e,
|
|
114
|
-
"Check your JS expression syntax. Available variables: data, outputs, block.",
|
|
115
|
-
);
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
return textResult({
|
|
120
|
-
status: "Done",
|
|
121
|
-
block: blockInfo,
|
|
122
|
-
data,
|
|
123
|
-
outputs: summarizeOutputs(state.outputs as Record<string, unknown> | undefined),
|
|
124
|
-
});
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
// Still running — wait up to 5s for overview to change, then re-poll.
|
|
128
|
-
// Minimum 500ms delay to avoid busy-looping if awaitChange resolves immediately.
|
|
129
|
-
const pollStart = Date.now();
|
|
130
|
-
try {
|
|
131
|
-
const result = await project.overview.getFullValue();
|
|
132
|
-
await project.overview.awaitChange(AbortSignal.timeout(5000), result.uTag);
|
|
133
|
-
} catch {
|
|
134
|
-
// timeout or abort — just re-poll
|
|
135
|
-
}
|
|
136
|
-
const elapsed = Date.now() - pollStart;
|
|
137
|
-
if (elapsed < 500) {
|
|
138
|
-
await new Promise((r) => setTimeout(r, 500 - elapsed));
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
// Timed out while running
|
|
143
|
-
const overview = await project.overview.getValue();
|
|
144
|
-
const block = overview?.blocks.find((b) => b.id === blockId);
|
|
145
|
-
return timedOutResult({
|
|
146
|
-
status: block?.calculationStatus ?? "Unknown",
|
|
147
|
-
hint: "The block is still running. Call await_block_done again with a longer timeout.",
|
|
148
|
-
});
|
|
149
|
-
});
|
|
416
|
+
async (input, call) => {
|
|
417
|
+
return await ctx.withProject(input.projectId, (project) =>
|
|
418
|
+
awaitBlockDone(project, input, { call }),
|
|
419
|
+
);
|
|
150
420
|
},
|
|
151
421
|
);
|
|
152
422
|
}
|
|
@@ -3,6 +3,7 @@ import { z } from "zod";
|
|
|
3
3
|
import { projectIdInput, blockIdInput } from "./ids";
|
|
4
4
|
import type { ToolContext, ToolRegistrar } from "./types";
|
|
5
5
|
import { errorResult, hints, textResult } from "./types";
|
|
6
|
+
import { findBlock } from "./readiness";
|
|
6
7
|
|
|
7
8
|
/** An action context tree node as the tool returns it: cut to the requested depth and history. */
|
|
8
9
|
export type LimitedBlockActionContextNode = Omit<BlockActionContextNode, "children"> & {
|
|
@@ -99,6 +100,8 @@ export function registerBlockActionContextTools(server: ToolRegistrar, ctx: Tool
|
|
|
99
100
|
},
|
|
100
101
|
async ({ projectId, blockId, mode, contextId, maxDepth, maxTransitions }) => {
|
|
101
102
|
return await ctx.withProject(projectId, async (project) => {
|
|
103
|
+
const { error } = await findBlock(project, projectId, blockId);
|
|
104
|
+
if (error) return error;
|
|
102
105
|
const tree = await project.getBlockActionContext(blockId, mode ?? "prod");
|
|
103
106
|
if (tree === undefined)
|
|
104
107
|
return errorResult(
|
package/src/tools/block-api.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { type BlockCodeFeatureFlags, checkBlockFlag } from "@milaboratories/pl-model-common";
|
|
2
2
|
import { BLOCK_STORAGE_FACADE_VERSION } from "@platforma-sdk/model";
|
|
3
|
+
import { isModelApiVersionMismatchError } from "./errors";
|
|
3
4
|
import { errorResult } from "./types";
|
|
4
5
|
|
|
5
6
|
/**
|
|
@@ -18,7 +19,21 @@ export const UNSUPPORTED_BLOCK_API_TEXT =
|
|
|
18
19
|
* other error.
|
|
19
20
|
*/
|
|
20
21
|
export function unsupportedBlockApiResult(e: unknown) {
|
|
21
|
-
return e
|
|
22
|
-
|
|
23
|
-
|
|
22
|
+
return isModelApiVersionMismatchError(e) ? errorResult(UNSUPPORTED_BLOCK_API_TEXT) : undefined;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* The same guard asked before a write is planned, from the block's entry in the project overview:
|
|
27
|
+
* the error result when its feature flags put it on another block API, `undefined` when they put
|
|
28
|
+
* it on {@link SUPPORTED_BLOCK_API}. A block whose pack is not loaded yet has no flags; it is not
|
|
29
|
+
* refused here, and the middle layer's own check decides at the write.
|
|
30
|
+
*
|
|
31
|
+
* So a block on block API 1 is told it is not supported, rather than being refused for whatever
|
|
32
|
+
* its storage, which looks nothing like BlockModelV3 data, fails first.
|
|
33
|
+
*/
|
|
34
|
+
export function blockApiGuardResult(block: { featureFlags: BlockCodeFeatureFlags | undefined }) {
|
|
35
|
+
if (block.featureFlags === undefined) return undefined;
|
|
36
|
+
return checkBlockFlag(block.featureFlags, "requiresModelAPIVersion", SUPPORTED_BLOCK_API)
|
|
37
|
+
? undefined
|
|
38
|
+
: errorResult(UNSUPPORTED_BLOCK_API_TEXT);
|
|
24
39
|
}
|