Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
428 changes: 230 additions & 198 deletions README.md

Large diffs are not rendered by default.

63 changes: 51 additions & 12 deletions src/TockState.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Context, createContext, Dispatch, Reducer, useContext } from 'react';
import { QuickReply } from './model/buttons';
import { Message } from './model/messages';
import { Message, MessageType } from './model/messages';
import { parseKeep } from './streaming';

export const TockStateContext: Context<TockState | undefined> = createContext<
TockState | undefined
Expand Down Expand Up @@ -40,13 +41,15 @@ export interface TockAction {
| 'SET_QUICKREPLIES'
| 'ADD_MESSAGE'
| 'UPDATE_MESSAGE'
| 'DISCARD_STREAMED_MESSAGE'
| 'SET_METADATA'
| 'SET_LOADING'
| 'SET_SSE_INITIALIZING'
| 'CLEAR_MESSAGES'
| 'SET_ERROR';
quickReplies?: QuickReply[];
messages?: Message[];
responseId?: string;
loading?: boolean;
sseInitializing?: boolean;
metadata?: Record<string, string>;
Expand Down Expand Up @@ -81,28 +84,50 @@ export const tockReducer: Reducer<TockState, TockAction> = (
if (action.messages) {
let messageAlreadyExists = false;

const newMessages = state.messages.map((message) => {
const newMessages = state.messages.map((current) => {
// Search if a streamed message with the same responseId already exists
const messageExists = action.messages?.find((m) => {
const isMessageExists =
m.metadata?.RESPONSE_ID === message.metadata?.RESPONSE_ID &&
const incoming = action.messages?.find((m) => {
const exists =
m.metadata?.RESPONSE_ID === current.metadata?.RESPONSE_ID &&
m.metadata?.TOCK_STREAM_RESPONSE === 'true' &&
message.metadata?.TOCK_STREAM_RESPONSE === 'true';
current.metadata?.TOCK_STREAM_RESPONSE === 'true';

if (isMessageExists) {
if (exists) {
messageAlreadyExists = true;
}

return isMessageExists;
return exists;
});

// If the message already exists, replace with the new message
if (messageExists) {
return messageExists;
// If the message already exists, replace it with the new message,
// or append the fragment to it when the server streams deltas
if (incoming) {
if (
incoming.metadata?.TOCK_STREAM_MODE === 'delta' &&
current.type === MessageType.message &&
incoming.type === MessageType.message
) {
// Desynchronized stream: wait for the final response
if (current.metadata?.TOCK_STREAM_DESYNC === 'true') {
return current;
}
const keep = parseKeep(incoming.metadata?.TOCK_STREAM_KEEP);
if (keep === undefined || keep > current.message.length) {
return {
...current,
metadata: { ...current.metadata, TOCK_STREAM_DESYNC: 'true' },
};
}
return {
...incoming,
message: current.message.slice(0, keep) + incoming.message,
};
}
return incoming;
}

// Else return the original message
return message;
return current;
});

// If the message is new, add it to the messages
Expand Down Expand Up @@ -138,6 +163,20 @@ export const tockReducer: Reducer<TockState, TockAction> = (
};
}
break;
case 'DISCARD_STREAMED_MESSAGE':
if (action.responseId) {
return {
...state,
messages: state.messages.filter(
(m) =>
!(
m.metadata?.RESPONSE_ID === action.responseId &&
m.metadata?.TOCK_STREAM_RESPONSE === 'true'
),
),
};
}
break;
case 'SET_LOADING':
if (action.loading != undefined) {
return {
Expand Down
1 change: 1 addition & 0 deletions src/model/responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface BotConnectorMessage {
widget?: BotConnectorWidget;
image?: BotConnectorImage;
deepLink?: string;
actionId?: string;
}

export interface BotConnectorCard {
Expand Down
1 change: 1 addition & 0 deletions src/settings/TockSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface LocalStorageSettings {

export interface NetworkSettings {
disableSse: boolean;
streamMode?: 'delta';
retryOnPingTimeoutMs: number;
extraHeadersProvider?: () => Promise<Record<string, string>>;
}
Expand Down
72 changes: 72 additions & 0 deletions src/streaming.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { BotConnectorResponse } from './model/responses';

type Metadata = Record<string, string> | undefined;

/**
* Streaming contract of the Tock web connector (metadata of a response envelope):
* - `TOCK_STREAM_RESPONSE=true`: the message replaces the one with the same `RESPONSE_ID`;
* - `TOCK_STREAM_PARTIAL=true`: transient frame of an answer being generated (never counted, never recorded);
* - `TOCK_STREAM_REPLACE=<actionId>`: the final message that replaces the streamed bubble, the others are added;
* - `TOCK_STREAM_DISCARD=true`: the streamed bubble must be removed;
* - `TOCK_REQUEST_ID`: identifier sent by the client with the question, echoed on every frame.
*/
export const isPartialFrame = (metadata: Metadata): boolean =>
metadata?.TOCK_STREAM_PARTIAL === 'true';

export const isDiscardFrame = (metadata: Metadata): boolean =>
metadata?.TOCK_STREAM_DISCARD === 'true';

/** True for the frames of the contract that designates (or removes) the streamed bubble. */
export const usesStreamContract = (metadata: Metadata): boolean =>
isPartialFrame(metadata) ||
isDiscardFrame(metadata) ||
metadata?.TOCK_STREAM_REPLACE !== undefined;

/** Metadata of a message that must not behave as the streamed bubble (stream flags removed). */
export const withoutStreamFlags = (
metadata: Record<string, string> | undefined,
): Record<string, string> | undefined => {
if (!metadata) return metadata;
const {
TOCK_STREAM_RESPONSE: _response,
TOCK_STREAM_PARTIAL: _partial,
TOCK_STREAM_REPLACE: _replace,
TOCK_STREAM_DISCARD: _discard,
TOCK_STREAM_MODE: _mode,
TOCK_STREAM_KEEP: _keep,
...rest
} = metadata;
return rest;
};

/** Parses TOCK_STREAM_KEEP: a non-negative integer, otherwise undefined. */
export const parseKeep = (value: string | undefined): number | undefined =>
value !== undefined && /^\d+$/.test(value) ? Number(value) : undefined;

/**
* A frame is correlated to a request (`TOCK_REQUEST_ID`) that this client is not
* handling, so it must be ignored. The rule is stable for the whole lifetime of a
* stream: a correlated frame is accepted only while its request is in flight here.
*
* It is deliberately NOT gated on `inFlightRequestIds.size > 0`: otherwise an idle
* tab (no request in flight) would accept a foreign tab's partial frames, then start
* rejecting the rest the moment it sends its own question — leaving an orphan
* provisional bubble that is never finalized nor recorded. For the same reason, a late
* frame arriving after the local request completed (success or error — the id is then
* removed from the in-flight set) is treated as foreign and dropped, so it cannot
* revive a closed/errored request.
*
* Frames without a request identifier (e.g. notifications) always pass.
*/
export const isFrameOfOtherRequest = (
botResponse: BotConnectorResponse,
inFlightRequestIds: ReadonlySet<string>,
): boolean => {
const requestId = botResponse.metadata?.TOCK_REQUEST_ID;
return !!requestId && !inFlightRequestIds.has(requestId);
};

export const newRequestId = (): string =>
typeof crypto !== 'undefined' && typeof crypto.randomUUID === 'function'
? crypto.randomUUID()
: `${Date.now().toString(36)}-${Math.random().toString(36).slice(2)}`;
Loading
Loading