Skip to content

Commit 38cdc20

Browse files
author
kappa90
committed
refactor(ph-ai): move chat agent specifics to dedicated classes
1 parent 449e27f commit 38cdc20

File tree

87 files changed

+1452
-1131
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

87 files changed

+1452
-1131
lines changed

ee/api/conversation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
)
3030
from posthog.utils import get_instance_region
3131

32-
from ee.hogai.agent.executor import AgentExecutor
3332
from ee.hogai.api.serializers import ConversationSerializer
33+
from ee.hogai.core.executor import AgentExecutor
3434
from ee.hogai.utils.aio import async_to_sync
3535
from ee.hogai.utils.sse import AssistantSSESerializer
3636
from ee.hogai.utils.types.base import AssistantMode

ee/api/max_tools.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from posthog.rate_limit import AIBurstRateThrottle, AISustainedRateThrottle
1616
from posthog.renderers import SafeJSONRenderer
1717

18-
from ee.hogai.utils.types import AssistantMode, AssistantState
18+
from ee.hogai.utils.types import AssistantState
1919
from ee.models.assistant import Conversation
2020

2121

@@ -53,17 +53,16 @@ class MaxToolsViewSet(TeamAndOrgViewSetMixin, GenericViewSet):
5353
required_scopes=["insight:read", "query:read"],
5454
)
5555
def create_and_query_insight(self, request: Request, *args, **kwargs):
56-
from ee.hogai.assistant import Assistant
56+
from ee.hogai.insights_assistant import InsightsAssistant
5757

5858
serializer = InsightsToolCallSerializer(data=request.data)
5959
serializer.is_valid(raise_exception=True)
6060
conversation = self.get_queryset().create(user=request.user, team=self.team, type=Conversation.Type.TOOL_CALL)
61-
assistant = Assistant.create(
61+
assistant = InsightsAssistant(
6262
self.team,
6363
conversation,
6464
user=cast(User, request.user),
6565
is_new_conversation=False, # we don't care about the conversation id being sent back to the client
66-
mode=AssistantMode.INSIGHTS_TOOL,
6766
initial_state=serializer.validated_data["state"],
6867
)
6968

ee/api/test/test_conversation.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def test_create_conversation(self):
102102
conversation_id = str(uuid.uuid4())
103103

104104
with patch(
105-
"ee.hogai.agent.executor.AgentExecutor.astream",
105+
"ee.hogai.core.executor.AgentExecutor.astream",
106106
return_value=_async_generator(),
107107
) as mock_start_workflow_and_stream:
108108
with patch("ee.api.conversation.StreamingHttpResponse", side_effect=self._create_mock_streaming_response):
@@ -129,7 +129,7 @@ def test_create_conversation(self):
129129

130130
def test_add_message_to_existing_conversation(self):
131131
with patch(
132-
"ee.hogai.agent.executor.AgentExecutor.astream",
132+
"ee.hogai.core.executor.AgentExecutor.astream",
133133
return_value=_async_generator(),
134134
) as mock_start_workflow_and_stream:
135135
with patch("ee.api.conversation.StreamingHttpResponse", side_effect=self._create_mock_streaming_response):
@@ -195,7 +195,7 @@ def test_invalid_message_format(self):
195195
def test_rate_limit_burst(self):
196196
# Create multiple requests to trigger burst rate limit
197197
with patch(
198-
"ee.hogai.agent.executor.AgentExecutor.astream",
198+
"ee.hogai.core.executor.AgentExecutor.astream",
199199
return_value=_async_generator(),
200200
):
201201
with patch("ee.api.conversation.StreamingHttpResponse", side_effect=self._create_mock_streaming_response):
@@ -227,7 +227,7 @@ def test_none_content_with_existing_conversation(self):
227227
user=self.user, team=self.team, status=Conversation.Status.IN_PROGRESS
228228
)
229229
with patch(
230-
"ee.hogai.agent.executor.AgentExecutor.astream",
230+
"ee.hogai.core.executor.AgentExecutor.astream",
231231
return_value=_async_generator(),
232232
) as mock_stream_conversation:
233233
with patch("ee.api.conversation.StreamingHttpResponse", side_effect=self._create_mock_streaming_response):
@@ -263,7 +263,7 @@ def test_missing_trace_id(self):
263263

264264
def test_nonexistent_conversation(self):
265265
with patch(
266-
"ee.hogai.agent.executor.AgentExecutor.astream",
266+
"ee.hogai.core.executor.AgentExecutor.astream",
267267
return_value=_async_generator(),
268268
):
269269
with patch("ee.api.conversation.StreamingHttpResponse", side_effect=self._create_mock_streaming_response):
@@ -296,7 +296,7 @@ def test_unauthenticated_request(self):
296296
)
297297
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
298298

299-
@patch("ee.hogai.agent.executor.AgentExecutor.cancel_workflow")
299+
@patch("ee.hogai.core.executor.AgentExecutor.cancel_workflow")
300300
def test_cancel_conversation(self, mock_cancel):
301301
conversation = Conversation.objects.create(
302302
user=self.user,
@@ -340,7 +340,7 @@ def test_cancel_other_teams_conversation(self):
340340
)
341341
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
342342

