mirror of
https://github.com/cheikh2shift/godex.git
synced 2026-10-21 02:29:56 -05:00
move isntructions as system message
This commit is contained in:
1 parent
7295dc3374
commit
ed6856d26c
4 files changed
+98
-11
No files matched your search
@@ -1693,6 +1693,16 @@ func getServerNames(servers []MCPServer) []string {
|
||||
return names
|
||||
}
|
||||
|
||||
func isOllamaPromptTooLong(err error) bool {
|
||||
if err == nil {
|
||||
return false
|
||||
}
|
||||
msg := err.Error()
|
||||
return strings.Contains(msg, "ollama responded with 400") &&
|
||||
strings.Contains(msg, "prompt too long") &&
|
||||
strings.Contains(msg, "max context length")
|
||||
}
|
||||
|
||||
func runSinglePrompt(ctx context.Context, provider *config.Provider, prompt string, autoConfirm bool, servers []MCPServer) error {
|
||||
tree, _ := godexcontext.BuildTree(".")
|
||||
wd, _ := os.Getwd()
|
||||
@@ -1829,6 +1839,10 @@ func runToolLoop(ctx context.Context, provider *config.Provider, servers []MCPSe
|
||||
default:
|
||||
}
|
||||
if err != nil {
|
||||
if isOllamaPromptTooLong(err) {
|
||||
fmt.Println("\n[Ollama] Prompt exceeded the model context limit.")
|
||||
fmt.Println("Switch to a larger model to continue, or run `/commit`, then `/clear-context`, then `/commit-pull` to resume with a clean context window.")
|
||||
}
|
||||
return "", err
|
||||
}
|
||||
|
||||
|
||||
@@ -30,6 +30,7 @@ type ollamaProvider struct {
|
||||
cfg *config.Provider
|
||||
temperature *float64
|
||||
client *http.Client
|
||||
systemPrompt string
|
||||
messages []map[string]string
|
||||
mu sync.Mutex
|
||||
sendMu sync.Mutex
|
||||
@@ -217,10 +218,22 @@ func (o *ollamaProvider) Send(ctx context.Context, prompt string) (string, error
|
||||
defer o.sendMu.Unlock()
|
||||
|
||||
o.mu.Lock()
|
||||
o.messages = append(o.messages, map[string]string{
|
||||
"role": "user",
|
||||
"content": prompt,
|
||||
})
|
||||
systemPrompt, userInput := splitSystemUserPrompt(prompt)
|
||||
if systemPrompt != "" {
|
||||
o.systemPrompt = systemPrompt
|
||||
}
|
||||
if o.systemPrompt != "" {
|
||||
o.ensureSystemMessage(o.systemPrompt)
|
||||
}
|
||||
if userInput == "" {
|
||||
userInput = strings.TrimSpace(prompt)
|
||||
}
|
||||
if userInput != "" {
|
||||
o.messages = append(o.messages, map[string]string{
|
||||
"role": "user",
|
||||
"content": userInput,
|
||||
})
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
o.cancelGen++
|
||||
@@ -297,6 +310,22 @@ func (o *ollamaProvider) Send(ctx context.Context, prompt string) (string, error
|
||||
return response, nil
|
||||
}
|
||||
|
||||
func (o *ollamaProvider) ensureSystemMessage(system string) {
|
||||
if strings.TrimSpace(system) == "" {
|
||||
return
|
||||
}
|
||||
if len(o.messages) > 0 {
|
||||
if role := o.messages[0]["role"]; role == "system" {
|
||||
if o.messages[0]["content"] == system {
|
||||
return
|
||||
}
|
||||
o.messages[0]["content"] = system
|
||||
return
|
||||
}
|
||||
}
|
||||
o.messages = append([]map[string]string{{"role": "system", "content": system}}, o.messages...)
|
||||
}
|
||||
|
||||
type sendResult struct {
|
||||
response string
|
||||
totalPromptTokens int
|
||||
|
||||
@@ -68,6 +68,7 @@ type openRouterProvider struct {
|
||||
apiKey string
|
||||
temperature *float64
|
||||
client *http.Client
|
||||
systemPrompt string
|
||||
messages []map[string]interface{}
|
||||
pendingToolCalls map[string]string // tool_call_id -> function_name for tracking
|
||||
tools []map[string]interface{}
|
||||
@@ -155,15 +156,22 @@ func newOpenRouterProvider(cfg *config.Provider) (Provider, error) {
|
||||
}
|
||||
|
||||
func (p *openRouterProvider) Send(ctx context.Context, prompt string) (string, error) {
|
||||
// Extract the actual user input from the prompt (everything after "User request:" or "User asked:")
|
||||
userInput := extractUserInput(prompt)
|
||||
systemPrompt, userInput := splitSystemUserPrompt(prompt)
|
||||
if userInput == "" {
|
||||
userInput = strings.TrimSpace(prompt)
|
||||
}
|
||||
|
||||
p.mu.Lock()
|
||||
if systemPrompt != "" {
|
||||
p.systemPrompt = systemPrompt
|
||||
}
|
||||
// Track only the user input in history
|
||||
p.messages = append(p.messages, map[string]interface{}{
|
||||
"role": "user",
|
||||
"content": userInput,
|
||||
})
|
||||
if userInput != "" {
|
||||
p.messages = append(p.messages, map[string]interface{}{
|
||||
"role": "user",
|
||||
"content": userInput,
|
||||
})
|
||||
}
|
||||
// Sliding window: keep only the last N messages (negative = unlimited)
|
||||
maxHistory := getMaxHistoryMessages()
|
||||
if maxHistory > 0 && len(p.messages) > maxHistory {
|
||||
@@ -173,7 +181,10 @@ func (p *openRouterProvider) Send(ctx context.Context, prompt string) (string, e
|
||||
copy(messages, p.messages)
|
||||
p.mu.Unlock()
|
||||
|
||||
systemMsg := buildSystemWorkingDirMessage(prompt)
|
||||
systemMsg := p.systemPrompt
|
||||
if systemMsg == "" {
|
||||
systemMsg = buildSystemWorkingDirMessage(prompt)
|
||||
}
|
||||
if systemMsg != "" {
|
||||
messages = prependSystemMessage(messages, systemMsg)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
package providers
|
||||
|
||||
import "strings"
|
||||
|
||||
const (
|
||||
userRequestMarker = "User request:"
|
||||
userAskedMarker = "User asked:"
|
||||
)
|
||||
|
||||
func splitSystemUserPrompt(prompt string) (string, string) {
|
||||
prompt = strings.TrimSpace(prompt)
|
||||
if prompt == "" {
|
||||
return "", ""
|
||||
}
|
||||
|
||||
idx := -1
|
||||
marker := ""
|
||||
if i := strings.Index(prompt, userRequestMarker); i != -1 {
|
||||
idx = i
|
||||
marker = userRequestMarker
|
||||
}
|
||||
if i := strings.Index(prompt, userAskedMarker); i != -1 && (idx == -1 || i < idx) {
|
||||
idx = i
|
||||
marker = userAskedMarker
|
||||
}
|
||||
if idx == -1 {
|
||||
return "", prompt
|
||||
}
|
||||
|
||||
system := strings.TrimSpace(prompt[:idx])
|
||||
user := strings.TrimSpace(prompt[idx+len(marker):])
|
||||
return system, user
|
||||
}
|
||||
Reference in new issue
Block a user