openai/chatkit-python

Public

mirrored from https://github.com/openai/chatkit-pythonAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.5.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

chatkit/actions.py

63lines · modeblame

f688d870victor-openai8 months ago1from __future__ import annotations
2
3from typing import Any, Generic, Literal, TypeVar, get_args, get_origin
4
5from pydantic import BaseModel, Field
69ea9ef8Jiwon Kim7 months ago6from typing_extensions import deprecated
f688d870victor-openai8 months ago7
8Handler = Literal["client", "server"]
9LoadingBehavior = Literal["auto", "none", "self", "container"]
10
11DEFAULT_HANDLER: Handler = "server"
12DEFAULT_LOADING_BEHAVIOR: LoadingBehavior = "auto"
13
14
1b15f7deJiwon Kim7 months ago15_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."
69ea9ef8Jiwon Kim7 months ago19)
20
21
1b15f7deJiwon Kim7 months ago22@_direct_usage_of_action_classes_deprecated
f688d870victor-openai8 months ago23class ActionConfig(BaseModel):
24type: str
25payload: Any = None
26handler: Handler = DEFAULT_HANDLER
27loadingBehavior: LoadingBehavior = DEFAULT_LOADING_BEHAVIOR
28
29
30TType = TypeVar("TType", bound=str)
31TPayload = TypeVar("TPayload")
32
33
1b15f7deJiwon Kim7 months ago34@_direct_usage_of_action_classes_deprecated
f688d870victor-openai8 months ago35class Action(BaseModel, Generic[TType, TPayload]):
36type: TType = Field(default=TType, frozen=True) # pyright: ignore
367da6d8David Weedon7 months ago37payload: TPayload = None # pyright: ignore - default to None to allow no-payload actions
f688d870victor-openai8 months ago38
39@classmethod
40def create(
41cls,
42payload: TPayload,
43handler: Handler = DEFAULT_HANDLER,
44loading_behavior: LoadingBehavior = DEFAULT_LOADING_BEHAVIOR,
45) -> ActionConfig:
46actionType: Any = None
47anno = cls.model_fields["type"].annotation
48if get_origin(anno) is Literal:
49lits = get_args(anno)
50if len(lits) == 1 and isinstance(lits[0], str):
51actionType = lits[0]
52
53if actionType is None:
54raise TypeError(
55"Cannot infer 'type' for this Action[...]. Do not call create() on generic Action."
56)
57
58return ActionConfig(
59type=actionType,
60payload=payload,
61handler=handler,
62loadingBehavior=loading_behavior,
63)