From e3d8c3fc408577f0006dd59a35734fa2e7c7b8a5 Mon Sep 17 00:00:00 2001 From: paulclindo Date: Mon, 30 Dec 2024 23:03:39 -0500 Subject: [PATCH] vector fs changes, fixes --- .../chat/context/set-job-scope-context.tsx | 2 +- .../src/components/sheet/data-table-cell.tsx | 8 ++--- .../vector-fs/components/all-files-tab.tsx | 6 ++-- .../vector-fs/components/vector-fs-folder.tsx | 5 +-- .../vector-fs/components/vector-fs-item.tsx | 20 ++++------- .../src/components/create-job/create-job.tsx | 4 +-- libs/shinkai-message-ts/src/api/jobs/index.ts | 35 ++++++++++++++++--- libs/shinkai-message-ts/src/api/jobs/types.ts | 30 +++++++++++----- libs/shinkai-message-ts/src/api/methods.ts | 25 ++++++++----- .../src/api/recurring-tasks/types.ts | 14 ++------ .../src/models/SchemaTypes.ts | 22 ------------ .../src/models/ShinkaiMessage.ts | 12 +------ .../src/v2/mutations/createJob/index.ts | 9 ++--- .../src/v2/mutations/createJob/types.ts | 6 ++-- .../v2/mutations/createRecurringTask/index.ts | 6 ---- .../v2/mutations/sendMessageToJob/index.ts | 7 ++-- .../src/v2/mutations/updateJobScope/types.ts | 6 ++-- .../v2/queries/getChatConversation/index.ts | 2 ++ .../src/v2/queries/getJobScope/types.ts | 2 +- 19 files changed, 107 insertions(+), 114 deletions(-) diff --git a/apps/shinkai-desktop/src/components/chat/context/set-job-scope-context.tsx b/apps/shinkai-desktop/src/components/chat/context/set-job-scope-context.tsx index 88f47c2b8..5787bfa78 100644 --- a/apps/shinkai-desktop/src/components/chat/context/set-job-scope-context.tsx +++ b/apps/shinkai-desktop/src/components/chat/context/set-job-scope-context.tsx @@ -1,4 +1,4 @@ -import { ShinkaiPath } from '@shinkai_network/shinkai-message-ts/models'; +import { ShinkaiPath } from '@shinkai_network/shinkai-message-ts/api/jobs/types'; import { TreeCheckboxSelectionKeys } from 'primereact/tree'; import React, { createContext, useContext, useState } from 'react'; import { createStore, useStore } from 'zustand'; diff --git a/apps/shinkai-desktop/src/components/sheet/data-table-cell.tsx b/apps/shinkai-desktop/src/components/sheet/data-table-cell.tsx index 681a303c8..e13cddf6a 100644 --- a/apps/shinkai-desktop/src/components/sheet/data-table-cell.tsx +++ b/apps/shinkai-desktop/src/components/sheet/data-table-cell.tsx @@ -386,8 +386,8 @@ export const VectorFsScopeDrawer = ({ const [nodes, setNodes] = useState([]); const auth = useAuth((state) => state.auth); - const { data: fileInfoArray, isSuccess: isVRFilesSuccess } = useGetVRPathSimplified( - { + const { data: fileInfoArray, isSuccess: isVRFilesSuccess } = + useGetVRPathSimplified({ nodeAddress: auth?.node_address ?? '', profile: auth?.profile ?? '', shinkaiIdentity: auth?.shinkai_identity ?? '', @@ -397,8 +397,7 @@ export const VectorFsScopeDrawer = ({ node_encryption_pk: auth?.node_encryption_pk ?? '', profile_encryption_sk: auth?.profile_encryption_sk ?? '', profile_identity_sk: auth?.profile_identity_sk ?? '', - }, - ); + }); useEffect(() => { if (isVRFilesSuccess) { @@ -482,6 +481,7 @@ const FileUploadButton = ({ onDrop: async (acceptedFiles) => { if (!auth || !fileInboxId) return; try { + // TODO: we need a jobId to upload files in shinkai sheet await addFileToInbox(auth.node_address, auth.api_v2_key, { file: acceptedFiles[0], filename: encodeURIComponent(acceptedFiles[0].name), diff --git a/apps/shinkai-desktop/src/components/vector-fs/components/all-files-tab.tsx b/apps/shinkai-desktop/src/components/vector-fs/components/all-files-tab.tsx index a8ca2902b..1dc62a3ad 100644 --- a/apps/shinkai-desktop/src/components/vector-fs/components/all-files-tab.tsx +++ b/apps/shinkai-desktop/src/components/vector-fs/components/all-files-tab.tsx @@ -492,8 +492,10 @@ const AllFiles = () => { } else { navigate('/inboxes', { state: { - selectedVRFiles: selectedFiles, - selectedVRFolders: selectedFolders, + selectedVRFiles: selectedFiles.map((file) => file.path), + selectedVRFolders: selectedFolders.map( + (folder) => folder.path, + ), }, }); } diff --git a/apps/shinkai-desktop/src/components/vector-fs/components/vector-fs-folder.tsx b/apps/shinkai-desktop/src/components/vector-fs/components/vector-fs-folder.tsx index 4407b54b0..64c994fe3 100644 --- a/apps/shinkai-desktop/src/components/vector-fs/components/vector-fs-folder.tsx +++ b/apps/shinkai-desktop/src/components/vector-fs/components/vector-fs-folder.tsx @@ -92,7 +92,8 @@ const VectorFsFolder = ({ layout === VectorFSLayout.Grid && 'bg-gray-400/30 p-2', ); const totalItem = - (folder.child_folders?.length ?? 0) + (folder.child_items?.length ?? 0); + // (folder.child_folders?.length ?? 0) + (folder.child_items?.length ?? 0); + 0; const FolderIcon = isSharedFolder ? SharedFolderIcon : DirectoryTypeIcon; @@ -144,7 +145,7 @@ const VectorFsFolder = ({ event.stopPropagation(); navigate('/inboxes', { state: { - selectedVRFolders: [folder], + selectedVRFolders: [folder.path], }, }); }} diff --git a/apps/shinkai-desktop/src/components/vector-fs/components/vector-fs-item.tsx b/apps/shinkai-desktop/src/components/vector-fs/components/vector-fs-item.tsx index 3c7cefedc..e9b26749d 100644 --- a/apps/shinkai-desktop/src/components/vector-fs/components/vector-fs-item.tsx +++ b/apps/shinkai-desktop/src/components/vector-fs/components/vector-fs-item.tsx @@ -77,7 +77,9 @@ const VectorFsItem = ({ layout === VectorFSLayout.Grid && 'bg-gray-400/30 p-2', ); - const createdDatetime = formatDateToUSLocaleString(new Date(file.created_time)); + const createdDatetime = formatDateToUSLocaleString( + new Date(file.created_time), + ); // const fileSize = size(file.size); if (isVRSelectionActive) { @@ -94,12 +96,7 @@ const VectorFsItem = ({ className="flex flex-1 items-center gap-3" htmlFor={`item-${file.path}`} > - {/* */} + - {/* */} + { + const fileData = await payload.file.arrayBuffer(); + + const formData = new FormData(); + formData.append('job_id', payload.job_id); + formData.append('filename', payload.filename); + formData.append('file_data', new Blob([fileData])); + + const response = await httpClient.post( + urlJoin(nodeAddress, '/v2/upload_file_to_job'), + formData, + { + headers: { Authorization: `Bearer ${bearerToken}` }, + responseType: 'json', + }, + ); + + return response.data as AddFileToInboxResponse; +}; + +export const uploadFilesToJob = async ( + nodeAddress: string, + bearerToken: string, + jobId: string, files: File[], ) => { - const folderId = await createFilesInbox(nodeAddress, bearerToken); for (const file of files) { - await addFileToInbox(nodeAddress, bearerToken, { + await addFileToJob(nodeAddress, bearerToken, { filename: encodeURIComponent(file.name), - file_inbox_name: folderId, + job_id: jobId, file, }); } - return folderId; }; export const downloadFileFromInbox = async ( diff --git a/libs/shinkai-message-ts/src/api/jobs/types.ts b/libs/shinkai-message-ts/src/api/jobs/types.ts index 20a04e121..272b88fca 100644 --- a/libs/shinkai-message-ts/src/api/jobs/types.ts +++ b/libs/shinkai-message-ts/src/api/jobs/types.ts @@ -1,4 +1,3 @@ -import { JobScope } from '../../models/SchemaTypes'; import { AgentInbox } from '../../models/ShinkaiMessage'; type ResourceSource = { @@ -69,16 +68,23 @@ export type VRItemScope = Pick & { source: ResourceSource; }; +export enum VectorSearchMode { + FillUpTo25k = 'FillUpTo25k', + MergeSiblings = 'MergeSiblings', +} + +export type ShinkaiPath = string; + +export type JobScope = { + vector_fs_items: ShinkaiPath[]; + vector_fs_folders: ShinkaiPath[]; + vector_search_mode?: VectorSearchMode[]; +}; + export type CreateJobRequest = { llm_provider: string; job_creation_info: { - scope: { - network_folders: []; - vector_fs_folders: VRFolderScope[]; - vector_fs_items: VRItemScope[]; - local_vrpack: []; - local_vrkai: []; - }; + scope: JobScope; associated_ui: | 'Playground' | { @@ -96,9 +102,9 @@ export type JobMessageRequest = { job_message: { job_id: string; content: string; - files_inbox: string; parent: string | null; tool_key?: string; + files?: string[]; }; }; export type JobMessageResponse = { @@ -308,3 +314,9 @@ export type RetryMessageRequest = { export type RemoveJobRequest = { job_id: string; }; +export type AddFileToJobRequest = { + job_id: string; + filename: string; + file: File; +}; +export type AddFileToJobResponse = string; diff --git a/libs/shinkai-message-ts/src/api/methods.ts b/libs/shinkai-message-ts/src/api/methods.ts index e6743fe81..312f31a31 100644 --- a/libs/shinkai-message-ts/src/api/methods.ts +++ b/libs/shinkai-message-ts/src/api/methods.ts @@ -148,7 +148,7 @@ export const sendTextMessageWithFilesToJob = async ( nodeAddress, bearerToken, job_id, - files + files, ); if (uploadResponse.status !== 'success') { @@ -171,9 +171,9 @@ export const sendTextMessageWithFilesToJob = async ( { headers: { 'Content-Type': 'application/json', - 'Authorization': `Bearer ${bearerToken}`, + Authorization: `Bearer ${bearerToken}`, }, - } + }, ); const data = response.data; @@ -505,7 +505,16 @@ export const retrieveVRPathSimplified = async ( receiver_subidentity: string, path = '/', setupDetailsState: CredentialsPayload, -): Promise<{ data: any; status: string }> => { +): Promise<{ + data: { + created_time: string; + has_embeddings: boolean; + is_directory: boolean; + modified_time: string; + path: string; + }[]; + status: string; +}> => { const messageStr = ShinkaiMessageBuilderWrapper.retrievePathSimplified( setupDetailsState.profile_encryption_sk, setupDetailsState.profile_identity_sk, @@ -595,9 +604,9 @@ export const uploadFilesToVR = async ( { headers: { 'Content-Type': 'multipart/form-data', - 'Authorization': `Bearer ${bearerToken}`, + Authorization: `Bearer ${bearerToken}`, }, - } + }, ); if (response.status !== 200) { @@ -1688,9 +1697,9 @@ export const uploadFilesToJob = async ( { headers: { 'Content-Type': 'multipart/form-data', - 'Authorization': `Bearer ${bearerToken}`, + Authorization: `Bearer ${bearerToken}`, }, - } + }, ); if (response.status !== 200) { diff --git a/libs/shinkai-message-ts/src/api/recurring-tasks/types.ts b/libs/shinkai-message-ts/src/api/recurring-tasks/types.ts index 3d8053d0b..0d27e8acc 100644 --- a/libs/shinkai-message-ts/src/api/recurring-tasks/types.ts +++ b/libs/shinkai-message-ts/src/api/recurring-tasks/types.ts @@ -1,9 +1,4 @@ -import { - JobConfig, - JobMessage, - VRFolderScope, - VRItemScope, -} from '../jobs/types'; +import { JobConfig, JobMessage } from '../jobs/types'; export type RecurringTaskAction = | { @@ -19,11 +14,8 @@ export type RecurringTaskAction = llm_provider: string; job_creation_info: { scope: { - network_folders: []; - vector_fs_folders: VRFolderScope[]; - vector_fs_items: VRItemScope[]; - local_vrpack: []; - local_vrkai: []; + vector_fs_folders: string[]; + vector_fs_items: string[]; }; is_hidden?: boolean; associated_ui?: null; diff --git a/libs/shinkai-message-ts/src/models/SchemaTypes.ts b/libs/shinkai-message-ts/src/models/SchemaTypes.ts index c28107509..5bb30f10a 100644 --- a/libs/shinkai-message-ts/src/models/SchemaTypes.ts +++ b/libs/shinkai-message-ts/src/models/SchemaTypes.ts @@ -67,28 +67,6 @@ export enum MessageSchemaType { SearchShinkaiTool = 'SearchShinkaiTool', } -export enum VectorSearchMode { - FillUpTo25k = 'FillUpTo25k', - MergeSiblings = 'MergeSiblings' -} - -export type ShinkaiPath = string; - -export interface JobScope { - vector_fs_items: ShinkaiPath[]; - vector_fs_folders: ShinkaiPath[]; - vector_search_mode: VectorSearchMode[]; -} - -export interface JobCreation { - scope: JobScope; -} - -export interface JobMessage { - job_id: string; - content: string; -} - export interface JobToolCall { tool_id: string; inputs: Record; diff --git a/libs/shinkai-message-ts/src/models/ShinkaiMessage.ts b/libs/shinkai-message-ts/src/models/ShinkaiMessage.ts index 6835768e3..7321f2352 100644 --- a/libs/shinkai-message-ts/src/models/ShinkaiMessage.ts +++ b/libs/shinkai-message-ts/src/models/ShinkaiMessage.ts @@ -1,4 +1,4 @@ -import { JobScope, MessageSchemaType, TSEncryptionMethod } from './SchemaTypes'; +import { MessageSchemaType, TSEncryptionMethod } from './SchemaTypes'; export interface InternalMetadata { sender_subidentity: string; @@ -69,16 +69,6 @@ export type AgentInbox = { type: 'Agent' | 'LLMProvider'; }; -export type SmartInbox = { - custom_name: string; - inbox_id: string; - last_message?: ShinkaiMessage; - is_finished: boolean; - job_scope?: JobScope; - agent?: AgentInbox; - datetime_created: string; -}; - export type CreateChatInboxResponse = { status: string; message: string; diff --git a/libs/shinkai-node-state/src/v2/mutations/createJob/index.ts b/libs/shinkai-node-state/src/v2/mutations/createJob/index.ts index 853774576..29b976019 100644 --- a/libs/shinkai-node-state/src/v2/mutations/createJob/index.ts +++ b/libs/shinkai-node-state/src/v2/mutations/createJob/index.ts @@ -2,7 +2,7 @@ import { createJob as createJobApi, sendMessageToJob, updateChatConfig, - uploadFilesToInbox, + uploadFilesToJob, } from '@shinkai_network/shinkai-message-ts/api/jobs/index'; import { CreateJobInput } from './types'; @@ -26,9 +26,6 @@ export const createJob = async ({ scope: { vector_fs_items: selectedVRFiles ?? [], vector_fs_folders: selectedVRFolders ?? [], - local_vrpack: [], - local_vrkai: [], - network_folders: [], }, associated_ui: sheetId ? { Sheet: sheetId } : null, is_hidden: isHidden, @@ -42,16 +39,14 @@ export const createJob = async ({ }); } - let folderId = ''; if (files && files.length > 0) { - folderId = await uploadFilesToInbox(nodeAddress, token, files); + await uploadFilesToJob(nodeAddress, token, jobId, files); } await sendMessageToJob(nodeAddress, token, { job_message: { content, job_id: jobId, - files_inbox: folderId, parent: '', tool_key: toolKey, }, diff --git a/libs/shinkai-node-state/src/v2/mutations/createJob/types.ts b/libs/shinkai-node-state/src/v2/mutations/createJob/types.ts index 4c2665355..e8999b97b 100644 --- a/libs/shinkai-node-state/src/v2/mutations/createJob/types.ts +++ b/libs/shinkai-node-state/src/v2/mutations/createJob/types.ts @@ -1,6 +1,8 @@ import { Token } from '@shinkai_network/shinkai-message-ts/api/general/types'; -import { JobConfig } from '@shinkai_network/shinkai-message-ts/api/jobs/types'; -import { ShinkaiPath } from '@shinkai_network/shinkai-message-ts/models'; +import { + JobConfig, + ShinkaiPath, +} from '@shinkai_network/shinkai-message-ts/api/jobs/types'; export type CreateJobInput = Token & { nodeAddress: string; diff --git a/libs/shinkai-node-state/src/v2/mutations/createRecurringTask/index.ts b/libs/shinkai-node-state/src/v2/mutations/createRecurringTask/index.ts index f603525aa..b9c986379 100644 --- a/libs/shinkai-node-state/src/v2/mutations/createRecurringTask/index.ts +++ b/libs/shinkai-node-state/src/v2/mutations/createRecurringTask/index.ts @@ -23,9 +23,6 @@ export const createRecurringTask = async ({ scope: { vector_fs_items: [], vector_fs_folders: [], - local_vrpack: [], - local_vrkai: [], - network_folders: [], }, associated_ui: null, is_hidden: true, @@ -43,9 +40,6 @@ export const createRecurringTask = async ({ scope: { vector_fs_folders: [], vector_fs_items: [], - local_vrpack: [], - local_vrkai: [], - network_folders: [], }, }, llm_provider: llmProvider, diff --git a/libs/shinkai-node-state/src/v2/mutations/sendMessageToJob/index.ts b/libs/shinkai-node-state/src/v2/mutations/sendMessageToJob/index.ts index 0d68f9ad0..a259c7ee4 100644 --- a/libs/shinkai-node-state/src/v2/mutations/sendMessageToJob/index.ts +++ b/libs/shinkai-node-state/src/v2/mutations/sendMessageToJob/index.ts @@ -1,6 +1,6 @@ import { sendMessageToJob as sendMessageToJobApi, - uploadFilesToInbox, + uploadFilesToJob, } from '@shinkai_network/shinkai-message-ts/api/jobs/index'; import { SendMessageToJobInput } from './types'; @@ -14,18 +14,17 @@ export const sendMessageToJob = async ({ files, toolKey, }: SendMessageToJobInput) => { - let folderId = ''; if (files && files.length > 0) { - folderId = await uploadFilesToInbox(nodeAddress, token, files); + await uploadFilesToJob(nodeAddress, token, jobId, files); } return await sendMessageToJobApi(nodeAddress, token, { job_message: { content: message, job_id: jobId, - files_inbox: folderId, parent: parent, tool_key: toolKey, + files: [], }, }); }; diff --git a/libs/shinkai-node-state/src/v2/mutations/updateJobScope/types.ts b/libs/shinkai-node-state/src/v2/mutations/updateJobScope/types.ts index c9333b723..d32be02ab 100644 --- a/libs/shinkai-node-state/src/v2/mutations/updateJobScope/types.ts +++ b/libs/shinkai-node-state/src/v2/mutations/updateJobScope/types.ts @@ -1,6 +1,8 @@ import { Token } from '@shinkai_network/shinkai-message-ts/api/general/types'; -import { UpdateChatConfigResponse } from '@shinkai_network/shinkai-message-ts/api/jobs/types'; -import { JobScope } from '@shinkai_network/shinkai-message-ts/models/SchemaTypes'; +import { + JobScope, + UpdateChatConfigResponse, +} from '@shinkai_network/shinkai-message-ts/api/jobs/types'; export type UpdateChatConfigOutput = UpdateChatConfigResponse; diff --git a/libs/shinkai-node-state/src/v2/queries/getChatConversation/index.ts b/libs/shinkai-node-state/src/v2/queries/getChatConversation/index.ts index 957695737..e8fc70139 100644 --- a/libs/shinkai-node-state/src/v2/queries/getChatConversation/index.ts +++ b/libs/shinkai-node-state/src/v2/queries/getChatConversation/index.ts @@ -28,6 +28,7 @@ const createUserMessage = async ( const attachments: UserMessage['attachments'] = []; if (inbox) { + // TODO: list files from jobId, inboxes no longer exist const fileNames = await getFileNames(nodeAddress, token, { inboxName: inbox, }); @@ -42,6 +43,7 @@ const createUserMessage = async ( if (name.match(/\.(jpg|jpeg|png|gif)$/i)) { try { + // TODO: download file from jobId, inboxes no longer exist const response = await downloadFileFromInbox( nodeAddress, token, diff --git a/libs/shinkai-node-state/src/v2/queries/getJobScope/types.ts b/libs/shinkai-node-state/src/v2/queries/getJobScope/types.ts index 31ee8b3b1..26262ecaf 100644 --- a/libs/shinkai-node-state/src/v2/queries/getJobScope/types.ts +++ b/libs/shinkai-node-state/src/v2/queries/getJobScope/types.ts @@ -1,5 +1,5 @@ import { Token } from '@shinkai_network/shinkai-message-ts/api/general/types'; -import { JobScope } from '@shinkai_network/shinkai-message-ts/models/SchemaTypes'; +import { JobScope } from '@shinkai_network/shinkai-message-ts/api/jobs/types'; export type GetJobScopeInput = Token & { nodeAddress: string;