Live API-এর সেশন ম্যানেজ করা

Gemini Live API প্রসেসটি সেশন নামের অডিও বা টেক্সটের একটানা স্ট্রিম প্রসেস করে। আপনি সেশন লাইফসাইকেল ম্যানেজ করতে পারবেন, প্রাথমিক হ্যান্ডশেক থেকে শুরু করে সুন্দরভাবে সেশন শেষ করা পর্যন্ত।

সেশনের সীমা

Live API-এর জন্য, সেশন বলতে এমন একটি স্থায়ী কানেকশনকে বোঝায় যেখানে ইনপুট ও আউটপুট একটি কানেকশনের মাধ্যমে ক্রমাগত স্ট্রিম করা হয়।

সেশন নিম্নলিখিত সীমাগুলির মধ্যে যেকোনও একটি অতিক্রম করলে, কানেকশন বন্ধ হয়ে যায়। তবে মনে রাখবেন, Live API এইসব সেশন-সম্পর্কিত সীমা ম্যানেজ করার জন্য কিছু বিকল্প প্রদান করে (নিচে দেখুন)।

  • সেশন কন্টেক্সট উইন্ডো ১২৮ হাজার টোকেনের মধ্যে সীমাবদ্ধ।

    এই কন্টেক্সট উইন্ডো সংক্রান্ত সীমাবদ্ধতার কারণে, ইনপুট মোডালিটি অনুযায়ী সেশনের আনুমানিক সর্বাধিক দৈর্ঘ্য এখানে দেওয়া হল:

    • শুধুমাত্র অডিও ইনপুট সেশন ১৫ মিনিট পর্যন্ত সীমাবদ্ধ।
    • ভিডিও + অডিও ইনপুট ২ মিনিট পর্যন্ত সীমাবদ্ধ।
  • কানেকশনের সময়সীমা প্রায় ১০ মিনিট পর্যন্ত সীমিত থাকে।

    কানেকশন শেষ হওয়ার প্রায় বিজ্ঞপ্তি ৬০ সেকেন্ড আগে আপনি পাবেন।

সেশন সংক্রান্ত সীমা ম্যানেজ করার জন্য এখানে কিছু বিকল্প দেওয়া হল:

সেশন শুরু করা

কীভাবে সেশন শুরু করতে হয় তা দেখানোর জন্য সম্পূর্ণ স্নিপেট পেতে Live API-এর জন্য শুরু করার গাইড দেখুন।

সেশনের মাঝে আপডেট করা

Live API মডেলগুলি মিড-সেশন আপডেটের জন্য নিম্নলিখিত উন্নত ক্ষমতা প্রদান করে:

ইনক্রিমেন্টাল কন্টেন্ট আপডেট যোগ করা

অ্যাক্টিভ সেশন চলাকালীন আপনি ইনক্রিমেন্টাল আপডেট যোগ করতে পারবেন। টেক্সট ইনপুট পাঠাতে, সেশন কন্টেক্সট সেট করতে বা সেশন কন্টেক্সট রিস্টোর করতে এটি ব্যবহার করুন।

  • দীর্ঘ কন্টেক্সটের ক্ষেত্রে, আমরা পরবর্তী ইন্টার‍্যাকশনের জন্য কন্টেক্সট উইন্ডো খালি করতে একটি মেসেজের সারসংক্ষেপ প্রদান করার সাজেশন দিই।

  • ছোট প্রসঙ্গে, আপনি ইভেন্টের সঠিক ক্রম বোঝাতে, নিচে দেওয়া স্নিপেটের মতো, ধাপে ধাপে ইন্টার‍্যাকশন পাঠাতে পারেন।

Swift

// Define initial turns (history/context).
let turns: [ModelContent] = [
  ModelContent(role: "user", parts: [TextPart("What is the capital of France?")]),
  ModelContent(role: "model", parts: [TextPart("Paris")]),
]

// Send history, keeping the conversational turn OPEN (false).
await session.sendContent(turns, turnComplete: false)

// Define the new user query.
let newTurn: [ModelContent] = [
  ModelContent(role: "user", parts: [TextPart("What is the capital of Germany?")]),
]

