Spaces:
Running
Running
Download src/lib/mockState.ts from evalstate/mcp-webcam: direct link, hf CLI and curl.
- Browser
- Download file 11 kB
-
https://huggingface.co/spaces/evalstate/mcp-webcam/resolve/main/src/lib/mockState.ts
- Command line
-
hf download hf://spaces/evalstate/mcp-webcam/src/lib/mockState.ts
-
curl -L -o mockState.ts https://huggingface.co/spaces/evalstate/mcp-webcam/resolve/main/src/lib/mockState.ts
11 kB
| import type { | |
| AppConfigView, | |
| DashboardState, | |
| EventLogEntry, | |
| EventsConfig, | |
| FireEventRequest, | |
| FireEventResponse, | |
| } from '../../shared/api-types'; | |
| function makeMockThumbnail(label: string, accent: string): string { | |
| const svg = `<svg xmlns="http://www.w3.org/2000/svg" width="320" height="180" viewBox="0 0 320 180"> | |
| <defs> | |
| <linearGradient id="g" x1="0" y1="0" x2="1" y2="1"> | |
| <stop offset="0%" stop-color="#18181b"/> | |
| <stop offset="100%" stop-color="#27272a"/> | |
| </linearGradient> | |
| </defs> | |
| <rect width="320" height="180" fill="url(#g)"/> | |
| <circle cx="160" cy="82" r="34" fill="none" stroke="${accent}" stroke-width="2.5" opacity="0.85"/> | |
| <circle cx="160" cy="82" r="14" fill="${accent}" opacity="0.25"/> | |
| <text x="160" y="146" fill="#d4d4d8" font-family="ui-monospace,monospace" font-size="12" text-anchor="middle">${label}</text> | |
| </svg>`; | |
| return `data:image/svg+xml;charset=utf-8,${encodeURIComponent(svg)}`; | |
| } | |
| export function createMockConfig(): AppConfigView { | |
| return { | |
| version: '0.3.0', | |
| mode: 'http', | |
| mcpPath: '/mcp', | |
| mcpHost: 'http://localhost:3334', | |
| mcpHostConfigured: true, | |
| modernProtocol: '2026-07-28', | |
| legacyFallback: true, | |
| }; | |
| } | |
| export function createMockDashboardState(user: string, tabId = 'tab_studio_01'): DashboardState { | |
| const now = Date.now(); | |
| const ago = (sec: number) => new Date(now - sec * 1000).toISOString(); | |
| return { | |
| user, | |
| serverTime: new Date(now).toISOString(), | |
| epoch: 'ep_k9m2', | |
| headCursor: 'ep_k9m2:14', | |
| config: { | |
| pushEnabled: true, | |
| pollEnabled: true, | |
| heartbeatMs: 15000, | |
| nextPollMs: 3000, | |
| }, | |
| tabs: [ | |
| { | |
| tabId, | |
| connectedAt: ago(420), | |
| primary: true, | |
| userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 Chrome/134.0.0.0 Safari/537.36', | |
| frozen: false, | |
| deviceLabel: 'FaceTime HD Camera (Built-in)', | |
| width: 1280, | |
| height: 720, | |
| pendingCaptures: 0, | |
| }, | |
| { | |
| tabId: 'tab_desk_02', | |
| connectedAt: ago(115), | |
| primary: false, | |
| userAgent: 'Mozilla/5.0 (X11; Linux x86_64) Gecko/20100101 Firefox/136.0', | |
| frozen: true, | |
| deviceLabel: 'Logitech Brio 4K', | |
| width: 1920, | |
| height: 1080, | |
| pendingCaptures: 0, | |
| }, | |
| ], | |
| push: [ | |
| { | |
| streamId: 'str_f8a21c', | |
| subscriptionId: 'sub_01', | |
| client: { | |
| key: 'fast-agent-mcp@0.10.73', | |
| name: 'fast-agent-mcp', | |
| version: '0.10.73', | |
| source: 'envelope', | |
| era: 'modern', | |
| protocolVersion: '2026-07-28', | |
| transport: 'http', | |
| eventsCapable: true, | |
| }, | |
| eventName: 'webcam.message', | |
| arguments: {}, | |
| cursor: 'ep_k9m2:14', | |
| heartbeatMs: 15000, | |
| openedAt: ago(318), | |
| lastFrameAt: ago(9), | |
| lastHeartbeatAt: ago(9), | |
| delivered: 6, | |
| }, | |
| { | |
| streamId: 'str_9b4d0e', | |
| subscriptionId: 'sub_02', | |
| client: { | |
| key: 'studio-watcher@1.2.0', | |
| name: 'studio-watcher', | |
| version: '1.2.0', | |
| source: 'envelope', | |
| era: 'modern', | |
| protocolVersion: '2026-07-28', | |
| transport: 'http', | |
| eventsCapable: true, | |
| }, | |
| eventName: 'webcam.snapshot', | |
| arguments: {}, | |
| cursor: 'ep_k9m2:13', | |
| heartbeatMs: 15000, | |
| openedAt: ago(94), | |
| lastFrameAt: ago(14), | |
| lastHeartbeatAt: ago(14), | |
| delivered: 2, | |
| }, | |
| ], | |
| pollers: [ | |
| { | |
| pollerId: 'pol_7c2e19', | |
| client: { | |
| key: 'ci-inspector@0.4.1', | |
| name: 'ci-inspector', | |
| version: '0.4.1', | |
| source: 'envelope', | |
| era: 'modern', | |
| protocolVersion: '2026-07-28', | |
| transport: 'http', | |
| eventsCapable: true, | |
| }, | |
| eventName: 'webcam.message', | |
| arguments: {}, | |
| cursor: 'ep_k9m2:14', | |
| polls: 42, | |
| delivered: 5, | |
| firstSeenAt: ago(260), | |
| lastPollAt: ago(2), | |
| nextPollMs: 3000, | |
| status: 'active', | |
| revokePending: false, | |
| }, | |
| ], | |
| callers: [ | |
| { | |
| client: { | |
| key: 'fast-agent-mcp@0.10.73', | |
| name: 'fast-agent-mcp', | |
| version: '0.10.73', | |
| source: 'envelope', | |
| era: 'modern', | |
| protocolVersion: '2026-07-28', | |
| transport: 'http', | |
| eventsCapable: true, | |
| }, | |
| firstSeenAt: ago(330), | |
| lastSeenAt: ago(18), | |
| requests: 14, | |
| lastMethod: 'tools/call', | |
| lastDetail: 'get_event_snapshot', | |
| lastResponse: 'json', | |
| methods: { | |
| 'server/discover': 1, | |
| 'tools/list': 1, | |
| 'events/list': 1, | |
| 'events/stream': 1, | |
| 'tools/call': 10, | |
| }, | |
| status: 'active', | |
| }, | |
| { | |
| client: { | |
| key: 'claude-desktop@0.9.4', | |
| name: 'claude-desktop', | |
| version: '0.9.4', | |
| source: 'initialize', | |
| era: 'legacy', | |
| protocolVersion: '2025-11-25', | |
| transport: 'http', | |
| eventsCapable: false, | |
| }, | |
| firstSeenAt: ago(610), | |
| lastSeenAt: ago(85), | |
| requests: 7, | |
| lastMethod: 'tools/call', | |
| lastDetail: 'capture', | |
| lastResponse: 'json', | |
| methods: { | |
| initialize: 1, | |
| 'tools/list': 2, | |
| 'tools/call': 4, | |
| }, | |
| status: 'idle', | |
| }, | |
| ], | |
| events: [ | |
| { | |
| eventId: 'evt_k9m2_14', | |
| seq: 14, | |
| name: 'webcam.message', | |
| timestamp: ago(22), | |
| cursor: 'ep_k9m2:14', | |
| source: 'ui', | |
| message: 'What component am I holding up to the camera?', | |
| snapshot: { | |
| eventId: 'evt_k9m2_14', | |
| mimeType: 'image/jpeg', | |
| width: 1280, | |
| height: 720, | |
| bytes: 84210, | |
| frozen: false, | |
| url: makeMockThumbnail('evt_k9m2_14 · 1280×720', '#2dd4bf'), | |
| available: true, | |
| }, | |
| delivery: { | |
| push: 1, | |
| poll: 1, | |
| recipients: ['fast-agent-mcp 0.10.73', 'ci-inspector 0.4.1'], | |
| }, | |
| }, | |
| { | |
| eventId: 'evt_k9m2_13', | |
| seq: 13, | |
| name: 'webcam.snapshot', | |
| timestamp: ago(95), | |
| cursor: 'ep_k9m2:13', | |
| source: 'auto', | |
| message: 'Desk periodic check', | |
| snapshot: { | |
| eventId: 'evt_k9m2_13', | |
| mimeType: 'image/jpeg', | |
| width: 1280, | |
| height: 720, | |
| bytes: 79440, | |
| frozen: false, | |
| url: makeMockThumbnail('evt_k9m2_13 · auto', '#f59e0b'), | |
| available: true, | |
| }, | |
| delivery: { | |
| push: 1, | |
| poll: 0, | |
| recipients: ['studio-watcher 1.2.0'], | |
| }, | |
| }, | |
| ], | |
| activity: [ | |
| { | |
| id: 108, | |
| at: ago(2), | |
| kind: 'poll', | |
| summary: 'events/poll webcam.message (0 new)', | |
| client: 'ci-inspector 0.4.1', | |
| count: 9, | |
| }, | |
| { | |
| id: 107, | |
| at: ago(18), | |
| kind: 'call', | |
| summary: 'tools/call · get_event_snapshot (evt_k9m2_14)', | |
| client: 'fast-agent-mcp 0.10.73', | |
| }, | |
| { | |
| id: 106, | |
| at: ago(22), | |
| kind: 'event.fired', | |
| summary: 'Fired webcam.message (evt_k9m2_14) → 1 push, 1 poll', | |
| }, | |
| { | |
| id: 105, | |
| at: ago(85), | |
| kind: 'capture', | |
| summary: 'Captured webcam frame for claude-desktop · tools/call capture', | |
| client: 'claude-desktop 0.9.4', | |
| }, | |
| { | |
| id: 104, | |
| at: ago(94), | |
| kind: 'stream.opened', | |
| summary: 'Opened events/stream for webcam.snapshot', | |
| client: 'studio-watcher 1.2.0', | |
| }, | |
| ], | |
| }; | |
| } | |
| let mockSeq = 15; | |
| let mockActivityId = 109; | |
| export function applyMockFireEvent( | |
| prev: DashboardState, | |
| req: FireEventRequest, | |
| ): { next: DashboardState; response: FireEventResponse } { | |
| const seq = mockSeq++; | |
| const eventId = `evt_k9m2_${seq}`; | |
| const cursor = `${prev.epoch}:${seq}`; | |
| const nowIso = new Date().toISOString(); | |
| const matchingPush = prev.config.pushEnabled | |
| ? prev.push.filter((p) => p.eventName === req.name) | |
| : []; | |
| const matchingPollers = prev.config.pollEnabled | |
| ? prev.pollers.filter((p) => p.eventName === req.name && p.status !== 'gone') | |
| : []; | |
| const recipients = [ | |
| ...matchingPush.map((p) => | |
| p.client.name ? `${p.client.name}${p.client.version ? ` ${p.client.version}` : ''}` : p.client.key, | |
| ), | |
| ...matchingPollers.map((p) => | |
| p.client.name ? `${p.client.name}${p.client.version ? ` ${p.client.version}` : ''}` : p.client.key, | |
| ), | |
| ]; | |
| const entry: EventLogEntry = { | |
| eventId, | |
| seq, | |
| name: req.name, | |
| timestamp: nowIso, | |
| cursor, | |
| source: req.source ?? 'ui', | |
| message: req.message ?? null, | |
| snapshot: req.snapshot | |
| ? { | |
| eventId, | |
| mimeType: 'image/jpeg', | |
| width: req.width ?? 1280, | |
| height: req.height ?? 720, | |
| bytes: Math.round((req.snapshot.length * 3) / 4), | |
| frozen: Boolean(req.frozen), | |
| url: req.snapshot, | |
| available: true, | |
| } | |
| : null, | |
| delivery: { | |
| push: matchingPush.length, | |
| poll: matchingPollers.length, | |
| recipients, | |
| }, | |
| }; | |
| const next: DashboardState = { | |
| ...prev, | |
| serverTime: nowIso, | |
| headCursor: cursor, | |
| push: prev.push.map((p) => | |
| matchingPush.some((m) => m.streamId === p.streamId) | |
| ? { ...p, cursor, delivered: p.delivered + 1, lastFrameAt: nowIso } | |
| : p, | |
| ), | |
| pollers: prev.pollers.map((p) => | |
| matchingPollers.some((m) => m.pollerId === p.pollerId) | |
| ? { ...p, cursor, delivered: p.delivered + 1, polls: p.polls + 1, lastPollAt: nowIso } | |
| : p, | |
| ), | |
| events: [entry, ...prev.events].slice(0, 50), | |
| activity: [ | |
| { | |
| id: mockActivityId++, | |
| at: nowIso, | |
| kind: 'event.fired' as const, | |
| summary: `Fired ${req.name} (${eventId}) → ${matchingPush.length} push, ${matchingPollers.length} poll`, | |
| }, | |
| ...prev.activity, | |
| ].slice(0, 100), | |
| }; | |
| return { | |
| next, | |
| response: { | |
| eventId, | |
| cursor, | |
| seq, | |
| pushTargets: matchingPush.length, | |
| pollers: matchingPollers.length, | |
| }, | |
| }; | |
| } | |
| export function applyMockPatchConfig( | |
| prev: DashboardState, | |
| patch: Partial<EventsConfig>, | |
| ): DashboardState { | |
| const nowIso = new Date().toISOString(); | |
| const nextConfig = { ...prev.config, ...patch }; | |
| return { | |
| ...prev, | |
| serverTime: nowIso, | |
| config: nextConfig, | |
| activity: [ | |
| { | |
| id: mockActivityId++, | |
| at: nowIso, | |
| kind: 'config' as const, | |
| summary: `Updated delivery config (push=${nextConfig.pushEnabled ? 'on' : 'off'}, poll=${nextConfig.pollEnabled ? 'on' : 'off'})`, | |
| }, | |
| ...prev.activity, | |
| ].slice(0, 100), | |
| }; | |
| } | |