mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-23 22:59:09 +00:00
fix(agent): include select option labels and field name in autofill schema
This commit is contained in:
@@ -13,8 +13,8 @@ import { s3Service } from '@shumai/core/src/s3/s3'
|
||||
import { uploadService } from '@shumai/core/src/upload/upload'
|
||||
import { VersionStackService } from '@shumai/core/src/versionStack/versionStack'
|
||||
import { AssetType, prisma, Prisma, type Skill } from '@shumai/db'
|
||||
import { ApplicationFailure, Context } from '@temporalio/activity'
|
||||
import { registerLocalCancelHandler, unregisterLocalCancelHandler } from '@shumai/workflow-core'
|
||||
import { ApplicationFailure, Context } from '@temporalio/activity'
|
||||
import { generateKeyBetween } from 'jittered-fractional-indexing'
|
||||
import { ulid } from 'ulid'
|
||||
import { DatabaseSessionStorage } from '../database-session-storage'
|
||||
|
||||
@@ -320,29 +320,55 @@ export function fieldsToTypeBoxSchema(fields: AutofillField[]) {
|
||||
const properties: Record<string, TSchema> = {}
|
||||
for (const f of fields) {
|
||||
let schema: TSchema
|
||||
const fieldName = f.config.name
|
||||
switch (f.config.type) {
|
||||
case 'text':
|
||||
case 'longText':
|
||||
schema = Type.String()
|
||||
schema = Type.String({ title: fieldName })
|
||||
break
|
||||
case 'number':
|
||||
case 'rating':
|
||||
schema = Type.Number()
|
||||
schema = Type.Number({ title: fieldName })
|
||||
break
|
||||
case 'toggle':
|
||||
schema = Type.Boolean()
|
||||
schema = Type.Boolean({ title: fieldName })
|
||||
break
|
||||
case 'select':
|
||||
case 'select': {
|
||||
const options = f.config.select?.options || []
|
||||
schema = Type.String({
|
||||
enum: f.config.select?.options?.map((o) => o.id) || [],
|
||||
title: fieldName,
|
||||
enum: options.map((o) => o.id),
|
||||
})
|
||||
if (options.length > 0) {
|
||||
const optionMapStr = options.map((o) => `"${o.id}" (${o.displayName})`).join(', ')
|
||||
const optionDesc = `[Allowed options: ${optionMapStr}]`
|
||||
const baseDesc = f.description ? `${fieldName}: ${f.description}` : fieldName
|
||||
schema.description = `${baseDesc} ${optionDesc}`
|
||||
}
|
||||
break
|
||||
}
|
||||
case 'selectMulti': {
|
||||
const options = f.config.selectMulti?.options || []
|
||||
schema = Type.Array(
|
||||
Type.String({
|
||||
enum: options.map((o) => o.id),
|
||||
}),
|
||||
{ title: fieldName },
|
||||
)
|
||||
if (options.length > 0) {
|
||||
const optionMapStr = options.map((o) => `"${o.id}" (${o.displayName})`).join(', ')
|
||||
const optionDesc = `[Allowed options: ${optionMapStr}]`
|
||||
const baseDesc = f.description ? `${fieldName}: ${f.description}` : fieldName
|
||||
schema.description = `${baseDesc} ${optionDesc}`
|
||||
}
|
||||
break
|
||||
}
|
||||
default:
|
||||
schema = Type.String()
|
||||
schema = Type.String({ title: fieldName })
|
||||
}
|
||||
|
||||
if (f.description) {
|
||||
schema.description = f.description
|
||||
if (!schema.description) {
|
||||
schema.description = f.description ? `${fieldName}: ${f.description}` : fieldName
|
||||
}
|
||||
properties[f.id] = schema
|
||||
}
|
||||
|
||||
@@ -9,8 +9,11 @@ describe('fieldsToTypeBoxSchema', () => {
|
||||
]
|
||||
const schema = fieldsToTypeBoxSchema(fields)
|
||||
expect(schema.properties.f1.type).toBe('string')
|
||||
expect(schema.properties.f1.description).toBe('The title')
|
||||
expect(schema.properties.f1.title).toBe('Title')
|
||||
expect(schema.properties.f1.description).toBe('Title: The title')
|
||||
expect(schema.properties.f2.type).toBe('string')
|
||||
expect(schema.properties.f2.title).toBe('Desc')
|
||||
expect(schema.properties.f2.description).toBe('Desc')
|
||||
})
|
||||
|
||||
it('converts numeric fields', () => {
|
||||
@@ -20,21 +23,25 @@ describe('fieldsToTypeBoxSchema', () => {
|
||||
]
|
||||
const schema = fieldsToTypeBoxSchema(fields)
|
||||
expect(schema.properties.f1.type).toBe('number')
|
||||
expect(schema.properties.f1.title).toBe('Price')
|
||||
expect(schema.properties.f2.type).toBe('number')
|
||||
expect(schema.properties.f2.title).toBe('Rating')
|
||||
})
|
||||
|
||||
it('converts toggle fields', () => {
|
||||
const fields: AutofillField[] = [{ id: 'f1', config: { name: 'Active', type: 'toggle' } }]
|
||||
const schema = fieldsToTypeBoxSchema(fields)
|
||||
expect(schema.properties.f1.type).toBe('boolean')
|
||||
expect(schema.properties.f1.title).toBe('Active')
|
||||
})
|
||||
|
||||
it('converts select fields with enums', () => {
|
||||
it('converts select fields with enums and option descriptions', () => {
|
||||
const fields: AutofillField[] = [
|
||||
{
|
||||
id: 'f1',
|
||||
description: 'Species of animal',
|
||||
config: {
|
||||
name: 'Status',
|
||||
name: 'Species',
|
||||
type: 'select',
|
||||
select: {
|
||||
options: [
|
||||
@@ -47,7 +54,36 @@ describe('fieldsToTypeBoxSchema', () => {
|
||||
]
|
||||
const schema = fieldsToTypeBoxSchema(fields)
|
||||
expect(schema.properties.f1.type).toBe('string')
|
||||
expect(schema.properties.f1.title).toBe('Species')
|
||||
expect(schema.properties.f1.enum).toEqual(['opt1', 'opt2'])
|
||||
expect(schema.properties.f1.description).toBe(
|
||||
'Species: Species of animal [Allowed options: "opt1" (Option 1), "opt2" (Option 2)]',
|
||||
)
|
||||
})
|
||||
|
||||
it('converts selectMulti fields with array enums and option descriptions', () => {
|
||||
const fields: AutofillField[] = [
|
||||
{
|
||||
id: 'f1',
|
||||
config: {
|
||||
name: 'Tags',
|
||||
type: 'selectMulti',
|
||||
selectMulti: {
|
||||
options: [
|
||||
{ id: 'opt1', displayName: 'Tag 1', color: 'red' },
|
||||
{ id: 'opt2', displayName: 'Tag 2', color: 'blue' },
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
]
|
||||
const schema = fieldsToTypeBoxSchema(fields)
|
||||
expect(schema.properties.f1.type).toBe('array')
|
||||
expect(schema.properties.f1.title).toBe('Tags')
|
||||
expect(schema.properties.f1.items.enum).toEqual(['opt1', 'opt2'])
|
||||
expect(schema.properties.f1.description).toBe(
|
||||
'Tags [Allowed options: "opt1" (Tag 1), "opt2" (Tag 2)]',
|
||||
)
|
||||
})
|
||||
|
||||
it('defaults to string for unknown types', () => {
|
||||
@@ -57,5 +93,6 @@ describe('fieldsToTypeBoxSchema', () => {
|
||||
]
|
||||
const schema = fieldsToTypeBoxSchema(fields)
|
||||
expect(schema.properties.f1.type).toBe('string')
|
||||
expect(schema.properties.f1.title).toBe('Unknown')
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,13 +1,7 @@
|
||||
import { assetService } from '@shumai/core/src/asset/asset'
|
||||
import { PaginatedData, PaginationParams, paginateQuery } from '@shumai/core/src/pagination'
|
||||
import { s3Service } from '@shumai/core/src/s3/s3'
|
||||
import {
|
||||
AssetStatus,
|
||||
AssetType,
|
||||
Prisma,
|
||||
TaskStatus,
|
||||
prisma,
|
||||
} from '@shumai/db'
|
||||
import { AssetStatus, AssetType, Prisma, TaskStatus, prisma } from '@shumai/db'
|
||||
import {
|
||||
ConfirmFileUploadRequest,
|
||||
CreateUploadTaskRequest,
|
||||
|
||||
Reference in New Issue
Block a user