From 10131363f093e1d3e154cab5bed663e8754289e6 Mon Sep 17 00:00:00 2001 From: Marco Beretta <81851188+berry-13@users.noreply.github.com> Date: Sat, 20 Dec 2025 00:06:25 +0100 Subject: [PATCH] feat: improve pattern matching in findMatchingPattern function for more specific model name matches --- packages/data-provider/src/tokens.ts | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/packages/data-provider/src/tokens.ts b/packages/data-provider/src/tokens.ts index f5c6d6eedc..40df709023 100644 --- a/packages/data-provider/src/tokens.ts +++ b/packages/data-provider/src/tokens.ts @@ -311,22 +311,22 @@ export const maxTokensMap: Record> = { }; /** - * Finds the first matching pattern in the tokens map. - * Searches in reverse order to match more specific patterns first. + * Finds the most specific matching pattern in the tokens map. + * Matches the longest key that the model name starts with. * - * Note: This relies on the insertion order of keys in the tokensMap object. - * More specific patterns must be defined later in the object to be matched first. - * If the order of keys is changed, the matching behavior may be affected. + * @param modelName - The model name to match against patterns. + * @param tokensMap - Map of model patterns to token limits. + * @returns The matched pattern key or null if no match found. */ export function findMatchingPattern( modelName: string, tokensMap: Record, ): string | null { - const keys = Object.keys(tokensMap); const lowerModelName = modelName.toLowerCase(); - for (let i = keys.length - 1; i >= 0; i--) { - const modelKey = keys[i]; - if (lowerModelName.startsWith(modelKey)) { + // Sort keys by length descending to match most specific (longest) pattern first + const keys = Object.keys(tokensMap).sort((a, b) => b.length - a.length); + for (const modelKey of keys) { + if (lowerModelName.startsWith(modelKey.toLowerCase())) { return modelKey; } }