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(editor): Show warning when user activates workflow with free AI credits credential (no-changelog) #12510

Merged
merged 2 commits into from
Jan 8, 2025
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
66 changes: 65 additions & 1 deletion packages/editor-ui/src/components/WorkflowActivator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,31 @@ import { useWorkflowsStore } from '@/stores/workflows.store';
import { createTestingPinia } from '@pinia/testing';
import { createComponentRenderer } from '@/__tests__/render';
import { mockedStore } from '@/__tests__/utils';
import { EXECUTE_WORKFLOW_TRIGGER_NODE_TYPE } from '@/constants';
import { EXECUTE_WORKFLOW_TRIGGER_NODE_TYPE, WOOCOMMERCE_TRIGGER_NODE_TYPE } from '@/constants';
import { useCredentialsStore } from '@/stores/credentials.store';
import { useToast } from '@/composables/useToast';

const renderComponent = createComponentRenderer(WorkflowActivator);
let mockWorkflowsStore: ReturnType<typeof mockedStore<typeof useWorkflowsStore>>;
let mockCredentialsStore: ReturnType<typeof mockedStore<typeof useCredentialsStore>>;

vi.mock('@/composables/useToast', () => {
const showMessage = vi.fn();
return {
useToast: () => {
return {
showMessage,
};
},
};
});

describe('WorkflowActivator', () => {
beforeEach(() => {
createTestingPinia();

mockWorkflowsStore = mockedStore(useWorkflowsStore);
mockCredentialsStore = mockedStore(useCredentialsStore);
});

afterEach(() => {
Expand Down Expand Up @@ -79,4 +94,53 @@ describe('WorkflowActivator', () => {
);
expect(getByTestId('workflow-activator-status')).toHaveTextContent('Inactive');
});

it('Should show warning toast if the workflow to be activated has free OpenAI credentials', async () => {
const toast = useToast();

mockWorkflowsStore.workflow.usedCredentials = [
{
id: '1',
name: '',
credentialType: '',
currentUserHasAccess: false,
},
];

mockCredentialsStore.state.credentials = {
'1': {
id: '1',
name: 'OpenAI',
type: 'openAiApi',
data: '',
isManaged: true,
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
},
};

mockWorkflowsStore.workflowTriggerNodes = [
{ type: WOOCOMMERCE_TRIGGER_NODE_TYPE, disabled: false } as never,
];

const { rerender } = renderComponent({
props: {
workflowActive: false,
workflowId: '1',
workflowPermissions: { update: true },
},
});

await rerender({ workflowActive: true });

expect(toast.showMessage).toHaveBeenCalledWith(
expect.objectContaining({
title: "You're using free OpenAI API credits",
message:
'To make sure your workflow runs smoothly in the future, replace the free OpenAI API credits with your own API key.',
type: 'warning',
duration: 0,
}),
);
});
});
27 changes: 26 additions & 1 deletion packages/editor-ui/src/components/WorkflowActivator.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import { useWorkflowActivate } from '@/composables/useWorkflowActivate';
import { useWorkflowsStore } from '@/stores/workflows.store';
import { getActivatableTriggerNodes } from '@/utils/nodeTypesUtils';
import type { VNode } from 'vue';
import { computed, h } from 'vue';
import { computed, h, watch } from 'vue';
import { useI18n } from '@/composables/useI18n';
import type { PermissionsRecord } from '@/permissions';
import { EXECUTE_WORKFLOW_TRIGGER_NODE_TYPE, PLACEHOLDER_EMPTY_WORKFLOW_ID } from '@/constants';
import WorkflowActivationErrorMessage from './WorkflowActivationErrorMessage.vue';
import { useCredentialsStore } from '@/stores/credentials.store';
import type { IUsedCredential } from '@/Interface';

const props = defineProps<{
workflowActive: boolean;
Expand All @@ -20,6 +22,7 @@ const workflowActivate = useWorkflowActivate();

const i18n = useI18n();
const workflowsStore = useWorkflowsStore();
const credentialsStore = useCredentialsStore();

const isWorkflowActive = computed((): boolean => {
const activeWorkflows = workflowsStore.activeWorkflows;
Expand Down Expand Up @@ -69,6 +72,14 @@ const disabled = computed((): boolean => {
return false;
});

const currentWorkflowHasFreeAiCredits = computed((): boolean => {
if (!workflowsStore?.workflow?.usedCredentials) return false;
return workflowsStore.workflow.usedCredentials.some(
(usedCredential: IUsedCredential) =>
credentialsStore.state.credentials[usedCredential.id].isManaged,
);
});

async function activeChanged(newActiveState: boolean) {
return await workflowActivate.updateWorkflowActivation(props.workflowId, newActiveState);
}
Expand Down Expand Up @@ -100,6 +111,20 @@ async function displayActivationError() {
duration: 0,
});
}

watch(
() => props.workflowActive,
(workflowActive) => {
RicardoE105 marked this conversation as resolved.
Show resolved Hide resolved
if (workflowActive && currentWorkflowHasFreeAiCredits.value) {
showMessage({
title: i18n.baseText('freeAi.credits.showWarning.workflow.activation.title'),
message: i18n.baseText('freeAi.credits.showWarning.workflow.activation.description'),
type: 'warning',
duration: 0,
});
}
},
);
</script>

<template>
Expand Down
4 changes: 3 additions & 1 deletion packages/editor-ui/src/plugins/i18n/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -2849,5 +2849,7 @@
"freeAi.credits.callout.success.title.part2": "gpt-4o-mini, text-embedding-3-small, dall-e-3, tts-1, whisper-1, and text-moderation-latest",
"freeAi.credits.credentials.edit": "This is a managed credential and cannot be edited.",
"freeAi.credits.showError.claim.title": "Free AI credits",
"freeAi.credits.showError.claim.message": "Enable to claim credits"
"freeAi.credits.showError.claim.message": "Enable to claim credits",
"freeAi.credits.showWarning.workflow.activation.title": "You're using free OpenAI API credits",
"freeAi.credits.showWarning.workflow.activation.description": "To make sure your workflow runs smoothly in the future, replace the free OpenAI API credits with your own API key."
}
Loading