-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathacp_example_usage.py
More file actions
264 lines (203 loc) · 7.44 KB
/
acp_example_usage.py
File metadata and controls
264 lines (203 loc) · 7.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
"""
Example usage of the AuggieACPClient.
This demonstrates all the key features:
1. Starting/stopping the agent
2. Sending messages and getting responses
3. Listening to events via a custom listener
4. Clearing context
"""
from auggie_sdk.acp import AuggieACPClient, AgentEventListener
from typing import Optional, Any
class MyEventListener(AgentEventListener):
"""Example event listener that prints all agent events."""
def on_agent_message_chunk(self, text: str) -> None:
"""Called when the agent sends a message chunk."""
print(f"[AGENT MESSAGE] {text}", end="", flush=True)
def on_tool_call(
self,
tool_call_id: str,
title: str,
kind: Optional[str] = None,
status: Optional[str] = None,
) -> None:
"""Called when the agent starts a tool call."""
print(f"\n[TOOL CALL START] {title}")
print(f" ID: {tool_call_id}")
print(f" Kind: {kind}")
print(f" Status: {status}")
def on_tool_response(
self,
tool_call_id: str,
status: Optional[str] = None,
content: Optional[Any] = None,
) -> None:
"""Called when a tool response is received."""
print(f"[TOOL RESPONSE] {tool_call_id}")
print(f" Status: {status}")
if content:
print(f" Content: {str(content)[:100]}...") # Truncate long content
def on_agent_thought(self, text: str) -> None:
"""Called when the agent shares its internal reasoning."""
print(f"[AGENT THOUGHT] {text}", end="", flush=True)
def example_basic_usage():
"""Example 1: Basic usage without event listener."""
print("=" * 80)
print("EXAMPLE 1: Basic Usage")
print("=" * 80)
# Create client without listener
# You can optionally specify model and workspace_root:
# client = AuggieACPClient(
# model="sonnet4.5", # Model string
# workspace_root="/path/to/workspace"
# )
client = AuggieACPClient()
# Start the agent
print("Starting agent...")
client.start()
print(f"Agent started! Session ID: {client.session_id}\n")
# Send a simple message
message = "What is 2 + 2? Answer in one sentence."
print(f"Sending: {message}")
response = client.send_message(message)
print(f"Response: {response}\n")
# Stop the agent
print("Stopping agent...")
client.stop()
print("Agent stopped.\n")
def example_with_listener():
"""Example 2: Using an event listener to see what the agent is doing."""
print("=" * 80)
print("EXAMPLE 2: With Event Listener")
print("=" * 80)
# Create client with listener
listener = MyEventListener()
client = AuggieACPClient(listener=listener)
print("Starting agent...")
client.start()
print(f"Agent started! Session ID: {client.session_id}\n")
# Send a message that will trigger tool calls
message = "Please read the README.md file in the current directory and summarize it in one sentence."
print(f"Sending: {message}\n")
response = client.send_message(message, timeout=30.0)
print(f"\n\nFinal Response: {response}\n")
client.stop()
print("Agent stopped.\n")
def example_context_manager():
"""Example 3: Using context manager for automatic cleanup."""
print("=" * 80)
print("EXAMPLE 3: Context Manager")
print("=" * 80)
listener = MyEventListener()
# Use context manager - automatically starts and stops
with AuggieACPClient(listener=listener) as client:
print(f"Agent started! Session ID: {client.session_id}\n")
message = "What is 10 * 5?"
print(f"Sending: {message}\n")
response = client.send_message(message)
print(f"\n\nFinal Response: {response}\n")
print("Agent automatically stopped.\n")
def example_clear_context():
"""Example 4: Clearing context between conversations."""
print("=" * 80)
print("EXAMPLE 4: Clearing Context")
print("=" * 80)
client = AuggieACPClient()
client.start()
# First conversation
print("First conversation:")
print(f"Session ID: {client.session_id}")
response1 = client.send_message("Remember this number: 42")
print(f"Response: {response1}\n")
# Clear context (restarts agent with new session)
print("Clearing context...")
old_session_id = client.session_id
client.clear_context()
print(
f"Context cleared! Old session: {old_session_id}, New session: {client.session_id}\n"
)
# Second conversation - agent won't remember the number
print("Second conversation (after clearing context):")
response2 = client.send_message("What number did I ask you to remember?")
print(f"Response: {response2}\n")
client.stop()
print("Agent stopped.\n")
def example_multiple_messages():
"""Example 5: Multiple messages in the same session."""
print("=" * 80)
print("EXAMPLE 5: Multiple Messages in Same Session")
print("=" * 80)
client = AuggieACPClient()
client.start()
print(f"Session ID: {client.session_id}\n")
# Send multiple messages in the same session
messages = [
"What is 5 + 3?",
"What is that number multiplied by 2?",
"What is the final result?",
]
for i, message in enumerate(messages, 1):
print(f"Message {i}: {message}")
response = client.send_message(message)
print(f"Response {i}: {response}\n")
client.stop()
print("Agent stopped.\n")
def example_model_and_workspace():
"""Example 6: Using model and workspace configuration."""
print("=" * 80)
print("EXAMPLE 6: Model and Workspace Configuration")
print("=" * 80)
import os
# Create client with specific model and workspace
# Use model string directly:
client = AuggieACPClient(
model="sonnet4.5", # Model string
workspace_root=os.getcwd(), # Specify workspace root
)
# Or use a string directly:
# client = AuggieACPClient(
# model="sonnet4.5", # String also works
# workspace_root=os.getcwd(),
# )
print(f"Starting agent with model: {client.model}")
print(f"Workspace root: {client.workspace_root}\n")
client.start()
print(f"Agent started! Session ID: {client.session_id}\n")
# Send a message
message = "What is 5 * 7? Answer with just the number."
print(f"Sending: {message}")
response = client.send_message(message)
print(f"Response: {response}\n")
# Stop the agent
client.stop()
print("Agent stopped.\n")
def main():
"""Run all examples."""
print("\n" + "=" * 80)
print("AUGGIE ACP CLIENT - EXAMPLES")
print("=" * 80 + "\n")
# Run examples
example_basic_usage()
input("Press Enter to continue to next example...")
example_with_listener()
input("Press Enter to continue to next example...")
example_context_manager()
input("Press Enter to continue to next example...")
example_clear_context()
input("Press Enter to continue to next example...")
example_multiple_messages()
input("Press Enter to continue to next example...")
example_model_and_workspace()
print("\n" + "=" * 80)
print("ALL EXAMPLES COMPLETED")
print("=" * 80 + "\n")
if __name__ == "__main__":
# Run just one example for quick testing
# Uncomment the one you want to run:
# example_basic_usage()
# example_with_listener()
# example_context_manager()
# example_clear_context()
# example_multiple_messages()
example_model_and_workspace()
# Or run all examples:
# main()