@milaboratories/pl-mcp-server 53.1.0 → 55.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +30 -37
- package/dist/_virtual/_rolldown/runtime.cjs +22 -0
- package/dist/agent-tables.cjs +116 -0
- package/dist/agent-tables.cjs.map +1 -0
- package/dist/agent-tables.js +115 -0
- package/dist/agent-tables.js.map +1 -0
- package/dist/instructions.cjs +16 -12
- package/dist/instructions.cjs.map +1 -1
- package/dist/instructions.js +16 -12
- package/dist/instructions.js.map +1 -1
- package/dist/package.cjs +1 -1
- package/dist/package.js +1 -1
- package/dist/project-holds.cjs +122 -0
- package/dist/project-holds.cjs.map +1 -0
- package/dist/project-holds.js +122 -0
- package/dist/project-holds.js.map +1 -0
- package/dist/project-list.cjs +16 -0
- package/dist/project-list.cjs.map +1 -0
- package/dist/project-list.js +16 -0
- package/dist/project-list.js.map +1 -0
- package/dist/server-log.cjs +48 -18
- package/dist/server-log.cjs.map +1 -1
- package/dist/server-log.js +48 -19
- package/dist/server-log.js.map +1 -1
- package/dist/server.cjs +71 -18
- package/dist/server.cjs.map +1 -1
- package/dist/server.d.ts +37 -24
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +71 -19
- package/dist/server.js.map +1 -1
- package/dist/tools/agent-tables.cjs +424 -0
- package/dist/tools/agent-tables.cjs.map +1 -0
- package/dist/tools/agent-tables.js +417 -0
- package/dist/tools/agent-tables.js.map +1 -0
- package/dist/tools/await.cjs +216 -72
- package/dist/tools/await.cjs.map +1 -1
- package/dist/tools/await.js +212 -74
- package/dist/tools/await.js.map +1 -1
- package/dist/tools/block-action-context.cjs +14 -9
- package/dist/tools/block-action-context.cjs.map +1 -1
- package/dist/tools/block-action-context.js +14 -9
- package/dist/tools/block-action-context.js.map +1 -1
- package/dist/tools/block-api.cjs +17 -2
- package/dist/tools/block-api.cjs.map +1 -1
- package/dist/tools/block-api.js +17 -3
- package/dist/tools/block-api.js.map +1 -1
- package/dist/tools/block-params.cjs +275 -6
- package/dist/tools/block-params.cjs.map +1 -1
- package/dist/tools/block-params.js +273 -8
- package/dist/tools/block-params.js.map +1 -1
- package/dist/tools/block-state.cjs +343 -57
- package/dist/tools/block-state.cjs.map +1 -1
- package/dist/tools/block-state.js +337 -61
- package/dist/tools/block-state.js.map +1 -1
- package/dist/tools/block-storage.cjs +153 -0
- package/dist/tools/block-storage.cjs.map +1 -0
- package/dist/tools/block-storage.js +147 -0
- package/dist/tools/block-storage.js.map +1 -0
- package/dist/tools/block-updates.cjs +405 -0
- package/dist/tools/block-updates.cjs.map +1 -0
- package/dist/tools/block-updates.js +397 -0
- package/dist/tools/block-updates.js.map +1 -0
- package/dist/tools/blocks.cjs +199 -76
- package/dist/tools/blocks.cjs.map +1 -1
- package/dist/tools/blocks.js +193 -76
- package/dist/tools/blocks.js.map +1 -1
- package/dist/tools/data-query.cjs +470 -101
- package/dist/tools/data-query.cjs.map +1 -1
- package/dist/tools/data-query.js +465 -106
- package/dist/tools/data-query.js.map +1 -1
- package/dist/tools/errors.cjs +21 -0
- package/dist/tools/errors.cjs.map +1 -0
- package/dist/tools/errors.js +20 -0
- package/dist/tools/errors.js.map +1 -0
- package/dist/tools/files.cjs +643 -0
- package/dist/tools/files.cjs.map +1 -0
- package/dist/tools/files.js +613 -0
- package/dist/tools/files.js.map +1 -0
- package/dist/tools/ids.cjs +72 -0
- package/dist/tools/ids.cjs.map +1 -1
- package/dist/tools/ids.js +63 -2
- package/dist/tools/ids.js.map +1 -1
- package/dist/tools/imports.cjs +195 -0
- package/dist/tools/imports.cjs.map +1 -0
- package/dist/tools/imports.js +186 -0
- package/dist/tools/imports.js.map +1 -0
- package/dist/tools/logs.cjs +111 -30
- package/dist/tools/logs.cjs.map +1 -1
- package/dist/tools/logs.js +108 -31
- package/dist/tools/logs.js.map +1 -1
- package/dist/tools/paging.cjs +27 -0
- package/dist/tools/paging.cjs.map +1 -0
- package/dist/tools/paging.js +26 -0
- package/dist/tools/paging.js.map +1 -0
- package/dist/tools/params-merge.cjs +106 -0
- package/dist/tools/params-merge.cjs.map +1 -0
- package/dist/tools/params-merge.js +106 -0
- package/dist/tools/params-merge.js.map +1 -0
- package/dist/tools/project-template.cjs +75 -0
- package/dist/tools/project-template.cjs.map +1 -0
- package/dist/tools/project-template.js +72 -0
- package/dist/tools/project-template.js.map +1 -0
- package/dist/tools/projects.cjs +68 -38
- package/dist/tools/projects.cjs.map +1 -1
- package/dist/tools/projects.js +69 -40
- package/dist/tools/projects.js.map +1 -1
- package/dist/tools/readiness.cjs +104 -0
- package/dist/tools/readiness.cjs.map +1 -0
- package/dist/tools/readiness.js +99 -0
- package/dist/tools/readiness.js.map +1 -0
- package/dist/tools/result-pool.cjs +260 -0
- package/dist/tools/result-pool.cjs.map +1 -0
- package/dist/tools/result-pool.js +252 -0
- package/dist/tools/result-pool.js.map +1 -0
- package/dist/tools/sandbox.cjs +112 -14
- package/dist/tools/sandbox.cjs.map +1 -1
- package/dist/tools/sandbox.js +110 -15
- package/dist/tools/sandbox.js.map +1 -1
- package/dist/tools/sdk-types.cjs +24 -0
- package/dist/tools/sdk-types.cjs.map +1 -0
- package/dist/tools/sdk-types.js +24 -0
- package/dist/tools/sdk-types.js.map +1 -0
- package/dist/tools/settle.cjs +155 -0
- package/dist/tools/settle.cjs.map +1 -0
- package/dist/tools/settle.js +148 -0
- package/dist/tools/settle.js.map +1 -0
- package/dist/tools/tokens.cjs +49 -8
- package/dist/tools/tokens.cjs.map +1 -1
- package/dist/tools/tokens.js +45 -9
- package/dist/tools/tokens.js.map +1 -1
- package/dist/tools/types.cjs +62 -11
- package/dist/tools/types.cjs.map +1 -1
- package/dist/tools/types.js +60 -12
- package/dist/tools/types.js.map +1 -1
- package/dist/tools/value-outline.cjs +119 -0
- package/dist/tools/value-outline.cjs.map +1 -0
- package/dist/tools/value-outline.js +117 -0
- package/dist/tools/value-outline.js.map +1 -0
- package/package.json +9 -8
- package/src/agent-tables.test.ts +246 -0
- package/src/agent-tables.ts +145 -0
- package/src/instructions.ts +16 -12
- package/src/project-holds.test.ts +362 -0
- package/src/project-holds.ts +207 -0
- package/src/project-list.test.ts +38 -0
- package/src/project-list.ts +25 -0
- package/src/server-log.test.ts +71 -6
- package/src/server-log.ts +58 -27
- package/src/server.logging.test.ts +46 -51
- package/src/server.test.ts +166 -22
- package/src/server.ts +97 -37
- package/src/tools/agent-tables.test.ts +650 -0
- package/src/tools/agent-tables.ts +673 -0
- package/src/tools/await.test.ts +287 -0
- package/src/tools/await.ts +393 -122
- package/src/tools/block-action-context.ts +17 -13
- package/src/tools/block-api.ts +19 -4
- package/src/tools/block-params.test.ts +828 -0
- package/src/tools/block-params.ts +626 -10
- package/src/tools/block-state.test.ts +480 -0
- package/src/tools/block-state.ts +678 -83
- package/src/tools/block-storage.test.ts +270 -0
- package/src/tools/block-storage.ts +195 -0
- package/src/tools/block-updates.test.ts +605 -0
- package/src/tools/block-updates.ts +751 -0
- package/src/tools/blocks.test.ts +169 -22
- package/src/tools/blocks.ts +354 -113
- package/src/tools/data-query.test.ts +641 -0
- package/src/tools/data-query.ts +839 -145
- package/src/tools/errors.test.ts +33 -0
- package/src/tools/errors.ts +23 -0
- package/src/tools/files.test.ts +842 -0
- package/src/tools/files.ts +990 -0
- package/src/tools/ids.test.ts +87 -0
- package/src/tools/ids.ts +89 -1
- package/src/tools/imports.ts +304 -0
- package/src/tools/logs.test.ts +70 -0
- package/src/tools/logs.ts +154 -44
- package/src/tools/paging.test.ts +39 -0
- package/src/tools/paging.ts +39 -0
- package/src/tools/params-merge.test.ts +274 -0
- package/src/tools/params-merge.ts +149 -0
- package/src/tools/project-template.test.ts +230 -0
- package/src/tools/project-template.ts +135 -0
- package/src/tools/projects.test.ts +122 -0
- package/src/tools/projects.ts +136 -48
- package/src/tools/readiness.test.ts +195 -0
- package/src/tools/readiness.ts +189 -0
- package/src/tools/result-pool.test.ts +377 -0
- package/src/tools/result-pool.ts +483 -0
- package/src/tools/sandbox.test.ts +128 -10
- package/src/tools/sandbox.ts +132 -18
- package/src/tools/sdk-types.ts +54 -0
- package/src/tools/settle.test.ts +340 -0
- package/src/tools/settle.ts +312 -0
- package/src/tools/tokens.ts +74 -9
- package/src/tools/types.ts +84 -12
- package/src/tools/value-outline.ts +143 -0
- package/dist/tools/screenshot.cjs +0 -44
- package/dist/tools/screenshot.cjs.map +0 -1
- package/dist/tools/screenshot.js +0 -44
- package/dist/tools/screenshot.js.map +0 -1
- package/dist/tools/ui-interaction.cjs +0 -140
- package/dist/tools/ui-interaction.cjs.map +0 -1
- package/dist/tools/ui-interaction.js +0 -140
- package/dist/tools/ui-interaction.js.map +0 -1
- package/src/tools/screenshot.ts +0 -61
- package/src/tools/ui-interaction.ts +0 -184
package/dist/tools/data-query.js
CHANGED
|
@@ -1,11 +1,27 @@
|
|
|
1
|
-
import { errorResult, hints, textResult, transformFailedResult } from "./types.js";
|
|
2
|
-
import { blockIdInput, projectIdInput } from "./ids.js";
|
|
3
|
-
import {
|
|
1
|
+
import { RESULT_CHAR_LIMIT, errorResult, hints, readSignal, textResult, transformFailedResult } from "./types.js";
|
|
2
|
+
import { blockIdInput, columnIdFromAgent, frameColumnIdInput, pFrameHandleInput, pTableHandleInput, projectIdInput } from "./ids.js";
|
|
3
|
+
import { clipMiddle, failedOutputsNote, outputErrorText, summarizeOutputs } from "./tokens.js";
|
|
4
|
+
import { expressionTimeoutText, safeEval } from "./sandbox.js";
|
|
5
|
+
import { ownValue } from "./block-storage.js";
|
|
6
|
+
import { STALE_RESULTS_NOTE, findBlock } from "./readiness.js";
|
|
7
|
+
import { clamp, isPlainObject } from "@milaboratories/helpers";
|
|
4
8
|
import { z } from "zod";
|
|
5
|
-
import { Annotation, pTableValue, readAnnotation } from "@milaboratories/pl-model-common";
|
|
9
|
+
import { Annotation, ValueType, createColumnValueIds, getAxisId, isGlobalPObjectId, isPFrameDriverError, isValueNA, pTableValue, readAnnotation } from "@milaboratories/pl-model-common";
|
|
6
10
|
// src/tools/data-query.ts
|
|
7
11
|
const HEX_HASH_RE = /^[a-f0-9]{64}$/;
|
|
8
12
|
/**
|
|
13
|
+
* The error of a tool given a handle the driver does not know (any more). A handle lives as long as
|
|
14
|
+
* a project it came from stays loaded, and the server lets a project go some time after the last
|
|
15
|
+
* call on it.
|
|
16
|
+
*/
|
|
17
|
+
function staleHandleResult(kind, handle, err) {
|
|
18
|
+
return errorResult(`${kind} handle ${handle} is not valid: ${err.message}`, "Handles stop working some time after the last call on their project. " + (kind === "PTable" ? "Call get_block_outputs again for a fresh handle, or create_table again for a table of your own (those also go when dropped or not read for a while)." : "Call get_block_outputs again for a fresh handle."));
|
|
19
|
+
}
|
|
20
|
+
/** Whether `err` is the driver's answer to a handle it does not know (any more). */
|
|
21
|
+
function isUnknownHandleError(err) {
|
|
22
|
+
return isPFrameDriverError(err) && /^Invalid P(Frame|Table) handle/.test(err.message);
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
9
25
|
* Try to resolve a 64-char hex handle as PTable, then PFrame.
|
|
10
26
|
* Returns a summary object or the original string if neither works.
|
|
11
27
|
*/
|
|
@@ -16,11 +32,11 @@ async function resolveHandle(handle, driver, maxColumns, cache) {
|
|
|
16
32
|
const summary = {
|
|
17
33
|
_type: "PTable",
|
|
18
34
|
handle,
|
|
19
|
-
rows: (await driver.getShape(handle)).rows,
|
|
20
35
|
columnCount: spec.length,
|
|
21
36
|
columns: spec.slice(0, maxColumns).map((s, idx) => ({
|
|
22
37
|
index: idx,
|
|
23
38
|
type: s.type,
|
|
39
|
+
...s.type === "column" ? { columnId: s.id } : {},
|
|
24
40
|
name: s.spec.name,
|
|
25
41
|
valueType: s.type === "column" ? s.spec.valueType : s.spec.type,
|
|
26
42
|
label: readAnnotation(s.spec, Annotation.Label)
|
|
@@ -40,6 +56,7 @@ async function resolveHandle(handle, driver, maxColumns, cache) {
|
|
|
40
56
|
handle,
|
|
41
57
|
columnCount: columns.length,
|
|
42
58
|
columns: columns.slice(0, maxColumns).map((c) => ({
|
|
59
|
+
columnId: c.columnId,
|
|
43
60
|
name: c.spec.name,
|
|
44
61
|
valueType: c.spec.valueType,
|
|
45
62
|
label: readAnnotation(c.spec, Annotation.Label)
|
|
@@ -57,138 +74,480 @@ async function resolveHandle(handle, driver, maxColumns, cache) {
|
|
|
57
74
|
}
|
|
58
75
|
/**
|
|
59
76
|
* Recursively walk a value tree, replacing 64-char hex handles
|
|
60
|
-
* with PFrame/PTable summaries (column specs
|
|
77
|
+
* with PFrame/PTable summaries (column specs).
|
|
61
78
|
*/
|
|
62
79
|
async function resolveHandlesInValue(value, driver, maxColumns, cache) {
|
|
63
80
|
if (value === null || value === void 0) return value;
|
|
64
81
|
if (typeof value === "string") return HEX_HASH_RE.test(value) ? resolveHandle(value, driver, maxColumns, cache) : value;
|
|
65
82
|
if (typeof value !== "object") return value;
|
|
66
83
|
if (Array.isArray(value)) return Promise.all(value.map((v) => resolveHandlesInValue(v, driver, maxColumns, cache)));
|
|
67
|
-
const
|
|
68
|
-
for (const [k, v] of Object.entries(value))
|
|
69
|
-
return
|
|
84
|
+
const entries = [];
|
|
85
|
+
for (const [k, v] of Object.entries(value)) entries.push([k, await resolveHandlesInValue(v, driver, maxColumns, cache)]);
|
|
86
|
+
return Object.fromEntries(entries);
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Replaces every PFrame or PTable summary of `outputs` (output key → resolved value) whose handle
|
|
90
|
+
* an earlier one already summed up with `{ sameAs: "<path>" }`, the path to that first summary in
|
|
91
|
+
* `outputs`, written as a JavaScript accessor: `main.table`, `list[0]`, `["a key"].x`. Outputs are
|
|
92
|
+
* walked in their order, values depth-first. A block often gives the same table in two outputs
|
|
93
|
+
* (D-025): each summary is then given once, and every repeat names the one it repeats.
|
|
94
|
+
*/
|
|
95
|
+
function dedupeHandleSummaries(outputs) {
|
|
96
|
+
const firstSeen = /* @__PURE__ */ new Map();
|
|
97
|
+
const walk = (value, path) => {
|
|
98
|
+
if (typeof value !== "object" || value === null) return value;
|
|
99
|
+
if (Array.isArray(value)) return value.map((v, i) => walk(v, `${path}[${i}]`));
|
|
100
|
+
const record = Object.fromEntries(Object.entries(value));
|
|
101
|
+
const handle = record.handle;
|
|
102
|
+
if ((record._type === "PTable" || record._type === "PFrame") && typeof handle === "string") {
|
|
103
|
+
const first = firstSeen.get(handle);
|
|
104
|
+
if (first !== void 0) return { sameAs: first };
|
|
105
|
+
firstSeen.set(handle, path);
|
|
106
|
+
return record;
|
|
107
|
+
}
|
|
108
|
+
return Object.fromEntries(Object.entries(record).map(([k, v]) => [k, walk(v, `${path}${propertyAccessor(k)}`)]));
|
|
109
|
+
};
|
|
110
|
+
return Object.fromEntries(Object.entries(outputs).map(([key, v]) => [key, walk(v, JS_IDENTIFIER.test(key) ? key : propertyAccessor(key))]));
|
|
111
|
+
}
|
|
112
|
+
/** A name JavaScript accepts after a dot. */
|
|
113
|
+
const JS_IDENTIFIER = /^[A-Za-z_$][\w$]*$/;
|
|
114
|
+
/** How JavaScript reads property `key`: `.key`, or `["key"]` for a key that is not a plain name. */
|
|
115
|
+
function propertyAccessor(key) {
|
|
116
|
+
return JS_IDENTIFIER.test(key) ? `.${key}` : `[${JSON.stringify(key)}]`;
|
|
70
117
|
}
|
|
71
|
-
/**
|
|
118
|
+
/**
|
|
119
|
+
* Converts a PTableVector column to a JSON-serializable array using the SDK helper. Binary values,
|
|
120
|
+
* which the helper does not read, are given by their size, `{bytes: N}`; NA as `null`.
|
|
121
|
+
*/
|
|
72
122
|
function vectorToJson(vector, rows) {
|
|
73
123
|
const result = [];
|
|
74
|
-
for (let i = 0; i < rows; i++)
|
|
124
|
+
for (let i = 0; i < rows; i++) if (vector.type === ValueType.Bytes) {
|
|
125
|
+
const value = vector.data[i];
|
|
126
|
+
result.push(isValueNA(vector, i) || !(value instanceof Uint8Array) ? null : { bytes: value.length });
|
|
127
|
+
} else result.push(pTableValue(vector, i));
|
|
75
128
|
return result;
|
|
76
129
|
}
|
|
130
|
+
const MIN_SAFE_BIGINT = BigInt(Number.MIN_SAFE_INTEGER);
|
|
131
|
+
const MAX_SAFE_BIGINT = BigInt(Number.MAX_SAFE_INTEGER);
|
|
132
|
+
/**
|
|
133
|
+
* The value at `row` of `vector`, or null for NA. A Long outside the safe integer range is its
|
|
134
|
+
* exact decimal text: `pTableValue` rounds it to a number, and a value's id is built from its text.
|
|
135
|
+
*/
|
|
136
|
+
function exactValue(vector, row) {
|
|
137
|
+
const raw = vector.data[row];
|
|
138
|
+
if (typeof raw === "bigint" && !isValueNA(vector, row)) return raw >= MIN_SAFE_BIGINT && raw <= MAX_SAFE_BIGINT ? Number(raw) : raw.toString();
|
|
139
|
+
return pTableValue(vector, row, { na: null });
|
|
140
|
+
}
|
|
141
|
+
/** The most rows one `query_table` call returns. */
|
|
142
|
+
const QUERY_MAX_ROWS = 1e4;
|
|
143
|
+
/** The most columns one `list_columns` page has. */
|
|
144
|
+
const LIST_COLUMNS_MAX = 1e3;
|
|
145
|
+
/** The most values one `get_column_values` call asks the driver for. */
|
|
146
|
+
const COLUMN_VALUES_MAX = 1e4;
|
|
147
|
+
function clampInt(v, fallback, min, max) {
|
|
148
|
+
if (v === void 0 || !Number.isFinite(v)) return fallback;
|
|
149
|
+
return clamp(Math.floor(v), min, max);
|
|
150
|
+
}
|
|
151
|
+
/** The columns of a PFrame, filtered and paged (`list_columns`). */
|
|
152
|
+
async function listColumnsResult(driver, pFrameHandle, options) {
|
|
153
|
+
let columns;
|
|
154
|
+
try {
|
|
155
|
+
columns = await driver.listColumns(pFrameHandle);
|
|
156
|
+
} catch (err) {
|
|
157
|
+
if (isUnknownHandleError(err)) return staleHandleResult("PFrame", pFrameHandle, err);
|
|
158
|
+
throw err;
|
|
159
|
+
}
|
|
160
|
+
const needle = options.name?.toLowerCase();
|
|
161
|
+
const matching = columns.filter((c) => {
|
|
162
|
+
if (options.valueType !== void 0 && c.spec.valueType !== options.valueType) return false;
|
|
163
|
+
if (needle !== void 0) {
|
|
164
|
+
const label = readAnnotation(c.spec, Annotation.Label) ?? "";
|
|
165
|
+
if (!c.spec.name.toLowerCase().includes(needle) && !label.toLowerCase().includes(needle)) return false;
|
|
166
|
+
}
|
|
167
|
+
for (const [key, value] of Object.entries(options.annotations ?? {})) if (ownValue(c.spec.annotations ?? {}, key) !== value) return false;
|
|
168
|
+
return true;
|
|
169
|
+
});
|
|
170
|
+
const offset = clampInt(options.offset, 0, 0, Number.MAX_SAFE_INTEGER);
|
|
171
|
+
const limit = clampInt(options.limit, 100, 1, LIST_COLUMNS_MAX);
|
|
172
|
+
const page = matching.slice(offset, offset + limit);
|
|
173
|
+
const end = offset + page.length;
|
|
174
|
+
const full = options.detail === "full";
|
|
175
|
+
return textResult({
|
|
176
|
+
total: matching.length,
|
|
177
|
+
...matching.length !== columns.length ? { inFrame: columns.length } : {},
|
|
178
|
+
offset,
|
|
179
|
+
hasMore: end < matching.length,
|
|
180
|
+
...end < matching.length ? { nextOffset: end } : {},
|
|
181
|
+
columns: page.map((c) => ({
|
|
182
|
+
columnId: c.columnId,
|
|
183
|
+
name: c.spec.name,
|
|
184
|
+
valueType: c.spec.valueType,
|
|
185
|
+
label: readAnnotation(c.spec, Annotation.Label),
|
|
186
|
+
...full ? {
|
|
187
|
+
visibility: readAnnotation(c.spec, Annotation.Table.Visibility),
|
|
188
|
+
domain: c.spec.domain,
|
|
189
|
+
annotations: c.spec.annotations,
|
|
190
|
+
axes: c.spec.axesSpec.map((a) => ({
|
|
191
|
+
name: a.name,
|
|
192
|
+
type: a.type,
|
|
193
|
+
domain: a.domain,
|
|
194
|
+
label: readAnnotation(a, Annotation.Label)
|
|
195
|
+
}))
|
|
196
|
+
} : { axes: c.spec.axesSpec.map((a) => a.name) }
|
|
197
|
+
}))
|
|
198
|
+
}, { hint: "Ask for fewer columns: a smaller `limit`, `detail: \"brief\"`, or a `name` filter." });
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* The column of a PFrame an agent's column id names: listed under exactly that id, or under the
|
|
202
|
+
* same id written another way (escaped, other spacing or key order at its top level, a PlRef for a
|
|
203
|
+
* global id). Ids of older block APIs are not `ColumnUniversalId`s: they match only as listed.
|
|
204
|
+
*/
|
|
205
|
+
function findFrameColumn(listed, written) {
|
|
206
|
+
const exact = listed.find((c) => c.columnId === written);
|
|
207
|
+
if (exact !== void 0) return exact;
|
|
208
|
+
const canonical = columnIdFromAgent(written);
|
|
209
|
+
if (canonical === void 0) return void 0;
|
|
210
|
+
return listed.find((c) => columnIdFromAgent(c.columnId) === canonical);
|
|
211
|
+
}
|
|
212
|
+
/** The distinct values of a column, or of one of its axes, in a PFrame (`get_column_values`). */
|
|
213
|
+
async function columnValuesResult(driver, input, signal) {
|
|
214
|
+
const handle = input.pFrameHandle;
|
|
215
|
+
let listed;
|
|
216
|
+
try {
|
|
217
|
+
listed = await driver.listColumns(handle);
|
|
218
|
+
} catch (err) {
|
|
219
|
+
if (isUnknownHandleError(err)) return staleHandleResult("PFrame", input.pFrameHandle, err);
|
|
220
|
+
throw err;
|
|
221
|
+
}
|
|
222
|
+
const column = findFrameColumn(listed, input.columnId);
|
|
223
|
+
if (column === void 0) return errorResult(`Column ${input.columnId} is not in PFrame ${input.pFrameHandle}.`, "list_columns on the PFrame gives the ids of its columns; ids can change when a block runs again: list again.");
|
|
224
|
+
const { columnId, spec } = column;
|
|
225
|
+
let axisSpec;
|
|
226
|
+
if (input.axis !== void 0) {
|
|
227
|
+
const axes = spec.axesSpec;
|
|
228
|
+
axisSpec = typeof input.axis === "number" ? axes[input.axis] : axes.find((a) => a.name === input.axis);
|
|
229
|
+
if (axisSpec === void 0) return errorResult(`The column has no axis ${JSON.stringify(input.axis)}.`, `Its axes: ${axes.map((a, i) => `${i}: ${a.name}`).join(", ")}.`);
|
|
230
|
+
}
|
|
231
|
+
const valueType = axisSpec !== void 0 ? axisSpec.type : spec.valueType;
|
|
232
|
+
if (input.search !== void 0 && valueType !== "String") return errorResult(`\`search\` works on string values; these are ${valueType}.`, "Leave out `search`.");
|
|
233
|
+
const axisId = axisSpec !== void 0 ? getAxisId(axisSpec) : void 0;
|
|
234
|
+
const filters = input.search === void 0 || input.search === "" ? [] : [{
|
|
235
|
+
type: "bySingleColumnV2",
|
|
236
|
+
column: axisId !== void 0 ? {
|
|
237
|
+
type: "axis",
|
|
238
|
+
id: axisId
|
|
239
|
+
} : {
|
|
240
|
+
type: "column",
|
|
241
|
+
id: columnId
|
|
242
|
+
},
|
|
243
|
+
predicate: {
|
|
244
|
+
operator: "StringIContains",
|
|
245
|
+
substring: input.search
|
|
246
|
+
}
|
|
247
|
+
}];
|
|
248
|
+
const limit = clampInt(input.limit, 100, 1, COLUMN_VALUES_MAX);
|
|
249
|
+
let response;
|
|
250
|
+
try {
|
|
251
|
+
response = await driver.getUniqueValues(handle, {
|
|
252
|
+
columnId,
|
|
253
|
+
...axisId !== void 0 ? { axis: axisId } : {},
|
|
254
|
+
filters,
|
|
255
|
+
limit
|
|
256
|
+
}, signal);
|
|
257
|
+
} catch (err) {
|
|
258
|
+
if (isUnknownHandleError(err)) return staleHandleResult("PFrame", input.pFrameHandle, err);
|
|
259
|
+
if (signal?.aborted) return errorResult("Reading the values was stopped: it took too long or the request was cancelled.", "Narrow it with `search`, or ask for a smaller `limit`.");
|
|
260
|
+
return errorResult(`Reading the values failed: ${errorMessage(err)}`);
|
|
261
|
+
}
|
|
262
|
+
const values = [];
|
|
263
|
+
let naDropped = 0;
|
|
264
|
+
for (let i = 0; i < response.values.data.length; i++) {
|
|
265
|
+
const v = exactValue(response.values, i);
|
|
266
|
+
if (v === null) naDropped++;
|
|
267
|
+
else values.push(v);
|
|
268
|
+
}
|
|
269
|
+
const valueIds = axisSpec === void 0 && isGlobalPObjectId(columnId) ? createColumnValueIds({
|
|
270
|
+
column: columnId,
|
|
271
|
+
values: values.map(String)
|
|
272
|
+
}) : void 0;
|
|
273
|
+
return textResult({
|
|
274
|
+
columnId,
|
|
275
|
+
...axisSpec !== void 0 ? { axis: axisSpec.name } : {},
|
|
276
|
+
valueType,
|
|
277
|
+
count: values.length,
|
|
278
|
+
overflow: response.overflow,
|
|
279
|
+
...naDropped > 0 ? { naDropped } : {},
|
|
280
|
+
values,
|
|
281
|
+
...valueIds !== void 0 ? { valueIds } : {}
|
|
282
|
+
}, { hint: "Ask for fewer values: a smaller `limit`, or `search`." });
|
|
283
|
+
}
|
|
284
|
+
/**
|
|
285
|
+
* The distinct values of a column of the result pool, or of one of its axes (`get_column_values`
|
|
286
|
+
* with `projectId`): read from a frame built over the column alone and released after. Its
|
|
287
|
+
* `valueIds` name the column by its pool id, and come, as for a frame, only for a block's export
|
|
288
|
+
* itself: a discovered pool column gets none.
|
|
289
|
+
*/
|
|
290
|
+
async function poolColumnValuesResult(project, projectId, driver, input, signal) {
|
|
291
|
+
const id = columnIdFromAgent(input.columnId);
|
|
292
|
+
if (id === void 0) return errorResult(`${input.columnId} is not a column id of the result pool.`, "Copy `columnId` whole from list_pool_columns.");
|
|
293
|
+
const frame = project.createPoolFrame([id]);
|
|
294
|
+
try {
|
|
295
|
+
let built;
|
|
296
|
+
try {
|
|
297
|
+
built = await frame.getValue();
|
|
298
|
+
} catch (err) {
|
|
299
|
+
return errorResult(`Reading column ${id} failed: ${errorMessage(err)}`);
|
|
300
|
+
}
|
|
301
|
+
if (built.type === "refused") return poolColumnRefusedResult(built.refused, projectId);
|
|
302
|
+
return await columnValuesResult(driver, {
|
|
303
|
+
...input,
|
|
304
|
+
pFrameHandle: built.handle,
|
|
305
|
+
columnId: id
|
|
306
|
+
}, signal);
|
|
307
|
+
} finally {
|
|
308
|
+
frame.resetState();
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
/** Why the values of pool columns cannot be read, by the issue the middle layer names. */
|
|
312
|
+
function poolColumnRefusedResult(refused, projectId) {
|
|
313
|
+
const lines = [];
|
|
314
|
+
const hints = /* @__PURE__ */ new Set();
|
|
315
|
+
for (const r of refused) switch (r.issue) {
|
|
316
|
+
case "unknown":
|
|
317
|
+
lines.push(`${r.columnId}: not in the result pool of project ${projectId}.`);
|
|
318
|
+
hints.add("Ids follow current specs: list_pool_columns again.");
|
|
319
|
+
break;
|
|
320
|
+
case "noData":
|
|
321
|
+
lines.push(`${r.columnId}: has no data; its block has not run, or exports it from a pre-run only.`);
|
|
322
|
+
hints.add("Run the block (run_block), wait for it (await_block_done), then try again.");
|
|
323
|
+
break;
|
|
324
|
+
case "notReady":
|
|
325
|
+
lines.push(`${r.columnId}: still being calculated by its block.`);
|
|
326
|
+
hints.add("Wait for the block (await_block_done), then try again.");
|
|
327
|
+
break;
|
|
328
|
+
case "failed":
|
|
329
|
+
lines.push(`${r.columnId}: its data failed: ${r.message ?? "no message"}.`);
|
|
330
|
+
hints.add("get_block_logs and get_project_overview tell why the block failed.");
|
|
331
|
+
break;
|
|
332
|
+
case "filtered":
|
|
333
|
+
lines.push(`${r.columnId}: filters an axis; its values would be the whole column's.`);
|
|
334
|
+
hints.add("Read the values of the column it filters, its `source`.");
|
|
335
|
+
}
|
|
336
|
+
return errorResult(`The values cannot be read.\n${lines.join("\n")}`, [...hints].join(" "));
|
|
337
|
+
}
|
|
338
|
+
const STALE_TABLE_NOTE = "The columns this table was built from changed since (a block ran again, or the project changed): call create_table again for current data.";
|
|
339
|
+
/**
|
|
340
|
+
* Rows of a PTable (`query_table`). `agentTable`: the table is one an agent built with
|
|
341
|
+
* create_table; the answer says so when it is stale, and the hints name create_table.
|
|
342
|
+
*/
|
|
343
|
+
async function queryTableResult(driver, input, signal, agentTable) {
|
|
344
|
+
const { pTableHandle, columns, transform, transformTimeout } = input;
|
|
345
|
+
const handle = pTableHandle;
|
|
346
|
+
const isAgentTable = agentTable !== void 0;
|
|
347
|
+
const staleFlag = agentTable?.stale === true ? {
|
|
348
|
+
stale: true,
|
|
349
|
+
staleNote: STALE_TABLE_NOTE
|
|
350
|
+
} : {};
|
|
351
|
+
let spec;
|
|
352
|
+
let totalRows;
|
|
353
|
+
try {
|
|
354
|
+
spec = await driver.getSpec(handle);
|
|
355
|
+
totalRows = (await driver.getShape(handle, signal)).rows;
|
|
356
|
+
} catch (err) {
|
|
357
|
+
if (isUnknownHandleError(err)) return staleHandleResult("PTable", pTableHandle, err);
|
|
358
|
+
if (signal?.aborted) return errorResult(`Reading the size of the table failed: ${errorMessage(err)}`, "The read took too long or was cancelled: try again, or ask for a smaller table.");
|
|
359
|
+
throw err;
|
|
360
|
+
}
|
|
361
|
+
if (columns !== void 0 && columns.length === 0) return errorResult("`columns` is empty: there is nothing to read.", "Leave `columns` out to read every column, or name the indices of the ones you need.");
|
|
362
|
+
const columnIndices = columns ?? spec.map((_, i) => i);
|
|
363
|
+
const outOfRange = columnIndices.filter((i) => !Number.isInteger(i) || i < 0 || i >= spec.length);
|
|
364
|
+
if (outOfRange.length > 0) return errorResult(`No columns ${outOfRange.join(", ")}: the table has columns 0 to ${spec.length - 1}.`, isAgentTable ? "create_table listed the table's columns with their indices." : "get_block_outputs lists the table's columns with their indices.");
|
|
365
|
+
const offset = clampInt(input.offset, 0, 0, Number.MAX_SAFE_INTEGER);
|
|
366
|
+
const effectiveLimit = clampInt(input.limit, 50, 0, QUERY_MAX_ROWS);
|
|
367
|
+
const length = Math.max(0, Math.min(effectiveLimit, totalRows - offset));
|
|
368
|
+
const range = {
|
|
369
|
+
offset,
|
|
370
|
+
length
|
|
371
|
+
};
|
|
372
|
+
let vectors;
|
|
373
|
+
try {
|
|
374
|
+
vectors = length === 0 ? [] : await driver.getData(handle, columnIndices, range, signal);
|
|
375
|
+
} catch (err) {
|
|
376
|
+
if (isUnknownHandleError(err)) return staleHandleResult("PTable", pTableHandle, err);
|
|
377
|
+
return errorResult(`Reading rows ${offset} to ${offset + length} of columns [${columnIndices.join(", ")}] failed: ${errorMessage(err)}`, signal?.aborted ? "The read took too long or was cancelled: ask for fewer rows or columns." : isAgentTable ? "Ask for fewer rows or columns; if a block it reads ran again, call create_table again." : "Ask for fewer rows or columns; if the block ran again, get_block_outputs gives a fresh handle.");
|
|
378
|
+
}
|
|
379
|
+
const actualRows = vectors.length > 0 ? vectors[0].data.length : 0;
|
|
380
|
+
const columnVectors = vectors.map((v) => vectorToJson(v, actualRows));
|
|
381
|
+
const rows = [];
|
|
382
|
+
for (let r = 0; r < actualRows; r++) rows.push(columnVectors.map((col) => col[r]));
|
|
383
|
+
const columnHeaders = columnIndices.map((idx) => {
|
|
384
|
+
const s = spec[idx];
|
|
385
|
+
return {
|
|
386
|
+
index: idx,
|
|
387
|
+
type: s.type,
|
|
388
|
+
...s.type === "column" ? { columnId: s.id } : {},
|
|
389
|
+
name: s.spec.name,
|
|
390
|
+
label: readAnnotation(s.spec, Annotation.Label)
|
|
391
|
+
};
|
|
392
|
+
});
|
|
393
|
+
const bytesColumns = columnIndices.filter((_, i) => vectors[i]?.type === ValueType.Bytes);
|
|
394
|
+
const bytesNote = bytesColumns.length > 0 ? { note: `Columns ${bytesColumns.join(", ")} hold binary values; they are given by their size, \`{bytes: N}\`.` } : {};
|
|
395
|
+
const hasMore = offset + actualRows < totalRows;
|
|
396
|
+
if (transform) try {
|
|
397
|
+
const result = await safeEval(transform, {
|
|
398
|
+
rows,
|
|
399
|
+
columns: columnHeaders,
|
|
400
|
+
offset,
|
|
401
|
+
rowCount: actualRows,
|
|
402
|
+
totalRows
|
|
403
|
+
}, transformTimeout);
|
|
404
|
+
return textResult(result);
|
|
405
|
+
} catch (e) {
|
|
406
|
+
return transformFailedResult(e, "Check your JS expression syntax. Available variables: rows, columns, offset, rowCount, totalRows.");
|
|
407
|
+
}
|
|
408
|
+
return textResult({
|
|
409
|
+
...staleFlag,
|
|
410
|
+
offset,
|
|
411
|
+
rowCount: actualRows,
|
|
412
|
+
totalRows,
|
|
413
|
+
hasMore,
|
|
414
|
+
...hasMore ? { nextOffset: offset + actualRows } : {},
|
|
415
|
+
...bytesNote,
|
|
416
|
+
columns: columnHeaders,
|
|
417
|
+
rows
|
|
418
|
+
}, { hint: "Ask for fewer rows (`limit`) or columns (`columns`), or use `transform`." });
|
|
419
|
+
}
|
|
420
|
+
/** The most characters of the error text of one output named in `keys`; it is cut in the middle. */
|
|
421
|
+
const NAMED_OUTPUT_ERROR_CHARS = RESULT_CHAR_LIMIT / 2;
|
|
422
|
+
/**
|
|
423
|
+
* The outputs of a block for `get_block_outputs`: `{ calculationStatus, stale, note?, outputs }`,
|
|
424
|
+
* `outputs` output key → value, PFrame and PTable handles summed up (once per handle). An output
|
|
425
|
+
* that failed, or has no value, gives its `status` instead; a failed output named in `keys` also
|
|
426
|
+
* its error text, one not named a note saying how to read it. `note` says when the results are
|
|
427
|
+
* from an earlier configuration.
|
|
428
|
+
*/
|
|
429
|
+
async function blockOutputsResult(outputs, driver, options) {
|
|
430
|
+
const all = Object.keys(outputs);
|
|
431
|
+
if (options.keys !== void 0) {
|
|
432
|
+
const unknown = options.keys.filter((k) => !Object.hasOwn(outputs, k));
|
|
433
|
+
if (unknown.length > 0) return errorResult(`The block has no outputs ${unknown.join(", ")}.`, `Its outputs: ${all.join(", ") || "none"}.`);
|
|
434
|
+
}
|
|
435
|
+
const keys = options.keys ?? all;
|
|
436
|
+
const selected = Object.fromEntries(keys.map((k) => [k, outputs[k]]));
|
|
437
|
+
const summaries = new Map(summarizeOutputs(selected).map((s) => [s.key, s]));
|
|
438
|
+
const cache = /* @__PURE__ */ new Map();
|
|
439
|
+
const result = {};
|
|
440
|
+
for (const key of keys) {
|
|
441
|
+
const output = selected[key];
|
|
442
|
+
const summary = summaries.get(key);
|
|
443
|
+
if (summary === void 0 || !summary.hasValue) {
|
|
444
|
+
const status = summary?.status ?? "error";
|
|
445
|
+
const error = status === "error" ? outputErrorText(output) : void 0;
|
|
446
|
+
result[key] = error === void 0 ? { status } : options.keys !== void 0 ? {
|
|
447
|
+
status,
|
|
448
|
+
error: clipMiddle(error, NAMED_OUTPUT_ERROR_CHARS)
|
|
449
|
+
} : {
|
|
450
|
+
status,
|
|
451
|
+
note: failedOutputsNote(1, key)
|
|
452
|
+
};
|
|
453
|
+
continue;
|
|
454
|
+
}
|
|
455
|
+
result[key] = await resolveHandlesInValue(isPlainObject(output) ? output.value : void 0, driver, options.maxColumns, cache);
|
|
456
|
+
}
|
|
457
|
+
const { calculationStatus, stale } = options.block;
|
|
458
|
+
return textResult({
|
|
459
|
+
calculationStatus,
|
|
460
|
+
stale,
|
|
461
|
+
...calculationStatus === "Done" && stale ? { note: STALE_RESULTS_NOTE } : {},
|
|
462
|
+
outputs: dedupeHandleSummaries(result)
|
|
463
|
+
}, { hint: "Ask for fewer outputs (`keys`), a smaller `maxColumns`, or read one output with get_block_state `transform`, e.g. `outputs.<key>?.value`." });
|
|
464
|
+
}
|
|
465
|
+
function errorMessage(err) {
|
|
466
|
+
if (err instanceof Error) {
|
|
467
|
+
const cause = err.cause instanceof Error ? `: ${err.cause.message}` : "";
|
|
468
|
+
return `${err.message}${cause}`;
|
|
469
|
+
}
|
|
470
|
+
return String(err);
|
|
471
|
+
}
|
|
77
472
|
function registerDataQueryTools(server, ctx) {
|
|
473
|
+
const driver = () => ctx.requireMl().internalDriverKit.pFrameDriver;
|
|
78
474
|
server.registerTool("get_block_outputs", {
|
|
79
475
|
title: "Get Block Outputs",
|
|
80
476
|
annotations: hints.read,
|
|
81
|
-
description: "Get block output values
|
|
477
|
+
description: "Get block output values: `{ calculationStatus, stale, note?, outputs }`, `outputs` a JSON map, output key → value. `stale: true` with `calculationStatus` Done means the results are from an earlier configuration (`note` says so); run_block runs it again. PFrame/PTable handles are resolved inline to summaries with column specs (with `columnId`, for get_column_values); query_table counts rows; a handle already summed up earlier appears as `{ sameAs: \"<path>\" }`, the path to its summary in this result written as a JS accessor (`main.table`, `list[0]`). An output that failed or has no value gives `{ status }` instead (error or pending); a failed one named in `keys` adds its `error` text. Use this to discover block results and available data before querying tables.",
|
|
82
478
|
inputSchema: z.object({
|
|
83
479
|
projectId: projectIdInput.describe("Project ID"),
|
|
84
480
|
blockId: blockIdInput.describe("Block ID"),
|
|
481
|
+
keys: z.array(z.string()).optional().describe("Only these outputs (default: all). get_block_state lists the output keys."),
|
|
85
482
|
maxColumns: z.number().optional().default(30).describe("Max columns to show per PFrame/PTable summary (default 30).")
|
|
86
483
|
})
|
|
87
|
-
}, async ({ projectId, blockId, maxColumns }) => {
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
result[key] = await resolveHandlesInValue(output.value, driver, maxColumns, cache);
|
|
100
|
-
}
|
|
101
|
-
return textResult(result);
|
|
484
|
+
}, async ({ projectId, blockId, keys, maxColumns }) => {
|
|
485
|
+
return await ctx.withProject(projectId, async (project) => {
|
|
486
|
+
const { block, error } = await findBlock(project, projectId, blockId);
|
|
487
|
+
if (error) return error;
|
|
488
|
+
const state = await project.getBlockState(blockId).getValue();
|
|
489
|
+
if (!state.outputs) return errorResult("Block has no outputs yet.", "The block may not have been run. Use get_project_overview to check its calculationStatus, then run_block if needed.");
|
|
490
|
+
return await blockOutputsResult(state.outputs, driver(), {
|
|
491
|
+
block,
|
|
492
|
+
keys,
|
|
493
|
+
maxColumns
|
|
494
|
+
});
|
|
495
|
+
});
|
|
102
496
|
});
|
|
103
497
|
server.registerTool("list_columns", {
|
|
104
498
|
title: "List Columns",
|
|
105
499
|
annotations: hints.read,
|
|
106
|
-
description: "List
|
|
107
|
-
inputSchema: z.object({
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
500
|
+
description: "List the columns of a PFrame, a page at a time: `{ total, offset, hasMore, nextOffset?, columns: [{ columnId, name, valueType, label, axes }] }`. Filter by `name` (part of the name or label), `valueType` or `annotations`; `detail: \"full\"` adds domains, annotations and axis specs. `columnId` is the column's id as a block or table refers to it. Use get_block_outputs first to find the PFrame handle; get_column_values reads a column's values and their `valueIds` by its `columnId`.",
|
|
501
|
+
inputSchema: z.object({
|
|
502
|
+
projectId: projectIdInput.describe("Project the handle came from"),
|
|
503
|
+
pFrameHandle: pFrameHandleInput.describe("PFrame handle (64-char hex hash from get_block_outputs)"),
|
|
504
|
+
name: z.string().optional().describe("Only columns whose name or label contains this (case-insensitive)."),
|
|
505
|
+
valueType: z.string().optional().describe("Only columns of this value type: Int, Long, Float, Double, String…"),
|
|
506
|
+
annotations: z.record(z.string(), z.string()).optional().describe("Only columns with these annotation values, e.g. `{\"pl7.app/isAnchor\": \"true\"}`."),
|
|
507
|
+
detail: z.enum(["brief", "full"]).optional().describe("`brief` (default): ids, names, labels, axis names; `full`: whole specs."),
|
|
508
|
+
offset: z.number().optional().describe("Columns to skip (default 0)."),
|
|
509
|
+
limit: z.number().optional().describe(`Columns per page (default 100, at most ${LIST_COLUMNS_MAX}).`)
|
|
510
|
+
})
|
|
511
|
+
}, async ({ projectId, pFrameHandle, ...options }) => {
|
|
512
|
+
return await ctx.withProject(projectId, () => listColumnsResult(driver(), pFrameHandle, options));
|
|
513
|
+
});
|
|
514
|
+
server.registerTool("get_column_values", {
|
|
515
|
+
title: "Get Column Values",
|
|
516
|
+
annotations: hints.read,
|
|
517
|
+
description: "Get the distinct values of a column of a PFrame, or of one of its axes: the choices a block offers for a value of its data (a sample, a chain, a gene…). Result: `{ columnId, axis?, valueType, count, overflow, naDropped?, values, valueIds? }`; `valueIds` holds the ColumnValueId of each value, in the same order: copy it into a block that picks values. `valueIds` come only for a block's export itself, the columns a block can use; a column local to one block, a filtered, overridden or discovered column, or an axis, gives none. `overflow: true` means there are more than `limit` values. Missing values (NA) are left out. Column ids come from list_columns or the PFrame summaries of get_block_outputs; values of a pool column come by its `columnId` from list_pool_columns, without `pFrameHandle`.",
|
|
518
|
+
inputSchema: z.object({
|
|
519
|
+
projectId: projectIdInput.describe("Project the handle came from, or of the result-pool column."),
|
|
520
|
+
pFrameHandle: pFrameHandleInput.optional().describe("PFrame handle (64-char hex hash from get_block_outputs); omit for a pool column."),
|
|
521
|
+
columnId: frameColumnIdInput.describe("Column id, from list_columns, or from list_pool_columns without `pFrameHandle`."),
|
|
522
|
+
axis: z.union([z.number(), z.string()]).optional().describe("An axis of the column, by index or name: its values instead of the column's."),
|
|
523
|
+
search: z.string().optional().describe("Only values that contain this text (case-insensitive; string values only)."),
|
|
524
|
+
limit: z.number().optional().describe(`The most values to return (default 100, at most ${COLUMN_VALUES_MAX}).`)
|
|
525
|
+
})
|
|
526
|
+
}, async ({ pFrameHandle, projectId, ...input }, call) => {
|
|
527
|
+
const signal = readSignal(call?.mcpReq.signal);
|
|
528
|
+
return await ctx.withProject(projectId, (project) => pFrameHandle === void 0 ? poolColumnValuesResult(project, projectId, driver(), input, signal) : columnValuesResult(driver(), {
|
|
529
|
+
...input,
|
|
530
|
+
pFrameHandle
|
|
531
|
+
}, signal));
|
|
122
532
|
});
|
|
123
533
|
server.registerTool("query_table", {
|
|
124
534
|
title: "Query Table",
|
|
125
535
|
annotations: hints.read,
|
|
126
|
-
description:
|
|
536
|
+
description: `Query data from a PTable. Returns rows as arrays of values, with \`columns: [{ index, type, columnId?, name, label }]\` (\`columnId\` on columns, not axes), \`totalRows\`, \`hasMore\` and \`nextOffset\` for the next page. At most ${QUERY_MAX_ROWS} rows per call. The handle comes from get_block_outputs, or from create_table for a table of your own; for such a table \`stale: true\` says its source columns changed since it was built (not given with \`transform\`). Use \`transform\` to process results server-side and return only what you need.`,
|
|
127
537
|
inputSchema: z.object({
|
|
128
|
-
|
|
129
|
-
|
|
538
|
+
projectId: projectIdInput.describe("Project the handle came from"),
|
|
539
|
+
pTableHandle: pTableHandleInput.describe("PTable handle (64-char hex hash from get_block_outputs or create_table)"),
|
|
540
|
+
columns: z.array(z.number()).optional().describe("Column indices to retrieve (default: all; not empty). get_block_outputs and create_table list column indices. Binary columns give each value's size, `{bytes: N}`."),
|
|
130
541
|
offset: z.number().optional().default(0).describe("Row offset (default 0)"),
|
|
131
|
-
limit: z.number().optional().default(50).describe(
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
transformTimeout: z.number().optional().default(5e3).describe("Timeout in ms for transform evaluation (default 5000).")
|
|
542
|
+
limit: z.number().optional().default(50).describe(`Number of rows to return (default 50, at most ${QUERY_MAX_ROWS}).`),
|
|
543
|
+
transform: z.string().optional().describe("JS expression evaluated server-side against query results. Available variables: `rows` (array of row arrays), `columns` (column headers), `offset`, `rowCount`, `totalRows`. Example: `rows.map(r => r[0])` — extract first column only."),
|
|
544
|
+
transformTimeout: z.number().optional().default(5e3).describe(expressionTimeoutText("transform evaluation", 5e3))
|
|
135
545
|
})
|
|
136
|
-
}, async (
|
|
137
|
-
const
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
try {
|
|
141
|
-
spec = await pFrameDriver.getSpec(handle);
|
|
142
|
-
} catch (err) {
|
|
143
|
-
return textResult({ error: `getSpec failed: ${err}` });
|
|
144
|
-
}
|
|
145
|
-
const range = {
|
|
146
|
-
offset,
|
|
147
|
-
length: Math.min(limit, maxLimit)
|
|
148
|
-
};
|
|
149
|
-
const columnIndices = columns ?? spec.map((_, i) => i);
|
|
150
|
-
let vectors;
|
|
151
|
-
try {
|
|
152
|
-
vectors = await pFrameDriver.getData(handle, columnIndices, range);
|
|
153
|
-
} catch (err) {
|
|
154
|
-
return textResult({
|
|
155
|
-
error: `getData failed: ${err}`,
|
|
156
|
-
columnIndices,
|
|
157
|
-
range
|
|
158
|
-
});
|
|
159
|
-
}
|
|
160
|
-
const actualRows = vectors.length > 0 ? vectors[0].data.length : 0;
|
|
161
|
-
const columnVectors = vectors.map((v) => vectorToJson(v, actualRows));
|
|
162
|
-
const rows = [];
|
|
163
|
-
for (let r = 0; r < actualRows; r++) rows.push(columnVectors.map((col) => col[r]));
|
|
164
|
-
const columnHeaders = columnIndices.map((idx) => {
|
|
165
|
-
const s = spec[idx];
|
|
166
|
-
return {
|
|
167
|
-
index: idx,
|
|
168
|
-
type: s.type,
|
|
169
|
-
name: s.type === "column" ? s.spec.name : s.spec.name,
|
|
170
|
-
label: readAnnotation(s.spec, Annotation.Label)
|
|
171
|
-
};
|
|
172
|
-
});
|
|
173
|
-
if (transform) try {
|
|
174
|
-
const result = await safeEval(transform, {
|
|
175
|
-
rows,
|
|
176
|
-
columns: columnHeaders,
|
|
177
|
-
offset,
|
|
178
|
-
rowCount: actualRows
|
|
179
|
-
}, transformTimeout);
|
|
180
|
-
return textResult(result);
|
|
181
|
-
} catch (e) {
|
|
182
|
-
return transformFailedResult(e, "Check your JS expression syntax. Available variables: rows, columns, offset, rowCount.");
|
|
183
|
-
}
|
|
184
|
-
return textResult({
|
|
185
|
-
offset,
|
|
186
|
-
rowCount: actualRows,
|
|
187
|
-
columns: columnHeaders,
|
|
188
|
-
rows
|
|
189
|
-
});
|
|
190
|
-
});
|
|
546
|
+
}, async (input, call) => await ctx.withProject(input.projectId, async (_project, ml) => {
|
|
547
|
+
const table = ctx.agentTables.use(input.pTableHandle);
|
|
548
|
+
return await queryTableResult(ml.internalDriverKit.pFrameDriver, input, readSignal(call?.mcpReq.signal), table === void 0 ? void 0 : { stale: table.stale });
|
|
549
|
+
}));
|
|
191
550
|
}
|
|
192
|
-
export { registerDataQueryTools };
|
|
551
|
+
export { COLUMN_VALUES_MAX, LIST_COLUMNS_MAX, NAMED_OUTPUT_ERROR_CHARS, QUERY_MAX_ROWS, blockOutputsResult, columnValuesResult, dedupeHandleSummaries, listColumnsResult, poolColumnValuesResult, queryTableResult, registerDataQueryTools };
|
|
193
552
|
|
|
194
553
|
//# sourceMappingURL=data-query.js.map
|