// Send the final query, CLOSING the turn (true) to trigger the model response.
await session.sendContent(newTurn, turnComplete: true)

Kotlin

// Define initial turns (history/context).
val turns = listOf(
  content("user") {
    text("What is the capital of France?")
  },
  content("model") {
    text("Paris")
  }
)

// Send history, keeping the conversational turn OPEN (false).
turns.forEach {
  session.send(
    content = it,
    turnComplete = false
  )
}

// Define the new user query.
val newTurn = content("user") {
  text("What is the capital of Germany?")
}

// Send the final query, CLOSING the turn (true) to trigger the model response.
session.send(
  content = newTurn,
  turnComplete = true
)

Java

// Define initial turns (history/context).
List turns =
    Arrays.asList(
        new Content.Builder().setRole("user").addText("What is the capital of France?").build(),
        new Content.Builder().setRole("model").addText("Paris").build());

for (Content turn : turns) {
  session.send(
    turn,
    false // turnComplete: false
  );
}

// Define the new user query.
Content newTurn = new Content.Builder().addText("What is the capital of Germany?").build();

// Send the final query, CLOSING the turn (true) to trigger the model response.
session.send(
  newTurn,
  true // isTurnComplete: true
);

Web

const turns = [{ text: "Hello from the user!" }];

await session.send(
  turns,
  false // turnComplete: false
);

console.log("Sent history. Waiting for next input...");

// Define the new user query.
const newTurn [{ text: "And what is the capital of Germany?" }];

// Send the final query, CLOSING the turn (true) to trigger the model response.
await session.send(
    newTurn,
    true // turnComplete: true
);
console.log("Sent final query. Model response expected now.");

Dart

// Define initial turns (history/context).
final List turns = [
  Content(
    "user",
    [Part.text("What is the capital of France?")],
  ),
  Content(
    "model",
    [Part.text("Paris")],
  ),
];

// Send history, keeping the conversational turn OPEN (false).
await session.send(
  input: turns,
  turnComplete: false,
);

// Define the new user query.
final List newTurn = [
  Content(
    "user",
    [Part.text("What is the capital of Germany?")],
  ),
];

// Send the final query, CLOSING the turn (true) to trigger the model response.
await session.send(
  input: newTurn,
  turnComplete: true,
);

Unity

// Define initial turns (history/context).
List turns = new List {
    new ModelContent("user", new ModelContent.TextPart("What is the capital of France?") ),
    new ModelContent("model", new ModelContent.TextPart("Paris") ),
};

// Send history, keeping the conversational turn OPEN (false).
foreach (ModelContent turn in turns)
{
    await session.SendAsync(
        content: turn,
        turnComplete: false
    );
}

// Define the new user query.
ModelContent newTurn = ModelContent.Text("What is the capital of Germany?");

// Send the final query, CLOSING the turn (true) to trigger the model response.
await session.SendAsync(
    content: newTurn,
    turnComplete: true
);

সেশনের মাঝে সিস্টেম নির্দেশাবলী আপডেট করা

আপনার API প্রদানকারী হিসেবে Agent Platform Gemini API (formerly Vertex AI) ব্যবহার করলেই শুধুমাত্র উপলভ্য।

অ্যাক্টিভ সেশন চলাকালীন আপনি সিস্টেম নির্দেশাবলী আপডেট করতে পারবেন। মডেলের উত্তর অ্যাডাপ্ট করতে এটি ব্যবহার করুন, যেমন উত্তরের ভাষা পরিবর্তন করতে বা টোন পরিবর্তন করতে।

সেশন চলাকালীন সিস্টেম নির্দেশাবলী আপডেট করতে, আপনি system ভূমিকা সহ টেক্সট কন্টেন্ট পাঠাতে পারেন। সেশনের বাকি সময় ধরে আপডেট করা সিস্টেম নির্দেশাবলী প্রযোজ্য থাকবে।

Swift

await session.sendContent(
  [ModelContent(
    role: "system",
    parts: [TextPart("new system instruction")]
  )],
  turnComplete: false
)

Kotlin

// In a coroutine scope
session.send(
    content = content("system") {
        text("new system instruction")
    },
    turnComplete = false
)

