Skip to content

Commit e95a02a

Browse files
committed
Updates for socket messages from proxy agent
1 parent 1644523 commit e95a02a

File tree

3 files changed

+34
-36
lines changed

3 files changed

+34
-36
lines changed

src/backend/v3/callbacks/response_handlers.py

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,6 @@ def agent_response_callback(message: ChatMessageContent, user_id: str = None) ->
1616

1717
# Get agent name to determine handling
1818
agent_name = message.name or "Unknown Agent"
19-
20-
21-
# Debug information about the message
22-
# message_type = type(message).__name__
23-
# metadata = getattr(message, 'metadata', {})
24-
# # when streaming code - list the coder info first once -
25-
# if 'code' in metadata and metadata['code'] is True:
26-
# if coderagent == False:
27-
# print(f"\n **{agent_name}** [{message_type}]")
28-
# print("-" * (len(agent_name) + len(message_type) + 10))
29-
# coderagent = True
30-
# print(message.content, end='', flush=False)
31-
# return
32-
# elif coderagent == True:
33-
# coderagent = False
3419

3520
role = getattr(message, 'role', 'unknown')
3621

src/backend/v3/magentic_agents/proxy_agent.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from collections.abc import AsyncIterable
77
from typing import AsyncIterator, Optional
88

9+
import v3.models.messages as agent_messages
910
from pydantic import Field
1011
from semantic_kernel.agents import ( # pylint: disable=no-name-in-module
1112
AgentResponseItem, AgentThread)
@@ -104,8 +105,6 @@ def __init__(self, user_id: str = None, **kwargs):
104105
)
105106
self.instructions = ""
106107

107-
self.user_id = user_id or current_user_id.get()
108-
109108
def _create_message_content(self, content: str, thread_id: str = None) -> ChatMessageContent:
110109
"""Create a ChatMessageContent with proper metadata."""
111110
return ChatMessageContent(
@@ -117,18 +116,23 @@ def _create_message_content(self, content: str, thread_id: str = None) -> ChatMe
117116

118117
async def _trigger_response_callbacks(self, message_content: ChatMessageContent):
119118
"""Manually trigger the same response callbacks used by other agents."""
119+
# Get current user_id dynamically instead of using stored value
120+
current_user = current_user_id.get() or self.user_id or ""
121+
120122
# Trigger the standard agent response callback
121-
agent_response_callback(message_content, self.user_id)
123+
agent_response_callback(message_content, current_user)
122124

123125
async def _trigger_streaming_callbacks(self, content: str, is_final: bool = False):
124126
"""Manually trigger streaming callbacks for real-time updates."""
127+
# Get current user_id dynamically instead of using stored value
128+
current_user = current_user_id.get() or self.user_id or ""
125129
streaming_message = StreamingChatMessageContent(
126130
role=AuthorRole.ASSISTANT,
127131
content=content,
128132
name=self.name,
129133
choice_index=0
130134
)
131-
await streaming_agent_response_callback(streaming_message, is_final, self.user_id)
135+
await streaming_agent_response_callback(streaming_message, is_final, current_user)
132136

133137
async def invoke(self, message: str,*, thread: AgentThread | None = None,**kwargs) -> AsyncIterator[ChatMessageContent]:
134138
"""Ask human user for clarification about the message."""
@@ -142,9 +146,9 @@ async def invoke(self, message: str,*, thread: AgentThread | None = None,**kwarg
142146
# Send clarification request via response handlers
143147
clarification_request = f"I need clarification about: {message}"
144148
clarification_message = self._create_message_content(clarification_request, thread.id)
145-
# await self._trigger_response_callbacks(clarification_message)
149+
await self._trigger_response_callbacks(clarification_message)
146150

147-
# Get human input
151+
# Get human input - replace this with awaiting a websocket call or API handler when available
148152
human_response = input("Please provide clarification: ").strip()
149153

150154
if not human_response:
@@ -154,7 +158,6 @@ async def invoke(self, message: str,*, thread: AgentThread | None = None,**kwarg
154158

155159
# Send response via response handlers
156160
response_message = self._create_message_content(response, thread.id)
157-
#await self._trigger_response_callbacks(response_message)
158161

159162
chat_message = response_message
160163

@@ -183,7 +186,8 @@ async def invoke_stream(self, messages, thread=None, **kwargs) -> AsyncIterator[
183186

184187
# Send clarification request via streaming callbacks
185188
clarification_request = f"I need clarification about: {message}"
186-
#await self._trigger_streaming_callbacks(clarification_request)
189+
self._create_message_content(clarification_request, thread.id)
190+
await self._trigger_streaming_callbacks(clarification_request)
187191

188192
# Get human input - replace with websocket call when available
189193
human_response = input("Please provide clarification: ").strip()
@@ -192,9 +196,6 @@ async def invoke_stream(self, messages, thread=None, **kwargs) -> AsyncIterator[
192196
human_response = "No additional clarification provided."
193197

194198
response = f"Human clarification: {human_response}"
195-
196-
# Send response via streaming callbacks
197-
#await self._trigger_streaming_callbacks(response, is_final=True)
198199

199200
chat_message = self._create_message_content(response, thread.id)
200201

src/backend/v3/orchestration/human_approval_manager.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,22 +77,34 @@ async def plan(self, magentic_context: MagenticContext) -> Any:
7777
approval_response = await self._wait_for_user_approval()
7878

7979
if approval_response and approval_response.approved:
80-
print("Plan approved - proceeding with execution...")
80+
print("Plan approved - proceeding with execution...")
8181
return plan
8282
else:
83-
print("❌ Plan execution cancelled by user")
84-
return ChatMessageContent(
85-
role="assistant",
86-
content="Plan execution was cancelled by the user."
87-
)
83+
print("Plan execution cancelled by user")
84+
await connection_config.send_status_update_async({
85+
"type": "plan_approval_response",
86+
"data": approval_response
87+
})
88+
raise Exception("Plan execution cancelled by user")
89+
# return ChatMessageContent(
90+
# role="assistant",
91+
# content="Plan execution was cancelled by the user."
92+
# )
93+
8894

8995
async def _wait_for_user_approval(self) -> Optional[messages.PlanApprovalResponse]:
9096
"""Wait for user approval response."""
9197
user_id = current_user_id.get()
92-
print(f"🔍 DEBUG: user_id from context = {user_id}") # <-- PUT BREAKPOINT HERE
93-
94-
# Return None to cancel plan (for now, just to test context)
95-
return None
98+
# Temporarily use console input for approval - will switch to WebSocket or API in future
99+
response = input("\nApprove this execution plan? [y/n]: ").strip().lower()
100+
if response in ['y', 'yes']:
101+
return messages.PlanApprovalResponse(approved=True)
102+
elif response in ['n', 'no']:
103+
return messages.PlanApprovalResponse(approved=False)
104+
else:
105+
print("Invalid input. Please enter 'y' for yes or 'n' for no.")
106+
return await self._wait_for_user_approval()
107+
96108

97109
async def prepare_final_answer(self, magentic_context: MagenticContext) -> ChatMessageContent:
98110
"""

0 commit comments

Comments
 (0)