claude-coder 1.8.2 → 1.8.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/README.md +167 -167
  2. package/bin/cli.js +172 -172
  3. package/package.json +52 -52
  4. package/src/commands/auth.js +290 -240
  5. package/src/commands/setup-modules/helpers.js +99 -99
  6. package/src/commands/setup-modules/index.js +25 -25
  7. package/src/commands/setup-modules/mcp.js +94 -94
  8. package/src/commands/setup-modules/provider.js +260 -260
  9. package/src/commands/setup-modules/safety.js +61 -61
  10. package/src/commands/setup-modules/simplify.js +52 -52
  11. package/src/commands/setup.js +172 -172
  12. package/src/common/assets.js +236 -236
  13. package/src/common/config.js +125 -125
  14. package/src/common/constants.js +55 -55
  15. package/src/common/indicator.js +222 -222
  16. package/src/common/interaction.js +170 -170
  17. package/src/common/logging.js +77 -77
  18. package/src/common/sdk.js +50 -50
  19. package/src/common/tasks.js +88 -88
  20. package/src/common/utils.js +161 -161
  21. package/src/core/coding.js +55 -55
  22. package/src/core/context.js +117 -117
  23. package/src/core/go.js +310 -310
  24. package/src/core/harness.js +484 -484
  25. package/src/core/hooks.js +533 -533
  26. package/src/core/init.js +171 -171
  27. package/src/core/plan.js +325 -325
  28. package/src/core/prompts.js +227 -227
  29. package/src/core/query.js +49 -49
  30. package/src/core/repair.js +46 -46
  31. package/src/core/runner.js +195 -195
  32. package/src/core/scan.js +89 -89
  33. package/src/core/session.js +56 -56
  34. package/src/core/simplify.js +53 -52
  35. package/templates/bash-process.md +12 -12
  36. package/templates/codingSystem.md +65 -65
  37. package/templates/codingUser.md +17 -17
  38. package/templates/coreProtocol.md +29 -29
  39. package/templates/goSystem.md +130 -130
  40. package/templates/guidance.json +52 -52
  41. package/templates/planSystem.md +78 -78
  42. package/templates/planUser.md +8 -8
  43. package/templates/playwright.md +16 -16
  44. package/templates/requirements.example.md +57 -57
  45. package/templates/scanSystem.md +120 -120
  46. package/templates/scanUser.md +10 -10
  47. package/templates/test_rule.md +194 -194
