Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions __tests__/chart-contracts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ import { charts as fixtures } from './fixtures/charts';
import type { ChartSpec } from '../src/types';

describe('native chart contracts', () => {
it('covers every advisor chart and renders each native data shape through the SDK', async () => {
it('keeps the advisor aligned with the contract and renders native chart objects', async () => {
expect(z.toJSONSchema(vizInputSchema, { io: 'input' }).oneOf).toHaveLength(20);
expect(Object.keys(fixtures).sort()).toEqual(chartDefinitions.map((chart) => chart.type).sort());
const chartTypes = vizInputSchema.options.map((schema) => schema.shape.chartType.value);
expect(chartDefinitions.map((chart) => chart.type).sort()).toEqual(chartTypes.sort());
const ava = new AVA({ llm: { model: 'unused' } });
for (const [chartType, config] of Object.entries(fixtures)) {
const spec = vizInputSchema.parse({ chartType, config: { ...config, customOption: true } });
Expand All @@ -26,12 +27,11 @@ describe('native chart contracts', () => {
}
});

it('accepts native summary strings without adding summaries to automatic chart recommendations', async () => {
it('renders native summary strings', async () => {
const ava = new AVA({ llm: { model: 'unused' } });
const spec = { chartType: 'summary' as const, config: '# Sales report\nRevenue increased by 10%.' };
const html = await ava.viz(spec);
expect(runInNewContext(`(${html.match(/const visConfig = (.+);/)![1]})`, {}, { timeout: 1000 })).toBe(spec.config);
expect(chartDefinitions.some((chart) => chart.type === 'summary')).toBe(false);
expect(() => vizInputSchema.parse({ ...spec, config: { type: 'summary', text: 'Report' } })).toThrow();
for (const config of ['', ' ', 'vis column\ndata\n - category A\n value 1']) {
expect(() => vizInputSchema.parse({ ...spec, config })).toThrow();
Expand Down
18 changes: 17 additions & 1 deletion __tests__/visualization.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function reply(text: string) {
describe('chart recommendation', () => {
it('generates a chart specification without rendering HTML', async () => {
reply('column');
reply(`\`\`\`json\n${json}\n\`\`\``);
reply(`\`\`\`json\n${JSON.stringify({ data: config.data })}\n\`\`\``);
expect(await ava['recommend'](result)).toEqual({ chartType: 'column', config });
expect(generateText).toHaveBeenCalledTimes(2);
const prompt = vi.mocked(generateText).mock.calls[0][0].prompt;
Expand Down Expand Up @@ -56,6 +56,22 @@ describe('chart recommendation', () => {
await expect(ava['recommend'](result)).rejects.toThrow('Model unavailable');
});

it('generates and renders a requested GPT-Vis summary through visualize', async () => {
const query = 'Create a visual summary of revenue by city';
const summary = '# Revenue\nHangzhou earned [100](metric_value, origin=100).';
reply('summary');
reply(JSON.stringify(summary));

const visualization = await ava.visualize({ ...result, query });

expect(visualization).toMatchObject({ chartType: 'summary', config: summary });
expect(vi.mocked(generateText).mock.calls[0][0].prompt).toContain('"summary"');
expect(vi.mocked(generateText).mock.calls[1][0].prompt).toContain('"type":"string"');
expect(
runInNewContext(`(${visualization!.html.match(/const visConfig = (.+);/)![1]})`, {}, { timeout: 1000 })
).toBe(summary);
});

it('renders the recommended specification through visualize', async () => {
const onEvent = vi.fn();
ava.on('visualizestart', onEvent);
Expand Down
4 changes: 2 additions & 2 deletions site/content/docs/en/reference/api.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -369,11 +369,11 @@ Neither requires a loaded source. Both use the model and return `null` for empty
| ---------------- | ----------- | -------- | --------------------------------------------------------------------------------------------------- |
| `spec` | `ChartSpecInput` | Yes | Chart specification before defaults are applied. |
| `spec.chartType` | `ChartType` | Yes | One of the types below; must match `config.type` for JSON charts. |
| `spec.config` | `ChartSpecInput['config']` | Yes | Native GPT-Vis config; a missing `config.type` is filled from `chartType`. For `summary`, pass a T8 Syntax string. |
| `spec.config` | `ChartSpecInput['config']` | Yes | Native GPT-Vis config; a missing `config.type` is filled from `chartType`. For `summary`, pass a GPT-Vis summary string. |

`ChartType` values: `line`, `column`, `bar`, `pie`, `area`, `scatter`, `dual-axes`, `histogram`, `boxplot`, `radar`, `funnel`, `waterfall`, `liquid`, `word-cloud`, `violin`, `venn`, `treemap`, `sankey`, `table`, `summary`.

GPT-Vis supports 26 visualization types. AVA recommends the 19 JSON chart types above and additionally accepts native summary strings through `viz({ chartType: 'summary', config: '# Sales report' })`. The six relationship graph types are outside AVA's current contract. `ChartSpec` describes the normalized result, with `config.type` filled for JSON charts.
GPT-Vis supports 26 visualization types. AVA can recommend the 20 types above. For `summary`, `config` contains the native GPT-Vis summary string. You can also render a summary directly through `viz({ chartType: 'summary', config: '# Sales report' })`. The six relationship graph types are outside AVA's current contract. `ChartSpec` describes the normalized result, with `config.type` filled for JSON charts.

`spec.html?: string` accepts the complete result from `visualize()`. Rendering always uses `config` to generate fresh HTML; an existing `html` value is not reused. Other unknown envelope fields are rejected.

Expand Down
2 changes: 1 addition & 1 deletion site/content/docs/en/visualization/overview.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,6 @@ if (spec) {
| Composition & flow | `pie`, `treemap`, `sankey`, `waterfall`, `funnel` |
| Other | `radar`, `liquid`, `word-cloud`, `venn`, `table` |

The table lists AVA's 19 recommended JSON chart types. GPT-Vis itself supports 26 types; AVA additionally accepts native summary strings through `viz({ chartType: 'summary', config: '# Sales report' })`. Its six relationship graph types are outside AVA's current contract. A chart's suitability depends on its data requirements and the user's question. The recommendation module supplies the 19 chart definitions to the model.
AVA can recommend the 19 chart types in the table and `summary`. A summary has a native GPT-Vis string in `config`. You can also render it directly through `viz({ chartType: 'summary', config: '# Sales report' })`. The six GPT-Vis relationship graph types are outside AVA's current contract. A chart's suitability depends on its data requirements and the user's question.

See [recommend parameters](/documentation/reference/api/#visualize-and-recommend) and [viz parameters](/documentation/reference/api/#viz) for all fields and chart types.
4 changes: 2 additions & 2 deletions site/content/docs/zh/reference/api.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -371,11 +371,11 @@ Setter 接收选项对象,同步返回 `Semantic`。AVA 验证并复制结果
| ---------------- | ----------- | -------- | --------------------------------------------------------------------------------------------------- |
| `spec` | `ChartSpecInput` | 是 | 应用默认值之前的图表描述对象。 |
| `spec.chartType` | `ChartType` | 是 | 下方类型之一;JSON 图表必须与 `config.type` 一致。 |
| `spec.config` | `ChartSpecInput['config']` | 是 | 原生 GPT-Vis 配置;省略 `config.type` 时由 `chartType` 补齐。`summary` 使用 T8 Syntax 字符串。 |
| `spec.config` | `ChartSpecInput['config']` | 是 | 原生 GPT-Vis 配置;省略 `config.type` 时由 `chartType` 补齐。`summary` 使用 GPT-Vis 原生摘要字符串。 |

`ChartType` 可选值:`line`、`column`、`bar`、`pie`、`area`、`scatter`、`dual-axes`、`histogram`、`boxplot`、`radar`、`funnel`、`waterfall`、`liquid`、`word-cloud`、`violin`、`venn`、`treemap`、`sankey`、`table`、`summary`。

GPT-Vis 支持 26 种可视化类型。AVA 自动推荐上述 19 种 JSON 图表,也允许通过 `viz({ chartType: 'summary', config: '# 销售报告' })` 渲染原生摘要字符串。6 种关系图暂不在 AVA 契约范围内。`ChartSpec` 描述规范化后的结果,其中 JSON 图表的 `config.type` 已补齐。
GPT-Vis 支持 26 种可视化类型。AVA 可以推荐上述 20 种图表。`summary` 的 `config` 为 GPT-Vis 原生摘要字符串。也可以通过 `viz({ chartType: 'summary', config: '# 销售报告' })` 渲染原生摘要字符串。6 种关系图暂不在 AVA 契约范围内。`ChartSpec` 描述规范化后的结果,其中 JSON 图表的 `config.type` 已补齐。

`spec.html?: string` 用于接收 `visualize()` 的完整返回值。渲染始终根据 `config` 生成新的 HTML,不复用已有 `html`;外层其他未知字段会被拒绝。

Expand Down
2 changes: 1 addition & 1 deletion site/content/docs/zh/visualization/overview.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,6 @@ if (spec) {
| 构成与流向 | `pie`, `treemap`, `sankey`, `waterfall`, `funnel` |
| 其他 | `radar`, `liquid`, `word-cloud`, `venn`, `table` |

上表列出 AVA 自动推荐的 19 种 JSON 图表。GPT-Vis 本身支持 26 种类型;AVA 还支持通过 `viz({ chartType: 'summary', config: '# 销售报告' })` 渲染原生摘要字符串,其余 6 种关系图暂不在 AVA 契约范围内。图表是否合适,取决于数据要求和用户问题。推荐模块会将 19 种图表的定义提供给模型。
AVA 可以推荐上表中的 19 种图表和 `summary`。摘要的 `config` 为 GPT-Vis 原生字符串。也可以通过 `viz({ chartType: 'summary', config: '# 销售报告' })` 直接渲染摘要。GPT-Vis 的 6 种关系图暂不在 AVA 契约范围内。图表是否合适,取决于数据要求和用户问题。

完整字段与图表类型见 [recommend 参数](/documentation/reference/api/#visualize-and-recommend)和 [viz 参数](/documentation/reference/api/#viz)。
6 changes: 3 additions & 3 deletions skills/ava/references/visualization.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# Visualization

Verified against the published `@antv/gpt-vis` **1.3.1** package (2026-11-09). Use the [official chart reference](https://gpt-vis.antv.vision/docs/charts) for native options. AVA accepts a JSON Spec; SDK Syntax strings and T8 Summary text are not CLI inputs.
Verified against the published `@antv/gpt-vis` **1.3.1** package (2026-11-09). Use the [official chart reference](https://gpt-vis.antv.vision/docs/charts) for native options. AVA accepts a JSON spec; use GPT-Vis summary text as `config` when `chartType` is `summary`.

## Spec and Field Mapping

A Spec has exactly two keys: `{ "chartType": "…", "config": {…} }`. Put the fields below directly in `config`; most types use `config.data` as an array of row objects. `value`, `x`, and `y` must be numbers. Map a second dimension to row field `group`.
A chart spec uses `{ "chartType": "…", "config": {…} }`. Put the fields below directly in `config`; most types use `config.data` as an array of row objects. `value`, `x`, and `y` must be numbers. Map a second dimension to row field `group`.

GPT-Vis supports 26 types; AVA currently accepts the 19 below. Its CLI does not register `flow-diagram`, `network-graph`, `mindmap`, `indented-tree`, `organization-chart`, `fishbone-diagram`, or `summary`.
AVA can recommend the 19 chart types below and `summary`, which uses `{ "chartType": "summary", "config": "# Sales report" }`.

| `chartType` | Data fields or structure |
| --- | --- |
Expand Down
2 changes: 1 addition & 1 deletion src/cli/commands/recommend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const help = `${description}.
Usage:
ava recommend --query "<text>" --data <json|@file|->

Returns { "chartType": "...", "config": {...} }, or null if no chart is recommended.
Returns { chartType, config }, or null if no chart is recommended.
A non-null result can be passed to ava viz --spec.

Options:
Expand Down
2 changes: 1 addition & 1 deletion src/cli/commands/viz.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const help = `${description}.
Usage:
ava viz --spec <json|@file|-> [--output <path>]

Spec must contain chartType and config (GPT-Vis JSON config), as returned by ava recommend.
Spec must contain chartType and config (a native GPT-Vis config), as returned by ava recommend.
Without --output, returns the validated { chartType, config } as JSON.
With --output, writes HTML and returns { "output": "<absolute-path>" }.
No API key is required.
Expand Down
9 changes: 6 additions & 3 deletions src/contracts/visualize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,12 @@ const chartSchemas = [
z
.object({
chartType: z.literal('summary'),
config: z.string().refine((text) => text.trim().length > 0 && !text.trim().startsWith('vis '), {
message: 'Summary requires nonempty T8 text, not vis chart syntax',
}),
config: z
.string()
.refine((text) => text.trim().length > 0 && !text.trim().startsWith('vis '), {
message: 'Summary requires nonempty GPT-Vis summary text',
})
.describe('GPT-Vis summary text with Markdown headings and paragraphs.'),
html: z.string().optional(),
})
.strict(),
Expand Down
35 changes: 12 additions & 23 deletions src/visualization/generator.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
/**
* GPT-Vis visualization generator
*
* Strategy: LLM only generates a GPT-Vis JSON config, then we wrap it
* into an HTML template locally. This avoids unreliable regex extraction
* from LLM-generated HTML and ensures the HTML is always well-formed.
* The model generates a GPT-Vis config. AVA wraps it in standalone HTML.
*/

import { generateText } from 'ai';
Expand All @@ -15,7 +13,7 @@ import { vizInputSchema } from '../contracts';
import type { LLMConfig, ChartType, ChartConfig } from '../types';

/**
* Wrap a GPT-Vis JSON config into a standalone HTML file that can be opened in any browser
* Wrap a native GPT-Vis config in standalone HTML.
*/
export function wrapConfigInHTML(config: ChartConfig): string {
// JSON.parse preserves __proto__ as data; escape script terminators and JS line separators.
Expand Down Expand Up @@ -56,16 +54,16 @@ export function wrapConfigInHTML(config: ChartConfig): string {
}

/**
* Generate a GPT-Vis JSON config for the chart type, data, and user query.
* Generate a config matching the selected GPT-Vis chart schema.
*/
export async function generateVisualizationConfig(
chartType: ChartType,
data: any,
data: unknown,
query: string,
llmConfig: LLMConfig
): Promise<Record<string, unknown>> {
): Promise<unknown> {
const chartSchema = vizInputSchema.options.find((schema) => schema.shape.chartType.value === chartType)!;
const prompt = `你是一个 GPT-Vis 可视化专家。根据图表类型、数据和用户查询,生成对应的 GPT-Vis JSON 配置。
const prompt = `你是一个 GPT-Vis 可视化专家。根据图表类型、数据和用户查询,生成对应的 GPT-Vis 配置。

## 任务信息

Expand All @@ -78,21 +76,18 @@ export async function generateVisualizationConfig(
${JSON.stringify(data, null, 2)}
\`\`\`

## JSON 配置规则
## 配置规则

输出一个 JSON 对象。数值字段必须是 number,不要带单位或百分号;保留原始数值,单位写在标题或坐标轴标题中。
输出一个符合以下 JSON Schema 的 JSON 值。
下面的 JSON Schema 来自 AVA 使用的原生图表输入契约,字段形状、主题与展示选项以它为准:

${JSON.stringify(z.toJSONSchema(chartSchema.shape.config, { io: 'input' }))}

## 要求

1. 只生成一个 JSON 对象,不要生成 HTML 或任何其他代码
2. \`type\` 必须是 ${chartType}
3. 数据字段映射必须正确,字段名必须与上述 schema 一致
4. 根据数据特征生成合适的标题,单位写在标题或坐标轴标题中
5. 不要生成 width height,图表会按照容器自适应大小
6. 直接输出 JSON,不要用代码块包裹,不要有任何说明文字`;
1. 只使用给定数据,保留数值和单位。数值字段必须是 number,单位可写在标题或标签中
2. 根据数据特征生成合适的标题,不要设置宽高
3. 直接输出 JSON,不要生成 HTML、代码块或说明文字`;

const { text } = await generateText({
model: languageModel(llmConfig),
Expand All @@ -106,11 +101,5 @@ ${JSON.stringify(z.toJSONSchema(chartSchema.shape.config, { io: 'input' }))}
.replace(/^```(?:json)?\s*\n?/i, '')
.replace(/\n?\s*```$/, '')
.trim();
const config = JSON.parse(json) as unknown;

if (typeof config !== 'object' || config === null || Array.isArray(config)) {
throw new Error('Generated GPT-Vis config must be a JSON object.');
}

return { ...(config as Record<string, unknown>), type: chartType };
return JSON.parse(json) as unknown;
}
8 changes: 8 additions & 0 deletions src/visualization/vis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,14 @@ const chartDefinitions: ChartTypeDefinition[] = [
dataRequirements: ['任意结构化数据'],
limitations: [],
},
{
type: 'summary',
name: '文本摘要',
features: ['概括数据结论和关键指标'],
useCases: ['明确请求可视化摘要或数据简报'],
dataRequirements: ['有可概括的数据或指标'],
limitations: ['普通问答或已明确指定其他图表的场景'],
},
];

export { chartDefinitions };
Loading