Commit Graph

117 Commits

Author SHA1 Message Date
hailin 0dbe711ed3 feat: add detailed logging to voice pipeline (STT/Agent/TTS timing)
Log timestamps, content, and event details at each pipeline stage
to help diagnose voice-agent integration issues.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-24 02:47:21 -08:00
hailin 1d5c834dfe feat: add event buffering to agent WS gateway for late subscribers
Buffer stream events when no WS clients are subscribed yet, then replay
them when a client subscribes. This eliminates the race condition where
events are lost between task creation and WS subscription.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-24 02:41:38 -08:00
hailin 370e32599f fix: subscribe to agent WS before creating task to avoid race condition
The engine stream could emit text events before the voice pipeline
subscribed, causing all text to be lost.  Now we connect and subscribe
first, then POST the task.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-24 02:35:57 -08:00
hailin 82d12a5ff5 feat: mount voice model cache volumes to avoid re-downloading on restart
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-24 02:28:28 -08:00
hailin abf5e29419 feat: route voice pipeline through agent-service instead of direct LLM
Voice calls now use the same agent task + WS subscription flow as the
chat UI, enabling tool use and command execution during voice sessions.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-24 00:47:31 -08:00
hailin 7afbd54fce fix: rewrite voice pipeline for direct WebSocket I/O, fix TTS and navigation
Root cause: Pipecat's WebsocketServerTransport creates its own WebSocket
server on (host,port) and expects FrameProcessor subclasses. Our code was
passing a FastAPI WebSocket object as 'host' and using plain STT/TTS/VAD
service classes that aren't FrameProcessors. The pipeline crashed immediately
when receiving audio, causing "disconnects when speaking".

Changes:
- **base_pipeline.py**: Complete rewrite — replaced Pipecat Pipeline with
  direct async loop: WebSocket → VAD → STT → Claude LLM → TTS → WebSocket.
  Supports barge-in (interrupt TTS when user speaks), audio chunking, and
  24kHz→16kHz TTS resampling.
- **session_router.py**: Pass WebSocket directly to pipeline instead of
  wrapping in AppTransport.
- **app_transport.py**: Deprecated (no longer needed).
- **kokoro_service.py**: Fix misaki compatibility (MutableToken→MToken
  rename), use correct Chinese voice 'zf_xiaoxiao', handle torch tensors.
- **main.py**: Apply misaki monkey-patch before importing kokoro.
- **settings.py**: Change default TTS voice from 'zh_female_1' (non-existent)
  to 'zf_xiaoxiao' (valid Kokoro-82M Chinese female voice).
- **requirements.txt**: Remove pipecat-ai dependency, pin kokoro==0.3.5 +
  misaki==0.7.17, add Chinese NLP deps (pypinyin, cn2an, jieba, ordered-set).
- **agent_call_page.dart**: Wrap each cleanup step in try/catch to ensure
  Navigator.pop() always executes after call ends. Add 3s timeout on session
  delete request.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 23:34:35 -08:00
hailin 6cd53e713c fix: bypass JWT for voice WebSocket route (fixes 401 on WS upgrade)
根因:Kong 日志显示 voice WebSocket 连接被 JWT 插件返回 401,
因为 WebSocket RFC 6455 不支持自定义 header,Flutter 的
WebSocketChannel.connect 无法携带 Authorization header。

修复策略(业界标准做法):
1. Kong: 将 voice-service 的 JWT 从 service 级别改为 route
   级别,仅在 voice-api 和 twilio-webhook 路由启用 JWT,
   voice-ws 路由免除(session 创建已通过 JWT 验证,
   session_id 本身作为认证凭据)
2. 后端: session_router 返回的 websocket_url 改为
   /ws/voice/{session_id}(匹配 Kong voice-ws 路由路径)
