|
| 1 | +import type { SessionState, WithParts } from "./state" |
| 2 | + |
| 3 | +const MESSAGE_REF_REGEX = /^m(\d{4})$/ |
| 4 | +const BLOCK_REF_REGEX = /^b([1-9]\d*)$/ |
| 5 | +const MESSAGE_ID_TAG_NAME = "dcp-message-id" |
| 6 | + |
| 7 | +const MESSAGE_REF_WIDTH = 4 |
| 8 | +const MESSAGE_REF_MIN_INDEX = 0 |
| 9 | +export const MESSAGE_REF_MAX_INDEX = 9999 |
| 10 | + |
| 11 | +export type ParsedBoundaryId = |
| 12 | + | { |
| 13 | + kind: "message" |
| 14 | + ref: string |
| 15 | + index: number |
| 16 | + } |
| 17 | + | { |
| 18 | + kind: "compressed-block" |
| 19 | + ref: string |
| 20 | + blockId: number |
| 21 | + } |
| 22 | + |
| 23 | +export function formatMessageRef(index: number): string { |
| 24 | + if ( |
| 25 | + !Number.isInteger(index) || |
| 26 | + index < MESSAGE_REF_MIN_INDEX || |
| 27 | + index > MESSAGE_REF_MAX_INDEX |
| 28 | + ) { |
| 29 | + throw new Error( |
| 30 | + `Message ID index out of bounds: ${index}. Supported range is 0-${MESSAGE_REF_MAX_INDEX}.`, |
| 31 | + ) |
| 32 | + } |
| 33 | + return `m${index.toString().padStart(MESSAGE_REF_WIDTH, "0")}` |
| 34 | +} |
| 35 | + |
| 36 | +export function formatBlockRef(blockId: number): string { |
| 37 | + if (!Number.isInteger(blockId) || blockId < 1) { |
| 38 | + throw new Error(`Invalid block ID: ${blockId}`) |
| 39 | + } |
| 40 | + return `b${blockId}` |
| 41 | +} |
| 42 | + |
| 43 | +export function parseMessageRef(ref: string): number | null { |
| 44 | + const normalized = ref.trim().toLowerCase() |
| 45 | + const match = normalized.match(MESSAGE_REF_REGEX) |
| 46 | + if (!match) { |
| 47 | + return null |
| 48 | + } |
| 49 | + const index = Number.parseInt(match[1], 10) |
| 50 | + return Number.isInteger(index) ? index : null |
| 51 | +} |
| 52 | + |
| 53 | +export function parseBlockRef(ref: string): number | null { |
| 54 | + const normalized = ref.trim().toLowerCase() |
| 55 | + const match = normalized.match(BLOCK_REF_REGEX) |
| 56 | + if (!match) { |
| 57 | + return null |
| 58 | + } |
| 59 | + const id = Number.parseInt(match[1], 10) |
| 60 | + return Number.isInteger(id) ? id : null |
| 61 | +} |
| 62 | + |
| 63 | +export function parseBoundaryId(id: string): ParsedBoundaryId | null { |
| 64 | + const normalized = id.trim().toLowerCase() |
| 65 | + const messageIndex = parseMessageRef(normalized) |
| 66 | + if (messageIndex !== null) { |
| 67 | + return { |
| 68 | + kind: "message", |
| 69 | + ref: formatMessageRef(messageIndex), |
| 70 | + index: messageIndex, |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + const blockId = parseBlockRef(normalized) |
| 75 | + if (blockId !== null) { |
| 76 | + return { |
| 77 | + kind: "compressed-block", |
| 78 | + ref: formatBlockRef(blockId), |
| 79 | + blockId, |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + return null |
| 84 | +} |
| 85 | + |
| 86 | +export function formatMessageIdTag(ref: string): string { |
| 87 | + return `<${MESSAGE_ID_TAG_NAME}>${ref}</${MESSAGE_ID_TAG_NAME}>` |
| 88 | +} |
| 89 | + |
| 90 | +export function assignMessageRefs(state: SessionState, messages: WithParts[]): number { |
| 91 | + let assigned = 0 |
| 92 | + |
| 93 | + for (const message of messages) { |
| 94 | + const rawMessageId = message.info.id |
| 95 | + if (typeof rawMessageId !== "string" || rawMessageId.length === 0) { |
| 96 | + continue |
| 97 | + } |
| 98 | + |
| 99 | + const existingRef = state.messageIds.byRawId.get(rawMessageId) |
| 100 | + if (existingRef) { |
| 101 | + if (state.messageIds.byRef.get(existingRef) !== rawMessageId) { |
| 102 | + state.messageIds.byRef.set(existingRef, rawMessageId) |
| 103 | + } |
| 104 | + continue |
| 105 | + } |
| 106 | + |
| 107 | + const ref = allocateNextMessageRef(state) |
| 108 | + state.messageIds.byRawId.set(rawMessageId, ref) |
| 109 | + state.messageIds.byRef.set(ref, rawMessageId) |
| 110 | + assigned++ |
| 111 | + } |
| 112 | + |
| 113 | + return assigned |
| 114 | +} |
| 115 | + |
| 116 | +function allocateNextMessageRef(state: SessionState): string { |
| 117 | + let candidate = Number.isInteger(state.messageIds.nextRef) |
| 118 | + ? Math.max(MESSAGE_REF_MIN_INDEX, state.messageIds.nextRef) |
| 119 | + : MESSAGE_REF_MIN_INDEX |
| 120 | + |
| 121 | + while (candidate <= MESSAGE_REF_MAX_INDEX) { |
| 122 | + const ref = formatMessageRef(candidate) |
| 123 | + if (!state.messageIds.byRef.has(ref)) { |
| 124 | + state.messageIds.nextRef = candidate + 1 |
| 125 | + return ref |
| 126 | + } |
| 127 | + candidate++ |
| 128 | + } |
| 129 | + |
| 130 | + throw new Error( |
| 131 | + `Message ID alias capacity exceeded. Cannot allocate more than ${formatMessageRef(MESSAGE_REF_MAX_INDEX)} aliases in this session.`, |
| 132 | + ) |
| 133 | +} |
0 commit comments