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

Add chat API support #278

Merged
merged 8 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/livekit-rtc/rust-sdks
Submodule rust-sdks updated 100 files
1 change: 1 addition & 0 deletions packages/livekit-rtc/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@ export { StreamState, TrackKind, TrackSource } from './proto/track_pb.js';
export { VideoBufferType, VideoRotation } from './proto/video_frame_pb.js';
export { ParticipantKind } from './proto/participant_pb.js';
export { dispose } from './ffi_client.js';
export type { ChatMessage } from './types.js';
59 changes: 59 additions & 0 deletions packages/livekit-rtc/src/participant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import { FfiClient, FfiHandle } from './ffi_client.js';
import type { OwnedParticipant, ParticipantInfo, ParticipantKind } from './proto/participant_pb.js';
import type {
ChatMessage,
PublishDataCallback,
PublishDataResponse,
PublishSipDtmfCallback,
Expand All @@ -12,6 +13,8 @@ import type {
PublishTrackResponse,
PublishTranscriptionCallback,
PublishTranscriptionResponse,
SendChatMessageCallback,
SendChatMessageResponse,
SetLocalAttributesCallback,
SetLocalAttributesResponse,
SetLocalMetadataCallback,
Expand All @@ -23,11 +26,13 @@ import type {
UnpublishTrackResponse,
} from './proto/room_pb.js';
import {
EditChatMessageRequest,
TranscriptionSegment as ProtoTranscriptionSegment,
PublishDataRequest,
PublishSipDtmfRequest,
PublishTrackRequest,
PublishTranscriptionRequest,
SendChatMessageRequest,
SetLocalAttributesRequest,
SetLocalMetadataRequest,
SetLocalNameRequest,
Expand Down Expand Up @@ -185,6 +190,60 @@ export class LocalParticipant extends Participant {
});
}

async sendChatMessage(
text: string,
destinationIdentities?: Array<string>,
Copy link
Contributor

Choose a reason for hiding this comment

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

does this default to all? Worth adding docstrings

senderIdentity?: string,
) {
const req = new SendChatMessageRequest({
localParticipantHandle: this.ffi_handle.handle,
message: text,
destinationIdentities,
senderIdentity,
});

const res = FfiClient.instance.request<SendChatMessageResponse>({
message: { case: 'sendChatMessage', value: req },
});

const cb = await FfiClient.instance.waitFor<SendChatMessageCallback>((ev) => {
return ev.message.case == 'chatMessage' && ev.message.value.asyncId == res.asyncId;
});

if (cb.error) {
throw new Error(cb.error);
}
return cb.chatMessage!;
}

async editChatMessage(
editText: string,
originalMessage: ChatMessage,
destinationIdentities?: Array<string>,
Copy link
Contributor

Choose a reason for hiding this comment

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

seems kinda weird that you can give different values here than to the original. this might also cause participants who join a call after an initial message is sent but before an edit to receive the edit and maybe have strange behavior? not sure if there's an easy way to solve though

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah, had the same feeling and opted to omit destinationIdentites on client-sdk-js altogether for now because of that

senderIdentity?: string,
) {
const req = new EditChatMessageRequest({
localParticipantHandle: this.ffi_handle.handle,
editText,
originalMessage,
destinationIdentities,
senderIdentity,
});

const res = FfiClient.instance.request<SendChatMessageResponse>({
message: { case: 'editChatMessage', value: req },
});

const cb = await FfiClient.instance.waitFor<SendChatMessageCallback>((ev) => {
return ev.message.case == 'chatMessage' && ev.message.value.asyncId == res.asyncId;
});

if (cb.error) {
throw new Error(cb.error);
}
return cb.chatMessage!;
}

async updateName(name: string) {
const req = new SetLocalNameRequest({
localParticipantHandle: this.ffi_handle.handle,
Expand Down
30 changes: 29 additions & 1 deletion packages/livekit-rtc/src/proto/ffi_pb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import type { BinaryReadOptions, FieldList, JsonReadOptions, JsonValue, PartialMessage, PlainMessage } from "@bufbuild/protobuf";
import { Message, proto3, protoInt64 } from "@bufbuild/protobuf";
import { ConnectCallback, ConnectRequest, ConnectResponse, DisconnectCallback, DisconnectRequest, DisconnectResponse, GetSessionStatsCallback, GetSessionStatsRequest, GetSessionStatsResponse, PublishDataCallback, PublishDataRequest, PublishDataResponse, PublishSipDtmfCallback, PublishSipDtmfRequest, PublishSipDtmfResponse, PublishTrackCallback, PublishTrackRequest, PublishTrackResponse, PublishTranscriptionCallback, PublishTranscriptionRequest, PublishTranscriptionResponse, RoomEvent, SetLocalAttributesCallback, SetLocalAttributesRequest, SetLocalAttributesResponse, SetLocalMetadataCallback, SetLocalMetadataRequest, SetLocalMetadataResponse, SetLocalNameCallback, SetLocalNameRequest, SetLocalNameResponse, SetSubscribedRequest, SetSubscribedResponse, UnpublishTrackCallback, UnpublishTrackRequest, UnpublishTrackResponse } from "./room_pb.js";
import { ConnectCallback, ConnectRequest, ConnectResponse, DisconnectCallback, DisconnectRequest, DisconnectResponse, EditChatMessageRequest, GetSessionStatsCallback, GetSessionStatsRequest, GetSessionStatsResponse, PublishDataCallback, PublishDataRequest, PublishDataResponse, PublishSipDtmfCallback, PublishSipDtmfRequest, PublishSipDtmfResponse, PublishTrackCallback, PublishTrackRequest, PublishTrackResponse, PublishTranscriptionCallback, PublishTranscriptionRequest, PublishTranscriptionResponse, RoomEvent, SendChatMessageCallback, SendChatMessageRequest, SendChatMessageResponse, SetLocalAttributesCallback, SetLocalAttributesRequest, SetLocalAttributesResponse, SetLocalMetadataCallback, SetLocalMetadataRequest, SetLocalMetadataResponse, SetLocalNameCallback, SetLocalNameRequest, SetLocalNameResponse, SetSubscribedRequest, SetSubscribedResponse, UnpublishTrackCallback, UnpublishTrackRequest, UnpublishTrackResponse } from "./room_pb.js";
import { CreateAudioTrackRequest, CreateAudioTrackResponse, CreateVideoTrackRequest, CreateVideoTrackResponse, EnableRemoteTrackRequest, EnableRemoteTrackResponse, GetStatsCallback, GetStatsRequest, GetStatsResponse, LocalTrackMuteRequest, LocalTrackMuteResponse, TrackEvent } from "./track_pb.js";
import { CaptureVideoFrameRequest, CaptureVideoFrameResponse, NewVideoSourceRequest, NewVideoSourceResponse, NewVideoStreamRequest, NewVideoStreamResponse, VideoConvertRequest, VideoConvertResponse, VideoStreamEvent, VideoStreamFromParticipantRequest, VideoStreamFromParticipantResponse } from "./video_frame_pb.js";
import { AudioStreamEvent, AudioStreamFromParticipantRequest, AudioStreamFromParticipantResponse, CaptureAudioFrameCallback, CaptureAudioFrameRequest, CaptureAudioFrameResponse, ClearAudioBufferRequest, ClearAudioBufferResponse, NewAudioResamplerRequest, NewAudioResamplerResponse, NewAudioSourceRequest, NewAudioSourceResponse, NewAudioStreamRequest, NewAudioStreamResponse, RemixAndResampleRequest, RemixAndResampleResponse } from "./audio_frame_pb.js";
Expand Down Expand Up @@ -153,6 +153,18 @@ export class FfiRequest extends Message<FfiRequest> {
*/
value: PublishSipDtmfRequest;
case: "publishSipDtmf";
} | {
/**
* @generated from field: livekit.proto.SendChatMessageRequest send_chat_message = 33;
*/
value: SendChatMessageRequest;
case: "sendChatMessage";
} | {
/**
* @generated from field: livekit.proto.EditChatMessageRequest edit_chat_message = 34;
*/
value: EditChatMessageRequest;
case: "editChatMessage";
} | {
/**
* Track
Expand Down Expand Up @@ -290,6 +302,8 @@ export class FfiRequest extends Message<FfiRequest> {
{ no: 12, name: "get_session_stats", kind: "message", T: GetSessionStatsRequest, oneof: "message" },
{ no: 13, name: "publish_transcription", kind: "message", T: PublishTranscriptionRequest, oneof: "message" },
{ no: 14, name: "publish_sip_dtmf", kind: "message", T: PublishSipDtmfRequest, oneof: "message" },
{ no: 33, name: "send_chat_message", kind: "message", T: SendChatMessageRequest, oneof: "message" },
{ no: 34, name: "edit_chat_message", kind: "message", T: EditChatMessageRequest, oneof: "message" },
{ no: 15, name: "create_video_track", kind: "message", T: CreateVideoTrackRequest, oneof: "message" },
{ no: 16, name: "create_audio_track", kind: "message", T: CreateAudioTrackRequest, oneof: "message" },
{ no: 17, name: "local_track_mute", kind: "message", T: LocalTrackMuteRequest, oneof: "message" },
Expand Down Expand Up @@ -416,6 +430,12 @@ export class FfiResponse extends Message<FfiResponse> {
*/
value: PublishSipDtmfResponse;
case: "publishSipDtmf";
} | {
/**
* @generated from field: livekit.proto.SendChatMessageResponse send_chat_message = 33;
*/
value: SendChatMessageResponse;
case: "sendChatMessage";
} | {
/**
* Track
Expand Down Expand Up @@ -553,6 +573,7 @@ export class FfiResponse extends Message<FfiResponse> {
{ no: 12, name: "get_session_stats", kind: "message", T: GetSessionStatsResponse, oneof: "message" },
{ no: 13, name: "publish_transcription", kind: "message", T: PublishTranscriptionResponse, oneof: "message" },
{ no: 14, name: "publish_sip_dtmf", kind: "message", T: PublishSipDtmfResponse, oneof: "message" },
{ no: 33, name: "send_chat_message", kind: "message", T: SendChatMessageResponse, oneof: "message" },
{ no: 15, name: "create_video_track", kind: "message", T: CreateVideoTrackResponse, oneof: "message" },
{ no: 16, name: "create_audio_track", kind: "message", T: CreateAudioTrackResponse, oneof: "message" },
{ no: 17, name: "local_track_mute", kind: "message", T: LocalTrackMuteResponse, oneof: "message" },
Expand Down Expand Up @@ -721,6 +742,12 @@ export class FfiEvent extends Message<FfiEvent> {
*/
value: PublishSipDtmfCallback;
case: "publishSipDtmf";
} | {
/**
* @generated from field: livekit.proto.SendChatMessageCallback chat_message = 22;
*/
value: SendChatMessageCallback;
case: "chatMessage";
} | { case: undefined; value?: undefined } = { case: undefined };

constructor(data?: PartialMessage<FfiEvent>) {
Expand Down Expand Up @@ -751,6 +778,7 @@ export class FfiEvent extends Message<FfiEvent> {
{ no: 19, name: "get_session_stats", kind: "message", T: GetSessionStatsCallback, oneof: "message" },
{ no: 20, name: "panic", kind: "message", T: Panic, oneof: "message" },
{ no: 21, name: "publish_sip_dtmf", kind: "message", T: PublishSipDtmfCallback, oneof: "message" },
{ no: 22, name: "chat_message", kind: "message", T: SendChatMessageCallback, oneof: "message" },
]);

static fromBinary(bytes: Uint8Array, options?: Partial<BinaryReadOptions>): FfiEvent {
Expand Down
Loading
Loading