feat: Add torch system for multi-browser MCP control
- Add TorchButton component to header (replaces dropdowns) - Add torch store for managing torch state - Add torch service for requesting/releasing torch - Add torch event handlers in WebMCP service - Remove ComponentsDropdown and ToolsDropdown from header The torch system allows controlling which browser receives MCP tool calls when multiple browsers are connected. Requires WebMCP library update to fully function.
This commit is contained in:
103
frontend/src/services/torch.ts
Normal file
103
frontend/src/services/torch.ts
Normal file
@@ -0,0 +1,103 @@
|
||||
import { useTorchStore } from '../stores/torch'
|
||||
import { getWebMCP } from './webmcp'
|
||||
|
||||
/**
|
||||
* Request the torch from the server
|
||||
*/
|
||||
export async function requestTorch(): Promise<boolean> {
|
||||
const torchStore = useTorchStore()
|
||||
const webmcp = getWebMCP()
|
||||
|
||||
if (!webmcp || !webmcp.isConnected) {
|
||||
console.error('[Torch] WebMCP not connected')
|
||||
return false
|
||||
}
|
||||
|
||||
torchStore.setRequesting(true)
|
||||
|
||||
try {
|
||||
// Send request to server via WebSocket
|
||||
webmcp.socket?.send(JSON.stringify({
|
||||
type: 'requestTorch'
|
||||
}))
|
||||
return true
|
||||
} catch (e) {
|
||||
console.error('[Torch] Error requesting torch:', e)
|
||||
torchStore.setRequesting(false)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Release the torch
|
||||
*/
|
||||
export async function releaseTorch(): Promise<boolean> {
|
||||
const torchStore = useTorchStore()
|
||||
const webmcp = getWebMCP()
|
||||
|
||||
if (!webmcp || !webmcp.isConnected) {
|
||||
console.error('[Torch] WebMCP not connected')
|
||||
return false
|
||||
}
|
||||
|
||||
if (!torchStore.hasTorch) {
|
||||
console.warn('[Torch] Cannot release - do not have torch')
|
||||
return false
|
||||
}
|
||||
|
||||
try {
|
||||
webmcp.socket?.send(JSON.stringify({
|
||||
type: 'releaseTorch'
|
||||
}))
|
||||
return true
|
||||
} catch (e) {
|
||||
console.error('[Torch] Error releasing torch:', e)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize torch event handlers
|
||||
* Call this after WebMCP is connected
|
||||
*/
|
||||
export function initTorchHandlers() {
|
||||
const torchStore = useTorchStore()
|
||||
const webmcp = getWebMCP()
|
||||
|
||||
if (!webmcp) {
|
||||
console.error('[Torch] WebMCP not initialized')
|
||||
return
|
||||
}
|
||||
|
||||
// Listen for torch state updates
|
||||
webmcp.on('torchUpdate', (data: { holderId: string | null; clients: any[] }) => {
|
||||
console.log('[Torch] State updated:', data)
|
||||
torchStore.setClients(data.clients)
|
||||
torchStore.setRequesting(false)
|
||||
})
|
||||
|
||||
// Listen for client ID assignment
|
||||
webmcp.on('clientId', (data: { id: string }) => {
|
||||
console.log('[Torch] Client ID assigned:', data.id)
|
||||
torchStore.setClientId(data.id)
|
||||
})
|
||||
|
||||
// Listen for torch granted
|
||||
webmcp.on('torchGranted', () => {
|
||||
console.log('[Torch] Torch granted!')
|
||||
torchStore.setRequesting(false)
|
||||
})
|
||||
|
||||
// Listen for torch denied
|
||||
webmcp.on('torchDenied', (data: { reason: string }) => {
|
||||
console.warn('[Torch] Torch denied:', data.reason)
|
||||
torchStore.setRequesting(false)
|
||||
})
|
||||
|
||||
// Listen for torch released
|
||||
webmcp.on('torchReleased', () => {
|
||||
console.log('[Torch] Torch released')
|
||||
})
|
||||
|
||||
console.log('[Torch] Handlers initialized')
|
||||
}
|
||||
Reference in New Issue
Block a user