fix(client): Add reason to unregistration to skip persistence

This commit is contained in:
hkfires
2025-09-18 20:58:43 +08:00
parent 8f0a345e2a
commit d9f8129a32
5 changed files with 70 additions and 15 deletions

View File

@@ -61,13 +61,28 @@ type GeminiWebClient struct {
modelsRegistered bool
}
func (c *GeminiWebClient) UnregisterClient() {
func (c *GeminiWebClient) UnregisterClient() { c.unregisterClient(false) }
// UnregisterClientWithReason allows the watcher to avoid recreating deleted auth files.
func (c *GeminiWebClient) UnregisterClientWithReason(reason interfaces.UnregisterReason) {
skipPersist := reason == interfaces.UnregisterReasonAuthFileRemoved
c.unregisterClient(skipPersist)
}
func (c *GeminiWebClient) unregisterClient(skipPersist bool) {
if c.cookiePersistCancel != nil {
c.cookiePersistCancel()
c.cookiePersistCancel = nil
}
// Flush cookie snapshot to main token file and remove snapshot
c.flushCookieSnapshotToMain()
if skipPersist {
if c.snapshotManager != nil && c.tokenFilePath != "" {
log.Debugf("skipping Gemini Web snapshot flush because auth file is missing: %s", filepath.Base(c.tokenFilePath))
util.RemoveCookieSnapshots(c.tokenFilePath)
}
} else {
// Flush cookie snapshot to main token file and remove snapshot
c.flushCookieSnapshotToMain()
}
if c.gwc != nil {
c.gwc.Close(0)
c.gwc = nil

View File

@@ -507,10 +507,25 @@ func (c *QwenClient) SetUnavailable() {
}
// UnregisterClient flushes cookie snapshot back into the main token file.
func (c *QwenClient) UnregisterClient() {
func (c *QwenClient) UnregisterClient() { c.unregisterClient(false) }
// UnregisterClientWithReason allows the watcher to skip persistence when the auth file is removed.
func (c *QwenClient) UnregisterClientWithReason(reason interfaces.UnregisterReason) {
skipPersist := reason == interfaces.UnregisterReasonAuthFileRemoved
c.unregisterClient(skipPersist)
}
func (c *QwenClient) unregisterClient(skipPersist bool) {
if c.snapshotManager == nil {
return
}
if skipPersist {
if c.tokenFilePath != "" {
log.Debugf("skipping Qwen snapshot flush because auth file is missing: %s", filepath.Base(c.tokenFilePath))
util.RemoveCookieSnapshots(c.tokenFilePath)
}
return
}
if err := c.snapshotManager.Flush(); err != nil {
log.Errorf("Failed to flush Qwen cookie snapshot to main for %s: %v", filepath.Base(c.tokenFilePath), err)
}