Skip to content

Commit 5b485d7

Browse files
committed
Merge branch 'zhsama/dar-es-salaam' into feat/support-agent-sandbox
# Conflicts: # api/core/llm_generator/llm_generator.py # api/core/llm_generator/output_models.py # api/core/llm_generator/output_parser/structured_output.py # api/tests/unit_tests/utils/structured_output_parser/test_structured_output_parser.py
2 parents bf66627 + 39ec2b3 commit 5b485d7

File tree

5 files changed

+87
-17
lines changed

5 files changed

+87
-17
lines changed

web/app/components/workflow/nodes/_base/components/variable/utils.ts

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,7 @@ import {
6262
import { VAR_REGEX } from '@/config'
6363
import { AppModeEnum } from '@/types/app'
6464
import { OUTPUT_FILE_SUB_VARIABLES } from '../../../constants'
65-
import {
66-
67-
Type,
68-
} from '../../../llm/types'
65+
import { FILE_REF_FORMAT, Type } from '../../../llm/types'
6966
import { VarType as ToolVarType } from '../../../tool/types'
7067

7168
export const isSystemVar = (valueSelector: ValueSelector) => {
@@ -124,7 +121,16 @@ export const inputVarTypeToVarType = (type: InputVarType): VarType => {
124121
)
125122
}
126123

127-
const structTypeToVarType = (type: Type, isArray?: boolean): VarType => {
124+
const structTypeToVarType = (
125+
type: Type,
126+
isArray?: boolean,
127+
format?: string,
128+
itemsFormat?: string,
129+
): VarType => {
130+
if (isArray && itemsFormat === FILE_REF_FORMAT)
131+
return VarType.arrayFile
132+
if (!isArray && format === FILE_REF_FORMAT)
133+
return VarType.file
128134
if (isArray) {
129135
return (
130136
(
@@ -178,6 +184,7 @@ const findExceptVarInStructuredProperties = (
178184
const isObj = item.type === Type.object
179185
const isArray = item.type === Type.array
180186
const arrayType = item.items?.type
187+
const arrayFormat = item.items?.format
181188

182189
if (
183190
!isObj
@@ -187,6 +194,8 @@ const findExceptVarInStructuredProperties = (
187194
type: structTypeToVarType(
188195
isArray ? arrayType! : item.type,
189196
isArray,
197+
item.format,
198+
arrayFormat,
190199
),
191200
},
192201
[key],
@@ -218,6 +227,7 @@ const findExceptVarInStructuredOutput = (
218227
const isObj = item.type === Type.object
219228
const isArray = item.type === Type.array
220229
const arrayType = item.items?.type
230+
const arrayFormat = item.items?.format
221231
if (
222232
!isObj
223233
&& !filterVar(
@@ -226,6 +236,8 @@ const findExceptVarInStructuredOutput = (
226236
type: structTypeToVarType(
227237
isArray ? arrayType! : item.type,
228238
isArray,
239+
item.format,
240+
arrayFormat,
229241
),
230242
},
231243
[key],
@@ -1154,8 +1166,14 @@ export const getVarType = ({
11541166
return
11551167

11561168
currProperties = currProperties.properties[key]
1157-
if (isLast)
1158-
type = structTypeToVarType(currProperties?.type)
1169+
if (isLast) {
1170+
if (currProperties?.format === FILE_REF_FORMAT)
1171+
type = VarType.file
1172+
else if (currProperties?.type === Type.array && currProperties?.items?.format === FILE_REF_FORMAT)
1173+
type = VarType.arrayFile
1174+
else
1175+
type = structTypeToVarType(currProperties?.type)
1176+
}
11591177
})
11601178
return type
11611179
}
@@ -1970,15 +1988,20 @@ const varToValueSelectorList = (
19701988
Object.keys(
19711989
(v.children as StructuredOutput)?.schema?.properties || {},
19721990
).forEach((key) => {
1973-
const type = (v.children as StructuredOutput)?.schema?.properties[key].type
1991+
const schemaProperty = (v.children as StructuredOutput)?.schema?.properties[key]
1992+
const type = schemaProperty?.type
19741993
const isArray = type === Type.array
1975-
const arrayType = (v.children as StructuredOutput)?.schema?.properties[
1976-
key
1977-
].items?.type
1994+
const arrayType = schemaProperty?.items?.type
1995+
const arrayFormat = schemaProperty?.items?.format
19781996
varToValueSelectorList(
19791997
{
19801998
variable: key,
1981-
type: structTypeToVarType(isArray ? arrayType! : type, isArray),
1999+
type: structTypeToVarType(
2000+
isArray ? arrayType! : type,
2001+
isArray,
2002+
schemaProperty?.format,
2003+
arrayFormat,
2004+
),
19822005
},
19832006
[...parentValueSelector, v.variable],
19842007
res,

web/app/components/workflow/nodes/llm/components/json-schema-config-modal/visual-editor/edit-card/index.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,21 @@ const TYPE_OPTIONS = [
4444
{ value: Type.number, text: 'number' },
4545
{ value: Type.boolean, text: 'boolean' },
4646
{ value: Type.object, text: 'object' },
47+
{ value: Type.file, text: 'file' },
4748
{ value: ArrayType.string, text: 'array[string]' },
4849
{ value: ArrayType.number, text: 'array[number]' },
4950
{ value: ArrayType.object, text: 'array[object]' },
51+
{ value: ArrayType.file, text: 'array[file]' },
5052
]
5153

5254
const MAXIMUM_DEPTH_TYPE_OPTIONS = [
5355
{ value: Type.string, text: 'string' },
5456
{ value: Type.number, text: 'number' },
5557
{ value: Type.boolean, text: 'boolean' },
58+
{ value: Type.file, text: 'file' },
5659
{ value: ArrayType.string, text: 'array[string]' },
5760
{ value: ArrayType.number, text: 'array[number]' },
61+
{ value: ArrayType.file, text: 'array[file]' },
5862
]
5963

6064
const EditCard: FC<EditCardProps> = ({

web/app/components/workflow/nodes/llm/components/json-schema-config-modal/visual-editor/hooks.ts

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { EditData } from './edit-card'
44
import { noop } from 'es-toolkit/function'
55
import { produce } from 'immer'
66
import Toast from '@/app/components/base/toast'
7-
import { ArrayType, Type } from '../../../types'
7+
import { ArrayType, FILE_REF_FORMAT, Type } from '../../../types'
88
import { findPropertyWithPath } from '../../../utils'
99
import { useMittContext } from './context'
1010
import { useVisualEditorStore } from './store'
@@ -133,13 +133,18 @@ export const useSchemaNodeOperations = (props: VisualEditorProps) => {
133133
}
134134
if (schema.type === Type.array)
135135
delete schema.items
136+
delete schema.format
136137
switch (newType) {
137138
case Type.object:
138139
schema.type = Type.object
139140
schema.properties = {}
140141
schema.required = []
141142
schema.additionalProperties = false
142143
break
144+
case Type.file:
145+
schema.type = Type.string
146+
schema.format = FILE_REF_FORMAT
147+
break
143148
case ArrayType.string:
144149
schema.type = Type.array
145150
schema.items = {
@@ -167,6 +172,13 @@ export const useSchemaNodeOperations = (props: VisualEditorProps) => {
167172
additionalProperties: false,
168173
}
169174
break
175+
case ArrayType.file:
176+
schema.type = Type.array
177+
schema.items = {
178+
type: Type.string,
179+
format: FILE_REF_FORMAT,
180+
}
181+
break
170182
default:
171183
schema.type = newType as Type
172184
}
@@ -309,13 +321,18 @@ export const useSchemaNodeOperations = (props: VisualEditorProps) => {
309321
}
310322
if (schema.type === Type.array)
311323
delete schema.items
324+
delete schema.format
312325
switch (newType) {
313326
case Type.object:
314327
schema.type = Type.object
315328
schema.properties = {}
316329
schema.required = []
317330
schema.additionalProperties = false
318331
break
332+
case Type.file:
333+
schema.type = Type.string
334+
schema.format = FILE_REF_FORMAT
335+
break
319336
case ArrayType.string:
320337
schema.type = Type.array
321338
schema.items = {
@@ -343,6 +360,13 @@ export const useSchemaNodeOperations = (props: VisualEditorProps) => {
343360
additionalProperties: false,
344361
}
345362
break
363+
case ArrayType.file:
364+
schema.type = Type.array
365+
schema.items = {
366+
type: Type.string,
367+
format: FILE_REF_FORMAT,
368+
}
369+
break
346370
default:
347371
schema.type = newType as Type
348372
}
@@ -398,13 +422,18 @@ export const useSchemaNodeOperations = (props: VisualEditorProps) => {
398422
}
399423
if (schema.type === Type.array)
400424
delete schema.items
425+
delete schema.format
401426
switch (newType) {
402427
case Type.object:
403428
schema.type = Type.object
404429
schema.properties = {}
405430
schema.required = []
406431
schema.additionalProperties = false
407432
break
433+
case Type.file:
434+
schema.type = Type.string
435+
schema.format = FILE_REF_FORMAT
436+
break
408437
case ArrayType.string:
409438
schema.type = Type.array
410439
schema.items = {
@@ -432,6 +461,13 @@ export const useSchemaNodeOperations = (props: VisualEditorProps) => {
432461
additionalProperties: false,
433462
}
434463
break
464+
case ArrayType.file:
465+
schema.type = Type.array
466+
schema.items = {
467+
type: Type.string,
468+
format: FILE_REF_FORMAT,
469+
}
470+
break
435471
default:
436472
schema.type = newType as Type
437473
}

web/app/components/workflow/nodes/llm/types.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ export type LLMNodeType = CommonNodeType & {
3636
max_iterations?: number
3737
}
3838

39+
export const FILE_REF_FORMAT = 'dify-file-ref'
40+
3941
export enum Type {
4042
string = 'string',
4143
number = 'number',
@@ -54,12 +56,13 @@ export enum ArrayType {
5456
number = 'array[number]',
5557
boolean = 'array[boolean]',
5658
object = 'array[object]',
59+
file = 'array[file]',
5760
}
5861

5962
export type TypeWithArray = Type | ArrayType
6063

6164
type ArrayItemType = Exclude<Type, Type.array>
62-
export type ArrayItems = Omit<Field, 'type'> & { type: ArrayItemType }
65+
export type ArrayItems = Omit<Field, 'type' | 'format'> & { type: ArrayItemType, format?: string }
6366

6467
export type SchemaEnumType = string[] | number[]
6568

@@ -70,6 +73,7 @@ export type Field = {
7073
}
7174
required?: string[] // Key of required properties in object
7275
description?: string
76+
format?: string
7377
items?: ArrayItems // Array has items. Define the item type
7478
enum?: SchemaEnumType // Enum values
7579
additionalProperties?: false // Required in object by api. Just set false

web/app/components/workflow/nodes/llm/utils.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,24 @@ import type { ValidationError } from 'jsonschema'
22
import type { ArrayItems, Field, LLMNodeType } from './types'
33
import { z } from 'zod'
44
import { draft07Validator, forbidBooleanProperties } from '@/utils/validators'
5-
import { ArrayType, Type } from './types'
5+
import { ArrayType, FILE_REF_FORMAT, Type } from './types'
66

77
export const checkNodeValid = (_payload: LLMNodeType) => {
88
return true
99
}
1010

1111
export const getFieldType = (field: Field) => {
12-
const { type, items, enum: enums } = field
12+
const { type, items, enum: enums, format } = field
13+
if (format === FILE_REF_FORMAT)
14+
return Type.file
1315
if (field.schemaType === 'file')
1416
return Type.file
1517
if (enums && enums.length > 0)
1618
return Type.enumType
1719
if (type !== Type.array || !items)
1820
return type
19-
21+
if (items.format === FILE_REF_FORMAT || items.type === Type.file)
22+
return ArrayType.file
2023
return ArrayType[items.type as keyof typeof ArrayType]
2124
}
2225

0 commit comments

Comments
 (0)