Java

session.send(
    new Content.Builder()
        .setRole("system")
        .addText("new system instruction")
        .build(),
    /* turnComplete: */ false
);

Web

Not yet supported for Web apps - check back soon!

Dart

try {
  await _session.send(
    input: Content(
      'system',
      [Part.text('new system instruction')],
    ),
    turnComplete: false,
  );
} catch (e) {
  print('Failed to update system instructions: $e');
}

Unity

try
{
    await session.SendAsync(
        content: new ModelContent(
            "system",
            new ModelContent.TextPart("new system instruction")
        ),
        turnComplete: false
    );
}
catch (Exception e)
{
    Debug.LogError($"Failed to update system instructions: {e.Message}");
}

কন্টেক্সট উইন্ডো কম্প্রেস করা

এই পৃষ্ঠায় প্রদানকারী-নির্দিষ্ট কন্টেন্ট ও কোড দেখতে আপনার Gemini API প্রদানকারীর আইকনে ক্লিক করুন।

Live API সেশন কনটেক্সট উইন্ডো রিয়েল-টাইম স্ট্রিম করা ডেটা (অডিওর জন্য প্রতি সেকেন্ডে ২৫টি টোকেন (TPS) এবং ভিডিওর জন্য ২৫৮টি TPS) এবং অন্যান্য কন্টেন্ট স্টোর করে, যার মধ্যে টেক্সট ইনপুট ও মডেল আউটপুট অন্তর্ভুক্ত। সব Live API মডেলে সেশন কন্টেক্সট উইন্ডোর সীমা ১২৮ হাজার টোকেন।

ডিফল্ট হিসেবে, এই কন্টেক্সট উইন্ডো সংক্রান্ত বিধিনিষেধের কারণে, ইনপুট মোডালিটি অনুযায়ী এখানে সেশনের আনুমানিক সর্বাধিক দৈর্ঘ্য দেওয়া হল:

  • শুধুমাত্র অডিও ইনপুট সেশন ১৫ মিনিট পর্যন্ত সীমাবদ্ধ।
  • ভিডিও + অডিও ইনপুট ২ মিনিট পর্যন্ত সীমাবদ্ধ।

দীর্ঘক্ষণ ধরে চলা সেশনে, কথোপকথন যত এগোয়, অডিও এবং/অথবা ভিডিও টোকেনের ইতিহাস জমা হতে থাকে। এই ইতিহাস মডেলের সীমা অতিক্রম করলে, মডেল হ্যালুসিনেশন করতে পারে, ধীরে ধীরে কাজ করতে পারে বা সেশনটি জোর করে বন্ধ করে দেওয়া হতে পারে।

আরও দীর্ঘ সেশন চালু করতে, আপনি কন্টেক্সট উইন্ডো কম্প্রেশন চালু করতে পারেন, এর জন্য LiveGenerationConfig-এর অংশ হিসেবে contextWindowCompression ফিল্ড সেট করুন। চালু করা হলে, ডিফল্ট বা নির্দিষ্ট সীমার মধ্যে কনটেক্সট সাইজ বজায় রাখতে, সবচেয়ে পুরনো টার্ন অটোমেটিক বাতিল করতে বা সেগুলির সারসংক্ষেপ করতে, সার্ভার একটি স্লাইডিং-উইন্ডো মেকানিজম ব্যবহার করে। সিস্টেম নির্দেশাবলী বাতিল করা হয় না এবং সবসময় কনটেক্সট উইন্ডোর শুরুতে থাকে।

ব্যবহারকারীর দৃষ্টিকোণ থেকে, এটি তাত্ত্বিকভাবে অসীম সেশন সময়সীমার অনুমতি দেয় কারণ "মেমরি" ক্রমাগত ম্যানেজ করা হয়।

