iamai.message module¶
iamai message module.
Implemented the commonly used basic message Message and message field MessageSegment models for adapter use.
Adapter developers can implement subclasses of the message classes in this module or define different message types as needed, but it is recommended that subclasses of the message classes in this module be used if possible.
- class iamai.message.Message(*messages: List[MessageSegmentT] | MessageSegmentT | str | Mapping[str, Any])[source]¶
Bases:
ABC,List[MessageSegmentT]Message.
This class is a subclass of
Listand overrides the__init__()method. Can handle str,Mapping,MessageSegment,List[MessageSegment]directly. This class overloads the+and+=operators, and can directly perform sum operations with objects ofMessage,MessageSegmentand other types of objects. Adapter developers need to inherit this class and override theget_segment_class()method.- endswith(suffix: str | MessageSegmentT, start: SupportsIndex | None = None, end: SupportsIndex | None = None) bool[source]¶
Implement string-like
endswith()method.When the
suffixtype is str, it will convert itself to thestrtype, and then call theendswith()method of thestrtype. When thesuffixtype is MessageSegment, the start andendparameters will have no effect.Will determine whether the last message field in the list is equal to suffix.
- Parameters:
suffix – suffix.
start – Start checking the position.
end – Stop checking the position.
- Returns:
test result.
- get_plain_text() str[source]¶
Get the plain text part of the message.
- Returns:
The plain text portion of the message.
- abstract classmethod get_segment_class() Type[MessageSegmentT][source]¶
Get the message field class.
- Returns:
Message field class.
- replace(old: str, new: str, count: int = -1) Self[source]¶
- replace(old: MessageSegmentT, new: MessageSegmentT | None, count: int = -1) Self
Implement string-like
replace()method.When
oldis of typestr,newmust also bestr, and this method will only process the message fields whereis_text()isTrue. Whenoldis of MessageSegment type,newcan beMessageSegmentorNone, and this method will process all message fields.And replace the message fields that meet the criteria.
Nonemeans to delete the matching message field.- Parameters:
old – The matched string or message field.
new – The string or message field to be replaced.
count – the number of replacements.
- Returns:
The replaced message object.
- startswith(prefix: str | MessageSegmentT, start: SupportsIndex | None = None, end: SupportsIndex | None = None) bool[source]¶
Implement string-like
startswith()method.When the
prefixtype isstr, it will convert itself to thestrtype, and then call thestartswith()method of thestrtype. When theprefixtype isMessageSegment, thestartand end parameters will have no effect,It will be judged whether the first message field of the list is equal to
prefix.- Parameters:
prefix – prefix.
start – Start checking the position.
end – Stop checking the position.
- Returns:
test result.
- class iamai.message.MessageSegment(*, type: str, data: ~typing.Dict[str, ~typing.Any] = <factory>)[source]¶
Bases:
ABC,BaseModel,Mapping[str,Any],Generic[MessageT]Message field.
This class implements all
Mappingtype methods, all of which operate on thedataattribute. This class overrides the+and+=operators, and can directly perform sum operations with objects of types such asMessage,MessageSegmentand return aMessageobject. Adapter developers need to inherit this class and override theget_message_class()method.- type¶
message field type.
- Type:
str
- data¶
message field content.
- Type:
Dict[str, Any]
- data: Dict[str, Any]¶
- classmethod from_mapping(msg: Mapping[Any, Any]) Self[source]¶
Used to convert
Mappinginto message fields.Subclasses can override this method to change the default behavior for
Mappingif necessary.- Parameters:
msg – Data to be parsed into message fields.
- Returns:
Message fields converted by Mapping.
- abstract classmethod from_str(msg: str) Self[source]¶
Used to convert
strto a message field, subclasses should override this method.- Parameters:
msg – Data to be parsed into message fields.
- Returns:
Message fields converted by
str.
- get(key: str, default: Any | None = None) Any[source]¶
Returns the value of
keyifkeyexists in thedatadictionary, otherwise returnsdefault.
- abstract classmethod get_message_class() Type[MessageT][source]¶
Get the message class.
- Returns:
Message class.
- is_text() bool[source]¶
is a plain text message field.
- Returns:
Whether it is a plain text message field.
- items() ItemsView[str, Any][source]¶
Returns a new view consisting of
datadictionary items ((key, value)pairs).
- model_config: ClassVar[ConfigDict] = {}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- type: str¶