import 'dart:async'; import 'dart:convert'; import 'package:flutter/services.dart'; /// OpenAI和MCP集成插件 class OpenAI { /// 插件通道 static const MethodChannel _channel = MethodChannel('com.yunqiinnovation.open_ai'); /// 流式输出事件回调 static final Map _streamCallbacks = {}; /// 构造函数 OpenAI() { _channel.setMethodCallHandler(_handleMethodCall); } /// 处理来自原生端的方法调用 Future _handleMethodCall(MethodCall call) async { if (call.method == 'onStreamEvent') { final Map args = Map.from(call.arguments); final String streamId = args['streamId']; final String type = args['type']; final callback = _streamCallbacks[streamId]; if (callback != null) { switch (type) { case 'token': callback.onToken(args['data']); break; case 'complete': callback.onComplete(); _streamCallbacks.remove(streamId); break; case 'error': callback.onError(Exception(args['error'])); _streamCallbacks.remove(streamId); break; case 'functionCall': final functionCall = jsonDecode(args['data']); callback.onFunctionCall(functionCall); break; case 'functionCallResult': final functionCall = jsonDecode(args['functionCall']); final functionCallResult = jsonDecode(args['result']); callback.onFunctionCallResult(functionCall, functionCallResult); break; } } } return null; } /// 初始化OpenAI服务 /// /// [apiKey] OpenAI API密钥 /// [baseUrl] API基础URL,可选 /// [model] 模型名称,可选 /// [mcpServer] MCP服务器URL,可选 Future initialize({ required String apiKey, String baseUrl = '', String model = '', String mcpServer = '', }) async { final result = await _channel.invokeMethod('initialize', { 'apiKey': apiKey, 'baseUrl': baseUrl, 'model': model, 'mcpServer': mcpServer, }); return result ?? false; } /// 创建用户消息 Future> createUserMessage(String content) async { final result = await _channel.invokeMethod('createUserMessage', { 'content': content, }); return jsonDecode(result ?? '{}'); } /// 创建系统消息 Future> createSystemMessage(String content) async { final result = await _channel.invokeMethod('createSystemMessage', { 'content': content, }); return jsonDecode(result ?? '{}'); } /// 创建助手消息 Future> createAssistantMessage(String content) async { final result = await _channel.invokeMethod('createAssistantMessage', { 'content': content, }); return jsonDecode(result ?? '{}'); } /// 创建带图片的用户消息 Future> createUserMessageWithImage( String text, String imageBase64, ) async { final result = await _channel.invokeMethod( 'createUserMessageWithImage', { 'text': text, 'imageBase64': imageBase64, }, ); return jsonDecode(result ?? '{}'); } /// 发送消息(非流式输出) Future sendMessage(List> messages) async { final messagesJson = jsonEncode(messages); return await _channel.invokeMethod('sendMessage', { 'messages': messagesJson, }); } /// 发送消息(流式输出) Future sendMessageStream( List> messages, StreamCallback callback, ) async { final messagesJson = jsonEncode(messages); final streamId = DateTime.now().millisecondsSinceEpoch.toString(); // 注册回调 _streamCallbacks[streamId] = callback; final result = await _channel.invokeMethod('sendMessageStream', { 'messages': messagesJson, 'streamId': streamId, }); return result ?? streamId; } /// 取消当前流式请求 Future cancelCurrentStream() async { final result = await _channel.invokeMethod('cancelCurrentStream'); return result ?? false; } /// 释放资源 Future dispose() async { await _channel.invokeMethod('dispose'); _streamCallbacks.clear(); } } /// 流式输出回调接口 class StreamCallback { /// 收到令牌 final void Function(String token) onToken; /// 完成回调 final void Function() onComplete; /// 错误回调 final void Function(Exception e) onError; /// 函数调用回调 final void Function(Map functionCall) onFunctionCall; /// 函数调用结果回调 final void Function( Map functionCall, Map functionCallResult, ) onFunctionCallResult; /// 构造函数 StreamCallback({ required this.onToken, required this.onComplete, required this.onError, required this.onFunctionCall, required this.onFunctionCallResult, }); }