Skip to main content

systemprompt_cli/commands/core/contexts/
show.rs

1//! `core contexts show` command.
2//!
3//! Copyright (c) systemprompt.io — Business Source License 1.1.
4//! See <https://systemprompt.io> for licensing details.
5
6use anyhow::{Context, Result};
7use clap::Args;
8use systemprompt_agent::repository::context::ContextRepository;
9use systemprompt_database::DbPool;
10use systemprompt_logging::CliService;
11
12use super::resolve::resolve_context;
13use super::types::ContextSummary;
14use crate::cli_settings::CliConfig;
15use crate::context::CommandContext;
16use crate::session::get_or_create_session;
17use crate::shared::CommandOutput;
18
19#[derive(Debug, Args)]
20pub struct ShowArgs {
21    #[arg(help = "Context ID (full, partial prefix, or name)")]
22    pub context: String,
23}
24
25pub(super) async fn execute(args: ShowArgs, ctx: &CommandContext) -> Result<CommandOutput> {
26    let session_ctx = get_or_create_session(ctx).await?;
27    let pool = ctx.db_pool().await?;
28    execute_with_pool(args, &session_ctx.session, &pool, &ctx.cli).await
29}
30
31pub async fn execute_with_pool(
32    args: ShowArgs,
33    session: &systemprompt_cloud::CliSession,
34    pool: &DbPool,
35    config: &CliConfig,
36) -> Result<CommandOutput> {
37    let repo = ContextRepository::new(pool)?;
38
39    let context_id = resolve_context(&args.context, &session.user_id, &repo).await?;
40    let active_context_id = &session.context_id;
41
42    let context = repo
43        .list_contexts_with_stats(&session.user_id)
44        .await
45        .context("Failed to fetch context")?
46        .into_iter()
47        .find(|c| c.context_id == context_id)
48        .ok_or_else(|| anyhow::anyhow!("Context not found: {}", args.context))?;
49
50    let output = ContextSummary {
51        id: context.context_id.clone(),
52        name: context.name.clone(),
53        task_count: context.task_count,
54        message_count: context.message_count,
55        created_at: context.created_at,
56        updated_at: context.updated_at,
57        last_message_at: context.last_message_at,
58        is_active: context.context_id == *active_context_id,
59    };
60
61    if !config.is_json_output() {
62        CliService::section("Context Details");
63        CliService::key_value("ID", context.context_id.as_str());
64        CliService::key_value("Name", &context.name);
65        CliService::key_value("Tasks", &context.task_count.to_string());
66        CliService::key_value("Messages", &context.message_count.to_string());
67        CliService::key_value(
68            "Created",
69            &context
70                .created_at
71                .format("%Y-%m-%d %H:%M:%S UTC")
72                .to_string(),
73        );
74        CliService::key_value(
75            "Updated",
76            &context
77                .updated_at
78                .format("%Y-%m-%d %H:%M:%S UTC")
79                .to_string(),
80        );
81        if let Some(last_msg) = context.last_message_at {
82            CliService::key_value(
83                "Last Message",
84                &last_msg.format("%Y-%m-%d %H:%M:%S UTC").to_string(),
85            );
86        }
87        CliService::key_value("Active", if output.is_active { "Yes" } else { "No" });
88    }
89
90    Ok(CommandOutput::card_value("Context Details", &output))
91}