mirror of
https://github.com/run-llama/LlamaIndexTS.git
synced 2026-07-16 07:14:29 -04:00
feat: add support for interrupted and other server content event in live api (#1980)
Co-authored-by: Marcus Schiesser <mail@marcusschiesser.de>
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
---
|
||||
"@llamaindex/google": patch
|
||||
"@llamaindex/core": patch
|
||||
---
|
||||
|
||||
Added interrupted, generationComplete and turnComplete event support in the live api
|
||||
@@ -17,13 +17,23 @@ export type CloseEvent = { type: "close" };
|
||||
|
||||
export type SetupCompleteEvent = { type: "setupComplete" };
|
||||
|
||||
// a client message has interrupted current model generation
|
||||
export type InterruptedEvent = { type: "interrupted" };
|
||||
|
||||
export type GenerationCompleteEvent = { type: "generationComplete" };
|
||||
|
||||
export type TurnCompleteEvent = { type: "turnComplete" };
|
||||
|
||||
export type LiveEvent =
|
||||
| OpenEvent
|
||||
| AudioEvent
|
||||
| TextEvent
|
||||
| ErrorEvent
|
||||
| CloseEvent
|
||||
| SetupCompleteEvent;
|
||||
| SetupCompleteEvent
|
||||
| InterruptedEvent
|
||||
| GenerationCompleteEvent
|
||||
| TurnCompleteEvent;
|
||||
|
||||
export const liveEvents = {
|
||||
open: { include: (e: LiveEvent): e is OpenEvent => e.type === "open" },
|
||||
@@ -41,8 +51,18 @@ export const liveEvents = {
|
||||
include: (e: LiveEvent): e is SetupCompleteEvent =>
|
||||
e.type === "setupComplete",
|
||||
},
|
||||
interrupted: {
|
||||
include: (e: LiveEvent): e is InterruptedEvent => e.type === "interrupted",
|
||||
},
|
||||
generationComplete: {
|
||||
include: (e: LiveEvent): e is GenerationCompleteEvent =>
|
||||
e.type === "generationComplete",
|
||||
},
|
||||
turnComplete: {
|
||||
include: (e: LiveEvent): e is TurnCompleteEvent =>
|
||||
e.type === "turnComplete",
|
||||
},
|
||||
};
|
||||
|
||||
export abstract class LiveLLMSession {
|
||||
protected eventQueue: LiveEvent[] = [];
|
||||
protected eventResolvers: ((value: LiveEvent) => void)[] = [];
|
||||
|
||||
@@ -40,6 +40,18 @@ export class GeminiLiveSession extends LiveLLMSession {
|
||||
super();
|
||||
}
|
||||
|
||||
private isInterruptedEvent(event: LiveServerMessage): boolean {
|
||||
return event.serverContent?.interrupted === true;
|
||||
}
|
||||
|
||||
private isGenerationCompleteEvent(event: LiveServerMessage): boolean {
|
||||
return event.serverContent?.generationComplete === true;
|
||||
}
|
||||
|
||||
private isTurnCompleteEvent(event: LiveServerMessage): boolean {
|
||||
return event.serverContent?.turnComplete === true;
|
||||
}
|
||||
|
||||
private isTextEvent(event: LiveServerMessage): boolean {
|
||||
return event.serverContent?.modelTurn?.parts?.[0]?.text !== undefined;
|
||||
}
|
||||
@@ -111,6 +123,21 @@ export class GeminiLiveSession extends LiveLLMSession {
|
||||
if (this.isToolCallEvent(event)) {
|
||||
this.handleToolCallEvent(event, toolCalls);
|
||||
}
|
||||
if (this.isInterruptedEvent(event)) {
|
||||
this.pushEventToQueue({
|
||||
type: "interrupted",
|
||||
});
|
||||
}
|
||||
if (this.isGenerationCompleteEvent(event)) {
|
||||
this.pushEventToQueue({
|
||||
type: "generationComplete",
|
||||
});
|
||||
}
|
||||
if (this.isTurnCompleteEvent(event)) {
|
||||
this.pushEventToQueue({
|
||||
type: "turnComplete",
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private executeToolCall(toolCall: FunctionCall, tool: BaseTool) {
|
||||
|
||||
Reference in New Issue
Block a user