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.

CopilotKit Flutter #

Very Good Ventures

Developed with 💙 by Very Good Ventures 🦄

ci coverage pub package License: MIT style: very good analysis


Flutter widgets and blocs for the CopilotKit SDK. Add agent chat, frontend tools, shared state and human-in-the-loop to a Flutter app. It re-exports copilotkit, the pure Dart core.

Installation 💻 #

Requires Flutter 3.47 or later.

flutter pub add copilotkit_flutter

Reading the agent below the client #

CopilotAgentScope opens an agent on the nearest CopilotKit client and provides it to its subtree, so a tool card, a form or any other widget can read the agent's state or edit its shared state without wiring the client by hand. The client owns the agent: the scope never closes it.

CopilotAgentScope(
  agentId: 'ops',
  child: AgentStateBuilder<SharedState?>(
    selector: (state) => state.sharedState,
    builder: (context, shared) => Text('${shared?.toJson()}'),
  ),
)

// Below the scope, e.g. in a tool card or a button:
CopilotAgentScope.of(context).setSharedState(
  const SharedState({'theme': 'dark'}),
);

The agent's conversation is rebuilt when the runtime sends a MESSAGES_SNAPSHOT, tool calls included, so tool calls that only exist in a snapshot (a loaded history) render through ToolCallWidget like live ones.

Asking the user #

CopilotHumanInTheLoop registers a HumanInTheLoopTool while it is mounted and renders its calls with their arguments decoded by the tool's decode. respond is null until the call waits for the user, so a card can only answer when it is time.

A tool's parameters is an ack schema. Neither copilotkit_flutter nor copilotkit re-exports ack, so add it to your app with flutter pub add ack and import it:

import 'package:ack/ack.dart';

final confirmAction = HumanInTheLoopTool(
  name: 'confirmAction',
  description: 'Ask the user to confirm an action before doing it.',
  parameters: Ack.object({'action': Ack.string()}),
  decode: ConfirmAction.fromJson,
);

CopilotHumanInTheLoop(
  tool: confirmAction,
  builder: (context, call, args, respond) => Column(
    children: [
      Text('${args?.action}'),
      if (respond != null) ...[
        FilledButton(
          onPressed: () => respond({'decision': 'approve'}),
          child: const Text('Approve'),
        ),
        OutlinedButton(
          onPressed: () => respond({'decision': 'reject'}),
          child: const Text('Reject'),
        ),
      ],
    ],
  ),
  child: const ChatPage(),
)

The widget the builder returns keeps its state while the call moves from streaming to waiting to complete. Keep the tool stable across rebuilds (a field, not a new instance in build): a different tool replaces the registered one, and unmounting the widget while a call waits answers the call as cancelled. The agent's isRunning stays true while a call waits; read CopilotAgentState.awaitingResponseIds to know the agent waits for the user.

Showing a component #

CopilotComponent is the Flutter version of useComponent: it lets the agent show a widget in the chat, with nothing to run in the app. It registers a tool without a handler while it is mounted, and renders each call with the arguments decoded by decode, partial while they stream in. The tool's description starts with the same text as upstream's, so the agent knows the tool only displays something.

// A field, not built in `build`: a new schema on each rebuild would register
// the tool again every time.
static final _parameters = Ack.object({
  'title': Ack.string(),
  'bars': Ack.list(Ack.object({'label': Ack.string(), 'value': Ack.double()})),
});

CopilotComponent<BarChartArgs>(
  name: 'showBarChart',
  description: 'Shows a horizontal bar chart of labelled values.',
  parameters: _parameters,
  decode: BarChartArgs.fromJson,
  builder: (context, call, args) => BarChartCard(call: call, args: args),
  child: const ChatPage(),
)

The call's result is ''. The agent runs again after it, unless followUp is false.

Learning from what the user does #

Warning

Experimental. Learning captures a trajectory, which is in early access on CopilotKit Intelligence and needs the project to be enabled for it. Without that, capture stops after the refused join. See Trajectories.

Set learning on CopilotKit to capture a trajectory for Automatic Learning while it is mounted, then say what to capture:

CopilotKit(
  runtimeUrl: runtimeUrl,
  learning: const CopilotLearning(),
  child: MaterialApp(
    navigatorObservers: [CopilotNavigatorObserver()],
    home: const ChatPage(),
  ),
)

// In a tool card: the tap is captured with the call's thread and tool.
CopilotAction(
  name: 'approve',
  child: FilledButton(onPressed: approve, child: const Text('Approve')),
)

CopilotNavigatorObserver records navigation events from route names, and CopilotAction records a click for a tap on its child without taking the tap from it. Record outcomes with CopilotKit.of(context).emitTrajectoryEvent('deal.approved', {...}). Capture is opt-in and records no text the user types; ask for the user's consent first. Nothing is captured without learning.

Contributing #

To run the tests and the checks CI runs, see CONTRIBUTING.md.

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