Files
ARTEX/third_party/norma/examples/planner-memory/main.go
T
wangdefa f782b612ec build: 内置 norma 模块,构建不再依赖上游仓库
把 github.com/Autumn-27/norma v0.4.3(commit 320ae32)的源码复制到
third_party/norma,并用 go.mod 的 replace 指向本地副本。上游删除仓库或
改写 tag 都不再影响本仓库构建。

- 已验证 GOPROXY=off 下可完整编译并打出发行包
- go.sum 中该模块的两条校验记录随之删除(本地 replace 不需要哈希)
- 升级方法与授权注意事项见 third_party/README.md
2026-10-20 21:10:41 -07:00

101 lines
2.8 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Example planner-memory shows the Phase-4 features: plan mode (the agent
// explores read-only, presents a plan for approval, then implements) and
// persistent memory (relevant memories are auto-injected, and the agent can
// record durable facts). Both layer onto the same Session with no change to the
// core loop.
package main
import (
"bufio"
"context"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/Autumn-27/norma/agentcore"
"github.com/Autumn-27/norma/harness"
"github.com/Autumn-27/norma/llm"
"github.com/Autumn-27/norma/memory"
"github.com/Autumn-27/norma/permission"
)
func main() {
model := envOr("AGENT_MODEL", "claude-opus-4-8")
prov, err := llm.NewProvider(llm.Config{Format: llm.FormatAnthropic, Model: model})
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
wd, _ := os.Getwd()
// Memory persisted under the working directory.
store := memory.NewStore(filepath.Join(wd, ".agent-memory"))
sess := agentcore.NewSession(agentcore.Options{
Provider: prov,
SystemPrompt: []string{"You are a coding agent. Plan before you build; record durable facts (preferences, project constraints) to memory."},
WorkingDir: wd,
MaxTurns: 50,
// Plan mode: start read-only; ask the terminal to approve the plan.
Plan: &agentcore.PlanOptions{
StartInPlanMode: true,
TargetMode: permission.ModeAcceptEdits,
Approver: approvePlan,
},
// Memory: inject relevant memories per prompt and expose the index.
Memory: &agentcore.MemoryOptions{
Store: store,
AutoInject: true,
IncludeIndexInPrompt: true,
},
})
fmt.Printf("planner-memory (%s) — plan mode + memory. Ctrl-D to quit.\n", model)
in := bufio.NewScanner(os.Stdin)
for {
fmt.Print("\n› ")
if !in.Scan() {
return
}
line := strings.TrimSpace(in.Text())
if line == "" {
continue
}
for ev, err := range sess.Prompt(context.Background(), line) {
if err != nil {
fmt.Fprintf(os.Stderr, "[error: %v]\n", err)
break
}
switch ev.Kind {
case harness.KindText:
fmt.Print(ev.Text)
case harness.KindToolUse:
fmt.Printf("\n· %s\n", ev.ToolUse.Name)
case harness.KindResult:
fmt.Println()
}
}
}
}
// approvePlan prints the plan and asks for terminal approval.
func approvePlan(_ context.Context, plan string) (bool, string) {
fmt.Printf("\n--- Proposed plan ---\n%s\n---------------------\nApprove this plan? [y/N] ", plan)
r := bufio.NewReader(os.Stdin)
line, _ := r.ReadString('\n')
if s := strings.ToLower(strings.TrimSpace(line)); s == "y" || s == "yes" {
return true, ""
}
fmt.Print("Feedback (optional): ")
fb, _ := r.ReadString('\n')
return false, strings.TrimSpace(fb)
}
func envOr(k, def string) string {
if v := os.Getenv(k); v != "" {
return v
}
return def
}