MCPcopy Create free account
hub / github.com/Zoo-Code-Org/Zoo-Code / truncateOpenAiCallId

Function truncateOpenAiCallId

src/utils/tool-id.ts:25–42  ·  view source on GitHub ↗
(id: string, maxLength: number = OPENAI_CALL_ID_MAX_LENGTH)

Source from the content-addressed store, hash-verified

23 * @returns The truncated ID, or original if already within limits
24 */
25export 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.

Callers 2

tool-id.spec.tsFile · 0.90
sanitizeOpenAiCallIdFunction · 0.85

Calls 1

updateMethod · 0.65

Tested by

no test coverage detected