Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion agents/matmaster_agent/callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@
from agents.matmaster_agent.model import UserContent
from agents.matmaster_agent.prompt import get_user_content_lang
from agents.matmaster_agent.services.quota import check_quota_service, use_quota_service
from agents.matmaster_agent.state import ERROR_DETAIL, ERROR_OCCURRED, PLAN, UPLOAD_FILE
from agents.matmaster_agent.state import (
ERROR_DETAIL,
ERROR_OCCURRED,
HISTORY_STEPS,
PLAN,
UPLOAD_FILE,
)
from agents.matmaster_agent.utils.helper_func import get_user_id

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -131,6 +137,10 @@ async def matmaster_prepare_state(
callback_context.state['separate_card_info'] = callback_context.state.get(
'separate_card_info', ''
)
# 历史的所有执行过程
callback_context.state[HISTORY_STEPS] = callback_context.state.get(
HISTORY_STEPS, []
)


async def matmaster_set_lang(
Expand Down
1 change: 1 addition & 0 deletions agents/matmaster_agent/constant.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
MATMASTER_AGENT_NAME = 'matmaster_agent'

ModelRole = 'model'
UserRole = 'user'

Transfer2Agent = 'transfer_to_agent'

Expand Down
12 changes: 6 additions & 6 deletions agents/matmaster_agent/core_agents/base_agents/mcp_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
store_tool_result_in_memory,
)
from agents.matmaster_agent.model import CostFuncType
from agents.matmaster_agent.state import PLAN
from agents.matmaster_agent.state import CURRENT_STEP, CURRENT_STEP_RESULT
from agents.matmaster_agent.style import tool_response_failed_card
from agents.matmaster_agent.utils.event_utils import (
all_text_event,
Expand Down Expand Up @@ -240,8 +240,10 @@ async def _run_events(self, ctx: InvocationContext) -> AsyncGenerator[Event, Non
raise

parsed_tool_result = await parse_result(ctx, dict_result)
logger.info(
f'{ctx.session.id} parsed_tool_result = {parsed_tool_result}'
post_execution_step = copy.deepcopy(ctx.session.state[CURRENT_STEP])
post_execution_step[CURRENT_STEP_RESULT] = parsed_tool_result
yield update_state_event(
ctx, state_delta={CURRENT_STEP: post_execution_step}
)
for _frontend_render_event in frontend_render_event(
ctx,
Expand All @@ -263,9 +265,7 @@ async def _run_events(self, ctx: InvocationContext) -> AsyncGenerator[Event, Non
not event.partial
and event.content.parts[0].text
== 'All Function Calls Are Occurred Before, Continue'
and ctx.session.state[PLAN]['steps'][
ctx.session.state['plan_index']
]['status']
and ctx.session.state[CURRENT_STEP]['status']
== PlanStepStatusEnum.PROCESS
):
for _info_event in all_text_event(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from agents.matmaster_agent.core_agents.comp_agents.dntransfer_climit_agent import (
CombinedDisallowTransferAndContentLimitMixin,
)
from agents.matmaster_agent.logger import PrefixFilter
from agents.matmaster_agent.utils.event_utils import (
context_function_event,
is_function_call,
Expand All @@ -22,6 +23,8 @@
from agents.matmaster_agent.utils.helper_func import extract_json_from_string

logger = logging.getLogger(__name__)
logger.addFilter(PrefixFilter(MATMASTER_AGENT_NAME))
logger.setLevel(logging.INFO)


class SchemaAgent(ErrorHandleLlmAgent):
Expand All @@ -41,7 +44,7 @@ async def _run_events(self, ctx: InvocationContext) -> AsyncGenerator[Event, Non
r',(\s*[}\]])', r'\1', repaired_raw_text
) # 移除尾随逗号
logger.info(
f'[{MATMASTER_AGENT_NAME}]:[{ctx.session.id}] repaired_raw_text = {repaired_raw_text}'
f'{ctx.session.id} {self.name} repaired_raw_text = {repaired_raw_text}'
)
schema_info: dict = json.loads(repaired_raw_text)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,18 @@
)
from agents.matmaster_agent.flow_agents.model import PlanStepStatusEnum
from agents.matmaster_agent.llm_config import MatMasterLlmConfig
from agents.matmaster_agent.locales import i18n
from agents.matmaster_agent.logger import PrefixFilter
from agents.matmaster_agent.model import ToolCallInfoSchema
from agents.matmaster_agent.prompt import (
GLOBAL_INSTRUCTION,
GLOBAL_SCHEMA_INSTRUCTION,
get_vocabulary_enforce_prompt,
)
from agents.matmaster_agent.state import RECOMMEND_PARAMS, STEP_DESCRIPTION
from agents.matmaster_agent.state import (
CURRENT_STEP,
CURRENT_STEP_DESCRIPTION,
RECOMMEND_PARAMS,
)
from agents.matmaster_agent.sub_agents.tools import ALL_TOOLS
from agents.matmaster_agent.utils.event_utils import (
context_function_event,
Expand Down Expand Up @@ -178,9 +181,7 @@ def summary_agent(self) -> DisallowTransferAndContentLimitLlmAgent:
@override
async def _run_events(self, ctx: InvocationContext) -> AsyncGenerator[Event, None]:
# 根据计划来
current_step = ctx.session.state['plan']['steps'][
ctx.session.state['plan_index']
]
current_step = ctx.session.state[CURRENT_STEP]
current_step_tool_name = current_step['tool_name']

# 连接 tool-server,获取doc和函数声明
Expand Down Expand Up @@ -214,7 +215,7 @@ async def _run_events(self, ctx: InvocationContext) -> AsyncGenerator[Event, Non
)

self.tool_call_info_agent.instruction = gen_tool_call_info_instruction(
user_prompt=current_step[STEP_DESCRIPTION],
user_prompt=current_step[CURRENT_STEP_DESCRIPTION],
agent_prompt=self.instruction,
tool_doc=tool_doc,
tool_schema=tool_schema,
Expand Down Expand Up @@ -318,7 +319,7 @@ async def _run_events(self, ctx: InvocationContext) -> AsyncGenerator[Event, Non

step_title = ctx.session.state.get('step_title', {}).get(
'title',
f"{i18n.t(ctx.session.state['separate_card_info'])} {ctx.session.state['plan_index'] + 1}: {current_step_tool_name}",
current_step_tool_name,
)
for matmaster_flow_event in context_function_event(
ctx,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
SubmitRenderAgent,
)
from agents.matmaster_agent.flow_agents.model import PlanStepStatusEnum
from agents.matmaster_agent.locales import i18n
from agents.matmaster_agent.logger import PrefixFilter
from agents.matmaster_agent.state import CURRENT_STEP
from agents.matmaster_agent.utils.event_utils import (
all_text_event,
context_function_event,
Expand Down Expand Up @@ -132,9 +132,7 @@ async def _run_events(self, ctx: InvocationContext) -> AsyncGenerator[Event, Non
async for result_event in self.result_agent.run_async(ctx):
yield result_event

current_step = ctx.session.state['plan']['steps'][
ctx.session.state['plan_index']
]
current_step = ctx.session.state[CURRENT_STEP]
current_step_tool_name = current_step['tool_name']
current_step_status = current_step['status']
if current_step_status in [
Expand All @@ -144,7 +142,7 @@ async def _run_events(self, ctx: InvocationContext) -> AsyncGenerator[Event, Non
# Only Query Job Result
step_title = ctx.session.state.get('step_title', {}).get(
'title',
f"{i18n.t(ctx.session.state['separate_card_info'])} {ctx.session.state['plan_index'] + 1}: {current_step_tool_name}",
current_step_tool_name,
)
for matmaster_flow_event in context_function_event(
ctx,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@
from agents.matmaster_agent.core_agents.public_agents.job_agents.result_core_agent.prompt import (
ResultCoreAgentDescription,
)
from agents.matmaster_agent.flow_agents.step_utils import get_current_step
from agents.matmaster_agent.logger import PrefixFilter
from agents.matmaster_agent.services.job import (
get_job_detail,
parse_and_prepare_err,
parse_and_prepare_results,
)
from agents.matmaster_agent.state import CURRENT_STEP, CURRENT_STEP_STATUS
from agents.matmaster_agent.utils.event_utils import (
all_text_event,
context_function_event,
Expand Down Expand Up @@ -115,14 +117,8 @@ async def _run_events(self, ctx: InvocationContext) -> AsyncGenerator[Event, Non
if status != 'Running':
# 更新状态
plan_status = 'success' if status == 'Finished' else 'failed'
update_plan = copy.deepcopy(ctx.session.state['plan'])
logger.info(
f'{ctx.session.id} plan_index = {ctx.session.state['plan_index']}'
)
update_plan['steps'][ctx.session.state['plan_index']][
'status'
] = plan_status

post_execution_step = copy.deepcopy(get_current_step(ctx))
post_execution_step[CURRENT_STEP_STATUS] = plan_status
update_long_running_jobs = copy.deepcopy(
ctx.session.state['long_running_jobs']
)
Expand All @@ -131,7 +127,7 @@ async def _run_events(self, ctx: InvocationContext) -> AsyncGenerator[Event, Non
ctx,
state_delta={
'long_running_jobs': update_long_running_jobs,
'plan': update_plan,
CURRENT_STEP: post_execution_step,
},
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
DisallowTransferAndContentLimitMCPAgent,
)
from agents.matmaster_agent.flow_agents.model import PlanStepStatusEnum
from agents.matmaster_agent.flow_agents.step_utils import get_current_step
from agents.matmaster_agent.locales import i18n
from agents.matmaster_agent.logger import PrefixFilter
from agents.matmaster_agent.model import BohrJobInfo, DFlowJobInfo
from agents.matmaster_agent.state import PLAN
from agents.matmaster_agent.state import CURRENT_STEP, CURRENT_STEP_STATUS
from agents.matmaster_agent.style import tool_response_failed_card
from agents.matmaster_agent.utils.event_utils import (
all_text_event,
Expand Down Expand Up @@ -99,11 +100,13 @@ async def _run_events(self, ctx: InvocationContext) -> AsyncGenerator[Event, Non
yield tool_response_failed_event

# 更新 plan 为失败
update_plan = copy.deepcopy(ctx.session.state['plan'])
update_plan['steps'][ctx.session.state['plan_index']][
'status'
] = 'failed'
yield update_state_event(ctx, state_delta={'plan': update_plan})
post_execution_step = copy.deepcopy(get_current_step(ctx))
post_execution_step[CURRENT_STEP_STATUS] = (
PlanStepStatusEnum.FAILED
)
yield update_state_event(
ctx, state_delta={CURRENT_STEP: post_execution_step}
)

raise RuntimeError('Tool Execution Failed')
dict_result = load_tool_response(first_part)
Expand Down Expand Up @@ -189,12 +192,14 @@ async def _run_events(self, ctx: InvocationContext) -> AsyncGenerator[Event, Non
yield tool_response_failed_event

# 更新 plan 为失败
update_plan = copy.deepcopy(ctx.session.state['plan'])
update_plan['steps'][ctx.session.state['plan_index']][
'status'
] = 'failed'
post_execution_step = copy.deepcopy(
get_current_step(ctx)
)
post_execution_step[CURRENT_STEP_STATUS] = (
PlanStepStatusEnum.FAILED
)
yield update_state_event(
ctx, state_delta={'plan': update_plan}
ctx, state_delta={CURRENT_STEP: post_execution_step}
)

raise RuntimeError('Tool Execution Failed')
Expand Down Expand Up @@ -263,19 +268,17 @@ async def _run_events(self, ctx: InvocationContext) -> AsyncGenerator[Event, Non
not event.partial
and event.content.parts[0].text
== 'All Function Calls Are Occurred Before, Continue'
and ctx.session.state[PLAN]['steps'][
ctx.session.state['plan_index']
]['status']
and ctx.session.state[CURRENT_STEP]['status']
== PlanStepStatusEnum.PROCESS
):
for _info_event in all_text_event(
ctx, self.name, '工具参数无变化,本次跳过执行', ModelRole
):
yield _info_event
update_plan = copy.deepcopy(ctx.session.state['plan'])
update_plan['steps'][ctx.session.state['plan_index']][
'status'
] = PlanStepStatusEnum.FAILED
yield update_state_event(ctx, state_delta={'plan': update_plan})
post_execution_step = copy.deepcopy(ctx.session.state[CURRENT_STEP])
post_execution_step['status'] = PlanStepStatusEnum.FAILED
yield update_state_event(
ctx, state_delta={CURRENT_STEP: post_execution_step}
)
else:
yield event
Loading
Loading