Skip to content

Commit

Permalink
feat: edit cron tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
paulclindo committed Dec 16, 2024
1 parent 42a6ee1 commit 4e24f6f
Show file tree
Hide file tree
Showing 8 changed files with 763 additions and 532 deletions.
622 changes: 622 additions & 0 deletions apps/shinkai-desktop/src/components/cron-task/component/cron-task.tsx

Large diffs are not rendered by default.

532 changes: 3 additions & 529 deletions apps/shinkai-desktop/src/pages/create-task.tsx

Large diffs are not rendered by default.

35 changes: 35 additions & 0 deletions apps/shinkai-desktop/src/pages/edit-task.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { useGetRecurringTask } from '@shinkai_network/shinkai-node-state/v2/queries/getRecurringTask/useGetRecurringTask';
import { useParams } from 'react-router-dom';

import CronTask from '../components/cron-task/component/cron-task';
import { useAuth } from '../store/auth';

export default function EditTaskPage() {
const auth = useAuth((state) => state.auth);
const { taskId } = useParams();
const { data: task, isSuccess: isGetRecurringTaskSuccess } =
useGetRecurringTask({
nodeAddress: auth?.node_address ?? '',
token: auth?.api_v2_key ?? '',
recurringTaskId: taskId ?? '',
});

return (
<CronTask
initialValues={
isGetRecurringTaskSuccess
? {
action: task.action,
cron: task.cron,
task_id: task.task_id,
created_at: task.created_at,
last_modified: task.last_modified,
description: task.description,
name: task.name,
}
: undefined
}
mode="edit"
/>
);
}
3 changes: 2 additions & 1 deletion apps/shinkai-desktop/src/routes/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import CreateChatPage from '../pages/create-chat';
import CreateTaskPage from '../pages/create-task';
import CreateToolPage from '../pages/create-tool';
import CryptoWalletPage from '../pages/crypto-wallet';
import EditTaskPage from '../pages/edit-task';
import EditToolPage from '../pages/edit-tool';
import { ExportConnection } from '../pages/export-connection';
import FreeSubscriptionsPage from '../pages/free-subscription';
Expand Down Expand Up @@ -328,7 +329,7 @@ const AppRoutes = () => {
<Route element={<Tasks />} index />
<Route element={<TaskLogs />} path={':taskId'} />
<Route element={<CreateTaskPage />} path={'create'} />
<Route element={<EditToolPage />} path={'edit/:toolRouterKey'} />
<Route element={<EditTaskPage />} path={'edit/:taskId'} />
</Route>
<Route
element={
Expand Down
4 changes: 2 additions & 2 deletions libs/shinkai-message-ts/src/api/recurring-tasks/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export type CreateRecurringTaskRequest = {
action: RecurringTaskAction;
};

export type CreateRecurringTaskResponse = Record<string, never>;
export type CreateRecurringTaskResponse = RecurringTask;

export type RecurringTask = {
task_id: number;
Expand All @@ -57,7 +57,7 @@ export type GetRecurringTaskRequest = {
export type GetRecurringTaskResponse = RecurringTask;
export type SetRecurringTaskRequest = {
cron_task_id: string;
} & Omit<RecurringTask, 'task_id'>;
} & Omit<RecurringTask, 'task_id' | 'created_at' | 'last_modified'>;

export type SetRecurringTaskResponse = RecurringTask;
export type RemoveRecurringTaskRequest = {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { setRecurringTask as setRecurringTaskApi } from '@shinkai_network/shinkai-message-ts/api/recurring-tasks/index';

import { UpdateRecurringTaskInput } from './types';

export const updateRecurringTask = async ({
nodeAddress,
token,
name,
description,
llmProvider,
cronExpression,
chatConfig,
message,
toolRouterKey,
taskId,
jobId,
}: UpdateRecurringTaskInput) => {
const response = await setRecurringTaskApi(nodeAddress, token, {
cron_task_id: taskId,
name: name,
description: description,
cron: cronExpression,
action: {
CreateJobWithConfigAndMessage: {
config: chatConfig,
job_creation_info: {
associated_ui: null,
is_hidden: false,
scope: {
vector_fs_folders: [],
vector_fs_items: [],
local_vrpack: [],
local_vrkai: [],
network_folders: [],
},
},
message: {
job_id: jobId,
content: message,
tool_router_key: toolRouterKey,

files_inbox: '',
},
},
},
});
return response;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Token } from '@shinkai_network/shinkai-message-ts/api/general/types';
import { JobConfig } from '@shinkai_network/shinkai-message-ts/api/jobs/types';
import { CreateRecurringTaskResponse } from '@shinkai_network/shinkai-message-ts/api/recurring-tasks/types';

export type UpdateRecurringTaskOutput = CreateRecurringTaskResponse;

export type UpdateRecurringTaskInput = Token & {
nodeAddress: string;
name: string;
description?: string;
taskId: string;
jobId: string;
cronExpression: string;
message: string;
toolRouterKey?: string;
llmProvider: string;
chatConfig: JobConfig;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import {
useMutation,
type UseMutationOptions,
useQueryClient,
} from '@tanstack/react-query';

import { FunctionKeyV2 } from '../../constants';
import { APIError } from '../../types';
import { updateRecurringTask } from './index';
import { UpdateRecurringTaskInput, UpdateRecurringTaskOutput } from './types';

type Options = UseMutationOptions<
UpdateRecurringTaskOutput,
APIError,
UpdateRecurringTaskInput
>;

export const useUpdateRecurringTask = (options?: Options) => {
const queryClient = useQueryClient();
return useMutation({
mutationFn: updateRecurringTask,
...options,
onSuccess: (response, variables, context) => {
queryClient.invalidateQueries({
queryKey: [FunctionKeyV2.GET_RECURRING_TASKS],
});

if (options?.onSuccess) {
options.onSuccess(response, variables, context);
}
},
});
};

0 comments on commit 4e24f6f

Please sign in to comment.