@milaboratories/pl-mcp-server 53.1.0 → 55.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +30 -37
- package/dist/_virtual/_rolldown/runtime.cjs +22 -0
- package/dist/agent-tables.cjs +116 -0
- package/dist/agent-tables.cjs.map +1 -0
- package/dist/agent-tables.js +115 -0
- package/dist/agent-tables.js.map +1 -0
- package/dist/instructions.cjs +16 -12
- package/dist/instructions.cjs.map +1 -1
- package/dist/instructions.js +16 -12
- package/dist/instructions.js.map +1 -1
- package/dist/package.cjs +1 -1
- package/dist/package.js +1 -1
- package/dist/project-holds.cjs +122 -0
- package/dist/project-holds.cjs.map +1 -0
- package/dist/project-holds.js +122 -0
- package/dist/project-holds.js.map +1 -0
- package/dist/project-list.cjs +16 -0
- package/dist/project-list.cjs.map +1 -0
- package/dist/project-list.js +16 -0
- package/dist/project-list.js.map +1 -0
- package/dist/server-log.cjs +48 -18
- package/dist/server-log.cjs.map +1 -1
- package/dist/server-log.js +48 -19
- package/dist/server-log.js.map +1 -1
- package/dist/server.cjs +71 -18
- package/dist/server.cjs.map +1 -1
- package/dist/server.d.ts +37 -24
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +71 -19
- package/dist/server.js.map +1 -1
- package/dist/tools/agent-tables.cjs +424 -0
- package/dist/tools/agent-tables.cjs.map +1 -0
- package/dist/tools/agent-tables.js +417 -0
- package/dist/tools/agent-tables.js.map +1 -0
- package/dist/tools/await.cjs +216 -72
- package/dist/tools/await.cjs.map +1 -1
- package/dist/tools/await.js +212 -74
- package/dist/tools/await.js.map +1 -1
- package/dist/tools/block-action-context.cjs +14 -9
- package/dist/tools/block-action-context.cjs.map +1 -1
- package/dist/tools/block-action-context.js +14 -9
- package/dist/tools/block-action-context.js.map +1 -1
- package/dist/tools/block-api.cjs +17 -2
- package/dist/tools/block-api.cjs.map +1 -1
- package/dist/tools/block-api.js +17 -3
- package/dist/tools/block-api.js.map +1 -1
- package/dist/tools/block-params.cjs +275 -6
- package/dist/tools/block-params.cjs.map +1 -1
- package/dist/tools/block-params.js +273 -8
- package/dist/tools/block-params.js.map +1 -1
- package/dist/tools/block-state.cjs +343 -57
- package/dist/tools/block-state.cjs.map +1 -1
- package/dist/tools/block-state.js +337 -61
- package/dist/tools/block-state.js.map +1 -1
- package/dist/tools/block-storage.cjs +153 -0
- package/dist/tools/block-storage.cjs.map +1 -0
- package/dist/tools/block-storage.js +147 -0
- package/dist/tools/block-storage.js.map +1 -0
- package/dist/tools/block-updates.cjs +405 -0
- package/dist/tools/block-updates.cjs.map +1 -0
- package/dist/tools/block-updates.js +397 -0
- package/dist/tools/block-updates.js.map +1 -0
- package/dist/tools/blocks.cjs +199 -76
- package/dist/tools/blocks.cjs.map +1 -1
- package/dist/tools/blocks.js +193 -76
- package/dist/tools/blocks.js.map +1 -1
- package/dist/tools/data-query.cjs +470 -101
- package/dist/tools/data-query.cjs.map +1 -1
- package/dist/tools/data-query.js +465 -106
- package/dist/tools/data-query.js.map +1 -1
- package/dist/tools/errors.cjs +21 -0
- package/dist/tools/errors.cjs.map +1 -0
- package/dist/tools/errors.js +20 -0
- package/dist/tools/errors.js.map +1 -0
- package/dist/tools/files.cjs +643 -0
- package/dist/tools/files.cjs.map +1 -0
- package/dist/tools/files.js +613 -0
- package/dist/tools/files.js.map +1 -0
- package/dist/tools/ids.cjs +72 -0
- package/dist/tools/ids.cjs.map +1 -1
- package/dist/tools/ids.js +63 -2
- package/dist/tools/ids.js.map +1 -1
- package/dist/tools/imports.cjs +195 -0
- package/dist/tools/imports.cjs.map +1 -0
- package/dist/tools/imports.js +186 -0
- package/dist/tools/imports.js.map +1 -0
- package/dist/tools/logs.cjs +111 -30
- package/dist/tools/logs.cjs.map +1 -1
- package/dist/tools/logs.js +108 -31
- package/dist/tools/logs.js.map +1 -1
- package/dist/tools/paging.cjs +27 -0
- package/dist/tools/paging.cjs.map +1 -0
- package/dist/tools/paging.js +26 -0
- package/dist/tools/paging.js.map +1 -0
- package/dist/tools/params-merge.cjs +106 -0
- package/dist/tools/params-merge.cjs.map +1 -0
- package/dist/tools/params-merge.js +106 -0
- package/dist/tools/params-merge.js.map +1 -0
- package/dist/tools/project-template.cjs +75 -0
- package/dist/tools/project-template.cjs.map +1 -0
- package/dist/tools/project-template.js +72 -0
- package/dist/tools/project-template.js.map +1 -0
- package/dist/tools/projects.cjs +68 -38
- package/dist/tools/projects.cjs.map +1 -1
- package/dist/tools/projects.js +69 -40
- package/dist/tools/projects.js.map +1 -1
- package/dist/tools/readiness.cjs +104 -0
- package/dist/tools/readiness.cjs.map +1 -0
- package/dist/tools/readiness.js +99 -0
- package/dist/tools/readiness.js.map +1 -0
- package/dist/tools/result-pool.cjs +260 -0
- package/dist/tools/result-pool.cjs.map +1 -0
- package/dist/tools/result-pool.js +252 -0
- package/dist/tools/result-pool.js.map +1 -0
- package/dist/tools/sandbox.cjs +112 -14
- package/dist/tools/sandbox.cjs.map +1 -1
- package/dist/tools/sandbox.js +110 -15
- package/dist/tools/sandbox.js.map +1 -1
- package/dist/tools/sdk-types.cjs +24 -0
- package/dist/tools/sdk-types.cjs.map +1 -0
- package/dist/tools/sdk-types.js +24 -0
- package/dist/tools/sdk-types.js.map +1 -0
- package/dist/tools/settle.cjs +155 -0
- package/dist/tools/settle.cjs.map +1 -0
- package/dist/tools/settle.js +148 -0
- package/dist/tools/settle.js.map +1 -0
- package/dist/tools/tokens.cjs +49 -8
- package/dist/tools/tokens.cjs.map +1 -1
- package/dist/tools/tokens.js +45 -9
- package/dist/tools/tokens.js.map +1 -1
- package/dist/tools/types.cjs +62 -11
- package/dist/tools/types.cjs.map +1 -1
- package/dist/tools/types.js +60 -12
- package/dist/tools/types.js.map +1 -1
- package/dist/tools/value-outline.cjs +119 -0
- package/dist/tools/value-outline.cjs.map +1 -0
- package/dist/tools/value-outline.js +117 -0
- package/dist/tools/value-outline.js.map +1 -0
- package/package.json +9 -8
- package/src/agent-tables.test.ts +246 -0
- package/src/agent-tables.ts +145 -0
- package/src/instructions.ts +16 -12
- package/src/project-holds.test.ts +362 -0
- package/src/project-holds.ts +207 -0
- package/src/project-list.test.ts +38 -0
- package/src/project-list.ts +25 -0
- package/src/server-log.test.ts +71 -6
- package/src/server-log.ts +58 -27
- package/src/server.logging.test.ts +46 -51
- package/src/server.test.ts +166 -22
- package/src/server.ts +97 -37
- package/src/tools/agent-tables.test.ts +650 -0
- package/src/tools/agent-tables.ts +673 -0
- package/src/tools/await.test.ts +287 -0
- package/src/tools/await.ts +393 -122
- package/src/tools/block-action-context.ts +17 -13
- package/src/tools/block-api.ts +19 -4
- package/src/tools/block-params.test.ts +828 -0
- package/src/tools/block-params.ts +626 -10
- package/src/tools/block-state.test.ts +480 -0
- package/src/tools/block-state.ts +678 -83
- package/src/tools/block-storage.test.ts +270 -0
- package/src/tools/block-storage.ts +195 -0
- package/src/tools/block-updates.test.ts +605 -0
- package/src/tools/block-updates.ts +751 -0
- package/src/tools/blocks.test.ts +169 -22
- package/src/tools/blocks.ts +354 -113
- package/src/tools/data-query.test.ts +641 -0
- package/src/tools/data-query.ts +839 -145
- package/src/tools/errors.test.ts +33 -0
- package/src/tools/errors.ts +23 -0
- package/src/tools/files.test.ts +842 -0
- package/src/tools/files.ts +990 -0
- package/src/tools/ids.test.ts +87 -0
- package/src/tools/ids.ts +89 -1
- package/src/tools/imports.ts +304 -0
- package/src/tools/logs.test.ts +70 -0
- package/src/tools/logs.ts +154 -44
- package/src/tools/paging.test.ts +39 -0
- package/src/tools/paging.ts +39 -0
- package/src/tools/params-merge.test.ts +274 -0
- package/src/tools/params-merge.ts +149 -0
- package/src/tools/project-template.test.ts +230 -0
- package/src/tools/project-template.ts +135 -0
- package/src/tools/projects.test.ts +122 -0
- package/src/tools/projects.ts +136 -48
- package/src/tools/readiness.test.ts +195 -0
- package/src/tools/readiness.ts +189 -0
- package/src/tools/result-pool.test.ts +377 -0
- package/src/tools/result-pool.ts +483 -0
- package/src/tools/sandbox.test.ts +128 -10
- package/src/tools/sandbox.ts +132 -18
- package/src/tools/sdk-types.ts +54 -0
- package/src/tools/settle.test.ts +340 -0
- package/src/tools/settle.ts +312 -0
- package/src/tools/tokens.ts +74 -9
- package/src/tools/types.ts +84 -12
- package/src/tools/value-outline.ts +143 -0
- package/dist/tools/screenshot.cjs +0 -44
- package/dist/tools/screenshot.cjs.map +0 -1
- package/dist/tools/screenshot.js +0 -44
- package/dist/tools/screenshot.js.map +0 -1
- package/dist/tools/ui-interaction.cjs +0 -140
- package/dist/tools/ui-interaction.cjs.map +0 -1
- package/dist/tools/ui-interaction.js +0 -140
- package/dist/tools/ui-interaction.js.map +0 -1
- package/src/tools/screenshot.ts +0 -61
- package/src/tools/ui-interaction.ts +0 -184
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
import { describe, expect, test } from "vitest";
|
|
2
|
+
import {
|
|
3
|
+
blockView,
|
|
4
|
+
diffValues,
|
|
5
|
+
missingTopLevelKeys,
|
|
6
|
+
pluginOf,
|
|
7
|
+
readStorageView,
|
|
8
|
+
} from "./block-storage";
|
|
9
|
+
import { asPluginHandle } from "./ids";
|
|
10
|
+
import { outlineValue, stripBytes } from "./value-outline";
|
|
11
|
+
|
|
12
|
+
/** Raw block storage of a BlockModelV3 block, in the format the SDK writes. */
|
|
13
|
+
function storage(data: unknown, plugins: Record<string, { name: string; data: unknown }> = {}) {
|
|
14
|
+
return {
|
|
15
|
+
__pl_a7f3e2b9__: "v1",
|
|
16
|
+
__dataVersion: "v3",
|
|
17
|
+
__data: data,
|
|
18
|
+
__pluginRegistry: Object.fromEntries(Object.entries(plugins).map(([h, p]) => [h, p.name])),
|
|
19
|
+
__plugins: Object.fromEntries(
|
|
20
|
+
Object.entries(plugins).map(([h, p]) => [h, { __dataVersion: "v1", __data: p.data }]),
|
|
21
|
+
),
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
describe("readStorageView", () => {
|
|
26
|
+
test("reads data, version and registered plugins", () => {
|
|
27
|
+
const view = readStorageView(
|
|
28
|
+
storage({ a: 1 }, { counter: { name: "counterPlugin", data: { count: 10 } } }),
|
|
29
|
+
);
|
|
30
|
+
expect(view).toEqual({
|
|
31
|
+
dataVersion: "v3",
|
|
32
|
+
data: { a: 1 },
|
|
33
|
+
plugins: { counter: { name: "counterPlugin", dataVersion: "v1", data: { count: 10 } } },
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
test("a numeric data version of early storage is kept as it is", () => {
|
|
38
|
+
const view = readStorageView({
|
|
39
|
+
__pl_a7f3e2b9__: "v1",
|
|
40
|
+
__dataVersion: 1,
|
|
41
|
+
__data: { a: 1 },
|
|
42
|
+
__pluginRegistry: { counter: "counterPlugin" },
|
|
43
|
+
__plugins: { counter: { __dataVersion: 2, __data: { count: 1 } } },
|
|
44
|
+
});
|
|
45
|
+
expect(view.dataVersion).toBe(1);
|
|
46
|
+
expect(view.data).toEqual({ a: 1 });
|
|
47
|
+
expect(view.plugins[asPluginHandle("counter")]).toEqual({
|
|
48
|
+
name: "counterPlugin",
|
|
49
|
+
dataVersion: 2,
|
|
50
|
+
data: { count: 1 },
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
test("storage without plugin fields has no plugins", () => {
|
|
55
|
+
expect(
|
|
56
|
+
readStorageView({ __pl_a7f3e2b9__: "v1", __dataVersion: "v1", __data: 5 }).plugins,
|
|
57
|
+
).toEqual({});
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
test("a plugin handle that is a prototype key is a plugin like any other", () => {
|
|
61
|
+
const raw = JSON.parse(
|
|
62
|
+
'{"__pl_a7f3e2b9__":"v1","__dataVersion":"v3","__data":{},' +
|
|
63
|
+
'"__pluginRegistry":{"__proto__":"protoPlugin","toString":"strPlugin"},' +
|
|
64
|
+
'"__plugins":{"__proto__":{"__dataVersion":"v1","__data":{"n":1}}}}',
|
|
65
|
+
);
|
|
66
|
+
const view = readStorageView(raw);
|
|
67
|
+
expect(Object.keys(view.plugins)).toEqual(["__proto__", "toString"]);
|
|
68
|
+
expect(pluginOf(view, asPluginHandle("__proto__"))).toEqual({
|
|
69
|
+
name: "protoPlugin",
|
|
70
|
+
dataVersion: "v1",
|
|
71
|
+
data: { n: 1 },
|
|
72
|
+
});
|
|
73
|
+
// `__plugins` has no `toString` entry of its own: its data is not the inherited function.
|
|
74
|
+
expect(pluginOf(view, asPluginHandle("toString"))).toEqual({
|
|
75
|
+
name: "strPlugin",
|
|
76
|
+
dataVersion: undefined,
|
|
77
|
+
data: undefined,
|
|
78
|
+
});
|
|
79
|
+
expect(pluginOf(view, asPluginHandle("constructor"))).toBeUndefined();
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
test("storage in another format is the data itself", () => {
|
|
83
|
+
expect(readStorageView({ x: 1 })).toEqual({
|
|
84
|
+
dataVersion: undefined,
|
|
85
|
+
data: { x: 1 },
|
|
86
|
+
plugins: {},
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
describe("blockView", () => {
|
|
92
|
+
test("a BlockModelV3 block shows data, plugins and an output summary", () => {
|
|
93
|
+
const view = blockView({
|
|
94
|
+
blockStorage: storage({ a: 1 }, { counter: { name: "counterPlugin", data: { count: 1 } } }),
|
|
95
|
+
outputs: { x: { ok: true, value: 5, stable: true } },
|
|
96
|
+
});
|
|
97
|
+
expect(view).toEqual({
|
|
98
|
+
dataVersion: "v3",
|
|
99
|
+
data: { a: 1 },
|
|
100
|
+
plugins: { counter: { name: "counterPlugin", dataVersion: "v1", data: { count: 1 } } },
|
|
101
|
+
outputs: [{ key: "x", status: "ok", hasValue: true, tokensEstimate: 1 }],
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
test("failed outputs carry no error text; one note counts them and says where to read one", () => {
|
|
106
|
+
const failed = { ok: false, errors: [{ message: "run failed\ncaused by: out of memory" }] };
|
|
107
|
+
const view = blockView({
|
|
108
|
+
blockStorage: storage({ a: 1 }),
|
|
109
|
+
outputs: { x: { ok: true, value: 5, stable: true }, main: failed, table: failed },
|
|
110
|
+
});
|
|
111
|
+
expect(view.outputs).toEqual([
|
|
112
|
+
{ key: "x", status: "ok", hasValue: true, tokensEstimate: 1 },
|
|
113
|
+
{ key: "main", status: "error", hasValue: false },
|
|
114
|
+
{ key: "table", status: "error", hasValue: false },
|
|
115
|
+
]);
|
|
116
|
+
expect(JSON.stringify(view)).not.toContain("out of memory");
|
|
117
|
+
expect(view.outputsNote).toBe(
|
|
118
|
+
'2 outputs failed; get_block_outputs with `keys: ["main"]` gives its error.',
|
|
119
|
+
);
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
test("no output failed: no note", () => {
|
|
123
|
+
const view = blockView({
|
|
124
|
+
blockStorage: storage({}),
|
|
125
|
+
outputs: { x: { ok: true, value: 5, stable: false } },
|
|
126
|
+
});
|
|
127
|
+
expect(view).not.toHaveProperty("outputsNote");
|
|
128
|
+
});
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
describe("missingTopLevelKeys", () => {
|
|
132
|
+
test("lists the dropped top-level keys", () => {
|
|
133
|
+
expect(missingTopLevelKeys({ a: 1, b: 2, c: 3 }, { a: 1 })).toEqual(["b", "c"]);
|
|
134
|
+
expect(missingTopLevelKeys({ a: 1 }, { a: 2, d: 4 })).toEqual([]);
|
|
135
|
+
expect(missingTopLevelKeys(undefined, { a: 1 })).toEqual([]);
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
test("a dropped key named like a prototype key is still dropped", () => {
|
|
139
|
+
expect(missingTopLevelKeys({ toString: 1, constructor: 2, a: 3 }, { a: 3 })).toEqual([
|
|
140
|
+
"toString",
|
|
141
|
+
"constructor",
|
|
142
|
+
]);
|
|
143
|
+
});
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
describe("diffValues", () => {
|
|
147
|
+
test("reports added, removed and replaced paths with small values", () => {
|
|
148
|
+
expect(
|
|
149
|
+
diffValues(
|
|
150
|
+
{ numbers: [1, 2], label: "x", gone: true, "odd key": 1 },
|
|
151
|
+
{ numbers: [1, 3, 4], label: "x", fresh: { y: 1 }, "odd key": 2 },
|
|
152
|
+
),
|
|
153
|
+
).toEqual({
|
|
154
|
+
changes: [
|
|
155
|
+
{ path: "numbers[1]", op: "replace", from: 2, to: 3 },
|
|
156
|
+
{ path: "numbers[2]", op: "add", to: 4 },
|
|
157
|
+
{ path: "gone", op: "remove", from: true },
|
|
158
|
+
{ path: '["odd key"]', op: "replace", from: 1, to: 2 },
|
|
159
|
+
{ path: "fresh", op: "add", to: { y: 1 } },
|
|
160
|
+
],
|
|
161
|
+
});
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
test("a large value is given by its size", () => {
|
|
165
|
+
const long = "x".repeat(500);
|
|
166
|
+
expect(diffValues({ a: "s" }, { a: long }).changes).toEqual([
|
|
167
|
+
{ path: "a", op: "replace", from: "s", to: { chars: 502 } },
|
|
168
|
+
]);
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
test("stops at 50 paths and counts the rest", () => {
|
|
172
|
+
const before = { list: Array.from({ length: 80 }, () => 0) };
|
|
173
|
+
const after = { list: Array.from({ length: 80 }, () => 1) };
|
|
174
|
+
const diff = diffValues(before, after);
|
|
175
|
+
expect(diff.changes).toHaveLength(50);
|
|
176
|
+
expect(diff.moreChanges).toBe(30);
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
test("a key named like a prototype key is compared as its own value only", () => {
|
|
180
|
+
expect(diffValues({ a: 1 }, { a: 1, constructor: 1 })).toEqual({
|
|
181
|
+
changes: [{ path: "constructor", op: "add", to: 1 }],
|
|
182
|
+
});
|
|
183
|
+
expect(diffValues({ toString: "x" }, {})).toEqual({
|
|
184
|
+
changes: [{ path: "toString", op: "remove", from: "x" }],
|
|
185
|
+
});
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
test("a whole replaced value is the root path", () => {
|
|
189
|
+
expect(diffValues(1, 2)).toEqual({
|
|
190
|
+
changes: [{ path: "(root)", op: "replace", from: 1, to: 2 }],
|
|
191
|
+
});
|
|
192
|
+
});
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
describe("stripBytes", () => {
|
|
196
|
+
test("replaces typed arrays, Buffers and ArrayBuffers with their size", () => {
|
|
197
|
+
const value = {
|
|
198
|
+
logo: { type: "explicit-bytes", mimeType: "image/png", content: new Uint8Array(24439) },
|
|
199
|
+
buf: Buffer.from("hello"),
|
|
200
|
+
ab: new ArrayBuffer(8),
|
|
201
|
+
kept: [1, 2, 3],
|
|
202
|
+
};
|
|
203
|
+
expect(stripBytes(value)).toEqual({
|
|
204
|
+
logo: { type: "explicit-bytes", mimeType: "image/png", content: { bytes: 24439 } },
|
|
205
|
+
buf: { bytes: 5 },
|
|
206
|
+
ab: { bytes: 8 },
|
|
207
|
+
kept: [1, 2, 3],
|
|
208
|
+
});
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
test("leaves plain data alone, the JSON forms of bytes too", () => {
|
|
212
|
+
const bytes = new Uint8Array(32).fill(7);
|
|
213
|
+
const value: unknown = JSON.parse(JSON.stringify({ a: bytes, numbers: [10, 20, 30] }));
|
|
214
|
+
expect(stripBytes(value)).toBe(value);
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
test("keeps `__proto__` and `constructor` keys in the copy", () => {
|
|
218
|
+
const value: Record<string, unknown> = JSON.parse('{"__proto__": {"a": 1}, "constructor": 2}');
|
|
219
|
+
const inner = Object.getOwnPropertyDescriptor(value, "__proto__")?.value as Record<
|
|
220
|
+
string,
|
|
221
|
+
unknown
|
|
222
|
+
>;
|
|
223
|
+
inner.blob = new Uint8Array(2);
|
|
224
|
+
const stripped = stripBytes(value);
|
|
225
|
+
expect(stripped).not.toBe(value);
|
|
226
|
+
expect(Object.keys(stripped as object)).toEqual(["__proto__", "constructor"]);
|
|
227
|
+
expect(Object.getPrototypeOf(stripped)).toBe(Object.prototype);
|
|
228
|
+
expect(JSON.stringify(stripped)).toBe(
|
|
229
|
+
'{"__proto__":{"a":1,"blob":{"bytes":2}},"constructor":2}',
|
|
230
|
+
);
|
|
231
|
+
});
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
describe("outlineValue", () => {
|
|
235
|
+
test("gives types, sizes and keys down to the depth", () => {
|
|
236
|
+
expect(outlineValue({ a: "xy", b: [1, 2], c: { d: { e: 1 } } }, { maxDepth: 2 })).toEqual({
|
|
237
|
+
type: "object",
|
|
238
|
+
chars: 38,
|
|
239
|
+
keys: 3,
|
|
240
|
+
fields: {
|
|
241
|
+
a: { type: "string", chars: 4 },
|
|
242
|
+
b: { type: "array", chars: 5, length: 2, first: { type: "number", chars: 1 } },
|
|
243
|
+
c: {
|
|
244
|
+
type: "object",
|
|
245
|
+
chars: 13,
|
|
246
|
+
keys: 1,
|
|
247
|
+
fields: { d: { type: "object", chars: 7, keys: 1 } },
|
|
248
|
+
},
|
|
249
|
+
},
|
|
250
|
+
});
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
test("bounds the number of keys shown", () => {
|
|
254
|
+
const wide = Object.fromEntries(Array.from({ length: 100 }, (_, i) => [`k${i}`, i]));
|
|
255
|
+
const outline = outlineValue(wide, { maxKeys: 10 });
|
|
256
|
+
expect(outline).toMatchObject({ type: "object", keys: 100, moreKeys: 90 });
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
test("keeps `__proto__` and `constructor` keys among the fields", () => {
|
|
260
|
+
const outline = outlineValue(JSON.parse('{"__proto__": {"a": 1}, "constructor": 2}'));
|
|
261
|
+
expect(outline.type).toBe("object");
|
|
262
|
+
const fields = outline.type === "object" ? outline.fields : undefined;
|
|
263
|
+
expect(Object.keys(fields ?? {})).toEqual(["__proto__", "constructor"]);
|
|
264
|
+
expect(Object.getPrototypeOf(fields)).toBe(Object.prototype);
|
|
265
|
+
expect(JSON.stringify(fields)).toBe(
|
|
266
|
+
'{"__proto__":{"type":"object","chars":7,"keys":1,"fields":{"a":{"type":"number","chars":1}}},' +
|
|
267
|
+
'"constructor":{"type":"number","chars":1}}',
|
|
268
|
+
);
|
|
269
|
+
});
|
|
270
|
+
});
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
import { isDeepStrictEqual } from "node:util";
|
|
2
|
+
import type { PluginHandle, PluginName } from "@platforma-sdk/model";
|
|
3
|
+
import { isBlockStorage, normalizeBlockStorage } from "@platforma-sdk/model";
|
|
4
|
+
import { isPlainObject } from "@milaboratories/helpers";
|
|
5
|
+
import { importsOf, type ImportsSummary } from "./imports";
|
|
6
|
+
import { outputsView, type OutputsView } from "./tokens";
|
|
7
|
+
import { jsonChars } from "./value-outline";
|
|
8
|
+
import { asPluginHandle, asPluginName } from "./ids";
|
|
9
|
+
|
|
10
|
+
/** The most changed paths a write reports. */
|
|
11
|
+
export const MAX_CHANGES = 50;
|
|
12
|
+
|
|
13
|
+
/** The most characters of a value a reported change carries; a larger one is given by its size. */
|
|
14
|
+
const CHANGE_VALUE_CHARS = 200;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* The version of a block's or a plugin's data model, as stored: a string; a number in storage
|
|
18
|
+
* written by early SDK releases, shown as it is (the SDK's `normalizeBlockStorage` replaces it
|
|
19
|
+
* with its legacy marker, which says less).
|
|
20
|
+
*/
|
|
21
|
+
export type DataVersion = string | number | undefined;
|
|
22
|
+
|
|
23
|
+
/** The stored `__dataVersion` of `entry`. */
|
|
24
|
+
function dataVersionOf(entry: unknown): DataVersion {
|
|
25
|
+
if (!isPlainObject(entry)) return undefined;
|
|
26
|
+
const version = entry.__dataVersion;
|
|
27
|
+
return typeof version === "string" || typeof version === "number" ? version : undefined;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** The data of one plugin of a block. */
|
|
31
|
+
export type PluginStorage = {
|
|
32
|
+
/** The plugin type (its globally unique name). */
|
|
33
|
+
name: PluginName;
|
|
34
|
+
dataVersion: DataVersion;
|
|
35
|
+
data: unknown;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
/** What a block keeps in its storage, read without running its model. */
|
|
39
|
+
export type StorageView = {
|
|
40
|
+
dataVersion: DataVersion;
|
|
41
|
+
data: unknown;
|
|
42
|
+
/** Plugin handle → its data; plugins the block registers, nothing else. */
|
|
43
|
+
plugins: Record<PluginHandle, PluginStorage>;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Reads a block's raw storage (`blockStorage` of the block state) through the SDK's
|
|
48
|
+
* `normalizeBlockStorage`: the data, and the data of every plugin the block registers. Storage in
|
|
49
|
+
* another format (a block that has not written any yet) is read as the data itself, as the SDK
|
|
50
|
+
* does, with no version. Data versions are read as stored ({@link DataVersion}).
|
|
51
|
+
*/
|
|
52
|
+
export function readStorageView(rawStorage: unknown): StorageView {
|
|
53
|
+
if (!isBlockStorage(rawStorage)) return { dataVersion: undefined, data: rawStorage, plugins: {} };
|
|
54
|
+
const storage = normalizeBlockStorage(rawStorage);
|
|
55
|
+
const stored = new Map<string, unknown>(Object.entries(storage.__plugins ?? {}));
|
|
56
|
+
// `Object.fromEntries` defines own keys, so a handle such as `__proto__` stays a key.
|
|
57
|
+
const plugins: Record<PluginHandle, PluginStorage> = Object.fromEntries(
|
|
58
|
+
Object.entries(storage.__pluginRegistry ?? {}).map(([handle, name]) => {
|
|
59
|
+
const entry = stored.get(handle);
|
|
60
|
+
const plugin: PluginStorage = {
|
|
61
|
+
name: asPluginName(String(name)),
|
|
62
|
+
dataVersion: dataVersionOf(entry),
|
|
63
|
+
data: isPlainObject(entry) ? entry.__data : undefined,
|
|
64
|
+
};
|
|
65
|
+
return [asPluginHandle(handle), plugin];
|
|
66
|
+
}),
|
|
67
|
+
);
|
|
68
|
+
return { dataVersion: dataVersionOf(rawStorage), data: storage.__data, plugins };
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* `obj[key]` when `key` is a key of `obj` itself; `undefined` for a key it only inherits
|
|
73
|
+
* (`toString`, `constructor`, `__proto__`), so a name an agent gives never finds one of those.
|
|
74
|
+
*/
|
|
75
|
+
export function ownValue(obj: Record<string, unknown>, key: string): unknown {
|
|
76
|
+
return Object.hasOwn(obj, key) ? obj[key] : undefined;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** The plugin `handle` of a block's storage; `undefined` when the block registers no such plugin. */
|
|
80
|
+
export function pluginOf(storage: StorageView, handle: PluginHandle): PluginStorage | undefined {
|
|
81
|
+
return Object.hasOwn(storage.plugins, handle) ? storage.plugins[handle] : undefined;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** One block as every block tool shows it. */
|
|
85
|
+
export type BlockView = {
|
|
86
|
+
dataVersion: DataVersion;
|
|
87
|
+
data: unknown;
|
|
88
|
+
plugins?: Record<string, { name: string; dataVersion: DataVersion; data: unknown }>;
|
|
89
|
+
/** The files the block imports, when its outputs carry import progress. */
|
|
90
|
+
imports?: ImportsSummary;
|
|
91
|
+
} & OutputsView;
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* The shared block view: data, plugin data, the output summary and, when the outputs carry import
|
|
95
|
+
* progress, the imports summary. Built from stored fields; nothing is run.
|
|
96
|
+
*
|
|
97
|
+
* @param state the block state: its raw storage and outputs
|
|
98
|
+
*/
|
|
99
|
+
export function blockView(state: { blockStorage: unknown; outputs?: unknown }): BlockView {
|
|
100
|
+
const rawOutputs = isPlainObject(state.outputs) ? state.outputs : undefined;
|
|
101
|
+
const outputs = outputsView(rawOutputs);
|
|
102
|
+
const imports = importsOf(rawOutputs);
|
|
103
|
+
const storage = readStorageView(state.blockStorage);
|
|
104
|
+
const view: BlockView = {
|
|
105
|
+
dataVersion: storage.dataVersion,
|
|
106
|
+
data: storage.data,
|
|
107
|
+
...outputs,
|
|
108
|
+
...(imports !== undefined ? { imports } : {}),
|
|
109
|
+
};
|
|
110
|
+
const plugins = Object.entries(storage.plugins);
|
|
111
|
+
if (plugins.length > 0)
|
|
112
|
+
view.plugins = Object.fromEntries(
|
|
113
|
+
plugins.map(([handle, p]) => [
|
|
114
|
+
handle,
|
|
115
|
+
{ name: p.name, dataVersion: p.dataVersion, data: p.data },
|
|
116
|
+
]),
|
|
117
|
+
);
|
|
118
|
+
return view;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** The top-level keys of `before` that `after` does not have. Both are read as JSON objects. */
|
|
122
|
+
export function missingTopLevelKeys(before: unknown, after: unknown): string[] {
|
|
123
|
+
if (!isPlainObject(before)) return [];
|
|
124
|
+
const kept = isPlainObject(after) ? after : {};
|
|
125
|
+
return Object.keys(before).filter(
|
|
126
|
+
(k) => before[k] !== undefined && ownValue(kept, k) === undefined,
|
|
127
|
+
);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** One changed path of a write. */
|
|
131
|
+
export type ValueChange = {
|
|
132
|
+
/**
|
|
133
|
+
* `a.b[2].c`; keys that are not identifiers in brackets: `a["key with space"]`; `(root)` for the
|
|
134
|
+
* value as a whole.
|
|
135
|
+
*/
|
|
136
|
+
path: string;
|
|
137
|
+
op: "add" | "remove" | "replace";
|
|
138
|
+
/** The old value, when it is small; otherwise its size as `{chars: N}`. */
|
|
139
|
+
from?: unknown;
|
|
140
|
+
/** The new value, when it is small; otherwise its size as `{chars: N}`. */
|
|
141
|
+
to?: unknown;
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
/** The changes between two values, at most `limit` of them. */
|
|
145
|
+
export type ValueDiff = {
|
|
146
|
+
changes: ValueChange[];
|
|
147
|
+
/** How many more changed paths there are past `changes`. */
|
|
148
|
+
moreChanges?: number;
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
const IDENTIFIER = /^[A-Za-z_$][A-Za-z0-9_$]*$/;
|
|
152
|
+
|
|
153
|
+
/** The path of `key` under `path`, in the form of {@link ValueChange.path}. */
|
|
154
|
+
export function childPath(path: string, key: string): string {
|
|
155
|
+
if (IDENTIFIER.test(key)) return path === "" ? key : `${path}.${key}`;
|
|
156
|
+
return `${path}[${JSON.stringify(key)}]`;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function small(value: unknown): unknown {
|
|
160
|
+
const chars = jsonChars(value);
|
|
161
|
+
return chars <= CHANGE_VALUE_CHARS ? value : { chars };
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* The paths where `after` differs from `before`, down to the leaves: objects key by key, arrays
|
|
166
|
+
* index by index. Reports at most `limit` paths and counts the rest.
|
|
167
|
+
*/
|
|
168
|
+
export function diffValues(before: unknown, after: unknown, limit = MAX_CHANGES): ValueDiff {
|
|
169
|
+
const changes: ValueChange[] = [];
|
|
170
|
+
let more = 0;
|
|
171
|
+
const push = (change: ValueChange) => {
|
|
172
|
+
if (changes.length < limit) changes.push({ ...change, path: change.path || "(root)" });
|
|
173
|
+
else more++;
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
function walk(path: string, a: unknown, b: unknown): void {
|
|
177
|
+
if (isDeepStrictEqual(a, b)) return;
|
|
178
|
+
if (a === undefined) return push({ path, op: "add", to: small(b) });
|
|
179
|
+
if (b === undefined) return push({ path, op: "remove", from: small(a) });
|
|
180
|
+
if (Array.isArray(a) && Array.isArray(b)) {
|
|
181
|
+
for (let i = 0; i < Math.max(a.length, b.length); i++)
|
|
182
|
+
walk(`${path}[${i}]`, i < a.length ? a[i] : undefined, i < b.length ? b[i] : undefined);
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
if (isPlainObject(a) && isPlainObject(b)) {
|
|
186
|
+
const keys = new Set([...Object.keys(a), ...Object.keys(b)]);
|
|
187
|
+
for (const key of keys) walk(childPath(path, key), ownValue(a, key), ownValue(b, key));
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
push({ path, op: "replace", from: small(a), to: small(b) });
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
walk("", before, after);
|
|
194
|
+
return more > 0 ? { changes, moreChanges: more } : { changes };
|
|
195
|
+
}
|