Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions chatkit/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from typing import Any, Generic, Literal, TypeVar, get_args, get_origin

from pydantic import BaseModel, Field
from typing_extensions import deprecated

Handler = Literal["client", "server"]
LoadingBehavior = Literal["auto", "none", "self", "container"]
Expand All @@ -11,6 +12,14 @@
DEFAULT_LOADING_BEHAVIOR: LoadingBehavior = "auto"


_direct_usage_of_action_classes_deprecated = deprecated(
"Direct usage of named action classes is deprecated. "
"Use WidgetTemplate to build widgets from .widget files instead. "
"Visit https://widgets.chatkit.studio/ to author widget files."
)


@_direct_usage_of_action_classes_deprecated
class ActionConfig(BaseModel):
type: str
payload: Any = None
Expand All @@ -22,6 +31,7 @@ class ActionConfig(BaseModel):
TPayload = TypeVar("TPayload")


@_direct_usage_of_action_classes_deprecated
class Action(BaseModel, Generic[TType, TPayload]):
type: TType = Field(default=TType, frozen=True) # pyright: ignore
payload: TPayload = None # pyright: ignore - default to None to allow no-payload actions
Expand Down
28 changes: 18 additions & 10 deletions chatkit/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
is_streaming_req,
)
from .version import __version__
from .widgets import Markdown, Text, WidgetComponent, WidgetComponentBase, WidgetRoot
from .widgets import WidgetComponent, WidgetComponentBase, WidgetRoot

DEFAULT_PAGE_SIZE = 20
DEFAULT_ERROR_MESSAGE = "An error occurred when generating a response."
Expand All @@ -82,6 +82,11 @@ def diff_widget(
Compare two WidgetRoots and return a list of deltas.
"""

def is_streaming_text(component: WidgetComponentBase) -> bool:
return getattr(component, "type", None) in {"Markdown", "Text"} and isinstance(
getattr(component, "value", None), str
)

def full_replace(before: WidgetComponentBase, after: WidgetComponentBase) -> bool:
if (
before.type != after.type
Expand All @@ -108,10 +113,10 @@ def full_replace_value(before_value: Any, after_value: Any) -> bool:

for field in before.model_fields_set.union(after.model_fields_set):
if (
isinstance(before, (Markdown, Text))
and isinstance(after, (Markdown, Text))
is_streaming_text(before)
and is_streaming_text(after)
and field == "value"
and after.value.startswith(before.value)
and getattr(after, "value", "").startswith(getattr(before, "value", ""))
):
# Appends to the value prop of Markdown or Text do not trigger a full replace
continue
Expand All @@ -129,11 +134,11 @@ def full_replace_value(before_value: Any, after_value: Any) -> bool:

def find_all_streaming_text_components(
component: WidgetComponent | WidgetRoot,
) -> dict[str, Markdown | Text]:
) -> dict[str, WidgetComponentBase]:
components = {}

def recurse(component: WidgetComponent | WidgetRoot):
if isinstance(component, (Markdown, Text)) and component.id:
if is_streaming_text(component) and component.id:
components[component.id] = component

if hasattr(component, "children"):
Expand All @@ -154,16 +159,19 @@ def recurse(component: WidgetComponent | WidgetRoot):
f"Node {id} was not present when the widget was initially rendered. All nodes with ID must persist across all widget updates."
)

if before_node.value != after_node.value:
if not after_node.value.startswith(before_node.value):
before_value = str(getattr(before_node, "value", None))
after_value = str(getattr(after_node, "value", None))

if before_value != after_value:
if not after_value.startswith(before_value):
raise ValueError(
f"Node {id} was updated with a new value that is not a prefix of the initial value. All widget updates must be cumulative."
)
done = not after_node.streaming
done = not getattr(after_node, "streaming", False)
deltas.append(
WidgetStreamingTextValueDelta(
component_id=id,
delta=after_node.value[len(before_node.value) :],
delta=after_value[len(before_value) :],
done=done,
)
)
Expand Down
Loading