(id: string, maxLength: number = OPENAI_CALL_ID_MAX_LENGTH)
| 23 | * @returns The truncated ID, or original if already within limits |
| 24 | */ |
| 25 | export function truncateOpenAiCallId(id: string, maxLength: number = OPENAI_CALL_ID_MAX_LENGTH): string { |
| 26 | if (id.length <= maxLength) { |
| 27 | return id |
| 28 | } |
| 29 | |
| 30 | // Use 8-char hash suffix for uniqueness (from MD5, sufficient for collision resistance in this context) |
| 31 | const hashSuffixLength = 8 |
| 32 | const separator = "_" |
| 33 | // Reserve space for separator + hash |
| 34 | const prefixMaxLength = maxLength - separator.length - hashSuffixLength |
| 35 | |
| 36 | // Create hash of the full original ID for uniqueness |
| 37 | const hash = crypto.createHash("md5").update(id).digest("hex").slice(0, hashSuffixLength) |
| 38 | |
| 39 | // Take the prefix and append hash |
| 40 | const prefix = id.slice(0, prefixMaxLength) |
| 41 | return `${prefix}${separator}${hash}` |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Sanitize and truncate a tool call ID for OpenAI's Responses API. |
no test coverage detected