mirror of
https://github.com/YspCoder/clawgo.git
synced 2026-05-19 06:07:28 +08:00
introduce stable session ids and richer sessions index metadata
This commit is contained in:
@@ -2,6 +2,8 @@ package session
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
|
"crypto/sha1"
|
||||||
|
"encoding/hex"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@@ -14,6 +16,7 @@ import (
|
|||||||
|
|
||||||
type Session struct {
|
type Session struct {
|
||||||
Key string `json:"key"`
|
Key string `json:"key"`
|
||||||
|
SessionID string `json:"session_id,omitempty"`
|
||||||
Kind string `json:"kind,omitempty"`
|
Kind string `json:"kind,omitempty"`
|
||||||
Messages []providers.Message `json:"messages"`
|
Messages []providers.Message `json:"messages"`
|
||||||
Summary string `json:"summary,omitempty"`
|
Summary string `json:"summary,omitempty"`
|
||||||
@@ -76,11 +79,12 @@ func (sm *SessionManager) GetOrCreate(key string) *Session {
|
|||||||
}
|
}
|
||||||
|
|
||||||
session = &Session{
|
session = &Session{
|
||||||
Key: key,
|
Key: key,
|
||||||
Kind: detectSessionKind(key),
|
SessionID: deriveSessionID(key),
|
||||||
Messages: []providers.Message{},
|
Kind: detectSessionKind(key),
|
||||||
Created: time.Now(),
|
Messages: []providers.Message{},
|
||||||
Updated: time.Now(),
|
Created: time.Now(),
|
||||||
|
Updated: time.Now(),
|
||||||
}
|
}
|
||||||
sm.sessions[key] = session
|
sm.sessions[key] = session
|
||||||
|
|
||||||
@@ -240,6 +244,7 @@ func (sm *SessionManager) Save(session *Session) error {
|
|||||||
|
|
||||||
metaPath := filepath.Join(sm.storage, session.Key+".meta")
|
metaPath := filepath.Join(sm.storage, session.Key+".meta")
|
||||||
meta := map[string]interface{}{
|
meta := map[string]interface{}{
|
||||||
|
"session_id": session.SessionID,
|
||||||
"kind": session.Kind,
|
"kind": session.Kind,
|
||||||
"summary": session.Summary,
|
"summary": session.Summary,
|
||||||
"last_language": session.LastLanguage,
|
"last_language": session.LastLanguage,
|
||||||
@@ -278,6 +283,7 @@ func (sm *SessionManager) List(limit int) []Session {
|
|||||||
s.mu.RLock()
|
s.mu.RLock()
|
||||||
items = append(items, Session{
|
items = append(items, Session{
|
||||||
Key: s.Key,
|
Key: s.Key,
|
||||||
|
SessionID: s.SessionID,
|
||||||
Kind: s.Kind,
|
Kind: s.Kind,
|
||||||
Summary: s.Summary,
|
Summary: s.Summary,
|
||||||
LastLanguage: s.LastLanguage,
|
LastLanguage: s.LastLanguage,
|
||||||
@@ -352,6 +358,13 @@ func fromJSONLLine(line []byte) (providers.Message, bool) {
|
|||||||
return providers.Message{Role: role, Content: content, ToolCallID: event.Message.ToolCallID}, true
|
return providers.Message{Role: role, Content: content, ToolCallID: event.Message.ToolCallID}, true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func deriveSessionID(key string) string {
|
||||||
|
sum := sha1.Sum([]byte("clawgo-session:" + key))
|
||||||
|
h := hex.EncodeToString(sum[:])
|
||||||
|
// UUID-like deterministic id
|
||||||
|
return h[0:8] + "-" + h[8:12] + "-" + h[12:16] + "-" + h[16:20] + "-" + h[20:32]
|
||||||
|
}
|
||||||
|
|
||||||
func detectSessionKind(key string) string {
|
func detectSessionKind(key string) string {
|
||||||
k := strings.TrimSpace(strings.ToLower(key))
|
k := strings.TrimSpace(strings.ToLower(key))
|
||||||
switch {
|
switch {
|
||||||
@@ -380,11 +393,17 @@ func (sm *SessionManager) writeOpenClawSessionsIndex() error {
|
|||||||
for key, s := range sm.sessions {
|
for key, s := range sm.sessions {
|
||||||
s.mu.RLock()
|
s.mu.RLock()
|
||||||
sessionFile := filepath.Join(sm.storage, key+".jsonl")
|
sessionFile := filepath.Join(sm.storage, key+".jsonl")
|
||||||
|
sid := strings.TrimSpace(s.SessionID)
|
||||||
|
if sid == "" {
|
||||||
|
sid = deriveSessionID(key)
|
||||||
|
}
|
||||||
entry := map[string]interface{}{
|
entry := map[string]interface{}{
|
||||||
"sessionId": key,
|
"sessionId": sid,
|
||||||
"updatedAt": s.Updated.UnixMilli(),
|
"sessionKey": key,
|
||||||
"chatType": mapKindToChatType(s.Kind),
|
"updatedAt": s.Updated.UnixMilli(),
|
||||||
|
"chatType": mapKindToChatType(s.Kind),
|
||||||
"sessionFile": sessionFile,
|
"sessionFile": sessionFile,
|
||||||
|
"kind": s.Kind,
|
||||||
}
|
}
|
||||||
s.mu.RUnlock()
|
s.mu.RUnlock()
|
||||||
index[key] = entry
|
index[key] = entry
|
||||||
@@ -447,6 +466,7 @@ func (sm *SessionManager) loadSessions() error {
|
|||||||
data, err := os.ReadFile(filepath.Join(sm.storage, file.Name()))
|
data, err := os.ReadFile(filepath.Join(sm.storage, file.Name()))
|
||||||
if err == nil {
|
if err == nil {
|
||||||
var meta struct {
|
var meta struct {
|
||||||
|
SessionID string `json:"session_id"`
|
||||||
Kind string `json:"kind"`
|
Kind string `json:"kind"`
|
||||||
Summary string `json:"summary"`
|
Summary string `json:"summary"`
|
||||||
LastLanguage string `json:"last_language"`
|
LastLanguage string `json:"last_language"`
|
||||||
@@ -455,6 +475,10 @@ func (sm *SessionManager) loadSessions() error {
|
|||||||
Created time.Time `json:"created"`
|
Created time.Time `json:"created"`
|
||||||
}
|
}
|
||||||
if err := json.Unmarshal(data, &meta); err == nil {
|
if err := json.Unmarshal(data, &meta); err == nil {
|
||||||
|
session.SessionID = strings.TrimSpace(meta.SessionID)
|
||||||
|
if session.SessionID == "" {
|
||||||
|
session.SessionID = deriveSessionID(session.Key)
|
||||||
|
}
|
||||||
session.Kind = meta.Kind
|
session.Kind = meta.Kind
|
||||||
if strings.TrimSpace(session.Kind) == "" {
|
if strings.TrimSpace(session.Kind) == "" {
|
||||||
session.Kind = detectSessionKind(session.Key)
|
session.Kind = detectSessionKind(session.Key)
|
||||||
|
|||||||
Reference in New Issue
Block a user