import requests
import time
API_KEY = "your_api_key"
BASE_URL = "https://api.gumloop.com/api/v1"
HEADERS = {
"Content-Type": "application/json",
"Authorization": f"Bearer {API_KEY}"
}
def trigger_agent(gummie_id: str, message: str, user_id: str, project_id: str = None) -> dict:
"""
Trigger a Gumloop agent and wait for the response.
Args:
gummie_id: The agent's unique ID
message: The message to send to the agent
user_id: Your Gumloop user ID
project_id: Optional team/workspace ID
Returns:
dict with 'response' (agent's text reply) and 'messages' (full conversation)
"""
# Step 1: Start the agent interaction
payload = {
"gummie_id": gummie_id,
"message": message,
"user_id": user_id,
}
if project_id:
payload["project_id"] = project_id
start_response = requests.post(
f"{BASE_URL}/start_agent",
headers=HEADERS,
json=payload
)
start_response.raise_for_status()
interaction_id = start_response.json()["interaction_id"]
print(f"Started agent interaction: {interaction_id}")
# Step 2: Poll for completion
params = {"user_id": user_id}
if project_id:
params["project_id"] = project_id
while True:
status_response = requests.get(
f"{BASE_URL}/agent_status/{interaction_id}",
headers=HEADERS,
params=params
)
status_response.raise_for_status()
result = status_response.json()
state = result["state"]
if state == "COMPLETED":
print(f"Agent response: {result['response']}")
return result
elif state == "FAILED":
error_msg = result.get("error_message", "Unknown error")
raise RuntimeError(f"Agent interaction failed: {error_msg}")
else:
# Still processing -- wait and poll again
time.sleep(2)
# Usage
result = trigger_agent(
gummie_id="abc123DEFghiJKL",
message="Summarize the Q4 sales report",
user_id="your_user_id",
project_id="your_project_id" # optional
)
print(result["response"]) # The agent's final text response
print(result["messages"]) # Full conversation with tool call details