আপনি স্লাইডিং-উইন্ডো মেকানিজম এবং ঐচ্ছিকভাবে কম্প্রেশন ট্রিগার করা টোকেনের সংখ্যা কনফিগার করতে পারবেন: (নিচে উপলভ্য সেটিংস এবং ভ্যালু দেখুন )। এইসব সেটিংস ব্যবহার করার ব্যাপারে কিছু উচ্চ-স্তরের বিবেচনা এখানে দেওয়া হল:

  • targetTokens খুব কম সেটিং করলে, একটানা স্ট্রিমের জন্য আরও বেশি প্রসঙ্গ রুম খালি হয়ে যাবে, কিন্তু মডেলটি কথোপকথনের পুরনো টার্ন দ্রুত "ভুলে" যাবে।

  • targetTokens-কে triggerTokens-এর কাছাকাছি সেট করলে আরও বেশি মেমরি সেভ করা যায়, তবে কম্প্রেশন রুটিন অনেক বেশি ঘনঘন ট্রিগার হবে।

সেটিং কনফিগারেশনে সেট করা না থাকলে স্লাইডিং উইন্ডোর ডিফল্ট ভ্যালু ন্যূনতম ভ্যালু সর্বাধিক ভ্যালু
triggerTokens
কম্প্রেশন ট্রিগার করার আগে কনটেক্সট লেন্থ
মডেলের কন্টেক্সট উইন্ডো সীমার ৮০% ৫,০০০ ১২৮,০০০
targetTokens
রাখার জন্য টোকেনের টার্গেট সংখ্যা
triggerTokens ভ্যালুর ৫০%
  • triggerTokens স্পষ্টভাবে সেট করা না থাকলে, targetTokens ডিফল্ট triggerTokens ভ্যালুর ৫০% হিসেবে ডিফল্ট সেট করা হয়।
  • targetTokens ভ্যালু অবশ্যই triggerTokens ভ্যালুর চেয়ে কম হতে হবে।
0 ১২৮,০০০

Swift


// ...

let liveModel = FirebaseAI.firebaseAI(backend: .googleAI()).liveModel(
  modelName: "GEMINI_LIVE_API_MODEL_NAME",
  // Enable context window compression.
  // (Optional) Configure the number of tokens in the context window that triggers the compression.
  generationConfig: LiveGenerationConfig(
    responseModalities: [.audio],
    contextWindowCompression: ContextWindowCompressionConfig(
      triggerTokens: 10000,
      slidingWindow: SlidingWindow(
        targetTokens: 2000,
      )
    )
  )
)

Kotlin


// ...

val liveModel = Firebase.ai(backend = GenerativeBackend.googleAI()).liveModel(
    modelName = "GEMINI_LIVE_API_MODEL_NAME",
    // Enable context window compression.
    // (Optional) Configure the number of tokens in the context window that triggers the compression.
    generationConfig = liveGenerationConfig {
        responseModality = ResponseModality.AUDIO,
        contextWindowCompression = ContextWindowCompressionConfig(
            triggerTokens = 10000,
            slidingWindow = SlidingWindow(targetTokens = 2000)
        )
    }
)

Java


// ...

LiveGenerativeModel lm = FirebaseAI.getInstance(GenerativeBackend.googleAI()).liveModel(
        "GEMINI_LIVE_API_MODEL_NAME",
        // Enable context window compression.
        // (Optional) Configure the number of tokens in the context window that triggers the compression.
        new LiveGenerationConfig.Builder()
                .setResponseModality(ResponseModality.AUDIO)
                .setContextWindowCompression(
                        new ContextWindowCompressionConfig(10000, new SlidingWindow(2000))
                )
                .build()
);

Web


// ...

const ai = getAI(firebaseApp, { backend: new GoogleAIBackend() });

const liveModel = getLiveGenerativeModel(ai, {
  model: "GEMINI_LIVE_API_MODEL_NAME",
  // Enable context window compression.
  // (Optional) Configure the number of tokens in the context window that triggers the compression.
  generationConfig: {
    responseModalities: [ResponseModality.AUDIO],
    contextWindowCompression: {
      triggerTokens: 10000,
      slidingWindow: {
        targetTokens: 2000,
      },
    },
  },
});

Dart


// ...

