Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated the type checking in order to fix #28 and #29. #32

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 7 additions & 5 deletions convoviz/models/_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from __future__ import annotations

from typing import TYPE_CHECKING, Any, ClassVar, Literal
from typing import TYPE_CHECKING, Any, ClassVar, Literal, Union, Dict, List

from pydantic import BaseModel, ConfigDict

Expand All @@ -16,6 +16,9 @@

AuthorRole = Literal["user", "assistant", "system", "tool"]

# Define a new type that can be either a string or a dictionary
PartType = Union[str, Dict[str, Any]]


class MessageAuthor(BaseModel):
"""Type of the `author` field in a `message`."""
Expand All @@ -24,16 +27,14 @@ class MessageAuthor(BaseModel):
name: str | None = None
metadata: dict[str, Any]


class MessageContent(BaseModel):
"""Type of the `content` field in a `message`."""

content_type: str
parts: list[str] | None = None
parts: List[PartType] | None = None
text: str | None = None
result: str | None = None


class MessageMetadata(BaseModel):
"""Type of the `metadata` field in a `message`."""

Expand Down Expand Up @@ -77,12 +78,13 @@ def header(self) -> str:
@property
def text(self) -> str:
"""Get the text content of the message."""
if self.content.parts is not None:
if self.content.parts is not None and isinstance(self.content.parts[0], str):
return str(self.content.parts[0])
if self.content.text is not None:
return code_block(self.content.text)
if self.content.result is not None:
return self.content.result
return ""

# this error caught some hidden bugs in the data. need more of these
err_msg = f"No valid content found in message: {self.id}"
Expand Down