@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/block-state.ts
CHANGED
|
@@ -1,12 +1,527 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { isDeepStrictEqual } from "node:util";
|
|
2
|
+
import type { BlockCodeFeatureFlags, BlockId } from "@milaboratories/pl-model-common";
|
|
3
|
+
import type { BlockReadiness } from "@milaboratories/pl-middle-layer";
|
|
3
4
|
import { z } from "zod";
|
|
4
|
-
import {
|
|
5
|
-
import
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
5
|
+
import { isPlainObject } from "@milaboratories/helpers";
|
|
6
|
+
import { projectIdInput, blockIdInput, pluginHandleInput } from "./ids";
|
|
7
|
+
import type { AuthorMarker, ToolContext, ToolRegistrar } from "./types";
|
|
8
|
+
import {
|
|
9
|
+
blockView,
|
|
10
|
+
diffValues,
|
|
11
|
+
missingTopLevelKeys,
|
|
12
|
+
pluginOf,
|
|
13
|
+
readStorageView,
|
|
14
|
+
} from "./block-storage";
|
|
15
|
+
import { clip, outputsView } from "./tokens";
|
|
16
|
+
import {
|
|
17
|
+
blockStatus,
|
|
18
|
+
buildReadiness,
|
|
19
|
+
findBlock,
|
|
20
|
+
type Readiness,
|
|
21
|
+
type ReadinessOverviewBlock,
|
|
22
|
+
} from "./readiness";
|
|
23
|
+
import {
|
|
24
|
+
awaitBlockSettled,
|
|
25
|
+
clampWait,
|
|
26
|
+
pendingOutputKeys,
|
|
27
|
+
progressReporter,
|
|
28
|
+
type SettleBlockState,
|
|
29
|
+
type SettleProject,
|
|
30
|
+
type WaitContext,
|
|
31
|
+
} from "./settle";
|
|
32
|
+
import { importsOf } from "./imports";
|
|
33
|
+
import { expressionTimeoutText, safeEval } from "./sandbox";
|
|
34
|
+
import { invalidHandlesResult, invalidNewImportHandles, type FileLsDriver } from "./files";
|
|
35
|
+
import { page, pagingArgs } from "./paging";
|
|
36
|
+
import { blockApiGuardResult, unsupportedBlockApiResult } from "./block-api";
|
|
8
37
|
import { errorResult, hints, textResult, transformFailedResult } from "./types";
|
|
9
38
|
|
|
39
|
+
export { blockStatus, type BlockStatus } from "./readiness";
|
|
40
|
+
|
|
41
|
+
/** How many blocks get_project_overview returns when the caller does not say. */
|
|
42
|
+
export const OVERVIEW_DEFAULT_LIMIT = 50;
|
|
43
|
+
/** The most blocks one get_project_overview call returns. */
|
|
44
|
+
export const OVERVIEW_MAX_LIMIT = 200;
|
|
45
|
+
|
|
46
|
+
/** The most characters of an output or exports error the project overview carries per block. */
|
|
47
|
+
const OVERVIEW_ERROR_CHARS = 300;
|
|
48
|
+
|
|
49
|
+
/** Default timeout of a `transform` or `update` expression, in milliseconds. */
|
|
50
|
+
export const DEFAULT_EXPRESSION_TIMEOUT = 5000;
|
|
51
|
+
|
|
52
|
+
/** The default wait of a write for the block's outputs to settle, in milliseconds. */
|
|
53
|
+
export const WRITE_WAIT_DEFAULT_MS = 30_000;
|
|
54
|
+
/** The shortest wait a write may ask for. */
|
|
55
|
+
export const WRITE_WAIT_MIN_MS = 1_000;
|
|
56
|
+
/** The longest wait a write may ask for: for clients whose request timeout is longer than the common 60 s. */
|
|
57
|
+
export const WRITE_WAIT_MAX_MS = 120_000;
|
|
58
|
+
|
|
59
|
+
/** What {@link writeBlockData} needs of a project; a `Project` of the middle layer has it. */
|
|
60
|
+
export type BlockDataProject = {
|
|
61
|
+
overview: {
|
|
62
|
+
getValue(): Promise<
|
|
63
|
+
| {
|
|
64
|
+
blocks: { id: string; featureFlags: BlockCodeFeatureFlags | undefined }[];
|
|
65
|
+
}
|
|
66
|
+
| undefined
|
|
67
|
+
>;
|
|
68
|
+
};
|
|
69
|
+
getBlockState(blockId: BlockId): { getValue(): Promise<{ blockStorage: unknown }> };
|
|
70
|
+
mutateBlockStorage(
|
|
71
|
+
blockId: BlockId,
|
|
72
|
+
payload: { operation: string; value: unknown },
|
|
73
|
+
author?: AuthorMarker,
|
|
74
|
+
): Promise<void>;
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
const setBlockDataInput = z.object({
|
|
78
|
+
projectId: projectIdInput.describe("Project ID"),
|
|
79
|
+
blockId: blockIdInput.describe("Block ID"),
|
|
80
|
+
pluginId: pluginHandleInput
|
|
81
|
+
.optional()
|
|
82
|
+
.describe(
|
|
83
|
+
"Write the data of this plugin of the block instead of the block's own data: a handle " +
|
|
84
|
+
"listed under `plugins` by get_block_state.",
|
|
85
|
+
),
|
|
86
|
+
data: z
|
|
87
|
+
.unknown()
|
|
88
|
+
.optional()
|
|
89
|
+
.describe(
|
|
90
|
+
"The whole new data (the block's, or the plugin's with `pluginId`): the value " +
|
|
91
|
+
"get_block_state returned, changed. Not a patch: it replaces the stored value whole, " +
|
|
92
|
+
"so a change made in between by someone else (the user in the application) is " +
|
|
93
|
+
"overwritten. To change some fields of the current value, send `update` instead. " +
|
|
94
|
+
"Give `data` or `update`, not both.",
|
|
95
|
+
),
|
|
96
|
+
update: z
|
|
97
|
+
.string()
|
|
98
|
+
.optional()
|
|
99
|
+
.describe(
|
|
100
|
+
"Instead of `data`: a JS expression evaluated in the server against the current data, " +
|
|
101
|
+
"available as `data`; its value is the whole new data. " +
|
|
102
|
+
"Example: `({ ...data, numbers: [1, 2, 3] })`.",
|
|
103
|
+
),
|
|
104
|
+
removeKeys: z
|
|
105
|
+
.array(z.string())
|
|
106
|
+
.optional()
|
|
107
|
+
.describe(
|
|
108
|
+
"Top-level keys of the current data the write removes on purpose. A write that leaves out " +
|
|
109
|
+
"a top-level key not named here is refused.",
|
|
110
|
+
),
|
|
111
|
+
updateTimeout: z
|
|
112
|
+
.number()
|
|
113
|
+
.optional()
|
|
114
|
+
.describe(expressionTimeoutText("the `update` expression", DEFAULT_EXPRESSION_TIMEOUT)),
|
|
115
|
+
wait: z
|
|
116
|
+
.boolean()
|
|
117
|
+
.optional()
|
|
118
|
+
.describe(
|
|
119
|
+
"Wait for the block's outputs to settle on the new data before answering (default true).",
|
|
120
|
+
),
|
|
121
|
+
timeout: z
|
|
122
|
+
.number()
|
|
123
|
+
.optional()
|
|
124
|
+
.describe(
|
|
125
|
+
`The longest wait in ms (default ${WRITE_WAIT_DEFAULT_MS}, at most ${WRITE_WAIT_MAX_MS}).`,
|
|
126
|
+
),
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
/** What checks the import handles a write adds: the middle layer's ls driver. */
|
|
130
|
+
export type ImportHandleChecker = Pick<FileLsDriver, "getLocalFileSize">;
|
|
131
|
+
|
|
132
|
+
/** Input of the `set_block_data` tool. */
|
|
133
|
+
export type SetBlockDataInput = z.infer<typeof setBlockDataInput>;
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* The third way to give the new value of a write, next to `data` and `update`: a function of the
|
|
137
|
+
* current value and the raw storage it was read from. It refuses, or gives the new value.
|
|
138
|
+
*/
|
|
139
|
+
export type ComputeNext = (
|
|
140
|
+
current: unknown,
|
|
141
|
+
rawStorage: unknown,
|
|
142
|
+
) => Promise<WriteRefused | { kind: "next"; next: unknown }>;
|
|
143
|
+
|
|
144
|
+
/** What {@link writeBlockData} writes: the tool's input, or a {@link ComputeNext} in place of `data`. */
|
|
145
|
+
export type WriteInput = Omit<SetBlockDataInput, "projectId" | "wait" | "timeout"> & {
|
|
146
|
+
computeNext?: ComputeNext;
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
/** What {@link writeBlockData} did. */
|
|
150
|
+
export type WriteOutcome =
|
|
151
|
+
| WriteRefused
|
|
152
|
+
| { kind: "written"; unchanged: boolean; changes: ReturnType<typeof diffValues> };
|
|
153
|
+
|
|
154
|
+
/** A write that was refused: nothing was written; `result` is the tool's error result. */
|
|
155
|
+
export type WriteRefused = { kind: "refused"; result: ReturnType<typeof errorResult> };
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* The `set_block_data` tool on one project: {@link writeBlockData}, then the wait for the block's
|
|
159
|
+
* outputs to settle on the new data ({@link settleAfterChange}). Once the value is written the
|
|
160
|
+
* result is `ok`, whatever the wait finds.
|
|
161
|
+
*/
|
|
162
|
+
export async function writeAndSettle(
|
|
163
|
+
project: BlockDataProject & ChangeProject,
|
|
164
|
+
author: AuthorMarker,
|
|
165
|
+
input: Omit<SetBlockDataInput, "projectId">,
|
|
166
|
+
importHandles?: ImportHandleChecker,
|
|
167
|
+
call?: WaitContext,
|
|
168
|
+
) {
|
|
169
|
+
const { wait, timeout, ...write } = input;
|
|
170
|
+
const outcome = await writeBlockData(project, author, write, importHandles);
|
|
171
|
+
if (outcome.kind === "refused") return outcome.result;
|
|
172
|
+
return textResult({
|
|
173
|
+
ok: true,
|
|
174
|
+
...writeSummary(outcome),
|
|
175
|
+
...(await settleAfterChange(project, input.blockId, {
|
|
176
|
+
wait: wait ?? true,
|
|
177
|
+
timeoutMs: clampWait(timeout, WRITE_WAIT_DEFAULT_MS, WRITE_WAIT_MIN_MS, WRITE_WAIT_MAX_MS),
|
|
178
|
+
call,
|
|
179
|
+
notePrerun: true,
|
|
180
|
+
})),
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/** The `unchanged` and `changes` part of the result of a write. */
|
|
185
|
+
function writeSummary(outcome: Extract<WriteOutcome, { kind: "written" }>) {
|
|
186
|
+
return outcome.unchanged ? { unchanged: true, changes: [] } : outcome.changes;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Writes a block's data, or one plugin's data, whole (D-004, D-007). The steps:
|
|
191
|
+
*
|
|
192
|
+
* 1. the block must be in the project;
|
|
193
|
+
* 2. the current value is read from the block's storage; a plugin must be one the block registers;
|
|
194
|
+
* 3. the new value is `data`, the value of `update` evaluated against the current one, or the
|
|
195
|
+
* value `computeNext` gives;
|
|
196
|
+
* 4. guards: a plain-object value stays a plain object; no top-level key of the current value is
|
|
197
|
+
* dropped unless it is named in `removeKeys` (not asked of `computeNext`, which decides what
|
|
198
|
+
* its value keeps);
|
|
199
|
+
* 5. with `importHandles`, the import handles of the new value that the current one does not have
|
|
200
|
+
* must check out (see {@link invalidNewImportHandles});
|
|
201
|
+
* 6. a value equal to the current one is not written: `unchanged: true`;
|
|
202
|
+
* 7. one `mutateBlockStorage`: `update-block-data`, or `update-plugin-data` with `pluginId`.
|
|
203
|
+
*
|
|
204
|
+
* Writes of this server to one block run one at a time with the other changes of the block
|
|
205
|
+
* ({@link oneAtATime}), so each reads what the one before it wrote. A write from elsewhere
|
|
206
|
+
* (the application, another server) between the read of step 2 and the write of step 7 is
|
|
207
|
+
* overwritten: the last writer wins.
|
|
208
|
+
*
|
|
209
|
+
* Returns whether the value was written and its changed paths, or the error result of a refusal
|
|
210
|
+
* (nothing is written then).
|
|
211
|
+
*/
|
|
212
|
+
export async function writeBlockData(
|
|
213
|
+
project: BlockDataProject,
|
|
214
|
+
author: AuthorMarker,
|
|
215
|
+
input: WriteInput,
|
|
216
|
+
importHandles?: ImportHandleChecker,
|
|
217
|
+
): Promise<WriteOutcome> {
|
|
218
|
+
return await oneAtATime(project, input.blockId, () =>
|
|
219
|
+
writeNow(project, author, input, importHandles),
|
|
220
|
+
);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/** The changes of this server in progress, per project and block. */
|
|
224
|
+
const blockQueues = new WeakMap<object, Map<BlockId, Promise<void>>>();
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Runs `fn`, a change of one block of the project (its data, its block pack, its removal), after
|
|
228
|
+
* the changes of that block this server started before it (Q-020). Changes of the block list need
|
|
229
|
+
* no queue here: the middle layer runs the project's transactions one at a time.
|
|
230
|
+
*/
|
|
231
|
+
export async function oneAtATime<T>(
|
|
232
|
+
project: object,
|
|
233
|
+
blockId: BlockId,
|
|
234
|
+
fn: () => Promise<T>,
|
|
235
|
+
): Promise<T> {
|
|
236
|
+
let queues = blockQueues.get(project);
|
|
237
|
+
if (queues === undefined) {
|
|
238
|
+
queues = new Map();
|
|
239
|
+
blockQueues.set(project, queues);
|
|
240
|
+
}
|
|
241
|
+
const run = (queues.get(blockId) ?? Promise.resolve()).then(fn);
|
|
242
|
+
const tail = run.then(
|
|
243
|
+
() => {},
|
|
244
|
+
() => {},
|
|
245
|
+
);
|
|
246
|
+
queues.set(blockId, tail);
|
|
247
|
+
try {
|
|
248
|
+
return await run;
|
|
249
|
+
} finally {
|
|
250
|
+
if (queues.get(blockId) === tail) queues.delete(blockId);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
async function writeNow(
|
|
255
|
+
project: BlockDataProject,
|
|
256
|
+
author: AuthorMarker,
|
|
257
|
+
input: WriteInput,
|
|
258
|
+
importHandles: ImportHandleChecker | undefined,
|
|
259
|
+
): Promise<WriteOutcome> {
|
|
260
|
+
const { blockId, pluginId, data, update, computeNext } = input;
|
|
261
|
+
const ways = [data, update, computeNext].filter((way) => way !== undefined).length;
|
|
262
|
+
if (ways !== 1)
|
|
263
|
+
return refuse(
|
|
264
|
+
errorResult(
|
|
265
|
+
"Give either `data` or `update`. Nothing was changed.",
|
|
266
|
+
"`data` is the whole new value; `update` is an expression computing it from the current " +
|
|
267
|
+
"one.",
|
|
268
|
+
),
|
|
269
|
+
);
|
|
270
|
+
|
|
271
|
+
const overview = await project.overview.getValue();
|
|
272
|
+
if (!overview)
|
|
273
|
+
return refuse(errorResult("Project overview not available yet. Nothing was changed."));
|
|
274
|
+
const block = overview.blocks.find((b) => b.id === blockId);
|
|
275
|
+
if (!block)
|
|
276
|
+
return refuse(
|
|
277
|
+
errorResult(
|
|
278
|
+
`Block ${blockId} not found. Nothing was changed.`,
|
|
279
|
+
"Use get_project_overview to list the block IDs of the project.",
|
|
280
|
+
),
|
|
281
|
+
);
|
|
282
|
+
// Before the write is planned: a block on block API 1 keeps its data in another shape, and a
|
|
283
|
+
// guard of the plan would refuse it for that first (Q-042).
|
|
284
|
+
const otherApi = blockApiGuardResult(block);
|
|
285
|
+
if (otherApi) return refuse(otherApi);
|
|
286
|
+
|
|
287
|
+
const state = await project.getBlockState(blockId).getValue();
|
|
288
|
+
const planned = await planWrite(state.blockStorage, input, importHandles);
|
|
289
|
+
if (planned.kind !== "write") return planned;
|
|
290
|
+
const { current, next } = planned;
|
|
291
|
+
if (isDeepStrictEqual(current, next))
|
|
292
|
+
return { kind: "written", unchanged: true, changes: { changes: [] } };
|
|
293
|
+
|
|
294
|
+
const payload =
|
|
295
|
+
pluginId !== undefined
|
|
296
|
+
? { operation: "update-plugin-data", pluginId, value: next }
|
|
297
|
+
: { operation: "update-block-data", value: next };
|
|
298
|
+
try {
|
|
299
|
+
await project.mutateBlockStorage(blockId, payload, author);
|
|
300
|
+
} catch (e: unknown) {
|
|
301
|
+
const unsupported = unsupportedBlockApiResult(e);
|
|
302
|
+
if (unsupported) return refuse(unsupported);
|
|
303
|
+
throw e;
|
|
304
|
+
}
|
|
305
|
+
return { kind: "written", unchanged: false, changes: diffValues(current, next) };
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/** A refused write with `result` as the tool's error result. */
|
|
309
|
+
export function refuse(result: ReturnType<typeof errorResult>): WriteRefused {
|
|
310
|
+
return { kind: "refused", result };
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/**
|
|
314
|
+
* Steps 2–5 of {@link writeBlockData} on the block's storage: the current value, the new one and
|
|
315
|
+
* the guards between them, the check of new import handles included.
|
|
316
|
+
*/
|
|
317
|
+
async function planWrite(
|
|
318
|
+
rawStorage: unknown,
|
|
319
|
+
input: WriteInput,
|
|
320
|
+
importHandles: ImportHandleChecker | undefined,
|
|
321
|
+
): Promise<WriteRefused | { kind: "write"; current: unknown; next: unknown }> {
|
|
322
|
+
const { blockId, pluginId, data, update, computeNext, removeKeys, updateTimeout } = input;
|
|
323
|
+
const storage = readStorageView(rawStorage);
|
|
324
|
+
let current: unknown;
|
|
325
|
+
if (pluginId !== undefined) {
|
|
326
|
+
const plugin = pluginOf(storage, pluginId);
|
|
327
|
+
if (plugin === undefined) {
|
|
328
|
+
const known = Object.keys(storage.plugins);
|
|
329
|
+
return refuse(
|
|
330
|
+
errorResult(
|
|
331
|
+
`Block ${blockId} has no plugin "${pluginId}". Nothing was changed.`,
|
|
332
|
+
known.length > 0
|
|
333
|
+
? `Its plugins: ${known.join(", ")}.`
|
|
334
|
+
: "The block has no plugins: omit pluginId to write the block's own data.",
|
|
335
|
+
),
|
|
336
|
+
);
|
|
337
|
+
}
|
|
338
|
+
current = plugin.data;
|
|
339
|
+
} else {
|
|
340
|
+
current = storage.data;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
let next: unknown;
|
|
344
|
+
if (computeNext !== undefined) {
|
|
345
|
+
const computed = await computeNext(current, rawStorage);
|
|
346
|
+
if (computed.kind === "refused") return computed;
|
|
347
|
+
next = computed.next;
|
|
348
|
+
} else if (update !== undefined) {
|
|
349
|
+
try {
|
|
350
|
+
next = await safeEval(update, { data: current }, updateTimeout ?? DEFAULT_EXPRESSION_TIMEOUT);
|
|
351
|
+
} catch (e: unknown) {
|
|
352
|
+
return refuse(
|
|
353
|
+
transformFailedResult(
|
|
354
|
+
e,
|
|
355
|
+
"Nothing was changed. The expression gets the current data as `data` and must return " +
|
|
356
|
+
"the whole new data, e.g. `({ ...data, key: 1 })`.",
|
|
357
|
+
),
|
|
358
|
+
);
|
|
359
|
+
}
|
|
360
|
+
if (next === undefined || next === null)
|
|
361
|
+
return refuse(
|
|
362
|
+
errorResult(
|
|
363
|
+
"The `update` expression returned no value. Nothing was changed.",
|
|
364
|
+
"Return the whole new data, e.g. `({ ...data, key: 1 })`.",
|
|
365
|
+
),
|
|
366
|
+
);
|
|
367
|
+
} else {
|
|
368
|
+
next = data;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
if (isPlainObject(current) && !isPlainObject(next))
|
|
372
|
+
return refuse(
|
|
373
|
+
errorResult(
|
|
374
|
+
"The current data is an object; the new data must be an object too. Nothing was changed.",
|
|
375
|
+
"Read the data with get_block_state, change it and write the whole object back.",
|
|
376
|
+
),
|
|
377
|
+
);
|
|
378
|
+
|
|
379
|
+
const allowed = new Set(removeKeys ?? []);
|
|
380
|
+
const dropped =
|
|
381
|
+
computeNext !== undefined
|
|
382
|
+
? []
|
|
383
|
+
: missingTopLevelKeys(current, next).filter((k) => !allowed.has(k));
|
|
384
|
+
if (dropped.length > 0)
|
|
385
|
+
return refuse(
|
|
386
|
+
errorResult(
|
|
387
|
+
`The new data leaves out top-level keys of the current data: ${dropped.join(", ")}. ` +
|
|
388
|
+
"Nothing was changed.",
|
|
389
|
+
"Send the whole object: read it with get_block_state, change it and write all of it " +
|
|
390
|
+
"back. To remove keys on purpose, name them in `removeKeys`.",
|
|
391
|
+
),
|
|
392
|
+
);
|
|
393
|
+
|
|
394
|
+
// A value equal to the current one has no new handles: nothing is checked.
|
|
395
|
+
if (importHandles !== undefined) {
|
|
396
|
+
const invalid = await invalidNewImportHandles(importHandles, current, next);
|
|
397
|
+
if (invalid.length > 0) return refuse(invalidHandlesResult(invalid));
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
return { kind: "write", current, next };
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
/** What {@link readinessOf} reads of a project; a middle-layer `Project` has it. */
|
|
404
|
+
export type ReadinessProject = {
|
|
405
|
+
overview: { getValue(): Promise<{ blocks: ReadinessOverviewBlock[] } | undefined> };
|
|
406
|
+
getBlockReadiness(blockId: BlockId): { getValue(): Promise<BlockReadiness | undefined> };
|
|
407
|
+
};
|
|
408
|
+
|
|
409
|
+
/**
|
|
410
|
+
* The readiness of a block (see {@link Readiness}): the block's overview entry, those of the blocks
|
|
411
|
+
* above, and what its model makes of its current data. `undefined` when the project has no such
|
|
412
|
+
* block. A model that cannot be run leaves out what only the model says.
|
|
413
|
+
*/
|
|
414
|
+
export async function readinessOf(
|
|
415
|
+
project: ReadinessProject,
|
|
416
|
+
blockId: BlockId,
|
|
417
|
+
): Promise<Readiness | undefined> {
|
|
418
|
+
const overview = await project.overview.getValue();
|
|
419
|
+
const block = overview?.blocks.find((b) => b.id === blockId);
|
|
420
|
+
if (overview === undefined || block === undefined) return undefined;
|
|
421
|
+
let model;
|
|
422
|
+
try {
|
|
423
|
+
model = await project.getBlockReadiness(blockId).getValue();
|
|
424
|
+
} catch {
|
|
425
|
+
model = undefined;
|
|
426
|
+
}
|
|
427
|
+
return buildReadiness(block, overview.blocks, model);
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
/** Options of {@link settleAfterChange}. */
|
|
431
|
+
export type SettleAfterChangeOptions = {
|
|
432
|
+
/** Wait for the outputs to settle; otherwise look once. */
|
|
433
|
+
wait: boolean;
|
|
434
|
+
timeoutMs: number;
|
|
435
|
+
/** The tool call: its abort signal ends the wait, its progress token asks for progress. */
|
|
436
|
+
call?: WaitContext;
|
|
437
|
+
/** Say when the pre-run outputs show an earlier configuration (after a write, D-008). */
|
|
438
|
+
notePrerun: boolean;
|
|
439
|
+
};
|
|
440
|
+
|
|
441
|
+
/** What {@link settleAfterChange} reads of a project; a middle-layer `Project` has it. */
|
|
442
|
+
export type ChangeProject = SettleProject<SettleBlockState> & ReadinessProject;
|
|
443
|
+
|
|
444
|
+
/**
|
|
445
|
+
* What a tool that changed a block adds to its result: whether the block's outputs settled on the
|
|
446
|
+
* change (after waiting up to `timeoutMs`), how long it waited, the readiness and an output
|
|
447
|
+
* summary; unsettled, what the outputs wait for.
|
|
448
|
+
*
|
|
449
|
+
* It never throws: the change is made by then, and an error result would have the agent make it
|
|
450
|
+
* again (a second block, a second write). A failure to read the block afterwards comes back as
|
|
451
|
+
* `settled: false` with `settleError`.
|
|
452
|
+
*/
|
|
453
|
+
export async function settleAfterChange(
|
|
454
|
+
project: ChangeProject,
|
|
455
|
+
blockId: BlockId,
|
|
456
|
+
options: SettleAfterChangeOptions,
|
|
457
|
+
): Promise<
|
|
458
|
+
Awaited<ReturnType<typeof settleNow>> | { settled: false; settleError: string; note: string }
|
|
459
|
+
> {
|
|
460
|
+
try {
|
|
461
|
+
return await settleNow(project, blockId, options);
|
|
462
|
+
} catch (e: unknown) {
|
|
463
|
+
return {
|
|
464
|
+
settled: false,
|
|
465
|
+
settleError: e instanceof Error ? e.message : String(e),
|
|
466
|
+
note:
|
|
467
|
+
"The change was made; reading the block afterwards failed. Do not repeat the change: " +
|
|
468
|
+
"get_block_state shows the block.",
|
|
469
|
+
};
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
async function settleNow(
|
|
474
|
+
project: ChangeProject,
|
|
475
|
+
blockId: BlockId,
|
|
476
|
+
options: SettleAfterChangeOptions,
|
|
477
|
+
) {
|
|
478
|
+
const settle = await awaitBlockSettled(project, blockId, {
|
|
479
|
+
timeoutMs: options.wait ? options.timeoutMs : 0,
|
|
480
|
+
signal: options.call?.mcpReq.signal,
|
|
481
|
+
waitForRun: false,
|
|
482
|
+
onProgress: options.wait
|
|
483
|
+
? progressReporter(options.call, "Waiting for the block's outputs to settle")
|
|
484
|
+
: undefined,
|
|
485
|
+
});
|
|
486
|
+
const readiness = await readinessOf(project, blockId);
|
|
487
|
+
const imports = importsOf(settle.state.outputs);
|
|
488
|
+
const notes: string[] = [];
|
|
489
|
+
if (options.notePrerun && readiness?.prerun !== undefined && readiness.prerun !== "staged")
|
|
490
|
+
notes.push(
|
|
491
|
+
`The pre-run does not run on this data (prerun: ${readiness.prerun}), so the pre-run ` +
|
|
492
|
+
"outputs still show the previous configuration.",
|
|
493
|
+
);
|
|
494
|
+
if (!settle.settled && settle.stoppedBy === "upstream-not-run" && settle.waitingOn !== undefined)
|
|
495
|
+
notes.push(
|
|
496
|
+
`The outputs wait for block ${settle.waitingOn} above, which has not run; they compute ` +
|
|
497
|
+
"after it runs: run_block on this block starts it with the other stale blocks above.",
|
|
498
|
+
);
|
|
499
|
+
else if (!settle.settled && settle.waitingOn !== undefined)
|
|
500
|
+
notes.push(
|
|
501
|
+
`The outputs wait for block ${settle.waitingOn} above, which is running; ` +
|
|
502
|
+
"await_block_done on it waits for it.",
|
|
503
|
+
);
|
|
504
|
+
else if (!settle.settled && options.wait)
|
|
505
|
+
notes.push("The outputs are still computing; get_block_state shows them when they settle.");
|
|
506
|
+
return {
|
|
507
|
+
settled: settle.settled,
|
|
508
|
+
waitedMs: settle.waitedMs,
|
|
509
|
+
...(settle.settled
|
|
510
|
+
? {}
|
|
511
|
+
: {
|
|
512
|
+
pendingOutputs: settle.pendingOutputs,
|
|
513
|
+
...(settle.waitingOn !== undefined
|
|
514
|
+
? { waitingOn: settle.waitingOn, waitingOnStatus: settle.waitingOnStatus }
|
|
515
|
+
: {}),
|
|
516
|
+
runningUpstreams: settle.runningUpstreams,
|
|
517
|
+
}),
|
|
518
|
+
readiness,
|
|
519
|
+
...outputsView(settle.state.outputs),
|
|
520
|
+
...(imports !== undefined ? { imports } : {}),
|
|
521
|
+
...(notes.length > 0 ? { note: notes.join(" ") } : {}),
|
|
522
|
+
};
|
|
523
|
+
}
|
|
524
|
+
|
|
10
525
|
export function registerBlockStateTools(server: ToolRegistrar, ctx: ToolContext): void {
|
|
11
526
|
server.registerTool(
|
|
12
527
|
"get_project_overview",
|
|
@@ -14,29 +529,72 @@ export function registerBlockStateTools(server: ToolRegistrar, ctx: ToolContext)
|
|
|
14
529
|
title: "Get Project Overview",
|
|
15
530
|
annotations: hints.read,
|
|
16
531
|
description:
|
|
17
|
-
"Get project
|
|
532
|
+
"Get the project's blocks in order, each with its status and what it depends on. " +
|
|
533
|
+
"`status`: running, limbo (results from before an upstream changed: it failed, was " +
|
|
534
|
+
"stopped or ran again), failed (the last run ended with errors in its outputs; " +
|
|
535
|
+
"`outputsError` holds the error, get_block_state reads each output's), up-to-date, " +
|
|
536
|
+
"ready (can run), blocked (cannot run: invalid inputs, an invalid upstream or a " +
|
|
537
|
+
"missing reference; get_block_state says why). " +
|
|
538
|
+
"Built from stored state; no block model is run.\n\n" +
|
|
539
|
+
`Result: \`{ label, blocks, totalBlocks, offset, hasMore, notFound? }\`: at most ` +
|
|
540
|
+
`${OVERVIEW_DEFAULT_LIMIT} blocks unless \`limit\` says otherwise; page with \`offset\`, ` +
|
|
541
|
+
"or ask for some blocks by id with `blockIds`.",
|
|
18
542
|
inputSchema: z.object({
|
|
19
543
|
projectId: projectIdInput.describe("Project ID"),
|
|
544
|
+
blockIds: z
|
|
545
|
+
.array(blockIdInput)
|
|
546
|
+
.optional()
|
|
547
|
+
.describe(
|
|
548
|
+
"Only these blocks, in project order; `notFound` lists the ids the project lacks.",
|
|
549
|
+
),
|
|
550
|
+
...pagingArgs("blocks", OVERVIEW_DEFAULT_LIMIT, OVERVIEW_MAX_LIMIT),
|
|
20
551
|
}),
|
|
21
552
|
},
|
|
22
|
-
async ({ projectId }) => {
|
|
553
|
+
async ({ projectId, blockIds, limit, offset }) => {
|
|
23
554
|
return await ctx.withProject(projectId, async (project) => {
|
|
24
555
|
const overview = await project.overview.getValue();
|
|
25
556
|
if (!overview) return errorResult("Project overview not available yet.");
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
blocks: overview.blocks.
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
557
|
+
const wanted = blockIds === undefined ? undefined : new Set<string>(blockIds);
|
|
558
|
+
const selected =
|
|
559
|
+
wanted === undefined ? overview.blocks : overview.blocks.filter((b) => wanted.has(b.id));
|
|
560
|
+
const known = new Set(overview.blocks.map((b) => b.id));
|
|
561
|
+
const notFound = (blockIds ?? []).filter((id) => !known.has(id));
|
|
562
|
+
const { items, total, ...paging } = page(selected, offset, limit ?? OVERVIEW_DEFAULT_LIMIT);
|
|
563
|
+
return textResult(
|
|
564
|
+
{
|
|
565
|
+
label: overview.meta.label,
|
|
566
|
+
totalBlocks: total,
|
|
567
|
+
...paging,
|
|
568
|
+
...(notFound.length > 0 ? { notFound } : {}),
|
|
569
|
+
blocks: items.map((b) => ({
|
|
570
|
+
id: b.id,
|
|
571
|
+
title: b.title ?? b.label,
|
|
572
|
+
subtitle: b.subtitle,
|
|
573
|
+
status: blockStatus(b),
|
|
574
|
+
calculationStatus: b.calculationStatus,
|
|
575
|
+
canRun: b.canRun,
|
|
576
|
+
stale: b.stale,
|
|
577
|
+
inputsValid: b.inputsValid,
|
|
578
|
+
missingReference: b.missingReference,
|
|
579
|
+
outputErrors: b.outputErrors,
|
|
580
|
+
outputsError:
|
|
581
|
+
b.outputsError === undefined
|
|
582
|
+
? undefined
|
|
583
|
+
: clip(b.outputsError, OVERVIEW_ERROR_CHARS),
|
|
584
|
+
exportsError:
|
|
585
|
+
b.exportsError === undefined
|
|
586
|
+
? undefined
|
|
587
|
+
: clip(b.exportsError, OVERVIEW_ERROR_CHARS),
|
|
588
|
+
upstreams: b.upstreams,
|
|
589
|
+
downstreams: b.downstreams,
|
|
590
|
+
})),
|
|
591
|
+
},
|
|
592
|
+
{
|
|
593
|
+
hint:
|
|
594
|
+
"Ask for fewer blocks: a smaller `limit` with `offset` to page, or `blockIds` for " +
|
|
595
|
+
"the blocks you need.",
|
|
596
|
+
},
|
|
597
|
+
);
|
|
40
598
|
});
|
|
41
599
|
},
|
|
42
600
|
);
|
|
@@ -47,13 +605,25 @@ export function registerBlockStateTools(server: ToolRegistrar, ctx: ToolContext)
|
|
|
47
605
|
title: "Get Block State",
|
|
48
606
|
annotations: hints.read,
|
|
49
607
|
description:
|
|
50
|
-
"Get
|
|
51
|
-
"
|
|
52
|
-
"Default:
|
|
608
|
+
"Get one block: its data (the configuration set_block_data writes), the data of its " +
|
|
609
|
+
"plugins, and a summary of its outputs.\n\n" +
|
|
610
|
+
"Default result: `{ dataVersion, data, plugins?, outputs: [{ key, " +
|
|
611
|
+
"status, hasValue, tokensEstimate? }], outputsNote?, outputsSettled, readiness }`. " +
|
|
612
|
+
"Output `status` is ok, pending (no final value yet) or error; `outputsNote` says how " +
|
|
613
|
+
"to read an error. " +
|
|
614
|
+
"`outputsSettled` is true when no output is pending. `readiness`: `{ status, blockers: " +
|
|
615
|
+
"[{ kind, message, blockId? }], warnings, prerun, prerunReason? }`, why the block can or " +
|
|
616
|
+
"cannot run, in the words of the block's own model (kind args), or a missing reference " +
|
|
617
|
+
"or a block above that cannot run; `prerun` says whether the pre-run outputs follow the " +
|
|
618
|
+
"current data (staged) or still show an earlier one. The options a block offers for " +
|
|
619
|
+
"results of blocks above appear only once those results exist, and some blocks publish " +
|
|
620
|
+
"results only from their run: if an option is missing and the block above has not run, " +
|
|
621
|
+
"run it first (run_block, await_block_done).\n\n" +
|
|
53
622
|
"Transform examples:\n" +
|
|
54
|
-
"- `outputs.logs?.value` —
|
|
55
|
-
"- `data` —
|
|
56
|
-
|
|
623
|
+
"- `outputs.logs?.value` — one output value\n" +
|
|
624
|
+
"- `data.settings` — one part of the data\n" +
|
|
625
|
+
'- `plugins["counter"]` — one plugin\'s data\n' +
|
|
626
|
+
"- `({ preset: outputs.preset?.value, qc: outputs.qc?.value })` — several outputs",
|
|
57
627
|
inputSchema: z.object({
|
|
58
628
|
projectId: projectIdInput.describe("Project ID"),
|
|
59
629
|
blockId: blockIdInput.describe("Block ID"),
|
|
@@ -61,39 +631,48 @@ export function registerBlockStateTools(server: ToolRegistrar, ctx: ToolContext)
|
|
|
61
631
|
.string()
|
|
62
632
|
.optional()
|
|
63
633
|
.describe(
|
|
64
|
-
"JS expression evaluated server-side against
|
|
65
|
-
"Available variables: `data` (block
|
|
66
|
-
"Omit for default
|
|
634
|
+
"JS expression evaluated server-side against the block. " +
|
|
635
|
+
"Available variables: `data` (block data), `plugins` (plugin handle → plugin " +
|
|
636
|
+
"data), `outputs` (raw outputs object). Omit for the default view.",
|
|
67
637
|
),
|
|
68
638
|
transformTimeout: z
|
|
69
639
|
.number()
|
|
70
640
|
.optional()
|
|
71
|
-
.default(
|
|
72
|
-
.describe("
|
|
641
|
+
.default(DEFAULT_EXPRESSION_TIMEOUT)
|
|
642
|
+
.describe(expressionTimeoutText("transform evaluation", DEFAULT_EXPRESSION_TIMEOUT)),
|
|
73
643
|
}),
|
|
74
644
|
},
|
|
75
645
|
async ({ projectId, blockId, transform, transformTimeout }) => {
|
|
76
646
|
return await ctx.withProject(projectId, async (project) => {
|
|
647
|
+
const { error } = await findBlock(project, projectId, blockId);
|
|
648
|
+
if (error) return error;
|
|
77
649
|
const state = await project.getBlockState(blockId).getValue();
|
|
78
|
-
const data = deriveDataFromStorage(state.blockStorage);
|
|
79
650
|
if (transform) {
|
|
651
|
+
const storage = readStorageView(state.blockStorage);
|
|
80
652
|
try {
|
|
81
653
|
const result = await safeEval(
|
|
82
654
|
transform,
|
|
83
|
-
{
|
|
655
|
+
{
|
|
656
|
+
data: storage.data,
|
|
657
|
+
plugins: Object.fromEntries(
|
|
658
|
+
Object.entries(storage.plugins).map(([handle, p]) => [handle, p.data]),
|
|
659
|
+
),
|
|
660
|
+
outputs: state.outputs,
|
|
661
|
+
},
|
|
84
662
|
transformTimeout,
|
|
85
663
|
);
|
|
86
664
|
return textResult(result);
|
|
87
665
|
} catch (e: unknown) {
|
|
88
666
|
return transformFailedResult(
|
|
89
667
|
e,
|
|
90
|
-
"Check your JS expression syntax. Available variables: data, outputs.",
|
|
668
|
+
"Check your JS expression syntax. Available variables: data, plugins, outputs.",
|
|
91
669
|
);
|
|
92
670
|
}
|
|
93
671
|
}
|
|
94
672
|
return textResult({
|
|
95
|
-
|
|
96
|
-
|
|
673
|
+
...blockView(state),
|
|
674
|
+
outputsSettled: pendingOutputKeys(state.outputs).length === 0,
|
|
675
|
+
readiness: await readinessOf(project, blockId),
|
|
97
676
|
});
|
|
98
677
|
});
|
|
99
678
|
},
|
|
@@ -104,35 +683,49 @@ export function registerBlockStateTools(server: ToolRegistrar, ctx: ToolContext)
|
|
|
104
683
|
{
|
|
105
684
|
title: "Set Block Data",
|
|
106
685
|
annotations: hints.destructive,
|
|
107
|
-
description:
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
data
|
|
112
|
-
|
|
686
|
+
description:
|
|
687
|
+
"Write the whole data of a block (its configuration), or of one of its plugins with " +
|
|
688
|
+
"`pluginId`. For what a block's kind params describe, set_block_params is the way; " +
|
|
689
|
+
"set_block_data is for what params do not cover: table filters, plot settings, plugin " +
|
|
690
|
+
"data, blocks whose params are `{}` or that give none. " +
|
|
691
|
+
"Read the data with get_block_state, change it, and send the whole object " +
|
|
692
|
+
"back as `data` (it replaces the stored value, edits made since your read included); " +
|
|
693
|
+
"or send `update`, an expression the server evaluates against the current data, " +
|
|
694
|
+
"which is the way to edit some fields. Not a patch: a write that leaves out a top-level key of the current data " +
|
|
695
|
+
"is refused unless the key is named in `removeKeys`. A write equal to the current data " +
|
|
696
|
+
"is skipped (`unchanged: true`). Binary data reads as `{bytes: N}`, and a write stores " +
|
|
697
|
+
"that stub as it is: change such data with `update`, which runs on the stored value. " +
|
|
698
|
+
"The write re-derives the block's args; the block's results, and those of blocks " +
|
|
699
|
+
"below it, become stale until they run again. Only blocks on the current block API can " +
|
|
700
|
+
"be written.\n\n" +
|
|
701
|
+
"By default the call then waits (up to `timeout`) for the block's outputs to settle on " +
|
|
702
|
+
"the new data: the pre-run outputs (option lists, previews) recompute without run_block. " +
|
|
703
|
+
"A wait that runs out is not an error: the write was made, and `settled: false` says " +
|
|
704
|
+
"what is still pending.\n\n" +
|
|
705
|
+
"Result: `{ ok, unchanged?, changes: [{ path, op, from?, to? }], moreChanges?, settled, " +
|
|
706
|
+
"waitedMs, readiness, outputs, outputsNote?, note? }`: at most 50 changed paths; " +
|
|
707
|
+
"`settled` false comes with `pendingOutputs`, `waitingOn` (a block above that is running " +
|
|
708
|
+
"or has not run) and `runningUpstreams`, or with `settleError` when the block could not " +
|
|
709
|
+
"be read after the write (the write was made: do not repeat it); `readiness` as " +
|
|
710
|
+
"get_block_state gives it, the block's own messages " +
|
|
711
|
+
"on what keeps it from running; `note` says when the pre-run outputs still show the " +
|
|
712
|
+
"previous configuration.\n\n" +
|
|
713
|
+
"Files are given to a block as import handles from get_file_handles, written where the " +
|
|
714
|
+
"block's data takes a file. A handle the current data does not hold yet must check out " +
|
|
715
|
+
"(a local file that exists unchanged since the handle was made), or the write is " +
|
|
716
|
+
"refused; `imports` in the result and in get_block_state shows the upload progress.",
|
|
717
|
+
inputSchema: setBlockDataInput,
|
|
113
718
|
},
|
|
114
|
-
async ({ projectId,
|
|
115
|
-
return await ctx.withProject(projectId,
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
{ operation: "update-block-data", value },
|
|
125
|
-
ctx.getAuthorMarker(),
|
|
126
|
-
);
|
|
127
|
-
} catch (e: unknown) {
|
|
128
|
-
if (e instanceof ModelAPIVersionMismatchError) {
|
|
129
|
-
await project.setBlockArgs(blockId, value, ctx.getAuthorMarker());
|
|
130
|
-
} else {
|
|
131
|
-
throw e;
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
return textResult({ ok: true });
|
|
135
|
-
});
|
|
719
|
+
async ({ projectId, ...input }, call) => {
|
|
720
|
+
return await ctx.withProject(projectId, (project) =>
|
|
721
|
+
writeAndSettle(
|
|
722
|
+
project,
|
|
723
|
+
ctx.getAuthorMarker(),
|
|
724
|
+
input,
|
|
725
|
+
ctx.requireMl().internalDriverKit.lsDriver,
|
|
726
|
+
call,
|
|
727
|
+
),
|
|
728
|
+
);
|
|
136
729
|
},
|
|
137
730
|
);
|
|
138
731
|
}
|