( configSystemInstruction: Content | string | undefined, contextVars?: Record<string, VarValue>, )
| 1044 | * @returns Processed Content object or undefined |
| 1045 | */ |
| 1046 | export function parseConfigSystemInstruction( |
| 1047 | configSystemInstruction: Content | string | undefined, |
| 1048 | contextVars?: Record<string, VarValue>, |
| 1049 | ): Content | undefined { |
| 1050 | if (!configSystemInstruction) { |
| 1051 | return undefined; |
| 1052 | } |
| 1053 | |
| 1054 | // Make a copy to avoid mutating the original |
| 1055 | let configInstruction = clone(configSystemInstruction); |
| 1056 | |
| 1057 | // Load systemInstruction from file if it's a file path |
| 1058 | if (typeof configSystemInstruction === 'string') { |
| 1059 | configInstruction = loadFile(configSystemInstruction, contextVars); |
| 1060 | } |
| 1061 | |
| 1062 | // Convert string to Content structure |
| 1063 | if (typeof configInstruction === 'string') { |
| 1064 | configInstruction = { parts: [{ text: configInstruction }] }; |
| 1065 | } |
| 1066 | |
| 1067 | // Render Nunjucks templates in all text parts |
| 1068 | if (contextVars && configInstruction) { |
| 1069 | const nunjucks = getNunjucksEngine(); |
| 1070 | for (const part of configInstruction.parts) { |
| 1071 | if (part.text) { |
| 1072 | try { |
| 1073 | part.text = nunjucks.renderString(part.text, contextVars); |
| 1074 | } catch (err) { |
| 1075 | throw new Error(`Unable to render nunjucks in systemInstruction: ${err}`); |
| 1076 | } |
| 1077 | } |
| 1078 | } |
| 1079 | } |
| 1080 | |
| 1081 | return configInstruction; |
| 1082 | } |
| 1083 | |
| 1084 | export function geminiFormatAndSystemInstructions( |
| 1085 | prompt: string, |
no test coverage detected
searching dependent graphs…