Skip to content

Commit c585b8e

Browse files
committed
优化代理的启动钩子,统一打印格式;更新.gitignore以忽略.vscode目录
1 parent c7257c9 commit c585b8e

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,4 @@ cython_debug/
144144
.pypirc
145145
.aider*
146146
*.env
147+
.vscode/

examples/handoffs/message_filter_streaming.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ def spanish_handoff_message_filter(handoff_message_data: HandoffInputData) -> Ha
3636
)
3737

3838

39+
class PrintAgentHooks(AgentHooks):
40+
41+
async def on_start(self, context: RunContextWrapper[TContext], agent: Agent[TContext]) -> None:
42+
print(f'当前: {agent.name} 代理处理的请求已开始。')
43+
return await super().on_start(context, agent)
44+
3945
deepseek = get_agent_chat_model('deepseek-v3')
4046

4147
# 第一个代理:简洁助手
@@ -44,28 +50,20 @@ def spanish_handoff_message_filter(handoff_message_data: HandoffInputData) -> Ha
4450
instructions="保持极度简洁的回答。",
4551
tools=[random_number_tool],
4652
model=deepseek,
53+
hooks=PrintAgentHooks(),
4754
)
4855

49-
class CustomAgentHooks(AgentHooks):
50-
def __init__(self, display_name: str):
51-
self.display_name = display_name
52-
53-
async def on_start(self, context: RunContextWrapper[TContext], agent: Agent[TContext]) -> None:
54-
print(f'当{self.display_name} 代理处理的请求已开始。')
55-
return await super().on_start(context, agent)
56-
57-
5856
# 西班牙语代理
5957
spanish_agent = Agent(
6058
name="西班牙语助手",
6159
instructions="只使用西班牙语交流,并保持极度简洁。",
6260
handoff_description="一个西班牙语助手。",
6361
model=deepseek,
64-
hooks=CustomAgentHooks(display_name="西班牙语助手")
62+
hooks=PrintAgentHooks()
6563
)
6664

6765

68-
def on_spanish_agent_handoff(_ctx: RunContextWrapper[None], data = None) -> None:
66+
def on_spanish_agent_handoff(_ctx: RunContextWrapper[None]) -> None:
6967
print('开始交接到西班牙语助手。')
7068

7169
# 第二个代理:通用助手
@@ -76,6 +74,7 @@ def on_spanish_agent_handoff(_ctx: RunContextWrapper[None], data = None) -> None
7674
),
7775
handoffs=[handoff(spanish_agent, input_filter=spanish_handoff_message_filter, on_handoff=on_spanish_agent_handoff)],
7876
model=deepseek,
77+
hooks=PrintAgentHooks(),
7978
)
8079

8180

0 commit comments

Comments
 (0)