-
Notifications
You must be signed in to change notification settings - Fork 286
Expand file tree
/
Copy pathmain.py
More file actions
1378 lines (1209 loc) · 63.1 KB
/
main.py
File metadata and controls
1378 lines (1209 loc) · 63.1 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
import json
import re
import asyncio
import base64
import uuid
import time
import xml.etree.ElementTree as ET
from datetime import datetime, timedelta, timezone
import requests
from src.agent.capability import MatchingCapability
from src.main import AgentWorker
from src.agent.capability_worker import CapabilityWorker
# =============================================================================
# PACKAGE TRACKER ABILITY — Direct Carrier API Version
# Tracks packages via UPS, FedEx, USPS, and DHL direct carrier APIs.
# No third-party aggregator. Configure only the carriers you have credentials for.
# =============================================================================
# ── FedEx ────────────────────────────────────────────────────────────────────
# Get credentials from https://developer.fedex.com → My Apps → Track API scope
FEDEX_API_KEY = "your_fedex_api_key" # ← paste here
FEDEX_SECRET_KEY = "your_fedex_secret_key" # ← paste here
FEDEX_USE_SANDBOX = True # Set False for production (apis.fedex.com)
# ── UPS ──────────────────────────────────────────────────────────────────────
# Get credentials from https://developer.ups.com → Add App → Track product
UPS_CLIENT_ID = "your_ups_client_id"
UPS_CLIENT_SECRET = "your_ups_client_secret"
UPS_USE_CIE = True # Set False for production (onlinetools.ups.com)
# ── USPS ─────────────────────────────────────────────────────────────────────
# Register at https://registration.shippingapis.com/
USPS_USER_ID = "your_usps_user_id"
# USPS has no sandbox — the same endpoint handles all requests
# ── DHL ──────────────────────────────────────────────────────────────────────
# Get API key from https://developer.dhl.com → Shipment Tracking - Unified
DHL_API_KEY = "your_dhl_api_key"
DHL_USE_TEST = True # Set False for production
# ── Derived base URLs ─────────────────────────────────────────────────────────
FEDEX_BASE_URL = "https://apis-sandbox.fedex.com" if FEDEX_USE_SANDBOX else "https://apis.fedex.com"
UPS_BASE_URL = "https://wwwcie.ups.com" if UPS_USE_CIE else "https://onlinetools.ups.com"
USPS_BASE_URL = "https://secure.shippingapis.com"
DHL_BASE_URL = "https://api.dhl.com"
PACKAGES_FILE = "pkgtracker_packages.json"
MAX_PACKAGES = 20
DELIVERED_CLEANUP_DAYS = 2
EXIT_WORDS = [
"done", "exit", "stop", "quit", "bye", "goodbye",
"nothing else", "all good", "nope", "no thanks", "i'm good",
"no", "that's it", "that's all",
]
CANCEL_WORDS = ["never mind", "cancel", "forget it", "nevermind"]
# Normalized status tags (all lowercase) → voice descriptions
STATUS_DESCRIPTIONS = {
"delivered": "Your package was delivered.",
"out_for_delivery": "Your package is out for delivery today.",
"in_transit": "Your package is in transit.",
"exception": "There's an issue with your package. Check the carrier website for details.",
"returned": "Your package is being returned to the sender.",
"held": "Your package is being held for pickup.",
"cancelled": "This shipment has been cancelled.",
"label_created": "A label was created but the carrier hasn't picked it up yet.",
"pending": "Tracking registered but no carrier scan yet.",
"unknown": "Status not available.",
}
CARRIER_DISPLAY = {
"ups": "UPS",
"fedex": "FedEx",
"usps": "USPS",
"dhl": "DHL",
"amazon": "Amazon Logistics",
}
# ── LLM prompt templates ──────────────────────────────────────────────────────
INTENT_CLASSIFICATION_PROMPT = """Classify this user input for a package tracking ability. Return ONLY valid JSON.
The user may want to:
- "add" a new tracking number
- "status_all" check status of all packages
- "status_one" check status of a specific package (include the nickname they mentioned)
- "list" their tracked packages
- "remove" a package from tracking (include the nickname they mentioned)
If unsure, default to "status_all".
Return format: {"mode": "add|status_all|status_one|list|remove", "nickname": "optional nickname if mentioned"}
User input: {user_input}
Recent conversation context: {context}"""
TRACKING_NUMBER_EXTRACT_PROMPT = """The user is providing a package tracking number. They may have typed it, pasted it, or spoken it aloud.
Extract ONLY the alphanumeric tracking number. Remove any filler words, spaces, or descriptions.
Return ONLY the tracking number string, nothing else.
User input: {user_input}"""
STATUS_SUMMARY_PROMPT = """You are a concise package status assistant. Summarize the user's package tracking updates in 2-4 sentences for voice.
Prioritize: packages out for delivery today, packages with status changes, then everything else.
Use nicknames, not tracking numbers. Include expected delivery dates when available. Be brief.
Package data:
{package_data}"""
NICKNAME_MATCH_PROMPT = """The user is asking about a specific package. Match their request to one of these tracked packages.
Return ONLY the package ID that best matches. If no good match, return "none".
User said: {user_input}
Tracked packages:
{packages_list}
Return ONLY the package ID string, nothing else."""
class PackageTrackerCapability(MatchingCapability):
worker: AgentWorker = None
capability_worker: CapabilityWorker = None
packages: list = None
initial_request: str = None
# OAuth token cache — persist across invocations of the same instance
_fedex_token: str = None
_fedex_token_expires: float = 0.0
_ups_token: str = None
_ups_token_expires: float = 0.0
# {{register_capability}}
def call(self, worker: AgentWorker):
self.worker = worker
self.capability_worker = CapabilityWorker(self.worker)
self.packages = []
self.initial_request = ""
try:
val = worker.live_transcription
if val and isinstance(val, str):
self.initial_request = val.strip()
except AttributeError:
pass
self.worker.session_tasks.create(self.run())
# ─────────────────────────────────────────────────────────────────────────
# MAIN ENTRY POINT
# ─────────────────────────────────────────────────────────────────────────
async def run(self):
try:
if not self._check_api_keys():
await self.capability_worker.speak(
"I need at least one carrier API key to track packages. "
"Please add your FedEx, UPS, USPS, or DHL credentials in the ability settings."
)
return
await self._load_packages()
await self._cleanup_delivered()
if self.initial_request:
intent = self._classify_trigger_intent(self._get_trigger_context())
mode = intent.get("mode", "status_all")
self.log_info(f"Package Tracker activated (live_transcription). Mode: {mode}")
elif not self.packages:
mode = "add"
intent = {"mode": "add", "nickname": None}
self.log_info("Package Tracker activated. No packages on file → add mode.")
else:
# Path 3: has packages, trigger phrase not captured by live_transcription.
# Ask a direct yes/no question. "Are you adding a tracking number?"
# maps well to the most common trigger ("track a package"). If yes →
# add mode immediately. If no → check status. This avoids the user
# having to figure out what to say after hearing a vague "Package tracker."
response = await self.capability_worker.run_io_loop(
"Package tracker. Are you adding a new tracking number?"
)
if not response or self._is_exit(response):
return
lower = response.lower().strip()
# Fast-path: map natural "yes / add / track" signals to add mode
# without needing LLM classification.
ADD_SIGNALS = ["yes", "yeah", "yep", "yup", "add", "new", "track",
"number", "id", "sure", "ok", "okay"]
if any(sig in lower for sig in ADD_SIGNALS):
mode = "add"
intent = {"mode": "add", "nickname": None}
self.log_info("Package Tracker activated (Path 3 yes). Mode: add")
else:
# "no", "check", "status", or anything else → classify normally
self.initial_request = response
intent = self._classify_trigger_intent(self._get_trigger_context())
mode = intent.get("mode", "status_all")
self.log_info(f"Package Tracker activated (Path 3 no). Mode: {mode}")
if mode == "add":
await self._handle_add()
elif mode == "status_all":
await self._handle_status_all()
elif mode == "status_one":
await self._handle_status_one(intent.get("nickname"))
elif mode == "list":
await self._handle_list()
elif mode == "remove":
await self._handle_remove(intent.get("nickname"))
else:
await self._handle_status_all()
except Exception as e:
self.log_err(f"Package Tracker unhandled error: {e}")
await self.capability_worker.speak(
"Something went wrong with the package tracker. Please try again."
)
finally:
self.capability_worker.resume_normal_flow()
# ─────────────────────────────────────────────────────────────────────────
# API KEY CHECKS
# ─────────────────────────────────────────────────────────────────────────
def _check_api_keys(self) -> bool:
return any([
self._carrier_configured("fedex"),
self._carrier_configured("ups"),
self._carrier_configured("usps"),
self._carrier_configured("dhl"),
])
def _carrier_configured(self, carrier: str) -> bool:
if carrier == "fedex":
return (FEDEX_API_KEY not in ("", "your_fedex_api_key")
and FEDEX_SECRET_KEY not in ("", "your_fedex_secret_key"))
if carrier == "ups":
return (UPS_CLIENT_ID not in ("", "your_ups_client_id")
and UPS_CLIENT_SECRET not in ("", "your_ups_client_secret"))
if carrier == "usps":
return USPS_USER_ID not in ("", "your_usps_user_id")
if carrier == "dhl":
return DHL_API_KEY not in ("", "your_dhl_api_key")
return False
# ─────────────────────────────────────────────────────────────────────────
# CARRIER AUTO-DETECTION (from tracking number format)
# ─────────────────────────────────────────────────────────────────────────
def _detect_carrier_from_number(self, tracking_number: str) -> str:
"""Best-effort detection from tracking number format.
Returns: 'ups' | 'fedex' | 'usps' | 'dhl' | 'amazon' | 'unknown'."""
t = tracking_number.upper().replace(" ", "").replace("-", "")
if t.startswith("TBA"):
return "amazon" # Amazon Logistics
if re.match(r"^1Z[A-Z0-9]{16}$", t):
return "ups" # UPS 18-char
if re.match(r"^(JJD|JVGL|GM|LX|RX|3S)", t):
return "dhl" # DHL Express prefixes
if re.match(r"^\d{10}$", t):
return "dhl" # DHL Express 10-digit
if re.match(r"^9[2-6]\d{18,}$", t):
return "usps" # USPS 20+ digit
if re.match(r"^[A-Z]{2}\d{9}US$", t):
return "usps" # USPS international
if t.isdigit() and len(t) in (12, 15, 20):
return "fedex" # FedEx numeric
if re.match(r"^96\d{20}$", t):
return "fedex" # FedEx Ground 96-prefix
return "unknown"
def _parse_carrier_from_text(self, text: str) -> str:
"""Parse carrier name from user speech. Returns carrier key or None."""
lower = text.lower()
if "ups" in lower:
return "ups"
if "fedex" in lower or "fed ex" in lower:
return "fedex"
if "usps" in lower or "postal" in lower or "post office" in lower:
return "usps"
if "dhl" in lower:
return "dhl"
return None
# ─────────────────────────────────────────────────────────────────────────
# FEDEX API
# ─────────────────────────────────────────────────────────────────────────
def _get_fedex_token(self) -> str:
"""Return a cached or freshly-obtained FedEx OAuth token."""
now = time.time()
if self._fedex_token and now < self._fedex_token_expires - 60:
return self._fedex_token
try:
resp = requests.post(
f"{FEDEX_BASE_URL}/oauth/token",
headers={"Content-Type": "application/x-www-form-urlencoded"},
data={
"grant_type": "client_credentials",
"client_id": FEDEX_API_KEY,
"client_secret": FEDEX_SECRET_KEY,
},
timeout=10,
)
if resp.status_code == 200:
data = resp.json()
self._fedex_token = data.get("access_token")
self._fedex_token_expires = now + int(data.get("expires_in", 3600))
return self._fedex_token
self.log_err(f"FedEx token error: {resp.status_code} {resp.text[:200]}")
return None
except Exception as e:
self.log_err(f"FedEx token exception: {e}")
return None
def _track_fedex(self, tracking_number: str) -> dict:
"""Track a FedEx package. Returns normalized status dict or {'error': ...}."""
try:
token = self._get_fedex_token()
if not token:
return {"error": "FedEx authentication failed. Check your API key and secret."}
resp = requests.post(
f"{FEDEX_BASE_URL}/track/v1/trackingnumbers",
headers={
"Authorization": f"Bearer {token}",
"Content-Type": "application/json",
"X-locale": "en_US",
},
json={
"trackingInfo": [
{"trackingNumberInfo": {"trackingNumber": tracking_number}}
],
"includeDetailedScans": True,
},
timeout=15,
)
if resp.status_code != 200:
self.log_err(f"FedEx track error: {resp.status_code} {resp.text[:300]}")
return {"error": f"FedEx returned HTTP {resp.status_code}"}
data = resp.json()
complete = data.get("output", {}).get("completeTrackResults", [])
if not complete:
return {"error": "No FedEx tracking results returned"}
result = complete[0].get("trackResults", [{}])[0]
# Check for errors embedded in the result
errors = result.get("error", {})
if errors:
err_msg = errors.get("message", "Tracking not found")
self.log_err(f"FedEx track result error: {err_msg}")
return {"error": err_msg}
latest = result.get("latestStatusDetail", {})
status_code = latest.get("code", "")
status_desc = latest.get("description", "")
scan_events = result.get("scanEvents", [])
location = ""
if scan_events:
loc = scan_events[0].get("scanLocation", {})
city = loc.get("city", "")
state = loc.get("stateOrProvinceCode", "")
location = f"{city}, {state}".strip(", ")
expected_delivery = (
result.get("estimatedDeliveryTimeWindow", {})
.get("window", {})
.get("ends", "")
or result.get("standardTransitTimeWindow", {})
.get("window", {})
.get("ends", "")
)
return {
"carrier": "fedex",
"tracking_number": tracking_number,
"status": self._normalize_fedex_status(status_code, status_desc),
"status_detail": status_desc,
"location": location,
"timestamp": scan_events[0].get("date", "") if scan_events else "",
"expected_delivery": expected_delivery,
}
except Exception as e:
self.log_err(f"FedEx track exception: {e}")
return {"error": str(e)}
def _normalize_fedex_status(self, code: str, desc: str) -> str:
c, d = code.upper(), desc.upper()
if c == "DL" or "DELIVERED" in d:
return "delivered"
if c == "OD" or "OUT FOR DELIVERY" in d:
return "out_for_delivery"
if c in ("IT", "AR", "DP", "AF", "OC") or "IN TRANSIT" in d \
or "ARRIVED" in d or "DEPARTED" in d:
return "in_transit"
if c == "DE" or "EXCEPTION" in d or "DELAY" in d:
return "exception"
if c == "HL" or "HOLD" in d:
return "held"
if c == "RS" or "RETURN" in d:
return "returned"
if c == "CA" or "CANCEL" in d:
return "cancelled"
if c in ("PU", "PX") or "PICKED UP" in d or "LABEL" in d:
return "label_created"
return "in_transit"
# ─────────────────────────────────────────────────────────────────────────
# UPS API
# ─────────────────────────────────────────────────────────────────────────
def _get_ups_token(self) -> str:
"""Return a cached or freshly-obtained UPS OAuth token."""
now = time.time()
if self._ups_token and now < self._ups_token_expires - 60:
return self._ups_token
try:
credentials = base64.b64encode(
f"{UPS_CLIENT_ID}:{UPS_CLIENT_SECRET}".encode()
).decode()
resp = requests.post(
f"{UPS_BASE_URL}/security/v1/oauth/token",
headers={
"Authorization": f"Basic {credentials}",
"Content-Type": "application/x-www-form-urlencoded",
},
data={"grant_type": "client_credentials"},
timeout=10,
)
if resp.status_code == 200:
data = resp.json()
self._ups_token = data.get("access_token")
self._ups_token_expires = now + int(data.get("expires_in", 14400))
return self._ups_token
self.log_err(f"UPS token error: {resp.status_code} {resp.text[:200]}")
return None
except Exception as e:
self.log_err(f"UPS token exception: {e}")
return None
def _track_ups(self, tracking_number: str) -> dict:
"""Track a UPS package. Returns normalized status dict or {'error': ...}."""
try:
token = self._get_ups_token()
if not token:
return {"error": "UPS authentication failed. Check your client ID and secret."}
resp = requests.get(
f"{UPS_BASE_URL}/api/track/v1/details/{tracking_number}",
headers={
"Authorization": f"Bearer {token}",
"transId": str(uuid.uuid4()),
"transactionSrc": "PackageTracker",
},
params={"locale": "en_US", "returnSignature": "false"},
timeout=15,
)
if resp.status_code != 200:
self.log_err(f"UPS track error: {resp.status_code} {resp.text[:300]}")
return {"error": f"UPS returned HTTP {resp.status_code}"}
data = resp.json()
shipments = data.get("trackResponse", {}).get("shipment", [])
if not shipments:
return {"error": "No UPS shipment found for that tracking number"}
package = shipments[0].get("package", [{}])[0]
activity = package.get("activity", [])
status_type = ""
status_desc = ""
location = ""
timestamp = ""
if activity:
latest = activity[0]
status_obj = latest.get("status", {})
status_type = status_obj.get("type", "")
status_desc = status_obj.get("description", "")
loc = latest.get("location", {}).get("address", {})
city = loc.get("city", "")
state = loc.get("stateProvince", "")
location = f"{city}, {state}".strip(", ")
timestamp = f"{latest.get('date', '')} {latest.get('time', '')}".strip()
# Delivery date: find entry with type "DEL"
delivery_date = ""
for dd in package.get("deliveryDate", []):
if dd.get("type") == "DEL":
delivery_date = dd.get("date", "")
break
if not delivery_date and package.get("deliveryDate"):
delivery_date = package["deliveryDate"][0].get("date", "")
return {
"carrier": "ups",
"tracking_number": tracking_number,
"status": self._normalize_ups_status(status_type, status_desc),
"status_detail": status_desc,
"location": location,
"timestamp": timestamp,
"expected_delivery": delivery_date,
}
except Exception as e:
self.log_err(f"UPS track exception: {e}")
return {"error": str(e)}
def _normalize_ups_status(self, type_: str, desc: str) -> str:
t, d = type_.upper(), desc.upper()
if t == "D" or "DELIVERED" in d:
return "delivered"
if t == "O" or "OUT FOR DELIVERY" in d:
return "out_for_delivery"
if t == "X" or "EXCEPTION" in d or "DELAY" in d:
return "exception"
if t == "M" or "LABEL CREATED" in d:
return "label_created"
if "RETURNED" in d:
return "returned"
return "in_transit" # covers I, P, and any other in-motion type
# ─────────────────────────────────────────────────────────────────────────
# USPS API
# ─────────────────────────────────────────────────────────────────────────
def _track_usps(self, tracking_number: str) -> dict:
"""Track a USPS package via the Web Tools XML API."""
try:
xml_body = (
f'<TrackRequest USERID="{USPS_USER_ID}">'
f'<TrackID ID="{tracking_number}"></TrackID>'
f'</TrackRequest>'
)
resp = requests.get(
f"{USPS_BASE_URL}/ShippingAPI.dll",
params={"API": "TrackV2", "XML": xml_body},
timeout=15,
)
if resp.status_code != 200:
self.log_err(f"USPS track error: {resp.status_code}")
return {"error": f"USPS returned HTTP {resp.status_code}"}
root = ET.fromstring(resp.text)
error_node = root.find(".//Error")
if error_node is not None:
err_desc = error_node.findtext("Description", "Unknown USPS error")
self.log_err(f"USPS error: {err_desc}")
return {"error": err_desc}
summary = root.find(".//TrackSummary")
if summary is None:
return {"error": "No USPS tracking summary in response"}
event_time = summary.findtext("EventTime", "")
event_date = summary.findtext("EventDate", "")
status_desc = summary.findtext("Event", "")
city = summary.findtext("EventCity", "")
state = summary.findtext("EventState", "")
location = f"{city}, {state}".strip(", ")
expected_delivery = summary.findtext("ExpectedDeliveryDate", "")
return {
"carrier": "usps",
"tracking_number": tracking_number,
"status": self._normalize_usps_status(status_desc),
"status_detail": status_desc,
"location": location,
"timestamp": f"{event_date} {event_time}".strip(),
"expected_delivery": expected_delivery,
}
except ET.ParseError as e:
self.log_err(f"USPS XML parse error: {e}")
return {"error": "Invalid XML response from USPS"}
except Exception as e:
self.log_err(f"USPS track exception: {e}")
return {"error": str(e)}
def _normalize_usps_status(self, desc: str) -> str:
d = desc.upper()
if "DELIVERED" in d:
return "delivered"
if "OUT FOR DELIVERY" in d:
return "out_for_delivery"
if "IN TRANSIT" in d or "ARRIVED" in d or "DEPARTED" in d \
or "PROCESSED" in d or "ACCEPTED" in d:
return "in_transit"
if "HELD" in d or "HOLD" in d or "AVAILABLE FOR PICKUP" in d:
return "held"
if "RETURN" in d:
return "returned"
if "ALERT" in d or "NOTICE" in d:
return "exception"
if "LABEL" in d or "ELECTRONIC" in d:
return "label_created"
return "in_transit"
# ─────────────────────────────────────────────────────────────────────────
# DHL API
# ─────────────────────────────────────────────────────────────────────────
def _track_dhl(self, tracking_number: str) -> dict:
"""Track a DHL package via the Unified Tracking API."""
try:
resp = requests.get(
f"{DHL_BASE_URL}/track/shipments",
headers={
"DHL-API-Key": DHL_API_KEY,
"Accept": "application/json",
},
params={"trackingNumber": tracking_number, "language": "en"},
timeout=15,
)
if resp.status_code != 200:
self.log_err(f"DHL track error: {resp.status_code} {resp.text[:300]}")
return {"error": f"DHL returned HTTP {resp.status_code}"}
data = resp.json()
shipments = data.get("shipments", [])
if not shipments:
return {"error": "No DHL shipment found for that tracking number"}
shipment = shipments[0]
events = shipment.get("events", [])
status_obj = shipment.get("status", {})
status_code = status_obj.get("status", "")
status_desc = status_obj.get("description", "")
location = ""
timestamp = ""
if events:
latest = events[0]
loc = latest.get("location", {}).get("address", {})
city = loc.get("addressLocality", "")
country = loc.get("countryCode", "")
location = f"{city}, {country}".strip(", ")
timestamp = latest.get("timestamp", "")
expected_delivery = shipment.get("estimatedTimeOfDelivery", "")
return {
"carrier": "dhl",
"tracking_number": tracking_number,
"status": self._normalize_dhl_status(status_code, status_desc),
"status_detail": status_desc,
"location": location,
"timestamp": timestamp,
"expected_delivery": expected_delivery,
}
except Exception as e:
self.log_err(f"DHL track exception: {e}")
return {"error": str(e)}
def _normalize_dhl_status(self, code: str, desc: str) -> str:
c, d = code.upper(), desc.upper()
if c == "DELIVERED" or "DELIVERED" in d:
return "delivered"
if c in ("OUT-FOR-DELIVERY",) or "OUT FOR DELIVERY" in d:
return "out_for_delivery"
if c in ("TRANSIT", "IN-TRANSIT", "PROCESSED") \
or "TRANSIT" in d or "ARRIVED" in d:
return "in_transit"
if "EXCEPTION" in d or "DELAY" in d or "DAMAGE" in d:
return "exception"
if "RETURN" in d:
return "returned"
if "HELD" in d or "HOLD" in d:
return "held"
if "LABEL" in d:
return "label_created"
return "in_transit"
# ─────────────────────────────────────────────────────────────────────────
# UNIFIED TRACKING ROUTER
# ─────────────────────────────────────────────────────────────────────────
def _track_package(self, carrier: str, tracking_number: str) -> dict:
"""Route to the appropriate carrier API. Returns normalized dict or {'error': ...}."""
if carrier == "ups":
return self._track_ups(tracking_number)
if carrier == "fedex":
return self._track_fedex(tracking_number)
if carrier == "usps":
return self._track_usps(tracking_number)
if carrier == "dhl":
return self._track_dhl(tracking_number)
return {"error": f"Carrier '{carrier}' is not supported"}
# ─────────────────────────────────────────────────────────────────────────
# TRIGGER CONTEXT & INTENT CLASSIFICATION
# ─────────────────────────────────────────────────────────────────────────
def _get_trigger_context(self) -> str:
try:
history = self.worker.agent_memory.full_message_history
recent = history[-5:] if len(history) > 5 else history
parts = []
for msg in recent:
role = msg.get("role", "unknown")
content = msg.get("content", "")
if content:
parts.append(f"{role}: {content}")
return "\n".join(parts)
except Exception:
return ""
def _classify_trigger_intent(self, context: str) -> dict:
user_input = self.initial_request
if not user_input:
try:
history = self.worker.agent_memory.full_message_history
for msg in reversed(history):
if msg.get("role") == "user" and msg.get("content"):
user_input = msg["content"]
break
except Exception:
pass
self.log_info(f"Classifying trigger intent from: '{user_input}'")
lower = (user_input or "").lower()
ADD_KEYWORDS = [
"track a package", "track my package", "track this package",
"track package", "add a tracking", "new tracking",
"track my order", "track an order", "track order", "add tracking",
]
STATUS_ALL_KEYWORDS = [
"any packages", "package update", "package status",
"what's shipping", "check my packages", "delivery update",
"check on my package", "check my order",
]
LIST_KEYWORDS = [
"how many packages", "list my packages", "list packages",
"what am i tracking",
]
REMOVE_KEYWORDS = [
"stop tracking", "remove package", "delete tracking", "remove tracking",
]
for phrase in ADD_KEYWORDS:
if phrase in lower:
return {"mode": "add", "nickname": None}
for phrase in LIST_KEYWORDS:
if phrase in lower:
return {"mode": "list", "nickname": None}
for phrase in REMOVE_KEYWORDS:
if phrase in lower:
return {"mode": "remove", "nickname": None}
for phrase in STATUS_ALL_KEYWORDS:
if phrase in lower:
return {"mode": "status_all", "nickname": None}
m = re.search(r"where(?:'s| is) (?:my |the )?(.+)", lower)
if m:
return {"mode": "status_one", "nickname": m.group(1).strip()}
prompt = self._build_prompt(
INTENT_CLASSIFICATION_PROMPT,
user_input=user_input or "",
context=context,
)
raw = self.capability_worker.text_to_text_response(prompt)
clean = raw.replace("```json", "").replace("```", "").strip()
try:
result = json.loads(clean)
if isinstance(result, dict) and "mode" in result:
return result
except json.JSONDecodeError:
self.log_err(f"Failed to parse intent JSON: {clean[:200]}")
return {"mode": "status_all", "nickname": None}
# ─────────────────────────────────────────────────────────────────────────
# PERSISTENCE
# ─────────────────────────────────────────────────────────────────────────
async def _load_packages(self):
try:
exists = await self.capability_worker.check_if_file_exists(PACKAGES_FILE, False)
if exists:
raw = await self.capability_worker.read_file(PACKAGES_FILE, False)
data = json.loads(raw)
self.packages = data.get("packages", [])
else:
self.packages = []
except Exception as e:
self.log_err(f"Error loading packages: {e}")
self.packages = []
async def _save_packages(self):
try:
data = {"packages": self.packages}
await self.capability_worker.delete_file(PACKAGES_FILE, False)
await self.capability_worker.write_file(
PACKAGES_FILE, json.dumps(data, indent=2), False
)
except Exception as e:
self.log_err(f"Error saving packages: {e}")
# ─────────────────────────────────────────────────────────────────────────
# AUTO-CLEANUP DELIVERED PACKAGES
# ─────────────────────────────────────────────────────────────────────────
async def _cleanup_delivered(self):
now = datetime.now(timezone.utc)
cleaned = []
removed_count = 0
for pkg in self.packages:
if pkg.get("delivered_date"):
try:
delivered_dt = datetime.fromisoformat(pkg["delivered_date"])
if delivered_dt.tzinfo is None:
delivered_dt = delivered_dt.replace(tzinfo=timezone.utc)
if now - delivered_dt > timedelta(days=DELIVERED_CLEANUP_DAYS):
removed_count += 1
continue
except Exception:
pass
cleaned.append(pkg)
if removed_count > 0:
self.packages = cleaned
await self._save_packages()
self.log_info(f"Auto-cleaned {removed_count} delivered packages")
# ─────────────────────────────────────────────────────────────────────────
# HANDLE: ADD TRACKING
# ─────────────────────────────────────────────────────────────────────────
async def _handle_add(self):
active_count = len([p for p in self.packages if p.get("last_status") != "delivered"])
if active_count >= MAX_PACKAGES:
await self.capability_worker.speak(
f"You're already tracking {MAX_PACKAGES} packages. "
"Remove one before adding another."
)
return
while True:
tracking_number = await self._capture_tracking_number()
if not tracking_number:
return
carrier = None
# Step 1: auto-detect carrier from tracking number format
configured_carriers = [c for c in ("fedex", "ups", "usps", "dhl") if self._carrier_configured(c)]
configured_display = " and ".join(CARRIER_DISPLAY[c] for c in configured_carriers)
detected = self._detect_carrier_from_number(tracking_number)
self.log_info(f"Auto-detected carrier: '{detected}' for {tracking_number}")
if detected == "amazon":
await self.capability_worker.speak(
"That looks like an Amazon Logistics number. "
"Amazon doesn't have a public tracking API, so I can't check it here."
)
confirmed = await self.capability_worker.run_confirmation_loop(
"Want to track a different package instead?"
)
if confirmed:
continue
return
elif detected != "unknown" and detected not in configured_carriers:
# Detected a carrier we don't have credentials for — fail fast
display = CARRIER_DISPLAY.get(detected, detected.upper())
await self.capability_worker.speak(
f"That looks like a {display} number, but I'm not set up for {display} yet. "
f"I can currently track {configured_display}."
)
confirmed = await self.capability_worker.run_confirmation_loop(
"Want to try a different tracking number?"
)
if confirmed:
continue
return
elif detected != "unknown":
# Detected + configured — ask user to confirm
display = CARRIER_DISPLAY.get(detected, detected.upper())
confirmed = await self.capability_worker.run_confirmation_loop(
f"This looks like a {display} tracking number. Is that right?"
)
if confirmed:
carrier = detected
if not carrier:
# Unknown carrier or user declined detection
if len(configured_carriers) == 1:
# Only one carrier set up — offer it directly instead of asking
single_display = CARRIER_DISPLAY[configured_carriers[0]]
confirmed = await self.capability_worker.run_confirmation_loop(
f"I couldn't identify the carrier. "
f"The only one I'm set up for is {single_display}. "
f"Should I try tracking with {single_display}?"
)
if not confirmed:
return
carrier = configured_carriers[0]
else:
# Multiple carriers configured — ask the user, listing only what's available
response = await self.capability_worker.run_io_loop(
f"Which carrier is this? I can track {configured_display}."
)
if not response or self._is_cancel(response):
return
carrier = self._parse_carrier_from_text(response)
if not carrier or not self._carrier_configured(carrier):
await self.capability_worker.speak(
f"I can currently track {configured_display}. "
"Please say one of those carrier names."
)
confirmed = await self.capability_worker.run_confirmation_loop(
"Want to try again with a different tracking number?"
)
if confirmed:
continue
return
# Step 2: get nickname
nickname = await self._capture_nickname()
if not nickname:
nickname = "My package"
# Step 3: call carrier API to verify the number and get initial status
display = CARRIER_DISPLAY.get(carrier, carrier.upper())
await self.capability_worker.speak(f"Checking with {display} now.")
result = await asyncio.to_thread(self._track_package, carrier, tracking_number)
if "error" in result:
self.log_err(f"Track on add failed: {result['error']}")
await self.capability_worker.speak(
f"Sorry, {display} couldn't verify that tracking number. "
f"{result['error']}."
)
confirmed = await self.capability_worker.run_confirmation_loop(
"Want to try a different tracking number?"
)
if confirmed:
continue
return
# Step 4: save and confirm
now_iso = datetime.now(timezone.utc).isoformat()
pkg_id = f"pkg_{datetime.now(timezone.utc).strftime('%Y%m%d%H%M%S%f')[:20]}"
new_package = {
"id": pkg_id,
"tracking_number": tracking_number,
"carrier": carrier,
"carrier_display": display,
"nickname": nickname,
"last_status": result.get("status", "pending"),
"last_status_detail": result.get("status_detail", ""),
"location": result.get("location", ""),
"expected_delivery": result.get("expected_delivery", ""),
"last_checked": now_iso,
"added_date": now_iso,
"delivered_date": now_iso if result.get("status") == "delivered" else None,
}
self.packages.append(new_package)
await self._save_packages()
status_spoken = STATUS_DESCRIPTIONS.get(result.get("status", "pending"), "Status pending.")
delivery_info = ""
if result.get("expected_delivery"):
delivery_info = f" Expected delivery: {self._format_date_for_speech(result['expected_delivery'])}."
await self.capability_worker.speak(
f"Got it. I'm now tracking your {nickname} via {display}. "
f"{status_spoken}{delivery_info}"
)
confirmed = await self.capability_worker.run_confirmation_loop(
"Want to track another package?"
)
if not confirmed:
return
async def _capture_tracking_number(self) -> str:
ask_prompts = [
"Please say your tracking number now, or type it in the chat.",
"I didn't catch that. Please say the tracking number again.",
"One more try — say each digit clearly, or type the number.",
]
for attempt in range(3):
user_input = await self.capability_worker.run_io_loop(ask_prompts[attempt])
if not user_input:
if attempt == 2: