|
| 1 | +import type { Message, ToolCall } from '#/message'; |
| 2 | + |
| 3 | +export interface ToolCallIdPolicy { |
| 4 | + normalize: (id: string) => string; |
| 5 | + maxLength?: number; |
| 6 | +} |
| 7 | + |
| 8 | +const EMPTY_TOOL_CALL_ID = 'tool_call'; |
| 9 | +const TOOL_CALL_ID_SAFE_CHARS = /[^a-zA-Z0-9_-]/g; |
| 10 | + |
| 11 | +export function sanitizeToolCallId(id: string, maxLength?: number): string { |
| 12 | + const sanitized = id.replace(TOOL_CALL_ID_SAFE_CHARS, '_'); |
| 13 | + return maxLength === undefined ? sanitized : sanitized.slice(0, maxLength); |
| 14 | +} |
| 15 | + |
| 16 | +export function sanitizeOpenAIResponsesCallId(id: string, maxLength?: number): string { |
| 17 | + const [callId] = id.split('|', 1); |
| 18 | + return sanitizeToolCallId(callId ?? id, maxLength); |
| 19 | +} |
| 20 | + |
| 21 | +export function normalizeToolCallIdsForProvider( |
| 22 | + messages: Message[], |
| 23 | + policy: ToolCallIdPolicy, |
| 24 | +): Message[] { |
| 25 | + const rawIds = collectToolCallIds(messages); |
| 26 | + if (rawIds.length === 0) return messages; |
| 27 | + |
| 28 | + const mappedIds = buildToolCallIdMap(rawIds, policy); |
| 29 | + let changed = false; |
| 30 | + const normalizedMessages = messages.map((message) => { |
| 31 | + let messageChanged = false; |
| 32 | + let toolCalls = message.toolCalls; |
| 33 | + |
| 34 | + if (message.toolCalls.length > 0) { |
| 35 | + toolCalls = message.toolCalls.map((toolCall) => { |
| 36 | + const mappedId = mappedIds.get(toolCall.id); |
| 37 | + if (mappedId === undefined || mappedId === toolCall.id) return toolCall; |
| 38 | + messageChanged = true; |
| 39 | + return { ...toolCall, id: mappedId } satisfies ToolCall; |
| 40 | + }); |
| 41 | + } |
| 42 | + |
| 43 | + const toolCallId = |
| 44 | + message.toolCallId === undefined ? undefined : mappedIds.get(message.toolCallId); |
| 45 | + const mappedToolCallId = toolCallId ?? message.toolCallId; |
| 46 | + if (mappedToolCallId !== message.toolCallId) { |
| 47 | + messageChanged = true; |
| 48 | + } |
| 49 | + |
| 50 | + if (!messageChanged) return message; |
| 51 | + changed = true; |
| 52 | + return { ...message, toolCalls, toolCallId: mappedToolCallId }; |
| 53 | + }); |
| 54 | + |
| 55 | + return changed ? normalizedMessages : messages; |
| 56 | +} |
| 57 | + |
| 58 | +function collectToolCallIds(messages: Message[]): string[] { |
| 59 | + const ids: string[] = []; |
| 60 | + const seen = new Set<string>(); |
| 61 | + const append = (id: string): void => { |
| 62 | + if (seen.has(id)) return; |
| 63 | + seen.add(id); |
| 64 | + ids.push(id); |
| 65 | + }; |
| 66 | + |
| 67 | + for (const message of messages) { |
| 68 | + for (const toolCall of message.toolCalls) { |
| 69 | + append(toolCall.id); |
| 70 | + } |
| 71 | + if (message.toolCallId !== undefined) { |
| 72 | + append(message.toolCallId); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + return ids; |
| 77 | +} |
| 78 | + |
| 79 | +function buildToolCallIdMap( |
| 80 | + rawIds: string[], |
| 81 | + policy: ToolCallIdPolicy, |
| 82 | +): Map<string, string> { |
| 83 | + const mappedIds = new Map<string, string>(); |
| 84 | + const usedIds = new Set<string>(); |
| 85 | + |
| 86 | + for (const rawId of rawIds) { |
| 87 | + const normalized = policy.normalize(rawId); |
| 88 | + if (normalized === rawId && normalized.length > 0) { |
| 89 | + mappedIds.set(rawId, normalized); |
| 90 | + usedIds.add(normalized); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + for (const rawId of rawIds) { |
| 95 | + if (mappedIds.has(rawId)) continue; |
| 96 | + const normalized = policy.normalize(rawId); |
| 97 | + const unique = makeUniqueToolCallId(normalized, usedIds, policy.maxLength); |
| 98 | + mappedIds.set(rawId, unique); |
| 99 | + usedIds.add(unique); |
| 100 | + } |
| 101 | + |
| 102 | + return mappedIds; |
| 103 | +} |
| 104 | + |
| 105 | +function makeUniqueToolCallId( |
| 106 | + normalized: string, |
| 107 | + usedIds: Set<string>, |
| 108 | + maxLength: number | undefined, |
| 109 | +): string { |
| 110 | + const base = normalized.length > 0 ? normalized : EMPTY_TOOL_CALL_ID; |
| 111 | + const candidate = truncateToolCallId(base, maxLength, ''); |
| 112 | + if (!usedIds.has(candidate)) return candidate; |
| 113 | + |
| 114 | + for (let i = 2; ; i++) { |
| 115 | + const suffix = `_${i}`; |
| 116 | + const suffixed = truncateToolCallId(base, maxLength, suffix); |
| 117 | + if (!usedIds.has(suffixed)) return suffixed; |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +function truncateToolCallId( |
| 122 | + base: string, |
| 123 | + maxLength: number | undefined, |
| 124 | + suffix: string, |
| 125 | +): string { |
| 126 | + if (maxLength === undefined) return `${base}${suffix}`; |
| 127 | + const baseLength = maxLength - suffix.length; |
| 128 | + if (baseLength <= 0) { |
| 129 | + throw new Error(`Tool call id maxLength ${maxLength} is too small for suffix ${suffix}.`); |
| 130 | + } |
| 131 | + return `${base.slice(0, baseLength)}${suffix}`; |
| 132 | +} |
0 commit comments