Spaces:
Configuration error

File size: 6,683 Bytes
023064b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/**
 * Plugin Integration Example — How to use the plugin system with the agentic pipeline
 *
 * This file shows how to integrate plugins without modifying core agentic code.
 * Place this in your custom entry point or import in autopilot.ts
 */

import { createPluginRegistry, setupPluginsForAutopilot, getPluginRegistry, registerAllPlugins } from './index.js';
import { logInfo, logWarn, logError } from '../../shared/logging/runtime-logging.js';
import { PluginPlan, PluginAssets, PluginStylePlan, PluginFilterGraph } from './core/types.js';
import { runAgenticPipeline, renderAgenticSlideshow, PipelineRequest } from '../orchestrate.js';
import { PluginContext } from './core/types.js';

/**
 * Example 1: Using plugins with autopilot (recommended)
 */
export async function runWithPluginsAutopilot(
    topic: string,
    title: string,
    options: {
        configPath?: string;
        pluginConfig?: Record<string, { enabled?: boolean; config?: Record<string, unknown> }>;
    } = {},
): Promise<string> {
    // 1. Setup plugins
    const registry = await setupPluginsForAutopilot(
        `job_${Date.now()}`,
        `./workspace/jobs/job_${Date.now()}`,
        { topic, title, ...options },
    );

    // 2. Apply custom plugin config if provided
    if (options.pluginConfig) {
        for (const [name, cfg] of Object.entries(options.pluginConfig)) {
            registry.setEnabled(name, cfg.enabled ?? true);
            if (cfg.config) registry.setConfig(name, cfg.config);
        }
    }

    // 3. Run pipeline with plugin hooks integrated
    const req: PipelineRequest = {
        topic,
        title,
        backend: 'agent',
        // Plugin configs passed via context
    };

    const res = await runAgenticPipeline(req, (progress) => {
        logInfo(`[${progress.stage}] ${progress.percent}% - ${progress.message}`);
    });

    // 4. Apply plugin render modifications
    const pluginRegistry = getPluginRegistry();
    if (pluginRegistry) {
        // Get the base filtergraph from pipeline
        const filtergraph = res.workspace; // Contains render manifest

        // Run plugin render hooks
        const enhancedFiltergraph = await pluginRegistry.invokeOnRender(filtergraph as any);

        // Render with enhanced filtergraph
        const output = await renderAgenticSlideshow(res, {
            // Pass enhanced filtergraph options
        });

        // Post-render hooks (thumbnails, metadata, transcode)
        await pluginRegistry.invokeOnPostRender(output);

        return output;
    }

    // Fallback: standard render
    return await renderAgenticSlideshow(res);
}

/**
 * Example 2: Manual plugin integration (for custom pipelines)
 */
export async function runWithPluginsManual(topic: string, title: string, customPlugins?: string[]): Promise<string> {
    // Create context
    const jobId = `job_${Date.now()}`;
    const context = new PluginContext({
        jobId,
        workspaceRoot: `./workspace/jobs/${jobId}`,
        config: { topic, title },
    });

    // Create and populate registry
    const registry = await createPluginRegistry(context);
    registerAllPlugins(registry, {
        'genre-style': { enabled: true, config: { genre: 'reels' } },
        'platform-export': { enabled: true, config: { platforms: ['tiktok', 'reels'] } },
    });

    // Enable custom plugins
    for (const name of customPlugins ?? []) {
        registry.setEnabled(name, true);
    }

    // Initialize
    await registry.invokeOnLoad();

    // Run pipeline stages with plugin hooks
    // Stage 1: Plan
    let plan: PluginPlan = {
        topic,
        title,
        orientation: 'portrait',
        aspect: '9:16',
        scenes: [
            {
                sceneNumber: 1,
                voiceoverText: 'Hook',
                searchKeywords: ['hook'],
                visualPreference: 'image',
                durationSec: 3,
                metadata: {},
            },
            {
                sceneNumber: 2,
                voiceoverText: 'Content',
                searchKeywords: ['content'],
                visualPreference: 'image',
                durationSec: 4,
                metadata: {},
            },
        ],
        totalDurationSec: 7,
        metadata: {},
    };

    plan = await registry.invokeOnPlan(plan);

    // Stage 2: Acquire (mock)
    let assets: PluginAssets = { scenes: [], music: [], metadata: {} };
    assets = await registry.invokeOnAcquire(assets);

    // Stage 3: Style
    let style: PluginStylePlan = { preset: 'reels', transitions: [], grades: [], kinetics: [], metadata: {} };
    style = await registry.invokeOnStyle(style);

    // Stage 4: Render
    let filtergraph: PluginFilterGraph = { videoInputs: [], audioInputs: [], filters: [], outputs: [], metadata: {} };
    filtergraph = await registry.invokeOnRender(filtergraph);

    // Render video
    // const output = await renderAgenticSlideshow(plan, filtergraph);

    // Stage 5: Post-render
    // await registry.invokeOnPostRender(output);

    return 'output.mp4';
}

/**
 * Example 3: CLI Integration
 *
 * Usage in bin/agentic-auto.ts or similar:
 *
 * import { createPluginRegistry, registerAllPlugins } from '../src/agentic/plugins/index.js';
 *
 * const registry = await createPluginRegistry(context);
 * registerAllPlugins(registry, customConfigFromCLI);
 *
 * // Then in pipeline stages, call:
 * plan = await registry.invokeOnPlan(plan);
 * style = await registry.invokeOnStyle(style);
 * filtergraph = await registry.invokeOnRender(filtergraph);
 * await registry.invokeOnPostRender(outputPath);
 */

/**
 * Example 4: Creating a Custom Plugin
 *
 * Create file: ./my-plugins/custom-effect.ts
 *
 * import { AgenticPlugin, PluginCategory, Capability } from '../src/agentic/plugins/core/types.js';
 *
 * export const customEffectPlugin: AgenticPlugin = {
 *     metadata: {
 *         name: 'custom-effect',
 *         version: '1.0.0',
 *         description: 'My custom effect',
 *     },
 *     capabilities: [Capability.MOTION_KEYFRAMES],
 *     category: PluginCategory.MOTION,
 *     defaultConfig: { intensity: 1.0 },
 *     hooks: {
 *         onRenderFilter: async (scene, ctx) => {
 *             const cfg = ctx.getConfig('custom-effect');
 *             // Add custom filter
 *             return { ...scene, filterChain: scene.filterChain + ',custom=filter' };
 *         }
 *     }
 * };
 *
 * // Register:
 * import { customEffectPlugin } from './my-plugins/custom-effect.js';
 * registry.register(customEffectPlugin, { intensity: 0.8 }, true);
 */

// Export for use in other entry points
export { createPluginRegistry, registerAllPlugins, getPluginRegistry };