343-
@patch("ee.hogai.agent.executor.AgentExecutor.cancel_workflow")
343+
@patch("ee.hogai.core.executor.AgentExecutor.cancel_workflow")
344344
def test_cancel_conversation_with_async_cleanup(self, mock_cancel):
345345
"""Test that cancel endpoint properly handles async cleanup."""
346346
conversation = Conversation.objects.create(
@@ -360,7 +360,7 @@ def test_cancel_conversation_with_async_cleanup(self, mock_cancel):
360360

361361
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
362362

363-
@patch("ee.hogai.agent.executor.AgentExecutor.cancel_workflow")
363+
@patch("ee.hogai.core.executor.AgentExecutor.cancel_workflow")
364364
def test_cancel_conversation_async_cleanup_failure(self, mock_cancel):
365365
"""Test cancel endpoint behavior when async cleanup fails."""
366366
conversation = Conversation.objects.create(
@@ -428,7 +428,7 @@ def test_stream_from_in_progress_conversation(self):
428428
user=self.user, team=self.team, status=Conversation.Status.IN_PROGRESS
429429
)
430430
with patch(
431-
"ee.hogai.agent.executor.AgentExecutor.astream",
431+
"ee.hogai.core.executor.AgentExecutor.astream",
432432
return_value=_async_generator(),
433433
) as mock_stream_conversation:
434434
with patch("ee.api.conversation.StreamingHttpResponse", side_effect=self._create_mock_streaming_response):
@@ -606,7 +606,7 @@ def test_billing_context_validation_valid_data(self):
606606
conversation = Conversation.objects.create(user=self.user, team=self.team)
607607

608608
with patch(
609-
"ee.hogai.agent.executor.AgentExecutor.astream",
609+
"ee.hogai.core.executor.AgentExecutor.astream",
610610
return_value=_async_generator(),
611611
) as mock_start_workflow_and_stream:
612612
with patch("ee.api.conversation.StreamingHttpResponse", side_effect=self._create_mock_streaming_response):
@@ -630,7 +630,7 @@ def test_billing_context_validation_invalid_data(self):
630630
conversation = Conversation.objects.create(user=self.user, team=self.team)
631631

632632
with patch(
633-
"ee.hogai.agent.executor.AgentExecutor.astream",
633+
"ee.hogai.core.executor.AgentExecutor.astream",
634634
return_value=_async_generator(),
635635
) as mock_start_workflow_and_stream:
636636
with patch("ee.api.conversation.StreamingHttpResponse", side_effect=self._create_mock_streaming_response):

ee/api/test/test_max_tools.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from mistralai_azure import AssistantMessage
55

6-
from ee.hogai.assistant.insights_assistant import InsightsAssistant
6+
from ee.hogai.insights_assistant import InsightsAssistant
77

88

99
class TestMaxToolsAPI(APIBaseTest):

ee/hogai/api/serializers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
from posthog.api.shared import UserBasicSerializer
99
from posthog.exceptions_capture import capture_exception
1010

11+
from ee.hogai.chat_agent.graph import AssistantGraph
1112
from ee.hogai.graph.deep_research.graph import DeepResearchAssistantGraph
1213
from ee.hogai.graph.deep_research.types import DeepResearchState
13-
from ee.hogai.graph.graph import AssistantGraph
1414
from ee.hogai.utils.helpers import should_output_assistant_message
1515
from ee.hogai.utils.types import AssistantState
1616
from ee.hogai.utils.types.composed import AssistantMaxGraphState

ee/hogai/api/test/test_serializers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from posthog.schema import AssistantMessage, AssistantToolCallMessage, ContextMessage
55

66
from ee.hogai.api.serializers import ConversationSerializer
7-
from ee.hogai.graph.graph import AssistantGraph
7+
from ee.hogai.chat_agent.graph import AssistantGraph
88
from ee.hogai.utils.types import AssistantState
99
from ee.models.assistant import Conversation
1010

ee/hogai/assistant/__init__.py

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

ee/hogai/assistant/assistant.py

Lines changed: 0 additions & 52 deletions
This file was deleted.
File renamed without changes.

ee/hogai/graph/graph.py renamed to ee/hogai/chat_agent/graph.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
from collections.abc import Callable
22

3+
from ee.hogai.chat_agent.loop_graph.graph import ChatAgentLoopGraph
34
from ee.hogai.django_checkpoint.checkpointer import DjangoCheckpointer
4-
from ee.hogai.graph.agent_executor import AgentExecutorGraph
5-
from ee.hogai.graph.title_generator.nodes import TitleGeneratorNode
6-
from ee.hogai.graph.usage import UsageNode
7-
from ee.hogai.utils.types.base import AssistantGraphName, AssistantNodeName, AssistantState
8-
9-
from .memory.nodes import (
5+
from ee.hogai.graph.memory.nodes import (
106
MemoryCollectorNode,
117
MemoryCollectorToolsNode,
128
MemoryInitializerInterruptNode,
@@ -16,9 +12,12 @@
1612
MemoryOnboardingFinalizeNode,
1713
MemoryOnboardingNode,
1814
)
15+
from ee.hogai.graph.title_generator.nodes import TitleGeneratorNode
16+
from ee.hogai.graph.usage.nodes import UsageNode
17+
from ee.hogai.utils.types.base import AssistantGraphName, AssistantNodeName, AssistantState
1918

2019

21-
class AssistantGraph(AgentExecutorGraph):
20+
class AssistantGraph(ChatAgentLoopGraph):
2221
@property
2322
def graph_name(self) -> AssistantGraphName:
2423
return AssistantGraphName.ASSISTANT

0 commit comments

Comments
 (0)