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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
"""
Research Service - A2A Server

Standalone research agent implementing Google A2A protocol.
Fetches data from 6 MCP servers using TRUE MCP protocol (subprocess + JSON-RPC)
and returns aggregated research data.

Deployed on HuggingFace Spaces as a separate A2A agent.
"""

import asyncio
import logging
import os
import uuid
from datetime import datetime
from enum import Enum
from typing import Any, Dict, List, Optional

from dotenv import load_dotenv
load_dotenv()

from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from pydantic import BaseModel

# TRUE MCP protocol client (subprocess + JSON-RPC)
from mcp_client import fetch_all_research_data

logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger("research-service")

# FastAPI app
app = FastAPI(
    title="Research Service",
    description="Financial research service for SWOT analysis - fetches data from 6 MCP servers using TRUE MCP protocol",
    version="1.1.2"
)

# CORS for cross-origin requests from main SWOT app
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Server configuration
A2A_SERVER_URL = os.getenv("A2A_SERVER_URL", "https://vn6295337-researcher-agent.hf.space")


# ============================================================
# A2A PROTOCOL TYPES
# ============================================================

class TaskStatus(str, Enum):
    SUBMITTED = "submitted"
    WORKING = "working"
    COMPLETED = "completed"
    FAILED = "failed"
    CANCELED = "canceled"


class MetricEntry(BaseModel):
    """Granular metric entry for partial results streaming."""
    source: str
    metric: str
    value: Any
    timestamp: str
    # Temporal fields for financial data
    end_date: Optional[str] = None      # "2023-09-30"
    fiscal_year: Optional[int] = None   # 2023
    form: Optional[str] = None          # "10-K" or "10-Q"


class Task(BaseModel):
    id: str
    status: TaskStatus
    message: Optional[Dict[str, Any]] = None
    artifacts: Optional[list] = None
    partial_metrics: Optional[List[MetricEntry]] = None  # For streaming during WORKING status
    error: Optional[str] = None
    created_at: str
    updated_at: str


# In-memory task store
TASK_STORE: Dict[str, Task] = {}


# ============================================================
# AGENT CARD
# ============================================================

AGENT_CARD = {
    "name": "research-service",
    "version": "1.1.2",
    "description": "Financial research service that collects data from 6 MCP servers using TRUE MCP protocol (subprocess + JSON-RPC) for SWOT analysis.",
    "url": A2A_SERVER_URL,
    "capabilities": {
        "streaming": False,
        "pushNotifications": False,
        "stateTransitionHistory": False,
        "partialResults": True  # Supports partial_metrics during WORKING status
    },
    "authentication": {
        "schemes": []
    },
    "defaultInputModes": ["text"],
    "defaultOutputModes": ["data"],
    "skills": [
        {
            "id": "research-company",
            "name": "Company Research",
            "description": "Fetch comprehensive financial data for a company from SEC EDGAR, Yahoo Finance, FRED, Tavily News, and Finnhub Sentiment",
            "inputModes": ["text"],
            "outputModes": ["data"],
            "examples": [
                {"input": "Research Tesla", "output": "Aggregated financial data for TSLA"},
                {"input": "Research AAPL Apple Inc", "output": "Financial data for Apple"}
            ]
        }
    ],
    "dataSources": [
        "SEC EDGAR (financials, filings)",
        "Yahoo Finance (valuation, volatility)",
        "FRED (macro indicators)",
        "Tavily (news search)",
        "Finnhub (sentiment)",
        "Reddit (sentiment)"
    ]
}


@app.get("/.well-known/agent.json")
async def get_agent_card():
    """Return the A2A agent card."""
    return JSONResponse(content=AGENT_CARD)


# ============================================================
# JSON-RPC HANDLERS
# ============================================================

def create_jsonrpc_response(id: Any, result: Any = None, error: Any = None) -> dict:
    """Create a JSON-RPC 2.0 response."""
    response = {"jsonrpc": "2.0", "id": id}
    if error:
        response["error"] = error
    else:
        response["result"] = result
    return response


