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

feat: edit cron tasks + view logs #565

Merged
merged 3 commits into from
Dec 16, 2024
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
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"
/>
);
}
46 changes: 24 additions & 22 deletions apps/shinkai-desktop/src/pages/task-logs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { useRemoveRecurringTask } from '@shinkai_network/shinkai-node-state/v2/m
import { useGetRecurringTask } from '@shinkai_network/shinkai-node-state/v2/queries/getRecurringTask/useGetRecurringTask';
import { useGetRecurringTaskLogs } from '@shinkai_network/shinkai-node-state/v2/queries/getRecurringTaskLogs/useGetRecurringTaskLogs';
import {
Badge,
Button,
buttonVariants,
Dialog,
Expand All @@ -20,6 +21,7 @@ import {
} from '@shinkai_network/shinkai-ui';
import { cn } from '@shinkai_network/shinkai-ui/utils';
import cronstrue from 'cronstrue';
import { formatDate } from 'date-fns';
import { Edit, TrashIcon } from 'lucide-react';
import React from 'react';
import { useNavigate, useParams } from 'react-router-dom';
Expand Down Expand Up @@ -51,8 +53,6 @@ export const TaskLogs = () => {
recurringTaskId: taskId ?? '',
});

console.log(logs, 'logs');

return (
<SubpageLayout alignLeft className="px-4" title="Cron Logs">
{isGetRecurringTaskPending && (
Expand Down Expand Up @@ -105,26 +105,28 @@ export const TaskLogs = () => {
</p>
</div>
)}
{isSuccess && logs.length > 0 && (
<div className="p-10 text-center text-sm">
<p className="text-white">Logs found</p>
<p className="text-gray-80 text-xs">TODO: Display logs</p>
</div>
)}
{/*{isSuccess && (*/}
{/* <TaskCard*/}
{/* cronExpression={task.cron}*/}
{/* description={task.description}*/}
{/* key={task.task_id}*/}
{/* name={task.name}*/}
{/* prompt={*/}
{/* 'CreateJobWithConfigAndMessage' in task.action*/}
{/* ? task.action.CreateJobWithConfigAndMessage.message.content*/}
{/* : ''*/}
{/* }*/}
{/* taskId={task.task_id}*/}
{/* />*/}
{/*)}*/}
{isSuccess &&
logs.length > 0 &&
logs.map((log) => (
<div className="flex items-center gap-10" key={log.execution_time}>
<span className="text-gray-80">
{formatDate(
new Date(log.execution_time),
'yyyy-MM-dd HH:mm:ss',
)}
</span>
<Badge
className={cn(
'text-sm',
log.success
? 'bg-green-900 text-green-400'
: 'bg-red-900 text-red-400',
)}
>
{log.success ? 'Success' : 'Failed'}
</Badge>
</div>
))}
</div>
</SubpageLayout>
);
Expand Down
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
3 changes: 2 additions & 1 deletion libs/shinkai-message-ts/src/api/recurring-tasks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
CreateRecurringTaskRequest,
CreateRecurringTaskResponse,
GetRecurringTaskLogsRequest,
GetRecurringTaskLogsResponse,
GetRecurringTaskRequest,
GetRecurringTaskResponse,
GetRecurringTasksResponse,
Expand Down Expand Up @@ -106,5 +107,5 @@ export const getRecurringTaskLogs = async (
responseType: 'json',
},
);
return response.data;
return response.data as GetRecurringTaskLogsResponse;
};
10 changes: 8 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 All @@ -69,3 +69,9 @@ export type RemoveRecurringTaskResponse = {
export type GetRecurringTaskLogsRequest = {
cron_task_id: string;
};
export type GetRecurringTaskLogsResponse = {
task_id: string;
execution_time: string;
success: boolean;
error_message: string;
}[];
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);
}
},
});
};