Skip to content

Commit

Permalink
adding upload to job
Browse files Browse the repository at this point in the history
  • Loading branch information
nicarq committed Dec 30, 2024
1 parent 955a5e2 commit f94d920
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 71 deletions.
97 changes: 71 additions & 26 deletions libs/shinkai-message-ts/src/api/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,41 +136,50 @@ export const sendTextMessageWithInbox = async (
}
};

export const sendTextMessageWithFilesForInbox = async (
export const sendTextMessageWithFilesToJob = async (
nodeAddress: string,
sender: string,
sender_subidentity: string,
receiver: string,
text_message: string,
job_inbox: string,
job_id: string,
files: File[],
setupDetailsState: CredentialsPayload,
bearerToken: string,
): Promise<{ inboxId: string; message: ShinkaiMessage }> => {
const fileUploader = new FileUploader(
// Upload files using the uploadFilesToVR function
const uploadResponse = await uploadFilesToJob(
nodeAddress,
setupDetailsState.profile_encryption_sk,
setupDetailsState.profile_identity_sk,
setupDetailsState.node_encryption_pk,
job_inbox,
sender,
sender_subidentity,
receiver,
bearerToken,
job_id,
files
);

await fileUploader.createFolder();
for (const fileToUpload of files) {
await fileUploader.uploadEncryptedFile(fileToUpload);
if (uploadResponse.status !== 'success') {
throw new Error('Failed to upload files');
}
const message = await fileUploader.finalizeAndSend(text_message, null);

if (message.body && 'unencrypted' in message.body) {
const inboxId = message.body.unencrypted.internal_metadata.inbox;
return { inboxId, message };
} else {
console.warn('message body is null or encrypted');
// TODO: workaround to skip error reading encrypted message
return { inboxId: job_inbox, message };
}
// Prepare the message payload
const messagePayload = {
job_message: {
job_id,
content: text_message,
files: [], // update with an array of string with the file paths
},
};

// Send the message to the job
const response = await httpClient.post(
urlJoin(nodeAddress, '/v2/job_message'),
messagePayload,
{
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${bearerToken}`,
},
}
);

const data = response.data;

// Assuming the response contains the message and inboxId
return { inbox_id: job_id, message: data };
};

export const updateInboxName = async (
Expand Down Expand Up @@ -1659,3 +1668,39 @@ export const removeRowsSheet = async (
const data = response.data;
return data;
};

export const uploadFilesToJob = async (
nodeAddress: string,
bearerToken: string,
jobId: string,
files: File[],
): Promise<{ status: string }> => {
try {
for (const fileToUpload of files) {
const formData = new FormData();
formData.append('file_data', fileToUpload);
formData.append('filename', fileToUpload.name);
formData.append('job_id', jobId);

const response = await httpClient.post(
urlJoin(nodeAddress, '/v2/upload_file_to_job'),
formData,
{
headers: {
'Content-Type': 'multipart/form-data',
'Authorization': `Bearer ${bearerToken}`,
},
}
);

if (response.status !== 200) {
throw new Error(`Failed to upload file: ${fileToUpload.name}`);
}
}

return { status: 'success' };
} catch (error) {
console.error('Error uploadFilesToJob:', error);
throw error;
}
};

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { sendTextMessageWithFilesToJob } from '@shinkai_network/shinkai-message-ts/api';

import { SendMessageWithFilesToInboxInput } from './types';

export const sendMessageWithFilesToJob = async ({
nodeAddress,
message,
jobId,
files,
}: SendMessageWithFilesToInboxInput) => {
return await sendTextMessageWithFilesToJob(
nodeAddress,
message,
jobId,
files,
);
};
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import type {
CredentialsPayload,
ShinkaiMessage,
} from '@shinkai_network/shinkai-message-ts/models';

export type SendMessageWithFilesToInboxInput = CredentialsPayload & {
export type SendMessageWithFilesToInboxInput = {
nodeAddress: string;
sender: string;
receiver: string;
senderSubidentity: string;
token: string;
message: string;
inboxId: string;
jobId: string;
files: File[];
};

export type SendMessageWithFilesToInboxOutput = {
inboxId: string;
jobId: string;
message: ShinkaiMessage;
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import {
useQueryClient,
} from '@tanstack/react-query';

import { FunctionKey } from '../../constants';
import { sendMessageWithFilesToInbox } from '.';
import { FunctionKey } from '../../../lib/constants';
import { sendMessageWithFilesToJob } from '.';
import {
SendMessageWithFilesToInboxInput,
SendMessageWithFilesToInboxOutput,
Expand All @@ -20,7 +20,7 @@ type Options = UseMutationOptions<
export const useSendMessageWithFilesToInbox = (options?: Options) => {
const queryClient = useQueryClient();
return useMutation({
mutationFn: sendMessageWithFilesToInbox,
mutationFn: sendMessageWithFilesToJob,
...options,
onSuccess: (response, variables, context) => {
queryClient.invalidateQueries({
Expand Down

0 comments on commit f94d920

Please sign in to comment.