Skip to content

Commit 9cb759a

Browse files
committed
fix(state): handle JSON parsing errors and ensure consistent state updates
1 parent 9cfaef6 commit 9cb759a

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

src/agents/GraphRunner.ts

+12-6
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ export class GraphRunner extends Runnable<GraphRunnerInput, GraphRunnerOutput> {
132132

133133
logger(message.content as string);
134134

135-
// Create state update prompt if node has state variables defined
135+
let sharedState = state.state;
136136
if (node.state?.length > 0) {
137137
const stateUpdatePrompt = PromptTemplate.fromTemplate(
138138
`Based on the message from an AI agent to a human, update the following state variables:
@@ -153,17 +153,23 @@ export class GraphRunner extends Runnable<GraphRunnerInput, GraphRunnerOutput> {
153153
},
154154
);
155155

156-
const stateUpdate = JSON.parse(update.content as string);
157-
const updatedState = {
156+
let stateUpdate: Record<string, string>;
157+
try {
158+
stateUpdate = JSON.parse(update.content as string);
159+
} catch (error) {
160+
logger(`Error parsing state update: ${error}`);
161+
stateUpdate = {};
162+
}
163+
sharedState = {
158164
...state.state,
159-
...stateUpdate
165+
...stateUpdate,
160166
};
161167
}
162168

163169
return {
164170
lastNode: node.name,
165171
messages: [message],
166-
state: updatedState,
172+
state: sharedState,
167173
};
168174
};
169175

@@ -226,7 +232,7 @@ export class GraphRunner extends Runnable<GraphRunnerInput, GraphRunnerOutput> {
226232
return {
227233
lastNode: node.name,
228234
messages: [message],
229-
sharedState: state.sharedState,
235+
state: state.state,
230236
};
231237
};
232238

0 commit comments

Comments
 (0)