I would just advise that whichever solution you use, try not to use any volatile functions if possible, which includes OFFSET. For the solution provided by @rockinfreakshow, I think this would work:
=scan(,sequence(rows(B2:B)),lambda(a,i,
let(c,index(B:B,i+1),
if(c="",,
if(index(A:A,row(c))<>index(A:A,row(c)-1),c,
if(c=index(B:B,i)+1,
regexreplace(a&"","(\d{4})(-\d{4})?$","$1-"&c&""),a&", "&c))))))
And similarly with the solution provided by @z..:
=SCAN(,
SEQUENCE(ROWS(B2:B)),
LAMBDA(prev, i,
LET(cur, INDEX(B:B, i+1),
IF(cur = "", , "" &
IF(
INDEX(A:A, i+1) <> INDEX(A:A, i),
cur,
IF(
INDEX(B:B, i) + 1 <> cur,
prev & ", " & cur,
REGEXEXTRACT(
prev,
IF(REGEXMATCH(prev, ","), ".*, \d+", "\d+")
) & "-" & cur
)
)
)
)
)
)