···11/**
22 * TID (Timestamp Identifier) generation for ATProto
33 * Based on: https://atproto.com/specs/record-key#record-key-type-tid
44+ *
55+ * This uses the official ATProto TID implementation from @atproto/common-web
66+ * to ensure compatibility and avoid precision issues with large numbers.
47 */
5866-const B32_CHARS = '234567abcdefghijklmnopqrstuvwxyz';
99+import { TID } from '@atproto/common-web';
710811/**
912 * Generate a TID from a Date object
1010- * TID encodes Unix microseconds in base32
1313+ * Uses the official ATProto TID.fromTime method
1114 */
1215export function generateTID(date: Date): string {
1316 // Convert to Unix microseconds
1414- // JS only gives us millisecond precision, so we multiply by 1000
1515- const unixMicros = Math.floor(date.getTime() * 1000);
1717+ // The ATProto implementation expects microseconds
1818+ const unixMicros = date.getTime() * 1000;
16191717- let tid = '';
1818- for (let i = 0; i < 13; i++) {
1919- // Extract 5 bits at a time (base32)
2020- const shift = 60 - (i * 5);
2121- const index = Math.floor(unixMicros / Math.pow(2, shift)) % 32;
2222- tid += B32_CHARS[index];
2323- }
2020+ // Use a fixed clockid of 0 for deterministic TID generation from timestamps
2121+ // This ensures the same playedTime always generates the same TID
2222+ const clockid = 0;
24232525- return tid;
2424+ return TID.fromTime(unixMicros, clockid).toString();
2625}
27262827/**