feat: complete v0.3.1 agent workflow
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
package session
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
|
||||
"agentu/internal/safeio"
|
||||
)
|
||||
|
||||
const sessionIndexFile = "index.json"
|
||||
|
||||
type sessionIndex struct {
|
||||
Sessions []SessionMeta `json:"sessions"`
|
||||
}
|
||||
|
||||
func metaFromSession(sess *Session) SessionMeta {
|
||||
return SessionMeta{
|
||||
ID: sess.ID,
|
||||
Name: sess.Name,
|
||||
CreatedAt: sess.CreatedAt,
|
||||
UpdatedAt: sess.UpdatedAt,
|
||||
Provider: sess.Provider,
|
||||
Model: sess.Model,
|
||||
MessageCount: len(sess.Messages),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Store) saveIndexFromSessions(metas []SessionMeta) error {
|
||||
sort.Slice(metas, func(i, j int) bool {
|
||||
return metas[i].UpdatedAt.After(metas[j].UpdatedAt)
|
||||
})
|
||||
idx := sessionIndex{Sessions: metas}
|
||||
data, err := json.MarshalIndent(idx, "", " ")
|
||||
if err != nil {
|
||||
return fmt.Errorf("marshal session index: %w", err)
|
||||
}
|
||||
path := filepath.Join(s.dir, sessionIndexFile)
|
||||
return safeio.AtomicWriteFile(path, data, 0o644)
|
||||
}
|
||||
|
||||
func (s *Store) loadIndex() ([]SessionMeta, error) {
|
||||
path := filepath.Join(s.dir, sessionIndexFile)
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var idx sessionIndex
|
||||
if err := json.Unmarshal(data, &idx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
sort.Slice(idx.Sessions, func(i, j int) bool {
|
||||
return idx.Sessions[i].UpdatedAt.After(idx.Sessions[j].UpdatedAt)
|
||||
})
|
||||
return idx.Sessions, nil
|
||||
}
|
||||
Reference in New Issue
Block a user