Repository navigation
feat(api): filter listed agents by activity state #748
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mvanhorn
wants to merge
4
commits into
omnara-ai:main
Choose a base branch
from
mvanhorn:feat/agent-activity-filter
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,826
−1,190
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
2cbf396
feat(api): filter listed agents by activity state
mvanhorn 9b248ae
Merge upstream main into feat/agent-activity-filter
mvanhorn ac7aa7b
fix(web): show no results when a status filter matches nothing
mvanhorn 97ac2e0
Merge upstream main into feat/agent-activity-filter
mvanhorn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
frontend/apps/web/src/components/agents/AgentsSection.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| /** @vitest-environment happy-dom */ | ||
|
|
||
| import { OmnaraClientProvider } from '@omnara/react' | ||
| import { createOmnaraClient } from '@omnara/sdk' | ||
| import { QueryClient, QueryClientProvider } from '@tanstack/react-query' | ||
| import { act } from 'react' | ||
| import { createRoot, type Root } from 'react-dom/client' | ||
| import { afterAll, afterEach, beforeAll, beforeEach, expect, it, vi } from 'vitest' | ||
|
|
||
| import { AgentsSection } from '@/components/agents/AgentsSection' | ||
| import { fakeApi, jsonResponse } from '@/test/fake-api' | ||
| import { fakeId } from '@/test/fixtures' | ||
| import { enableReactActEnvironment } from '@/test/react-act' | ||
|
|
||
| const orgId = fakeId('org') | ||
| const projectId = fakeId('proj') | ||
|
|
||
| let container: HTMLDivElement | ||
| let root: Root | ||
| let restoreActEnvironment: () => void | ||
|
|
||
| beforeAll(() => { | ||
| restoreActEnvironment = enableReactActEnvironment() | ||
| }) | ||
|
|
||
| afterAll(() => { | ||
| restoreActEnvironment() | ||
| }) | ||
|
|
||
| beforeEach(() => { | ||
| container = document.createElement('div') | ||
| document.body.append(container) | ||
| root = createRoot(container) | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| act(() => { | ||
| root.unmount() | ||
| }) | ||
| container.remove() | ||
| }) | ||
|
|
||
| function menuItem(role: string, name: string) { | ||
| const item = Array.from(document.querySelectorAll<HTMLElement>(`[role="${role}"]`)).find( | ||
| (candidate) => candidate.textContent.includes(name), | ||
| ) | ||
| if (!item) throw new Error(`No ${role} named ${name}`) | ||
| return item | ||
| } | ||
|
|
||
| it('shows no results, not the empty copy, when a status filter matches nothing', async () => { | ||
| const api = fakeApi([ | ||
| { | ||
| method: 'GET', | ||
| path: `/api/v1/orgs/${orgId}/projects/${projectId}/agents`, | ||
| respond: () => jsonResponse({ data: [], next_cursor: null }), | ||
| }, | ||
| ]) | ||
| const client = createOmnaraClient({ baseUrl: 'https://omnara.test/api/v1' }) | ||
| client.setConfig({ fetch: api.fetch }) | ||
| const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } }) | ||
|
|
||
| act(() => { | ||
| root.render( | ||
| <OmnaraClientProvider client={client}> | ||
| <QueryClientProvider client={queryClient}> | ||
| <AgentsSection | ||
| orgId={orgId} | ||
| projectId={projectId} | ||
| canManage | ||
| emptyMessage="No agents yet." | ||
| emptyAction={<a href="/launch">Launch an agent</a>} | ||
| /> | ||
| </QueryClientProvider> | ||
| </OmnaraClientProvider>, | ||
| ) | ||
| }) | ||
| await vi.waitFor(() => { | ||
| expect(container.textContent).toContain('No agents yet.') | ||
| }) | ||
| expect(container.textContent).toContain('Launch an agent') | ||
|
|
||
| const filters = Array.from(container.querySelectorAll('button')).find((button) => | ||
| button.textContent.includes('Filters'), | ||
| ) | ||
| if (!filters) throw new Error('No filters button') | ||
| act(() => { | ||
| filters.focus() | ||
| filters.dispatchEvent(new KeyboardEvent('keydown', { key: 'Enter', bubbles: true })) | ||
| }) | ||
| act(() => { | ||
| menuItem('menuitem', 'Status').click() | ||
| }) | ||
| act(() => { | ||
| menuItem('menuitemradio', 'Idle').click() | ||
| }) | ||
|
|
||
| await vi.waitFor(() => { | ||
| expect(api.requests.at(-1)?.url.searchParams.get('activity_state')).toBe('idle') | ||
| }) | ||
| expect(container.textContent).toContain('No results.') | ||
| expect(container.textContent).not.toContain('No agents yet.') | ||
| expect(container.textContent).not.toContain('Launch an agent') | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,26 +36,35 @@ import { type PaginationControls, usePagedQuery } from '@/hooks/use-paged-query' | |
| import { useProjectDirectory } from '@/hooks/use-project-directory' | ||
| import { resourceSortOptions, useResourceList } from '@/hooks/use-resource-list' | ||
| import { agentIcon, profileIcon } from '@/lib/agent-icon' | ||
| import { agentStatusLabel } from '@/lib/agent-status' | ||
| import { | ||
| activityFilterOptions, | ||
| type ActivityFilterValue, | ||
| agentStatusLabel, | ||
| } from '@/lib/agent-status' | ||
|
|
||
| type AgentListControls = ReturnType<typeof useAgentListControls> | ||
|
|
||
| function useAgentListControls() { | ||
| const list = useResourceList<AgentListSort>('-updated_at') | ||
| const [includeSubagents, setIncludeSubagents] = useState(false) | ||
| const [includeArchived, setIncludeArchived] = useState(false) | ||
| const [activity, setActivity] = useState<ActivityFilterValue>('all') | ||
| // Each card's cost, with its subagents, comes with the list rather than a request per card. | ||
| const filters: AgentListFilters = { ...list.apiFilters, include_usage: true } | ||
| if (includeSubagents) filters.include_subagents = true | ||
| if (includeArchived) filters.include_archived = true | ||
| if (activity !== 'all') filters.activity_state = activity | ||
| return { | ||
| list, | ||
| filters, | ||
| isFiltering: list.isFiltering || activity !== 'all', | ||
| includeSubagents, | ||
| setIncludeSubagents, | ||
| includeArchived, | ||
| setIncludeArchived, | ||
| resetKey: `${list.queryKey}:${includeSubagents ? 'all' : 'top'}:${includeArchived ? 'archived' : 'active'}`, | ||
| activity, | ||
| setActivity, | ||
| resetKey: `${list.queryKey}:${includeSubagents ? 'all' : 'top'}:${includeArchived ? 'archived' : 'active'}:${activity}`, | ||
|
cursor[bot] marked this conversation as resolved.
Resolved
|
||
| } | ||
| } | ||
|
|
||
|
|
@@ -172,6 +181,14 @@ function AgentList({ | |
| showSearch | ||
| sort={{ value: list.sort, options: resourceSortOptions, onChange: list.setSort }} | ||
| filters={{ | ||
| activity: { | ||
| value: controls.activity, | ||
| options: activityFilterOptions, | ||
| onChange: (value) => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: |
||
| const option = activityFilterOptions.find((candidate) => candidate.value === value) | ||
| if (option) controls.setActivity(option.value) | ||
| }, | ||
| }, | ||
| subagents: { | ||
| checked: controls.includeSubagents, | ||
| onChange: controls.setIncludeSubagents, | ||
|
|
@@ -192,7 +209,7 @@ function AgentList({ | |
| canManage={canManage(agent)} | ||
| /> | ||
| )} | ||
| isFiltered={list.isFiltering} | ||
| isFiltered={controls.isFiltering} | ||
| pagination={pagination} | ||
| isPending={isPending} | ||
| isError={isError} | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
with a status filter selected and no active agent in the results, the list stops polling:
useAgents/useOrgAgentsonly poll while a loaded row is active. e.g. withWaitingselected while agents run, the list is empty, so an agent that later asks a question doesn't show up until something else refetches (a reload, or refocusing the tab after 30s). could the hooks keep polling while a non-archivedactivity_statefilter is set?