@adapttable/ai-react 0.2.5 → 0.2.6
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/CHANGELOG.md +15 -0
- package/dist/index.cjs +21 -653
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +8 -53
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.ts +8 -53
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +11 -643
- package/dist/index.js.map +1 -1
- package/package.json +5 -5
package/dist/index.js
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import {
|
|
3
|
-
import { AGENT_ALWAYS_ALLOW_STATE, AGENT_APPROVAL_STATE, AGENT_PROGRESS_STATE, AGENT_VIEW_STATE, FeatureStateScope,
|
|
2
|
+
import { alwaysAllowFor, assertAlwaysAllow, bindLiveSession, capabilityKind, closeTransaction, contractFingerprint, createApprovalMemory, createRevisionCounter, createTableAssistant, displayProposals, exclusionKey, mayAlwaysAllow, openTransaction, perItemRefusal, readerResolver, recordDecision, registerWebMcpTools, revisionToken, sampledColumns, settleDecisions, sharedApproval, tableActionSignature, viewInputsFromRuntime, viewRevisionStamp } from "@adapttable/ai";
|
|
3
|
+
import { AGENT_ALWAYS_ALLOW_STATE, AGENT_APPROVAL_STATE, AGENT_PROGRESS_STATE, AGENT_VIEW_STATE, FeatureStateScope, featureStateKey, useFeatureState, useTableRuntime } from "@adapttable/react/adapter";
|
|
4
4
|
import { useCallback, useDebugValue, useEffect, useLayoutEffect, useMemo, useRef, useState, useSyncExternalStore } from "react";
|
|
5
5
|
import { sampleColumns } from "@adapttable/ai/context";
|
|
6
|
-
import { pageSizeOptions } from "@adapttable/core";
|
|
7
6
|
import { flushSync } from "react-dom";
|
|
8
7
|
import { jsx } from "react/jsx-runtime";
|
|
9
8
|
import { createSpeechInput, readRememberedLanguage, rememberLanguage } from "@adapttable/ai/voice";
|
|
@@ -131,651 +130,12 @@ function inputsOf(options, approval, alwaysAllow, viewState, progress) {
|
|
|
131
130
|
* `@adapttable/ai` stays React-free. This package mounts a provider that
|
|
132
131
|
* observes the live table and publishes a versioned manifest.
|
|
133
132
|
*/
|
|
134
|
-
const PAGE_ONLY_SOURCE = {
|
|
135
|
-
fullDataset: false,
|
|
136
|
-
grouping: false,
|
|
137
|
-
selectAcrossPages: false,
|
|
138
|
-
exportScope: "page",
|
|
139
|
-
totalCount: "loaded"
|
|
140
|
-
};
|
|
141
133
|
/**
|
|
142
134
|
* Feature-state key for the live {@link AgentSession}.
|
|
143
135
|
*
|
|
144
136
|
* @public
|
|
145
137
|
*/
|
|
146
138
|
const TABLE_AGENT_STATE = featureStateKey("table-agent");
|
|
147
|
-
function mergeColumn(base, patch) {
|
|
148
|
-
const extra = patch?.[base.id];
|
|
149
|
-
if (!extra) return base;
|
|
150
|
-
return {
|
|
151
|
-
...base,
|
|
152
|
-
...extra
|
|
153
|
-
};
|
|
154
|
-
}
|
|
155
|
-
function cellRecord(row, columns, wanted) {
|
|
156
|
-
const record = row && typeof row === "object" ? row : {};
|
|
157
|
-
const cells = {};
|
|
158
|
-
for (const column of columns) {
|
|
159
|
-
if (!column.readable) continue;
|
|
160
|
-
if (wanted && !wanted.includes(column.id)) continue;
|
|
161
|
-
cells[column.id] = record[column.id];
|
|
162
|
-
}
|
|
163
|
-
return cells;
|
|
164
|
-
}
|
|
165
|
-
/**
|
|
166
|
-
* `JSON.stringify` that never throws on host rows: a BigInt is written as its
|
|
167
|
-
* digits with an `n`, and a value that contains itself is written once, not
|
|
168
|
-
* followed back in.
|
|
169
|
-
*/
|
|
170
|
-
function stampJson(value) {
|
|
171
|
-
const ancestors = [];
|
|
172
|
-
return JSON.stringify(value, function replace(_key, next) {
|
|
173
|
-
if (typeof next === "bigint") return `${next.toString()}n`;
|
|
174
|
-
if (typeof next !== "object" || next === null) return next;
|
|
175
|
-
while (ancestors.length > 0 && ancestors.at(-1) !== this) ancestors.pop();
|
|
176
|
-
if (ancestors.includes(next)) return void 0;
|
|
177
|
-
ancestors.push(next);
|
|
178
|
-
return next;
|
|
179
|
-
});
|
|
180
|
-
}
|
|
181
|
-
function viewRevisionStamp(view) {
|
|
182
|
-
const table = view?.neutralTable;
|
|
183
|
-
if (table) return monotonicRevision(table.revisions, void 0).token;
|
|
184
|
-
const rows = view?.rows ?? [];
|
|
185
|
-
const getRowId = view?.getRowId;
|
|
186
|
-
const query = view?.query;
|
|
187
|
-
return stampJson({
|
|
188
|
-
ids: rows.map((row) => getRowId ? getRowId(row) : null),
|
|
189
|
-
payloads: rows,
|
|
190
|
-
page: query?.page ?? 1,
|
|
191
|
-
limit: query?.limit ?? 10,
|
|
192
|
-
search: query?.search ?? "",
|
|
193
|
-
sortBy: query?.sortBy,
|
|
194
|
-
sortDir: query?.sortDir,
|
|
195
|
-
filters: query?.extra,
|
|
196
|
-
groupBy: view?.groupingState?.groupBy,
|
|
197
|
-
aggregateOverrides: view?.groupingState?.aggregateOverrides,
|
|
198
|
-
pinnedColumns: view?.pinning?.columns,
|
|
199
|
-
pinnedRows: view?.pinning?.rows,
|
|
200
|
-
hiddenColumns: view?.columnLayout?.hidden,
|
|
201
|
-
columnOrder: view?.columnLayout?.keys
|
|
202
|
-
});
|
|
203
|
-
}
|
|
204
|
-
function liveReadRows(runtime, columns, query, readMax) {
|
|
205
|
-
const table = runtime.view()?.neutralTable;
|
|
206
|
-
if (table) return readRowsFromNeutral(table, columns, query, readMax);
|
|
207
|
-
const view = runtime.view();
|
|
208
|
-
const rows = (query.scope ?? "visible") === "visible" ? view?.visibleRows ?? view?.rows ?? [] : view?.rows ?? [];
|
|
209
|
-
const getRowId = view?.getRowId ?? (() => "");
|
|
210
|
-
const hidden = columns.filter((column) => !column.readable).map((c) => c.id);
|
|
211
|
-
const limit = Math.max(0, Math.min(query.limit, readMax));
|
|
212
|
-
return {
|
|
213
|
-
rows: rows.slice(query.offset, query.offset + limit).map((row) => ({
|
|
214
|
-
rowKey: getRowId(row),
|
|
215
|
-
cells: cellRecord(row, columns, query.columns)
|
|
216
|
-
})),
|
|
217
|
-
offset: query.offset,
|
|
218
|
-
limit,
|
|
219
|
-
redacted: hidden
|
|
220
|
-
};
|
|
221
|
-
}
|
|
222
|
-
function liveResolveRow(runtime, ref, scope) {
|
|
223
|
-
const table = runtime.view()?.neutralTable;
|
|
224
|
-
if (table) return resolveRowFromNeutral(table, ref);
|
|
225
|
-
const view = runtime.view();
|
|
226
|
-
const rows = scope === "visible" ? view?.visibleRows ?? view?.rows ?? [] : view?.rows ?? [];
|
|
227
|
-
const getRowId = view?.getRowId ?? (() => "");
|
|
228
|
-
if ("rowKey" in ref) return {
|
|
229
|
-
rowKey: ref.rowKey,
|
|
230
|
-
scope
|
|
231
|
-
};
|
|
232
|
-
const row = rows[ref.position - 1];
|
|
233
|
-
if (!row) throw new Error(`no row at 1-based position ${ref.position}`);
|
|
234
|
-
return {
|
|
235
|
-
rowKey: getRowId(row),
|
|
236
|
-
scope: ref.scope,
|
|
237
|
-
position: ref.position
|
|
238
|
-
};
|
|
239
|
-
}
|
|
240
|
-
function findRow(runtime, rowKey) {
|
|
241
|
-
const table = runtime.view()?.neutralTable;
|
|
242
|
-
if (table) return table.rowByKey(rowKey);
|
|
243
|
-
const view = runtime.view();
|
|
244
|
-
const rows = view?.rows ?? [];
|
|
245
|
-
const getRowId = view?.getRowId;
|
|
246
|
-
if (!getRowId) return void 0;
|
|
247
|
-
return rows.find((row) => getRowId(row) === rowKey);
|
|
248
|
-
}
|
|
249
|
-
function stampColumnLayout(columns, layout) {
|
|
250
|
-
if (!layout) return columns;
|
|
251
|
-
const byId = new Map(columns.map((column) => [column.id, column]));
|
|
252
|
-
const hidden = new Set(layout.hidden);
|
|
253
|
-
return (layout.keys.length > 0 ? layout.keys : columns.map((c) => c.id)).map((id) => {
|
|
254
|
-
const existing = byId.get(id);
|
|
255
|
-
const visible = !hidden.has(id);
|
|
256
|
-
if (existing) return {
|
|
257
|
-
...existing,
|
|
258
|
-
visible
|
|
259
|
-
};
|
|
260
|
-
return {
|
|
261
|
-
id,
|
|
262
|
-
label: id,
|
|
263
|
-
type: "unknown",
|
|
264
|
-
readable: true,
|
|
265
|
-
writable: false,
|
|
266
|
-
sortable: false,
|
|
267
|
-
visible
|
|
268
|
-
};
|
|
269
|
-
});
|
|
270
|
-
}
|
|
271
|
-
function columnsForRuntime(options, runtime) {
|
|
272
|
-
const table = runtime.view()?.neutralTable;
|
|
273
|
-
if (table) return agentColumnsFromNeutral(table, options.columns);
|
|
274
|
-
if (!options.columns) return [];
|
|
275
|
-
return Object.entries(options.columns).map(([id, extra]) => mergeColumn({
|
|
276
|
-
id,
|
|
277
|
-
label: extra.label ?? id,
|
|
278
|
-
type: extra.type ?? "unknown",
|
|
279
|
-
readable: extra.readable ?? true,
|
|
280
|
-
writable: extra.writable ?? false,
|
|
281
|
-
sortable: extra.sortable ?? false
|
|
282
|
-
}, options.columns));
|
|
283
|
-
}
|
|
284
|
-
function observationPolicy(options) {
|
|
285
|
-
const approval = sharedApproval(options.approval);
|
|
286
|
-
return {
|
|
287
|
-
...options.writePolicy ? { writePolicy: options.writePolicy } : {},
|
|
288
|
-
approval: approval.policy,
|
|
289
|
-
...approval.presentation ? { presentation: approval.presentation } : {},
|
|
290
|
-
...options.commit ? { commit: options.commit } : {}
|
|
291
|
-
};
|
|
292
|
-
}
|
|
293
|
-
function observationFromRuntime(options, runtime, revision, apply) {
|
|
294
|
-
const view = runtime.view();
|
|
295
|
-
const table = view?.neutralTable;
|
|
296
|
-
const query = view?.query;
|
|
297
|
-
const ids = runtime.featureIds();
|
|
298
|
-
const pages = serverPagination(view, query);
|
|
299
|
-
if (table) {
|
|
300
|
-
const live = observationFromNeutral(table, options, revision, apply, ids, query ? {
|
|
301
|
-
page: query.page,
|
|
302
|
-
limit: query.limit,
|
|
303
|
-
search: query.search,
|
|
304
|
-
sortBy: query.sortBy,
|
|
305
|
-
sortDir: query.sortDir,
|
|
306
|
-
pinnedColumns: view?.pinning?.columns,
|
|
307
|
-
pinnedRows: view?.pinning?.rows,
|
|
308
|
-
hiddenColumns: view?.columnLayout?.hidden,
|
|
309
|
-
columnOrder: view?.columnLayout?.keys
|
|
310
|
-
} : void 0);
|
|
311
|
-
const sizes = offeredPageSizes(query);
|
|
312
|
-
return {
|
|
313
|
-
...live,
|
|
314
|
-
columns: stampColumnLayout(live.columns, view?.columnLayout),
|
|
315
|
-
pagination: live.pagination ? {
|
|
316
|
-
...live.pagination,
|
|
317
|
-
pageSizeOptions: sizes
|
|
318
|
-
} : {
|
|
319
|
-
...pages,
|
|
320
|
-
pageSizeOptions: sizes
|
|
321
|
-
},
|
|
322
|
-
groupBy: view?.groupingState?.groupBy,
|
|
323
|
-
aggregations: aggregationsFor(aggregationInputs(view, options)),
|
|
324
|
-
availableFilters: agentFiltersFromDefs(view?.filterDefs, view?.filterRegistry, options.columns),
|
|
325
|
-
filters: query?.extra
|
|
326
|
-
};
|
|
327
|
-
}
|
|
328
|
-
const columns = stampColumnLayout(columnsForRuntime(options, runtime).map((column) => mergeColumn(column, options.columns)), view?.columnLayout);
|
|
329
|
-
return agentObservation({
|
|
330
|
-
tableId: options.tableId,
|
|
331
|
-
viewRevision: revision,
|
|
332
|
-
featureIds: ids,
|
|
333
|
-
columns,
|
|
334
|
-
source: view?.sourceCapabilities ?? PAGE_ONLY_SOURCE,
|
|
335
|
-
operations: {
|
|
336
|
-
...deriveRuntimeOperations(view),
|
|
337
|
-
applyView: apply.applyView !== void 0
|
|
338
|
-
},
|
|
339
|
-
apply: options.apply ?? {},
|
|
340
|
-
policy: observationPolicy(options),
|
|
341
|
-
view: {
|
|
342
|
-
search: query?.search ?? "",
|
|
343
|
-
...query?.sortBy === void 0 ? {} : { sortBy: query.sortBy },
|
|
344
|
-
...query?.sortDir === void 0 ? {} : { sortDir: query.sortDir },
|
|
345
|
-
...view?.groupingState?.groupBy === void 0 ? {} : { groupBy: view.groupingState.groupBy },
|
|
346
|
-
...view?.pinning?.columns ? { pinnedColumns: view.pinning.columns } : {},
|
|
347
|
-
...view?.pinning?.rows ? { pinnedRows: view.pinning.rows } : {},
|
|
348
|
-
...view?.columnLayout?.hidden ? { hiddenColumns: view.columnLayout.hidden } : {},
|
|
349
|
-
...view?.columnLayout?.keys ? { columnOrder: view.columnLayout.keys } : {},
|
|
350
|
-
...query?.extra === void 0 ? {} : { filters: query.extra }
|
|
351
|
-
},
|
|
352
|
-
pagination: pages,
|
|
353
|
-
rowAddressScope: "visible",
|
|
354
|
-
readMax: options.readMax ?? 50,
|
|
355
|
-
aggregations: aggregationsFor(aggregationInputs(view, options)),
|
|
356
|
-
availableFilters: agentFiltersFromDefs(view?.filterDefs, view?.filterRegistry, options.columns)
|
|
357
|
-
});
|
|
358
|
-
}
|
|
359
|
-
/**
|
|
360
|
-
* What a server-backed table's pages are, from what the source will say.
|
|
361
|
-
*
|
|
362
|
-
* The total is the server's count for the *current query* — the figure the
|
|
363
|
-
* table's own pagination is computed from — and it is published only when the
|
|
364
|
-
* source claims to have counted. A source that has not counted keeps an
|
|
365
|
-
* unknown total rather than being handed the rows that happen to be loaded,
|
|
366
|
-
* and a short page is the one thing it still proves about its own end.
|
|
367
|
-
*/
|
|
368
|
-
/** The sizes the table's own rows-per-page control lists. */
|
|
369
|
-
function offeredPageSizes(query) {
|
|
370
|
-
const limit = query?.limit ?? 10;
|
|
371
|
-
const published = query?.defaultLimit ?? limit;
|
|
372
|
-
return pageSizeOptions([limit, published]);
|
|
373
|
-
}
|
|
374
|
-
function serverPagination(view, query) {
|
|
375
|
-
const counted = (view?.sourceCapabilities ?? PAGE_ONLY_SOURCE).totalCount === "exact";
|
|
376
|
-
const total = query?.total;
|
|
377
|
-
const loaded = view?.rows.length;
|
|
378
|
-
return agentPagination({
|
|
379
|
-
page: query?.page ?? 1,
|
|
380
|
-
pageSize: query?.limit ?? 10,
|
|
381
|
-
pageSizeOptions: offeredPageSizes(query),
|
|
382
|
-
...counted && total !== void 0 ? { totalRows: total } : {},
|
|
383
|
-
...loaded === void 0 ? {} : { loadedRows: loaded },
|
|
384
|
-
canJump: query?.setPage !== void 0
|
|
385
|
-
});
|
|
386
|
-
}
|
|
387
|
-
function liveEditCells(runtime, extra, edits) {
|
|
388
|
-
if (extra?.editCells) return extra.editCells(edits);
|
|
389
|
-
const onCellEdit = runtime.view()?.editing?.onCellEdit;
|
|
390
|
-
if (!onCellEdit) throw new Error("editCells is not wired");
|
|
391
|
-
for (const edit of edits) {
|
|
392
|
-
const row = findRow(runtime, edit.rowKey);
|
|
393
|
-
if (!row) throw new Error(`row "${edit.rowKey}" is not in the current view`);
|
|
394
|
-
onCellEdit(row, edit.column, edit.value);
|
|
395
|
-
}
|
|
396
|
-
}
|
|
397
|
-
function liveStageCells(runtime, extra, edits) {
|
|
398
|
-
if (extra?.stageCells) return extra.stageCells(edits);
|
|
399
|
-
const stage = runtime.view()?.editing?.stageCell;
|
|
400
|
-
if (!stage) throw new Error("stageCells is not wired");
|
|
401
|
-
for (const edit of edits) {
|
|
402
|
-
const row = findRow(runtime, edit.rowKey);
|
|
403
|
-
if (!row) throw new Error(`row "${edit.rowKey}" is not in the current view`);
|
|
404
|
-
stage(row, edit.rowKey, edit.column, typeof edit.value === "string" ? edit.value : String(edit.value));
|
|
405
|
-
}
|
|
406
|
-
}
|
|
407
|
-
function liveSetSelection(runtime, extra, ids) {
|
|
408
|
-
extra?.setSelection?.(ids);
|
|
409
|
-
if (!extra?.setSelection) runtime.view()?.selection?.replace(ids);
|
|
410
|
-
}
|
|
411
|
-
function rowScope(ref) {
|
|
412
|
-
return "scope" in ref ? ref.scope : "visible";
|
|
413
|
-
}
|
|
414
|
-
function pickReadRows(runtime, extra, columns, readMax) {
|
|
415
|
-
return (query) => {
|
|
416
|
-
if (extra?.readRows) return extra.readRows(query);
|
|
417
|
-
return liveReadRows(runtime, columns, query, readMax);
|
|
418
|
-
};
|
|
419
|
-
}
|
|
420
|
-
function pickResolveRow(runtime, extra) {
|
|
421
|
-
return (ref) => {
|
|
422
|
-
if (extra?.resolveRow) return extra.resolveRow(ref);
|
|
423
|
-
return liveResolveRow(runtime, ref, rowScope(ref));
|
|
424
|
-
};
|
|
425
|
-
}
|
|
426
|
-
function liveQueryFilters(query) {
|
|
427
|
-
return query ? query : {};
|
|
428
|
-
}
|
|
429
|
-
function applyLiveFilters(query, filters) {
|
|
430
|
-
const live = liveQueryFilters(query);
|
|
431
|
-
if (!live.setExtras && !live.clearExtras) return false;
|
|
432
|
-
if (filters == null) {
|
|
433
|
-
live.clearExtras?.();
|
|
434
|
-
return true;
|
|
435
|
-
}
|
|
436
|
-
if (typeof filters !== "object" || Array.isArray(filters)) throw new TypeError("setFilters requires a filter object");
|
|
437
|
-
if (Object.keys(filters).length === 0) {
|
|
438
|
-
live.clearExtras?.();
|
|
439
|
-
return true;
|
|
440
|
-
}
|
|
441
|
-
live.setExtras?.(filters);
|
|
442
|
-
return true;
|
|
443
|
-
}
|
|
444
|
-
function requireQuery(view, name) {
|
|
445
|
-
const fn = view()?.query?.[name];
|
|
446
|
-
if (!fn) throw new Error(`${name} is not wired`);
|
|
447
|
-
return fn;
|
|
448
|
-
}
|
|
449
|
-
/**
|
|
450
|
-
* What the LIVE TABLE can do, before the host's own callbacks are laid over
|
|
451
|
-
* it.
|
|
452
|
-
*
|
|
453
|
-
* `currentApply` builds `{ ...applyFromRuntime(...), ...options.apply }`, so
|
|
454
|
-
* a callback the host supplied is the one that runs. A guard here that
|
|
455
|
-
* checked for the same callback would be shadowed by that spread and could
|
|
456
|
-
* never fire, which is why the view operations below reach straight for the
|
|
457
|
-
* live table. `extra` still travels to the row, edit, stage and selection
|
|
458
|
-
* helpers, which read it for their own reasons.
|
|
459
|
-
*/
|
|
460
|
-
function applyFromRuntime(runtime, options, extra) {
|
|
461
|
-
const columns = columnsForRuntime(options, runtime);
|
|
462
|
-
const readMax = options.readMax ?? 50;
|
|
463
|
-
const view = () => runtime.view();
|
|
464
|
-
return {
|
|
465
|
-
readRows: pickReadRows(runtime, extra, columns, readMax),
|
|
466
|
-
resolveRow: pickResolveRow(runtime, extra),
|
|
467
|
-
setPage: (page) => {
|
|
468
|
-
requireQuery(view, "setPage")(page);
|
|
469
|
-
},
|
|
470
|
-
setLimit: (limit) => {
|
|
471
|
-
requireQuery(view, "setLimit")(limit);
|
|
472
|
-
},
|
|
473
|
-
setSearch: (search) => {
|
|
474
|
-
requireQuery(view, "setSearch")(search);
|
|
475
|
-
},
|
|
476
|
-
setSort: (key, dir) => {
|
|
477
|
-
requireQuery(view, "setSort")(key, dir);
|
|
478
|
-
},
|
|
479
|
-
setGroupBy: (key) => {
|
|
480
|
-
const grouping = view()?.groupingState;
|
|
481
|
-
if (!grouping) throw new Error("setGroupBy is not wired");
|
|
482
|
-
grouping.setGroupBy(key);
|
|
483
|
-
},
|
|
484
|
-
setAggregations: (patch) => {
|
|
485
|
-
applyAggregations(aggregationInputs(view(), options), patch);
|
|
486
|
-
},
|
|
487
|
-
pinColumn: (key, side) => {
|
|
488
|
-
const pinning = view()?.pinning;
|
|
489
|
-
if (!pinning?.setColumnPin) throw new Error("pinColumn is not wired");
|
|
490
|
-
pinning.setColumnPin(key, side);
|
|
491
|
-
},
|
|
492
|
-
hideColumn: (key, hidden) => {
|
|
493
|
-
const layout = view()?.columnLayout;
|
|
494
|
-
if (!layout?.setHidden) throw new Error("hideColumn is not wired");
|
|
495
|
-
layout.setHidden(key, hidden);
|
|
496
|
-
},
|
|
497
|
-
moveColumn: (key, toIndex) => {
|
|
498
|
-
const layout = view()?.columnLayout;
|
|
499
|
-
if (!layout?.move) throw new Error("moveColumn is not wired");
|
|
500
|
-
layout.move(key, toIndex);
|
|
501
|
-
},
|
|
502
|
-
setColumnOrder: (order) => {
|
|
503
|
-
const layout = view()?.columnLayout;
|
|
504
|
-
if (!layout?.setOrder) throw new Error("setColumnOrder is not wired");
|
|
505
|
-
layout.setOrder(order);
|
|
506
|
-
},
|
|
507
|
-
pinRow: (rowKey, side) => {
|
|
508
|
-
const pinning = view()?.pinning;
|
|
509
|
-
if (!pinning?.setRowPin) throw new Error("pinRow is not wired");
|
|
510
|
-
pinning.setRowPin(rowKey, side);
|
|
511
|
-
},
|
|
512
|
-
editCells: (edits) => liveEditCells(runtime, extra, edits),
|
|
513
|
-
stageCells: (edits) => liveStageCells(runtime, extra, edits),
|
|
514
|
-
setSelection: (ids) => liveSetSelection(runtime, extra, ids)
|
|
515
|
-
};
|
|
516
|
-
}
|
|
517
|
-
/**
|
|
518
|
-
* The live view and filter catalog, in the shape the context builder takes.
|
|
519
|
-
*
|
|
520
|
-
* The same values the observation carries, read straight off the runtime so
|
|
521
|
-
* the caller does not have to rebuild an observation to get at them. What the
|
|
522
|
-
* table cannot answer for is simply absent, and the sanitized view names the
|
|
523
|
-
* gap rather than defaulting it.
|
|
524
|
-
*/
|
|
525
|
-
/**
|
|
526
|
-
* Column ids whose author asked for live values.
|
|
527
|
-
*
|
|
528
|
-
* The flag is the author's, so it is read from the contract rather than from
|
|
529
|
-
* anything a reader can move. A column the agent may not read is not sampled
|
|
530
|
-
* whatever it says — the sampler refuses it too, and asking twice is cheaper
|
|
531
|
-
* than a read that comes back empty.
|
|
532
|
-
*/
|
|
533
|
-
function sampledColumns(session) {
|
|
534
|
-
return session.manifest().columns.filter((column) => column.readable && column.ai?.sample === true).map((column) => column.id);
|
|
535
|
-
}
|
|
536
|
-
function viewInputsFromRuntime(runtime, options, samples) {
|
|
537
|
-
const view = runtime.view();
|
|
538
|
-
const query = view?.query;
|
|
539
|
-
const filters = agentFiltersFromDefs(view?.filterDefs, view?.filterRegistry, options.columns);
|
|
540
|
-
const aggregations = aggregationsFor(aggregationInputs(view, options));
|
|
541
|
-
return {
|
|
542
|
-
...filters && filters.length > 0 ? { filters } : {},
|
|
543
|
-
...aggregations ? { aggregations } : {},
|
|
544
|
-
...Object.keys(samples).length > 0 ? { samples } : {},
|
|
545
|
-
view: {
|
|
546
|
-
...query?.page === void 0 ? {} : { page: query.page },
|
|
547
|
-
...query?.limit === void 0 ? {} : { limit: query.limit },
|
|
548
|
-
...query?.search === void 0 ? {} : { search: query.search },
|
|
549
|
-
...query?.sortBy ? { sortBy: query.sortBy } : {},
|
|
550
|
-
...query?.sortDir ? { sortDir: query.sortDir } : {},
|
|
551
|
-
...view?.groupingState?.groupBy ? { groupBy: view.groupingState.groupBy } : {},
|
|
552
|
-
...query?.extra ? { filters: query.extra } : {},
|
|
553
|
-
...view?.pinning?.columns ? { pinnedColumns: view.pinning.columns } : {},
|
|
554
|
-
...view?.pinning?.rows ? { pinnedRows: view.pinning.rows } : {},
|
|
555
|
-
...view?.columnLayout?.hidden ? { hiddenColumns: view.columnLayout.hidden } : {},
|
|
556
|
-
...view?.columnLayout?.keys ? { columnOrder: view.columnLayout.keys } : {}
|
|
557
|
-
}
|
|
558
|
-
};
|
|
559
|
-
}
|
|
560
|
-
/**
|
|
561
|
-
* A row-by-row refusal, with the reader's stated reason when they gave one.
|
|
562
|
-
*
|
|
563
|
-
* The reason travels with the refusal so the receipt and the model-visible
|
|
564
|
-
* result can say why rather than only that.
|
|
565
|
-
*/
|
|
566
|
-
function perItemRefusal(transaction, stated) {
|
|
567
|
-
return {
|
|
568
|
-
...settleDecisions(transaction.decisions, "rejected"),
|
|
569
|
-
...stated ? { reason: stated } : {}
|
|
570
|
-
};
|
|
571
|
-
}
|
|
572
|
-
/** One comparable string for an exclusion list, order-insensitive. */
|
|
573
|
-
function exclusionKey(keys) {
|
|
574
|
-
return keys ? [...keys].sort((a, b) => a.localeCompare(b)).join("|") : "";
|
|
575
|
-
}
|
|
576
|
-
/**
|
|
577
|
-
* What a capability does to the table, from the live catalog.
|
|
578
|
-
*
|
|
579
|
-
* Read through the session rather than kept in a list here: the definition is
|
|
580
|
-
* the only thing that knows, and it is published on the catalog entry so
|
|
581
|
-
* every surface reads the same answer.
|
|
582
|
-
*/
|
|
583
|
-
function capabilityKind(session, capability) {
|
|
584
|
-
if (!session || capability === void 0) return void 0;
|
|
585
|
-
return session.catalog().find((entry) => entry.key === capability)?.kind;
|
|
586
|
-
}
|
|
587
|
-
/**
|
|
588
|
-
* The opt-in list, with an action's own demand for a human applied.
|
|
589
|
-
*
|
|
590
|
-
* The shared configuration is what the binding can see directly; an action
|
|
591
|
-
* that sets `approval: "required"` is read from the custom definition it was
|
|
592
|
-
* declared on, so a capability that must always be confirmed never offers the
|
|
593
|
-
* control however the table was configured.
|
|
594
|
-
*/
|
|
595
|
-
function alwaysAllowFor(approval, capabilities, capability) {
|
|
596
|
-
const shared = sharedApproval(approval);
|
|
597
|
-
if (capability === void 0) return shared.alwaysAllow;
|
|
598
|
-
const definition = capabilities?.find((entry) => entry.key === capability);
|
|
599
|
-
return resolveApproval(shared, definition?.ai).alwaysAllow;
|
|
600
|
-
}
|
|
601
|
-
/**
|
|
602
|
-
* How this binding finds what the person in front of the table may see.
|
|
603
|
-
*
|
|
604
|
-
* The lookups are React's — only this side knows how to find a row in a
|
|
605
|
-
* runtime view. What is done with them is not: the enrichment algorithm lives
|
|
606
|
-
* in `@adapttable/ai`, so every binding reports an unreadable column the same
|
|
607
|
-
* way rather than each inventing its own blank cell.
|
|
608
|
-
*/
|
|
609
|
-
function readerResolver(runtime, columns) {
|
|
610
|
-
const view = runtime.view();
|
|
611
|
-
const recordFor = (rowKey) => {
|
|
612
|
-
const row = findRow(runtime, rowKey);
|
|
613
|
-
return row && typeof row === "object" ? row : void 0;
|
|
614
|
-
};
|
|
615
|
-
return {
|
|
616
|
-
rowLabel: (rowKey) => {
|
|
617
|
-
const row = findRow(runtime, rowKey);
|
|
618
|
-
return row !== void 0 && view?.rowLabel ? view.rowLabel(row) : void 0;
|
|
619
|
-
},
|
|
620
|
-
cellValue: (rowKey, column) => recordFor(rowKey)?.[column],
|
|
621
|
-
cellText: (rowKey, column, value) => {
|
|
622
|
-
const format = (runtime.view()?.groupingState?.columns?.find((entry) => entry.key === column))?.formatValue;
|
|
623
|
-
const record = recordFor(rowKey);
|
|
624
|
-
if (!format || !record) return void 0;
|
|
625
|
-
return format({
|
|
626
|
-
...record,
|
|
627
|
-
[column]: value
|
|
628
|
-
});
|
|
629
|
-
},
|
|
630
|
-
readable: (column) => columns?.[column]?.readable !== false,
|
|
631
|
-
columnLabel: (column) => columns?.[column]?.label
|
|
632
|
-
};
|
|
633
|
-
}
|
|
634
|
-
/**
|
|
635
|
-
* The live table's grouping state, as the neutral rules want it.
|
|
636
|
-
*
|
|
637
|
-
* A projection, not a decision: it names where the facts are and which
|
|
638
|
-
* columns the agent may read. What may be offered and what may be set is
|
|
639
|
-
* `@adapttable/ai`'s to answer, so a second binding gets the same answers.
|
|
640
|
-
*/
|
|
641
|
-
function aggregationInputs(view, options) {
|
|
642
|
-
return {
|
|
643
|
-
state: view?.groupingState,
|
|
644
|
-
grouping: view?.sourceCapabilities?.grouping,
|
|
645
|
-
allows: (key) => options.columns?.[key]?.readable !== false
|
|
646
|
-
};
|
|
647
|
-
}
|
|
648
|
-
function asCallable(value) {
|
|
649
|
-
if (typeof value !== "function") return void 0;
|
|
650
|
-
return value;
|
|
651
|
-
}
|
|
652
|
-
/**
|
|
653
|
-
* View mutations whose default implementation belongs to this binding.
|
|
654
|
-
*
|
|
655
|
-
* They call React-backed runtime setters. Committing those setters before the
|
|
656
|
-
* call returns lets the session attribute the resulting table revision to the
|
|
657
|
-
* exact apply call that made it. Host overrides are deliberately excluded:
|
|
658
|
-
* only the host can say when its own async callback has settled.
|
|
659
|
-
*/
|
|
660
|
-
const BINDING_VIEW_MUTATIONS = /* @__PURE__ */ new Set([
|
|
661
|
-
"setPage",
|
|
662
|
-
"setLimit",
|
|
663
|
-
"setSearch",
|
|
664
|
-
"setSort",
|
|
665
|
-
"setFilters",
|
|
666
|
-
"setGroupBy",
|
|
667
|
-
"setAggregations",
|
|
668
|
-
"pinColumn",
|
|
669
|
-
"hideColumn",
|
|
670
|
-
"moveColumn",
|
|
671
|
-
"setColumnOrder",
|
|
672
|
-
"pinRow",
|
|
673
|
-
"setSelection"
|
|
674
|
-
]);
|
|
675
|
-
function createRevisionCounter() {
|
|
676
|
-
let token;
|
|
677
|
-
let revision = 1;
|
|
678
|
-
const bumpFromToken = (nextToken) => {
|
|
679
|
-
if (token === void 0) {
|
|
680
|
-
token = nextToken;
|
|
681
|
-
return revision;
|
|
682
|
-
}
|
|
683
|
-
if (token === nextToken) return revision;
|
|
684
|
-
token = nextToken;
|
|
685
|
-
revision += 1;
|
|
686
|
-
return revision;
|
|
687
|
-
};
|
|
688
|
-
return {
|
|
689
|
-
bumpFrom(revisions) {
|
|
690
|
-
return bumpFromToken(revisionToken(revisions));
|
|
691
|
-
},
|
|
692
|
-
bumpFromStamp(stamp) {
|
|
693
|
-
return bumpFromToken(stamp);
|
|
694
|
-
},
|
|
695
|
-
current() {
|
|
696
|
-
return revision;
|
|
697
|
-
}
|
|
698
|
-
};
|
|
699
|
-
}
|
|
700
|
-
function currentApply(options, runtime) {
|
|
701
|
-
const apply = {
|
|
702
|
-
...applyFromRuntime(runtime, options, options.apply),
|
|
703
|
-
...options.apply
|
|
704
|
-
};
|
|
705
|
-
apply.setFilters = (filters) => {
|
|
706
|
-
if (options.apply?.setFilters) {
|
|
707
|
-
options.apply.setFilters(filters);
|
|
708
|
-
return;
|
|
709
|
-
}
|
|
710
|
-
if (!applyLiveFilters(runtime.view()?.query, filters)) throw new Error("setFilters is not wired");
|
|
711
|
-
};
|
|
712
|
-
return apply;
|
|
713
|
-
}
|
|
714
|
-
function bindLiveSession(optionsRef, runtimeRef, revisionCounter, flushAdmission, waitForChrome, reportProgress) {
|
|
715
|
-
const apply = new Proxy({}, { get(_target, prop) {
|
|
716
|
-
if (typeof prop !== "string") return void 0;
|
|
717
|
-
if (!(prop in currentApply(optionsRef.current, runtimeRef.current))) return void 0;
|
|
718
|
-
return (...args) => {
|
|
719
|
-
const options = optionsRef.current;
|
|
720
|
-
const fn = asCallable(currentApply(options, runtimeRef.current)[prop]);
|
|
721
|
-
if (!fn) return void 0;
|
|
722
|
-
const host = options.apply;
|
|
723
|
-
if (BINDING_VIEW_MUTATIONS.has(prop) && !asCallable(host?.[prop])) {
|
|
724
|
-
let result;
|
|
725
|
-
flushSync(() => {
|
|
726
|
-
result = fn(...args);
|
|
727
|
-
});
|
|
728
|
-
return result;
|
|
729
|
-
}
|
|
730
|
-
return fn(...args);
|
|
731
|
-
};
|
|
732
|
-
} });
|
|
733
|
-
const observe = () => {
|
|
734
|
-
const options = optionsRef.current;
|
|
735
|
-
if (options.observe) return options.observe();
|
|
736
|
-
const runtimeView = runtimeRef.current.view();
|
|
737
|
-
const table = runtimeView?.neutralTable;
|
|
738
|
-
let viewRevision = revisionCounter.current();
|
|
739
|
-
if (table) viewRevision = revisionCounter.bumpFrom(table.revisions);
|
|
740
|
-
else if (runtimeView) viewRevision = revisionCounter.bumpFromStamp(viewRevisionStamp(runtimeView));
|
|
741
|
-
return observationFromRuntime(options, runtimeRef.current, viewRevision, apply);
|
|
742
|
-
};
|
|
743
|
-
const onApprove = (subject, signal) => {
|
|
744
|
-
const options = optionsRef.current;
|
|
745
|
-
if (options.onApprove) return options.onApprove(subject, signal);
|
|
746
|
-
return waitForChrome.current(subject, signal);
|
|
747
|
-
};
|
|
748
|
-
const inner = createAgentSession({
|
|
749
|
-
observe,
|
|
750
|
-
apply,
|
|
751
|
-
onApprove,
|
|
752
|
-
onProgress: (report) => {
|
|
753
|
-
reportProgress.current(report);
|
|
754
|
-
},
|
|
755
|
-
capabilities: [...optionsRef.current.capabilities ?? [], ...tableActionCapabilities(runtimeRef.current.view()?.actions, {
|
|
756
|
-
actions: () => runtimeRef.current.view()?.actions,
|
|
757
|
-
rowFor: (rowKey) => {
|
|
758
|
-
const view = runtimeRef.current.view();
|
|
759
|
-
return view?.rows.find((row) => view.getRowId(row) === rowKey);
|
|
760
|
-
},
|
|
761
|
-
selectedIds: () => {
|
|
762
|
-
const selection = runtimeRef.current.view()?.selection;
|
|
763
|
-
return selection ? [...selection.selectedIds] : void 0;
|
|
764
|
-
}
|
|
765
|
-
})],
|
|
766
|
-
...optionsRef.current.capabilityApproval ? { capabilityApproval: optionsRef.current.capabilityApproval } : {},
|
|
767
|
-
...optionsRef.current.excludeCapabilities ? { excludeCapabilities: optionsRef.current.excludeCapabilities } : {}
|
|
768
|
-
});
|
|
769
|
-
return {
|
|
770
|
-
catalog: () => inner.catalog(),
|
|
771
|
-
describe: (key) => inner.describe(key),
|
|
772
|
-
execute: async (key, args, expectedRevision, idempotencyKey, signal) => {
|
|
773
|
-
flushAdmission.current();
|
|
774
|
-
return await inner.execute(key, args, expectedRevision, idempotencyKey, signal);
|
|
775
|
-
},
|
|
776
|
-
manifest: () => inner.manifest()
|
|
777
|
-
};
|
|
778
|
-
}
|
|
779
139
|
/** One open approval, whichever shape the write took. */
|
|
780
140
|
function TableAgentProvider({ feature, children }) {
|
|
781
141
|
const options = feature.options;
|
|
@@ -805,7 +165,15 @@ function TableAgentProvider({ feature, children }) {
|
|
|
805
165
|
sessionRef.current = null;
|
|
806
166
|
revisionCounterRef.current = createRevisionCounter();
|
|
807
167
|
}
|
|
808
|
-
sessionRef.current ??= bindLiveSession(
|
|
168
|
+
sessionRef.current ??= bindLiveSession({
|
|
169
|
+
options: optionsRef,
|
|
170
|
+
runtime: runtimeRef,
|
|
171
|
+
revisions: revisionCounterRef.current,
|
|
172
|
+
flushAdmission,
|
|
173
|
+
waitForChrome,
|
|
174
|
+
reportProgress,
|
|
175
|
+
flush: flushSync
|
|
176
|
+
});
|
|
809
177
|
const session = sessionRef.current;
|
|
810
178
|
const [revocations, setRevocations] = useState(0);
|
|
811
179
|
const [progress, setProgress] = useState(null);
|