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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
"""FinAgent Gradio frontend β€” application entry point.

This module constructs the Gradio Blocks application used as the FinAgent
Hugging Face Space. The outer shell (title, theme, custom CSS) is defined
in :func:`create_app`; UI widgets, session state, and event wiring are
added by later tasks (6.2, 6.3, 6.4).

The vLLM inference endpoint URL is read from the ``VLLM_ENDPOINT_URL``
environment variable at module import time so it can be validated early
by the Analyze event handler.
"""

import os
import time
from datetime import datetime

import gradio as gr

from rendering import (
    build_css,
    render_activity_entry,
    render_activity_feed,
    render_error_card,
    render_signal_card,
    render_summary,
)
from validation import validate_portfolio_value, validate_tickers

# Inference endpoint URL for the vLLM server powering the agent pipeline.
# Read at import time; the event handler is responsible for surfacing a
# user-facing error when this value is missing.
VLLM_ENDPOINT_URL = os.environ.get("VLLM_ENDPOINT_URL")

# Hard cap on total pipeline execution time per Requirement 4.4. The
# pipeline loop checks the elapsed wall-clock time before dispatching
# each ticker and yields a timeout warning once this budget is exceeded.
TIMEOUT_SECONDS = 180


def _format_progress_message(
    ticker: str,
    i: int,
    total: int,
    elapsed_seconds: int,
) -> str:
    """Format the in-flight progress message shown above the activity feed.

    Pulled out of :func:`run_analysis` as a pure, side-effect-free helper
    so the progress string's contract (Requirements 4.2, 4.3) can be
    exercised directly by property tests without constructing a full
    Gradio event loop.

    Args:
        ticker: The ticker symbol currently being analyzed.
        i: 1-indexed position of ``ticker`` in the watchlist.
        total: Total number of tickers in the watchlist (``i <= total``).
        elapsed_seconds: Whole seconds elapsed since the analysis began;
            rendered verbatim in the ``(elapsed: Ns)`` suffix.

    Returns:
        A Markdown-formatted string of the form::

            **Analyzing ticker {i} of {total}** β€” {ticker} (elapsed: {N}s)

        which contains the ticker, ``i``, and ``total`` as substrings
        (Property 6 in the design document).
    """
    return (
        f"**Analyzing ticker {i} of {total}** β€” {ticker} "
        f"(elapsed: {elapsed_seconds}s)"
    )


