forked from AsyncFuncAI/jules-agent-sdk-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasync_example.py
More file actions
102 lines (77 loc) · 3.33 KB
/
async_example.py
File metadata and controls
102 lines (77 loc) · 3.33 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
"""Async usage example for Jules Agent SDK."""
import os
import asyncio
from jules_agent_sdk import AsyncJulesClient
from jules_agent_sdk.models import SessionState
# Your API key - set via environment variable or replace with your actual API key
# Example: export JULES_API_KEY="your-api-key-here"
API_KEY = os.environ.get("JULES_API_KEY", "your-api-key-here")
async def monitor_session(client, session_id):
"""Monitor a session and print progress updates."""
print(f"\n[{session_id}] Monitoring session...")
while True:
session = await client.sessions.get(session_id)
print(f"[{session_id}] State: {session.state}")
if session.state in [SessionState.COMPLETED, SessionState.FAILED]:
return session
await asyncio.sleep(5)
async def create_and_track_session(client, prompt, source):
"""Create a session and track it to completion."""
print(f"\nCreating session: {prompt[:50]}...")
session = await client.sessions.create(
prompt=prompt, source=source, starting_branch="main"
)
print(f"Session created: {session.id}")
# Wait for completion
final_session = await client.sessions.wait_for_completion(session.id)
# Get activities
activities = await client.activities.list_all(session.id)
print(f"Session {session.id} completed with {len(activities)} activities")
return final_session
async def main():
"""Main async function."""
# Use async context manager for automatic cleanup
async with AsyncJulesClient(api_key=API_KEY) as client:
# List sources
print("=== Fetching Sources ===")
sources = await client.sources.list_all()
print(f"Found {len(sources)} sources")
if not sources:
print("No sources available. Please connect a source first.")
return
source_id = sources[0].name
# Example 1: Single session
print("\n=== Example 1: Single Session ===")
await create_and_track_session(
client=client,
prompt="Add comprehensive error handling to the API endpoints",
source=source_id,
)
# Example 2: Multiple concurrent sessions
print("\n=== Example 2: Concurrent Sessions ===")
tasks = [
create_and_track_session(
client, "Improve database query performance", source_id
),
create_and_track_session(
client, "Add unit tests for authentication module", source_id
),
create_and_track_session(
client, "Update documentation with latest API changes", source_id
),
]
# Run all sessions concurrently
results = await asyncio.gather(*tasks, return_exceptions=True)
print("\n=== Results ===")
for i, result in enumerate(results):
if isinstance(result, Exception):
print(f"Task {i+1} failed: {result}")
else:
print(f"Task {i+1} completed: {result.state}")
# Example 3: List all sessions
print("\n=== Example 3: List All Sessions ===")
all_sessions = await client.sessions.list(page_size=10)
for session in all_sessions["sessions"]:
print(f" {session.id}: {session.state} - {session.title or session.prompt[:50]}")
if __name__ == "__main__":
asyncio.run(main())