* Add German translation and harden paths against unset env vars Add a German locale to the GUI, tray menu, usage alerts and website home page, and refuse to start when the home folder is unset or relative so nothing is written under the working folder; the pi plugin no longer adopts the user's PI_CODING_AGENT_DIR, and an inherited APPIMAGE no longer selects portable mode. Scripts refuse to run from the wrong folder or empty a shared out folder, and a new internal/testenv sandboxes every test package from the user's real files, which also fixes the Claude picker and Termux installer tests on Windows. * Makefile: know magpie's folder by its go.mod, so paths with spaces work The check compared $(CURDIR) with $(dir $(abspath …)), which make splits on spaces, so a checkout under 'My Projects' or 'John Doe' refused every target. It now reads go.mod relative to the working folder and wants magpie's module there: make -C and a symlinked Makefile work, make -f from another folder still stops before any recipe runs. * i18n: German for the strings main added since, with a parity test 75 strings main added (group order, patterns, slow-start wait, usage caps, RTK savings …) get their German, and two that main dropped go. gui-de.test.cjs checks, as gui-ja does, that de has every zh string with its placeholders and tags, and that the window reads in German when picked or on a German system. * appdir: ignore relative folder variables when reading, keep them for children CheckEnv no longer unsets anything. magpie reads the XDG folders, APPDATA/LOCALAPPDATA and every agent's folder variable through appdir.Getenv/LookupEnv, which pass over a relative value; the environment stays as it is, so the agents magpie starts get the variable as the user set it. That covers what proc.UserPath lends from the login shell too, which is set after main's check and reached the desktop app unchecked. On Windows a path rooted on the current drive (\Users\x, Git Bash's /c/x) counts as usable, and magpie version and help answer without a home, before run makes any folder. * Sandbox the tests main added since internal/wslrun gets testenv's TestMain, and four tests main added that set HOME alone set their home with testenv.SetHome, so on Windows they don't read the runner's profile through USERPROFILE. * agentenv: PI_CONFIG_DIR and OPENCODE_DB take relative names on purpose omp puts $PI_CONFIG_DIR under the home and OpenCode a relative $OPENCODE_DB in its data folder, so neither depends on the working folder: appdir no longer passes them over. * i18n: German for the Library's multi-select and System One's decision API The 19 strings main added since (#791's skill multi-select, the decision API snippets) get their German; the one main replaced (four APIs → five APIs) goes. * appdir: tidy the import into magpie's group; say what Go makes of /c/x The appdir import joins the group of magpie's own imports where a file has one. rooted's comment now says that Go takes a Git Bash path like /c/x as \c\x on the current drive, not C:\x.
161 lines
4.8 KiB
Go
161 lines
4.8 KiB
Go
package sessions
|
|
|
|
import (
|
|
"encoding/json"
|
|
"os"
|
|
"path/filepath"
|
|
"regexp"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/appdir"
|
|
)
|
|
|
|
// Cline's CLI (3, on the Cline SDK) keeps a session in a folder of its own
|
|
// under its sessions folder (~/.cline/data/sessions): <id>.json, the
|
|
// manifest — the cwd, the time started, the first prompt, the title in its
|
|
// metadata — and <id>.messages.json, the conversation, written anew whole
|
|
// as it goes on. A subagent's is <agent id>.messages.json in the folder of
|
|
// the session it ran in. An assistant message carries its model
|
|
// (modelInfo.id), its time (ts, milliseconds) and its usage (metrics): the
|
|
// AI SDK's counts, input with the cache in it, output with the reasoning.
|
|
// A session forked from another (metadata.fork) starts with a copy of that
|
|
// one's messages, which count where they were first written: only those
|
|
// from its forkedAt on count in it. `cline --id <id>` picks one up again.
|
|
|
|
// ClineSessionDir is Cline's sessions folder: $CLINE_SESSION_DATA_DIR, else
|
|
// sessions in $CLINE_DATA_DIR, else in data in $CLINE_DIR, else in
|
|
// ~/.cline/data.
|
|
func ClineSessionDir() string {
|
|
if d := strings.TrimSpace(appdir.Getenv("CLINE_SESSION_DATA_DIR")); d != "" {
|
|
return d
|
|
}
|
|
data := strings.TrimSpace(appdir.Getenv("CLINE_DATA_DIR"))
|
|
if data == "" {
|
|
dir := strings.TrimSpace(appdir.Getenv("CLINE_DIR"))
|
|
if dir == "" {
|
|
home, _ := os.UserHomeDir()
|
|
dir = filepath.Join(home, ".cline")
|
|
}
|
|
data = filepath.Join(dir, "data")
|
|
}
|
|
return filepath.Join(data, "sessions")
|
|
}
|
|
|
|
func clineFiles() []file {
|
|
paths, _ := filepath.Glob(filepath.Join(ClineSessionDir(), "*", "*.messages.json"))
|
|
var out []file
|
|
for _, p := range paths {
|
|
id := filepath.Base(filepath.Dir(p))
|
|
f := file{agent: "cline", key: "cline:" + id, path: p, main: filepath.Base(p) == id+".messages.json"}
|
|
if !stat(&f) {
|
|
continue
|
|
}
|
|
if f.main {
|
|
// its title and its fork are in the manifest, beside it
|
|
m := file{path: filepath.Join(filepath.Dir(p), id+".json")}
|
|
if stat(&m) {
|
|
f.manifest = m.path
|
|
f.size += m.size
|
|
if m.mod.After(f.mod) {
|
|
f.mod = m.mod
|
|
}
|
|
}
|
|
}
|
|
out = append(out, f)
|
|
}
|
|
return out
|
|
}
|
|
|
|
type clineManifest struct {
|
|
SessionID string `json:"session_id"`
|
|
StartedAt string `json:"started_at"`
|
|
Cwd string `json:"cwd"`
|
|
Prompt string `json:"prompt"`
|
|
Metadata struct {
|
|
Title string `json:"title"`
|
|
Fork *struct {
|
|
ForkedAt string `json:"forkedAt"`
|
|
} `json:"fork"`
|
|
} `json:"metadata"`
|
|
}
|
|
|
|
type clineMessage struct {
|
|
Role string `json:"role"`
|
|
Content json.RawMessage `json:"content"`
|
|
TS int64 `json:"ts"`
|
|
ModelInfo *struct {
|
|
ID string `json:"id"`
|
|
} `json:"modelInfo"`
|
|
Metrics *struct {
|
|
Input int `json:"inputTokens"`
|
|
Output int `json:"outputTokens"`
|
|
CacheRead int `json:"cacheReadTokens"`
|
|
CacheWrite int `json:"cacheWriteTokens"`
|
|
} `json:"metrics"`
|
|
}
|
|
|
|
// parseCline reads a session's messages whole, as they are written.
|
|
func parseCline(f file) *state {
|
|
s := &state{Size: f.size, Mod: f.mod.UnixNano()}
|
|
var since time.Time
|
|
if f.manifest != "" {
|
|
var m clineManifest
|
|
if b, err := os.ReadFile(f.manifest); err == nil && json.Unmarshal(b, &m) == nil {
|
|
s.ID, s.Cwd = m.SessionID, m.Cwd
|
|
s.Named = title(m.Metadata.Title)
|
|
if m.Metadata.Fork != nil {
|
|
since, _ = time.Parse(time.RFC3339Nano, m.Metadata.Fork.ForkedAt)
|
|
}
|
|
start, _ := time.Parse(time.RFC3339Nano, m.StartedAt)
|
|
if since.IsZero() || !start.Before(since) {
|
|
s.saw(start, f.main)
|
|
}
|
|
clineTitle(s, m.Prompt)
|
|
}
|
|
}
|
|
b, err := os.ReadFile(f.path)
|
|
if err != nil {
|
|
return s
|
|
}
|
|
var doc struct {
|
|
Messages []clineMessage `json:"messages"`
|
|
}
|
|
if json.Unmarshal(b, &doc) != nil {
|
|
return s
|
|
}
|
|
for _, m := range doc.Messages {
|
|
if f.main && m.Role == "user" && s.Title == "" && s.First == "" {
|
|
clineTitle(s, ccText(m.Content))
|
|
}
|
|
at := ms(m.TS)
|
|
if !since.IsZero() && at.Before(since) {
|
|
continue // the copy of the session it was forked from
|
|
}
|
|
s.saw(at, f.main)
|
|
if u := m.Metrics; m.Role == "assistant" && u != nil && m.ModelInfo != nil && m.ModelInfo.ID != "" {
|
|
s.use(dateOf(at), m.ModelInfo.ID, Tokens{Input: max(0, u.Input-u.CacheRead-u.CacheWrite), Output: u.Output, CacheRead: u.CacheRead, CacheWrite: u.CacheWrite})
|
|
}
|
|
}
|
|
return s
|
|
}
|
|
|
|
var (
|
|
clineNotice = regexp.MustCompile(`(?s)<mode_notice\b[^>]*>.*?</mode_notice>`)
|
|
clineInput = regexp.MustCompile(`</?(?:user_input|user_command)\b[^>]*>`)
|
|
)
|
|
|
|
// clineTitle takes a session's title from a prompt: the words typed, out of
|
|
// the tags Cline wraps them in (<user_input mode="act">) and its notices.
|
|
func clineTitle(s *state, prompt string) {
|
|
if s.Title != "" || s.First != "" {
|
|
return
|
|
}
|
|
t := strings.TrimSpace(clineInput.ReplaceAllString(clineNotice.ReplaceAllString(prompt, " "), " "))
|
|
if strings.HasPrefix(t, "<") {
|
|
s.First = untagged(t)
|
|
} else {
|
|
s.Title = title(t)
|
|
}
|
|
}
|