Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.
The contextoverflow event fires on a LanguageModel instance when a call to prompt(), promptStreaming(), or append() causes the session's contextUsage to exceed the contextWindow.
Use the event name in methods like addEventListener(), or set an event handler property.
addEventListener("contextoverflow", (event) => {})
oncontextoverflow = (event) => {}A generic Event.
The code below shows two methods of creating an event listener for the contextoverflow event.
const session = await LanguageModel.create();
session.addEventListener("contextoverflow", () => {
console.warn("Context overflow detected.");
});Alternatively:
const session = await LanguageModel.create();
session.oncontextoverflow = () => {
console.warn(
"The session's context window is full. " +
"Consider cloning the session or starting a new one.",
);
};The following example creates a new session when the contextoverflow event is triggered.
let session = await LanguageModel.create({
initialPrompts: [{ role: "system", content: "You are a helpful assistant." }],
});
session.addEventListener("contextoverflow", async () => {
console.log("Context full — creating a fresh session.");
session.destroy();
session = await LanguageModel.create({
initialPrompts: [
{ role: "system", content: "You are a helpful assistant." },
],
});
});
async function chat(userMessage) {
const response = await session.prompt(userMessage);
return response;
}