mirror of
https://github.com/langgenius/dify.git
synced 2026-05-06 18:27:19 +08:00
fix(web): template generate
This commit is contained in:
parent
c18c953a7c
commit
7384a3c121
@ -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')
|
||||
})
|
||||
})
|
||||
})
|
||||
@ -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) => {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user