@@ -7,11 +7,6 @@ import {
77 type BaseServerTool ,
88 type ServerToolContext ,
99} from '@/lib/copilot/tools/server/base-tool'
10- import {
11- computeWorkflowReadHashFromWorkflowState ,
12- getWorkflowReadHash ,
13- upsertWorkflowReadHashForWorkflowState ,
14- } from '@/lib/copilot/workflow-read-hashes'
1510import { applyTargetedLayout } from '@/lib/workflows/autolayout'
1611import {
1712 DEFAULT_HORIZONTAL_SPACING ,
@@ -98,33 +93,19 @@ export const editWorkflowServerTool: BaseServerTool<EditWorkflowParams, unknown>
9893 chatId : context . chatId ,
9994 } )
10095
101- if ( ! context . chatId ) {
102- throw new Error (
103- 'Workflow has changed or was not read in this chat. Re-read the workflow before editing.'
104- )
105- }
106-
10796 assertServerToolNotAborted ( context )
10897
109- const fromDb = await getCurrentWorkflowStateFromDb ( workflowId )
110- const workflowState = fromDb . workflowState
111- const storedReadHash = await getWorkflowReadHash ( context . chatId , workflowId )
112- if ( ! storedReadHash ) {
113- throw new Error (
114- 'Workflow has changed or was not read in this chat. Re-read the workflow before editing.'
115- )
116- }
117-
118- const currentReadState = computeWorkflowReadHashFromWorkflowState ( {
119- blocks : workflowState . blocks || { } ,
120- edges : workflowState . edges || [ ] ,
121- loops : workflowState . loops || { } ,
122- parallels : workflowState . parallels || { } ,
123- } )
124- if ( storedReadHash !== currentReadState . hash ) {
125- throw new Error (
126- 'Workflow changed since it was last read in this chat. Re-read the workflow before editing.'
127- )
98+ let workflowState : any
99+ if ( currentUserWorkflow ) {
100+ try {
101+ workflowState = JSON . parse ( currentUserWorkflow )
102+ } catch ( error ) {
103+ logger . error ( 'Failed to parse currentUserWorkflow' , error )
104+ throw new Error ( 'Invalid currentUserWorkflow format' )
105+ }
106+ } else {
107+ const fromDb = await getCurrentWorkflowStateFromDb ( workflowId )
108+ workflowState = fromDb . workflowState
128109 }
129110
130111 // Get permission config for the user
@@ -318,20 +299,12 @@ export const editWorkflowServerTool: BaseServerTool<EditWorkflowParams, unknown>
318299 logger . info ( 'Workflow state persisted to database' , { workflowId } )
319300
320301 const sanitizationWarnings = validation . warnings . length > 0 ? validation . warnings : undefined
321- assertServerToolNotAborted ( context )
322- const updatedReadState = await upsertWorkflowReadHashForWorkflowState (
323- context . chatId ,
324- workflowId ,
325- workflowStateForDb
326- )
327302
328303 return {
329304 success : true ,
330305 workflowId,
331306 workflowName : workflowName ?? 'Workflow' ,
332307 workflowState : { ...finalWorkflowState , blocks : layoutedBlocks } ,
333- copilotSanitizedWorkflowState : updatedReadState . sanitizedState ,
334- workflowReadHash : updatedReadState . hash ,
335308 ...( inputErrors && {
336309 inputValidationErrors : inputErrors ,
337310 inputValidationMessage : `${ inputErrors . length } input(s) were rejected due to validation errors. The workflow was still updated with valid inputs only. Errors: ${ inputErrors . join ( '; ' ) } ` ,
0 commit comments