feat: Add torch client identity, early connection and auto-request
- Named clients persisted in localStorage, editable from dropdown - Auto-request: clients can auto-receive torch when no holder exists - Early torch init in App.vue (fires before WebMCP, awaited later) - Deferred torch connection in toolRegistry for race condition safety - TorchButton rewritten as dropdown with name editor, toggle, client list - Server broadcasts client names, supports update-name message - MCP tool handlers display client names
This commit is contained in:
@@ -8,6 +8,7 @@
|
||||
interface TorchClient {
|
||||
ws: any
|
||||
id: string
|
||||
name: string
|
||||
userAgent: string
|
||||
hostname: string
|
||||
connectedAt: Date
|
||||
@@ -27,6 +28,7 @@ function generateClientId(): string {
|
||||
function broadcastTorchState(broadcast: (message: string, filter?: (ws: any) => boolean) => void) {
|
||||
const clientList = Array.from(torchClients.values()).map(c => ({
|
||||
id: c.id,
|
||||
name: c.name,
|
||||
userAgent: c.userAgent,
|
||||
hostname: c.hostname,
|
||||
connectedAt: c.connectedAt.toISOString(),
|
||||
@@ -50,6 +52,7 @@ export function handleTorchConnect(ws: any, broadcast: (message: string, filter?
|
||||
torchClients.set(ws, {
|
||||
ws,
|
||||
id,
|
||||
name: 'Anonymous',
|
||||
userAgent: 'Unknown',
|
||||
hostname: 'Unknown',
|
||||
connectedAt: new Date()
|
||||
@@ -68,8 +71,14 @@ export function handleTorchMessage(ws: any, data: any, broadcast: (message: stri
|
||||
case 'register': {
|
||||
client.userAgent = data.userAgent || 'Unknown'
|
||||
client.hostname = data.hostname || 'Unknown'
|
||||
client.name = data.name || 'Anonymous'
|
||||
|
||||
// Auto-grant torch if requested and no one holds it
|
||||
if (data.autoRequest && torchHolderId === null) {
|
||||
torchHolderId = client.id
|
||||
console.log(`[Torch] Auto-granted torch to ${client.name} (${client.id})`)
|
||||
}
|
||||
|
||||
// No auto-assign - torch must be explicitly requested
|
||||
const hasTorch = torchHolderId === client.id
|
||||
|
||||
ws.send(JSON.stringify({
|
||||
@@ -78,7 +87,15 @@ export function handleTorchMessage(ws: any, data: any, broadcast: (message: stri
|
||||
hasTorch
|
||||
}))
|
||||
|
||||
console.log(`[Torch] Registered: ${client.id} (torch: ${hasTorch})`)
|
||||
console.log(`[Torch] Registered: ${client.name} (${client.id}) (torch: ${hasTorch})`)
|
||||
broadcastTorchState(broadcast)
|
||||
break
|
||||
}
|
||||
|
||||
case 'update-name': {
|
||||
const newName = (data.name || '').substring(0, 20) || 'Anonymous'
|
||||
client.name = newName
|
||||
console.log(`[Torch] Name updated: ${client.id} → ${newName}`)
|
||||
broadcastTorchState(broadcast)
|
||||
break
|
||||
}
|
||||
@@ -88,7 +105,7 @@ export function handleTorchMessage(ws: any, data: any, broadcast: (message: stri
|
||||
torchHolderId = client.id
|
||||
|
||||
ws.send(JSON.stringify({ type: 'granted' }))
|
||||
console.log(`[Torch] Transferred: ${previousHolder} → ${client.id}`)
|
||||
console.log(`[Torch] Transferred: ${previousHolder} → ${client.name} (${client.id})`)
|
||||
broadcastTorchState(broadcast)
|
||||
break
|
||||
}
|
||||
@@ -97,7 +114,7 @@ export function handleTorchMessage(ws: any, data: any, broadcast: (message: stri
|
||||
if (torchHolderId === client.id) {
|
||||
torchHolderId = null
|
||||
ws.send(JSON.stringify({ type: 'released' }))
|
||||
console.log(`[Torch] Released by: ${client.id}`)
|
||||
console.log(`[Torch] Released by: ${client.name} (${client.id})`)
|
||||
broadcastTorchState(broadcast)
|
||||
}
|
||||
break
|
||||
@@ -135,7 +152,7 @@ export function handleTorchMessage(ws: any, data: any, broadcast: (message: stri
|
||||
torchHolderId = targetId
|
||||
|
||||
ws.send(JSON.stringify({ type: 'transferred', targetId }))
|
||||
console.log(`[Torch] Transferred by ${client.id}: ${previousHolder} → ${targetId}`)
|
||||
console.log(`[Torch] Transferred by ${client.name} (${client.id}): ${previousHolder} → ${targetId}`)
|
||||
broadcastTorchState(broadcast)
|
||||
break
|
||||
}
|
||||
@@ -148,7 +165,7 @@ export function handleTorchMessage(ws: any, data: any, broadcast: (message: stri
|
||||
export function handleTorchDisconnect(ws: any, broadcast: (message: string, filter?: (ws: any) => boolean) => void) {
|
||||
const client = torchClients.get(ws)
|
||||
if (client) {
|
||||
console.log(`[Torch] Client disconnected: ${client.id}`)
|
||||
console.log(`[Torch] Client disconnected: ${client.name} (${client.id})`)
|
||||
|
||||
// If this client had the torch, release it (no auto-assign)
|
||||
if (torchHolderId === client.id) {
|
||||
|
||||
Reference in New Issue
Block a user