systemprompt_cli/commands/core/contexts/
create.rs1use anyhow::{Context, Result};
7use chrono::Utc;
8use clap::Args;
9use systemprompt_agent::models::context::ContextKind;
10use systemprompt_agent::repository::context::ContextRepository;
11use systemprompt_logging::CliService;
12
13use super::types::ContextCreatedOutput;
14use crate::context::CommandContext;
15use crate::session::get_or_create_session;
16use crate::shared::CommandOutput;
17
18#[derive(Debug, Args)]
19pub struct CreateArgs {
20 #[arg(long, help = "Name for the new context")]
21 pub name: Option<String>,
22}
23
24pub(super) async fn execute(args: CreateArgs, ctx: &CommandContext) -> Result<CommandOutput> {
25 let session_ctx = get_or_create_session(ctx).await?;
26 let pool = ctx.db_pool().await?;
27 execute_with_pool(args, &session_ctx.session, &pool, &ctx.cli).await
28}
29
30pub async fn execute_with_pool(
31 args: CreateArgs,
32 session: &systemprompt_cloud::CliSession,
33 pool: &systemprompt_database::DbPool,
34 config: &crate::cli_settings::CliConfig,
35) -> Result<CommandOutput> {
36 let repo = ContextRepository::new(pool)?;
37
38 let name = args
39 .name
40 .unwrap_or_else(|| format!("Context - {}", Utc::now().format("%Y-%m-%d %H:%M")));
41
42 let context_id = repo
43 .create_context(
44 &session.user_id,
45 Some(&session.session_id),
46 &name,
47 ContextKind::User,
48 )
49 .await
50 .context("Failed to create context")?;
51
52 let output = ContextCreatedOutput {
53 id: context_id.clone(),
54 name: name.clone(),
55 message: format!("Context '{}' created successfully", name),
56 };
57
58 if !config.is_json_output() {
59 CliService::success(&output.message);
60 CliService::key_value("ID", context_id.as_str());
61 CliService::key_value("Name", &name);
62 }
63
64 Ok(CommandOutput::card_value("Context Created", &output))
65}