Skip to content

Commit 4dbdad7

Browse files
committed
Added generics for state & forwarded props; poetry -> uv; linting
Changelog: changed
1 parent 8442577 commit 4dbdad7

File tree

9 files changed

+87
-35
lines changed

9 files changed

+87
-35
lines changed

python-sdk/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,4 @@ venv.bak/
6363

6464
# Project specific
6565
.DS_Store
66+
/uv.lock

python-sdk/.pre-commit-config.yaml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
repos:
2+
- repo: https://github.com/pre-commit/pre-commit-hooks
3+
rev: v5.0.0
4+
hooks:
5+
- id: trailing-whitespace
6+
exclude_types: [ jupyter ]
7+
- id: end-of-file-fixer
8+
exclude_types: [ jupyter ]
9+
- id: check-docstring-first
10+
- id: debug-statements
11+
- id: check-ast
12+
- repo: https://github.com/charliermarsh/ruff-pre-commit
13+
rev: v0.11.8
14+
hooks:
15+
- id: ruff
16+
args: [
17+
--fix
18+
]
19+
- id: ruff-format
20+
- repo: https://github.com/pre-commit/mirrors-mypy
21+
rev: v1.15.0
22+
hooks:
23+
- id: mypy
24+
args: [
25+
--python-version=3.12,
26+
--disallow-untyped-calls,
27+
--disallow-untyped-defs,
28+
--disallow-incomplete-defs,
29+
--check-untyped-defs,
30+
--no-implicit-optional,
31+
--warn-redundant-casts,
32+
--ignore-missing-imports,
33+
]
34+
additional_dependencies:
35+
- "types-pytz"
36+
exclude_types: [ jupyter ]
37+
exclude: "tests"
38+
- repo: https://github.com/kynan/nbstripout
39+
rev: 0.8.1
40+
hooks:
41+
- id: nbstripout

python-sdk/ag_ui/__init__.py

Whitespace-only changes.

python-sdk/ag_ui/core/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
Context,
4747
Tool,
4848
RunAgentInput,
49-
State
5049
)
5150

5251
__all__ = [
@@ -92,5 +91,4 @@
9291
"Context",
9392
"Tool",
9493
"RunAgentInput",
95-
"State"
9694
]

python-sdk/ag_ui/core/events.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
"""
44

55
from enum import Enum
6-
from typing import Annotated, Any, List, Literal, Optional, Union
6+
from typing import Annotated, List, Literal, Optional, Union, Generic
77

88
from pydantic import Field
99

10-
from .types import ConfiguredBaseModel, Message, State
10+
from .types import ConfiguredBaseModel, Message, AgentStateT, JSONValue
1111

1212

1313
class EventType(str, Enum):
@@ -46,7 +46,7 @@ class BaseEvent(ConfiguredBaseModel):
4646
"""
4747
type: EventType
4848
timestamp: Optional[int] = None
49-
raw_event: Optional[Any] = None
49+
raw_event: Optional[JSONValue] = None
5050

5151

5252
class TextMessageStartEvent(BaseEvent):
@@ -161,20 +161,20 @@ class ThinkingEndEvent(BaseEvent):
161161
"""
162162
type: Literal[EventType.THINKING_END] = EventType.THINKING_END # pyright: ignore[reportIncompatibleVariableOverride]
163163

164-
class StateSnapshotEvent(BaseEvent):
164+
class StateSnapshotEvent(BaseEvent, Generic[AgentStateT]):
165165
"""
166166
Event containing a snapshot of the state.
167167
"""
168168
type: Literal[EventType.STATE_SNAPSHOT] = EventType.STATE_SNAPSHOT # pyright: ignore[reportIncompatibleVariableOverride]
169-
snapshot: State
169+
snapshot: AgentStateT
170170

171171

172172
class StateDeltaEvent(BaseEvent):
173173
"""
174174
Event containing a delta of the state.
175175
"""
176176
type: Literal[EventType.STATE_DELTA] = EventType.STATE_DELTA # pyright: ignore[reportIncompatibleVariableOverride]
177-
delta: List[Any] # JSON Patch (RFC 6902)
177+
delta: JSONValue # JSON Patch (RFC 6902)
178178

179179

180180
class MessagesSnapshotEvent(BaseEvent):
@@ -190,7 +190,7 @@ class RawEvent(BaseEvent):
190190
Event containing a raw event.
191191
"""
192192
type: Literal[EventType.RAW] = EventType.RAW # pyright: ignore[reportIncompatibleVariableOverride]
193-
event: Any
193+
event: JSONValue
194194
source: Optional[str] = None
195195

196196

