Skip to content

feat: Добавлено получение истории сообщений с сервера #275

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
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
3 changes: 3 additions & 0 deletions src/assistantSdk/assistant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,8 @@ export const createAssistant = ({ getMeta, ...configuration }: VpsConfiguration
// обработка входящих команд, и событий аппа
subscriptions.push(
client.on('systemMessage', (systemMessage: SystemMessageDataType, originalMessage: OriginalMessageType) => {
console.log('systemMessage', systemMessage);

if (originalMessage.messageName === 'ANSWER_TO_USER') {
const { activate_app_info, items, app_info: mesAppInfo, character } = systemMessage;

Expand Down Expand Up @@ -408,6 +410,7 @@ export const createAssistant = ({ getMeta, ...configuration }: VpsConfiguration
closeApp,
listen: voice.listen,
sendServerAction,
getChatHistory: protocol.getChatHistory,
sendText,
start,
stop: () => {
Expand Down
2 changes: 2 additions & 0 deletions src/assistantSdk/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ export const createClient = (
});

const off = protocol.on('incoming', (message: OriginalMessageType) => {
console.log('protocol.on incoming', message);

if (message.voice) {
emit('voice', message.voice.data || new Uint8Array(), message);
}
Expand Down
26 changes: 26 additions & 0 deletions src/assistantSdk/client/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import {
Cancel,
ICancel,
IMessage,
IGetHistoryRequest,
IChatHistoryRequest,
ChatHistoryRequest,
} from '../../proto';
import { VpsVersion } from '../../typings';

Expand Down Expand Up @@ -73,6 +76,7 @@ export const createClientMethods = ({
| { legacyDevice: LegacyDevice }
| { initialSettings: InitialSettings }
| { cancel: Cancel }
| IChatHistoryRequest
) & {
last: 1 | -1;
messageName?: string;
Expand Down Expand Up @@ -114,6 +118,27 @@ export const createClientMethods = ({
});
};

const getChatHistory = (
data: Omit<IChatHistoryRequest, 'getHistoryRequest'> & { historyParams?: IGetHistoryRequest },
last = true,
messageId = getMessageId(),
) => {
const { uuid, device, historyParams } = data;

return send({
payload: {
...ChatHistoryRequest.create({
uuid,
device,
getHistoryRequest: historyParams,
}),
messageName: 'GET_HISTORY',
last: last ? 1 : -1,
},
messageId,
});
};

const sendCancel = (data: ICancel, last = true, messageId = getMessageId()) => {
return send({
payload: {
Expand Down Expand Up @@ -278,6 +303,7 @@ export const createClientMethods = ({
return {
sendDevice,
sendInitialSettings,
getChatHistory,
sendCancel,
sendLegacyDevice,
sendSettings,
Expand Down
20 changes: 19 additions & 1 deletion src/assistantSdk/client/protocol.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createNanoEvents } from '../../nanoevents';
import { IDevice, IInitialSettings, ILegacyDevice, IMessage, Message } from '../../proto';
import { IDevice, IInitialSettings, ILegacyDevice, IMessage, Message, IGetHistoryRequest, IDevice } from '../../proto';
import { VpsConfiguration, OriginalMessageType, VpsVersion } from '../../typings';

import { createClientMethods } from './methods';
Expand Down Expand Up @@ -143,6 +143,7 @@ export const createProtocol = (
const {
sendDevice: sendDeviceOriginal,
sendInitialSettings: sendInitialSettingsOriginal,
getChatHistory: getChatHistoryOriginal,
sendCancel,
sendLegacyDevice: sendLegacyDeviceOriginal,
sendSettings: sendSettingsOriginal,
Expand Down Expand Up @@ -171,6 +172,22 @@ export const createProtocol = (
return sendInitialSettingsOriginal(data, ...args);
}) as typeof sendInitialSettingsOriginal;

const getChatHistory = (data?: {
userId?: string;
userChannel?: string;
device?: IDevice;
historyParams?: IGetHistoryRequest;
}) => {
return getChatHistoryOriginal({
uuid: {
userId: data?.userId || userId,
userChannel: data?.userChannel || userChannel,
},
device: data?.device || device,
historyParams: data?.historyParams,
});
};

const sendLegacyDevice = ((data: ILegacyDevice, ...args: never[]) => {
currentSettings = { ...currentSettings, legacyDevice: data };

Expand Down Expand Up @@ -300,6 +317,7 @@ export const createProtocol = (
subscriptions.splice(0, subscriptions.length).map((unsubscribe) => unsubscribe());
},
on,
getChatHistory,
getMessageId,
sendCancel,
sendText,
Expand Down
Loading