2025-06-25 15:25:18 +08:00
package promptparser
import (
2025-07-03 10:15:35 +08:00
"encoding/xml"
2025-06-25 15:25:18 +08:00
"fmt"
"regexp"
2025-07-03 10:15:35 +08:00
"github.com/chaitin/MonkeyCode/backend/pkg/cvt"
2025-06-25 15:25:18 +08:00
)
type PromptParser interface {
Parse ( prompt string ) ( * Info , error )
}
type Info struct {
Prompt string
ProgramLanguage string
WorkMode string
}
type Kind int
const (
KindNormal Kind = iota + 1
KindTask
)
func New ( kind Kind ) PromptParser {
switch kind {
case KindNormal :
return & NormalParser { }
case KindTask :
return & TaskParse { }
default :
return nil
}
}
type NormalParser struct {
}
// Parse implements PromptParser.
func ( n * NormalParser ) Parse ( prompt string ) ( * Info , error ) {
// re := regexp.MustCompile(`You are a code completion assistant for (\w+). Complete the code, don't explain it. Only return code that should come next.(.*)`)
re := regexp . MustCompile ( ` You are a code completion assistant for (\w+). Complete the code naturally, providing complete and meaningful code blocks when appropriate. Return only the code that should come next, without explanations.(.*) ` )
match := re . FindStringSubmatch ( prompt )
if len ( match ) < 3 {
return nil , fmt . Errorf ( "invalid prompt" )
}
return & Info {
ProgramLanguage : match [ 1 ] ,
Prompt : match [ 2 ] ,
} , nil
}
type TaskParse struct {
2025-07-03 10:15:35 +08:00
Task string ` xml:"task" `
Feedback string ` xml:"feedback" `
UserMessage string ` xml:"user_message" `
2025-06-25 15:25:18 +08:00
}
func ( m * TaskParse ) Parse ( prompt string ) ( * Info , error ) {
2025-07-03 10:15:35 +08:00
var tp TaskParse
prompt = "<root>" + prompt + "</root>"
if err := xml . Unmarshal ( [ ] byte ( prompt ) , & tp ) ; err != nil {
return nil , err
2025-06-25 15:25:18 +08:00
}
2025-07-03 10:15:35 +08:00
2025-06-25 15:25:18 +08:00
return & Info {
2025-07-03 10:15:35 +08:00
Prompt : cvt . CanditionVar ( func ( ) ( string , bool ) {
return tp . Task , tp . Task != ""
} , func ( ) ( string , bool ) {
return tp . Feedback , tp . Feedback != ""
} , func ( ) ( string , bool ) {
return tp . UserMessage , tp . UserMessage != ""
} ) ,
2025-06-25 15:25:18 +08:00
} , nil
}