@ejazullah/browser-mcp 0.0.56
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.
- package/LICENSE +202 -0
- package/README.md +860 -0
- package/cli.js +19 -0
- package/index.d.ts +23 -0
- package/index.js +1061 -0
- package/lib/auth.js +82 -0
- package/lib/browserContextFactory.js +205 -0
- package/lib/browserServerBackend.js +125 -0
- package/lib/config.js +266 -0
- package/lib/context.js +232 -0
- package/lib/databaseLogger.js +264 -0
- package/lib/extension/cdpRelay.js +346 -0
- package/lib/extension/extensionContextFactory.js +56 -0
- package/lib/extension/main.js +26 -0
- package/lib/fileUtils.js +32 -0
- package/lib/httpServer.js +39 -0
- package/lib/index.js +39 -0
- package/lib/javascript.js +49 -0
- package/lib/log.js +21 -0
- package/lib/loop/loop.js +69 -0
- package/lib/loop/loopClaude.js +152 -0
- package/lib/loop/loopOpenAI.js +143 -0
- package/lib/loop/main.js +60 -0
- package/lib/loopTools/context.js +66 -0
- package/lib/loopTools/main.js +49 -0
- package/lib/loopTools/perform.js +32 -0
- package/lib/loopTools/snapshot.js +29 -0
- package/lib/loopTools/tool.js +18 -0
- package/lib/manualPromise.js +111 -0
- package/lib/mcp/inProcessTransport.js +72 -0
- package/lib/mcp/server.js +93 -0
- package/lib/mcp/transport.js +217 -0
- package/lib/mongoDBLogger.js +252 -0
- package/lib/package.js +20 -0
- package/lib/program.js +113 -0
- package/lib/response.js +172 -0
- package/lib/sessionLog.js +156 -0
- package/lib/tab.js +266 -0
- package/lib/tools/cdp.js +169 -0
- package/lib/tools/common.js +55 -0
- package/lib/tools/console.js +33 -0
- package/lib/tools/dialogs.js +47 -0
- package/lib/tools/evaluate.js +53 -0
- package/lib/tools/extraction.js +217 -0
- package/lib/tools/files.js +44 -0
- package/lib/tools/forms.js +180 -0
- package/lib/tools/getext.js +99 -0
- package/lib/tools/install.js +53 -0
- package/lib/tools/interactions.js +191 -0
- package/lib/tools/keyboard.js +86 -0
- package/lib/tools/mouse.js +99 -0
- package/lib/tools/navigate.js +70 -0
- package/lib/tools/network.js +41 -0
- package/lib/tools/pdf.js +40 -0
- package/lib/tools/screenshot.js +75 -0
- package/lib/tools/selectors.js +233 -0
- package/lib/tools/snapshot.js +169 -0
- package/lib/tools/states.js +147 -0
- package/lib/tools/tabs.js +87 -0
- package/lib/tools/tool.js +33 -0
- package/lib/tools/utils.js +74 -0
- package/lib/tools/wait.js +56 -0
- package/lib/tools.js +64 -0
- package/lib/utils.js +26 -0
- package/openapi.json +683 -0
- package/package.json +92 -0
package/lib/context.js
ADDED
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Microsoft Corporation.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
import debug from 'debug';
|
|
17
|
+
import { logUnhandledError } from './log.js';
|
|
18
|
+
import { Tab } from './tab.js';
|
|
19
|
+
import { outputFile } from './config.js';
|
|
20
|
+
const testDebug = debug('pw:mcp:test');
|
|
21
|
+
export class Context {
|
|
22
|
+
tools;
|
|
23
|
+
config;
|
|
24
|
+
sessionLog;
|
|
25
|
+
options;
|
|
26
|
+
_browserContextPromise;
|
|
27
|
+
_browserContextFactory;
|
|
28
|
+
_tabs = [];
|
|
29
|
+
_currentTab;
|
|
30
|
+
_clientInfo;
|
|
31
|
+
static _allContexts = new Set();
|
|
32
|
+
_closeBrowserContextPromise;
|
|
33
|
+
_isRunningTool = false;
|
|
34
|
+
_abortController = new AbortController();
|
|
35
|
+
constructor(options) {
|
|
36
|
+
this.tools = options.tools;
|
|
37
|
+
this.config = options.config;
|
|
38
|
+
this.sessionLog = options.sessionLog;
|
|
39
|
+
this.options = options;
|
|
40
|
+
this._browserContextFactory = options.browserContextFactory;
|
|
41
|
+
this._clientInfo = options.clientInfo;
|
|
42
|
+
testDebug('create context');
|
|
43
|
+
Context._allContexts.add(this);
|
|
44
|
+
}
|
|
45
|
+
static async disposeAll() {
|
|
46
|
+
await Promise.all([...Context._allContexts].map(context => context.dispose()));
|
|
47
|
+
}
|
|
48
|
+
tabs() {
|
|
49
|
+
return this._tabs;
|
|
50
|
+
}
|
|
51
|
+
currentTab() {
|
|
52
|
+
return this._currentTab;
|
|
53
|
+
}
|
|
54
|
+
currentTabOrDie() {
|
|
55
|
+
if (!this._currentTab)
|
|
56
|
+
throw new Error('No open pages available. Use the "browser_navigate" tool to navigate to a page first.');
|
|
57
|
+
return this._currentTab;
|
|
58
|
+
}
|
|
59
|
+
async newTab() {
|
|
60
|
+
const { browserContext } = await this._ensureBrowserContext();
|
|
61
|
+
const page = await browserContext.newPage();
|
|
62
|
+
this._currentTab = this._tabs.find(t => t.page === page);
|
|
63
|
+
return this._currentTab;
|
|
64
|
+
}
|
|
65
|
+
async selectTab(index) {
|
|
66
|
+
const tab = this._tabs[index];
|
|
67
|
+
if (!tab)
|
|
68
|
+
throw new Error(`Tab ${index} not found`);
|
|
69
|
+
await tab.page.bringToFront();
|
|
70
|
+
this._currentTab = tab;
|
|
71
|
+
return tab;
|
|
72
|
+
}
|
|
73
|
+
async updateBrowserContextFactory(newFactory) {
|
|
74
|
+
// Close existing browser context first
|
|
75
|
+
await this.closeBrowserContext();
|
|
76
|
+
// Update to the new factory
|
|
77
|
+
this._browserContextFactory = newFactory;
|
|
78
|
+
}
|
|
79
|
+
async ensureTab() {
|
|
80
|
+
const { browserContext } = await this._ensureBrowserContext();
|
|
81
|
+
if (!this._currentTab)
|
|
82
|
+
await browserContext.newPage();
|
|
83
|
+
return this._currentTab;
|
|
84
|
+
}
|
|
85
|
+
async closeTab(index) {
|
|
86
|
+
const tab = index === undefined ? this._currentTab : this._tabs[index];
|
|
87
|
+
if (!tab)
|
|
88
|
+
throw new Error(`Tab ${index} not found`);
|
|
89
|
+
const url = tab.page.url();
|
|
90
|
+
await tab.page.close();
|
|
91
|
+
return url;
|
|
92
|
+
}
|
|
93
|
+
async outputFile(name) {
|
|
94
|
+
return outputFile(this.config, this._clientInfo.rootPath, name);
|
|
95
|
+
}
|
|
96
|
+
_onPageCreated(page) {
|
|
97
|
+
const tab = new Tab(this, page, tab => this._onPageClosed(tab));
|
|
98
|
+
this._tabs.push(tab);
|
|
99
|
+
if (!this._currentTab)
|
|
100
|
+
this._currentTab = tab;
|
|
101
|
+
}
|
|
102
|
+
_onPageClosed(tab) {
|
|
103
|
+
const index = this._tabs.indexOf(tab);
|
|
104
|
+
if (index === -1)
|
|
105
|
+
return;
|
|
106
|
+
this._tabs.splice(index, 1);
|
|
107
|
+
if (this._currentTab === tab)
|
|
108
|
+
this._currentTab = this._tabs[Math.min(index, this._tabs.length - 1)];
|
|
109
|
+
if (!this._tabs.length)
|
|
110
|
+
void this.closeBrowserContext();
|
|
111
|
+
}
|
|
112
|
+
async closeBrowserContext() {
|
|
113
|
+
if (!this._closeBrowserContextPromise)
|
|
114
|
+
this._closeBrowserContextPromise = this._closeBrowserContextImpl().catch(logUnhandledError);
|
|
115
|
+
await this._closeBrowserContextPromise;
|
|
116
|
+
this._closeBrowserContextPromise = undefined;
|
|
117
|
+
}
|
|
118
|
+
isRunningTool() {
|
|
119
|
+
return this._isRunningTool;
|
|
120
|
+
}
|
|
121
|
+
setRunningTool(isRunningTool) {
|
|
122
|
+
this._isRunningTool = isRunningTool;
|
|
123
|
+
}
|
|
124
|
+
async _closeBrowserContextImpl() {
|
|
125
|
+
if (!this._browserContextPromise)
|
|
126
|
+
return;
|
|
127
|
+
testDebug('close context');
|
|
128
|
+
const promise = this._browserContextPromise;
|
|
129
|
+
this._browserContextPromise = undefined;
|
|
130
|
+
await promise.then(async ({ browserContext, close }) => {
|
|
131
|
+
if (this.config.saveTrace)
|
|
132
|
+
await browserContext.tracing.stop();
|
|
133
|
+
await close();
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
async dispose() {
|
|
137
|
+
this._abortController.abort('MCP context disposed');
|
|
138
|
+
await this.closeBrowserContext();
|
|
139
|
+
Context._allContexts.delete(this);
|
|
140
|
+
}
|
|
141
|
+
async _setupRequestInterception(context) {
|
|
142
|
+
if (this.config.network?.allowedOrigins?.length) {
|
|
143
|
+
await context.route('**', route => route.abort('blockedbyclient'));
|
|
144
|
+
for (const origin of this.config.network.allowedOrigins)
|
|
145
|
+
await context.route(`*://${origin}/**`, route => route.continue());
|
|
146
|
+
}
|
|
147
|
+
if (this.config.network?.blockedOrigins?.length) {
|
|
148
|
+
for (const origin of this.config.network.blockedOrigins)
|
|
149
|
+
await context.route(`*://${origin}/**`, route => route.abort('blockedbyclient'));
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
_ensureBrowserContext() {
|
|
153
|
+
if (!this._browserContextPromise) {
|
|
154
|
+
this._browserContextPromise = this._setupBrowserContext();
|
|
155
|
+
this._browserContextPromise.catch(() => {
|
|
156
|
+
this._browserContextPromise = undefined;
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
return this._browserContextPromise;
|
|
160
|
+
}
|
|
161
|
+
async _setupBrowserContext() {
|
|
162
|
+
if (this._closeBrowserContextPromise)
|
|
163
|
+
throw new Error('Another browser context is being closed.');
|
|
164
|
+
// TODO: move to the browser context factory to make it based on isolation mode.
|
|
165
|
+
const result = await this._browserContextFactory.createContext(this._clientInfo, this._abortController.signal);
|
|
166
|
+
const { browserContext } = result;
|
|
167
|
+
await this._setupRequestInterception(browserContext);
|
|
168
|
+
if (this.sessionLog)
|
|
169
|
+
await InputRecorder.create(this, browserContext);
|
|
170
|
+
for (const page of browserContext.pages())
|
|
171
|
+
this._onPageCreated(page);
|
|
172
|
+
browserContext.on('page', page => this._onPageCreated(page));
|
|
173
|
+
if (this.config.saveTrace) {
|
|
174
|
+
await browserContext.tracing.start({
|
|
175
|
+
name: 'trace',
|
|
176
|
+
screenshots: false,
|
|
177
|
+
snapshots: true,
|
|
178
|
+
sources: false,
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
return result;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
export class InputRecorder {
|
|
185
|
+
_context;
|
|
186
|
+
_browserContext;
|
|
187
|
+
constructor(context, browserContext) {
|
|
188
|
+
this._context = context;
|
|
189
|
+
this._browserContext = browserContext;
|
|
190
|
+
}
|
|
191
|
+
static async create(context, browserContext) {
|
|
192
|
+
const recorder = new InputRecorder(context, browserContext);
|
|
193
|
+
await recorder._initialize();
|
|
194
|
+
return recorder;
|
|
195
|
+
}
|
|
196
|
+
async _initialize() {
|
|
197
|
+
const sessionLog = this._context.sessionLog;
|
|
198
|
+
await this._browserContext._enableRecorder({
|
|
199
|
+
mode: 'recording',
|
|
200
|
+
recorderMode: 'api',
|
|
201
|
+
}, {
|
|
202
|
+
actionAdded: (page, data, code) => {
|
|
203
|
+
if (this._context.isRunningTool())
|
|
204
|
+
return;
|
|
205
|
+
const tab = Tab.forPage(page);
|
|
206
|
+
if (tab)
|
|
207
|
+
sessionLog.logUserAction(data.action, tab, code, false);
|
|
208
|
+
},
|
|
209
|
+
actionUpdated: (page, data, code) => {
|
|
210
|
+
if (this._context.isRunningTool())
|
|
211
|
+
return;
|
|
212
|
+
const tab = Tab.forPage(page);
|
|
213
|
+
if (tab)
|
|
214
|
+
sessionLog.logUserAction(data.action, tab, code, true);
|
|
215
|
+
},
|
|
216
|
+
signalAdded: (page, data) => {
|
|
217
|
+
if (this._context.isRunningTool())
|
|
218
|
+
return;
|
|
219
|
+
if (data.signal.name !== 'navigation')
|
|
220
|
+
return;
|
|
221
|
+
const tab = Tab.forPage(page);
|
|
222
|
+
const navigateAction = {
|
|
223
|
+
name: 'navigate',
|
|
224
|
+
url: data.signal.url,
|
|
225
|
+
signals: [],
|
|
226
|
+
};
|
|
227
|
+
if (tab)
|
|
228
|
+
sessionLog.logUserAction(navigateAction, tab, `await page.goto('${data.signal.url}');`, false);
|
|
229
|
+
},
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
}
|
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Microsoft Corporation.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
import fs from 'fs';
|
|
17
|
+
import path from 'path';
|
|
18
|
+
import { logUnhandledError } from './log.js';
|
|
19
|
+
import { MongoDBLogger } from './mongoDBLogger.js';
|
|
20
|
+
export class DatabaseLogger {
|
|
21
|
+
_dbFile;
|
|
22
|
+
_pendingWrites = [];
|
|
23
|
+
_writeQueue = Promise.resolve();
|
|
24
|
+
_flushTimeout;
|
|
25
|
+
_mongoLogger = null;
|
|
26
|
+
_useMongo = false;
|
|
27
|
+
_sessionId;
|
|
28
|
+
constructor(dbFilePath, mongoConfig) {
|
|
29
|
+
this._dbFile = dbFilePath;
|
|
30
|
+
this._sessionId = mongoConfig?.sessionId || `session_${Date.now()}`;
|
|
31
|
+
this._initializeDatabase();
|
|
32
|
+
// Try to initialize MongoDB if config is provided or env var is set
|
|
33
|
+
if (mongoConfig || process.env.MONGODB_URL) {
|
|
34
|
+
this._initializeMongoDB(mongoConfig).catch(logUnhandledError);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
_initializeDatabase() {
|
|
38
|
+
// Create database file with header if it doesn't exist
|
|
39
|
+
if (!fs.existsSync(this._dbFile)) {
|
|
40
|
+
const header = [];
|
|
41
|
+
fs.writeFileSync(this._dbFile, JSON.stringify(header, null, 2));
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
async _initializeMongoDB(config) {
|
|
45
|
+
try {
|
|
46
|
+
this._mongoLogger = new MongoDBLogger({ ...config, sessionId: this._sessionId });
|
|
47
|
+
const connected = await this._mongoLogger.connect();
|
|
48
|
+
if (connected) {
|
|
49
|
+
this._useMongo = true;
|
|
50
|
+
// eslint-disable-next-line no-console
|
|
51
|
+
console.error(`✓ Using MongoDB for interaction logging (Session: ${this._sessionId})`);
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
// eslint-disable-next-line no-console
|
|
55
|
+
console.error('✓ Using JSON file for interaction logging (MongoDB unavailable)');
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
catch (error) {
|
|
59
|
+
// eslint-disable-next-line no-console
|
|
60
|
+
console.error('✓ Using JSON file for interaction logging (MongoDB unavailable)');
|
|
61
|
+
logUnhandledError(error);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Log an element interaction to the database
|
|
66
|
+
*/
|
|
67
|
+
logInteraction(interaction) {
|
|
68
|
+
const interactionWithSession = { ...interaction, sessionId: this._sessionId };
|
|
69
|
+
// Log to MongoDB if available
|
|
70
|
+
if (this._useMongo && this._mongoLogger) {
|
|
71
|
+
this._mongoLogger.logInteraction(interactionWithSession).catch(logUnhandledError);
|
|
72
|
+
}
|
|
73
|
+
// Always log to JSON file as backup
|
|
74
|
+
this._pendingWrites.push(interactionWithSession);
|
|
75
|
+
// Debounce writes
|
|
76
|
+
if (this._flushTimeout)
|
|
77
|
+
clearTimeout(this._flushTimeout);
|
|
78
|
+
this._flushTimeout = setTimeout(() => this._flush(), 500);
|
|
79
|
+
}
|
|
80
|
+
async _flush() {
|
|
81
|
+
if (this._pendingWrites.length === 0)
|
|
82
|
+
return;
|
|
83
|
+
const interactions = [...this._pendingWrites];
|
|
84
|
+
this._pendingWrites = [];
|
|
85
|
+
this._writeQueue = this._writeQueue
|
|
86
|
+
.then(async () => {
|
|
87
|
+
try {
|
|
88
|
+
// Read existing data
|
|
89
|
+
let existingData = [];
|
|
90
|
+
if (fs.existsSync(this._dbFile)) {
|
|
91
|
+
const content = await fs.promises.readFile(this._dbFile, 'utf-8');
|
|
92
|
+
if (content.trim()) {
|
|
93
|
+
existingData = JSON.parse(content);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
// Append new interactions
|
|
97
|
+
existingData.push(...interactions);
|
|
98
|
+
// Write back to file
|
|
99
|
+
await fs.promises.writeFile(this._dbFile, JSON.stringify(existingData, null, 2), 'utf-8');
|
|
100
|
+
}
|
|
101
|
+
catch (error) {
|
|
102
|
+
logUnhandledError(error);
|
|
103
|
+
}
|
|
104
|
+
})
|
|
105
|
+
.catch(logUnhandledError);
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Flush all pending writes immediately
|
|
109
|
+
*/
|
|
110
|
+
async flushSync() {
|
|
111
|
+
if (this._flushTimeout) {
|
|
112
|
+
clearTimeout(this._flushTimeout);
|
|
113
|
+
this._flushTimeout = undefined;
|
|
114
|
+
}
|
|
115
|
+
await this._flush();
|
|
116
|
+
await this._writeQueue;
|
|
117
|
+
// Flush MongoDB if available
|
|
118
|
+
if (this._mongoLogger) {
|
|
119
|
+
await this._mongoLogger.flushSync();
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Get MongoDB logger instance
|
|
124
|
+
*/
|
|
125
|
+
getMongoLogger() {
|
|
126
|
+
return this._mongoLogger;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Check if using MongoDB
|
|
130
|
+
*/
|
|
131
|
+
isUsingMongo() {
|
|
132
|
+
return this._useMongo;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Get session ID
|
|
136
|
+
*/
|
|
137
|
+
getSessionId() {
|
|
138
|
+
return this._sessionId;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Disconnect from MongoDB
|
|
142
|
+
*/
|
|
143
|
+
async disconnect() {
|
|
144
|
+
if (this._mongoLogger) {
|
|
145
|
+
await this._mongoLogger.disconnect();
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Query interactions by tool name
|
|
150
|
+
*/
|
|
151
|
+
async queryByToolName(toolName, sessionId) {
|
|
152
|
+
await this.flushSync();
|
|
153
|
+
// Try MongoDB first
|
|
154
|
+
if (this._useMongo && this._mongoLogger) {
|
|
155
|
+
return await this._mongoLogger.queryByToolName(toolName, sessionId);
|
|
156
|
+
}
|
|
157
|
+
// Fall back to JSON file
|
|
158
|
+
if (!fs.existsSync(this._dbFile))
|
|
159
|
+
return [];
|
|
160
|
+
const content = await fs.promises.readFile(this._dbFile, 'utf-8');
|
|
161
|
+
if (!content.trim())
|
|
162
|
+
return [];
|
|
163
|
+
const data = JSON.parse(content);
|
|
164
|
+
return data.filter(item => item.toolName === toolName &&
|
|
165
|
+
(!sessionId || item.sessionId === sessionId || item.sessionId === this._sessionId));
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Query interactions by element ref
|
|
169
|
+
*/
|
|
170
|
+
async queryByElementRef(ref, sessionId) {
|
|
171
|
+
await this.flushSync();
|
|
172
|
+
// Try MongoDB first
|
|
173
|
+
if (this._useMongo && this._mongoLogger) {
|
|
174
|
+
return await this._mongoLogger.queryByElementRef(ref, sessionId);
|
|
175
|
+
}
|
|
176
|
+
// Fall back to JSON file
|
|
177
|
+
if (!fs.existsSync(this._dbFile))
|
|
178
|
+
return [];
|
|
179
|
+
const content = await fs.promises.readFile(this._dbFile, 'utf-8');
|
|
180
|
+
if (!content.trim())
|
|
181
|
+
return [];
|
|
182
|
+
const data = JSON.parse(content);
|
|
183
|
+
return data.filter(item => item.elementRef === ref &&
|
|
184
|
+
(!sessionId || item.sessionId === sessionId || item.sessionId === this._sessionId));
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Query interactions by URL
|
|
188
|
+
*/
|
|
189
|
+
async queryByUrl(url, sessionId) {
|
|
190
|
+
await this.flushSync();
|
|
191
|
+
// Try MongoDB first
|
|
192
|
+
if (this._useMongo && this._mongoLogger) {
|
|
193
|
+
return await this._mongoLogger.queryByUrl(url, sessionId);
|
|
194
|
+
}
|
|
195
|
+
// Fall back to JSON file
|
|
196
|
+
if (!fs.existsSync(this._dbFile))
|
|
197
|
+
return [];
|
|
198
|
+
const content = await fs.promises.readFile(this._dbFile, 'utf-8');
|
|
199
|
+
if (!content.trim())
|
|
200
|
+
return [];
|
|
201
|
+
const data = JSON.parse(content);
|
|
202
|
+
return data.filter(item => item.url.includes(url) &&
|
|
203
|
+
(!sessionId || item.sessionId === sessionId || item.sessionId === this._sessionId));
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Get all interactions
|
|
207
|
+
*/
|
|
208
|
+
async getAllInteractions(sessionId) {
|
|
209
|
+
await this.flushSync();
|
|
210
|
+
// Try MongoDB first
|
|
211
|
+
if (this._useMongo && this._mongoLogger) {
|
|
212
|
+
return await this._mongoLogger.getAllInteractions(sessionId);
|
|
213
|
+
}
|
|
214
|
+
// Fall back to JSON file
|
|
215
|
+
if (!fs.existsSync(this._dbFile))
|
|
216
|
+
return [];
|
|
217
|
+
const content = await fs.promises.readFile(this._dbFile, 'utf-8');
|
|
218
|
+
if (!content.trim())
|
|
219
|
+
return [];
|
|
220
|
+
const data = JSON.parse(content);
|
|
221
|
+
if (!sessionId)
|
|
222
|
+
return data.filter(item => item.sessionId === this._sessionId);
|
|
223
|
+
return data.filter(item => item.sessionId === sessionId);
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* Get interaction statistics
|
|
227
|
+
*/
|
|
228
|
+
async getStatistics(sessionId) {
|
|
229
|
+
// Try MongoDB first
|
|
230
|
+
if (this._useMongo && this._mongoLogger) {
|
|
231
|
+
return await this._mongoLogger.getStatistics(sessionId);
|
|
232
|
+
}
|
|
233
|
+
// Fall back to calculating from JSON file
|
|
234
|
+
const interactions = await this.getAllInteractions(sessionId);
|
|
235
|
+
const toolCounts = {};
|
|
236
|
+
const elementCounts = {};
|
|
237
|
+
const urlCounts = {};
|
|
238
|
+
interactions.forEach(interaction => {
|
|
239
|
+
toolCounts[interaction.toolName] = (toolCounts[interaction.toolName] || 0) + 1;
|
|
240
|
+
elementCounts[interaction.elementRef] = (elementCounts[interaction.elementRef] || 0) + 1;
|
|
241
|
+
urlCounts[interaction.url] = (urlCounts[interaction.url] || 0) + 1;
|
|
242
|
+
});
|
|
243
|
+
return {
|
|
244
|
+
totalInteractions: interactions.length,
|
|
245
|
+
toolCounts,
|
|
246
|
+
elementCounts,
|
|
247
|
+
urlCounts,
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
251
|
+
* Clear all data from the database
|
|
252
|
+
*/
|
|
253
|
+
async clearDatabase() {
|
|
254
|
+
await this.flushSync();
|
|
255
|
+
await fs.promises.writeFile(this._dbFile, JSON.stringify([], null, 2));
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* Create a database logger instance
|
|
260
|
+
*/
|
|
261
|
+
export function createDatabaseLogger(outputFolder, mongoConfig) {
|
|
262
|
+
const dbPath = path.join(outputFolder, 'interactions.json');
|
|
263
|
+
return new DatabaseLogger(dbPath, mongoConfig);
|
|
264
|
+
}
|