fix: Switch from tiktoken to js-tiktoken for worker compatibility #703
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Relates to:
Eliza currently uses
tiktoken
which relies on WASM bindings. This causes build failures in Cloudflare Workers and other edge runtime environments where WASM support is limited or unavailable. This PR migrates the token truncation functionality fromtiktoken
tojs-tiktoken
, which is a pure JavaScript implementation of the same tokenization logic. This change removes the WASM dependency while maintaining the same tokenization functionality.Risks
Seems the collaborator on both packages are the same person so this should be a trusted package. (verify pls)
https://www.npmjs.com/package/js-tiktoken
https://www.npmjs.com/package/tiktoken
Background
What does this PR do?
This PR migrates the token truncation functionality from
tiktoken
tojs-tiktoken
, which is a pure JavaScript implementation of the same tokenization logic. This change removes the WASM dependency while maintaining the same tokenization functionality.What kind of change is this?
Pretty beefy package change and maybe opinionated but id be willing to defend the change if anyone objects. :)
Key changes:
tiktoken
import withjs-tiktoken
TextDecoder
usage sincejs-tiktoken.decode()
returns strings directlyencoding.free()
call as JavaScript handles memory managementjs-tiktoken
interfacesWhy are we doing this? Any context or related work?
Edge compat is gud.
Documentation changes needed?
Testing
Where should a reviewer start?
Take a look at the included test file for instructions on how to use it.
The automated tests have been done to ensure it:
Discord username
antpb