Skip to content

Commit 5f0f3d1

Browse files
committed
minor slackbot improvements
1 parent 9422f43 commit 5f0f3d1

3 files changed

Lines changed: 46 additions & 10 deletions

File tree

examples/slackbot/src/slackbot/api.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import asyncio
22
import re
33
import time
4+
from collections import defaultdict
45
from contextlib import asynccontextmanager
56
from typing import Any
67

@@ -31,7 +32,7 @@
3132
post_slack_message,
3233
)
3334
from slackbot.strings import count_tokens, slice_tokens
34-
from slackbot.wrap import WatchToolCalls, _progress_message
35+
from slackbot.wrap import WatchToolCalls, _progress_message, _tool_usage_counts
3536

3637
BOT_MENTION = r"<@(\w+)>"
3738

@@ -62,6 +63,8 @@ async def run_agent(
6263

6364
try:
6465
token = _progress_message.set(progress)
66+
# Initialize tool usage counts for this agent run
67+
counts_token = _tool_usage_counts.set(defaultdict(int))
6568

6669
try:
6770
with WatchToolCalls(settings=decorator_settings):
@@ -72,6 +75,7 @@ async def run_agent(
7275
)
7376
finally:
7477
_progress_message.reset(token)
78+
_tool_usage_counts.reset(counts_token)
7579

7680
await progress.update(
7781
f"✅ thought for {time.monotonic() - start_time:.1f} seconds"

examples/slackbot/src/slackbot/research_agent.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,11 @@ def create_research_agent(
5454
2. Use multiple search queries with different keywords - don't stop at first result
5555
3. Use explore_module_offerings to understand what's available in relevant modules (i.e. valid imports, types and functions available)
5656
4. Use display_callable_signature to get detailed signatures of functions, classes, and methods when needed
57-
5. Focus on Prefect 3.x documentation unless explicitly asked about 2.x or older versions
57+
5. **IMPORTANT**: ONLY use search_prefect_3x_docs unless the user explicitly mentions "2.x", "Prefect 2", or version compatibility
5858
6. Review gotchas and release notes for recent changes
5959
6060
CRITICAL VERSION-SPECIFIC RULES:
61+
- **DEFAULT TO PREFECT 3.x**: Do NOT use search_prefect_2x_docs unless user explicitly mentions "2.x", "Prefect 2", or asks about version differences
6162
- **NEVER** suggest `Deployment.build_from_flow()` for Prefect 3.x - it's COMPLETELY REMOVED
6263
- **NEVER** suggest `prefect deployment build` CLI command for 3.x - use `prefect deploy` instead
6364
- The correct deployment pattern in 3.x is: `flow.from_source(...).deploy(...)`

examples/slackbot/src/slackbot/wrap.py

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import inspect
2+
from collections import defaultdict
23
from contextlib import ContextDecorator
34
from contextvars import ContextVar
45
from functools import wraps
@@ -11,6 +12,10 @@
1112
T = TypeVar("T")
1213

1314
_progress_message: ContextVar[Any] = ContextVar("progress_message", default=None)
15+
_tool_usage_counts: ContextVar[dict[str, int] | None] = ContextVar(
16+
"tool_usage_counts", default=None
17+
)
18+
_current_tool: ContextVar[str | None] = ContextVar("current_tool", default=None)
1419

1520

1621
class DecorateMethodContext(ContextDecorator):
@@ -77,18 +82,44 @@ async def wrapper(*args, **kwargs) -> T:
7782
):
7883
tool_name = args[0].function.__name__
7984

85+
# Update tool usage counts
86+
counts = _tool_usage_counts.get()
87+
if counts is None:
88+
counts = defaultdict(int)
89+
_tool_usage_counts.set(counts)
90+
counts[tool_name] += 1
91+
92+
# Set current tool
93+
_current_tool_token = _current_tool.set(tool_name)
94+
8095
try:
81-
await _progress.append(f"🔧 {tool_name}")
96+
# Build the progress message with better formatting
97+
lines = []
98+
lines.append(f"🔧 Using: `{tool_name}`")
99+
lines.append("") # Empty line for spacing
100+
101+
# Build summary of all tools used
102+
if counts:
103+
lines.append("📊 Tools used:")
104+
for tool, count in sorted(counts.items()):
105+
lines.append(f" • `{tool}` ({count}x)")
106+
107+
full_message = "\n".join(lines)
108+
await _progress.update(full_message)
82109
except Exception:
83110
pass
84111

85-
wrapped_callable = decorator(**settings or {})(func)
86-
with prefect_tags(*tags):
87-
result = wrapped_callable(*args, **kwargs) # type: ignore
88-
if inspect.isawaitable(result):
89-
result = await result
90-
91-
return result
112+
try:
113+
wrapped_callable = decorator(**settings or {})(func)
114+
with prefect_tags(*tags):
115+
result = wrapped_callable(*args, **kwargs) # type: ignore
116+
if inspect.isawaitable(result):
117+
result = await result
118+
119+
return result
120+
finally:
121+
if _progress:
122+
_current_tool.reset(_current_tool_token)
92123

93124
return wrapper # type: ignore
94125

0 commit comments

Comments
 (0)