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 List and 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 of Message, MessageSegment and other types of objects. Adapter developers need to inherit this class and override the get_segment_class() method.

copy() Self[source]

Return a shallow copy of itself.

Returns:

A shallow copy of itself.

endswith(suffix: str | MessageSegmentT, start: SupportsIndex | None = None, end: SupportsIndex | None = None) bool[source]

Implement string-like endswith() method.

When the suffix type is str, it will convert itself to the str type, and then call the endswith() method of the str type. When the suffix type is MessageSegment, the start and end parameters 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.

is_text() bool[source]

Whether it is a plain text message.

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 old is of type str, new must also be str, and this method will only process the message fields where is_text() is True. When old is of MessageSegment type, new can be MessageSegment or None, and this method will process all message fields.

And replace the message fields that meet the criteria. None means 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 prefix type is str, it will convert itself to the str type, and then call the startswith() method of the str type. When the prefix type is MessageSegment, the start and 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 Mapping type methods, all of which operate on the data attribute. This class overrides the + and += operators, and can directly perform sum operations with objects of types such as Message, MessageSegment and return a Message object. Adapter developers need to inherit this class and override the get_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 Mapping into message fields.

Subclasses can override this method to change the default behavior for Mapping if 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 str to 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 key if key exists in the data dictionary, otherwise returns default.

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 data dictionary items ((key, value) pairs).

keys() KeysView[str][source]

Returns a new view consisting of data dictionary keys.

model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

type: str
values() ValuesView[Any][source]

Returns a new view consisting of data dictionary values.