Fix: 修复 MessageSegment 在有额外数据时报错 (#1055)

This commit is contained in:
Ju4tCode
2022-06-20 15:52:12 +08:00
committed by GitHub
parent c91c9380a7
commit f11970132c
3 changed files with 18 additions and 14 deletions

View File

@ -66,7 +66,11 @@ class MessageSegment(abc.ABC, Generic[TM]):
return value
if not isinstance(value, dict):
raise ValueError(f"Expected dict for MessageSegment, got {type(value)}")
return cls(**value)
if "type" not in value:
raise ValueError(
f"Expected dict with 'type' for MessageSegment, got {value}"
)
return cls(type=value["type"], data=value.get("data", {}))
def get(self, key: str, default: Any = None):
return asdict(self).get(key, default)