final _liveModel = FirebaseAI.googleAI().liveGenerativeModel(
  model: 'GEMINI_LIVE_API_MODEL_NAME',
  // Enable context window compression.
  // (Optional) Configure the number of tokens in the context window that triggers the compression.
  liveGenerationConfig: LiveGenerationConfig(
    responseModalities: [ResponseModalities.audio],
    contextWindowCompression: ContextWindowCompressionConfig(
      triggerTokens: 10000,
      slidingWindow: SlidingWindow(targetTokens: 2000),
    ),
  ),
);

Unity


// ...

var liveModel = FirebaseAI.GetInstance(FirebaseAI.Backend.GoogleAI()).GetLiveModel(
    modelName: "GEMINI_LIVE_API_MODEL_NAME",
    // Enable context window compression.
    // (Optional) Configure the number of tokens in the context window that triggers the compression.
    liveGenerationConfig: new LiveGenerationConfig(
        responseModalities: new[] { ResponseModality.Audio },
        contextWindowCompression: new ContextWindowCompressionConfig(
            triggerTokens: 10000,
            slidingWindow: new SlidingWindow(targetTokens: 2000)
        )
    )
);

কোনও সেশন কখন শেষ হতে চলেছে তা শনাক্ত করা

একটি অবিচ্ছিন্ন WebSocket কানেকশনের সর্বাধিক সময়সীমা হল ১০ মিনিট। কানেকশন শেষ হওয়ার ৬০ সেকেন্ড আগে ক্লায়েন্টকে একটি বন্ধ হয়ে যাচ্ছে বিজ্ঞপ্তি পাঠানো হয়, যা আপনাকে আরও অ্যাকশন নিতে (যেমন, সেশন আবার চালু করে) সাহায্য করতে পারে।

নিম্নলিখিত উদাহরণে দেখানো হয়েছে যে কীভাবে চলে যাচ্ছে বিজ্ঞপ্তি শুনে আসন্ন কানেকশন বন্ধ করা শনাক্ত করা যায়:

Swift

for try await response in session.responses {
  switch response.payload {

  case .goingAwayNotice(let goingAwayNotice):
    // Prepare for the session to close soon
    if let timeLeft = goingAwayNotice.timeLeft {
        print("Server going away in \(timeLeft) seconds")
    }
  }
}

Kotlin

for (response in session.responses) {
    when (val message = response.payload) {
        is LiveServerGoAway -> {
            // Prepare for the session to close soon
            val remaining = message.timeLeft
            logger.info("Server going away in $remaining")
        }
    }
}

Java

session.getResponses().forEach(response -> {
    if (response.getPayload() instanceof LiveServerResponse.GoingAwayNotice) {
        LiveServerResponse.GoingAwayNotice notice = (LiveServerResponse.GoingAwayNotice) response.getPayload();
        // Prepare for the session to close soon
        Duration timeLeft = notice.getTimeLeft();
    }
});

Web

for await (const message of session.receive()) {
  switch (message.type) {

  ...
  case "goingAwayNotice":
    console.log("Server going away. Time left:", message.timeLeft);
    break;
  }
}

Dart

Future _handleLiveServerMessage(LiveServerResponse response) async {
  final message = response.message;
  if (message is GoingAwayNotice) {
     // Prepare for the session to close soon
     developer.log('Server going away. Time left: ${message.timeLeft}');
  }
}

Unity

foreach (var response in session.Responses) {
    if (response.Payload is LiveSessionGoingAway notice) {
        // Prepare for the session to close soon
        TimeSpan timeLeft = notice.TimeLeft;
        Debug.Log($"Server going away notice received. Remaining: {timeLeft}");
    }
}

সেশন আবার চালু করা

