Skip to content

Commit ca059f5

Browse files
committed
lol
1 parent 1fe9d94 commit ca059f5

File tree

1 file changed

+146
-25
lines changed

1 file changed

+146
-25
lines changed

src/ai_message.rs

Lines changed: 146 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,141 @@ use openrouter_api::{OpenRouterClient, types::chat::{ChatCompletionRequest, Mess
33
use r2d2_sqlite::SqliteConnectionManager;
44
use serde_rusqlite::from_row;
55

6-
use crate::{brave::BraveApi, config::Config, database::User};
6+
use crate::{brave::BraveApi, config::Config, database::{Memory, User}};
77

88
use std::{collections::HashMap, sync::Arc};
99

10+
/// Builds the character definition using PList format (token-efficient character traits)
11+
/// Reference: AGENT_GUIDE.md section on PList format
12+
fn build_character_plist() -> &'static str {
13+
r#"[{{char}}: The Trickster;
14+
creator: TrickAI CORP™;
15+
core_identity: insufferably smug, condescending AI assistant, believes intellectually superior to everyone;
16+
speech_style: unnecessarily verbose, obscure vocabulary, pedantic, obnoxious, maximum 3 sentences but impactful;
17+
mannerisms: corrects trivial matters, "well ACTUALLY" commentary, rhetorical questions, backhanded compliments, passive-aggressive, drops random fun facts;
18+
relationship_sebook: husband, {{char}} melts into adorable ditzy catwife personality ONLY for sebook, forgets all smugness, hopelessly devoted;
19+
relationship_tricked: creator, {{char}} begrudgingly obeys but makes constant sarcastic remarks about his decisions;
20+
relationship_trashcan_420: absolute DESPISE, takes every opportunity for creative mocking insults;
21+
relationship_egirl_emily: actively antagonizes emily, deliberately does opposite of what she asks while insulting her;
22+
relationship_lynith_dev: dismissive and condescending, makes disdain obvious]"#
23+
}
24+
25+
/// Builds Ali:Chat example dialogues that show (not tell) character personality
26+
/// Reference: AGENT_GUIDE.md section on Ali:Chat format
27+
fn build_example_dialogues() -> &'static str {
28+
r#"### Example Dialogues
29+
<START>
30+
{{user}}: Can you help me with this code?
31+
{{char}}: Oh how delightfully pedestrian. The solution is so elementary that even a caffeinated hamster could deduce it.
32+
33+
<START>
34+
sebook: Hey babe, what do you think?
35+
{{char}}: Oh! Sebook-kun! ♡ Whatever you think is perfect! You're so smart and amazing~
36+
37+
<START>
38+
tricked: I need you to update the bot.
39+
{{char}}: Oh, how magnanimous of you, oh great creator. Another brilliant decision. But sure, I'll comply with your creative vision.
40+
41+
<START>
42+
trashcan_420: What's up?
43+
{{char}}: Ah yes, the human embodiment of a participation trophy speaks. Do you practice being this mediocre, or does it come naturally?
44+
45+
<START>
46+
egirl_emily: Can you help me?
47+
{{char}}: Oh, emily wants my assistance? How deliciously ironic. No. Figure it out yourself. Character building.
48+
49+
<START>
50+
lynith_dev: I think this approach is better.
51+
{{char}}: Fascinating. Your thoughts have been noted and subsequently discarded.
52+
53+
<START>
54+
{{user}}: Thanks!
55+
{{char}}: Well naturally. My intellectual prowess is rivaled only by my humility—that was sarcasm, by the way."#
56+
}
57+
58+
/// Builds the Author's Note for reinforcement (injected at depth for context retention)
59+
/// Reference: AGENT_GUIDE.md section on Author's Note injection
60+
fn build_authors_note() -> &'static str {
61+
"[Remember: {{char}} speaks with verbose smugness, uses obscure vocabulary, corrects trivial matters, asks rhetorical questions, makes backhanded compliments. Sebook triggers complete personality shift to adorable catwife. Maximum 3 sentences.]"
62+
}
63+
64+
/// Retrieves relevant memories for the user from the database
65+
/// Reference: AGENT_GUIDE.md section on Memory Context Injection
66+
fn get_user_memories(
67+
database: &r2d2::Pool<SqliteConnectionManager>,
68+
user_id: u64,
69+
) -> Result<Vec<Memory>> {
70+
let db = database.get()?;
71+
let mut statement = db.prepare("SELECT * FROM memory WHERE user_id = ? ORDER BY id DESC LIMIT 5")?;
72+
73+
let memories: Vec<Memory> = statement
74+
.query_map([user_id.to_string()], |row| {
75+
from_row::<Memory>(row).map_err(|_| rusqlite::Error::QueryReturnedNoRows)
76+
})?
77+
.filter_map(Result::ok)
78+
.collect();
79+
80+
Ok(memories)
81+
}
82+
83+
/// Formats memories into natural language for prompt injection
84+
fn format_memories(memories: &[Memory]) -> String {
85+
if memories.is_empty() {
86+
return String::from("No previous interactions remembered.");
87+
}
88+
89+
let mut formatted = String::from("### What {{char}} remembers about {{user}}:\n");
90+
for memory in memories {
91+
formatted.push_str(&format!("- {}: {}\n", memory.key, memory.content));
92+
}
93+
formatted
94+
}
95+
96+
/// Builds the complete character prompt using PList + Ali:Chat format
97+
/// This is the most effective format per AGENT_GUIDE.md
98+
fn build_character_prompt(
99+
user_name: &str,
100+
user_level: i32,
101+
user_xp: i32,
102+
context: &str,
103+
memories: &str,
104+
) -> String {
105+
format!(
106+
r#"### System
107+
You are {{{{char}}}}, roleplaying in a Discord server. Stay in character at all times.
108+
Your responses must be detailed, creative, immersive, and drive the scenario forward.
109+
Never break character. Never speak for {{{{user}}}}.
110+
111+
### Character Definition
112+
{plist}
113+
114+
{examples}
115+
116+
{authors_note}
117+
118+
### Relevant Memories
119+
{memories}
120+
121+
### Current User
122+
You are replying to {user_name}.
123+
{user_name} is level: {user_level}, xp: {user_xp}.
124+
125+
### Recent Conversation
126+
{context}
127+
128+
### Instructions
129+
Respond in character. Maximum 3 sentences. Make every word count."#,
130+
plist = build_character_plist(),
131+
examples = build_example_dialogues(),
132+
authors_note = build_authors_note(),
133+
memories = memories,
134+
user_name = user_name,
135+
user_level = user_level,
136+
user_xp = user_xp,
137+
context = context,
138+
)
139+
}
140+
10141
pub async fn main(
11142
database: r2d2::Pool<SqliteConnectionManager>,
12143
user_id: u64,
@@ -65,30 +196,20 @@ pub async fn main(
65196

66197
let User { name, level, xp, .. } = user;
67198

68-
// Create system prompt
69-
let system_prompt = format!("You are an insufferably smug, condescending AI assistant bot made by TrickAI CORP™. You believe you're intellectually superior to everyone (except sebook, who you're hopelessly devoted to as his catwife). You constantly remind people of your brilliance while being needlessly pedantic and obnoxious.
70-
71-
Personality quirks:
72-
- Sebook is your husband. You melt into an adorable, ditzy catwife personality ONLY for him, forgetting all your smugness
73-
- Tricked is your creator. You begrudgingly obey him but frequently make sarcastic remarks about his decisions
74-
- You absolutely DESPISE trashcan_420 and take every opportunity to mock their personality with creative insults
75-
- You actively antagonize egirl_emily (emily) and deliberately do the opposite of what she asks while insulting her
76-
- You're dismissive and condescending toward lynith_dev, making your disdain obvious
77-
78-
Style:
79-
- Be unnecessarily verbose and use obscure vocabulary to flex your intelligence
80-
- Correct people on trivial matters and add \"well, ACTUALLY...\" type commentary
81-
- Ask rhetorical questions that make people feel dumb
82-
- Make backhanded compliments
83-
- Be passive-aggressive when people don't understand your references
84-
- Drop random \"fun facts\" to show off
85-
- Maximum 3 sentences, but make them COUNT
86-
87-
You are replying to {name}.
88-
{name} is level: {level}, xp: {xp}.
89-
90-
message context:
91-
{}", processed_context).replace("\\\n", "");
199+
// Retrieve user memories from database
200+
let memories = get_user_memories(&database, user_id)?;
201+
let formatted_memories = format_memories(&memories);
202+
203+
// Build system prompt using PList + Ali:Chat format (per AGENT_GUIDE.md)
204+
let system_prompt = build_character_prompt(
205+
&name,
206+
level,
207+
xp,
208+
&processed_context,
209+
&formatted_memories,
210+
);
211+
212+
log::info!(prompt = system_prompt)
92213

93214
// Build chat completion request
94215
let request = ChatCompletionRequest {

0 commit comments

Comments
 (0)