|
| 1 | +/** |
| 2 | + * Analysis Results Tools |
| 3 | + * Инструменты для получения результатов AI анализа видео |
| 4 | + */ |
| 5 | + |
| 6 | +import { analysisStorageService } from "@/domains/ai-services/services/analysis-storage-service" |
| 7 | + |
| 8 | +import { BaseAITool } from "../../../base" |
| 9 | +import type { AIToolExecutionOptions, AIToolMetadata, AIToolResult, IAITool } from "../../../types" |
| 10 | + |
| 11 | +// ============================================================================ |
| 12 | +// Types |
| 13 | +// ============================================================================ |
| 14 | + |
| 15 | +interface GetAnalysisResultsInput { |
| 16 | + /** Путь к видео файлу или "all" для всех */ |
| 17 | + videoPath?: string |
| 18 | + /** Тип анализа: comprehensive, montage, unified или all */ |
| 19 | + analysisType?: "comprehensive" | "montage" | "unified" | "all" |
| 20 | + /** Включить метаданные */ |
| 21 | + includeMetadata?: boolean |
| 22 | +} |
| 23 | + |
| 24 | +interface AnalysisResultsOutput { |
| 25 | + success: boolean |
| 26 | + videoPath?: string |
| 27 | + analysisType: string |
| 28 | + results: { |
| 29 | + comprehensive?: any |
| 30 | + montage?: any |
| 31 | + unified?: any |
| 32 | + } |
| 33 | + metadata?: any |
| 34 | + analyzedVideos?: string[] |
| 35 | + stats?: { |
| 36 | + comprehensiveCount: number |
| 37 | + montageCount: number |
| 38 | + unifiedCount: number |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +// ============================================================================ |
| 43 | +// GET ANALYSIS RESULTS TOOL |
| 44 | +// ============================================================================ |
| 45 | + |
| 46 | +export class GetAnalysisResultsTool extends BaseAITool implements IAITool { |
| 47 | + metadata: AIToolMetadata = { |
| 48 | + name: "get-analysis-results", |
| 49 | + displayName: "Получить результаты анализа", |
| 50 | + description: |
| 51 | + "Получает результаты AI Director анализа для видео. Можно запросить данные для конкретного видео или список всех проанализированных видео.", |
| 52 | + domain: "analysis", |
| 53 | + category: "content-intelligence", |
| 54 | + tags: ["analysis", "ai-director", "video", "results", "data"], |
| 55 | + version: "1.0.0", |
| 56 | + author: "Timeline Studio", |
| 57 | + dependencies: [], |
| 58 | + inputSchema: { |
| 59 | + type: "object", |
| 60 | + properties: { |
| 61 | + videoPath: { |
| 62 | + type: "string", |
| 63 | + description: |
| 64 | + 'Путь к видео файлу. Оставьте пустым или "all" для получения списка всех проанализированных видео', |
| 65 | + }, |
| 66 | + analysisType: { |
| 67 | + type: "string", |
| 68 | + enum: ["comprehensive", "montage", "unified", "all"], |
| 69 | + description: "Тип анализа для получения. По умолчанию: all", |
| 70 | + }, |
| 71 | + includeMetadata: { |
| 72 | + type: "boolean", |
| 73 | + description: "Включить метаданные анализа (даты, длительность и т.д.)", |
| 74 | + }, |
| 75 | + }, |
| 76 | + required: [], |
| 77 | + }, |
| 78 | + outputSchema: { |
| 79 | + type: "object", |
| 80 | + properties: { |
| 81 | + success: { type: "boolean" }, |
| 82 | + videoPath: { type: "string" }, |
| 83 | + analysisType: { type: "string" }, |
| 84 | + results: { type: "object" }, |
| 85 | + metadata: { type: "object" }, |
| 86 | + analyzedVideos: { type: "array", items: { type: "string" } }, |
| 87 | + stats: { type: "object" }, |
| 88 | + }, |
| 89 | + }, |
| 90 | + examples: [ |
| 91 | + { |
| 92 | + description: "Получить все результаты анализа для видео", |
| 93 | + input: { videoPath: "/path/to/video.mp4", analysisType: "all" }, |
| 94 | + expectedOutput: { success: true, results: {} }, |
| 95 | + }, |
| 96 | + { |
| 97 | + description: "Получить список всех проанализированных видео", |
| 98 | + input: { videoPath: "all" }, |
| 99 | + expectedOutput: { success: true, analyzedVideos: [] }, |
| 100 | + }, |
| 101 | + ], |
| 102 | + } |
| 103 | + |
| 104 | + async execute( |
| 105 | + input: GetAnalysisResultsInput, |
| 106 | + options?: AIToolExecutionOptions, |
| 107 | + ): Promise<AIToolResult<AnalysisResultsOutput>> { |
| 108 | + return this.executeWithErrorHandling( |
| 109 | + async (_context) => { |
| 110 | + const { videoPath, analysisType = "all", includeMetadata = true } = input |
| 111 | + |
| 112 | + // Если запрос на все видео или не указан путь |
| 113 | + if (!videoPath || videoPath === "all") { |
| 114 | + const analyzedVideos = await analysisStorageService.getAnalyzedVideos() |
| 115 | + const stats = await analysisStorageService.getStorageStats() |
| 116 | + |
| 117 | + return { |
| 118 | + success: true, |
| 119 | + analysisType: "list", |
| 120 | + results: {}, |
| 121 | + analyzedVideos, |
| 122 | + stats, |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + // Получаем результаты для конкретного видео |
| 127 | + const results: AnalysisResultsOutput["results"] = {} |
| 128 | + let metadata: any |
| 129 | + |
| 130 | + if (analysisType === "comprehensive" || analysisType === "all") { |
| 131 | + const comprehensive = await analysisStorageService.loadComprehensiveAnalysis(videoPath) |
| 132 | + if (comprehensive.success && comprehensive.data) { |
| 133 | + results.comprehensive = comprehensive.data |
| 134 | + |
| 135 | + // Загружаем метаданные |
| 136 | + if (includeMetadata && comprehensive.data.analysis_id) { |
| 137 | + metadata = await analysisStorageService.loadAnalysisMetadata(comprehensive.data.analysis_id) |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + if (analysisType === "montage" || analysisType === "all") { |
| 143 | + const montage = await analysisStorageService.loadMontageAnalysis(videoPath) |
| 144 | + if (montage.success && montage.data) { |
| 145 | + results.montage = montage.data |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + if (analysisType === "unified" || analysisType === "all") { |
| 150 | + const unified = await analysisStorageService.loadUnifiedAnalysis(videoPath) |
| 151 | + if (unified.success && unified.data) { |
| 152 | + results.unified = unified.data |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + const hasResults = Object.keys(results).length > 0 |
| 157 | + |
| 158 | + return { |
| 159 | + success: hasResults, |
| 160 | + videoPath, |
| 161 | + analysisType, |
| 162 | + results, |
| 163 | + metadata: includeMetadata ? metadata : undefined, |
| 164 | + } |
| 165 | + }, |
| 166 | + input, |
| 167 | + options, |
| 168 | + ) |
| 169 | + } |
| 170 | + |
| 171 | + validate(input: any): boolean { |
| 172 | + // Валидация не строгая - все поля опциональны |
| 173 | + return typeof input === "object" && input !== null |
| 174 | + } |
| 175 | + |
| 176 | + getSchema(): { input: any; output: any } { |
| 177 | + return { |
| 178 | + input: this.metadata.inputSchema, |
| 179 | + output: this.metadata.outputSchema, |
| 180 | + } |
| 181 | + } |
| 182 | +} |
| 183 | + |
| 184 | +// ============================================================================ |
| 185 | +// LIST MEDIA FILES TOOL |
| 186 | +// ============================================================================ |
| 187 | + |
| 188 | +export class ListProjectMediaTool extends BaseAITool implements IAITool { |
| 189 | + metadata: AIToolMetadata = { |
| 190 | + name: "list-project-media", |
| 191 | + displayName: "Список медиа в проекте", |
| 192 | + description: "Получает список всех медиа файлов, добавленных в проект, с их основной информацией", |
| 193 | + domain: "analysis", |
| 194 | + category: "content-intelligence", |
| 195 | + tags: ["media", "project", "files", "list"], |
| 196 | + version: "1.0.0", |
| 197 | + author: "Timeline Studio", |
| 198 | + dependencies: [], |
| 199 | + inputSchema: { |
| 200 | + type: "object", |
| 201 | + properties: { |
| 202 | + includeAnalysisStatus: { |
| 203 | + type: "boolean", |
| 204 | + description: "Включить статус анализа для каждого файла", |
| 205 | + }, |
| 206 | + }, |
| 207 | + required: [], |
| 208 | + }, |
| 209 | + outputSchema: { |
| 210 | + type: "object", |
| 211 | + properties: { |
| 212 | + success: { type: "boolean" }, |
| 213 | + files: { |
| 214 | + type: "array", |
| 215 | + items: { |
| 216 | + type: "object", |
| 217 | + properties: { |
| 218 | + path: { type: "string" }, |
| 219 | + name: { type: "string" }, |
| 220 | + hasAnalysis: { type: "boolean" }, |
| 221 | + }, |
| 222 | + }, |
| 223 | + }, |
| 224 | + totalCount: { type: "number" }, |
| 225 | + }, |
| 226 | + }, |
| 227 | + examples: [ |
| 228 | + { |
| 229 | + description: "Получить список всех медиа файлов", |
| 230 | + input: { includeAnalysisStatus: true }, |
| 231 | + expectedOutput: { success: true, files: [], totalCount: 0 }, |
| 232 | + }, |
| 233 | + ], |
| 234 | + } |
| 235 | + |
| 236 | + async execute( |
| 237 | + input: { includeAnalysisStatus?: boolean }, |
| 238 | + options?: AIToolExecutionOptions, |
| 239 | + ): Promise<AIToolResult<any>> { |
| 240 | + return this.executeWithErrorHandling( |
| 241 | + async (_context) => { |
| 242 | + // Получаем список проанализированных видео из storage |
| 243 | + const analyzedVideos = await analysisStorageService.getAnalyzedVideos() |
| 244 | + |
| 245 | + const files = analyzedVideos.map((path) => ({ |
| 246 | + path, |
| 247 | + name: path.split("/").pop() || path, |
| 248 | + hasAnalysis: true, |
| 249 | + })) |
| 250 | + |
| 251 | + return { |
| 252 | + success: true, |
| 253 | + files, |
| 254 | + totalCount: files.length, |
| 255 | + } |
| 256 | + }, |
| 257 | + input, |
| 258 | + options, |
| 259 | + ) |
| 260 | + } |
| 261 | + |
| 262 | + validate(input: any): boolean { |
| 263 | + return typeof input === "object" && input !== null |
| 264 | + } |
| 265 | + |
| 266 | + getSchema(): { input: any; output: any } { |
| 267 | + return { |
| 268 | + input: this.metadata.inputSchema, |
| 269 | + output: this.metadata.outputSchema, |
| 270 | + } |
| 271 | + } |
| 272 | +} |
| 273 | + |
| 274 | +// ============================================================================ |
| 275 | +// Exports |
| 276 | +// ============================================================================ |
| 277 | + |
| 278 | +export const analysisResultsTools = [new GetAnalysisResultsTool(), new ListProjectMediaTool()] |
| 279 | + |
| 280 | +export const ANALYSIS_RESULTS_TOOLS_COUNT = analysisResultsTools.length |
0 commit comments