Receive Node Responses
When the node processes the messages it pushes them in the inboxes, from which the responses can be fetched. There are 2 approaches: HTTP one and the other which allows to Websockets usage. Let's dive in.
HTTP approach​
HTTP approach allows to fetch messages from specific inboxes using endpoint exposed by the node. In order to fetch such a message a unique (deterministically defined) inbox name must be generated by the service which wants to pull the responses. Here's a set of steps how to do it:
- First ,we need to generate
inboxname, which can be done using dedicated function available in the Shinkai Library and calledgetJobInboxNameFromParams. Functions acceptsjobIdparameter as and sends the inbox id as a result. - Then, we need to create a message for the node where we say to the node, that we want to fetch specific information from the node.
- Having
ShinkaiMessagein place, we can calllast_messages_from_inboxto fetch specific inbox messages - At this stage, it's crucial to determine the location of our message within the payload. For an exemplary implementation of this process, refer to the
ShinkaiManager->getMessagesmethod within the Shinkai Slackbot repository.
For the steps 1-3, this is the exemplary implementation:
const inbox = InboxName.getJobInboxNameFromParams(jobId).value;
const messageBuilder = new ShinkaiMessageBuilder(
encryptionSecretKey,
signatureSecretKey,
receiverPublicKey
);
await messageBuilder.init();
const inboxMessage = messageBuilder
.set_message_raw_content(
JSON.stringify({ inbox: inbox, count: 10, offset: null })
)
.set_body_encryption(TSEncryptionMethod.None)
.set_message_schema_type(MessageSchemaType.APIGetMessagesFromInboxRequest)
.set_internal_metadata_with_inbox(
profileName,
"",
inbox,
TSEncryptionMethod.None
)
.set_external_metadata_with_intra_sender(
shinkaiName,
shinkaiName,
profileName
)
.build();
// then call `last_messages_from_inbox` and pass inboxMessage as a payload
Websocket approach​
This approach is implemented on the node, however not yet integrated in any example, so this part of the documentation will be updated soon.