Skip to content

Commit 063a2f4

Browse files
committed
fix(ai): make the editor tools discoverable and state what the user is on
The system prompt told the model to call getEditorState first and to reach for takeScreenshot / execJsInLivePreview "listed below", but none of the phoenix-editor tools were in its tool list: they sat behind ToolSearch. It was being told to use tools it could not see, so it fell back to what it could — an Explore subagent grepping the project — and only discovered the editor tools later, usually to verify work already finished. - alwaysLoad on the four "look at what the user is looking at" tools: getEditorState, takeScreenshot, execJsInLivePreview, controlEditor. ~1.2k tokens on the cached system block; one ToolSearch round-trip costs a whole model turn, so it pays for itself immediately. - searchHint on all ten, worded in the terms someone would search with (browser, rendered page, DOM, screenshot, responsive) rather than the "live preview" jargon, so the six deferred ones stay findable. - Prepend one line naming the active file, the unsaved buffers and what the live preview renders. The panel assembles it, since the data and the context chips both live there; this side only renders the prose. Says plainly when a list is complete, because told only that a list *can* be truncated the model re-checks with getEditorState — the exact lookup this is meant to save. Measured on the same prompt against a page with a runtime-only bug: baseline +alwaysLoad +both ToolSearch calls 2 0 0 first move Explore↴ Explore↴ the right file model turns 10 9 6-9 wall time 85.5s 76.1s 17-27s cost $0.2335 $0.2960 $0.08-0.11 The line itself costs ~106 tokens typically, ~195 worst case.
1 parent e0fe117 commit 063a2f4

2 files changed

Lines changed: 86 additions & 9 deletions

File tree

‎src-node/claude-code-agent.js‎

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,39 @@ function _formatAnswers(answer) {
315315
return answerText.trim();
316316
}
317317

318+
/**
319+
* Render the editor context the panel sent into the line prepended to the
320+
* prompt. The panel assembles it because that is where the data and the
321+
* user's context chips already live; this only turns it into prose. Returns
322+
* "" when the panel sent nothing, i.e. the user dismissed those chips.
323+
*/
324+
function _buildEditorContextLine(ctx) {
325+
if (!ctx || (!ctx.activeFile && !ctx.livePreviewFile)) {
326+
return "";
327+
}
328+
const parts = ["Editor state (auto-supplied, no tool call needed):"];
329+
if (ctx.activeFile) {
330+
parts.push("the user is editing " + ctx.activeFile + ".");
331+
if (ctx.unsaved) {
332+
parts.push("Unsaved, so stale on disk: " + ctx.unsaved + ".");
333+
}
334+
}
335+
if (ctx.livePreviewFile) {
336+
parts.push(ctx.livePreviewFile === ctx.activeFile
337+
? "The live preview is showing that same file."
338+
: "The live preview is showing " + ctx.livePreviewFile + ".");
339+
}
340+
// Say plainly when the lists are complete. Left merely to infer it, the
341+
// agent calls getEditorState to check — the exact lookup this line is
342+
// here to save.
343+
parts.push(ctx.truncated
344+
? "Trust this over searching for it yourself; call getEditorState for the names cut " +
345+
"from a list, or if you need the cursor, the selection or a fresher view."
346+
: "That is the complete set. Trust it over searching or double-checking; call " +
347+
"getEditorState only if you need the cursor, the selection or a fresher view.");
348+
return parts.join(" ");
349+
}
350+
318351
/**
319352
* Detect whether a PostToolUse `tool_response` represents an error result.
320353
* Used to suppress diff-card painting when the SDK's native Edit/Write itself
@@ -855,7 +888,8 @@ exports.checkAvailability = async function (opts) {
855888
* aiProgress, aiTextStream, aiToolEdit, aiError, aiComplete
856889
*/
857890
exports.sendPrompt = async function (params) {
858-
const { prompt, projectPath, sessionAction, model, locale, selectionContext, images, envOverrides, permissionMode, additionalDirectories } = params;
891+
const { prompt, projectPath, sessionAction, model, locale, selectionContext, editorContext,
892+
images, envOverrides, permissionMode, additionalDirectories } = params;
859893
const requestId = Date.now().toString(36) + Math.random().toString(36).slice(2, 7);
860894

861895
// Handle session
@@ -874,6 +908,12 @@ exports.sendPrompt = async function (params) {
874908

875909
currentAbortController = new AbortController();
876910

911+
// Prepend what the user is looking at. The panel knows the active file,
912+
// the unsaved buffers and the live preview target for certain, so stating
913+
// them costs one line and removes the reason to go hunting: without it
914+
// the model opens by grepping the project for a file already on screen.
915+
const editorContextLine = _buildEditorContextLine(editorContext);
916+
877917
// Prepend selection context to the prompt if available
878918
let enrichedPrompt = prompt;
879919
if (selectionContext) {
@@ -896,6 +936,9 @@ exports.sendPrompt = async function (params) {
896936
" to read the selected content if needed." + previewSnippet + "\n" + prompt;
897937
}
898938
}
939+
if (editorContextLine) {
940+
enrichedPrompt = editorContextLine + "\n\n" + enrichedPrompt;
941+
}
899942

900943
// Run the query asynchronously — don't await here so we return requestId immediately
901944
_runQuery(requestId, enrichedPrompt, projectPath, model, currentAbortController.signal, locale, images, envOverrides, permissionMode, additionalDirectories)

‎src-node/mcp-editor-tools.js‎

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,11 @@ function createEditorMcpServer(sdkModule, nodeConnector, clarificationAccessors)
154154
}
155155
return _maybeAppendHint(result, hasClarification);
156156
},
157-
{ annotations: { readOnlyHint: true } }
157+
{
158+
annotations: { readOnlyHint: true },
159+
alwaysLoad: true,
160+
searchHint: "which file the user has open in Phoenix Code editor, plus cursor, selection, and what the live preview (an embedded browser rendering their HTML or Markdown) is showing"
161+
}
158162
);
159163