def run_analysis(
    ticker_input,
    risk_tolerance,
    portfolio_value,
    trading_style,
    activity_log,
    signals_state,
):
    """Analyze button event handler β€” streams UI updates as a generator.

    This is the task 7.1 slice of the full handler: it covers environment
    and input validation plus initial state setup. The pipeline execution
    loop (task 7.2), error handling / completion yields (task 7.3), and
    the ``_render_signals_dashboard`` helper (task 7.4) are added by
    subsequent tasks.

    Each ``yield`` produces the tuple wired to ``analyze_btn.click``'s
    outputs in :func:`create_app`, in this order::

        (analyze_btn, error_display, progress_text,
         activity_feed, signals_dashboard,
         activity_log, signals_state)

    Yields:
        Tuples of :class:`gradio.update` calls and session-state values
        that incrementally update the UI during analysis.
    """
    # --- Environment validation ---
    # The vLLM endpoint URL is required to run the pipeline. Surface a
    # clear configuration error and re-enable the button so the user can
    # retry once the variable is set.
    if not VLLM_ENDPOINT_URL:
        yield (
            gr.update(interactive=True),
            gr.update(
                value=(
                    "❌ Configuration error: `VLLM_ENDPOINT_URL` "
                    "environment variable is not set."
                ),
                visible=True,
            ),
            gr.update(visible=False),
            gr.update(),
            gr.update(),
            activity_log,
            signals_state,
        )
        return

    # --- Ticker validation ---
    validation = validate_tickers(ticker_input)
    if not validation.valid:
        yield (
            gr.update(interactive=True),
            gr.update(value=f"❌ {validation.error_message}", visible=True),
            gr.update(visible=False),
            gr.update(),
            gr.update(),
            activity_log,
            signals_state,
        )
        return

    # --- Portfolio value validation ---
    portfolio_error = validate_portfolio_value(portfolio_value)
    if portfolio_error:
        yield (
            gr.update(interactive=True),
            gr.update(value=f"❌ {portfolio_error}", visible=True),
            gr.update(visible=False),
            gr.update(),
            gr.update(),
            activity_log,
            signals_state,
        )
        return

    # --- Initialize per-run state ---
    # Reset activity log and signals for a fresh run; capture the start
    # timestamp so the pipeline loop (task 7.2) can enforce the timeout
    # and report elapsed time in progress updates.
    activity_log = []
    signals_state = []
    analysis_start = time.time()
    tickers = validation.tickers
    total = len(tickers)

    # Seed the activity feed with a start entry so the user sees
    # immediate feedback the moment the button is clicked.
    start_entry = render_activity_entry(
        datetime.now(),
        "System",
        f"Analysis started for {total} ticker(s)",
        False,
    )
    activity_log.append(start_entry)

    # Initial yield: disable button to prevent re-submission, hide any
    # stale error banner, show the progress indicator, seed the activity
    # feed, and clear the signals dashboard from any previous run.
    yield (
        gr.update(interactive=False),
        gr.update(visible=False),
        gr.update(value=f"**Analyzing ticker 1 of {total}**", visible=True),
        render_activity_feed(activity_log),
        "",
        activity_log,
        signals_state,
    )

    # --- Pipeline execution loop (task 7.2) ---
    # The entire pipeline setup + per-ticker loop runs inside a single
    # ``try`` block (task 7.3) so any unexpected exception β€” import
    # failure, runner construction error, or an unhandled crash inside
    # ``_run_single`` β€” is surfaced to the user as a visible error and
    # the Analyze button is re-enabled instead of leaving the UI in a
    # locked, spinning state (Requirements 8.1, 8.3).
    try:
        # Import the orchestration package lazily so ``app.py`` remains
        # importable in environments where ``crew`` isn't installed
        # (e.g., isolated unit-test runs of the validation/rendering
        # modules).
        from crew import (
            LLMConfig,
            OrchestratorConfig,
            TradePreferences,
            WatchlistRunner,
        )
        from crew.callbacks import ActivityEvent, ActivityFeedCallback, EventType

        # Configure the orchestrator to point at the vLLM inference endpoint.
        config = OrchestratorConfig(
            llm=LLMConfig(base_url=VLLM_ENDPOINT_URL),
        )

        preferences = TradePreferences(
            risk_tolerance=risk_tolerance,
            trading_style=trading_style,
            portfolio_value=float(portfolio_value),
        )

        # Buffer events emitted by the runner/crew during ``_run_single``.
        # The callback handler runs synchronously on the same thread, so
        # a plain list plus a closure is sufficient β€” we drain it into
        # the activity feed after each ticker completes.
        pending_events: list[ActivityEvent] = []

        def event_handler(event: ActivityEvent) -> None:
            pending_events.append(event)

        callback = ActivityFeedCallback(handler=event_handler)

        # Build the tools dict that maps each agent's name to its tool
        # function list. Without this, every agent runs without tools
        # and the LLM just hallucinates plausible-looking numbers
        # instead of calling yfinance / ddgs / pandas-ta.
        from tools import (
            search_news,
            get_price_change,
            get_volume,
            get_financials,
            get_earnings,
            get_peers,
            get_price_history,
            calculate_indicators,
            calculate_position_size,
            set_stop_loss,
        )

        crew_tools = {
            "market_scanner": [search_news, get_price_change, get_volume],
            "fundamental_analyst": [get_financials, get_earnings, get_peers],
            "technical_analyst": [get_price_history, calculate_indicators],
            "risk_manager": [calculate_position_size, set_stop_loss],
        }

        runner = WatchlistRunner(
            config=config,
            tools=crew_tools,
            callback=callback,
            preferences=preferences,
        )

        for i, ticker in enumerate(tickers, 1):
            # Enforce the overall pipeline timeout (Requirement 4.4). We
            # check before dispatching each ticker so a slow ticker
            # can't push the total past the budget unnoticed; when
            # exceeded we append a timeout entry, re-enable the Analyze
            # button, and return early.
            elapsed = time.time() - analysis_start
            if elapsed > TIMEOUT_SECONDS:
                timeout_entry = render_activity_entry(
                    datetime.now(),
                    "System",
                    f"⚠️ Analysis timed out after {TIMEOUT_SECONDS}s",
                    False,
                )
                activity_log.append(timeout_entry)
                yield (
                    gr.update(interactive=True),
                    gr.update(
                        value="⚠️ Analysis timed out. Try fewer tickers.",
                        visible=True,
                    ),
                    gr.update(visible=False),
                    render_activity_feed(activity_log),
                    _render_signals_dashboard(signals_state),
                    activity_log,
                    signals_state,
                )
                return

            # Progress message surfaces the current ticker, its position
            # in the batch, and elapsed seconds (Requirements 4.2, 4.3,
            # 3.6). The format is centralized in
            # :func:`_format_progress_message` so the contract can be
            # property-tested directly.
            progress_msg = _format_progress_message(
                ticker=ticker,
                i=i,
                total=total,
                elapsed_seconds=int(elapsed),
            )

            # Run the crew pipeline for this ticker. ``_run_single``
            # handles its own exception isolation and always returns a
            # CrewResult (success or failure), so per-ticker errors
            # won't break the outer loop.
            result = runner._run_single(ticker)

            # Translate buffered callback events into activity feed HTML
            # entries. Task-start events show a spinner to indicate work
            # in progress; all other event types render as static lines.
            for event in pending_events:
                entry = render_activity_entry(
                    event.timestamp,
                    event.agent_name,
                    event.message,
                    is_spinner=(event.event_type == EventType.TASK_START),
                )
                activity_log.append(entry)
            pending_events.clear()

            # Collect the per-ticker outcome. Successful runs contribute
            # a TradingSignal; failures contribute an error dict that
            # ``_render_signals_dashboard`` renders via
            # ``render_error_card`` (Requirement 5.5).
            if result.success and result.signal is not None:
                signals_state.append(result.signal)
            else:
                signals_state.append(
                    {
                        "ticker": ticker,
                        "error": result.error or "Unknown error",
                    }
                )

            # Stream the intermediate state to the UI: keep the button
            # disabled (still running), hide the error banner, refresh
            # the progress line, activity feed, and signals dashboard.
            yield (
                gr.update(interactive=False),
                gr.update(visible=False),
                gr.update(value=progress_msg, visible=True),
                render_activity_feed(activity_log),
                _render_signals_dashboard(signals_state),
                activity_log,
                signals_state,
            )

    except Exception as e:
        # --- Unhandled pipeline error (task 7.3) ---
        # Log the failure to the activity feed so the user can see what
        # went wrong in-context (Requirement 8.2), surface a visible
        # error banner with the exception message, hide the progress
        # indicator, and re-enable the Analyze button so the user can
        # retry (Requirements 8.1, 8.3). Any signals already collected
        # from earlier tickers are preserved in ``signals_state`` and
        # re-rendered in the final yield.
        error_entry = render_activity_entry(
            datetime.now(),
            "System",
            f"❌ Error: {str(e)}",
            False,
        )
        activity_log.append(error_entry)
        yield (
            gr.update(interactive=True),
            gr.update(value=f"❌ An error occurred: {str(e)}", visible=True),
            gr.update(visible=False),
            render_activity_feed(activity_log),
            _render_signals_dashboard(signals_state),
            activity_log,
            signals_state,
        )
        return

    # --- Successful completion (task 7.3) ---
    # The pipeline finished without raising. Append a completion entry
    # reporting total elapsed time (Requirement 8.4), hide the progress
    # indicator, clear any lingering error banner, and re-enable the
    # Analyze button for the next run.
    elapsed_total = time.time() - analysis_start
    complete_entry = render_activity_entry(
        datetime.now(),
        "System",
        f"βœ… Analysis complete ({int(elapsed_total)}s)",
        False,
    )
    activity_log.append(complete_entry)

    yield (
        gr.update(interactive=True),
        gr.update(visible=False),
        gr.update(visible=False),
        render_activity_feed(activity_log),
        _render_signals_dashboard(signals_state),
        activity_log,
        signals_state,
    )


