diff --git a/apps/shinkai-app/src/pages/Chat.tsx b/apps/shinkai-app/src/pages/Chat.tsx index 063b41920..2d22e91c7 100644 --- a/apps/shinkai-app/src/pages/Chat.tsx +++ b/apps/shinkai-app/src/pages/Chat.tsx @@ -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, diff --git a/apps/shinkai-app/src/pages/CreateChat.tsx b/apps/shinkai-app/src/pages/CreateChat.tsx index 320d4f922..97a287b65 100644 --- a/apps/shinkai-app/src/pages/CreateChat.tsx +++ b/apps/shinkai-app/src/pages/CreateChat.tsx @@ -46,15 +46,17 @@ const CreateChat: React.FC = () => { if (!auth) return; const [receiver, ...rest] = data.receiver.split('/'); - createChat({ + await 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, }); }; diff --git a/apps/shinkai-app/src/pages/Home.tsx b/apps/shinkai-app/src/pages/Home.tsx index bd11aa32b..9692868bb 100644 --- a/apps/shinkai-app/src/pages/Home.tsx +++ b/apps/shinkai-app/src/pages/Home.tsx @@ -47,18 +47,16 @@ const MessageButton = ({ inbox }: { inbox: SmartInbox }) => { const onSubmit = async (data: z.infer) => { if (!auth) return; - console.log('data', data); await updateInboxName({ sender: auth?.shinkai_identity ?? '', senderSubidentity: auth?.profile, receiver: auth.shinkai_identity, - receiverSubidentity: '', 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, - inboxId: decodeURIComponent(inbox.inbox_id), + inboxId: inbox.inbox_id, inboxName: data.inboxName, }); setIsEditable(false); @@ -149,7 +147,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}`, diff --git a/apps/shinkai-visor/src/components/create-inbox/create-inbox.tsx b/apps/shinkai-visor/src/components/create-inbox/create-inbox.tsx index 7aaef723f..cf9237173 100644 --- a/apps/shinkai-visor/src/components/create-inbox/create-inbox.tsx +++ b/apps/shinkai-visor/src/components/create-inbox/create-inbox.tsx @@ -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, }); }; diff --git a/apps/shinkai-visor/src/components/inboxes/inboxes.tsx b/apps/shinkai-visor/src/components/inboxes/inboxes.tsx index ef8e5beb6..49de8671a 100644 --- a/apps/shinkai-visor/src/components/inboxes/inboxes.tsx +++ b/apps/shinkai-visor/src/components/inboxes/inboxes.tsx @@ -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}`, diff --git a/libs/shinkai-message-ts/src/api/methods.ts b/libs/shinkai-message-ts/src/api/methods.ts index 76e00c0bf..2feda3945 100644 --- a/libs/shinkai-message-ts/src/api/methods.ts +++ b/libs/shinkai-message-ts/src/api/methods.ts @@ -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 @@ -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, @@ -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, @@ -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, @@ -236,12 +232,10 @@ export const updateInboxName = async ( sender: string, sender_subidentity: string, receiver: string, - receiver_subidentity: string, setupDetailsState: CredentialsPayload, inboxName: string, inboxId: string - // TODO: fix type -): Promise => { +): Promise<{ success: string; data: null }> => { try { const messageString = ShinkaiMessageBuilderWrapper.update_shinkai_inbox_name( @@ -251,7 +245,7 @@ export const updateInboxName = async ( sender, sender_subidentity, receiver, - "", + '', inboxId, inboxName ); @@ -266,7 +260,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; @@ -278,7 +272,7 @@ export const getLastMessagesFromInbox = async ( count: number, lastKey: string | undefined, setupDetailsState: LastMessagesFromInboxCredentialsPayload -): Promise => { +): Promise => { try { const messageStr = ShinkaiMessageBuilderWrapper.get_last_messages_from_inbox( @@ -310,47 +304,6 @@ export const getLastMessagesFromInbox = async ( } }; -export const getLastUnreadMessagesFromInbox = async ( - inbox: string, - count: number, - fromKey: string | undefined, - setupDetailsState: LastMessagesFromInboxCredentialsPayload -): Promise => { - 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', diff --git a/libs/shinkai-message-ts/src/wasm/ShinkaiMessageBuilderWrapper.ts b/libs/shinkai-message-ts/src/wasm/ShinkaiMessageBuilderWrapper.ts index 3654aa696..dcaaeea84 100644 --- a/libs/shinkai-message-ts/src/wasm/ShinkaiMessageBuilderWrapper.ts +++ b/libs/shinkai-message-ts/src/wasm/ShinkaiMessageBuilderWrapper.ts @@ -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, @@ -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(); @@ -435,7 +436,7 @@ export class ShinkaiMessageBuilderWrapper { sender, sender_subidentity, receiver, - "" + '' ); } @@ -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(); @@ -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(); @@ -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, @@ -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; } @@ -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(); diff --git a/libs/shinkai-node-state/src/lib/mutations/createChat/index.ts b/libs/shinkai-node-state/src/lib/mutations/createChat/index.ts index 2943bcf8b..805be6552 100644 --- a/libs/shinkai-node-state/src/lib/mutations/createChat/index.ts +++ b/libs/shinkai-node-state/src/lib/mutations/createChat/index.ts @@ -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, @@ -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, @@ -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; diff --git a/libs/shinkai-node-state/src/lib/mutations/createChat/types.ts b/libs/shinkai-node-state/src/lib/mutations/createChat/types.ts index 7fe20278e..f2684add9 100644 --- a/libs/shinkai-node-state/src/lib/mutations/createChat/types.ts +++ b/libs/shinkai-node-state/src/lib/mutations/createChat/types.ts @@ -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; diff --git a/libs/shinkai-node-state/src/lib/mutations/createChat/useCreateChat.ts b/libs/shinkai-node-state/src/lib/mutations/createChat/useCreateChat.ts index 33939a2e8..efe857b77 100644 --- a/libs/shinkai-node-state/src/lib/mutations/createChat/useCreateChat.ts +++ b/libs/shinkai-node-state/src/lib/mutations/createChat/useCreateChat.ts @@ -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; diff --git a/libs/shinkai-node-state/src/lib/mutations/updateInboxName/index.ts b/libs/shinkai-node-state/src/lib/mutations/updateInboxName/index.ts index 3906d0ab8..f88656948 100644 --- a/libs/shinkai-node-state/src/lib/mutations/updateInboxName/index.ts +++ b/libs/shinkai-node-state/src/lib/mutations/updateInboxName/index.ts @@ -1,46 +1,30 @@ -import { - updateInboxName as updateInboxNameApi, -} from '@shinkai_network/shinkai-message-ts/api'; -import { - type ShinkaiMessage, -} from "@shinkai_network/shinkai-message-ts/models"; +import { updateInboxName as updateInboxNameApi } from '@shinkai_network/shinkai-message-ts/api'; +import { type ShinkaiMessage } from '@shinkai_network/shinkai-message-ts/models'; + +import { UpdateInboxNameInput } from './types'; export type SmartInbox = { custom_name: string; inbox_id: string; last_message: ShinkaiMessage; }; - export const updateInboxName = async ({ senderSubidentity, sender, receiver, - receiverSubidentity, my_device_encryption_sk, my_device_identity_sk, node_encryption_pk, profile_encryption_sk, - profile_identity_sk, // eslint-disable-next-line @typescript-eslint/no-explicit-any + profile_identity_sk, inboxName, - inboxId, // eslint-disable-next-line @typescript-eslint/no-explicit-any -}: { - senderSubidentity: string; - sender: string; - receiver: string; - receiverSubidentity: string; - my_device_encryption_sk: string; - my_device_identity_sk: string; - node_encryption_pk: string; - profile_encryption_sk: string; - profile_identity_sk: string; - inboxName: string; - inboxId: string; -}) => { + inboxId, +}: UpdateInboxNameInput) => { const response = await updateInboxNameApi( sender, senderSubidentity, receiver, - receiverSubidentity, + { my_device_encryption_sk, my_device_identity_sk, @@ -51,7 +35,6 @@ export const updateInboxName = async ({ inboxName, inboxId ); - console.log("updateInboxName response:", response); return response; }; diff --git a/libs/shinkai-node-state/src/lib/mutations/updateInboxName/types.ts b/libs/shinkai-node-state/src/lib/mutations/updateInboxName/types.ts index 7f441d27b..784602802 100644 --- a/libs/shinkai-node-state/src/lib/mutations/updateInboxName/types.ts +++ b/libs/shinkai-node-state/src/lib/mutations/updateInboxName/types.ts @@ -1,5 +1,11 @@ -/* eslint-disable @typescript-eslint/no-explicit-any */ +import { CredentialsPayload } from '@shinkai_network/shinkai-message-ts/models'; -export type UpdateInboxNamebInput = any; +export type UpdateInboxNameInput = CredentialsPayload & { + senderSubidentity: string; + sender: string; + receiver: string; + inboxName: string; + inboxId: string; +}; -export type UpdateInboxNameOutput = any; +export type UpdateInboxNameOutput = { success: string; data: null }; diff --git a/libs/shinkai-node-state/src/lib/mutations/updateInboxName/useUpdateInboxName.ts b/libs/shinkai-node-state/src/lib/mutations/updateInboxName/useUpdateInboxName.ts index f14c24f03..b72a97d28 100644 --- a/libs/shinkai-node-state/src/lib/mutations/updateInboxName/useUpdateInboxName.ts +++ b/libs/shinkai-node-state/src/lib/mutations/updateInboxName/useUpdateInboxName.ts @@ -1,15 +1,24 @@ -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 { updateInboxName } from "."; -import type { UpdateInboxNamebInput,UpdateInboxNameOutput } from "./types"; +import { FunctionKey, queryClient } from '../../constants'; +import { updateInboxName } from '.'; +import type { UpdateInboxNameInput, UpdateInboxNameOutput } from './types'; -// eslint-disable-next-line @typescript-eslint/no-explicit-any -type Options = UseMutationOptions; +type Options = UseMutationOptions< + UpdateInboxNameOutput, + Error, + UpdateInboxNameInput +>; export const useUpdateInboxName = (options?: Options) => { return useMutation({ mutationFn: updateInboxName, - ...options, + onSuccess: (...onSuccessParameters) => { + queryClient.invalidateQueries([FunctionKey.GET_INBOXES]); + if (options?.onSuccess) { + options.onSuccess(...onSuccessParameters); + } + }, }); }; diff --git a/libs/shinkai-node-state/src/lib/queries/getInboxes/index.ts b/libs/shinkai-node-state/src/lib/queries/getInboxes/index.ts index d730288ae..1f52f0337 100644 --- a/libs/shinkai-node-state/src/lib/queries/getInboxes/index.ts +++ b/libs/shinkai-node-state/src/lib/queries/getInboxes/index.ts @@ -1,6 +1,6 @@ -import { getAllInboxesForProfile } from "@shinkai_network/shinkai-message-ts/api"; +import { getAllInboxesForProfile } from '@shinkai_network/shinkai-message-ts/api'; -import type { GetInboxesInput } from "./types"; +import type { GetInboxesInput } from './types'; export const getInboxes = async ({ receiver, @@ -26,8 +26,5 @@ export const getInboxes = async ({ profile_identity_sk, } ); - return inboxes.map((inbox) => ({ - ...inbox, - inbox_id: encodeURIComponent(inbox.inbox_id), - custom_name: encodeURIComponent(inbox.custom_name), - }));}; + return inboxes; +};