Added Generics to the Contexts in the Typescript SDK models #73
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.
Features Added:
object
object
Reason:
I would like to strongly type the AIChatMessage and other context-enabled types; however right now, I would have to redefine the type using type intersection. The more ergonomic approach is to use generics for define what context type should be for these types/interfaces.
Demo:
With the proposed change in this PR, one can set the context type like so:
and we can have proper typing:
![image](https://private-user-images.githubusercontent.com/338265/358019604-d6addd57-481a-41f1-acd2-f30a50a8d3b8.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MzkzMDc1OTMsIm5iZiI6MTczOTMwNzI5MywicGF0aCI6Ii8zMzgyNjUvMzU4MDE5NjA0LWQ2YWRkZDU3LTQ4MWEtNDFmMS1hY2QyLWYzMGE1MGE4ZDNiOC5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjUwMjExJTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI1MDIxMVQyMDU0NTNaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT0zNGFhYmZiOTk3NzQ3MGM0MzQzNDFjNWE2ODRiNDc0MmIxZTg2M2Q2MTU1NzY1ZmRjYjQzNGI2ODdkY2UzZjFlJlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCJ9.7FFMk7-_u8vT_2P74qW0LJ8V4tU2VReSKW8n-VZQacA)
![image](https://private-user-images.githubusercontent.com/338265/358019903-c5d90f73-99a8-47bd-a2df-9a0bb36d1c83.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MzkzMDc1OTMsIm5iZiI6MTczOTMwNzI5MywicGF0aCI6Ii8zMzgyNjUvMzU4MDE5OTAzLWM1ZDkwZjczLTk5YTgtNDdiZC1hMmRmLTlhMGJiMzZkMWM4My5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjUwMjExJTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI1MDIxMVQyMDU0NTNaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT03MDFiZmJkMjVjZmFhYWI4ZjI1MWY2OGU5ZmIzYjEyOWJhYjBiYjEyZmM1YjI3MjgwMThlNWFhYzNkM2Y1ZjY0JlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCJ9.Ir8qKE1Hn_7X3SxUvFu0hiGj15uhOGAhzUJpPov9QIU)
Also, if the dev tries to set the context to a non-object, it will properly have a type-check error:
![image](https://private-user-images.githubusercontent.com/338265/358020352-b738f397-6440-4491-8821-e3d58a92014e.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MzkzMDc1OTMsIm5iZiI6MTczOTMwNzI5MywicGF0aCI6Ii8zMzgyNjUvMzU4MDIwMzUyLWI3MzhmMzk3LTY0NDAtNDQ5MS04ODIxLWUzZDU4YTkyMDE0ZS5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjUwMjExJTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI1MDIxMVQyMDU0NTNaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT0zYzc3NTUwN2VmYTAwYTc5YmUxMThhNTJjYzk3ZGU3MTM4Mjg0NjcxNTA2N2U1ODViM2FjZWMyZDRlZDU5NjMzJlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCJ9.H0eM4iihbAO-FNA7SawRm39wc0F2Uvh9PKOEbCYnIqQ)