From 43b0f52e08c510f669d67ce4525c09a0ee2cb267 Mon Sep 17 00:00:00 2001 From: mengnankkkk Date: Sat, 14 Nov 2026 18:01:19 +0800 Subject: [PATCH] fix: add chart --- .../github/malonetalk/agent/AgentService.java | 11 +- .../agent/datasource/SqlExecutor.java | 9 + .../agent/tools/impls/ExecuteSqlTool.java | 20 +- .../agent/tools/impls/GenerateChartTool.java | 93 +++++++++ .../malonetalk/dto/ChatStreamEvent.java | 8 + .../malonetalk/model/bo/ChartPayloadBo.java | 25 +++ .../malonetalk/model/bo/ChartSpecBo.java | 26 +++ .../malonetalk/model/bo/QuerySnapshotBo.java | 25 +++ .../model/converter/ChartConverter.java | 96 +++++++++ .../malonetalk/service/ChartService.java | 189 ++++++++++++++++++ .../service/SqlPermissionService.java | 20 +- .../malonetalk/service/SqlQueryScope.java | 13 ++ .../model/converter/ChartConverterTest.java | 66 ++++++ data-agent-frontend/package.json | 2 +- data-agent-frontend/src/api/agent.ts | 12 ++ .../src/composables/useAgentChat.ts | 3 +- data-agent-frontend/src/utils/chartOption.ts | 76 +++++++ .../src/views/chat/components/ChatChart.vue | 104 ++++++++++ .../src/views/chat/components/ChatMessage.vue | 12 ++ .../tests/chartOption.test.mjs | 93 +++++++++ 20 files changed, 890 insertions(+), 13 deletions(-) create mode 100644 data-agent-backend/src/main/java/io/github/malonetalk/agent/tools/impls/GenerateChartTool.java create mode 100644 data-agent-backend/src/main/java/io/github/malonetalk/model/bo/ChartPayloadBo.java create mode 100644 data-agent-backend/src/main/java/io/github/malonetalk/model/bo/ChartSpecBo.java create mode 100644 data-agent-backend/src/main/java/io/github/malonetalk/model/bo/QuerySnapshotBo.java create mode 100644 data-agent-backend/src/main/java/io/github/malonetalk/model/converter/ChartConverter.java create mode 100644 data-agent-backend/src/main/java/io/github/malonetalk/service/ChartService.java create mode 100644 data-agent-backend/src/test/java/io/github/malonetalk/model/converter/ChartConverterTest.java create mode 100644 data-agent-frontend/src/utils/chartOption.ts create mode 100644 data-agent-frontend/src/views/chat/components/ChatChart.vue create mode 100644 data-agent-frontend/tests/chartOption.test.mjs diff --git a/data-agent-backend/src/main/java/io/github/malonetalk/agent/AgentService.java b/data-agent-backend/src/main/java/io/github/malonetalk/agent/AgentService.java index 558701c..18d7e4c 100644 --- a/data-agent-backend/src/main/java/io/github/malonetalk/agent/AgentService.java +++ b/data-agent-backend/src/main/java/io/github/malonetalk/agent/AgentService.java @@ -21,6 +21,7 @@ import io.agentscope.core.agent.EventType; import io.agentscope.core.agent.StreamOptions; import io.agentscope.core.memory.InMemoryMemory; +import io.agentscope.core.memory.Memory; import io.agentscope.core.message.ContentBlock; import io.agentscope.core.message.Msg; import io.agentscope.core.message.MsgRole; @@ -278,8 +279,12 @@ private Flux toErrorEvent(Throwable exception) { } private ReActAgent createAgent(ToolCallContext toolCallContext) { + var memory = new InMemoryMemory(); ToolExecutionContext context = - ToolExecutionContext.builder().register(toolCallContext).build(); + ToolExecutionContext.builder() + .register(toolCallContext) + .register(Memory.class, memory) + .build(); return ReActAgent.builder() .name("DataAgent") .sysPrompt(systemPrompt()) @@ -287,7 +292,7 @@ private ReActAgent createAgent(ToolCallContext toolCallContext) { .toolkit(toolkit) .toolExecutionContext(context) .skillBox(skillBox) - .memory(new InMemoryMemory()) + .memory(memory) .maxIters(10) .enablePendingToolRecovery(true) .build(); @@ -305,6 +310,8 @@ private String systemPrompt() { 先调用 get_datasources;调用 get_tables、get_table_schema、get_metric_caliber、 execute_sql 或 export_table 时,如果存在多个可选数据源,必须传 datasource_id。 不同数据源的同名表不能混用。 + 用户需要图表时,先执行 SQL 获取 Result ID,再调用 generate_chart,图表数值必须来自该查询结果。 + SQL 负责聚合与排序;绘图只指定字段映射。修改图表类型时可复用本会话已有的 Result ID。 """ .formatted(today, today.minusDays(1), today.plusDays(1)); } diff --git a/data-agent-backend/src/main/java/io/github/malonetalk/agent/datasource/SqlExecutor.java b/data-agent-backend/src/main/java/io/github/malonetalk/agent/datasource/SqlExecutor.java index 2cda0e7..2de0eae 100644 --- a/data-agent-backend/src/main/java/io/github/malonetalk/agent/datasource/SqlExecutor.java +++ b/data-agent-backend/src/main/java/io/github/malonetalk/agent/datasource/SqlExecutor.java @@ -89,6 +89,15 @@ public AuthorizedSqlBo authorizeSelect(DatasourcePo datasource, String sql, Inte return sqlPermissions.authorize(userId, datasource, parseAllowedSelect(sql)); } + public AuthorizedSqlBo authorizeSelect( + DatasourcePo datasource, + String sql, + Integer userId, + Map> previousColumns) { + return sqlPermissions.authorize( + userId, datasource, parseAllowedSelect(sql), previousColumns); + } + public String validateSelectSql(String sql) { parseAllowedSelect(sql); return trimTrailingSemicolon(sql); diff --git a/data-agent-backend/src/main/java/io/github/malonetalk/agent/tools/impls/ExecuteSqlTool.java b/data-agent-backend/src/main/java/io/github/malonetalk/agent/tools/impls/ExecuteSqlTool.java index 7555993..0305300 100644 --- a/data-agent-backend/src/main/java/io/github/malonetalk/agent/tools/impls/ExecuteSqlTool.java +++ b/data-agent-backend/src/main/java/io/github/malonetalk/agent/tools/impls/ExecuteSqlTool.java @@ -17,8 +17,6 @@ */ package io.github.malonetalk.agent.tools.impls; -import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.ObjectMapper; import io.agentscope.core.tool.Tool; import io.agentscope.core.tool.ToolParam; import io.github.malonetalk.agent.ToolCallContext; @@ -30,8 +28,10 @@ import io.github.malonetalk.agent.tools.ToolDatasourceResolver; import io.github.malonetalk.agent.tools.ToolExceptionMapper; import io.github.malonetalk.agent.tools.ToolResultBoConverter; +import io.github.malonetalk.model.bo.QuerySnapshotBo; import io.github.malonetalk.model.bo.SqlToolMetadataBo; import io.github.malonetalk.model.bo.ToolResultBo; +import io.github.malonetalk.model.converter.ChartConverter; import io.github.malonetalk.model.po.DatasourcePo; import java.util.Map; import lombok.AllArgsConstructor; @@ -44,7 +44,7 @@ public class ExecuteSqlTool implements DataAgentTool { private final ToolDatasourceResolver datasourceResolver; private final SqlExecutor sqlExecutor; private final ToolExceptionMapper toolExceptionMapper; - private final ObjectMapper objectMapper; + private final ChartConverter chartConverter; @RestTool @Tool( @@ -80,9 +80,17 @@ public ToolResultBo executeSql( .returnedRows(execution.result().rows().size()) .truncated(execution.result().truncated()) .build(); - Map resultMetadata = - objectMapper.convertValue(metadata, new TypeReference<>() {}); - return new ToolResultBo(formatResult(execution.result()), resultMetadata); + Map resultMetadata = chartConverter.toMetadata(metadata); + var snapshot = chartConverter.snapshot(sql, execution.result()); + snapshot.ifPresent( + value -> resultMetadata.put(QuerySnapshotBo.METADATA_KEY, value)); + String reference = + snapshot.map(value -> "Result ID: " + value.get("resultId") + "\n") + .orElse( + "Chart snapshot exceeds size limit. Select fewer" + + " columns or aggregate the SQL.\n"); + return new ToolResultBo( + reference + formatResult(execution.result()), resultMetadata); }); } diff --git a/data-agent-backend/src/main/java/io/github/malonetalk/agent/tools/impls/GenerateChartTool.java b/data-agent-backend/src/main/java/io/github/malonetalk/agent/tools/impls/GenerateChartTool.java new file mode 100644 index 0000000..5775446 --- /dev/null +++ b/data-agent-backend/src/main/java/io/github/malonetalk/agent/tools/impls/GenerateChartTool.java @@ -0,0 +1,93 @@ +/* + * Copyright (C) 2026 github.com/MaloneTalk + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * limitations under the License. + */ +package io.github.malonetalk.agent.tools.impls; + +import io.agentscope.core.memory.Memory; +import io.agentscope.core.tool.Tool; +import io.agentscope.core.tool.ToolParam; +import io.github.malonetalk.agent.ToolCallContext; +import io.github.malonetalk.agent.tools.DataAgentTool; +import io.github.malonetalk.agent.tools.ToolExceptionMapper; +import io.github.malonetalk.agent.tools.ToolResultBoConverter; +import io.github.malonetalk.model.bo.ChartPayloadBo; +import io.github.malonetalk.model.bo.ChartSpecBo; +import io.github.malonetalk.model.bo.ToolResultBo; +import io.github.malonetalk.model.converter.ChartConverter; +import io.github.malonetalk.service.ChartService; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Component; + +@Component +@RequiredArgsConstructor +public class GenerateChartTool implements DataAgentTool { + private final ChartService chartService; + private final ChartConverter converter; + private final ToolExceptionMapper toolExceptionMapper; + + @Tool( + name = "generate_chart", + converter = ToolResultBoConverter.class, + description = + """ + 根据本会话 execute_sql 的真实查询结果生成交互图表,图表会直接展示在聊天中。 + 必须先执行 SQL 并获得 Result ID,再调用本工具;只填写图表类型和字段映射,不传数据或代码。 + SQL 应先完成聚合和 ORDER BY;截断、空结果或重复维度需要先调整 SQL。 + 支持 bar、line、pie、scatter。折线图和柱状图支持多个指标及可选分组。 + 饼图仅支持一个非负指标且不分组;散点图要求横纵轴均为数值。 + 改图时可复用同一个 Result ID。成功后无需重复输出图表配置或数据。 + """) + public ToolResultBo generateChart( + @ToolParam(name = "result_id", description = "Result ID returned by execute_sql") + String resultId, + @ToolParam(name = "chart_type", description = "bar, line, pie or scatter") + String chartType, + @ToolParam(name = "title", description = "Short chart title") String title, + @ToolParam( + name = "x_field", + description = "Query column for the category or scatter x axis") + String xField, + @ToolParam(name = "y_fields", description = "Numeric query columns for measures") + List yFields, + @ToolParam( + name = "series_field", + description = "Optional query column for grouping", + required = false) + String seriesField, + Memory memory, + ToolCallContext context) { + return toolExceptionMapper.runResult( + () -> { + var spec = + ChartSpecBo.builder() + .chartType(chartType) + .title(title) + .xField(xField) + .yFields(yFields) + .seriesField(Optional.ofNullable(seriesField).orElse("")) + .build(); + var chart = + chartService.generate(resultId, spec, memory.getMessages(), context); + return new ToolResultBo( + "Chart generated: " + title, + Map.of(ChartPayloadBo.METADATA_KEY, converter.toMetadata(chart))); + }); + } +} diff --git a/data-agent-backend/src/main/java/io/github/malonetalk/dto/ChatStreamEvent.java b/data-agent-backend/src/main/java/io/github/malonetalk/dto/ChatStreamEvent.java index fbbeda1..021d233 100644 --- a/data-agent-backend/src/main/java/io/github/malonetalk/dto/ChatStreamEvent.java +++ b/data-agent-backend/src/main/java/io/github/malonetalk/dto/ChatStreamEvent.java @@ -19,7 +19,9 @@ import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; import io.github.malonetalk.enums.ChatStreamEventType; +import io.github.malonetalk.model.bo.ChartPayloadBo; import java.util.Map; import lombok.Builder; @@ -46,5 +48,11 @@ public record ToolResultInfo( public ToolResultInfo(String id, String name, String output, boolean suspended) { this(id, name, output, suspended, Map.of()); } + + @JsonProperty("chart") + @JsonInclude(JsonInclude.Include.NON_NULL) + public Object chart() { + return metadata.get(ChartPayloadBo.METADATA_KEY); + } } } diff --git a/data-agent-backend/src/main/java/io/github/malonetalk/model/bo/ChartPayloadBo.java b/data-agent-backend/src/main/java/io/github/malonetalk/model/bo/ChartPayloadBo.java new file mode 100644 index 0000000..36abe11 --- /dev/null +++ b/data-agent-backend/src/main/java/io/github/malonetalk/model/bo/ChartPayloadBo.java @@ -0,0 +1,25 @@ +/* + * Copyright (C) 2026 github.com/MaloneTalk + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * limitations under the License. + */ +package io.github.malonetalk.model.bo; + +import java.util.List; +import java.util.Map; + +public record ChartPayloadBo(ChartSpecBo spec, List> rows) { + public static final String METADATA_KEY = "chart"; +} diff --git a/data-agent-backend/src/main/java/io/github/malonetalk/model/bo/ChartSpecBo.java b/data-agent-backend/src/main/java/io/github/malonetalk/model/bo/ChartSpecBo.java new file mode 100644 index 0000000..b1fe32a --- /dev/null +++ b/data-agent-backend/src/main/java/io/github/malonetalk/model/bo/ChartSpecBo.java @@ -0,0 +1,26 @@ +/* + * Copyright (C) 2026 github.com/MaloneTalk + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * limitations under the License. + */ +package io.github.malonetalk.model.bo; + +import java.util.List; +import lombok.Builder; + +/** Only field mappings are supplied by the model; values come from a query snapshot. */ +@Builder +public record ChartSpecBo( + String chartType, String title, String xField, List yFields, String seriesField) {} diff --git a/data-agent-backend/src/main/java/io/github/malonetalk/model/bo/QuerySnapshotBo.java b/data-agent-backend/src/main/java/io/github/malonetalk/model/bo/QuerySnapshotBo.java new file mode 100644 index 0000000..9b944e4 --- /dev/null +++ b/data-agent-backend/src/main/java/io/github/malonetalk/model/bo/QuerySnapshotBo.java @@ -0,0 +1,25 @@ +/* + * Copyright (C) 2026 github.com/MaloneTalk + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * limitations under the License. + */ +package io.github.malonetalk.model.bo; + +import io.github.malonetalk.agent.datasource.QueryResult; + +/** Query data retained in tool metadata and restored with the conversation. */ +public record QuerySnapshotBo(String resultId, String originalSql, QueryResult result) { + public static final String METADATA_KEY = "querySnapshot"; +} diff --git a/data-agent-backend/src/main/java/io/github/malonetalk/model/converter/ChartConverter.java b/data-agent-backend/src/main/java/io/github/malonetalk/model/converter/ChartConverter.java new file mode 100644 index 0000000..8e6c7af --- /dev/null +++ b/data-agent-backend/src/main/java/io/github/malonetalk/model/converter/ChartConverter.java @@ -0,0 +1,96 @@ +/* + * Copyright (C) 2026 github.com/MaloneTalk + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * limitations under the License. + */ +package io.github.malonetalk.model.converter; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import io.github.malonetalk.agent.datasource.QueryResult; +import io.github.malonetalk.model.bo.ChartPayloadBo; +import io.github.malonetalk.model.bo.QuerySnapshotBo; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.UUID; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Component; +import org.springframework.util.StringUtils; + +@Component +@RequiredArgsConstructor +public class ChartConverter { + private static final int MAX_SNAPSHOT_BYTES = 256 * 1024; + private final ObjectMapper objectMapper; + + public Optional> snapshot(String sql, QueryResult result) + throws JsonProcessingException { + List> rows = result.rows().stream().map(this::jsonRow).toList(); + var snapshot = + new QuerySnapshotBo( + UUID.randomUUID().toString(), + sql, + new QueryResult( + result.columns(), rows, result.totalRows(), result.truncated())); + Map value = toMetadata(snapshot); + return objectMapper.writeValueAsBytes(value).length <= MAX_SNAPSHOT_BYTES + ? Optional.of(value) + : Optional.empty(); + } + + public Map toMetadata(Object value) { + return objectMapper.convertValue(value, new TypeReference<>() {}); + } + + public Map toMetadata(ChartPayloadBo chart) { + var spec = chart.spec(); + List> rows = + chart.rows().stream() + .map( + row -> { + Map converted = new LinkedHashMap<>(row); + if (!"scatter".equals(spec.chartType())) { + converted.computeIfPresent( + spec.xField(), (field, value) -> value.toString()); + } + if (StringUtils.hasText(spec.seriesField())) { + converted.computeIfPresent( + spec.seriesField(), + (field, value) -> value.toString()); + } + return converted; + }) + .toList(); + return toMetadata((Object) new ChartPayloadBo(spec, rows)); + } + + private Map jsonRow(Map row) { + Map converted = new LinkedHashMap<>(); + row.forEach( + (field, value) -> + converted.put( + field, + value == null + || value instanceof String + || value instanceof Number + || value instanceof Boolean + ? value + : value.toString())); + return converted; + } +} diff --git a/data-agent-backend/src/main/java/io/github/malonetalk/service/ChartService.java b/data-agent-backend/src/main/java/io/github/malonetalk/service/ChartService.java new file mode 100644 index 0000000..52e6deb --- /dev/null +++ b/data-agent-backend/src/main/java/io/github/malonetalk/service/ChartService.java @@ -0,0 +1,189 @@ +/* + * Copyright (C) 2026 github.com/MaloneTalk + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * limitations under the License. + */ +package io.github.malonetalk.service; + +import com.fasterxml.jackson.databind.ObjectMapper; +import io.agentscope.core.message.ContentBlock; +import io.agentscope.core.message.Msg; +import io.agentscope.core.message.MsgRole; +import io.agentscope.core.message.ToolResultBlock; +import io.github.malonetalk.agent.ToolCallContext; +import io.github.malonetalk.agent.datasource.QueryResult; +import io.github.malonetalk.agent.datasource.SqlExecutor; +import io.github.malonetalk.agent.tools.ToolDatasourceResolver; +import io.github.malonetalk.exception.BusinessException; +import io.github.malonetalk.exception.ErrorCode; +import io.github.malonetalk.model.bo.ChartPayloadBo; +import io.github.malonetalk.model.bo.ChartSpecBo; +import io.github.malonetalk.model.bo.QuerySnapshotBo; +import io.github.malonetalk.model.bo.SqlToolMetadataBo; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Set; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.util.CollectionUtils; +import org.springframework.util.StringUtils; + +@Service +@RequiredArgsConstructor +public class ChartService { + private static final Set CHART_TYPES = Set.of("bar", "line", "pie", "scatter"); + private static final int MAX_SERIES = 20; + private final ToolDatasourceResolver datasourceResolver; + private final SqlExecutor sqlExecutor; + private final ObjectMapper objectMapper; + + public ChartPayloadBo generate( + String resultId, ChartSpecBo spec, List messages, ToolCallContext context) { + validateSpec(resultId, spec); + ToolResultBlock source = findResult(resultId, messages); + var snapshot = + objectMapper.convertValue( + source.getMetadata().get(QuerySnapshotBo.METADATA_KEY), + QuerySnapshotBo.class); + var metadata = objectMapper.convertValue(source.getMetadata(), SqlToolMetadataBo.class); + var datasource = datasourceResolver.resolve(context, metadata.datasourceId()); + sqlExecutor.authorizeSelect( + datasource, + snapshot.originalSql(), + context.userId(), + Optional.ofNullable(metadata.authorizedColumns()) + .orElseThrow(() -> BusinessException.of(ErrorCode.FORBIDDEN))); + validateData(spec, snapshot.result()); + return new ChartPayloadBo(spec, snapshot.result().rows()); + } + + private ToolResultBlock findResult(String resultId, List messages) { + for (Msg message : messages) { + if (message.getRole() != MsgRole.TOOL) continue; + for (ContentBlock block : message.getContent()) { + if (block instanceof ToolResultBlock result + && "execute_sql".equals(result.getName()) + && result.getMetadata().get(QuerySnapshotBo.METADATA_KEY) + instanceof Map snapshot + && resultId.equals(snapshot.get("resultId"))) { + return result; + } + } + } + throw BusinessException.of( + ErrorCode.BAD_REQUEST, + "Query result not found in this conversation. Execute the SQL first and use its" + + " Result ID."); + } + + private void validateSpec(String resultId, ChartSpecBo spec) { + if (!StringUtils.hasText(resultId) + || !StringUtils.hasText(spec.chartType()) + || !CHART_TYPES.contains(spec.chartType()) + || !StringUtils.hasText(spec.title()) + || !StringUtils.hasText(spec.xField()) + || CollectionUtils.isEmpty(spec.yFields()) + || spec.yFields().stream().anyMatch(field -> !StringUtils.hasText(field))) { + throw invalid( + "Specify a result_id, chart_type (bar/line/pie/scatter), title, x_field and" + + " y_fields."); + } + if (new HashSet<>(spec.yFields()).size() != spec.yFields().size()) { + throw invalid("y_fields must not contain duplicate fields."); + } + if ("pie".equals(spec.chartType()) + && (spec.yFields().size() != 1 || StringUtils.hasText(spec.seriesField()))) { + throw invalid("Pie charts require exactly one y field and no series_field."); + } + } + + private void validateData(ChartSpecBo spec, QueryResult result) { + if (result.truncated()) + throw invalid("Query result is truncated. Aggregate or filter the SQL first."); + if (result.rows().isEmpty()) throw invalid("Query result is empty."); + var fields = new HashSet<>(spec.yFields()); + fields.add(spec.xField()); + if (StringUtils.hasText(spec.seriesField())) fields.add(spec.seriesField()); + if (!result.columns().containsAll(fields)) + throw invalid("Chart fields must exist in the query result."); + + for (String field : spec.yFields()) validateNumbers(result, spec, field); + Set> dimensions = new HashSet<>(); + Set groups = new HashSet<>(); + for (Map row : result.rows()) { + Object x = + Optional.ofNullable(row.get(spec.xField())) + .orElseThrow( + () -> + invalid( + "x_field contains null. Handle missing" + + " dimensions in SQL.")); + Object group = + StringUtils.hasText(spec.seriesField()) + ? Optional.ofNullable(row.get(spec.seriesField())) + .orElseThrow( + () -> + invalid( + "series_field contains null. Handle it" + + " in SQL.")) + : ""; + groups.add(group); + if ((long) groups.size() * spec.yFields().size() > MAX_SERIES) { + throw invalid( + "Charts support at most " + + MAX_SERIES + + " series. Reduce y_fields or filter series_field in SQL."); + } + if ("scatter".equals(spec.chartType())) { + if (!(x instanceof Number number) || !Double.isFinite(number.doubleValue())) { + throw invalid("Scatter charts require a numeric x_field."); + } + } else if (!dimensions.add(List.of(x, group))) { + throw invalid( + "Duplicate chart dimensions. Aggregate by x_field and series_field in" + + " SQL."); + } + } + } + + private void validateNumbers(QueryResult result, ChartSpecBo spec, String field) { + boolean hasUsableValue = false; + boolean pie = "pie".equals(spec.chartType()); + for (Map row : result.rows()) { + Optional value = Optional.ofNullable(row.get(field)); + if (value.isEmpty() && Set.of("bar", "line").contains(spec.chartType())) continue; + Number number = + value.filter(Number.class::isInstance) + .map(Number.class::cast) + .filter(candidate -> Double.isFinite(candidate.doubleValue())) + .orElseThrow(() -> invalid("Chart measure must be numeric: " + field)); + if (pie && number.doubleValue() < 0) { + throw invalid("Pie charts cannot contain negative values."); + } + hasUsableValue |= !pie || number.doubleValue() > 0; + } + if (!hasUsableValue) + throw invalid( + pie + ? "Pie charts require at least one positive value." + : "Chart measure has no numeric values: " + field); + } + + private BusinessException invalid(String message) { + return BusinessException.of(ErrorCode.BAD_REQUEST, message); + } +} diff --git a/data-agent-backend/src/main/java/io/github/malonetalk/service/SqlPermissionService.java b/data-agent-backend/src/main/java/io/github/malonetalk/service/SqlPermissionService.java index d2246b9..f939ccd 100644 --- a/data-agent-backend/src/main/java/io/github/malonetalk/service/SqlPermissionService.java +++ b/data-agent-backend/src/main/java/io/github/malonetalk/service/SqlPermissionService.java @@ -58,6 +58,14 @@ public class SqlPermissionService { private final ColumnSemanticInfoMapper metadata; public AuthorizedSqlBo authorize(Integer userId, DatasourcePo datasource, Select select) { + return authorize(userId, datasource, select, Map.of()); + } + + public AuthorizedSqlBo authorize( + Integer userId, + DatasourcePo datasource, + Select select, + Map> previousColumns) { var user = permissions.require(userId, PermissionEnum.ANALYSIS_SQL_EXECUTE); var scope = data.loadScope(user, datasource.getId()); if (scope.unrestricted()) { @@ -69,7 +77,7 @@ public AuthorizedSqlBo authorize(Integer userId, DatasourcePo datasource, Select } if (scope.tables().isEmpty()) throw denied(); try { - Inspector inspector = new Inspector(scope, datasource); + Inspector inspector = new Inspector(scope, datasource, previousColumns); inspector.inspect(select, Optional.empty()); return new AuthorizedSqlBo(select, columnSnapshot(inspector.visibleColumns)); } catch (BusinessException ex) { @@ -99,10 +107,15 @@ private class Inspector { private final DataPermissionScopeBo scope; private final Integer datasourceId; private final String identifierQuote; + private final Map> previousColumns; private final Map> visibleColumns = new HashMap<>(); - Inspector(DataPermissionScopeBo scope, DatasourcePo datasource) { + Inspector( + DataPermissionScopeBo scope, + DatasourcePo datasource, + Map> previousColumns) { this.scope = scope; + this.previousColumns = previousColumns; this.datasourceId = datasource.getId(); var type = DataSourceType.fromCode(datasource.getType()) @@ -236,7 +249,8 @@ private FromItem project(FromItem source, SqlQueryScope query) { query.addSource( table.getAlias() == null ? table.getName() : table.getAlias().getName(), columns.stream().map(ColumnInfo::getColumnName).collect(Collectors.toSet()), - scope.hidden(name)); + scope.hidden(name), + previousColumns.getOrDefault(normalizeIdentifier(name), Set.of())); String projection = columns.stream() .map(c -> quote(c.getColumnName())) diff --git a/data-agent-backend/src/main/java/io/github/malonetalk/service/SqlQueryScope.java b/data-agent-backend/src/main/java/io/github/malonetalk/service/SqlQueryScope.java index f40826c..0dc25d5 100644 --- a/data-agent-backend/src/main/java/io/github/malonetalk/service/SqlQueryScope.java +++ b/data-agent-backend/src/main/java/io/github/malonetalk/service/SqlQueryScope.java @@ -22,6 +22,7 @@ import io.github.malonetalk.exception.BusinessException; import io.github.malonetalk.exception.ErrorCode; import java.util.HashMap; +import java.util.HashSet; import java.util.LinkedHashSet; import java.util.Map; import java.util.Optional; @@ -42,13 +43,23 @@ final class SqlQueryScope { private final Optional parent; // Each source maps its column names to whether the column is accessible. private final Map> sources = new HashMap<>(); + // Saved wildcard results must reject columns revoked from that source since execution. + private final Set narrowedSources = new HashSet<>(); final Set outputColumns = new LinkedHashSet<>(); void addSource(String alias, Set visible, Set hidden) { + addSource(alias, visible, hidden, Set.of()); + } + + void addSource(String alias, Set visible, Set hidden, Set previous) { Map columns = new HashMap<>(); visible.forEach(column -> columns.put(normalizeIdentifier(column), true)); hidden.forEach(column -> columns.put(normalizeIdentifier(column), false)); if (sources.putIfAbsent(normalizeIdentifier(alias), columns) != null) throw denied(); + if (previous.stream() + .anyMatch(column -> !columns.getOrDefault(normalizeIdentifier(column), false))) { + narrowedSources.add(normalizeIdentifier(alias)); + } } void requireColumn(Column column) { @@ -114,11 +125,13 @@ void addOutput(SelectItem item) { } outputColumns.add(normalizeIdentifier(alias)); } else if (expression instanceof AllTableColumns all) { + if (narrowedSources.contains(tableName(all.getTable()))) throw denied(); var columns = Optional.ofNullable(sources.get(tableName(all.getTable()))) .orElseThrow(this::denied); addVisibleOutputs(columns); } else if (expression instanceof AllColumns) { + if (!narrowedSources.isEmpty()) throw denied(); sources.values().forEach(this::addVisibleOutputs); } else if (expression instanceof Column column) { outputColumns.add(normalizeIdentifier(column.getColumnName())); diff --git a/data-agent-backend/src/test/java/io/github/malonetalk/model/converter/ChartConverterTest.java b/data-agent-backend/src/test/java/io/github/malonetalk/model/converter/ChartConverterTest.java new file mode 100644 index 0000000..f21c225 --- /dev/null +++ b/data-agent-backend/src/test/java/io/github/malonetalk/model/converter/ChartConverterTest.java @@ -0,0 +1,66 @@ +/* + * Copyright (C) 2026 github.com/MaloneTalk + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * limitations under the License. + */ +package io.github.malonetalk.model.converter; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fasterxml.jackson.databind.ObjectMapper; +import io.github.malonetalk.model.bo.ChartPayloadBo; +import io.github.malonetalk.model.bo.ChartSpecBo; +import java.math.BigInteger; +import java.util.List; +import java.util.Map; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +class ChartConverterTest { + @ParameterizedTest + @ValueSource(strings = {"bar", "line", "pie", "scatter"}) + void preservesLargeDimensionsThroughJsonWithoutMutatingSource(String chartType) + throws Exception { + var mapper = new ObjectMapper(); + var group = new BigInteger("18446744073709551615"); + var rows = + List.>of( + Map.of("id", 9007199254740992L, "group", group, "amount", 10), + Map.of("id", 9007199254740993L, "group", group, "amount", 20)); + var spec = + ChartSpecBo.builder() + .chartType(chartType) + .title("Amounts") + .xField("id") + .yFields(List.of("amount")) + .seriesField("pie".equals(chartType) ? "" : "group") + .build(); + var metadata = new ChartConverter(mapper).toMetadata(new ChartPayloadBo(spec, rows)); + var restored = mapper.readValue(mapper.writeValueAsString(metadata), ChartPayloadBo.class); + + for (int i = 0; i < rows.size(); i++) { + var expectedId = rows.get(i).get("id"); + assertEquals( + "scatter".equals(chartType) ? expectedId : expectedId.toString(), + restored.rows().get(i).get("id")); + if (!"pie".equals(chartType)) { + assertEquals(group.toString(), restored.rows().get(i).get("group")); + } + assertEquals(rows.get(i).get("amount"), restored.rows().get(i).get("amount")); + assertEquals(9007199254740992L + i, rows.get(i).get("id")); + assertEquals(group, rows.get(i).get("group")); + } + } +} diff --git a/data-agent-frontend/package.json b/data-agent-frontend/package.json index efab9bf..879fab6 100644 --- a/data-agent-frontend/package.json +++ b/data-agent-frontend/package.json @@ -4,7 +4,7 @@ "version": "0.0.0", "type": "module", "scripts": { - "test": "node --test tests/roleDataScope.test.mjs", + "test": "node --test", "dev": "vite", "build": "vue-tsc -b && vite build", "preview": "vite preview", diff --git a/data-agent-frontend/src/api/agent.ts b/data-agent-frontend/src/api/agent.ts index 8a079c9..4c08666 100644 --- a/data-agent-frontend/src/api/agent.ts +++ b/data-agent-frontend/src/api/agent.ts @@ -37,6 +37,18 @@ export interface ToolResultInfo { id: string; name: string; output: string; + chart?: ChartPayload | null; +} + +export interface ChartPayload { + spec: { + chartType: 'bar' | 'line' | 'pie' | 'scatter'; + title: string; + xField: string; + yFields: string[]; + seriesField: string; + }; + rows: Record[]; } export interface AnswerProvenance { diff --git a/data-agent-frontend/src/composables/useAgentChat.ts b/data-agent-frontend/src/composables/useAgentChat.ts index dd57aa1..f18d205 100644 --- a/data-agent-frontend/src/composables/useAgentChat.ts +++ b/data-agent-frontend/src/composables/useAgentChat.ts @@ -21,6 +21,7 @@ import { fetchSessionHistory, createSession, type ChatStreamEventType, + type ToolResultInfo, } from '@/api/agent'; import { INTERACTIVE_TOOLS, isInteractiveTool } from '@/utils/interactiveTools'; @@ -34,7 +35,7 @@ export interface TraceStep { type: ChatStreamEventType; content: string | null; toolCall: { id: string; name: string; input: Record } | null; - toolResult: { id: string; name: string; output: string } | null; + toolResult: ToolResultInfo | null; errorCode: string | null; } diff --git a/data-agent-frontend/src/utils/chartOption.ts b/data-agent-frontend/src/utils/chartOption.ts new file mode 100644 index 0000000..2c1372b --- /dev/null +++ b/data-agent-frontend/src/utils/chartOption.ts @@ -0,0 +1,76 @@ +/* + * Copyright (C) 2026 github.com/MaloneTalk + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or any later version. + * + * This program is distributed in the hope that it will be useful + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import type { EChartsOption, SeriesOption } from 'echarts'; +import type { ChartPayload } from '@/api/agent'; + +export function buildChartOption({ spec, rows }: ChartPayload): EChartsOption { + const { chartType, xField, yFields, seriesField } = spec; + const common: EChartsOption = { + aria: { enabled: true }, + tooltip: { trigger: 'item', renderMode: 'richText' }, + legend: { type: 'scroll', top: 0 }, + }; + + if (chartType === 'pie') { + return { + ...common, + series: [ + { + type: 'pie', + radius: ['30%', '65%'], + data: rows.map(row => ({ name: String(row[xField]), value: Number(row[yFields[0]]) })), + }, + ], + }; + } + + const categories = [...new Set(rows.map(row => String(row[xField])))]; + const groups = new Map(); + for (const row of rows) { + const group = seriesField ? String(row[seriesField]) : ''; + if (!groups.has(group)) groups.set(group, []); + groups.get(group)!.push(row); + } + + const series: SeriesOption[] = [...groups].flatMap(([group, groupRows]) => { + const byCategory = new Map(groupRows.map(row => [String(row[xField]), row])); + return yFields.map(field => ({ + type: chartType, + name: seriesField ? `${group} · ${field}` : field, + data: + chartType === 'scatter' + ? groupRows.map(row => [Number(row[xField]), Number(row[field])]) + : categories.map(category => { + const value = byCategory.get(category)?.[field]; + return value == null ? null : Number(value); + }), + })); + }); + + return { + ...common, + tooltip: { trigger: chartType === 'scatter' ? 'item' : 'axis', renderMode: 'richText' }, + grid: { left: 20, right: 20, top: 45, bottom: 20, containLabel: true }, + xAxis: + chartType === 'scatter' + ? { type: 'value', name: xField } + : { type: 'category', data: categories, axisLabel: { hideOverlap: true } }, + yAxis: { type: 'value', name: yFields.length === 1 ? yFields[0] : undefined }, + series, + }; +} diff --git a/data-agent-frontend/src/views/chat/components/ChatChart.vue b/data-agent-frontend/src/views/chat/components/ChatChart.vue new file mode 100644 index 0000000..9accd44 --- /dev/null +++ b/data-agent-frontend/src/views/chat/components/ChatChart.vue @@ -0,0 +1,104 @@ + + + + + + + diff --git a/data-agent-frontend/src/views/chat/components/ChatMessage.vue b/data-agent-frontend/src/views/chat/components/ChatMessage.vue index e50db08..1e1e95a 100644 --- a/data-agent-frontend/src/views/chat/components/ChatMessage.vue +++ b/data-agent-frontend/src/views/chat/components/ChatMessage.vue @@ -20,6 +20,8 @@ import { marked } from 'marked'; import { Check, CopyDocument } from '@element-plus/icons-vue'; import type { ChatMessage as ChatMessageType } from '@/composables/useAgentChat'; + import type { ChartPayload } from '@/api/agent'; + import ChatChart from './ChatChart.vue'; import ChatFileDownload from './ChatFileDownload.vue'; import TracePanel from './TracePanel.vue'; @@ -33,6 +35,15 @@ const SUMMARY_MARKER = '\n\nSummary:\n'; + const charts = computed(() => { + const results = new Map(); + for (const step of props.message.traceSteps) { + const result = step.toolResult; + if (result?.name === 'generate_chart' && result.chart) results.set(result.id, result.chart); + } + return [...results].map(([id, chart]) => ({ id, chart })); + }); + const contentParts = computed(() => { const idx = props.message.content.indexOf(SUMMARY_MARKER); if (idx === -1) { @@ -104,6 +115,7 @@ @preview-report="c => emit('previewReport', c)" />
+
diff --git a/data-agent-frontend/tests/chartOption.test.mjs b/data-agent-frontend/tests/chartOption.test.mjs new file mode 100644 index 0000000..820934c --- /dev/null +++ b/data-agent-frontend/tests/chartOption.test.mjs @@ -0,0 +1,93 @@ +/* + * Copyright (C) 2026 github.com/MaloneTalk + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or any later version. + * + * This program is distributed in the hope that it will be useful + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import assert from 'node:assert/strict'; +import { readFileSync } from 'node:fs'; +import { test } from 'node:test'; +import { URL } from 'node:url'; +import ts from 'typescript'; + +const source = readFileSync(new URL('../src/utils/chartOption.ts', import.meta.url), 'utf8'); +const { outputText } = ts.transpileModule(source, { + compilerOptions: { module: ts.ModuleKind.ESNext, target: ts.ScriptTarget.ES2020 }, +}); +const { buildChartOption } = await import(`data:text/javascript,${encodeURIComponent(outputText)}`); +const spec = { + chartType: 'bar', + title: 'Amounts', + xField: 'id', + yFields: ['amount'], + seriesField: '', +}; + +test('empty groups have separate legends from groups named after a measure', () => { + const option = buildChartOption({ + spec: { ...spec, seriesField: 'group' }, + rows: [ + { id: 'Jan', group: '', amount: 10 }, + { id: 'Jan', group: 'amount', amount: 20 }, + ], + }); + assert.deepEqual( + option.series.map(series => series.name), + [' · amount', 'amount · amount'], + ); + assert.deepEqual( + option.series.map(series => series.data), + [[10], [20]], + ); +}); + +test('category charts retain large identifiers and gaps between grouped line values', () => { + const option = buildChartOption({ + spec: { ...spec, chartType: 'line', seriesField: 'group' }, + rows: [ + { id: '9007199254740992', group: 'A', amount: 10 }, + { id: '9007199254740993', group: 'B', amount: 20 }, + { id: '9007199254740994', group: 'A', amount: null }, + ], + }); + assert.deepEqual(option.xAxis.data, ['9007199254740992', '9007199254740993', '9007199254740994']); + assert.deepEqual( + option.series.map(series => series.data), + [ + [10, null, null], + [null, 20, null], + ], + ); +}); + +test('fields shared by dimensions and measures still render numeric values', () => { + const rows = [{ id: '10' }, { id: '20' }]; + for (const chartType of ['bar', 'line', 'pie', 'scatter']) { + const option = buildChartOption({ spec: { ...spec, chartType, yFields: ['id'] }, rows }); + assert.deepEqual( + option.series[0].data, + chartType === 'pie' + ? [ + { name: '10', value: 10 }, + { name: '20', value: 20 }, + ] + : chartType === 'scatter' + ? [ + [10, 10], + [20, 20], + ] + : [10, 20], + ); + } +});