def parse_research_request(message: dict) -> tuple[Optional[str], Optional[str]]:
    """
    Parse company name and ticker from message.

    Expects message format:
    {
        "parts": [{"type": "text", "text": "Research Tesla"}]
    }
    """
    parts = message.get("parts", [])
    if not parts:
        return None, None

    text = ""
    for part in parts:
        if part.get("type") == "text":
            text = part.get("text", "")
            break

    # Parse "Research <company>" format
    text = text.strip()
    if text.lower().startswith("research "):
        text = text[9:].strip()

    # Check if format is "TICKER CompanyName" or just "CompanyName"
    words = text.split()
    if len(words) >= 2 and words[0].isupper() and len(words[0]) <= 5:
        ticker = words[0]
        company_name = " ".join(words[1:])
    else:
        # Use ticker lookup
        from utils.ticker_lookup import get_ticker, normalize_company_name
        company_name = normalize_company_name(text)
        ticker = get_ticker(text)
        if not ticker:
            ticker = text.upper().replace(" ", "")[:5]

    # Clean company name (strip "- Common Stock", "Inc.", etc.)
    from configs.company_name_filters import clean_company_name
    company_name = clean_company_name(company_name)

    return ticker, company_name


async def handle_message_send(params: dict, request_id: Any) -> dict:
    """
    Handle message/send JSON-RPC method.

    Creates a new task and starts processing in background.
    """
    message = params.get("message", {})

    # Parse request
    ticker, company_name = parse_research_request(message)

    if not ticker:
        return create_jsonrpc_response(
            request_id,
            error={"code": -32602, "message": "Invalid params: could not parse company/ticker"}
        )

    # Create task
    task_id = str(uuid.uuid4())
    now = datetime.now().isoformat()

    task = Task(
        id=task_id,
        status=TaskStatus.SUBMITTED,
        message=message,
        partial_metrics=[],
        created_at=now,
        updated_at=now
    )
    TASK_STORE[task_id] = task

    # Start background processing
    asyncio.create_task(process_research_task(task_id, ticker, company_name))

    logger.info(f"Created task {task_id} for {company_name} ({ticker})")

    return create_jsonrpc_response(request_id, result={
        "task": {
            "id": task_id,
            "status": task.status.value
        }
    })


def create_progress_callback(task_id: str):
    """Create a progress callback that receives structured metric payloads."""
    def callback(payload: dict):
        task = TASK_STORE.get(task_id)
        if task and task.status == TaskStatus.WORKING:
            # Extract fields from structured payload
            source = payload.get("source", "unknown")
            metric = payload.get("metric", "unknown")
            value = payload.get("value")
            end_date = payload.get("end_date")
            fiscal_year = payload.get("fiscal_year")
            form = payload.get("form")

            entry = MetricEntry(
                source=source,
                metric=metric,
                value=value,
                timestamp=datetime.now().isoformat(),
                end_date=end_date,
                fiscal_year=fiscal_year,
                form=form
            )
            if task.partial_metrics is None:
                task.partial_metrics = []
            task.partial_metrics.append(entry)
            task.updated_at = datetime.now().isoformat()
            temporal_info = f" ({form} {fiscal_year})" if fiscal_year else ""
            logger.info(f"Task {task_id}: [{source}] {metric} = {value}{temporal_info}")
    return callback


async def process_research_task(task_id: str, ticker: str, company_name: str):
    """Background task processor with partial metrics streaming."""
    task = TASK_STORE.get(task_id)
    if not task:
        return

    # Update to working status
    task.status = TaskStatus.WORKING
    task.updated_at = datetime.now().isoformat()

    try:
        # Create progress callback for partial metrics
        progress_callback = create_progress_callback(task_id)

        # Fetch research data with progress streaming
        logger.info(f"Task {task_id}: Starting research for {company_name} ({ticker})")
        result = await fetch_all_research_data(ticker, company_name, progress_callback)

        # Create artifact
        task.artifacts = [{
            "type": "data",
            "mimeType": "application/json",
            "data": result
        }]
        task.status = TaskStatus.COMPLETED
        logger.info(f"Task {task_id}: Research completed - {len(result.get('sources_available', []))} sources")

    except Exception as e:
        logger.error(f"Task {task_id}: Research failed - {e}")
        task.status = TaskStatus.FAILED
        task.error = str(e)

    task.updated_at = datetime.now().isoformat()


