(agentId: string, systemPrompt: string, iterationId?: string)
| 320 | }; |
| 321 | |
| 322 | export async function preProcess(agentId: string, systemPrompt: string, iterationId?: string): Promise<PreProcessorResult> { |
| 323 | let modifiedPrompt = systemPrompt; |
| 324 | // A JSON System One request (decision models): text a sensor splices in ($MEMORY, $CLIPBOARD, ...) |
| 325 | // sits inside a JSON string, so it's escaped and a quote or newline can't break the parse. |
| 326 | // Image sensors replace their placeholder with '' and are unaffected. |
| 327 | const jsonPrompt = isJsonPrompt(systemPrompt); |
| 328 | const result: PreProcessorResult = { |
| 329 | modifiedPrompt, |
| 330 | images: [], |
| 331 | imageSources: {}, |
| 332 | microphone: "", |
| 333 | screenAudio: "", |
| 334 | allAudio: "" |
| 335 | }; |
| 336 | |
| 337 | try { |
| 338 | Logger.debug(agentId, 'Starting prompt pre-processing'); |
| 339 | |
| 340 | //if (/\$MICROPHONE/.test(systemPrompt)) { |
| 341 | // Logger.debug(agentId, '$MICROPHONE placeholder detected, ensuring recognition is started.'); |
| 342 | // try { |
| 343 | // await ensureRecognitionStarted(agentId); |
| 344 | // } catch (speechError: any) { |
| 345 | // Logger.error(agentId, `Failed to ensure speech recognition active in preProcess: ${speechError.message}`); |
| 346 | // modifiedPrompt = modifiedPrompt.replace(/\$MICROPHONE/g, `[Speech input unavailable: ${speechError.message}]`); |
| 347 | // } |
| 348 | //} |
| 349 | |
| 350 | for (const [key, processor] of Object.entries(processors)) { |
| 351 | //if (key === '$MICROPHONE' && modifiedPrompt.includes('[Speech input unavailable:')) { |
| 352 | // continue; |
| 353 | //} |
| 354 | |
| 355 | processor.regex.lastIndex = 0; |
| 356 | let match; |
| 357 | |
| 358 | // Need to re-construct the string in a way that doesn't invalidate ongoing regex exec |
| 359 | let currentSearchIndex = 0; |
| 360 | let tempPrompt = ""; |
| 361 | |
| 362 | while ((match = processor.regex.exec(modifiedPrompt)) !== null) { |
| 363 | const placeholder = match[0]; |
| 364 | const matchIndex = match.index; |
| 365 | |
| 366 | // Append the part of the string before the current match |
| 367 | tempPrompt += modifiedPrompt.substring(currentSearchIndex, matchIndex); |
| 368 | |
| 369 | Logger.debug(agentId, `Processing placeholder: ${placeholder} (at index ${matchIndex} using ${key})`); |
| 370 | const processorResult = await processor.handler(agentId, modifiedPrompt, match, iterationId); |
| 371 | |
| 372 | let replacement = placeholder; // Default to keeping the placeholder if no replacementText |
| 373 | if (processorResult.replacementText !== undefined) { |
| 374 | replacement = jsonPrompt ? jsonEscape(processorResult.replacementText) : processorResult.replacementText; |
| 375 | } |
| 376 | tempPrompt += replacement; |
| 377 | |
| 378 | currentSearchIndex = matchIndex + placeholder.length; |
| 379 | processor.regex.lastIndex = currentSearchIndex; // Ensure regex continues after this placeholder |
no test coverage detected