Skip to content
Closed
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

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@

import io.agentscope.core.tool.Tool;
import io.agentscope.core.tool.ToolParam;
import io.github.malonetalk.agent.datasource.QueryResult;
import io.github.malonetalk.agent.datasource.SqlExecutor;
import io.github.malonetalk.agent.datasource.SqlExecutor.SqlExecutionException;
import io.github.malonetalk.agent.datasource.SqlExecutor.SqlSecurityException;
import io.github.malonetalk.common.QueryResult;
import io.github.malonetalk.entity.Datasource;
import io.github.malonetalk.enums.Status;
import io.github.malonetalk.infrastructure.SqlExecutor;
import io.github.malonetalk.infrastructure.SqlExecutor.SqlExecutionException;
import io.github.malonetalk.infrastructure.SqlExecutor.SqlSecurityException;
import io.github.malonetalk.service.DatasourceService;
import java.util.List;
import lombok.AllArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,20 @@

import io.agentscope.core.tool.Tool;
import io.agentscope.core.tool.ToolParam;
import io.github.malonetalk.agent.datasource.ColumnInfo;
import io.github.malonetalk.agent.datasource.SchemaReader;
import io.github.malonetalk.agent.datasource.SchemaReader.SchemaReadException;
import io.github.malonetalk.entity.Datasource;
import io.github.malonetalk.enums.Status;
import io.github.malonetalk.service.DatasourceService;
import java.util.List;
import lombok.AllArgsConstructor;
import io.github.malonetalk.common.ToolResult;
import io.github.malonetalk.dto.PageResponse;
import io.github.malonetalk.dto.semantic.TableSchemaSemanticPrompt;
import io.github.malonetalk.service.semantic.SemanticMergeService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@AllArgsConstructor
@RequiredArgsConstructor
public class GetTableSchemaTool implements MarkAgentTool {

private final DatasourceService dataSourceService;
private final SchemaReader schemaReader;
private final SemanticMergeService semanticMergeService;

@Tool(
name = "get_table_schema",
Expand All @@ -45,43 +41,30 @@ public class GetTableSchemaTool implements MarkAgentTool {
+ " type, whether it is primary key, whether it allows null, default value"
+ " and column comments. This tool should be called to understand the table"
+ " structure before generating SQL.")
public String getTableSchema(
public ToolResult<TableSchemaSemanticPrompt> getTableSchema(
@ToolParam(name = "table_name", description = "The table name to query schema for")
String tableName) {
List<Datasource> activeDataSources =
dataSourceService.findByStatus(Status.ACTIVE.getCode());

if (activeDataSources.isEmpty()) {
return "No active datasource available, cannot get table schema.";
}

if (activeDataSources.size() > 1) {
log.warn(
"Found {} active data sources, using the first one.", activeDataSources.size());
}

Datasource datasource = activeDataSources.get(0);

String tableName,
@ToolParam(
name = "column_page",
description = "Optional column page number, default is 1")
Integer columnPage,
@ToolParam(
name = "column_page_size",
description =
"Optional column page size, default is 20, maximum is 100")
Integer columnPageSize) {
try {
List<ColumnInfo> columns = schemaReader.getTableSchema(datasource, tableName);
return formatSchema(tableName, columns);
} catch (SchemaReadException e) {
return "Failed to get table schema: " + e.getMessage();
int resolvedPage = PageResponse.resolvePage(columnPage);
int resolvedPageSize = PageResponse.resolvePageSize(columnPageSize);
return ToolResult.success(
semanticMergeService.getTableSchema(tableName, resolvedPage, resolvedPageSize));
} catch (IllegalStateException e) {
return ToolResult.error("Data source parsing failed", e.getMessage());
} catch (IllegalArgumentException e) {
return ToolResult.error("Invalid arguments", e.getMessage());
} catch (RuntimeException e) {
log.error("Failed to get schema for table {}: {}", tableName, e.getMessage(), e);
return ToolResult.error("Failed to retrieve schema", e.getMessage());
}
}

private String formatSchema(String tableName, List<ColumnInfo> columns) {
if (columns.isEmpty()) {
return "Table " + tableName + " does not exist or has no column information.";
}

StringBuilder sb = new StringBuilder();
sb.append("Schema of table ").append(tableName).append(":\n");

for (ColumnInfo col : columns) {
sb.append(" - ").append(col.toString()).append("\n");
}

return sb.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,42 +18,42 @@
package io.github.malonetalk.agent.tools;

import io.agentscope.core.tool.Tool;
import io.github.malonetalk.entity.Datasource;
import io.github.malonetalk.entity.TableInfo;
import io.github.malonetalk.enums.Status;
import io.github.malonetalk.service.DatasourceService;
import io.github.malonetalk.service.semantic.table.TableSemanticService;
import java.util.Collections;
import java.util.List;
import lombok.AllArgsConstructor;
import io.agentscope.core.tool.ToolParam;
import io.github.malonetalk.agent.tools.response.TablePromptResponse;
import io.github.malonetalk.common.ToolResult;
import io.github.malonetalk.dto.PageResponse;
import io.github.malonetalk.service.semantic.SemanticMergeService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@AllArgsConstructor
@RequiredArgsConstructor
public class GetTablesTool implements MarkAgentTool {

private final DatasourceService dataSourceService;
private final TableSemanticService tableSemanticService;
private final SemanticMergeService semanticMergeService;

@Tool(name = "get_tables", description = "获取数据库中的表信息,包括表名和表描述")
public List<TableInfo> getTables() {
List<Datasource> activeDataSources =
dataSourceService.findByStatus(Status.ACTIVE.getCode());

if (activeDataSources.isEmpty()) {
return Collections.emptyList();
}

if (activeDataSources.size() > 1) {
log.warn(
"Found {} active data sources, using the first one. This may cause data"
+ " inconsistency.",
activeDataSources.size());
public ToolResult<PageResponse<TablePromptResponse>> getTables(
@ToolParam(name = "page", description = "Optional page number, default is 1")
Integer page,
@ToolParam(
name = "page_size",
description = "Optional page size, default is 20, maximum is 100")
Integer pageSize) {
try {
int resolvedPage = PageResponse.resolvePage(page);
int resolvedPageSize = PageResponse.resolvePageSize(pageSize);
return ToolResult.success(
semanticMergeService.getVisibleTablePromptPage(resolvedPage, resolvedPageSize));
} catch (IllegalStateException e) {
return ToolResult.error("Data source parsing failed", e.getMessage());
} catch (IllegalArgumentException e) {
return ToolResult.error("Invalid arguments", e.getMessage());
} catch (RuntimeException e) {
log.error("Failed to retrieve visible tables: {}", e.getMessage(), e);
return ToolResult.error("Failed to retrieve tables", e.getMessage());
}

Datasource dataSource = activeDataSources.get(0);
return tableSemanticService.listTableInfosByDatasourceId(dataSource.getId());
}
}
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.
* limitations under the License.
*/
package io.github.malonetalk.agent.tools.response;

public record ColumnPromptResponse(
String name,
String type,
Boolean primaryKey,
Boolean nullable,
String defaultValue,
String description) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* 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 <https://www.gnu.org/licenses/>.
* limitations under the License.
*/
package io.github.malonetalk.agent.tools.response;

import java.util.List;

public record TablePromptResponse(
String name, String domain, String description, List<TableRelationResponse> relations) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* 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 <https://www.gnu.org/licenses/>.
* limitations under the License.
*/
package io.github.malonetalk.agent.tools.response;

import java.util.List;

public record TableRelationResponse(
String relationType,
String source,
String sourceTableName,
List<String> sourceColumnNames,
String targetTableName,
List<String> targetColumnNames,
String description) {}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
* limitations under the License.
*/
package io.github.malonetalk.agent.datasource;
package io.github.malonetalk.common;

import java.util.ArrayList;
import java.util.LinkedHashMap;
Expand Down
Loading
Loading