ai-utils.js
Version:
Build AI applications, chatbots, and agents with JavaScript and TypeScript.
29 lines (28 loc) • 992 B
JavaScript
import SecureJSON from "secure-json-parse";
import { z } from "zod";
import { ApiCallError } from "../../util/api/ApiCallError.js";
export const cohereErrorDataSchema = z.object({
message: z.string(),
});
export class CohereError extends ApiCallError {
constructor({ data, statusCode, url, requestBodyValues, message = data.message, }) {
super({ message, statusCode, requestBodyValues, url });
Object.defineProperty(this, "data", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
this.data = data;
}
}
export const failedCohereCallResponseHandler = async ({ response, url, requestBodyValues }) => {
const responseBody = await response.text();
const parsedError = cohereErrorDataSchema.parse(SecureJSON.parse(responseBody));
return new CohereError({
url,
requestBodyValues,
statusCode: response.status,
data: parsedError,
});
};