-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathjules_service.py
More file actions
67 lines (59 loc) · 2.38 KB
/
Copy pathjules_service.py
File metadata and controls
67 lines (59 loc) · 2.38 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
import os
import sys
import requests
class JulesService:
def __init__(self, api_url="https://api.jules.ai/v1/sessions", api_key=None):
self.api_url = api_url
self.api_key = api_key or os.environ.get("JULES_API_KEY")
def _get_headers(self):
if not self.api_key or not self.api_key.strip():
raise ValueError("JULES_API_KEY is not set or empty")
return {
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json",
}
def create_session(self, prompt: str, branch: str, title: str, owner: str, repo_name: str) -> str:
"""
Creates a new Jules session via the API and returns the session ID.
Mimics .github/scripts/jules_ops.py create_jules_session.
"""
payload = {
"prompt": prompt,
"branch": branch,
"title": title,
"owner": owner,
"repo_name": repo_name,
}
try:
response = requests.post(self.api_url, headers=self._get_headers(), json=payload)
response.raise_for_status()
response_data = response.json()
session_id = response_data.get("id")
if not session_id:
raise RuntimeError("Could not find session ID in API response.")
return session_id
except requests.exceptions.RequestException as e:
error_msg = f"Error creating Jules session: {e}"
if e.response is not None:
error_msg += f"\nResponse: {e.response.text}"
raise RuntimeError(error_msg)
def delete_session(self, session_id: str):
"""
Deletes a Jules session via the API.
Mimics .github/scripts/jules_ops.py delete_jules_session.
"""
if not session_id:
raise ValueError("session_id is required for the 'delete' command.")
url = f"{self.api_url}/{session_id}"
headers = {
"Authorization": f"Bearer {self.api_key}",
}
try:
response = requests.delete(url, headers=headers)
response.raise_for_status()
return True
except requests.exceptions.RequestException as e:
error_msg = f"Error deleting Jules session: {e}"
if e.response is not None:
error_msg += f"\nResponse: {e.response.text}"
raise RuntimeError(error_msg)