MCPcopy Create free account
hub / github.com/Superflows-AI/superflows / fillVariables

Function fillVariables

lib/v3/utils.ts:53–117  ·  view source on GitHub ↗
(
  requests: {
    text: string;
    embedded_text: string;
    variable_values: Record<string, any>;
    primary_question: boolean;
  }[],
  variables: Record<string, any>,
  embedAll: Record<string, string[]>,
)

Source from the content-addressed store, hash-verified

51}
52
53export function fillVariables(
54 requests: {
55 text: string;
56 embedded_text: string;
57 variable_values: Record<string, any>;
58 primary_question: boolean;
59 }[],
60 variables: Record<string, any>,
61 embedAll: Record<string, string[]>,
62): {
63 text: string;
64 embedded_text: string;
65 variable_values: Record<string, any>;
66 primary_question: boolean;
67}[] {
68 return requests
69 .map(({ text, embedded_text, primary_question, variable_values }) => {
70 const matched = embedded_text.match(/\{.*?}/) as RegExpMatchArray | null;
71 if (!matched)
72 return [{ text, embedded_text, primary_question, variable_values }];
73
74 const matchName = matched[0].slice(1, -1);
75 if (matchName in embedAll) {
76 const possibleValues = embedAll[matchName];
77 return possibleValues
78 .map((val) => {
79 const newText = embedded_text.replace(`{${matchName}}`, val);
80 return fillVariables(
81 [
82 {
83 text,
84 embedded_text: newText,
85 primary_question,
86 variable_values: { ...variable_values, [matchName]: val },
87 },
88 ],
89 variables,
90 embedAll,
91 );
92 })
93 .flat();
94 } else if (matchName in variables) {
95 // @ts-ignore
96 const val = variables[matchName];
97 return fillVariables(
98 [
99 {
100 text,
101 embedded_text: embedded_text.replace(
102 `{${matchName}}`,
103 Array.isArray(val) ? val.join(", ") : val,
104 ),
105 primary_question,
106 variable_values: { ...variable_values, [matchName]: val },
107 },
108 ],
109 variables,
110 embedAll,

Callers 2

handlerFunction · 0.90
utils.test.tsFile · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected