@open-artifact/data-core 0.0.0-stage → 0.2.1
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/LICENSE +202 -0
- package/NOTICE +5 -0
- package/README.md +5 -2
- package/package.json +37 -4
- package/src/addresses.d.ts +27 -0
- package/src/addresses.js +119 -0
- package/src/answers.d.ts +50 -0
- package/src/answers.js +190 -0
- package/src/check-page.d.ts +26 -0
- package/src/check-page.js +423 -0
- package/src/check.d.ts +47 -0
- package/src/check.js +541 -0
- package/src/connector-types.d.ts +354 -0
- package/src/connector-types.js +1 -0
- package/src/connectors.d.ts +14 -0
- package/src/connectors.js +43 -0
- package/src/drafts.d.ts +72 -0
- package/src/drafts.js +158 -0
- package/src/fill.d.ts +50 -0
- package/src/fill.js +274 -0
- package/src/forms.d.ts +13 -0
- package/src/forms.js +15 -0
- package/src/index.d.ts +23 -0
- package/src/index.js +23 -0
- package/src/limiter.d.ts +29 -0
- package/src/limiter.js +121 -0
- package/src/look.d.ts +101 -0
- package/src/look.js +417 -0
- package/src/model.d.ts +78 -0
- package/src/model.js +327 -0
- package/src/paths.d.ts +97 -0
- package/src/paths.js +116 -0
- package/src/readers.d.ts +36 -0
- package/src/readers.js +466 -0
- package/src/records.d.ts +21 -0
- package/src/records.js +41 -0
- package/src/rule-pattern.d.ts +7 -0
- package/src/rule-pattern.js +12 -0
- package/src/rules.d.ts +36 -0
- package/src/rules.js +144 -0
- package/src/sql.d.ts +36 -0
- package/src/sql.js +160 -0
- package/src/tables.d.ts +53 -0
- package/src/tables.js +266 -0
- package/src/text.d.ts +22 -0
- package/src/text.js +65 -0
- package/src/types.d.ts +252 -0
- package/src/types.js +1 -0
- package/src/versions.d.ts +16 -0
- package/src/versions.js +2 -0
- package/src/view-context.d.ts +120 -0
- package/src/view-context.js +256 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/NOTICE
ADDED
package/README.md
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @open-artifact/data-core
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The Data type's model, free of DOM and React: dataset and dashboard documents, filters and their rules, SQL filling, tables and data files, connector answer readers, dash/look colors, drafts, the view context and the project check.
|
|
4
|
+
|
|
5
|
+
Part of [Open Artifact](https://github.com/JimLiu/open-artifact): slide decks, design canvases and whiteboards an agent makes
|
|
6
|
+
and you view, edit and comment on in one local page. Source: [`packages/data-core`](https://github.com/JimLiu/open-artifact/tree/main/packages/data-core).
|
package/package.json
CHANGED
|
@@ -1,6 +1,39 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@open-artifact/data-core",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.1",
|
|
4
|
+
"description": "The Data type's model, free of DOM and React: dataset and dashboard documents, filters and their rules, SQL filling, tables and data files, connector answer readers, dash/look colors, drafts, the view context and the project check.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"types": "./src/index.d.ts",
|
|
9
|
+
"default": "./src/index.js"
|
|
10
|
+
},
|
|
11
|
+
"./package.json": "./package.json"
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"src",
|
|
15
|
+
"LICENSE",
|
|
16
|
+
"NOTICE"
|
|
17
|
+
],
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"@open-artifact/live-store": "0.2.1",
|
|
20
|
+
"@open-artifact/wasm-data-rules": "0.2.1"
|
|
21
|
+
},
|
|
22
|
+
"license": "Apache-2.0",
|
|
23
|
+
"author": {
|
|
24
|
+
"name": "Jim Liu 宝玉",
|
|
25
|
+
"url": "https://github.com/JimLiu"
|
|
26
|
+
},
|
|
27
|
+
"homepage": "https://github.com/JimLiu/open-artifact#readme",
|
|
28
|
+
"repository": {
|
|
29
|
+
"type": "git",
|
|
30
|
+
"url": "git+https://github.com/JimLiu/open-artifact.git",
|
|
31
|
+
"directory": "packages/data-core"
|
|
32
|
+
},
|
|
33
|
+
"bugs": {
|
|
34
|
+
"url": "https://github.com/JimLiu/open-artifact/issues"
|
|
35
|
+
},
|
|
36
|
+
"publishConfig": {
|
|
37
|
+
"access": "public"
|
|
38
|
+
}
|
|
39
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { SourceAddress, SourceResolution, Table } from './types.ts';
|
|
2
|
+
/** The attributes a page marks an element's data with. */
|
|
3
|
+
export declare const SOURCE_ATTRIBUTE = "data-source";
|
|
4
|
+
export declare const FIELD_ATTRIBUTE = "data-field";
|
|
5
|
+
export declare const WHERE_ATTRIBUTE = "data-where";
|
|
6
|
+
export declare const ROW_ATTRIBUTE = "data-row";
|
|
7
|
+
export declare const NO_MATCHING_ROW_MESSAGE = "no matching row";
|
|
8
|
+
/** The one part of an element the address readers use. */
|
|
9
|
+
export interface AttributeReader {
|
|
10
|
+
getAttribute(name: string): string | null;
|
|
11
|
+
}
|
|
12
|
+
export type SourceWhere = NonNullable<SourceAddress['where']>;
|
|
13
|
+
export declare function decodeWhereComponent(text: string): string;
|
|
14
|
+
/** `data-where="col=value&other=value"`, each part URI-decoded; parts without a column are skipped. */
|
|
15
|
+
export declare function parseWhereAddress(text: string): SourceWhere;
|
|
16
|
+
/** `data-row`: a whole number, or `-1` when it isn't one. */
|
|
17
|
+
export declare function parseRowIndex(text: string): number;
|
|
18
|
+
/** The distinct dataset ids an element's `data-source` names. */
|
|
19
|
+
export declare function getElementSourceIds(element: AttributeReader): string[];
|
|
20
|
+
/** Where a marked element's value comes from (`data-row` counts only without `data-where`). */
|
|
21
|
+
export declare function readSourceAddress(element: AttributeReader, source?: string): SourceAddress;
|
|
22
|
+
export declare function hasCellAddress(address: SourceAddress): boolean;
|
|
23
|
+
export declare function unresolvedSourceResolution(kind: SourceResolution['kind'], reason: string): SourceResolution;
|
|
24
|
+
/** The rows (and cell, for one row and a field) an address picks from a table, or why it picks none. */
|
|
25
|
+
export declare function resolveTableAddress(address: SourceAddress, table: Table): SourceResolution;
|
|
26
|
+
/** An address resolved against the loaded tables (`null` while a table is still loading). */
|
|
27
|
+
export declare function resolveSourceAddress(address: SourceAddress, tables: ReadonlyMap<string, Table | null>): SourceResolution;
|
package/src/addresses.js
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { isValidDatasetId } from "./paths.js";
|
|
2
|
+
import { cellToString } from "./tables.js";
|
|
3
|
+
/** The attributes a page marks an element's data with. */
|
|
4
|
+
export const SOURCE_ATTRIBUTE = 'data-source';
|
|
5
|
+
export const FIELD_ATTRIBUTE = 'data-field';
|
|
6
|
+
export const WHERE_ATTRIBUTE = 'data-where';
|
|
7
|
+
export const ROW_ATTRIBUTE = 'data-row';
|
|
8
|
+
export const NO_MATCHING_ROW_MESSAGE = 'no matching row';
|
|
9
|
+
export function decodeWhereComponent(text) {
|
|
10
|
+
try {
|
|
11
|
+
return decodeURIComponent(text);
|
|
12
|
+
}
|
|
13
|
+
catch {
|
|
14
|
+
return text;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
/** `data-where="col=value&other=value"`, each part URI-decoded; parts without a column are skipped. */
|
|
18
|
+
export function parseWhereAddress(text) {
|
|
19
|
+
const where = [];
|
|
20
|
+
for (const part of text.split('&')) {
|
|
21
|
+
const equals = part.indexOf('=');
|
|
22
|
+
const column = equals <= 0 ? '' : decodeWhereComponent(part.slice(0, equals)).trim();
|
|
23
|
+
if (column === '')
|
|
24
|
+
continue;
|
|
25
|
+
where.push({ column, value: decodeWhereComponent(part.slice(equals + 1)) });
|
|
26
|
+
}
|
|
27
|
+
return where;
|
|
28
|
+
}
|
|
29
|
+
/** `data-row`: a whole number, or `-1` when it isn't one. */
|
|
30
|
+
export function parseRowIndex(text) {
|
|
31
|
+
const index = /^\d+$/.test(text.trim()) ? Number(text.trim()) : -1;
|
|
32
|
+
return Number.isSafeInteger(index) ? index : -1;
|
|
33
|
+
}
|
|
34
|
+
/** The distinct dataset ids an element's `data-source` names. */
|
|
35
|
+
export function getElementSourceIds(element) {
|
|
36
|
+
return [...new Set((element.getAttribute(SOURCE_ATTRIBUTE) ?? '').split(/\s+/).filter(isValidDatasetId))];
|
|
37
|
+
}
|
|
38
|
+
/** Where a marked element's value comes from (`data-row` counts only without `data-where`). */
|
|
39
|
+
export function readSourceAddress(element, source = getElementSourceIds(element)[0] ?? '') {
|
|
40
|
+
const address = { source };
|
|
41
|
+
const field = element.getAttribute(FIELD_ATTRIBUTE)?.trim();
|
|
42
|
+
if (field !== undefined && field !== '')
|
|
43
|
+
address.field = field;
|
|
44
|
+
const where = element.getAttribute(WHERE_ATTRIBUTE);
|
|
45
|
+
if (where !== null)
|
|
46
|
+
address.where = parseWhereAddress(where);
|
|
47
|
+
const row = element.getAttribute(ROW_ATTRIBUTE);
|
|
48
|
+
if (row !== null && where === null)
|
|
49
|
+
address.row = parseRowIndex(row);
|
|
50
|
+
return address;
|
|
51
|
+
}
|
|
52
|
+
export function hasCellAddress(address) {
|
|
53
|
+
return address.field !== undefined || address.where !== undefined || address.row !== undefined;
|
|
54
|
+
}
|
|
55
|
+
export function unresolvedSourceResolution(kind, reason) {
|
|
56
|
+
return { status: 'unresolved', kind, reason, rowIndexes: [] };
|
|
57
|
+
}
|
|
58
|
+
const rowIndexCache = new WeakMap();
|
|
59
|
+
function getTableRowIndex(table, columns) {
|
|
60
|
+
const byColumns = rowIndexCache.get(table) ?? new Map();
|
|
61
|
+
rowIndexCache.set(table, byColumns);
|
|
62
|
+
const key = JSON.stringify(columns);
|
|
63
|
+
const found = byColumns.get(key);
|
|
64
|
+
if (found !== undefined)
|
|
65
|
+
return found;
|
|
66
|
+
const index = new Map();
|
|
67
|
+
table.rows.forEach((row, rowIndex) => {
|
|
68
|
+
const value = JSON.stringify(columns.map((column) => cellToString(row[column])));
|
|
69
|
+
const rows = index.get(value);
|
|
70
|
+
if (rows === undefined)
|
|
71
|
+
index.set(value, [rowIndex]);
|
|
72
|
+
else
|
|
73
|
+
rows.push(rowIndex);
|
|
74
|
+
});
|
|
75
|
+
byColumns.set(key, index);
|
|
76
|
+
return index;
|
|
77
|
+
}
|
|
78
|
+
function resolveWhereRows(where, table) {
|
|
79
|
+
if (where.length === 0)
|
|
80
|
+
return 'data-where isn’t col=value';
|
|
81
|
+
const missing = where.find((part) => !table.columns.includes(part.column));
|
|
82
|
+
if (missing !== undefined)
|
|
83
|
+
return `no column “${missing.column}”`;
|
|
84
|
+
const rows = getTableRowIndex(table, where.map((part) => part.column)).get(JSON.stringify(where.map((part) => part.value))) ?? [];
|
|
85
|
+
return rows.length === 0 ? NO_MATCHING_ROW_MESSAGE : [...rows];
|
|
86
|
+
}
|
|
87
|
+
function resolveAddressRows(address, table) {
|
|
88
|
+
if (address.where !== undefined)
|
|
89
|
+
return resolveWhereRows(address.where, table);
|
|
90
|
+
if (address.row === undefined)
|
|
91
|
+
return table.rows.map((_, index) => index);
|
|
92
|
+
return address.row >= 0 && address.row < table.rows.length
|
|
93
|
+
? [address.row]
|
|
94
|
+
: `no row ${address.row < 0 ? 'by that number' : String(address.row)}`;
|
|
95
|
+
}
|
|
96
|
+
/** The rows (and cell, for one row and a field) an address picks from a table, or why it picks none. */
|
|
97
|
+
export function resolveTableAddress(address, table) {
|
|
98
|
+
if (address.field !== undefined && !table.columns.includes(address.field))
|
|
99
|
+
return unresolvedSourceResolution('cells', `no column “${address.field}”`);
|
|
100
|
+
const rows = resolveAddressRows(address, table);
|
|
101
|
+
if (typeof rows === 'string')
|
|
102
|
+
return unresolvedSourceResolution('cells', rows);
|
|
103
|
+
const resolution = { status: 'ok', kind: 'cells', table, rowIndexes: rows };
|
|
104
|
+
if (address.field === undefined)
|
|
105
|
+
return resolution;
|
|
106
|
+
resolution.field = address.field;
|
|
107
|
+
if (rows.length === 1)
|
|
108
|
+
resolution.cell = table.rows[rows[0]]?.[address.field] ?? null;
|
|
109
|
+
return resolution;
|
|
110
|
+
}
|
|
111
|
+
/** An address resolved against the loaded tables (`null` while a table is still loading). */
|
|
112
|
+
export function resolveSourceAddress(address, tables) {
|
|
113
|
+
const table = tables.get(address.source);
|
|
114
|
+
if (table === undefined)
|
|
115
|
+
return unresolvedSourceResolution('cells', `no dataset “${address.source}”`);
|
|
116
|
+
if (table === null)
|
|
117
|
+
return { status: 'pending', kind: 'cells', rowIndexes: [] };
|
|
118
|
+
return resolveTableAddress(address, table);
|
|
119
|
+
}
|
package/src/answers.d.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { CachedToolResult, McpError, McpToolResult, ReaderError, ToolCall, ToolCallGot } from './connector-types.ts';
|
|
2
|
+
import type { ResultMetadata, Table, UnknownRecord } from './types.ts';
|
|
3
|
+
/** The error codes a tool call through the host reports as they are; any other failure is "upstream_error". */
|
|
4
|
+
export declare const MCP_ERROR_CODES: ReadonlySet<string>;
|
|
5
|
+
export declare function isRecordArray(value: unknown): value is UnknownRecord[];
|
|
6
|
+
/** A record whose values are all scalars (no objects or lists). */
|
|
7
|
+
export declare function isScalarRecord(record: UnknownRecord): boolean;
|
|
8
|
+
/** A list of records, or a record holding one under `rows`. */
|
|
9
|
+
export declare function isTablePayload(value: unknown): boolean;
|
|
10
|
+
/** The table of a rows answer that has at least one row, else `null`. */
|
|
11
|
+
export declare function getNonemptyTable(value: unknown): Table | null;
|
|
12
|
+
/** A list as a record keyed `"1"`, `"2"`, …. */
|
|
13
|
+
export declare function arrayToNumberedRecord(items: readonly unknown[]): UnknownRecord;
|
|
14
|
+
/** A sentence (with a leading space) saying what an answer was, for a failure message; empty when not text or an object. */
|
|
15
|
+
export declare function describeAnswerShape(answer: unknown): string;
|
|
16
|
+
/** What error an answer reports: its `error` text, or `error.message`; empty when none. */
|
|
17
|
+
export declare function getLoaderAnswerError(answer: unknown): string;
|
|
18
|
+
/** A result's `meta` list: entries with a label and a value, typed "text" or "code" (with its language). */
|
|
19
|
+
export declare function parseResultMetadata(value: unknown): ResultMetadata[];
|
|
20
|
+
export declare function withResultMetadata(table: Table, metadata: unknown): Table;
|
|
21
|
+
export declare function copyResultMetadata(from?: {
|
|
22
|
+
meta?: ResultMetadata[];
|
|
23
|
+
}): ResultMetadata[];
|
|
24
|
+
/** A reader's failure as an error a loader can throw; the whole failure travels with it. */
|
|
25
|
+
export declare class ResultReaderError extends Error {
|
|
26
|
+
readonly failure: ReaderError;
|
|
27
|
+
readonly meta: ResultMetadata[];
|
|
28
|
+
readonly quoted: boolean;
|
|
29
|
+
constructor(failure: ReaderError);
|
|
30
|
+
}
|
|
31
|
+
/** A tool call's failure: its own code when the host reports a known one, else "upstream_error". */
|
|
32
|
+
export declare function normalizeMcpError(error: unknown): McpError;
|
|
33
|
+
/** When the host's tool cache stored this answer, or `null` for a fresh call. */
|
|
34
|
+
export declare function getCachedStoredAt(result: McpToolResult): number | null;
|
|
35
|
+
/** A cached answer the host pushed (`{type: "data", result}` with a stored time), or `null`. */
|
|
36
|
+
export declare function parseCachedToolResult(value: unknown): CachedToolResult | null;
|
|
37
|
+
/** The text of a failed tool call. */
|
|
38
|
+
export declare function getToolErrorText(result: McpToolResult): string;
|
|
39
|
+
/** A tool's answer: its payload, its structured content, or its first text item (parsed when it is JSON). */
|
|
40
|
+
export declare function unwrapToolPayload(result: McpToolResult): unknown;
|
|
41
|
+
/** What a call got back, in short. */
|
|
42
|
+
export declare function summarizeToolAnswer(answer: unknown): ToolCallGot;
|
|
43
|
+
/** Adds a call to a source's chain; a repeat of the last tool (a poll apart from other calls) is counted on it. */
|
|
44
|
+
export declare function recordToolCall(calls: ToolCall[], tool: string, ms: number, ok: boolean, got: ToolCallGot, poll?: boolean): void;
|
|
45
|
+
/** One call of a source's chain: "tool ×2 · 1.5 s", "… · failed". */
|
|
46
|
+
export declare function describeToolCall(call: ToolCall): string;
|
|
47
|
+
/** What a call got back: "12 rows", "text", "keys: a, b". */
|
|
48
|
+
export declare function describeToolCallGot(got: ToolCallGot): string;
|
|
49
|
+
/** A source's calls in one line ("a · 120 ms → b ×3 · 6.0 s"), at most 160 characters. */
|
|
50
|
+
export declare function summarizeToolCallChain(calls?: readonly ToolCall[]): string;
|
package/src/answers.js
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
import { isPlainObject, normalizeCodeLanguage } from "./text.js";
|
|
2
|
+
import { normalizeTable } from "./tables.js";
|
|
3
|
+
/** The error codes a tool call through the host reports as they are; any other failure is "upstream_error". */
|
|
4
|
+
export const MCP_ERROR_CODES = new Set([
|
|
5
|
+
'not_declared',
|
|
6
|
+
'not_granted',
|
|
7
|
+
'consent_required',
|
|
8
|
+
'capability_disabled',
|
|
9
|
+
'capability_removed',
|
|
10
|
+
'rate_limited',
|
|
11
|
+
'upstream_error',
|
|
12
|
+
'tool_error',
|
|
13
|
+
]);
|
|
14
|
+
export function isRecordArray(value) {
|
|
15
|
+
return Array.isArray(value) && value.every(isPlainObject);
|
|
16
|
+
}
|
|
17
|
+
/** A record whose values are all scalars (no objects or lists). */
|
|
18
|
+
export function isScalarRecord(record) {
|
|
19
|
+
return Object.values(record).every((value) => value === null || typeof value !== 'object');
|
|
20
|
+
}
|
|
21
|
+
/** A list of records, or a record holding one under `rows`. */
|
|
22
|
+
export function isTablePayload(value) {
|
|
23
|
+
return isRecordArray(value) || (isPlainObject(value) && isRecordArray(value.rows));
|
|
24
|
+
}
|
|
25
|
+
/** The table of a rows answer that has at least one row, else `null`. */
|
|
26
|
+
export function getNonemptyTable(value) {
|
|
27
|
+
const table = isTablePayload(value) ? normalizeTable(value) : null;
|
|
28
|
+
return table === null || table.rows.length === 0 ? null : table;
|
|
29
|
+
}
|
|
30
|
+
/** A list as a record keyed `"1"`, `"2"`, …. */
|
|
31
|
+
export function arrayToNumberedRecord(items) {
|
|
32
|
+
return Object.fromEntries(items.map((item, index) => [String(index + 1), item]));
|
|
33
|
+
}
|
|
34
|
+
/** A sentence (with a leading space) saying what an answer was, for a failure message; empty when not text or an object. */
|
|
35
|
+
export function describeAnswerShape(answer) {
|
|
36
|
+
if (typeof answer === 'string')
|
|
37
|
+
return ` The answer is text, ${answer.length.toLocaleString('en-US')} characters.`;
|
|
38
|
+
if (!isPlainObject(answer))
|
|
39
|
+
return '';
|
|
40
|
+
return ` The answer’s keys: ${Object.keys(answer).join(', ')}.`;
|
|
41
|
+
}
|
|
42
|
+
/** What error an answer reports: its `error` text, or `error.message`; empty when none. */
|
|
43
|
+
export function getLoaderAnswerError(answer) {
|
|
44
|
+
const error = isPlainObject(answer) ? answer.error : undefined;
|
|
45
|
+
if (typeof error === 'string')
|
|
46
|
+
return error;
|
|
47
|
+
return isPlainObject(error) && typeof error.message === 'string' ? error.message : '';
|
|
48
|
+
}
|
|
49
|
+
function parseResultMetadataEntry(entry) {
|
|
50
|
+
if (!isPlainObject(entry))
|
|
51
|
+
return [];
|
|
52
|
+
const { label, value, type, lang } = entry;
|
|
53
|
+
if (typeof label !== 'string' || typeof value !== 'string' || label.trim() === '' || value.trim() === '')
|
|
54
|
+
return [];
|
|
55
|
+
if (type !== 'code')
|
|
56
|
+
return [{ label, value, type: 'text' }];
|
|
57
|
+
const language = normalizeCodeLanguage(lang);
|
|
58
|
+
return [language === undefined ? { label, value, type } : { label, value, type, lang: language }];
|
|
59
|
+
}
|
|
60
|
+
/** A result's `meta` list: entries with a label and a value, typed "text" or "code" (with its language). */
|
|
61
|
+
export function parseResultMetadata(value) {
|
|
62
|
+
try {
|
|
63
|
+
return Array.isArray(value) ? value.flatMap(parseResultMetadataEntry) : [];
|
|
64
|
+
}
|
|
65
|
+
catch {
|
|
66
|
+
return [];
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
export function withResultMetadata(table, metadata) {
|
|
70
|
+
const meta = parseResultMetadata(metadata);
|
|
71
|
+
return meta.length === 0 ? table : { ...table, meta };
|
|
72
|
+
}
|
|
73
|
+
export function copyResultMetadata(from) {
|
|
74
|
+
return (from?.meta ?? []).map((entry) => ({ ...entry }));
|
|
75
|
+
}
|
|
76
|
+
/** A reader's failure as an error a loader can throw; the whole failure travels with it. */
|
|
77
|
+
export class ResultReaderError extends Error {
|
|
78
|
+
failure;
|
|
79
|
+
meta;
|
|
80
|
+
quoted;
|
|
81
|
+
constructor(failure) {
|
|
82
|
+
super(failure.error);
|
|
83
|
+
this.name = 'ResultReaderError';
|
|
84
|
+
this.failure = failure;
|
|
85
|
+
this.meta = failure.meta ?? [];
|
|
86
|
+
this.quoted = failure.quoted === true;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
/** A tool call's failure: its own code when the host reports a known one, else "upstream_error". */
|
|
90
|
+
export function normalizeMcpError(error) {
|
|
91
|
+
if (typeof error === 'object' && error !== null && 'code' in error) {
|
|
92
|
+
const { code, message } = error;
|
|
93
|
+
const text = typeof message === 'string' ? message : String(code);
|
|
94
|
+
if (typeof code === 'string' && MCP_ERROR_CODES.has(code))
|
|
95
|
+
return { code, message: text };
|
|
96
|
+
}
|
|
97
|
+
return { code: 'upstream_error', message: error instanceof Error ? error.message : 'mcp call failed' };
|
|
98
|
+
}
|
|
99
|
+
/** When the host's tool cache stored this answer, or `null` for a fresh call. */
|
|
100
|
+
export function getCachedStoredAt(result) {
|
|
101
|
+
const storedAt = result.cache?.storedAt;
|
|
102
|
+
return typeof storedAt === 'number' && Number.isFinite(storedAt) ? storedAt : null;
|
|
103
|
+
}
|
|
104
|
+
/** A cached answer the host pushed (`{type: "data", result}` with a stored time), or `null`. */
|
|
105
|
+
export function parseCachedToolResult(value) {
|
|
106
|
+
if (!isPlainObject(value) || value.type !== 'data')
|
|
107
|
+
return null;
|
|
108
|
+
const result = value.result;
|
|
109
|
+
if (!isPlainObject(result))
|
|
110
|
+
return null;
|
|
111
|
+
const at = getCachedStoredAt(result);
|
|
112
|
+
return at === null ? null : { result: result, at };
|
|
113
|
+
}
|
|
114
|
+
/** The text of a failed tool call. */
|
|
115
|
+
export function getToolErrorText(result) {
|
|
116
|
+
return result.content?.find((item) => typeof item?.text === 'string')?.text ?? 'tool call failed';
|
|
117
|
+
}
|
|
118
|
+
/** A tool's answer: its payload, its structured content, or its first text item (parsed when it is JSON). */
|
|
119
|
+
export function unwrapToolPayload(result) {
|
|
120
|
+
if (result.payload !== undefined)
|
|
121
|
+
return result.payload;
|
|
122
|
+
if (result.structuredContent !== undefined)
|
|
123
|
+
return result.structuredContent;
|
|
124
|
+
const first = result.content?.[0];
|
|
125
|
+
if (first && typeof first.text === 'string')
|
|
126
|
+
try {
|
|
127
|
+
return JSON.parse(first.text);
|
|
128
|
+
}
|
|
129
|
+
catch {
|
|
130
|
+
return first.text;
|
|
131
|
+
}
|
|
132
|
+
return undefined;
|
|
133
|
+
}
|
|
134
|
+
/** What a call got back, in short. */
|
|
135
|
+
export function summarizeToolAnswer(answer) {
|
|
136
|
+
if (Array.isArray(answer))
|
|
137
|
+
return { kind: 'rows', n: answer.length };
|
|
138
|
+
if (typeof answer === 'string')
|
|
139
|
+
return { kind: 'text' };
|
|
140
|
+
if (typeof answer !== 'object' || answer === null)
|
|
141
|
+
return answer === undefined ? { kind: 'nothing' } : { kind: 'plain', type: typeof answer };
|
|
142
|
+
return { kind: 'keys', keys: Object.keys(answer) };
|
|
143
|
+
}
|
|
144
|
+
/** Adds a call to a source's chain; a repeat of the last tool (a poll apart from other calls) is counted on it. */
|
|
145
|
+
export function recordToolCall(calls, tool, ms, ok, got, poll = false) {
|
|
146
|
+
const last = calls.at(-1);
|
|
147
|
+
if (last === undefined || last.tool !== tool || (last.poll === true) !== poll) {
|
|
148
|
+
calls.push({ tool, n: 1, ms: Math.round(ms), ok, got, ...(poll ? { poll: true } : {}) });
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
last.n += 1;
|
|
152
|
+
last.ms += Math.round(ms);
|
|
153
|
+
last.ok = ok;
|
|
154
|
+
last.got = got;
|
|
155
|
+
}
|
|
156
|
+
const decimal = new Intl.NumberFormat('en-US');
|
|
157
|
+
/** One call of a source's chain: "tool ×2 · 1.5 s", "… · failed". */
|
|
158
|
+
export function describeToolCall(call) {
|
|
159
|
+
const name = call.n > 1 ? `${call.tool} ×${decimal.format(call.n)}` : call.tool;
|
|
160
|
+
const time = call.ms < 1000
|
|
161
|
+
? `${decimal.format(call.ms)} ms`
|
|
162
|
+
: `${Number((call.ms / 1000).toFixed(1)).toLocaleString('en-US', {
|
|
163
|
+
minimumFractionDigits: 1,
|
|
164
|
+
maximumFractionDigits: 1,
|
|
165
|
+
useGrouping: false,
|
|
166
|
+
})} s`;
|
|
167
|
+
return call.ok ? `${name} · ${time}` : `${name} · ${time} · failed`;
|
|
168
|
+
}
|
|
169
|
+
/** What a call got back: "12 rows", "text", "keys: a, b". */
|
|
170
|
+
export function describeToolCallGot(got) {
|
|
171
|
+
switch (got.kind) {
|
|
172
|
+
case 'rows':
|
|
173
|
+
return got.n === 1 ? `${decimal.format(got.n)} row` : `${decimal.format(got.n)} rows`;
|
|
174
|
+
case 'keys':
|
|
175
|
+
return `keys: ${got.keys.join(', ')}`;
|
|
176
|
+
case 'text':
|
|
177
|
+
case 'nothing':
|
|
178
|
+
case 'failed':
|
|
179
|
+
return got.kind;
|
|
180
|
+
case 'plain':
|
|
181
|
+
return got.type;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
/** A source's calls in one line ("a · 120 ms → b ×3 · 6.0 s"), at most 160 characters. */
|
|
185
|
+
export function summarizeToolCallChain(calls) {
|
|
186
|
+
if (calls === undefined || calls.length === 0)
|
|
187
|
+
return '';
|
|
188
|
+
const text = calls.map(describeToolCall).join(' → ');
|
|
189
|
+
return text.length <= 160 ? text : `${text.slice(0, 159)}…`;
|
|
190
|
+
}
|