@keneth80/k-chart 1.19.0 → 1.20.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/CHANGELOG.md +19 -0
- package/README.md +28 -1
- package/ai/catalog.json +2291 -0
- package/ai/chart-plan.schema.json +372 -0
- package/ai/examples/intent-to-config.jsonl +32 -0
- package/ai/prompts/dashboard-system.md +42 -0
- package/docs/ai/agent-usage.md +79 -0
- package/docs/ai/chart-selection.md +65 -0
- package/docs/ai/index.md +195 -0
- package/lib/ai/catalog.d.ts +4 -0
- package/lib/ai/catalog.d.ts.map +1 -0
- package/lib/ai/catalog.js +676 -0
- package/lib/ai/catalog.js.map +1 -0
- package/lib/ai/compiler.d.ts +63 -0
- package/lib/ai/compiler.d.ts.map +1 -0
- package/lib/ai/compiler.js +950 -0
- package/lib/ai/compiler.js.map +1 -0
- package/lib/ai/contracts.d.ts +117 -0
- package/lib/ai/contracts.d.ts.map +1 -0
- package/lib/ai/contracts.js +3 -0
- package/lib/ai/contracts.js.map +1 -0
- package/lib/ai/index.d.ts +6 -0
- package/lib/ai/index.d.ts.map +1 -0
- package/lib/ai/index.js +22 -0
- package/lib/ai/index.js.map +1 -0
- package/lib/ai/schema.d.ts +272 -0
- package/lib/ai/schema.d.ts.map +1 -0
- package/lib/ai/schema.js +199 -0
- package/lib/ai/schema.js.map +1 -0
- package/lib/ai/validate-plan.d.ts +5 -0
- package/lib/ai/validate-plan.d.ts.map +1 -0
- package/lib/ai/validate-plan.js +877 -0
- package/lib/ai/validate-plan.js.map +1 -0
- package/llms.txt +37 -0
- package/package.json +19 -4
package/docs/ai/index.md
ADDED
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
# KChart AI integration
|
|
2
|
+
|
|
3
|
+
KChart AI integration is a provider-neutral planning layer. A language model selects a supported visualization and returns a declarative `ChartPlan`; deterministic application code validates that plan and compiles it into KChart configuration.
|
|
4
|
+
|
|
5
|
+
The model must not return executable JavaScript or TypeScript. This keeps provider output inspectable, testable, and safe to reject without replacing the currently rendered chart.
|
|
6
|
+
|
|
7
|
+
## Sources of truth
|
|
8
|
+
|
|
9
|
+
| Resource | Purpose |
|
|
10
|
+
| --- | --- |
|
|
11
|
+
| `src/ai/catalog.ts` | Typed capability metadata used at runtime |
|
|
12
|
+
| `ai/catalog.json` | Generated catalog for tools that do not import TypeScript |
|
|
13
|
+
| `src/ai/schema.ts` | Source JSON Schema object |
|
|
14
|
+
| `ai/chart-plan.schema.json` | Generated model-output schema |
|
|
15
|
+
| `ai/examples/intent-to-config.jsonl` | Natural-language examples and regression fixtures |
|
|
16
|
+
| `docs/ai/chart-selection.md` | Human-readable selection policy |
|
|
17
|
+
|
|
18
|
+
Run the following after changing the catalog or schema:
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
npm run generate:ai
|
|
22
|
+
npm run test:unit
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
`npm run test:unit` checks that committed generated artifacts match their TypeScript sources and validates every natural-language fixture.
|
|
26
|
+
|
|
27
|
+
## Recommended pipeline
|
|
28
|
+
|
|
29
|
+
```text
|
|
30
|
+
user prompt + data schema
|
|
31
|
+
|
|
|
32
|
+
v
|
|
33
|
+
deterministic data profiler
|
|
34
|
+
|
|
|
35
|
+
v
|
|
36
|
+
LLM returns ChartPlan JSON
|
|
37
|
+
|
|
|
38
|
+
v
|
|
39
|
+
validateKChartAIPlan()
|
|
40
|
+
|
|
|
41
|
+
v
|
|
42
|
+
compileKChartAIPlan()
|
|
43
|
+
|
|
|
44
|
+
v
|
|
45
|
+
render preview and explain selections
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
The data profiler should send field names, inferred types, nullability, row count, and a small redacted sample. It should not send an entire large dataset to the model.
|
|
49
|
+
|
|
50
|
+
## Runtime validation
|
|
51
|
+
|
|
52
|
+
```ts
|
|
53
|
+
import {
|
|
54
|
+
assertKChartAIPlan,
|
|
55
|
+
kChartAICatalog,
|
|
56
|
+
kChartAIPlanJsonSchema,
|
|
57
|
+
validateKChartAIPlan,
|
|
58
|
+
} from "@keneth80/k-chart/ai";
|
|
59
|
+
|
|
60
|
+
const result = validateKChartAIPlan(providerResponse);
|
|
61
|
+
|
|
62
|
+
if (!result.valid) {
|
|
63
|
+
console.error(result.issues);
|
|
64
|
+
// Keep the last successfully rendered chart.
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const plan = assertKChartAIPlan(providerResponse);
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
Validation checks capability ids, supported renderers, required field bindings, declared data fields, option compatibility, and strict top-level properties. Row-count guidance is reported as a warning rather than silently changing the requested chart.
|
|
72
|
+
|
|
73
|
+
## Compile a validated plan
|
|
74
|
+
|
|
75
|
+
`compileKChartAIPlan()` converts provider JSON into a real `KChartConfiguration`. The application supplies the selector and actual rows, so the model never controls DOM targets, callbacks, credentials, or the full dataset.
|
|
76
|
+
|
|
77
|
+
```ts
|
|
78
|
+
import {
|
|
79
|
+
compileKChartAIPlan,
|
|
80
|
+
renderKChartAIPlan,
|
|
81
|
+
} from "@keneth80/k-chart/ai";
|
|
82
|
+
|
|
83
|
+
const compiled = compileKChartAIPlan(providerResponse, {
|
|
84
|
+
selector: "#chart",
|
|
85
|
+
data: actualRows,
|
|
86
|
+
configuration: {
|
|
87
|
+
width: 960,
|
|
88
|
+
height: 480,
|
|
89
|
+
margin: { top: 72, right: 32, bottom: 56, left: 68 },
|
|
90
|
+
},
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
if (compiled.configuration) {
|
|
94
|
+
console.log(compiled.configuration, compiled.warnings);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const rendered = renderKChartAIPlan(providerResponse, {
|
|
98
|
+
selector: "#chart",
|
|
99
|
+
data: actualRows,
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
await rendered.controller.whenRenderComplete();
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
The compiler validates the plan again, infers axes when they are omitted, converts time-axis bounds to `Date`, applies only capability-specific setting allowlists, and keeps categorical zoom disabled. `renderKChartAIPlan()` is a convenience for chart-series plans; adapter-only plans intentionally have no core chart configuration.
|
|
106
|
+
|
|
107
|
+
### Trusted runtime settings
|
|
108
|
+
|
|
109
|
+
Model settings are JSON-only and filtered before they reach a factory. Supply callbacks, GeoJSON, provider instances, map styles, and credentials through trusted application settings:
|
|
110
|
+
|
|
111
|
+
```ts
|
|
112
|
+
const compiled = compileKChartAIPlan(regionPlan, {
|
|
113
|
+
selector: "#map",
|
|
114
|
+
data: regionalSales,
|
|
115
|
+
trustedSeriesSettings: {
|
|
116
|
+
"series.geo-region-map.svg": {
|
|
117
|
+
geoJson: koreaAdministrativeGeoJson,
|
|
118
|
+
onRegionClick: ({ data }) => openRegion(data),
|
|
119
|
+
},
|
|
120
|
+
},
|
|
121
|
+
});
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
Use `capability#0`, `capability#1`, and so on when a plan contains the same capability more than once. A capability-wide key acts as the default and the indexed key overrides it.
|
|
125
|
+
|
|
126
|
+
### Custom series and recipes
|
|
127
|
+
|
|
128
|
+
`series.custom` and `recipe.*` need application-owned rendering or lifecycle code. Register deterministic compilers instead of asking the model to generate functions:
|
|
129
|
+
|
|
130
|
+
```ts
|
|
131
|
+
const compiled = compileKChartAIPlan(recipePlan, {
|
|
132
|
+
selector: "#chart",
|
|
133
|
+
data,
|
|
134
|
+
seriesCompilers: {
|
|
135
|
+
"recipe.column.stacked": ({ selector, bindings, trustedSettings }) =>
|
|
136
|
+
createStackedColumnRecipe({
|
|
137
|
+
...trustedSettings,
|
|
138
|
+
selector,
|
|
139
|
+
xField: bindings.x as string,
|
|
140
|
+
fields: bindings.segments as string[],
|
|
141
|
+
}),
|
|
142
|
+
},
|
|
143
|
+
});
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
The callback is trusted source code owned by the application. `settings` contains filtered provider JSON; `trustedSettings` contains values supplied by the application.
|
|
147
|
+
|
|
148
|
+
## Public AI entry points
|
|
149
|
+
|
|
150
|
+
Applications can import the typed runtime API without adding AI metadata to the main KChart entry bundle:
|
|
151
|
+
|
|
152
|
+
```ts
|
|
153
|
+
import {
|
|
154
|
+
compileKChartAIPlan,
|
|
155
|
+
kChartAICatalog,
|
|
156
|
+
kChartAIPlanJsonSchema,
|
|
157
|
+
renderKChartAIPlan,
|
|
158
|
+
validateKChartAIPlan,
|
|
159
|
+
} from "@keneth80/k-chart/ai";
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
Tools that only need static assets can resolve these package subpaths:
|
|
163
|
+
|
|
164
|
+
- `@keneth80/k-chart/ai/catalog.json`
|
|
165
|
+
- `@keneth80/k-chart/ai/chart-plan.schema.json`
|
|
166
|
+
- `@keneth80/k-chart/llms.txt`
|
|
167
|
+
|
|
168
|
+
Each catalog entry declares both `apiKind` and `apiName`. `factory` identifies an importable function, `config-property` identifies configuration owned by the chart, `controller-method` identifies an imperative controller signal, and `recipe` identifies an application-owned composition.
|
|
169
|
+
|
|
170
|
+
## Optional adapters
|
|
171
|
+
|
|
172
|
+
Three.js, CesiumJS, and MapLibre integrations use `plan.adapters`, not `plan.series`. Adapter-only plans are valid. The compiler returns adapter descriptors with `importPath`, `apiName`, the actual data reference, field bindings, filtered plan settings, and separate trusted settings. The consuming application performs the dynamic client-side import and owns the adapter lifecycle.
|
|
173
|
+
|
|
174
|
+
Keep adapters as dynamic client-side imports in SSR frameworks. Provide tokens, map styles, imagery, terrain, and provider objects through `trustedAdapterSettings`, never through model output.
|
|
175
|
+
|
|
176
|
+
## Security boundary
|
|
177
|
+
|
|
178
|
+
- Never pass provider output to `eval`, `Function`, dynamic import, or JSX compilation.
|
|
179
|
+
- Map capability ids to an application-owned factory registry.
|
|
180
|
+
- Restrict formatters, callbacks, colors, and themes to serializable settings or registered preset ids.
|
|
181
|
+
- Validate remote URLs and geographic providers separately.
|
|
182
|
+
- Keep API tokens outside prompts and generated configuration.
|
|
183
|
+
- Limit sample rows and redact personal or confidential values.
|
|
184
|
+
|
|
185
|
+
## Current scope
|
|
186
|
+
|
|
187
|
+
The catalog covers first-class presets and series, documented custom recipes, core options, and optional adapter packages. `recipe.*` capabilities are intentionally separate from `series.*`: recipes require deterministic application-owned composition and do not imply that a same-named factory exists.
|
|
188
|
+
|
|
189
|
+
The deterministic ChartPlan compiler is the shared runtime boundary for the
|
|
190
|
+
Playground and dashboard template. The Playground provider now returns validated
|
|
191
|
+
JSON plans and deterministic editor source calls `compileKChartAIPlan()`; model
|
|
192
|
+
output is never evaluated as JavaScript. Its server API and client UI are
|
|
193
|
+
controlled separately with `KCHART_AI_ENABLED` and
|
|
194
|
+
`NEXT_PUBLIC_KCHART_AI_ENABLED`, so QA and Live builds can keep the unfinished
|
|
195
|
+
experience disabled.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"catalog.d.ts","sourceRoot":"","sources":["../../src/ai/catalog.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACR,kBAAkB,EAClB,eAAe,EAElB,MAAM,aAAa,CAAC;AAspBrB,eAAO,MAAM,eAAe,EAAE,eAuB7B,CAAC;AAEF,eAAO,MAAM,sBAAsB,GAC/B,IAAI,MAAM,KACX,kBAAkB,GAAG,SAEvB,CAAC"}
|