File size: 3,772 Bytes
b39afbe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/**
 * Copyright (c) 2023 MERCENARIES.AI PTE. LTD.
 * All rights reserved.
 */

//@ts-check

// @ts-ignore
// @ts-ignore
import { OAIBaseComponent, OmniComponentMacroTypes } from 'omni-sockets';

// @ts-ignore
function generateTitle(value) {
  const title = value
    .replace(/_/g, ' ') // Replace all underscores with spaces
    // @ts-ignore
    .replace(/\b\w/g, (match) => match.toUpperCase()); // Capitalize the first letter of each word

  return title;
}

// @ts-ignore
function setComponentInputs(component, inputs) {
  // @ts-ignore
  inputs.forEach(function (input) {
    const name = input.name;
    const type = input.type;
    const customSocket = input.customSocket;
    const description = input.description;
    const default_value = input.defaultValue;
    let title = input.title;
    const choices = input.choices;
    const minimum = input.minimum;
    const maximum = input.maximum;
    const step = input.step;
    const allow_multiple = input.allowMultiple;

    if (!title || title === '') title = generateTitle(name);

    component.addInput(
      component
        .createInput(name, type, customSocket)
        .set('title', title || '')
        .set('description', description || '')
        .set('choices', choices || null)
        .set('minimum', minimum || null)
        .set('maximum', maximum || null)
        .set('step', step || null)
        .set('allowMultiple', allow_multiple || null)
        .setDefault(default_value)
        .toOmniIO()
    );
  });
  return component;
}

// @ts-ignore
function setComponentOutputs(component, outputs) {
  // @ts-ignore
  outputs.forEach(function (output) {
    const name = output.name;
    const type = output.type;
    const customSocket = output.customSocket;
    const description = output.description;
    let title = output.title;

    if (!title || title === '') title = generateTitle(name);

    component.addOutput(
      component
        .createOutput(name, type, customSocket)
        .set('title', title || '')
        .set('description', description || '')
        .toOmniIO()
    );
  });
  return component;
}

// @ts-ignore
function setComponentControls(component, controls) {
  // @ts-ignore
  controls.forEach(function (control) {
    const name = control.name;
    let title = control.title;
    const placeholder = control.placeholder;
    const description = control.description;

    if (!title || title === '') title = generateTitle(name);

    component.addControl(
      component
        .createControl(name)
        .set('title', title || '')
        .set('placeholder', placeholder || '')
        .set('description', description || '')
        .toOmniControl()
    );
  });
  return component;
}

function createComponent(
  // @ts-ignore
  group_id,
  // @ts-ignore
  id,
  // @ts-ignore
  title,
  // @ts-ignore
  category,
  // @ts-ignore
  description,
  // @ts-ignore
  summary,
  // @ts-ignore
  links,
  // @ts-ignore
  inputs,
  // @ts-ignore
  outputs,
  // @ts-ignore
  controls,
  // @ts-ignore
  payloadParser
) {
  if (!links) links = {};

  let baseComponent = OAIBaseComponent.create(group_id, id)
    .fromScratch()
    .set('title', title)
    .set('category', category)
    .set('description', description)
    .setMethod('X-CUSTOM')
    .setMeta({
      source: {
        summary,
        links
      }
    });

  baseComponent = setComponentInputs(baseComponent, inputs);
  baseComponent = setComponentOutputs(baseComponent, outputs);
  if (controls) baseComponent = setComponentControls(baseComponent, controls);
  baseComponent.setMacro(OmniComponentMacroTypes.EXEC, payloadParser);

  const component = baseComponent.toJSON();
  return component;
}

export { createComponent, setComponentInputs, setComponentOutputs, setComponentControls };