feat: Allow using Gemini Web without login
- Remove requirement to login before using Gemini Web - If logged in (cookies exist), use the account - If not logged in, continue without login (browser opens) - When browser is visible, prompt user they can login or continue without - Save cookies only if user actually logs in
This commit is contained in:
parent
69df57555d
commit
9f41d3b5b5
|
|
@ -49,17 +49,31 @@ func (p *Provider) Generate(ctx context.Context, model string, messages []apityp
|
|||
return err
|
||||
}
|
||||
|
||||
// 檢查是否有可用的已登入 session
|
||||
session := p.pool.GetAvailable()
|
||||
if session == nil {
|
||||
return fmt.Errorf("no available sessions - please run 'gemini-login <session-name>' first")
|
||||
}
|
||||
needLogin := false
|
||||
|
||||
fmt.Printf("[GeminiWeb] Using session: %s\n", session.Name)
|
||||
if session == nil {
|
||||
// 沒有 session,建立一個新的
|
||||
fmt.Printf("[GeminiWeb] No existing session found, creating new session...\n")
|
||||
var err error
|
||||
session, err = p.pool.CreateSession(fmt.Sprintf("session-%d", time.Now().Unix()))
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create session: %w", err)
|
||||
}
|
||||
needLogin = true
|
||||
fmt.Printf("[GeminiWeb] Created new session: %s\n", session.Name)
|
||||
} else {
|
||||
fmt.Printf("[GeminiWeb] Using existing session: %s\n", session.Name)
|
||||
}
|
||||
|
||||
p.pool.StartSession(session)
|
||||
defer p.pool.EndSession(session)
|
||||
|
||||
browser, err := NewBrowser(p.cfg.GeminiBrowserVisible)
|
||||
// 如果沒有登入過,強制使用可見瀏覽器
|
||||
visible := p.cfg.GeminiBrowserVisible || needLogin
|
||||
|
||||
browser, err := NewBrowser(visible)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create browser: %w", err)
|
||||
}
|
||||
|
|
@ -70,16 +84,18 @@ func (p *Provider) Generate(ctx context.Context, model string, messages []apityp
|
|||
return fmt.Errorf("failed to create page: %w", err)
|
||||
}
|
||||
|
||||
// 嘗試載入 cookies
|
||||
if session.CookieFile != "" {
|
||||
fmt.Printf("[GeminiWeb] Loading cookies from: %s\n", session.CookieFile)
|
||||
cookies, err := LoadCookiesFromFile(session.CookieFile)
|
||||
if err == nil {
|
||||
if err := SetCookiesOnPage(page, cookies); err != nil {
|
||||
return fmt.Errorf("failed to set cookies: %w", err)
|
||||
}
|
||||
fmt.Printf("[GeminiWeb] Loaded %d cookies\n", len(cookies))
|
||||
fmt.Printf("[GeminiWeb] Warning: failed to set cookies: %v\n", err)
|
||||
} else {
|
||||
fmt.Printf("[GeminiWeb] Warning: could not load cookies: %v\n", err)
|
||||
fmt.Printf("[GeminiWeb] Loaded %d cookies\n", len(cookies))
|
||||
}
|
||||
} else {
|
||||
fmt.Printf("[GeminiWeb] No existing cookies found\n")
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -91,10 +107,42 @@ func (p *Provider) Generate(ctx context.Context, model string, messages []apityp
|
|||
time.Sleep(2 * time.Second)
|
||||
|
||||
fmt.Printf("[GeminiWeb] Checking login status...\n")
|
||||
if !IsLoggedIn(page) {
|
||||
return fmt.Errorf("session not logged in - please run 'gemini-login %s' first", session.Name)
|
||||
if IsLoggedIn(page) {
|
||||
fmt.Printf("[GeminiWeb] Logged in (using saved cookies)\n")
|
||||
} else {
|
||||
fmt.Printf("[GeminiWeb] Not logged in - continuing without login\n")
|
||||
|
||||
if visible {
|
||||
// 開啟可見瀏覽器,提示使用者可以登入
|
||||
fmt.Println("\n========================================")
|
||||
fmt.Println("Browser is now open. You can:")
|
||||
fmt.Println("1. Log in to Gemini (to use your account)")
|
||||
fmt.Println("2. Or continue without login (limited functionality)")
|
||||
fmt.Println("\nPress Enter when ready to continue...")
|
||||
fmt.Println("========================================\n")
|
||||
|
||||
// 等待使用者按 Enter
|
||||
var input string
|
||||
fmt.Scanln(&input)
|
||||
|
||||
// 檢查是否已登入
|
||||
if IsLoggedIn(page) {
|
||||
fmt.Printf("[GeminiWeb] Login detected! Saving cookies for future use...\n")
|
||||
cookies, err := GetPageCookies(page)
|
||||
if err != nil {
|
||||
fmt.Printf("[GeminiWeb] Warning: could not get cookies: %v\n", err)
|
||||
} else {
|
||||
if err := SaveCookiesToFile(cookies, session.CookieFile); err != nil {
|
||||
fmt.Printf("[GeminiWeb] Warning: could not save cookies: %v\n", err)
|
||||
} else {
|
||||
fmt.Printf("[GeminiWeb] Saved %d cookies to %s\n", len(cookies), session.CookieFile)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
fmt.Printf("[GeminiWeb] Continuing without login\n")
|
||||
}
|
||||
}
|
||||
}
|
||||
fmt.Printf("[GeminiWeb] Logged in successfully\n")
|
||||
|
||||
fmt.Printf("[GeminiWeb] Selecting model: %s\n", model)
|
||||
if err := SelectModel(page, model); err != nil {
|
||||
|
|
|
|||
Loading…
Reference in New Issue