UNPKG

ai-utils.js

Version:

Build AI applications, chatbots, and agents with JavaScript and TypeScript.

32 lines (31 loc) 1.09 kB
import { z } from "zod"; import SecureJSON from "secure-json-parse"; import { ApiCallError } from "../../util/api/ApiCallError.js"; export const automatic1111ErrorDataSchema = z.object({ error: z.string(), detail: z.string(), body: z.string(), errors: z.string(), }); export class Automatic1111Error extends ApiCallError { constructor({ data, statusCode, url, requestBodyValues, message = data.detail, }) { super({ message, statusCode, requestBodyValues, url }); Object.defineProperty(this, "data", { enumerable: true, configurable: true, writable: true, value: void 0 }); this.data = data; } } export const failedAutomatic1111CallResponseHandler = async ({ response, url, requestBodyValues }) => { const responseBody = await response.text(); const parsedError = automatic1111ErrorDataSchema.parse(SecureJSON.parse(responseBody)); return new Automatic1111Error({ url, requestBodyValues, statusCode: response.status, data: parsedError, }); };