@@ -25,6 +25,16 @@ const ALLOWED_USER_FIELDS = [
25
25
'termsAccepted' ,
26
26
] as const ;
27
27
28
+ /**
29
+ * List of allowed request body fields that can be used in header placeholders.
30
+ * These are common fields from the request body that are safe to expose in headers.
31
+ */
32
+ const ALLOWED_BODY_FIELDS = [
33
+ 'conversationId' ,
34
+ 'parentMessageId' ,
35
+ 'messageId'
36
+ ] as const ;
37
+
28
38
/**
29
39
* Processes a string value to replace user field placeholders
30
40
* @param value - The string value to process
@@ -61,21 +71,46 @@ function processUserPlaceholders(value: string, user?: TUser): string {
61
71
return value ;
62
72
}
63
73
74
+ /**
75
+ * Processes a string value to replace request body field placeholders
76
+ * @param value - The string value to process
77
+ * @param body - The request body object
78
+ * @returns The processed string with placeholders replaced
79
+ */
80
+ function processBodyPlaceholders ( value : string , body : Record < string , any > ) : string {
81
+
82
+ for ( const field of ALLOWED_BODY_FIELDS ) {
83
+ const placeholder = `{{LIBRECHAT_BODY_${ field . toUpperCase ( ) } }}` ;
84
+ if ( ! value . includes ( placeholder ) ) {
85
+ continue ;
86
+ }
87
+
88
+ const fieldValue = body [ field ] ;
89
+ const replacementValue = fieldValue == null ? '' : String ( fieldValue ) ;
90
+ value = value . replace ( new RegExp ( placeholder , 'g' ) , replacementValue ) ;
91
+ }
92
+
93
+ return value ;
94
+ }
95
+
64
96
/**
65
97
* Processes a single string value by replacing various types of placeholders
66
98
* @param originalValue - The original string value to process
67
99
* @param customUserVars - Optional custom user variables to replace placeholders
68
100
* @param user - Optional user object for replacing user field placeholders
101
+ * @param body - Optional request body object for replacing body field placeholders
69
102
* @returns The processed string with all placeholders replaced
70
103
*/
71
104
function processSingleValue ( {
72
105
originalValue,
73
106
customUserVars,
74
107
user,
108
+ body = undefined ,
75
109
} : {
76
110
originalValue : string ;
77
111
customUserVars ?: Record < string , string > ;
78
112
user ?: TUser ;
113
+ body ?: Record < string , any > ;
79
114
} ) : string {
80
115
let value = originalValue ;
81
116
@@ -92,7 +127,12 @@ function processSingleValue({
92
127
// 2. Replace user field placeholders (e.g., {{LIBRECHAT_USER_EMAIL}}, {{LIBRECHAT_USER_ID}})
93
128
value = processUserPlaceholders ( value , user ) ;
94
129
95
- // 3. Replace system environment variables
130
+ // 3. Replace body field placeholders (e.g., {{LIBRECHAT_BODY_CONVERSATIONID}}, {{LIBRECHAT_BODY_PARENTMESSAGEID}})
131
+ if ( body ) {
132
+ value = processBodyPlaceholders ( value , body ) ;
133
+ }
134
+
135
+ // 4. Replace system environment variables
96
136
value = extractEnvVariable ( value ) ;
97
137
98
138
return value ;
@@ -151,16 +191,18 @@ export function processMCPEnv(
151
191
}
152
192
153
193
/**
154
- * Resolves header values by replacing user placeholders, custom variables, and environment variables
194
+ * Resolves header values by replacing user placeholders, custom variables, body variables, and environment variables
155
195
* @param headers - The headers object to process
156
196
* @param user - Optional user object for replacing user field placeholders (can be partial with just id)
157
197
* @param customUserVars - Optional custom user variables to replace placeholders
198
+ * @param body - Optional request body object for replacing body field placeholders
158
199
* @returns - The processed headers with all placeholders replaced
159
200
*/
160
201
export function resolveHeaders (
161
202
headers : Record < string , string > | undefined ,
162
203
user ?: Partial < TUser > | { id : string } ,
163
204
customUserVars ?: Record < string , string > ,
205
+ body ?: Record < string , any > ,
164
206
) {
165
207
const resolvedHeaders = { ...( headers ?? { } ) } ;
166
208
@@ -170,6 +212,7 @@ export function resolveHeaders(
170
212
originalValue : headers [ key ] ,
171
213
customUserVars,
172
214
user : user as TUser ,
215
+ body,
173
216
} ) ;
174
217
} ) ;
175
218
}
0 commit comments