Skip to content

Commit c0ca377

Browse files
author
Davide Antelmo
committed
remove duplicated codes
1 parent 3b08f91 commit c0ca377

3 files changed

Lines changed: 21 additions & 241 deletions

File tree

app/backend/app/agents/azure_chat/_chatkit_events_handler.py

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

app/backend/app/agents/foundry_v2/_chatkit_events_handler.py renamed to app/backend/app/routers/chatkit/_chatkit_events_handler.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,8 @@ async def handle_events(self, thread_id: str, events: AsyncIterable[WorkflowEven
130130
descriptive_title = f"Connected to {event.data.target} "
131131
handoff_result_task = CustomTask(title=descriptive_title, icon="check-circle-filled")
132132
taskResultUpdate = TaskItem(thread_id=thread_id,id=f"tsk_{uuid.uuid4().hex[:8]}", task=handoff_result_task, created_at=datetime.now())
133-
yield ThreadItemAddedEvent(item=taskResultUpdate)
133+
yield ThreadItemAddedEvent(item=taskResultUpdate)
134+
continue
134135

135136
if event.type == "output":
136137
if isinstance(event.data, AgentResponseUpdate) \
@@ -152,6 +153,9 @@ async def handle_events(self, thread_id: str, events: AsyncIterable[WorkflowEven
152153
#force this to be FunctionCallContent
153154
function_call_content = event.data.contents[0] #type: ignore
154155

156+
#skip any framework handoff_to_ function call
157+
if function_call_content.name and function_call_content.name.startswith("handoff_to_"):
158+
continue
155159

156160
if function_call_content.name:
157161
call_id = function_call_content.call_id
@@ -166,9 +170,16 @@ async def handle_events(self, thread_id: str, events: AsyncIterable[WorkflowEven
166170
and event.data.contents \
167171
and isinstance(event.data.contents, list) \
168172
and all(item.type == "function_result" for item in event.data.contents):
173+
169174
function_result_content = event.data.contents[0] #type: ignore
175+
176+
#skip any framework handoff_to_ function call. check if result dict has the key "handoff_to"
177+
if function_result_content.result and isinstance(function_result_content.result, dict) and function_result_content.result.get("handoff_to"):
178+
continue
179+
170180
if function_result_content.call_id :
171-
tool_name = self.tool_name_id_map.get(function_result_content.call_id, function_result_content.call_id)
181+
182+
tool_name = self.tool_name_id_map.get(function_result_content.call_id, function_result_content.name)
172183
descriptive_title = event_description_map[tool_name]["end"] if tool_name in event_description_map else tool_name
173184
function_result_task = CustomTask(title=descriptive_title, icon="check-circle-filled")
174185
taskResultUpdate = TaskItem(thread_id=thread_id,id=function_result_content.call_id, task=function_result_task, created_at=datetime.now())
@@ -182,7 +193,7 @@ async def handle_events(self, thread_id: str, events: AsyncIterable[WorkflowEven
182193
function_approval_content : Content = event.data.contents[0].function_call #type: ignore
183194
tool_name = function_approval_content.name
184195
parsed_args = function_approval_content.arguments
185-
function_approval_content.call_id
196+
call_id = function_approval_content.call_id
186197
# approval_request_widget = build_approval_request(tool_name=tool_name, tool_args=parsed_args, call_id=function_approval_content.call_id, request_id=function_approval_content.id)
187198
# Server Managed Widget Item
188199
# widget_item = WidgetItem(
@@ -200,18 +211,19 @@ async def handle_events(self, thread_id: str, events: AsyncIterable[WorkflowEven
200211
args={
201212
"tool_name": tool_name,
202213
"tool_args": parsed_args,
203-
"call_id": function_approval_content.call_id,
214+
"call_id": call_id,
204215
"request_id": function_approval_content.id
205216
}
206217
)
207218
#CustomThreadItemDoneEvent is a chatkit custom thread item type to support client widget. we disable pylance to avoid linting error
208219
yield CustomThreadItemDoneEvent(type="thread.item.done", item=client_widget_item) #type: ignore
220+
continue
209221

210222

211-
else:
212-
event_description = event_description_map.get(event.type, event.type)
213-
progressUpdate = ProgressUpdateEvent(text=event_description, icon="atom")
214-
yield progressUpdate
223+
# Handle other event types as progress updates with event type as description
224+
event_description = event_description_map.get(event.type, event.type)
225+
progressUpdate = ProgressUpdateEvent(text=event_description, icon="atom")
226+
yield progressUpdate
215227

216228
# Finalize the message
217229
if self.message_started:

app/backend/app/routers/chatkit/chatkit_server.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from chatkit.actions import Action
77
from chatkit.server import ChatKitServer,agents_sdk_user_agent_override
88
from chatkit.errors import CustomStreamError, StreamError, ErrorCode
9+
from app.routers.chatkit._chatkit_events_handler import ChatKitEventsHandler
910
from agent_framework.observability import get_tracer
1011
from app.config.settings import settings
1112

@@ -42,10 +43,8 @@
4243

4344
if settings.AGENTS_TYPE == "azure_chat":
4445
from app.agents.azure_chat.handoff_orchestrator import HandoffOrchestrator
45-
from app.agents.azure_chat._chatkit_events_handler import ChatKitEventsHandler
4646
elif settings.AGENTS_TYPE == "foundry_v2":
4747
from app.agents.foundry_v2.handoff_orchestrator import HandoffOrchestrator
48-
from app.agents.foundry_v2._chatkit_events_handler import ChatKitEventsHandler
4948
else:
5049
raise ValueError(f"Unsupported AGENTS_TYPE: {settings.AGENTS_TYPE}")
5150
from app.common.chatkit.types import ClientWidgetItem, CustomThreadItemDoneEvent

0 commit comments

Comments
 (0)