n8n-nodes-difyai
Version:
Community node for Dify API
330 lines • 14.4 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.DifyAI = void 0;
const n8n_workflow_1 = require("n8n-workflow");
class DifyAI {
constructor() {
this.description = {
displayName: 'DifyAI',
name: 'difyAi',
icon: 'file:difyLogo.svg',
group: ['transform'],
version: 1,
description: 'Consume Dify API',
defaults: {
name: 'DifyAI',
},
inputs: ['main'],
outputs: ['main'],
credentials: [
{
name: 'difyApi',
required: true,
},
],
properties: [
{
displayName: 'Operation',
name: 'operation',
type: 'options',
noDataExpression: true,
options: [
{
name: 'Send Chat Message',
value: 'chatMessage',
description: 'Send a chat message request',
action: 'Send a chat message request',
},
{
name: 'Audio to Text',
value: 'audioToText',
description: 'Convert an audio file to text',
action: 'Convert an audio file to text',
},
{
name: 'File Upload',
value: 'fileUpload',
description: 'Upload a file to the server',
action: 'Upload a file to the server',
},
],
default: 'chatMessage',
},
{
displayName: 'User ID',
name: 'user',
type: 'string',
default: 'abc-123',
description: 'Unique identifier for the user',
},
{
displayName: 'Query',
name: 'query',
type: 'string',
default: '',
description: 'User input/question content',
displayOptions: {
show: {
operation: ['chatMessage'],
},
},
},
{
displayName: 'Conversation ID',
name: 'conversationId',
type: 'string',
default: '',
description: 'Conversation ID to continue the conversation',
displayOptions: {
show: {
operation: ['chatMessage'],
},
},
},
{
displayName: 'Response Mode',
name: 'responseMode',
type: 'options',
options: [
{ name: 'Streaming', value: 'streaming' },
{ name: 'Blocking', value: 'blocking' },
],
default: 'blocking',
description: 'The mode of response from the API',
displayOptions: {
show: {
operation: ['chatMessage'],
},
},
},
{
displayName: 'Files',
name: 'files',
type: 'fixedCollection',
placeholder: 'Add File',
typeOptions: {
multipleValues: true,
},
default: {},
displayOptions: {
show: {
operation: ['chatMessage'],
},
},
options: [
{
name: 'fileProperties',
displayName: 'File',
values: [
{
displayName: 'Type',
name: 'type',
type: 'options',
options: [
{
name: 'Audio',
value: 'audio',
description: 'MP3, M4A, WAV, WEBM, AMR',
},
{
name: 'Custom',
value: 'custom',
description: 'Other file types',
},
{
name: 'Document',
value: 'document',
description: "TXT, MD, MARKDOWN, PDF, HTML, XLSX, XLS, DOCX, CSV, EML, MSG, PPTX, PPT, XML, EPUB",
},
{
name: 'Image',
value: 'image',
description: 'JPG, JPEG, PNG, GIF, WEBP, SVG',
},
{
name: 'Video',
value: 'video',
description: 'MP4, MOV, MPEG, MPGA',
},
],
default: 'image',
},
{
displayName: 'Transfer Method',
name: 'transfer_method',
type: 'options',
options: [
{
name: 'Remote URL',
value: 'remote_url',
},
{
name: 'Local File',
value: 'local_file',
},
],
default: 'remote_url',
},
{
displayName: 'URL',
name: 'url',
type: 'string',
default: '',
description: 'Image URL (when transfer method is remote_url)',
displayOptions: {
show: {
transfer_method: ['remote_url'],
},
},
},
{
displayName: 'Upload File ID',
name: 'upload_file_id',
type: 'string',
default: '',
description: 'Uploaded file ID (when transfer method is local_file)',
displayOptions: {
show: {
transfer_method: ['local_file'],
},
},
},
],
},
],
},
{
displayName: 'Binary Property',
name: 'binaryPropertyName',
type: 'string',
default: 'data',
description: 'Name of the binary property which contains the file',
displayOptions: {
show: {
operation: ['audioToText', 'fileUpload'],
},
},
},
],
};
}
async execute() {
const items = this.getInputData();
const returnData = [];
const credentials = await this.getCredentials('difyApi');
let baseUrl = credentials.baseUrl;
if (baseUrl.startsWith('http://')) {
baseUrl = baseUrl.replace('http://', 'https://');
}
if (baseUrl.endsWith('/')) {
baseUrl = baseUrl.slice(0, -1);
}
if (baseUrl.includes('/v1')) {
baseUrl = baseUrl.split('/v1')[0];
}
baseUrl = `${baseUrl}/v1`;
const operation = this.getNodeParameter('operation', 0);
for (let i = 0; i < items.length; i++) {
let responseData;
if (operation === 'chatMessage') {
const query = this.getNodeParameter('query', i);
const user = this.getNodeParameter('user', i);
const conversationId = this.getNodeParameter('conversationId', i);
const responseMode = this.getNodeParameter('responseMode', i);
const filesData = this.getNodeParameter('files', i);
let files = undefined;
if (filesData &&
filesData.fileProperties &&
Array.isArray(filesData.fileProperties) &&
filesData.fileProperties.length > 0) {
files = filesData.fileProperties;
}
const body = {
query,
inputs: {},
user,
conversation_id: conversationId || undefined,
response_mode: responseMode,
files,
};
const options = {
method: 'POST',
uri: `${baseUrl}/chat-messages`,
headers: {
Authorization: `Bearer ${credentials.apiKey}`,
'Content-Type': 'application/json',
},
body,
json: true,
};
responseData = await this.helpers.request(options);
}
else if (operation === 'audioToText') {
const user = this.getNodeParameter('user', i);
const binaryPropertyName = this.getNodeParameter('binaryPropertyName', i);
const item = items[i];
if (!item.binary || !item.binary[binaryPropertyName]) {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), `No binary data property "${binaryPropertyName}" on item!`);
}
const binaryData = item.binary[binaryPropertyName];
const fileBuffer = await this.helpers.getBinaryDataBuffer(i, binaryPropertyName);
const fileName = binaryData.fileName || 'audioFile';
const mimeType = binaryData.mimeType || 'application/octet-stream';
const options = {
method: 'POST',
uri: `${baseUrl}/audio-to-text`,
headers: {
Authorization: `Bearer ${credentials.apiKey}`,
},
formData: {
file: {
value: fileBuffer,
options: {
filename: fileName,
contentType: mimeType,
},
},
user,
},
json: true,
};
responseData = await this.helpers.request(options);
}
else if (operation === 'fileUpload') {
const user = this.getNodeParameter('user', i);
const binaryPropertyName = this.getNodeParameter('binaryPropertyName', i);
const item = items[i];
if (!item.binary || !item.binary[binaryPropertyName]) {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), `No binary data property "${binaryPropertyName}" on item!`);
}
const binaryData = item.binary[binaryPropertyName];
const fileBuffer = await this.helpers.getBinaryDataBuffer(i, binaryPropertyName);
const fileName = binaryData.fileName || 'uploadFile';
const mimeType = binaryData.mimeType || 'application/octet-stream';
const options = {
method: 'POST',
uri: `${baseUrl}/files/upload`,
headers: {
Authorization: `Bearer ${credentials.apiKey}`,
},
formData: {
file: {
value: fileBuffer,
options: {
filename: fileName,
contentType: mimeType,
},
},
user,
},
json: true,
};
responseData = await this.helpers.request(options);
}
returnData.push({ json: responseData });
}
return [returnData];
}
}
exports.DifyAI = DifyAI;
//# sourceMappingURL=DifyAi.node.js.map