Skip to main content

systemprompt_cli/session/
context.rs

1//! `CliSessionContext` binding a CLI invocation to a persisted session row.
2//!
3//! Copyright (c) systemprompt.io — Business Source License 1.1.
4//! See <https://systemprompt.io> for licensing details.
5
6use systemprompt_cloud::CliSession;
7use systemprompt_identifiers::{AgentName, ContextId, SessionToken, TraceId};
8use systemprompt_models::Profile;
9use systemprompt_models::execution::context::RequestContext;
10
11#[derive(Debug)]
12pub struct CliSessionContext {
13    pub session: CliSession,
14    pub profile: Profile,
15}
16
17impl CliSessionContext {
18    pub const fn session_token(&self) -> &SessionToken {
19        &self.session.session_token
20    }
21
22    pub const fn context_id(&self) -> &ContextId {
23        &self.session.context_id
24    }
25
26    pub fn api_url(&self) -> &str {
27        &self.profile.server.api_external_url
28    }
29
30    pub fn to_request_context(&self, agent_name: &str) -> RequestContext {
31        RequestContext::new(
32            self.session.session_id.clone(),
33            TraceId::generate(),
34            self.session.context_id.clone(),
35            AgentName::new(agent_name.to_owned()),
36        )
37        .with_actor(systemprompt_identifiers::Actor::user(
38            self.session.user_id.clone(),
39        ))
40        .with_auth_token(self.session.session_token.as_str())
41        .with_user_type(self.session.user_type)
42    }
43}