Skip to content

Commit 4fa6eec

Browse files
committed
feat: Enhance CrosstabTable and StepFlow components to improve AI-generated data processing, ensuring correct mapping of value dimensions and validation of cell values for better data integrity
1 parent e3695b1 commit 4fa6eec

File tree

3 files changed

+61
-15
lines changed

3 files changed

+61
-15
lines changed

src/renderer/src/components/pages/crosstab/CrosstabTable.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ export default function CrosstabTable({
8383
const cellKey = `${hPath}|${vPath}`
8484
const cellData = tableData[cellKey]
8585

86+
// 数据处理逻辑
87+
8688
if (cellData && selectedValueDimension) {
8789
gridData[cellKey] = cellData[selectedValueDimension] || ''
8890
} else if (cellData && !selectedValueDimension && metadata.valueDimensions.length > 0) {

src/renderer/src/components/pages/crosstab/CrosstabUtils.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,19 @@ export const PROMPT_TEMPLATES = {
105105
[VALUE_DIMENSIONS]
106106
107107
# 输出格式
108-
严格遵守以下JSON格式:
108+
严格遵守以下JSON格式,必须使用值维度的id作为键
109109
\`\`\`json
110110
{
111-
"value1": "针对值维度1的具体内容",
112-
"value2": "针对值维度2的具体内容"
111+
"值维度1的id": "针对值维度1的具体内容",
112+
"值维度2的id": "针对值维度2的具体内容"
113113
}
114114
\`\`\`
115115
116+
重要说明:
117+
- 必须使用值维度列表中每个维度的 id 字段作为 JSON 对象的键
118+
- 不要使用 "value1", "value2" 等示例键名
119+
- 确保为每个值维度都生成对应的内容
120+
116121
请为当前单元格位置生成所有值维度的内容。`,
117122

118123
// 维度建议生成模板

src/renderer/src/components/pages/crosstab/StepFlow.tsx

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -480,23 +480,62 @@ export default function StepFlow({ chat, userInput, onStepComplete, getLLMConfig
480480
}
481481
})
482482

483-
const jsonContent = extractJsonContent(result)
483+
const jsonContent = extractJsonContent(result)
484484
const cellValues = JSON.parse(jsonContent)
485485

486-
// 更新本地tableData状态
487-
currentTableData[cellKey] = cellValues
488-
489-
// 立即更新当前单元格数据到UI
490-
dispatch({
491-
type: 'UPDATE_CROSSTAB_DATA',
492-
payload: {
493-
chatId: chat.id,
494-
data: {
495-
tableData: { ...currentTableData }
496-
}
486+
// 处理AI生成的数据格式,确保键是实际的值维度ID
487+
const processedCellValues: { [key: string]: string } = {}
488+
489+
if (valueDimensions.length > 0) {
490+
// 检查是否使用了通用键格式
491+
const keys = Object.keys(cellValues)
492+
const hasGenericKeys = keys.some(key => key.match(/^value\d+$/))
493+
494+
if (hasGenericKeys) {
495+
// 映射通用键到实际的值维度ID
496+
valueDimensions.forEach((dimension, index) => {
497+
const genericKey = `value${index + 1}`
498+
if (cellValues[genericKey]) {
499+
processedCellValues[dimension.id] = cellValues[genericKey]
500+
}
501+
})
502+
} else {
503+
// 检查是否直接使用了值维度ID
504+
valueDimensions.forEach(dimension => {
505+
if (cellValues[dimension.id]) {
506+
processedCellValues[dimension.id] = cellValues[dimension.id]
507+
}
508+
})
497509
}
510+
511+
// 如果没有找到匹配的键,尝试使用第一个可用的值作为第一个维度的值
512+
if (Object.keys(processedCellValues).length === 0 && Object.keys(cellValues).length > 0) {
513+
const firstDimension = valueDimensions[0]
514+
const firstValue = Object.values(cellValues)[0]
515+
processedCellValues[firstDimension.id] = firstValue as string
516+
}
517+
}
518+
519+
// 确保所有维度都有值
520+
const validatedCellValues: { [key: string]: string } = {}
521+
valueDimensions.forEach(dim => {
522+
validatedCellValues[dim.id] = processedCellValues[dim.id] || ''
498523
})
499524

525+
// 更新本地tableData状态
526+
currentTableData[cellKey] = validatedCellValues
527+
528+
// 立即更新当前单元格数据到UI
529+
dispatch({
530+
type: 'UPDATE_CROSSTAB_DATA',
531+
payload: {
532+
chatId: chat.id,
533+
data: {
534+
tableData: { ...currentTableData }
535+
}
536+
}
537+
})
538+
500539
completedCells++
501540
} catch (error) {
502541
dispatch({

0 commit comments

Comments
 (0)