@@ -1,240 +1,290 @@
1
- 'use strict';
2
-
3
- const fs = require('fs');
4
- const os = require('os');
5
- const path = require('path');
6
- const { execSync } = require('child_process');
7
- const { loadConfig, log } = require('../common/config');
8
- const { assets } = require('../common/assets');
9
- const { appendGitignore } = require('../common/utils');
10
-
11
- function updateGitignore(entry) {
12
- if (appendGitignore(assets.projectRoot, entry)) {
13
- log('ok', `.gitignore 已添加: ${entry}`);
14
- }
15
- }
16
-
17
- function updateMcpConfig(mcpPath, mode) {
18
- let mcpConfig = {};
19
- if (fs.existsSync(mcpPath)) {
20
- try { mcpConfig = JSON.parse(fs.readFileSync(mcpPath, 'utf8')); } catch {}
21
- }
22
-
23
- if (!mcpConfig.mcpServers) mcpConfig.mcpServers = {};
24
-
25
- const args = ['@playwright/mcp@latest'];
26
- const projectRoot = assets.projectRoot;
27
-
28
- switch (mode) {
29
- case 'persistent': {
30
- const browserProfilePath = assets.path('browserProfile');
31
- const relProfile = path.relative(projectRoot, browserProfilePath).split(path.sep).join('/');
32
- args.push(`--user-data-dir=${relProfile}`);
33
- break;
34
- }
35
- case 'isolated': {
36
- const playwrightAuthPath = assets.path('playwrightAuth');
37
- const relAuth = path.relative(projectRoot, playwrightAuthPath).split(path.sep).join('/');
38
- args.push('--isolated', `--storage-state=${relAuth}`);
39
- break;
40
- }
41
- case 'extension':
42
- args.push('--extension');
43
- break;
44
- }
45
-
46
- mcpConfig.mcpServers.playwright = { command: 'npx', args };
47
- fs.writeFileSync(mcpPath, JSON.stringify(mcpConfig, null, 2) + '\n', 'utf8');
48
- log('ok', `.mcp.json 已配置 Playwright MCP (${mode} 模式)`);
49
- }
50
-
51
- function enableMcpPlaywrightEnv() {
52
- const envPath = assets.path('env');
53
- if (!envPath || !fs.existsSync(envPath)) return;
54
-
55
- let content = fs.readFileSync(envPath, 'utf8');
56
- if (/^MCP_PLAYWRIGHT=/m.test(content)) {
57
- content = content.replace(/^MCP_PLAYWRIGHT=.*/m, 'MCP_PLAYWRIGHT=true');
58
- } else {
59
- const suffix = content.endsWith('\n') ? '' : '\n';
60
- content += `${suffix}MCP_PLAYWRIGHT=true\n`;
61
- }
62
- fs.writeFileSync(envPath, content, 'utf8');
63
- log('ok', '.claude-coder/.env 已设置 MCP_PLAYWRIGHT=true');
64
- }
65
-
66
- async function authPersistent(url) {
67
- const profileDir = assets.path('browserProfile');
68
- if (!fs.existsSync(profileDir)) fs.mkdirSync(profileDir, { recursive: true });
69
-
70
- const lockFile = path.join(profileDir, 'SingletonLock');
71
- if (fs.existsSync(lockFile)) {
72
- fs.unlinkSync(lockFile);
73
- log('warn', '已清理残留的 SingletonLock(上次浏览器未正常关闭)');
74
- }
75
-
76
- console.log('操作步骤:');
77
- console.log(' 1. 浏览器将自动打开,请手动完成登录');
78
- console.log(' 2. 登录成功后关闭浏览器窗口');
79
- console.log(' 3. 登录状态将保存在持久化配置中');
80
- console.log(' 4. MCP 后续会话自动复用此登录状态');
81
- console.log('');
82
-
83
- const scriptContent = [
84
- `let chromium;`,
85
- `try { chromium = require('playwright').chromium; } catch {`,
86
- ` try { chromium = require('@playwright/test').chromium; } catch {`,
87
- ` console.error('错误: 未找到 playwright 模块');`,
88
- ` console.error('请安装: npx playwright install chromium');`,
89
- ` process.exit(1);`,
90
- ` }`,
91
- `}`,
92
- `(async () => {`,
93
- ` const ctx = await chromium.launchPersistentContext(${JSON.stringify(profileDir)}, { headless: false });`,
94
- ` const page = ctx.pages()[0] || await ctx.newPage();`,
95
- ` try { await page.goto(${JSON.stringify(url)}); } catch {}`,
96
- ` console.log('请在浏览器中完成登录后关闭窗口...');`,
97
- ` await new Promise(r => {`,
98
- ` ctx.on('close', r);`,
99
- ` const t = setInterval(() => { try { if (!ctx.pages().length) { clearInterval(t); r(); } } catch { clearInterval(t); r(); } }, 2000);`,
100
- ` });`,
101
- ` try { await ctx.close(); } catch {}`,
102
- `})().then(() => process.exit(0)).catch(() => process.exit(0));`,
103
- ].join('\n');
104
-
105
- const tmpScript = path.join(os.tmpdir(), `pw-auth-${Date.now()}.js`);
106
- fs.writeFileSync(tmpScript, scriptContent);
107
-
108
- const helperModules = path.join(__dirname, '..', 'node_modules');
109
- const existingNodePath = process.env.NODE_PATH || '';
110
- const nodePath = existingNodePath ? `${helperModules}:${existingNodePath}` : helperModules;
111
- const projectRoot = assets.projectRoot;
112
-
113
- let scriptOk = false;
114
- try {
115
- execSync(`node "${tmpScript}"`, {
116
- stdio: 'inherit',
117
- cwd: projectRoot,
118
- env: { ...process.env, NODE_PATH: nodePath },
119
- });
120
- scriptOk = true;
121
- } catch {
122
- const profileFiles = fs.readdirSync(profileDir);
123
- scriptOk = profileFiles.length > 2;
124
- if (!scriptOk) {
125
- log('error', 'Playwright 启动失败,且未检测到有效的浏览器配置');
126
- log('info', '请确保已安装 Chromium: npx playwright install chromium');
127
- try { fs.unlinkSync(tmpScript); } catch {}
128
- return;
129
- }
130
- log('warn', '浏览器退出码非零,但已检测到有效配置,继续...');
131
- }
132
-
133
- try { fs.unlinkSync(tmpScript); } catch {}
134
-
135
- const mcpPath = assets.path('mcpConfig');
136
- log('ok', '登录状态已保存到持久化配置');
137
- updateMcpConfig(mcpPath, 'persistent');
138
- updateGitignore('.claude-coder/.runtime/browser-profile');
139
- enableMcpPlaywrightEnv();
140
-
141
- console.log('');
142
- log('ok', '配置完成!');
143
- const relProfile = path.relative(projectRoot, profileDir);
144
- log('info', `MCP 使用 persistent 模式 (user-data-dir: ${relProfile})`);
145
- log('info', '如需更新登录状态,重新运行 claude-coder auth');
146
- }
147
-
148
- async function authIsolated(url) {
149
- const playwrightAuthPath = assets.path('playwrightAuth');
150
- const projectRoot = assets.projectRoot;
151
-
152
- console.log('操作步骤:');
153
- console.log(' 1. 浏览器将自动打开,请手动完成登录');
154
- console.log(' 2. 登录成功后关闭浏览器窗口');
155
- console.log(' 3. 登录状态(cookies + localStorage)将保存到 playwright-auth.json');
156
- console.log(' 4. MCP 每次会话自动从此文件加载初始状态');
157
- console.log('');
158
-
159
- try {
160
- execSync(
161
- `npx playwright codegen --save-storage="${playwrightAuthPath}" "${url}"`,
162
- { stdio: 'inherit', cwd: projectRoot }
163
- );
164
- } catch (err) {
165
- if (!fs.existsSync(playwrightAuthPath)) {
166
- log('error', `Playwright 登录状态导出失败: ${err.message}`);
167
- log('info', '请确保已安装: npx playwright install chromium');
168
- return;
169
- }
170
- }
171
-
172
- if (!fs.existsSync(playwrightAuthPath)) {
173
- log('error', '未检测到导出的登录状态文件');
174
- return;
175
- }
176
-
177
- const mcpPath = assets.path('mcpConfig');
178
- log('ok', '登录状态已保存到 playwright-auth.json');
179
- updateMcpConfig(mcpPath, 'isolated');
180
- updateGitignore('.claude-coder/playwright-auth.json');
181
- enableMcpPlaywrightEnv();
182
-
183
- console.log('');
184
- log('ok', '配置完成!');
185
- log('info', 'MCP 使用 isolated 模式 (storage-state)');
186
- log('info', 'cookies localStorage 每次会话自动从 playwright-auth.json 加载');
187
- log('info', '如需更新登录状态,重新运行 claude-coder auth');
188
- }
189
-
190
- function authExtension() {
191
- console.log('Extension 模式说明:');
192
- console.log('');
193
- console.log(' 此模式通过 Chrome 扩展连接到您正在运行的浏览器。');
194
- console.log(' MCP 将直接使用浏览器中已有的登录态和扩展。');
195
- console.log('');
196
- console.log(' 前置条件:');
197
- console.log(' 1. 安装 "Playwright MCP Bridge" Chrome/Edge 扩展');
198
- console.log(' https://chromewebstore.google.com/detail/playwright-mcp-bridge/mmlmfjhmonkocbjadbfplnigmagldckm');
199
- console.log(' 2. 确保浏览器已启动且扩展已启用');
200
- console.log(' 3. 无需额外认证操作,您的浏览器登录态将自动可用');
201
- console.log('');
202
-
203
- const mcpPath = assets.path('mcpConfig');
204
- updateMcpConfig(mcpPath, 'extension');
205
- enableMcpPlaywrightEnv();
206
-
207
- console.log('');
208
- log('ok', '配置完成!');
209
- log('info', 'MCP 使用 extension 模式(连接真实浏览器)');
210
- log('info', '确保 Chrome/Edge 已运行且 Playwright MCP Bridge 扩展已启用');
211
- }
212
-
213
- async function auth(url) {
214
- assets.ensureDirs();
215
- const config = loadConfig();
216
- const mode = config.playwrightMode;
217
- const targetUrl = url || 'http://localhost:3000';
218
-
219
- log('info', `Playwright 模式: ${mode}`);
220
- log('info', `目标 URL: ${targetUrl}`);
221
- console.log('');
222
-
223
- switch (mode) {
224
- case 'persistent':
225
- await authPersistent(targetUrl);
226
- break;
227
- case 'isolated':
228
- await authIsolated(targetUrl);
229
- break;
230
- case 'extension':
231
- authExtension();
232
- break;
233
- default:
234
- log('error', `未知的 Playwright 模式: ${mode}`);
235
- log('info', '请运行 claude-coder setup 重新配置');
236
- return;
237
- }
238
- }
239
-
240
- module.exports = { auth, updateMcpConfig };
1
+ 'use strict';
2
+
3
+ const fs = require('fs');
4
+ const os = require('os');
5
+ const path = require('path');
6
+ const { execSync } = require('child_process');
7
+ const { loadConfig, log } = require('../common/config');
8
+ const { assets } = require('../common/assets');
9
+ const { appendGitignore } = require('../common/utils');
10
+
11
+ function resolvePlaywright() {
12
+ const { createRequire } = require('module');
13
+ const pkg = 'playwright';
14
+
15
+ try {
16
+ return path.dirname(require.resolve(`${pkg}/package.json`));
17
+ } catch {}
18
+
19
+ try {
20
+ const r = createRequire(path.join(process.cwd(), 'noop.js'));
21
+ return path.dirname(r.resolve(`${pkg}/package.json`));
22
+ } catch {}
23
+
24
+ try {
25
+ const globalRoot = execSync('npm root -g', { encoding: 'utf8', stdio: ['pipe', 'pipe', 'pipe'] }).trim();
26
+ const pkgJsonPath = path.join(globalRoot, pkg, 'package.json');
27
+ if (fs.existsSync(pkgJsonPath)) return path.join(globalRoot, pkg);
28
+ } catch {}
29
+
30
+ return null;
31
+ }
32
+
33
+ function normalizeUrl(url) {
34
+ if (!url) return null;
35
+ return /^https?:\/\//.test(url) ? url : `http://${url}`;
36
+ }
37
+
38
+ function updateGitignore(entry) {
39
+ if (appendGitignore(assets.projectRoot, entry)) {
40
+ log('ok', `.gitignore 已添加: ${entry}`);
41
+ }
42
+ }
43
+
44
+ function updateMcpConfig(mcpPath, mode) {
45
+ let mcpConfig = {};
46
+ if (fs.existsSync(mcpPath)) {
47
+ try { mcpConfig = JSON.parse(fs.readFileSync(mcpPath, 'utf8')); } catch {}
48
+ }
49
+
50
+ if (!mcpConfig.mcpServers) mcpConfig.mcpServers = {};
51
+
52
+ const args = ['@playwright/mcp@latest'];
53
+ const projectRoot = assets.projectRoot;
54
+
55
+ switch (mode) {
56
+ case 'persistent': {
57
+ const browserProfilePath = assets.path('browserProfile');
58
+ const relProfile = path.relative(projectRoot, browserProfilePath).split(path.sep).join('/');
59
+ args.push(`--user-data-dir=${relProfile}`);
60
+ break;
61
+ }
62
+ case 'isolated': {
63
+ const playwrightAuthPath = assets.path('playwrightAuth');
64
+ const relAuth = path.relative(projectRoot, playwrightAuthPath).split(path.sep).join('/');
65
+ args.push('--isolated', `--storage-state=${relAuth}`);
66
+ break;
67
+ }
68
+ case 'extension':
69
+ args.push('--extension');
70
+ break;
71
+ }
72
+
73
+ mcpConfig.mcpServers.playwright = { command: 'npx', args };
74
+ fs.writeFileSync(mcpPath, JSON.stringify(mcpConfig, null, 2) + '\n', 'utf8');
75
+ log('ok', `.mcp.json 已配置 Playwright MCP (${mode} 模式)`);
76
+ }
77
+
78
+ function enableMcpPlaywrightEnv() {
79
+ const envPath = assets.path('env');
80
+ if (!envPath || !fs.existsSync(envPath)) return;
81
+
82
+ let content = fs.readFileSync(envPath, 'utf8');
83
+ if (/^MCP_PLAYWRIGHT=/m.test(content)) {
84
+ content = content.replace(/^MCP_PLAYWRIGHT=.*/m, 'MCP_PLAYWRIGHT=true');
85
+ } else {
86
+ const suffix = content.endsWith('\n') ? '' : '\n';
87
+ content += `${suffix}MCP_PLAYWRIGHT=true\n`;
88
+ }
89
+ fs.writeFileSync(envPath, content, 'utf8');
90
+ log('ok', '.claude-coder/.env 已设置 MCP_PLAYWRIGHT=true');
91
+ }
92
+
93
+ // ─────────────────────────────────────────────────────────────
94
+ // 浏览器脚本(session cookie 自动持久化)
95
+ // ─────────────────────────────────────────────────────────────
96
+
97
+ function buildBrowserScript(playwrightDir, profileDir, url) {
98
+ const thirtyDays = Math.floor(Date.now() / 1000) + 86400 * 30;
99
+ return [
100
+ `const { chromium } = require(${JSON.stringify(playwrightDir)});`,
101
+ `(async () => {`,
102
+ ` const ctx = await chromium.launchPersistentContext(${JSON.stringify(profileDir)}, { headless: false });`,
103
+ ` const page = ctx.pages()[0] || await ctx.newPage();`,
104
+ ` try { await page.goto(${JSON.stringify(url)}); } catch {}`,
105
+ ` console.log('请在浏览器中完成操作后关闭窗口...');`,
106
+ ` const persistSessionCookies = async () => {`,
107
+ ` try {`,
108
+ ` const cookies = await ctx.cookies();`,
109
+ ` const session = cookies.filter(c => c.expires === -1);`,
110
+ ` if (session.length > 0) {`,
111
+ ` await ctx.addCookies(session.map(c => ({ ...c, expires: ${thirtyDays} })));`,
112
+ ` console.log('已将 ' + session.length + ' 个 session cookie 转为持久化');`,
113
+ ` }`,
114
+ ` } catch {}`,
115
+ ` };`,
116
+ ` ctx.on('page', p => p.on('close', () => persistSessionCookies()));`,
117
+ ` for (const p of ctx.pages()) p.on('close', () => persistSessionCookies());`,
118
+ ` await new Promise(r => {`,
119
+ ` ctx.on('close', r);`,
120
+ ` const t = setInterval(async () => {`,
121
+ ` try {`,
122
+ ` if (!ctx.pages().length) { clearInterval(t); await persistSessionCookies(); r(); }`,
123
+ ` } catch { clearInterval(t); r(); }`,
124
+ ` }, 2000);`,
125
+ ` });`,
126
+ ` try { await ctx.close(); } catch {}`,
127
+ `})().then(() => process.exit(0)).catch(() => process.exit(0));`,
128
+ ].join('\n');
129
+ }
130
+
131
+ function runBrowserScript(script, cwd) {
132
+ const tmpScript = path.join(os.tmpdir(), `pw-auth-${Date.now()}.js`);
133
+ fs.writeFileSync(tmpScript, script);
134
+ try {
135
+ execSync(`node "${tmpScript}"`, { stdio: 'inherit', cwd });
136
+ return true;
137
+ } catch {
138
+ return false;
139
+ } finally {
140
+ try { fs.unlinkSync(tmpScript); } catch {}
141
+ }
142
+ }
143
+
144
+ // ─────────────────────────────────────────────────────────────
145
+ // auth 模式实现
146
+ // ─────────────────────────────────────────────────────────────
147
+
148
+ async function authPersistent(url) {
149
+ const playwrightDir = resolvePlaywright();
150
+ if (!playwrightDir) {
151
+ log('error', '未找到 playwright 模块');
152
+ log('info', '请安装: npm install -g playwright && npx playwright install chromium');
153
+ return;
154
+ }
155
+
156
+ const profileDir = assets.path('browserProfile');
157
+ if (!fs.existsSync(profileDir)) fs.mkdirSync(profileDir, { recursive: true });
158
+
159
+ const lockFile = path.join(profileDir, 'SingletonLock');
160
+ if (fs.existsSync(lockFile)) {
161
+ fs.unlinkSync(lockFile);
162
+ log('warn', '已清理残留的 SingletonLock(上次浏览器未正常关闭)');
163
+ }
164
+
165
+ console.log('操作步骤:');
166
+ console.log(' 1. 浏览器将自动打开,请手动完成登录');
167
+ console.log(' 2. 登录成功后关闭浏览器窗口');
168
+ console.log(' 3. 登录状态将保存在持久化配置中(session cookie 自动转持久化)');
169
+ console.log(' 4. MCP 后续会话自动复用此登录状态');
170
+ console.log('');
171
+
172
+ const script = buildBrowserScript(playwrightDir, profileDir, url);
173
+ const projectRoot = assets.projectRoot;
174
+
175
+ const ok = runBrowserScript(script, projectRoot);
176
+ if (!ok) {
177
+ const profileFiles = fs.readdirSync(profileDir);
178
+ if (profileFiles.length <= 2) {
179
+ log('error', 'Playwright 启动失败,且未检测到有效的浏览器配置');
180
+ log('info', '请确保已安装 Chromium: npx playwright install chromium');
181
+ return;
182
+ }
183
+ log('warn', '浏览器退出码非零,但已检测到有效配置,继续...');
184
+ }
185
+
186
+ const mcpPath = assets.path('mcpConfig');
187
+ log('ok', '登录状态已保存到持久化配置');
188
+ updateMcpConfig(mcpPath, 'persistent');
189
+ updateGitignore('.claude-coder/.runtime/browser-profile');
190
+ enableMcpPlaywrightEnv();
191
+
192
+ console.log('');
193
+ log('ok', '配置完成!');
194
+ const relProfile = path.relative(projectRoot, profileDir);
195
+ log('info', `MCP 使用 persistent 模式 (user-data-dir: ${relProfile})`);
196
+ log('info', '验证: 再次运行 claude-coder auth <URL>,浏览器应直接进入已登录状态');
197
+ }
198
+
199
+ async function authIsolated(url) {
200
+ const playwrightAuthPath = assets.path('playwrightAuth');
201
+ const projectRoot = assets.projectRoot;
202
+
203
+ console.log('操作步骤:');
204
+ console.log(' 1. 浏览器将自动打开,请手动完成登录');
205
+ console.log(' 2. 登录成功后关闭浏览器窗口');
206
+ console.log(' 3. 登录状态(cookies + localStorage)将保存到 playwright-auth.json');
207
+ console.log(' 4. MCP 每次会话自动从此文件加载初始状态');
208
+ console.log('');
209
+
210
+ try {
211
+ execSync(
212
+ `npx playwright codegen --save-storage="${playwrightAuthPath}" "${url}"`,
213
+ { stdio: 'inherit', cwd: projectRoot }
214
+ );
215
+ } catch (err) {
216
+ if (!fs.existsSync(playwrightAuthPath)) {
217
+ log('error', `Playwright 登录状态导出失败: ${err.message}`);
218
+ log('info', '请确保已安装: npx playwright install chromium');
219
+ return;
220
+ }
221
+ }
222
+
223
+ if (!fs.existsSync(playwrightAuthPath)) {
224
+ log('error', '未检测到导出的登录状态文件');
225
+ return;
226
+ }
227
+
228
+ const mcpPath = assets.path('mcpConfig');
229
+ log('ok', '登录状态已保存到 playwright-auth.json');
230
+ updateMcpConfig(mcpPath, 'isolated');
231
+ updateGitignore('.claude-coder/playwright-auth.json');
232
+ enableMcpPlaywrightEnv();
233
+
234
+ console.log('');
235
+ log('ok', '配置完成!');
236
+ log('info', 'MCP 使用 isolated 模式 (storage-state)');
237
+ log('info', 'cookies 和 localStorage 每次会话自动从 playwright-auth.json 加载');
238
+ }
239
+
240
+ function authExtension() {
241
+ console.log('Extension 模式说明:');
242
+ console.log('');
243
+ console.log(' 此模式通过 Chrome 扩展连接到您正在运行的浏览器。');
244
+ console.log(' MCP 将直接使用浏览器中已有的登录态和扩展。');
245
+ console.log('');
246
+ console.log(' 前置条件:');
247
+ console.log(' 1. 安装 "Playwright MCP Bridge" Chrome/Edge 扩展');
248
+ console.log(' https://chromewebstore.google.com/detail/playwright-mcp-bridge/mmlmfjhmonkocbjadbfplnigmagldckm');
249
+ console.log(' 2. 确保浏览器已启动且扩展已启用');
250
+ console.log(' 3. 无需额外认证操作,您的浏览器登录态将自动可用');
251
+ console.log('');
252
+
253
+ const mcpPath = assets.path('mcpConfig');
254
+ updateMcpConfig(mcpPath, 'extension');
255
+ enableMcpPlaywrightEnv();
256
+
257
+ console.log('');
258
+ log('ok', '配置完成!');
259
+ log('info', 'MCP 使用 extension 模式(连接真实浏览器)');
260
+ log('info', '确保 Chrome/Edge 已运行且 Playwright MCP Bridge 扩展已启用');
261
+ }
262
+
263
+ async function auth(url) {
264
+ assets.ensureDirs();
265
+ const config = loadConfig();
266
+ const mode = config.playwrightMode;
267
+ const targetUrl = normalizeUrl(url) || 'http://localhost:3000';
268
+
269
+ log('info', `Playwright 模式: ${mode}`);
270
+ log('info', `目标 URL: ${targetUrl}`);
271
+ console.log('');
272
+
273
+ switch (mode) {
274
+ case 'persistent':
275
+ await authPersistent(targetUrl);
276
+ break;
277
+ case 'isolated':
278
+ await authIsolated(targetUrl);
279
+ break;
280
+ case 'extension':
281
+ authExtension();
282
+ break;
283
+ default:
284
+ log('error', `未知的 Playwright 模式: ${mode}`);
285
+ log('info', '请运行 claude-coder setup 重新配置');
286
+ return;
287
+ }
288
+ }
289
+
290
+ module.exports = { auth, updateMcpConfig };