# LiveKit Voice AI - React Native 快速开始

## ✅ 服务状态

```
✅ LiveKit Server: localhost:7880 (Docker)
✅ FunASR STT:     localhost:8080 (SenseVoiceSmall)
✅ Agnes AI LLM:   api.agnes-ai.cn/v1 (agnes-2.5-flash)
✅ Edge TTS:       zh-CN-XiaoxiaoNeural (已启用)
✅ Agent Worker:   已注册 (AW_q969...)
```

## 📱 React Native 测试步骤

### 1. 获取 Mac IP 地址
```bash
ipconfig getifaddr en0
```
记录输出，例如：`192.168.31.100`

### 2. 获取连接 Token
```bash
source ~/s2s-env/bin/activate
python3 /Users/leo/.hermes/workspace/livekit-agents/gen_token.py
```

### 3. 创建 React Native 项目
```bash
npx react-native init VoiceAI
cd VoiceAI
npm install livekit-client
```

### 4. 配置 iOS (允许 HTTP 连接)
编辑 `ios/VoiceAI/Info.plist`，在 `<dict>` 中添加：
```xml
<key>NSAppTransportSecurity</key>
<dict>
  <key>NSAllowsArbitraryLoads</key>
  <true/>
</dict>
```

### 5. 替换 App.js
将以下内容复制到 `App.js`，**记得替换 SERVER_URL 和 TOKEN**：

```javascript
import React from 'react';
import { View, Text, StyleSheet, Button } from 'react-native';
import { LiveKitRoom, useVoiceAssistant } from 'livekit-client';

// ⚠️ 替换为你的 Mac IP (从 ipconfig getifaddr en0 获取)
const SERVER_URL = 'ws://192.168.31.100:7880';
// ⚠️ 替换为 gen_token.py 输出的 token
const TOKEN = 'YOUR_TOKEN_HERE';

export default function App() {
  const { state, start, stop } = useVoiceAssistant();
  
  return (
    <LiveKitRoom
      serverUrl={SERVER_URL}
      token={TOKEN}
      connectOptions={{ autoSubscribe: true }}
    >
      <View style={styles.container}>
        <Text style={styles.title}>🎙️ Voice AI Agent</Text>
        <Text style={styles.status}>状态: {state}</Text>
        <Button title="开始对话" onPress={start} color="#00b4ff" />
        <Button 
          title="停止" 
          onPress={stop} 
          color="#ff6b6b"
          disabled={state === 'idle' || state === 'disconnected'}
        />
      </View>
    </LiveKitRoom>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#080c18',
    alignItems: 'center',
    justifyContent: 'center',
    padding: 20,
  },
  title: {
    fontSize: 28,
    fontWeight: 'bold',
    color: '#00b4ff',
    marginBottom: 20,
  },
  status: {
    fontSize: 18,
    color: '#00ffaa',
    marginBottom: 30,
  },
});
```

### 6. 运行应用
```bash
npx react-native run-ios
# 或
npx react-native run-android
```

## 🧪 测试流程

1. **保持 Agent 运行** (另一终端):
   ```bash
   source ~/s2s-env/bin/activate
   cd /Users/leo/.hermes/workspace/livekit-agents
   python3 agent_server.py start
   ```

2. **启动 React Native App**

3. **点击"开始对话"** - 应显示"已连接"

4. **说话测试** - Agent 应识别语音并回复

## 🔍 故障排查

| 问题 | 解决方案 |
|------|----------|
| Token invalid | 重新运行 `python3 gen_token.py` 获取新 token |
| Connection refused | 确认 Mac IP 正确 (`ipconfig getifaddr en0`) |
| Audio not working | 检查 iOS 麦克风权限设置 |
| No response | 查看 Agent 终端日志 |

## 📊 当前 Token

```
Room: voice-ai-test
Identity: rn-user
Token: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
```

(运行 `python3 gen_token.py` 获取最新 token)
