@gravity-rail/sdk 0.7.2 → 0.7.3
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/dist/{chunk-WXSLDWIA.js → chunk-YCASSXLL.js} +856 -2
- package/dist/chunk-YCASSXLL.js.map +1 -0
- package/dist/{client-DiCwhhSe.d.ts → client-B_cweJhP.d.cts} +136 -31
- package/dist/{client-CoeH9W54.d.cts → client-C-on7whi.d.ts} +136 -31
- package/dist/helpers.d.cts +2 -2
- package/dist/helpers.d.ts +2 -2
- package/dist/index.cjs +2090 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +276 -3
- package/dist/index.d.ts +276 -3
- package/dist/index.js +1223 -1
- package/dist/index.js.map +1 -1
- package/dist/{inquiries-BIlL6Obb.d.cts → inquiries-BX3PuU-7.d.cts} +1 -1
- package/dist/{inquiries-BIlL6Obb.d.ts → inquiries-BX3PuU-7.d.ts} +1 -1
- package/dist/internal.cjs +856 -1
- package/dist/internal.cjs.map +1 -1
- package/dist/internal.d.cts +4 -4
- package/dist/internal.d.ts +4 -4
- package/dist/internal.js +3 -1
- package/dist/schemas.d.cts +2 -2
- package/dist/schemas.d.ts +2 -2
- package/dist/{index-LmHmlaqN.d.cts → scopes-CNbQi30F.d.cts} +3 -3
- package/dist/{index-F3VaUQxJ.d.ts → scopes-ColqadkM.d.ts} +3 -3
- package/package.json +2 -2
- package/dist/chunk-WXSLDWIA.js.map +0 -1
package/dist/index.js
CHANGED
|
@@ -95,6 +95,7 @@ import {
|
|
|
95
95
|
areDependenciesSatisfied,
|
|
96
96
|
canPreviewVoice,
|
|
97
97
|
clearRegistry,
|
|
98
|
+
createWorkspaceClient,
|
|
98
99
|
decodePermissions,
|
|
99
100
|
encodePermissions,
|
|
100
101
|
eventRuleActionParams,
|
|
@@ -121,7 +122,7 @@ import {
|
|
|
121
122
|
requiresAssistantParticipant,
|
|
122
123
|
usesBuiltinAssistant,
|
|
123
124
|
validateFeatureDependencies
|
|
124
|
-
} from "./chunk-
|
|
125
|
+
} from "./chunk-YCASSXLL.js";
|
|
125
126
|
import {
|
|
126
127
|
AttendeeResponseStatus,
|
|
127
128
|
AttendeeStatus,
|
|
@@ -136,6 +137,1212 @@ import {
|
|
|
136
137
|
DataTypeRecordScope,
|
|
137
138
|
IndexStatus
|
|
138
139
|
} from "./chunk-QS6ZRQUZ.js";
|
|
140
|
+
|
|
141
|
+
// src/workspace-builder/resource.ts
|
|
142
|
+
function isRecord(value) {
|
|
143
|
+
return !!value && typeof value === "object" && !Array.isArray(value);
|
|
144
|
+
}
|
|
145
|
+
function isPlainRecord(value) {
|
|
146
|
+
if (!isRecord(value)) return false;
|
|
147
|
+
const prototype = Object.getPrototypeOf(value);
|
|
148
|
+
return prototype === Object.prototype || prototype === null;
|
|
149
|
+
}
|
|
150
|
+
function slugify(name) {
|
|
151
|
+
return name.toLowerCase().replaceAll(" ", "_").replaceAll("-", "_");
|
|
152
|
+
}
|
|
153
|
+
var WorkspaceBuilderResource = class {
|
|
154
|
+
_id;
|
|
155
|
+
_uuid;
|
|
156
|
+
_created = false;
|
|
157
|
+
_existed = false;
|
|
158
|
+
matchKey() {
|
|
159
|
+
return void 0;
|
|
160
|
+
}
|
|
161
|
+
adopt(existing) {
|
|
162
|
+
this._id = existing.id;
|
|
163
|
+
this._uuid = existing.uuid;
|
|
164
|
+
this._existed = true;
|
|
165
|
+
}
|
|
166
|
+
};
|
|
167
|
+
function resolveRefs(params) {
|
|
168
|
+
return resolveRefValue(params);
|
|
169
|
+
}
|
|
170
|
+
function resolveRefValue(value) {
|
|
171
|
+
if (value instanceof WorkspaceBuilderResource) return value._uuid;
|
|
172
|
+
if (Array.isArray(value)) return value.map(resolveRefValue);
|
|
173
|
+
if (!isPlainRecord(value)) return value;
|
|
174
|
+
const out = {};
|
|
175
|
+
for (const [key, nestedValue] of Object.entries(value)) {
|
|
176
|
+
out[key] = resolveRefValue(nestedValue);
|
|
177
|
+
}
|
|
178
|
+
return out;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// src/workspace-builder/automation.ts
|
|
182
|
+
var MemberFilterBuilder = class extends WorkspaceBuilderResource {
|
|
183
|
+
constructor(name, options = {}) {
|
|
184
|
+
super();
|
|
185
|
+
this.name = name;
|
|
186
|
+
this.description = options.description ?? "";
|
|
187
|
+
}
|
|
188
|
+
description;
|
|
189
|
+
rules = [];
|
|
190
|
+
addRule(objectType, filterType, options = {}) {
|
|
191
|
+
this.rules.push({
|
|
192
|
+
objectType,
|
|
193
|
+
filterType,
|
|
194
|
+
...options.objectIds ? { objectIds: options.objectIds } : {},
|
|
195
|
+
...options.query ? { query: options.query } : {}
|
|
196
|
+
});
|
|
197
|
+
return this;
|
|
198
|
+
}
|
|
199
|
+
planLine() {
|
|
200
|
+
return `MemberFilter: ${this.name}${this.rules.length ? ` (${this.rules.length} rules)` : ""}`;
|
|
201
|
+
}
|
|
202
|
+
async apply(client) {
|
|
203
|
+
const payload = {
|
|
204
|
+
name: this.name,
|
|
205
|
+
description: this.description,
|
|
206
|
+
rules: this.rules
|
|
207
|
+
};
|
|
208
|
+
const result = await client.createMemberFilter(payload);
|
|
209
|
+
this._id = result.id;
|
|
210
|
+
this._uuid = result.uuid;
|
|
211
|
+
this._created = true;
|
|
212
|
+
}
|
|
213
|
+
};
|
|
214
|
+
var RoutineBuilder = class extends WorkspaceBuilderResource {
|
|
215
|
+
constructor(name, cronSchedule = "", options = {}) {
|
|
216
|
+
super();
|
|
217
|
+
this.name = name;
|
|
218
|
+
this.cronSchedule = cronSchedule;
|
|
219
|
+
this.active = options.active ?? false;
|
|
220
|
+
this.description = options.description ?? "";
|
|
221
|
+
}
|
|
222
|
+
active;
|
|
223
|
+
description;
|
|
224
|
+
rules = [];
|
|
225
|
+
addRule(actionType, options = {}) {
|
|
226
|
+
this.rules.push({
|
|
227
|
+
actionType,
|
|
228
|
+
workflow: options.workflow,
|
|
229
|
+
memberFilter: options.memberFilter,
|
|
230
|
+
actionParams: options.actionParams ?? {}
|
|
231
|
+
});
|
|
232
|
+
return this;
|
|
233
|
+
}
|
|
234
|
+
planLine() {
|
|
235
|
+
const parts = [
|
|
236
|
+
...this.cronSchedule ? [`cron=${this.cronSchedule}`] : [],
|
|
237
|
+
...this.rules.length ? [`${this.rules.length} rules`] : [],
|
|
238
|
+
this.active ? "active" : "inactive"
|
|
239
|
+
];
|
|
240
|
+
return `Routine: ${this.name} (${parts.join(", ")})`;
|
|
241
|
+
}
|
|
242
|
+
async apply(client) {
|
|
243
|
+
const eventRules = this.rules.map((rule) => {
|
|
244
|
+
const actionParams = { ...rule.actionParams };
|
|
245
|
+
if (rule.workflow?._id !== void 0) actionParams.workflowId = rule.workflow._id;
|
|
246
|
+
if (rule.memberFilter?._id !== void 0)
|
|
247
|
+
actionParams.memberFilterId = rule.memberFilter._id;
|
|
248
|
+
return {
|
|
249
|
+
uuid: crypto.randomUUID(),
|
|
250
|
+
objectType: "routine" /* ROUTINE */,
|
|
251
|
+
eventType: "routine:triggered" /* ROUTINE_TRIGGERED */,
|
|
252
|
+
actionType: rule.actionType,
|
|
253
|
+
actionParams,
|
|
254
|
+
active: true
|
|
255
|
+
};
|
|
256
|
+
});
|
|
257
|
+
const payload = {
|
|
258
|
+
name: this.name,
|
|
259
|
+
cronString: this.cronSchedule,
|
|
260
|
+
scheduleMode: this.cronSchedule ? "CRON" : void 0,
|
|
261
|
+
scheduleEnabled: this.active,
|
|
262
|
+
...this.description ? { description: this.description } : {},
|
|
263
|
+
...eventRules.length ? { eventRules } : {}
|
|
264
|
+
};
|
|
265
|
+
const result = await client.createRoutine(payload);
|
|
266
|
+
this._id = result.id;
|
|
267
|
+
this._uuid = result.uuid;
|
|
268
|
+
this._created = true;
|
|
269
|
+
}
|
|
270
|
+
};
|
|
271
|
+
var NotificationRuleBuilder = class extends WorkspaceBuilderResource {
|
|
272
|
+
constructor(name, options = {}) {
|
|
273
|
+
super();
|
|
274
|
+
this.name = name;
|
|
275
|
+
this.noticeType = options.noticeType ?? options.notice_type ?? name;
|
|
276
|
+
this.memberId = options.memberId ?? options.member_id;
|
|
277
|
+
this.workflow = options.workflow;
|
|
278
|
+
this.prompt = options.prompt ?? options.message;
|
|
279
|
+
this.notifySms = Boolean(options.notifySms ?? options.notify_sms);
|
|
280
|
+
this.notifyEmail = Boolean(options.notifyEmail ?? options.notify_email);
|
|
281
|
+
this.notifyVoice = Boolean(options.notifyVoice ?? options.notify_voice);
|
|
282
|
+
}
|
|
283
|
+
noticeType;
|
|
284
|
+
memberId;
|
|
285
|
+
workflow;
|
|
286
|
+
prompt;
|
|
287
|
+
notifySms;
|
|
288
|
+
notifyEmail;
|
|
289
|
+
notifyVoice;
|
|
290
|
+
planLine() {
|
|
291
|
+
const parts = [];
|
|
292
|
+
if (this.memberId !== void 0) parts.push(`memberId=${this.memberId}`);
|
|
293
|
+
if (this.workflow) parts.push(`workflow='${this.workflow.name}'`);
|
|
294
|
+
return `NotificationRule: ${this.name}${parts.length ? ` (${parts.join(", ")})` : ""}`;
|
|
295
|
+
}
|
|
296
|
+
async apply(client) {
|
|
297
|
+
if (this.memberId === void 0) {
|
|
298
|
+
throw new Error(`Notification rule '${this.name}' requires memberId`);
|
|
299
|
+
}
|
|
300
|
+
const payload = {
|
|
301
|
+
noticeType: this.noticeType,
|
|
302
|
+
memberId: this.memberId,
|
|
303
|
+
workflowId: this.workflow?._id,
|
|
304
|
+
prompt: this.prompt ?? null,
|
|
305
|
+
notifySms: this.notifySms,
|
|
306
|
+
notifyEmail: this.notifyEmail,
|
|
307
|
+
notifyVoice: this.notifyVoice
|
|
308
|
+
};
|
|
309
|
+
const result = await client.createNotificationRule(payload);
|
|
310
|
+
this._id = result.id;
|
|
311
|
+
this._created = true;
|
|
312
|
+
}
|
|
313
|
+
};
|
|
314
|
+
|
|
315
|
+
// src/workspace-builder/forms.ts
|
|
316
|
+
function normalizeOptions(options) {
|
|
317
|
+
if (options.length === 0) return [];
|
|
318
|
+
const first = options[0];
|
|
319
|
+
if (isRecord(first)) return options;
|
|
320
|
+
if (Array.isArray(first)) {
|
|
321
|
+
return options.map((entry) => ({ label: String(entry[0]), value: entry[1] }));
|
|
322
|
+
}
|
|
323
|
+
return options.map((option) => ({ label: String(option), value: String(option) }));
|
|
324
|
+
}
|
|
325
|
+
function createFieldDraft(fieldType, name, slug, options = {}) {
|
|
326
|
+
return {
|
|
327
|
+
name,
|
|
328
|
+
slug,
|
|
329
|
+
fieldType,
|
|
330
|
+
required: Boolean(options.required),
|
|
331
|
+
order: options.order ?? 0,
|
|
332
|
+
...options.description ? { description: options.description } : {},
|
|
333
|
+
...options.choices ? { options: options.choices } : {},
|
|
334
|
+
...options.cel ? { celFunction: options.cel } : {}
|
|
335
|
+
};
|
|
336
|
+
}
|
|
337
|
+
var FormBuilder = class extends WorkspaceBuilderResource {
|
|
338
|
+
name;
|
|
339
|
+
slug;
|
|
340
|
+
description;
|
|
341
|
+
prompt;
|
|
342
|
+
collection;
|
|
343
|
+
fields = [];
|
|
344
|
+
eventRules = [];
|
|
345
|
+
constructor(name, slug, options = {}) {
|
|
346
|
+
super();
|
|
347
|
+
this.name = name;
|
|
348
|
+
this.slug = slug ?? slugify(name);
|
|
349
|
+
this.description = options.description ?? "";
|
|
350
|
+
this.prompt = options.prompt ?? "";
|
|
351
|
+
this.collection = options.collection ?? false;
|
|
352
|
+
for (const field of options.fields ?? []) {
|
|
353
|
+
if (!isRecord(field) || !field.name) continue;
|
|
354
|
+
const choices = field.options ? normalizeOptions(field.options) : void 0;
|
|
355
|
+
this.fields.push(
|
|
356
|
+
createFieldDraft(
|
|
357
|
+
field.fieldType ?? field.field_type ?? "text" /* TEXT */,
|
|
358
|
+
field.name,
|
|
359
|
+
field.slug ?? slugify(field.name),
|
|
360
|
+
{
|
|
361
|
+
required: Boolean(field.required ?? field.isRequired),
|
|
362
|
+
order: field.order ?? 0,
|
|
363
|
+
description: field.description,
|
|
364
|
+
choices
|
|
365
|
+
}
|
|
366
|
+
)
|
|
367
|
+
);
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
field(fieldType, name, slug, options = {}) {
|
|
371
|
+
this.fields.push(createFieldDraft(fieldType, name, slug, options));
|
|
372
|
+
return this;
|
|
373
|
+
}
|
|
374
|
+
enumField(name, slug, options, settings = {}) {
|
|
375
|
+
return this.field("enum" /* ENUM */, name, slug, {
|
|
376
|
+
...settings,
|
|
377
|
+
choices: normalizeOptions(options)
|
|
378
|
+
});
|
|
379
|
+
}
|
|
380
|
+
textField(name, slug, settings = {}) {
|
|
381
|
+
return this.field("text" /* TEXT */, name, slug, settings);
|
|
382
|
+
}
|
|
383
|
+
numberField(name, slug, settings = {}) {
|
|
384
|
+
return this.field("number" /* NUMBER */, name, slug, settings);
|
|
385
|
+
}
|
|
386
|
+
dateField(name, slug, settings = {}) {
|
|
387
|
+
return this.field("date" /* DATE */, name, slug, settings);
|
|
388
|
+
}
|
|
389
|
+
booleanField(name, slug, settings = {}) {
|
|
390
|
+
return this.field("boolean" /* BOOLEAN */, name, slug, settings);
|
|
391
|
+
}
|
|
392
|
+
functionField(name, slug, cel, settings = {}) {
|
|
393
|
+
return this.field("function" /* FUNCTION */, name, slug, { ...settings, cel });
|
|
394
|
+
}
|
|
395
|
+
emailField(name, slug, settings = {}) {
|
|
396
|
+
return this.field("email" /* EMAIL */, name, slug, settings);
|
|
397
|
+
}
|
|
398
|
+
phoneField(name, slug, settings = {}) {
|
|
399
|
+
return this.field("phone" /* PHONE */, name, slug, settings);
|
|
400
|
+
}
|
|
401
|
+
longtextField(name, slug, settings = {}) {
|
|
402
|
+
return this.field("longtext" /* LONGTEXT */, name, slug, settings);
|
|
403
|
+
}
|
|
404
|
+
integerField(name, slug, settings = {}) {
|
|
405
|
+
return this.field("integer" /* INTEGER */, name, slug, settings);
|
|
406
|
+
}
|
|
407
|
+
datetimeField(name, slug, settings = {}) {
|
|
408
|
+
return this.field("datetime" /* DATETIME */, name, slug, settings);
|
|
409
|
+
}
|
|
410
|
+
timeOfDayField(name, slug, settings = {}) {
|
|
411
|
+
return this.field("time_of_day" /* TIME_OF_DAY */, name, slug, settings);
|
|
412
|
+
}
|
|
413
|
+
usZipCodeField(name, slug, settings = {}) {
|
|
414
|
+
return this.field("us_zip_code" /* US_ZIP_CODE */, name, slug, settings);
|
|
415
|
+
}
|
|
416
|
+
arrayField(name, slug, settings = {}) {
|
|
417
|
+
return this.field("array" /* ARRAY */, name, slug, settings);
|
|
418
|
+
}
|
|
419
|
+
objectField(name, slug, settings = {}) {
|
|
420
|
+
return this.field("object" /* OBJECT */, name, slug, settings);
|
|
421
|
+
}
|
|
422
|
+
onRecordCreated(action, params = {}, condition, name) {
|
|
423
|
+
this.eventRules.push({
|
|
424
|
+
eventType: "data_record:created" /* DATA_RECORD_CREATED */,
|
|
425
|
+
actionType: action,
|
|
426
|
+
actionParams: params,
|
|
427
|
+
condition,
|
|
428
|
+
name
|
|
429
|
+
});
|
|
430
|
+
return this;
|
|
431
|
+
}
|
|
432
|
+
onRecordUpdated(action, params = {}, condition, name) {
|
|
433
|
+
this.eventRules.push({
|
|
434
|
+
eventType: "data_record:updated" /* DATA_RECORD_UPDATED */,
|
|
435
|
+
actionType: action,
|
|
436
|
+
actionParams: params,
|
|
437
|
+
condition,
|
|
438
|
+
name
|
|
439
|
+
});
|
|
440
|
+
return this;
|
|
441
|
+
}
|
|
442
|
+
matchKey() {
|
|
443
|
+
return this.slug;
|
|
444
|
+
}
|
|
445
|
+
planLine() {
|
|
446
|
+
return `Form: ${this.name} (slug=${this.slug}, ${this.fields.length} fields)`;
|
|
447
|
+
}
|
|
448
|
+
async apply(client) {
|
|
449
|
+
if (this._existed) return;
|
|
450
|
+
const payload = {
|
|
451
|
+
name: this.name,
|
|
452
|
+
slug: this.slug,
|
|
453
|
+
description: this.description,
|
|
454
|
+
prompt: this.prompt,
|
|
455
|
+
isCollection: this.collection,
|
|
456
|
+
fields: this.fields
|
|
457
|
+
};
|
|
458
|
+
const result = await client.createDataType(payload);
|
|
459
|
+
this._id = result.id;
|
|
460
|
+
this._uuid = result.uuid;
|
|
461
|
+
this._created = true;
|
|
462
|
+
}
|
|
463
|
+
};
|
|
464
|
+
var MemberLabelBuilder = class extends WorkspaceBuilderResource {
|
|
465
|
+
name;
|
|
466
|
+
slug;
|
|
467
|
+
color;
|
|
468
|
+
description;
|
|
469
|
+
constructor(name, slug, color = "8b5cf6", description = "") {
|
|
470
|
+
super();
|
|
471
|
+
this.name = name;
|
|
472
|
+
this.slug = slug ?? slugify(name);
|
|
473
|
+
this.color = color;
|
|
474
|
+
this.description = description;
|
|
475
|
+
}
|
|
476
|
+
matchKey() {
|
|
477
|
+
return this.slug;
|
|
478
|
+
}
|
|
479
|
+
planLine() {
|
|
480
|
+
return `Label: ${this.name} (slug=${this.slug})`;
|
|
481
|
+
}
|
|
482
|
+
async apply(client) {
|
|
483
|
+
if (this._existed) return;
|
|
484
|
+
const payload = {
|
|
485
|
+
name: this.name,
|
|
486
|
+
slug: this.slug,
|
|
487
|
+
color: this.color,
|
|
488
|
+
description: this.description
|
|
489
|
+
};
|
|
490
|
+
const result = await client.createMemberLabel(payload);
|
|
491
|
+
this._id = result.id;
|
|
492
|
+
this._uuid = result.uuid;
|
|
493
|
+
this._created = true;
|
|
494
|
+
}
|
|
495
|
+
};
|
|
496
|
+
|
|
497
|
+
// src/workspace-builder/journeys.ts
|
|
498
|
+
var JourneyStepBuilder = class extends WorkspaceBuilderResource {
|
|
499
|
+
constructor(name, journey, order = 0, workflow, description = "") {
|
|
500
|
+
super();
|
|
501
|
+
this.name = name;
|
|
502
|
+
this.journey = journey;
|
|
503
|
+
this.order = order;
|
|
504
|
+
this.workflow = workflow;
|
|
505
|
+
this.description = description;
|
|
506
|
+
}
|
|
507
|
+
connections = [];
|
|
508
|
+
connectsTo(child, condition, required = false, timeoutSeconds) {
|
|
509
|
+
this.connections.push({ parent: this, child, condition, required, timeoutSeconds });
|
|
510
|
+
return this;
|
|
511
|
+
}
|
|
512
|
+
planLine() {
|
|
513
|
+
return `Step: ${this.name}${this.workflow ? ` -> workflow '${this.workflow.name}'` : ""}`;
|
|
514
|
+
}
|
|
515
|
+
async apply(client) {
|
|
516
|
+
if (this.journey._id === void 0) return;
|
|
517
|
+
const payload = {
|
|
518
|
+
name: this.name,
|
|
519
|
+
description: this.description,
|
|
520
|
+
order: this.order,
|
|
521
|
+
...this.workflow?._id ? { workflowId: this.workflow._id } : {}
|
|
522
|
+
};
|
|
523
|
+
const result = await client.createStep(this.journey._id, payload);
|
|
524
|
+
this._id = result.id;
|
|
525
|
+
this._uuid = result.uuid;
|
|
526
|
+
this._created = true;
|
|
527
|
+
}
|
|
528
|
+
};
|
|
529
|
+
var JourneyBuilder = class extends WorkspaceBuilderResource {
|
|
530
|
+
constructor(name, options = {}) {
|
|
531
|
+
super();
|
|
532
|
+
this.name = name;
|
|
533
|
+
this.description = options.description ?? "";
|
|
534
|
+
}
|
|
535
|
+
description;
|
|
536
|
+
steps = [];
|
|
537
|
+
goals = [];
|
|
538
|
+
step(name, options = {}) {
|
|
539
|
+
const step = new JourneyStepBuilder(
|
|
540
|
+
name,
|
|
541
|
+
this,
|
|
542
|
+
options.order ?? 0,
|
|
543
|
+
options.workflow,
|
|
544
|
+
options.description ?? ""
|
|
545
|
+
);
|
|
546
|
+
this.steps.push(step);
|
|
547
|
+
return step;
|
|
548
|
+
}
|
|
549
|
+
goal(name, options = {}) {
|
|
550
|
+
this.goals.push({
|
|
551
|
+
name,
|
|
552
|
+
description: options.description ?? "",
|
|
553
|
+
step: options.step,
|
|
554
|
+
completionCel: options.completionCel ?? options.completion_cel,
|
|
555
|
+
value: options.value,
|
|
556
|
+
order: options.order ?? 0
|
|
557
|
+
});
|
|
558
|
+
return this;
|
|
559
|
+
}
|
|
560
|
+
planLine() {
|
|
561
|
+
const parts = [];
|
|
562
|
+
if (this.steps.length) parts.push(`${this.steps.length} steps`);
|
|
563
|
+
if (this.goals.length) parts.push(`${this.goals.length} goals`);
|
|
564
|
+
return `Journey: ${this.name}${parts.length ? ` (${parts.join(", ")})` : ""}`;
|
|
565
|
+
}
|
|
566
|
+
async apply(client) {
|
|
567
|
+
const result = await client.createJourney({
|
|
568
|
+
name: this.name,
|
|
569
|
+
description: this.description
|
|
570
|
+
});
|
|
571
|
+
this._id = result.id;
|
|
572
|
+
this._uuid = result.uuid;
|
|
573
|
+
this._created = true;
|
|
574
|
+
}
|
|
575
|
+
};
|
|
576
|
+
function buildJourneyConnectionPayload(connection) {
|
|
577
|
+
if (!connection.parent._uuid || !connection.child._uuid) return void 0;
|
|
578
|
+
return {
|
|
579
|
+
parentStepUuid: connection.parent._uuid,
|
|
580
|
+
childStepUuid: connection.child._uuid,
|
|
581
|
+
...connection.condition ? { celCondition: connection.condition } : {},
|
|
582
|
+
...connection.required ? { required: true } : {},
|
|
583
|
+
...connection.timeoutSeconds ? { timeoutSeconds: connection.timeoutSeconds } : {}
|
|
584
|
+
};
|
|
585
|
+
}
|
|
586
|
+
function buildJourneyGoalPayload(goal) {
|
|
587
|
+
return {
|
|
588
|
+
name: goal.name,
|
|
589
|
+
description: goal.description,
|
|
590
|
+
order: goal.order,
|
|
591
|
+
...goal.step?._uuid ? { goalStepUuid: goal.step._uuid } : {},
|
|
592
|
+
...goal.completionCel ? { completionCel: goal.completionCel } : {},
|
|
593
|
+
...goal.value !== void 0 ? { value: goal.value } : {}
|
|
594
|
+
};
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
// src/workspace-builder/role-update.ts
|
|
598
|
+
var RoleUpdate = class extends WorkspaceBuilderResource {
|
|
599
|
+
constructor(roleId, workflowIds, dataTypeIds) {
|
|
600
|
+
super();
|
|
601
|
+
this.roleId = roleId;
|
|
602
|
+
this.workflowIds = workflowIds;
|
|
603
|
+
this.dataTypeIds = dataTypeIds;
|
|
604
|
+
}
|
|
605
|
+
planLine() {
|
|
606
|
+
const parts = [];
|
|
607
|
+
if (this.workflowIds) parts.push(`workflows=${JSON.stringify(this.workflowIds)}`);
|
|
608
|
+
if (this.dataTypeIds) parts.push(`dataTypes=${JSON.stringify(this.dataTypeIds)}`);
|
|
609
|
+
return `RoleUpdate: roleId=${this.roleId}${parts.length ? ` (${parts.join(", ")})` : ""}`;
|
|
610
|
+
}
|
|
611
|
+
async apply(client) {
|
|
612
|
+
const payload = {};
|
|
613
|
+
if (this.workflowIds) payload.workflowIds = this.workflowIds;
|
|
614
|
+
if (this.dataTypeIds) payload.dataTypeIds = this.dataTypeIds;
|
|
615
|
+
if (Object.keys(payload).length) await client.updateMemberRole(this.roleId, payload);
|
|
616
|
+
this._id = this.roleId;
|
|
617
|
+
this._created = true;
|
|
618
|
+
}
|
|
619
|
+
};
|
|
620
|
+
|
|
621
|
+
// src/workspace-builder/workflows.ts
|
|
622
|
+
var PERM_KEYS = {
|
|
623
|
+
canRead: "canRead",
|
|
624
|
+
canCreate: "canCreate",
|
|
625
|
+
canEdit: "canEdit",
|
|
626
|
+
can_read: "canRead",
|
|
627
|
+
can_create: "canCreate",
|
|
628
|
+
can_edit: "canEdit"
|
|
629
|
+
};
|
|
630
|
+
var ABILITY_TYPE_MAP = {
|
|
631
|
+
data_access: "integration:data_access",
|
|
632
|
+
file_access: "integration:file_access",
|
|
633
|
+
escalation: "oversight:escalation"
|
|
634
|
+
};
|
|
635
|
+
var AbilityDef = class {
|
|
636
|
+
constructor(abilityType, options = {}) {
|
|
637
|
+
this.abilityType = abilityType;
|
|
638
|
+
this.config = options.config ?? {};
|
|
639
|
+
this.enabled = options.enabled ?? true;
|
|
640
|
+
this.celCondition = options.celCondition ?? options.cel_condition;
|
|
641
|
+
this.form = options.form;
|
|
642
|
+
for (const [inputKey, outputKey] of Object.entries(PERM_KEYS)) {
|
|
643
|
+
if (inputKey in options) this.perms[outputKey] = options[inputKey];
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
uuid = crypto.randomUUID();
|
|
647
|
+
config;
|
|
648
|
+
enabled;
|
|
649
|
+
celCondition;
|
|
650
|
+
form;
|
|
651
|
+
perms = {};
|
|
652
|
+
toAbility() {
|
|
653
|
+
const config = { ...this.config };
|
|
654
|
+
if (this.form?._uuid) {
|
|
655
|
+
config.dataTypeUuid = this.form._uuid;
|
|
656
|
+
Object.assign(config, this.perms);
|
|
657
|
+
}
|
|
658
|
+
return {
|
|
659
|
+
uuid: this.uuid,
|
|
660
|
+
abilityType: ABILITY_TYPE_MAP[this.abilityType] ?? this.abilityType,
|
|
661
|
+
config,
|
|
662
|
+
enabled: this.enabled,
|
|
663
|
+
...this.celCondition ? { celCondition: this.celCondition } : {}
|
|
664
|
+
};
|
|
665
|
+
}
|
|
666
|
+
};
|
|
667
|
+
var TaskBuilder = class extends WorkspaceBuilderResource {
|
|
668
|
+
constructor(name, workflow, prompt = "", start = false, description = "") {
|
|
669
|
+
super();
|
|
670
|
+
this.name = name;
|
|
671
|
+
this.workflow = workflow;
|
|
672
|
+
this.prompt = prompt;
|
|
673
|
+
this.start = start;
|
|
674
|
+
this.description = description;
|
|
675
|
+
}
|
|
676
|
+
transitions = [];
|
|
677
|
+
eventRules = [];
|
|
678
|
+
transitionsTo(child, when, autoNext = false, required = false) {
|
|
679
|
+
if (child === this) {
|
|
680
|
+
throw new Error(`Task '${this.name}' cannot transition to itself`);
|
|
681
|
+
}
|
|
682
|
+
this.transitions.push({ parent: this, child, when, autoNext, required });
|
|
683
|
+
return this;
|
|
684
|
+
}
|
|
685
|
+
onEnter(action, params = {}, name) {
|
|
686
|
+
this.eventRules.push({
|
|
687
|
+
eventType: "task:execute" /* TASK_EXECUTE */,
|
|
688
|
+
actionType: action,
|
|
689
|
+
actionParams: params,
|
|
690
|
+
name
|
|
691
|
+
});
|
|
692
|
+
return this;
|
|
693
|
+
}
|
|
694
|
+
initialMessage(message, suggestions) {
|
|
695
|
+
return this.onEnter("assistant:message", {
|
|
696
|
+
message,
|
|
697
|
+
...suggestions ? { suggestions } : {}
|
|
698
|
+
});
|
|
699
|
+
}
|
|
700
|
+
planLine() {
|
|
701
|
+
return `Task: ${this.name}${this.start ? " [start]" : ""}`;
|
|
702
|
+
}
|
|
703
|
+
async apply(client) {
|
|
704
|
+
if (this._existed || this.workflow._id === void 0) return;
|
|
705
|
+
const result = await client.createTask({
|
|
706
|
+
workflowId: this.workflow._id,
|
|
707
|
+
name: this.name,
|
|
708
|
+
prompt: this.prompt,
|
|
709
|
+
description: this.description
|
|
710
|
+
});
|
|
711
|
+
this._id = result.id;
|
|
712
|
+
this._uuid = result.uuid;
|
|
713
|
+
this._created = true;
|
|
714
|
+
}
|
|
715
|
+
};
|
|
716
|
+
var WorkflowBuilder = class extends WorkspaceBuilderResource {
|
|
717
|
+
constructor(name, options = {}) {
|
|
718
|
+
super();
|
|
719
|
+
this.name = name;
|
|
720
|
+
this.description = options.description ?? "";
|
|
721
|
+
this.prompt = options.prompt;
|
|
722
|
+
this.agentId = options.agentId ?? options.agent_id;
|
|
723
|
+
}
|
|
724
|
+
description;
|
|
725
|
+
prompt;
|
|
726
|
+
agentId;
|
|
727
|
+
tasks = [];
|
|
728
|
+
abilities = [];
|
|
729
|
+
task(name, options = {}) {
|
|
730
|
+
const task = new TaskBuilder(
|
|
731
|
+
name,
|
|
732
|
+
this,
|
|
733
|
+
options.prompt ?? "",
|
|
734
|
+
options.start ?? false,
|
|
735
|
+
options.description ?? ""
|
|
736
|
+
);
|
|
737
|
+
this.tasks.push(task);
|
|
738
|
+
return task;
|
|
739
|
+
}
|
|
740
|
+
ability(abilityType, options = {}) {
|
|
741
|
+
this.abilities.push(new AbilityDef(abilityType, options));
|
|
742
|
+
return this;
|
|
743
|
+
}
|
|
744
|
+
get startTask() {
|
|
745
|
+
return this.tasks.find((task) => task.start) ?? this.tasks[0];
|
|
746
|
+
}
|
|
747
|
+
matchKey() {
|
|
748
|
+
return this.name;
|
|
749
|
+
}
|
|
750
|
+
adopt(existing) {
|
|
751
|
+
super.adopt(existing);
|
|
752
|
+
const start = this.startTask;
|
|
753
|
+
if (start && existing.startTaskId) {
|
|
754
|
+
start._id = existing.startTaskId;
|
|
755
|
+
start._existed = true;
|
|
756
|
+
}
|
|
757
|
+
}
|
|
758
|
+
planLine() {
|
|
759
|
+
const details = [];
|
|
760
|
+
if (this.tasks.length) details.push(`${this.tasks.length} tasks`);
|
|
761
|
+
if (this.abilities.length) details.push(`${this.abilities.length} abilities`);
|
|
762
|
+
return `Workflow: ${this.name}${details.length ? ` (${details.join(", ")})` : ""}`;
|
|
763
|
+
}
|
|
764
|
+
async apply(client) {
|
|
765
|
+
if (this._existed) return;
|
|
766
|
+
const payload = {
|
|
767
|
+
name: this.name,
|
|
768
|
+
description: this.description,
|
|
769
|
+
createMode: "taskless",
|
|
770
|
+
...this.prompt ? { prompt: this.prompt } : {},
|
|
771
|
+
...this.agentId ? { agentId: this.agentId } : {},
|
|
772
|
+
...this.abilities.length ? { abilities: this.abilities.map((ability) => ability.toAbility()) } : {}
|
|
773
|
+
};
|
|
774
|
+
const result = await client.createWorkflow(payload);
|
|
775
|
+
this._id = result.id;
|
|
776
|
+
this._uuid = result.uuid;
|
|
777
|
+
this._created = true;
|
|
778
|
+
}
|
|
779
|
+
};
|
|
780
|
+
var AgentBuilder = class extends WorkspaceBuilderResource {
|
|
781
|
+
constructor(name, options = {}) {
|
|
782
|
+
super();
|
|
783
|
+
this.name = name;
|
|
784
|
+
this.chatModelId = options.chatModelId ?? options.chat_model_id ?? "";
|
|
785
|
+
this.systemPrompt = options.systemPrompt ?? options.system_prompt ?? "";
|
|
786
|
+
this.memberRoleId = options.roleId ?? options.role_id;
|
|
787
|
+
this.tools = options.tools ?? [];
|
|
788
|
+
this.codeInterpreterEnabled = options.codeInterpreterEnabled ?? options.code_interpreter_enabled ?? false;
|
|
789
|
+
}
|
|
790
|
+
chatModelId;
|
|
791
|
+
systemPrompt;
|
|
792
|
+
memberRoleId;
|
|
793
|
+
tools;
|
|
794
|
+
codeInterpreterEnabled;
|
|
795
|
+
planLine() {
|
|
796
|
+
const parts = [];
|
|
797
|
+
if (this.chatModelId) parts.push(`model=${this.chatModelId}`);
|
|
798
|
+
if (this.tools.length) parts.push(`${this.tools.length} tools`);
|
|
799
|
+
return `Agent: ${this.name}${parts.length ? ` (${parts.join(", ")})` : ""}`;
|
|
800
|
+
}
|
|
801
|
+
async apply(client) {
|
|
802
|
+
if (this.memberRoleId === void 0) {
|
|
803
|
+
throw new Error(`Agent '${this.name}' requires memberRoleId`);
|
|
804
|
+
}
|
|
805
|
+
if (!this.chatModelId) {
|
|
806
|
+
throw new Error(`Agent '${this.name}' requires chatModelId`);
|
|
807
|
+
}
|
|
808
|
+
const payload = {
|
|
809
|
+
name: this.name,
|
|
810
|
+
memberRoleId: this.memberRoleId,
|
|
811
|
+
chatModelId: this.chatModelId,
|
|
812
|
+
systemPrompt: this.systemPrompt,
|
|
813
|
+
...this.tools.length ? { tools: this.tools } : {},
|
|
814
|
+
...this.codeInterpreterEnabled ? { codeInterpreterEnabled: true } : {}
|
|
815
|
+
};
|
|
816
|
+
const result = await client.createAgent(payload);
|
|
817
|
+
this._id = result.id;
|
|
818
|
+
this._uuid = result.uuid;
|
|
819
|
+
this._created = true;
|
|
820
|
+
}
|
|
821
|
+
};
|
|
822
|
+
|
|
823
|
+
// src/workspace-builder/orchestrator.ts
|
|
824
|
+
var PHASES = /* @__PURE__ */ new Map([
|
|
825
|
+
[FormBuilder, 0],
|
|
826
|
+
[MemberLabelBuilder, 0],
|
|
827
|
+
[AgentBuilder, 1],
|
|
828
|
+
[WorkflowBuilder, 2],
|
|
829
|
+
[TaskBuilder, 3],
|
|
830
|
+
[RoleUpdate, 4],
|
|
831
|
+
[MemberFilterBuilder, 5],
|
|
832
|
+
[RoutineBuilder, 6],
|
|
833
|
+
[NotificationRuleBuilder, 6],
|
|
834
|
+
[JourneyBuilder, 7],
|
|
835
|
+
[JourneyStepBuilder, 8]
|
|
836
|
+
]);
|
|
837
|
+
var WorkspaceBuilder = class {
|
|
838
|
+
constructor(client) {
|
|
839
|
+
this.client = client;
|
|
840
|
+
}
|
|
841
|
+
resources = [];
|
|
842
|
+
form(name, options = {}) {
|
|
843
|
+
const form = new FormBuilder(name, options.slug, options);
|
|
844
|
+
this.resources.push(form);
|
|
845
|
+
return form;
|
|
846
|
+
}
|
|
847
|
+
workflow(name, options = {}) {
|
|
848
|
+
const workflow = new WorkflowBuilder(name, options);
|
|
849
|
+
this.resources.push(workflow);
|
|
850
|
+
return workflow;
|
|
851
|
+
}
|
|
852
|
+
label(name, options = {}) {
|
|
853
|
+
const label = new MemberLabelBuilder(
|
|
854
|
+
name,
|
|
855
|
+
options.slug,
|
|
856
|
+
options.color,
|
|
857
|
+
options.description ?? ""
|
|
858
|
+
);
|
|
859
|
+
this.resources.push(label);
|
|
860
|
+
return label;
|
|
861
|
+
}
|
|
862
|
+
journey(name, options = {}) {
|
|
863
|
+
const journey = new JourneyBuilder(name, options);
|
|
864
|
+
this.resources.push(journey);
|
|
865
|
+
return journey;
|
|
866
|
+
}
|
|
867
|
+
agent(name, options = {}) {
|
|
868
|
+
const agent = new AgentBuilder(name, options);
|
|
869
|
+
this.resources.push(agent);
|
|
870
|
+
return agent;
|
|
871
|
+
}
|
|
872
|
+
routine(name, cronSchedule = "", options = {}) {
|
|
873
|
+
const routine = new RoutineBuilder(name, cronSchedule, options);
|
|
874
|
+
this.resources.push(routine);
|
|
875
|
+
return routine;
|
|
876
|
+
}
|
|
877
|
+
memberFilter(name, options = {}) {
|
|
878
|
+
const filter = new MemberFilterBuilder(name, options);
|
|
879
|
+
this.resources.push(filter);
|
|
880
|
+
return filter;
|
|
881
|
+
}
|
|
882
|
+
notificationRule(name, options = {}) {
|
|
883
|
+
const rule = new NotificationRuleBuilder(name, options);
|
|
884
|
+
this.resources.push(rule);
|
|
885
|
+
return rule;
|
|
886
|
+
}
|
|
887
|
+
roleUpdate(roleId, workflowIds, dataTypeIds) {
|
|
888
|
+
const update = new RoleUpdate(roleId, workflowIds, dataTypeIds);
|
|
889
|
+
this.resources.push(update);
|
|
890
|
+
return update;
|
|
891
|
+
}
|
|
892
|
+
plan() {
|
|
893
|
+
const resources = this.sortedResources();
|
|
894
|
+
writeLine(`
|
|
895
|
+
${"=".repeat(50)}`);
|
|
896
|
+
writeLine("Plan");
|
|
897
|
+
writeLine("=".repeat(50));
|
|
898
|
+
for (const resource of resources) {
|
|
899
|
+
writeLine(` + ${resource.planLine()}`);
|
|
900
|
+
}
|
|
901
|
+
const wiring = this.collectWiringSummary(resources);
|
|
902
|
+
if (wiring.length) {
|
|
903
|
+
writeLine("\n Wiring:");
|
|
904
|
+
for (const line of wiring) writeLine(` ${line}`);
|
|
905
|
+
}
|
|
906
|
+
writeLine(`
|
|
907
|
+
Total: ${resources.length} resources, ${wiring.length} connections`);
|
|
908
|
+
writeLine(`${"=".repeat(50)}
|
|
909
|
+
`);
|
|
910
|
+
}
|
|
911
|
+
async preview() {
|
|
912
|
+
const resources = this.sortedResources();
|
|
913
|
+
await this.resolveExisting(resources);
|
|
914
|
+
writeLine(`
|
|
915
|
+
${"=".repeat(50)}`);
|
|
916
|
+
writeLine("Preview");
|
|
917
|
+
writeLine("=".repeat(50));
|
|
918
|
+
for (const resource of resources) {
|
|
919
|
+
if (resource._existed) {
|
|
920
|
+
writeLine(` = ${resource.planLine()} (exists, id=${resource._id})`);
|
|
921
|
+
} else {
|
|
922
|
+
writeLine(` + ${resource.planLine()}`);
|
|
923
|
+
}
|
|
924
|
+
}
|
|
925
|
+
const wiring = this.collectWiringSummary(resources);
|
|
926
|
+
if (wiring.length) {
|
|
927
|
+
writeLine("\n Wiring:");
|
|
928
|
+
for (const line of wiring) writeLine(` ${line}`);
|
|
929
|
+
}
|
|
930
|
+
const toCreate = resources.filter((resource) => !resource._existed);
|
|
931
|
+
const existing = resources.filter((resource) => resource._existed);
|
|
932
|
+
writeLine(
|
|
933
|
+
`
|
|
934
|
+
Total: ${toCreate.length} to create, ${existing.length} existing, ${wiring.length} connections`
|
|
935
|
+
);
|
|
936
|
+
writeLine(`${"=".repeat(50)}
|
|
937
|
+
`);
|
|
938
|
+
}
|
|
939
|
+
async apply() {
|
|
940
|
+
const resources = this.sortedResources();
|
|
941
|
+
await this.resolveExisting(resources);
|
|
942
|
+
const summary = {};
|
|
943
|
+
writeLine("Applying...");
|
|
944
|
+
for (const resource of resources) {
|
|
945
|
+
try {
|
|
946
|
+
if (resource._existed) {
|
|
947
|
+
const key = resourceKey(resource);
|
|
948
|
+
summary[key] ??= [];
|
|
949
|
+
summary[key].push(resource._id);
|
|
950
|
+
writeLine(` Exists ${resource.planLine()} -> id=${resource._id}`);
|
|
951
|
+
continue;
|
|
952
|
+
}
|
|
953
|
+
await resource.apply(this.client);
|
|
954
|
+
if (resource._created || resource._id !== void 0) {
|
|
955
|
+
const key = resourceKey(resource);
|
|
956
|
+
summary[key] ??= [];
|
|
957
|
+
summary[key].push(resource._id);
|
|
958
|
+
const startTag = resource instanceof TaskBuilder && resource.start ? " [start]" : "";
|
|
959
|
+
writeLine(` Created ${resource.planLine()}${startTag} -> id=${resource._id}`);
|
|
960
|
+
} else {
|
|
961
|
+
writeLine(` Skipped: ${resource.planLine()}`);
|
|
962
|
+
}
|
|
963
|
+
} catch (err) {
|
|
964
|
+
writeLine(` FAILED: ${resource.planLine()}: ${summarizeError(err)}`);
|
|
965
|
+
throw new ApplyError(resource, err, summary);
|
|
966
|
+
}
|
|
967
|
+
}
|
|
968
|
+
await this.applyTransitions();
|
|
969
|
+
await this.applyEventRules(resources);
|
|
970
|
+
await this.applyJourneyConnections(resources);
|
|
971
|
+
await this.applyJourneyGoals(resources);
|
|
972
|
+
const count = Object.values(summary).reduce((total, ids) => total + ids.length, 0);
|
|
973
|
+
writeLine(`
|
|
974
|
+
Done! Created ${count} resources.`);
|
|
975
|
+
return summary;
|
|
976
|
+
}
|
|
977
|
+
async resolveExisting(resources) {
|
|
978
|
+
const formSlugs = new Map(
|
|
979
|
+
resources.filter((resource) => resource instanceof FormBuilder).map((resource) => [resource.slug, resource])
|
|
980
|
+
);
|
|
981
|
+
const labelSlugs = new Map(
|
|
982
|
+
resources.filter(
|
|
983
|
+
(resource) => resource instanceof MemberLabelBuilder
|
|
984
|
+
).map((resource) => [resource.slug, resource])
|
|
985
|
+
);
|
|
986
|
+
const workflowNames = new Map(
|
|
987
|
+
resources.filter((resource) => resource instanceof WorkflowBuilder).map((resource) => [resource.name, resource])
|
|
988
|
+
);
|
|
989
|
+
if (formSlugs.size) {
|
|
990
|
+
for (const existing of await this.client.getDataTypes()) {
|
|
991
|
+
if (existing.slug && formSlugs.has(existing.slug)) {
|
|
992
|
+
formSlugs.get(existing.slug).adopt(existing);
|
|
993
|
+
}
|
|
994
|
+
}
|
|
995
|
+
}
|
|
996
|
+
if (labelSlugs.size) {
|
|
997
|
+
for (const existing of await this.client.getMemberLabels()) {
|
|
998
|
+
if (existing.slug && labelSlugs.has(existing.slug)) {
|
|
999
|
+
labelSlugs.get(existing.slug).adopt(existing);
|
|
1000
|
+
}
|
|
1001
|
+
}
|
|
1002
|
+
}
|
|
1003
|
+
if (!workflowNames.size) return;
|
|
1004
|
+
for (const existing of await this.client.getWorkflows()) {
|
|
1005
|
+
if (existing.name && workflowNames.has(existing.name)) {
|
|
1006
|
+
workflowNames.get(existing.name).adopt(existing);
|
|
1007
|
+
}
|
|
1008
|
+
}
|
|
1009
|
+
const adoptedWorkflows = resources.filter(
|
|
1010
|
+
(resource) => resource instanceof WorkflowBuilder && resource._existed && resource._id !== void 0
|
|
1011
|
+
);
|
|
1012
|
+
if (!adoptedWorkflows.length) return;
|
|
1013
|
+
const existingTasks = await this.client.getTasks();
|
|
1014
|
+
for (const workflow of adoptedWorkflows) {
|
|
1015
|
+
const workflowTasks = existingTasks.filter((task) => task.workflowId === workflow._id);
|
|
1016
|
+
const taskByName = new Map(workflowTasks.map((task) => [task.name, task]));
|
|
1017
|
+
for (const taskBuilder of workflow.tasks) {
|
|
1018
|
+
const match = taskByName.get(taskBuilder.name);
|
|
1019
|
+
if (match) taskBuilder.adopt(match);
|
|
1020
|
+
}
|
|
1021
|
+
const ids = /* @__PURE__ */ new Map();
|
|
1022
|
+
for (const taskBuilder of workflow.tasks) {
|
|
1023
|
+
if (taskBuilder._id !== void 0) {
|
|
1024
|
+
ids.set(taskBuilder._id, [...ids.get(taskBuilder._id) ?? [], taskBuilder]);
|
|
1025
|
+
}
|
|
1026
|
+
}
|
|
1027
|
+
for (const builders of ids.values()) {
|
|
1028
|
+
if (builders.length <= 1) continue;
|
|
1029
|
+
const nameAdopted = builders.filter((task) => taskByName.has(task.name));
|
|
1030
|
+
for (const task of builders.filter((builder) => !nameAdopted.includes(builder))) {
|
|
1031
|
+
task._id = void 0;
|
|
1032
|
+
task._existed = false;
|
|
1033
|
+
}
|
|
1034
|
+
}
|
|
1035
|
+
}
|
|
1036
|
+
}
|
|
1037
|
+
sortedResources() {
|
|
1038
|
+
const all = [];
|
|
1039
|
+
for (const resource of this.resources) {
|
|
1040
|
+
all.push(resource);
|
|
1041
|
+
if (resource instanceof WorkflowBuilder) {
|
|
1042
|
+
const start = resource.startTask;
|
|
1043
|
+
const tasks = start === void 0 ? resource.tasks : [start, ...resource.tasks.filter((task) => task !== start)];
|
|
1044
|
+
all.push(...tasks);
|
|
1045
|
+
} else if (resource instanceof JourneyBuilder) {
|
|
1046
|
+
all.push(...resource.steps);
|
|
1047
|
+
}
|
|
1048
|
+
}
|
|
1049
|
+
return all.sort(
|
|
1050
|
+
(a, b) => (PHASES.get(a.constructor) ?? 99) - (PHASES.get(b.constructor) ?? 99)
|
|
1051
|
+
);
|
|
1052
|
+
}
|
|
1053
|
+
collectWiringSummary(resources) {
|
|
1054
|
+
const lines = [];
|
|
1055
|
+
for (const resource of resources) {
|
|
1056
|
+
if (resource instanceof TaskBuilder) {
|
|
1057
|
+
for (const transition of resource.transitions) {
|
|
1058
|
+
lines.push(`${transition.parent.name} -> ${transition.child.name}`);
|
|
1059
|
+
}
|
|
1060
|
+
for (const rule of resource.eventRules) {
|
|
1061
|
+
lines.push(`${rule.actionType} on enter '${resource.name}'`);
|
|
1062
|
+
}
|
|
1063
|
+
} else if (resource instanceof FormBuilder) {
|
|
1064
|
+
for (const rule of resource.eventRules) {
|
|
1065
|
+
lines.push(`${rule.actionType} on ${rule.eventType} '${resource.name}'`);
|
|
1066
|
+
}
|
|
1067
|
+
} else if (resource instanceof RoutineBuilder) {
|
|
1068
|
+
for (const rule of resource.rules)
|
|
1069
|
+
lines.push(`${rule.actionType} rule on '${resource.name}'`);
|
|
1070
|
+
} else if (resource instanceof JourneyStepBuilder) {
|
|
1071
|
+
for (const connection of resource.connections) {
|
|
1072
|
+
lines.push(`${connection.parent.name} -> ${connection.child.name}`);
|
|
1073
|
+
}
|
|
1074
|
+
} else if (resource instanceof JourneyBuilder) {
|
|
1075
|
+
for (const goal of resource.goals) lines.push(`Goal: ${goal.name}`);
|
|
1076
|
+
}
|
|
1077
|
+
}
|
|
1078
|
+
return lines;
|
|
1079
|
+
}
|
|
1080
|
+
async applyTransitions() {
|
|
1081
|
+
const parentChildren = /* @__PURE__ */ new Map();
|
|
1082
|
+
for (const resource of this.sortedResources()) {
|
|
1083
|
+
if (!(resource instanceof TaskBuilder)) continue;
|
|
1084
|
+
for (const transition of resource.transitions) {
|
|
1085
|
+
parentChildren.set(transition.parent, [
|
|
1086
|
+
...parentChildren.get(transition.parent) ?? [],
|
|
1087
|
+
transition
|
|
1088
|
+
]);
|
|
1089
|
+
}
|
|
1090
|
+
}
|
|
1091
|
+
for (const [parent, transitions] of parentChildren) {
|
|
1092
|
+
if (parent._id === void 0) continue;
|
|
1093
|
+
const childTasks = transitions.flatMap((transition) => {
|
|
1094
|
+
const taskId = transition.child._id;
|
|
1095
|
+
if (taskId === void 0 || taskId === parent._id) return [];
|
|
1096
|
+
return [
|
|
1097
|
+
{
|
|
1098
|
+
taskId,
|
|
1099
|
+
choicePrompt: transition.when,
|
|
1100
|
+
celCondition: null,
|
|
1101
|
+
autoNext: transition.autoNext,
|
|
1102
|
+
required: transition.required
|
|
1103
|
+
}
|
|
1104
|
+
];
|
|
1105
|
+
});
|
|
1106
|
+
if (!childTasks.length) continue;
|
|
1107
|
+
await this.client.updateTask(parent._id, { childTasks });
|
|
1108
|
+
writeLine(
|
|
1109
|
+
` Wired: ${parent.name} -> [${transitions.map((tr) => tr.child.name).join(", ")}]`
|
|
1110
|
+
);
|
|
1111
|
+
}
|
|
1112
|
+
}
|
|
1113
|
+
async applyEventRules(resources) {
|
|
1114
|
+
for (const resource of resources) {
|
|
1115
|
+
if (resource instanceof TaskBuilder) {
|
|
1116
|
+
for (const rule of resource.eventRules) {
|
|
1117
|
+
try {
|
|
1118
|
+
const payload = {
|
|
1119
|
+
objectType: "task" /* TASK */,
|
|
1120
|
+
taskId: resource._id,
|
|
1121
|
+
eventType: rule.eventType,
|
|
1122
|
+
actionType: rule.actionType,
|
|
1123
|
+
actionParams: resolveRefs(rule.actionParams),
|
|
1124
|
+
...rule.name ? { name: rule.name } : {}
|
|
1125
|
+
};
|
|
1126
|
+
await this.applyEventRule(
|
|
1127
|
+
payload,
|
|
1128
|
+
`${rule.actionType} on enter '${resource.name}'`
|
|
1129
|
+
);
|
|
1130
|
+
} catch (err) {
|
|
1131
|
+
handleEventRuleApplyFailure(resource.name, err);
|
|
1132
|
+
}
|
|
1133
|
+
}
|
|
1134
|
+
} else if (resource instanceof FormBuilder) {
|
|
1135
|
+
for (const rule of resource.eventRules) {
|
|
1136
|
+
try {
|
|
1137
|
+
const payload = {
|
|
1138
|
+
objectType: "data_record" /* DATA_RECORD */,
|
|
1139
|
+
dataTypeId: resource._id,
|
|
1140
|
+
eventType: rule.eventType,
|
|
1141
|
+
actionType: rule.actionType,
|
|
1142
|
+
actionParams: resolveRefs(rule.actionParams),
|
|
1143
|
+
...rule.condition ? { conditions: rule.condition } : {},
|
|
1144
|
+
...rule.name ? { name: rule.name } : {}
|
|
1145
|
+
};
|
|
1146
|
+
await this.applyEventRule(
|
|
1147
|
+
payload,
|
|
1148
|
+
`${rule.actionType} on ${rule.eventType} '${resource.name}'`
|
|
1149
|
+
);
|
|
1150
|
+
} catch (err) {
|
|
1151
|
+
handleEventRuleApplyFailure(resource.name, err);
|
|
1152
|
+
}
|
|
1153
|
+
}
|
|
1154
|
+
}
|
|
1155
|
+
}
|
|
1156
|
+
}
|
|
1157
|
+
async applyEventRule(payload, label) {
|
|
1158
|
+
const existing = await this.findMatchingEventRule(payload);
|
|
1159
|
+
if (!existing) {
|
|
1160
|
+
await this.client.createEventRule(payload);
|
|
1161
|
+
writeLine(` Wired: ${label}`);
|
|
1162
|
+
return;
|
|
1163
|
+
}
|
|
1164
|
+
const update = eventRuleUpdate(existing, payload);
|
|
1165
|
+
if (Object.keys(update).length) {
|
|
1166
|
+
await this.client.updateEventRule(existing.id, update);
|
|
1167
|
+
writeLine(` Updated: ${label}`);
|
|
1168
|
+
} else {
|
|
1169
|
+
writeLine(` Exists: ${label}`);
|
|
1170
|
+
}
|
|
1171
|
+
}
|
|
1172
|
+
async findMatchingEventRule(payload) {
|
|
1173
|
+
const sourceFilter = eventRuleSourceFilter(payload);
|
|
1174
|
+
const candidates = await this.client.searchEventRules({
|
|
1175
|
+
objectType: payload.objectType,
|
|
1176
|
+
eventType: payload.eventType,
|
|
1177
|
+
actionType: payload.actionType,
|
|
1178
|
+
includeDisabled: true,
|
|
1179
|
+
...sourceFilter
|
|
1180
|
+
});
|
|
1181
|
+
return candidates.find((candidate) => eventRulesMatch(candidate, payload));
|
|
1182
|
+
}
|
|
1183
|
+
async applyJourneyConnections(resources) {
|
|
1184
|
+
for (const resource of resources) {
|
|
1185
|
+
if (!(resource instanceof JourneyStepBuilder)) continue;
|
|
1186
|
+
for (const connection of resource.connections) {
|
|
1187
|
+
if (resource.journey._id === void 0) continue;
|
|
1188
|
+
const payload = buildJourneyConnectionPayload(connection);
|
|
1189
|
+
if (!payload) continue;
|
|
1190
|
+
await this.client.createConnection(resource.journey._id, payload);
|
|
1191
|
+
writeLine(` Wired: ${connection.parent.name} -> ${connection.child.name}`);
|
|
1192
|
+
}
|
|
1193
|
+
}
|
|
1194
|
+
}
|
|
1195
|
+
async applyJourneyGoals(resources) {
|
|
1196
|
+
for (const resource of resources) {
|
|
1197
|
+
if (!(resource instanceof JourneyBuilder) || resource._id === void 0) continue;
|
|
1198
|
+
for (const goal of resource.goals) {
|
|
1199
|
+
await this.client.createGoal(resource._id, buildJourneyGoalPayload(goal));
|
|
1200
|
+
writeLine(` Created goal: ${goal.name}`);
|
|
1201
|
+
}
|
|
1202
|
+
}
|
|
1203
|
+
}
|
|
1204
|
+
};
|
|
1205
|
+
var ApplyError = class extends Error {
|
|
1206
|
+
constructor(resource, cause, createdSoFar) {
|
|
1207
|
+
super(
|
|
1208
|
+
`Failed creating ${resource.planLine()}: ${summarizeError(cause)}
|
|
1209
|
+
Already created: ${JSON.stringify(
|
|
1210
|
+
createdSoFar
|
|
1211
|
+
)}`
|
|
1212
|
+
);
|
|
1213
|
+
this.resource = resource;
|
|
1214
|
+
this.cause = cause;
|
|
1215
|
+
this.createdSoFar = createdSoFar;
|
|
1216
|
+
}
|
|
1217
|
+
};
|
|
1218
|
+
function resourceKey(resource) {
|
|
1219
|
+
if (resource instanceof RoleUpdate) return "roleUpdates";
|
|
1220
|
+
if (resource instanceof FormBuilder) return "forms";
|
|
1221
|
+
if (resource instanceof MemberLabelBuilder) return "labels";
|
|
1222
|
+
if (resource instanceof WorkflowBuilder) return "workflows";
|
|
1223
|
+
if (resource instanceof TaskBuilder) return "tasks";
|
|
1224
|
+
if (resource instanceof MemberFilterBuilder) return "memberFilters";
|
|
1225
|
+
if (resource instanceof RoutineBuilder) return "routines";
|
|
1226
|
+
if (resource instanceof NotificationRuleBuilder) return "notificationRules";
|
|
1227
|
+
if (resource instanceof JourneyBuilder) return "journeys";
|
|
1228
|
+
if (resource instanceof JourneyStepBuilder) return "journeySteps";
|
|
1229
|
+
if (resource instanceof AgentBuilder) return "agents";
|
|
1230
|
+
return "resources";
|
|
1231
|
+
}
|
|
1232
|
+
var SEARCHABLE_EVENT_RULE_SOURCE_FIELDS = [
|
|
1233
|
+
"taskId",
|
|
1234
|
+
"dataTypeId",
|
|
1235
|
+
"routineId",
|
|
1236
|
+
"workflowId",
|
|
1237
|
+
"journeyStepId"
|
|
1238
|
+
];
|
|
1239
|
+
var EVENT_RULE_SOURCE_FIELDS = [
|
|
1240
|
+
...SEARCHABLE_EVENT_RULE_SOURCE_FIELDS,
|
|
1241
|
+
"calendarId",
|
|
1242
|
+
"calendarEventTypeId"
|
|
1243
|
+
];
|
|
1244
|
+
function eventRuleSourceFilter(payload) {
|
|
1245
|
+
const filter = {};
|
|
1246
|
+
for (const field of SEARCHABLE_EVENT_RULE_SOURCE_FIELDS) {
|
|
1247
|
+
const value = payload[field];
|
|
1248
|
+
if (value !== void 0 && value !== null) {
|
|
1249
|
+
filter[field] = value;
|
|
1250
|
+
}
|
|
1251
|
+
}
|
|
1252
|
+
return filter;
|
|
1253
|
+
}
|
|
1254
|
+
function eventRulesMatch(existing, payload) {
|
|
1255
|
+
if (normalizeString(existing.name) !== normalizeString(payload.name)) return false;
|
|
1256
|
+
if (existing.objectType !== payload.objectType) return false;
|
|
1257
|
+
if (existing.eventType !== payload.eventType) return false;
|
|
1258
|
+
if (existing.actionType !== payload.actionType) return false;
|
|
1259
|
+
for (const field of EVENT_RULE_SOURCE_FIELDS) {
|
|
1260
|
+
if ((existing[field] ?? null) !== (payload[field] ?? null)) return false;
|
|
1261
|
+
}
|
|
1262
|
+
if (normalizeString(payload.name)) return true;
|
|
1263
|
+
if (normalizeString(existing.conditions) !== normalizeString(payload.conditions)) return false;
|
|
1264
|
+
if (!eventRuleValueEqual(existing.actionParams, payload.actionParams ?? null)) return false;
|
|
1265
|
+
if (!eventRuleValueEqual(existing.triggerParams, payload.triggerParams ?? null)) return false;
|
|
1266
|
+
return true;
|
|
1267
|
+
}
|
|
1268
|
+
function eventRuleUpdate(existing, payload) {
|
|
1269
|
+
const update = {};
|
|
1270
|
+
const conditions = payload.conditions ?? null;
|
|
1271
|
+
if (normalizeString(existing.conditions) !== normalizeString(conditions)) {
|
|
1272
|
+
update.conditions = conditions;
|
|
1273
|
+
}
|
|
1274
|
+
const actionParams = payload.actionParams ?? null;
|
|
1275
|
+
if (!eventRuleValueEqual(existing.actionParams, actionParams)) {
|
|
1276
|
+
update.actionParams = payload.actionParams;
|
|
1277
|
+
}
|
|
1278
|
+
const triggerParams = payload.triggerParams ?? null;
|
|
1279
|
+
if (!eventRuleValueEqual(existing.triggerParams, triggerParams)) {
|
|
1280
|
+
update.triggerParams = payload.triggerParams;
|
|
1281
|
+
}
|
|
1282
|
+
const active = payload.active ?? true;
|
|
1283
|
+
if (existing.active !== active) {
|
|
1284
|
+
update.active = active;
|
|
1285
|
+
}
|
|
1286
|
+
const delay = payload.delay ?? 0;
|
|
1287
|
+
if (existing.delay !== delay) {
|
|
1288
|
+
update.delay = delay;
|
|
1289
|
+
}
|
|
1290
|
+
const order = payload.order ?? 0;
|
|
1291
|
+
if (existing.order !== order) {
|
|
1292
|
+
update.order = order;
|
|
1293
|
+
}
|
|
1294
|
+
return update;
|
|
1295
|
+
}
|
|
1296
|
+
function normalizeString(value) {
|
|
1297
|
+
return value ?? "";
|
|
1298
|
+
}
|
|
1299
|
+
function eventRuleValueEqual(left, right) {
|
|
1300
|
+
return JSON.stringify(stableValue(left)) === JSON.stringify(stableValue(right));
|
|
1301
|
+
}
|
|
1302
|
+
function stableValue(value) {
|
|
1303
|
+
if (Array.isArray(value)) return value.map(stableValue);
|
|
1304
|
+
if (!value || typeof value !== "object") return value;
|
|
1305
|
+
const prototype = Object.getPrototypeOf(value);
|
|
1306
|
+
if (prototype !== Object.prototype && prototype !== null) return value;
|
|
1307
|
+
return Object.fromEntries(
|
|
1308
|
+
Object.entries(value).sort(([left], [right]) => left.localeCompare(right)).map(([key, nested]) => [key, stableValue(nested)])
|
|
1309
|
+
);
|
|
1310
|
+
}
|
|
1311
|
+
function writeLine(message = "") {
|
|
1312
|
+
process.stdout.write(`${message}
|
|
1313
|
+
`);
|
|
1314
|
+
}
|
|
1315
|
+
function handleEventRuleApplyFailure(resourceName, err) {
|
|
1316
|
+
if (hasStatus(err, 403)) {
|
|
1317
|
+
writeLine(
|
|
1318
|
+
` WARNING: Event rule on '${resourceName}' failed (retry exec() with scopes=['automations:admin'])`
|
|
1319
|
+
);
|
|
1320
|
+
return;
|
|
1321
|
+
}
|
|
1322
|
+
const summary = summarizeError(err);
|
|
1323
|
+
writeLine(` FAILED: Event rule on '${resourceName}': ${summary}`);
|
|
1324
|
+
throw new Error(`Failed wiring event rule on '${resourceName}': ${summary}`);
|
|
1325
|
+
}
|
|
1326
|
+
function summarizeError(err) {
|
|
1327
|
+
if (err instanceof Error) return err.name || "Error";
|
|
1328
|
+
return typeof err;
|
|
1329
|
+
}
|
|
1330
|
+
function hasStatus(err, status) {
|
|
1331
|
+
if (typeof err === "object" && err !== null) {
|
|
1332
|
+
const candidate = err;
|
|
1333
|
+
if (candidate.status === status || candidate.statusCode === status) return true;
|
|
1334
|
+
}
|
|
1335
|
+
const message = err instanceof Error ? err.message : typeof err === "string" ? err : "";
|
|
1336
|
+
return message.includes(String(status));
|
|
1337
|
+
}
|
|
1338
|
+
async function runWorkspaceScript(client, build, mode = "plan") {
|
|
1339
|
+
const workspace = new WorkspaceBuilder(client);
|
|
1340
|
+
await build(workspace);
|
|
1341
|
+
if (mode === "plan") return workspace.plan();
|
|
1342
|
+
if (mode === "preview") return workspace.preview();
|
|
1343
|
+
if (mode === "apply") return workspace.apply();
|
|
1344
|
+
throw new Error(`Unknown mode '${mode}'. Use plan, preview, or apply.`);
|
|
1345
|
+
}
|
|
139
1346
|
export {
|
|
140
1347
|
AbilityConfigIssueCode,
|
|
141
1348
|
AbilityConfigIssueSeverity,
|
|
@@ -144,7 +1351,9 @@ export {
|
|
|
144
1351
|
AccessGrantStatus,
|
|
145
1352
|
AccessGrantTrigger,
|
|
146
1353
|
AccessLevel,
|
|
1354
|
+
AgentBuilder,
|
|
147
1355
|
ApiError,
|
|
1356
|
+
ApplyError,
|
|
148
1357
|
AppsApi,
|
|
149
1358
|
AttendeeResponseStatus,
|
|
150
1359
|
AttendeeStatus,
|
|
@@ -181,19 +1390,25 @@ export {
|
|
|
181
1390
|
FhirClientType,
|
|
182
1391
|
FhirConnectionState,
|
|
183
1392
|
FhirVendor,
|
|
1393
|
+
FormBuilder,
|
|
184
1394
|
GravityRailClient,
|
|
185
1395
|
IndexStatus,
|
|
186
1396
|
InvitationStatus,
|
|
187
1397
|
JobPosition,
|
|
1398
|
+
JourneyBuilder,
|
|
1399
|
+
JourneyStepBuilder,
|
|
188
1400
|
MASKED_SECRET_SENTINEL,
|
|
189
1401
|
McpServerState,
|
|
190
1402
|
MemberFeature,
|
|
1403
|
+
MemberFilterBuilder,
|
|
1404
|
+
MemberLabelBuilder,
|
|
191
1405
|
MemberType,
|
|
192
1406
|
MenuItemResponseItemType,
|
|
193
1407
|
MenuItemResponseItemType as MenuItemType,
|
|
194
1408
|
ModelProvider,
|
|
195
1409
|
ModelType,
|
|
196
1410
|
NO_ASSISTANT_PARTICIPANT_CHAT_TYPES,
|
|
1411
|
+
NotificationRuleBuilder,
|
|
197
1412
|
OIDCAdminConfigViewProviderType,
|
|
198
1413
|
ORG_ONLY_RESOURCES,
|
|
199
1414
|
OrgApi,
|
|
@@ -219,6 +1434,8 @@ export {
|
|
|
219
1434
|
Region,
|
|
220
1435
|
RegistrationIdentifier,
|
|
221
1436
|
Resource,
|
|
1437
|
+
RoleUpdate,
|
|
1438
|
+
RoutineBuilder,
|
|
222
1439
|
SCOPES,
|
|
223
1440
|
SCOPE_PRESETS,
|
|
224
1441
|
SMS_DELIVERY_FAILURE_STATUSES,
|
|
@@ -238,15 +1455,19 @@ export {
|
|
|
238
1455
|
TTS_MODEL_ELEVENLABS_TURBO,
|
|
239
1456
|
TTS_MODEL_POCKET,
|
|
240
1457
|
TTS_PREVIEW_PROVIDERS,
|
|
1458
|
+
TaskBuilder,
|
|
241
1459
|
TemplatesApi,
|
|
242
1460
|
ToolkitState,
|
|
243
1461
|
WorkflowAccessWarningResourceType,
|
|
1462
|
+
WorkflowBuilder,
|
|
244
1463
|
WorkflowAccessWarningResourceType as WorkflowResourceType,
|
|
1464
|
+
WorkspaceBuilder,
|
|
245
1465
|
WorkspaceFeature,
|
|
246
1466
|
WorkspaceProvisionStatus,
|
|
247
1467
|
areDependenciesSatisfied,
|
|
248
1468
|
canPreviewVoice,
|
|
249
1469
|
clearRegistry,
|
|
1470
|
+
createWorkspaceClient,
|
|
250
1471
|
decodePermissions,
|
|
251
1472
|
encodePermissions,
|
|
252
1473
|
eventRuleActionParams,
|
|
@@ -271,6 +1492,7 @@ export {
|
|
|
271
1492
|
isRegistryInitialized,
|
|
272
1493
|
isSMSDeliveryFailureStatus,
|
|
273
1494
|
requiresAssistantParticipant,
|
|
1495
|
+
runWorkspaceScript,
|
|
274
1496
|
usesBuiltinAssistant,
|
|
275
1497
|
validateFeatureDependencies
|
|
276
1498
|
};
|