An in-app agent
An in-app agent works with your app's users, in a chat or behind a button.
Start with an agent-aware screen. Give TandemScope your backend's URL as agent:, and put the screen and the chat in its child. The backend must follow the backend contract; the order desk agents show the integration on three frameworks.
child: TandemScope(
surface: (context) => context.read<OrderCubit>(),
agent: Uri.parse('http://localhost:8000/agent'),
approvals: const .fromState(),
onEvent: showOrderMessages,localhost reaches a backend on your computer only from the iOS simulator, macOS and Chrome. On an Android emulator or a phone, use the address that the order desk example lists for it.
Without a backend, connect the screen to Gemini through Firebase AI Logic: pass a FirebaseConnection from tandem_firebase_ai to TandemScope..
An ordinary rebuild keeps the session. Changing agent, the connection passed to TandemScope., or thread replaces it. Keep connection and thread objects in fields so a rebuild reuses them. Leaving the widget tree disposes the session. See Keep the conversation for thread ownership.
approvals: lets your chat draw approval cards. Use onEvent to react when a method returns or a run fails.
Build the chat
Tandem does not include a chat widget. You build the chat with your app's wording, language and styling. TandemSessionScope. returns the session and rebuilds the widget when the session changes. Items in session. have the sealed type ChatItem, so a switch can cover every kind:
Widget build(BuildContext context) => switch (item) {
// A message of session.wake is for the agent, not the user.
UserMessage(fromApp: true) => const SizedBox.shrink(),
UserMessage(:final text) => _Bubble(text, fromUser: true),
AgentMessage(:final text, :final stopped) => _Bubble(
text,
stopped: stopped,
),
ChatNotice(:final kind) => _Notice(kind),
// A card of another call that lands at the same index gets a new state.
final ApprovalRequest request => _ApprovalCard(
request,
approvalBuilder,
key: ValueKey((session, request.key)),
),
};Use request. for approval cards and any data captured when the request shows. This ApprovalKey stays the same when the request's status changes. A later request gets its own key even when the caller reuses a toolCallId.
The example's AgentChat is a complete chat in one file, ready to copy. It sends text with session. and uses the session status to switch its button between Send and Stop.
Trigger a task from a button
A button can call session. with a prepared message, such as text pasted into a form. Show progress from session. and the agent's latest AgentMessage from session.. A screen without a chat still needs a place for approval requests, such as a dialog through approvals: .
The tandem-in-app-agent skill guides your coding agent through either UI. Install the package's skills for the version your app uses.