mirror of
https://github.com/block/buzz.git
synced 2026-11-26 03:01:38 +00:00
Without a Builderlab session, mobile can't tell which agents run on Builderbot, so the Agents page labeled every agent "Manage on desktop", including remote ones. Now, when the community supports remote agents and there's no Builderlab session: - The Agents page shows "Sign in to Builderlab to manage remote agents" with a **Sign in** button (the same reconnect flow as Add). - Agent cards and the navigation subtitle drop the "Manage on desktop" label. - An agent's profile says to sign in to see whether it runs on Builderbot or desktop. ### Before / after iOS simulator, signed in to the remote-agent-pilot community with no Builderlab session. | Before (main) | After (this PR) | | --- | --- | | <img src="https://raw.githubusercontent.com/block/buzz/9be9cebedc8f5ff172e97a3df2cbf505a90e41c4/pr-8907--before-agents.png" width="300"> | <img src="https://raw.githubusercontent.com/block/buzz/9be9cebedc8f5ff172e97a3df2cbf505a90e41c4/pr-8907--after-agents.png" width="300"> | ### Validation - New widget test `agents_signed_out_prompt_test.dart` (runs with `BUZZ_AUTH_DOMAINS`, added to the CI list). - `just mobile-check` and the mobile test suite pass. - Checked the signed-out Agents page in the iOS simulator. buzz-review-completed (manual testing waived by Tom) 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Signed-off-by: Tom Brow <tomb@squareup.com> Co-authored-by: Tom Brow <tomb@squareup.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
415 lines
16 KiB
Dart
415 lines
16 KiB
Dart
import 'dart:async';
|
||
|
||
import 'package:flutter/material.dart';
|
||
import 'package:flutter_hooks/flutter_hooks.dart';
|
||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||
|
||
import '../../shared/agents/owned_agent_directory.dart';
|
||
import '../../shared/widgets/bee_refresh_indicator.dart';
|
||
import '../../shared/agents/remote_agents.dart';
|
||
import '../../shared/community/community_provider.dart';
|
||
import '../../shared/me/default_agent.dart';
|
||
import '../../shared/hosted/hosted_flow.dart';
|
||
import '../../shared/hosted/hosted_flow_provider.dart';
|
||
import '../../shared/identity_names/identity_names_provider.dart';
|
||
import '../../shared/mentions/agent_identity_provider.dart';
|
||
import '../../shared/profile/user_cache_provider.dart';
|
||
import '../../shared/relay/relay.dart';
|
||
import '../../shared/theme/theme.dart';
|
||
import '../../shared/widgets/modal_presentation.dart';
|
||
import '../../shared/widgets/agent_navigation_icon.dart';
|
||
import '../channels/channel.dart';
|
||
import '../channels/channel_detail_page.dart';
|
||
import '../profile/user_profile_sheet.dart';
|
||
import 'create_agent_page.dart';
|
||
import 'agent_card.dart';
|
||
import 'remote_agent_model_control.dart';
|
||
import '../../shared/widgets/frosted_app_bar.dart';
|
||
import '../../shared/widgets/frosted_scaffold.dart';
|
||
import '../../shared/theme/buzz_icons.dart';
|
||
import 'agent_card_grid.dart';
|
||
import 'delete_agent_action.dart';
|
||
import 'agent_identity_sections.dart';
|
||
import 'bestie_agent_action.dart';
|
||
|
||
/// Opens the active community's full-page Agents navigation stack.
|
||
Future<void> showAgents(BuildContext context) async {
|
||
final container = ProviderScope.containerOf(context);
|
||
final controller = container.read(remoteAgentControllerProvider);
|
||
unawaited(controller.run(controller.refresh));
|
||
final channel = await Navigator.of(context).push<Channel>(
|
||
MaterialPageRoute(
|
||
builder: (_) => _AgentsNavigation(controller: controller),
|
||
),
|
||
);
|
||
if (channel != null && context.mounted) {
|
||
await Navigator.of(context).push<void>(
|
||
MaterialPageRoute(builder: (_) => ChannelDetailPage(channel: channel)),
|
||
);
|
||
}
|
||
}
|
||
|
||
class _AgentsNavigation extends HookConsumerWidget {
|
||
const _AgentsNavigation({required this.controller});
|
||
|
||
final RemoteAgentController controller;
|
||
|
||
@override
|
||
Widget build(BuildContext context, WidgetRef ref) {
|
||
final communityId = ref.watch(activeCommunityProvider).asData?.value?.id;
|
||
final heroController = useMemoized(HeroController.new);
|
||
final navigator = useMemoized(() => GlobalKey<NavigatorState>(), [
|
||
communityId,
|
||
]);
|
||
return NavigatorPopHandler<Channel>(
|
||
onPopWithResult: (result) {
|
||
if (navigator.currentState?.canPop() ?? false) {
|
||
navigator.currentState!.pop(result);
|
||
}
|
||
},
|
||
child: Navigator(
|
||
key: navigator,
|
||
observers: [heroController],
|
||
onGenerateRoute: (_) => MaterialPageRoute<void>(
|
||
builder: (listContext) => Material(
|
||
color: context.colors.surface,
|
||
child: AgentsPage(
|
||
controller: controller,
|
||
onClose: () => Navigator.of(context).pop(),
|
||
onSelect: (pubkey) async {
|
||
final channel = await showBuzzModalBottomSheet<Channel>(
|
||
context: listContext,
|
||
isScrollControlled: true,
|
||
showDragHandle: false,
|
||
showCloseButton: false,
|
||
padBottom: false,
|
||
builder: (_) => UserProfileSheet(
|
||
pubkey: pubkey,
|
||
agentPresentation: true,
|
||
agentSubtitleIcon:
|
||
controller.agents.any(
|
||
(agent) => agent['agent_pubkey'] == pubkey,
|
||
)
|
||
? const AgentNavigationIcon(size: 16)
|
||
: null,
|
||
agentSubtitle:
|
||
controller.agents.any(
|
||
(agent) => agent['agent_pubkey'] == pubkey,
|
||
)
|
||
? 'Builderbot agent'
|
||
: agentLocationUnknown(
|
||
ref.read(activeCommunityProvider).asData?.value,
|
||
controller,
|
||
)
|
||
? null
|
||
: 'Manage on desktop',
|
||
primaryActionsSpacing: Grid.md,
|
||
contextualActions: (_) =>
|
||
BestieAgentAction(pubkey: pubkey, compact: true),
|
||
agentIdentity: (_) => AgentIdentitySections(
|
||
pubkey: pubkey,
|
||
controller: controller,
|
||
),
|
||
agentSettings: (_) => DeleteAgentAction(
|
||
asSettingsRow: true,
|
||
controller: controller,
|
||
pubkey: pubkey,
|
||
),
|
||
),
|
||
);
|
||
if (channel != null && context.mounted) {
|
||
Navigator.of(context).pop(channel);
|
||
}
|
||
},
|
||
),
|
||
),
|
||
),
|
||
),
|
||
);
|
||
}
|
||
}
|
||
|
||
/// The current user's verified agents in the active community.
|
||
class AgentsPage extends HookConsumerWidget {
|
||
const AgentsPage({
|
||
required this.controller,
|
||
required this.onClose,
|
||
required this.onSelect,
|
||
super.key,
|
||
});
|
||
final VoidCallback onClose;
|
||
final ValueChanged<String> onSelect;
|
||
final RemoteAgentController controller;
|
||
|
||
@override
|
||
Widget build(BuildContext context, WidgetRef ref) {
|
||
useListenable(controller);
|
||
useListenable(controller.account.subjectChanges);
|
||
final flow = ref.watch(hostedFlowProvider);
|
||
useListenable(flow);
|
||
final signingIn =
|
||
flow.reconnecting &&
|
||
(flow.stage == HostedFlowStage.signIn ||
|
||
flow.stage == HostedFlowStage.working);
|
||
// Account sign-in belongs to the shared hosted flow; reload once it lands.
|
||
useEffect(() {
|
||
if (flow.reconnecting && flow.stage == HostedFlowStage.complete) {
|
||
unawaited(controller.run(controller.refresh));
|
||
}
|
||
return null;
|
||
}, [flow.stage]);
|
||
final community = ref.watch(activeCommunityProvider).asData?.value;
|
||
final bestie = ref.watch(defaultAgentProvider).asData?.value;
|
||
final ownedDirectory = ref.watch(ownedAgentDirectoryProvider);
|
||
// The account's remote agents with a verified owner profile here are
|
||
// listed too, so one stays listed while a deletion that already removed
|
||
// its registration is retried. Every other agent comes from its listed
|
||
// registration.
|
||
final candidates = {
|
||
if (controller.owner == community?.pubkey)
|
||
for (final agent in controller.agents) agent['agent_pubkey'] as String,
|
||
};
|
||
final candidateKey = (candidates.toList()..sort()).join(',');
|
||
final attempt = useState(0);
|
||
final registryFuture = useMemoized(() async {
|
||
final fetch = ref.read(relaySessionProvider.notifier).fetchHistory;
|
||
return ownedCommunityAgents(candidates, community?.pubkey, fetch);
|
||
}, [community?.relayUrl, community?.pubkey, candidateKey, attempt.value]);
|
||
final registered = useFuture(registryFuture, preserveState: false);
|
||
final scope = '${community?.relayUrl}|${community?.pubkey}|$candidateKey';
|
||
final lastOwned = useRef<(String, Set<String>)?>(null);
|
||
if (registered.data case final owned?) lastOwned.value = (scope, owned);
|
||
final owned =
|
||
registered.data ??
|
||
(lastOwned.value?.$1 == scope ? lastOwned.value?.$2 : null);
|
||
final keys = {
|
||
...?owned,
|
||
for (final entry
|
||
in ownedDirectory.asData?.value ?? <AgentDirectoryEntry>[])
|
||
entry.pubkey,
|
||
}.where((key) => !controller.deletedAgents.contains(key)).toList();
|
||
final names = watchIdentityNames(
|
||
ref,
|
||
keys,
|
||
agentPubkeys: keys.toSet(),
|
||
fallbackNames: {
|
||
for (final entry
|
||
in ownedDirectory.asData?.value ?? <AgentDirectoryEntry>[])
|
||
if (entry.displayName != null) entry.pubkey: entry.displayName!,
|
||
for (final agent in controller.agents)
|
||
agent['agent_pubkey'] as String: agent['agent_name'] as String,
|
||
},
|
||
);
|
||
// Buzz App orders names without case and uses the identity as a stable tie.
|
||
keys.sort((a, b) {
|
||
final byName = names
|
||
.labelFor(a)
|
||
.toLowerCase()
|
||
.compareTo(names.labelFor(b).toLowerCase());
|
||
return byName != 0 ? byName : a.compareTo(b);
|
||
});
|
||
final profiles = ref.watch(userCacheProvider);
|
||
final creating = useState(false);
|
||
final needsSignIn = agentLocationUnknown(community, controller);
|
||
final signInNeeded = builderlabSignInNeeded(community, controller);
|
||
final failed =
|
||
registered.hasError ||
|
||
ownedDirectory.hasError ||
|
||
// Remote listing failures matter only where remote agents are shown.
|
||
(supportsRemoteAgents(community) && controller.listingFailed);
|
||
final loading =
|
||
!creating.value &&
|
||
(controller.busy ||
|
||
ownedDirectory.isLoading ||
|
||
registered.connectionState != ConnectionState.done);
|
||
|
||
Future<void> refresh() async {
|
||
await controller.run(controller.refresh);
|
||
if (context.mounted) {
|
||
attempt.value++;
|
||
ref.invalidate(ownedAgentDirectoryProvider);
|
||
}
|
||
}
|
||
|
||
final VoidCallback? addAgent =
|
||
community == null || controller.busy || creating.value || signingIn
|
||
? null
|
||
: () async {
|
||
if (controller.account.account == null) {
|
||
await flow.reconnectAccount();
|
||
return;
|
||
}
|
||
creating.value = true;
|
||
try {
|
||
await Navigator.of(context).push<bool>(
|
||
MaterialPageRoute(
|
||
builder: (_) => CreateAgentPage(
|
||
controller: controller,
|
||
community: community,
|
||
onCreated: () {
|
||
ref.invalidate(agentDirectoryProvider);
|
||
ref.invalidate(ownedAgentDirectoryProvider);
|
||
},
|
||
),
|
||
),
|
||
);
|
||
} finally {
|
||
if (context.mounted) creating.value = false;
|
||
}
|
||
};
|
||
|
||
final headerStyle = context.textTheme.titleMedium?.copyWith(
|
||
fontSize: 22,
|
||
fontWeight: FontWeight.w600,
|
||
color: navigationPrimaryForeground(context),
|
||
);
|
||
final topHeight = frostedAppBarHeight(
|
||
context,
|
||
nativeLargeTitle: true,
|
||
titleStyle: headerStyle,
|
||
bottomHeight: Grid.xxs,
|
||
);
|
||
return FrostedScaffold(
|
||
backgroundColor: context.colors.surface,
|
||
appBar: FrostedAppBar(
|
||
nativeActions: [
|
||
if (supportsRemoteAgents(community))
|
||
IosNavigationAction(
|
||
label: 'Add agent',
|
||
symbol: 'plus',
|
||
onPressed: addAgent,
|
||
),
|
||
],
|
||
nativeTitle: 'Agents',
|
||
nativeLargeTitle: true,
|
||
automaticallyImplyLeading: false,
|
||
nativeLeading: IosNavigationAction(
|
||
label: 'Close agents',
|
||
symbol: 'xmark',
|
||
onPressed: onClose,
|
||
),
|
||
leading: IconButton(
|
||
tooltip: 'Close agents',
|
||
icon: const Icon(BuzzIcons.x),
|
||
onPressed: onClose,
|
||
),
|
||
horizontalInset: Grid.gutter,
|
||
|
||
title: Text('Agents', style: headerStyle),
|
||
titleStyle: headerStyle,
|
||
bottomHeight: Grid.xxs,
|
||
bottom: const SizedBox.expand(),
|
||
actions: [
|
||
if (supportsRemoteAgents(community))
|
||
IconButton(
|
||
tooltip: 'Add agent',
|
||
onPressed: addAgent,
|
||
icon: const Icon(Icons.add),
|
||
),
|
||
],
|
||
),
|
||
body: SafeArea(
|
||
top: false,
|
||
child: Material(
|
||
color: Colors.transparent,
|
||
child: BeeRefreshIndicator(
|
||
edgeOffset: topHeight,
|
||
onRefresh: refresh,
|
||
child: CustomScrollView(
|
||
physics: const AlwaysScrollableScrollPhysics(),
|
||
slivers: [
|
||
SliverToBoxAdapter(child: SizedBox(height: topHeight)),
|
||
SliverToBoxAdapter(
|
||
child: Column(
|
||
children: [
|
||
if (failed && keys.isEmpty)
|
||
ListTile(
|
||
title: const Text('Could not load agents'),
|
||
trailing: TextButton(
|
||
onPressed: refresh,
|
||
child: const Text('Retry'),
|
||
),
|
||
),
|
||
if (signInNeeded)
|
||
ListTile(
|
||
title: const Text(
|
||
'Sign in to Builderlab to manage remote agents',
|
||
),
|
||
trailing: TextButton(
|
||
onPressed: signingIn
|
||
? null
|
||
: () => flow.reconnectAccount(),
|
||
child: const Text('Sign in'),
|
||
),
|
||
),
|
||
if (!loading && !failed && !needsSignIn && keys.isEmpty)
|
||
const Padding(
|
||
padding: EdgeInsets.all(Grid.sm),
|
||
child: Text(
|
||
'You haven’t added any agents yet.',
|
||
textAlign: TextAlign.center,
|
||
),
|
||
),
|
||
],
|
||
),
|
||
),
|
||
AgentCardGrid(
|
||
itemCount: keys.length,
|
||
itemBuilder: (context, index) {
|
||
final key = keys[index];
|
||
final remote = controller.agents
|
||
.where((row) => row['agent_pubkey'] == key)
|
||
.firstOrNull;
|
||
final editable =
|
||
community != null &&
|
||
supportsRemoteAgents(community) &&
|
||
controller.owner == community.pubkey &&
|
||
owned?.contains(key) == true &&
|
||
controller.activeAgents.contains(key) &&
|
||
remote != null;
|
||
return AgentCard(
|
||
key: ValueKey(key),
|
||
name: names.labelFor(key),
|
||
isBestie: key == bestie,
|
||
imageUrl:
|
||
profiles[key]?.avatarUrl ??
|
||
ownedDirectory.asData?.value
|
||
.where((entry) => entry.pubkey == key)
|
||
.firstOrNull
|
||
?.avatarUrl,
|
||
location: remote != null ? 'Remote' : null,
|
||
onOpen: () => onSelect(key),
|
||
footer: editable
|
||
? RemoteAgentModelControl(
|
||
key: ValueKey('model-$key'),
|
||
controller: controller,
|
||
agent: remote,
|
||
community: community,
|
||
isCurrent: () =>
|
||
ref
|
||
.read(activeCommunityProvider)
|
||
.asData
|
||
?.value
|
||
?.id ==
|
||
community.id,
|
||
)
|
||
: null,
|
||
subtitle: editable
|
||
? null
|
||
: remote != null
|
||
? 'Builderbot agent'
|
||
: needsSignIn
|
||
? null
|
||
: 'Manage on desktop',
|
||
);
|
||
},
|
||
),
|
||
],
|
||
),
|
||
),
|
||
),
|
||
),
|
||
);
|
||
}
|
||
}
|