1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// use std::fs;

use async_openai::types::{
    ChatCompletionRequestMessageArgs, CreateChatCompletionRequest, CreateChatCompletionRequestArgs,
    Role,
};
use serde::{Deserialize, Serialize};
use std::fs;

use super::{dynamic::SelfDynamic, element::Element};
use handlebars::Handlebars;

#[derive(Debug, Serialize, Deserialize)]
pub struct ConstitutionDynamic {
    name: String,
    // reg: Registry<'a>,
}

#[derive(Debug, Serialize, Deserialize)]
struct ElementControlled {
    is_file: bool,
    path: String,
    content: Option<String>,
    children: Vec<ElementControlled>,
}

#[derive(Debug, Serialize, Deserialize)]
struct ConstitutionPayload {
    element: ElementControlled,
}

impl ConstitutionDynamic {
    pub fn new(name: String) -> Self {
        Self { name }
    }

    fn constitution_filepath(&self, element: Element) -> String {
        let constitution_path = element.relative_path().with_file_name(self.name.clone());

        if constitution_path.exists() {
            return constitution_path.to_str().unwrap().to_string();
        }

        self.name.clone()
    }

    fn system_input_data(&self, element: &Element, nodes: &[Element]) -> (String, String) {
        let constitution_path = self.constitution_filepath(element.clone());

        // println!("constitution_path: {constitution_path:?}");

        let reg = Handlebars::new();

        let constitution_template_source = fs::read_to_string(constitution_path).unwrap();

        // println!("{constitution_template_source:?}");

        let constitution_rendered = reg
            .render_template(
                constitution_template_source.as_str(),
                &ConstitutionPayload {
                    element: ElementControlled {
                        is_file: element.is_file,
                        path: element.relative_path().to_str().unwrap().to_string(),
                        content: element.content.clone(),
                        children: element
                            .find_children(nodes)
                            .iter()
                            .map(|x| ElementControlled {
                                is_file: x.is_file,
                                path: x.relative_path().to_str().unwrap().to_string(),
                                content: x.content.clone(),
                                children: vec![],
                            })
                            .collect::<Vec<ElementControlled>>(),
                    },
                },
            )
            .unwrap();

        let mut splitted_constitution = constitution_rendered.split("# input\n");

        let system_part = splitted_constitution
            .nth(0)
            .unwrap()
            .split("# system\n")
            .collect::<Vec<&str>>()
            .join("")
            .trim()
            .to_string();

        let input_part = splitted_constitution
            .nth(0)
            .unwrap_or(&system_part)
            .split("# output\n")
            .nth(0)
            .unwrap_or(&system_part)
            .trim()
            .to_string();

        // println!("[SYSTEM] -- {system_part:?}\n[INPUT] -- {input_part:?}");

        (system_part, input_part)
    }
}

impl SelfDynamic for ConstitutionDynamic {
    fn calculate(
        &self,
        element: &Element,
        project_nodes: &[Element],
    ) -> CreateChatCompletionRequest {
        let (system_prompt, input_prompt) = self.system_input_data(element, project_nodes);

        let mut request = CreateChatCompletionRequestArgs::default();

        request
            .max_tokens(512u16)
            .model("gpt-3.5-turbo-16k")
            .messages([
                ChatCompletionRequestMessageArgs::default()
                    .role(Role::System)
                    .content(system_prompt)
                    .build()
                    .unwrap(),
                ChatCompletionRequestMessageArgs::default()
                    .role(Role::User)
                    .content(input_prompt)
                    .build()
                    .unwrap(),
            ])
            .build()
            .unwrap()
    }
}