@devries-cameron20 / composio Mirror · read-only / commits / 07d9fe577
Fork 0

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

Mirrored from https://github.com/Camo67/composio.git

Synced Nov 22, 10:45 UTC

feat(ts/core): address cursorbot

jkomyno committed Feb 6, 2026 at 01:08 UTC 07d9fe57766577573e8862ded03dc627f91247ce
2 files changed +80 -40
python/composio/core/models/triggers.py
+68 -29
@@ -1074,18 +1074,11 @@ class Triggers(Resource):
1074 f"Failed to parse webhook payload as JSON: {e}"
1075 ) from e
1076
1077 - # Try V3 first (has 'type' starting with 'composio.' and 'metadata')
1078 - # Validate metadata is a dict with all required fields (matching TypeScript's zod schema)
1077 + # Try V3 first (has 'type' starting with 'composio.' and 'metadata' as dict)
1078 + # Metadata shape varies by event type (trigger vs connection events),
1079 + # so we only check that it's a dict here.
1080 v3_metadata = data.get("metadata") if isinstance(data, dict) else None
1080 - v3_metadata_valid = (
1081 - isinstance(v3_metadata, dict)
1082 - and "log_id" in v3_metadata
1083 - and "trigger_slug" in v3_metadata
1084 - and "trigger_id" in v3_metadata
1085 - and "connected_account_id" in v3_metadata
1086 - and "auth_config_id" in v3_metadata
1087 - and "user_id" in v3_metadata
1088 - )
1081 + v3_metadata_valid = isinstance(v3_metadata, dict)
1082 v3_type = data.get("type", "") if isinstance(data, dict) else ""
1083 if (
1084 isinstance(data, dict)
@@ -1214,36 +1207,82 @@ class Triggers(Resource):
1207 def _normalize_v3_payload(self, data: WebhookPayloadV3) -> TriggerEvent:
1208 """Normalize V3 payload to TriggerEvent format."""
1209 metadata = data["metadata"]
1217 - return t.cast(
1218 - TriggerEvent,
1219 - {
1220 - "id": metadata["trigger_id"],
1221 - "uuid": metadata["trigger_id"],
1222 - "user_id": metadata["user_id"],
1223 - "toolkit_slug": metadata["trigger_slug"].split("_")[0].upper()
1224 - if "_" in metadata["trigger_slug"]
1225 - else "UNKNOWN",
1226 - "trigger_slug": metadata["trigger_slug"],
1227 - "metadata": {
1210 +
1211 + # Check if this is a trigger event (has trigger-specific metadata fields)
1212 + is_trigger_event = all(
1213 + k in metadata
1214 + for k in (
1215 + "trigger_id",
1216 + "trigger_slug",
1217 + "user_id",
1218 + "connected_account_id",
1219 + "auth_config_id",
1220 + "log_id",
1221 + )
1222 + )
1223 +
1224 + if is_trigger_event:
1225 + return t.cast(
1226 + TriggerEvent,
1227 + {
1228 "id": metadata["trigger_id"],
1229 "uuid": metadata["trigger_id"],
1230 + "user_id": metadata["user_id"],
1231 "toolkit_slug": metadata["trigger_slug"].split("_")[0].upper()
1232 if "_" in metadata["trigger_slug"]
1233 else "UNKNOWN",
1234 "trigger_slug": metadata["trigger_slug"],
1235 + "metadata": {
1236 + "id": metadata["trigger_id"],
1237 + "uuid": metadata["trigger_id"],
1238 + "toolkit_slug": metadata["trigger_slug"].split("_")[0].upper()
1239 + if "_" in metadata["trigger_slug"]
1240 + else "UNKNOWN",
1241 + "trigger_slug": metadata["trigger_slug"],
1242 + "trigger_data": None,
1243 + "trigger_config": {},
1244 + "connected_account": {
1245 + "id": metadata["connected_account_id"],
1246 + "uuid": metadata["connected_account_id"],
1247 + "auth_config_id": metadata["auth_config_id"],
1248 + "auth_config_uuid": metadata["auth_config_id"],
1249 + "user_id": metadata["user_id"],
1250 + "status": "ACTIVE",
1251 + },
1252 + },
1253 + "payload": data["data"],
1254 + "original_payload": None,
1255 + },
1256 + )
1257 +
1258 + # Non-trigger V3 event (e.g., connection expired)
1259 + event_type = data.get("type", "")
1260 + return t.cast(
1261 + TriggerEvent,
1262 + {
1263 + "id": data.get("id", ""),
1264 + "uuid": data.get("id", ""),
1265 + "user_id": "",
1266 + "toolkit_slug": "COMPOSIO",
1267 + "trigger_slug": event_type,
1268 + "metadata": {
1269 + "id": data.get("id", ""),
1270 + "uuid": data.get("id", ""),
1271 + "toolkit_slug": "COMPOSIO",
1272 + "trigger_slug": event_type,
1273 "trigger_data": None,
1274 "trigger_config": {},
1275 "connected_account": {
1237 - "id": metadata["connected_account_id"],
1238 - "uuid": metadata["connected_account_id"],
1239 - "auth_config_id": metadata["auth_config_id"],
1240 - "auth_config_uuid": metadata["auth_config_id"],
1241 - "user_id": metadata["user_id"],
1276 + "id": "",
1277 + "uuid": "",
1278 + "auth_config_id": "",
1279 + "auth_config_uuid": "",
1280 + "user_id": "",
1281 "status": "ACTIVE",
1282 },
1283 },
1245 - "payload": data["data"],
1246 - "original_payload": None,
1284 + "payload": data.get("data", {}),
1285 + "original_payload": data,
1286 },
1287 )
1288
python/tests/test_triggers.py
+12 -11
@@ -580,23 +580,24 @@ class TestVerifyWebhook:
580 ):
581 """Test V3 payload with non-trigger event type is detected as V3, not V2.
582
583 - This is a regression test for PLEN-1397: V3 payloads with event types
584 - other than 'composio.trigger.message' should still be detected as V3.
583 + Uses realistic connection metadata (project_id, org_id) instead of
584 + fabricated trigger metadata, verifying V3 detection works for events
585 + with different metadata shapes.
586 """
586 - # V3 payload with a different event type
587 payload_dict = {
588 "id": "msg_abc123",
589 "timestamp": "2024-01-01T00:00:00Z",
590 - "type": "composio.connected_account.expired", # Not 'composio.trigger.message'
590 + "type": "composio.connected_account.expired",
591 "metadata": {
592 - "log_id": "log-123",
593 - "trigger_slug": "",
594 - "trigger_id": "",
595 - "connected_account_id": "conn-123",
596 - "auth_config_id": "auth-123",
597 - "user_id": "user-456",
592 + "project_id": "pr_koucdrMIwRsf",
593 + "org_id": "4a4ded8f-d3ae-4dea-a229-c30234298b05",
594 + },
595 + "data": {
596 + "toolkit": {"slug": "gmail"},
597 + "id": "ca__IvSeEzEBjVt",
598 + "user_id": "test-user",
599 + "status": "EXPIRED",
600 },
599 - "data": {"reason": "token_expired"},
601 }
602 payload = json.dumps(payload_dict)
603 signature = self.create_signature(