MCPcopy
hub / github.com/promptfoo/promptfoo / maybeLoadResponseFormatFromExternalFile

Function maybeLoadResponseFormatFromExternalFile

src/util/file.ts:436–473  ·  view source on GitHub ↗
(
  responseFormat: any,
  vars?: Record<string, VarValue>,
)

Source from the content-addressed store, hash-verified

434 * @returns The processed response_format with all files loaded
435 */
436export function maybeLoadResponseFormatFromExternalFile(
437 responseFormat: any,
438 vars?: Record<string, VarValue>,
439): any {
440 if (responseFormat === undefined || responseFormat === null) {
441 return responseFormat;
442 }
443
444 // First, render variables and load the outer response_format
445 const rendered = renderVarsInObject(responseFormat, vars);
446 const loaded = maybeLoadFromExternalFile(rendered);
447
448 if (!loaded || typeof loaded !== 'object') {
449 return loaded;
450 }
451
452 // For json_schema type, check if the nested schema is a file reference
453 if (loaded.type === 'json_schema') {
454 const nestedSchema = loaded.schema || loaded.json_schema?.schema;
455
456 if (nestedSchema) {
457 // Render and load the nested schema
458 const loadedSchema = maybeLoadFromExternalFile(renderVarsInObject(nestedSchema, vars));
459
460 // Return with the loaded schema in place
461 if (loaded.schema !== undefined) {
462 return { ...loaded, schema: loadedSchema };
463 } else if (loaded.json_schema?.schema !== undefined) {
464 return {
465 ...loaded,
466 json_schema: { ...loaded.json_schema, schema: loadedSchema },
467 };
468 }
469 }
470 }
471
472 return loaded;
473}
474
475/**
476 * Renders variables in a tools object and loads from external file if applicable.

Callers 9

file.test.tsFile · 0.90
callApiInternalMethod · 0.90
buildResponsesBodyMethod · 0.85
getOpenAiBodyMethod · 0.85
getAzureResponsesBodyMethod · 0.85
callApiMethod · 0.85
getRequestBodyMethod · 0.85
getOpenAiBodyMethod · 0.85
getOpenAiBodyMethod · 0.85

Calls 2

renderVarsInObjectFunction · 0.90

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…