fix: Validate uniqueness in edit_canvas, fix canvas_css remove schema, clear stale session storage
This commit is contained in:
@@ -76,8 +76,13 @@ function internalClearAllTools() {
|
||||
for (const name of registeredToolsSet) {
|
||||
webmcpInstance.unregisterTool(name)
|
||||
}
|
||||
// Clear session storage to prevent stale schemas
|
||||
if (typeof webmcpInstance._clearStoredItems === 'function') {
|
||||
webmcpInstance._clearStoredItems()
|
||||
}
|
||||
console.log(`[ToolRegistry] Cleared ${registeredToolsSet.size} tools`)
|
||||
registeredToolsSet.clear()
|
||||
toolConfigsCache = null
|
||||
}
|
||||
|
||||
// Tool configurations cache
|
||||
|
||||
@@ -477,43 +477,50 @@ export function createCanvasHandlers(): ToolConfig[] {
|
||||
if (!container) return 'Error: canvas no encontrado'
|
||||
|
||||
const attr = args.attribute || 'innerHTML'
|
||||
const elements = args.all
|
||||
? Array.from(container.querySelectorAll(args.selector))
|
||||
: [container.querySelector(args.selector)].filter(Boolean) as Element[]
|
||||
const allElements = Array.from(container.querySelectorAll(args.selector))
|
||||
|
||||
if (elements.length === 0) {
|
||||
if (allElements.length === 0) {
|
||||
return `Error: no match ${args.selector}`
|
||||
}
|
||||
|
||||
// Find which elements contain old_value
|
||||
const matchingElements = args.old_value
|
||||
? allElements.filter(el => {
|
||||
const current = attr === 'style' ? (el as HTMLElement).style.cssText : (el as any)[attr]
|
||||
return current && current.includes(args.old_value!)
|
||||
})
|
||||
: allElements
|
||||
|
||||
if (args.old_value && matchingElements.length === 0) {
|
||||
return `Error: old_value not found`
|
||||
}
|
||||
|
||||
if (!args.all && matchingElements.length > 1) {
|
||||
return `Error: ${matchingElements.length} matches found. Use all:true or narrow selector.`
|
||||
}
|
||||
|
||||
const elements = args.all ? matchingElements : [matchingElements[0]]
|
||||
let editedCount = 0
|
||||
|
||||
for (const el of elements) {
|
||||
const htmlEl = el as HTMLElement
|
||||
|
||||
if (args.old_value) {
|
||||
// Modo Edit: buscar y reemplazar
|
||||
const current = attr === 'style' ? htmlEl.style.cssText : (htmlEl as any)[attr]
|
||||
if (current && current.includes(args.old_value)) {
|
||||
const newVal = current.replace(args.old_value, args.new_value)
|
||||
if (attr === 'style') {
|
||||
htmlEl.style.cssText = newVal
|
||||
} else {
|
||||
(htmlEl as any)[attr] = newVal
|
||||
}
|
||||
editedCount++
|
||||
const newVal = current.replace(args.old_value, args.new_value)
|
||||
if (attr === 'style') {
|
||||
htmlEl.style.cssText = newVal
|
||||
} else {
|
||||
(htmlEl as any)[attr] = newVal
|
||||
}
|
||||
} else {
|
||||
// Modo Write: reemplazar todo
|
||||
if (attr === 'style') {
|
||||
htmlEl.style.cssText = args.new_value
|
||||
} else {
|
||||
(htmlEl as any)[attr] = args.new_value
|
||||
}
|
||||
editedCount++
|
||||
}
|
||||
}
|
||||
|
||||
if (editedCount === 0) {
|
||||
return `Error: old_value not found`
|
||||
editedCount++
|
||||
}
|
||||
|
||||
return `OK ${editedCount} edited`
|
||||
@@ -534,9 +541,9 @@ export function createCanvasHandlers(): ToolConfig[] {
|
||||
description: 'inject (default), replace, remove'
|
||||
}
|
||||
},
|
||||
required: ['css']
|
||||
required: []
|
||||
},
|
||||
handler: (args: { css: string; id?: string; mode?: string }) => {
|
||||
handler: (args: { css?: string; id?: string; mode?: string }) => {
|
||||
const mode = args.mode || 'inject'
|
||||
const styleId = args.id ? `canvas-css-${args.id}` : `canvas-css-${Date.now()}`
|
||||
|
||||
@@ -550,6 +557,8 @@ export function createCanvasHandlers(): ToolConfig[] {
|
||||
return `Error: css:${args.id} not found`
|
||||
}
|
||||
|
||||
if (!args.css) return 'Error: css required for inject/replace'
|
||||
|
||||
let styleEl = args.id ? document.getElementById(`canvas-css-${args.id}`) : null
|
||||
|
||||
if (mode === 'replace' && styleEl) {
|
||||
|
||||
@@ -193,6 +193,10 @@ export function clearAllTools() {
|
||||
}
|
||||
console.log(`[WebMCP] Cleared ${registeredTools.size} tools`)
|
||||
registeredTools.clear()
|
||||
// Clear session storage to prevent stale schemas on reconnect
|
||||
if (typeof webmcpInstance._clearStoredItems === 'function') {
|
||||
webmcpInstance._clearStoredItems()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user