Live API কথোপকথনের প্রসঙ্গ যাতে হারিয়ে না যায়, সেই জন্য সেশন আবার চালু করার সুবিধা আছে। প্রতিটি সেশনের একটি হ্যান্ডেল থাকে এবং এটি নিম্নলিখিত উপায়ে ব্যবহার করা যেতে পারে:

  • কানেকশনের সময়সীমা শেষ হওয়ার আগে সেশন বজায় রাখা

    একটি অবিচ্ছিন্ন WebSocket কানেকশনের সর্বাধিক সময়সীমা হল ১০ মিনিট। আপনি চলে যাওয়া বিজ্ঞপ্তি শোনার মাধ্যমে কানেকশন কখন শেষ হতে চলেছে তা শনাক্ত করতে পারবেন এবং তারপর সেশন হ্যান্ডেল ব্যবহার করে নতুন কানেকশন স্থাপন করার মাধ্যমে সেশনের মেয়াদ বাড়াতে পারবেন।

  • কানেকশন ড্রপ হওয়ার সাথে সাথেই সেশন আবার চালু করা

    সর্বাধিক কানেকশন টাইম লিমিটের আগে কোনও কানেকশন বন্ধ হয়ে গেলে বা ড্রপ হয়ে গেলে (যেমন, ওয়াই-ফাই থেকে 5G-তে পরিবর্তন করা), সার্ভার সেশন স্টেট প্রায় ১০ মিনিট ধরে রাখে। এই উইন্ডোতে, আপনি সেশন হ্যান্ডেল ব্যবহার করে নতুন কানেকশন তৈরি করে সেশন আবার চালু করতে পারবেন।

  • দীর্ঘ সময় পরে সেশন আবার চালু করা

    কানেকশন শেষ হয়ে যাওয়ার পরে, সার্ভার কয়েক ঘণ্টার জন্য সেশন স্টেট সেভ করে রাখে। এই উইন্ডোতে, সেশন হ্যান্ডেল ব্যবহার করে নতুন কানেকশন তৈরি করার মাধ্যমে আপনি সেশন আবার চালু করতে পারবেন। মনে রাখবেন যে এই উইন্ডোটি দুটি Gemini API প্রদানকারীর জন্য আলাদা: Gemini Developer API হল ২ ঘণ্টা | Agent Platform Gemini API (formerly Vertex AI) হল ২৪ ঘণ্টা।

সাধারণত, সেশন আবার চালু করার সুবিধা বন্ধ করা থাকে। সেশন আবার চালু করার সুবিধা চালু করতে, নতুন কানেকশন তৈরি করার সময় খালি আবার চালু করার কনফিগারেশন পাস করুন। চালু করা হলে, সার্ভার সেশন আবার চালু করার হ্যান্ডেল সহ আপডেট মাঝে মাঝে পাঠায়। সেশন ডিসকানেক্ট হয়ে গেলে, আপনি আবার কানেক্ট করতে এবং এই হ্যান্ডেলটি পাস করতে পারবেন যাতে সেশনটি আগের মতোই চলতে থাকে।

সেশন আবার চালু করার দুটি বিকল্প নিম্নলিখিত উদাহরণে দেখানো হয়েছে:

Swift

// Local variable to save the active session handle
var activeSessionHandle: String?

// Initialize the session. Passing an empty config requests the server to send SessionResumptionUpdate
var session = try await liveModel.connect(
  sessionResumption: SessionResumptionConfig()
)

// Start receiving responses
for try await message in session.responses {
  // Check for new session handles inside your message handling loop
  switch message.payload {
  case let .sessionResumptionUpdate(updateMessage):
    guard let newHandle = updateMessage.newHandle, updateMessage.resumable else {
      continue
    }
    activeSessionHandle = newHandle
    print("SessionResumptionUpdate: handle \(newHandle)")
  // ... handle other LiveServerMessage types ...
  default:
    break
  }
}

// The following are alternative options to resume a session. Choose only one.

// Option 1: Create and connect a session to resume with the saved handle
if let handle = activeSessionHandle {
  session = try await liveModel.connect(
    sessionResumption: SessionResumptionConfig(handle: handle)
  )
}

// Option 2: Resume the session directly on an existing session object
if let handle = activeSessionHandle {
  try await session.resumeSession(
    sessionResumption: SessionResumptionConfig(handle: handle)
  )
}

Kotlin

// Local variable to save the active session handle
var activeSessionHandle: String? = null

// Initialize the session. Passing an empty config requests the server to send SessionResumptionUpdate
var session = liveModel.connect(
    sessionResumption = SessionResumptionConfig()
)

// Start receiving responses
session.receive().collect { message ->
    // Process other received response types...

    // Check for new session handles inside your message handling loop
    if (message is LiveSessionResumptionUpdate) {
        if (message.resumable == true && message.newHandle != null) {
            activeSessionHandle = message.newHandle
            Log.d("TAG", "SessionResumptionUpdate: handle ${message.newHandle}")
        }
    }
}

