mirror of
https://github.com/simstudioai/sim.git
synced 2026-02-12 23:45:07 -05:00
* v0 * v1 * Basic ss tes * Ss tests * Stuff * Add mcp * mcp v1 * Improvement * Fix * BROKEN * Checkpoint * Streaming * Fix abort * Things are broken * Streaming seems to work but copilot is dumb * Fix edge issue * LUAAAA * Fix stream buffer * Fix lint * Checkpoint * Initial temp state, in the middle of a refactor * Initial test shows diff store still working * Tool refactor * First cleanup pass complete - untested * Continued cleanup * Refactor * Refactor complete - no testing yet * Fix - cursor makes me sad * Fix mcp * Clean up mcp * Updated mcp * Add respond to subagents * Fix definitions * Add tools * Add tools * Add copilot mcp tracking * Fix lint * Fix mcp * Fix * Updates * Clean up mcp * Fix copilot mcp tool names to be sim prefixed * Add opus 4.6 * Fix discovery tool * Fix * Remove logs * Fix go side tool rendering * Update docs * Fix hydration * Fix tool call resolution * Fix * Fix lint * Fix superagent and autoallow integrations * Fix always allow * Update block * Remove plan docs * Fix hardcoded ff * Fix dropped provider * Fix lint * Fix tests * Fix dead messages array * Fix discovery * Fix run workflow * Fix run block * Fix run from block in copilot * Fix lint * Fix skip and mtb * Fix typing * Fix tool call * Bump api version * Fix bun lock * Nuke bad files
168 lines
5.6 KiB
TypeScript
168 lines
5.6 KiB
TypeScript
import { createLogger } from '@sim/logger'
|
|
import { stripWorkflowDiffMarkers } from '@/lib/workflows/diff'
|
|
import { useWorkflowRegistry } from '../workflows/registry/store'
|
|
import { useSubBlockStore } from '../workflows/subblock/store'
|
|
import { mergeSubblockState } from '../workflows/utils'
|
|
import { useWorkflowStore } from '../workflows/workflow/store'
|
|
import type { WorkflowState } from '../workflows/workflow/types'
|
|
import type { WorkflowDiffState } from './types'
|
|
|
|
const logger = createLogger('WorkflowDiffStore')
|
|
|
|
export function cloneWorkflowState(state: WorkflowState): WorkflowState {
|
|
return {
|
|
...state,
|
|
blocks: structuredClone(state.blocks || {}),
|
|
edges: structuredClone(state.edges || []),
|
|
loops: structuredClone(state.loops || {}),
|
|
parallels: structuredClone(state.parallels || {}),
|
|
}
|
|
}
|
|
|
|
export function extractSubBlockValues(
|
|
workflowState: WorkflowState
|
|
): Record<string, Record<string, any>> {
|
|
const values: Record<string, Record<string, any>> = {}
|
|
Object.entries(workflowState.blocks || {}).forEach(([blockId, block]) => {
|
|
values[blockId] = {}
|
|
Object.entries(block.subBlocks || {}).forEach(([subBlockId, subBlock]) => {
|
|
values[blockId][subBlockId] = subBlock?.value ?? null
|
|
})
|
|
})
|
|
return values
|
|
}
|
|
|
|
export function applyWorkflowStateToStores(
|
|
workflowId: string,
|
|
workflowState: WorkflowState,
|
|
options?: { updateLastSaved?: boolean }
|
|
) {
|
|
logger.debug('[applyWorkflowStateToStores] Applying state', {
|
|
workflowId,
|
|
blockCount: Object.keys(workflowState.blocks || {}).length,
|
|
edgeCount: workflowState.edges?.length ?? 0,
|
|
edgePreview: workflowState.edges?.slice(0, 3).map((e) => `${e.source} -> ${e.target}`),
|
|
})
|
|
const workflowStore = useWorkflowStore.getState()
|
|
const cloned = cloneWorkflowState(workflowState)
|
|
logger.debug('[applyWorkflowStateToStores] Cloned state edges', {
|
|
clonedEdgeCount: cloned.edges?.length ?? 0,
|
|
})
|
|
workflowStore.replaceWorkflowState(cloned, options)
|
|
const subBlockValues = extractSubBlockValues(workflowState)
|
|
useSubBlockStore.getState().setWorkflowValues(workflowId, subBlockValues)
|
|
|
|
// Verify what's in the store after apply
|
|
const afterState = workflowStore.getWorkflowState()
|
|
logger.info('[applyWorkflowStateToStores] Applied workflow state to stores', {
|
|
workflowId,
|
|
afterEdgeCount: afterState.edges?.length ?? 0,
|
|
})
|
|
}
|
|
|
|
export function captureBaselineSnapshot(workflowId: string): WorkflowState {
|
|
const workflowStore = useWorkflowStore.getState()
|
|
const currentState = workflowStore.getWorkflowState()
|
|
const mergedBlocks = mergeSubblockState(currentState.blocks, workflowId)
|
|
|
|
return {
|
|
...cloneWorkflowState(currentState),
|
|
blocks: structuredClone(mergedBlocks),
|
|
}
|
|
}
|
|
|
|
export async function persistWorkflowStateToServer(
|
|
workflowId: string,
|
|
workflowState: WorkflowState
|
|
): Promise<boolean> {
|
|
try {
|
|
const cleanState = stripWorkflowDiffMarkers(cloneWorkflowState(workflowState))
|
|
const response = await fetch(`/api/workflows/${workflowId}/state`, {
|
|
method: 'PUT',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify({
|
|
...cleanState,
|
|
lastSaved: Date.now(),
|
|
}),
|
|
})
|
|
|
|
if (!response.ok) {
|
|
const errorText = await response.text().catch(() => '')
|
|
throw new Error(errorText || 'Failed to persist workflow state')
|
|
}
|
|
|
|
const activeWorkflowId = useWorkflowRegistry.getState().activeWorkflowId
|
|
if (activeWorkflowId === workflowId) {
|
|
useWorkflowStore.setState({ lastSaved: Date.now() })
|
|
}
|
|
|
|
return true
|
|
} catch (error) {
|
|
logger.error('Failed to persist workflow state after copilot edit', error)
|
|
return false
|
|
}
|
|
}
|
|
|
|
export async function getLatestUserMessageId(): Promise<string | null> {
|
|
try {
|
|
const { useCopilotStore } = await import('@/stores/panel/copilot/store')
|
|
const { messages } = useCopilotStore.getState()
|
|
if (!Array.isArray(messages) || messages.length === 0) {
|
|
return null
|
|
}
|
|
|
|
for (let i = messages.length - 1; i >= 0; i--) {
|
|
const m = messages[i]
|
|
if (m?.role === 'user' && m?.id) {
|
|
return m.id
|
|
}
|
|
}
|
|
} catch (error) {
|
|
logger.warn('Failed to capture trigger message id', { error })
|
|
}
|
|
return null
|
|
}
|
|
|
|
export async function findLatestEditWorkflowToolCallId(): Promise<string | undefined> {
|
|
try {
|
|
const { useCopilotStore } = await import('@/stores/panel/copilot/store')
|
|
const { messages, toolCallsById } = useCopilotStore.getState()
|
|
|
|
for (let mi = messages.length - 1; mi >= 0; mi--) {
|
|
const message = messages[mi]
|
|
if (message.role !== 'assistant' || !message.contentBlocks) continue
|
|
for (const block of message.contentBlocks) {
|
|
if (block?.type === 'tool_call' && block.toolCall?.name === 'edit_workflow') {
|
|
return block.toolCall?.id
|
|
}
|
|
}
|
|
}
|
|
|
|
const fallback = Object.values(toolCallsById).filter((call) => call.name === 'edit_workflow')
|
|
|
|
return fallback.length ? fallback[fallback.length - 1].id : undefined
|
|
} catch (error) {
|
|
logger.warn('Failed to resolve edit_workflow tool call id', { error })
|
|
return undefined
|
|
}
|
|
}
|
|
|
|
export function createBatchedUpdater(set: (updates: Partial<WorkflowDiffState>) => void) {
|
|
let updateTimer: NodeJS.Timeout | null = null
|
|
const UPDATE_DEBOUNCE_MS = 16
|
|
let pendingUpdates: Partial<WorkflowDiffState> = {}
|
|
return (updates: Partial<WorkflowDiffState>) => {
|
|
Object.assign(pendingUpdates, updates)
|
|
if (updateTimer) {
|
|
clearTimeout(updateTimer)
|
|
}
|
|
updateTimer = setTimeout(() => {
|
|
set(pendingUpdates)
|
|
pendingUpdates = {}
|
|
updateTimer = null
|
|
}, UPDATE_DEBOUNCE_MS)
|
|
}
|
|
}
|