Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix/output schema #146

Merged
merged 3 commits into from
Feb 7, 2025
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 67 additions & 16 deletions frontend/src/store/flowSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,20 +389,38 @@ const flowSlice = createSlice({
state.projectName = action.payload
},

setWorkflowInputVariable: (state, action: PayloadAction<{ key: string; value: any }>) => {
setWorkflowInputVariable: (
state,
action: PayloadAction<{
key: string
value: any
}>
) => {
const { key, value } = action.payload
state.workflowInputVariables[key] = value

// Set the output schema for the input node
const inputNode = state.nodes.find((node) => node.type === 'InputNode')
if (inputNode) {
const currentConfig = state.nodeConfigs[inputNode.id] || {}
const updatedSchema = {
...(currentConfig.output_schema || {}),
[key]: value,
}

// Generate JSON schema
const jsonSchema = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider extracting the JSON schema generation (lines 411-418) into a utility function to reduce duplication.

type: 'object',
properties: Object.fromEntries(
Object.entries(updatedSchema).map(([k, type]) => [k, { type }])
),
required: Object.keys(updatedSchema)
}

state.nodeConfigs[inputNode.id] = {
...currentConfig,
output_schema: {
...(currentConfig.output_schema || {}),
[key]: value,
},
output_schema: updatedSchema,
output_json_schema: JSON.stringify(jsonSchema, null, 2)
}
}

Expand All @@ -429,18 +447,34 @@ const flowSlice = createSlice({
}
},

deleteWorkflowInputVariable: (state, action: PayloadAction<{ key: string }>) => {
deleteWorkflowInputVariable: (
state,
action: PayloadAction<{
key: string
}>
) => {
const { key } = action.payload

// Remove from input node output schema
const inputNode = state.nodes.find((node) => node.type === 'InputNode')
if (inputNode) {
const currentConfig = state.nodeConfigs[inputNode.id] || {}
const currentSchema = { ...(currentConfig.output_schema || {}) }
delete currentSchema[key]
const updatedSchema = { ...(currentConfig.output_schema || {}) }
delete updatedSchema[key]

// Generate JSON schema
const jsonSchema = {
type: 'object',
properties: Object.fromEntries(
Object.entries(updatedSchema).map(([k, type]) => [k, { type }])
),
required: Object.keys(updatedSchema)
}

state.nodeConfigs[inputNode.id] = {
...currentConfig,
output_schema: currentSchema,
output_schema: updatedSchema,
output_json_schema: JSON.stringify(jsonSchema, null, 2)
}
}
// Remove from global workflowInputVariables
Expand Down Expand Up @@ -472,7 +506,13 @@ const flowSlice = createSlice({
}
},

updateWorkflowInputVariableKey: (state, action: PayloadAction<{ oldKey: string; newKey: string }>) => {
updateWorkflowInputVariableKey: (
state,
action: PayloadAction<{
oldKey: string
newKey: string
}>
) => {
const { oldKey, newKey } = action.payload

// Only proceed if keys differ
Expand All @@ -486,14 +526,25 @@ const flowSlice = createSlice({
const inputNode = state.nodes.find((node) => node.type === 'InputNode')
if (inputNode) {
const currentConfig = state.nodeConfigs[inputNode.id] || {}
const currentSchema = { ...(currentConfig.output_schema || {}) }
if (currentSchema.hasOwnProperty(oldKey)) {
currentSchema[newKey] = currentSchema[oldKey]
delete currentSchema[oldKey]
const updatedSchema = { ...(currentConfig.output_schema || {}) }
if (updatedSchema.hasOwnProperty(oldKey)) {
updatedSchema[newKey] = updatedSchema[oldKey]
delete updatedSchema[oldKey]
}

// Generate JSON schema
const jsonSchema = {
type: 'object',
properties: Object.fromEntries(
Object.entries(updatedSchema).map(([k, type]) => [k, { type }])
),
required: Object.keys(updatedSchema)
}

state.nodeConfigs[inputNode.id] = {
...currentConfig,
output_schema: currentSchema,
output_schema: updatedSchema,
output_json_schema: JSON.stringify(jsonSchema, null, 2)
}
}

Expand All @@ -505,7 +556,7 @@ const flowSlice = createSlice({
return edge
})

// 4. Rebuild RouterNode/CoalesceNode schemas connected to the input node,
// 4. Rebuild RouterNode/CoalesceNode schemas connected to the input node
if (inputNode?.id) {
const connectedRouterNodes = state.nodes.filter(
(targetNode) =>
Expand Down