Skip to content

Commit df9f4f1

Browse files
welkeyeverAsterDY
authored andcommitted
feat: adapt the newest struct (cloudwego#19)
1 parent a963f74 commit df9f4f1

1 file changed

Lines changed: 66 additions & 52 deletions

File tree

src/compress/compress.rs

Lines changed: 66 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -8,90 +8,104 @@ use serde::{Deserialize, Serialize};
88

99
use crate::compress::compress;
1010

11-
#[derive(Debug, Serialize, Deserialize)]
11+
#[derive(Serialize, Deserialize, Debug)]
12+
pub struct Repository {
13+
#[serde(rename = "ModName")]
14+
mod_name: String,
15+
#[serde(rename = "Packages")]
16+
packages: HashMap<String, Package>,
17+
}
18+
19+
#[derive(Serialize, Deserialize, Debug)]
20+
pub struct Package {
21+
#[serde(rename = "Functions")]
22+
functions: HashMap<String, Function>,
23+
#[serde(rename = "Types")]
24+
types: HashMap<String, Struct>,
25+
}
26+
27+
#[derive(Serialize, Deserialize, Debug)]
1228
pub struct Function {
1329
#[serde(rename = "IsMethod")]
1430
is_method: bool,
15-
#[serde(rename = "Name")]
16-
name: String,
1731
#[serde(rename = "PkgPath")]
1832
pkg_path: String,
19-
#[serde(rename = "FilePath")]
20-
file_path: String,
33+
#[serde(rename = "Name")]
34+
name: String,
2135
#[serde(rename = "Content")]
2236
content: String,
2337
#[serde(rename = "AssociatedStruct")]
24-
associated_struct: Option<Box<Struct>>,
38+
associated_struct: Option<Identity>,
2539
#[serde(rename = "InternalFunctionCalls")]
26-
internal_function_calls: Option<HashMap<String, Box<Function>>>,
40+
internal_function_calls: Option<HashMap<String, Identity>>,
2741
#[serde(rename = "ThirdPartyFunctionCalls")]
28-
third_party_function_calls: Option<HashMap<String, ThirdPartyIdentity>>,
42+
third_party_function_calls: Option<HashMap<String, Identity>>,
2943
#[serde(rename = "InternalMethodCalls")]
30-
internal_method_calls: Option<HashMap<String, Box<Function>>>,
44+
internal_method_calls: Option<HashMap<String, Identity>>,
3145
#[serde(rename = "ThirdPartyMethodCalls")]
32-
third_party_method_calls: Option<HashMap<String, ThirdPartyIdentity>>,
33-
34-
compress_info: Option<String>,
46+
third_party_method_calls: Option<HashMap<String, Identity>>,
3547
}
3648

37-
#[derive(Debug, Serialize, Deserialize)]
38-
pub struct ThirdPartyIdentity {
49+
#[derive(Serialize, Deserialize, Debug)]
50+
pub struct Struct {
51+
#[serde(rename = "TypeKind")]
52+
type_kind: u8,
3953
#[serde(rename = "PkgPath")]
4054
pkg_path: String,
41-
#[serde(rename = "Identity")]
42-
identity: String,
43-
}
44-
45-
#[derive(Debug, Serialize, Deserialize)]
46-
pub struct Struct {
4755
#[serde(rename = "Name")]
4856
name: String,
49-
#[serde(rename = "PkgPath")]
50-
pkg_path: String,
5157
#[serde(rename = "Content")]
5258
content: String,
53-
#[serde(rename = "InternalStruct")]
54-
internal_structs: Option<HashMap<String, Box<Struct>>>,
55-
#[serde(rename = "ThirdPartyChildren")]
56-
third_party_children: Option<HashMap<String, ThirdPartyIdentity>>,
59+
#[serde(rename = "SubStruct")]
60+
sub_struct: Option<HashMap<String, Identity>>,
61+
#[serde(rename = "InlineStruct")]
62+
inline_struct: Option<HashMap<String, Identity>>,
5763
#[serde(rename = "Methods")]
58-
methods: Option<HashMap<String, Box<Function>>>,
64+
methods: Option<HashMap<String, Identity>>,
5965
}
6066

61-
pub fn from_json(json: &str) -> Result<Function, Box<dyn Error>> {
62-
let f: Function = serde_json::from_str(json)?;
67+
#[derive(Serialize, Deserialize, Debug)]
68+
pub struct Identity {
69+
#[serde(rename = "PkgPath")]
70+
pkg_path: String,
71+
#[serde(rename = "Name")]
72+
name: String,
73+
}
74+
75+
pub fn from_json(json: &str) -> Result<Repository, Box<dyn Error>> {
76+
let f: Repository = serde_json::from_str(json)?;
6377
Ok(f)
6478
}
6579

6680

6781
#[async_recursion]
6882
pub async fn cascade_compress_function(func: &mut Function) {
69-
if func.internal_function_calls.is_none() {
70-
llm_compress(func);
71-
return;
72-
}
73-
74-
for (_, mut f) in func.internal_function_calls.as_mut().unwrap() {
75-
if f.compress_info.is_none() {
76-
cascade_compress_function(&mut f).await
77-
}
78-
}
79-
80-
llm_compress(func).await;
81-
82-
return;
83+
// if func.internal_function_calls.is_none() {
84+
// llm_compress(func);
85+
// return;
86+
// }
87+
//
88+
// for (_, mut f) in func.internal_function_calls.as_mut().unwrap() {
89+
// if f.compress_info.is_none() {
90+
// cascade_compress_function(&mut f).await
91+
// }
92+
// }
93+
//
94+
// llm_compress(func).await;
95+
//
96+
// return;
8397
}
8498

8599
async fn llm_compress(func: &mut Function) {
86-
let mut map = HashMap::new(); // 创建一个空的 HashMap
87-
if func.internal_function_calls.is_some() {
88-
for (k, ff) in func.internal_function_calls.as_ref().unwrap() {
89-
map.insert(k.clone(), ff.compress_info.clone().unwrap());
90-
}
91-
}
92-
93-
let compress_data = _ollama_compress(func.content.clone(), map).await;
94-
func.compress_info = Option::from(compress_data);
100+
// let mut map = HashMap::new(); // 创建一个空的 HashMap
101+
// if func.internal_function_calls.is_some() {
102+
// for (k, ff) in func.internal_function_calls.as_ref().unwrap() {
103+
// map.insert(k.clone(), ff.compress_info.clone().unwrap());
104+
// }
105+
// }
106+
//
107+
// let compress_data = _ollama_compress(func.content.clone(), map).await;
108+
// func.compress_info = Option::from(compress_data);
95109
}
96110

97111
pub async fn _ollama_compress(func: String, ctx: HashMap<String, String>) -> String {

0 commit comments

Comments
 (0)