Skip to main content

remem/context/render/
entry.rs

1use anyhow::Result;
2
3use super::generate_context_output_for_invocation;
4use crate::context::hook_warning::append_hook_integrity_warning;
5use crate::context::invocation::{
6    direct_context_invocation, resolve_context_invocation, resolve_cursor_context_invocation,
7    ContextCliOptions, ContextInvocation,
8};
9
10pub fn generate_context(
11    cwd: &str,
12    session_id: Option<&str>,
13    use_colors: bool,
14    host_arg: Option<&str>,
15    debug: bool,
16) -> Result<()> {
17    let invocation = direct_context_invocation(cwd, session_id, use_colors, host_arg, debug);
18    generate_context_for_invocation(invocation, false)
19}
20
21pub fn generate_context_from_cli(
22    cwd: Option<String>,
23    session_id: Option<String>,
24    use_colors: bool,
25    host: Option<String>,
26    debug: bool,
27    force: bool,
28    gate_mode: Option<String>,
29) -> Result<()> {
30    let (invocation, stdin_warning) = resolve_context_invocation(ContextCliOptions {
31        cwd,
32        session_id,
33        host,
34        use_colors,
35        debug,
36        force,
37        gate_mode,
38    })?;
39    let mut stdout = generate_context_output_for_invocation(invocation, true)?;
40    append_hook_integrity_warning(&mut stdout, stdin_warning.as_deref());
41    print!("{stdout}");
42    Ok(())
43}
44
45/// Cursor `remem context` entrypoint (GH-823): bounded stdin read, strict
46/// exact `sessionStart` validation, then the shared render pipeline. Any
47/// parse/limit failure returns before context generation with empty stdout
48/// and no side effects (B-009); no CLI/current-cwd fallback exists.
49pub fn generate_cursor_context_from_stdin() -> Result<()> {
50    let bytes = crate::cursor_hook::input::read_bounded_hook_stdin(&mut std::io::stdin().lock())?;
51    generate_cursor_context_from_bytes(&bytes)
52}
53
54pub fn generate_cursor_context_from_bytes(bytes: &[u8]) -> Result<()> {
55    let event = crate::cursor_hook::input::parse_session_start(bytes)?;
56    let invocation = resolve_cursor_context_invocation(&event);
57    generate_context_for_invocation(invocation, true)
58}
59
60pub(super) fn generate_context_for_invocation(
61    invocation: ContextInvocation,
62    use_gate: bool,
63) -> Result<()> {
64    let stdout = generate_context_output_for_invocation(invocation, use_gate)?;
65    print!("{stdout}");
66    Ok(())
67}