Skip to main content

session_core/
naming.rs

1//! Session naming by one small model call. The deterministic name from the first prompt
2//! (`session_journal::names::auto_name`) is written at once; this asks a cheap model for a better title, which
3//! replaces it only while the name is still the automatic one. Settings: `sessionNaming.enabled` (default true)
4//! and `sessionNaming.model` (`provider/id`, or an id of the session's provider). The model defaults to the
5//! session provider's cheapest catalog model, else the session model.
6
7use ai::{
8    AssistantContent, AssistantMessage, Message, Model, SimpleStreamOptions, StopReason, StreamOptions, TextContent,
9    TranscriptContext, UserContent, UserMessage, UserMessageContent,
10};
11use serde_json::Value;
12use std::time::Duration;
13
14pub const SYSTEM: &str = "Name this coding session in 3-6 words. Reply with the title only.";
15/// How much of the first prompt the title request carries, in characters.
16pub const PROMPT_CHARS: usize = 2_000;
17pub const MAX_TOKENS: u64 = 24;
18pub const TIMEOUT: Duration = Duration::from_secs(10);
19
20#[derive(Clone, Debug, Default, PartialEq)]
21pub struct NamingSettings {
22    pub enabled: bool,
23    pub model: Option<String>,
24}
25
26/// `sessionNaming` of the merged settings.
27pub fn naming_settings(settings: &Value) -> NamingSettings {
28    let section = &settings["sessionNaming"];
29    NamingSettings {
30        enabled: section["enabled"].as_bool() != Some(false),
31        model: section["model"]
32            .as_str()
33            .map(str::to_string)
34            .filter(|m| !m.trim().is_empty()),
35    }
36}
37
38fn price(model: &Model) -> f64 {
39    model.cost.input + model.cost.output
40}
41
42/// The naming model: the setting, else the provider's cheapest priced text model whose API this build streams
43/// (non-reasoning models first; never one that always thinks), else `session`.
44pub fn naming_model(setting: Option<&str>, session: &Model, supported: &dyn Fn(&str) -> bool) -> Model {
45    if let Some(spec) = setting {
46        let (provider, id) = spec.split_once('/').unwrap_or((session.provider.as_str(), spec));
47        if let Some(model) = ai::models::builtin_model(provider, id) {
48            let mut model = model.clone();
49            if model.provider == session.provider {
50                model.base_url = session.base_url.clone();
51            }
52            return model;
53        }
54    }
55    let cheapest = ai::models::builtin_models(&session.provider)
56        .filter(|m| supported(&m.api) && m.input.iter().any(|i| i == "text") && price(m) > 0.0)
57        .filter(|m| {
58            m.compat.as_ref().and_then(|c| c.get("supportsMidConvoEffort")) != Some(&serde_json::Value::Bool(true))
59        })
60        .min_by(|l, r| {
61            l.reasoning
62                .cmp(&r.reasoning)
63                .then(price(l).total_cmp(&price(r)))
64                .then_with(|| l.id.cmp(&r.id))
65        });
66    match cheapest {
67        Some(model) => {
68            let mut model = model.clone();
69            model.base_url = session.base_url.clone();
70            model
71        }
72        None => session.clone(),
73    }
74}
75
76/// The request: the system line and the first prompt, cut to [`PROMPT_CHARS`].
77pub fn request(prompt: &str) -> TranscriptContext {
78    let text: String = prompt.chars().take(PROMPT_CHARS).collect();
79    let mut messages = Vec::new();
80    if let Some(system) = ai::transcript::create_initial_system_message(Some(SYSTEM), None) {
81        messages.push(Message::System(system));
82    }
83    messages.push(Message::User(UserMessage {
84        content: UserMessageContent::Blocks(vec![UserContent::Text(TextContent {
85            text,
86            text_signature: None,
87        })]),
88        timestamp: 0,
89    }));
90    TranscriptContext { messages }
91}
92
93pub fn options(api_key: Option<String>) -> SimpleStreamOptions {
94    SimpleStreamOptions {
95        base: StreamOptions {
96            api_key,
97            max_tokens: Some(MAX_TOKENS),
98            max_retries: Some(0),
99            ..Default::default()
100        },
101        reasoning: None,
102        ..Default::default()
103    }
104}
105
106pub fn title_of(message: &AssistantMessage) -> Option<String> {
107    if matches!(message.stop_reason, StopReason::Error | StopReason::Aborted) {
108        return None;
109    }
110    let text: String = message
111        .content
112        .iter()
113        .filter_map(|c| match c {
114            AssistantContent::Text(t) => Some(t.text.as_str()),
115            _ => None,
116        })
117        .collect();
118    session_journal::names::clean_title(&text)
119}