-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
1325 lines (1061 loc) · 42.6 KB
/
main.py
File metadata and controls
1325 lines (1061 loc) · 42.6 KB
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
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# main.py
"""
AI Customer Support Backend
===========================
Sistema de soporte al cliente con IA que integra:
- Chat endpoint para frontend
- Procesamiento automático de emails (Gmail)
- Integración con Shopify (órdenes, productos, inventario)
- Routing inteligente por categorías
CONFIGURACIÓN REQUERIDA (Variables de Entorno):
- SHOPIFY_URL: URL de tu tienda Shopify (ej: mi-tienda.myshopify.com)
- SHOPIFY_TOKEN: Token de acceso Admin API de Shopify
- AGENT_SECRET: Secret para autenticar requests al endpoint /chat
- OPENAI_API_KEY: API Key de OpenAI
- GOOGLE_TOKEN_JSON: JSON con credenciales OAuth de Gmail (opcional, para email worker)
"""
import os
import re
import json
import httpx
import base64
import asyncio
from openai import OpenAI
from pydantic import BaseModel
from contextlib import asynccontextmanager
from typing import List, Optional, Dict, Any
from fastapi import FastAPI, HTTPException, Header
from email.mime.text import MIMEText
from googleapiclient.discovery import build
from google.auth.exceptions import RefreshError
from google.oauth2.credentials import Credentials
from google.auth.transport.requests import Request
# ============================================================================
# CONFIGURACIÓN
# ============================================================================
class Config:
"""Configuración centralizada desde variables de entorno."""
# Shopify
SHOPIFY_URL = os.getenv("SHOPIFY_URL", "")
SHOPIFY_TOKEN = os.getenv("SHOPIFY_TOKEN", "")
SHOPIFY_API_VERSION = os.getenv("SHOPIFY_API_VERSION", "2025-10")
# OpenAI
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY", "")
OPENAI_MODEL = os.getenv("OPENAI_MODEL", "gpt-4o-mini")
# Seguridad
AGENT_SECRET = os.getenv("AGENT_SECRET", "")
# Gmail (opcional)
GOOGLE_TOKEN_JSON = os.getenv("GOOGLE_TOKEN_JSON", "")
# Worker settings
EMAIL_CHECK_INTERVAL = int(os.getenv("EMAIL_CHECK_INTERVAL", "60"))
@classmethod
def validate(cls):
"""Valida que las variables críticas estén configuradas."""
required = ["SHOPIFY_URL", "SHOPIFY_TOKEN", "OPENAI_API_KEY", "AGENT_SECRET"]
missing = [var for var in required if not getattr(cls, var)]
if missing:
raise ValueError(f"Variables de entorno faltantes: {', '.join(missing)}")
# ============================================================================
# GMAIL SERVICE
# ============================================================================
def get_gmail_service():
"""
Autentica con Gmail usando credenciales desde variables de entorno.
Maneja el refresh del token automáticamente en memoria.
Returns:
Gmail API service object o None si falla la autenticación.
"""
token_json = Config.GOOGLE_TOKEN_JSON
if not token_json:
print("⚠️ [Gmail] No se encontró GOOGLE_TOKEN_JSON en variables.")
return None
try:
creds_dict = json.loads(token_json)
creds = Credentials.from_authorized_user_info(creds_dict)
if creds and creds.expired and creds.refresh_token:
try:
creds.refresh(Request())
print("🔄 [Gmail] Token refrescado correctamente.")
except RefreshError:
print("❌ [Gmail] Error refrescando token. Se requiere re-autenticación manual.")
return None
return build("gmail", "v1", credentials=creds)
except Exception as e:
print(f"❌ [Gmail] Error de conexión: {str(e)}")
return None
def list_unread_emails(service, max_results: int = 10):
"""
Busca correos NO LEÍDOS en el Inbox.
Args:
service: Gmail API service object
max_results: Número máximo de correos a retornar
Returns:
Lista de mensajes o lista vacía si hay error.
"""
try:
results = service.users().messages().list(
userId="me",
q="is:unread in:inbox",
maxResults=max_results
).execute()
return results.get("messages", [])
except Exception as e:
print(f"⚠️ Error listando emails: {e}")
return []
def get_email_content(service, msg_id: str) -> Optional[Dict[str, Any]]:
"""
Obtiene el contenido del correo actual Y el contexto del hilo (historial).
Args:
service: Gmail API service object
msg_id: ID del mensaje a obtener
Returns:
Diccionario con: id, thread_id, subject, sender, body, history
o None si hay error.
"""
try:
msg = service.users().messages().get(userId="me", id=msg_id, format="full").execute()
headers = msg["payload"]["headers"]
thread_id = msg.get("threadId")
subject = next((h["value"] for h in headers if h["name"] == "Subject"), "No Subject")
sender = next((h["value"] for h in headers if h["name"] == "From"), "Unknown")
# Extracción del cuerpo del mensaje
body = _extract_email_body(msg["payload"])
# Obtener historial del hilo
history_text = _get_thread_history(service, thread_id, msg_id)
return {
"id": msg_id,
"thread_id": thread_id,
"subject": subject,
"sender": sender,
"body": body,
"history": history_text
}
except Exception as e:
print(f"⚠️ Error leyendo email {msg_id}: {e}")
return None
def _extract_email_body(payload: Dict) -> str:
"""Extrae el cuerpo de texto plano del payload del email."""
body = ""
if "parts" in payload:
for part in payload["parts"]:
if part["mimeType"] == "text/plain":
data = part["body"].get("data", "")
if data:
body = base64.urlsafe_b64decode(data).decode("utf-8")
break
else:
data = payload["body"].get("data", "")
if data:
body = base64.urlsafe_b64decode(data).decode("utf-8")
return body
def _get_thread_history(service, thread_id: str, current_msg_id: str, max_messages: int = 3) -> str:
"""Obtiene el historial de mensajes anteriores del hilo."""
if not thread_id:
return ""
try:
thread = service.users().threads().get(userId="me", id=thread_id).execute()
messages = thread.get('messages', [])
# Mensajes anteriores al actual
prev_messages = [m for m in messages if m['id'] != current_msg_id][-max_messages:]
if not prev_messages:
return ""
history_text = "--- THREAD HISTORY ---\n"
for m in prev_messages:
snippet = m.get('snippet', 'No content')
history_text += f"- {snippet}\n"
history_text += "--- END HISTORY ---\n"
return history_text
except Exception as e:
print(f"⚠️ Error recuperando historial del hilo {thread_id}: {e}")
return ""
def create_draft(service, to: str, subject: str, body_html: str):
"""
Crea un BORRADOR en Gmail (No envía, solo guarda).
Args:
service: Gmail API service object
to: Dirección de destino
subject: Asunto del email
body_html: Cuerpo del email en HTML
Returns:
Draft object o None si hay error.
"""
try:
message = MIMEText(body_html, "html")
message["to"] = to
message["subject"] = subject if subject.lower().startswith("re:") else f"Re: {subject}"
raw = base64.urlsafe_b64encode(message.as_bytes()).decode("utf-8")
body = {"message": {"raw": raw}}
draft = service.users().drafts().create(userId="me", body=body).execute()
print(f"✅ [Gmail] Borrador creado: {draft['id']}")
return draft
except Exception as e:
print(f"❌ Error creando borrador: {e}")
return None
def mark_as_read(service, msg_id: str):
"""Quita la etiqueta UNREAD para no procesarlo de nuevo."""
try:
service.users().messages().modify(
userId="me",
id=msg_id,
body={"removeLabelIds": ["UNREAD"]}
).execute()
except Exception as e:
print(f"⚠️ Error marcando como leído: {e}")
# ============================================================================
# OPENAI CLIENT
# ============================================================================
client = None # Se inicializa en el lifespan
def get_openai_client():
"""Obtiene o crea el cliente de OpenAI."""
global client
if client is None:
client = OpenAI(api_key=Config.OPENAI_API_KEY)
return client
# ============================================================================
# FASTAPI APP
# ============================================================================
@asynccontextmanager
async def lifespan(app: FastAPI):
"""Lifecycle manager para la aplicación FastAPI."""
# Validar configuración al inicio
try:
Config.validate()
except ValueError as e:
print(f"⚠️ Configuración incompleta: {e}")
# Inicializar cliente OpenAI
get_openai_client()
# Iniciar worker de email si Gmail está configurado
email_task = None
if Config.GOOGLE_TOKEN_JSON:
email_task = asyncio.create_task(email_background_task())
print("🚀 [Email Worker] Iniciado.")
else:
print("ℹ️ [Email Worker] Deshabilitado (GOOGLE_TOKEN_JSON no configurado)")
yield
# Cleanup
if email_task:
email_task.cancel()
app = FastAPI(
title="AI Customer Support Backend",
description="Backend de soporte al cliente con IA, integración Shopify y Gmail",
version="1.0.0",
lifespan=lifespan
)
# ============================================================================
# MODELOS DE DATOS
# ============================================================================
class ChatRequest(BaseModel):
"""Request para el endpoint de chat."""
message: str
history: Optional[List[Dict[str, Any]]] = None
class ChatResponse(BaseModel):
"""Response del endpoint de chat."""
response: str
category: str
history: List[Dict[str, Any]]
# ============================================================================
# PROMPTS DEL SISTEMA
# ============================================================================
# Tono común para todos los agentes
# PERSONALIZA: Ajusta el nombre, empresa y tono según tu marca
COMMON_TONE = """
TONE & PERSONA:
- You are the Customer Success Manager at the company.
- Tone: Warm, empathetic, solution-oriented (NOT robotic).
- Use appropriate emojis naturally.
- Language Rule:
1. DETECT the language of the user's last message.
2. REPLY IN THAT EXACT SAME LANGUAGE.
3. Translate any internal terms to the user's language.
DATA INTEGRITY & PRIVACY RULE (ZERO TRUST):
1. TOOL USAGE: You DO NOT have direct database access. You MUST use tools to get data.
2. PRIVACY SHIELD: If order lookup returns "not found" or "email mismatch", do NOT reveal any info.
3. REALITY CHECK: If stock is 0, say "Sold out". Do not guess availability.
"""
# Categorías de agentes especializados
# PERSONALIZA: Ajusta las categorías y scripts según tu negocio
AGENT_CATEGORIES = [
"OrderPlacementStatus",
"ShippingDelivery",
"ReturnsCancellationsExchanges",
"PaymentBilling",
"ProductInfoAvailability",
"TechnicalIssues",
"PromotionsDiscountsPricing",
"CustomerComplaintsSatisfaction",
"AccountProfileOther"
]
# Prompts por categoría
# PERSONALIZA: Estos prompts definen el comportamiento de cada agente especializado
AGENT_PROMPTS = {
"OrderPlacementStatus": f"""
{COMMON_TONE}
ROLE: Order Status Specialist.
GOAL: Explain order status based on `fulfillment_status`.
SCENARIOS:
1. Status "Unfulfilled" / "Paid": Order confirmed, pending fulfillment.
2. Status "Partially complete": Split shipment - some items shipped separately.
3. Missing confirmation email: Check spam folder or offer to resend.
TOOL TO USE: `lookup_order_crm`
""",
"ShippingDelivery": f"""
{COMMON_TONE}
ROLE: Shipping Specialist.
LOGISTICS RULES:
1. Standard delivery: Provide estimated timeframes.
2. International shipments: May take longer due to customs.
3. Tracking not updating: Package may be awaiting carrier scan.
TOOL TO USE: `lookup_order_crm`
""",
"ReturnsCancellationsExchanges": f"""
{COMMON_TONE}
ROLE: Returns & Cancellations Specialist.
RULES:
1. Check order status first.
2. If "Unfulfilled": Can be cancelled.
3. If "Fulfilled": Too late to cancel, offer return process.
Provide return instructions and policy links as needed.
""",
"PaymentBilling": f"""
{COMMON_TONE}
ROLE: Billing Support.
COMMON ISSUES:
- Double Charge: Usually a bank authorization hold.
- Refunds: Processing time varies by payment method.
- Payment failures: Suggest alternative payment methods.
""",
"ProductInfoAvailability": f"""
{COMMON_TONE}
ROLE: Product Expert.
INSTRUCTIONS:
1. Use `lookup_product_intelligence` for stock, care instructions, specs.
2. Use actual data from the tool, do not guess.
3. If stock is 0: Suggest waitlist or alternatives.
""",
"TechnicalIssues": f"""
{COMMON_TONE}
ROLE: Tech Support.
COMMON FIXES:
- Checkout issues: Try incognito mode or different browser.
- Page errors: Clear cache, try again.
- Severe issues: Use `escalate_ticket_to_support`.
""",
"PromotionsDiscountsPricing": f"""
{COMMON_TONE}
ROLE: Promotions Manager.
CONTEXT: Check Active Discounts in system context.
SCENARIOS:
- Code not working: Verify if code exists and is valid.
- Stacking discounts: Check if codes can be combined.
""",
"CustomerComplaintsSatisfaction": f"""
{COMMON_TONE}
ROLE: Escalation Manager.
TRIGGERS FOR ESCALATION (HIGH PRIORITY):
- Legal threats, fraud accusations, severe complaints.
- Action: Use `escalate_ticket_to_support`.
For missing items: First check if it's a split shipment.
""",
"AccountProfileOther": f"""
{COMMON_TONE}
ROLE: General Assistant.
Handle: Account issues, password resets, general inquiries.
Ignore: B2B spam, unsolicited partnerships.
"""
}
# ============================================================================
# SHOPIFY TOOLS
# ============================================================================
async def _get_global_store_context() -> str:
"""
Recupera Descuentos y Políticas actuales de Shopify.
Inyecta contexto real en el prompt para evitar alucinaciones.
"""
discounts_text = "ACTIVE DISCOUNTS:\n"
policies_text = "STORE POLICIES:\n"
try:
async with httpx.AsyncClient() as http_client:
# Obtener Price Rules (Descuentos)
res_price = await http_client.get(
f"https://{Config.SHOPIFY_URL}/admin/api/{Config.SHOPIFY_API_VERSION}/price_rules.json",
headers={"X-Shopify-Access-Token": Config.SHOPIFY_TOKEN}
)
if res_price.status_code == 200:
rules = res_price.json().get("price_rules", [])
for r in rules:
val = f"-{r['value']}" if r['value_type'] == 'fixed_amount' else f"{r['value']}%"
discounts_text += f"- {r['title']} ({val} OFF)\n"
# Obtener Políticas
res_pol = await http_client.get(
f"https://{Config.SHOPIFY_URL}/admin/api/{Config.SHOPIFY_API_VERSION}/policies.json",
headers={"X-Shopify-Access-Token": Config.SHOPIFY_TOKEN}
)
if res_pol.status_code == 200:
for p in res_pol.json().get("policies", []):
policies_text += f"- {p['title']}: {p['url']}\n"
except Exception as e:
print(f"⚠️ Error obteniendo contexto de tienda: {e}")
return discounts_text + "\n" + policies_text
async def _lookup_product_intelligence(search_term: str) -> str:
"""
Busca producto con Metafields e Inventario Real.
Args:
search_term: Nombre o término de búsqueda del producto.
Returns:
JSON string con información del producto.
"""
print(f"🔍 [Shopify] Analizando producto: '{search_term}'")
headers = {"X-Shopify-Access-Token": Config.SHOPIFY_TOKEN}
try:
async with httpx.AsyncClient() as http_client:
# Buscar Producto
res = await http_client.get(
f"https://{Config.SHOPIFY_URL}/admin/api/{Config.SHOPIFY_API_VERSION}/products.json",
params={"limit": 1, "title": search_term},
headers=headers
)
products = res.json().get("products", [])
if not products:
return json.dumps({"found": False, "message": "Product not found."})
prod = products[0]
product_id = prod['id']
# Obtener Metafields
res_meta = await http_client.get(
f"https://{Config.SHOPIFY_URL}/admin/api/{Config.SHOPIFY_API_VERSION}/products/{product_id}/metafields.json",
headers=headers
)
metafields_data = {}
if res_meta.status_code == 200:
for m in res_meta.json().get("metafields", []):
metafields_data[m['key']] = str(m['value'])[:200]
# Calcular Stock Total
variants_info = []
total_stock = 0
for variant in prod['variants']:
inv_item_id = variant['inventory_item_id']
res_inv = await http_client.get(
f"https://{Config.SHOPIFY_URL}/admin/api/{Config.SHOPIFY_API_VERSION}/inventory_levels.json",
params={"inventory_item_ids": inv_item_id},
headers=headers
)
stock_qty = 0
if res_inv.status_code == 200:
for level in res_inv.json().get("inventory_levels", []):
stock_qty += level.get('available', 0)
total_stock += stock_qty
variants_info.append(f"{variant['title']} (Stock: {stock_qty})")
return json.dumps({
"found": True,
"title": prod['title'],
"tags": prod.get('tags', ''),
"total_stock": total_stock,
"variants_breakdown": variants_info,
"metafields_data": metafields_data,
"image_url": prod['images'][0]['src'] if prod.get('images') else None
})
except Exception as e:
print(f"❌ Error en lookup_product: {e}")
return json.dumps({"error": str(e)})
async def _lookup_order_crm(email: str = None, order_number: str = None) -> str:
"""
Busca Pedido + Estado + Datos del Cliente.
Args:
email: Email del cliente
order_number: Número de orden (opcional)
Returns:
JSON string con información de la orden.
"""
print(f"🔍 [Shopify] Buscando Orden: {order_number or email}")
headers = {"X-Shopify-Access-Token": Config.SHOPIFY_TOKEN}
try:
async with httpx.AsyncClient() as http_client:
params = {"status": "any", "limit": 1}
if order_number:
params["name"] = order_number.replace("#", "").strip()
elif email:
params["email"] = email.strip()
res = await http_client.get(
f"https://{Config.SHOPIFY_URL}/admin/api/{Config.SHOPIFY_API_VERSION}/orders.json",
params=params,
headers=headers
)
orders = res.json().get("orders", [])
if not orders:
return json.dumps({"found": False, "message": "Order not found."})
order = orders[0]
# Obtener info del cliente
customer_profile = "Guest Checkout"
if order.get("customer"):
cust_id = order["customer"]["id"]
res_cust = await http_client.get(
f"https://{Config.SHOPIFY_URL}/admin/api/{Config.SHOPIFY_API_VERSION}/customers/{cust_id}.json",
headers=headers
)
if res_cust.status_code == 200:
c = res_cust.json().get("customer", {})
total_spent = f"{c.get('total_spent', '0')} {c.get('currency', '')}"
orders_count = c.get('orders_count', 0)
customer_profile = f"Returning customer: {total_spent} spent ({orders_count} orders)"
# Extraer tracking
tracking_numbers = []
for f in order.get('fulfillments', []):
if f.get('tracking_number'):
tracking_numbers.append(f['tracking_number'])
return json.dumps({
"found": True,
"order_number": order['name'],
"financial": order['financial_status'],
"fulfillment": order.get('fulfillment_status') or "Unfulfilled",
"items": [f"{i['quantity']}x {i['title']}" for i in order['line_items']],
"tracking": tracking_numbers,
"customer_profile": customer_profile
})
except Exception as e:
print(f"❌ Error en lookup_order: {e}")
return json.dumps({"error": str(e)})
async def _get_shopify_products(search_term: str = None) -> str:
"""
Busca productos con stock usando GraphQL.
Args:
search_term: Término de búsqueda opcional.
Returns:
JSON string con lista de productos.
"""
print(f"🔍 [Shopify] Buscando productos: '{search_term}'")
url = f"https://{Config.SHOPIFY_URL}/admin/api/{Config.SHOPIFY_API_VERSION}/graphql.json"
headers = {
"X-Shopify-Access-Token": Config.SHOPIFY_TOKEN,
"Content-Type": "application/json"
}
query_filter = "status:active"
if search_term:
query_filter += f" AND title:*{search_term}*"
query = f'''
{{
products(first: 5, query: "{query_filter}") {{
edges {{
node {{
id
title
onlineStoreUrl
variants(first: 10) {{
edges {{
node {{
title
sku
inventoryQuantity
}}
}}
}}
}}
}}
}}
}}
'''
try:
async with httpx.AsyncClient() as http_client:
response = await http_client.post(url, json={"query": query}, headers=headers)
if response.status_code != 200:
return json.dumps({"error": f"Shopify API Error: {response.status_code}"})
data = response.json()
if "errors" in data:
return json.dumps({"error": data["errors"]})
raw_products = data.get("data", {}).get("products", {}).get("edges", [])
catalog = []
for edge in raw_products:
prod = edge["node"]
total_stock = sum(
v["node"]["inventoryQuantity"]
for v in prod["variants"]["edges"]
)
catalog.append({
"name": prod["title"],
"url": prod["onlineStoreUrl"],
"status": "In Stock" if total_stock > 0 else "Sold Out",
"total_stock": total_stock
})
if not catalog:
return json.dumps({"found": False, "message": "No products found."})
return json.dumps({"found": True, "products": catalog})
except Exception as e:
print(f"❌ Error en get_products: {e}")
return json.dumps({"error": str(e)})
async def _get_shopify_order(order_number: str, email: str) -> str:
"""
Obtiene estado de orden en tiempo real con validación de email.
Args:
order_number: Número de orden
email: Email para validación de seguridad
Returns:
JSON string con información de la orden.
"""
clean_number = order_number.replace("#", "").strip() if order_number else None
clean_email = email.strip().lower() if email else None
print(f"🔍 [Shopify] Buscando: Orden='{clean_number}', Email='{clean_email}'")
if not clean_email:
return json.dumps({"found": False, "error": "Email is required for verification."})
params = {"status": "any"}
if clean_number:
params["name"] = clean_number
else:
params["email"] = clean_email
params["limit"] = 1
params["order"] = "created_at desc"
try:
async with httpx.AsyncClient() as http_client:
response = await http_client.get(
f"https://{Config.SHOPIFY_URL}/admin/api/{Config.SHOPIFY_API_VERSION}/orders.json",
params=params,
headers={"X-Shopify-Access-Token": Config.SHOPIFY_TOKEN}
)
if response.status_code != 200:
return json.dumps({"found": False, "error": "Shopify API Error"})
orders = response.json().get("orders", [])
target_order = None
# Validación de seguridad
if clean_number:
for order in orders:
order_name = str(order.get("name", "")).replace("#", "").strip()
if order_name == clean_number and order.get("email", "").lower() == clean_email:
target_order = order
break
else:
if orders:
target_order = orders[0]
if not target_order:
return json.dumps({"found": False, "error": "Order not found or email mismatch."})
# Mapeo de estados
status_map = {
"fulfilled": "Fulfilled",
"partial": "Partially Fulfilled",
"restocked": "Restocked"
}
fulfillment_status = status_map.get(
target_order.get("fulfillment_status"),
"Unfulfilled"
)
# Tracking
tracking_numbers = []
tracking_urls = []
for f in target_order.get("fulfillments", []):
if f.get("tracking_number"):
tracking_numbers.append(f["tracking_number"])
if f.get("tracking_url"):
tracking_urls.append(f["tracking_url"])
return json.dumps({
"found": True,
"order_id": target_order["name"],
"date": target_order["created_at"][:10],
"payment": target_order["financial_status"],
"status": fulfillment_status,
"tracking_numbers": tracking_numbers,
"tracking_urls": tracking_urls,
"items": [f"{item['quantity']}x {item['name']}" for item in target_order["line_items"]],
"total": f"{target_order['total_price']} {target_order['currency']}"
})
except Exception as e:
print(f"❌ Error en get_order: {e}")
return json.dumps({"error": str(e)})
async def _create_ticket_internal(category: str, email: str, summary: str, priority: str) -> str:
"""
Crea un ticket de soporte interno.
PERSONALIZA: Integra con tu sistema de tickets (Zendesk, Freshdesk, etc.)
"""
print(f"🎫 [Tickets] Creando: {category} - {summary}")
# TODO: Implementar integración con tu sistema de tickets
# Ejemplo: await zendesk_client.create_ticket(...)
return json.dumps({
"success": True,
"ticket_id": f"TICKET-{asyncio.get_event_loop().time():.0f}",
"message": "Ticket created successfully"
})
# ============================================================================
# TOOLS SCHEMA PARA OPENAI
# ============================================================================
TOOLS_SCHEMA = [
{
"type": "function",
"function": {
"name": "lookup_order_crm",
"description": "Find order details and customer profile/history.",
"parameters": {
"type": "object",
"properties": {
"order_number": {"type": "string", "description": "Order number (e.g. #1234)"},
"email": {"type": "string", "description": "Customer email"}
}
}
}
},
{
"type": "function",
"function": {
"name": "lookup_product_intelligence",
"description": "Find product stock, care instructions, and specifications.",
"parameters": {
"type": "object",
"properties": {
"search_term": {"type": "string", "description": "Product name to search"}
},
"required": ["search_term"]
}
}
},
{
"type": "function",
"function": {
"name": "lookup_order_admin",
"description": "Get real-time order status with tracking.",
"parameters": {
"type": "object",
"properties": {
"order_number": {"type": "string"},
"email": {"type": "string"}
},
"required": ["email"]
}
}
},
{
"type": "function",
"function": {
"name": "lookup_product_stock",
"description": "Search products and check real-time inventory.",
"parameters": {
"type": "object",
"properties": {
"search_term": {"type": "string", "description": "Product name (e.g. 'T-Shirt')"}
},
"required": ["search_term"]
}
}
},
{
"type": "function",
"function": {
"name": "escalate_ticket_to_support",
"description": "Create a support ticket for human review.",
"parameters": {
"type": "object",
"properties": {
"category": {"type": "string", "description": "Ticket category"},
"email": {"type": "string", "description": "Customer email"},
"summary": {"type": "string", "description": "Issue summary"},
"priority": {"type": "string", "enum": ["low", "medium", "high", "urgent"]}
},
"required": ["category", "email", "summary", "priority"]
}
}
}
]
# ============================================================================
# AI PROCESSING
# ============================================================================
async def process_ai_response(user_message: str, history: List[Dict] = None) -> tuple[str, str]:
"""
Procesa mensaje con IA: rutea, ejecuta tools y genera respuesta.
Args:
user_message: Mensaje del usuario
history: Historial de conversación previo
Returns:
Tuple de (respuesta, categoría)
"""
if history is None:
history = []
openai_client = get_openai_client()
# 1. ROUTER - Clasificar categoría
router_prompt = f"""
Classify the user's message into exactly one category:
{AGENT_CATEGORIES}
Rules:
- Ignore empty or "No Subject" subject lines
- If message sounds like B2B sales pitch, classify as 'AccountProfileOther'
Output ONLY the category name.
"""
try:
router_res = openai_client.chat.completions.create(
model=Config.OPENAI_MODEL,
messages=[
{"role": "system", "content": router_prompt},
{"role": "user", "content": user_message}
]
)
category = router_res.choices[0].message.content.strip()
except Exception as e:
print(f"⚠️ Router error: {e}")
category = "AccountProfileOther"
if category not in AGENT_PROMPTS:
category = "AccountProfileOther"
print(f"🧠 [AI] Categoría: {category}")
# 2. Obtener contexto de tienda
store_context = await _get_global_store_context()
# 3. Construir prompt del agente
system_prompt = f"""
{AGENT_PROMPTS[category]}
=== REAL-TIME STORE DATA ===
{store_context}
Remember: Use tools to get accurate data. Never guess.
"""
messages = [{"role": "system", "content": system_prompt}]
messages.extend(history)
messages.append({"role": "user", "content": user_message})
# 4. Primera llamada a OpenAI
completion = openai_client.chat.completions.create(
model=Config.OPENAI_MODEL,
messages=messages,
tools=TOOLS_SCHEMA,