fix(web): template generate

This commit is contained in:
JzoNg 2026-04-29 18:05:34 +08:00
parent c18c953a7c
commit 7384a3c121
2 changed files with 34 additions and 1 deletions

View File

@ -0,0 +1,26 @@
import { buildTemplateCsvContent } from '../input-fields-utils'
describe('input fields utils', () => {
describe('buildTemplateCsvContent', () => {
it('should append expected_output as the last CSV column', () => {
expect(buildTemplateCsvContent([
{ name: 'query', type: 'string' },
{ name: 'context', type: 'string' },
])).toBe('query,context,expected_output\n')
})
it('should not duplicate expected_output when it already exists', () => {
expect(buildTemplateCsvContent([
{ name: 'query', type: 'string' },
{ name: 'expected_output', type: 'string' },
])).toBe('query,expected_output\n')
})
it('should escape CSV column names before appending expected_output', () => {
expect(buildTemplateCsvContent([
{ name: 'query,text', type: 'string' },
{ name: 'answer "draft"', type: 'string' },
])).toBe('"query,text","answer ""draft""",expected_output\n')
})
})
})

View File

@ -10,6 +10,8 @@ export type InputField = {
type: string
}
export const EXPECTED_OUTPUT_FIELD_NAME = 'expected_output'
export const getGraphNodes = (graph?: Record<string, unknown>) => {
return Array.isArray(graph?.nodes) ? graph.nodes as Node[] : []
}
@ -63,7 +65,12 @@ const escapeCsvCell = (value: string) => {
}
export const buildTemplateCsvContent = (inputFields: InputField[]) => {
return `${inputFields.map(field => escapeCsvCell(field.name)).join(',')}\n`
const fieldNames = inputFields.map(field => field.name)
const templateFieldNames = fieldNames.includes(EXPECTED_OUTPUT_FIELD_NAME)
? fieldNames
: [...fieldNames, EXPECTED_OUTPUT_FIELD_NAME]
return `${templateFieldNames.map(escapeCsvCell).join(',')}\n`
}
export const getFileExtension = (fileName: string) => {