Track feature interactions (#2959)
Co-authored-by: Orca <help@stably.ai>
This commit is contained in:
@@ -4,6 +4,7 @@ import { EventEmitter } from 'events'
|
||||
import { lstat, mkdir, mkdtemp, rm, writeFile } from 'fs/promises'
|
||||
import { tmpdir } from 'os'
|
||||
import { join } from 'path'
|
||||
import { ipcMain } from 'electron'
|
||||
import type { WorktreeLineage, WorktreeMeta } from '../../shared/types'
|
||||
import {
|
||||
addWorktree,
|
||||
@@ -34,6 +35,36 @@ import { registerSshGitProvider, unregisterSshGitProvider } from '../providers/s
|
||||
import { DEFAULT_REPO_BADGE_COLOR } from '../../shared/constants'
|
||||
import { advertisedUrlWatcher } from '../ports/advertised-url-watcher'
|
||||
|
||||
const electronMocks = vi.hoisted(() => {
|
||||
type Listener = (...args: unknown[]) => void
|
||||
const listeners = new Map<string, Set<Listener>>()
|
||||
const ipcMain = {
|
||||
on: vi.fn((channel: string, listener: Listener) => {
|
||||
const existing = listeners.get(channel) ?? new Set<Listener>()
|
||||
existing.add(listener)
|
||||
listeners.set(channel, existing)
|
||||
return ipcMain
|
||||
}),
|
||||
removeListener: vi.fn((channel: string, listener: Listener) => {
|
||||
listeners.get(channel)?.delete(listener)
|
||||
return ipcMain
|
||||
}),
|
||||
emit: vi.fn((channel: string, ...args: unknown[]) => {
|
||||
for (const listener of listeners.get(channel) ?? []) {
|
||||
listener(...args)
|
||||
}
|
||||
return true
|
||||
})
|
||||
}
|
||||
return {
|
||||
BrowserWindow: { fromId: vi.fn((_id: number): unknown => null) },
|
||||
ipcMain,
|
||||
app: { getPath: vi.fn(() => '/tmp') }
|
||||
}
|
||||
})
|
||||
|
||||
vi.mock('electron', () => electronMocks)
|
||||
|
||||
const {
|
||||
MOCK_GIT_WORKTREES,
|
||||
addWorktreeMock,
|
||||
@@ -273,6 +304,11 @@ vi.mock('../git/repo', async (importOriginal) => {
|
||||
|
||||
afterEach(() => {
|
||||
advertisedUrlWatcher.clear()
|
||||
electronMocks.BrowserWindow.fromId.mockReset()
|
||||
electronMocks.BrowserWindow.fromId.mockReturnValue(null)
|
||||
electronMocks.ipcMain.on.mockClear()
|
||||
electronMocks.ipcMain.removeListener.mockClear()
|
||||
electronMocks.ipcMain.emit.mockClear()
|
||||
vi.mocked(listWorktrees).mockResolvedValue(MOCK_GIT_WORKTREES)
|
||||
vi.mocked(addWorktree).mockReset()
|
||||
vi.mocked(assertWorktreeCleanForRemoval).mockReset()
|
||||
@@ -4971,6 +5007,81 @@ describe('OrcaRuntimeService', () => {
|
||||
])
|
||||
})
|
||||
|
||||
it('forwards inactive mobile terminal creation to the renderer without focusing it', async () => {
|
||||
const focusTerminal = vi.fn()
|
||||
const runtime = new OrcaRuntimeService(store)
|
||||
runtime.setNotifier({
|
||||
focusTerminal,
|
||||
worktreesChanged: vi.fn(),
|
||||
reposChanged: vi.fn(),
|
||||
activateWorktree: vi.fn(),
|
||||
createTerminal: vi.fn(),
|
||||
revealTerminalSession: vi.fn(),
|
||||
splitTerminal: vi.fn(),
|
||||
renameTerminal: vi.fn(),
|
||||
closeTerminal: vi.fn(),
|
||||
closeSessionTab: vi.fn(),
|
||||
sleepWorktree: vi.fn(),
|
||||
terminalFitOverrideChanged: vi.fn(),
|
||||
terminalDriverChanged: vi.fn()
|
||||
})
|
||||
const send = vi.fn((_channel: string, payload: { requestId: string; activate?: boolean }) => {
|
||||
runtime.syncWindowGraph(1, {
|
||||
tabs: [],
|
||||
leaves: [],
|
||||
mobileSessionTabs: [
|
||||
{
|
||||
worktree: TEST_WORKTREE_ID,
|
||||
publicationEpoch: 'epoch-1',
|
||||
snapshotVersion: 1,
|
||||
activeGroupId: 'group-1',
|
||||
activeTabId: null,
|
||||
activeTabType: null,
|
||||
tabs: [
|
||||
{
|
||||
type: 'terminal',
|
||||
id: 'tab-renderer::pane:1',
|
||||
parentTabId: 'tab-renderer',
|
||||
leafId: 'pane:1',
|
||||
title: 'Terminal',
|
||||
isActive: false
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
})
|
||||
ipcMain.emit(
|
||||
'terminal:tabCreateReply',
|
||||
{},
|
||||
{
|
||||
requestId: payload.requestId,
|
||||
tabId: 'tab-renderer',
|
||||
title: 'Terminal'
|
||||
}
|
||||
)
|
||||
})
|
||||
runtime.attachWindow(1)
|
||||
runtime.syncWindowGraph(1, { tabs: [], leaves: [] })
|
||||
electronMocks.BrowserWindow.fromId.mockReturnValue({
|
||||
isDestroyed: () => false,
|
||||
webContents: { send }
|
||||
})
|
||||
|
||||
const result = await runtime.createMobileSessionTerminal(`id:${TEST_WORKTREE_ID}`, {
|
||||
activate: false
|
||||
})
|
||||
|
||||
expect(send).toHaveBeenCalledWith(
|
||||
'terminal:requestTabCreate',
|
||||
expect.objectContaining({
|
||||
worktreeId: TEST_WORKTREE_ID,
|
||||
activate: false
|
||||
})
|
||||
)
|
||||
expect(focusTerminal).not.toHaveBeenCalled()
|
||||
expect(result.tab).toMatchObject({ parentTabId: 'tab-renderer', isActive: false })
|
||||
})
|
||||
|
||||
it('reports browser tab creation as unsupported for headless runtime servers', async () => {
|
||||
const runtime = new OrcaRuntimeService(store)
|
||||
runtime.syncWindowGraph(0, { tabs: [], leaves: [] })
|
||||
|
||||
@@ -56,6 +56,7 @@ import type {
|
||||
TabGroupLayoutNode,
|
||||
TuiAgent
|
||||
} from '../../shared/types'
|
||||
import type { FeatureInteractionId } from '../../shared/feature-interactions'
|
||||
import { FOLDER_WORKSPACE_INSTANCE_SEPARATOR, splitWorktreeId } from '../../shared/worktree-id'
|
||||
import { isFolderRepo } from '../../shared/repo-kind'
|
||||
import { getNextProjectGroupOrder } from '../../shared/project-groups'
|
||||
@@ -439,6 +440,7 @@ type RuntimeStore = {
|
||||
getWorkspaceSession?: Store['getWorkspaceSession']
|
||||
getUI?: Store['getUI']
|
||||
updateUI?: Store['updateUI']
|
||||
recordFeatureInteraction?: Store['recordFeatureInteraction']
|
||||
listAutomations?: Store['listAutomations']
|
||||
listAutomationRuns?: Store['listAutomationRuns']
|
||||
createAutomation?: Store['createAutomation']
|
||||
@@ -1330,6 +1332,13 @@ export class OrcaRuntimeService {
|
||||
return this.store.getUI()
|
||||
}
|
||||
|
||||
recordFeatureInteraction(id: FeatureInteractionId): PersistedUIState {
|
||||
if (!this.store?.recordFeatureInteraction) {
|
||||
throw new Error('runtime_unavailable')
|
||||
}
|
||||
return this.store.recordFeatureInteraction(id)
|
||||
}
|
||||
|
||||
getClientSettings(): Pick<
|
||||
GlobalSettings,
|
||||
| 'defaultTuiAgent'
|
||||
@@ -9289,7 +9298,8 @@ export class OrcaRuntimeService {
|
||||
worktreeId,
|
||||
afterTabId: afterDesktopTabId,
|
||||
targetGroupId: opts.targetGroupId,
|
||||
command: opts.command
|
||||
command: opts.command,
|
||||
activate: opts.activate
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
@@ -0,0 +1,229 @@
|
||||
import { describe, expect, it, vi } from 'vitest'
|
||||
import { z } from 'zod'
|
||||
import type { PersistedUIState } from '../../../shared/types'
|
||||
import { getDefaultUIState } from '../../../shared/constants'
|
||||
import {
|
||||
ORCA_RUNTIME_RPC_BROWSER_UI_SOURCE,
|
||||
ORCA_RUNTIME_RPC_FEATURE_INTERACTION_SOURCE_KEY
|
||||
} from '../../../shared/runtime-rpc-feature-interaction-source'
|
||||
import { RpcDispatcher } from './dispatcher'
|
||||
import { defineMethod, defineStreamingMethod, type RpcRequest } from './core'
|
||||
import type { OrcaRuntimeService } from '../orca-runtime'
|
||||
|
||||
function makeRequest(method: string, params: unknown = {}): RpcRequest {
|
||||
return { id: 'req-1', authToken: 'tok', method, params }
|
||||
}
|
||||
|
||||
function makeRuntime(ui: PersistedUIState = getDefaultUIState()): OrcaRuntimeService {
|
||||
let currentUI = ui
|
||||
return {
|
||||
getRuntimeId: () => 'test-runtime',
|
||||
getUIState: vi.fn(() => currentUI),
|
||||
recordFeatureInteraction: vi.fn((id) => {
|
||||
const featureInteractions = currentUI.featureInteractions ?? {}
|
||||
const existing = featureInteractions[id]
|
||||
currentUI = {
|
||||
...currentUI,
|
||||
featureInteractions: {
|
||||
...featureInteractions,
|
||||
[id]: {
|
||||
firstInteractedAt: existing?.firstInteractedAt ?? Date.now(),
|
||||
interactionCount: (existing?.interactionCount ?? 0) + 1
|
||||
}
|
||||
}
|
||||
}
|
||||
return currentUI
|
||||
}),
|
||||
updateUIState: vi.fn((updates: Partial<PersistedUIState>) => {
|
||||
currentUI = { ...currentUI, ...updates }
|
||||
return currentUI
|
||||
})
|
||||
} as unknown as OrcaRuntimeService
|
||||
}
|
||||
|
||||
const METHODS = [
|
||||
defineMethod({
|
||||
name: 'browser.click',
|
||||
params: z.object({}),
|
||||
handler: () => ({ clicked: true })
|
||||
}),
|
||||
defineMethod({
|
||||
name: 'browser.tabCreate',
|
||||
params: z.object({}),
|
||||
handler: () => ({ browserPageId: 'page-1' })
|
||||
}),
|
||||
defineMethod({
|
||||
name: 'browser.tabShow',
|
||||
params: z.object({}),
|
||||
handler: () => ({ tab: { id: 'page-1' } })
|
||||
}),
|
||||
defineMethod({
|
||||
name: 'browser.viewport',
|
||||
params: z.object({}),
|
||||
handler: () => ({ ok: true })
|
||||
}),
|
||||
defineMethod({
|
||||
name: 'browser.eval',
|
||||
params: z.object({}),
|
||||
handler: () => ({ value: 'ok' })
|
||||
}),
|
||||
defineStreamingMethod({
|
||||
name: 'browser.screencast',
|
||||
params: z.object({}),
|
||||
handler: async (_params, _options, emit) => {
|
||||
emit({ type: 'frame' })
|
||||
emit({ type: 'end' })
|
||||
}
|
||||
}),
|
||||
defineStreamingMethod({
|
||||
name: 'browser.screencast.binaryOnly',
|
||||
params: z.object({}),
|
||||
handler: async () => {}
|
||||
}),
|
||||
defineMethod({
|
||||
name: 'browser.screencast.unsubscribe',
|
||||
params: z.object({}),
|
||||
handler: () => ({ ok: true })
|
||||
}),
|
||||
defineMethod({
|
||||
name: 'browser.profileImportFromBrowser',
|
||||
params: z.object({}),
|
||||
handler: () => ({ ok: true })
|
||||
}),
|
||||
defineMethod({
|
||||
name: 'browser.profileList',
|
||||
params: z.object({}),
|
||||
handler: () => ({ profiles: [] })
|
||||
}),
|
||||
defineMethod({
|
||||
name: 'browser.profileClearDefaultCookies',
|
||||
params: z.object({}),
|
||||
handler: () => ({ cleared: false })
|
||||
}),
|
||||
defineMethod({
|
||||
name: 'computer.permissions',
|
||||
params: z.object({}),
|
||||
handler: () => ({ opened: true })
|
||||
}),
|
||||
defineMethod({
|
||||
name: 'computer.click',
|
||||
params: z.object({}),
|
||||
handler: () => ({ clicked: true })
|
||||
}),
|
||||
defineMethod({
|
||||
name: 'orchestration.send',
|
||||
params: z.object({}),
|
||||
handler: () => ({ id: 'msg-1' })
|
||||
}),
|
||||
defineMethod({
|
||||
name: 'browser.fail',
|
||||
params: z.object({}),
|
||||
handler: () => {
|
||||
throw new Error('nope')
|
||||
}
|
||||
})
|
||||
]
|
||||
|
||||
describe('RpcDispatcher feature interactions', () => {
|
||||
it('records runtime feature use after successful runtime tool methods', async () => {
|
||||
const runtime = makeRuntime()
|
||||
const dispatcher = new RpcDispatcher({ runtime, methods: METHODS })
|
||||
|
||||
await dispatcher.dispatch(makeRequest('browser.click'))
|
||||
await dispatcher.dispatch(makeRequest('computer.click'))
|
||||
await dispatcher.dispatch(makeRequest('orchestration.send'))
|
||||
|
||||
expect(runtime.recordFeatureInteraction).toHaveBeenCalledWith('agent-browser-use')
|
||||
expect(runtime.recordFeatureInteraction).toHaveBeenCalledWith('computer-use')
|
||||
expect(runtime.recordFeatureInteraction).toHaveBeenCalledWith('agent-orchestration')
|
||||
})
|
||||
|
||||
it('keeps setup and cookie import separate from actual runtime use', async () => {
|
||||
const runtime = makeRuntime()
|
||||
const dispatcher = new RpcDispatcher({ runtime, methods: METHODS })
|
||||
|
||||
await dispatcher.dispatch(makeRequest('computer.permissions'))
|
||||
await dispatcher.dispatch(makeRequest('browser.profileImportFromBrowser'))
|
||||
await dispatcher.dispatch(makeRequest('browser.profileList'))
|
||||
await dispatcher.dispatch(makeRequest('browser.profileClearDefaultCookies'))
|
||||
|
||||
expect(runtime.recordFeatureInteraction).toHaveBeenCalledWith('computer-use-setup')
|
||||
expect(runtime.recordFeatureInteraction).toHaveBeenCalledWith('cookie-import')
|
||||
expect(runtime.recordFeatureInteraction).toHaveBeenCalledTimes(2)
|
||||
})
|
||||
|
||||
it('does not record failed runtime methods', async () => {
|
||||
const runtime = makeRuntime()
|
||||
const dispatcher = new RpcDispatcher({ runtime, methods: METHODS })
|
||||
|
||||
await dispatcher.dispatch(makeRequest('browser.fail'))
|
||||
|
||||
expect(runtime.recordFeatureInteraction).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('records unmarked browser display RPCs as agent browser use', async () => {
|
||||
const runtime = makeRuntime()
|
||||
const dispatcher = new RpcDispatcher({ runtime, methods: METHODS })
|
||||
const replies: string[] = []
|
||||
|
||||
await dispatcher.dispatch(makeRequest('browser.tabCreate'))
|
||||
await dispatcher.dispatch(makeRequest('browser.tabShow'))
|
||||
await dispatcher.dispatch(makeRequest('browser.screencast.unsubscribe'))
|
||||
expect(runtime.recordFeatureInteraction).toHaveBeenCalledTimes(2)
|
||||
expect(runtime.recordFeatureInteraction).toHaveBeenNthCalledWith(1, 'agent-browser-use')
|
||||
expect(runtime.recordFeatureInteraction).toHaveBeenNthCalledWith(2, 'agent-browser-use')
|
||||
|
||||
await dispatcher.dispatchStreaming(makeRequest('browser.screencast'), (response) => {
|
||||
replies.push(response)
|
||||
})
|
||||
|
||||
expect(replies).toHaveLength(2)
|
||||
expect(runtime.recordFeatureInteraction).toHaveBeenCalledTimes(3)
|
||||
expect(runtime.recordFeatureInteraction).toHaveBeenLastCalledWith('agent-browser-use')
|
||||
})
|
||||
|
||||
it('records binary-only browser display streams as agent browser use', async () => {
|
||||
const runtime = makeRuntime()
|
||||
const dispatcher = new RpcDispatcher({ runtime, methods: METHODS })
|
||||
const replies: string[] = []
|
||||
|
||||
await dispatcher.dispatchStreaming(makeRequest('browser.screencast.binaryOnly'), (response) => {
|
||||
replies.push(response)
|
||||
})
|
||||
|
||||
expect(replies).toHaveLength(0)
|
||||
expect(runtime.recordFeatureInteraction).toHaveBeenCalledTimes(1)
|
||||
expect(runtime.recordFeatureInteraction).toHaveBeenCalledWith('agent-browser-use')
|
||||
})
|
||||
|
||||
it('does not record browser pane UI-originated browser RPCs as agent browser use', async () => {
|
||||
const runtime = makeRuntime()
|
||||
const dispatcher = new RpcDispatcher({ runtime, methods: METHODS })
|
||||
const browserPaneUiParams = {
|
||||
[ORCA_RUNTIME_RPC_FEATURE_INTERACTION_SOURCE_KEY]: ORCA_RUNTIME_RPC_BROWSER_UI_SOURCE
|
||||
}
|
||||
|
||||
await dispatcher.dispatch(makeRequest('browser.viewport', browserPaneUiParams))
|
||||
await dispatcher.dispatch(makeRequest('browser.eval', browserPaneUiParams))
|
||||
await dispatcher.dispatchStreaming(
|
||||
makeRequest('browser.screencast', browserPaneUiParams),
|
||||
() => {}
|
||||
)
|
||||
await dispatcher.dispatch(makeRequest('browser.click'))
|
||||
|
||||
expect(runtime.recordFeatureInteraction).toHaveBeenCalledTimes(1)
|
||||
expect(runtime.recordFeatureInteraction).toHaveBeenCalledWith('agent-browser-use')
|
||||
})
|
||||
|
||||
it('records each successful non-streaming runtime feature interaction', async () => {
|
||||
const runtime = makeRuntime()
|
||||
const dispatcher = new RpcDispatcher({ runtime, methods: METHODS })
|
||||
|
||||
await dispatcher.dispatch(makeRequest('browser.click'))
|
||||
await dispatcher.dispatch(makeRequest('browser.click'))
|
||||
await dispatcher.dispatch(makeRequest('browser.screencast.unsubscribe'))
|
||||
|
||||
expect(runtime.recordFeatureInteraction).toHaveBeenCalledTimes(2)
|
||||
expect(runtime.recordFeatureInteraction).toHaveBeenLastCalledWith('agent-browser-use')
|
||||
})
|
||||
})
|
||||
@@ -14,6 +14,8 @@ import {
|
||||
type RpcResponse
|
||||
} from './core'
|
||||
import type { TerminalStreamFrame } from '../../../shared/terminal-stream-protocol'
|
||||
import type { FeatureInteractionId } from '../../../shared/feature-interactions'
|
||||
import { isBrowserPaneUiRuntimeRpcParams } from '../../../shared/runtime-rpc-feature-interaction-source'
|
||||
import { errorResponse, mapBrowserError, mapRuntimeError, successResponse } from './errors'
|
||||
import { ALL_RPC_METHODS } from './methods'
|
||||
import type { OrcaRuntimeService } from '../orca-runtime'
|
||||
@@ -66,6 +68,7 @@ export class RpcDispatcher {
|
||||
runtime: this.runtime,
|
||||
signal: options?.signal
|
||||
})
|
||||
this.recordRuntimeFeatureInteraction(request.method, result, undefined, request.params)
|
||||
return successResponse(request.id, meta, result)
|
||||
} catch (error) {
|
||||
return this.mapError(request, meta, error)
|
||||
@@ -116,6 +119,7 @@ export class RpcDispatcher {
|
||||
sendBinary: options?.sendBinary,
|
||||
registerBinaryStreamHandler: options?.registerBinaryStreamHandler
|
||||
})
|
||||
this.recordRuntimeFeatureInteraction(request.method, result, undefined, request.params)
|
||||
reply(JSON.stringify(successResponse(request.id, meta, result)))
|
||||
} catch (error) {
|
||||
reply(JSON.stringify(this.mapError(request, meta, error)))
|
||||
@@ -123,14 +127,21 @@ export class RpcDispatcher {
|
||||
return
|
||||
}
|
||||
|
||||
const recordedStreamingFeatureInteractions = new Set<FeatureInteractionId>()
|
||||
const emit = (result: unknown): void => {
|
||||
this.recordRuntimeFeatureInteraction(
|
||||
request.method,
|
||||
result,
|
||||
recordedStreamingFeatureInteractions,
|
||||
request.params
|
||||
)
|
||||
const response = successResponse(request.id, meta, result)
|
||||
response.streaming = true
|
||||
reply(JSON.stringify(response))
|
||||
}
|
||||
|
||||
try {
|
||||
await method.handler(
|
||||
const result = await method.handler(
|
||||
parsedParams.value,
|
||||
{
|
||||
runtime: this.runtime,
|
||||
@@ -142,6 +153,12 @@ export class RpcDispatcher {
|
||||
},
|
||||
emit
|
||||
)
|
||||
this.recordRuntimeFeatureInteraction(
|
||||
request.method,
|
||||
result,
|
||||
recordedStreamingFeatureInteractions,
|
||||
request.params
|
||||
)
|
||||
} catch (error) {
|
||||
reply(JSON.stringify(this.mapError(request, meta, error)))
|
||||
}
|
||||
@@ -182,4 +199,63 @@ export class RpcDispatcher {
|
||||
private meta(): RpcEnvelopeMeta {
|
||||
return { runtimeId: this.runtime.getRuntimeId() }
|
||||
}
|
||||
|
||||
private recordRuntimeFeatureInteraction(
|
||||
method: string,
|
||||
result: unknown,
|
||||
alreadyRecorded?: Set<FeatureInteractionId>,
|
||||
rawParams?: unknown
|
||||
): void {
|
||||
const id = getRuntimeFeatureInteractionId(method, result, rawParams)
|
||||
if (!id) {
|
||||
return
|
||||
}
|
||||
if (alreadyRecorded?.has(id)) {
|
||||
return
|
||||
}
|
||||
try {
|
||||
this.runtime.recordFeatureInteraction(id)
|
||||
alreadyRecorded?.add(id)
|
||||
} catch {
|
||||
// Best-effort education state must not break runtime tools.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function getRuntimeFeatureInteractionId(
|
||||
method: string,
|
||||
result: unknown,
|
||||
rawParams?: unknown
|
||||
): FeatureInteractionId | null {
|
||||
if (method === 'browser.profileImportFromBrowser') {
|
||||
return hasBooleanResult(result, 'ok') ? 'cookie-import' : null
|
||||
}
|
||||
if (method === 'browser.profileClearDefaultCookies') {
|
||||
return hasBooleanResult(result, 'cleared') ? 'cookie-import' : null
|
||||
}
|
||||
if (method === 'browser.screencast.unsubscribe') {
|
||||
return null
|
||||
}
|
||||
if (method.startsWith('browser.') && isBrowserPaneUiRuntimeRpcParams(rawParams)) {
|
||||
return null
|
||||
}
|
||||
if (method.startsWith('browser.') && !method.startsWith('browser.profile')) {
|
||||
return 'agent-browser-use'
|
||||
}
|
||||
if (method === 'computer.permissions') {
|
||||
return 'computer-use-setup'
|
||||
}
|
||||
if (method.startsWith('computer.') && method !== 'computer.capabilities') {
|
||||
return 'computer-use'
|
||||
}
|
||||
if (method.startsWith('orchestration.')) {
|
||||
return 'agent-orchestration'
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
function hasBooleanResult(value: unknown, key: string): boolean {
|
||||
return (
|
||||
value !== null && typeof value === 'object' && (value as Record<string, unknown>)[key] === true
|
||||
)
|
||||
}
|
||||
|
||||
@@ -160,6 +160,10 @@ describe('client UI RPC methods', () => {
|
||||
classifierVersion: 2
|
||||
}
|
||||
}
|
||||
},
|
||||
featureTipsSeenIds: ['voice-dictation'],
|
||||
featureInteractions: {
|
||||
tasks: { firstInteractedAt: 100, interactionCount: 2 }
|
||||
}
|
||||
}
|
||||
const runtime = {
|
||||
@@ -187,6 +191,10 @@ describe('client UI RPC methods', () => {
|
||||
classifierVersion: 2
|
||||
}
|
||||
}
|
||||
},
|
||||
featureTipsSeenIds: ['voice-dictation'],
|
||||
featureInteractions: {
|
||||
tasks: { firstInteractedAt: 100, interactionCount: 2 }
|
||||
}
|
||||
}
|
||||
const response = await dispatcher.dispatch(makeRequest('ui.set', payload))
|
||||
@@ -195,6 +203,25 @@ describe('client UI RPC methods', () => {
|
||||
expect(response).toMatchObject({ ok: true, result: { ui: updated } })
|
||||
})
|
||||
|
||||
it('records a feature interaction through the runtime host', async () => {
|
||||
const updated: PersistedUIState = {
|
||||
...getDefaultUIState(),
|
||||
featureInteractions: {
|
||||
tasks: { firstInteractedAt: 100, interactionCount: 1 }
|
||||
}
|
||||
}
|
||||
const runtime = {
|
||||
getRuntimeId: () => 'test-runtime',
|
||||
recordFeatureInteraction: vi.fn(() => updated)
|
||||
} as unknown as OrcaRuntimeService
|
||||
const dispatcher = new RpcDispatcher({ runtime, methods: CLIENT_UI_METHODS })
|
||||
|
||||
const response = await dispatcher.dispatch(makeRequest('ui.recordFeatureInteraction', 'tasks'))
|
||||
|
||||
expect(runtime.recordFeatureInteraction).toHaveBeenCalledWith('tasks')
|
||||
expect(response).toMatchObject({ ok: true, result: { ui: updated } })
|
||||
})
|
||||
|
||||
it('rejects unknown and malformed UI update fields', async () => {
|
||||
const runtime = {
|
||||
getRuntimeId: () => 'test-runtime',
|
||||
@@ -209,4 +236,53 @@ describe('client UI RPC methods', () => {
|
||||
expect(response).toMatchObject({ ok: false, error: { code: 'invalid_argument' } })
|
||||
expect(runtime.updateUIState).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('rejects unknown feature interaction ids', async () => {
|
||||
const runtime = {
|
||||
getRuntimeId: () => 'test-runtime',
|
||||
updateUIState: vi.fn()
|
||||
} as unknown as OrcaRuntimeService
|
||||
const dispatcher = new RpcDispatcher({ runtime, methods: CLIENT_UI_METHODS })
|
||||
|
||||
const response = await dispatcher.dispatch(
|
||||
makeRequest('ui.set', {
|
||||
featureInteractions: {
|
||||
unknown: { firstInteractedAt: 100 }
|
||||
}
|
||||
})
|
||||
)
|
||||
|
||||
expect(response).toMatchObject({ ok: false, error: { code: 'invalid_argument' } })
|
||||
expect(runtime.updateUIState).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('rejects unknown feature tip ids', async () => {
|
||||
const runtime = {
|
||||
getRuntimeId: () => 'test-runtime',
|
||||
updateUIState: vi.fn()
|
||||
} as unknown as OrcaRuntimeService
|
||||
const dispatcher = new RpcDispatcher({ runtime, methods: CLIENT_UI_METHODS })
|
||||
|
||||
const response = await dispatcher.dispatch(
|
||||
makeRequest('ui.set', { featureTipsSeenIds: ['voice-dictation', 'unknown-tip'] })
|
||||
)
|
||||
|
||||
expect(response).toMatchObject({ ok: false, error: { code: 'invalid_argument' } })
|
||||
expect(runtime.updateUIState).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('rejects unknown feature interaction ids for increment RPC', async () => {
|
||||
const runtime = {
|
||||
getRuntimeId: () => 'test-runtime',
|
||||
recordFeatureInteraction: vi.fn()
|
||||
} as unknown as OrcaRuntimeService
|
||||
const dispatcher = new RpcDispatcher({ runtime, methods: CLIENT_UI_METHODS })
|
||||
|
||||
const response = await dispatcher.dispatch(
|
||||
makeRequest('ui.recordFeatureInteraction', 'unknown-feature')
|
||||
)
|
||||
|
||||
expect(response).toMatchObject({ ok: false, error: { code: 'invalid_argument' } })
|
||||
expect(runtime.recordFeatureInteraction).not.toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,10 +1,16 @@
|
||||
import { z } from 'zod'
|
||||
import {
|
||||
isFeatureInteractionId,
|
||||
type FeatureInteractionId
|
||||
} from '../../../../shared/feature-interactions'
|
||||
import { isFeatureTipId } from '../../../../shared/feature-tips'
|
||||
import { isTuiAgent } from '../../../../shared/tui-agent-config'
|
||||
import type { PersistedUIState } from '../../../../shared/types'
|
||||
import { defineMethod, type RpcMethod } from '../core'
|
||||
|
||||
const NullableString = z.string().nullable()
|
||||
const StringArray = z.array(z.string())
|
||||
const FeatureTipIds = z.array(z.custom(isFeatureTipId, { message: 'Unknown feature tip id' }))
|
||||
const UnknownRecord = z.record(z.string(), z.unknown())
|
||||
const UnknownRecordArray = z.array(UnknownRecord)
|
||||
const WorktreeCardProperty = z.enum([
|
||||
@@ -48,6 +54,28 @@ const WorkspaceCleanup = z
|
||||
dismissals: z.record(z.string(), WorkspaceCleanupDismissal)
|
||||
})
|
||||
.strict()
|
||||
const FeatureInteractionRecord = z
|
||||
.object({
|
||||
firstInteractedAt: z.number().finite().nonnegative(),
|
||||
interactionCount: z.number().int().positive().optional()
|
||||
})
|
||||
.strict()
|
||||
const FeatureInteractions = z
|
||||
.record(z.string(), FeatureInteractionRecord)
|
||||
.superRefine((value, ctx) => {
|
||||
for (const id of Object.keys(value)) {
|
||||
if (!isFeatureInteractionId(id)) {
|
||||
ctx.addIssue({
|
||||
code: z.ZodIssueCode.custom,
|
||||
message: `Unknown feature interaction id: ${id}`,
|
||||
path: [id]
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
const FeatureInteractionIdParam = z.custom<FeatureInteractionId>(isFeatureInteractionId, {
|
||||
message: 'Unknown feature interaction id'
|
||||
})
|
||||
const GitHubProjectRef = z
|
||||
.object({
|
||||
owner: z.string(),
|
||||
@@ -159,7 +187,9 @@ const UiUpdate = z
|
||||
customSidekicks: UnknownRecordArray.optional(),
|
||||
sidekickSize: z.number().finite().optional(),
|
||||
taskResumeState: TaskResumeState.optional(),
|
||||
workspaceCleanup: WorkspaceCleanup.optional()
|
||||
workspaceCleanup: WorkspaceCleanup.optional(),
|
||||
featureTipsSeenIds: FeatureTipIds.optional(),
|
||||
featureInteractions: FeatureInteractions.optional()
|
||||
})
|
||||
.strict()
|
||||
.default({})
|
||||
@@ -186,5 +216,12 @@ export const CLIENT_UI_METHODS: RpcMethod[] = [
|
||||
handler: (params, { runtime }) => ({
|
||||
ui: runtime.updateUIState(params as Partial<PersistedUIState>)
|
||||
})
|
||||
}),
|
||||
defineMethod({
|
||||
name: 'ui.recordFeatureInteraction',
|
||||
params: FeatureInteractionIdParam,
|
||||
handler: (params, { runtime }) => ({
|
||||
ui: runtime.recordFeatureInteraction(params)
|
||||
})
|
||||
})
|
||||
]
|
||||
|
||||
@@ -270,6 +270,7 @@ const MOBILE_RPC_METHOD_ALLOWLIST = new Set([
|
||||
'terminal.unsubscribe',
|
||||
'terminal.updateViewport',
|
||||
'ui.get',
|
||||
'ui.recordFeatureInteraction',
|
||||
'ui.set',
|
||||
'worktree.activate',
|
||||
'worktree.create',
|
||||
|
||||
Reference in New Issue
Block a user