Spaces:
Running
Running
File size: 5,817 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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
/**
* Copyright (c) 2023 MERCENARIES.AI PTE. LTD.
* All rights reserved.
*/
import Rete from 'rete';
import { type OmniControl, type OmniIO } from './types.js';
import { WorkflowClientControlManager } from '../WorkflowClientControlManager.js';
class OAIControl31 extends Rete.Control {
data: OmniControl;
props: { ikey: string };
emitter?: any;
constructor(config: OmniControl, control: any, emitter: any) {
super(config.name);
this.data = JSON.parse(JSON.stringify(config));
this.emitter = emitter;
this.props = { ikey: config.name };
// @ts-expect-error
this.component = control;
if (!control) {
console.error('Could not find component for ' + config.controlType);
}
// handle various types of choices array
}
async initChoices(): Promise<void> {
if (this.data.choices) {
const choices = this.data.choices;
if (Array.isArray(choices)) {
this.data.choices = choices.map(function (v: any) {
if (typeof v === 'object') {
return v;
} else {
return { value: v, title: v };
}
});
}
if (typeof this.data.choices === 'object') {
const choices = this.data.choices as { block: any; args: any; map: any; cache?: 'global' | 'user' | 'none' };
if (choices.block) {
let list: any = ['Internal Error Fetching choices'];
try {
list = await (globalThis as any).client.runBlock({
block: choices.block,
args: choices.args || {},
cache: choices.cache ?? choices.map.cache ?? 'none'
});
} catch (ex: any) {
console.error('Could not load choices for ' + this.data.name + ': ' + ex.message);
list = ['ERROR: ' + ex.message, this.data.default];
}
if (list.error) {
console.error('Could not load choices for ' + this.data.name + ': ' + list.error.message);
list = ['ERROR: ' + list.error, this.data.default];
}
const root = choices.map?.root;
if (root && list[root] != null && Array.isArray(list[root])) {
list = list[root];
}
if (!Array.isArray(list)) {
list = Array.from(Object.values(list));
}
interface Choice {
value: any;
title: string;
description: string;
}
const filterRegex = new RegExp(choices.map?.filter?.value);
this.data.choices = list
.map((v: any) => {
let e: Choice = { value: v, title: v, description: '' };
if (choices.map?.value && choices.map?.title) {
e = {
value: v[choices.map.value],
title: v[choices.map.title],
description: v[choices.map.description] || ''
};
}
return e;
})
.filter((e: Choice) => e.value && filterRegex.test(e.title))
.sort((a: Choice, b: Choice) => b.title.localeCompare(a.title));
}
}
}
}
get dataType() {
return this.data.dataType ?? 'string';
}
get controlType() {
console.log('Access to field controlType on control');
return this.data.controlType;
}
get type() {
console.trace();
console.log('Access to deprecated field type on control');
return this.data.dataType;
}
get opts() {
return this.data;
}
get displays() {
return this.data.displays ?? null;
}
get minimum() {
return this.data.minimum;
}
get description() {
return this.data.description;
}
get title() {
return this.data.title ?? this.data.name;
}
get maximum() {
return this.data.maximum;
}
get customData() {
return this.data.customData ?? {};
}
custom(key: string) {
return this.data.customData?.[key] ?? null;
}
get choices() {
return this.data.choices ?? ['(default)'];
}
get readonly() {
return this.data.readonly ?? false;
}
_formatValue(val: any) {
if (val) {
if ((this.dataType === 'number' || this.dataType == 'float') && typeof val === 'string') {
val = parseFloat(val);
} else if (this.dataType === 'integer' && typeof val === 'string') {
val = parseFloat(val);
} else if (this.dataType === 'boolean' && typeof val === 'number') {
val = val != 0;
} else if (this.dataType === 'boolean' && typeof val === 'string') {
val = [true, 'true', '1', 'on', 'active'].includes(val.toLowerCase());
}
}
return val;
}
setValue(val: any) {
// Readonly or 'displays' properties are not settable
if (this.displays || this.readonly) {
return;
}
val = this._formatValue(val);
this.putData(this.props.ikey, val);
// @ts-ignore
this.update();
}
static async fromControl(ctl: OmniControl, emitter: any): Promise<OAIControl31> {
const control = WorkflowClientControlManager.getInstance().get(ctl.controlType);
const ret = new OAIControl31(ctl, control, emitter);
await ret.initChoices();
return ret;
}
static async fromIO(ctlType: string, io: OmniIO, emitter: any): Promise<OAIControl31> {
const control = {
dataType: io.type,
controlType: ctlType,
name: io.name,
title: io.title,
choices: io.choices,
description: io.description,
step: io.step,
default: io.default,
minimum: io.minimum,
maximum: io.maximum,
required: io.required,
...(io.control || {})
};
const ctl = WorkflowClientControlManager.getInstance().get(ctlType);
const ret = new OAIControl31(control, ctl, emitter);
await ret.initChoices();
return ret;
}
}
export default OAIControl31;
|