async def handle_tasks_get(params: dict, request_id: Any) -> dict:
    """
    Handle tasks/get JSON-RPC method.

    Returns the status, partial_metrics (if WORKING), and result (if COMPLETED).
    """
    task_id = params.get("taskId") or params.get("id")

    if not task_id:
        return create_jsonrpc_response(
            request_id,
            error={"code": -32602, "message": "Invalid params: taskId required"}
        )

    task = TASK_STORE.get(task_id)

    if not task:
        return create_jsonrpc_response(
            request_id,
            error={"code": -32001, "message": f"Task not found: {task_id}"}
        )

    result = {
        "task": {
            "id": task.id,
            "status": task.status.value,
            "createdAt": task.created_at,
            "updatedAt": task.updated_at
        }
    }

    # Include partial_metrics for WORKING and COMPLETED (ensures final sources aren't missed)
    if task.partial_metrics and task.status in (TaskStatus.WORKING, TaskStatus.COMPLETED):
        result["task"]["partial_metrics"] = [m.model_dump() for m in task.partial_metrics]

    if task.status == TaskStatus.COMPLETED and task.artifacts:
        result["task"]["artifacts"] = task.artifacts

    if task.status == TaskStatus.FAILED and task.error:
        result["task"]["error"] = {"message": task.error}

    return create_jsonrpc_response(request_id, result=result)


async def handle_tasks_cancel(params: dict, request_id: Any) -> dict:
    """Handle tasks/cancel JSON-RPC method."""
    task_id = params.get("taskId") or params.get("id")

    if not task_id:
        return create_jsonrpc_response(
            request_id,
            error={"code": -32602, "message": "Invalid params: taskId required"}
        )

    task = TASK_STORE.get(task_id)

    if not task:
        return create_jsonrpc_response(
            request_id,
            error={"code": -32001, "message": f"Task not found: {task_id}"}
        )

    if task.status in [TaskStatus.SUBMITTED, TaskStatus.WORKING]:
        task.status = TaskStatus.CANCELED
        task.updated_at = datetime.now().isoformat()

    return create_jsonrpc_response(request_id, result={
        "task": {"id": task.id, "status": task.status.value}
    })


# ============================================================
# MAIN ENDPOINT
# ============================================================

@app.post("/")
async def handle_jsonrpc(request: Request):
    """
    Main JSON-RPC 2.0 endpoint for A2A protocol.

    Supported methods:
    - message/send: Start a new research task
    - tasks/get: Get task status, partial_metrics, and result
    - tasks/cancel: Cancel a running task
    """
    try:
        body = await request.json()
    except Exception:
        return JSONResponse(content=create_jsonrpc_response(
            None,
            error={"code": -32700, "message": "Parse error"}
        ))

    # Validate JSON-RPC request
    if body.get("jsonrpc") != "2.0":
        return JSONResponse(content=create_jsonrpc_response(
            body.get("id"),
            error={"code": -32600, "message": "Invalid Request: must be JSON-RPC 2.0"}
        ))

    method = body.get("method")
    params = body.get("params", {})
    request_id = body.get("id")

    # Route to handler
    if method == "message/send":
        response = await handle_message_send(params, request_id)
    elif method == "tasks/get":
        response = await handle_tasks_get(params, request_id)
    elif method == "tasks/cancel":
        response = await handle_tasks_cancel(params, request_id)
    else:
        response = create_jsonrpc_response(
            request_id,
            error={"code": -32601, "message": f"Method not found: {method}"}
        )

    return JSONResponse(content=response)


# ============================================================
# HEALTH CHECK
# ============================================================

@app.get("/health")
async def health_check():
    """Health check endpoint."""
    return {
        "status": "healthy",
        "agent": "research-service",
        "version": "1.1.2",
        "protocol": "TRUE MCP (subprocess + JSON-RPC)",
        "tasks_in_memory": len(TASK_STORE),
        "capabilities": ["partial_metrics_streaming"]
    }


@app.get("/")
async def root():
    """Root endpoint with API info."""
    return {
        "name": "Research Service",
        "version": "1.1.2",
        "protocol": "A2A (JSON-RPC 2.0) + TRUE MCP (subprocess)",
        "endpoints": {
            "POST /": "JSON-RPC endpoint (message/send, tasks/get, tasks/cancel)",
            "GET /.well-known/agent.json": "Agent card",
            "GET /health": "Health check"
        }
    }


# ============================================================
# MAIN
# ============================================================

if __name__ == "__main__":
    import uvicorn
    port = int(os.getenv("PORT", "7860"))  # HuggingFace Spaces default
    logger.info(f"Starting Research Service on port {port}")
    uvicorn.run(app, host="0.0.0.0", port=port)