n8n-nodes-harmony-ai 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -30,6 +30,8 @@ npm run build
30
30
 
31
31
  `shared/harmony-public-api.gen.ts` holds types generated by `@hey-api/openapi-ts` from Harmony's public OpenAPI spec. Replace it when the public API changes.
32
32
 
33
+ Releases publish from the Publish workflow through npm trusted publishing, with provenance. Bump `version` in `package.json` and `HARMONY_PACKAGE_VERSION` in `shared/harmony-api.constants.ts` first.
34
+
33
35
  ## License
34
36
 
35
37
  [MIT](LICENSE)
@@ -2,10 +2,14 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.HarmonyAi = void 0;
4
4
  const n8n_workflow_1 = require("n8n-workflow");
5
+ const bulk_enrollment_util_ts_1 = require("../../shared/bulk-enrollment.util.js");
5
6
  const contact_fields_util_ts_1 = require("../../shared/contact-fields.util.js");
7
+ const custom_api_call_util_ts_1 = require("../../shared/custom-api-call.util.js");
6
8
  const harmony_api_constants_ts_1 = require("../../shared/harmony-api.constants.js");
7
9
  const harmony_api_util_ts_1 = require("../../shared/harmony-api.util.js");
8
10
  const ADD_TO_AGENT = "addToAgent";
11
+ const ADD_MANY_TO_AGENT = "addManyToAgent";
12
+ const API_CALL = "apiCall";
9
13
  const UPSERT = "upsert";
10
14
  const UPDATE = "updateByExternalId";
11
15
  const GET = "get";
@@ -118,11 +122,7 @@ async function runContactOperation({ context, operation, itemIndex, agentWorkflo
118
122
  },
119
123
  });
120
124
  }
