Function decodeTokenIds

  • Decodes an encoded string of token IDs into an array of individual token IDs using bigint for precise calculations.

    The encoded token IDs can be in the following formats:

    1. Single numbers: '123' => ['123']
    2. Comma-separated numbers: '1,2,3,4' => ['1', '2', '3', '4']
    3. Ranges of numbers: '5:8' => ['5', '6', '7', '8']
    4. Combinations of single numbers and ranges: '1,3:5,8' => ['1', '3', '4', '5', '8']
    5. Wildcard '' (matches all token IDs): '' => ['*']

    Parameters

    • encodedTokenIds: string

      The encoded string of token IDs to be decoded.

    Returns string[]

    An array of individual token IDs after decoding the input.

    Throws

    If the input is not correctly formatted or if bigint operations fail.

    Example

    const encoded = '1,3:5,8';
    const decoded = decodeTokenIds(encoded); // Output: ['1', '3', '4', '5', '8']

    Example

    const encodedWildcard = '*';
    const decodedWildcard = decodeTokenIds(encodedWildcard); // Output: ['*']

    Example

    const emptyEncoded = '';
    const decodedEmpty = decodeTokenIds(emptyEncoded); // Output: []