pywry.chat¶
Core chat data models, configuration objects, and HTML builder helpers.
Message Models¶
pywry.chat.ChatMessage
¶
Bases: BaseModel
A single chat message.
Attributes:
| Name | Type | Description |
|---|---|---|
role |
Literal['user', 'assistant', 'system', 'tool']
|
Semantic role of the message within the conversation. |
content |
str | list[ChatContentPart]
|
Message body as plain text or structured content parts. |
message_id |
str
|
Stable message identifier used across UI and backend events. |
timestamp |
float
|
Unix timestamp when the message was created. |
metadata |
dict[str, Any]
|
Arbitrary provider- or application-specific metadata. |
tool_calls |
list[ToolCall] | None
|
Requested tool invocations attached to assistant messages. |
tool_call_id |
str | None
|
Tool-call identifier when this message is a tool result. |
model |
str | None
|
Model name that produced the message, when known. |
usage |
dict[str, Any] | None
|
Token or billing metadata returned by the provider. |
stopped |
bool
|
Indicates generation stopped early, typically due to cancellation. |
Attributes¶
message_id
class-attribute
instance-attribute
¶
metadata
class-attribute
instance-attribute
¶
Classes¶
Config
¶
Pydantic model configuration.
Functions¶
validate_content_length
classmethod
¶
Reject content exceeding MAX_CONTENT_LENGTH.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
str | list[ChatContentPart]
|
Candidate message content provided to the model. |
required |
Returns:
| Type | Description |
|---|---|
str | list[ChatContentPart]
|
The original content when it satisfies size limits. |
Raises:
| Type | Description |
|---|---|
ValueError
|
Raised when plain-text content exceeds MAX_CONTENT_LENGTH. |
text_content
¶
Return the plain-text content regardless of content type.
Returns:
| Type | Description |
|---|---|
str
|
The plain-text message body, flattening structured text parts. |
pywry.chat.ChatThread
¶
Bases: BaseModel
A conversation thread containing messages.
Attributes:
| Name | Type | Description |
|---|---|---|
thread_id |
str
|
Stable identifier for the conversation thread. |
title |
str
|
Human-readable thread title shown in the UI. |
messages |
list[ChatMessage]
|
Ordered transcript of messages in the thread. |
created_at |
float
|
Unix timestamp when the thread was created. |
updated_at |
float
|
Unix timestamp when the thread was last updated. |
metadata |
dict[str, Any]
|
Arbitrary application-specific thread metadata. |
status |
Literal['active', 'archived']
|
Lifecycle state of the thread in the chat UI. |
Attributes¶
thread_id
class-attribute
instance-attribute
¶
messages
class-attribute
instance-attribute
¶
messages: list[ChatMessage] = Field(default_factory=list)
metadata
class-attribute
instance-attribute
¶
Content Parts¶
pywry.chat.TextPart
¶
pywry.chat.ImagePart
¶
pywry.chat.ResourceLinkPart
¶
Bases: BaseModel
Resource link content part.
Attributes:
| Name | Type | Description |
|---|---|---|
type |
Literal['resource_link']
|
Discriminator used when serializing mixed chat content parts. |
uri |
str
|
Resource URI exposed to the frontend or MCP consumer. |
name |
str | None
|
Optional human-readable label for the resource. |
mime_type |
str | None
|
Optional MIME type associated with the linked resource. |
Tool Call Models¶
pywry.chat.ToolCallFunction
¶
pywry.chat.ToolCall
¶
Bases: BaseModel
Tool invocation attached to an assistant message.
Attributes:
| Name | Type | Description |
|---|---|---|
id |
str
|
Stable tool-call identifier used to correlate tool results. |
type |
Literal['function']
|
Tool-call type discriminator. |
function |
ToolCallFunction
|
Function name and serialized arguments requested by the model. |
Attributes¶
Configuration¶
pywry.chat.SlashCommand
¶
Bases: BaseModel
A slash command registered for the chat input.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
Slash-prefixed command name entered by the user. |
description |
str
|
Human-readable description displayed in command pickers. |
handler_event |
str
|
Backend event name fired when the command is invoked. |
args_schema |
dict[str, Any] | None
|
Optional JSON schema describing accepted command arguments. |
builtin |
bool
|
Indicates whether the command ships with PyWry by default. |
pywry.chat.ChatConfig
¶
Bases: BaseModel
Configuration for the chat engine.
Attributes:
| Name | Type | Description |
|---|---|---|
system_prompt |
str | None
|
Optional system prompt prepended to model conversations. |
model |
str
|
Default model identifier used for generations. |
temperature |
float
|
Sampling temperature passed to provider backends. |
max_tokens |
int
|
Maximum token budget requested per generation. |
streaming |
bool
|
Enables streaming responses when supported by the provider. |
persist |
bool
|
Persists chat history between sessions when enabled. |
slash_commands |
list[SlashCommand]
|
Commands exposed in the chat input UI. |
provider |
str | None
|
Explicit provider name when overriding model-based selection. |
provider_config |
dict[str, Any]
|
Arbitrary provider-specific configuration values. |
Attributes¶
temperature
class-attribute
instance-attribute
¶
slash_commands
class-attribute
instance-attribute
¶
slash_commands: list[SlashCommand] = Field(default_factory=_default_slash_commands)
provider_config
class-attribute
instance-attribute
¶
pywry.chat.ChatWidgetConfig
¶
Bases: BaseModel
Full widget configuration including UI and chat settings.
Attributes:
| Name | Type | Description |
|---|---|---|
title |
str
|
Window or panel title presented to the user. |
width |
int
|
Initial widget width in pixels. |
height |
int
|
Initial widget height in pixels. |
theme |
Literal['dark', 'light', 'system']
|
Preferred widget theme. |
show_sidebar |
bool
|
Controls visibility of conversation-management UI. |
show_settings |
bool
|
Controls visibility of chat settings controls. |
toolbar_position |
Literal['top', 'bottom']
|
Placement of widget toolbar controls. |
chat_config |
ChatConfig
|
Nested chat-engine configuration. |
Attributes¶
toolbar_position
class-attribute
instance-attribute
¶
chat_config
class-attribute
instance-attribute
¶
chat_config: ChatConfig = Field(default_factory=ChatConfig)
pywry.chat.ChatTaskState
¶
Bases: BaseModel
Tracks an MCP task lifecycle for a chat_send_message call.
Attributes:
| Name | Type | Description |
|---|---|---|
task_id |
str
|
Stable identifier for the MCP task. |
thread_id |
str
|
Conversation thread associated with the task. |
message_id |
str
|
Message that initiated the task. |
status |
Literal['working', 'input_required', 'completed', 'failed', 'cancelled']
|
Current MCP task status. |
status_message |
str
|
Human-readable progress or error status. |
created_at |
float
|
Unix timestamp when the task state was created. |
poll_interval |
float | None
|
Suggested polling interval for clients watching task progress. |
Generation¶
pywry.chat.GenerationHandle
dataclass
¶
GenerationHandle(task: Task[Any] | None = None, cancel_event: Event = Event(), message_id: str = '', widget_id: str = '', thread_id: str = '', created_at: float = time(), _content_parts: list[str] = list())
Tracks an in-flight LLM generation for stop-button cancellation.
The cooperative cancellation pattern follows OAuthFlow._cancellation_event in auth/flow.py: the cancel_event is checked between chunks, and task.cancel() serves as a backup for non-cooperative generators.
Attributes:
| Name | Type | Description |
|---|---|---|
task |
Task[Any] | None
|
Async task performing the active generation, when available. |
cancel_event |
Event
|
Cooperative cancellation signal checked by streaming providers. |
message_id |
str
|
Assistant message being populated by the generation. |
widget_id |
str
|
Widget instance associated with the generation. |
thread_id |
str
|
Conversation thread associated with the generation. |
created_at |
float
|
Unix timestamp when the handle was created. |
_content_parts |
list[str]
|
Internal list of streamed chunks accumulated so far. |
Attributes¶
cancel_event
class-attribute
instance-attribute
¶
_content_parts
class-attribute
instance-attribute
¶
is_expired
property
¶
Check if this handle has exceeded its TTL.
Returns:
| Type | Description |
|---|---|
bool
|
True when the handle is older than GENERATION_HANDLE_TTL. |
partial_content
property
¶
Return content accumulated so far.
Returns:
| Type | Description |
|---|---|
str
|
Concatenated streamed chunks recorded on the handle. |
Functions¶
append_chunk
¶
Record a streamed chunk.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chunk
|
str
|
Incremental content emitted by a streaming provider. |
required |
cancel
¶
Request cooperative cancellation.
Returns:
| Type | Description |
|---|---|
bool
|
True if cancellation was newly requested, False if already cancelled. |
pywry.chat.GenerationCancelledError
¶
HTML Builder¶
pywry.chat.build_chat_html
¶
build_chat_html(*, show_sidebar: bool = True, show_settings: bool = True, enable_context: bool = False, enable_file_attach: bool = False, file_accept_types: list[str] | None = None, container_id: str = '', header_actions: str = '') -> str
Build the HTML structure for a chat widget.
The layout follows VS Code's Copilot Chat pattern: a compact header
bar with conversation management and settings, a full-width scrollable
message area, and an input bar at the bottom. All components are
inside a single pywry-chat container.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
show_sidebar
|
bool
|
Include the thread/conversation picker in the header bar. When False the conversation dropdown is hidden but new-chat and other header buttons remain. |
True
|
show_settings
|
bool
|
Include the settings toggle button in the header. |
True
|
enable_context
|
bool
|
Enable @mention widget references in the chat input. |
False
|
enable_file_attach
|
bool
|
Show the attach button (📎) and enable drag-and-drop file
attachments. Independent of |
False
|
file_accept_types
|
list[str] | None
|
Restrict the file picker to specific extensions (e.g.
|
None
|
container_id
|
str
|
Optional id for the outer container div. |
''
|
header_actions
|
str
|
Extra HTML injected into the header-actions area (right side of the header bar). Developers can add custom buttons here. |
''
|
Returns:
| Type | Description |
|---|---|
str
|
HTML string for the chat widget. |