33from vellum .client .types import ChatMessage , ChatMessageContent
44from vellum .workflows .descriptors .base import BaseDescriptor
55from vellum .workflows .descriptors .utils import resolve_value
6+ from vellum .workflows .exceptions import NodeException
67from vellum .workflows .references .lazy import LazyReference
78from vellum .workflows .references .output import OutputReference
89from vellum .workflows .references .state_value import StateValueReference
@@ -35,30 +36,35 @@ class Config(BaseTrigger.Config):
3536 output : Optional [BaseDescriptor [Any ]] = None
3637 state : Optional [StateValueReference [List [ChatMessage ]]] = None
3738
38- def _get_state_key (self ) -> str :
39- """Returns the state attribute key to use for chat history ."""
39+ def _resolve_state (self , state : "BaseState" ) -> Optional [ List [ ChatMessage ]] :
40+ """Resolves the chat history list from state, handling parent state walking ."""
4041 state_ref = self .Config .state
4142 if state_ref is not None :
42- return state_ref .name
43- return "chat_history"
43+ try :
44+ return state_ref .resolve (state )
45+ except NodeException :
46+ return None
47+ if not hasattr (state , "chat_history" ):
48+ return None
49+ return state .chat_history
4450
4551 def __on_workflow_initiated__ (self , state : "BaseState" ) -> None :
4652 """Appends user message to state chat history at workflow start."""
47- chat_history_key = self ._get_state_key ( )
48- if not hasattr ( state , chat_history_key ) :
53+ chat_history = self ._resolve_state ( state )
54+ if chat_history is None :
4955 return
5056
5157 user_message = ChatMessage (
5258 role = "USER" ,
5359 content = self .message if not isinstance (self .message , str ) else None ,
5460 text = self .message if isinstance (self .message , str ) else None ,
5561 )
56- getattr ( state , chat_history_key ) .append (user_message )
62+ chat_history .append (user_message )
5763
5864 def __on_workflow_fulfilled__ (self , state : "BaseState" ) -> None :
5965 """Appends assistant response to state chat history after workflow completion."""
60- chat_history_key = self ._get_state_key ( )
61- if not hasattr ( state , chat_history_key ) :
66+ chat_history = self ._resolve_state ( state )
67+ if chat_history is None :
6268 return
6369
6470 output = self .Config .output
@@ -69,7 +75,7 @@ def __on_workflow_fulfilled__(self, state: "BaseState") -> None:
6975 content = resolved_output if not isinstance (resolved_output , str ) else None ,
7076 text = resolved_output if isinstance (resolved_output , str ) else None ,
7177 )
72- getattr ( state , chat_history_key ) .append (assistant_message )
78+ chat_history .append (assistant_message )
7379
7480 def _resolve_output (
7581 self ,
0 commit comments