diff --git a/scripts/mcp-tools.test.mjs b/scripts/mcp-tools.test.mjs index 8cc5f88..cafc78e 100644 --- a/scripts/mcp-tools.test.mjs +++ b/scripts/mcp-tools.test.mjs @@ -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 }); @@ -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); +}); diff --git a/weboperator-bridge/bridge.js b/weboperator-bridge/bridge.js index a1e6826..b54c182 100755 --- a/weboperator-bridge/bridge.js +++ b/weboperator-bridge/bridge.js @@ -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) { @@ -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') { diff --git a/weboperator-bridge/mcp-server.js b/weboperator-bridge/mcp-server.js index 15b85b1..a455c37 100755 --- a/weboperator-bridge/mcp-server.js +++ b/weboperator-bridge/mcp-server.js @@ -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); @@ -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); diff --git a/weboperator-bridge/tools.js b/weboperator-bridge/tools.js index 7bc804b..9225024 100644 --- a/weboperator-bridge/tools.js +++ b/weboperator-bridge/tools.js @@ -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 };