Skip to content

Commit fe270fd

Browse files
committed
refactor(conversations): Improve type handling and code formatting across conversation stores to prepare for MPHU WhatsApp Service
- Refactor type annotations in conversation store methods - Update import statements to improve readability - Simplify complex type definitions in callback and firebase conversation stores - Remove unnecessary whitespace and improve code formatting - Standardize message type handling across conversation store implementations - Remove unused or redundant code sections This refactoring aims to enhance type safety and code consistency in conversation store modules.
1 parent 99847e4 commit fe270fd

File tree

9 files changed

+38
-185
lines changed

9 files changed

+38
-185
lines changed

agentle/agents/apis/object_schema.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,20 @@ def from_json_schema(
4646
) -> ObjectSchema | ArraySchema | PrimitiveSchema:
4747
"""
4848
Recursively convert a JSON Schema definition to Agentle schema types.
49-
49+
5050
This method handles deeply nested objects, arrays, and primitives,
5151
making it easy to convert complex JSON Schema definitions.
52-
52+
5353
Args:
5454
schema: JSON Schema definition (dict with 'type', 'properties', etc.)
55-
55+
5656
Returns:
5757
Appropriate schema type (ObjectSchema, ArraySchema, or PrimitiveSchema)
58-
58+
5959
Example:
6060
```python
6161
from agentle.agents.apis.object_schema import ObjectSchema
62-
62+
6363
json_schema = {
6464
"type": "object",
6565
"properties": {
@@ -79,7 +79,7 @@ def from_json_schema(
7979
}
8080
}
8181
}
82-
82+
8383
schema = ObjectSchema.from_json_schema(json_schema)
8484
```
8585
"""

agentle/agents/apis/params/boolean_param.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,21 @@ def boolean_param(
1111
location: ParameterLocation = ParameterLocation.QUERY,
1212
) -> EndpointParameter:
1313
"""Create a boolean parameter.
14-
14+
1515
Args:
1616
name: Parameter name
1717
description: Parameter description
1818
required: Whether the parameter is required
1919
default: Default value for the parameter
2020
location: Where the parameter should be placed in the request
21-
21+
2222
Returns:
2323
EndpointParameter configured for boolean values
24-
24+
2525
Example:
2626
```python
2727
from agentle.agents.apis.params.boolean_param import boolean_param
28-
28+
2929
boolean_param(
3030
name="enabled",
3131
description="Enable feature",

agentle/agents/apis/params/number_param.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def number_param(
1414
format: str | None = None,
1515
) -> EndpointParameter:
1616
"""Create a number (float/decimal) parameter.
17-
17+
1818
Args:
1919
name: Parameter name
2020
description: Parameter description
@@ -24,14 +24,14 @@ def number_param(
2424
default: Default value for the parameter
2525
location: Where the parameter should be placed in the request
2626
format: Format hint (e.g., 'float', 'double', 'decimal')
27-
27+
2828
Returns:
2929
EndpointParameter configured for number values
30-
30+
3131
Example:
3232
```python
3333
from agentle.agents.apis.params.number_param import number_param
34-
34+
3535
number_param(
3636
name="price",
3737
description="Product price",

agentle/agents/conversations/callback_conversation_store.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,7 @@ def __init__(
8181
self,
8282
get_callback: Callable[
8383
[str],
84-
Awaitable[
85-
Sequence[
86-
DeveloperMessage
87-
| UserMessage
88-
| AssistantMessage
89-
]
90-
],
84+
Awaitable[Sequence[DeveloperMessage | UserMessage | AssistantMessage]],
9185
],
9286
add_callback: Callable[
9387
[

agentle/agents/conversations/conversation_store.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
from agentle.generations.models.messages.assistant_message import AssistantMessage
66
from agentle.generations.models.messages.developer_message import DeveloperMessage
7-
from agentle.generations.models.messages.generated_assistant_message import GeneratedAssistantMessage
7+
from agentle.generations.models.messages.generated_assistant_message import (
8+
GeneratedAssistantMessage,
9+
)
810
from agentle.generations.models.messages.user_message import UserMessage
911

1012

@@ -45,7 +47,12 @@ async def get_conversation_history_async(
4547

4648
@abc.abstractmethod
4749
async def add_message_async[T = Any](
48-
self, chat_id: str, message: DeveloperMessage | UserMessage | AssistantMessage | GeneratedAssistantMessage[T]
50+
self,
51+
chat_id: str,
52+
message: DeveloperMessage
53+
| UserMessage
54+
| AssistantMessage
55+
| GeneratedAssistantMessage[T],
4956
) -> None: ...
5057

5158
@abc.abstractmethod

agentle/agents/conversations/firebase_conversation_store.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,12 @@ def __init__(
3232

3333
@override
3434
async def add_message_async[T = Any](
35-
self, chat_id: str, message: DeveloperMessage | UserMessage | AssistantMessage | GeneratedAssistantMessage[T]
35+
self,
36+
chat_id: str,
37+
message: DeveloperMessage
38+
| UserMessage
39+
| AssistantMessage
40+
| GeneratedAssistantMessage[T],
3641
) -> None:
3742
from google.cloud import firestore
3843

agentle/agents/whatsapp/whatsapp_bot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2285,7 +2285,7 @@ async def _send_response(
22852285
... )
22862286
"""
22872287
response_text = ""
2288-
2288+
22892289
if isinstance(response, GeneratedAssistantMessage):
22902290
# Check if we have structured output (parsed)
22912291
if response.parsed:

agentle/utils/parse_streaming_json.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,12 @@ def fix_common_json_issues(json_str: str) -> str:
101101
in_string = False
102102
escape_next = False
103103
last_quote_pos = -1
104-
104+
105105
for i, char in enumerate(json_str):
106106
if escape_next:
107107
escape_next = False
108108
continue
109-
if char == '\\':
109+
if char == "\\":
110110
escape_next = True
111111
continue
112112
if char == '"':
@@ -141,20 +141,22 @@ def extract_data_manually(json_str: str) -> dict[str, Any]:
141141
# Pattern: "key": "value..." - capture everything until the next unescaped quote or EOF
142142
string_pattern = r'["\']([\w]+)["\']:\s*["\']([^"\']*?)(?:["\']|$)'
143143
string_matches = re.findall(string_pattern, json_str, re.DOTALL)
144-
144+
145145
# Also try to capture very long strings that span multiple lines
146146
# This catches incomplete strings during streaming
147147
long_string_pattern = r'["\']([\w_]+)["\']:\s*["\'](.+?)(?:["\'],?\s*["}]|$)'
148148
long_matches = re.findall(long_string_pattern, json_str, re.DOTALL)
149149

150150
for key, value in string_matches:
151151
data[key] = value
152-
152+
153153
# Prefer long_matches for fields that might be truncated in string_matches
154154
for key, value in long_matches:
155155
# Only override if the long match has more content
156156
existing = data.get(key, "")
157-
if key not in data or (isinstance(existing, str) and len(value) > len(existing)):
157+
if key not in data or (
158+
isinstance(existing, str) and len(value) > len(existing)
159+
):
158160
data[key] = value
159161

160162
# Extract string key-value pairs with unquoted keys

examples/test_api_openapi.py

Lines changed: 0 additions & 155 deletions
This file was deleted.

0 commit comments

Comments
 (0)