121
- const contact = {
122
- ...(0, contact_fields_util_ts_1.readContactFields)({ context, itemIndex, fieldsParameter: "contactFields" }),
123
- sourceKind: (0, contact_fields_util_ts_1.readSourceKind)({ context, itemIndex }),
124
- externalId: context.getNodeParameter("externalId", itemIndex, "") || null,
125
- };
125
+ const contact = (0, contact_fields_util_ts_1.readUpsertContact)({ context, itemIndex });
126
126
  if (operation === UPSERT) {
127
127
  return await (0, harmony_api_util_ts_1.harmonyApiRequest)({
128
128
  context,
@@ -132,6 +132,13 @@ async function runContactOperation({ context, operation, itemIndex, agentWorkflo
132
132
  }
133
133
  return await addToAgent({ context, itemIndex, contact, agentWorkflowIds });
134
134
  }
135
+ async function runItem({ context, resource, operation, itemIndex, agentWorkflowIds, }) {
136
+ if (resource === "call")
137
+ return await getCall({ context, itemIndex });
138
+ if (resource === "customApiCall")
139
+ return await (0, custom_api_call_util_ts_1.customApiCall)({ context, itemIndex });
140
+ return await runContactOperation({ context, operation, itemIndex, agentWorkflowIds });
141
+ }
135
142
  class HarmonyAi {
136
143
  description = {
137
144
  displayName: "Harmony AI",
@@ -155,6 +162,7 @@ class HarmonyAi {
155
162
  options: [
156
163
  { name: "Call", value: "call" },
157
164
  { name: "Contact", value: "contact" },
165
+ { name: "Custom API Call", value: "customApiCall" },
158
166
  ],
159
167
  default: "contact",
160
168
  },
@@ -165,6 +173,12 @@ class HarmonyAi {
165
173
  noDataExpression: true,
166
174
  displayOptions: { show: { resource: ["contact"] } },
167
175
  options: [
176
+ {
177
+ name: "Add Many to Agent",
178
+ value: ADD_MANY_TO_AGENT,
179
+ action: "Add many contacts to an agent",
180
+ description: "Send every input item to a voice agent in as few requests as possible. Each output item says whether Harmony accepted its contact.",
181
+ },
168
182
  {
169
183
  name: "Add to Agent",
170
184
  value: ADD_TO_AGENT,
@@ -205,6 +219,15 @@ class HarmonyAi {
205
219
  options: [{ name: "Get", value: GET, action: "Get a call" }],
206
220
  default: GET,
207
221
  },
222
+ {
223
+ displayName: "Operation",
224
+ name: "operation",
225
+ type: "options",
226
+ noDataExpression: true,
227
+ displayOptions: { show: { resource: ["customApiCall"] } },
228
+ options: [{ name: "Make an API Call", value: API_CALL, action: "Make an API call" }],
229
+ default: API_CALL,
230
+ },
208
231
  {
209
232
  displayName: "Voice Agent Name or ID",
210
233
  name: "agentId",
@@ -214,7 +237,7 @@ class HarmonyAi {
214
237
  default: "",
215
238
  description: 'The agent that calls the contact. A contact this agent already handled is skipped. Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code/expressions/">expression</a>.',
216
239
  displayOptions: {
217
- show: { resource: ["contact"], operation: [ADD_TO_AGENT] },
240
+ show: { resource: ["contact"], operation: [ADD_TO_AGENT, ADD_MANY_TO_AGENT] },
218
241
  },
219
242
  },
220
243
  {
@@ -227,7 +250,14 @@ class HarmonyAi {
227
250
  displayOptions: { show: { resource: ["contact"], operation: [GET] } },
228
251
  },
229
252
  (0, contact_fields_util_ts_1.sourceKindProperty)({
230
- operations: [ADD_TO_AGENT, UPSERT, UPDATE, GET_BY_EXTERNAL_ID, GET_CALLS],
253
+ operations: [
254
+ ADD_TO_AGENT,
255
+ ADD_MANY_TO_AGENT,
256
+ UPSERT,
257
+ UPDATE,
258
+ GET_BY_EXTERNAL_ID,
259
+ GET_CALLS,
260
+ ],
231
261
  }),
232
262
  {
233
263
  displayName: "External ID",
@@ -236,7 +266,7 @@ class HarmonyAi {
236
266
  default: "",
237
267
  description: "The contact's ID in the source system. When empty, a new contact gets its Harmony ID as its external ID.",
238
268
  displayOptions: {
239
- show: { resource: ["contact"], operation: [ADD_TO_AGENT, UPSERT] },
269
+ show: { resource: ["contact"], operation: [ADD_TO_AGENT, ADD_MANY_TO_AGENT, UPSERT] },
240
270
  },
241
271
  },
242
272
  {
@@ -251,7 +281,7 @@ class HarmonyAi {
251
281
  },
252
282
  },
253
283
  (0, contact_fields_util_ts_1.contactFieldsProperty)({
254
- operations: [ADD_TO_AGENT, UPSERT],
284
+ operations: [ADD_TO_AGENT, ADD_MANY_TO_AGENT, UPSERT],
255
285
  name: "contactFields",
256
286
  displayName: "Contact Fields",
257
287
  }),
@@ -260,7 +290,7 @@ class HarmonyAi {
260
290
  name: "updateFields",
261
291
  displayName: "Update Fields",
262
292
  }),
263
- (0, contact_fields_util_ts_1.customFieldsProperty)({ operations: [ADD_TO_AGENT, UPSERT, UPDATE] }),
293
+ (0, contact_fields_util_ts_1.customFieldsProperty)({ operations: [ADD_TO_AGENT, ADD_MANY_TO_AGENT, UPSERT, UPDATE] }),
264
294
  {
265
295
  displayName: "Return All",
266
296
  name: "returnAll",
@@ -303,6 +333,59 @@ class HarmonyAi {
303
333
  description: "Whether to add the call's utterances to the output as transcript",
304
334
  displayOptions: { show: { resource: ["call"], operation: [GET] } },
305
335
  },
336
+ {
337
+ displayName: "Method",
338
+ name: "method",
339
+ type: "options",
340
+ options: [
341
+ { name: "DELETE", value: "DELETE" },
342
+ { name: "GET", value: "GET" },
343
+ { name: "PATCH", value: "PATCH" },
344
+ { name: "POST", value: "POST" },
345
+ { name: "PUT", value: "PUT" },
346
+ ],
347
+ default: "GET",
348
+ displayOptions: { show: { resource: ["customApiCall"] } },
349
+ },
350
+ {
351
+ displayName: "Path",
352
+ name: "path",
353
+ type: "string",
354
+ required: true,
355
+ default: "",
356
+ placeholder: "/api/v1/contacts",
357
+ description: 'A Harmony API path, starting with /api/v1/. The node sends it to your region with the credential\'s API key. See the API reference on <a href="https://docs.harmony.ai/">docs.harmony.ai</a>.',
358
+ displayOptions: { show: { resource: ["customApiCall"] } },
359
+ },
360
+ {
361
+ displayName: "Query Parameters",
362
+ name: "queryParameters",
363
+ type: "fixedCollection",
364
+ typeOptions: { multipleValues: true },
365
+ placeholder: "Add Parameter",
366
+ default: {},
367
+ displayOptions: { show: { resource: ["customApiCall"] } },
368
+ options: [
369
+ {
370
+ displayName: "Parameter",
371
+ name: "parameter",
372
+ values: [
373
+ { displayName: "Name", name: "name", type: "string", default: "" },
374
+ { displayName: "Value", name: "value", type: "string", default: "" },
375
+ ],
376
+ },
377
+ ],
378
+ },
379
+ {
380
+ displayName: "Body",
381
+ name: "body",
382
+ type: "json",
383
+ default: "{}",
384
+ description: "The JSON request body",
385
+ displayOptions: {
386
+ show: { resource: ["customApiCall"], method: ["POST", "PUT", "PATCH"] },
387
+ },
388
+ },
306
389
  ],
307
390
  };
308
391
  methods = {
@@ -316,12 +399,13 @@ class HarmonyAi {
316
399
  const items = this.getInputData();
317
400
  const returnData = [];
318
401
  const agentWorkflowIds = new Map();
402
+ if (items.length > 0 && this.getNodeParameter("operation", 0) === ADD_MANY_TO_AGENT) {
403
+ return [await (0, bulk_enrollment_util_ts_1.addManyToAgent)({ context: this, agentWorkflowIds })];
404
+ }
319
405
  for (let itemIndex = 0; itemIndex < items.length; itemIndex++) {
320
406
  const resource = this.getNodeParameter("resource", itemIndex);
321
407
  const operation = this.getNodeParameter("operation", itemIndex);
322
- const run = resource === "call"
323
- ? getCall({ context: this, itemIndex })
324
- : runContactOperation({ context: this, operation, itemIndex, agentWorkflowIds });
408
+ const run = runItem({ context: this, resource, operation, itemIndex, agentWorkflowIds });
325
409
  const result = this.continueOnFail()
326
410
  ? await run.catch((error) => (0, harmony_api_util_ts_1.failureJson)({ error }))
327
411
  : await run;
@@ -0,0 +1,69 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.addManyToAgent = addManyToAgent;
4
+ const contact_fields_util_ts_1 = require("./contact-fields.util.js");
5
+ const harmony_api_util_ts_1 = require("./harmony-api.util.js");
6
+ const BULK_CONTACTS_LIMIT = 1000;
7
+ function groupByAgent({ context, results, }) {
8
+ const contactsByAgent = new Map();
9
+ for (let itemIndex = 0; itemIndex < context.getInputData().length; itemIndex++) {
10
+ try {
11
+ const agentId = context.getNodeParameter("agentId", itemIndex);
12
+ const pending = contactsByAgent.get(agentId) ?? [];
13
+ pending.push({ itemIndex, contact: (0, contact_fields_util_ts_1.readUpsertContact)({ context, itemIndex }) });
14
+ contactsByAgent.set(agentId, pending);
15
+ }
16
+ catch (error) {
17
+ if (!context.continueOnFail())
18
+ throw error;
19
+ results[itemIndex] = (0, harmony_api_util_ts_1.failureJson)({ error });
20
+ }
21
+ }
22
+ return contactsByAgent;
23
+ }
24
+ async function sendBatch({ context, agentId, batch, agentWorkflowIds, results, }) {
25
+ const itemIndex = batch[0]?.itemIndex ?? 0;
26
+ const workflowId = agentWorkflowIds.get(agentId) ?? (0, harmony_api_util_ts_1.findAgentWorkflowId)({ context, itemIndex, agentId });
27
+ agentWorkflowIds.set(agentId, workflowId);
28
+ const result = await (0, harmony_api_util_ts_1.harmonyApiRequest)({
29
+ context,
30
+ itemIndex,
31
+ request: {
32
+ method: "POST",
33
+ path: "/api/v1/contacts/bulk",
34
+ body: {
35
+ assignmentOptions: { workflowId: await workflowId },
36
+ contacts: batch.map(({ contact }) => contact),
37
+ },
38
+ },
39
+ });
40
+ const rejections = new Map(result.rejected.map(({ index, reason }) => [index, reason]));
41
+ batch.forEach(({ itemIndex: batchItemIndex, contact }, position) => {
42
+ const reason = rejections.get(position);
43
+ results[batchItemIndex] = {
44
+ accepted: reason === undefined,
45
+ ...(reason === undefined ? {} : { reason }),
46
+ agentId,
47
+ sourceKind: contact.sourceKind,
48
+ externalId: contact.externalId,
49
+ };
50
+ });
51
+ }
52
+ async function addManyToAgent({ context, agentWorkflowIds, }) {
53
+ const results = [];
54
+ for (const [agentId, pending] of groupByAgent({ context, results })) {
55
+ for (let start = 0; start < pending.length; start += BULK_CONTACTS_LIMIT) {
56
+ const batch = pending.slice(start, start + BULK_CONTACTS_LIMIT);
57
+ try {
58
+ await sendBatch({ context, agentId, batch, agentWorkflowIds, results });
59
+ }
60
+ catch (error) {
61
+ if (!context.continueOnFail())
62
+ throw error;
63
+ for (const { itemIndex } of batch)
64
+ results[itemIndex] = (0, harmony_api_util_ts_1.failureJson)({ error });
65
+ }
66
+ }
67
+ }
68
+ return results.map((json, item) => ({ json, pairedItem: { item } }));
69
+ }
@@ -5,6 +5,7 @@ exports.contactFieldsProperty = contactFieldsProperty;
5
5
  exports.customFieldsProperty = customFieldsProperty;
6
6
  exports.readContactFields = readContactFields;
7
7
  exports.readSourceKind = readSourceKind;
8
+ exports.readUpsertContact = readUpsertContact;
8
9
  const harmony_api_constants_ts_1 = require("./harmony-api.constants.js");
9
10
  function sourceKindProperty({ operations }) {
10
11
  return {
@@ -124,3 +125,10 @@ function readContactFields({ context, itemIndex, fieldsParameter, }) {
124
125
  function readSourceKind({ context, itemIndex, }) {
125
126
  return context.getNodeParameter("sourceKind", itemIndex);
126
127
  }
128
+ function readUpsertContact({ context, itemIndex, }) {
129
+ return {
130
+ ...readContactFields({ context, itemIndex, fieldsParameter: "contactFields" }),
131
+ sourceKind: readSourceKind({ context, itemIndex }),
132
+ externalId: context.getNodeParameter("externalId", itemIndex, "") || null,
133
+ };
134
+ }
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.customApiCall = customApiCall;
4
+ const n8n_workflow_1 = require("n8n-workflow");
5
+ const harmony_api_util_ts_1 = require("./harmony-api.util.js");
6
+ const HARMONY_API_PATH = /^\/api\/v\d+\//;
7
+ const METHODS_WITH_BODY = ["POST", "PUT", "PATCH"];
8
+ function readQuery({ context, itemIndex, }) {
9
+ const parameters = context.getNodeParameter("queryParameters.parameter", itemIndex, []);
10
+ const query = {};
11
+ for (const { name, value } of parameters.filter((parameter) => parameter.name !== "")) {
12
+ const existing = query[name];
13
+ query[name] = existing === undefined ? value : [...[existing].flat(), value];
14
+ }
15
+ return query;
16
+ }
17
+ function readBody({ context, itemIndex, }) {
18
+ const body = context.getNodeParameter("body", itemIndex, "{}");
19
+ if (typeof body !== "string")
20
+ return body;
21
+ try {
22
+ return JSON.parse(body);
23
+ }
24
+ catch {
25
+ throw new n8n_workflow_1.NodeOperationError(context.getNode(), "Body is not valid JSON", { itemIndex });
26
+ }
27
+ }
28
+ function toItem({ value }) {
29
+ if (typeof value === "object" && value !== null)
30
+ return value;
31
+ return value === undefined || value === "" ? {} : { value: value };
32
+ }
33
+ function toItems({ response }) {
34
+ return Array.isArray(response)
35
+ ? response.map((value) => toItem({ value }))
36
+ : toItem({ value: response });
37
+ }
38
+ async function customApiCall({ context, itemIndex, }) {
39
+ const method = context.getNodeParameter("method", itemIndex);
40
+ const path = context.getNodeParameter("path", itemIndex).trim();
41
+ if (!HARMONY_API_PATH.test(path)) {
42
+ throw new n8n_workflow_1.NodeOperationError(context.getNode(), `Path must be a Harmony API path that starts with /api/v1/, such as /api/v1/contacts. Got: ${path}`, { itemIndex });
43
+ }
44
+ const response = await (0, harmony_api_util_ts_1.harmonyApiRequest)({
45
+ context,
46
+ itemIndex,
47
+ request: {
48
+ method,
49
+ path,
50
+ qs: readQuery({ context, itemIndex }),
51
+ ...(METHODS_WITH_BODY.includes(method) ? { body: readBody({ context, itemIndex }) } : {}),
52
+ },
53
+ });
54
+ return toItems({ response });
55
+ }
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.WEBHOOK_SIGNATURE_TOLERANCE_SECONDS = exports.HARMONY_SOURCE_KINDS = exports.HARMONY_REGION_BASE_URLS = exports.HARMONY_USER_AGENT = exports.HARMONY_PACKAGE_VERSION = exports.HARMONY_CREDENTIAL_NAME = void 0;
4
4
  exports.HARMONY_CREDENTIAL_NAME = "harmonyAiApi";
5
- exports.HARMONY_PACKAGE_VERSION = "0.1.0";
5
+ exports.HARMONY_PACKAGE_VERSION = "0.1.1";
6
6
  exports.HARMONY_USER_AGENT = `harmony-n8n/${exports.HARMONY_PACKAGE_VERSION}`;
7
7
  exports.HARMONY_REGION_BASE_URLS = {
8
8
  us: "https://api.harmony.ai",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "n8n-nodes-harmony-ai",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "n8n nodes for Harmony AI voice agents: send contacts to Harmony voice agents, look up contacts, calls and transcripts, and trigger flows when calls start, end, or have a recording.",
5
5
  "keywords": [
6
6
  "harmony",