Skip to content

Commit

Permalink
feat: adding S2C logs to conversation history
Browse files Browse the repository at this point in the history
  • Loading branch information
Disfractal committed Oct 29, 2024
1 parent 69e79a3 commit a589ce3
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/web-core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Server to Client log events
- Latency reporting support
- S2C logs added to conversation history

### Changed

Expand Down
38 changes: 35 additions & 3 deletions packages/web-core/src/components/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
ConversationMapItem,
Extension,
InworlControlAction,
LogLevel,
TaskParameter,
TriggerParameter,
User,
Expand All @@ -26,12 +27,13 @@ interface InworldHistoryAddProps<InworldPacketT> {

export enum CHAT_HISTORY_TYPE {
ACTOR = 'actor',
CONVERSATION_UPDATE = 'conversation_update',
INTERACTION_END = 'interaction_end',
LOG_EVENT = 'log_event',
NARRATED_ACTION = 'narrated_action',
SCENE_CHANGE = 'scene_change',
TRIGGER_EVENT = 'trigger_event',
TASK_EVENT = 'task_event',
INTERACTION_END = 'interaction_end',
SCENE_CHANGE = 'scene_change',
CONVERSATION_UPDATE = 'conversation_update',
}

export interface HistoryItemBase {
Expand Down Expand Up @@ -84,6 +86,13 @@ export interface HistoryItemNarratedAction extends HistoryItemBase {
characters?: Character[];
}

export interface HistoryItemLogEvent extends HistoryItemBase {
type: CHAT_HISTORY_TYPE.LOG_EVENT;
text: string;
level: LogLevel;
metadata?: Record<string, string>;
}

export interface HistoryItemSceneChange {
date: Date;
id: string;
Expand Down Expand Up @@ -115,6 +124,7 @@ export interface HistoryItemConversationUpdate {

export type HistoryItem =
| HistoryItemActor
| HistoryItemLogEvent
| HistoryItemTriggerEvent
| HistoryItemTaskEvent
| HistoryInteractionEnd
Expand Down Expand Up @@ -254,6 +264,13 @@ export class InworldHistory<
};
break;

case packet.isLog():
historyItem = {
...this.combineLogItem(packet),
fromHistory,
};
break;

case packet.isInteractionEnd():
const controlItem: HistoryInteractionEnd = {
...this.combineInteractionEndItem(packet),
Expand Down Expand Up @@ -685,6 +702,21 @@ export class InworldHistory<
};
}

private combineLogItem(packet: InworldPacketT): HistoryItemLogEvent {
return {
id: packet.packetId.utteranceId,
scene: this.scene,
conversationId: packet.packetId.conversationId,
date: new Date(packet.date),
interactionId: packet.packetId.interactionId,
level: packet.log.level,
metadata: packet.log.metadata,
source: packet.routing.source,
text: packet.log.text,
type: CHAT_HISTORY_TYPE.LOG_EVENT,
};
}

private convertToExtendedType(packet: InworldPacketT, item: HistoryItem) {
return this.extension?.historyItem?.(packet, item) || item;
}
Expand Down

0 comments on commit a589ce3

Please sign in to comment.