canonicalToolCallId function

String canonicalToolCallId(
  1. String id
)

Canonical wire form of a tool-call id: the projection every provider adapter applies before putting an id on the wire — characters outside [a-zA-Z0-9_-] become _ (Anthropic/Google/OpenAI _normalizeToolCallId) and the result truncates to 40 chars (the strictest limit, OpenAI). Pairing checks and the uniqueness stamp compare canonical forms, so two raw ids that collapse into one provider-side id count as duplicates even when their raw forms differ.

Implementation

String canonicalToolCallId(String id) {
  final sanitized = id.replaceAll(RegExp('[^a-zA-Z0-9_-]'), '_');
  return sanitized.length <= 40 ? sanitized : sanitized.substring(0, 40);
}