mugib_sdk 1.0.3
mugib_sdk: ^1.0.3 copied to clipboard
Official Flutter SDK for Mugib chat sessions, voice agents, and session payload with one apiKey.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:mugib_sdk/mugib_sdk.dart';
void main() => runApp(const MugibSdkExampleApp());
class MugibSdkExampleApp extends StatelessWidget {
const MugibSdkExampleApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Mugib SDK Example',
theme: ThemeData(colorSchemeSeed: const Color(0xFF1677FF)),
home: const DemoPage(),
);
}
}
class DemoPage extends StatefulWidget {
const DemoPage({super.key});
@override
State<DemoPage> createState() => _DemoPageState();
}
class _DemoPageState extends State<DemoPage> {
static const _apiKey = String.fromEnvironment('MUGIB_API_KEY', defaultValue: '');
static const _baseUrl = String.fromEnvironment('MUGIB_BASE_URL', defaultValue: 'https://api.mugib.com');
static const _agentId = int.fromEnvironment('MUGIB_AGENT_ID', defaultValue: 0);
late final MugibClient _mugib;
MugibChatSession? _session;
final _log = <String>[];
bool _busy = false;
@override
void initState() {
super.initState();
_mugib = MugibClient(apiKey: _apiKey, baseUrl: _baseUrl);
}
void _add(String line) => setState(() => _log.add(line));
Future<void> _runChatDemo() async {
if (_apiKey.isEmpty) {
_add('Set MUGIB_API_KEY to run live demo.');
return;
}
setState(() => _busy = true);
try {
final session = await _mugib.chat.createSession(pageUrl: '/demo');
_session = session;
_add('Session: ${session.id}');
final reply = await session.send(
'Hello',
payload: {'source': 'mugib_sdk_example'},
);
_add('Bot: ${reply.reply}');
final buf = StringBuffer();
await for (final e in session.sendStream('What can you help with?')) {
if (e is MugibStreamText) buf.write(e.text);
}
_add('Stream: $buf');
} on MugibException catch (e) {
_add('Error: ${e.message}');
} finally {
setState(() => _busy = false);
}
}
Future<void> _runVoiceDemo() async {
if (_apiKey.isEmpty || _agentId <= 0) {
_add('Set MUGIB_API_KEY and MUGIB_AGENT_ID for voice demo.');
return;
}
_add('Voice: use mugib.voice.client($_agentId) — see README for mic setup.');
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Mugib SDK')),
body: Padding(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
FilledButton(
onPressed: _busy ? null : _runChatDemo,
child: const Text('Run chat demo'),
),
const SizedBox(height: 8),
OutlinedButton(
onPressed: _runVoiceDemo,
child: const Text('Voice demo info'),
),
if (_session != null) ...[
const SizedBox(height: 8),
Text('Active session: ${_session!.id}', style: Theme.of(context).textTheme.bodySmall),
],
const Divider(height: 24),
Expanded(
child: ListView.builder(
itemCount: _log.length,
itemBuilder: (_, i) => Text(_log[i]),
),
),
],
),
),
);
}
}