openai/chatkit-python

Public

mirrored fromhttps://github.com/openai/chatkit-pythonAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.5.2

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

chatkit/actions.py

63lines · modecode

1from __future__ import annotations
2
3from typing import Any, Generic, Literal, TypeVar, get_args, get_origin
4
5from pydantic import BaseModel, Field
6from typing_extensions import deprecated
7
8Handler = Literal["client", "server"]
9LoadingBehavior = Literal["auto", "none", "self", "container"]
10
11DEFAULT_HANDLER: Handler = "server"
12DEFAULT_LOADING_BEHAVIOR: LoadingBehavior = "auto"
13
14
15_direct_usage_of_action_classes_deprecated = deprecated(
16 "Direct usage of named action classes is deprecated. "
17 "Use WidgetTemplate to build widgets from .widget files instead. "
18 "Visit https://widgets.chatkit.studio/ to author widget files."
19)
20
21
22@_direct_usage_of_action_classes_deprecated
23class ActionConfig(BaseModel):
24 type: str
25 payload: Any = None
26 handler: Handler = DEFAULT_HANDLER
27 loadingBehavior: LoadingBehavior = DEFAULT_LOADING_BEHAVIOR
28
29
30TType = TypeVar("TType", bound=str)
31TPayload = TypeVar("TPayload")
32
33
34@_direct_usage_of_action_classes_deprecated
35class Action(BaseModel, Generic[TType, TPayload]):
36 type: TType = Field(default=TType, frozen=True) # pyright: ignore
37 payload: TPayload = None # pyright: ignore - default to None to allow no-payload actions
38
39 @classmethod
40 def create(
41 cls,
42 payload: TPayload,
43 handler: Handler = DEFAULT_HANDLER,
44 loading_behavior: LoadingBehavior = DEFAULT_LOADING_BEHAVIOR,
45 ) -> ActionConfig:
46 actionType: Any = None
47 anno = cls.model_fields["type"].annotation
48 if get_origin(anno) is Literal:
49 lits = get_args(anno)
50 if len(lits) == 1 and isinstance(lits[0], str):
51 actionType = lits[0]
52
53 if actionType is None:
54 raise TypeError(
55 "Cannot infer 'type' for this Action[...]. Do not call create() on generic Action."
56 )
57
58 return ActionConfig(
59 type=actionType,
60 payload=payload,
61 handler=handler,
62 loadingBehavior=loading_behavior,
63 )
64