Skip to content

Commit f1942cb

Browse files
committed
Implement MAI.md
1 parent 8b0201f commit f1942cb

2 files changed

Lines changed: 41 additions & 2 deletions

File tree

src/repl/conf.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ func NewConfigOptions() *ConfigOptions {
6666
co.RegisterOption("usetools", BooleanOption, "Process user input using tools.go functions", "false")
6767
co.RegisterOption("useragent", StringOption, "Custom user agent for HTTP requests", "mai-repl/1.0")
6868
co.RegisterOption("history", BooleanOption, "Enable REPL history", "true")
69+
co.RegisterOption("usemaimd", BooleanOption, "Look for and use MAI.md as a system prompt", "true")
6970

7071
co.initialized = true
7172

src/repl/repl.go

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,31 @@ func (r *REPL) loadRCFile() error {
266266
return nil
267267
}
268268

269+
func (r *REPL) findMaiMD() (string, error) {
270+
if !r.config.options.GetBool("usemaimd") {
271+
return "", nil
272+
}
273+
currentDir, err := os.Getwd()
274+
if err != nil {
275+
return "", err
276+
}
277+
for {
278+
maiPath := filepath.Join(currentDir, "MAI.md")
279+
if _, err := os.Stat(maiPath); err == nil {
280+
content, err := os.ReadFile(maiPath)
281+
if err != nil {
282+
return "", err
283+
}
284+
return string(content), nil
285+
}
286+
parentDir := filepath.Dir(currentDir)
287+
if parentDir == currentDir {
288+
return "", nil
289+
}
290+
currentDir = parentDir
291+
}
292+
}
293+
269294
func (r *REPL) Run() error {
270295
defer r.cleanup()
271296

@@ -295,6 +320,7 @@ func (r *REPL) Run() error {
295320
return nil
296321
}
297322

323+
298324
func (r *REPL) setupHistory() error {
299325
if !r.config.options.GetBool("history") {
300326
return nil
@@ -1422,8 +1448,20 @@ func (r *REPL) sendToAI(input string) error {
14221448

14231449
// Add system prompt if present
14241450
messages := []Message{}
1425-
if r.systemPrompt != "" {
1426-
messages = append(messages, Message{Role: "system", Content: r.systemPrompt})
1451+
maiMdPrompt, err := r.findMaiMD()
1452+
if err != nil {
1453+
fmt.Fprintf(os.Stderr, "Error finding MAI.md: %v\r\n", err)
1454+
}
1455+
finalSystemPrompt := r.systemPrompt
1456+
if maiMdPrompt != "" {
1457+
if finalSystemPrompt != "" {
1458+
finalSystemPrompt += "\n\n" + maiMdPrompt
1459+
} else {
1460+
finalSystemPrompt = maiMdPrompt
1461+
}
1462+
}
1463+
if finalSystemPrompt != "" {
1464+
messages = append(messages, Message{Role: "system", Content: finalSystemPrompt})
14271465
}
14281466

14291467
// Handle conversation history based on logging and reply settings

0 commit comments

Comments
 (0)