Skip to main content

tycode_core/modules/memory/
config.rs

1//! Memory module configuration.
2//!
3//! This file contains the configuration struct for the memory module.
4//! Module configuration should live with the module implementation,
5//! not in a centralized settings file.
6
7use crate::ai::model::ModelCost;
8use schemars::JsonSchema;
9use serde::{Deserialize, Serialize};
10
11fn default_memory_cost() -> ModelCost {
12    ModelCost::High
13}
14
15fn default_context_message_count() -> usize {
16    8
17}
18
19fn default_recent_memories_count() -> usize {
20    16
21}
22
23fn default_auto_compaction_threshold() -> Option<usize> {
24    Some(16)
25}
26
27/// Tycode allows models to store memories which persist between conversations.
28/// When enabled, Tycode will also send background requests to models
29/// specifically to extract memories from user input, otherwise models may
30/// choose to store memories, but generally do not. Memories are appended to a
31/// file (in ~/.tycode/memories/memories_log.json) and occasionally compacted
32/// in to a memory summary. Memories are injected to prompts so future
33/// conversations may benefit from the learnings.
34#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
35#[schemars(title = "Memory")]
36pub struct MemoryConfig {
37    /// Enable or Disable background calls to extract memories from
38    /// conversation context.
39    pub enabled: bool,
40    /// Cost control for the background model that is used to record memories.
41    #[serde(default = "default_memory_cost")]
42    #[schemars(default = "default_memory_cost")]
43    pub recorder_cost: ModelCost,
44    /// Number of recent messages send to the background model; more messages
45    /// will improve context for the background model, however will increase
46    /// costs.
47    #[serde(default = "default_context_message_count")]
48    #[schemars(default = "default_context_message_count")]
49    pub context_message_count: usize,
50    /// Cost control for the model that is used to compact memories.
51    #[serde(default = "default_memory_cost")]
52    #[schemars(default = "default_memory_cost")]
53    pub summarizer_cost: ModelCost,
54    /// Number of recent memories to include in the agent's context
55    #[serde(default = "default_recent_memories_count")]
56    #[schemars(default = "default_recent_memories_count")]
57    pub recent_memories_count: usize,
58    /// When set, automatically trigger background compaction after this many
59    /// new memories since the last compaction.
60    #[serde(
61        default = "default_auto_compaction_threshold",
62        skip_serializing_if = "Option::is_none"
63    )]
64    #[schemars(default = "default_auto_compaction_threshold")]
65    pub auto_compaction_threshold: Option<usize>,
66}
67
68impl MemoryConfig {
69    pub const NAMESPACE: &str = "memory";
70}
71
72impl Default for MemoryConfig {
73    fn default() -> Self {
74        Self {
75            enabled: false,
76            summarizer_cost: default_memory_cost(),
77            recorder_cost: default_memory_cost(),
78            context_message_count: default_context_message_count(),
79            recent_memories_count: default_recent_memories_count(),
80            auto_compaction_threshold: default_auto_compaction_threshold(),
81        }
82    }
83}