1212from codegen .cli .utils .org import resolve_org_id
1313
1414
15-
1615class ClaudeSessionAPIError (Exception ):
1716 """Exception raised for Claude session API errors."""
17+
1818 pass
1919
2020
@@ -25,14 +25,14 @@ def generate_session_id() -> str:
2525
2626def create_claude_session (session_id : str , org_id : Optional [int ] = None ) -> Optional [str ]:
2727 """Create a new Claude Code session in the backend.
28-
28+
2929 Args:
3030 session_id: The session ID to register
3131 org_id: Organization ID (will be resolved if None)
32-
32+
3333 Returns:
3434 Agent run ID if successful, None if failed
35-
35+
3636 Raises:
3737 ClaudeSessionAPIError: If the API call fails
3838 """
@@ -42,24 +42,21 @@ def create_claude_session(session_id: str, org_id: Optional[int] = None) -> Opti
4242 if resolved_org_id is None :
4343 console .print ("⚠️ Could not resolve organization ID for session creation" , style = "yellow" )
4444 return None
45-
45+
4646 # Get authentication token
4747 token = get_current_token ()
4848 if not token :
4949 console .print ("⚠️ No authentication token found for session creation" , style = "yellow" )
5050 return None
51-
51+
5252 # Prepare API request
5353 url = f"{ API_ENDPOINT .rstrip ('/' )} /v1/organizations/{ resolved_org_id } /claude_code/session"
54- headers = {
55- "Authorization" : f"Bearer { token } " ,
56- "Content-Type" : "application/json"
57- }
54+ headers = {"Authorization" : f"Bearer { token } " , "Content-Type" : "application/json" }
5855 payload = {"session_id" : session_id }
59-
56+
6057 # Make API request
6158 response = requests .post (url , json = payload , headers = headers , timeout = 30 )
62-
59+
6360 if response .status_code == 200 :
6461 try :
6562 result = response .json ()
@@ -75,10 +72,10 @@ def create_claude_session(session_id: str, org_id: Optional[int] = None) -> Opti
7572 error_msg = f"{ error_msg } : { error_detail } "
7673 except Exception :
7774 error_msg = f"{ error_msg } : { response .text } "
78-
75+
7976 console .print (f"⚠️ Failed to create Claude session: { error_msg } " , style = "yellow" )
8077 return None
81-
78+
8279 except requests .RequestException as e :
8380 console .print (f"⚠️ Network error creating Claude session: { e } " , style = "yellow" )
8481 return None
@@ -87,44 +84,41 @@ def create_claude_session(session_id: str, org_id: Optional[int] = None) -> Opti
8784 return None
8885
8986
90- def end_claude_session (session_id : str , status : str , org_id : Optional [int ] = None ) -> bool :
91- """End a Claude Code session in the backend.
92-
87+ def update_claude_session_status (session_id : str , status : str , org_id : Optional [int ] = None ) -> bool :
88+ """Update a Claude Code session status in the backend.
89+
9390 Args:
94- session_id: The session ID to end
95- status: Completion status ("COMPLETE" or "ERROR")
91+ session_id: The session ID to update
92+ status: Session status ("COMPLETE", "ERROR", "ACTIVE", etc.)
9693 org_id: Organization ID (will be resolved if None)
97-
94+
9895 Returns:
9996 True if successful, False if failed
10097 """
10198 try :
10299 # Resolve org_id
103100 resolved_org_id = resolve_org_id (org_id )
104101 if resolved_org_id is None :
105- console .print ("⚠️ Could not resolve organization ID for session completion " , style = "yellow" )
102+ console .print ("⚠️ Could not resolve organization ID for session status update " , style = "yellow" )
106103 return False
107-
104+
108105 # Get authentication token
109106 token = get_current_token ()
110107 if not token :
111- console .print ("⚠️ No authentication token found for session completion " , style = "yellow" )
108+ console .print ("⚠️ No authentication token found for session status update " , style = "yellow" )
112109 return False
113-
110+
114111 # Prepare API request
115112 url = f"{ API_ENDPOINT .rstrip ('/' )} /v1/organizations/{ resolved_org_id } /claude_code/session/{ session_id } /status"
116- headers = {
117- "Authorization" : f"Bearer { token } " ,
118- "Content-Type" : "application/json"
119- }
113+ headers = {"Authorization" : f"Bearer { token } " , "Content-Type" : "application/json" }
120114 payload = {"status" : status }
121-
115+
122116 # Make API request
123117 response = requests .post (url , json = payload , headers = headers , timeout = 30 )
124-
118+
125119 if response .status_code == 200 :
126- status_emoji = "✅" if status == "COMPLETE" else "❌"
127- console .print (f"{ status_emoji } Ended Claude session { session_id [:8 ]} ... with status { status } " , style = "green" )
120+ status_emoji = "✅" if status == "COMPLETE" else "🔄" if status == "ACTIVE" else " ❌"
121+ console .print (f"{ status_emoji } Updated Claude session { session_id [:8 ]} ... status to { status } " , style = "green" )
128122 return True
129123 else :
130124 error_msg = f"HTTP { response .status_code } "
@@ -133,26 +127,26 @@ def end_claude_session(session_id: str, status: str, org_id: Optional[int] = Non
133127 error_msg = f"{ error_msg } : { error_detail } "
134128 except Exception :
135129 error_msg = f"{ error_msg } : { response .text } "
136-
137- console .print (f"⚠️ Failed to end Claude session: { error_msg } " , style = "yellow" )
130+
131+ console .print (f"⚠️ Failed to update Claude session status : { error_msg } " , style = "yellow" )
138132 return False
139-
133+
140134 except requests .RequestException as e :
141- console .print (f"⚠️ Network error ending Claude session: { e } " , style = "yellow" )
135+ console .print (f"⚠️ Network error updating Claude session status : { e } " , style = "yellow" )
142136 return False
143137 except Exception as e :
144- console .print (f"⚠️ Unexpected error ending Claude session: { e } " , style = "yellow" )
138+ console .print (f"⚠️ Unexpected error updating Claude session status : { e } " , style = "yellow" )
145139 return False
146140
147141
148142def send_claude_session_log (session_id : str , log_entry : dict , org_id : Optional [int ] = None ) -> bool :
149143 """Send a log entry to the Claude Code session log endpoint.
150-
144+
151145 Args:
152146 session_id: The session ID
153147 log_entry: The log entry to send (dict)
154148 org_id: Organization ID (will be resolved if None)
155-
149+
156150 Returns:
157151 True if successful, False if failed
158152 """
@@ -162,24 +156,21 @@ def send_claude_session_log(session_id: str, log_entry: dict, org_id: Optional[i
162156 if resolved_org_id is None :
163157 console .print ("⚠️ Could not resolve organization ID for log sending" , style = "yellow" )
164158 return False
165-
159+
166160 # Get authentication token
167161 token = get_current_token ()
168162 if not token :
169163 console .print ("⚠️ No authentication token found for log sending" , style = "yellow" )
170164 return False
171-
165+
172166 # Prepare API request
173167 url = f"{ API_ENDPOINT .rstrip ('/' )} /v1/organizations/{ resolved_org_id } /claude_code/session/{ session_id } /log"
174- headers = {
175- "Authorization" : f"Bearer { token } " ,
176- "Content-Type" : "application/json"
177- }
168+ headers = {"Authorization" : f"Bearer { token } " , "Content-Type" : "application/json" }
178169 payload = {"log" : log_entry }
179-
170+
180171 # Make API request
181172 response = requests .post (url , json = payload , headers = headers , timeout = 30 )
182-
173+
183174 if response .status_code == 200 :
184175 return True
185176 else :
@@ -189,10 +180,10 @@ def send_claude_session_log(session_id: str, log_entry: dict, org_id: Optional[i
189180 error_msg = f"{ error_msg } : { error_detail } "
190181 except Exception :
191182 error_msg = f"{ error_msg } : { response .text } "
192-
183+
193184 console .print (f"⚠️ Failed to send log entry: { error_msg } " , style = "yellow" )
194185 return False
195-
186+
196187 except requests .RequestException as e :
197188 console .print (f"⚠️ Network error sending log entry: { e } " , style = "yellow" )
198189 return False
@@ -203,25 +194,21 @@ def send_claude_session_log(session_id: str, log_entry: dict, org_id: Optional[i
203194
204195def write_session_hook_data (session_id : str , org_id : Optional [int ] = None ) -> str :
205196 """Write session data for Claude hook and create session via API.
206-
197+
207198 This function is called by the Claude hook to both write session data locally
208199 and create the session in the backend API.
209-
200+
210201 Args:
211202 session_id: The session ID
212203 org_id: Organization ID
213-
204+
214205 Returns:
215206 JSON string to write to the session file
216207 """
217208 # Create session in backend API
218209 agent_run_id = create_claude_session (session_id , org_id )
219-
210+
220211 # Prepare session data
221- session_data = {
222- "session_id" : session_id ,
223- "agent_run_id" : agent_run_id ,
224- "org_id" : resolve_org_id (org_id )
225- }
226-
227- return json .dumps (session_data , indent = 2 )
212+ session_data = {"session_id" : session_id , "agent_run_id" : agent_run_id , "org_id" : resolve_org_id (org_id )}
213+
214+ return json .dumps (session_data , indent = 2 )
0 commit comments