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 |
str
|
Semantic role — |
content |
str | list[ContentBlock]
|
Message body as plain text or ACP content blocks. |
message_id |
str
|
Stable 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[ACPToolCall] | None
|
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. |
usage |
dict[str, Any] | None
|
Token or billing metadata. |
stopped |
bool
|
Whether generation stopped early (e.g. cancellation). |
Attributes¶
message_id
class-attribute
instance-attribute
¶
metadata
class-attribute
instance-attribute
¶
Functions¶
validate_content_length
classmethod
¶
Reject content exceeding MAX_CONTENT_LENGTH.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
str | list[ContentBlock]
|
Candidate message content. |
required |
Returns:
| Type | Description |
|---|---|
str | list[ContentBlock]
|
The original content when it satisfies size limits. |
Raises:
| Type | Description |
|---|---|
ValueError
|
When plain-text content exceeds |
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 |
str
|
Lifecycle state — |
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
¶
Bases: BaseModel
Plain text content block.
Attributes:
| Name | Type | Description |
|---|---|---|
type |
str
|
Discriminator — always |
text |
str
|
Plain text payload. |
annotations |
dict[str, Any] | None
|
Optional ACP annotations. |
pywry.chat.ImagePart
¶
Bases: BaseModel
Base64-encoded image content block.
Attributes:
| Name | Type | Description |
|---|---|---|
type |
str
|
Discriminator — always |
data |
str
|
Base64-encoded image bytes. |
mime_type |
str
|
MIME type (e.g. |
uri |
str | None
|
Optional source URI. |
annotations |
dict[str, Any] | None
|
Optional ACP annotations. |
pywry.chat.ResourceLinkPart
¶
Bases: BaseModel
Reference to an agent-accessible resource without embedding content.
Attributes:
| Name | Type | Description |
|---|---|---|
type |
str
|
Discriminator — always |
uri |
str
|
Resource URI. |
name |
str
|
Human-readable label. |
mimeType |
str | None
|
MIME type of the linked resource. |
title |
str | None
|
Display title. |
description |
str | None
|
Longer description of the resource. |
size |
int | None
|
Resource size in bytes. |
annotations |
dict[str, Any] | None
|
Optional ACP annotations. |
Tool Call Models¶
pywry.chat.ACPToolCall
¶
Bases: BaseModel
ACP tool invocation attached to an assistant message.
Attributes:
| Name | Type | Description |
|---|---|---|
toolCallId |
str
|
Unique identifier within the session. |
title |
str
|
Human-readable description shown in the UI. |
name |
str
|
Tool name. |
kind |
str
|
Category from the ACP taxonomy. |
status |
str
|
Lifecycle state. |
arguments |
dict[str, Any]
|
Tool arguments. |
content |
list[ContentBlock] | None
|
Result content blocks (populated on completion). |
locations |
list[ToolCallLocation] | None
|
Affected file paths with optional line numbers. |
pywry.chat.ToolCallLocation
¶
Configuration¶
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. |
temperature |
float
|
Sampling temperature. |
max_tokens |
int
|
Maximum token budget per generation. |
streaming |
bool
|
Enable streaming responses. |
persist |
bool
|
Persist chat history between sessions. |
pywry.chat.ChatWidgetConfig
¶
Bases: BaseModel
Full widget configuration including UI and chat settings.
Attributes:
| Name | Type | Description |
|---|---|---|
title |
str
|
Window or panel title. |
width |
int
|
Initial widget width in pixels. |
height |
int
|
Initial widget height in pixels. |
theme |
str
|
Preferred widget theme. |
show_sidebar |
bool
|
Show conversation-management UI. |
show_settings |
bool
|
Show chat settings controls. |
toolbar_position |
str
|
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 |
str
|
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.
Uses cooperative cancellation: 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. |
cancel_event |
Event
|
Cooperative cancellation signal. |
message_id |
str
|
Assistant message being populated. |
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. |
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
|
|
partial_content
property
¶
Return content accumulated so far.
Returns:
| Type | Description |
|---|---|
str
|
Concatenated streamed chunks. |
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
|
|
pywry.chat.GenerationCancelledError
¶
Bases: Exception
Raised by providers when cancel_event is detected mid-stream.
Attributes:
| Name | Type | Description |
|---|---|---|
partial_content |
str
|
Content accumulated before cancellation. |
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.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
show_sidebar
|
bool
|
Include the thread/conversation picker in the header bar. |
True
|
show_settings
|
bool
|
Include the settings toggle button in the header. |
True
|
enable_context
|
bool
|
Enable |
False
|
enable_file_attach
|
bool
|
Show the attach button and enable drag-and-drop file attachments. |
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. |
''
|
Returns:
| Type | Description |
|---|---|
str
|
HTML string for the chat widget. |