diff --git a/internal/application/service/extract.go b/internal/application/service/extract.go index f9a9d59c..3d789d6d 100644 --- a/internal/application/service/extract.go +++ b/internal/application/service/extract.go @@ -649,7 +649,7 @@ func (s *DataTableSummaryService) processTableData(ctx context.Context, resource // 构建共用的schema和样本数据描述 schemaDesc := tableSchema.Description() - sampleDesc := s.buildSampleDataDescription(sampleResult, 10) + sampleDesc := s.buildSampleDataDescription(ctx, sampleResult, 10) // 使用AI生成表格摘要和列描述 customInstructions := "" @@ -860,12 +860,36 @@ func (s *DataTableSummaryService) generateColumnDescriptions(ctx context.Context } // buildSampleDataDescription builds a formatted sample data description -func (s *DataTableSummaryService) buildSampleDataDescription(sampleData *types.ToolResult, maxRows int) string { +func (s *DataTableSummaryService) buildSampleDataDescription(ctx context.Context, sampleData *types.ToolResult, maxRows int) string { var builder strings.Builder builder.WriteString(fmt.Sprintf("Sample data (first %d rows):\n", maxRows)) - rows, ok := sampleData.Data["rows"].([]map[string]interface{}) - if !ok { + if sampleData == nil || sampleData.Data == nil { + return builder.String() + } + + rawRows, exists := sampleData.Data["rows"] + if !exists || rawRows == nil { + return builder.String() + } + + // DataAnalysisTool returns []map[string]string. A decoded ToolResult can + // instead contain []map[string]interface{}, so normalize both shapes before + // serializing the sample rows. + var rows []interface{} + switch typedRows := rawRows.(type) { + case []map[string]string: + rows = make([]interface{}, len(typedRows)) + for i, row := range typedRows { + rows[i] = row + } + case []map[string]interface{}: + rows = make([]interface{}, len(typedRows)) + for i, row := range typedRows { + rows[i] = row + } + default: + logger.Warnf(ctx, "[TableSummary] Unsupported sample rows type: %T", rawRows) return builder.String() } diff --git a/internal/application/service/extract_data_table_summary_test.go b/internal/application/service/extract_data_table_summary_test.go new file mode 100644 index 00000000..4c059ef7 --- /dev/null +++ b/internal/application/service/extract_data_table_summary_test.go @@ -0,0 +1,65 @@ +package service + +import ( + "context" + "strings" + "testing" + + "github.com/Tencent/WeKnora/internal/types" +) + +func TestBuildSampleDataDescriptionIncludesDataAnalysisRows(t *testing.T) { + service := &DataTableSummaryService{} + result := &types.ToolResult{Data: map[string]interface{}{ + "rows": []map[string]string{ + {"date": "20250101", "status": "approved"}, + {"date": "20250102", "status": "pending"}, + }, + }} + + got := service.buildSampleDataDescription(context.Background(), result, 10) + for _, want := range []string{ + `"date":"20250101"`, + `"status":"approved"`, + `"date":"20250102"`, + `"status":"pending"`, + } { + if !strings.Contains(got, want) { + t.Errorf("sample description missing %s:\n%s", want, got) + } + } +} + +func TestBuildSampleDataDescriptionSupportsDecodedRows(t *testing.T) { + service := &DataTableSummaryService{} + result := &types.ToolResult{Data: map[string]interface{}{ + "rows": []map[string]interface{}{ + {"date": "20250101", "count": float64(3)}, + }, + }} + + got := service.buildSampleDataDescription(context.Background(), result, 10) + for _, want := range []string{`"date":"20250101"`, `"count":3`} { + if !strings.Contains(got, want) { + t.Errorf("sample description missing %s:\n%s", want, got) + } + } +} + +func TestBuildSampleDataDescriptionLimitsRows(t *testing.T) { + service := &DataTableSummaryService{} + result := &types.ToolResult{Data: map[string]interface{}{ + "rows": []map[string]string{ + {"id": "first"}, + {"id": "second"}, + }, + }} + + got := service.buildSampleDataDescription(context.Background(), result, 1) + if !strings.Contains(got, `"id":"first"`) { + t.Fatalf("first row missing:\n%s", got) + } + if strings.Contains(got, `"id":"second"`) { + t.Fatalf("sample limit was ignored:\n%s", got) + } +}