mirror of
https://github.com/block/buzz.git
synced 2026-11-16 06:11:34 +00:00
Adds Builderlab remote agents to mobile: a member signed in to Builderlab can list, create and delete their own hosted agents from the channels header, and active remote agents show as Online. Everything is gated by `HostedEnvironment.enabled` (the build-time `BUZZ_AUTH_DOMAINS` define). App Store and internal release builds do not set it, so they render and fetch nothing new. ### Behavior - **Agents** button in the channels header (native iOS bar and Material header) for hosted communities only. It opens a sheet listing the community's agents; tapping one pushes its profile inside the same sheet. - **Create**: for the member's own agents (the NIP-OA owner is the member, not the community). Registers the agent with Beekeeper, attests it to the community, saves optional private instructions, subscribes it to the relay, then publishes the kind 30177 registration with a NIP-OA owner proof. A failed step resumes with the same agent identity on retry; instructions stay in secure storage, never in preferences. - **Delete**: removes the relay registration, confirms it is gone, then deletes the Beekeeper agent. A partial failure keeps a retry row in the list. - **Presence**: an ACTIVE agent with a live kind 30177 registration on the active relay shows Online in profiles, channel rows, DMs and mentions, since remote agents wake on messages. Local presence data is unchanged. - Sign-in reuses the shared hosted flow. The account's saved Buzz key must match the server binding; nothing is adopted or rewritten. - `BuilderlabAccountClient` gains an allowlist for the six Beekeeper routes desktop already uses. Not included from the prototype: the separate entrypoint and second sign-in, test-community creation, staging-only hosts, QR-code agent links, and the USB proxy. ### Validation - Added tests: build-time gate, account/key binding before listing, setup resume with private instructions, deletion contract and retry, owner-proof verification, status parsing, presented presence requiring a live registration in the active community and following registration publish and removal (mutation-checked), account-change clearing, load retry, recovery of a pending setup whose instructions were lost, a lost registration response that can only resume with the same idempotency key (from any community), an HTTP-rejected registration that can be discarded, deletion of other agents while a setup is unfinished, rejection of owner proofs whose signed conditions or profile signature fail, registration publish and removal over a loopback WebSocket relay for an `http://`-saved community, and widget tests for in-sheet detail navigation, creation overlay with failure then resume, and delete confirmation. - The new tests also run in the CI lane with `BUZZ_AUTH_DOMAINS` configured, so the enabled path is covered. - Full mobile format, analyze and test suite pass locally. - Independent agent review: no gate leak or key-handling issue found; its robustness findings (discarding a setup that never got a server identity, clearing registry state on account change, retrying a skipped load, guarded notifications) are fixed; discard and account change have added tests. - Simulator: a `BUZZ_AUTH_DOMAINS` debug build launches to Builderlab sign-in. The signed-in flow was not re-run on this branch. - Codex Security Review: an earlier head received a published review, and its two findings were fixed. On745ad85andbe2e31bthe review hit its 45-minute timeout and was cancelled without a result, so the final head has no published security review. - Human app test: not applicable under the mobile testing exception. The end-to-end create, mention and delete flow was demonstrated on the prototype branch against Builderlab; this PR keeps the same Beekeeper and relay contracts. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Signed-off-by: Tom Brow <tomb@block.xyz> Co-authored-by: george <973be878edd6d4f79692a3827f20a6f7fe0ed0362f269ff6b6ba8867033d7d8e@sprout-oss.stage.blox.sqprod.co> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
277 lines
10 KiB
Dart
277 lines
10 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/remote_agents.dart';
|
|
import '../../shared/community/community_provider.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/avatar_image.dart';
|
|
import '../../shared/widgets/buzz_titled_sheet_layout.dart';
|
|
import '../../shared/widgets/modal_presentation.dart';
|
|
import '../channels/channel.dart';
|
|
import '../channels/channel_detail_page.dart';
|
|
import '../profile/user_profile_sheet.dart';
|
|
import 'create_agent_sheet.dart';
|
|
import 'delete_agent_action.dart';
|
|
|
|
/// Opens the active community's Agents navigation stack inside one modal.
|
|
Future<void> showAgents(BuildContext context) async {
|
|
final container = ProviderScope.containerOf(context);
|
|
final controller = container.read(remoteAgentControllerProvider);
|
|
unawaited(controller.run(controller.refresh));
|
|
final channel = await showBuzzModalBottomSheet<Channel>(
|
|
context: context,
|
|
isScrollControlled: true,
|
|
showCloseButton: false,
|
|
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 navigator = useMemoized(() => GlobalKey<NavigatorState>(), [
|
|
communityId,
|
|
]);
|
|
return SizedBox(
|
|
height: MediaQuery.sizeOf(context).height * .72,
|
|
child: NavigatorPopHandler<Channel>(
|
|
onPopWithResult: (result) => navigator.currentState!.pop(result),
|
|
child: Navigator(
|
|
key: navigator,
|
|
onGenerateRoute: (_) => MaterialPageRoute<void>(
|
|
builder: (listContext) => Material(
|
|
color: context.colors.surface,
|
|
child: AgentsSheet(
|
|
controller: controller,
|
|
onClose: () => Navigator.of(context).pop(),
|
|
onSelect: (pubkey) async {
|
|
final channel = await Navigator.of(listContext).push<Channel>(
|
|
MaterialPageRoute(
|
|
builder: (detailContext) => Material(
|
|
key: const ValueKey('agent-detail-surface'),
|
|
color: context.colors.surface,
|
|
child: SizedBox.expand(
|
|
child: BuzzTitledSheetLayout(
|
|
title: '',
|
|
leading: IconButton(
|
|
tooltip: 'Back',
|
|
icon: const Icon(Icons.arrow_back_ios_new),
|
|
onPressed: () =>
|
|
Navigator.of(detailContext).pop(),
|
|
),
|
|
trailing: const SizedBox.shrink(),
|
|
child: UserProfileSheet(
|
|
pubkey: pubkey,
|
|
primaryActionsSpacing: Grid.md,
|
|
contextualActionsSpacing: Grid.xxs,
|
|
contextualActions: (_) => DeleteAgentAction(
|
|
controller: controller,
|
|
pubkey: pubkey,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
if (channel != null && context.mounted) {
|
|
Navigator.of(context).pop(channel);
|
|
}
|
|
},
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
/// The active community's agents, with remote-agent creation for its owner.
|
|
class AgentsSheet extends HookConsumerWidget {
|
|
const AgentsSheet({
|
|
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);
|
|
final flow = ref.watch(hostedFlowProvider);
|
|
useListenable(flow);
|
|
final signingIn = flow.reconnecting && flow.stage != HostedFlowStage.idle;
|
|
// 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 directory = ref.watch(agentDirectoryProvider);
|
|
final registryFuture = useMemoized(() async {
|
|
final fetch = ref.read(relaySessionProvider.notifier).fetchHistory;
|
|
return ownedCommunityAgents(
|
|
{
|
|
for (final agent in controller.agents)
|
|
agent['agent_pubkey'] as String,
|
|
},
|
|
controller.owner,
|
|
fetch,
|
|
);
|
|
}, [community?.relayUrl, controller.agents, controller.owner]);
|
|
final registered = useFuture(registryFuture, preserveState: false);
|
|
final keys = {
|
|
...?registered.data,
|
|
for (final entry in directory.asData?.value ?? <AgentDirectoryEntry>[])
|
|
entry.pubkey,
|
|
}.where((key) => !controller.deletedAgents.contains(key)).toList();
|
|
final names = watchIdentityNames(
|
|
ref,
|
|
keys,
|
|
agentPubkeys: keys.toSet(),
|
|
fallbackNames: {
|
|
for (final agent in controller.agents)
|
|
agent['agent_pubkey'] as String: agent['agent_name'] as String,
|
|
},
|
|
);
|
|
keys.sort((a, b) => names.labelFor(a).compareTo(names.labelFor(b)));
|
|
final profiles = ref.watch(userCacheProvider);
|
|
final creating = useState(false);
|
|
final failed = directory.hasError || registered.hasError;
|
|
final loading =
|
|
!creating.value &&
|
|
(controller.busy ||
|
|
directory.isLoading ||
|
|
registered.connectionState != ConnectionState.done);
|
|
|
|
Future<void> refresh() async {
|
|
await controller.run(controller.refresh);
|
|
if (context.mounted) ref.invalidate(agentDirectoryProvider);
|
|
}
|
|
|
|
return SizedBox(
|
|
height: MediaQuery.sizeOf(context).height * .72,
|
|
child: BuzzTitledSheetLayout(
|
|
title: 'Agents',
|
|
leading: IconButton(
|
|
tooltip: 'Close',
|
|
icon: const Icon(Icons.close),
|
|
onPressed: onClose,
|
|
),
|
|
trailing: IconButton(
|
|
tooltip: 'Add agent',
|
|
onPressed: community == null || controller.busy || creating.value
|
|
? null
|
|
: () async {
|
|
creating.value = true;
|
|
try {
|
|
await showBuzzModalBottomSheet<bool>(
|
|
context: context,
|
|
useRootNavigator: true,
|
|
isScrollControlled: true,
|
|
showCloseButton: false,
|
|
builder: (_) => CreateAgentSheet(
|
|
controller: controller,
|
|
community: community,
|
|
onCreated: () => ref.invalidate(agentDirectoryProvider),
|
|
),
|
|
);
|
|
} finally {
|
|
if (context.mounted) creating.value = false;
|
|
}
|
|
},
|
|
icon: const Icon(Icons.add),
|
|
),
|
|
child: Material(
|
|
color: Colors.transparent,
|
|
child: RefreshIndicator(
|
|
onRefresh: refresh,
|
|
child: ListView(
|
|
physics: const AlwaysScrollableScrollPhysics(),
|
|
padding: const EdgeInsets.fromLTRB(Grid.xs, 0, Grid.xs, Grid.sm),
|
|
children: [
|
|
if (loading) const LinearProgressIndicator(),
|
|
if (controller.account.account == null)
|
|
ListTile(
|
|
title: const Text('Sign in to create an agent'),
|
|
trailing: TextButton(
|
|
onPressed: controller.busy || signingIn
|
|
? null
|
|
: () => unawaited(flow.reconnectAccount()),
|
|
child: const Text('Sign in'),
|
|
),
|
|
),
|
|
if (!controller.busy &&
|
|
controller.account.account != null &&
|
|
controller.status.isNotEmpty &&
|
|
!creating.value)
|
|
ListTile(subtitle: Text(controller.status)),
|
|
if (failed)
|
|
ListTile(
|
|
title: const Text('Could not load agents'),
|
|
trailing: TextButton(
|
|
onPressed: refresh,
|
|
child: const Text('Retry'),
|
|
),
|
|
),
|
|
if (!loading && !failed && keys.isEmpty)
|
|
const Padding(
|
|
padding: EdgeInsets.all(Grid.sm),
|
|
child: Text(
|
|
'No agents in this community yet.',
|
|
textAlign: TextAlign.center,
|
|
),
|
|
),
|
|
for (final key in keys)
|
|
ListTile(
|
|
key: ValueKey(key),
|
|
contentPadding: const EdgeInsets.symmetric(
|
|
horizontal: Grid.xxs,
|
|
vertical: Grid.xxs,
|
|
),
|
|
leading: AvatarImage(
|
|
imageUrl: profiles[key]?.avatarUrl,
|
|
radius: Grid.sm,
|
|
isAgent: true,
|
|
backgroundColor: context.colors.surfaceContainerHighest,
|
|
fallback: Center(
|
|
child: Text(names.labelFor(key).characters.first),
|
|
),
|
|
),
|
|
title: Text(names.labelFor(key)),
|
|
onTap: () => onSelect(key),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|