-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathutil.ts
318 lines (275 loc) · 7.08 KB
/
util.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import { InferableError } from "./errors";
import Ajv from "ajv";
import addFormats from "ajv-formats";
import { z } from "zod";
import { JsonSchemaInput, JsonSchema } from "./types";
import { blobSchema } from "./contract";
type ValidationError = {
path: string;
error: string;
};
// Name restriction for Services and Functions
const ALLOWED_NAME_CHARACTERS = /^[a-zA-Z0-9]+$/;
const MAX_NAME_LENGTH = 30;
export const validateFunctionArgs = (schema: any, args: unknown) => {
try {
if (isZodType(schema)) {
schema.parse(args);
} else {
const ajv = new Ajv();
addFormats(ajv);
ajv.compile({
...schema,
$schema: undefined,
});
ajv.validate(schema, args);
}
} catch (e: unknown) {}
};
export const validateServiceName = (name: string) => {
if (!ALLOWED_NAME_CHARACTERS.test(name)) {
throw new InferableError(
`Service name must only contain letters and numbers. Got: ${name}`,
);
}
if (name.length > MAX_NAME_LENGTH) {
throw new InferableError(
`Service name must be less than ${MAX_NAME_LENGTH} characters: Got ${name} with length ${name.length}.`,
);
}
};
export const validateFunctionName = (name: string) => {
if (!ALLOWED_NAME_CHARACTERS.test(name)) {
throw new InferableError(
`Function name must only contain letters and numbers. Got: ${name}`,
);
}
};
export const validatePropertyName = (name: string) => {
const ALLOWED_PROPERTY_NAME_CHARACTERS = /^[a-zA-Z0-9_]+$/;
if (!ALLOWED_PROPERTY_NAME_CHARACTERS.test(name)) {
throw new InferableError(
`Property name must only contain letters, numbers and underscore '_'. Got: ${name}`,
);
}
};
export const validateDescription = (description?: string) => {
if (description === "") {
throw new InferableError("Description must not be empty");
}
};
/*
* Validate a function schema.
*/
export const validateFunctionSchema = (
input: JsonSchemaInput,
): { path: string; error: string }[] => {
delete input.properties?.undefined;
if (!input || !input.properties) {
return [{ path: "", error: "Schema must be defined" }];
}
const errors = Object.keys(input.properties)
.map((key) => {
return validateProperty(key, input.properties[key]);
})
.flat();
if (errors.length > 0) {
return errors;
}
const ajv = new Ajv();
addFormats(ajv);
try {
ajv.compile({
...input,
$schema: undefined,
});
} catch (error) {
if (error instanceof Error) {
return ajvErrorToFailures(error);
}
throw new InferableError("Unknown JSON schema compilation error", {
error,
});
}
return [];
};
/**
* Recursively validate $.properties
*/
const validateProperty = (
key: string,
value: JsonSchema,
): ValidationError[] => {
let errors: ValidationError[] = [];
try {
validatePropertyName(key);
} catch (error) {
if (error instanceof Error) {
errors.push({
path: `${key}`,
error: error.message,
});
} else {
throw error;
}
}
if (value && typeof value === "object" && "properties" in value) {
const properties = (value.properties as Record<string, JsonSchema>) || {};
errors = errors.concat(
Object.keys(properties)
.map((key) => {
return validateProperty(key, properties[key]);
})
.flat(),
);
}
return errors;
};
/*
* Accepts an AJV compilation error and extracts the error details from the message.
*/
export const ajvErrorToFailures = (
error: Error,
): { path: string; error: string }[] => {
// example: /data/properties/name some error message
if (error.message.startsWith("schema is invalid:")) {
return error.message
.replace("schema is invalid:", "")
.split(",")
.map((s) => s.trim())
.map((s) => {
const firstSpace = s.indexOf(" ");
if (firstSpace === -1) {
throw new InferableError(
"Could not extract failures from AJV error",
{
error,
},
);
}
return {
path: s.slice(0, firstSpace),
error: s.slice(firstSpace + 1),
};
});
}
return [
{
path: "",
error: error.message,
},
];
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const isZodType = (input: any): input is z.ZodTypeAny => {
return input?._def?.typeName;
};
export const BLOB_DATA_KEY = "__inferable_blob_data";
export const extractBlobs = (
content: unknown,
): { content: unknown; blobs: z.infer<typeof blobExtractionSchema>[] } => {
if (!content || typeof content !== "object") {
return {
blobs: [],
content,
};
}
const keys = Object.keys(content);
if (keys.length === 0) {
return {
blobs: [],
content,
};
}
const blobs: Record<string, z.infer<typeof blobExtractionSchema>> = {};
for (const key of keys) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const value = (content as any)[key];
if (value && typeof value === "object" && BLOB_DATA_KEY in value) {
const parsedBlob = blobExtractionSchema.safeParse(value[BLOB_DATA_KEY]);
if (!parsedBlob.success) {
console.error(parsedBlob.error);
throw new InferableError("Found invalid Blob data");
}
blobs[key] = parsedBlob.data;
}
}
return {
blobs: Object.values(blobs),
content: Object.fromEntries(
Object.entries(content).filter(([key]) => !blobs[key]),
),
};
};
const blobExtractionSchema = blobSchema
.omit({
id: true,
createdAt: true,
runId: true,
jobId: true,
})
.and(
z.object({
data: z.string(),
}),
);
type BlobType = "application/json" | "image/png" | "image/jpeg";
export const blob = ({
name,
data,
type,
}: {
name: string;
type: BlobType;
data: Buffer | object;
}) => {
if (!(data instanceof Buffer)) {
if (type !== "application/json") {
throw new InferableError("Object type must be application/json");
}
data = Buffer.from(JSON.stringify(data));
}
const encoded = data.toString("base64");
return {
[BLOB_DATA_KEY]: {
name,
type,
encoding: "base64",
data: encoded,
size: Buffer.from(encoded).byteLength,
},
};
};
export const INTERRUPT_KEY = "__inferable_interrupt";
type VALID_INTERRUPT_TYPES = "approval";
const interruptResultSchema = z.discriminatedUnion("type", [
z.object({
type: z.literal("approval"),
}),
]);
export const extractInterrupt = (
input: unknown,
): z.infer<typeof interruptResultSchema> | undefined => {
if (input && typeof input === "object" && INTERRUPT_KEY in input) {
const parsedInterrupt = interruptResultSchema.safeParse(
input[INTERRUPT_KEY],
);
if (!parsedInterrupt.success) {
throw new InferableError("Found invalid Interrupt data");
}
return parsedInterrupt.data;
}
};
export class Interrupt {
[INTERRUPT_KEY]: {
type: VALID_INTERRUPT_TYPES;
};
constructor(type: VALID_INTERRUPT_TYPES) {
this[INTERRUPT_KEY] = {
type,
};
}
static approval() {
return new Interrupt("approval");
}
}