|
| 1 | +""" |
| 2 | +Serializers for converting internal badge session data to Open Badges 3.0 format. |
| 3 | +
|
| 4 | +The internal badge entry stored in ``AIWorkflowSession.metadata['badges']`` |
| 5 | +uses a mix of snake_case keys and a structure tailored to the LLM pipeline. |
| 6 | +Before the ``BADGE_GENERATION`` event is emitted, the payload is normalised to |
| 7 | +a valid **OpenBadgeCredential** (OB 3.0 / W3C VC) document so that consumers |
| 8 | +can process a standards-compliant structure without any knowledge of the |
| 9 | +internal representation. |
| 10 | +
|
| 11 | +Reference spec: |
| 12 | + https://1edtech.github.io/openbadges-specification/ob_v3p0.html |
| 13 | +""" |
| 14 | +import uuid as _uuid |
| 15 | + |
| 16 | +# OB 3.0 JSON-LD contexts |
| 17 | +_OB3_CONTEXTS = [ |
| 18 | + "https://www.w3.org/ns/credentials/v2", |
| 19 | + "https://purl.imsglobal.org/spec/ob/v3p0/context-3.0.3.json", |
| 20 | +] |
| 21 | + |
| 22 | + |
| 23 | +def to_open_badge_credential(badge_info: dict) -> dict: |
| 24 | + """ |
| 25 | + Serialize a session badge entry to an Open Badges 3.0 ``OpenBadgeCredential``. |
| 26 | +
|
| 27 | + Only the fields that map cleanly onto the OB 3.0 schema are included. |
| 28 | + Internal fields such as ``course_context``, ``badge_configuration``, or |
| 29 | + ``enable_skill_extraction`` are intentionally omitted from the event |
| 30 | + payload. |
| 31 | +
|
| 32 | + Args: |
| 33 | + badge_info (dict): Full session badge entry as stored in |
| 34 | + ``AIWorkflowSession.metadata['badges']``. Expected keys:: |
| 35 | +
|
| 36 | + { |
| 37 | + "id": str, # internal UUID |
| 38 | + "status": str, |
| 39 | + "created_at": str, # ISO-8601 |
| 40 | + "generated_response": { |
| 41 | + "credentialSubject" | "credential_subject": { |
| 42 | + "achievement": { |
| 43 | + "name": str, |
| 44 | + "description": str, |
| 45 | + "criteria": {"narrative": str} |
| 46 | + } |
| 47 | + }, |
| 48 | + "skills": [ # optional |
| 49 | + { |
| 50 | + "type": str, |
| 51 | + "targetName": str, |
| 52 | + "targetType": str, |
| 53 | + "targetUrl": str, |
| 54 | + "targetDescription": str # optional |
| 55 | + } |
| 56 | + ] |
| 57 | + }, |
| 58 | + "image": { |
| 59 | + "id": str, # URL of the badge image |
| 60 | + } |
| 61 | + } |
| 62 | +
|
| 63 | + Returns: |
| 64 | + dict: An ``OpenBadgeCredential`` document:: |
| 65 | +
|
| 66 | + { |
| 67 | + "@context": [...], |
| 68 | + "id": "urn:uuid:<badge_id>", |
| 69 | + "type": ["VerifiableCredential", "OpenBadgeCredential"], |
| 70 | + "name": "<achievement name>", |
| 71 | + "image": {...}, # present only when a badge image exists |
| 72 | + "validFrom": "<ISO-8601 timestamp>", |
| 73 | + "issuer": {...}, |
| 74 | + "credentialSubject": { |
| 75 | + "type": ["AchievementSubject"], |
| 76 | + "achievement": { |
| 77 | + "id": "urn:uuid:<achievement_id>", |
| 78 | + "type": ["Achievement"], |
| 79 | + "name": "<name>", |
| 80 | + "description": "<description>", |
| 81 | + "criteria": {"narrative": "<narrative>"}, |
| 82 | + "alignment": [...], # present only when skills exist |
| 83 | + "image": {...} |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + """ |
| 88 | + generated = badge_info.get('generated_response') or {} |
| 89 | + |
| 90 | + subject_data = ( |
| 91 | + generated.get('credentialSubject') |
| 92 | + or generated.get('credential_subject') |
| 93 | + or {} |
| 94 | + ) |
| 95 | + achievement_data = subject_data.get('achievement', {}) |
| 96 | + criteria_data = achievement_data.get('criteria', {}) |
| 97 | + |
| 98 | + badge_id = badge_info.get('id') or str(_uuid.uuid4()) |
| 99 | + valid_from = badge_info.get('created_at', '') |
| 100 | + achievement_name = achievement_data.get('name', '') |
| 101 | + |
| 102 | + # Build Achievement node |
| 103 | + achievement: dict = { |
| 104 | + "id": f"urn:uuid:{str(_uuid.uuid4())}", |
| 105 | + "type": ["Achievement"], |
| 106 | + "name": achievement_name, |
| 107 | + "description": achievement_data.get('description', ''), |
| 108 | + "criteria": { |
| 109 | + "narrative": criteria_data.get('narrative', ''), |
| 110 | + }, |
| 111 | + } |
| 112 | + |
| 113 | + skills = generated.get('skills') or [] |
| 114 | + if skills: |
| 115 | + alignment = [] |
| 116 | + for skill in skills: |
| 117 | + entry: dict = { |
| 118 | + "type": ["Alignment"], |
| 119 | + "targetName": skill.get('target_name', ''), |
| 120 | + "targetUrl": skill.get('target_url', ''), |
| 121 | + "targetType": skill.get('target_type', ''), |
| 122 | + } |
| 123 | + target_description = skill.get('target_description') |
| 124 | + if target_description: |
| 125 | + entry["targetDescription"] = target_description |
| 126 | + alignment.append(entry) |
| 127 | + achievement["alignment"] = alignment |
| 128 | + |
| 129 | + badge_image = badge_info.get('image') or {} |
| 130 | + achievement["image"] = { |
| 131 | + "type": "Image", |
| 132 | + "id": badge_image.get('id', ''), |
| 133 | + "caption": achievement_name, |
| 134 | + } |
| 135 | + |
| 136 | + issuer = { |
| 137 | + "id": "urn:uuid:issuer-" + str(_uuid.uuid4()), |
| 138 | + "type": "Profile", |
| 139 | + "name": badge_info.get('organization', 'Unknown Organization'), |
| 140 | + } |
| 141 | + |
| 142 | + credential: dict = { |
| 143 | + "@context": _OB3_CONTEXTS, |
| 144 | + "id": f"urn:uuid:{badge_id}", |
| 145 | + "type": ["VerifiableCredential", "OpenBadgeCredential"], |
| 146 | + "name": achievement_name, |
| 147 | + "image": achievement.get("image"), |
| 148 | + "credentialSubject": { |
| 149 | + "id": f"urn:uuid:{str(_uuid.uuid4())}", |
| 150 | + "type": ["AchievementSubject"], |
| 151 | + "achievement": achievement, |
| 152 | + }, |
| 153 | + "issuer": issuer, |
| 154 | + } |
| 155 | + |
| 156 | + if valid_from: |
| 157 | + credential["validFrom"] = valid_from |
| 158 | + |
| 159 | + return credential |
0 commit comments