def _render_signals_dashboard(signals: list) -> str:
    """Render the full signals dashboard HTML (summary bar + cards).

    Iterates the collected results and produces a dashboard composed of
    an aggregate :func:`render_summary` bar followed by one HTML card per
    result: a :func:`render_signal_card` for :class:`TradingSignal`-like
    objects and a :func:`render_error_card` for error dicts of the form
    ``{"ticker": ..., "error": ...}``.

    The action label is read defensively via ``getattr(item.action,
    "value", item.action)`` so this helper works both with the orchestrator's
    ``Action`` enum (``Action.BUY.value == "BUY"``) and with plain string
    actions, without importing from the ``crew`` package.

    Args:
        signals: Ordered list of :class:`TradingSignal`-like objects and/or
            error dicts accumulated during an analysis run.

    Returns:
        Concatenated HTML string (summary bar followed by card HTML joined
        by newlines), or an empty string when ``signals`` is empty.
    """
    if not signals:
        return ""

    cards: list[str] = []
    buy_count = 0
    sell_count = 0
    hold_count = 0

    for item in signals:
        # Error dicts are produced when a ticker fails analysis β€” render
        # them as dedicated error cards and skip action counting.
        if isinstance(item, dict) and "error" in item:
            cards.append(render_error_card(item["ticker"], item["error"]))
            continue

        cards.append(render_signal_card(item))

        # Categorize the action for the summary bar. ``item.action`` may
        # be an enum (``.value`` gives the label) or already a string β€”
        # ``getattr`` with a default covers both without importing the
        # orchestrator's ``Action`` enum.
        action_label = str(getattr(item.action, "value", item.action)).upper()
        if action_label == "BUY":
            buy_count += 1
        elif action_label == "SELL":
            sell_count += 1
        elif action_label == "HOLD":
            hold_count += 1

    total = len(signals)
    summary = render_summary(total, buy_count, sell_count, hold_count)

    return summary + "\n".join(cards)


