botbrowser-mcp 0.1.4 → 0.1.6

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.
@@ -0,0 +1,195 @@
1
+ /**
2
+ * Playwright 浏览器实例管理器
3
+ */
4
+ import { chromium } from 'playwright';
5
+ import { ProfileRepository } from '../db/repositories/profile.js';
6
+ import { InstanceRepository } from '../db/repositories/instance.js';
7
+ import { AccountRepository } from '../db/repositories/account.js';
8
+ import fs from 'fs/promises';
9
+ import path from 'path';
10
+ import os from 'os';
11
+ export class PlaywrightManager {
12
+ instances = new Map();
13
+ profileRepo = new ProfileRepository();
14
+ instanceRepo = new InstanceRepository();
15
+ accountRepo = new AccountRepository();
16
+ /**
17
+ * 启动浏览器实例
18
+ */
19
+ async launchInstance(profileAlias, accountId, launchOptions) {
20
+ const profile = this.profileRepo.getByAlias(profileAlias);
21
+ if (!profile) {
22
+ throw new Error(`浏览器配置不存在: ${profileAlias}`);
23
+ }
24
+ // 验证账号
25
+ if (accountId) {
26
+ const account = this.accountRepo.getById(accountId);
27
+ if (!account || account.profile_alias !== profileAlias) {
28
+ throw new Error(`账号 ${accountId} 不属于配置 ${profileAlias}`);
29
+ }
30
+ }
31
+ // 为每个实例创建独立的用户数据目录
32
+ const timestamp = Date.now();
33
+ const randomId = Math.random().toString(36).substring(7);
34
+ const userDataDir = path.join(os.tmpdir(), `botbrowser-${profileAlias}-${timestamp}-${randomId}`);
35
+ // 使用传入的 launchOptions,如果没有则使用默认值
36
+ const options = launchOptions || {};
37
+ // 确保有基本的 args 数组
38
+ if (!options.args) {
39
+ options.args = [];
40
+ }
41
+ // 添加基本参数(如果还没有的话)
42
+ if (!options.args.includes('--no-first-run')) {
43
+ options.args.push('--no-first-run', '--no-default-browser-check');
44
+ }
45
+ // 防指纹参数
46
+ if (profile.fingerprint_path) {
47
+ options.args.push(`--bot-profile=${profile.fingerprint_path}`);
48
+ }
49
+ // 代理配置(profile 中的代理优先级更高)
50
+ if (profile.proxy_server) {
51
+ options.proxy = {
52
+ server: profile.proxy_server,
53
+ };
54
+ if (profile.proxy_username) {
55
+ options.proxy.username = profile.proxy_username;
56
+ options.proxy.password = profile.proxy_password || '';
57
+ }
58
+ if (profile.proxy_bypass) {
59
+ options.proxy.bypass = profile.proxy_bypass;
60
+ }
61
+ }
62
+ // 设置默认 viewport(如果配置中没有指定)
63
+ if (!options.viewport) {
64
+ options.viewport = { width: 1280, height: 720 };
65
+ }
66
+ // 如果配置指定了 storage_state_path,尝试加载
67
+ if (profile.storage_state_path) {
68
+ try {
69
+ await fs.access(profile.storage_state_path);
70
+ options.storageState = profile.storage_state_path;
71
+ }
72
+ catch {
73
+ // 文件不存在,使用默认空状态
74
+ }
75
+ }
76
+ // 使用 launchPersistentContext 启动(支持 userDataDir)
77
+ const context = await chromium.launchPersistentContext(userDataDir, {
78
+ executablePath: profile.executable_path,
79
+ ...options, // launchOptions 中可以包含 headless、channel 等配置
80
+ });
81
+ // 保存到数据库
82
+ const instanceId = this.instanceRepo.create({
83
+ profile_alias: profileAlias,
84
+ account_id: accountId,
85
+ is_active: this.instances.size === 0 ? 1 : 0, // 第一个实例自动激活
86
+ });
87
+ // 保存到内存
88
+ this.instances.set(instanceId, {
89
+ id: instanceId,
90
+ context,
91
+ profileAlias,
92
+ accountId,
93
+ userDataDir,
94
+ });
95
+ // 如果是第一个实例,设置为活跃
96
+ if (this.instances.size === 1) {
97
+ this.instanceRepo.setActive(instanceId);
98
+ }
99
+ // 更新配置的最后使用时间
100
+ this.profileRepo.updateLastUsed(profileAlias);
101
+ return instanceId;
102
+ }
103
+ /**
104
+ * 切换活跃实例
105
+ */
106
+ async switchActive(instanceId) {
107
+ if (!this.instances.has(instanceId)) {
108
+ throw new Error(`实例 ${instanceId} 不存在`);
109
+ }
110
+ this.instanceRepo.setActive(instanceId);
111
+ }
112
+ /**
113
+ * 停止实例
114
+ */
115
+ async stopInstance(instanceId) {
116
+ const instance = this.instances.get(instanceId);
117
+ if (!instance) {
118
+ throw new Error(`实例 ${instanceId} 不存在`);
119
+ }
120
+ // 保存 storageState(如果配置指定了路径)
121
+ const profile = this.profileRepo.getByAlias(instance.profileAlias);
122
+ if (profile && profile.storage_state_path) {
123
+ const storageState = await instance.context.storageState();
124
+ await fs.writeFile(profile.storage_state_path, JSON.stringify(storageState, null, 2));
125
+ }
126
+ // 关闭浏览器
127
+ await instance.context.close();
128
+ // 清理用户数据目录
129
+ try {
130
+ await fs.rm(instance.userDataDir, { recursive: true, force: true });
131
+ }
132
+ catch (err) {
133
+ console.error(`清理用户数据目录失败: ${instance.userDataDir}`, err);
134
+ }
135
+ // 从内存和数据库删除
136
+ this.instances.delete(instanceId);
137
+ this.instanceRepo.delete(instanceId);
138
+ // 如果删除的是活跃实例,激活第一个可用实例
139
+ const activeInstance = this.instanceRepo.getActive();
140
+ if (!activeInstance && this.instances.size > 0) {
141
+ const firstId = this.instances.keys().next().value;
142
+ if (firstId !== undefined) {
143
+ this.instanceRepo.setActive(firstId);
144
+ }
145
+ }
146
+ }
147
+ /**
148
+ * 停止所有实例
149
+ */
150
+ async stopAll() {
151
+ const ids = Array.from(this.instances.keys());
152
+ for (const id of ids) {
153
+ await this.stopInstance(id);
154
+ }
155
+ }
156
+ /**
157
+ * 获取活跃实例的上下文
158
+ */
159
+ getActiveContext() {
160
+ const activeInstance = this.instanceRepo.getActive();
161
+ if (!activeInstance)
162
+ return null;
163
+ const instance = this.instances.get(activeInstance.id);
164
+ if (!instance)
165
+ return null;
166
+ this.instanceRepo.updateLastActive(activeInstance.id);
167
+ return instance.context;
168
+ }
169
+ /**
170
+ * 获取所有实例列表
171
+ */
172
+ listInstances() {
173
+ return Array.from(this.instances.values()).map(inst => ({
174
+ id: inst.id,
175
+ profile_alias: inst.profileAlias,
176
+ account_id: inst.accountId,
177
+ is_active: this.instanceRepo.getActive()?.id === inst.id,
178
+ }));
179
+ }
180
+ /**
181
+ * 清理已停止的实例记录(实例在内存中不存在但数据库有记录)
182
+ */
183
+ async cleanupOrphaned() {
184
+ const dbInstances = this.instanceRepo.getAll();
185
+ let cleaned = 0;
186
+ for (const dbInst of dbInstances) {
187
+ if (!this.instances.has(dbInst.id)) {
188
+ this.instanceRepo.delete(dbInst.id);
189
+ cleaned++;
190
+ }
191
+ }
192
+ return cleaned;
193
+ }
194
+ }
195
+ //# sourceMappingURL=manager.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"manager.js","sourceRoot":"","sources":["../../src/playwright/manager.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,EAAE,QAAQ,EAA2B,MAAM,YAAY,CAAC;AAC/D,OAAO,EAAE,iBAAiB,EAAE,MAAM,+BAA+B,CAAC;AAClE,OAAO,EAAE,kBAAkB,EAAE,MAAM,gCAAgC,CAAC;AACpE,OAAO,EAAE,iBAAiB,EAAE,MAAM,+BAA+B,CAAC;AAClE,OAAO,EAAE,MAAM,aAAa,CAAC;AAC7B,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,EAAE,MAAM,IAAI,CAAC;AAUpB,MAAM,OAAO,iBAAiB;IACpB,SAAS,GAAiC,IAAI,GAAG,EAAE,CAAC;IACpD,WAAW,GAAG,IAAI,iBAAiB,EAAE,CAAC;IACtC,YAAY,GAAG,IAAI,kBAAkB,EAAE,CAAC;IACxC,WAAW,GAAG,IAAI,iBAAiB,EAAE,CAAC;IAE9C;;OAEG;IACH,KAAK,CAAC,cAAc,CAAC,YAAoB,EAAE,SAAkB,EAAE,aAAmB;QAChF,MAAM,OAAO,GAAG,IAAI,CAAC,WAAW,CAAC,UAAU,CAAC,YAAY,CAAC,CAAC;QAC1D,IAAI,CAAC,OAAO,EAAE,CAAC;YACb,MAAM,IAAI,KAAK,CAAC,aAAa,YAAY,EAAE,CAAC,CAAC;QAC/C,CAAC;QAED,OAAO;QACP,IAAI,SAAS,EAAE,CAAC;YACd,MAAM,OAAO,GAAG,IAAI,CAAC,WAAW,CAAC,OAAO,CAAC,SAAS,CAAC,CA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,cAAc;YAAE,OAAO,IAAI,CAAC;QAEjC,MAAM,QAAQ,GAAG,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,cAAc,CAAC,EAAE,CAAC,CAAC;QACvD,IAAI,CAAC,QAAQ;YAAE,OAAO,IAAI,CAAC;QAE3B,IAAI,CAAC,YAAY,CAAC,gBAAgB,CAAC,cAAc,CAAC,EAAE,CAAC,CAAC;QACtD,OAAO,QAAQ,CAAC,OAAO,CAAC;IAC1B,CAAC;IAED;;OAEG;IACH,aAAa;QACX,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,MAAM,EAAE,CAAC,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;YACtD,EAAE,EAAE,IAAI,CAAC,EAAE;YACX,aAAa,EAAE,IAAI,CAAC,YAAY;YAChC,UAAU,EAAE,IAAI,CAAC,SAAS;YAC1B,SAAS,EAAE,IAAI,CAAC,YAAY,CAAC,SAAS,EAAE,EAAE,EAAE,KAAK,IAAI,CAAC,EAAE;SACzD,CAAC,CAAC,CAAC;IACN,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,eAAe;QACnB,MAAM,WAAW,GAAG,IAAI,CAAC,YAAY,CAAC,MAAM,EAAE,CAAC;QAC/C,IAAI,OAAO,GAAG,CAAC,CAAC;QAEhB,KAAK,MAAM,MAAM,IAAI,WAAW,EAAE,CAAC;YACjC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,MAAM,CAAC,EAAE,CAAC,EAAE,CAAC;gBACnC,IAAI,CAAC,YAAY,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;gBACpC,OAAO,EAAE,CAAC;YACZ,CAAC;QACH,CAAC;QAED,OAAO,OAAO,CAAC;IACjB,CAAC;CACF"}
@@ -0,0 +1,116 @@
1
+ export declare const accountTools: {
2
+ add_account: {
3
+ description: string;
4
+ inputSchema: {
5
+ type: string;
6
+ properties: {
7
+ profile_alias: {
8
+ type: string;
9
+ description: string;
10
+ };
11
+ username: {
12
+ type: string;
13
+ description: string;
14
+ };
15
+ metadata: {
16
+ type: string;
17
+ description: string;
18
+ };
19
+ };
20
+ required: string[];
21
+ };
22
+ handler: (args: any) => Promise<{
23
+ content: {
24
+ type: string;
25
+ text: string;
26
+ }[];
27
+ }>;
28
+ };
29
+ list_accounts: {
30
+ description: string;
31
+ inputSchema: {
32
+ type: string;
33
+ properties: {
34
+ profile_alias: {
35
+ type: string;
36
+ description: string;
37
+ };
38
+ };
39
+ };
40
+ handler: (args: any) => Promise<{
41
+ content: {
42
+ type: string;
43
+ text: string;
44
+ }[];
45
+ }>;
46
+ };
47
+ find_account: {
48
+ description: string;
49
+ inputSchema: {
50
+ type: string;
51
+ properties: {
52
+ profile_alias: {
53
+ type: string;
54
+ description: string;
55
+ };
56
+ username: {
57
+ type: string;
58
+ description: string;
59
+ };
60
+ };
61
+ required: string[];
62
+ };
63
+ handler: (args: any) => Promise<{
64
+ content: {
65
+ type: string;
66
+ text: string;
67
+ }[];
68
+ }>;
69
+ };
70
+ update_account: {
71
+ description: string;
72
+ inputSchema: {
73
+ type: string;
74
+ properties: {
75
+ id: {
76
+ type: string;
77
+ description: string;
78
+ };
79
+ username: {
80
+ type: string;
81
+ description: string;
82
+ };
83
+ metadata: {
84
+ type: string;
85
+ description: string;
86
+ };
87
+ };
88
+ required: string[];
89
+ };
90
+ handler: (args: any) => Promise<{
91
+ content: {
92
+ type: string;
93
+ text: string;
94
+ }[];
95
+ }>;
96
+ };
97
+ delete_account: {
98
+ description: string;
99
+ inputSchema: {
100
+ type: string;
101
+ properties: {
102
+ id: {
103
+ type: string;
104
+ description: string;
105
+ };
106
+ };
107
+ required: string[];
108
+ };
109
+ handler: (args: any) => Promise<{
110
+ content: {
111
+ type: string;
112
+ text: string;
113
+ }[];
114
+ }>;
115
+ };
116
+ };
@@ -0,0 +1,110 @@
1
+ /**
2
+ * 账号管理工具
3
+ */
4
+ import { AccountRepository } from '../db/repositories/account.js';
5
+ const accountRepo = new AccountRepository();
6
+ export const accountTools = {
7
+ add_account: {
8
+ description: '添加账号到浏览器配置',
9
+ inputSchema: {
10
+ type: 'object',
11
+ properties: {
12
+ profile_alias: { type: 'string', description: '浏览器配置别名' },
13
+ username: { type: 'string', description: '账号用户名' },
14
+ metadata: { type: 'string', description: '账号元数据(密码、邮箱等信息,格式自由)' },
15
+ },
16
+ required: ['profile_alias', 'username'],
17
+ },
18
+ handler: async (args) => {
19
+ const id = accountRepo.create(args);
20
+ return {
21
+ content: [{
22
+ type: 'text',
23
+ text: `账号 "${args.username}" 添加成功,ID: ${id}`,
24
+ }],
25
+ };
26
+ },
27
+ },
28
+ list_accounts: {
29
+ description: '列出所有账号或指定配置的账号',
30
+ inputSchema: {
31
+ type: 'object',
32
+ properties: {
33
+ profile_alias: { type: 'string', description: '浏览器配置别名(可选)' },
34
+ },
35
+ },
36
+ handler: async (args) => {
37
+ const accounts = args.profile_alias
38
+ ? accountRepo.getByProfile(args.profile_alias)
39
+ : accountRepo.getAll();
40
+ return {
41
+ content: [{
42
+ type: 'text',
43
+ text: JSON.stringify(accounts, null, 2),
44
+ }],
45
+ };
46
+ },
47
+ },
48
+ find_account: {
49
+ description: '查找指定配置下的账号',
50
+ inputSchema: {
51
+ type: 'object',
52
+ properties: {
53
+ profile_alias: { type: 'string', description: '浏览器配置别名' },
54
+ username: { type: 'string', description: '账号用户名' },
55
+ },
56
+ required: ['profile_alias', 'username'],
57
+ },
58
+ handler: async (args) => {
59
+ const account = accountRepo.findByUsername(args.profile_alias, args.username);
60
+ return {
61
+ content: [{
62
+ type: 'text',
63
+ text: account ? JSON.stringify(account, null, 2) : '未找到账号',
64
+ }],
65
+ };
66
+ },
67
+ },
68
+ update_account: {
69
+ description: '更新账号信息',
70
+ inputSchema: {
71
+ type: 'object',
72
+ properties: {
73
+ id: { type: 'number', description: '账号ID' },
74
+ username: { type: 'string', description: '新用户名' },
75
+ metadata: { type: 'string', description: '新元数据' },
76
+ },
77
+ required: ['id'],
78
+ },
79
+ handler: async (args) => {
80
+ const { id, ...updates } = args;
81
+ accountRepo.update(id, updates);
82
+ return {
83
+ content: [{
84
+ type: 'text',
85
+ text: `账号 ${id} 更新成功`,
86
+ }],
87
+ };
88
+ },
89
+ },
90
+ delete_account: {
91
+ description: '删除账号',
92
+ inputSchema: {
93
+ type: 'object',
94
+ properties: {
95
+ id: { type: 'number', description: '账号ID' },
96
+ },
97
+ required: ['id'],
98
+ },
99
+ handler: async (args) => {
100
+ accountRepo.delete(args.id);
101
+ return {
102
+ content: [{
103
+ type: 'text',
104
+ text: `账号 ${args.id} 删除成功`,
105
+ }],
106
+ };
107
+ },
108
+ },
109
+ };
110
+ //# sourceMappingURL=account.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"account.js","sourceRoot":"","sources":["../../src/tools/account.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,EAAE,iBAAiB,EAAE,MAAM,+BAA+B,CAAC;AAElE,MAAM,WAAW,GAAG,IAAI,iBAAiB,EAAE,CAAC;AAE5C,MAAM,CAAC,MAAM,YAAY,GAAG;IAC1B,WAAW,EAAE;QACX,WAAW,EAAE,YAAY;QACzB,WAAW,EAAE;YACX,IAAI,EAAE,QAAQ;YACd,UAAU,EAAE;gBACV,aAAa,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,WAAW,EAAE,SAAS,EAAE;gBACzD,QAAQ,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,WAAW,EAAE,OAAO,EAAE;gBAClD,QAAQ,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,WAAW,EAAE,sBAAsB,EAAE;aAClE;YACD,QAAQ,EAAE,CAAC,eAAe,EAAE,UAAU,CAAC;SACxC;QACD,OAAO,EAAE,KAAK,EAAE,IAAS,EAAE,EAAE;YAC3B,MAAM,EAAE,GAAG,WAAW,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;YACpC,OAAO;gBACL,OAAO,EAAE,C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,IAAI,EAAE,MAAM;wBACZ,IAAI,EAAE,MAAM,IAAI,CAAC,EAAE,OAAO;qBAC3B,CAAC;aACH,CAAC;QACJ,CAAC;KACF;CACF,CAAC"}
@@ -0,0 +1,178 @@
1
+ /**
2
+ * 浏览器操作工具
3
+ * 基于 Playwright API 实现常用浏览器操作
4
+ */
5
+ import { PlaywrightManager } from '../playwright/manager.js';
6
+ export declare function setManager(m: PlaywrightManager): void;
7
+ export declare const browserTools: {
8
+ browser_navigate: {
9
+ description: string;
10
+ inputSchema: {
11
+ type: string;
12
+ properties: {
13
+ url: {
14
+ type: string;
15
+ description: string;
16
+ };
17
+ };
18
+ required: string[];
19
+ };
20
+ handler: (args: any) => Promise<{
21
+ content: {
22
+ type: string;
23
+ text: string;
24
+ }[];
25
+ }>;
26
+ };
27
+ browser_click: {
28
+ description: string;
29
+ inputSchema: {
30
+ type: string;
31
+ properties: {
32
+ selector: {
33
+ type: string;
34
+ description: string;
35
+ };
36
+ };
37
+ required: string[];
38
+ };
39
+ handler: (args: any) => Promise<{
40
+ content: {
41
+ type: string;
42
+ text: string;
43
+ }[];
44
+ }>;
45
+ };
46
+ browser_fill: {
47
+ description: string;
48
+ inputSchema: {
49
+ type: string;
50
+ properties: {
51
+ selector: {
52
+ type: string;
53
+ description: string;
54
+ };
55
+ value: {
56
+ type: string;
57
+ description: string;
58
+ };
59
+ };
60
+ required: string[];
61
+ };
62
+ handler: (args: any) => Promise<{
63
+ content: {
64
+ type: string;
65
+ text: string;
66
+ }[];
67
+ }>;
68
+ };
69
+ browser_screenshot: {
70
+ description: string;
71
+ inputSchema: {
72
+ type: string;
73
+ properties: {
74
+ path: {
75
+ type: string;
76
+ description: string;
77
+ };
78
+ fullPage: {
79
+ type: string;
80
+ description: string;
81
+ };
82
+ };
83
+ required: string[];
84
+ };
85
+ handler: (args: any) => Promise<{
86
+ content: {
87
+ type: string;
88
+ text: string;
89
+ }[];
90
+ }>;
91
+ };
92
+ browser_get_text: {
93
+ description: string;
94
+ inputSchema: {
95
+ type: string;
96
+ properties: {
97
+ selector: {
98
+ type: string;
99
+ description: string;
100
+ };
101
+ };
102
+ required: string[];
103
+ };
104
+ handler: (args: any) => Promise<{
105
+ content: {
106
+ type: string;
107
+ text: string;
108
+ }[];
109
+ }>;
110
+ };
111
+ browser_wait: {
112
+ description: string;
113
+ inputSchema: {
114
+ type: string;
115
+ properties: {
116
+ selector: {
117
+ type: string;
118
+ description: string;
119
+ };
120
+ timeout: {
121
+ type: string;
122
+ description: string;
123
+ };
124
+ };
125
+ };
126
+ handler: (args: any) => Promise<{
127
+ content: {
128
+ type: string;
129
+ text: string;
130
+ }[];
131
+ }>;
132
+ };
133
+ browser_evaluate: {
134
+ description: string;
135
+ inputSchema: {
136
+ type: string;
137
+ properties: {
138
+ script: {
139
+ type: string;
140
+ description: string;
141
+ };
142
+ };
143
+ required: string[];
144
+ };
145
+ handler: (args: any) => Promise<{
146
+ content: {
147
+ type: string;
148
+ text: string;
149
+ }[];
150
+ }>;
151
+ };
152
+ browser_new_page: {
153
+ description: string;
154
+ inputSchema: {
155
+ type: string;
156
+ properties: {};
157
+ };
158
+ handler: () => Promise<{
159
+ content: {
160
+ type: string;
161
+ text: string;
162
+ }[];
163
+ }>;
164
+ };
165
+ browser_get_url: {
166
+ description: string;
167
+ inputSchema: {
168
+ type: string;
169
+ properties: {};
170
+ };
171
+ handler: () => Promise<{
172
+ content: {
173
+ type: string;
174
+ text: string;
175
+ }[];
176
+ }>;
177
+ };
178
+ };