@positronic/core 0.0.52 → 0.0.53
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/src/dsl/{loop-messages.js → agent-messages.js} +21 -21
- package/dist/src/dsl/brain-runner.js +6 -6
- package/dist/src/dsl/brain-state-machine.js +2 -2
- package/dist/src/dsl/brain.js +5 -1912
- package/dist/src/dsl/builder/brain.js +944 -0
- package/dist/src/dsl/builder/step.js +75 -0
- package/dist/src/dsl/constants.js +9 -10
- package/dist/src/dsl/create-brain.js +63 -0
- package/dist/src/dsl/definitions/blocks.js +1 -0
- package/dist/src/dsl/definitions/brain-types.js +4 -0
- package/dist/src/dsl/definitions/events.js +2 -0
- package/dist/src/dsl/definitions/run-params.js +1 -0
- package/dist/src/dsl/definitions/steps.js +2 -0
- package/dist/src/dsl/execution/constants.js +14 -0
- package/dist/src/dsl/execution/event-stream.js +1638 -0
- package/dist/src/dsl/execution/retry.js +298 -0
- package/dist/src/dsl/types.js +2 -2
- package/dist/src/index.js +5 -3
- package/dist/src/tools/index.js +181 -0
- package/dist/src/ui/component-utils.js +107 -0
- package/dist/src/ui/generate-page-html.js +36 -0
- package/dist/src/ui/generate-ui.js +601 -0
- package/dist/src/ui/types.js +165 -0
- package/dist/src/ui/validate-form.js +428 -0
- package/dist/src/yaml/data-validator.js +302 -0
- package/dist/src/yaml/index.js +9 -0
- package/dist/src/yaml/parser.js +224 -0
- package/dist/src/yaml/schema-extractor.js +330 -0
- package/dist/src/yaml/type-inference.js +210 -0
- package/dist/src/yaml/types.js +12 -0
- package/dist/types/clients/types.d.ts +42 -0
- package/dist/types/clients/types.d.ts.map +1 -1
- package/dist/types/dsl/agent-messages.d.ts +18 -0
- package/dist/types/dsl/agent-messages.d.ts.map +1 -0
- package/dist/types/dsl/brain-runner.d.ts +2 -2
- package/dist/types/dsl/brain-runner.d.ts.map +1 -1
- package/dist/types/dsl/brain-state-machine.d.ts.map +1 -1
- package/dist/types/dsl/brain.d.ts +7 -273
- package/dist/types/dsl/brain.d.ts.map +1 -1
- package/dist/types/dsl/builder/brain.d.ts +200 -0
- package/dist/types/dsl/builder/brain.d.ts.map +1 -0
- package/dist/types/dsl/builder/step.d.ts +15 -0
- package/dist/types/dsl/builder/step.d.ts.map +1 -0
- package/dist/types/dsl/constants.d.ts +8 -9
- package/dist/types/dsl/constants.d.ts.map +1 -1
- package/dist/types/dsl/create-brain.d.ts +80 -0
- package/dist/types/dsl/create-brain.d.ts.map +1 -0
- package/dist/types/dsl/definitions/blocks.d.ts +62 -0
- package/dist/types/dsl/definitions/blocks.d.ts.map +1 -0
- package/dist/types/dsl/definitions/brain-types.d.ts +33 -0
- package/dist/types/dsl/definitions/brain-types.d.ts.map +1 -0
- package/dist/types/dsl/definitions/events.d.ts +129 -0
- package/dist/types/dsl/definitions/events.d.ts.map +1 -0
- package/dist/types/dsl/definitions/run-params.d.ts +26 -0
- package/dist/types/dsl/definitions/run-params.d.ts.map +1 -0
- package/dist/types/dsl/definitions/steps.d.ts +20 -0
- package/dist/types/dsl/definitions/steps.d.ts.map +1 -0
- package/dist/types/dsl/example-webhook.d.ts +1 -1
- package/dist/types/dsl/example-webhook.d.ts.map +1 -1
- package/dist/types/dsl/execution/constants.d.ts +16 -0
- package/dist/types/dsl/execution/constants.d.ts.map +1 -0
- package/dist/types/dsl/execution/event-stream.d.ts +44 -0
- package/dist/types/dsl/execution/event-stream.d.ts.map +1 -0
- package/dist/types/dsl/execution/retry.d.ts +30 -0
- package/dist/types/dsl/execution/retry.d.ts.map +1 -0
- package/dist/types/dsl/types.d.ts +35 -14
- package/dist/types/dsl/types.d.ts.map +1 -1
- package/dist/types/index.d.ts +9 -7
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/tools/index.d.ts +33 -0
- package/dist/types/tools/index.d.ts.map +1 -0
- package/dist/types/ui/component-utils.d.ts +19 -0
- package/dist/types/ui/component-utils.d.ts.map +1 -0
- package/dist/types/ui/generate-page-html.d.ts +39 -0
- package/dist/types/ui/generate-page-html.d.ts.map +1 -0
- package/dist/types/ui/generate-ui.d.ts +47 -0
- package/dist/types/ui/generate-ui.d.ts.map +1 -0
- package/dist/types/ui/types.d.ts +138 -0
- package/dist/types/ui/types.d.ts.map +1 -0
- package/dist/types/ui/validate-form.d.ts +45 -0
- package/dist/types/ui/validate-form.d.ts.map +1 -0
- package/dist/types/yaml/data-validator.d.ts +35 -0
- package/dist/types/yaml/data-validator.d.ts.map +1 -0
- package/dist/types/yaml/index.d.ts +11 -0
- package/dist/types/yaml/index.d.ts.map +1 -0
- package/dist/types/yaml/parser.d.ts +20 -0
- package/dist/types/yaml/parser.d.ts.map +1 -0
- package/dist/types/yaml/schema-extractor.d.ts +29 -0
- package/dist/types/yaml/schema-extractor.d.ts.map +1 -0
- package/dist/types/yaml/type-inference.d.ts +50 -0
- package/dist/types/yaml/type-inference.d.ts.map +1 -0
- package/dist/types/yaml/types.d.ts +89 -0
- package/dist/types/yaml/types.d.ts.map +1 -0
- package/package.json +4 -2
- package/dist/types/dsl/loop-messages.d.ts +0 -18
- package/dist/types/dsl/loop-messages.d.ts.map +0 -1
|
@@ -0,0 +1,601 @@
|
|
|
1
|
+
function _array_like_to_array(arr, len) {
|
|
2
|
+
if (len == null || len > arr.length) len = arr.length;
|
|
3
|
+
for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
|
|
4
|
+
return arr2;
|
|
5
|
+
}
|
|
6
|
+
function _array_with_holes(arr) {
|
|
7
|
+
if (Array.isArray(arr)) return arr;
|
|
8
|
+
}
|
|
9
|
+
function _array_without_holes(arr) {
|
|
10
|
+
if (Array.isArray(arr)) return _array_like_to_array(arr);
|
|
11
|
+
}
|
|
12
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
|
|
13
|
+
try {
|
|
14
|
+
var info = gen[key](arg);
|
|
15
|
+
var value = info.value;
|
|
16
|
+
} catch (error) {
|
|
17
|
+
reject(error);
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
if (info.done) {
|
|
21
|
+
resolve(value);
|
|
22
|
+
} else {
|
|
23
|
+
Promise.resolve(value).then(_next, _throw);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
function _async_to_generator(fn) {
|
|
27
|
+
return function() {
|
|
28
|
+
var self = this, args = arguments;
|
|
29
|
+
return new Promise(function(resolve, reject) {
|
|
30
|
+
var gen = fn.apply(self, args);
|
|
31
|
+
function _next(value) {
|
|
32
|
+
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
|
|
33
|
+
}
|
|
34
|
+
function _throw(err) {
|
|
35
|
+
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
|
|
36
|
+
}
|
|
37
|
+
_next(undefined);
|
|
38
|
+
});
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
function _instanceof(left, right) {
|
|
42
|
+
if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
|
|
43
|
+
return !!right[Symbol.hasInstance](left);
|
|
44
|
+
} else {
|
|
45
|
+
return left instanceof right;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
function _iterable_to_array(iter) {
|
|
49
|
+
if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
|
|
50
|
+
}
|
|
51
|
+
function _iterable_to_array_limit(arr, i) {
|
|
52
|
+
var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
|
|
53
|
+
if (_i == null) return;
|
|
54
|
+
var _arr = [];
|
|
55
|
+
var _n = true;
|
|
56
|
+
var _d = false;
|
|
57
|
+
var _s, _e;
|
|
58
|
+
try {
|
|
59
|
+
for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
|
|
60
|
+
_arr.push(_s.value);
|
|
61
|
+
if (i && _arr.length === i) break;
|
|
62
|
+
}
|
|
63
|
+
} catch (err) {
|
|
64
|
+
_d = true;
|
|
65
|
+
_e = err;
|
|
66
|
+
} finally{
|
|
67
|
+
try {
|
|
68
|
+
if (!_n && _i["return"] != null) _i["return"]();
|
|
69
|
+
} finally{
|
|
70
|
+
if (_d) throw _e;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return _arr;
|
|
74
|
+
}
|
|
75
|
+
function _non_iterable_rest() {
|
|
76
|
+
throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
77
|
+
}
|
|
78
|
+
function _non_iterable_spread() {
|
|
79
|
+
throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
80
|
+
}
|
|
81
|
+
function _sliced_to_array(arr, i) {
|
|
82
|
+
return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest();
|
|
83
|
+
}
|
|
84
|
+
function _to_consumable_array(arr) {
|
|
85
|
+
return _array_without_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_spread();
|
|
86
|
+
}
|
|
87
|
+
function _unsupported_iterable_to_array(o, minLen) {
|
|
88
|
+
if (!o) return;
|
|
89
|
+
if (typeof o === "string") return _array_like_to_array(o, minLen);
|
|
90
|
+
var n = Object.prototype.toString.call(o).slice(8, -1);
|
|
91
|
+
if (n === "Object" && o.constructor) n = o.constructor.name;
|
|
92
|
+
if (n === "Map" || n === "Set") return Array.from(n);
|
|
93
|
+
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
|
|
94
|
+
}
|
|
95
|
+
function _ts_generator(thisArg, body) {
|
|
96
|
+
var f, y, t, _ = {
|
|
97
|
+
label: 0,
|
|
98
|
+
sent: function() {
|
|
99
|
+
if (t[0] & 1) throw t[1];
|
|
100
|
+
return t[1];
|
|
101
|
+
},
|
|
102
|
+
trys: [],
|
|
103
|
+
ops: []
|
|
104
|
+
}, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
105
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() {
|
|
106
|
+
return this;
|
|
107
|
+
}), g;
|
|
108
|
+
function verb(n) {
|
|
109
|
+
return function(v) {
|
|
110
|
+
return step([
|
|
111
|
+
n,
|
|
112
|
+
v
|
|
113
|
+
]);
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
function step(op) {
|
|
117
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
118
|
+
while(g && (g = 0, op[0] && (_ = 0)), _)try {
|
|
119
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
120
|
+
if (y = 0, t) op = [
|
|
121
|
+
op[0] & 2,
|
|
122
|
+
t.value
|
|
123
|
+
];
|
|
124
|
+
switch(op[0]){
|
|
125
|
+
case 0:
|
|
126
|
+
case 1:
|
|
127
|
+
t = op;
|
|
128
|
+
break;
|
|
129
|
+
case 4:
|
|
130
|
+
_.label++;
|
|
131
|
+
return {
|
|
132
|
+
value: op[1],
|
|
133
|
+
done: false
|
|
134
|
+
};
|
|
135
|
+
case 5:
|
|
136
|
+
_.label++;
|
|
137
|
+
y = op[1];
|
|
138
|
+
op = [
|
|
139
|
+
0
|
|
140
|
+
];
|
|
141
|
+
continue;
|
|
142
|
+
case 7:
|
|
143
|
+
op = _.ops.pop();
|
|
144
|
+
_.trys.pop();
|
|
145
|
+
continue;
|
|
146
|
+
default:
|
|
147
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
|
|
148
|
+
_ = 0;
|
|
149
|
+
continue;
|
|
150
|
+
}
|
|
151
|
+
if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
|
|
152
|
+
_.label = op[1];
|
|
153
|
+
break;
|
|
154
|
+
}
|
|
155
|
+
if (op[0] === 6 && _.label < t[1]) {
|
|
156
|
+
_.label = t[1];
|
|
157
|
+
t = op;
|
|
158
|
+
break;
|
|
159
|
+
}
|
|
160
|
+
if (t && _.label < t[2]) {
|
|
161
|
+
_.label = t[2];
|
|
162
|
+
_.ops.push(op);
|
|
163
|
+
break;
|
|
164
|
+
}
|
|
165
|
+
if (t[2]) _.ops.pop();
|
|
166
|
+
_.trys.pop();
|
|
167
|
+
continue;
|
|
168
|
+
}
|
|
169
|
+
op = body.call(thisArg, _);
|
|
170
|
+
} catch (e) {
|
|
171
|
+
op = [
|
|
172
|
+
6,
|
|
173
|
+
e
|
|
174
|
+
];
|
|
175
|
+
y = 0;
|
|
176
|
+
} finally{
|
|
177
|
+
f = t = 0;
|
|
178
|
+
}
|
|
179
|
+
if (op[0] & 5) throw op[1];
|
|
180
|
+
return {
|
|
181
|
+
value: op[0] ? op[1] : void 0,
|
|
182
|
+
done: true
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
import { v4 as uuidv4 } from 'uuid';
|
|
187
|
+
import { z } from 'zod';
|
|
188
|
+
import { parseTemplate } from '../yaml/parser.js';
|
|
189
|
+
import { inferDataType, validateDataBindings } from '../yaml/data-validator.js';
|
|
190
|
+
import { extractFormSchema, validateAgainstZod } from '../yaml/schema-extractor.js';
|
|
191
|
+
import { describeDataShape } from '../yaml/type-inference.js';
|
|
192
|
+
/**
|
|
193
|
+
* Get the type name from a Zod schema for documentation.
|
|
194
|
+
*/ function getZodTypeName(schema) {
|
|
195
|
+
if (_instanceof(schema, z.ZodString)) return 'string';
|
|
196
|
+
if (_instanceof(schema, z.ZodNumber)) return 'number';
|
|
197
|
+
if (_instanceof(schema, z.ZodBoolean)) return 'boolean';
|
|
198
|
+
if (_instanceof(schema, z.ZodEnum)) {
|
|
199
|
+
var values = schema._def.values;
|
|
200
|
+
return values.map(function(v) {
|
|
201
|
+
return '"'.concat(v, '"');
|
|
202
|
+
}).join(' | ');
|
|
203
|
+
}
|
|
204
|
+
if (_instanceof(schema, z.ZodArray)) {
|
|
205
|
+
return "".concat(getZodTypeName(schema.element), "[]");
|
|
206
|
+
}
|
|
207
|
+
if (_instanceof(schema, z.ZodOptional)) {
|
|
208
|
+
return getZodTypeName(schema.unwrap());
|
|
209
|
+
}
|
|
210
|
+
if (_instanceof(schema, z.ZodDefault)) {
|
|
211
|
+
return getZodTypeName(schema._def.innerType);
|
|
212
|
+
}
|
|
213
|
+
if (_instanceof(schema, z.ZodObject)) {
|
|
214
|
+
return 'object';
|
|
215
|
+
}
|
|
216
|
+
return 'unknown';
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Format a Zod object schema into documentation for the LLM.
|
|
220
|
+
*/ function formatPropsSchema(schema) {
|
|
221
|
+
var shape = schema.shape;
|
|
222
|
+
var lines = [];
|
|
223
|
+
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
|
|
224
|
+
try {
|
|
225
|
+
for(var _iterator = Object.entries(shape)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
|
|
226
|
+
var _step_value = _sliced_to_array(_step.value, 2), key = _step_value[0], fieldSchema = _step_value[1];
|
|
227
|
+
var zodField = fieldSchema;
|
|
228
|
+
var isOptional = _instanceof(zodField, z.ZodOptional) || _instanceof(zodField, z.ZodDefault);
|
|
229
|
+
var typeName = getZodTypeName(zodField);
|
|
230
|
+
var description = zodField._def.description || '';
|
|
231
|
+
var optionalMarker = isOptional ? '?' : '';
|
|
232
|
+
var descPart = description ? " - ".concat(description) : '';
|
|
233
|
+
lines.push(" - ".concat(key).concat(optionalMarker, ": ").concat(typeName).concat(descPart));
|
|
234
|
+
}
|
|
235
|
+
} catch (err) {
|
|
236
|
+
_didIteratorError = true;
|
|
237
|
+
_iteratorError = err;
|
|
238
|
+
} finally{
|
|
239
|
+
try {
|
|
240
|
+
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
241
|
+
_iterator.return();
|
|
242
|
+
}
|
|
243
|
+
} finally{
|
|
244
|
+
if (_didIteratorError) {
|
|
245
|
+
throw _iteratorError;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
return lines.join('\n');
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* Convert a ComponentNode tree to a flat array of Placements.
|
|
253
|
+
*/ function treeToPlacementsRecursive(node, parentId, placements) {
|
|
254
|
+
var id = uuidv4();
|
|
255
|
+
// Convert PropValue to raw values
|
|
256
|
+
var props = {};
|
|
257
|
+
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
|
|
258
|
+
try {
|
|
259
|
+
for(var _iterator = Object.entries(node.props)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
|
|
260
|
+
var _step_value = _sliced_to_array(_step.value, 2), key = _step_value[0], propValue = _step_value[1];
|
|
261
|
+
if (propValue.type === 'binding') {
|
|
262
|
+
props[key] = "{{".concat(propValue.path, "}}");
|
|
263
|
+
} else {
|
|
264
|
+
props[key] = propValue.value;
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
} catch (err) {
|
|
268
|
+
_didIteratorError = true;
|
|
269
|
+
_iteratorError = err;
|
|
270
|
+
} finally{
|
|
271
|
+
try {
|
|
272
|
+
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
273
|
+
_iterator.return();
|
|
274
|
+
}
|
|
275
|
+
} finally{
|
|
276
|
+
if (_didIteratorError) {
|
|
277
|
+
throw _iteratorError;
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
placements.push({
|
|
282
|
+
id: id,
|
|
283
|
+
component: node.component,
|
|
284
|
+
props: props,
|
|
285
|
+
parentId: parentId
|
|
286
|
+
});
|
|
287
|
+
var _iteratorNormalCompletion1 = true, _didIteratorError1 = false, _iteratorError1 = undefined;
|
|
288
|
+
try {
|
|
289
|
+
// Recurse into children
|
|
290
|
+
for(var _iterator1 = node.children[Symbol.iterator](), _step1; !(_iteratorNormalCompletion1 = (_step1 = _iterator1.next()).done); _iteratorNormalCompletion1 = true){
|
|
291
|
+
var child = _step1.value;
|
|
292
|
+
treeToPlacementsRecursive(child, id, placements);
|
|
293
|
+
}
|
|
294
|
+
} catch (err) {
|
|
295
|
+
_didIteratorError1 = true;
|
|
296
|
+
_iteratorError1 = err;
|
|
297
|
+
} finally{
|
|
298
|
+
try {
|
|
299
|
+
if (!_iteratorNormalCompletion1 && _iterator1.return != null) {
|
|
300
|
+
_iterator1.return();
|
|
301
|
+
}
|
|
302
|
+
} finally{
|
|
303
|
+
if (_didIteratorError1) {
|
|
304
|
+
throw _iteratorError1;
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
return id;
|
|
309
|
+
}
|
|
310
|
+
/**
|
|
311
|
+
* Convert a ComponentNode tree to a flat Placements array.
|
|
312
|
+
*/ function treeToPlacements(root) {
|
|
313
|
+
var placements = [];
|
|
314
|
+
treeToPlacementsRecursive(root, null, placements);
|
|
315
|
+
return placements;
|
|
316
|
+
}
|
|
317
|
+
/**
|
|
318
|
+
* Create the validate_template tool for YAML validation.
|
|
319
|
+
*/ function createValidateTemplateTool(components, schema, data) {
|
|
320
|
+
var dataType = inferDataType(data);
|
|
321
|
+
return {
|
|
322
|
+
description: "Validate a YAML template. Checks that:\n1. The YAML is valid and can be parsed\n2. All component names are valid\n3. All data bindings (like {{email.subject}}) reference valid paths in the provided data\n4. Form components have a Button for submission\n5. The form fields will produce data matching the expected schema (if schema provided)\n\nCall this after generating your YAML template to verify it's correct before finalizing.",
|
|
323
|
+
inputSchema: z.object({
|
|
324
|
+
yaml: z.string().describe('The complete YAML template to validate')
|
|
325
|
+
}),
|
|
326
|
+
execute: function(args) {
|
|
327
|
+
var _errors, _errors1;
|
|
328
|
+
var yaml = args.yaml;
|
|
329
|
+
var errors = [];
|
|
330
|
+
// 1. Parse YAML
|
|
331
|
+
var root;
|
|
332
|
+
try {
|
|
333
|
+
var template = parseTemplate(yaml);
|
|
334
|
+
root = template.root;
|
|
335
|
+
} catch (error) {
|
|
336
|
+
return {
|
|
337
|
+
valid: false,
|
|
338
|
+
errors: [
|
|
339
|
+
{
|
|
340
|
+
type: 'parse-error',
|
|
341
|
+
message: _instanceof(error, Error) ? error.message : 'Failed to parse YAML'
|
|
342
|
+
}
|
|
343
|
+
]
|
|
344
|
+
};
|
|
345
|
+
}
|
|
346
|
+
// 2. Validate component names
|
|
347
|
+
var componentNames = new Set(Object.keys(components));
|
|
348
|
+
componentNames.add('List'); // Built-in loop component
|
|
349
|
+
var unknownComponents = validateComponentNames(root, componentNames);
|
|
350
|
+
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
|
|
351
|
+
try {
|
|
352
|
+
for(var _iterator = unknownComponents[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
|
|
353
|
+
var compName = _step.value;
|
|
354
|
+
errors.push({
|
|
355
|
+
type: 'unknown-component',
|
|
356
|
+
message: 'Unknown component: "'.concat(compName, '"')
|
|
357
|
+
});
|
|
358
|
+
}
|
|
359
|
+
} catch (err) {
|
|
360
|
+
_didIteratorError = true;
|
|
361
|
+
_iteratorError = err;
|
|
362
|
+
} finally{
|
|
363
|
+
try {
|
|
364
|
+
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
365
|
+
_iterator.return();
|
|
366
|
+
}
|
|
367
|
+
} finally{
|
|
368
|
+
if (_didIteratorError) {
|
|
369
|
+
throw _iteratorError;
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
// 3. Validate data bindings
|
|
374
|
+
var bindingResult = validateDataBindings(root, dataType);
|
|
375
|
+
(_errors = errors).push.apply(_errors, _to_consumable_array(bindingResult.errors));
|
|
376
|
+
// 4. Validate Form components have a Button for submission
|
|
377
|
+
var formButtonErrors = validateFormHasButton(root);
|
|
378
|
+
(_errors1 = errors).push.apply(_errors1, _to_consumable_array(formButtonErrors));
|
|
379
|
+
// 5. Validate form schema if provided
|
|
380
|
+
var extractedFields;
|
|
381
|
+
if (schema) {
|
|
382
|
+
var _errors2;
|
|
383
|
+
var extracted = extractFormSchema(root);
|
|
384
|
+
extractedFields = extracted.fields.map(function(f) {
|
|
385
|
+
return {
|
|
386
|
+
name: f.name,
|
|
387
|
+
type: f.type
|
|
388
|
+
};
|
|
389
|
+
});
|
|
390
|
+
var schemaErrors = validateAgainstZod(extracted, schema);
|
|
391
|
+
(_errors2 = errors).push.apply(_errors2, _to_consumable_array(schemaErrors));
|
|
392
|
+
}
|
|
393
|
+
return {
|
|
394
|
+
valid: errors.length === 0,
|
|
395
|
+
errors: errors.map(function(e) {
|
|
396
|
+
return {
|
|
397
|
+
type: e.type,
|
|
398
|
+
message: e.message
|
|
399
|
+
};
|
|
400
|
+
}),
|
|
401
|
+
extractedFields: extractedFields
|
|
402
|
+
};
|
|
403
|
+
}
|
|
404
|
+
};
|
|
405
|
+
}
|
|
406
|
+
/**
|
|
407
|
+
* Recursively find all component names that aren't in the allowed set.
|
|
408
|
+
*/ function validateComponentNames(node, allowed) {
|
|
409
|
+
var unknown = [];
|
|
410
|
+
if (!allowed.has(node.component)) {
|
|
411
|
+
unknown.push(node.component);
|
|
412
|
+
}
|
|
413
|
+
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
|
|
414
|
+
try {
|
|
415
|
+
for(var _iterator = node.children[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
|
|
416
|
+
var child = _step.value;
|
|
417
|
+
var _unknown;
|
|
418
|
+
(_unknown = unknown).push.apply(_unknown, _to_consumable_array(validateComponentNames(child, allowed)));
|
|
419
|
+
}
|
|
420
|
+
} catch (err) {
|
|
421
|
+
_didIteratorError = true;
|
|
422
|
+
_iteratorError = err;
|
|
423
|
+
} finally{
|
|
424
|
+
try {
|
|
425
|
+
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
426
|
+
_iterator.return();
|
|
427
|
+
}
|
|
428
|
+
} finally{
|
|
429
|
+
if (_didIteratorError) {
|
|
430
|
+
throw _iteratorError;
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
return unknown;
|
|
435
|
+
}
|
|
436
|
+
/**
|
|
437
|
+
* Check if a component tree contains a Button component.
|
|
438
|
+
*/ function hasButtonDescendant(node) {
|
|
439
|
+
if (node.component === 'Button') {
|
|
440
|
+
return true;
|
|
441
|
+
}
|
|
442
|
+
return node.children.some(hasButtonDescendant);
|
|
443
|
+
}
|
|
444
|
+
/**
|
|
445
|
+
* Validate that all Form components have a Button for submission.
|
|
446
|
+
*/ function validateFormHasButton(root) {
|
|
447
|
+
var errors = [];
|
|
448
|
+
function checkNode(node) {
|
|
449
|
+
if (node.component === 'Form') {
|
|
450
|
+
if (!hasButtonDescendant(node)) {
|
|
451
|
+
errors.push({
|
|
452
|
+
type: 'form-missing-submit-button',
|
|
453
|
+
message: 'Form component requires a Button for submission'
|
|
454
|
+
});
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
|
|
458
|
+
try {
|
|
459
|
+
// Check children for nested forms
|
|
460
|
+
for(var _iterator = node.children[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
|
|
461
|
+
var child = _step.value;
|
|
462
|
+
checkNode(child);
|
|
463
|
+
}
|
|
464
|
+
} catch (err) {
|
|
465
|
+
_didIteratorError = true;
|
|
466
|
+
_iteratorError = err;
|
|
467
|
+
} finally{
|
|
468
|
+
try {
|
|
469
|
+
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
470
|
+
_iterator.return();
|
|
471
|
+
}
|
|
472
|
+
} finally{
|
|
473
|
+
if (_didIteratorError) {
|
|
474
|
+
throw _iteratorError;
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
checkNode(root);
|
|
480
|
+
return errors;
|
|
481
|
+
}
|
|
482
|
+
/**
|
|
483
|
+
* Build the system prompt describing the YAML DSL.
|
|
484
|
+
*/ function buildSystemPrompt(components, hasSchema) {
|
|
485
|
+
var componentDocs = Object.entries(components).map(function(param) {
|
|
486
|
+
var _param = _sliced_to_array(param, 2), name = _param[0], comp = _param[1];
|
|
487
|
+
var desc = comp.description.split('\n')[0];
|
|
488
|
+
if (comp.propsSchema) {
|
|
489
|
+
var propsDoc = formatPropsSchema(comp.propsSchema);
|
|
490
|
+
return "### ".concat(name, "\n").concat(desc, "\nProps:\n").concat(propsDoc);
|
|
491
|
+
}
|
|
492
|
+
return "### ".concat(name, "\n").concat(desc);
|
|
493
|
+
}).join('\n\n');
|
|
494
|
+
return 'You are a UI generator that creates YAML templates for dynamic pages.\n\n## YAML Format\n\nGenerate a YAML template with a single root component. Components are objects where the key is the component name and the value contains props and children.\n\n```yaml\nForm:\n submitLabel: "Submit"\n children:\n - Heading:\n content: "Contact Form"\n level: "1"\n - Input:\n name: "email"\n label: "Email Address"\n type: "email"\n required: true\n - TextArea:\n name: "message"\n label: "Your Message"\n```\n\n## Data Bindings\n\nUse `{{path}}` syntax to bind props to data values:\n- `{{user.name}}` - binds to the "name" property of "user" in the data\n- `{{items}}` - binds to the "items" array\n\n## List Component (Loops)\n\nUse the List component to iterate over arrays:\n\n```yaml\nList:\n items: "{{emails}}"\n as: "email"\n children:\n - Container:\n children:\n - Text:\n content: "{{email.subject}}"\n - Checkbox:\n name: "selectedIds"\n value: "{{email.id}}"\n label: "Select"\n```\n\nThe `as` prop defines the variable name for each item (defaults to "item").\nInside the List\'s children, you can reference `{{email.fieldName}}` for item properties.\n\n## Available Components\n\n'.concat(componentDocs, "\n\n## Important Rules\n\n1. Generate ONLY valid YAML - no markdown code fences, no extra text\n2. Use proper YAML indentation (2 spaces)\n3. String values with special characters should be quoted\n4. The root must be a single component (usually Form or Container)\n5. Use `children:` array for nested components\n6. Do NOT use markdown formatting in content strings - no **bold**, *italic*, or [links](url). These are React components, not markdown. URLs should be included as plain text.\n").concat(hasSchema ? '7. After generating the YAML, call validate_template to verify it matches the expected schema' : '');
|
|
495
|
+
}
|
|
496
|
+
/**
|
|
497
|
+
* Build the user prompt with data shape and instructions.
|
|
498
|
+
*/ function buildUserPrompt(prompt, data, schema) {
|
|
499
|
+
var dataShape = describeDataShape(data);
|
|
500
|
+
var userPrompt = "## Available Data\n```typescript\n".concat(dataShape, "\n```\n\n## Instructions\n").concat(prompt);
|
|
501
|
+
if (schema) {
|
|
502
|
+
var schemaFields = Object.entries(schema.shape).map(function(param) {
|
|
503
|
+
var _param = _sliced_to_array(param, 2), name = _param[0], field = _param[1];
|
|
504
|
+
var isOptional = _instanceof(field, z.ZodOptional);
|
|
505
|
+
var baseType = isOptional ? field.unwrap() : field;
|
|
506
|
+
var typeName = 'unknown';
|
|
507
|
+
if (_instanceof(baseType, z.ZodString)) typeName = 'string';
|
|
508
|
+
else if (_instanceof(baseType, z.ZodNumber)) typeName = 'number';
|
|
509
|
+
else if (_instanceof(baseType, z.ZodBoolean)) typeName = 'boolean';
|
|
510
|
+
else if (_instanceof(baseType, z.ZodArray)) typeName = "".concat(typeName, "[]");
|
|
511
|
+
return "- ".concat(name, ": ").concat(typeName).concat(isOptional ? ' (optional)' : '');
|
|
512
|
+
}).join('\n');
|
|
513
|
+
userPrompt += "\n\n## Expected Form Output\nThe form must collect these fields:\n".concat(schemaFields, "\n\nCall validate_template after generating your YAML to verify correctness.");
|
|
514
|
+
}
|
|
515
|
+
return userPrompt;
|
|
516
|
+
}
|
|
517
|
+
/**
|
|
518
|
+
* Generate a UI page using an LLM agent loop with YAML templates.
|
|
519
|
+
*
|
|
520
|
+
* The agent receives the user's prompt and generates a YAML template describing
|
|
521
|
+
* the component tree. The template is validated and converted to placements.
|
|
522
|
+
*
|
|
523
|
+
* @example
|
|
524
|
+
* ```typescript
|
|
525
|
+
* import { components } from '@positronic/gen-ui-components';
|
|
526
|
+
*
|
|
527
|
+
* const result = await generateUI({
|
|
528
|
+
* client,
|
|
529
|
+
* prompt: 'Create a form to collect user name and email',
|
|
530
|
+
* components,
|
|
531
|
+
* schema: z.object({ name: z.string(), email: z.string() }),
|
|
532
|
+
* data: { user: { name: 'John' } },
|
|
533
|
+
* });
|
|
534
|
+
*
|
|
535
|
+
* // result.placements contains the component tree as a flat array
|
|
536
|
+
* // result.rootId is the ID of the root component
|
|
537
|
+
* // result.yaml is the generated YAML template
|
|
538
|
+
* ```
|
|
539
|
+
*/ export function generateUI(params) {
|
|
540
|
+
return _async_to_generator(function() {
|
|
541
|
+
var client, prompt, components, schema, _params_data, data, _params_maxSteps, maxSteps, _params_system, systemPrompt, userPrompt, validateTool, result, _result_text, yamlContent, yamlMatch, placements, rootId, _placements_find, template;
|
|
542
|
+
return _ts_generator(this, function(_state) {
|
|
543
|
+
switch(_state.label){
|
|
544
|
+
case 0:
|
|
545
|
+
client = params.client, prompt = params.prompt, components = params.components, schema = params.schema, _params_data = params.data, data = _params_data === void 0 ? {} : _params_data, _params_maxSteps = params.maxSteps, maxSteps = _params_maxSteps === void 0 ? 10 : _params_maxSteps;
|
|
546
|
+
systemPrompt = (_params_system = params.system) !== null && _params_system !== void 0 ? _params_system : buildSystemPrompt(components, !!schema);
|
|
547
|
+
userPrompt = buildUserPrompt(prompt, data, schema);
|
|
548
|
+
// Create the validate_template tool
|
|
549
|
+
validateTool = createValidateTemplateTool(components, schema, data);
|
|
550
|
+
return [
|
|
551
|
+
4,
|
|
552
|
+
client.streamText({
|
|
553
|
+
system: systemPrompt,
|
|
554
|
+
prompt: userPrompt,
|
|
555
|
+
tools: {
|
|
556
|
+
validate_template: validateTool
|
|
557
|
+
},
|
|
558
|
+
maxSteps: maxSteps
|
|
559
|
+
})
|
|
560
|
+
];
|
|
561
|
+
case 1:
|
|
562
|
+
result = _state.sent();
|
|
563
|
+
// Extract YAML from the response text
|
|
564
|
+
// The LLM should output YAML directly or in a code block
|
|
565
|
+
yamlContent = (_result_text = result.text) !== null && _result_text !== void 0 ? _result_text : '';
|
|
566
|
+
// Strip markdown code fences if present
|
|
567
|
+
yamlMatch = yamlContent.match(/```(?:yaml)?\s*([\s\S]*?)```/);
|
|
568
|
+
if (yamlMatch) {
|
|
569
|
+
yamlContent = yamlMatch[1].trim();
|
|
570
|
+
} else {
|
|
571
|
+
yamlContent = yamlContent.trim();
|
|
572
|
+
}
|
|
573
|
+
// Try to parse and convert to placements
|
|
574
|
+
placements = [];
|
|
575
|
+
if (yamlContent) {
|
|
576
|
+
try {
|
|
577
|
+
;
|
|
578
|
+
template = parseTemplate(yamlContent);
|
|
579
|
+
placements = treeToPlacements(template.root);
|
|
580
|
+
rootId = (_placements_find = placements.find(function(p) {
|
|
581
|
+
return p.parentId === null;
|
|
582
|
+
})) === null || _placements_find === void 0 ? void 0 : _placements_find.id;
|
|
583
|
+
} catch (e) {
|
|
584
|
+
// If parsing fails, return empty placements
|
|
585
|
+
// The validation tool should have caught this during generation
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
return [
|
|
589
|
+
2,
|
|
590
|
+
{
|
|
591
|
+
placements: placements,
|
|
592
|
+
rootId: rootId,
|
|
593
|
+
yaml: yamlContent || undefined,
|
|
594
|
+
text: result.text,
|
|
595
|
+
usage: result.usage
|
|
596
|
+
}
|
|
597
|
+
];
|
|
598
|
+
}
|
|
599
|
+
});
|
|
600
|
+
})();
|
|
601
|
+
}
|