AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents
TypeScript ai-agentai-agent-toolsai-agentsai-agents-frameworkmcpmcp-servermcp-toolsmcpsn8n-alternativeno-code-automationworkflowworkflow-automationworkflows
import { isNil } from '@activepieces/core-utils';
import {
AIProviderAuthConfig,
AiProviderKeyStatus,
AIProviderWithoutSensitiveData,
CreateAIProviderRequest,
UpdateAIProviderRequest,
} from '@activepieces/shared';
import { useMutation, useQuery } from '@tanstack/react-query';
import { AxiosError } from 'axios';
import { authenticationSession } from '@/lib/authentication-session';
import { aiProviderApi } from '../api/ai-provider-api';
export const aiProviderKeys = {
configs: ['ai-provider-configs'] as const,
forProject: (projectId: string | null) =>
['ai-providers', projectId] as const,
configModels: (configId?: string) =>
configId === undefined
? (['ai-provider-config-models'] as const)
: (['ai-provider-config-models', configId] as const),
};
export const aiProviderQueries = {
useAiProviderConfigs: () =>
useQuery({
queryKey: aiProviderKeys.configs,
queryFn: () => aiProviderApi.listConfigs(),
}),
useConfigModels: (configId: string | undefined) =>
useQuery({
queryKey: aiProviderKeys.configModels(configId),
queryFn: () =>
isNil(configId) ? [] : aiProviderApi.listModelsForConfig(configId),
enabled: !isNil(configId),
}),
useProjectAiProviders: () => {
const projectId = authenticationSession.getProjectId();
return useQuery({
queryKey: aiProviderKeys.forProject(projectId),
queryFn: () =>
isNil(projectId) ? [] : aiProviderApi.listForProject(projectId),
enabled: !isNil(projectId),
});
},
};
export const aiProviderMutations = {
useRecheckAiProvider: ({
onSuccess,
onError,
}: {
onSuccess: (result: { status: AiProviderKeyStatus }) => void;
onError: (
error: AxiosError<{ message?: string; params?: { message: string } }>,
) => void;
}) => {
return useMutation({
mutationFn: (providerId: string) => aiProviderApi.recheck(providerId),
onSuccess,
onError,
});
},
useDeleteAiProvider: ({
onSuccess,
onError,
}: {
onSuccess: () => void;
onError?: (
error: AxiosError<{ message?: string; params?: { message: string } }>,
) => void;
}) => {
return useMutation({
mutationFn: (providerId: string) => aiProviderApi.delete(providerId),
onSuccess,
onError,
});
},
useUpdateAiProvider: ({
onSuccess,
onError,
}: {
onSuccess: () => void;
onError?: (
error: AxiosError<{ message?: string; params?: { message: string } }>,
) => void;
}) => {
return useMutation({
mutationFn: ({
providerId,
request,
}: {
providerId: string;
request: UpdateAIProviderRequest;
}) => aiProviderApi.update(providerId, request),
onSuccess,
onError,
});
},
useUpsertAiProvider: ({
providerId,
onSuccess,
onError,
}: UpsertAiProviderOptions) => {
return useMutation({
mutationFn: async (
data: CreateAIProviderRequest,
): Promise<AIProviderWithoutSensitiveData | undefined> => {
if (providerId) {
const updateData: UpdateAIProviderRequest = {
displayName: data.displayName,
config: data.config,
...(hasAnyAuthFieldFilled(data.auth) ? { auth: data.auth } : {}),
};
await aiProviderApi.update(providerId, updateData);
return undefined;
}
return aiProviderApi.upsert(data);
},
onSuccess,
onError,
});
},
};
export const hasAnyAuthFieldFilled = (
auth: AIProviderAuthConfig | undefined,
): boolean => {
if (!auth) {
return false;
}
return Object.values(auth).some(
(value) => typeof value === 'string' && value.length > 0,
);
};
type UpsertAiProviderOptions = {
providerId?: string;
onSuccess: (created?: AIProviderWithoutSensitiveData) => void;
onError: (
error: AxiosError<{ message?: string; params?: { message: string } }>,
) => void;
};