Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

feat(tasks): move through artifacts with the keyboard (#110173)

Shy Alter64368ae5d9d8
diff --git a/products/posthog_ai/frontend/scenes/TaskTracker/components/TaskRunArtifacts.tsx b/products/posthog_ai/frontend/scenes/TaskTracker/components/TaskRunArtifacts.tsx
index 3fddac40d616..d91f678c015f 100644
--- a/products/posthog_ai/frontend/scenes/TaskTracker/components/TaskRunArtifacts.tsx
+++ b/products/posthog_ai/frontend/scenes/TaskTracker/components/TaskRunArtifacts.tsx
@@ -1,5 +1,5 @@
import { useActions, useValues } from 'kea'
-import { Suspense, lazy, useEffect, useMemo, useState } from 'react'
+import { KeyboardEvent, Suspense, lazy, useEffect, useMemo, useState } from 'react'
import {
IconBolt,
@@ -95,6 +95,7 @@ import {
artifactPreviewKind,
formatArtifactSize,
isTextPreview,
+ listboxKeyTarget,
parseCsv,
LIVE_OBJECT_KINDS,
LIVING_ADAPTER_LABEL,
@@ -504,20 +505,38 @@ function ArtifactNav({ taskId }: { taskId: string }): JSX.Element {
// Cited PostHog objects sit under their own label, after the files.
const objects = files.filter((file) => !!postHogObjectRef(file.latest))
const livingDocuments = files.filter((file) => !!file.latest.living)
+ const onKeyDown = (event: KeyboardEvent<HTMLDivElement>): void => {
+ const target = event.target as HTMLElement
+ if (event.altKey || event.ctrlKey || event.metaKey || target.getAttribute('role') !== 'option') {
+ return
+ }
+ // The rendered rows give the order, so the keys follow the list as it reads, across its groups.
+ const options = Array.from(event.currentTarget.querySelectorAll<HTMLElement>('[role="option"]'))
+ const next = options[listboxKeyTarget(event.key, options.indexOf(target), options.length) ?? -1]
+ if (!next?.dataset.fileKey) {
+ return
+ }
+ event.preventDefault()
+ selectArtifact(next.dataset.fileKey, 'keyboard')
+ next.focus()
+ }
const renderRow = (file: ArtifactFile): JSX.Element => {
const selected = file.key === selectedFile?.key
return (
<Item
key={file.key}
size="xs"
- role="option"
aria-selected={selected}
+ // Roving tabindex: Tab enters the list on the open file, and the arrow keys move from there.
+ tabIndex={selected ? 0 : -1}
+ data-file-key={file.key}
className={cn(
'w-full cursor-pointer rounded-md border-transparent text-left hover:bg-fill-hover',
selected && 'bg-fill-selected hover:bg-fill-selected'
)}
+ // Item drops a `role` prop, so the option role goes on the rendered element.
// eslint-disable-next-line react/forbid-elements
- render={<button type="button" />}
+ render={<button type="button" role="option" />}
onClick={() => selectArtifact(file.key)}
data-attr="task-artifact-nav-item"
>
@@ -544,6 +563,7 @@ function ArtifactNav({ taskId }: { taskId: string }): JSX.Element {
<div
role="listbox"
aria-label="Files"
+ onKeyDown={onKeyDown}
className="flex min-h-0 flex-1 flex-col gap-px overflow-y-auto p-1.5"
>
{files.filter((file) => !postHogObjectRef(file.latest) && !file.latest.living).map(renderRow)}
@@ -831,7 +851,8 @@ function ArtifactToolbar({
<IconExternal className="size-4" />
</IconAction>
)}
- {(kind !== 'reference' || (objectRef && LIVE_OBJECT_KINDS.has(objectRef.objectKind))) && (
+ {/* Full page always keeps its exit, because the stepper can land on an object with no embed. */}
+ {(expanded || kind !== 'reference' || (objectRef && LIVE_OBJECT_KINDS.has(objectRef.objectKind))) && (
<IconAction
label={expanded ? 'Exit full page' : 'Open full page'}
onClick={() => onExpandedChange(!expanded)}
diff --git a/products/posthog_ai/frontend/scenes/TaskTracker/taskRunArtifacts.test.ts b/products/posthog_ai/frontend/scenes/TaskTracker/taskRunArtifacts.test.ts
index 53dab58f9b60..fee88af64d0a 100644
--- a/products/posthog_ai/frontend/scenes/TaskTracker/taskRunArtifacts.test.ts
+++ b/products/posthog_ai/frontend/scenes/TaskTracker/taskRunArtifacts.test.ts
@@ -10,6 +10,7 @@ import {
groupArtifactVersions,
livingArtifactFiles,
livingArtifactsFromResponse,
+ listboxKeyTarget,
parseCsv,
visibleRunArtifacts,
} from './taskRunArtifacts'
@@ -86,6 +87,19 @@ describe('taskRunArtifacts', () => {
expect(artifactPreviewKind(artifact(overrides))).toBe(expected)
})
+ test.each([
+ ['ArrowDown', 1, 4, 2],
+ ['ArrowDown', 3, 4, 3],
+ ['ArrowUp', 2, 4, 1],
+ ['ArrowUp', 0, 4, 0],
+ ['Home', 2, 4, 0],
+ ['End', 0, 4, 3],
+ ['Enter', 1, 4, null],
+ ['ArrowDown', 0, 0, null],
+ ])('listboxKeyTarget moves %s from %i of %i to %p', (key, current, count, expected) => {
+ expect(listboxKeyTarget(key, current, count)).toBe(expected)
+ })
+
it('visibleRunArtifacts keeps files the agent wrote and cited PostHog objects', () => {
const kept = artifact({ id: 'kept' })
const insight = artifact({
diff --git a/products/posthog_ai/frontend/scenes/TaskTracker/taskRunArtifacts.ts b/products/posthog_ai/frontend/scenes/TaskTracker/taskRunArtifacts.ts
index 7fd59d19dd49..bd48e1e4161b 100644
--- a/products/posthog_ai/frontend/scenes/TaskTracker/taskRunArtifacts.ts
+++ b/products/posthog_ai/frontend/scenes/TaskTracker/taskRunArtifacts.ts
@@ -149,6 +149,28 @@ export function parseCsv(text: string): string[][] {
return rows.filter((cells) => cells.some((cell) => cell !== ''))
}
+/**
+ * The option a listbox key moves to, or null for a key the list does not handle.
+ * The arrows stop at the ends and do not wrap, as in the WAI-ARIA listbox pattern.
+ */
+export function listboxKeyTarget(key: string, current: number, count: number): number | null {
+ if (count === 0) {
+ return null
+ }
+ switch (key) {
+ case 'ArrowUp':
+ return Math.max(0, current - 1)
+ case 'ArrowDown':
+ return Math.min(count - 1, current + 1)
+ case 'Home':
+ return 0
+ case 'End':
+ return count - 1
+ default:
+ return null
+ }
+}
+
/** A file the agent wrote, with the run that holds it. Downloads must name that run, not the open one. */
export interface RunArtifact extends TaskRunArtifactResponseApi {
runId: string
diff --git a/products/posthog_ai/frontend/scenes/TaskTracker/taskRunArtifactsLogic.ts b/products/posthog_ai/frontend/scenes/TaskTracker/taskRunArtifactsLogic.ts
index 1041c41fd2ad..691a2f64a52d 100644
--- a/products/posthog_ai/frontend/scenes/TaskTracker/taskRunArtifactsLogic.ts
+++ b/products/posthog_ai/frontend/scenes/TaskTracker/taskRunArtifactsLogic.ts
@@ -50,6 +50,8 @@ export interface TaskRunArtifactsLogicProps {
taskId: string
}
+export type ArtifactSelectSource = 'click' | 'keyboard'
+
export interface ArtifactText {
artifactId: string
text: string | null
@@ -183,8 +185,12 @@ export interface taskRunArtifactsLogicActions {
reportObjectOpened: (objectKind: string) => {
objectKind: string
}
- selectArtifact: (fileKey: string) => {
+ selectArtifact: (
+ fileKey: string,
+ source?: ArtifactSelectSource
+ ) => {
fileKey: string
+ source: ArtifactSelectSource
}
selectVersion: (artifactId: string | null) => {
artifactId: string | null
@@ -272,7 +278,7 @@ export const taskRunArtifactsLogic = kea<taskRunArtifactsLogicType>([
})),
actions({
setActiveTab: (tab: TaskRunTab) => ({ tab }),
- selectArtifact: (fileKey: string) => ({ fileKey }),
+ selectArtifact: (fileKey: string, source: ArtifactSelectSource = 'click') => ({ fileKey, source }),
// `null` follows the latest version, so a new upload of the open file shows at once.
selectVersion: (artifactId: string | null) => ({ artifactId }),
stepArtifact: (delta: number) => ({ delta }),
@@ -526,7 +532,7 @@ export const taskRunArtifactsLogic = kea<taskRunArtifactsLogicType>([
actions.loadArtifactMedia(artifact)
}
}
- const previewSelected = (): void => {
+ const previewSelected = (source?: ArtifactSelectSource): void => {
const artifact = values.selectedArtifact
if (values.activeTab !== 'artifacts' || !artifact || !values.selectedKind) {
return
@@ -537,6 +543,8 @@ export const taskRunArtifactsLogic = kea<taskRunArtifactsLogicType>([
content_type: artifact.content_type ?? null,
version_count: values.selectedFile?.versions.length ?? 1,
living_adapter: artifact.living?.adapter ?? null,
+ // Only a pick in the file list or the stepper has a source. Opening the tab or a link has none.
+ ...(source ? { source } : {}),
})
}
return {
@@ -548,7 +556,7 @@ export const taskRunArtifactsLogic = kea<taskRunArtifactsLogicType>([
}
previewSelected()
},
- selectArtifact: previewSelected,
+ selectArtifact: ({ source }) => previewSelected(source),
selectVersion: () => {
// pinned: analytics event name and properties. Renaming them breaks insights.
posthog.capture('task artifact version selected', {