-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathflow-step.ts
More file actions
51 lines (47 loc) · 1.38 KB
/
flow-step.ts
File metadata and controls
51 lines (47 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { FlowAction } from "@/flows/types/flow-action";
export type FlowStep = {
name: string;
ask: string;
hiddenExecute?: boolean;
response?: string;
markdownEditor?: boolean;
cachedResponseRegex: string;
values: Record<string, string>;
preActions: FlowAction[];
postActions: FlowAction[];
};
export function fillStepWithValued(
step: FlowStep,
cachedValue: Record<number, any>,
): { replaced: boolean; ask: string } {
const regex = new RegExp(/\$\$([a-zA-Z0-9_]+)\$\$/);
let newValue = step.ask;
let isChanged = false;
// 2. find $$placeholder$$ in step.ask
if (step.ask && step.values) {
const matched = step.ask.match(regex);
if (matched) {
// 1. replace $$placeholder$$ with step.values.placeholder
const placeholder = matched[1];
const value = step.values[placeholder];
if (value) {
isChanged = true;
newValue = step.ask.replace(regex, value);
}
}
}
// 3. find value in cachedValue, format: $$response:1$$
if (step.ask && cachedValue) {
const regex = new RegExp(/\$\$response:([0-9]+)\$\$/);
const matched = step.ask.match(regex);
if (matched) {
const index = parseInt(matched[1]);
const value = cachedValue[index];
if (value) {
isChanged = true;
newValue = step.ask.replace(regex, value);
}
}
}
return { replaced: isChanged, ask: newValue };
}