File size: 3,279 Bytes
e4e2691
 
 
 
 
 
 
 
34d9cf1
 
e4e2691
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ec47327
 
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
from flask import Flask, render_template, request, Response, stream_with_context
import json
import time
from workflow_orchestrator import WorkflowOrchestrator

app = Flask(__name__)

# ==================== CONFIGURATION ====================
import os
COHERE_API_KEY = os.getenv("COHERE_API_KEY")

# ==================== FLASK ROUTES ====================

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/stream_workflow')
def stream_workflow():
    task = request.args.get('task', 'Search about KSA Vision 2030')
    
    def generate():
        orchestrator = WorkflowOrchestrator(COHERE_API_KEY)
        
        def on_event(event_type, payload):
            data = {
                'type': event_type,
            }
            
            if isinstance(payload, dict):
                data.update(payload)
            
        # 1. Start
        yield f"data: {json.dumps({'type': 'status', 'node': 'start', 'msg': 'Connecting to Neural Network...'})}\n\n"
        
        yield f"data: {json.dumps({'type': 'activate', 'node': 'planner', 'msg': 'Planner: Analyzing complexity...'})}\n\n"
        plan = orchestrator.agents["planner"].execute(task, {})
        steps = plan['steps']
        yield f"data: {json.dumps({'type': 'log', 'msg': f'Strategized {len(steps)} execution steps based on real analysis.', 'role': 'planner'})}\n\n"

        accumulated_data = ""

        # 2. Execution Loop
        for i, step in enumerate(steps):
            yield f"data: {json.dumps({'type': 'activate', 'node': 'executor', 'msg': f'Researching: {step}'})}\n\n"
            
            exec_res = orchestrator.agents["executor"].execute(step, {})
            accumulated_data += f"\nSection {i+1}: {step}\n{exec_res['output']}\n"
            
            yield f"data: {json.dumps({'type': 'activate', 'node': 'validator', 'msg': 'Verifying sources...'})}\n\n"
            val_res = orchestrator.agents["validator"].execute(exec_res, {})
            
            yield f"data: {json.dumps({'type': 'activate', 'node': 'decision', 'msg': 'Quality Gate'})}\n\n"
            
            if val_res['is_valid']:
                citations_count = len(exec_res.get('citations', []))
                yield f"data: {json.dumps({'type': 'log', 'msg': f'✅ Validated with {citations_count} citations.', 'role': 'success'})}\n\n"
            else:
                yield f"data: {json.dumps({'type': 'log', 'msg': f'⚠️ Low confidence data.', 'role': 'warning'})}\n\n"
            
            time.sleep(0.5)

        # 3. Final Report
        yield f"data: {json.dumps({'type': 'activate', 'node': 'end', 'msg': 'Generating Final Report...'})}\n\n"
        
        final_prompt = f"""
        You are an AI analyst. The user asked: "{task}".
        Based on the following research data, write a comprehensive executive summary in Markdown:
        
        {accumulated_data}
        """
        final_report = orchestrator.co.chat(message=final_prompt, model="command-a-03-2025", temperature=0.3).text
        
        yield f"data: {json.dumps({'type': 'finish', 'report': final_report})}\n\n"

    return Response(stream_with_context(generate()), mimetype='text/event-stream')

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=7860)