|
| 1 | +"""Custom workflow tasks for the cloud agent factory demo.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from typing import Dict, List, Tuple |
| 6 | + |
| 7 | +from mcp_agent.executor.workflow_task import workflow_task |
| 8 | + |
| 9 | + |
| 10 | +_KNOWLEDGE_BASE: Tuple[Dict[str, str], ...] = ( |
| 11 | + { |
| 12 | + "topic": "pricing", |
| 13 | + "summary": "Current pricing tiers: Free, Pro ($29/mo), Enterprise (custom).", |
| 14 | + "faq": ( |
| 15 | + "Pro tier includes 3 seats, Enterprise supports SSO and audit logging. " |
| 16 | + "Discounts available for annual billing." |
| 17 | + ), |
| 18 | + }, |
| 19 | + { |
| 20 | + "topic": "availability", |
| 21 | + "summary": "The service offers 99.9% uptime backed by regional failover.", |
| 22 | + "faq": ( |
| 23 | + "Scheduled maintenance occurs Sundays 02:00-03:00 UTC. " |
| 24 | + "Status page: https://status.example.com" |
| 25 | + ), |
| 26 | + }, |
| 27 | + { |
| 28 | + "topic": "integrations", |
| 29 | + "summary": "Native integrations include Slack, Jira, and Salesforce connectors.", |
| 30 | + "faq": ( |
| 31 | + "Slack integration supports slash commands. Jira integration syncs tickets " |
| 32 | + "bi-directionally every 5 minutes." |
| 33 | + ), |
| 34 | + }, |
| 35 | + { |
| 36 | + "topic": "security", |
| 37 | + "summary": "SOC 2 Type II certified, data encrypted in transit and at rest.", |
| 38 | + "faq": ( |
| 39 | + "Role-based access control is available on Pro+. Admins can require MFA. " |
| 40 | + "Security whitepaper: https://example.com/security" |
| 41 | + ), |
| 42 | + }, |
| 43 | +) |
| 44 | + |
| 45 | + |
| 46 | +@workflow_task(name="cloud_agent_factory.knowledge_base_lookup") |
| 47 | +async def knowledge_base_lookup_task(request: dict) -> List[str]: |
| 48 | + """ |
| 49 | + Return the most relevant knowledge-base snippets for a customer query. |
| 50 | +
|
| 51 | + The knowledge base is embedded in the code so the example works identically |
| 52 | + in local and hosted environments. |
| 53 | + """ |
| 54 | + |
| 55 | + query = str(request.get("query", "")).lower() |
| 56 | + limit = max(1, int(request.get("limit", 3))) |
| 57 | + |
| 58 | + if not query.strip(): |
| 59 | + return [] |
| 60 | + |
| 61 | + ranked = sorted( |
| 62 | + _KNOWLEDGE_BASE, |
| 63 | + key=lambda entry: _score(query, entry), |
| 64 | + reverse=True, |
| 65 | + ) |
| 66 | + top_entries = ranked[:limit] |
| 67 | + |
| 68 | + formatted: List[str] = [] |
| 69 | + for entry in top_entries: |
| 70 | + formatted.append( |
| 71 | + f"*Topic*: {entry['topic']}\nSummary: {entry['summary']}\nFAQ: {entry['faq']}" |
| 72 | + ) |
| 73 | + return formatted |
| 74 | + |
| 75 | + |
| 76 | +def _score(query: str, entry: Dict[str, str]) -> int: |
| 77 | + score = 0 |
| 78 | + for token in query.split(): |
| 79 | + if len(token) < 3: |
| 80 | + continue |
| 81 | + token_lower = token.lower() |
| 82 | + if token_lower in entry["topic"].lower(): |
| 83 | + score += 3 |
| 84 | + if token_lower in entry["summary"].lower(): |
| 85 | + score += 2 |
| 86 | + if token_lower in entry["faq"].lower(): |
| 87 | + score += 1 |
| 88 | + return score |
0 commit comments