ResolveSystemPrompt: expand env, fall back to embedded by filename
This commit is contained in:
parent
86880feb3a
commit
77aabfbe04
|
|
@ -5,6 +5,7 @@ import (
|
||||||
_ "embed"
|
_ "embed"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
@ -21,6 +22,13 @@ var SessionDBus string
|
||||||
//go:embed SYSTEM_PROMPT_QWEN.md
|
//go:embed SYSTEM_PROMPT_QWEN.md
|
||||||
var QwenSystemPrompt string
|
var QwenSystemPrompt string
|
||||||
|
|
||||||
|
// embeddedPromptByFile maps source filenames (without .md) to embedded prompt
|
||||||
|
// content, so ResolveSystemPrompt can fall back to embedded prompts when the
|
||||||
|
// filesystem path doesn't resolve.
|
||||||
|
var embeddedPromptByFile = map[string]string{
|
||||||
|
"SYSTEM_PROMPT_QWEN": QwenSystemPrompt,
|
||||||
|
}
|
||||||
|
|
||||||
// Environment renders the environment context block with live values.
|
// Environment renders the environment context block with live values.
|
||||||
func Environment(cwd, platform string, isGitRepo bool, sandbox string) string {
|
func Environment(cwd, platform string, isGitRepo bool, sandbox string) string {
|
||||||
date := time.Now().Format("2006-01-02")
|
date := time.Now().Format("2006-01-02")
|
||||||
|
|
@ -60,21 +68,27 @@ func OperationalModel(enable9P, enableDBus bool, envVars map[string]string) stri
|
||||||
return strings.Join(parts, "\n")
|
return strings.Join(parts, "\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
// ResolveSystemPrompt reads from overridePath if non-empty, otherwise returns
|
// ResolveSystemPrompt expands env vars in overridePath, then tries to read the
|
||||||
// the embedded default. If overridePath is a known key ("qwen", "devstral"),
|
// file from the filesystem. If the file doesn't exist, it checks whether the
|
||||||
// the corresponding embedded prompt is returned instead.
|
// filename matches an embedded prompt (e.g. "SYSTEM_PROMPT_QWEN.md" -> Qwen
|
||||||
|
// system prompt). If no match is found, the embedded default is returned.
|
||||||
func ResolveSystemPrompt(overridePath string) string {
|
func ResolveSystemPrompt(overridePath string) string {
|
||||||
switch overridePath {
|
if overridePath == "" {
|
||||||
case "qwen":
|
return DefaultSystemPrompt
|
||||||
return QwenSystemPrompt
|
|
||||||
}
|
}
|
||||||
if overridePath != "" {
|
expanded := os.Expand(overridePath, os.Getenv)
|
||||||
data, err := os.ReadFile(overridePath)
|
data, err := os.ReadFile(expanded)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return strings.TrimRight(string(data), "\n")
|
return strings.TrimRight(string(data), "\n")
|
||||||
|
}
|
||||||
|
// File not found — check embedded prompts by basename.
|
||||||
|
base := filepath.Base(expanded)
|
||||||
|
if key := strings.TrimSuffix(base, filepath.Ext(base)); key != "" {
|
||||||
|
if content, ok := embeddedPromptByFile[key]; ok {
|
||||||
|
return content
|
||||||
}
|
}
|
||||||
// Fall through to embedded default if file can't be read.
|
|
||||||
fmt.Fprintf(os.Stderr, "system_prompt override %q: %v (using default)\n", overridePath, err)
|
|
||||||
}
|
}
|
||||||
|
// Fall through to embedded default.
|
||||||
|
fmt.Fprintf(os.Stderr, "system_prompt %q: %v (using default)\n", expanded, err)
|
||||||
return DefaultSystemPrompt
|
return DefaultSystemPrompt
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Reference in New Issue