File size: 3,861 Bytes
afa0cbf | 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 | use codex_code_mode_protocol::CellId;
use codex_code_mode_protocol::RuntimeResponse;
use codex_code_mode_protocol::WaitOutcome;
use codex_code_mode_protocol::WaitRequest;
use codex_code_mode_protocol::host::WireCellId;
use codex_code_mode_protocol::host::WireRuntimeResponse;
use codex_code_mode_protocol::host::WireWaitOutcome;
use codex_code_mode_protocol::host::WireWaitRequest;
use super::RemoteSession;
pub(super) fn public_cell_id(generation: u64, cell_id: &WireCellId) -> CellId {
if generation == 1 {
CellId::new(cell_id.as_str().to_string())
} else {
CellId::new(format!("g{generation}:{}", cell_id.as_str()))
}
}
pub(super) fn public_cell_id_from_protocol(generation: u64, cell_id: &CellId) -> CellId {
public_cell_id(generation, &WireCellId::new(cell_id.as_str()))
}
pub(super) fn remote_cell_id(
session: &RemoteSession,
cell_id: &CellId,
) -> Result<WireCellId, String> {
if session.generation == 1 {
if cell_id.as_str().starts_with('g') && cell_id.as_str().contains(':') {
return Err(format!(
"cell {cell_id} belongs to a stale code-mode host generation"
));
}
return Ok(WireCellId::new(cell_id.as_str()));
}
let prefix = format!("g{}:", session.generation);
let Some(remote_id) = cell_id.as_str().strip_prefix(&prefix) else {
return Err(format!(
"cell {cell_id} belongs to a stale code-mode host generation"
));
};
Ok(WireCellId::new(remote_id))
}
pub(super) fn remote_wait_request(
session: &RemoteSession,
request: WaitRequest,
) -> Result<WireWaitRequest, String> {
Ok(WireWaitRequest {
cell_id: remote_cell_id(session, &request.cell_id)?,
yield_time_ms: request.yield_time_ms,
})
}
pub(super) fn public_runtime_response(
generation: u64,
response: RuntimeResponse,
) -> RuntimeResponse {
match response {
RuntimeResponse::Yielded {
cell_id,
content_items,
code_mode_host_duration,
} => RuntimeResponse::Yielded {
cell_id: public_cell_id_from_protocol(generation, &cell_id),
content_items,
code_mode_host_duration,
},
RuntimeResponse::Terminated {
cell_id,
content_items,
code_mode_host_duration,
} => RuntimeResponse::Terminated {
cell_id: public_cell_id_from_protocol(generation, &cell_id),
content_items,
code_mode_host_duration,
},
RuntimeResponse::Result {
cell_id,
content_items,
error_text,
code_mode_host_duration,
} => RuntimeResponse::Result {
cell_id: public_cell_id_from_protocol(generation, &cell_id),
content_items,
error_text,
code_mode_host_duration,
},
}
}
pub(super) fn public_wait_outcome(generation: u64, outcome: WaitOutcome) -> WaitOutcome {
match outcome {
WaitOutcome::LiveCell(response) => {
WaitOutcome::LiveCell(public_runtime_response(generation, response))
}
WaitOutcome::MissingCell(response) => {
WaitOutcome::MissingCell(public_runtime_response(generation, response))
}
}
}
pub(super) fn runtime_response_cell_id(response: &WireRuntimeResponse) -> &WireCellId {
match response {
WireRuntimeResponse::Yielded { cell_id, .. }
| WireRuntimeResponse::Terminated { cell_id, .. }
| WireRuntimeResponse::Result { cell_id, .. } => cell_id,
}
}
pub(super) fn wait_outcome_cell_id(outcome: &WireWaitOutcome) -> &WireCellId {
match outcome {
WireWaitOutcome::LiveCell(response) | WireWaitOutcome::MissingCell(response) => {
runtime_response_cell_id(response)
}
}
}
|