mirror of
https://github.com/foss42/apidash.git
synced 2025-12-02 02:39:19 +08:00
SSEDislay: SelectedModel provided via parameter & StreamingFixes
This commit is contained in:
@@ -1,27 +1,29 @@
|
||||
import 'dart:convert';
|
||||
import 'package:apidash/models/request_model.dart';
|
||||
import 'package:apidash/providers/collection_providers.dart';
|
||||
import 'package:apidash/providers/history_providers.dart';
|
||||
import 'package:apidash_design_system/apidash_design_system.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:genai/genai.dart';
|
||||
import 'package:genai/models/ai_request_model.dart';
|
||||
|
||||
class SSEDisplay extends ConsumerStatefulWidget {
|
||||
class SSEDisplay extends StatefulWidget {
|
||||
final LLMModel? selectedLLModel;
|
||||
final List<String> sseOutput;
|
||||
const SSEDisplay({
|
||||
super.key,
|
||||
required this.sseOutput,
|
||||
this.selectedLLModel,
|
||||
});
|
||||
|
||||
@override
|
||||
ConsumerState<SSEDisplay> createState() => _SSEDisplayState();
|
||||
State<SSEDisplay> createState() => _SSEDisplayState();
|
||||
}
|
||||
|
||||
class _SSEDisplayState extends ConsumerState<SSEDisplay> {
|
||||
class _SSEDisplayState extends State<SSEDisplay> {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final requestModel = ref.read(selectedRequestModelProvider);
|
||||
final aiRequestModel = requestModel?.aiRequestModel;
|
||||
final isAIOutput = (aiRequestModel != null);
|
||||
|
||||
final theme = Theme.of(context);
|
||||
final fontSizeMedium = theme.textTheme.bodyMedium?.fontSize;
|
||||
final isDark = theme.brightness == Brightness.dark;
|
||||
@@ -35,78 +37,90 @@ class _SSEDisplayState extends ConsumerState<SSEDisplay> {
|
||||
);
|
||||
}
|
||||
|
||||
if (isAIOutput) {
|
||||
if (widget.selectedLLModel != null) {
|
||||
// For RAW Text output (only AI Requests)
|
||||
String out = "";
|
||||
final mc = widget.selectedLLModel!.provider.modelController;
|
||||
for (String x in widget.sseOutput) {
|
||||
x = x.substring(6);
|
||||
if (x.contains('[DONE]')) continue;
|
||||
out += aiRequestModel.model.provider.modelController
|
||||
.streamOutputFormatter(jsonDecode(x)) ??
|
||||
"<?>";
|
||||
x = x.trim();
|
||||
if (x.isEmpty || x.contains('[DONE]')) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Start with JSON
|
||||
final pos = x.indexOf('{');
|
||||
if (pos == -1) continue;
|
||||
x = x.substring(pos);
|
||||
|
||||
Map? dec;
|
||||
try {
|
||||
dec = jsonDecode(x);
|
||||
final z = mc.streamOutputFormatter(dec!);
|
||||
out += z ?? '<?>';
|
||||
} catch (e) {
|
||||
print("Error in JSONDEC $e");
|
||||
}
|
||||
}
|
||||
return SingleChildScrollView(
|
||||
child: Text(out),
|
||||
);
|
||||
}
|
||||
|
||||
return SingleChildScrollView(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: (widget.sseOutput)
|
||||
.reversed
|
||||
.where((e) => e != '')
|
||||
.map<Widget>((chunk) {
|
||||
Map<String, dynamic>? parsedJson;
|
||||
try {
|
||||
parsedJson = jsonDecode(chunk);
|
||||
} catch (_) {}
|
||||
return ListView(
|
||||
padding: kP1,
|
||||
children: widget.sseOutput.reversed
|
||||
.where((e) => e.trim() != '')
|
||||
.map<Widget>((chunk) {
|
||||
Map<String, dynamic>? parsedJson;
|
||||
try {
|
||||
parsedJson = jsonDecode(chunk);
|
||||
} catch (_) {}
|
||||
|
||||
return Card(
|
||||
color: theme.colorScheme.surfaceContainerLowest,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: kBorderRadius6,
|
||||
),
|
||||
child: Padding(
|
||||
padding: kP8,
|
||||
child: parsedJson != null
|
||||
? Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: parsedJson.entries.map((entry) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: 2.0),
|
||||
child: Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
'${entry.key}: ',
|
||||
style: kCodeStyle.copyWith(
|
||||
fontSize: fontSizeMedium,
|
||||
color: isDark ? kColorGQL.toDark : kColorGQL,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
return Card(
|
||||
color: theme.colorScheme.surfaceContainerLowest,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: kBorderRadius6,
|
||||
),
|
||||
child: Padding(
|
||||
padding: kP8,
|
||||
child: parsedJson != null
|
||||
? Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: parsedJson.entries.map((entry) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: 2.0),
|
||||
child: Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
'${entry.key}: ',
|
||||
style: kCodeStyle.copyWith(
|
||||
fontSize: fontSizeMedium,
|
||||
color: isDark ? kColorGQL.toDark : kColorGQL,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
const SizedBox(width: 4),
|
||||
Expanded(
|
||||
child: Text(
|
||||
entry.value.toString(),
|
||||
style: kCodeStyle,
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 4),
|
||||
Expanded(
|
||||
child: Text(
|
||||
entry.value.toString(),
|
||||
style: kCodeStyle,
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}).toList(),
|
||||
)
|
||||
: Text(
|
||||
chunk.toString().trim(),
|
||||
style: kCodeStyle.copyWith(
|
||||
fontSize: fontSizeMedium,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}).toList(),
|
||||
)
|
||||
: Text(
|
||||
chunk.toString().trim(),
|
||||
style: kCodeStyle.copyWith(
|
||||
fontSize: fontSizeMedium,
|
||||
),
|
||||
),
|
||||
);
|
||||
}).toList(),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}).toList(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user