release: v1.3.1 - Pipeline Queue Integration, OCR Fix, Worker Compatibility
Major Changes: - Pipeline Queue integration completed (internal.file + pipeline.commands) - Fixed old job_worker database compatibility with new command_queue table - Compiled Swift OCR binary (was missing, causing empty OCR results) - Added deep scan functionality through re-scan API Fixes: - Changed CommandJob.id from i32 to i64 for database compatibility - Added UUID generation in create_command (message_id field) - Fixed claim_pending_commands to query queue_name = 'internal.file' - Fixed field mappings: command_id → message_id, job_type → command_type - Added support for 'processing' status in CommandJobStatus Queue System: - internal.file queue: for register/unregister/re-scan commands (job_worker) - pipeline.commands queue: for vectorize/store_asrx/etc commands (pipeline_worker) Documentation: - Added comprehensive queue system documentation - Created M4 workspace status reports - Updated AGENTS.md with queue system details
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,351 @@
|
||||
# Studio 集成 Agent Search 文件清单
|
||||
|
||||
**日期**: 2026-10-11
|
||||
**状态**: 实施清单
|
||||
|
||||
---
|
||||
|
||||
## 📁 需要创建的文件
|
||||
|
||||
### Studio 前端文件
|
||||
|
||||
| 文件路径 | 用途 | 优先级 |
|
||||
|---------|------|:------:|
|
||||
| `src/components/AgentSearch/AgentSearchBar.tsx` | 搜索栏组件 | P0 |
|
||||
| `src/components/AgentSearch/AgentSearchResults.tsx` | 搜索结果组件 | P0 |
|
||||
| `src/components/AgentSearch/MoreButton.tsx` | More 按钮组件 | P0 |
|
||||
| `src/components/AgentSearch/AgentSearch.tsx` | 主搜索组件 | P0 |
|
||||
| `src/components/AgentSearch/AgentSearch.css` | 样式文件 | P0 |
|
||||
| `src/services/agent-search.ts` | API 服务 | P0 |
|
||||
| `src/types/agent-search.ts` | TypeScript 类型 | P0 |
|
||||
| `src/pages/SearchPage.tsx` | 搜索页面 | P1 |
|
||||
|
||||
---
|
||||
|
||||
## 🔧 需要修改的文件
|
||||
|
||||
### Studio 前端
|
||||
|
||||
| 文件路径 | 修改内容 | 优先级 |
|
||||
|---------|---------|:------:|
|
||||
| `src/App.tsx` | 添加路由 | P0 |
|
||||
| `.env.local` | 添加环境变量 | P0 |
|
||||
| `package.json` | 安装依赖(axios, lucide-react) | P0 |
|
||||
|
||||
---
|
||||
|
||||
## 📚 参考文档
|
||||
|
||||
| 文档路径 | 用途 |
|
||||
|---------|------|
|
||||
| `docs_v1.0/GUIDES/Studio_Agent_Search_Integration_Guide.md` | 完整集成指南 |
|
||||
| `docs_v1.0/GUIDES/Studio_Quick_Start.md` | 快速开始指南 |
|
||||
| `docs_v1.0/DESIGN/Agent_Search_Interface.md` | API 设计文档 |
|
||||
| `docs_v1.0/DESIGN/Agent_Search_Pagination.md` | 分页设计文档 |
|
||||
|
||||
---
|
||||
|
||||
## 📋 实施步骤
|
||||
|
||||
### Phase 1: 最小实现(1 天)
|
||||
|
||||
- [ ] 创建 `types/agent-search.ts`
|
||||
- [ ] 创建 `services/agent-search.ts`
|
||||
- [ ] 创建 `components/AgentSearch/AgentSearch.tsx`
|
||||
- [ ] 创建 `components/AgentSearch/AgentSearch.css`
|
||||
- [ ] 修改 `App.tsx` 添加路由
|
||||
- [ ] 测试基本搜索功能
|
||||
|
||||
---
|
||||
|
||||
### Phase 2: 完整 UI(1 天)
|
||||
|
||||
- [ ] 创建 `AgentSearchBar.tsx`
|
||||
- [ ] 创建 `AgentSearchResults.tsx`
|
||||
- [ ] 创建 `MoreButton.tsx`
|
||||
- [ ] 完善 CSS 样式
|
||||
- [ ] 添加响应式布局
|
||||
|
||||
---
|
||||
|
||||
### Phase 3: 后端集成(2-3 天)
|
||||
|
||||
- [ ] 实现 `/api/v1/agent/search` API
|
||||
- [ ] 实现 `/api/v1/agent/search/more` API
|
||||
- [ ] 实现 Visual Search Worker
|
||||
- [ ] 实现会话管理
|
||||
- [ ] 端到端测试
|
||||
|
||||
---
|
||||
|
||||
## 🎨 文件模板
|
||||
|
||||
### 1. TypeScript 类型 (最小版本)
|
||||
|
||||
```typescript
|
||||
// src/types/agent-search.ts
|
||||
export interface SearchResult {
|
||||
id: string;
|
||||
score: number;
|
||||
file_uuid: string;
|
||||
thumbnail?: string;
|
||||
snippet?: string;
|
||||
}
|
||||
|
||||
export interface AgentSearchResponse {
|
||||
results: SearchResult[];
|
||||
total_count: number;
|
||||
has_more: boolean;
|
||||
session_id: string;
|
||||
summary: string;
|
||||
next_offset?: number;
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 2. API 服务 (最小版本)
|
||||
|
||||
```typescript
|
||||
// src/services/agent-search.ts
|
||||
import axios from 'axios';
|
||||
|
||||
const api = axios.create({
|
||||
baseURL: process.env.REACT_APP_API_URL || 'http://localhost:3002',
|
||||
headers: {
|
||||
'Authorization': `Bearer ${process.env.REACT_APP_API_KEY}`
|
||||
}
|
||||
});
|
||||
|
||||
export const agentSearchService = {
|
||||
search: async (query: string) => {
|
||||
const { data } = await api.post('/api/v1/search/smart', {
|
||||
query,
|
||||
limit: 5
|
||||
});
|
||||
return {
|
||||
results: data.results,
|
||||
total_count: data.results.length,
|
||||
has_more: false,
|
||||
session_id: 'temp',
|
||||
summary: `Found ${data.results.length} results`
|
||||
};
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 3. 组件 (最小版本)
|
||||
|
||||
```typescript
|
||||
// src/components/AgentSearch/AgentSearch.tsx
|
||||
import React, { useState } from 'react';
|
||||
import { agentSearchService } from '../../services/agent-search';
|
||||
import './AgentSearch.css';
|
||||
|
||||
export const AgentSearch = () => {
|
||||
const [query, setQuery] = useState('');
|
||||
const [results, setResults] = useState([]);
|
||||
const [loading, setLoading] = useState(false);
|
||||
|
||||
const handleSearch = async () => {
|
||||
setLoading(true);
|
||||
try {
|
||||
const data = await agentSearchService.search(query);
|
||||
setResults(data.results);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="agent-search">
|
||||
<input
|
||||
value={query}
|
||||
onChange={(e) => setQuery(e.target.value)}
|
||||
placeholder="Search videos..."
|
||||
/>
|
||||
<button onClick={handleSearch} disabled={loading}>
|
||||
{loading ? 'Searching...' : 'Search'}
|
||||
</button>
|
||||
<div className="results">
|
||||
{results.map((r, i) => (
|
||||
<div key={i} className="result">
|
||||
<p>{r.snippet}</p>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 4. CSS (最小版本)
|
||||
|
||||
```css
|
||||
/* src/components/AgentSearch/AgentSearch.css */
|
||||
.agent-search {
|
||||
max-width: 800px;
|
||||
margin: 20px auto;
|
||||
}
|
||||
|
||||
.agent-search input {
|
||||
width: 100%;
|
||||
padding: 12px;
|
||||
font-size: 16px;
|
||||
margin-bottom: 16px;
|
||||
}
|
||||
|
||||
.agent-search button {
|
||||
padding: 8px 16px;
|
||||
background: #4a90e2;
|
||||
color: white;
|
||||
border: none;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.results {
|
||||
margin-top: 20px;
|
||||
}
|
||||
|
||||
.result {
|
||||
border: 1px solid #ddd;
|
||||
padding: 12px;
|
||||
margin-bottom: 12px;
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 5. 路由配置
|
||||
|
||||
```typescript
|
||||
// src/App.tsx (添加路由)
|
||||
import { AgentSearch } from './components/AgentSearch/AgentSearch';
|
||||
|
||||
// 在 Routes 中添加
|
||||
<Route path="/search" element={<AgentSearch />} />
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 6. 环境变量
|
||||
|
||||
```env
|
||||
# .env.local
|
||||
REACT_APP_API_URL=http://localhost:3002
|
||||
REACT_APP_API_KEY=muser_demo_key_32chars_abcdef1234567890
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## ✅ 测试清单
|
||||
|
||||
### 最小实现测试
|
||||
|
||||
- [ ] 输入查询
|
||||
- [ ] 点击搜索
|
||||
- [ ] 显示结果
|
||||
- [ ] 样式正确
|
||||
|
||||
---
|
||||
|
||||
### 完整功能测试
|
||||
|
||||
- [ ] Top 5 结果显示
|
||||
- [ ] More 按钮
|
||||
- [ ] 追加 10 个结果
|
||||
- [ ] 响应式布局
|
||||
- [ ] 加载状态
|
||||
- [ ] 错误处理
|
||||
|
||||
---
|
||||
|
||||
## 🚀 部署清单
|
||||
|
||||
### 开发环境
|
||||
|
||||
- [ ] 创建所有文件
|
||||
- [ ] 安装依赖
|
||||
- [ ] 配置环境变量
|
||||
- [ ] 测试功能
|
||||
|
||||
---
|
||||
|
||||
### 生产环境
|
||||
|
||||
- [ ] 构建前端
|
||||
- [ ] 配置生产环境变量
|
||||
- [ ] 部署到服务器
|
||||
- [ ] 测试线上功能
|
||||
|
||||
---
|
||||
|
||||
## 📦 依赖清单
|
||||
|
||||
```json
|
||||
{
|
||||
"dependencies": {
|
||||
"axios": "^1.6.0",
|
||||
"lucide-react": "^0.294.0"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 📝 注意事项
|
||||
|
||||
### 开发时
|
||||
|
||||
1. **使用临时 API**: 后端未实现时使用 `/api/v1/search/smart`
|
||||
2. **测试数据**: 确保有足够的测试视频
|
||||
3. **错误处理**: 添加友好的错误提示
|
||||
|
||||
---
|
||||
|
||||
### 部署时
|
||||
|
||||
1. **环境变量**: 确保生产环境变量正确
|
||||
2. **API Key**: 使用生产环境 API Key
|
||||
3. **性能优化**: 开启 Gzip、CDN 等
|
||||
|
||||
---
|
||||
|
||||
## 🆘 故障排除
|
||||
|
||||
### 问题 1: API 返回 404
|
||||
|
||||
**解决方案**: 检查 API URL 和端点是否正确
|
||||
|
||||
```bash
|
||||
curl http://localhost:3002/health
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 问题 2: 认证失败
|
||||
|
||||
**解决方案**: 检查 API Key 是否正确
|
||||
|
||||
```bash
|
||||
curl -H "Authorization: Bearer YOUR_KEY" \
|
||||
http://localhost:3002/api/v1/search/smart
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 问题 3: 无结果显示
|
||||
|
||||
**解决方案**: 检查数据库是否有数据
|
||||
|
||||
```bash
|
||||
psql -U accusys -d momentry -c "SELECT COUNT(*) FROM videos;"
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
**文件清单完成!按此清单实施即可完成集成。**
|
||||
@@ -0,0 +1,837 @@
|
||||
# Momentry Studio - Pipeline Queue 集成指南
|
||||
|
||||
**版本**: 1.0
|
||||
**日期**: 2026-10-11
|
||||
**适用**: Momentry Studio (Vue 3 + Tauri 2)
|
||||
|
||||
---
|
||||
|
||||
## 📋 概述
|
||||
|
||||
Momentry Core 已将所有 Pipeline API 改为异步队列模式。本指南说明 Studio 如何适配这些变化。
|
||||
|
||||
### 主要变化
|
||||
|
||||
| 之前 | 现在 | 影响 |
|
||||
|------|------|------|
|
||||
| 同步等待 60-120s | 立即返回 50ms | ✅ 用户体验提升 |
|
||||
| 无进度显示 | 可查询进度 | ✅ 可显示进度条 |
|
||||
| 单任务处理 | 队列并行 | ✅ 性能提升 |
|
||||
|
||||
---
|
||||
|
||||
## 🔌 API 变更
|
||||
|
||||
### 1. 单个命令提交
|
||||
|
||||
**端点**: `POST /api/v1/file/:file_uuid/vectorize`
|
||||
|
||||
**之前** (同步阻塞):
|
||||
```typescript
|
||||
// ❌ 旧方式 - 阻塞 120 秒
|
||||
const response = await fetch('/api/v1/file/xxx/vectorize')
|
||||
const data = await response.json()
|
||||
// 120 秒后才返回
|
||||
```
|
||||
|
||||
**现在** (异步队列):
|
||||
```typescript
|
||||
// ✅ 新方式 - 立即返回
|
||||
const response = await fetch('/api/v1/file/xxx/vectorize?api_key=YOUR_KEY', {
|
||||
method: 'POST'
|
||||
})
|
||||
const data = await response.json()
|
||||
// 立即返回 (50ms)
|
||||
// {
|
||||
// success: true,
|
||||
// command_id: "abc-123",
|
||||
// file_uuid: "xxx",
|
||||
// status: "queued",
|
||||
// estimated_duration_sec: 120
|
||||
// }
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 2. 命令状态查询
|
||||
|
||||
**新端点**: `GET /api/v1/pipeline/status/:command_id`
|
||||
|
||||
```typescript
|
||||
const response = await fetch('/api/v1/pipeline/status/abc-123?api_key=YOUR_KEY')
|
||||
const data = await response.json()
|
||||
// {
|
||||
// command_id: "abc-123",
|
||||
// status: "pending" | "processing" | "completed" | "failed",
|
||||
// processing_started_at: "2026-10-11T10:00:00Z",
|
||||
// processing_completed_at: null,
|
||||
// last_error: null
|
||||
// }
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 3. 完整 Pipeline 提交
|
||||
|
||||
**新端点**: `POST /api/v1/file/:file_uuid/pipeline`
|
||||
|
||||
```typescript
|
||||
const response = await fetch('/api/v1/file/xxx/pipeline?api_key=YOUR_KEY', {
|
||||
method: 'POST'
|
||||
})
|
||||
const data = await response.json()
|
||||
// {
|
||||
// success: true,
|
||||
// file_uuid: "xxx",
|
||||
// commands_queued: 5,
|
||||
// command_ids: ["id1", "id2", "id3", "id4", "id5"],
|
||||
// estimated_total_duration_sec: 150,
|
||||
// status: "queued"
|
||||
// }
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 💻 Vue 3 集成示例
|
||||
|
||||
### 1. API 服务封装
|
||||
|
||||
创建文件: `src/services/pipelineService.ts`
|
||||
|
||||
```typescript
|
||||
// src/services/pipelineService.ts
|
||||
|
||||
const API_BASE = 'http://localhost:3002/api/v1'
|
||||
const API_KEY = 'YOUR_API_KEY'
|
||||
|
||||
export interface QueueSubmitResponse {
|
||||
success: boolean
|
||||
command_id: string
|
||||
file_uuid: string
|
||||
status: string
|
||||
estimated_duration_sec: number
|
||||
}
|
||||
|
||||
export interface CommandStatusResponse {
|
||||
command_id: string
|
||||
status: 'pending' | 'processing' | 'completed' | 'failed'
|
||||
processing_started_at?: string
|
||||
processing_completed_at?: string
|
||||
last_error?: string
|
||||
}
|
||||
|
||||
export interface FullPipelineResponse {
|
||||
success: boolean
|
||||
file_uuid: string
|
||||
commands_queued: number
|
||||
command_ids: string[]
|
||||
estimated_total_duration_sec: number
|
||||
status: string
|
||||
}
|
||||
|
||||
class PipelineService {
|
||||
private async post<T>(endpoint: string): Promise<T> {
|
||||
const response = await fetch(`${API_BASE}${endpoint}?api_key=${API_KEY}`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
})
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`API error: ${response.statusText}`)
|
||||
}
|
||||
|
||||
return response.json()
|
||||
}
|
||||
|
||||
private async get<T>(endpoint: string): Promise<T> {
|
||||
const response = await fetch(`${API_BASE}${endpoint}?api_key=${API_KEY}`)
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`API error: ${response.statusText}`)
|
||||
}
|
||||
|
||||
return response.json()
|
||||
}
|
||||
|
||||
/**
|
||||
* 提交向量化命令
|
||||
*/
|
||||
async vectorizeChunks(fileUuid: string): Promise<QueueSubmitResponse> {
|
||||
return this.post<QueueSubmitResponse>(`/file/${fileUuid}/vectorize`)
|
||||
}
|
||||
|
||||
/**
|
||||
* 提交 Rule1 命令
|
||||
*/
|
||||
async executeRule1(fileUuid: string): Promise<QueueSubmitResponse> {
|
||||
return this.post<QueueSubmitResponse>(`/file/${fileUuid}/rule1`)
|
||||
}
|
||||
|
||||
/**
|
||||
* 提交 Phase1 命令
|
||||
*/
|
||||
async executePhase1(fileUuid: string): Promise<QueueSubmitResponse> {
|
||||
return this.post<QueueSubmitResponse>(`/file/${fileUuid}/phase1`)
|
||||
}
|
||||
|
||||
/**
|
||||
* 提交标记完成命令
|
||||
*/
|
||||
async markComplete(fileUuid: string): Promise<QueueSubmitResponse> {
|
||||
return this.post<QueueSubmitResponse>(`/file/${fileUuid}/complete`)
|
||||
}
|
||||
|
||||
/**
|
||||
* 提交完整 Pipeline 流程
|
||||
*/
|
||||
async submitFullPipeline(fileUuid: string): Promise<FullPipelineResponse> {
|
||||
return this.post<FullPipelineResponse>(`/file/${fileUuid}/pipeline`)
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询命令状态
|
||||
*/
|
||||
async getCommandStatus(commandId: string): Promise<CommandStatusResponse> {
|
||||
return this.get<CommandStatusResponse>(`/pipeline/status/${commandId}`)
|
||||
}
|
||||
|
||||
/**
|
||||
* 轮询命令状态直到完成
|
||||
*/
|
||||
async pollCommandStatus(
|
||||
commandId: string,
|
||||
onProgress?: (status: CommandStatusResponse) => void,
|
||||
intervalMs: number = 2000,
|
||||
timeoutMs: number = 300000 // 5 分钟
|
||||
): Promise<CommandStatusResponse> {
|
||||
const startTime = Date.now()
|
||||
|
||||
while (Date.now() - startTime < timeoutMs) {
|
||||
const status = await this.getCommandStatus(commandId)
|
||||
|
||||
if (onProgress) {
|
||||
onProgress(status)
|
||||
}
|
||||
|
||||
if (status.status === 'completed' || status.status === 'failed') {
|
||||
return status
|
||||
}
|
||||
|
||||
await new Promise(resolve => setTimeout(resolve, intervalMs))
|
||||
}
|
||||
|
||||
throw new Error('Command polling timeout')
|
||||
}
|
||||
}
|
||||
|
||||
export const pipelineService = new PipelineService()
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 2. Vue Composable
|
||||
|
||||
创建文件: `src/composables/usePipeline.ts`
|
||||
|
||||
```typescript
|
||||
// src/composables/usePipeline.ts
|
||||
|
||||
import { ref, computed } from 'vue'
|
||||
import { pipelineService, type CommandStatusResponse } from '@/services/pipelineService'
|
||||
|
||||
export function usePipeline() {
|
||||
const isSubmitting = ref(false)
|
||||
const currentCommandId = ref<string | null>(null)
|
||||
const commandStatus = ref<CommandStatusResponse | null>(null)
|
||||
const error = ref<string | null>(null)
|
||||
|
||||
const statusText = computed(() => {
|
||||
if (!commandStatus.value) return ''
|
||||
|
||||
const status = commandStatus.value.status
|
||||
switch (status) {
|
||||
case 'pending':
|
||||
return '等待处理'
|
||||
case 'processing':
|
||||
return '处理中'
|
||||
case 'completed':
|
||||
return '已完成'
|
||||
case 'failed':
|
||||
return '失败'
|
||||
default:
|
||||
return status
|
||||
}
|
||||
})
|
||||
|
||||
const isProcessing = computed(() => {
|
||||
return commandStatus.value?.status === 'pending' ||
|
||||
commandStatus.value?.status === 'processing'
|
||||
})
|
||||
|
||||
/**
|
||||
* 提交向量化并监控进度
|
||||
*/
|
||||
async function vectorizeWithProgress(fileUuid: string): Promise<void> {
|
||||
isSubmitting.value = true
|
||||
error.value = null
|
||||
|
||||
try {
|
||||
// 提交命令
|
||||
const response = await pipelineService.vectorizeChunks(fileUuid)
|
||||
currentCommandId.value = response.command_id
|
||||
|
||||
console.log(`Command queued: ${response.command_id}`)
|
||||
console.log(`Estimated duration: ${response.estimated_duration_sec}s`)
|
||||
|
||||
// 轮询状态
|
||||
const finalStatus = await pipelineService.pollCommandStatus(
|
||||
response.command_id,
|
||||
(status) => {
|
||||
commandStatus.value = status
|
||||
console.log(`Status update: ${status.status}`)
|
||||
}
|
||||
)
|
||||
|
||||
if (finalStatus.status === 'failed') {
|
||||
error.value = finalStatus.last_error || 'Pipeline failed'
|
||||
}
|
||||
|
||||
} catch (err) {
|
||||
error.value = err instanceof Error ? err.message : 'Unknown error'
|
||||
console.error('Pipeline error:', err)
|
||||
} finally {
|
||||
isSubmitting.value = false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 提交完整 Pipeline
|
||||
*/
|
||||
async function submitFullPipeline(fileUuid: string): Promise<string[]> {
|
||||
isSubmitting.value = true
|
||||
error.value = null
|
||||
|
||||
try {
|
||||
const response = await pipelineService.submitFullPipeline(fileUuid)
|
||||
console.log(`${response.commands_queued} commands queued`)
|
||||
console.log(`Estimated total duration: ${response.estimated_total_duration_sec}s`)
|
||||
|
||||
return response.command_ids
|
||||
|
||||
} catch (err) {
|
||||
error.value = err instanceof Error ? err.message : 'Unknown error'
|
||||
throw err
|
||||
} finally {
|
||||
isSubmitting.value = false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询命令状态
|
||||
*/
|
||||
async function checkStatus(commandId: string): Promise<void> {
|
||||
try {
|
||||
commandStatus.value = await pipelineService.getCommandStatus(commandId)
|
||||
} catch (err) {
|
||||
error.value = err instanceof Error ? err.message : 'Unknown error'
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
// State
|
||||
isSubmitting,
|
||||
currentCommandId,
|
||||
commandStatus,
|
||||
error,
|
||||
|
||||
// Computed
|
||||
statusText,
|
||||
isProcessing,
|
||||
|
||||
// Methods
|
||||
vectorizeWithProgress,
|
||||
submitFullPipeline,
|
||||
checkStatus
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 3. Vue 组件示例
|
||||
|
||||
创建文件: `src/components/PipelineProgress.vue`
|
||||
|
||||
```vue
|
||||
<!-- src/components/PipelineProgress.vue -->
|
||||
|
||||
<template>
|
||||
<div class="pipeline-progress">
|
||||
<!-- 提交按钮 -->
|
||||
<button
|
||||
@click="handleVectorize"
|
||||
:disabled="isSubmitting || isProcessing"
|
||||
class="btn btn-primary"
|
||||
>
|
||||
{{ isSubmitting ? '提交中...' : isProcessing ? '处理中...' : '开始向量化' }}
|
||||
</button>
|
||||
|
||||
<!-- 进度显示 -->
|
||||
<div v-if="currentCommandId" class="mt-4">
|
||||
<div class="flex items-center gap-2">
|
||||
<span class="font-medium">状态:</span>
|
||||
<span
|
||||
:class="{
|
||||
'text-yellow-600': commandStatus?.status === 'pending',
|
||||
'text-blue-600': commandStatus?.status === 'processing',
|
||||
'text-green-600': commandStatus?.status === 'completed',
|
||||
'text-red-600': commandStatus?.status === 'failed'
|
||||
}"
|
||||
>
|
||||
{{ statusText }}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<div class="mt-2 text-sm text-gray-500">
|
||||
命令 ID: {{ currentCommandId }}
|
||||
</div>
|
||||
|
||||
<!-- 处理时间 -->
|
||||
<div v-if="commandStatus?.processing_started_at" class="mt-2 text-sm">
|
||||
开始时间: {{ formatTime(commandStatus.processing_started_at) }}
|
||||
</div>
|
||||
|
||||
<div v-if="commandStatus?.processing_completed_at" class="mt-2 text-sm">
|
||||
完成时间: {{ formatTime(commandStatus.processing_completed_at) }}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 错误信息 -->
|
||||
<div v-if="error" class="mt-4 p-3 bg-red-50 border border-red-200 rounded">
|
||||
<p class="text-red-600 text-sm">{{ error }}</p>
|
||||
</div>
|
||||
|
||||
<!-- 成功信息 -->
|
||||
<div v-if="commandStatus?.status === 'completed'" class="mt-4 p-3 bg-green-50 border border-green-200 rounded">
|
||||
<p class="text-green-600 text-sm">✓ 向量化完成</p>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { usePipeline } from '@/composables/usePipeline'
|
||||
|
||||
const props = defineProps<{
|
||||
fileUuid: string
|
||||
}>()
|
||||
|
||||
const {
|
||||
isSubmitting,
|
||||
currentCommandId,
|
||||
commandStatus,
|
||||
error,
|
||||
statusText,
|
||||
isProcessing,
|
||||
vectorizeWithProgress
|
||||
} = usePipeline()
|
||||
|
||||
async function handleVectorize() {
|
||||
await vectorizeWithProgress(props.fileUuid)
|
||||
}
|
||||
|
||||
function formatTime(isoString: string): string {
|
||||
return new Date(isoString).toLocaleString('zh-TW')
|
||||
}
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
.pipeline-progress {
|
||||
padding: 1rem;
|
||||
}
|
||||
</style>
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 4. 文件列表视图集成
|
||||
|
||||
修改文件: `src/views/FilesView.vue`
|
||||
|
||||
```vue
|
||||
<!-- src/views/FilesView.vue - 部分代码 -->
|
||||
|
||||
<template>
|
||||
<div class="files-view">
|
||||
<!-- 文件列表 -->
|
||||
<div v-for="file in files" :key="file.uuid" class="file-item">
|
||||
<div class="file-info">
|
||||
<span class="file-name">{{ file.name }}</span>
|
||||
<span class="file-status">{{ getFileStatus(file) }}</span>
|
||||
</div>
|
||||
|
||||
<!-- Pipeline 进度组件 -->
|
||||
<PipelineProgress
|
||||
v-if="file.showProgress"
|
||||
:file-uuid="file.uuid"
|
||||
@close="file.showProgress = false"
|
||||
/>
|
||||
|
||||
<!-- 操作按钮 -->
|
||||
<div class="file-actions">
|
||||
<button
|
||||
@click="startPipeline(file)"
|
||||
:disabled="file.isProcessing"
|
||||
class="btn btn-primary"
|
||||
>
|
||||
{{ file.isProcessing ? '处理中' : '开始处理' }}
|
||||
</button>
|
||||
|
||||
<button
|
||||
v-if="file.commandId"
|
||||
@click="checkPipelineStatus(file)"
|
||||
class="btn btn-secondary"
|
||||
>
|
||||
查看状态
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { ref } from 'vue'
|
||||
import PipelineProgress from '@/components/PipelineProgress.vue'
|
||||
import { pipelineService } from '@/services/pipelineService'
|
||||
|
||||
interface File {
|
||||
uuid: string
|
||||
name: string
|
||||
isProcessing: boolean
|
||||
showProgress: boolean
|
||||
commandId?: string
|
||||
}
|
||||
|
||||
const files = ref<File[]>([
|
||||
// 从 API 加载文件列表
|
||||
])
|
||||
|
||||
async function startPipeline(file: File) {
|
||||
file.showProgress = true
|
||||
file.isProcessing = true
|
||||
|
||||
try {
|
||||
const response = await pipelineService.submitFullPipeline(file.uuid)
|
||||
file.commandId = response.command_ids[0]
|
||||
|
||||
console.log(`Pipeline started for ${file.name}`)
|
||||
console.log(`Commands: ${response.commands_queued}`)
|
||||
console.log(`Estimated: ${response.estimated_total_duration_sec}s`)
|
||||
|
||||
} catch (error) {
|
||||
console.error('Failed to start pipeline:', error)
|
||||
file.isProcessing = false
|
||||
}
|
||||
}
|
||||
|
||||
async function checkPipelineStatus(file: File) {
|
||||
if (!file.commandId) return
|
||||
|
||||
try {
|
||||
const status = await pipelineService.getCommandStatus(file.commandId)
|
||||
console.log(`Status: ${status.status}`)
|
||||
|
||||
if (status.status === 'completed') {
|
||||
file.isProcessing = false
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.error('Failed to check status:', error)
|
||||
}
|
||||
}
|
||||
|
||||
function getFileStatus(file: File): string {
|
||||
if (file.isProcessing) return '处理中'
|
||||
if (file.commandId) return '已完成'
|
||||
return '未处理'
|
||||
}
|
||||
</script>
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 📱 Tauri 特定配置
|
||||
|
||||
### 1. 更新 API 配置
|
||||
|
||||
修改文件: `src/config/api.ts`
|
||||
|
||||
```typescript
|
||||
// src/config/api.ts
|
||||
|
||||
export const API_CONFIG = {
|
||||
BASE_URL: 'http://localhost:3002/api/v1',
|
||||
API_KEY: import.meta.env.VITE_API_KEY || '',
|
||||
TIMEOUT: 5000, // 5 seconds for quick API calls
|
||||
POLL_INTERVAL: 2000, // 2 seconds for status polling
|
||||
POLL_TIMEOUT: 300000 // 5 minutes max
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 2. 环境变量
|
||||
|
||||
创建文件: `.env`
|
||||
|
||||
```env
|
||||
VITE_API_KEY=muser_68600856036340bcafc01930eb4bd839_1774418104_97221b69
|
||||
VITE_API_BASE_URL=http://localhost:3002/api/v1
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 3. 错误处理中间件
|
||||
|
||||
创建文件: `src/middleware/apiError.ts`
|
||||
|
||||
```typescript
|
||||
// src/middleware/apiError.ts
|
||||
|
||||
import { ref } from 'vue'
|
||||
|
||||
export interface ApiError {
|
||||
message: string
|
||||
code?: string
|
||||
timestamp: Date
|
||||
}
|
||||
|
||||
const errors = ref<ApiError[]>([])
|
||||
|
||||
export function useApiError() {
|
||||
function addError(message: string, code?: string) {
|
||||
errors.value.push({
|
||||
message,
|
||||
code,
|
||||
timestamp: new Date()
|
||||
})
|
||||
|
||||
// 显示错误通知
|
||||
console.error(`API Error: ${message}`)
|
||||
}
|
||||
|
||||
function clearErrors() {
|
||||
errors.value = []
|
||||
}
|
||||
|
||||
function getRecentErrors(count: number = 5): ApiError[] {
|
||||
return errors.value.slice(-count)
|
||||
}
|
||||
|
||||
return {
|
||||
errors,
|
||||
addError,
|
||||
clearErrors,
|
||||
getRecentErrors
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 🎯 最佳实践
|
||||
|
||||
### 1. 状态管理
|
||||
|
||||
**推荐**: 使用 Pinia 管理全局 Pipeline 状态
|
||||
|
||||
```typescript
|
||||
// src/stores/pipeline.ts
|
||||
|
||||
import { defineStore } from 'pinia'
|
||||
import { pipelineService, type CommandStatusResponse } from '@/services/pipelineService'
|
||||
|
||||
export const usePipelineStore = defineStore('pipeline', {
|
||||
state: () => ({
|
||||
commands: new Map<string, CommandStatusResponse>(),
|
||||
isPolling: false
|
||||
}),
|
||||
|
||||
actions: {
|
||||
async pollCommand(commandId: string) {
|
||||
const status = await pipelineService.getCommandStatus(commandId)
|
||||
this.commands.set(commandId, status)
|
||||
return status
|
||||
},
|
||||
|
||||
startPolling(commandIds: string[]) {
|
||||
if (this.isPolling) return
|
||||
|
||||
this.isPolling = true
|
||||
|
||||
const interval = setInterval(async () => {
|
||||
for (const id of commandIds) {
|
||||
const status = await this.pollCommand(id)
|
||||
|
||||
if (status.status === 'completed' || status.status === 'failed') {
|
||||
commandIds = commandIds.filter(i => i !== id)
|
||||
}
|
||||
}
|
||||
|
||||
if (commandIds.length === 0) {
|
||||
clearInterval(interval)
|
||||
this.isPolling = false
|
||||
}
|
||||
}, 2000)
|
||||
}
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 2. 用户体验优化
|
||||
|
||||
**显示进度条**:
|
||||
```vue
|
||||
<template>
|
||||
<div class="progress-bar">
|
||||
<div
|
||||
class="progress-fill"
|
||||
:style="{ width: progress + '%' }"
|
||||
></div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup>
|
||||
const progress = computed(() => {
|
||||
if (!commandStatus.value) return 0
|
||||
|
||||
switch (commandStatus.value.status) {
|
||||
case 'pending':
|
||||
return 10
|
||||
case 'processing':
|
||||
return 50
|
||||
case 'completed':
|
||||
return 100
|
||||
case 'failed':
|
||||
return 0
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
})
|
||||
</script>
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 3. 错误处理
|
||||
|
||||
**统一错误处理**:
|
||||
```typescript
|
||||
async function safeApiCall<T>(
|
||||
apiCall: () => Promise<T>,
|
||||
errorMessage: string
|
||||
): Promise<T | null> {
|
||||
try {
|
||||
return await apiCall()
|
||||
} catch (error) {
|
||||
console.error(errorMessage, error)
|
||||
|
||||
// 显示错误通知
|
||||
showErrorToast(`${errorMessage}: ${error.message}`)
|
||||
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
// 使用
|
||||
const status = await safeApiCall(
|
||||
() => pipelineService.getCommandStatus(commandId),
|
||||
'获取命令状态失败'
|
||||
)
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 📝 迁移清单
|
||||
|
||||
### 需要修改的文件
|
||||
|
||||
| 文件 | 修改内容 | 优先级 |
|
||||
|------|---------|:------:|
|
||||
| `src/services/api.ts` | 更新 API 调用方式 | 高 |
|
||||
| `src/components/VideoProcess.vue` | 改用异步队列 API | 高 |
|
||||
| `src/views/FilesView.vue` | 添加进度显示 | 高 |
|
||||
| `src/stores/pipeline.ts` | 新增 Pipeline 状态管理 | 中 |
|
||||
| `src/composables/usePipeline.ts` | 新增 Pipeline composable | 中 |
|
||||
| `src/config/api.ts` | 更新 API 配置 | 低 |
|
||||
|
||||
---
|
||||
|
||||
## ✅ 验证步骤
|
||||
|
||||
1. **API 连接测试**
|
||||
```bash
|
||||
curl http://localhost:3002/api/v1/health
|
||||
```
|
||||
|
||||
2. **命令提交测试**
|
||||
```bash
|
||||
curl -X POST "http://localhost:3002/api/v1/file/xxx/vectorize?api_key=YOUR_KEY"
|
||||
```
|
||||
|
||||
3. **状态查询测试**
|
||||
```bash
|
||||
curl "http://localhost:3002/api/v1/pipeline/status/COMMAND_ID?api_key=YOUR_KEY"
|
||||
```
|
||||
|
||||
4. **UI 测试**
|
||||
- 提交 Pipeline 命令
|
||||
- 检查状态显示
|
||||
- 验证进度更新
|
||||
- 测试错误处理
|
||||
|
||||
---
|
||||
|
||||
## 🚀 部署流程
|
||||
|
||||
1. **更新 Studio 代码**
|
||||
- 复制 service 文件
|
||||
- 复制 composable 文件
|
||||
- 更新组件代码
|
||||
|
||||
2. **配置环境变量**
|
||||
- 设置 API_KEY
|
||||
- 设置 API_BASE_URL
|
||||
|
||||
3. **测试功能**
|
||||
- 启动 Studio
|
||||
- 测试 Pipeline 提交
|
||||
- 测试状态查询
|
||||
|
||||
4. **发布**
|
||||
- Build Tauri app
|
||||
- 发布新版本
|
||||
|
||||
---
|
||||
|
||||
## 💡 提示
|
||||
|
||||
### 性能优化
|
||||
|
||||
- **轮询间隔**: 建议 2 秒,避免过于频繁
|
||||
- **超时时间**: 5 分钟,根据实际任务调整
|
||||
- **并发限制**: 避免同时提交太多命令
|
||||
|
||||
### 用户体验
|
||||
|
||||
- 显示预估时间
|
||||
- 提供取消功能(未来功能)
|
||||
- 显示错误详情
|
||||
- 支持重试
|
||||
|
||||
---
|
||||
|
||||
**文档完成!可按此指南更新 Momentry Studio。**
|
||||
@@ -0,0 +1,220 @@
|
||||
# Studio Pipeline 集成 - 文件清单
|
||||
|
||||
**日期**: 2026-10-11
|
||||
**完整指南**: `docs_v1.0/GUIDES/Studio_Pipeline_Queue_Integration.md`
|
||||
|
||||
---
|
||||
|
||||
## 📂 需要创建的文件
|
||||
|
||||
### 1. 核心服务文件
|
||||
|
||||
#### `momentry_studio/src/services/pipelineService.ts` ⭐ **必需**
|
||||
- API 调用封装
|
||||
- 类型定义
|
||||
- 错误处理
|
||||
|
||||
**主要方法**:
|
||||
- `vectorizeChunks(fileUuid)` - 向量化
|
||||
- `executeRule1(fileUuid)` - Rule1 执行
|
||||
- `submitFullPipeline(fileUuid)` - 完整 pipeline
|
||||
- `getCommandStatus(commandId)` - 状态查询
|
||||
- `pollCommandStatus(commandId, onProgress)` - 轮询状态
|
||||
|
||||
---
|
||||
|
||||
#### `momentry_studio/src/composables/usePipeline.ts` ⭐ **必需**
|
||||
- Vue 3 Composable
|
||||
- 响应式状态管理
|
||||
- 进度监控
|
||||
|
||||
**提供的功能**:
|
||||
- 提交命令
|
||||
- 监控进度
|
||||
- 错误处理
|
||||
- 状态显示
|
||||
|
||||
---
|
||||
|
||||
### 2. Vue 组件
|
||||
|
||||
#### `momentry_studio/src/components/PipelineProgress.vue` ⭐ **推荐**
|
||||
- 进度显示组件
|
||||
- 状态指示器
|
||||
- 错误展示
|
||||
|
||||
**特性**:
|
||||
- 实时状态更新
|
||||
- 美观的进度条
|
||||
- 错误信息展示
|
||||
|
||||
---
|
||||
|
||||
### 3. 配置文件
|
||||
|
||||
#### `momentry_studio/src/config/api.ts`
|
||||
- API 基础配置
|
||||
- 超时设置
|
||||
- 轮询参数
|
||||
|
||||
#### `momentry_studio/.env`
|
||||
- API Key
|
||||
- API Base URL
|
||||
|
||||
---
|
||||
|
||||
### 4. 状态管理(可选)
|
||||
|
||||
#### `momentry_studio/src/stores/pipeline.ts` (可选)
|
||||
- Pinia Store
|
||||
- 全局状态管理
|
||||
- 多组件共享
|
||||
|
||||
---
|
||||
|
||||
## 📝 需要修改的文件
|
||||
|
||||
### 高优先级
|
||||
|
||||
1. **`src/views/FilesView.vue`**
|
||||
- 集成 PipelineProgress 组件
|
||||
- 添加"开始处理"按钮
|
||||
- 显示处理状态
|
||||
|
||||
2. **`src/views/VideoDetailView.vue`**
|
||||
- 添加 Pipeline 控制面板
|
||||
- 显示命令状态
|
||||
- 进度监控
|
||||
|
||||
3. **`src/components/VideoList.vue`**
|
||||
- 批量处理功能
|
||||
- 状态指示器
|
||||
|
||||
---
|
||||
|
||||
## 🔧 快速开始
|
||||
|
||||
### 第一步:创建核心文件
|
||||
|
||||
```bash
|
||||
# 1. 创建 service
|
||||
touch momentry_studio/src/services/pipelineService.ts
|
||||
|
||||
# 2. 创建 composable
|
||||
touch momentry_studio/src/composables/usePipeline.ts
|
||||
|
||||
# 3. 创建组件
|
||||
touch momentry_studio/src/components/PipelineProgress.vue
|
||||
```
|
||||
|
||||
### 第二步:复制代码
|
||||
|
||||
从完整指南中复制代码到对应文件:
|
||||
- `pipelineService.ts` → 完整代码见指南
|
||||
- `usePipeline.ts` → 完整代码见指南
|
||||
- `PipelineProgress.vue` → 完整代码见指南
|
||||
|
||||
### 第三步:更新配置
|
||||
|
||||
```bash
|
||||
# 创建 .env
|
||||
echo "VITE_API_KEY=YOUR_API_KEY" > momentry_studio/.env
|
||||
echo "VITE_API_BASE_URL=http://localhost:3002/api/v1" >> momentry_studio/.env
|
||||
```
|
||||
|
||||
### 第四步:集成到视图
|
||||
|
||||
修改 `FilesView.vue`,添加:
|
||||
```vue
|
||||
<template>
|
||||
<PipelineProgress :file-uuid="selectedFile.uuid" />
|
||||
</template>
|
||||
|
||||
<script setup>
|
||||
import PipelineProgress from '@/components/PipelineProgress.vue'
|
||||
</script>
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 📋 完整文件树
|
||||
|
||||
```
|
||||
momentry_studio/
|
||||
├── src/
|
||||
│ ├── services/
|
||||
│ │ └── pipelineService.ts ⭐ 新建
|
||||
│ ├── composables/
|
||||
│ │ └── usePipeline.ts ⭐ 新建
|
||||
│ ├── components/
|
||||
│ │ └── PipelineProgress.vue ⭐ 新建
|
||||
│ ├── stores/
|
||||
│ │ └── pipeline.ts (可选)
|
||||
│ ├── config/
|
||||
│ │ └── api.ts 修改
|
||||
│ ├── views/
|
||||
│ │ ├── FilesView.vue 修改
|
||||
│ │ └── VideoDetailView.vue 修改
|
||||
│ └── middleware/
|
||||
│ └── apiError.ts (可选)
|
||||
└── .env 新建
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## ✅ 验证清单
|
||||
|
||||
### 必需文件
|
||||
- [ ] `pipelineService.ts` 已创建
|
||||
- [ ] `usePipeline.ts` 已创建
|
||||
- [ ] `PipelineProgress.vue` 已创建
|
||||
- [ ] `.env` 已配置
|
||||
|
||||
### 修改文件
|
||||
- [ ] `FilesView.vue` 已集成
|
||||
- [ ] `api.ts` 已更新
|
||||
|
||||
### 测试验证
|
||||
- [ ] 提交 Pipeline 命令成功
|
||||
- [ ] 状态查询正常工作
|
||||
- [ ] 进度显示正确
|
||||
- [ ] 错误处理正常
|
||||
|
||||
---
|
||||
|
||||
## 🎯 优先级建议
|
||||
|
||||
### Phase 1: 核心功能(1-2 小时)
|
||||
1. 创建 `pipelineService.ts`
|
||||
2. 创建 `usePipeline.ts`
|
||||
3. 测试 API 调用
|
||||
|
||||
### Phase 2: UI 集成(1-2 小时)
|
||||
1. 创建 `PipelineProgress.vue`
|
||||
2. 修改 `FilesView.vue`
|
||||
3. 测试 UI 交互
|
||||
|
||||
### Phase 3: 完善(可选)
|
||||
1. 添加 Pinia Store
|
||||
2. 错误处理优化
|
||||
3. 性能优化
|
||||
|
||||
---
|
||||
|
||||
## 📞 需要帮助?
|
||||
|
||||
查看完整集成指南:
|
||||
```
|
||||
/Users/accusys/momentry_core/docs_v1.0/GUIDES/Studio_Pipeline_Queue_Integration.md
|
||||
```
|
||||
|
||||
包含:
|
||||
- ✅ 完整代码示例
|
||||
- ✅ API 使用说明
|
||||
- ✅ 最佳实践
|
||||
- ✅ 错误处理
|
||||
- ✅ 性能优化建议
|
||||
|
||||
---
|
||||
|
||||
**准备好开始了吗?从创建 `pipelineService.ts` 开始!**
|
||||
@@ -0,0 +1,294 @@
|
||||
# Studio 使用 Agent Search 快速开始
|
||||
|
||||
**版本**: 1.0
|
||||
**日期**: 2026-10-11
|
||||
|
||||
---
|
||||
|
||||
## 🚀 5 分钟快速开始
|
||||
|
||||
### 步骤 1: 复制组件文件
|
||||
|
||||
```bash
|
||||
# 创建目录
|
||||
mkdir -p src/components/AgentSearch
|
||||
mkdir -p src/services
|
||||
mkdir -p src/types
|
||||
|
||||
# 复制文件(从文档中复制代码)
|
||||
# 1. AgentSearchBar.tsx
|
||||
# 2. AgentSearchResults.tsx
|
||||
# 3. MoreButton.tsx
|
||||
# 4. AgentSearch.tsx
|
||||
# 5. agent-search.ts
|
||||
# 6. agent-search.ts (types)
|
||||
# 7. AgentSearch.css
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 步骤 2: 安装依赖
|
||||
|
||||
```bash
|
||||
npm install axios lucide-react
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 步骤 3: 配置环境变量
|
||||
|
||||
创建 `.env.local`:
|
||||
|
||||
```env
|
||||
REACT_APP_API_URL=http://localhost:3002
|
||||
REACT_APP_API_KEY=muser_demo_key_32chars_abcdef1234567890
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 步骤 4: 添加路由
|
||||
|
||||
在 `App.tsx` 中:
|
||||
|
||||
```typescript
|
||||
import { AgentSearch } from './components/AgentSearch/AgentSearch';
|
||||
|
||||
// 在路由中添加
|
||||
<Route path="/search" element={<AgentSearch />} />
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 步骤 5: 测试
|
||||
|
||||
```bash
|
||||
# 启动开发服务器
|
||||
npm run dev
|
||||
|
||||
# 访问
|
||||
http://localhost:5173/search
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 📝 最小实现(5 个文件)
|
||||
|
||||
### 1. 类型定义 (types/agent-search.ts)
|
||||
|
||||
```typescript
|
||||
export interface SearchResult {
|
||||
id: string;
|
||||
score: number;
|
||||
file_uuid: string;
|
||||
thumbnail?: string;
|
||||
snippet?: string;
|
||||
}
|
||||
|
||||
export interface AgentSearchResponse {
|
||||
results: SearchResult[];
|
||||
total_count: number;
|
||||
has_more: boolean;
|
||||
session_id: string;
|
||||
summary: string;
|
||||
next_offset?: number;
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 2. API 服务 (services/agent-search.ts)
|
||||
|
||||
```typescript
|
||||
import axios from 'axios';
|
||||
|
||||
const api = axios.create({
|
||||
baseURL: process.env.REACT_APP_API_URL || 'http://localhost:3002',
|
||||
headers: {
|
||||
'Authorization': `Bearer ${process.env.REACT_APP_API_KEY}`
|
||||
}
|
||||
});
|
||||
|
||||
export const agentSearchService = {
|
||||
search: async (query: string) => {
|
||||
const { data } = await api.post('/api/v1/agent/search', {
|
||||
query,
|
||||
limit: 5
|
||||
});
|
||||
return data;
|
||||
},
|
||||
|
||||
more: async (sessionId: string, offset: number) => {
|
||||
const { data } = await api.post('/api/v1/agent/search/more', {
|
||||
session_id: sessionId,
|
||||
current_offset: offset,
|
||||
limit: 10
|
||||
});
|
||||
return data;
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 3. 搜索组件 (components/AgentSearch.tsx)
|
||||
|
||||
```typescript
|
||||
import React, { useState } from 'react';
|
||||
import { agentSearchService } from '../../services/agent-search';
|
||||
import './AgentSearch.css';
|
||||
|
||||
export const AgentSearch = () => {
|
||||
const [query, setQuery] = useState('');
|
||||
const [results, setResults] = useState([]);
|
||||
const [hasMore, setHasMore] = useState(false);
|
||||
const [sessionId, setSessionId] = useState('');
|
||||
const [nextOffset, setNextOffset] = useState(0);
|
||||
|
||||
const handleSearch = async () => {
|
||||
const data = await agentSearchService.search(query);
|
||||
setResults(data.results);
|
||||
setHasMore(data.has_more);
|
||||
setSessionId(data.session_id);
|
||||
setNextOffset(data.next_offset || 0);
|
||||
};
|
||||
|
||||
const handleMore = async () => {
|
||||
const data = await agentSearchService.more(sessionId, nextOffset);
|
||||
setResults([...results, ...data.results]);
|
||||
setHasMore(data.has_more);
|
||||
setNextOffset(data.next_offset || 0);
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="agent-search">
|
||||
<input
|
||||
value={query}
|
||||
onChange={(e) => setQuery(e.target.value)}
|
||||
placeholder="Search videos..."
|
||||
/>
|
||||
<button onClick={handleSearch}>Search</button>
|
||||
|
||||
<div className="results">
|
||||
{results.map((r, i) => (
|
||||
<div key={i} className="result">
|
||||
<img src={r.thumbnail} />
|
||||
<p>{r.snippet}</p>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
|
||||
{hasMore && (
|
||||
<button onClick={handleMore}>
|
||||
More Results
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 4. 样式文件 (components/AgentSearch.css)
|
||||
|
||||
```css
|
||||
.agent-search {
|
||||
max-width: 800px;
|
||||
margin: 20px auto;
|
||||
}
|
||||
|
||||
.agent-search input {
|
||||
width: 100%;
|
||||
padding: 12px;
|
||||
font-size: 16px;
|
||||
margin-bottom: 16px;
|
||||
}
|
||||
|
||||
.agent-search button {
|
||||
padding: 8px 16px;
|
||||
background: #4a90e2;
|
||||
color: white;
|
||||
border: none;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.results {
|
||||
margin-top: 20px;
|
||||
}
|
||||
|
||||
.result {
|
||||
border: 1px solid #ddd;
|
||||
padding: 12px;
|
||||
margin-bottom: 12px;
|
||||
}
|
||||
|
||||
.result img {
|
||||
width: 100%;
|
||||
max-width: 200px;
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 5. 使用页面 (pages/SearchPage.tsx)
|
||||
|
||||
```typescript
|
||||
import { AgentSearch } from '../components/AgentSearch';
|
||||
|
||||
export const SearchPage = () => {
|
||||
return (
|
||||
<div>
|
||||
<h1>Video Search</h1>
|
||||
<AgentSearch />
|
||||
</div>
|
||||
);
|
||||
};
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## ⚠️ 注意事项
|
||||
|
||||
### 后端 API 未实现时
|
||||
|
||||
如果 `/api/v1/agent/search` 未实现,可以使用现有的 Smart Search API:
|
||||
|
||||
```typescript
|
||||
// 临时方案
|
||||
export const agentSearchService = {
|
||||
search: async (query: string) => {
|
||||
const { data } = await api.post('/api/v1/search/smart', {
|
||||
query,
|
||||
limit: 5
|
||||
});
|
||||
|
||||
// 转换格式
|
||||
return {
|
||||
results: data.results,
|
||||
total_count: data.results.length,
|
||||
has_more: false,
|
||||
session_id: 'temp',
|
||||
summary: `Found ${data.results.length} results`
|
||||
};
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 🎯 下一步
|
||||
|
||||
1. **实现后端 API**:
|
||||
- `/api/v1/agent/search`
|
||||
- `/api/v1/agent/search/more`
|
||||
|
||||
2. **实现 Visual Search Worker**
|
||||
|
||||
3. **完善 UI**:
|
||||
- 加载动画
|
||||
- 错误处理
|
||||
- 空结果提示
|
||||
|
||||
---
|
||||
|
||||
**快速开始完成!复制代码即可使用。**
|
||||
Reference in New Issue
Block a user