feat: Implement torch system via HTTP for multi-browser control
- Add /api/torch endpoints for torch state management - Torch system uses HTTP polling instead of WebSocket - Only browser with torch connects to MCP - Other browsers disconnect and poll for torch state - Auto-assign torch to first registered client - Auto-reassign torch when holder disconnects This approach requires no changes to WebMCP library.
This commit is contained in:
@@ -1,28 +1,106 @@
|
||||
import { useTorchStore } from '../stores/torch'
|
||||
import { getWebMCP } from './webmcp'
|
||||
import { getWebMCP, autoConnect } from './webmcp'
|
||||
import { endpoints } from '../config/endpoints'
|
||||
|
||||
const API_BASE = endpoints.api
|
||||
|
||||
let pollingInterval: number | null = null
|
||||
let clientId: string | null = null
|
||||
|
||||
/**
|
||||
* Request the torch from the server
|
||||
* Register this browser as a torch client
|
||||
*/
|
||||
export async function registerClient(): Promise<string | null> {
|
||||
try {
|
||||
const res = await fetch(`${API_BASE}/torch/register`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
userAgent: navigator.userAgent,
|
||||
hostname: window.location.hostname
|
||||
})
|
||||
})
|
||||
|
||||
if (!res.ok) {
|
||||
console.error('[Torch] Failed to register:', res.status)
|
||||
return null
|
||||
}
|
||||
|
||||
const data = await res.json()
|
||||
clientId = data.id
|
||||
|
||||
const torchStore = useTorchStore()
|
||||
torchStore.setClientId(data.id)
|
||||
|
||||
console.log(`[Torch] Registered as ${data.id}, hasTorch: ${data.hasTorch}`)
|
||||
|
||||
// If we have the torch, connect to MCP
|
||||
if (data.hasTorch) {
|
||||
torchStore.setTorchState(data.id)
|
||||
await connectToMCP()
|
||||
}
|
||||
|
||||
return data.id
|
||||
} catch (e) {
|
||||
console.error('[Torch] Error registering:', e)
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Unregister this browser
|
||||
*/
|
||||
export async function unregisterClient(): Promise<void> {
|
||||
if (!clientId) return
|
||||
|
||||
try {
|
||||
await fetch(`${API_BASE}/torch/client/${clientId}`, {
|
||||
method: 'DELETE'
|
||||
})
|
||||
console.log('[Torch] Unregistered')
|
||||
} catch (e) {
|
||||
console.error('[Torch] Error unregistering:', e)
|
||||
}
|
||||
|
||||
clientId = null
|
||||
}
|
||||
|
||||
/**
|
||||
* Request the torch
|
||||
*/
|
||||
export async function requestTorch(): Promise<boolean> {
|
||||
const torchStore = useTorchStore()
|
||||
const webmcp = getWebMCP()
|
||||
|
||||
if (!webmcp || !webmcp.isConnected) {
|
||||
console.error('[Torch] WebMCP not connected')
|
||||
if (!clientId) {
|
||||
console.error('[Torch] Not registered')
|
||||
return false
|
||||
}
|
||||
|
||||
const torchStore = useTorchStore()
|
||||
torchStore.setRequesting(true)
|
||||
|
||||
try {
|
||||
// Send request to server via WebSocket
|
||||
webmcp.socket?.send(JSON.stringify({
|
||||
type: 'requestTorch'
|
||||
}))
|
||||
const res = await fetch(`${API_BASE}/torch/request`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ clientId })
|
||||
})
|
||||
|
||||
if (!res.ok) {
|
||||
console.error('[Torch] Failed to request:', res.status)
|
||||
torchStore.setRequesting(false)
|
||||
return false
|
||||
}
|
||||
|
||||
const data = await res.json()
|
||||
console.log('[Torch] Torch granted!')
|
||||
|
||||
// Update state and connect to MCP
|
||||
torchStore.setTorchState(clientId)
|
||||
torchStore.setRequesting(false)
|
||||
await connectToMCP()
|
||||
|
||||
return true
|
||||
} catch (e) {
|
||||
console.error('[Torch] Error requesting torch:', e)
|
||||
console.error('[Torch] Error requesting:', e)
|
||||
torchStore.setRequesting(false)
|
||||
return false
|
||||
}
|
||||
@@ -32,72 +110,161 @@ export async function requestTorch(): Promise<boolean> {
|
||||
* 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')
|
||||
if (!clientId) {
|
||||
console.error('[Torch] Not registered')
|
||||
return false
|
||||
}
|
||||
|
||||
const torchStore = useTorchStore()
|
||||
|
||||
if (!torchStore.hasTorch) {
|
||||
console.warn('[Torch] Cannot release - do not have torch')
|
||||
return false
|
||||
}
|
||||
|
||||
try {
|
||||
webmcp.socket?.send(JSON.stringify({
|
||||
type: 'releaseTorch'
|
||||
}))
|
||||
const res = await fetch(`${API_BASE}/torch/release`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ clientId })
|
||||
})
|
||||
|
||||
if (!res.ok) {
|
||||
console.error('[Torch] Failed to release:', res.status)
|
||||
return false
|
||||
}
|
||||
|
||||
console.log('[Torch] Torch released')
|
||||
|
||||
// Update state and disconnect from MCP
|
||||
torchStore.setTorchState(null)
|
||||
disconnectFromMCP()
|
||||
|
||||
return true
|
||||
} catch (e) {
|
||||
console.error('[Torch] Error releasing torch:', e)
|
||||
console.error('[Torch] Error releasing:', e)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize torch event handlers
|
||||
* Call this after WebMCP is connected
|
||||
* Fetch current torch state from server
|
||||
*/
|
||||
export function initTorchHandlers() {
|
||||
const torchStore = useTorchStore()
|
||||
const webmcp = getWebMCP()
|
||||
export async function fetchTorchState(): Promise<void> {
|
||||
try {
|
||||
const res = await fetch(`${API_BASE}/torch`)
|
||||
if (!res.ok) return
|
||||
|
||||
const data = await res.json()
|
||||
const torchStore = useTorchStore()
|
||||
|
||||
torchStore.setClients(data.clients)
|
||||
|
||||
// Check if torch holder changed
|
||||
const hadTorch = torchStore.hasTorch
|
||||
const hasTorchNow = data.holderId === clientId
|
||||
|
||||
if (hadTorch && !hasTorchNow) {
|
||||
// We lost the torch - disconnect
|
||||
console.log('[Torch] Lost torch, disconnecting from MCP')
|
||||
torchStore.setTorchState(data.holderId)
|
||||
disconnectFromMCP()
|
||||
} else if (!hadTorch && hasTorchNow) {
|
||||
// We got the torch - connect
|
||||
console.log('[Torch] Got torch, connecting to MCP')
|
||||
torchStore.setTorchState(data.holderId)
|
||||
await connectToMCP()
|
||||
} else {
|
||||
torchStore.setTorchState(data.holderId)
|
||||
}
|
||||
} catch (e) {
|
||||
// Silently ignore polling errors
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Start polling for torch state changes
|
||||
*/
|
||||
export function startTorchPolling(intervalMs: number = 2000) {
|
||||
if (pollingInterval) return
|
||||
|
||||
console.log('[Torch] Starting polling...')
|
||||
pollingInterval = window.setInterval(fetchTorchState, intervalMs)
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop polling
|
||||
*/
|
||||
export function stopTorchPolling() {
|
||||
if (pollingInterval) {
|
||||
window.clearInterval(pollingInterval)
|
||||
pollingInterval = null
|
||||
console.log('[Torch] Polling stopped')
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Connect to MCP (when we have the torch)
|
||||
*/
|
||||
async function connectToMCP(): Promise<void> {
|
||||
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)
|
||||
})
|
||||
if (webmcp.isConnected) {
|
||||
console.log('[Torch] Already connected to MCP')
|
||||
return
|
||||
}
|
||||
|
||||
// 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')
|
||||
console.log('[Torch] Connecting to MCP...')
|
||||
const success = await autoConnect()
|
||||
if (success) {
|
||||
console.log('[Torch] Connected to MCP')
|
||||
} else {
|
||||
console.error('[Torch] Failed to connect to MCP')
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Disconnect from MCP (when we lose the torch)
|
||||
*/
|
||||
function disconnectFromMCP(): void {
|
||||
const webmcp = getWebMCP()
|
||||
if (!webmcp) return
|
||||
|
||||
if (webmcp.isConnected && typeof webmcp.disconnect === 'function') {
|
||||
console.log('[Torch] Disconnecting from MCP...')
|
||||
webmcp.disconnect()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize torch system
|
||||
*/
|
||||
export async function initTorch(): Promise<void> {
|
||||
// Register this client
|
||||
await registerClient()
|
||||
|
||||
// Start polling for state changes
|
||||
startTorchPolling()
|
||||
|
||||
// Cleanup on page unload
|
||||
window.addEventListener('beforeunload', () => {
|
||||
stopTorchPolling()
|
||||
// Note: unregisterClient is async, may not complete before unload
|
||||
// Server should handle stale clients via timeout
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Cleanup torch system
|
||||
*/
|
||||
export async function destroyTorch(): Promise<void> {
|
||||
stopTorchPolling()
|
||||
await unregisterClient()
|
||||
|
||||
const torchStore = useTorchStore()
|
||||
torchStore.reset()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user