-
Notifications
You must be signed in to change notification settings - Fork 407
feat(activity): name bots and the system principal instead of "Automation" #6237
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
ced8ae0
feat(activity): name bots and the system principal instead of "Automa…
cursoragent 6d6be1a
perf(activity): subscribe to the bots list once per context consumer,…
cursoragent 0f1e1d9
fix(activity): read team-bot rows as Bot when the bots list fails to …
cursoragent 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
90 changes: 90 additions & 0 deletions
90
apps/web/src/features/activity/context/activity-context.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,90 @@ | ||
| import { MACRO_SYSTEM_BOT_ID } from '@core/constant/macroSystem'; | ||
| import { createRoot } from 'solid-js'; | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| const useBotsQuery = vi.fn(); | ||
|
|
||
| vi.mock('@queries/bots/bots', () => ({ | ||
| useBotsQuery: () => useBotsQuery(), | ||
| })); | ||
| vi.mock('@core/context/user', () => ({ useUserId: () => () => 'me' })); | ||
| vi.mock('@core/user', () => ({ | ||
| tryMacroId: () => undefined, | ||
| useDisplayName: () => [() => ''], | ||
| })); | ||
| vi.mock('@property/editor/hooks/useAllProperties', () => ({ | ||
| useAllProperties: () => () => [], | ||
| })); | ||
| vi.mock('@property/hooks', () => ({ usePropertyEntityDisplay: () => ({}) })); | ||
| vi.mock('@service-storage/graphql-soup', () => ({ | ||
| getGraphqlSoupClient: () => ({}), | ||
| })); | ||
|
|
||
| const { useActivityContext } = await import('./activity-context'); | ||
|
|
||
| const TEAM_BOT = '11111111-1111-4111-8111-111111111111'; | ||
| const OTHER_BOT = '22222222-2222-4222-8222-222222222222'; | ||
|
|
||
| describe('appActivityContext.botName', () => { | ||
| beforeEach(() => { | ||
| useBotsQuery.mockReset(); | ||
| useBotsQuery.mockReturnValue({ | ||
| isPending: false, | ||
| isSuccess: true, | ||
| data: [ | ||
| { id: TEAM_BOT, name: 'Triage' }, | ||
| { id: OTHER_BOT, name: 'Digest' }, | ||
| ], | ||
| }); | ||
| }); | ||
|
|
||
| it('subscribes to the bots list once per consumer, however many bot rows it names', () => { | ||
| createRoot((dispose) => { | ||
| const context = useActivityContext(); | ||
| const first = context.botName(() => TEAM_BOT); | ||
| const second = context.botName(() => OTHER_BOT); | ||
|
|
||
| expect(first()).toBe('Triage'); | ||
| expect(second()).toBe('Digest'); | ||
| expect(first()).toBe('Triage'); | ||
| expect(useBotsQuery).toHaveBeenCalledTimes(1); | ||
| dispose(); | ||
| }); | ||
| }); | ||
|
|
||
| it('is undefined while the list loads and `Bot` once it has failed', () => { | ||
| useBotsQuery.mockReturnValue({ | ||
| isPending: true, | ||
| isSuccess: false, | ||
| data: undefined, | ||
| }); | ||
| createRoot((dispose) => { | ||
| const context = useActivityContext(); | ||
| expect(context.botName(() => TEAM_BOT)()).toBeUndefined(); | ||
| dispose(); | ||
| }); | ||
|
|
||
| useBotsQuery.mockReturnValue({ | ||
| isPending: false, | ||
| isSuccess: false, | ||
| isError: true, | ||
| data: undefined, | ||
| }); | ||
| createRoot((dispose) => { | ||
| const context = useActivityContext(); | ||
| expect(context.botName(() => TEAM_BOT)()).toBe('Bot'); | ||
| dispose(); | ||
| }); | ||
| }); | ||
|
|
||
| it('never fetches the bots list for first-party bots', () => { | ||
| createRoot((dispose) => { | ||
| const context = useActivityContext(); | ||
| const name = context.botName(() => MACRO_SYSTEM_BOT_ID); | ||
|
|
||
| expect(name()).toBe('System'); | ||
| expect(useBotsQuery).not.toHaveBeenCalled(); | ||
| dispose(); | ||
| }); | ||
| }); | ||
| }); |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { parseActor } from './actor'; | ||
|
|
||
| describe('parseActor', () => { | ||
| it('parses users from macro ids', () => { | ||
| expect(parseActor('macro|sarah@example.com')).toEqual({ | ||
| kind: 'user', | ||
| id: 'macro|sarah@example.com', | ||
| }); | ||
| }); | ||
|
|
||
| it('parses bots from bot principals', () => { | ||
| expect(parseActor('bot|00000000-0000-0000-0000-00000000a1a1')).toEqual({ | ||
| kind: 'bot', | ||
| botId: '00000000-0000-0000-0000-00000000a1a1', | ||
| }); | ||
| }); | ||
|
|
||
| it('keeps anything else raw', () => { | ||
| expect(parseActor('system:nightly')).toEqual({ | ||
| kind: 'unknown', | ||
| raw: 'system:nightly', | ||
| }); | ||
| expect(parseActor('bot|')).toEqual({ kind: 'unknown', raw: 'bot|' }); | ||
| expect(parseActor('macro|no-at-sign')).toEqual({ | ||
| kind: 'unknown', | ||
| raw: 'macro|no-at-sign', | ||
| }); | ||
| }); | ||
| }); |
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,24 @@ | ||
| import { type MacroId, tryMacroId } from '@core/user/macroId'; | ||
|
|
||
| /** | ||
| * Who performed an activity event, parsed once from the wire `actorId`. | ||
| * The backend emits `macro|<email>` for users and `bot|<uuid>` for bots | ||
| * (first-party agents, team bots, and the system principal). Anything else | ||
| * is preserved raw so the row can still say something honest. | ||
| */ | ||
| export type Actor = | ||
| | { kind: 'user'; id: MacroId } | ||
| | { kind: 'bot'; botId: string } | ||
| | { kind: 'unknown'; raw: string }; | ||
|
|
||
| const BOT_PREFIX = 'bot|'; | ||
|
|
||
| export function parseActor(actorId: string): Actor { | ||
| const user = tryMacroId(actorId); | ||
| if (user) return { kind: 'user', id: user }; | ||
| if (actorId.startsWith(BOT_PREFIX)) { | ||
| const botId = actorId.slice(BOT_PREFIX.length); | ||
| if (botId.length > 0) return { kind: 'bot', botId }; | ||
| } | ||
| return { kind: 'unknown', raw: actorId }; | ||
| } |
45 changes: 39 additions & 6 deletions
45
apps/web/src/features/activity/primitives/actor-name.test.ts
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 |
|---|---|---|
| @@ -1,26 +1,59 @@ | ||
| import { MACRO_AGENT_PRINCIPAL_ID } from '@core/constant/macroAgent'; | ||
| import { MACRO_SYSTEM_PRINCIPAL_ID } from '@core/constant/macroSystem'; | ||
| import { createRoot } from 'solid-js'; | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { | ||
| createMockActivityContext, | ||
| MOCK_VIEWER_ID, | ||
| } from '../tests/mock-context'; | ||
| import { createActorName } from './actor-name'; | ||
|
|
||
| function nameOf( | ||
| context: ReturnType<typeof createMockActivityContext>, | ||
| actorId: string | ||
| ): string { | ||
| return createRoot((dispose) => { | ||
| const name = createActorName(context, () => actorId)(); | ||
| dispose(); | ||
| return name; | ||
| }); | ||
| } | ||
|
|
||
| describe('createActorName', () => { | ||
| const context = createMockActivityContext(); | ||
|
|
||
| it('names the viewer "You"', () => { | ||
| expect(createActorName(context, () => MOCK_VIEWER_ID)()).toBe('You'); | ||
| expect(nameOf(context, MOCK_VIEWER_ID)).toBe('You'); | ||
| }); | ||
|
|
||
| it('resolves other users through displayName', () => { | ||
| expect(createActorName(context, () => 'macro|sarah@example.com')()).toBe( | ||
| 'sarah' | ||
| expect(nameOf(context, 'macro|sarah@example.com')).toBe('sarah'); | ||
| }); | ||
|
|
||
| it('names the system principal "System"', () => { | ||
| expect(nameOf(context, MACRO_SYSTEM_PRINCIPAL_ID)).toBe('System'); | ||
| }); | ||
|
|
||
| it('names first-party bots from their constants', () => { | ||
| expect(nameOf(context, MACRO_AGENT_PRINCIPAL_ID)).toBe('Macro'); | ||
| }); | ||
|
|
||
| it('resolves team bots through botName', () => { | ||
| expect(nameOf(context, 'bot|deadbeef-0000-0000-0000-000000000001')).toBe( | ||
| 'Bot deadbeef' | ||
| ); | ||
| }); | ||
|
|
||
| it('labels non-user actors as automation', () => { | ||
| expect(createActorName(context, () => 'system:nightly')()).toBe( | ||
| 'Automation' | ||
| it('reads empty while a team bot name is still loading', () => { | ||
| const loading = createMockActivityContext({ | ||
| botName: () => () => undefined, | ||
| }); | ||
| expect(nameOf(loading, 'bot|deadbeef-0000-0000-0000-000000000001')).toBe( | ||
| '' | ||
| ); | ||
| }); | ||
|
|
||
| it('never says "Automation" for ids it cannot parse', () => { | ||
| expect(nameOf(context, 'system:nightly')).toBe('Unknown'); | ||
| }); | ||
| }); |
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 |
|---|---|---|
| @@ -1,16 +1,33 @@ | ||
| import type { Accessor } from 'solid-js'; | ||
| import { type Accessor, createMemo } from 'solid-js'; | ||
| import { match } from 'ts-pattern'; | ||
| import type { ActivityContext } from '../context/activity-context'; | ||
| import { parseActor } from '../core/actor'; | ||
|
|
||
| /** "You" for the viewer, "Automation" for non-user actors, else the name. */ | ||
| /** | ||
| * The name an activity row shows for its actor. The viewer reads "You", | ||
| * other users read their display name, bots read their bot name (the | ||
| * system principal reads "System"), and ids the app cannot parse read | ||
| * "Unknown". Resolvers are created lazily per actor kind so a user row | ||
| * never subscribes to the bots list. | ||
| */ | ||
| export function createActorName( | ||
| context: Pick<ActivityContext, 'currentUserId' | 'displayName'>, | ||
| context: Pick<ActivityContext, 'currentUserId' | 'displayName' | 'botName'>, | ||
| actorId: Accessor<string> | ||
| ): Accessor<string> { | ||
| const remote = context.displayName(actorId); | ||
| return () => { | ||
| const name = remote(); | ||
| if (name === undefined) return 'Automation'; | ||
| if (actorId() === context.currentUserId()) return 'You'; | ||
| return name; | ||
| }; | ||
| const resolver = createMemo<Accessor<string>>(() => { | ||
| const id = actorId(); | ||
| if (id === context.currentUserId()) return () => 'You'; | ||
| return match(parseActor(id)) | ||
| .with({ kind: 'user' }, (actor) => { | ||
| const remote = context.displayName(() => actor.id); | ||
| return () => remote() ?? 'Unknown'; | ||
| }) | ||
| .with({ kind: 'bot' }, (actor) => { | ||
| const remote = context.botName(() => actor.botId); | ||
| return () => remote() ?? ''; | ||
| }) | ||
| .with({ kind: 'unknown' }, () => () => 'Unknown') | ||
| .exhaustive(); | ||
| }); | ||
| return () => resolver()(); | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| /** | ||
| * Identity for the autonomous Macro platform principal. Mirrors | ||
| * `bot_id::MACRO_SYSTEM_BOT_ID` on the backend, which attributes actions the | ||
| * platform takes on its own (onboarding seeds, scheduled jobs) to this bot. | ||
| */ | ||
| export const MACRO_SYSTEM_BOT_ID = '00000000-0000-0000-0000-000000005759'; | ||
|
|
||
| /** | ||
| * Canonical principal id for the system bot, matching the `bot|<uuid>` form | ||
| * used for bot senders, participants, and activity actors everywhere else. | ||
| */ | ||
| export const MACRO_SYSTEM_PRINCIPAL_ID = `bot|${MACRO_SYSTEM_BOT_ID}`; | ||
|
|
||
| /** Display name for the system principal. */ | ||
| export const MACRO_SYSTEM_NAME = 'System'; | ||
|
|
||
| /** | ||
| * Whether an id refers to the system bot. Accepts both the bare UUID and the | ||
| * `bot|<uuid>` participant/sender form. | ||
| */ | ||
| export function isMacroSystemId(id: string | undefined): boolean { | ||
| if (!id) return false; | ||
| const bare = id.startsWith('bot|') ? id.slice('bot|'.length) : id; | ||
| return bare === MACRO_SYSTEM_BOT_ID; | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.