refactor(gemini-web): Consolidate conversation data into single BoltDB file

This commit is contained in:
hkfires
2025-09-25 09:40:27 +08:00
parent 9810834f20
commit 7f2e2fee56
2 changed files with 13 additions and 32 deletions

View File

@@ -420,13 +420,10 @@ func (c *GeminiClient) generateOnce(prompt string, files []string, model Model,
form.Set("f.req", string(outerJSON))
req, _ := http.NewRequest(http.MethodPost, EndpointGenerate, strings.NewReader(form.Encode()))
// headers
applyHeaders(req, HeadersGemini)
applyHeaders(req, model.ModelHeader)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded;charset=utf-8")
for k, v := range c.Cookies {
req.AddCookie(&http.Cookie{Name: k, Value: v})
}
applyCookies(req, c.Cookies)
resp, err := c.httpClient.Do(req)
if err != nil {

View File

@@ -82,12 +82,12 @@ func NewGeminiWebState(cfg *config.Config, token *gemini.GeminiWebTokenStorage,
}
func (s *GeminiWebState) loadConversationCaches() {
if path := s.convStorePath(); path != "" {
if path := s.convPath(); path != "" {
if store, err := LoadConvStore(path); err == nil {
s.convStore = store
}
}
if path := s.convDataPath(); path != "" {
if path := s.convPath(); path != "" {
if items, index, err := LoadConvData(path); err == nil {
s.convData = items
s.convIndex = index
@@ -95,20 +95,14 @@ func (s *GeminiWebState) loadConversationCaches() {
}
}
func (s *GeminiWebState) convStorePath() string {
// convPath returns the BoltDB file path used for both account metadata and conversation data.
func (s *GeminiWebState) convPath() string {
base := s.storagePath
if base == "" {
base = s.accountID + ".json"
// Use accountID directly as base name; ConvBoltPath will append .bolt.
base = s.accountID
}
return ConvStorePath(base)
}
func (s *GeminiWebState) convDataPath() string {
base := s.storagePath
if base == "" {
base = s.accountID + ".json"
}
return ConvDataPath(base)
return ConvBoltPath(base)
}
func (s *GeminiWebState) GetRequestMutex() *sync.Mutex { return &s.reqMu }
@@ -392,7 +386,7 @@ func (s *GeminiWebState) persistConversation(modelName string, prep *geminiWebPr
storeSnapshot[k] = cp
}
s.convMu.Unlock()
_ = SaveConvStore(s.convStorePath(), storeSnapshot)
_ = SaveConvStore(s.convPath(), storeSnapshot)
}
if !s.useReusableContext() {
@@ -420,7 +414,7 @@ func (s *GeminiWebState) persistConversation(modelName string, prep *geminiWebPr
indexSnapshot[k] = v
}
s.convMu.Unlock()
_ = SaveConvData(s.convDataPath(), dataSnapshot, indexSnapshot)
_ = SaveConvData(s.convPath(), dataSnapshot, indexSnapshot)
}
func (s *GeminiWebState) addAPIResponseData(ctx context.Context, line []byte) {
@@ -557,19 +551,9 @@ func HashConversation(clientID, model string, msgs []StoredMessage) string {
return Sha256Hex(b.String())
}
// ConvStorePath returns the path for account-level metadata persistence based on token file path.
func ConvStorePath(tokenFilePath string) string {
wd, err := os.Getwd()
if err != nil || wd == "" {
wd = "."
}
convDir := filepath.Join(wd, "conv")
base := strings.TrimSuffix(filepath.Base(tokenFilePath), filepath.Ext(tokenFilePath))
return filepath.Join(convDir, base+".bolt")
}
// ConvDataPath returns the path for full conversation persistence based on token file path.
func ConvDataPath(tokenFilePath string) string {
// ConvBoltPath returns the BoltDB file path used for both account metadata and conversation data.
// Different logical datasets are kept in separate buckets within this single DB file.
func ConvBoltPath(tokenFilePath string) string {
wd, err := os.Getwd()
if err != nil || wd == "" {
wd = "."