From cd6afcfa20b5d17912b572329b2c3893458359c3 Mon Sep 17 00:00:00 2001 From: jdz Date: Wed, 28 Oct 2026 22:56:19 +0800 Subject: [PATCH 1/3] =?UTF-8?q?feat(genai):=20=E6=96=B0=E5=A2=9E=E6=9C=AC?= =?UTF-8?q?=E5=9C=B0=20OpenAI=20=E5=85=BC=E5=AE=B9=20API=20=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/genai-gateway-validation.md | 44 ++ lib/main.dart | 12 +- lib/models/feature.dart | 11 + lib/pages/genai_api_page.dart | 360 ++++++++++++ lib/pages/home_page.dart | 3 +- lib/services/genai/genai_gateway.dart | 664 ++++++++++++++++++++++ lib/services/genai/genai_login.dart | 118 ++++ lib/services/genai/genai_tools.dart | 173 ++++++ lib/services/genai_api_service.dart | 161 ++++++ lib/services/service_provider.dart | 6 +- lib/services/storage_service.dart | 12 + macos/Runner/Release.entitlements | 2 + test/genai_gateway_test.dart | 760 ++++++++++++++++++++++++++ third_party/GenAI2OpenAI/LICENSE | 21 + tool/genai_gateway_smoke.dart | 157 ++++++ 15 files changed, 2500 insertions(+), 4 deletions(-) create mode 100644 docs/genai-gateway-validation.md create mode 100644 lib/pages/genai_api_page.dart create mode 100644 lib/services/genai/genai_gateway.dart create mode 100644 lib/services/genai/genai_login.dart create mode 100644 lib/services/genai/genai_tools.dart create mode 100644 lib/services/genai_api_service.dart create mode 100644 test/genai_gateway_test.dart create mode 100644 third_party/GenAI2OpenAI/LICENSE create mode 100644 tool/genai_gateway_smoke.dart diff --git a/docs/genai-gateway-validation.md b/docs/genai-gateway-validation.md new file mode 100644 index 00000000..4e0ed3aa --- /dev/null +++ b/docs/genai-gateway-validation.md @@ -0,0 +1,44 @@ +# GenAI 网关修复与验收 + +2026-10-28,已同步本地上游 `b517039`,以及其工作区的原生工具增量、空回复重试更新。对照 `/proj/GenAI2OpenAI/docs/TechPie工具链问题交接.md`。 + +## 已修复 + +- 先接收最后一段正文,再处理结束原因。保留 `length`、`content_filter`;没有 finish 或 DONE 的 EOF 返回错误。 +- 工具解析支持标准对象、嵌套 function、顶层数组、尾随链接、多个 JSON 对象、多个 XML 块。保留全部调用及已有 ID。 +- 工具名称必须在本次声明中,arguments 必须能解析为 JSON 对象。只在上游正常结束、调用及参数已完整时补齐外层数组或唯一 tool_calls 包装对象的闭合符;不补调用、参数、字符串或逗号后缺失的值。未知工具、错误参数不回退成成功正文。普通 name 字段、字段说明和空 tool_calls 数组保留为普通答案。这里不做完整 JSON Schema 校验。 +- 支持 Responses 的函数工具定义、function_call / function_call_output 历史、函数输出和 SSE added/delta/done/completed 生命周期。截断为 incomplete,错误为 failed。 +- 补充执行任务时应当返回工具调用的提示,落实 required 和指定函数;auto 仍允许正常总结。仅在上游正常结束且正文和原生工具调用均为空时,使用相同历史重试一次;首轮纯推理不向客户端发布。两次均为空时报错。错误、EOF、length、已输出正文或工具调用均不重试,不重放工具副作用。 +- Chat 优先采用 max_completion_tokens,其次 max_tokens;Responses 使用 max_output_tokens。 +- 原生 tool_calls 按 index 聚合名称及参数分片,兼容 message 和旧 function_call 字段,聚合后统一校验名称及参数;未启用工具时不接收原生调用。 +- SSE 在等待上游响应头时发送心跳,客户端断开时取消等待并关闭对应上游连接。 +- 请求日志前缀 `genai_request`。只记录请求 ID、路径、模型、计数、tool_choice、输出上限、耗时、结束原因、DONE、取消状态、异常类别,不记录凭据、正文或工具结果。`saw_done=false` 表示本次没有读到 DONE;已有 finish_reason 时也正常结束读取。 + +## 拆分前的整包验证 + +以下记录来自 PR #135 的本机安装与整包测试,包含 Linux DDL、手表等模块。本 PR 仅保留 GenAI 网关、功能页和对应测试。 + +- `flutter test test/genai_gateway_test.dart --no-pub`:62 项通过。 +- `flutter test --no-pub`:全套 740 项通过;更新了截止日期空状态断言,以及设置页日志入口的滚动定位。 +- 对修改的 Dart 文件定向运行 `flutter analyze --no-pub`:无问题。 +- `flutter build linux --release --no-pub`:成功。 +- `tool/genai_gateway_smoke.dart`:真实 ChatGLM,在 Chat Completions 和 Responses 上分别完成流式请求工具、夹具返回、非流式总结;开发网关和安装版各验证一次。只提供 fixture_read 夹具,结果为固定编号,没有执行文件或 Git 操作,没有测试 GPT。 +- 安装路径:`/home/jdz/.local/opt/techpie-linux-widgets-v1/bundle`。 +- 本轮安装进程 PID 404337,`127.0.0.1:5000` 属于该进程;核对其加载的 libapp.so 与构建产物一致。 +- libapp.so SHA256:`41666b4dfccc439d6d1622a0f084f4d8816766d204fa11f061cb06e068b7510d`。 +- 上一版备份:`/home/jdz/.local/opt/techpie-linux-widgets-v1/bundle-backup-20261028-194654`。 +- 本次通过 `techpie --genai-api` 恢复 API 服务。平常仍可从 GenAI API 页启动。 + +日志: + +- `/tmp/techpie-all-sync-tests-final.log` +- `/tmp/techpie-genai-sync-tests.log` +- `/tmp/techpie-genai-sync-analyze.log` +- `/tmp/techpie-linux-build-sync.log` +- `/tmp/techpie-real-chatglm-sync.log` +- `/tmp/techpie-installed-chatglm-sync.log` +- `/tmp/techpie-installed-genai-sync.log` + +## VS Code 原客户端 + +尚未完成原客户端的文件读取、编辑验收;本机无该会话的客户端工具记录。原截图的实际 API 路径和停止原因仍未确定。重测时,用请求日志中的 request_id、path、结束原因与客户端工具记录对应。不要以模型声称完成或界面步骤计数认定实际执行成功。 diff --git a/lib/main.dart b/lib/main.dart index 41ffecb8..b24c190e 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -17,6 +17,7 @@ import 'services/debug_logger.dart'; import 'services/ecard_bind_service.dart'; import 'services/ecard_widget_service.dart'; import 'services/egate_app_service.dart'; +import 'services/genai_api_service.dart'; import 'services/http_client.dart'; import 'services/oa_gym_service.dart'; import 'services/schedule_service.dart'; @@ -56,10 +57,10 @@ void main(List args) async { // } final prefs = await SharedPreferences.getInstance(); - await _realMain(prefs); + await _realMain(prefs, startGenaiApi: args.contains('--genai-api')); } -Future _realMain(SharedPreferences prefs) async { +Future _realMain(SharedPreferences prefs, {bool startGenaiApi = false}) async { final storageService = StorageService(prefs); final debugLogger = DebugLogger()..enabled = storageService.debugMode; final httpClient = LoggingHttpClient(debugLogger); @@ -109,6 +110,8 @@ Future _realMain(SharedPreferences prefs) async { // It talks to GitHub rather than our backend, so it keeps its own client // instead of the logging one the API services share. final updateService = UpdateService(); + final genaiApiService = GenaiApiService(storageService); + if (startGenaiApi) unawaited(genaiApiService.start()); authService.onLogout = () async { // Third-party bindings persist across logouts — they will be used by the @@ -183,6 +186,7 @@ Future _realMain(SharedPreferences prefs) async { campusCardService: campusCardService, ecardBindService: ecardBindService, ecardWidgetService: ecardWidgetService, + genaiApiService: genaiApiService, ), ); @@ -311,6 +315,7 @@ class TechPieApp extends StatefulWidget { final CampusCardService campusCardService; final EcardBindService ecardBindService; final EcardWidgetService? ecardWidgetService; + final GenaiApiService? genaiApiService; const TechPieApp({ super.key, @@ -329,6 +334,7 @@ class TechPieApp extends StatefulWidget { required this.campusCardService, required this.ecardBindService, this.ecardWidgetService, + this.genaiApiService, }); @override @@ -367,6 +373,7 @@ class _TechPieAppState extends State with WidgetsBindingObserver { WidgetsBinding.instance.removeObserver(this); widget.ecardWidgetService?.clearOpenPayHandler(); widget.campusCardService.dispose(); + widget.genaiApiService?.dispose(); super.dispose(); } @@ -450,6 +457,7 @@ class _TechPieAppState extends State with WidgetsBindingObserver { campusCardService: widget.campusCardService, ecardBindService: widget.ecardBindService, ecardWidgetService: widget.ecardWidgetService, + genaiApiService: widget.genaiApiService, child: MaterialApp( navigatorKey: _navigatorKey, scaffoldMessengerKey: rootMessengerKey, diff --git a/lib/models/feature.dart b/lib/models/feature.dart index 70600584..1e02afb9 100644 --- a/lib/models/feature.dart +++ b/lib/models/feature.dart @@ -4,6 +4,7 @@ import '../features/campus_card/app/app_providers.dart'; import '../pages/campus_card_page.dart'; import '../pages/egate_app_page.dart'; import '../pages/elrc_recordings_page.dart'; +import '../pages/genai_api_page.dart'; import '../pages/oa_gym_page.dart'; import '../widgets/adaptive_page_navigation.dart'; @@ -39,6 +40,16 @@ class Feature { } final featureEntries = [ + Feature( + id: 'genai_api', + description: 'GenAI API', + mode: FeatureMode.native, + nativeEntry: (context) => pushAdaptivePage( + context, + builder: (_) => const GenaiApiPage(), + ), + icon: const Icon(Icons.auto_awesome_outlined), + ), Feature( id: 'elrc_recordings', description: '课程录播', diff --git a/lib/pages/genai_api_page.dart b/lib/pages/genai_api_page.dart new file mode 100644 index 00000000..ae0fb170 --- /dev/null +++ b/lib/pages/genai_api_page.dart @@ -0,0 +1,360 @@ +import 'dart:async'; + +import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; + +import '../services/genai_api_service.dart'; +import '../services/service_provider.dart'; +import '../utils/platform.dart'; +import '../widgets/adaptive_button.dart'; +import '../widgets/adaptive_feedback.dart'; +import '../widgets/adaptive_page_navigation.dart'; +import '../widgets/adaptive_select.dart'; +import '../widgets/blurred_app_bar.dart'; +import '../widgets/ios/ios_native_navigation_bar.dart'; + +class GenaiApiPage extends StatefulWidget { + const GenaiApiPage({super.key, this.service}); + final GenaiApiService? service; + + @override + State createState() => _GenaiApiPageState(); +} + +class _GenaiApiPageState extends State { + GenaiApiService? _service; + final _apiToken = TextEditingController(); + final _port = TextEditingController(text: '5000'); + String _host = '127.0.0.1'; + bool _ready = false; + bool _showToken = false; + bool _ownsService = false; + bool _saving = false; + + @override + void didChangeDependencies() { + super.didChangeDependencies(); + if (_service != null) return; + final provider = widget.service == null ? ServiceProvider.of(context) : null; + _service = widget.service ?? provider?.genaiApiService; + if (_service == null) { + _service = GenaiApiService(provider!.storageService); + _ownsService = true; + } + unawaited(_load()); + } + + Future _load() async { + try { + await _service!.load(); + if (!mounted) return; + _apiToken.text = _service!.apiToken; + _port.text = '${_service!.port}'; + _host = _service!.host; + setState(() => _ready = true); + } catch (_) { + if (mounted) setState(() {}); + } + } + + @override + void dispose() { + if (_ownsService) _service?.dispose(); + _apiToken.dispose(); + _port.dispose(); + super.dispose(); + } + + Future _start() async { + if (_saving) return; + setState(() => _saving = true); + try { + await _service!.saveSettings(apiToken: _apiToken.text, port: _port.text, host: _host); + await _service!.start(); + } on FormatException catch (e) { + if (mounted) { + showAdaptiveFeedback( + context: context, + message: e.message, + style: AdaptiveFeedbackStyle.error, + ); + } + } catch (_) { + if (mounted) { + showAdaptiveFeedback( + context: context, + message: '保存失败,请重试', + style: AdaptiveFeedbackStyle.error, + ); + } + } finally { + if (mounted) setState(() => _saving = false); + } + } + + Future _save() async { + if (_saving) return; + setState(() => _saving = true); + try { + await _service!.saveSettings(apiToken: _apiToken.text, port: _port.text, host: _host); + if (mounted) showAdaptiveFeedback(context: context, message: '已保存'); + } catch (e) { + if (mounted) { + showAdaptiveFeedback( + context: context, + message: e is FormatException ? e.message : '保存失败,请重试', + ); + } + } finally { + if (mounted) setState(() => _saving = false); + } + } + + Future _copy(String text) async { + await Clipboard.setData(ClipboardData(text: text)); + if (mounted) showAdaptiveFeedback(context: context, message: '已复制'); + } + + @override + Widget build(BuildContext context) { + final service = _service!; + final useIosChrome = isIos(); + final top = useIosChrome || usesLegacyIosChrome() + ? 16.0 + : adaptiveTopBarHeight() + MediaQuery.viewPaddingOf(context).top + 16; + return ListenableBuilder( + listenable: service, + builder: (context, _) { + final editable = _ready && !service.busy && !service.running && !_saving; + return Scaffold( + extendBodyBehindAppBar: !useIosChrome && !usesLegacyIosChrome(), + appBar: useIosChrome + ? IosNativeNavigationBar( + title: 'GenAI API', + leadingItems: const [ + IosNativeNavigationBarItem(id: 'back', title: '返回', sfSymbol: 'chevron.left'), + ], + onItemPressed: (_) => Navigator.of(context).maybePop(), + ) + : const BlurredAppBar(title: Text('GenAI API')), + body: ListView( + padding: + EdgeInsets.fromLTRB(16, top, 16, MediaQuery.viewPaddingOf(context).bottom + 24), + children: [ + Card.outlined( + child: Padding( + padding: const EdgeInsets.all(16), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + ListTile( + contentPadding: EdgeInsets.zero, + leading: Icon( + service.loggedIn ? Icons.check_circle_outline : Icons.person_outline,), + title: Text(service.loggedIn ? '已登录 GenAI' : '登录 GenAI'), + trailing: editable + ? TextButton( + onPressed: () => unawaited( + pushAdaptivePage( + context, + builder: (_) => _GenaiLoginPage(service: service), + ), + ), + child: Text(service.loggedIn ? '切换账号' : '登录'), + ) + : null, + ), + if (service.loggedIn && editable) + TextButton( + onPressed: () async { + try { + await service.logout(); + } catch (_) { + if (context.mounted) { + showAdaptiveFeedback(context: context, message: '退出失败,请重试'); + } + } + }, + child: const Text('退出登录'), + ), + ], + ), + ), + ), + const SizedBox(height: 16), + Card.outlined( + child: Padding( + padding: const EdgeInsets.all(16), + child: Column( + crossAxisAlignment: CrossAxisAlignment.stretch, + children: [ + Text('本地 API', style: Theme.of(context).textTheme.titleMedium), + const SizedBox(height: 12), + TextField( + controller: _port, + enabled: editable, + decoration: const InputDecoration(labelText: '端口'), + keyboardType: TextInputType.number, + inputFormatters: [FilteringTextInputFormatter.digitsOnly], + ), + const SizedBox(height: 12), + TextField( + controller: _apiToken, + enabled: editable, + obscureText: !_showToken, + autocorrect: false, + enableSuggestions: false, + decoration: InputDecoration( + labelText: 'API Token', + suffixIcon: Row( + mainAxisSize: MainAxisSize.min, + children: [ + IconButton( + tooltip: _showToken ? '隐藏' : '显示', + onPressed: () => setState(() => _showToken = !_showToken), + icon: Icon( + _showToken + ? Icons.visibility_off_outlined + : Icons.visibility_outlined, + ), + ), + IconButton( + tooltip: '复制 Token', + onPressed: _ready ? () => unawaited(_copy(_apiToken.text)) : null, + icon: const Icon(Icons.copy_outlined), + ), + ], + ), + ), + ), + const SizedBox(height: 16), + const Text('监听范围'), + const SizedBox(height: 8), + IgnorePointer( + ignoring: !editable, + child: AdaptiveSelect( + value: _host, + accessibilityLabel: '监听范围', + options: const [ + AdaptiveSelectOption(value: '127.0.0.1', label: '仅本机'), + AdaptiveSelectOption(value: '0.0.0.0', label: '局域网'), + ], + onChanged: (value) => setState(() => _host = value), + ), + ), + TextButton( + onPressed: editable ? () => unawaited(_save()) : null, + child: const Text('保存设置'), + ), + const SizedBox(height: 20), + AdaptiveButton( + onPressed: !_ready || + service.busy || + _saving || + (!service.running && !service.loggedIn) + ? null + : () => unawaited(service.running ? service.stop() : _start()), + icon: service.running ? Icons.stop_rounded : Icons.play_arrow_rounded, + sfSymbol: service.running ? 'stop.fill' : 'play.fill', + label: service.running ? '停止服务' : '启动服务', + loading: service.busy, + role: AdaptiveButtonRole.prominent, + ), + const SizedBox(height: 12), + Text(service.running ? '运行中' : '未启动', textAlign: TextAlign.center), + if (service.running) ...[ + const SizedBox(height: 12), + SelectableText(service.endpoint), + TextButton( + onPressed: () => unawaited(_copy(service.endpoint)), + child: const Text('复制地址'), + ), + ], + ], + ), + ), + ), + if (service.error != null) + Padding( + padding: const EdgeInsets.all(16), + child: Text(service.error!), + ), + if (!_ready && service.error != null) + TextButton(onPressed: () => unawaited(_load()), child: const Text('重试')), + ], + ), + ); + }, + ); + } +} + +class _GenaiLoginPage extends StatefulWidget { + const _GenaiLoginPage({required this.service}); + final GenaiApiService service; + @override + State<_GenaiLoginPage> createState() => _GenaiLoginPageState(); +} + +class _GenaiLoginPageState extends State<_GenaiLoginPage> { + final _account = TextEditingController(); + final _password = TextEditingController(); + @override + void dispose() { + _account.dispose(); + _password.dispose(); + super.dispose(); + } + + Future _login() async { + final ok = await widget.service.login(_account.text, _password.text); + if (!mounted) return; + if (ok) Navigator.of(context).pop(); + } + + @override + Widget build(BuildContext context) => ListenableBuilder( + listenable: widget.service, + builder: (context, _) => Scaffold( + appBar: AppBar(title: const Text('登录 GenAI')), + body: ListView( + padding: const EdgeInsets.all(24), + children: [ + TextField( + controller: _account, + enabled: !widget.service.busy, + decoration: const InputDecoration(labelText: '学号'), + keyboardType: TextInputType.text, + textInputAction: TextInputAction.next, + autocorrect: false, + enableSuggestions: false, + ), + const SizedBox(height: 12), + TextField( + controller: _password, + enabled: !widget.service.busy, + decoration: const InputDecoration(labelText: '密码'), + obscureText: true, + onSubmitted: (_) { + if (!widget.service.busy) unawaited(_login()); + }, + ), + const SizedBox(height: 24), + AdaptiveButton( + onPressed: widget.service.busy ? null : () => unawaited(_login()), + icon: Icons.login, + sfSymbol: 'person.crop.circle', + label: '登录', + loading: widget.service.busy, + role: AdaptiveButtonRole.prominent, + ), + if (widget.service.error != null) + Padding( + padding: const EdgeInsets.only(top: 16), + child: Text(widget.service.error!), + ), + ], + ), + ), + ); +} diff --git a/lib/pages/home_page.dart b/lib/pages/home_page.dart index d58445c4..33d96192 100644 --- a/lib/pages/home_page.dart +++ b/lib/pages/home_page.dart @@ -381,7 +381,8 @@ class _HomePageState extends State with TickerProviderStateMixin { } void _handleFeatureTap(Feature feature) { - if (!ServiceProvider.of(context).authService.isLoggedIn) { + if (feature.id != 'genai_api' && + !ServiceProvider.of(context).authService.isLoggedIn) { unawaited(presentLoginPage(context)); return; } diff --git a/lib/services/genai/genai_gateway.dart b/lib/services/genai/genai_gateway.dart new file mode 100644 index 00000000..d0933708 --- /dev/null +++ b/lib/services/genai/genai_gateway.dart @@ -0,0 +1,664 @@ +// GenAI request mapping adapted from GenAI2OpenAI. +// See third_party/GenAI2OpenAI/LICENSE. +import 'dart:async'; +import 'dart:convert'; +import 'dart:io'; + +import 'genai_tools.dart'; + +class GenaiGateway { + GenaiGateway({ + Uri? upstream, + HttpClient Function()? clientFactory, + this.onDiagnostic, + this.heartbeatInterval = const Duration(seconds: 5), + }) : upstream = upstream ?? Uri.parse('https://genai.shanghaitech.edu.cn'), + _clientFactory = clientFactory ?? HttpClient.new; + + final void Function(Map)? onDiagnostic; + final Uri upstream; + final HttpClient Function() _clientFactory; + final Duration heartbeatInterval; + final Set _streams = {}; + HttpServer? _server; + HttpClient? _client; + String _genaiToken = ''; + String _apiToken = ''; + final Map _routes = {}; + int? get port => _server?.port; + bool get running => _server != null; + + Future start({ + required String host, + required int port, + required String genaiToken, + required String apiToken, + }) async { + if (_server != null) return; + _genaiToken = genaiToken; + _apiToken = apiToken; + _client = _clientFactory()..connectionTimeout = const Duration(seconds: 15); + try { + _server = await HttpServer.bind(host, port); + _server!.listen((request) => unawaited(_handle(request))); + } catch (_) { + _client!.close(force: true); + _client = null; + rethrow; + } + } + + Future stop() async { + final server = _server; + _server = null; + _client?.close(force: true); + _client = null; + for (final client in _streams) { + client.close(force: true); + } + _streams.clear(); + _genaiToken = ''; + _apiToken = ''; + _routes.clear(); + await server?.close(force: true); + } + + void _json(HttpResponse response, Object body, [int status = 200]) { + response.statusCode = status; + response.headers.contentType = ContentType.json; + response.write(jsonEncode(body)); + } + + Future _upstream(String path, {Object? body, HttpClient? client}) async { + final uri = upstream.resolve(path); + final request = await (client ?? _client!).openUrl(body == null ? 'GET' : 'POST', uri); + request.headers.set('X-Access-Token', _genaiToken); + request.headers.set('Origin', 'https://genai.shanghaitech.edu.cn'); + request.headers.set('Referer', 'https://genai.shanghaitech.edu.cn/dialogue'); + request.headers.set(HttpHeaders.acceptHeader, 'application/json, text/event-stream'); + request.headers.contentType = ContentType.json; + if (body != null) request.write(jsonEncode(body)); + return request.close().timeout(const Duration(seconds: 180)); + } + + Future>> _models() async { + final res = await _upstream('/htk/ai/aiModel/list?pageNo=1&pageSize=999&showStatusList=2%2C3'); + final data = jsonDecode(await utf8.decoder.bind(res).join()); + if (res.statusCode != 200 || + data is! Map || + data['success'] != true || + data['result'] is! Map || + data['result']['records'] is! List) { + throw const FormatException('获取模型失败,请重新登录'); + } + final records = (data['result']['records'] as List).whereType>(); + final models = >[]; + final seen = {}; + for (final record in records) { + final id = record['aiType']; + if (id is! String || id.isEmpty || !seen.add(id)) continue; + if (record['rootAiType'] is String) { + _routes[id] = record['rootAiType'] as String; + } + models.add({'id': id, 'object': 'model', 'owned_by': 'genai'}); + } + return models; + } + + Future _handle(HttpRequest request) async { + final watch = Stopwatch()..start(); + final diagnostic = { + 'request_id': 'req_${DateTime.now().microsecondsSinceEpoch}', + 'path': request.uri.path, + 'cancelled': false, + 'call_count': 0, + 'content_chars': 0, + 'saw_done': false, + }; + request.response.headers.set('X-Request-Id', diagnostic['request_id']!); + var completed = false; + var streaming = false; + Timer? heartbeat; + StreamController>? sse; + Future? ssePump; + final cancelled = Completer(); + HttpClient? streamClient; + void closeStream() { + heartbeat?.cancel(); + streamClient?.close(force: true); + _streams.remove(streamClient); + } + + void cancelRequest() { + if (completed) return; + diagnostic['cancelled'] = true; + if (!cancelled.isCompleted) cancelled.complete(); + closeStream(); + } + + void writeSse(String value) { + if (cancelled.isCompleted) throw const HttpException('Client disconnected'); + sse!.add(utf8.encode(value)); + } + + try { + final path = request.uri.path; + if (path == '/health' && request.method == 'GET') { + _json(request.response, {'status': 'ok'}); + return; + } + var key = request.headers.value(HttpHeaders.authorizationHeader) ?? ''; + if (key.toLowerCase().startsWith('bearer ')) key = key.substring(7).trim(); + key = key.isNotEmpty + ? key + : request.headers.value('x-api-key') ?? request.headers.value('api-key') ?? ''; + if (key != _apiToken) { + _json( + request.response, + { + 'error': {'message': 'API Token 不对', 'type': 'authentication_error'}, + }, + 401, + ); + return; + } + if (path == '/v1/models' && request.method == 'GET') { + _json(request.response, {'object': 'list', 'data': await _models()}); + return; + } + if (request.method != 'POST' || (path != '/v1/chat/completions' && path != '/v1/responses')) { + _json( + request.response, + { + 'error': {'message': '接口不存在'}, + }, + 404, + ); + return; + } + final data = jsonDecode(await utf8.decoder.bind(request).join()); + if (data is! Map) throw const FormatException('请求格式不对'); + final model = data['model']; + if (model is! String || model.isEmpty) throw const FormatException('请选择模型'); + final isResponses = path == '/v1/responses'; + var messages = isResponses ? _responseMessages(data) : data['messages']; + if (messages is! List || messages.isEmpty || messages.any((m) => m is! Map)) { + throw const FormatException('消息不能为空'); + } + messages = messages.map((m) => Map.from(m as Map)).toList(); + diagnostic.addAll({'model': model, 'message_count': messages.length}); + final tools = data['tools']; + final toolList = tools is List + ? tools.whereType>().toList() + : >[]; + final allowed = toolList + .map((t) => (t['function'] is Map ? t['function']['name'] : t['name'])) + .whereType() + .toSet(); + final choice = data['tool_choice']; + final selected = choice is Map + ? (choice['function'] is Map ? choice['function']['name'] : choice['name']) + : null; + if ((choice == 'required' && allowed.isEmpty) || + (selected != null && !allowed.contains(selected))) { + throw const FormatException('请选择可用工具'); + } + final limit = isResponses + ? data['max_output_tokens'] ?? 30000 + : data['max_completion_tokens'] ?? data['max_tokens'] ?? 30000; + if (limit is! int || limit < 1) throw const FormatException('输出上限必须是正整数'); + diagnostic.addAll({ + 'tool_count': toolList.length, + 'tool_choice': selected ?? (choice is String ? choice : 'auto'), + 'output_limit': limit, + }); + final hasTools = tools is List && tools.isNotEmpty && data['tool_choice'] != 'none'; + if (hasTools) { + messages = [ + ...messages, + { + 'role': 'system', + 'content': '需要调用工具时,只输出 JSON:' + '{"tool_calls":[{"name":"工具名","arguments":{}}]}。' + '如果需要通过工具查看文件或执行操作,本轮必须输出工具调用,不能只描述接下来要做什么;完成后再总结。' + '不需要工具时正常回答。tool_choice: ${jsonEncode(data['tool_choice'] ?? 'auto')}。' + '工具:${jsonEncode(tools)}', + } + ]; + } + final upstreamMessages = messages.map((dynamic m) { + if (m['role'] == 'tool') { + return { + 'role': 'user', + 'content': '工具 ${m['name'] ?? m['tool_call_id']} 返回:${m['content']}', + }; + } + if (m['role'] == 'assistant' && m['tool_calls'] != null) { + return { + 'role': 'assistant', + 'content': '${m['content'] ?? ''}\n已请求调用工具:${jsonEncode(m['tool_calls'])}', + }; + } + return m; + }).toList(); + final root = _routes[model] ?? + (RegExp(r'^(gpt|o3|o4-mini)', caseSensitive: false).hasMatch(model) + ? 'azure' + : 'xinference'); + final wantsStream = data['stream'] == true; + streamClient = _clientFactory()..connectionTimeout = const Duration(seconds: 10); + _streams.add(streamClient); + unawaited( + request.response.done.then( + (_) { + if (!completed) diagnostic['cancelled'] = true; + closeStream(); + }, + onError: (Object error, StackTrace stack) { + diagnostic['cancelled'] = true; + closeStream(); + }, + ), + ); + if (wantsStream) { + streaming = true; + request.response.headers.contentType = + ContentType('text', 'event-stream', charset: 'utf-8'); + request.response.headers.set(HttpHeaders.cacheControlHeader, 'no-cache'); + request.response.headers.set('X-Accel-Buffering', 'no'); + request.response.bufferOutput = false; + sse = StreamController>(onCancel: cancelRequest); + ssePump = request.response.addStream(sse.stream).then( + (_) { + if (!completed) cancelRequest(); + }, + onError: (Object error, StackTrace stack) => cancelRequest(), + ); + writeSse(': keep-alive\n\n'); + heartbeat = Timer.periodic(heartbeatInterval, (timer) { + if (!cancelled.isCompleted) writeSse(': keep-alive\n\n'); + }); + } + final id = '${isResponses ? 'resp' : 'chatcmpl'}-${DateTime.now().microsecondsSinceEpoch}'; + final created = DateTime.now().millisecondsSinceEpoch ~/ 1000; + final content = StringBuffer(); + final reasoning = StringBuffer(); + Future emit(Object event, [String? name]) async { + if (name != null) writeSse('event: $name\n'); + writeSse('data: ${event is String ? event : jsonEncode(event)}\n\n'); + } + + Map chunk(Map delta, [String? finish]) => { + 'id': id, + 'object': 'chat.completion.chunk', + 'created': created, + 'model': model, + 'choices': [ + {'index': 0, 'delta': delta, 'finish_reason': finish}, + ], + }; + if (wantsStream && isResponses) { + await emit( + { + 'type': 'response.created', + 'sequence_number': 0, + 'response': { + 'id': id, + 'object': 'response', + 'status': 'in_progress', + 'output': [], + }, + }, + 'response.created', + ); + } + String? upstreamFinish; + var sawDone = false; + var nativeCalls = GenaiToolAccumulator(); + for (var attempt = 1; attempt <= 2; attempt++) { + if (cancelled.isCompleted) throw const HttpException('Client disconnected'); + diagnostic['attempts'] = attempt; + if (attempt > 1) { + streamClient!.close(force: true); + _streams.remove(streamClient); + streamClient = _clientFactory()..connectionTimeout = const Duration(seconds: 10); + _streams.add(streamClient); + content.clear(); + reasoning.clear(); + nativeCalls = GenaiToolAccumulator(); + upstreamFinish = null; + sawDone = false; + } + var published = false; + final upstreamResponse = await Future.any([ + cancelled.future + .then((_) => throw const HttpException('Client disconnected')), + _upstream( + '/htk/chat/start/chat', + client: streamClient, + body: { + 'chatInfo': '', + 'messages': upstreamMessages, + 'type': '3', + 'stream': true, + 'aiType': model, + 'aiSecType': '1', + 'promptTokens': 0, + 'rootAiType': root, + 'maxToken': limit, + }, + ), + ]); + diagnostic['connect_ms'] = watch.elapsedMilliseconds; + if (upstreamResponse.statusCode != 200) { + await upstreamResponse.drain(); + throw FormatException('GenAI 返回 ${upstreamResponse.statusCode}'); + } + + await for (final line in upstreamResponse + .transform(utf8.decoder) + .transform(const LineSplitter()) + .timeout(const Duration(seconds: 180))) { + final text = line.startsWith('data:') ? line.substring(5).trim() : line.trim(); + if (text == '[DONE]') { + sawDone = true; + break; + } + if (text.isEmpty) continue; + dynamic event; + try { + event = jsonDecode(text); + } on FormatException { + continue; + } + if (event is! Map) continue; + if (event['errMsg'] != null || event['success'] == false || event['error'] != null) { + throw FormatException('${event['errMsg'] ?? event['message'] ?? event['error']}'); + } + final choices = event['choices']; + if (choices is! List || choices.isEmpty) continue; + final choice = choices.first as Map; + final delta = choice['delta'] ?? choice['message']; + if (delta is Map) { + final fragments = delta['tool_calls'] ?? + (delta['function_call'] == null + ? null + : [ + {'index': 0, 'function': delta['function_call']}, + ]); + nativeCalls.add(fragments); + if (nativeCalls.isNotEmpty && !hasTools) throw const FormatException('本次请求未启用工具'); + final body = delta['content']; + final thought = delta['reasoning'] ?? delta['reasoning_content']; + if (body is String && body.isNotEmpty) { + diagnostic.putIfAbsent('first_content_ms', () => watch.elapsedMilliseconds); + content.write(body); + } + if (thought is String) reasoning.write(thought); + if (wantsStream && !hasTools && !isResponses && content.toString().trim().isNotEmpty) { + await emit( + chunk({ + 'role': 'assistant', + if (body is String) 'content': published ? body : content.toString(), + if (reasoning.isNotEmpty) + 'reasoning_content': published ? (thought ?? '') : reasoning.toString(), + }), + ); + published = true; + } + } + if (choice['finish_reason'] != null) { + upstreamFinish = choice['finish_reason'].toString(); + break; + } + } + final empty = content.toString().trim().isEmpty && !nativeCalls.isNotEmpty; + final normalEnd = upstreamFinish == 'stop' || (upstreamFinish == null && sawDone); + if (empty && normalEnd && attempt == 1) continue; + break; + } + diagnostic.addAll({ + 'content_chars': content.length, + 'upstream_finish_reason': upstreamFinish, + 'saw_done': sawDone, + }); + if (upstreamFinish == null && !sawDone) { + throw const FormatException('GenAI 回复中断'); + } + if (content.toString().trim().isEmpty && !nativeCalls.isNotEmpty) { + throw const FormatException('GenAI 没有回复,请重试'); + } + heartbeat?.cancel(); + final truncated = upstreamFinish != null && + upstreamFinish != 'stop' && + upstreamFinish != 'tool_calls' && + upstreamFinish != 'function_call'; + if (hasTools && + truncated && + (nativeCalls.isNotEmpty || looksLikeToolProtocol(content.toString()))) { + throw const FormatException('工具调用未完成,请重试'); + } + final calls = hasTools && !truncated + ? parseGenaiToolCalls( + nativeCalls.isNotEmpty + ? jsonEncode({'tool_calls': nativeCalls.result()}) + : content.toString(), + allowed, + choice, + ) + : >[]; + if ((upstreamFinish == 'tool_calls' || upstreamFinish == 'function_call') && calls.isEmpty) { + throw const FormatException('GenAI 未返回完整工具调用'); + } + diagnostic['call_count'] = calls.length; + final finish = truncated + ? upstreamFinish + : calls.isEmpty + ? (upstreamFinish ?? 'stop') + : 'tool_calls'; + if (isResponses) { + final response = { + 'id': id, + 'object': 'response', + 'created_at': created, + 'status': truncated ? 'incomplete' : 'completed', + if (truncated) + 'incomplete_details': {'reason': finish == 'length' ? 'max_output_tokens' : finish}, + 'model': model, + 'output_text': calls.isEmpty ? content.toString() : '', + 'output': [ + for (final call in calls) + { + 'id': 'fc_${call['id']}', + 'type': 'function_call', + 'status': 'completed', + 'call_id': call['id'], + 'name': call['function']['name'], + 'arguments': call['function']['arguments'], + }, + if (calls.isEmpty) + { + 'id': 'msg_$id', + 'type': 'message', + 'role': 'assistant', + 'status': truncated ? 'incomplete' : 'completed', + 'content': [ + {'type': 'output_text', 'text': content.toString(), 'annotations': []}, + ], + }, + ], + }; + if (wantsStream) { + var sequence = 1; + Future responseEvent(String type, Map fields) => + emit({'type': type, 'sequence_number': sequence++, ...fields}, type); + final output = response['output'] as List; + for (var i = 0; i < output.length; i++) { + final item = output[i] as Map; + final isCall = item['type'] == 'function_call'; + await responseEvent('response.output_item.added', { + 'output_index': i, + 'item': { + ...item, + 'status': 'in_progress', + if (isCall) 'arguments': '', + if (!isCall) 'content': [], + }, + }); + if (isCall) { + await responseEvent( + 'response.function_call_arguments.delta', + {'item_id': item['id'], 'output_index': i, 'delta': item['arguments']}, + ); + await responseEvent( + 'response.function_call_arguments.done', + {'item_id': item['id'], 'output_index': i, 'arguments': item['arguments']}, + ); + } else { + final part = (item['content'] as List).first; + final fields = {'item_id': item['id'], 'output_index': i, 'content_index': 0}; + await responseEvent('response.content_part.added', { + ...fields, + 'part': {'type': 'output_text', 'text': '', 'annotations': []}, + }); + await responseEvent( + 'response.output_text.delta', + {...fields, 'delta': content.toString()}, + ); + await responseEvent( + 'response.output_text.done', + {...fields, 'text': content.toString()}, + ); + await responseEvent('response.content_part.done', {...fields, 'part': part}); + } + await responseEvent('response.output_item.done', {'output_index': i, 'item': item}); + } + final terminal = truncated ? 'response.incomplete' : 'response.completed'; + await responseEvent(terminal, {'response': response}); + } else { + _json(request.response, response); + } + } else if (wantsStream) { + if (hasTools) { + await emit( + chunk( + calls.isEmpty + ? {'content': content.toString()} + : { + 'tool_calls': [ + for (var i = 0; i < calls.length; i++) {'index': i, ...calls[i]}, + ], + }, + ), + ); + } + await emit(chunk({}, finish)); + await emit('[DONE]'); + } else { + _json(request.response, { + 'id': id, + 'object': 'chat.completion', + 'created': created, + 'model': model, + 'choices': [ + { + 'index': 0, + 'finish_reason': finish, + 'message': { + 'role': 'assistant', + 'content': calls.isEmpty ? content.toString() : null, + if (calls.isNotEmpty) 'tool_calls': calls, + if (reasoning.isNotEmpty) 'reasoning_content': reasoning.toString(), + }, + } + ], + }); + } + completed = true; + } catch (e) { + diagnostic['exception_category'] = e.runtimeType.toString(); + final message = e is FormatException ? e.message : '请求失败,请重试'; + try { + if (streaming) { + if (request.uri.path == '/v1/responses') { + writeSse('event: response.failed\ndata: ${jsonEncode({ + 'type': 'response.failed', + 'response': { + 'object': 'response', + 'status': 'failed', + 'error': {'code': 'upstream_error', 'message': message}, + }, + })}\n\n'); + } else { + writeSse('data: ${jsonEncode({ + 'error': {'message': message, 'type': 'upstream_error'}, + })}\n\ndata: [DONE]\n\n'); + } + } else { + _json( + request.response, + { + if (request.uri.path == '/v1/responses') ...{ + 'id': 'resp_${diagnostic['request_id']}', + 'object': 'response', + 'status': 'failed', + 'output': [], + }, + 'error': {'message': message, 'code': 'upstream_error'}, + }, + e is FormatException ? 400 : 502, + ); + } + } catch (_) {/* Client already disconnected. */} + } finally { + completed = true; + await sse?.close(); + await ssePump; + diagnostic['completion_ms'] = watch.elapsedMilliseconds; + try { + onDiagnostic?.call(Map.unmodifiable(diagnostic)); + } catch (_) {} + closeStream(); + try { + await request.response.close(); + } catch (_) {/* Client disconnected. */} + } + } + + List> _responseMessages(Map data) { + final input = data['input']; + return [ + if (data['instructions'] is String) {'role': 'system', 'content': data['instructions']}, + if (input is String) {'role': 'user', 'content': input}, + if (input is List) + for (final m in input.whereType>()) + if (m['type'] == 'function_call') + { + 'role': 'assistant', + 'tool_calls': [ + { + 'id': m['call_id'], + 'type': 'function', + 'function': {'name': m['name'], 'arguments': m['arguments']}, + } + ], + } + else if (m['type'] == 'function_call_output') + { + 'role': 'tool', + 'tool_call_id': m['call_id'], + 'content': m['output'] is String ? m['output'] : jsonEncode(m['output']), + } + else + { + 'role': m['role'] ?? 'user', + 'content': m['content'] is List + ? (m['content'] as List) + .whereType>() + .map((part) => part['text'] ?? '') + .join('\n') + : m['content'], + }, + ]; + } +} diff --git a/lib/services/genai/genai_login.dart b/lib/services/genai/genai_login.dart new file mode 100644 index 00000000..dac0956e --- /dev/null +++ b/lib/services/genai/genai_login.dart @@ -0,0 +1,118 @@ +// Adapted from GenAI2OpenAI's CAS login. See third_party/GenAI2OpenAI/LICENSE. +import 'dart:convert'; +import 'dart:io'; +import 'dart:typed_data'; + +import 'package:pointycastle/export.dart'; + +class GenaiLogin { + GenaiLogin({HttpClient Function()? clientFactory, Uri? genaiBase, Uri? idsBase}) + : _clientFactory = clientFactory ?? HttpClient.new, + _genaiBase = genaiBase ?? Uri.parse('https://genai.shanghaitech.edu.cn'), + _idsBase = idsBase ?? Uri.parse('https://ids.shanghaitech.edu.cn'); + + final HttpClient Function() _clientFactory; + final Uri _genaiBase; + final Uri _idsBase; + + static String encryptPassword(String password, String salt) { + final cipher = PaddedBlockCipher('AES/CBC/PKCS7') + ..init( + true, + PaddedBlockCipherParameters( + ParametersWithIV( + KeyParameter(Uint8List.fromList(utf8.encode(salt))), + Uint8List.fromList(utf8.encode('Nu1LNu1LNu1LNu1L')), + ), + null, + ), + ); + return base64Encode( + cipher.process(Uint8List.fromList(utf8.encode('${List.filled(16, 'Nu1L').join()}$password'))), + ); + } + + static String field(String html, String name) { + final input = RegExp(r']*>', caseSensitive: false) + .allMatches(html) + .map((m) => m.group(0)!) + .where( + (tag) => RegExp('(?:name|id)=["\u0027]${RegExp.escape(name)}["\u0027]').hasMatch(tag), + ); + if (input.isEmpty) return ''; + return RegExp('value=["\u0027]([^"\u0027]*)["\u0027]').firstMatch(input.first)?.group(1) ?? ''; + } + + Future login(String account, String password) async { + final client = _clientFactory()..connectionTimeout = const Duration(seconds: 15); + final cookies = >{}; + Future<({HttpClientResponse response, String body})> request(Uri uri, {String? form}) async { + final req = await client.openUrl(form == null ? 'GET' : 'POST', uri); + req.followRedirects = false; + req.headers.set(HttpHeaders.userAgentHeader, 'Mozilla/5.0 TechPie'); + req.cookies.addAll(cookies[uri.host]?.values ?? []); + if (form != null) { + req.headers.contentType = + ContentType('application', 'x-www-form-urlencoded', charset: 'utf-8'); + req.write(form); + } + final res = await req.close().timeout(const Duration(seconds: 30)); + for (final cookie in res.cookies) { + (cookies[uri.host] ??= {})[cookie.name] = cookie; + } + return ( + response: res, + body: await utf8.decoder.bind(res).join().timeout(const Duration(seconds: 30)) + ); + } + + try { + var uri = _genaiBase.resolve('/htk/user/login'); + var result = await request(uri); + for (var i = 0; i < 12; i++) { + final token = uri.queryParameters['token']; + if (token != null && token.isNotEmpty) return token; + final location = result.response.headers.value(HttpHeaders.locationHeader); + if (result.response.statusCode >= 300 && + result.response.statusCode < 400 && + location != null) { + uri = uri.resolve(location); + final redirectedToken = uri.queryParameters['token']; + if (redirectedToken != null && redirectedToken.isNotEmpty) return redirectedToken; + result = await request(uri); + continue; + } + final salt = field(result.body, 'pwdEncryptSalt'); + final execution = field(result.body, 'execution'); + if (salt.isNotEmpty && execution.isNotEmpty) { + final form = Uri( + queryParameters: { + 'username': account.trim(), + 'password': encryptPassword(password, salt), + 'lt': field(result.body, 'lt'), + 'dllt': 'generalLogin', + 'execution': execution, + '_eventId': 'submit', + 'rmShown': '1', + }, + ).query; + result = await request(uri, form: form); + if (result.response.statusCode < 300 || result.response.statusCode >= 400) { + if (result.body.contains('captcha') || result.body.contains('验证码')) { + throw const FormatException('请在网页登录后重试'); + } + throw const FormatException('学号或密码不对'); + } + continue; + } + final service = RegExp(r'var service = \["(.*?)"').firstMatch(result.body)?.group(1); + if (service == null) throw const FormatException('登录失败,请重试'); + uri = _idsBase.resolve('/authserver/login?service=$service'); + result = await request(uri); + } + throw const FormatException('登录失败,请重试'); + } finally { + client.close(force: true); + } + } +} diff --git a/lib/services/genai/genai_tools.dart b/lib/services/genai/genai_tools.dart new file mode 100644 index 00000000..1292b2eb --- /dev/null +++ b/lib/services/genai/genai_tools.dart @@ -0,0 +1,173 @@ +import 'dart:convert'; + +bool _isToolObject(dynamic value) => + value is Map && + ((value['function'] is Map && (value['function'] as Map).containsKey('name')) || + (value.containsKey('name') && value.containsKey('arguments'))); + +bool looksLikeToolProtocol(String text) => + text.contains('') || + (RegExp(r'^\s*[\[{]\s*(?:[\[{" ]|$)').hasMatch(text) && + RegExp(r'"tool_calls"\s*:|"function"\s*:\s*\{|"name"\s*:\s*"[^"\n]+"\s*,\s*"arguments"\s*:') + .hasMatch(text)); + +/// Only missing outer array/wrapper closures may be added. Calls, arguments, +/// strings and comma-separated values must already be complete. +dynamic _repairWrapper(String text, List stack, bool quoted) { + final shape = stack.join(); + if (quoted || !['[', '{', '{['].contains(shape)) return null; + try { + final suffix = stack.reversed.map((c) => c == '[' ? ']' : '}').join(); + final value = jsonDecode(text + suffix); + final calls = value is Map ? value['tool_calls'] : value; + if (value is Map && (value.length != 1 || !value.containsKey('tool_calls'))) return null; + if (calls is! List || calls.isEmpty || !calls.every(_isToolObject)) return null; + return value; + } on FormatException { + return null; + } +} + +/// Parses outer containers atomically, never salvaging a truncated inner call. +/// Validates declared names and object arguments, not full JSON Schema. +List> parseGenaiToolCalls(String text, Set allowed, dynamic choice) { + final values = []; + final xml = text.contains(''); + final blocks = xml + ? RegExp(r'\s*([\s\S]*?)\s*') + .allMatches(text) + .map((m) => m[1]!) + .toList() + : [text]; + if (xml && blocks.length != ''.allMatches(text).length) { + throw const FormatException('工具调用未完成'); + } + for (final block in blocks) { + var cursor = 0; + while (cursor < block.length) { + final start = block.indexOf(RegExp(r'[\[{]'), cursor); + if (start < 0) break; + final stack = []; + var quoted = false; + var escaped = false; + var end = -1; + for (var i = start; i < block.length; i++) { + final c = block[i]; + if (quoted) { + if (escaped) { + escaped = false; + } else if (c == '\\') { + escaped = true; + } else if (c == '"') { + quoted = false; + } + continue; + } + if (c == '"') { + quoted = true; + continue; + } + if (c == '{' || c == '[') stack.add(c); + if (c == '}' || c == ']') { + if (stack.isEmpty || (stack.removeLast() == '{' ? c != '}' : c != ']')) break; + if (stack.isEmpty) { + end = i + 1; + break; + } + } + } + dynamic repaired; + if (end < 0) { + if (xml || looksLikeToolProtocol(block.substring(start))) { + repaired = _repairWrapper(block.substring(start), stack, quoted); + if (repaired == null) throw const FormatException('工具调用 JSON 未完成'); + end = block.length; + } else { + break; + } + } + final segment = block.substring(start, end); + try { + final value = repaired ?? jsonDecode(segment); + if (value is Map && value.containsKey('tool_calls')) { + if (value['tool_calls'] is! List) throw const FormatException('tool_calls 必须是数组'); + values.addAll(value['tool_calls'] as List); + } else if (_isToolObject(value) || (xml && value is Map && value.containsKey('name'))) { + values.add(value); + } else if (value is List && value.any(_isToolObject)) { + values.addAll(value); + } else if (xml) { + throw const FormatException('工具调用格式不对'); + } + } on FormatException { + if (xml || looksLikeToolProtocol(segment)) rethrow; + } + cursor = end; + } + } + final calls = >[]; + for (final raw in values) { + if (raw is! Map) throw const FormatException('工具调用必须是对象'); + final function = raw['function'] is Map ? raw['function'] as Map : raw; + final name = function['name']; + if (name is! String || !allowed.contains(name)) throw const FormatException('工具名称不在本次请求中'); + dynamic args = function['arguments'] ?? {}; + if (args is String) args = jsonDecode(args); + if (args is! Map) throw const FormatException('工具参数必须是 JSON 对象'); + calls.add({ + 'id': raw['id'] is String && (raw['id'] as String).isNotEmpty + ? raw['id'] + : 'call_${DateTime.now().microsecondsSinceEpoch}_${calls.length}', + 'type': 'function', + 'function': {'name': name, 'arguments': jsonEncode(args)}, + }); + } + final selected = choice is Map + ? (choice['function'] is Map ? choice['function']['name'] : choice['name']) + : null; + if ((choice == 'required' && calls.isEmpty) || + (selected != null && + (calls.isEmpty || calls.any((c) => c['function']['name'] != selected)))) { + throw const FormatException('GenAI 未返回指定工具调用'); + } + return calls; +} + +/// Aggregate native tool deltas by index before validating their full arguments. +class GenaiToolAccumulator { + final _calls = >{}; + bool get isNotEmpty => _calls.isNotEmpty; + void add(dynamic fragments) { + if (fragments == null) return; + if (fragments is! List) throw const FormatException('工具增量必须是数组'); + for (var position = 0; position < fragments.length; position++) { + final fragment = fragments[position]; + if (fragment is! Map) throw const FormatException('工具增量格式不对'); + final index = fragment['index'] ?? position; + if (index is! int || index < 0) throw const FormatException('工具序号不对'); + final call = _calls.putIfAbsent( + index, + () => { + 'type': 'function', + 'function': {'name': '', 'arguments': ''}, + }, + ); + if (fragment['id'] is String && (fragment['id'] as String).isNotEmpty) { + call['id'] = fragment['id']; + } + final function = fragment['function']; + if (function is! Map) continue; + for (final field in ['name', 'arguments']) { + dynamic value = function[field]; + if (value == null) continue; + if (field == 'arguments' && value is Map) value = jsonEncode(value); + if (value is! String) throw const FormatException('工具增量字段格式不对'); + final target = call['function'] as Map; + target[field] = (target[field] as String) + value; + } + } + } + + List> result() => + [for (final index in (_calls.keys.toList()..sort())) _calls[index]!]; +} diff --git a/lib/services/genai_api_service.dart b/lib/services/genai_api_service.dart new file mode 100644 index 00000000..f5055de5 --- /dev/null +++ b/lib/services/genai_api_service.dart @@ -0,0 +1,161 @@ +import 'dart:async'; +import 'dart:convert'; +import 'dart:io'; +import 'dart:math'; + +import 'package:flutter/foundation.dart'; + +import 'genai/genai_gateway.dart'; +import 'genai/genai_login.dart'; +import 'storage_service.dart'; + +class GenaiApiService extends ChangeNotifier { + GenaiApiService(this.storage, {GenaiGateway? gateway, GenaiLogin? login}) + : _gateway = gateway ?? + GenaiGateway( + onDiagnostic: (metadata) { + // Request metadata only; never log prompts, tool results or credentials. + stdout.writeln('genai_request ${jsonEncode(metadata)}'); + }, + ), + _login = login ?? GenaiLogin(); + + final StorageService storage; + final GenaiGateway _gateway; + final GenaiLogin _login; + Future? _loading; + bool _disposed = false; + bool busy = false; + String? error; + String? _genaiToken; + String _apiToken = ''; + String _host = '127.0.0.1'; + int _port = 5000; + + bool get running => _gateway.running; + bool get loggedIn => (_genaiToken ?? '').isNotEmpty; + String get host => _host; + int get port => _port; + String get apiToken => _apiToken; + String get endpoint => 'http://127.0.0.1:${_gateway.port ?? _port}/v1'; + + void _notify() { + if (!_disposed) notifyListeners(); + } + + Future load() => _loading ??= _load(); + Future _load() async { + try { + _genaiToken = await storage.loadGenaiToken(); + _apiToken = await storage.loadGenaiApiToken() ?? ''; + if (_apiToken.isEmpty) { + final random = Random.secure(); + _apiToken = + 'sk-${List.generate(24, (_) => random.nextInt(256).toRadixString(16).padLeft(2, '0')).join()}'; + await storage.saveGenaiApiToken(_apiToken); + } + _host = storage.genaiApiHost; + _port = storage.genaiApiPort; + } catch (_) { + _loading = null; + error = '读取设置失败,请重试'; + rethrow; + } finally { + _notify(); + } + } + + Future login(String account, String password) async { + if (busy || running) return false; + if (account.trim().isEmpty || password.isEmpty) { + error = '请输入学号和密码'; + _notify(); + return false; + } + busy = true; + error = null; + _notify(); + try { + final token = await _login.login(account, password); + await storage.saveGenaiToken(token); + _genaiToken = token; + return true; + } on FormatException catch (e) { + error = e.message; + return false; + } catch (_) { + error = '登录失败,请重试'; + return false; + } finally { + busy = false; + _notify(); + } + } + + Future logout() async { + if (busy) return; + await stop(); + await storage.clearGenaiToken(); + _genaiToken = null; + error = null; + _notify(); + } + + Future saveSettings({ + required String apiToken, + required String port, + required String host, + }) async { + if (running) return; + final parsed = int.tryParse(port); + if (parsed == null || parsed < 1 || parsed > 65535) { + throw const FormatException('端口填 1–65535'); + } + if (apiToken.trim().isEmpty) throw const FormatException('请输入 API Token'); + final nextHost = host == '0.0.0.0' ? host : '127.0.0.1'; + await storage.saveGenaiApiToken(apiToken.trim()); + await storage.setGenaiApiPort(parsed); + await storage.setGenaiApiHost(nextHost); + _apiToken = apiToken.trim(); + _port = parsed; + _host = nextHost; + _notify(); + } + + Future start() async { + if (busy || running) return running; + busy = true; + error = null; + _notify(); + try { + await load(); + if (!loggedIn) throw const FormatException('请先登录 GenAI'); + await _gateway.start(host: _host, port: _port, genaiToken: _genaiToken!, apiToken: _apiToken); + return true; + } on FormatException catch (e) { + error = e.message; + return false; + } on SocketException catch (_) { + error = '端口已占用,换一个试试'; + return false; + } catch (_) { + error = '启动失败,请重试'; + return false; + } finally { + busy = false; + _notify(); + } + } + + Future stop() async { + await _gateway.stop(); + _notify(); + } + + @override + void dispose() { + _disposed = true; + unawaited(_gateway.stop()); + super.dispose(); + } +} diff --git a/lib/services/service_provider.dart b/lib/services/service_provider.dart index a166d008..7ff92e2f 100644 --- a/lib/services/service_provider.dart +++ b/lib/services/service_provider.dart @@ -7,6 +7,7 @@ import 'debug_logger.dart'; import 'ecard_bind_service.dart'; import 'ecard_widget_service.dart'; import 'egate_app_service.dart'; +import 'genai_api_service.dart'; import 'oa_gym_service.dart'; import 'schedule_service.dart'; import 'storage_service.dart'; @@ -32,6 +33,7 @@ class ServiceProvider extends InheritedWidget { final CampusCardService campusCardService; final EcardBindService ecardBindService; final EcardWidgetService? ecardWidgetService; + final GenaiApiService? genaiApiService; const ServiceProvider({ super.key, @@ -50,6 +52,7 @@ class ServiceProvider extends InheritedWidget { required this.campusCardService, required this.ecardBindService, this.ecardWidgetService, + this.genaiApiService, required super.child, }); @@ -76,5 +79,6 @@ class ServiceProvider extends InheritedWidget { updateService != oldWidget.updateService || campusCardService != oldWidget.campusCardService || ecardBindService != oldWidget.ecardBindService || - ecardWidgetService != oldWidget.ecardWidgetService; + ecardWidgetService != oldWidget.ecardWidgetService || + genaiApiService != oldWidget.genaiApiService; } diff --git a/lib/services/storage_service.dart b/lib/services/storage_service.dart index b42ed5c3..c59f0764 100644 --- a/lib/services/storage_service.dart +++ b/lib/services/storage_service.dart @@ -33,6 +33,8 @@ class StorageService { static const _syncMasterKeyKey = 'sync_master_key'; // secure storage static const _deviceIdKey = 'device_id'; static const _maskPersonalInfoKey = 'mask_personal_info'; + static const _genaiApiPortKey = 'genai_api_port'; + static const _genaiApiHostKey = 'genai_api_host'; final FlutterSecureStorage _secure; final SharedPreferences _prefs; @@ -208,6 +210,16 @@ class StorageService { Future setUseLocalhost(bool value) => _prefs.setBool(_useLocalhostKey, value); + String get genaiApiHost => _prefs.getString(_genaiApiHostKey) ?? '127.0.0.1'; + Future setGenaiApiHost(String value) => _prefs.setString(_genaiApiHostKey, value); + int get genaiApiPort => _prefs.getInt(_genaiApiPortKey) ?? 5000; + Future setGenaiApiPort(int value) => _prefs.setInt(_genaiApiPortKey, value); + Future loadGenaiToken() => _secure.read(key: 'genai_upstream_token'); + Future saveGenaiToken(String value) => _secure.write(key: 'genai_upstream_token', value: value); + Future clearGenaiToken() => _secure.delete(key: 'genai_upstream_token'); + Future loadGenaiApiToken() => _secure.read(key: 'genai_local_api_token'); + Future saveGenaiApiToken(String value) => _secure.write(key: 'genai_local_api_token', value: value); + // Cloud-sync settings. The master-password-derived key is the one sensitive // piece — it lives in secure storage, never in SharedPreferences. bool get syncEnabled => _prefs.getBool(_syncEnabledKey) ?? false; diff --git a/macos/Runner/Release.entitlements b/macos/Runner/Release.entitlements index 85cd58de..ca757edc 100644 --- a/macos/Runner/Release.entitlements +++ b/macos/Runner/Release.entitlements @@ -6,6 +6,8 @@ com.apple.security.network.client + com.apple.security.network.server + keychain-access-groups $(AppIdentifierPrefix)$(CFBundleIdentifier) diff --git a/test/genai_gateway_test.dart b/test/genai_gateway_test.dart new file mode 100644 index 00000000..100d49d3 --- /dev/null +++ b/test/genai_gateway_test.dart @@ -0,0 +1,760 @@ +import 'dart:async'; +import 'dart:convert'; +import 'dart:io'; + +import 'package:flutter_test/flutter_test.dart'; +import 'package:techpie/services/genai/genai_gateway.dart'; +import 'package:techpie/services/genai/genai_login.dart'; +import 'package:techpie/services/genai/genai_tools.dart'; + +void main() { + late HttpServer upstream; + late GenaiGateway gateway; + late HttpClient client; + final received = {}; + String reply = '你好'; + bool businessError = false; + String? finishReason = 'stop'; + bool done = false; + List>>? attemptPackets; + final bodies = []; + final diagnostics = >[]; + Completer? upstreamGate; + + setUp(() async { + upstream = await HttpServer.bind(InternetAddress.loopbackIPv4, 0); + received.clear(); + reply = '你好'; + businessError = false; + finishReason = 'stop'; + done = false; + diagnostics.clear(); + attemptPackets = null; + bodies.clear(); + upstreamGate = null; + upstream.listen((request) async { + received['token'] = request.headers.value('x-access-token'); + if (request.uri.path.endsWith('/list')) { + request.response.headers.contentType = ContentType.json; + request.response.write( + jsonEncode({ + 'success': true, + 'result': { + 'records': [ + {'aiType': 'test-model', 'rootAiType': 'azure'}, + {'aiType': 'test-model'}, + ], + }, + }), + ); + } else { + await upstreamGate?.future; + received['body'] = jsonDecode(await utf8.decoder.bind(request).join()); + bodies.add(received['body']); + request.response.headers.contentType = + ContentType('text', 'event-stream', charset: 'utf-8'); + if (attemptPackets != null) { + final index = bodies.length - 1; + final packets = + attemptPackets![index < attemptPackets!.length ? index : attemptPackets!.length - 1]; + for (final packet in packets) { + request.response.write('data: ${jsonEncode(packet)}\n\n'); + } + } else if (businessError) { + request.response + .write('data: ${jsonEncode({'success': false, 'message': 'Token失效'})}\n\n'); + } else { + // The final text shares an event with finish_reason. + request.response.write('data: ${jsonEncode({ + 'choices': [ + { + 'delta': {'content': reply, 'reasoning': '思考'}, + 'finish_reason': finishReason, + } + ], + })}\n\n'); + } + } + if (done) request.response.write('data: [DONE]\n\n'); + await request.response.close(); + }); + gateway = GenaiGateway( + onDiagnostic: diagnostics.add, + upstream: Uri.parse('http://127.0.0.1:${upstream.port}'), + heartbeatInterval: const Duration(milliseconds: 30), + ); + await gateway.start( + host: '127.0.0.1', + port: 0, + genaiToken: 'upstream-token', + apiToken: 'local-token', + ); + client = HttpClient(); + }); + tearDown(() async { + client.close(force: true); + await gateway.stop(); + await upstream.close(force: true); + }); + + Future<({int status, String body})> request( + String path, { + Object? body, + String? token = 'local-token', + }) async { + final req = await client.openUrl( + body == null ? 'GET' : 'POST', + Uri.parse('http://127.0.0.1:${gateway.port}$path'), + ); + if (token != null) req.headers.set('Authorization', 'Bearer $token'); + if (body != null) { + req.headers.contentType = ContentType.json; + req.write(jsonEncode(body)); + } + final res = await req.close(); + return (status: res.statusCode, body: await utf8.decoder.bind(res).join()); + } + + test('local token is separate from the upstream token', () async { + expect((await request('/v1/models', token: null)).status, 401); + expect((await request('/v1/models', token: 'upstream-token')).status, 401); + final res = await request('/v1/models'); + expect(res.status, 200); + expect((jsonDecode(res.body)['data'] as List).length, 1); + expect(received['token'], 'upstream-token'); + }); + + test('nonstream reply preserves final text and model routing', () async { + await request('/v1/models'); + final res = await request( + '/v1/chat/completions', + body: { + 'model': 'test-model', + 'messages': [ + {'role': 'user', 'content': 'hi'}, + ], + }, + ); + expect(res.status, 200); + expect(jsonDecode(res.body)['choices'][0]['message']['content'], '你好'); + expect(received['body']['rootAiType'], 'azure'); + expect(received['token'], 'upstream-token'); + }); + + test('SSE contains text, reasoning and completion', () async { + final res = await request( + '/v1/chat/completions', + body: { + 'model': 'test-model', + 'stream': true, + 'messages': [ + {'role': 'user', 'content': 'hi'}, + ], + }, + ); + expect(res.status, 200); + expect(res.body, contains('你好')); + expect(res.body, contains('reasoning_content')); + expect(res.body, contains('[DONE]')); + }); + + test('tool JSON becomes tool_calls', () async { + reply = '{"tool_calls":[{"name":"weather","arguments":{"city":"上海"}}]}'; + final res = await request( + '/v1/chat/completions', + body: { + 'model': 'test-model', + 'messages': [ + {'role': 'user', 'content': '天气'}, + ], + 'tools': [ + { + 'type': 'function', + 'function': {'name': 'weather'}, + } + ], + }, + ); + final choice = jsonDecode(res.body)['choices'][0]; + expect(choice['finish_reason'], 'tool_calls'); + expect(choice['message']['tool_calls'][0]['function']['arguments'], '{"city":"上海"}'); + }); + + test('Responses endpoint accepts instructions and input', () async { + final res = await request( + '/v1/responses', + body: {'model': 'test-model', 'input': 'hi', 'instructions': 'test'}, + ); + expect(jsonDecode(res.body)['output_text'], '你好'); + expect(received['body']['messages'][0]['role'], 'system'); + }); + + test('business errors are returned instead of empty replies', () async { + businessError = true; + final res = await request( + '/v1/chat/completions', + body: { + 'model': 'test-model', + 'messages': [ + {'role': 'user', 'content': 'hi'}, + ], + }, + ); + expect(res.status, isNot(200)); + expect(res.body, contains('Token失效')); + }); + + test('server releases the port when stopped', () async { + final port = gateway.port!; + await gateway.stop(); + final replacement = await HttpServer.bind('127.0.0.1', port); + await replacement.close(force: true); + expect(gateway.running, false); + }); + + test('CAS fields accept either attribute order and quote style', () { + expect(GenaiLogin.field('', 'pwdEncryptSalt'), 'salt'); + expect(GenaiLogin.field("", 'execution'), 'e1s1'); + }); + test('CAS password encryption matches GenAI2OpenAI', () { + expect( + GenaiLogin.encryptPassword('test-password', '1234567890123456'), + '2LWYSMdnDJSym1TSN54ueiM+MTqBzvtIizRb62ttRF7LHyqJnnRF7TSKdRbojVUw0FcpVIctqIMgcad48Yo0iXPW9Ddd5+5IsUJRylmMEbw=', + ); + }); + + test('CAS login follows cookies and extracts token from redirect', () async { + final ids = await HttpServer.bind('127.0.0.1', 0); + addTearDown(() => ids.close(force: true)); + ids.listen((req) async { + if (req.uri.path == '/htk/user/login') { + req.response.statusCode = 302; + req.response.headers.set('location', '/authserver/login'); + } else if (req.method == 'GET') { + req.response.cookies.add(Cookie('session', 'fixture')); + req.response.headers.contentType = ContentType.html; + req.response.write('' + ''); + } else { + expect(req.cookies.single.value, 'fixture'); + final form = Uri.splitQueryString(await utf8.decoder.bind(req).join()); + expect(form['username'], 'test-student'); + expect(form['password'], GenaiLogin.encryptPassword('test-password', '1234567890123456')); + req.response.statusCode = HttpStatus.found; + req.response.headers.set('location', '/dialogue?token=fixture-token'); + } + await req.response.close(); + }); + final login = GenaiLogin(genaiBase: Uri.parse('http://127.0.0.1:${ids.port}')); + expect(await login.login('test-student', 'test-password'), 'fixture-token'); + }); + test('tool SSE sends heartbeats before upstream headers arrive', () async { + upstreamGate = Completer(); + final req = + await client.postUrl(Uri.parse('http://127.0.0.1:${gateway.port}/v1/chat/completions')); + req.headers.set('Authorization', 'Bearer local-token'); + req.headers.contentType = ContentType.json; + req.write( + jsonEncode({ + 'model': 'test-model', + 'stream': true, + 'messages': [ + {'role': 'user', 'content': 'hi'}, + ], + 'tools': [ + { + 'type': 'function', + 'function': {'name': 'weather'}, + } + ], + }), + ); + final res = await req.close().timeout(const Duration(seconds: 2)); + expect(res.statusCode, 200); + final lines = StreamIterator(res.transform(utf8.decoder).transform(const LineSplitter())); + try { + expect(await lines.moveNext().timeout(const Duration(seconds: 2)), true); + expect(lines.current, ': keep-alive'); + expect(upstreamGate!.isCompleted, false); + await lines.moveNext(); // Empty separator. + expect(await lines.moveNext().timeout(const Duration(seconds: 2)), true); + expect(lines.current, ': keep-alive'); + upstreamGate!.complete(); + final text = StringBuffer(); + while (await lines.moveNext()) { + text.writeln(lines.current); + } + expect(text.toString(), contains('[DONE]')); + expect(text.toString(), contains('你好')); + } finally { + if (!upstreamGate!.isCompleted) upstreamGate!.complete(); + await lines.cancel(); + } + }); + + test('empty tool streams return an error and terminate', () async { + reply = ''; + final res = await request( + '/v1/chat/completions', + body: { + 'model': 'test-model', + 'stream': true, + 'messages': [ + {'role': 'user', 'content': 'hi'}, + ], + 'tools': [ + { + 'type': 'function', + 'function': {'name': 'weather'}, + } + ], + }, + ); + expect(res.body, contains('upstream_error')); + expect(res.body, contains('[DONE]')); + expect(res.body, isNot(contains('"finish_reason":"stop"'))); + }); + final tool = { + 'type': 'function', + 'function': {'name': 'weather'}, + }; + Map payload({bool stream = false, bool tools = false}) => { + 'model': 'test-model', + 'stream': stream, + 'messages': [ + {'role': 'user', 'content': 'hi'}, + ], + if (tools) 'tools': [tool], + }; + for (final stream in [false, true]) { + for (final reason in ['length', 'content_filter']) { + test('$reason is preserved, stream=$stream', () async { + finishReason = reason; + final res = await request('/v1/chat/completions', body: payload(stream: stream)); + expect(res.body, contains('"finish_reason":"$reason"')); + expect(res.body, contains('你好')); + }); + } + test('EOF is an error, stream=$stream', () async { + finishReason = null; + final res = await request('/v1/chat/completions', body: payload(stream: stream)); + expect(res.body, contains('回复中断')); + expect(res.body, isNot(contains('"finish_reason":"stop"'))); + }); + test('explicit DONE succeeds, stream=$stream', () async { + finishReason = null; + done = true; + final res = await request('/v1/chat/completions', body: payload(stream: stream)); + expect(res.body, contains('"finish_reason":"stop"')); + expect(diagnostics.last['saw_done'], true); + }); + test('truncated tool is never executed, stream=$stream', () async { + finishReason = 'length'; + reply = '{"name":"weather","arguments":{}}'; + final res = await request('/v1/chat/completions', body: payload(stream: stream, tools: true)); + expect(res.body, contains('工具调用未完成')); + expect(res.body, isNot(contains('"finish_reason":"tool_calls"'))); + }); + test('multiple tools survive HTTP, stream=$stream', () async { + reply = + '[{"function":{"name":"weather","arguments":"{}"}},{"name":"weather","arguments":{}}] https://example.org'; + final res = await request('/v1/chat/completions', body: payload(stream: stream, tools: true)); + expect(res.body, contains('"finish_reason":"tool_calls"')); + expect(diagnostics.last['call_count'], 2); + }); + test('invalid tools fail HTTP, stream=$stream', () async { + reply = '[{"name":"unknown","arguments":{}}]'; + final res = await request('/v1/chat/completions', body: payload(stream: stream, tools: true)); + expect(res.body, contains('工具名称不在本次请求中')); + }); + test('Responses tools native lifecycle, stream=$stream', () async { + reply = '{"function":{"name":"weather","arguments":"{}"}}'; + final res = await request( + '/v1/responses', + body: { + 'model': 'test-model', + 'stream': stream, + 'input': '天气', + 'tools': [ + { + 'type': 'function', + 'name': 'weather', + 'parameters': {'type': 'object'}, + } + ], + }, + ); + expect(res.body, contains('"type":"function_call"')); + expect(res.body, isNot(contains('"type":"message"'))); + if (stream) { + expect(res.body, contains('response.function_call_arguments.delta')); + expect(res.body, contains('response.output_item.done')); + expect(res.body, contains('response.completed')); + } + }); + } + final goodCalls = [ + '{"tool_calls":[{"name":"weather","arguments":{}}]}', + '[{"id":"call_x","type":"function","function":{"name":"weather","arguments":"{}"}}] link', + '{"function":{"name":"weather","arguments":{}}}', + '{"name":"weather"}{"name":"weather"}', + '{"name":"weather","arguments":{}} {"name":"weather","arguments":{}}', + ]; + for (var i = 0; i < goodCalls.length; i++) { + test('parser supported format $i', () { + final calls = parseGenaiToolCalls(goodCalls[i], {'weather'}, null); + expect(calls.length, i >= 3 ? 2 : 1); + expect(calls.first['function']['arguments'], '{}'); + if (i == 1) expect(calls.first['id'], 'call_x'); + }); + } + final badCalls = [ + '{"tool_calls":[{"name":"weather"},', + '{"name":"weather"}', + '{"name":"unknown","arguments":{}}', + '{"name":"weather","arguments":"oops"}', + '{"name":"weather","arguments":[]}', + '{"name":"weather","arguments":"[]"}', + '{"tool_calls":"oops"}', + '[{"name":"weather","arguments":{}},4]', + ]; + for (var i = 0; i < badCalls.length; i++) { + test('parser rejects invalid format $i', () { + expect(() => parseGenaiToolCalls(badCalls[i], {'weather'}, null), throwsFormatException); + }); + } + test('required and selected tools are enforced; final text stays valid', () async { + expect(parseGenaiToolCalls('已完成', {'weather'}, null), isEmpty); + for (final choice in [ + 'required', + { + 'type': 'function', + 'function': {'name': 'weather'}, + } + ]) { + final res = await request( + '/v1/chat/completions', + body: {...payload(tools: true), 'tool_choice': choice}, + ); + expect(res.body, contains('未返回指定工具调用')); + } + }); + test('completion limit precedence and diagnostics contain metadata only', () async { + await request( + '/v1/chat/completions', + body: {...payload(), 'max_completion_tokens': 321, 'max_tokens': 12}, + ); + expect(received['body']['maxToken'], 321); + expect(diagnostics.last['output_limit'], 321); + expect(jsonEncode(diagnostics), isNot(contains('你好'))); + expect(jsonEncode(diagnostics), isNot(contains('upstream-token'))); + }); + test('Responses history preserves call IDs, arguments, output and assistant content', () async { + final res = await request( + '/v1/responses', + body: { + 'model': 'test-model', + 'input': [ + { + 'type': 'message', + 'role': 'user', + 'content': [ + {'type': 'input_text', 'text': '天气'}, + ], + }, + {'type': 'function_call', 'call_id': 'call_x', 'name': 'weather', 'arguments': '{}'}, + {'type': 'function_call_output', 'call_id': 'call_x', 'output': '夹具:晴'}, + ], + 'max_output_tokens': 77, + }, + ); + expect(jsonDecode(res.body)['output_text'], '你好'); + expect(jsonEncode(received['body']['messages']), contains('call_x')); + expect(jsonEncode(received['body']['messages']), contains('夹具:晴')); + expect(received['body']['maxToken'], 77); + await request( + '/v1/chat/completions', + body: { + ...payload(), + 'messages': [ + { + 'role': 'assistant', + 'content': '先看天气', + 'tool_calls': [ + { + 'id': 'call_x', + 'function': {'name': 'weather', 'arguments': '{}'}, + } + ], + }, + {'role': 'tool', 'tool_call_id': 'call_x', 'content': '晴'}, + ], + }, + ); + expect(received['body']['messages'][0]['content'], contains('先看天气')); + }); + test('Responses length is incomplete and EOF streaming is failed', () async { + finishReason = 'length'; + var res = await request('/v1/responses', body: {'model': 'test-model', 'input': 'hi'}); + expect(jsonDecode(res.body)['status'], 'incomplete'); + finishReason = null; + res = await request( + '/v1/responses', + body: {'model': 'test-model', 'input': 'hi', 'stream': true}, + ); + expect(res.body, contains('response.failed')); + expect(res.body, isNot(contains('response.completed'))); + }); + test('client cancellation closes the pending upstream request', () async { + upstreamGate = Completer(); + final req = + await client.postUrl(Uri.parse('http://127.0.0.1:${gateway.port}/v1/chat/completions')); + req.headers.set('Authorization', 'Bearer local-token'); + req.headers.contentType = ContentType.json; + req.write(jsonEncode(payload(stream: true))); + final res = await req.close(); + final subscription = res.listen((_) {}); + await Future.delayed(const Duration(milliseconds: 50)); + await subscription.cancel(); + client.close(force: true); + try { + for (var i = 0; i < 100 && diagnostics.isEmpty; i++) { + await Future.delayed(const Duration(milliseconds: 20)); + } + expect(diagnostics, isNotEmpty); + expect(diagnostics.last['cancelled'], true); + } finally { + upstreamGate!.complete(); + await subscription.cancel(); + } + }); + test('Responses text lifecycle orders added, delta, done, completed', () async { + final res = await request( + '/v1/responses', + body: {'model': 'test-model', 'input': 'hi', 'stream': true}, + ); + final events = const LineSplitter() + .convert(res.body) + .where((l) => l.startsWith('data:')) + .map((l) => jsonDecode(l.substring(5))) + .toList(); + expect(events.map((e) => e['type']).toList(), [ + 'response.created', + 'response.output_item.added', + 'response.content_part.added', + 'response.output_text.delta', + 'response.output_text.done', + 'response.content_part.done', + 'response.output_item.done', + 'response.completed', + ]); + expect(events.map((e) => e['sequence_number']).toList(), List.generate(8, (i) => i)); + finishReason = 'length'; + final incomplete = await request( + '/v1/responses', + body: {'model': 'test-model', 'input': 'hi', 'stream': true}, + ); + expect(incomplete.body, contains('response.incomplete')); + expect(incomplete.body, isNot(contains('response.completed'))); + }); + test('Responses EOF nonstream has failed status', () async { + finishReason = null; + final res = await request('/v1/responses', body: {'model': 'test-model', 'input': 'hi'}); + expect(jsonDecode(res.body)['status'], 'failed'); + }); + test('only complete calls with missing outer wrappers are repaired', () { + const full = '{"tool_calls":[{"id":"call_x","function":{"name":"weather","arguments":"{}"}}]}'; + for (final text in [ + full.substring(0, full.length - 1), + full.substring(0, full.length - 2), + '[{"name":"weather","arguments":{}}', + ]) { + expect(parseGenaiToolCalls(text, {'weather'}, null).length, 1); + } + for (final text in [ + full.substring(0, full.length - 3), + '${full.substring(0, full.length - 2)},', + '{"tool_calls":[{"name":"weather","arguments":{"path":"abc', + '[{"name":"weather","arguments":{"path":"abc"}', + '${full.substring(0, full.length - 1)},"other":[', + ]) { + expect(() => parseGenaiToolCalls(text, {'weather'}, null), throwsFormatException); + } + }); + test('normal JSON and field explanations are final answers', () { + for (final text in [ + '已完成,字段为 `"name": "demo"`。', + '```json\n{"name":"demo","version":"1.0"}\n```', + '检查结果:{"ok":true}\n字段示例包含 "name": "weather"。', + '{"tool_calls":[]}', + '{"function":"documentation","description":"API 示例"}', + '[{"name":"demo","version":"1.0"}]', + ]) { + expect(parseGenaiToolCalls(text, {'weather'}, null), isEmpty); + } + }); + Map packet(Map delta, [String? finish]) => { + 'choices': [ + {'delta': delta, 'finish_reason': finish}, + ], + }; + for (final stream in [false, true]) { + test('empty generation retries once with identical history, stream=$stream', () async { + attemptPackets = [ + [ + packet({'reasoning': 'discard this thought'}, 'stop'), + ], + [ + packet({'content': 'recovered'}, 'stop'), + ] + ]; + final res = await request('/v1/chat/completions', body: payload(stream: stream)); + expect(res.body, contains('recovered')); + expect(res.body, isNot(contains('discard this thought'))); + expect(bodies.length, 2); + expect(bodies[0], bodies[1]); + expect(diagnostics.last['attempts'], 2); + }); + test('two empty generations stop, stream=$stream', () async { + reply = ''; + final res = await request('/v1/chat/completions', body: payload(stream: stream, tools: true)); + expect(res.body, contains('没有回复')); + expect(bodies.length, 2); + }); + test('errors, EOF, length and partial output never retry, stream=$stream', () async { + for (final scenario in ['error', 'eof', 'length', 'partial']) { + bodies.clear(); + attemptPackets = [ + [ + if (scenario == 'error') + { + 'error': 'fixture failure', + } + else + packet( + {'content': scenario == 'partial' ? 'partial' : ''}, + scenario == 'length' ? 'length' : null, + ), + ] + ]; + await request('/v1/chat/completions', body: payload(stream: stream)); + expect(bodies.length, 1, reason: scenario); + } + }); + test('native tool fragments accumulate and do not retry, stream=$stream', () async { + attemptPackets = [ + [ + packet({ + 'tool_calls': [ + { + 'index': 0, + 'id': 'call_native', + 'function': {'name': 'wea', 'arguments': '{"city":'}, + } + ], + }), + packet( + { + 'tool_calls': [ + { + 'index': 0, + 'function': {'name': 'ther', 'arguments': '"上海"}'}, + } + ], + }, + 'tool_calls', + ), + ] + ]; + final res = await request('/v1/chat/completions', body: payload(stream: stream, tools: true)); + expect(res.body, contains('call_native')); + expect(res.body, contains('weather')); + expect(res.body, contains('上海')); + expect(bodies.length, 1); + expect(diagnostics.last['call_count'], 1); + }); + test('native calls with disabled tools or bad arguments fail, stream=$stream', () async { + for (final tools in [false, true]) { + attemptPackets = [ + [ + packet( + { + 'tool_calls': [ + { + 'function': {'name': 'weather', 'arguments': '[]'}, + } + ], + }, + 'tool_calls', + ), + ] + ]; + final res = + await request('/v1/chat/completions', body: payload(stream: stream, tools: tools)); + expect(res.body, contains('error')); + expect(res.body, isNot(contains('"finish_reason":"tool_calls"'))); + } + }); + } + test('native message fallback and legacy function call', () async { + for (final legacy in [false, true]) { + attemptPackets = [ + [ + { + 'choices': [ + { + 'message': legacy + ? { + 'function_call': {'name': 'weather', 'arguments': '{}'}, + } + : { + 'tool_calls': [ + { + 'id': 'call_message', + 'function': {'name': 'weather', 'arguments': {}}, + } + ], + }, + 'finish_reason': legacy ? 'function_call' : 'tool_calls', + } + ], + } + ] + ]; + final res = await request('/v1/chat/completions', body: payload(tools: true)); + expect(jsonDecode(res.body)['choices'][0]['finish_reason'], 'tool_calls'); + } + }); + test('Responses supports native calls after empty retry', () async { + attemptPackets = [ + [packet({}, 'stop')], + [ + packet( + { + 'tool_calls': [ + { + 'id': 'call_r', + 'function': {'name': 'weather', 'arguments': '{}'}, + } + ], + }, + 'tool_calls', + ), + ] + ]; + final res = await request( + '/v1/responses', + body: { + 'model': 'test-model', + 'input': 'hi', + 'stream': true, + 'tools': [ + {'type': 'function', 'name': 'weather'}, + ], + }, + ); + expect(res.body, contains('"type":"function_call"')); + expect(res.body, contains('call_r')); + expect(bodies.length, 2); + }); +} diff --git a/third_party/GenAI2OpenAI/LICENSE b/third_party/GenAI2OpenAI/LICENSE new file mode 100644 index 00000000..708ceb52 --- /dev/null +++ b/third_party/GenAI2OpenAI/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2025 GeekPie_ Association @ ShanghaiTech University + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/tool/genai_gateway_smoke.dart b/tool/genai_gateway_smoke.dart new file mode 100644 index 00000000..c2d7e12c --- /dev/null +++ b/tool/genai_gateway_smoke.dart @@ -0,0 +1,157 @@ +// Supply credentials as one JSON line on stdin; nothing sensitive is printed. +// Performs fixture calls only: no filesystem or command execution tools. +import 'dart:convert'; +import 'dart:io'; +import 'package:techpie/services/genai/genai_gateway.dart'; + +Future main(List args) async { + final credentials = + jsonDecode(await stdin.transform(utf8.decoder).transform(const LineSplitter()).first) + as Map; + final gateway = GenaiGateway(onDiagnostic: (d) => stdout.writeln(jsonEncode(d))); + final client = HttpClient()..connectionTimeout = const Duration(seconds: 15); + final existing = args.contains('--installed'); + try { + if (!existing) { + await gateway.start( + host: '127.0.0.1', + port: 0, + genaiToken: credentials['upstream'] as String, + apiToken: credentials['local'] as String, + ); + } + final base = 'http://127.0.0.1:${existing ? 5000 : gateway.port}'; + Future send(String path, [Map? body]) async { + final req = await client.openUrl(body == null ? 'GET' : 'POST', Uri.parse('$base$path')); + req.headers.set('Authorization', 'Bearer ${credentials['local']}'); + if (body != null) { + req.headers.contentType = ContentType.json; + req.write(jsonEncode(body)); + } + final res = await req.close(); + final text = await utf8.decoder.bind(res).join().timeout(const Duration(seconds: 180)); + if (res.statusCode != 200) throw StateError('HTTP ${res.statusCode}'); + if (body?['stream'] == true) { + final events = const LineSplitter() + .convert(text) + .where((l) => l.startsWith('data:') && !l.contains('[DONE]')) + .map((l) => jsonDecode(l.substring(5))) + .toList(); + if (events.any((e) => e['error'] != null || e['type'] == 'response.failed')) { + throw StateError('SSE error'); + } + if (path.endsWith('responses')) return events.last['response']; + final calls = []; + final content = StringBuffer(); + String? finish; + for (final e in events) { + final c = e['choices'][0]; + final d = c['delta']; + if (d['tool_calls'] is List) calls.addAll(d['tool_calls'] as List); + if (d['content'] is String) content.write(d['content']); + finish = c['finish_reason'] as String? ?? finish; + } + return { + 'choices': [ + { + 'finish_reason': finish, + 'message': { + 'role': 'assistant', + 'content': content.toString(), + if (calls.isNotEmpty) 'tool_calls': calls, + }, + } + ], + }; + } + return jsonDecode(text); + } + + final models = await send('/v1/models'); + final model = (models['data'] as List) + .map((m) => m['id'] as String) + .firstWhere((m) => m.toLowerCase().contains('chatglm')); + for (final responses in [false, true]) { + final path = responses ? '/v1/responses' : '/v1/chat/completions'; + final tools = [ + responses + ? { + 'type': 'function', + 'name': 'fixture_read', + 'description': '读取测试夹具编号', + 'parameters': {'type': 'object', 'properties': {}}, + } + : { + 'type': 'function', + 'function': { + 'name': 'fixture_read', + 'description': '读取测试夹具编号', + 'parameters': {'type': 'object', 'properties': {}}, + }, + }, + ]; + final prompt = '调用 fixture_read 获取测试编号。收到结果后用一句话原样报告编号。工具只读取测试夹具。'; + final initial = {'role': 'user', 'content': prompt}; + final first = await send(path, { + 'model': model, + 'stream': true, + 'tools': tools, + 'tool_choice': 'required', + if (responses) 'input': prompt else 'messages': [initial], + if (responses) 'max_output_tokens': 1024 else 'max_completion_tokens': 1024, + }); + dynamic call; + if (responses) { + if (first['status'] != 'completed') throw StateError('incomplete response'); + call = (first['output'] as List).singleWhere((x) => x['type'] == 'function_call'); + if (call['name'] != 'fixture_read') throw StateError('unexpected tool'); + } else { + if (first['choices'][0]['finish_reason'] != 'tool_calls') { + throw StateError('missing tool finish'); + } + call = first['choices'][0]['message']['tool_calls'][0]; + if (call['function']['name'] != 'fixture_read') throw StateError('unexpected tool'); + } + final second = await send(path, { + 'model': model, + 'stream': false, + 'tools': tools, + 'tool_choice': 'auto', + if (responses) + 'input': [ + initial, + call, + { + 'type': 'function_call_output', + 'call_id': call['call_id'], + 'output': '测试编号:FIXTURE-731', + } + ] + else + 'messages': [ + initial, + first['choices'][0]['message'], + { + 'role': 'tool', + 'tool_call_id': call['id'], + 'name': 'fixture_read', + 'content': '测试编号:FIXTURE-731', + } + ], + if (responses) 'max_output_tokens': 1024 else 'max_completion_tokens': 1024, + }); + final answer = responses ? second['output_text'] : second['choices'][0]['message']['content']; + if (answer is! String || !answer.contains('FIXTURE-731')) { + throw StateError('fixture summary missing'); + } + stdout.writeln( + jsonEncode( + {'api': path, 'model': model, 'fixture_round_trip': 'passed', 'installed': existing}, + ), + ); + } + } finally { + client.close(force: true); + await gateway.stop(); + } +} From 748d472ec2820a81088e095a8514c7612f539677 Mon Sep 17 00:00:00 2001 From: jdz Date: Wed, 28 Oct 2026 23:47:06 +0800 Subject: [PATCH 2/3] =?UTF-8?q?feat(genai):=20=E4=B8=A4=E5=B0=8F=E6=97=B6?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E7=BB=AD=E6=9C=9F=E5=B9=B6=E7=83=AD=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E4=B8=8A=E6=B8=B8=20Token?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/genai-gateway-validation.md | 10 + lib/main.dart | 1 + lib/pages/genai_api_page.dart | 1 + lib/services/genai/genai_gateway.dart | 8 +- lib/services/genai/genai_session.dart | 42 +++ lib/services/genai_api_service.dart | 168 ++++++++++-- lib/services/storage_service.dart | 24 +- test/genai_api_service_test.dart | 361 ++++++++++++++++++++++++++ 8 files changed, 589 insertions(+), 26 deletions(-) create mode 100644 lib/services/genai/genai_session.dart create mode 100644 test/genai_api_service_test.dart diff --git a/docs/genai-gateway-validation.md b/docs/genai-gateway-validation.md index 4e0ed3aa..f6d64f83 100644 --- a/docs/genai-gateway-validation.md +++ b/docs/genai-gateway-validation.md @@ -14,6 +14,16 @@ - SSE 在等待上游响应头时发送心跳,客户端断开时取消等待并关闭对应上游连接。 - 请求日志前缀 `genai_request`。只记录请求 ID、路径、模型、计数、tool_choice、输出上限、耗时、结束原因、DONE、取消状态、异常类别,不记录凭据、正文或工具结果。`saw_done=false` 表示本次没有读到 DONE;已有 finish_reason 时也正常结束读取。 +## Token 自动续期 + +- 登录后将账号、密码、上游 Token 和到期时间一起保存到系统安全存储,有效期为 2 小时。 +- 应用运行时到期自动登录;启动应用时恢复计时。休眠恢复后的请求也会先检查有效期。 +- 新 Token 写入本地记录并更新运行中的网关,端口、本地 API Token 和已有流式响应保持不变。 +- 并发请求共用一次刷新;自动登录失败后 1 分钟重试,退出登录会清除账号密码并取消续期。 +- 旧版记录没有账号密码和到期时间,需要重新登录一次。 + +本次本地验证:自动续期与 HTTP 服务测试 13 项,原有网关测试 62 项;本分支完整 Flutter 测试 728 项通过,静态检查通过。 + ## 拆分前的整包验证 以下记录来自 PR #135 的本机安装与整包测试,包含 Linux DDL、手表等模块。本 PR 仅保留 GenAI 网关、功能页和对应测试。 diff --git a/lib/main.dart b/lib/main.dart index b24c190e..fbc8976e 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -111,6 +111,7 @@ Future _realMain(SharedPreferences prefs, {bool startGenaiApi = false}) as // instead of the logging one the API services share. final updateService = UpdateService(); final genaiApiService = GenaiApiService(storageService); + unawaited(genaiApiService.initialize()); if (startGenaiApi) unawaited(genaiApiService.start()); authService.onLogout = () async { diff --git a/lib/pages/genai_api_page.dart b/lib/pages/genai_api_page.dart index ae0fb170..7a8b3b51 100644 --- a/lib/pages/genai_api_page.dart +++ b/lib/pages/genai_api_page.dart @@ -152,6 +152,7 @@ class _GenaiApiPageState extends State { leading: Icon( service.loggedIn ? Icons.check_circle_outline : Icons.person_outline,), title: Text(service.loggedIn ? '已登录 GenAI' : '登录 GenAI'), + subtitle: const Text('保存账号密码,每 2 小时自动登录'), trailing: editable ? TextButton( onPressed: () => unawaited( diff --git a/lib/services/genai/genai_gateway.dart b/lib/services/genai/genai_gateway.dart index d0933708..6412e668 100644 --- a/lib/services/genai/genai_gateway.dart +++ b/lib/services/genai/genai_gateway.dart @@ -11,11 +11,13 @@ class GenaiGateway { Uri? upstream, HttpClient Function()? clientFactory, this.onDiagnostic, + this.tokenProvider, this.heartbeatInterval = const Duration(seconds: 5), }) : upstream = upstream ?? Uri.parse('https://genai.shanghaitech.edu.cn'), _clientFactory = clientFactory ?? HttpClient.new; final void Function(Map)? onDiagnostic; + Future Function()? tokenProvider; final Uri upstream; final HttpClient Function() _clientFactory; final Duration heartbeatInterval; @@ -48,6 +50,8 @@ class GenaiGateway { } } + void updateGenaiToken(String token) => _genaiToken = token; + Future stop() async { final server = _server; _server = null; @@ -70,9 +74,11 @@ class GenaiGateway { } Future _upstream(String path, {Object? body, HttpClient? client}) async { + final provider = tokenProvider; + final token = provider == null ? _genaiToken : await provider(); final uri = upstream.resolve(path); final request = await (client ?? _client!).openUrl(body == null ? 'GET' : 'POST', uri); - request.headers.set('X-Access-Token', _genaiToken); + request.headers.set('X-Access-Token', token); request.headers.set('Origin', 'https://genai.shanghaitech.edu.cn'); request.headers.set('Referer', 'https://genai.shanghaitech.edu.cn/dialogue'); request.headers.set(HttpHeaders.acceptHeader, 'application/json, text/event-stream'); diff --git a/lib/services/genai/genai_session.dart b/lib/services/genai/genai_session.dart new file mode 100644 index 00000000..3f248ad2 --- /dev/null +++ b/lib/services/genai/genai_session.dart @@ -0,0 +1,42 @@ +class GenaiSession { + const GenaiSession({ + required this.account, + required this.password, + required this.token, + required this.expiresAt, + }); + + final String account; + final String password; + final String token; + final DateTime expiresAt; + + Map toJson() => { + 'account': account, + 'password': password, + 'token': token, + 'expiresAt': expiresAt.millisecondsSinceEpoch, + }; + + factory GenaiSession.fromJson(Map json) { + final account = json['account']; + final password = json['password']; + final token = json['token']; + final expiresAt = json['expiresAt']; + if (account is! String || + account.isEmpty || + password is! String || + password.isEmpty || + token is! String || + token.isEmpty || + expiresAt is! int) { + throw const FormatException('请重新登录 GenAI'); + } + return GenaiSession( + account: account, + password: password, + token: token, + expiresAt: DateTime.fromMillisecondsSinceEpoch(expiresAt, isUtc: true), + ); + } +} diff --git a/lib/services/genai_api_service.dart b/lib/services/genai_api_service.dart index f5055de5..97f3380b 100644 --- a/lib/services/genai_api_service.dart +++ b/lib/services/genai_api_service.dart @@ -7,33 +7,48 @@ import 'package:flutter/foundation.dart'; import 'genai/genai_gateway.dart'; import 'genai/genai_login.dart'; +import 'genai/genai_session.dart'; import 'storage_service.dart'; class GenaiApiService extends ChangeNotifier { - GenaiApiService(this.storage, {GenaiGateway? gateway, GenaiLogin? login}) - : _gateway = gateway ?? + GenaiApiService( + this.storage, { + GenaiGateway? gateway, + GenaiLogin? login, + DateTime Function()? now, + }) : _gateway = gateway ?? GenaiGateway( onDiagnostic: (metadata) { // Request metadata only; never log prompts, tool results or credentials. stdout.writeln('genai_request ${jsonEncode(metadata)}'); }, ), - _login = login ?? GenaiLogin(); + _login = login ?? GenaiLogin(), + _now = now ?? DateTime.now { + _gateway.tokenProvider = _ensureToken; + } final StorageService storage; final GenaiGateway _gateway; + static const tokenLifetime = Duration(hours: 2); + static const _retryDelay = Duration(minutes: 1); + final GenaiLogin _login; + final DateTime Function() _now; + GenaiSession? _session; + Timer? _refreshTimer; + Future? _refreshing; + int _generation = 0; Future? _loading; bool _disposed = false; bool busy = false; String? error; - String? _genaiToken; String _apiToken = ''; String _host = '127.0.0.1'; int _port = 5000; bool get running => _gateway.running; - bool get loggedIn => (_genaiToken ?? '').isNotEmpty; + bool get loggedIn => _session != null; String get host => _host; int get port => _port; String get apiToken => _apiToken; @@ -43,10 +58,19 @@ class GenaiApiService extends ChangeNotifier { if (!_disposed) notifyListeners(); } + Future initialize() async { + try { + await load(); + } catch (_) { + // load exposes the settings error on the feature page. + } + } + Future load() => _loading ??= _load(); Future _load() async { + final generation = _generation; try { - _genaiToken = await storage.loadGenaiToken(); + final session = await storage.loadGenaiSession(); _apiToken = await storage.loadGenaiApiToken() ?? ''; if (_apiToken.isEmpty) { final random = Random.secure(); @@ -56,6 +80,9 @@ class GenaiApiService extends ChangeNotifier { } _host = storage.genaiApiHost; _port = storage.genaiApiPort; + if (_disposed || generation != _generation) return; + _session = session; + _scheduleRefresh(); } catch (_) { _loading = null; error = '读取设置失败,请重试'; @@ -65,8 +92,75 @@ class GenaiApiService extends ChangeNotifier { } } + void _scheduleRefresh({bool retry = false}) { + _refreshTimer?.cancel(); + if (_disposed || _session == null) return; + final remaining = _session!.expiresAt.difference(_now()); + final delay = retry ? _retryDelay : (remaining.isNegative ? Duration.zero : remaining); + _refreshTimer = Timer(delay, () => unawaited(_refreshOnTimer())); + } + + Future _refreshOnTimer() async { + try { + await _ensureToken(); + _scheduleRefresh(); + } catch (_) { + // _refresh records the error and schedules a retry. + } + } + + Future _ensureToken() { + if (_disposed || _session == null) { + throw const FormatException('请先登录 GenAI'); + } + if (_now().isBefore(_session!.expiresAt)) return Future.value(_session!.token); + return _refreshing ??= _refresh().whenComplete(() => _refreshing = null); + } + + Future _refresh() async { + final session = _session!; + final generation = _generation; + try { + final token = await _login.login(session.account, session.password); + final next = GenaiSession( + account: session.account, + password: session.password, + token: token, + expiresAt: _now().add(tokenLifetime), + ); + await _acceptSession(next, generation); + error = null; + _notify(); + return token; + } catch (_) { + if (!_disposed && generation == _generation) { + error = '自动登录失败,1 分钟后重试'; + _scheduleRefresh(retry: true); + _notify(); + } + rethrow; + } + } + + Future _acceptSession(GenaiSession session, int generation) async { + if (_disposed || generation != _generation) throw StateError('Session changed'); + await storage.saveGenaiSession(session); + if (_disposed || generation != _generation) throw StateError('Session changed'); + _session = session; + _gateway.updateGenaiToken(session.token); + _scheduleRefresh(); + } + + Future _waitForRefresh() async { + try { + await _refreshing; + } catch (_) { + // Account changes and logout can supersede an in-flight refresh. + } + } + Future login(String account, String password) async { - if (busy || running) return false; + if (_disposed || busy || running) return false; if (account.trim().isEmpty || password.isEmpty) { error = '请输入学号和密码'; _notify(); @@ -76,9 +170,21 @@ class GenaiApiService extends ChangeNotifier { error = null; _notify(); try { - final token = await _login.login(account, password); - await storage.saveGenaiToken(token); - _genaiToken = token; + await load(); + final generation = ++_generation; + _refreshTimer?.cancel(); + await _waitForRefresh(); + if (_disposed) return false; + final token = await _login.login(account.trim(), password); + await _acceptSession( + GenaiSession( + account: account.trim(), + password: password, + token: token, + expiresAt: _now().add(tokenLifetime), + ), + generation, + ); return true; } on FormatException catch (e) { error = e.message; @@ -88,17 +194,27 @@ class GenaiApiService extends ChangeNotifier { return false; } finally { busy = false; + _scheduleRefresh(); _notify(); } } Future logout() async { - if (busy) return; - await stop(); - await storage.clearGenaiToken(); - _genaiToken = null; - error = null; + if (_disposed || busy) return; + busy = true; + ++_generation; + _refreshTimer?.cancel(); + _session = null; _notify(); + try { + await stop(); + await _waitForRefresh(); + await storage.clearGenaiSession(); + error = null; + } finally { + busy = false; + _notify(); + } } Future saveSettings({ @@ -123,23 +239,29 @@ class GenaiApiService extends ChangeNotifier { } Future start() async { - if (busy || running) return running; + if (_disposed || busy || running) return running; busy = true; error = null; _notify(); try { await load(); - if (!loggedIn) throw const FormatException('请先登录 GenAI'); - await _gateway.start(host: _host, port: _port, genaiToken: _genaiToken!, apiToken: _apiToken); + final token = await _ensureToken(); + if (_disposed) return false; + try { + await _gateway.start(host: _host, port: _port, genaiToken: token, apiToken: _apiToken); + } on SocketException { + throw const FormatException('端口已占用,换一个试试'); + } + if (_disposed) { + await _gateway.stop(); + return false; + } return true; } on FormatException catch (e) { error = e.message; return false; - } on SocketException catch (_) { - error = '端口已占用,换一个试试'; - return false; } catch (_) { - error = '启动失败,请重试'; + error ??= '启动失败,请重试'; return false; } finally { busy = false; @@ -155,6 +277,8 @@ class GenaiApiService extends ChangeNotifier { @override void dispose() { _disposed = true; + ++_generation; + _refreshTimer?.cancel(); unawaited(_gateway.stop()); super.dispose(); } diff --git a/lib/services/storage_service.dart b/lib/services/storage_service.dart index c59f0764..b140bf9f 100644 --- a/lib/services/storage_service.dart +++ b/lib/services/storage_service.dart @@ -19,6 +19,7 @@ import '../models/renew_status.dart'; import '../models/third_party_account.dart'; import '../models/user_session.dart'; import '../utils/secure_storage.dart'; +import 'genai/genai_session.dart'; class StorageService { static const _sessionKey = 'user_session'; @@ -214,9 +215,26 @@ class StorageService { Future setGenaiApiHost(String value) => _prefs.setString(_genaiApiHostKey, value); int get genaiApiPort => _prefs.getInt(_genaiApiPortKey) ?? 5000; Future setGenaiApiPort(int value) => _prefs.setInt(_genaiApiPortKey, value); - Future loadGenaiToken() => _secure.read(key: 'genai_upstream_token'); - Future saveGenaiToken(String value) => _secure.write(key: 'genai_upstream_token', value: value); - Future clearGenaiToken() => _secure.delete(key: 'genai_upstream_token'); + Future loadGenaiSession() async { + final raw = await _secure.read(key: 'genai_session'); + if (raw == null) return null; + try { + return GenaiSession.fromJson(jsonDecode(raw) as Map); + } on FormatException { + return null; + } on TypeError { + return null; + } + } + + Future saveGenaiSession(GenaiSession session) => + _secure.write(key: 'genai_session', value: jsonEncode(session.toJson())); + + Future clearGenaiSession() async { + await _secure.delete(key: 'genai_session'); + await _secure.delete(key: 'genai_upstream_token'); + } + Future loadGenaiApiToken() => _secure.read(key: 'genai_local_api_token'); Future saveGenaiApiToken(String value) => _secure.write(key: 'genai_local_api_token', value: value); diff --git a/test/genai_api_service_test.dart b/test/genai_api_service_test.dart new file mode 100644 index 00000000..bea3c221 --- /dev/null +++ b/test/genai_api_service_test.dart @@ -0,0 +1,361 @@ +import 'dart:async'; +import 'dart:convert'; +import 'dart:io'; + +import 'package:flutter_secure_storage_ohos/flutter_secure_storage_ohos.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:shared_preferences/shared_preferences.dart'; +import 'package:techpie/services/genai/genai_gateway.dart'; +import 'package:techpie/services/genai/genai_login.dart'; +import 'package:techpie/services/genai/genai_session.dart'; +import 'package:techpie/services/genai_api_service.dart'; +import 'package:techpie/services/storage_service.dart'; + +class _Login extends GenaiLogin { + final calls = <(String, String)>[]; + Future Function()? next; + + @override + Future login(String account, String password) async { + calls.add((account, password)); + return next == null ? 'token-${calls.length}' : await next!(); + } +} + +Future _storage({Map secrets = const {}}) async { + SharedPreferences.setMockInitialValues({'genai_api_port': 0}); + FlutterSecureStorage.setMockInitialValues(Map.of(secrets)); + return StorageService(await SharedPreferences.getInstance()); +} + +GenaiSession _session(DateTime expiresAt) => GenaiSession( + account: 'student', + password: 'fixture-password', + token: 'saved-token', + expiresAt: expiresAt, + ); + +void main() { + TestWidgetsFlutterBinding.ensureInitialized(); + final initial = DateTime.utc(2026, 10, 28, 12); + + testWidgets('login saves credentials and the two-hour expiry in secure storage', (tester) async { + final storage = await _storage(); + final login = _Login(); + final service = GenaiApiService(storage, login: login, now: () => initial); + expect(await service.login(' student ', 'fixture-password'), isTrue); + final saved = (await storage.loadGenaiSession())!; + expect(saved.account, 'student'); + expect(saved.password, 'fixture-password'); + expect(saved.token, 'token-1'); + expect(saved.expiresAt, initial.add(const Duration(hours: 2))); + final prefs = await SharedPreferences.getInstance(); + expect(prefs.getKeys(), {'genai_api_port'}); + expect(service.apiToken, startsWith('sk-')); + service.dispose(); + }); + + testWidgets('timer refreshes without a page or running API and renews the deadline', + (tester) async { + var now = initial; + final storage = await _storage(); + final login = _Login(); + final service = GenaiApiService(storage, login: login, now: () => now); + await service.login('student', 'fixture-password'); + final apiToken = service.apiToken; + now = now.add(const Duration(hours: 2)); + await tester.pump(const Duration(hours: 2)); + expect(login.calls, [('student', 'fixture-password'), ('student', 'fixture-password')]); + expect((await storage.loadGenaiSession())!.token, 'token-2'); + expect((await storage.loadGenaiSession())!.expiresAt, now.add(const Duration(hours: 2))); + expect(service.apiToken, apiToken); + expect(service.running, isFalse); + now = now.add(const Duration(hours: 2)); + await tester.pump(const Duration(hours: 2)); + expect(login.calls, hasLength(3)); + service.dispose(); + }); + + testWidgets('app initialization restores the remaining lifetime rather than extending it', + (tester) async { + var now = initial; + final storage = await _storage(); + final expiry = initial.add(const Duration(hours: 1)); + await storage.saveGenaiSession(_session(expiry)); + final login = _Login(); + final service = GenaiApiService(storage, login: login, now: () => now); + await service.initialize(); + expect(service.loggedIn, isTrue); + expect(login.calls, isEmpty); + now = expiry; + await tester.pump(const Duration(hours: 1)); + expect(login.calls, hasLength(1)); + expect((await storage.loadGenaiSession())!.token, 'token-1'); + service.dispose(); + }); + + testWidgets('failed automatic login retries after one minute without losing credentials', + (tester) async { + var now = initial; + final storage = await _storage(); + await storage.saveGenaiSession(_session(initial)); + final login = _Login()..next = () async => throw const SocketException('offline'); + final service = GenaiApiService(storage, login: login, now: () => now); + await service.initialize(); + await tester.pump(const Duration(milliseconds: 1)); + expect(login.calls, hasLength(1)); + expect(service.error, '自动登录失败,1 分钟后重试'); + expect((await storage.loadGenaiSession())!.token, 'saved-token'); + now = now.add(const Duration(seconds: 59)); + await tester.pump(const Duration(seconds: 59)); + expect(login.calls, hasLength(1)); + login.next = null; + now = now.add(const Duration(seconds: 1)); + await tester.pump(const Duration(seconds: 1)); + expect(login.calls, hasLength(2)); + expect((await storage.loadGenaiSession())!.token, 'token-2'); + expect(service.error, isNull); + service.dispose(); + }); + + testWidgets('logout during refresh clears credentials and ignores the late token', + (tester) async { + final storage = await _storage(secrets: {'genai_upstream_token': 'legacy-token'}); + await storage.saveGenaiSession(_session(initial)); + final pending = Completer(); + final login = _Login()..next = () => pending.future; + final service = GenaiApiService(storage, login: login, now: () => initial); + await service.initialize(); + await tester.pump(const Duration(milliseconds: 1)); + expect(login.calls, hasLength(1)); + final logout = service.logout(); + await tester.pump(const Duration(milliseconds: 1)); + pending.complete('late-token'); + await logout; + expect(service.loggedIn, isFalse); + expect(await storage.loadGenaiSession(), isNull); + final secure = const FlutterSecureStorage(); + expect(await secure.read(key: 'genai_upstream_token'), isNull); + expect(await storage.loadGenaiApiToken(), service.apiToken); + await tester.pump(const Duration(hours: 3)); + expect(login.calls, hasLength(1)); + service.dispose(); + }); + + testWidgets('dispose ignores a pending refresh and cancels future timers', (tester) async { + final storage = await _storage(); + await storage.saveGenaiSession(_session(initial)); + final pending = Completer(); + final login = _Login()..next = () => pending.future; + final service = GenaiApiService(storage, login: login, now: () => initial); + await service.initialize(); + await tester.pump(const Duration(milliseconds: 1)); + service.dispose(); + pending.complete('late-token'); + await tester.pump(const Duration(milliseconds: 1)); + expect((await storage.loadGenaiSession())!.token, 'saved-token'); + await tester.pump(const Duration(hours: 3)); + expect(login.calls, hasLength(1)); + }); + + testWidgets('account switch waits for the old refresh and saves only the new account', + (tester) async { + final storage = await _storage(); + await storage.saveGenaiSession(_session(initial)); + final pending = Completer(); + final login = _Login()..next = () => pending.future; + final service = GenaiApiService(storage, login: login, now: () => initial); + await service.initialize(); + await tester.pump(const Duration(milliseconds: 1)); + final switchAccount = service.login('other-student', 'other-password'); + await tester.pump(const Duration(milliseconds: 1)); + login.next = null; + pending.complete('old-account-token'); + expect(await switchAccount, isTrue); + final saved = (await storage.loadGenaiSession())!; + expect(saved.account, 'other-student'); + expect(saved.password, 'other-password'); + expect(saved.token, 'token-2'); + service.dispose(); + }); + + testWidgets('legacy token without expiry requires a new login', (tester) async { + final storage = await _storage(secrets: {'genai_upstream_token': 'legacy-token'}); + final login = _Login(); + final service = GenaiApiService(storage, login: login, now: () => initial); + await service.initialize(); + expect(service.loggedIn, isFalse); + expect(await service.start(), isFalse); + expect(login.calls, isEmpty); + service.dispose(); + }); + + group('running HTTP gateway', () { + late HttpServer upstream; + late HttpClient client; + late GenaiGateway gateway; + late GenaiApiService service; + late StorageService storage; + late _Login login; + late DateTime now; + Completer? streamEntered; + Completer? releaseStream; + final tokens = []; + + setUp(() async { + HttpOverrides.global = null; + now = initial; + tokens.clear(); + streamEntered = null; + releaseStream = null; + storage = await _storage(); + upstream = await HttpServer.bind(InternetAddress.loopbackIPv4, 0); + upstream.listen((request) async { + tokens.add(request.headers.value('x-access-token')); + if (request.uri.path.endsWith('/list')) { + request.response.headers.contentType = ContentType.json; + request.response.write( + jsonEncode({ + 'success': true, + 'result': { + 'records': >[], + }, + }), + ); + } else { + await request.drain(); + streamEntered?.complete(); + await releaseStream?.future; + request.response.headers.contentType = ContentType('text', 'event-stream'); + request.response.write('data: ${jsonEncode({ + 'choices': [ + { + 'delta': {'content': 'still running'}, + 'finish_reason': 'stop', + }, + ], + })}\n\ndata: [DONE]\n\n'); + } + await request.response.close(); + }); + gateway = GenaiGateway( + upstream: Uri.parse('http://127.0.0.1:${upstream.port}'), + heartbeatInterval: const Duration(milliseconds: 30), + ); + login = _Login(); + service = GenaiApiService(storage, gateway: gateway, login: login, now: () => now); + await service.login('student', 'fixture-password'); + expect(await service.start(), isTrue); + client = HttpClient(); + }); + + tearDown(() async { + service.dispose(); + client.close(force: true); + await gateway.stop(); + await upstream.close(force: true); + }); + + Future models({String? apiToken}) async { + final request = await client.getUrl(Uri.parse('${service.endpoint}/models')); + request.headers.set('Authorization', 'Bearer ${apiToken ?? service.apiToken}'); + final response = await request.close(); + await response.drain(); + return response.statusCode; + } + + test('expired concurrent requests share one login and use the new token on the same port', + () async { + final port = gateway.port; + final apiToken = service.apiToken; + expect(await models(), 200); + now = now.add(const Duration(hours: 2)); + final pending = Completer(); + final entered = Completer(); + login.next = () { + entered.complete(); + return pending.future; + }; + final provider = gateway.tokenProvider!; + final bothWaiting = Completer(); + var waiting = 0; + gateway.tokenProvider = () { + if (++waiting == 2) bothWaiting.complete(); + return provider(); + }; + final first = models(); + await entered.future; + final second = models(); + await bothWaiting.future; + pending.complete('refreshed-token'); + expect(await Future.wait([first, second]), [200, 200]); + expect(login.calls, hasLength(2)); + expect(tokens, ['token-1', 'refreshed-token', 'refreshed-token']); + expect((await storage.loadGenaiSession())!.token, 'refreshed-token'); + expect(gateway.port, port); + expect(service.running, isTrue); + expect(service.apiToken, apiToken); + expect(await storage.loadGenaiApiToken(), apiToken); + }); + + test('refresh keeps an existing streaming response alive', () async { + streamEntered = Completer(); + releaseStream = Completer(); + final request = await client.postUrl(Uri.parse('${service.endpoint}/chat/completions')); + request.headers.set('Authorization', 'Bearer ${service.apiToken}'); + request.headers.contentType = ContentType.json; + request.write( + jsonEncode({ + 'model': 'test-model', + 'messages': [ + {'role': 'user', 'content': 'fixture'}, + ], + 'stream': true, + }), + ); + final response = await request.close(); + final body = utf8.decoder.bind(response).join(); + await streamEntered!.future; + final port = gateway.port; + now = now.add(const Duration(hours: 2)); + expect(await models(), 200); + expect(tokens, ['token-1', 'token-2']); + expect(gateway.port, port); + releaseStream!.complete(); + expect(await body, contains('still running')); + expect((await storage.loadGenaiSession())!.token, 'token-2'); + }); + + test('failed refresh never forwards the expired token and a later request can recover', + () async { + now = now.add(const Duration(hours: 3)); + login.next = () async => throw const FormatException('登录失败'); + expect(await models(), isNot(200)); + expect(tokens, isEmpty); + expect((await storage.loadGenaiSession())!.token, 'token-1'); + login.next = null; + expect(await models(), 200); + expect(tokens, ['token-3']); + expect((await storage.loadGenaiSession())!.token, 'token-3'); + expect(service.error, isNull); + }); + + test('invalid local API token does not trigger upstream login', () async { + now = now.add(const Duration(hours: 3)); + expect(await models(apiToken: 'wrong-token'), 401); + expect(login.calls, hasLength(1)); + expect(tokens, isEmpty); + }); + + test('starting after app restart refreshes an expired saved session', () async { + await service.stop(); + service.dispose(); + now = now.add(const Duration(hours: 3)); + service = GenaiApiService(storage, gateway: gateway, login: login, now: () => now); + expect(await service.start(), isTrue); + expect(login.calls, hasLength(2)); + expect(await models(), 200); + expect(tokens, ['token-2']); + }); + }); +} From a0cbc0b01c12b712e51cc151a20f7cb9a629d5e7 Mon Sep 17 00:00:00 2001 From: dzhes Date: Fri, 13 Nov 2026 18:22:39 +0800 Subject: [PATCH 3/3] docs: use concise English developer documentation --- docs/genai-gateway-validation.md | 71 +++++++++++++++++--------------- 1 file changed, 37 insertions(+), 34 deletions(-) diff --git a/docs/genai-gateway-validation.md b/docs/genai-gateway-validation.md index f6d64f83..3d7d7994 100644 --- a/docs/genai-gateway-validation.md +++ b/docs/genai-gateway-validation.md @@ -1,45 +1,48 @@ -# GenAI 网关修复与验收 +# GenAI gateway fixes and acceptance -2026-10-28,已同步本地上游 `b517039`,以及其工作区的原生工具增量、空回复重试更新。对照 `/proj/GenAI2OpenAI/docs/TechPie工具链问题交接.md`。 +On 2026-10-28, this branch incorporated local upstream `b517039` and its native-tool and empty-response retry updates. The handoff reference is `/proj/GenAI2OpenAI/docs/TechPie工具链问题交接.md`. -## 已修复 +## Gateway behavior -- 先接收最后一段正文,再处理结束原因。保留 `length`、`content_filter`;没有 finish 或 DONE 的 EOF 返回错误。 -- 工具解析支持标准对象、嵌套 function、顶层数组、尾随链接、多个 JSON 对象、多个 XML 块。保留全部调用及已有 ID。 -- 工具名称必须在本次声明中,arguments 必须能解析为 JSON 对象。只在上游正常结束、调用及参数已完整时补齐外层数组或唯一 tool_calls 包装对象的闭合符;不补调用、参数、字符串或逗号后缺失的值。未知工具、错误参数不回退成成功正文。普通 name 字段、字段说明和空 tool_calls 数组保留为普通答案。这里不做完整 JSON Schema 校验。 -- 支持 Responses 的函数工具定义、function_call / function_call_output 历史、函数输出和 SSE added/delta/done/completed 生命周期。截断为 incomplete,错误为 failed。 -- 补充执行任务时应当返回工具调用的提示,落实 required 和指定函数;auto 仍允许正常总结。仅在上游正常结束且正文和原生工具调用均为空时,使用相同历史重试一次;首轮纯推理不向客户端发布。两次均为空时报错。错误、EOF、length、已输出正文或工具调用均不重试,不重放工具副作用。 -- Chat 优先采用 max_completion_tokens,其次 max_tokens;Responses 使用 max_output_tokens。 -- 原生 tool_calls 按 index 聚合名称及参数分片,兼容 message 和旧 function_call 字段,聚合后统一校验名称及参数;未启用工具时不接收原生调用。 -- SSE 在等待上游响应头时发送心跳,客户端断开时取消等待并关闭对应上游连接。 -- 请求日志前缀 `genai_request`。只记录请求 ID、路径、模型、计数、tool_choice、输出上限、耗时、结束原因、DONE、取消状态、异常类别,不记录凭据、正文或工具结果。`saw_done=false` 表示本次没有读到 DONE;已有 finish_reason 时也正常结束读取。 +- Consume the final text delta before processing the finish reason. Preserve `length` and `content_filter`; report an error for EOF without a finish marker or DONE. +- Parse standard tool objects, nested `function` objects, top-level arrays, trailing links, multiple JSON objects and multiple XML blocks. Preserve every call and existing ID. +- Require declared tool names and arguments that parse as JSON objects. After a normal upstream finish, complete a missing outer array or unique `tool_calls` wrapper only when all calls and arguments are complete. Reject incomplete calls, parameters, strings and values after commas. Treat ordinary `name` fields, field descriptions and empty `tool_calls` arrays as text answers. Validation covers names and JSON-object arguments; full JSON Schema validation remains outside this implementation. +- Support Responses function definitions, `function_call` / `function_call_output` history, function results and the SSE added/delta/done/completed lifecycle. Map truncation to `incomplete` and errors to `failed`. +- Prompt for actual tool calls when execution is required, enforce `required` and named-function choices, and allow ordinary summaries in `auto` mode. Retry generation once with the same history after a normal finish containing neither text nor native calls. Buffer reasoning from the first attempt. Report an error if both attempts are empty. Errors, EOF, `length`, published text and tool calls end the retry path; tool side effects are never replayed. +- Prefer `max_completion_tokens` over `max_tokens` for Chat; use `max_output_tokens` for Responses. +- Aggregate native tool-name and argument fragments by index, including `message` and legacy `function_call` fields. Validate the completed calls and reject native calls when tools are disabled. +- Send SSE heartbeats while waiting for upstream headers. Cancel the wait and close its upstream connection when the client disconnects. +- Log `genai_request` metadata: request ID, path, model, counts, tool choice, output limit, duration, finish reason, DONE, cancellation and exception type. Credentials, message text and tool results stay out of logs. `saw_done=false` means DONE was absent; a finish reason still permits normal completion. -## Token 自动续期 +## Token renewal -- 登录后将账号、密码、上游 Token 和到期时间一起保存到系统安全存储,有效期为 2 小时。 -- 应用运行时到期自动登录;启动应用时恢复计时。休眠恢复后的请求也会先检查有效期。 -- 新 Token 写入本地记录并更新运行中的网关,端口、本地 API Token 和已有流式响应保持不变。 -- 并发请求共用一次刷新;自动登录失败后 1 分钟重试,退出登录会清除账号密码并取消续期。 -- 旧版记录没有账号密码和到期时间,需要重新登录一次。 +Secure storage retains the account, password, upstream token and two-hour expiry after login. The running app renews the token at expiry, restores its timer on startup and checks expiry before requests after resume. The new token updates storage and the live gateway while preserving its port, local API token and existing streams. -本次本地验证:自动续期与 HTTP 服务测试 13 项,原有网关测试 62 项;本分支完整 Flutter 测试 728 项通过,静态检查通过。 +Concurrent requests share one refresh. Automatic login failures retry after one minute. Sign-out clears the credentials and cancels renewal. Older records require one new login to populate credentials and expiry. -## 拆分前的整包验证 +Local validation for this branch: 13 renewal/HTTP-service tests, 62 gateway tests, 728 full Flutter tests and static analysis passed. -以下记录来自 PR #135 的本机安装与整包测试,包含 Linux DDL、手表等模块。本 PR 仅保留 GenAI 网关、功能页和对应测试。 +## Validation before the PR split -- `flutter test test/genai_gateway_test.dart --no-pub`:62 项通过。 -- `flutter test --no-pub`:全套 740 项通过;更新了截止日期空状态断言,以及设置页日志入口的滚动定位。 -- 对修改的 Dart 文件定向运行 `flutter analyze --no-pub`:无问题。 -- `flutter build linux --release --no-pub`:成功。 -- `tool/genai_gateway_smoke.dart`:真实 ChatGLM,在 Chat Completions 和 Responses 上分别完成流式请求工具、夹具返回、非流式总结;开发网关和安装版各验证一次。只提供 fixture_read 夹具,结果为固定编号,没有执行文件或 Git 操作,没有测试 GPT。 -- 安装路径:`/home/jdz/.local/opt/techpie-linux-widgets-v1/bundle`。 -- 本轮安装进程 PID 404337,`127.0.0.1:5000` 属于该进程;核对其加载的 libapp.so 与构建产物一致。 -- libapp.so SHA256:`41666b4dfccc439d6d1622a0f084f4d8816766d204fa11f061cb06e068b7510d`。 -- 上一版备份:`/home/jdz/.local/opt/techpie-linux-widgets-v1/bundle-backup-20261028-194654`。 -- 本次通过 `techpie --genai-api` 恢复 API 服务。平常仍可从 GenAI API 页启动。 +The following evidence belongs to the local installation and combined PR #135 build, which also contained Linux DDL and watch modules. This PR contains the GenAI gateway, feature page and their tests. -日志: +| Check | Recorded result | +| --- | --- | +| `flutter test test/genai_gateway_test.dart --no-pub` | 62 passed | +| `flutter test --no-pub` | 740 passed; updated deadline empty-state assertions and settings-log scrolling | +| Targeted `flutter analyze --no-pub` | No issues | +| `flutter build linux --release --no-pub` | Succeeded | +| `tool/genai_gateway_smoke.dart` | Real ChatGLM streaming tool request, fixture result and non-streaming summary through both Chat Completions and Responses, in development and installed gateways | + +The smoke check exposed only `fixture_read` with fixed IDs. It exercised fixture tool round trips; file/Git execution and GPT models await separate acceptance. + +- Installation: `/home/jdz/.local/opt/techpie-linux-widgets-v1/bundle`. +- Recorded process: PID 404337 owned `127.0.0.1:5000`; its loaded `libapp.so` matched the build. +- `libapp.so` SHA-256: `41666b4dfccc439d6d1622a0f084f4d8816766d204fa11f061cb06e068b7510d`. +- Previous bundle: `/home/jdz/.local/opt/techpie-linux-widgets-v1/bundle-backup-20261028-194654`. +- `techpie --genai-api` restored the API service for this run. The GenAI API page remains the normal entry point. + +Logs: - `/tmp/techpie-all-sync-tests-final.log` - `/tmp/techpie-genai-sync-tests.log` @@ -49,6 +52,6 @@ - `/tmp/techpie-installed-chatglm-sync.log` - `/tmp/techpie-installed-genai-sync.log` -## VS Code 原客户端 +## Original VS Code client -尚未完成原客户端的文件读取、编辑验收;本机无该会话的客户端工具记录。原截图的实际 API 路径和停止原因仍未确定。重测时,用请求日志中的 request_id、path、结束原因与客户端工具记录对应。不要以模型声称完成或界面步骤计数认定实际执行成功。 +File reading and editing in the original client await acceptance. The session has no local client-tool execution record, and the screenshot's API path and stop reason remain unresolved. Match request IDs, paths and finish reasons to client tool records when retesting. Those execution records are the acceptance evidence for actual file operations.