@@ -33,7 +33,10 @@ import {
3333import { useSettingsStore } from "@features/settings/stores/settingsStore" ;
3434import { taskViewedApi } from "@features/sidebar/hooks/useTaskViewed" ;
3535import { isNotification , POSTHOG_NOTIFICATIONS } from "@posthog/agent" ;
36- import { getAvailableModes } from "@posthog/agent/execution-mode" ;
36+ import {
37+ getAvailableCodexModes ,
38+ getAvailableModes ,
39+ } from "@posthog/agent/execution-mode" ;
3740import { DEFAULT_GATEWAY_MODEL } from "@posthog/agent/gateway-models" ;
3841import { getIsOnline } from "@renderer/stores/connectivityStore" ;
3942import { trpcClient } from "@renderer/trpc/client" ;
@@ -89,15 +92,23 @@ const LOCAL_SESSION_RECOVERY_FAILED_MESSAGE =
8992 * is available in the UI even without a local agent connection.
9093 */
9194function buildCloudDefaultConfigOptions (
92- initialMode = "plan" ,
95+ initialMode : string | undefined ,
96+ adapter : Adapter = "claude" ,
9397) : SessionConfigOption [ ] {
94- const modes = getAvailableModes ( ) ;
98+ const modes =
99+ adapter === "codex" ? getAvailableCodexModes ( ) : getAvailableModes ( ) ;
100+ const currentMode =
101+ typeof initialMode === "string"
102+ ? initialMode
103+ : adapter === "codex"
104+ ? "auto"
105+ : "plan" ;
95106 return [
96107 {
97108 id : "mode" ,
98109 name : "Approval Preset" ,
99110 type : "select" ,
100- currentValue : initialMode ,
111+ currentValue : currentMode ,
101112 options : modes . map ( ( mode ) => ( {
102113 value : mode . id ,
103114 name : mode . name ,
@@ -399,7 +410,6 @@ export class SessionService {
399410 . getState ( )
400411 . getAdapter ( taskRunId ) ;
401412 const resolvedAdapter = adapter ?? storedAdapter ;
402-
403413 const persistedConfigOptions = getPersistedConfigOptions ( taskRunId ) ;
404414
405415 const session = this . createBaseSession ( taskRunId , taskId , taskTitle ) ;
@@ -1684,7 +1694,20 @@ export class SessionService {
16841694 // in run state (pending_user_message), NOT via user_message command.
16851695
16861696 // Start the watcher immediately so we don't miss status updates.
1687- this . watchCloudTask ( session . taskId , newRun . id , auth . apiHost , auth . teamId ) ;
1697+ const initialMode =
1698+ typeof newRun . state ?. initial_permission_mode === "string"
1699+ ? newRun . state . initial_permission_mode
1700+ : undefined ;
1701+ this . watchCloudTask (
1702+ session . taskId ,
1703+ newRun . id ,
1704+ auth . apiHost ,
1705+ auth . teamId ,
1706+ undefined ,
1707+ newRun . log_url ,
1708+ initialMode ,
1709+ newRun . runtime_adapter ?? session . adapter ?? "claude" ,
1710+ ) ;
16881711
16891712 // Invalidate task queries so the UI picks up the new run metadata
16901713 queryClient . invalidateQueries ( { queryKey : [ "tasks" ] } ) ;
@@ -2211,6 +2234,7 @@ export class SessionService {
22112234 onStatusChange ?: ( ) => void ,
22122235 logUrl ?: string ,
22132236 initialMode ?: string ,
2237+ adapter : Adapter = "claude" ,
22142238 ) : ( ) => void {
22152239 const taskRunId = runId ;
22162240 const startToken = ++ this . nextCloudTaskWatchToken ;
@@ -2226,10 +2250,21 @@ export class SessionService {
22262250 existingWatcher . onStatusChange = onStatusChange ;
22272251 // Ensure configOptions is populated on revisit
22282252 const existing = sessionStoreSetters . getSessionByTaskId ( taskId ) ;
2229- if ( existing && ! existing . configOptions ?. length ) {
2230- sessionStoreSetters . updateSession ( existing . taskRunId , {
2231- configOptions : buildCloudDefaultConfigOptions ( initialMode ) ,
2232- } ) ;
2253+ if ( existing ) {
2254+ const existingMode = getConfigOptionByCategory (
2255+ existing . configOptions ,
2256+ "mode" ,
2257+ ) ?. currentValue ;
2258+ const currentMode =
2259+ typeof existingMode === "string" ? existingMode : initialMode ;
2260+ const shouldRefreshConfigOptions =
2261+ ! existing . configOptions ?. length || existing . adapter !== adapter ;
2262+ if ( shouldRefreshConfigOptions ) {
2263+ sessionStoreSetters . updateSession ( existing . taskRunId , {
2264+ adapter,
2265+ configOptions : buildCloudDefaultConfigOptions ( currentMode , adapter ) ,
2266+ } ) ;
2267+ }
22332268 }
22342269 return ( ) => { } ;
22352270 }
@@ -2263,14 +2298,28 @@ export class SessionService {
22632298 const session = this . createBaseSession ( taskRunId , taskId , taskTitle ) ;
22642299 session . status = "disconnected" ;
22652300 session . isCloud = true ;
2266- session . configOptions = buildCloudDefaultConfigOptions ( initialMode ) ;
2301+ session . adapter = adapter ;
2302+ session . configOptions = buildCloudDefaultConfigOptions (
2303+ initialMode ,
2304+ adapter ,
2305+ ) ;
22672306 sessionStoreSetters . setSession ( session ) ;
22682307 } else {
22692308 // Ensure cloud flag and configOptions are set on existing sessions
22702309 const updates : Partial < AgentSession > = { } ;
22712310 if ( ! existing . isCloud ) updates . isCloud = true ;
2272- if ( ! existing . configOptions ?. length ) {
2273- updates . configOptions = buildCloudDefaultConfigOptions ( initialMode ) ;
2311+ if ( existing . adapter !== adapter ) updates . adapter = adapter ;
2312+ if ( ! existing . configOptions ?. length || existing . adapter !== adapter ) {
2313+ const existingMode = getConfigOptionByCategory (
2314+ existing . configOptions ,
2315+ "mode" ,
2316+ ) ?. currentValue ;
2317+ const currentMode =
2318+ typeof existingMode === "string" ? existingMode : initialMode ;
2319+ updates . configOptions = buildCloudDefaultConfigOptions (
2320+ currentMode ,
2321+ adapter ,
2322+ ) ;
22742323 }
22752324 if ( Object . keys ( updates ) . length > 0 ) {
22762325 sessionStoreSetters . updateSession ( existing . taskRunId , updates ) ;
0 commit comments