@@ -200,7 +200,7 @@ class CustomEvent(BaseEvent):
200200
"""
201201
type: Literal[EventType.CUSTOM] = EventType.CUSTOM # pyright: ignore[reportIncompatibleVariableOverride]
202202
name: str
203-
value: Any
203+
value: JSONValue
204204

205205

206206
class RunStartedEvent(BaseEvent):
@@ -219,7 +219,7 @@ class RunFinishedEvent(BaseEvent):
219219
type: Literal[EventType.RUN_FINISHED] = EventType.RUN_FINISHED # pyright: ignore[reportIncompatibleVariableOverride]
220220
thread_id: str
221221
run_id: str
222-
result: Optional[Any] = None
222+
result: JSONValue = None
223223

224224

225225
class RunErrorEvent(BaseEvent):

python-sdk/ag_ui/core/types.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@
22
This module contains the types for the Agent User Interaction Protocol Python SDK.
33
"""
44

5-
from typing import Annotated, Any, List, Literal, Optional, Union
5+
from typing import Annotated, Any, List, Literal, Optional, Union, Generic
6+
from typing_extensions import TypeVar
67

78
from pydantic import BaseModel, ConfigDict, Field
89
from pydantic.alias_generators import to_camel
910

11+
JSONValue = Union[str, int, float, bool, None, dict[str, Any], list[Any]]
12+
AgentStateT = TypeVar('AgentStateT', default=JSONValue, contravariant=True)
13+
FwdPropsT = TypeVar('FwdPropsT', default=JSONValue, contravariant=True)
14+
1015

1116
class ConfiguredBaseModel(BaseModel):
1217
"""
@@ -51,15 +56,13 @@ class DeveloperMessage(BaseMessage):
5156
A developer message.
5257
"""
5358
role: Literal["developer"] = "developer" # pyright: ignore[reportIncompatibleVariableOverride]
54-
content: str
5559

5660

5761
class SystemMessage(BaseMessage):
5862
"""
5963
A system message.
6064
"""
6165
role: Literal["system"] = "system" # pyright: ignore[reportIncompatibleVariableOverride]
62-
content: str
6366

6467

6568
class AssistantMessage(BaseMessage):
@@ -75,7 +78,6 @@ class UserMessage(BaseMessage):
7578
A user message.
7679
"""
7780
role: Literal["user"] = "user" # pyright: ignore[reportIncompatibleVariableOverride]
78-
content: str
7981

8082

8183
class ToolMessage(ConfiguredBaseModel):
@@ -114,18 +116,14 @@ class Tool(ConfiguredBaseModel):
114116
parameters: Any # JSON Schema for the tool parameters
115117

116118

117-
class RunAgentInput(ConfiguredBaseModel):
119+
class RunAgentInput(ConfiguredBaseModel, Generic[AgentStateT, FwdPropsT]):
118120
"""
119121
Input for running an agent.
120122
"""
121123
thread_id: str
122124
run_id: str
123-
state: Any
125+
state: AgentStateT
124126
messages: List[Message]
125127
tools: List[Tool]
126128
context: List[Context]
127-
forwarded_props: Any
128-
129-
130-
# State can be any type
131-
State = Any
129+
forwarded_props: FwdPropsT

python-sdk/ag_ui/encoder/encoder.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class EventEncoder:
1010
"""
1111
Encodes Agent User Interaction events.
1212
"""
13-
def __init__(self, accept: str = None):
13+
def __init__(self, accept: str | None = None):
1414
pass
1515

1616
def get_content_type(self) -> str:

python-sdk/pyproject.toml

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,30 @@
1-
[tool.poetry]
1+
[project]
22
name = "ag-ui-protocol"
3-
version = "0.1.8"
3+
version = "0.1.9"
44
description = ""
5-
authors = ["Markus Ecker <[email protected]>"]
5+
authors = [
6+
{ name = "Markus Ecker", email = "[email protected]" },
7+
]
68
readme = "README.md"
7-
packages = [{include = "ag_ui", from = "."}]
8-
[tool.poetry.dependencies]
9-
python = "^3.9"
10-
pydantic = "^2.11.2"
9+
requires-python = ">=3.9,<4.0"
10+
dependencies = [
11+
"pydantic>=2.11.2,<3.0.0",
12+
]
13+
packages = [
14+
{ include = "ag_ui", from = "ag_ui" }
15+
]
16+
17+
[tool.hatch.build.targets.wheel]
18+
packages = ["ag_ui"]
1119

1220

1321
[build-system]
14-
requires = ["poetry-core"]
15-
build-backend = "poetry.core.masonry.api"
22+
requires = ["hatchling"]
23+
build-backend = "hatchling.build"
24+
25+
[dependency-groups]
26+
dev = [
27+
"mypy>=1.17.1",
28+
"pyright>=1.1.403",
29+
"ruff>=0.12.9",
30+
]

python-sdk/tests/test_events.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import unittest
2-
import json
32
from datetime import datetime
4-
from pydantic import ValidationError, TypeAdapter
3+
from pydantic import TypeAdapter
54

6-
from ag_ui.core.types import Message, UserMessage, AssistantMessage, FunctionCall, ToolCall
5+
from ag_ui.core.types import UserMessage, AssistantMessage, FunctionCall, ToolCall
76
from ag_ui.core.events import (
87
EventType,
98
BaseEvent,

0 commit comments

Comments
 (0)