def create_app() -> gr.Blocks:
    """Build and return the Gradio Blocks application.

    Creates the outer shell: page title, dark Base theme with an emerald
    primary hue and slate neutral hue, and the custom dark financial
    terminal CSS from :func:`rendering.build_css`.

    Returns:
        The configured :class:`gradio.Blocks` instance, ready to have UI
        widgets and event handlers attached by subsequent tasks.
    """
    custom_css = build_css()

    with gr.Blocks(
        title="FinAgent - AI Trading Signals",
        theme=gr.themes.Base(
            primary_hue="emerald",
            neutral_hue="slate",
        ),
        css=custom_css,
    ) as app:
        # --- Header ---
        gr.Markdown(
            "# πŸ€– FinAgent\n"
            "### AI-Powered Trading Signal Generator"
        )

        # --- Session State (per-user, isolated by gr.State) ---
        activity_log = gr.State([])      # list[str]: HTML strings for activity feed entries
        signals_state = gr.State([])     # list[TradingSignal | dict]: collected signals / errors
        start_time = gr.State(None)      # Optional[float]: time.time() when analysis started

        # --- Main Layout: Input panel (left) + Activity/Signals (right) ---
        with gr.Row():
            # Left Column: Input Panel
            with gr.Column(scale=1):
                ticker_input = gr.Textbox(
                    label="Watchlist",
                    placeholder="AAPL, NVDA, TSLA, BTC-USD",
                    info="Comma-separated tickers (max 10)",
                )
                risk_tolerance = gr.Dropdown(
                    choices=["Conservative", "Moderate", "Aggressive"],
                    value="Moderate",
                    label="Risk Tolerance",
                )
                portfolio_value = gr.Number(
                    value=10000,
                    minimum=0,
                    label="Portfolio Value ($)",
                )
                trading_style = gr.Dropdown(
                    choices=["Day Trading", "Swing Trading", "Position Trading"],
                    value="Swing Trading",
                    label="Trading Style",
                )
                analyze_btn = gr.Button(
                    "πŸ” Analyze",
                    variant="primary",
                    interactive=True,
                )
                error_display = gr.Markdown(visible=False)

            # Right Column: Activity Feed + Signals Dashboard
            with gr.Column(scale=2):
                progress_text = gr.Markdown(visible=False)
                activity_feed = gr.HTML(
                    label="Agent Activity",
                    value="<div class='activity-feed'></div>",
                )
                signals_dashboard = gr.HTML(
                    label="Trading Signals",
                    value="",
                )

        # --- Event Wiring ---
        # The Analyze button streams results from the ``run_analysis``
        # generator. Inputs carry the user's configuration plus the
        # per-session activity log and collected signals so the handler
        # can extend them incrementally. Outputs cover every widget the
        # handler mutates during the run (button interactivity, error
        # banner, progress text, activity feed, signals dashboard) plus
        # the two session-state values it updates.
        analyze_btn.click(
            fn=run_analysis,
            inputs=[
                ticker_input,
                risk_tolerance,
                portfolio_value,
                trading_style,
                activity_log,
                signals_state,
            ],
            outputs=[
                analyze_btn,
                error_display,
                progress_text,
                activity_feed,
                signals_dashboard,
                activity_log,
                signals_state,
            ],
        )

        # --- Footer ---
        gr.Markdown(
            "⚠️ **Disclaimer:** Trading signals are for informational purposes only "
            "and do not constitute financial advice. Always do your own research."
        )

    # Expose widgets and session state on the returned Blocks so subsequent
    # tasks (6.3 event wiring) can reference them without re-entering the
    # context. These attributes are an internal contract between tasks in
    # this module only.
    app.ticker_input = ticker_input
    app.risk_tolerance = risk_tolerance
    app.portfolio_value = portfolio_value
    app.trading_style = trading_style
    app.analyze_btn = analyze_btn
    app.error_display = error_display
    app.progress_text = progress_text
    app.activity_feed = activity_feed
    app.signals_dashboard = signals_dashboard
    app.activity_log = activity_log
    app.signals_state = signals_state
    app.start_time = start_time

    return app


if __name__ == "__main__":
    # Launch the Gradio app on all interfaces so the Hugging Face Space
    # container can route external traffic to it (per Requirement 7.5).
    create_app().launch(server_name="0.0.0.0")