@@ -1027,6 +1027,18 @@ mod.aiChatCompletions = function(messages, temperatureOrCb, cb)
10271027 local headers = {}
10281028 headers [" Content-Type" ] = " application/json"
10291029
1030+ -- AI can insert annotations which can be empty arrays.
1031+ -- The JSON encoder encodes those empty arrays as `{}` instead of `[]`
1032+ -- and that is not valid for the API.
1033+ -- Ommiting them is ok though so setting them to nil fixes the issue.
1034+ for _ , m in messages do
1035+ if m .annotations then
1036+ if # m .annotations == 0 then
1037+ m .annotations = nil
1038+ end
1039+ end
1040+ end
1041+
10301042 local body = {}
10311043 body .model = " gpt-4o-mini"
10321044 body .messages = messages
@@ -1089,6 +1101,22 @@ end
10891101
10901102-- Badges
10911103
1104+ local function badgeFixFields (badge )
1105+ if badge .userDidUnlock ~= nil then
1106+ badge .unlocked = badge .userDidUnlock
1107+ badge .userDidUnlock = nil
1108+ else
1109+ badge .unlocked = false
1110+ end
1111+ if badge .userUnlockedAt ~= nil then
1112+ badge .unlockedAt = badge .userUnlockedAt
1113+ badge .userUnlockedAt = nil
1114+ end
1115+ if badge .rarity == nil then
1116+ badge .rarity = 0
1117+ end
1118+ end
1119+
10921120-- Callback signature: cb(error, badges)
10931121mod .listBadgesForWorld = function (_ , worldId , cb )
10941122 local url = mod .kApiAddr .. " /worlds/" .. worldId .. " /badges"
@@ -1097,6 +1125,24 @@ mod.listBadgesForWorld = function(_, worldId, cb)
10971125 return cb (mod :error (res .StatusCode , " status code: " .. res .StatusCode ))
10981126 end
10991127 local badges = JSON :Decode (res .Body )
1128+ for _ , badge in badges do
1129+ badgeFixFields (badge )
1130+ end
1131+ cb (nil , badges )
1132+ end )
1133+ return req
1134+ end
1135+
1136+ mod .listBadgesForUser = function (_ , userID , cb )
1137+ local url = mod .kApiAddr .. " /users/" .. userID .. " /badges"
1138+ local req = HTTP :Get (url , function (res )
1139+ if res .StatusCode ~= 200 then
1140+ return cb (mod :error (res .StatusCode , " status code: " .. res .StatusCode ))
1141+ end
1142+ local badges = JSON :Decode (res .Body )
1143+ for _ , badge in badges do
1144+ badgeFixFields (badge )
1145+ end
11001146 cb (nil , badges )
11011147 end )
11021148 return req
0 commit comments