fix(data-analysis): 修复 Excel 表格摘要丢失样本数据

main
liguowei 2026-08-13 10:33:22 +08:00 committed by lyingbug
parent 11f2b4dcee
commit e9f1e87662
2 changed files with 93 additions and 4 deletions

View File

@ -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()
}

View File

@ -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)
}
}