Skip to content

Commit 1ddc78e

Browse files
authored
Merge pull request #725 from AnguseZhang/feat/intent_ui
feat: 添加用户意图识别时的UI通知事件
2 parents 221e995 + fb24161 commit 1ddc78e

2 files changed

Lines changed: 135 additions & 0 deletions

File tree

agents/matmaster_agent/flow_agents/agent.py

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
MATMASTER_FLOW,
4343
MATMASTER_FLOW_PLANS,
4444
MATMASTER_GENERATE_NPS,
45+
MATMASTER_INTENT_UI,
46+
MATMASTER_THINKING_UI,
4547
)
4648
from agents.matmaster_agent.flow_agents.execution_agent.agent import (
4749
MatMasterSupervisorAgent,
@@ -368,9 +370,42 @@ async def _run_expand_agent(
368370
context + EXPAND_INSTRUCTION + EXPAND_INPUT_EXAMPLES_PROMPT
369371
)
370372
# 3. 运行 Agent
373+
for _intent_ui_event in context_function_event(
374+
ctx,
375+
self.name,
376+
MATMASTER_INTENT_UI,
377+
None,
378+
ModelRole,
379+
{
380+
'matmaster_intent_ui_args': json.dumps(
381+
{
382+
'title': '正在进行用户问题扩写...',
383+
'status': 'start',
384+
}
385+
)
386+
},
387+
):
388+
yield _intent_ui_event
389+
371390
async for expand_event in self.expand_agent.run_async(ctx):
372391
yield expand_event
373392

393+
for _intent_ui_event in context_function_event(
394+
ctx,
395+
self.name,
396+
MATMASTER_INTENT_UI,
397+
None,
398+
ModelRole,
399+
{
400+
'matmaster_intent_ui_args': json.dumps(
401+
{
402+
'status': 'end',
403+
}
404+
)
405+
},
406+
):
407+
yield _intent_ui_event
408+
374409
async def _build_icl_prompt(self, ctx: InvocationContext):
375410
raw_content = ctx.session.state['expand']['update_user_content']
376411
UPDATE_USER_CONTENT = '\nUSER INPUT FOR THIS TASK:\n' + sanitize_braces(
@@ -397,9 +432,42 @@ async def _run_scene_agent(
397432
SCENE_INSTRUCTION + UPDATE_USER_CONTENT + SCENE_EXAMPLES_PROMPT
398433
)
399434
# 3. 运行 Agent
435+
for _intent_ui_event in context_function_event(
436+
ctx,
437+
self.name,
438+
MATMASTER_INTENT_UI,
439+
None,
440+
ModelRole,
441+
{
442+
'matmaster_intent_ui_args': json.dumps(
443+
{
444+
'title': '正在进行场景划分...',
445+
'status': 'start',
446+
}
447+
)
448+
},
449+
):
450+
yield _intent_ui_event
451+
400452
async for scene_event in self.scene_agent.run_async(ctx):
401453
yield scene_event
402454

455+
for _intent_ui_event in context_function_event(
456+
ctx,
457+
self.name,
458+
MATMASTER_INTENT_UI,
459+
None,
460+
ModelRole,
461+
{
462+
'matmaster_intent_ui_args': json.dumps(
463+
{
464+
'status': 'end',
465+
}
466+
)
467+
},
468+
):
469+
yield _intent_ui_event
470+
403471
# 4. 将之前的场景带到后面的会话中去
404472
before_scenes = ctx.session.state['scenes']
405473
single_scene = ctx.session.state['single_scenes']['type']
@@ -481,6 +549,23 @@ async def _run_plan_make_agent(
481549
thinking_text = ''
482550
if not skip_thinking:
483551
try:
552+
for _thinking_ui_event in context_function_event(
553+
ctx,
554+
self.name,
555+
MATMASTER_THINKING_UI,
556+
None,
557+
ModelRole,
558+
{
559+
'matmaster_thinking_ui_args': json.dumps(
560+
{
561+
'title': '正在思考',
562+
'status': 'start',
563+
}
564+
)
565+
},
566+
):
567+
yield _thinking_ui_event
568+
484569
self._thinking_agent.set_thinking_params(
485570
available_tools_with_info_str,
486571
session_file_summary,
@@ -513,6 +598,21 @@ async def _run_plan_make_agent(
513598
f'{ctx.session.id} reasoning_agent result length={len(thinking_text)}, '
514599
f'preview={repr(thinking_text[:300]) if thinking_text else "empty"}'
515600
)
601+
for _thinking_ui_event in context_function_event(
602+
ctx,
603+
self.name,
604+
MATMASTER_THINKING_UI,
605+
None,
606+
ModelRole,
607+
{
608+
'matmaster_thinking_ui_args': json.dumps(
609+
{
610+
'status': 'end',
611+
}
612+
)
613+
},
614+
):
615+
yield _thinking_ui_event
516616
except Exception as e:
517617
logger.warning(
518618
f'{ctx.session.id} reasoning_agent failed: {e}, proceed without thinking'
@@ -961,9 +1061,42 @@ async def _run_async_impl(
9611061

9621062
# 用户意图识别(一旦进入 research 模式,暂时无法退出)
9631063
if ctx.session.state['intent'].get('type', None) != IntentEnum.RESEARCH:
1064+
for _intent_ui_event in context_function_event(
1065+
ctx,
1066+
self.name,
1067+
MATMASTER_INTENT_UI,
1068+
None,
1069+
ModelRole,
1070+
{
1071+
'matmaster_intent_ui_args': json.dumps(
1072+
{
1073+
'title': '正在进行意图识别...',
1074+
'status': 'start',
1075+
}
1076+
)
1077+
},
1078+
):
1079+
yield _intent_ui_event
1080+
9641081
async for intent_event in self.intent_agent.run_async(ctx):
9651082
yield intent_event
9661083

1084+
for _intent_ui_event in context_function_event(
1085+
ctx,
1086+
self.name,
1087+
MATMASTER_INTENT_UI,
1088+
None,
1089+
ModelRole,
1090+
{
1091+
'matmaster_intent_ui_args': json.dumps(
1092+
{
1093+
'status': 'end',
1094+
}
1095+
)
1096+
},
1097+
):
1098+
yield _intent_ui_event
1099+
9671100
# 如果用户上传文件,强制为 research 模式
9681101
if (
9691102
ctx.session.state[UPLOAD_FILE]

agents/matmaster_agent/flow_agents/constant.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
MATMASTER_FLOW = 'matmaster_flow'
1010
MATMASTER_FLOW_PLANS = 'matmaster_flow_plans'
1111
MATMASTER_GENERATE_NPS = 'matmaster_generate_nps'
12+
MATMASTER_INTENT_UI = 'matmaster_intent_ui'
13+
MATMASTER_THINKING_UI = 'matmaster_thinking_ui'
1214

1315
# matmaster_flow 展示文案,直接传给前端(正常执行无标签则传空字符串)
1416
EXECUTION_TYPE_LABEL_RETRY = '重试工具'

0 commit comments

Comments
 (0)