@@ -6,6 +6,8 @@ enum MacNodeCodexThreadCatalogContract {
66 static let pluginId = " codex "
77 static let capability = " codex-app-server-threads "
88 static let listCommand = " codex.appServer.threads.list.v1 "
9+ static let turnsCommand = " codex.appServer.thread.turns.list.v1 "
10+ static let commands = [ listCommand , turnsCommand ]
911}
1012
1113enum MacNodeCodexThreadCatalog {
@@ -42,11 +44,11 @@ enum MacNodeCodexThreadCatalog {
4244 case . unsupportedAppServerHomeScope :
4345 " UNAVAILABLE: paired macOS Codex catalog requires appServer.homeScope user "
4446 case . appServerUnavailable :
45- " UNAVAILABLE: Codex app-server thread list failed "
47+ " UNAVAILABLE: Codex app-server request failed "
4648 case . responseTooLarge :
47- " UNAVAILABLE: Codex app-server thread metadata exceeded the size limit "
49+ " UNAVAILABLE: Codex app-server response exceeded the size limit "
4850 case . timedOut :
49- " UNAVAILABLE: Codex app-server thread list timed out "
51+ " UNAVAILABLE: Codex app-server request timed out "
5052 }
5153 }
5254
@@ -65,6 +67,12 @@ enum MacNodeCodexThreadCatalog {
6567 var cwd : String ?
6668 }
6769
70+ private struct TurnParams {
71+ var threadId : String
72+ var cursor : String ?
73+ var limit = 20
74+ }
75+
6876 private struct ConfiguredAppServer {
6977 var transport : String ?
7078 var homeScope : String ?
@@ -171,6 +179,15 @@ enum MacNodeCodexThreadCatalog {
171179 }
172180 }
173181
182+ static func turns ( paramsJSON : String ? ) async throws -> String {
183+ let params = try decodeTurnParams ( paramsJSON )
184+ let root = OpenClawConfigFile . loadDict ( )
185+ guard self . shouldAdvertise ( root : root ) else {
186+ throw CatalogError . catalogDisabled
187+ }
188+ return try await self . turns ( params : params , invocation : resolveInvocation ( root : root ) )
189+ }
190+
174191 static func list (
175192 paramsJSON : String ? ,
176193 loadRoot : ( ) -> [ String : Any ] ) async throws -> String
@@ -186,6 +203,93 @@ enum MacNodeCodexThreadCatalog {
186203 return try await self . list ( params : params , invocation : invocation )
187204 }
188205
206+ static func turns (
207+ paramsJSON : String ? ,
208+ executable : String ,
209+ arguments : [ String ] ? = nil ,
210+ cwd : URL ? = nil ,
211+ clearEnv : [ String ] = [ ] ,
212+ timeoutSeconds : Double = MacNodeCodexThreadCatalog . defaultTimeoutSeconds ,
213+ maxLineBytes : Int = 20 * 1024 * 1024 ) async throws -> String
214+ {
215+ let params = try decodeTurnParams ( paramsJSON )
216+ return try await self . turns (
217+ params : params ,
218+ invocation : ResolvedInvocation (
219+ executable : executable ,
220+ arguments : arguments ?? self . defaultArguments ,
221+ cwd : cwd ,
222+ clearEnv : clearEnv ) ,
223+ timeoutSeconds : timeoutSeconds ,
224+ maxLineBytes : maxLineBytes )
225+ }
226+
227+ private static func turns (
228+ params : TurnParams ,
229+ invocation : ResolvedInvocation ,
230+ timeoutSeconds : Double = MacNodeCodexThreadCatalog . defaultTimeoutSeconds ,
231+ maxLineBytes : Int = 20 * 1024 * 1024 ) async throws -> String
232+ {
233+ let deadline = Date ( ) . addingTimeInterval ( max ( 0.01 , timeoutSeconds ) )
234+ try await self . requireCatalogThread (
235+ params . threadId ,
236+ invocation : invocation ,
237+ deadline : deadline )
238+ var requestParams : [ String : Any ] = [
239+ " threadId " : params . threadId ,
240+ " limit " : params . limit ,
241+ " sortDirection " : " desc " ,
242+ " itemsView " : " full " ,
243+ ]
244+ if let cursor = params . cursor {
245+ requestParams [ " cursor " ] = cursor
246+ }
247+ let session = try CodexAppServerThreadRequestSession (
248+ invocation : invocation ,
249+ method : " thread/turns/list " ,
250+ requestParams : requestParams ,
251+ timeoutSeconds : max ( 0.01 , deadline . timeIntervalSinceNow ) ,
252+ maxLineBytes : maxLineBytes )
253+ let output = try await session . run ( )
254+ guard let payload = String ( data : output . resultData , encoding : . utf8 ) else {
255+ throw CatalogError . appServerUnavailable
256+ }
257+ return payload
258+ }
259+
260+ private static func requireCatalogThread (
261+ _ threadId : String ,
262+ invocation : ResolvedInvocation ,
263+ deadline : Date ) async throws
264+ {
265+ var cursor : String ?
266+ var seenCursors = Set < String > ( )
267+ for _ in 0 ..< 100 {
268+ let remainingTimeout = deadline . timeIntervalSinceNow
269+ guard remainingTimeout > 0 else { throw CatalogError . timedOut }
270+ let payload = try await list (
271+ params : ListParams ( cursor : cursor , limit : 100 ) ,
272+ invocation : invocation ,
273+ timeoutSeconds : remainingTimeout )
274+ guard let response = try JSONSerialization . jsonObject ( with : Data ( payload . utf8 ) ) as? [ String : Any ] ,
275+ let sessions = response [ " sessions " ] as? [ [ String : Any ] ]
276+ else {
277+ throw CatalogError . appServerUnavailable
278+ }
279+ if sessions . contains ( where : { $0 [ " threadId " ] as? String == threadId } ) {
280+ return
281+ }
282+ guard let nextCursor = response [ " nextCursor " ] as? String ,
283+ ! nextCursor . isEmpty ,
284+ ! seenCursors . contains ( nextCursor )
285+ else { break }
286+ seenCursors . insert ( nextCursor )
287+ cursor = nextCursor
288+ }
289+ throw CatalogError . invalidParams (
290+ " Codex session is not a non-archived interactive CLI or VS Code session " )
291+ }
292+
189293 static func shouldAdvertise ( root : [ String : Any ] ? = nil ) -> Bool {
190294 let root = root ?? OpenClawConfigFile . loadDict ( )
191295 guard OpenClawConfigFile . configuredBundledPluginAllowed (
@@ -231,13 +335,14 @@ enum MacNodeCodexThreadCatalog {
231335 maxLineBytes : Int = 5 * 1024 * 1024 ) async throws -> String
232336 {
233337 guard params . searchTerm != nil else {
234- let session = try CodexAppServerThreadListSession (
338+ let session = try CodexAppServerThreadRequestSession (
235339 invocation : invocation ,
236- listParams : self . appServerParams ( params ) ,
340+ method : " thread/list " ,
341+ requestParams : self . appServerParams ( params ) ,
237342 timeoutSeconds : timeoutSeconds ,
238343 maxLineBytes : maxLineBytes )
239344 let output = try await session . run ( )
240- return try self . normalize ( listResultData : output . listResultData )
345+ return try self . normalize ( listResultData : output . resultData )
241346 }
242347
243348 // Native search also inspects transcript-derived previews. Scan a bounded
@@ -258,14 +363,15 @@ enum MacNodeCodexThreadCatalog {
258363 var pageParams = params
259364 pageParams . cursor = cursor
260365 pageParams . limit = remainingLimit
261- let session = try CodexAppServerThreadListSession (
366+ let session = try CodexAppServerThreadRequestSession (
262367 invocation : invocation ,
263- listParams : self . appServerParams ( pageParams ) ,
368+ method : " thread/list " ,
369+ requestParams : self . appServerParams ( pageParams ) ,
264370 timeoutSeconds : remainingTimeout ,
265371 maxLineBytes : maxLineBytes )
266372 let output = try await session . run ( )
267373 let page = try self . normalizedResponse (
268- listResultData : output . listResultData ,
374+ listResultData : output . resultData ,
269375 searchTerm : params . searchTerm )
270376 if pageIndex == 0 {
271377 backwardsCursor = page . backwardsCursor
@@ -878,6 +984,39 @@ extension MacNodeCodexThreadCatalog {
878984 return params
879985 }
880986
987+ private static func decodeTurnParams ( _ paramsJSON : String ? ) throws -> TurnParams {
988+ guard let paramsJSON ,
989+ let data = paramsJSON . data ( using : . utf8 ) ,
990+ let raw = try? JSONSerialization . jsonObject ( with : data ) as? [ String : Any ]
991+ else {
992+ throw CatalogError . invalidParams ( " parameters must be a valid JSON object " )
993+ }
994+ let allowed = Set ( [ " threadId " , " cursor " , " limit " ] )
995+ if let unknown = raw . keys . first ( where : { ! allowed . contains ( $0 ) } ) {
996+ throw CatalogError . invalidParams ( " unknown Codex transcript parameter: \( unknown ) " )
997+ }
998+ guard let threadId = try optionalString (
999+ raw ,
1000+ key : " threadId " ,
1001+ maxLength : maxSessionIdLength )
1002+ else {
1003+ throw CatalogError . invalidParams ( " threadId is required " )
1004+ }
1005+ var params = TurnParams ( threadId : threadId )
1006+ params . cursor = try self . optionalString ( raw , key : " cursor " , maxLength : self . maxCursorLength )
1007+ if let value = raw [ " limit " ] {
1008+ guard let number = value as? NSNumber ,
1009+ CFGetTypeID ( number ) != CFBooleanGetTypeID ( ) ,
1010+ number . doubleValue . rounded ( ) == number . doubleValue ,
1011+ ( 1 ... 50 ) . contains ( number . intValue )
1012+ else {
1013+ throw CatalogError . invalidParams ( " limit must be an integer from 1 to 50 " )
1014+ }
1015+ params . limit = number . intValue
1016+ }
1017+ return params
1018+ }
1019+
8811020 private static func optionalString (
8821021 _ params : [ String : Any ] ,
8831022 key : String ,
@@ -1069,22 +1208,22 @@ extension MacNodeCodexThreadCatalog {
10691208 }
10701209}
10711210
1072- private final class CodexAppServerThreadListSession : @unchecked Sendable {
1211+ private final class CodexAppServerThreadRequestSession : @unchecked Sendable {
10731212 struct Output {
1074- var listResultData : Data
1213+ var resultData : Data
10751214 }
10761215
10771216 private enum Phase {
10781217 case initialize
1079- case list
1218+ case request
10801219 }
10811220
10821221 private let process = Process ( )
10831222 private let stdinPipe = Pipe ( )
10841223 private let stdoutPipe = Pipe ( )
10851224 private let stderrPipe = Pipe ( )
10861225 private let queue = DispatchQueue ( label : " ai.openclaw.codex-thread-catalog " )
1087- private let listRequestData : Data
1226+ private let requestData : Data
10881227 private let timeoutSeconds : Double
10891228 private let maxLineBytes : Int
10901229 private var continuation : CheckedContinuation < Output , Error > ?
@@ -1101,7 +1240,8 @@ private final class CodexAppServerThreadListSession: @unchecked Sendable {
11011240
11021241 init (
11031242 invocation : MacNodeCodexThreadCatalog . ResolvedInvocation ,
1104- listParams : [ String : Any ] ,
1243+ method : String ,
1244+ requestParams : [ String : Any ] ,
11051245 timeoutSeconds : Double ,
11061246 maxLineBytes : Int ) throws
11071247 {
@@ -1119,10 +1259,10 @@ private final class CodexAppServerThreadListSession: @unchecked Sendable {
11191259 self . process . standardError = self . stderrPipe
11201260 self . timeoutSeconds = max ( 0.01 , timeoutSeconds )
11211261 self . maxLineBytes = max ( 1 , maxLineBytes )
1122- self . listRequestData = try Self . jsonData ( [
1262+ self . requestData = try Self . jsonData ( [
11231263 " id " : 2 ,
1124- " method " : " thread/list " ,
1125- " params " : listParams ,
1264+ " method " : method ,
1265+ " params " : requestParams ,
11261266 ] )
11271267 }
11281268
@@ -1237,21 +1377,21 @@ private final class CodexAppServerThreadListSession: @unchecked Sendable {
12371377 self . finish ( . failure ( MacNodeCodexThreadCatalog . CatalogError . appServerUnavailable ) )
12381378 return
12391379 }
1240- self . phase = . list
1380+ self . phase = . request
12411381 do {
12421382 try self . write ( Self . initializedNotificationData ( ) )
1243- try self . write ( self . listRequestData )
1383+ try self . write ( self . requestData )
12441384 } catch {
12451385 self . finish ( . failure ( MacNodeCodexThreadCatalog . CatalogError . appServerUnavailable ) )
12461386 }
1247- case ( . list , 2 ) :
1387+ case ( . request , 2 ) :
12481388 guard let result = message [ " result " ] as? [ String : Any ] ,
12491389 let resultData = try? Self . jsonData ( result )
12501390 else {
12511391 self . finish ( . failure ( MacNodeCodexThreadCatalog . CatalogError . appServerUnavailable ) )
12521392 return
12531393 }
1254- self . finish ( . success ( Output ( listResultData : resultData ) ) )
1394+ self . finish ( . success ( Output ( resultData : resultData ) ) )
12551395 default :
12561396 break
12571397 }
@@ -1289,6 +1429,7 @@ private final class CodexAppServerThreadListSession: @unchecked Sendable {
12891429 " title " : " OpenClaw macOS Node " ,
12901430 " version " : GatewayEnvironment . appVersionString ( ) ?? " unknown " ,
12911431 ] ,
1432+ " capabilities " : [ " experimentalApi " : true ] ,
12921433 ] ,
12931434 ] )
12941435 }
0 commit comments