Merge branch 'main' into refactor/category-dialog

This commit is contained in:
yassinedorbozgithub 2025-02-04 05:41:35 +01:00
commit 5c5db5b6af
7 changed files with 52 additions and 70 deletions

View File

@ -1,28 +1,36 @@
/*
* Copyright © 2024 Hexastack. All rights reserved.
* Copyright © 2025 Hexastack. All rights reserved.
*
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
* 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file).
*/
import { z } from 'zod';
export enum ButtonType {
postback = 'postback',
web_url = 'web_url',
}
export type PostBackButton = {
type: ButtonType.postback;
title: string;
payload: string;
};
const postBackButtonSchema = z.object({
type: z.literal(ButtonType.postback),
title: z.string(),
payload: z.string(),
});
export type WebUrlButton = {
type: ButtonType.web_url;
title: string;
url: string;
messenger_extensions?: boolean;
webview_height_ratio?: 'compact' | 'tall' | 'full';
};
const webUrlButtonSchema = z.object({
type: z.literal(ButtonType.web_url),
title: z.string(),
url: z.string().url(),
messenger_extensions: z.boolean().optional(),
webview_height_ratio: z.enum(['compact', 'tall', 'full']).optional(),
});
export type Button = PostBackButton | WebUrlButton;
export const buttonSchema = z.union([postBackButtonSchema, webUrlButtonSchema]);
export type PostBackButton = z.infer<typeof postBackButtonSchema>;
export type WebUrlButton = z.infer<typeof webUrlButtonSchema>;
export type Button = z.infer<typeof buttonSchema>;

View File

@ -13,7 +13,7 @@ import { z } from 'zod';
// entity is `String` for NLP entities
export const captureVarSchema = z.object({
entity: z.union([z.number().min(-2).max(-1), z.string()]),
context_var: z.string(),
context_var: z.string().regex(/^[a-z][a-z_0-9]*$/),
});
export type CaptureVar = z.infer<typeof captureVarSchema>;

View File

@ -1,11 +1,13 @@
/*
* Copyright © 2024 Hexastack. All rights reserved.
* Copyright © 2025 Hexastack. All rights reserved.
*
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
* 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file).
*/
import { z } from 'zod';
import { ChannelName } from '@/channel/types';
export type SubscriberChannelData<C extends ChannelName = 'unknown-channel'> =
@ -17,3 +19,11 @@ export type SubscriberChannelData<C extends ChannelName = 'unknown-channel'> =
// Channel's specific attributes
[P in keyof SubscriberChannelDict[C]]: SubscriberChannelDict[C][P];
};
export const channelDataSchema = z
.object({
name: z.string().regex(/-channel$/) as z.ZodType<ChannelName>,
})
.passthrough();
export type Channel = z.infer<typeof channelDataSchema>;

View File

@ -1,5 +1,5 @@
/*
* Copyright © 2024 Hexastack. All rights reserved.
* Copyright © 2025 Hexastack. All rights reserved.
*
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
@ -13,17 +13,15 @@ import {
ValidatorConstraintInterface,
} from 'class-validator';
export function isChannelData(channel: any) {
return (
typeof channel === 'object' &&
channel.name &&
typeof channel.name === 'string'
);
import { Channel, channelDataSchema } from '../schemas/types/channel';
export function isChannelData(channel: Channel) {
return channelDataSchema.safeParse(channel).success;
}
@ValidatorConstraint({ async: false })
export class ChannelDataValidator implements ValidatorConstraintInterface {
validate(channel: any) {
validate(channel: Channel) {
return isChannelData(channel);
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright © 2024 Hexastack. All rights reserved.
* Copyright © 2025 Hexastack. All rights reserved.
*
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
@ -13,18 +13,10 @@ import {
ValidatorConstraintInterface,
} from 'class-validator';
import { Position } from '../schemas/types/position';
import { Position, positionSchema } from '../schemas/types/position';
export function isPosition(position: Position) {
return (
typeof position === 'object' &&
!isNaN(position.x) &&
!isNaN(position.y) &&
position.x !== Infinity &&
position.x !== -Infinity &&
position.y !== Infinity &&
position.y !== -Infinity
);
return positionSchema.safeParse(position).success;
}
@ValidatorConstraint({ async: false })

View File

@ -1,5 +1,5 @@
/*
* Copyright © 2024 Hexastack. All rights reserved.
* Copyright © 2025 Hexastack. All rights reserved.
*
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
@ -12,43 +12,17 @@ import {
ValidatorConstraint,
ValidatorConstraintInterface,
} from 'class-validator';
import Joi from 'joi';
type Tentity = -1 | -2;
export interface CaptureVar {
// entity=`-1` to match text message
// entity=`-2` for postback payload
// entity is `String` for NLP entities
entity: Tentity | string;
context_var: string;
}
const allowedEntityValues: Tentity[] = [-1, -2];
import { CaptureVar, captureVarSchema } from '../schemas/types/capture-var';
export function isValidVarCapture(vars: CaptureVar[]) {
const captureSchema = Joi.array().items(
Joi.object().keys({
entity: Joi.alternatives().try(
// `-1` to match text message & `-2` for postback payload
Joi.number()
.valid(...allowedEntityValues)
.required(),
// String for NLP entities
Joi.string().required(),
),
context_var: Joi.string()
.regex(/^[a-z][a-z_0-9]*$/)
.required(),
}),
);
const captureCheck = captureSchema.validate(vars);
if (captureCheck.error) {
// eslint-disable-next-line
console.log('Capture vars validation failed!', captureCheck.error);
if (!Array.isArray(vars)) {
return false;
}
return !captureCheck.error;
return vars.every(
(captureVar) => captureVarSchema.safeParse(captureVar).success,
);
}
@ValidatorConstraint({ async: false })

View File

@ -13,6 +13,7 @@ import {
import { BlockFull } from '@/chat/schemas/block.schema';
import { FileType } from '@/chat/schemas/types/attachment';
import { ButtonType } from '@/chat/schemas/types/button';
import { CaptureVar } from '@/chat/schemas/types/capture-var';
import {
OutgoingMessageFormat,
PayloadType,
@ -20,7 +21,6 @@ import {
import { BlockOptions, ContentOptions } from '@/chat/schemas/types/options';
import { Pattern } from '@/chat/schemas/types/pattern';
import { QuickReplyType } from '@/chat/schemas/types/quick-reply';
import { CaptureVar } from '@/chat/validation-rules/is-valid-capture';
import { modelInstance } from './misc';