Skip to main content

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:

  1. First ,we need to generate inbox name, which can be done using dedicated function available in the Shinkai Library and called getJobInboxNameFromParams. Functions accepts jobId parameter as and sends the inbox id as a result.
  2. 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.
  3. Having ShinkaiMessage in place, we can call last_messages_from_inbox to fetch specific inbox messages
  4. 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->getMessages method 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.