| 1 | // Older servers presented a steer as a derived notice without messageId. |
| 2 | // Recover its owner from the explicit record identity so joining a snapshot |
| 3 | // with canonical history cannot produce another row for the same insertion. |
| 4 | export function historyMessageIdentity(message: { messageId?: string; recordId?: string; role: string; content: string; code?: string }, outerRecordId?: string): string | undefined { |
| 5 | if (message.messageId) return message.messageId; |
| 6 | if (message.role !== "notice" || (message.code !== "unapplied_steer" && !message.content.startsWith("↪ "))) return undefined; |
| 7 | return /^m:(.+):notice:0$/.exec(message.recordId || outerRecordId || "")?.[1]; |
| 8 | } |
| 9 | |
| 10 | export function createUniqueItemIDAllocator(): (preferred: string, fallback: string) => string { |
| 11 | const used = new Set<string>(); |
| 12 | return (preferred, fallback) => { |
| 13 | const base = preferred || fallback; |
| 14 | if (!used.has(base)) { |
| 15 | used.add(base); |
| 16 | return base; |
| 17 | } |
| 18 | let suffix = 1; |
| 19 | while (used.has(`${base}#${suffix}`)) suffix += 1; |
| 20 | const id = `${base}#${suffix}`; |
| 21 | used.add(id); |
| 22 | return id; |
| 23 | }; |
| 24 | } |
| 25 |