From f8ebd9ee9f297d9f3b87862b082d417ef96b968f Mon Sep 17 00:00:00 2001 From: cogwheel <172976095+cogwheel0@users.noreply.github.com> Date: Wed, 17 Dec 2025 19:40:12 +0530 Subject: [PATCH] feat(api_service): Add extended assistant message fields for OpenWebUI --- lib/core/services/api_service.dart | 66 +++++++++++++++++++++++++----- 1 file changed, 55 insertions(+), 11 deletions(-) diff --git a/lib/core/services/api_service.dart b/lib/core/services/api_service.dart index 6a00242..2ec01be 100644 --- a/lib/core/services/api_service.dart +++ b/lib/core/services/api_service.dart @@ -114,14 +114,17 @@ List> _convertCodeExecutionsToOpenWebUIFormat( // Convert the result if present if (exec.result != null) { final execResult = {}; - if (exec.result!.output != null) execResult['output'] = exec.result!.output; + if (exec.result!.output != null) + execResult['output'] = exec.result!.output; if (exec.result!.error != null) execResult['error'] = exec.result!.error; if (exec.result!.files.isNotEmpty) { execResult['files'] = exec.result!.files - .map((f) => { - if (f.name != null) 'name': f.name, - if (f.url != null) 'url': f.url, - }) + .map( + (f) => { + if (f.name != null) 'name': f.name, + if (f.url != null) 'url': f.url, + }, + ) .toList(); } if (execResult.isNotEmpty) { @@ -939,11 +942,30 @@ class ApiService { 'role': msg.role, 'content': msg.content, 'timestamp': msg.timestamp.millisecondsSinceEpoch ~/ 1000, + // Assistant message fields + if (msg.role == 'assistant' && msg.model != null) 'model': msg.model, + if (msg.role == 'assistant' && msg.model != null) + 'modelName': msg.model, + if (msg.role == 'assistant') 'modelIdx': 0, + if (msg.role == 'assistant') 'done': true, + // User message fields if (msg.role == 'user' && model != null) 'models': [model], if (msg.attachmentIds != null && msg.attachmentIds!.isNotEmpty) 'attachment_ids': List.from(msg.attachmentIds!), if (_sanitizeFilesForWebUI(msg.files) != null) 'files': _sanitizeFilesForWebUI(msg.files), + // Assistant message extended fields + if (msg.statusHistory.isNotEmpty) + 'statusHistory': msg.statusHistory.map((s) => s.toJson()).toList(), + if (msg.followUps.isNotEmpty) + 'followUps': List.from(msg.followUps), + if (msg.codeExecutions.isNotEmpty) + 'code_executions': _convertCodeExecutionsToOpenWebUIFormat( + msg.codeExecutions, + ), + if (msg.sources.isNotEmpty) + 'sources': _convertSourcesToOpenWebUIFormat(msg.sources), + if (msg.usage != null) 'usage': msg.usage, // Preserve error field for OpenWebUI compatibility if (msg.error != null) 'error': msg.error!.toJson(), }; @@ -961,11 +983,30 @@ class ApiService { 'role': msg.role, 'content': msg.content, 'timestamp': msg.timestamp.millisecondsSinceEpoch ~/ 1000, + // Assistant message fields + if (msg.role == 'assistant' && msg.model != null) 'model': msg.model, + if (msg.role == 'assistant' && msg.model != null) + 'modelName': msg.model, + if (msg.role == 'assistant') 'modelIdx': 0, + if (msg.role == 'assistant') 'done': true, + // User message fields if (msg.role == 'user' && model != null) 'models': [model], if (msg.attachmentIds != null && msg.attachmentIds!.isNotEmpty) 'attachment_ids': List.from(msg.attachmentIds!), if (_sanitizeFilesForWebUI(msg.files) != null) 'files': _sanitizeFilesForWebUI(msg.files), + // Assistant message extended fields + if (msg.statusHistory.isNotEmpty) + 'statusHistory': msg.statusHistory.map((s) => s.toJson()).toList(), + if (msg.followUps.isNotEmpty) + 'followUps': List.from(msg.followUps), + if (msg.codeExecutions.isNotEmpty) + 'code_executions': _convertCodeExecutionsToOpenWebUIFormat( + msg.codeExecutions, + ), + if (msg.sources.isNotEmpty) + 'sources': _convertSourcesToOpenWebUIFormat(msg.sources), + if (msg.usage != null) 'usage': msg.usage, // Preserve error field for OpenWebUI compatibility if (msg.error != null) 'error': msg.error!.toJson(), }); @@ -1080,8 +1121,9 @@ class ApiService { if (msg.followUps.isNotEmpty) 'followUps': List.from(msg.followUps), if (msg.codeExecutions.isNotEmpty) - 'code_executions': - _convertCodeExecutionsToOpenWebUIFormat(msg.codeExecutions), + 'code_executions': _convertCodeExecutionsToOpenWebUIFormat( + msg.codeExecutions, + ), // Convert sources back to OpenWebUI format (with document array) if (msg.sources.isNotEmpty) 'sources': _convertSourcesToOpenWebUIFormat(msg.sources), @@ -1122,8 +1164,9 @@ class ApiService { if (msg.followUps.isNotEmpty) 'followUps': List.from(msg.followUps), if (msg.codeExecutions.isNotEmpty) - 'code_executions': - _convertCodeExecutionsToOpenWebUIFormat(msg.codeExecutions), + 'code_executions': _convertCodeExecutionsToOpenWebUIFormat( + msg.codeExecutions, + ), // Convert sources back to OpenWebUI format (with document array) if (msg.sources.isNotEmpty) 'sources': _convertSourcesToOpenWebUIFormat(msg.sources), @@ -1161,8 +1204,9 @@ class ApiService { if (ver.followUps.isNotEmpty) 'followUps': List.from(ver.followUps), if (ver.codeExecutions.isNotEmpty) - 'code_executions': - _convertCodeExecutionsToOpenWebUIFormat(ver.codeExecutions), + 'code_executions': _convertCodeExecutionsToOpenWebUIFormat( + ver.codeExecutions, + ), // Convert sources back to OpenWebUI format (with document array) if (ver.sources.isNotEmpty) 'sources': _convertSourcesToOpenWebUIFormat(ver.sources),