diff --git a/packages/comfyui-desktop-bridge-types/README.md b/packages/comfyui-desktop-bridge-types/README.md index b90ae42aa..2e3f9f9f4 100644 --- a/packages/comfyui-desktop-bridge-types/README.md +++ b/packages/comfyui-desktop-bridge-types/README.md @@ -41,20 +41,23 @@ while omitting part of it. ## Comfy pages hosted in Desktop's Apps view -A Comfy page that Desktop shows in its Apps view gets two objects instead, and -nothing else of the bridge above: `window.comfyDesktopAuth`, Desktop's account -session, and `window.comfyDesktopLocal`, a ComfyUI on this computer. +A Comfy page that Desktop shows in its Apps view gets three objects instead, +and nothing else of the bridge above: `window.comfyDesktopAuth`, Desktop's +account session, `window.comfyDesktopLocal`, a ComfyUI on this computer, and +`window.comfyDesktopTelemetry`, the page's usage events. ```ts import type { ComfyDesktopHostedAuthBridge, - ComfyDesktopHostedLocalBridge + ComfyDesktopHostedLocalBridge, + ComfyDesktopHostedTelemetryBridge } from '@comfyorg/comfyui-desktop-bridge-types' declare global { interface Window { comfyDesktopAuth?: ComfyDesktopHostedAuthBridge comfyDesktopLocal?: ComfyDesktopHostedLocalBridge + comfyDesktopTelemetry?: ComfyDesktopHostedTelemetryBridge } } ``` @@ -83,8 +86,36 @@ the workflow, or `{ opened: false, error }` with the reason in words. Both members are absent on Desktop builds that predate them, so a page checks for them and keeps its file download as the fallback. -Both objects are absent in a browser, so a page keeps its own sign-in and its -own way to reach a local ComfyUI as the fallback. +`track(name, props)` reports what the person does on the page, such as +`track('click', { target: 'run' })`. Desktop sends it as +`comfy.desktop.apps.` only when the person allows telemetry, and adds +`app_id`, the app whose page Desktop loaded in that view. An event the page +sends while the view loads another app is dropped. + +- A name starts with a lowercase letter, then lowercase letters, digits and + `_`, up to 48 characters in all (`^[a-z][a-z0-9_]{0,47}$`), so `2_click` is + dropped. `app_opened` and `app_page_failed` are Desktop's own and are + refused, and so is `error`. +- Desktop keeps up to 16 properties. A key starts with a lowercase letter, + then lowercase letters, digits and `_`, up to 32 characters in all + (`^[a-z][a-z0-9_]{0,31}$`), so `{ Run: true }` is dropped. A value is a + boolean, a finite number or a slug of up to 64 characters + (`^[a-z0-9][a-z0-9._-]{0,63}$`). Free text, any other key or value, and an + `app` or `app_id` from the page are dropped. +- Desktop sets these keys on every event itself, so the page's value for any + of them is dropped: `app_version`, `app_channel`, `app_env`, `is_packaged`, + `platform`, `arch`, `client`, `installation_id` and `sac_state`. +- Desktop sends at most 60 events a minute for one app and 1,000 page events a + session. Events telemetry did not send, as while consent is off, do not + count. +- Desktop's telemetry also sends at most 60 of one event name a minute, shared + by every app. If one app sends 60 `click` events in a minute, a `click` from + another app in that minute is dropped too. + +The page is not told when Desktop drops an event. + +All three objects are absent in a browser, so a page keeps its own sign-in and +its own way to reach a local ComfyUI as the fallback, and sends no events. ## Provenance diff --git a/packages/comfyui-desktop-bridge-types/comfyDesktopBridge.d.ts b/packages/comfyui-desktop-bridge-types/comfyDesktopBridge.d.ts index 84e8d6586..047a9b7bf 100644 --- a/packages/comfyui-desktop-bridge-types/comfyDesktopBridge.d.ts +++ b/packages/comfyui-desktop-bridge-types/comfyDesktopBridge.d.ts @@ -305,6 +305,23 @@ export interface ComfyDesktopHostedLocalBridge { /** The person's ComfyUI builds on this computer. Absent on Desktop builds that predate it. */ localBuilds?(): Promise } +/** + * Usage events from a Comfy page hosted in Desktop's Apps view. Desktop + * exposes it to that page as `window.comfyDesktopTelemetry` and sends an event + * as `comfy.desktop.apps.` only when the person allows telemetry. + * `name` matches `^[a-z][a-z0-9_]{0,47}$`; `app_opened` and `app_page_failed` + * are refused because Desktop sends them itself, and so is `error`. Desktop + * sends at most 60 events a minute for one app and 1,000 page events a + * session, and drops the rest. Desktop keeps at most 16 + * properties, each named `^[a-z][a-z0-9_]{0,31}$` with a boolean, a finite + * number or a slug of at most 64 characters (`^[a-z0-9][a-z0-9._-]{0,63}$`), + * and drops anything else. Desktop sets `app_id` itself, to the app whose + * page it loaded in that view; an `app` or `app_id` from the page is ignored, + * and an event sent while the view loads another app is dropped. + */ +export interface ComfyDesktopHostedTelemetryBridge { + track(name: string, props?: Record): void +} export interface ComfyDesktop2TerminalBridge { subscribe(installationId?: string): Promise unsubscribe(installationId?: string): Promise diff --git a/packages/comfyui-desktop-bridge-types/package.json b/packages/comfyui-desktop-bridge-types/package.json index 68294220e..bea6b0a86 100644 --- a/packages/comfyui-desktop-bridge-types/package.json +++ b/packages/comfyui-desktop-bridge-types/package.json @@ -1,6 +1,6 @@ { "name": "@comfyorg/comfyui-desktop-bridge-types", - "version": "0.12.0", + "version": "0.13.0", "description": "TypeScript definitions for the Comfy Desktop hosted frontend bridge", "type": "module", "main": "./index.js", diff --git a/src/main/apps/appPageEvents.test.ts b/src/main/apps/appPageEvents.test.ts new file mode 100644 index 000000000..185a2608f --- /dev/null +++ b/src/main/apps/appPageEvents.test.ts @@ -0,0 +1,230 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' +import os from 'os' +import path from 'path' + +vi.mock('electron', () => ({ + app: { + getPath: () => path.join(os.tmpdir(), 'launcher-test'), + isPackaged: true, + on: () => {} + }, + BrowserWindow: { getAllWindows: () => [] } +})) + +const sdkCaptures = vi.hoisted( + () => [] as Array<{ event: string; properties?: Record }> +) + +vi.mock('posthog-node', () => ({ + PostHog: class { + on(): () => void { + return () => {} + } + capture(call: { event: string; properties?: Record }): void { + sdkCaptures.push(call) + } + identify(): void {} + flush(): Promise { + return Promise.resolve() + } + shutdown(): Promise { + return Promise.resolve() + } + } +})) + +import * as telemetry from '../lib/telemetry' +import { + APP_PAGE_EVENT_PREFIX, + DESKTOP_APP_EVENTS, + MAX_PAGE_EVENT_PROPERTIES, + UNLIMITED_EVENT_NAMES, + _resetPageEventLimitsForTest, + checkAppPageEvent, + trackAppPageEvent +} from './appPageEvents' + +const APP = 'remove-background' + +describe('checkAppPageEvent', () => { + it('sends a page event under the Apps prefix with the app id Desktop holds', () => { + expect( + checkAppPageEvent( + 'run_started', + { where: 'cloud', model: 'birefnet', seconds: 12.4, fast: true }, + APP + ) + ).toEqual({ + event: 'comfy.desktop.apps.run_started', + properties: { where: 'cloud', model: 'birefnet', seconds: 12.4, fast: true, app_id: APP } + }) + }) + + it.each(['click', 'run_started', 'example_used', 'a', `a${'b'.repeat(47)}`, 'step_2_done'])( + 'accepts the event name %s', + (name) => { + expect(checkAppPageEvent(name, {}, APP)?.event).toBe(`${APP_PAGE_EVENT_PREFIX}${name}`) + } + ) + + it.each([ + ['too long', `a${'b'.repeat(48)}`], + ['starting with a digit', '2fast'], + ['starting with an underscore', '_hidden'], + ['with capitals', 'RunStarted'], + ['with a dot', 'apps.run_started'], + ['with a dash', 'run-started'], + ['with a space', 'run started'], + ['empty', ''], + ['not a string', 42] + ])('refuses an event name %s', (_why, name) => { + expect(checkAppPageEvent(name, {}, APP)).toBeNull() + }) + + it.each([...DESKTOP_APP_EVENTS])('refuses %s, which only Desktop sends', (name) => { + expect(['app_opened', 'app_page_failed']).toContain(name) + expect(checkAppPageEvent(name, {}, APP)).toBeNull() + }) + + it('takes app_id from Desktop and ignores the app the page names', () => { + expect( + checkAppPageEvent('click', { app: 'other-app', app_id: 'other-app', target: 'run' }, APP) + ).toEqual({ + event: 'comfy.desktop.apps.click', + properties: { target: 'run', app_id: APP } + }) + }) + + it.each([ + ['no app id', null], + ['an app id the catalog would not list', 'Remove Background'], + ['an app id with a dot', 'remove.background'], + ['an app id longer than the catalog allows', `a${'b'.repeat(128)}`] + ])('drops the event when Desktop holds %s for the page', (_why, appId) => { + expect(checkAppPageEvent('click', { target: 'run' }, appId)).toBeNull() + }) + + it.each(['HDR', 'Upscale_4x', `a${'b'.repeat(127)}`])( + 'keeps the catalog app id %s as Desktop holds it', + (appId) => { + expect(checkAppPageEvent('click', {}, appId)?.properties).toEqual({ app_id: appId }) + } + ) + + it('refuses error, whose full name the shared telemetry limit lets through unlimited', () => { + expect(UNLIMITED_EVENT_NAMES.has('error')).toBe(true) + expect(checkAppPageEvent('error', {}, APP)).toBeNull() + expect(checkAppPageEvent('run_error', {}, APP)?.event).toBe('comfy.desktop.apps.run_error') + }) + + it('drops free text, bad values and bad property names, keeping the event', () => { + expect( + checkAppPageEvent( + 'run_started', + { + where: 'local', + prompt: 'a cat on the moon', + file: '/Users/someone/Pictures/me.png', + name: 'My Model.safetensors', + long: 'a'.repeat(65), + nan: Number.NaN, + forever: Number.POSITIVE_INFINITY, + nothing: null, + list: ['a'], + nested: { a: 1 }, + Capital: 'x', + '1st': 'x', + [`a${'b'.repeat(32)}`]: 'x', + $set: 'x', + app_version: '9.9.9', + installation_id: 'fake', + ok: false, + count: 0 + }, + APP + ) + ).toEqual({ + event: 'comfy.desktop.apps.run_started', + properties: { where: 'local', ok: false, count: 0, app_id: APP } + }) + }) + + it('keeps a 64-character slug and a 32-character property name', () => { + const key = `a${'b'.repeat(31)}` + const value = `a${'b'.repeat(63)}` + expect(checkAppPageEvent('click', { [key]: value }, APP)?.properties).toEqual({ + [key]: value, + app_id: APP + }) + }) + + it(`keeps at most ${MAX_PAGE_EVENT_PROPERTIES} properties besides app_id`, () => { + const many = Object.fromEntries(Array.from({ length: 20 }, (_, i) => [`p${i}`, i])) + const kept = checkAppPageEvent('click', many, APP)?.properties ?? {} + expect(Object.keys(kept)).toHaveLength(MAX_PAGE_EVENT_PROPERTIES + 1) + expect(kept).toMatchObject({ p0: 0, p15: 15, app_id: APP }) + expect(kept).not.toHaveProperty('p16') + }) + + it.each([ + ['not an object', 'run'], + ['a list', ['run']], + ['missing', undefined] + ])('sends only app_id when the properties are %s', (_why, props) => { + expect(checkAppPageEvent('click', props, APP)?.properties).toEqual({ app_id: APP }) + }) +}) + +describe('trackAppPageEvent through Desktop telemetry', () => { + beforeEach(() => { + process.env['POSTHOG_API_KEY'] = 'test-key' + process.env['POSTHOG_ENABLED'] = '1' + telemetry._resetForTest() + _resetPageEventLimitsForTest() + telemetry.initTelemetry({ appVersion: '0.0.0', appEnv: 'test', isPackaged: true }) + }) + + afterEach(() => { + telemetry._resetForTest() + sdkCaptures.length = 0 + delete process.env['POSTHOG_API_KEY'] + delete process.env['POSTHOG_ENABLED'] + }) + + function appCaptures(): Array<{ event: string; properties?: Record }> { + return sdkCaptures.filter((c) => c.event.startsWith(APP_PAGE_EVENT_PREFIX)) + } + + it('sends a good event to PostHog when the person allows telemetry', () => { + telemetry.setConsentState('granted') + telemetry.bindAnonymousId('anon-1', 'anon-1', {}) + + expect(trackAppPageEvent('run_started', { where: 'cloud', prompt: 'a cat' }, APP)).toBe(true) + + const sent = appCaptures() + expect(sent.map((c) => c.event)).toEqual(['comfy.desktop.apps.run_started']) + expect(sent[0]?.properties).toMatchObject({ app_id: APP, where: 'cloud' }) + expect(sent[0]?.properties).not.toHaveProperty('prompt') + }) + + it.each(['denied', 'undecided'] as const)('sends nothing while consent is %s', (consent) => { + telemetry.setConsentState(consent) + telemetry.bindAnonymousId('anon-1', 'anon-1', {}) + + expect(trackAppPageEvent('run_started', { where: 'cloud' }, APP)).toBe(false) + expect(trackAppPageEvent('click', { target: 'run' }, APP)).toBe(false) + + expect(appCaptures()).toEqual([]) + }) + + it('sends nothing for an event it refuses, even with consent', () => { + telemetry.setConsentState('granted') + telemetry.bindAnonymousId('anon-1', 'anon-1', {}) + + expect(trackAppPageEvent('app_opened', {}, APP)).toBe(false) + expect(trackAppPageEvent('Not An Event', {}, APP)).toBe(false) + expect(trackAppPageEvent('click', {}, null)).toBe(false) + + expect(appCaptures()).toEqual([]) + }) +}) diff --git a/src/main/apps/appPageEvents.ts b/src/main/apps/appPageEvents.ts new file mode 100644 index 000000000..e163ffd90 --- /dev/null +++ b/src/main/apps/appPageEvents.ts @@ -0,0 +1,110 @@ +import { APP_ID_PATTERN } from '../../shared/appsLocation' +import * as mainTelemetry from '../lib/telemetry' +import type { TelemetryContext } from '../lib/telemetry' + +export const APP_PAGE_EVENT_PREFIX = 'comfy.desktop.apps.' +export const MAX_PAGE_EVENT_PROPERTIES = 16 +export const PAGE_EVENTS_PER_MINUTE = 60 +export const PAGE_EVENTS_PER_SESSION = 1_000 +const MINUTE_MS = 60_000 + +const EVENT_NAME = /^[a-z][a-z0-9_]{0,47}$/ +const PROPERTY_NAME = /^[a-z][a-z0-9_]{0,31}$/ +const SLUG = /^[a-z0-9][a-z0-9._-]{0,63}$/ + +export const DESKTOP_APP_EVENTS: ReadonlySet = new Set(['app_opened', 'app_page_failed']) +export const UNLIMITED_EVENT_NAMES: ReadonlySet = new Set(['error']) +const DESKTOP_PROPERTIES: ReadonlySet = new Set(['app', 'app_id']) + +type PropertyValue = boolean | number | string + +function isPlainRecord(value: unknown): value is Record { + return typeof value === 'object' && value !== null && !Array.isArray(value) +} + +function propertyValue(value: unknown): PropertyValue | undefined { + if (typeof value === 'boolean') return value + if (typeof value === 'number') return Number.isFinite(value) ? value : undefined + if (typeof value === 'string') return SLUG.test(value) ? value : undefined + return undefined +} + +function isPageProperty(key: string): boolean { + return ( + PROPERTY_NAME.test(key) && + !DESKTOP_PROPERTIES.has(key) && + !mainTelemetry.DEFAULT_EVENT_PROPERTY_NAMES.has(key) + ) +} + +export interface CheckedPageEvent { + event: string + properties: TelemetryContext +} + +export function checkAppPageEvent( + name: unknown, + props: unknown, + appId: string | null +): CheckedPageEvent | null { + if ( + typeof name !== 'string' || + !EVENT_NAME.test(name) || + DESKTOP_APP_EVENTS.has(name) || + UNLIMITED_EVENT_NAMES.has(name) + ) { + return null + } + if (appId === null || !APP_ID_PATTERN.test(appId)) return null + const properties: TelemetryContext = {} + let kept = 0 + for (const [key, raw] of Object.entries(isPlainRecord(props) ? props : {})) { + if (kept === MAX_PAGE_EVENT_PROPERTIES) break + if (!isPageProperty(key)) continue + const value = propertyValue(raw) + if (value === undefined) continue + properties[key] = value + kept++ + } + properties['app_id'] = appId + return { event: `${APP_PAGE_EVENT_PREFIX}${name}`, properties } +} + +const sentThisMinute = new Map() +let sentThisSession = 0 + +function recentFor(appId: string, now: number): number[] { + const recent = (sentThisMinute.get(appId) ?? []).filter((at) => at > now - MINUTE_MS) + sentThisMinute.set(appId, recent) + return recent +} + +function isUnderPageLimits(appId: string, now: number): boolean { + return ( + sentThisSession < PAGE_EVENTS_PER_SESSION && + recentFor(appId, now).length < PAGE_EVENTS_PER_MINUTE + ) +} + +function countSent(appId: string, now: number): void { + recentFor(appId, now).push(now) + sentThisSession++ +} + +export function _resetPageEventLimitsForTest(): void { + sentThisMinute.clear() + sentThisSession = 0 +} + +export function trackAppPageEvent(name: unknown, props: unknown, appId: string | null): boolean { + try { + const checked = checkAppPageEvent(name, props, appId) + const now = Date.now() + if (!checked || appId === null || !isUnderPageLimits(appId, now)) return false + const sent = mainTelemetry.capture(checked.event, checked.properties) + if (sent) countSent(appId, now) + return sent + } catch { + return false + } +} diff --git a/src/main/apps/appPageView.test.ts b/src/main/apps/appPageView.test.ts index 1c094fad9..68e2a28de 100644 --- a/src/main/apps/appPageView.test.ts +++ b/src/main/apps/appPageView.test.ts @@ -1,6 +1,6 @@ import { EventEmitter } from 'node:events' import { beforeEach, describe, expect, it, vi } from 'vitest' -import type { WebContents, WebFrameMain } from 'electron' +import type { IpcMainEvent, WebContents, WebFrameMain } from 'electron' type PermissionCheck = (contents: unknown, permission: string, requestingOrigin: string) => boolean type PermissionRequest = ( @@ -41,7 +41,10 @@ vi.mock('../lib/comfyDownloadManager', () => ({ attachSessionDownloadHandler: mocks.attachDownloads })) vi.mock('../lib/contextMenu', () => ({ attachContextMenu: vi.fn() })) -vi.mock('../lib/telemetry', () => ({ capture: mocks.capture })) +vi.mock('../lib/telemetry', () => ({ + capture: mocks.capture, + DEFAULT_EVENT_PROPERTY_NAMES: new Set() +})) vi.mock('./appsCatalog', () => ({ pageUrlForListedApp: (id: string) => mocks.pages.get(id) ?? null })) @@ -63,8 +66,10 @@ import { _resetForTest, captureAppPage, hideAppPage, + loadedAppPage, showAppPage } from './appPageView' +import { trackFromAppsPage } from '../lib/ipc/registerAppsTelemetryHandlers' const APPS_URL = 'https://comfy.org/apps/' const CUTOUT = 'remove-background' @@ -751,3 +756,222 @@ describe('isAppsPageSender', () => { expect(isAppsPageSender(page, null)).toBe(false) }) }) + +const HDR = 'sdr-to-hdr' +const HDR_URL = `${APPS_URL}sdr-to-hdr/desktop.html` +const RUN = { name: 'run_started', props: { where: 'cloud' } } + +function eventFrom(contents: FakeContents): IpcMainEvent { + return { + sender: contents, + senderFrame: { ...contents.mainFrame, url: contents.url } + } as unknown as IpcMainEvent +} + +function runsSent(): unknown[][] { + return mocks.capture.mock.calls.filter(([name]) => name === 'comfy.desktop.apps.run_started') +} + +describe('the app a hosted page has loaded', () => { + beforeEach(() => { + mocks.pages.set(HDR, HDR_URL) + }) + + it('is no app until the page Desktop loads commits, then the app with that document', async () => { + await openCutout() + const page = hostedPage().webContents as unknown as WebContents + expect(loadedAppPage(page)).toBeNull() + + finishLoading() + + expect(loadedAppPage(page)).toEqual({ appId: CUTOUT, document: 1 }) + }) + + it('is no app while the next app loads, then the next app once its page commits', async () => { + await openCutout() + finishLoading() + const page = hostedPage().webContents as unknown as WebContents + + await showAppPage(panelContents(), HDR, PLACEMENT) + expect(loadedAppPage(page)).toBeNull() + + finishLoading(HDR_URL) + expect(loadedAppPage(page)).toEqual({ appId: HDR, document: 2 }) + }) + + it("keeps the app when the page moves within its own address, and drops it when the page goes to another app's", async () => { + await openCutout() + finishLoading() + const page = hostedPage().webContents + + page.emit('did-navigate', {}, `${CUTOUT_URL}?example=dog`, 200) + expect(loadedAppPage(page as unknown as WebContents)).toEqual({ appId: CUTOUT, document: 2 }) + + page.emit('did-navigate', {}, HDR_URL, 200) + expect(loadedAppPage(page as unknown as WebContents)).toBeNull() + }) + + it('keeps the app while the page moves within its own page, by query or hash', async () => { + await openCutout() + finishLoading() + const page = hostedPage().webContents + + page.emit('did-navigate-in-page', {}, `${CUTOUT_URL}?example=dog`, true) + page.emit('did-navigate-in-page', {}, `${CUTOUT_URL}#result`, true) + + expect(loadedAppPage(page as unknown as WebContents)).toEqual({ appId: CUTOUT, document: 1 }) + }) + + it('keeps the app on the address its page was redirected to, through moves within it', async () => { + await openCutout() + const moved = `${APPS_URL}remove-background/v2/desktop.html` + finishLoading(moved) + const page = hostedPage().webContents + + page.emit('did-navigate-in-page', {}, `${moved}#result`, true) + expect(loadedAppPage(page as unknown as WebContents)).toEqual({ appId: CUTOUT, document: 1 }) + }) + + it("keeps a hash-route app within its route, and drops it on another app's route", async () => { + await showAppPage(panelContents(), UPSCALE, PLACEMENT) + finishLoading(UPSCALE_URL) + const page = hostedPage().webContents as unknown as WebContents + const moveTo = (url: string) => + hostedPage().webContents.emit('did-navigate-in-page', {}, url, true) + + moveTo(`${UPSCALE_URL}/step-2`) + expect(loadedAppPage(page)).toEqual({ appId: UPSCALE, document: 1 }) + + moveTo(`${APPS_URL}#/upscale-pro`) + expect(loadedAppPage(page)).toBeNull() + + moveTo(`${APPS_URL}#/other-app`) + expect(loadedAppPage(page)).toBeNull() + }) + + it.each([ + [`${UPSCALE_URL}?mode=before`, `${UPSCALE_URL}?mode=after`], + [`${UPSCALE_URL}/`, `${UPSCALE_URL}/step-2`] + ])('keeps a hash-route app listed at %s while it moves to %s', async (listedUrl, nextUrl) => { + mocks.pages.set(UPSCALE, listedUrl) + await showAppPage(panelContents(), UPSCALE, PLACEMENT) + finishLoading(listedUrl) + const page = hostedPage().webContents + + page.url = nextUrl + page.emit('did-navigate-in-page', {}, nextUrl, true) + + expect(loadedAppPage(page as unknown as WebContents)).toEqual({ appId: UPSCALE, document: 1 }) + await trackFromAppsPage(eventFrom(page), RUN) + expect(runsSent()).toEqual([ + ['comfy.desktop.apps.run_started', { where: 'cloud', app_id: UPSCALE }] + ]) + }) + + it.each([`${UPSCALE_URL}?mode=before`, `${UPSCALE_URL}/`])( + "drops a hash-route app listed at %s on another app's route and on the list", + async (listedUrl) => { + mocks.pages.set(UPSCALE, listedUrl) + await showAppPage(panelContents(), UPSCALE, PLACEMENT) + finishLoading(listedUrl) + const page = hostedPage().webContents as unknown as WebContents + const moveTo = (url: string) => + hostedPage().webContents.emit('did-navigate-in-page', {}, url, true) + + moveTo(`${APPS_URL}#/upscale-pro`) + expect(loadedAppPage(page)).toBeNull() + + moveTo(listedUrl) + expect(loadedAppPage(page)).toEqual({ appId: UPSCALE, document: 1 }) + + moveTo(`${APPS_URL}#/other-app?mode=before`) + expect(loadedAppPage(page)).toBeNull() + + moveTo(listedUrl) + moveTo(`${APPS_URL}#/`) + expect(loadedAppPage(page)).toBeNull() + } + ) + + it('is no app for contents Desktop does not host', async () => { + await openCutout() + finishLoading() + + expect(loadedAppPage(panelContents())).toBeNull() + expect(loadedAppPage(new FakeContents() as unknown as WebContents)).toBeNull() + }) +}) + +describe('usage events from a hosted page that switches apps', () => { + beforeEach(() => { + mocks.pages.set(HDR, HDR_URL) + }) + + it('drops what the old page sends while the next app loads, and tags the next page with its own app', async () => { + await openCutout() + finishLoading() + const page = hostedPage().webContents + + await trackFromAppsPage(eventFrom(page), RUN) + expect(runsSent()).toEqual([ + ['comfy.desktop.apps.run_started', { where: 'cloud', app_id: CUTOUT }] + ]) + + await showAppPage(panelContents(), HDR, PLACEMENT) + expect(await trackFromAppsPage(eventFrom(page), RUN)).toBe(false) + expect(runsSent()).toHaveLength(1) + + finishLoading(HDR_URL) + await trackFromAppsPage(eventFrom(page), RUN) + expect(runsSent()).toEqual([ + ['comfy.desktop.apps.run_started', { where: 'cloud', app_id: CUTOUT }], + ['comfy.desktop.apps.run_started', { where: 'cloud', app_id: HDR }] + ]) + }) + + it('tags events again after the person goes back to the list and reopens an app drawn by a hash route', async () => { + await showAppPage(panelContents(), UPSCALE, PLACEMENT) + finishLoading(UPSCALE_URL) + const page = hostedPage().webContents + + page.url = `${APPS_URL}#/` + page.emit('did-navigate-in-page', {}, page.url, true) + expect(loadedAppPage(page as unknown as WebContents)).toBeNull() + + await showAppPage(panelContents(), UPSCALE, PLACEMENT) + expect(await trackFromAppsPage(eventFrom(page), RUN)).toBe(false) + + page.url = UPSCALE_URL + page.emit('did-navigate-in-page', {}, UPSCALE_URL, true) + expect(loadedAppPage(page as unknown as WebContents)).toEqual({ appId: UPSCALE, document: 1 }) + + await trackFromAppsPage(eventFrom(page), RUN) + expect(runsSent()).toEqual([ + ['comfy.desktop.apps.run_started', { where: 'cloud', app_id: UPSCALE }] + ]) + }) + + it('takes no app from a hash change that is not the route Desktop is loading', async () => { + await openCutout() + finishLoading() + const page = hostedPage().webContents + + await showAppPage(panelContents(), HDR, PLACEMENT) + page.emit('did-navigate-in-page', {}, `${CUTOUT_URL}#step-2`, true) + page.emit('did-navigate-in-page', {}, HDR_URL, false) + + expect(loadedAppPage(page as unknown as WebContents)).toBeNull() + }) + + it('drops an event whose page was replaced while Desktop was checking it', async () => { + await openCutout() + finishLoading() + const page = hostedPage().webContents + + const sending = trackFromAppsPage(eventFrom(page), RUN) + page.emit('did-navigate', {}, CUTOUT_URL, 200) + + expect(await sending).toBe(false) + expect(runsSent()).toEqual([]) + }) +}) diff --git a/src/main/apps/appPageView.ts b/src/main/apps/appPageView.ts index a41b6d98d..d51c47db4 100644 --- a/src/main/apps/appPageView.ts +++ b/src/main/apps/appPageView.ts @@ -29,9 +29,19 @@ const PAGE_PERMISSIONS: ReadonlySet = new Set(['clipboard-sanitized-writ type PageFailure = 'http_status' | 'network' | 'crashed' | 'redirected' +export interface LoadedAppPage { + appId: string + document: number +} + interface HostedPage { view: WebContentsView appId: string | null + loaded: LoadedAppPage | null + loadStarted: boolean + loadingUrl: string | null + routeBase: string | null + documents: number status: AppPageStatus wanted: boolean focusWhenReady: boolean @@ -111,13 +121,83 @@ function fail(windowKey: number, hosted: HostedPage, reason: PageFailure): void setStatus(windowKey, hosted, 'failed') } +const pagePath = (url: string): string | null => { + try { + const { origin, pathname } = new URL(url) + return origin + pathname.replace(/(\/index)?\.html$|\/$/, '') + } catch { + return null + } +} + +const routeOf = (url: string): string => { + try { + return (new URL(url).hash.split('?', 1)[0] ?? '').replace(/^(#\/.+?)\/+$/, '$1') + } catch { + return '' + } +} + +function isInAppRoute(base: string, url: string): boolean { + if (pagePath(base) === null || pagePath(base) !== pagePath(url)) return false + const route = routeOf(base) + if (!route.startsWith('#/')) return true + const at = routeOf(url) + return at === route || at.startsWith(`${route}/`) +} + +function routeBaseOf(hosted: HostedPage): string | null { + if (hosted.routeBase !== null) return hosted.routeBase + return hosted.appId === null ? null : pageUrlForListedApp(hosted.appId) +} + +function isInLoadedRoute(hosted: HostedPage, url: string): boolean { + const base = routeBaseOf(hosted) + return base !== null && isInAppRoute(base, url) +} + +function commitDocument(hosted: HostedPage, url: string): void { + hosted.documents += 1 + const appId = hosted.appId + const started = hosted.loadStarted + hosted.loadStarted = false + hosted.loadingUrl = null + if (started) hosted.routeBase = url + hosted.loaded = + appId !== null && (started || isInLoadedRoute(hosted, url)) + ? { appId, document: hosted.documents } + : null +} + +function moveWithinDocument(hosted: HostedPage, url: string): void { + const appId = hosted.appId + if (hosted.loadStarted) { + if (appId === null || url !== hosted.loadingUrl) return + hosted.loadStarted = false + hosted.loadingUrl = null + hosted.routeBase = url + hosted.loaded = { appId, document: hosted.documents } + return + } + hosted.loaded = + appId !== null && isInLoadedRoute(hosted, url) ? { appId, document: hosted.documents } : null +} + function load(windowKey: number, hosted: HostedPage, url: string): void { + hosted.loaded = null + hosted.loadStarted = true + hosted.loadingUrl = url + hosted.routeBase = null setStatus(windowKey, hosted, 'loading') void hosted.view.webContents.loadURL(url).catch(() => {}) } function closeForList(windowKey: number, hosted: HostedPage, pageStillShowsApp: boolean): void { - if (!pageStillShowsApp) hosted.appId = null + if (!pageStillShowsApp) { + hosted.appId = null + hosted.loaded = null + hosted.routeBase = null + } conceal(hosted) panelOf(windowKey)?.send(APP_PAGE_CLOSED_CHANNEL) } @@ -150,6 +230,11 @@ function createHostedPage({ windowKey, entry }: PageHost, location: AppsLocation const hosted: HostedPage = { view, appId: null, + loaded: null, + loadStarted: false, + loadingUrl: null, + routeBase: null, + documents: 0, status: 'loading', wanted: false, focusWhenReady: false, @@ -180,11 +265,14 @@ function createHostedPage({ windowKey, entry }: PageHost, location: AppsLocation return { action: 'deny' } }) contents.on('did-navigate', (_event, url, httpResponseCode) => { + commitDocument(hosted, url) if (isAppsListUrl(location, url)) closeForList(windowKey, hosted, false) else if (httpResponseCode >= FIRST_HTTP_ERROR_STATUS) fail(windowKey, hosted, 'http_status') }) contents.on('did-navigate-in-page', (_event, url, isMainFrame) => { - if (isMainFrame && isAppsListUrl(location, url)) closeForList(windowKey, hosted, false) + if (!isMainFrame) return + if (isAppsListUrl(location, url)) closeForList(windowKey, hosted, false) + else moveWithinDocument(hosted, url) }) contents.on('dom-ready', () => { if (hosted.status !== 'loading' || !isInAppsScope(location, contents.getURL())) return @@ -277,6 +365,13 @@ function concealWhenPanelGoes(windowKey: number, panel: WebContents): void { }) } +export function loadedAppPage(contents: WebContents): LoadedAppPage | null { + for (const hosted of hostedByWindow.values()) { + if (hosted.view.webContents === contents) return hosted.loaded + } + return null +} + export async function showAppPage( panel: WebContents, appId: unknown, diff --git a/src/main/apps/appsCatalog.ts b/src/main/apps/appsCatalog.ts index fcfa48f36..1b0abf147 100644 --- a/src/main/apps/appsCatalog.ts +++ b/src/main/apps/appsCatalog.ts @@ -1,4 +1,4 @@ -import { isAppsListUrl, isInAppsScope } from '../../shared/appsLocation' +import { APP_ID_PATTERN, isAppsListUrl, isInAppsScope } from '../../shared/appsLocation' import type { AppsLocation } from '../../shared/appsLocation' import type { AppCard } from '../../types/ipc' import { fetchJSON } from '../lib/fetch' @@ -9,7 +9,6 @@ const FETCH_TIMEOUT_MS = 8000 const CACHE_TTL_MS = 5 * 60 * 1000 const MAX_APPS = 200 const MAX_TEXT_LENGTH = 4096 -const APP_ID_PATTERN = /^[a-z0-9][a-z0-9_-]{0,127}$/i interface ListedApp { card: AppCard diff --git a/src/main/lib/ipc/index.ts b/src/main/lib/ipc/index.ts index 62f7dcd5b..ea21ede8b 100644 --- a/src/main/lib/ipc/index.ts +++ b/src/main/lib/ipc/index.ts @@ -40,6 +40,7 @@ import { registerEmbeddedSessionHandlers } from './registerEmbeddedSessionHandle import { registerCatalogHandlers } from './registerCatalogHandlers' import { registerAppsHandlers } from './registerAppsHandlers' import { registerAppsLocalHandlers } from './registerAppsLocalHandlers' +import { registerAppsTelemetryHandlers } from './registerAppsTelemetryHandlers' import { reconcileAdoptedSettings } from '../desktopAdopt' import { finalizeComfyBuilderRecovery, @@ -287,5 +288,6 @@ export function register(callbacks: RegisterCallbacks = {}): Promise { registerCatalogHandlers() registerAppsHandlers() registerAppsLocalHandlers() + registerAppsTelemetryHandlers() return startupRecovery.then(() => undefined) } diff --git a/src/main/lib/ipc/registerAppsTelemetryHandlers.test.ts b/src/main/lib/ipc/registerAppsTelemetryHandlers.test.ts new file mode 100644 index 000000000..6a7ce7be9 --- /dev/null +++ b/src/main/lib/ipc/registerAppsTelemetryHandlers.test.ts @@ -0,0 +1,230 @@ +import { beforeEach, describe, expect, it, vi } from 'vitest' + +type Listener = (event: unknown, payload: unknown) => void +interface LoadedPage { + appId: string + document: number +} + +const mocks = vi.hoisted(() => ({ + listeners: new Map(), + isAppsPage: true, + appsEnabled: true, + page: { appId: 'remove-background', document: 1 } as LoadedPage | null, + whileAuthorizing: () => {}, + capture: vi.fn<(event: string, properties: unknown) => boolean>(() => true) +})) + +vi.mock('electron', () => ({ + ipcMain: { + on: (channel: string, listener: Listener) => mocks.listeners.set(channel, listener) + } +})) +vi.mock('../../apps/appsConfig', () => ({ + areAppsEnabled: async () => { + mocks.whileAuthorizing() + return mocks.appsEnabled + } +})) +vi.mock('../../apps/appsPages', () => ({ isAppsPageSender: () => mocks.isAppsPage })) +vi.mock('../../apps/appPageView', () => ({ loadedAppPage: () => mocks.page })) +vi.mock('../telemetry', () => ({ + capture: mocks.capture, + DEFAULT_EVENT_PROPERTY_NAMES: new Set(['app_version', 'installation_id']) +})) + +import { + PAGE_EVENTS_PER_MINUTE, + PAGE_EVENTS_PER_SESSION, + _resetPageEventLimitsForTest, + trackAppPageEvent +} from '../../apps/appPageEvents' +import { + APPS_TELEMETRY_TRACK_CHANNEL, + registerAppsTelemetryHandlers, + trackFromAppsPage +} from './registerAppsTelemetryHandlers' + +const EVENT = { sender: {}, senderFrame: {} } as unknown as Electron.IpcMainEvent +const RUN = { name: 'run_started', props: { where: 'cloud' } } + +beforeEach(() => { + vi.clearAllMocks() + mocks.listeners.clear() + mocks.isAppsPage = true + mocks.appsEnabled = true + mocks.page = { appId: 'remove-background', document: 1 } + mocks.whileAuthorizing = () => {} + _resetPageEventLimitsForTest() + mocks.capture.mockReturnValue(true) + registerAppsTelemetryHandlers() +}) + +describe('usage events from the hosted Apps page', () => { + it('listens on the channel the Apps preload sends on', async () => { + const listener = mocks.listeners.get(APPS_TELEMETRY_TRACK_CHANNEL) + expect(APPS_TELEMETRY_TRACK_CHANNEL).toBe('apps-telemetry:track') + expect(listener).toBeTypeOf('function') + + listener?.(EVENT, RUN) + + await vi.waitFor(() => + expect(mocks.capture).toHaveBeenCalledWith('comfy.desktop.apps.run_started', { + where: 'cloud', + app_id: 'remove-background' + }) + ) + }) + + it('sends any well-formed event through Desktop telemetry with the Apps prefix', async () => { + expect(await trackFromAppsPage(EVENT, { name: 'click', props: { target: 'run' } })).toBe(true) + expect( + await trackFromAppsPage(EVENT, { + name: 'example_used', + props: { example: 'dance-1', first: true, index: 2 } + }) + ).toBe(true) + + expect(mocks.capture.mock.calls).toEqual([ + ['comfy.desktop.apps.click', { target: 'run', app_id: 'remove-background' }], + [ + 'comfy.desktop.apps.example_used', + { example: 'dance-1', first: true, index: 2, app_id: 'remove-background' } + ] + ]) + }) + + it('fills app_id from the app Desktop loaded in that view, not from the page', async () => { + mocks.page = { appId: 'upscale', document: 3 } + + await trackFromAppsPage(EVENT, { + name: 'click', + props: { app: 'remove-background', app_id: 'remove-background', target: 'run' } + }) + + expect(mocks.capture).toHaveBeenCalledWith('comfy.desktop.apps.click', { + target: 'run', + app_id: 'upscale' + }) + }) + + it.each([ + ['a sender that is not the hosted Apps page', { isAppsPage: false }], + ['the Apps page while Apps is switched off', { appsEnabled: false }], + ['a page Desktop holds no loaded app for, as while the next app loads', { page: null }] + ])('refuses %s', async (_who, state) => { + Object.assign(mocks, state) + + expect(await trackFromAppsPage(EVENT, RUN)).toBe(false) + + expect(mocks.capture).not.toHaveBeenCalled() + }) + + it.each([ + ['the next app loads', () => (mocks.page = null)], + [ + 'the page is replaced by another document', + () => (mocks.page = { appId: 'remove-background', document: 2 }) + ], + ['the view moves to another app', () => (mocks.page = { appId: 'upscale', document: 2 })] + ])('drops an event when %s while Desktop checks the sender', async (_what, change) => { + mocks.whileAuthorizing = change + + expect(await trackFromAppsPage(EVENT, RUN)).toBe(false) + + expect(mocks.capture).not.toHaveBeenCalled() + }) + + it.each([ + ['app_opened, which Desktop sends itself', { name: 'app_opened', props: {} }], + ['app_page_failed, which Desktop sends itself', { name: 'app_page_failed', props: {} }], + ['a name outside the pattern', { name: 'Run Started', props: {} }], + ['a payload with no name', { props: { where: 'cloud' } }], + ['a payload that is not an object', 'run_started'], + ['no payload', undefined] + ])('drops %s', async (_what, payload) => { + expect(await trackFromAppsPage(EVENT, payload)).toBe(false) + + expect(mocks.capture).not.toHaveBeenCalled() + }) + + it('drops free text and properties Desktop sets itself before sending', async () => { + await trackFromAppsPage(EVENT, { + name: 'run_started', + props: { + where: 'cloud', + prompt: 'a cat on the moon', + file: '/Users/someone/me.png', + app_version: '9.9.9', + installation_id: 'fake' + } + }) + + expect(mocks.capture).toHaveBeenCalledWith('comfy.desktop.apps.run_started', { + where: 'cloud', + app_id: 'remove-background' + }) + }) +}) + +describe('how many page events Desktop sends', () => { + const sent = () => mocks.capture.mock.calls.length + + it(`sends at most ${PAGE_EVENTS_PER_MINUTE} a minute for one app, whatever their names, then again a minute later`, () => { + vi.useFakeTimers() + try { + vi.setSystemTime(new Date('2026-11-14T10:00:00Z')) + for (let i = 0; i <= PAGE_EVENTS_PER_MINUTE; i++) { + trackAppPageEvent(`step_${i}`, {}, 'genjutsu') + } + expect(sent()).toBe(PAGE_EVENTS_PER_MINUTE) + + expect(trackAppPageEvent('click', {}, 'upscale')).toBe(true) + + vi.setSystemTime(new Date('2026-11-14T10:01:01Z')) + expect(trackAppPageEvent('click', {}, 'genjutsu')).toBe(true) + expect(sent()).toBe(PAGE_EVENTS_PER_MINUTE + 2) + } finally { + vi.useRealTimers() + } + }) + + it(`sends at most ${PAGE_EVENTS_PER_SESSION} page events in all, so pages cannot use up Desktop's own telemetry`, () => { + vi.useFakeTimers() + try { + for (let i = 0; i < PAGE_EVENTS_PER_SESSION + 5; i++) { + vi.setSystemTime(new Date(Date.UTC(2026, 10, 14, 10, 0, 0) + i * 60_000)) + trackAppPageEvent('click', {}, 'genjutsu') + } + expect(sent()).toBe(PAGE_EVENTS_PER_SESSION) + } finally { + vi.useRealTimers() + } + }) + + it('counts no event telemetry did not send, as while consent is off', () => { + vi.useFakeTimers() + try { + mocks.capture.mockReturnValue(false) + for (let i = 0; i < PAGE_EVENTS_PER_SESSION + 5; i++) { + vi.setSystemTime(new Date(Date.UTC(2026, 10, 14, 10, 0, 0) + i * 60_000)) + trackAppPageEvent('click', {}, 'genjutsu') + } + for (let i = 0; i < PAGE_EVENTS_PER_MINUTE; i++) { + trackAppPageEvent(`step_${i}`, {}, 'genjutsu') + } + + mocks.capture.mockReturnValue(true) + expect(trackAppPageEvent('click', {}, 'genjutsu')).toBe(true) + } finally { + vi.useRealTimers() + } + }) + + it('counts no event it refuses', () => { + for (let i = 0; i < PAGE_EVENTS_PER_MINUTE; i++) trackAppPageEvent('error', {}, 'genjutsu') + + expect(trackAppPageEvent('click', {}, 'genjutsu')).toBe(true) + expect(sent()).toBe(1) + }) +}) diff --git a/src/main/lib/ipc/registerAppsTelemetryHandlers.ts b/src/main/lib/ipc/registerAppsTelemetryHandlers.ts new file mode 100644 index 000000000..01450e6c4 --- /dev/null +++ b/src/main/lib/ipc/registerAppsTelemetryHandlers.ts @@ -0,0 +1,37 @@ +import { ipcMain } from 'electron' +import type { IpcMainEvent } from 'electron' + +import { trackAppPageEvent } from '../../apps/appPageEvents' +import { loadedAppPage } from '../../apps/appPageView' +import type { LoadedAppPage } from '../../apps/appPageView' +import { areAppsEnabled } from '../../apps/appsConfig' +import { isAppsPageSender } from '../../apps/appsPages' + +export const APPS_TELEMETRY_TRACK_CHANNEL = 'apps-telemetry:track' + +interface TrackPayload { + name?: unknown + props?: unknown +} + +async function isFromAppsPage(event: IpcMainEvent): Promise { + return isAppsPageSender(event.sender, event.senderFrame) && (await areAppsEnabled()) +} + +function isSamePage(now: LoadedAppPage | null, sent: LoadedAppPage): boolean { + return now !== null && now.appId === sent.appId && now.document === sent.document +} + +export async function trackFromAppsPage(event: IpcMainEvent, payload: unknown): Promise { + const sentFrom = loadedAppPage(event.sender) + if (sentFrom === null || !(await isFromAppsPage(event))) return false + if (!isSamePage(loadedAppPage(event.sender), sentFrom)) return false + const { name, props } = (payload && typeof payload === 'object' ? payload : {}) as TrackPayload + return trackAppPageEvent(name, props, sentFrom.appId) +} + +export function registerAppsTelemetryHandlers(): void { + ipcMain.on(APPS_TELEMETRY_TRACK_CHANNEL, (event, payload: unknown) => { + trackFromAppsPage(event, payload).catch(() => {}) + }) +} diff --git a/src/preload/appsPreload.ts b/src/preload/appsPreload.ts index 4b3de63e9..f3f81c348 100644 --- a/src/preload/appsPreload.ts +++ b/src/preload/appsPreload.ts @@ -4,6 +4,7 @@ import type { ComfyDesktop2AuthState, ComfyDesktopHostedAuthBridge, ComfyDesktopHostedLocalBridge, + ComfyDesktopHostedTelemetryBridge, ComfyDesktopLocalEvent } from '../types/comfyDesktopBridge' @@ -37,5 +38,10 @@ const local: ComfyDesktopHostedLocalBridge = { } } +const telemetry: ComfyDesktopHostedTelemetryBridge = { + track: (name, props) => ipcRenderer.send('apps-telemetry:track', { name, props }) +} + contextBridge.exposeInMainWorld('comfyDesktopAuth', auth) contextBridge.exposeInMainWorld('comfyDesktopLocal', local) +contextBridge.exposeInMainWorld('comfyDesktopTelemetry', telemetry) diff --git a/src/renderer/src/panel/PanelApp.test.ts b/src/renderer/src/panel/PanelApp.test.ts index f7fc00b63..01fa8ef78 100644 --- a/src/renderer/src/panel/PanelApp.test.ts +++ b/src/renderer/src/panel/PanelApp.test.ts @@ -62,6 +62,9 @@ vi.mock('../views/ChooserView.vue', () => ({ '
' } })) +vi.mock('./AssetsView.vue', () => ({ + default: { name: 'AssetsView', template: '
' } +})) vi.mock('./BuildDetailsView.vue', () => ({ default: { name: 'BuildDetailsView', @@ -1570,6 +1573,44 @@ describe('PanelApp', () => { ) }) + it('records each sidebar page the person opens, and not a click on the page already shown', async () => { + window.history.replaceState({}, '', '/?panel=chooser&firstUseCompleted=true') + vi.mocked(window.api.getAppsEnabled).mockResolvedValue(true) + const wrapper = mountPanel() + await flushPromises() + const events = captureTelemetry() + const opened = () => + events.filter(({ actionName }) => actionName === 'comfy.desktop.sidebar.page_opened') + + for (const page of ['home', 'apps', 'apps', 'assets', 'chooser', 'chooser', 'home']) { + await wrapper.get(`[data-testid="app-sidebar-${page}"]`).trigger('click') + await flushPromises() + } + + expect(opened().map(({ context }) => context)).toEqual([ + { page: 'apps', from_page: 'home' }, + { page: 'assets', from_page: 'apps' }, + { page: 'chooser', from_page: 'assets' }, + { page: 'home', from_page: 'chooser' } + ]) + }) + + it('does not record a sidebar page open for See all apps on Home', async () => { + window.history.replaceState({}, '', '/?panel=chooser&firstUseCompleted=true') + vi.mocked(window.api.getAppsEnabled).mockResolvedValue(true) + const wrapper = mountPanel() + await flushPromises() + const events = captureTelemetry() + + await wrapper.get('[data-testid="home-see-all-apps"]').trigger('click') + await flushPromises() + + expect(wrapper.get('[data-testid="apps-view"]').isVisible()).toBe(true) + expect( + events.filter(({ actionName }) => actionName === 'comfy.desktop.sidebar.page_opened') + ).toEqual([]) + }) + it('returns to the list of builds from All builds on Home', async () => { window.history.replaceState({}, '', '/?panel=chooser&firstUseCompleted=true') const wrapper = mountPanel() diff --git a/src/renderer/src/panel/PanelApp.vue b/src/renderer/src/panel/PanelApp.vue index f4d57d20a..30b4358d3 100644 --- a/src/renderer/src/panel/PanelApp.vue +++ b/src/renderer/src/panel/PanelApp.vue @@ -237,6 +237,16 @@ function openAppFromHome(app: AppCard): void { dashboardHomePage.value = 'apps' } +const SIDEBAR_PAGE_OPENED_EVENT = 'comfy.desktop.sidebar.page_opened' + +function handleSidebarClick(page: DashboardPage): void { + const fromPage = sidebarPage.value + if (page !== fromPage) { + emitTelemetryAction(SIDEBAR_PAGE_OPENED_EVENT, { page, from_page: fromPage }) + } + handleSidebarNavigate(page) +} + function handleSidebarNavigate(page: DashboardPage): void { closeBuildDetails() if (page === 'apps') appsVisits.value += 1 @@ -503,7 +513,7 @@ onUnmounted(() => { :active-page="sidebarPage" :active-build-id="sidebarBuildId" :make-room="appIsOpen" - @navigate="handleSidebarNavigate" + @navigate="handleSidebarClick" @open-build="openBuildFromSidebar" @new-build="handleChooserShowNewInstall" /> diff --git a/src/shared/appsLocation.ts b/src/shared/appsLocation.ts index e4f342ad4..b17948bc2 100644 --- a/src/shared/appsLocation.ts +++ b/src/shared/appsLocation.ts @@ -1,4 +1,5 @@ export const DEFAULT_APPS_URL = 'https://comfy.org/apps/' +export const APP_ID_PATTERN = /^[a-z0-9][a-z0-9_-]{0,127}$/i export interface AppsLocation { url: string diff --git a/src/types/comfyDesktopBridge.ts b/src/types/comfyDesktopBridge.ts index 564cf5cdf..6f1c71606 100644 --- a/src/types/comfyDesktopBridge.ts +++ b/src/types/comfyDesktopBridge.ts @@ -300,6 +300,24 @@ export interface ComfyDesktopHostedLocalBridge { localBuilds?(): Promise } +/** + * Usage events from a Comfy page hosted in Desktop's Apps view. Desktop + * exposes it to that page as `window.comfyDesktopTelemetry` and sends an event + * as `comfy.desktop.apps.` only when the person allows telemetry. + * `name` matches `^[a-z][a-z0-9_]{0,47}$`; `app_opened` and `app_page_failed` + * are refused because Desktop sends them itself, and so is `error`. Desktop + * sends at most 60 events a minute for one app and 1,000 page events a + * session, and drops the rest. Desktop keeps at most 16 + * properties, each named `^[a-z][a-z0-9_]{0,31}$` with a boolean, a finite + * number or a slug of at most 64 characters (`^[a-z0-9][a-z0-9._-]{0,63}$`), + * and drops anything else. Desktop sets `app_id` itself, to the app whose + * page it loaded in that view; an `app` or `app_id` from the page is ignored, + * and an event sent while the view loads another app is dropped. + */ +export interface ComfyDesktopHostedTelemetryBridge { + track(name: string, props?: Record): void +} + export interface ComfyDesktop2TerminalBridge { subscribe(installationId?: string): Promise unsubscribe(installationId?: string): Promise