Refactor DashBot

This commit is contained in:
Ankit Mahato
2025-09-29 07:25:22 +05:30
parent bd86a71fa8
commit f38ee9f5bf
130 changed files with 391 additions and 521 deletions

View File

@@ -0,0 +1,15 @@
import 'package:flutter/widgets.dart';
class DashbotIcons {
DashbotIcons._();
static String get dashbotIcon1 => 'assets/dashbot/dashbot_icon_1.png';
static String get dashbotIcon2 => 'assets/dashbot/dashbot_icon_2.png';
static Image getDashbotIcon1({double? width, double? height, BoxFit? fit}) {
return Image.asset(dashbotIcon1, width: width, height: height, fit: fit);
}
static Image getDashbotIcon2({double? width, double? height, BoxFit? fit}) {
return Image.asset(dashbotIcon2, width: width, height: height, fit: fit);
}
}

View File

@@ -0,0 +1,22 @@
import 'package:apidash/models/models.dart';
import '../routes/dashbot_routes.dart';
/// Computes the base Dashbot route for a given request based on whether a
/// response exists.
/// - Returns [DashbotRoutes.dashbotHome] if the request has a response (either
/// statusCode or responseStatus present).
/// - Otherwise returns [DashbotRoutes.dashbotDefault].
String computeDashbotBaseRoute(RequestModel? req) {
final hasResponse = (req?.httpResponseModel?.statusCode != null) ||
(req?.responseStatus != null);
return hasResponse ? DashbotRoutes.dashbotHome : DashbotRoutes.dashbotDefault;
}
/// Returns true if the route that should be shown for [req] differs from the
/// currently active [currentRoute].
/// This helper is pure and does not perform any side effects.
bool needsDashbotRouteChange(RequestModel? req, String currentRoute) {
final target = computeDashbotBaseRoute(req);
return target != currentRoute;
}

View File

@@ -0,0 +1,30 @@
import 'dart:convert';
/// Lightweight JSON parser helper to avoid adding dependencies.
/// Intended for parsing AI agent structured outputs that may be wrapped
/// in markdown code fences or include extra prose.
class MessageJson {
static Map<String, dynamic> safeParse(String input) {
// Try strict JSON first
try {
return _parseJson(input);
} catch (_) {
// If input looks like markdown fenced block containing JSON, try to extract
final start = input.indexOf('{');
final end = input.lastIndexOf('}');
if (start != -1 && end != -1 && end > start) {
final slice = input.substring(start, end + 1);
return _parseJson(slice);
}
rethrow;
}
}
static Map<String, dynamic> _parseJson(String s) {
final decoded = jsonDecode(s);
if (decoded is Map<String, dynamic>) {
return decoded;
}
return {};
}
}

View File

@@ -0,0 +1,28 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../dashbot_dashboard.dart';
import '../providers/providers.dart';
void showDashbotWindow(BuildContext context, WidgetRef ref) {
final isDashbotActive = ref.read(dashbotWindowNotifierProvider).isActive;
final isDashbotPopped = ref.read(dashbotWindowNotifierProvider).isPopped;
final windowNotifier = ref.read(dashbotWindowNotifierProvider.notifier);
if (isDashbotActive) return;
if (!isDashbotPopped) return;
final overlay = Overlay.of(context);
OverlayEntry? entry;
entry = OverlayEntry(
builder: (context) => ProviderScope(
child: DashbotWindow(
onClose: () {
entry?.remove();
windowNotifier.toggleActive();
},
),
),
);
// Mark active and insert overlay
windowNotifier.toggleActive();
overlay.insert(entry);
}

View File

@@ -0,0 +1,4 @@
export 'dashbot_icons.dart';
export 'dashbot_route_utils.dart';
export 'safe_parse_json_message.dart';
export 'show_dashbot.dart';