n8n-nodes-coze
Version:
n8n node for Coze(扣子), supporting AI chat, workflow automation, file processing, and text-to-speech.
218 lines • 8.67 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Coze = void 0;
const workspace_1 = require("./descriptions/workspace");
const chat_1 = require("./descriptions/chat");
const workflow_1 = require("./descriptions/workflow");
const file_1 = require("./descriptions/file");
const audio_1 = require("./descriptions/audio");
const workspace_2 = require("./actions/workspace");
const chat_2 = require("./actions/chat");
const workflow_2 = require("./actions/workflow");
const file_2 = require("./actions/file");
const audio_2 = require("./actions/audio");
class Coze {
constructor() {
this.description = {
displayName: 'Coze',
name: 'coze',
icon: 'file:coze.svg',
group: ['transform'],
version: 1,
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
description: 'Interact with Coze AI platform',
defaults: {
name: 'Coze',
},
inputs: ["main"],
outputs: ["main"],
credentials: [
{
name: 'cozeTokenApi',
required: true,
displayOptions: {
show: {
authentication: ['cozeTokenApi'],
},
},
},
{
name: 'cozeOAuth2Api',
required: true,
displayOptions: {
show: {
authentication: ['cozeOAuth2Api'],
},
},
},
],
requestDefaults: {
baseURL: 'https://api.coze.cn',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
},
properties: [
{
displayName: 'Authentication',
name: 'authentication',
type: 'options',
options: [
{
name: 'Service Token',
value: 'cozeTokenApi',
},
{
name: 'OAuth2',
value: 'cozeOAuth2Api',
},
],
default: 'cozeTokenApi',
},
{
displayName: 'Resource',
name: 'resource',
type: 'options',
noDataExpression: true,
options: [
{
name: 'Audio',
value: 'audio',
},
{
name: 'Chat',
value: 'chat',
},
{
name: 'File',
value: 'file',
},
{
name: 'Workflow',
value: 'workflow',
},
{
name: 'Workspace',
value: 'workspace',
},
],
default: 'workspace',
},
...workspace_1.workspaceProperties,
...chat_1.chatProperties,
...workflow_1.workflowProperties,
...file_1.fileProperties,
...audio_1.audioProperties,
],
};
this.methods = {
loadOptions: {
async getWorkspaces() {
const authentication = this.getCurrentNodeParameter('authentication');
const credentials = await this.getCredentials(authentication);
const options = {
baseURL: credentials.baseUrl,
method: 'GET',
url: `/v1/workspaces`,
json: true,
};
const { code, data } = await this.helpers.requestWithAuthentication.call(this, authentication, options);
if (code === 0 && Array.isArray(data === null || data === void 0 ? void 0 : data.workspaces)) {
return data.workspaces.map((workspace) => ({
name: workspace.name,
value: workspace.id,
}));
}
return [];
},
async getBots() {
const workspaceId = this.getCurrentNodeParameter('workspaceId');
if (!workspaceId) {
return [];
}
const authentication = this.getCurrentNodeParameter('authentication');
const credentials = await this.getCredentials(authentication);
const options = {
baseURL: credentials.baseUrl,
method: 'GET',
url: `/v1/bots`,
qs: {
workspace_id: workspaceId,
},
json: true,
};
const { code, data } = await this.helpers.requestWithAuthentication.call(this, authentication, options);
if (code === 0 && Array.isArray(data === null || data === void 0 ? void 0 : data.items)) {
return data.items.map((bot) => ({
name: bot.name,
value: bot.id,
}));
}
return [];
},
async getVoices() {
const authentication = this.getCurrentNodeParameter('authentication');
const credentials = await this.getCredentials(authentication);
const options = {
baseURL: credentials.baseUrl,
method: 'GET',
url: `/v1/audio/voices`,
json: true,
};
const { code, data } = await this.helpers.requestWithAuthentication.call(this, authentication, options);
if (code === 0 && Array.isArray(data === null || data === void 0 ? void 0 : data.voice_list)) {
return data.voice_list.map((voice) => ({
name: voice.name,
value: voice.voice_id,
}));
}
return [];
},
},
};
}
async execute() {
const items = this.getInputData();
const returnData = [];
const resource = this.getNodeParameter('resource', 0);
for (let i = 0; i < items.length; i++) {
try {
let responseData;
if (resource === 'workspace') {
responseData = await workspace_2.executeWorkspace.call(this, i);
}
else if (resource === 'chat') {
responseData = await chat_2.executeChat.call(this, i);
}
else if (resource === 'workflow') {
responseData = await workflow_2.executeWorkflow.call(this, i);
}
else if (resource === 'file') {
responseData = await file_2.executeFile.call(this, i);
}
else if (resource === 'audio') {
responseData = await audio_2.executeAudio.call(this, i);
}
if (Array.isArray(responseData)) {
returnData.push.apply(returnData, responseData);
}
else {
returnData.push(responseData);
}
}
catch (error) {
if (this.continueOnFail()) {
returnData.push({
error: error.message,
});
continue;
}
throw error;
}
}
return [this.helpers.returnJsonArray(returnData)];
}
}
exports.Coze = Coze;
//# sourceMappingURL=Coze.node.js.map