3. FastAPI: 在 app 级别增加 /ws/voice/{session_id} 顶级
   WebSocket 路由,委托给 session_router 的 handler

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 21:30:11 -08:00
hailin 2a87dd346e fix: send empty JSON body to voice session endpoint (fixes 422)
FastAPI 的 create_session 端点声明了 Pydantic request body
(CreateSessionRequest),虽然所有字段都有默认值,但 FastAPI
仍要求请求包含有效 JSON body(至少 {})。Flutter 端 dio.post
未传 data 参数导致 Content-Type 缺失,FastAPI 返回 422
Unprocessable Entity。修复:添加 data: {} 发送空 JSON 对象。

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 21:21:48 -08:00
hailin 45c54acb87 fix: improve voice call page UI centering and error display
1. 布局居中:将 Column 包裹在 Center 中,所有文本添加
   textAlign: TextAlign.center,确保头像、标题、副标题
   在各种屏幕尺寸上居中显示。

2. 错误展示优化:将 SnackBar 大面积红色块替换为行内错误卡片,
   采用圆角容器 + error icon + 简洁文案,视觉上更融洽。
   新增 _errorMessage 状态字段 + _friendlyError() 方法,
   将 DioException 等异常转换为中文友好提示(如 "语音服务
   暂不可用 (503)"),避免用户看到大段英文 stacktrace。

3. 错误状态清理:点击接听时自动清除上一次的 _errorMessage。

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 21:10:49 -08:00
hailin b7814d42a9 fix: resolve 3 timeline UI bugs (blank start, spinner style, tool status)
1. 任务开始时空白状态:当 agent streaming 启动但尚无 assistant
   响应时,在时间线末尾插入虚拟 "处理中..." 节点(带脉动 * 动画),
   避免用户发送 prompt 后界面无任何反馈。
   (chat_page.dart: _needsWorkingNode + ListView itemCount+1)

2. * 动画从旋转改为脉动:_SpinnerDot 由 Transform.rotate 改为
   Transform.scale(0.6x↔1.2x 缩放 + 透明度 0.6↔1.0 呼吸),
   duration 从 1000ms 降至 800ms 并启用 reverse,视觉效果类似
   星星闪烁而非机械旋转。
   (timeline_event_node.dart: _SpinnerDotState)

3. 工具执行完成后状态卡在 spinner:ToolResultEvent 到达时仅创建
   新 toolResult 消息,未回溯更新对应 toolUse 消息的 ToolStatus,
   导致时间线上工具节点永远显示 executing spinner。修复:在
   ToolResultEvent handler 中向前查找最近的 executing 状态的
   toolUse 消息,将其 status 更新为 completed/error。
   (chat_providers.dart: ToolResultEvent case)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 18:13:30 -08:00
hailin 20325a84bd feat: redesign chat UI from bubble style to timeline workflow
Replace traditional chat bubble layout with a Claude Code-inspired
timeline/workflow design:
- Vertical gray line connecting sequential event nodes
- Colored dots for each event (green=done, red=error, yellow=warning)
- Animated spinning asterisk (*) on active nodes
- Streaming text with blinking cursor in timeline nodes
- Tool execution shown as code blocks within timeline
- User prompts as distinct nodes with person icon

New file: timeline_event_node.dart (TimelineEventNode, CodeBlock)
Rewritten: chat_page.dart (timeline layout, no more bubbles)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 17:33:42 -08:00
hailin 74be945e4a feat: enable token-level streaming and fix duplicate message bubble
Backend:
- Add includePartialMessages: true to SDK query options
- Handle stream_event/content_block_delta for real-time text streaming
- Skip text/thinking blocks from complete assistant messages (already
  streamed via deltas) to avoid duplication
- Change default result summary to empty string

Flutter:
- Only show CompletedEvent summary when no assistant text was streamed
  (prevents duplicate message bubble)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 17:24:48 -08:00
hailin 5f827b0961 fix: revert ws/wss protocols - Kong OSS handles WS over http/https
Kong 3.7 OSS doesn't support ws/wss protocol identifiers (Enterprise only).
WebSocket upgrades are handled transparently over http/https protocols.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 17:02:53 -08:00
hailin 2e6a5e7292 chore: update lockfile for ws/platform-ws dependencies
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 16:54:14 -08:00
hailin 86d7cac631 fix: replace Socket.IO with raw WebSocket to fix 502 on /ws/agent
Socket.IO requires its own handshake protocol (EIO=4) which Kong cannot
proxy as a plain WebSocket upgrade, causing 502 Bad Gateway. Switch to
@nestjs/platform-ws (WsAdapter) with manual session room tracking so
Flutter's IOWebSocketChannel can connect directly.

Also add ws/wss protocols to Kong WebSocket routes.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 16:52:43 -08:00
hailin 5e31b15dcf fix: use IOWebSocketChannel for headers support
WebSocketChannel.connect does not accept headers parameter in
web_socket_channel 2.4.0. Use IOWebSocketChannel.connect instead.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 16:45:35 -08:00
hailin 803cea0fe4 fix: pass JWT token in WebSocket connection headers
WebSocket connections to /ws/agent were rejected by Kong (401)
because the Authorization header was not included. Now reads
access_token from secure storage and passes it in the WebSocket
upgrade request headers.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 16:43:31 -08:00
hailin 9cdc4933dc fix: add python-multipart dependency for voice-service
Required by FastAPI for form/file upload parsing. Missing dependency
may cause import errors and container restart loops.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 16:10:50 -08:00
hailin 3cb9ebd407 fix: release QueryRunner connections to prevent pool exhaustion
TenantAwareRepository.getRepository() was calling createQueryRunner()
without ever releasing it, causing database connection pool exhaustion.
This caused ops-service (and eventually other services) to hang on
all API requests once the pool filled up.

Replaced getRepository() with withRepository() pattern that wraps
operations in try/finally to always release the QueryRunner.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 15:55:06 -08:00
hailin a6cd3c20d9 feat: add WebSocket robustness to voice call (heartbeat, reconnect, jitter buffer)
Addresses reliability gaps in the real-time voice WebSocket connection
between Flutter client and Python voice-service backend.

Backend (voice-service):
- Heartbeat: new _heartbeat_sender coroutine sends JSON ping text frames
  every 15s alongside the Pipecat pipeline; failed send = dead connection
- Session preservation: on WebSocket disconnect, sessions are now marked
  "disconnected" with a timestamp instead of being deleted, allowing
  reconnection within a configurable TTL (default 60s)
- Reconnect endpoint: POST /sessions/{id}/reconnect verifies the session
  is alive and in "disconnected" state, returns fresh websocket_url
- Reconnect-aware WS handler: detects "disconnected" sessions, cancels
  stale pipeline tasks, creates a new pipeline, sends "session.resumed"
- Background cleanup: asyncio loop every 30s removes sessions that have
  been disconnected longer than session_ttl
- Structured event protocol: text frames = JSON control messages
  (ping/pong/session.resumed/session.ended/error), binary = PCM audio
- New settings: session_ttl (60s), heartbeat_interval (15s),
  heartbeat_timeout (45s)

Flutter (agent_call_page.dart):
- Heartbeat monitoring: tracks last server ping timestamp, triggers
  reconnect if no ping received in 45s (3 missed intervals)
- Auto-reconnect: exponential backoff (1s→2s→4s→8s→16s), max 5 attempts;
  calls /reconnect endpoint to verify session, rebuilds WebSocket,
  resets audio buffer, restarts heartbeat
- Reconnecting UI: yellow warning banner "重新连接中... (N/5)" with
  spinner overlay during reconnection attempts
- WebSocket data routing: _onWsData distinguishes String (JSON control)
  from binary (audio) frames, handles ping/session.resumed/session.ended
- User-initiated disconnect guard: _userEndedCall flag prevents reconnect
  attempts when user intentionally hangs up
- session_id field compatibility: supports session_id/sessionId/id

Flutter (pcm_player.dart):
- Jitter buffer: queues incoming PCM chunks, starts playback only after
  accumulating 4800 bytes (150ms at 16kHz 16-bit mono) to smooth out
  network timing variance
- reset() method: clears buffer on reconnect to discard stale audio
- Buffer underrun handling: re-enters buffering phase if queue empties

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 07:32:19 -08:00
hailin dfc541b571 feat: add Markdown rendering and phone-call voice entry to chat UI
Chat message rendering:
- MessageBubble: replace plain SelectableText with MarkdownBody for
  assistant messages, with full dark-theme stylesheet (headers, code
  blocks, tables, blockquotes, list bullets)
- StreamTextWidget: render completed messages as MarkdownBody, keep
  plain-text + blinking cursor for actively streaming messages

Voice interaction redesign:
- Remove all long-press-to-record code (~100 lines): AudioRecorder,
  SpeechEnhancer, mic pulse animation, voice indicator bar,
  SingleTickerProviderStateMixin
- Add phone-call button in AppBar (Icons.call) that navigates to the
  existing AgentCallPage for full-duplex voice conversation
- Add prominent "语音通话" entry button on empty chat state
- AgentCallPage was already fully implemented (ringing → connecting →
  active → ended, dual-direction WebSocket audio, GTCRN denoise,
  Kokoro TTS playback, waveform visualization) but previously unused

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 07:31:40 -08:00
hailin bed17f32f9 fix: install bash in Alpine container for Agent SDK shell access
The Claude Agent SDK Bash tool requires a POSIX shell. Alpine only has
busybox ash, causing "No suitable shell found" errors. Install bash
and set SHELL=/bin/bash.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 06:52:23 -08:00
hailin d4391eef97 fix: run services as non-root user for SDK bypassPermissions
SDK blocks bypassPermissions when running as root for security.
Add non-root 'appuser' to Dockerfile.service and update volume
mounts to use /home/appuser/.claude paths.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 06:41:10 -08:00
hailin 04a18a7899 fix: use acceptEdits mode and mount .claude.json for SDK
- bypassPermissions blocked by SDK when running as root
- Switch to acceptEdits with canUseTool for programmatic control
- Mount .claude.json config file into container

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 06:37:31 -08:00
hailin db1d0620f2 debug: add stderr callback to SDK engine for error visibility
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 06:34:42 -08:00
hailin d40f66ce14 fix: use bypassPermissions mode for headless SDK execution
In a Docker container without TTY, permissionMode 'default' blocks
waiting for interactive permission prompts. Switch to bypassPermissions
with canUseTool callback for programmatic risk-based access control.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 06:30:38 -08:00
hailin 3a6f9d9447 fix: mount .claude directory as read-write for SDK debug logs
SDK writes debug logs to ~/.claude/debug/ at runtime.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 06:21:31 -08:00
hailin 14e8d7019a fix: use dynamic import helper for ESM-only claude-agent-sdk
tsc with module=commonjs converts `await import()` to require(),
which breaks ESM-only packages. Use Function('return import()')
workaround to preserve native dynamic import at runtime.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 06:18:04 -08:00
hailin b963b7d4da feat: enable SDK subscription mode with OAuth credentials mount
- Mount ~/.claude/ into agent-service container for OAuth token access
- Switch default engine to claude_agent_sdk
- Remove ANTHROPIC_API_KEY from env in subscription mode so SDK uses OAuth
- Keep API key mode for per-tenant billing

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 06:14:45 -08:00
hailin 9126225317 fix: disable TLS verification for Anthropic proxy (self-signed cert)
Follow iConsulting pattern: set NODE_TLS_REJECT_UNAUTHORIZED=0 when
ANTHROPIC_BASE_URL is configured, enabling connection through the
self-signed proxy at 67.223.119.33.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 05:52:02 -08:00
hailin 810dcd7def feat: switch default engine to claude_api with base URL support
- Change AGENT_ENGINE_TYPE from claude_code_cli to claude_api in docker-compose
- Add ANTHROPIC_BASE_URL env var support to claude-api-engine
- Add ANTHROPIC_BASE_URL to agent-service environment in docker-compose

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 05:45:08 -08:00
hailin 9a1ecf10ec fix: add restart policy, global error handlers, and fix tenant schema bug
- Add restart: unless-stopped to all 12 Docker services
- Add process.on(unhandledRejection/uncaughtException) to all 7 service main.ts
- Fix handleEventTrigger using tenantId UUID as schema name instead of slug lookup
- Wrap Redis event subscription callbacks in try/catch

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 05:30:34 -08:00
hailin 6dcfe7cd9a chore: gitignore iOS 自动生成文件
- ios/Flutter/ephemeral/ — 构建临时文件
- ios/Runner/GeneratedPluginRegistrant.* — 插件注册表

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 05:10:54 -08:00
hailin 4651291468 style: 导航栏去掉蓝色药丸背景,改为图标/文字高亮
- indicatorColor: transparent 去掉 Material 3 默认的选中背景
- 选中项:图标 + 文字改为 primary 紫色,字重 w600
- 未选中项:图标 + 文字灰色 (textSecondary),字重 w400
- 与微信/支付宝/飞书的导航栏风格一致

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 05:09:34 -08:00
hailin 6a84519090 refactor: 移除 AppBar 刷新按钮,统一使用下拉刷新
6 个页面(仪表盘、服务器、任务、告警、审批、常驻指令)
删除右上角 IconButton(Icons.refresh),保留已有的 RefreshIndicator 下拉刷新。
Terminal 页面的刷新按钮是"重新连接"功能,保持不变。

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 05:08:16 -08:00
hailin 1c291ce6c0 fix: Scheduler 用 slug 构建 tenant schema 名
tenant schema 是 it0_t_{slug}(如 it0_t_default),不是 it0_t_{uuid}。

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 05:03:04 -08:00
hailin 09274aa6af fix: Scheduler 查询 public.tenants 而非 it0_shared.tenants
数据库实际 schema 是 public.tenants,不是 it0_shared.tenants。

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 05:00:17 -08:00
hailin 840318f449 fix: Scheduler 缺少 tenant 上下文导致 ops-service 卡死
根因: @Cron 定时任务在 HTTP 请求上下文之外运行,
TenantAwareRepository 需要 AsyncLocalStorage 中的 tenant 信息,
每分钟抛 "Tenant context not initialized" 错误。

修复:
- scanCronOrders: 查 it0_shared.tenants 获取所有活跃租户,
  在 TenantContextService.run() 上下文中逐一执行
- handleEventTrigger: 从 Redis event 中提取 tenantId,
  同样包裹在 TenantContextService.run() 中
- 每个 tenant 循环加 try/catch 防止单个租户出错影响其他

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 04:55:52 -08:00
hailin 666b173906 fix: 根治 Unhandled Exception — async void 拦截器 + 全局错误兜底
根本原因:Dio interceptor 的 onError/onRequest 签名是 void,
标 async 后变成 Future<void> 但没人 await,内部异常全部变成
Unhandled Exception 崩溃。

修复:
- RetryInterceptor: onError 改为同步调度,retry 逻辑移到独立
  _retry() 方法并用 try/catch 包裹全部路径
- DedupInterceptor: 防止 Completer 重复 complete,retry 请求
  跳过去重避免与原始请求冲突
- TokenInterceptor: onRequest 和 onError 的 async lambda 全部
  包裹 try/catch,异常时 fallback 到 handler.next()
- main.dart: 三层全局错误兜底 —
  1) FlutterError.onError 捕获框架错误
  2) PlatformDispatcher.onError 捕获平台通道错误
  3) runZonedGuarded 捕获所有漏网的异步异常