// The following are alternative options to resume a session. Choose only one.

// Option 1: Create and connect a session to resume with the saved handle
activeSessionHandle?.let { handle ->
    session = liveModel.connect(
        sessionResumption = SessionResumptionConfig(handle = handle)
    )
}

// Option 2: Resume the session directly on an existing session object
activeSessionHandle?.let { handle ->
    session.resumeSession(
        sessionResumption = SessionResumptionConfig(handle = handle)
    )
}

Java

For Java, session resumption is not yet supported. Check back soon!

Web

// Local variable to save the active session handle
let activeSessionHandle = null;

// Initialize the session. Passing an empty object requests the server to send SessionResumptionUpdate
let session = await liveModel.connect({});

// Start receiving responses
for await (const message of session.receive()) {
  // Process other received response types...

  // Check for new session handles inside your message handling loop
  if (message.type === 'sessionResumptionUpdate') {
    if (message.resumable && message.newHandle) {
      activeSessionHandle = message.newHandle;
      console.log(`SessionResumptionUpdate: handle ${activeSessionHandle}`);
    }
  }
}

// The following are alternative options to resume a session. Choose only one.

// Option 1: Create and connect a session to resume with the saved handle
if (activeSessionHandle) {
  session = await liveModel.connect({
    handle: activeSessionHandle
  });
}

// Option 2: Resume the session directly on an existing session object
if (activeSessionHandle) {
  await session.resumeSession({
    handle: activeSessionHandle
  });
}

Dart

// Local variable to save the active session handle
String? _activeSessionHandle;

// Initialize the session. Passing an empty config requests the server to send SessionResumptionUpdate
var _session = await _liveModel.connect(
  sessionResumption: SessionResumptionConfig(),
);

// Start receiving responses
await for (final message in _session.receive()) {
  // Process other received response types...

  // Check for new session handles inside your message handling loop
  if (message is SessionResumptionUpdate &&
      message.resumable != null &&
      message.resumable!) {
    _activeSessionHandle = message.newHandle;
    log('SessionResumptionUpdate: handle ${message.newHandle}');
  }
}

// The following are alternative options to resume a session. Choose only one.

// Option 1: Create and connect a session to resume with the saved handle
if (_activeSessionHandle != null) {
  _session = await _liveModel.connect(
    sessionResumption: SessionResumptionConfig.resume(_activeSessionHandle!),
  );
}

// Option 2: Alternatively, resume the session directly on an existing session object
if (_activeSessionHandle != null) {
  await _session.resumeSession(
    sessionResumption: SessionResumptionConfig.resume(_activeSessionHandle!),
  );
}

Unity

// Local variable to save the active session handle
string activeSessionHandle = null;

// Initialize the session. Passing an empty config requests the server to send SessionResumptionUpdate
var session = await liveModel.ConnectAsync(
    sessionResumption: new SessionResumptionConfig()
);

// Start receiving responses
await foreach (var response in session.ReceiveAsync())
{
  // Process other received response types...

  // Check for new session handles inside your message handling loop
  if (response.Message is LiveSessionResumptionUpdate updateMessage)
  {
    if (updateMessage.Resumable == true && !string.IsNullOrEmpty(updateMessage.NewHandle))
    {
      activeSessionHandle = updateMessage.NewHandle;
      Debug.Log($"SessionResumptionUpdate: handle {activeSessionHandle}");
    }
  }
}

// The following are alternative options to resume a session. Choose only one.

// Option 1: Create and connect a session to resume with the saved handle
if (!string.IsNullOrEmpty(activeSessionHandle)) {
  session = await liveModel.ConnectAsync(
      sessionResumption: new SessionResumptionConfig(activeSessionHandle)
  );
}

// Option 2: Resume the session directly on an existing session object
if (!string.IsNullOrEmpty(activeSessionHandle)) {
  await session.ResumeSessionAsync(
      sessionResumption: new SessionResumptionConfig(activeSessionHandle)
  );
}