Skip to content

feat: add contextVar regex validation #921

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions api/src/chat/controllers/context-var.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ describe('ContextVarController', () => {
label: 'contextVarLabel2',
name: 'test_add',
permanent: false,
pattern: '/.+/',
};
const result = await contextVarController.create(contextVarCreateDto);

Expand Down
12 changes: 10 additions & 2 deletions api/src/chat/dto/context-var.dto.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/*
* 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 { ApiProperty, PartialType } from '@nestjs/swagger';
import { ApiProperty, ApiPropertyOptional, PartialType } from '@nestjs/swagger';
import { IsBoolean, IsNotEmpty, IsOptional, IsString } from 'class-validator';

import { DtoConfig } from '@/utils/types/dto.types';
Expand All @@ -26,6 +26,14 @@ export class ContextVarCreateDto {
@IsOptional()
@IsBoolean()
permanent?: boolean;

@ApiPropertyOptional({
description: 'Context var pattern',
type: String,
})
@IsOptional()
@IsString()
pattern?: string;
}

export class ContextVarUpdateDto extends PartialType(ContextVarCreateDto) {}
Expand Down
12 changes: 11 additions & 1 deletion api/src/chat/schemas/context-var.schema.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -10,8 +10,11 @@ import { ModelDefinition, Prop, Schema, SchemaFactory } from '@nestjs/mongoose';

import { BaseSchema } from '@/utils/generics/base-schema';
import { LifecycleHookManager } from '@/utils/generics/lifecycle-hook-manager';
import { buildZodSchemaValidator } from '@/utils/helpers/zod-validation';
import { THydratedDocument } from '@/utils/types/filter.types';

import { stringRegexPatternSchema } from './types/pattern';

@Schema({ timestamps: true })
export class ContextVar extends BaseSchema {
@Prop({
Expand Down Expand Up @@ -39,6 +42,13 @@ export class ContextVar extends BaseSchema {
default: false,
})
permanent: boolean;

@Prop({
type: String,
validate: buildZodSchemaValidator(stringRegexPatternSchema),
default: '/.+/',
})
pattern?: string;
}

export const ContextVarModel: ModelDefinition = LifecycleHookManager.attach({
Expand Down
6 changes: 3 additions & 3 deletions api/src/chat/services/context-var.service.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -30,11 +30,11 @@ export class ContextVarService extends BaseService<
* Retrieves a mapping of context variable names to their corresponding `ContextVar` objects for a given block.
*
* @param {Block | BlockFull} block - The block containing the capture variables to retrieve context variables for.
* @returns {Promise<Record<string, ContextVar>>} A promise that resolves to a record mapping context variable names to `ContextVar` objects.
* @returns {Promise<Record<string, ContextVar | undefined>>} A promise that resolves to a record mapping context variable names to `ContextVar` objects or undefined if not found.
*/
async getContextVarsByBlock(
block: Block | BlockFull,
): Promise<Record<string, ContextVar>> {
): Promise<Record<string, ContextVar | undefined>> {
const vars = await this.find({
name: { $in: block.capture_vars.map(({ context_var }) => context_var) },
});
Expand Down
38 changes: 29 additions & 9 deletions api/src/chat/services/conversation.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* 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 { Injectable, Logger } from '@nestjs/common';
import { Injectable } from '@nestjs/common';

import EventWrapper from '@/channel/lib/EventWrapper';
import { BaseService } from '@/utils/generics/base-service';
Expand Down Expand Up @@ -114,16 +114,36 @@ export class ConversationService extends BaseService<
contextValue =
typeof contextValue === 'string' ? contextValue.trim() : contextValue;

if (
profile.context?.vars &&
contextVars[capture.context_var]?.permanent
) {
Logger.debug(
`Adding context var to subscriber: ${capture.context_var} = ${contextValue}`,
const context_var = contextVars[capture.context_var];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 98-100.
There's obsolete commented out code. Can we get rid of it ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the feedback, your are right but.
Let's keep the update as short as possible to facilitate the review process.
I propose to create a dedicated issue for the recommended update.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a duplicate issue. I already created it previously. See the original one

const { permanent, pattern = '' } = context_var || {};
const isValidContextValue = (() => {
try {
return new RegExp(pattern.slice(1, -1)).test(`${contextValue}`);
} catch (error) {
this.logger.error(
`Invalid regex pattern for ContextVar[${capture.context_var}]: ${pattern}`,
);
return false;
}
})();

if (isValidContextValue) {
if (profile.context?.vars && permanent) {
this.logger.debug(
`Adding context var to subscriber: ${capture.context_var} = ${contextValue}`,
);
profile.context.vars[capture.context_var] = contextValue;
}
convo.context!.vars[capture.context_var] = contextValue;
} else {
this.logger.warn(
`ContextVar[${capture.context_var}] value does not match the pattern!`,
);
this.logger.debug(`value:"${contextValue}" pattern:"${pattern}"`);
this.logger.warn(
`ContextVar[${capture.context_var}] update skipped!`,
);
profile.context.vars[capture.context_var] = contextValue;
}
convo.context!.vars[capture.context_var] = contextValue;
});
}

Expand Down
1 change: 1 addition & 0 deletions api/src/utils/test/fixtures/contextvar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type TContentVarFixtures = FixturesTypeBuilder<ContextVar, ContextVarCreateDto>;

export const contentVarDefaultValues: TContentVarFixtures['defaultValues'] = {
permanent: false,
pattern: '/.+/',
};

const contextVars: TContentVarFixtures['values'][] = [
Expand Down
37 changes: 37 additions & 0 deletions frontend/src/components/context-vars/ContextVarForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { Controller, useForm } from "react-hook-form";

import { ContentContainer, ContentItem } from "@/app-components/dialogs";
import { Input } from "@/app-components/inputs/Input";
import { RegexInput } from "@/app-components/inputs/RegexInput";
import { useCreate } from "@/hooks/crud/useCreate";
import { useUpdate } from "@/hooks/crud/useUpdate";
import { useToast } from "@/hooks/useToast";
Expand All @@ -21,6 +22,8 @@ import { ComponentFormProps } from "@/types/common/dialogs.types";
import { IContextVar, IContextVarAttributes } from "@/types/context-var.types";
import { slugify } from "@/utils/string";

import { isRegex } from "../visual-editor/form/inputs/triggers/PatternInput";

export const ContextVarForm: FC<ComponentFormProps<IContextVar>> = ({
data,
Wrapper = Fragment,
Expand Down Expand Up @@ -59,6 +62,7 @@ export const ContextVarForm: FC<ComponentFormProps<IContextVar>> = ({
name: data?.name || "",
label: data?.label || "",
permanent: data?.permanent || false,
pattern: data?.pattern || "",
},
});
const validationRules = {
Expand Down Expand Up @@ -86,6 +90,7 @@ export const ContextVarForm: FC<ComponentFormProps<IContextVar>> = ({
name: data.name,
label: data.label,
permanent: data.permanent,
pattern: data.pattern,
});
} else {
reset();
Expand Down Expand Up @@ -135,6 +140,38 @@ export const ContextVarForm: FC<ComponentFormProps<IContextVar>> = ({
/>
<FormHelperText>{t("help.permanent")}</FormHelperText>
</ContentItem>
<ContentItem>
<Controller
name="pattern"
control={control}
render={({ field: { value, ...rest } }) => (
<RegexInput
{...rest}
{...register("pattern", {
validate: (pattern) => {
try {
if (pattern) {
new RegExp(pattern.slice(1, -1));

return true;
}

return false;
} catch (_e) {
return t("message.regex_is_invalid");
}
},
setValueAs: (v) => (isRegex(v) ? v : `/${v}/`),
})}
error={!!errors.pattern}
value={value?.slice(1, -1) || ""}
label={t("label.regex")}
onChange={rest.onChange}
helperText={errors.pattern ? errors.pattern.message : null}
/>
)}
/>
</ContentItem>
</ContentContainer>
</form>
</Wrapper>
Expand Down
11 changes: 10 additions & 1 deletion frontend/src/components/context-vars/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import { faAsterisk } from "@fortawesome/free-solid-svg-icons";
import AddIcon from "@mui/icons-material/Add";
import DeleteIcon from "@mui/icons-material/Delete";
import { Button, Grid, Paper, Switch } from "@mui/material";
import { Button, Chip, Grid, Paper, Switch } from "@mui/material";
import { GridColDef, GridRowSelectionModel } from "@mui/x-data-grid";
import { useState } from "react";

Expand Down Expand Up @@ -110,6 +110,15 @@ export const ContextVars = () => {
renderHeader,
headerAlign: "left",
},
{
flex: 1,
field: "pattern",
headerName: t("label.regex"),
disableColumnMenu: true,
renderHeader,
headerAlign: "left",
renderCell: ({ row }) => <Chip label={row?.pattern} variant="role" />,
},
{
maxWidth: 120,
field: "permanent",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
import { OutcomeInput } from "./OutcomeInput";
import { PostbackInput } from "./PostbackInput";

const isRegex = (str: Pattern) => {
export const isRegex = (str: Pattern) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can do this instead to check for regex validation:

  try {
    new RegExp(str.slice(1, -1));
    return true;
  } catch (e) {
    return false;
  }

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The isRegex function is used to detect from an input the corresponding form type to show.
If we use this function the text case will be impacted directly.

return typeof str === "string" && str.startsWith("/") && str.endsWith("/");
};
const getType = (pattern: Pattern): PatternType => {
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/types/context-var.types.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -14,6 +14,7 @@ export interface IContextVarAttributes {
name: string;
label: string;
permanent: boolean;
pattern?: string;
}

export interface IContextVarStub
Expand Down