feat: if-rule

This commit is contained in:
tomasz-io 2025-04-03 13:36:33 -07:00
parent b39cb836ad
commit 211fcb7740
2 changed files with 27 additions and 4 deletions

View file

@ -453,16 +453,22 @@ function getMessageStringContent(message?: UserChatMessage): string {
const getSystemMessage = ({
userMessage,
rules,
currentModel,
}: {
userMessage?: UserChatMessage;
rules: Rule[];
currentModel: string;
}) => {
const messageStringContent = getMessageStringContent(userMessage);
const filePathsFromMessage = extractPathsFromCodeBlocks(messageStringContent);
return rules
.filter((rule) => {
return isRuleActive({ rule, activePaths: filePathsFromMessage });
return isRuleActive({
rule,
activePaths: filePathsFromMessage,
currentModel,
});
})
.map((rule) => {
if (typeof rule === "string") {
@ -525,6 +531,7 @@ function compileChatMessages({
}
const lastUserMessage = getLastUserMessage(msgsCopy);
debugger;
msgsCopy = addSystemMessage({
messages: msgsCopy,
@ -533,6 +540,7 @@ function compileChatMessages({
getSystemMessage({
userMessage: lastUserMessage,
rules,
currentModel: modelName,
}),
originalMessages: msgs,
});

View file

@ -10,7 +10,19 @@ const createGlobFunction = (activePaths: string[]) => {
};
};
const evaluateIf = (condition: string, activePaths: string[]) => {
const contains = (str: string, searchStr: string): boolean => {
return str.toLowerCase().includes(searchStr.toLowerCase());
};
const evaluateIf = ({
condition,
activePaths,
currentModel,
}: {
condition: string;
activePaths: string[];
currentModel: string;
}) => {
const expression = condition.match(TEMPLATE_VAR_REGEX)?.[1]?.trim();
if (!expression) {
return true;
@ -20,10 +32,11 @@ const evaluateIf = (condition: string, activePaths: string[]) => {
const evaluate = compileExpression(expression, {
extraFunctions: {
glob: createGlobFunction(activePaths),
contains,
},
constants: {
current: {
model: "gpt-4",
model: { model: currentModel },
},
},
});
@ -38,16 +51,18 @@ const evaluateIf = (condition: string, activePaths: string[]) => {
export const isRuleActive = ({
rule,
activePaths,
currentModel,
}: {
rule: Rule;
activePaths: string[];
currentModel: string;
}): boolean => {
if (typeof rule === "string") {
return true;
}
if (rule.if) {
return evaluateIf(rule.if, activePaths);
return evaluateIf({ condition: rule.if, activePaths, currentModel });
}
return true;