- receiveTimeout/sendTimeout 不再触发重试(服务器已收到请求)
- 超时调整: connect 10s, send 30s, receive 30s
- 仪表盘卡片 IntrinsicHeight 等高对齐

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 04:37:39 -08:00
hailin 68004409a3 fix: 仪表盘概览卡片等高对齐
- Row 外层包 IntrinsicHeight + CrossAxisAlignment.stretch,三卡自动等高
- Loading/Error 卡片去掉固定 height:140,改为 padding + stretch 拉伸

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 04:28:40 -08:00
hailin 4e55e9a616 feat: 补齐大厂级网络层 — 401并发锁、请求去重、结构化错误日志
## 1. TokenRefreshLock(401 并发刷新竞态修复)
- 新增 `core/network/token_refresh_lock.dart`
- 使用 Completer 实现互斥锁:多个请求同时 401 时,
  仅第一个触发 refreshToken(),其余等待同一结果
- 防止 5 个页面同时 401 → 5 次 refresh → 4 次失败踢出用户

## 2. DedupInterceptor(请求去重)
- 新增 `core/network/dedup_interceptor.dart`
- 相同 GET URL 在飞行中时,后续请求复用第一个的结果
- 防止:用户快速点重试、页面切换重复加载、下拉刷新连点
- 仅限 GET,POST/PUT/DELETE 等写操作始终放行

