Skip to content

Commit

Permalink
Merge pull request #30 from guchengxi1994/refactor-flow
Browse files Browse the repository at this point in the history
Refactor flow
  • Loading branch information
guchengxi1994 authored May 12, 2024
2 parents bd5ea2b + 13fd46b commit 36af52b
Show file tree
Hide file tree
Showing 18 changed files with 664 additions and 203 deletions.
86 changes: 45 additions & 41 deletions lib/llm/langchain/components/tools_item.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,52 +15,56 @@ class ToolsItem extends ConsumerStatefulWidget {
class _ToolsItemState extends ConsumerState<ToolsItem> {
@override
Widget build(BuildContext context) {
return InkWell(
return GestureDetector(
onTap: () {
ref.read(toolProvider.notifier).changeState(widget.toolModel);
ref.read(toolProvider.notifier).jumpTo(1);
},
child: FittedBox(
child: Stack(
children: [
const SizedBox(
height: 70,
width: 200,
),
Positioned(
bottom: 0,
child: Container(
width: 200,
height: 50,
padding: const EdgeInsets.only(left: 30),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(4),
color: Colors.white,
border: Border.all(color: AppStyle.appColor),
boxShadow: const [
BoxShadow(
color: AppStyle.appColor,
offset: Offset(0, 4),
blurRadius: 10,
spreadRadius: 3,
)
]),
child: Center(
child: Text(
widget.toolModel.name,
style: const TextStyle(fontFamily: "xing", fontSize: 20),
child: MouseRegion(
cursor: SystemMouseCursors.click,
child: FittedBox(
child: Stack(
children: [
const SizedBox(
height: 70,
width: 200,
),
Positioned(
bottom: 0,
child: Container(
width: 200,
height: 50,
padding: const EdgeInsets.only(left: 30),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(4),
color: Colors.white,
border: Border.all(color: AppStyle.appColor),
boxShadow: const [
BoxShadow(
color: AppStyle.appColor,
offset: Offset(0, 4),
blurRadius: 10,
spreadRadius: 3,
)
]),
child: Center(
child: Text(
widget.toolModel.name,
style:
const TextStyle(fontFamily: "xing", fontSize: 20),
),
),
),
)),
Positioned(
top: 0,
left: 10,
child: SizedBox(
width: 50,
height: 50,
child: Image.asset(widget.toolModel.imgPath),
)),
],
)),
Positioned(
top: 0,
left: 10,
child: SizedBox(
width: 50,
height: 50,
child: Image.asset(widget.toolModel.imgPath),
)),
],
),
),
),
);
Expand Down
16 changes: 6 additions & 10 deletions lib/llm/template_editor/components/chain_flow.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,10 @@ class _ChainFlowState extends ConsumerState<ChainFlow> {
bounds: ref
.watch(chainFlowProvider)
.items
.map((e) => (
Offset(100, e.start * 50 + 25),
Offset(100, e.end * 50 + 25)
))
.map((e) => e.ids
.map((ei) => Offset(100, ei * 50 + 25))
.toList())
.toList(),
radius: 50, // 圆弧的半径
color: Colors.blue, // 圆弧的颜色
),
size: Size(100, childrenHeight),
),
Expand All @@ -100,10 +97,9 @@ class _ChainFlowState extends ConsumerState<ChainFlow> {
.read(chainFlowProvider
.notifier)
.addItem(ChainFlowItem(
end: i,
endContent: e,
start: first!,
startContent: firstStr!));
ids: [first!, i],
contents: [firstStr!, e],
));

first = null;
firstStr = null;
Expand Down
56 changes: 27 additions & 29 deletions lib/llm/template_editor/components/connector_painter.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import 'package:flutter/material.dart';

class ConnectorPainter extends CustomPainter {
final List<(Offset, Offset)> bounds;
final double radius;
final Color color;
final List<List<Offset>> bounds;

ConnectorPainter({
required this.bounds,
required this.radius,
required this.color,
});

@override
Expand All @@ -17,30 +13,32 @@ class ConnectorPainter extends CustomPainter {
..color = Colors.blue // 线条颜色
..strokeWidth = 2; // 线条宽度

for (final point in bounds) {
final startPoint = point.$1;
final endPoint = point.$2;

Path path = Path()
..moveTo(startPoint.dx, startPoint.dy)
..lineTo(startPoint.dx - 10, startPoint.dy - 10)
..lineTo(startPoint.dx - 10, startPoint.dy + 10)
..close();

Path path2 = Path()
..moveTo(endPoint.dx, endPoint.dy)
..lineTo(endPoint.dx - 10, endPoint.dy - 10)
..lineTo(endPoint.dx - 10, endPoint.dy + 10)
..close();

final left1 = Offset(startPoint.dx / 2, startPoint.dy);
final left2 = Offset(endPoint.dx / 2, endPoint.dy);

canvas.drawPath(path, paint);
canvas.drawLine(startPoint, left1, paint);
canvas.drawLine(left1, left2, paint);
canvas.drawLine(left2, endPoint, paint);
canvas.drawPath(path2, paint);
for (var i = 0; i < bounds.length; i++) {
for (var j = 0; j < bounds[i].length - 1; j++) {
final startPoint = bounds[i][j];
final endPoint = bounds[i][j + 1];

Path path = Path()
..moveTo(startPoint.dx, startPoint.dy)
..lineTo(startPoint.dx - 10, startPoint.dy - 10)
..lineTo(startPoint.dx - 10, startPoint.dy + 10)
..close();

Path path2 = Path()
..moveTo(endPoint.dx, endPoint.dy)
..lineTo(endPoint.dx - 10, endPoint.dy - 10)
..lineTo(endPoint.dx - 10, endPoint.dy + 10)
..close();

final left1 = Offset(startPoint.dx / 2, startPoint.dy);
final left2 = Offset(endPoint.dx / 2, endPoint.dy);

canvas.drawPath(path, paint);
canvas.drawLine(startPoint, left1, paint);
canvas.drawLine(left1, left2, paint);
canvas.drawLine(left2, endPoint, paint);
canvas.drawPath(path2, paint);
}
}
}

Expand Down
76 changes: 76 additions & 0 deletions lib/llm/template_editor/components/loading_dialog.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import 'package:all_in_one/layout/styles.dart';
import 'package:all_in_one/src/rust/api/llm_api.dart';
import 'package:all_in_one/src/rust/llm/template.dart';
import 'package:blurrycontainer/blurrycontainer.dart';
import 'package:flutter/material.dart';

class LoadingDialog extends StatefulWidget {
const LoadingDialog({super.key});

@override
State<LoadingDialog> createState() => _LoadingDialogState();
}

class _LoadingDialogState extends State<LoadingDialog> {
final stateStream = templateStateStream();
String state = "evaluating...";

@override
void initState() {
super.initState();
stateStream.listen((event) {
if (mounted) {
switch (event) {
case TemplateRunningStage.format:
setState(() {
state = "formating...";
});
case TemplateRunningStage.eval:
setState(() {
state = "evaluating...";
});
case TemplateRunningStage.optimize:
setState(() {
state = "optimizing...";
});
case TemplateRunningStage.done:
setState(() {
state = "done";
});
Future.delayed(const Duration(milliseconds: 500), () {
Navigator.of(context).pop();
});
}
}
});
}

@override
Widget build(BuildContext context) {
return Align(
alignment: Alignment.centerRight,
child: BlurryContainer(
width: MediaQuery.of(context).size.width -
LayoutStyle.sidebarCollapse -
10,
height: MediaQuery.of(context).size.height,
blur: 5,
elevation: 0,
color: Colors.transparent,
padding: const EdgeInsets.all(8),
borderRadius: const BorderRadius.all(Radius.circular(0)),
child: Center(
child: Card(
child: SizedBox(
width: 200,
height: 200,
child: Center(
child: Text(state),
),
),
),
),
),
);
}
}
10 changes: 0 additions & 10 deletions lib/llm/template_editor/extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,6 @@ extension EditorStateExtension on EditorState {
}
final plainTexts = buffer.toString();

print("""
================================
$plainTexts
========================
""");

return plainTexts;
}
}
41 changes: 29 additions & 12 deletions lib/llm/template_editor/notifiers/chain_flow_notifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,41 @@ class ChainFlowNotifier extends Notifier<ChainFlowState> {
/// 不灵活的实现方案
Future<List<(String, int, int?)>> toRust() async {
List<(String, int, int?)> result = [];
if (state.items.isEmpty) {
final items = await templateToPrompts(template: state.content);
result =
items.mapIndexed((index, element) => (element, index, null)).toList();
} else {
for (final i in state.items) {
result.add((i.startContent, i.start, i.end));
result.add((i.endContent, i.end, null));
final items = await templateToPrompts(template: state.content);
final itemTuple =
items.mapIndexed((index, element) => ((index, element))).toList();

List<int> ids = [];
for (final i in state.items) {
ids.addAll(i.ids);
for (int j = 0; j < i.ids.length; j++) {
if (j < i.ids.length - 1) {
result.add((i.contents[j], i.ids[j], ids[j + 1]));
} else {
result.add((i.contents[j], i.ids[j], null));
}
}
}

itemTuple.retainWhere((element) => !ids.contains(element.$1));

for (final t in itemTuple) {
result.add((t.$2, t.$1, null));
}

return result;
}

removeItem(int start, int end) {
ChainFlowItem item =
ChainFlowItem(end: end, endContent: "", start: start, startContent: "");
final items = Set.from(state.items)..remove(item);
clear() {
state = state.copyWith({}, "");
}

/// 这里需要添加逻辑
/// 一个prompt不能出现
/// 在多个链中
removeItem(int id) {
final items = Set<ChainFlowItem>.from(state.items)
..removeWhere((element) => element.ids.contains(id));
state = state.copyWith(items.cast<ChainFlowItem>(), null);
}
}
Expand Down
Loading

0 comments on commit 36af52b

Please sign in to comment.