mirror of
https://github.com/simstudioai/sim.git
synced 2026-01-10 23:48:09 -05:00
Fix: table subblock input
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { useEffect, useRef } from 'react'
|
||||
import { useEffect, useMemo, useRef } from 'react'
|
||||
import { Trash2 } from 'lucide-react'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Input } from '@/components/ui/input'
|
||||
@@ -47,16 +47,22 @@ export function Table({ columns, blockId, subBlockId }: TableProps) {
|
||||
}
|
||||
}, [value])
|
||||
|
||||
// Initialize with empty row if no value exists
|
||||
const rows = (value as any[]) || [
|
||||
{
|
||||
id: crypto.randomUUID(),
|
||||
cells: Object.fromEntries(columns.map((col) => [col, ''])),
|
||||
},
|
||||
]
|
||||
// Ensure value is properly typed and initialized
|
||||
const rows = useMemo(() => {
|
||||
if (!Array.isArray(value)) {
|
||||
// Initialize with a single empty row if value is null or invalid
|
||||
return [
|
||||
{
|
||||
id: crypto.randomUUID(),
|
||||
cells: Object.fromEntries(columns.map((col) => [col, ''])),
|
||||
},
|
||||
]
|
||||
}
|
||||
return value as TableRow[]
|
||||
}, [value, columns])
|
||||
|
||||
const handleCellChange = (rowIndex: number, column: string, value: string) => {
|
||||
const updatedRows = rows.map((row, idx) =>
|
||||
const updatedRows = [...rows].map((row, idx) =>
|
||||
idx === rowIndex
|
||||
? {
|
||||
...row,
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
import { useCallback } from 'react'
|
||||
import { useWorkflowStore } from '@/stores/workflow/store'
|
||||
|
||||
export function useSubBlockValue(blockId: string, subBlockId: string) {
|
||||
export function useSubBlockValue<T = any>(
|
||||
blockId: string,
|
||||
subBlockId: string
|
||||
): readonly [T | null, (value: T) => void] {
|
||||
const value = useWorkflowStore(
|
||||
useCallback(
|
||||
(state) => state.blocks[blockId]?.subBlocks[subBlockId]?.value ?? null,
|
||||
@@ -12,11 +15,11 @@ export function useSubBlockValue(blockId: string, subBlockId: string) {
|
||||
const updateSubBlock = useWorkflowStore((state) => state.updateSubBlock)
|
||||
|
||||
const setValue = useCallback(
|
||||
(newValue: any) => {
|
||||
updateSubBlock(blockId, subBlockId, newValue)
|
||||
(newValue: T) => {
|
||||
updateSubBlock(blockId, subBlockId, newValue as any)
|
||||
},
|
||||
[blockId, subBlockId, updateSubBlock]
|
||||
)
|
||||
|
||||
return [value, setValue] as const
|
||||
return [value as T | null, setValue] as const
|
||||
}
|
||||
|
||||
@@ -38,19 +38,26 @@ export const useWorkflowStore = create<WorkflowStoreWithHistory>()(
|
||||
const block = state.blocks[blockId]
|
||||
if (!block) return state
|
||||
|
||||
const blockConfig = getBlock(block.type)
|
||||
if (!blockConfig) return state
|
||||
const processedValue = Array.isArray(value)
|
||||
? value
|
||||
: typeof value === 'string'
|
||||
? value
|
||||
: JSON.stringify(value, null, 2)
|
||||
|
||||
// Validate responseFormat if it's the agent block's responseFormat input
|
||||
if (blockConfig.type === 'agent' && subBlockId === 'responseFormat' && value) {
|
||||
// Only attempt JSON parsing for agent responseFormat validation
|
||||
if (
|
||||
block.type === 'agent' &&
|
||||
subBlockId === 'responseFormat' &&
|
||||
typeof processedValue === 'string'
|
||||
) {
|
||||
console.log('Validating responseFormat input:', {
|
||||
type: typeof value,
|
||||
rawValue: value,
|
||||
type: typeof processedValue,
|
||||
rawValue: processedValue,
|
||||
})
|
||||
|
||||
try {
|
||||
// Parse the input string to validate JSON but keep original string value
|
||||
const parsed = JSON.parse(value)
|
||||
const parsed = JSON.parse(processedValue)
|
||||
console.log('Parsed responseFormat:', parsed)
|
||||
|
||||
// Simple validation of required schema structure
|
||||
@@ -81,35 +88,18 @@ export const useWorkflowStore = create<WorkflowStoreWithHistory>()(
|
||||
}
|
||||
}
|
||||
|
||||
// Create new subBlocks state with the original value
|
||||
const newSubBlocks = {
|
||||
...block.subBlocks,
|
||||
[subBlockId]: {
|
||||
...block.subBlocks[subBlockId],
|
||||
value:
|
||||
// Keep tools as arrays
|
||||
subBlockId === 'tools' && Array.isArray(value)
|
||||
? value
|
||||
: // Keep responseFormat as string
|
||||
subBlockId === 'responseFormat'
|
||||
? value
|
||||
: // For all other values, use the previous logic of stringifying
|
||||
typeof value === 'string'
|
||||
? value
|
||||
: JSON.stringify(value, null, 2),
|
||||
},
|
||||
}
|
||||
|
||||
// Resolve new outputs
|
||||
const newOutputs = resolveOutputType(blockConfig.workflow.outputs, newSubBlocks)
|
||||
|
||||
return {
|
||||
blocks: {
|
||||
...state.blocks,
|
||||
[blockId]: {
|
||||
...block,
|
||||
subBlocks: newSubBlocks,
|
||||
outputs: newOutputs,
|
||||
subBlocks: {
|
||||
...block.subBlocks,
|
||||
[subBlockId]: {
|
||||
...block.subBlocks[subBlockId],
|
||||
value: processedValue,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user