Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
paulclindo committed Nov 7, 2023
1 parent d1d5c25 commit 2728e45
Show file tree
Hide file tree
Showing 14 changed files with 99 additions and 148 deletions.
2 changes: 1 addition & 1 deletion apps/shinkai-app/src/pages/Chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ const Chat: React.FC = () => {
profile_identity_sk: auth.profile_identity_sk,
});
} else {
sendMessageToInbox({
await sendMessageToInbox({
sender: auth.shinkai_identity,
sender_subidentity: auth.profile,
receiver,
Expand Down
4 changes: 3 additions & 1 deletion apps/shinkai-app/src/pages/CreateChat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,15 @@ const CreateChat: React.FC = () => {

createChat({
sender: auth.shinkai_identity,
senderSubidentity: `${auth.profile}/device/${auth.registration_name}`,
senderSubidentity: auth.profile,
receiver,
receiverSubidentity: rest.join('/'),
message: data.message,
my_device_encryption_sk: auth.my_device_encryption_sk,
my_device_identity_sk: auth.my_device_identity_sk,
node_encryption_pk: auth.node_encryption_pk,
profile_encryption_sk: auth.profile_encryption_sk,
profile_identity_sk: auth.profile_identity_sk,
});
};

Expand Down
4 changes: 2 additions & 2 deletions apps/shinkai-app/src/pages/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const MessageButton = ({ inbox }: { inbox: SmartInbox }) => {
node_encryption_pk: auth.node_encryption_pk,
profile_encryption_sk: auth.profile_encryption_sk,
profile_identity_sk: auth.profile_identity_sk,
inboxId: decodeURIComponent(inbox.inbox_id),
inboxId: inbox.inbox_id,
inboxName: data.inboxName,
});
setIsEditable(false);
Expand Down Expand Up @@ -149,7 +149,7 @@ const Home: React.FC = () => {

const { inboxes } = useGetInboxes({
sender: auth?.shinkai_identity ?? '',
senderSubidentity: `${auth?.profile}/device/${auth?.registration_name}`,
senderSubidentity: auth?.profile ?? '',
// Assuming receiver and target_shinkai_name_profile are the same as sender
receiver: auth?.shinkai_identity ?? '',
targetShinkaiNameProfile: `${auth?.shinkai_identity}/${auth?.profile}`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,15 @@ export const CreateInbox = () => {
const [receiver, ...rest] = values.receiverIdentity.split('/');
createChat({
sender: auth.shinkai_identity,
senderSubidentity: `${auth.profile}/device/${auth.registration_name}`,
senderSubidentity: auth.profile,
receiver,
receiverSubidentity: rest.join('/'),
message: values.message,
my_device_encryption_sk: auth.my_device_encryption_sk,
my_device_identity_sk: auth.my_device_identity_sk,
node_encryption_pk: auth.node_encryption_pk,
profile_encryption_sk: auth.profile_encryption_sk,
profile_identity_sk: auth.profile_identity_sk,
});
};

Expand Down
2 changes: 1 addition & 1 deletion apps/shinkai-visor/src/components/inboxes/inboxes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const Inboxes = () => {
const sender = auth?.shinkai_identity ?? '';
const { inboxes } = useGetInboxes({
sender: auth?.shinkai_identity ?? '',
senderSubidentity: `${auth?.profile}/device/${auth?.registration_name}`,
senderSubidentity: auth?.profile ?? '',
// Assuming receiver and target_shinkai_name_profile are the same as sender
receiver: sender,
targetShinkaiNameProfile: `${auth?.shinkai_identity}/${auth?.profile}`,
Expand Down
80 changes: 17 additions & 63 deletions libs/shinkai-message-ts/src/api/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,7 @@ export const createChatWithMessage = async (
receiver: string,
receiver_subidentity: string,
text_message: string,
setupDetailsState: {
my_device_encryption_sk: string;
my_device_identity_sk: string;
node_encryption_pk: string;
}
setupDetailsState: CredentialsPayload
): Promise<{ inboxId: string; message: ShinkaiMessage }> => {
const senderShinkaiName = new ShinkaiNameWrapper(
sender + '/' + sender_subidentity
Expand All @@ -76,8 +72,8 @@ export const createChatWithMessage = async (

try {
const messageStr = ShinkaiMessageBuilderWrapper.create_chat_with_message(
setupDetailsState.my_device_encryption_sk,
setupDetailsState.my_device_identity_sk,
setupDetailsState.profile_encryption_sk,
setupDetailsState.profile_identity_sk,
setupDetailsState.node_encryption_pk,
sender,
sender_subidentity,
Expand Down Expand Up @@ -121,8 +117,8 @@ export const sendTextMessageWithInbox = async (
try {
const messageStr =
ShinkaiMessageBuilderWrapper.send_text_message_with_inbox(
setupDetailsState.my_device_encryption_sk,
setupDetailsState.my_device_identity_sk,
setupDetailsState.profile_encryption_sk,
setupDetailsState.profile_identity_sk,
setupDetailsState.node_encryption_pk,
sender,
sender_subidentity,
Expand All @@ -141,12 +137,12 @@ export const sendTextMessageWithInbox = async (
headers: { 'Content-Type': 'application/json' },
});
await handleHttpError(response);
if (message.body && 'unencrypted' in message.body) {
const inboxId = message.body.unencrypted.internal_metadata.inbox;
return { inboxId, message };
} else {
throw new Error('message body is null or encrypted');
}
// if (message.body && 'unencrypted' in message.body) {
// const inboxId = message.body.unencrypted.internal_metadata.inbox;
// return { inboxId, message };
// } else {
// throw new Error('message body is null or encrypted');
// }
} catch (error) {
console.error('Error sending text message:', error);
throw error;
Expand Down Expand Up @@ -203,8 +199,8 @@ export const getAllInboxesForProfile = async (
try {
const messageString =
ShinkaiMessageBuilderWrapper.get_all_inboxes_for_profile(
setupDetailsState.my_device_encryption_sk,
setupDetailsState.my_device_identity_sk,
setupDetailsState.profile_encryption_sk,
setupDetailsState.profile_identity_sk,
setupDetailsState.node_encryption_pk,
sender,
sender_subidentity,
Expand Down Expand Up @@ -240,8 +236,7 @@ export const updateInboxName = async (
setupDetailsState: CredentialsPayload,
inboxName: string,
inboxId: string
// TODO: fix type
): Promise<any> => {
): Promise<{ success: string; data: null }> => {
try {
const messageString =
ShinkaiMessageBuilderWrapper.update_shinkai_inbox_name(
Expand All @@ -251,7 +246,7 @@ export const updateInboxName = async (
sender,
sender_subidentity,
receiver,
"",
'',
inboxId,
inboxName
);
Expand All @@ -266,7 +261,7 @@ export const updateInboxName = async (
});
const data = await response.json();
await handleHttpError(response);
return data.data;
return data;
} catch (error) {
console.error('Error updating inbox name:', error);
throw error;
Expand All @@ -278,7 +273,7 @@ export const getLastMessagesFromInbox = async (
count: number,
lastKey: string | undefined,
setupDetailsState: LastMessagesFromInboxCredentialsPayload
): Promise<any[]> => {
): Promise<ShinkaiMessage[]> => {
try {
const messageStr =
ShinkaiMessageBuilderWrapper.get_last_messages_from_inbox(
Expand Down Expand Up @@ -310,47 +305,6 @@ export const getLastMessagesFromInbox = async (
}
};

export const getLastUnreadMessagesFromInbox = async (
inbox: string,
count: number,
fromKey: string | undefined,
setupDetailsState: LastMessagesFromInboxCredentialsPayload
): Promise<any[]> => {
try {
const messageStr =
ShinkaiMessageBuilderWrapper.get_last_messages_from_inbox(
setupDetailsState.profile_encryption_sk,
setupDetailsState.profile_identity_sk,
setupDetailsState.node_encryption_pk,
inbox,
count,
fromKey,
setupDetailsState.shinkai_identity,
setupDetailsState.profile,
setupDetailsState.shinkai_identity
);

const message = JSON.parse(messageStr);

const apiEndpoint = ApiConfig.getInstance().getEndpoint();
const response = await fetch(
`${apiEndpoint}/v1/last_unread_messages_from_inbox`,
{
method: 'POST',
body: JSON.stringify(message),
headers: { 'Content-Type': 'application/json' },
}
);
await handleHttpError(response);
const data = await response.json();
return data;
// dispatch(receiveUnreadMessagesFromInbox(inbox, results));
} catch (error) {
console.error('Error getting last messages from inbox:', error);
throw error;
}
};

export const submitRequestRegistrationCode = async (
identity_permissions: string,
code_type = 'profile',
Expand Down
49 changes: 22 additions & 27 deletions libs/shinkai-message-ts/src/wasm/ShinkaiMessageBuilderWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,17 @@ export class ShinkaiMessageBuilderWrapper {
this.wasmBuilder.external_metadata(recipient, sender);
}

external_metadata_with_intra(recipient: string, sender: string, intra_sender: string): void {
this.wasmBuilder.external_metadata_with_intra(recipient, sender, intra_sender);
};
external_metadata_with_intra(
recipient: string,
sender: string,
intra_sender: string
): void {
this.wasmBuilder.external_metadata_with_intra(
recipient,
sender,
intra_sender
);
}

external_metadata_with_other(
recipient: string,
Expand Down Expand Up @@ -271,16 +279,9 @@ export class ShinkaiMessageBuilderWrapper {

builder.message_raw_content(target_shinkai_name_profile);
builder.message_schema_type(MessageSchemaType.TextContent.toString());
builder.internal_metadata(
sender_subidentity,
'',
'None'
);
builder.external_metadata(receiver, sender);
// TODO: fix encryption
builder.body_encryption(
'None'
);
builder.internal_metadata(sender_subidentity, '', 'None');
builder.external_metadata_with_intra(receiver, sender, sender_subidentity);
builder.body_encryption('DiffieHellmanChaChaPoly1305');

const message = builder.build_to_string();

Expand Down Expand Up @@ -435,7 +436,7 @@ export class ShinkaiMessageBuilderWrapper {
sender,
sender_subidentity,
receiver,
""
''
);
}

Expand Down Expand Up @@ -465,7 +466,7 @@ export class ShinkaiMessageBuilderWrapper {
'None'
);
builder.external_metadata_with_intra(receiver, sender, sender_subidentity);
builder.body_encryption('None');
builder.body_encryption('DiffieHellmanChaChaPoly1305');

const message = builder.build_to_string();

Expand Down Expand Up @@ -499,7 +500,7 @@ export class ShinkaiMessageBuilderWrapper {
);
builder.external_metadata_with_intra(receiver, sender, sender_subidentity);

builder.body_encryption('None');
builder.body_encryption('DiffieHellmanChaChaPoly1305');

const message = builder.build_to_string();

Expand All @@ -514,7 +515,7 @@ export class ShinkaiMessageBuilderWrapper {
symmetric_key_sk: string,
sender: string,
sender_subidentity: string,
receiver: string,
receiver: string
): string {
const builder = new ShinkaiMessageBuilderWrapperWASM(
my_encryption_secret_key,
Expand All @@ -523,17 +524,12 @@ export class ShinkaiMessageBuilderWrapper {
);

builder.message_raw_content(symmetric_key_sk);
builder.message_schema_type(MessageSchemaType.SymmetricKeyExchange.toString());
builder.internal_metadata_with_inbox(
sender_subidentity,
'',
inbox,
'None'
builder.message_schema_type(
MessageSchemaType.SymmetricKeyExchange.toString()
);
builder.internal_metadata_with_inbox(sender_subidentity, '', inbox, 'None');
builder.external_metadata_with_intra(receiver, sender, sender_subidentity);
builder.body_encryption(
'DiffieHellmanChaChaPoly1305'
);
builder.body_encryption('DiffieHellmanChaChaPoly1305');
const message = builder.build_to_string();
return message;
}
Expand Down Expand Up @@ -563,7 +559,6 @@ export class ShinkaiMessageBuilderWrapper {
'None'
);
builder.external_metadata_with_intra(receiver, sender, sender_subidentity);
// TODO: fix encryption
builder.body_encryption('DiffieHellmanChaChaPoly1305');

const message = builder.build_to_string();
Expand Down
8 changes: 6 additions & 2 deletions libs/shinkai-node-state/src/lib/mutations/createChat/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createChatWithMessage } from "@shinkai_network/shinkai-message-ts/api";
import { createChatWithMessage } from '@shinkai_network/shinkai-message-ts/api';

import { CreateChatInput } from "./types";
import { CreateChatInput } from './types';

export const createChat = async ({
sender,
Expand All @@ -11,6 +11,8 @@ export const createChat = async ({
my_device_encryption_sk,
my_device_identity_sk,
node_encryption_pk,
profile_encryption_sk,
profile_identity_sk,
}: CreateChatInput) => {
const response = await createChatWithMessage(
sender,
Expand All @@ -22,6 +24,8 @@ export const createChat = async ({
my_device_encryption_sk,
my_device_identity_sk,
node_encryption_pk,
profile_encryption_sk,
profile_identity_sk,
}
);
return response;
Expand Down
7 changes: 2 additions & 5 deletions libs/shinkai-node-state/src/lib/mutations/createChat/types.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import type {
CredentialsPayload,
ShinkaiMessage,
} from "@shinkai_network/shinkai-message-ts/models";
} from '@shinkai_network/shinkai-message-ts/models';

export type CreateChatInput = Pick<
CredentialsPayload,
"my_device_encryption_sk" | "my_device_identity_sk" | "node_encryption_pk"
> & {
export type CreateChatInput = CredentialsPayload & {
sender: string;
senderSubidentity: string;
receiver: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { UseMutationOptions } from "@tanstack/react-query";
import { useMutation } from "@tanstack/react-query";
import type { UseMutationOptions } from '@tanstack/react-query';
import { useMutation } from '@tanstack/react-query';

import { FunctionKey, queryClient } from "../../constants";
import { createChat } from ".";
import { CreateChatInput, CreateChatOutput } from "./types";
import { FunctionKey, queryClient } from '../../constants';
import { createChat } from '.';
import { CreateChatInput, CreateChatOutput } from './types';

type Options = UseMutationOptions<CreateChatOutput, Error, CreateChatInput>;

Expand Down
Loading

0 comments on commit 2728e45

Please sign in to comment.