## 3. ErrorLogInterceptor + ErrorLogger(结构化错误日志)
- 新增 `core/network/error_log_interceptor.dart` — Dio 拦截器
- 新增 `core/services/error_logger.dart` — 持久化日志服务
- 每个失败请求记录:时间戳、方法、URL、状态码、错误类型、重试次数
- 本地 SharedPreferences 存储最近 50 条,支持 summary 统计
- debug 模式同步 debugPrint 输出
- 预留 Sentry/Crashlytics flush 接口

## 4. Dio 拦截器管线优化
拦截器顺序调整为大厂标准管线:
1. DedupInterceptor — 去重(最先,防止重复请求进入管线)
2. TokenInterceptor — 注入 token + 401 刷新(带并发锁)
3. TenantInterceptor — 注入 X-Tenant-Id
4. RetryInterceptor — 指数退避重试
5. ErrorLogInterceptor — 错误日志(最后,记录最终失败)

移除 LogInterceptor(被 ErrorLogInterceptor 替代,且不再在
release 模式下打印请求 body 造成性能损耗)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 04:05:53 -08:00
hailin 94652857cd feat: 生产级 API 错误处理 — 重试拦截器、友好错误提示、网络监测、WebSocket 退避
## 问题
用户看到原始 DioException 堆栈(如 "DioException [unknown]: null Error:
HttpException: Connection reset by peer"),且无重试机制,网络抖动即报错。

## 变更

### 1. RetryInterceptor(指数退避自动重试)
- 新增 `core/network/retry_interceptor.dart`
- 自动重试:连接超时、发送超时、Connection reset、502/503/504/429
- 指数退避(800ms → 1.6s → 3.2s)+ 随机抖动防雪崩
- 最多 3 次重试,非瞬态错误(401/403/404)不重试
- 集成到 dio_client,优化超时:connect 8s、send 15s、receive 20s

### 2. ErrorHandler 全面升级(友好中文错误提示)
- 重写 `core/errors/error_handler.dart`,新增 `friendlyMessage()` 静态方法
- 所有 DioExceptionType 映射为具体中文:
  - Connection reset → "连接被服务器重置,请稍后重试"
  - Connection refused → "服务器拒绝连接,请确认服务是否启动"
  - Timeout → "连接超时,服务器无响应"
  - 401 → "登录已过期,请重新登录"
  - 403/404/429/500/502/503 各有独立提示
- 新增 TimeoutFailure 类型
- 所有 Failure.message 默认中文

### 3. 网络连接监测 + 离线 Banner
- 新增 `core/network/connectivity_provider.dart` — 每30秒探测服务器可达性
- 新增 `core/widgets/offline_banner.dart` — 黄色警告横幅 "网络连接不可用"
- 集成到 ScaffoldWithNav,所有页面顶部自动显示离线状态

### 4. 统一错误展示(杜绝 e.toString())
- 新增 `core/widgets/error_view.dart` — 统一错误 UI(图标 + 友好文案 + 重试按钮)
- 替换 6 个页面的内联错误 Column 为 ErrorView:
  tasks_page / servers_page / alerts_page / approvals_page / standing_orders_page
- 替换 dashboard 的 3 处 _SummaryCardError(message: e.toString())
- 替换 4 个 provider 的 e.toString(): chat / auth / settings / approvals
- 全项目零 e.toString() 残留(仅剩 time.minute.toString() 时间格式化)

### 5. WebSocket 重连增强
- 指数退避(1s → 2s → 4s → ... → 60s 上限)+ 随机抖动
- 最多 10 次自动重连,超限后停止
- disconnect() 阻止自动重连
- 新增 reconnect() 手动重连方法

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 04:01:04 -08:00
hailin 1075a6b265 refactor: 重构 chat_page 对接完整架构,集成全部 stream event widget
## 问题
chat_page.dart 包含内联的简化版 ChatMessage/ChatState/ChatNotifier(约180行),
绕过了已实现的完整 Clean Architecture 层:
- domain/entities/chat_message.dart(含 ToolExecution、ApprovalRequest)
- domain/entities/stream_event.dart(9种 sealed class 事件)
- chat_providers.dart(完整 ChatNotifier 支持审批/工具/常驻指令)
- 5 个独立 widget 全部闲置未使用

## 变更
1. 删除内联重复代码(~180行):ChatRole、ChatContentType、内联 ChatMessage、
   内联 ChatState、内联 ChatNotifier、chatMessagesProvider
2. 切换到正式 chatProvider(chat_providers.dart),支持全部 9 种 StreamEvent
3. 集成 5 个已有 widget:
   - MessageBubble — 用户/AI 消息气泡(带时间戳)
   - StreamTextWidget — AI 流式回复动画光标
   - ToolExecutionCard — 工具执行详情(名称/输入/输出/状态/风险等级)
   - ApprovalActionCard — 审批卡片(倒计时/通过/拒绝/过期处理)
   - AgentThinkingIndicator — 思考动画指示器
4. 新增 _AgentStatusBar — 实时状态条(思考中/执行中/等待审批)
5. 新增 _StandingOrderDraftCard — 常驻指令草案渲染
6. AppBar + 输入区添加停止按钮,审批等待时显示提示
7. 消息渲染按 MessageType 分发:text/thinking/toolUse/toolResult/approval/standingOrderDraft

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 03:21:44 -08:00
hailin 15e6fca6c0 fix: translate all remaining English UI strings to Chinese and remove dead code
- Translate approval_action_card (Approve/Reject/Cancel/Expired)
- Translate tool_execution_card status labels (Executing/Completed/Error)
- Translate chat_providers error messages and stream content
- Translate message_bubble "Thinking..." indicator
- Translate terminal page tooltips (Reconnect/Disconnect)
- Translate fallback values (Untitled/Unknown/No message) across all pages
- Translate auth error "Login failed" and stream error messages
- Remove dead voice_providers.dart (used speech_to_text which is not installed)
- Remove dead voice_input_button.dart (not referenced anywhere)
- Fix widget_test.dart (was referencing non-existent MyApp class)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 02:07:57 -08:00
hailin 9f44878fea fix: unify all pages to Chinese + fix bottom nav selected state
1. 所有页面英文文本统一替换为中文(仪表盘、对话、任务、告警、
   服务器、常驻指令、审批、终端、设置)
2. 底部导航栏添加 selectedIndex 追踪当前路由,点击后正确高亮
3. 导航图标添加 outlined/filled 选中态区分
4. 设置页重构为大厂风格(圆角图标分组 + 主题底部弹窗选择)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 01:59:09 -08:00
hailin b76b5246cc fix: align settings page style with other pages using Card containers
设置页改为与 Dashboard/Alerts 等页面一致的卡片分组风格:
每个设置分区用 Card 容器包裹,内部 ListTile 用 Divider(height:1) 分隔,
统一使用 AppColors.surface 卡片背景色,外边距 padding: 16。

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 01:51:42 -08:00
hailin d1993a1175 feat: add auto-login with token restore on app startup
App 启动时从 SecureStorage 读取已存储的 JWT,解析用户信息自动恢复登录状态,
无需每次重新输入密码。Token 过期则自动尝试 refresh,refresh 失败才跳转登录页。

- 新增 tryRestoreSession() 从 JWT payload 解码用户信息
- 新增 _isTokenExpired() 检查 token 是否过期(预留 60s 缓冲)
- refreshToken() 成功后恢复 AuthState + tenant 上下文
- 新增 /splash 启动页,尝试恢复后决定跳转 dashboard 或 login

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 01:47:20 -08:00
hailin 8015154a3e feat: replace default Flutter icon with iAgent robot logo
使用项目自有的绿色机器人 SVG logo 生成各分辨率 Android 启动图标,
替换默认的 Flutter 蓝色纸飞机。支持 Android Adaptive Icon(白底 + 机器人前景)。
同时生成 iOS 图标。

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 01:41:36 -08:00
hailin 3d9b25391e fix: enable core library desugaring for flutter_local_notifications
flutter_local_notifications 插件依赖 java.time API,需要启用 Android core
library desugaring 才能在低版本设备上运行。

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 01:24:38 -08:00
hailin 092a561867 feat: 完成 iAgent App 三大功能 + 修复租户上下文
## 功能一:设置页(完整实现)
- 新增浅色主题(lightTheme),支持深色/浅色/跟随系统三种模式
- app.dart 接入 themeMode 动态切换
- 设置页完整重写:个人信息编辑、修改密码、主题切换、通知开关
- 新增 settings_remote_datasource 对接后端 admin/settings API
- settings_providers 新增 AccountProfileNotifier 管理远程个人资料

## 功能二:语音通话(音频集成)
- 添加 flutter_sound 依赖,创建 PcmPlayer 流式 PCM 播放器
- agent_call_page 替换空壳:真实麦克风采集(record + GTCRN 降噪)
- 真实 PCM 16kHz 流式播放,基于 RMS 能量驱动波形动画
- 修复 WebSocket URL 路径:/ws/voice/ → /api/v1/voice/ws/
- voice_repository_impl 支持后端返回相对路径自动拼接

## 功能三:推送通知(WebSocket MVP)
- 添加 flutter_local_notifications + socket_io_client 依赖
- 创建 AppNotification 实体、NotificationService(Socket.IO 连接 comm-service)
- 通知 providers:列表管理 + 未读计数
- 登录后自动连接通知服务,登出断开
- 底部导航 Alerts 标签添加未读角标(Badge)
- AndroidManifest 添加 POST_NOTIFICATIONS 权限
- main.dart 初始化本地通知插件

## 修复:租户上下文未初始化(500错误)
- 根因:登录后未设置 currentTenantIdProvider,导致 X-Tenant-Id 头缺失
- Flutter 端:login() 成功后从 JWT 设置 tenantId,logout 时清除
- 后端:tenant-context.middleware 增加 JWT tenantId 回退逻辑
- AuthUser 模型新增 tenantId 字段解析

新增 5 个文件,修改 16 个文件,添加 3 个依赖包

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 01:10:52 -08:00