@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
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
import { asBlockId, type BlockId } from "@milaboratories/pl-model-common";
|
|
2
|
+
import { clamp } from "@milaboratories/helpers";
|
|
3
|
+
import { delay } from "es-toolkit";
|
|
4
|
+
|
|
5
|
+
/** What {@link awaitBlockSettled} reads of a block state. */
|
|
6
|
+
export type SettleBlockState = {
|
|
7
|
+
outputs?: Record<string, unknown> | undefined;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
/** What {@link awaitBlockSettled} reads of a block of the project overview. */
|
|
11
|
+
export type SettleOverviewBlock = {
|
|
12
|
+
id: string;
|
|
13
|
+
calculationStatus: string;
|
|
14
|
+
upstreams: string[];
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
/** The block state computable as {@link awaitBlockSettled} uses it; a middle-layer `Computable`. */
|
|
18
|
+
export type SettleStateComputable<S extends SettleBlockState> = {
|
|
19
|
+
getFullValue(): Promise<{ value: S; uTag: string; unstableMarker?: string | undefined }>;
|
|
20
|
+
awaitChange(abortSignal?: AbortSignal, uTag?: string): Promise<void>;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
/** What {@link awaitBlockSettled} needs of a project; a `Project` of the middle layer has it. */
|
|
24
|
+
export type SettleProject<S extends SettleBlockState> = {
|
|
25
|
+
overview: { getValue(): Promise<{ blocks: SettleOverviewBlock[] } | undefined> };
|
|
26
|
+
getBlockState(blockId: BlockId): SettleStateComputable<S>;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
/** Options of {@link awaitBlockSettled}. */
|
|
30
|
+
export type SettleOptions = {
|
|
31
|
+
/** The longest the wait takes. */
|
|
32
|
+
timeoutMs: number;
|
|
33
|
+
/** Ends the wait early: the request was cancelled. */
|
|
34
|
+
signal?: AbortSignal;
|
|
35
|
+
/**
|
|
36
|
+
* Wait for the block's run as well: a running block is not settled, and a block that is not
|
|
37
|
+
* calculated or is in limbo ends the wait (`stoppedBy`).
|
|
38
|
+
*/
|
|
39
|
+
waitForRun: boolean;
|
|
40
|
+
/**
|
|
41
|
+
* Called on every look at the block, for progress notifications: at least every
|
|
42
|
+
* {@link WAKE_INTERVAL_MS}, also while the block does not change.
|
|
43
|
+
*/
|
|
44
|
+
onProgress?: (elapsedMs: number, timeoutMs: number) => void;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
/** How {@link awaitBlockSettled} ended. */
|
|
48
|
+
export type SettleResult<S extends SettleBlockState> = {
|
|
49
|
+
/** Every output of the block is computed or failed (and with `waitForRun`, the run is over). */
|
|
50
|
+
settled: boolean;
|
|
51
|
+
/**
|
|
52
|
+
* Why the wait ended unsettled: out of time, the request was cancelled, the outputs wait for an
|
|
53
|
+
* upstream block that is running or that has not run (not calculated, or in limbo), named by
|
|
54
|
+
* `waitingOn`, or (with `waitForRun`) the block is not calculated or is in limbo. `block-gone`:
|
|
55
|
+
* the block was removed while waiting.
|
|
56
|
+
*/
|
|
57
|
+
stoppedBy?:
|
|
58
|
+
| "timeout"
|
|
59
|
+
| "aborted"
|
|
60
|
+
| "upstream-running"
|
|
61
|
+
| "upstream-not-run"
|
|
62
|
+
| "not-calculated"
|
|
63
|
+
| "limbo"
|
|
64
|
+
| "block-gone";
|
|
65
|
+
waitedMs: number;
|
|
66
|
+
/** The block's calculation status when the wait ended. */
|
|
67
|
+
calculationStatus: string | undefined;
|
|
68
|
+
/** The last block state read. */
|
|
69
|
+
state: S;
|
|
70
|
+
/** The outputs that have no final value yet. */
|
|
71
|
+
pendingOutputs: string[];
|
|
72
|
+
/**
|
|
73
|
+
* The upstream block the pending outputs wait for, when the block state names it and that
|
|
74
|
+
* block is running or has not run (see `waitingOnStatus`).
|
|
75
|
+
*/
|
|
76
|
+
waitingOn?: BlockId;
|
|
77
|
+
/** The calculation status of the `waitingOn` block. */
|
|
78
|
+
waitingOnStatus?: string;
|
|
79
|
+
/** The blocks above that are running. */
|
|
80
|
+
runningUpstreams: BlockId[];
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
/** The shortest time one look at the block takes, so a computable that changes all the time does not spin. */
|
|
84
|
+
const MIN_ITERATION_MS = 100;
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* The longest one look at the block waits for a change before looking again: a wait on a block
|
|
88
|
+
* that does not change still wakes on schedule, so its progress notifications go out.
|
|
89
|
+
*/
|
|
90
|
+
export const WAKE_INTERVAL_MS = 5000;
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Waits until every output of a block is either computed (stable) or failed. Only the outputs of
|
|
94
|
+
* this block count: the block state's own `stable` flag also waits for the titles of every other
|
|
95
|
+
* block of the project.
|
|
96
|
+
*
|
|
97
|
+
* Returns early, unsettled, when the pending outputs wait for an upstream block that is running
|
|
98
|
+
* or has not run: the block state is marked `prod_not_locked:<id>` or `no_data:<id>:<export>` and
|
|
99
|
+
* block `<id>` is running (the outputs settle only after it finishes, which may take longer than
|
|
100
|
+
* any wait here), or is not calculated or in limbo (they settle only after somebody runs it).
|
|
101
|
+
* (With `waitForRun` and the block itself running, the wait goes on: the run includes the
|
|
102
|
+
* upstream.)
|
|
103
|
+
*/
|
|
104
|
+
export async function awaitBlockSettled<S extends SettleBlockState>(
|
|
105
|
+
project: SettleProject<S>,
|
|
106
|
+
blockId: BlockId,
|
|
107
|
+
options: SettleOptions,
|
|
108
|
+
): Promise<SettleResult<S>> {
|
|
109
|
+
const start = Date.now();
|
|
110
|
+
return await withDeadline(options.timeoutMs, options.signal, (signal) =>
|
|
111
|
+
settleLoop(project, blockId, options, start, signal),
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Runs `wait` with a signal that fires when `signal` (the request's) does or `timeoutMs` runs
|
|
117
|
+
* out, whichever comes first.
|
|
118
|
+
*/
|
|
119
|
+
export async function withDeadline<T>(
|
|
120
|
+
timeoutMs: number,
|
|
121
|
+
signal: AbortSignal | undefined,
|
|
122
|
+
wait: (signal: AbortSignal) => Promise<T>,
|
|
123
|
+
): Promise<T> {
|
|
124
|
+
const deadline = new AbortController();
|
|
125
|
+
const deadlineTimer = setTimeout(() => deadline.abort(), Math.max(0, timeoutMs));
|
|
126
|
+
try {
|
|
127
|
+
return await wait(signal ? AbortSignal.any([signal, deadline.signal]) : deadline.signal);
|
|
128
|
+
} finally {
|
|
129
|
+
clearTimeout(deadlineTimer);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* One wait of a waiting loop for a computable to change from `uTag`: it ends on the change, on
|
|
135
|
+
* `signal`, or after {@link WAKE_INTERVAL_MS} without a change (so the loop looks again and sends
|
|
136
|
+
* its progress), and not before {@link MIN_ITERATION_MS} after `iterationStart` (so a computable
|
|
137
|
+
* that changes all the time does not spin).
|
|
138
|
+
*/
|
|
139
|
+
export async function awaitChangeOrWake(
|
|
140
|
+
computable: { awaitChange(abortSignal?: AbortSignal, uTag?: string): Promise<void> },
|
|
141
|
+
uTag: string,
|
|
142
|
+
signal: AbortSignal,
|
|
143
|
+
iterationStart: number,
|
|
144
|
+
): Promise<void> {
|
|
145
|
+
const wake = new AbortController();
|
|
146
|
+
const wakeTimer = setTimeout(() => wake.abort(), WAKE_INTERVAL_MS);
|
|
147
|
+
try {
|
|
148
|
+
await computable.awaitChange(AbortSignal.any([signal, wake.signal]), uTag);
|
|
149
|
+
} catch {
|
|
150
|
+
// the wait ended or the wake-up came; the next look decides
|
|
151
|
+
} finally {
|
|
152
|
+
clearTimeout(wakeTimer);
|
|
153
|
+
}
|
|
154
|
+
const spent = Date.now() - iterationStart;
|
|
155
|
+
if (spent < MIN_ITERATION_MS && !signal.aborted)
|
|
156
|
+
// An abort ends the pause early (`delay` rejects then); the next look sees the signal.
|
|
157
|
+
await delay(MIN_ITERATION_MS - spent, { signal }).catch(() => {});
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
async function settleLoop<S extends SettleBlockState>(
|
|
161
|
+
project: SettleProject<S>,
|
|
162
|
+
blockId: BlockId,
|
|
163
|
+
options: SettleOptions,
|
|
164
|
+
start: number,
|
|
165
|
+
signal: AbortSignal,
|
|
166
|
+
): Promise<SettleResult<S>> {
|
|
167
|
+
const computable = project.getBlockState(blockId);
|
|
168
|
+
|
|
169
|
+
for (;;) {
|
|
170
|
+
const iterationStart = Date.now();
|
|
171
|
+
const full = await computable.getFullValue();
|
|
172
|
+
const overview = await project.overview.getValue();
|
|
173
|
+
const blocks = overview?.blocks ?? [];
|
|
174
|
+
const block = blocks.find((b) => b.id === blockId);
|
|
175
|
+
const status = block?.calculationStatus;
|
|
176
|
+
const running = new Set(
|
|
177
|
+
blocks.filter((b) => b.calculationStatus === "Running").map((b) => b.id),
|
|
178
|
+
);
|
|
179
|
+
const pendingOutputs = pendingOutputKeys(full.value.outputs);
|
|
180
|
+
const runningUpstreams = (block?.upstreams ?? [])
|
|
181
|
+
.filter((id) => running.has(id))
|
|
182
|
+
.map(asBlockId);
|
|
183
|
+
const markerUpstream = upstreamOfMarker(full.unstableMarker);
|
|
184
|
+
const markerStatus = blocks.find((b) => b.id === markerUpstream)?.calculationStatus;
|
|
185
|
+
const upstreamHalt =
|
|
186
|
+
markerStatus === "Running"
|
|
187
|
+
? "upstream-running"
|
|
188
|
+
: markerStatus === "NotCalculated" || markerStatus === "Limbo"
|
|
189
|
+
? "upstream-not-run"
|
|
190
|
+
: undefined;
|
|
191
|
+
const waitingOn = upstreamHalt === undefined ? undefined : markerUpstream;
|
|
192
|
+
|
|
193
|
+
const result = (
|
|
194
|
+
settled: boolean,
|
|
195
|
+
stoppedBy?: SettleResult<S>["stoppedBy"],
|
|
196
|
+
): SettleResult<S> => ({
|
|
197
|
+
settled,
|
|
198
|
+
...(stoppedBy === undefined ? {} : { stoppedBy }),
|
|
199
|
+
waitedMs: Date.now() - start,
|
|
200
|
+
calculationStatus: status,
|
|
201
|
+
state: full.value,
|
|
202
|
+
pendingOutputs,
|
|
203
|
+
...(waitingOn !== undefined ? { waitingOn, waitingOnStatus: markerStatus } : {}),
|
|
204
|
+
runningUpstreams,
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
if (overview !== undefined && block === undefined) return result(false, "block-gone");
|
|
208
|
+
|
|
209
|
+
if (options.waitForRun && status === "NotCalculated") return result(false, "not-calculated");
|
|
210
|
+
if (options.waitForRun && status === "Limbo") return result(false, "limbo");
|
|
211
|
+
const runPending = options.waitForRun && status === "Running";
|
|
212
|
+
|
|
213
|
+
if (!runPending && full.value.outputs !== undefined && pendingOutputs.length === 0)
|
|
214
|
+
return result(true);
|
|
215
|
+
|
|
216
|
+
if (!runPending && pendingOutputs.length > 0 && upstreamHalt !== undefined)
|
|
217
|
+
return result(false, upstreamHalt);
|
|
218
|
+
|
|
219
|
+
if (signal.aborted) return result(false, options.signal?.aborted ? "aborted" : "timeout");
|
|
220
|
+
options.onProgress?.(Date.now() - start, options.timeoutMs);
|
|
221
|
+
|
|
222
|
+
await awaitChangeOrWake(computable, full.uTag, signal, iterationStart);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/** The keys of outputs that are computed but not final yet (`stable: false`). */
|
|
227
|
+
export function pendingOutputKeys(outputs: Record<string, unknown> | undefined): string[] {
|
|
228
|
+
if (outputs === undefined) return [];
|
|
229
|
+
return Object.entries(outputs)
|
|
230
|
+
.filter(
|
|
231
|
+
([, o]) =>
|
|
232
|
+
typeof o === "object" &&
|
|
233
|
+
o !== null &&
|
|
234
|
+
"ok" in o &&
|
|
235
|
+
o.ok === true &&
|
|
236
|
+
"stable" in o &&
|
|
237
|
+
o.stable === false,
|
|
238
|
+
)
|
|
239
|
+
.map(([key]) => key);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* The upstream block an unstable marker of the result pool names: `prod_not_locked:<id>` (the
|
|
244
|
+
* block's results are not final yet) or `no_data:<id>:<export>` (an export of it has no data yet).
|
|
245
|
+
*/
|
|
246
|
+
export function upstreamOfMarker(marker: string | undefined): BlockId | undefined {
|
|
247
|
+
if (marker === undefined) return undefined;
|
|
248
|
+
const match = /^(?:prod_not_locked:(.+)|no_data:([^:]+):)/.exec(marker);
|
|
249
|
+
const id = match?.[1] ?? match?.[2];
|
|
250
|
+
return id === undefined ? undefined : asBlockId(id);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/** The context of a tool call, as far as a waiting tool uses it. */
|
|
254
|
+
export type WaitContext = {
|
|
255
|
+
mcpReq: {
|
|
256
|
+
signal: AbortSignal;
|
|
257
|
+
_meta?: { progressToken?: string | number; [key: string]: unknown } | undefined;
|
|
258
|
+
notify: (notification: {
|
|
259
|
+
method: "notifications/progress";
|
|
260
|
+
params: {
|
|
261
|
+
progressToken: string | number;
|
|
262
|
+
progress: number;
|
|
263
|
+
total?: number;
|
|
264
|
+
message?: string;
|
|
265
|
+
};
|
|
266
|
+
}) => Promise<void>;
|
|
267
|
+
};
|
|
268
|
+
};
|
|
269
|
+
|
|
270
|
+
/** How often a waiting tool sends a progress notification. */
|
|
271
|
+
const PROGRESS_INTERVAL_MS = 5000;
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* Returns a progress callback (for {@link SettleOptions.onProgress} and the like) that sends a
|
|
275
|
+
* progress notification once the progress grew by at least `step` since the last one sent, when
|
|
276
|
+
* the request asked for progress (carries a `progressToken`); `undefined` otherwise. By default
|
|
277
|
+
* progress counts milliseconds waited, sent at most every {@link PROGRESS_INTERVAL_MS}; a count
|
|
278
|
+
* of items takes a `step` of 1. The progress a client sees only grows. `message` is the default
|
|
279
|
+
* text; a call may give its own (a later phase of the same wait).
|
|
280
|
+
*/
|
|
281
|
+
export function progressReporter(
|
|
282
|
+
ctx: WaitContext | undefined,
|
|
283
|
+
message: string,
|
|
284
|
+
step: number = PROGRESS_INTERVAL_MS,
|
|
285
|
+
): ((progress: number, total: number, phase?: string) => void) | undefined {
|
|
286
|
+
const token = ctx?.mcpReq._meta?.progressToken;
|
|
287
|
+
if (ctx === undefined || token === undefined) return undefined;
|
|
288
|
+
let lastSent = 0;
|
|
289
|
+
return (progress, total, phase) => {
|
|
290
|
+
if (progress - lastSent < step) return;
|
|
291
|
+
lastSent = progress;
|
|
292
|
+
ctx.mcpReq
|
|
293
|
+
.notify({
|
|
294
|
+
method: "notifications/progress",
|
|
295
|
+
params: { progressToken: token, progress, total, message: phase ?? message },
|
|
296
|
+
})
|
|
297
|
+
.catch(() => {
|
|
298
|
+
// the client went away; the wait ends on the abort signal
|
|
299
|
+
});
|
|
300
|
+
};
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/** Clamps a wait the caller asked for to `[min, max]`; `fallback` when none was asked for. */
|
|
304
|
+
export function clampWait(
|
|
305
|
+
requested: number | undefined,
|
|
306
|
+
fallback: number,
|
|
307
|
+
min: number,
|
|
308
|
+
max: number,
|
|
309
|
+
): number {
|
|
310
|
+
if (requested === undefined || !Number.isFinite(requested)) return fallback;
|
|
311
|
+
return clamp(Math.round(requested), min, max);
|
|
312
|
+
}
|
package/src/tools/tokens.ts
CHANGED
|
@@ -70,15 +70,80 @@ export function estimateTokens(value: unknown, nodeLimit = NODE_LIMIT): number |
|
|
|
70
70
|
return overflow ? `>${tokens} (truncated at ${nodeLimit} nodes)` : tokens;
|
|
71
71
|
}
|
|
72
72
|
|
|
73
|
-
/**
|
|
74
|
-
export
|
|
75
|
-
|
|
76
|
-
|
|
73
|
+
/** One block output, summed up without its value or its error. */
|
|
74
|
+
export type OutputSummary = {
|
|
75
|
+
key: string;
|
|
76
|
+
/**
|
|
77
|
+
* `error`: the output failed; `pending`: it has no final value yet (the model marks it not
|
|
78
|
+
* stable); `ok`: it is computed.
|
|
79
|
+
*/
|
|
80
|
+
status: "ok" | "pending" | "error";
|
|
81
|
+
hasValue: boolean;
|
|
82
|
+
tokensEstimate?: number | string;
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
/** Cuts `text` to `max` characters, marking the cut. */
|
|
86
|
+
export function clip(text: string, max: number): string {
|
|
87
|
+
return text.length <= max ? text : `${text.slice(0, max - 1)}…`;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** Cuts `text` to `max` characters in the middle, keeping its start and its end. */
|
|
91
|
+
export function clipMiddle(text: string, max: number): string {
|
|
92
|
+
if (text.length <= max) return text;
|
|
93
|
+
const head = Math.ceil((max - 1) / 2);
|
|
94
|
+
return `${text.slice(0, head)}…${text.slice(text.length - (max - 1 - head))}`;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** The error text of a failed output: the messages of its errors, one per line. */
|
|
98
|
+
export function outputErrorText(output: unknown): string | undefined {
|
|
99
|
+
const errors =
|
|
100
|
+
typeof output === "object" && output !== null && "errors" in output ? output.errors : undefined;
|
|
101
|
+
if (!Array.isArray(errors) || errors.length === 0) return undefined;
|
|
102
|
+
const messages = errors.map((e: unknown) =>
|
|
103
|
+
typeof e === "object" && e !== null && "message" in e && typeof e.message === "string"
|
|
104
|
+
? e.message
|
|
105
|
+
: String(e),
|
|
106
|
+
);
|
|
107
|
+
return messages.join("\n");
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Sums up block outputs: per output its status, whether it has a value and the estimated tokens of
|
|
112
|
+
* that value. Error texts are left out: one failed run fails every output with the same text.
|
|
113
|
+
*/
|
|
114
|
+
export function summarizeOutputs(outputs: Record<string, unknown> | undefined): OutputSummary[] {
|
|
77
115
|
if (!outputs) return [];
|
|
78
|
-
return Object.entries(outputs).map(([key, out]) => {
|
|
79
|
-
const o = out
|
|
80
|
-
const
|
|
81
|
-
const
|
|
82
|
-
|
|
116
|
+
return Object.entries(outputs).map(([key, out]): OutputSummary => {
|
|
117
|
+
const o = typeof out === "object" && out !== null ? out : {};
|
|
118
|
+
const ok = "ok" in o && o.ok === true;
|
|
119
|
+
const value = "value" in o ? o.value : undefined;
|
|
120
|
+
const hasValue = ok && value != null;
|
|
121
|
+
const summary: OutputSummary = {
|
|
122
|
+
key,
|
|
123
|
+
status: !ok ? "error" : "stable" in o && o.stable === false ? "pending" : "ok",
|
|
124
|
+
hasValue,
|
|
125
|
+
};
|
|
126
|
+
if (hasValue) summary.tokensEstimate = estimateTokens(value);
|
|
127
|
+
return summary;
|
|
83
128
|
});
|
|
84
129
|
}
|
|
130
|
+
|
|
131
|
+
/** The output summary of a block view, and when outputs failed a note on where to read why. */
|
|
132
|
+
export type OutputsView = { outputs: OutputSummary[]; outputsNote?: string };
|
|
133
|
+
|
|
134
|
+
/** Sums up block outputs ({@link summarizeOutputs}); a failed output adds `outputsNote`. */
|
|
135
|
+
export function outputsView(outputs: Record<string, unknown> | undefined): OutputsView {
|
|
136
|
+
const summaries = summarizeOutputs(outputs);
|
|
137
|
+
const failed = summaries.filter((s) => s.status === "error");
|
|
138
|
+
const first = failed[0];
|
|
139
|
+
if (first === undefined) return { outputs: summaries };
|
|
140
|
+
return { outputs: summaries, outputsNote: failedOutputsNote(failed.length, first.key) };
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** Says how many outputs failed and how to read the error of one of them, `key`. */
|
|
144
|
+
export function failedOutputsNote(failed: number, key: string): string {
|
|
145
|
+
return (
|
|
146
|
+
`${failed} ${failed === 1 ? "output" : "outputs"} failed; get_block_outputs with ` +
|
|
147
|
+
`\`keys: [${JSON.stringify(key)}]\` gives its error.`
|
|
148
|
+
);
|
|
149
|
+
}
|
package/src/tools/types.ts
CHANGED
|
@@ -14,7 +14,9 @@ import type {
|
|
|
14
14
|
ToolAnnotations,
|
|
15
15
|
ToolCallback,
|
|
16
16
|
} from "@modelcontextprotocol/server";
|
|
17
|
+
import type { AgentTables } from "../agent-tables";
|
|
17
18
|
import type { McpInstanceId, PlMcpServerCallbacks } from "../server";
|
|
19
|
+
import { jsonChars, outlineValue, stripBytes } from "./value-outline";
|
|
18
20
|
|
|
19
21
|
export type { AuthorMarker };
|
|
20
22
|
|
|
@@ -25,10 +27,22 @@ export interface ToolContext {
|
|
|
25
27
|
/**
|
|
26
28
|
* Runs `fn` on the project, loading it when it is not loaded. The server holds the project
|
|
27
29
|
* while `fn` runs, so neither the application nor anyone else can unload it under the call.
|
|
30
|
+
* `fn` is also given the middle layer the project is of, which the server took when the call
|
|
31
|
+
* started: its drivers are the ones that know the project's handles.
|
|
28
32
|
*/
|
|
29
|
-
withProject: <T>(
|
|
33
|
+
withProject: <T>(
|
|
34
|
+
projectId: ProjectId,
|
|
35
|
+
fn: (project: Project, ml: MiddleLayer) => Promise<T>,
|
|
36
|
+
) => Promise<T>;
|
|
37
|
+
/**
|
|
38
|
+
* The tables agents built with create_table, by handle: reads find their project here, and
|
|
39
|
+
* the server gives them back when they expire, are dropped or lose their project.
|
|
40
|
+
*/
|
|
41
|
+
agentTables: AgentTables;
|
|
30
42
|
/** The holder of this server's holds, as the middle layer's `projectHolders` lists them. */
|
|
31
43
|
projectHolder: ProjectHolder;
|
|
44
|
+
/** How many tool calls of this server are working on the project now. */
|
|
45
|
+
callsInFlight: (projectId: ProjectId) => number;
|
|
32
46
|
callbacks: PlMcpServerCallbacks;
|
|
33
47
|
/**
|
|
34
48
|
* Returns the marker for the next write made through MCP. All writes of one server instance carry
|
|
@@ -117,24 +131,75 @@ export const hints = {
|
|
|
117
131
|
idempotentHint: true,
|
|
118
132
|
openWorldHint: false,
|
|
119
133
|
},
|
|
120
|
-
/** Acts on the application window or runs given code: the effect depends on what it hits. */
|
|
121
|
-
unpredictable: {
|
|
122
|
-
readOnlyHint: false,
|
|
123
|
-
destructiveHint: true,
|
|
124
|
-
idempotentHint: false,
|
|
125
|
-
openWorldHint: false,
|
|
126
|
-
},
|
|
127
134
|
} as const satisfies Record<string, ToolHints>;
|
|
128
135
|
|
|
129
|
-
|
|
136
|
+
/** How long one driver read of a tool (a table, a column's values, a file) may take. */
|
|
137
|
+
export const READ_TIMEOUT_MS = 60_000;
|
|
138
|
+
|
|
139
|
+
/** The signal of one driver read: the tool call's, and {@link READ_TIMEOUT_MS}. */
|
|
140
|
+
export function readSignal(callSignal: AbortSignal | undefined): AbortSignal {
|
|
141
|
+
const timeout = AbortSignal.timeout(READ_TIMEOUT_MS);
|
|
142
|
+
return callSignal === undefined ? timeout : AbortSignal.any([callSignal, timeout]);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** The most characters the text of a tool result may have. */
|
|
146
|
+
export const RESULT_CHAR_LIMIT = 60_000;
|
|
147
|
+
|
|
148
|
+
/** What a tool says when its result is over {@link RESULT_CHAR_LIMIT} and it has no better hint. */
|
|
149
|
+
const DEFAULT_OVER_LIMIT_HINT =
|
|
150
|
+
"Ask for a part of it: use `transform` where the tool has one (get_block_state, " +
|
|
151
|
+
"await_block_done, query_table), or narrower arguments.";
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Returns the result of a tool: `data` as JSON text. Every tool result goes through here, so two
|
|
155
|
+
* rules hold for all of them:
|
|
156
|
+
*
|
|
157
|
+
* - binary data is replaced by `{bytes: N}` (see {@link stripBytes}): it never helps an agent and
|
|
158
|
+
* its JSON is many times its size;
|
|
159
|
+
* - a result over {@link RESULT_CHAR_LIMIT} characters is replaced by an outline of it (keys, types
|
|
160
|
+
* and sizes, see {@link outlineValue}) with `hint`, saying how to ask for a part. It is not an
|
|
161
|
+
* error: the agent learns what is there and asks again.
|
|
162
|
+
*/
|
|
163
|
+
export function textResult(data: unknown, options: { hint?: string } = {}) {
|
|
164
|
+
const value = stripBytes(data);
|
|
165
|
+
const text = JSON.stringify(value) ?? "null";
|
|
166
|
+
if (text.length <= RESULT_CHAR_LIMIT) return { content: [{ type: "text" as const, text }] };
|
|
130
167
|
return {
|
|
131
|
-
content: [
|
|
168
|
+
content: [
|
|
169
|
+
{
|
|
170
|
+
type: "text" as const,
|
|
171
|
+
text: JSON.stringify(overLimitOutline(value, text.length, options.hint)),
|
|
172
|
+
},
|
|
173
|
+
],
|
|
132
174
|
};
|
|
133
175
|
}
|
|
134
176
|
|
|
135
|
-
/**
|
|
177
|
+
/** What stands for a result over the limit; the outline loses depth until it fits. */
|
|
178
|
+
function overLimitOutline(value: unknown, chars: number, hint: string | undefined) {
|
|
179
|
+
const head = {
|
|
180
|
+
resultTooLarge: true,
|
|
181
|
+
chars,
|
|
182
|
+
limit: RESULT_CHAR_LIMIT,
|
|
183
|
+
note: "The result is over the size limit; this outline shows its keys, types and sizes (chars).",
|
|
184
|
+
hint: hint ?? DEFAULT_OVER_LIMIT_HINT,
|
|
185
|
+
};
|
|
186
|
+
for (const maxDepth of [3, 2, 1, 0]) {
|
|
187
|
+
const outlined = { ...head, outline: outlineValue(value, { maxDepth }) };
|
|
188
|
+
if (jsonChars(outlined) <= RESULT_CHAR_LIMIT) return outlined;
|
|
189
|
+
}
|
|
190
|
+
return head;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Return an MCP error result with an actionable hint for the AI agent. The text is cut at
|
|
195
|
+
* {@link RESULT_CHAR_LIMIT} characters.
|
|
196
|
+
*/
|
|
136
197
|
export function errorResult(message: string, hint?: string) {
|
|
137
|
-
const
|
|
198
|
+
const full = hint ? `${message}\n\nHint: ${hint}` : message;
|
|
199
|
+
const text =
|
|
200
|
+
full.length <= RESULT_CHAR_LIMIT
|
|
201
|
+
? full
|
|
202
|
+
: `${full.slice(0, RESULT_CHAR_LIMIT - 40)}… [cut: ${full.length} chars in all]`;
|
|
138
203
|
return {
|
|
139
204
|
content: [{ type: "text" as const, text }],
|
|
140
205
|
isError: true,
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shapes of values the tools return: binary data replaced by its size, and outlines that stand for
|
|
3
|
+
* values too large to return. Has no dependencies, so both the shared result path and the block
|
|
4
|
+
* view use it.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/** What binary data is replaced with in a tool result. */
|
|
8
|
+
export type BytesStub = { bytes: number };
|
|
9
|
+
|
|
10
|
+
/** Whether `v` has a `toJSON` of its own kind (a `Date`), which `JSON.stringify` calls. */
|
|
11
|
+
function hasToJson(v: object): boolean {
|
|
12
|
+
return "toJSON" in v && typeof v.toJSON === "function";
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Returns `value` with every piece of live binary data replaced by `{bytes: N}`: typed arrays,
|
|
17
|
+
* Buffers and `ArrayBuffer`s. What such data became after it went through JSON is plain data and
|
|
18
|
+
* stays. Parts that hold no binary data are returned as they are, not copied.
|
|
19
|
+
*/
|
|
20
|
+
export function stripBytes(value: unknown): unknown {
|
|
21
|
+
const inProgress = new Set<object>();
|
|
22
|
+
|
|
23
|
+
function walk(v: unknown): unknown {
|
|
24
|
+
if (v === null || typeof v !== "object") return v;
|
|
25
|
+
if (ArrayBuffer.isView(v)) return { bytes: v.byteLength } satisfies BytesStub;
|
|
26
|
+
if (v instanceof ArrayBuffer) return { bytes: v.byteLength } satisfies BytesStub;
|
|
27
|
+
if (hasToJson(v)) return v;
|
|
28
|
+
// A cycle cannot be serialized anyway; leave it for JSON.stringify to report.
|
|
29
|
+
if (inProgress.has(v)) return v;
|
|
30
|
+
inProgress.add(v);
|
|
31
|
+
try {
|
|
32
|
+
if (Array.isArray(v)) {
|
|
33
|
+
let out: unknown[] | undefined;
|
|
34
|
+
for (let i = 0; i < v.length; i++) {
|
|
35
|
+
const next = walk(v[i]);
|
|
36
|
+
if (next !== v[i]) {
|
|
37
|
+
out ??= v.slice();
|
|
38
|
+
out[i] = next;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return out ?? v;
|
|
42
|
+
}
|
|
43
|
+
let changed = false;
|
|
44
|
+
const entries = Object.entries(v).map(([key, item]) => {
|
|
45
|
+
const next = walk(item);
|
|
46
|
+
if (next !== item) changed = true;
|
|
47
|
+
return [key, next] as const;
|
|
48
|
+
});
|
|
49
|
+
// `Object.fromEntries` defines own keys: a key such as `__proto__` stays a key of the copy.
|
|
50
|
+
return changed ? Object.fromEntries(entries) : v;
|
|
51
|
+
} finally {
|
|
52
|
+
inProgress.delete(v);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return walk(value);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** The JSON length of `value`; 0 for what JSON has no text for. */
|
|
60
|
+
export function jsonChars(value: unknown): number {
|
|
61
|
+
try {
|
|
62
|
+
return JSON.stringify(value)?.length ?? 0;
|
|
63
|
+
} catch {
|
|
64
|
+
return 0;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* An outline of a value: its type and JSON size and, for objects and arrays, what they hold, down
|
|
70
|
+
* to a depth. It stands for a value too large to return.
|
|
71
|
+
*/
|
|
72
|
+
export type ValueOutline =
|
|
73
|
+
| { type: "null" | "boolean" | "number" | "string"; chars: number }
|
|
74
|
+
| {
|
|
75
|
+
type: "object";
|
|
76
|
+
chars: number;
|
|
77
|
+
keys: number;
|
|
78
|
+
/** The outlines of the first keys, while the depth and the node budget last. */
|
|
79
|
+
fields?: Record<string, ValueOutline>;
|
|
80
|
+
/** How many keys `fields` leaves out. */
|
|
81
|
+
moreKeys?: number;
|
|
82
|
+
}
|
|
83
|
+
| {
|
|
84
|
+
type: "array";
|
|
85
|
+
chars: number;
|
|
86
|
+
length: number;
|
|
87
|
+
/** The outline of the first element, while the depth and the node budget last. */
|
|
88
|
+
first?: ValueOutline;
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
/** Limits of {@link outlineValue}. */
|
|
92
|
+
export type OutlineOptions = {
|
|
93
|
+
/** How many levels below the root get outlined (default 3). */
|
|
94
|
+
maxDepth?: number;
|
|
95
|
+
/** How many keys of one object get outlined (default 40). */
|
|
96
|
+
maxKeys?: number;
|
|
97
|
+
/** How many outlines in total (default 400); the outline stays bounded whatever the value. */
|
|
98
|
+
maxNodes?: number;
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Outlines `value`: for each object its keys with their types and JSON sizes, for each array its
|
|
103
|
+
* length and the outline of its first element. Bounded in depth, keys per object and nodes in all.
|
|
104
|
+
*/
|
|
105
|
+
export function outlineValue(value: unknown, options: OutlineOptions = {}): ValueOutline {
|
|
106
|
+
const maxDepth = options.maxDepth ?? 3;
|
|
107
|
+
const maxKeys = options.maxKeys ?? 40;
|
|
108
|
+
let budget = options.maxNodes ?? 400;
|
|
109
|
+
|
|
110
|
+
function walk(v: unknown, depth: number): ValueOutline {
|
|
111
|
+
budget--;
|
|
112
|
+
const chars = jsonChars(v);
|
|
113
|
+
if (v === null || v === undefined) return { type: "null", chars };
|
|
114
|
+
if (typeof v === "boolean") return { type: "boolean", chars };
|
|
115
|
+
if (typeof v === "number" || typeof v === "bigint") return { type: "number", chars };
|
|
116
|
+
if (typeof v === "string") return { type: "string", chars };
|
|
117
|
+
if (Array.isArray(v)) {
|
|
118
|
+
const outline: ValueOutline = { type: "array", chars, length: v.length };
|
|
119
|
+
if (v.length > 0 && depth < maxDepth && budget > 0) outline.first = walk(v[0], depth + 1);
|
|
120
|
+
return outline;
|
|
121
|
+
}
|
|
122
|
+
if (typeof v === "object") {
|
|
123
|
+
const entries = Object.entries(v);
|
|
124
|
+
const outline: ValueOutline = { type: "object", chars, keys: entries.length };
|
|
125
|
+
if (entries.length > 0 && depth < maxDepth && budget > 0) {
|
|
126
|
+
const fields: [string, ValueOutline][] = [];
|
|
127
|
+
for (const [key, item] of entries) {
|
|
128
|
+
if (fields.length >= maxKeys || budget <= 0) break;
|
|
129
|
+
fields.push([key, walk(item, depth + 1)]);
|
|
130
|
+
}
|
|
131
|
+
const shown = fields.length;
|
|
132
|
+
// `Object.fromEntries` defines own keys: a key such as `__proto__` stays a key of the outline.
|
|
133
|
+
outline.fields = Object.fromEntries(fields);
|
|
134
|
+
if (shown < entries.length) outline.moreKeys = entries.length - shown;
|
|
135
|
+
}
|
|
136
|
+
return outline;
|
|
137
|
+
}
|
|
138
|
+
// Functions and symbols: JSON drops them.
|
|
139
|
+
return { type: "null", chars };
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
return walk(value, 0);
|
|
143
|
+
}
|