01c36eb32e
- 删除 mock_api_client、mock_calendar_service、mock_history_service - 新增 fixed_length_code_input、link_button、message_composer 共享组件 - 优化登录/注册/密码重置页面使用新组件 - 简化 injection.dart 移除 mock 分支 - 更新 env.dart 配置(BACKEND_URL 替换 API_URL) - 后端 agentscope 工具和测试更新 - 重构 AGENTS.md 文档结构 - 新增 deploy/ 目录和 protocol 文档
582 lines
17 KiB
Dart
582 lines
17 KiB
Dart
import 'dart:convert';
|
|
import 'dart:typed_data';
|
|
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
import 'package:image_picker/image_picker.dart';
|
|
import 'package:social_app/core/api/i_api_client.dart';
|
|
import 'package:social_app/core/di/injection.dart';
|
|
|
|
import '../../data/models/ag_ui_event.dart';
|
|
import '../../data/models/chat_list_item.dart';
|
|
import '../../data/services/ag_ui_service.dart';
|
|
|
|
enum AgentStage { intent, execution, report }
|
|
|
|
class ChatState {
|
|
final List<ChatListItem> items;
|
|
final bool isSending;
|
|
final bool isWaitingFirstToken;
|
|
final bool isStreaming;
|
|
final bool isCancelling;
|
|
final bool isLoadingHistory;
|
|
final String? currentMessageId;
|
|
final String? error;
|
|
final DateTime? oldestLoadedDate;
|
|
final bool hasEarlierHistory;
|
|
final AgentStage? currentStage;
|
|
|
|
const ChatState({
|
|
this.items = const [],
|
|
this.isSending = false,
|
|
this.isWaitingFirstToken = false,
|
|
this.isStreaming = false,
|
|
this.isCancelling = false,
|
|
this.isLoadingHistory = false,
|
|
this.currentMessageId,
|
|
this.error,
|
|
this.oldestLoadedDate,
|
|
this.hasEarlierHistory = false,
|
|
this.currentStage,
|
|
});
|
|
|
|
bool get isLoading =>
|
|
isSending ||
|
|
isWaitingFirstToken ||
|
|
isStreaming ||
|
|
isCancelling ||
|
|
isLoadingHistory;
|
|
|
|
static const _unset = Object();
|
|
|
|
ChatState copyWith({
|
|
List<ChatListItem>? items,
|
|
bool? isSending,
|
|
bool? isWaitingFirstToken,
|
|
bool? isStreaming,
|
|
bool? isCancelling,
|
|
bool? isLoadingHistory,
|
|
Object? currentMessageId = _unset,
|
|
Object? error = _unset,
|
|
Object? oldestLoadedDate = _unset,
|
|
bool? hasEarlierHistory,
|
|
Object? currentStage = _unset,
|
|
}) {
|
|
return ChatState(
|
|
items: items ?? this.items,
|
|
isSending: isSending ?? this.isSending,
|
|
isWaitingFirstToken: isWaitingFirstToken ?? this.isWaitingFirstToken,
|
|
isStreaming: isStreaming ?? this.isStreaming,
|
|
isCancelling: isCancelling ?? this.isCancelling,
|
|
isLoadingHistory: isLoadingHistory ?? this.isLoadingHistory,
|
|
currentMessageId: currentMessageId == _unset
|
|
? this.currentMessageId
|
|
: currentMessageId as String?,
|
|
error: error == _unset ? this.error : error as String?,
|
|
oldestLoadedDate: oldestLoadedDate == _unset
|
|
? this.oldestLoadedDate
|
|
: oldestLoadedDate as DateTime?,
|
|
hasEarlierHistory: hasEarlierHistory ?? this.hasEarlierHistory,
|
|
currentStage: currentStage == _unset
|
|
? this.currentStage
|
|
: currentStage as AgentStage?,
|
|
);
|
|
}
|
|
}
|
|
|
|
class ChatBloc extends Cubit<ChatState> {
|
|
final AgUiService _service;
|
|
final Map<String, String> _toolCallArgsBuffer = {};
|
|
final Map<String, Uint8List> _attachmentPreviewCache = <String, Uint8List>{};
|
|
final Map<String, Future<Uint8List?>> _attachmentPreviewInflight =
|
|
<String, Future<Uint8List?>>{};
|
|
|
|
ChatBloc({AgUiService? service, required IApiClient apiClient})
|
|
: _service = service ?? AgUiService(apiClient: apiClient),
|
|
super(const ChatState()) {
|
|
_service.onEvent = _handleEvent;
|
|
}
|
|
|
|
void _handleEvent(AgUiEvent event) {
|
|
switch (event.type) {
|
|
case AgUiEventType.runStarted:
|
|
emit(
|
|
state.copyWith(
|
|
isSending: false,
|
|
isWaitingFirstToken: true,
|
|
isCancelling: false,
|
|
error: null,
|
|
currentStage: null,
|
|
),
|
|
);
|
|
case AgUiEventType.runFinished:
|
|
emit(
|
|
state.copyWith(
|
|
isSending: false,
|
|
isWaitingFirstToken: false,
|
|
isStreaming: false,
|
|
isCancelling: false,
|
|
currentMessageId: null,
|
|
currentStage: null,
|
|
),
|
|
);
|
|
case AgUiEventType.runError:
|
|
final errorEvent = event as RunErrorEvent;
|
|
emit(
|
|
state.copyWith(
|
|
isSending: false,
|
|
isWaitingFirstToken: false,
|
|
isStreaming: false,
|
|
isCancelling: false,
|
|
currentMessageId: null,
|
|
error: errorEvent.message,
|
|
currentStage: null,
|
|
),
|
|
);
|
|
case AgUiEventType.stepStarted:
|
|
_handleStepStarted(event as StepStartedEvent);
|
|
case AgUiEventType.stepFinished:
|
|
_handleStepFinished(event as StepFinishedEvent);
|
|
case AgUiEventType.textMessageStart:
|
|
_handleTextMessageStart(event as TextMessageStartEvent);
|
|
case AgUiEventType.textMessageContent:
|
|
_handleTextMessageContent(event as TextMessageContentEvent);
|
|
case AgUiEventType.textMessageEnd:
|
|
_handleTextMessageEnd(event as TextMessageEndEvent);
|
|
case AgUiEventType.toolCallStart:
|
|
_handleToolCallStart(event as ToolCallStartEvent);
|
|
case AgUiEventType.toolCallArgs:
|
|
_handleToolCallArgs(event as ToolCallArgsEvent);
|
|
case AgUiEventType.toolCallEnd:
|
|
_handleToolCallEnd(event as ToolCallEndEvent);
|
|
case AgUiEventType.toolCallResult:
|
|
_handleToolCallResult(event as ToolCallResultEvent);
|
|
case AgUiEventType.toolCallError:
|
|
_handleToolCallError(event as ToolCallErrorEvent);
|
|
case AgUiEventType.stateSnapshot:
|
|
_handleStateSnapshot(event as StateSnapshotEvent);
|
|
case AgUiEventType.messagesSnapshot:
|
|
_handleMessagesSnapshot(event as MessagesSnapshotEvent);
|
|
case AgUiEventType.unknown:
|
|
break;
|
|
}
|
|
}
|
|
|
|
void _handleStepStarted(StepStartedEvent event) {
|
|
emit(state.copyWith(currentStage: _stageFromName(event.stepName)));
|
|
}
|
|
|
|
void _handleStepFinished(StepFinishedEvent event) {
|
|
if (state.currentStage == _stageFromName(event.stepName)) {
|
|
emit(state.copyWith(currentStage: null));
|
|
}
|
|
}
|
|
|
|
void _handleTextMessageStart(TextMessageStartEvent startEvent) {
|
|
final newMessage = TextMessageItem(
|
|
id: startEvent.messageId,
|
|
content: '',
|
|
timestamp: DateTime.now(),
|
|
sender: MessageSender.ai,
|
|
isStreaming: true,
|
|
);
|
|
emit(
|
|
state.copyWith(
|
|
items: [...state.items, newMessage],
|
|
currentMessageId: startEvent.messageId,
|
|
isWaitingFirstToken: false,
|
|
isStreaming: true,
|
|
),
|
|
);
|
|
}
|
|
|
|
void _handleTextMessageContent(TextMessageContentEvent contentEvent) {
|
|
final updatedItems = state.items.map((item) {
|
|
if (item.id == contentEvent.messageId && item is TextMessageItem) {
|
|
return item.copyWith(content: item.content + contentEvent.delta);
|
|
}
|
|
return item;
|
|
}).toList();
|
|
emit(state.copyWith(items: updatedItems));
|
|
}
|
|
|
|
void _handleTextMessageEnd(TextMessageEndEvent endEvent) {
|
|
final updatedItems = state.items.map((item) {
|
|
if (item.id == endEvent.messageId && item is TextMessageItem) {
|
|
return item.copyWith(isStreaming: false);
|
|
}
|
|
return item;
|
|
}).toList();
|
|
emit(
|
|
state.copyWith(
|
|
items: updatedItems,
|
|
currentMessageId: null,
|
|
isStreaming: false,
|
|
),
|
|
);
|
|
}
|
|
|
|
void _handleToolCallStart(ToolCallStartEvent startEvent) {
|
|
_toolCallArgsBuffer[startEvent.toolCallId] = '';
|
|
final newToolCall = ToolCallItem(
|
|
id: startEvent.toolCallId,
|
|
callId: startEvent.toolCallId,
|
|
toolName: startEvent.toolCallName,
|
|
args: {},
|
|
status: ToolCallStatus.pending,
|
|
timestamp: DateTime.now(),
|
|
sender: MessageSender.ai,
|
|
);
|
|
emit(state.copyWith(items: [...state.items, newToolCall]));
|
|
}
|
|
|
|
void _handleToolCallArgs(ToolCallArgsEvent argsEvent) {
|
|
_toolCallArgsBuffer[argsEvent.toolCallId] =
|
|
(_toolCallArgsBuffer[argsEvent.toolCallId] ?? '') + argsEvent.delta;
|
|
}
|
|
|
|
void _handleToolCallEnd(ToolCallEndEvent endEvent) {
|
|
final argsBuffer = _toolCallArgsBuffer[endEvent.toolCallId] ?? '';
|
|
Map<String, dynamic> parsedArgs = {};
|
|
if (argsBuffer.isNotEmpty) {
|
|
try {
|
|
parsedArgs = jsonDecode(argsBuffer) as Map<String, dynamic>;
|
|
} catch (_) {}
|
|
}
|
|
_toolCallArgsBuffer.remove(endEvent.toolCallId);
|
|
final updatedItems = state.items.map((item) {
|
|
if (item.id == endEvent.toolCallId && item is ToolCallItem) {
|
|
final nextStatus = item.toolName == 'front.navigate_to_route'
|
|
? ToolCallStatus.pending
|
|
: ToolCallStatus.executing;
|
|
return item.copyWith(args: parsedArgs, status: nextStatus);
|
|
}
|
|
return item;
|
|
}).toList();
|
|
emit(state.copyWith(items: updatedItems));
|
|
}
|
|
|
|
void _handleToolCallResult(ToolCallResultEvent resultEvent) {
|
|
final filteredItems = state.items.where((item) {
|
|
if (item.id == resultEvent.toolCallId && item is ToolCallItem) {
|
|
return false;
|
|
}
|
|
return true;
|
|
}).toList();
|
|
final uiCard = resultEvent.ui;
|
|
if (uiCard == null) {
|
|
emit(state.copyWith(items: filteredItems));
|
|
return;
|
|
}
|
|
final resultItem = ToolResultItem(
|
|
id: resultEvent.messageId,
|
|
callId: resultEvent.toolCallId,
|
|
uiCard: uiCard,
|
|
timestamp: DateTime.now(),
|
|
sender: MessageSender.ai,
|
|
);
|
|
emit(state.copyWith(items: [...filteredItems, resultItem]));
|
|
}
|
|
|
|
void _handleToolCallError(ToolCallErrorEvent errorEvent) {
|
|
_toolCallArgsBuffer.remove(errorEvent.toolCallId);
|
|
final updatedItems = state.items.map((item) {
|
|
if (item.id == errorEvent.toolCallId && item is ToolCallItem) {
|
|
return item.copyWith(
|
|
status: ToolCallStatus.error,
|
|
errorMessage: errorEvent.error,
|
|
);
|
|
}
|
|
return item;
|
|
}).toList();
|
|
emit(state.copyWith(items: updatedItems));
|
|
}
|
|
|
|
void _handleMessagesSnapshot(MessagesSnapshotEvent snapshotEvent) {
|
|
final newItems = _convertSnapshotMessages(snapshotEvent.messages);
|
|
final allItems = [...newItems, ...state.items];
|
|
|
|
// Determine oldest date and history availability
|
|
DateTime? newOldestDate = state.oldestLoadedDate;
|
|
bool newHasEarlierHistory = false;
|
|
|
|
if (newItems.isNotEmpty) {
|
|
newOldestDate = _extractDateFromItems(newItems);
|
|
if (newOldestDate != null) {
|
|
newHasEarlierHistory = _service.hasEarlierHistory(newOldestDate);
|
|
}
|
|
} else if (newOldestDate != null) {
|
|
newHasEarlierHistory = _service.hasEarlierHistory(newOldestDate);
|
|
}
|
|
|
|
emit(
|
|
state.copyWith(
|
|
items: allItems,
|
|
oldestLoadedDate: newOldestDate,
|
|
hasEarlierHistory: newHasEarlierHistory,
|
|
),
|
|
);
|
|
}
|
|
|
|
void _handleStateSnapshot(StateSnapshotEvent stateSnapshotEvent) {
|
|
final snapshot = stateSnapshotEvent.snapshot;
|
|
if (snapshot['scope'] != 'history_day') {
|
|
return;
|
|
}
|
|
final rawMessages = snapshot['messages'];
|
|
if (rawMessages is! List<dynamic>) {
|
|
_handleMessagesSnapshot(MessagesSnapshotEvent(messages: const []));
|
|
return;
|
|
}
|
|
final parsed = <SnapshotMessage>[];
|
|
for (final raw in rawMessages) {
|
|
if (raw is! Map<String, dynamic>) {
|
|
continue;
|
|
}
|
|
parsed.add(SnapshotMessage.fromJson(raw));
|
|
}
|
|
_handleMessagesSnapshot(MessagesSnapshotEvent(messages: parsed));
|
|
}
|
|
|
|
List<ChatListItem> _convertSnapshotMessages(List<SnapshotMessage> messages) {
|
|
return messages.map((msg) {
|
|
final timestamp = msg.timestamp ?? DateTime.now();
|
|
switch (msg.role) {
|
|
case 'user':
|
|
return TextMessageItem(
|
|
id: msg.id,
|
|
content: msg.content ?? '',
|
|
timestamp: timestamp,
|
|
sender: MessageSender.user,
|
|
attachments: msg.attachments ?? const [],
|
|
);
|
|
case 'assistant':
|
|
return TextMessageItem(
|
|
id: msg.id,
|
|
content: msg.content ?? '',
|
|
timestamp: timestamp,
|
|
sender: MessageSender.ai,
|
|
);
|
|
case 'tool' when msg.ui != null:
|
|
return ToolResultItem(
|
|
id: msg.id,
|
|
callId: msg.toolCallId ?? '',
|
|
uiCard: msg.ui!,
|
|
timestamp: timestamp,
|
|
sender: MessageSender.ai,
|
|
);
|
|
default:
|
|
return TextMessageItem(
|
|
id: msg.id,
|
|
content: msg.content ?? '',
|
|
timestamp: timestamp,
|
|
sender: MessageSender.ai,
|
|
);
|
|
}
|
|
}).toList();
|
|
}
|
|
|
|
DateTime? _extractDateFromItems(List<ChatListItem> items) {
|
|
if (items.isEmpty) return null;
|
|
|
|
return items
|
|
.map(
|
|
(item) => DateTime(
|
|
item.timestamp.year,
|
|
item.timestamp.month,
|
|
item.timestamp.day,
|
|
),
|
|
)
|
|
.reduce((a, b) => a.isBefore(b) ? a : b);
|
|
}
|
|
|
|
Future<void> sendMessage(String content, {List<XFile>? images}) async {
|
|
final attachments = (images ?? const <XFile>[])
|
|
.map(
|
|
(image) => <String, dynamic>{
|
|
"path": image.path,
|
|
"mimeType": "image/*",
|
|
},
|
|
)
|
|
.toList();
|
|
final userMessage = TextMessageItem(
|
|
id: 'user-${DateTime.now().millisecondsSinceEpoch}',
|
|
content: content,
|
|
timestamp: DateTime.now(),
|
|
sender: MessageSender.user,
|
|
attachments: attachments,
|
|
);
|
|
emit(
|
|
state.copyWith(
|
|
items: [...state.items, userMessage],
|
|
isSending: true,
|
|
isWaitingFirstToken: true,
|
|
isStreaming: false,
|
|
isCancelling: false,
|
|
error: null,
|
|
),
|
|
);
|
|
try {
|
|
await _service.sendMessage(content, images: images);
|
|
} catch (error) {
|
|
emit(
|
|
state.copyWith(
|
|
isSending: false,
|
|
isWaitingFirstToken: false,
|
|
isStreaming: false,
|
|
isCancelling: false,
|
|
error: error.toString(),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
Future<void> loadHistory() async {
|
|
if (state.isLoadingHistory) return;
|
|
emit(state.copyWith(isLoadingHistory: true));
|
|
try {
|
|
await _service.loadHistory();
|
|
} finally {
|
|
emit(state.copyWith(isLoadingHistory: false));
|
|
}
|
|
}
|
|
|
|
Future<void> loadMoreHistory() async {
|
|
if (state.isLoadingHistory || !state.hasEarlierHistory) return;
|
|
if (state.oldestLoadedDate == null) return;
|
|
emit(state.copyWith(isLoadingHistory: true));
|
|
try {
|
|
await _service.loadHistory(beforeDate: state.oldestLoadedDate);
|
|
} finally {
|
|
emit(state.copyWith(isLoadingHistory: false));
|
|
}
|
|
}
|
|
|
|
Future<void> approveToolCall(String toolCallId) async {
|
|
ToolCallItem? target;
|
|
for (final item in state.items) {
|
|
if (item is ToolCallItem && item.callId == toolCallId) {
|
|
target = item;
|
|
break;
|
|
}
|
|
}
|
|
if (target == null) {
|
|
return;
|
|
}
|
|
final updatedItems = state.items.map((item) {
|
|
if (item is ToolCallItem && item.callId == toolCallId) {
|
|
return item.copyWith(
|
|
status: ToolCallStatus.executing,
|
|
errorMessage: null,
|
|
);
|
|
}
|
|
return item;
|
|
}).toList();
|
|
emit(
|
|
state.copyWith(
|
|
items: updatedItems,
|
|
isSending: false,
|
|
isWaitingFirstToken: true,
|
|
isStreaming: false,
|
|
isCancelling: false,
|
|
error: null,
|
|
),
|
|
);
|
|
try {
|
|
await _service.approveToolCall(
|
|
toolCallId: target.callId,
|
|
toolName: target.toolName,
|
|
args: target.args,
|
|
);
|
|
} catch (error) {
|
|
final failedItems = state.items.map((item) {
|
|
if (item is ToolCallItem && item.callId == toolCallId) {
|
|
return item.copyWith(
|
|
status: ToolCallStatus.error,
|
|
errorMessage: error.toString(),
|
|
);
|
|
}
|
|
return item;
|
|
}).toList();
|
|
emit(
|
|
state.copyWith(
|
|
items: failedItems,
|
|
isSending: false,
|
|
isWaitingFirstToken: false,
|
|
isStreaming: false,
|
|
isCancelling: false,
|
|
error: error.toString(),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
Future<String> transcribeAudioFile(String filePath) {
|
|
return _service.transcribeAudio(filePath);
|
|
}
|
|
|
|
Future<bool> cancelCurrentRun() async {
|
|
if (!(state.isWaitingFirstToken ||
|
|
state.isStreaming ||
|
|
state.isCancelling)) {
|
|
return false;
|
|
}
|
|
emit(state.copyWith(isCancelling: true, error: null));
|
|
try {
|
|
await _service.cancelCurrentRun();
|
|
emit(
|
|
state.copyWith(
|
|
isSending: false,
|
|
isWaitingFirstToken: false,
|
|
isStreaming: false,
|
|
isCancelling: false,
|
|
currentMessageId: null,
|
|
),
|
|
);
|
|
return true;
|
|
} catch (error) {
|
|
emit(state.copyWith(isCancelling: false, error: error.toString()));
|
|
return false;
|
|
}
|
|
}
|
|
|
|
Future<Uint8List?> loadAttachmentPreview(String previewPath) async {
|
|
final cached = _attachmentPreviewCache[previewPath];
|
|
if (cached != null) {
|
|
return cached;
|
|
}
|
|
final pending = _attachmentPreviewInflight[previewPath];
|
|
if (pending != null) {
|
|
return pending;
|
|
}
|
|
final future = _service
|
|
.fetchAttachmentPreview(previewPath)
|
|
.then((bytes) {
|
|
_attachmentPreviewCache[previewPath] = bytes;
|
|
return bytes;
|
|
})
|
|
.catchError((_) => null)
|
|
.whenComplete(() {
|
|
_attachmentPreviewInflight.remove(previewPath);
|
|
});
|
|
_attachmentPreviewInflight[previewPath] = future;
|
|
return future;
|
|
}
|
|
|
|
void clearError() {
|
|
emit(state.copyWith(error: null));
|
|
}
|
|
}
|
|
|
|
AgentStage? _stageFromName(String value) {
|
|
switch (value) {
|
|
case 'intent':
|
|
return AgentStage.intent;
|
|
case 'execution':
|
|
return AgentStage.execution;
|
|
case 'report':
|
|
return AgentStage.report;
|
|
default:
|
|
return null;
|
|
}
|
|
}
|