mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-01 05:14:25 +00:00
Merge pull request #235 from shumaiOne/fix/enum-autofill-schema
fix(agent): include select option labels and field name in autofill schema
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
import {
|
import {
|
||||||
type AgentMessage,
|
type AgentMessage,
|
||||||
type AgentTool,
|
type AgentTool,
|
||||||
type SessionTreeEntry,
|
type SessionTreeEntry,
|
||||||
} from '@earendil-works/pi-agent-core'
|
} from '@earendil-works/pi-agent-core'
|
||||||
import { type ImageContent } from '@earendil-works/pi-ai'
|
import { type ImageContent } from '@earendil-works/pi-ai'
|
||||||
import { assetService } from '@shumai/core/src/asset/asset'
|
import { assetService } from '@shumai/core/src/asset/asset'
|
||||||
@@ -13,16 +13,16 @@ import { s3Service } from '@shumai/core/src/s3/s3'
|
|||||||
import { uploadService } from '@shumai/core/src/upload/upload'
|
import { uploadService } from '@shumai/core/src/upload/upload'
|
||||||
import { VersionStackService } from '@shumai/core/src/versionStack/versionStack'
|
import { VersionStackService } from '@shumai/core/src/versionStack/versionStack'
|
||||||
import { AssetType, prisma, Prisma, type Skill } from '@shumai/db'
|
import { AssetType, prisma, Prisma, type Skill } from '@shumai/db'
|
||||||
import { ApplicationFailure, Context } from '@temporalio/activity'
|
|
||||||
import { registerLocalCancelHandler, unregisterLocalCancelHandler } from '@shumai/workflow-core'
|
import { registerLocalCancelHandler, unregisterLocalCancelHandler } from '@shumai/workflow-core'
|
||||||
|
import { ApplicationFailure, Context } from '@temporalio/activity'
|
||||||
import { generateKeyBetween } from 'jittered-fractional-indexing'
|
import { generateKeyBetween } from 'jittered-fractional-indexing'
|
||||||
import { ulid } from 'ulid'
|
import { ulid } from 'ulid'
|
||||||
import { DatabaseSessionStorage } from '../database-session-storage'
|
import { DatabaseSessionStorage } from '../database-session-storage'
|
||||||
import {
|
import {
|
||||||
createAgentSession,
|
createAgentSession,
|
||||||
fieldsToTypeBoxSchema,
|
fieldsToTypeBoxSchema,
|
||||||
type AutofillField,
|
type AutofillField,
|
||||||
type DbProviderInfo,
|
type DbProviderInfo,
|
||||||
} from '../index'
|
} from '../index'
|
||||||
|
|
||||||
import { aiUsageService } from '@shumai/core/src/ai-usage/ai-usage'
|
import { aiUsageService } from '@shumai/core/src/ai-usage/ai-usage'
|
||||||
|
|||||||
@@ -320,6 +320,8 @@ export function fieldsToTypeBoxSchema(fields: AutofillField[]) {
|
|||||||
const properties: Record<string, TSchema> = {}
|
const properties: Record<string, TSchema> = {}
|
||||||
for (const f of fields) {
|
for (const f of fields) {
|
||||||
let schema: TSchema
|
let schema: TSchema
|
||||||
|
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':
|
||||||
@@ -332,17 +334,36 @@ export function fieldsToTypeBoxSchema(fields: AutofillField[]) {
|
|||||||
case 'toggle':
|
case 'toggle':
|
||||||
schema = Type.Boolean()
|
schema = Type.Boolean()
|
||||||
break
|
break
|
||||||
case 'select':
|
case 'select': {
|
||||||
|
const options = f.config.select?.options || []
|
||||||
schema = Type.String({
|
schema = Type.String({
|
||||||
enum: f.config.select?.options?.map((o) => o.id) || [],
|
enum: options.map((o) => o.id),
|
||||||
})
|
})
|
||||||
|
if (options.length > 0) {
|
||||||
|
const optionLines = options.map((o) => `- ${o.displayName} => ${o.id}`).join('\n')
|
||||||
|
schema.description = `The field '${fieldName}' represents ${fieldDesc}.\nSelect one option and return the option ID as the value.\n\nAvailable options:\n${optionLines}`
|
||||||
|
}
|
||||||
break
|
break
|
||||||
|
}
|
||||||
|
case 'selectMulti': {
|
||||||
|
const options = f.config.selectMulti?.options || []
|
||||||
|
schema = Type.Array(
|
||||||
|
Type.String({
|
||||||
|
enum: options.map((o) => o.id),
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
if (options.length > 0) {
|
||||||
|
const optionLines = options.map((o) => `- ${o.displayName} => ${o.id}`).join('\n')
|
||||||
|
schema.description = `The field '${fieldName}' represents ${fieldDesc}.\nSelect applicable options and return the option IDs as the value.\n\nAvailable options:\n${optionLines}`
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
schema = Type.String()
|
schema = Type.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
if (f.description) {
|
if (!schema.description) {
|
||||||
schema.description = f.description
|
schema.description = `The field '${fieldName}' represents ${fieldDesc}.`
|
||||||
}
|
}
|
||||||
properties[f.id] = schema
|
properties[f.id] = schema
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,8 +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('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("The field 'Desc' represents Desc.")
|
||||||
})
|
})
|
||||||
|
|
||||||
it('converts numeric fields', () => {
|
it('converts numeric fields', () => {
|
||||||
@@ -29,12 +30,13 @@ describe('fieldsToTypeBoxSchema', () => {
|
|||||||
expect(schema.properties.f1.type).toBe('boolean')
|
expect(schema.properties.f1.type).toBe('boolean')
|
||||||
})
|
})
|
||||||
|
|
||||||
it('converts select fields with enums', () => {
|
it('converts select fields with enums and option descriptions', () => {
|
||||||
const fields: AutofillField[] = [
|
const fields: AutofillField[] = [
|
||||||
{
|
{
|
||||||
id: 'f1',
|
id: 'f1',
|
||||||
|
description: 'Species of animal',
|
||||||
config: {
|
config: {
|
||||||
name: 'Status',
|
name: 'Species',
|
||||||
type: 'select',
|
type: 'select',
|
||||||
select: {
|
select: {
|
||||||
options: [
|
options: [
|
||||||
@@ -48,6 +50,33 @@ 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.enum).toEqual(['opt1', 'opt2'])
|
expect(schema.properties.f1.enum).toEqual(['opt1', 'opt2'])
|
||||||
|
expect(schema.properties.f1.description).toBe(
|
||||||
|
"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",
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
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.items.enum).toEqual(['opt1', 'opt2'])
|
||||||
|
expect(schema.properties.f1.description).toBe(
|
||||||
|
"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",
|
||||||
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('defaults to string for unknown types', () => {
|
it('defaults to string for unknown types', () => {
|
||||||
|
|||||||
@@ -1,13 +1,7 @@
|
|||||||
import { assetService } from '@shumai/core/src/asset/asset'
|
import { assetService } from '@shumai/core/src/asset/asset'
|
||||||
import { PaginatedData, PaginationParams, paginateQuery } from '@shumai/core/src/pagination'
|
import { PaginatedData, PaginationParams, paginateQuery } from '@shumai/core/src/pagination'
|
||||||
import { s3Service } from '@shumai/core/src/s3/s3'
|
import { s3Service } from '@shumai/core/src/s3/s3'
|
||||||
import {
|
import { AssetStatus, AssetType, Prisma, TaskStatus, prisma } from '@shumai/db'
|
||||||
AssetStatus,
|
|
||||||
AssetType,
|
|
||||||
Prisma,
|
|
||||||
TaskStatus,
|
|
||||||
prisma,
|
|
||||||
} from '@shumai/db'
|
|
||||||
import {
|
import {
|
||||||
ConfirmFileUploadRequest,
|
ConfirmFileUploadRequest,
|
||||||
CreateUploadTaskRequest,
|
CreateUploadTaskRequest,
|
||||||
|
|||||||
Reference in New Issue
Block a user