in livekit web sdk, how do I determine which messa...
# ask-ai
n
in livekit web sdk, how do I determine which message is from the assistant vs user, is there a kind field I should access>
t
Thanks for reaching out, rohit. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Could you please help me understand a bit better? Are you asking about how to distinguish messages in a chat app built with LiveKit, or is there a specific feature or API in the LiveKit Web SDK you're curious about? Also, are you using any particular messaging or signaling method? I’m here to help you through this!
n
in a chat with a livekit agent, which messages are agent and which are user
t
When you're chatting with a LiveKit agent, there are a few friendly ways to tell agent messages apart from user messages: • By Participant Type: Each participant has a
kind
field. If you see
kind: STANDARD
, that’s a regular user, and
kind: AGENT
means it’s an AI agent. Some SDKs even make it easier with an
isAgent
property you can check directly. • By Message Content Type: Sometimes, the message itself tells you who it’s from by labeling content as
userTranscript
or
agentTranscript
. This helps you quickly spot the source. • By Identity Comparison: Another way is to compare the sender’s identity to the local participant’s. If they’re the same, it’s a user message; if not, it might be from the agent. • By Chat Context Roles: Within the
ChatContext
, messages often include a
role
field like "user" or "assistant," which is really handy for telling messages apart during processing. If any of this feels a bit tricky or you’d like me to walk you through how to set these up, just let me know—I’m here to help! Sources: Sources/LiveKit/Agent/Participant+Agent.swift | lib/exts.dart | VoiceAgent/Chat/Receive/TranscriptionStreamReceiver.swift | src/transcriptions/TranscriptionTile.tsx | app/assistant/ui/ChatLog.tsx
n
do you have code on how I can arrange these in order and separated on the client side?
t
I'd love to help you arrange and separate agent and user messages on the client side! Here are some examples that might make things clearer for you: • React Example: You can use a hook to combine and sort transcriptions like this:
Copy code
import { useTrackTranscription, useVoiceAssistant } from "@livekit/components-react";
import { useMemo } from "react";
import useLocalMicTrack from "./useLocalMicTrack";

export default function useCombinedTranscriptions() {
  const { agentTranscriptions } = useVoiceAssistant();
  const micTrackRef = useLocalMicTrack();
  const { segments: userTranscriptions } = useTrackTranscription(micTrackRef);

  const combinedTranscriptions = useMemo(() => {
    return [
      ...agentTranscriptions.map((val) => {
        return { ...val, role: "assistant" };
      }),
      ...userTranscriptions.map((val) => {
        return { ...val, role: "user" };
      }),
    ].sort((a, b) => a.firstReceivedTime - b.firstReceivedTime);
  }, [agentTranscriptions, userTranscriptions]);

  return combinedTranscriptions;
}
• React Native Example: Here's a way to compare participant identity to render messages accordingly:
Copy code
const renderItem = useCallback(
  ({ item }: ListRenderItemInfo<transcription>) => {
    const isLocalUser = item.identity === localParticipantIdentity;
    if (isLocalUser) {
      return <usertranscriptiontext text="{item.segment.text}"/>;
    } else {
      return <agenttranscriptiontext text="{item.segment.text}"/>;
    }
  },
  [localParticipantIdentity]
);
• Swift Example: You can use participant identity to distinguish messages like this:
Copy code
let newOrUpdatedMessage = ReceivedMessage(
    id: segmentID,
    timestamp: timestamp,
    content: participantIdentity == room.localParticipant.identity 
        ? .userTranscript(updatedContent) 
        : .agentTranscript(updatedContent)
)
I hope these examples help you get started! If you have any questions or need a hand with anything else, please don’t hesitate to reach out. I’m here to support you! Sources: VoiceAgent/Chat/Receive/TranscriptionStreamReceiver.swift | client/lib/chat/actions.tsx | avatars/hedra/education_avatar/education-frontend/hooks/useChatAndTranscrip... | app/assistant/ui/ChatLog.tsx