fix(agent): format autofill schema property descriptions with field name and available options list

This commit is contained in:
yiling
2026-07-28 21:44:56 +08:00
parent d6a74b8294
commit 19df011dc2
2 changed files with 10 additions and 13 deletions
+6 -9
View File
@@ -321,6 +321,7 @@ export function fieldsToTypeBoxSchema(fields: AutofillField[]) {
for (const f of fields) { for (const f of fields) {
let schema: TSchema let schema: TSchema
const fieldName = f.config.name const fieldName = f.config.name
const fieldDesc = f.description || fieldName
switch (f.config.type) { switch (f.config.type) {
case 'text': case 'text':
case 'longText': case 'longText':
@@ -339,10 +340,8 @@ export function fieldsToTypeBoxSchema(fields: AutofillField[]) {
enum: options.map((o) => o.id), enum: options.map((o) => o.id),
}) })
if (options.length > 0) { if (options.length > 0) {
const optionMapStr = options.map((o) => `"${o.id}" (${o.displayName})`).join(', ') const optionLines = options.map((o) => `- ${o.displayName} => ${o.id}`).join('\n')
const optionDesc = `[Allowed options: ${optionMapStr}]` schema.description = `The field '${fieldName}' represents ${fieldDesc}.\nSelect one option and return the option ID as the value.\n\nAvailable options:\n${optionLines}`
const baseDesc = f.description ? `${fieldName}: ${f.description}` : fieldName
schema.description = `${baseDesc} ${optionDesc}`
} }
break break
} }
@@ -354,10 +353,8 @@ export function fieldsToTypeBoxSchema(fields: AutofillField[]) {
}), }),
) )
if (options.length > 0) { if (options.length > 0) {
const optionMapStr = options.map((o) => `"${o.id}" (${o.displayName})`).join(', ') const optionLines = options.map((o) => `- ${o.displayName} => ${o.id}`).join('\n')
const optionDesc = `[Allowed options: ${optionMapStr}]` schema.description = `The field '${fieldName}' represents ${fieldDesc}.\nSelect applicable options and return the option IDs as the value.\n\nAvailable options:\n${optionLines}`
const baseDesc = f.description ? `${fieldName}: ${f.description}` : fieldName
schema.description = `${baseDesc} ${optionDesc}`
} }
break break
} }
@@ -366,7 +363,7 @@ export function fieldsToTypeBoxSchema(fields: AutofillField[]) {
} }
if (!schema.description) { if (!schema.description) {
schema.description = f.description ? `${fieldName}: ${f.description}` : fieldName schema.description = `The field '${fieldName}' represents ${fieldDesc}.`
} }
properties[f.id] = schema properties[f.id] = schema
} }
+4 -4
View File
@@ -9,9 +9,9 @@ describe('fieldsToTypeBoxSchema', () => {
] ]
const schema = fieldsToTypeBoxSchema(fields) const schema = fieldsToTypeBoxSchema(fields)
expect(schema.properties.f1.type).toBe('string') expect(schema.properties.f1.type).toBe('string')
expect(schema.properties.f1.description).toBe('Title: The title') expect(schema.properties.f1.description).toBe("The field 'Title' represents The title.")
expect(schema.properties.f2.type).toBe('string') expect(schema.properties.f2.type).toBe('string')
expect(schema.properties.f2.description).toBe('Desc') expect(schema.properties.f2.description).toBe("The field 'Desc' represents Desc.")
}) })
it('converts numeric fields', () => { it('converts numeric fields', () => {
@@ -51,7 +51,7 @@ describe('fieldsToTypeBoxSchema', () => {
expect(schema.properties.f1.type).toBe('string') expect(schema.properties.f1.type).toBe('string')
expect(schema.properties.f1.enum).toEqual(['opt1', 'opt2']) expect(schema.properties.f1.enum).toEqual(['opt1', 'opt2'])
expect(schema.properties.f1.description).toBe( expect(schema.properties.f1.description).toBe(
'Species: Species of animal [Allowed options: "opt1" (Option 1), "opt2" (Option 2)]', "The field 'Species' represents Species of animal.\nSelect one option and return the option ID as the value.\n\nAvailable options:\n- Option 1 => opt1\n- Option 2 => opt2",
) )
}) })
@@ -75,7 +75,7 @@ describe('fieldsToTypeBoxSchema', () => {
expect(schema.properties.f1.type).toBe('array') expect(schema.properties.f1.type).toBe('array')
expect(schema.properties.f1.items.enum).toEqual(['opt1', 'opt2']) expect(schema.properties.f1.items.enum).toEqual(['opt1', 'opt2'])
expect(schema.properties.f1.description).toBe( expect(schema.properties.f1.description).toBe(
'Tags [Allowed options: "opt1" (Tag 1), "opt2" (Tag 2)]', "The field 'Tags' represents Tags.\nSelect applicable options and return the option IDs as the value.\n\nAvailable options:\n- Tag 1 => opt1\n- Tag 2 => opt2",
) )
}) })