feat(agent): 新增通用 {{.Now}} 提示词变量(每轮实时渲染)
- 各 agent 渲染路径(chat/worker/mainagent/goals/planner)统一提供 Now(nowStr());
自定义会话 agent 每次触发都会重新 renderSystem,故 {{.Now}} 每轮实时刷新
- server 端 globalPromptVars + withGlobalVars:在保存校验/变量列表/预览/详情放行
{{.Now}},否则引用它的自定义提示词会被 validateTemplate 拒绝
- 无 DB 迁移(handler 层注入通用变量,不动 agent_prompt_vars 表)
- 用途:配合固定的开始时间戳相减,判断"已运行多久/是否进入最后N小时"
- 新增 agent/prompt_now_test.go:{{.Now}} 渲染实时时间、未知变量安全回退
This commit is contained in:
1 parent
7afff7e5f5
commit
7064216188
8 files changed
+86
-14
No files matched your search
@@ -0,0 +1,50 @@
|
||||
package agent
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// TestChatNowVarRenders verifies the universal {{.Now}} runtime variable: a custom
|
||||
// (chat) agent prompt referencing it renders the live server time each turn, rather
|
||||
// than failing template execution and falling back to DefaultAssistantPrompt.
|
||||
func TestChatNowVarRenders(t *testing.T) {
|
||||
prev := PromptOverride
|
||||
defer func() { PromptOverride = prev }()
|
||||
|
||||
PromptOverride = func(key string) (string, bool) {
|
||||
if key == "tec_benchmark" {
|
||||
return "当前时间:{{.Now}}", true
|
||||
}
|
||||
return "", false
|
||||
}
|
||||
|
||||
out := chatSystem("tec_benchmark", "/tmp/x")
|
||||
if !strings.Contains(out, "当前时间:") {
|
||||
t.Fatalf("custom prompt body missing, likely fell back to default: %q", out)
|
||||
}
|
||||
year := time.Now().Format("2006")
|
||||
if !strings.Contains(out, year) {
|
||||
t.Fatalf("{{.Now}} did not render the live time (want year %s): %q", year, out)
|
||||
}
|
||||
}
|
||||
|
||||
// TestChatUnknownVarFallsBack verifies an out-of-catalog {{.X}} still degrades
|
||||
// safely to the default assistant prompt (never a half-rendered prompt).
|
||||
func TestChatUnknownVarFallsBack(t *testing.T) {
|
||||
prev := PromptOverride
|
||||
defer func() { PromptOverride = prev }()
|
||||
|
||||
PromptOverride = func(key string) (string, bool) {
|
||||
return "引用了不存在的变量:{{.Bogus}}", true
|
||||
}
|
||||
|
||||
out := chatSystem("whatever", "/tmp/x")
|
||||
if strings.Contains(out, "引用了不存在的变量") {
|
||||
t.Fatalf("broken template should have fallen back, got custom body: %q", out)
|
||||
}
|
||||
if !strings.Contains(out, DefaultAssistantPrompt) {
|
||||
t.Fatalf("expected fallback to DefaultAssistantPrompt, got: %q", out)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user