Skip to main content

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

Pure Dart core of the CopilotKit SDK. Connects to a Copilot Runtime over AG-UI to run agents, frontend tools, shared state and threads.

example/main.dart

// Headless use of the CopilotKit core without Flutter.
// ignore_for_file: avoid_print

import 'dart:convert';

import 'package:ack/ack.dart';
import 'package:copilotkit/copilotkit.dart';

Future<void> main() async {
  final client = CopilotKitClient(
    runtimeUrl: Uri.parse('http://localhost:8200/api/copilotkit'),
    headers: () async => {'Authorization': 'Bearer <token>'},
    properties: const {'userId': '42'},
    onError: (error, stackTrace) => print('CopilotKit error: $error'),
  );

  // A frontend tool. In Flutter, `CopilotTool` registers it for the
  // lifetime of a widget and renders `ToolCallView`s with a builder.
  final showWeather = FrontendTool(
    name: 'showWeather',
    description: 'Show the weather for a city',
    parameters: Ack.object({'city': Ack.string()}),
    decode: _WeatherArgs.fromJson,
    handler: (args, {required signal}) async =>
        jsonEncode({'city': args.city, 'forecast': 'sunny'}),
  );

  // App data the agent can answer from. In Flutter, `CopilotContext`.
  final user = AgentContextEntry(
    description: 'Current user',
    value: const _User('Ada').toJson(),
  );

  client
    ..registerTool(showWeather)
    ..addContext(user);
  final agent = client.agent('default');
  final subscription = agent.stream.listen((state) {
    for (final call in state.toolCalls) {
      final view = switch (call.status) {
        ToolCallStatus.inProgress => 'loading…',
        ToolCallStatus.executing =>
          'weather for ${showWeather.tryDecode(call.args)?.city}',
        ToolCallStatus.complete => call.result!,
      };
      print('${call.name}: $view');
    }
    print(
      'running: ${state.isRunning}, '
      'messages: ${state.messages.length}',
    );
  });

  // Shared state is sent with every run.
  agent
    ..setSharedState(const SharedState({'theme': 'dark'}))
    ..patchSharedState(const [StatePatchAdd(path: '/units', value: 'metric')]);

  // Completes once the whole turn ends, tool calls included.
  await client.runAgent(
    'default',
    message: const UserMessage(
      id: 'user-1',
      content: "What's the weather in Lima?",
    ),
  );

  await subscription.cancel();
  client
    ..removeContext(user.id)
    ..unregisterTool(showWeather);
  await client.close();
}

/// The arguments of the `showWeather` tool.
class const _WeatherArgs(final String city) {
  factory fromJson(Map<String, Object?> json) =>
      _WeatherArgs(json['city']! as String);
}

/// The current user, shared with the agent as context.
class const _User(final String name) {
  Map<String, Object?> toJson() => {'name': name};
}
0
likes
150
points
--
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

Pure Dart core of the CopilotKit SDK. Connects to a Copilot Runtime over AG-UI to run agents, frontend tools, shared state and threads.

Repository (GitHub)
View/report issues
Contributing

Topics

#ai #agents #copilotkit #ag-ui

License

MIT (license)

Dependencies

ack, ag_ui, collection, equatable, http, json_annotation, meta, uuid, web_socket_channel

More

Packages that depend on copilotkit