@jaypie/fabric 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +677 -0
- package/dist/cjs/commander/FabricCommander.d.ts +94 -0
- package/dist/cjs/commander/createCommanderOptions.d.ts +25 -0
- package/dist/cjs/commander/fabricCommand.d.ts +43 -0
- package/dist/cjs/commander/index.cjs +1487 -0
- package/dist/cjs/commander/index.cjs.map +1 -0
- package/dist/cjs/commander/index.d.ts +6 -0
- package/dist/cjs/commander/parseCommanderOptions.d.ts +32 -0
- package/dist/cjs/commander/registerServiceCommand.d.ts +43 -0
- package/dist/cjs/commander/types.d.ts +107 -0
- package/dist/cjs/constants.d.ts +12 -0
- package/dist/cjs/convert-date.d.ts +47 -0
- package/dist/cjs/convert.d.ts +69 -0
- package/dist/cjs/data/FabricData.d.ts +42 -0
- package/dist/cjs/data/index.cjs +1575 -0
- package/dist/cjs/data/index.cjs.map +1 -0
- package/dist/cjs/data/index.d.ts +5 -0
- package/dist/cjs/data/services/archive.d.ts +8 -0
- package/dist/cjs/data/services/create.d.ts +8 -0
- package/dist/cjs/data/services/delete.d.ts +8 -0
- package/dist/cjs/data/services/execute.d.ts +8 -0
- package/dist/cjs/data/services/index.d.ts +7 -0
- package/dist/cjs/data/services/list.d.ts +8 -0
- package/dist/cjs/data/services/read.d.ts +8 -0
- package/dist/cjs/data/services/update.d.ts +8 -0
- package/dist/cjs/data/transforms.d.ts +80 -0
- package/dist/cjs/data/types.d.ts +190 -0
- package/dist/cjs/express/FabricRouter.d.ts +29 -0
- package/dist/cjs/express/fabricExpress.d.ts +16 -0
- package/dist/cjs/express/index.cjs +505 -0
- package/dist/cjs/express/index.cjs.map +1 -0
- package/dist/cjs/express/index.d.ts +3 -0
- package/dist/cjs/express/types.d.ts +51 -0
- package/dist/cjs/helpers/fallback.d.ts +21 -0
- package/dist/cjs/helpers/index.d.ts +3 -0
- package/dist/cjs/helpers/resolvedName.d.ts +24 -0
- package/dist/cjs/http/FabricHttpServer.d.ts +31 -0
- package/dist/cjs/http/authorization.d.ts +30 -0
- package/dist/cjs/http/cors.d.ts +40 -0
- package/dist/cjs/http/fabricHttp.d.ts +28 -0
- package/dist/cjs/http/httpTransform.d.ts +36 -0
- package/dist/cjs/http/index.cjs +1820 -0
- package/dist/cjs/http/index.cjs.map +1 -0
- package/dist/cjs/http/index.d.ts +10 -0
- package/dist/cjs/http/stream.d.ts +185 -0
- package/dist/cjs/http/types.d.ts +343 -0
- package/dist/cjs/index/index.d.ts +8 -0
- package/dist/cjs/index/keyBuilder.d.ts +81 -0
- package/dist/cjs/index/registry.d.ts +56 -0
- package/dist/cjs/index/types.d.ts +54 -0
- package/dist/cjs/index.cjs +1674 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/cjs/index.d.ts +18 -0
- package/dist/cjs/lambda/createLambdaService.d.ts +33 -0
- package/dist/cjs/lambda/fabricLambda.d.ts +36 -0
- package/dist/cjs/lambda/index.cjs +967 -0
- package/dist/cjs/lambda/index.cjs.map +1 -0
- package/dist/cjs/lambda/index.d.ts +2 -0
- package/dist/cjs/lambda/types.d.ts +68 -0
- package/dist/cjs/llm/createLlmTool.d.ts +40 -0
- package/dist/cjs/llm/fabricTool.d.ts +40 -0
- package/dist/cjs/llm/index.cjs +1107 -0
- package/dist/cjs/llm/index.cjs.map +1 -0
- package/dist/cjs/llm/index.d.ts +3 -0
- package/dist/cjs/llm/inputToJsonSchema.d.ts +32 -0
- package/dist/cjs/llm/types.d.ts +61 -0
- package/dist/cjs/mcp/fabricMcp.d.ts +38 -0
- package/dist/cjs/mcp/index.cjs +938 -0
- package/dist/cjs/mcp/index.cjs.map +1 -0
- package/dist/cjs/mcp/index.d.ts +2 -0
- package/dist/cjs/mcp/registerMcpTool.d.ts +38 -0
- package/dist/cjs/mcp/types.d.ts +60 -0
- package/dist/cjs/models/base.d.ts +209 -0
- package/dist/cjs/resolve-date.d.ts +47 -0
- package/dist/cjs/resolve.d.ts +69 -0
- package/dist/cjs/resolveService.d.ts +49 -0
- package/dist/cjs/service.d.ts +13 -0
- package/dist/cjs/status.d.ts +30 -0
- package/dist/cjs/types/elementaryTypes.d.ts +84 -0
- package/dist/cjs/types/fieldCategory.d.ts +20 -0
- package/dist/cjs/types/fieldDefinition.d.ts +46 -0
- package/dist/cjs/types/index.d.ts +4 -0
- package/dist/cjs/types.d.ts +56 -0
- package/dist/esm/commander/FabricCommander.d.ts +94 -0
- package/dist/esm/commander/createCommanderOptions.d.ts +25 -0
- package/dist/esm/commander/fabricCommand.d.ts +43 -0
- package/dist/esm/commander/index.d.ts +6 -0
- package/dist/esm/commander/index.js +1482 -0
- package/dist/esm/commander/index.js.map +1 -0
- package/dist/esm/commander/parseCommanderOptions.d.ts +32 -0
- package/dist/esm/commander/registerServiceCommand.d.ts +43 -0
- package/dist/esm/commander/types.d.ts +107 -0
- package/dist/esm/constants.d.ts +12 -0
- package/dist/esm/convert-date.d.ts +47 -0
- package/dist/esm/convert.d.ts +69 -0
- package/dist/esm/data/FabricData.d.ts +42 -0
- package/dist/esm/data/index.d.ts +5 -0
- package/dist/esm/data/index.js +1548 -0
- package/dist/esm/data/index.js.map +1 -0
- package/dist/esm/data/services/archive.d.ts +8 -0
- package/dist/esm/data/services/create.d.ts +8 -0
- package/dist/esm/data/services/delete.d.ts +8 -0
- package/dist/esm/data/services/execute.d.ts +8 -0
- package/dist/esm/data/services/index.d.ts +7 -0
- package/dist/esm/data/services/list.d.ts +8 -0
- package/dist/esm/data/services/read.d.ts +8 -0
- package/dist/esm/data/services/update.d.ts +8 -0
- package/dist/esm/data/transforms.d.ts +80 -0
- package/dist/esm/data/types.d.ts +190 -0
- package/dist/esm/express/FabricRouter.d.ts +29 -0
- package/dist/esm/express/fabricExpress.d.ts +16 -0
- package/dist/esm/express/index.d.ts +3 -0
- package/dist/esm/express/index.js +500 -0
- package/dist/esm/express/index.js.map +1 -0
- package/dist/esm/express/types.d.ts +51 -0
- package/dist/esm/helpers/fallback.d.ts +21 -0
- package/dist/esm/helpers/index.d.ts +3 -0
- package/dist/esm/helpers/resolvedName.d.ts +24 -0
- package/dist/esm/http/FabricHttpServer.d.ts +31 -0
- package/dist/esm/http/authorization.d.ts +30 -0
- package/dist/esm/http/cors.d.ts +40 -0
- package/dist/esm/http/fabricHttp.d.ts +28 -0
- package/dist/esm/http/httpTransform.d.ts +36 -0
- package/dist/esm/http/index.d.ts +10 -0
- package/dist/esm/http/index.js +1775 -0
- package/dist/esm/http/index.js.map +1 -0
- package/dist/esm/http/stream.d.ts +185 -0
- package/dist/esm/http/types.d.ts +343 -0
- package/dist/esm/index/index.d.ts +8 -0
- package/dist/esm/index/keyBuilder.d.ts +81 -0
- package/dist/esm/index/registry.d.ts +56 -0
- package/dist/esm/index/types.d.ts +54 -0
- package/dist/esm/index.d.ts +18 -0
- package/dist/esm/index.js +1606 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/lambda/createLambdaService.d.ts +33 -0
- package/dist/esm/lambda/fabricLambda.d.ts +36 -0
- package/dist/esm/lambda/index.d.ts +2 -0
- package/dist/esm/lambda/index.js +965 -0
- package/dist/esm/lambda/index.js.map +1 -0
- package/dist/esm/lambda/types.d.ts +68 -0
- package/dist/esm/llm/createLlmTool.d.ts +40 -0
- package/dist/esm/llm/fabricTool.d.ts +40 -0
- package/dist/esm/llm/index.d.ts +3 -0
- package/dist/esm/llm/index.js +1104 -0
- package/dist/esm/llm/index.js.map +1 -0
- package/dist/esm/llm/inputToJsonSchema.d.ts +32 -0
- package/dist/esm/llm/types.d.ts +61 -0
- package/dist/esm/mcp/fabricMcp.d.ts +38 -0
- package/dist/esm/mcp/index.d.ts +2 -0
- package/dist/esm/mcp/index.js +936 -0
- package/dist/esm/mcp/index.js.map +1 -0
- package/dist/esm/mcp/registerMcpTool.d.ts +38 -0
- package/dist/esm/mcp/types.d.ts +60 -0
- package/dist/esm/models/base.d.ts +209 -0
- package/dist/esm/resolve-date.d.ts +47 -0
- package/dist/esm/resolve.d.ts +69 -0
- package/dist/esm/resolveService.d.ts +49 -0
- package/dist/esm/service.d.ts +13 -0
- package/dist/esm/status.d.ts +30 -0
- package/dist/esm/types/elementaryTypes.d.ts +84 -0
- package/dist/esm/types/fieldCategory.d.ts +20 -0
- package/dist/esm/types/fieldDefinition.d.ts +46 -0
- package/dist/esm/types/index.d.ts +4 -0
- package/dist/esm/types.d.ts +56 -0
- package/package.json +122 -0
|
@@ -0,0 +1,1107 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var errors = require('@jaypie/errors');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Meta-modeling Constants
|
|
7
|
+
*/
|
|
8
|
+
// =============================================================================
|
|
9
|
+
// Constants
|
|
10
|
+
// =============================================================================
|
|
11
|
+
/** Root organizational unit */
|
|
12
|
+
/** Fabric version - used to identify pre-instantiated Services */
|
|
13
|
+
const FABRIC_VERSION = "0.1.0";
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Date Type Conversion for @jaypie/fabric
|
|
17
|
+
*
|
|
18
|
+
* Adds Date as a supported type in the fabric type system.
|
|
19
|
+
* Follows the same conversion patterns as String, Number, Boolean.
|
|
20
|
+
*/
|
|
21
|
+
/**
|
|
22
|
+
* Convert a value to a Date
|
|
23
|
+
*
|
|
24
|
+
* Supported inputs:
|
|
25
|
+
* - Date: returned as-is (validated)
|
|
26
|
+
* - Number: treated as Unix timestamp (milliseconds)
|
|
27
|
+
* - String: parsed via Date constructor (ISO 8601, etc.)
|
|
28
|
+
* - Object with value property: unwrapped and converted
|
|
29
|
+
*
|
|
30
|
+
* @throws BadRequestError if value cannot be converted to valid Date
|
|
31
|
+
*/
|
|
32
|
+
function fabricDate(value) {
|
|
33
|
+
// Already a Date
|
|
34
|
+
if (value instanceof Date) {
|
|
35
|
+
if (Number.isNaN(value.getTime())) {
|
|
36
|
+
throw new errors.BadRequestError("Invalid Date value");
|
|
37
|
+
}
|
|
38
|
+
return value;
|
|
39
|
+
}
|
|
40
|
+
// Null/undefined
|
|
41
|
+
if (value === null || value === undefined) {
|
|
42
|
+
throw new errors.BadRequestError("Cannot convert null or undefined to Date");
|
|
43
|
+
}
|
|
44
|
+
// Object with value property (fabric pattern)
|
|
45
|
+
if (typeof value === "object" && value !== null && "value" in value) {
|
|
46
|
+
return fabricDate(value.value);
|
|
47
|
+
}
|
|
48
|
+
// Number (timestamp in milliseconds)
|
|
49
|
+
if (typeof value === "number") {
|
|
50
|
+
if (Number.isNaN(value)) {
|
|
51
|
+
throw new errors.BadRequestError("Cannot convert NaN to Date");
|
|
52
|
+
}
|
|
53
|
+
const date = new Date(value);
|
|
54
|
+
if (Number.isNaN(date.getTime())) {
|
|
55
|
+
throw new errors.BadRequestError(`Cannot convert ${value} to Date`);
|
|
56
|
+
}
|
|
57
|
+
return date;
|
|
58
|
+
}
|
|
59
|
+
// String (ISO 8601 or parseable format)
|
|
60
|
+
if (typeof value === "string") {
|
|
61
|
+
// Empty string is invalid
|
|
62
|
+
if (value.trim() === "") {
|
|
63
|
+
throw new errors.BadRequestError("Cannot convert empty string to Date");
|
|
64
|
+
}
|
|
65
|
+
const date = new Date(value);
|
|
66
|
+
if (Number.isNaN(date.getTime())) {
|
|
67
|
+
throw new errors.BadRequestError(`Cannot convert "${value}" to Date`);
|
|
68
|
+
}
|
|
69
|
+
return date;
|
|
70
|
+
}
|
|
71
|
+
// Boolean cannot be converted to Date
|
|
72
|
+
if (typeof value === "boolean") {
|
|
73
|
+
throw new errors.BadRequestError("Cannot convert boolean to Date");
|
|
74
|
+
}
|
|
75
|
+
// Arrays - attempt single element extraction
|
|
76
|
+
if (Array.isArray(value)) {
|
|
77
|
+
if (value.length === 1) {
|
|
78
|
+
return fabricDate(value[0]);
|
|
79
|
+
}
|
|
80
|
+
throw new errors.BadRequestError(`Cannot convert array with ${value.length} elements to Date`);
|
|
81
|
+
}
|
|
82
|
+
throw new errors.BadRequestError(`Cannot convert ${typeof value} to Date`);
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Type guard for Date type in schema definitions
|
|
86
|
+
*/
|
|
87
|
+
function isDateType(type) {
|
|
88
|
+
return type === Date;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// Fabric functions for @jaypie/fabric
|
|
92
|
+
/**
|
|
93
|
+
* Try to parse a string as JSON if it looks like JSON
|
|
94
|
+
* Returns the parsed value or the original string if not JSON
|
|
95
|
+
*/
|
|
96
|
+
function tryParseJson(value) {
|
|
97
|
+
const trimmed = value.trim();
|
|
98
|
+
if ((trimmed.startsWith("{") && trimmed.endsWith("}")) ||
|
|
99
|
+
(trimmed.startsWith("[") && trimmed.endsWith("]"))) {
|
|
100
|
+
try {
|
|
101
|
+
return JSON.parse(trimmed);
|
|
102
|
+
}
|
|
103
|
+
catch {
|
|
104
|
+
// Not valid JSON, return original
|
|
105
|
+
return value;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return value;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Unwrap arrays and objects to get to the scalar value
|
|
112
|
+
* - Single-element arrays unwrap to their element
|
|
113
|
+
* - Objects with value property unwrap to that value
|
|
114
|
+
* - Recursively unwraps nested structures
|
|
115
|
+
*/
|
|
116
|
+
function unwrapToScalar(value) {
|
|
117
|
+
if (value === undefined || value === null) {
|
|
118
|
+
return value;
|
|
119
|
+
}
|
|
120
|
+
// Unwrap single-element arrays
|
|
121
|
+
if (Array.isArray(value)) {
|
|
122
|
+
if (value.length === 0) {
|
|
123
|
+
return undefined;
|
|
124
|
+
}
|
|
125
|
+
if (value.length === 1) {
|
|
126
|
+
return unwrapToScalar(value[0]);
|
|
127
|
+
}
|
|
128
|
+
throw new errors.BadRequestError("Cannot convert multi-value array to scalar");
|
|
129
|
+
}
|
|
130
|
+
// Unwrap objects with value property
|
|
131
|
+
if (typeof value === "object") {
|
|
132
|
+
const obj = value;
|
|
133
|
+
if ("value" in obj) {
|
|
134
|
+
return unwrapToScalar(obj.value);
|
|
135
|
+
}
|
|
136
|
+
throw new errors.BadRequestError("Object must have a value attribute");
|
|
137
|
+
}
|
|
138
|
+
return value;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Prepare a value for scalar conversion by parsing JSON strings and unwrapping
|
|
142
|
+
*/
|
|
143
|
+
function prepareForScalarConversion(value) {
|
|
144
|
+
if (value === undefined || value === null) {
|
|
145
|
+
return value;
|
|
146
|
+
}
|
|
147
|
+
// Try to parse JSON strings
|
|
148
|
+
if (typeof value === "string") {
|
|
149
|
+
const parsed = tryParseJson(value);
|
|
150
|
+
if (parsed !== value) {
|
|
151
|
+
// Successfully parsed, unwrap the result
|
|
152
|
+
return unwrapToScalar(parsed);
|
|
153
|
+
}
|
|
154
|
+
return value;
|
|
155
|
+
}
|
|
156
|
+
// Unwrap arrays and objects
|
|
157
|
+
if (Array.isArray(value) || typeof value === "object") {
|
|
158
|
+
return unwrapToScalar(value);
|
|
159
|
+
}
|
|
160
|
+
return value;
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Convert a value to a boolean
|
|
164
|
+
* - Arrays, objects, and JSON strings are unwrapped first
|
|
165
|
+
* - String "true" becomes true
|
|
166
|
+
* - String "false" becomes false
|
|
167
|
+
* - Strings that parse to numbers: positive = true, zero or negative = false
|
|
168
|
+
* - Numbers: positive = true, zero or negative = false
|
|
169
|
+
* - Boolean passes through
|
|
170
|
+
*/
|
|
171
|
+
function fabricBoolean(value) {
|
|
172
|
+
// Prepare value by parsing JSON and unwrapping arrays/objects
|
|
173
|
+
const prepared = prepareForScalarConversion(value);
|
|
174
|
+
if (prepared === undefined || prepared === null) {
|
|
175
|
+
return undefined;
|
|
176
|
+
}
|
|
177
|
+
if (typeof prepared === "boolean") {
|
|
178
|
+
return prepared;
|
|
179
|
+
}
|
|
180
|
+
if (typeof prepared === "string") {
|
|
181
|
+
if (prepared === "") {
|
|
182
|
+
return undefined;
|
|
183
|
+
}
|
|
184
|
+
const lower = prepared.toLowerCase();
|
|
185
|
+
if (lower === "true") {
|
|
186
|
+
return true;
|
|
187
|
+
}
|
|
188
|
+
if (lower === "false") {
|
|
189
|
+
return false;
|
|
190
|
+
}
|
|
191
|
+
// Try to parse as number
|
|
192
|
+
const num = parseFloat(prepared);
|
|
193
|
+
if (isNaN(num)) {
|
|
194
|
+
throw new errors.BadRequestError(`Cannot convert "${prepared}" to Boolean`);
|
|
195
|
+
}
|
|
196
|
+
return num > 0;
|
|
197
|
+
}
|
|
198
|
+
if (typeof prepared === "number") {
|
|
199
|
+
if (isNaN(prepared)) {
|
|
200
|
+
throw new errors.BadRequestError("Cannot convert NaN to Boolean");
|
|
201
|
+
}
|
|
202
|
+
return prepared > 0;
|
|
203
|
+
}
|
|
204
|
+
throw new errors.BadRequestError(`Cannot convert ${typeof prepared} to Boolean`);
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Convert a value to a number
|
|
208
|
+
* - Arrays, objects, and JSON strings are unwrapped first
|
|
209
|
+
* - String "" becomes undefined
|
|
210
|
+
* - String "true" becomes 1
|
|
211
|
+
* - String "false" becomes 0
|
|
212
|
+
* - Strings that parse to numbers use those values
|
|
213
|
+
* - Strings that parse to NaN throw BadRequestError
|
|
214
|
+
* - Boolean true becomes 1, false becomes 0
|
|
215
|
+
* - Number passes through
|
|
216
|
+
*/
|
|
217
|
+
function fabricNumber(value) {
|
|
218
|
+
// Prepare value by parsing JSON and unwrapping arrays/objects
|
|
219
|
+
const prepared = prepareForScalarConversion(value);
|
|
220
|
+
if (prepared === undefined || prepared === null) {
|
|
221
|
+
return undefined;
|
|
222
|
+
}
|
|
223
|
+
if (typeof prepared === "number") {
|
|
224
|
+
if (isNaN(prepared)) {
|
|
225
|
+
throw new errors.BadRequestError("Cannot convert NaN to Number");
|
|
226
|
+
}
|
|
227
|
+
return prepared;
|
|
228
|
+
}
|
|
229
|
+
if (typeof prepared === "boolean") {
|
|
230
|
+
return prepared ? 1 : 0;
|
|
231
|
+
}
|
|
232
|
+
if (typeof prepared === "string") {
|
|
233
|
+
if (prepared === "") {
|
|
234
|
+
return undefined;
|
|
235
|
+
}
|
|
236
|
+
const lower = prepared.toLowerCase();
|
|
237
|
+
if (lower === "true") {
|
|
238
|
+
return 1;
|
|
239
|
+
}
|
|
240
|
+
if (lower === "false") {
|
|
241
|
+
return 0;
|
|
242
|
+
}
|
|
243
|
+
const num = parseFloat(prepared);
|
|
244
|
+
if (isNaN(num)) {
|
|
245
|
+
throw new errors.BadRequestError(`Cannot convert "${prepared}" to Number`);
|
|
246
|
+
}
|
|
247
|
+
return num;
|
|
248
|
+
}
|
|
249
|
+
throw new errors.BadRequestError(`Cannot convert ${typeof prepared} to Number`);
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* Convert a value to a string
|
|
253
|
+
* - Arrays, objects, and JSON strings are unwrapped first
|
|
254
|
+
* - String "" becomes undefined
|
|
255
|
+
* - Boolean true becomes "true", false becomes "false"
|
|
256
|
+
* - Number converts to string representation
|
|
257
|
+
* - String passes through
|
|
258
|
+
*/
|
|
259
|
+
function fabricString(value) {
|
|
260
|
+
// Prepare value by parsing JSON and unwrapping arrays/objects
|
|
261
|
+
const prepared = prepareForScalarConversion(value);
|
|
262
|
+
if (prepared === undefined || prepared === null) {
|
|
263
|
+
return undefined;
|
|
264
|
+
}
|
|
265
|
+
if (typeof prepared === "string") {
|
|
266
|
+
if (prepared === "") {
|
|
267
|
+
return undefined;
|
|
268
|
+
}
|
|
269
|
+
return prepared;
|
|
270
|
+
}
|
|
271
|
+
if (typeof prepared === "boolean") {
|
|
272
|
+
return prepared ? "true" : "false";
|
|
273
|
+
}
|
|
274
|
+
if (typeof prepared === "number") {
|
|
275
|
+
if (isNaN(prepared)) {
|
|
276
|
+
throw new errors.BadRequestError("Cannot convert NaN to String");
|
|
277
|
+
}
|
|
278
|
+
return String(prepared);
|
|
279
|
+
}
|
|
280
|
+
throw new errors.BadRequestError(`Cannot convert ${typeof prepared} to String`);
|
|
281
|
+
}
|
|
282
|
+
/**
|
|
283
|
+
* Convert a value to an array
|
|
284
|
+
* - Non-arrays become arrays containing that value
|
|
285
|
+
* - Arrays of a single value become that value (unwrapped)
|
|
286
|
+
* - Multi-value arrays throw BadRequestError
|
|
287
|
+
* - undefined/null become undefined
|
|
288
|
+
*/
|
|
289
|
+
function fabricArray(value) {
|
|
290
|
+
if (value === undefined || value === null) {
|
|
291
|
+
return undefined;
|
|
292
|
+
}
|
|
293
|
+
if (Array.isArray(value)) {
|
|
294
|
+
// Arrays pass through (single-element unwrapping happens when converting FROM array)
|
|
295
|
+
return value;
|
|
296
|
+
}
|
|
297
|
+
// Non-arrays become single-element arrays
|
|
298
|
+
return [value];
|
|
299
|
+
}
|
|
300
|
+
/**
|
|
301
|
+
* Convert a value to an object with a value property
|
|
302
|
+
* - Scalars become { value: scalar }
|
|
303
|
+
* - Arrays become { value: array }
|
|
304
|
+
* - Objects with a value attribute pass through
|
|
305
|
+
* - Objects without a value attribute throw BadRequestError
|
|
306
|
+
* - undefined/null become undefined
|
|
307
|
+
*/
|
|
308
|
+
function fabricObject(value) {
|
|
309
|
+
if (value === undefined || value === null) {
|
|
310
|
+
return undefined;
|
|
311
|
+
}
|
|
312
|
+
// Check if already an object (but not an array)
|
|
313
|
+
if (typeof value === "object" && !Array.isArray(value)) {
|
|
314
|
+
const obj = value;
|
|
315
|
+
if ("value" in obj) {
|
|
316
|
+
return obj;
|
|
317
|
+
}
|
|
318
|
+
throw new errors.BadRequestError("Object must have a value attribute");
|
|
319
|
+
}
|
|
320
|
+
// Scalars and arrays become { value: ... }
|
|
321
|
+
return { value };
|
|
322
|
+
}
|
|
323
|
+
/**
|
|
324
|
+
* Check if a type is a typed array (e.g., [String], [Number], [], etc.)
|
|
325
|
+
*/
|
|
326
|
+
function isTypedArrayType(type) {
|
|
327
|
+
return Array.isArray(type);
|
|
328
|
+
}
|
|
329
|
+
/**
|
|
330
|
+
* Split a string on comma or tab delimiters for typed array conversion.
|
|
331
|
+
* Only splits if the string contains commas or tabs.
|
|
332
|
+
* Returns the original value if not a string or no delimiters found.
|
|
333
|
+
*/
|
|
334
|
+
function splitStringForArray(value) {
|
|
335
|
+
if (typeof value !== "string") {
|
|
336
|
+
return value;
|
|
337
|
+
}
|
|
338
|
+
// Check for comma or tab delimiters
|
|
339
|
+
if (value.includes(",")) {
|
|
340
|
+
return value.split(",").map((s) => s.trim());
|
|
341
|
+
}
|
|
342
|
+
if (value.includes("\t")) {
|
|
343
|
+
return value.split("\t").map((s) => s.trim());
|
|
344
|
+
}
|
|
345
|
+
return value;
|
|
346
|
+
}
|
|
347
|
+
/**
|
|
348
|
+
* Try to parse a string as JSON for array context.
|
|
349
|
+
* Returns parsed value if it's an array, otherwise returns original.
|
|
350
|
+
*/
|
|
351
|
+
function tryParseJsonArray(value) {
|
|
352
|
+
if (typeof value !== "string") {
|
|
353
|
+
return value;
|
|
354
|
+
}
|
|
355
|
+
const trimmed = value.trim();
|
|
356
|
+
if (trimmed.startsWith("[") && trimmed.endsWith("]")) {
|
|
357
|
+
try {
|
|
358
|
+
const parsed = JSON.parse(trimmed);
|
|
359
|
+
if (Array.isArray(parsed)) {
|
|
360
|
+
return parsed;
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
catch {
|
|
364
|
+
// Not valid JSON, fall through
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
return value;
|
|
368
|
+
}
|
|
369
|
+
/**
|
|
370
|
+
* Get the element type from a typed array type
|
|
371
|
+
* Returns undefined for untyped arrays ([])
|
|
372
|
+
*/
|
|
373
|
+
function getArrayElementType(type) {
|
|
374
|
+
if (type.length === 0) {
|
|
375
|
+
return undefined; // Untyped array
|
|
376
|
+
}
|
|
377
|
+
const elementType = type[0];
|
|
378
|
+
// Handle constructor types
|
|
379
|
+
if (elementType === Boolean)
|
|
380
|
+
return "boolean";
|
|
381
|
+
if (elementType === Number)
|
|
382
|
+
return "number";
|
|
383
|
+
if (elementType === String)
|
|
384
|
+
return "string";
|
|
385
|
+
if (elementType === Object)
|
|
386
|
+
return "object";
|
|
387
|
+
// Handle string types
|
|
388
|
+
if (elementType === "boolean")
|
|
389
|
+
return "boolean";
|
|
390
|
+
if (elementType === "number")
|
|
391
|
+
return "number";
|
|
392
|
+
if (elementType === "string")
|
|
393
|
+
return "string";
|
|
394
|
+
if (elementType === "object")
|
|
395
|
+
return "object";
|
|
396
|
+
// Handle shorthand types
|
|
397
|
+
if (elementType === "")
|
|
398
|
+
return "string"; // "" shorthand for String
|
|
399
|
+
if (typeof elementType === "object" &&
|
|
400
|
+
elementType !== null &&
|
|
401
|
+
Object.keys(elementType).length === 0) {
|
|
402
|
+
return "object"; // {} shorthand for Object
|
|
403
|
+
}
|
|
404
|
+
throw new errors.BadRequestError(`Unknown array element type: ${String(elementType)}`);
|
|
405
|
+
}
|
|
406
|
+
/**
|
|
407
|
+
* Convert a value to a typed array
|
|
408
|
+
* - Tries to parse JSON arrays first
|
|
409
|
+
* - Splits strings on comma/tab if present
|
|
410
|
+
* - Wraps non-arrays in an array
|
|
411
|
+
* - Converts each element to the specified element type
|
|
412
|
+
*/
|
|
413
|
+
function fabricTypedArray(value, elementType) {
|
|
414
|
+
// Try to parse JSON array first
|
|
415
|
+
let processed = tryParseJsonArray(value);
|
|
416
|
+
// If still a string, try to split on comma/tab
|
|
417
|
+
processed = splitStringForArray(processed);
|
|
418
|
+
// Convert to array (wraps non-arrays)
|
|
419
|
+
const array = fabricArray(processed);
|
|
420
|
+
if (array === undefined) {
|
|
421
|
+
return undefined;
|
|
422
|
+
}
|
|
423
|
+
// If no element type specified, return as-is
|
|
424
|
+
if (elementType === undefined) {
|
|
425
|
+
return array;
|
|
426
|
+
}
|
|
427
|
+
// Convert each element to the element type
|
|
428
|
+
return array.map((element, index) => {
|
|
429
|
+
try {
|
|
430
|
+
switch (elementType) {
|
|
431
|
+
case "boolean":
|
|
432
|
+
return fabricBoolean(element);
|
|
433
|
+
case "number":
|
|
434
|
+
return fabricNumber(element);
|
|
435
|
+
case "object":
|
|
436
|
+
return fabricObject(element);
|
|
437
|
+
case "string":
|
|
438
|
+
return fabricString(element);
|
|
439
|
+
default:
|
|
440
|
+
throw new errors.BadRequestError(`Unknown element type: ${elementType}`);
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
catch (error) {
|
|
444
|
+
if (error instanceof errors.BadRequestError) {
|
|
445
|
+
throw new errors.BadRequestError(`Cannot convert array element at index ${index}: ${error.message}`);
|
|
446
|
+
}
|
|
447
|
+
throw error;
|
|
448
|
+
}
|
|
449
|
+
});
|
|
450
|
+
}
|
|
451
|
+
/**
|
|
452
|
+
* Fabric a value to the specified type
|
|
453
|
+
*/
|
|
454
|
+
function fabric(value, type) {
|
|
455
|
+
// Check for Date type first
|
|
456
|
+
if (isDateType(type)) {
|
|
457
|
+
return fabricDate(value);
|
|
458
|
+
}
|
|
459
|
+
// Check for typed array types
|
|
460
|
+
if (isTypedArrayType(type)) {
|
|
461
|
+
const elementType = getArrayElementType(type);
|
|
462
|
+
return fabricTypedArray(value, elementType);
|
|
463
|
+
}
|
|
464
|
+
const normalizedType = normalizeType(type);
|
|
465
|
+
switch (normalizedType) {
|
|
466
|
+
case "array":
|
|
467
|
+
return fabricArray(value);
|
|
468
|
+
case "boolean":
|
|
469
|
+
return fabricBoolean(value);
|
|
470
|
+
case "number":
|
|
471
|
+
return fabricNumber(value);
|
|
472
|
+
case "object":
|
|
473
|
+
return fabricObject(value);
|
|
474
|
+
case "string":
|
|
475
|
+
return fabricString(value);
|
|
476
|
+
default:
|
|
477
|
+
throw new errors.BadRequestError(`Unknown type: ${String(type)}`);
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
/**
|
|
481
|
+
* Normalize type to string representation
|
|
482
|
+
*/
|
|
483
|
+
function normalizeType(type) {
|
|
484
|
+
if (type === Array || type === "array") {
|
|
485
|
+
return "array";
|
|
486
|
+
}
|
|
487
|
+
if (type === Boolean || type === "boolean") {
|
|
488
|
+
return "boolean";
|
|
489
|
+
}
|
|
490
|
+
if (type === Number || type === "number") {
|
|
491
|
+
return "number";
|
|
492
|
+
}
|
|
493
|
+
if (type === Object || type === "object") {
|
|
494
|
+
return "object";
|
|
495
|
+
}
|
|
496
|
+
if (type === String || type === "string") {
|
|
497
|
+
return "string";
|
|
498
|
+
}
|
|
499
|
+
throw new errors.BadRequestError(`Unknown type: ${String(type)}`);
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
// Service for @jaypie/fabric
|
|
503
|
+
/**
|
|
504
|
+
* Check if a single-element array is a typed array type constructor.
|
|
505
|
+
*/
|
|
506
|
+
function isTypedArrayConstructor$1(element) {
|
|
507
|
+
return (element === Boolean ||
|
|
508
|
+
element === Number ||
|
|
509
|
+
element === String ||
|
|
510
|
+
element === Object ||
|
|
511
|
+
element === "boolean" ||
|
|
512
|
+
element === "number" ||
|
|
513
|
+
element === "string" ||
|
|
514
|
+
element === "object" ||
|
|
515
|
+
element === "" ||
|
|
516
|
+
(typeof element === "object" &&
|
|
517
|
+
element !== null &&
|
|
518
|
+
!(element instanceof RegExp) &&
|
|
519
|
+
Object.keys(element).length === 0));
|
|
520
|
+
}
|
|
521
|
+
/**
|
|
522
|
+
* Check if a type is a validated string type (array of string literals and/or RegExp).
|
|
523
|
+
* Distinguishes from typed arrays like [String], [Number], etc.
|
|
524
|
+
*/
|
|
525
|
+
function isValidatedStringType(type) {
|
|
526
|
+
if (!Array.isArray(type)) {
|
|
527
|
+
return false;
|
|
528
|
+
}
|
|
529
|
+
// Empty array is untyped array, not validated string
|
|
530
|
+
if (type.length === 0) {
|
|
531
|
+
return false;
|
|
532
|
+
}
|
|
533
|
+
// Single-element arrays with type constructors are typed arrays
|
|
534
|
+
if (type.length === 1 && isTypedArrayConstructor$1(type[0])) {
|
|
535
|
+
return false;
|
|
536
|
+
}
|
|
537
|
+
// Check that all elements are strings or RegExp
|
|
538
|
+
return type.every((item) => typeof item === "string" || item instanceof RegExp);
|
|
539
|
+
}
|
|
540
|
+
/**
|
|
541
|
+
* Check if a type is a validated number type (array of number literals).
|
|
542
|
+
* Distinguishes from typed arrays like [Number], etc.
|
|
543
|
+
*/
|
|
544
|
+
function isValidatedNumberType(type) {
|
|
545
|
+
if (!Array.isArray(type)) {
|
|
546
|
+
return false;
|
|
547
|
+
}
|
|
548
|
+
// Empty array is untyped array, not validated number
|
|
549
|
+
if (type.length === 0) {
|
|
550
|
+
return false;
|
|
551
|
+
}
|
|
552
|
+
// Single-element arrays with type constructors are typed arrays
|
|
553
|
+
if (type.length === 1 && isTypedArrayConstructor$1(type[0])) {
|
|
554
|
+
return false;
|
|
555
|
+
}
|
|
556
|
+
// Check that all elements are numbers
|
|
557
|
+
return type.every((item) => typeof item === "number");
|
|
558
|
+
}
|
|
559
|
+
/**
|
|
560
|
+
* Parse input string as JSON if it's a string
|
|
561
|
+
*/
|
|
562
|
+
function parseInput(input) {
|
|
563
|
+
if (input === undefined || input === null) {
|
|
564
|
+
return {};
|
|
565
|
+
}
|
|
566
|
+
if (typeof input === "string") {
|
|
567
|
+
if (input === "") {
|
|
568
|
+
return {};
|
|
569
|
+
}
|
|
570
|
+
try {
|
|
571
|
+
const parsed = JSON.parse(input);
|
|
572
|
+
if (typeof parsed !== "object" ||
|
|
573
|
+
parsed === null ||
|
|
574
|
+
Array.isArray(parsed)) {
|
|
575
|
+
throw new errors.BadRequestError("Input must be an object");
|
|
576
|
+
}
|
|
577
|
+
return parsed;
|
|
578
|
+
}
|
|
579
|
+
catch (error) {
|
|
580
|
+
if (error instanceof errors.BadRequestError) {
|
|
581
|
+
throw error;
|
|
582
|
+
}
|
|
583
|
+
throw new errors.BadRequestError("Invalid JSON input");
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
if (typeof input === "object" && !Array.isArray(input)) {
|
|
587
|
+
return input;
|
|
588
|
+
}
|
|
589
|
+
throw new errors.BadRequestError("Input must be an object or JSON string");
|
|
590
|
+
}
|
|
591
|
+
/**
|
|
592
|
+
* Run validation on a value (supports async validators)
|
|
593
|
+
*/
|
|
594
|
+
async function runValidation(value, validate, fieldName) {
|
|
595
|
+
if (typeof validate === "function") {
|
|
596
|
+
const result = await validate(value);
|
|
597
|
+
if (result === false) {
|
|
598
|
+
throw new errors.BadRequestError(`Validation failed for field "${fieldName}"`);
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
else if (validate instanceof RegExp) {
|
|
602
|
+
if (typeof value !== "string" || !validate.test(value)) {
|
|
603
|
+
throw new errors.BadRequestError(`Validation failed for field "${fieldName}"`);
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
else if (Array.isArray(validate)) {
|
|
607
|
+
// Check if value matches any item in the array
|
|
608
|
+
for (const item of validate) {
|
|
609
|
+
if (item instanceof RegExp) {
|
|
610
|
+
if (typeof value === "string" && item.test(value)) {
|
|
611
|
+
return; // Match found
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
else if (typeof item === "function") {
|
|
615
|
+
try {
|
|
616
|
+
const result = await item(value);
|
|
617
|
+
if (result !== false) {
|
|
618
|
+
return; // Match found
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
catch {
|
|
622
|
+
// Continue to next item
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
else if (value === item) {
|
|
626
|
+
return; // Scalar match found
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
throw new errors.BadRequestError(`Validation failed for field "${fieldName}"`);
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
/**
|
|
633
|
+
* Check if a field is required
|
|
634
|
+
* A field is required unless it has a default OR required is explicitly false
|
|
635
|
+
*/
|
|
636
|
+
function isFieldRequired$1(definition) {
|
|
637
|
+
if (definition.required === false) {
|
|
638
|
+
return false;
|
|
639
|
+
}
|
|
640
|
+
if (definition.default !== undefined) {
|
|
641
|
+
return false;
|
|
642
|
+
}
|
|
643
|
+
return true;
|
|
644
|
+
}
|
|
645
|
+
/**
|
|
646
|
+
* Process a single field through conversion and validation
|
|
647
|
+
*/
|
|
648
|
+
async function processField(fieldName, value, definition) {
|
|
649
|
+
// Apply default if value is undefined
|
|
650
|
+
let processedValue = value;
|
|
651
|
+
if (processedValue === undefined && definition.default !== undefined) {
|
|
652
|
+
processedValue = definition.default;
|
|
653
|
+
}
|
|
654
|
+
// Determine actual type and validation
|
|
655
|
+
let actualType = definition.type;
|
|
656
|
+
let validation = definition.validate;
|
|
657
|
+
// Handle bare RegExp shorthand: /regex/
|
|
658
|
+
if (definition.type instanceof RegExp) {
|
|
659
|
+
actualType = String;
|
|
660
|
+
validation = definition.type; // The RegExp becomes the validation
|
|
661
|
+
}
|
|
662
|
+
// Handle validated string shorthand: ["value1", "value2"] or [/regex/]
|
|
663
|
+
else if (isValidatedStringType(definition.type)) {
|
|
664
|
+
actualType = String;
|
|
665
|
+
validation = definition.type; // The array becomes the validation
|
|
666
|
+
}
|
|
667
|
+
// Handle validated number shorthand: [1, 2, 3]
|
|
668
|
+
else if (isValidatedNumberType(definition.type)) {
|
|
669
|
+
actualType = Number;
|
|
670
|
+
validation = definition.type; // The array becomes the validation
|
|
671
|
+
}
|
|
672
|
+
// Fabric to target type
|
|
673
|
+
const convertedValue = fabric(processedValue, actualType);
|
|
674
|
+
// Check if required field is missing
|
|
675
|
+
if (convertedValue === undefined && isFieldRequired$1(definition)) {
|
|
676
|
+
throw new errors.BadRequestError(`Missing required field "${fieldName}"`);
|
|
677
|
+
}
|
|
678
|
+
// Run validation if provided
|
|
679
|
+
if (validation !== undefined && convertedValue !== undefined) {
|
|
680
|
+
await runValidation(convertedValue, validation, fieldName);
|
|
681
|
+
}
|
|
682
|
+
return convertedValue;
|
|
683
|
+
}
|
|
684
|
+
/**
|
|
685
|
+
* Fabric a service function
|
|
686
|
+
*
|
|
687
|
+
* Service builds a function that initiates a "controller" step that:
|
|
688
|
+
* - Parses the input if it is a string to object
|
|
689
|
+
* - Fabrics each input field to its type
|
|
690
|
+
* - Calls the validation function or regular expression or checks the array
|
|
691
|
+
* - Calls the service function and returns the response
|
|
692
|
+
*
|
|
693
|
+
* The returned function has config properties for introspection.
|
|
694
|
+
*/
|
|
695
|
+
function fabricService(config) {
|
|
696
|
+
const { input: inputDefinitions, service } = config;
|
|
697
|
+
const handler = async (rawInput, context) => {
|
|
698
|
+
// Parse input (handles string JSON)
|
|
699
|
+
const parsedInput = parseInput(rawInput);
|
|
700
|
+
// If no input definitions, pass through to service or return parsed input
|
|
701
|
+
if (!inputDefinitions) {
|
|
702
|
+
if (service) {
|
|
703
|
+
return service(parsedInput, context);
|
|
704
|
+
}
|
|
705
|
+
return parsedInput;
|
|
706
|
+
}
|
|
707
|
+
// Process all fields in parallel
|
|
708
|
+
const entries = Object.entries(inputDefinitions);
|
|
709
|
+
const processedValues = await Promise.all(entries.map(([fieldName, definition]) => processField(fieldName, parsedInput[fieldName], definition)));
|
|
710
|
+
// Build processed input object
|
|
711
|
+
const processedInput = {};
|
|
712
|
+
entries.forEach(([fieldName], index) => {
|
|
713
|
+
processedInput[fieldName] = processedValues[index];
|
|
714
|
+
});
|
|
715
|
+
// Return processed input if no service, otherwise call service
|
|
716
|
+
if (service) {
|
|
717
|
+
return service(processedInput, context);
|
|
718
|
+
}
|
|
719
|
+
return processedInput;
|
|
720
|
+
};
|
|
721
|
+
// Attach config properties directly to handler for flat access
|
|
722
|
+
const typedHandler = handler;
|
|
723
|
+
typedHandler.$fabric = FABRIC_VERSION;
|
|
724
|
+
if (config.alias !== undefined)
|
|
725
|
+
typedHandler.alias = config.alias;
|
|
726
|
+
if (config.description !== undefined)
|
|
727
|
+
typedHandler.description = config.description;
|
|
728
|
+
if (config.input !== undefined)
|
|
729
|
+
typedHandler.input = config.input;
|
|
730
|
+
if (config.service !== undefined)
|
|
731
|
+
typedHandler.service = config.service;
|
|
732
|
+
return typedHandler;
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
// Resolve inline service definitions to full Service objects
|
|
736
|
+
/**
|
|
737
|
+
* Type guard to check if a value is a pre-instantiated Service
|
|
738
|
+
* A Service is a function with the `$fabric` property set by fabricService
|
|
739
|
+
*/
|
|
740
|
+
function isService(value) {
|
|
741
|
+
return typeof value === "function" && "$fabric" in value;
|
|
742
|
+
}
|
|
743
|
+
/**
|
|
744
|
+
* Resolve a service configuration to a full Service object
|
|
745
|
+
*
|
|
746
|
+
* Supports two patterns:
|
|
747
|
+
* 1. Inline service definition - pass a plain function as `service` along with
|
|
748
|
+
* `alias`, `description`, and `input` in the config
|
|
749
|
+
* 2. Pre-instantiated Service - pass a Service object as `service`
|
|
750
|
+
*
|
|
751
|
+
* When a pre-instantiated Service is passed, config fields act as overrides:
|
|
752
|
+
* - `alias` overrides service.alias
|
|
753
|
+
* - `description` overrides service.description
|
|
754
|
+
* - `input` overrides service.input
|
|
755
|
+
*
|
|
756
|
+
* The original Service is never mutated - a new Service is created when overrides
|
|
757
|
+
* are applied.
|
|
758
|
+
*
|
|
759
|
+
* @example
|
|
760
|
+
* ```typescript
|
|
761
|
+
* // Inline service definition
|
|
762
|
+
* const service = resolveService({
|
|
763
|
+
* alias: "greet",
|
|
764
|
+
* description: "Greet a user",
|
|
765
|
+
* input: { name: { type: String } },
|
|
766
|
+
* service: ({ name }) => `Hello, ${name}!`,
|
|
767
|
+
* });
|
|
768
|
+
*
|
|
769
|
+
* // Pre-instantiated with override
|
|
770
|
+
* const baseService = fabricService({ alias: "foo", service: (x) => x });
|
|
771
|
+
* const overridden = resolveService({
|
|
772
|
+
* alias: "bar", // Override alias
|
|
773
|
+
* service: baseService,
|
|
774
|
+
* });
|
|
775
|
+
* ```
|
|
776
|
+
*/
|
|
777
|
+
function resolveService(config) {
|
|
778
|
+
const { alias, description, input, service } = config;
|
|
779
|
+
if (isService(service)) {
|
|
780
|
+
// Service is pre-instantiated - config fields act as overrides
|
|
781
|
+
// Create new Service with merged properties (config overrides service)
|
|
782
|
+
return fabricService({
|
|
783
|
+
alias: alias ?? service.alias,
|
|
784
|
+
description: description ?? service.description,
|
|
785
|
+
input: input ?? service.input,
|
|
786
|
+
service: service.service,
|
|
787
|
+
});
|
|
788
|
+
}
|
|
789
|
+
// Service is an inline function - create Service from config
|
|
790
|
+
return fabricService({
|
|
791
|
+
alias,
|
|
792
|
+
description,
|
|
793
|
+
input,
|
|
794
|
+
service,
|
|
795
|
+
});
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
// Convert fabric input definitions to JSON Schema
|
|
799
|
+
/**
|
|
800
|
+
* Check if a single-element array is a typed array type constructor.
|
|
801
|
+
*/
|
|
802
|
+
function isTypedArrayConstructor(element) {
|
|
803
|
+
return (element === Boolean ||
|
|
804
|
+
element === Number ||
|
|
805
|
+
element === String ||
|
|
806
|
+
element === Object ||
|
|
807
|
+
element === "boolean" ||
|
|
808
|
+
element === "number" ||
|
|
809
|
+
element === "string" ||
|
|
810
|
+
element === "object" ||
|
|
811
|
+
element === "" ||
|
|
812
|
+
(typeof element === "object" &&
|
|
813
|
+
element !== null &&
|
|
814
|
+
!(element instanceof RegExp) &&
|
|
815
|
+
Object.keys(element).length === 0));
|
|
816
|
+
}
|
|
817
|
+
/**
|
|
818
|
+
* Get the JSON Schema type string for a conversion type
|
|
819
|
+
*/
|
|
820
|
+
function getJsonSchemaType(type) {
|
|
821
|
+
// Handle constructors
|
|
822
|
+
if (type === Boolean || type === "boolean")
|
|
823
|
+
return "boolean";
|
|
824
|
+
if (type === Number || type === "number")
|
|
825
|
+
return "number";
|
|
826
|
+
if (type === String || type === "string")
|
|
827
|
+
return "string";
|
|
828
|
+
if (type === Object || type === "object")
|
|
829
|
+
return "object";
|
|
830
|
+
if (type === Array || type === "array")
|
|
831
|
+
return "array";
|
|
832
|
+
// Handle Date type (represented as string in JSON Schema)
|
|
833
|
+
if (isDateType(type))
|
|
834
|
+
return "string";
|
|
835
|
+
// Handle RegExp (converts to string)
|
|
836
|
+
if (type instanceof RegExp)
|
|
837
|
+
return "string";
|
|
838
|
+
// Handle arrays
|
|
839
|
+
if (Array.isArray(type)) {
|
|
840
|
+
// Empty array = untyped array
|
|
841
|
+
if (type.length === 0)
|
|
842
|
+
return "array";
|
|
843
|
+
// Single-element typed array like [String], [Number], etc.
|
|
844
|
+
if (type.length === 1 && isTypedArrayConstructor(type[0])) {
|
|
845
|
+
return "array";
|
|
846
|
+
}
|
|
847
|
+
// Validated string type: ["value1", "value2"] or [/regex/]
|
|
848
|
+
if (type.every((item) => typeof item === "string" || item instanceof RegExp)) {
|
|
849
|
+
return "string";
|
|
850
|
+
}
|
|
851
|
+
// Validated number type: [1, 2, 3]
|
|
852
|
+
if (type.every((item) => typeof item === "number")) {
|
|
853
|
+
return "number";
|
|
854
|
+
}
|
|
855
|
+
}
|
|
856
|
+
// Default to string
|
|
857
|
+
return "string";
|
|
858
|
+
}
|
|
859
|
+
/**
|
|
860
|
+
* Get the array item type for typed arrays
|
|
861
|
+
*/
|
|
862
|
+
function getArrayItemType(type) {
|
|
863
|
+
if (!Array.isArray(type) || type.length !== 1)
|
|
864
|
+
return undefined;
|
|
865
|
+
const element = type[0];
|
|
866
|
+
if (element === Boolean || element === "boolean")
|
|
867
|
+
return "boolean";
|
|
868
|
+
if (element === Number || element === "number")
|
|
869
|
+
return "number";
|
|
870
|
+
if (element === String || element === "string" || element === "")
|
|
871
|
+
return "string";
|
|
872
|
+
if (element === Object || element === "object")
|
|
873
|
+
return "object";
|
|
874
|
+
if (typeof element === "object" &&
|
|
875
|
+
element !== null &&
|
|
876
|
+
!(element instanceof RegExp) &&
|
|
877
|
+
Object.keys(element).length === 0) {
|
|
878
|
+
return "object";
|
|
879
|
+
}
|
|
880
|
+
return undefined;
|
|
881
|
+
}
|
|
882
|
+
/**
|
|
883
|
+
* Get enum values for validated types
|
|
884
|
+
*/
|
|
885
|
+
function getEnumValues(type) {
|
|
886
|
+
if (!Array.isArray(type))
|
|
887
|
+
return undefined;
|
|
888
|
+
if (type.length === 0)
|
|
889
|
+
return undefined;
|
|
890
|
+
if (type.length === 1 && isTypedArrayConstructor(type[0]))
|
|
891
|
+
return undefined;
|
|
892
|
+
// Check if it's a validated string type (strings only, no RegExp)
|
|
893
|
+
if (type.every((item) => typeof item === "string")) {
|
|
894
|
+
return type;
|
|
895
|
+
}
|
|
896
|
+
// Check if it's a validated number type
|
|
897
|
+
if (type.every((item) => typeof item === "number")) {
|
|
898
|
+
return type;
|
|
899
|
+
}
|
|
900
|
+
return undefined;
|
|
901
|
+
}
|
|
902
|
+
/**
|
|
903
|
+
* Check if a field is required
|
|
904
|
+
*/
|
|
905
|
+
function isFieldRequired(definition) {
|
|
906
|
+
if (definition.required === false) {
|
|
907
|
+
return false;
|
|
908
|
+
}
|
|
909
|
+
if (definition.default !== undefined) {
|
|
910
|
+
return false;
|
|
911
|
+
}
|
|
912
|
+
return true;
|
|
913
|
+
}
|
|
914
|
+
/**
|
|
915
|
+
* Convert a single input field definition to JSON Schema property
|
|
916
|
+
*/
|
|
917
|
+
function inputFieldToJsonSchema(definition) {
|
|
918
|
+
const schemaType = getJsonSchemaType(definition.type);
|
|
919
|
+
const property = {
|
|
920
|
+
type: schemaType,
|
|
921
|
+
};
|
|
922
|
+
// Add description
|
|
923
|
+
if (definition.description) {
|
|
924
|
+
property.description = definition.description;
|
|
925
|
+
}
|
|
926
|
+
// Add enum for validated types
|
|
927
|
+
const enumValues = getEnumValues(definition.type);
|
|
928
|
+
if (enumValues) {
|
|
929
|
+
property.enum = enumValues;
|
|
930
|
+
}
|
|
931
|
+
// Add items for typed arrays
|
|
932
|
+
if (schemaType === "array") {
|
|
933
|
+
const itemType = getArrayItemType(definition.type);
|
|
934
|
+
if (itemType) {
|
|
935
|
+
property.items = { type: itemType };
|
|
936
|
+
}
|
|
937
|
+
}
|
|
938
|
+
return property;
|
|
939
|
+
}
|
|
940
|
+
/**
|
|
941
|
+
* Convert fabric input definitions to JSON Schema
|
|
942
|
+
*
|
|
943
|
+
* @param input - The input definitions
|
|
944
|
+
* @param options - Conversion options
|
|
945
|
+
* @returns JSON Schema object compatible with LLM tool parameters
|
|
946
|
+
*/
|
|
947
|
+
function inputToJsonSchema(input, options = {}) {
|
|
948
|
+
const { exclude = [] } = options;
|
|
949
|
+
const schema = {
|
|
950
|
+
properties: {},
|
|
951
|
+
required: [],
|
|
952
|
+
type: "object",
|
|
953
|
+
};
|
|
954
|
+
if (!input) {
|
|
955
|
+
return schema;
|
|
956
|
+
}
|
|
957
|
+
// Sort keys alphabetically
|
|
958
|
+
const sortedKeys = Object.keys(input).sort();
|
|
959
|
+
for (const key of sortedKeys) {
|
|
960
|
+
// Skip excluded fields
|
|
961
|
+
if (exclude.includes(key)) {
|
|
962
|
+
continue;
|
|
963
|
+
}
|
|
964
|
+
const definition = input[key];
|
|
965
|
+
schema.properties[key] = inputFieldToJsonSchema(definition);
|
|
966
|
+
if (isFieldRequired(definition)) {
|
|
967
|
+
schema.required.push(key);
|
|
968
|
+
}
|
|
969
|
+
}
|
|
970
|
+
return schema;
|
|
971
|
+
}
|
|
972
|
+
|
|
973
|
+
// Fabric an LLM tool from a service
|
|
974
|
+
/**
|
|
975
|
+
* Fabric an LLM tool from a fabric service
|
|
976
|
+
*
|
|
977
|
+
* This function creates an LLM tool compatible with @jaypie/llm Toolkit.
|
|
978
|
+
* It automatically:
|
|
979
|
+
* - Uses service.alias as the tool name (or custom name)
|
|
980
|
+
* - Uses service.description as the tool description (or custom)
|
|
981
|
+
* - Converts input definitions to JSON Schema parameters
|
|
982
|
+
* - Wraps the service as the tool's call function
|
|
983
|
+
*
|
|
984
|
+
* @param config - Configuration including service and optional overrides
|
|
985
|
+
* @returns An object containing the fabricated LLM tool
|
|
986
|
+
*
|
|
987
|
+
* @example
|
|
988
|
+
* ```typescript
|
|
989
|
+
* import { fabricService } from "@jaypie/fabric";
|
|
990
|
+
* import { fabricTool } from "@jaypie/fabric/llm";
|
|
991
|
+
* import { Toolkit } from "@jaypie/llm";
|
|
992
|
+
*
|
|
993
|
+
* const myService = fabricService({
|
|
994
|
+
* alias: "greet",
|
|
995
|
+
* description: "Greet a user by name",
|
|
996
|
+
* input: {
|
|
997
|
+
* userName: { type: String, description: "The user's name" },
|
|
998
|
+
* loud: { type: Boolean, default: false, description: "Shout the greeting" },
|
|
999
|
+
* },
|
|
1000
|
+
* service: ({ userName, loud }) => {
|
|
1001
|
+
* const greeting = `Hello, ${userName}!`;
|
|
1002
|
+
* return loud ? greeting.toUpperCase() : greeting;
|
|
1003
|
+
* },
|
|
1004
|
+
* });
|
|
1005
|
+
*
|
|
1006
|
+
* const { tool } = fabricTool({ service: myService });
|
|
1007
|
+
*
|
|
1008
|
+
* // Use with Toolkit
|
|
1009
|
+
* const toolkit = new Toolkit([tool]);
|
|
1010
|
+
* ```
|
|
1011
|
+
*/
|
|
1012
|
+
function fabricTool(config) {
|
|
1013
|
+
const { alias, description, exclude, input, message, name, onComplete, onError, onFatal, onMessage, service: serviceOrFunction, } = config;
|
|
1014
|
+
// Resolve inline service or apply overrides to pre-instantiated service
|
|
1015
|
+
const service = resolveService({
|
|
1016
|
+
alias,
|
|
1017
|
+
description,
|
|
1018
|
+
input,
|
|
1019
|
+
service: serviceOrFunction,
|
|
1020
|
+
});
|
|
1021
|
+
// Determine tool name (priority: name > service.alias > "tool")
|
|
1022
|
+
const toolName = name ?? service.alias ?? "tool";
|
|
1023
|
+
// Determine tool description
|
|
1024
|
+
const toolDescription = service.description ?? "";
|
|
1025
|
+
// Convert input definitions to JSON Schema
|
|
1026
|
+
const parameters = inputToJsonSchema(service.input, { exclude });
|
|
1027
|
+
// Create context callbacks that wrap with error swallowing
|
|
1028
|
+
const sendMessage = onMessage
|
|
1029
|
+
? async (msg) => {
|
|
1030
|
+
try {
|
|
1031
|
+
await onMessage(msg);
|
|
1032
|
+
}
|
|
1033
|
+
catch {
|
|
1034
|
+
// Swallow errors - callback failures should not halt execution
|
|
1035
|
+
}
|
|
1036
|
+
}
|
|
1037
|
+
: undefined;
|
|
1038
|
+
const contextOnError = onError
|
|
1039
|
+
? async (error) => {
|
|
1040
|
+
try {
|
|
1041
|
+
await onError(error);
|
|
1042
|
+
}
|
|
1043
|
+
catch {
|
|
1044
|
+
// Swallow errors - callback failures should not halt execution
|
|
1045
|
+
}
|
|
1046
|
+
}
|
|
1047
|
+
: undefined;
|
|
1048
|
+
const contextOnFatal = onFatal
|
|
1049
|
+
? async (error) => {
|
|
1050
|
+
try {
|
|
1051
|
+
await onFatal(error);
|
|
1052
|
+
}
|
|
1053
|
+
catch {
|
|
1054
|
+
// Swallow errors - callback failures should not halt execution
|
|
1055
|
+
}
|
|
1056
|
+
}
|
|
1057
|
+
: undefined;
|
|
1058
|
+
// Create context for the service
|
|
1059
|
+
const context = {
|
|
1060
|
+
onError: contextOnError,
|
|
1061
|
+
onFatal: contextOnFatal,
|
|
1062
|
+
sendMessage,
|
|
1063
|
+
};
|
|
1064
|
+
// Create the call function that invokes the service
|
|
1065
|
+
const call = async (args) => {
|
|
1066
|
+
try {
|
|
1067
|
+
const result = await service(args, context);
|
|
1068
|
+
// Call onComplete if provided
|
|
1069
|
+
if (onComplete) {
|
|
1070
|
+
try {
|
|
1071
|
+
await onComplete(result);
|
|
1072
|
+
}
|
|
1073
|
+
catch {
|
|
1074
|
+
// Swallow errors - callback failures should not halt execution
|
|
1075
|
+
}
|
|
1076
|
+
}
|
|
1077
|
+
return result;
|
|
1078
|
+
}
|
|
1079
|
+
catch (error) {
|
|
1080
|
+
// Any thrown error is fatal
|
|
1081
|
+
if (onFatal) {
|
|
1082
|
+
await onFatal(error);
|
|
1083
|
+
}
|
|
1084
|
+
else if (onError) {
|
|
1085
|
+
await onError(error);
|
|
1086
|
+
}
|
|
1087
|
+
throw error;
|
|
1088
|
+
}
|
|
1089
|
+
};
|
|
1090
|
+
// Build the tool
|
|
1091
|
+
const tool = {
|
|
1092
|
+
call,
|
|
1093
|
+
description: toolDescription,
|
|
1094
|
+
name: toolName,
|
|
1095
|
+
parameters,
|
|
1096
|
+
type: "function",
|
|
1097
|
+
};
|
|
1098
|
+
// Add message if provided
|
|
1099
|
+
if (message !== undefined) {
|
|
1100
|
+
tool.message = message;
|
|
1101
|
+
}
|
|
1102
|
+
return { tool };
|
|
1103
|
+
}
|
|
1104
|
+
|
|
1105
|
+
exports.fabricTool = fabricTool;
|
|
1106
|
+
exports.inputToJsonSchema = inputToJsonSchema;
|
|
1107
|
+
//# sourceMappingURL=index.cjs.map
|