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