Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,14 @@ function PlaygroundHeaderBase({
const { mutateAsync: publishTool, isPending: isPublishingTool } =
usePublishTool({
onSuccess: async (response) => {
await open(
`${SHINKAI_STORE_URL}/store/revisions/complete?id=${response.response.revisionId}`,
);
const storeUrl = `${SHINKAI_STORE_URL}/store/revisions/complete?id=${response.response.revisionId}`;
try {
await open(storeUrl);
} catch (error) {
toast.error('Tool published, but failed to open the store', {
description: `${error instanceof Error ? error.message : String(error)}. Please open: ${storeUrl}`,
});
}
},
onError: (error) => {
toast.error('Failed to publish tool', {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,14 @@ export default function ToolDetailsCard({
isSuccess: isPublishToolSuccess,
} = usePublishTool({
onSuccess: async (response) => {
await open(
`${SHINKAI_STORE_URL}/store/revisions/complete?id=${response.response.revisionId}`,
);
const storeUrl = `${SHINKAI_STORE_URL}/store/revisions/complete?id=${response.response.revisionId}`;
try {
await open(storeUrl);
} catch (error) {
toast.error('Tool published, but failed to open the store', {
description: `${error instanceof Error ? error.message : String(error)}. Please open: ${storeUrl}`,
});
}
},
onError: (error) => {
toast.error('Failed to publish tool', {
Expand Down
44 changes: 42 additions & 2 deletions apps/shinkai-desktop/src/pages/agents.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { DotsVerticalIcon } from '@radix-ui/react-icons';
import { useTranslation } from '@shinkai_network/shinkai-i18n';
import { type RecurringTask } from '@shinkai_network/shinkai-message-ts/api/recurring-tasks/types';
import { useExportAgent } from '@shinkai_network/shinkai-node-state/v2/mutations/exportAgent/useExportAgent';
import { usePublishAgent } from '@shinkai_network/shinkai-node-state/v2/mutations/publishAgent/usePublishAgent';
import { useRemoveAgent } from '@shinkai_network/shinkai-node-state/v2/mutations/removeAgent/useRemoveAgent';
import { useGetAgents } from '@shinkai_network/shinkai-node-state/v2/queries/getAgents/useGetAgents';
import {
Expand Down Expand Up @@ -47,8 +48,9 @@ import { cn } from '@shinkai_network/shinkai-ui/utils';
import { save } from '@tauri-apps/plugin-dialog';
import * as fs from '@tauri-apps/plugin-fs';
import { BaseDirectory } from '@tauri-apps/plugin-fs';
import { open } from '@tauri-apps/plugin-shell';
import cronstrue from 'cronstrue';
import { Edit, Plus, TrashIcon } from 'lucide-react';
import { Edit, Plus, Rocket, TrashIcon } from 'lucide-react';
import React, { useMemo, useState } from 'react';
import { Link, useNavigate } from 'react-router';
import { toast } from 'sonner';
Expand All @@ -57,6 +59,7 @@ import ImportAgentModal from '../components/agent/import-agent-modal';
import { useGetStoreAgents } from '../components/store/store-client';

import { useAuth } from '../store/auth';
import { SHINKAI_STORE_URL } from '../utils/store';

function AgentsPage() {
const auth = useAuth((state) => state.auth);
Expand Down Expand Up @@ -260,6 +263,25 @@ const AgentCard = ({
},
});

const { mutateAsync: publishAgent, isPending: isPublishingAgent } =
usePublishAgent({
onSuccess: async (response) => {
const storeUrl = `${SHINKAI_STORE_URL}/store/revisions/complete?id=${response.response.revisionId}`;
try {
await open(storeUrl);
} catch (error) {
toast.error('Agent published, but failed to open the store', {
description: `${error instanceof Error ? error.message : String(error)}. Please open: ${storeUrl}`,
});
}
},
onError: (error) => {
toast.error('Failed to publish agent', {
description: error.response?.data?.message ?? error.message,
});
},
});

const hasScheduledTasks =
scheduledTasks?.length && scheduledTasks?.length > 0;

Expand Down Expand Up @@ -375,6 +397,7 @@ const AgentCard = ({
onClick: () => {
void navigate(`/agents/edit/${agentId}`);
},
disabled: false,
},
{
name: 'Export',
Expand All @@ -386,19 +409,36 @@ const AgentCard = ({
token: auth?.api_v2_key ?? '',
});
},
disabled: false,
},
{
name: t('agents.publishDialog.publish'),
icon: <Rocket className="mr-3 h-4 w-4" />,
onClick: () => {
void publishAgent({
agentId,
nodeAddress: auth?.node_address ?? '',
token: auth?.api_v2_key ?? '',
});
},
disabled: isPublishingAgent,
},
{
name: t('common.delete'),
icon: <TrashIcon className="mr-3 h-4 w-4" />,
onClick: () => {
setIsDeleteAgentDrawerOpen(true);
},
disabled: false,
},
].map((option) => (
<React.Fragment key={option.name}>
{option.name === 'Delete' && <DropdownMenuSeparator />}
{option.name === t('common.delete') && (
<DropdownMenuSeparator />
)}
<DropdownMenuItem
key={option.name}
disabled={option.disabled}
onClick={(event) => {
event.stopPropagation();
option.onClick();
Expand Down
18 changes: 18 additions & 0 deletions libs/shinkai-message-ts/src/api/agents/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
type GetAgentResponse,
type GetAgentsResponse,
type ImportAgentResponse,
type PublishAgentRequest,
type PublishAgentResponse,
type RemoveAgentRequest,
type RemoveAgentResponse,
type UpdateAgentRequest,
Expand Down Expand Up @@ -147,3 +149,19 @@ export const importAgentFromUrl = async (
);
return response.data as ImportAgentResponse;
};

export const publishAgent = async (
nodeAddress: string,
bearerToken: string,
payload: PublishAgentRequest,
) => {
const response = await httpClient.get(
urlJoin(nodeAddress, '/v2/publish_agent'),
{
params: { agent_id: payload.agent_id },
headers: { Authorization: `Bearer ${bearerToken}` },
responseType: 'json',
},
);
return response.data as PublishAgentResponse;
};
14 changes: 14 additions & 0 deletions libs/shinkai-message-ts/src/api/agents/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,17 @@ export type ImportAgentRequest = {
};

export type ImportAgentResponse = Agent;

export type PublishAgentRequest = {
agent_id: string;
};

export type PublishAgentResponse = {
message: string;
response: {
message: string;
revisionId: string;
};
status: string;
agent_id: string;
};
15 changes: 15 additions & 0 deletions libs/shinkai-node-state/src/v2/mutations/publishAgent/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { publishAgent as publishAgentApi } from '@shinkai_network/shinkai-message-ts/api/agents/index';

import { type PublishAgentInput } from './types';

export const publishAgent = async ({
nodeAddress,
token,
agentId,
}: PublishAgentInput) => {
const response = await publishAgentApi(nodeAddress, token, {
agent_id: agentId,
});
return response;
};

10 changes: 10 additions & 0 deletions libs/shinkai-node-state/src/v2/mutations/publishAgent/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { type PublishAgentResponse } from '@shinkai_network/shinkai-message-ts/api/agents/types';
import { type Token } from '@shinkai_network/shinkai-message-ts/api/general/types';

export type PublishAgentOutput = PublishAgentResponse;

export type PublishAgentInput = Token & {
nodeAddress: string;
agentId: string;
};

Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import {
useMutation,
type UseMutationOptions,
useQueryClient,
} from '@tanstack/react-query';

import { FunctionKeyV2 } from '../../constants';
import { type APIError } from '../../types';
import { type PublishAgentInput, type PublishAgentOutput } from './types';
import { publishAgent } from './index';

type Options = UseMutationOptions<
PublishAgentOutput,
APIError,
PublishAgentInput
>;

export const usePublishAgent = (options?: Options) => {
const queryClient = useQueryClient();
return useMutation({
mutationFn: publishAgent,
...options,
onSuccess: async (response, variables, context) => {
await queryClient.invalidateQueries({
queryKey: [FunctionKeyV2.GET_AGENTS],
});

if (options?.onSuccess) {
try {
await options.onSuccess(response, variables, context);
} catch (error) {
console.error(error);
}
}
},
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ export const usePublishTool = (options?: Options) => {
});

if (options?.onSuccess) {
options.onSuccess(response, variables, context);
try {
await options.onSuccess(response, variables, context);
} catch (error) {
console.error(error);
}
}
},
});
Expand Down