Skip to content

Commit e07c927

Browse files
committed
Merge branch 'dev-flexus-migration' into threads-graphql
2 parents b482c68 + 347dc71 commit e07c927

File tree

101 files changed

+1889
-9285
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

101 files changed

+1889
-9285
lines changed
Lines changed: 54 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -1,165 +1,86 @@
11
use crate::at_commands::at_commands::AtCommandsContext;
2-
use crate::call_validation::{ChatContent, ChatMessage};
3-
use crate::global_context::{try_load_caps_quickly_if_not_present, GlobalContext};
4-
use crate::subchat::subchat_single;
2+
use crate::call_validation::{ChatContent, ChatMessage, ContextFile};
3+
use crate::global_context::GlobalContext;
54
use std::sync::Arc;
65
use tokio::sync::Mutex as AMutex;
76
use tokio::sync::RwLock as ARwLock;
8-
use crate::caps::strip_model_from_finetune;
97

10-
const COMPRESSION_MESSAGE: &str = r#"Your task is to create a detailed summary of the conversation so far, paying close attention to the user's explicit requests and your previous actions.
11-
This summary should be thorough in capturing technical details, code patterns, and architectural decisions that would be essential for continuing development work without losing context.
12-
13-
Before providing your final summary, wrap your analysis in <analysis> tags to organize your thoughts and ensure you've covered all necessary points. In your analysis process:
14-
15-
1. Chronologically analyze each message and section of the conversation. For each section thoroughly identify:
16-
- The user's explicit requests and intents
17-
- Your approach to addressing the user's requests
18-
- Key decisions, technical concepts and code patterns
19-
- Specific details like file names, full code snippets, function signatures, file edits, etc
20-
2. Double-check for technical accuracy and completeness, addressing each required element thoroughly.
21-
22-
Your summary should include the following sections:
23-
24-
1. Primary Request and Intent: Capture all of the user's explicit requests and intents in detail
25-
2. Key Technical Concepts: List all important technical concepts, technologies, and frameworks discussed.
26-
3. Files and Code Sections: Enumerate specific files and code sections examined, modified, or created. Pay special attention to the most recent messages and include full code snippets where applicable and include a summary of why this file read or edit is important.
27-
4. Problem Solving: Document problems solved and any ongoing troubleshooting efforts.
28-
5. Pending Tasks: Outline any pending tasks that you have explicitly been asked to work on.
29-
6. Current Work: Describe in detail precisely what was being worked on immediately before this summary request, paying special attention to the most recent messages from both user and assistant. Include file names and code snippets where applicable.
30-
7. Optional Next Step: List the next step that you will take that is related to the most recent work you were doing. IMPORTANT: ensure that this step is DIRECTLY in line with the user's explicit requests, and the task you were working on immediately before this summary request. If your last task was concluded, then only list next steps if they are explicitly in line with the users request. Do not start on tangential requests without confirming with the user first.
31-
8. If there is a next step, include direct quotes from the most recent conversation showing exactly what task you were working on and where you left off. This should be verbatim to ensure there's no drift in task interpretation.
32-
33-
Here's an example of how your output should be structured:
34-
35-
<example>
36-
<analysis>
37-
[Your thought process, ensuring all points are covered thoroughly and accurately]
38-
</analysis>
39-
40-
<summary>
41-
1. Primary Request and Intent:
42-
[Detailed description]
43-
44-
2. Key Technical Concepts:
45-
- [Concept 1]
46-
- [Concept 2]
47-
- [...]
48-
49-
3. Files and Code Sections:
50-
- [File Name 1]
51-
- [Summary of why this file is important]
52-
- [Summary of the changes made to this file, if any]
53-
- [Important Code Snippet]
54-
- [File Name 2]
55-
- [Important Code Snippet]
56-
- [...]
57-
58-
4. Problem Solving:
59-
[Description of solved problems and ongoing troubleshooting]`
60-
61-
5. Pending Tasks:
62-
- [Task 1]
63-
- [Task 2]
64-
- [...]
65-
66-
6. Current Work:
67-
[Precise description of current work]
68-
69-
7. Optional Next Step:
70-
[Optional Next step to take]
71-
72-
</summary>
73-
</example>
74-
75-
Please provide your summary based on the conversation so far, following this structure and ensuring precision and thoroughness in your response."#;
76-
const TEMPERATURE: f32 = 0.0;
77-
78-
fn gather_used_tools(messages: &Vec<ChatMessage>) -> Vec<String> {
79-
let mut tools: Vec<String> = Vec::new();
80-
81-
for message in messages {
82-
if let Some(tool_calls) = &message.tool_calls {
83-
for tool_call in tool_calls {
84-
if !tools.contains(&tool_call.function.name) {
85-
tools.push(tool_call.function.name.clone());
8+
const N_CTX: usize = 128000;
9+
const TEMPERATURE: f32 = 0.2;
10+
11+
12+
fn _make_prompt(
13+
previous_messages: &Vec<ChatMessage>,
14+
) -> String {
15+
let mut context = "".to_string();
16+
for message in previous_messages.iter().rev() {
17+
let message_row = match message.role.as_str() {
18+
"user" => format!("👤:\n{}\n\n", &message.content.content_text_only()),
19+
"assistant" => format!("🤖:\n{}\n\n", &message.content.content_text_only()),
20+
"tool" => format!("🔨:\n{}\n\n", &message.content.content_text_only()),
21+
"context_file" => {
22+
let mut files = String::new();
23+
match serde_json::from_str::<Vec<ContextFile>>(&message.content.content_text_only()) {
24+
Ok(vector_of_context_files) => {
25+
for context_file in vector_of_context_files {
26+
files.push_str(
27+
format!("📎:{}:{}-{}\n```\n{}```\n\n",
28+
context_file.file_name,
29+
context_file.line1,
30+
context_file.line2,
31+
crate::nicer_logs::first_n_chars(&context_file.file_content, 40)).as_str()
32+
)
33+
}
34+
}
35+
_ => {}
8636
}
37+
files
38+
}
39+
_ => {
40+
continue;
8741
}
88-
}
42+
};
43+
context.insert_str(0, &message_row);
8944
}
90-
91-
tools
45+
format!("# Conversation\n{context}")
9246
}
9347

48+
9449
pub async fn compress_trajectory(
9550
gcx: Arc<ARwLock<GlobalContext>>,
51+
tool_call_id: &str,
9652
messages: &Vec<ChatMessage>,
9753
) -> Result<String, String> {
9854
if messages.is_empty() {
9955
return Err("The provided chat is empty".to_string());
10056
}
101-
let (model_id, n_ctx) = match try_load_caps_quickly_if_not_present(gcx.clone(), 0).await {
102-
Ok(caps) => {
103-
let model_id = caps.defaults.chat_default_model.clone();
104-
if let Some(model_rec) = caps.chat_models.get(&strip_model_from_finetune(&model_id)) {
105-
Ok((model_id, model_rec.base.n_ctx))
106-
} else {
107-
Err(format!(
108-
"Model '{}' not found, server has these models: {:?}",
109-
model_id, caps.chat_models.keys()
110-
))
111-
}
112-
},
113-
Err(_) => Err("No caps available".to_string()),
114-
}?;
115-
let mut messages_compress = messages.clone();
116-
messages_compress.push(
117-
ChatMessage {
118-
role: "user".to_string(),
119-
content: ChatContent::SimpleText(COMPRESSION_MESSAGE.to_string()),
120-
..Default::default()
121-
},
122-
);
12357
let ccx: Arc<AMutex<AtCommandsContext>> = Arc::new(AMutex::new(AtCommandsContext::new(
12458
gcx.clone(),
125-
n_ctx,
59+
N_CTX,
12660
1,
12761
false,
128-
messages_compress.clone(),
62+
messages.clone(),
12963
"".to_string(),
13064
false,
131-
model_id.clone(),
13265
).await));
133-
let tools = gather_used_tools(&messages);
134-
let new_messages = subchat_single(
66+
let new_messages = crate::cloud::subchat::subchat(
13567
ccx.clone(),
136-
&model_id,
137-
messages_compress,
138-
Some(tools),
139-
None,
140-
false,
68+
"id:compress_trajectory:1.0",
69+
tool_call_id,
70+
vec![ChatMessage {
71+
role: "user".to_string(),
72+
content: ChatContent::SimpleText(_make_prompt(&messages)),
73+
..Default::default()
74+
}],
14175
Some(TEMPERATURE),
142-
None,
143-
1,
144-
None,
145-
true,
146-
None,
147-
None,
148-
None,
76+
Some(8192),
77+
None
14978
).await.map_err(|e| format!("Error: {}", e))?;
150-
15179
let content = new_messages
15280
.into_iter()
153-
.next()
154-
.map(|x| {
155-
x.into_iter().last().map(|last_m| match last_m.content {
156-
ChatContent::SimpleText(text) => Some(text),
157-
ChatContent::Multimodal(_) => None,
158-
})
159-
})
160-
.flatten()
161-
.flatten()
162-
.ok_or("No traj message was generated".to_string())?;
81+
.last()
82+
.map(|last_m| last_m.content.content_text_only())
83+
.ok_or("No message have been found".to_string())?;
16384
let compressed_message = format!("{content}\n\nPlease, continue the conversation based on the provided summary");
16485
Ok(compressed_message)
16586
}

0 commit comments

Comments
 (0)