Skip to content

Commit

Permalink
Add separate structured request for polling
Browse files Browse the repository at this point in the history
  • Loading branch information
peterszerzo committed Jan 12, 2024
1 parent fe17571 commit 41aee23
Showing 1 changed file with 12 additions and 36 deletions.
48 changes: 12 additions & 36 deletions packages/chat-core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// TODO: isomorphic-fetch is currently removed due to UMD bundler issues
// import fetch from "isomorphic-fetch";
// TODO: isomorphic-fetch is currently causing issues in the UMD build
import fetch from "isomorphic-fetch";
import ReconnectingWebSocket from "reconnecting-websocket";
import { equals, findLastIndex, update, reverse } from "ramda";
import { v4 as uuid } from "uuid";
Expand Down Expand Up @@ -75,10 +75,7 @@ export type UserResponsePayload =
| ({
type: "structured";
context?: Context;
} & StructuredRequest)
| {
type: "pollPendingDataRequest";
};
} & StructuredRequest);

// Failure message

Expand Down Expand Up @@ -141,6 +138,7 @@ export interface StructuredRequest {
choiceId?: string;
intentId?: string;
slots?: Slots | SlotValue[];
poll?: boolean;
}

interface BotRequest {
Expand Down Expand Up @@ -282,8 +280,15 @@ export const createConversation = (config: Config): ConversationHandler => {
newResponse,
);
if (response.metadata.hasPendingDataRequest) {
appendStructuredUserResponse({ poll: true });
setTimeout(() => {
pollPendingDataRequest();
sendToBot({
request: {
structured: {
poll: true,
},
},
});
}, 1500);
}
} else {
Expand Down Expand Up @@ -446,35 +451,6 @@ export const createConversation = (config: Config): ConversationHandler => {
});
};

const pollPendingDataRequest = () => {
const lastIntentId = reverse(state.responses).find(
(response): response is BotResponse =>
Boolean(response.type === "bot" && response.payload.metadata?.intentId),
)?.payload.metadata?.intentId;
if (lastIntentId) {
const newResponse: Response = {
type: "user",
receivedAt: new Date().getTime(),
payload: {
type: "pollPendingDataRequest",
},
};
sendToBot({
request: {
structured: {
intentId: lastIntentId,
},
},
});
setState(
{
responses: [...state.responses, newResponse],
},
newResponse,
);
}
};

const unsubscribe = (subscriber: Subscriber) => {
subscribers = subscribers.filter((fn) => fn !== subscriber);
};
Expand Down

0 comments on commit 41aee23

Please sign in to comment.