codex-mobile-bridge 0.1.0

Remote bridge and service manager for codex-mobile.
Documentation
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
use anyhow::Result;
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ApiError {
    pub code: String,
    pub message: String,
}

impl ApiError {
    pub fn new(code: impl Into<String>, message: impl Into<String>) -> Self {
        Self {
            code: code.into(),
            message: message.into(),
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct RuntimeRecord {
    pub runtime_id: String,
    pub display_name: String,
    pub codex_home: Option<String>,
    pub codex_binary: String,
    pub is_primary: bool,
    pub auto_start: bool,
    pub created_at_ms: i64,
    pub updated_at_ms: i64,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct RuntimeStatusSnapshot {
    pub runtime_id: String,
    pub status: String,
    pub codex_home: Option<String>,
    pub user_agent: Option<String>,
    pub platform_family: Option<String>,
    pub platform_os: Option<String>,
    pub last_error: Option<String>,
    pub pid: Option<u32>,
    pub updated_at_ms: i64,
}

impl RuntimeStatusSnapshot {
    pub fn stopped(runtime_id: impl Into<String>) -> Self {
        Self {
            runtime_id: runtime_id.into(),
            status: "stopped".to_string(),
            codex_home: None,
            user_agent: None,
            platform_family: None,
            platform_os: None,
            last_error: None,
            pid: None,
            updated_at_ms: now_millis(),
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct RuntimeSummary {
    pub runtime_id: String,
    pub display_name: String,
    pub codex_home: Option<String>,
    pub codex_binary: String,
    pub is_primary: bool,
    pub auto_start: bool,
    pub created_at_ms: i64,
    pub updated_at_ms: i64,
    pub status: RuntimeStatusSnapshot,
}

impl RuntimeSummary {
    pub fn from_parts(record: &RuntimeRecord, status: RuntimeStatusSnapshot) -> Self {
        Self {
            runtime_id: record.runtime_id.clone(),
            display_name: record.display_name.clone(),
            codex_home: record.codex_home.clone(),
            codex_binary: record.codex_binary.clone(),
            is_primary: record.is_primary,
            auto_start: record.auto_start,
            created_at_ms: record.created_at_ms,
            updated_at_ms: record.updated_at_ms,
            status,
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct WorkspaceRecord {
    pub id: String,
    pub display_name: String,
    pub root_path: String,
    pub trusted: bool,
    pub created_at_ms: i64,
    pub updated_at_ms: i64,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(default)]
pub struct ThreadStatusInfo {
    pub kind: String,
    pub reason: Option<String>,
    pub raw: Value,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(default)]
pub struct ThreadTokenUsage {
    pub input_tokens: Option<i64>,
    pub cached_input_tokens: Option<i64>,
    pub output_tokens: Option<i64>,
    pub reasoning_tokens: Option<i64>,
    pub total_tokens: Option<i64>,
    pub raw: Value,
    pub updated_at_ms: i64,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(default)]
pub struct ThreadSummary {
    pub id: String,
    pub runtime_id: String,
    pub workspace_id: Option<String>,
    pub name: Option<String>,
    pub note: Option<String>,
    pub preview: String,
    pub cwd: String,
    pub status: String,
    pub status_info: ThreadStatusInfo,
    pub token_usage: Option<ThreadTokenUsage>,
    pub model_provider: String,
    pub source: String,
    pub created_at: i64,
    pub updated_at: i64,
    pub is_loaded: bool,
    pub is_active: bool,
    pub archived: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TimelineEntry {
    pub id: String,
    pub runtime_id: String,
    pub thread_id: String,
    pub turn_id: Option<String>,
    pub item_id: Option<String>,
    pub entry_type: String,
    pub title: Option<String>,
    pub text: String,
    pub status: Option<String>,
    #[serde(default)]
    pub metadata: Value,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(default)]
pub struct PendingServerRequestOption {
    pub label: String,
    pub description: Option<String>,
    pub value: Option<Value>,
    pub is_other: bool,
    pub raw: Value,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(default)]
pub struct PendingServerRequestQuestion {
    pub id: String,
    pub header: Option<String>,
    pub question: Option<String>,
    pub required: bool,
    pub options: Vec<PendingServerRequestOption>,
    pub raw: Value,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(default)]
pub struct PendingServerRequestRecord {
    pub request_id: String,
    pub runtime_id: String,
    pub rpc_request_id: Value,
    pub request_type: String,
    pub thread_id: Option<String>,
    pub turn_id: Option<String>,
    pub item_id: Option<String>,
    pub title: Option<String>,
    pub reason: Option<String>,
    pub command: Option<String>,
    pub cwd: Option<String>,
    pub grant_root: Option<String>,
    pub tool_name: Option<String>,
    pub arguments: Option<Value>,
    #[serde(default)]
    pub questions: Vec<PendingServerRequestQuestion>,
    pub proposed_execpolicy_amendment: Option<Value>,
    pub network_approval_context: Option<Value>,
    pub schema: Option<Value>,
    #[serde(default)]
    pub available_decisions: Vec<String>,
    pub raw_payload: Value,
    pub created_at_ms: i64,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PersistedEvent {
    pub seq: i64,
    pub event_type: String,
    pub runtime_id: Option<String>,
    pub thread_id: Option<String>,
    pub payload: Value,
    pub created_at_ms: i64,
}

#[derive(Debug, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum ClientEnvelope {
    Hello {
        device_id: String,
        last_ack_seq: Option<i64>,
    },
    Request {
        request_id: String,
        action: String,
        #[serde(default)]
        payload: Value,
    },
    AckEvents {
        last_seq: i64,
    },
    Ping,
}

#[derive(Debug, Serialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum ServerEnvelope {
    Hello {
        runtime: RuntimeStatusSnapshot,
        runtimes: Vec<RuntimeSummary>,
        workspaces: Vec<WorkspaceRecord>,
        pending_requests: Vec<PendingServerRequestRecord>,
    },
    Response {
        request_id: String,
        success: bool,
        #[serde(skip_serializing_if = "Option::is_none")]
        data: Option<Value>,
        #[serde(skip_serializing_if = "Option::is_none")]
        error: Option<ApiError>,
    },
    Event {
        seq: i64,
        event_type: String,
        #[serde(skip_serializing_if = "Option::is_none")]
        runtime_id: Option<String>,
        #[serde(skip_serializing_if = "Option::is_none")]
        thread_id: Option<String>,
        payload: Value,
    },
    Pong {
        server_time_ms: i64,
    },
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct GetRuntimeStatusRequest {
    pub runtime_id: Option<String>,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct StartRuntimeRequest {
    pub runtime_id: Option<String>,
    pub display_name: Option<String>,
    pub codex_home: Option<String>,
    pub codex_binary: Option<String>,
    pub auto_start: Option<bool>,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct StopRuntimeRequest {
    pub runtime_id: String,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct RestartRuntimeRequest {
    pub runtime_id: String,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PruneRuntimeRequest {
    pub runtime_id: String,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct UpsertWorkspaceRequest {
    pub display_name: String,
    pub root_path: String,
    pub trusted: Option<bool>,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ListThreadsRequest {
    pub workspace_id: Option<String>,
    pub runtime_id: Option<String>,
    pub limit: Option<usize>,
    pub cursor: Option<String>,
    pub archived: Option<bool>,
    pub search_term: Option<String>,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct StartThreadRequest {
    pub workspace_id: String,
    pub runtime_id: Option<String>,
    pub relative_path: Option<String>,
    pub model: Option<String>,
    pub name: Option<String>,
    pub note: Option<String>,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ResumeThreadRequest {
    pub thread_id: String,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ReadThreadRequest {
    pub thread_id: String,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SendTurnRequest {
    pub thread_id: String,
    pub text: String,
    pub relative_path: Option<String>,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct InterruptTurnRequest {
    pub thread_id: String,
    pub turn_id: String,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct UpdateThreadRequest {
    pub thread_id: String,
    pub name: Option<String>,
    pub note: Option<String>,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ArchiveThreadRequest {
    pub thread_id: String,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct UnarchiveThreadRequest {
    pub thread_id: String,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct RespondPendingRequestRequest {
    pub request_id: String,
    pub response: Value,
}

pub fn ok_response(request_id: String, data: Value) -> ServerEnvelope {
    ServerEnvelope::Response {
        request_id,
        success: true,
        data: Some(data),
        error: None,
    }
}

pub fn error_response(request_id: String, error: ApiError) -> ServerEnvelope {
    ServerEnvelope::Response {
        request_id,
        success: false,
        data: None,
        error: Some(error),
    }
}

pub fn event_envelope(event: PersistedEvent) -> ServerEnvelope {
    ServerEnvelope::Event {
        seq: event.seq,
        event_type: event.event_type,
        runtime_id: event.runtime_id,
        thread_id: event.thread_id,
        payload: event.payload,
    }
}

pub fn now_millis() -> i64 {
    let now = std::time::SystemTime::now();
    let since_epoch = now
        .duration_since(std::time::UNIX_EPOCH)
        .unwrap_or_default();
    since_epoch.as_millis() as i64
}

pub fn json_string(value: &Value) -> String {
    match value {
        Value::String(inner) => inner.clone(),
        _ => value.to_string(),
    }
}

pub fn require_payload<T: for<'de> Deserialize<'de>>(payload: Value) -> Result<T> {
    Ok(serde_json::from_value(payload)?)
}

pub fn status_payload(runtime: &RuntimeSummary) -> Value {
    json!({ "runtime": runtime })
}

pub fn runtime_list_payload(runtimes: &[RuntimeSummary]) -> Value {
    json!({ "runtimes": runtimes })
}