ResolveSystemPrompt: expand env, fall back to embedded by filename

This commit is contained in:
lkn 2026-07-27 20:31:50 +02:00
parent 86880feb3a
commit 77aabfbe04
1 changed files with 26 additions and 12 deletions

View File

@ -5,6 +5,7 @@ import (
_ "embed"
"fmt"
"os"
"path/filepath"
"strings"
"time"
)
@ -21,6 +22,13 @@ var SessionDBus string
//go:embed SYSTEM_PROMPT_QWEN.md
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.
func Environment(cwd, platform string, isGitRepo bool, sandbox string) string {
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")
}
// ResolveSystemPrompt reads from overridePath if non-empty, otherwise returns
// the embedded default. If overridePath is a known key ("qwen", "devstral"),
// the corresponding embedded prompt is returned instead.
// ResolveSystemPrompt expands env vars in overridePath, then tries to read the
// file from the filesystem. If the file doesn't exist, it checks whether the
// 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 {
switch overridePath {
case "qwen":
return QwenSystemPrompt
if overridePath == "" {
return DefaultSystemPrompt
}
if overridePath != "" {
data, err := os.ReadFile(overridePath)
if err == nil {
return strings.TrimRight(string(data), "\n")
expanded := os.Expand(overridePath, os.Getenv)
data, err := os.ReadFile(expanded)
if err == nil {
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
}