Add SharedState component for state sharing
This commit is contained in:
parent
62a7dc77d6
commit
f3a7570aad
1 changed files with 38 additions and 0 deletions
38
src/backend/langflow/components/utilities/SharedState.py
Normal file
38
src/backend/langflow/components/utilities/SharedState.py
Normal file
|
|
@ -0,0 +1,38 @@
|
||||||
|
from typing import Union
|
||||||
|
|
||||||
|
from langflow import CustomComponent
|
||||||
|
from langflow.field_typing import Text
|
||||||
|
from langflow.schema import Record
|
||||||
|
|
||||||
|
|
||||||
|
class SharedState(CustomComponent):
|
||||||
|
display_name = "Shared State"
|
||||||
|
description = "A component to share state between components."
|
||||||
|
|
||||||
|
def build_config(self):
|
||||||
|
return {
|
||||||
|
"name": {"display_name": "Name", "info": "The name of the state."},
|
||||||
|
"record": {"display_name": "Record", "info": "The record to store."},
|
||||||
|
"append": {
|
||||||
|
"display_name": "Append",
|
||||||
|
"info": "If True, the record will be appended to the state.",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
def build(
|
||||||
|
self, name: str, record: Union[Text, Record], append: bool = False
|
||||||
|
) -> Record:
|
||||||
|
if append:
|
||||||
|
self.append_state(name, record)
|
||||||
|
else:
|
||||||
|
self.update_state(name, record)
|
||||||
|
|
||||||
|
state = self.get_state(name)
|
||||||
|
if not isinstance(state, Record):
|
||||||
|
if isinstance(state, str):
|
||||||
|
state = Record(text=state)
|
||||||
|
elif isinstance(state, dict):
|
||||||
|
state = Record(data=state)
|
||||||
|
else:
|
||||||
|
state = Record(text=str(state))
|
||||||
|
return state
|
||||||
Loading…
Add table
Add a link
Reference in a new issue