Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion scripts/mcp-tools.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { test } from 'node:test';
import assert from 'node:assert/strict';
import { createRequire } from 'node:module';
const require = createRequire(import.meta.url);
const { imageContent } = require('../weboperator-bridge/tools.js');
const { imageContent, formatTaskResultForAgent } = require('../weboperator-bridge/tools.js');

test('image bytes are MCP image content and are not repeated in the text block', () => {
const content = imageContent({ kind: 'image', mimeType: 'image/png', base64: 'aW1hZ2U=', width: 2 });
Expand All @@ -11,3 +11,14 @@ test('image bytes are MCP image content and are not repeated in the text block',
assert(!content[1].text.includes('aW1hZ2U='));
for (const result of [null, {}, { mimeType: 'text/html', base64: 'data' }]) assert.throws(() => imageContent(result), /Expected/);
});

test('a finished task is summarized from its done step, then from extracted data', () => {
const done = formatTaskResultForAgent({ id: 't1', status: 'done', goal: 'g', steps: [
{ toolCall: { name: 'extract', arguments: {} }, result: { extracted: 'rows' } },
{ toolCall: { name: 'done', arguments: { summary: 'All 50 titles' } } },
] });
assert.deepEqual([done.ok, done.taskId, done.answer, done.stepCount], [true, 't1', 'All 50 titles', 2]);
assert.equal(formatTaskResultForAgent({ status: 'failed', goal: 'g', steps: [{ result: { extracted: 'partial' } }] }).answer, 'partial');
assert.equal(formatTaskResultForAgent({ status: 'failed', goal: 'g', error: 'boom', steps: [] }).answer, 'Goal failed: boom');
assert.equal(formatTaskResultForAgent(null).ok, false);
});
64 changes: 1 addition & 63 deletions weboperator-bridge/bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ function sendAgentFrame(socket, obj) {
socket.write(Buffer.concat([header, body]));
}

const { TOOLS, TOOL_TYPES, imageContent } = require('./tools');
const { TOOLS, TOOL_TYPES, imageContent, formatTaskResultForAgent } = require('./tools');
const AGENT_TOOLS = TOOLS.map(({ name, description, inputSchema }) => ({ type: 'function', function: { name, description, parameters: inputSchema } }));

async function executeToolByName(name, args = {}, timeoutMs = 30_000) {
Expand All @@ -545,68 +545,6 @@ async function executeToolByName(name, args = {}, timeoutMs = 30_000) {
}
}

function formatTaskResultForAgent(task) {
if (!task) return { ok: false, status: 'failed', error: 'Task not found or timed out' };

const steps = Array.isArray(task.steps) ? task.steps : [];
let answer = '';
const extractedList = [];

for (const step of steps) {
if (step.toolCall) {
const args = step.toolCall.arguments || {};
if (step.toolCall.name === 'done') {
if (args.answer || args.text || args.note || args.summary) {
answer = String(args.answer || args.text || args.note || args.summary);
}
}
if (step.toolCall.name === 'extract' && args.instruction) {
if (step.result && step.result.extracted) {
extractedList.push(step.result.extracted);
}
}
}
if (step.result && step.result.extracted !== undefined) {
extractedList.push(step.result.extracted);
}
if (!answer && step.note && step.status === 'ok') {
answer = step.note;
}
}

if (!answer && extractedList.length > 0) {
answer = typeof extractedList[extractedList.length - 1] === 'string'
? extractedList[extractedList.length - 1]
: JSON.stringify(extractedList, null, 2);
}

if (!answer && task.plan && Array.isArray(task.plan.steps)) {
const doneSteps = task.plan.steps.filter((s) => s.status === 'done');
if (doneSteps.length > 0) {
answer = doneSteps.map((s) => `✓ ${s.description}`).join('\n');
}
}

if (!answer && task.status === 'done') {
answer = `Goal completed successfully: "${task.goal}"`;
} else if (!answer && task.status === 'failed') {
answer = `Goal failed: ${task.error || 'Unknown error'}`;
}

return {
ok: task.status === 'done',
taskId: task.taskId || task.id,
status: task.status,
goal: task.goal,
answer,
extracted: extractedList.length > 0 ? extractedList : undefined,
stepCount: steps.length,
error: task.error,
modelUsed: task.modelUsed,
};
}


async function handleMcpHttp(msg) {
const { id, method, params } = msg || {};
if (method === 'initialize') {
Expand Down
64 changes: 1 addition & 63 deletions weboperator-bridge/mcp-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const API_TOKEN = process.env.WEBOPERATOR_API_TOKEN || '';
// element straight from a snapshot (e.g. "@e12") — including scroll containers,
// which is how a virtualized list (Gmail, chat apps) is scrolled without
// touching the window.
const { TOOLS, TOOL_TYPES, imageContent } = require('./tools');
const { TOOLS, TOOL_TYPES, imageContent, formatTaskResultForAgent } = require('./tools');

process.stdout.on('error', (err) => {
if (err && err.code === 'EPIPE') process.exit(0);
Expand Down Expand Up @@ -392,68 +392,6 @@ async function handleToolCall(name, args) {
}
}

function formatTaskResultForAgent(task) {
if (!task) return { ok: false, status: 'failed', error: 'Task not found or timed out' };

const steps = Array.isArray(task.steps) ? task.steps : [];
let answer = '';
const extractedList = [];

for (const step of steps) {
if (step.toolCall) {
const args = step.toolCall.arguments || {};
if (step.toolCall.name === 'done') {
if (args.answer || args.text || args.note || args.summary) {
answer = String(args.answer || args.text || args.note || args.summary);
}
}
if (step.toolCall.name === 'extract' && args.instruction) {
if (step.result && step.result.extracted) {
extractedList.push(step.result.extracted);
}
}
}
if (step.result && step.result.extracted !== undefined) {
extractedList.push(step.result.extracted);
}
if (!answer && step.note && step.status === 'ok') {
answer = step.note;
}
}

if (!answer && extractedList.length > 0) {
answer = typeof extractedList[extractedList.length - 1] === 'string'
? extractedList[extractedList.length - 1]
: JSON.stringify(extractedList, null, 2);
}

if (!answer && task.plan && Array.isArray(task.plan.steps)) {
const doneSteps = task.plan.steps.filter((s) => s.status === 'done');
if (doneSteps.length > 0) {
answer = doneSteps.map((s) => `✓ ${s.description}`).join('\n');
}
}

if (!answer && task.status === 'done') {
answer = `Goal completed successfully: "${task.goal}"`;
} else if (!answer && task.status === 'failed') {
answer = `Goal failed: ${task.error || 'Unknown error'}`;
}

return {
ok: task.status === 'done',
taskId: task.taskId || task.id,
status: task.status,
goal: task.goal,
answer,
extracted: extractedList.length > 0 ? extractedList : undefined,
stepCount: steps.length,
error: task.error,
modelUsed: task.modelUsed,
};
}


function sendJsonRpc(obj) {
const line = JSON.stringify(obj) + '\n';
process.stdout.write(line);
Expand Down
64 changes: 63 additions & 1 deletion weboperator-bridge/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,4 +291,66 @@ function imageContent(result) {
return [{ type: 'image', data: base64, mimeType: result.mimeType }, { type: 'text', text: JSON.stringify(metadata) }];
}

module.exports = { TOOLS, TOOL_TYPES, imageContent };
/** The agent-facing summary of a finished task, shared by the stdio and HTTP MCP servers. */
function formatTaskResultForAgent(task) {
if (!task) return { ok: false, status: 'failed', error: 'Task not found or timed out' };

const steps = Array.isArray(task.steps) ? task.steps : [];
let answer = '';
const extractedList = [];

for (const step of steps) {
if (step.toolCall) {
const args = step.toolCall.arguments || {};
if (step.toolCall.name === 'done') {
if (args.answer || args.text || args.note || args.summary) {
answer = String(args.answer || args.text || args.note || args.summary);
}
}
if (step.toolCall.name === 'extract' && args.instruction) {
if (step.result && step.result.extracted) {
extractedList.push(step.result.extracted);
}
}
}
if (step.result && step.result.extracted !== undefined) {
extractedList.push(step.result.extracted);
}
if (!answer && step.note && step.status === 'ok') {
answer = step.note;
}
}

if (!answer && extractedList.length > 0) {
answer = typeof extractedList[extractedList.length - 1] === 'string'
? extractedList[extractedList.length - 1]
: JSON.stringify(extractedList, null, 2);
}

if (!answer && task.plan && Array.isArray(task.plan.steps)) {
const doneSteps = task.plan.steps.filter((s) => s.status === 'done');
if (doneSteps.length > 0) {
answer = doneSteps.map((s) => `✓ ${s.description}`).join('\n');
}
}

if (!answer && task.status === 'done') {
answer = `Goal completed successfully: "${task.goal}"`;
} else if (!answer && task.status === 'failed') {
answer = `Goal failed: ${task.error || 'Unknown error'}`;
}

return {
ok: task.status === 'done',
taskId: task.taskId || task.id,
status: task.status,
goal: task.goal,
answer,
extracted: extractedList.length > 0 ? extractedList : undefined,
stepCount: steps.length,
error: task.error,
modelUsed: task.modelUsed,
};
}

module.exports = { TOOLS, TOOL_TYPES, imageContent, formatTaskResultForAgent };
Loading