mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-18 02:21:13 +00:00
feat: Add more telemetry to free AI credits feature (no-changelog) (#12493)
This commit is contained in:
@@ -4,16 +4,20 @@ import {
|
||||
CHAIN_LLM_LANGCHAIN_NODE_TYPE,
|
||||
CHAIN_SUMMARIZATION_LANGCHAIN_NODE_TYPE,
|
||||
EXECUTE_WORKFLOW_NODE_TYPE,
|
||||
FREE_AI_CREDITS_ERROR_TYPE,
|
||||
FREE_AI_CREDITS_USED_ALL_CREDITS_ERROR_CODE,
|
||||
HTTP_REQUEST_NODE_TYPE,
|
||||
HTTP_REQUEST_TOOL_LANGCHAIN_NODE_TYPE,
|
||||
LANGCHAIN_CUSTOM_TOOLS,
|
||||
MERGE_NODE_TYPE,
|
||||
OPEN_AI_API_CREDENTIAL_TYPE,
|
||||
OPENAI_LANGCHAIN_NODE_TYPE,
|
||||
STICKY_NODE_TYPE,
|
||||
WEBHOOK_NODE_TYPE,
|
||||
WORKFLOW_TOOL_LANGCHAIN_NODE_TYPE,
|
||||
} from './Constants';
|
||||
import { ApplicationError } from './errors/application.error';
|
||||
import type { NodeApiError } from './errors/node-api.error';
|
||||
import type {
|
||||
IConnection,
|
||||
INode,
|
||||
@@ -29,6 +33,10 @@ import type {
|
||||
IRun,
|
||||
} from './Interfaces';
|
||||
import { getNodeParameters } from './NodeHelpers';
|
||||
import { jsonParse } from './utils';
|
||||
|
||||
const isNodeApiError = (error: unknown): error is NodeApiError =>
|
||||
typeof error === 'object' && error !== null && 'name' in error && error?.name === 'NodeApiError';
|
||||
|
||||
export function getNodeTypeForName(workflow: IWorkflowBase, nodeName: string): INode | undefined {
|
||||
return workflow.nodes.find((node) => node.name === nodeName);
|
||||
@@ -489,3 +497,31 @@ export function extractLastExecutedNodeCredentialData(
|
||||
|
||||
return { credentialId: id, credentialType };
|
||||
}
|
||||
|
||||
export const userInInstanceRanOutOfFreeAiCredits = (runData: IRun): boolean => {
|
||||
const credentials = extractLastExecutedNodeCredentialData(runData);
|
||||
|
||||
if (!credentials) return false;
|
||||
|
||||
if (credentials.credentialType !== OPEN_AI_API_CREDENTIAL_TYPE) return false;
|
||||
|
||||
const { error } = runData.data.resultData;
|
||||
|
||||
if (!isNodeApiError(error) || !error.messages[0]) return false;
|
||||
|
||||
const rawErrorResponse = error.messages[0].replace(`${error.httpCode} -`, '');
|
||||
|
||||
try {
|
||||
const errorResponse = jsonParse<{ error: { code: number; type: string } }>(rawErrorResponse);
|
||||
if (
|
||||
errorResponse?.error?.type === FREE_AI_CREDITS_ERROR_TYPE &&
|
||||
errorResponse.error.code === FREE_AI_CREDITS_USED_ALL_CREDITS_ERROR_CODE
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user