Skip to main content

copilotkit_flutter 0.1.0 copy "copilotkit_flutter: ^0.1.0" to clipboard
copilotkit_flutter: ^0.1.0 copied to clipboard

Flutter widgets and blocs for the CopilotKit SDK. Add agent chat, frontend tools, shared state and human-in-the-loop to a Flutter app.

example/main.dart

// A minimal chat app on the CopilotKit Flutter SDK. Run a Copilot Runtime
// first: see the CopilotKit docs for the Flutter quickstart.

import 'package:copilotkit_flutter/copilotkit_flutter.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

// On the Android emulator, use http://10.0.2.2:8200/api/copilotkit.
final Uri runtimeUrl = Uri.parse('http://localhost:8200/api/copilotkit');

void main() {
  runApp(
    CopilotKit(
      runtimeUrl: runtimeUrl,
      child: const MaterialApp(home: ChatPage()),
    ),
  );
}

class ChatPage extends StatelessWidget {
  const new({super.key});

  @override
  Widget build(BuildContext context) {
    return CopilotChatProvider(
      agentId: 'default',
      child: Scaffold(
        appBar: AppBar(title: const Text('CopilotKit')),
        body: const SafeArea(
          child: Column(
            children: [
              Expanded(child: MessageList()),
              Composer(),
            ],
          ),
        ),
      ),
    );
  }
}

class MessageList extends StatelessWidget {
  const new({super.key});

  @override
  Widget build(BuildContext context) {
    final rows = context.select<CopilotChatBloc, List<ChatRow>>(
      (bloc) => bloc.state.rows,
    );
    return ListView(
      padding: const EdgeInsets.all(16),
      children: [
        for (final row in rows)
          switch (row) {
            ChatMessageRow(message: UserMessage(:final content)) => ListTile(
              leading: const Icon(Icons.person),
              title: Text(content),
            ),
            ChatMessageRow(message: AssistantMessage(:final content)) =>
              ListTile(
                leading: const Icon(Icons.smart_toy),
                title: Text(content ?? ''),
              ),
            ChatMessageRow() => const SizedBox.shrink(),
            ChatToolCallRow(:final call) => ToolCallWidget(call: call),
          },
      ],
    );
  }
}

class Composer extends StatefulWidget {
  const new({super.key});

  @override
  State<Composer> createState() => _ComposerState();
}

class _ComposerState extends State<Composer> {
  final _controller = TextEditingController();

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  void _send() {
    final bloc = context.read<CopilotChatBloc>();
    // Keep what the user typed until the agent can take it.
    if (bloc.state.isRunning) return;
    bloc.add(ChatMessageSent(_controller.text));
    _controller.clear();
  }

  @override
  Widget build(BuildContext context) {
    final isRunning = context.select<CopilotChatBloc, bool>(
      (bloc) => bloc.state.isRunning,
    );
    return Padding(
      padding: const EdgeInsets.all(8),
      child: TextField(
        controller: _controller,
        onSubmitted: (_) => _send(),
        decoration: InputDecoration(
          hintText: 'Ask something',
          // While the agent replies, a spinner takes the send button's place.
          suffixIcon: isRunning
              ? const Padding(
                  padding: EdgeInsets.all(12),
                  child: SizedBox.square(
                    dimension: 24,
                    child: CircularProgressIndicator(strokeWidth: 2),
                  ),
                )
              : IconButton(icon: const Icon(Icons.send), onPressed: _send),
        ),
      ),
    );
  }
}
0
likes
160
points
--
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

Flutter widgets and blocs for the CopilotKit SDK. Add agent chat, frontend tools, shared state and human-in-the-loop to a Flutter app.

Repository (GitHub)
View/report issues
Contributing

Topics

#ai #agents #copilotkit #ag-ui #chat

License

MIT (license)

Dependencies

ack, bloc, bloc_concurrency, collection, copilotkit, equatable, flutter, flutter_bloc, uuid

More

Packages that depend on copilotkit_flutter