提交记录修改docs/guide/accounts.md+3 −1看文件
修改internal/workbench/server.go+1 −0看文件
新增internal/workbench/terminal_login_link.go+94 −0看文件
新增internal/workbench/terminal_login_link_test.go+58 −0看文件
修改internal/workbench/terminal_service.go+3 −0看文件
修改internal/workbench/terminal_session.go+3 −0看文件
修改ui/package-lock.json+7 −0看文件
修改ui/package.json+1 −0看文件
新增ui/scripts/terminal-login-link-smoke.mjs+36 −0看文件
修改ui/src/main.jsx+1 −0看文件
新增ui/src/terminal-login-link.mjs+8 −0看文件
修改ui/src/views.jsx+16 −1看文件
| @@ -22,7 +22,9 @@ keywords: 多账号 账号池 登录 加入调度 自动分配 接续 切换 分 | ||
| 22 | 22 | |
| 23 | 23 | ## 将已有登录加入调度 |
| 24 | 24 | |
| 25 | 删除:在 Cage 普通终端中完成 Claude Code 登录后,账号面板会显示默认终端检测到的邮箱。核对后点“加入调度”。它只读取 Cage 环境里的登录,不读取主机系统终端上的账号。 | |
| 25 | 新增:在 Cage 普通终端运行 `claude auth login`。如果终端显示 Claude 官方授权链接,点击它即可在 Cage 浏览器的临时标签中继续。按页面提示完成后,回到原终端确认登录结果。 | |
| 26 | 新增: | |
| 27 | 新增:登录成功后,账号面板会显示默认终端检测到的邮箱。核对后点“加入调度”。它只读取 Cage 环境里的登录,不读取主机系统终端上的账号。 | |
| 26 | 28 | |
| 27 | 29 | 如果普通终端原先是 A,后来登录成 B,Cage 会停止将默认目录分配给 A。B 需要单独加入。要让 A、B 长期共存,保留一份默认登录,另一个账号通过独立登录添加。 |
| 28 | 30 | |
| @@ -479,6 +479,7 @@ func (s *Server) handler() http.Handler { | ||
| 479 | 479 | mux.HandleFunc("DELETE /api/clipboard/files/{id}", s.clipboardArchive) |
| 480 | 480 | mux.HandleFunc("POST /api/terminals", s.newTerminal) |
| 481 | 481 | mux.HandleFunc("DELETE /api/terminals/{id}", s.closeTerminal) |
| 482 | 新增: mux.HandleFunc("POST /api/terminals/{id}/login-link", s.terminalLoginLink) | |
| 482 | 483 | mux.HandleFunc("POST /api/terminals/{id}/metadata", s.terminalSessionMetadata) |
| 483 | 484 | mux.HandleFunc("GET /api/terminals/{id}/stream", s.terminalStream) |
| 484 | 485 | web, _ := fs.Sub(assets, "web") |
| @@ -0,0 +1,94 @@ | ||
| 1 | 新增:package workbench | |
| 2 | 新增: | |
| 3 | 新增:import ( | |
| 4 | 新增: "context" | |
| 5 | 新增: "encoding/json" | |
| 6 | 新增: "net/http" | |
| 7 | 新增: "net/url" | |
| 8 | 新增: "strings" | |
| 9 | 新增: "time" | |
| 10 | 新增:) | |
| 11 | 新增: | |
| 12 | 新增:func claudeAuthorizationURL(raw string) bool { | |
| 13 | 新增: if len(raw) > 8192 || strings.ContainsAny(raw, "\r\n\t \\") { | |
| 14 | 新增: return false | |
| 15 | 新增: } | |
| 16 | 新增: u, err := url.Parse(raw) | |
| 17 | 新增: if err != nil || u.Scheme != "https" || u.User != nil || u.RawQuery == "" || | |
| 18 | 新增: (u.EscapedPath() != "/oauth/authorize" && u.EscapedPath() != "/oauth/authorize/") { | |
| 19 | 新增: return false | |
| 20 | 新增: } | |
| 21 | 新增: switch strings.ToLower(u.Host) { | |
| 22 | 新增: case "claude.ai", "claude.com", "console.anthropic.com", "platform.claude.com", "platform.anthropic.com": | |
| 23 | 新增: return true | |
| 24 | 新增: } | |
| 25 | 新增: return false | |
| 26 | 新增:} | |
| 27 | 新增: | |
| 28 | 新增:func (s *Server) terminalLoginLink(w http.ResponseWriter, r *http.Request) { | |
| 29 | 新增: var input struct { | |
| 30 | 新增: URL string `json:"url"` | |
| 31 | 新增: } | |
| 32 | 新增: if !readJSON(w, r, &input) { | |
| 33 | 新增: return | |
| 34 | 新增: } | |
| 35 | 新增: if !claudeAuthorizationURL(input.URL) { | |
| 36 | 新增: http.Error(w, "请选择 Claude 官方授权链接", 400) | |
| 37 | 新增: return | |
| 38 | 新增: } | |
| 39 | 新增: terminal := s.terminals.get(r.PathValue("id")) | |
| 40 | 新增: if terminal == nil { | |
| 41 | 新增: http.NotFound(w, r) | |
| 42 | 新增: return | |
| 43 | 新增: } | |
| 44 | 新增: terminal.loginLinkMu.Lock() | |
| 45 | 新增: defer terminal.loginLinkMu.Unlock() | |
| 46 | 新增: if !terminal.ownsConversation() { | |
| 47 | 新增: http.Error(w, "登录终端已关闭,请重新发起登录", 409) | |
| 48 | 新增: return | |
| 49 | 新增: } | |
| 50 | 新增: s.mu.Lock() | |
| 51 | 新增: ready, token := s.ready, s.credentials.Token | |
| 52 | 新增: s.mu.Unlock() | |
| 53 | 新增: if !ready { | |
| 54 | 新增: http.Error(w, "请先启动工作环境", 409) | |
| 55 | 新增: return | |
| 56 | 新增: } | |
| 57 | 新增: | |
| 58 | 新增: login := terminal.id | |
| 59 | 新增: if terminal.loginGuestID != "" { | |
| 60 | 新增: // Match the official CLI helper's private browser and cleanup key. | |
| 61 | 新增: login = terminal.loginGuestID | |
| 62 | 新增: } | |
| 63 | 新增: ctx, cancel := context.WithTimeout(r.Context(), 15*time.Second) | |
| 64 | 新增: defer cancel() | |
| 65 | 新增: payload, _ := json.Marshal(map[string]string{"login": login, "url": input.URL}) | |
| 66 | 新增: data, err := s.guestAPI(ctx, token, "POST", "/login-open", payload) | |
| 67 | 新增: if terminal.loginGuestID == "" { | |
| 68 | 新增: terminal.loginLinkCleanup.Do(func() { | |
| 69 | 新增: go func() { | |
| 70 | 新增: lifetime := s.lifetimeContext() | |
| 71 | 新增: select { | |
| 72 | 新增: case <-lifetime.Done(): | |
| 73 | 新增: return | |
| 74 | 新增: case <-terminal.done: | |
| 75 | 新增: } | |
| 76 | 新增: terminal.loginLinkMu.Lock() | |
| 77 | 新增: defer terminal.loginLinkMu.Unlock() | |
| 78 | 新增: ctx, cancel := context.WithTimeout(lifetime, 8*time.Second) | |
| 79 | 新增: defer cancel() | |
| 80 | 新增: body, _ := json.Marshal(map[string]string{"login": login}) | |
| 81 | 新增: _, _ = s.guestAPI(ctx, token, "POST", "/login-finish", body) | |
| 82 | 新增: }() | |
| 83 | 新增: }) | |
| 84 | 新增: } | |
| 85 | 新增: var tab struct { | |
| 86 | 新增: ID string `json:"id"` | |
| 87 | 新增: } | |
| 88 | 新增: if err != nil || json.Unmarshal(data, &tab) != nil || tab.ID == "" { | |
| 89 | 新增: http.Error(w, "Cage 浏览器未能打开授权页,请检查浏览器连接并重试", 502) | |
| 90 | 新增: return | |
| 91 | 新增: } | |
| 92 | 新增: w.Header().Set("Cache-Control", "no-store") | |
| 93 | 新增: jsonReply(w, tab) | |
| 94 | 新增:} | |
| @@ -0,0 +1,58 @@ | ||
| 1 | 新增:package workbench | |
| 2 | 新增: | |
| 3 | 新增:import ( | |
| 4 | 新增: "encoding/json" | |
| 5 | 新增: "net/http" | |
| 6 | 新增: "net/http/httptest" | |
| 7 | 新增: "testing" | |
| 8 | 新增: "time" | |
| 9 | 新增:) | |
| 10 | 新增: | |
| 11 | 新增:func TestTerminalLoginLinkUsesGuestPrivateBrowser(t *testing.T) { | |
| 12 | 新增: const address = "https://claude.ai/oauth/authorize?state=fixture" | |
| 13 | 新增: s := testServer(t) | |
| 14 | 新增: s.ready = true | |
| 15 | 新增: terminal := newTerminalSession(nil) | |
| 16 | 新增: terminal.id = "1234567890abcdef" | |
| 17 | 新增: s.terminals.sessions = map[string]*terminalSession{terminal.id: terminal} | |
| 18 | 新增: finished := make(chan struct{}) | |
| 19 | 新增: guest := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | |
| 20 | 新增: var in struct{ URL, Login string } | |
| 21 | 新增: if err := json.NewDecoder(r.Body).Decode(&in); err != nil || in.Login != terminal.id { | |
| 22 | 新增: t.Error("incorrect login request") | |
| 23 | 新增: } | |
| 24 | 新增: switch r.URL.Path { | |
| 25 | 新增: case "/login-open": | |
| 26 | 新增: if in.URL != address { | |
| 27 | 新增: t.Error("lost authorization parameters") | |
| 28 | 新增: } | |
| 29 | 新增: _, _ = w.Write([]byte(`{"id":"tab","url":"private"}`)) | |
| 30 | 新增: case "/login-finish": | |
| 31 | 新增: if in.URL != "" { | |
| 32 | 新增: t.Error("cleanup exposed URL") | |
| 33 | 新增: } | |
| 34 | 新增: _, _ = w.Write([]byte(`{"ok":true}`)) | |
| 35 | 新增: close(finished) | |
| 36 | 新增: default: | |
| 37 | 新增: t.Error("unexpected browser route") | |
| 38 | 新增: } | |
| 39 | 新增: })) | |
| 40 | 新增: defer guest.Close() | |
| 41 | 新增: s.browserControl = localBrowserClient(t, s.lifetimeContext(), guest) | |
| 42 | 新增: defer s.browserControl.close() | |
| 43 | 新增: for _, invalid := range []string{"http://claude.ai/oauth/authorize?x=1", "https://claude.ai:90/oauth/authorize?x=1", "https://[email protected]/oauth/authorize?x=1", "https://claude.ai/redirect?x=1", "https://claude.ai/oauth/authorize"} { | |
| 44 | 新增: if claudeAuthorizationURL(invalid) { | |
| 45 | 新增: t.Fatalf("accepted %q", invalid) | |
| 46 | 新增: } | |
| 47 | 新增: } | |
| 48 | 新增: w := request(s, "POST", "/api/terminals/"+terminal.id+"/login-link", s.origin, `{"url":"`+address+`"}`, true) | |
| 49 | 新增: if w.Code != 200 || w.Body.String() != "{\"id\":\"tab\"}\n" { | |
| 50 | 新增: t.Fatalf("%d %s", w.Code, w.Body.String()) | |
| 51 | 新增: } | |
| 52 | 新增: close(terminal.done) | |
| 53 | 新增: select { | |
| 54 | 新增: case <-finished: | |
| 55 | 新增: case <-time.After(2 * time.Second): | |
| 56 | 新增: t.Fatal("closing the terminal retained its login tab") | |
| 57 | 新增: } | |
| 58 | 新增:} | |
| @@ -64,6 +64,9 @@ func (m *terminalManager) startSessionWithPTY(cmd *exec.Cmd, workspace, kind str | ||
| 64 | 64 | session := newTerminalSession(file) |
| 65 | 65 | session.id, session.title, session.workspace, session.cmd = randomToken()[:16], title, workspace, cmd |
| 66 | 66 | session.kind, session.claudeAccountID = kind, selection.ID |
| 67 | 新增: if kind == "claude-login" { | |
| 68 | 新增: session.loginGuestID = selection.GuestID | |
| 69 | 新增: } | |
| 67 | 70 | if selection.GuestID == "default" { |
| 68 | 71 | session.claudeAccountSource = "default" |
| 69 | 72 | } else if selection.ID != "" { |
| @@ -26,6 +26,9 @@ type terminalSession struct { | ||
| 26 | 26 | conversationEnded bool |
| 27 | 27 | workspace string |
| 28 | 28 | kind, claudeAccountID string |
| 29 | 新增: loginGuestID string | |
| 30 | 新增: loginLinkMu sync.Mutex | |
| 31 | 新增: loginLinkCleanup sync.Once | |
| 29 | 32 | claudeAccountSource string |
| 30 | 33 | cmd *exec.Cmd |
| 31 | 34 | file terminalPTY |
| @@ -9,6 +9,7 @@ | ||
| 9 | 9 | "version": "0.1.7-rc.1", |
| 10 | 10 | "dependencies": { |
| 11 | 11 | "@xterm/addon-fit": "0.11.0", |
| 12 | 新增: "@xterm/addon-web-links": "0.12.0", | |
| 12 | 13 | "@xterm/xterm": "6.0.0", |
| 13 | 14 | "lucide-react": "1.65.0", |
| 14 | 15 | "marked": "18.1.3", |
| @@ -573,6 +574,12 @@ | ||
| 573 | 574 | "integrity": "sha512-jYcgT6xtVYhnhgxh3QgYDnnNMYTcf8ElbxxFzX0IZo+vabQqSPAjC3c1wJrKB5E19VwQei89QCiZZP86DCPF7g==", |
| 574 | 575 | "license": "MIT" |
| 575 | 576 | }, |
| 577 | 新增: "node_modules/@xterm/addon-web-links": { | |
| 578 | 新增: "version": "0.12.0", | |
| 579 | 新增: "resolved": "https://registry.npmjs.org/@xterm/addon-web-links/-/addon-web-links-0.12.0.tgz", | |
| 580 | 新增: "integrity": "sha512-4Smom3RPyVp7ZMYOYDoC/9eGJJJqYhnPLGGqJ6wOBfB8VxPViJNSKdgRYb8NpaM6YSelEKbA2SStD7lGyqaobw==", | |
| 581 | 新增: "license": "MIT" | |
| 582 | 新增: }, | |
| 576 | 583 | "node_modules/@xterm/xterm": { |
| 577 | 584 | "version": "6.0.0", |
| 578 | 585 | "resolved": "https://registry.npmjs.org/@xterm/xterm/-/xterm-6.0.0.tgz", |
| @@ -12,6 +12,7 @@ | ||
| 12 | 12 | }, |
| 13 | 13 | "dependencies": { |
| 14 | 14 | "@xterm/addon-fit": "0.11.0", |
| 15 | 新增: "@xterm/addon-web-links": "0.12.0", | |
| 15 | 16 | "@xterm/xterm": "6.0.0", |
| 16 | 17 | "lucide-react": "1.65.0", |
| 17 | 18 | "marked": "18.1.3", |
| @@ -0,0 +1,36 @@ | ||
| 1 | 新增:// Click both link formats emitted by command-line tools in the production xterm. | |
| 2 | 新增:import assert from "node:assert/strict"; | |
| 3 | 新增:import { createWorkbench } from "./workbench-fixture.mjs"; | |
| 4 | 新增: | |
| 5 | 新增:const url = "https://claude.ai/oauth/authorize?state=" + "fixture".repeat(22); | |
| 6 | 新增:for (const format of ["printed", "OSC 8"]) { | |
| 7 | 新增: const opened = []; | |
| 8 | 新增: const { browser, page, errors } = await createWorkbench({ mockWebSockets: false }); | |
| 9 | 新增: try { | |
| 10 | 新增: await page.route("**/api/terminals/*/login-link", route => { | |
| 11 | 新增: opened.push(route.request().postDataJSON()); | |
| 12 | 新增: return route.fulfill({ json: { id: "login-tab" } }); | |
| 13 | 新增: }); | |
| 14 | 新增: await page.routeWebSocket("**/api/**", socket => { | |
| 15 | 新增: if (!socket.url().includes("/terminals/")) return socket.close(); | |
| 16 | 新增: socket.send(Buffer.from(format === "printed" ? url : `\x1b]8;;${url}\x07Open Claude\x1b]8;;\x07`)); | |
| 17 | 新增: }); | |
| 18 | 新增: await page.reload(); | |
| 19 | 新增: await page.getByRole("button", { name: "启动 Cage 环境", exact: true }).click(); | |
| 20 | 新增: await page.getByRole("button", { name: "新建终端", exact: true }).first().click(); | |
| 21 | 新增: await page.getByRole("menuitem", { name: "普通终端", exact: true }).click(); | |
| 22 | 新增: const row = page.locator(".xterm-rows > div").first(); | |
| 23 | 新增: await row.filter({ hasText: format === "printed" ? "claude.ai" : "Open Claude" }).waitFor(); | |
| 24 | 新增: const rect = await row.boundingBox(); | |
| 25 | 新增: await page.mouse.move(rect.x + 20, rect.y + rect.height / 2); | |
| 26 | 新增: await page.waitForFunction(() => !!document.querySelector(".xterm-cursor-pointer")); | |
| 27 | 新增: assert.equal(opened.length, 0); | |
| 28 | 新增: await page.mouse.down(); | |
| 29 | 新增: await page.mouse.up(); | |
| 30 | 新增: await page.getByRole("tab", { name: /账号授权/ }).waitFor(); | |
| 31 | 新增: assert.deepEqual(opened, [{ url }]); | |
| 32 | 新增: assert.equal(page.context().pages().length, 1); | |
| 33 | 新增: assert.deepEqual(errors, []); | |
| 34 | 新增: } finally { await browser.close(); } | |
| 35 | 新增:} | |
| 36 | 新增:console.log("Printed and OSC 8 authorization links open the guest login tab on click."); | |
| @@ -1495,6 +1495,7 @@ function App() { | ||
| 1495 | 1495 | <div className="pane-body"> |
| 1496 | 1496 | <TerminalView |
| 1497 | 1497 | session={{ ...tab, id: tab.sessionId }} |
| 1498 | 新增: onLoginTab={({ id }) => showBrowserResult({ id, kind: "web", login: true, incognito: true, title: "账号授权" })} | |
| 1498 | 1499 | onControls={(control) => |
| 1499 | 1500 | control |
| 1500 | 1501 | ? terminalControls.current.set(tab.id, control) |
| @@ -0,0 +1,8 @@ | ||
| 1 | 新增:// Match printed links in xterm's wrapped buffer. OSC 8 links also use this | |
| 2 | 新增:// exact destination check before opening the guest's private login tab. | |
| 3 | 新增:export const claudeLoginLink = /https:\/\/(?:claude\.ai|claude\.com|console\.anthropic\.com|platform\.claude\.com|platform\.anthropic\.com)\/oauth\/authorize\/?\?[^\s"'<>\\\x1b]+/i; | |
| 4 | 新增: | |
| 5 | 新增:export function isClaudeLoginLink(uri) { | |
| 6 | 新增: const match = claudeLoginLink.exec(uri); | |
| 7 | 新增: return uri.length <= 8192 && match?.[0] === uri; | |
| 8 | 新增:} | |
| @@ -9,6 +9,8 @@ import React, { | ||
| 9 | 9 | import { useAppearance } from "./appearance"; |
| 10 | 10 | import { terminalTheme, systemCodeFont } from "./appearance-state.mjs"; |
| 11 | 11 | import { Terminal } from "@xterm/xterm"; |
| 12 | 新增:import { WebLinksAddon } from "@xterm/addon-web-links"; | |
| 13 | 新增:import { claudeLoginLink, isClaudeLoginLink } from "./terminal-login-link.mjs"; | |
| 12 | 14 | import { FitAddon } from "@xterm/addon-fit"; |
| 13 | 15 | import RFB from "../.generated/novnc/core/rfb.js"; |
| 14 | 16 | import { api, writeClipboard, nativeAvailable } from "./api"; |
| @@ -267,7 +269,7 @@ export const RemoteView = forwardRef(function RemoteView( | ||
| 267 | 269 | ); |
| 268 | 270 | }); |
| 269 | 271 | |
| 270 | 删除:export function TerminalView({ session, visible, focused = true, onStatus, onControls, onAccountChanged }) { | |
| 272 | 新增:export function TerminalView({ session, visible, focused = true, onStatus, onControls, onAccountChanged, onLoginTab }) { | |
| 271 | 273 | const { appearance, theme, variant } = useAppearance(); |
| 272 | 274 | const box = useRef(), |
| 273 | 275 | instance = useRef(), |
| @@ -290,7 +292,19 @@ export function TerminalView({ session, visible, focused = true, onStatus, onCon | ||
| 290 | 292 | result.then(reset, reset); |
| 291 | 293 | return result; |
| 292 | 294 | }; |
| 295 | 新增: const activateLogin = async (_event, uri) => { | |
| 296 | 新增: if (disposed || !visibleRef.current || !focusedRef.current || !isClaudeLoginLink(uri)) return; | |
| 297 | 新增: try { | |
| 298 | 新增: const tab = await api(`/terminals/${session.id}/login-link`, { url: uri }); | |
| 299 | 新增: if (!disposed && visibleRef.current && focusedRef.current) onLoginTab?.(tab); | |
| 300 | 新增: } catch (error) { if (!disposed) onStatus(error.message); } | |
| 301 | 新增: }; | |
| 293 | 302 | const terminal = new Terminal({ |
| 303 | 新增: linkHandler: { activate(event, uri) { | |
| 304 | 新增: if (isClaudeLoginLink(uri)) return void activateLogin(event, uri); | |
| 305 | 新增: // Keep xterm's user confirmation for other explicit HTTP(S) links. | |
| 306 | 新增: if (window.confirm(`打开此终端链接?\n${uri}`)) window.open(uri, "_blank", "noopener,noreferrer"); | |
| 307 | 新增: } }, | |
| 294 | 308 | fontFamily: theme.fonts.code || systemCodeFont, |
| 295 | 309 | fontSize: appearance.codeFontSize, |
| 296 | 310 | lineHeight: 1.3, |
| @@ -302,6 +316,7 @@ export function TerminalView({ session, visible, focused = true, onStatus, onCon | ||
| 302 | 316 | }); |
| 303 | 317 | const fitter = new FitAddon(); |
| 304 | 318 | terminal.loadAddon(fitter); |
| 319 | 新增: terminal.loadAddon(new WebLinksAddon(activateLogin, { urlRegex: claudeLoginLink })); | |
| 305 | 320 | terminal.open(box.current); |
| 306 | 321 | instance.current = terminal; |
| 307 | 322 | fit.current = fitter; |

