@wordbricks/playwright-mcp 0.1.7 → 0.1.8
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/config.d.ts +5 -0
- package/lib/browserContextFactory.js +16 -3
- package/lib/config.js +2 -0
- package/lib/program.js +1 -0
- package/lib/tab.js +1 -1
- package/package.json +5 -4
- package/lib/loop/loop.js +0 -69
- package/lib/loop/loopClaude.js +0 -152
- package/lib/loop/loopOpenAI.js +0 -141
- package/lib/loop/main.js +0 -60
package/config.d.ts
CHANGED
|
@@ -63,6 +63,11 @@ export type Config = {
|
|
|
63
63
|
* Remote endpoint to connect to an existing Playwright server.
|
|
64
64
|
*/
|
|
65
65
|
remoteEndpoint?: string;
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Path to a JavaScript file to inject into all pages using addInitScript.
|
|
69
|
+
*/
|
|
70
|
+
initScript?: string;
|
|
66
71
|
},
|
|
67
72
|
|
|
68
73
|
server?: {
|
|
@@ -27,6 +27,12 @@ import { createHash } from './utils/guid.js';
|
|
|
27
27
|
import { outputFile } from './config.js';
|
|
28
28
|
import { extensionPath } from './utils/extensionPath.js';
|
|
29
29
|
const TIMEOUT_STR = '30m';
|
|
30
|
+
async function applyInitScript(browserContext, config) {
|
|
31
|
+
if (config.browser.initScript) {
|
|
32
|
+
const scriptContent = await fs.promises.readFile(config.browser.initScript, 'utf8');
|
|
33
|
+
await browserContext.addInitScript(scriptContent);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
30
36
|
export function contextFactory(config) {
|
|
31
37
|
if (config.browser.remoteEndpoint)
|
|
32
38
|
return new RemoteContextFactory(config);
|
|
@@ -138,7 +144,9 @@ class IsolatedContextFactory extends BaseContextFactory {
|
|
|
138
144
|
});
|
|
139
145
|
}
|
|
140
146
|
async _doCreateContext(browser) {
|
|
141
|
-
|
|
147
|
+
const browserContext = await browser.newContext(this.config.browser.contextOptions);
|
|
148
|
+
await applyInitScript(browserContext, this.config);
|
|
149
|
+
return browserContext;
|
|
142
150
|
}
|
|
143
151
|
}
|
|
144
152
|
class CdpContextFactory extends BaseContextFactory {
|
|
@@ -149,7 +157,9 @@ class CdpContextFactory extends BaseContextFactory {
|
|
|
149
157
|
return playwright.chromium.connectOverCDP(this.config.browser.cdpEndpoint);
|
|
150
158
|
}
|
|
151
159
|
async _doCreateContext(browser) {
|
|
152
|
-
|
|
160
|
+
const browserContext = this.config.browser.isolated ? await browser.newContext() : browser.contexts()[0];
|
|
161
|
+
await applyInitScript(browserContext, this.config);
|
|
162
|
+
return browserContext;
|
|
153
163
|
}
|
|
154
164
|
}
|
|
155
165
|
class RemoteContextFactory extends BaseContextFactory {
|
|
@@ -164,7 +174,9 @@ class RemoteContextFactory extends BaseContextFactory {
|
|
|
164
174
|
return playwright[this.config.browser.browserName].connect(String(url));
|
|
165
175
|
}
|
|
166
176
|
async _doCreateContext(browser) {
|
|
167
|
-
|
|
177
|
+
const browserContext = await browser.newContext();
|
|
178
|
+
await applyInitScript(browserContext, this.config);
|
|
179
|
+
return browserContext;
|
|
168
180
|
}
|
|
169
181
|
}
|
|
170
182
|
class PersistentContextFactory {
|
|
@@ -199,6 +211,7 @@ class PersistentContextFactory {
|
|
|
199
211
|
handleSIGTERM: false,
|
|
200
212
|
args,
|
|
201
213
|
});
|
|
214
|
+
await applyInitScript(browserContext, this.config);
|
|
202
215
|
// Start auto-close timer
|
|
203
216
|
this._startAutoCloseTimer(browserContext);
|
|
204
217
|
const close = () => this._closeBrowserContext(browserContext, userDataDir);
|
package/lib/config.js
CHANGED
|
@@ -120,6 +120,7 @@ export function configFromCLIOptions(cliOptions) {
|
|
|
120
120
|
launchOptions,
|
|
121
121
|
contextOptions,
|
|
122
122
|
cdpEndpoint: cliOptions.cdpEndpoint,
|
|
123
|
+
initScript: cliOptions.initScript,
|
|
123
124
|
},
|
|
124
125
|
server: {
|
|
125
126
|
port: cliOptions.port,
|
|
@@ -151,6 +152,7 @@ function configFromEnv() {
|
|
|
151
152
|
options.headless = envToBoolean(process.env.PLAYWRIGHT_MCP_HEADLESS);
|
|
152
153
|
options.host = envToString(process.env.PLAYWRIGHT_MCP_HOST);
|
|
153
154
|
options.ignoreHttpsErrors = envToBoolean(process.env.PLAYWRIGHT_MCP_IGNORE_HTTPS_ERRORS);
|
|
155
|
+
options.initScript = envToString(process.env.PLAYWRIGHT_MCP_INIT_SCRIPT);
|
|
154
156
|
options.isolated = envToBoolean(process.env.PLAYWRIGHT_MCP_ISOLATED);
|
|
155
157
|
if (process.env.PLAYWRIGHT_MCP_IMAGE_RESPONSES === 'omit')
|
|
156
158
|
options.imageResponses = 'omit';
|
package/lib/program.js
CHANGED
|
@@ -39,6 +39,7 @@ program
|
|
|
39
39
|
.option('--headless', 'run browser in headless mode, headed by default')
|
|
40
40
|
.option('--host <host>', 'host to bind server to. Default is localhost. Use 0.0.0.0 to bind to all interfaces.')
|
|
41
41
|
.option('--ignore-https-errors', 'ignore https errors')
|
|
42
|
+
.option('--init-script <path>', 'path to a JavaScript file to inject into all pages using addInitScript.')
|
|
42
43
|
.option('--isolated', 'keep the browser profile in memory, do not save it to disk.')
|
|
43
44
|
.option('--image-responses <mode>', 'whether to send image responses to the client. Can be "allow" or "omit", Defaults to "allow".')
|
|
44
45
|
.option('--no-sandbox', 'disable the sandbox for all process types that are normally sandboxed.')
|
package/lib/tab.js
CHANGED
|
@@ -203,7 +203,7 @@ export class Tab extends EventEmitter {
|
|
|
203
203
|
// This avoids invalidating refs obtained from browser_get_snapshot while still
|
|
204
204
|
// initializing the mapping when needed (e.g., after navigation).
|
|
205
205
|
await this.page._snapshotForAI();
|
|
206
|
-
return params.map(param => this.page.locator(`aria-ref=${param.ref}`)
|
|
206
|
+
return params.map(param => this.page.locator(`aria-ref=${param.ref}`));
|
|
207
207
|
}
|
|
208
208
|
async waitForTimeout(time) {
|
|
209
209
|
if (this._javaScriptBlocked()) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wordbricks/playwright-mcp",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.8",
|
|
4
4
|
"description": "Playwright Tools for MCP",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"repository": {
|
|
@@ -60,8 +60,8 @@
|
|
|
60
60
|
"lodash": "^4.17.21",
|
|
61
61
|
"mime": "^4.0.7",
|
|
62
62
|
"ms": "^2.1.3",
|
|
63
|
-
"playwright": "npm:rebrowser-playwright@1.
|
|
64
|
-
"playwright-core": "npm:rebrowser-playwright@1.
|
|
63
|
+
"playwright": "npm:rebrowser-playwright@1.52.0",
|
|
64
|
+
"playwright-core": "npm:rebrowser-playwright@1.52.0",
|
|
65
65
|
"raw-body": "^3.0.0",
|
|
66
66
|
"typescript-parsec": "0.3.4",
|
|
67
67
|
"ws": "^8.18.1",
|
|
@@ -70,9 +70,10 @@
|
|
|
70
70
|
},
|
|
71
71
|
"devDependencies": {
|
|
72
72
|
"@anthropic-ai/sdk": "^0.57.0",
|
|
73
|
+
"@cfworker/json-schema": "^4.1.1",
|
|
73
74
|
"@eslint/eslintrc": "^3.2.0",
|
|
74
75
|
"@eslint/js": "^9.19.0",
|
|
75
|
-
"@playwright/test": "1.
|
|
76
|
+
"@playwright/test": "1.52.0",
|
|
76
77
|
"@stylistic/eslint-plugin": "^3.0.1",
|
|
77
78
|
"@tomjs/unzip-crx": "1.1.3",
|
|
78
79
|
"@types/chrome": "^0.0.315",
|
package/lib/loop/loop.js
DELETED
|
@@ -1,69 +0,0 @@
|
|
|
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
|
-
export async function runTask(delegate, client, task, oneShot = false) {
|
|
18
|
-
const { tools } = await client.listTools();
|
|
19
|
-
const taskContent = oneShot ? `Perform following task: ${task}.` : `Perform following task: ${task}. Once the task is complete, call the "done" tool.`;
|
|
20
|
-
const conversation = delegate.createConversation(taskContent, tools, oneShot);
|
|
21
|
-
for (let iteration = 0; iteration < 5; ++iteration) {
|
|
22
|
-
debug('history')('Making API call for iteration', iteration);
|
|
23
|
-
const toolCalls = await delegate.makeApiCall(conversation);
|
|
24
|
-
if (toolCalls.length === 0)
|
|
25
|
-
throw new Error('Call the "done" tool when the task is complete.');
|
|
26
|
-
const toolResults = [];
|
|
27
|
-
for (const toolCall of toolCalls) {
|
|
28
|
-
const doneResult = delegate.checkDoneToolCall(toolCall);
|
|
29
|
-
if (doneResult !== null)
|
|
30
|
-
return conversation.messages;
|
|
31
|
-
const { name, arguments: args, id } = toolCall;
|
|
32
|
-
try {
|
|
33
|
-
debug('tool')(name, args);
|
|
34
|
-
const response = await client.callTool({
|
|
35
|
-
name,
|
|
36
|
-
arguments: args,
|
|
37
|
-
});
|
|
38
|
-
const responseContent = (response.content || []);
|
|
39
|
-
debug('tool')(responseContent);
|
|
40
|
-
const text = responseContent.filter(part => part.type === 'text').map(part => part.text).join('\n');
|
|
41
|
-
toolResults.push({
|
|
42
|
-
toolCallId: id,
|
|
43
|
-
content: text,
|
|
44
|
-
});
|
|
45
|
-
}
|
|
46
|
-
catch (error) {
|
|
47
|
-
debug('tool')(error);
|
|
48
|
-
toolResults.push({
|
|
49
|
-
toolCallId: id,
|
|
50
|
-
content: `Error while executing tool "${name}": ${error instanceof Error ? error.message : String(error)}\n\nPlease try to recover and complete the task.`,
|
|
51
|
-
isError: true,
|
|
52
|
-
});
|
|
53
|
-
// Skip remaining tool calls for this iteration
|
|
54
|
-
for (const remainingToolCall of toolCalls.slice(toolCalls.indexOf(toolCall) + 1)) {
|
|
55
|
-
toolResults.push({
|
|
56
|
-
toolCallId: remainingToolCall.id,
|
|
57
|
-
content: `This tool call is skipped due to previous error.`,
|
|
58
|
-
isError: true,
|
|
59
|
-
});
|
|
60
|
-
}
|
|
61
|
-
break;
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
delegate.addToolResults(conversation, toolResults);
|
|
65
|
-
if (oneShot)
|
|
66
|
-
return conversation.messages;
|
|
67
|
-
}
|
|
68
|
-
throw new Error('Failed to perform step, max attempts reached');
|
|
69
|
-
}
|
package/lib/loop/loopClaude.js
DELETED
|
@@ -1,152 +0,0 @@
|
|
|
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
|
-
const model = 'claude-sonnet-4-20250514';
|
|
17
|
-
export class ClaudeDelegate {
|
|
18
|
-
_anthropic;
|
|
19
|
-
async anthropic() {
|
|
20
|
-
if (!this._anthropic) {
|
|
21
|
-
const anthropic = await import('@anthropic-ai/sdk');
|
|
22
|
-
this._anthropic = new anthropic.Anthropic();
|
|
23
|
-
}
|
|
24
|
-
return this._anthropic;
|
|
25
|
-
}
|
|
26
|
-
createConversation(task, tools, oneShot) {
|
|
27
|
-
const llmTools = tools.map(tool => ({
|
|
28
|
-
name: tool.name,
|
|
29
|
-
description: tool.description || '',
|
|
30
|
-
inputSchema: tool.inputSchema,
|
|
31
|
-
}));
|
|
32
|
-
if (!oneShot) {
|
|
33
|
-
llmTools.push({
|
|
34
|
-
name: 'done',
|
|
35
|
-
description: 'Call this tool when the task is complete.',
|
|
36
|
-
inputSchema: {
|
|
37
|
-
type: 'object',
|
|
38
|
-
properties: {},
|
|
39
|
-
},
|
|
40
|
-
});
|
|
41
|
-
}
|
|
42
|
-
return {
|
|
43
|
-
messages: [{
|
|
44
|
-
role: 'user',
|
|
45
|
-
content: task
|
|
46
|
-
}],
|
|
47
|
-
tools: llmTools,
|
|
48
|
-
};
|
|
49
|
-
}
|
|
50
|
-
async makeApiCall(conversation) {
|
|
51
|
-
// Convert generic messages to Claude format
|
|
52
|
-
const claudeMessages = [];
|
|
53
|
-
for (const message of conversation.messages) {
|
|
54
|
-
if (message.role === 'user') {
|
|
55
|
-
claudeMessages.push({
|
|
56
|
-
role: 'user',
|
|
57
|
-
content: message.content
|
|
58
|
-
});
|
|
59
|
-
}
|
|
60
|
-
else if (message.role === 'assistant') {
|
|
61
|
-
const content = [];
|
|
62
|
-
// Add text content
|
|
63
|
-
if (message.content) {
|
|
64
|
-
content.push({
|
|
65
|
-
type: 'text',
|
|
66
|
-
text: message.content,
|
|
67
|
-
citations: []
|
|
68
|
-
});
|
|
69
|
-
}
|
|
70
|
-
// Add tool calls
|
|
71
|
-
if (message.toolCalls) {
|
|
72
|
-
for (const toolCall of message.toolCalls) {
|
|
73
|
-
content.push({
|
|
74
|
-
type: 'tool_use',
|
|
75
|
-
id: toolCall.id,
|
|
76
|
-
name: toolCall.name,
|
|
77
|
-
input: toolCall.arguments
|
|
78
|
-
});
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
claudeMessages.push({
|
|
82
|
-
role: 'assistant',
|
|
83
|
-
content
|
|
84
|
-
});
|
|
85
|
-
}
|
|
86
|
-
else if (message.role === 'tool') {
|
|
87
|
-
// Tool results are added differently - we need to find if there's already a user message with tool results
|
|
88
|
-
const lastMessage = claudeMessages[claudeMessages.length - 1];
|
|
89
|
-
const toolResult = {
|
|
90
|
-
type: 'tool_result',
|
|
91
|
-
tool_use_id: message.toolCallId,
|
|
92
|
-
content: message.content,
|
|
93
|
-
is_error: message.isError,
|
|
94
|
-
};
|
|
95
|
-
if (lastMessage && lastMessage.role === 'user' && Array.isArray(lastMessage.content)) {
|
|
96
|
-
// Add to existing tool results message
|
|
97
|
-
lastMessage.content.push(toolResult);
|
|
98
|
-
}
|
|
99
|
-
else {
|
|
100
|
-
// Create new tool results message
|
|
101
|
-
claudeMessages.push({
|
|
102
|
-
role: 'user',
|
|
103
|
-
content: [toolResult]
|
|
104
|
-
});
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
// Convert generic tools to Claude format
|
|
109
|
-
const claudeTools = conversation.tools.map(tool => ({
|
|
110
|
-
name: tool.name,
|
|
111
|
-
description: tool.description,
|
|
112
|
-
input_schema: tool.inputSchema,
|
|
113
|
-
}));
|
|
114
|
-
const anthropic = await this.anthropic();
|
|
115
|
-
const response = await anthropic.messages.create({
|
|
116
|
-
model,
|
|
117
|
-
max_tokens: 10000,
|
|
118
|
-
messages: claudeMessages,
|
|
119
|
-
tools: claudeTools,
|
|
120
|
-
});
|
|
121
|
-
// Extract tool calls and add assistant message to generic conversation
|
|
122
|
-
const toolCalls = response.content.filter(block => block.type === 'tool_use');
|
|
123
|
-
const textContent = response.content.filter(block => block.type === 'text').map(block => block.text).join('');
|
|
124
|
-
const llmToolCalls = toolCalls.map(toolCall => ({
|
|
125
|
-
name: toolCall.name,
|
|
126
|
-
arguments: toolCall.input,
|
|
127
|
-
id: toolCall.id,
|
|
128
|
-
}));
|
|
129
|
-
// Add assistant message to generic conversation
|
|
130
|
-
conversation.messages.push({
|
|
131
|
-
role: 'assistant',
|
|
132
|
-
content: textContent,
|
|
133
|
-
toolCalls: llmToolCalls.length > 0 ? llmToolCalls : undefined
|
|
134
|
-
});
|
|
135
|
-
return llmToolCalls;
|
|
136
|
-
}
|
|
137
|
-
addToolResults(conversation, results) {
|
|
138
|
-
for (const result of results) {
|
|
139
|
-
conversation.messages.push({
|
|
140
|
-
role: 'tool',
|
|
141
|
-
toolCallId: result.toolCallId,
|
|
142
|
-
content: result.content,
|
|
143
|
-
isError: result.isError,
|
|
144
|
-
});
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
checkDoneToolCall(toolCall) {
|
|
148
|
-
if (toolCall.name === 'done')
|
|
149
|
-
return toolCall.arguments.result;
|
|
150
|
-
return null;
|
|
151
|
-
}
|
|
152
|
-
}
|
package/lib/loop/loopOpenAI.js
DELETED
|
@@ -1,141 +0,0 @@
|
|
|
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
|
-
const model = 'gpt-4.1';
|
|
17
|
-
export class OpenAIDelegate {
|
|
18
|
-
_openai;
|
|
19
|
-
async openai() {
|
|
20
|
-
if (!this._openai) {
|
|
21
|
-
const oai = await import('openai');
|
|
22
|
-
this._openai = new oai.OpenAI();
|
|
23
|
-
}
|
|
24
|
-
return this._openai;
|
|
25
|
-
}
|
|
26
|
-
createConversation(task, tools, oneShot) {
|
|
27
|
-
const genericTools = tools.map(tool => ({
|
|
28
|
-
name: tool.name,
|
|
29
|
-
description: tool.description || '',
|
|
30
|
-
inputSchema: tool.inputSchema,
|
|
31
|
-
}));
|
|
32
|
-
if (!oneShot) {
|
|
33
|
-
genericTools.push({
|
|
34
|
-
name: 'done',
|
|
35
|
-
description: 'Call this tool when the task is complete.',
|
|
36
|
-
inputSchema: {
|
|
37
|
-
type: 'object',
|
|
38
|
-
properties: {},
|
|
39
|
-
},
|
|
40
|
-
});
|
|
41
|
-
}
|
|
42
|
-
return {
|
|
43
|
-
messages: [{
|
|
44
|
-
role: 'user',
|
|
45
|
-
content: task
|
|
46
|
-
}],
|
|
47
|
-
tools: genericTools,
|
|
48
|
-
};
|
|
49
|
-
}
|
|
50
|
-
async makeApiCall(conversation) {
|
|
51
|
-
// Convert generic messages to OpenAI format
|
|
52
|
-
const openaiMessages = [];
|
|
53
|
-
for (const message of conversation.messages) {
|
|
54
|
-
if (message.role === 'user') {
|
|
55
|
-
openaiMessages.push({
|
|
56
|
-
role: 'user',
|
|
57
|
-
content: message.content
|
|
58
|
-
});
|
|
59
|
-
}
|
|
60
|
-
else if (message.role === 'assistant') {
|
|
61
|
-
const toolCalls = [];
|
|
62
|
-
if (message.toolCalls) {
|
|
63
|
-
for (const toolCall of message.toolCalls) {
|
|
64
|
-
toolCalls.push({
|
|
65
|
-
id: toolCall.id,
|
|
66
|
-
type: 'function',
|
|
67
|
-
function: {
|
|
68
|
-
name: toolCall.name,
|
|
69
|
-
arguments: JSON.stringify(toolCall.arguments)
|
|
70
|
-
}
|
|
71
|
-
});
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
const assistantMessage = {
|
|
75
|
-
role: 'assistant'
|
|
76
|
-
};
|
|
77
|
-
if (message.content)
|
|
78
|
-
assistantMessage.content = message.content;
|
|
79
|
-
if (toolCalls.length > 0)
|
|
80
|
-
assistantMessage.tool_calls = toolCalls;
|
|
81
|
-
openaiMessages.push(assistantMessage);
|
|
82
|
-
}
|
|
83
|
-
else if (message.role === 'tool') {
|
|
84
|
-
openaiMessages.push({
|
|
85
|
-
role: 'tool',
|
|
86
|
-
tool_call_id: message.toolCallId,
|
|
87
|
-
content: message.content,
|
|
88
|
-
});
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
// Convert generic tools to OpenAI format
|
|
92
|
-
const openaiTools = conversation.tools.map(tool => ({
|
|
93
|
-
type: 'function',
|
|
94
|
-
function: {
|
|
95
|
-
name: tool.name,
|
|
96
|
-
description: tool.description,
|
|
97
|
-
parameters: tool.inputSchema,
|
|
98
|
-
},
|
|
99
|
-
}));
|
|
100
|
-
const openai = await this.openai();
|
|
101
|
-
const response = await openai.chat.completions.create({
|
|
102
|
-
model,
|
|
103
|
-
messages: openaiMessages,
|
|
104
|
-
tools: openaiTools,
|
|
105
|
-
tool_choice: 'auto'
|
|
106
|
-
});
|
|
107
|
-
const message = response.choices[0].message;
|
|
108
|
-
// Extract tool calls and add assistant message to generic conversation
|
|
109
|
-
const toolCalls = message.tool_calls || [];
|
|
110
|
-
const genericToolCalls = toolCalls.map(toolCall => {
|
|
111
|
-
const functionCall = toolCall.function;
|
|
112
|
-
return {
|
|
113
|
-
name: functionCall.name,
|
|
114
|
-
arguments: JSON.parse(functionCall.arguments),
|
|
115
|
-
id: toolCall.id,
|
|
116
|
-
};
|
|
117
|
-
});
|
|
118
|
-
// Add assistant message to generic conversation
|
|
119
|
-
conversation.messages.push({
|
|
120
|
-
role: 'assistant',
|
|
121
|
-
content: message.content || '',
|
|
122
|
-
toolCalls: genericToolCalls.length > 0 ? genericToolCalls : undefined
|
|
123
|
-
});
|
|
124
|
-
return genericToolCalls;
|
|
125
|
-
}
|
|
126
|
-
addToolResults(conversation, results) {
|
|
127
|
-
for (const result of results) {
|
|
128
|
-
conversation.messages.push({
|
|
129
|
-
role: 'tool',
|
|
130
|
-
toolCallId: result.toolCallId,
|
|
131
|
-
content: result.content,
|
|
132
|
-
isError: result.isError,
|
|
133
|
-
});
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
checkDoneToolCall(toolCall) {
|
|
137
|
-
if (toolCall.name === 'done')
|
|
138
|
-
return toolCall.arguments.result;
|
|
139
|
-
return null;
|
|
140
|
-
}
|
|
141
|
-
}
|
package/lib/loop/main.js
DELETED
|
@@ -1,60 +0,0 @@
|
|
|
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
|
-
/* eslint-disable no-console */
|
|
17
|
-
import path from 'path';
|
|
18
|
-
import url from 'url';
|
|
19
|
-
import dotenv from 'dotenv';
|
|
20
|
-
import { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio.js';
|
|
21
|
-
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
|
|
22
|
-
import { program } from 'commander';
|
|
23
|
-
import { OpenAIDelegate } from './loopOpenAI.js';
|
|
24
|
-
import { ClaudeDelegate } from './loopClaude.js';
|
|
25
|
-
import { runTask } from './loop.js';
|
|
26
|
-
dotenv.config();
|
|
27
|
-
const __filename = url.fileURLToPath(import.meta.url);
|
|
28
|
-
async function run(delegate) {
|
|
29
|
-
const transport = new StdioClientTransport({
|
|
30
|
-
command: 'node',
|
|
31
|
-
args: [
|
|
32
|
-
path.resolve(__filename, '../../../cli.js'),
|
|
33
|
-
'--save-session',
|
|
34
|
-
'--output-dir', path.resolve(__filename, '../../../sessions')
|
|
35
|
-
],
|
|
36
|
-
stderr: 'inherit',
|
|
37
|
-
env: process.env,
|
|
38
|
-
});
|
|
39
|
-
const client = new Client({ name: 'test', version: '1.0.0' });
|
|
40
|
-
await client.connect(transport);
|
|
41
|
-
await client.ping();
|
|
42
|
-
for (const task of tasks) {
|
|
43
|
-
const messages = await runTask(delegate, client, task);
|
|
44
|
-
for (const message of messages)
|
|
45
|
-
console.log(`${message.role}: ${message.content}`);
|
|
46
|
-
}
|
|
47
|
-
await client.close();
|
|
48
|
-
}
|
|
49
|
-
const tasks = [
|
|
50
|
-
'Open https://playwright.dev/',
|
|
51
|
-
];
|
|
52
|
-
program
|
|
53
|
-
.option('--model <model>', 'model to use')
|
|
54
|
-
.action(async (options) => {
|
|
55
|
-
if (options.model === 'claude')
|
|
56
|
-
await run(new ClaudeDelegate());
|
|
57
|
-
else
|
|
58
|
-
await run(new OpenAIDelegate());
|
|
59
|
-
});
|
|
60
|
-
void program.parseAsync(process.argv);
|