Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update wasm with intra sender #36

Merged
merged 7 commits into from
Oct 11, 2023
Merged
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
8 changes: 4 additions & 4 deletions apps/shinkai-app/src/pages/Chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ const Chat: React.FC = () => {
if (selectedFile) {
// Call sendTextMessageWithFilesForInbox if selectedFile is not null
({ inboxId, message } = await sendTextMessageWithFilesForInbox(
sender,
'',
shinkai_identity,
profile,
receiver,
inputMessage,
deserializedId,
Expand All @@ -145,8 +145,8 @@ const Chat: React.FC = () => {
));
} else {
({ inboxId, message } = await sendTextMessageWithInbox(
sender,
'',
shinkai_identity,
profile,
receiver,
inputMessage,
deserializedId,
Expand Down
82 changes: 47 additions & 35 deletions apps/shinkai-app/src/pages/CreateJob.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,36 @@ import {
IonSelectOption,
IonTextarea,
IonTitle,
} from "@ionic/react";
} from '@ionic/react';
import {
createJob, getProfileAgents, sendMessageToJob
} from "@shinkai_network/shinkai-message-ts/api";
import { SerializedAgent } from "@shinkai_network/shinkai-message-ts/models";
import { InboxNameWrapper, JobCreationWrapper,JobScopeWrapper } from "@shinkai_network/shinkai-message-ts/wasm";
import { History } from "history";
import { useEffect, useState } from "react";
import { useDispatch, useSelector } from "react-redux";
import { useHistory } from "react-router-dom";

import Button from "../components/ui/Button";
import { IonContentCustom, IonHeaderCustom } from "../components/ui/Layout";
import { useSetup } from "../hooks/usetSetup";
import { RootState } from "../store";
import { addAgents } from "../store/actions";
createJob,
getProfileAgents,
sendMessageToJob,
} from '@shinkai_network/shinkai-message-ts/api';
import { SerializedAgent } from '@shinkai_network/shinkai-message-ts/models';
import {
InboxNameWrapper,
JobCreationWrapper,
JobScopeWrapper,
} from '@shinkai_network/shinkai-message-ts/wasm';
import { History } from 'history';
import { useEffect, useState } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { useHistory } from 'react-router-dom';

import Button from '../components/ui/Button';
import { IonContentCustom, IonHeaderCustom } from '../components/ui/Layout';
import { useSetup } from '../hooks/usetSetup';
import { RootState } from '../store';
import { addAgents } from '../store/actions';

const CreateJob: React.FC = () => {
useSetup();
const dispatch = useDispatch();
const setupDetailsState = useSelector(
(state: RootState) => state.setupDetails
);
const [jobContent, setJobContent] = useState("");
const [jobContent, setJobContent] = useState('');
const [selectedAgent, setSelectedAgent] = useState<SerializedAgent | null>(
null
);
Expand All @@ -44,71 +50,77 @@ const CreateJob: React.FC = () => {

useEffect(() => {
const fetchAgents = async () => {
const { shinkai_identity, profile } =
setupDetailsState;
const { shinkai_identity, profile } = setupDetailsState;
const node_name = shinkai_identity;
const sender_subidentity = `${profile}`;

const profiles = await getProfileAgents(
node_name + '/' + sender_subidentity,
'',
node_name,
sender_subidentity,
node_name,
setupDetailsState
);
dispatch(addAgents(profiles));

// Set the first agent as the selected agent
if (profiles.length > 0) {
setSelectedAgent(profiles[0]);
}
};

fetchAgents();
}, [dispatch, setupDetailsState]);

const handleCreateJob = async () => {
console.log("Creating job with content:", jobContent);
console.log('Creating job with content:', jobContent);

const { shinkai_identity, profile } = setupDetailsState;
const sender = shinkai_identity + "/" + profile;
const sender = shinkai_identity + '/' + profile;

const job_creation = JobCreationWrapper.empty().get_scope;
console.log("buckets: ", job_creation.buckets);
console.log("scope:", job_creation);
console.log('buckets: ', job_creation.buckets);
console.log('scope:', job_creation);

const scope = new JobScopeWrapper(
job_creation.buckets,
job_creation.documents
);
console.log("scope:", scope.to_jsvalue());
console.log('scope:', scope.to_jsvalue());

console.log("Selected agent:", selectedAgent);
console.log('Selected agent:', selectedAgent);

const receiver = shinkai_identity;
const receiver_subidentity = `${profile}/agent/${selectedAgent?.id}`;

// Call createJob
const jobId = await createJob(
scope.to_jsvalue(),
sender,
shinkai_identity,
profile,
receiver,
receiver_subidentity,
setupDetailsState
);
console.log("Job created with id:", jobId);
console.log('Job created with id:', jobId);

if (jobId) {
const result = await sendMessageToJob(
jobId.toString(),
jobContent,
"",
sender,
'',
shinkai_identity,
profile,
receiver,
receiver_subidentity,
setupDetailsState
);
dispatch({ type: "SEND_MESSAGE_SUCCESS", payload: result });
dispatch({ type: 'SEND_MESSAGE_SUCCESS', payload: result });

// Hacky solution because react-router can't handle dots in the URL
const jobInboxName = InboxNameWrapper.get_job_inbox_name_from_params(
jobId.toString()
);
const encodedJobId = jobInboxName.get_value.replace(/\./g, "~");
const encodedJobId = jobInboxName.get_value.replace(/\./g, '~');
history.push(`/job-chat/${encodeURIComponent(encodedJobId)}`);
}
};
Expand All @@ -124,12 +136,12 @@ const CreateJob: React.FC = () => {
<IonContentCustom>
<IonGrid
className={
"md:rounded-[1.25rem] bg-white dark:bg-slate-800 p-4 md:p-10 space-y-2 md:space-y-4"
'md:rounded-[1.25rem] bg-white dark:bg-slate-800 p-4 md:p-10 space-y-2 md:space-y-4'
}
>
<IonRow>
<IonCol>
<h2 className={"text-lg mb-3 md:mb-8 text-center"}>
<h2 className={'text-lg mb-3 md:mb-8 text-center'}>
New Job Details
</h2>

Expand All @@ -156,7 +168,7 @@ const CreateJob: React.FC = () => {
/>
</IonItem>

<div style={{ marginTop: "20px" }}>
<div style={{ marginTop: '20px' }}>
<Button onClick={handleCreateJob}>Create Job</Button>
</div>
</IonCol>
Expand Down
10 changes: 4 additions & 6 deletions apps/shinkai-app/src/pages/JobChat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,13 @@ const JobChat: React.FC = () => {
console.log('Sending message: ', inputMessage);
if (inputMessage.trim() === '') return;

const sender = `${shinkai_identity}/${profile}`;
console.log('Sender:', sender);

const message_to_send = inputMessage;
setInputMessage('');
setSelectedFile(null);
if (selectedFile) {
sendTextMessageWithFilesForInbox(
sender,
'',
shinkai_identity,
profile,
shinkai_identity,
message_to_send,
deserializedId,
Expand All @@ -86,7 +83,8 @@ const JobChat: React.FC = () => {
extractJobIdFromInbox(deserializedId.toString()),
message_to_send,
'',
sender,
shinkai_identity,
profile,
shinkai_identity,
'',
setupDetailsState
Expand Down
10 changes: 5 additions & 5 deletions apps/shinkai-visor/src/components/inbox/inbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,13 @@ export const Inbox = () => {
fromPreviousMessagesRef.current = false;
const decodedInboxId = decodeURIComponent(inboxId);
if (isJobInbox(decodedInboxId)) {
const sender = `${auth.shinkai_identity}/${auth.profile}`;
const jobId = extractJobIdFromInbox(decodedInboxId);
sendMessageToJob({
jobId,
message: value,
sender,
files_inbox: '',
shinkaiIdentity: auth.shinkai_identity,
profile: auth.profile,
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,
Expand All @@ -126,12 +125,13 @@ export const Inbox = () => {
const sender = `${auth.shinkai_identity}/${auth.profile}/device/${auth.registration_name}`;
const receiver = extractReceiverShinkaiName(decodedInboxId, sender);
sendMessageToInbox({
sender,
sender: auth.shinkai_identity,
sender_subidentity: auth.profile,
receiver,
message: value,
inboxId: inboxId as string,
my_device_encryption_sk: auth.profile_encryption_sk,
my_device_identity_sk: auth.profile_identity_sk,
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-visor/src/store/inbox/inbox-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,13 @@ export const sendMessage = createAsyncThunk<
ApiConfig.getInstance().setEndpoint(node.nodeData.nodeAddress);

if (isJobInbox(args.inboxId)) {
const sender = `${node.nodeData.shinkaiIdentity}/${node.nodeData.profile}`;
const jobId = extractJobIdFromInbox(args.inboxId);
await sendMessageToJob(
jobId,
args.message,
'',
sender,
node.nodeData.shinkaiIdentity,
node.nodeData.profile,
node.nodeData.shinkaiIdentity,
'',
{
Expand Down
12 changes: 6 additions & 6 deletions apps/shinkai-visor/src/store/jobs/jobs-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ export const createJob = createAsyncThunk<

ApiConfig.getInstance().setEndpoint(node.nodeData.nodeAddress);

const sender = node.nodeData.shinkaiIdentity + "/" + node.nodeData.profile;
const receiver = node.nodeData.shinkaiIdentity;
const receiver_subidentity = `${node.nodeData.profile}/agent/${args.agentId}`;

const job_creation = JobCreationWrapper.empty().get_scope;
Expand All @@ -34,8 +32,9 @@ export const createJob = createAsyncThunk<

const jobId = await createJobApi(
scope.to_jsvalue(),
sender,
receiver,
node.nodeData.shinkaiIdentity,
node.nodeData.profile,
node.nodeData.shinkaiIdentity,
receiver_subidentity,
{
my_device_encryption_sk: node.credentials.myDeviceEncryptionSharedKey,
Expand All @@ -50,8 +49,9 @@ export const createJob = createAsyncThunk<
jobId,
args.content,
'',
sender,
receiver,
node.nodeData.shinkaiIdentity,
node.nodeData.profile,
node.nodeData.shinkaiIdentity,
receiver_subidentity,
{
my_device_encryption_sk: node.credentials.myDeviceEncryptionSharedKey,
Expand Down
2 changes: 1 addition & 1 deletion libs/shinkai-message-ts/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@shinkai_network/shinkai-message-ts",
"version": "0.0.7",
"version": "0.0.9",
"description": "Shinkai Message TS wrapper for wasm library",
"type": "module",
"main": "src/index.js",
Expand Down
Loading
Loading