160164
const takeScreenshotTool = sdkModule.tool(
@@ -219,7 +223,11 @@ function createEditorMcpServer(sdkModule, nodeConnector, clarificationAccessors)
219223
}
220224
return _maybeAppendHint(toolResult, hasClarification);
221225
},
222-
{ annotations: { readOnlyHint: true } }
226+
{
227+
annotations: { readOnlyHint: true },
228+
alwaysLoad: true,
229+
searchHint: "screenshot the user's Phoenix Code editor app window, or the page rendered in their live preview browser"
230+
}
223231
);
224232

225233
const execJsInLivePreviewTool = sdkModule.tool(
@@ -265,7 +273,11 @@ function createEditorMcpServer(sdkModule, nodeConnector, clarificationAccessors)
265273
}
266274
return _maybeAppendHint(toolResult, hasClarification);
267275
},
268-
{ annotations: { readOnlyHint: true } }
276+
{
277+
annotations: { readOnlyHint: true },
278+
alwaysLoad: true,
279+
searchHint: "run JS in the user's live preview browser to inspect the rendered page's DOM, console or JS state"
280+
}
269281
);
270282

271283
const controlEditorTool = sdkModule.tool(
@@ -330,7 +342,11 @@ function createEditorMcpServer(sdkModule, nodeConnector, clarificationAccessors)
330342
};
331343
return _maybeAppendHint(toolResult, hasClarification);
332344
},
333-
{ annotations: { readOnlyHint: true } }
345+
{
346+
annotations: { readOnlyHint: true },
347+
alwaysLoad: true,
348+
searchHint: "open, close or switch files in Phoenix Code, toggle the live preview browser"
349+
}
334350
);
335351

336352
const resizeLivePreviewTool = sdkModule.tool(
@@ -364,7 +380,10 @@ function createEditorMcpServer(sdkModule, nodeConnector, clarificationAccessors)
364380
}
365381
return _maybeAppendHint(toolResult, hasClarification);
366382
},
367-
{ annotations: { readOnlyHint: true } }
383+
{
384+
annotations: { readOnlyHint: true },
385+
searchHint: "resize the user's live preview browser viewport to check a responsive layout"
386+
}
368387
);
369388

370389
const waitTool = sdkModule.tool(
@@ -383,7 +402,10 @@ function createEditorMcpServer(sdkModule, nodeConnector, clarificationAccessors)
383402
};
384403
return _maybeAppendHint(toolResult, hasClarification);
385404
},
386-
{ annotations: { readOnlyHint: true } }
405+
{
406+
annotations: { readOnlyHint: true },
407+
searchHint: "pause before re-checking the user's live preview browser"
408+
}
387409
);
388410

389411
const execJsInEditorTool = sdkModule.tool(
@@ -457,6 +479,9 @@ function createEditorMcpServer(sdkModule, nodeConnector, clarificationAccessors)
457479
};
458480
}
459481
return _maybeAppendHint(toolResult, hasClarification);
482+
},
483+
{
484+
searchHint: "run JS against Phoenix Code's own editor API, not the page in its live preview"
460485
}
461486
);
462487

@@ -527,6 +552,9 @@ function createEditorMcpServer(sdkModule, nodeConnector, clarificationAccessors)
527552
};
528553
}
529554
return _maybeAppendHint(toolResult, hasClarification);
555+
},
556+
{
557+
searchHint: "read or change the user's Phoenix Code editor preferences"
530558
}
531559
);
532560

@@ -574,7 +602,10 @@ function createEditorMcpServer(sdkModule, nodeConnector, clarificationAccessors)
574602
};
575603
return _maybeAppendHint(toolResult, hasClarification);
576604
},
577-
{ annotations: { readOnlyHint: true } }
605+
{
606+
annotations: { readOnlyHint: true },
607+
searchHint: "look up Phoenix Code editor feature or API documentation"
608+
}
578609
);
579610

580611
const getUserClarificationTool = sdkModule.tool(
@@ -607,7 +638,10 @@ function createEditorMcpServer(sdkModule, nodeConnector, clarificationAccessors)
607638
content: [{ type: "text", text: "No clarification queued." }]
608639
};
609640
},
610-
{ annotations: { readOnlyHint: true } }
641+
{
642+
annotations: { readOnlyHint: true },
643+
searchHint: "read a follow-up the user typed into this conversation while you were still working"
644+
}
611645
);
612646

613647
return sdkModule.createSdkMcpServer({

0 commit comments

Comments
 (0)