Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Generics to the Contexts in the Typescript SDK models #73

Merged
merged 3 commits into from
Sep 19, 2024

Conversation

howlowck
Copy link
Member

Features Added:

  • Enables Generics to the Context for all types/interfaces that has a context
  • The Generic has type constraint to object
  • It's a non-breaking change as the generic is optional and defaults to the original 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:

type UserMsgContext = {
  messageId: string;
  userId: string;
};

type AssistantMsgContext = {
  messageId: string;
  feedback?: {
    rating: number;
    comments: string;
  };
};

type UserMessage = AIChatMessage<UserMsgContext>;
type AssistantMessage = AIChatMessage<AssistantMsgContext>;

and we can have proper typing:
image
image

Also, if the dev tries to set the context to a non-object, it will properly have a type-check error:
image

Copy link
Member

@anfibiacreativa anfibiacreativa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@rohit-ganguly rohit-ganguly merged commit 540122f into microsoft:main Sep 19, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants